Struct Grammar

Source
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

Source

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 Debug for Grammar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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)

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FromStr for Grammar

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl TryFrom<Vec<(String, Expr)>> for Grammar

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: Vec<(String, Expr)>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.