Skip to content
Myr

parser.y

This file

Path

src/parser.y

1,014 lines · 22.3 KiB

Last changed by

Constant fold all kinds of constantsEmanuele Torrebefore adoption3b6e7dd

History of this file

Take it

1%{2#include <assert.h>3#include <math.h>4#include <stdio.h>5#include <string.h>6#include "compile.h"7#include "jv_alloc.h"8#include "builtin.h"9#define YYMALLOC jv_mem_alloc10#define YYFREE jv_mem_free11%}12%code requires {13#include "locfile.h"14struct lexer_param;1516#define YYLTYPE location17#define YYLLOC_DEFAULT(Loc, Rhs, N)             \18  do {                                          \19    if (N) {                                    \20      (Loc).start = YYRHSLOC(Rhs, 1).start;     \21      (Loc).end = YYRHSLOC(Rhs, N).end;         \22    } else {                                    \23      (Loc).start = YYRHSLOC(Rhs, 0).end;       \24      (Loc).end = YYRHSLOC(Rhs, 0).end;         \25    }                                           \26  } while (0)27}2829%locations30%define parse.error verbose31%define api.pure32%union {33  jv literal;34  block blk;35}3637%destructor { jv_free($$); } <literal>38%destructor { block_free($$); } <blk>3940%parse-param {block* answer}41%parse-param {int* errors}42%parse-param {struct locfile* locations}43%parse-param {struct lexer_param* lexer_param_ptr}44%lex-param {block* answer}45%lex-param {int* errors}46%lex-param {struct locfile* locations}47%lex-param {struct lexer_param* lexer_param_ptr}484950%token INVALID_CHARACTER51%token <literal> IDENT52%token <literal> FIELD53%token <literal> BINDING54%token <literal> LITERAL55%token <literal> FORMAT56%token REC ".."57%token SETMOD "%="58%token EQ "=="59%token NEQ "!="60%token DEFINEDOR "//"61%token AS "as"62%token DEF "def"63%token MODULE "module"64%token IMPORT "import"65%token INCLUDE "include"66%token IF "if"67%token THEN "then"68%token ELSE "else"69%token ELSE_IF "elif"70%token REDUCE "reduce"71%token FOREACH "foreach"72%token END "end"73%token AND "and"74%token OR "or"75%token TRY "try"76%token CATCH "catch"77%token LABEL "label"78%token BREAK "break"79%token LOC "$__loc__"80%token SETPIPE "|="81%token SETPLUS "+="82%token SETMINUS "-="83%token SETMULT "*="84%token SETDIV "/="85%token SETDEFINEDOR "//="86%token LESSEQ "<="87%token GREATEREQ ">="88%token ALTERNATION "?//"8990%token QQSTRING_START91%token <literal> QQSTRING_TEXT92%token QQSTRING_INTERP_START93%token QQSTRING_INTERP_END94%token QQSTRING_END9596/* Instead of raising this, find a way to use precedence to resolve97 * shift-reduce conflicts. */98%expect 099100%precedence FUNCDEF101%right '|'102%left ','103%right "//"104%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETMOD SETDEFINEDOR105%left OR106%left AND107%nonassoc NEQ EQ '<' '>' LESSEQ GREATEREQ108%left '+' '-'109%left '*' '/' '%'110%precedence NONOPT /* non-optional; rules for which a specialized111                      '?' rule should be preferred over Exp '?' */112%precedence '?'113%precedence "try"114%precedence "catch"115116117%type <blk> Exp Term118%type <blk> MkDict MkDictPair ExpD119%type <blk> ElseBody120%type <blk> String QQString121%type <blk> FuncDef FuncDefs122%type <blk> Module Import Imports ImportWhat ImportFrom123%type <blk> Param Params Arg Args124%type <blk> Patterns RepPatterns Pattern ArrayPats ObjPats ObjPat125%type <literal> Keyword126%type <literal> StringStart127%{128#include "lexer.h"129struct lexer_param {130  yyscan_t lexer;131};132#define FAIL(loc, msg)                                             \133  do {                                                             \134    location l = loc;                                              \135    yyerror(&l, answer, errors, locations, lexer_param_ptr, msg);  \136    /*YYERROR*/;                                                   \137  } while (0)138139void yyerror(YYLTYPE* loc, block* answer, int* errors,140             struct locfile* locations, struct lexer_param* lexer_param_ptr, const char *s){141  (*errors)++;142  if (strstr(s, "unexpected")) {143#ifdef WIN32144      locfile_locate(locations, *loc, "jq: error: %s (Windows cmd shell quoting issues?)", s);145#else146      locfile_locate(locations, *loc, "jq: error: %s (Unix shell quoting issues?)", s);147#endif148  } else {149      locfile_locate(locations, *loc, "jq: error: %s", s);150  }151}152153int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,154          struct locfile* locations, struct lexer_param* lexer_param_ptr) {155  yyscan_t lexer = lexer_param_ptr->lexer;156  int tok = jq_yylex(yylval, yylloc, lexer);157  if ((tok == LITERAL || tok == QQSTRING_TEXT) && !jv_is_valid(yylval->literal)) {158    jv msg = jv_invalid_get_msg(jv_copy(yylval->literal));159    if (jv_get_kind(msg) == JV_KIND_STRING) {160      FAIL(*yylloc, jv_string_value(msg));161    } else {162      FAIL(*yylloc, "Invalid literal");163    }164    jv_free(msg);165    jv_free(yylval->literal);166    yylval->literal = jv_null();167  }168  return tok;169}170171/* Returns string message if the block is a constant that is not valid as an172 * object key. */173static jv check_object_key(block k) {174  if (block_is_const(k) && block_const_kind(k) != JV_KIND_STRING) {175    char errbuf[15];176    return jv_string_fmt("Cannot use %s (%s) as object key",177        jv_kind_name(block_const_kind(k)),178        jv_dump_string_trunc(block_const(k), errbuf, sizeof(errbuf)));179  }180  return jv_invalid();181}182183static block gen_index(block obj, block key) {184  return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX));185}186187static block gen_index_opt(block obj, block key) {188  return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX_OPT));189}190191static block gen_slice_index(block obj, block start, block end, opcode idx_op) {192  block key = BLOCK(gen_subexp(gen_const(jv_object())),193                    gen_subexp(gen_const(jv_string("start"))),194                    gen_subexp(start),195                    gen_op_simple(INSERT),196                    gen_subexp(gen_const(jv_string("end"))),197                    gen_subexp(end),198                    gen_op_simple(INSERT));199  return BLOCK(key, obj, gen_op_simple(idx_op));200}201202static block constant_fold(block a, block b, int op) {203  if (!block_is_single(a) || !block_is_const(a) ||204      !block_is_single(b) || !block_is_const(b))205    return gen_noop();206207  jv jv_a = block_const(a);208  block_free(a);209  jv jv_b = block_const(b);210  block_free(b);211212  jv res = jv_invalid();213  switch (op) {214  case '+': res = binop_plus(jv_a, jv_b); break;215  case '-': res = binop_minus(jv_a, jv_b); break;216  case '*': res = binop_multiply(jv_a, jv_b); break;217  case '/': res = binop_divide(jv_a, jv_b); break;218  case '%': res = binop_mod(jv_a, jv_b); break;219  case EQ: res = binop_equal(jv_a, jv_b); break;220  case NEQ: res = binop_notequal(jv_a, jv_b); break;221  case '<': res = binop_less(jv_a, jv_b); break;222  case '>': res = binop_greater(jv_a, jv_b); break;223  case LESSEQ: res = binop_lesseq(jv_a, jv_b); break;224  case GREATEREQ: res = binop_greatereq(jv_a, jv_b); break;225  }226227  if (jv_is_valid(res))228    return gen_const(res);229230  return gen_error(jv_invalid_get_msg(res));231}232233static block gen_binop(block a, block b, int op) {234  block folded = constant_fold(a, b, op);235  if (!block_is_noop(folded))236    return folded;237238  const char* funcname = 0;239  switch (op) {240  case '+': funcname = "_plus"; break;241  case '-': funcname = "_minus"; break;242  case '*': funcname = "_multiply"; break;243  case '/': funcname = "_divide"; break;244  case '%': funcname = "_mod"; break;245  case EQ: funcname = "_equal"; break;246  case NEQ: funcname = "_notequal"; break;247  case '<': funcname = "_less"; break;248  case '>': funcname = "_greater"; break;249  case LESSEQ: funcname = "_lesseq"; break;250  case GREATEREQ: funcname = "_greatereq"; break;251  }252  assert(funcname);253254  return gen_call(funcname, BLOCK(gen_lambda(a), gen_lambda(b)));255}256257static block gen_format(block a, jv fmt) {258  return BLOCK(a, gen_call("format", gen_lambda(gen_const(fmt))));259}260261static block gen_definedor_assign(block object, block val) {262  block tmp = gen_op_var_fresh(STOREV, "tmp");263  return BLOCK(gen_op_simple(DUP),264               val, tmp,265               gen_call("_modify", BLOCK(gen_lambda(object),266                                         gen_lambda(gen_definedor(gen_noop(),267                                                                  gen_op_bound(LOADV, tmp))))));268}269270static block gen_update(block object, block val, int optype) {271  block tmp = gen_op_var_fresh(STOREV, "tmp");272  return BLOCK(gen_op_simple(DUP),273               val,274               tmp,275               gen_call("_modify", BLOCK(gen_lambda(object),276                                         gen_lambda(gen_binop(gen_noop(),277                                                              gen_op_bound(LOADV, tmp),278                                                              optype)))));279}280281static block gen_loc_object(location *loc, struct locfile *locations) {282  return gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),283                             jv_string("line"), jv_number(locfile_get_line(locations, loc->start) + 1)));284}285286%}287288%%289TopLevel:290Module Imports Exp {291  *answer = BLOCK($1, $2, gen_op_simple(TOP), $3);292} |293Module Imports FuncDefs {294  *answer = BLOCK($1, $2, $3);295}296297Module:298%empty {299  $$ = gen_noop();300} |301"module" Exp ';' {302  if (!block_is_const($2)) {303    FAIL(@$, "Module metadata must be constant");304    $$ = gen_noop();305    block_free($2);306  } else if (block_const_kind($2) != JV_KIND_OBJECT) {307    FAIL(@$, "Module metadata must be an object");308    $$ = gen_noop();309    block_free($2);310  } else {311    $$ = gen_module($2);312  }313}314315Imports:316%empty {317  $$ = gen_noop();318} |319Import Imports {320  $$ = BLOCK($1, $2);321}322323FuncDefs:324%empty {325  $$ = gen_noop();326} |327FuncDef FuncDefs {328  $$ = block_join($1, $2);329}330331Exp:332FuncDef Exp %prec FUNCDEF {333  $$ = block_bind_referenced($1, $2, OP_IS_CALL_PSEUDO);334} |335336Term "as" Patterns '|' Exp {337  $$ = gen_destructure($1, $3, $5);338} |339"reduce" Term "as" Patterns '(' Exp ';' Exp ')' {340  $$ = gen_reduce($2, $4, $6, $8);341} |342343"foreach" Term "as" Patterns '(' Exp ';' Exp ';' Exp ')' {344  $$ = gen_foreach($2, $4, $6, $8, $10);345} |346347"foreach" Term "as" Patterns '(' Exp ';' Exp ')' {348  $$ = gen_foreach($2, $4, $6, $8, gen_noop());349} |350351"if" Exp "then" Exp ElseBody {352  $$ = gen_cond($2, $4, $5);353} |354"if" Exp "then" error {355  FAIL(@$, "Possibly unterminated 'if' statement");356  $$ = $2;357} |358359"try" Exp "catch" Exp {360  $$ = gen_try($2, $4);361} |362"try" Exp {363  $$ = gen_try($2, gen_op_simple(BACKTRACK));364} |365"try" Exp "catch" error {366  FAIL(@$, "Possibly unterminated 'try' statement");367  $$ = $2;368} |369370"label" BINDING '|' Exp {371  jv v = jv_string_fmt("*label-%s", jv_string_value($2));372  $$ = gen_location(@$, locations, gen_label(jv_string_value(v), $4));373  jv_free($2);374  jv_free(v);375} |376377Exp '?' {378  $$ = gen_try($1, gen_op_simple(BACKTRACK));379} |380381Exp '=' Exp {382  $$ = gen_call("_assign", BLOCK(gen_lambda($1), gen_lambda($3)));383} |384385Exp "or" Exp {386  $$ = gen_or($1, $3);387} |388389Exp "and" Exp {390  $$ = gen_and($1, $3);391} |392393Exp "//" Exp {394  $$ = gen_definedor($1, $3);395} |396397Exp "//=" Exp {398  $$ = gen_definedor_assign($1, $3);399} |400401Exp "|=" Exp {402  $$ = gen_call("_modify", BLOCK(gen_lambda($1), gen_lambda($3)));403} |404405Exp '|' Exp {406  $$ = block_join($1, $3);407} |408409Exp ',' Exp {410  $$ = gen_both($1, $3);411} |412413Exp '+' Exp {414  $$ = gen_binop($1, $3, '+');415} |416417Exp "+=" Exp {418  $$ = gen_update($1, $3, '+');419} |420421'-' Exp {422  $$ = BLOCK($2, gen_call("_negate", gen_noop()));423} |424425Exp '-' Exp {426  $$ = gen_binop($1, $3, '-');427} |428429Exp "-=" Exp {430  $$ = gen_update($1, $3, '-');431} |432433Exp '*' Exp {434  $$ = gen_binop($1, $3, '*');435} |436437Exp "*=" Exp {438  $$ = gen_update($1, $3, '*');439} |440441Exp '/' Exp {442  $$ = gen_binop($1, $3, '/');443} |444445Exp '%' Exp {446  $$ = gen_binop($1, $3, '%');447} |448449Exp "/=" Exp {450  $$ = gen_update($1, $3, '/');451} |452453Exp SETMOD Exp {454  $$ = gen_update($1, $3, '%');455} |456457Exp "==" Exp {458  $$ = gen_binop($1, $3, EQ);459} |460461Exp "!=" Exp {462  $$ = gen_binop($1, $3, NEQ);463} |464465Exp '<' Exp {466  $$ = gen_binop($1, $3, '<');467} |468469Exp '>' Exp {470  $$ = gen_binop($1, $3, '>');471} |472473Exp "<=" Exp {474  $$ = gen_binop($1, $3, LESSEQ);475} |476477Exp ">=" Exp {478  $$ = gen_binop($1, $3, GREATEREQ);479} |480481Term {482  $$ = $1;483}484485Import:486ImportWhat ';' {487  $$ = $1;488} |489ImportWhat Exp ';' {490  if (!block_is_const($2)) {491    FAIL(@$, "Module metadata must be constant");492    $$ = gen_noop();493    block_free($1);494    block_free($2);495  } else if (block_const_kind($2) != JV_KIND_OBJECT) {496    FAIL(@$, "Module metadata must be an object");497    $$ = gen_noop();498    block_free($1);499    block_free($2);500  } else {501    $$ = gen_import_meta($1, $2);502  }503}504505ImportWhat:506"import" ImportFrom "as" BINDING {507  jv v = block_const($2);508  // XXX Make gen_import take only blocks and the int is_data so we509  // don't have to free so much stuff here510  $$ = gen_import(jv_string_value(v), jv_string_value($4), 1);511  block_free($2);512  jv_free($4);513  jv_free(v);514} |515"import" ImportFrom "as" IDENT {516  jv v = block_const($2);517  $$ = gen_import(jv_string_value(v), jv_string_value($4), 0);518  block_free($2);519  jv_free($4);520  jv_free(v);521} |522"include" ImportFrom {523  jv v = block_const($2);524  $$ = gen_import(jv_string_value(v), NULL, 0);525  block_free($2);526  jv_free(v);527}528529ImportFrom:530String {531  if (!block_is_const($1)) {532    FAIL(@$, "Import path must be constant");533    $$ = gen_const(jv_string(""));534    block_free($1);535  } else {536    $$ = $1;537  }538}539540FuncDef:541"def" IDENT ':' Exp ';' {542  $$ = gen_function(jv_string_value($2), gen_noop(), $4);543  jv_free($2);544} |545546"def" IDENT '(' Params ')' ':' Exp ';' {547  $$ = gen_function(jv_string_value($2), $4, $7);548  jv_free($2);549}550551Params:552Param {553  $$ = $1;554} |555Params ';' Param {556  $$ = BLOCK($1, $3);557}558559Param:560BINDING {561  $$ = gen_param_regular(jv_string_value($1));562  jv_free($1);563} |564IDENT {565  $$ = gen_param(jv_string_value($1));566  jv_free($1);567}568569570StringStart:571FORMAT QQSTRING_START {572  $$ = $1;573} |574QQSTRING_START {575  $$ = jv_string("text");576}577578579String:580StringStart QQString QQSTRING_END {581  $$ = $2;582  jv_free($1);583};584585586QQString:587%empty {588  $$ = gen_const(jv_string(""));589} |590QQString QQSTRING_TEXT {591  $$ = gen_binop($1, gen_const($2), '+');592} |593QQString QQSTRING_INTERP_START Exp QQSTRING_INTERP_END {594  $$ = gen_binop($1, gen_format($3, jv_copy($<literal>0)), '+');595}596597598ElseBody:599"elif" Exp "then" Exp ElseBody {600  $$ = gen_cond($2, $4, $5);601} |602"else" Exp "end" {603  $$ = $2;604} |605"end" {606  $$ = gen_noop();607}608609ExpD:610ExpD '|' ExpD {611  $$ = block_join($1, $3);612} |613'-' ExpD {614  $$ = BLOCK($2, gen_call("_negate", gen_noop()));615} |616Term {617  $$ = $1;618}619620621Term:622'.' {623  $$ = gen_noop();624} |625REC {626  $$ = gen_call("recurse", gen_noop());627} |628BREAK BINDING {629  jv v = jv_string_fmt("*label-%s", jv_string_value($2));     // impossible symbol630  $$ = gen_location(@$, locations,631                    BLOCK(gen_op_unbound(LOADV, jv_string_value(v)),632                    gen_call("error", gen_noop())));633  jv_free(v);634  jv_free($2);635} |636BREAK error {637  FAIL(@$, "break requires a label to break to");638  $$ = gen_noop();639} |640Term FIELD '?' {641  $$ = gen_index_opt($1, gen_const($2));642} |643FIELD '?' {644  $$ = gen_index_opt(gen_noop(), gen_const($1));645} |646Term '.' String '?' {647  $$ = gen_index_opt($1, $3);648} |649'.' String '?' {650  $$ = gen_index_opt(gen_noop(), $2);651} |652Term FIELD %prec NONOPT {653  $$ = gen_index($1, gen_const($2));654} |655FIELD %prec NONOPT {656  $$ = gen_index(gen_noop(), gen_const($1));657} |658Term '.' String %prec NONOPT {659  $$ = gen_index($1, $3);660} |661'.' String %prec NONOPT {662  $$ = gen_index(gen_noop(), $2);663} |664'.' error {665  FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");666  $$ = gen_noop();667} |668'.' IDENT error {669  jv_free($2);670  FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");671  $$ = gen_noop();672} |673/* FIXME: string literals */674Term '[' Exp ']' '?' {675  $$ = gen_index_opt($1, $3);676} |677Term '[' Exp ']' %prec NONOPT {678  $$ = gen_index($1, $3);679} |680Term '.' '[' Exp ']' '?' {681  $$ = gen_index_opt($1, $4);682} |683Term '.' '[' Exp ']' %prec NONOPT {684  $$ = gen_index($1, $4);685} |686Term '[' ']' '?' {687  $$ = block_join($1, gen_op_simple(EACH_OPT));688} |689Term '[' ']' %prec NONOPT {690  $$ = block_join($1, gen_op_simple(EACH));691} |692Term '.' '[' ']' '?' {693  $$ = block_join($1, gen_op_simple(EACH_OPT));694} |695Term '.' '[' ']' %prec NONOPT {696  $$ = block_join($1, gen_op_simple(EACH));697} |698Term '[' Exp ':' Exp ']' '?' {699  $$ = gen_slice_index($1, $3, $5, INDEX_OPT);700} |701Term '[' Exp ':' ']' '?' {702  $$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX_OPT);703} |704Term '[' ':' Exp ']' '?' {705  $$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX_OPT);706} |707Term '[' Exp ':' Exp ']' %prec NONOPT {708  $$ = gen_slice_index($1, $3, $5, INDEX);709} |710Term '[' Exp ':' ']' %prec NONOPT {711  $$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX);712} |713Term '[' ':' Exp ']' %prec NONOPT {714  $$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX);715} |716LITERAL {717  $$ = gen_const($1);718} |719String {720  $$ = $1;721} |722FORMAT {723  $$ = gen_format(gen_noop(), $1);724} |725'(' Exp ')' {726  $$ = $2;727} |728'[' Exp ']' {729  $$ = gen_collect($2);730} |731'[' ']' {732  $$ = gen_const(jv_array());733} |734'{' MkDict '}' {735  block o = gen_const_object($2);736  if (o.first != NULL)737    $$ = o;738  else739    $$ = BLOCK(gen_subexp(gen_const(jv_object())), $2, gen_op_simple(POP));740} |741/*742 * This `$$$$varname` hack is strictly private to jq builtins.  DO NOT USE!!743 *744 * This is used in `_modify`, in src/builtin.jq, to avoid holding on to a745 * reference to `.`.746 *747 * We could just have the compiler emit bytecode for `_modify` so it can use748 * LOADVN w/o needing jq syntax for LOADVN.749 *750 * This syntax, `$$$$varname`, violates referential transparency: it has751 * side-effects that are surprising.752 *753 * DO NOT USE!!  I will break your jq code if you do use this outside754 * src/builtin.jq.755 */756'$' '$' '$' BINDING {757  $$ = gen_location(@$, locations, gen_op_unbound(LOADVN, jv_string_value($4)));758  jv_free($4);759} |760BINDING {761  $$ = gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($1)));762  jv_free($1);763} |764"$__loc__" {765  $$ = gen_loc_object(&@$, locations);766} |767IDENT {768  const char *s = jv_string_value($1);769  if (strcmp(s, "false") == 0)770    $$ = gen_const(jv_false());771  else if (strcmp(s, "true") == 0)772    $$ = gen_const(jv_true());773  else if (strcmp(s, "null") == 0)774    $$ = gen_const(jv_null());775  else776    $$ = gen_location(@$, locations, gen_call(s, gen_noop()));777  jv_free($1);778} |779IDENT '(' Args ')' {780  $$ = gen_call(jv_string_value($1), $3);781  $$ = gen_location(@1, locations, $$);782  jv_free($1);783} |784'(' error ')' { $$ = gen_noop(); } |785'[' error ']' { $$ = gen_noop(); } |786Term '[' error ']' { $$ = $1; } |787'{' error '}' { $$ = gen_noop(); }788789Args:790Arg {791  $$ = $1;792} |793Args ';' Arg {794  $$ = BLOCK($1, $3);795}796797Arg:798Exp {799  $$ = gen_lambda($1);800}801802RepPatterns:803RepPatterns "?//" Pattern {804  $$ = BLOCK($1, gen_destructure_alt($3));805} |806Pattern {807  $$ = gen_destructure_alt($1);808}809810Patterns:811RepPatterns "?//" Pattern {812  $$ = BLOCK($1, $3);813} |814Pattern {815  $$ = $1;816}817818Pattern:819BINDING {820  $$ = gen_op_unbound(STOREV, jv_string_value($1));821  jv_free($1);822} |823'[' ArrayPats ']' {824  $$ = BLOCK($2, gen_op_simple(POP));825} |826'{' ObjPats '}' {827  $$ = BLOCK($2, gen_op_simple(POP));828}829830ArrayPats:831Pattern {832  $$ = gen_array_matcher(gen_noop(), $1);833} |834ArrayPats ',' Pattern {835  $$ = gen_array_matcher($1, $3);836}837838ObjPats:839ObjPat {840  $$ = $1;841} |842ObjPats ',' ObjPat {843  $$ = BLOCK($1, $3);844}845846ObjPat:847BINDING {848  $$ = gen_object_matcher(gen_const($1), gen_op_unbound(STOREV, jv_string_value($1)));849} |850BINDING ':' Pattern {851  $$ = gen_object_matcher(gen_const($1), BLOCK(gen_op_simple(DUP), gen_op_unbound(STOREV, jv_string_value($1)), $3));852} |853IDENT ':' Pattern {854  $$ = gen_object_matcher(gen_const($1), $3);855} |856Keyword ':' Pattern {857  $$ = gen_object_matcher(gen_const($1), $3);858} |859String ':' Pattern {860  $$ = gen_object_matcher($1, $3);861} |862'(' Exp ')' ':' Pattern {863  jv msg = check_object_key($2);864  if (jv_is_valid(msg)) {865    FAIL(@$, jv_string_value(msg));866  }867  jv_free(msg);868  $$ = gen_object_matcher($2, $5);869} |870error ':' Pattern {871  FAIL(@$, "May need parentheses around object key expression");872  $$ = $3;873}874875Keyword:876"as" {877  $$ = jv_string("as");878} |879"def" {880  $$ = jv_string("def");881} |882"module" {883  $$ = jv_string("module");884} |885"import" {886  $$ = jv_string("import");887} |888"include" {889  $$ = jv_string("include");890} |891"if" {892  $$ = jv_string("if");893} |894"then" {895  $$ = jv_string("then");896} |897"else" {898  $$ = jv_string("else");899} |900"elif" {901  $$ = jv_string("elif");902} |903"reduce" {904  $$ = jv_string("reduce");905} |906"foreach" {907  $$ = jv_string("foreach");908} |909"end" {910  $$ = jv_string("end");911} |912"and" {913  $$ = jv_string("and");914} |915"or" {916  $$ = jv_string("or");917} |918"try" {919  $$ = jv_string("try");920} |921"catch" {922  $$ = jv_string("catch");923} |924"label" {925  $$ = jv_string("label");926} |927"break" {928  $$ = jv_string("break");929}930931MkDict:932%empty {933  $$=gen_noop();934} |935 MkDictPair { $$ = $1; }936| MkDictPair ',' MkDict { $$=block_join($1, $3); }937| error ',' MkDict { $$ = $3; }938939MkDictPair:940IDENT ':' ExpD {941  $$ = gen_dictpair(gen_const($1), $3);942 }943| Keyword ':' ExpD {944  $$ = gen_dictpair(gen_const($1), $3);945  }946| String ':' ExpD {947  $$ = gen_dictpair($1, $3);948  }949| String {950  $$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),951                              gen_op_simple(DUP2), gen_op_simple(INDEX)));952  }953| BINDING ':' ExpD {954  $$ = gen_dictpair(gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($1))),955                    $3);956  jv_free($1);957  }958| BINDING {959  $$ = gen_dictpair(gen_const($1),960                    gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($1))));961  }962| IDENT {963  $$ = gen_dictpair(gen_const(jv_copy($1)),964                    gen_index(gen_noop(), gen_const($1)));965  }966| "$__loc__" {967  $$ = gen_dictpair(gen_const(jv_string("__loc__")),968                    gen_loc_object(&@$, locations));969  }970| Keyword {971  $$ = gen_dictpair(gen_const(jv_copy($1)),972                    gen_index(gen_noop(), gen_const($1)));973  }974| '(' Exp ')' ':' ExpD {975  jv msg = check_object_key($2);976  if (jv_is_valid(msg)) {977    FAIL(@$, jv_string_value(msg));978  }979  jv_free(msg);980  $$ = gen_dictpair($2, $5);981  }982| error ':' ExpD {983  FAIL(@$, "May need parentheses around object key expression");984  $$ = $3;985  }986%%987988int jq_parse(struct locfile* locations, block* answer) {989  struct lexer_param scanner;990  YY_BUFFER_STATE buf;991  jq_yylex_init_extra(0, &scanner.lexer);992  buf = jq_yy_scan_bytes(locations->data, locations->length, scanner.lexer);993  int errors = 0;994  *answer = gen_noop();995  yyparse(answer, &errors, locations, &scanner);996  jq_yy_delete_buffer(buf, scanner.lexer);997  jq_yylex_destroy(scanner.lexer);998  if (errors > 0) {999    block_free(*answer);1000    *answer = gen_noop();1001  }1002  return errors;1003}10041005int jq_parse_library(struct locfile* locations, block* answer) {1006  int errs = jq_parse(locations, answer);1007  if (errs) return errs;1008  if (block_has_main(*answer)) {1009    locfile_locate(locations, UNKNOWN_LOCATION, "jq: error: library should only have function definitions, not a main expression");1010    return 1;1011  }1012  assert(block_has_only_binders_and_imports(*answer, OP_IS_CALL_PSEUDO));1013  return 0;1014}