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

Commit

Permalink
Merge pull request #57 from dtolnay/none
Browse files Browse the repository at this point in the history
Traverse into None-delimited groups
  • Loading branch information
dtolnay authored May 24, 2020
2 parents 091e136 + 51a988b commit c749724
Showing 1 changed file with 20 additions and 6 deletions.
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),
}
}
}
}

0 comments on commit c749724

Please sign in to comment.