diff options
Diffstat (limited to 'vendor/winnow/examples/arithmetic')
-rw-r--r-- | vendor/winnow/examples/arithmetic/bench.rs | 6 | ||||
-rw-r--r-- | vendor/winnow/examples/arithmetic/parser.rs | 43 | ||||
-rw-r--r-- | vendor/winnow/examples/arithmetic/parser_ast.rs | 64 |
3 files changed, 60 insertions, 53 deletions
diff --git a/vendor/winnow/examples/arithmetic/bench.rs b/vendor/winnow/examples/arithmetic/bench.rs index 0f6ec8eef..6504454c4 100644 --- a/vendor/winnow/examples/arithmetic/bench.rs +++ b/vendor/winnow/examples/arithmetic/bench.rs @@ -1,5 +1,7 @@ mod parser; +use winnow::prelude::*; + use parser::expr; #[allow(clippy::eq_op, clippy::erasing_op)] @@ -7,11 +9,11 @@ fn arithmetic(c: &mut criterion::Criterion) { let data = " 2*2 / ( 5 - 1) + 3 / 4 * (2 - 7 + 567 *12 /2) + 3*(1+2*( 45 /2));"; assert_eq!( - expr(data), + expr.parse_peek(data), Ok((";", 2 * 2 / (5 - 1) + 3 * (1 + 2 * (45 / 2)),)) ); c.bench_function("arithmetic", |b| { - b.iter(|| expr(data).unwrap()); + b.iter(|| expr.parse_peek(data).unwrap()); }); } diff --git a/vendor/winnow/examples/arithmetic/parser.rs b/vendor/winnow/examples/arithmetic/parser.rs index ba6347a43..50ffbdbbb 100644 --- a/vendor/winnow/examples/arithmetic/parser.rs +++ b/vendor/winnow/examples/arithmetic/parser.rs @@ -7,17 +7,16 @@ use winnow::{ combinator::delimited, combinator::fold_repeat, token::one_of, - IResult, }; // Parser definition -pub fn expr(i: &str) -> IResult<&str, i64> { - let (i, init) = term(i)?; +pub fn expr(i: &mut &str) -> PResult<i64> { + let init = term.parse_next(i)?; fold_repeat( 0.., - (one_of("+-"), term), + (one_of(['+', '-']), term), move || init, |acc, (op, val): (char, i64)| { if op == '+' { @@ -33,12 +32,12 @@ pub fn expr(i: &str) -> IResult<&str, i64> { // We read an initial factor and for each time we find // a * or / operator followed by another factor, we do // the math by folding everything -fn term(i: &str) -> IResult<&str, i64> { - let (i, init) = factor(i)?; +fn term(i: &mut &str) -> PResult<i64> { + let init = factor.parse_next(i)?; fold_repeat( 0.., - (one_of("*/"), factor), + (one_of(['*', '/']), factor), move || init, |acc, (op, val): (char, i64)| { if op == '*' { @@ -55,7 +54,7 @@ fn term(i: &str) -> IResult<&str, i64> { // We look for a digit suite, and try to convert it. // If either str::from_utf8 or FromStr::from_str fail, // we fallback to the parens parser defined above -fn factor(i: &str) -> IResult<&str, i64> { +fn factor(i: &mut &str) -> PResult<i64> { delimited( spaces, alt(( @@ -69,35 +68,35 @@ fn factor(i: &str) -> IResult<&str, i64> { } // We parse any expr surrounded by parens, ignoring all whitespaces around those -fn parens(i: &str) -> IResult<&str, i64> { +fn parens(i: &mut &str) -> PResult<i64> { delimited('(', expr, ')').parse_next(i) } #[test] fn factor_test() { - assert_eq!(factor("3"), Ok(("", 3))); - assert_eq!(factor(" 12"), Ok(("", 12))); - assert_eq!(factor("537 "), Ok(("", 537))); - assert_eq!(factor(" 24 "), Ok(("", 24))); + assert_eq!(factor.parse_peek("3"), Ok(("", 3))); + assert_eq!(factor.parse_peek(" 12"), Ok(("", 12))); + assert_eq!(factor.parse_peek("537 "), Ok(("", 537))); + assert_eq!(factor.parse_peek(" 24 "), Ok(("", 24))); } #[test] fn term_test() { - assert_eq!(term(" 12 *2 / 3"), Ok(("", 8))); - assert_eq!(term(" 2* 3 *2 *2 / 3"), Ok(("", 8))); - assert_eq!(term(" 48 / 3/2"), Ok(("", 8))); + assert_eq!(term.parse_peek(" 12 *2 / 3"), Ok(("", 8))); + assert_eq!(term.parse_peek(" 2* 3 *2 *2 / 3"), Ok(("", 8))); + assert_eq!(term.parse_peek(" 48 / 3/2"), Ok(("", 8))); } #[test] fn expr_test() { - assert_eq!(expr(" 1 + 2 "), Ok(("", 3))); - assert_eq!(expr(" 12 + 6 - 4+ 3"), Ok(("", 17))); - assert_eq!(expr(" 1 + 2*3 + 4"), Ok(("", 11))); + assert_eq!(expr.parse_peek(" 1 + 2 "), Ok(("", 3))); + assert_eq!(expr.parse_peek(" 12 + 6 - 4+ 3"), Ok(("", 17))); + assert_eq!(expr.parse_peek(" 1 + 2*3 + 4"), Ok(("", 11))); } #[test] fn parens_test() { - assert_eq!(expr(" ( 2 )"), Ok(("", 2))); - assert_eq!(expr(" 2* ( 3 + 4 ) "), Ok(("", 14))); - assert_eq!(expr(" 2*2 / ( 5 - 1) + 3"), Ok(("", 4))); + assert_eq!(expr.parse_peek(" ( 2 )"), Ok(("", 2))); + assert_eq!(expr.parse_peek(" 2* ( 3 + 4 ) "), Ok(("", 14))); + assert_eq!(expr.parse_peek(" 2*2 / ( 5 - 1) + 3"), Ok(("", 4))); } diff --git a/vendor/winnow/examples/arithmetic/parser_ast.rs b/vendor/winnow/examples/arithmetic/parser_ast.rs index fcf897e34..5fb9847c0 100644 --- a/vendor/winnow/examples/arithmetic/parser_ast.rs +++ b/vendor/winnow/examples/arithmetic/parser_ast.rs @@ -9,7 +9,6 @@ use winnow::{ combinator::alt, combinator::repeat, combinator::{delimited, preceded}, - IResult, }; #[derive(Debug)] @@ -44,47 +43,47 @@ impl Display for Expr { } } -pub fn expr(i: &str) -> IResult<&str, Expr> { - let (i, initial) = term(i)?; - let (i, remainder) = repeat( +pub fn expr(i: &mut &str) -> PResult<Expr> { + let initial = term(i)?; + let remainder = repeat( 0.., alt(( - |i| { - let (i, add) = preceded("+", term).parse_next(i)?; - Ok((i, (Oper::Add, add))) + |i: &mut &str| { + let add = preceded("+", term).parse_next(i)?; + Ok((Oper::Add, add)) }, - |i| { - let (i, sub) = preceded("-", term).parse_next(i)?; - Ok((i, (Oper::Sub, sub))) + |i: &mut &str| { + let sub = preceded("-", term).parse_next(i)?; + Ok((Oper::Sub, sub)) }, )), ) .parse_next(i)?; - Ok((i, fold_exprs(initial, remainder))) + Ok(fold_exprs(initial, remainder)) } -fn term(i: &str) -> IResult<&str, Expr> { - let (i, initial) = factor(i)?; - let (i, remainder) = repeat( +fn term(i: &mut &str) -> PResult<Expr> { + let initial = factor(i)?; + let remainder = repeat( 0.., alt(( - |i| { - let (i, mul) = preceded("*", factor).parse_next(i)?; - Ok((i, (Oper::Mul, mul))) + |i: &mut &str| { + let mul = preceded("*", factor).parse_next(i)?; + Ok((Oper::Mul, mul)) }, - |i| { - let (i, div) = preceded("/", factor).parse_next(i)?; - Ok((i, (Oper::Div, div))) + |i: &mut &str| { + let div = preceded("/", factor).parse_next(i)?; + Ok((Oper::Div, div)) }, )), ) .parse_next(i)?; - Ok((i, fold_exprs(initial, remainder))) + Ok(fold_exprs(initial, remainder)) } -fn factor(i: &str) -> IResult<&str, Expr> { +fn factor(i: &mut &str) -> PResult<Expr> { alt(( delimited(multispace, digit, multispace) .try_map(FromStr::from_str) @@ -94,7 +93,7 @@ fn factor(i: &str) -> IResult<&str, Expr> { .parse_next(i) } -fn parens(i: &str) -> IResult<&str, Expr> { +fn parens(i: &mut &str) -> PResult<Expr> { delimited( multispace, delimited("(", expr.map(|e| Expr::Paren(Box::new(e))), ")"), @@ -118,7 +117,9 @@ fn fold_exprs(initial: Expr, remainder: Vec<(Oper, Expr)>) -> Expr { #[test] fn factor_test() { assert_eq!( - factor(" 3 ").map(|(i, x)| (i, format!("{:?}", x))), + factor + .parse_peek(" 3 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(("", String::from("Value(3)"))) ); } @@ -126,7 +127,8 @@ fn factor_test() { #[test] fn term_test() { assert_eq!( - term(" 3 * 5 ").map(|(i, x)| (i, format!("{:?}", x))), + term.parse_peek(" 3 * 5 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(("", String::from("Mul(Value(3), Value(5))"))) ); } @@ -134,18 +136,21 @@ fn term_test() { #[test] fn expr_test() { assert_eq!( - expr(" 1 + 2 * 3 ").map(|(i, x)| (i, format!("{:?}", x))), + expr.parse_peek(" 1 + 2 * 3 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(("", String::from("Add(Value(1), Mul(Value(2), Value(3)))"))) ); assert_eq!( - expr(" 1 + 2 * 3 / 4 - 5 ").map(|(i, x)| (i, format!("{:?}", x))), + expr.parse_peek(" 1 + 2 * 3 / 4 - 5 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(( "", String::from("Sub(Add(Value(1), Div(Mul(Value(2), Value(3)), Value(4))), Value(5))") )) ); assert_eq!( - expr(" 72 / 2 / 3 ").map(|(i, x)| (i, format!("{:?}", x))), + expr.parse_peek(" 72 / 2 / 3 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(("", String::from("Div(Div(Value(72), Value(2)), Value(3))"))) ); } @@ -153,7 +158,8 @@ fn expr_test() { #[test] fn parens_test() { assert_eq!( - expr(" ( 1 + 2 ) * 3 ").map(|(i, x)| (i, format!("{:?}", x))), + expr.parse_peek(" ( 1 + 2 ) * 3 ") + .map(|(i, x)| (i, format!("{:?}", x))), Ok(( "", String::from("Mul(Paren(Add(Value(1), Value(2))), Value(3))") |