5 * LTTng filter expression parser
7 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
32 #include "filter-ast.h"
33 #include "filter-parser.h"
35 __attribute__((visibility("hidden")))
37 __attribute__((visibility("hidden")))
38 int filter_parser_debug = 0;
40 __attribute__((visibility("hidden")))
41 int yyparse(struct filter_parser_ctx *parser_ctx);
42 __attribute__((visibility("hidden")))
43 int yylex(union YYSTYPE *yyval, struct filter_parser_ctx *parser_ctx);
44 __attribute__((visibility("hidden")))
45 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
46 __attribute__((visibility("hidden")))
47 int yylex_destroy(yyscan_t yyparser_ctx);
48 __attribute__((visibility("hidden")))
49 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
52 struct cds_list_head gc;
57 static const char *node_type_to_str[] = {
58 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
59 [ NODE_ROOT ] = "NODE_ROOT",
60 [ NODE_EXPRESSION ] = "NODE_EXPRESSION",
61 [ NODE_OP ] = "NODE_OP",
62 [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
65 __attribute__((visibility("hidden")))
66 const char *node_type(struct filter_node *node)
68 if (node->type < NR_NODE_TYPES)
69 return node_type_to_str[node->type];
74 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
77 struct gc_string *gstr;
80 /* TODO: could be faster with find first bit or glib Gstring */
81 /* sizeof long to account for malloc header (int or long ?) */
82 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
85 gstr = malloc(alloclen);
86 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
87 gstr->alloclen = alloclen;
92 * note: never use gc_string_append on a string that has external references.
93 * gsrc will be garbage collected immediately, and gstr might be.
94 * Should only be used to append characters to a string literal or constant.
96 __attribute__((visibility("hidden")))
97 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
98 struct gc_string *gstr,
99 struct gc_string *gsrc)
101 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
104 /* TODO: could be faster with find first bit or glib Gstring */
105 /* sizeof long to account for malloc header (int or long ?) */
106 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
109 if (alloclen > gstr->alloclen) {
110 struct gc_string *newgstr;
112 newgstr = gc_string_alloc(parser_ctx, newlen);
113 strcpy(newgstr->s, gstr->s);
114 strcat(newgstr->s, gsrc->s);
115 cds_list_del(&gstr->gc);
119 strcat(gstr->s, gsrc->s);
121 cds_list_del(&gsrc->gc);
126 __attribute__((visibility("hidden")))
127 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
129 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
130 strcpy(lvalp->gs->s, src);
133 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
136 struct filter_ast *ast = filter_parser_get_ast(scanner);
137 struct filter_node *node;
139 node = malloc(sizeof(*node));
142 memset(node, 0, sizeof(*node));
144 cds_list_add(&node->gc, &ast->allocated_nodes);
148 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
151 case NODE_EXPRESSION:
160 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
168 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
170 struct filter_node *lchild,
171 struct filter_node *rchild)
173 struct filter_ast *ast = filter_parser_get_ast(scanner);
174 struct filter_node *node;
176 node = malloc(sizeof(*node));
179 memset(node, 0, sizeof(*node));
180 node->type = NODE_OP;
181 cds_list_add(&node->gc, &ast->allocated_nodes);
182 node->u.op.type = type;
183 node->u.op.lchild = lchild;
184 node->u.op.rchild = rchild;
188 __attribute__((visibility("hidden")))
189 void yyerror(struct filter_parser_ctx *parser_ctx, const char *str)
191 fprintf(stderr, "error %s\n", str);
194 __attribute__((visibility("hidden")))
200 #define parse_error(parser_ctx, str) \
202 yyerror(parser_ctx, YY_("parse error: " str "\n")); \
206 static void free_strings(struct cds_list_head *list)
208 struct gc_string *gstr, *tmp;
210 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
214 static struct filter_ast *filter_ast_alloc(void)
216 struct filter_ast *ast;
218 ast = malloc(sizeof(*ast));
221 memset(ast, 0, sizeof(*ast));
222 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
223 ast->root.type = NODE_ROOT;
227 static void filter_ast_free(struct filter_ast *ast)
229 struct filter_node *node, *tmp;
231 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
235 __attribute__((visibility("hidden")))
236 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
238 return yyparse(parser_ctx);
241 __attribute__((visibility("hidden")))
242 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
244 struct filter_parser_ctx *parser_ctx;
247 yydebug = filter_parser_debug;
249 parser_ctx = malloc(sizeof(*parser_ctx));
252 memset(parser_ctx, 0, sizeof(*parser_ctx));
254 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
256 fprintf(stderr, "yylex_init error\n");
257 goto cleanup_parser_ctx;
259 /* Start processing new stream */
260 yyrestart(input, parser_ctx->scanner);
262 parser_ctx->ast = filter_ast_alloc();
263 if (!parser_ctx->ast)
265 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
268 fprintf(stdout, "parser_ctx input is a%s.\n",
269 isatty(fileno(input)) ? "n interactive tty" :
270 " noninteractive file");
275 ret = yylex_destroy(parser_ctx->scanner);
277 fprintf(stderr, "yylex_destroy error\n");
283 __attribute__((visibility("hidden")))
284 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
288 free_strings(&parser_ctx->allocated_strings);
289 filter_ast_free(parser_ctx->ast);
290 ret = yylex_destroy(parser_ctx->scanner);
292 fprintf(stderr, "yylex_destroy error\n");
300 %parse-param {struct filter_parser_ctx *parser_ctx}
301 %lex-param {struct filter_parser_ctx *parser_ctx}
302 %start translation_unit
303 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
304 %token ESCSEQ CHAR_STRING_TOKEN
305 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
306 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
307 %token STAR PLUS MINUS
308 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
309 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
310 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
311 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
313 %token <gs> IDENTIFIER
319 struct gc_string *gs;
320 struct filter_node *n;
323 %type <gs> s_char s_char_sequence c_char c_char_sequence
325 %type <n> primary_expression
326 %type <n> postfix_expression
327 %type <n> unary_expression
328 %type <n> unary_operator
329 %type <n> multiplicative_expression
330 %type <n> additive_expression
331 %type <n> shift_expression
332 %type <n> relational_expression
333 %type <n> equality_expression
334 %type <n> and_expression
335 %type <n> exclusive_or_expression
336 %type <n> inclusive_or_expression
337 %type <n> logical_and_expression
338 %type <n> logical_or_expression
349 | c_char_sequence c_char
350 { $$ = gc_string_append(parser_ctx, $1, $2); }
358 parse_error(parser_ctx, "escape sequences not supported yet");
362 /* 1.6 String literals */
367 | s_char_sequence s_char
368 { $$ = gc_string_append(parser_ctx, $1, $2); }
376 parse_error(parser_ctx, "escape sequences not supported yet");
383 $$ = make_node(parser_ctx, NODE_EXPRESSION);
384 $$->u.expression.type = AST_EXP_IDENTIFIER;
385 $$->u.expression.u.identifier = yylval.gs->s;
389 $$ = make_node(parser_ctx, NODE_EXPRESSION);
390 $$->u.expression.type = AST_EXP_CONSTANT;
391 sscanf(yylval.gs->s, "%" PRIu64,
392 &$$->u.expression.u.constant);
396 $$ = make_node(parser_ctx, NODE_EXPRESSION);
397 $$->u.expression.type = AST_EXP_CONSTANT;
398 sscanf(yylval.gs->s, "0%" PRIo64,
399 &$$->u.expression.u.constant);
401 | HEXADECIMAL_CONSTANT
403 $$ = make_node(parser_ctx, NODE_EXPRESSION);
404 $$->u.expression.type = AST_EXP_CONSTANT;
405 sscanf(yylval.gs->s, "0x%" PRIx64,
406 &$$->u.expression.u.constant);
410 $$ = make_node(parser_ctx, NODE_EXPRESSION);
411 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
412 sscanf(yylval.gs->s, "%lg",
413 &$$->u.expression.u.float_constant);
415 | STRING_LITERAL_START DQUOTE
417 $$ = make_node(parser_ctx, NODE_EXPRESSION);
418 $$->u.expression.type = AST_EXP_STRING;
419 $$->u.expression.u.string = "";
421 | STRING_LITERAL_START s_char_sequence DQUOTE
423 $$ = make_node(parser_ctx, NODE_EXPRESSION);
424 $$->u.expression.type = AST_EXP_STRING;
425 $$->u.expression.u.string = $2->s;
427 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
429 $$ = make_node(parser_ctx, NODE_EXPRESSION);
430 $$->u.expression.type = AST_EXP_STRING;
431 $$->u.expression.u.string = $2->s;
433 | LPAREN expression RPAREN
435 $$ = make_node(parser_ctx, NODE_EXPRESSION);
436 $$->u.expression.type = AST_EXP_NESTED;
437 $$->u.expression.u.child = $2;
444 | postfix_expression DOT IDENTIFIER
446 $$ = make_node(parser_ctx, NODE_EXPRESSION);
447 $$->u.expression.type = AST_EXP_IDENTIFIER;
448 $$->u.expression.post_op = AST_LINK_DOT;
449 $$->u.expression.u.identifier = $3->s;
450 $$->u.expression.prev = $1;
452 | postfix_expression RARROW IDENTIFIER
454 $$ = make_node(parser_ctx, NODE_EXPRESSION);
455 $$->u.expression.type = AST_EXP_IDENTIFIER;
456 $$->u.expression.post_op = AST_LINK_RARROW;
457 $$->u.expression.u.identifier = $3->s;
458 $$->u.expression.prev = $1;
465 | unary_operator unary_expression
468 $$->u.unary_op.child = $2;
475 $$ = make_node(parser_ctx, NODE_UNARY_OP);
476 $$->u.unary_op.type = AST_UNARY_PLUS;
480 $$ = make_node(parser_ctx, NODE_UNARY_OP);
481 $$->u.unary_op.type = AST_UNARY_MINUS;
485 $$ = make_node(parser_ctx, NODE_UNARY_OP);
486 $$->u.unary_op.type = AST_UNARY_NOT;
490 $$ = make_node(parser_ctx, NODE_UNARY_OP);
491 $$->u.unary_op.type = AST_UNARY_BIN_NOT;
495 multiplicative_expression
498 | multiplicative_expression STAR unary_expression
500 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
502 | multiplicative_expression DIV_OP unary_expression
504 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
506 | multiplicative_expression MOD_OP unary_expression
508 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
513 : multiplicative_expression
515 | additive_expression PLUS multiplicative_expression
517 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
519 | additive_expression MINUS multiplicative_expression
521 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
526 : additive_expression
528 | shift_expression LEFT_OP additive_expression
530 $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3);
532 | shift_expression RIGHT_OP additive_expression
534 $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3);
538 relational_expression
541 | relational_expression LT_OP shift_expression
543 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
545 | relational_expression GT_OP shift_expression
547 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
549 | relational_expression LE_OP shift_expression
551 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
553 | relational_expression GE_OP shift_expression
555 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
560 : relational_expression
562 | equality_expression EQ_OP relational_expression
564 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
566 | equality_expression NE_OP relational_expression
568 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
573 : equality_expression
575 | and_expression AND_BIN equality_expression
577 $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3);
581 exclusive_or_expression
584 | exclusive_or_expression XOR_BIN and_expression
586 $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3);
590 inclusive_or_expression
591 : exclusive_or_expression
593 | inclusive_or_expression OR_BIN exclusive_or_expression
595 $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3);
599 logical_and_expression
600 : inclusive_or_expression
602 | logical_and_expression AND_OP inclusive_or_expression
604 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
608 logical_or_expression
609 : logical_and_expression
611 | logical_or_expression OR_OP logical_and_expression
613 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
618 : logical_or_expression
625 parser_ctx->ast->root.u.root.child = $1;