12

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

yole
  • 92,896
  • 20
  • 260
  • 197
Fuchs
  • 133
  • 6
  • 2
    Thank you for your help Bart. However, the links you send me may give an explanation on how recoverWhile work, but once again pins are just used without explaining it at all, as if I could magically understand it without anybody ever explaining it anywhere. – Fuchs Feb 09 '18 at 09:09
  • 1
    The value assigned to pin is the 1-based index of the term in the grammar rule through which a match attempt should not be undone in the event of an error. If the value of pin is 2 then the matching process is "pinned" at the second term upon matching. The first term may match but while this may not be considered to be definitive, matching the second term should be. Under an error recovery scenario the rule must be considered to have matched even though an error is defined as "nothing matches". Note that errors are otherwise horribly ambiguous. – Chris Mountford Nov 11 '20 at 01:14

1 Answers1

17

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

mathze
  • 458
  • 1
  • 6
  • 11
Argb32
  • 1,365
  • 8
  • 10