Struct pidgin::Grammar [−][src]
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 Grammar
s directly. Rather, the grammar!
macro compiles them for you.
Fields
name: Option<String>
Implementations
impl Grammar
[src]
pub fn matcher(&self) -> Result<Matcher, Error>
[src]
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.
pub fn rule(&self, rule: &str) -> Option<Grammar>
[src]
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."));
pub fn rx(&self) -> Result<Regex, Error>
[src]
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
impl Clone for Grammar
[src]
impl Debug for Grammar
[src]
impl Display for Grammar
[src]
impl Eq for Grammar
[src]
impl Ord for Grammar
[src]
fn cmp(&self, other: &Grammar) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<Grammar> for Grammar
[src]
fn eq(&self, other: &Grammar) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl PartialOrd<Grammar> for Grammar
[src]
Auto Trait Implementations
impl RefUnwindSafe for Grammar
impl Send for Grammar
impl Sync for Grammar
impl Unpin for Grammar
impl UnwindSafe for Grammar
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,
pub fn borrow_mut(&mut self) -> &mut T
[src]
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.
pub fn to_owned(&self) -> T
[src]
pub 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.
pub 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>,