Skip to content

refactor(parser): parse_value, move reserved keyword values check - #16415

Merged
132ikl merged 3 commits into
nushell:mainfrom
blindFS:parse_value_refactor
Oct 16, 2025
Merged

132ikl merged 3 commits into
nushell:mainfrom
blindFS:parse_value_refactor

Conversation

@blindFS

@blindFS blindFS commented Aug 11, 2025 •

Copy link
Copy Markdown
Contributor

A PoC PR regarding the topic discussed in #16408

Description

Better readability in my opinion.

User-Facing Changes

Release notes summary

Fixes the inconsistent parsing of inf/INF, NaN/nan

Tests + Formatting

After Submitting

@github-actions github-actions Bot added the A:parser Issues related to parsing label Aug 11, 2025
@blindFS
blindFS marked this pull request as ready for review August 11, 2025 19:42
@kaathewisegit

kaathewisegit commented Aug 12, 2025 •

Copy link
Copy Markdown
Contributor

In general, I like this idea. But the semantics get kind of wobbly. This makes oneof dependent on the order of types, for example:

~> def test [arg: oneof<bool, string>] { $arg }
~> test true | describe 
bool
~> def test [arg: oneof<string, bool>] { $arg }
~> test true | describe 
string

I think it makes sense to coerce literals (and I think inf and nan would count as literals), but less so for keywords.

Rust and a few other languages have a distinction between strict and weak keywords. The former are forbidden everywhere while the latter are accepted outside of their position (so let union = 1 is valid Rust, even though union is a keyword, because it's weak).

With that in mind, I think true and false don't make much sense as weak keywords, because they can be used as arguments

@blindFS

blindFS commented Aug 12, 2025

Copy link
Copy Markdown
Contributor Author

@kaathewisegit Yeah, there're nasty edge cases of course.

Actually, I don't like this kind of context dependent parsing at all. I chose it just because it feels more consistent given how dynamic current nushell already is.

Back to the strict/week keywords topic, I don't see what's the difference between inf and false actually. We can further simplify your example to

def test [arg: oneof<string, int>] { $arg }; test 1 | describe # => string

which has nothing to do with keyword values.

I can make it behave closer to current release by an extra match branch like this (we should probably allow them to be parsed as path/directory anyway):

match shape {        
        ...
        // Check for reserved keyword values
        SyntaxShape::String
            if matches!(bytes, b"true" | b"false")
                || matches!(
                    bytes.to_ascii_lowercase().as_slice(),
                    b"-inf" | b"inf" | b"nan"
                ) =>
        {
            working_set.error(ParseError::Expected("string", span));
            Expression::garbage(working_set, span)
        }
        SyntaxShape::String => parse_string(working_set, span),
       ...
}

But again, it feels more confusing/inconsistent to me.

@blindFS

blindFS commented Aug 12, 2025

Copy link
Copy Markdown
Contributor Author

What's worse:

> def test [foo: string] {}; test 1 # no error
> let foo: string = 1

Error: nu::parser::type_mismatch

  × Type mismatch.
   ╭─[entry #10:1:19]
 1 │ let foo: string = 1
   ·                   ┬
   ·                   ╰── expected string, found int

@kaathewisegit

Copy link
Copy Markdown
Contributor

Yeah, in that regard following the existing rules sounds like the better idea.

I feel that Nushell really needs some kind of a formal-ish grammar. The current parsing situation is all over the place

@blindFS

blindFS commented Aug 12, 2025

Copy link
Copy Markdown
Contributor Author

Yeah, in that regard following the existing rules sounds like the better idea.

I feel that Nushell really needs some kind of a formal-ish grammar. The current parsing situation is all over the place

I have a lot of expectations for new-nu-parser.

@blindFS

blindFS commented Aug 13, 2025

Copy link
Copy Markdown
Contributor Author

Let's make it a non-breaking change in case some users actually use something like oneof<string, bool> in their scripts.

Comment thread crates/nu-parser/src/parser.rs Resolved
@blindFS
blindFS force-pushed the parse_value_refactor branch from 6edd7c5 to 86ebd34 Compare October 16, 2025 05:10
@132ikl
132ikl merged commit 649ee03 into nushell:main Oct 16, 2025
@132ikl 132ikl added notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes labels Oct 16, 2025
@github-actions github-actions Bot added this to the v0.109.0 milestone Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants