Skip to content

Commit

Permalink
Recursively respan tokens interpolated from a macro_rules metavariable
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 27, 2021
1 parent f45df31 commit ce4d081
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,24 @@ pub fn parse(tokens: &mut TokenStream, s: &str) {

pub fn parse_spanned(tokens: &mut TokenStream, span: Span, s: &str) {
let s: TokenStream = s.parse().expect("invalid token stream");
tokens.extend(s.into_iter().map(|mut t| {
t.set_span(span);
t
}));
tokens.extend(s.into_iter().map(|t| respan_token_tree(t, span)));
}

// Token tree with every span replaced by the given one.
fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
match &mut token {
TokenTree::Group(g) => {
let stream = g
.stream()
.into_iter()
.map(|token| respan_token_tree(token, span))
.collect();
*g = Group::new(g.delimiter(), stream);
g.set_span(span);
}
other => other.set_span(span),
}
token
}

pub fn push_ident(tokens: &mut TokenStream, s: &str) {
Expand Down

0 comments on commit ce4d081

Please sign in to comment.