Skip to content

Commit

Permalink
Merge pull request #240 from epage/name
Browse files Browse the repository at this point in the history
fix(multi): Rename 'many' as 'repeat'
  • Loading branch information
epage committed Apr 28, 2023
2 parents 65cbf5b + 3d2ef56 commit b3aabcc
Show file tree
Hide file tree
Showing 32 changed files with 238 additions and 217 deletions.
4 changes: 2 additions & 2 deletions assets/trace.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions benches/contains_token.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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> {
Expand All @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions examples/arithmetic/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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)| {
Expand All @@ -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)| {
Expand Down
6 changes: 3 additions & 3 deletions examples/arithmetic/parser_ast.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, multispace0 as multispace},
combinator::alt,
combinator::many0,
combinator::repeat0,
combinator::{delimited, preceded},
IResult,
};
Expand Down Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
6 changes: 3 additions & 3 deletions examples/http/parser.rs
Original file line number Diff line number Diff line change
@@ -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];

Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn parse(data: &[u8]) -> Option<Vec<(Request<'_>, Vec<Header<'_>>)>> {

fn request(input: Stream<'_>) -> IResult<Stream<'_>, (Request<'_>, Vec<Header<'_>>)> {
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)))
Expand Down Expand Up @@ -86,7 +86,7 @@ fn message_header_value(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
fn message_header(input: Stream<'_>) -> IResult<Stream<'_>, 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 }))
}
Expand Down
6 changes: 3 additions & 3 deletions examples/http/parser_streaming.rs
Original file line number Diff line number Diff line change
@@ -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]>;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn parse(data: &[u8]) -> Option<Vec<(Request<'_>, Vec<Header<'_>>)>> {

fn request(input: Stream<'_>) -> IResult<Stream<'_>, (Request<'_>, Vec<Header<'_>>)> {
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)))
Expand Down Expand Up @@ -88,7 +88,7 @@ fn message_header_value(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
fn message_header(input: Stream<'_>) -> IResult<Stream<'_>, 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 }))
}
Expand Down
4 changes: 2 additions & 2 deletions examples/ini/bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winnow::combinator::many0;
use winnow::combinator::repeat0;
use winnow::prelude::*;

mod parser;
Expand Down Expand Up @@ -32,7 +32,7 @@ file=payroll.dat
\0";

fn acc(i: parser::Stream<'_>) -> IResult<parser::Stream<'_>, 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");
Expand Down
6 changes: 3 additions & 3 deletions examples/ini/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ 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,
};

pub type Stream<'i> = &'i [u8];

pub fn categories(i: Stream<'_>) -> IResult<Stream<'_>, 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)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ 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},
};

pub type Stream<'i> = &'i str;

pub fn categories(input: Stream<'_>) -> IResult<Stream<'_>, 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<Stream<'_>, (&str, HashMap<&str, &str>)> {
Expand All @@ -28,7 +28,7 @@ fn category(i: Stream<'_>) -> IResult<Stream<'_>, &str> {
}

fn keys_and_values(input: Stream<'_>) -> IResult<Stream<'_>, HashMap<&str, &str>> {
many0(key_value).parse_next(input)
repeat0(key_value).parse_next(input)
}

fn key_value(i: Stream<'_>) -> IResult<Stream<'_>, (&str, &str)> {
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -87,7 +87,7 @@ fn string<'i, E: ParseError<Stream<'i>> + ContextError<Stream<'i>, &'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
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -96,7 +96,7 @@ fn string<'i, E: ParseError<Stream<'i>> + ContextError<Stream<'i>, &'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
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -88,7 +88,7 @@ fn string<'i, E: ParseError<Stream<'i>> + ContextError<Stream<'i>, &'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
}),
Expand Down
4 changes: 2 additions & 2 deletions examples/ndjson/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -92,7 +92,7 @@ fn string<'i, E: ParseError<Stream<'i>> + ContextError<Stream<'i>, &'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
}),
Expand Down
8 changes: 4 additions & 4 deletions examples/s_expression/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/string/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/fuzz_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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)| {
Expand Down Expand Up @@ -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)| {
Expand Down

0 comments on commit b3aabcc

Please sign in to comment.