Skip to content
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

fix(comb): Generalize repeat_till0 as repeat_till #439

Merged
merged 2 commits into from
Jan 26, 2024
Merged
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/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! | combinator | usage | input | new input | output | comment |
//! |---|---|---|---|---|---|
//! | [`repeat`] | `repeat(1..=3, "ab")` | `"ababc"` | `"c"` | `Ok(vec!["ab", "ab"])` |Applies the parser between m and n times (n included) and returns the list of results in a Vec|
//! | [`repeat_till0`] | `repeat_till0(tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `"g"` | `Ok((vec!["ab", "ab"], "ef"))` |Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second|
//! | [`repeat_till`] | `repeat_till(0.., tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `"g"` | `Ok((vec!["ab", "ab"], "ef"))` |Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second|
//! | [`separated`] | `separated(1..=3, "ab", ",")` | `"ab,ab,ab."` | `"."` | `Ok(vec!["ab", "ab", "ab"])` |Applies the parser and separator between m and n times (n included) and returns the list of results in a Vec|
//! | [`fold_repeat`] | `fold_repeat(1..=2, be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `[3]` | `Ok(3)` |Applies the parser between m and n times (n included) and folds the list of return value|
//!
Expand Down
142 changes: 117 additions & 25 deletions src/combinator/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ where
/// # #[cfg(feature = "std")] {
/// # use winnow::{error::ErrMode, error::{InputError, ErrorKind}, error::Needed};
/// # use winnow::prelude::*;
/// use winnow::combinator::repeat_till0;
/// use winnow::combinator::repeat_till;
/// use winnow::token::tag;
///
/// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
/// repeat_till0("abc", "end").parse_peek(s)
/// repeat_till(0.., "abc", "end").parse_peek(s)
/// };
///
/// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
Expand All @@ -298,42 +298,134 @@ where
/// # }
/// ```
#[doc(alias = "many_till0")]
pub fn repeat_till0<I, O, C, P, E, F, G>(mut f: F, mut g: G) -> impl Parser<I, (C, P), E>
pub fn repeat_till<I, O, C, P, E, F, G>(
range: impl Into<Range>,
mut f: F,
mut g: G,
) -> impl Parser<I, (C, P), E>
where
I: Stream,
C: Accumulate<O>,
F: Parser<I, O, E>,
G: Parser<I, P, E>,
E: ParserError<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) {
Ok(o) => return Ok((res, o)),
Err(ErrMode::Backtrack(_)) => {
i.reset(start);
match f.parse_next(i) {
Err(e) => return Err(e.append(i, ErrorKind::Many)),
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",
));
}
let Range {
start_inclusive,
end_inclusive,
} = range.into();
trace("repeat_till", move |i: &mut I| {
match (start_inclusive, end_inclusive) {
(0, None) => repeat_till0_(&mut f, &mut g, i),
(start, end) => repeat_till_m_n_(start, end.unwrap_or(usize::MAX), &mut f, &mut g, i),
}
})
}

res.accumulate(o);
/// Deprecated, replaced with [`repeat_till`]
#[deprecated(since = "0.5.35", note = "Replaced with `repeat_till`")]
#[inline(always)]
pub fn repeat_till0<I, O, C, P, E, F, G>(f: F, g: G) -> impl Parser<I, (C, P), E>
where
I: Stream,
C: Accumulate<O>,
F: Parser<I, O, E>,
G: Parser<I, P, E>,
E: ParserError<I>,
{
repeat_till(0.., f, g)
}

fn repeat_till0_<I, O, C, P, E, F, G>(f: &mut F, g: &mut G, i: &mut I) -> PResult<(C, P), E>
where
I: Stream,
C: Accumulate<O>,
F: Parser<I, O, E>,
G: Parser<I, P, E>,
E: ParserError<I>,
{
let mut res = C::initial(None);
loop {
let start = i.checkpoint();
let len = i.eof_offset();
match g.parse_next(i) {
Ok(o) => return Ok((res, o)),
Err(ErrMode::Backtrack(_)) => {
i.reset(start);
match f.parse_next(i) {
Err(e) => return Err(e.append(i, ErrorKind::Many)),
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"));
}

res.accumulate(o);
}
}
Err(e) => return Err(e),
}
Err(e) => return Err(e),
}
})
}
}

