Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Traverse into None-delimited groups #57

Merged
merged 1 commit into from
May 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
use proc_macro::{token_stream, TokenStream, TokenTree};
use std::iter::Peekable;
use proc_macro::{token_stream, Delimiter, TokenStream, TokenTree};

pub type Iter<'a> = &'a mut IterImpl;

pub struct IterImpl {
tokens: Peekable<token_stream::IntoIter>,
stack: Vec<token_stream::IntoIter>,
peeked: Option<TokenTree>,
}

pub fn new(tokens: TokenStream) -> IterImpl {
IterImpl {
tokens: tokens.into_iter().peekable(),
stack: vec![tokens.into_iter()],
peeked: None,
}
}

impl IterImpl {
pub fn peek(&mut self) -> Option<&TokenTree> {
self.tokens.peek()
self.peeked = self.next();
self.peeked.as_ref()
}
}

impl Iterator for IterImpl {
type Item = TokenTree;

fn next(&mut self) -> Option<Self::Item> {
self.tokens.next()
if let Some(tt) = self.peeked.take() {
return Some(tt);
}
loop {
let top = self.stack.last_mut()?;
match top.next() {
None => drop(self.stack.pop()),
Some(TokenTree::Group(ref group)) if group.delimiter() == Delimiter::None => {
self.stack.push(group.stream().into_iter());
}
Some(tt) => return Some(tt),
}
}
}
}