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 package org.eclipse.linuxtools.ctf.parser;
82 import java.util.HashSet;
86 public CTFParser(TokenStream input, boolean verbose) {
88 this.verbose = verbose;
92 * This method is overriden to disable automatic error recovery.
93 * On a mismatched token, it simply re-throw an exception.
96 protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException {
97 throw new MismatchedTokenException(ttype, input);
101 * Checks if a given name has been defined has a type.
102 * From: http://www.antlr.org/grammar/1153358328744/C.g
104 * @param name The name to check.
105 * @return True if is is a type, false otherwise.
107 boolean isTypeName(String name) {
108 for (int i = Symbols_stack.size() - 1; i >= 0; i--) {
109 Symbols_scope scope = (Symbols_scope) Symbols_stack.get(i);
110 if (scope.types.contains(name)) {
117 void addTypeName(String name) {
118 $Symbols::types.add(name);
120 debug_print("New type: " + name);
124 boolean _inTypedef = false;
127 debug_print("typedefOn");
132 debug_print("typedefOff");
136 boolean inTypedef() {
140 boolean _inTypealiasAlias = false;
142 void typealiasAliasOn() {
143 debug_print("typealiasAliasOn");
144 _inTypealiasAlias = true;
147 void typealiasAliasOff() {
148 debug_print("typealiasAliasOff");
149 _inTypealiasAlias = false;
152 boolean inTypealiasAlias() {
153 return _inTypealiasAlias;
156 void debug_print(String str) {
158 System.out.println(str);
162 /* Prints rule entry and exit while parsing */
163 boolean verbose = false;
167 * Override the catch clause to disable automatic error recovery.
168 * By default, the catch block of every rule simple rethrows the error.
171 catch (RecognitionException e) {
176 /* The top-level rule. */
180 $Symbols::types = new HashSet<String>();
182 : declaration+ EOF -> ^(ROOT declaration+)
187 ( HEX_LITERAL -> ^(UNARY_EXPRESSION_HEX HEX_LITERAL SIGN*)
188 | DECIMAL_LITERAL -> ^(UNARY_EXPRESSION_DEC DECIMAL_LITERAL SIGN*)
189 | OCTAL_LITERAL -> ^(UNARY_EXPRESSION_OCT OCTAL_LITERAL SIGN*)
194 : (IDENTIFIER) => IDENTIFIER
195 -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
196 | (ctfKeyword) => ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
197 | (STRING_LITERAL) => STRING_LITERAL
198 -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
199 /*| (LPAREN unaryExpression RPAREN)*/ // Not supported yet
205 postfixExpressionSuffix
206 : OPENBRAC unaryExpression CLOSEBRAC!
207 | (ref=DOT | ref=ARROW) IDENTIFIER
208 -> ^($ref ^(UNARY_EXPRESSION_STRING IDENTIFIER))
212 : primaryExpression postfixExpressionSuffix*
213 | ctfSpecifierHead postfixExpressionSuffix+ // added for ctf-v1.8
218 /* | ((SIGN postfixExpression[true]) | postfixExpression[false]) */
222 : STRING_LITERAL -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
223 | IDENTIFIER -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
224 | ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
235 : declarationSpecifiers declaratorList? TERM
236 // When the declaration is completely parsed and was a typedef,
237 // we add the declarators to the symbol table.
239 ^(DECLARATION ^(TYPEDEF declaratorList declarationSpecifiers))
240 -> ^(DECLARATION declarationSpecifiers declaratorList?)
244 declarationSpecifiers
246 // We don't want to keep the typedef keyword in the specifier list.
247 // Instead, we keep track that we encountered a typedef in the declaration.
248 storageClassSpecifier
251 )+ -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
255 : declarator (SEPARATOR declarator)*
256 -> ^(TYPE_DECLARATOR_LIST declarator+)
259 abstractDeclaratorList
260 : abstractDeclarator (SEPARATOR abstractDeclarator)*
261 -> ^(TYPE_DECLARATOR_LIST abstractDeclarator+)
264 storageClassSpecifier
265 : TYPEDEFTOK { typedefOn(); }
285 | { inTypealiasAlias() || isTypeName(input.LT(1).getText()) }? => typedefName
293 : ALIGNTOK LPAREN unaryExpression RPAREN -> ^(ALIGN unaryExpression)
296 // you can have an empty struct but not an empty variant
300 $Symbols::types = new HashSet<String>();
302 : LCURL structOrVariantDeclarationList? RCURL
303 -> ^(STRUCT_BODY structOrVariantDeclarationList?)
309 // We have an IDENTIFIER after 'struct'
317 ( /* structBody can return an empty tree, so we need those ? */
328 // We have a body after 'struct'
337 ) -> ^(STRUCT structName? structBody? alignAttribute?)
341 : IDENTIFIER -> ^(STRUCT_NAME IDENTIFIER)
344 structOrVariantDeclarationList
345 : structOrVariantDeclaration+
348 structOrVariantDeclaration
352 declarationSpecifiers
354 /* If we met a "typedef" */
355 {inTypedef()}? => declaratorList {typedefOff();}
356 -> ^(TYPEDEF declaratorList declarationSpecifiers)
357 | structOrVariantDeclaratorList
358 -> ^(SV_DECLARATION declarationSpecifiers structOrVariantDeclaratorList)
363 typealiasDecl -> typealiasDecl
368 specifierQualifierList
369 : (typeQualifier | typeSpecifier)+
370 -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
373 structOrVariantDeclaratorList
374 : structOrVariantDeclarator (SEPARATOR structOrVariantDeclarator)*
375 -> ^(TYPE_DECLARATOR_LIST structOrVariantDeclarator+)
378 structOrVariantDeclarator
380 /* Bitfields not supported yet */
381 (declarator (COLON numberLiteral)?) -> declarator
382 /*| (COLON numberLiteral)*/
403 | (variantTag variantBody)
405 ) -> ^(VARIANT variantName? variantTag? variantBody?)
409 : IDENTIFIER -> ^(VARIANT_NAME IDENTIFIER)
415 $Symbols::types = new HashSet<String>();
417 : LCURL structOrVariantDeclarationList RCURL
418 -> ^(VARIANT_BODY structOrVariantDeclarationList)
422 : LT IDENTIFIER GT -> ^(VARIANT_TAG IDENTIFIER)
428 // Lines 1 to 5, when we have "ENUMTOK IDENTIFIER".
432 enumContainerType enumBody
436 // no enumDeclarator or enumBodym
440 // Lines 1, 2, 4, 5, when we have no IDENTIFIER.
442 enumContainerType enumBody
446 ) -> ^(ENUM enumName? enumContainerType? enumBody?)
450 : IDENTIFIER -> ^(ENUM_NAME IDENTIFIER)
454 : LCURL enumeratorList SEPARATOR? RCURL -> ^(ENUM_BODY enumeratorList)
458 : COLON declarationSpecifiers -> ^(ENUM_CONTAINER_TYPE declarationSpecifiers)
462 : enumerator (SEPARATOR enumerator)* -> (^(ENUM_ENUMERATOR enumerator))+
466 : enumConstant enumeratorValue?
470 : ASSIGNMENT e1=unaryExpression
473 | ELIPSES e2=unaryExpression
474 -> ^(ENUM_VALUE_RANGE $e1 $e2)
479 : pointer* directDeclarator
480 -> ^(TYPE_DECLARATOR pointer* directDeclarator)
486 { if (inTypedef()) addTypeName($IDENTIFIER.text); }
487 { debug_print($IDENTIFIER.text); }
488 /*| LPAREN declarator RPAREN*/ /* Not supported yet */
490 directDeclaratorSuffix*
493 directDeclaratorSuffix
494 : OPENBRAC directDeclaratorLength CLOSEBRAC
495 -> ^(LENGTH directDeclaratorLength)
498 directDeclaratorLength
503 : pointer+ directAbstractDeclarator?
504 -> ^(TYPE_DECLARATOR pointer+ directAbstractDeclarator?)
505 | directAbstractDeclarator
506 -> ^(TYPE_DECLARATOR directAbstractDeclarator)
510 * In the CTF grammar, direct-abstract-declarator can be empty (because of
511 * identifier-opt). We take care of that by appending a '?' to each use of
512 * "abstractDeclaratorList".
514 directAbstractDeclarator
517 | (LPAREN abstractDeclarator RPAREN)
519 OPENBRAC unaryExpression? CLOSEBRAC
524 : POINTER typeQualifierList? -> ^(POINTER typeQualifierList?)
532 : {inTypealiasAlias() || isTypeName(input.LT(1).getText())}? IDENTIFIER { if ((inTypedef() || inTypealiasAlias()) && !isTypeName($IDENTIFIER.text)) { addTypeName($IDENTIFIER.text); } }
536 * What goes in the target part of a typealias.
538 * For example, the integer part in:
539 * typealias integer {...} := my_new_integer;
542 : declarationSpecifiers abstractDeclaratorList?
546 * What goes in the alias part of a typealias.
548 * For example, the my_new_integer part in:
549 * typealias integer {...} := my_new_integer;
558 : abstractDeclaratorList
559 | declarationSpecifiers abstractDeclaratorList?
563 : TYPEALIASTOK typealiasTarget TYPE_ASSIGNMENT typealiasAlias
565 ^(TYPEALIAS_TARGET typealiasTarget)
566 ^(TYPEALIAS_ALIAS typealiasAlias))
571 // TODO: Ajouter ceux qui manquent
580 // event {...}, stream {...}, trace {...}
581 : ctfSpecifierHead ctfBody -> ^(ctfSpecifierHead ctfBody)
583 | typealiasDecl -> ^(DECLARATION typealiasDecl)
588 | STREAMTOK -> STREAM
592 | CALLSITETOK -> CALLSITE
596 /* ctfBody can return an empty tree if the body is empty */
597 : FLOATINGPOINTTOK ctfBody -> ^(FLOATING_POINT ctfBody?)
598 | INTEGERTOK ctfBody -> ^(INTEGER ctfBody?)
599 | STRINGTOK ctfBody? -> ^(STRING ctfBody?)
605 $Symbols::types = new HashSet<String>();
607 : LCURL ctfAssignmentExpressionList? RCURL -> ctfAssignmentExpressionList?
610 ctfAssignmentExpressionList
611 : (ctfAssignmentExpression TERM!)+
614 ctfAssignmentExpression
620 : left=unaryExpression
621 ( assignment=ASSIGNMENT right1=unaryExpression
622 -> ^(CTF_EXPRESSION_VAL
624 ^(CTF_RIGHT $right1))
625 | type_assignment=TYPE_ASSIGNMENT right2=typeSpecifier
626 -> ^(CTF_EXPRESSION_TYPE
628 ^(CTF_RIGHT ^(TYPE_SPECIFIER_LIST $right2)))
630 | (declarationSpecifiers {inTypedef()}? declaratorList)
631 -> ^(TYPEDEF declaratorList declarationSpecifiers)