Add LTTNG_HIDDEN macro for hidden attribute
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-parser.y
CommitLineData
953192ba
MD
1%{
2/*
3 * filter-parser.y
4 *
5 * LTTng filter expression parser
6 *
7 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
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.
12 *
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.
17 *
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
21 *
22 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
23 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <string.h>
28#include <stdlib.h>
29#include <assert.h>
30#include <errno.h>
31#include <inttypes.h>
953192ba 32#include "filter-ast.h"
95b9bd90 33#include "filter-parser.h"
953192ba 34
5439dd25 35__attribute__((visibility("hidden")))
953192ba 36int yydebug;
d00c599e 37__attribute__((visibility("hidden")))
953192ba
MD
38int filter_parser_debug = 0;
39
5439dd25 40__attribute__((visibility("hidden")))
953192ba 41int yyparse(struct filter_parser_ctx *parser_ctx);
5439dd25 42__attribute__((visibility("hidden")))
953192ba 43int yylex(union YYSTYPE *yyval, struct filter_parser_ctx *parser_ctx);
5439dd25 44__attribute__((visibility("hidden")))
953192ba 45int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
5439dd25 46__attribute__((visibility("hidden")))
953192ba 47int yylex_destroy(yyscan_t yyparser_ctx);
5439dd25 48__attribute__((visibility("hidden")))
953192ba
MD
49void yyrestart(FILE * in_str, yyscan_t parser_ctx);
50
51struct gc_string {
52 struct cds_list_head gc;
53 size_t alloclen;
54 char s[];
55};
56
57static 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",
63};
64
5439dd25 65__attribute__((visibility("hidden")))
953192ba
MD
66const char *node_type(struct filter_node *node)
67{
68 if (node->type < NR_NODE_TYPES)
69 return node_type_to_str[node->type];
70 else
71 return NULL;
72}
73
74static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
75 size_t len)
76{
77 struct gc_string *gstr;
78 size_t alloclen;
79
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;
83 alloclen *= 2);
84
85 gstr = malloc(alloclen);
86 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
87 gstr->alloclen = alloclen;
88 return gstr;
89}
90
91/*
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.
95 */
5439dd25 96__attribute__((visibility("hidden")))
953192ba
MD
97struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
98 struct gc_string *gstr,
99 struct gc_string *gsrc)
100{
101 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
102 size_t alloclen;
103
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;
107 alloclen *= 2);
108
109 if (alloclen > gstr->alloclen) {
110 struct gc_string *newgstr;
111
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);
116 free(gstr);
117 gstr = newgstr;
118 } else {
119 strcat(gstr->s, gsrc->s);
120 }
121 cds_list_del(&gsrc->gc);
122 free(gsrc);
123 return gstr;
124}
125
5439dd25 126__attribute__((visibility("hidden")))
953192ba
MD
127void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
128{
129 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
130 strcpy(lvalp->gs->s, src);
131}
132
133static struct filter_node *make_node(struct filter_parser_ctx *scanner,
134 enum node_type type)
135{
136 struct filter_ast *ast = filter_parser_get_ast(scanner);
137 struct filter_node *node;
138
139 node = malloc(sizeof(*node));
140 if (!node)
141 return NULL;
142 memset(node, 0, sizeof(*node));
143 node->type = type;
144 cds_list_add(&node->gc, &ast->allocated_nodes);
145
146 switch (type) {
147 case NODE_ROOT:
148 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
149 break;
150
151 case NODE_EXPRESSION:
152 break;
153 case NODE_OP:
154 break;
155 case NODE_UNARY_OP:
156 break;
157
158 case NODE_UNKNOWN:
159 default:
160 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
161 (int) type);
162 break;
163 }
164
165 return node;
166}
167
168static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
169 enum op_type type,
170 struct filter_node *lchild,
171 struct filter_node *rchild)
172{
173 struct filter_ast *ast = filter_parser_get_ast(scanner);
174 struct filter_node *node;
175
176 node = malloc(sizeof(*node));
177 if (!node)
178 return NULL;
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;
185 return node;
186}
187
5439dd25 188__attribute__((visibility("hidden")))
953192ba
MD
189void yyerror(struct filter_parser_ctx *parser_ctx, const char *str)
190{
191 fprintf(stderr, "error %s\n", str);
192}
193
5439dd25 194__attribute__((visibility("hidden")))
953192ba
MD
195int yywrap(void)
196{
197 return 1;
198}
199
200#define parse_error(parser_ctx, str) \
201do { \
202 yyerror(parser_ctx, YY_("parse error: " str "\n")); \
203 YYERROR; \
204} while (0)
205
206static void free_strings(struct cds_list_head *list)
207{
208 struct gc_string *gstr, *tmp;
209
210 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
211 free(gstr);
212}
213
214static struct filter_ast *filter_ast_alloc(void)
215{
216 struct filter_ast *ast;
217
218 ast = malloc(sizeof(*ast));
219 if (!ast)
220 return NULL;
221 memset(ast, 0, sizeof(*ast));
222 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
223 ast->root.type = NODE_ROOT;
224 return ast;
225}
226
227static void filter_ast_free(struct filter_ast *ast)
228{
229 struct filter_node *node, *tmp;
230
231 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
232 free(node);
37600d79 233 free(ast);
953192ba
MD
234}
235
d00c599e 236__attribute__((visibility("hidden")))
953192ba
MD
237int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
238{
239 return yyparse(parser_ctx);
240}
241
d00c599e 242__attribute__((visibility("hidden")))
953192ba
MD
243struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
244{
245 struct filter_parser_ctx *parser_ctx;
246 int ret;
247
248 yydebug = filter_parser_debug;
249
250 parser_ctx = malloc(sizeof(*parser_ctx));
251 if (!parser_ctx)
252 return NULL;
253 memset(parser_ctx, 0, sizeof(*parser_ctx));
254
255 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
256 if (ret) {
257 fprintf(stderr, "yylex_init error\n");
258 goto cleanup_parser_ctx;
259 }
260 /* Start processing new stream */
261 yyrestart(input, parser_ctx->scanner);
262
263 parser_ctx->ast = filter_ast_alloc();
264 if (!parser_ctx->ast)
265 goto cleanup_lexer;
266 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
267
268 if (yydebug)
269 fprintf(stdout, "parser_ctx input is a%s.\n",
270 isatty(fileno(input)) ? "n interactive tty" :
271 " noninteractive file");
272
273 return parser_ctx;
274
275cleanup_lexer:
276 ret = yylex_destroy(parser_ctx->scanner);
277 if (!ret)
278 fprintf(stderr, "yylex_destroy error\n");
279cleanup_parser_ctx:
280 free(parser_ctx);
281 return NULL;
282}
283
d00c599e 284__attribute__((visibility("hidden")))
953192ba
MD
285void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
286{
287 int ret;
288
289 free_strings(&parser_ctx->allocated_strings);
290 filter_ast_free(parser_ctx->ast);
291 ret = yylex_destroy(parser_ctx->scanner);
292 if (ret)
293 fprintf(stderr, "yylex_destroy error\n");
294 free(parser_ctx);
295}
296
297%}
298
299%define api.pure
300 /* %locations */
301%parse-param {struct filter_parser_ctx *parser_ctx}
302%lex-param {struct filter_parser_ctx *parser_ctx}
303%start translation_unit
304%token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
305%token ESCSEQ CHAR_STRING_TOKEN
e90d8561 306%token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
953192ba
MD
307%token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
308%token STAR PLUS MINUS
309%token MOD_OP DIV_OP RIGHT_OP LEFT_OP
310%token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
311%token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
312%token XOR_BIN AND_BIN OR_BIN NOT_BIN
313
314%token <gs> IDENTIFIER
315%token ERROR
316%union
317{
318 long long ll;
319 char c;
320 struct gc_string *gs;
321 struct filter_node *n;
322}
323
324%type <gs> s_char s_char_sequence c_char c_char_sequence
325
326%type <n> primary_expression
327%type <n> postfix_expression
328%type <n> unary_expression
329%type <n> unary_operator
330%type <n> multiplicative_expression
331%type <n> additive_expression
332%type <n> shift_expression
333%type <n> relational_expression
334%type <n> equality_expression
335%type <n> and_expression
336%type <n> exclusive_or_expression
337%type <n> inclusive_or_expression
338%type <n> logical_and_expression
339%type <n> logical_or_expression
340%type <n> expression
341
342%%
343
344
345/* 1.5 Constants */
346
347c_char_sequence:
348 c_char
349 { $$ = $1; }
350 | c_char_sequence c_char
351 { $$ = gc_string_append(parser_ctx, $1, $2); }
352 ;
353
354c_char:
355 CHAR_STRING_TOKEN
356 { $$ = yylval.gs; }
357 | ESCSEQ
358 {
359 parse_error(parser_ctx, "escape sequences not supported yet");
360 }
361 ;
362
363/* 1.6 String literals */
364
365s_char_sequence:
366 s_char
367 { $$ = $1; }
368 | s_char_sequence s_char
369 { $$ = gc_string_append(parser_ctx, $1, $2); }
370 ;
371
372s_char:
373 CHAR_STRING_TOKEN
374 { $$ = yylval.gs; }
375 | ESCSEQ
376 {
377 parse_error(parser_ctx, "escape sequences not supported yet");
378 }
379 ;
380
381primary_expression
382 : IDENTIFIER
383 {
384 $$ = make_node(parser_ctx, NODE_EXPRESSION);
385 $$->u.expression.type = AST_EXP_IDENTIFIER;
386 $$->u.expression.u.identifier = yylval.gs->s;
387 }
388 | DECIMAL_CONSTANT
389 {
390 $$ = make_node(parser_ctx, NODE_EXPRESSION);
391 $$->u.expression.type = AST_EXP_CONSTANT;
392 sscanf(yylval.gs->s, "%" PRIu64,
393 &$$->u.expression.u.constant);
394 }
395 | OCTAL_CONSTANT
396 {
397 $$ = make_node(parser_ctx, NODE_EXPRESSION);
398 $$->u.expression.type = AST_EXP_CONSTANT;
399 sscanf(yylval.gs->s, "0%" PRIo64,
400 &$$->u.expression.u.constant);
401 }
402 | HEXADECIMAL_CONSTANT
403 {
404 $$ = make_node(parser_ctx, NODE_EXPRESSION);
405 $$->u.expression.type = AST_EXP_CONSTANT;
406 sscanf(yylval.gs->s, "0x%" PRIx64,
407 &$$->u.expression.u.constant);
408 }
e90d8561
MD
409 | FLOAT_CONSTANT
410 {
411 $$ = make_node(parser_ctx, NODE_EXPRESSION);
412 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
413 sscanf(yylval.gs->s, "%lg",
414 &$$->u.expression.u.float_constant);
415 }
953192ba
MD
416 | STRING_LITERAL_START DQUOTE
417 {
418 $$ = make_node(parser_ctx, NODE_EXPRESSION);
419 $$->u.expression.type = AST_EXP_STRING;
420 $$->u.expression.u.string = "";
421 }
422 | STRING_LITERAL_START s_char_sequence DQUOTE
423 {
424 $$ = make_node(parser_ctx, NODE_EXPRESSION);
425 $$->u.expression.type = AST_EXP_STRING;
426 $$->u.expression.u.string = $2->s;
427 }
428 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
429 {
430 $$ = make_node(parser_ctx, NODE_EXPRESSION);
431 $$->u.expression.type = AST_EXP_STRING;
432 $$->u.expression.u.string = $2->s;
433 }
434 | LPAREN expression RPAREN
435 {
436 $$ = make_node(parser_ctx, NODE_EXPRESSION);
437 $$->u.expression.type = AST_EXP_NESTED;
438 $$->u.expression.u.child = $2;
439 }
440 ;
441
442postfix_expression
443 : primary_expression
444 { $$ = $1; }
445 | postfix_expression DOT IDENTIFIER
446 {
447 $$ = make_node(parser_ctx, NODE_EXPRESSION);
448 $$->u.expression.type = AST_EXP_IDENTIFIER;
449 $$->u.expression.post_op = AST_LINK_DOT;
450 $$->u.expression.u.identifier = $3->s;
451 $$->u.expression.prev = $1;
452 }
453 | postfix_expression RARROW IDENTIFIER
454 {
455 $$ = make_node(parser_ctx, NODE_EXPRESSION);
456 $$->u.expression.type = AST_EXP_IDENTIFIER;
457 $$->u.expression.post_op = AST_LINK_RARROW;
458 $$->u.expression.u.identifier = $3->s;
459 $$->u.expression.prev = $1;
460 }
461 ;
462
463unary_expression
464 : postfix_expression
465 { $$ = $1; }
466 | unary_operator unary_expression
467 {
468 $$ = $1;
469 $$->u.unary_op.child = $2;
470 }
471 ;
472
473unary_operator
474 : PLUS
475 {
476 $$ = make_node(parser_ctx, NODE_UNARY_OP);
477 $$->u.unary_op.type = AST_UNARY_PLUS;
478 }
479 | MINUS
480 {
481 $$ = make_node(parser_ctx, NODE_UNARY_OP);
482 $$->u.unary_op.type = AST_UNARY_MINUS;
483 }
484 | NOT_OP
485 {
486 $$ = make_node(parser_ctx, NODE_UNARY_OP);
487 $$->u.unary_op.type = AST_UNARY_NOT;
488 }
ab78f161
CB
489 | NOT_BIN
490 {
491 $$ = make_node(parser_ctx, NODE_UNARY_OP);
492 $$->u.unary_op.type = AST_UNARY_BIN_NOT;
493 }
953192ba
MD
494 ;
495
496multiplicative_expression
497 : unary_expression
498 { $$ = $1; }
499 | multiplicative_expression STAR unary_expression
500 {
501 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
502 }
503 | multiplicative_expression DIV_OP unary_expression
504 {
505 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
506 }
507 | multiplicative_expression MOD_OP unary_expression
508 {
509 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
510 }
511 ;
512
513additive_expression
514 : multiplicative_expression
515 { $$ = $1; }
516 | additive_expression PLUS multiplicative_expression
517 {
518 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
519 }
520 | additive_expression MINUS multiplicative_expression
521 {
522 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
523 }
524 ;
525
526shift_expression
527 : additive_expression
528 { $$ = $1; }
529 | shift_expression LEFT_OP additive_expression
530 {
531 $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3);
532 }
533 | shift_expression RIGHT_OP additive_expression
534 {
535 $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3);
536 }
537 ;
538
539relational_expression
540 : shift_expression
541 { $$ = $1; }
542 | relational_expression LT_OP shift_expression
543 {
544 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
545 }
546 | relational_expression GT_OP shift_expression
547 {
548 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
549 }
550 | relational_expression LE_OP shift_expression
551 {
552 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
553 }
554 | relational_expression GE_OP shift_expression
555 {
556 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
557 }
558 ;
559
560equality_expression
561 : relational_expression
562 { $$ = $1; }
563 | equality_expression EQ_OP relational_expression
564 {
565 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
566 }
567 | equality_expression NE_OP relational_expression
568 {
569 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
570 }
571 ;
572
573and_expression
574 : equality_expression
575 { $$ = $1; }
576 | and_expression AND_BIN equality_expression
577 {
578 $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3);
579 }
580 ;
581
582exclusive_or_expression
583 : and_expression
584 { $$ = $1; }
585 | exclusive_or_expression XOR_BIN and_expression
586 {
587 $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3);
588 }
589 ;
590
591inclusive_or_expression
592 : exclusive_or_expression
593 { $$ = $1; }
594 | inclusive_or_expression OR_BIN exclusive_or_expression
595 {
596 $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3);
597 }
598 ;
599
600logical_and_expression
601 : inclusive_or_expression
602 { $$ = $1; }
603 | logical_and_expression AND_OP inclusive_or_expression
604 {
605 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
606 }
607 ;
608
609logical_or_expression
610 : logical_and_expression
611 { $$ = $1; }
612 | logical_or_expression OR_OP logical_and_expression
613 {
614 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
615 }
616 ;
617
618expression
619 : logical_or_expression
620 { $$ = $1; }
621 ;
622
623translation_unit
624 : expression
625 {
626 parser_ctx->ast->root.u.root.child = $1;
627 }
628 ;
This page took 0.0803 seconds and 5 git commands to generate.