Search code examples
dslxtext

Rule written for DSL in xtext, It is not working as expected


I am trying -->> VL:'this is test dsl in xtext' IS ABC

TestLabelBase returns ResultExpressionRhs:
   'VL:' path=stringRule;

The Rule I tried was:

TestLabel returns ResultExpressionRhs:
   TestLabelBase ('IS' modifier=alphabateModifier)?;

alphabateModifier:
   (abc?='ABC' | def?='DEF' | ghi?='GHI');

It would recognize the IS but not the ABC.


Solution

  • The following xtext grammar will parse your example

    grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
    
    generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
    
    Model
      : lables+=TestLabel+
      ;
    
    TestLabel returns ResultExpressionRhs
       : TestLabelBase ('IS' modifier=AlphabateModifier)?
       ;
    
    TestLabelBase returns ResultExpressionRhs
      : 'VL:' path=STRING
      ;   
    
    AlphabateModifier
      : (abc?='ABC' | def?='DEF' | ghi?='GHI')
      ;
    

    It parses the following test file just fine:

    VL:'this is test dsl in xtext' IS ABC
    VL:'this is test dsl in xtext' IS DEF
    VL:'this is test dsl in xtext' IS GHI
    

    Cheers, Steve