-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Rework and fix Attrpath and Dynamic parsing #106
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! This has been on my todo list for a bit :)
The name of NODE_KEY is weird to me. It's called attrpath in Nix and in nixpkgs.
Should we rename it?
+1 to renaming
a or b is Apply, not "OrDefault", which matches the result of Nix.
Does this fix #23?
Yes. I referenced it in the description now. |
I'd prefer to keep this PR focused on parsing fixes, and leave the renaming to another PR later. Maybe after the refactoring of high-level AST in #105. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last thing from me: Theimpl TryFrom<SyntaxNode> for ParsedType
should be updated to account for ParsedType::HasAttr
as well.
@oxalica This looks good. I'll merge after it's rebased |
Current handling of Attrpath and Dynamic `${` is mostly incorrect. - The dynamic attribute `${` can only appear as an Attr inside an Attrpath, and itself cannot make up an expression. But currently we parse it as a kind of Expr. This commit reject this usage. - Dot separated attributes, aka. Attrpath or Key, can appear in 3 places: 1. After a "." in a Select expression `<expression>.<attrpath>` https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L439 2. After a "?" in a HasAttr expression `<expression> ? <attrpath>` https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L416 3. As a binding key in Let or Attrset-like expression `<attrpath> = <expression>;` https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L541 To make it consistent, all usage above are now wrapped in a NODE_ATTRPATH, which consists of multiple {NODE_DYNAMIC,NODE_IDENT,NODE_STRING} interspersed by TOKEN_DOT. NODE_KEY_VALUE is also renamed to NODE_ATTRPATH_VALUE. Benefiting from flat Attrpath instead of nested one, we can now get rid of NODE_OR_DEFAULT, since it can be trivially distinguished by checking if there is an `or` in NODE_ATTRPATH_VALUE.
This |
It looks like we are parsing |
|
Yes, it's still not perfect but maybe it's kind of off-topic for this PR.
No "only". It's also valid in attrpath
And the "fallback to ident" takes precedence over left-associative of Apply. 🤯
|
I was only highlighting the differences, we obviously already parse this correctly
Thanks bro
Yeah unfortunate. That's what happens when you put call inside of inside of |
@@ -7,6 +7,7 @@ macro_rules! T { | |||
(in) => ($crate::SyntaxKind::TOKEN_IN); | |||
(inherit) => ($crate::SyntaxKind::TOKEN_INHERIT); | |||
(let) => ($crate::SyntaxKind::TOKEN_LET); | |||
(or) => ($crate::SyntaxKind::TOKEN_OR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this here?
I created #116 for the fix. BTW: You are able to push to my PR branch before merging it, if you have write access to the repo, I think? |
Summary & Motivation
Rework and fix Attrpath and Dynamic parsing
Current handling of Attrpath and Dynamic
${
is mostly incorrect.The dynamic attribute
${
can only appear as an Attr inside an Attrpath,and itself cannot make up an expression.
But currently we parse it as a kind of Expr.
This commit reject this usage.
Dot separated attributes, aka. Attrpath or Key, can appear in 3 places:
After a "." in a Select expression
<expression>.<attrpath>
https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L439
After a "?" in a HasAttr expression
<expression> ? <attrpath>
https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L416
As a binding key in Let or Attrset-like expression
<attrpath> = <expression>;
https://github.com/NixOS/nix/blob/2.10.3/src/libexpr/parser.y#L541
To make it consistent, all usage above are now wrapped in a
NODE_ATTRPATH,
which consists of multiple {NODE_DYNAMIC,NODE_IDENT,NODE_STRING}
interspersed by TOKEN_DOT.
NODE_KEY_VALUE is also renamed to NODE_ATTRPATH_VALUE.
Benefiting from flat Attrpath instead of nested one, we can now get
rid of NODE_OR_DEFAULT, since it can be trivially distinguished by
checking if there is an
or
in NODE_ATTRPATH_VALUE.Backwards-incompatible changes
NODE_OR_DEFAULT
is removedNODE_KEY
->NODE_ATTRPATH
,NODE_KEY_VALUE
->NODE_ATTRPATH_VALUE
BinOp::IsSet
is removed since it's actually not a normal binary operator.NODE_SELECT
is flatten in favor ofNODE_ATTRPATH
which consists of multiple Attrs, includinga.b.c
anda.b.c or expr
a or b
is Apply, not "OrDefault", which matches the result of Nix. Fixes Doesn't parse valid Nix edge-cases #23${
is considered invalid at Expr places now, which matches the result of Nix.Further context
The name ofNODE_KEY
is weird to me. It's calledattrpath
in Nix and in nixpkgs.Should we rename it? This would also cause API breakage.Renamed.