Skip to content

Commit

Permalink
Lex $foo as a single token, instead of using '$' IDENT
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emanuele6 committed Jul 9, 2023
1 parent c08ecba commit 4c139f8
Show file tree
Hide file tree
Showing 7 changed files with 1,584 additions and 1,617 deletions.
339 changes: 174 additions & 165 deletions src/lexer.c

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions src/lexer.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct lexer_param;
"catch" { return CATCH; }
"label" { return LABEL; }
"break" { return BREAK; }
"__loc__" { return LOC; }
"$__loc__" { return LOC; }
"|=" { return SETPIPE; }
"+=" { return SETPLUS; }
"-=" { return SETMINUS; }
Expand Down Expand Up @@ -122,6 +122,7 @@ struct lexer_param;

([a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext); return IDENT;}
\.[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext+1); return FIELD;}
\$([a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext+1); return BINDING;}

[ \n\t]+ {}

Expand Down
Loading

0 comments on commit 4c139f8

Please sign in to comment.