Grammar Kit: skipping input and resuming parsing for error recovery

Hi,

 

I'm currently struggling to implement error recovery in a grammar for a language based on S-expressions. A subset of the grammar is below as an example:

 

{
    tokens = [
        LPAREN = '('
        RPAREN = ')'
       IDENTIFIER = 'regexp:\.?[a-zA-Z][a-zA-Z\d\._]*'
        SPACE = 'regexp:[\s\n]'
    ]
}

Statement ::= BlockDeclStatement |
TypeDeclStatement |
TypeAttributeDeclStatement

BlockDeclStatement ::= '(' 'block' Identifier Statement* ')'
TypeDeclStatement ::= '(' 'type' Identifier ')'
TypeAttributeDeclStatement ::= '(' 'typeattribute' Identifier ')'

Identifier ::= IDENTIFIER

 

My problem comes from getting an invalid statement within a block like so:

(block name 
(type foo)
(bar))
^ expected one of block, type, typeattribute, found bar

 

When "bar" is encountered I'd like to skip input until the matching closing parenthesis is found and then resume with the "Statement*" rule in BlockDeclStatement. I'm not sure what the best way to go about this is. I'd assume the best place to start would be recoverWhile on "Statement" but I'm not quite sure where to proceed from there.

Any input would be appreciated.

1 comment
Comment actions Permalink

I've written a small TUTORIAL to walk people through the only 2 recovery concepts present in Grammar-Kit: "pin" and "recoverWhile".

https://github.com/JetBrains/Grammar-Kit/blob/master/TUTORIAL.md

0

Please sign in to leave a comment.