choice function

This commit is contained in:
Greg Shuflin 2022-10-16 20:07:45 -07:00
parent eaafdaa777
commit 4456734134
2 changed files with 41 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use crate::parser::Parser; use crate::parser::{ParseResult, Parser};
pub fn choice<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E> pub fn choice2<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E>
where where
P1: Parser<I, O, E>, P1: Parser<I, O, E>,
P2: Parser<I, O, E>, P2: Parser<I, O, E>,
@ -12,6 +12,43 @@ where
} }
} }
pub fn choice<C, I, O, E>(choices: C) -> impl Parser<I, O, E>
where
C: Choice<I, O, E>,
I: Clone,
{
move |input| choices.parse(input)
}
pub trait Choice<I: Clone, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>;
}
impl<I, O, E, P1, P2> Choice<I, O, E> for (P1, P2)
where
P1: Parser<I, O, E>,
P2: Parser<I, O, E>,
I: Clone,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
let parser1 = &self.0;
let parser2 = &self.1;
//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].iter() {
match parser.parse(input.clone()) {
Ok(result) => return Ok(result),
Err(e) => {
err = Some(e);
}
}
}
Err(err.unwrap())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -20,7 +57,7 @@ mod tests {
#[test] #[test]
fn test_choice() { fn test_choice() {
let p = choice(literal("gnostika").to(1), one_or_more(literal(" ")).to(2)); let p = choice2(literal("gnostika").to(1), one_or_more(literal(" ")).to(2));
assert_eq!(p.parse("gnostika twentynine"), Ok((1, " twentynine"))); assert_eq!(p.parse("gnostika twentynine"), Ok((1, " twentynine")));
} }
} }

View File

@ -45,7 +45,7 @@ mod tests {
let json_true = literal("true").to(JsonValue::Bool(true)); let json_true = literal("true").to(JsonValue::Bool(true));
let json_false = literal("false").to(JsonValue::Bool(false)); let json_false = literal("false").to(JsonValue::Bool(false));
let json_value = choice(json_null, choice(json_true, json_false)); let json_value = choice((json_null, choice((json_true, json_false))));
assert_eq!(json_value.parse("true"), Ok((JsonValue::Bool(true), ""))); assert_eq!(json_value.parse("true"), Ok((JsonValue::Bool(true), "")));
} }