-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::input::InputError; | ||
use crate::parser::Parser; | ||
use std::iter::FusedIterator; | ||
|
||
/// An iterator that lazily parses the input using the provided parser. | ||
/// | ||
/// See [`Parser::parse_iterator`]. | ||
#[derive(Copy, Clone)] | ||
#[must_use = "iterators are lazy and do nothing unless consumed"] | ||
pub struct ParserIterator<'a, P> { | ||
pub(super) input: &'a str, | ||
pub(super) remaining: &'a [u8], | ||
pub(super) parser: P, | ||
} | ||
|
||
impl<'a, P: Parser> Iterator for ParserIterator<'a, P> { | ||
type Item = Result<P::Output<'a>, InputError>; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.remaining.is_empty() { | ||
return None; | ||
} | ||
|
||
match self.parser.parse(self.remaining) { | ||
Ok((v, remaining)) => { | ||
self.remaining = remaining; | ||
Some(Ok(v)) | ||
} | ||
Err((err, remaining)) => { | ||
self.remaining = &[]; // Ensure future calls return None | ||
Some(Err(InputError::new(self.input, remaining, err))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a, P: Parser> FusedIterator for ParserIterator<'a, P> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters