From: Nicolas Williams Date: Mon, 25 May 2015 18:16:45 +0000 (-0500) Subject: Keywords should be OK as object keys (fix #794) X-Git-Tag: jq-1.5rc2~89 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11f084f9a71ac5feacb563096aa68deb57a3bc41;p=jq Keywords should be OK as object keys (fix #794) With this change it's now OK to use keywords as object keys like so: {if:0} --- diff --git a/parser.y b/parser.y index 00d097c..029b879 100644 --- a/parser.y +++ b/parser.y @@ -108,6 +108,7 @@ struct lexer_param; %type Exp Term MkDict MkDictPair ExpD ElseBody QQString %type FuncDef FuncDefs String Import Imports Param Params %type Arg Args Module +%type Keyword %{ #include "lexer.h" struct lexer_param { @@ -735,6 +736,62 @@ Exp { $$ = gen_lambda($1); } +Keyword: +"as" { + $$ = jv_string("as"); +} | +"def" { + $$ = jv_string("def"); +} | +"module" { + $$ = jv_string("module"); +} | +"import" { + $$ = jv_string("import"); +} | +"if" { + $$ = jv_string("if"); +} | +"then" { + $$ = jv_string("then"); +} | +"else" { + $$ = jv_string("else"); +} | +"elif" { + $$ = jv_string("elif"); +} | +"reduce" { + $$ = jv_string("reduce"); +} | +"foreach" { + $$ = jv_string("foreach"); +} | +"end" { + $$ = jv_string("end"); +} | +"and" { + $$ = jv_string("and"); +} | +"or" { + $$ = jv_string("or"); +} | +"try" { + $$ = jv_string("try"); +} | +"catch" { + $$ = jv_string("catch"); +} | +"label" { + $$ = jv_string("label"); +} | +"break" { + $$ = jv_string("break"); +} | +"__loc__" { + $$ = jv_string("__loc__"); +} + MkDict: %empty { $$=gen_noop(); @@ -743,10 +800,13 @@ MkDict: | MkDictPair ',' MkDict { $$=block_join($1, $3); } | error ',' MkDict { $$ = $3; } -MkDictPair -: IDENT ':' ExpD { +MkDictPair: +IDENT ':' ExpD { $$ = gen_dictpair(gen_const($1), $3); } +| Keyword ':' ExpD { + $$ = gen_dictpair(gen_const($1), $3); + } | String ':' ExpD { $$ = gen_dictpair($1, $3); } diff --git a/tests/all.test b/tests/all.test index 950801a..0511597 100644 --- a/tests/all.test +++ b/tests/all.test @@ -1185,3 +1185,7 @@ try join(",") catch . ["1","2",{"a":{"b":{"c":33}}}] "string (\",\") and object ({\"a\":{\"b\":{...) cannot be added" +{if:0,and:1,or:2,then:3,else:4,elif:5,end:6,as:7,def:8,reduce:9,foreach:10,try:11,catch:12,label:13,import:14,module:15} +null +{"if":0,"and":1,"or":2,"then":3,"else":4,"elif":5,"end":6,"as":7,"def":8,"reduce":9,"foreach":10,"try":11,"catch":12,"label":13,"import":14,"module":15} +