Function winnow::combinator::separated1
source · pub fn separated1<I, O, C, O2, E, P, S>(
parser: P,
sep: S,
) -> impl Parser<I, C, E>
Expand description
Accumulate
the output of a parser, interleaed with sep
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::combinator::separated1;
use winnow::token::tag;
fn parser(s: &str) -> IResult<&str, Vec<&str>> {
separated1("abc", "|").parse_peek(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(InputError::new("", ErrorKind::Tag))));
assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Tag))));