[][src]Struct pidgin::Match

pub struct Match<'t> { /* fields omitted */ }

This is a node in a parse tree. It is functionally similar to regex::Match, in fact providing much the same API, but unlike a regex::Match a pidgin::Match always corresponds to some rule, it knows what rule it corresponds to, and it records any sub-matches involved in its parsing.

The lifetime parameter 't represents the lifetime of the &str matched against.

Methods

impl<'t> Match<'t>[src]

pub fn as_str(&self) -> &'t str[src]

Returns the matched text.

Examples

let m = grammar!{
	foo => ("bar")
}.matcher()?.parse("   bar   ").unwrap();

assert_eq!("bar", m.as_str());

pub fn start(&self) -> usize[src]

Returns the starting offset of the match.

Examples

let m = grammar!{
	foo => ("bar")
}.matcher()?.parse("   bar   ").unwrap();

assert_eq!(3, m.start());

pub fn end(&self) -> usize[src]

Returns the ending offset of the match.

Examples

let m = grammar!{
	foo => ("bar")
}.matcher()?.parse("   bar   ").unwrap();

assert_eq!(6, m.end());

pub fn rule(&self) -> &str[src]

Returns the grammar rule matched.

Examples

let m = grammar!{
	foo => ("bar")
}.matcher()?.parse("   bar   ").unwrap();

assert_eq!("foo", m.rule());

pub fn children(&self) -> Option<&[Match<'t>]>[src]

Returns the sub-matches of this match, if any.

Examples

let m = grammar!{
	TOP -> <foo> <bar> <baz>
	foo => (1)
	bar => (2)
	baz => (3)
}.matcher()?.parse(" 1  2  3 ").unwrap();

let children = m.children().unwrap();

assert_eq!(3, children.len());
assert_eq!("1", children[0].as_str());
assert_eq!("foo", children[0].rule());
assert_eq!("2", children[1].as_str());
assert_eq!("bar", children[1].rule());
assert_eq!("3", children[2].as_str());
assert_eq!("baz", children[2].rule());

pub fn name(&self, name: &str) -> Option<&Match>[src]

Returns the first Match defined by the given rule under this parse node searching recursively, depth-first, left-to-right.

Examples

let matcher = grammar!{
	TOP -> <foo> <bar>
	foo -> (1) <baz>
	bar -> (2) <baz>
	baz -> [["prawn", "shrimp", "crevette"]]
}.matcher()?;

let p = matcher.parse("1 crevette 2 shrimp").unwrap();
let baz = p.name("baz").unwrap();

assert_eq!("crevette", baz.as_str());

pub fn all_names(&self, name: &str) -> Vec<&Match>[src]

Returns all Matches matching the given rule in the parse tree under this node. Matches are ordered as found by a depth-first left-to-right search of the parse tree.

Examples

let matcher = grammar!{
	TOP -> <foo> <bar>
	foo -> (1) <baz>
	bar -> (2) <baz>
	baz -> [["prawn", "shrimp", "crevette"]]
}.matcher()?;

let p = matcher.parse("1 crevette 2 shrimp").unwrap();
let names = p.all_names("baz");

assert_eq!("crevette", names[0].as_str());
assert_eq!("shrimp", names[1].as_str());

pub fn has(&self, name: &str) -> bool[src]

Returns whether the given rule matched for any node in the parse tree.

Examples

let g = grammar!{
    TOP => <animal> | <thing>
    animal => [["cat", "dog", "camel"]]
    thing  => [["carpet", "crate", "cartoon"]]
};

let m = g.matcher()?;

assert!(m.parse("cat").unwrap().has("animal"));

Trait Implementations

impl<'t> Eq for Match<'t>[src]

impl<'t> Clone for Match<'t>[src]

impl<'t> PartialOrd<Match<'t>> for Match<'t>[src]

impl<'t> Ord for Match<'t>[src]

impl<'t> PartialEq<Match<'t>> for Match<'t>[src]

impl<'t> Display for Match<'t>[src]

impl<'t> Debug for Match<'t>[src]

Auto Trait Implementations

impl<'t> Unpin for Match<'t>

impl<'t> Sync for Match<'t>

impl<'t> Send for Match<'t>

impl<'t> UnwindSafe for Match<'t>

impl<'t> RefUnwindSafe for Match<'t>

Blanket Implementations

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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

impl<T> From<T> for T[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.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]