1 // SPDX-License-Identifier: GPL-2.0
3 use proc_macro::{Delimiter, Group, Ident, Spacing, Span, TokenTree};
5 fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
6 let mut tokens = tokens.iter();
7 let mut segments = Vec::new();
12 Some(TokenTree::Literal(lit)) => {
13 // Allow us to concat string literals by stripping quotes
14 let mut value = lit.to_string();
15 if value.starts_with('"') && value.ends_with('"') {
19 segments.push((value, lit.span()));
21 Some(TokenTree::Ident(ident)) => {
22 let mut value = ident.to_string();
23 if value.starts_with("r#") {
24 value.replace_range(0..2, "");
26 segments.push((value, ident.span()));
28 Some(TokenTree::Punct(p)) if p.as_char() == ':' => {
29 let Some(TokenTree::Ident(ident)) = tokens.next() else {
30 panic!("expected identifier as modifier");
33 let (mut value, sp) = segments.pop().expect("expected identifier before modifier");
34 match ident.to_string().as_str() {
35 // Set the overall span of concatenated token as current span
39 "span modifier should only appear at most once"
43 "lower" => value = value.to_lowercase(),
44 "upper" => value = value.to_uppercase(),
45 v => panic!("unknown modifier `{v}`"),
47 segments.push((value, sp));
49 _ => panic!("unexpected token in paste segments"),
53 let pasted: String = segments.into_iter().map(|x| x.0).collect();
54 TokenTree::Ident(Ident::new(&pasted, span.unwrap_or(group_span)))
57 pub(crate) fn expand(tokens: &mut Vec<TokenTree>) {
58 for token in tokens.iter_mut() {
59 if let TokenTree::Group(group) = token {
60 let delimiter = group.delimiter();
61 let span = group.span();
62 let mut stream: Vec<_> = group.stream().into_iter().collect();
63 // Find groups that looks like `[< A B C D >]`
64 if delimiter == Delimiter::Bracket
66 && matches!(&stream[0], TokenTree::Punct(p) if p.as_char() == '<')
67 && matches!(&stream[stream.len() - 1], TokenTree::Punct(p) if p.as_char() == '>')
69 // Replace the group with concatenated token
70 *token = concat(&stream[1..stream.len() - 1], span);
72 // Recursively expand tokens inside the group
74 let mut group = Group::new(delimiter, stream.into_iter().collect());
76 *token = TokenTree::Group(group);
81 // Path segments cannot contain invisible delimiter group, so remove them if any.
82 for i in (0..tokens.len().saturating_sub(3)).rev() {
83 // Looking for a double colon
85 (&tokens[i + 1], &tokens[i + 2]),
86 (TokenTree::Punct(a), TokenTree::Punct(b))
87 if a.as_char() == ':' && a.spacing() == Spacing::Joint && b.as_char() == ':'
89 match &tokens[i + 3] {
90 TokenTree::Group(group) if group.delimiter() == Delimiter::None => {
91 tokens.splice(i + 3..i + 4, group.stream());
97 TokenTree::Group(group) if group.delimiter() == Delimiter::None => {
98 tokens.splice(i..i + 1, group.stream());