How can I create a goto expression in xtext?

50 Views Asked by At

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?

2

There are 2 best solutions below

1
Christian Dietrich On BEST ANSWER

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

0
Banoffee On

I think you want a terminal that is essentially "ID INT" and then use it to crossreference your Label. I think this is going to be a lot of work just to be able to allow "spaces" in your labels. Why not simply rely on terminal "ID" and users may name them "bb2" if they wish?