Struct Repeat
pub struct Repeat<P, I, O, C, E>{ /* private fields */ }
dep_winnow
only.Implementations§
§impl<ParseNext, Input, Output, Error> Repeat<ParseNext, Input, Output, (), Error>
impl<ParseNext, Input, Output, Error> Repeat<ParseNext, Input, Output, (), Error>
pub 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 op
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.op
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"])));
pub fn verify_fold<Init, Op, Result>(
self,
init: Init,
op: Op,
) -> impl Parser<Input, Result, Error>
pub fn verify_fold<Init, Op, Result>( self, init: Init, op: Op, ) -> impl Parser<Input, Result, Error>
Akin to Repeat::fold
, but for containers that can reject an element.
This stops before n
when the parser returns ErrMode::Backtrack
. To instead chain an error up, see
cut_err
. Additionally, if the fold function returns None
, the parser will
stop and return an error.
§Arguments
init
A function returning the initial value.op
The function that combines a result off
with the current accumulator.
Warning: If the parser passed to repeat
accepts empty inputs
(like alpha0
or digit0
), verify_fold
will return an error,
to prevent going into an infinite loop.
§Example
Guaranteeing that the input had unique elements:
use winnow::combinator::repeat;
use std::collections::HashSet;
fn parser(s: &str) -> IResult<&str, HashSet<&str>> {
repeat(
0..,
"abc"
).verify_fold(
HashSet::new,
|mut acc: HashSet<_>, item| {
if acc.insert(item) {
Some(acc)
} else {
None
}
}
).parse_peek(s)
}
assert_eq!(parser("abc"), Ok(("", HashSet::from(["abc"]))));
assert_eq!(parser("abcabc"), Err(ErrMode::Backtrack(InputError::new("abc", ErrorKind::Verify))));
assert_eq!(parser("abc123"), Ok(("123", HashSet::from(["abc"]))));
assert_eq!(parser("123123"), Ok(("123123", HashSet::from([]))));
assert_eq!(parser(""), Ok(("", HashSet::from([]))));
pub fn try_fold<Init, Op, OpError, Result>(
self,
init: Init,
op: Op,
) -> impl Parser<Input, Result, Error>where
Init: FnMut() -> Result,
Op: FnMut(Result, Output) -> Result<Result, OpError>,
Error: FromExternalError<Input, OpError>,
pub fn try_fold<Init, Op, OpError, Result>(
self,
init: Init,
op: Op,
) -> impl Parser<Input, Result, Error>where
Init: FnMut() -> Result,
Op: FnMut(Result, Output) -> Result<Result, OpError>,
Error: FromExternalError<Input, OpError>,
Akin to Repeat::fold
, but for containers that can error when an element is accumulated.
This stops before n
when the parser returns ErrMode::Backtrack
. To instead chain an error up, see
cut_err
. Additionally, if the fold function returns an error, the parser will
stop and return it.
§Arguments
init
A function returning the initial value.op
The function that combines a result off
with the current accumulator.
Warning: If the parser passed to repeat
accepts empty inputs
(like alpha0
or digit0
), try_fold
will return an error,
to prevent going into an infinite loop.
§Example
Writing the output to a vector of bytes:
use winnow::combinator::repeat;
use std::io::Write;
use std::io::Error;
fn parser(s: &str) -> IResult<&str, Vec<u8>> {
repeat(
0..,
"abc"
).try_fold(
Vec::new,
|mut acc, item: &str| -> Result<_, Error> {
acc.write(item.as_bytes())?;
Ok(acc)
}
).parse_peek(s)
}
assert_eq!(parser("abc"), Ok(("", b"abc".to_vec())));
assert_eq!(parser("abc123"), Ok(("123", b"abc".to_vec())));
assert_eq!(parser("123123"), Ok(("123123", vec![])));
assert_eq!(parser(""), Ok(("", vec![])));
Trait Implementations§
§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>
§fn parse(&mut self, input: I) -> Result<O, ParseError<I, E>> ⓘ
fn parse(&mut self, input: I) -> Result<O, ParseError<I, E>> ⓘ
input
, generating O
from it§fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
§fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
std::convert::From
Read more§fn recognize(self) -> Take<Self, I, O, E>
fn recognize(self) -> Take<Self, I, O, E>
Parser::take
Parser::take
§fn with_taken(self) -> WithTaken<Self, I, O, E>
fn with_taken(self) -> WithTaken<Self, I, O, E>
§fn with_recognized(self) -> WithTaken<Self, I, O, E>
fn with_recognized(self) -> WithTaken<Self, I, O, E>
Parser::with_taken
Parser::with_taken
§fn span(self) -> Span<Self, I, O, E>
fn span(self) -> Span<Self, I, O, E>
§fn with_span(self) -> WithSpan<Self, I, O, E>
fn with_span(self) -> WithSpan<Self, I, O, E>
§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>
§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>
Result
over the output of a parser. Read more§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>
§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>
§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>
§fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
std::str::FromStr
to the output of the parser Read more§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>
§fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
§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§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.