Cleanup parser code, add some node IDs to AST
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
CommitLineData
8b9d5b5e
MD
1%{
2/*
c59a87f5 3 * ctf-parser.y
8b9d5b5e
MD
4 *
5 * Common Trace Format Metadata Grammar.
c59a87f5
MD
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.
8b9d5b5e
MD
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>
34d3acc4 27#include "ctf-scanner.h"
8b9d5b5e
MD
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
34d3acc4
MD
34int yyparse(struct ctf_scanner *scanner);
35int yylex(union YYSTYPE *yyval, struct ctf_scanner *scanner);
36int yylex_init_extra(struct ctf_scanner *scanner, yyscan_t * ptr_yy_globals);
37int yylex_destroy(yyscan_t yyscanner) ;
38void yyset_in(FILE * in_str, yyscan_t scanner);
8b9d5b5e 39
8b9d5b5e
MD
40int yydebug;
41
8b9d5b5e
MD
42struct gc_string {
43 struct cds_list_head gc;
44 char s[];
45};
46
8b9d5b5e
MD
47char *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
34d3acc4 58static struct gc_string *gc_string_alloc(struct ctf_scanner *scanner, size_t len)
8b9d5b5e
MD
59{
60 struct gc_string *gstr;
61
62 gstr = malloc(sizeof(*gstr) + len);
34d3acc4 63 cds_list_add(&gstr->gc, &scanner->allocated_strings);
8b9d5b5e
MD
64 return gstr;
65}
66
34d3acc4 67void setstring(struct ctf_scanner *scanner, YYSTYPE *lvalp, const char *src)
8b9d5b5e 68{
34d3acc4
MD
69 lvalp->gs = gc_string_alloc(scanner, strlen(src) + 1);
70 strcpy(lvalp->gs->s, src);
8b9d5b5e
MD
71}
72
609bd1bf
MD
73static void init_scope(struct ctf_scanner_scope *scope,
74 struct ctf_scanner_scope *parent)
8b9d5b5e
MD
75{
76 scope->parent = parent;
77 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
78 (GDestroyNotify) free, NULL);
79}
80
609bd1bf 81static void finalize_scope(struct ctf_scanner_scope *scope)
8b9d5b5e
MD
82{
83 g_hash_table_destroy(scope->types);
84}
85
34d3acc4 86static void push_scope(struct ctf_scanner *scanner)
8b9d5b5e 87{
609bd1bf 88 struct ctf_scanner_scope *ns;
8b9d5b5e
MD
89
90 printf_dbg_noarg("push scope\n");
609bd1bf 91 ns = malloc(sizeof(struct ctf_scanner_scope));
34d3acc4
MD
92 init_scope(ns, scanner->cs);
93 scanner->cs = ns;
8b9d5b5e
MD
94}
95
34d3acc4 96static void pop_scope(struct ctf_scanner *scanner)
8b9d5b5e 97{
609bd1bf 98 struct ctf_scanner_scope *os;
8b9d5b5e
MD
99
100 printf_dbg_noarg("pop scope\n");
34d3acc4
MD
101 os = scanner->cs;
102 scanner->cs = os->parent;
8b9d5b5e
MD
103 finalize_scope(os);
104 free(os);
105}
106
609bd1bf 107static int lookup_type(struct ctf_scanner_scope *s, const char *id)
8b9d5b5e
MD
108{
109 int ret;
110
111 ret = (int) g_hash_table_lookup(s->types, id);
112 printf_dbg("lookup %p %s %d\n", s, id, ret);
113 return ret;
114}
115
34d3acc4 116int is_type(struct ctf_scanner *scanner, const char *id)
8b9d5b5e 117{
609bd1bf 118 struct ctf_scanner_scope *it;
8b9d5b5e
MD
119 int ret = 0;
120
34d3acc4 121 for (it = scanner->cs; it != NULL; it = it->parent) {
8b9d5b5e
MD
122 if (lookup_type(it, id)) {
123 ret = 1;
124 break;
125 }
126 }
127 printf_dbg("is type %s %d\n", id, ret);
128 return ret;
129}
130
34d3acc4 131static void add_type(struct ctf_scanner *scanner, const char *id)
8b9d5b5e
MD
132{
133 char *type_id = NULL;
134
135 printf_dbg("add type %s\n", id);
34d3acc4 136 if (lookup_type(scanner->cs, id))
8b9d5b5e
MD
137 return;
138 strredup(&type_id, id);
34d3acc4 139 g_hash_table_insert(scanner->cs->types, type_id, type_id);
8b9d5b5e
MD
140}
141
34d3acc4 142void yyerror(struct ctf_scanner *scanner, const char *str)
8b9d5b5e
MD
143{
144 fprintf(stderr, "error %s\n", str);
145}
146
147int yywrap(void)
148{
149 return 1;
150}
151
34d3acc4 152static void free_strings(struct cds_list_head *list)
8b9d5b5e
MD
153{
154 struct gc_string *gstr, *tmp;
155
34d3acc4 156 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
8b9d5b5e
MD
157 free(gstr);
158}
159
34d3acc4 160static struct ctf_ast *ctf_ast_alloc(void)
8b9d5b5e 161{
34d3acc4
MD
162 struct ctf_ast *ast;
163
164 ast = malloc(sizeof(*ast));
165 if (!ast)
166 return NULL;
167 memset(ast, 0, sizeof(*ast));
168 return ast;
169}
170
171static void ctf_ast_free(struct ctf_ast *ast)
172{
173}
174
175int ctf_scanner_append_ast(struct ctf_scanner *scanner)
176{
177 return yyparse(scanner);
178}
179
180struct ctf_scanner *ctf_scanner_alloc(FILE *input)
181{
182 struct ctf_scanner *scanner;
183 int ret;
184
185 scanner = malloc(sizeof(*scanner));
186 if (!scanner)
187 return NULL;
188 memset(scanner, 0, sizeof(*scanner));
189
190 ret = yylex_init_extra(scanner, &scanner->scanner);
191 if (ret) {
192 fprintf(stderr, "yylex_init error\n");
193 goto cleanup_scanner;
194 }
195 yyset_in(input, scanner);
196
197 scanner->ast = ctf_ast_alloc();
198 if (!scanner->ast)
199 goto cleanup_lexer;
200 init_scope(&scanner->root_scope, NULL);
201 CDS_INIT_LIST_HEAD(&scanner->allocated_strings);
202
203 return scanner;
204
205cleanup_lexer:
206 ret = yylex_destroy(scanner->scanner);
207 if (!ret)
208 fprintf(stderr, "yylex_destroy error\n");
209cleanup_scanner:
210 free(scanner);
211 return NULL;
212}
213
214void ctf_scanner_free(struct ctf_scanner *scanner)
215{
216 int ret;
217
218 finalize_scope(&scanner->root_scope);
219 free_strings(&scanner->allocated_strings);
220 ctf_ast_free(scanner->ast);
221 ret = yylex_destroy(scanner->scanner);
222 if (ret)
223 fprintf(stderr, "yylex_destroy error\n");
224 free(scanner);
225}
8b9d5b5e
MD
226
227%}
228
34d3acc4
MD
229%define api.pure
230 /* %locations */
231%parse-param {struct ctf_scanner *scanner}
232%lex-param {struct ctf_scanner *scanner}
8b9d5b5e 233%start file
8b9d5b5e
MD
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
236%token ERROR
237%union
238{
239 long long ll;
240 char c;
241 struct gc_string *gs;
242 struct ctf_node *n;
243}
244
8b9d5b5e
MD
245%%
246
247file:
248 declaration
249 | file declaration
250 ;
251
252keywords:
253 VOID
254 | CHAR
255 | SHORT
256 | INT
257 | LONG
258 | FLOAT
259 | DOUBLE
260 | SIGNED
261 | UNSIGNED
262 | _BOOL
263 | _COMPLEX
264 | FLOATING_POINT
265 | INTEGER
266 | STRING
267 | ENUM
268 | VARIANT
269 | STRUCT
270 | CONST
271 | TYPEDEF
272 | EVENT
273 | STREAM
274 | TRACE
275 ;
276
277/* 1.5 Constants */
278
279c_char_sequence:
280 c_char
281 | c_char_sequence c_char
282 ;
283
284c_char:
285 CHAR_STRING_TOKEN
286 | ESCSEQ
287 ;
288
289/* 1.6 String literals */
290
291s_char_sequence:
292 s_char
293 | s_char_sequence s_char
294 ;
295
296s_char:
297 CHAR_STRING_TOKEN
298 | ESCSEQ
299 ;
300
301/* 2: Phrase structure grammar */
302
303postfix_expression:
304 IDENTIFIER
305 | ID_TYPE
306 | keywords
307 | DECIMAL_CONSTANT
308 | OCTAL_CONSTANT
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
319 ;
320
321unary_expression:
322 postfix_expression
323 | PLUS postfix_expression
324 | MINUS postfix_expression
325 ;
326
327unary_expression_or_range:
328 unary_expression DOTDOTDOT unary_expression
329 | unary_expression
330 ;
331
332/* 2.2: Declarations */
333
334declaration:
335 declaration_specifiers SEMICOLON
336 | event_declaration
337 | stream_declaration
338 | trace_declaration
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
344 ;
345
346event_declaration:
347 event_declaration_begin event_declaration_end
348 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
349 ;
350
351event_declaration_begin:
352 EVENT LBRAC
fce8006d 353 { push_scope(scanner); }
8b9d5b5e
MD
354 ;
355
356event_declaration_end:
357 RBRAC SEMICOLON
fce8006d 358 { pop_scope(scanner); }
8b9d5b5e
MD
359 ;
360
361
362stream_declaration:
363 stream_declaration_begin stream_declaration_end
364 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
365 ;
366
367stream_declaration_begin:
368 STREAM LBRAC
fce8006d 369 { push_scope(scanner); }
8b9d5b5e
MD
370 ;
371
372stream_declaration_end:
373 RBRAC SEMICOLON
fce8006d 374 { pop_scope(scanner); }
8b9d5b5e
MD
375 ;
376
377
378trace_declaration:
379 trace_declaration_begin trace_declaration_end
380 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
381 ;
382
383trace_declaration_begin:
384 TRACE LBRAC
fce8006d 385 { push_scope(scanner); }
8b9d5b5e
MD
386 ;
387
388trace_declaration_end:
389 RBRAC SEMICOLON
fce8006d 390 { pop_scope(scanner); }
8b9d5b5e
MD
391 ;
392
393declaration_specifiers:
394 CONST
395 | type_specifier
396 | declaration_specifiers CONST
397 | declaration_specifiers type_specifier
398 ;
399
400type_declarator_list:
401 type_declarator
402 | type_declarator_list COMMA type_declarator
403 ;
404
405abstract_type_declarator_list:
406 abstract_type_declarator
407 | abstract_type_declarator_list COMMA abstract_type_declarator
408 ;
409
410type_specifier:
411 VOID
412 | CHAR
413 | SHORT
414 | INT
415 | LONG
416 | FLOAT
417 | DOUBLE
418 | SIGNED
419 | UNSIGNED
420 | _BOOL
421 | _COMPLEX
422 | ID_TYPE
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
427 | STRING LBRAC RBRAC
428 | STRING LBRAC ctf_assignment_expression_list RBRAC
429 | ENUM enum_type_specifier
430 | VARIANT variant_type_specifier
431 | STRUCT struct_type_specifier
432 ;
433
434struct_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
438 | IDENTIFIER
439 | ID_TYPE
440 ;
441
442struct_declaration_begin:
443 LBRAC
fce8006d 444 { push_scope(scanner); }
8b9d5b5e
MD
445 ;
446
447struct_declaration_end:
448 RBRAC
fce8006d 449 { pop_scope(scanner); }
8b9d5b5e
MD
450 ;
451
452variant_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
466 ;
467
468variant_declaration_begin:
469 LBRAC
fce8006d 470 { push_scope(scanner); }
8b9d5b5e
MD
471 ;
472
473variant_declaration_end:
474 RBRAC
fce8006d 475 { pop_scope(scanner); }
8b9d5b5e
MD
476 ;
477
478type_specifier_or_integer_constant:
479 declaration_specifiers
480 | DECIMAL_CONSTANT
481 | OCTAL_CONSTANT
482 | HEXADECIMAL_CONSTANT
483 ;
484
485enum_type_specifier:
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
496 | IDENTIFIER
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
500 | ID_TYPE
501 | ID_TYPE LT type_specifier_or_integer_constant GT
502 ;
503
504struct_or_variant_declaration_list:
505 /* empty */
506 | struct_or_variant_declaration_list struct_or_variant_declaration
507 ;
508
509struct_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
516 ;
517
518specifier_qualifier_list:
519 CONST
520 | type_specifier
521 | specifier_qualifier_list CONST
522 | specifier_qualifier_list type_specifier
523 ;
524
525struct_or_variant_declarator_list:
526 struct_or_variant_declarator
527 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
528 ;
529
530struct_or_variant_declarator:
531 declarator
532 | COLON unary_expression
533 | declarator COLON unary_expression
534 ;
535
536enumerator_list:
537 enumerator
538 | enumerator_list COMMA enumerator
539 ;
540
541enumerator:
542 IDENTIFIER
543 | ID_TYPE
544 | keywords
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
552 ;
553
554abstract_declarator_list:
555 abstract_declarator
556 | abstract_declarator_list COMMA abstract_declarator
557 ;
558
559abstract_declarator:
560 direct_abstract_declarator
561 | pointer direct_abstract_declarator
562 ;
563
564direct_abstract_declarator:
565 /* empty */
566 | IDENTIFIER
567 | LPAREN abstract_declarator RPAREN
568 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
569 | direct_abstract_declarator LSBRAC RSBRAC
570 ;
571
572declarator:
573 direct_declarator
574 | pointer direct_declarator
575 ;
576
577direct_declarator:
578 IDENTIFIER
579 | LPAREN declarator RPAREN
580 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
581 ;
582
583type_declarator:
584 direct_type_declarator
585 | pointer direct_type_declarator
586 ;
587
588direct_type_declarator:
589 IDENTIFIER
fce8006d 590 { add_type(scanner, $1->s); }
8b9d5b5e
MD
591 | LPAREN type_declarator RPAREN
592 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
593 ;
594
595abstract_type_declarator:
596 direct_abstract_type_declarator
597 | pointer direct_abstract_type_declarator
598 ;
599
600direct_abstract_type_declarator:
601 /* empty */
602 | IDENTIFIER
fce8006d 603 { add_type(scanner, $1->s); }
8b9d5b5e
MD
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
607 ;
608
609pointer:
610 STAR
611 | STAR pointer
612 | STAR type_qualifier_list pointer
613 ;
614
615type_qualifier_list:
616 CONST
617 | type_qualifier_list CONST
618 ;
619
620/* 2.3: CTF-specific declarations */
621
622ctf_assignment_expression_list:
623 ctf_assignment_expression SEMICOLON
624 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
625 ;
626
627ctf_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
635 ;
This page took 0.047676 seconds and 4 git commands to generate.