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