Skip to content

Commit

Permalink
Merge 44f637c into 7e2d627
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 3, 2024
2 parents 7e2d627 + 44f637c commit f6b1a80
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 579 deletions.
60 changes: 29 additions & 31 deletions fuzz/fuzz_targets/fuzz_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winnow::prelude::*;
use winnow::{
ascii::{digit1 as digit, space0 as space},
combinator::alt,
combinator::fold_repeat,
combinator::repeat,
combinator::{delimited, terminated},
};

Expand Down Expand Up @@ -65,24 +65,23 @@ fn term(i: &mut &str) -> PResult<i64> {
e
})?;

let res = fold_repeat(
0..,
alt((('*', factor), ('/', factor.verify(|i| *i != 0)))),
|| init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc.saturating_mul(val)
} else {
match acc.checked_div(val) {
Some(v) => v,
// we get a division with overflow because we can get acc = i64::MIN and val = -1
// the division by zero is already checked earlier by verify
None => i64::MAX,
let res = repeat(0.., alt((('*', factor), ('/', factor.verify(|i| *i != 0)))))
.fold(
|| init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc.saturating_mul(val)
} else {
match acc.checked_div(val) {
Some(v) => v,
// we get a division with overflow because we can get acc = i64::MIN and val = -1
// the division by zero is already checked earlier by verify
None => i64::MAX,
}
}
}
},
)
.parse_next(i);
},
)
.parse_next(i);

decr();
res
Expand All @@ -95,19 +94,18 @@ fn expr(i: &mut &str) -> PResult<i64> {
e
})?;

let res = fold_repeat(
0..,
(alt(('+', '-')), term),
|| init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc.saturating_add(val)
} else {
acc.saturating_sub(val)
}
},
)
.parse_next(i);
let res = repeat(0.., (alt(('+', '-')), term))
.fold(
|| init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc.saturating_add(val)
} else {
acc.saturating_sub(val)
}
},
)
.parse_next(i);

decr();
res
Expand Down
22 changes: 5 additions & 17 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,6 @@ where
.parse_next(input)
}

/// Deprecated, replaced with [`till_line_ending`]
#[deprecated(since = "0.5.35", note = "Replaced with `till_line_ending`")]
#[inline(always)]
pub fn not_line_ending<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Slice, E>
where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
{
till_line_ending(input)
}

fn till_line_ending_<I, E: ParserError<I>, const PARTIAL: bool>(
input: &mut I,
) -> PResult<<I as Stream>::Slice, E>
Expand Down Expand Up @@ -1351,6 +1338,7 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
I: Compare<Caseless<&'static str>>,
<I as Stream>::Slice: ParseSlice<O>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::IterOffsets: Clone,
Expand All @@ -1365,27 +1353,27 @@ where
}

#[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug
#[allow(deprecated)]
fn recognize_float_or_exceptions<I, E: ParserError<I>>(
input: &mut I,
) -> PResult<<I as Stream>::Slice, E>
where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
I: Compare<Caseless<&'static str>>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
{
alt((
recognize_float,
crate::token::tag_no_case("nan"),
crate::token::tag(Caseless("nan")),
(
opt(one_of(['+', '-'])),
crate::token::tag_no_case("infinity"),
crate::token::tag(Caseless("infinity")),
)
.recognize(),
(opt(one_of(['+', '-'])), crate::token::tag_no_case("inf")).recognize(),
(opt(one_of(['+', '-'])), crate::token::tag(Caseless("inf"))).recognize(),
))
.parse_next(input)
}
Expand Down
45 changes: 2 additions & 43 deletions src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,19 +2438,6 @@ where
})
}

/// Deprecated since 0.5.27, replaced with [`length_take`]
#[deprecated(since = "0.5.27", note = "Replaced with `length_take`")]
pub fn length_data<I, N, E, F>(f: F) -> impl Parser<I, <I as Stream>::Slice, E>
where
I: StreamIsPartial,
I: Stream,
N: ToUsize,
F: Parser<I, N, E>,
E: ParserError<I>,
{
length_take(f)
}

/// Parse a length-prefixed slice ([TLV](https://en.wikipedia.org/wiki/Type-length-value))
///
/// *Complete version*: Returns an error if there is not enough input data.
Expand Down Expand Up @@ -2509,20 +2496,6 @@ where
})
}

/// Deprecated since 0.5.27, replaced with [`length_and_then`]
#[deprecated(since = "0.5.27", note = "Replaced with `length_and_then`")]
pub fn length_value<I, O, N, E, F, G>(f: F, g: G) -> impl Parser<I, O, E>
where
I: StreamIsPartial,
I: Stream + UpdateSlice + Clone,
N: ToUsize,
F: Parser<I, N, E>,
G: Parser<I, O, E>,
E: ParserError<I>,
{
length_and_then(f, g)
}

