[][src]Struct bnf::Grammar

pub struct Grammar { /* fields omitted */ }

A Grammar is comprised of any number of Productions

Methods

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 from_str(s: &str) -> Result<Self, Error>[src]

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, StdRng};
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar = Grammar::from_str(input).unwrap();
    let seed: &[_] = &[1,2,3,4];
    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::from_str(input).unwrap();
    let sentence = grammar.generate();
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }

}

Trait Implementations

impl PartialEq<Grammar> for Grammar[src]

impl Eq for Grammar[src]

impl Clone for Grammar[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Hash for Grammar[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Debug for Grammar[src]

impl Display for Grammar[src]

impl FromStr for Grammar[src]

type Err = Error

The associated error which can be returned from parsing.

Auto Trait Implementations

impl Send for Grammar

impl Unpin for Grammar

impl Sync for Grammar

impl UnwindSafe for Grammar

impl RefUnwindSafe for Grammar

Blanket Implementations

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

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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