[][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 Clone for Grammar
[src]

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

Performs copy-assignment from source. 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 Sync for Grammar

Blanket Implementations

impl<T> From for T
[src]

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

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

type Owned = T

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

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

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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