From 56b6e54e88c9ff22115cc040cf7393dc89a5dca9 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 5 May 2024 18:14:44 +0200 Subject: [PATCH] implement alt for dynamic arrays (#1754) --- src/branch/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/branch/tests.rs | 16 ++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/branch/mod.rs b/src/branch/mod.rs index 80c3d1f84..20205f595 100644 --- a/src/branch/mod.rs +++ b/src/branch/mod.rs @@ -212,6 +212,44 @@ impl< } } +impl< + Input: Clone, + Output, + Error: ParseError, + A: Parser, + > Parser for Choice<&mut [A]> +{ + type Output = Output; + type Error = Error; + + #[inline] + fn process( + &mut self, + input: Input, + ) -> crate::PResult { + let mut error = None; + + for branch in self.parser.iter_mut() { + match branch.process::(input.clone()) { + Err(Err::Error(e)) => match error { + None => error = Some(e), + Some(err) => error = Some(OM::Error::combine(err, e, |e1, e2| e1.or(e2))), + }, + res => return res, + } + } + + match error { + Some(e) => Err(Err::Error(OM::Error::map(e, |err| { + Error::append(input, ErrorKind::Alt, err) + }))), + None => Err(Err::Error(OM::Error::bind(|| { + Error::from_error_kind(input, ErrorKind::Alt) + }))), + } + } +} + macro_rules! permutation_trait( ( $name1:ident $ty1:ident $item1:ident diff --git a/src/branch/tests.rs b/src/branch/tests.rs index b409d0b3f..0e374a590 100644 --- a/src/branch/tests.rs +++ b/src/branch/tests.rs @@ -131,6 +131,22 @@ fn alt_array() { assert_eq!(alt1(defg), Ok((&b"g"[..], (&b"def"[..])))); } +#[test] +fn alt_dynamic_array() { + fn alt1(i: &[u8]) -> IResult<&[u8], &[u8]> { + alt(&mut [tag("a"), tag("bc"), tag("def")][..]).parse(i) + } + + let a = &b"a"[..]; + assert_eq!(alt1(a), Ok((&b""[..], (&b"a"[..])))); + + let bc = &b"bc"[..]; + assert_eq!(alt1(bc), Ok((&b""[..], (&b"bc"[..])))); + + let defg = &b"defg"[..]; + assert_eq!(alt1(defg), Ok((&b"g"[..], (&b"def"[..])))); +} + #[test] fn permutation_test() { #[allow(clippy::type_complexity)]