Skip to content

Create Parser implementation for rest #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
/// bits::<_, _, Error<(&[u8], usize)>, _, _>((
/// take(4usize),
/// take(8usize),
/// bytes::<_, _, Error<&[u8]>, _, _>(rest)
/// bytes::<_, _, Error<&[u8]>, _, _>(rest())
/// ))(input)
/// }
///
Expand Down
34 changes: 27 additions & 7 deletions src/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
pub fn rest<T, E: ParseError<T>>() -> impl Parser<T, Output = T, Error = E>
where
T: Input,
{
Ok(input.take_split(input.input_len()))
Rest {
i: PhantomData,
e: PhantomData,
}
}

/// Parser implementation for [rest]
pub struct Rest<I, E> {
i: PhantomData<I>,
e: PhantomData<E>,
}

impl<I: Input, E: ParseError<I>> Parser<I> for Rest<I, E> {
type Output = I;
type Error = E;

fn process<OM: OutputMode>(&mut self, input: I) -> PResult<OM, I, Self::Output, Self::Error> {
let (i, o) = input.take_split(input.input_len());
Ok((i, OM::Output::bind(|| o)))
}
}

/// Return the length of the remaining input.
Expand Down Expand Up @@ -766,7 +786,7 @@ where
/// fn parser(input: &str) -> IResult<&str, &str> {
/// alt((
/// preceded(one_of("+-"), digit1),
/// rest
/// rest()
/// )).parse(input)
/// }
///
Expand All @@ -789,7 +809,7 @@ where
/// fn parser(input: &str) -> IResult<&str, &str> {
/// alt((
/// preceded(one_of("+-"), cut(digit1)),
/// rest
/// rest()
/// )).parse(input)
/// }
///
Expand Down
5 changes: 3 additions & 2 deletions src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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]
Expand Down