Function combine::parser::combinator::factory [−][src]
pub fn factory<Input, P, R>(p: P) -> Factory<P, R> where
Input: Stream,
P: FnMut(&mut Input) -> R,
R: Parser<Input>,
Expand description
Constructs the parser lazily on each parse_*
call. This is similar to lazy
but it
takes Input
as an argument and 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")));