pub struct Repeat<P, I, O, C, E>{ /* private fields */ }
Expand description
Implementation of repeat
Implementations§
Source§impl<ParseNext, Input, Output, Error> Repeat<ParseNext, Input, Output, (), Error>
impl<ParseNext, Input, Output, Error> Repeat<ParseNext, Input, Output, (), Error>
Sourcepub fn fold<Init, Op, Result>(
self,
init: Init,
op: Op,
) -> impl Parser<Input, Result, Error>
pub fn fold<Init, Op, Result>( self, init: Init, op: Op, ) -> impl Parser<Input, Result, Error>
Repeats the embedded parser, calling g
to gather the results
This stops before n
when the parser returns ErrMode::Backtrack
. To instead chain an error up, see
cut_err
.
§Arguments
init
A function returning the initial value.g
The function that combines a result off
with the current accumulator.
Warning: If the parser passed to fold
accepts empty inputs
(like alpha0
or digit0
), fold_repeat
will return an error,
to prevent going into an infinite loop.
§Example
Zero or more repetitions:
use winnow::combinator::repeat;
fn parser(s: &str) -> IResult<&str, Vec<&str>> {
repeat(
0..,
"abc"
).fold(
Vec::new,
|mut acc: Vec<_>, item| {
acc.push(item);
acc
}
).parse_peek(s)
}
assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
assert_eq!(parser("123123"), Ok(("123123", vec![])));
assert_eq!(parser(""), Ok(("", vec![])));
One or more repetitions:
use winnow::combinator::repeat;
fn parser(s: &str) -> IResult<&str, Vec<&str>> {
repeat(
1..,
"abc",
).fold(
Vec::new,
|mut acc: Vec<_>, item| {
acc.push(item);
acc
}
).parse_peek(s)
}
assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
assert_eq!(parser("123123"), Err(ErrMode::Backtrack(InputError::new("123123", ErrorKind::Many))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Many))));
Arbitrary number of repetitions:
use winnow::combinator::repeat;
fn parser(s: &str) -> IResult<&str, Vec<&str>> {
repeat(
0..=2,
"abc",
).fold(
Vec::new,
|mut acc: Vec<_>, item| {
acc.push(item);
acc
}
).parse_peek(s)
}
assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
assert_eq!(parser("123123"), Ok(("123123", vec![])));
assert_eq!(parser(""), Ok(("", vec![])));
assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));
Trait Implementations§
Source§impl<P, I, O, C, E> Parser<I, C, E> for Repeat<P, I, O, C, E>
impl<P, I, O, C, E> Parser<I, C, E> for Repeat<P, I, O, C, E>
Source§fn parse_next(&mut self, i: &mut I) -> PResult<C, E>
fn parse_next(&mut self, i: &mut I) -> PResult<C, E>
Source§fn parse(&mut self, input: I) -> Result<O, ParseError<I, E>>
fn parse(&mut self, input: I) -> Result<O, ParseError<I, E>>
Parse all of
input
, generating O
from itSource§fn parse_peek(&mut self, input: I) -> IResult<I, O, E>
fn parse_peek(&mut self, input: I) -> IResult<I, O, E>
Source§fn by_ref(&mut self) -> ByRef<'_, Self>where
Self: Sized,
fn by_ref(&mut self) -> ByRef<'_, Self>where
Self: Sized,
Treat
&mut Self
as a parser Read moreSource§fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
Produce a type’s default value Read more
Source§fn void(self) -> Void<Self, I, O, E>where
Self: Sized,
fn void(self) -> Void<Self, I, O, E>where
Self: Sized,
Discards the output of the
Parser
Read moreSource§fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
Convert the parser’s output to another type using
std::convert::From
Read moreSource§fn recognize(self) -> Take<Self, I, O, E>
fn recognize(self) -> Take<Self, I, O, E>
👎Deprecated since 0.6.14: Replaced with
Parser::take
Replaced with
Parser::take
Source§fn with_taken(self) -> WithTaken<Self, I, O, E>
fn with_taken(self) -> WithTaken<Self, I, O, E>
Produce the consumed input with the output Read more
Source§fn with_recognized(self) -> WithTaken<Self, I, O, E>
fn with_recognized(self) -> WithTaken<Self, I, O, E>
👎Deprecated since 0.6.14: Replaced with
Parser::with_taken
Replaced with
Parser::with_taken
Source§fn span(self) -> Span<Self, I, O, E>
fn span(self) -> Span<Self, I, O, E>
Produce the location of the consumed input as produced value. Read more
Source§fn with_span(self) -> WithSpan<Self, I, O, E>
fn with_span(self) -> WithSpan<Self, I, O, E>
Produce the location of consumed input with the output Read more
Source§fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
Maps a function over the output of a parser Read more
Source§fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>
fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>
Applies a function returning a
Result
over the output of a parser. Read moreSource§fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
Source§fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
Creates a parser from the output of this one Read more
Source§fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E>
fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E>
Applies a second parser over the output of the first one Read more
Source§fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
Apply
std::str::FromStr
to the output of the parser Read moreSource§fn verify<G, O2>(self, filter: G) -> Verify<Self, G, I, O, O2, E>
fn verify<G, O2>(self, filter: G) -> Verify<Self, G, I, O, O2, E>
Returns the output of the child parser if it satisfies a verification function. Read more
Source§fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
If parsing fails, add context to the error Read more
Source§fn complete_err(self) -> CompleteErr<Self>where
Self: Sized,
fn complete_err(self) -> CompleteErr<Self>where
Self: Sized,
Auto Trait Implementations§
impl<P, I, O, C, E> Freeze for Repeat<P, I, O, C, E>where
P: Freeze,
impl<P, I, O, C, E> RefUnwindSafe for Repeat<P, I, O, C, E>
impl<P, I, O, C, E> Send for Repeat<P, I, O, C, E>
impl<P, I, O, C, E> Sync for Repeat<P, I, O, C, E>
impl<P, I, O, C, E> Unpin for Repeat<P, I, O, C, E>
impl<P, I, O, C, E> UnwindSafe for Repeat<P, I, O, C, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more