Function winnow::combinator::repeat_till

source ·
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

§Example

use winnow::combinator::repeat_till;

fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
  repeat_till(0.., "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"))));