1 /* Copyright (C) 2017 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
4 * Provide more verbose and specific error messages instead of just "syntax
7 %define parse.error verbose
10 * Verbose error messages may contain incorrect information if LAC (Lookahead
11 * Correction) is not enabled.
13 %define parse.lac full
15 /* Avoid symbol clashes (lopsub might also expose yy* symbols). */
16 %define api.prefix {mp_yy}
19 * Although locations are automatically enabled as soon as the grammar uses the
20 * special @N tokens, specifying %locations explicitly allows for more accurate
21 * syntax error messages.
26 * Generate a pure (reentrant) parser. With this option enabled, yylval and
27 * yylloc become local variables in yyparse(), and a different calling
28 * convention is used for yylex().
32 /* Additional arguments to yylex(), yyparse() and yyerror() */
33 %param {struct mp_context *ctx}
34 %param {struct mp_ast_node **ast}
35 %param {mp_yyscan_t yyscanner} /* reentrant lexers */
47 int yylex(MP_YYSTYPE *lvalp, MP_YYLTYPE *llocp, struct mp_context *ctx,
48 struct mp_ast_node **ast, mp_yyscan_t yyscanner);
49 static void yyerror(YYLTYPE *llocp, struct mp_context *ctx,
50 struct mp_ast_node **ast, mp_yyscan_t yyscanner, const char *msg);
60 static struct mp_ast_node *ast_node_raw(int id)
62 struct mp_ast_node *node = para_malloc(sizeof(struct mp_ast_node));
67 /* This is non-static because it is also called from the lexer. */
68 struct mp_ast_node *mp_new_ast_leaf_node(int id)
70 struct mp_ast_node *node = ast_node_raw(id);
71 node->num_children = 0;
75 static struct mp_ast_node *ast_node_new_unary(int id, struct mp_ast_node *child)
77 struct mp_ast_node *node = ast_node_raw(id);
78 node->num_children = 1;
79 node->children = para_malloc(sizeof(struct mp_ast_node *));
80 node->children[0] = child;
84 static struct mp_ast_node *ast_node_new_binary(int id, struct mp_ast_node *left,
85 struct mp_ast_node *right)
87 struct mp_ast_node *node = ast_node_raw(id);
88 node->num_children = 2;
89 node->children = para_malloc(2 * sizeof(struct mp_ast_node *));
90 node->children[0] = left;
91 node->children[1] = right;
95 void mp_free_ast(struct mp_ast_node *root)
99 if (root->num_children > 0) {
101 for (i = 0; i < root->num_children; i++)
102 mp_free_ast(root->children[i]);
103 free(root->children);
105 union mp_semantic_value *sv = &root->sv;
111 regfree(&sv->re_pattern.preg);
113 case WILDCARD_PATTERN:
114 free(sv->wc_pattern.pat);
121 static int eval_node(struct mp_ast_node *node, struct mp_context *ctx,
122 union mp_semantic_value *result);
124 static void eval_binary_op(struct mp_ast_node *node, struct mp_context *ctx,
125 union mp_semantic_value *v1, union mp_semantic_value *v2)
127 eval_node(node->children[0], ctx, v1);
128 eval_node(node->children[1], ctx, v2);
131 static int eval_node(struct mp_ast_node *node, struct mp_context *ctx,
132 union mp_semantic_value *result)
136 union mp_semantic_value v1, v2;
141 result->strval = node->sv.strval;
144 result->strval = mp_path(ctx);
147 result->strval = mp_artist(ctx);
150 result->strval = mp_title(ctx);
153 result->strval = mp_album(ctx);
156 result->strval = mp_comment(ctx);
160 result->intval = node->sv.intval;
163 eval_binary_op(node, ctx, &v1, &v2);
164 result->intval = v1.intval + v2.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 if (v2.intval == 0) {
179 PARA_ERROR_LOG("division by zero\n");
183 result->intval = v1.intval / v2.intval;
186 eval_node(node->children[0], ctx, &v1);
187 result->intval = -v1.intval;
190 result->intval = mp_year(ctx);
192 case NUM_ATTRIBUTES_SET:
193 result->intval = mp_num_attributes_set(ctx);
196 result->intval = mp_num_played(ctx);
199 result->intval = mp_image_id(ctx);
202 result->intval = mp_lyrics_id(ctx);
205 result->intval = mp_bitrate(ctx);
208 result->intval = mp_frequency(ctx);
211 result->intval= mp_channels(ctx);
215 arg = node->children[0]->sv.strval;
216 result->boolval = mp_is_set(arg, ctx);
219 result->boolval = true;
222 result->boolval = false;
225 eval_binary_op(node, ctx, &v1, &v2);
226 result->boolval = v1.boolval || v2.boolval;
229 eval_binary_op(node, ctx, &v1, &v2);
230 result->boolval = v1.boolval && v2.boolval;
233 eval_node(node->children[0], ctx, &v1);
234 result->boolval = !v1.boolval;
237 ret = eval_node(node->children[0], ctx, &v1);
238 eval_node(node->children[1], ctx, &v2);
239 if (ret == ST_STRVAL)
240 result->boolval = !strcmp(v1.strval, v2.strval);
242 result->boolval = v1.intval == v2.intval;
245 ret = eval_node(node->children[0], ctx, &v1);
246 eval_node(node->children[1], ctx, &v2);
247 if (ret == ST_STRVAL)
248 result->boolval = strcmp(v1.strval, v2.strval);
250 result->boolval = v1.intval != v2.intval;
253 eval_binary_op(node, ctx, &v1, &v2);
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;
264 case GREATER_OR_EQUAL:
265 eval_binary_op(node, ctx, &v1, &v2);
266 result->boolval = v1.intval >= v2.intval;
269 eval_binary_op(node, ctx, &v1, &v2);
270 result->boolval = fnmatch(v2.wc_pattern.pat, v1.strval,
271 v2.wc_pattern.flags) == 0;
274 eval_binary_op(node, ctx, &v1, &v2);
275 result->boolval = regexec(&v2.re_pattern.preg, v1.strval,
279 result->re_pattern = node->sv.re_pattern;
280 return ST_REGEX_PATTERN;
281 case WILDCARD_PATTERN:
282 result->wc_pattern = node->sv.wc_pattern;
283 return ST_WC_PATTERN;
285 PARA_EMERG_LOG("bug: invalid node id %d\n", node->id);
290 bool mp_eval_ast(struct mp_ast_node *root, struct mp_context *ctx)
292 union mp_semantic_value v;
293 int ret = eval_node(root, ctx, &v);
295 if (ret == ST_INTVAL)
296 return v.intval != 0;
297 if (ret == ST_STRVAL)
298 return v.strval[0] != 0;
299 if (ret == ST_BOOLVAL)
307 struct mp_ast_node *node;
312 %token <node> STRING_LITERAL
313 %token <node> REGEX_PATTERN
314 %token <node> WILDCARD_PATTERN
316 /* keywords with semantic value */
321 %token <node> COMMENT
323 %token <node> NUM_ATTRIBUTES_SET
324 %token <node> NUM_PLAYED
325 %token <node> IMAGE_ID
326 %token <node> LYRICS_ID
327 %token <node> BITRATE
328 %token <node> FREQUENCY
329 %token <node> CHANNELS
330 %token <node> FALSE TRUE
332 /* keywords without semantic value */
335 /* operators, ordered by precendence */
338 %left EQUAL NOT_EQUAL
339 %left LESS_THAN LESS_OR_EQUAL GREATER_OR_EQUAL REGEX_MATCH FILENAME_MATCH
342 %right NOT NEG /* negation (unary minus) */
352 /* empty */ {*ast = NULL; return 0;}
353 | string {*ast = $1; return 0;}
354 | exp {*ast = $1; return 0;}
355 | boolexp {*ast = $1; return 0;}
357 string: STRING_LITERAL {$$ = $1;}
358 | PATH {$$ = mp_new_ast_leaf_node(PATH);}
359 | ARTIST {$$ = mp_new_ast_leaf_node(ARTIST);}
360 | TITLE {$$ = mp_new_ast_leaf_node(TITLE);}
361 | ALBUM {$$ = mp_new_ast_leaf_node(ALBUM);}
362 | COMMENT {$$ = mp_new_ast_leaf_node(COMMENT);}
366 | exp '+' exp {$$ = ast_node_new_binary('+', $1, $3);}
367 | exp '-' exp {$$ = ast_node_new_binary('-', $1, $3);}
368 | exp '*' exp {$$ = ast_node_new_binary('*', $1, $3);}
369 | exp '/' exp {$$ = ast_node_new_binary('/', $1, $3);}
370 | '-' exp %prec NEG {$$ = ast_node_new_unary(NEG, $2);}
371 | '(' exp ')' {$$ = $2;}
372 | YEAR {$$ = mp_new_ast_leaf_node(YEAR);}
373 | NUM_ATTRIBUTES_SET {$$ = mp_new_ast_leaf_node(NUM_ATTRIBUTES_SET);}
374 | NUM_PLAYED {$$ = mp_new_ast_leaf_node(NUM_PLAYED);}
375 | IMAGE_ID {$$ = mp_new_ast_leaf_node(IMAGE_ID);}
376 | LYRICS_ID {$$ = mp_new_ast_leaf_node(LYRICS_ID);}
377 | BITRATE {$$ = mp_new_ast_leaf_node(BITRATE);}
378 | FREQUENCY {$$ = mp_new_ast_leaf_node(FREQUENCY);}
379 | CHANNELS {$$ = mp_new_ast_leaf_node(CHANNELS);}
382 boolexp: IS_SET '(' STRING_LITERAL ')' {$$ = ast_node_new_unary(IS_SET, $3);}
383 | TRUE {$$ = mp_new_ast_leaf_node(TRUE);}
384 | FALSE {$$ = mp_new_ast_leaf_node(FALSE);}
385 | '(' boolexp ')' {$$ = $2;}
386 | boolexp OR boolexp {$$ = ast_node_new_binary(OR, $1, $3);}
387 | boolexp AND boolexp {$$ = ast_node_new_binary(AND, $1, $3);}
388 | NOT boolexp {$$ = ast_node_new_unary(NOT, $2);}
389 | exp EQUAL exp {$$ = ast_node_new_binary(EQUAL, $1, $3);}
390 | exp NOT_EQUAL exp {$$ = ast_node_new_binary(NOT_EQUAL, $1, $3);}
391 | exp '<' exp {$$ = ast_node_new_binary('<', $1, $3);}
392 | exp '>' exp {$$ = ast_node_new_binary('>', $1, $3);}
393 | exp LESS_OR_EQUAL exp {
394 $$ = ast_node_new_binary(LESS_OR_EQUAL, $1, $3);
396 | exp GREATER_OR_EQUAL exp {
397 $$ = ast_node_new_binary(GREATER_OR_EQUAL, $1, $3);
399 | string REGEX_MATCH REGEX_PATTERN {
400 $$ = ast_node_new_binary(REGEX_MATCH, $1, $3);
402 | string FILENAME_MATCH WILDCARD_PATTERN {
403 $$ = ast_node_new_binary(FILENAME_MATCH, $1, $3);
405 | string EQUAL string {$$ = ast_node_new_binary(EQUAL, $1, $3);}
406 | string NOT_EQUAL string {$$ = ast_node_new_binary(NOT_EQUAL, $1, $3);}
410 /* Called by yyparse() on error */
411 static void yyerror(YYLTYPE *llocp, struct mp_context *ctx,
412 struct mp_ast_node **ast, mp_yyscan_t yyscanner, const char *msg)
414 mp_parse_error(llocp->first_line, ctx, "%s", msg);