combine::parser::combinator

Function factory

Source
pub fn factory<P, R>(p: P) -> Factory<P, R>
where P: FnMut() -> R, R: Parser,
Expand description

Constructs the parser lazily on each parse_* call. This is similar to lazy but it allows different parsers to be returned on each call to p while still reporting the correct errors.


let mut parsers: Vec<FnOpaque<_, _>> = vec![opaque(|f| f(&mut digit())), opaque(|f| f(&mut letter()))];
let mut iter = parsers.into_iter().cycle();
let mut parser = many(factory(move || iter.next().unwrap()));
assert_eq!(parser.parse("1a2b3cd"), Ok(("1a2b3c".to_string(), "d")));