blob: e9b70346086ae1e925c9ab09c4027ac7eee8f332 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnay3cfd1d32018-01-03 00:22:08 -08009#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -070010macro_rules! ast_struct {
11 (
12 $(#[$attr:meta])*
Michael Layzell734adb42017-06-07 16:58:31 -040013 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 Tolnay9c76bcb2017-12-26 23:14:59 -050032 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 Crichton2e0229c2017-05-23 09:34:50 -070042 pub struct $name:ident $($rest:tt)*
Alex Crichton62a0a592017-05-22 13:58:53 -070043 ) => {
44 $(#[$attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070045 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
46 #[cfg_attr(feature = "clone-impls", derive(Clone))]
47 pub struct $name $($rest)*
Michael Layzell734adb42017-06-07 16:58:31 -040048 };
Alex Crichton62a0a592017-05-22 13:58:53 -070049}
50
David Tolnay3cfd1d32018-01-03 00:22:08 -080051#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -070052macro_rules! ast_enum {
53 (
54 $(#[$enum_attr:meta])*
David Tolnay360efd22018-01-04 23:35:26 -080055 pub enum $name:ident $(# $tags:ident)* { $($variants:tt)* }
Alex Crichton62a0a592017-05-22 13:58:53 -070056 ) => (
57 $(#[$enum_attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070058 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
59 #[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichton62a0a592017-05-22 13:58:53 -070060 pub enum $name {
61 $($variants)*
62 }
63 )
64}
65
David Tolnay3cfd1d32018-01-03 00:22:08 -080066#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -070067macro_rules! ast_enum_of_structs {
68 (
69 $(#[$enum_attr:meta])*
70 pub enum $name:ident {
71 $(
72 $(#[$variant_attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -050073 pub $variant:ident $( ($member:ident $($rest:tt)*) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 )*
75 }
76
77 $($remaining:tt)*
78 ) => (
79 ast_enum! {
80 $(#[$enum_attr])*
81 pub enum $name {
82 $(
83 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050084 $variant $( ($member) )*,
Alex Crichton62a0a592017-05-22 13:58:53 -070085 )*
86 }
87 }
88
89 $(
90 maybe_ast_struct! {
91 $(#[$variant_attr])*
David Tolnayfcfb9002017-12-28 22:04:29 -050092 $(
93 pub struct $member $($rest)*
94 )*
Alex Crichton62a0a592017-05-22 13:58:53 -070095 }
96
David Tolnayfcfb9002017-12-28 22:04:29 -050097 $(
98 impl From<$member> for $name {
99 fn from(e: $member) -> $name {
100 $name::$variant(e)
101 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 }
David Tolnayfcfb9002017-12-28 22:04:29 -0500103 )*
Alex Crichton62a0a592017-05-22 13:58:53 -0700104 )*
105
David Tolnay85f07bb2017-11-12 10:25:17 -0800106 #[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 generate_to_tokens! {
108 $($remaining)*
David Tolnayfcfb9002017-12-28 22:04:29 -0500109 ()
110 tokens
111 $name { $($variant $( [$($rest)*] )*,)* }
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }
113 )
114}
115
David Tolnay0cec3e62018-07-21 09:08:30 -0700116#[cfg(
117 all(
118 feature = "printing",
119 any(feature = "full", feature = "derive")
120 )
121)]
Alex Crichton62a0a592017-05-22 13:58:53 -0700122macro_rules! generate_to_tokens {
123 (do_not_generate_to_tokens $($foo:tt)*) => ();
124
David Tolnayfcfb9002017-12-28 22:04:29 -0500125 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident, $($next:tt)*}) => {
126 generate_to_tokens!(
127 ($($arms)* $name::$variant => {})
128 $tokens $name { $($next)* }
129 );
130 };
131
132 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident [$($rest:tt)*], $($next:tt)*}) => {
133 generate_to_tokens!(
134 ($($arms)* $name::$variant(ref _e) => to_tokens_call!(_e, $tokens, $($rest)*),)
135 $tokens $name { $($next)* }
136 );
137 };
138
139 (($($arms:tt)*) $tokens:ident $name:ident {}) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700141 fn to_tokens(&self, $tokens: &mut ::proc_macro2::TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 match *self {
David Tolnayfcfb9002017-12-28 22:04:29 -0500143 $($arms)*
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }
145 }
146 }
David Tolnayfcfb9002017-12-28 22:04:29 -0500147 };
Michael Layzell734adb42017-06-07 16:58:31 -0400148}
149
David Tolnay85f07bb2017-11-12 10:25:17 -0800150#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400151macro_rules! to_tokens_call {
152 ($e:ident, $tokens:ident, $($rest:tt)*) => {
153 $e.to_tokens($tokens)
154 };
155}
156
David Tolnay0cec3e62018-07-21 09:08:30 -0700157#[cfg(
158 all(
159 feature = "printing",
160 feature = "derive",
161 not(feature = "full")
162 )
163)]
Michael Layzell734adb42017-06-07 16:58:31 -0400164macro_rules! to_tokens_call {
165 // If the variant is marked as #full, don't auto-generate to-tokens for it.
166 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
167 unreachable!()
168 };
169 ($e:ident, $tokens:ident, $($rest:tt)*) => {
170 $e.to_tokens($tokens)
171 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700172}
173
David Tolnay3cfd1d32018-01-03 00:22:08 -0800174#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700175macro_rules! maybe_ast_struct {
176 (
177 $(#[$attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -0500178 $(
179 pub struct $name:ident
180 )*
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 ) => ();
182
183 ($($rest:tt)*) => (ast_struct! { $($rest)* });
184}
David Tolnay4c614be2017-11-10 00:02:38 -0800185
David Tolnay0cec3e62018-07-21 09:08:30 -0700186#[cfg(
187 all(
188 feature = "parsing",
189 any(feature = "full", feature = "derive")
190 )
191)]
David Tolnay4c614be2017-11-10 00:02:38 -0800192macro_rules! impl_synom {
193 ($t:ident $description:tt $($parser:tt)+) => {
194 impl Synom for $t {
195 named!(parse -> Self, $($parser)+);
196
197 fn description() -> Option<&'static str> {
198 Some($description)
199 }
200 }
201 }
202}