Struct PrattParser
struct PrattParser<R: RuleType> { ... }
Struct containing operators and precedences, which can perform Pratt parsing on
primary, prefix, postfix and infix expressions over Pairs. The tokens in Pairs
should alternate in the order:
prefix* ~ primary ~ postfix* ~ (infix ~ prefix* ~ primary ~ postfix*)*
Panics
Panics will occur when:
pairsis empty- The tokens in
pairsdoes not alternate in the expected order. - No
map_*function is specified for a certain kind of operator encountered inpairs.
Example
The following pest grammar defines a calculator which can be used for Pratt parsing.
WHITESPACE = _{ " " | "\t" | NEWLINE }
program = { SOI ~ expr ~ EOI }
expr = { prefix* ~ primary ~ postfix* ~ (infix ~ prefix* ~ primary ~ postfix* )* }
infix = _{ add | sub | mul | div | pow }
add = { "+" } // Addition
sub = { "-" } // Subtraction
mul = { "*" } // Multiplication
div = { "/" } // Division
pow = { "^" } // Exponentiation
prefix = _{ neg }
neg = { "-" } // Negation
postfix = _{ fac }
fac = { "!" } // Factorial
primary = _{ int | "(" ~ expr ~ ")" }
int = @{ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT+ | ASCII_DIGIT) }
Below is a PrattParser that is able to parse an expr in the above grammar. The order
of precedence corresponds to the order in which op is called. Thus, mul will
have higher precedence than add. Operators can also be chained with | to give them equal
precedence.
# use ;
#
#
let pratt =
new
.op
.op
.op
.op
.op;
To parse an expression, call the map_primary, map_prefix, map_postfix,
map_infix and parse methods as follows:
# use ;
#
#
Note that map_prefix, map_postfix and map_infix only need to be specified if the
grammar contains the corresponding operators.
Implementations
impl<R: RuleType> PrattParser<R>
fn new() -> SelfInstantiate a new
PrattParser.fn op(self: Self, op: Op<R>) -> SelfAdd
optoPrattParser.fn map_primary<'pratt, 'a, 'i, X, T>(self: &'pratt Self, primary: X) -> PrattParserMap<'pratt, 'a, 'i, R, X, T> where X: FnMut(Pair<'i, R>) -> T, R: 'prattMaps primary expressions with a closure
primary.
impl<R> Freeze for PrattParser<R>
impl<R> RefUnwindSafe for PrattParser<R>
impl<R> Send for PrattParser<R>
impl<R> Sync for PrattParser<R>
impl<R> Unpin for PrattParser<R>
impl<R> UnsafeUnpin for PrattParser<R>
impl<R> UnwindSafe for PrattParser<R>
impl<R: RuleType> Default for PrattParser<R>
fn default() -> Self
impl<T> Any for PrattParser<R>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for PrattParser<R>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for PrattParser<R>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for PrattParser<R>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for PrattParser<R>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for PrattParser<R>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for PrattParser<R>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>