0
votes

I need to parse the following

model { 

// any content, including brackets {}

var x= {} ; 

// any content, including brackets {}
}

If I do it like this :

model : MODEL OBR modelBody CBR;

modelBody: modelBodyLine;

modelBodyLine: TEXT* (OBR TEXT* CBR)* TEXT*;

TEXT : ('a'..'z'|'A'..'Z'| '_' | '-')+ ;
OBR: '{';
CBR: '}';

I get this error

warning(200): /SWL Parser/src/ro/sft/swl/language/parser/SWL.g:46:16: Decision can match input such as "TEXT" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input |---> modelBodyLine: TEXT* (OBR TEXT* CBR)* TEXT*;

So what would be the best way to parse it ?

1

1 Answers

1
votes

I use the following genericBlock rule for these situations. This rule relies on your lexer to at minimum properly return a CBR token for the final } and to properly match a CBR token for every nested OBR token it creates.

genericBlock
  : OBR
    ( ~(OBR | CBR)
    | genericBlock
    )*
    CBR
  ;