Skip to content

Commit

Permalink
Merge pull request #199 from dtolnay/respan
Browse files Browse the repository at this point in the history
Recursively respan tokens interpolated from a macro_rules metavariable
  • Loading branch information
dtolnay authored Dec 27, 2021
2 parents f45df31 + ce4d081 commit d74545e
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 d74545e

Please sign in to comment.