Skip to content

iterator_complete for complete parsers #1841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,8 @@ where

/// Creates an iterator from input data and a parser.
///
/// Call the iterator's [ParserIterator::finish] method to get the remaining input if successful,
/// or the error value if we encountered an error.
/// Call the iterator's [ParserIterator::finish] method to get the remaining
/// input if successful, or the error value if we encountered an error.
///
/// On [`Err::Error`], iteration will stop. To instead chain an error up, see [`cut`].
///
Expand All @@ -887,63 +887,82 @@ where
/// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect());
/// assert_eq!(res, Ok(("123", ())));
/// ```
pub fn iterator<Input, Error, F>(input: Input, f: F) -> ParserIterator<Input, Error, F>
pub fn iterator<Input, Error, F>(input: Input, f: F) -> ParserIterator<Input, Error, F, Streaming>
where
F: Parser<Input>,
Error: ParseError<Input>,
{
ParserIterator {
iterator: f,
input,
state: Some(State::Running),
state: State::Running,
_streaming: PhantomData,
}
}

/// Same as [iterator] but for complete parsers
pub fn iterator_complete<Input, Error, F>(
input: Input,
f: F,
) -> ParserIterator<Input, Error, F, Complete>
where
F: Parser<Input>,
Error: ParseError<Input>,
{
ParserIterator {
iterator: f,
input,
state: State::Running,
_streaming: PhantomData,
}
}

/// Main structure associated to the [iterator] function.
pub struct ParserIterator<I, E, F> {
pub struct ParserIterator<I, E, F, S> {
iterator: F,
input: I,
state: Option<State<E>>,
state: State<E>,
_streaming: std::marker::PhantomData<S>,
}

impl<I: Clone, E, F> ParserIterator<I, E, F> {
impl<I: Clone, E, F, S> ParserIterator<I, E, F, S> {
/// Returns the remaining input if parsing was successful, or the error if we encountered an error.
pub fn finish(mut self) -> IResult<I, (), E> {
match self.state.take().unwrap() {
pub fn finish(self) -> IResult<I, (), E> {
match self.state {
State::Running | State::Done => Ok((self.input, ())),
State::Failure(e) => Err(Err::Failure(e)),
State::Incomplete(i) => Err(Err::Incomplete(i)),
}
}
}

impl<Input, Output, Error, F> core::iter::Iterator for ParserIterator<Input, Error, F>
impl<Input, Output, Error, F, S> core::iter::Iterator for ParserIterator<Input, Error, F, S>
where
F: Parser<Input, Output = Output, Error = Error>,
Input: Clone,
S: IsStreaming,
{
type Item = Output;

fn next(&mut self) -> Option<Self::Item> {
if let State::Running = self.state.take().unwrap() {
if let State::Running = self.state {
let input = self.input.clone();

match (self.iterator).parse(input) {
match (self.iterator).process::<OutputM<Emit, Emit, S>>(input) {
Ok((i, o)) => {
self.input = i;
self.state = Some(State::Running);
Some(o)
}
Err(Err::Error(_)) => {
self.state = Some(State::Done);
self.state = State::Done;
None
}
Err(Err::Failure(e)) => {
self.state = Some(State::Failure(e));
self.state = State::Failure(e);
None
}
Err(Err::Incomplete(i)) => {
self.state = Some(State::Incomplete(i));
self.state = State::Incomplete(i);
None
}
}
Expand Down