fuel_pest::prec_climber

Struct PrecClimber

Source
pub struct PrecClimber<R: Clone + 'static> { /* private fields */ }
Expand description

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§

Source§

impl<R: RuleType> PrecClimber<R>

Source

pub 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
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)
]);
Source

pub fn climb<P, F, G, T>(&self, pairs: P, primary: F, infix: G) -> T
where P: Iterator<Item = Pair<R>>, F: FnMut(Pair<R>) -> T, G: FnMut(T, Pair<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§

Source§

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for PrecClimber<R>

§

impl<R> RefUnwindSafe for PrecClimber<R>
where R: RefUnwindSafe,

§

impl<R> Send for PrecClimber<R>
where R: Sync + Send,

§

impl<R> Sync for PrecClimber<R>
where R: Sync,

§

impl<R> Unpin for PrecClimber<R>
where R: Unpin,

§

impl<R> UnwindSafe for PrecClimber<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.