Struct Match

Source
pub struct Match<'t> { /* private fields */ }
Expand description

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.

Implementations§

Source§

impl<'t> Match<'t>

Source

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

Returns the matched text.

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

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

pub fn start(&self) -> usize

Returns the starting offset of the match.

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

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

pub fn end(&self) -> usize

Returns the ending offset of the match.

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

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

pub fn rule(&self) -> &str

Returns the grammar rule matched.

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

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

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

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());
Source

pub fn name(&self, name: &str) -> Option<&Match<'_>>

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());
Source

pub fn all_names(&self, name: &str) -> Vec<&Match<'_>>

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());
Source

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

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§

Source§

impl<'t> Clone for Match<'t>

Source§

fn clone(&self) -> Match<'t>

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

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

Performs copy-assignment from source. Read more
Source§

impl<'t> Debug for Match<'t>

Source§

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

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

impl<'t> Display for Match<'t>

Source§

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

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

impl<'t> Ord for Match<'t>

Source§

fn cmp(&self, other: &Match<'t>) -> 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<'t> PartialEq for Match<'t>

Source§

fn eq(&self, other: &Match<'t>) -> bool

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

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<'t> PartialOrd for Match<'t>

Source§

fn partial_cmp(&self, other: &Match<'t>) -> 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<'t> Eq for Match<'t>

Auto Trait Implementations§

§

impl<'t> Freeze for Match<'t>

§

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

§

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

§

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

§

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

§

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

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.