3

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?

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54

1 Answers1

1

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

fratnk
  • 93
  • 5
  • The problem is standard events rather than states (those are rarely standard), but I see what you mean. Meanwhile, I have created an extension to my Xpand templates which includes both the explicit and implicit eventnames in every list of events, and I seem to get around with the events/baseevents pairing. But I'll look into your suggestion and see if it would help to clean up the current solution. I'll get back to you! – weltraumpirat Jan 24 '11 at 16:30
  • Okay, I've taken a look at the part of the documentation you linked to, but I can't see how this is going to solve my problem: If I use `events+=[Event]`, I imply that events references events that already exists. Which means: In my generated editor (no longer Xtext!), I have to first specify an event, which I can later reference by name. The editor (and the MWEReader) will check for its existence and throw an error, if the event can't be found. Now I want to be able to EITHER have this functionality, OR enter one out of a couple of common IDs, which should also be checked. – weltraumpirat Jan 24 '11 at 16:55
  • In short: If I understand correctly, the solution you suggest works for the original Xtext grammar, but not for the generated language and editor. – weltraumpirat Jan 24 '11 at 16:58
  • No, actually it is intended to work in generated language editor. Or to be more precise: It is not the editor. You could use as wall notepad to edit your files, the import still works! – fratnk Feb 04 '11 at 19:10