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