How to access context information in an Xtext validator?
Frage
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
?
Lösung
alternatively (Model)rule.eContainer()
should give you the model
Andere Tipps
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')+;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow