diff --git a/benchmarks/benches/json.rs b/benchmarks/benches/json.rs index 3d4f44a74..6eda885ac 100644 --- a/benchmarks/benches/json.rs +++ b/benchmarks/benches/json.rs @@ -95,7 +95,9 @@ fn string(input: &str) -> IResult<&str, String> { )(input) } -fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, O, E>>(f: F) -> impl Parser<&'a str, O, E> { +fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, Output = O, Error = E>>( + f: F, +) -> impl Parser<&'a str, Output = O, Error = E> { delimited(multispace0, f, multispace0) } diff --git a/benchmarks/benches/json_streaming.rs b/benchmarks/benches/json_streaming.rs index 57cc618f3..36b33d85c 100644 --- a/benchmarks/benches/json_streaming.rs +++ b/benchmarks/benches/json_streaming.rs @@ -95,7 +95,9 @@ fn string(input: &str) -> IResult<&str, String> { )(input) } -fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, O, E>>(f: F) -> impl Parser<&'a str, O, E> { +fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, Output = O, Error = E>>( + f: F, +) -> impl Parser<&'a str, Output = O, Error = E> { delimited(multispace0, f, multispace0) } diff --git a/examples/s_expression.rs b/examples/s_expression.rs index be2398437..65b6b6df6 100644 --- a/examples/s_expression.rs +++ b/examples/s_expression.rs @@ -154,7 +154,7 @@ fn parse_constant(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { /// takes a parsing function and returns a new parsing function. fn s_exp<'a, O1, F>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O1, VerboseError<&'a str>> where - F: Parser<&'a str, O1, VerboseError<&'a str>>, + F: Parser<&'a str, Output = O1, Error = VerboseError<&'a str>>, { delimited( char('('), diff --git a/src/bits/mod.rs b/src/bits/mod.rs index 7f59b1399..f120cb353 100644 --- a/src/bits/mod.rs +++ b/src/bits/mod.rs @@ -41,7 +41,7 @@ where E1: ParseError<(I, usize)> + ErrorConvert, E2: ParseError, I: Input, - P: Parser<(I, usize), O, E1>, + P: Parser<(I, usize), Output = O, Error = E1>, { move |input: I| match parser.parse((input, 0)) { Ok(((rest, offset), result)) => { @@ -86,7 +86,7 @@ where E1: ParseError + ErrorConvert, E2: ParseError<(I, usize)>, I: Input + Clone, - P: Parser, + P: Parser, { move |(input, offset): (I, usize)| { let inner = if offset % 8 != 0 { diff --git a/src/branch/mod.rs b/src/branch/mod.rs index e03622cb0..7011ea50f 100644 --- a/src/branch/mod.rs +++ b/src/branch/mod.rs @@ -128,7 +128,7 @@ macro_rules! alt_trait_impl( ($($id:ident)+) => ( impl< Input: Clone, Output, Error: ParseError, - $($id: Parser),+ + $($id: Parser),+ > Alt for ( $($id),+ ) { fn choice(&mut self, input: Input) -> IResult { @@ -159,7 +159,7 @@ macro_rules! alt_trait_inner( alt_trait!(A B C D E F G H I J K L M N O P Q R S T U); // Manually implement Alt for (A,), the 1-tuple type -impl, A: Parser> +impl, A: Parser> Alt for (A,) { fn choice(&mut self, input: Input) -> IResult { @@ -191,7 +191,7 @@ macro_rules! permutation_trait_impl( ($($name:ident $ty:ident $item:ident),+) => ( impl< Input: Clone, $($ty),+ , Error: ParseError, - $($name: Parser),+ + $($name: Parser),+ > Permutation for ( $($name),+ ) { fn permutation(&mut self, mut input: Input) -> IResult { diff --git a/src/bytes/complete.rs b/src/bytes/complete.rs index 00a76e544..d70cddb06 100644 --- a/src/bytes/complete.rs +++ b/src/bytes/complete.rs @@ -483,7 +483,7 @@ where /// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#))); /// ``` /// -pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>( +pub fn escaped<'a, I: 'a, Error, F, G>( mut normal: F, control_char: char, mut escapable: G, @@ -491,8 +491,8 @@ pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>( where I: Clone + crate::traits::Offset + Input, ::Item: crate::traits::AsChar, - F: Parser, - G: Parser, + F: Parser, + G: Parser, Error: ParseError, { use crate::traits::AsChar; @@ -602,8 +602,8 @@ where O1: crate::traits::ExtendInto, O2: crate::traits::ExtendInto, ::Item: crate::traits::AsChar, - F: Parser, - G: Parser, + F: Parser, + G: Parser, Error: ParseError, { use crate::traits::AsChar; diff --git a/src/bytes/streaming.rs b/src/bytes/streaming.rs index 465860ba1..00bf3f228 100644 --- a/src/bytes/streaming.rs +++ b/src/bytes/streaming.rs @@ -499,7 +499,7 @@ where /// assert_eq!(esc("12\\\"34;"), Ok((";", "12\\\"34"))); /// ``` /// -pub fn escaped( +pub fn escaped( mut normal: F, control_char: char, mut escapable: G, @@ -507,8 +507,8 @@ pub fn escaped( where I: Input + Clone + crate::traits::Offset, ::Item: crate::traits::AsChar, - F: Parser, - G: Parser, + F: Parser, + G: Parser, Error: ParseError, { use crate::traits::AsChar; @@ -606,8 +606,8 @@ where O1: crate::traits::ExtendInto, O2: crate::traits::ExtendInto, ::Item: crate::traits::AsChar, - F: Parser, - G: Parser, + F: Parser, + G: Parser, Error: ParseError, { use crate::traits::AsChar; diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index 8b43b91dc..4810568e3 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -69,10 +69,10 @@ where /// assert_eq!(parser.parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); /// # } /// ``` -pub fn map(mut parser: F, mut f: G) -> impl FnMut(I) -> IResult +pub fn map(mut parser: F, mut f: G) -> impl FnMut(I) -> IResult where - F: Parser, - G: FnMut(O1) -> O2, + F: Parser, + G: FnMut(>::Output) -> O, { move |input: I| { let (input, o1) = parser.parse(input)?; @@ -100,13 +100,13 @@ where /// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapRes)))); /// # } /// ``` -pub fn map_res, E2, F, G>( +pub fn map_res, E2, F, G>( mut parser: F, mut f: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: FnMut(O1) -> Result, + F: Parser, + G: FnMut(>::Output) -> Result, { move |input: I| { let i = input.clone(); @@ -138,13 +138,13 @@ where /// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); /// # } /// ``` -pub fn map_opt, F, G>( +pub fn map_opt, F, G>( mut parser: F, mut f: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: FnMut(O1) -> Option, + F: Parser, + G: FnMut(>::Output) -> Option, { move |input: I| { let i = input.clone(); @@ -172,13 +172,13 @@ where /// assert_eq!(parse("123"), Err(Err::Error(("123", ErrorKind::Eof)))); /// # } /// ``` -pub fn map_parser, F, G>( +pub fn map_parser, F, G>( mut parser: F, mut applied_parser: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, + F: Parser, + G: Parser<>::Output, Output = O, Error = E>, { move |input: I| { let (input, o1) = parser.parse(input)?; @@ -202,14 +202,14 @@ where /// assert_eq!(parse(&[4, 0, 1, 2][..]), Err(Err::Error((&[0, 1, 2][..], ErrorKind::Eof)))); /// # } /// ``` -pub fn flat_map, F, G, H>( +pub fn flat_map, F, G, H>( mut parser: F, mut applied_parser: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: FnMut(O1) -> H, - H: Parser, + F: Parser, + G: FnMut(>::Output) -> H, + H: Parser, { move |input: I| { let (input, o1) = parser.parse(input)?; @@ -235,9 +235,11 @@ where /// assert_eq!(parser("123;"), Ok(("123;", None))); /// # } /// ``` -pub fn opt, F>(mut f: F) -> impl FnMut(I) -> IResult, E> +pub fn opt, F>( + mut f: F, +) -> impl FnMut(I) -> IResult>::Output>, E> where - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -267,12 +269,12 @@ where /// assert_eq!(parser(false, "123;"), Ok(("123;", None))); /// # } /// ``` -pub fn cond, F>( +pub fn cond, F>( b: bool, mut f: F, -) -> impl FnMut(I) -> IResult, E> +) -> impl FnMut(I) -> IResult>::Output>, E> where - F: Parser, + F: Parser, { move |input: I| { if b { @@ -300,9 +302,11 @@ where /// assert_eq!(parser("123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); /// # } /// ``` -pub fn peek, F>(mut f: F) -> impl FnMut(I) -> IResult +pub fn peek, F>( + mut f: F, +) -> impl FnMut(I) -> IResult>::Output, E> where - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -354,7 +358,7 @@ pub fn eof>(input: I) -> IResult, F>(mut f: F) -> impl FnMut(I) -> IResult where - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -380,10 +384,12 @@ where /// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); /// # } /// ``` -pub fn all_consuming, F>(mut f: F) -> impl FnMut(I) -> IResult +pub fn all_consuming, F>( + mut f: F, +) -> impl FnMut(I) -> IResult>::Output, E> where I: InputLength, - F: Parser, + F: Parser, { move |input: I| { let (input, res) = f.parse(input)?; @@ -413,14 +419,14 @@ where /// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); /// # } /// ``` -pub fn verify, F, G>( +pub fn verify, F, G>( mut first: F, second: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult>::Output, E> where - F: Parser, + F: Parser, G: Fn(&O2) -> bool, - O1: Borrow, + >::Output: Borrow, O2: ?Sized, { move |input: I| { @@ -449,12 +455,12 @@ where /// assert_eq!(parser("123abcd;"), Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); /// # } /// ``` -pub fn value, F>( +pub fn value, F>( val: O1, mut parser: F, ) -> impl FnMut(I) -> IResult where - F: Parser, + F: Parser, { move |input: I| parser.parse(input).map(|(i, _)| (i, val.clone())) } @@ -473,9 +479,9 @@ where /// assert_eq!(parser("abcd"), Err(Err::Error(("abcd", ErrorKind::Not)))); /// # } /// ``` -pub fn not, F>(mut parser: F) -> impl FnMut(I) -> IResult +pub fn not, F>(mut parser: F) -> impl FnMut(I) -> IResult where - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -502,11 +508,11 @@ where /// assert_eq!(parser("abcd;"),Err(Err::Error((";", ErrorKind::Char)))); /// # } /// ``` -pub fn recognize, F>( +pub fn recognize, F>( mut parser: F, ) -> impl FnMut(I) -> IResult where - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -557,11 +563,13 @@ where /// assert_eq!(recognize_parser("abcd"), consumed_parser("abcd")); /// # } /// ``` -pub fn consumed(mut parser: F) -> impl FnMut(I) -> IResult +pub fn consumed( + mut parser: F, +) -> impl FnMut(I) -> IResult>::Output), E> where I: Clone + Offset + Input, E: ParseError, - F: Parser, + F: Parser, { move |input: I| { let i = input.clone(); @@ -627,9 +635,11 @@ where /// assert_eq!(parser("+"), Err(Err::Failure(Error { input: "", code: ErrorKind::Digit }))); /// # } /// ``` -pub fn cut, F>(mut parser: F) -> impl FnMut(I) -> IResult +pub fn cut, F>( + mut parser: F, +) -> impl FnMut(I) -> IResult>::Output, E> where - F: Parser, + F: Parser, { move |input: I| match parser.parse(input) { Err(Err::Error(e)) => Err(Err::Failure(e)), @@ -665,7 +675,7 @@ where E1: Into, E1: ParseError, E2: ParseError, - F: Parser, + F: Parser, { //map(parser, Into::into) move |input: I| match parser.parse(input) { @@ -696,9 +706,9 @@ where /// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect()); /// assert_eq!(res, Ok(("123", ()))); /// ``` -pub fn iterator(input: Input, f: F) -> ParserIterator +pub fn iterator(input: Input, f: F) -> ParserIterator where - F: Parser, + F: Parser, Error: ParseError, { ParserIterator { diff --git a/src/error.rs b/src/error.rs index cd9a0273c..4a1193315 100644 --- a/src/error.rs +++ b/src/error.rs @@ -235,7 +235,7 @@ pub fn context, F, O>( mut f: F, ) -> impl FnMut(I) -> IResult where - F: Parser, + F: Parser, { move |i: I| match f.parse(i.clone()) { Ok(o) => Ok(o), diff --git a/src/internal.rs b/src/internal.rs index 4ad258c3d..deadd7e07 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -234,82 +234,67 @@ where } /// All nom parsers implement this trait -pub trait Parser { +pub trait Parser { + /// Type of the produced value + type Output; + /// Error type of this parser + type Error: ParseError; + /// A parser takes in input type, and returns a `Result` containing /// either the remaining input and the output value, or an error - fn parse(&mut self, input: I) -> IResult; + fn parse(&mut self, input: Input) -> IResult; /// Maps a function over the result of a parser - fn map(self, g: G) -> Map + fn map(self, g: G) -> Map where - G: FnMut(O) -> O2, + G: FnMut(Self::Output) -> O2, Self: core::marker::Sized, { - Map { - f: self, - g, - phantom: core::marker::PhantomData, - } + Map { f: self, g } } /// Applies a function returning a `Result` over the result of a parser. - fn map_res(self, g: G) -> MapRes + fn map_res(self, g: G) -> MapRes where - G: Fn(O) -> Result, - E: FromExternalError, + G: Fn(Self::Output) -> Result, + Self::Error: FromExternalError, Self: core::marker::Sized, { - MapRes { - f: self, - g, - phantom: core::marker::PhantomData, - } + MapRes { f: self, g } } /// Applies a function returning an `Option` over the result of a parser. - fn map_opt(self, g: G) -> MapOpt + fn map_opt(self, g: G) -> MapOpt where - G: Fn(O) -> Option, + G: Fn(Self::Output) -> Option, Self: core::marker::Sized, { - MapOpt { - f: self, - g, - phantom: core::marker::PhantomData, - } + MapOpt { f: self, g } } /// Creates a second parser from the output of the first one, then apply over the rest of the input - fn flat_map(self, g: G) -> FlatMap + fn flat_map(self, g: G) -> FlatMap where - G: FnMut(O) -> H, - H: Parser, + G: FnMut(Self::Output) -> H, + H: Parser, Self: core::marker::Sized, { - FlatMap { - f: self, - g, - phantom: core::marker::PhantomData, - } + FlatMap { f: self, g } } /// Applies a second parser over the output of the first one - fn and_then(self, g: G) -> AndThen + fn and_then(self, g: G) -> AndThen where - G: Parser, + G: Parser, Self: core::marker::Sized, { - AndThen { - f: self, - g, - phantom: core::marker::PhantomData, - } + AndThen { f: self, g } } /// Applies a second parser after the first one, return their results as a tuple fn and(self, g: G) -> And where - G: Parser, + G: Parser, Self: core::marker::Sized, { And { f: self, g } @@ -318,7 +303,7 @@ pub trait Parser { /// Applies a second parser over the input if the first one failed fn or(self, g: G) -> Or where - G: Parser, + G: Parser, Self: core::marker::Sized, { Or { f: self, g } @@ -326,24 +311,24 @@ pub trait Parser { /// automatically converts the parser's output and error values to another type, as long as they /// implement the `From` trait - fn into, E2: From>(self) -> Into + fn into, E2: From>(self) -> Into where Self: core::marker::Sized, { Into { f: self, - phantom_out1: core::marker::PhantomData, - phantom_err1: core::marker::PhantomData, phantom_out2: core::marker::PhantomData, phantom_err2: core::marker::PhantomData, } } } -impl Parser for F +impl, F> Parser for F where F: FnMut(I) -> IResult, { + type Output = O; + type Error = E; fn parse(&mut self, i: I) -> IResult { self(i) } @@ -352,10 +337,12 @@ where macro_rules! impl_parser_for_tuple { ($($parser:ident $output:ident),+) => ( #[allow(non_snake_case)] - impl, $($parser),+> Parser for ($($parser),+,) + impl, $($parser),+> Parser for ($($parser),+,) where - $($parser: Parser),+ + $($parser: Parser),+ { + type Output = ($($output),+,); + type Error = E; fn parse(&mut self, i: I) -> IResult { let ($(ref mut $parser),+,) = *self; @@ -386,7 +373,9 @@ impl_parser_for_tuples!(P1 O1, P2 O2, P3 O3, P4 O4, P5 O5, P6 O6, P7 O7, P8 O8, use alloc::boxed::Box; #[cfg(feature = "alloc")] -impl Parser for Box> { +impl> Parser for Box> { + type Output = O; + type Error = E; fn parse(&mut self, input: I) -> IResult { (**self).parse(input) } @@ -394,13 +383,17 @@ impl Parser for Box> { /// Implementation of `Parser::map` #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] -pub struct Map { +pub struct Map { f: F, g: G, - phantom: core::marker::PhantomData, } -impl, G: FnMut(O1) -> O2> Parser for Map { +impl, F: Parser, G: FnMut(>::Output) -> O2> + Parser for Map +{ + type Output = O2; + type Error = E; + fn parse(&mut self, i: I) -> IResult { match self.f.parse(i) { Err(e) => Err(e), @@ -410,65 +403,81 @@ impl, G: FnMut(O1) -> O2> Parser for } /// Implementation of `Parser::map_res` -pub struct MapRes { +pub struct MapRes { f: F, g: G, - phantom: core::marker::PhantomData, } -impl Parser for MapRes +impl Parser for MapRes where I: Clone, - E: FromExternalError, - F: Parser, - G: Fn(O) -> Result, + >::Error: FromExternalError, + F: Parser, + G: Fn(>::Output) -> Result, { - fn parse(&mut self, input: I) -> IResult { + type Output = O2; + type Error = >::Error; + fn parse(&mut self, input: I) -> IResult>::Error> { let i = input.clone(); let (input, o1) = self.f.parse(input)?; match (self.g)(o1) { Ok(o2) => Ok((input, o2)), - Err(e) => Err(Err::Error(E::from_external_error(i, ErrorKind::MapRes, e))), + Err(e) => Err(Err::Error(>::Error::from_external_error( + i, + ErrorKind::MapRes, + e, + ))), } } } /// Implementation of `Parser::map_opt` -pub struct MapOpt { +pub struct MapOpt { f: F, g: G, - phantom: core::marker::PhantomData, } -impl Parser for MapOpt +impl Parser for MapOpt where I: Clone, - E: ParseError, - F: Parser, - G: Fn(O) -> Option, + F: Parser, + G: Fn(>::Output) -> Option, { - fn parse(&mut self, input: I) -> IResult { + type Output = O2; + type Error = >::Error; + + fn parse(&mut self, input: I) -> IResult>::Error> { let i = input.clone(); let (input, o1) = self.f.parse(input)?; match (self.g)(o1) { Some(o2) => Ok((input, o2)), - None => Err(Err::Error(E::from_error_kind(i, ErrorKind::MapOpt))), + None => Err(Err::Error(>::Error::from_error_kind( + i, + ErrorKind::MapOpt, + ))), } } } /// Implementation of `Parser::flat_map` #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] -pub struct FlatMap { +pub struct FlatMap { f: F, g: G, - phantom: core::marker::PhantomData, } -impl, G: FnMut(O1) -> H, H: Parser> Parser - for FlatMap +impl< + I, + E: ParseError, + F: Parser, + G: FnMut(>::Output) -> H, + H: Parser, + > Parser for FlatMap { - fn parse(&mut self, i: I) -> IResult { + type Output = >::Output; + type Error = E; + + fn parse(&mut self, i: I) -> IResult { let (i, o1) = self.f.parse(i)?; (self.g)(o1).parse(i) } @@ -476,16 +485,18 @@ impl, G: FnMut(O1) -> H, H: Parser> /// Implementation of `Parser::and_then` #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] -pub struct AndThen { +pub struct AndThen { f: F, g: G, - phantom: core::marker::PhantomData, } -impl, G: Parser> Parser - for AndThen +impl, G: Parser<>::Output, Error = >::Error>> + Parser for AndThen { - fn parse(&mut self, i: I) -> IResult { + type Output = >::Output>>::Output; + type Error = >::Error; + + fn parse(&mut self, i: I) -> IResult { let (i, o1) = self.f.parse(i)?; let (_, o2) = self.g.parse(o1)?; Ok((i, o2)) @@ -499,8 +510,13 @@ pub struct And { g: G, } -impl, G: Parser> Parser for And { - fn parse(&mut self, i: I) -> IResult { +impl, F: Parser, G: Parser> Parser + for And +{ + type Output = (>::Output, >::Output); + type Error = E; + + fn parse(&mut self, i: I) -> IResult { let (i, o1) = self.f.parse(i)?; let (i, o2) = self.g.parse(i)?; Ok((i, (o1, o2))) @@ -514,10 +530,18 @@ pub struct Or { g: G, } -impl, F: Parser, G: Parser> - Parser for Or +impl< + I: Clone, + O, + E: ParseError, + F: Parser, + G: Parser, + > Parser for Or { - fn parse(&mut self, i: I) -> IResult { + type Output = >::Output; + type Error = >::Error; + + fn parse(&mut self, i: I) -> IResult { match self.f.parse(i.clone()) { Err(Err::Error(e1)) => match self.g.parse(i) { Err(Err::Error(e2)) => Err(Err::Error(e1.or(e2))), @@ -530,24 +554,23 @@ impl, F: Parser, G: Parser< /// Implementation of `Parser::into` #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] -pub struct Into, E1, E2: From> { +pub struct Into { f: F, - phantom_out1: core::marker::PhantomData, - phantom_err1: core::marker::PhantomData, phantom_out2: core::marker::PhantomData, phantom_err2: core::marker::PhantomData, } impl< I: Clone, - O1, - O2: From, - E1, - E2: crate::error::ParseError + From, - F: Parser, - > Parser for Into + O2: From<>::Output>, + E2: crate::error::ParseError + From<>::Error>, + F: Parser, + > Parser for Into { - fn parse(&mut self, i: I) -> IResult { + type Output = O2; + type Error = E2; + + fn parse(&mut self, i: I) -> IResult { match self.f.parse(i) { Ok((i, o)) => Ok((i, o.into())), Err(Err::Error(e)) => Err(Err::Error(e.into())), diff --git a/src/multi/mod.rs b/src/multi/mod.rs index 1056add83..07f5ba766 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -54,11 +54,12 @@ const MAX_INITIAL_CAPACITY_BYTES: usize = 65536; /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn many0(mut f: F) -> impl FnMut(I) -> IResult, E> +pub fn many0( + mut f: F, +) -> impl FnMut(I) -> IResult>::Output>, >::Error> where I: Clone + InputLength, - F: Parser, - E: ParseError, + F: Parser, { move |mut i: I| { let mut acc = crate::lib::std::vec::Vec::with_capacity(4); @@ -70,7 +71,10 @@ where Ok((i1, o)) => { // infinite loop check: the parser must always consume if i1.input_len() == len { - return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many0))); + return Err(Err::Error(>::Error::from_error_kind( + i, + ErrorKind::Many0, + ))); } i = i1; @@ -109,14 +113,19 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn many1(mut f: F) -> impl FnMut(I) -> IResult, E> +pub fn many1( + mut f: F, +) -> impl FnMut(I) -> IResult>::Output>, >::Error> where I: Clone + InputLength, - F: Parser, - E: ParseError, + F: Parser, { move |mut i: I| match f.parse(i.clone()) { - Err(Err::Error(err)) => Err(Err::Error(E::append(i, ErrorKind::Many1, err))), + Err(Err::Error(err)) => Err(Err::Error(>::Error::append( + i, + ErrorKind::Many1, + err, + ))), Err(e) => Err(e), Ok((i1, o)) => { let mut acc = crate::lib::std::vec::Vec::with_capacity(4); @@ -131,7 +140,10 @@ where Ok((i1, o)) => { // infinite loop check: the parser must always consume if i1.input_len() == len { - return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))); + return Err(Err::Error(>::Error::from_error_kind( + i, + ErrorKind::Many1, + ))); } i = i1; @@ -166,14 +178,14 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn many_till( +pub fn many_till( mut f: F, mut g: G, -) -> impl FnMut(I) -> IResult, P), E> +) -> impl FnMut(I) -> IResult>::Output>, >::Output), E> where I: Clone + InputLength, - F: Parser, - G: Parser, + F: Parser, + G: Parser, E: ParseError, { move |mut i: I| { @@ -229,14 +241,14 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn separated_list0( +pub fn separated_list0( mut sep: G, mut f: F, -) -> impl FnMut(I) -> IResult, E> +) -> impl FnMut(I) -> IResult>::Output>, E> where I: Clone + InputLength, - F: Parser, - G: Parser, + F: Parser, + G: Parser, E: ParseError, { move |mut i: I| { @@ -303,14 +315,14 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn separated_list1( +pub fn separated_list1( mut sep: G, mut f: F, -) -> impl FnMut(I) -> IResult, E> +) -> impl FnMut(I) -> IResult>::Output>, E> where I: Clone + InputLength, - F: Parser, - G: Parser, + F: Parser, + G: Parser, E: ParseError, { move |mut i: I| { @@ -381,14 +393,14 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn many_m_n( +pub fn many_m_n( min: usize, max: usize, mut parse: F, -) -> impl FnMut(I) -> IResult, E> +) -> impl FnMut(I) -> IResult>::Output>, E> where I: Clone + InputLength, - F: Parser, + F: Parser, E: ParseError, { move |mut input: I| { @@ -396,8 +408,8 @@ where return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN))); } - let max_initial_capacity = - MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::().max(1); + let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES + / crate::lib::std::mem::size_of::<>::Output>().max(1); let mut res = crate::lib::std::vec::Vec::with_capacity(min.min(max_initial_capacity)); for count in 0..max { let len = input.input_len(); @@ -453,10 +465,10 @@ where /// assert_eq!(parser("123123"), Ok(("123123", 0))); /// assert_eq!(parser(""), Ok(("", 0))); /// ``` -pub fn many0_count(mut f: F) -> impl FnMut(I) -> IResult +pub fn many0_count(mut f: F) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, + F: Parser, E: ParseError, { move |i: I| { @@ -511,10 +523,10 @@ where /// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Many1Count)))); /// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Many1Count)))); /// ``` -pub fn many1_count(mut f: F) -> impl FnMut(I) -> IResult +pub fn many1_count(mut f: F) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, + F: Parser, E: ParseError, { move |i: I| { @@ -570,16 +582,19 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn count(mut f: F, count: usize) -> impl FnMut(I) -> IResult, E> +pub fn count( + mut f: F, + count: usize, +) -> impl FnMut(I) -> IResult>::Output>, E> where I: Clone + PartialEq, - F: Parser, + F: Parser, E: ParseError, { move |i: I| { let mut input = i.clone(); - let max_initial_capacity = - MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::().max(1); + let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES + / crate::lib::std::mem::size_of::<>::Output>().max(1); let mut res = crate::lib::std::vec::Vec::with_capacity(count.min(max_initial_capacity)); for _ in 0..count { @@ -626,10 +641,13 @@ where /// assert_eq!(parser(""), Err(Err::Error(Error::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 FnMut(I) -> IResult + 'a +pub fn fill<'a, I, E, F>( + mut f: F, + buf: &'a mut [>::Output], +) -> impl FnMut(I) -> IResult + 'a where I: Clone + PartialEq, - F: Parser + 'a, + F: Parser + 'a, E: ParseError, { move |i: I| { @@ -690,15 +708,15 @@ where /// assert_eq!(parser("123123"), Ok(("123123", vec![]))); /// assert_eq!(parser(""), Ok(("", vec![]))); /// ``` -pub fn fold_many0( +pub fn fold_many0( mut f: F, mut init: H, mut g: G, ) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, - G: FnMut(R, O) -> R, + F: Parser, + G: FnMut(R, >::Output) -> R, H: FnMut() -> R, E: ParseError, { @@ -766,15 +784,15 @@ where /// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Many1)))); /// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Many1)))); /// ``` -pub fn fold_many1( +pub fn fold_many1( mut f: F, mut init: H, mut g: G, ) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, - G: FnMut(R, O) -> R, + F: Parser, + G: FnMut(R, >::Output) -> R, H: FnMut() -> R, E: ParseError, { @@ -855,7 +873,7 @@ where /// assert_eq!(parser(""), Ok(("", vec![]))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); /// ``` -pub fn fold_many_m_n( +pub fn fold_many_m_n( min: usize, max: usize, mut parse: F, @@ -864,8 +882,8 @@ pub fn fold_many_m_n( ) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, - G: FnMut(R, O) -> R, + F: Parser, + G: FnMut(R, >::Output) -> R, H: FnMut() -> R, E: ParseError, { @@ -921,11 +939,11 @@ where /// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"efg"[..], &b"abc"[..]))); /// assert_eq!(parser(b"\x00\x03a"), Err(Err::Incomplete(Needed::new(2)))); /// ``` -pub fn length_data(mut f: F) -> impl FnMut(I) -> IResult +pub fn length_data(mut f: F) -> impl FnMut(I) -> IResult where I: Input, - N: ToUsize, - F: Parser, + >::Output: ToUsize, + F: Parser, E: ParseError, { move |i: I| { @@ -966,12 +984,15 @@ where /// assert_eq!(parser(b"\x00\x03123123"), Err(Err::Error(Error::new(&b"123"[..], ErrorKind::Tag)))); /// assert_eq!(parser(b"\x00\x03a"), Err(Err::Incomplete(Needed::new(2)))); /// ``` -pub fn length_value(mut f: F, mut g: G) -> impl FnMut(I) -> IResult +pub fn length_value( + mut f: F, + mut g: G, +) -> impl FnMut(I) -> IResult>::Output, E> where I: Clone + Input, - N: ToUsize, - F: Parser, - G: Parser, + >::Output: ToUsize, + F: Parser, + G: Parser, E: ParseError, { move |i: I| { @@ -1018,12 +1039,15 @@ where /// assert_eq!(parser(b"\x03123123123"), Err(Err::Error(Error::new(&b"123123123"[..], ErrorKind::Tag)))); /// ``` #[cfg(feature = "alloc")] -pub fn length_count(mut f: F, mut g: G) -> impl FnMut(I) -> IResult, E> +pub fn length_count( + mut f: F, + mut g: G, +) -> impl FnMut(I) -> IResult>::Output>, E> where I: Clone, - N: ToUsize, - F: Parser, - G: Parser, + >::Output: ToUsize, + F: Parser, + G: Parser, E: ParseError, { move |i: I| { @@ -1136,14 +1160,14 @@ where /// ``` #[cfg(feature = "alloc")] #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] -pub fn many( +pub fn many( range: G, mut parse: F, ) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, - Collection: Extend + Default, + F: Parser, + Collection: Extend<>::Output> + Default, E: ParseError, G: NomRange, { @@ -1221,7 +1245,7 @@ where /// assert_eq!(parser(""), Ok(("", vec![]))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); /// ``` -pub fn fold( +pub fn fold( range: J, mut parse: F, mut init: H, @@ -1229,8 +1253,8 @@ pub fn fold( ) -> impl FnMut(I) -> IResult where I: Clone + InputLength, - F: Parser, - G: FnMut(R, O) -> R, + F: Parser, + G: FnMut(R, >::Output) -> R, H: FnMut() -> R, E: ParseError, J: NomRange, diff --git a/src/sequence/mod.rs b/src/sequence/mod.rs index 23079534d..fd322f526 100644 --- a/src/sequence/mod.rs +++ b/src/sequence/mod.rs @@ -31,8 +31,8 @@ pub fn pair, F, G>( mut second: G, ) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, + F: Parser, + G: Parser, { move |input: I| { let (input, o1) = first.parse(input)?; @@ -60,13 +60,13 @@ where /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); /// ``` -pub fn preceded, F, G>( +pub fn preceded, F, G>( mut first: F, mut second: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, + F: Parser, + G: Parser, { move |input: I| { let (input, _) = first.parse(input)?; @@ -94,13 +94,13 @@ where /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); /// ``` -pub fn terminated, F, G>( +pub fn terminated, F, G>( mut first: F, mut second: G, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, + F: Parser, + G: Parser, { move |input: I| { let (input, o1) = first.parse(input)?; @@ -130,15 +130,15 @@ where /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); /// ``` -pub fn separated_pair, F, G, H>( +pub fn separated_pair, F, G, H>( mut first: F, mut sep: G, mut second: H, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, - H: Parser, + F: Parser, + G: Parser, + H: Parser, { move |input: I| { let (input, o1) = first.parse(input)?; @@ -169,15 +169,15 @@ where /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); /// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); /// ``` -pub fn delimited, F, G, H>( +pub fn delimited, F, G, H>( mut first: F, mut second: G, mut third: H, -) -> impl FnMut(I) -> IResult +) -> impl FnMut(I) -> IResult where - F: Parser, - G: Parser, - H: Parser, + F: Parser, + G: Parser, + H: Parser, { move |input: I| { let (input, _) = first.parse(input)?; @@ -197,7 +197,7 @@ pub trait Tuple { } #[allow(deprecated)] -impl, F: Parser> +impl, F: Parser> Tuple for (F,) { fn parse(&mut self, input: Input) -> IResult { @@ -224,9 +224,8 @@ macro_rules! tuple_trait_impl( #[allow(deprecated)] impl< Input: Clone, $($ty),+ , Error: ParseError, - $($name: Parser),+ + $($name: Parser),+ > Tuple for ( $($name),+ ) { - fn parse(&mut self, input: Input) -> IResult { tuple_trait_inner!(0, self, input, (), $($name)+) diff --git a/src/str.rs b/src/str.rs index 93d4c164c..496f47fa8 100644 --- a/src/str.rs +++ b/src/str.rs @@ -493,7 +493,10 @@ mod test { let b = "ababcd"; fn f(i: &str) -> IResult<&str, &str> { - recognize::<_, Vec<&str>, _, _>(many(1.., alt((tag("a"), tag("b")))))(i) + recognize(many::<_, _, Vec<&str>, _, _>( + 1.., + alt((tag("a"), tag("b"))), + ))(i) } assert_eq!(f(a), Ok((&a[6..], a))); diff --git a/tests/fnmut.rs b/tests/fnmut.rs index 976a47d7b..30ca9f0b2 100644 --- a/tests/fnmut.rs +++ b/tests/fnmut.rs @@ -8,7 +8,7 @@ fn parse() { let mut counter = 0; let res = { - let mut parser = many::<_, _, (), Vec<&str>, _, _>(0.., |i| { + let mut parser = many::<_, (), Vec<&str>, _, _>(0.., |i| { counter += 1; tag("abc")(i) }); @@ -25,7 +25,7 @@ fn accumulate() { let mut v = Vec::new(); let (_, count) = { - let mut parser = many0_count::<_, _, (), _>(|i| { + let mut parser = many0_count::<_, (), _>(|i| { let (i, o) = tag("abc")(i)?; v.push(o); Ok((i, ())) diff --git a/tests/issues.rs b/tests/issues.rs index 80d3e4e2e..a0755f764 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -172,7 +172,7 @@ fn issue_942() { fn issue_many_m_n_with_zeros() { use nom::character::complete::char; use nom::multi::many; - let mut parser = many::<_, _, (), Vec, _, _>(0..=0, char('a')); + let mut parser = many::<_, (), Vec, _, _>(0..=0, char('a')); assert_eq!(parser("aaa"), Ok(("aaa", vec!()))); } @@ -248,12 +248,12 @@ fn issue_1459_clamp_capacity() { // shouldn't panic use nom::multi::many_m_n; - let mut parser = many_m_n::<_, _, (), _>(usize::MAX, usize::MAX, char('a')); + let mut parser = many_m_n::<_, (), _>(usize::MAX, usize::MAX, char('a')); assert_eq!(parser("a"), Err(nom::Err::Error(()))); // shouldn't panic use nom::multi::count; - let mut parser = count::<_, _, (), _>(char('a'), usize::MAX); + let mut parser = count::<_, (), _>(char('a'), usize::MAX); assert_eq!(parser("a"), Err(nom::Err::Error(()))); } diff --git a/tests/json.rs b/tests/json.rs index 417fe4bbc..0d11bdbd0 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -90,7 +90,9 @@ fn string(input: &str) -> IResult<&str, String> { )(input) } -fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, O, E>>(f: F) -> impl Parser<&'a str, O, E> { +fn ws<'a, O, E: ParseError<&'a str>, F: Parser<&'a str, Output = O, Error = E>>( + f: F, +) -> impl Parser<&'a str, Output = O, Error = E> { delimited(multispace0, f, multispace0) }