5 * Common Trace Format Metadata Grammar.
7 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
25 #include <helpers/list.h>
27 #include "ctf-scanner.h"
28 #include "ctf-parser.h"
31 #define printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, args)
32 #define printf_dbg_noarg(fmt) fprintf(stderr, "%s: " fmt, __func__)
34 int yyparse(struct ctf_scanner *scanner);
35 int yylex(union YYSTYPE *yyval, struct ctf_scanner *scanner);
36 int yylex_init_extra(struct ctf_scanner *scanner, yyscan_t * ptr_yy_globals);
37 int yylex_destroy(yyscan_t yyscanner) ;
38 void yyset_in(FILE * in_str, yyscan_t scanner);
43 struct cds_list_head gc;
47 char *strredup(char **dest, const char *src)
49 size_t len = strlen(src) + 1;
51 *dest = realloc(*dest, len);
58 static struct gc_string *gc_string_alloc(struct ctf_scanner *scanner, size_t len)
60 struct gc_string *gstr;
62 gstr = malloc(sizeof(*gstr) + len);
63 cds_list_add(&gstr->gc, &scanner->allocated_strings);
67 void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src)
69 lvalp->gs = gc_string_alloc(scanner, strlen(src) + 1);
70 strcpy(lvalp->gs->s, src);
73 static void init_scope(struct ctf_scanner_scope *scope,
74 struct ctf_scanner_scope *parent)
76 scope->parent = parent;
77 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
78 (GDestroyNotify) free, NULL);
81 static void finalize_scope(struct ctf_scanner_scope *scope)
83 g_hash_table_destroy(scope->types);
86 static void push_scope(struct ctf_scanner *scanner)
88 struct ctf_scanner_scope *ns;
90 printf_dbg_noarg("push scope\n");
91 ns = malloc(sizeof(struct ctf_scanner_scope));
92 init_scope(ns, scanner->cs);
96 static void pop_scope(struct ctf_scanner *scanner)
98 struct ctf_scanner_scope *os;
100 printf_dbg_noarg("pop scope\n");
102 scanner->cs = os->parent;
107 static int lookup_type(struct ctf_scanner_scope *s, const char *id)
111 ret = (int) g_hash_table_lookup(s->types, id);
112 printf_dbg("lookup %p %s %d\n", s, id, ret);
116 int is_type(struct ctf_scanner *scanner, const char *id)
118 struct ctf_scanner_scope *it;
121 for (it = scanner->cs; it != NULL; it = it->parent) {
122 if (lookup_type(it, id)) {
127 printf_dbg("is type %s %d\n", id, ret);
131 static void add_type(struct ctf_scanner *scanner, const char *id)
133 char *type_id = NULL;
135 printf_dbg("add type %s\n", id);
136 if (lookup_type(scanner->cs, id))
138 strredup(&type_id, id);
139 g_hash_table_insert(scanner->cs->types, type_id, type_id);
142 void yyerror(struct ctf_scanner *scanner, const char *str)
144 fprintf(stderr, "error %s\n", str);
152 static void free_strings(struct cds_list_head *list)
154 struct gc_string *gstr, *tmp;
156 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
160 static struct ctf_ast *ctf_ast_alloc(void)
164 ast = malloc(sizeof(*ast));
167 memset(ast, 0, sizeof(*ast));
171 static void ctf_ast_free(struct ctf_ast *ast)
175 int ctf_scanner_append_ast(struct ctf_scanner *scanner)
177 return yyparse(scanner);
180 struct ctf_scanner *ctf_scanner_alloc(FILE *input)
182 struct ctf_scanner *scanner;
185 scanner = malloc(sizeof(*scanner));
188 memset(scanner, 0, sizeof(*scanner));
190 ret = yylex_init_extra(scanner, &scanner->scanner);
192 fprintf(stderr, "yylex_init error\n");
193 goto cleanup_scanner;
195 yyset_in(input, scanner);
197 scanner->ast = ctf_ast_alloc();
200 init_scope(&scanner->root_scope, NULL);
201 CDS_INIT_LIST_HEAD(&scanner->allocated_strings);
206 ret = yylex_destroy(scanner->scanner);
208 fprintf(stderr, "yylex_destroy error\n");
214 void ctf_scanner_free(struct ctf_scanner *scanner)
218 finalize_scope(&scanner->root_scope);
219 free_strings(&scanner->allocated_strings);
220 ctf_ast_free(scanner->ast);
221 ret = yylex_destroy(scanner->scanner);
223 fprintf(stderr, "yylex_destroy error\n");
231 %parse-param {struct ctf_scanner *scanner}
232 %lex-param {struct ctf_scanner *scanner}
234 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE ESCSEQ CHAR_STRING_TOKEN LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW STAR PLUS MINUS LT GT TYPEASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA CONST CHAR DOUBLE ENUM EVENT FLOATING_POINT FLOAT INTEGER INT LONG SHORT SIGNED STREAM STRING STRUCT TRACE TYPEALIAS TYPEDEF UNSIGNED VARIANT VOID _BOOL _COMPLEX _IMAGINARY DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT
235 %token <gs> IDENTIFIER ID_TYPE
241 struct gc_string *gs;
281 | c_char_sequence c_char
289 /* 1.6 String literals */
293 | s_char_sequence s_char
301 /* 2: Phrase structure grammar */
309 | HEXADECIMAL_CONSTANT
310 | STRING_LITERAL_START DQUOTE
311 | STRING_LITERAL_START s_char_sequence DQUOTE
312 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
313 | LPAREN unary_expression RPAREN
314 | postfix_expression LSBRAC unary_expression RSBRAC
315 | postfix_expression DOT IDENTIFIER
316 | postfix_expression DOT ID_TYPE
317 | postfix_expression RARROW IDENTIFIER
318 | postfix_expression RARROW ID_TYPE
323 | PLUS postfix_expression
324 | MINUS postfix_expression
327 unary_expression_or_range:
328 unary_expression DOTDOTDOT unary_expression
332 /* 2.2: Declarations */
335 declaration_specifiers SEMICOLON
339 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
340 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
341 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
342 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list SEMICOLON
343 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list SEMICOLON
347 event_declaration_begin event_declaration_end
348 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
351 event_declaration_begin:
353 { push_scope(scanner); }
356 event_declaration_end:
358 { pop_scope(scanner); }
363 stream_declaration_begin stream_declaration_end
364 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
367 stream_declaration_begin:
369 { push_scope(scanner); }
372 stream_declaration_end:
374 { pop_scope(scanner); }
379 trace_declaration_begin trace_declaration_end
380 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
383 trace_declaration_begin:
385 { push_scope(scanner); }
388 trace_declaration_end:
390 { pop_scope(scanner); }
393 declaration_specifiers:
396 | declaration_specifiers CONST
397 | declaration_specifiers type_specifier
400 type_declarator_list:
402 | type_declarator_list COMMA type_declarator
405 abstract_type_declarator_list:
406 abstract_type_declarator
407 | abstract_type_declarator_list COMMA abstract_type_declarator
423 | FLOATING_POINT LBRAC RBRAC
424 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
425 | INTEGER LBRAC RBRAC
426 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
428 | STRING LBRAC ctf_assignment_expression_list RBRAC
429 | ENUM enum_type_specifier
430 | VARIANT variant_type_specifier
431 | STRUCT struct_type_specifier
434 struct_type_specifier:
435 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
436 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
437 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
442 struct_declaration_begin:
444 { push_scope(scanner); }
447 struct_declaration_end:
449 { pop_scope(scanner); }
452 variant_type_specifier:
453 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
454 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
455 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
456 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
457 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
458 | IDENTIFIER LT IDENTIFIER GT
459 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
460 | IDENTIFIER LT ID_TYPE GT
461 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
462 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
463 | ID_TYPE LT IDENTIFIER GT
464 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
465 | ID_TYPE LT ID_TYPE GT
468 variant_declaration_begin:
470 { push_scope(scanner); }
473 variant_declaration_end:
475 { pop_scope(scanner); }
478 type_specifier_or_integer_constant:
479 declaration_specifiers
482 | HEXADECIMAL_CONSTANT
486 LBRAC enumerator_list RBRAC
487 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
488 | IDENTIFIER LBRAC enumerator_list RBRAC
489 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
490 | ID_TYPE LBRAC enumerator_list RBRAC
491 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
492 | LBRAC enumerator_list COMMA RBRAC
493 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
494 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
495 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
497 | IDENTIFIER LT type_specifier_or_integer_constant GT
498 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
499 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
501 | ID_TYPE LT type_specifier_or_integer_constant GT
504 struct_or_variant_declaration_list:
506 | struct_or_variant_declaration_list struct_or_variant_declaration
509 struct_or_variant_declaration:
510 specifier_qualifier_list struct_or_variant_declarator_list SEMICOLON
511 | specifier_qualifier_list TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
512 | TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
513 | specifier_qualifier_list TYPEDEF type_declarator_list SEMICOLON
514 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON specifier_qualifier_list abstract_type_declarator_list SEMICOLON
515 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON type_declarator_list SEMICOLON
518 specifier_qualifier_list:
521 | specifier_qualifier_list CONST
522 | specifier_qualifier_list type_specifier
525 struct_or_variant_declarator_list:
526 struct_or_variant_declarator
527 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
530 struct_or_variant_declarator:
532 | COLON unary_expression
533 | declarator COLON unary_expression
538 | enumerator_list COMMA enumerator
545 | STRING_LITERAL_START DQUOTE
546 | STRING_LITERAL_START s_char_sequence DQUOTE
547 | IDENTIFIER EQUAL unary_expression_or_range
548 | ID_TYPE EQUAL unary_expression_or_range
549 | keywords EQUAL unary_expression_or_range
550 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
551 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
554 abstract_declarator_list:
556 | abstract_declarator_list COMMA abstract_declarator
560 direct_abstract_declarator
561 | pointer direct_abstract_declarator
564 direct_abstract_declarator:
567 | LPAREN abstract_declarator RPAREN
568 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
569 | direct_abstract_declarator LSBRAC RSBRAC
574 | pointer direct_declarator
579 | LPAREN declarator RPAREN
580 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
584 direct_type_declarator
585 | pointer direct_type_declarator
588 direct_type_declarator:
590 { add_type(scanner, $1->s); }
591 | LPAREN type_declarator RPAREN
592 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
595 abstract_type_declarator:
596 direct_abstract_type_declarator
597 | pointer direct_abstract_type_declarator
600 direct_abstract_type_declarator:
603 { add_type(scanner, $1->s); }
604 | LPAREN abstract_type_declarator RPAREN
605 | direct_abstract_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
606 | direct_abstract_type_declarator LSBRAC RSBRAC
612 | STAR type_qualifier_list pointer
617 | type_qualifier_list CONST
620 /* 2.3: CTF-specific declarations */
622 ctf_assignment_expression_list:
623 ctf_assignment_expression SEMICOLON
624 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
627 ctf_assignment_expression:
628 unary_expression EQUAL unary_expression
629 | unary_expression TYPEASSIGN type_specifier
630 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
631 | TYPEDEF declaration_specifiers type_declarator_list
632 | declaration_specifiers TYPEDEF type_declarator_list
633 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list
634 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list