This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from dtolnay/none
Traverse into None-delimited groups
- Loading branch information
Showing
1 changed file
with
20 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} | ||
} |