Add source file headers to CTF lexer/parser
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
1 %{
2 /*
3 * ctf-parser.y
4 *
5 * Common Trace Format Metadata Grammar.
6 *
7 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <helpers/list.h>
26 #include <glib.h>
27 #include "ctf-scanner.h"
28 #include "ctf-parser.h"
29 #include "ctf-ast.h"
30
31 #define printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, args)
32 #define printf_dbg_noarg(fmt) fprintf(stderr, "%s: " fmt, __func__)
33
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);
39
40 int yydebug;
41
42 struct gc_string {
43 struct cds_list_head gc;
44 char s[];
45 };
46
47 char *strredup(char **dest, const char *src)
48 {
49 size_t len = strlen(src) + 1;
50
51 *dest = realloc(*dest, len);
52 if (!*dest)
53 return NULL;
54 strcpy(*dest, src);
55 return *dest;
56 }
57
58 static struct gc_string *gc_string_alloc(struct ctf_scanner *scanner, size_t len)
59 {
60 struct gc_string *gstr;
61
62 gstr = malloc(sizeof(*gstr) + len);
63 cds_list_add(&gstr->gc, &scanner->allocated_strings);
64 return gstr;
65 }
66
67 void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src)
68 {
69 lvalp->gs = gc_string_alloc(scanner, strlen(src) + 1);
70 strcpy(lvalp->gs->s, src);
71 }
72
73 static void init_scope(struct scope *scope, struct scope *parent)
74 {
75 scope->parent = parent;
76 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
77 (GDestroyNotify) free, NULL);
78 }
79
80 static void finalize_scope(struct scope *scope)
81 {
82 g_hash_table_destroy(scope->types);
83 }
84
85 static void push_scope(struct ctf_scanner *scanner)
86 {
87 struct scope *ns;
88
89 printf_dbg_noarg("push scope\n");
90 ns = malloc(sizeof(struct scope));
91 init_scope(ns, scanner->cs);
92 scanner->cs = ns;
93 }
94
95 static void pop_scope(struct ctf_scanner *scanner)
96 {
97 struct scope *os;
98
99 printf_dbg_noarg("pop scope\n");
100 os = scanner->cs;
101 scanner->cs = os->parent;
102 finalize_scope(os);
103 free(os);
104 }
105
106 static int lookup_type(struct scope *s, const char *id)
107 {
108 int ret;
109
110 ret = (int) g_hash_table_lookup(s->types, id);
111 printf_dbg("lookup %p %s %d\n", s, id, ret);
112 return ret;
113 }
114
115 int is_type(struct ctf_scanner *scanner, const char *id)
116 {
117 struct scope *it;
118 int ret = 0;
119
120 for (it = scanner->cs; it != NULL; it = it->parent) {
121 if (lookup_type(it, id)) {
122 ret = 1;
123 break;
124 }
125 }
126 printf_dbg("is type %s %d\n", id, ret);
127 return ret;
128 }
129
130 static void add_type(struct ctf_scanner *scanner, const char *id)
131 {
132 char *type_id = NULL;
133
134 printf_dbg("add type %s\n", id);
135 if (lookup_type(scanner->cs, id))
136 return;
137 strredup(&type_id, id);
138 g_hash_table_insert(scanner->cs->types, type_id, type_id);
139 }
140
141 void yyerror(struct ctf_scanner *scanner, const char *str)
142 {
143 fprintf(stderr, "error %s\n", str);
144 }
145
146 int yywrap(void)
147 {
148 return 1;
149 }
150
151 static void free_strings(struct cds_list_head *list)
152 {
153 struct gc_string *gstr, *tmp;
154
155 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
156 free(gstr);
157 }
158
159 static struct ctf_ast *ctf_ast_alloc(void)
160 {
161 struct ctf_ast *ast;
162
163 ast = malloc(sizeof(*ast));
164 if (!ast)
165 return NULL;
166 memset(ast, 0, sizeof(*ast));
167 return ast;
168 }
169
170 static void ctf_ast_free(struct ctf_ast *ast)
171 {
172 }
173
174 int ctf_scanner_append_ast(struct ctf_scanner *scanner)
175 {
176 return yyparse(scanner);
177 }
178
179 struct ctf_scanner *ctf_scanner_alloc(FILE *input)
180 {
181 struct ctf_scanner *scanner;
182 int ret;
183
184 scanner = malloc(sizeof(*scanner));
185 if (!scanner)
186 return NULL;
187 memset(scanner, 0, sizeof(*scanner));
188
189 ret = yylex_init_extra(scanner, &scanner->scanner);
190 if (ret) {
191 fprintf(stderr, "yylex_init error\n");
192 goto cleanup_scanner;
193 }
194 yyset_in(input, scanner);
195
196 scanner->ast = ctf_ast_alloc();
197 if (!scanner->ast)
198 goto cleanup_lexer;
199 init_scope(&scanner->root_scope, NULL);
200 CDS_INIT_LIST_HEAD(&scanner->allocated_strings);
201
202 return scanner;
203
204 cleanup_lexer:
205 ret = yylex_destroy(scanner->scanner);
206 if (!ret)
207 fprintf(stderr, "yylex_destroy error\n");
208 cleanup_scanner:
209 free(scanner);
210 return NULL;
211 }
212
213 void ctf_scanner_free(struct ctf_scanner *scanner)
214 {
215 int ret;
216
217 finalize_scope(&scanner->root_scope);
218 free_strings(&scanner->allocated_strings);
219 ctf_ast_free(scanner->ast);
220 ret = yylex_destroy(scanner->scanner);
221 if (ret)
222 fprintf(stderr, "yylex_destroy error\n");
223 free(scanner);
224 }
225
226 %}
227
228 %define api.pure
229 /* %locations */
230 %parse-param {struct ctf_scanner *scanner}
231 %lex-param {struct ctf_scanner *scanner}
232 %start file
233 %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
234 %token <gs> IDENTIFIER ID_TYPE
235 %token ERROR
236 %union
237 {
238 long long ll;
239 char c;
240 struct gc_string *gs;
241 struct ctf_node *n;
242 }
243
244 %%
245
246 file:
247 declaration
248 | file declaration
249 ;
250
251 keywords:
252 VOID
253 | CHAR
254 | SHORT
255 | INT
256 | LONG
257 | FLOAT
258 | DOUBLE
259 | SIGNED
260 | UNSIGNED
261 | _BOOL
262 | _COMPLEX
263 | FLOATING_POINT
264 | INTEGER
265 | STRING
266 | ENUM
267 | VARIANT
268 | STRUCT
269 | CONST
270 | TYPEDEF
271 | EVENT
272 | STREAM
273 | TRACE
274 ;
275
276 /* 1.5 Constants */
277
278 c_char_sequence:
279 c_char
280 | c_char_sequence c_char
281 ;
282
283 c_char:
284 CHAR_STRING_TOKEN
285 | ESCSEQ
286 ;
287
288 /* 1.6 String literals */
289
290 s_char_sequence:
291 s_char
292 | s_char_sequence s_char
293 ;
294
295 s_char:
296 CHAR_STRING_TOKEN
297 | ESCSEQ
298 ;
299
300 /* 2: Phrase structure grammar */
301
302 postfix_expression:
303 IDENTIFIER
304 | ID_TYPE
305 | keywords
306 | DECIMAL_CONSTANT
307 | OCTAL_CONSTANT
308 | HEXADECIMAL_CONSTANT
309 | STRING_LITERAL_START DQUOTE
310 | STRING_LITERAL_START s_char_sequence DQUOTE
311 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
312 | LPAREN unary_expression RPAREN
313 | postfix_expression LSBRAC unary_expression RSBRAC
314 | postfix_expression DOT IDENTIFIER
315 | postfix_expression DOT ID_TYPE
316 | postfix_expression RARROW IDENTIFIER
317 | postfix_expression RARROW ID_TYPE
318 ;
319
320 unary_expression:
321 postfix_expression
322 | PLUS postfix_expression
323 | MINUS postfix_expression
324 ;
325
326 unary_expression_or_range:
327 unary_expression DOTDOTDOT unary_expression
328 | unary_expression
329 ;
330
331 /* 2.2: Declarations */
332
333 declaration:
334 declaration_specifiers SEMICOLON
335 | event_declaration
336 | stream_declaration
337 | trace_declaration
338 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
339 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
340 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
341 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list SEMICOLON
342 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list SEMICOLON
343 ;
344
345 event_declaration:
346 event_declaration_begin event_declaration_end
347 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
348 ;
349
350 event_declaration_begin:
351 EVENT LBRAC
352 {
353 push_scope(scanner);
354 }
355 ;
356
357 event_declaration_end:
358 RBRAC SEMICOLON
359 {
360 pop_scope(scanner);
361 }
362 ;
363
364
365 stream_declaration:
366 stream_declaration_begin stream_declaration_end
367 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
368 ;
369
370 stream_declaration_begin:
371 STREAM LBRAC
372 {
373 push_scope(scanner);
374 }
375 ;
376
377 stream_declaration_end:
378 RBRAC SEMICOLON
379 {
380 pop_scope(scanner);
381 }
382 ;
383
384
385 trace_declaration:
386 trace_declaration_begin trace_declaration_end
387 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
388 ;
389
390 trace_declaration_begin:
391 TRACE LBRAC
392 {
393 push_scope(scanner);
394 }
395 ;
396
397 trace_declaration_end:
398 RBRAC SEMICOLON
399 {
400 pop_scope(scanner);
401 }
402 ;
403
404 declaration_specifiers:
405 CONST
406 | type_specifier
407 | declaration_specifiers CONST
408 | declaration_specifiers type_specifier
409 ;
410
411 type_declarator_list:
412 type_declarator
413 | type_declarator_list COMMA type_declarator
414 ;
415
416 abstract_type_declarator_list:
417 abstract_type_declarator
418 | abstract_type_declarator_list COMMA abstract_type_declarator
419 ;
420
421 type_specifier:
422 VOID
423 | CHAR
424 | SHORT
425 | INT
426 | LONG
427 | FLOAT
428 | DOUBLE
429 | SIGNED
430 | UNSIGNED
431 | _BOOL
432 | _COMPLEX
433 | ID_TYPE
434 | FLOATING_POINT LBRAC RBRAC
435 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
436 | INTEGER LBRAC RBRAC
437 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
438 | STRING LBRAC RBRAC
439 | STRING LBRAC ctf_assignment_expression_list RBRAC
440 | ENUM enum_type_specifier
441 | VARIANT variant_type_specifier
442 | STRUCT struct_type_specifier
443 ;
444
445 struct_type_specifier:
446 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
447 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
448 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
449 | IDENTIFIER
450 | ID_TYPE
451 ;
452
453 struct_declaration_begin:
454 LBRAC
455 {
456 push_scope(scanner);
457 }
458 ;
459
460 struct_declaration_end:
461 RBRAC
462 {
463 pop_scope(scanner);
464 }
465 ;
466
467 variant_type_specifier:
468 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
469 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
470 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
471 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
472 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
473 | IDENTIFIER LT IDENTIFIER GT
474 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
475 | IDENTIFIER LT ID_TYPE GT
476 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
477 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
478 | ID_TYPE LT IDENTIFIER GT
479 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
480 | ID_TYPE LT ID_TYPE GT
481 ;
482
483 variant_declaration_begin:
484 LBRAC
485 {
486 push_scope(scanner);
487 }
488 ;
489
490 variant_declaration_end:
491 RBRAC
492 {
493 pop_scope(scanner);
494 }
495 ;
496
497 type_specifier_or_integer_constant:
498 declaration_specifiers
499 | DECIMAL_CONSTANT
500 | OCTAL_CONSTANT
501 | HEXADECIMAL_CONSTANT
502 ;
503
504 enum_type_specifier:
505 LBRAC enumerator_list RBRAC
506 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
507 | IDENTIFIER LBRAC enumerator_list RBRAC
508 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
509 | ID_TYPE LBRAC enumerator_list RBRAC
510 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
511 | LBRAC enumerator_list COMMA RBRAC
512 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
513 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
514 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
515 | IDENTIFIER
516 | IDENTIFIER LT type_specifier_or_integer_constant GT
517 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
518 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
519 | ID_TYPE
520 | ID_TYPE LT type_specifier_or_integer_constant GT
521 ;
522
523 struct_or_variant_declaration_list:
524 /* empty */
525 | struct_or_variant_declaration_list struct_or_variant_declaration
526 ;
527
528 struct_or_variant_declaration:
529 specifier_qualifier_list struct_or_variant_declarator_list SEMICOLON
530 | specifier_qualifier_list TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
531 | TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
532 | specifier_qualifier_list TYPEDEF type_declarator_list SEMICOLON
533 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON specifier_qualifier_list abstract_type_declarator_list SEMICOLON
534 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON type_declarator_list SEMICOLON
535 ;
536
537 specifier_qualifier_list:
538 CONST
539 | type_specifier
540 | specifier_qualifier_list CONST
541 | specifier_qualifier_list type_specifier
542 ;
543
544 struct_or_variant_declarator_list:
545 struct_or_variant_declarator
546 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
547 ;
548
549 struct_or_variant_declarator:
550 declarator
551 | COLON unary_expression
552 | declarator COLON unary_expression
553 ;
554
555 enumerator_list:
556 enumerator
557 | enumerator_list COMMA enumerator
558 ;
559
560 enumerator:
561 IDENTIFIER
562 | ID_TYPE
563 | keywords
564 | STRING_LITERAL_START DQUOTE
565 | STRING_LITERAL_START s_char_sequence DQUOTE
566 | IDENTIFIER EQUAL unary_expression_or_range
567 | ID_TYPE EQUAL unary_expression_or_range
568 | keywords EQUAL unary_expression_or_range
569 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
570 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
571 ;
572
573 abstract_declarator_list:
574 abstract_declarator
575 | abstract_declarator_list COMMA abstract_declarator
576 ;
577
578 abstract_declarator:
579 direct_abstract_declarator
580 | pointer direct_abstract_declarator
581 ;
582
583 direct_abstract_declarator:
584 /* empty */
585 | IDENTIFIER
586 | LPAREN abstract_declarator RPAREN
587 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
588 | direct_abstract_declarator LSBRAC RSBRAC
589 ;
590
591 declarator:
592 direct_declarator
593 | pointer direct_declarator
594 ;
595
596 direct_declarator:
597 IDENTIFIER
598 | LPAREN declarator RPAREN
599 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
600 ;
601
602 type_declarator:
603 direct_type_declarator
604 | pointer direct_type_declarator
605 ;
606
607 direct_type_declarator:
608 IDENTIFIER
609 {
610 add_type(scanner, $1->s);
611 }
612 | LPAREN type_declarator RPAREN
613 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
614 ;
615
616 abstract_type_declarator:
617 direct_abstract_type_declarator
618 | pointer direct_abstract_type_declarator
619 ;
620
621 direct_abstract_type_declarator:
622 /* empty */
623 | IDENTIFIER
624 {
625 add_type(scanner, $1->s);
626 }
627 | LPAREN abstract_type_declarator RPAREN
628 | direct_abstract_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
629 | direct_abstract_type_declarator LSBRAC RSBRAC
630 ;
631
632 pointer:
633 STAR
634 | STAR pointer
635 | STAR type_qualifier_list pointer
636 ;
637
638 type_qualifier_list:
639 CONST
640 | type_qualifier_list CONST
641 ;
642
643 /* 2.3: CTF-specific declarations */
644
645 ctf_assignment_expression_list:
646 ctf_assignment_expression SEMICOLON
647 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
648 ;
649
650 ctf_assignment_expression:
651 unary_expression EQUAL unary_expression
652 | unary_expression TYPEASSIGN type_specifier
653 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
654 | TYPEDEF declaration_specifiers type_declarator_list
655 | declaration_specifiers TYPEDEF type_declarator_list
656 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list
657 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list
658 ;
This page took 0.043024 seconds and 5 git commands to generate.