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

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 #2675
This commit is contained in:
Emanuele Torre authored and Nico Williams committed 2023-07-09 17:23:19 -05:00
1 parent c08ecbaf23
commit 193f432e08
7 files changed
+1574 -1608

No files matched your search

Generated
+87 -85
View File
@@ -76,48 +76,49 @@ struct lexer_param;
INVALID_CHARACTER = 258, /* INVALID_CHARACTER */
IDENT = 259, /* IDENT */
FIELD = 260, /* FIELD */
LITERAL = 261, /* LITERAL */
FORMAT = 262, /* FORMAT */
REC = 263, /* ".." */
SETMOD = 264, /* "%=" */
EQ = 265, /* "==" */
NEQ = 266, /* "!=" */
DEFINEDOR = 267, /* "//" */
AS = 268, /* "as" */
DEF = 269, /* "def" */
MODULE = 270, /* "module" */
IMPORT = 271, /* "import" */
INCLUDE = 272, /* "include" */
IF = 273, /* "if" */
THEN = 274, /* "then" */
ELSE = 275, /* "else" */
ELSE_IF = 276, /* "elif" */
REDUCE = 277, /* "reduce" */
FOREACH = 278, /* "foreach" */
END = 279, /* "end" */
AND = 280, /* "and" */
OR = 281, /* "or" */
TRY = 282, /* "try" */
CATCH = 283, /* "catch" */
LABEL = 284, /* "label" */
BREAK = 285, /* "break" */
LOC = 286, /* "__loc__" */
SETPIPE = 287, /* "|=" */
SETPLUS = 288, /* "+=" */
SETMINUS = 289, /* "-=" */
SETMULT = 290, /* "*=" */
SETDIV = 291, /* "/=" */
SETDEFINEDOR = 292, /* "//=" */
LESSEQ = 293, /* "<=" */
GREATEREQ = 294, /* ">=" */
ALTERNATION = 295, /* "?//" */
QQSTRING_START = 296, /* QQSTRING_START */
QQSTRING_TEXT = 297, /* QQSTRING_TEXT */
QQSTRING_INTERP_START = 298, /* QQSTRING_INTERP_START */
QQSTRING_INTERP_END = 299, /* QQSTRING_INTERP_END */
QQSTRING_END = 300, /* QQSTRING_END */
FUNCDEF = 301, /* FUNCDEF */
NONOPT = 302 /* NONOPT */
BINDING = 261, /* BINDING */
LITERAL = 262, /* LITERAL */
FORMAT = 263, /* FORMAT */
REC = 264, /* ".." */
SETMOD = 265, /* "%=" */
EQ = 266, /* "==" */
NEQ = 267, /* "!=" */
DEFINEDOR = 268, /* "//" */
AS = 269, /* "as" */
DEF = 270, /* "def" */
MODULE = 271, /* "module" */
IMPORT = 272, /* "import" */
INCLUDE = 273, /* "include" */
IF = 274, /* "if" */
THEN = 275, /* "then" */
ELSE = 276, /* "else" */
ELSE_IF = 277, /* "elif" */
REDUCE = 278, /* "reduce" */
FOREACH = 279, /* "foreach" */
END = 280, /* "end" */
AND = 281, /* "and" */
OR = 282, /* "or" */
TRY = 283, /* "try" */
CATCH = 284, /* "catch" */
LABEL = 285, /* "label" */
BREAK = 286, /* "break" */
LOC = 287, /* "$__loc__" */
SETPIPE = 288, /* "|=" */
SETPLUS = 289, /* "+=" */
SETMINUS = 290, /* "-=" */
SETMULT = 291, /* "*=" */
SETDIV = 292, /* "/=" */
SETDEFINEDOR = 293, /* "//=" */
LESSEQ = 294, /* "<=" */
GREATEREQ = 295, /* ">=" */
ALTERNATION = 296, /* "?//" */
QQSTRING_START = 297, /* QQSTRING_START */
QQSTRING_TEXT = 298, /* QQSTRING_TEXT */
QQSTRING_INTERP_START = 299, /* QQSTRING_INTERP_START */
QQSTRING_INTERP_END = 300, /* QQSTRING_INTERP_END */
QQSTRING_END = 301, /* QQSTRING_END */
FUNCDEF = 302, /* FUNCDEF */
NONOPT = 303 /* NONOPT */
};
typedef enum yytokentype yytoken_kind_t;
#endif
@@ -129,48 +130,49 @@ struct lexer_param;
#define INVALID_CHARACTER 258
#define IDENT 259
#define FIELD 260
#define LITERAL 261
#define FORMAT 262
#define REC 263
#define SETMOD 264
#define EQ 265
#define NEQ 266
#define DEFINEDOR 267
#define AS 268
#define DEF 269
#define MODULE 270
#define IMPORT 271
#define INCLUDE 272
#define IF 273
#define THEN 274
#define ELSE 275
#define ELSE_IF 276
#define REDUCE 277
#define FOREACH 278
#define END 279
#define AND 280
#define OR 281
#define TRY 282
#define CATCH 283
#define LABEL 284
#define BREAK 285
#define LOC 286
#define SETPIPE 287
#define SETPLUS 288
#define SETMINUS 289
#define SETMULT 290
#define SETDIV 291
#define SETDEFINEDOR 292
#define LESSEQ 293
#define GREATEREQ 294
#define ALTERNATION 295
#define QQSTRING_START 296
#define QQSTRING_TEXT 297
#define QQSTRING_INTERP_START 298
#define QQSTRING_INTERP_END 299
#define QQSTRING_END 300
#define FUNCDEF 301
#define NONOPT 302
#define BINDING 261
#define LITERAL 262
#define FORMAT 263
#define REC 264
#define SETMOD 265
#define EQ 266
#define NEQ 267
#define DEFINEDOR 268
#define AS 269
#define DEF 270
#define MODULE 271
#define IMPORT 272
#define INCLUDE 273
#define IF 274
#define THEN 275
#define ELSE 276
#define ELSE_IF 277
#define REDUCE 278
#define FOREACH 279
#define END 280
#define AND 281
#define OR 282
#define TRY 283
#define CATCH 284
#define LABEL 285
#define BREAK 286
#define LOC 287
#define SETPIPE 288
#define SETPLUS 289
#define SETMINUS 290
#define SETMULT 291
#define SETDIV 292
#define SETDEFINEDOR 293
#define LESSEQ 294
#define GREATEREQ 295
#define ALTERNATION 296
#define QQSTRING_START 297
#define QQSTRING_TEXT 298
#define QQSTRING_INTERP_START 299
#define QQSTRING_INTERP_END 300
#define QQSTRING_END 301
#define FUNCDEF 302
#define NONOPT 303
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
@@ -181,7 +183,7 @@ union YYSTYPE
jv literal;
block blk;
#line 185 "src/parser.h"
#line 187 "src/parser.h"
};
typedef union YYSTYPE YYSTYPE;