Debian Codegraph @

src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4

raw
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 grammar IDL;
16
17 @header {
18 //package com.eprosima.idl.parser.grammar;
19
20 import com.eprosima.idl.context.Context;
21 import com.eprosima.idl.generator.manager.TemplateManager;
22 import com.eprosima.idl.generator.manager.TemplateGroup;
23 import com.eprosima.idl.generator.manager.TemplateUtil;
24 import com.eprosima.idl.parser.typecode.*;
25 import com.eprosima.idl.parser.tree.*;
26 import com.eprosima.idl.util.Pair;
27 import com.eprosima.idl.parser.strategy.DefaultErrorStrategy;
28 import com.eprosima.idl.parser.listener.DefaultErrorListener;
29 import com.eprosima.idl.parser.exception.ParseException;
30 import com.eprosima.idl.parser.exception.RuntimeGenerationException;
31
32 import java.util.Vector;
33 }
34
35 @parser::members {
36 private TemplateManager tmanager = null;
37 private Context ctx = null;
38
39 public Context getContext_()
40 {
41 return ctx;
42 }
43 }
44
45 @lexer::members{
46 Context ctx = null;
47
48 public void setContext(Context _ctx)
49 {
50 ctx = _ctx;
51 }
52 }
53
54 specification [Context context, TemplateManager templatemanager, TemplateGroup maintemplates] returns [Specification spec = null]
55 @init{
56 //! Used to catch each definition grammar element in the whole IDL file.
57 Pair<Vector<Definition>, TemplateGroup> dtg = null;
58 List<Definition> specificationChildren = new ArrayList<Definition>();
59 ctx = context;
60 tmanager = templatemanager;
61
62 // Set error handler
63 DefaultErrorListener listener = new DefaultErrorListener(ctx);
64 this.setErrorHandler(DefaultErrorStrategy.INSTANCE);
65 // Select listener for errors.
66 ((Lexer)this._input.getTokenSource()).removeErrorListeners();
67 ((Lexer)this._input.getTokenSource()).addErrorListener(listener);
68 this.removeErrorListeners();
69 this.addErrorListener(listener);
70 }
71 : import_decl*
72 (
73 definition[null, null]
74 {
75 dtg=$definition.dtg;
76 if (dtg!=null) {
77 if(maintemplates != null && dtg.second() != null) { // Don't add templates for forward decl.
78 maintemplates.setAttribute("definitions", dtg.second());
79 }
80 for(int count = 0; count < dtg.first().size(); ++count)
81 {
82 ctx.addDefinition(dtg.first().get(count));
83 specificationChildren.add(dtg.first().get(count));
84 }
85 }
86 }
87 )+
88 {
89 if(getNumberOfSyntaxErrors() == 0)
90 {
91 $spec = new Specification();
92 $spec.setDefinitions(specificationChildren);
93 }
94 }
95 ;
96
97 /*!
98 * @brief This grammar element represents a global definition: type declaration, interface, module, etc...
99 * @return A pair with the object that represents the definition (interface for other objects like module grammar element)
100 * and the template group generated by the definition (in fact the template group generated by the element who inherits).
101 */
102 definition [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [Pair<Vector<Definition>, TemplateGroup> dtg = null]
103 @init {
104 Vector<Definition> vector = new Vector<Definition>();
105 Pair<Vector<TypeDeclaration>, TemplateGroup> tdtg = null;
106 Pair<ConstDeclaration, TemplateGroup> cdtg = null;
107 Pair<com.eprosima.idl.parser.tree.Exception, TemplateGroup> etg = null;
108 Pair<Interface, TemplateGroup> itg = null;
109 Pair<com.eprosima.idl.parser.tree.Module, TemplateGroup> mtg = null;
110 Pair<AnnotationDeclaration, TemplateGroup> atg = null;
111
112 if(annotations == null) annotations = new Vector<Annotation>();
113 }
114 : type_decl[annotations, defs] SEMICOLON
115 {
116 tdtg=$type_decl.returnPair;
117 if(tdtg!=null)
118 {
119 for(TypeDeclaration tydl : tdtg.first())
120 {
121 vector.add(tydl);
122 }
123 $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, tdtg.second());
124 }
125 } // Type Declaration
126 | const_decl[null] SEMICOLON { cdtg=$const_decl.returnPair; if(cdtg!=null){ vector.add(cdtg.first()); $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, cdtg.second());}} // Const Declaration
127 | except_decl SEMICOLON { etg=$except_decl.returnPair; if(etg!=null){ vector.add(etg.first()); $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, etg.second());}} // Exception.
128 | interface_or_forward_decl[annotations] SEMICOLON { itg=$interface_or_forward_decl.itg; if(itg!=null){ vector.add(itg.first()); $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, itg.second());}} // Interface
129 | module SEMICOLON { mtg=$module.returnPair; if(mtg!=null){ vector.add(mtg.first()); $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, mtg.second());}} // Module
130 | value SEMICOLON
131 | type_id_decl SEMICOLON
132 | type_prefix_decl SEMICOLON
133 | event SEMICOLON
134 | component SEMICOLON
135 | home_decl SEMICOLON
136 | annotation_decl SEMICOLON { atg=$annotation_decl.returnPair; if(atg!=null){ vector.add(atg.first()); $dtg = new Pair<Vector<Definition>, TemplateGroup>(vector, atg.second());}}
137 | annotation_appl
138 {
139 annotations.add($annotation_appl.annotation);
140 }
141 aux_definition[annotations, defs]{$dtg=$aux_definition.dtg;}
142 ;
143
144 aux_definition [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [Pair<Vector<Definition>, TemplateGroup> dtg = null]
145 : definition[annotations, defs] {$dtg=$definition.dtg;}
146 ;
147
148 /*!
149 * @brief This grammar expression catches a module definition.
150 * @return This grammar expression returns the Module object as DefinitionContainer to be stored.
151 * Also the TemplateGroup of module is returned.
152 */
153 module returns [Pair<com.eprosima.idl.parser.tree.Module, TemplateGroup> returnPair = null]
154 @init{
155 com.eprosima.idl.parser.tree.Module moduleObject = null;
156 TemplateGroup moduleTemplates = null;
157 TemplateGroup tg = null;
158 // Store old namespace.
159 String name = null, old_scope = ctx.getScope();
160 Token tk = null;
161 }
162 : ( KW_MODULE )
163 {
164 tk = _input.LT(1);
165 }
166 identifier
167 {
168 String error = ctx.checkIdentifier(Definition.Kind.MODULE, ctx.getScope(), $identifier.id);
169 if (error != null)
170 {
171 throw new ParseException(null, "Illegal identifier: " + error);
172 }
173 name = ctx.removeEscapeCharacter($identifier.id);
174 // Check if the module already was defined.
175 moduleObject = ctx.existsModule(ctx.getScope() + "::" + name);
176
177 if(moduleObject == null)
178 {
179 // Create the Module object.
180 moduleObject = ctx.createModule(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, tk);
181 }
182
183 // Add the module to the context and update to the new namespace (internally call ctx.setScope)
184 moduleTemplates = ctx.addModule(moduleObject);
185 }
186 // Each definition is stored in the Module and each TemplateGroup is set as attribute in the TemplateGroup of the module.
187 LEFT_BRACE
188 definition_list[moduleObject]{ tg=$definition_list.dlTemplates; if(moduleTemplates!=null && tg!=null)moduleTemplates.setAttribute("definition_list", tg);}
189 RIGHT_BRACE
190 {
191 // Set the old namespace.
192 ctx.setScope(old_scope);
193 // Create the returned data.
194 $returnPair = new Pair<com.eprosima.idl.parser.tree.Module, TemplateGroup>(moduleObject, moduleTemplates);
195 }
196 ;
197
198
199 /*!
200 * @brief This grammar expression catches a list of definitions.
201 * @param dc An object that inherits from DefinitionContainer. This object could store all definitions.
202 * @return This grammar expression returns the template group with all template groups of definitions.
203 */
204 definition_list [DefinitionContainer dc] returns [TemplateGroup dlTemplates]
205 @init{
206 Pair<Vector<Definition>, TemplateGroup> dtg = null;
207 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
208 if(tmanager != null) {
209 $dlTemplates = tmanager.createTemplateGroup("definition_list");
210 }
211 }
212 }
213 : (
214 definition[null, dc.getDefinitions()]
215 {
216 dtg=$definition.dtg;
217 if(dtg!=null)
218 {
219 for(int count = 0; count < dtg.first().size(); ++count)
220 {
221 ctx.addDefinition(dtg.first().get(count));
222 dc.add(dtg.first().get(count));
223 }
224
225 if($dlTemplates != null && dtg.second() != null)
226 {
227 // Set parent
228 dtg.second().setAttribute("parent", dc);
229 // Print template into definitions rule
230 $dlTemplates.setAttribute("definitions", dtg.second());
231 }
232 }
233 }
234 )+
235 ;
236
237 /*!
238 * @brief This grammar expression catches currently a interface declaration.
239 * @return This grammar expression returns the interface object and its template group.
240 */
241 interface_or_forward_decl [Vector<Annotation> annotations] returns [Pair<Interface, TemplateGroup> itg = null]
242 : interface_decl[annotations] {$itg = $interface_decl.returnPair;}
243 | forward_decl
244 ;
245
246 interface_decl [Vector<Annotation> annotations] returns [Pair<Interface, TemplateGroup> returnPair = null]
247 @init {
248 Token tk = null;
249 Interface interfaceObject = null;
250 TemplateGroup interfaceTemplates = null;
251 TemplateGroup tg = null;
252 String name = null, old_scope = ctx.getScope();
253 }
254 : ( ( KW_ABSTRACT | KW_LOCAL )? ( KW_INTERFACE )
255 {
256 tk = _input.LT(1);
257 }
258 identifier
259 {
260 String error = ctx.checkIdentifier(Definition.Kind.INTERFACE, ctx.getScope(), $identifier.id);
261 if (error != null)
262 {
263 throw new ParseException(null, "Illegal identifier: " + error);
264 }
265 name = ctx.removeEscapeCharacter($identifier.id);
266 // Retrieve from forward or Create the Interface object.
267 interfaceObject = ctx.getInterface(name);
268 if (interfaceObject == null)
269 {
270 interfaceObject = ctx.createInterface(name, tk);
271 }
272
273 // Add annotations.
274 for(Annotation annotation : annotations)
275 {
276 interfaceObject.addAnnotation(ctx, annotation);
277 }
278
279 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
280 {
281 if(tmanager != null) {
282 interfaceTemplates = tmanager.createTemplateGroup("interface");
283 interfaceTemplates.setAttribute("ctx", ctx);
284 // Set the the interface object to the TemplateGroup of the module.
285 interfaceTemplates.setAttribute("interface", interfaceObject);
286 }
287 }
288
289 // Update to a new namespace.
290 if(old_scope.isEmpty())
291 ctx.setScope(name);
292 else
293 ctx.setScope(old_scope + "::" + name);
294 }
295 ( interface_inheritance_spec[interfaceObject] )?
296 LEFT_BRACE interface_body[interfaceObject]{ tg=$interface_body.elTemplates; if(interfaceTemplates!=null && tg!=null)interfaceTemplates.setAttribute("export_list", tg);} RIGHT_BRACE )
297 {
298 // Set the old namespace.
299 ctx.setScope(old_scope);
300 // Create the returned data.
301 $returnPair = new Pair<Interface, TemplateGroup>(interfaceObject, interfaceTemplates);
302 }
303 ;
304
305 forward_decl
306 @init {
307 Token tk = null;
308 TemplateGroup interfaceTemplates = null;
309 }
310 : ( KW_ABSTRACT | KW_LOCAL )? ( KW_INTERFACE )
311 {
312 tk = _input.LT(1);
313 }
314 identifier
315 {
316 // Create the Interface object.
317 Interface interfaceObject = ctx.createInterface($identifier.id, tk);
318
319 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
320 {
321 if(tmanager != null)
322 {
323 interfaceTemplates = tmanager.createTemplateGroup("interface");
324 interfaceTemplates.setAttribute("ctx", ctx);
325 // Set the the interface object to the TemplateGroup of the module.
326 interfaceTemplates.setAttribute("interface", interfaceObject);
327 }
328 }
329 }
330 ;
331
332
333 /*!
334 * @brief This grammar expression catches a list of exports.
335 * @param dc An object that inherits from ExportContainer. This object could store all exports.
336 * @return This grammar expression returns the template group with all template groups of exports.
337 */
338 interface_body [ExportContainer ec] returns [TemplateGroup elTemplates]
339 @init{
340 Pair<Vector<Export>, TemplateGroup> etg = null;
341 if(tmanager != null) {
342 $elTemplates = tmanager.createTemplateGroup("export_list");
343 }
344 }
345 : (
346 export[null]
347 {
348 etg=$export.etg;
349 if(etg!=null)
350 {
351 for(int count = 0; count < etg.first().size(); ++count)
352 {
353 ec.add(etg.first().get(count));
354 etg.first().get(count).resolve(ctx);
355 }
356
357 if($elTemplates != null && etg.second() != null)
358 {
359 // Add parent
360 etg.second().setAttribute("parent", ec);
361 // Print template into exports rule
362 $elTemplates.setAttribute("exports", etg.second());
363 }
364 }
365 }
366 )*
367 ;
368
369 /*!
370 * @brief This grammar element represents an export of a interface: type declaration, operation, etc...
371 * @return A pair with the object that represents the export (interface for other objects like operation grammar element)
372 * and the template group generated by the export (in fact the template group generated by the element who inherits).
373 */
374 export [Vector<Annotation> annotations] returns [Pair<Vector<Export>, TemplateGroup> etg = null]
375 @init {
376 Vector<Export> vector = new Vector<Export>();
377 Pair<Vector<TypeDeclaration>, TemplateGroup> tetg = null;
378 Pair<ConstDeclaration, TemplateGroup> cetg = null;
379 Pair<Operation, TemplateGroup> oetg = null;
380 Pair<com.eprosima.idl.parser.tree.Exception, TemplateGroup> eetg = null;
381
382 if(annotations == null) annotations = new Vector<Annotation>();
383 }
384 : type_decl[annotations, null] SEMICOLON { tetg=$type_decl.returnPair; if(tetg!=null){ for(TypeDeclaration tydl : tetg.first()) vector.add(tydl); $etg = new Pair<Vector<Export>, TemplateGroup>(vector, tetg.second());} } // Type Declaration
385 | const_decl[null] SEMICOLON { cetg=$const_decl.returnPair; if(cetg!=null){ vector.add(cetg.first()); $etg = new Pair<Vector<Export>, TemplateGroup>(vector, cetg.second());}} // Const Declaration
386 | except_decl SEMICOLON { eetg=$except_decl.returnPair; if(eetg!=null){ vector.add(eetg.first()); $etg = new Pair<Vector<Export>, TemplateGroup>(vector, eetg.second());}} // Exception
387 | attr_decl SEMICOLON
388 { System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Attribute declarations are not supported. Ignoring..."); }
389 | op_decl[annotations] SEMICOLON { oetg=$op_decl.returnPair; if(oetg!=null){ vector.add(oetg.first()); $etg = new Pair<Vector<Export>, TemplateGroup>(vector, oetg.second());}} // Operation
390 | type_id_decl SEMICOLON
391 | type_prefix_decl SEMICOLON
392 | annotation_appl
393 {
394 annotations.add($annotation_appl.annotation);
395 }
396 aux_export[annotations]{$etg=$aux_export.etg;}
397 ;
398
399 aux_export [Vector<Annotation> annotations] returns [Pair<Vector<Export>, TemplateGroup> etg = null]
400 : export[annotations] {$etg=$export.etg;}
401 ;
402
403 interface_inheritance_spec [Interface interfaceObject]
404 @init{
405 Vector<Pair<String, Token>> iflist = null;
406 }
407 : COLON scoped_name_list { iflist=$scoped_name_list.retlist; }
408 {
409 for(Pair<String, Token> pair : iflist)
410 {
411 Interface base = ctx.getInterface(pair.first());
412
413 if(base != null)
414 {
415 if(!$interfaceObject.addBase(base))
416 throw new ParseException(pair.second(), " is duplicated.");
417 }
418 else
419 {
420 throw new ParseException(pair.second(), "was not defined previously");
421 }
422 }
423 }
424 ;
425
426 interface_name
427 : scoped_name
428 ;
429
430 scoped_name_list returns [Vector<Pair<String, Token>> retlist = null]
431 @init{
432 $retlist = new Vector<Pair<String, Token>>();
433 Pair<String, Token> pair = null;
434 }
435 : scoped_name{ pair=$scoped_name.pair; $retlist.add(pair);} (COMA scoped_name{ pair=$scoped_name.pair; $retlist.add(pair);})*
436 ;
437
438 scoped_name returns [Pair<String, Token> pair = null]
439 @init{
440 String literalStr = "";
441 Token tk = _input.LT(1);
442 }
443 : ( {literalStr += _input.LT(1).getText();} DOUBLE_COLON )?
444 {literalStr += _input.LT(1).getText();} (ID /* identifier */ | KW_OBJECT )
445 (
446 /* An escaped KW may be part of the scoped_name */
447 {literalStr += _input.LT(1).getText();} DOUBLE_COLON
448 (identifier { literalStr+=$identifier.id; } | KW_OBJECT { literalStr+="Object"; } )
449 )*
450 {$pair = new Pair<String, Token>(literalStr, tk);}
451 ;
452
453 value
454 @init{
455 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): ValueType declarations are not supported. Ignoring...");
456 }
457 : ( value_decl | value_abs_decl | value_box_decl | value_forward_decl )
458 ;
459
460 value_forward_decl
461 : ( KW_ABSTRACT )? KW_VALUETYPE ID
462 ;
463
464 value_box_decl
465 : KW_VALUETYPE ID type_spec[null]
466 ;
467
468 value_abs_decl
469 : KW_ABSTRACT KW_VALUETYPE ID value_inheritance_spec LEFT_BRACE export[null]* RIGHT_BRACE
470 ;
471
472 value_decl
473 : value_header LEFT_BRACE value_element* RIGHT_BRACE
474 ;
475
476 value_header
477 : ( KW_CUSTOM )? KW_VALUETYPE ID value_inheritance_spec
478 ;
479
480 value_inheritance_spec
481 : ( COLON ( KW_TRUNCATABLE )? value_name ( COMA value_name )* )? ( KW_SUPPORTS interface_name ( COMA interface_name )* )?
482 ;
483
484 value_name
485 : scoped_name
486 ;
487
488 value_element
489 : ( export[null] | state_member | init_decl )
490 ;
491
492 state_member
493 : ( KW_PUBLIC | KW_PRIVATE ) type_spec[null] declarators SEMICOLON
494 ;
495
496 init_decl
497 : KW_FACTORY ID LEFT_BRACKET ( init_param_decls )? RIGHT_BRACKET ( raises_expr )? SEMICOLON
498 ;
499
500 init_param_decls
501 : init_param_decl ( COMA init_param_decl )*
502 ;
503
504 init_param_decl
505 : init_param_attribute param_type_spec simple_declarator
506 ;
507
508 init_param_attribute
509 : KW_IN
510 ;
511
512 const_decl [AnnotationDeclaration annotation] returns [Pair<ConstDeclaration, TemplateGroup> returnPair = null]
513 @init {
514 ConstDeclaration constDecl = null;
515 TypeCode typecode = null;
516 TemplateGroup template = null;
517 String constName = null, constValue = null;
518 TemplateGroup constTemplates = null;
519 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
520 if(tmanager != null) {
521 constTemplates = tmanager.createTemplateGroup("const_decl");
522 }
523 }
524 Token tk = null;
525 }
526 : KW_CONST const_type[annotation] { typecode=$const_type.returnPair.first(); template=$const_type.returnPair.second(); tk = _input.LT(1);} identifier
527 {
528 String error = ctx.checkIdentifier(Definition.Kind.CONST_DECLARATION, ctx.getScope(), $identifier.id);
529 if (error != null)
530 {
531 throw new ParseException(null, "Illegal identifier: " + error);
532 }
533 constName = ctx.removeEscapeCharacter($identifier.id);
534 }
535 EQUAL const_exp { constValue=$const_exp.literalStr; }
536 {
537 if(typecode != null)
538 {
539 constDecl = new ConstDeclaration(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), constName,
540 typecode, constValue,
541 typecode.isPrimitive()
542 && !typecode.isIsEnumType()
543 && !typecode.isIsCharType()
544 && !typecode.isIsWCharType()
545 ? ctx.evaluate_literal(constValue) : constValue, tk);
546
547 if(constTemplates != null)
548 {
549 constTemplates.setAttribute("ctx", ctx);
550 constTemplates.setAttribute("const", constDecl);
551 if (template !=null)
552 {
553 constTemplates.setAttribute("const_type", template);
554 }
555 }
556
557 $returnPair = new Pair<ConstDeclaration, TemplateGroup>(constDecl, constTemplates);
558 }
559 }
560 ;
561
562 // TODO Not supported fixed types: Show warning
563 const_type[AnnotationDeclaration annotation] returns [Pair<TypeCode, TemplateGroup> returnPair = null]
564 @init{
565 Pair<String, Token> pair = null;
566 TypeCode typecode = null;
567 }
568 : integer_type { $returnPair = new Pair<TypeCode, TemplateGroup>($integer_type.typecode, null); }
569 | char_type { $returnPair = new Pair<TypeCode, TemplateGroup>($char_type.typecode, null); }
570 | wide_char_type { $returnPair = new Pair<TypeCode, TemplateGroup>($wide_char_type.typecode, null); }
571 | boolean_type { $returnPair = new Pair<TypeCode, TemplateGroup>($boolean_type.typecode, null); }
572 | floating_pt_type { $returnPair = new Pair<TypeCode, TemplateGroup>($floating_pt_type.typecode, null); }
573 | string_type { $returnPair = $string_type.returnPair; }
574 | wide_string_type { $returnPair = $wide_string_type.returnPair; }
575 | any_type { $returnPair = new Pair<TypeCode, TemplateGroup>($any_type.typecode, null); }
576 | fixed_pt_const_type
577 | scoped_name
578 {
579 pair = $scoped_name.pair;
580
581 // Find first at annotation scope
582 if ($annotation != null)
583 {
584 typecode = $annotation.getTypeCode(pair.first());
585 }
586
587 // Find typecode in the global map if not found.
588 if (typecode == null)
589 {
590 typecode = ctx.getTypeCode(pair.first());
591 }
592
593 if(typecode == null)
594 {
595 throw new ParseException(pair.second(), "was not defined previously");
596 }
597
598 $returnPair = new Pair<TypeCode, TemplateGroup>(typecode, null);
599 }
600 | octet_type { $returnPair = new Pair<TypeCode, TemplateGroup>($octet_type.typecode, null); }
601 ;
602
603 /* EXPRESSIONS */
604
605 const_exp returns [String literalStr = null]
606 @init{
607 String aux = null;
608 }
609 : or_expr { $literalStr = $or_expr.literalStr; }
610 ;
611
612 or_expr returns [String literalStr = null]
613 @init{
614 String aux = null;
615 }
616 : xor_expr { $literalStr = $xor_expr.literalStr; }
617 (
618 {$literalStr += _input.LT(1).getText();}
619 PIPE
620 xor_expr
621 { aux=$xor_expr.literalStr; $literalStr += aux;}
622 )*
623 ;
624
625 xor_expr returns [String literalStr = null]
626 @init{
627 String aux = null;
628 }
629 : and_expr { $literalStr = $and_expr.literalStr; }
630 (
631 {$literalStr += _input.LT(1).getText();}
632 CARET
633 and_expr
634 { aux=$and_expr.literalStr; $literalStr += aux;}
635 )*
636 ;
637
638 and_expr returns [String literalStr = null]
639 @init{
640 String aux = null;
641 }
642 : shift_expr { $literalStr = $shift_expr.literalStr; }
643 (
644 {$literalStr += _input.LT(1).getText();}
645 AMPERSAND
646 shift_expr
647 { aux=$shift_expr.literalStr; $literalStr += aux;}
648 )*
649 ;
650
651 shift_expr returns [String literalStr = null]
652 @init{
653 String aux = null;
654 }
655 : add_expr { $literalStr = $add_expr.literalStr; }
656 (
657 {$literalStr += _input.LT(1).getText();}
658 ( RIGHT_SHIFT | LEFT_SHIFT )
659 add_expr
660 { aux=$add_expr.literalStr; $literalStr += aux;}
661 )*
662 ;
663
664 add_expr returns [String literalStr = null]
665 @init{
666 String aux = null;
667 }
668 : mult_expr { $literalStr = $mult_expr.literalStr; }
669 (
670 {$literalStr += _input.LT(1).getText();}
671 ( PLUS | MINUS )
672 mult_expr
673 { aux=$mult_expr.literalStr; $literalStr += aux;}
674 )*
675 ;
676
677 mult_expr returns [String literalStr = null]
678 @init{
679 String aux = null;
680 }
681 : unary_expr { $literalStr = $unary_expr.literalStr; }
682 (
683 {$literalStr += _input.LT(1).getText();}
684 ( '*' | SLASH | PERCENT )
685 unary_expr
686 { aux=$unary_expr.literalStr; $literalStr += aux;}
687 )*
688 ;
689
690 unary_expr returns [String literalStr = null]
691 @init{
692 String aux = null;
693 }
694 :
695 {$literalStr = _input.LT(1).getText();}
696 ( MINUS | PLUS | TILDE )
697 primary_expr
698 { aux=$primary_expr.literalStr; $literalStr += aux;}
699 | primary_expr { $literalStr = $primary_expr.literalStr; }
700 ;
701
702 //unary_operator
703 // : ( MINUS | PLUS | TILDE )
704 // ;
705
706 primary_expr returns [String literalStr = null]
707 @init{
708 String aux = null;
709 }
710 : scoped_name { $literalStr = $scoped_name.pair.first(); }
711 | literal { $literalStr = $literal.pair.first(); }
712 | {$literalStr = _input.LT(1).getText();}
713 LEFT_BRACKET
714 const_exp
715 { aux=$const_exp.literalStr; $literalStr += aux; $literalStr += _input.LT(1).getText();}
716 RIGHT_BRACKET
717 ;
718
719 literal returns [Pair<String, Token> pair = null]
720 @init{
721 Token tk = _input.LT(1);
722 String literalStr = tk.getText();
723 }
724 : ( HEX_LITERAL
725 | INTEGER_LITERAL
726 | STRING_LITERAL { literalStr = ctx.concatStringLiterals(literalStr); }
727 | WIDE_STRING_LITERAL { literalStr = ctx.concatStringLiterals(literalStr); }
728 | CHARACTER_LITERAL
729 | WIDE_CHARACTER_LITERAL
730 | FIXED_PT_LITERAL
731 | FLOATING_PT_LITERAL
732 | boolean_literal {literalStr = $boolean_literal.literalStr;} )
733 {$pair = new Pair<String, Token>(literalStr, tk);}
734 ;
735
736 boolean_literal returns [String literalStr = null]
737 : 'TRUE'{ $literalStr = "true";}
738 | 'FALSE'{ $literalStr = "false";}
739 ;
740
741 positive_int_const returns [String literalStr = null]
742 : const_exp { $literalStr = $const_exp.literalStr; }
743 {
744 // TODO Calcular la expresion
745 /*try {
746 int value = Integer.parseInt($literalStr);
747
748 if(value < 0)
749 throw new ParseException($literalStr, "expression is not supported. You must use a positive integer.");
750 } catch(NumberFormatException e) {
751 }*/
752 }
753 ;
754
755 type_decl [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [Pair<Vector<TypeDeclaration>, TemplateGroup> returnPair = null]
756 @init {
757 Pair<Vector<TypeCode>, TemplateGroup> ttg = null;
758 Vector<TypeDeclaration> vector = null;
759 Token tk = null;
760 String fw_name = null;
761 }
762 : ( KW_TYPEDEF {tk = _input.LT(1);} type_declarator[null, annotations] { ttg=$type_declarator.returnPair; }
763 | struct_type[annotations] { ttg=$struct_type.returnPair; fw_name = $struct_type.fw_name; }
764 | union_type[annotations, defs] { ttg=$union_type.returnPair; fw_name = $union_type.fw_name; }
765 | enum_type[annotations] { ttg=$enum_type.returnPair; }
766 | bitset_type[annotations] { ttg=$bitset_type.returnPair; }
767 | bitmask_type[annotations] { ttg=$bitmask_type.returnPair; }
768 | KW_NATIVE { System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Native declarations are not supported. Ignoring..."); } simple_declarator
769 | constr_forward_decl )
770 {
771 if(ttg!=null)
772 {
773 vector = new Vector<TypeDeclaration>();
774
775 for(int count = 0; count < ttg.first().size(); ++count)
776 {
777 String name = null;
778 if(ttg.first().get(count) instanceof MemberedTypeCode)
779 name = ((MemberedTypeCode)ttg.first().get(count)).getName();
780 else if(ttg.first().get(count) instanceof AliasTypeCode)
781 name = ((AliasTypeCode)ttg.first().get(count)).getName();
782
783 if (fw_name != null)
784 {
785 if (ctx.getScope() != null && !ctx.getScope().isEmpty())
786 {
787 fw_name = ctx.getScope() + "::" + fw_name;
788 }
789 }
790
791 TypeDeclaration typedeclaration = (fw_name == null) ? new TypeDeclaration(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, ttg.first().get(count), tk) : ctx.getTypeDeclaration(fw_name);
792
793 // Add type declaration to the map with all typedeclarations.
794 if (fw_name == null)
795 {
796 ctx.addTypeDeclaration(typedeclaration);
797 }
798
799 vector.add(typedeclaration);
800
801 $returnPair = new Pair<Vector<TypeDeclaration>, TemplateGroup>(vector, ttg.second());
802 }
803 }
804 }
805 ;
806
807 type_declarator [AnnotationDeclaration annotation, Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
808 @init {
809 Vector<TypeCode> vector = null;
810 AliasTypeCode typedefTypecode = null;
811 TemplateGroup typedefTemplates = null;
812 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
813 if(tmanager != null) {
814 typedefTemplates = tmanager.createTemplateGroup("typedef_decl");
815 }
816 }
817 }
818 : type_spec[annotation] declarators
819 {
820 if($type_spec.returnPair.first() != null)
821 {
822 vector = new Vector<TypeCode>();
823
824 for(int count = 0; count < $declarators.ret.size(); ++count)
825 {
826 typedefTypecode = ctx.createAliasTypeCode(ctx.getScope(), $declarators.ret.get(count).first().first().first());
827
828 if($declarators.ret.get(count).first().second() != null)
829 {
830 // Array declaration
831 $declarators.ret.get(count).first().second().setContentTypeCode($type_spec.returnPair.first());
832 typedefTypecode.setContentTypeCode($declarators.ret.get(count).first().second());
833 }
834 else
835 {
836 // Simple declaration
837 typedefTypecode.setContentTypeCode($type_spec.returnPair.first());
838 }
839
840 // Apply annotations to the TypeCode
841 if (null != annotations)
842 {
843 for(Annotation ann : annotations)
844 {
845 if (ann != null) // Some annotations may be ignored
846 {
847 typedefTypecode.addAnnotation(ctx, ann);
848 }
849 }
850 }
851
852 if(typedefTemplates != null) {
853 typedefTemplates.setAttribute("typedefs", typedefTypecode);
854 if($declarators.ret.get(count).second() != null)
855 {
856 typedefTemplates.setAttribute("declarator_type", $declarators.ret.get(count).second());
857 }
858 }
859
860 vector.add(typedefTypecode);
861 }
862
863 if(typedefTemplates != null) {
864 typedefTemplates.setAttribute("ctx", ctx);
865 if($type_spec.returnPair.second() != null)
866 {
867 typedefTemplates.setAttribute("typedefs_type", $type_spec.returnPair.second());
868 }
869 }
870
871 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, typedefTemplates);
872 }
873 }
874 ;
875
876 type_spec [AnnotationDeclaration annotation] returns [Pair<TypeCode, TemplateGroup> returnPair = null, Definition def = null]
877 : simple_type_spec[annotation] { $returnPair=$simple_type_spec.returnPair; $def=$simple_type_spec.def; }
878 | constr_type_spec
879 ;
880
881 simple_type_spec [AnnotationDeclaration annotation] returns [Pair<TypeCode, TemplateGroup> returnPair = null, Definition def = null]
882 @init {
883 Pair<String, Token> pair = null;
884 TypeCode typecode = null;
885 }
886 : base_type_spec { $returnPair= new Pair<TypeCode, TemplateGroup>($base_type_spec.typecode, null); }
887 | template_type_spec { $returnPair=$template_type_spec.returnPair; }
888 | scoped_name
889 {
890 pair=$scoped_name.pair;
891
892 // Look for it first on annotation
893 if ($annotation != null)
894 {
895 typecode = $annotation.getTypeCode(pair.first());
896 }
897
898 // Find typecode in the global map.
899 if(typecode == null)
900 {
901 typecode = ctx.getTypeCode(pair.first());
902 }
903 $returnPair = new Pair<TypeCode, TemplateGroup>(typecode, null);
904
905 if(typecode == null)
906 {
907 // Maybe an interface
908 $def = ctx.getInterface(pair.first());
909
910 if ($def == null)
911 {
912 throw new ParseException(pair.second(), "was not defined previously");
913 }
914 }
915 }
916 ;
917
918 bitfield_type_spec returns [TypeCode typecode = null]
919 : integer_type { $typecode=$integer_type.typecode; }
920 | boolean_type { $typecode=$boolean_type.typecode; }
921 | octet_type { $typecode=$octet_type.typecode; }
922 ;
923
924 base_type_spec returns [TypeCode typecode = null]
925 : floating_pt_type { $typecode=$floating_pt_type.typecode; }
926 | integer_type { $typecode=$integer_type.typecode; }
927 | char_type { $typecode=$char_type.typecode; }
928 | wide_char_type { $typecode=$wide_char_type.typecode; }
929 | boolean_type { $typecode=$boolean_type.typecode; }
930 | octet_type { $typecode=$octet_type.typecode; }
931 | any_type
932 | object_type { $typecode=$object_type.typecode; }
933 | value_base_type
934 ;
935
936 template_type_spec returns [Pair<TypeCode, TemplateGroup> returnPair = null]
937 : sequence_type { $returnPair=new Pair<TypeCode, TemplateGroup>($sequence_type.returnPair.first(), $sequence_type.returnPair.second()); }
938 | set_type { $returnPair=new Pair<TypeCode, TemplateGroup>($set_type.returnPair.first(), $set_type.returnPair.second()); }
939 | map_type { $returnPair=new Pair<TypeCode, TemplateGroup>($map_type.returnPair.first(), $map_type.returnPair.second()); }
940 | string_type { $returnPair=$string_type.returnPair; }
941 | wide_string_type { $returnPair=$wide_string_type.returnPair; }
942 | fixed_pt_type
943 ;
944
945 constr_type_spec returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
946 : struct_type[null]
947 | union_type[null, null]
948 | enum_type[null]
949 | bitset_type[null]
950 | bitmask_type[null]
951 ;
952
953 declarators returns [Vector<Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>> ret = new Vector<Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>>()]
954 : declarator
955 {
956 if($declarator.ret != null)
957 $ret.add($declarator.ret);
958 else
959 throw new ParseException(null, "Cannot parse type declarator");
960 }
961 ( COMA declarator
962 {
963 if($declarator.ret != null)
964 $ret.add($declarator.ret);
965 else
966 throw new ParseException(null, "Cannot parse type declarator");
967 }
968 )*
969 ;
970
971 simple_declarators returns [Vector<Pair<Pair<String, Token>, ContainerTypeCode>> ret = new Vector<Pair<Pair<String, Token>, ContainerTypeCode>>()]
972 : simple_declarator
973 {
974 if($simple_declarator.ret != null)
975 $ret.add($simple_declarator.ret);
976 else
977 throw new ParseException(null, "Cannot parse type declarator");
978 }
979 ( COMA simple_declarator
980 {
981 if($simple_declarator.ret != null)
982 $ret.add($simple_declarator.ret);
983 else
984 throw new ParseException(null, "Cannot parse type declarator");
985 }
986 )*
987 ;
988
989 declarator returns [Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup> ret = null]
990 : simple_declarator { $ret=new Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>($simple_declarator.ret, null); }
991 | complex_declarator { $ret=$complex_declarator.ret; }
992 ;
993
994 simple_declarator returns [Pair<Pair<String, Token>, ContainerTypeCode> ret = null]
995 @init
996 {
997 Token tk = _input.LT(1);
998 }
999 : identifier
1000 {
1001 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1002 if (error != null)
1003 {
1004 throw new ParseException(null, "Illegal identifier: " + error);
1005 }
1006 String name = ctx.removeEscapeCharacter($identifier.id);
1007 Pair<String, Token> p = new Pair<String, Token>(name, tk);
1008 $ret = new Pair<Pair<String, Token>, ContainerTypeCode>(p, null);
1009 }
1010 ;
1011
1012 complex_declarator returns [Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup> ret = null]
1013 : array_declarator { $ret=$array_declarator.pair; }
1014 ;
1015
1016 floating_pt_type returns [TypeCode typecode = null]
1017 : ( KW_FLOAT { $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_FLOAT);}
1018 | KW_DOUBLE { $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_DOUBLE);}
1019 | KW_LONG KW_DOUBLE { $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_LONGDOUBLE);}
1020 )
1021 ;
1022
1023 integer_type returns [TypeCode typecode = null]
1024 : signed_int { $typecode = $signed_int.typecode; }
1025 | unsigned_int { $typecode = $unsigned_int.typecode; }
1026 ;
1027
1028 signed_int returns [TypeCode typecode = null]
1029 : signed_short_int { $typecode = $signed_short_int.typecode; }
1030 | signed_long_int { $typecode = $signed_long_int.typecode; }
1031 | signed_longlong_int { $typecode = $signed_longlong_int.typecode; }
1032 | signed_tiny_int { $typecode = $signed_tiny_int.typecode; }
1033 ;
1034
1035 signed_tiny_int returns [TypeCode typecode]
1036 @init{
1037 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_INT8);
1038 }
1039 : KW_INT8
1040 ;
1041
1042 signed_short_int returns [TypeCode typecode]
1043 @init{
1044 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_SHORT);
1045 }
1046 : KW_SHORT
1047 | KW_INT16
1048 ;
1049
1050 signed_long_int returns [TypeCode typecode]
1051 @init{
1052 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_LONG);
1053 }
1054 : KW_LONG
1055 | KW_INT32
1056 ;
1057
1058 signed_longlong_int returns [TypeCode typecode]
1059 @init{
1060 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_LONGLONG);
1061 }
1062 : KW_LONG KW_LONG
1063 | KW_INT64
1064 ;
1065
1066 unsigned_int returns [TypeCode typecode = null]
1067 : unsigned_short_int { $typecode = $unsigned_short_int.typecode; }
1068 | unsigned_long_int { $typecode = $unsigned_long_int.typecode; }
1069 | unsigned_longlong_int { $typecode = $unsigned_longlong_int.typecode; }
1070 | unsigned_tiny_int { $typecode = $unsigned_tiny_int.typecode; }
1071 ;
1072
1073 unsigned_tiny_int returns [TypeCode typecode]
1074 @init{
1075 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_UINT8);
1076 }
1077 : KW_UINT8
1078 ;
1079
1080 unsigned_short_int returns [TypeCode typecode]
1081 @init{
1082 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_USHORT);
1083 }
1084 : KW_UNSIGNED KW_SHORT
1085 | KW_UINT16
1086 ;
1087
1088 unsigned_long_int returns [TypeCode typecode]
1089 @init{
1090 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_ULONG);
1091 }
1092 : KW_UNSIGNED KW_LONG
1093 | KW_UINT32
1094 ;
1095
1096 unsigned_longlong_int returns [TypeCode typecode]
1097 @init{
1098 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_ULONGLONG);
1099 }
1100 : KW_UNSIGNED KW_LONG KW_LONG
1101 | KW_UINT64
1102 ;
1103
1104 char_type returns [TypeCode typecode]
1105 @init{
1106 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_CHAR);
1107 }
1108 : KW_CHAR
1109 ;
1110
1111 wide_char_type returns [TypeCode typecode]
1112 @init{
1113 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_WCHAR);
1114 }
1115 : KW_WCHAR
1116 ;
1117
1118 boolean_type returns [TypeCode typecode]
1119 @init{
1120 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_BOOLEAN);
1121 }
1122 : KW_BOOLEAN
1123 ;
1124
1125 octet_type returns [TypeCode typecode]
1126 @init{
1127 $typecode = ctx.createPrimitiveTypeCode(Kind.KIND_OCTET);
1128 }
1129 : KW_OCTET
1130 ;
1131
1132 any_type returns [TypeCode typecode]
1133 @init{
1134 $typecode = new AnyTypeCode();
1135 }
1136 : KW_ANY
1137 ;
1138
1139 object_type returns [TypeCode typecode]
1140 @init{
1141 Token tk = _input.LT(1);
1142 }
1143 : KW_OBJECT
1144 {
1145 // Find typecode in the global map.
1146 $typecode = ctx.getTypeCode("Object");
1147
1148 if($typecode == null)
1149 {
1150 throw new ParseException(tk, ". Object type is not supported");
1151 }
1152 }
1153 ;
1154
1155 annotation_decl returns [Pair<AnnotationDeclaration, TemplateGroup> returnPair = null]
1156 :
1157 annotation_def { $returnPair=$annotation_def.returnPair; }
1158 | annotation_forward_dcl
1159 ;
1160
1161 annotation_def returns [Pair<AnnotationDeclaration, TemplateGroup> returnPair = null]
1162 @init
1163 {
1164 TemplateGroup annotationTemplates = null;
1165 String old_scope = ctx.getScope();
1166 String name = null;
1167 }
1168 : annotation_header
1169 {
1170 if($annotation_header.annotation != null)
1171 {
1172 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1173 {
1174 if(tmanager != null)
1175 {
1176 annotationTemplates = tmanager.createTemplateGroup("annotation");
1177 annotationTemplates.setAttribute("ctx", ctx);
1178 // Set the annotation object to the TemplateGroup of the annotation.
1179 annotationTemplates.setAttribute("annotation", $annotation_header.annotation);
1180 }
1181 }
1182
1183 name = $annotation_header.annotation.getName();
1184
1185 // Update to a new namespace.
1186 if(old_scope.isEmpty())
1187 ctx.setScope(name);
1188 else
1189 ctx.setScope(old_scope + "::" + name);
1190 }
1191 }
1192 LEFT_BRACE
1193 annotation_body[$annotation_header.annotation]
1194 RIGHT_BRACE
1195 {
1196 // Set the old namespace.
1197 ctx.setScope(old_scope);
1198 // Create the returned data.
1199 $returnPair = new Pair<AnnotationDeclaration, TemplateGroup>($annotation_header.annotation, annotationTemplates);
1200 }
1201 ;
1202
1203 annotation_header returns [AnnotationDeclaration annotation = null]
1204 @init
1205 {
1206 Token tk = null;
1207 }
1208 : KW_AT_ANNOTATION
1209 {
1210 tk = _input.LT(1);
1211 }
1212 identifier
1213 {
1214 String error = ctx.checkIdentifier(Definition.Kind.ANNOTATION, ctx.getScope(), $identifier.id);
1215 if (error != null)
1216 {
1217 throw new ParseException(null, "Illegal identifier: " + error);
1218 }
1219 String name = ctx.removeEscapeCharacter($identifier.id);
1220 $annotation = ctx.createAnnotationDeclaration(name, tk);
1221 }
1222 ( annotation_inheritance_spec[$annotation] )?
1223 ;
1224
1225 annotation_inheritance_spec [AnnotationDeclaration annotation]
1226 @init
1227 {
1228 AnnotationDeclaration inhanno = null;
1229 }
1230 :
1231 COLON scoped_name
1232 {
1233 if(annotation != null)
1234 {
1235 inhanno = ctx.getAnnotationDeclaration($scoped_name.pair.first());
1236
1237 if(inhanno != null)
1238 {
1239 annotation.addMembers(inhanno);
1240 }
1241 else
1242 {
1243 throw new ParseException($scoped_name.pair.second(), "was not defined previously");
1244 }
1245 }
1246 }
1247 ;
1248
1249 annotation_body [AnnotationDeclaration annotation]
1250 @init
1251 {
1252 Pair<Vector<TypeCode>, TemplateGroup> pairtype = null;
1253 Pair<ConstDeclaration, TemplateGroup> pairconst = null;
1254 }
1255 :
1256 (
1257 annotation_member[annotation]
1258 | enum_type[null] SEMICOLON
1259 {
1260 pairtype = $enum_type.returnPair;
1261 $annotation.addEnums(pairtype.first());
1262 }
1263 | const_decl[annotation] SEMICOLON
1264 {
1265 pairconst = $const_decl.returnPair;
1266 $annotation.addConstDecl(pairconst.first());
1267 }
1268 | KW_TYPEDEF type_declarator[annotation, null] SEMICOLON
1269 {
1270 pairtype = $type_declarator.returnPair;
1271 $annotation.addTypeDefs(pairtype.first());
1272 }
1273 )*
1274 ;
1275
1276 annotation_member [AnnotationDeclaration annotation] returns [Pair<TypeCode, TemplateGroup> returnPair = null]
1277 @init
1278 {
1279 String literalStr = null;
1280 }
1281 :
1282 const_type[annotation] {$returnPair=$const_type.returnPair;} simple_declarator ( KW_DEFAULT const_exp { literalStr=$const_exp.literalStr; } )? SEMICOLON
1283 {
1284 if(!$annotation.addMember(new AnnotationMember($simple_declarator.ret.first().first(), $const_type.returnPair.first(), literalStr)))
1285 {
1286 throw new ParseException($simple_declarator.ret.first().second(), $simple_declarator.ret.first().first() + " was defined previously");
1287 }
1288 }
1289 ;
1290
1291 annotation_forward_dcl
1292 :
1293 KW_AT_ANNOTATION scoped_name
1294 ;
1295
1296 bitset_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
1297 @init {
1298 String name = null;
1299 Vector<TypeCode> vector = null;
1300 BitsetTypeCode typecode = null;
1301 TypeCode superType = null;
1302 TemplateGroup bitsetTemplates = null;
1303 } : KW_BITSET
1304 identifier
1305 {
1306 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1307 if (error != null)
1308 {
1309 throw new ParseException(null, "Illegal identifier: " + error);
1310 }
1311 name = ctx.removeEscapeCharacter($identifier.id);
1312 typecode = ctx.createBitsetTypeCode(ctx.getScope(), name);
1313
1314 // Apply annotations to the TypeCode
1315 if (null != annotations)
1316 {
1317 for(Annotation annotation : annotations)
1318 {
1319 if (annotation != null) // Some annotations may be ignored
1320 {
1321 typecode.addAnnotation(ctx, annotation);
1322 }
1323 }
1324 }
1325 }
1326 ( COLON scoped_name
1327 {
1328 superType = ctx.getTypeCode($scoped_name.pair.first());
1329 if (superType != null)
1330 {
1331 typecode.addInheritance(ctx, superType);
1332 }
1333
1334 }
1335 )?
1336 LEFT_BRACE bitfield[typecode] RIGHT_BRACE
1337 {
1338 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1339 {
1340 if(tmanager != null) {
1341 bitsetTemplates = tmanager.createTemplateGroup("bitset_type");
1342 bitsetTemplates.setAttribute("ctx", ctx);
1343 bitsetTemplates.setAttribute("bitset", typecode);
1344 }
1345 }
1346
1347 // Return the returned data.
1348 vector = new Vector<TypeCode>();
1349 vector.add(typecode);
1350 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, bitsetTemplates);
1351 }
1352 ;
1353
1354 bitfield [BitsetTypeCode owner]
1355 : (
1356 (
1357 bitfield_spec simple_declarators SEMICOLON
1358 {
1359 if($bitfield_spec.bitfieldType != null)
1360 {
1361 for(int count = 0; count < $simple_declarators.ret.size(); ++count)
1362 {
1363 Bitfield bitfield = null;
1364
1365 // Only simple declaration
1366 bitfield = new Bitfield($owner, $bitfield_spec.bitfieldType, $simple_declarators.ret.get(count).first().first());
1367
1368 $owner.addBitfield(bitfield);
1369 }
1370 }
1371 }
1372 )
1373 |
1374 (
1375 bitfield_spec SEMICOLON
1376 {
1377 if($bitfield_spec.bitfieldType != null)
1378 {
1379 Bitfield bitfield = null;
1380
1381 // Only simple declaration
1382 bitfield = new Bitfield($owner, $bitfield_spec.bitfieldType, "");
1383
1384 $owner.addBitfield(bitfield);
1385 }
1386 }
1387 )
1388 )+
1389 ;
1390
1391 bitfield_spec returns [BitfieldSpec bitfieldType = null]
1392 @init {
1393 TypeCode type = null;
1394 String bitsize = null;
1395 }
1396 : KW_BITFIELD
1397 LEFT_ANG_BRACKET positive_int_const { bitsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET
1398 {
1399 $bitfieldType = ctx.createBitfieldSpec(bitsize, ctx.createPrimitiveTypeCode(BitfieldSpec.generateKind(bitsize)));
1400 }
1401 | KW_BITFIELD
1402 LEFT_ANG_BRACKET positive_int_const { bitsize=$positive_int_const.literalStr; } COMA bitfield_type_spec { type=$bitfield_type_spec.typecode; } RIGHT_ANG_BRACKET
1403 {
1404 $bitfieldType = ctx.createBitfieldSpec(bitsize, type);
1405 }
1406 ;
1407
1408 bitmask_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
1409 @init {
1410 String name = null;
1411 Vector<TypeCode> vector = null;
1412 BitmaskTypeCode typecode = null;
1413 TemplateGroup bitmaskTemplates = null;
1414 } : KW_BITMASK
1415 identifier
1416 {
1417 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1418 if (error != null)
1419 {
1420 throw new ParseException(null, "Illegal identifier: " + error);
1421 }
1422 name = ctx.removeEscapeCharacter($identifier.id);
1423 typecode = ctx.createBitmaskTypeCode(ctx.getScope(), name);
1424
1425 // Apply annotations to the TypeCode
1426 if (null != annotations)
1427 {
1428 for(Annotation annotation : annotations)
1429 {
1430 if (annotation != null) // Some annotations may be ignored
1431 {
1432 typecode.addAnnotation(ctx, annotation);
1433 }
1434 }
1435 }
1436 }
1437 LEFT_BRACE bit_values[typecode] RIGHT_BRACE
1438 {
1439 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1440 {
1441 if(tmanager != null) {
1442 bitmaskTemplates = tmanager.createTemplateGroup("bitmask_type");
1443 bitmaskTemplates.setAttribute("ctx", ctx);
1444 bitmaskTemplates.setAttribute("bitmask", typecode);
1445 }
1446 }
1447
1448 // Return the returned data.
1449 vector = new Vector<TypeCode>();
1450 vector.add(typecode);
1451 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, bitmaskTemplates);
1452 }
1453 ;
1454
1455 bit_values [BitmaskTypeCode owner]
1456 @init
1457 {
1458 Vector<Annotation> annots = new Vector<Annotation>();
1459 } :
1460 (annotation_appl { annots.add($annotation_appl.annotation); })* identifier
1461 {
1462 Bitmask bitmask_f = new Bitmask(owner, $identifier.id);
1463 for (Annotation ann : annots)
1464 {
1465 bitmask_f.addAnnotation(ctx, ann);
1466 }
1467 owner.addBitmask(bitmask_f);
1468 annots = new Vector<Annotation>();
1469 }
1470 (
1471 COMA (annotation_appl { annots.add($annotation_appl.annotation); })* identifier
1472 {
1473 Bitmask bitmask_o = new Bitmask(owner, $identifier.id);
1474 for (Annotation ann : annots)
1475 {
1476 bitmask_o.addAnnotation(ctx, ann);
1477 }
1478 owner.addBitmask(bitmask_o);
1479 annots = new Vector<Annotation>();
1480 }
1481 )*
1482 ;
1483
1484 struct_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
1485 @init{
1486 String name = null;
1487 Vector<TypeCode> vector = null;
1488 StructTypeCode structTP = null;
1489 StructTypeCode parentStruct = null;
1490 TemplateGroup structTemplates = null;
1491 Boolean fw_declaration = false;
1492 }
1493 : KW_STRUCT
1494 identifier
1495 {
1496 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1497 if (error != null)
1498 {
1499 throw new ParseException(null, "Illegal identifier: " + error);
1500 }
1501 name = ctx.removeEscapeCharacter($identifier.id);
1502 String fw_name = name;
1503
1504 // Find typecode in the global map.
1505 if (ctx.getScope() != null && !ctx.getScope().isEmpty())
1506 {
1507 fw_name = ctx.getScope() + "::" + name;
1508 }
1509
1510 TypeCode typecode = ctx.getTypeCode(fw_name);
1511
1512 if(typecode != null)
1513 {
1514 if (typecode instanceof StructTypeCode)
1515 {
1516 fw_declaration = true;
1517 structTP = (StructTypeCode)typecode;
1518 if (structTP.isDefined())
1519 {
1520 System.out.println("ERROR (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Redefinition: " + fw_name);
1521 throw new ParseException(_input.LT(1), fw_name + " redefinition");
1522 }
1523 }
1524 else
1525 {
1526 System.out.println("ERROR (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Identifier already used.");
1527 }
1528 }
1529 else
1530 {
1531 structTP = ctx.createStructTypeCode(name);
1532 }
1533 structTP.setDefined();
1534
1535 // Apply annotations to the TypeCode
1536 if (null != annotations)
1537 {
1538 for(Annotation annotation : annotations)
1539 {
1540 if (annotation != null) // Some annotations may be ignored
1541 {
1542 structTP.addAnnotation(ctx, annotation);
1543 }
1544 }
1545 }
1546 }
1547 (COLON scoped_name
1548 {
1549 TypeCode scopedType = ctx.getTypeCode($scoped_name.pair.first());
1550 structTP.addInheritance(ctx, scopedType);
1551 }
1552 )?
1553 LEFT_BRACE member_list[structTP] RIGHT_BRACE
1554 {
1555 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1556 {
1557 if(tmanager != null) {
1558 structTemplates = tmanager.createTemplateGroup("struct_type");
1559 structTemplates.setAttribute("ctx", ctx);
1560 structTemplates.setAttribute("struct", structTP);
1561 if($member_list.tg != null)
1562 {
1563 structTemplates.setAttribute("member_list", $member_list.tg);
1564 }
1565 }
1566 }
1567 // Return the returned data.
1568 vector = new Vector<TypeCode>();
1569 structTP.setForwarded(false);
1570 vector.add(structTP);
1571 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, structTemplates);
1572 $fw_name = (fw_declaration) ? name : null;
1573 }
1574 |
1575 KW_STRUCT
1576 identifier
1577 {
1578 // Forward declaration
1579 name=$identifier.id;
1580 structTP = ctx.createStructTypeCode(name);
1581
1582 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1583 {
1584 if(tmanager != null) {
1585 structTemplates = tmanager.createTemplateGroup("fwd_decl");
1586 structTemplates.setAttribute("ctx", ctx);
1587 structTemplates.setAttribute("type", structTP);
1588 }
1589 }
1590
1591 // Return the returned data.
1592 vector = new Vector<TypeCode>();
1593 structTP.setForwarded(true);
1594 vector.add(structTP);
1595 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, structTemplates);
1596 $fw_name = null;
1597 }
1598 ;
1599
1600 member_list [StructTypeCode structTP] returns [TemplateGroup tg = null]
1601 : (
1602 member_def
1603 {
1604 if($member_def.ret != null)
1605 {
1606 for(Pair<Pair<Pair<String, Token>, TemplateGroup>, Member> pair : $member_def.ret)
1607 {
1608 if(!$structTP.addMember(pair.second()))
1609 throw new ParseException(pair.first().first().second(), pair.first().first().first() + " was defined previously");
1610
1611 if(pair.first().second() != null)
1612 {
1613 $tg = pair.first().second();
1614 }
1615 }
1616 }
1617 }
1618 )*
1619 ;
1620
1621 member_def returns [Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Member>> ret = null]
1622 : member { $ret=$member.ret; }
1623 | annotation_appl defret=member_def
1624 {
1625 if($defret.ret != null)
1626 {
1627 $ret=$defret.ret;
1628
1629 for(Pair<Pair<Pair<String, Token>, TemplateGroup>, Member> pair : $ret)
1630 {
1631 if(pair.second() != null)
1632 pair.second().addAnnotation(ctx, $annotation_appl.annotation);
1633 }
1634 }
1635 }
1636 ;
1637
1638 member returns [Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Member>> ret = new Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Member>>()]
1639 @init{
1640 TemplateGroup memberTemplates = null;
1641 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
1642 if(tmanager != null)
1643 {
1644 memberTemplates = tmanager.createTemplateGroup("member_type");
1645 }
1646 }
1647 }
1648 : type_spec[null] declarators SEMICOLON
1649 {
1650 if($type_spec.returnPair.first()!=null)
1651 {
1652 if(memberTemplates != null)
1653 {
1654 memberTemplates.setAttribute("ctx", ctx);
1655 if($type_spec.returnPair.second()!=null)
1656 {
1657 memberTemplates.setAttribute("type_member", $type_spec.returnPair.second());
1658 }
1659 }
1660
1661 for(int count = 0; count < $declarators.ret.size(); ++count)
1662 {
1663 Member member = null;
1664
1665 if($declarators.ret.get(count).first().second() != null)
1666 {
1667 // Array declaration
1668 $declarators.ret.get(count).first().second().setContentTypeCode($type_spec.returnPair.first());
1669 member = new Member($declarators.ret.get(count).first().second(), $declarators.ret.get(count).first().first().first());
1670
1671 }
1672 else
1673 {
1674 // Simple declaration
1675 member = new Member($type_spec.returnPair.first(), $declarators.ret.get(count).first().first().first());
1676 }
1677
1678 if(memberTemplates != null)
1679 {
1680 memberTemplates.setAttribute("member", member);
1681 if($declarators.ret.get(count).second()!=null)
1682 {
1683 memberTemplates.setAttribute("declarators", $declarators.ret.get(count).second());
1684 }
1685 }
1686
1687 Pair<Pair<String, Token>, TemplateGroup> pair = new Pair<Pair<String, Token>, TemplateGroup>($declarators.ret.get(count).first().first(), memberTemplates);
1688 $ret.add(new Pair<Pair<Pair<String, Token>, TemplateGroup>, Member>(pair, member));
1689 }
1690 }
1691 }
1692 ;
1693
1694 union_type [Vector<Annotation> annotations, ArrayList<Definition> defs] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null, String fw_name = null]
1695 @init {
1696 String name = null;
1697 int line = 0;
1698 TypeCode dist_type = null;
1699 Vector<TypeCode> vector = null;
1700 UnionTypeCode unionTP = null;
1701 TemplateGroup unionTemplates = null;
1702 Boolean fw_decl = false;
1703 ArrayList<Annotation> discriminator_annotations = new ArrayList<Annotation>();
1704 }
1705 : KW_UNION
1706 identifier
1707 {
1708 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1709 if (error != null)
1710 {
1711 throw new ParseException(null, "Illegal identifier: " + error);
1712 }
1713 name = ctx.removeEscapeCharacter($identifier.id);
1714 String fw_name = name;
1715
1716 // Find typecode in the global map.
1717 if (ctx.getScope() != null && !ctx.getScope().isEmpty())
1718 {
1719 fw_name = ctx.getScope() + "::" + name;
1720 }
1721
1722 TypeCode typecode = ctx.getTypeCode(fw_name);
1723
1724 if(typecode != null)
1725 {
1726 if (typecode instanceof UnionTypeCode)
1727 {
1728 fw_decl = true;
1729 unionTP = (UnionTypeCode)typecode;
1730 if (unionTP.isDefined())
1731 {
1732 System.out.println("ERROR (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Redefinition: " + fw_name);
1733 throw new ParseException(_input.LT(1), fw_name + " redefinition");
1734 }
1735 }
1736 else
1737 {
1738 System.out.println("ERROR (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Identifier already used.");
1739 }
1740 }
1741 }
1742 KW_SWITCH LEFT_BRACKET (annotation_appl { discriminator_annotations.add($annotation_appl.annotation); })? switch_type_spec { dist_type=$switch_type_spec.typecode; } RIGHT_BRACKET
1743 {
1744 // TODO Check supported types for discriminator: long, enumeration, etc...
1745 if (fw_decl)
1746 {
1747 try
1748 {
1749 unionTP.setDiscriminatorType(dist_type);
1750 }
1751 catch (RuntimeGenerationException ex)
1752 {
1753 System.out.println("ERROR ("+ ex.getMessage()+ ")");
1754 throw new ParseException(_input.LT(1), fw_name + " wrong discriminator member_id");
1755 }
1756 }
1757 else
1758 {
1759 unionTP = ctx.createUnionTypeCode(ctx.getScope(), name, dist_type);
1760 }
1761 unionTP.setDefined();
1762
1763 // Set discriminator annotations.
1764 for(Annotation annotation : discriminator_annotations)
1765 {
1766 unionTP.getDiscriminator().addAnnotation(ctx, annotation);
1767 }
1768
1769 // Apply annotations to the TypeCode
1770 if (null != annotations)
1771 {
1772 for(Annotation annotation : annotations)
1773 {
1774 if (annotation != null) // Some annotations may be ignored
1775 {
1776 unionTP.addAnnotation(ctx, annotation);
1777 }
1778 }
1779 }
1780
1781 line= _input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : 1;
1782 }
1783 LEFT_BRACE switch_body[unionTP] RIGHT_BRACE
1784 {
1785 // Calculate default label.
1786 TemplateUtil.find_and_set_default_discriminator_value(defs, unionTP);
1787
1788 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1789 {
1790 if(tmanager != null) {
1791 unionTemplates = tmanager.createTemplateGroup("union_type");
1792 unionTemplates.setAttribute("ctx", ctx);
1793 unionTemplates.setAttribute("union", unionTP);
1794 if($switch_body.tg != null)
1795 {
1796 unionTemplates.setAttribute("switch_type", $switch_body.tg);
1797 }
1798 }
1799 }
1800
1801 // Return the returned data.
1802 vector = new Vector<TypeCode>();
1803 unionTP.setForwarded(fw_decl);
1804 vector.add(unionTP);
1805 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, unionTemplates);
1806 $fw_name = (fw_decl) ? name : null;
1807 }
1808 |
1809 KW_UNION
1810 identifier
1811 {
1812 name=$identifier.id;
1813 // TODO Check supported types for discriminator: long, enumeration, etc...
1814 unionTP = ctx.createUnionTypeCode(ctx.getScope(), name);
1815
1816 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1817 {
1818 if(tmanager != null) {
1819 unionTemplates = tmanager.createTemplateGroup("fwd_decl");
1820 unionTemplates.setAttribute("ctx", ctx);
1821 unionTemplates.setAttribute("type", unionTP);
1822 }
1823 }
1824
1825 // Return the returned data.
1826 vector = new Vector<TypeCode>();
1827 unionTP.setForwarded(true);
1828 vector.add(unionTP);
1829 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, unionTemplates);
1830 $fw_name = null;
1831
1832 }
1833 ;
1834
1835 switch_type_spec returns [TypeCode typecode = null]
1836 @init {
1837 Pair<String, Token> pair = null;
1838 }
1839 : integer_type { $typecode=$integer_type.typecode; }
1840 | char_type { $typecode=$char_type.typecode; }
1841 | wide_char_type { $typecode = $wide_char_type.typecode; }
1842 | octet_type { $typecode=$octet_type.typecode; }
1843 | boolean_type { $typecode=$boolean_type.typecode; }
1844 | scoped_name
1845 {
1846 pair=$scoped_name.pair;
1847 // Find typecode in the global map.
1848 $typecode = ctx.getTypeCode(pair.first());
1849 if($typecode == null)
1850 throw new ParseException(pair.second(), "was not defined previously");
1851 }
1852 ;
1853
1854 switch_body [UnionTypeCode unionTP] returns [TemplateGroup tg = null]
1855 @init {
1856 }
1857 : case_stmt_list[unionTP] {$tg=$case_stmt_list.tg;}
1858 ;
1859
1860 case_stmt_list [UnionTypeCode unionTP] returns [TemplateGroup tg = null]
1861 @init {
1862 }
1863 : (case_stmt[unionTP] {$tg=$case_stmt.tg;})+
1864 ;
1865
1866 case_stmt [UnionTypeCode unionTP] returns [TemplateGroup tg = null]
1867 @init
1868 {
1869 List<String> labels = new ArrayList<String>();
1870 boolean defaul = false;
1871 List<Annotation> annotations = new ArrayList<Annotation>();
1872 }
1873 : ( KW_CASE const_exp
1874 {
1875 labels.add(TemplateUtil.checkUnionLabel(unionTP.getDiscriminator().getTypecode(), $const_exp.literalStr));
1876 } COLON
1877 | KW_DEFAULT { defaul = true; } COLON
1878 )+
1879 (annotation_appl { annotations.add($annotation_appl.annotation); })* element_spec[labels, defaul] SEMICOLON
1880 {
1881 if($element_spec.ret != null)
1882 {
1883 for (Annotation ann : annotations)
1884 {
1885 $element_spec.ret.second().addAnnotation(ctx, ann);
1886 }
1887
1888 int ret = unionTP.addMember($element_spec.ret.second());
1889
1890 if(ret == -1)
1891 {
1892 throw new ParseException($element_spec.ret.first().first().second(), " is already defined.");
1893 }
1894 else if(ret == -2)
1895 {
1896 throw new ParseException($element_spec.ret.first().first().second(), " is also a default attribute. Another was defined previously.");
1897 }
1898
1899 $tg=$element_spec.ret.first().second();
1900 }
1901 }
1902 ;
1903
1904 //case_label
1905 // : KW_CASE const_exp COLON
1906 // | KW_DEFAULT COLON
1907 // ;
1908
1909 element_spec [List<String> labels, boolean isDefault] returns [Pair<Pair<Pair<String, Token>, TemplateGroup>, UnionMember> ret = null]
1910 @init{
1911 TemplateGroup elementTemplates = null;
1912 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
1913 if(tmanager != null)
1914 {
1915 elementTemplates = tmanager.createTemplateGroup("element_type");
1916 }
1917 }
1918 }
1919 : type_spec[null] declarator
1920 {
1921 if($type_spec.returnPair.first() != null)
1922 {
1923 if(elementTemplates != null)
1924 {
1925 elementTemplates.setAttribute("ctx", ctx);
1926 if($type_spec.returnPair.second()!=null)
1927 {
1928 elementTemplates.setAttribute("type_element", $type_spec.returnPair.second());
1929 }
1930 }
1931
1932 UnionMember member = null;
1933
1934 if($declarator.ret.first().second() != null)
1935 {
1936 $declarator.ret.first().second().setContentTypeCode($type_spec.returnPair.first());
1937 member = new UnionMember($declarator.ret.first().second(), $declarator.ret.first().first().first(), labels, isDefault);
1938 }
1939 else
1940 {
1941 member = new UnionMember($type_spec.returnPair.first(), $declarator.ret.first().first().first(), labels, isDefault);
1942 }
1943
1944 if(elementTemplates != null)
1945 {
1946 elementTemplates.setAttribute("element", member);
1947 if($declarator.ret.second()!=null)
1948 {
1949 elementTemplates.setAttribute("declarator", $declarator.ret.second());
1950 }
1951 }
1952
1953 Pair<Pair<String, Token>, TemplateGroup> pair = new Pair<Pair<String, Token>, TemplateGroup>($declarator.ret.first().first(), elementTemplates);
1954 $ret = new Pair<Pair<Pair<String, Token>, TemplateGroup>, UnionMember>(pair, member);
1955 }
1956 }
1957 ;
1958
1959 enum_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, TemplateGroup> returnPair = null]
1960 @init{
1961 String name = null;
1962 Vector<TypeCode> vector = null;
1963 EnumTypeCode enumTP = null;
1964 TemplateGroup enumTemplates = null;
1965 }
1966 : KW_ENUM
1967 identifier
1968 {
1969 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
1970 if (error != null)
1971 {
1972 throw new ParseException(null, "Illegal identifier: " + error);
1973 }
1974 name = ctx.removeEscapeCharacter($identifier.id);
1975 enumTP = ctx.createEnumTypeCode(ctx.getScope(), name);
1976
1977 // Apply annotations to the TypeCode
1978 if (null != annotations)
1979 {
1980 for(Annotation annotation : annotations)
1981 {
1982 if (annotation != null) // Some annotations may be ignored
1983 {
1984 enumTP.addAnnotation(ctx, annotation);
1985 }
1986 }
1987 }
1988 }
1989 LEFT_BRACE enumerator_list[enumTP] RIGHT_BRACE
1990 {
1991 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
1992 {
1993 if(tmanager != null) {
1994 enumTemplates = tmanager.createTemplateGroup("enum_type");
1995 enumTemplates.setAttribute("ctx", ctx);
1996 enumTemplates.setAttribute("enum", enumTP);
1997 }
1998 }
1999
2000 // Return the returned data.
2001 vector = new Vector<TypeCode>();
2002 vector.add(enumTP);
2003 $returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, enumTemplates);
2004 }
2005 ;
2006
2007 enumerator_list [EnumTypeCode enumTP]
2008 : enumerator[enumTP] (COMA enumerator[enumTP])*
2009 ;
2010
2011 enumerator [EnumTypeCode enumTP]
2012 @init{
2013 String name = null;
2014 ArrayList<Annotation> annotations = new ArrayList<Annotation>();
2015 }
2016 : (annotation_appl { annotations.add($annotation_appl.annotation); })* identifier
2017 {
2018 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
2019 if (error != null)
2020 {
2021 throw new ParseException(null, "Illegal identifier: " + error);
2022 }
2023 name = ctx.removeEscapeCharacter($identifier.id);
2024 EnumMember new_field = new EnumMember(name);
2025 for (Annotation ann : annotations)
2026 {
2027 new_field.addAnnotation(ctx, ann);
2028 }
2029 enumTP.addMember(new_field);
2030 }
2031 ;
2032
2033 sequence_type returns [Pair<SequenceTypeCode, TemplateGroup> returnPair = null]
2034 @init {
2035 TypeCode type = null;
2036 String maxsize = null;
2037 Definition def = null;
2038 SequenceTypeCode typecode = null;
2039 TemplateGroup sequenceTemplates = null;
2040 TemplateGroup template = null;
2041 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
2042 if (tmanager != null) {
2043 sequenceTemplates = tmanager.createTemplateGroup("sequence_type");
2044 }
2045 }
2046 }
2047 : ( (KW_SEQUENCE)
2048 LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.returnPair.first(); template=$simple_type_spec.returnPair.second(); def=$simple_type_spec.def; } COMA positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET
2049 | (KW_SEQUENCE)
2050 LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.returnPair.first(); template=$simple_type_spec.returnPair.second(); def=$simple_type_spec.def; } RIGHT_ANG_BRACKET )
2051 {
2052 if(type != null)
2053 {
2054 typecode = ctx.createSequenceTypeCode(maxsize);
2055 typecode.setContentTypeCode(type);
2056 }
2057 else if (def != null)
2058 {
2059 typecode = ctx.createSequenceTypeCode(maxsize);
2060 typecode.setContentDefinition(def);
2061 }
2062
2063 if(sequenceTemplates != null)
2064 {
2065 sequenceTemplates.setAttribute("sequence", typecode);
2066 sequenceTemplates.setAttribute("ctx", ctx);
2067 if (template != null) {
2068 sequenceTemplates.setAttribute("type_sequence", template);
2069 }
2070 }
2071
2072 $returnPair = new Pair<SequenceTypeCode, TemplateGroup>(typecode, sequenceTemplates);
2073 }
2074 ;
2075
2076 set_type returns [Pair<SetTypeCode, TemplateGroup> returnPair = null]
2077 @init {
2078 TypeCode type = null;
2079 SetTypeCode typecode = null;
2080 String maxsize = null;
2081 Definition def = null;
2082 } : ( KW_SET
2083 LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.returnPair.first(); def=$simple_type_spec.def; } COMA positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET
2084 | KW_SET
2085 LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.returnPair.first(); def=$simple_type_spec.def; } RIGHT_ANG_BRACKET )
2086 {
2087 typecode = ctx.createSetTypeCode(maxsize);
2088 if (type != null)
2089 {
2090 typecode.setContentTypeCode(type);
2091 }
2092 else if (def != null)
2093 {
2094 typecode.setContentDefinition(def);
2095 }
2096
2097 $returnPair = new Pair<SetTypeCode, TemplateGroup>(typecode, $simple_type_spec.returnPair.second());
2098 }
2099 ;
2100
2101 map_type returns [Pair<MapTypeCode, TemplateGroup> returnPair = null]
2102 @init {
2103 TypeCode keyType = null;
2104 TypeCode valueType = null;
2105 Definition keyDef = null;
2106 Definition valueDef = null;
2107 String maxsize = null;
2108 MapTypeCode typecode = null;
2109 TemplateGroup mapTemplates = null;
2110 TemplateGroup keyTemplate = null;
2111 TemplateGroup valueTemplate = null;
2112 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
2113 if(tmanager != null) {
2114 mapTemplates = tmanager.createTemplateGroup("map_type");
2115 }
2116 }
2117 } : KW_MAP
2118 LEFT_ANG_BRACKET simple_type_spec[null]
2119 {
2120 keyType=$simple_type_spec.returnPair.first();
2121 keyTemplate=$simple_type_spec.returnPair.second();
2122 keyDef=$simple_type_spec.def;
2123 }
2124 COMA simple_type_spec[null]
2125 {
2126 valueType=$simple_type_spec.returnPair.first();
2127 valueTemplate=$simple_type_spec.returnPair.second();
2128 valueDef=$simple_type_spec.def;
2129 }
2130 (COMA positive_int_const { maxsize=$positive_int_const.literalStr; } )?
2131 RIGHT_ANG_BRACKET
2132 {
2133 typecode = ctx.createMapTypeCode(maxsize);
2134
2135 if (keyType != null)
2136 {
2137 typecode.setKeyTypeCode(keyType);
2138 }
2139 else if (keyDef != null)
2140 {
2141 typecode.setKeyDefinition(keyDef);
2142 }
2143
2144 if (valueType != null)
2145 {
2146 typecode.setValueTypeCode(valueType);
2147 }
2148 else if (valueDef != null)
2149 {
2150 typecode.setValueDefinition(valueDef);
2151 }
2152
2153 if(mapTemplates != null) {
2154 mapTemplates.setAttribute("map", typecode);
2155 mapTemplates.setAttribute("ctx", ctx);
2156 if(keyTemplate != null)
2157 {
2158 mapTemplates.setAttribute("key_type", keyTemplate);
2159 }
2160 if(valueTemplate != null)
2161 {
2162 mapTemplates.setAttribute("value_type", valueTemplate);
2163 }
2164 }
2165
2166 $returnPair = new Pair<MapTypeCode, TemplateGroup>(typecode, mapTemplates);
2167 }
2168 ;
2169
2170 string_type returns [Pair<TypeCode, TemplateGroup> returnPair = null]
2171 @init{
2172 String maxsize = null;
2173 TypeCode typecode = null;
2174 TemplateGroup stringTemplates = null;
2175 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
2176 if(tmanager != null) {
2177 stringTemplates = tmanager.createTemplateGroup("string_type");
2178 }
2179 }
2180 }
2181 : ( KW_STRING LEFT_ANG_BRACKET positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET
2182 | KW_STRING )
2183 {
2184 typecode = ctx.createStringTypeCode(Kind.KIND_STRING, maxsize);
2185 if (stringTemplates != null) {
2186 stringTemplates.setAttribute("string", typecode);
2187 stringTemplates.setAttribute("ctx", ctx);
2188 }
2189
2190 $returnPair = new Pair<TypeCode, TemplateGroup>(typecode, stringTemplates);
2191 }
2192 ;
2193
2194 wide_string_type returns [Pair<TypeCode, TemplateGroup> returnPair = null]
2195 @init
2196 {
2197 String maxsize = null;
2198 TypeCode typecode = null;
2199 TemplateGroup wstringTemplates = null;
2200 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
2201 if(tmanager != null) {
2202 wstringTemplates = tmanager.createTemplateGroup("wide_string_type");
2203 }
2204 }
2205 }
2206 : ( KW_WSTRING LEFT_ANG_BRACKET positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET
2207 | KW_WSTRING )
2208 {
2209 typecode = ctx.createStringTypeCode(Kind.KIND_WSTRING, maxsize);
2210 if (wstringTemplates != null) {
2211 wstringTemplates.setAttribute("wstring", typecode);
2212 wstringTemplates.setAttribute("ctx", ctx);
2213 }
2214
2215 $returnPair = new Pair<TypeCode, TemplateGroup>(typecode, wstringTemplates);
2216 }
2217 ;
2218
2219 array_declarator returns [Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup> pair = null]
2220 @init
2221 {
2222 Token tk = _input.LT(1);
2223 ArrayTypeCode typecode = ctx.createArrayTypeCode();
2224 TemplateGroup arrayTemplates = null;
2225 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
2226 if(tmanager != null) {
2227 arrayTemplates = tmanager.createTemplateGroup("array_declarator");
2228 }
2229 }
2230 }
2231 : ID
2232 (
2233 fixed_array_size
2234 {
2235 typecode.addDimension($fixed_array_size.literalStr, ctx.evaluate_literal($fixed_array_size.literalStr));
2236 }
2237 )+
2238 {
2239 if(arrayTemplates != null)
2240 {
2241 arrayTemplates.setAttribute("array", typecode);
2242 arrayTemplates.setAttribute("ctx", ctx);
2243 }
2244 Pair<String, Token> p = new Pair<String, Token>(tk.getText(), tk);
2245 Pair<Pair<String, Token>, ContainerTypeCode> pp = new Pair<Pair<String, Token>, ContainerTypeCode>(p, typecode);
2246 $pair = new Pair<Pair<Pair<String, Token>, ContainerTypeCode>, TemplateGroup>(pp, arrayTemplates);
2247 }
2248 ;
2249
2250 fixed_array_size returns [String literalStr = null]
2251 : LEFT_SQUARE_BRACKET
2252 positive_int_const { $literalStr=$positive_int_const.literalStr; }
2253 RIGHT_SQUARE_BRACKET
2254 ;
2255
2256 attr_decl returns [Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, TypeCode>> ret = null, Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Definition>> retDef = null]
2257 : readonly_attr_spec
2258 | attr_spec { $ret=$attr_spec.ret; $retDef=$attr_spec.retDef; }
2259 ;
2260
2261 except_decl returns [Pair<com.eprosima.idl.parser.tree.Exception, TemplateGroup> returnPair = null]
2262 @init {
2263 String name = null;
2264 com.eprosima.idl.parser.tree.Exception exceptionObject = null;
2265 TemplateGroup exTemplates = null;
2266 Token tk = null;
2267 }
2268 : KW_EXCEPTION
2269 {
2270 tk = _input.LT(1);
2271 }
2272 identifier
2273 {
2274 String error = ctx.checkIdentifier(Definition.Kind.TYPE_DECLARATION, ctx.getScope(), $identifier.id);
2275 if (error != null)
2276 {
2277 throw new ParseException(null, "Illegal identifier: " + error);
2278 }
2279 name = ctx.removeEscapeCharacter($identifier.id);
2280 }
2281 {
2282 // Create the Exception object.
2283 exceptionObject = ctx.createException(name, tk);
2284
2285 if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
2286 {
2287 if(tmanager != null) {
2288 exTemplates = tmanager.createTemplateGroup("exception");
2289 exTemplates.setAttribute("ctx", ctx);
2290 // Set the the exception object to the TemplateGroup of the module.
2291 exTemplates.setAttribute("exception", exceptionObject);
2292 }
2293 }
2294 // Its a dependency.
2295 else
2296 {
2297 ctx.addIncludeDependency(ctx.getScopeFile());
2298 }
2299 }
2300 LEFT_BRACE opt_member_list[exceptionObject] RIGHT_BRACE
2301 {
2302 // Create the returned data.
2303 $returnPair = new Pair<com.eprosima.idl.parser.tree.Exception, TemplateGroup>(exceptionObject, exTemplates);
2304 }
2305 ;
2306
2307 opt_member_list [com.eprosima.idl.parser.tree.Exception exceptionObject]
2308 : (
2309 member
2310 {
2311 for(int count = 0; count < $member.ret.size(); ++count)
2312 $exceptionObject.addMember($member.ret.get(count).second());
2313 }
2314 )*
2315 ;
2316
2317 /*!
2318 * @brief This grammar expression catches a operation.
2319 * @return This grammar expression returns the operation object and its template group.
2320 */
2321 op_decl [Vector<Annotation> annotations] returns [Pair<Operation, TemplateGroup> returnPair = null]
2322 @init {
2323 Operation operationObject = null;
2324 TemplateGroup operationTemplates = null;
2325 if(tmanager != null) {
2326 operationTemplates = tmanager.createTemplateGroup("operation");
2327 }
2328 TemplateGroup tpl = null;
2329 TemplateGroup template = null;
2330 String name = "";
2331 Token tk = null, tkoneway = null;
2332 TypeCode retType = null;
2333 Vector<Pair<String, Token>> exceptions = null;
2334 }
2335 : ( op_attribute { tkoneway=$op_attribute.token; } )?
2336 op_type_spec { if(null != $op_type_spec.returnPair) {retType=$op_type_spec.returnPair.first();
2337 template=$op_type_spec.returnPair.second();}}
2338 {
2339 tk = _input.LT(1);
2340 name += tk.getText();
2341 }
2342 ID
2343 {
2344 // Create the Operation object.
2345 operationObject = ctx.createOperation(name, tk);
2346
2347 // Add annotations.
2348 for(Annotation annotation : annotations)
2349 operationObject.addAnnotation(ctx, annotation);
2350
2351 if(operationTemplates != null)
2352 {
2353 operationTemplates.setAttribute("ctx", ctx);
2354 // Set the the interface object to the TemplateGroup of the module.
2355 operationTemplates.setAttribute("operation", operationObject);
2356 if(template != null)
2357 {
2358 operationTemplates.setAttribute("operation_type", template);
2359 }
2360 }
2361
2362 // Set return type
2363 operationObject.setRettype(retType);
2364
2365 // Set oneway
2366 if(tkoneway != null)
2367 {
2368 operationObject.setOneway(true);
2369
2370 if(retType != null)
2371 {
2372 throw new ParseException(tkoneway, ". Oneway function cannot have a return type.");
2373 }
2374 }
2375 }
2376 parameter_decls[operationObject] { tpl=$parameter_decls.tpl; }
2377 (
2378 raises_expr { exceptions=$raises_expr.exlist; }
2379 {
2380 // Search global exceptions and add them to the operation.
2381 for(Pair<String, Token> pair : exceptions)
2382 {
2383 com.eprosima.idl.parser.tree.Exception exception = ctx.getException(pair.first());
2384
2385 if(exception != null)
2386 operationObject.addException(exception);
2387 else
2388 operationObject.addUnresolvedException(pair.first());
2389 }
2390 }
2391 )?
2392 ( context_expr )?
2393 {
2394 if(operationTemplates != null) {
2395 // Store the parameter list template group in the operation template group.
2396 operationTemplates.setAttribute("param_list", tpl);
2397 }
2398 // Create the returned data.
2399 $returnPair = new Pair<Operation, TemplateGroup>(operationObject, operationTemplates);
2400 }
2401 ;
2402
2403 op_attribute returns [Token token = null]
2404 @init {
2405 Token tk = _input.LT(1);
2406 }
2407 : KW_ONEWAY { $token = tk;}
2408 ;
2409
2410 op_type_spec returns [Pair<TypeCode, TemplateGroup> returnPair = null, Definition def = null]
2411 : param_type_spec { $returnPair=$param_type_spec.returnPair; $def=$param_type_spec.def; }
2412 | KW_VOID
2413 ;
2414
2415 parameter_decls [Operation operation] returns [TemplateGroup tpl]
2416 @init {
2417 if(tmanager != null) {
2418 $tpl = tmanager.createTemplateGroup("param_list");
2419 }
2420 }
2421 : LEFT_BRACKET (param_decl_list[operation, $tpl])? RIGHT_BRACKET
2422 ;
2423
2424 param_decl_list [Operation operation, TemplateGroup tpl]
2425 @init {
2426 Pair<Param, TemplateGroup> ptg = null;
2427 }
2428 : param_decl { ptg=$param_decl.returnPair; }
2429 {
2430 if(ptg!=null) {
2431 operation.add(ptg.first());
2432 if(tpl != null) {
2433 tpl.setAttribute("parameters", ptg.second());
2434 }
2435 }
2436 }
2437 (
2438 COMA param_decl { ptg=$param_decl.returnPair; }
2439 {
2440 if(ptg!=null) {
2441 operation.add(ptg.first());
2442 if(tpl != null) {
2443 tpl.setAttribute("parameters", ptg.second());
2444 }
2445 }
2446 }
2447 )*
2448 ;
2449
2450 param_decl returns [Pair<Param, TemplateGroup> returnPair = null]
2451 @init{
2452 ArrayList<Annotation> annotations = new ArrayList<Annotation>();
2453 }
2454 : (annotation_appl { annotations.add($annotation_appl.annotation); })* param_def
2455 {
2456 $returnPair=$param_def.returnPair;
2457 for (Annotation ann : annotations)
2458 {
2459 $returnPair.first().addAnnotation(ctx, ann);
2460 }
2461 }
2462 ;
2463
2464 param_def returns [Pair<Param, TemplateGroup> returnPair = null]
2465 @init{
2466 TemplateGroup paramTemplate = null;
2467 TemplateGroup template = null;
2468 if(tmanager != null) {
2469 paramTemplate = tmanager.createTemplateGroup("param");
2470 }
2471 TypeCode typecode = null;
2472 Definition definition = null;
2473 String literalStr = _input.LT(1).getText();
2474 }
2475 : ('in' | 'out' | 'inout')
2476 param_type_spec { typecode=$param_type_spec.returnPair.first(); template=$param_type_spec.returnPair.second(); definition=$param_type_spec.def; }
2477 simple_declarator
2478 {
2479 if(typecode != null)
2480 {
2481 Param param = null;
2482 if(literalStr.equals("in"))
2483 param = ctx.createParam($simple_declarator.ret.first().first(), typecode, Param.Kind.IN_PARAM);
2484 else if(literalStr.equals("out"))
2485 param = ctx.createParam($simple_declarator.ret.first().first(), typecode, Param.Kind.OUT_PARAM);
2486 else if(literalStr.equals("inout"))
2487 param = ctx.createParam($simple_declarator.ret.first().first(), typecode, Param.Kind.INOUT_PARAM);
2488
2489 if(paramTemplate != null) {
2490 paramTemplate.setAttribute("parameter", param);
2491 if(template != null)
2492 {
2493 paramTemplate.setAttribute("parameter_type", template);
2494 }
2495 }
2496 $returnPair = new Pair<Param, TemplateGroup>(param, paramTemplate);
2497 }
2498 else if (definition != null)
2499 {
2500 Param param = null;
2501 if(literalStr.equals("in"))
2502 param = ctx.createParam($simple_declarator.ret.first().first(), definition, Param.Kind.IN_PARAM);
2503 else if(literalStr.equals("out"))
2504 param = ctx.createParam($simple_declarator.ret.first().first(), definition, Param.Kind.OUT_PARAM);
2505 else if(literalStr.equals("inout"))
2506 param = ctx.createParam($simple_declarator.ret.first().first(), definition, Param.Kind.INOUT_PARAM);
2507
2508 if(paramTemplate != null) {
2509 paramTemplate.setAttribute("parameter", param);
2510 paramTemplate.setAttribute("parameter_type", template);
2511 }
2512 $returnPair = new Pair<Param, TemplateGroup>(param, paramTemplate);
2513 }
2514 }
2515 ;
2516
2517 //param_attribute
2518 // : KW_IN
2519 // | KW_OUT
2520 // | KW_INOUT
2521 // ;
2522
2523 raises_expr returns [Vector<Pair<String, Token>> exlist = null]
2524 : KW_RAISES LEFT_BRACKET scoped_name_list { $exlist=$scoped_name_list.retlist; } RIGHT_BRACKET
2525 ;
2526
2527 context_expr
2528 @init {
2529 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Context declarations are not supported. Ignoring...");
2530 }
2531 : KW_CONTEXT LEFT_BRACKET STRING_LITERAL ( COMA STRING_LITERAL )* RIGHT_BRACKET
2532 ;
2533
2534 param_type_spec returns [Pair<TypeCode, TemplateGroup> returnPair = null, Definition def = null]
2535 @init{
2536 Pair<String, Token> pair = null;
2537 TypeCode typecode = null;
2538 }
2539 : base_type_spec { $returnPair=new Pair<TypeCode, TemplateGroup>($base_type_spec.typecode, null); }
2540 | string_type { $returnPair=$string_type.returnPair; }
2541 | wide_string_type { $returnPair=$wide_string_type.returnPair; }
2542 | scoped_name
2543 {
2544 pair=$scoped_name.pair;
2545 // Find typecode in the global map.
2546 typecode = ctx.getTypeCode(pair.first());
2547 $returnPair = new Pair<TypeCode, TemplateGroup>(typecode, null);
2548
2549 if(typecode == null)
2550 {
2551 // Maybe an interface
2552 $def = ctx.getInterface(pair.first());
2553
2554 if ($def == null)
2555 {
2556 throw new ParseException(pair.second(), "was not defined previously");
2557 }
2558 }
2559 }
2560 ;
2561
2562 fixed_pt_type
2563 @init{
2564 Token tk = _input.LT(1);
2565 }
2566 : KW_FIXED LEFT_ANG_BRACKET positive_int_const COMA positive_int_const RIGHT_ANG_BRACKET
2567 {throw new ParseException(tk, ". Fixed type is not supported");}
2568 ;
2569
2570 fixed_pt_const_type
2571 @init{
2572 Token tk = _input.LT(1);
2573 }
2574 : KW_FIXED
2575 {throw new ParseException(tk, ". Fixed type is not supported");}
2576 ;
2577
2578 value_base_type
2579 @init{
2580 Token tk = _input.LT(1);
2581 }
2582 : KW_VALUEBASE
2583 {throw new ParseException(tk, ". Value type is not supported");}
2584 ;
2585
2586 constr_forward_decl
2587 : KW_STRUCT ID
2588 | KW_UNION ID
2589 ;
2590
2591 import_decl
2592 : KW_IMPORT
2593 {
2594 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Import declarations are not supported. Ignoring...");
2595 }
2596 imported_scope SEMICOLON
2597 ;
2598
2599 imported_scope
2600 : scoped_name | STRING_LITERAL
2601 ;
2602
2603 type_id_decl
2604 : KW_TYPEID
2605 {
2606 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): TypeID declarations are not supported. Ignoring...");
2607 }
2608 scoped_name STRING_LITERAL
2609 ;
2610
2611 type_prefix_decl
2612 : KW_TYPEPREFIX
2613 {
2614 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): TypePrefix declarations are not supported. Ignoring...");
2615 }
2616 scoped_name STRING_LITERAL
2617 ;
2618
2619 readonly_attr_spec
2620 : KW_READONLY KW_ATTRIBUTE param_type_spec readonly_attr_declarator
2621 ;
2622
2623 readonly_attr_declarator
2624 : simple_declarator raises_expr
2625 | simple_declarator ( COMA simple_declarator )*
2626 ;
2627
2628 attr_spec returns [Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, TypeCode>> ret = new Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, TypeCode>>(), Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Definition>> retDef = new Vector<Pair<Pair<Pair<String, Token>, TemplateGroup>, Definition>>()]
2629 @init {
2630 TypeCode typecode = null;
2631 Definition definition = null;
2632 }
2633 : KW_ATTRIBUTE param_type_spec { typecode=$param_type_spec.returnPair.first(); definition=$param_type_spec.def; } attr_declarator
2634 {
2635 if(typecode != null)
2636 {
2637 for(int count = 0; count < $attr_declarator.ret.size(); ++count)
2638 {
2639 // attr_declarator always is a simple declarator. Not a complex (array):
2640 // Simple declaration
2641 Pair<Pair<String, Token>, TemplateGroup> pair = new Pair<Pair<String, Token>, TemplateGroup>($attr_declarator.ret.get(count).first(), $param_type_spec.returnPair.second());
2642 $ret.add(new Pair<Pair<Pair<String, Token>, TemplateGroup>, TypeCode>(pair, typecode));
2643 }
2644 }
2645 else if(definition != null)
2646 {
2647 for(int count = 0; count < $attr_declarator.ret.size(); ++count)
2648 {
2649 // attr_declarator always is a simple declarator. Not a complex (array):
2650 // Simple declaration
2651 Pair<Pair<String, Token>, TemplateGroup> pair = new Pair<Pair<String, Token>, TemplateGroup>($attr_declarator.ret.get(count).first(), $param_type_spec.returnPair.second());
2652 $retDef.add(new Pair<Pair<Pair<String, Token>, TemplateGroup>, Definition>(pair, definition));
2653 }
2654 }
2655 }
2656 ;
2657
2658 attr_declarator returns [Vector<Pair<Pair<String, Token>, ContainerTypeCode>> ret]
2659 @init {
2660 $ret = new Vector<Pair<Pair<String, Token>, ContainerTypeCode>>();
2661 }
2662 : simple_declarator {$ret.add($simple_declarator.ret);}
2663 ( attr_raises_expr
2664 | (COMA simple_declarator {$ret.add($simple_declarator.ret);})*
2665 )
2666 ;
2667
2668 attr_raises_expr
2669 : get_excep_expr ( set_excep_expr )?
2670 | set_excep_expr
2671 ;
2672
2673 get_excep_expr
2674 : KW_GETRAISES exception_list
2675 ;
2676
2677 set_excep_expr
2678 : KW_SETRAISES exception_list
2679 ;
2680
2681 exception_list
2682 : LEFT_BRACKET scoped_name ( COMA scoped_name )* RIGHT_BRACKET
2683 ;
2684
2685 // Component Stuff
2686
2687 component
2688 : component_decl
2689 | component_forward_decl
2690 ;
2691
2692 component_forward_decl
2693 : KW_COMPONENT
2694 {
2695 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Component declarations are not supported. Ignoring...");
2696 }
2697 ID
2698 ;
2699
2700 component_decl
2701 : component_header LEFT_BRACE component_body RIGHT_BRACE
2702 ;
2703
2704 component_header
2705 : KW_COMPONENT ID ( component_inheritance_spec )? ( supported_interface_spec )?
2706 ;
2707
2708 supported_interface_spec
2709 : KW_SUPPORTS scoped_name ( COMA scoped_name )*
2710 ;
2711
2712 component_inheritance_spec
2713 : COLON scoped_name
2714 ;
2715
2716 component_body
2717 : component_export*
2718 ;
2719
2720 component_export
2721 : provides_decl SEMICOLON
2722 | uses_decl SEMICOLON
2723 | emits_decl SEMICOLON
2724 | publishes_decl SEMICOLON
2725 | consumes_decl SEMICOLON
2726 | attr_decl SEMICOLON
2727 ;
2728
2729 provides_decl
2730 : KW_PROVIDES interface_type ID
2731 ;
2732
2733 interface_type
2734 : scoped_name
2735 | KW_OBJECT
2736 ;
2737
2738 uses_decl
2739 : KW_USES ( KW_MULTIPLE )? interface_type ID
2740 ;
2741
2742 emits_decl
2743 : KW_EMITS scoped_name ID
2744 ;
2745
2746 publishes_decl
2747 : KW_PUBLISHES scoped_name ID
2748 ;
2749
2750 consumes_decl
2751 : KW_CONSUMES scoped_name ID
2752 ;
2753
2754 home_decl
2755 @init {
2756 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Home declarations are not supported. Ignoring...");
2757 }
2758 : home_header home_body
2759 ;
2760
2761 home_header
2762 : KW_HOME ID ( home_inheritance_spec )? ( supported_interface_spec )? KW_MANAGES scoped_name ( primary_key_spec )?
2763 ;
2764
2765 home_inheritance_spec
2766 : COLON scoped_name
2767 ;
2768
2769 primary_key_spec
2770 : KW_PRIMARYKEY scoped_name
2771 ;
2772
2773 home_body
2774 : LEFT_BRACE home_export* RIGHT_BRACE
2775 ;
2776
2777 home_export
2778 : export[null]
2779 | factory_decl SEMICOLON
2780 | finder_decl SEMICOLON
2781 ;
2782
2783 factory_decl
2784 : KW_FACTORY ID LEFT_BRACKET ( init_param_decls )? RIGHT_BRACKET ( raises_expr )?
2785 ;
2786
2787 finder_decl
2788 : KW_FINDER ID LEFT_BRACKET ( init_param_decls )? RIGHT_BRACKET ( raises_expr )?
2789 ;
2790
2791 event
2792 @init{
2793 System.out.println("WARNING (File " + ctx.getFilename() + ", Line " + (_input.LT(1) != null ? _input.LT(1).getLine() - ctx.getCurrentIncludeLine() : "1") + "): Event declarations are not supported. Ignoring...");
2794 }
2795 : ( event_decl | event_abs_decl | event_forward_decl)
2796 ;
2797
2798 event_forward_decl
2799 : ( KW_ABSTRACT )? KW_EVENTTYPE ID
2800 ;
2801
2802 event_abs_decl
2803 : KW_ABSTRACT KW_EVENTTYPE ID value_inheritance_spec LEFT_BRACE export[null]* RIGHT_BRACE
2804 ;
2805
2806 event_decl
2807 : event_header LEFT_BRACE value_element* RIGHT_BRACE
2808 ;
2809
2810 event_header
2811 : ( KW_CUSTOM )? KW_EVENTTYPE ID value_inheritance_spec
2812 ;
2813
2814 annotation_appl returns [Annotation annotation = null]
2815 @init
2816 {
2817 AnnotationDeclaration anndecl = null;
2818 String name;
2819 }
2820 :
2821 AT
2822 (
2823 scoped_name { name = $scoped_name.pair.first(); }
2824 | KW_DEFAULT { name = "default"; }
2825 )
2826 {
2827 anndecl = ctx.getAnnotationDeclaration(name);
2828 if(anndecl == null)
2829 {
2830 throw new ParseException($scoped_name.pair.second(), "was not defined previously");
2831 }
2832 else
2833 {
2834 $annotation = new Annotation(anndecl);
2835 }
2836
2837 }
2838 ( LEFT_BRACKET ( annotation_appl_params[$annotation, name] )? RIGHT_BRACKET )?
2839 ;
2840
2841 // TODO Support several members in annotations.
2842 annotation_appl_params [Annotation annotation, String tkannot]
2843 : const_exp
2844 {
2845 if(annotation != null && !annotation.addValue($const_exp.literalStr))
2846 {
2847 System.out.println("WARNING: " + tkannot + " doesn't has only one attribute.");
2848 }
2849 }
2850 | annotation_appl_param[annotation] ( COMA annotation_appl_param[annotation] )*
2851 ;
2852
2853 annotation_appl_param [Annotation annotation]
2854 @init
2855 {
2856 Token tk = _input.LT(1);
2857 }
2858 : identifier EQUAL const_exp
2859 {
2860 if(annotation != null && !annotation.addValue($identifier.id, $const_exp.literalStr))
2861 throw new ParseException(tk, "is not an attribute of annotation " + annotation.getName());
2862 }
2863 ;
2864
2865 identifier returns [String id]
2866 @init {
2867 $id = _input.LT(1).getText();
2868 }
2869 : ID
2870 ;
2871
2872
2873 INTEGER_LITERAL : ('0' | '1'..'9' '0'..'9'*) INTEGER_TYPE_SUFFIX? ;
2874
2875 OCTAL_LITERAL : '0' ('0'..'7')+ INTEGER_TYPE_SUFFIX? ;
2876
2877 HEX_LITERAL : '0' ('x' | 'X') HEX_DIGIT+ INTEGER_TYPE_SUFFIX? ;
2878
2879 fragment
2880 HEX_DIGIT : ( '0'..'9' | 'a'..'f' | 'A'..'F' ) ;
2881
2882 fragment
2883 INTEGER_TYPE_SUFFIX : ('l' | 'L') ;
2884
2885 FLOATING_PT_LITERAL
2886 : ('0'..'9')+ '.' ('0'..'9')* EXPONENT? FLOAT_TYPE_SUFFIX?
2887 | '.' ('0'..'9')+ EXPONENT? FLOAT_TYPE_SUFFIX?
2888 | ('0'..'9')+ EXPONENT FLOAT_TYPE_SUFFIX?
2889 | ('0'..'9')+ EXPONENT? FLOAT_TYPE_SUFFIX
2890 ;
2891
2892 FIXED_PT_LITERAL
2893 : FLOATING_PT_LITERAL
2894 ;
2895
2896 fragment
2897 EXPONENT : ('e' | 'E') (PLUS|MINUS)? ('0'..'9')+ ;
2898
2899 fragment
2900 FLOAT_TYPE_SUFFIX : ('f' | 'F' | 'd' | 'D') ;
2901
2902 WIDE_CHARACTER_LITERAL
2903 : 'L' CHARACTER_LITERAL
2904 ;
2905
2906 CHARACTER_LITERAL
2907 : '\'' ( ESCAPE_SEQUENCE | ~('\'' | '\\') ) '\''
2908 ;
2909
2910 WIDE_STRING_LITERAL
2911 : ('L' STRING_LITERAL(WS*))+
2912 ;
2913
2914 STRING_LITERAL
2915 : ('"' ( ESCAPE_SEQUENCE | ~('\\' | '"') )* '"'(WS*))+
2916 ;
2917
2918 BOOLEAN_LITERAL
2919 : 'TRUE'
2920 | 'FALSE'
2921 ;
2922
2923 fragment
2924 ESCAPE_SEQUENCE
2925 : '\\' ('b' | 't' | 'n' | 'f' | 'r' | '"' | '\'' | '\\' | 'v' | 'a' | '?')
2926 | UNICODE_ESCAPE
2927 | HEX_ESCAPE
2928 | OCTAL_ESCAPE
2929 ;
2930
2931 fragment
2932 OCTAL_ESCAPE
2933 : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
2934 | '\\' ('0'..'7') ('0'..'7')
2935 | '\\' ('0'..'7')
2936 ;
2937
2938 fragment
2939 UNICODE_ESCAPE
2940 : '\\' 'u' HEX_DIGIT? HEX_DIGIT? HEX_DIGIT? HEX_DIGIT
2941 ;
2942
2943 fragment
2944 HEX_ESCAPE
2945 : '\\' 'x' HEX_DIGIT? HEX_DIGIT? HEX_DIGIT
2946 ;
2947
2948 fragment
2949 LETTER
2950 : '\u0024'
2951 | '\u0041'..'\u005a'
2952 | '\u005f'
2953 | '\u0061'..'\u007a'
2954 | '\u00c0'..'\u00d6'
2955 | '\u00d8'..'\u00f6'
2956 | '\u00f8'..'\u00ff'
2957 | '\u0100'..'\u1fff'
2958 | '\u3040'..'\u318f'
2959 | '\u3300'..'\u337f'
2960 | '\u3400'..'\u3d2d'
2961 | '\u4e00'..'\u9fff'
2962 | '\uf900'..'\ufaff'
2963 ;
2964
2965 fragment
2966 ID_DIGIT
2967 : '\u0030'..'\u0039'
2968 | '\u0660'..'\u0669'
2969 | '\u06f0'..'\u06f9'
2970 | '\u0966'..'\u096f'
2971 | '\u09e6'..'\u09ef'
2972 | '\u0a66'..'\u0a6f'
2973 | '\u0ae6'..'\u0aef'
2974 | '\u0b66'..'\u0b6f'
2975 | '\u0be7'..'\u0bef'
2976 | '\u0c66'..'\u0c6f'
2977 | '\u0ce6'..'\u0cef'
2978 | '\u0d66'..'\u0d6f'
2979 | '\u0e50'..'\u0e59'
2980 | '\u0ed0'..'\u0ed9'
2981 | '\u1040'..'\u1049'
2982 ;
2983
2984 SEMICOLON: ';';
2985 COLON: ':';
2986 COMA: ',';
2987 LEFT_BRACE: '{';
2988 RIGHT_BRACE: '}';
2989 LEFT_BRACKET: '(';
2990 RIGHT_BRACKET: ')';
2991 LEFT_SQUARE_BRACKET: '[';
2992 RIGHT_SQUARE_BRACKET: ']';
2993 TILDE: '~';
2994 SLASH: '/';
2995 LEFT_ANG_BRACKET: '<';
2996 RIGHT_ANG_BRACKET: '>';
2997 STAR: '*';
2998 PLUS: '+';
2999 MINUS: '-';
3000 CARET: '^';
3001 AMPERSAND: '&';
3002 PIPE: '|';
3003 EQUAL: '=';
3004 PERCENT: '%';
3005 AT: '@';
3006
3007 DOUBLE_COLON: '::';
3008 RIGHT_SHIFT: '>>';
3009 LEFT_SHIFT: '<<';
3010
3011 KW_SETRAISES: 'setraises';
3012 KW_OUT: 'out';
3013 KW_EMITS: 'emits';
3014 KW_STRING: 'string';
3015 KW_SWITCH: 'switch';
3016 KW_PUBLISHES: 'publishes';
3017 KW_TYPEDEF: 'typedef';
3018 KW_USES: 'uses';
3019 KW_PRIMARYKEY: 'primarykey';
3020 KW_CUSTOM: 'custom';
3021 KW_OCTET: 'octet';
3022 KW_SEQUENCE: 'sequence';
3023 KW_IMPORT: 'import';
3024 KW_STRUCT: 'struct';
3025 KW_NATIVE: 'native';
3026 KW_READONLY: 'readonly';
3027 KW_FINDER: 'finder';
3028 KW_RAISES: 'raises';
3029 KW_VOID: 'void';
3030 KW_PRIVATE: 'private';
3031 KW_EVENTTYPE: 'eventtype';
3032 KW_WCHAR: 'wchar';
3033 KW_IN: 'in';
3034 KW_DEFAULT: 'default';
3035 KW_PUBLIC: 'public';
3036 KW_SHORT: 'short';
3037 KW_LONG: 'long';
3038 KW_ENUM: 'enum';
3039 KW_WSTRING: 'wstring';
3040 KW_CONTEXT: 'context';
3041 KW_HOME: 'home';
3042 KW_FACTORY: 'factory';
3043 KW_EXCEPTION: 'exception';
3044 KW_GETRAISES: 'getraises';
3045 KW_CONST: 'const';
3046 KW_VALUEBASE: 'ValueBase';
3047 KW_VALUETYPE: 'valuetype';
3048 KW_SUPPORTS: 'supports';
3049 KW_MODULE: 'module';
3050 KW_OBJECT: 'Object';
3051 KW_TRUNCATABLE: 'truncatable';
3052 KW_UNSIGNED: 'unsigned';
3053 KW_FIXED: 'fixed';
3054 KW_UNION: 'union';
3055 KW_ONEWAY: 'oneway';
3056 KW_ANY: 'any';
3057 KW_CHAR: 'char';
3058 KW_CASE: 'case';
3059 KW_FLOAT: 'float';
3060 KW_BOOLEAN: 'boolean';
3061 KW_MULTIPLE: 'multiple';
3062 KW_ABSTRACT: 'abstract';
3063 KW_INOUT: 'inout';
3064 KW_PROVIDES: 'provides';
3065 KW_CONSUMES: 'consumes';
3066 KW_DOUBLE: 'double';
3067 KW_TYPEPREFIX: 'typeprefix';
3068 KW_TYPEID: 'typeid';
3069 KW_ATTRIBUTE: 'attribute';
3070 KW_LOCAL: 'local';
3071 KW_MANAGES: 'manages';
3072 KW_INTERFACE: 'interface';
3073 KW_COMPONENT: 'component';
3074 KW_SET: 'set';
3075 KW_MAP: 'map';
3076 KW_BITFIELD: 'bitfield';
3077 KW_BITSET: 'bitset';
3078 KW_BITMASK: 'bitmask';
3079 KW_INT8: 'int8';
3080 KW_UINT8: 'uint8';
3081 KW_INT16: 'int16';
3082 KW_UINT16: 'uint16';
3083 KW_INT32: 'int32';
3084 KW_UINT32: 'uint32';
3085 KW_INT64: 'int64';
3086 KW_UINT64: 'uint64';
3087 KW_AT_ANNOTATION: '@annotation';
3088
3089 ID
3090 : LETTER (LETTER|ID_DIGIT)*
3091 ;
3092
3093 WS
3094 : (' ' | '\r' | '\t' | '\u000C' | '\n') -> channel(HIDDEN)
3095 ;
3096
3097 PREPROC_DIRECTIVE
3098 :
3099 '#'
3100 (~'\n')* '\n'
3101 {
3102 ctx.processPreprocessorLine(new String(getText()), getLine());
3103 skip();
3104 //newline();
3105 }
3106 ;
3107
3108 COMMENT
3109 : '/*' .*? '*/' -> channel(HIDDEN)
3110 ;
3111
3112 LINE_COMMENT
3113 : '//' ~('\n' | '\r')* '\r'? '\n' -> channel(HIDDEN)
3114 ;
3115
3116 // [EOF] IDL.g