Skip to content

Commit

Permalink
fix(comb): Deprecate fold_repeat in favor of repeat().fold()
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 31, 2024
1 parent 6fbc341 commit 2b6402a
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 182 deletions.
4 changes: 2 additions & 2 deletions assets/trace.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 25 additions & 27 deletions examples/arithmetic/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winnow::{
ascii::{digit1 as digits, multispace0 as multispaces},
combinator::alt,
combinator::delimited,
combinator::fold_repeat,
combinator::repeat,
token::one_of,
};

Expand All @@ -14,19 +14,18 @@ use winnow::{
pub fn expr(i: &mut &str) -> PResult<i64> {
let init = term.parse_next(i)?;

fold_repeat(
0..,
(one_of(['+', '-']), term),
move || init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc + val
} else {
acc - val
}
},
)
.parse_next(i)
repeat(0.., (one_of(['+', '-']), term))
.fold(
move || init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc + val
} else {
acc - val
}
},
)
.parse_next(i)
}

// We read an initial factor and for each time we find
Expand All @@ -35,19 +34,18 @@ pub fn expr(i: &mut &str) -> PResult<i64> {
fn term(i: &mut &str) -> PResult<i64> {
let init = factor.parse_next(i)?;

fold_repeat(
0..,
(one_of(['*', '/']), factor),
move || init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc * val
} else {
acc / val
}
},
)
.parse_next(i)
repeat(0.., (one_of(['*', '/']), factor))
.fold(
move || init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc * val
} else {
acc / val
}
},
)
.parse_next(i)
}

// We transform an integer string into a i64, ignoring surrounding whitespace
Expand Down
52 changes: 25 additions & 27 deletions examples/arithmetic/parser_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winnow::{
ascii::{digit1 as digits, multispace0 as multispaces},
combinator::alt,
combinator::delimited,
combinator::fold_repeat,
combinator::repeat,
token::one_of,
};

