Skip to content

Commit

Permalink
Merge pull request #127 from epage/extend
Browse files Browse the repository at this point in the history
feat(multi): Switch from Vec to impl Accumulate
  • Loading branch information
epage committed Feb 3, 2023
2 parents 2f52238 + 1414499 commit 77623fd
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 224 deletions.
3 changes: 1 addition & 2 deletions examples/ini/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ pub fn categories(i: Input<'_>) -> IResult<Input<'_>, HashMap<&str, HashMap<&str
many0(separated_pair(
category,
opt(multispace),
many0(terminated(key_value, opt(multispace))).map(|vec: Vec<_>| vec.into_iter().collect()),
many0(terminated(key_value, opt(multispace))),
))
.map(|vec: Vec<_>| vec.into_iter().collect())
.parse_next(i)
}

Expand Down
19 changes: 2 additions & 17 deletions examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ use winnow::{
pub type Input<'i> = &'i str;

pub fn categories(input: Input<'_>) -> IResult<Input<'_>, HashMap<&str, HashMap<&str, &str>>> {
match categories_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
}
}

#[allow(clippy::type_complexity)]
fn categories_aggregator(i: Input<'_>) -> IResult<Input<'_>, Vec<(&str, HashMap<&str, &str>)>> {
many0(category_and_keys)(i)
many0(category_and_keys)(input)
}

fn category_and_keys(i: Input<'_>) -> IResult<Input<'_>, (&str, HashMap<&str, &str>)> {
Expand All @@ -35,14 +27,7 @@ fn category(i: Input<'_>) -> IResult<Input<'_>, &str> {
}

fn keys_and_values(input: Input<'_>) -> IResult<Input<'_>, HashMap<&str, &str>> {
match keys_and_values_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
}
}

fn keys_and_values_aggregator(i: Input<'_>) -> IResult<Input<'_>, Vec<(&str, &str)>> {
many0(key_value)(i)
many0(key_value)(input)
}

fn key_value(i: Input<'_>) -> IResult<Input<'_>, (&str, &str)> {
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
('{', ws),
cut_err(terminated(
separated_list0((ws, ',', ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, ',', ws), key_value),
(ws, '}'),
)),
)
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
('{', ws),
cut_err(terminated(
separated_list0((ws, ',', ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, ',', ws), key_value),
(ws, '}'),
)),
)
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
(one_of('{'), ws),
cut_err(terminated(
separated_list0((ws, one_of(','), ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, one_of(','), ws), key_value),
(ws, one_of('}')),
)),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn array(i: &str) -> IResult<&str, ()> {
preceded(
'[',
cut_err(terminated(
separated_list0(preceded(sp, ','), value).map(|_| ()),
separated_list0(preceded(sp, ','), value),
preceded(sp, ']'),
)),
)
Expand All @@ -243,7 +243,7 @@ fn hash(i: &str) -> IResult<&str, ()> {
preceded(
'{',
cut_err(terminated(
separated_list0(preceded(sp, ','), key_value).map(|_| ()),
separated_list0(preceded(sp, ','), key_value),
preceded(sp, '}'),
)),
)
Expand Down
24 changes: 12 additions & 12 deletions src/_cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
//! preceded(
//! alt(("0x", "0X")),
//! many1(
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_'))
//! ).recognize()
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -191,8 +191,8 @@
//! preceded(
//! alt(("0x", "0X")),
//! many1(
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_'))
//! ).recognize()
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! ).map_res(
//! |out: &str| i64::from_str_radix(&str::replace(&out, "_", ""), 16)
//! ).parse_next(input)
Expand All @@ -215,8 +215,8 @@
//! preceded(
//! alt(("0o", "0O")),
//! many1(
//! terminated(one_of("01234567"), many0('_'))
//! ).recognize()
//! terminated(one_of("01234567"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -237,8 +237,8 @@
//! preceded(
//! alt(("0b", "0B")),
//! many1(
//! terminated(one_of("01"), many0('_'))
//! ).recognize()
//! terminated(one_of("01"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -256,8 +256,8 @@
//!
//! fn decimal(input: &str) -> IResult<&str, &str> {
//! many1(
//! terminated(one_of("0123456789"), many0('_'))
//! )
//! terminated(one_of("0123456789"), many0('_').map(|()| ()))
//! ).map(|()| ())
//! .recognize()
//! .parse_next(input)
//! }
Expand Down Expand Up @@ -311,8 +311,8 @@
//!
//! fn decimal(input: &str) -> IResult<&str, &str> {
//! many1(
//! terminated(one_of("0123456789"), many0('_'))
//! )
//! terminated(one_of("0123456789"), many0('_').map(|()| ()))
//! ).map(|()| ())
//! .recognize()
//! .parse_next(input)
//! }
Expand Down
26 changes: 11 additions & 15 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,26 +805,24 @@ where
since = "0.1.0",
note = "Replaced with `winnow::character::escaped_transform`"
)]
pub fn escaped_transform<I, Error, F, G, O1, O2, ExtendItem, Output>(
pub fn escaped_transform<I, Error, F, G, Output>(
mut normal: F,
control_char: char,
mut transform: G,
) -> impl FnMut(I) -> IResult<I, Output, Error>
where
I: Input + Offset,
<I as Input>::Token: crate::input::AsChar,
I: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Output: crate::input::Accumulate<<I as Input>::Slice>,
F: Parser<I, <I as Input>::Slice, Error>,
G: Parser<I, <I as Input>::Slice, Error>,
Error: ParseError<I>,
{
move |input: I| escaped_transform_internal(input, &mut normal, control_char, &mut transform)
}

#[cfg(feature = "alloc")]
pub(crate) fn escaped_transform_internal<I, Error, F, G, O1, O2, ExtendItem, Output>(
pub(crate) fn escaped_transform_internal<I, Error, F, G, Output>(
input: I,
normal: &mut F,
control_char: char,
Expand All @@ -833,17 +831,15 @@ pub(crate) fn escaped_transform_internal<I, Error, F, G, O1, O2, ExtendItem, Out
where
I: Input + Offset,
<I as Input>::Token: crate::input::AsChar,
I: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Output: crate::input::Accumulate<<I as Input>::Slice>,
F: Parser<I, <I as Input>::Slice, Error>,
G: Parser<I, <I as Input>::Slice, Error>,
Error: ParseError<I>,
{
use crate::input::AsChar;

let mut offset = 0;
let mut res = input.new_builder();
let mut res = Output::initial(Some(input.input_len()));

let i = input.clone();

Expand All @@ -852,7 +848,7 @@ where
let (remainder, _) = i.next_slice(offset);
match normal.parse_next(remainder.clone()) {
Ok((i2, o)) => {
o.extend_into(&mut res);
res.accumulate(o);
if i2.input_len() == 0 {
return Ok((i.next_slice(i.input_len()).0, res));
} else if i2.input_len() == current_len {
Expand All @@ -874,7 +870,7 @@ where
} else {
match transform.parse_next(i.next_slice(next).0) {
Ok((i2, o)) => {
o.extend_into(&mut res);
res.accumulate(o);
if i2.input_len() == 0 {
return Ok((i.next_slice(i.input_len()).0, res));
} else {
Expand Down
26 changes: 11 additions & 15 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,26 +861,24 @@ where
since = "0.1.0",
note = "Replaced with `winnow::character::escaped_transform` with input wrapped in `winnow::input::Streaming`"
)]
pub fn escaped_transform<I, Error, F, G, O1, O2, ExtendItem, Output>(
pub fn escaped_transform<I, Error, F, G, Output>(
mut normal: F,
control_char: char,
mut transform: G,
) -> impl FnMut(I) -> IResult<I, Output, Error>
where
I: Input + Offset,
<I as Input>::Token: crate::input::AsChar,
I: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Output: crate::input::Accumulate<<I as Input>::Slice>,
F: Parser<I, <I as Input>::Slice, Error>,
G: Parser<I, <I as Input>::Slice, Error>,
Error: ParseError<I>,
{
move |input: I| escaped_transform_internal(input, &mut normal, control_char, &mut transform)
}

#[cfg(feature = "alloc")]
pub(crate) fn escaped_transform_internal<I, Error, F, G, O1, O2, ExtendItem, Output>(
pub(crate) fn escaped_transform_internal<I, Error, F, G, Output>(
input: I,
normal: &mut F,
control_char: char,
Expand All @@ -889,17 +887,15 @@ pub(crate) fn escaped_transform_internal<I, Error, F, G, O1, O2, ExtendItem, Out
where
I: Input + Offset,
<I as Input>::Token: crate::input::AsChar,
I: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Output: crate::input::Accumulate<<I as Input>::Slice>,
F: Parser<I, <I as Input>::Slice, Error>,
G: Parser<I, <I as Input>::Slice, Error>,
Error: ParseError<I>,
{
use crate::input::AsChar;

let mut offset = 0;
let mut res = input.new_builder();
let mut res = Output::initial(Some(input.input_len()));

let i = input.clone();

Expand All @@ -908,7 +904,7 @@ where
let remainder = i.next_slice(offset).0;
match normal.parse_next(remainder.clone()) {
Ok((i2, o)) => {
o.extend_into(&mut res);
res.accumulate(o);
if i2.input_len() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
} else if i2.input_len() == current_len {
Expand All @@ -927,7 +923,7 @@ where
} else {
match transform.parse_next(i.next_slice(next).0) {
Ok((i2, o)) => {
o.extend_into(&mut res);
res.accumulate(o);
if i2.input_len() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/character/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ where
/// ```
#[cfg(feature = "alloc")]
#[inline(always)]
pub fn escaped_transform<I, Error, F, G, O1, O2, ExtendItem, Output, const STREAMING: bool>(
pub fn escaped_transform<I, Error, F, G, Output, const STREAMING: bool>(
mut normal: F,
control_char: char,
mut transform: G,
Expand All @@ -1265,11 +1265,9 @@ where
I: InputIsStreaming<STREAMING>,
I: Input + Offset,
<I as Input>::Token: crate::input::AsChar,
I: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Output: crate::input::Accumulate<<I as Input>::Slice>,
F: Parser<I, <I as Input>::Slice, Error>,
G: Parser<I, <I as Input>::Slice, Error>,
Error: ParseError<I>,
{
move |input: I| {
Expand Down

0 comments on commit 77623fd

Please sign in to comment.