Files
aho_corasick
anyhow
drone_config
drone_core
drone_core_macros
drone_ctypes
drone_macros_core
futures
futures_channel
futures_core
futures_io
futures_macro
futures_sink
futures_task
futures_util
if_chain
inflector
cases
camelcase
case
classcase
kebabcase
pascalcase
screamingsnakecase
sentencecase
snakecase
tablecase
titlecase
traincase
numbers
deordinalize
ordinalize
string
constants
deconstantize
demodulize
pluralize
singularize
suffix
foreignkey
lazy_static
memchr
pin_project_lite
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
regex
regex_syntax
serde
serde_derive
syn
toml
typenum
unicode_xid
  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
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use drone_macros_core::unkeywordize;
use inflector::Inflector;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{
    braced,
    parse::{Parse, ParseStream, Result},
    parse_macro_input, token, AttrStyle, Attribute, Ident, Path, Token, Visibility,
};

struct Input {
    prev_macro: Option<Path>,
    next_macro_attrs: Vec<Attribute>,
    next_macro_vis: Visibility,
    next_macro: Ident,
    macro_root_path: Option<Path>,
    root_path: Path,
    blocks: Vec<Block>,
}

struct Block {
    attrs: Vec<Attribute>,
    vis: Visibility,
    ident: Ident,
    regs: Vec<Reg>,
}

struct Reg {
    attrs: Vec<Attribute>,
    ident: Ident,
    skip: bool,
}

impl Parse for Input {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let next_macro_attrs = input.call(Attribute::parse_outer)?;
        let next_macro_vis = input.parse()?;
        input.parse::<Token![macro]>()?;
        let next_macro = input.parse()?;
        input.parse::<Token![;]>()?;
        let prev_macro = if input.peek(Token![use]) {
            input.parse::<Token![use]>()?;
            input.parse::<Token![macro]>()?;
            let prev_macro = input.parse()?;
            input.parse::<Token![;]>()?;
            Some(prev_macro)
        } else {
            None
        };
        let root_path = input.parse()?;
        input.parse::<Token![;]>()?;
        input.parse::<Token![crate]>()?;
        let macro_root_path = if input.peek(Token![;]) {
            input.parse::<Token![;]>()?;
            None
        } else {
            let path = input.parse()?;
            input.parse::<Token![;]>()?;
            Some(path)
        };
        let mut blocks = Vec::new();
        while !input.is_empty() {
            blocks.push(input.parse()?);
        }
        Ok(Self {
            prev_macro,
            next_macro_attrs,
            next_macro_vis,
            next_macro,
            macro_root_path,
            root_path,
            blocks,
        })
    }
}

impl Parse for Block {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let vis = input.parse()?;
        input.parse::<Token![mod]>()?;
        let ident = input.parse()?;
        let content;
        braced!(content in input);
        let mut regs = Vec::new();
        while !content.is_empty() {
            regs.push(content.parse()?);
        }
        Ok(Self { attrs, vis, ident, regs })
    }
}

impl Parse for Reg {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let skip = input.parse::<Option<Token![!]>>()?.is_some();
        let ident = input.parse()?;
        input.parse::<Token![;]>()?;
        Ok(Self { attrs, ident, skip })
    }
}

