blob: 17104446d4bbc65b46c61b8fb2a409a37c6410ce [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])*
62 pub $variant:ident($member:ident $($rest:tt)*),
63 )*
64 }
65
66 $($remaining:tt)*
67 ) => (
68 ast_enum! {
69 $(#[$enum_attr])*
70 pub enum $name {
71 $(
72 $(#[$variant_attr])*
73 $variant($member),
74 )*
75 }
76 }
77
78 $(
79 maybe_ast_struct! {
80 $(#[$variant_attr])*
81 pub struct $member $($rest)*
82 }
83
84 impl From<$member> for $name {
85 fn from(e: $member) -> $name {
86 $name::$variant(e)
87 }
88 }
89 )*
90
David Tolnay85f07bb2017-11-12 10:25:17 -080091 #[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -070092 generate_to_tokens! {
93 $($remaining)*
Michael Layzell734adb42017-06-07 16:58:31 -040094 enum $name { $($variant [$($rest)*],)* }
Alex Crichton62a0a592017-05-22 13:58:53 -070095 }
96 )
97}
98
David Tolnay85f07bb2017-11-12 10:25:17 -080099#[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700100macro_rules! generate_to_tokens {
101 (do_not_generate_to_tokens $($foo:tt)*) => ();
102
Michael Layzell734adb42017-06-07 16:58:31 -0400103 (enum $name:ident { $($variant:ident [$($rest:tt)*],)* }) => (
Alex Crichton62a0a592017-05-22 13:58:53 -0700104 impl ::quote::ToTokens for $name {
105 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
106 match *self {
107 $(
Michael Layzell734adb42017-06-07 16:58:31 -0400108 $name::$variant(ref _e) =>
109 to_tokens_call!(_e, tokens, $($rest)*),
Alex Crichton62a0a592017-05-22 13:58:53 -0700110 )*
111 }
112 }
113 }
Michael Layzell734adb42017-06-07 16:58:31 -0400114 );
115}
116
David Tolnay85f07bb2017-11-12 10:25:17 -0800117#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400118macro_rules! to_tokens_call {
119 ($e:ident, $tokens:ident, $($rest:tt)*) => {
120 $e.to_tokens($tokens)
121 };
122}
123
David Tolnay85f07bb2017-11-12 10:25:17 -0800124#[cfg(all(feature = "printing", not(feature = "full")))]
Michael Layzell734adb42017-06-07 16:58:31 -0400125macro_rules! to_tokens_call {
126 // If the variant is marked as #full, don't auto-generate to-tokens for it.
127 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
128 unreachable!()
129 };
130 ($e:ident, $tokens:ident, $($rest:tt)*) => {
131 $e.to_tokens($tokens)
132 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700133}
134
135macro_rules! maybe_ast_struct {
136 (
137 $(#[$attr:meta])*
138 pub struct $name:ident
139 ) => ();
140
141 ($($rest:tt)*) => (ast_struct! { $($rest)* });
142}
David Tolnay4c614be2017-11-10 00:02:38 -0800143
144#[cfg(all(feature = "full", feature = "parsing"))]
145macro_rules! impl_synom {
146 ($t:ident $description:tt $($parser:tt)+) => {
147 impl Synom for $t {
148 named!(parse -> Self, $($parser)+);
149
150 fn description() -> Option<&'static str> {
151 Some($description)
152 }
153 }
154 }
155}