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

361 lines
8.0 KiB
Plaintext
Raw Normal View History

2012-08-16 01:00:30 +01:00
%{
#include <stdio.h>
#include <string.h>
#include "compile.h"
%}
%code requires {
#include "locfile.h"
#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)
}
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}
2012-08-16 01:00:30 +01:00
%parse-param {yyscan_t lexer}
%lex-param {block* answer}
%lex-param {int* errors}
%lex-param {struct locfile* locations}
2012-08-16 01:00:30 +01:00
%lex-param {yyscan_t lexer}
%token INVALID_CHARACTER
%token <literal> IDENT
%token <literal> LITERAL
2012-08-16 01:00:30 +01:00
%token EQ "=="
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"
2012-08-27 10:11:55 +01:00
%token SETPIPE "|="
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"
2012-09-04 15:34:34 +01:00
%token END "end"
%token AND "and"
%token OR "or"
%token NOT "not"
/* revolting hack */
%left ';'
%left '|'
%left ','
2012-09-04 15:34:34 +01:00
%right "//"
2012-08-27 10:11:55 +01:00
%nonassoc '=' SETPIPE
2012-08-16 01:00:30 +01:00
%nonassoc EQ
%left OR
%left AND
%left '+' '-'
%left '*' '/'
2012-08-16 01:00:30 +01:00
2012-09-04 15:34:34 +01:00
2012-09-04 16:05:24 +01:00
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody
2012-08-16 01:00:30 +01:00
%{
#include "lexer.yy.h"
#define FAIL(loc, msg) \
do { \
location l = loc; \
yyerror(&l, answer, errors, locations, lexer, msg); \
/*YYERROR*/; \
} while (0)
void yyerror(YYLTYPE* loc, block* answer, int* errors,
struct locfile* locations, yyscan_t lexer, const char *s){
(*errors)++;
locfile_locate(locations, *loc, "error: %s", s);
2012-08-16 01:00:30 +01:00
}
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
struct locfile* locations, yyscan_t lexer) {
while (1) {
int tok = jq_yylex(yylval, yylloc, lexer);
if (tok == INVALID_CHARACTER) {
FAIL(*yylloc, "Invalid character");
} else {
if (tok == LITERAL && !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) {
block b = gen_subexp(k);
block_append(&b, gen_subexp(v));
block_append(&b, gen_op_simple(INSERT));
return b;
}
static block gen_index(block obj, block key) {
return block_join(obj, block_join(gen_subexp(key), gen_op_simple(INDEX)));
}
static block gen_binop(block a, block b, int op) {
const char* funcname = 0;
switch (op) {
case '+': funcname = "_plus"; break;
case '-': funcname = "_minus"; break;
case '*': funcname = "_multiply"; break;
case '/': funcname = "_divide"; break;
case EQ: funcname = "_equal"; break;
}
assert(funcname);
block c = gen_noop();
block_append(&c, gen_subexp(a));
block_append(&c, gen_subexp(b));
block_append(&c, gen_op_call(CALL_1_1, gen_op_block_unbound(CLOSURE_REF, funcname)));
return c;
}
2012-08-16 01:00:30 +01:00
%}
%%
program: Exp { *answer = $1; }
Exp:
2012-08-21 18:14:13 +01:00
"def" IDENT ':' Exp ';' Exp {
block body = block_join($4, gen_op_simple(RET));
$$ = block_bind(gen_op_block_defn(CLOSURE_CREATE, jv_string_value($2), body),
$6, OP_IS_CALL_PSEUDO);
jv_free($2);
2012-08-21 18:14:13 +01:00
} |
2012-08-26 14:25:56 +01:00
"def" IDENT '(' IDENT ')' ':' Exp ';' Exp {
block body = block_bind(gen_op_block_unbound(CLOSURE_PARAM, jv_string_value($4)), block_join($7, gen_op_simple(RET)), OP_IS_CALL_PSEUDO);
$$ = block_bind(gen_op_block_defn(CLOSURE_CREATE, jv_string_value($2), body), $9, OP_IS_CALL_PSEUDO);
jv_free($2);
jv_free($4);
2012-08-26 14:25:56 +01:00
} |
2012-08-16 01:00:30 +01:00
Term "as" '$' IDENT '|' Exp {
$$ = gen_op_simple(DUP);
block_append(&$$, $1);
block_append(&$$, block_bind(gen_op_var_unbound(STOREV, jv_string_value($4)), $6, OP_HAS_VARIABLE));
jv_free($4);
2012-08-16 01:00:30 +01:00
} |
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 error {
FAIL(@$, "Possibly unterminated 'if' statment");
$$ = $2;
} |
2012-09-04 15:34:34 +01:00
2012-08-27 10:11:55 +01:00
Exp '=' Exp {
block assign = gen_op_simple(DUP);
block_append(&assign, $3);
block_append(&assign, gen_op_simple(SWAP));
block_append(&assign, $1);
block_append(&assign, gen_op_simple(SWAP));
$$ = gen_assign(assign);
} |
Exp "or" Exp {
$$ = gen_or($1, $3);
} |
Exp "and" Exp {
$$ = gen_and($1, $3);
} |
"not" Exp {
$$ = gen_not($2);
} |
2012-09-04 15:34:34 +01:00
Exp "//" Exp {
$$ = gen_definedor($1, $3);
} |
2012-08-27 10:11:55 +01:00
Exp "|=" Exp {
block assign = $1;
block_append(&assign, gen_op_simple(DUP));
block_append(&assign, $3);
$$ = gen_assign(assign);
} |
2012-08-16 01:00:30 +01:00
Exp '|' Exp {
$$ = block_join($1, $3);
} |
Exp ',' Exp {
$$ = gen_both($1, $3);
} |
2012-08-16 01:16:08 +01:00
Exp '+' Exp {
$$ = gen_binop($1, $3, '+');
} |
Exp '-' Exp {
$$ = gen_binop($1, $3, '-');
2012-08-16 01:16:08 +01:00
} |
Exp '*' Exp {
$$ = gen_binop($1, $3, '*');
} |
Exp '/' Exp {
$$ = gen_binop($1, $3, '/');
} |
Exp "==" Exp {
$$ = gen_binop($1, $3, EQ);
} |
2012-08-16 01:00:30 +01:00
Term {
$$ = $1;
}
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:
ExpD '|' ExpD {
$$ = block_join($1, $3);
} |
Term {
$$ = $1;
}
Term:
'.' {
$$ = gen_noop();
} |
Term '.' IDENT {
$$ = gen_index($1, gen_op_const(LOADK, $3));
2012-08-16 01:00:30 +01:00
} |
'.' IDENT {
$$ = gen_index(gen_noop(), gen_op_const(LOADK, $2));
2012-08-16 01:00:30 +01:00
} |
/* FIXME: string literals */
Term '[' Exp ']' {
$$ = gen_index($1, $3);
} |
Term '[' ']' {
$$ = block_join($1, gen_op_simple(EACH));
} |
LITERAL {
$$ = gen_op_const(LOADK, $1);
2012-08-16 01:00:30 +01:00
} |
'(' Exp ')' {
$$ = $2;
} |
'[' Exp ']' {
$$ = gen_collect($2);
} |
'[' ']' {
$$ = gen_op_const(LOADK, jv_array());
2012-08-16 01:00:30 +01:00
} |
'{' MkDict '}' {
$$ = gen_subexp(gen_op_const(LOADK, jv_object()));
2012-08-16 01:00:30 +01:00
block_append(&$$, $2);
block_append(&$$, gen_op_simple(POP));
} |
'$' IDENT {
$$ = gen_location(@$, gen_op_var_unbound(LOADV, jv_string_value($2)));
jv_free($2);
2012-08-21 18:14:13 +01:00
} |
IDENT {
$$ = gen_location(@$, gen_op_call(CALL_1_1, gen_op_block_unbound(CLOSURE_REF, jv_string_value($1))));
jv_free($1);
2012-08-26 14:25:56 +01:00
} |
IDENT '(' Exp ')' {
2012-08-26 14:25:56 +01:00
$$ = gen_op_call(CALL_1_1,
block_join(gen_op_block_unbound(CLOSURE_REF, jv_string_value($1)),
2012-08-26 14:25:56 +01:00
block_bind(gen_op_block_defn(CLOSURE_CREATE,
"lambda",
block_join($3, gen_op_simple(RET))),
2012-08-26 14:25:56 +01:00
gen_noop(), OP_IS_CALL_PSEUDO)));
$$ = gen_location(@1, $$);
jv_free($1);
} |
'(' error ')' { $$ = gen_noop(); } |
'[' error ']' { $$ = gen_noop(); } |
Term '[' error ']' { $$ = $1; } |
'{' error '}' { $$ = gen_noop(); }
2012-08-16 01:00:30 +01:00
MkDict:
{
$$=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
: IDENT ':' ExpD {
$$ = gen_dictpair(gen_op_const(LOADK, $1), $3);
2012-08-16 01:00:30 +01:00
}
| LITERAL ':' ExpD {
if (jv_get_kind($1) != JV_KIND_STRING) {
FAIL(@1, "Object keys must be strings");
}
$$ = gen_dictpair(gen_op_const(LOADK, $1), $3);
}
2012-08-16 01:00:30 +01:00
| IDENT {
$$ = gen_dictpair(gen_op_const(LOADK, jv_copy($1)),
gen_index(gen_noop(), gen_op_const(LOADK, $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) {
2012-08-16 01:00:30 +01:00
yyscan_t scanner;
YY_BUFFER_STATE buf;
jq_yylex_init_extra(0, &scanner);
buf = jq_yy_scan_bytes(locations->data, locations->length, scanner);
int errors = 0;
*answer = gen_noop();
yyparse(answer, &errors, locations, scanner);
jq_yy_delete_buffer(buf, scanner);
jq_yylex_destroy(scanner);
if (errors > 0) {
block_free(*answer);
*answer = gen_noop();
}
return errors;
2012-08-16 01:00:30 +01:00
}