From 2b59c45fd5a93caa8c9ad32eb31cc11631520216 Mon Sep 17 00:00:00 2001 From: Richard Meester Date: Wed, 12 Feb 2025 15:05:22 -0800 Subject: [PATCH] Create Parser implementation for rest --- src/bits/mod.rs | 2 +- src/combinator/mod.rs | 34 +++++++++++++++++++++++++++------- src/combinator/tests.rs | 5 +++-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/bits/mod.rs b/src/bits/mod.rs index f120cb353..5e867797f 100644 --- a/src/bits/mod.rs +++ b/src/bits/mod.rs @@ -73,7 +73,7 @@ where /// bits::<_, _, Error<(&[u8], usize)>, _, _>(( /// take(4usize), /// take(8usize), -/// bytes::<_, _, Error<&[u8]>, _, _>(rest) +/// bytes::<_, _, Error<&[u8]>, _, _>(rest()) /// ))(input) /// } /// diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index fe6655b98..00ca2972d 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -24,17 +24,37 @@ mod tests; /// Return the remaining input. /// /// ```rust -/// # use nom::error::ErrorKind; +/// use nom::error; +/// use nom::Parser; /// use nom::combinator::rest; -/// assert_eq!(rest::<_,(_, ErrorKind)>("abc"), Ok(("", "abc"))); -/// assert_eq!(rest::<_,(_, ErrorKind)>(""), Ok(("", ""))); +/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete("abc"), Ok(("", "abc"))); +/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete(""), Ok(("", ""))); /// ``` #[inline] -pub fn rest>(input: T) -> IResult +pub fn rest>() -> impl Parser where T: Input, { - Ok(input.take_split(input.input_len())) + Rest { + i: PhantomData, + e: PhantomData, + } +} + +/// Parser implementation for [rest] +pub struct Rest { + i: PhantomData, + e: PhantomData, +} + +impl> Parser for Rest { + type Output = I; + type Error = E; + + fn process(&mut self, input: I) -> PResult { + let (i, o) = input.take_split(input.input_len()); + Ok((i, OM::Output::bind(|| o))) + } } /// Return the length of the remaining input. @@ -766,7 +786,7 @@ where /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( /// preceded(one_of("+-"), digit1), -/// rest +/// rest() /// )).parse(input) /// } /// @@ -789,7 +809,7 @@ where /// fn parser(input: &str) -> IResult<&str, &str> { /// alt(( /// preceded(one_of("+-"), cut(digit1)), -/// rest +/// rest() /// )).parse(input) /// } /// diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index d02713f26..5d7eef7fe 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -7,6 +7,7 @@ use crate::internal::{Err, IResult, Needed}; #[cfg(feature = "alloc")] use crate::lib::std::boxed::Box; use crate::number::complete::u8; +use crate::Parser; macro_rules! assert_parse( ($left: expr, $right: expr) => { @@ -73,14 +74,14 @@ fn end_of_input() { 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_complete(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_complete(input), Ok((empty, input))); } #[test]