Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ literal returns [Pair<String, Token> 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); }
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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));
//}}}
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/eprosima/idl/parser/tree/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
195 changes: 187 additions & 8 deletions src/main/java/com/eprosima/idl/parser/tree/AnnotationMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
{
Expand All @@ -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;
}
}
Expand All @@ -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())
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading