[][src]Struct bnf::Grammar

pub struct Grammar { /* fields omitted */ }

A Grammar is comprised of any number of Productions

Implementations

impl Grammar[src]

pub fn new() -> Grammar[src]

Construct a new Grammar

pub fn from_parts(v: Vec<Production>) -> Grammar[src]

Construct an Grammar from Productions

pub fn add_production(&mut self, prod: Production)[src]

Add Production to the Grammar

pub fn remove_production(&mut self, prod: &Production) -> Option<Production>[src]

Remove Production from the Grammar

pub fn productions_iter(&self) -> Iter<'_>[src]

Get iterator of the Grammar's Productions

pub fn productions_iter_mut(&mut self) -> IterMut<'_>[src]

Get mutable iterator of the Grammar's Productions

pub fn generate_seeded(&self, rng: &mut StdRng) -> Result<String, Error>[src]

Generate a random sentence from self and seed for random. Use if interested in reproducing the output generated. Begins from lhs of first production.

Example

extern crate bnf;
extern crate rand;
use rand::{SeedableRng, rngs::StdRng};
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar: Grammar = input.parse().unwrap();
    let seed: [u8; 32] = [0; 32];
    let mut rng: StdRng = SeedableRng::from_seed(seed);
    let sentence = grammar.generate_seeded(&mut rng);
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }

}

pub fn generate(&self) -> Result<String, Error>[src]

Generate a random sentence from self. Begins from lhs of first production.

Example

extern crate bnf;
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar: Grammar = input.parse().unwrap();
    let sentence = grammar.generate();
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }

}

Trait Implementations

impl Clone for Grammar[src]

impl Debug for Grammar[src]

impl Default for Grammar[src]

impl<'de> Deserialize<'de> for Grammar[src]

impl Display for Grammar[src]

impl Eq for Grammar[src]

impl FromStr for Grammar[src]

type Err = Error

The associated error which can be returned from parsing.

impl Hash for Grammar[src]

impl PartialEq<Grammar> for Grammar[src]

impl Serialize for Grammar[src]

impl StructuralEq for Grammar[src]

impl StructuralPartialEq for Grammar[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,