Struct Pairs

struct Pairs<'i, R> { ... }

An iterator over Pairs. It is created by pest::state and Pair::into_inner.

Implementations

impl<'i, R: RuleType> Pairs<'i, R>

fn as_str(self: &Self) -> &'i str

Captures a slice from the &str defined by the starting position of the first token Pair and the ending position of the last token Pair of the Pairs. This also captures the input between those two token Pairs.

Examples

# use std::rc::Rc;
# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    a,
    b
}

let input = "a b";
let pairs = pest::state(input, |state| {
    // generating Token pairs with Rule::a and Rule::b ...
#     state.rule(Rule::a, |s| s.match_string("a")).and_then(|s| s.skip(1))
#         .and_then(|s| s.rule(Rule::b, |s| s.match_string("b")))
}).unwrap();

assert_eq!(pairs.as_str(), "a b");
fn get_input(self: &Self) -> &'i str

Returns the input string of Pairs.

This function returns the input string of Pairs as a &str. This is the source string from which Pairs was created. The returned &str can be used to examine the contents of Pairs or to perform further processing on the string.

Examples

# use std::rc::Rc;
# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    a,
    b
}

// Example: Get input string from Pairs

let input = "a b";
let pairs = pest::state(input, |state| {
    // generating Token pairs with Rule::a and Rule::b ...
#     state.rule(Rule::a, |s| s.match_string("a")).and_then(|s| s.skip(1))
#         .and_then(|s| s.rule(Rule::b, |s| s.match_string("b")))
}).unwrap();

assert_eq!(pairs.as_str(), "a b");
assert_eq!(input, pairs.get_input());
fn concat(self: &Self) -> String

Captures inner token Pairs and concatenates resulting &strs. This does not capture the input between token Pairs.

Examples

# use std::rc::Rc;
# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    a,
    b
}

let input = "a b";
let pairs = pest::state(input, |state| {
    // generating Token pairs with Rule::a and Rule::b ...
#     state.rule(Rule::a, |s| s.match_string("a")).and_then(|s| s.skip(1))
#         .and_then(|s| s.rule(Rule::b, |s| s.match_string("b")))
}).unwrap();

assert_eq!(pairs.concat(), "ab");
fn flatten(self: Self) -> FlatPairs<'i, R>

Flattens the Pairs.

Examples

# use std::rc::Rc;
# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    a,
    b
}

let input = "";
let pairs = pest::state(input, |state| {
    // generating nested Token pair with Rule::b inside Rule::a
#     state.rule(Rule::a, |state| {
#         state.rule(Rule::b, |s| Ok(s))
#     })
}).unwrap();
let tokens: Vec<_> = pairs.flatten().tokens().collect();

