Search code examples
parsingintellij-ideaintellij-pluginbnfgrammar-kit

Pin & recoverWhile in a .bnf (Parsing)


I've searched the internet far and wide (for at least half a day now) and I can't seem to find the answers needed.

Currently I'm trying to create a .bnf-file for an IntelliJ-Plugin with custom language support.

A few tutorials mention the existance of {pin=1},{pin=2} and {recoverWhile=xyz}, but I didn't find any real explanation on their uses, and if there are any other things I should know (maybe a {pin=3} also exists?).

So could somebody tell me what exactly those flags, methods or however they're called are, and how to use them in my .bnf, please?

Thank you for your help and best regards, Fuchs


Solution

  • These attributes are explained here:

    https://github.com/JetBrains/Grammar-Kit/blob/master/HOWTO.md#22-using-recoverwhile-attribute https://github.com/JetBrains/Grammar-Kit/blob/master/TUTORIAL.md

    But the usage is not trivial. A good idea is to use Live Preview to play around with it.

    My understanding:

    Pin and recoverWhile attributes are used to recover parser from errors.

    Pin specifies a part of the rule (by index or literally) after successful parsing of which the rule considered successful. In the example:

    expr ::= expr1 "+" expr2 {pin=1}
    

    if expr1 is matched, the whole rule will be considered successful and parser will try yo match the rest.

    if pin=2 the rule will be considered successful after matching "+" and will fail if expr1 or "+" not matched.

    RecoverWhile attribute specifies where to skip after parsing the rule. Independently of its success. For example

    {recoverWhile=expr_recover}
    expr_recover ::= !(";" | ".")
    

    will skip all input before ";" or ".". I.e. parser will start matching next rule from ";" or ".".