pub struct Grammar(/* private fields */);Expand description
A state machine that produces arbitrary matching expressions or byte sequences from Unstructured.
§Implementation
§Construction
Grammar is constructed using from_str of a string that specifies the syntax of expressions:
- A peg parser converts the string into an “intermediary representation” AST (in ir.rs).
- The IR is validated for duplicate and conflicting rule definitions
- The IR is converted to a state machine in which regex is parsed and rule definitions are indexed.
§Expression Generation
Unstructured is used to generate arbitrary choices and loops to traverse the state machine.
When a “leaf” state is reached, e.g. a pre-defined rule, regex, or string literal, Unstructured
is used to write arbitrary data (except for string literals) to an output buffer.
Implementations§
Source§impl Grammar
impl Grammar
Sourcepub fn expression<V: Visitor>(
&self,
u: &mut Unstructured<'_>,
max_depth: Option<usize>,
) -> Result<V>
pub fn expression<V: Visitor>( &self, u: &mut Unstructured<'_>, max_depth: Option<usize>, ) -> Result<V>
Returns a resulting Visitor after an arbitirary state machine traversal.
The state machine traversal starts at the first rule in the grammar.
Trait Implementations§
Source§impl Display for Grammar
Pretty prints the state machine.
impl Display for Grammar
Pretty prints the state machine.
It’s helpful to check if the compiled state machine matches what is expected from the the un-parsed grammar (the printed rules are more verbose and order of operations is clearer)