diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
commit | d1b2d29528b7794b41e66fc2136e395a02f8529b (patch) | |
tree | a4a17504b260206dec3cf55b2dca82929a348ac2 /vendor/winnow/src/combinator | |
parent | Releasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/winnow/src/combinator')
-rw-r--r-- | vendor/winnow/src/combinator/branch.rs | 80 | ||||
-rw-r--r-- | vendor/winnow/src/combinator/core.rs | 175 | ||||
-rw-r--r-- | vendor/winnow/src/combinator/multi.rs | 454 | ||||
-rw-r--r-- | vendor/winnow/src/combinator/parser.rs | 265 | ||||
-rw-r--r-- | vendor/winnow/src/combinator/sequence.rs | 76 | ||||
-rw-r--r-- | vendor/winnow/src/combinator/tests.rs | 268 |
6 files changed, 649 insertions, 669 deletions
diff --git a/vendor/winnow/src/combinator/branch.rs b/vendor/winnow/src/combinator/branch.rs index 042b27d53..2a712ec1d 100644 --- a/vendor/winnow/src/combinator/branch.rs +++ b/vendor/winnow/src/combinator/branch.rs @@ -1,4 +1,4 @@ -use crate::error::{ErrMode, ErrorKind, ParseError}; +use crate::error::{ErrMode, ErrorKind, ParserError}; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -11,7 +11,7 @@ pub use crate::dispatch; /// 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>; + fn choice(&mut self, input: &mut I) -> PResult<O, E>; } /// Pick the first successful parser @@ -25,13 +25,13 @@ pub trait Alt<I, O, E> { /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::Error,error::ErrorKind, error::Needed}; +/// # use winnow::{error::ErrMode, error::InputError,error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::alt; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, &str> { -/// alt((alpha1, digit1)).parse_next(input) +/// alt((alpha1, digit1)).parse_peek(input) /// }; /// /// // the first parser, alpha1, recognizes the input @@ -41,14 +41,14 @@ pub trait Alt<I, O, E> { /// 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)))); +/// assert_eq!(parser(" "), Err(ErrMode::Backtrack(InputError::new(" ", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "choice")] -pub fn alt<I: Stream, O, E: ParseError<I>, List: Alt<I, O, E>>( +pub fn alt<I: Stream, O, E: ParserError<I>, List: Alt<I, O, E>>( mut l: List, ) -> impl Parser<I, O, E> { - trace("alt", move |i: I| l.choice(i)) + trace("alt", move |i: &mut I| l.choice(i)) } /// Helper trait for the [permutation()] combinator. @@ -56,7 +56,7 @@ pub fn alt<I: Stream, O, E: ParseError<I>, List: Alt<I, O, E>>( /// 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>; + fn permutation(&mut self, input: &mut I) -> PResult<O, E>; } /// Applies a list of parsers in any order. @@ -66,13 +66,13 @@ pub trait Permutation<I, O, E> { /// tuple of the parser results. /// /// ```rust -/// # use winnow::{error::ErrMode,error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode,error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::permutation; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, (&str, &str)> { -/// permutation((alpha1, digit1)).parse_next(input) +/// permutation((alpha1, digit1)).parse_peek(input) /// } /// /// // permutation recognizes alphabetic characters then digit @@ -82,20 +82,20 @@ pub trait Permutation<I, O, E> { /// 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)))); +/// assert_eq!(parser("abc;"), Err(ErrMode::Backtrack(InputError::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::{error::ErrMode, error::{InputError, ErrorKind}}; /// # use winnow::prelude::*; /// use winnow::combinator::permutation; /// use winnow::token::any; /// /// fn parser(input: &str) -> IResult<&str, (char, char)> { -/// permutation((any, 'a')).parse_next(input) +/// permutation((any, 'a')).parse_peek(input) /// } /// /// // any parses 'b', then char('a') parses 'a' @@ -103,13 +103,13 @@ pub trait Permutation<I, O, E> { /// /// // 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)))); +/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(InputError::new("b", ErrorKind::Verify)))); /// ``` /// -pub fn permutation<I: Stream, O, E: ParseError<I>, List: Permutation<I, O, E>>( +pub fn permutation<I: Stream, O, E: ParserError<I>, List: Permutation<I, O, E>>( mut l: List, ) -> impl Parser<I, O, E> { - trace("permutation", move |i: I| l.permutation(i)) + trace("permutation", move |i: &mut I| l.permutation(i)) } macro_rules! alt_trait( @@ -130,13 +130,14 @@ macro_rules! alt_trait( macro_rules! alt_trait_impl( ($($id:ident)+) => ( impl< - I: Clone, Output, Error: ParseError<I>, + I: Stream, Output, Error: ParserError<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)+), + fn choice(&mut self, input: &mut I) -> PResult<Output, Error> { + let start = input.checkpoint(); + match self.0.parse_next(input) { + Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, start, e, $($id)+), res => res, } } @@ -145,25 +146,26 @@ macro_rules! alt_trait_impl( ); macro_rules! alt_trait_inner( - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => ( - match $self.$it.parse_next($input.clone()) { + ($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident $($id:ident)+) => ({ + $input.reset($start.clone()); + match $self.$it.parse_next($input) { Err(ErrMode::Backtrack(e)) => { let err = $err.or(e); - succ!($it, alt_trait_inner!($self, $input, err, $($id)+)) + succ!($it, alt_trait_inner!($self, $input, $start, err, $($id)+)) } res => res, } - ); - ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => ( + }); + ($it:tt, $self:expr, $input:expr, $start:ident, $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> { +impl<I, O, E: ParserError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) { + fn choice(&mut self, input: &mut I) -> PResult<O, E> { self.0.parse_next(input) } } @@ -191,27 +193,29 @@ macro_rules! permutation_trait( macro_rules! permutation_trait_impl( ($($name:ident $ty:ident $item:ident),+) => ( impl< - I: Clone, $($ty),+ , Error: ParseError<I>, + I: Stream, $($ty),+ , Error: ParserError<I>, $($name: Parser<I, $ty, Error>),+ > Permutation<I, ( $($ty),+ ), Error> for ( $($name),+ ) { - fn permutation(&mut self, mut input: I) -> IResult<I, ( $($ty),+ ), Error> { + fn permutation(&mut self, input: &mut I) -> PResult<( $($ty),+ ), Error> { let mut res = ($(Option::<$ty>::None),+); loop { let mut err: Option<Error> = None; - permutation_trait_inner!(0, self, input, res, err, $($name)+); + let start = input.checkpoint(); + permutation_trait_inner!(0, self, input, start, 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 + input.reset(start.clone()); return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Alt))); } // All parsers were applied match res { - ($(Some($item)),+) => return Ok((input, ($($item),+))), + ($(Some($item)),+) => return Ok(($($item),+)), _ => unreachable!(), } } @@ -221,11 +225,11 @@ macro_rules! permutation_trait_impl( ); macro_rules! permutation_trait_inner( - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => ( + ($it:tt, $self:expr, $input:ident, $start: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; + $input.reset($start.clone()); + match $self.$it.parse_next($input) { + Ok(o) => { $res.$it = Some(o); continue; } @@ -238,9 +242,9 @@ macro_rules! permutation_trait_inner( Err(e) => return Err(e), }; } - succ!($it, permutation_trait_inner!($self, $input, $res, $err, $($id)*)); + succ!($it, permutation_trait_inner!($self, $input, $start, $res, $err, $($id)*)); ); - ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr,) => (); + ($it:tt, $self:expr, $input:ident, $start:ident, $res:expr, $err:expr,) => (); ); permutation_trait!( diff --git a/vendor/winnow/src/combinator/core.rs b/vendor/winnow/src/combinator/core.rs index 75551d7c5..d784b4e9e 100644 --- a/vendor/winnow/src/combinator/core.rs +++ b/vendor/winnow/src/combinator/core.rs @@ -1,4 +1,4 @@ -use crate::error::{ErrMode, ErrorKind, Needed, ParseError}; +use crate::error::{ErrMode, ErrorKind, Needed, ParserError}; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -8,21 +8,19 @@ use crate::*; /// # Example /// /// ```rust +/// # use winnow::prelude::*; /// # use winnow::error::ErrorKind; -/// # use winnow::error::Error; +/// # use winnow::error::InputError; /// use winnow::combinator::rest; -/// assert_eq!(rest::<_,Error<_>>("abc"), Ok(("", "abc"))); -/// assert_eq!(rest::<_,Error<_>>(""), Ok(("", ""))); +/// assert_eq!(rest::<_,InputError<_>>.parse_peek("abc"), Ok(("", "abc"))); +/// assert_eq!(rest::<_,InputError<_>>.parse_peek(""), Ok(("", ""))); /// ``` #[inline] -pub fn rest<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn rest<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: Stream, { - trace("rest", move |input: I| { - Ok(input.next_slice(input.eof_offset())) - }) - .parse_next(input) + trace("rest", move |input: &mut I| Ok(input.finish())).parse_next(input) } /// Return the length of the remaining input. @@ -32,20 +30,21 @@ where /// # Example /// /// ```rust +/// # use winnow::prelude::*; /// # use winnow::error::ErrorKind; -/// # use winnow::error::Error; +/// # use winnow::error::InputError; /// use winnow::combinator::rest_len; -/// assert_eq!(rest_len::<_,Error<_>>("abc"), Ok(("abc", 3))); -/// assert_eq!(rest_len::<_,Error<_>>(""), Ok(("", 0))); +/// assert_eq!(rest_len::<_,InputError<_>>.parse_peek("abc"), Ok(("abc", 3))); +/// assert_eq!(rest_len::<_,InputError<_>>.parse_peek(""), Ok(("", 0))); /// ``` #[inline] -pub fn rest_len<I, E: ParseError<I>>(input: I) -> IResult<I, usize, E> +pub fn rest_len<I, E: ParserError<I>>(input: &mut I) -> PResult<usize, E> where I: Stream, { - trace("rest_len", move |input: I| { + trace("rest_len", move |input: &mut I| { let len = input.eof_offset(); - Ok((input, len)) + Ok(len) }) .parse_next(input) } @@ -57,29 +56,32 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// use winnow::combinator::opt; /// use winnow::ascii::alpha1; /// # fn main() { /// /// fn parser(i: &str) -> IResult<&str, Option<&str>> { -/// opt(alpha1).parse_next(i) +/// opt(alpha1).parse_peek(i) /// } /// /// assert_eq!(parser("abcd;"), Ok((";", Some("abcd")))); /// assert_eq!(parser("123;"), Ok(("123;", None))); /// # } /// ``` -pub fn opt<I: Stream, O, E: ParseError<I>, F>(mut f: F) -> impl Parser<I, Option<O>, E> +pub fn opt<I: Stream, O, E: ParserError<I>, F>(mut f: F) -> impl Parser<I, Option<O>, E> where F: Parser<I, O, E>, { - trace("opt", move |input: I| { - let i = input.clone(); + trace("opt", move |input: &mut I| { + let start = input.checkpoint(); match f.parse_next(input) { - Ok((i, o)) => Ok((i, Some(o))), - Err(ErrMode::Backtrack(_)) => Ok((i, None)), + Ok(o) => Ok(Some(o)), + Err(ErrMode::Backtrack(_)) => { + input.reset(start); + Ok(None) + } Err(e) => Err(e), } }) @@ -90,35 +92,32 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, IResult}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::cond; /// use winnow::ascii::alpha1; /// # fn main() { /// /// fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> { -/// cond(b, alpha1).parse_next(i) +/// cond(b, alpha1).parse_peek(i) /// } /// /// assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd")))); /// assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None))); -/// assert_eq!(parser(true, "123;"), Err(ErrMode::Backtrack(Error::new("123;", ErrorKind::Slice)))); +/// assert_eq!(parser(true, "123;"), Err(ErrMode::Backtrack(InputError::new("123;", ErrorKind::Slice)))); /// assert_eq!(parser(false, "123;"), Ok(("123;", None))); /// # } /// ``` -pub fn cond<I, O, E: ParseError<I>, F>(b: bool, mut f: F) -> impl Parser<I, Option<O>, E> +pub fn cond<I, O, E: ParserError<I>, F>(b: bool, mut f: F) -> impl Parser<I, Option<O>, E> where I: Stream, F: Parser<I, O, E>, { - trace("cond", move |input: I| { + trace("cond", move |input: &mut I| { if b { - match f.parse_next(input) { - Ok((i, o)) => Ok((i, Some(o))), - Err(e) => Err(e), - } + f.parse_next(input).map(Some) } else { - Ok((input, None)) + Ok(None) } }) } @@ -128,7 +127,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::peek; /// use winnow::ascii::alpha1; @@ -136,22 +135,21 @@ where /// /// let mut parser = peek(alpha1); /// -/// assert_eq!(parser.parse_next("abcd;"), Ok(("abcd;", "abcd"))); -/// assert_eq!(parser.parse_next("123;"), Err(ErrMode::Backtrack(Error::new("123;", ErrorKind::Slice)))); +/// assert_eq!(parser.parse_peek("abcd;"), Ok(("abcd;", "abcd"))); +/// assert_eq!(parser.parse_peek("123;"), Err(ErrMode::Backtrack(InputError::new("123;", ErrorKind::Slice)))); /// # } /// ``` #[doc(alias = "look_ahead")] #[doc(alias = "rewind")] -pub fn peek<I: Stream, O, E: ParseError<I>, F>(mut f: F) -> impl Parser<I, O, E> +pub fn peek<I: Stream, O, E: ParserError<I>, F>(mut f: F) -> impl Parser<I, O, E> where F: Parser<I, O, E>, { - trace("peek", move |input: I| { - let i = input.clone(); - match f.parse_next(input) { - Ok((_, o)) => Ok((i, o)), - Err(e) => Err(e), - } + trace("peek", move |input: &mut I| { + let start = input.checkpoint(); + let res = f.parse_next(input); + input.reset(start); + res }) } @@ -163,21 +161,21 @@ where /// /// ```rust /// # use std::str; -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::combinator::eof; /// # use winnow::prelude::*; /// /// let mut parser = eof; -/// assert_eq!(parser.parse_next("abc"), Err(ErrMode::Backtrack(Error::new("abc", ErrorKind::Eof)))); -/// assert_eq!(parser.parse_next(""), Ok(("", ""))); +/// assert_eq!(parser.parse_peek("abc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Eof)))); +/// assert_eq!(parser.parse_peek(""), Ok(("", ""))); /// ``` #[doc(alias = "end")] #[doc(alias = "eoi")] -pub fn eof<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E> +pub fn eof<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E> where I: Stream, { - trace("eof", move |input: I| { + trace("eof", move |input: &mut I| { if input.eof_offset() == 0 { Ok(input.next_slice(0)) } else { @@ -194,7 +192,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; /// # use winnow::prelude::*; /// use winnow::combinator::not; /// use winnow::ascii::alpha1; @@ -202,19 +200,21 @@ where /// /// let mut parser = not(alpha1); /// -/// assert_eq!(parser.parse_next("123"), Ok(("123", ()))); -/// assert_eq!(parser.parse_next("abcd"), Err(ErrMode::Backtrack(Error::new("abcd", ErrorKind::Not)))); +/// assert_eq!(parser.parse_peek("123"), Ok(("123", ()))); +/// assert_eq!(parser.parse_peek("abcd"), Err(ErrMode::Backtrack(InputError::new("abcd", ErrorKind::Not)))); /// # } /// ``` -pub fn not<I: Stream, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, (), E> +pub fn not<I: Stream, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, (), E> where F: Parser<I, O, E>, { - trace("not", move |input: I| { - let i = input.clone(); - match parser.parse_next(input) { - Ok(_) => Err(ErrMode::from_error_kind(i, ErrorKind::Not)), - Err(ErrMode::Backtrack(_)) => Ok((i, ())), + trace("not", move |input: &mut I| { + let start = input.checkpoint(); + let res = parser.parse_next(input); + input.reset(start); + match res { + Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Not)), + Err(ErrMode::Backtrack(_)) => Ok(()), Err(e) => Err(e), } }) @@ -229,7 +229,7 @@ where /// /// Without `cut_err`: /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::token::one_of; /// # use winnow::ascii::digit1; /// # use winnow::combinator::rest; @@ -240,9 +240,9 @@ where /// /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( -/// preceded(one_of("+-"), digit1), +/// preceded(one_of(['+', '-']), digit1), /// rest -/// )).parse_next(input) +/// )).parse_peek(input) /// } /// /// assert_eq!(parser("+10 ab"), Ok((" ab", "10"))); @@ -253,7 +253,7 @@ where /// /// With `cut_err`: /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// # use winnow::token::one_of; /// # use winnow::ascii::digit1; @@ -265,22 +265,22 @@ where /// /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( -/// preceded(one_of("+-"), cut_err(digit1)), +/// preceded(one_of(['+', '-']), cut_err(digit1)), /// rest -/// )).parse_next(input) +/// )).parse_peek(input) /// } /// /// assert_eq!(parser("+10 ab"), Ok((" ab", "10"))); /// assert_eq!(parser("ab"), Ok(("", "ab"))); -/// assert_eq!(parser("+"), Err(ErrMode::Cut(Error { input: "", kind: ErrorKind::Slice }))); +/// assert_eq!(parser("+"), Err(ErrMode::Cut(InputError::new("", ErrorKind::Slice )))); /// # } /// ``` -pub fn cut_err<I, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, O, E> +pub fn cut_err<I, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, O, E> where I: Stream, F: Parser<I, O, E>, { - trace("cut_err", move |input: I| { + trace("cut_err", move |input: &mut I| { parser.parse_next(input).map_err(|e| e.cut()) }) } @@ -289,12 +289,12 @@ where /// /// This attempts the parse, allowing other parsers to be tried on failure, like with /// [`winnow::combinator::alt`][crate::combinator::alt]. -pub fn backtrack_err<I, O, E: ParseError<I>, F>(mut parser: F) -> impl Parser<I, O, E> +pub fn backtrack_err<I, O, E: ParserError<I>, F>(mut parser: F) -> impl Parser<I, O, E> where I: Stream, F: Parser<I, O, E>, { - trace("backtrack_err", move |input: I| { + trace("backtrack_err", move |input: &mut I| { parser.parse_next(input).map_err(|e| e.backtrack()) }) } @@ -313,17 +313,17 @@ where /// # use winnow::prelude::*; /// # use winnow::combinator::todo; /// -/// fn parser(input: &str) -> IResult<&str, u64> { +/// fn parser(input: &mut &str) -> PResult<u64> { /// todo(input) /// } /// ``` #[track_caller] -pub fn todo<I, O, E>(input: I) -> IResult<I, O, E> +pub fn todo<I, O, E>(input: &mut I) -> PResult<O, E> where I: Stream, { #![allow(clippy::todo)] - trace("todo", move |_input: I| todo!("unimplemented parse")).parse_next(input) + trace("todo", move |_input: &mut I| todo!("unimplemented parse")).parse_next(input) } /// Repeats the embedded parser, lazily returning the results @@ -352,7 +352,7 @@ pub fn iterator<I, O, E, F>(input: I, parser: F) -> ParserIterator<F, I, O, E> where F: Parser<I, O, E>, I: Stream, - E: ParseError<I>, + E: ParserError<I>, { ParserIterator { parser, @@ -380,7 +380,7 @@ where I: Stream, { /// Returns the remaining input if parsing was successful, or the error if we encountered an error. - pub fn finish(mut self) -> IResult<I, (), E> { + pub fn finish(mut self) -> PResult<(I, ()), E> { match self.state.take().unwrap() { State::Running | State::Done => Ok((self.input, ())), State::Failure(e) => Err(ErrMode::Cut(e)), @@ -398,15 +398,15 @@ where fn next(&mut self) -> Option<Self::Item> { if let State::Running = self.state.take().unwrap() { - let input = self.input.clone(); + let start = self.input.checkpoint(); - match self.parser.parse_next(input) { - Ok((i, o)) => { - self.input = i; + match self.parser.parse_next(&mut self.input) { + Ok(o) => { self.state = Some(State::Running); Some(o) } Err(ErrMode::Backtrack(_)) => { + self.input.reset(start); self.state = Some(State::Done); None } @@ -442,20 +442,20 @@ enum State<E> { /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError}; /// # use winnow::prelude::*; /// use winnow::combinator::alt; /// use winnow::combinator::success; /// -/// let mut parser = success::<_,_,Error<_>>(10); -/// assert_eq!(parser.parse_next("xyz"), Ok(("xyz", 10))); +/// let mut parser = success::<_,_,InputError<_>>(10); +/// assert_eq!(parser.parse_peek("xyz"), Ok(("xyz", 10))); /// /// fn sign(input: &str) -> IResult<&str, isize> { /// alt(( /// '-'.value(-1), /// '+'.value(1), -/// success::<_,_,Error<_>>(1) -/// )).parse_next(input) +/// success::<_,_,InputError<_>>(1) +/// )).parse_peek(input) /// } /// assert_eq!(sign("+10"), Ok(("10", 1))); /// assert_eq!(sign("-10"), Ok(("10", -1))); @@ -463,8 +463,8 @@ enum State<E> { /// ``` #[doc(alias = "value")] #[doc(alias = "empty")] -pub fn success<I: Stream, O: Clone, E: ParseError<I>>(val: O) -> impl Parser<I, O, E> { - trace("success", move |input: I| Ok((input, val.clone()))) +pub fn success<I: Stream, O: Clone, E: ParserError<I>>(val: O) -> impl Parser<I, O, E> { + trace("success", move |_input: &mut I| Ok(val.clone())) } /// A parser which always fails. @@ -475,15 +475,16 @@ pub fn success<I: Stream, O: Clone, E: ParseError<I>>(val: O) -> impl Parser<I, /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, IResult}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, IResult}; +/// # use winnow::prelude::*; /// use winnow::combinator::fail; /// /// let s = "string"; -/// assert_eq!(fail::<_, &str, _>(s), Err(ErrMode::Backtrack(Error::new(s, ErrorKind::Fail)))); +/// assert_eq!(fail::<_, &str, _>.parse_peek(s), Err(ErrMode::Backtrack(InputError::new(s, ErrorKind::Fail)))); /// ``` #[doc(alias = "unexpected")] -pub fn fail<I: Stream, O, E: ParseError<I>>(i: I) -> IResult<I, O, E> { - trace("fail", |i| { +pub fn fail<I: Stream, O, E: ParserError<I>>(i: &mut I) -> PResult<O, E> { + trace("fail", |i: &mut I| { Err(ErrMode::from_error_kind(i, ErrorKind::Fail)) }) .parse_next(i) diff --git a/vendor/winnow/src/combinator/multi.rs b/vendor/winnow/src/combinator/multi.rs index 950432601..1fdb7535b 100644 --- a/vendor/winnow/src/combinator/multi.rs +++ b/vendor/winnow/src/combinator/multi.rs @@ -2,12 +2,12 @@ use crate::error::ErrMode; use crate::error::ErrorKind; -use crate::error::ParseError; +use crate::error::ParserError; use crate::stream::Accumulate; use crate::stream::Range; use crate::stream::Stream; use crate::trace::trace; -use crate::IResult; +use crate::PResult; use crate::Parser; /// [`Accumulate`] the output of a parser into a container, like `Vec` @@ -37,7 +37,7 @@ use crate::Parser; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(0.., "abc").parse_next(s) +/// repeat(0.., "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -50,38 +50,38 @@ use crate::Parser; /// One or more reptitions: /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(1.., "abc").parse_next(s) +/// repeat(1.., "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); /// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// # } /// ``` /// /// Fixed number of repeitions: /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(2, "abc").parse_next(s) +/// repeat(2, "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); -/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); /// # } /// ``` @@ -95,7 +95,7 @@ use crate::Parser; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// repeat(0..=2, "abc").parse_next(s) +/// repeat(0..=2, "abc").parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -120,13 +120,13 @@ where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { let Range { start_inclusive, end_inclusive, } = range.into(); - trace("repeat", move |i: I| { + trace("repeat", move |i: &mut I| { match (start_inclusive, end_inclusive) { (0, None) => repeat0_(&mut f, i), (1, None) => repeat1_(&mut f, i), @@ -136,84 +136,63 @@ where }) } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn repeat0<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - repeat(0.., f) -} - -fn repeat0_<I, O, C, E, F>(f: &mut F, mut i: I) -> IResult<I, C, E> +fn repeat0_<I, O, C, E, F>(f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { let mut acc = C::initial(None); loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match f.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, acc)), + match f.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(acc); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - i = i1; acc.accumulate(o); } } } } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn repeat1<I, O, C, E, F>(f: F) -> impl Parser<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - repeat(1.., f) -} - -fn repeat1_<I, O, C, E, F>(f: &mut F, mut i: I) -> IResult<I, C, E> +fn repeat1_<I, O, C, E, F>(f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - match f.parse_next(i.clone()) { + match f.parse_next(i) { Err(e) => Err(e.append(i, ErrorKind::Many)), - Ok((i1, o)) => { + Ok(o) => { let mut acc = C::initial(None); acc.accumulate(o); - i = i1; loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match f.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, acc)), + match f.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(acc); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - i = i1; acc.accumulate(o); } } @@ -235,19 +214,19 @@ where /// /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::repeat_till0; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> { -/// repeat_till0("abc", "end").parse_next(s) +/// repeat_till0("abc", "end").parse_peek(s) /// }; /// /// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end")))); -/// assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(Error::new("123end", ErrorKind::Tag)))); -/// assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(Error::new("123123end", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123end"), Err(ErrMode::Backtrack(InputError::new("123end", ErrorKind::Tag)))); +/// assert_eq!(parser("123123end"), Err(ErrMode::Backtrack(InputError::new("123123end", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end")))); /// # } /// ``` @@ -258,20 +237,22 @@ where C: Accumulate<O>, F: Parser<I, O, E>, G: Parser<I, P, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("repeat_till0", move |mut i: I| { + trace("repeat_till0", move |i: &mut I| { let mut res = C::initial(None); loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match g.parse_next(i.clone()) { - Ok((i1, o)) => return Ok((i1, (res, o))), + match g.parse_next(i) { + Ok(o) => return Ok((res, o)), Err(ErrMode::Backtrack(_)) => { - match f.parse_next(i.clone()) { + i.reset(start); + match f.parse_next(i) { Err(e) => return Err(e.append(i, ErrorKind::Many)), - Ok((i1, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert( i, "`repeat` parsers must always consume", @@ -279,7 +260,6 @@ where } res.accumulate(o); - i = i1; } } } @@ -308,7 +288,7 @@ where /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// separated0("abc", "|").parse_next(s) +/// separated0("abc", "|").parse_peek(s) /// } /// /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); @@ -326,37 +306,46 @@ where C: Accumulate<O>, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("separated0", move |mut i: I| { + trace("separated0", move |i: &mut I| { let mut res = C::initial(None); - match parser.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + let start = i.checkpoint(); + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { res.accumulate(o); - i = i1; } } loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, _)) => { + Ok(_) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "sep parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i2, o)) => { + Ok(o) => { res.accumulate(o); - i = i2; } } } @@ -380,20 +369,20 @@ where /// /// ```rust /// # #[cfg(feature = "std")] { -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated1; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// separated1("abc", "|").parse_next(s) +/// separated1("abc", "|").parse_peek(s) /// } /// /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); /// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); /// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Tag)))); /// # } /// ``` #[doc(alias = "sep_by1")] @@ -404,37 +393,42 @@ where C: Accumulate<O>, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, { - trace("separated1", move |mut i: I| { + trace("separated1", move |i: &mut I| { let mut res = C::initial(None); // Parse the first element - match parser.parse_next(i.clone()) { + match parser.parse_next(i) { Err(e) => return Err(e), - Ok((i1, o)) => { + Ok(o) => { res.accumulate(o); - i = i1; } } loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i1, _)) => { + Ok(_) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "sep parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, res)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(res); + } Err(e) => return Err(e), - Ok((i2, o)) => { + Ok(o) => { res.accumulate(o); - i = i2; } } } @@ -451,18 +445,18 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated_foldl1; /// use winnow::ascii::dec_int; /// /// fn parser(s: &str) -> IResult<&str, i32> { -/// separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_next(s) +/// separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_peek(s) /// } /// /// assert_eq!(parser("9-3-5"), Ok(("", 1))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Slice)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Slice)))); /// ``` pub fn separated_foldl1<I, O, O2, E, P, S, Op>( mut parser: P, @@ -473,29 +467,35 @@ where I: Stream, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, Op: Fn(O, O2, O) -> O, { - trace("separated_foldl1", move |i: I| { - let (mut i, mut ol) = parser.parse_next(i)?; + trace("separated_foldl1", move |i: &mut I| { + let mut ol = parser.parse_next(i)?; loop { + let start = i.checkpoint(); let len = i.eof_offset(); - match sep.parse_next(i.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, ol)), + match sep.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(ol); + } Err(e) => return Err(e), - Ok((i1, s)) => { + Ok(s) => { // infinite loop check: the parser must always consume - if i1.eof_offset() == len { + if i.eof_offset() == len { return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } - match parser.parse_next(i1.clone()) { - Err(ErrMode::Backtrack(_)) => return Ok((i, ol)), + match parser.parse_next(i) { + Err(ErrMode::Backtrack(_)) => { + i.reset(start); + return Ok(ol); + } Err(e) => return Err(e), - Ok((i2, or)) => { + Ok(or) => { ol = op(ol, s, or); - i = i2; } } } @@ -512,19 +512,19 @@ where /// # Example /// /// ``` -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::separated_foldr1; /// use winnow::ascii::dec_uint; /// /// fn parser(s: &str) -> IResult<&str, u32> { -/// separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_next(s) +/// separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_peek(s) /// } /// /// assert_eq!(parser("2^3^2"), Ok(("", 512))); /// assert_eq!(parser("2"), Ok(("", 2))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice)))); -/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(Error::new("def|abc", ErrorKind::Slice)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice)))); +/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Slice)))); /// ``` #[cfg(feature = "alloc")] pub fn separated_foldr1<I, O, O2, E, P, S, Op>( @@ -536,12 +536,12 @@ where I: Stream, P: Parser<I, O, E>, S: Parser<I, O2, E>, - E: ParseError<I>, + E: ParserError<I>, Op: Fn(O, O2, O) -> O, { - trace("separated_foldr1", move |i: I| { - let (i, ol) = parser.parse_next(i)?; - let (i, all): (_, crate::lib::std::vec::Vec<(O2, O)>) = + trace("separated_foldr1", move |i: &mut I| { + let ol = parser.parse_next(i)?; + let all: crate::lib::std::vec::Vec<(O2, O)> = repeat(0.., (sep.by_ref(), parser.by_ref())).parse_next(i)?; if let Some((s, or)) = all .into_iter() @@ -549,24 +549,19 @@ where .reduce(|(sr, or), (sl, ol)| (sl, op(ol, sr, or))) { let merged = op(ol, s, or); - Ok((i, merged)) + Ok(merged) } else { - Ok((i, ol)) + Ok(ol) } }) } -fn repeat_m_n_<I, O, C, E, F>( - min: usize, - max: usize, - parse: &mut F, - mut input: I, -) -> IResult<I, C, E> +fn repeat_m_n_<I, O, C, E, F>(min: usize, max: usize, parse: &mut F, input: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); @@ -574,11 +569,12 @@ where let mut res = C::initial(Some(min)); for count in 0..max { + let start = input.checkpoint(); let len = input.eof_offset(); - match parse.parse_next(input.clone()) { - Ok((tail, value)) => { + match parse.parse_next(input) { + Ok(value) => { // infinite loop check: the parser must always consume - if tail.eof_offset() == len { + if input.eof_offset() == len { return Err(ErrMode::assert( input, "`repeat` parsers must always consume", @@ -586,13 +582,13 @@ where } res.accumulate(value); - input = tail; } Err(ErrMode::Backtrack(e)) => { if count < min { return Err(ErrMode::Backtrack(e.append(input, ErrorKind::Many))); } else { - return Ok((input, res)); + input.reset(start); + return Ok(res); } } Err(e) => { @@ -601,38 +597,22 @@ where } } - Ok((input, res)) + Ok(res) } -/// Deprecated, replaced by [`repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `repeat`")] -#[inline(always)] -pub fn count<I, O, C, E, F>(f: F, count: usize) -> impl Parser<I, C, E> +fn repeat_n_<I, O, C, E, F>(count: usize, f: &mut F, i: &mut I) -> PResult<C, E> where I: Stream, C: Accumulate<O>, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - repeat(count, f) -} - -fn repeat_n_<I, O, C, E, F>(count: usize, f: &mut F, i: I) -> IResult<I, C, E> -where - I: Stream, - C: Accumulate<O>, - F: Parser<I, O, E>, - E: ParseError<I>, -{ - let mut input = i.clone(); let mut res = C::initial(Some(count)); for _ in 0..count { - let input_ = input.clone(); - match f.parse_next(input_) { - Ok((i, o)) => { + match f.parse_next(i) { + Ok(o) => { res.accumulate(o); - input = i; } Err(e) => { return Err(e.append(i, ErrorKind::Many)); @@ -640,7 +620,7 @@ where } } - Ok((input, res)) + Ok(res) } /// Repeats the embedded parser, filling the given slice with results. @@ -654,38 +634,34 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::fill; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, [&str; 2]> { /// let mut buf = ["", ""]; -/// let (rest, ()) = fill("abc", &mut buf).parse_next(s)?; +/// let (rest, ()) = fill("abc", &mut buf).parse_peek(s)?; /// Ok((rest, buf)) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", ["abc", "abc"]))); -/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Tag)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); +/// assert_eq!(parser("abc123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", ["abc", "abc"]))); /// ``` pub fn fill<'a, I, O, E, F>(mut f: F, buf: &'a mut [O]) -> impl Parser<I, (), E> + 'a where I: Stream + 'a, F: Parser<I, O, E> + 'a, - E: ParseError<I> + 'a, + E: ParserError<I> + 'a, { - trace("fill", move |i: I| { - let mut input = i.clone(); - + trace("fill", move |i: &mut I| { for elem in buf.iter_mut() { - let input_ = input.clone(); - match f.parse_next(input_) { - Ok((i, o)) => { + match f.parse_next(i) { + Ok(o) => { *elem = o; - input = i; } Err(e) => { return Err(e.append(i, ErrorKind::Many)); @@ -693,7 +669,7 @@ where } } - Ok((input, ())) + Ok(()) }) } @@ -732,7 +708,7 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -743,7 +719,7 @@ where /// /// One or more repetitions: /// ```rust -/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; +/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; /// use winnow::combinator::fold_repeat; /// use winnow::token::tag; @@ -757,13 +733,13 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); /// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); -/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Many)))); -/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Many)))); +/// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Many)))); +/// assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Many)))); /// ``` /// /// Arbitrary number of repetitions: @@ -782,7 +758,7 @@ where /// acc.push(item); /// acc /// } -/// ).parse_next(s) +/// ).parse_peek(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -806,13 +782,13 @@ where F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { let Range { start_inclusive, end_inclusive, } = range.into(); - trace("fold_repeat", move |i: I| { + trace("fold_repeat", move |i: &mut I| { match (start_inclusive, end_inclusive) { (0, None) => fold_repeat0_(&mut f, &mut init, &mut g, i), (1, None) => fold_repeat1_(&mut f, &mut init, &mut g, i), @@ -828,48 +804,39 @@ where }) } -/// Deprecated, replaced by [`fold_repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `fold_repeat`")] -#[inline(always)] -pub fn fold_repeat0<I, O, E, F, G, H, R>(mut f: F, mut init: H, mut g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - trace("fold_repeat0", move |i: I| { - fold_repeat0_(&mut f, &mut init, &mut g, i) - }) -} - -fn fold_repeat0_<I, O, E, F, G, H, R>(f: &mut F, init: &mut H, g: &mut G, i: I) -> IResult<I, R, E> +fn fold_repeat0_<I, O, E, F, G, H, R>( + f: &mut F, + init: &mut H, + g: &mut G, + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { let mut res = init(); - let mut input = i; loop { - let i_ = input.clone(); + let start = input.checkpoint(); let len = input.eof_offset(); - match f.parse_next(i_) { - Ok((i, o)) => { + match f.parse_next(input) { + Ok(o) => { // infinite loop check: the parser must always consume - if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + if input.eof_offset() == len { + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } res = g(res, o); - input = i; } Err(ErrMode::Backtrack(_)) => { - return Ok((input, res)); + input.reset(start); + return Ok(res); } Err(e) => { return Err(e); @@ -878,60 +845,50 @@ where } } -/// Deprecated, replaced by [`fold_repeat`] -#[deprecated(since = "0.4.6", note = "Replaced with `fold_repeat`")] -#[inline(always)] -pub fn fold_repeat1<I, O, E, F, G, H, R>(mut f: F, mut init: H, mut g: G) -> impl Parser<I, R, E> -where - I: Stream, - F: Parser<I, O, E>, - G: FnMut(R, O) -> R, - H: FnMut() -> R, - E: ParseError<I>, -{ - trace("fold_repeat1", move |i: I| { - fold_repeat1_(&mut f, &mut init, &mut g, i) - }) -} - -fn fold_repeat1_<I, O, E, F, G, H, R>(f: &mut F, init: &mut H, g: &mut G, i: I) -> IResult<I, R, E> +fn fold_repeat1_<I, O, E, F, G, H, R>( + f: &mut F, + init: &mut H, + g: &mut G, + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { - let _i = i.clone(); let init = init(); - match f.parse_next(_i) { - Err(ErrMode::Backtrack(_)) => Err(ErrMode::from_error_kind(i, ErrorKind::Many)), + match f.parse_next(input) { + Err(ErrMode::Backtrack(_)) => Err(ErrMode::from_error_kind(input, ErrorKind::Many)), Err(e) => Err(e), - Ok((i1, o1)) => { + Ok(o1) => { let mut acc = g(init, o1); - let mut input = i1; loop { - let _input = input.clone(); + let start = input.checkpoint(); let len = input.eof_offset(); - match f.parse_next(_input) { + match f.parse_next(input) { Err(ErrMode::Backtrack(_)) => { + input.reset(start); break; } Err(e) => return Err(e), - Ok((i, o)) => { + Ok(o) => { // infinite loop check: the parser must always consume - if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + if input.eof_offset() == len { + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } acc = g(acc, o); - input = i; } } } - Ok((input, acc)) + Ok(acc) } } } @@ -942,14 +899,14 @@ fn fold_repeat_m_n_<I, O, E, F, G, H, R>( parse: &mut F, init: &mut H, fold: &mut G, - mut input: I, -) -> IResult<I, R, E> + input: &mut I, +) -> PResult<R, E> where I: Stream, F: Parser<I, O, E>, G: FnMut(R, O) -> R, H: FnMut() -> R, - E: ParseError<I>, + E: ParserError<I>, { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); @@ -957,11 +914,12 @@ where let mut acc = init(); for count in 0..max { + let start = input.checkpoint(); let len = input.eof_offset(); - match parse.parse_next(input.clone()) { - Ok((tail, value)) => { + match parse.parse_next(input) { + Ok(value) => { // infinite loop check: the parser must always consume - if tail.eof_offset() == len { + if input.eof_offset() == len { return Err(ErrMode::assert( input, "`repeat` parsers must always consume", @@ -969,13 +927,13 @@ where } acc = fold(acc, value); - input = tail; } //FInputXMError: handle failure properly Err(ErrMode::Backtrack(err)) => { if count < min { return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Many))); } else { + input.reset(start); break; } } @@ -983,5 +941,5 @@ where } } - Ok((input, acc)) + Ok(acc) } diff --git a/vendor/winnow/src/combinator/parser.rs b/vendor/winnow/src/combinator/parser.rs index 12b223a2e..969c2d525 100644 --- a/vendor/winnow/src/combinator/parser.rs +++ b/vendor/winnow/src/combinator/parser.rs @@ -1,8 +1,8 @@ -use crate::error::{ContextError, ErrMode, ErrorKind, FromExternalError, ParseError}; +use crate::error::{AddContext, ErrMode, ErrorKind, FromExternalError, ParserError}; use crate::lib::std::borrow::Borrow; use crate::lib::std::ops::Range; +use crate::stream::StreamIsPartial; use crate::stream::{Location, Stream}; -use crate::stream::{Offset, StreamIsPartial}; use crate::trace::trace; use crate::trace::trace_result; use crate::*; @@ -20,7 +20,8 @@ impl<'p, P> ByRef<'p, P> { } impl<'p, I, O, E, P: Parser<I, O, E>> Parser<I, O, E> for ByRef<'p, P> { - fn parse_next(&mut self, i: I) -> IResult<I, O, E> { + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { self.p.parse_next(i) } } @@ -62,24 +63,22 @@ where F: Parser<I, O, E>, G: Fn(O) -> O2, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { match self.parser.parse_next(i) { Err(e) => Err(e), - Ok((i, o)) => Ok((i, (self.map)(o))), + Ok(o) => Ok((self.map)(o)), } } } -#[deprecated(since = "0.4.2", note = "Replaced with `TryMap`")] -pub use TryMap as MapRes; - /// Implementation of [`Parser::try_map`] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] pub struct TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { parser: F, @@ -95,7 +94,7 @@ impl<F, G, I, O, O2, E, E2> TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { pub(crate) fn new(parser: F, map: G) -> Self { @@ -115,16 +114,17 @@ impl<F, G, I, O, O2, E, E2> Parser<I, O2, E> for TryMap<F, G, I, O, O2, E, E2> where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, - I: Clone, + I: Stream, E: FromExternalError<I, E2>, { - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - let res = match (self.map)(o) { - Ok(o2) => Ok((input, o2)), - Err(e) => Err(ErrMode::from_external_error(i, ErrorKind::Verify, e)), - }; + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.map)(o).map_err(|err| { + input.reset(start); + ErrMode::from_external_error(input, ErrorKind::Verify, err) + }); trace_result("verify", &res); res } @@ -136,8 +136,8 @@ pub struct VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, + I: Stream, + E: ParserError<I>, { parser: F, map: G, @@ -151,8 +151,8 @@ impl<F, G, I, O, O2, E> VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, + I: Stream, + E: ParserError<I>, { pub(crate) fn new(parser: F, map: G) -> Self { Self { @@ -170,16 +170,17 @@ impl<F, G, I, O, O2, E> Parser<I, O2, E> for VerifyMap<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, - I: Clone, - E: ParseError<I>, -{ - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - let res = match (self.map)(o) { - Some(o2) => Ok((input, o2)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Verify)), - }; + I: Stream, + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.map)(o).ok_or_else(|| { + input.reset(start); + ErrMode::from_error_kind(input, ErrorKind::Verify) + }); trace_result("verify", &res); res } @@ -192,6 +193,7 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { outer: F, inner: G, @@ -206,6 +208,7 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { pub(crate) fn new(outer: F, inner: G) -> Self { Self { @@ -224,12 +227,18 @@ where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, + I: Stream, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let (i, mut o) = self.outer.parse_next(i)?; + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let start = i.checkpoint(); + let mut o = self.outer.parse_next(i)?; let _ = o.complete(); - let (_, o2) = self.inner.parse_next(o)?; - Ok((i, o2)) + let o2 = self.inner.parse_next(&mut o).map_err(|err| { + i.reset(start); + err + })?; + Ok(o2) } } @@ -240,7 +249,7 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, + E: ParserError<I>, { p: P, i: core::marker::PhantomData<I>, @@ -254,7 +263,7 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, + E: ParserError<I>, { pub(crate) fn new(p: P) -> Self { Self { @@ -272,17 +281,18 @@ where P: Parser<I, O, E>, I: Stream, O: crate::stream::ParseSlice<O2>, - E: ParseError<I>, -{ - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let input = i.clone(); - let (i, o) = self.p.parse_next(i)?; - - let res = o - .parse_slice() - .ok_or_else(|| ErrMode::from_error_kind(input, ErrorKind::Verify)); + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let start = i.checkpoint(); + let o = self.p.parse_next(i)?; + let res = o.parse_slice().ok_or_else(|| { + i.reset(start); + ErrMode::from_error_kind(i, ErrorKind::Verify) + }); trace_result("verify", &res); - Ok((i, res?)) + res } } @@ -328,8 +338,9 @@ where G: FnMut(O) -> H, H: Parser<I, O2, E>, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - let (i, o) = self.f.parse_next(i)?; + #[inline(always)] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + let o = self.f.parse_next(i)?; (self.g)(o).parse_next(i) } } @@ -350,14 +361,14 @@ impl<F, I, O, E> Parser<I, O, E> for CompleteErr<F> where I: Stream, F: Parser<I, O, E>, - E: ParseError<I>, + E: ParserError<I>, { - fn parse_next(&mut self, input: I) -> IResult<I, O, E> { - trace("complete_err", |input: I| { - let i = input.clone(); + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { + trace("complete_err", |input: &mut I| { match (self.f).parse_next(input) { Err(ErrMode::Incomplete(_)) => { - Err(ErrMode::from_error_kind(i, ErrorKind::Complete)) + Err(ErrMode::from_error_kind(input, ErrorKind::Complete)) } rest => rest, } @@ -372,10 +383,10 @@ pub struct Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, + E: ParserError<I>, { parser: F, filter: G, @@ -389,10 +400,10 @@ impl<F, G, I, O, O2, E> Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, + E: ParserError<I>, { pub(crate) fn new(parser: F, filter: G) -> Self { Self { @@ -410,20 +421,19 @@ impl<F, G, I, O, O2, E> Parser<I, O, E> for Verify<F, G, I, O, O2, E> where F: Parser<I, O, E>, G: Fn(&O2) -> bool, - I: Clone, + I: Stream, O: Borrow<O2>, O2: ?Sized, - E: ParseError<I>, -{ - fn parse_next(&mut self, input: I) -> IResult<I, O, E> { - let i = input.clone(); - let (input, o) = self.parser.parse_next(input)?; - - let res = if (self.filter)(o.borrow()) { - Ok((input, o)) - } else { - Err(ErrMode::from_error_kind(i, ErrorKind::Verify)) - }; + E: ParserError<I>, +{ + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { + let start = input.checkpoint(); + let o = self.parser.parse_next(input)?; + let res = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { + input.reset(start); + ErrMode::from_error_kind(input, ErrorKind::Verify) + }); trace_result("verify", &res); res } @@ -464,10 +474,9 @@ where F: Parser<I, O, E>, O2: Clone, { - fn parse_next(&mut self, input: I) -> IResult<I, O2, E> { - (self.parser) - .parse_next(input) - .map(|(i, _)| (i, self.val.clone())) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { + (self.parser).parse_next(input).map(|_| self.val.clone()) } } @@ -501,8 +510,9 @@ impl<F, I, O, E> Parser<I, (), E> for Void<F, I, O, E> where F: Parser<I, O, E>, { - fn parse_next(&mut self, input: I) -> IResult<I, (), E> { - (self.parser).parse_next(input).map(|(i, _)| (i, ())) + #[inline(always)] + fn parse_next(&mut self, input: &mut I) -> PResult<(), E> { + (self.parser).parse_next(input).map(|_| ()) } } @@ -511,7 +521,7 @@ where pub struct Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { parser: F, i: core::marker::PhantomData<I>, @@ -522,7 +532,7 @@ where impl<F, I, O, E> Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { pub(crate) fn new(parser: F) -> Self { Self { @@ -537,14 +547,17 @@ where impl<I, O, E, F> Parser<I, <I as Stream>::Slice, E> for Recognize<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { - fn parse_next(&mut self, input: I) -> IResult<I, <I as Stream>::Slice, E> { - let i = input.clone(); - match (self.parser).parse_next(i) { - Ok((i, _)) => { - let offset = input.offset_to(&i); - Ok(input.next_slice(offset)) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<<I as Stream>::Slice, E> { + let checkpoint = input.checkpoint(); + match (self.parser).parse_next(input) { + Ok(_) => { + let offset = input.offset_from(&checkpoint); + input.reset(checkpoint); + let recognized = input.next_slice(offset); + Ok(recognized) } Err(e) => Err(e), } @@ -556,7 +569,7 @@ where pub struct WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { parser: F, i: core::marker::PhantomData<I>, @@ -567,7 +580,7 @@ where impl<F, I, O, E> WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { pub(crate) fn new(parser: F) -> Self { Self { @@ -582,15 +595,17 @@ where impl<F, I, O, E> Parser<I, (O, <I as Stream>::Slice), E> for WithRecognized<F, I, O, E> where F: Parser<I, O, E>, - I: Stream + Offset, + I: Stream, { - fn parse_next(&mut self, input: I) -> IResult<I, (O, <I as Stream>::Slice), E> { - let i = input.clone(); - match (self.parser).parse_next(i) { - Ok((remaining, result)) => { - let offset = input.offset_to(&remaining); - let (remaining, recognized) = input.next_slice(offset); - Ok((remaining, (result, recognized))) + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<(O, <I as Stream>::Slice), E> { + let checkpoint = input.checkpoint(); + match (self.parser).parse_next(input) { + Ok(result) => { + let offset = input.offset_from(&checkpoint); + input.reset(checkpoint); + let recognized = input.next_slice(offset); + Ok((result, recognized)) } Err(e) => Err(e), } @@ -602,7 +617,7 @@ where pub struct Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { parser: F, i: core::marker::PhantomData<I>, @@ -613,7 +628,7 @@ where impl<F, I, O, E> Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { pub(crate) fn new(parser: F) -> Self { Self { @@ -628,13 +643,14 @@ where impl<I, O, E, F> Parser<I, Range<usize>, E> for Span<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { - fn parse_next(&mut self, input: I) -> IResult<I, Range<usize>, E> { + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<Range<usize>, E> { let start = input.location(); - self.parser.parse_next(input).map(move |(remaining, _)| { - let end = remaining.location(); - (remaining, (start..end)) + self.parser.parse_next(input).map(move |_| { + let end = input.location(); + start..end }) } } @@ -644,7 +660,7 @@ where pub struct WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { parser: F, i: core::marker::PhantomData<I>, @@ -655,7 +671,7 @@ where impl<F, I, O, E> WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { pub(crate) fn new(parser: F) -> Self { Self { @@ -670,16 +686,15 @@ where impl<F, I, O, E> Parser<I, (O, Range<usize>), E> for WithSpan<F, I, O, E> where F: Parser<I, O, E>, - I: Clone + Location, + I: Stream + Location, { - fn parse_next(&mut self, input: I) -> IResult<I, (O, Range<usize>), E> { + #[inline] + fn parse_next(&mut self, input: &mut I) -> PResult<(O, Range<usize>), E> { let start = input.location(); - self.parser - .parse_next(input) - .map(move |(remaining, output)| { - let end = remaining.location(); - (remaining, (output, (start..end))) - }) + self.parser.parse_next(input).map(move |output| { + let end = input.location(); + (output, (start..end)) + }) } } @@ -718,11 +733,9 @@ where F: Parser<I, O, E>, O: Into<O2>, { - fn parse_next(&mut self, i: I) -> IResult<I, O2, E> { - match self.parser.parse_next(i) { - Ok((i, o)) => Ok((i, o.into())), - Err(err) => Err(err), - } + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { + self.parser.parse_next(i).map(|o| o.into()) } } @@ -761,7 +774,8 @@ where F: Parser<I, O, E>, E: Into<E2>, { - fn parse_next(&mut self, i: I) -> IResult<I, O, E2> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E2> { match self.parser.parse_next(i) { Ok(ok) => Ok(ok), Err(ErrMode::Backtrack(e)) => Err(ErrMode::Backtrack(e.into())), @@ -777,7 +791,7 @@ pub struct Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { parser: F, @@ -791,7 +805,7 @@ impl<F, I, O, E, C> Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { pub(crate) fn new(parser: F, context: C) -> Self { @@ -809,17 +823,18 @@ impl<F, I, O, E, C> Parser<I, O, E> for Context<F, I, O, E, C> where F: Parser<I, O, E>, I: Stream, - E: ContextError<I, C>, + E: AddContext<I, C>, C: Clone + crate::lib::std::fmt::Debug, { - fn parse_next(&mut self, i: I) -> IResult<I, O, E> { + #[inline] + fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { #[cfg(feature = "debug")] let name = format!("context={:?}", self.context); #[cfg(not(feature = "debug"))] let name = "context"; - trace(name, move |i: I| { + trace(name, move |i: &mut I| { (self.parser) - .parse_next(i.clone()) + .parse_next(i) .map_err(|err| err.map(|err| err.add_context(i, self.context.clone()))) }) .parse_next(i) diff --git a/vendor/winnow/src/combinator/sequence.rs b/vendor/winnow/src/combinator/sequence.rs index 89c29a548..5cfeb9cb4 100644 --- a/vendor/winnow/src/combinator/sequence.rs +++ b/vendor/winnow/src/combinator/sequence.rs @@ -1,4 +1,4 @@ -use crate::error::ParseError; +use crate::error::ParserError; use crate::stream::Stream; use crate::trace::trace; use crate::*; @@ -12,7 +12,7 @@ use crate::*; /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::combinator::preceded; @@ -20,13 +20,13 @@ use crate::*; /// /// let mut parser = preceded("abc", "efg"); /// -/// assert_eq!(parser.parse_next("abcefg"), Ok(("", "efg"))); -/// assert_eq!(parser.parse_next("abcefghij"), Ok(("hij", "efg"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abcefg"), Ok(("", "efg"))); +/// assert_eq!(parser.parse_peek("abcefghij"), Ok(("hij", "efg"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "ignore_then")] -pub fn preceded<I, O1, O2, E: ParseError<I>, F, G>( +pub fn preceded<I, O1, O2, E: ParserError<I>, F, G>( mut first: F, mut second: G, ) -> impl Parser<I, O2, E> @@ -35,8 +35,8 @@ where F: Parser<I, O1, E>, G: Parser<I, O2, E>, { - trace("preceded", move |input: I| { - let (input, _) = first.parse_next(input)?; + trace("preceded", move |input: &mut I| { + let _ = first.parse_next(input)?; second.parse_next(input) }) } @@ -50,7 +50,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::error::Needed::Size; /// use winnow::combinator::terminated; @@ -58,13 +58,13 @@ where /// /// let mut parser = terminated("abc", "efg"); /// -/// assert_eq!(parser.parse_next("abcefg"), Ok(("", "abc"))); -/// assert_eq!(parser.parse_next("abcefghij"), Ok(("hij", "abc"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abcefg"), Ok(("", "abc"))); +/// assert_eq!(parser.parse_peek("abcefghij"), Ok(("hij", "abc"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "then_ignore")] -pub fn terminated<I, O1, O2, E: ParseError<I>, F, G>( +pub fn terminated<I, O1, O2, E: ParserError<I>, F, G>( mut first: F, mut second: G, ) -> impl Parser<I, O1, E> @@ -73,9 +73,9 @@ where F: Parser<I, O1, E>, G: Parser<I, O2, E>, { - trace("terminated", move |input: I| { - let (input, o1) = first.parse_next(input)?; - second.parse_next(input).map(|(i, _)| (i, o1)) + trace("terminated", move |input: &mut I| { + let o1 = first.parse_next(input)?; + second.parse_next(input).map(|_| o1) }) } @@ -89,7 +89,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// # use winnow::prelude::*; /// use winnow::combinator::separated_pair; @@ -97,12 +97,12 @@ where /// /// let mut parser = separated_pair("abc", "|", "efg"); /// -/// assert_eq!(parser.parse_next("abc|efg"), Ok(("", ("abc", "efg")))); -/// assert_eq!(parser.parse_next("abc|efghij"), Ok(("hij", ("abc", "efg")))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("abc|efg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser.parse_peek("abc|efghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` -pub fn separated_pair<I, O1, O2, O3, E: ParseError<I>, F, G, H>( +pub fn separated_pair<I, O1, O2, O3, E: ParserError<I>, F, G, H>( mut first: F, mut sep: G, mut second: H, @@ -113,10 +113,10 @@ where G: Parser<I, O2, E>, H: Parser<I, O3, E>, { - trace("separated_pair", move |input: I| { - let (input, o1) = first.parse_next(input)?; - let (input, _) = sep.parse_next(input)?; - second.parse_next(input).map(|(i, o2)| (i, (o1, o2))) + trace("separated_pair", move |input: &mut I| { + let o1 = first.parse_next(input)?; + let _ = sep.parse_next(input)?; + second.parse_next(input).map(|o2| (o1, o2)) }) } @@ -130,7 +130,7 @@ where /// # Example /// /// ```rust -/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed}; +/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed}; /// # use winnow::error::Needed::Size; /// # use winnow::prelude::*; /// use winnow::combinator::delimited; @@ -138,14 +138,14 @@ where /// /// let mut parser = delimited("(", "abc", ")"); /// -/// assert_eq!(parser.parse_next("(abc)"), Ok(("", "abc"))); -/// assert_eq!(parser.parse_next("(abc)def"), Ok(("def", "abc"))); -/// assert_eq!(parser.parse_next(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag)))); -/// assert_eq!(parser.parse_next("123"), Err(ErrMode::Backtrack(Error::new("123", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("(abc)"), Ok(("", "abc"))); +/// assert_eq!(parser.parse_peek("(abc)def"), Ok(("def", "abc"))); +/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag)))); +/// assert_eq!(parser.parse_peek("123"), Err(ErrMode::Backtrack(InputError::new("123", ErrorKind::Tag)))); /// ``` #[doc(alias = "between")] #[doc(alias = "padded")] -pub fn delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>( +pub fn delimited<I, O1, O2, O3, E: ParserError<I>, F, G, H>( mut first: F, mut second: G, mut third: H, @@ -156,9 +156,9 @@ where G: Parser<I, O2, E>, H: Parser<I, O3, E>, { - trace("delimited", move |input: I| { - let (input, _) = first.parse_next(input)?; - let (input, o2) = second.parse_next(input)?; - third.parse_next(input).map(|(i, _)| (i, o2)) + trace("delimited", move |input: &mut I| { + let _ = first.parse_next(input)?; + let o2 = second.parse_next(input)?; + third.parse_next(input).map(|_| o2) }) } diff --git a/vendor/winnow/src/combinator/tests.rs b/vendor/winnow/src/combinator/tests.rs index cdb787760..62dc420e5 100644 --- a/vendor/winnow/src/combinator/tests.rs +++ b/vendor/winnow/src/combinator/tests.rs @@ -5,11 +5,13 @@ use crate::binary::u16; use crate::binary::u8; use crate::binary::Endianness; use crate::error::ErrMode; -use crate::error::Error; use crate::error::ErrorKind; +use crate::error::InputError; use crate::error::Needed; -use crate::error::ParseError; +use crate::error::ParserError; +use crate::stream::Stream; use crate::token::take; +use crate::unpeek; use crate::IResult; use crate::Parser; use crate::Partial; @@ -19,7 +21,7 @@ use crate::lib::std::vec::Vec; macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::IResult<_, _, Error<_>> = $left; + let res: $crate::IResult<_, _, InputError<_>> = $left; assert_eq!(res, $right); }; ); @@ -29,16 +31,16 @@ fn eof_on_slices() { let not_over: &[u8] = &b"Hello, world!"[..]; let is_over: &[u8] = &b""[..]; - let res_not_over = eof(not_over); + let res_not_over = eof.parse_peek(not_over); assert_parse!( res_not_over, Err(ErrMode::Backtrack(error_position!( - not_over, + ¬_over, ErrorKind::Eof ))) ); - let res_over = eof(is_over); + let res_over = eof.parse_peek(is_over); assert_parse!(res_over, Ok((is_over, is_over))); } @@ -47,16 +49,16 @@ fn eof_on_strs() { let not_over: &str = "Hello, world!"; let is_over: &str = ""; - let res_not_over = eof(not_over); + let res_not_over = eof.parse_peek(not_over); assert_parse!( res_not_over, Err(ErrMode::Backtrack(error_position!( - not_over, + ¬_over, ErrorKind::Eof ))) ); - let res_over = eof(is_over); + let res_over = eof.parse_peek(is_over); assert_parse!(res_over, Ok((is_over, is_over))); } @@ -64,20 +66,20 @@ fn eof_on_strs() { fn rest_on_slices() { let input: &[u8] = &b"Hello, world!"[..]; let empty: &[u8] = &b""[..]; - assert_parse!(rest(input), Ok((empty, input))); + assert_parse!(rest.parse_peek(input), Ok((empty, input))); } #[test] fn rest_on_strs() { let input: &str = "Hello, world!"; let empty: &str = ""; - assert_parse!(rest(input), Ok((empty, input))); + assert_parse!(rest.parse_peek(input), Ok((empty, input))); } #[test] fn rest_len_on_slices() { let input: &[u8] = &b"Hello, world!"[..]; - assert_parse!(rest_len(input), Ok((input, input.len()))); + assert_parse!(rest_len.parse_peek(input), Ok((input, input.len()))); } use crate::lib::std::convert::From; @@ -87,12 +89,12 @@ impl From<u32> for CustomError { } } -impl<I> ParseError<I> for CustomError { - fn from_error_kind(_: I, _: ErrorKind) -> Self { +impl<I> ParserError<I> for CustomError { + fn from_error_kind(_: &I, _: ErrorKind) -> Self { CustomError } - fn append(self, _: I, _: ErrorKind) -> Self { + fn append(self, _: &I, _: ErrorKind) -> Self { CustomError } } @@ -101,14 +103,14 @@ struct CustomError; #[allow(dead_code)] fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError> { //fix_error!(input, CustomError<_>, alphanumeric) - crate::ascii::alphanumeric1(input) + crate::ascii::alphanumeric1.parse_peek(input) } #[test] fn test_parser_flat_map() { let input: &[u8] = &[3, 100, 101, 102, 103, 104][..]; assert_parse!( - u8.flat_map(take).parse_next(input), + u8.flat_map(take).parse_peek(input), Ok((&[103, 104][..], &[100, 101, 102][..])) ); } @@ -116,7 +118,7 @@ fn test_parser_flat_map() { #[allow(dead_code)] fn test_closure_compiles_195(input: &[u8]) -> IResult<&[u8], ()> { u8.flat_map(|num| repeat(num as usize, u16(Endianness::Big))) - .parse_next(input) + .parse_peek(input) } #[test] @@ -124,15 +126,15 @@ fn test_parser_verify_map() { let input: &[u8] = &[50][..]; assert_parse!( u8.verify_map(|u| if u < 20 { Some(u) } else { None }) - .parse_next(input), - Err(ErrMode::Backtrack(Error { - input: &[50][..], - kind: ErrorKind::Verify - })) + .parse_peek(input), + Err(ErrMode::Backtrack(InputError::new( + &[50][..], + ErrorKind::Verify + ))) ); assert_parse!( u8.verify_map(|u| if u > 20 { Some(u) } else { None }) - .parse_next(input), + .parse_peek(input), Ok((&[][..], 50)) ); } @@ -141,7 +143,7 @@ fn test_parser_verify_map() { fn test_parser_map_parser() { let input: &[u8] = &[100, 101, 102, 103, 104][..]; assert_parse!( - take(4usize).and_then(take(2usize)).parse_next(input), + take(4usize).and_then(take(2usize)).parse_peek(input), Ok((&[104][..], &[100, 101][..])) ); } @@ -149,11 +151,11 @@ fn test_parser_map_parser() { #[test] #[cfg(feature = "std")] fn test_parser_into() { - use crate::error::Error; + use crate::error::InputError; use crate::token::take; - let mut parser = take::<_, _, Error<_>>(3u8).output_into(); - let result: IResult<&[u8], Vec<u8>> = parser.parse_next(&b"abcdefg"[..]); + let mut parser = take::<_, _, InputError<_>>(3u8).output_into(); + let result: IResult<&[u8], Vec<u8>> = parser.parse_peek(&b"abcdefg"[..]); assert_eq!(result, Ok((&b"defg"[..], vec![97, 98, 99]))); } @@ -161,7 +163,7 @@ fn test_parser_into() { #[test] fn opt_test() { fn opt_abcd(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Option<&[u8]>> { - opt("abcd").parse_next(i) + opt("abcd").parse_peek(i) } let a = &b"abcdef"[..]; @@ -184,7 +186,7 @@ fn opt_test() { #[test] fn peek_test() { fn peek_tag(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - peek("abcd").parse_next(i) + peek("abcd").parse_peek(i) } assert_eq!( @@ -198,7 +200,7 @@ fn peek_test() { assert_eq!( peek_tag(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -207,13 +209,13 @@ fn peek_test() { #[test] fn not_test() { fn not_aaa(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, ()> { - not("aaa").parse_next(i) + not("aaa").parse_peek(i) } assert_eq!( not_aaa(Partial::new(&b"aaa"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"aaa"[..]), + &Partial::new(&b"aaa"[..]), ErrorKind::Not ))) ); @@ -234,7 +236,7 @@ fn test_parser_verify() { fn test(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { take(5u8) .verify(|slice: &[u8]| slice[0] == b'a') - .parse_next(i) + .parse_peek(i) } assert_eq!( test(Partial::new(&b"bcd"[..])), @@ -243,7 +245,7 @@ fn test_parser_verify() { assert_eq!( test(Partial::new(&b"bcdefg"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"bcdefg"[..]), + &Partial::new(&b"bcdefg"[..]), ErrorKind::Verify ))) ); @@ -261,21 +263,21 @@ fn test_parser_verify_ref() { let mut parser1 = take(3u8).verify(|s: &[u8]| s == &b"abc"[..]); assert_eq!( - parser1.parse_next(&b"abcd"[..]), + parser1.parse_peek(&b"abcd"[..]), Ok((&b"d"[..], &b"abc"[..])) ); assert_eq!( - parser1.parse_next(&b"defg"[..]), - Err(ErrMode::Backtrack(Error { - input: &b"defg"[..], - kind: ErrorKind::Verify - })) + parser1.parse_peek(&b"defg"[..]), + Err(ErrMode::Backtrack(InputError::new( + &b"defg"[..], + ErrorKind::Verify + ))) ); fn parser2(i: &[u8]) -> IResult<&[u8], u32> { crate::binary::be_u32 .verify(|val: &u32| *val < 3) - .parse_next(i) + .parse_peek(i) } } @@ -288,15 +290,15 @@ fn test_parser_verify_alloc() { .verify(|s: &[u8]| s == &b"abc"[..]); assert_eq!( - parser1.parse_next(&b"abcd"[..]), + parser1.parse_peek(&b"abcd"[..]), Ok((&b"d"[..], b"abc".to_vec())) ); assert_eq!( - parser1.parse_next(&b"defg"[..]), - Err(ErrMode::Backtrack(Error { - input: &b"defg"[..], - kind: ErrorKind::Verify - })) + parser1.parse_peek(&b"defg"[..]), + Err(ErrMode::Backtrack(InputError::new( + &b"defg"[..], + ErrorKind::Verify + ))) ); } @@ -306,26 +308,20 @@ fn fail_test() { let b = "another string"; assert_eq!( - fail::<_, &str, _>(a), - Err(ErrMode::Backtrack(Error { - input: a, - kind: ErrorKind::Fail - })) + fail::<_, &str, _>.parse_peek(a), + Err(ErrMode::Backtrack(InputError::new(a, ErrorKind::Fail))) ); assert_eq!( - fail::<_, &str, _>(b), - Err(ErrMode::Backtrack(Error { - input: b, - kind: ErrorKind::Fail - })) + fail::<_, &str, _>.parse_peek(b), + Err(ErrMode::Backtrack(InputError::new(b, ErrorKind::Fail))) ); } #[test] fn complete() { fn err_test(i: &[u8]) -> IResult<&[u8], &[u8]> { - let (i, _) = "ijkl".parse_next(i)?; - "mnop".parse_next(i) + let (i, _) = "ijkl".parse_peek(i)?; + "mnop".parse_peek(i) } let a = &b"ijklmn"[..]; @@ -333,7 +329,7 @@ fn complete() { assert_eq!( res_a, Err(ErrMode::Backtrack(error_position!( - &b"mn"[..], + &&b"mn"[..], ErrorKind::Tag ))) ); @@ -343,7 +339,7 @@ fn complete() { fn separated_pair_test() { #[allow(clippy::type_complexity)] fn sep_pair_abc_def(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (&[u8], &[u8])> { - separated_pair("abc", ",", "def").parse_next(i) + separated_pair("abc", ",", "def").parse_peek(i) } assert_eq!( @@ -361,21 +357,21 @@ fn separated_pair_test() { assert_eq!( sep_pair_abc_def(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( sep_pair_abc_def(Partial::new(&b"xxx,def"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx,def"[..]), + &Partial::new(&b"xxx,def"[..]), ErrorKind::Tag ))) ); assert_eq!( sep_pair_abc_def(Partial::new(&b"abc,xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -384,7 +380,7 @@ fn separated_pair_test() { #[test] fn preceded_test() { fn preceded_abcd_efgh(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - preceded("abcd", "efgh").parse_next(i) + preceded("abcd", "efgh").parse_peek(i) } assert_eq!( @@ -402,21 +398,21 @@ fn preceded_test() { assert_eq!( preceded_abcd_efgh(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( preceded_abcd_efgh(Partial::new(&b"xxxxdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxxdef"[..]), + &Partial::new(&b"xxxxdef"[..]), ErrorKind::Tag ))) ); assert_eq!( preceded_abcd_efgh(Partial::new(&b"abcdxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -425,7 +421,7 @@ fn preceded_test() { #[test] fn terminated_test() { fn terminated_abcd_efgh(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - terminated("abcd", "efgh").parse_next(i) + terminated("abcd", "efgh").parse_peek(i) } assert_eq!( @@ -443,21 +439,21 @@ fn terminated_test() { assert_eq!( terminated_abcd_efgh(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( terminated_abcd_efgh(Partial::new(&b"xxxxdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxxdef"[..]), + &Partial::new(&b"xxxxdef"[..]), ErrorKind::Tag ))) ); assert_eq!( terminated_abcd_efgh(Partial::new(&b"abcdxxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxx"[..]), + &Partial::new(&b"xxxx"[..]), ErrorKind::Tag ))) ); @@ -466,7 +462,7 @@ fn terminated_test() { #[test] fn delimited_test() { fn delimited_abc_def_ghi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - delimited("abc", "def", "ghi").parse_next(i) + delimited("abc", "def", "ghi").parse_peek(i) } assert_eq!( @@ -488,28 +484,28 @@ fn delimited_test() { assert_eq!( delimited_abc_def_ghi(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"xxxdefghi"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxdefghi"[..]), + &Partial::new(&b"xxxdefghi"[..]), ErrorKind::Tag ),)) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"abcxxxghi"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxghi"[..]), + &Partial::new(&b"xxxghi"[..]), ErrorKind::Tag ))) ); assert_eq!( delimited_abc_def_ghi(Partial::new(&b"abcdefxxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); @@ -520,7 +516,7 @@ fn delimited_test() { fn alt_test() { #[cfg(feature = "alloc")] use crate::{ - error::ParseError, + error::ParserError, lib::std::{ fmt::Debug, string::{String, ToString}, @@ -546,12 +542,12 @@ fn alt_test() { } #[cfg(feature = "alloc")] - impl<I: Debug> ParseError<I> for ErrorStr { - fn from_error_kind(input: I, kind: ErrorKind) -> Self { + impl<I: Debug> ParserError<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 { + fn append(self, input: &I, kind: ErrorKind) -> Self { ErrorStr(format!( "custom error message: ({:?}, {:?}) - {:?}", input, kind, self @@ -560,7 +556,7 @@ fn alt_test() { } fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - Ok((&b""[..], input)) + Ok(input.peek_finish()) } #[allow(unused_variables)] @@ -573,13 +569,19 @@ fn alt_test() { } fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work)).parse_next(i) + alt((unpeek(dont_work), unpeek(dont_work))).parse_peek(i) } fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, work)).parse_next(i) + alt((unpeek(dont_work), unpeek(work))).parse_peek(i) } fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { - alt((dont_work, dont_work, work2, dont_work)).parse_next(i) + alt(( + unpeek(dont_work), + unpeek(dont_work), + unpeek(work2), + unpeek(dont_work), + )) + .parse_peek(i) } //named!(alt1, alt!(dont_work | dont_work)); //named!(alt2, alt!(dont_work | work)); @@ -589,7 +591,7 @@ fn alt_test() { assert_eq!( alt1(a), Err(ErrMode::Backtrack(error_node_position!( - a, + &a, ErrorKind::Alt, ErrorStr("abcd".to_string()) ))) @@ -598,7 +600,7 @@ fn alt_test() { assert_eq!(alt3(a), Ok((a, &b""[..]))); fn alt4(i: &[u8]) -> IResult<&[u8], &[u8]> { - alt(("abcd", "efgh")).parse_next(i) + alt(("abcd", "efgh")).parse_peek(i) } let b = &b"efgh"[..]; assert_eq!(alt4(a), Ok((&b""[..], a))); @@ -608,7 +610,7 @@ fn alt_test() { #[test] fn alt_incomplete() { fn alt1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { - alt(("a", "bc", "def")).parse_next(i) + alt(("a", "bc", "def")).parse_peek(i) } let a = &b""[..]; @@ -630,7 +632,7 @@ fn alt_incomplete() { assert_eq!( alt1(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(a), + &Partial::new(a), ErrorKind::Tag ))) ); @@ -650,7 +652,7 @@ fn alt_incomplete() { 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) + permutation(("abcd", "efg", "hi")).parse_peek(i) } let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); @@ -675,9 +677,9 @@ fn permutation_test() { assert_eq!( perm(Partial::new(d)), Err(ErrMode::Backtrack(error_node_position!( - Partial::new(&b"efgxyzabcdefghi"[..]), + &Partial::new(&b"efgxyzabcdefghi"[..]), ErrorKind::Alt, - error_position!(Partial::new(&b"xyzabcdefghi"[..]), ErrorKind::Tag) + error_position!(&Partial::new(&b"xyzabcdefghi"[..]), ErrorKind::Tag) ))) ); @@ -692,13 +694,13 @@ fn permutation_test() { #[cfg(feature = "alloc")] fn separated0_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abcd", ",").parse_next(i) + separated0("abcd", ",").parse_peek(i) } fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("", ",").parse_next(i) + separated0("", ",").parse_peek(i) } fn multi_longsep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abcd", "..").parse_next(i) + separated0("abcd", "..").parse_peek(i) } let a = &b"abcdef"[..]; @@ -748,7 +750,7 @@ fn separated0_test() { #[cfg_attr(debug_assertions, should_panic)] fn separated0_empty_sep_test() { fn empty_sep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated0("abc", "").parse_next(i) + separated0("abc", "").parse_peek(i) } let i = &b"abcabc"[..]; @@ -757,7 +759,7 @@ fn separated0_empty_sep_test() { assert_eq!( empty_sep(Partial::new(i)), Err(ErrMode::Backtrack(error_position!( - Partial::new(i_err_pos), + &Partial::new(i_err_pos), ErrorKind::Assert ))) ); @@ -767,10 +769,10 @@ fn separated0_empty_sep_test() { #[cfg(feature = "alloc")] fn separated1_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated1("abcd", ",").parse_next(i) + separated1("abcd", ",").parse_peek(i) } fn multi_longsep(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - separated1("abcd", "..").parse_next(i) + separated1("abcd", "..").parse_peek(i) } let a = &b"abcdef"[..]; @@ -789,7 +791,7 @@ fn separated1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Tag ))) ); @@ -817,7 +819,7 @@ fn separated1_test() { #[cfg(feature = "alloc")] fn repeat0_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(0.., "abcd").parse_next(i) + repeat(0.., "abcd").parse_peek(i) } assert_eq!( @@ -851,13 +853,13 @@ fn repeat0_test() { #[cfg_attr(debug_assertions, should_panic)] fn repeat0_empty_test() { fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(0.., "").parse_next(i) + repeat(0.., "").parse_peek(i) } assert_eq!( multi_empty(Partial::new(&b"abcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"abcdef"[..]), + &Partial::new(&b"abcdef"[..]), ErrorKind::Assert ))) ); @@ -867,7 +869,7 @@ fn repeat0_empty_test() { #[cfg(feature = "alloc")] fn repeat1_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(1.., "abcd").parse_next(i) + repeat(1.., "abcd").parse_peek(i) } let a = &b"abcdef"[..]; @@ -885,7 +887,7 @@ fn repeat1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Tag ))) ); @@ -900,7 +902,7 @@ fn repeat1_test() { fn repeat_till_test() { #[allow(clippy::type_complexity)] fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> { - repeat_till0("abcd", "efgh").parse_next(i) + repeat_till0("abcd", "efgh").parse_peek(i) } let a = b"abcdabcdefghabcd"; @@ -914,9 +916,9 @@ fn repeat_till_test() { assert_eq!( multi(&c[..]), Err(ErrMode::Backtrack(error_node_position!( - &c[..], + &&c[..], ErrorKind::Many, - error_position!(&c[..], ErrorKind::Tag) + error_position!(&&c[..], ErrorKind::Tag) ))) ); } @@ -926,23 +928,23 @@ fn repeat_till_test() { fn infinite_many() { fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { println!("input: {:?}", input); - Err(ErrMode::Backtrack(error_position!(input, ErrorKind::Tag))) + Err(ErrMode::Backtrack(error_position!(&input, ErrorKind::Tag))) } // should not go into an infinite loop fn multi0(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(0.., tst).parse_next(i) + repeat(0.., unpeek(tst)).parse_peek(i) } let a = &b"abcdef"[..]; assert_eq!(multi0(a), Ok((a, Vec::new()))); fn multi1(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(1.., tst).parse_next(i) + repeat(1.., unpeek(tst)).parse_peek(i) } let a = &b"abcdef"[..]; assert_eq!( multi1(a), - Err(ErrMode::Backtrack(error_position!(a, ErrorKind::Tag))) + Err(ErrMode::Backtrack(error_position!(&a, ErrorKind::Tag))) ); } @@ -950,7 +952,7 @@ fn infinite_many() { #[cfg(feature = "alloc")] fn repeat_test() { fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(2..=4, "Abcd").parse_next(i) + repeat(2..=4, "Abcd").parse_peek(i) } let a = &b"Abcdef"[..]; @@ -962,7 +964,7 @@ fn repeat_test() { assert_eq!( multi(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ef"[..]), + &Partial::new(&b"ef"[..]), ErrorKind::Tag ))) ); @@ -992,7 +994,7 @@ fn repeat_test() { fn count_test() { const TIMES: usize = 2; fn cnt_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - repeat(TIMES, "abc").parse_next(i) + repeat(TIMES, "abc").parse_peek(i) } assert_eq!( @@ -1010,21 +1012,21 @@ fn count_test() { assert_eq!( cnt_2(Partial::new(&b"xxx"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxx"[..]), + &Partial::new(&b"xxx"[..]), ErrorKind::Tag ))) ); assert_eq!( cnt_2(Partial::new(&b"xxxabcabcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxabcabcdef"[..]), + &Partial::new(&b"xxxabcabcdef"[..]), ErrorKind::Tag ))) ); assert_eq!( cnt_2(Partial::new(&b"abcxxxabcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"xxxabcdef"[..]), + &Partial::new(&b"xxxabcdef"[..]), ErrorKind::Tag ))) ); @@ -1035,7 +1037,7 @@ fn count_test() { fn count_zero() { const TIMES: usize = 0; fn counter_2(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - repeat(TIMES, "abc").parse_next(i) + repeat(TIMES, "abc").parse_peek(i) } let done = &b"abcabcabcdef"[..]; @@ -1078,11 +1080,11 @@ impl<I> From<(I, ErrorKind)> for NilError { } } -impl<I> ParseError<I> for NilError { - fn from_error_kind(_: I, _: ErrorKind) -> NilError { +impl<I> ParserError<I> for NilError { + fn from_error_kind(_: &I, _: ErrorKind) -> NilError { NilError } - fn append(self, _: I, _: ErrorKind) -> NilError { + fn append(self, _: &I, _: ErrorKind) -> NilError { NilError } } @@ -1095,7 +1097,7 @@ fn fold_repeat0_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(0.., "abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(0.., "abcd", Vec::new, fold_into_vec).parse_peek(i) } assert_eq!( @@ -1133,13 +1135,13 @@ fn fold_repeat0_empty_test() { acc } fn multi_empty(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(0.., "", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(0.., "", Vec::new, fold_into_vec).parse_peek(i) } assert_eq!( multi_empty(Partial::new(&b"abcdef"[..])), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"abcdef"[..]), + &Partial::new(&b"abcdef"[..]), ErrorKind::Assert ))) ); @@ -1153,7 +1155,7 @@ fn fold_repeat1_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(1.., "abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(1.., "abcd", Vec::new, fold_into_vec).parse_peek(i) } let a = &b"abcdef"[..]; @@ -1171,7 +1173,7 @@ fn fold_repeat1_test() { assert_eq!( multi(Partial::new(c)), Err(ErrMode::Backtrack(error_position!( - Partial::new(c), + &Partial::new(c), ErrorKind::Many ))) ); @@ -1189,7 +1191,7 @@ fn fold_repeat_test() { acc } fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { - fold_repeat(2..=4, "Abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat(2..=4, "Abcd", Vec::new, fold_into_vec).parse_peek(i) } let a = &b"Abcdef"[..]; @@ -1201,7 +1203,7 @@ fn fold_repeat_test() { assert_eq!( multi(Partial::new(a)), Err(ErrMode::Backtrack(error_position!( - Partial::new(&b"ef"[..]), + &Partial::new(&b"ef"[..]), ErrorKind::Tag ))) ); @@ -1229,7 +1231,7 @@ fn fold_repeat_test() { #[test] fn repeat0_count_test() { fn count0_nums(i: &[u8]) -> IResult<&[u8], usize> { - repeat(0.., (digit, ",")).parse_next(i) + repeat(0.., (digit, ",")).parse_peek(i) } assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); @@ -1247,7 +1249,7 @@ fn repeat0_count_test() { #[test] fn repeat1_count_test() { fn count1_nums(i: &[u8]) -> IResult<&[u8], usize> { - repeat(1.., (digit, ",")).parse_next(i) + repeat(1.., (digit, ",")).parse_peek(i) } assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); @@ -1260,7 +1262,7 @@ fn repeat1_count_test() { assert_eq!( count1_nums(&b"hello"[..]), Err(ErrMode::Backtrack(error_position!( - &b"hello"[..], + &&b"hello"[..], ErrorKind::Slice ))) ); |