Move to kernel style SPDX license identifiers
[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 *
ab5be9fa 7 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
953192ba 8 *
ab5be9fa 9 * SPDX-License-Identifier: LGPL-2.1-only
953192ba
MD
10 *
11 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
12 */
13
14#include <stdio.h>
15#include <unistd.h>
16#include <string.h>
17#include <stdlib.h>
18#include <assert.h>
19#include <errno.h>
20#include <inttypes.h>
953192ba 21#include "filter-ast.h"
95b9bd90 22#include "filter-parser.h"
953192ba 23
a187da1a
DG
24#include <common/macros.h>
25
8ab7c0d9
MD
26#define WIDTH_u64_SCANF_IS_A_BROKEN_API "20"
27#define WIDTH_o64_SCANF_IS_A_BROKEN_API "22"
28#define WIDTH_x64_SCANF_IS_A_BROKEN_API "17"
29#define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */
30
a187da1a 31LTTNG_HIDDEN
953192ba 32int yydebug;
a187da1a 33LTTNG_HIDDEN
953192ba
MD
34int filter_parser_debug = 0;
35
a187da1a 36LTTNG_HIDDEN
9039edd4 37int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
a187da1a 38LTTNG_HIDDEN
9039edd4 39int yylex(union YYSTYPE *yyval, yyscan_t scanner);
a187da1a 40LTTNG_HIDDEN
953192ba 41int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
a187da1a 42LTTNG_HIDDEN
953192ba 43int yylex_destroy(yyscan_t yyparser_ctx);
a187da1a 44LTTNG_HIDDEN
953192ba
MD
45void yyrestart(FILE * in_str, yyscan_t parser_ctx);
46
47struct gc_string {
48 struct cds_list_head gc;
49 size_t alloclen;
50 char s[];
51};
52
53static const char *node_type_to_str[] = {
54 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
55 [ NODE_ROOT ] = "NODE_ROOT",
56 [ NODE_EXPRESSION ] = "NODE_EXPRESSION",
57 [ NODE_OP ] = "NODE_OP",
58 [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
59};
60
a187da1a 61LTTNG_HIDDEN
953192ba
MD
62const char *node_type(struct filter_node *node)
63{
64 if (node->type < NR_NODE_TYPES)
65 return node_type_to_str[node->type];
66 else
67 return NULL;
68}
69
70static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
71 size_t len)
72{
73 struct gc_string *gstr;
74 size_t alloclen;
75
76 /* TODO: could be faster with find first bit or glib Gstring */
77 /* sizeof long to account for malloc header (int or long ?) */
78 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
79 alloclen *= 2);
80
be61ee34
MD
81 gstr = zmalloc(alloclen);
82 if (!gstr) {
83 goto end;
84 }
953192ba
MD
85 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
86 gstr->alloclen = alloclen;
be61ee34 87end:
953192ba
MD
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 */
a187da1a 96LTTNG_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
a187da1a 126LTTNG_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
be61ee34 139 node = zmalloc(sizeof(*node));
953192ba
MD
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
be61ee34 176 node = zmalloc(sizeof(*node));
953192ba
MD
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
a187da1a 188LTTNG_HIDDEN
9039edd4 189void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
953192ba
MD
190{
191 fprintf(stderr, "error %s\n", str);
192}
193
a187da1a 194LTTNG_HIDDEN
953192ba
MD
195int yywrap(void)
196{
197 return 1;
198}
199
200#define parse_error(parser_ctx, str) \
201do { \
9039edd4 202 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
953192ba
MD
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
be61ee34 218 ast = zmalloc(sizeof(*ast));
953192ba
MD
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
a187da1a 236LTTNG_HIDDEN
953192ba
MD
237int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
238{
9039edd4 239 return yyparse(parser_ctx, parser_ctx->scanner);
953192ba
MD
240}
241
a187da1a 242LTTNG_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
be61ee34 250 parser_ctx = zmalloc(sizeof(*parser_ctx));
953192ba
MD
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
a187da1a 284LTTNG_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}
9039edd4
ZT
302%parse-param {yyscan_t scanner}
303%lex-param {yyscan_t scanner}
953192ba
MD
304%start translation_unit
305%token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
306%token ESCSEQ CHAR_STRING_TOKEN
e90d8561 307%token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
953192ba
MD
308%token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
309%token STAR PLUS MINUS
310%token MOD_OP DIV_OP RIGHT_OP LEFT_OP
311%token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
312%token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
313%token XOR_BIN AND_BIN OR_BIN NOT_BIN
314
586dc72f 315%token <gs> IDENTIFIER GLOBAL_IDENTIFIER
953192ba
MD
316%token ERROR
317%union
318{
319 long long ll;
320 char c;
321 struct gc_string *gs;
322 struct filter_node *n;
323}
324
325%type <gs> s_char s_char_sequence c_char c_char_sequence
326
327%type <n> primary_expression
bff988fa
MD
328%type <n> prefix_expression
329%type <n> prefix_expression_rec
953192ba
MD
330%type <n> postfix_expression
331%type <n> unary_expression
332%type <n> unary_operator
333%type <n> multiplicative_expression
334%type <n> additive_expression
335%type <n> shift_expression
336%type <n> relational_expression
337%type <n> equality_expression
338%type <n> and_expression
339%type <n> exclusive_or_expression
340%type <n> inclusive_or_expression
341%type <n> logical_and_expression
342%type <n> logical_or_expression
343%type <n> expression
bff988fa 344%type <n> identifiers
953192ba
MD
345
346%%
347
348
349/* 1.5 Constants */
350
351c_char_sequence:
352 c_char
353 { $$ = $1; }
354 | c_char_sequence c_char
355 { $$ = gc_string_append(parser_ctx, $1, $2); }
356 ;
357
358c_char:
359 CHAR_STRING_TOKEN
360 { $$ = yylval.gs; }
361 | ESCSEQ
362 {
363 parse_error(parser_ctx, "escape sequences not supported yet");
364 }
365 ;
366
367/* 1.6 String literals */
368
369s_char_sequence:
370 s_char
371 { $$ = $1; }
372 | s_char_sequence s_char
373 { $$ = gc_string_append(parser_ctx, $1, $2); }
374 ;
375
376s_char:
377 CHAR_STRING_TOKEN
378 { $$ = yylval.gs; }
379 | ESCSEQ
380 {
381 parse_error(parser_ctx, "escape sequences not supported yet");
382 }
383 ;
384
bff988fa
MD
385primary_expression:
386 DECIMAL_CONSTANT
953192ba
MD
387 {
388 $$ = make_node(parser_ctx, NODE_EXPRESSION);
389 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
390 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
391 &$$->u.expression.u.constant) != 1) {
392 parse_error(parser_ctx, "cannot scanf decimal constant");
393 }
953192ba
MD
394 }
395 | OCTAL_CONSTANT
396 {
397 $$ = make_node(parser_ctx, NODE_EXPRESSION);
398 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
399 if (!strcmp(yylval.gs->s, "0")) {
400 $$->u.expression.u.constant = 0;
401 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
402 &$$->u.expression.u.constant) != 1) {
403 parse_error(parser_ctx, "cannot scanf octal constant");
404 }
953192ba
MD
405 }
406 | HEXADECIMAL_CONSTANT
407 {
408 $$ = make_node(parser_ctx, NODE_EXPRESSION);
409 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
410 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
411 &$$->u.expression.u.constant) != 1) {
412 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
413 }
953192ba 414 }
e90d8561
MD
415 | FLOAT_CONSTANT
416 {
417 $$ = make_node(parser_ctx, NODE_EXPRESSION);
418 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
8ab7c0d9
MD
419 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
420 &$$->u.expression.u.float_constant) != 1) {
421 parse_error(parser_ctx, "cannot scanf float constant");
422 }
e90d8561 423 }
953192ba
MD
424 | STRING_LITERAL_START DQUOTE
425 {
426 $$ = make_node(parser_ctx, NODE_EXPRESSION);
427 $$->u.expression.type = AST_EXP_STRING;
428 $$->u.expression.u.string = "";
429 }
430 | STRING_LITERAL_START s_char_sequence DQUOTE
431 {
432 $$ = make_node(parser_ctx, NODE_EXPRESSION);
433 $$->u.expression.type = AST_EXP_STRING;
434 $$->u.expression.u.string = $2->s;
435 }
436 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
437 {
438 $$ = make_node(parser_ctx, NODE_EXPRESSION);
439 $$->u.expression.type = AST_EXP_STRING;
440 $$->u.expression.u.string = $2->s;
441 }
442 | LPAREN expression RPAREN
443 {
444 $$ = make_node(parser_ctx, NODE_EXPRESSION);
445 $$->u.expression.type = AST_EXP_NESTED;
446 $$->u.expression.u.child = $2;
447 }
448 ;
449
bff988fa
MD
450identifiers
451 : IDENTIFIER
452 {
453 $$ = make_node(parser_ctx, NODE_EXPRESSION);
454 $$->u.expression.type = AST_EXP_IDENTIFIER;
455 $$->u.expression.u.identifier = yylval.gs->s;
456 }
457 | GLOBAL_IDENTIFIER
458 {
459 $$ = make_node(parser_ctx, NODE_EXPRESSION);
460 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
461 $$->u.expression.u.identifier = yylval.gs->s;
462 }
463 ;
464
465prefix_expression_rec
466 : LSBRAC unary_expression RSBRAC
467 {
468 $$ = $2;
469 }
470 | LSBRAC unary_expression RSBRAC prefix_expression_rec
471 {
472 $$ = $2;
473 $$->u.expression.pre_op = AST_LINK_BRACKET;
474 $$->u.expression.prev = $4;
475 }
476 ;
477
478prefix_expression
479 : identifiers
480 {
481 $$ = $1;
482 }
483 | identifiers prefix_expression_rec
661dfdd1
MD
484 {
485 $$ = $1;
486 $$->u.expression.pre_op = AST_LINK_BRACKET;
bff988fa 487 $$->u.expression.next_bracket = $2;
661dfdd1 488 }
bff988fa
MD
489 ;
490
491postfix_expression
492 : prefix_expression
953192ba 493 {
bff988fa
MD
494 $$ = $1;
495 }
496 | postfix_expression DOT prefix_expression
497 {
498 $$ = $3;
953192ba 499 $$->u.expression.post_op = AST_LINK_DOT;
953192ba
MD
500 $$->u.expression.prev = $1;
501 }
bff988fa 502 | postfix_expression RARROW prefix_expression
953192ba 503 {
bff988fa 504 $$ = $3;
953192ba 505 $$->u.expression.post_op = AST_LINK_RARROW;
953192ba
MD
506 $$->u.expression.prev = $1;
507 }
508 ;
509
510unary_expression
511 : postfix_expression
512 { $$ = $1; }
bff988fa
MD
513 | primary_expression
514 { $$ = $1; }
953192ba
MD
515 | unary_operator unary_expression
516 {
517 $$ = $1;
518 $$->u.unary_op.child = $2;
519 }
520 ;
521
522unary_operator
523 : PLUS
524 {
525 $$ = make_node(parser_ctx, NODE_UNARY_OP);
526 $$->u.unary_op.type = AST_UNARY_PLUS;
527 }
528 | MINUS
529 {
530 $$ = make_node(parser_ctx, NODE_UNARY_OP);
531 $$->u.unary_op.type = AST_UNARY_MINUS;
532 }
533 | NOT_OP
534 {
535 $$ = make_node(parser_ctx, NODE_UNARY_OP);
536 $$->u.unary_op.type = AST_UNARY_NOT;
537 }
ab78f161
CB
538 | NOT_BIN
539 {
540 $$ = make_node(parser_ctx, NODE_UNARY_OP);
bff988fa 541 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
ab78f161 542 }
953192ba
MD
543 ;
544
545multiplicative_expression
546 : unary_expression
547 { $$ = $1; }
548 | multiplicative_expression STAR unary_expression
549 {
550 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
551 }
552 | multiplicative_expression DIV_OP unary_expression
553 {
554 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
555 }
556 | multiplicative_expression MOD_OP unary_expression
557 {
558 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
559 }
560 ;
561
562additive_expression
563 : multiplicative_expression
564 { $$ = $1; }
565 | additive_expression PLUS multiplicative_expression
566 {
567 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
568 }
569 | additive_expression MINUS multiplicative_expression
570 {
571 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
572 }
573 ;
574
575shift_expression
576 : additive_expression
577 { $$ = $1; }
578 | shift_expression LEFT_OP additive_expression
579 {
116d3c01 580 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
953192ba
MD
581 }
582 | shift_expression RIGHT_OP additive_expression
583 {
116d3c01 584 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
953192ba
MD
585 }
586 ;
587
831b702b 588and_expression
953192ba
MD
589 : shift_expression
590 { $$ = $1; }
831b702b
MD
591 | and_expression AND_BIN shift_expression
592 {
593 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
594 }
595 ;
596
597exclusive_or_expression
598 : and_expression
599 { $$ = $1; }
600 | exclusive_or_expression XOR_BIN and_expression
601 {
602 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
603 }
604 ;
605
606inclusive_or_expression
607 : exclusive_or_expression
608 { $$ = $1; }
609 | inclusive_or_expression OR_BIN exclusive_or_expression
610 {
611 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
612 }
613 ;
614
615relational_expression
616 : inclusive_or_expression
617 { $$ = $1; }
618 | relational_expression LT_OP inclusive_or_expression
953192ba
MD
619 {
620 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
621 }
831b702b 622 | relational_expression GT_OP inclusive_or_expression
953192ba
MD
623 {
624 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
625 }
831b702b 626 | relational_expression LE_OP inclusive_or_expression
953192ba
MD
627 {
628 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
629 }
831b702b 630 | relational_expression GE_OP inclusive_or_expression
953192ba
MD
631 {
632 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
633 }
634 ;
635
636equality_expression
637 : relational_expression
638 { $$ = $1; }
639 | equality_expression EQ_OP relational_expression
640 {
641 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
642 }
643 | equality_expression NE_OP relational_expression
644 {
645 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
646 }
647 ;
648
953192ba 649logical_and_expression
831b702b 650 : equality_expression
953192ba 651 { $$ = $1; }
831b702b 652 | logical_and_expression AND_OP equality_expression
953192ba
MD
653 {
654 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
655 }
656 ;
657
658logical_or_expression
659 : logical_and_expression
660 { $$ = $1; }
661 | logical_or_expression OR_OP logical_and_expression
662 {
663 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
664 }
665 ;
666
667expression
668 : logical_or_expression
669 { $$ = $1; }
670 ;
671
672translation_unit
673 : expression
674 {
675 parser_ctx->ast->root.u.root.child = $1;
676 }
677 ;
This page took 0.090896 seconds and 5 git commands to generate.