fn repeat_till_m_n_<I, O, C, P, E, F, G>(
min: usize,
max: usize,
f: &mut F,
g: &mut G,
i: &mut I,
) -> PResult<(C, P), E>
where
I: Stream,
C: Accumulate<O>,
F: Parser<I, O, E>,
G: Parser<I, P, E>,
E: ParserError<I>,
{
if min > max {
return Err(ErrMode::Cut(E::from_error_kind(i, ErrorKind::Many)));
}

let mut res = C::initial(Some(min));
for _ in 0..min {
match f.parse_next(i) {
Ok(o) => {
res.accumulate(o);
}
Err(e) => {
return Err(e.append(i, ErrorKind::Many));
}
}
}
for count in min..=max {
let start = i.checkpoint();
let len = i.eof_offset();
match g.parse_next(i) {
Ok(o) => return Ok((res, o)),
Err(ErrMode::Backtrack(err)) => {
if count == max {
return Err(ErrMode::Backtrack(err));
}
i.reset(start);
match f.parse_next(i) {
Err(e) => {
return Err(e.append(i, ErrorKind::Many));
}
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"));
}

res.accumulate(o);
}
}
}
Err(e) => return Err(e),
}
}
unreachable!()
}

/// [`Accumulate`] the output of a parser, interleaved with `sep`
Expand Down
42 changes: 41 additions & 1 deletion src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,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_peek(i)
repeat_till(0.., "abcd", "efgh").parse_peek(i)
}

let a = b"abcdabcdefghabcd";
Expand All @@ -987,6 +987,46 @@ fn repeat_till_test() {
);
}

#[test]
#[cfg(feature = "alloc")]
fn repeat_till_range_test() {
#[allow(clippy::type_complexity)]
fn multi(i: &str) -> IResult<&str, (Vec<&str>, &str)> {
repeat_till(2..=4, "ab", "cd").parse_peek(i)
}

assert_eq!(
multi("cd"),
Err(ErrMode::Backtrack(error_node_position!(
&"cd",
ErrorKind::Many,
error_position!(&"cd", ErrorKind::Tag)
)))
);
assert_eq!(
multi("abcd"),
Err(ErrMode::Backtrack(error_node_position!(
&"cd",
ErrorKind::Many,
error_position!(&"cd", ErrorKind::Tag)
)))
);
assert_eq!(multi("ababcd"), Ok(("", (vec!["ab", "ab"], "cd"))));
assert_eq!(multi("abababcd"), Ok(("", (vec!["ab", "ab", "ab"], "cd"))));
assert_eq!(
multi("ababababcd"),
Ok(("", (vec!["ab", "ab", "ab", "ab"], "cd")))
);
assert_eq!(
multi("abababababcd"),
Err(ErrMode::Backtrack(error_node_position!(
&"cd",
ErrorKind::Many,
error_position!(&"abcd", ErrorKind::Tag)
)))
);
}

#[test]
#[cfg(feature = "std")]
fn infinite_many() {
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ fn overflow_incomplete_many1() {
#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_many_till0() {
use winnow::combinator::repeat_till0;
use winnow::combinator::repeat_till;

#[allow(clippy::type_complexity)]
fn multi(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (Vec<&[u8]>, &[u8])> {
repeat_till0(length_take(be_u64), "abc").parse_peek(i)
repeat_till(0.., length_take(be_u64), "abc").parse_peek(i)
}

// Trigger an overflow in repeat_till0
// Trigger an overflow in repeat_till
assert_eq!(
multi(Partial::new(
&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]
Expand Down
Loading