rust-parser-combinator/src/choice.rs

22 lines
540 B
Rust
Raw Normal View History

2024-01-26 00:07:52 -08:00
use crate::Parser;
pub fn choice<'a, I, O, E>(parsers: &'a [&'a dyn Parser<I, O, E>]) -> impl Parser<I, O, E> + 'a
where
I: Clone,
{
move |input: I| {
//TODO need a more principled way to return an error when no choices work
let mut err = None;
for parser in parsers.iter() {
match parser.parse(input.clone()) {
Ok(res) => return Ok(res),
Err(e) => {
err = Some(e);
}
}
}
Err(err.unwrap())
}
}