Function winnow::multi::separated1
source · pub fn separated1<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 until ErrMode::Backtrack
.
Fails if the element parser does not produce at least one element.$
This stops when either parser returns ErrMode::Backtrack
. To instead chain an error up, see
cut_err
.
Arguments
sep
Parses the separator between list elements.f
Parses the elements of the list.
Example
use winnow::multi::separated1;
use winnow::bytes::tag;
fn parser(s: &str) -> IResult<&str, Vec<&str>> {
separated1("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(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag))));
assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Tag))));