j-brant / SmaCC

MIT License
24 stars 16 forks source link

Abusing scanner states for polymorphic grammar rules #4

Open cdlm opened 6 years ago

cdlm commented 6 years ago

Feature suggestion: writing a grammar rule in terms of tokens that have different definitions depending on the scanner state…

%states default insideBraces insideParens;

insideBraces <any> : [^{}]+ ;
insideBraces <leftDelimiter> : \{ ;
insideBraces <rightDelimiter> : \} ;

insideParens <any> : [^()]+ ;
insideParens <leftDelimiter> : \( ;
insideParens <rightDelimiter> : \) ;

# this rule would only be used when the scanner is in one of the two states above
DelimitedContent
    : <any> 'content' {{}}
    | <leftDelimiter> 'content'
        DelimitedContent 'content' *
        <rightDelimiter> 'content' {{}}
;
cdlm commented 6 years ago

After discussion with @ToshRaka I got a working parser by duplicating the rule into ParenthesizedContent and BracedContent, and the <any> tokens into <notBraces> and <notParens>.