blob: 9925d1788b3db39cf5792a82545204d108f90bb0 [file] [log] [blame]
David Tolnay3cfd1d32018-01-03 00:22:08 -08001#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -07002macro_rules! ast_struct {
3 (
4 $(#[$attr:meta])*
Michael Layzell734adb42017-06-07 16:58:31 -04005 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 Tolnay9c76bcb2017-12-26 23:14:59 -050024 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 Crichton2e0229c2017-05-23 09:34:50 -070034 pub struct $name:ident $($rest:tt)*
Alex Crichton62a0a592017-05-22 13:58:53 -070035 ) => {
36 $(#[$attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070037 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
38 #[cfg_attr(feature = "clone-impls", derive(Clone))]
39 pub struct $name $($rest)*
Michael Layzell734adb42017-06-07 16:58:31 -040040 };
Alex Crichton62a0a592017-05-22 13:58:53 -070041}
42
David Tolnay3cfd1d32018-01-03 00:22:08 -080043#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -070044macro_rules! ast_enum {
45 (
46 $(#[$enum_attr:meta])*
David Tolnay360efd22018-01-04 23:35:26 -080047 pub enum $name:ident $(# $tags:ident)* { $($variants:tt)* }
Alex Crichton62a0a592017-05-22 13:58:53 -070048 ) => (
49 $(#[$enum_attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070050 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
51 #[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichton62a0a592017-05-22 13:58:53 -070052 pub enum $name {
53 $($variants)*
54 }
55 )
56}
57
David Tolnay3cfd1d32018-01-03 00:22:08 -080058#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -070059macro_rules! ast_enum_of_structs {
60 (
61 $(#[$enum_attr:meta])*
62 pub enum $name:ident {
63 $(
64 $(#[$variant_attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -050065 pub $variant:ident $( ($member:ident $($rest:tt)*) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 )*
67 }
68
69 $($remaining:tt)*
70 ) => (
71 ast_enum! {
72 $(#[$enum_attr])*
73 pub enum $name {
74 $(
75 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050076 $variant $( ($member) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 )*
78 }
79 }
80
81 $(
82 maybe_ast_struct! {
83 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050084 $(
85 pub struct $member $($rest)*
86 )*
Alex Crichton62a0a592017-05-22 13:58:53 -070087 }
88
David Tolnayfcfb9002017-12-28 22:04:29 -050089 $(
90 impl From<$member> for $name {
91 fn from(e: $member) -> $name {
92 $name::$variant(e)
93 }
Alex Crichton62a0a592017-05-22 13:58:53 -070094 }
David Tolnayfcfb9002017-12-28 22:04:29 -050095 )*
Alex Crichton62a0a592017-05-22 13:58:53 -070096 )*
97
David Tolnay85f07bb2017-11-12 10:25:17 -080098 #[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -070099 generate_to_tokens! {
100 $($remaining)*
David Tolnayfcfb9002017-12-28 22:04:29 -0500101 ()
102 tokens
103 $name { $($variant $( [$($rest)*] )*,)* }
Alex Crichton62a0a592017-05-22 13:58:53 -0700104 }
105 )
106}
107
David Tolnay3cfd1d32018-01-03 00:22:08 -0800108#[cfg(all(feature = "printing", any(feature = "full", feature = "derive")))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700109macro_rules! generate_to_tokens {
110 (do_not_generate_to_tokens $($foo:tt)*) => ();
111
David Tolnayfcfb9002017-12-28 22:04:29 -0500112 (($($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 Crichton62a0a592017-05-22 13:58:53 -0700127 impl ::quote::ToTokens for $name {
David Tolnayfcfb9002017-12-28 22:04:29 -0500128 fn to_tokens(&self, $tokens: &mut ::quote::Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 match *self {
David Tolnayfcfb9002017-12-28 22:04:29 -0500130 $($arms)*
Alex Crichton62a0a592017-05-22 13:58:53 -0700131 }
132 }
133 }
David Tolnayfcfb9002017-12-28 22:04:29 -0500134 };
Michael Layzell734adb42017-06-07 16:58:31 -0400135}
136
David Tolnay85f07bb2017-11-12 10:25:17 -0800137#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400138macro_rules! to_tokens_call {
139 ($e:ident, $tokens:ident, $($rest:tt)*) => {
140 $e.to_tokens($tokens)
141 };
142}
143
David Tolnay3cfd1d32018-01-03 00:22:08 -0800144#[cfg(all(feature = "printing", feature = "derive", not(feature = "full")))]
Michael Layzell734adb42017-06-07 16:58:31 -0400145macro_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 Crichton62a0a592017-05-22 13:58:53 -0700153}
154
David Tolnay3cfd1d32018-01-03 00:22:08 -0800155#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700156macro_rules! maybe_ast_struct {
157 (
158 $(#[$attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -0500159 $(
160 pub struct $name:ident
161 )*
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 ) => ();
163
164 ($($rest:tt)*) => (ast_struct! { $($rest)* });
165}
David Tolnay4c614be2017-11-10 00:02:38 -0800166
David Tolnay0a0d78c2018-01-05 15:24:01 -0800167#[cfg(all(feature = "parsing", any(feature = "full", feature = "derive")))]
David Tolnay4c614be2017-11-10 00:02:38 -0800168macro_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}