I'm trying to modify Xtext domain model example, so that entity properties can have two more attributes (in addition to the 'many' that is already included in the example). All attributes need to be optional and their order irrelevant. It seems that, no matter what I do, the first attribute must be the one that is listed first. So, for example, if I have this in xtext:
(many ?= 'many')? & (aBool ?= 'isBool')? & (anEnum = EnumType)?
If I use 'many', then it must be the first attribute, or I get an error. If I leave it out completely, I get no errors.
So, how do I tell xtext that I want three optional attributes, in an unordered group?
Apparently, all I had to do is enclose the group in parentheses :/
((many ?= 'many')? & (aBool ?= 'isBool')? & (anEnum = EnumType)?)
This helped.
The & operator is very problematic in Xtext! Because the parser has to implement all possible permutations. It would be better to implement an abstract Parser Rule 'OptionalElement' which can occure infinite often in a list. This parser rule then is specified as 'Many', 'Bool' or 'Enum'. The grammar then would look like this:
AnyRule: /* some stuff */ optElement+=OptionalElement*;
OptionalElement: Many | Bool | Enum;
Many: {Many} 'many';
Bool: {Bool} 'isBool';
Enum: anEnum=EnumType;
Then you only have to implement a check method in the validator class (...mydsl.validation.MyDslValidator) which checks whether Many, Bool or Enum occures max. one time in the 'optElement' list.
The class/method/field modifier are defined in the same way in the Java grammar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With