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])* |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 23 | pub struct $name:ident #manual_extra_traits $($rest:tt)* |
| 24 | ) => { |
| 25 | $(#[$attr])* |
| 26 | #[cfg_attr(feature = "extra-traits", derive(Debug))] |
| 27 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 28 | pub struct $name $($rest)* |
| 29 | }; |
| 30 | |
| 31 | ( |
| 32 | $(#[$attr:meta])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 33 | pub struct $name:ident $($rest:tt)* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | ) => { |
| 35 | $(#[$attr])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 36 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 37 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 38 | pub struct $name $($rest)* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 39 | }; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | macro_rules! ast_enum { |
| 43 | ( |
| 44 | $(#[$enum_attr:meta])* |
| 45 | pub enum $name:ident { $($variants:tt)* } |
| 46 | ) => ( |
| 47 | $(#[$enum_attr])* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 48 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 49 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 50 | pub enum $name { |
| 51 | $($variants)* |
| 52 | } |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | macro_rules! ast_enum_of_structs { |
| 57 | ( |
| 58 | $(#[$enum_attr:meta])* |
| 59 | pub enum $name:ident { |
| 60 | $( |
| 61 | $(#[$variant_attr:meta])* |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 62 | pub $variant:ident $( ($member:ident $($rest:tt)*) )*, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 63 | )* |
| 64 | } |
| 65 | |
| 66 | $($remaining:tt)* |
| 67 | ) => ( |
| 68 | ast_enum! { |
| 69 | $(#[$enum_attr])* |
| 70 | pub enum $name { |
| 71 | $( |
| 72 | $(#[$variant_attr])* |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 73 | $variant $( ($member) )*, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | )* |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $( |
| 79 | maybe_ast_struct! { |
| 80 | $(#[$variant_attr])* |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 81 | $( |
| 82 | pub struct $member $($rest)* |
| 83 | )* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 86 | $( |
| 87 | impl From<$member> for $name { |
| 88 | fn from(e: $member) -> $name { |
| 89 | $name::$variant(e) |
| 90 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 91 | } |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 92 | )* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 93 | )* |
| 94 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 95 | #[cfg(feature = "printing")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 96 | generate_to_tokens! { |
| 97 | $($remaining)* |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 98 | () |
| 99 | tokens |
| 100 | $name { $($variant $( [$($rest)*] )*,)* } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 101 | } |
| 102 | ) |
| 103 | } |
| 104 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 105 | #[cfg(feature = "printing")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 106 | macro_rules! generate_to_tokens { |
| 107 | (do_not_generate_to_tokens $($foo:tt)*) => (); |
| 108 | |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 109 | (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident, $($next:tt)*}) => { |
| 110 | generate_to_tokens!( |
| 111 | ($($arms)* $name::$variant => {}) |
| 112 | $tokens $name { $($next)* } |
| 113 | ); |
| 114 | }; |
| 115 | |
| 116 | (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident [$($rest:tt)*], $($next:tt)*}) => { |
| 117 | generate_to_tokens!( |
| 118 | ($($arms)* $name::$variant(ref _e) => to_tokens_call!(_e, $tokens, $($rest)*),) |
| 119 | $tokens $name { $($next)* } |
| 120 | ); |
| 121 | }; |
| 122 | |
| 123 | (($($arms:tt)*) $tokens:ident $name:ident {}) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 124 | impl ::quote::ToTokens for $name { |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 125 | fn to_tokens(&self, $tokens: &mut ::quote::Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 126 | match *self { |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 127 | $($arms)* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 131 | }; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 132 | } |
| 133 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 134 | #[cfg(all(feature = "printing", feature = "full"))] |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 135 | macro_rules! to_tokens_call { |
| 136 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 137 | $e.to_tokens($tokens) |
| 138 | }; |
| 139 | } |
| 140 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 141 | #[cfg(all(feature = "printing", not(feature = "full")))] |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 142 | macro_rules! to_tokens_call { |
| 143 | // If the variant is marked as #full, don't auto-generate to-tokens for it. |
| 144 | ($e:ident, $tokens:ident, #full $($rest:tt)*) => { |
| 145 | unreachable!() |
| 146 | }; |
| 147 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 148 | $e.to_tokens($tokens) |
| 149 | }; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | macro_rules! maybe_ast_struct { |
| 153 | ( |
| 154 | $(#[$attr:meta])* |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame^] | 155 | $( |
| 156 | pub struct $name:ident |
| 157 | )* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 158 | ) => (); |
| 159 | |
| 160 | ($($rest:tt)*) => (ast_struct! { $($rest)* }); |
| 161 | } |
David Tolnay | 4c614be | 2017-11-10 00:02:38 -0800 | [diff] [blame] | 162 | |
| 163 | #[cfg(all(feature = "full", feature = "parsing"))] |
| 164 | macro_rules! impl_synom { |
| 165 | ($t:ident $description:tt $($parser:tt)+) => { |
| 166 | impl Synom for $t { |
| 167 | named!(parse -> Self, $($parser)+); |
| 168 | |
| 169 | fn description() -> Option<&'static str> { |
| 170 | Some($description) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |