]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
1 | macro_rules! ast_struct { |
2 | ( | |
3 | [$($attrs_pub:tt)*] | |
4 | struct $name:ident #full $($rest:tt)* | |
5 | ) => { | |
6 | #[cfg(feature = "full")] | |
e74abb32 XL |
7 | $($attrs_pub)* struct $name $($rest)* |
8 | ||
9 | #[cfg(not(feature = "full"))] | |
e74abb32 | 10 | $($attrs_pub)* struct $name { |
1b1a35ee | 11 | _noconstruct: ::std::marker::PhantomData<::proc_macro2::Span>, |
e74abb32 XL |
12 | } |
13 | ||
14 | #[cfg(all(not(feature = "full"), feature = "printing"))] | |
15 | impl ::quote::ToTokens for $name { | |
16 | fn to_tokens(&self, _: &mut ::proc_macro2::TokenStream) { | |
17 | unreachable!() | |
18 | } | |
19 | } | |
20 | }; | |
21 | ||
e74abb32 XL |
22 | ( |
23 | [$($attrs_pub:tt)*] | |
24 | struct $name:ident $($rest:tt)* | |
25 | ) => { | |
e74abb32 XL |
26 | $($attrs_pub)* struct $name $($rest)* |
27 | }; | |
28 | ||
29 | ($($t:tt)*) => { | |
30 | strip_attrs_pub!(ast_struct!($($t)*)); | |
31 | }; | |
32 | } | |
33 | ||
34 | macro_rules! ast_enum { | |
35 | // Drop the `#no_visit` attribute, if present. | |
36 | ( | |
37 | [$($attrs_pub:tt)*] | |
38 | enum $name:ident #no_visit $($rest:tt)* | |
39 | ) => ( | |
40 | ast_enum!([$($attrs_pub)*] enum $name $($rest)*); | |
41 | ); | |
42 | ||
e74abb32 XL |
43 | ( |
44 | [$($attrs_pub:tt)*] | |
45 | enum $name:ident $($rest:tt)* | |
46 | ) => ( | |
e74abb32 XL |
47 | $($attrs_pub)* enum $name $($rest)* |
48 | ); | |
49 | ||
50 | ($($t:tt)*) => { | |
51 | strip_attrs_pub!(ast_enum!($($t)*)); | |
52 | }; | |
53 | } | |
54 | ||
55 | macro_rules! ast_enum_of_structs { | |
56 | ( | |
57 | $(#[$enum_attr:meta])* | |
58 | $pub:ident $enum:ident $name:ident #$tag:ident $body:tt | |
59 | $($remaining:tt)* | |
60 | ) => { | |
61 | ast_enum!($(#[$enum_attr])* $pub $enum $name #$tag $body); | |
62 | ast_enum_of_structs_impl!($pub $enum $name $body $($remaining)*); | |
63 | }; | |
64 | ||
65 | ( | |
66 | $(#[$enum_attr:meta])* | |
67 | $pub:ident $enum:ident $name:ident $body:tt | |
68 | $($remaining:tt)* | |
69 | ) => { | |
70 | ast_enum!($(#[$enum_attr])* $pub $enum $name $body); | |
71 | ast_enum_of_structs_impl!($pub $enum $name $body $($remaining)*); | |
72 | }; | |
73 | } | |
74 | ||
75 | macro_rules! ast_enum_of_structs_impl { | |
76 | ( | |
77 | $pub:ident $enum:ident $name:ident { | |
78 | $( | |
79 | $(#[$variant_attr:meta])* | |
80 | $variant:ident $( ($member:ident) )*, | |
81 | )* | |
82 | } | |
83 | ||
84 | $($remaining:tt)* | |
85 | ) => { | |
86 | check_keyword_matches!(pub $pub); | |
87 | check_keyword_matches!(enum $enum); | |
88 | ||
60c5eb7d XL |
89 | $($( |
90 | ast_enum_from_struct!($name::$variant, $member); | |
91 | )*)* | |
e74abb32 XL |
92 | |
93 | #[cfg(feature = "printing")] | |
94 | generate_to_tokens! { | |
95 | $($remaining)* | |
96 | () | |
97 | tokens | |
98 | $name { $($variant $($member)*,)* } | |
99 | } | |
100 | }; | |
101 | } | |
102 | ||
60c5eb7d XL |
103 | macro_rules! ast_enum_from_struct { |
104 | // No From<TokenStream> for verbatim variants. | |
105 | ($name:ident::Verbatim, $member:ident) => {}; | |
106 | ||
107 | ($name:ident::$variant:ident, $member:ident) => { | |
108 | impl From<$member> for $name { | |
109 | fn from(e: $member) -> $name { | |
110 | $name::$variant(e) | |
111 | } | |
112 | } | |
113 | }; | |
114 | } | |
115 | ||
e74abb32 XL |
116 | #[cfg(feature = "printing")] |
117 | macro_rules! generate_to_tokens { | |
118 | (do_not_generate_to_tokens $($foo:tt)*) => (); | |
119 | ||
120 | (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident, $($next:tt)*}) => { | |
121 | generate_to_tokens!( | |
122 | ($($arms)* $name::$variant => {}) | |
123 | $tokens $name { $($next)* } | |
124 | ); | |
125 | }; | |
126 | ||
127 | (($($arms:tt)*) $tokens:ident $name:ident { $variant:ident $member:ident, $($next:tt)*}) => { | |
128 | generate_to_tokens!( | |
129 | ($($arms)* $name::$variant(_e) => _e.to_tokens($tokens),) | |
130 | $tokens $name { $($next)* } | |
131 | ); | |
132 | }; | |
133 | ||
134 | (($($arms:tt)*) $tokens:ident $name:ident {}) => { | |
135 | impl ::quote::ToTokens for $name { | |
136 | fn to_tokens(&self, $tokens: &mut ::proc_macro2::TokenStream) { | |
137 | match self { | |
138 | $($arms)* | |
139 | } | |
140 | } | |
141 | } | |
142 | }; | |
143 | } | |
144 | ||
145 | macro_rules! strip_attrs_pub { | |
146 | ($mac:ident!($(#[$m:meta])* $pub:ident $($t:tt)*)) => { | |
147 | check_keyword_matches!(pub $pub); | |
148 | ||
149 | $mac!([$(#[$m])* $pub] $($t)*); | |
150 | }; | |
151 | } | |
152 | ||
153 | macro_rules! check_keyword_matches { | |
154 | (struct struct) => {}; | |
155 | (enum enum) => {}; | |
156 | (pub pub) => {}; | |
157 | } |