我有一个Xtext语法,可以这样做:
Model:
(names += Name)*
(rules += Rule)*
;
Rule:
'rule' ruleName = ID;
Name:
name = ID+;
terminal ID:
('a'..'z')+;
我想验证名称块中已声明的ruleName
。我可以在JavaValidator
中访问规则名称本身:
@Check
public void checkName(Rule rule) {
rule.getName(); // how to compare to names without access to Model object?
}
但我无法访问names
中的Model
字段。我如何在JavaValidator
?
答案 0 :(得分:2)
或者(Model)rule.eContainer()
应该为您提供模型
答案 1 :(得分:0)
如果您不必将Name
定义为终端,请考虑使用cross-references:
grammar org.example.YourDSL
with org.eclipse.xtext.common.Terminals
generate secrets "http://www.example.org/yourdsl"
Model:
(names += Name)*
(rules += Rule)*
;
Name:
name=ID; // name of the property is important!
Rule:
'rule' name=[Name];
// Override ID from org.eclipse.xtext.common.Terminals
terminal ID:
('a'..'z')+;