pub fn separated_list0<I, O, O2, E, F, G>(sep: G, f: F) -> impl FnMut(I)
where I: Clone + InputLength, F: Parser<I, O, E>, G: Parser<I, O2, E>, E: ParseError<I>,
Available on crate feature mtls only.
Expand description

Alternates between two parsers to produce a list of elements.

This stops when either parser returns Err::Error and returns the results that were accumulated. To instead chain an error up, see cut.

§Arguments

  • sep Parses the separator between list elements.
  • f Parses the elements of the list.
use nom::multi::separated_list0;
use nom::bytes::complete::tag;

fn parser(s: &str) -> IResult<&str, Vec<&str>> {
  separated_list0(tag("|"), tag("abc"))(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![])));