I have an Xtext grammar that does something like this:

Model:
  (names += Name)*
  (rules += Rule)*
;

Rule:
  'rule' ruleName = ID;

Name:
  name = ID+;

terminal ID:
  ('a'..'z')+;

I want to validate the ruleName has been declared in the names block. I can access the rule name itself in the JavaValidator like this:

@Check
public void checkName(Rule rule) {
  rule.getName(); // how to compare to names without access to Model object?
}

but I cannot access the names field from Model. How do I do that in the JavaValidator?

有帮助吗?

解决方案

alternatively (Model)rule.eContainer() should give you the model

其他提示

If it is not mandatory for you to define Name as terminal, then consider using 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')+;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top