Function winnow::multi::separated0

source ·
pub fn separated0<I, O, C, O2, E, P, S>(
    parser: P,
    sep: S
) -> impl Parser<I, C, E>where
    I: Stream,
    C: Accumulate<O>,
    P: Parser<I, O, E>,
    S: Parser<I, O2, E>,
    E: ParseError<I>,
Expand description

Alternates between two parsers to produce a list of elements.

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

Arguments

  • parser Parses the elements of the list.
  • sep Parses the separator between list elements.

Example

use winnow::multi::separated0;
use winnow::bytes::tag;

fn parser(s: &str) -> IResult<&str, Vec<&str>> {
  separated0("abc", "|").parse_next(s)
}

assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"])));
assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"])));
assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"])));
assert_eq!(parser(""), Ok(("", vec![])));
assert_eq!(parser("def|abc"), Ok(("def|abc", vec![])));