Implement empty iterator for types with no ast node children.

This commit is contained in:
Tom Alexander
2023-09-27 15:38:33 -04:00
parent e4cfc296e5
commit 7419b75d76
3 changed files with 72 additions and 29 deletions

30
src/iter/macros.rs Normal file
View File

@@ -0,0 +1,30 @@
/// Create iterators for ast nodes where it only has to iterate over children
macro_rules! simple_iter {
($astnodetype:ty, $itertype:ident, $innertype:ty) => {
pub struct $itertype<'r, 's> {
next: $innertype,
}
impl<'r, 's> Iterator for $itertype<'r, 's> {
type Item = AstNode<'r, 's>;
fn next(&mut self) -> Option<Self::Item> {
self.next.next().map(Into::<AstNode>::into)
}
}
impl<'r, 's> IntoIterator for &'r $astnodetype {
type Item = AstNode<'r, 's>;
type IntoIter = $itertype<'r, 's>;
fn into_iter(self) -> Self::IntoIter {
$itertype {
next: self.children.iter(),
}
}
}
};
}
pub(crate) use simple_iter;