| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
mod ast; |
| 30 |
mod copy; |
| 31 |
mod emit; |
| 32 |
mod fill; |
| 33 |
mod pairs; |
| 34 |
mod parse; |
| 35 |
mod symbolic; |
| 36 |
|
| 37 |
use proc_macro::TokenStream; |
| 38 |
use syn::parse_macro_input; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[proc_macro] |
| 57 |
pub fn declare(input: TokenStream) -> TokenStream { |
| 58 |
|
| 59 |
drop(copy::taken()); |
| 60 |
let declaration = parse_macro_input!(input as ast::Declaration); |
| 61 |
let copied = copy::taken(); |
| 62 |
match expand(&declaration) { |
| 63 |
Ok(expansion) => { |
| 64 |
let watched = watch(&copied); |
| 65 |
quote::quote! { #expansion #watched }.into() |
| 66 |
} |
| 67 |
Err(error) => error.into_compile_error().into(), |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
fn watch(copied: &[std::path::PathBuf]) -> proc_macro2::TokenStream { |
| 79 |
let markers = copied.iter().map(|path| { |
| 80 |
let path = path.to_string_lossy(); |
| 81 |
quote::quote! { const _: &[u8] = include_bytes!(#path); } |
| 82 |
}); |
| 83 |
quote::quote! { #(#markers)* } |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
fn expand(declaration: &ast::Declaration) -> syn::Result<proc_macro2::TokenStream> { |
| 88 |
pairs::check(&declaration.items)?; |
| 89 |
let shape = emit::declaration(declaration)?; |
| 90 |
let promise = symbolic::promise(declaration); |
| 91 |
if !symbolic::wanted(declaration) { |
| 92 |
return Ok(quote::quote! { #shape #promise }); |
| 93 |
} |
| 94 |
|
| 95 |
let staged = symbolic::stage(declaration)?; |
| 96 |
let twin = emit::declaration(&staged.declaration)?; |
| 97 |
let filler = fill::filler(declaration, &staged.fill)?; |
| 98 |
let (guards, loops, holes, sites) = staged.counts; |
| 99 |
let counts = &staged.counts_ident; |
| 100 |
let vis = declaration |
| 101 |
.vis |
| 102 |
.clone() |
| 103 |
.unwrap_or(syn::Visibility::Inherited); |
| 104 |
let doc = format!( |
| 105 |
" What a plan has to answer for to drive [`{}`].", |
| 106 |
staged.declaration.name |
| 107 |
); |
| 108 |
|
| 109 |
Ok(quote::quote! { |
| 110 |
#shape |
| 111 |
#promise |
| 112 |
#twin |
| 113 |
#filler |
| 114 |
#[doc = #doc] |
| 115 |
#vis const #counts: ::quasi_router::stage::Shape = ::quasi_router::stage::Shape { |
| 116 |
guards: #guards, |
| 117 |
loops: #loops, |
| 118 |
holes: #holes, |
| 119 |
sites: #sites, |
| 120 |
}; |
| 121 |
}) |
| 122 |
} |
| 123 |
|