Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1 | macro_rules! ast_struct { |
| 2 | ( |
| 3 | $(#[$attr:meta])* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 4 | pub struct $name:ident #full $($rest:tt)* |
| 5 | ) => { |
| 6 | #[cfg(feature = "full")] |
| 7 | $(#[$attr])* |
| 8 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 9 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 10 | pub struct $name $($rest)* |
| 11 | |
| 12 | #[cfg(not(feature = "full"))] |
| 13 | $(#[$attr])* |
| 14 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 15 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 16 | pub struct $name { |
| 17 | _noconstruct: (), |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | ( |
| 22 | $(#[$attr:meta])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 23 | pub struct $name:ident $($rest:tt)* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 24 | ) => { |
| 25 | $(#[$attr])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 26 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 27 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 28 | pub struct $name $($rest)* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 29 | }; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | macro_rules! ast_enum { |
| 33 | ( |
| 34 | $(#[$enum_attr:meta])* |
| 35 | pub enum $name:ident { $($variants:tt)* } |
| 36 | ) => ( |
| 37 | $(#[$enum_attr])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 38 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 39 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | pub enum $name { |
| 41 | $($variants)* |
| 42 | } |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | macro_rules! ast_enum_of_structs { |
| 47 | ( |
| 48 | $(#[$enum_attr:meta])* |
| 49 | pub enum $name:ident { |
| 50 | $( |
| 51 | $(#[$variant_attr:meta])* |
| 52 | pub $variant:ident($member:ident $($rest:tt)*), |
| 53 | )* |
| 54 | } |
| 55 | |
| 56 | $($remaining:tt)* |
| 57 | ) => ( |
| 58 | ast_enum! { |
| 59 | $(#[$enum_attr])* |
| 60 | pub enum $name { |
| 61 | $( |
| 62 | $(#[$variant_attr])* |
| 63 | $variant($member), |
| 64 | )* |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $( |
| 69 | maybe_ast_struct! { |
| 70 | $(#[$variant_attr])* |
| 71 | pub struct $member $($rest)* |
| 72 | } |
| 73 | |
| 74 | impl From<$member> for $name { |
| 75 | fn from(e: $member) -> $name { |
| 76 | $name::$variant(e) |
| 77 | } |
| 78 | } |
| 79 | )* |
| 80 | |
| 81 | generate_to_tokens! { |
| 82 | $($remaining)* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 83 | enum $name { $($variant [$($rest)*],)* } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 84 | } |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | macro_rules! generate_to_tokens { |
| 89 | (do_not_generate_to_tokens $($foo:tt)*) => (); |
| 90 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 91 | (enum $name:ident { $($variant:ident [$($rest:tt)*],)* }) => ( |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 92 | #[cfg(feature = "printing")] |
| 93 | impl ::quote::ToTokens for $name { |
| 94 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 95 | match *self { |
| 96 | $( |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 97 | $name::$variant(ref _e) => |
| 98 | to_tokens_call!(_e, tokens, $($rest)*), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 99 | )* |
| 100 | } |
| 101 | } |
| 102 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame^] | 103 | ); |
| 104 | } |
| 105 | |
| 106 | #[cfg(feature = "full")] |
| 107 | macro_rules! to_tokens_call { |
| 108 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 109 | $e.to_tokens($tokens) |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | #[cfg(not(feature = "full"))] |
| 114 | macro_rules! to_tokens_call { |
| 115 | // If the variant is marked as #full, don't auto-generate to-tokens for it. |
| 116 | ($e:ident, $tokens:ident, #full $($rest:tt)*) => { |
| 117 | unreachable!() |
| 118 | }; |
| 119 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 120 | $e.to_tokens($tokens) |
| 121 | }; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | macro_rules! maybe_ast_struct { |
| 125 | ( |
| 126 | $(#[$attr:meta])* |
| 127 | pub struct $name:ident |
| 128 | ) => (); |
| 129 | |
| 130 | ($($rest:tt)*) => (ast_struct! { $($rest)* }); |
| 131 | } |