Function winnow::multi::separated_foldr1

source ·
pub fn separated_foldr1<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: ParseError<I>,
    Op: Fn(O, O2, O) -> O,
Expand description

Alternates between two parsers, merging the results (right associative)

This stops when either parser returns ErrMode::Backtrack. To instead chain an error up, see cut_err.

Example

use winnow::multi::separated_foldr1;
use winnow::character::dec_uint;

fn parser(s: &str) -> IResult<&str, u32> {
  separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_next(s)
}

assert_eq!(parser("2^3^2"), Ok(("", 512)));
assert_eq!(parser("2"), Ok(("", 2)));
assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice))));
assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Slice))));