mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
wip: iterator magic
This commit is contained in:
parent
34b35d08be
commit
38f69ddd50
1 changed files with 28 additions and 2 deletions
|
|
@ -161,7 +161,31 @@ pub trait Atom<'a>: Sized {
|
|||
Num(usize),
|
||||
Sym(&'a str),
|
||||
Key(&'a str),
|
||||
Exp(TokenIterator<'a>),
|
||||
Exp(RefAtomIterator<'a>),
|
||||
}
|
||||
type RefAtomResult<'a> = Result<RefAtom<'a>, ParseError>;
|
||||
#[derive(Clone, PartialEq)] pub struct RefAtomIterator<'a>(TokenIterator<'a>);
|
||||
impl<'a> Iterator for RefAtomIterator<'a> {
|
||||
type Item = RefAtomResult<'a>;
|
||||
fn next (&mut self) -> Option<RefAtomResult<'a>> {
|
||||
Some(if let Some(result) = Iterator::next(&mut self.0) {
|
||||
match result {
|
||||
Err(e) => Err(e),
|
||||
Ok(token) => match token.kind {
|
||||
Nil => Err(ParseError::Empty),
|
||||
Num => match to_number(token.slice_source(self.0.0)) {
|
||||
Ok(n) => Ok(RefAtom::Num(n)),
|
||||
Err(e) => Err(e)
|
||||
},
|
||||
Sym => Ok(RefAtom::Sym(token.slice_source(self.0.0))),
|
||||
Key => Ok(RefAtom::Key(token.slice_source(self.0.0))),
|
||||
Exp => todo!()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return None
|
||||
})
|
||||
}
|
||||
}
|
||||
impl<'a> RefAtom<'a> {
|
||||
pub fn to_arc_atom (&self) -> ArcAtom {
|
||||
|
|
@ -195,7 +219,9 @@ impl<'a> Atom<'a> for RefAtom<'a> {
|
|||
},
|
||||
Sym => Ok(RefAtom::Sym(token.slice_source(source))),
|
||||
Key => Ok(RefAtom::Key(token.slice_source(source))),
|
||||
Exp => Ok(RefAtom::Exp(TokenIterator::new(token.slice_source(source))))
|
||||
Exp => Ok(
|
||||
RefAtom::Exp(RefAtomIterator(TokenIterator::new(token.slice_source(source))))
|
||||
)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue