Introduce a macro for empty iterators.

This commit is contained in:
Tom Alexander
2023-09-27 15:47:01 -04:00
parent 7419b75d76
commit 4359fc9266
2 changed files with 47 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/// Create iterators for ast nodes where it only has to iterate over children
macro_rules! simple_iter {
macro_rules! children_iter {
($astnodetype:ty, $itertype:ident, $innertype:ty) => {
pub struct $itertype<'r, 's> {
next: $innertype,
@@ -27,4 +27,35 @@ macro_rules! simple_iter {
};
}
pub(crate) use simple_iter;
pub(crate) use children_iter;
/// Create iterators for ast nodes that do not contain any ast node children.
macro_rules! empty_iter {
($astnodetype:ty, $itertype:ident) => {
pub struct $itertype<'r, 's> {
phantom: PhantomData<&'r $astnodetype>,
}
impl<'r, 's> Iterator for $itertype<'r, 's> {
type Item = AstNode<'r, 's>;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
impl<'r, 's> IntoIterator for &'r $astnodetype {
type Item = AstNode<'r, 's>;
type IntoIter = $itertype<'r, 's>;
fn into_iter(self) -> Self::IntoIter {
$itertype {
phantom: PhantomData,
}
}
}
};
}
pub(crate) use empty_iter;