pub fn separated_foldl1<Input, Output, Sep, Error, ParseNext, SepParser, Op>(
parser: ParseNext,
sep: SepParser,
op: Op,
) -> impl Parser<Input, Output, Error>where
Input: Stream,
ParseNext: Parser<Input, Output, Error>,
SepParser: Parser<Input, Sep, Error>,
Error: ParserError<Input>,
Op: FnMut(Output, Sep, Output) -> Output,
Expand description
Alternates between two parsers, merging the results (left associative)
This stops when either parser returns ErrMode::Backtrack
. To instead chain an error up, see
cut_err
.
ยงExample
use winnow::combinator::separated_foldl1;
use winnow::ascii::dec_int;
fn parser(s: &str) -> IResult<&str, i32> {
separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_peek(s)
}
assert_eq!(parser("9-3-5"), Ok(("", 1)));
assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token))));
assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Verify))));