Struct PrecClimber

struct PrecClimber<R: Clone + 'static> { ... }

List of operators and precedences, which can perform precedence climbing on infix expressions contained in a Pairs. The token pairs contained in the Pairs should start with a primary pair and then alternate between an operator and a primary.

Implementations

impl<R: RuleType> PrecClimber<R>

fn new(ops: Vec<Operator<R>>) -> PrecClimber<R>

Creates a new PrecClimber from the Operators contained in ops. Every entry in the Vec has precedence index + 1. In order to have operators with same precedence, they need to be chained with | between them.

Examples

# use pest::prec_climber::{Assoc, Operator, PrecClimber};
# #[allow(non_camel_case_types)]
# #[allow(dead_code)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
# enum Rule {
#     plus,
#     minus,
#     times,
#     divide,
#     power
# }
PrecClimber::new(vec![
    Operator::new(Rule::plus, Assoc::Left) | Operator::new(Rule::minus, Assoc::Left),
    Operator::new(Rule::times, Assoc::Left) | Operator::new(Rule::divide, Assoc::Left),
    Operator::new(Rule::power, Assoc::Right)
]);
fn climb<'i, P, F, G, T>(self: &Self, pairs: P, primary: F, infix: G) -> T
where
    P: Iterator<Item = Pair<'i, R>>,
    F: FnMut(Pair<'i, R>) -> T,
    G: FnMut(T, Pair<'i, R>, T) -> T

Performs the precedence climbing algorithm on the pairs in a similar manner to map-reduce. Primary pairs are mapped with primary and then reduced to one single result with infix.

Panics

Panics will occur when pairs is empty or when the alternating primary, operator, primary order is not respected.

Examples

let primary = |pair| {
    consume(pair, climber)
};
let infix = |lhs: i32, op: Pair<Rule>, rhs: i32| {
    match op.rule() {
        Rule::plus => lhs + rhs,
        Rule::minus => lhs - rhs,
        Rule::times => lhs * rhs,
        Rule::divide => lhs / rhs,
        Rule::power => lhs.pow(rhs as u32),
        _ => unreachable!()
    }
};

let result = climber.climb(pairs, primary, infix);

impl<R> Freeze for PrecClimber<R>

impl<R> RefUnwindSafe for PrecClimber<R>

impl<R> Send for PrecClimber<R>

impl<R> Sync for PrecClimber<R>

impl<R> Unpin for PrecClimber<R>

impl<R> UnsafeUnpin for PrecClimber<R>

impl<R> UnwindSafe for PrecClimber<R>

impl<R: $crate::fmt::Debug + Clone + 'static> Debug for PrecClimber<R>

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

impl<T> Any for PrecClimber<R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for PrecClimber<R>

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

impl<T> BorrowMut for PrecClimber<R>

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

impl<T> From for PrecClimber<R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for PrecClimber<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 PrecClimber<R>

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

impl<T, U> TryInto for PrecClimber<R>

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