简体   繁体   中英

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?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM