|
| 1 | +// force-host |
| 2 | +// no-prefer-dynamic |
| 3 | +#![crate_type = "proc-macro"] |
| 4 | + |
| 5 | +extern crate proc_macro; |
| 6 | + |
| 7 | +use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree}; |
| 8 | + |
| 9 | +#[proc_macro] |
| 10 | +pub fn expand(_: TokenStream) -> TokenStream { |
| 11 | + // Hand expansion/rewriting of |
| 12 | + // ``` |
| 13 | + // quote! { |
| 14 | + // output_mut(|o| o.copied_text = "".into()); |
| 15 | + // output_mut(|o| o.copied_text = format!("{:?}", self.tile_db)); |
| 16 | + // }.into() |
| 17 | + // ``` |
| 18 | + stream([ |
| 19 | + ident("output_mut"), |
| 20 | + group( |
| 21 | + Delimiter::Parenthesis, |
| 22 | + [ |
| 23 | + or(), |
| 24 | + ident("o"), |
| 25 | + or(), |
| 26 | + ident("o"), |
| 27 | + dot(), |
| 28 | + ident("copied_text"), |
| 29 | + eq(), |
| 30 | + string(""), |
| 31 | + dot(), |
| 32 | + ident("into"), |
| 33 | + group(Delimiter::Parenthesis, []), |
| 34 | + ], |
| 35 | + ), |
| 36 | + semi(), |
| 37 | + ident("output_mut"), |
| 38 | + group( |
| 39 | + Delimiter::Parenthesis, |
| 40 | + [ |
| 41 | + or(), |
| 42 | + ident("o"), |
| 43 | + or(), |
| 44 | + ident("o"), |
| 45 | + dot(), |
| 46 | + ident("copied_text"), |
| 47 | + eq(), |
| 48 | + ident("format"), |
| 49 | + bang(), |
| 50 | + group( |
| 51 | + Delimiter::Parenthesis, |
| 52 | + [string("{:?}"), comma(), ident("self"), dot(), ident("tile_db")], |
| 53 | + ), |
| 54 | + ], |
| 55 | + ), |
| 56 | + semi(), |
| 57 | + ]) |
| 58 | +} |
| 59 | + |
| 60 | +fn stream(s: impl IntoIterator<Item = TokenTree>) -> TokenStream { |
| 61 | + s.into_iter().collect() |
| 62 | +} |
| 63 | + |
| 64 | +fn ident(i: &str) -> TokenTree { |
| 65 | + TokenTree::Ident(Ident::new(i, Span::call_site())) |
| 66 | +} |
| 67 | +fn group(d: Delimiter, s: impl IntoIterator<Item = TokenTree>) -> TokenTree { |
| 68 | + TokenTree::Group(Group::new(d, s.into_iter().collect())) |
| 69 | +} |
| 70 | +fn semi() -> TokenTree { |
| 71 | + TokenTree::Punct(Punct::new(';', Spacing::Alone)) |
| 72 | +} |
| 73 | +fn or() -> TokenTree { |
| 74 | + TokenTree::Punct(Punct::new('|', Spacing::Alone)) |
| 75 | +} |
| 76 | +fn dot() -> TokenTree { |
| 77 | + TokenTree::Punct(Punct::new('.', Spacing::Alone)) |
| 78 | +} |
| 79 | +fn eq() -> TokenTree { |
| 80 | + TokenTree::Punct(Punct::new('=', Spacing::Alone)) |
| 81 | +} |
| 82 | +fn bang() -> TokenTree { |
| 83 | + TokenTree::Punct(Punct::new('!', Spacing::Alone)) |
| 84 | +} |
| 85 | +fn comma() -> TokenTree { |
| 86 | + TokenTree::Punct(Punct::new(',', Spacing::Alone)) |
| 87 | +} |
| 88 | +fn string(s: &str) -> TokenTree { |
| 89 | + TokenTree::Literal(Literal::string(s)) |
| 90 | +} |
0 commit comments