From ccdb1b26befacf97840946c1a630b4e026713527 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Tue, 3 Oct 2023 12:47:50 +0200 Subject: [PATCH 1/5] Refs #19115: add TemplateGroup to string_type, wide_string_type, array_declarator, map_type, and sequence_type Signed-off-by: JLBuenoLopez-eProsima --- .../com/eprosima/idl/parser/grammar/IDL.g4 | 118 ++++++++++++++---- 1 file changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 index 3ee6f930..7ec54204 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -388,7 +388,6 @@ interface_body [ExportContainer ec] returns [TemplateGroup elTemplates] */ export [Vector annotations] returns [Pair, TemplateGroup> etg = null] @init { - // TODO Cambiar esto. No me gusta la forma. Vector vector = new Vector(); Pair, TemplateGroup> tetg = null; Pair cetg = null; @@ -1879,27 +1878,42 @@ enumerator [EnumTypeCode enumTP] } ; -sequence_type returns [SequenceTypeCode typecode = null] +sequence_type returns [Pair returnPair = null] @init { TypeCode type = null; String maxsize = null; Definition def = null; + SequenceTypeCode typecode = null; + TemplateGroup sequenceTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if (tmanager != null) { + sequenceTemplates = tmanager.createTemplateGroup("sequence_type"); + } + } } : ( (KW_SEQUENCE) LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } COMA positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET | (KW_SEQUENCE) LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } RIGHT_ANG_BRACKET ) { - if(type != null) - { - $typecode = ctx.createSequenceTypeCode(maxsize); - $typecode.setContentTypeCode(type); - } - else if (def != null) - { - $typecode = ctx.createSequenceTypeCode(maxsize); - $typecode.setContentDefinition(def); - } + if(type != null) + { + typecode = ctx.createSequenceTypeCode(maxsize); + typecode.setContentTypeCode(type); + } + else if (def != null) + { + typecode = ctx.createSequenceTypeCode(maxsize); + typecode.setContentDefinition(def); + } + + if(sequenceTemplates != null) + { + sequenceTemplates.setAttribute("sequence", typecode); + sequenceTemplates.setAttribute("ctx", ctx); + } + + $returnPair = new Pair(typecode, sequenceTemplates); } ; @@ -1925,13 +1939,20 @@ set_type returns [SetTypeCode typecode = null] } ; -map_type returns [MapTypeCode typecode = null] +map_type returns [Pair returnPair = null] @init { TypeCode keyType = null; TypeCode valueType = null; Definition keyDef = null; Definition valueDef = null; String maxsize = null; + MapTypeCode typecode = null; + TemplateGroup mapTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) { + mapTemplates = tmanager.createTemplateGroup("map_type"); + } + } } : KW_MAP LEFT_ANG_BRACKET simple_type_spec[null] { @@ -1946,52 +1967,95 @@ map_type returns [MapTypeCode typecode = null] (COMA positive_int_const { maxsize=$positive_int_const.literalStr; } )? RIGHT_ANG_BRACKET { - $typecode = ctx.createMapTypeCode(maxsize); + typecode = ctx.createMapTypeCode(maxsize); if (keyType != null) { - $typecode.setKeyTypeCode(keyType); + typecode.setKeyTypeCode(keyType); } else if (keyDef != null) { - $typecode.setKeyDefinition(keyDef); + typecode.setKeyDefinition(keyDef); } if (valueType != null) { - $typecode.setValueTypeCode(valueType); + typecode.setValueTypeCode(valueType); } else if (valueDef != null) { - $typecode.setValueDefinition(valueDef); + typecode.setValueDefinition(valueDef); + } + + if(mapTemplates != null) { + mapTemplates.setAttribute("map", typecode); + mapTemplates.setAttribute("ctx", ctx); } + + $returnPair = new Pair(typecode, mapTemplates); } ; -string_type returns [TypeCode typecode = null] +string_type returns [Pair returnPair = null] @init{ String maxsize = null; + TypeCode typecode = null; + TemplateGroup stringTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) { + stringTemplates = tmanager.createTemplateGroup("string_type"); + } + } } : ( KW_STRING LEFT_ANG_BRACKET positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET | KW_STRING ) - {$typecode = ctx.createStringTypeCode(Kind.KIND_STRING, maxsize);} + { + typecode = ctx.createStringTypeCode(Kind.KIND_STRING, maxsize); + if (stringTemplates != null) { + stringTemplates.setAttribute("string", typecode); + stringTemplates.setAttribute("ctx", ctx); + } + + $returnPair = new Pair(typecode, stringTemplates); + } ; -wide_string_type returns [TypeCode typecode = null] +wide_string_type returns [Pair returnPair = null] @init { String maxsize = null; + TypeCode typecode = null; + TemplateGroup wstringTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) { + wstringTemplates = tmanager.createTemplateGroup("wide_string_type"); + } + } } : ( KW_WSTRING LEFT_ANG_BRACKET positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET | KW_WSTRING ) - {$typecode = ctx.createStringTypeCode(Kind.KIND_WSTRING, maxsize);} + { + typecode = ctx.createStringTypeCode(Kind.KIND_WSTRING, maxsize); + if (wstringTemplates != null) { + wstringTemplates.setAttribute("wstring", typecode); + wstringTemplates.setAttribute("ctx", ctx); + } + + $returnPair = new Pair(typecode, wstringTemplates); + } ; -array_declarator returns [Pair, ContainerTypeCode> pair = null] +array_declarator returns [Pair, ContainerTypeCode>, TemplateGroup> pair = null] @init { Token tk = _input.LT(1); ArrayTypeCode typecode = ctx.createArrayTypeCode(); + TemplateGroup arrayTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) { + arrayTemplates = tmanager.createTemplateGroup("array_declarator"); + } + } } : ID ( @@ -2001,8 +2065,14 @@ array_declarator returns [Pair, ContainerTypeCode> pair = nu } )+ { + if(arrayTemplates != null) + { + arrayTemplates.setAttribute("array", typecode); + arrayTemplates.setAttribute("ctx", ctx); + } Pair p = new Pair(tk.getText(), tk); - $pair = new Pair, ContainerTypeCode>(p, typecode); + Pair, ContainerTypeCode> pp = new Pair, ContainerTypeCode>(p, typecode); + $pair = new Pair, ContainerTypeCode>, TemplateGroup>(pp, arrayTemplates); } ; From ee8f57a361febffff585aadb8cd273df2c216d11 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Tue, 3 Oct 2023 13:07:18 +0200 Subject: [PATCH 2/5] Refs #19115: extend const_type Signed-off-by: JLBuenoLopez-eProsima --- .../com/eprosima/idl/parser/grammar/IDL.g4 | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 index 7ec54204..b49eefcf 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -564,18 +564,19 @@ const_decl [AnnotationDeclaration annotation] returns [Pair returnPair = null] @init{ Pair pair = null; + TypeCode typecode = null; } - : integer_type { $typecode = $integer_type.typecode; } - | char_type { $typecode = $char_type.typecode; } - | wide_char_type { $typecode = $wide_char_type.typecode; } - | boolean_type { $typecode = $boolean_type.typecode; } - | floating_pt_type { $typecode = $floating_pt_type.typecode; } - | string_type { $typecode = $string_type.typecode; } - | wide_string_type { $typecode = $wide_string_type.typecode; } - | any_type { $typecode = $any_type.typecode; } + : integer_type { $returnPair = new Pair($integer_type.typecode, null); } + | char_type { $returnPair = new Pair($char_type.typecode, null); } + | wide_char_type { $returnPair = new Pair($wide_char_type.typecode, null); } + | boolean_type { $returnPair = new Pair($boolean_type.typecode, null); } + | floating_pt_type { $returnPair = new Pair($floating_pt_type.typecode, null); } + | string_type { $returnPair = $string_type.returnPair; } + | wide_string_type { $returnPair = $wide_string_type.returnPair; } + | any_type { $returnPair = new Pair($any_type.typecode, null); } | fixed_pt_const_type | scoped_name { @@ -584,21 +585,23 @@ const_type[AnnotationDeclaration annotation] returns [TypeCode typecode = null] // Find first at annotation scope if ($annotation != null) { - $typecode = $annotation.getTypeCode(pair.first()); + typecode = $annotation.getTypeCode(pair.first()); } // Find typecode in the global map if not found. - if ($typecode == null) + if (typecode == null) { - $typecode = ctx.getTypeCode(pair.first()); + typecode = ctx.getTypeCode(pair.first()); } - if($typecode == null) + if(typecode == null) { throw new ParseException(pair.second(), "was not defined previously"); } + + $returnPair = new Pair(typecode, null); } - | octet_type { $typecode = $octet_type.typecode; } + | octet_type { $returnPair = new Pair($octet_type.typecode, null); } ; /* EXPRESSIONS */ From 7bfc21587b491327957f5ab380e2d8f541f38e85 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Tue, 3 Oct 2023 15:32:58 +0200 Subject: [PATCH 3/5] Refs #19115: support const_decl string/wstring templates Signed-off-by: JLBuenoLopez-eProsima --- .../com/eprosima/idl/parser/grammar/IDL.g4 | 23 +++++++++++-------- .../idl/parser/tree/ConstDeclaration.java | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 index b49eefcf..eeaeea30 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -528,6 +528,7 @@ const_decl [AnnotationDeclaration annotation] returns [Pair(constDecl, constTemplates); @@ -926,11 +931,11 @@ base_type_spec returns [TypeCode typecode = null] ; template_type_spec returns [TypeCode typecode = null] - : sequence_type { $typecode=$sequence_type.typecode; } + : sequence_type { $typecode=$sequence_type.returnPair.first(); } | set_type { $typecode=$set_type.typecode; } - | map_type { $typecode=$map_type.typecode; } - | string_type { $typecode=$string_type.typecode; } - | wide_string_type { $typecode=$wide_string_type.typecode; } + | map_type { $typecode=$map_type.returnPair.first(); } + | string_type { $typecode=$string_type.returnPair.first(); } + | wide_string_type { $typecode=$wide_string_type.returnPair.first(); } | fixed_pt_type ; @@ -1002,7 +1007,7 @@ simple_declarator returns [Pair, ContainerTypeCode> ret = nu ; complex_declarator returns [Pair, ContainerTypeCode> ret = null] - : array_declarator { $ret=$array_declarator.pair; } + : array_declarator { $ret=$array_declarator.pair.first(); } ; floating_pt_type returns [TypeCode typecode = null] @@ -1256,7 +1261,7 @@ annotation_member [AnnotationDeclaration annotation] : const_type[annotation] simple_declarator ( KW_DEFAULT const_exp { literalStr=$const_exp.literalStr; } )? SEMICOLON { - if(!$annotation.addMember(new AnnotationMember($simple_declarator.ret.first().first(), $const_type.typecode, literalStr))) + if(!$annotation.addMember(new AnnotationMember($simple_declarator.ret.first().first(), $const_type.returnPair.first(), literalStr))) { throw new ParseException($simple_declarator.ret.first().second(), $simple_declarator.ret.first().first() + " was defined previously"); } @@ -2342,8 +2347,8 @@ param_type_spec returns [TypeCode typecode = null, Definition def = null] Pair pair = null; } : base_type_spec { $typecode=$base_type_spec.typecode; } - | string_type { $typecode=$string_type.typecode; } - | wide_string_type { $typecode=$wide_string_type.typecode; } + | string_type { $typecode=$string_type.returnPair.first(); } + | wide_string_type { $typecode=$wide_string_type.returnPair.first(); } | scoped_name { pair=$scoped_name.pair; diff --git a/src/main/java/com/eprosima/idl/parser/tree/ConstDeclaration.java b/src/main/java/com/eprosima/idl/parser/tree/ConstDeclaration.java index 5d07bdd4..8ec3a8e7 100644 --- a/src/main/java/com/eprosima/idl/parser/tree/ConstDeclaration.java +++ b/src/main/java/com/eprosima/idl/parser/tree/ConstDeclaration.java @@ -19,7 +19,7 @@ import org.antlr.v4.runtime.Token; -public class ConstDeclaration extends TreeNode implements Definition, Export +public class ConstDeclaration extends DefinitionContainer implements Definition, Export { public ConstDeclaration(String scopeFile, boolean isInScope, String scope, String name, TypeCode typecode, String value, Token token) { From 547274d0e4ffd741147ee5cf801185297501e2a6 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Wed, 4 Oct 2023 13:53:59 +0200 Subject: [PATCH 4/5] Refs #19115: propagate collection types Signed-off-by: JLBuenoLopez-eProsima --- .../com/eprosima/idl/parser/grammar/IDL.g4 | 311 ++++++++++++------ 1 file changed, 213 insertions(+), 98 deletions(-) diff --git a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 index eeaeea30..10c8966a 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -836,28 +836,32 @@ type_declarator [AnnotationDeclaration annotation] returns [Pair(); for(int count = 0; count < $declarators.ret.size(); ++count) { - typedefTypecode = ctx.createAliasTypeCode(ctx.getScope(), $declarators.ret.get(count).first().first()); + typedefTypecode = ctx.createAliasTypeCode(ctx.getScope(), $declarators.ret.get(count).first().first().first()); - if($declarators.ret.get(count).second() != null) + if($declarators.ret.get(count).first().second() != null) { // Array declaration - $declarators.ret.get(count).second().setContentTypeCode($type_spec.typecode); - typedefTypecode.setContentTypeCode($declarators.ret.get(count).second()); + $declarators.ret.get(count).first().second().setContentTypeCode($type_spec.returnPair.first()); + typedefTypecode.setContentTypeCode($declarators.ret.get(count).first().second()); } else { // Simple declaration - typedefTypecode.setContentTypeCode($type_spec.typecode); + typedefTypecode.setContentTypeCode($type_spec.returnPair.first()); } if(typedefTemplates != null) { typedefTemplates.setAttribute("typedefs", typedefTypecode); + if($declarators.ret.get(count).second() != null) + { + typedefTemplates.setAttribute("declarator_type", $declarators.ret.get(count).second()); + } } vector.add(typedefTypecode); @@ -865,6 +869,10 @@ type_declarator [AnnotationDeclaration annotation] returns [Pair, TemplateGroup>(vector, typedefTemplates); @@ -872,17 +880,18 @@ type_declarator [AnnotationDeclaration annotation] returns [Pair returnPair = null, Definition def = null] + : simple_type_spec[annotation] { $returnPair=$simple_type_spec.returnPair; $def=$simple_type_spec.def; } | constr_type_spec ; -simple_type_spec [AnnotationDeclaration annotation] returns [TypeCode typecode = null, Definition def = null] +simple_type_spec [AnnotationDeclaration annotation] returns [Pair returnPair = null, Definition def = null] @init { Pair pair = null; + TypeCode typecode = null; } - : base_type_spec { $typecode=$base_type_spec.typecode; } - | template_type_spec { $typecode=$template_type_spec.typecode; } + : base_type_spec { $returnPair= new Pair($base_type_spec.typecode, null); } + | template_type_spec { $returnPair=$template_type_spec.returnPair; } | scoped_name { pair=$scoped_name.pair; @@ -890,16 +899,17 @@ simple_type_spec [AnnotationDeclaration annotation] returns [TypeCode typecode = // Look for it first on annotation if ($annotation != null) { - $typecode = $annotation.getTypeCode(pair.first()); + typecode = $annotation.getTypeCode(pair.first()); } // Find typecode in the global map. - if($typecode == null) + if(typecode == null) { - $typecode = ctx.getTypeCode(pair.first()); + typecode = ctx.getTypeCode(pair.first()); } + $returnPair = new Pair(typecode, null); - if($typecode == null) + if(typecode == null) { // Maybe an interface $def = ctx.getInterface(pair.first()); @@ -930,12 +940,12 @@ base_type_spec returns [TypeCode typecode = null] | value_base_type ; -template_type_spec returns [TypeCode typecode = null] - : sequence_type { $typecode=$sequence_type.returnPair.first(); } - | set_type { $typecode=$set_type.typecode; } - | map_type { $typecode=$map_type.returnPair.first(); } - | string_type { $typecode=$string_type.returnPair.first(); } - | wide_string_type { $typecode=$wide_string_type.returnPair.first(); } +template_type_spec returns [Pair returnPair = null] + : sequence_type { $returnPair=new Pair($sequence_type.returnPair.first(), $sequence_type.returnPair.second()); } + | set_type { $returnPair=new Pair($set_type.returnPair.first(), $set_type.returnPair.second()); } + | map_type { $returnPair=new Pair($map_type.returnPair.first(), $map_type.returnPair.second()); } + | string_type { $returnPair=$string_type.returnPair; } + | wide_string_type { $returnPair=$wide_string_type.returnPair; } | fixed_pt_type ; @@ -947,7 +957,7 @@ constr_type_spec returns [Pair, TemplateGroup> returnPair = nul | bitmask_type ; -declarators returns [Vector, ContainerTypeCode>> ret = new Vector, ContainerTypeCode>>()] +declarators returns [Vector, ContainerTypeCode>, TemplateGroup>> ret = new Vector, ContainerTypeCode>, TemplateGroup>>()] : declarator { if($declarator.ret != null) @@ -983,8 +993,8 @@ simple_declarators returns [Vector, ContainerTypeCode>> )* ; -declarator returns [Pair, ContainerTypeCode> ret = null] - : simple_declarator { $ret=$simple_declarator.ret; } +declarator returns [Pair, ContainerTypeCode>, TemplateGroup> ret = null] + : simple_declarator { $ret=new Pair, ContainerTypeCode>, TemplateGroup>($simple_declarator.ret, null); } | complex_declarator { $ret=$complex_declarator.ret; } ; @@ -1006,8 +1016,8 @@ simple_declarator returns [Pair, ContainerTypeCode> ret = nu } ; -complex_declarator returns [Pair, ContainerTypeCode> ret = null] - : array_declarator { $ret=$array_declarator.pair.first(); } +complex_declarator returns [Pair, ContainerTypeCode>, TemplateGroup> ret = null] + : array_declarator { $ret=$array_declarator.pair; } ; floating_pt_type returns [TypeCode typecode = null] @@ -1253,13 +1263,13 @@ annotation_body [AnnotationDeclaration annotation] )* ; -annotation_member [AnnotationDeclaration annotation] +annotation_member [AnnotationDeclaration annotation] returns [Pair returnPair = null] @init { String literalStr = null; } : - const_type[annotation] simple_declarator ( KW_DEFAULT const_exp { literalStr=$const_exp.literalStr; } )? SEMICOLON + const_type[annotation] {$returnPair=$const_type.returnPair;} simple_declarator ( KW_DEFAULT const_exp { literalStr=$const_exp.literalStr; } )? SEMICOLON { if(!$annotation.addMember(new AnnotationMember($simple_declarator.ret.first().first(), $const_type.returnPair.first(), literalStr))) { @@ -1520,6 +1530,10 @@ struct_type returns [Pair, TemplateGroup> returnPair = null, St structTemplates = tmanager.createTemplateGroup("struct_type"); structTemplates.setAttribute("ctx", ctx); structTemplates.setAttribute("struct", structTP); + if($member_list.tg != null) + { + structTemplates.setAttribute("member_list", $member_list.tg); + } } } // Return the returned data. @@ -1559,23 +1573,28 @@ struct_type returns [Pair, TemplateGroup> returnPair = null, St } ; -member_list [StructTypeCode structTP] +member_list [StructTypeCode structTP] returns [TemplateGroup tg = null] : ( member_def - { - if($member_def.ret != null) - { - for(Pair, Member> pair : $member_def.ret) - { - if(!$structTP.addMember(pair.second())) - throw new ParseException(pair.first().second(), pair.first().first() + " was defined previously"); - } - } - } + { + if($member_def.ret != null) + { + for(Pair, TemplateGroup>, Member> pair : $member_def.ret) + { + if(!$structTP.addMember(pair.second())) + throw new ParseException(pair.first().first().second(), pair.first().first().first() + " was defined previously"); + + if(pair.first().second() != null) + { + $tg = pair.first().second(); + } + } + } + } )* ; -member_def returns [Vector, Member>> ret = null] +member_def returns [Vector, TemplateGroup>, Member>> ret = null] : member { $ret=$member.ret; } | annotation_appl defret=member_def { @@ -1583,7 +1602,7 @@ member_def returns [Vector, Member>> ret = null] { $ret=$defret.ret; - for(Pair, Member> pair : $ret) + for(Pair, TemplateGroup>, Member> pair : $ret) { if(pair.second() != null) pair.second().addAnnotation(ctx, $annotation_appl.annotation); @@ -1592,29 +1611,57 @@ member_def returns [Vector, Member>> ret = null] } ; -member returns [Vector, Member>> ret = new Vector, Member>>()] +member returns [Vector, TemplateGroup>, Member>> ret = new Vector, TemplateGroup>, Member>>()] +@init{ + TemplateGroup memberTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) + { + memberTemplates = tmanager.createTemplateGroup("member_type"); + } + } +} : type_spec[null] declarators SEMICOLON { - if($type_spec.typecode!=null) + if($type_spec.returnPair.first()!=null) { + if(memberTemplates != null) + { + memberTemplates.setAttribute("ctx", ctx); + if($type_spec.returnPair.second()!=null) + { + memberTemplates.setAttribute("type_member", $type_spec.returnPair.second()); + } + } + for(int count = 0; count < $declarators.ret.size(); ++count) { Member member = null; - if($declarators.ret.get(count).second() != null) + if($declarators.ret.get(count).first().second() != null) { // Array declaration - $declarators.ret.get(count).second().setContentTypeCode($type_spec.typecode); - member = new Member($declarators.ret.get(count).second(), $declarators.ret.get(count).first().first()); + $declarators.ret.get(count).first().second().setContentTypeCode($type_spec.returnPair.first()); + member = new Member($declarators.ret.get(count).first().second(), $declarators.ret.get(count).first().first().first()); } else { // Simple declaration - member = new Member($type_spec.typecode, $declarators.ret.get(count).first().first()); + member = new Member($type_spec.returnPair.first(), $declarators.ret.get(count).first().first().first()); + } + + if(memberTemplates != null) + { + memberTemplates.setAttribute("member", member); + if($declarators.ret.get(count).second()!=null) + { + memberTemplates.setAttribute("declarators", $declarators.ret.get(count).second()); + } } - $ret.add(new Pair, Member>($declarators.ret.get(count).first(), member)); + Pair, TemplateGroup> pair = new Pair, TemplateGroup>($declarators.ret.get(count).first().first(), memberTemplates); + $ret.add(new Pair, TemplateGroup>, Member>(pair, member)); } } } @@ -1692,6 +1739,10 @@ union_type [ArrayList defs] returns [Pair, Template unionTemplates = tmanager.createTemplateGroup("union_type"); unionTemplates.setAttribute("ctx", ctx); unionTemplates.setAttribute("union", unionTP); + if($switch_body.tg != null) + { + unionTemplates.setAttribute("switch_type", $switch_body.tg); + } } } @@ -1749,19 +1800,19 @@ switch_type_spec returns [TypeCode typecode = null] } ; -switch_body [UnionTypeCode unionTP] +switch_body [UnionTypeCode unionTP] returns [TemplateGroup tg = null] @init { } - : case_stmt_list[unionTP] + : case_stmt_list[unionTP] {$tg=$case_stmt_list.tg;} ; -case_stmt_list [UnionTypeCode unionTP] +case_stmt_list [UnionTypeCode unionTP] returns [TemplateGroup tg = null] @init { } - : (case_stmt[unionTP])+ + : (case_stmt[unionTP] {$tg=$case_stmt.tg;})+ ; -case_stmt [UnionTypeCode unionTP] +case_stmt [UnionTypeCode unionTP] returns [TemplateGroup tg = null] @init { List labels = new ArrayList(); @@ -1787,12 +1838,14 @@ case_stmt [UnionTypeCode unionTP] if(ret == -1) { - throw new ParseException($element_spec.ret.first().second(), " is already defined."); + throw new ParseException($element_spec.ret.first().first().second(), " is already defined."); } else if(ret == -2) { - throw new ParseException($element_spec.ret.first().second(), " is also a default attribute. Another was defined previously."); + throw new ParseException($element_spec.ret.first().first().second(), " is also a default attribute. Another was defined previously."); } + + $tg=$element_spec.ret.first().second(); } } ; @@ -1802,24 +1855,52 @@ case_stmt [UnionTypeCode unionTP] // | KW_DEFAULT COLON // ; -element_spec [List labels, boolean isDefault] returns [Pair, UnionMember> ret = null] +element_spec [List labels, boolean isDefault] returns [Pair, TemplateGroup>, UnionMember> ret = null] +@init{ + TemplateGroup elementTemplates = null; + if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { + if(tmanager != null) + { + elementTemplates = tmanager.createTemplateGroup("element_type"); + } + } +} : type_spec[null] declarator { - if($type_spec.typecode != null) + if($type_spec.returnPair.first() != null) { + if(elementTemplates != null) + { + elementTemplates.setAttribute("ctx", ctx); + if($type_spec.returnPair.second()!=null) + { + elementTemplates.setAttribute("type_element", $type_spec.returnPair.second()); + } + } + UnionMember member = null; - if($declarator.ret.second() != null) + if($declarator.ret.first().second() != null) { - $declarator.ret.second().setContentTypeCode($type_spec.typecode); - member = new UnionMember($declarator.ret.second(), $declarator.ret.first().first(), labels, isDefault); + $declarator.ret.first().second().setContentTypeCode($type_spec.returnPair.first()); + member = new UnionMember($declarator.ret.first().second(), $declarator.ret.first().first().first(), labels, isDefault); } else { - member = new UnionMember($type_spec.typecode, $declarator.ret.first().first(), labels, isDefault); + member = new UnionMember($type_spec.returnPair.first(), $declarator.ret.first().first().first(), labels, isDefault); + } + + if(elementTemplates != null) + { + elementTemplates.setAttribute("element", member); + if($declarator.ret.second()!=null) + { + elementTemplates.setAttribute("declarator", $declarator.ret.second()); + } } - $ret = new Pair, UnionMember>($declarator.ret.first(), member); + Pair, TemplateGroup> pair = new Pair, TemplateGroup>($declarator.ret.first().first(), elementTemplates); + $ret = new Pair, TemplateGroup>, UnionMember>(pair, member); } } ; @@ -1893,6 +1974,7 @@ sequence_type returns [Pair returnPair = null] Definition def = null; SequenceTypeCode typecode = null; TemplateGroup sequenceTemplates = null; + TemplateGroup template = null; if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { if (tmanager != null) { sequenceTemplates = tmanager.createTemplateGroup("sequence_type"); @@ -1900,9 +1982,9 @@ sequence_type returns [Pair returnPair = null] } } : ( (KW_SEQUENCE) - LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } COMA positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET + 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 | (KW_SEQUENCE) - LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } RIGHT_ANG_BRACKET ) + 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 ) { if(type != null) { @@ -1919,31 +2001,37 @@ sequence_type returns [Pair returnPair = null] { sequenceTemplates.setAttribute("sequence", typecode); sequenceTemplates.setAttribute("ctx", ctx); + if (template != null) { + sequenceTemplates.setAttribute("type_sequence", template); + } } $returnPair = new Pair(typecode, sequenceTemplates); } ; -set_type returns [SetTypeCode typecode = null] +set_type returns [Pair returnPair = null] @init { TypeCode type = null; + SetTypeCode typecode = null; String maxsize = null; Definition def = null; } : ( KW_SET - LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } COMA positive_int_const { maxsize=$positive_int_const.literalStr; } RIGHT_ANG_BRACKET + 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 | KW_SET - LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.typecode; def=$simple_type_spec.def; } RIGHT_ANG_BRACKET ) + LEFT_ANG_BRACKET simple_type_spec[null] { type=$simple_type_spec.returnPair.first(); def=$simple_type_spec.def; } RIGHT_ANG_BRACKET ) { - $typecode = ctx.createSetTypeCode(maxsize); + typecode = ctx.createSetTypeCode(maxsize); if (type != null) { - $typecode.setContentTypeCode(type); + typecode.setContentTypeCode(type); } else if (def != null) { - $typecode.setContentDefinition(def); + typecode.setContentDefinition(def); } + + $returnPair = new Pair(typecode, $simple_type_spec.returnPair.second()); } ; @@ -1956,6 +2044,8 @@ map_type returns [Pair returnPair = null] String maxsize = null; MapTypeCode typecode = null; TemplateGroup mapTemplates = null; + TemplateGroup keyTemplate = null; + TemplateGroup valueTemplate = null; if(ctx.isInScopedFile() || ctx.isScopeLimitToAll()) { if(tmanager != null) { mapTemplates = tmanager.createTemplateGroup("map_type"); @@ -1964,12 +2054,14 @@ map_type returns [Pair returnPair = null] } : KW_MAP LEFT_ANG_BRACKET simple_type_spec[null] { - keyType=$simple_type_spec.typecode; + keyType=$simple_type_spec.returnPair.first(); + keyTemplate=$simple_type_spec.returnPair.second(); keyDef=$simple_type_spec.def; } COMA simple_type_spec[null] { - valueType=$simple_type_spec.typecode; + valueType=$simple_type_spec.returnPair.first(); + valueTemplate=$simple_type_spec.returnPair.second(); valueDef=$simple_type_spec.def; } (COMA positive_int_const { maxsize=$positive_int_const.literalStr; } )? @@ -1998,6 +2090,14 @@ map_type returns [Pair returnPair = null] if(mapTemplates != null) { mapTemplates.setAttribute("map", typecode); mapTemplates.setAttribute("ctx", ctx); + if(keyTemplate != null) + { + mapTemplates.setAttribute("key_type", keyTemplate); + } + if(valueTemplate != null) + { + mapTemplates.setAttribute("value_type", valueTemplate); + } } $returnPair = new Pair(typecode, mapTemplates); @@ -2090,7 +2190,7 @@ fixed_array_size returns [String literalStr = null] RIGHT_SQUARE_BRACKET ; -attr_decl returns [Vector, TypeCode>> ret = null, Vector, Definition>> retDef = null] +attr_decl returns [Vector, TemplateGroup>, TypeCode>> ret = null, Vector, TemplateGroup>, Definition>> retDef = null] : readonly_attr_spec | attr_spec { $ret=$attr_spec.ret; $retDef=$attr_spec.retDef; } ; @@ -2163,32 +2263,37 @@ op_decl [Vector annotations] returns [Pair operationTemplates = tmanager.createTemplateGroup("operation"); } TemplateGroup tpl = null; + TemplateGroup template = null; String name = ""; Token tk = null, tkoneway = null; TypeCode retType = null; Vector> exceptions = null; } : ( op_attribute { tkoneway=$op_attribute.token; } )? - op_type_spec { retType=$op_type_spec.typecode; } + op_type_spec { retType=$op_type_spec.returnPair.first(); template=$op_type_spec.returnPair.second();} { tk = _input.LT(1); name += tk.getText(); } ID { - // Create the Operation object. - operationObject = ctx.createOperation(name, tk); + // Create the Operation object. + operationObject = ctx.createOperation(name, tk); - // Add annotations. - for(Annotation annotation : annotations) - operationObject.addAnnotation(ctx, annotation); + // Add annotations. + for(Annotation annotation : annotations) + operationObject.addAnnotation(ctx, annotation); - if(operationTemplates != null) - { - operationTemplates.setAttribute("ctx", ctx); - // Set the the interface object to the TemplateGroup of the module. - operationTemplates.setAttribute("operation", operationObject); - } + if(operationTemplates != null) + { + operationTemplates.setAttribute("ctx", ctx); + // Set the the interface object to the TemplateGroup of the module. + operationTemplates.setAttribute("operation", operationObject); + if(template != null) + { + operationTemplates.setAttribute("operation_type", template); + } + } // Set return type operationObject.setRettype(retType); @@ -2238,8 +2343,8 @@ op_attribute returns [Token token = null] : KW_ONEWAY { $token = tk;} ; -op_type_spec returns [TypeCode typecode = null, Definition def = null] - : param_type_spec { $typecode=$param_type_spec.typecode; $def=$param_type_spec.def; } +op_type_spec returns [Pair returnPair = null, Definition def = null] + : param_type_spec { $returnPair=$param_type_spec.returnPair; $def=$param_type_spec.def; } | KW_VOID ; @@ -2281,6 +2386,7 @@ param_decl_list [Operation operation, TemplateGroup tpl] param_decl returns [Pair returnPair = null] @init{ TemplateGroup paramTemplate = null; + TemplateGroup template = null; if(tmanager != null) { paramTemplate = tmanager.createTemplateGroup("param"); } @@ -2289,7 +2395,7 @@ param_decl returns [Pair returnPair = null] String literalStr = _input.LT(1).getText(); } : ('in' | 'out' | 'inout') - param_type_spec { typecode=$param_type_spec.typecode; definition=$param_type_spec.def; } + param_type_spec { typecode=$param_type_spec.returnPair.first(); template=$param_type_spec.returnPair.second(); definition=$param_type_spec.def; } simple_declarator { if(typecode != null) @@ -2304,6 +2410,10 @@ param_decl returns [Pair returnPair = null] if(paramTemplate != null) { paramTemplate.setAttribute("parameter", param); + if(template != null) + { + paramTemplate.setAttribute("parameter_type", template); + } } $returnPair = new Pair(param, paramTemplate); } @@ -2319,6 +2429,7 @@ param_decl returns [Pair returnPair = null] if(paramTemplate != null) { paramTemplate.setAttribute("parameter", param); + paramTemplate.setAttribute("parameter_type", template); } $returnPair = new Pair(param, paramTemplate); } @@ -2342,20 +2453,22 @@ context_expr : KW_CONTEXT LEFT_BRACKET STRING_LITERAL ( COMA STRING_LITERAL )* RIGHT_BRACKET ; -param_type_spec returns [TypeCode typecode = null, Definition def = null] +param_type_spec returns [Pair returnPair = null, Definition def = null] @init{ Pair pair = null; + TypeCode typecode = null; } - : base_type_spec { $typecode=$base_type_spec.typecode; } - | string_type { $typecode=$string_type.returnPair.first(); } - | wide_string_type { $typecode=$wide_string_type.returnPair.first(); } + : base_type_spec { $returnPair=new Pair($base_type_spec.typecode, null); } + | string_type { $returnPair=$string_type.returnPair; } + | wide_string_type { $returnPair=$wide_string_type.returnPair; } | scoped_name { pair=$scoped_name.pair; // Find typecode in the global map. - $typecode = ctx.getTypeCode(pair.first()); + typecode = ctx.getTypeCode(pair.first()); + $returnPair = new Pair(typecode, null); - if($typecode == null) + if(typecode == null) { // Maybe an interface $def = ctx.getInterface(pair.first()); @@ -2434,12 +2547,12 @@ readonly_attr_declarator | simple_declarator ( COMA simple_declarator )* ; -attr_spec returns [Vector, TypeCode>> ret = new Vector, TypeCode>>(), Vector, Definition>> retDef = new Vector, Definition>>()] +attr_spec returns [Vector, TemplateGroup>, TypeCode>> ret = new Vector, TemplateGroup>, TypeCode>>(), Vector, TemplateGroup>, Definition>> retDef = new Vector, TemplateGroup>, Definition>>()] @init { TypeCode typecode = null; Definition definition = null; } - : KW_ATTRIBUTE param_type_spec { typecode=$param_type_spec.typecode; definition=$param_type_spec.def; } attr_declarator + : KW_ATTRIBUTE param_type_spec { typecode=$param_type_spec.returnPair.first(); definition=$param_type_spec.def; } attr_declarator { if(typecode != null) { @@ -2447,7 +2560,8 @@ attr_spec returns [Vector, TypeCode>> ret = new Vector< { // attr_declarator always is a simple declarator. Not a complex (array): // Simple declaration - $ret.add(new Pair, TypeCode>($attr_declarator.ret.get(count).first(), typecode)); + Pair, TemplateGroup> pair = new Pair, TemplateGroup>($attr_declarator.ret.get(count).first(), $param_type_spec.returnPair.second()); + $ret.add(new Pair, TemplateGroup>, TypeCode>(pair, typecode)); } } else if(definition != null) @@ -2456,7 +2570,8 @@ attr_spec returns [Vector, TypeCode>> ret = new Vector< { // attr_declarator always is a simple declarator. Not a complex (array): // Simple declaration - $retDef.add(new Pair, Definition>($attr_declarator.ret.get(count).first(), definition)); + Pair, TemplateGroup> pair = new Pair, TemplateGroup>($attr_declarator.ret.get(count).first(), $param_type_spec.returnPair.second()); + $retDef.add(new Pair, TemplateGroup>, Definition>(pair, definition)); } } } From 721c1ae09b020c11fa9bc3786b932f70a9610825 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Thu, 5 Oct 2023 12:22:48 +0200 Subject: [PATCH 5/5] Refs #19115: include array type Signed-off-by: JLBuenoLopez-eProsima --- src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 index 10c8966a..72f61bde 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -2177,6 +2177,7 @@ array_declarator returns [Pair, ContainerTypeCode>, Tem { arrayTemplates.setAttribute("array", typecode); arrayTemplates.setAttribute("ctx", ctx); + arrayTemplates.setAttribute("array_type",tk.getText()); } Pair p = new Pair(tk.getText(), tk); Pair, ContainerTypeCode> pp = new Pair, ContainerTypeCode>(p, typecode);