javaxtextxtend

Xtext, implementing Long, Double and Float types


the question is very simple.
How do I implement long, double and float types correctly in my Xtext grammar so I can use them later in my interpreter? the interpreter is an extend class.

EDIT 1

In my code i have something like

IDValue: value=ID;
StringValue: value=STRING;
IntValue : value=INT;

SimpleValue:

     IntValue | StringValue
;

And i would like to have something like

DoubleValue : value=DOUBLE;

In my interpreter I can do

if (p.var0 instanceof StringValue)  : 

I expect to do also

 if (p.var0 instanceof DoubleValue)  : 

EDIT 2

This is the grammar:

Set:
    'SET' '(' attribute=ID ',' value=(AttributeValue ) ')'
;


StringValue: value=STRING;
IntValue : value=INT;
LongValue : value = LONG;
DoubleValue : value=DOUBLE;

SimpleValue:

     IntValue | StringValue | LongValue | DoubleValue
;


Structure: {Structure}
    '{' ID ':' var0=AttributeValue (',' ID ':' var1+=AttributeValue)* '}'
;

Array: {Array}
    '[' var0=AttributeValue(',' var1+=AttributeValue)* ']' 
;

AttributeValue : 
    SimpleValue |
    Structure |
    Array 
;

LONG returns ecore::ELong: INT ("L"|"l");
DOUBLE returns ecore::EDouble : INT'.'INT;

And this is the class in my interpreter where I execute the Set function and must recongnize the type of the variable (it's in Xtend):

def dispatch void exec(Set s) {
    val setAttribute = LocalMapAttribute.get(s.attribute.toString)
    val cosem = LocalMapCosem.get(setAttribute.object)
    val setValue = s.value.getType
    if (setValue instanceof Array) {Jtest.SetFunction(cosem,setAttribute.attribute_name,ArrayToList(setValue),"Array")} 
    else if (setValue instanceof String) {Jtest.SetFunction(cosem,setAttribute.attribute_name,setValue)}
    else if (setValue instanceof Structure) {Jtest.SetFunction(cosem,setAttribute.attribute_name,StructToList(setValue),"Struct")}
    else if (setValue instanceof Long) {println("LONG DETECTED" + setValue)}
    else if (setValue instanceof Double) {println("DOUBLE DETECTED" + setValue)}
    else if (setValue instanceof Integer) {Jtest.SetFunction(cosem,setAttribute.attribute_name,setValue )}
    
} 

To recongnize the type I use these methods:

def dispatch Array  getType (Array array){
//println("SET with Array") 
return array
}
def dispatch getType (SimpleValue simple){
    //println("SET with Simple Value") 
    return getTypeSimple(simple)
}
def dispatch Structure getType (Structure structure){
    //println("SET with Strcutre")
    return structure
}
def dispatch String getTypeSimple (StringValue str){
    //println("SET with Sting")
    return (str.value as String)
}
def dispatch int getTypeSimple (IntValue Int){
    //println("SET with int")
    return (Int.value )
}
def dispatch double getTypeSimple (DoubleValue Int){
    
    println("SET with DoubleValue")
    return (Int.value )
}
def dispatch long getTypeSimple (LongValue Int){
    println("SET with LongValue")
    return (Int.value )
}

The Value Converter:

public class MyDslValueConverters extends DefaultTerminalConverters {

    IValueConverter<Long> longValueConverter = new IValueConverter<Long>() {

        @Override
        public Long toValue(String string, INode node) throws ValueConverterException {
            // TODO make this more robust
            return Long.parseLong(string.substring(0, string.length()-1));
        }

        @Override
        public String toString(Long value) throws ValueConverterException {
            // TODO make this more robust
            return Long.toString(value)+"L";
        }

    };
    
    IValueConverter<Double> DoubleValueConverter = new IValueConverter<Double>() {

        @Override
        public Double toValue(String string, INode node) throws ValueConverterException {
            // TODO make this more robust
            return Double.parseDouble(string);
        }

        @Override
        public String toString(Double value) throws ValueConverterException {
            // TODO make this more robust
            return Double.toString(value);
        }

    };

}

Solution

  • you can import the ecore metamodel and thus its datatypes using import "http://www.eclipse.org/emf/2002/Ecore" as ecore. Then you can use them as return value in a terminal or datatype rule LONG returns ecore::ELong: INT ("L"|"l");. Finally you have to implement a ValueConverter that does the Convertion from String to Long and Vice Versa

    public class MyDslValueConverters extends DefaultTerminalConverters {
    
        IValueConverter<Long> longValueConverter = new IValueConverter<Long>() {
    
            @Override
            public Long toValue(String string, INode node) throws ValueConverterException {
                // TODO make this more robust
                return Long.parseLong(string.substring(0, string.length()-1));
            }
    
            @Override
            public String toString(Long value) throws ValueConverterException {
                // TODO make this more robust
                return Long.toString(value)+"L";
            }
    
        };
    
        @ValueConverter(rule = "LONG")
        public IValueConverter<Long> LONG() {
            return longValueConverter;
        }
    
    }
    

    And dont forget the binding

    class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
    
        override bindIValueConverterService() {
            return MyDslValueConverters
        }
    
    }