Struct pest::prec_climber::PrecClimber
[−]
[src]
pub struct PrecClimber<R: RuleType> { /* fields omitted */ }
A struct
useful in order to 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.
Methods
impl<R: RuleType> PrecClimber<R>
[src]
fn new(ops: Vec<Operator<R>>) -> PrecClimber<R>
[src]
Creates a new PrecClimber
from the Operator
s 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
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: Input, P, F, G, T>(&self, pairs: P, primary: F, infix: G) -> T where
P: Iterator<Item = Pair<R, I>>,
F: FnMut(Pair<R, I>) -> T,
G: FnMut(T, Pair<R, I>, T) -> T,
[src]
P: Iterator<Item = Pair<R, I>>,
F: FnMut(Pair<R, I>) -> T,
G: FnMut(T, Pair<R, I>, 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, StringInput>, 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);