long
语法中正确实现double
,float
和Xtext
类型,以便我以后可以在我的翻译中使用它们?
解释器是extend
类。
在我的代码中,我有类似
的内容IDValue: value=ID;
StringValue: value=STRING;
IntValue : value=INT;
SimpleValue:
IntValue | StringValue
;
我想要像
这样的东西DoubleValue : value=DOUBLE;
在我的翻译中,我可以做到
if (p.var0 instanceof StringValue) :
我希望也能
if (p.var0 instanceof DoubleValue) :
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;
这是我的解释器中的类,我执行Set函数并且必须重新确定变量的类型 它在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 )}
}
重新确定我使用这些方法的类型
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 )
价值转换器:
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);
}
};
}
答案 0 :(得分:2)
您可以使用import "http://www.eclipse.org/emf/2002/Ecore" as ecore
导入ecore元模型,从而导入其数据类型。然后,您可以将它们用作终端或数据类型规则LONG returns ecore::ELong: INT ("L"|"l");
中的返回值。最后,您必须实现一个ValueConverter,它将Convertion从String转换为Long和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;
}
}
并且不要忘记绑定
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
override bindIValueConverterService() {
return MyDslValueConverters
}
}