如何在Xtext验证器中访问上下文信息?

时间:2011-12-05 11:48:18

标签: java xtext

我有一个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

中执行此操作

2 个答案:

答案 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')+;