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])* |
| 62 | pub $variant:ident($member:ident $($rest:tt)*), |
| 63 | )* |
| 64 | } |
| 65 | |
| 66 | $($remaining:tt)* |
| 67 | ) => ( |
| 68 | ast_enum! { |
| 69 | $(#[$enum_attr])* |
| 70 | pub enum $name { |
| 71 | $( |
| 72 | $(#[$variant_attr])* |
| 73 | $variant($member), |
| 74 | )* |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $( |
| 79 | maybe_ast_struct! { |
| 80 | $(#[$variant_attr])* |
| 81 | pub struct $member $($rest)* |
| 82 | } |
| 83 | |
| 84 | impl From<$member> for $name { |
| 85 | fn from(e: $member) -> $name { |
| 86 | $name::$variant(e) |
| 87 | } |
| 88 | } |
| 89 | )* |
| 90 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 91 | #[cfg(feature = "printing")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 92 | generate_to_tokens! { |
| 93 | $($remaining)* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 94 | enum $name { $($variant [$($rest)*],)* } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 95 | } |
| 96 | ) |
| 97 | } |
| 98 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 99 | #[cfg(feature = "printing")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 100 | macro_rules! generate_to_tokens { |
| 101 | (do_not_generate_to_tokens $($foo:tt)*) => (); |
| 102 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 103 | (enum $name:ident { $($variant:ident [$($rest:tt)*],)* }) => ( |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 104 | impl ::quote::ToTokens for $name { |
| 105 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 106 | match *self { |
| 107 | $( |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 108 | $name::$variant(ref _e) => |
| 109 | to_tokens_call!(_e, tokens, $($rest)*), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 110 | )* |
| 111 | } |
| 112 | } |
| 113 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 114 | ); |
| 115 | } |
| 116 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 117 | #[cfg(all(feature = "printing", feature = "full"))] |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 118 | macro_rules! to_tokens_call { |
| 119 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 120 | $e.to_tokens($tokens) |
| 121 | }; |
| 122 | } |
| 123 | |
David Tolnay | 85f07bb | 2017-11-12 10:25:17 -0800 | [diff] [blame] | 124 | #[cfg(all(feature = "printing", not(feature = "full")))] |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 125 | macro_rules! to_tokens_call { |
| 126 | // If the variant is marked as #full, don't auto-generate to-tokens for it. |
| 127 | ($e:ident, $tokens:ident, #full $($rest:tt)*) => { |
| 128 | unreachable!() |
| 129 | }; |
| 130 | ($e:ident, $tokens:ident, $($rest:tt)*) => { |
| 131 | $e.to_tokens($tokens) |
| 132 | }; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | macro_rules! maybe_ast_struct { |
| 136 | ( |
| 137 | $(#[$attr:meta])* |
| 138 | pub struct $name:ident |
| 139 | ) => (); |
| 140 | |
| 141 | ($($rest:tt)*) => (ast_struct! { $($rest)* }); |
| 142 | } |
David Tolnay | 4c614be | 2017-11-10 00:02:38 -0800 | [diff] [blame] | 143 | |
| 144 | #[cfg(all(feature = "full", feature = "parsing"))] |
| 145 | macro_rules! impl_synom { |
| 146 | ($t:ident $description:tt $($parser:tt)+) => { |
| 147 | impl Synom for $t { |
| 148 | named!(parse -> Self, $($parser)+); |
| 149 | |
| 150 | fn description() -> Option<&'static str> { |
| 151 | Some($description) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |