blob: 519c73cf8e4c01a1672dad56f0fc6ec7222958f5 [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 Tolnay278f9e32018-08-14 22:41:11 -0700116#[cfg(all(
117 feature = "printing",
118 any(feature = "full", feature = "derive")
119))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700120macro_rules! generate_to_tokens {
121 (do_not_generate_to_tokens $($foo:tt)*) => ();
122
David Tolnayfcfb9002017-12-28 22:04:29 -0500123 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident, $($next:tt)*}) => {
124 generate_to_tokens!(
125 ($($arms)* $name::$variant => {})
126 $tokens $name { $($next)* }
127 );
128 };
129
130 (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident [$($rest:tt)*], $($next:tt)*}) => {
131 generate_to_tokens!(
132 ($($arms)* $name::$variant(ref _e) => to_tokens_call!(_e, $tokens, $($rest)*),)
133 $tokens $name { $($next)* }
134 );
135 };
136
137 (($($arms:tt)*) $tokens:ident $name:ident {}) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700139 fn to_tokens(&self, $tokens: &mut ::proc_macro2::TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 match *self {
David Tolnayfcfb9002017-12-28 22:04:29 -0500141 $($arms)*
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }
143 }
144 }
David Tolnayfcfb9002017-12-28 22:04:29 -0500145 };
Michael Layzell734adb42017-06-07 16:58:31 -0400146}
147
David Tolnay85f07bb2017-11-12 10:25:17 -0800148#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400149macro_rules! to_tokens_call {
150 ($e:ident, $tokens:ident, $($rest:tt)*) => {
151 $e.to_tokens($tokens)
152 };
153}
154
David Tolnay278f9e32018-08-14 22:41:11 -0700155#[cfg(all(
156 feature = "printing",
157 feature = "derive",
158 not(feature = "full")
159))]
Michael Layzell734adb42017-06-07 16:58:31 -0400160macro_rules! to_tokens_call {
161 // If the variant is marked as #full, don't auto-generate to-tokens for it.
162 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
163 unreachable!()
164 };
165 ($e:ident, $tokens:ident, $($rest:tt)*) => {
166 $e.to_tokens($tokens)
167 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700168}
169
David Tolnay3cfd1d32018-01-03 00:22:08 -0800170#[cfg(any(feature = "full", feature = "derive"))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700171macro_rules! maybe_ast_struct {
172 (
173 $(#[$attr:meta])*
David Tolnayfcfb9002017-12-28 22:04:29 -0500174 $(
175 pub struct $name:ident
176 )*
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 ) => ();
178
179 ($($rest:tt)*) => (ast_struct! { $($rest)* });
180}