Struct pidgin::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§

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.

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."));

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.