I have written following ParserRule in my Xtext grammar file:
TestSpec:
'{'
......
(('"my"' ':' myValue=(MySpec) (',' | '}'))?) &
......
'}'
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',')?)?) &
(('"prefix"' ':' prefixValue=(STRING) (',')?)?) &
(('"none"' ':' noneValue=(STRING) (',')?)?) &
)
'}';
Now in my Xtext editor, I want to ensure that the entries in “my” end with comma except the last one and it should give an error if comma is missing between say “suffix” and “prefix” like in following example entries:
"my": {
"suffix": "testString" ---> I want this to give an error for missing comma (,)
"prefix": "prefixString",
"none": "noneString"
}
Solution:
One solution that I thought of is to change the grammar to following:
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',' | '}'))?) &
(('"prefix"' ':' prefixValue=(STRING) (',' | '}'))?) &
(('"none"' ':' noneValue=(STRING) (',' | '}'))?) &
);
But I am not sure if this the preferred way of doing it. Is it possible to achieve this through Validations or some other more elegant ways?
This question is answered on Xtext Eclipse Community Forums.
I opted to solve it by using following standard pattern for comma-separated lists: