pub struct Grammar { /* private fields */ }
Expand description
A Grammar is comprised of any number of Productions
Implementations§
source§impl Grammar
impl Grammar
sourcepub fn from_parts(v: Vec<Production>) -> Grammar
pub fn from_parts(v: Vec<Production>) -> Grammar
Construct an Grammar
from Production
s
sourcepub fn add_production(&mut self, prod: Production)
pub fn add_production(&mut self, prod: Production)
Add Production
to the Grammar
sourcepub fn remove_production(&mut self, prod: &Production) -> Option<Production>
pub fn remove_production(&mut self, prod: &Production) -> Option<Production>
Remove Production
from the Grammar
sourcepub fn productions_iter(&self) -> impl Iterator<Item = &Production>
pub fn productions_iter(&self) -> impl Iterator<Item = &Production>
Get iterator of the Grammar
’s Production
s
sourcepub fn productions_iter_mut(&mut self) -> impl Iterator<Item = &mut Production>
pub fn productions_iter_mut(&mut self) -> impl Iterator<Item = &mut Production>
Get mutable iterator of the Grammar
’s Production
s
sourcepub fn parse_input<'gram>(
&'gram self,
input: &'gram str
) -> impl Iterator<Item = ParseTree<'_>>
pub fn parse_input<'gram>( &'gram self, input: &'gram str ) -> impl Iterator<Item = ParseTree<'_>>
Parse input strings according to Grammar
sourcepub fn generate_seeded(&self, rng: &mut StdRng) -> Result<String, Error>
pub fn generate_seeded(&self, rng: &mut StdRng) -> Result<String, Error>
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
use rand::{SeedableRng, rngs::StdRng};
use bnf::Grammar;
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)
}
sourcepub fn generate_seeded_callback(
&self,
rng: &mut StdRng,
f: impl Fn(&str, &str) -> bool
) -> Result<String, Error>
pub fn generate_seeded_callback( &self, rng: &mut StdRng, f: impl Fn(&str, &str) -> bool ) -> Result<String, Error>
Does the same as Grammar::generate_seeded
, except it takes a callback which is
executed on every production that is generated to check if it is okay.
When the callback returns true
, the generation continues as normal,
but when the callback returns false
, a new random option is tried.
The first parameter to the callback is the current production name, and the second parameter is the value that was attempted to be generated, but may be rejected.
sourcepub fn generate(&self) -> Result<String, Error>
pub fn generate(&self) -> Result<String, Error>
Generate a random sentence from self. Begins from lhs of first production.
Example
use bnf::Grammar;
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)
}
sourcepub fn generate_callback(
&self,
f: impl Fn(&str, &str) -> bool
) -> Result<String, Error>
pub fn generate_callback( &self, f: impl Fn(&str, &str) -> bool ) -> Result<String, Error>
Does the same as Grammar::generate
, except it takes a callback which is
executed on every production that is generated to check if it is okay.
When the callback returns true
, the generation continues as normal,
but when the callback returns false
, a new random option is tried.
The first parameter to the callback is the current production name, and the second parameter is the value that was attempted to be generated, but may be rejected.