1

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

但我无法namesModel. 我该怎么做JavaValidator

4

2 回答 2

2

或者(Model)rule.eContainer()应该给你模型

于 2011-12-06T19:52:16.230 回答
0

如果您不强制定义Name为终端,请考虑使用交叉引用

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')+;
于 2011-12-05T15:28:34.587 回答