2 * Copyright (C) 2017 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 * Provide more verbose and specific error messages instead of just "syntax
11 %define parse.error verbose
14 * Verbose error messages may contain incorrect information if LAC (Lookahead
15 * Correction) is not enabled.
17 %define parse.lac full
19 /* Avoid symbol clashes (lopsub might also expose yy* symbols). */
20 %define api.prefix {mp_yy}
23 * Although locations are automatically enabled as soon as the grammar uses the
24 * special @N tokens, specifying %locations explicitly allows for more accurate
25 * syntax error messages.
30 * Generate a pure (reentrant) parser. With this option enabled, yylval and
31 * yylloc become local variables in yyparse(), and a different calling
32 * convention is used for yylex().
36 /* Additional arguments to yylex(), yyparse() and yyerror() */
37 %param {struct mp_context *ctx}
38 %param {struct mp_ast_node **ast}
39 %param {mp_yyscan_t yyscanner} /* reentrant lexers */
51 int yylex(MP_YYSTYPE *lvalp, MP_YYLTYPE *llocp, struct mp_context *ctx,
52 struct mp_ast_node **ast, mp_yyscan_t yyscanner);
53 static void yyerror(YYLTYPE *llocp, struct mp_context *ctx,
54 struct mp_ast_node **ast, mp_yyscan_t yyscanner, const char *msg);
64 static struct mp_ast_node *ast_node_raw(int id)
66 struct mp_ast_node *node = para_malloc(sizeof(struct mp_ast_node));
71 /* This is non-static because it is also called from the lexer. */
72 struct mp_ast_node *mp_new_ast_leaf_node(int id)
74 struct mp_ast_node *node = ast_node_raw(id);
75 node->num_children = 0;
79 static struct mp_ast_node *ast_node_new_unary(int id, struct mp_ast_node *child)
81 struct mp_ast_node *node = ast_node_raw(id);
82 node->num_children = 1;
83 node->children = para_malloc(sizeof(struct mp_ast_node *));
84 node->children[0] = child;
88 static struct mp_ast_node *ast_node_new_binary(int id, struct mp_ast_node *left,
89 struct mp_ast_node *right)
91 struct mp_ast_node *node = ast_node_raw(id);
92 node->num_children = 2;
93 node->children = para_malloc(2 * sizeof(struct mp_ast_node *));
94 node->children[0] = left;
95 node->children[1] = right;
99 void mp_free_ast(struct mp_ast_node *root)
103 if (root->num_children > 0) {
105 for (i = 0; i < root->num_children; i++)
106 mp_free_ast(root->children[i]);
107 free(root->children);
109 union mp_semantic_value *sv = &root->sv;
115 regfree(&sv->re_pattern.preg);
117 case WILDCARD_PATTERN:
118 free(sv->wc_pattern.pat);
125 static int eval_node(struct mp_ast_node *node, struct mp_context *ctx,
126 union mp_semantic_value *result);
128 static void eval_binary_op(struct mp_ast_node *node, struct mp_context *ctx,
129 union mp_semantic_value *v1, union mp_semantic_value *v2)
131 eval_node(node->children[0], ctx, v1);
132 eval_node(node->children[1], ctx, v2);
135 static int eval_node(struct mp_ast_node *node, struct mp_context *ctx,
136 union mp_semantic_value *result)
140 union mp_semantic_value v1, v2;
145 result->strval = node->sv.strval;
148 result->strval = mp_path(ctx);
151 result->strval = mp_artist(ctx);
154 result->strval = mp_title(ctx);
157 result->strval = mp_album(ctx);
160 result->strval = mp_comment(ctx);
164 result->intval = node->sv.intval;
167 eval_binary_op(node, ctx, &v1, &v2);
168 result->intval = v1.intval + v2.intval;
171 eval_binary_op(node, ctx, &v1, &v2);
172 result->intval = v1.intval - v2.intval;
175 eval_binary_op(node, ctx, &v1, &v2);
176 result->intval = v1.intval * v2.intval;
179 eval_binary_op(node, ctx, &v1, &v2);
180 if (v2.intval == 0) {
183 PARA_ERROR_LOG("division by zero\n");
187 result->intval = v1.intval / v2.intval;
190 eval_node(node->children[0], ctx, &v1);
191 result->intval = -v1.intval;
194 result->intval = mp_year(ctx);
196 case NUM_ATTRIBUTES_SET:
197 result->intval = mp_num_attributes_set(ctx);
200 result->intval = mp_num_played(ctx);
203 result->intval = mp_image_id(ctx);
206 result->intval = mp_lyrics_id(ctx);
209 result->intval = mp_bitrate(ctx);
212 result->intval = mp_frequency(ctx);
215 result->intval= mp_channels(ctx);
219 arg = node->children[0]->sv.strval;
220 result->boolval = mp_is_set(arg, ctx);
223 result->boolval = true;
226 result->boolval = false;
229 eval_binary_op(node, ctx, &v1, &v2);
230 result->boolval = v1.boolval || v2.boolval;
233 eval_binary_op(node, ctx, &v1, &v2);
234 result->boolval = v1.boolval && v2.boolval;
237 eval_node(node->children[0], ctx, &v1);
238 result->boolval = !v1.boolval;
241 ret = eval_node(node->children[0], ctx, &v1);
242 eval_node(node->children[1], ctx, &v2);
243 if (ret == ST_STRVAL)
244 result->boolval = !strcmp(v1.strval, v2.strval);
246 result->boolval = v1.intval == v2.intval;
249 ret = eval_node(node->children[0], ctx, &v1);
250 eval_node(node->children[1], ctx, &v2);
251 if (ret == ST_STRVAL)
252 result->boolval = strcmp(v1.strval, v2.strval);
254 result->boolval = v1.intval != v2.intval;
257 eval_binary_op(node, ctx, &v1, &v2);
258 result->boolval = v1.intval < v2.intval;
261 eval_binary_op(node, ctx, &v1, &v2);
262 result->boolval = v1.intval > v2.intval;
265 eval_binary_op(node, ctx, &v1, &v2);
266 result->boolval = v1.intval <= v2.intval;
268 case GREATER_OR_EQUAL:
269 eval_binary_op(node, ctx, &v1, &v2);
270 result->boolval = v1.intval >= v2.intval;
273 eval_binary_op(node, ctx, &v1, &v2);
274 result->boolval = fnmatch(v2.wc_pattern.pat, v1.strval,
275 v2.wc_pattern.flags) == 0;
278 eval_binary_op(node, ctx, &v1, &v2);
279 result->boolval = regexec(&v2.re_pattern.preg, v1.strval,
283 result->re_pattern = node->sv.re_pattern;
284 return ST_REGEX_PATTERN;
285 case WILDCARD_PATTERN:
286 result->wc_pattern = node->sv.wc_pattern;
287 return ST_WC_PATTERN;
289 PARA_EMERG_LOG("bug: invalid node id %d\n", node->id);
294 bool mp_eval_ast(struct mp_ast_node *root, struct mp_context *ctx)
296 union mp_semantic_value v;
297 int ret = eval_node(root, ctx, &v);
299 if (ret == ST_INTVAL)
300 return v.intval != 0;
301 if (ret == ST_STRVAL)
302 return v.strval[0] != 0;
303 if (ret == ST_BOOLVAL)
311 struct mp_ast_node *node;
316 %token <node> STRING_LITERAL
317 %token <node> REGEX_PATTERN
318 %token <node> WILDCARD_PATTERN
320 /* keywords with semantic value */
325 %token <node> COMMENT
327 %token <node> NUM_ATTRIBUTES_SET
328 %token <node> NUM_PLAYED
329 %token <node> IMAGE_ID
330 %token <node> LYRICS_ID
331 %token <node> BITRATE
332 %token <node> FREQUENCY
333 %token <node> CHANNELS
334 %token <node> FALSE TRUE
336 /* keywords without semantic value */
339 /* operators, ordered by precendence */
342 %left EQUAL NOT_EQUAL
343 %left LESS_THAN LESS_OR_EQUAL GREATER_OR_EQUAL REGEX_MATCH FILENAME_MATCH
346 %right NOT NEG /* negation (unary minus) */
356 /* empty */ {*ast = NULL; return 0;}
357 | string {*ast = $1; return 0;}
358 | exp {*ast = $1; return 0;}
359 | boolexp {*ast = $1; return 0;}
361 string: STRING_LITERAL {$$ = $1;}
362 | PATH {$$ = mp_new_ast_leaf_node(PATH);}
363 | ARTIST {$$ = mp_new_ast_leaf_node(ARTIST);}
364 | TITLE {$$ = mp_new_ast_leaf_node(TITLE);}
365 | ALBUM {$$ = mp_new_ast_leaf_node(ALBUM);}
366 | COMMENT {$$ = mp_new_ast_leaf_node(COMMENT);}
370 | exp '+' exp {$$ = ast_node_new_binary('+', $1, $3);}
371 | exp '-' exp {$$ = ast_node_new_binary('-', $1, $3);}
372 | exp '*' exp {$$ = ast_node_new_binary('*', $1, $3);}
373 | exp '/' exp {$$ = ast_node_new_binary('/', $1, $3);}
374 | '-' exp %prec NEG {$$ = ast_node_new_unary(NEG, $2);}
375 | '(' exp ')' {$$ = $2;}
376 | YEAR {$$ = mp_new_ast_leaf_node(YEAR);}
377 | NUM_ATTRIBUTES_SET {$$ = mp_new_ast_leaf_node(NUM_ATTRIBUTES_SET);}
378 | NUM_PLAYED {$$ = mp_new_ast_leaf_node(NUM_PLAYED);}
379 | IMAGE_ID {$$ = mp_new_ast_leaf_node(IMAGE_ID);}
380 | LYRICS_ID {$$ = mp_new_ast_leaf_node(LYRICS_ID);}
381 | BITRATE {$$ = mp_new_ast_leaf_node(BITRATE);}
382 | FREQUENCY {$$ = mp_new_ast_leaf_node(FREQUENCY);}
383 | CHANNELS {$$ = mp_new_ast_leaf_node(CHANNELS);}
386 boolexp: IS_SET '(' STRING_LITERAL ')' {$$ = ast_node_new_unary(IS_SET, $3);}
387 | TRUE {$$ = mp_new_ast_leaf_node(TRUE);}
388 | FALSE {$$ = mp_new_ast_leaf_node(FALSE);}
389 | '(' boolexp ')' {$$ = $2;}
390 | boolexp OR boolexp {$$ = ast_node_new_binary(OR, $1, $3);}
391 | boolexp AND boolexp {$$ = ast_node_new_binary(AND, $1, $3);}
392 | NOT boolexp {$$ = ast_node_new_unary(NOT, $2);}
393 | exp EQUAL exp {$$ = ast_node_new_binary(EQUAL, $1, $3);}
394 | exp NOT_EQUAL exp {$$ = ast_node_new_binary(NOT_EQUAL, $1, $3);}
395 | exp '<' exp {$$ = ast_node_new_binary('<', $1, $3);}
396 | exp '>' exp {$$ = ast_node_new_binary('>', $1, $3);}
397 | exp LESS_OR_EQUAL exp {
398 $$ = ast_node_new_binary(LESS_OR_EQUAL, $1, $3);
400 | exp GREATER_OR_EQUAL exp {
401 $$ = ast_node_new_binary(GREATER_OR_EQUAL, $1, $3);
403 | string REGEX_MATCH REGEX_PATTERN {
404 $$ = ast_node_new_binary(REGEX_MATCH, $1, $3);
406 | string FILENAME_MATCH WILDCARD_PATTERN {
407 $$ = ast_node_new_binary(FILENAME_MATCH, $1, $3);
409 | string EQUAL string {$$ = ast_node_new_binary(EQUAL, $1, $3);}
410 | string NOT_EQUAL string {$$ = ast_node_new_binary(NOT_EQUAL, $1, $3);}
414 /* Called by yyparse() on error */
415 static void yyerror(YYLTYPE *llocp, struct mp_context *ctx,
416 struct mp_ast_node **ast, mp_yyscan_t yyscanner, const char *msg)
418 mp_parse_error(llocp->first_line, ctx, "%s", msg);