Function winnow::combinator::repeat_till0

source ·
pub fn repeat_till0<I, O, C, P, E, F, G>(
    f: F,
    g: G,
) -> impl Parser<I, (C, P), E>
where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, G: Parser<I, P, E>, E: ParserError<I>,
Expand description

Accumulate the output of parser f into a container, like Vec, until the parser g produces a result.

Returns a tuple of the results of f in a Vec and the result of g.

f keeps going so long as g produces ErrMode::Backtrack. To instead chain an error up, see cut_err.

To recognize a series of tokens, Accumulate into a () and then Parser::recognize.

§Example

use winnow::combinator::repeat_till0;
use winnow::token::tag;

fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
  repeat_till0("abc", "end").parse_peek(s)
};

assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(InputError::new("123end", ErrorKind::Tag))));
assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(InputError::new("123123end", ErrorKind::Tag))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));
assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));