diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/winnow/src/branch | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/winnow/src/branch')
-rw-r--r-- | vendor/winnow/src/branch/mod.rs | 275 | ||||
-rw-r--r-- | vendor/winnow/src/branch/tests.rs | 180 |
2 files changed, 0 insertions, 455 deletions
diff --git a/vendor/winnow/src/branch/mod.rs b/vendor/winnow/src/branch/mod.rs deleted file mode 100644 index 6a2c9bc3a..000000000 --- a/vendor/winnow/src/branch/mod.rs +++ /dev/null @@ -1,275 +0,0 @@ -//! Choice combinators - -#[cfg(test)] -mod tests; - -use crate::error::ErrMode; -use crate::error::ErrorKind; -use crate::error::ParseError; -use crate::stream::Stream; -use crate::trace::trace; -use crate::{IResult, Parser}; - -#[doc(inline)] -pub use crate::dispatch; - -/// Helper trait for the [alt()] combinator. -/// -/// This trait is implemented for tuples of up to 21 elements -pub trait Alt<I, O, E> { - /// Tests each parser in the tuple and returns the result of the first one that succeeds - fn choice(&mut self, input: I) -> IResult<I, O, E>; -} - -/// Pick the first successful parser -/// -/// For tight control over the error, add a final case using [`fail`][crate::combinator::fail]. -/// Alternatively, with a [custom error type][crate::_topic::error], it is possible to track all -/// errors or return the error of the parser that went the farthest in the input data. -/// -/// When the alternative cases have unique prefixes, [`dispatch`] can offer better performance. -/// -/// # Example -/// -/// ```rust -/// # use winnow::{error::ErrMode, error::Error,error::ErrorKind, error::Needed}; -/// # use winnow::prelude::*; -/// use winnow::character::{alpha1, digit1}; -/// use winnow::branch::alt; -/// # fn main() { -/// fn parser(input: &str) -> IResult<&str, &str> { -/// alt((alpha1, digit1)).parse_next(input) -/// }; -/// -/// // the first parser, alpha1, recognizes the input -/// assert_eq!(parser("abc"), Ok(("", "abc"))); -/// -/// // the first parser returns an error, so alt tries the second one -/// assert_eq!(parser("123456"), Ok(("", "123456"))); -/// -/// // both parsers failed, and with the default error type, alt will return the last error -/// assert_eq!(parser(" "), Err(ErrMode::Backtrack(Error::new(" ", ErrorKind::Slice)))); -/// # } -/// ``` -#[doc(alias = "choice")] -pub fn alt<I: Stream, O, E: ParseError<I>, List: Alt<I, O, E>>( - mut l: List, -) -> impl Parser<I, O, E> { - trace("alt", move |i: I| l.choice(i)) -} - -/// Helper trait for the [permutation()] combinator. -/// -/// This trait is implemented for tuples of up to 21 elements -pub trait Permutation<I, O, E> { - /// Tries to apply all parsers in the tuple in various orders until all of them succeed - fn permutation(&mut self, input: I) -> IResult<I, O, E>; -} - -/// Applies a list of parsers in any order. -/// -/// Permutation will succeed if all of the child parsers succeeded. -/// It takes as argument a tuple of parsers, and returns a -/// tuple of the parser results. -/// -/// ```rust -/// # use winnow::{error::ErrMode,error::{Error, ErrorKind}, error::Needed}; -/// # use winnow::prelude::*; -/// use winnow::character::{alpha1, digit1}; -/// use winnow::branch::permutation; -/// # fn main() { -/// fn parser(input: &str) -> IResult<&str, (&str, &str)> { -/// permutation((alpha1, digit1)).parse_next(input) -/// } -/// -/// // permutation recognizes alphabetic characters then digit -/// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); -/// -/// // but also in inverse order -/// assert_eq!(parser("123abc"), Ok(("", ("abc", "123")))); -/// -/// // it will fail if one of the parsers failed -/// assert_eq!(parser("abc;"), Err(ErrMode::Backtrack(Error::new(";", ErrorKind::Slice)))); -/// # } -/// ``` -/// -/// The parsers are applied greedily: if there are multiple unapplied parsers -/// that could parse the next slice of input, the first one is used. -/// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}}; -/// # use winnow::prelude::*; -/// use winnow::branch::permutation; -/// use winnow::bytes::any; -/// -/// fn parser(input: &str) -> IResult<&str, (char, char)> { -/// permutation((any, 'a')).parse_next(input) -/// } -/// -/// // any parses 'b', then char('a') parses 'a' -/// assert_eq!(parser("ba"), Ok(("", ('b', 'a')))); -/// -/// // any parses 'a', then char('a') fails on 'b', -/// // even though char('a') followed by any would succeed -/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(Error::new("b", ErrorKind::Verify)))); -/// ``` -/// -pub fn permutation<I: Stream, O, E: ParseError<I>, List: Permutation<I, O, E>>( - mut l: List, -) -> impl Parser<I, O, E> { - trace("permutation", move |i: I| l.permutation(i)) -} - -macro_rules! alt_trait( - ($first:ident $second:ident $($id: ident)+) => ( - alt_trait!(__impl $first $second; $($id)+); - ); - (__impl $($current:ident)*; $head:ident $($id: ident)+) => ( - alt_trait_impl!($($current)*); - - alt_trait!(__impl $($current)* $head; $($id)+); - ); - (__impl $($current:ident)*; $head:ident) => ( - alt_trait_impl!($($current)*); - alt_trait_impl!($($current)* $head); - ); -); - -macro_rules! alt_trait_impl( - ($($id:ident)+) => ( - impl< - I: Clone, Output, Error: ParseError<I>, - $($id: Parser<I, Output, Error>),+ - > Alt<I, Output, Error> for ( $($id),+ ) { - - fn choice(&mut self, input: I) -> IResult<I, Output, Error> { - match self.0.parse_next(input.clone()) { - Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, e, $($id)+), - res => res, - } - } - } - ); -); - -macro_rules! alt_trait_inner( - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => ( - match $self.$it.parse_next($input.clone()) { - Err(ErrMode::Backtrack(e)) => { - let err = $err.or(e); - succ!($it, alt_trait_inner!($self, $input, err, $($id)+)) - } - res => res, - } - ); - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => ( - Err(ErrMode::Backtrack($err.append($input, ErrorKind::Alt))) - ); -); - -alt_trait!(Alt2 Alt3 Alt4 Alt5 Alt6 Alt7 Alt8 Alt9 Alt10 Alt11 Alt12 Alt13 Alt14 Alt15 Alt16 Alt17 Alt18 Alt19 Alt20 Alt21 Alt22); - -// Manually implement Alt for (A,), the 1-tuple type -impl<I, O, E: ParseError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) { - fn choice(&mut self, input: I) -> IResult<I, O, E> { - self.0.parse_next(input) - } -} - -macro_rules! permutation_trait( - ( - $name1:ident $ty1:ident $item1:ident - $name2:ident $ty2:ident $item2:ident - $($name3:ident $ty3:ident $item3:ident)* - ) => ( - permutation_trait!(__impl $name1 $ty1 $item1, $name2 $ty2 $item2; $($name3 $ty3 $item3)*); - ); - ( - __impl $($name:ident $ty:ident $item:ident),+; - $name1:ident $ty1:ident $item1:ident $($name2:ident $ty2:ident $item2:ident)* - ) => ( - permutation_trait_impl!($($name $ty $item),+); - permutation_trait!(__impl $($name $ty $item),+ , $name1 $ty1 $item1; $($name2 $ty2 $item2)*); - ); - (__impl $($name:ident $ty:ident $item:ident),+;) => ( - permutation_trait_impl!($($name $ty $item),+); - ); -); - -macro_rules! permutation_trait_impl( - ($($name:ident $ty:ident $item:ident),+) => ( - impl< - I: Clone, $($ty),+ , Error: ParseError<I>, - $($name: Parser<I, $ty, Error>),+ - > Permutation<I, ( $($ty),+ ), Error> for ( $($name),+ ) { - - fn permutation(&mut self, mut input: I) -> IResult<I, ( $($ty),+ ), Error> { - let mut res = ($(Option::<$ty>::None),+); - - loop { - let mut err: Option<Error> = None; - permutation_trait_inner!(0, self, input, res, err, $($name)+); - - // If we reach here, every iterator has either been applied before, - // or errored on the remaining input - if let Some(err) = err { - // There are remaining parsers, and all errored on the remaining input - return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Alt))); - } - - // All parsers were applied - match res { - ($(Some($item)),+) => return Ok((input, ($($item),+))), - _ => unreachable!(), - } - } - } - } - ); -); - -macro_rules! permutation_trait_inner( - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => ( - if $res.$it.is_none() { - match $self.$it.parse_next($input.clone()) { - Ok((i, o)) => { - $input = i; - $res.$it = Some(o); - continue; - } - Err(ErrMode::Backtrack(e)) => { - $err = Some(match $err { - Some(err) => err.or(e), - None => e, - }); - } - Err(e) => return Err(e), - }; - } - succ!($it, permutation_trait_inner!($self, $input, $res, $err, $($id)*)); - ); - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr,) => (); -); - -permutation_trait!( - P1 O1 o1 - P2 O2 o2 - P3 O3 o3 - P4 O4 o4 - P5 O5 o5 - P6 O6 o6 - P7 O7 o7 - P8 O8 o8 - P9 O9 o9 - P10 O10 o10 - P11 O11 o11 - P12 O12 o12 - P13 O13 o13 - P14 O14 o14 - P15 O15 o15 - P16 O16 o16 - P17 O17 o17 - P18 O18 o18 - P19 O19 o19 - P20 O20 o20 - P21 O21 o21 -); diff --git a/vendor/winnow/src/branch/tests.rs b/vendor/winnow/src/branch/tests.rs deleted file mode 100644 index 80bc163ce..000000000 --- a/vendor/winnow/src/branch/tests.rs +++ /dev/null @@ -1,180 +0,0 @@ -use crate::branch::{alt, permutation}; - -use crate::error::ErrMode; -use crate::error::ErrorKind; -use crate::error::Needed; -use crate::IResult; -use crate::Parser; -use crate::Partial; -#[cfg(feature = "alloc")] -use crate::{ - error::ParseError, - lib::std::{ - fmt::Debug, - string::{String, ToString}, - }, -}; - -#[cfg(feature = "alloc")] -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct ErrorStr(String); - -#[cfg(feature = "alloc")] -impl From<u32> for ErrorStr { - fn from(i: u32) -> Self { - ErrorStr(format!("custom error code: {}", i)) - } -} - -#[cfg(feature = "alloc")] -impl<'a> From<&'a str> for ErrorStr { - fn from(i: &'a str) -> Self { - ErrorStr(format!("custom error message: {}", i)) - } -} - -#[cfg(feature = "alloc")] -impl<I: Debug> ParseError<I> for ErrorStr { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { - ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) - } - - fn append(self, input: I, kind: ErrorKind) -> Self { - ErrorStr(format!( - "custom error message: ({:?}, {:?}) - {:?}", - input, kind, self - )) - } -} - -#[cfg(feature = "alloc")] -#[test] -fn alt_test() { - fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - Ok((&b""[..], input)) - } - - #[allow(unused_variables)] - fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - Err(ErrMode::Backtrack(ErrorStr("abcd".to_string()))) - } - - fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - Ok((input, &b""[..])) - } - - fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work)).parse_next(i) - } - fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, work)).parse_next(i) - } - fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work, work2, dont_work)).parse_next(i) - } - //named!(alt1, alt!(dont_work | dont_work)); - //named!(alt2, alt!(dont_work | work)); - //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); - - let a = &b"abcd"[..]; - assert_eq!( - alt1(a), - Err(ErrMode::Backtrack(error_node_position!( - a, - ErrorKind::Alt, - ErrorStr("abcd".to_string()) - ))) - ); - assert_eq!(alt2(a), Ok((&b""[..], a))); - assert_eq!(alt3(a), Ok((a, &b""[..]))); - - fn alt4(i: &[u8]) -> IResult<&[u8], &[u8]> { - alt(("abcd", "efgh")).parse_next(i) - } - let b = &b"efgh"[..]; - assert_eq!(alt4(a), Ok((&b""[..], a))); - assert_eq!(alt4(b), Ok((&b""[..], b))); -} - -#[test] -fn alt_incomplete() { - fn alt1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - alt(("a", "bc", "def")).parse_next(i) - } - - let a = &b""[..]; - assert_eq!( - alt1(Partial::new(a)), - Err(ErrMode::Incomplete(Needed::new(1))) - ); - let a = &b"b"[..]; - assert_eq!( - alt1(Partial::new(a)), - Err(ErrMode::Incomplete(Needed::new(1))) - ); - let a = &b"bcd"[..]; - assert_eq!( - alt1(Partial::new(a)), - Ok((Partial::new(&b"d"[..]), &b"bc"[..])) - ); - let a = &b"cde"[..]; - assert_eq!( - alt1(Partial::new(a)), - Err(ErrMode::Backtrack(error_position!( - Partial::new(a), - ErrorKind::Tag - ))) - ); - let a = &b"de"[..]; - assert_eq!( - alt1(Partial::new(a)), - Err(ErrMode::Incomplete(Needed::new(1))) - ); - let a = &b"defg"[..]; - assert_eq!( - alt1(Partial::new(a)), - Ok((Partial::new(&b"g"[..]), &b"def"[..])) - ); -} - -#[test] -fn permutation_test() { - #[allow(clippy::type_complexity)] - fn perm(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8], &[u8])> { - permutation(("abcd", "efg", "hi")).parse_next(i) - } - - let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); - - let a = &b"abcdefghijk"[..]; - assert_eq!( - perm(Partial::new(a)), - Ok((Partial::new(&b"jk"[..]), expected)) - ); - let b = &b"efgabcdhijk"[..]; - assert_eq!( - perm(Partial::new(b)), - Ok((Partial::new(&b"jk"[..]), expected)) - ); - let c = &b"hiefgabcdjk"[..]; - assert_eq!( - perm(Partial::new(c)), - Ok((Partial::new(&b"jk"[..]), expected)) - ); - - let d = &b"efgxyzabcdefghi"[..]; - assert_eq!( - perm(Partial::new(d)), - Err(ErrMode::Backtrack(error_node_position!( - Partial::new(&b"efgxyzabcdefghi"[..]), - ErrorKind::Alt, - error_position!(Partial::new(&b"xyzabcdefghi"[..]), ErrorKind::Tag) - ))) - ); - - let e = &b"efgabc"[..]; - assert_eq!( - perm(Partial::new(e)), - Err(ErrMode::Incomplete(Needed::new(1))) - ); -} |