1# Error recovery (2022-05-07) 2 3Problem: we have two fairly generic cases of recovery bounded within a range: 4 - sequences: `int x; this is absolute garbage; x++;` 5 - brackets: `void foo() { this is garbage too }` 6 7So far, we've thought of these as different, and had precise ideas about 8brackets ("lazy parsing") and vague ones about sequences. 9Con we unify them instead? 10 11In both cases we want to recognize the bounds of the garbage item based on 12basic token-level features of the surrounding code, and avoid any interference 13with the surrounding code. 14 15## Brackets 16 17Consider a rule like `compound-stmt := { stmt-seq }`. 18 19The desired recovery is: 20- if we fail at `{ . stmt-seq }` 21- ... and we can find for the matching `}` 22- then consume up to that token as an opaque broken `stmt-seq` 23- ... and advance state to `{ stmt-seq . }` 24 25We can annotate the rule to describe this: `{ stmt-seq [recovery] }`. 26We can generalize as `{ stmt-seq [recovery=rbrace] }`, allowing for different 27**strategies** to find the resume point. 28 29(It's OK to assume we're skipping over one RHS nonterminal, we can always 30introduce a nonterminal for the bracket contents if necessary). 31 32## Sequences 33 34Can we apply the same technique to sequences? 35Simplest case: delimited right-recursive sequence. 36 37``` 38param-list := param 39param-list := param , param-list 40``` 41 42We need recovery in **both** rules. 43`param` in the first rule matches the *last* param in a list, 44in the second rule it matches all others. We want to recover in any position. 45 46If we want to be able to recovery `int x int y` as two parameters, then we 47should extract a `param-and-comma` rule that recovery could operate on. 48 49### Last vs not-last elements 50 51Sequences will usually have two rules with recovery, we discussed: 52 - how to pick the correct one to recover with 53 - in a left-recursive rule they correspond to last & not-last elements 54 - the recovery strategy knows whether we're recoverying last or not-last 55 - we could have the strategy return (pos, symbol parsed), and artificially 56 require distinct symbols (e.g. `stmt` vs `last_stmt`). 57 - we can rewrite left-recursion in the grammar as right-recursion 58 59However, on reflection I think we can simply allow recovery according to both 60rules. The "wrong" recovery will produce a parse head that dies. 61 62## How recovery fits into GLR 63 64Recovery should kick in at the point where we would otherwise abandon all 65variants of an high-level parse. 66 67e.g. Suppose we're parsing `static_cast<foo bar baz>(1)` and are up to `bar`. 68Our GSS looks something like: 69 70``` 71 "the static cast's type starts at foo" 72---> {expr := static_cast < . type > ( expr )} 73 | "foo... is a class name" 74 +---- {type := identifier .} 75 | "foo... is a template ID" 76 +---- {type := identifier . < template-arg-list >} 77``` 78 79Since `foo bar baz` isn't a valid class name or template ID, both active heads 80will soon die, as will the parent GSS Node - the latter should trigger recovery. 81 82- we need a refcount in GSS nodes so we can recognize never-reduced node death 83- when a node dies, we look up its recovery actions (symbol, strategy). 84 These are the union of the recovery actions for each item. 85 They can be stored in the action table. 86 Here: `actions[State, death] = Recovery(type, matching-angle-bracket)` 87- we try each strategy: feeding in the start position = token of the dying node 88 (`foo`) getting out the end position (`>`). 89- We form an opaque forest node with the correct symbol (`type`) spanning 90 [start, end) 91- We create a GSS node to represent the state after recovery. 92 The new state is found in the Goto table in the usual way. 93 94``` 95 "the static cast's type starts at foo" 96---> {expr := static_cast < . type > ( expr )} 97 | "`foo bar baz` is an unparseable type" 98 +---- {expr := static_cast < type . > (expr)} 99``` 100 101## Which recovery heads to activate 102 103We probably shouldn't *always* create active recovery heads when a recoverable 104node dies (and thus keep it alive). 105By design GLR creates multiple speculative parse heads and lets incorrect heads 106disappear. 107 108Concretely, the expression `(int *)(x)` is a valid cast, we probably shouldn't 109also parse it as a call whose callee is a broken expr. 110 111The simplest solution is to only create recovery heads if there are no normal 112heads remaining, i.e. if parsing is completely stuck. This is vulnerable if the 113"wrong" parse makes slightly more progress than the "right" parse which has 114better error recovery. 115 116A sophisticated variant might record recovery opportunities and pick the one 117with the rightmost *endpoint* when the last parse head dies. 118 119We should consider whether including every recovery in the parse forest might 120work after all - this would let disambiguation choose "broken" but likely parses 121over "valid" but unlikely ones. 122 123 124