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 d5a3b9c3..aea0cc6e 100644 --- a/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 +++ b/src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4 @@ -722,6 +722,7 @@ literal returns [Pair pair = null] String literalStr = tk.getText(); } : ( HEX_LITERAL + | OCTAL_LITERAL | INTEGER_LITERAL | STRING_LITERAL { literalStr = ctx.concatStringLiterals(literalStr); } | WIDE_STRING_LITERAL { literalStr = ctx.concatStringLiterals(literalStr); } diff --git a/src/main/java/com/eprosima/idl/context/Context.java b/src/main/java/com/eprosima/idl/context/Context.java index 1cb28087..32ceef0f 100644 --- a/src/main/java/com/eprosima/idl/context/Context.java +++ b/src/main/java/com/eprosima/idl/context/Context.java @@ -328,14 +328,14 @@ public Context( //{{{ @service AnnotationDeclaration serviceann = createAnnotationDeclaration(Annotation.service_str, null); - serviceann.addMember(new AnnotationMember(Annotation.platform_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str)); + serviceann.addMember(new AnnotationMember(Annotation.platform_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.any_str)); // CORBA, DDS, * (any), or custom value //}}} //{{{ topic AnnotationDeclaration topic_annotation = createAnnotationDeclaration(Annotation.topic_str, null); - topic_annotation.addMember(new AnnotationMember(Annotation.name_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str)); - topic_annotation.addMember(new AnnotationMember(Annotation.platform_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str)); + topic_annotation.addMember(new AnnotationMember(Annotation.name_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.empty_str)); + topic_annotation.addMember(new AnnotationMember(Annotation.platform_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.any_str)); //}}} //{{{ @try_construct @@ -350,7 +350,7 @@ public Context( //{{{ @unit AnnotationDeclaration unitsann = createAnnotationDeclaration(Annotation.unit_str, null); - unitsann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str)); + unitsann.addMember(new AnnotationMember(Annotation.value_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.empty_str)); //}}} //{{{ @value @@ -367,10 +367,10 @@ public Context( verbatimannenum.addMember(new EnumMember(Annotation.end_declaration_str)); verbatimannenum.addMember(new EnumMember(Annotation.after_declaration_str)); verbatimannenum.addMember(new EnumMember(Annotation.end_file_str)); - verbatimann.addMember(new AnnotationMember(Annotation.language_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.any_str)); + verbatimann.addMember(new AnnotationMember(Annotation.language_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.any_str)); // c, c++, java, idl, * (any), or custom value verbatimann.addMember(new AnnotationMember(Annotation.placement_str, verbatimannenum, Annotation.before_declaration_str)); - verbatimann.addMember(new AnnotationMember(Annotation.text_str, new PrimitiveTypeCode(Kind.KIND_STRING), Annotation.empty_str)); + verbatimann.addMember(new AnnotationMember(Annotation.text_str, new StringTypeCode(Kind.KIND_STRING, null, null), Annotation.empty_str)); //}}} } diff --git a/src/main/java/com/eprosima/idl/parser/tree/Annotation.java b/src/main/java/com/eprosima/idl/parser/tree/Annotation.java index 95e7e618..8fcfe47d 100644 --- a/src/main/java/com/eprosima/idl/parser/tree/Annotation.java +++ b/src/main/java/com/eprosima/idl/parser/tree/Annotation.java @@ -35,9 +35,7 @@ public class Annotation public static final String autoid_str = "autoid"; public static final String autoid_enum_str = "AutoidKind"; public static final String autoid_sequential_str = "SEQUENTIAL"; - public static final String autoid_sequential_value_str = "0"; public static final String autoid_hash_str = "HASH"; - public static final String autoid_hash_value_str = "1"; //}}} public static final String bit_bound_str = "bit_bound"; @@ -233,6 +231,17 @@ public String getValue() throws RuntimeGenerationException return ((AnnotationMember)m_members.values().toArray()[0]).getValue(); } + public String getValueFromAny(TypeCode typecode) throws RuntimeGenerationException + { + if(m_members.size() != 1) + { + throw new RuntimeGenerationException("Error in annotation " + getName() + + ": accessing value of a multiple parameter exception"); + } + + return ((AnnotationMember)m_members.values().toArray()[0]).getValueFromAny(typecode); + } + public String getValue(String attribute) { return m_members.get(attribute).getValue(); diff --git a/src/main/java/com/eprosima/idl/parser/tree/AnnotationMember.java b/src/main/java/com/eprosima/idl/parser/tree/AnnotationMember.java index 0984b852..2a4c8001 100644 --- a/src/main/java/com/eprosima/idl/parser/tree/AnnotationMember.java +++ b/src/main/java/com/eprosima/idl/parser/tree/AnnotationMember.java @@ -15,11 +15,14 @@ package com.eprosima.idl.parser.tree; import com.eprosima.idl.parser.exception.ParseException; -import com.eprosima.idl.parser.typecode.Member; +import com.eprosima.idl.parser.typecode.AnyTypeCode; import com.eprosima.idl.parser.typecode.EnumMember; import com.eprosima.idl.parser.typecode.EnumTypeCode; +import com.eprosima.idl.parser.typecode.Member; import com.eprosima.idl.parser.typecode.TypeCode; +import com.eprosima.idl.parser.exception.RuntimeGenerationException; + public class AnnotationMember { public AnnotationMember(String name, TypeCode typecode, String value) @@ -50,26 +53,68 @@ public TypeCode getTypecode() return m_typecode; } - public String getValue() + public String getNumericValue() { + String value = getValue(); + if (m_typecode.isIsEnumType()) { EnumTypeCode enumTC = (EnumTypeCode)m_typecode; int idx = 0; - int default_idx = 0; for (Member m : enumTC.getMembers()) { - if (m.getName().equals(m_value)) + String m_str = enumTC.getScopedname() + "::" + m.getName(); + if (m_str.equals(value)) { return Integer.toString(idx); } + idx++; + } + } + + return value; + } + + public String getValue() + { + if (m_typecode.isIsEnumType()) + { + EnumTypeCode enumTC = (EnumTypeCode)m_typecode; + String literal_value = ""; + String value = m_value; + + if (null == value) + { + value = ""; + } + + if (value.startsWith("\"") && value.endsWith("\"")) + { + value = value.substring(1, value.length() - 1); + } + + for (Member m : enumTC.getMembers()) + { + if (m.getName().equals(value)) + { + return enumTC.getScopedname() + "::" + m.getName(); + } else if (m.isAnnotationDefaultLiteral()) { - default_idx = idx; + literal_value = m.getName(); } - idx++; + else if (value.isEmpty()) + { + value = m.getName(); + } + } + + if (!literal_value.isEmpty()) + { + return enumTC.getScopedname() + "::" + literal_value; } - return Integer.toString(default_idx); + + return enumTC.getScopedname() + "::" + value; } else if (m_typecode.isIsStringType() || m_typecode.isIsWStringType()) { @@ -94,7 +139,13 @@ else if (m_typecode.isPrimitiveType()) if (m_value.startsWith("0x")) { // If it's hexadecimal, parse it using parseInt with radix 16 return Integer.toString(Integer.parseInt(m_value.substring(2), 16)); - } else { + } + else if (m_value.startsWith("0") && m_value.length() > 1 && Character.isDigit(m_value.charAt(1))) + { + return Integer.toString(Integer.parseInt(m_value.substring(1), 8)); + } + else + { return m_value; } } @@ -103,6 +154,89 @@ else if (m_typecode.isPrimitiveType()) return m_value; } + public String getValueFromAny(TypeCode typecode) throws RuntimeGenerationException + { + if (m_typecode instanceof AnyTypeCode) + { + if (typecode.isIsEnumType()) + { + EnumTypeCode enumTC = (EnumTypeCode)typecode; + String literal_value = ""; + String value = m_value; + + if (null == value) + { + value = ""; + } + + if (value.startsWith("\"") && value.endsWith("\"")) + { + value = value.substring(1, value.length() - 1); + } + for (Member m : enumTC.getMembers()) + { + if (m.getName().equals(value)) + { + return enumTC.getScopedname() + "::" + m.getName(); + } + else if (m.isAnnotationDefaultLiteral()) + { + literal_value = m.getName(); + } + else if (value.isEmpty()) + { + value = m.getName(); + } + } + + if (!literal_value.isEmpty()) + { + return enumTC.getScopedname() + "::" + literal_value; + } + + return enumTC.getScopedname() + "::" + value; + } + else if (typecode.isIsStringType() || typecode.isIsWStringType()) + { + if (m_value != null) + { + if (!m_value.startsWith("\"") && !m_value.endsWith("\"")) + { + return "\"" + m_value + "\""; + } + } + if (typecode.isIsWStringType()) + { + return "L\"\""; + } + return m_value; + } + else if (typecode.isPrimitiveType()) + { + if (m_value != null) + { + // Check if the string starts with "0x" to determine if it's hexadecimal + if (m_value.startsWith("0x")) { + // If it's hexadecimal, parse it using parseInt with radix 16 + return Integer.toString(Integer.parseInt(m_value.substring(2), 16)); + } + else if (m_value.startsWith("0") && m_value.length() > 1 && Character.isDigit(m_value.charAt(1))) + { + return Integer.toString(Integer.parseInt(m_value.substring(1), 8)); + } + else + { + return m_value; + } + } + return typecode.getInitialValue(); + } + return m_value; + } + + throw new RuntimeGenerationException("Annotation " + m_name + "is not from any type"); + } + public String getEnumStringValue() { if (m_value != null && m_typecode.isIsEnumType()) @@ -117,6 +251,7 @@ public String getEnumStringValue() } String[] value_with_scopes = value.split("::"); value = value_with_scopes[value_with_scopes.length - 1]; + if (m.getName().equals(value)) { return value; @@ -127,6 +262,50 @@ public String getEnumStringValue() return m_value; } + public String getPlacementEnumStringValue() + { + if (m_name == Annotation.placement_str && m_value != null && m_typecode.isIsEnumType()) + { + EnumTypeCode enumTC = (EnumTypeCode)m_typecode; + for (Member m : enumTC.getMembers()) + { + String value = m_value; + if (value.startsWith("\"") && value.endsWith("\"")) + { + value = value.substring(1, value.length() - 1); + } + String[] value_with_scopes = value.split("::"); + value = value_with_scopes[value_with_scopes.length - 1]; + + if (m.getName().equals(value)) + { + if (value.equals(Annotation.begin_file_str)) + { + return "begin-declaration-file"; + } + else if (value.equals(Annotation.before_declaration_str)) + { + return "before-declaration"; + } + else if (value.equals(Annotation.after_declaration_str)) + { + return "after-declaration"; + } + else if (value.equals(Annotation.end_declaration_str)) + { + return "end-declaration"; + } + else if (value.equals(Annotation.end_file_str)) + { + return "end-declaration-file"; + } + } + } + throw new ParseException(null, m_value + " is not a valid label for " + m_name); + } + throw new ParseException(null, m_name + "is not Placement annotation member"); + } + public void setValue(String value) { m_value = value; diff --git a/src/main/java/com/eprosima/idl/parser/tree/Module.java b/src/main/java/com/eprosima/idl/parser/tree/Module.java index 14cd9fbd..faa3d4ef 100644 --- a/src/main/java/com/eprosima/idl/parser/tree/Module.java +++ b/src/main/java/com/eprosima/idl/parser/tree/Module.java @@ -110,95 +110,5 @@ public boolean isIsAnnotation() return false; } - ////////// RESTful block ////////// - - public String getResourceCompleteBaseUri() - { - Annotation baseUri = getAnnotations().get("RESOURCES_BASE_URI"); - String baseUriStr = baseUri.getValue("value"); - - if(baseUriStr != null) - { - // Remove http:// - int posInit = baseUriStr.indexOf("//"); - - if(posInit == -1) - posInit = 0; - else - posInit += 2; - - return baseUriStr.substring(posInit); - } - - return baseUriStr; - } - - /* - * @brief This function return the base URI without the host. - * Also all spaces are converted to %20. - */ - public String getResourceBaseUri() - { - String baseUri = getResourceCompleteBaseUri(); - - if(baseUri != null) - { - // Remove host - int posEnd = baseUri.indexOf('/'); - - if(posEnd == -1) - return ""; - else - return baseUri.substring(posEnd).replace(" ", "%20"); - } - - return null; - } - - /* - * @brief This function return the base URI without the host. - * Also all spaces are converted to %20. - */ - public String getResourceBaseUriWithoutLastBackslace() - { - String baseUri = getResourceBaseUri(); - - if(baseUri != null) - { - if(!baseUri.isEmpty() && baseUri.charAt(baseUri.length() - 1) == '/') - { - if(baseUri.length() > 1) - baseUri = baseUri.substring(0, baseUri.length() - 1); - else - baseUri = ""; - } - return baseUri; - } - - return null; - } - - public String getResourceHost() { - Annotation path = getAnnotations().get("RESOURCES_BASE_URI"); - String pathStr = path.getValue("value"); - - // Remove http:// - int posInit = pathStr.indexOf("//"); - if(posInit == -1) - posInit = 0; - else - posInit += 2; - - // Remove path - int posEnd = pathStr.indexOf('/', posInit); - - if(posEnd == -1) - posEnd = pathStr.length()-1; - - return pathStr.substring(posInit, posEnd); - } - - ////////// End RESTful block ////////// - private Object m_parent = null; } diff --git a/src/main/java/com/eprosima/idl/parser/typecode/EnumTypeCode.java b/src/main/java/com/eprosima/idl/parser/typecode/EnumTypeCode.java index cfd107b1..c191a93a 100644 --- a/src/main/java/com/eprosima/idl/parser/typecode/EnumTypeCode.java +++ b/src/main/java/com/eprosima/idl/parser/typecode/EnumTypeCode.java @@ -97,12 +97,17 @@ public String getIdlTypename() @Override public String getInitialValue() { - if (getMembers().size() > 0) + String initial_value = getMembers().size() > 0 ? getScopedname() + "::" + getMembers().get(0).getName() : ""; + for (Member m : getMembers()) { - return getScopedname() + "::" + getMembers().get(0).getName(); + if (m.isAnnotationDefaultLiteral()) + { + initial_value = getScopedname() + "::" + m.getName(); + break; + } } - return ""; + return initial_value; } @Override @@ -133,7 +138,7 @@ public boolean isIsPlain() { return true; } - + public int getBitBound() { // TODO: pending @bit_bound annotation application to enum types diff --git a/src/main/java/com/eprosima/idl/parser/typecode/Member.java b/src/main/java/com/eprosima/idl/parser/typecode/Member.java index e91ca69a..a93001ec 100644 --- a/src/main/java/com/eprosima/idl/parser/typecode/Member.java +++ b/src/main/java/com/eprosima/idl/parser/typecode/Member.java @@ -143,11 +143,6 @@ public String getAnnotationIdValue() throws RuntimeGenerationException return ann.getValue(); } - public boolean isAnnotationHashid() - { - return null != getAnnotations().get(Annotation.hashid_str); - } - public String getAnnotationHashidValue() throws RuntimeGenerationException { Annotation ann = getAnnotations().get(Annotation.hashid_str); @@ -160,6 +155,17 @@ public String getAnnotationHashidValue() throws RuntimeGenerationException return ann.getValue(); } + public String getAnnotationDefaultValue() throws RuntimeGenerationException + { + Annotation ann = getAnnotations().get(Annotation.default_str); + if (ann != null) + { + return ann.getValueFromAny(m_typecode); + } + throw new RuntimeGenerationException("Error getting @" + Annotation.default_str + + " annotation value: annotation not found"); + } + private String m_name = null; private TypeCode m_typecode = null; diff --git a/src/main/java/com/eprosima/idl/parser/typecode/MemberAppliedAnnotations.java b/src/main/java/com/eprosima/idl/parser/typecode/MemberAppliedAnnotations.java index b9008733..7617b791 100644 --- a/src/main/java/com/eprosima/idl/parser/typecode/MemberAppliedAnnotations.java +++ b/src/main/java/com/eprosima/idl/parser/typecode/MemberAppliedAnnotations.java @@ -95,17 +95,6 @@ public boolean isAnnotationDefault() return m_annotations.get(Annotation.default_str) != null; } - public String getAnnotationDefaultValue() throws RuntimeGenerationException - { - Annotation ann = m_annotations.get(Annotation.default_str); - if (ann != null) - { - return ann.getValue(); - } - throw new RuntimeGenerationException("Error getting @" + Annotation.default_str + - " annotation value: annotation not found"); - } - public boolean isAnnotationDefaultLiteral() { return m_annotations.get(Annotation.default_literal_str) != null; diff --git a/src/main/java/com/eprosima/idl/parser/typecode/MemberedTypeCode.java b/src/main/java/com/eprosima/idl/parser/typecode/MemberedTypeCode.java index ad09f4b4..a1b19bf0 100644 --- a/src/main/java/com/eprosima/idl/parser/typecode/MemberedTypeCode.java +++ b/src/main/java/com/eprosima/idl/parser/typecode/MemberedTypeCode.java @@ -325,7 +325,7 @@ else if (member.isAnnotationHashid()) String value = member.getAnnotationHashidValue(); member.set_id(calculate_hash.apply(value.isEmpty() ? member.getName() : value)); } - else if (!isAnnotationAutoid() || getAnnotationAutoidValue().equals(Annotation.autoid_sequential_value_str)) + else if (!isAnnotationAutoid() || getAnnotationAutoidValue().equals(Annotation.autoid_sequential_str)) { member.set_id(++last_id_); } @@ -457,7 +457,10 @@ public String getAnnotationAutoidValue() throws RuntimeGenerationException " annotation not found."); } - return ann.getValue(); + String value= ann.getValue();; + String[] value_with_scopes = value.split("::"); + return value_with_scopes[value_with_scopes.length - 1]; + } public boolean isNonForwardedContent() diff --git a/src/main/java/com/eprosima/idl/parser/typecode/TypeCode.java b/src/main/java/com/eprosima/idl/parser/typecode/TypeCode.java index 6dfa1b9c..f30158d6 100644 --- a/src/main/java/com/eprosima/idl/parser/typecode/TypeCode.java +++ b/src/main/java/com/eprosima/idl/parser/typecode/TypeCode.java @@ -622,8 +622,13 @@ public boolean isAnnotationAutoidHash() { try { - return (ann.getValue().toUpperCase().equals(Annotation.autoid_hash_value_str) || - ann.getValue().isEmpty()); + String value= ann.getValue();; + if (value.isEmpty()) { + return true; + } + String[] value_with_scopes = value.split("::"); + value = value_with_scopes[value_with_scopes.length - 1]; + return value.equals(Annotation.autoid_hash_str); } catch (RuntimeGenerationException ex) {