eclipsegrammardslxtextgo-to-definition

How can I create a goto expression in xtext?


I want to create a goto expression as follows

//label
 <bb 2> :

//goto
goto <bb 2>;

The following grammar works fine for a simple ID. I have no idea how to reference the <ID INT> in the goto expression.

Goto returns Goto:
    {Goto}
    'goto' goto+=[Label]  ';'
;

LabelDef returns LabelDef:
    {LabelDef}
    label+= Label ':'
    ;

Label returns Label:
    {Label}
    name= ID
    ;

Do have any idea how to that?


Solution

  • the feature you are looking for is a DataType rule

    Goto returns Goto:
        {Goto}
        'goto' goto+=[Label|IDandINT]  ';'
    ;
    
    LabelDef returns LabelDef:
        {LabelDef}
        label+= Label ':'
        ;
    
    Label returns Label:
        {Label}
        name= IDandINT
        ;
    IDandINT: ID INT;
    

    you may also introduce / customize DefaultTerminalConverters/IValueConverter for the datatype rule to normalize whitespace