[−][src]Struct liblet::production::Production
The main type of this module.
It allows to abstract over grammar productions, having a defined left and right and side, which are ordered collections of symbols.
A production can be created easily from strings, following these rules:
- string can contain any number of whitespace
- multiple productions are divided by '\n' char
- string must have a sequence of symbol to the left of a "->"
- string must have a sequence of alternatives to the right of a "->"
- string can have only one "->" per production (per line)
- a sequence of symbol is a sequence of symbols divided by some whitespace
- a sequence of alternative is one ore more sequence of symbols divided by the "|" char
- string can't contain anything else
Examples
A -> B
leads to a production from the symbol A
to the symbol B
A -> B | C
leads to two productions, one for each alternative (A
-> B
and A
-> C
)
A -> B\nA -> C
leads to the same result of the above one, defining the two productions separately
A ->
will results in an error, because there's no right hand side for the production
Methods
impl Production
[src]
pub fn new<I>(lhs: I, rhs: I) -> Result<Production, ProductionError> where
I: IntoIterator<Item = Symbol>,
[src]
I: IntoIterator<Item = Symbol>,
Creates a new Production based on the left and right hand side given as collections of symbols.
Errors
It can return an error if either the left or right hand side is empty.
Examples
use liblet::production::Production; use liblet::symbol::symbol; // create left and right hand side for production "A -> B C" let lhs = vec![symbol("A")]; let rhs = vec![symbol("B"), symbol("C")]; // create a new production based on the previously defined left and right hand side let production = Production::new(lhs, rhs); assert!(production.is_ok());
pub fn new_from_string<'a, I>(
lhs: I,
rhs: I
) -> Result<Production, ProductionError> where
I: IntoIterator<Item = &'a str>,
[src]
lhs: I,
rhs: I
) -> Result<Production, ProductionError> where
I: IntoIterator<Item = &'a str>,
Creates a new Production based on the left and right hand side given as collections of &str
.
Errors
It can return an error if the strings are not convertible to Symbols. Otherwise, it acts like new.
Examples
use liblet::production::Production; use liblet::symbol::symbol; // create left and right hand side for production "A -> B C" let lhs = vec!["A"]; let rhs = vec!["B", "C"]; // create a new production based on the previously defined left and right hand side let production = Production::new_from_string(lhs, rhs); assert!(production.is_ok());
pub fn lhs(&self) -> Vec<Symbol>
[src]
Return the ordered collection of symbol representing the left hand side of the production.
Examples
use liblet::production::{Production,production}; use liblet::symbol::symbol; // create a production representing "A -> B C" let p = production("A", "B C"); assert_eq!(p.lhs(), vec![symbol("A")]);
pub fn rhs(&self) -> Vec<Symbol>
[src]
Return the ordered collection of symbol representing the right hand side of the production.
Examples
use liblet::production::{Production,production}; use liblet::symbol::symbol; // create a production representing "A -> B C" let p = production("A", "B C"); assert_eq!(p.rhs(), vec![symbol("B"), symbol("C")]);
pub fn symbols(&self) -> HashSet<Symbol>
[src]
Return a set containing all the different symbols which appears on the left and right hand side of the production.
Examples
use liblet::production::{Production,production}; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a production representing "A -> B C" let p = production("A", "B C"); let symbols: HashSet<Symbol> = vec![symbol("A"), symbol("B"), symbol("C")].into_iter().collect(); assert_eq!(p.symbols(), symbols);
pub fn from_string(string: &str) -> Result<Vec<Production>, ProductionError>
[src]
Create a collection of productions from a raw input string.
Errors
Can return an error if the raw string can't be parsed to obtain actual productions both due to wrong string formatting (LHS -> RHS | ...)(see from_string method from symbols for more info about string formatting symbols) and due to production creation error (see production constructor for more info).
In the case an empty or whitespace only string is given, it just returns an empty collection of productions.
Examples
use liblet::production::Production; let result = Production::from_string(" A -> B C B -> b ")?; assert_eq!(result.len(), 2);
pub fn from_iter<'a, I>(strings: I) -> Result<Vec<Production>, ProductionError> where
I: IntoIterator<Item = &'a str>,
[src]
I: IntoIterator<Item = &'a str>,
Create a collection of productions from a collection of raw input string.
Same as from_string but accepts an IntoIterator
.
Errors
Can return an error if any of the raw strings can't be parsed to obtain actual productions both due to wrong string formatting (LHS -> RHS | ...)(see from_string method from symbols for more info about string formatting symbols) and due to production creation error (see production constructor for more info).
In the case empty or whitespace only strings are given, it just returns an empty collection of productions.
Examples
use liblet::production::Production; let result = Production::from_iter(vec![ "A -> B C", "B -> b" ])?; assert_eq!(result.len(), 2);
pub fn such_that(
predicate: ProductionPredicate
) -> Box<dyn FnMut(&&Production) -> bool>
[src]
predicate: ProductionPredicate
) -> Box<dyn FnMut(&&Production) -> bool>
Return a boxed FnMut
which accepts a production and test the given predicate on it, returning a bool
ean value.
Examples
use liblet::production::{Production,ProductionPredicate}; use liblet::symbol::symbol; let p = Production::from_string(" A -> B C B -> b ")?; let closure = Production::such_that(ProductionPredicate::RhsEquals(vec![symbol("b")])); let expected = Production::new(vec![symbol("B")], vec![symbol("b")])?; let mut result = p.iter().filter(closure); assert_eq!(result.next(), Some(&expected)); assert_eq!(result.next(), None);
Trait Implementations
impl Clone for Production
[src]
fn clone(&self) -> Production
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for Production
[src]
impl<'de> Deserialize<'de> for Production
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl Display for Production
[src]
impl Eq for Production
[src]
impl Hash for Production
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl Ord for Production
[src]
fn cmp(&self, other: &Production) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[src]
impl PartialEq<Production> for Production
[src]
fn eq(&self, other: &Production) -> bool
[src]
fn ne(&self, other: &Production) -> bool
[src]
impl PartialOrd<Production> for Production
[src]
fn partial_cmp(&self, other: &Production) -> Option<Ordering>
[src]
fn lt(&self, other: &Production) -> bool
[src]
fn le(&self, other: &Production) -> bool
[src]
fn gt(&self, other: &Production) -> bool
[src]
fn ge(&self, other: &Production) -> bool
[src]
impl Serialize for Production
[src]
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl StructuralEq for Production
[src]
impl StructuralPartialEq for Production
[src]
impl<'_> TryFrom<&'_ str> for Production
[src]
Auto Trait Implementations
impl RefUnwindSafe for Production
impl Send for Production
impl Sync for Production
impl Unpin for Production
impl UnwindSafe for Production
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,