[−][src]Struct pest::prec_climber::PrecClimber
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.
Methods
impl<R: RuleType> PrecClimber<R>
[src]
pub 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) ]);
pub fn climb<'i, P, F, G, T>(&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,
[src]
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);
Trait Implementations
impl<R: Debug + RuleType> Debug for PrecClimber<R>
[src]
Auto Trait Implementations
impl<R> Send for PrecClimber<R> where
R: Send,
R: Send,
impl<R> Sync for PrecClimber<R> where
R: Sync,
R: Sync,
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,