1 parser grammar CTFParser;
6 ASTLabelType = CommonTree;
36 UNARY_EXPRESSION_STRING;
37 UNARY_EXPRESSION_STRING_QUOTES;
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.
80 /*******************************************************************************
81 * Copyright (c) 2010, 2013 Ericsson, Ecole Polytechnique de Montréal and others
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
89 * Matthew Khouzam - Initial API and implementation
90 * Simon Marchi - Initial API and implementation
91 * Etienne Bergeron - Update to Antlr 3.5 syntax
92 *******************************************************************************/
94 package org.eclipse.tracecompass.ctf.parser;
97 import java.util.HashSet;
101 public CTFParser(TokenStream input, boolean verbose) {
103 this.verbose = verbose;
107 * This method is overriden to disable automatic error recovery.
108 * On a mismatched token, it simply re-throw an exception.
111 protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException {
112 throw new MismatchedTokenException(ttype, input);
116 * Checks if a given name has been defined has a type.
117 * From: http://www.antlr.org/grammar/1153358328744/C.g
119 * @param name The name to check.
120 * @return True if is is a type, false otherwise.
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)) {
132 void addTypeName(String name) {
133 $Symbols::types.add(name);
135 debug_print("New type: " + name);
139 boolean _inTypedef = false;
142 debug_print("typedefOn");
147 debug_print("typedefOff");
151 boolean inTypedef() {
155 boolean _inTypealiasAlias = false;
157 void typealiasAliasOn() {
158 debug_print("typealiasAliasOn");
159 _inTypealiasAlias = true;
162 void typealiasAliasOff() {
163 debug_print("typealiasAliasOff");
164 _inTypealiasAlias = false;
167 boolean inTypealiasAlias() {
168 return _inTypealiasAlias;
171 void debug_print(String str) {
173 System.out.println(str);
177 /* Prints rule entry and exit while parsing */
178 boolean verbose = false;
182 * Override the catch clause to disable automatic error recovery.
183 * By default, the catch block of every rule simple rethrows the error.
186 catch (RecognitionException e) {
191 /* The top-level rule. */
195 $Symbols::types = new HashSet<String>();
197 : declaration+ EOF -> ^(ROOT declaration+)
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*)
209 : (IDENTIFIER) => IDENTIFIER
210 -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
211 | (ctfKeyword) => ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
212 | (STRING_LITERAL) => STRING_LITERAL
213 -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
214 /*| (LPAREN unaryExpression RPAREN)*/ // Not supported yet
220 postfixExpressionSuffix
221 : OPENBRAC unaryExpression CLOSEBRAC!
222 | (ref=DOT | ref=ARROW) IDENTIFIER
223 -> ^($ref ^(UNARY_EXPRESSION_STRING IDENTIFIER))
227 : primaryExpression postfixExpressionSuffix*
228 | ctfSpecifierHead postfixExpressionSuffix+ // added for ctf-v1.8
233 /* | ((SIGN postfixExpression[true]) | postfixExpression[false]) */
237 : STRING_LITERAL -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
238 | IDENTIFIER -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
239 | ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
250 : declarationSpecifiers declaratorList? TERM
251 // When the declaration is completely parsed and was a typedef,
252 // we add the declarators to the symbol table.
254 ^(DECLARATION ^(TYPEDEF declaratorList declarationSpecifiers))
255 -> ^(DECLARATION declarationSpecifiers declaratorList?)
259 declarationSpecifiers
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
266 )+ -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
270 : declarator (SEPARATOR declarator)*
271 -> ^(TYPE_DECLARATOR_LIST declarator+)
274 abstractDeclaratorList
275 : abstractDeclarator (SEPARATOR abstractDeclarator)*
276 -> ^(TYPE_DECLARATOR_LIST abstractDeclarator+)
279 storageClassSpecifier
280 : TYPEDEFTOK { typedefOn(); }
300 | { inTypealiasAlias() || isTypeName(input.LT(1).getText()) }? => typedefName
308 : ALIGNTOK LPAREN unaryExpression RPAREN -> ^(ALIGN unaryExpression)
311 // you can have an empty struct but not an empty variant
315 $Symbols::types = new HashSet<String>();
317 : LCURL structOrVariantDeclarationList? RCURL
318 -> ^(STRUCT_BODY structOrVariantDeclarationList?)
324 // We have an IDENTIFIER after 'struct'
332 ( /* structBody can return an empty tree, so we need those ? */
343 // We have a body after 'struct'
352 ) -> ^(STRUCT structName? structBody? alignAttribute?)
356 : IDENTIFIER -> ^(STRUCT_NAME IDENTIFIER)
359 structOrVariantDeclarationList
360 : structOrVariantDeclaration+
363 structOrVariantDeclaration
367 declarationSpecifiers
369 /* If we met a "typedef" */
370 {inTypedef()}? => declaratorList {typedefOff();}
371 -> ^(TYPEDEF declaratorList declarationSpecifiers)
372 | structOrVariantDeclaratorList
373 -> ^(SV_DECLARATION declarationSpecifiers structOrVariantDeclaratorList)
378 typealiasDecl -> typealiasDecl
383 specifierQualifierList
384 : (typeQualifier | typeSpecifier)+
385 -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
388 structOrVariantDeclaratorList
389 : structOrVariantDeclarator (SEPARATOR structOrVariantDeclarator)*
390 -> ^(TYPE_DECLARATOR_LIST structOrVariantDeclarator+)
393 structOrVariantDeclarator
395 /* Bitfields not supported yet */
396 (declarator (COLON numberLiteral)?) -> declarator
397 /*| (COLON numberLiteral)*/
418 | (variantTag variantBody)
420 ) -> ^(VARIANT variantName? variantTag? variantBody?)
424 : IDENTIFIER -> ^(VARIANT_NAME IDENTIFIER)
430 $Symbols::types = new HashSet<String>();
432 : LCURL structOrVariantDeclarationList RCURL
433 -> ^(VARIANT_BODY structOrVariantDeclarationList)
437 : LT IDENTIFIER GT -> ^(VARIANT_TAG IDENTIFIER)
443 // Lines 1 to 5, when we have "ENUMTOK IDENTIFIER".
447 enumContainerType enumBody
451 // no enumDeclarator or enumBodym
455 // Lines 1, 2, 4, 5, when we have no IDENTIFIER.
457 enumContainerType enumBody
461 ) -> ^(ENUM enumName? enumContainerType? enumBody?)
465 : IDENTIFIER -> ^(ENUM_NAME IDENTIFIER)
469 : LCURL enumeratorList SEPARATOR? RCURL -> ^(ENUM_BODY enumeratorList)
473 : COLON declarationSpecifiers -> ^(ENUM_CONTAINER_TYPE declarationSpecifiers)
477 : enumerator (SEPARATOR enumerator)* -> (^(ENUM_ENUMERATOR enumerator))+
481 : enumConstant enumeratorValue?
485 : ASSIGNMENT e1=unaryExpression
488 | ELIPSES e2=unaryExpression
489 -> ^(ENUM_VALUE_RANGE $e1 $e2)
494 : pointer* directDeclarator
495 -> ^(TYPE_DECLARATOR pointer* directDeclarator)
501 { if (inTypedef()) addTypeName($IDENTIFIER.text); }
502 { debug_print($IDENTIFIER.text); }
503 /*| LPAREN declarator RPAREN*/ /* Not supported yet */
505 directDeclaratorSuffix*
508 directDeclaratorSuffix
509 : OPENBRAC directDeclaratorLength CLOSEBRAC
510 -> ^(LENGTH directDeclaratorLength)
513 directDeclaratorLength
518 : pointer+ directAbstractDeclarator?
519 -> ^(TYPE_DECLARATOR pointer+ directAbstractDeclarator?)
520 | directAbstractDeclarator
521 -> ^(TYPE_DECLARATOR directAbstractDeclarator)
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".
529 directAbstractDeclarator
532 | (LPAREN abstractDeclarator RPAREN)
534 OPENBRAC unaryExpression? CLOSEBRAC
539 : POINTER typeQualifierList? -> ^(POINTER typeQualifierList?)
547 : {inTypealiasAlias() || isTypeName(input.LT(1).getText())}? IDENTIFIER { if ((inTypedef() || inTypealiasAlias()) && !isTypeName($IDENTIFIER.text)) { addTypeName($IDENTIFIER.text); } }
551 * What goes in the target part of a typealias.
553 * For example, the integer part in:
554 * typealias integer {...} := my_new_integer;
557 : declarationSpecifiers abstractDeclaratorList?
561 * What goes in the alias part of a typealias.
563 * For example, the my_new_integer part in:
564 * typealias integer {...} := my_new_integer;
573 : abstractDeclaratorList
574 | declarationSpecifiers abstractDeclaratorList?
578 : TYPEALIASTOK typealiasTarget TYPE_ASSIGNMENT typealiasAlias
580 ^(TYPEALIAS_TARGET typealiasTarget)
581 ^(TYPEALIAS_ALIAS typealiasAlias))
586 // TODO: Ajouter ceux qui manquent
595 // event {...}, stream {...}, trace {...}
596 : ctfSpecifierHead ctfBody -> ^(ctfSpecifierHead ctfBody)
598 | typealiasDecl -> ^(DECLARATION typealiasDecl)
603 | STREAMTOK -> STREAM
607 | CALLSITETOK -> CALLSITE
611 /* ctfBody can return an empty tree if the body is empty */
612 : FLOATINGPOINTTOK ctfBody -> ^(FLOATING_POINT ctfBody?)
613 | INTEGERTOK ctfBody -> ^(INTEGER ctfBody?)
614 | STRINGTOK ctfBody? -> ^(STRING ctfBody?)
620 $Symbols::types = new HashSet<String>();
622 : LCURL ctfAssignmentExpressionList? RCURL -> ctfAssignmentExpressionList?
625 ctfAssignmentExpressionList
626 : (ctfAssignmentExpression TERM!)+
629 ctfAssignmentExpression
635 : left=unaryExpression
636 ( assignment=ASSIGNMENT right1=unaryExpression
637 -> ^(CTF_EXPRESSION_VAL
639 ^(CTF_RIGHT $right1))
640 | type_assignment=TYPE_ASSIGNMENT right2=typeSpecifier
641 -> ^(CTF_EXPRESSION_TYPE
643 ^(CTF_RIGHT ^(TYPE_SPECIFIER_LIST $right2)))
645 | (declarationSpecifiers {inTypedef()}? declaratorList)
646 -> ^(TYPEDEF declaratorList declarationSpecifiers)