1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00
Files
stedolan-jq/parser.y

924 lines
20 KiB
Plaintext
Raw Permalink Normal View History

2012-08-16 01:00:30 +01:00
%{
#include <assert.h>
#include <math.h>
2012-08-16 01:00:30 +01:00
#include <stdio.h>
#include <string.h>
#include "compile.h"
#include "jv_alloc.h"
#define YYMALLOC jv_mem_alloc
#define YYFREE jv_mem_free
2012-08-16 01:00:30 +01:00
%}
%code requires {
#include "locfile.h"
struct lexer_param;
#define YYLTYPE location
#define YYLLOC_DEFAULT(Loc, Rhs, N) \
do { \
if (N) { \
(Loc).start = YYRHSLOC(Rhs, 1).start; \
(Loc).end = YYRHSLOC(Rhs, N).end; \
} else { \
(Loc).start = YYRHSLOC(Rhs, 0).end; \
(Loc).end = YYRHSLOC(Rhs, 0).end; \
} \
} while (0)
2015-07-19 09:36:16 -07:00
}
2012-08-16 01:00:30 +01:00
%locations
%error-verbose
2012-08-16 01:00:30 +01:00
%define api.pure
%union {
jv literal;
2012-08-16 01:00:30 +01:00
block blk;
}
%destructor { jv_free($$); } <literal>
%destructor { block_free($$); } <blk>
2012-08-16 01:00:30 +01:00
%parse-param {block* answer}
%parse-param {int* errors}
%parse-param {struct locfile* locations}
%parse-param {struct lexer_param* lexer_param_ptr}
%lex-param {block* answer}
%lex-param {int* errors}
%lex-param {struct locfile* locations}
%lex-param {struct lexer_param* lexer_param_ptr}
2012-08-16 01:00:30 +01:00
%token INVALID_CHARACTER
%token <literal> IDENT
%token <literal> FIELD
%token <literal> LITERAL
%token <literal> FORMAT
%token REC ".."
2013-06-17 20:21:37 -05:00
%token SETMOD "%="
2012-08-16 01:00:30 +01:00
%token EQ "=="
2012-10-23 17:01:39 +02:00
%token NEQ "!="
2012-09-04 15:34:34 +01:00
%token DEFINEDOR "//"
2012-08-16 01:00:30 +01:00
%token AS "as"
2012-08-21 18:14:13 +01:00
%token DEF "def"
%token MODULE "module"
%token IMPORT "import"
%token INCLUDE "include"
2012-09-04 15:34:34 +01:00
%token IF "if"
%token THEN "then"
%token ELSE "else"
2012-09-04 16:05:24 +01:00
%token ELSE_IF "elif"
%token REDUCE "reduce"
2014-07-06 03:24:29 -05:00
%token FOREACH "foreach"
2012-09-04 15:34:34 +01:00
%token END "end"
%token AND "and"
%token OR "or"
2014-07-05 20:54:42 -05:00
%token TRY "try"
%token CATCH "catch"
%token LABEL "label"
%token BREAK "break"
2015-03-30 15:55:54 -05:00
%token LOC "__loc__"
%token SETPIPE "|="
%token SETPLUS "+="
%token SETMINUS "-="
%token SETMULT "*="
%token SETDIV "/="
%token SETDEFINEDOR "//="
2012-10-07 22:34:12 +01:00
%token LESSEQ "<="
%token GREATEREQ ">="
%token QQSTRING_START
%token <literal> QQSTRING_TEXT
%token QQSTRING_INTERP_START
%token QQSTRING_INTERP_END
%token QQSTRING_END
/* Instead of raising this, find a way to use precedence to resolve
* shift-reduce conflicts. */
%expect 0
%precedence FUNCDEF
%right '|'
%left ','
2012-09-04 15:34:34 +01:00
%right "//"
2013-06-17 20:21:37 -05:00
%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETMOD SETDEFINEDOR
%left OR
%left AND
%nonassoc NEQ EQ '<' '>' LESSEQ GREATEREQ
%left '+' '-'
2013-06-17 20:21:37 -05:00
%left '*' '/' '%'
%precedence NONOPT /* non-optional; rules for which a specialized
'?' rule should be preferred over Exp '?' */
%precedence '?'
%precedence "try"
%precedence "catch"
2012-08-16 01:00:30 +01:00
2012-09-04 15:34:34 +01:00
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString
%type <blk> FuncDef FuncDefs String Import Imports Param Params
%type <blk> Arg Args Module Pattern ArrayPats ObjPats ObjPat
%type <literal> Keyword
2012-08-16 01:00:30 +01:00
%{
#include "lexer.h"
struct lexer_param {
yyscan_t lexer;
};
#define FAIL(loc, msg) \
do { \
location l = loc; \
yyerror(&l, answer, errors, locations, lexer_param_ptr, msg); \
/*YYERROR*/; \
} while (0)
2015-07-19 09:36:16 -07:00
void yyerror(YYLTYPE* loc, block* answer, int* errors,
struct locfile* locations, struct lexer_param* lexer_param_ptr, const char *s){
(*errors)++;
if (strstr(s, "unexpected")) {
#ifdef WIN32
locfile_locate(locations, *loc, "jq: error: %s (Windows cmd shell quoting issues?)", s);
#else
locfile_locate(locations, *loc, "jq: error: %s (Unix shell quoting issues?)", s);
#endif
} else {
locfile_locate(locations, *loc, "jq: error: %s", s);
}
2012-08-16 01:00:30 +01:00
}
2015-07-19 09:36:16 -07:00
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
struct locfile* locations, struct lexer_param* lexer_param_ptr) {
yyscan_t lexer = lexer_param_ptr->lexer;
int tok = jq_yylex(yylval, yylloc, lexer);
if ((tok == LITERAL || tok == QQSTRING_TEXT) && !jv_is_valid(yylval->literal)) {
jv msg = jv_invalid_get_msg(jv_copy(yylval->literal));
if (jv_get_kind(msg) == JV_KIND_STRING) {
FAIL(*yylloc, jv_string_value(msg));
} else {
FAIL(*yylloc, "Invalid literal");
}
jv_free(msg);
jv_free(yylval->literal);
yylval->literal = jv_null();
}
return tok;
}
2012-08-16 01:00:30 +01:00
static block gen_dictpair(block k, block v) {
return BLOCK(gen_subexp(k), gen_subexp(v), gen_op_simple(INSERT));
2012-08-16 01:00:30 +01:00
}
static block gen_index(block obj, block key) {
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX));
2012-08-16 01:00:30 +01:00
}
static block gen_index_opt(block obj, block key) {
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX_OPT));
}
static block gen_slice_index(block obj, block start, block end, opcode idx_op) {
2013-05-13 20:16:19 +01:00
block key = BLOCK(gen_subexp(gen_const(jv_object())),
gen_subexp(gen_const(jv_string("start"))),
gen_subexp(start),
gen_op_simple(INSERT),
gen_subexp(gen_const(jv_string("end"))),
gen_subexp(end),
gen_op_simple(INSERT));
return BLOCK(key, obj, gen_op_simple(idx_op));
2013-05-13 20:16:19 +01:00
}
2014-07-27 17:33:22 -05:00
static block constant_fold(block a, block b, int op) {
if (!block_is_single(a) || !block_is_const(a) ||
!block_is_single(b) || !block_is_const(b))
return gen_noop();
if (block_const_kind(a) != block_const_kind(b))
return gen_noop();
jv res = jv_invalid();
if (block_const_kind(a) == JV_KIND_NUMBER) {
double na = jv_number_value(block_const(a));
double nb = jv_number_value(block_const(b));
switch (op) {
case '+': res = jv_number(na + nb); break;
case '-': res = jv_number(na - nb); break;
case '*': res = jv_number(na * nb); break;
case '/': res = jv_number(na / nb); break;
case EQ: res = (na == nb ? jv_true() : jv_false()); break;
case NEQ: res = (na != nb ? jv_true() : jv_false()); break;
case '<': res = (na < nb ? jv_true() : jv_false()); break;
case '>': res = (na > nb ? jv_true() : jv_false()); break;
case LESSEQ: res = (na <= nb ? jv_true() : jv_false()); break;
case GREATEREQ: res = (na >= nb ? jv_true() : jv_false()); break;
default: break;
}
} else if (op == '+' && block_const_kind(a) == JV_KIND_STRING) {
res = jv_string_concat(block_const(a), block_const(b));
} else {
return gen_noop();
}
if (jv_get_kind(res) == JV_KIND_INVALID)
return gen_noop();
block_free(a);
block_free(b);
return gen_const(res);
}
static block gen_binop(block a, block b, int op) {
2014-07-27 17:33:22 -05:00
block folded = constant_fold(a, b, op);
if (!block_is_noop(folded))
return folded;
const char* funcname = 0;
switch (op) {
case '+': funcname = "_plus"; break;
case '-': funcname = "_minus"; break;
case '*': funcname = "_multiply"; break;
case '/': funcname = "_divide"; break;
2013-06-17 20:21:37 -05:00
case '%': funcname = "_mod"; break;
case EQ: funcname = "_equal"; break;
2012-10-23 17:01:39 +02:00
case NEQ: funcname = "_notequal"; break;
2012-10-07 22:34:12 +01:00
case '<': funcname = "_less"; break;
case '>': funcname = "_greater"; break;
case LESSEQ: funcname = "_lesseq"; break;
case GREATEREQ: funcname = "_greatereq"; break;
}
assert(funcname);
return gen_call(funcname, BLOCK(gen_lambda(a), gen_lambda(b)));
}
static block gen_format(block a, jv fmt) {
return BLOCK(a, gen_call("format", BLOCK(gen_lambda(gen_const(fmt)))));
2012-09-18 13:22:22 +01:00
}
2013-05-13 16:04:30 +01:00
static block gen_definedor_assign(block object, block val) {
2013-06-18 01:07:18 +01:00
block tmp = gen_op_var_fresh(STOREV, "tmp");
2013-05-13 16:04:30 +01:00
return BLOCK(gen_op_simple(DUP),
val, tmp,
gen_call("_modify", BLOCK(gen_lambda(object),
2015-07-19 09:36:16 -07:00
gen_lambda(gen_definedor(gen_noop(),
2013-06-18 01:07:18 +01:00
gen_op_bound(LOADV, tmp))))));
2013-05-13 16:04:30 +01:00
}
2015-07-19 09:36:16 -07:00
static block gen_update(block object, block val, int optype) {
2013-06-18 01:07:18 +01:00
block tmp = gen_op_var_fresh(STOREV, "tmp");
return BLOCK(gen_op_simple(DUP),
val,
tmp,
2015-07-19 09:36:16 -07:00
gen_call("_modify", BLOCK(gen_lambda(object),
gen_lambda(gen_binop(gen_noop(),
2013-06-18 01:07:18 +01:00
gen_op_bound(LOADV, tmp),
optype)))));
}
2012-08-16 01:00:30 +01:00
%}
%%
2012-09-18 10:17:38 +01:00
TopLevel:
Module Imports Exp {
*answer = BLOCK($1, $2, gen_op_simple(TOP), $3);
2012-09-18 10:17:38 +01:00
} |
Module Imports FuncDefs {
*answer = BLOCK($1, $2, $3);
2015-07-19 09:36:16 -07:00
}
2012-08-16 01:00:30 +01:00
Module:
%empty {
$$ = gen_noop();
} |
"module" Exp ';' {
if (!block_is_const($2)) {
FAIL(@$, "Module metadata must be constant.");
$$ = gen_noop();
} else {
$$ = gen_module($2);
}
}
Imports:
%empty {
$$ = gen_noop();
} |
Import Imports {
$$ = BLOCK($1, $2);
}
2012-09-18 10:17:38 +01:00
FuncDefs:
%empty {
2012-09-18 10:17:38 +01:00
$$ = gen_noop();
2012-08-21 18:14:13 +01:00
} |
2012-09-18 10:17:38 +01:00
FuncDef FuncDefs {
$$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);
2012-09-18 10:17:38 +01:00
}
2012-08-21 18:14:13 +01:00
2012-09-18 10:17:38 +01:00
Exp:
FuncDef Exp %prec FUNCDEF {
$$ = block_bind_referenced($1, $2, OP_IS_CALL_PSEUDO);
2012-08-26 14:25:56 +01:00
} |
Term "as" Pattern '|' Exp {
$$ = gen_destructure($1, $3, $5);
2012-08-16 01:00:30 +01:00
} |
"reduce" Term "as" Pattern '(' Exp ';' Exp ')' {
$$ = gen_reduce($2, $4, $6, $8);
2012-12-28 15:04:16 +00:00
} |
"foreach" Term "as" Pattern '(' Exp ';' Exp ';' Exp ')' {
$$ = gen_foreach($2, $4, $6, $8, $10);
2014-07-06 03:24:29 -05:00
} |
"foreach" Term "as" Pattern '(' Exp ';' Exp ')' {
$$ = gen_foreach($2, $4, $6, $8, gen_noop());
} |
2012-09-04 16:05:24 +01:00
"if" Exp "then" Exp ElseBody {
$$ = gen_cond($2, $4, $5);
2012-09-04 15:34:34 +01:00
} |
"if" Exp "then" error {
2013-10-06 17:52:04 +02:00
FAIL(@$, "Possibly unterminated 'if' statement");
$$ = $2;
} |
2012-09-04 15:34:34 +01:00
2014-07-05 20:54:42 -05:00
"try" Exp "catch" Exp {
//$$ = BLOCK(gen_op_target(FORK_OPT, $2), $2, $4);
$$ = gen_try($2, gen_try_handler($4));
2014-07-05 20:54:42 -05:00
} |
"try" Exp {
//$$ = BLOCK(gen_op_target(FORK_OPT, $2), $2, gen_op_simple(BACKTRACK));
$$ = gen_try($2, gen_op_simple(BACKTRACK));
} |
"try" Exp "catch" error {
FAIL(@$, "Possibly unterminated 'try' statement");
$$ = $2;
} |
"label" '$' IDENT '|' Exp {
jv v = jv_string_fmt("*label-%s", jv_string_value($3));
$$ = gen_location(@$, locations, gen_label(jv_string_value(v), $5));
jv_free($3);
jv_free(v);
} |
2014-07-06 00:11:55 -05:00
Exp '?' {
$$ = gen_try($1, gen_op_simple(BACKTRACK));
} |
2012-08-27 10:11:55 +01:00
Exp '=' Exp {
$$ = gen_call("_assign", BLOCK(gen_lambda($1), gen_lambda($3)));
2012-08-27 10:11:55 +01:00
} |
Exp "or" Exp {
$$ = gen_or($1, $3);
2015-07-19 09:36:16 -07:00
} |
Exp "and" Exp {
$$ = gen_and($1, $3);
} |
2012-09-04 15:34:34 +01:00
Exp "//" Exp {
$$ = gen_definedor($1, $3);
} |
Exp "//=" Exp {
2013-05-13 16:04:30 +01:00
$$ = gen_definedor_assign($1, $3);
} |
2012-08-27 10:11:55 +01:00
Exp "|=" Exp {
$$ = gen_call("_modify", BLOCK(gen_lambda($1), gen_lambda($3)));
2012-08-27 10:11:55 +01:00
} |
2015-07-19 09:36:16 -07:00
Exp '|' Exp {
$$ = block_join($1, $3);
2012-08-16 01:00:30 +01:00
} |
2015-07-19 09:36:16 -07:00
Exp ',' Exp {
$$ = gen_both($1, $3);
2012-08-16 01:00:30 +01:00
} |
2012-08-16 01:16:08 +01:00
Exp '+' Exp {
$$ = gen_binop($1, $3, '+');
} |
Exp "+=" Exp {
$$ = gen_update($1, $3, '+');
} |
'-' Exp {
$$ = BLOCK($2, gen_call("_negate", gen_noop()));
} |
Exp '-' Exp {
$$ = gen_binop($1, $3, '-');
2012-08-16 01:16:08 +01:00
} |
Exp "-=" Exp {
$$ = gen_update($1, $3, '-');
} |
Exp '*' Exp {
$$ = gen_binop($1, $3, '*');
} |
Exp "*=" Exp {
$$ = gen_update($1, $3, '*');
} |
Exp '/' Exp {
$$ = gen_binop($1, $3, '/');
if (block_is_const_inf($$))
FAIL(@$, "Division by zero?");
} |
2013-06-17 20:21:37 -05:00
Exp '%' Exp {
$$ = gen_binop($1, $3, '%');
if (block_is_const_inf($$))
FAIL(@$, "Remainder by zero?");
2013-06-17 20:21:37 -05:00
} |
Exp "/=" Exp {
$$ = gen_update($1, $3, '/');
} |
2013-06-17 20:21:37 -05:00
Exp SETMOD Exp {
$$ = gen_update($1, $3, '%');
} |
Exp "==" Exp {
$$ = gen_binop($1, $3, EQ);
} |
2012-10-23 17:01:39 +02:00
Exp "!=" Exp {
$$ = gen_binop($1, $3, NEQ);
} |
2012-10-07 22:34:12 +01:00
Exp '<' Exp {
$$ = gen_binop($1, $3, '<');
} |
Exp '>' Exp {
$$ = gen_binop($1, $3, '>');
} |
Exp "<=" Exp {
$$ = gen_binop($1, $3, LESSEQ);
} |
Exp ">=" Exp {
$$ = gen_binop($1, $3, GREATEREQ);
} |
2015-07-19 09:36:16 -07:00
Term {
$$ = $1;
2012-08-16 01:00:30 +01:00
}
Import:
"import" String "as" '$' IDENT ';' {
jv v = block_const($2);
// XXX Make gen_import take only blocks and the int is_data so we
// don't have to free so much stuff here
$$ = gen_import(jv_string_value(v), gen_noop(), jv_string_value($5), 1);
block_free($2);
jv_free($5);
jv_free(v);
} |
"import" String "as" IDENT ';' {
jv v = block_const($2);
$$ = gen_import(jv_string_value(v), gen_noop(), jv_string_value($4), 0);
block_free($2);
jv_free($4);
jv_free(v);
} |
"include" String ';' {
2015-06-26 23:40:37 -05:00
jv v = block_const($2);
$$ = gen_import(jv_string_value(v), gen_noop(), NULL, 0);
block_free($2);
jv_free(v);
} |
"import" String "as" IDENT Exp ';' {
if (!block_is_const($5)) {
FAIL(@$, "Module metadata must be constant.");
$$ = gen_noop();
} else {
jv v = block_const($2);
$$ = gen_import(jv_string_value(v), $5, jv_string_value($4), 0);
jv_free(v);
}
block_free($2);
jv_free($4);
} |
"include" String Exp ';' {
2015-06-26 23:40:37 -05:00
if (!block_is_const($3)) {
FAIL(@$, "Module metadata must be constant.");
$$ = gen_noop();
} else {
jv v = block_const($2);
$$ = gen_import(jv_string_value(v), $3, NULL, 0);
jv_free(v);
}
block_free($2);
} |
"import" String "as" '$' IDENT Exp ';' {
if (!block_is_const($6)) {
FAIL(@$, "Module metadata must be constant.");
$$ = gen_noop();
} else {
jv v = block_const($2);
$$ = gen_import(jv_string_value(v), $6, jv_string_value($5), 1);
jv_free(v);
}
block_free($2);
jv_free($5);
}
2012-09-18 10:17:38 +01:00
FuncDef:
"def" IDENT ':' Exp ';' {
$$ = gen_function(jv_string_value($2), gen_noop(), $4);
2012-09-18 10:17:38 +01:00
jv_free($2);
} |
2014-08-08 18:00:47 -05:00
"def" IDENT '(' Params ')' ':' Exp ';' {
$$ = gen_function(jv_string_value($2), $4, $7);
2012-09-18 10:17:38 +01:00
jv_free($2);
2014-08-08 18:00:47 -05:00
}
2014-08-08 18:00:47 -05:00
Params:
Param {
$$ = $1;
} |
2014-08-08 18:00:47 -05:00
Params ';' Param {
$$ = BLOCK($1, $3);
}
2014-08-08 18:00:47 -05:00
Param:
'$' IDENT {
$$ = gen_param_regular(jv_string_value($2));
jv_free($2);
} |
2014-08-08 18:00:47 -05:00
IDENT {
$$ = gen_param(jv_string_value($1));
jv_free($1);
}
String:
QQSTRING_START { $<literal>$ = jv_string("text"); } QQString QQSTRING_END {
$$ = $3;
jv_free($<literal>2);
} |
FORMAT QQSTRING_START { $<literal>$ = $1; } QQString QQSTRING_END {
$$ = $4;
jv_free($<literal>3);
}
QQString:
%empty {
$$ = gen_const(jv_string(""));
} |
QQString QQSTRING_TEXT {
$$ = gen_binop($1, gen_const($2), '+');
} |
QQString QQSTRING_INTERP_START Exp QQSTRING_INTERP_END {
$$ = gen_binop($1, gen_format($3, jv_copy($<literal>0)), '+');
}
2012-09-04 16:05:24 +01:00
ElseBody:
"elif" Exp "then" Exp ElseBody {
$$ = gen_cond($2, $4, $5);
} |
"else" Exp "end" {
$$ = $2;
}
2012-08-16 01:00:30 +01:00
ExpD:
2015-07-19 09:36:16 -07:00
ExpD '|' ExpD {
2012-08-16 01:00:30 +01:00
$$ = block_join($1, $3);
} |
'-' ExpD {
$$ = BLOCK($2, gen_call("_negate", gen_noop()));
} |
2012-08-16 01:00:30 +01:00
Term {
$$ = $1;
}
Term:
'.' {
2015-07-19 09:36:16 -07:00
$$ = gen_noop();
2012-08-16 01:00:30 +01:00
} |
REC {
$$ = gen_call("recurse", gen_noop());
} |
BREAK '$' IDENT {
jv v = jv_string_fmt("*label-%s", jv_string_value($3)); // impossible symbol
$$ = gen_location(@$, locations,
BLOCK(gen_op_unbound(LOADV, jv_string_value(v)),
gen_call("error", gen_noop())));
jv_free(v);
jv_free($3);
} |
BREAK error {
FAIL(@$, "break requires a label to break to");
$$ = gen_noop();
} |
Term FIELD '?' {
$$ = gen_index_opt($1, gen_const($2));
} |
2015-07-19 09:36:16 -07:00
FIELD '?' {
$$ = gen_index_opt(gen_noop(), gen_const($1));
} |
Term '.' String '?' {
$$ = gen_index_opt($1, $3);
} |
'.' String '?' {
$$ = gen_index_opt(gen_noop(), $2);
} |
Term FIELD %prec NONOPT {
$$ = gen_index($1, gen_const($2));
2012-08-16 01:00:30 +01:00
} |
FIELD %prec NONOPT {
2015-07-19 09:36:16 -07:00
$$ = gen_index(gen_noop(), gen_const($1));
2012-08-16 01:00:30 +01:00
} |
Term '.' String %prec NONOPT {
$$ = gen_index($1, $3);
} |
'.' String %prec NONOPT {
$$ = gen_index(gen_noop(), $2);
} |
'.' error {
FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");
$$ = gen_noop();
} |
'.' IDENT error {
jv_free($2);
FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");
$$ = gen_noop();
2015-07-19 09:36:16 -07:00
} |
2012-08-16 01:00:30 +01:00
/* FIXME: string literals */
Term '[' Exp ']' '?' {
2015-07-19 09:36:16 -07:00
$$ = gen_index_opt($1, $3);
} |
Term '[' Exp ']' %prec NONOPT {
2015-07-19 09:36:16 -07:00
$$ = gen_index($1, $3);
2012-08-16 01:00:30 +01:00
} |
Term '[' ']' '?' {
2015-07-19 09:36:16 -07:00
$$ = block_join($1, gen_op_simple(EACH_OPT));
} |
Term '[' ']' %prec NONOPT {
2015-07-19 09:36:16 -07:00
$$ = block_join($1, gen_op_simple(EACH));
2012-08-16 01:00:30 +01:00
} |
Term '[' Exp ':' Exp ']' '?' {
$$ = gen_slice_index($1, $3, $5, INDEX_OPT);
} |
Term '[' Exp ':' ']' '?' {
$$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX_OPT);
} |
Term '[' ':' Exp ']' '?' {
$$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX_OPT);
} |
Term '[' Exp ':' Exp ']' %prec NONOPT {
$$ = gen_slice_index($1, $3, $5, INDEX);
2013-05-13 20:16:19 +01:00
} |
Term '[' Exp ':' ']' %prec NONOPT {
$$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX);
2013-05-13 20:16:19 +01:00
} |
Term '[' ':' Exp ']' %prec NONOPT {
$$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX);
2013-05-13 20:16:19 +01:00
} |
LITERAL {
2015-07-19 09:36:16 -07:00
$$ = gen_const($1);
2012-08-16 01:00:30 +01:00
} |
String {
$$ = $1;
} |
FORMAT {
$$ = gen_format(gen_noop(), $1);
} |
2015-07-19 09:36:16 -07:00
'(' Exp ')' {
$$ = $2;
} |
'[' Exp ']' {
$$ = gen_collect($2);
2012-08-16 01:00:30 +01:00
} |
2015-07-19 09:36:16 -07:00
'[' ']' {
$$ = gen_const(jv_array());
2012-08-16 01:00:30 +01:00
} |
2015-07-19 09:36:16 -07:00
'{' MkDict '}' {
2014-08-09 20:47:03 -05:00
block o = gen_const_object($2);
if (o.first != NULL)
$$ = o;
else
$$ = BLOCK(gen_subexp(gen_const(jv_object())), $2, gen_op_simple(POP));
2012-08-16 01:00:30 +01:00
} |
2015-03-30 15:55:54 -05:00
'$' LOC {
$$ = gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
jv_string("line"), jv_number(locfile_get_line(locations, @$.start) + 1)));
} |
2012-08-16 01:00:30 +01:00
'$' IDENT {
$$ = gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2)));
jv_free($2);
2015-07-19 09:36:16 -07:00
} |
IDENT {
const char *s = jv_string_value($1);
if (strcmp(s, "false") == 0)
$$ = gen_const(jv_false());
else if (strcmp(s, "true") == 0)
$$ = gen_const(jv_true());
else if (strcmp(s, "null") == 0)
$$ = gen_const(jv_null());
else
$$ = gen_location(@$, locations, gen_call(s, gen_noop()));
jv_free($1);
2012-08-26 14:25:56 +01:00
} |
IDENT '(' Args ')' {
$$ = gen_call(jv_string_value($1), $3);
$$ = gen_location(@1, locations, $$);
2013-11-28 13:04:29 -06:00
jv_free($1);
} |
'(' error ')' { $$ = gen_noop(); } |
'[' error ']' { $$ = gen_noop(); } |
Term '[' error ']' { $$ = $1; } |
'{' error '}' { $$ = gen_noop(); }
2012-08-16 01:00:30 +01:00
Args:
Arg {
$$ = $1;
} |
Args ';' Arg {
$$ = BLOCK($1, $3);
}
Arg:
Exp {
$$ = gen_lambda($1);
}
Pattern:
'$' IDENT {
$$ = gen_op_unbound(STOREV, jv_string_value($2));
jv_free($2);
} |
'[' ArrayPats ']' {
$$ = BLOCK($2, gen_op_simple(POP));
} |
'{' ObjPats '}' {
$$ = BLOCK($2, gen_op_simple(POP));
}
ArrayPats:
Pattern {
$$ = gen_array_matcher(gen_noop(), $1);
} |
ArrayPats ',' Pattern {
$$ = gen_array_matcher($1, $3);
}
ObjPats:
ObjPat {
$$ = $1;
} |
ObjPats ',' ObjPat {
$$ = BLOCK($1, $3);
}
ObjPat:
'$' IDENT {
$$ = gen_object_matcher(gen_const($2), gen_op_unbound(STOREV, jv_string_value($2)));
} |
IDENT ':' Pattern {
$$ = gen_object_matcher(gen_const($1), $3);
} |
Keyword ':' Pattern {
$$ = gen_object_matcher(gen_const($1), $3);
} |
String ':' Pattern {
$$ = gen_object_matcher($1, $3);
} |
'(' Exp ')' ':' Pattern {
$$ = gen_object_matcher($2, $5);
}
Keyword:
"as" {
$$ = jv_string("as");
} |
"def" {
$$ = jv_string("def");
} |
"module" {
$$ = jv_string("module");
} |
"import" {
$$ = jv_string("import");
} |
"include" {
$$ = jv_string("include");
} |
"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__");
}
2012-08-16 01:00:30 +01:00
MkDict:
2015-07-19 09:36:16 -07:00
%empty {
$$=gen_noop();
} |
MkDictPair { $$ = $1; }
2012-08-16 01:00:30 +01:00
| MkDictPair ',' MkDict { $$=block_join($1, $3); }
| error ',' MkDict { $$ = $3; }
2012-08-16 01:00:30 +01:00
MkDictPair:
2015-07-19 09:36:16 -07:00
IDENT ':' ExpD {
$$ = gen_dictpair(gen_const($1), $3);
2012-08-16 01:00:30 +01:00
}
| Keyword ':' ExpD {
$$ = gen_dictpair(gen_const($1), $3);
}
| String ':' ExpD {
$$ = gen_dictpair($1, $3);
}
| String {
$$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),
gen_op_simple(DUP2), gen_op_simple(INDEX)));
}
| '$' IDENT {
$$ = gen_dictpair(gen_const($2),
gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
}
2012-08-16 01:00:30 +01:00
| IDENT {
$$ = gen_dictpair(gen_const(jv_copy($1)),
gen_index(gen_noop(), gen_const($1)));
2012-08-16 01:00:30 +01:00
}
| '(' Exp ')' ':' ExpD {
$$ = gen_dictpair($2, $5);
}
| '(' error ')' ':' ExpD { $$ = $5; }
2012-08-16 01:00:30 +01:00
%%
int jq_parse(struct locfile* locations, block* answer) {
struct lexer_param scanner;
2012-08-16 01:00:30 +01:00
YY_BUFFER_STATE buf;
jq_yylex_init_extra(0, &scanner.lexer);
buf = jq_yy_scan_bytes(locations->data, locations->length, scanner.lexer);
int errors = 0;
*answer = gen_noop();
yyparse(answer, &errors, locations, &scanner);
jq_yy_delete_buffer(buf, scanner.lexer);
jq_yylex_destroy(scanner.lexer);
if (errors > 0) {
block_free(*answer);
*answer = gen_noop();
}
return errors;
2012-08-16 01:00:30 +01:00
}
2012-09-18 10:17:38 +01:00
int jq_parse_library(struct locfile* locations, block* answer) {
int errs = jq_parse(locations, answer);
if (errs) return errs;
if (block_has_main(*answer)) {
locfile_locate(locations, UNKNOWN_LOCATION, "jq: error: library should only have function definitions, not a main expression");
2012-09-18 10:17:38 +01:00
return 1;
}
assert(block_has_only_binders_and_imports(*answer, OP_IS_CALL_PSEUDO));
2012-09-18 10:17:38 +01:00
return 0;
}