Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse improvements rebase #29

Merged
merged 9 commits into from
Jan 8, 2025

Conversation

InnocentZero
Copy link
Contributor

Supersedes #24 as it had too many merge conflicts with main.

@InnocentZero InnocentZero mentioned this pull request Dec 17, 2024
@132ikl
Copy link
Contributor

132ikl commented Dec 17, 2024

Should the parser parse unit types? In nushell's current parser, it seems like units are parsed specially, but not parsed as nothing values, and I can't seem to find the code where they are parsed.

ast -f "echo hi; null"
# => ╭───┬─────────┬────────────────────┬────────────────╮
# => │ # │ content │       shape        │      span      │
# => ├───┼─────────┼────────────────────┼────────────────┤
# => │ 0 │ echo    │ shape_internalcall │ ╭───────┬───╮  │
# => │   │         │                    │ │ start │ 0 │  │
# => │   │         │                    │ │ end   │ 4 │  │
# => │   │         │                    │ ╰───────┴───╯  │
# => │ 1 │ hi      │ shape_string       │ ╭───────┬───╮  │
# => │   │         │                    │ │ start │ 5 │  │
# => │   │         │                    │ │ end   │ 7 │  │
# => │   │         │                    │ ╰───────┴───╯  │
# => │ 2 │ null    │ shape_nothing      │ ╭───────┬────╮ │
# => │   │         │                    │ │ start │ 9  │ │
# => │   │         │                    │ │ end   │ 13 │ │
# => │   │         │                    │ ╰───────┴────╯ │
# => ╰───┴─────────┴────────────────────┴────────────────╯
ast -f "echo hi; ()"
# => ╭───┬─────────┬────────────────────┬───────────────╮
# => │ # │ content │       shape        │     span      │
# => ├───┼─────────┼────────────────────┼───────────────┤
# => │ 0 │ echo    │ shape_internalcall │ ╭───────┬───╮ │
# => │   │         │                    │ │ start │ 0 │ │
# => │   │         │                    │ │ end   │ 4 │ │
# => │   │         │                    │ ╰───────┴───╯ │
# => │ 1 │ hi      │ shape_string       │ ╭───────┬───╮ │
# => │   │         │                    │ │ start │ 5 │ │
# => │   │         │                    │ │ end   │ 7 │ │
# => │   │         │                    │ ╰───────┴───╯ │
# => ╰───┴─────────┴────────────────────┴───────────────╯

@InnocentZero
Copy link
Contributor Author

Ah, I checked it via () | describe. That output nothing. Should it not be treated as one of nothing or none? Imo introducing a third type for just this doesn't make sense to me.

@132ikl
Copy link
Contributor

132ikl commented Dec 17, 2024

I think it's actually just an empty sub-expression. For example, 1 | () | describe outputs int. Also, ast -f "{;}" (empty block, with semicolon to differentiate from record) doesn't output anything, similarly to ast -f "()", so I'm guessing both of these are either not parsed as a node at all, or are pruned from the AST before being output.

@sholderbach
Copy link
Member

Yeah we don't have that "unit type" notion from Rust. Maybe we should iterate a bit on how the empty expression is handled to make checking for it easier but as of now, I don't think it makes sense to add it to the type system.

@InnocentZero
Copy link
Contributor Author

Yeah we don't have that "unit type" notion from Rust. Maybe we should iterate a bit on how the empty expression is handled to make checking for it easier but as of now, I don't think it makes sense to add it to the type system.

Ah, but without that it simply throws a compile error as of now.

@kubouch
Copy link
Collaborator

kubouch commented Dec 18, 2024

I'll review it later, but IMO empty subexpression should evaluate to nothing, same as missing else branch. Then, 'a' | () and 'a' | null are inconsistent in the current Nushell. The question also is whether to allow 'a' | () at all, since 'a' | is not allowed...

@InnocentZero
Copy link
Contributor Author

Just to clear everyone, I've only added a new AST Node, it still evaluates to nothing. This makes it easier than having an Option<NodeId> for an expression everywhere.

@132ikl
Copy link
Contributor

132ikl commented Dec 20, 2024

This makes it easier than having an Option for an expression everywhere.

I would think () should just be a subexpression block containing no statements/expressions. It shouldn't require any special handling like requiring every NodeId to be optional or a special node, it should just be a Block node with a block that contains no statements.

@InnocentZero
Copy link
Contributor Author

I think the above should work, but I'm not really sure of when to give the type as null and when to give the type as nothing 😅

@132ikl
Copy link
Contributor

132ikl commented Dec 23, 2024

If you're asking about when a node should return Type::None vs Type::Nothing, then Type::None should be used when the node is a statement and does not return any value (like let foo = 123), and Type::Nothing should be used when a node is an expression which returns a null value (like (), since () is a sub-expression and all expressions must return a value)

src/parser.rs Outdated Show resolved Hide resolved
src/parser.rs Outdated Show resolved Hide resolved
@kubouch
Copy link
Collaborator

kubouch commented Dec 28, 2024

  • The alias seems good.
  • while I think doesn't need the flags. With the new parser, the keywords become real parser keywords instead of keyword/command hybrids as in current Nushell. We could special-case <keyword> -h/--help to give a help message and detect special cases like def --env/--wrapped, but otherwise I think we don't need to track flags on keywords.
  • And I think we can just ban (), see the comment.

Sorry for the merge conflicts, you can add these to the updated parser without too much effort, just replace self.next() with self.tokens.advance().

Signed-off-by: innocentzero <[email protected]>
Signed-off-by: innocentzero <[email protected]>
Signed-off-by: innocentzero <[email protected]>
Signed-off-by: innocentzero <[email protected]>
Signed-off-by: innocentzero <[email protected]>
Signed-off-by: innocentzero <[email protected]>
@InnocentZero InnocentZero force-pushed the parse-improvements-rebase branch from 42a8e44 to c8219cb Compare December 28, 2024 21:19
@InnocentZero
Copy link
Contributor Author

@kubouch have a look. Should be good to go ig.

Copy link
Collaborator

@kubouch kubouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small details, looks good otherwise.

src/parser.rs Outdated Show resolved Hide resolved
src/parser.rs Outdated Show resolved Hide resolved
src/parser.rs Show resolved Hide resolved
Signed-off-by: innocentzero <[email protected]>
@InnocentZero
Copy link
Contributor Author

@kubouch apologies for the delay. Should be good now.

@kubouch
Copy link
Collaborator

kubouch commented Jan 8, 2025

OK, thanks!

@kubouch kubouch merged commit 4b92442 into nushell:main Jan 8, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants