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