'xtext: Statements in different order

I'm trying to write a xtext rule, where I can write statements in different random orders but all statements have to be saved as a list.

I tried:

Root:
 ( (entity += Entity)? & (component += Component)? )*
;

But it doesn’t work. Can someone help me?



Solution 1:[1]

An easy way to achieve for goals:

  • Use Svens answer for the grammar part. This allows you to mix Entities and Components freely.
  • Add a custom validation check to ensure that at least one Entity and at least one Component is there.

The docu for custom validations is in the chapter Validation, subchapter "Custom Validation".

Solution 2:[2]

This does what you've asked for:

Root:
  (entity+=Entity | component+=Component)*
;

Solution 3:[3]

Just to supplement the answer, and to answer toms question in the comments. To do what is asked for while ensuring both Entity and Component appear at least once I still think it would be best to follow Sven Efftinges answer, and afterward implement a validator to ensure both an Entity and Component appears at least once. Generally, it is best to keep the grammar syntax simple and do more advanced checks with validators.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Priebe
Solution 2 Sven Efftinge
Solution 3 Nikolai Damm