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