Created
April 14, 2018 20:47
-
-
Save Geal/47e8166b32626433b517227e7d6de006 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
pub struct NomIterator<I,O> { | |
input: Option<I>, | |
parser: Box<Fn(I) -> IResult<I,O> + 'static>, | |
} | |
impl<I,O> NomIterator<I,O> { | |
fn new(input: I, parser: Box<Fn(I) -> IResult<I,O> + 'static>) -> NomIterator<I,O> { | |
NomIterator { | |
input: Some(input), | |
parser: parser | |
} | |
} | |
fn return_input(&mut self) -> I { | |
self.input.take().unwrap() | |
} | |
fn to_vec(self) -> (I, Vec<O>) { | |
let mut v = Vec::new(); | |
for o in self { | |
v.push(o); | |
} | |
let i = self.return_input(); | |
(i, v) | |
} | |
} | |
impl<I,O> Iterator for NomIterator<I,O> { | |
type Item = O; | |
fn next(&mut self) -> Option<Self::Item> { | |
let i = self.input.take().unwrap(); | |
match (self.parser)(i).ok() { | |
None => None, | |
Some((i,o)) => { | |
self.input = Some(i); | |
Some(o) | |
} | |
} | |
} | |
} | |
impl<I,O> std::iter::FusedIterator for NomIterator<I,O> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment