eclipsextexteclipse-emfecore

Eclipse Xtext : How to force Xtext to look for a reference with a specific Name


I defined a gramar like this

grammar org.xtext.example.mydsl1.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl1/MyDsl"

Model:
    persons+=(Person | David)
    greetings=(GreetDavid | Greet);

Person returns Person:
    'Holla. My name is' name=ID         //First "create" a person
;
David returns Person:
  'Holla. My name is' name='David'          //David is a special person. He has name David
;
GreetDavid returns Greeting:
    'Hello' person=[David]           //Only greet David with Hello
; 
Greet returns Greeting:
    'Hi' person=[Person]           //Greet other persons with Hi
;
 
 

Of course, Xtext complains that David is not a known type : "Cannot resolve type null"

How can I simply tell Xtext that David is a person that will be created with name "David"?

I dont want to change the Ecore and create a class specifically for David.

Thanks in advance


Solution

  • You could use a data type rule. e.g.

    DAVID:'David';
    David returns Person:
        'Holla. My name is' name=DAVID
    ;
    GreetDavid returns Greeting:
        'Hello' person=[Person|DAVID]           //Only greet David with Hello
    ;