need choice for 6-tuple

This commit is contained in:
Greg Shuflin 2022-10-20 23:39:51 -07:00
parent b7db411671
commit bdaf712a25

View File

@ -144,6 +144,48 @@ where
}
}
impl<I, O, E, P1, P2, P3, P4, P5, P6> Choice<I, O, E> for (P1, P2, P3, P4, P5, P6)
where
P1: Parser<I, O, E>,
P2: Parser<I, O, E>,
P3: Parser<I, O, E>,
P4: Parser<I, O, E>,
P5: Parser<I, O, E>,
P6: Parser<I, O, E>,
I: Clone,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
let parser1 = &self.0;
let parser2 = &self.1;
let parser3 = &self.2;
let parser4 = &self.3;
let parser5 = &self.4;
let parser6 = &self.5;
//TODO need a more principled way to return an error when no choices work
let mut err = None;
for parser in [
parser1 as &dyn Parser<I, O, E>,
parser2,
parser3,
parser4,
parser5,
parser6,
]
.iter()
{
match parser.parse(input.clone()) {
Ok(result) => return Ok(result),
Err(e) => {
err = Some(e);
}
}
}
Err(err.unwrap())
}
}
#[cfg(test)]
mod tests {
use super::*;