Java 签入Xtend验证程序中的类型
我想检查Xtend验证器中的功能类型 Xtext语法如下所示:Java 签入Xtend验证程序中的类型,java,validation,xtext,xtend,Java,Validation,Xtext,Xtend,我想检查Xtend验证器中的功能类型 Xtext语法如下所示: Element: 'Element' name=ID 'feature' feature=DOUBLE 'end' 'Element' ; 这就是它的使用方式: Element MyElement feature 2.5 end Element 如果尝试对功能使用整数值而不是双精度,编辑器将显示错误: 输入“84900”不匹配,需要规则\u DOUBLE 我想改写邮件。因此,我在验证器中创建了一个
Element:
'Element' name=ID
'feature' feature=DOUBLE
'end' 'Element'
;
这就是它的使用方式:
Element MyElement
feature 2.5
end Element
如果尝试对功能
使用整数
值而不是双精度
,编辑器将显示错误:
输入“84900”不匹配,需要规则\u DOUBLE
我想改写邮件。因此,我在验证器中创建了一个验证方法。在方法中,我想检查特征的类型。这就是我想做的:
@Check
def checkFeatureType(Element element) {
if (element.feature instanceof Double) {
// shows error!
}
}
instanceof
检查显示以下错误:
不兼容的条件操作数类型double或double和double
还是加倍
如何执行类型检查,或者是否有更好的方法覆盖上述标准消息?要覆盖解析器生成的消息,您需要专门化org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider,并在您的语言的运行时模块中绑定您自己的实现。多亏了Sebastian,我找到了解决方案 首先,有必要向名为
RuntimeModule
的RuntimeModule
中添加一个SyntaxErrorMessageProvider
。在我的开场白中给出的例子中,应该是MyLanguageRuntimeModule
public class MyLanguageRuntimeModule extends my.language.AbstractMyLanguageRuntimeModule {
public Class<? extends org.eclipse.xtext.generator.IGenerator> bindIGenerator() {
return my.language.generator.MyLanguageGenerator.class;
}
public Class<? extends ISyntaxErrorMessageProvider> bindISyntaxErrorMessageProvider() {
return MyLanguageSyntaxErrorMessageProvider.class;
}
}
我的类IssueCodes.java
如下所示(可以使用其他发行代码进行扩展):
@Override
public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) {
if (context.getRecognitionException() instanceof MismatchedTokenException) {
MismatchedTokenException exception = (MismatchedTokenException) context.getRecognitionException();
String value = exception.token.getText();
return new SyntaxErrorMessage("The type of " + value + " is wrong.", IssueCodes.FALSE_PARAMETER_TYPE);
}
// additional implementations
return null;
}
public interface IssueCodes {
String PREFIX = "my.language.";
String UNCAPITALIZED_ENTITY_NAME = "UncapitalizedEntityName";
String FALSE_PARAMETER_TYPE = "FalseParameterType";
}