blob: cf496403319877c011575a1dfcd4c1ca347ef578 [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])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070023 pub struct $name:ident $($rest:tt)*
Alex Crichton62a0a592017-05-22 13:58:53 -070024 ) => {
25 $(#[$attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070026 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
27 #[cfg_attr(feature = "clone-impls", derive(Clone))]
28 pub struct $name $($rest)*
Michael Layzell734adb42017-06-07 16:58:31 -040029 };
Alex Crichton62a0a592017-05-22 13:58:53 -070030}
31
32macro_rules! ast_enum {
33 (
34 $(#[$enum_attr:meta])*
35 pub enum $name:ident { $($variants:tt)* }
36 ) => (
37 $(#[$enum_attr])*
Alex Crichton2e0229c2017-05-23 09:34:50 -070038 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
39 #[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichton62a0a592017-05-22 13:58:53 -070040 pub enum $name {
41 $($variants)*
42 }
43 )
44}
45
46macro_rules! ast_enum_of_structs {
47 (
48 $(#[$enum_attr:meta])*
49 pub enum $name:ident {
50 $(
51 $(#[$variant_attr:meta])*
52 pub $variant:ident($member:ident $($rest:tt)*),
53 )*
54 }
55
56 $($remaining:tt)*
57 ) => (
58 ast_enum! {
59 $(#[$enum_attr])*
60 pub enum $name {
61 $(
62 $(#[$variant_attr])*
63 $variant($member),
64 )*
65 }
66 }
67
68 $(
69 maybe_ast_struct! {
70 $(#[$variant_attr])*
71 pub struct $member $($rest)*
72 }
73
74 impl From<$member> for $name {
75 fn from(e: $member) -> $name {
76 $name::$variant(e)
77 }
78 }
79 )*
80
David Tolnay85f07bb2017-11-12 10:25:17 -080081 #[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -070082 generate_to_tokens! {
83 $($remaining)*
Michael Layzell734adb42017-06-07 16:58:31 -040084 enum $name { $($variant [$($rest)*],)* }
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }
86 )
87}
88
David Tolnay85f07bb2017-11-12 10:25:17 -080089#[cfg(feature = "printing")]
Alex Crichton62a0a592017-05-22 13:58:53 -070090macro_rules! generate_to_tokens {
91 (do_not_generate_to_tokens $($foo:tt)*) => ();
92
Michael Layzell734adb42017-06-07 16:58:31 -040093 (enum $name:ident { $($variant:ident [$($rest:tt)*],)* }) => (
Alex Crichton62a0a592017-05-22 13:58:53 -070094 impl ::quote::ToTokens for $name {
95 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
96 match *self {
97 $(
Michael Layzell734adb42017-06-07 16:58:31 -040098 $name::$variant(ref _e) =>
99 to_tokens_call!(_e, tokens, $($rest)*),
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 )*
101 }
102 }
103 }
Michael Layzell734adb42017-06-07 16:58:31 -0400104 );
105}
106
David Tolnay85f07bb2017-11-12 10:25:17 -0800107#[cfg(all(feature = "printing", feature = "full"))]
Michael Layzell734adb42017-06-07 16:58:31 -0400108macro_rules! to_tokens_call {
109 ($e:ident, $tokens:ident, $($rest:tt)*) => {
110 $e.to_tokens($tokens)
111 };
112}
113
David Tolnay85f07bb2017-11-12 10:25:17 -0800114#[cfg(all(feature = "printing", not(feature = "full")))]
Michael Layzell734adb42017-06-07 16:58:31 -0400115macro_rules! to_tokens_call {
116 // If the variant is marked as #full, don't auto-generate to-tokens for it.
117 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
118 unreachable!()
119 };
120 ($e:ident, $tokens:ident, $($rest:tt)*) => {
121 $e.to_tokens($tokens)
122 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700123}
124
125macro_rules! maybe_ast_struct {
126 (
127 $(#[$attr:meta])*
128 pub struct $name:ident
129 ) => ();
130
131 ($($rest:tt)*) => (ast_struct! { $($rest)* });
132}
David Tolnay4c614be2017-11-10 00:02:38 -0800133
134#[cfg(all(feature = "full", feature = "parsing"))]
135macro_rules! impl_synom {
136 ($t:ident $description:tt $($parser:tt)+) => {
137 impl Synom for $t {
138 named!(parse -> Self, $($parser)+);
139
140 fn description() -> Option<&'static str> {
141 Some($description)
142 }
143 }
144 }
145}