pub fn separated_list0<I, E, F, G>(
sep: G,
f: F,
) -> impl Parser<I, Output = Vec<<F as Parser<I>>::Output>, Error = E>
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")).parse(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![])));