Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abusing scanner states for polymorphic grammar rules #4

Open
cdlm opened this issue Nov 5, 2018 · 1 comment
Open

Abusing scanner states for polymorphic grammar rules #4

cdlm opened this issue Nov 5, 2018 · 1 comment

Comments

@cdlm
Copy link

cdlm commented Nov 5, 2018

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
Copy link
Author

cdlm commented Nov 5, 2018

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>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant