From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- vendor/pest/tests/calculator.rs | 74 ++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) (limited to 'vendor/pest/tests/calculator.rs') diff --git a/vendor/pest/tests/calculator.rs b/vendor/pest/tests/calculator.rs index 54a50cc67..c5270550d 100644 --- a/vendor/pest/tests/calculator.rs +++ b/vendor/pest/tests/calculator.rs @@ -12,7 +12,7 @@ extern crate pest; use pest::error::Error; use pest::iterators::{Pair, Pairs}; -use pest::prec_climber::{Assoc, Operator, PrecClimber}; +use pest::pratt_parser::{Assoc, Op, PrattParser}; use pest::{state, ParseResult, Parser, ParserState}; #[allow(dead_code, non_camel_case_types)] @@ -33,7 +33,9 @@ struct CalculatorParser; impl Parser for CalculatorParser { fn parse(rule: Rule, input: &str) -> Result, Error> { - fn expression(state: Box>) -> ParseResult>> { + fn expression( + state: Box>, + ) -> ParseResult>> { state.rule(Rule::expression, |s| { s.sequence(|s| { primary(s).and_then(|s| { @@ -53,7 +55,7 @@ impl Parser for CalculatorParser { }) } - fn primary(state: Box>) -> ParseResult>> { + fn primary(state: Box>) -> ParseResult>> { state .sequence(|s| { s.match_string("(") @@ -63,7 +65,7 @@ impl Parser for CalculatorParser { .or_else(number) } - fn number(state: Box>) -> ParseResult>> { + fn number(state: Box>) -> ParseResult>> { state.rule(Rule::number, |s| { s.sequence(|s| { s.optional(|s| s.match_string("-")).and_then(|s| { @@ -78,27 +80,27 @@ impl Parser for CalculatorParser { }) } - fn plus(state: Box>) -> ParseResult>> { + fn plus(state: Box>) -> ParseResult>> { state.rule(Rule::plus, |s| s.match_string("+")) } - fn minus(state: Box>) -> ParseResult>> { + fn minus(state: Box>) -> ParseResult>> { state.rule(Rule::minus, |s| s.match_string("-")) } - fn times(state: Box>) -> ParseResult>> { + fn times(state: Box>) -> ParseResult>> { state.rule(Rule::times, |s| s.match_string("*")) } - fn divide(state: Box>) -> ParseResult>> { + fn divide(state: Box>) -> ParseResult>> { state.rule(Rule::divide, |s| s.match_string("/")) } - fn modulus(state: Box>) -> ParseResult>> { + fn modulus(state: Box>) -> ParseResult>> { state.rule(Rule::modulus, |s| s.match_string("%")) } - fn power(state: Box>) -> ParseResult>> { + fn power(state: Box>) -> ParseResult>> { state.rule(Rule::power, |s| s.match_string("^")) } @@ -109,8 +111,14 @@ impl Parser for CalculatorParser { } } -fn consume(pair: Pair, climber: &PrecClimber) -> i32 { - let primary = |pair| consume(pair, climber); +#[allow(deprecated)] +enum PrattOrPrecClimber<'a> { + Pratt(&'a PrattParser), + PrecClimber(&'a pest::prec_climber::PrecClimber), +} + +fn consume(pair: Pair, pratt_or_climber: &PrattOrPrecClimber) -> i32 { + let primary = |pair| consume(pair, pratt_or_climber); let infix = |lhs: i32, op: Pair, rhs: i32| match op.as_rule() { Rule::plus => lhs + rhs, Rule::minus => lhs - rhs, @@ -121,9 +129,16 @@ fn consume(pair: Pair, climber: &PrecClimber) -> i32 { _ => unreachable!(), }; - match pair.as_rule() { - Rule::expression => climber.climb(pair.into_inner(), primary, infix), - Rule::number => pair.as_str().parse().unwrap(), + #[allow(deprecated)] + match (pair.as_rule(), pratt_or_climber) { + (Rule::expression, PrattOrPrecClimber::Pratt(pratt)) => pratt + .map_primary(primary) + .map_infix(infix) + .parse(pair.into_inner()), + (Rule::expression, PrattOrPrecClimber::PrecClimber(climber)) => { + climber.climb(pair.into_inner(), primary, infix) + } + (Rule::number, _) => pair.as_str().parse().unwrap(), _ => unreachable!(), } } @@ -187,7 +202,9 @@ fn expression() { } #[test] +#[allow(deprecated)] fn prec_climb() { + use pest::prec_climber::{Assoc, Operator, PrecClimber}; let climber = PrecClimber::new(vec![ Operator::new(Rule::plus, Assoc::Left) | Operator::new(Rule::minus, Assoc::Left), Operator::new(Rule::times, Assoc::Left) @@ -197,5 +214,30 @@ fn prec_climb() { ]); let pairs = CalculatorParser::parse(Rule::expression, "-12+3*(4-9)^3^2/9%7381"); - assert_eq!(-1_525, consume(pairs.unwrap().next().unwrap(), &climber)); + assert_eq!( + -1_525, + consume( + pairs.unwrap().next().unwrap(), + &PrattOrPrecClimber::PrecClimber(&climber) + ) + ); +} + +#[test] +fn pratt_parse() { + let pratt = PrattParser::new() + .op(Op::infix(Rule::plus, Assoc::Left) | Op::infix(Rule::minus, Assoc::Left)) + .op(Op::infix(Rule::times, Assoc::Left) + | Op::infix(Rule::divide, Assoc::Left) + | Op::infix(Rule::modulus, Assoc::Left)) + .op(Op::infix(Rule::power, Assoc::Right)); + + let pairs = CalculatorParser::parse(Rule::expression, "-12+3*(4-9)^3^2/9%7381"); + assert_eq!( + -1_525, + consume( + pairs.unwrap().next().unwrap(), + &PrattOrPrecClimber::Pratt(&pratt) + ) + ); } -- cgit v1.2.3