Revert "ctf: Remove the callsite support"
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.parser / src / main / antlr3 / org / eclipse / tracecompass / ctf / parser / CTFParser.g
1 parser grammar CTFParser;
2
3 options {
4 language = Java;
5 output = AST;
6 ASTLabelType = CommonTree;
7 tokenVocab = CTFLexer;
8 }
9
10 tokens {
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;
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 */
75 scope Symbols {
76 Set<String> types;
77 }
78
79 @header {
80 /*******************************************************************************
81 * Copyright (c) 2010, 2015 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
94 package org.eclipse.tracecompass.ctf.parser;
95
96 import java.util.Set;
97 import java.util.HashSet;
98 }
99
100 @members {
101 public CTFParser(TokenStream input, boolean verbose) {
102 this(input);
103 this.verbose = verbose;
104 }
105
106 /**
107 * This method is overriden to disable automatic error recovery.
108 * On a mismatched token, it simply re-throw an exception.
109 */
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 + " " + $declaration);
136 }
137 }
138
139 void typedefOn() {
140 debug_print("typedefOn" + $declaration);
141 $declaration::isTypedef=true;
142 }
143
144 void typedefOff() {
145 debug_print("typedefOff" + $declaration);
146 $declaration::isTypedef=false;
147 }
148
149 boolean inTypedef() {
150 return $declaration::isTypedef;
151 }
152
153 boolean _inTypealiasAlias = false;
154
155 void typealiasAliasOn() {
156 debug_print("typealiasAliasOn");
157 _inTypealiasAlias = true;
158 }
159
160 void typealiasAliasOff() {
161 debug_print("typealiasAliasOff");
162 _inTypealiasAlias = false;
163 }
164
165 boolean inTypealiasAlias() {
166 return _inTypealiasAlias;
167 }
168
169 void debug_print(String str) {
170 if (verbose) {
171 System.out.println(str);
172 }
173 }
174
175 /* Prints rule entry and exit while parsing */
176 boolean verbose = false;
177 }
178
179 /*
180 * Override the catch clause to disable automatic error recovery.
181 * By default, the catch block of every rule simple rethrows the error.
182 */
183 @rulecatch {
184 catch (RecognitionException e) {
185 throw e;
186 }
187 }
188
189 /* The top-level rule. */
190 parse
191 scope Symbols;
192 @init {
193 $Symbols::types = new HashSet<String>();
194 }
195 : declaration+ EOF -> ^(ROOT declaration+)
196 ;
197
198 numberLiteral
199 : SIGN*
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*)
203 )
204 ;
205
206 primaryExpression
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
213 | numberLiteral
214 | enumConstant
215 | CHARACTER_LITERAL
216 ;
217
218 postfixExpressionSuffix
219 : OPENBRAC unaryExpression CLOSEBRAC!
220 | (ref=DOT | ref=ARROW) IDENTIFIER
221 -> ^($ref ^(UNARY_EXPRESSION_STRING IDENTIFIER))
222 ;
223
224 postfixCtfExpression
225 : (ref=DOT) ctfSpecifierHead
226 -> ^($ref ^(UNARY_EXPRESSION_STRING ctfSpecifierHead))
227 ;
228
229 postfixExpression
230 : (primaryExpression postfixExpressionSuffix*)
231 | (ctfSpecifierHead postfixCtfExpression* postfixExpressionSuffix+) // added for ctf-v1.8
232 ;
233
234 unaryExpression
235 : postfixExpression
236 /* | ((SIGN postfixExpression[true]) | postfixExpression[false]) */
237 ;
238
239 enumConstant
240 : STRING_LITERAL -> ^(UNARY_EXPRESSION_STRING_QUOTES STRING_LITERAL)
241 | IDENTIFIER -> ^(UNARY_EXPRESSION_STRING IDENTIFIER)
242 | ctfKeyword -> ^(UNARY_EXPRESSION_STRING ctfKeyword)
243 ;
244
245 // 2.2
246
247 declaration
248 scope{
249 boolean isTypedef;
250 }
251 @init {
252 typedefOff();
253 }
254 : declarationSpecifiers declaratorList? TERM
255 // When the declaration is completely parsed and was a typedef,
256 // we add the declarators to the symbol table.
257 -> {inTypedef()}?
258 ^(DECLARATION ^(TYPEDEF declaratorList declarationSpecifiers))
259 -> ^(DECLARATION declarationSpecifiers declaratorList?)
260 | ctfSpecifier TERM!
261 ;
262
263 declarationSpecifiers
264 : (
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
268 | typeQualifier
269 | typeSpecifier
270 )+ -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
271 ;
272
273 declaratorList
274 : declarator (SEPARATOR declarator)*
275 -> ^(TYPE_DECLARATOR_LIST declarator+)
276 ;
277
278 abstractDeclaratorList
279 : abstractDeclarator (SEPARATOR abstractDeclarator)*
280 -> ^(TYPE_DECLARATOR_LIST abstractDeclarator+)
281 ;
282
283 storageClassSpecifier
284 : TYPEDEFTOK { typedefOn(); }
285 ;
286
287 typeSpecifier
288 : FLOATTOK
289 | INTTOK
290 | LONGTOK
291 | SHORTTOK
292 | SIGNEDTOK
293 | UNSIGNEDTOK
294 | CHARTOK
295 | DOUBLETOK
296 | VOIDTOK
297 | BOOLTOK
298 | COMPLEXTOK
299 | IMAGINARYTOK
300 | structSpecifier
301 | variantSpecifier
302 | enumSpecifier
303 | ctfTypeSpecifier
304 | { inTypealiasAlias() || isTypeName(input.LT(1).getText()) }? => typedefName
305 ;
306
307 typeQualifier
308 : CONSTTOK
309 ;
310
311 alignAttribute
312 : ALIGNTOK LPAREN unaryExpression RPAREN -> ^(ALIGN unaryExpression)
313 ;
314
315 // you can have an empty struct but not an empty variant
316 structBody
317 scope Symbols;
318 @init {
319 $Symbols::types = new HashSet<String>();
320 }
321 : LCURL structOrVariantDeclarationList? RCURL
322 -> ^(STRUCT_BODY structOrVariantDeclarationList?)
323 ;
324
325 structSpecifier
326 : STRUCTTOK
327 (
328 // We have an IDENTIFIER after 'struct'
329 (
330 structName
331 (
332 alignAttribute
333 |
334 (
335 structBody
336 ( /* structBody can return an empty tree, so we need those ? */
337 alignAttribute
338 |
339 /* empty */
340 )
341 )
342 |
343 /* empty */
344 )
345 )
346 |
347 // We have a body after 'struct'
348 (
349 structBody
350 (
351 alignAttribute
352 |
353 /* empty */
354 )
355 )
356 ) -> ^(STRUCT structName? structBody? alignAttribute?)
357 ;
358
359 structName
360 : IDENTIFIER -> ^(STRUCT_NAME IDENTIFIER)
361 ;
362
363 structOrVariantDeclarationList
364 : structOrVariantDeclaration+
365 ;
366
367 structOrVariantDeclaration
368 :
369 (
370 (
371 declarationSpecifiers
372 (
373 /* If we met a "typedef" */
374 {inTypedef()}? => declaratorList
375 -> ^(TYPEDEF declaratorList declarationSpecifiers)
376 | structOrVariantDeclaratorList
377 -> ^(SV_DECLARATION declarationSpecifiers structOrVariantDeclaratorList)
378 )
379 )
380 |
381 // Lines 3 and 4
382 typealiasDecl -> typealiasDecl
383 )
384 TERM
385 ;
386
387 specifierQualifierList
388 : (typeQualifier | typeSpecifier)+
389 -> ^(TYPE_SPECIFIER_LIST typeQualifier* typeSpecifier*)
390 ;
391
392 structOrVariantDeclaratorList
393 : structOrVariantDeclarator (SEPARATOR structOrVariantDeclarator)*
394 -> ^(TYPE_DECLARATOR_LIST structOrVariantDeclarator+)
395 ;
396
397 structOrVariantDeclarator
398 :
399 /* Bitfields not supported yet */
400 (declarator (COLON numberLiteral)?) -> declarator
401 /*| (COLON numberLiteral)*/
402 ;
403
404 variantSpecifier
405 : VARIANTTOK
406 (
407 (
408 variantName
409 (
410 (
411 variantTag
412 (
413 variantBody
414 |
415 /* empty */
416 )
417 )
418 |
419 variantBody
420 )
421 )
422 | (variantTag variantBody)
423 | variantBody
424 ) -> ^(VARIANT variantName? variantTag? variantBody?)
425 ;
426
427 variantName
428 : IDENTIFIER -> ^(VARIANT_NAME IDENTIFIER)
429 ;
430
431 variantBody
432 scope Symbols;
433 @init {
434 $Symbols::types = new HashSet<String>();
435 }
436 : LCURL structOrVariantDeclarationList RCURL
437 -> ^(VARIANT_BODY structOrVariantDeclarationList)
438 ;
439
440 variantTag
441 : LT IDENTIFIER GT -> ^(VARIANT_TAG IDENTIFIER)
442 ;
443
444 enumSpecifier
445 : ENUMTOK
446 (
447 // Lines 1 to 5, when we have "ENUMTOK IDENTIFIER".
448 (
449 enumName
450 (
451 enumContainerType enumBody
452 |
453 enumBody
454 |
455 // no enumDeclarator or enumBodym
456 )
457 )
458 |
459 // Lines 1, 2, 4, 5, when we have no IDENTIFIER.
460 (
461 enumContainerType enumBody
462 |
463 enumBody
464 )
465 ) -> ^(ENUM enumName? enumContainerType? enumBody?)
466 ;
467
468 enumName
469 : IDENTIFIER -> ^(ENUM_NAME IDENTIFIER)
470 ;
471
472 enumBody
473 : LCURL enumeratorList SEPARATOR? RCURL -> ^(ENUM_BODY enumeratorList)
474 ;
475
476 enumContainerType
477 : COLON declarationSpecifiers -> ^(ENUM_CONTAINER_TYPE declarationSpecifiers)
478 ;
479
480 enumeratorList
481 : enumerator (SEPARATOR enumerator)* -> (^(ENUM_ENUMERATOR enumerator))+
482 ;
483
484 enumerator
485 : enumConstant enumeratorValue?
486 ;
487
488 enumeratorValue
489 : ASSIGNMENT e1=unaryExpression
490 ( /* empty */
491 -> ^(ENUM_VALUE $e1)
492 | ELIPSES e2=unaryExpression
493 -> ^(ENUM_VALUE_RANGE $e1 $e2)
494 )
495 ;
496
497 declarator
498 : pointer* directDeclarator
499 -> ^(TYPE_DECLARATOR pointer* directDeclarator)
500 ;
501
502 directDeclarator
503 : (
504 IDENTIFIER
505 { if (inTypedef()) addTypeName($IDENTIFIER.text); }
506 { debug_print($IDENTIFIER.text); }
507 /*| LPAREN declarator RPAREN*/ /* Not supported yet */
508 )
509 directDeclaratorSuffix*
510 ;
511
512 directDeclaratorSuffix
513 : OPENBRAC directDeclaratorLength CLOSEBRAC
514 -> ^(LENGTH directDeclaratorLength)
515 ;
516
517 directDeclaratorLength
518 : unaryExpression
519 ;
520
521 abstractDeclarator
522 : pointer+ directAbstractDeclarator?
523 -> ^(TYPE_DECLARATOR pointer+ directAbstractDeclarator?)
524 | directAbstractDeclarator
525 -> ^(TYPE_DECLARATOR directAbstractDeclarator)
526 ;
527
528 /**
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".
532 */
533 directAbstractDeclarator
534 : (
535 IDENTIFIER
536 | (LPAREN abstractDeclarator RPAREN)
537 ) (
538 OPENBRAC unaryExpression? CLOSEBRAC
539 )?
540 ;
541
542 pointer
543 : POINTER typeQualifierList? -> ^(POINTER typeQualifierList?)
544 ;
545
546 typeQualifierList
547 : typeQualifier+
548 ;
549
550 typedefName
551 : {inTypealiasAlias() || isTypeName(input.LT(1).getText())}? IDENTIFIER { if ((inTypedef() || inTypealiasAlias()) && !isTypeName($IDENTIFIER.text)) { addTypeName($IDENTIFIER.text); } }
552 ;
553
554 /**
555 * What goes in the target part of a typealias.
556 *
557 * For example, the integer part in:
558 * typealias integer {...} := my_new_integer;
559 */
560 typealiasTarget
561 : declarationSpecifiers abstractDeclaratorList?
562 ;
563
564 /**
565 * What goes in the alias part of a typealias.
566 *
567 * For example, the my_new_integer part in:
568 * typealias integer {...} := my_new_integer;
569 */
570 typealiasAlias
571 @init {
572 typealiasAliasOn();
573 }
574 @after {
575 typealiasAliasOff();
576 }
577 : abstractDeclaratorList
578 | declarationSpecifiers abstractDeclaratorList?
579 ;
580
581 typealiasDecl
582 : TYPEALIASTOK typealiasTarget TYPE_ASSIGNMENT typealiasAlias
583 -> ^(TYPEALIAS
584 ^(TYPEALIAS_TARGET typealiasTarget)
585 ^(TYPEALIAS_ALIAS typealiasAlias))
586 ;
587
588 // 2.3 CTF stuff
589
590 // TODO: Ajouter ceux qui manquent
591 ctfKeyword
592 : ALIGNTOK
593 | EVENTTOK
594 | SIGNEDTOK
595 | STRINGTOK
596 ;
597
598 ctfSpecifier
599 // event {...}, stream {...}, trace {...}
600 : ctfSpecifierHead ctfBody -> ^(ctfSpecifierHead ctfBody)
601 // typealias
602 | typealiasDecl -> ^(DECLARATION typealiasDecl)
603 ;
604
605 ctfSpecifierHead
606 : EVENTTOK -> EVENT
607 | STREAMTOK -> STREAM
608 | TRACETOK -> TRACE
609 | ENVTOK -> ENV
610 | CLOCKTOK -> CLOCK
611 | CALLSITETOK -> CALLSITE
612 ;
613
614 ctfTypeSpecifier
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?)
619 ;
620
621 ctfBody
622 scope Symbols;
623 @init {
624 $Symbols::types = new HashSet<String>();
625 }
626 : LCURL ctfAssignmentExpressionList? RCURL -> ctfAssignmentExpressionList?
627 ;
628
629 ctfAssignmentExpressionList
630 : (ctfAssignmentExpression TERM!)+
631 ;
632
633 ctfAssignmentExpression
634 @after {
635 if (inTypedef()) {
636 typedefOff();
637 }
638 }
639 : left=unaryExpression
640 ( assignment=ASSIGNMENT right1=unaryExpression
641 -> ^(CTF_EXPRESSION_VAL
642 ^(CTF_LEFT $left)
643 ^(CTF_RIGHT $right1))
644 | type_assignment=TYPE_ASSIGNMENT right2=typeSpecifier
645 -> ^(CTF_EXPRESSION_TYPE
646 ^(CTF_LEFT $left)
647 ^(CTF_RIGHT ^(TYPE_SPECIFIER_LIST $right2)))
648 )
649 | (declarationSpecifiers {inTypedef()}? declaratorList)
650 -> ^(TYPEDEF declaratorList declarationSpecifiers)
651 | typealiasDecl
652 ;
This page took 0.04544 seconds and 5 git commands to generate.