Expand Down Expand Up @@ -52,37 +52,35 @@ impl Display for Expr {
pub fn expr(i: &mut &str) -> PResult<Expr> {
let init = term.parse_next(i)?;

fold_repeat(
0..,
(one_of(['+', '-']), term),
move || init.clone(),
|acc, (op, val): (char, Expr)| {
if op == '+' {
Expr::Add(Box::new(acc), Box::new(val))
} else {
Expr::Sub(Box::new(acc), Box::new(val))
}
},
)
.parse_next(i)
repeat(0.., (one_of(['+', '-']), term))
.fold(
move || init.clone(),
|acc, (op, val): (char, Expr)| {
if op == '+' {
Expr::Add(Box::new(acc), Box::new(val))
} else {
Expr::Sub(Box::new(acc), Box::new(val))
}
},
)
.parse_next(i)
}

fn term(i: &mut &str) -> PResult<Expr> {
let init = factor.parse_next(i)?;

fold_repeat(
0..,
(one_of(['*', '/']), factor),
move || init.clone(),
|acc, (op, val): (char, Expr)| {
if op == '*' {
Expr::Mul(Box::new(acc), Box::new(val))
} else {
Expr::Div(Box::new(acc), Box::new(val))
}
},
)
.parse_next(i)
repeat(0.., (one_of(['*', '/']), factor))
.fold(
move || init.clone(),
|acc, (op, val): (char, Expr)| {
if op == '*' {
Expr::Mul(Box::new(acc), Box::new(val))
} else {
Expr::Div(Box::new(acc), Box::new(val))
}
},
)
.parse_next(i)
}

fn factor(i: &mut &str) -> PResult<Expr> {
Expand Down
9 changes: 6 additions & 3 deletions examples/arithmetic/parser_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use winnow::{
combinator::alt,
combinator::dispatch,
combinator::fail,
combinator::fold_repeat,
combinator::peek,
combinator::repeat,
combinator::{delimited, preceded, terminated},
Expand Down Expand Up @@ -125,12 +124,14 @@ fn token(i: &mut &str) -> PResult<Token> {
pub fn expr(i: &mut &[Token]) -> PResult<Expr> {
let init = term.parse_next(i)?;

fold_repeat(
repeat(
0..,
(
one_of([Token::Oper(Oper::Add), Token::Oper(Oper::Sub)]),
term,
),
)
.fold(
move || init.clone(),
|acc, (op, val): (Token, Expr)| {
if op == Token::Oper(Oper::Add) {
Expand All @@ -146,12 +147,14 @@ pub fn expr(i: &mut &[Token]) -> PResult<Expr> {
fn term(i: &mut &[Token]) -> PResult<Expr> {
let init = factor.parse_next(i)?;

fold_repeat(
repeat(
0..,
(
one_of([Token::Oper(Oper::Mul), Token::Oper(Oper::Div)]),
factor,
),
)
.fold(
move || init.clone(),
|acc, (op, val): (Token, Expr)| {
if op == Token::Oper(Oper::Mul) {
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winnow::{
combinator::alt,
combinator::cut_err,
combinator::{delimited, preceded, separated_pair, terminated},
combinator::{fold_repeat, separated},
combinator::{repeat, separated},
error::{AddContext, ParserError},
token::{any, none_of, take, take_while},
};
Expand Down Expand Up @@ -87,7 +87,7 @@ fn string<'i, E: ParserError<Stream<'i>> + AddContext<Stream<'i>, &'static str>>
// right branch (since we found the `"` character) but encountered an error when
// parsing the string
cut_err(terminated(
fold_repeat(0.., character, String::new, |mut string, c| {
repeat(0.., character).fold(String::new, |mut string, c| {
string.push(c);
string
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winnow::{
combinator::peek,
combinator::{alt, dispatch},
combinator::{delimited, preceded, separated_pair, terminated},
combinator::{fold_repeat, separated},
combinator::{repeat, separated},
error::{AddContext, ParserError},
token::{any, none_of, take, take_while},
};
Expand Down Expand Up @@ -96,7 +96,7 @@ fn string<'i, E: ParserError<Stream<'i>> + AddContext<Stream<'i>, &'static str>>
// right branch (since we found the `"` character) but encountered an error when
// parsing the string
cut_err(terminated(
fold_repeat(0.., character, String::new, |mut string, c| {
repeat(0.., character).fold(String::new, |mut string, c| {
string.push(c);
string
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winnow::{
combinator::alt,
combinator::{cut_err, rest},
combinator::{delimited, preceded, separated_pair, terminated},
combinator::{fold_repeat, separated},
combinator::{repeat, separated},
error::{AddContext, ParserError},
stream::Partial,
token::{any, none_of, take, take_while},
Expand Down Expand Up @@ -88,7 +88,7 @@ fn string<'i, E: ParserError<Stream<'i>> + AddContext<Stream<'i>, &'static str>>
// right branch (since we found the `"` character) but encountered an error when
// parsing the string
cut_err(terminated(
fold_repeat(0.., character, String::new, |mut string, c| {
repeat(0.., character).fold(String::new, |mut string, c| {
string.push(c);
string
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/ndjson/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winnow::{
combinator::alt,
combinator::cut_err,
combinator::{delimited, preceded, separated_pair, terminated},
combinator::{fold_repeat, separated},
combinator::{repeat, separated},
error::{AddContext, ParserError},
stream::Partial,
token::{any, none_of, take, take_while},
Expand Down Expand Up @@ -92,7 +92,7 @@ fn string<'i, E: ParserError<Stream<'i>> + AddContext<Stream<'i>, &'static str>>
// right branch (since we found the `"` character) but encountered an error when
// parsing the string
cut_err(terminated(
fold_repeat(0.., character, String::new, |mut string, c| {
repeat(0.., character).fold(String::new, |mut string, c| {
string.push(c);
string
}),
Expand Down
10 changes: 6 additions & 4 deletions examples/string/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use winnow::ascii::multispace1;
use winnow::combinator::alt;
use winnow::combinator::fold_repeat;
use winnow::combinator::repeat;
use winnow::combinator::{delimited, preceded};
use winnow::error::{FromExternalError, ParserError};
use winnow::prelude::*;
Expand All @@ -23,12 +23,14 @@ pub fn parse_string<'a, E>(input: &mut &'a str) -> PResult<String, E>
where
E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>,
{
// fold_repeat is the equivalent of iterator::fold. It runs a parser in a loop,
// Repeat::fold is the equivalent of iterator::fold. It runs a parser in a loop,
// and for each output value, calls a folding function on each output value.
let build_string = fold_repeat(
let build_string = repeat(
0..,
// Our parser function – parses a single string fragment
parse_fragment,
)
.fold(
// Our init value, an empty string
String::new,
// Our folding function. For each fragment, append the fragment to the
Expand All @@ -45,7 +47,7 @@ where

// Finally, parse the string. Note that, if `build_string` could accept a raw
// " character, the closing delimiter " would never match. When using
// `delimited` with a looping parser (like fold_repeat), be sure that the
// `delimited` with a looping parser (like Repeat::fold), be sure that the
// loop won't accidentally match your closing delimiter!
delimited('"', build_string, '"').parse_next(input)
}
Expand Down
Loading

0 comments on commit 2b6402a

Please sign in to comment.