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.

§Parameters

max_depth is nesting limit of rule referencing during the traversal. For example, the below grammar generates numbers less than or equal to the max_depth parameter.

one   : "1" | two ;
two   : "2" | three ;
three : "3" ;
Source

pub fn how_many(&self, max_depth: Option<usize>) -> Option<u64>

Returns the number of possible state machine traversals for this state machine or None if the result exceeds u64::MAX.

§Parameters

max_depth is the maximum nested references that the traversal is allowed. A max_depth of 0 will always return 0. grammar.how_many(depth) is the number of unique values possible from grammar.expression::<u64>(u, depth), barring hash collisions.

§Usage

Provides a rough possible number of equivalence classes of the grammar, which is useful for estimating overall coverage as the fuzzer discovers more classes over time.

§Limitations
  1. No traversals are counted inside of Regex and pre-defined (e.g. String) rules i.e. are counted as 1 possible traversal even though many regex expressions or Strings are possible.

  2. The result is not aware of duplicate outputs, e.g.

expr : "foo" | "foo" ;

counts as 2 traversals, even though every output is “foo”.

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.