pub struct Grammar {
pub rules: Vec<RuleDef>,
pub ast_config: AstConfig,
}Expand description
Main grammar builder
Fields§
§rules: Vec<RuleDef>§ast_config: AstConfigImplementations§
Source§impl Grammar
impl Grammar
pub fn new() -> Self
Sourcepub fn ast_config<F>(self, f: F) -> Self
pub fn ast_config<F>(self, f: F) -> Self
Configure AST integration settings
Sourcepub fn build(self) -> CompiledGrammar
pub fn build(self) -> CompiledGrammar
Finalize and validate the grammar
Sourcepub fn build_optimized(self) -> CompiledGrammar
pub fn build_optimized(self) -> CompiledGrammar
Build with automatic backtracking optimization
This is equivalent to calling .build().optimize_backtracking().
It detects Choice nodes with shared prefixes containing recursive rules
and rewrites them to factor out the common prefix.
Sourcepub fn build_with_memoization(self) -> CompiledGrammar
pub fn build_with_memoization(self) -> CompiledGrammar
Build with automatic memoization to avoid exponential backtracking.
This analyzes the grammar to identify rules that would benefit from
memoization and automatically wraps them. Use this when you have
patterns that cause exponential backtracking (like TypeScript’s
identifier<types>(args) vs comparison operators).
The process:
- Analyze the grammar to find Choice nodes with shared recursive prefixes
- Identify rule references in those prefixes
- Wrap those rules with memoization
- Build the grammar
Sourcepub fn build_optimized_with_memoization(self) -> CompiledGrammar
pub fn build_optimized_with_memoization(self) -> CompiledGrammar
Build with both prefix factoring optimization and automatic memoization.
This combines the benefits of both optimization strategies:
- Prefix factoring rewrites Choice nodes to factor out common prefixes
- Memoization caches results for rules that cause exponential backtracking
Use this for the most comprehensive backtracking prevention.