[][src]Struct liblet::production::Production

pub struct Production { /* fields omitted */ }

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]

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]

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]

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]

Return a boxed FnMut which accepts a production and test the given predicate on it, returning a boolean 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]

impl Debug for Production[src]

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

impl Display for Production[src]

impl Eq for Production[src]

impl Hash for Production[src]

impl Ord for Production[src]

impl PartialEq<Production> for Production[src]

impl PartialOrd<Production> for Production[src]

impl Serialize for Production[src]

impl StructuralEq for Production[src]

impl StructuralPartialEq for Production[src]

impl<'_> TryFrom<&'_ str> for Production[src]

type Error = ProductionError

The type returned in the event of a conversion error.

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> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

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

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

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, 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.