Commit | Line | Data |
---|---|---|
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> | |
953192ba | 18 | #include <inttypes.h> |
0ae3cfc6 | 19 | #include "common/bytecode/bytecode.h" |
953192ba | 20 | #include "filter-ast.h" |
95b9bd90 | 21 | #include "filter-parser.h" |
e4d2f27a | 22 | #include "memstream.h" |
953192ba | 23 | |
edf4b93e | 24 | #include <common/compat/errno.h> |
a187da1a DG |
25 | #include <common/macros.h> |
26 | ||
8ab7c0d9 MD |
27 | #define WIDTH_u64_SCANF_IS_A_BROKEN_API "20" |
28 | #define WIDTH_o64_SCANF_IS_A_BROKEN_API "22" | |
29 | #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17" | |
30 | #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */ | |
31 | ||
e4d2f27a JR |
32 | #ifdef DEBUG |
33 | static const int print_xml = 1; | |
34 | #define dbg_printf(fmt, args...) \ | |
35 | printf("[debug filter_parser] " fmt, ## args) | |
36 | #else | |
37 | static const int print_xml = 0; | |
38 | #define dbg_printf(fmt, args...) \ | |
39 | do { \ | |
40 | /* do nothing but check printf format */ \ | |
41 | if (0) \ | |
42 | printf("[debug filter_parser] " fmt, ## args); \ | |
43 | } while (0) | |
44 | #endif | |
45 | ||
a187da1a | 46 | LTTNG_HIDDEN |
953192ba | 47 | int yydebug; |
a187da1a | 48 | LTTNG_HIDDEN |
953192ba MD |
49 | int filter_parser_debug = 0; |
50 | ||
a187da1a | 51 | LTTNG_HIDDEN |
9039edd4 | 52 | int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner); |
a187da1a | 53 | LTTNG_HIDDEN |
9039edd4 | 54 | int yylex(union YYSTYPE *yyval, yyscan_t scanner); |
a187da1a | 55 | LTTNG_HIDDEN |
953192ba | 56 | int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals); |
a187da1a | 57 | LTTNG_HIDDEN |
953192ba | 58 | int yylex_destroy(yyscan_t yyparser_ctx); |
a187da1a | 59 | LTTNG_HIDDEN |
953192ba MD |
60 | void yyrestart(FILE * in_str, yyscan_t parser_ctx); |
61 | ||
62 | struct gc_string { | |
63 | struct cds_list_head gc; | |
64 | size_t alloclen; | |
65 | char s[]; | |
66 | }; | |
67 | ||
68 | static const char *node_type_to_str[] = { | |
69 | [ NODE_UNKNOWN ] = "NODE_UNKNOWN", | |
70 | [ NODE_ROOT ] = "NODE_ROOT", | |
71 | [ NODE_EXPRESSION ] = "NODE_EXPRESSION", | |
72 | [ NODE_OP ] = "NODE_OP", | |
73 | [ NODE_UNARY_OP ] = "NODE_UNARY_OP", | |
74 | }; | |
75 | ||
a187da1a | 76 | LTTNG_HIDDEN |
953192ba MD |
77 | const char *node_type(struct filter_node *node) |
78 | { | |
79 | if (node->type < NR_NODE_TYPES) | |
80 | return node_type_to_str[node->type]; | |
81 | else | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, | |
86 | size_t len) | |
87 | { | |
88 | struct gc_string *gstr; | |
89 | size_t alloclen; | |
90 | ||
91 | /* TODO: could be faster with find first bit or glib Gstring */ | |
92 | /* sizeof long to account for malloc header (int or long ?) */ | |
93 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len; | |
94 | alloclen *= 2); | |
95 | ||
be61ee34 MD |
96 | gstr = zmalloc(alloclen); |
97 | if (!gstr) { | |
98 | goto end; | |
99 | } | |
953192ba MD |
100 | cds_list_add(&gstr->gc, &parser_ctx->allocated_strings); |
101 | gstr->alloclen = alloclen; | |
be61ee34 | 102 | end: |
953192ba MD |
103 | return gstr; |
104 | } | |
105 | ||
106 | /* | |
107 | * note: never use gc_string_append on a string that has external references. | |
108 | * gsrc will be garbage collected immediately, and gstr might be. | |
109 | * Should only be used to append characters to a string literal or constant. | |
110 | */ | |
52dc69b2 | 111 | static |
953192ba MD |
112 | struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx, |
113 | struct gc_string *gstr, | |
114 | struct gc_string *gsrc) | |
115 | { | |
116 | size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1; | |
117 | size_t alloclen; | |
118 | ||
119 | /* TODO: could be faster with find first bit or glib Gstring */ | |
120 | /* sizeof long to account for malloc header (int or long ?) */ | |
121 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen; | |
122 | alloclen *= 2); | |
123 | ||
124 | if (alloclen > gstr->alloclen) { | |
125 | struct gc_string *newgstr; | |
126 | ||
127 | newgstr = gc_string_alloc(parser_ctx, newlen); | |
128 | strcpy(newgstr->s, gstr->s); | |
129 | strcat(newgstr->s, gsrc->s); | |
130 | cds_list_del(&gstr->gc); | |
131 | free(gstr); | |
132 | gstr = newgstr; | |
133 | } else { | |
134 | strcat(gstr->s, gsrc->s); | |
135 | } | |
136 | cds_list_del(&gsrc->gc); | |
137 | free(gsrc); | |
138 | return gstr; | |
139 | } | |
140 | ||
a187da1a | 141 | LTTNG_HIDDEN |
953192ba MD |
142 | void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src) |
143 | { | |
144 | lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1); | |
145 | strcpy(lvalp->gs->s, src); | |
146 | } | |
147 | ||
148 | static struct filter_node *make_node(struct filter_parser_ctx *scanner, | |
149 | enum node_type type) | |
150 | { | |
151 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
152 | struct filter_node *node; | |
153 | ||
be61ee34 | 154 | node = zmalloc(sizeof(*node)); |
953192ba MD |
155 | if (!node) |
156 | return NULL; | |
157 | memset(node, 0, sizeof(*node)); | |
158 | node->type = type; | |
159 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
160 | ||
161 | switch (type) { | |
162 | case NODE_ROOT: | |
163 | fprintf(stderr, "[error] %s: trying to create root node\n", __func__); | |
164 | break; | |
165 | ||
166 | case NODE_EXPRESSION: | |
167 | break; | |
168 | case NODE_OP: | |
169 | break; | |
170 | case NODE_UNARY_OP: | |
171 | break; | |
172 | ||
173 | case NODE_UNKNOWN: | |
174 | default: | |
175 | fprintf(stderr, "[error] %s: unknown node type %d\n", __func__, | |
176 | (int) type); | |
177 | break; | |
178 | } | |
179 | ||
180 | return node; | |
181 | } | |
182 | ||
183 | static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, | |
184 | enum op_type type, | |
185 | struct filter_node *lchild, | |
186 | struct filter_node *rchild) | |
187 | { | |
188 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
189 | struct filter_node *node; | |
190 | ||
be61ee34 | 191 | node = zmalloc(sizeof(*node)); |
953192ba MD |
192 | if (!node) |
193 | return NULL; | |
194 | memset(node, 0, sizeof(*node)); | |
195 | node->type = NODE_OP; | |
196 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
197 | node->u.op.type = type; | |
198 | node->u.op.lchild = lchild; | |
199 | node->u.op.rchild = rchild; | |
200 | return node; | |
201 | } | |
202 | ||
52dc69b2 | 203 | static |
9039edd4 | 204 | void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str) |
953192ba MD |
205 | { |
206 | fprintf(stderr, "error %s\n", str); | |
207 | } | |
953192ba MD |
208 | |
209 | #define parse_error(parser_ctx, str) \ | |
210 | do { \ | |
9039edd4 | 211 | yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \ |
953192ba MD |
212 | YYERROR; \ |
213 | } while (0) | |
214 | ||
215 | static void free_strings(struct cds_list_head *list) | |
216 | { | |
217 | struct gc_string *gstr, *tmp; | |
218 | ||
219 | cds_list_for_each_entry_safe(gstr, tmp, list, gc) | |
220 | free(gstr); | |
221 | } | |
222 | ||
223 | static struct filter_ast *filter_ast_alloc(void) | |
224 | { | |
225 | struct filter_ast *ast; | |
226 | ||
be61ee34 | 227 | ast = zmalloc(sizeof(*ast)); |
953192ba MD |
228 | if (!ast) |
229 | return NULL; | |
230 | memset(ast, 0, sizeof(*ast)); | |
231 | CDS_INIT_LIST_HEAD(&ast->allocated_nodes); | |
232 | ast->root.type = NODE_ROOT; | |
233 | return ast; | |
234 | } | |
235 | ||
236 | static void filter_ast_free(struct filter_ast *ast) | |
237 | { | |
238 | struct filter_node *node, *tmp; | |
239 | ||
240 | cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc) | |
241 | free(node); | |
37600d79 | 242 | free(ast); |
953192ba MD |
243 | } |
244 | ||
a187da1a | 245 | LTTNG_HIDDEN |
953192ba MD |
246 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx) |
247 | { | |
9039edd4 | 248 | return yyparse(parser_ctx, parser_ctx->scanner); |
953192ba MD |
249 | } |
250 | ||
a187da1a | 251 | LTTNG_HIDDEN |
953192ba MD |
252 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input) |
253 | { | |
254 | struct filter_parser_ctx *parser_ctx; | |
255 | int ret; | |
256 | ||
257 | yydebug = filter_parser_debug; | |
258 | ||
be61ee34 | 259 | parser_ctx = zmalloc(sizeof(*parser_ctx)); |
953192ba MD |
260 | if (!parser_ctx) |
261 | return NULL; | |
262 | memset(parser_ctx, 0, sizeof(*parser_ctx)); | |
263 | ||
264 | ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner); | |
265 | if (ret) { | |
266 | fprintf(stderr, "yylex_init error\n"); | |
267 | goto cleanup_parser_ctx; | |
268 | } | |
269 | /* Start processing new stream */ | |
270 | yyrestart(input, parser_ctx->scanner); | |
271 | ||
272 | parser_ctx->ast = filter_ast_alloc(); | |
273 | if (!parser_ctx->ast) | |
274 | goto cleanup_lexer; | |
275 | CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings); | |
276 | ||
277 | if (yydebug) | |
278 | fprintf(stdout, "parser_ctx input is a%s.\n", | |
279 | isatty(fileno(input)) ? "n interactive tty" : | |
280 | " noninteractive file"); | |
281 | ||
282 | return parser_ctx; | |
283 | ||
284 | cleanup_lexer: | |
285 | ret = yylex_destroy(parser_ctx->scanner); | |
286 | if (!ret) | |
287 | fprintf(stderr, "yylex_destroy error\n"); | |
288 | cleanup_parser_ctx: | |
289 | free(parser_ctx); | |
290 | return NULL; | |
291 | } | |
292 | ||
a187da1a | 293 | LTTNG_HIDDEN |
953192ba MD |
294 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) |
295 | { | |
296 | int ret; | |
297 | ||
953192ba MD |
298 | ret = yylex_destroy(parser_ctx->scanner); |
299 | if (ret) | |
300 | fprintf(stderr, "yylex_destroy error\n"); | |
5a843717 SM |
301 | |
302 | filter_ast_free(parser_ctx->ast); | |
303 | free_strings(&parser_ctx->allocated_strings); | |
304 | filter_ir_free(parser_ctx); | |
305 | free(parser_ctx->bytecode); | |
306 | free(parser_ctx->bytecode_reloc); | |
307 | ||
953192ba MD |
308 | free(parser_ctx); |
309 | } | |
310 | ||
e4d2f27a JR |
311 | LTTNG_HIDDEN |
312 | int filter_parser_ctx_create_from_filter_expression( | |
313 | const char *filter_expression, struct filter_parser_ctx **ctxp) | |
314 | { | |
315 | int ret; | |
316 | struct filter_parser_ctx *ctx = NULL; | |
317 | FILE *fmem = NULL; | |
318 | ||
a0377dfe FD |
319 | LTTNG_ASSERT(filter_expression); |
320 | LTTNG_ASSERT(ctxp); | |
e4d2f27a JR |
321 | |
322 | /* | |
323 | * Casting const to non-const, as the underlying function will use it in | |
324 | * read-only mode. | |
325 | */ | |
326 | fmem = lttng_fmemopen((void *) filter_expression, | |
327 | strlen(filter_expression), "r"); | |
328 | if (!fmem) { | |
329 | fprintf(stderr, "Error opening memory as stream\n"); | |
330 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
331 | goto error; | |
332 | } | |
333 | ctx = filter_parser_ctx_alloc(fmem); | |
334 | if (!ctx) { | |
335 | fprintf(stderr, "Error allocating parser\n"); | |
336 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
337 | goto filter_alloc_error; | |
338 | } | |
339 | ret = filter_parser_ctx_append_ast(ctx); | |
340 | if (ret) { | |
341 | fprintf(stderr, "Parse error\n"); | |
342 | ret = -LTTNG_ERR_FILTER_INVAL; | |
343 | goto parse_error; | |
344 | } | |
345 | if (print_xml) { | |
346 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
347 | if (ret) { | |
348 | fflush(stdout); | |
349 | fprintf(stderr, "XML print error\n"); | |
350 | ret = -LTTNG_ERR_FILTER_INVAL; | |
351 | goto parse_error; | |
352 | } | |
353 | } | |
354 | ||
355 | dbg_printf("Generating IR... "); | |
356 | fflush(stdout); | |
357 | ret = filter_visitor_ir_generate(ctx); | |
358 | if (ret) { | |
359 | fprintf(stderr, "Generate IR error\n"); | |
360 | ret = -LTTNG_ERR_FILTER_INVAL; | |
361 | goto parse_error; | |
362 | } | |
363 | dbg_printf("done\n"); | |
364 | ||
365 | dbg_printf("Validating IR... "); | |
366 | fflush(stdout); | |
367 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
368 | if (ret) { | |
369 | ret = -LTTNG_ERR_FILTER_INVAL; | |
370 | goto parse_error; | |
371 | } | |
372 | ||
373 | /* Normalize globbing patterns in the expression. */ | |
374 | ret = filter_visitor_ir_normalize_glob_patterns(ctx); | |
375 | if (ret) { | |
376 | ret = -LTTNG_ERR_FILTER_INVAL; | |
377 | goto parse_error; | |
378 | } | |
379 | ||
380 | /* Validate strings used as literals in the expression. */ | |
381 | ret = filter_visitor_ir_validate_string(ctx); | |
382 | if (ret) { | |
383 | ret = -LTTNG_ERR_FILTER_INVAL; | |
384 | goto parse_error; | |
385 | } | |
386 | ||
387 | /* Validate globbing patterns in the expression. */ | |
388 | ret = filter_visitor_ir_validate_globbing(ctx); | |
389 | if (ret) { | |
390 | ret = -LTTNG_ERR_FILTER_INVAL; | |
391 | goto parse_error; | |
392 | } | |
393 | ||
394 | dbg_printf("done\n"); | |
395 | ||
396 | dbg_printf("Generating bytecode... "); | |
397 | fflush(stdout); | |
398 | ret = filter_visitor_bytecode_generate(ctx); | |
399 | if (ret) { | |
400 | fprintf(stderr, "Generate bytecode error\n"); | |
401 | ret = -LTTNG_ERR_FILTER_INVAL; | |
402 | goto parse_error; | |
403 | } | |
404 | dbg_printf("done\n"); | |
405 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
406 | bytecode_get_len(&ctx->bytecode->b)); | |
407 | ||
408 | /* No need to keep the memory stream. */ | |
409 | if (fclose(fmem) != 0) { | |
410 | fprintf(stderr, "fclose (%d) \n", errno); | |
411 | ret = -LTTNG_ERR_FILTER_INVAL; | |
412 | } | |
413 | ||
414 | *ctxp = ctx; | |
415 | return 0; | |
416 | ||
417 | parse_error: | |
418 | filter_ir_free(ctx); | |
419 | filter_parser_ctx_free(ctx); | |
420 | filter_alloc_error: | |
421 | if (fclose(fmem) != 0) { | |
422 | fprintf(stderr, "fclose (%d) \n", errno); | |
423 | } | |
424 | error: | |
425 | return ret; | |
426 | } | |
427 | ||
953192ba MD |
428 | %} |
429 | ||
52dc69b2 SM |
430 | %code provides |
431 | { | |
432 | #include "common/macros.h" | |
433 | ||
434 | LTTNG_HIDDEN | |
435 | void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src); | |
436 | } | |
437 | ||
953192ba MD |
438 | %define api.pure |
439 | /* %locations */ | |
440 | %parse-param {struct filter_parser_ctx *parser_ctx} | |
9039edd4 ZT |
441 | %parse-param {yyscan_t scanner} |
442 | %lex-param {yyscan_t scanner} | |
953192ba MD |
443 | %start translation_unit |
444 | %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE | |
445 | %token ESCSEQ CHAR_STRING_TOKEN | |
e90d8561 | 446 | %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT |
953192ba MD |
447 | %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW |
448 | %token STAR PLUS MINUS | |
449 | %token MOD_OP DIV_OP RIGHT_OP LEFT_OP | |
450 | %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP | |
451 | %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA | |
452 | %token XOR_BIN AND_BIN OR_BIN NOT_BIN | |
453 | ||
586dc72f | 454 | %token <gs> IDENTIFIER GLOBAL_IDENTIFIER |
953192ba MD |
455 | %token ERROR |
456 | %union | |
457 | { | |
458 | long long ll; | |
459 | char c; | |
460 | struct gc_string *gs; | |
461 | struct filter_node *n; | |
462 | } | |
463 | ||
464 | %type <gs> s_char s_char_sequence c_char c_char_sequence | |
465 | ||
466 | %type <n> primary_expression | |
bff988fa MD |
467 | %type <n> prefix_expression |
468 | %type <n> prefix_expression_rec | |
953192ba MD |
469 | %type <n> postfix_expression |
470 | %type <n> unary_expression | |
471 | %type <n> unary_operator | |
472 | %type <n> multiplicative_expression | |
473 | %type <n> additive_expression | |
474 | %type <n> shift_expression | |
475 | %type <n> relational_expression | |
476 | %type <n> equality_expression | |
477 | %type <n> and_expression | |
478 | %type <n> exclusive_or_expression | |
479 | %type <n> inclusive_or_expression | |
480 | %type <n> logical_and_expression | |
481 | %type <n> logical_or_expression | |
482 | %type <n> expression | |
bff988fa | 483 | %type <n> identifiers |
953192ba MD |
484 | |
485 | %% | |
486 | ||
487 | ||
488 | /* 1.5 Constants */ | |
489 | ||
490 | c_char_sequence: | |
491 | c_char | |
492 | { $$ = $1; } | |
493 | | c_char_sequence c_char | |
494 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
495 | ; | |
496 | ||
497 | c_char: | |
498 | CHAR_STRING_TOKEN | |
499 | { $$ = yylval.gs; } | |
500 | | ESCSEQ | |
501 | { | |
502 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
503 | } | |
504 | ; | |
505 | ||
506 | /* 1.6 String literals */ | |
507 | ||
508 | s_char_sequence: | |
509 | s_char | |
510 | { $$ = $1; } | |
511 | | s_char_sequence s_char | |
512 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
513 | ; | |
514 | ||
515 | s_char: | |
516 | CHAR_STRING_TOKEN | |
517 | { $$ = yylval.gs; } | |
518 | | ESCSEQ | |
519 | { | |
520 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
521 | } | |
522 | ; | |
523 | ||
bff988fa MD |
524 | primary_expression: |
525 | DECIMAL_CONSTANT | |
953192ba MD |
526 | { |
527 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
528 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
529 | if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64, |
530 | &$$->u.expression.u.constant) != 1) { | |
531 | parse_error(parser_ctx, "cannot scanf decimal constant"); | |
532 | } | |
953192ba MD |
533 | } |
534 | | OCTAL_CONSTANT | |
535 | { | |
536 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
537 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
538 | if (!strcmp(yylval.gs->s, "0")) { |
539 | $$->u.expression.u.constant = 0; | |
540 | } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64, | |
541 | &$$->u.expression.u.constant) != 1) { | |
542 | parse_error(parser_ctx, "cannot scanf octal constant"); | |
543 | } | |
953192ba MD |
544 | } |
545 | | HEXADECIMAL_CONSTANT | |
546 | { | |
547 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
548 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
549 | if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64, |
550 | &$$->u.expression.u.constant) != 1) { | |
551 | parse_error(parser_ctx, "cannot scanf hexadecimal constant"); | |
552 | } | |
953192ba | 553 | } |
e90d8561 MD |
554 | | FLOAT_CONSTANT |
555 | { | |
556 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
557 | $$->u.expression.type = AST_EXP_FLOAT_CONSTANT; | |
8ab7c0d9 MD |
558 | if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg", |
559 | &$$->u.expression.u.float_constant) != 1) { | |
560 | parse_error(parser_ctx, "cannot scanf float constant"); | |
561 | } | |
e90d8561 | 562 | } |
953192ba MD |
563 | | STRING_LITERAL_START DQUOTE |
564 | { | |
565 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
566 | $$->u.expression.type = AST_EXP_STRING; | |
567 | $$->u.expression.u.string = ""; | |
568 | } | |
569 | | STRING_LITERAL_START s_char_sequence DQUOTE | |
570 | { | |
571 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
572 | $$->u.expression.type = AST_EXP_STRING; | |
573 | $$->u.expression.u.string = $2->s; | |
574 | } | |
575 | | CHARACTER_CONSTANT_START c_char_sequence SQUOTE | |
576 | { | |
577 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
578 | $$->u.expression.type = AST_EXP_STRING; | |
579 | $$->u.expression.u.string = $2->s; | |
580 | } | |
581 | | LPAREN expression RPAREN | |
582 | { | |
583 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
584 | $$->u.expression.type = AST_EXP_NESTED; | |
585 | $$->u.expression.u.child = $2; | |
586 | } | |
587 | ; | |
588 | ||
bff988fa MD |
589 | identifiers |
590 | : IDENTIFIER | |
591 | { | |
592 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
593 | $$->u.expression.type = AST_EXP_IDENTIFIER; | |
594 | $$->u.expression.u.identifier = yylval.gs->s; | |
595 | } | |
596 | | GLOBAL_IDENTIFIER | |
597 | { | |
598 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
599 | $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER; | |
600 | $$->u.expression.u.identifier = yylval.gs->s; | |
601 | } | |
602 | ; | |
603 | ||
604 | prefix_expression_rec | |
605 | : LSBRAC unary_expression RSBRAC | |
606 | { | |
607 | $$ = $2; | |
608 | } | |
609 | | LSBRAC unary_expression RSBRAC prefix_expression_rec | |
610 | { | |
611 | $$ = $2; | |
612 | $$->u.expression.pre_op = AST_LINK_BRACKET; | |
613 | $$->u.expression.prev = $4; | |
614 | } | |
615 | ; | |
616 | ||
617 | prefix_expression | |
618 | : identifiers | |
619 | { | |
620 | $$ = $1; | |
621 | } | |
622 | | identifiers prefix_expression_rec | |
661dfdd1 MD |
623 | { |
624 | $$ = $1; | |
625 | $$->u.expression.pre_op = AST_LINK_BRACKET; | |
bff988fa | 626 | $$->u.expression.next_bracket = $2; |
661dfdd1 | 627 | } |
bff988fa MD |
628 | ; |
629 | ||
630 | postfix_expression | |
631 | : prefix_expression | |
953192ba | 632 | { |
bff988fa MD |
633 | $$ = $1; |
634 | } | |
635 | | postfix_expression DOT prefix_expression | |
636 | { | |
637 | $$ = $3; | |
953192ba | 638 | $$->u.expression.post_op = AST_LINK_DOT; |
953192ba MD |
639 | $$->u.expression.prev = $1; |
640 | } | |
bff988fa | 641 | | postfix_expression RARROW prefix_expression |
953192ba | 642 | { |
bff988fa | 643 | $$ = $3; |
953192ba | 644 | $$->u.expression.post_op = AST_LINK_RARROW; |
953192ba MD |
645 | $$->u.expression.prev = $1; |
646 | } | |
647 | ; | |
648 | ||
649 | unary_expression | |
650 | : postfix_expression | |
651 | { $$ = $1; } | |
bff988fa MD |
652 | | primary_expression |
653 | { $$ = $1; } | |
953192ba MD |
654 | | unary_operator unary_expression |
655 | { | |
656 | $$ = $1; | |
657 | $$->u.unary_op.child = $2; | |
658 | } | |
659 | ; | |
660 | ||
661 | unary_operator | |
662 | : PLUS | |
663 | { | |
664 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
665 | $$->u.unary_op.type = AST_UNARY_PLUS; | |
666 | } | |
667 | | MINUS | |
668 | { | |
669 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
670 | $$->u.unary_op.type = AST_UNARY_MINUS; | |
671 | } | |
672 | | NOT_OP | |
673 | { | |
674 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
675 | $$->u.unary_op.type = AST_UNARY_NOT; | |
676 | } | |
ab78f161 CB |
677 | | NOT_BIN |
678 | { | |
679 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
bff988fa | 680 | $$->u.unary_op.type = AST_UNARY_BIT_NOT; |
ab78f161 | 681 | } |
953192ba MD |
682 | ; |
683 | ||
684 | multiplicative_expression | |
685 | : unary_expression | |
686 | { $$ = $1; } | |
687 | | multiplicative_expression STAR unary_expression | |
688 | { | |
689 | $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3); | |
690 | } | |
691 | | multiplicative_expression DIV_OP unary_expression | |
692 | { | |
693 | $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3); | |
694 | } | |
695 | | multiplicative_expression MOD_OP unary_expression | |
696 | { | |
697 | $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3); | |
698 | } | |
699 | ; | |
700 | ||
701 | additive_expression | |
702 | : multiplicative_expression | |
703 | { $$ = $1; } | |
704 | | additive_expression PLUS multiplicative_expression | |
705 | { | |
706 | $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3); | |
707 | } | |
708 | | additive_expression MINUS multiplicative_expression | |
709 | { | |
710 | $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3); | |
711 | } | |
712 | ; | |
713 | ||
714 | shift_expression | |
715 | : additive_expression | |
716 | { $$ = $1; } | |
717 | | shift_expression LEFT_OP additive_expression | |
718 | { | |
116d3c01 | 719 | $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3); |
953192ba MD |
720 | } |
721 | | shift_expression RIGHT_OP additive_expression | |
722 | { | |
116d3c01 | 723 | $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3); |
953192ba MD |
724 | } |
725 | ; | |
726 | ||
831b702b | 727 | and_expression |
953192ba MD |
728 | : shift_expression |
729 | { $$ = $1; } | |
831b702b MD |
730 | | and_expression AND_BIN shift_expression |
731 | { | |
732 | $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3); | |
733 | } | |
734 | ; | |
735 | ||
736 | exclusive_or_expression | |
737 | : and_expression | |
738 | { $$ = $1; } | |
739 | | exclusive_or_expression XOR_BIN and_expression | |
740 | { | |
741 | $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3); | |
742 | } | |
743 | ; | |
744 | ||
745 | inclusive_or_expression | |
746 | : exclusive_or_expression | |
747 | { $$ = $1; } | |
748 | | inclusive_or_expression OR_BIN exclusive_or_expression | |
749 | { | |
750 | $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3); | |
751 | } | |
752 | ; | |
753 | ||
754 | relational_expression | |
755 | : inclusive_or_expression | |
756 | { $$ = $1; } | |
757 | | relational_expression LT_OP inclusive_or_expression | |
953192ba MD |
758 | { |
759 | $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3); | |
760 | } | |
831b702b | 761 | | relational_expression GT_OP inclusive_or_expression |
953192ba MD |
762 | { |
763 | $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3); | |
764 | } | |
831b702b | 765 | | relational_expression LE_OP inclusive_or_expression |
953192ba MD |
766 | { |
767 | $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3); | |
768 | } | |
831b702b | 769 | | relational_expression GE_OP inclusive_or_expression |
953192ba MD |
770 | { |
771 | $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3); | |
772 | } | |
773 | ; | |
774 | ||
775 | equality_expression | |
776 | : relational_expression | |
777 | { $$ = $1; } | |
778 | | equality_expression EQ_OP relational_expression | |
779 | { | |
780 | $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3); | |
781 | } | |
782 | | equality_expression NE_OP relational_expression | |
783 | { | |
784 | $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3); | |
785 | } | |
786 | ; | |
787 | ||
953192ba | 788 | logical_and_expression |
831b702b | 789 | : equality_expression |
953192ba | 790 | { $$ = $1; } |
831b702b | 791 | | logical_and_expression AND_OP equality_expression |
953192ba MD |
792 | { |
793 | $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3); | |
794 | } | |
795 | ; | |
796 | ||
797 | logical_or_expression | |
798 | : logical_and_expression | |
799 | { $$ = $1; } | |
800 | | logical_or_expression OR_OP logical_and_expression | |
801 | { | |
802 | $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3); | |
803 | } | |
804 | ; | |
805 | ||
806 | expression | |
807 | : logical_or_expression | |
808 | { $$ = $1; } | |
809 | ; | |
810 | ||
811 | translation_unit | |
812 | : expression | |
813 | { | |
814 | parser_ctx->ast->root.u.root.child = $1; | |
815 | } | |
816 | ; |