Function winnow::multi::many_till0
source · pub fn many_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: ParseError<I>,
Expand description
Applies the parser f
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::multi::many_till0;
use winnow::bytes::tag;
fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
many_till0("abc", "end").parse_next(s)
};
assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(Error::new("123end", ErrorKind::Tag))));
assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(Error::new("123123end", ErrorKind::Tag))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag))));
assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));