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

Lex $foo as a single token, instead of using '$' IDENT #2681

Merged
merged 1 commit into from
Jul 9, 2023

Conversation

emanuele6
Copy link
Member

@emanuele6 emanuele6 commented Jul 9, 2023

Previously, variable/symbols were parsed as the combination of two tokens: '$' IDENT

This meant that using a keyword as variable name (e.g. $then, $label) did not work.
Attempts were made to allow $keyword to work by adding some '$' Keyword rules in the parser, but this did not allow $keyword in all places:

jq --arg label foo -n '$label' # ok

jq -n '"foo" as $label | .' # error

Treating $foo as a single token is much simpler, in my opinion.

This patch also changes how LOC is lexed: "$loc" instead of as "loc" that gets combined with '$' in the parser.

This patch also disallows having spaces after '$' when recalling a variable `$ foo' since that was probably just an unintentional side effect of the implementation, and it was not documented.

Fixes #2675 and fixes #526.

@emanuele6 emanuele6 requested a review from nicowilliams July 9, 2023 18:45
src/parser.y Outdated Show resolved Hide resolved
src/lexer.l Outdated Show resolved Hide resolved
@nicowilliams
Copy link
Contributor

We still need Keyword to include "label" for things like {label:"foo"}.

@emanuele6
Copy link
Member Author

emanuele6 commented Jul 9, 2023

I've only only renamed VARIABLE to BINDING and rebased the branch on top of master to make sure that c08ecba is actually working (it is =)).

@nicowilliams
Copy link
Contributor

LGTM. Anyone else want to chime in before we merge this?

@wader
Copy link
Member

wader commented Jul 9, 2023

LGMT. But could include some tests?

@nicowilliams
Copy link
Contributor

LGMT. But could include some tests?

What would a test here be, that $ foo fails?

@wader
Copy link
Member

wader commented Jul 9, 2023

I mostly was thinking of that keywords can now be used as binding, the whitespace thing might not be as important to test?

So maybe something like:

(. as $if | $if), (. as $label | $label)
123
123
123

@emanuele6
Copy link
Member Author

I added some tests

@emanuele6 emanuele6 force-pushed the vartoken branch 2 times, most recently from 4c139f8 to 2a7b623 Compare July 9, 2023 22:06
Previously, bindings were parsed as the combination of two tokens:
  '$' IDENT

This meant that using a keyword as variable name (e.g. $then, $label)
did not work.
Attempts were made to allow $keyword to work by adding some '$' Keyword
rules in the parser, but this did not allow $keyword in all places:

  jq --arg label foo -n '$label'  # ok

  jq -n '"foo" as $label | .'     # error

Treating $foo as a single token is much simpler, in my opinion.

This patch also changes how LOC is lexed: "$__loc__" instead of as
"__loc__" that gets combined with '$' in the parser.

This patch also disallows having spaces after '$' when recalling a
variable  `$ foo'  since that was probably just an unintentional side
effect of the implementation, and it was not documented.

Fixes jqlang#2675
@nicowilliams nicowilliams merged commit 193f432 into jqlang:master Jul 9, 2023
@nicowilliams
Copy link
Contributor

Thanks!

@emanuele6 emanuele6 deleted the vartoken branch July 9, 2023 23:09
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.

The keyword label can't be used as a binding name :( $end as a variable
3 participants