diff --git a/assets/trace.svg b/assets/trace.svg index f95dc03b..88eaf925 100644 --- a/assets/trace.svg +++ b/assets/trace.svg @@ -75,7 +75,7 @@ < any | +1 | verify | < one_of | +1 - > fold_many0 | "abc\"" + > fold_repeat0 | "abc\"" > alt | "abc\"" > take_till1 | "abc\"" < take_till1 | +3 @@ -99,7 +99,7 @@ < one_of | backtrack < preceded | backtrack < alt | backtrack - < fold_many0 | +3 + < fold_repeat0 | +3 > one_of | "\"" > any | "\"" < any | +1 diff --git a/benches/contains_token.rs b/benches/contains_token.rs index 238d3697..4be56132 100644 --- a/benches/contains_token.rs +++ b/benches/contains_token.rs @@ -1,7 +1,7 @@ use criterion::black_box; use winnow::combinator::alt; -use winnow::combinator::many0; +use winnow::combinator::repeat0; use winnow::prelude::*; use winnow::token::take_till1; use winnow::token::take_while1; @@ -55,22 +55,22 @@ fn contains_token(c: &mut criterion::Criterion) { fn parser_str(input: &str) -> IResult<&str, usize> { let contains = "0123456789"; - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } fn parser_slice(input: &str) -> IResult<&str, usize> { let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..]; - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } fn parser_array(input: &str) -> IResult<&str, usize> { let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } fn parser_tuple(input: &str) -> IResult<&str, usize> { let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } fn parser_closure_or(input: &str) -> IResult<&str, usize> { @@ -86,12 +86,12 @@ fn parser_closure_or(input: &str) -> IResult<&str, usize> { || c == '8' || c == '9' }; - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } fn parser_closure_matches(input: &str) -> IResult<&str, usize> { let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'); - many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) + repeat0(alt((take_while1(contains), take_till1(contains)))).parse_next(input) } const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; diff --git a/examples/arithmetic/parser.rs b/examples/arithmetic/parser.rs index e093db77..b009622c 100644 --- a/examples/arithmetic/parser.rs +++ b/examples/arithmetic/parser.rs @@ -5,7 +5,7 @@ use winnow::{ ascii::{digit1 as digits, space0 as spaces}, combinator::alt, combinator::delimited, - combinator::fold_many0, + combinator::fold_repeat0, token::one_of, IResult, }; @@ -15,7 +15,7 @@ use winnow::{ pub fn expr(i: &str) -> IResult<&str, i64> { let (i, init) = term(i)?; - fold_many0( + fold_repeat0( (one_of("+-"), term), move || init, |acc, (op, val): (char, i64)| { @@ -35,7 +35,7 @@ pub fn expr(i: &str) -> IResult<&str, i64> { fn term(i: &str) -> IResult<&str, i64> { let (i, init) = factor(i)?; - fold_many0( + fold_repeat0( (one_of("*/"), factor), move || init, |acc, (op, val): (char, i64)| { diff --git a/examples/arithmetic/parser_ast.rs b/examples/arithmetic/parser_ast.rs index 363720ca..4cc5fb9c 100644 --- a/examples/arithmetic/parser_ast.rs +++ b/examples/arithmetic/parser_ast.rs @@ -7,7 +7,7 @@ use winnow::prelude::*; use winnow::{ ascii::{digit1 as digit, multispace0 as multispace}, combinator::alt, - combinator::many0, + combinator::repeat0, combinator::{delimited, preceded}, IResult, }; @@ -46,7 +46,7 @@ impl Display for Expr { pub fn expr(i: &str) -> IResult<&str, Expr> { let (i, initial) = term(i)?; - let (i, remainder) = many0(alt(( + let (i, remainder) = repeat0(alt(( |i| { let (i, add) = preceded("+", term).parse_next(i)?; Ok((i, (Oper::Add, add))) @@ -63,7 +63,7 @@ pub fn expr(i: &str) -> IResult<&str, Expr> { fn term(i: &str) -> IResult<&str, Expr> { let (i, initial) = factor(i)?; - let (i, remainder) = many0(alt(( + let (i, remainder) = repeat0(alt(( |i| { let (i, mul) = preceded("*", factor).parse_next(i)?; Ok((i, (Oper::Mul, mul))) diff --git a/examples/http/parser.rs b/examples/http/parser.rs index ca8ca236..3bac3ade 100644 --- a/examples/http/parser.rs +++ b/examples/http/parser.rs @@ -1,4 +1,4 @@ -use winnow::{ascii::line_ending, combinator::many1, token::take_while1, IResult, Parser}; +use winnow::{ascii::line_ending, combinator::repeat1, token::take_while1, IResult, Parser}; pub type Stream<'i> = &'i [u8]; @@ -44,7 +44,7 @@ pub fn parse(data: &[u8]) -> Option, Vec>)>> { fn request(input: Stream<'_>) -> IResult, (Request<'_>, Vec>)> { let (input, req) = request_line(input)?; - let (input, h) = many1(message_header).parse_next(input)?; + let (input, h) = repeat1(message_header).parse_next(input)?; let (input, _) = line_ending(input)?; Ok((input, (req, h))) @@ -86,7 +86,7 @@ fn message_header_value(input: Stream<'_>) -> IResult, &[u8]> { fn message_header(input: Stream<'_>) -> IResult, Header<'_>> { let (input, name) = take_while1(is_token).parse_next(input)?; let (input, _) = ':'.parse_next(input)?; - let (input, value) = many1(message_header_value).parse_next(input)?; + let (input, value) = repeat1(message_header_value).parse_next(input)?; Ok((input, Header { name, value })) } diff --git a/examples/http/parser_streaming.rs b/examples/http/parser_streaming.rs index 85c53157..119cfc74 100644 --- a/examples/http/parser_streaming.rs +++ b/examples/http/parser_streaming.rs @@ -1,5 +1,5 @@ use winnow::{ - ascii::line_ending, combinator::many1, stream::Partial, token::take_while1, IResult, Parser, + ascii::line_ending, combinator::repeat1, stream::Partial, token::take_while1, IResult, Parser, }; pub type Stream<'i> = Partial<&'i [u8]>; @@ -46,7 +46,7 @@ pub fn parse(data: &[u8]) -> Option, Vec>)>> { fn request(input: Stream<'_>) -> IResult, (Request<'_>, Vec>)> { let (input, req) = request_line(input)?; - let (input, h) = many1(message_header).parse_next(input)?; + let (input, h) = repeat1(message_header).parse_next(input)?; let (input, _) = line_ending(input)?; Ok((input, (req, h))) @@ -88,7 +88,7 @@ fn message_header_value(input: Stream<'_>) -> IResult, &[u8]> { fn message_header(input: Stream<'_>) -> IResult, Header<'_>> { let (input, name) = take_while1(is_token).parse_next(input)?; let (input, _) = ':'.parse_next(input)?; - let (input, value) = many1(message_header_value).parse_next(input)?; + let (input, value) = repeat1(message_header_value).parse_next(input)?; Ok((input, Header { name, value })) } diff --git a/examples/ini/bench.rs b/examples/ini/bench.rs index 430a06cf..5d57eec7 100644 --- a/examples/ini/bench.rs +++ b/examples/ini/bench.rs @@ -1,4 +1,4 @@ -use winnow::combinator::many0; +use winnow::combinator::repeat0; use winnow::prelude::*; mod parser; @@ -32,7 +32,7 @@ file=payroll.dat \0"; fn acc(i: parser::Stream<'_>) -> IResult, Vec<(&str, &str)>> { - many0(parser::key_value).parse_next(i) + repeat0(parser::key_value).parse_next(i) } let mut group = c.benchmark_group("ini keys and values"); diff --git a/examples/ini/parser.rs b/examples/ini/parser.rs index 4d5fa89f..190b0430 100644 --- a/examples/ini/parser.rs +++ b/examples/ini/parser.rs @@ -4,8 +4,8 @@ use std::str; use winnow::prelude::*; use winnow::{ ascii::{alphanumeric1 as alphanumeric, multispace0 as multispace, space0 as space}, - combinator::many0, combinator::opt, + combinator::repeat0, combinator::{delimited, separated_pair, terminated}, token::take_while0, }; @@ -13,10 +13,10 @@ use winnow::{ pub type Stream<'i> = &'i [u8]; pub fn categories(i: Stream<'_>) -> IResult, HashMap<&str, HashMap<&str, &str>>> { - many0(separated_pair( + repeat0(separated_pair( category, opt(multispace), - many0(terminated(key_value, opt(multispace))), + repeat0(terminated(key_value, opt(multispace))), )) .parse_next(i) } diff --git a/examples/ini/parser_str.rs b/examples/ini/parser_str.rs index ced3bc93..2c280bef 100644 --- a/examples/ini/parser_str.rs +++ b/examples/ini/parser_str.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use winnow::prelude::*; use winnow::{ ascii::{alphanumeric1 as alphanumeric, space0 as space}, - combinator::many0, combinator::opt, + combinator::repeat0, combinator::{delimited, terminated}, token::{take_till0, take_while0, take_while1}, }; @@ -12,7 +12,7 @@ use winnow::{ pub type Stream<'i> = &'i str; pub fn categories(input: Stream<'_>) -> IResult, HashMap<&str, HashMap<&str, &str>>> { - many0(category_and_keys).parse_next(input) + repeat0(category_and_keys).parse_next(input) } fn category_and_keys(i: Stream<'_>) -> IResult, (&str, HashMap<&str, &str>)> { @@ -28,7 +28,7 @@ fn category(i: Stream<'_>) -> IResult, &str> { } fn keys_and_values(input: Stream<'_>) -> IResult, HashMap<&str, &str>> { - many0(key_value).parse_next(input) + repeat0(key_value).parse_next(input) } fn key_value(i: Stream<'_>) -> IResult, (&str, &str)> { diff --git a/examples/json/parser.rs b/examples/json/parser.rs index 981db34a..3708b003 100644 --- a/examples/json/parser.rs +++ b/examples/json/parser.rs @@ -7,7 +7,7 @@ use winnow::{ combinator::alt, combinator::cut_err, combinator::{delimited, preceded, separated_pair, terminated}, - combinator::{fold_many0, separated0}, + combinator::{fold_repeat0, separated0}, error::{ContextError, ParseError}, token::{any, none_of, take, take_while0}, }; @@ -87,7 +87,7 @@ fn string<'i, E: ParseError> + ContextError, &'static str> // right branch (since we found the `"` character) but encountered an error when // parsing the string cut_err(terminated( - fold_many0(character, String::new, |mut string, c| { + fold_repeat0(character, String::new, |mut string, c| { string.push(c); string }), diff --git a/examples/json/parser_dispatch.rs b/examples/json/parser_dispatch.rs index 894e05f5..09ca755d 100644 --- a/examples/json/parser_dispatch.rs +++ b/examples/json/parser_dispatch.rs @@ -10,7 +10,7 @@ use winnow::{ combinator::success, combinator::{alt, dispatch}, combinator::{delimited, preceded, separated_pair, terminated}, - combinator::{fold_many0, separated0}, + combinator::{fold_repeat0, separated0}, error::{ContextError, ParseError}, token::{any, none_of, take, take_while0}, }; @@ -96,7 +96,7 @@ fn string<'i, E: ParseError> + ContextError, &'static str> // right branch (since we found the `"` character) but encountered an error when // parsing the string cut_err(terminated( - fold_many0(character, String::new, |mut string, c| { + fold_repeat0(character, String::new, |mut string, c| { string.push(c); string }), diff --git a/examples/json/parser_partial.rs b/examples/json/parser_partial.rs index 1ff231d3..83e23f4c 100644 --- a/examples/json/parser_partial.rs +++ b/examples/json/parser_partial.rs @@ -7,7 +7,7 @@ use winnow::{ combinator::alt, combinator::{cut_err, rest}, combinator::{delimited, preceded, separated_pair, terminated}, - combinator::{fold_many0, separated0}, + combinator::{fold_repeat0, separated0}, error::{ContextError, ParseError}, stream::Partial, token::{any, none_of, take, take_while0}, @@ -88,7 +88,7 @@ fn string<'i, E: ParseError> + ContextError, &'static str> // right branch (since we found the `"` character) but encountered an error when // parsing the string cut_err(terminated( - fold_many0(character, String::new, |mut string, c| { + fold_repeat0(character, String::new, |mut string, c| { string.push(c); string }), diff --git a/examples/ndjson/parser.rs b/examples/ndjson/parser.rs index d7b899f4..c330002d 100644 --- a/examples/ndjson/parser.rs +++ b/examples/ndjson/parser.rs @@ -8,7 +8,7 @@ use winnow::{ combinator::alt, combinator::cut_err, combinator::{delimited, preceded, separated_pair, terminated}, - combinator::{fold_many0, separated0}, + combinator::{fold_repeat0, separated0}, error::{ContextError, ParseError}, stream::Partial, token::{any, none_of, take, take_while0}, @@ -92,7 +92,7 @@ fn string<'i, E: ParseError> + ContextError, &'static str> // right branch (since we found the `"` character) but encountered an error when // parsing the string cut_err(terminated( - fold_many0(character, String::new, |mut string, c| { + fold_repeat0(character, String::new, |mut string, c| { string.push(c); string }), diff --git a/examples/s_expression/parser.rs b/examples/s_expression/parser.rs index 1299b105..1212b869 100644 --- a/examples/s_expression/parser.rs +++ b/examples/s_expression/parser.rs @@ -5,7 +5,7 @@ use winnow::{ ascii::{alpha1, digit1, multispace0, multispace1}, combinator::alt, - combinator::many0, + combinator::repeat0, combinator::{cut_err, opt}, combinator::{delimited, preceded, terminated}, error::VerboseError, @@ -168,8 +168,8 @@ fn parse_keyword(i: &str) -> IResult<&str, Atom, VerboseError<&str>> { /// tuples are themselves a parser, used to sequence parsers together, so we can translate this /// directly and then map over it to transform the output into an `Expr::Application` fn parse_application(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { - let application_inner = - (parse_expr, many0(parse_expr)).map(|(head, tail)| Expr::Application(Box::new(head), tail)); + let application_inner = (parse_expr, repeat0(parse_expr)) + .map(|(head, tail)| Expr::Application(Box::new(head), tail)); // finally, we wrap it in an s-expression s_exp(application_inner).parse_next(i) } @@ -212,7 +212,7 @@ fn parse_quote(i: &str) -> IResult<&str, Expr, VerboseError<&str>> { // this should look very straight-forward after all we've done: // we find the `'` (quote) character, use cut_err to say that we're unambiguously // looking for an s-expression of 0 or more expressions, and then parse them - preceded("'", cut_err(s_exp(many0(parse_expr)))) + preceded("'", cut_err(s_exp(repeat0(parse_expr)))) .context("quote") .map(Expr::Quote) .parse_next(i) diff --git a/examples/string/parser.rs b/examples/string/parser.rs index a8d129f8..b2e39461 100644 --- a/examples/string/parser.rs +++ b/examples/string/parser.rs @@ -11,7 +11,7 @@ use winnow::ascii::multispace1; use winnow::combinator::alt; -use winnow::combinator::fold_many0; +use winnow::combinator::fold_repeat0; use winnow::combinator::{delimited, preceded}; use winnow::error::{FromExternalError, ParseError}; use winnow::prelude::*; @@ -23,9 +23,9 @@ pub fn parse_string<'a, E>(input: &'a str) -> IResult<&'a str, String, E> where E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { - // fold_many0 is the equivalent of iterator::fold. It runs a parser in a loop, + // fold_repeat0 is the equivalent of iterator::fold. It runs a parser in a loop, // and for each output value, calls a folding function on each output value. - let build_string = fold_many0( + let build_string = fold_repeat0( // Our parser function – parses a single string fragment parse_fragment, // Our init value, an empty string @@ -44,7 +44,7 @@ where // Finally, parse the string. Note that, if `build_string` could accept a raw // " character, the closing delimiter " would never match. When using - // `delimited` with a looping parser (like fold_many0), be sure that the + // `delimited` with a looping parser (like fold_repeat0), be sure that the // loop won't accidentally match your closing delimiter! delimited('"', build_string, '"').parse_next(input) } diff --git a/fuzz/fuzz_targets/fuzz_arithmetic.rs b/fuzz/fuzz_targets/fuzz_arithmetic.rs index 81bd8a0c..a0f1de9d 100644 --- a/fuzz/fuzz_targets/fuzz_arithmetic.rs +++ b/fuzz/fuzz_targets/fuzz_arithmetic.rs @@ -6,7 +6,7 @@ use winnow::prelude::*; use winnow::{ ascii::{digit1 as digit, space0 as space}, combinator::alt, - combinator::fold_many0, + combinator::fold_repeat0, combinator::{delimited, terminated}, }; @@ -64,7 +64,7 @@ fn term(i: &str) -> IResult<&str, i64> { e })?; - let res = fold_many0( + let res = fold_repeat0( alt((('*', factor), ('/', factor.verify(|i| *i != 0)))), || init, |acc, (op, val): (char, i64)| { @@ -93,7 +93,7 @@ fn expr(i: &str) -> IResult<&str, i64> { e })?; - let res = fold_many0( + let res = fold_repeat0( (alt(('+', '-')), term), || init, |acc, (op, val): (char, i64)| { diff --git a/src/_topic/partial.rs b/src/_topic/partial.rs index 6d884fc0..ecb55ab1 100644 --- a/src/_topic/partial.rs +++ b/src/_topic/partial.rs @@ -21,7 +21,7 @@ //! Caveats: //! - `winnow` takes the approach of re-parsing from scratch. Chunks should be relatively small to //! prevent the re-parsing overhead from dominating. -//! - Parsers like [`many0`] do not know when an `eof` is from insufficient data or the end of the +//! - Parsers like [`repeat0`] do not know when an `eof` is from insufficient data or the end of the //! stream, causing them to always report [`Incomplete`]. //! //! # Example @@ -39,7 +39,7 @@ #![allow(unused_imports)] // Used for intra-doc links use crate::binary::length_value; -use crate::combinator::many0; +use crate::combinator::repeat0; use crate::error::ErrMode::Incomplete; use crate::error::Needed; use crate::stream::Partial; diff --git a/src/_tutorial/chapter_5.rs b/src/_tutorial/chapter_5.rs index 85078543..9c8374e7 100644 --- a/src/_tutorial/chapter_5.rs +++ b/src/_tutorial/chapter_5.rs @@ -1,9 +1,9 @@ //! # Chapter 5: Repetition //! //! In [`chapter_3`], we covered how to sequence different parsers into a tuple but sometimes you need to run a -//! single parser many times into a [`Vec`]. +//! single parser multiple times, collecting the result into a [`Vec`]. //! -//! Let's take our `parse_digits` and collect a list of them with [`many0`]: +//! Let's take our `parse_digits` and collect a list of them with [`repeat0`]: //! ```rust //! # use winnow::IResult; //! # use winnow::Parser; @@ -12,11 +12,11 @@ //! # use winnow::token::take; //! # use winnow::combinator::fail; //! use winnow::combinator::opt; -//! use winnow::combinator::many0; +//! use winnow::combinator::repeat0; //! use winnow::combinator::terminated; //! //! fn parse_list(input: &str) -> IResult<&str, Vec> { -//! many0(terminated(parse_digits, opt(','))).parse_next(input) +//! repeat0(terminated(parse_digits, opt(','))).parse_next(input) //! } //! //! // ... @@ -132,7 +132,7 @@ //! } //! ``` //! -//! If you look closely at [`many0`], it isn't collecting directly into a [`Vec`] but +//! If you look closely at [`repeat0`], it isn't collecting directly into a [`Vec`] but //! [`Accumulate`] to gather the results. This let's us make more complex parsers than we did in //! [`chapter_2`] by accumulating the results into a `()` and [`recognize`][Parser::recognize]-ing the captured input: //! ```rust @@ -204,7 +204,7 @@ #![allow(unused_imports)] use super::chapter_2; use super::chapter_3; -use crate::combinator::many0; +use crate::combinator::repeat0; use crate::combinator::separated0; use crate::stream::Accumulate; use crate::Parser; diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index 9eea286b..f5f6f589 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -40,12 +40,12 @@ //! | combinator | usage | input | output | comment | //! |---|---|---|---|---| //! | [`count`][crate::combinator::count] | `count(take(2), 3)` | `"abcdefgh"` | `Ok(("gh", vec!["ab", "cd", "ef"]))` |Applies the child parser a specified number of times| -//! | [`many0`][crate::combinator::many0] | `many0("ab")` | `"abababc"` | `Ok(("c", vec!["ab", "ab", "ab"]))` |Applies the parser 0 or more times and returns the list of results in a Vec. `many1` does the same operation but must return at least one element| -//! | [`many_m_n`][crate::combinator::many_m_n] | `many_m_n(1, 3, "ab")` | `"ababc"` | `Ok(("c", vec!["ab", "ab"]))` |Applies the parser between m and n times (n included) and returns the list of results in a Vec| -//! | [`many_till0`][crate::combinator::many_till0] | `many_till0(tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `Ok(("g", (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| +//! | [`repeat0`][crate::combinator::repeat0] | `repeat0("ab")` | `"abababc"` | `Ok(("c", vec!["ab", "ab", "ab"]))` |Applies the parser 0 or more times and returns the list of results in a Vec. `repeat1` does the same operation but must return at least one element| +//! | [`repeat_m_n`][crate::combinator::repeat_m_n] | `repeat_m_n(1, 3, "ab")` | `"ababc"` | `Ok(("c", vec!["ab", "ab"]))` |Applies the parser between m and n times (n included) and returns the list of results in a Vec| +//! | [`repeat_till0`][crate::combinator::repeat_till0] | `repeat_till0(tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `Ok(("g", (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| //! | [`separated0`][crate::combinator::separated0] | `separated0("ab", ",")` | `"ab,ab,ab."` | `Ok((".", vec!["ab", "ab", "ab"]))` |`separated1` works like `separated0` but must returns at least one element| -//! | [`fold_many0`][crate::combinator::fold_many0] | `fold_many0(be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([], 6))` |Applies the parser 0 or more times and folds the list of return values. The `fold_many1` version must apply the child parser at least one time| -//! | [`fold_many_m_n`][crate::combinator::fold_many_m_n] | `fold_many_m_n(1, 2, be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([3], 3))` |Applies the parser between m and n times (n included) and folds the list of return value| +//! | [`fold_repeat0`][crate::combinator::fold_repeat0] | `fold_repeat0(be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([], 6))` |Applies the parser 0 or more times and folds the list of return values. The `fold_repeat1` version must apply the child parser at least one time| +//! | [`fold_repeat_m_n`][crate::combinator::fold_repeat_m_n] | `fold_repeat_m_n(1, 2, be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([3], 3))` |Applies the parser between m and n times (n included) and folds the list of return value| //! //! ## Partial related //! diff --git a/src/combinator/multi.rs b/src/combinator/multi.rs index 4b7e5fc5..2e62648b 100644 --- a/src/combinator/multi.rs +++ b/src/combinator/multi.rs @@ -15,7 +15,7 @@ use crate::Parser; /// /// To recognize a series of tokens, [`Accumulate`] into a `()` and then [`Parser::recognize`]. /// -/// **Warning:** if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `many0` will +/// **Warning:** if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `repeat0` will /// return an error, to prevent going into an infinite loop /// /// # Example @@ -24,11 +24,11 @@ use crate::Parser; /// # #[cfg(feature = "std")] { /// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::many0; +/// use winnow::combinator::repeat0; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// many0("abc").parse_next(s) +/// repeat0("abc").parse_next(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -40,14 +40,15 @@ use crate::Parser; #[doc(alias = "skip_many")] #[doc(alias = "repeated")] #[doc(alias = "many0_count")] -pub fn many0(mut f: F) -> impl Parser +#[doc(alias = "many0")] +pub fn repeat0(mut f: F) -> impl Parser where I: Stream, C: Accumulate, F: Parser, E: ParseError, { - trace("many0", move |mut i: I| { + trace("repeat0", move |mut i: I| { let mut acc = C::initial(None); loop { let len = i.eof_offset(); @@ -57,7 +58,7 @@ where Ok((i1, o)) => { // infinite loop check: the parser must always consume if i1.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } i = i1; @@ -78,8 +79,8 @@ where /// /// To recognize a series of tokens, [`Accumulate`] into a `()` and then [`Parser::recognize`]. /// -/// **Warning:** If the parser passed to `many1` accepts empty inputs -/// (like `alpha0` or `digit0`), `many1` will return an error, +/// **Warning:** If the parser passed to `repeat1` accepts empty inputs +/// (like `alpha0` or `digit0`), `repeat1` will return an error, /// to prevent going into an infinite loop. /// /// # Example @@ -88,11 +89,11 @@ where /// # #[cfg(feature = "std")] { /// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::many1; +/// use winnow::combinator::repeat1; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// many1("abc").parse_next(s) +/// repeat1("abc").parse_next(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -104,14 +105,15 @@ where #[doc(alias = "skip_many1")] #[doc(alias = "repeated")] #[doc(alias = "many1_count")] -pub fn many1(mut f: F) -> impl Parser +#[doc(alias = "many1")] +pub fn repeat1(mut f: F) -> impl Parser where I: Stream, C: Accumulate, F: Parser, E: ParseError, { - trace("many1", move |mut i: I| match f.parse_next(i.clone()) { + trace("repeat1", move |mut i: I| match f.parse_next(i.clone()) { Err(e) => Err(e.append(i, ErrorKind::Many)), Ok((i1, o)) => { let mut acc = C::initial(None); @@ -126,7 +128,7 @@ where Ok((i1, o)) => { // infinite loop check: the parser must always consume if i1.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } i = i1; @@ -153,11 +155,11 @@ where /// # #[cfg(feature = "std")] { /// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::many_till0; +/// use winnow::combinator::repeat_till0; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> { -/// many_till0("abc", "end").parse_next(s) +/// repeat_till0("abc", "end").parse_next(s) /// }; /// /// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end")))); @@ -167,7 +169,8 @@ where /// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end")))); /// # } /// ``` -pub fn many_till0(mut f: F, mut g: G) -> impl Parser +#[doc(alias = "many_till0")] +pub fn repeat_till0(mut f: F, mut g: G) -> impl Parser where I: Stream, C: Accumulate, @@ -175,7 +178,7 @@ where G: Parser, E: ParseError, { - trace("many_till0", move |mut i: I| { + trace("repeat_till0", move |mut i: I| { let mut res = C::initial(None); loop { let len = i.eof_offset(); @@ -187,7 +190,10 @@ where Ok((i1, o)) => { // infinite loop check: the parser must always consume if i1.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert( + i, + "`repeat` parsers must always consume", + )); } res.accumulate(o); @@ -399,7 +405,7 @@ where Ok((i1, s)) => { // infinite loop check: the parser must always consume if i1.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } match parser.parse_next(i1.clone()) { @@ -454,7 +460,7 @@ where trace("separated_foldr1", move |i: I| { let (i, ol) = parser.parse_next(i)?; let (i, all): (_, crate::lib::std::vec::Vec<(O2, O)>) = - many0((sep.by_ref(), parser.by_ref())).parse_next(i)?; + repeat0((sep.by_ref(), parser.by_ref())).parse_next(i)?; if let Some((s, or)) = all .into_iter() .rev() @@ -480,8 +486,8 @@ where /// /// To recognize a series of tokens, [`Accumulate`] into a `()` and then [`Parser::recognize`]. /// -/// **Warning:** If the parser passed to `many1` accepts empty inputs -/// (like `alpha0` or `digit0`), `many1` will return an error, +/// **Warning:** If the parser passed to `repeat_m_n` accepts empty inputs +/// (like `alpha0` or `digit0`), `repeat_m_n` will return an error, /// to prevent going into an infinite loop. /// /// # Example @@ -490,11 +496,11 @@ where /// # #[cfg(feature = "std")] { /// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::many_m_n; +/// use winnow::combinator::repeat_m_n; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// many_m_n(0, 2, "abc").parse_next(s) +/// repeat_m_n(0, 2, "abc").parse_next(s) /// } /// /// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); @@ -505,14 +511,15 @@ where /// # } /// ``` #[doc(alias = "repeated")] -pub fn many_m_n(min: usize, max: usize, mut parse: F) -> impl Parser +#[doc(alias = "many_m_n")] +pub fn repeat_m_n(min: usize, max: usize, mut parse: F) -> impl Parser where I: Stream, C: Accumulate, F: Parser, E: ParseError, { - trace("many_m_n", move |mut input: I| { + trace("repeat_m_n", move |mut input: I| { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); } @@ -524,7 +531,10 @@ where Ok((tail, value)) => { // infinite loop check: the parser must always consume if tail.eof_offset() == len { - return Err(ErrMode::assert(input, "many parsers must always consume")); + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } res.accumulate(value); @@ -669,7 +679,8 @@ where /// * `g` The function that combines a result of `f` with /// the current accumulator. /// -/// **Warning:** if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `many0` will +/// **Warning:** if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), +/// `fold_repeat0` will /// return an error, to prevent going into an infinite loop /// /// # Example @@ -677,11 +688,11 @@ where /// ```rust /// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::fold_many0; +/// use winnow::combinator::fold_repeat0; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// fold_many0( +/// fold_repeat0( /// "abc", /// Vec::new, /// |mut acc: Vec<_>, item| { @@ -696,7 +707,8 @@ where /// assert_eq!(parser("123123"), Ok(("123123", vec![]))); /// assert_eq!(parser(""), Ok(("", vec![]))); /// ``` -pub fn fold_many0(mut f: F, mut init: H, mut g: G) -> impl Parser +#[doc(alias = "fold_many0")] +pub fn fold_repeat0(mut f: F, mut init: H, mut g: G) -> impl Parser where I: Stream, F: Parser, @@ -704,7 +716,7 @@ where H: FnMut() -> R, E: ParseError, { - trace("fold_many0", move |i: I| { + trace("fold_repeat0", move |i: I| { let mut res = init(); let mut input = i; @@ -715,7 +727,7 @@ where Ok((i, o)) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); } res = g(res, o); @@ -743,8 +755,8 @@ where /// * `g` The function that combines a result of `f` with /// the current accumulator. /// -/// **Warning:** If the parser passed to `many1` accepts empty inputs -/// (like `alpha0` or `digit0`), `many1` will return an error, +/// **Warning:** If the parser passed to `fold_repeat1` accepts empty inputs +/// (like `alpha0` or `digit0`), `fold_repeat1` will return an error, /// to prevent going into an infinite loop. /// /// # Example @@ -752,11 +764,11 @@ where /// ```rust /// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::fold_many1; +/// use winnow::combinator::fold_repeat1; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// fold_many1( +/// fold_repeat1( /// "abc", /// Vec::new, /// |mut acc: Vec<_>, item| { @@ -771,7 +783,8 @@ where /// assert_eq!(parser("123123"), Err(ErrMode::Backtrack(Error::new("123123", ErrorKind::Many)))); /// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Many)))); /// ``` -pub fn fold_many1(mut f: F, mut init: H, mut g: G) -> impl Parser +#[doc(alias = "fold_many1")] +pub fn fold_repeat1(mut f: F, mut init: H, mut g: G) -> impl Parser where I: Stream, F: Parser, @@ -779,7 +792,7 @@ where H: FnMut() -> R, E: ParseError, { - trace("fold_many1", move |i: I| { + trace("fold_repeat1", move |i: I| { let _i = i.clone(); let init = init(); match f.parse_next(_i) { @@ -800,7 +813,10 @@ where Ok((i, o)) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "many parsers must always consume")); + return Err(ErrMode::assert( + i, + "`repeat` parsers must always consume", + )); } acc = g(acc, o); @@ -828,8 +844,8 @@ where /// * `g` The function that combines a result of `f` with /// the current accumulator. /// -/// **Warning:** If the parser passed to `many1` accepts empty inputs -/// (like `alpha0` or `digit0`), `many1` will return an error, +/// **Warning:** If the parser passed to `fold_repeat_m_n` accepts empty inputs +/// (like `alpha0` or `digit0`), `fold_repeat_m_n` will return an error, /// to prevent going into an infinite loop. /// /// # Example @@ -837,11 +853,11 @@ where /// ```rust /// # use winnow::{error::ErrMode, error::ErrorKind, error::Needed}; /// # use winnow::prelude::*; -/// use winnow::combinator::fold_many_m_n; +/// use winnow::combinator::fold_repeat_m_n; /// use winnow::token::tag; /// /// fn parser(s: &str) -> IResult<&str, Vec<&str>> { -/// fold_many_m_n( +/// fold_repeat_m_n( /// 0, /// 2, /// "abc", @@ -859,7 +875,8 @@ where /// assert_eq!(parser(""), Ok(("", vec![]))); /// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); /// ``` -pub fn fold_many_m_n( +#[doc(alias = "fold_many_m_n")] +pub fn fold_repeat_m_n( min: usize, max: usize, mut parse: F, @@ -873,7 +890,7 @@ where H: FnMut() -> R, E: ParseError, { - trace("fold_many_m_n", move |mut input: I| { + trace("fold_repeat_m_n", move |mut input: I| { if min > max { return Err(ErrMode::Cut(E::from_error_kind(input, ErrorKind::Many))); } @@ -885,7 +902,10 @@ where Ok((tail, value)) => { // infinite loop check: the parser must always consume if tail.eof_offset() == len { - return Err(ErrMode::assert(input, "many parsers must always consume")); + return Err(ErrMode::assert( + input, + "`repeat` parsers must always consume", + )); } acc = fold(acc, value); diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index 9558d593..be383a51 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -815,9 +815,9 @@ fn separated1_test() { #[test] #[cfg(feature = "alloc")] -fn many0_test() { +fn repeat0_test() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many0("abcd").parse_next(i) + repeat0("abcd").parse_next(i) } assert_eq!( @@ -849,9 +849,9 @@ fn many0_test() { #[test] #[cfg(feature = "alloc")] #[cfg_attr(debug_assertions, should_panic)] -fn many0_empty_test() { +fn repeat0_empty_test() { fn multi_empty(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many0("").parse_next(i) + repeat0("").parse_next(i) } assert_eq!( @@ -865,9 +865,9 @@ fn many0_empty_test() { #[test] #[cfg(feature = "alloc")] -fn many1_test() { +fn repeat1_test() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many1("abcd").parse_next(i) + repeat1("abcd").parse_next(i) } let a = &b"abcdef"[..]; @@ -897,10 +897,10 @@ fn many1_test() { #[test] #[cfg(feature = "alloc")] -fn many_till_test() { +fn repeat_till_test() { #[allow(clippy::type_complexity)] fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> { - many_till0("abcd", "efgh").parse_next(i) + repeat_till0("abcd", "efgh").parse_next(i) } let a = b"abcdabcdefghabcd"; @@ -931,13 +931,13 @@ fn infinite_many() { // should not go into an infinite loop fn multi0(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - many0(tst).parse_next(i) + repeat0(tst).parse_next(i) } let a = &b"abcdef"[..]; assert_eq!(multi0(a), Ok((a, Vec::new()))); fn multi1(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { - many1(tst).parse_next(i) + repeat1(tst).parse_next(i) } let a = &b"abcdef"[..]; assert_eq!( @@ -948,9 +948,9 @@ fn infinite_many() { #[test] #[cfg(feature = "alloc")] -fn many_m_n_test() { +fn repeat_m_n_test() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many_m_n(2, 4, "Abcd").parse_next(i) + repeat_m_n(2, 4, "Abcd").parse_next(i) } let a = &b"Abcdef"[..]; @@ -1089,13 +1089,13 @@ impl ParseError for NilError { #[test] #[cfg(feature = "alloc")] -fn fold_many0_test() { +fn fold_repeat0_test() { fn fold_into_vec(mut acc: Vec, item: T) -> Vec { acc.push(item); acc } fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - fold_many0("abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat0("abcd", Vec::new, fold_into_vec).parse_next(i) } assert_eq!( @@ -1127,13 +1127,13 @@ fn fold_many0_test() { #[test] #[cfg(feature = "alloc")] #[cfg_attr(debug_assertions, should_panic)] -fn fold_many0_empty_test() { +fn fold_repeat0_empty_test() { fn fold_into_vec(mut acc: Vec, item: T) -> Vec { acc.push(item); acc } fn multi_empty(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - fold_many0("", Vec::new, fold_into_vec).parse_next(i) + fold_repeat0("", Vec::new, fold_into_vec).parse_next(i) } assert_eq!( @@ -1147,13 +1147,13 @@ fn fold_many0_empty_test() { #[test] #[cfg(feature = "alloc")] -fn fold_many1_test() { +fn fold_repeat1_test() { fn fold_into_vec(mut acc: Vec, item: T) -> Vec { acc.push(item); acc } fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - fold_many1("abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat1("abcd", Vec::new, fold_into_vec).parse_next(i) } let a = &b"abcdef"[..]; @@ -1183,13 +1183,13 @@ fn fold_many1_test() { #[test] #[cfg(feature = "alloc")] -fn fold_many_m_n_test() { +fn fold_repeat_m_n_test() { fn fold_into_vec(mut acc: Vec, item: T) -> Vec { acc.push(item); acc } fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - fold_many_m_n(2, 4, "Abcd", Vec::new, fold_into_vec).parse_next(i) + fold_repeat_m_n(2, 4, "Abcd", Vec::new, fold_into_vec).parse_next(i) } let a = &b"Abcdef"[..]; @@ -1227,9 +1227,9 @@ fn fold_many_m_n_test() { } #[test] -fn many0_count_test() { +fn repeat0_count_test() { fn count0_nums(i: &[u8]) -> IResult<&[u8], usize> { - many0((digit, ",")).parse_next(i) + repeat0((digit, ",")).parse_next(i) } assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); @@ -1245,9 +1245,9 @@ fn many0_count_test() { } #[test] -fn many1_count_test() { +fn repeat1_count_test() { fn count1_nums(i: &[u8]) -> IResult<&[u8], usize> { - many1((digit, ",")).parse_next(i) + repeat1((digit, ",")).parse_next(i) } assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); diff --git a/src/multi.rs b/src/multi.rs index b1781881..653c40dd 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -8,8 +8,8 @@ use crate::stream::Accumulate; use crate::stream::{Stream, StreamIsPartial, ToUsize, UpdateSlice}; use crate::Parser; -/// Deprecated, replaced by [`combinator::many0`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::many0`")] +/// Deprecated, replaced by [`combinator::repeat0`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat0`")] #[inline(always)] pub fn many0(f: F) -> impl Parser where @@ -18,11 +18,11 @@ where F: Parser, E: ParseError, { - combinator::many0(f) + combinator::repeat0(f) } -/// Deprecated, replaced by [`combinator::many1`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::many1`")] +/// Deprecated, replaced by [`combinator::repeat1`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat1`")] #[inline(always)] pub fn many1(f: F) -> impl Parser where @@ -31,11 +31,11 @@ where F: Parser, E: ParseError, { - combinator::many1(f) + combinator::repeat1(f) } -/// Deprecated, replaced by [`combinator::many_till0`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::many_till0`")] +/// Deprecated, replaced by [`combinator::repeat_till0`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat_till0`")] #[inline(always)] pub fn many_till0(f: F, g: G) -> impl Parser where @@ -45,7 +45,7 @@ where G: Parser, E: ParseError, { - combinator::many_till0(f, g) + combinator::repeat_till0(f, g) } /// Deprecated, replaced by [`combinator::separated0`] @@ -105,8 +105,8 @@ where combinator::separated_foldr1(parser, sep, op) } -/// Deprecated, replaced by [`combinator::many_m_n`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::many_m_n`")] +/// Deprecated, replaced by [`combinator::repeat_m_n`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::repeat_m_n`")] #[inline(always)] pub fn many_m_n(min: usize, max: usize, parse: F) -> impl Parser where @@ -115,7 +115,7 @@ where F: Parser, E: ParseError, { - combinator::many_m_n(min, max, parse) + combinator::repeat_m_n(min, max, parse) } /// Deprecated, replaced by [`combinator::count`] @@ -143,8 +143,8 @@ where combinator::fill(f, buf) } -/// Deprecated, replaced by [`combinator::fold_many0`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_many0`")] +/// Deprecated, replaced by [`combinator::fold_repeat0`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat0`")] #[inline(always)] pub fn fold_many0(f: F, init: H, g: G) -> impl Parser where @@ -154,11 +154,11 @@ where H: FnMut() -> R, E: ParseError, { - combinator::fold_many0(f, init, g) + combinator::fold_repeat0(f, init, g) } -/// Deprecated, replaced by [`combinator::fold_many1`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_many1`")] +/// Deprecated, replaced by [`combinator::fold_repeat1`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat1`")] #[inline(always)] pub fn fold_many1(f: F, init: H, g: G) -> impl Parser where @@ -168,11 +168,11 @@ where H: FnMut() -> R, E: ParseError, { - combinator::fold_many1(f, init, g) + combinator::fold_repeat1(f, init, g) } -/// Deprecated, replaced by [`combinator::fold_many_m_n`] -#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_many_m_n`")] +/// Deprecated, replaced by [`combinator::fold_repeat_m_n`] +#[deprecated(since = "0.4.2", note = "Replaced with `combinator::fold_repeat_m_n`")] #[inline(always)] pub fn fold_many_m_n( min: usize, @@ -188,7 +188,7 @@ where H: FnMut() -> R, E: ParseError, { - combinator::fold_many_m_n(min, max, parse, init, fold) + combinator::fold_repeat_m_n(min, max, parse, init, fold) } /// Deprecated, replaced by [`binary::length_data`] diff --git a/src/token/mod.rs b/src/token/mod.rs index 8418b893..774cb333 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -387,7 +387,7 @@ where /// /// *Partial version*: will return a `ErrMode::Incomplete(Needed::new(1))` if the pattern reaches the end of the input. /// -/// To recognize a series of tokens, use [`many0`][crate::combinator::many0] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. +/// To recognize a series of tokens, use [`repeat0`][crate::combinator::repeat0] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. /// /// # Example /// @@ -469,7 +469,7 @@ where /// /// *Partial version* will return a `ErrMode::Incomplete(Needed::new(1))` or if the pattern reaches the end of the input. /// -/// To recognize a series of tokens, use [`many1`][crate::combinator::many1] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. +/// To recognize a series of tokens, use [`repeat1`][crate::combinator::repeat1] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. /// /// # Example /// @@ -573,7 +573,7 @@ where /// /// *Partial version* will return a `ErrMode::Incomplete(Needed::new(1))` if the pattern reaches the end of the input or is too short. /// -/// To recognize a series of tokens, use [`many_m_n`][crate::combinator::many_m_n] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. +/// To recognize a series of tokens, use [`repeat_m_n`][crate::combinator::repeat_m_n] to [`Accumulate`][crate::stream::Accumulate] into a `()` and then [`Parser::recognize`][crate::Parser::recognize]. /// /// # Example /// diff --git a/tests/testsuite/fnmut.rs b/tests/testsuite/fnmut.rs index 6e34ba89..feb6d5e4 100644 --- a/tests/testsuite/fnmut.rs +++ b/tests/testsuite/fnmut.rs @@ -1,6 +1,6 @@ #![cfg(feature = "alloc")] -use winnow::combinator::many0; +use winnow::combinator::repeat0; use winnow::Parser; #[test] @@ -9,7 +9,7 @@ fn parse() { let mut counter = 0; let res = { - let mut parser = many0::<_, _, Vec<_>, (), _>(|i| { + let mut parser = repeat0::<_, _, Vec<_>, (), _>(|i| { counter += 1; "abc".parse_next(i) }); @@ -26,7 +26,7 @@ fn accumulate() { let mut v = Vec::new(); let (_, count) = { - let mut parser = many0::<_, _, usize, (), _>(|i| { + let mut parser = repeat0::<_, _, usize, (), _>(|i| { let (i, o) = "abc".parse_next(i)?; v.push(o); Ok((i, ())) diff --git a/tests/testsuite/issues.rs b/tests/testsuite/issues.rs index 7d7d935c..f473b816 100644 --- a/tests/testsuite/issues.rs +++ b/tests/testsuite/issues.rs @@ -27,13 +27,13 @@ mod parse_int { use winnow::Partial; use winnow::{ ascii::{digit1 as digit, space1 as space}, - combinator::many0, combinator::opt, + combinator::repeat0, IResult, }; fn parse_ints(input: Partial<&[u8]>) -> IResult, Vec> { - many0(spaces_or_int).parse_next(input) + repeat0(spaces_or_int).parse_next(input) } fn spaces_or_int(input: Partial<&[u8]>) -> IResult, i32> { @@ -187,8 +187,8 @@ fn issue_942() { pub fn parser<'a, E: ParseError<&'a str> + ContextError<&'a str, &'static str>>( i: &'a str, ) -> IResult<&'a str, usize, E> { - use winnow::combinator::many0; - many0('a'.context("char_a")).parse_next(i) + use winnow::combinator::repeat0; + repeat0('a'.context("char_a")).parse_next(i) } assert_eq!(parser::<()>("aaa"), Ok(("", 3))); } @@ -196,8 +196,8 @@ fn issue_942() { #[test] #[cfg(feature = "std")] fn issue_many_m_n_with_zeros() { - use winnow::combinator::many_m_n; - let mut parser = many_m_n::<_, _, Vec<_>, (), _>(0, 0, 'a'); + use winnow::combinator::repeat_m_n; + let mut parser = repeat_m_n::<_, _, Vec<_>, (), _>(0, 0, 'a'); assert_eq!(parser.parse_next("aaa"), Ok(("aaa", vec![]))); } @@ -265,8 +265,8 @@ fn issue_x_looser_fill_bounds() { #[cfg(feature = "std")] fn issue_1459_clamp_capacity() { // shouldn't panic - use winnow::combinator::many_m_n; - let mut parser = many_m_n::<_, _, Vec<_>, (), _>(usize::MAX, usize::MAX, 'a'); + use winnow::combinator::repeat_m_n; + let mut parser = repeat_m_n::<_, _, Vec<_>, (), _>(usize::MAX, usize::MAX, 'a'); assert_eq!( parser.parse_next("a"), Err(winnow::error::ErrMode::Backtrack(())) diff --git a/tests/testsuite/multiline.rs b/tests/testsuite/multiline.rs index c07fd911..9be54764 100644 --- a/tests/testsuite/multiline.rs +++ b/tests/testsuite/multiline.rs @@ -2,7 +2,7 @@ use winnow::{ ascii::{alphanumeric1 as alphanumeric, line_ending as eol}, - combinator::many0, + combinator::repeat0, combinator::terminated, IResult, Parser, }; @@ -20,7 +20,7 @@ pub fn read_line(input: &str) -> IResult<&str, &str> { } pub fn read_lines(input: &str) -> IResult<&str, Vec<&str>> { - many0(read_line).parse_next(input) + repeat0(read_line).parse_next(input) } #[cfg(feature = "alloc")] diff --git a/tests/testsuite/overflow.rs b/tests/testsuite/overflow.rs index 9aa91d69..fe6d5faa 100644 --- a/tests/testsuite/overflow.rs +++ b/tests/testsuite/overflow.rs @@ -5,7 +5,7 @@ use winnow::binary::be_u64; use winnow::binary::length_data; #[cfg(feature = "alloc")] -use winnow::combinator::many0; +use winnow::combinator::repeat0; use winnow::error::{ErrMode, Needed}; use winnow::prelude::*; use winnow::token::take; @@ -31,7 +31,7 @@ fn overflow_incomplete_tuple() { #[cfg(feature = "alloc")] fn overflow_incomplete_length_bytes() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many0(length_data(be_u64)).parse_next(i) + repeat0(length_data(be_u64)).parse_next(i) } // Trigger an overflow in length_data @@ -47,10 +47,10 @@ fn overflow_incomplete_length_bytes() { #[cfg(feature = "alloc")] fn overflow_incomplete_many0() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many0(length_data(be_u64)).parse_next(i) + repeat0(length_data(be_u64)).parse_next(i) } - // Trigger an overflow in many0 + // Trigger an overflow in repeat0 assert_eq!( multi(Partial::new( &b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..] @@ -62,13 +62,13 @@ fn overflow_incomplete_many0() { #[test] #[cfg(feature = "alloc")] fn overflow_incomplete_many1() { - use winnow::combinator::many1; + use winnow::combinator::repeat1; fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many1(length_data(be_u64)).parse_next(i) + repeat1(length_data(be_u64)).parse_next(i) } - // Trigger an overflow in many1 + // Trigger an overflow in repeat1 assert_eq!( multi(Partial::new( &b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..] @@ -80,14 +80,14 @@ fn overflow_incomplete_many1() { #[test] #[cfg(feature = "alloc")] fn overflow_incomplete_many_till0() { - use winnow::combinator::many_till0; + use winnow::combinator::repeat_till0; #[allow(clippy::type_complexity)] fn multi(i: Partial<&[u8]>) -> IResult, (Vec<&[u8]>, &[u8])> { - many_till0(length_data(be_u64), "abc").parse_next(i) + repeat_till0(length_data(be_u64), "abc").parse_next(i) } - // Trigger an overflow in many_till0 + // Trigger an overflow in repeat_till0 assert_eq!( multi(Partial::new( &b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..] @@ -99,13 +99,13 @@ fn overflow_incomplete_many_till0() { #[test] #[cfg(feature = "alloc")] fn overflow_incomplete_many_m_n() { - use winnow::combinator::many_m_n; + use winnow::combinator::repeat_m_n; fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many_m_n(2, 4, length_data(be_u64)).parse_next(i) + repeat_m_n(2, 4, length_data(be_u64)).parse_next(i) } - // Trigger an overflow in many_m_n + // Trigger an overflow in repeat_m_n assert_eq!( multi(Partial::new( &b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..] @@ -153,7 +153,7 @@ fn overflow_incomplete_length_count() { #[cfg(feature = "alloc")] fn overflow_incomplete_length_data() { fn multi(i: Partial<&[u8]>) -> IResult, Vec<&[u8]>> { - many0(length_data(be_u64)).parse_next(i) + repeat0(length_data(be_u64)).parse_next(i) } assert_eq!( diff --git a/tests/testsuite/reborrow_fold.rs b/tests/testsuite/reborrow_fold.rs index ff974e0c..0c77d8d9 100644 --- a/tests/testsuite/reborrow_fold.rs +++ b/tests/testsuite/reborrow_fold.rs @@ -4,7 +4,7 @@ use std::str; use winnow::combinator::delimited; -use winnow::combinator::fold_many0; +use winnow::combinator::fold_repeat0; use winnow::prelude::*; use winnow::token::take_till1; use winnow::IResult; @@ -22,7 +22,7 @@ fn atom(_tomb: &mut ()) -> impl for<'a> FnMut(&'a [u8]) -> IResult<&'a [u8], Str fn list<'a>(i: &'a [u8], tomb: &mut ()) -> IResult<&'a [u8], String> { delimited( '(', - fold_many0(atom(tomb), String::new, |mut acc: String, next: String| { + fold_repeat0(atom(tomb), String::new, |mut acc: String, next: String| { acc.push_str(next.as_str()); acc }), diff --git a/tests/testsuite/utf8.rs b/tests/testsuite/utf8.rs index 3971c792..887aac89 100644 --- a/tests/testsuite/utf8.rs +++ b/tests/testsuite/utf8.rs @@ -3,7 +3,7 @@ mod test { use winnow::Parser; use winnow::Partial; #[cfg(feature = "alloc")] - use winnow::{combinator::alt, combinator::many1, token::tag_no_case}; + use winnow::{combinator::alt, combinator::repeat1, token::tag_no_case}; use winnow::{ error::ErrMode, error::{self, Error, ErrorKind}, @@ -505,7 +505,7 @@ mod test { let b = "ababcd"; fn f(i: &str) -> IResult<&str, &str> { - many1::<_, _, (), _, _>(alt(("a", "b"))) + repeat1::<_, _, (), _, _>(alt(("a", "b"))) .recognize() .parse_next(i) }