/// [`Accumulate`] a length-prefixed sequence of values ([TLV](https://en.wikipedia.org/wiki/Type-length-value))
///
/// If the length represents token counts, see instead [`length_take`]
Expand All @@ -2540,7 +2513,7 @@ where
/// # use winnow::prelude::*;
/// use winnow::Bytes;
/// use winnow::binary::u8;
/// use winnow::binary::length_count;
/// use winnow::binary::length_repeat;
/// use winnow::token::tag;
///
/// type Stream<'i> = &'i Bytes;
Expand All @@ -2550,7 +2523,7 @@ where
/// }
///
/// fn parser(s: Stream<'_>) -> IResult<Stream<'_>, Vec<&[u8]>> {
/// length_count(u8.map(|i| {
/// length_repeat(u8.map(|i| {
/// println!("got number: {}", i);
/// i
/// }), "abc").parse_peek(s)
Expand All @@ -2575,17 +2548,3 @@ where
repeat(n, g.by_ref()).parse_next(i)
})
}

/// Deprecated since 0.5.27, replaced with [`length_repeat`]
#[deprecated(since = "0.5.27", note = "Replaced with `length_repeat`")]
pub fn length_count<I, O, C, N, E, F, G>(f: F, g: G) -> impl Parser<I, C, E>
where
I: Stream,
N: ToUsize,
C: Accumulate<O>,
F: Parser<I, N, E>,
G: Parser<I, O, E>,
E: ParserError<I>,
{
length_repeat(f, g)
}
6 changes: 0 additions & 6 deletions src/combinator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,6 @@ pub fn empty<I: Stream, E: ParserError<I>>(_input: &mut I) -> PResult<(), E> {
Ok(())
}

/// Deprecated, replaced with [`empty`] + [`Parser::value`]
#[deprecated(since = "0.5.35", note = "Replaced with empty.value(...)`")]
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.
///
/// For example, it can be used as the last alternative in `alt` to
Expand Down
125 changes: 0 additions & 125 deletions src/combinator/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,6 @@ where
})
}

/// 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,
Expand Down Expand Up @@ -732,51 +718,6 @@ where
})
}

/// [`Accumulate`] the output of a parser, interleaved with `sep`
///
/// This stops when either parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see
/// [`cut_err`][crate::combinator::cut_err].
///
/// # Arguments
/// * `parser` Parses the elements of the list.
/// * `sep` Parses the separator between list elements.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "std")] {
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed};
/// # use winnow::prelude::*;
/// use winnow::combinator::separated0;
/// use winnow::token::tag;
///
/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
/// separated0("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(""), Ok(("", vec![])));
/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![])));
/// # }
/// ```
#[doc(alias = "sep_by")]
#[doc(alias = "separated_list0")]
#[deprecated(since = "0.5.19", note = "Replaced with `combinator::separated`")]
pub fn separated0<I, O, C, O2, E, P, S>(mut parser: P, mut sep: S) -> impl Parser<I, C, E>
where
I: Stream,
C: Accumulate<O>,
P: Parser<I, O, E>,
S: Parser<I, O2, E>,
E: ParserError<I>,
{
trace("separated0", move |i: &mut I| {
separated0_(&mut parser, &mut sep, i)
})
}

fn separated0_<I, O, C, O2, E, P, S>(
parser: &mut P,
separator: &mut S,
Expand Down Expand Up @@ -836,53 +777,6 @@ where
}
}

/// [`Accumulate`] the output of a parser, interleaved with `sep`
///
/// Fails if the element parser does not produce at least one element.$
///
/// This stops when either parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see
/// [`cut_err`][crate::combinator::cut_err].
///
/// # Arguments
/// * `sep` Parses the separator between list elements.
/// * `f` Parses the elements of the list.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "std")] {
/// # 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_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(InputError::new("", ErrorKind::Tag))));
/// assert_eq!(parser("def|abc"), Err(ErrMode::Backtrack(InputError::new("def|abc", ErrorKind::Tag))));
/// # }
/// ```
#[doc(alias = "sep_by1")]
#[doc(alias = "separated_list1")]
#[deprecated(since = "0.5.19", note = "Replaced with `combinator::separated`")]
pub fn separated1<I, O, C, O2, E, P, S>(mut parser: P, mut sep: S) -> impl Parser<I, C, E>
where
I: Stream,
C: Accumulate<O>,
P: Parser<I, O, E>,
S: Parser<I, O2, E>,
E: ParserError<I>,
{
trace("separated1", move |i: &mut I| {
separated1_(&mut parser, &mut sep, i)
})
}

fn separated1_<I, O, C, O2, E, P, S>(
parser: &mut P,
separator: &mut S,
Expand Down Expand Up @@ -1248,25 +1142,6 @@ where
})
}

/// Deprecated, replaced with [`Repeat::fold`]
#[deprecated(since = "0.5.36", note = "Replaced with `repeat(...).fold(...)`")]
#[inline(always)]
pub fn fold_repeat<I, O, E, F, G, H, R>(
range: impl Into<Range>,
f: F,
init: H,
g: G,
) -> impl Parser<I, R, E>
where
I: Stream,
F: Parser<I, O, E>,
G: FnMut(R, O) -> R,
H: FnMut() -> R,
E: ParserError<I>,
{
repeat(range, f).fold(init, g)
}

fn fold_repeat0_<I, O, E, F, G, H, R>(
f: &mut F,
init: &mut H,
Expand Down

0 comments on commit f6b1a80

Please sign in to comment.