pub fn repeat_till<Input, Output, Accumulator, Terminator, Error, ParseNext, TerminatorParser>(
occurrences: impl Into<Range>,
parse: ParseNext,
terminator: TerminatorParser,
) -> impl Parser<Input, (Accumulator, Terminator), Error>where
Input: Stream,
Accumulator: Accumulate<Output>,
ParseNext: Parser<Input, Output, Error>,
TerminatorParser: Parser<Input, Terminator, Error>,
Error: ParserError<Input>,
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 take a series of tokens, Accumulate
into a ()
(e.g. with .map(|((), _)| ())
)
and then Parser::take
.
See also
take_till
for recognizing up-to a member of a set of tokenstake_until
for recognizing up-to aliteral
(w/ optional simd optimizations)
§Example
use winnow::combinator::repeat_till;
fn parser<'i>(s: &mut &'i str) -> ModalResult<(Vec<&'i str>, &'i str)> {
repeat_till(0.., "abc", "end").parse_next(s)
};
assert_eq!(parser.parse_peek("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
assert!(parser.parse_peek("abc123end").is_err());
assert!(parser.parse_peek("123123end").is_err());
assert!(parser.parse_peek("").is_err());
assert_eq!(parser.parse_peek("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));