From 011fcf828ebd1325dbd4dfa21c4952f1be38a29f Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 6 Aug 2024 12:51:38 -0400 Subject: roll: fixup for pratt parser --- src/commands/roll.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/commands/roll.rs b/src/commands/roll.rs index 6cd084c..abd7466 100644 --- a/src/commands/roll.rs +++ b/src/commands/roll.rs @@ -1,11 +1,11 @@ -use std::result::Result as StdResult; - use lazy_static::lazy_static; use log::{ debug, error, }; +use pest::iterators::Pair; use rand::prelude::*; +use std::result::Result as StdResult; use thiserror::Error; use crate::{ @@ -32,10 +32,7 @@ pub(crate) enum CalcError { impl Calc { fn eval>(s: S) -> StdResult { use pest::{ - iterators::{ - Pair, - Pairs, - }, + iterators::Pairs, pratt_parser::PrattParser, Parser, }; @@ -46,16 +43,15 @@ impl Calc { static ref CLIMBER: PrattParser = { use pest::pratt_parser::{ Assoc::*, - Op as Operator, + Op, }; PrattParser::new() - .op(Operator::infix(add, Left) - | Operator::infix(sub, Left) - | Operator::infix(modulo, Left)) - .op(Operator::infix(mul, Left) | Operator::infix(div, Left)) - .op(Operator::infix(dice, Left)) - .op(Operator::infix(pow, Right)) + .op(Op::infix(add, Left) | Op::infix(sub, Left) | Op::infix(modulo, Left)) + .op(Op::infix(mul, Left) | Op::infix(div, Left)) + .op(Op::infix(dice, Left)) + .op(Op::infix(pow, Right)) + .op(Op::postfix(EOI)) // discarded below }; } @@ -172,6 +168,7 @@ impl Calc { Ok(result) }) + .map_postfix(|arg, _post| arg) // discard EOI .parse(p) } @@ -202,7 +199,8 @@ mod test { } } -#[poise::command(slash_command, prefix_command, guild_only, aliases("calc", "calculate"))] +/// Roll some number of dice or perform a calculation. +#[poise::command(prefix_command, guild_only, aliases("calc", "calculate"))] pub async fn roll(ctx: PoiseContext<'_>, #[rest] rest: String) -> anyhow::Result<()> { match Calc::eval(&rest) { Ok(result) => { -- cgit v1.3.1