22 lines
540 B
Rust
22 lines
540 B
Rust
|
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())
|
||
|
}
|
||
|
}
|