feat: add RecoverTo option for fault-tolerant parsing - #472
Open
Pastalikek65 wants to merge 1 commit into
Open
Conversation
RecoverTo(types...) enables skipping malformed input regions and resuming parse at the next token that can begin a registered production. It works inside repetition loops (struct slices parsed with @@* or @@+): when an element fails, the lexer scans forward for a synchronization token while never crossing structural literals (such as closing delimiters), then restarts the loop there. - recovery.go: startSet/nullable analysis over the node graph (literal, reference, strct, disjunction, sequence, capture, group, union), build-time resolution, and maybeRecover forward scan with lastRecovery progress guard. - nodes.go: hooks in group.Parse for both error and empty-match paths. - context.go/parser.go: recoverNode+structural sets threaded into parseContext. Full test suite green; fuzzed 45s / ~300k execs with no hang, panic or explosion. Closes alecthomas#342.
This was referenced Aug 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #342
Implements opt-in fault-tolerant parsing via a new
RecoverTo(types...)build option.What it does
When parsing fails, the parser scans forward from the error position for the first token that could begin one of the registered productions, skips the malformed region, and resumes parsing from that synchronization point. The partial AST up to the failure is retained (per participle's existing best-effort behavior).
Input like
call one broken call twoparses to[call:one, call:two]—brokenis skipped.Design
recovery.go): computes, per registered production, the token predicates that can begin it. Handlesliteral(incl. type constraints and case-insensitivity),reference,strct,disjunction,sequence(with nullability propagation through nullable heads),capture,groupandunion. Opaque nodes (custom,parseable,negation,lookaheadGroup) contribute nothing.maybeRecover): scans via a speculative branch (Next()skips elided tokens), never crosses structural literals (tokens appearing verbatim in the grammar that aren't sync points — e.g. closing delimiters), and guarantees termination via:lastRecoverycursor guard prevents re-recovering at the same position.nodes.go): only inside repetition loops (group*/+), on both the error path and the zero-match path. This keeps the change surgical and avoids the O(n²) blow-up a strct-level hook produced during development.RecoverToon a type not in the grammar is a build error.Testing
11 unit tests covering: skip-malformed, resume-after-brace (nested blocks,
}not crossed), no-sync-token falls back to original error, no-option no-op, multiple malformed,UseLookaheadinteraction, case-insensitive literals, unknown-type build error, unions,@@+(one-or-more), and struct-level sync on a recursive grammar.Fuzzed 45s / ~300k executions with no hang, panic or explosion (a fuzzer found two non-termination cases during development that the
lastRecoveryguard + structural-literal rules resolved).Open question for maintainer
RecoverTois deliberately sync-only (productions are not re-parsed into the AST after recovery); re-parsing recovered nodes would be a natural follow-up but adds complexity. Happy to extend if desired.