blob: 9b737171bb7479f2a991c772cf1c729e2ec642d6 [file] [log] [blame]
Alex Crichton62a0a592017-05-22 13:58:53 -07001macro_rules! ast_struct {
2 (
3 $(#[$attr:meta])*
Michael Layzell734adb42017-06-07 16:58:31 -04004 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 Tolnay9c76bcb2017-12-26 23:14:59 -050023 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 Crichton2e0229c2017-05-23 09:34:50 -070033 pub struct $name:ident $($rest:tt)*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 ) => {
35 $(#[$attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070036 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
37 #[cfg_attr(feature = "clone-impls", derive(Clone))]
38 pub struct $name $($rest)*
Michael Layzell734adb42017-06-07 16:58:31 -040039 };
Alex Crichton62a0a592017-05-22 13:58:53 -070040}
41
42macro_rules! ast_enum {
43 (
44 $(#[$enum_attr:meta])*
45 pub enum $name:ident { $($variants:tt)* }
46 ) => (
47 $(#[$enum_attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070048 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
49 #[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichton62a0a592017-05-22 13:58:53 -070050 pub enum $name {
51 $($variants)*
52 }
53 )
54}
55
56macro_rules! ast_enum_of_structs {
57 (
58 $(#[$enum_attr:meta])*
59 pub enum $name:ident {
60 $(
61 $(#[$variant_attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -050062 pub $variant:ident $( ($member:ident $($rest:tt)*) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070063 )*
64 }
65
66 $($remaining:tt)*
67 ) => (
68 ast_enum! {
69 $(#[$enum_attr])*
70 pub enum $name {
71 $(
72 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050073 $variant $( ($member) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 )*
75 }
76 }
77
78 $(
79 maybe_ast_struct! {
80 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050081 $(
82 pub struct $member $($rest)*
83 )*
Alex Crichton62a0a592017-05-22 13:58:53 -070084 }
85
David Tolnayfcfb9002017-12-28 22:04:29 -050086 $(
87 impl From<$member> for $name {
88 fn from(e: $member) -> $name {
89 $name::$variant(e)
90 }
Alex Crichton62a0a592017-05-22 13:58:53 -070091 }
David Tolnayfcfb9002017-12-28 22:04:29 -050092 )*
Alex Crichton62a0a592017-05-22 13:58:53 -070093 )*
94
David Tolnay85f07bb2017-11-12 10:25:17 -080095 #[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -070096 generate_to_tokens! {
97 $($remaining)*
David Tolnayfcfb9002017-12-28 22:04:29 -050098 ()
99 tokens
100 $name { $($variant $( [$($rest)*] )*,)* }
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }
102 )
103}
104
David Tolnay85f07bb2017-11-12 10:25:17 -0800105#[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700106macro_rules! generate_to_tokens {
107 (do_not_generate_to_tokens $($foo:tt)*) => ();
108
David Tolnayfcfb9002017-12-28 22:04:29 -0500109 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident, $($next:tt)*}) => {
110 generate_to_tokens!(
111 ($($arms)* $name::$variant => {})
112 $tokens $name { $($next)* }
113 );
114 };
115
116 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident [$($rest:tt)*], $($next:tt)*}) => {
117 generate_to_tokens!(
118 ($($arms)* $name::$variant(ref _e) => to_tokens_call!(_e, $tokens, $($rest)*),)
119 $tokens $name { $($next)* }
120 );
121 };
122
123 (($($arms:tt)*) $tokens:ident $name:ident {}) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 impl ::quote::ToTokens for $name {
David Tolnayfcfb9002017-12-28 22:04:29 -0500125 fn to_tokens(&self, $tokens: &mut ::quote::Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 match *self {
David Tolnayfcfb9002017-12-28 22:04:29 -0500127 $($arms)*
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 }
129 }
130 }
David Tolnayfcfb9002017-12-28 22:04:29 -0500131 };
Michael Layzell734adb42017-06-07 16:58:31 -0400132}
133
David Tolnay85f07bb2017-11-12 10:25:17 -0800134#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400135macro_rules! to_tokens_call {
136 ($e:ident, $tokens:ident, $($rest:tt)*) => {
137 $e.to_tokens($tokens)
138 };
139}
140
David Tolnay85f07bb2017-11-12 10:25:17 -0800141#[cfg(all(feature = "printing", not(feature = "full")))]
Michael Layzell734adb42017-06-07 16:58:31 -0400142macro_rules! to_tokens_call {
143 // If the variant is marked as #full, don't auto-generate to-tokens for it.
144 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
145 unreachable!()
146 };
147 ($e:ident, $tokens:ident, $($rest:tt)*) => {
148 $e.to_tokens($tokens)
149 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700150}
151
152macro_rules! maybe_ast_struct {
153 (
154 $(#[$attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -0500155 $(
156 pub struct $name:ident
157 )*
Alex Crichton62a0a592017-05-22 13:58:53 -0700158 ) => ();
159
160 ($($rest:tt)*) => (ast_struct! { $($rest)* });
161}
David Tolnay4c614be2017-11-10 00:02:38 -0800162
163#[cfg(all(feature = "full", feature = "parsing"))]
164macro_rules! impl_synom {
165 ($t:ident $description:tt $($parser:tt)+) => {
166 impl Synom for $t {
167 named!(parse -> Self, $($parser)+);
168
169 fn description() -> Option<&'static str> {
170 Some($description)
171 }
172 }
173 }
174}