Struct Grammar

Source
pub struct Grammar {
    pub name: Option<String>,
    /* private fields */
}
Expand description

A compiled collection of rules ready for the building of a Matcher or for use in the definition of a new rule.

You do not build new Grammars directly. Rather, the grammar! macro compiles them for you.

Fields§

§name: Option<String>

Implementations§

Source§

impl Grammar

Source

pub fn matcher(&self) -> Result<Matcher, Error>

Compiles a Matcher based on the Grammar’s rules.

§Examples
let m = grammar!{
	(?wbB)
	noun   => <person> | <place> | <thing>
	person => [["Moe", "Larry", "Curly", "Sade", "Diana Ross"]]
	place  => [["Brattleboro, Vermont"]]
	thing  => [["tiddly wink", "muffin", "kazoo"]]
}.matcher()?;
§Errors

If the Grammar contains an ill-formed r(rx) or one with a named capture which is repeated, an error will be returned.

Source

pub fn rule(&self, rule: &str) -> Option<Grammar>

Return a copy of one of the rules used by the grammar.

This is chiefly useful when combining grammars generated by the macro.

§Examples
#[macro_use] extern crate pidgin;
let library = grammar!{
    books => <cat> | <dog> | <camel>
    cat   => [["persian", "siamese", "calico", "tabby"]]
    dog   => [["dachshund", "chihuahua", "corgi", "malamute"]]
    camel => [["bactrian", "dromedary"]]
};
let g = grammar!{
    seen -> ("I saw a") g(library.rule("cat").unwrap()) (".")
};
let matcher = g.matcher().unwrap();
assert!(matcher.is_match("I saw a calico."));
Source

pub fn rx(&self) -> Result<Regex, Error>

Generates a non-capturing regex matching what the grammar matches.

§Examples
let g = grammar!{
    foo -> r(r"\A") <bar> r(r"\z")
    bar => (?i) [["cat", "camel", "corn"]]
};
let rx = g.rx()?.to_string();
assert_eq!(r"\A(?i:\s*c(?:orn|a(?:t|mel)))\s*\z", rx);
let g = grammar!{
    sentence    -> <capitalized_word> <other_words>? <terminal_punctuation>
    other_words -> <other_word>+
    other_word  -> <non_terminal_punctuation>? <word>
    capitalized_word         => r(r"\b[A-Z]\w*\b")
    word                     => r(r"\b\w+\b")
    terminal_punctuation     => r(r"[.?!]")
    non_terminal_punctuation => r("(?:--?|[,;'\"])")
};
let rx = g.rule("word").unwrap().rx().unwrap();
let p = g
    .matcher()?
    .parse("John, don't forget to pick up chips.")
    .unwrap();
let other_words = p.name("other_words").unwrap().as_str();
let other_words = rx
    .find_iter(other_words)
    .map(|m| m.as_str())
    .collect::<Vec<_>>();
assert_eq!(
    vec!["don", "t", "forget", "to", "pick", "up", "chips"],
    other_words
);

Trait Implementations§

Source§

impl Clone for Grammar

Source§

fn clone(&self) -> Grammar

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Grammar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Grammar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for Grammar

Source§

fn cmp(&self, other: &Grammar) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Grammar

Source§

fn eq(&self, other: &Grammar) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Grammar

Source§

fn partial_cmp(&self, other: &Grammar) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Grammar

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.