Function winnow::combinator::separated_foldl1

source ·
pub fn separated_foldl1<I, O, O2, E, P, S, Op>(
    parser: P,
    sep: S,
    op: Op,
) -> impl Parser<I, O, E>
where I: Stream, P: Parser<I, O, E>, S: Parser<I, O2, E>, E: ParserError<I>, Op: Fn(O, O2, O) -> O,
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::Slice))));
assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Slice))));