pub fn proc_macro(input: TokenStream) -> TokenStream {
    let Input {
        prev_macro,
        next_macro_attrs,
        next_macro_vis,
        next_macro,
        macro_root_path,
        root_path,
        blocks,
    } = parse_macro_input!(input);
    let mut tokens = Vec::new();
    let mut prev_macro = prev_macro.map(|prev_macro| quote!(#prev_macro));
    let macro_export = matches!(next_macro_vis, Visibility::Public(_));
    let (conditional_blocks, regular_blocks) =
        blocks.into_iter().partition::<Vec<_>, _>(|block| block.attrs.iter().any(is_cfg_attr));
    for (i, block) in conditional_blocks.into_iter().enumerate() {
        let mut cfg_attrs = block.attrs.iter().filter(|attr| is_cfg_attr(attr)).collect::<Vec<_>>();
        let cfg_macro = format_ident!("__{}_cfg_{}", next_macro, i);
        let doc_hidden_attr = doc_hidden_attr();
        tokens.extend(make_macro(
            macro_root_path.as_ref(),
            &root_path,
            prev_macro.as_ref(),
            &[&doc_hidden_attr, &negate_cfg_attrs(&cfg_attrs)],
            macro_export,
            &cfg_macro,
            &[],
        ));
        cfg_attrs.push(&doc_hidden_attr);
        tokens.extend(make_macro(
            macro_root_path.as_ref(),
            &root_path,
            prev_macro.as_ref(),
            &cfg_attrs,
            macro_export,
            &cfg_macro,
            &[&block],
        ));
        prev_macro =
            Some(if macro_export { quote!($crate::#cfg_macro) } else { quote!(#cfg_macro) });
    }
    tokens.extend(make_macro(
        macro_root_path.as_ref(),
        &root_path,
        prev_macro.as_ref(),
        &next_macro_attrs.iter().collect::<Vec<_>>(),
        macro_export,
        &next_macro,
        &regular_blocks.iter().collect::<Vec<_>>(),
    ));
    quote!(#(#tokens)*).into()
}

fn make_macro(
    macro_root_path: Option<&Path>,
    root_path: &Path,
    prev_macro: Option<&TokenStream2>,
    macro_attrs: &[&Attribute],
    macro_export: bool,
    macro_ident: &Ident,
    blocks: &[&Block],
) -> Vec<TokenStream2> {
    let mut tokens = Vec::new();
    let mut defs = Vec::new();
    for Block { attrs: block_attrs, vis: block_vis, ident: block_ident, regs } in blocks {
        let block_snk = block_ident.to_string().to_snake_case();
        let block_name = format_ident!("{}", unkeywordize(&block_snk));
        let mut block_tokens = Vec::new();
        let block_attrs_non_cfg =
            block_attrs.iter().filter(|attr| !is_cfg_attr(attr)).collect::<Vec<_>>();
        for Reg { attrs: reg_attrs, ident: reg_ident, skip } in regs {
            let reg_psc = format_ident!("{}", reg_ident.to_string().to_pascal_case());
            let reg_snk = reg_ident.to_string().to_snake_case();
            let reg_long = format_ident!("{}_{}", block_snk, reg_snk);
            let reg_short = format_ident!("{}", unkeywordize(&reg_snk));
            block_tokens.push(quote! {
                pub use #root_path::#reg_long as #reg_short;
                pub use #root_path::#reg_long::Reg as #reg_psc;
            });
            if !skip {
                let macro_root_path = macro_root_path.iter();
                defs.push(quote! {
                    #(#block_attrs_non_cfg)* #(#reg_attrs)*
                    #reg_long $crate#(#macro_root_path)*::#block_name::#reg_psc;
                });
            }
        }
        tokens.push(quote! {
            #(#block_attrs)*
            #block_vis mod #block_name {
                #(#block_tokens)*
            }
        });
    }
    let macro_vis = if macro_export { quote!(#[macro_export]) } else { quote!() };
    let macro_tokens = if let Some(prev_macro) = prev_macro {
        quote! {
            #prev_macro! {
                $(#[$attr])* index => $vis $ty;
                exclude => { $($undefs,)* };
                __extend => { #(#defs)* $($defs)* };
            }
        }
    } else {
        quote! {
            ::drone_core::reg::tokens_inner! {
                $(#[$attr])* $vis $ty
                { #(#defs)* $($defs)* }
                { $($undefs;)* }
            }
        }
    };
    tokens.push(quote! {
        #(#macro_attrs)*
        #macro_vis
        macro_rules! #macro_ident {
            (
                $(#[$attr:meta])* index => $vis:vis $ty:ident
                $(; $(exclude => { $($undefs:ident),* $(,)? })? $(;)?)?
            ) => {
                #macro_ident! {
                    $(#[$attr])* index => $vis $ty;
                    exclude => { $($($($undefs,)*)?)? };
                    __extend => {};
                }
            };
            (
                $(#[$attr:meta])* index => $vis:vis $ty:ident;
                exclude => { $($undefs:ident,)* };
                __extend => { $($defs:tt)* };
            ) => {
                #macro_tokens
            };
        }
    });
    tokens
}

fn negate_cfg_attrs(cfg_attrs: &[&Attribute]) -> Attribute {
    let cfg_attrs = cfg_attrs.iter().map(|attr| &attr.tokens).collect::<Vec<_>>();
    Attribute {
        pound_token: Token![#](Span::call_site()),
        style: AttrStyle::Outer,
        bracket_token: token::Bracket(Span::call_site()),
        path: format_ident!("cfg").into(),
        tokens: quote!((not(all(#(all#cfg_attrs),*)))),
    }
}

fn doc_hidden_attr() -> Attribute {
    Attribute {
        pound_token: Token![#](Span::call_site()),
        style: AttrStyle::Outer,
        bracket_token: token::Bracket(Span::call_site()),
        path: format_ident!("doc").into(),
        tokens: quote!((hidden)),
    }
}

fn is_cfg_attr(attr: &Attribute) -> bool {
    attr.path.leading_colon.is_none()
        && attr.path.segments.len() == 1
        && attr.path.segments.first().map_or(false, |x| x.ident == "cfg")
}