blob: 69a405e5796bbd2956e74c6b2625aed043bc27e2 [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
81 generate_to_tokens! {
82 $($remaining)*
Michael Layzell734adb42017-06-07 16:58:31 -040083 enum $name { $($variant [$($rest)*],)* }
Alex Crichton62a0a592017-05-22 13:58:53 -070084 }
85 )
86}
87
88macro_rules! generate_to_tokens {
89 (do_not_generate_to_tokens $($foo:tt)*) => ();
90
Michael Layzell734adb42017-06-07 16:58:31 -040091 (enum $name:ident { $($variant:ident [$($rest:tt)*],)* }) => (
Alex Crichton62a0a592017-05-22 13:58:53 -070092 #[cfg(feature = "printing")]
93 impl ::quote::ToTokens for $name {
94 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
95 match *self {
96 $(
Michael Layzell734adb42017-06-07 16:58:31 -040097 $name::$variant(ref _e) =>
98 to_tokens_call!(_e, tokens, $($rest)*),
Alex Crichton62a0a592017-05-22 13:58:53 -070099 )*
100 }
101 }
102 }
Michael Layzell734adb42017-06-07 16:58:31 -0400103 );
104}
105
106#[cfg(feature = "full")]
107macro_rules! to_tokens_call {
108 ($e:ident, $tokens:ident, $($rest:tt)*) => {
109 $e.to_tokens($tokens)
110 };
111}
112
113#[cfg(not(feature = "full"))]
114macro_rules! to_tokens_call {
115 // If the variant is marked as #full, don't auto-generate to-tokens for it.
116 ($e:ident, $tokens:ident, #full $($rest:tt)*) => {
117 unreachable!()
118 };
119 ($e:ident, $tokens:ident, $($rest:tt)*) => {
120 $e.to_tokens($tokens)
121 };
Alex Crichton62a0a592017-05-22 13:58:53 -0700122}
123
124macro_rules! maybe_ast_struct {
125 (
126 $(#[$attr:meta])*
127 pub struct $name:ident
128 ) => ();
129
130 ($($rest:tt)*) => (ast_struct! { $($rest)* });
131}