Skip to main content

Crate grammar_kit

Crate grammar_kit 

Source
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 (via IdentExt).
  • 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§

ParseContext
Holds the state for backtracking and error reporting. This must be passed mutably through the parsing chain.
ScopeStack
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