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