About
exception handling in a parser.
By default, parser will stop matching on any failure.
But they may have a sort of recovery mode that allows to define an alternate rule or match .
Example
extendedPin mode
For idea, it's called the extendedPin mode that defines the pin position (ie the token position in the rule).
Everything after the pin position is collected:
- as long as the rest of the rule components match;
- or a alternate rule (recoverWhile) holds true;
- or until the end of the file is reached.
The collected data will show up as:
- either expected token
- or an error block.
For instance:
property ::= id '=' expr {pin=2 recoverWhile=rule_recover}
private rule_recover ::= !(';' | id '=')
means:
- The matching of the property rule is considered successful if we get through the = part (due to pin=2)
- If there is a failure, skip all the tokens while rule_recover matches, i.e. while the parser doesn't encounter:
- ;
- or a rule start (id and =)
Note that recovery rule is always a predicate (a NOT predicate usually) hence it doesn't consume anything from the input.
Note that:
- the rule will not match at all until you get to the pin position
- the default value of pin is 1