Function nom::multi::many_till

source ยท
pub fn many_till<I, O, P, E, F, G>(
    f: F,
    g: G,
) -> impl FnMut(I) -> IResult<I, (Vec<O>, P), E>
where I: Clone + InputLength, F: Parser<I, O, E>, G: Parser<I, P, E>, E: ParseError<I>,
Expand description

Applies the parser f until the parser g produces a result.

Returns a tuple of the results of f in a Vec and the result of g.

f keeps going so long as g produces Err::Error. To instead chain an error up, see cut.

use nom::multi::many_till;
use nom::bytes::complete::tag;

fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
  many_till(tag("abc"), tag("end"))(s)
};

assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
assert_eq!(parser("abc123end"), Err(Err::Error(Error::new("123end", ErrorKind::Tag))));
assert_eq!(parser("123123end"), Err(Err::Error(Error::new("123123end", ErrorKind::Tag))));
assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));