From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- vendor/pest/tests/calculator.rs | 74 ++++++++++++++++++++++++++++++++--------- vendor/pest/tests/json.rs | 30 ++++++++--------- 2 files changed, 73 insertions(+), 31 deletions(-) (limited to 'vendor/pest/tests') 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) + ) + ); } diff --git a/vendor/pest/tests/json.rs b/vendor/pest/tests/json.rs index b9338c342..b66f39df7 100644 --- a/vendor/pest/tests/json.rs +++ b/vendor/pest/tests/json.rs @@ -39,11 +39,11 @@ struct JsonParser; impl Parser for JsonParser { fn parse(rule: Rule, input: &str) -> Result, Error> { - fn json(state: Box>) -> ParseResult>> { + fn json(state: Box>) -> ParseResult>> { value(state) } - fn object(state: Box>) -> ParseResult>> { + fn object(state: Box>) -> ParseResult>> { state.rule(Rule::object, |s| { s.sequence(|s| { s.match_string("{") @@ -72,7 +72,7 @@ impl Parser for JsonParser { }) } - fn pair(state: Box>) -> ParseResult>> { + fn pair(state: Box>) -> ParseResult>> { state.rule(Rule::pair, |s| { s.sequence(|s| { string(s) @@ -84,7 +84,7 @@ impl Parser for JsonParser { }) } - fn array(state: Box>) -> ParseResult>> { + fn array(state: Box>) -> ParseResult>> { state.rule(Rule::array, |s| { s.sequence(|s| { s.match_string("[") @@ -113,7 +113,7 @@ impl Parser for JsonParser { }) } - fn value(state: Box>) -> ParseResult>> { + fn value(state: Box>) -> ParseResult>> { state.rule(Rule::value, |s| { string(s) .or_else(number) @@ -124,7 +124,7 @@ impl Parser for JsonParser { }) } - fn string(state: Box>) -> ParseResult>> { + fn string(state: Box>) -> ParseResult>> { state.rule(Rule::string, |s| { s.match_string("\"") .and_then(|s| { @@ -143,7 +143,7 @@ impl Parser for JsonParser { }) } - fn escape(state: Box>) -> ParseResult>> { + fn escape(state: Box>) -> ParseResult>> { state.sequence(|s| { s.match_string("\\").and_then(|s| { s.match_string("\"") @@ -159,7 +159,7 @@ impl Parser for JsonParser { }) } - fn unicode(state: Box>) -> ParseResult>> { + fn unicode(state: Box>) -> ParseResult>> { state.sequence(|s| { s.match_string("u") .and_then(hex) @@ -168,14 +168,14 @@ impl Parser for JsonParser { }) } - fn hex(state: Box>) -> ParseResult>> { + fn hex(state: Box>) -> ParseResult>> { state .match_range('0'..'9') .or_else(|s| s.match_range('a'..'f')) .or_else(|s| s.match_range('A'..'F')) } - fn number(state: Box>) -> ParseResult>> { + fn number(state: Box>) -> ParseResult>> { state.rule(Rule::number, |s| { s.sequence(|s| { s.optional(|s| s.match_string("-")) @@ -195,7 +195,7 @@ impl Parser for JsonParser { }) } - fn int(state: Box>) -> ParseResult>> { + fn int(state: Box>) -> ParseResult>> { state.match_string("0").or_else(|s| { s.sequence(|s| { s.match_range('1'..'9') @@ -204,7 +204,7 @@ impl Parser for JsonParser { }) } - fn exp(state: Box>) -> ParseResult>> { + fn exp(state: Box>) -> ParseResult>> { state.sequence(|s| { s.match_string("E") .or_else(|s| s.match_string("e")) @@ -215,17 +215,17 @@ impl Parser for JsonParser { }) } - fn bool(state: Box>) -> ParseResult>> { + fn bool(state: Box>) -> ParseResult>> { state.rule(Rule::bool, |s| { s.match_string("true").or_else(|s| s.match_string("false")) }) } - fn null(state: Box>) -> ParseResult>> { + fn null(state: Box>) -> ParseResult>> { state.rule(Rule::null, |s| s.match_string("null")) } - fn skip(state: Box>) -> ParseResult>> { + fn skip(state: Box>) -> ParseResult>> { state.repeat(|s| { s.match_string(" ") .or_else(|s| s.match_string("\t")) -- cgit v1.2.3