Skip to main content

pratt_precedence

Macro pratt_precedence 

Source
macro_rules! pratt_precedence {
    (
        $(
            $first_head:ident :: $first_tail:ident $first_args:tt
            $( | $head:ident :: $tail:ident $args:tt )*
        ),* $(,)?
    ) => { ... };
    ($($t:tt)*) => { ... };
}
Expand description

Convenience macro for building a const Pratt parser precedence table.

Each argument is a precedence level: a list of Op constructors sharing the same precedence, separated by |. Levels are separated by ,; later levels bind more tightly than earlier ones.

Each operator must be written as a two-segment call, for example Op::infix(Rule::add, Assoc::Left). Fully qualified paths and turbofish forms are not accepted, because a macro matcher cannot follow an expression fragment with |. Import Op and use the short form.

§Example

static PRATT: ConstPrattParser<Rule, 7> = ConstPrattParser::new_const(pratt_precedence![
    Op::infix(Rule::add, Assoc::Left) | Op::infix(Rule::sub, Assoc::Left),
    Op::infix(Rule::mul, Assoc::Left) | Op::infix(Rule::div, Assoc::Left),
    Op::infix(Rule::pow, Assoc::Right),
    Op::prefix(Rule::neg),
    Op::postfix(Rule::fac),
]);

Fully qualified paths are rejected with a compile error:

static PRATT: ConstPrattParser<Rule, 2> = ConstPrattParser::new_const(pratt_precedence![
    pest::pratt_parser::Op::infix(Rule::add, Assoc::Left)
        | pest::pratt_parser::Op::infix(Rule::sub, Assoc::Left),
]);