Reputation: 22604
I have an Xtext grammar that describes statemachines, and I have been using references to previously declared events and states to describe transitions:
Event:
'event' name=ID
;
State:
'state' name=ID
;
Transition:
event=[Event] '=>' state=[State]
;
When I use MWE to generate an editor, it will validate the existence of referenced items. For example, writing
init => idle
would require these:
event init
state idle
to be present somewhere else in the code (btw I am using one file for each statemachine, so it needs to be in the same file). In my Xpand code templates, I can access event and state as elements of transition:
«FOREACH statemachine.transitions.event AS event-» // you get the idea
This works really well, and I have been using it for quite a while.
But since a number of events are common to all of my statemachines (init
, show
, hide
, finish
, and a few more), I want to be able to either reference an event the way I used to, or enter one of the above event names without having to declare the event in every file.
But I can't do this:
Transition:
event=( [Event] | ('init'|'show'|'hide'|'finish') ) '=>' state=[State]
;
So I decided to define a terminal EventID
, which is either of said keywords:
terminal EventID:
'init'|'show'|'hide'|'finish'
;
But I also can't do this:
Transition:
event=( [Event] | EventID ) '=>' state=[State]
;
Then I tried making Event a combined rule:
CustomEvent:
'event' name=ID
;
BaseEvent:
name=EventID
;
Event:
CustomEvent | BaseEvent
;
Transition:
event=[Event] '=>' state=[State]
;
which didn't work either.
For now I have decided to settle for a workaround:
Transition:
( event=[Event] | baseevent=EventID ) '=>' state=[State]
;
This works, but I will have to change all my templates to look for both events and baseevents.
I realize, this is quite a lot of text... So finally, here's my question:
Is there any way at all I can use the same element name for either a reference or an EventID?
Upvotes: 3
Views: 170
Reputation: 93
Your originaly problem was: You don't want to repeat yourself in several files. I suggest another approach to solve this problem: We don't you make a standard file, where you define your common states and import those states with the import statement described here: http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#syntax
Upvotes: 1