Expand description
§grammar-kit
grammar-kit is the runtime support library for parsers generated by syn-grammar. It provides essential utilities for backtracking, error recovery, and testing syn-based parsers.
§Features
§1. Speculative Parsing (Backtracking)
The attempt function allows parsers to try a parsing branch and revert the stream cursor if it fails, without consuming tokens. This enables LL(k) lookahead and backtracking logic.
§2. Intelligent Error Reporting
grammar-kit implements a “Deepest Error” heuristic. When multiple parsing branches fail, it preserves the error that occurred furthest into the token stream. This prevents generic “unexpected token” errors at the start of a block when a specific syntax error occurred deep inside it.
§3. Testing Framework
The testing module provides a fluent API for unit testing your parsers.
§Installation
Add this to your Cargo.toml:
[dependencies]
grammar-kit = "0.3.0"§Runtime Helpers
The library exposes several helper functions used by generated parsers:
attempt: Forks the input, runs a closure, and advances only on success.parse_ident: Parses identifiers, accepting Rust keywords (viaIdentExt).parse_int: Parses integer literals into typed Rust integers.skip_until: Skips tokens until a specific condition is met (used for error recovery).
Modules§
- testing
- Utilities for testing parsers generated by
syn-grammar.
Structs§
- Parse
Context - Holds the state for backtracking and error reporting. This must be passed mutably through the parsing chain.
- Scope
Stack - Generic symbol table that tracks variable definitions in nested scopes.
Functions§
- attempt
- Encapsulates a speculative parse attempt. Requires passing the ParseContext to manage error state.
- attempt_
recover - Wrapper around attempt used specifically for recovery blocks.
- parse_
ident - parse_
int - skip_
until