我想实现自己的DefaultTerminalConverters类,以使Integer代替终端规则VALUE_TERMINAL
我的语法中的VALUE_TERMINAL是:
terminal VALUE_TERMINAL:
( '0' .. '9' )+ ;
我自己的DefaultTerminalConverters的代码是:
import com.google.inject.Inject;
import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
import org.eclipse.xtext.nodemodel.INode;
public class MyLangValueConverter extends DefaultTerminalConverters {
@Inject MyINTValueConverter myINTValueConverter;
@ValueConverter(rule="VALUE_TERMINAL")
public IValueConverter<Integer> VALUE_TERMINAL() {
return myINTValueConverter;
}
private static class MyINTValueConverter extends AbstractLexerBasedConverter<Integer> {
@Override
public Integer toValue(String string, INode node) {
return new Integer(string);
}
@Override
public String toString(Integer value){
return String.valueOf(value);
}
}
}
当我在自己的DSL中写东西时,使用VALUE_TERMINAL时总是出现错误java.lang.Integer cannot be cast to java.lang.String
。可能是什么问题呢 ?
答案 0 :(得分:1)
问题是语法:
terminal VALUE_TERMINAL:
( '0' .. '9' )+ ;
是
的缩写import "http://www.eclipse.org/emf/2002/Ecore" as ecore
...
terminal VALUE_TERMINAL returns ecore::EString:
( '0' .. '9' )+ ;
,因此您需要为终端规则明确指定返回的数据类型。像
terminal VALUE_TERMINAL returns ecore::EInt:
或
terminal VALUE_TERMINAL returns ecore::EIntegerObject: