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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2016 macro-attr contributors.
// Copyright (c) 2020 Warlock <internalmike@gmail.com>.
//
// Licensed under the MIT license (see LICENSE or <http://opensource.org
// /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
// <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
// files in the project carrying such notice may not be copied, modified,
// or distributed except according to those terms.

/*!
This crate provides the `macro_attr!` macro that enables the use of custom, macro-based attributes and derivations.

The `macro_attr!` macro should be used to wrap an entire *single* item (`enum`, `struct`, *etc.*) declaration, including its attributes (both `derive` and others).  All attributes and derivations which whose names end with `!` will be assumed to be implemented by macros, and treated accordingly.

```rust
use macro_attr_2018::macro_attr;

// Define some traits to be derived.

trait TypeName {
    fn type_name() -> &'static str;
}

trait ReprType {
    type Repr;
}

// Define macros which derive implementations of these macros.

macro_rules! TypeName {
    // We can support any kind of item we want.
    (() $vis:vis enum $name:ident $($tail:tt)+) => { TypeName! { @impl $name } };
    (() $vis:vis struct $name:ident $($tail:tt)+) => { TypeName! { @impl $name } };

    // Inner rule to cut down on repetition.
    (@impl $name:ident) => {
        impl TypeName for $name {
            fn type_name() -> &'static str { stringify!($name) }
        }
    };
}

macro_rules! ReprType {
    // Note that we use a "derivation argument" here for the `$repr` type.
    (($repr:ty) $vis:vis enum $name:ident $($tail:tt)+) => {
        impl ReprType for $name {
            type Repr = $repr;
        }
    };
}
```
*/

#![no_std]
#![deny(warnings)]

/**
When given an item definition, including its attributes, this macro parses said attributes and dispatches any attributes or derivations suffixed with `!` to user-defined macros.  This allows multiple macros to process the same item.

Given the following input:

```ignore
#[derive(Copy, Name!(args...), Clone, Another!, Debug)]
struct Foo;
```

`macro_attr!` will expand to the equivalent of:

```ignore
#[derive(Copy, Clone, Debug)]
struct Foo;

Name!((args...) struct Foo;);
Another!(() struct Foo;);
```

Note that macro derives may be mixed with regular derives, or put in their own `#[derive(...)]` attribute.  Also note that macro derive invocations are *not* passed the other attributes on the item; input will consist of the arguments provided to the derivation (*i.e.* `(args...)` in this example), the item's visibility (if any), and the item definition itself.

A macro derivation invoked *without* arguments will be treated as though it was invoked with empty parentheses.  *i.e.* `#[derive(Name!)]` is equivalent to `#[derive(Name!())]`.

A derivation macro may expand to any number of new items derived from the provided input.
*/
#[macro_export]
macro_rules! macro_attr {
    ($($item:tt)*) => {
        $crate::macro_attr_impl! { $($item)* }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! macro_attr_impl {
    /*

    > **Convention**: a capture named `$fixed` is used for any part of a recursive rule that is needed in the terminal case, but is not actually being *used* for the recursive part.  This avoids having to constantly repeat the full capture pattern (and makes changing it easier).

    # Primary Invocation Forms

    These need to catch any valid form of item.

    */
    (
        $(#[$($attrs:tt)+])*
        enum $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (enum $($it)+)
        }
    };

    (
        $(#[$($attrs:tt)+])*
        struct $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (struct $($it)+)
        }
    };

    (
        $(#[$($attrs:tt)+])*
        trait $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (trait $($it)+)
        }
    };

    (
        $(#[$($attrs:tt)+])*
        pub $(($($vis:tt)+))? enum $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (pub $(($($vis)+))? enum $($it)+)
        }
    };

    (
        $(#[$($attrs:tt)+])*
        pub $(($($vis:tt)+))? struct $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (pub $(($($vis)+))? struct $($it)+)
        }
    };

    (
        $(#[$($attrs:tt)+])*
        pub $(($($vis:tt)+))? trait $($it:tt)+
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)+],)*), (), (),
            (pub $(($($vis)+))? trait $($it)+)
        }
    };

    /*

    # `@split_attrs`

    This is responsible for dividing all attributes on an item into two groups:

    - `#[derive(...)]`
    - Everything else.

    As part of this, it also explodes `#[derive(A, B(..), C, ...)]` into `A, B(..), C, ...`.  This is to simplify the next stage.

    */
    (
        @split_attrs
        (),
        $non_derives:tt,
        $derives:tt,
        $it:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            { $non_derives, $it },
            $derives,
            (),
            ()
        }
    };

    (
        @split_attrs
        (#[derive($($new_drvs:tt)*)], $(#[$($attrs:tt)*],)*),
        $non_derives:tt,
        ($($derives:tt)*),
        $it:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)*],)*),
            $non_derives,
            ($($derives)* $($new_drvs)*,),
            $it
        }
    };

    (
        @split_attrs
        (#[$new_attr:meta], $(#[$($attrs:tt)*],)*),
        ($($non_derives:tt)*),
        $derives:tt,
        $it:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_attrs
            ($(#[$($attrs)*],)*),
            ($($non_derives)* #[$new_attr],),
            $derives,
            $it
        }
    };

    /*

    # `@split_derive_attrs`

    This is responsible for taking the list of derivation attributes and splitting them into "built-in" and "custom" groups.

    A custom attribute is any which has a `!` after the name, like a macro.
    */

    (@split_derive_attrs
        { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) },
        ($(,)*), (), ($($user_drvs:tt)*)
    ) => {
        $crate::macro_attr_impl! {
            @as_item
            $(#[$($non_derives)*])*
            $($it)*
        }

        $crate::macro_attr_impl! {
            @expand_user_drvs
            ($($user_drvs)*), ($($it)*)
        }
    };

    (@split_derive_attrs
        { ($(#[$($non_derives:tt)*],)*), ($($it:tt)*) },
        ($(,)*), ($($bi_drvs:ident,)+), ($($user_drvs:tt)*)
    ) => {
        $crate::macro_attr_impl! {
            @as_item
            #[derive($($bi_drvs,)+)]
            $(#[$($non_derives)*])*
            $($it)*
        }

        $crate::macro_attr_impl! {
            @expand_user_drvs
            ($($user_drvs)*), ($($it)*)
        }
    };

    (@split_derive_attrs
        $fixed:tt,
        (,, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            $fixed, ($($tail)*), $bi_drvs, $user_drvs
        }
    };

    (@split_derive_attrs
        $fixed:tt,
        (, $($tail:tt)*), $bi_drvs:tt, $user_drvs:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            $fixed, ($($tail)*), $bi_drvs, $user_drvs
        }
    };

    /*

    ## Custom Derivations

    Now we can handle the custom derivations.  There are two forms we care about: those *with* an argument, and those *without*.

    The *reason* we care is that, in order to simplify the derivation macros, we want to detect the argument-less case and generate an empty pair of parens.

    */
    (@split_derive_attrs
        $fixed:tt,
        ($new_user:ident ! ($($new_user_args:tt)*), $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*)
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user($($new_user_args)*),)
        }
    };

    (@split_derive_attrs
        $fixed:tt,
        ($new_user:ident !, $($tail:tt)*), $bi_drvs:tt, ($($user_drvs:tt)*)
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            $fixed, ($($tail)*), $bi_drvs, ($($user_drvs)* $new_user(),)
        }
    };

    /*

    ## Non-Macro Derivations

    All the rest.

    */
    (@split_derive_attrs
        $fixed:tt,
        ($drv:ident, $($tail:tt)*), ($($bi_drvs:ident,)*), $user_drvs:tt
    ) => {
        $crate::macro_attr_impl! {
            @split_derive_attrs
            $fixed,
            ($($tail)*), ($($bi_drvs,)* $drv,), $user_drvs
        }
    };

    /*

    # `@expand_user_drvs`

    Finally, we have a recursive rule for expanding user derivations.  This is basically just using the derivation name as a macro identifier.

    This *has* to be recursive because we need to expand two independent repetition sequences simultaneously, and this causes `macro_rules!` to throw a wobbly.  Don't want that.  So, recursive it is.

    */
    (@expand_user_drvs
        (), ($($it:tt)*)
    ) => {};

    (@expand_user_drvs
        ($user_drv:ident $arg:tt, $($tail:tt)*), ($($it:tt)*)
    ) => {
        $user_drv! { $arg $($it)* }
        $crate::macro_attr_impl! {
            @expand_user_drvs
            ($($tail)*), ($($it)*)
        }
    };

    /*

    # Miscellaneous Rules

    */
    (@as_item $($i:item)*) => {$($i)*};
}