rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.parser / src / main / antlr3 / org / eclipse / tracecompass / ctf / parser / CTFParser.g
CommitLineData
866e5b51
FC
1parser grammar CTFParser;
2
3options {
91fd06f5
EB
4 language = Java;
5 output = AST;
6 ASTLabelType = CommonTree;
7 tokenVocab = CTFLexer;
866e5b51
FC
8}
9
10tokens {
91fd06f5
EB
11 ROOT;
12
13 EVENT;
14 STREAM;
15 TRACE;
16 ENV;
17 CLOCK;
18 CALLSITE;
19
20 DECLARATION;
21 SV_DECLARATION;
22 TYPE_SPECIFIER_LIST;
23 TYPE_DECLARATOR_LIST;
24 TYPE_DECLARATOR;
25
26 STRUCT;
27 STRUCT_NAME;
28 STRUCT_BODY;
29 ALIGN;
30
31 CTF_EXPRESSION_TYPE;
32 CTF_EXPRESSION_VAL;
33 CTF_LEFT;
34 CTF_RIGHT;
35
36 UNARY_EXPRESSION_STRING;
37 UNARY_EXPRESSION_STRING_QUOTES;
38 UNARY_EXPRESSION_DEC;
39 UNARY_EXPRESSION_HEX;
40 UNARY_EXPRESSION_OCT;
41 LENGTH;
42
43 TYPEDEF;
44
45 TYPEALIAS;
46 TYPEALIAS_TARGET;
47 TYPEALIAS_ALIAS;
48
49 INTEGER;
50 STRING;
51 FLOATING_POINT;
52
53 ENUM;
54 ENUM_CONTAINER_TYPE;
55 ENUM_ENUMERATOR;
56 ENUM_NAME;
57 ENUM_VALUE;
58 ENUM_VALUE_RANGE;
59 ENUM_BODY;
60
61 VARIANT;
62 VARIANT_NAME;
63 VARIANT_TAG;
64 VARIANT_BODY;
65
66 DECLARATOR;
67 LENGTH;
866e5b51
FC
68}
69
70/*
71 * Scope for the tracking of types.
72 * For now we just track the names (it's a simple Set), but
73 * later we will have to track the info about the target type.
74 */
75scope Symbols {
91fd06f5 76 Set<String> types;
866e5b51
FC
77}
78
79@header {
9ad3c628
AM
80/*******************************************************************************
81 * Copyright (c) 2010, 2013 Ericsson, Ecole Polytechnique de Montréal and others
82 *
83 * All rights reserved. This program and the accompanying materials are
84 * made available under the terms of the Eclipse Public License v1.0 which
85 * accompanies this distribution, and is available at
86 * http://www.eclipse.org/legal/epl-v10.html
87 *
88 * Contributors:
89 * Matthew Khouzam - Initial API and implementation
90 * Simon Marchi - Initial API and implementation
91 * Etienne Bergeron - Update to Antlr 3.5 syntax
92 *******************************************************************************/
93
94package org.eclipse.tracecompass.ctf.parser;
95
96import java.util.Set;
97import java.util.HashSet;
866e5b51
FC
98}
99
100@members {
91fd06f5
EB
101 public CTFParser(TokenStream input, boolean verbose) {
102 this(input);
103 this.verbose = verbose;
866e5b51 104 }
91fd06f5 105
32e3cfd4
EB
106 /**
107 * This method is overriden to disable automatic error recovery.
108 * On a mismatched token, it simply re-throw an exception.
109 */
91fd06f5
EB
110 @Override
111 protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException {
112 throw new MismatchedTokenException(ttype, input);
113 }
114
115 /**
116 * Checks if a given name has been defined has a type.
117 * From: http://www.antlr.org/grammar/1153358328744/C.g
118 *
119 * @param name The name to check.
120 * @return True if is is a type, false otherwise.
121 */
122 boolean isTypeName(String name) {
123 for (int i = Symbols_stack.size() - 1; i >= 0; i--) {
124 Symbols_scope scope = (Symbols_scope) Symbols_stack.get(i);
125 if (scope.types.contains(name)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 void addTypeName(String name) {
133 $Symbols::types.add(name);
134 if (verbose) {
135 debug_print("New type: " + name);
136 }
137 }
138
139 boolean _inTypedef = false;
140
141 void typedefOn() {
142 debug_print("typedefOn");
143 _inTypedef = true;
144 }
145
146 void typedefOff() {
147 debug_print("typedefOff");
148 _inTypedef = false;
149 }
150
151 boolean inTypedef() {
152 return _inTypedef;
153 }
154
155 boolean _inTypealiasAlias = false;
156
157 void typealiasAliasOn() {
158 debug_print("typealiasAliasOn");
159 _inTypealiasAlias = true;
866e5b51 160 }
91fd06f5
EB
161
162 void typealiasAliasOff() {
163 debug_print("typealiasAliasOff");
164 _inTypealiasAlias = false;
165 }
166
167 boolean inTypealiasAlias() {
168 return _inTypealiasAlias;
169 }
170
91fd06f5
EB
171 void debug_print(String str) {
172 if (verbose) {
173 System.out.println(str);
174 }
175 }
176
91fd06f5
EB
177 /* Prints rule entry and exit while parsing */
178 boolean verbose = false;
866e5b51
FC
179}
180
32e3cfd4
EB
181 /*
182 * Override the catch clause to disable automatic error recovery.
183 * By default, the catch block of every rule simple rethrows the error.
184 */
866e5b51 185@rulecatch {
91fd06f5
EB
186 catch (RecognitionException e) {
187 throw e;
188 }
866e5b51
FC
189}
190
191/* The top-level rule. */
192parse
193scope Symbols;
194@init {
91fd06f5 195 $Symbols::types = new HashSet<String>();
866e5b51 196}
91fd06f5 197 : declaration+ EOF -> ^(ROOT declaration+)
866e5b51
FC
198 ;
199
200numberLiteral
32e3cfd4
EB
201 : SIGN*
202 ( HEX_LITERAL -> ^(UNARY_EXPRESSION_HEX HEX_LITERAL SIGN*)
203 | DECIMAL_LITERAL -> ^(UNARY_EXPRESSION_DEC DECIMAL_LITERAL SIGN*)
204 | OCTAL_LITERAL -> ^(UNARY_EXPRESSION_OCT OCTAL_LITERAL SIGN*)
205 )
866e5b51
FC
206 ;
207
208primaryExpression
91fd06f5 209 : (IDENTIFIER) => IDENTIFIER
91fd06f5 210 -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
866e5b51 211 | (ctfKeyword) => ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
91fd06f5 212 | (STRING_LITERAL) => STRING_LITERAL
91fd06f5 213 -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
866e5b51 214 /*| (LPAREN unaryExpression RPAREN)*/ // Not supported yet
32e3cfd4
EB
215 | numberLiteral
216 | enumConstant
217 | CHARACTER_LITERAL
866e5b51
FC
218 ;
219
220postfixExpressionSuffix
32e3cfd4
EB
221 : OPENBRAC unaryExpression CLOSEBRAC!
222 | (ref=DOT | ref=ARROW) IDENTIFIER
223 -> ^($ref ^(UNARY_EXPRESSION_STRING IDENTIFIER))
866e5b51
FC
224 ;
225
226postfixExpression
32e3cfd4
EB
227 : primaryExpression postfixExpressionSuffix*
228 | ctfSpecifierHead postfixExpressionSuffix+ // added for ctf-v1.8
866e5b51
FC
229 ;
230
231unaryExpression
91fd06f5
EB
232 : postfixExpression
233 /* | ((SIGN postfixExpression[true]) | postfixExpression[false]) */
866e5b51
FC
234 ;
235
236enumConstant
91fd06f5
EB
237 : STRING_LITERAL -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
238 | IDENTIFIER -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
239 | ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
866e5b51 240 ;
32e3cfd4 241
866e5b51
FC
242// 2.2
243
244declaration
866e5b51 245@after {
91fd06f5
EB
246 if (inTypedef()) {
247 typedefOff();
248 }
866e5b51 249}
32e3cfd4 250 : declarationSpecifiers declaratorList? TERM
91fd06f5
EB
251 // When the declaration is completely parsed and was a typedef,
252 // we add the declarators to the symbol table.
32e3cfd4
EB
253 -> {inTypedef()}?
254 ^(DECLARATION ^(TYPEDEF declaratorList declarationSpecifiers))
91fd06f5 255 -> ^(DECLARATION declarationSpecifiers declaratorList?)
32e3cfd4 256 | ctfSpecifier TERM!
866e5b51
FC
257 ;
258
259declarationSpecifiers
91fd06f5
EB
260 : (
261 // We don't want to keep the typedef keyword in the specifier list.
262 // Instead, we keep track that we encountered a typedef in the declaration.
263 storageClassSpecifier
264 | typeQualifier
265 | typeSpecifier
866e5b51
FC
266 )+ -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
267 ;
268
269declaratorList
32e3cfd4
EB
270 : declarator (SEPARATOR declarator)*
271 -> ^(TYPE_DECLARATOR_LIST declarator+)
866e5b51
FC
272 ;
273
274abstractDeclaratorList
91fd06f5
EB
275 : abstractDeclarator (SEPARATOR abstractDeclarator)*
276 -> ^(TYPE_DECLARATOR_LIST abstractDeclarator+)
866e5b51
FC
277 ;
278
91fd06f5
EB
279storageClassSpecifier
280 : TYPEDEFTOK { typedefOn(); }
866e5b51
FC
281 ;
282
283typeSpecifier
91fd06f5 284 : FLOATTOK
866e5b51
FC
285 | INTTOK
286 | LONGTOK
287 | SHORTTOK
288 | SIGNEDTOK
289 | UNSIGNEDTOK
290 | CHARTOK
291 | DOUBLETOK
292 | VOIDTOK
293 | BOOLTOK
294 | COMPLEXTOK
295 | IMAGINARYTOK
296 | structSpecifier
297 | variantSpecifier
298 | enumSpecifier
299 | ctfTypeSpecifier
91fd06f5 300 | { inTypealiasAlias() || isTypeName(input.LT(1).getText()) }? => typedefName
866e5b51
FC
301 ;
302
303typeQualifier
91fd06f5 304 : CONSTTOK
866e5b51
FC
305 ;
306
91fd06f5
EB
307alignAttribute
308 : ALIGNTOK LPAREN unaryExpression RPAREN -> ^(ALIGN unaryExpression)
866e5b51 309 ;
91fd06f5 310
866e5b51
FC
311 // you can have an empty struct but not an empty variant
312structBody
313scope Symbols;
314@init {
91fd06f5 315 $Symbols::types = new HashSet<String>();
866e5b51 316}
91fd06f5
EB
317 : LCURL structOrVariantDeclarationList? RCURL
318 -> ^(STRUCT_BODY structOrVariantDeclarationList?)
866e5b51 319 ;
866e5b51 320
866e5b51 321structSpecifier
91fd06f5 322 : STRUCTTOK
866e5b51
FC
323 (
324 // We have an IDENTIFIER after 'struct'
325 (
91fd06f5
EB
326 structName
327 (
328 alignAttribute
329 |
330 (
331 structBody
332 ( /* structBody can return an empty tree, so we need those ? */
333 alignAttribute
334 |
335 /* empty */
336 )
337 )
338 |
339 /* empty */
340 )
866e5b51
FC
341 )
342 |
343 // We have a body after 'struct'
344 (
345 structBody
346 (
347 alignAttribute
348 |
349 /* empty */
350 )
351 )
352 ) -> ^(STRUCT structName? structBody? alignAttribute?)
353 ;
354
355structName
91fd06f5 356 : IDENTIFIER -> ^(STRUCT_NAME IDENTIFIER)
866e5b51
FC
357 ;
358
359structOrVariantDeclarationList
91fd06f5 360 : structOrVariantDeclaration+
866e5b51
FC
361 ;
362
363structOrVariantDeclaration
32e3cfd4 364 :
91fd06f5
EB
365 (
366 (
367 declarationSpecifiers
368 (
369 /* If we met a "typedef" */
370 {inTypedef()}? => declaratorList {typedefOff();}
371 -> ^(TYPEDEF declaratorList declarationSpecifiers)
372 | structOrVariantDeclaratorList
373 -> ^(SV_DECLARATION declarationSpecifiers structOrVariantDeclaratorList)
374 )
375 )
866e5b51 376 |
91fd06f5 377 // Lines 3 and 4
866e5b51
FC
378 typealiasDecl -> typealiasDecl
379 )
380 TERM
381 ;
382
383specifierQualifierList
91fd06f5
EB
384 : (typeQualifier | typeSpecifier)+
385 -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
866e5b51
FC
386 ;
387
388structOrVariantDeclaratorList
91fd06f5
EB
389 : structOrVariantDeclarator (SEPARATOR structOrVariantDeclarator)*
390 -> ^(TYPE_DECLARATOR_LIST structOrVariantDeclarator+)
866e5b51
FC
391 ;
392
393structOrVariantDeclarator
91fd06f5 394 :
866e5b51
FC
395 /* Bitfields not supported yet */
396 (declarator (COLON numberLiteral)?) -> declarator
397 /*| (COLON numberLiteral)*/
398 ;
399
400variantSpecifier
32e3cfd4 401 : VARIANTTOK
866e5b51
FC
402 (
403 (
91fd06f5 404 variantName
866e5b51
FC
405 (
406 (
407 variantTag
408 (
409 variantBody
410 |
411 /* empty */
412 )
413 )
414 |
415 variantBody
416 )
417 )
32e3cfd4
EB
418 | (variantTag variantBody)
419 | variantBody
866e5b51
FC
420 ) -> ^(VARIANT variantName? variantTag? variantBody?)
421 ;
422
423variantName
91fd06f5 424 : IDENTIFIER -> ^(VARIANT_NAME IDENTIFIER)
866e5b51
FC
425 ;
426
427variantBody
428scope Symbols;
429@init {
91fd06f5 430 $Symbols::types = new HashSet<String>();
866e5b51 431}
91fd06f5
EB
432 : LCURL structOrVariantDeclarationList RCURL
433 -> ^(VARIANT_BODY structOrVariantDeclarationList)
866e5b51
FC
434 ;
435
436variantTag
91fd06f5 437 : LT IDENTIFIER GT -> ^(VARIANT_TAG IDENTIFIER)
866e5b51
FC
438 ;
439
440enumSpecifier
32e3cfd4 441 : ENUMTOK
91fd06f5
EB
442 (
443 // Lines 1 to 5, when we have "ENUMTOK IDENTIFIER".
444 (
445 enumName
446 (
447 enumContainerType enumBody
448 |
449 enumBody
450 |
451 // no enumDeclarator or enumBodym
452 )
453 )
454 |
455 // Lines 1, 2, 4, 5, when we have no IDENTIFIER.
456 (
457 enumContainerType enumBody
458 |
459 enumBody
460 )
461 ) -> ^(ENUM enumName? enumContainerType? enumBody?)
866e5b51
FC
462 ;
463
464enumName
32e3cfd4 465 : IDENTIFIER -> ^(ENUM_NAME IDENTIFIER)
866e5b51 466 ;
91fd06f5 467
866e5b51 468enumBody
32e3cfd4 469 : LCURL enumeratorList SEPARATOR? RCURL -> ^(ENUM_BODY enumeratorList)
866e5b51
FC
470 ;
471
472enumContainerType
91fd06f5 473 : COLON declarationSpecifiers -> ^(ENUM_CONTAINER_TYPE declarationSpecifiers)
866e5b51
FC
474 ;
475
476enumeratorList
91fd06f5 477 : enumerator (SEPARATOR enumerator)* -> (^(ENUM_ENUMERATOR enumerator))+
866e5b51
FC
478 ;
479
480enumerator
91fd06f5 481 : enumConstant enumeratorValue?
866e5b51
FC
482 ;
483
484enumeratorValue
91fd06f5 485 : ASSIGNMENT e1=unaryExpression
32e3cfd4
EB
486 ( /* empty */
487 -> ^(ENUM_VALUE $e1)
488 | ELIPSES e2=unaryExpression
489 -> ^(ENUM_VALUE_RANGE $e1 $e2)
490 )
866e5b51 491 ;
91fd06f5 492
866e5b51 493declarator
32e3cfd4
EB
494 : pointer* directDeclarator
495 -> ^(TYPE_DECLARATOR pointer* directDeclarator)
866e5b51
FC
496 ;
497
498directDeclarator
91fd06f5
EB
499 : (
500 IDENTIFIER
501 { if (inTypedef()) addTypeName($IDENTIFIER.text); }
502 { debug_print($IDENTIFIER.text); }
503 /*| LPAREN declarator RPAREN*/ /* Not supported yet */
504 )
505 directDeclaratorSuffix*
506 ;
507
508directDeclaratorSuffix
32e3cfd4
EB
509 : OPENBRAC directDeclaratorLength CLOSEBRAC
510 -> ^(LENGTH directDeclaratorLength)
91fd06f5
EB
511 ;
512
513directDeclaratorLength
514 : unaryExpression
515 ;
516
866e5b51 517abstractDeclarator
32e3cfd4 518 : pointer+ directAbstractDeclarator?
91fd06f5
EB
519 -> ^(TYPE_DECLARATOR pointer+ directAbstractDeclarator?)
520 | directAbstractDeclarator
521 -> ^(TYPE_DECLARATOR directAbstractDeclarator)
866e5b51
FC
522 ;
523
32e3cfd4
EB
524/**
525 * In the CTF grammar, direct-abstract-declarator can be empty (because of
526 * identifier-opt). We take care of that by appending a '?' to each use of
527 * "abstractDeclaratorList".
528 */
866e5b51 529directAbstractDeclarator
91fd06f5
EB
530 : (
531 IDENTIFIER
532 | (LPAREN abstractDeclarator RPAREN)
533 ) (
534 OPENBRAC unaryExpression? CLOSEBRAC
866e5b51
FC
535 )?
536 ;
537
538pointer
91fd06f5 539 : POINTER typeQualifierList? -> ^(POINTER typeQualifierList?)
866e5b51
FC
540 ;
541
91fd06f5
EB
542typeQualifierList
543 : typeQualifier+
866e5b51
FC
544 ;
545
546typedefName
32e3cfd4 547 : {inTypealiasAlias() || isTypeName(input.LT(1).getText())}? IDENTIFIER { if ((inTypedef() || inTypealiasAlias()) && !isTypeName($IDENTIFIER.text)) { addTypeName($IDENTIFIER.text); } }
866e5b51
FC
548 ;
549
550/**
551 * What goes in the target part of a typealias.
552 *
553 * For example, the integer part in:
91fd06f5
EB
554 * typealias integer {...} := my_new_integer;
555 */
866e5b51 556typealiasTarget
91fd06f5 557 : declarationSpecifiers abstractDeclaratorList?
866e5b51 558 ;
91fd06f5 559
866e5b51
FC
560/**
561 * What goes in the alias part of a typealias.
562 *
563 * For example, the my_new_integer part in:
91fd06f5
EB
564 * typealias integer {...} := my_new_integer;
565 */
866e5b51
FC
566typealiasAlias
567@init {
91fd06f5 568 typealiasAliasOn();
866e5b51
FC
569}
570@after {
91fd06f5 571 typealiasAliasOff();
866e5b51 572}
32e3cfd4
EB
573 : abstractDeclaratorList
574 | declarationSpecifiers abstractDeclaratorList?
866e5b51 575 ;
91fd06f5 576
866e5b51 577typealiasDecl
91fd06f5 578 : TYPEALIASTOK typealiasTarget TYPE_ASSIGNMENT typealiasAlias
32e3cfd4
EB
579 -> ^(TYPEALIAS
580 ^(TYPEALIAS_TARGET typealiasTarget)
581 ^(TYPEALIAS_ALIAS typealiasAlias))
866e5b51
FC
582 ;
583
584// 2.3 CTF stuff
585
586// TODO: Ajouter ceux qui manquent
587ctfKeyword
91fd06f5 588 : ALIGNTOK
866e5b51
FC
589 | EVENTTOK
590 | SIGNEDTOK
591 | STRINGTOK
592 ;
593
594ctfSpecifier
91fd06f5
EB
595 // event {...}, stream {...}, trace {...}
596 : ctfSpecifierHead ctfBody -> ^(ctfSpecifierHead ctfBody)
866e5b51 597 // typealias
91fd06f5 598 | typealiasDecl -> ^(DECLARATION typealiasDecl)
866e5b51
FC
599 ;
600
601ctfSpecifierHead
91fd06f5
EB
602 : EVENTTOK -> EVENT
603 | STREAMTOK -> STREAM
604 | TRACETOK -> TRACE
605 | ENVTOK -> ENV
606 | CLOCKTOK -> CLOCK
607 | CALLSITETOK -> CALLSITE
866e5b51
FC
608 ;
609
610ctfTypeSpecifier
91fd06f5
EB
611 /* ctfBody can return an empty tree if the body is empty */
612 : FLOATINGPOINTTOK ctfBody -> ^(FLOATING_POINT ctfBody?)
866e5b51
FC
613 | INTEGERTOK ctfBody -> ^(INTEGER ctfBody?)
614 | STRINGTOK ctfBody? -> ^(STRING ctfBody?)
615 ;
616
617ctfBody
618scope Symbols;
619@init {
91fd06f5 620 $Symbols::types = new HashSet<String>();
866e5b51 621}
91fd06f5 622 : LCURL ctfAssignmentExpressionList? RCURL -> ctfAssignmentExpressionList?
866e5b51
FC
623 ;
624
91fd06f5
EB
625ctfAssignmentExpressionList
626 : (ctfAssignmentExpression TERM!)+
866e5b51
FC
627 ;
628
866e5b51 629ctfAssignmentExpression
866e5b51 630@after {
91fd06f5
EB
631 if (inTypedef()) {
632 typedefOff();
633 }
866e5b51 634}
32e3cfd4
EB
635 : left=unaryExpression
636 ( assignment=ASSIGNMENT right1=unaryExpression
637 -> ^(CTF_EXPRESSION_VAL
638 ^(CTF_LEFT $left)
639 ^(CTF_RIGHT $right1))
640 | type_assignment=TYPE_ASSIGNMENT right2=typeSpecifier
641 -> ^(CTF_EXPRESSION_TYPE
642 ^(CTF_LEFT $left)
643 ^(CTF_RIGHT ^(TYPE_SPECIFIER_LIST $right2)))
644 )
645 | (declarationSpecifiers {inTypedef()}? declaratorList)
646 -> ^(TYPEDEF declaratorList declarationSpecifiers)
647 | typealiasDecl
866e5b51 648 ;
This page took 0.163542 seconds and 5 git commands to generate.