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 + " " + $declaration);
140 debug_print("typedefOn" + $declaration);
141 $declaration::isTypedef=true;
145 debug_print("typedefOff" + $declaration);
146 $declaration::isTypedef=false;
149 boolean inTypedef() {
150 return $declaration::isTypedef;
153 boolean _inTypealiasAlias = false;
155 void typealiasAliasOn() {
156 debug_print("typealiasAliasOn");
157 _inTypealiasAlias = true;
160 void typealiasAliasOff() {
161 debug_print("typealiasAliasOff");
162 _inTypealiasAlias = false;
165 boolean inTypealiasAlias() {
166 return _inTypealiasAlias;
169 void debug_print(String str) {
171 System.out.println(str);
175 /* Prints rule entry and exit while parsing */
176 boolean verbose = false;
180 * Override the catch clause to disable automatic error recovery.
181 * By default, the catch block of every rule simple rethrows the error.
184 catch (RecognitionException e) {
189 /* The top-level rule. */
193 $Symbols::types = new HashSet<String>();
195 : declaration+ EOF -> ^(ROOT declaration+)
200 ( HEX_LITERAL -> ^(UNARY_EXPRESSION_HEX HEX_LITERAL SIGN*)
201 | DECIMAL_LITERAL -> ^(UNARY_EXPRESSION_DEC DECIMAL_LITERAL SIGN*)
202 | OCTAL_LITERAL -> ^(UNARY_EXPRESSION_OCT OCTAL_LITERAL SIGN*)
207 : (IDENTIFIER) => IDENTIFIER
208 -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
209 | (ctfKeyword) => ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
210 | (STRING_LITERAL) => STRING_LITERAL
211 -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
212 /*| (LPAREN unaryExpression RPAREN)*/ // Not supported yet
218 postfixExpressionSuffix
219 : OPENBRAC unaryExpression CLOSEBRAC!
220 | (ref=DOT | ref=ARROW) IDENTIFIER
221 -> ^($ref ^(UNARY_EXPRESSION_STRING IDENTIFIER))
225 : (ref=DOT) ctfSpecifierHead
226 -> ^($ref ^(UNARY_EXPRESSION_STRING ctfSpecifierHead))
230 : (primaryExpression postfixExpressionSuffix*)
231 | (ctfSpecifierHead postfixCtfExpression* postfixExpressionSuffix+) // added for ctf-v1.8
236 /* | ((SIGN postfixExpression[true]) | postfixExpression[false]) */
240 : STRING_LITERAL -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
241 | IDENTIFIER -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
242 | ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
254 : declarationSpecifiers declaratorList? TERM
255 // When the declaration is completely parsed and was a typedef,
256 // we add the declarators to the symbol table.
258 ^(DECLARATION ^(TYPEDEF declaratorList declarationSpecifiers))
259 -> ^(DECLARATION declarationSpecifiers declaratorList?)
263 declarationSpecifiers
265 // We don't want to keep the typedef keyword in the specifier list.
266 // Instead, we keep track that we encountered a typedef in the declaration.
267 storageClassSpecifier
270 )+ -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
274 : declarator (SEPARATOR declarator)*
275 -> ^(TYPE_DECLARATOR_LIST declarator+)
278 abstractDeclaratorList
279 : abstractDeclarator (SEPARATOR abstractDeclarator)*
280 -> ^(TYPE_DECLARATOR_LIST abstractDeclarator+)
283 storageClassSpecifier
284 : TYPEDEFTOK { typedefOn(); }
304 | { inTypealiasAlias() || isTypeName(input.LT(1).getText()) }? => typedefName
312 : ALIGNTOK LPAREN unaryExpression RPAREN -> ^(ALIGN unaryExpression)
315 // you can have an empty struct but not an empty variant
319 $Symbols::types = new HashSet<String>();
321 : LCURL structOrVariantDeclarationList? RCURL
322 -> ^(STRUCT_BODY structOrVariantDeclarationList?)
328 // We have an IDENTIFIER after 'struct'
336 ( /* structBody can return an empty tree, so we need those ? */
347 // We have a body after 'struct'
356 ) -> ^(STRUCT structName? structBody? alignAttribute?)
360 : IDENTIFIER -> ^(STRUCT_NAME IDENTIFIER)
363 structOrVariantDeclarationList
364 : structOrVariantDeclaration+
367 structOrVariantDeclaration
371 declarationSpecifiers
373 /* If we met a "typedef" */
374 {inTypedef()}? => declaratorList
375 -> ^(TYPEDEF declaratorList declarationSpecifiers)
376 | structOrVariantDeclaratorList
377 -> ^(SV_DECLARATION declarationSpecifiers structOrVariantDeclaratorList)
382 typealiasDecl -> typealiasDecl
387 specifierQualifierList
388 : (typeQualifier | typeSpecifier)+
389 -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
392 structOrVariantDeclaratorList
393 : structOrVariantDeclarator (SEPARATOR structOrVariantDeclarator)*
394 -> ^(TYPE_DECLARATOR_LIST structOrVariantDeclarator+)
397 structOrVariantDeclarator
399 /* Bitfields not supported yet */
400 (declarator (COLON numberLiteral)?) -> declarator
401 /*| (COLON numberLiteral)*/
422 | (variantTag variantBody)
424 ) -> ^(VARIANT variantName? variantTag? variantBody?)
428 : IDENTIFIER -> ^(VARIANT_NAME IDENTIFIER)
434 $Symbols::types = new HashSet<String>();
436 : LCURL structOrVariantDeclarationList RCURL
437 -> ^(VARIANT_BODY structOrVariantDeclarationList)
441 : LT IDENTIFIER GT -> ^(VARIANT_TAG IDENTIFIER)
447 // Lines 1 to 5, when we have "ENUMTOK IDENTIFIER".
451 enumContainerType enumBody
455 // no enumDeclarator or enumBodym
459 // Lines 1, 2, 4, 5, when we have no IDENTIFIER.
461 enumContainerType enumBody
465 ) -> ^(ENUM enumName? enumContainerType? enumBody?)
469 : IDENTIFIER -> ^(ENUM_NAME IDENTIFIER)
473 : LCURL enumeratorList SEPARATOR? RCURL -> ^(ENUM_BODY enumeratorList)
477 : COLON declarationSpecifiers -> ^(ENUM_CONTAINER_TYPE declarationSpecifiers)
481 : enumerator (SEPARATOR enumerator)* -> (^(ENUM_ENUMERATOR enumerator))+
485 : enumConstant enumeratorValue?
489 : ASSIGNMENT e1=unaryExpression
492 | ELIPSES e2=unaryExpression
493 -> ^(ENUM_VALUE_RANGE $e1 $e2)
498 : pointer* directDeclarator
499 -> ^(TYPE_DECLARATOR pointer* directDeclarator)
505 { if (inTypedef()) addTypeName($IDENTIFIER.text); }
506 { debug_print($IDENTIFIER.text); }
507 /*| LPAREN declarator RPAREN*/ /* Not supported yet */
509 directDeclaratorSuffix*
512 directDeclaratorSuffix
513 : OPENBRAC directDeclaratorLength CLOSEBRAC
514 -> ^(LENGTH directDeclaratorLength)
517 directDeclaratorLength
522 : pointer+ directAbstractDeclarator?
523 -> ^(TYPE_DECLARATOR pointer+ directAbstractDeclarator?)
524 | directAbstractDeclarator
525 -> ^(TYPE_DECLARATOR directAbstractDeclarator)
529 * In the CTF grammar, direct-abstract-declarator can be empty (because of
530 * identifier-opt). We take care of that by appending a '?' to each use of
531 * "abstractDeclaratorList".
533 directAbstractDeclarator
536 | (LPAREN abstractDeclarator RPAREN)
538 OPENBRAC unaryExpression? CLOSEBRAC
543 : POINTER typeQualifierList? -> ^(POINTER typeQualifierList?)
551 : {inTypealiasAlias() || isTypeName(input.LT(1).getText())}? IDENTIFIER { if ((inTypedef() || inTypealiasAlias()) && !isTypeName($IDENTIFIER.text)) { addTypeName($IDENTIFIER.text); } }
555 * What goes in the target part of a typealias.
557 * For example, the integer part in:
558 * typealias integer {...} := my_new_integer;
561 : declarationSpecifiers abstractDeclaratorList?
565 * What goes in the alias part of a typealias.
567 * For example, the my_new_integer part in:
568 * typealias integer {...} := my_new_integer;
577 : abstractDeclaratorList
578 | declarationSpecifiers abstractDeclaratorList?
582 : TYPEALIASTOK typealiasTarget TYPE_ASSIGNMENT typealiasAlias
584 ^(TYPEALIAS_TARGET typealiasTarget)
585 ^(TYPEALIAS_ALIAS typealiasAlias))
590 // TODO: Ajouter ceux qui manquent
599 // event {...}, stream {...}, trace {...}
600 : ctfSpecifierHead ctfBody -> ^(ctfSpecifierHead ctfBody)
602 | typealiasDecl -> ^(DECLARATION typealiasDecl)
607 | STREAMTOK -> STREAM
611 | CALLSITETOK -> CALLSITE
615 /* ctfBody can return an empty tree if the body is empty */
616 : FLOATINGPOINTTOK ctfBody -> ^(FLOATING_POINT ctfBody?)
617 | INTEGERTOK ctfBody -> ^(INTEGER ctfBody?)
618 | STRINGTOK ctfBody? -> ^(STRING ctfBody?)
624 $Symbols::types = new HashSet<String>();
626 : LCURL ctfAssignmentExpressionList? RCURL -> ctfAssignmentExpressionList?
629 ctfAssignmentExpressionList
630 : (ctfAssignmentExpression TERM!)+
633 ctfAssignmentExpression
639 : left=unaryExpression
640 ( assignment=ASSIGNMENT right1=unaryExpression
641 -> ^(CTF_EXPRESSION_VAL
643 ^(CTF_RIGHT $right1))
644 | type_assignment=TYPE_ASSIGNMENT right2=typeSpecifier
645 -> ^(CTF_EXPRESSION_TYPE
647 ^(CTF_RIGHT ^(TYPE_SPECIFIER_LIST $right2)))
649 | (declarationSpecifiers {inTypedef()}? declaratorList)
650 -> ^(TYPEDEF declaratorList declarationSpecifiers)