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>
impl<'t> Match<'t>
Sourcepub fn as_str(&self) -> &'t str
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());
Sourcepub fn start(&self) -> usize
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());
Sourcepub fn end(&self) -> usize
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());
Sourcepub fn rule(&self) -> &str
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());
Sourcepub fn children(&self) -> Option<&[Match<'t>]>
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());
Sourcepub fn name(&self, name: &str) -> Option<&Match<'_>>
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());
Sourcepub fn all_names(&self, name: &str) -> Vec<&Match<'_>>
pub fn all_names(&self, name: &str) -> Vec<&Match<'_>>
Returns all Match
es 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());
Sourcepub fn has(&self, name: &str) -> bool
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"));