| 1 |
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree}; |
| 2 |
|
| 3 |
/// Generates the equivalent of this Rust code as a TokenStream: |
| 4 |
/// |
| 5 |
/// ```nocompile |
| 6 |
/// ::ctor::__support::ctor_parse!(#[ctor] fn foo() { ... }); |
| 7 |
/// ::dtor::__support::dtor_parse!(#[dtor] fn foo() { ... }); |
| 8 |
/// ``` |
| 9 |
#[allow(unknown_lints, tail_expr_drop_order, unused)] |
| 10 |
pub(crate) fn generate( |
| 11 |
macro_crate: &str, |
| 12 |
macro_type: &str, |
| 13 |
attribute: TokenStream, |
| 14 |
item: TokenStream, |
| 15 |
) -> TokenStream { |
| 16 |
let mut inner = TokenStream::new(); |
| 17 |
|
| 18 |
// Search for crate_path in attributes |
| 19 |
let mut crate_path = None; |
| 20 |
let mut tokens = attribute.clone().into_iter().peekable(); |
| 21 |
|
| 22 |
while let Some(token) = tokens.next() { |
| 23 |
if let TokenTree::Ident(ident) = &token { |
| 24 |
if ident.to_string() == "crate_path" { |
| 25 |
// Look for = |
| 26 |
if let Some(TokenTree::Punct(punct)) = tokens.next() { |
| 27 |
if punct.as_char() == '=' { |
| 28 |
// Collect tokens until comma or end |
| 29 |
let mut path = TokenStream::new(); |
| 30 |
while let Some(token) = tokens.peek() { |
| 31 |
match token { |
| 32 |
TokenTree::Punct(p) if p.as_char() == ',' => { |
| 33 |
tokens.next(); |
| 34 |
break; |
| 35 |
} |
| 36 |
_ => { |
| 37 |
path.extend(std::iter::once(tokens.next().unwrap())); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
crate_path = Some(path); |
| 42 |
break; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if attribute.is_empty() { |
| 50 |
// #[ctor] |
| 51 |
inner.extend([ |
| 52 |
TokenTree::Punct(Punct::new('#', Spacing::Alone)), |
| 53 |
TokenTree::Group(Group::new( |
| 54 |
Delimiter::Bracket, |
| 55 |
TokenStream::from_iter([TokenTree::Ident(Ident::new( |
| 56 |
macro_type, |
| 57 |
Span::call_site(), |
| 58 |
))]), |
| 59 |
)), |
| 60 |
]); |
| 61 |
} else { |
| 62 |
inner.extend([ |
| 63 |
TokenTree::Punct(Punct::new('#', Spacing::Alone)), |
| 64 |
TokenTree::Group(Group::new( |
| 65 |
Delimiter::Bracket, |
| 66 |
TokenStream::from_iter([ |
| 67 |
TokenTree::Ident(Ident::new(macro_type, Span::call_site())), |
| 68 |
TokenTree::Group(Group::new(Delimiter::Parenthesis, attribute)), |
| 69 |
]), |
| 70 |
)), |
| 71 |
]); |
| 72 |
} |
| 73 |
|
| 74 |
inner.extend(item); |
| 75 |
|
| 76 |
let mut invoke = crate_path.unwrap_or_else(|| { |
| 77 |
TokenStream::from_iter([ |
| 78 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 79 |
TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 80 |
TokenTree::Ident(Ident::new(macro_crate, Span::call_site())), |
| 81 |
]) |
| 82 |
}); |
| 83 |
|
| 84 |
invoke.extend([ |
| 85 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 86 |
TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 87 |
TokenTree::Ident(Ident::new("__support", Span::call_site())), |
| 88 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 89 |
TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 90 |
TokenTree::Ident(Ident::new( |
| 91 |
&format!("{macro_type}_parse"), |
| 92 |
Span::call_site(), |
| 93 |
)), |
| 94 |
TokenTree::Punct(Punct::new('!', Spacing::Alone)), |
| 95 |
TokenTree::Group(Group::new(Delimiter::Parenthesis, inner)), |
| 96 |
TokenTree::Punct(Punct::new(';', Spacing::Alone)), |
| 97 |
]); |
| 98 |
|
| 99 |
invoke |
| 100 |
} |