assert_eq!(tokens.len(), 4);
fn find_first_tagged(self: &Self, tag: &'i str) -> Option<Pair<'i, R>>

Finds the first pair that has its node or branch tagged with the provided label. Searches in the flattened Pairs iterator.

Examples

Try to recognize the branch between add and mul

use pest::{state, ParseResult, ParserState};
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    number, // 0..9
    add,    // num + num
    mul,    // num * num
}
fn mark_branch(
    state: Box<ParserState<'_, Rule>>,
) -> ParseResult<Box<ParserState<'_, Rule>>> {
    expr(state, Rule::mul, "*")
        .and_then(|state| state.tag_node("mul"))
        .or_else(|state| expr(state, Rule::add, "+"))
        .and_then(|state| state.tag_node("add"))
}
fn expr<'a>(
    state: Box<ParserState<'a, Rule>>,
    r: Rule,
    o: &'static str,
) -> ParseResult<Box<ParserState<'a, Rule>>> {
    state.rule(r, |state| {
        state.sequence(|state| {
            number(state)
                .and_then(|state| state.tag_node("lhs"))
                .and_then(|state| state.match_string(o))
                .and_then(number)
                .and_then(|state| state.tag_node("rhs"))
        })
    })
}
fn number(state: Box<ParserState<'_, Rule>>) -> ParseResult<Box<ParserState<'_, Rule>>> {
    state.rule(Rule::number, |state| state.match_range('0'..'9'))
}
let input = "1+2";
let pairs = state(input, mark_branch).unwrap();
assert_eq!(pairs.find_first_tagged("add").unwrap().as_rule(), Rule::add);
assert_eq!(pairs.find_first_tagged("mul"), None);
fn find_tagged(self: Self, tag: &'i str) -> Filter<FlatPairs<'i, R>, impl FnMut(&Pair<'i, R>) -> bool + 'i>

Returns the iterator over pairs that have their node or branch tagged with the provided label. The iterator is built from a flattened Pairs iterator.

Examples

Try to recognize the node between left and right hand side

use pest::{state, ParseResult, ParserState};
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    number, // 0..9
    add,    // num + num
    mul,    // num * num
}
fn mark_branch(
    state: Box<ParserState<'_, Rule>>,
) -> ParseResult<Box<ParserState<'_, Rule>>> {
    expr(state, Rule::mul, "*")
        .and_then(|state| state.tag_node("mul"))
        .or_else(|state| expr(state, Rule::add, "+"))
        .and_then(|state| state.tag_node("add"))
}
fn expr<'a>(
    state: Box<ParserState<'a, Rule>>,
    r: Rule,
    o: &'static str,
) -> ParseResult<Box<ParserState<'a, Rule>>> {
    state.rule(r, |state| {
        state.sequence(|state| {
            number(state)
                .and_then(|state| state.tag_node("lhs"))
                .and_then(|state| state.match_string(o))
                .and_then(number)
                .and_then(|state| state.tag_node("rhs"))
        })
    })
}
fn number(state: Box<ParserState<'_, Rule>>) -> ParseResult<Box<ParserState<'_, Rule>>> {
    state.rule(Rule::number, |state| state.match_range('0'..'9'))
}

let input = "1+2";
let pairs = state(input, mark_branch).unwrap();
let mut left_numbers = pairs.find_tagged("lhs");
assert_eq!(left_numbers.next().unwrap().as_str(), "1");
assert_eq!(left_numbers.next(), None);
fn tokens(self: Self) -> Tokens<'i, R>

Returns the Tokens for the Pairs.

Examples

# use std::rc::Rc;
# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Rule {
    a
}

let input = "";
let pairs = pest::state(input, |state| {
    // generating Token pair with Rule::a ...
#     state.rule(Rule::a, |s| Ok(s))
}).unwrap();
let tokens: Vec<_> = pairs.tokens().collect();

assert_eq!(tokens.len(), 2);
fn peek(self: &Self) -> Option<Pair<'i, R>>

Peek at the first inner Pair without changing the position of this iterator.

impl<'i, R: RuleType> Pairs<'i, R>

fn single(pair: Pair<'i, R>) -> Self

Create a new Pairs iterator containing just the single Pair.

impl<'i, R> Freeze for Pairs<'i, R>

impl<'i, R> RefUnwindSafe for Pairs<'i, R>

impl<'i, R> Send for Pairs<'i, R>

impl<'i, R> Sync for Pairs<'i, R>

impl<'i, R> Unpin for Pairs<'i, R>

impl<'i, R> UnsafeUnpin for Pairs<'i, R>

impl<'i, R> UnwindSafe for Pairs<'i, R>

impl<'i, R: $crate::clone::Clone> Clone for Pairs<'i, R>

fn clone(self: &Self) -> Pairs<'i, R>

impl<'i, R: Eq> Eq for Pairs<'i, R>

impl<'i, R: Hash> Hash for Pairs<'i, R>

fn hash<H: Hasher>(self: &Self, state: &mut H)

impl<'i, R: PartialEq> PartialEq for Pairs<'i, R>

fn eq(self: &Self, other: &Pairs<'i, R>) -> bool

impl<'i, R: RuleType> Debug for Pairs<'i, R>

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

impl<'i, R: RuleType> Display for Pairs<'i, R>

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

impl<'i, R: RuleType> DoubleEndedIterator for Pairs<'i, R>

fn next_back(self: &mut Self) -> Option<<Self as >::Item>

impl<'i, R: RuleType> ExactSizeIterator for Pairs<'i, R>

fn len(self: &Self) -> usize

impl<'i, R: RuleType> Iterator for Pairs<'i, R>

fn next(self: &mut Self) -> Option<<Self as >::Item>
fn size_hint(self: &Self) -> (usize, Option<usize>)

impl<I> IntoIterator for Pairs<'i, R>

fn into_iter(self: Self) -> I

impl<T> Any for Pairs<'i, R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Pairs<'i, R>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Pairs<'i, R>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Pairs<'i, R>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Pairs<'i, R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Pairs<'i, R>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for Pairs<'i, R>

fn to_string(self: &Self) -> String

impl<T, U> Into for Pairs<'i, R>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Pairs<'i, R>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Pairs<'i, R>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>