futures_await_quote/
lib.rs1extern crate proc_macro;
62extern crate proc_macro2;
63
64mod tokens;
65pub use tokens::Tokens;
66
67mod to_tokens;
68pub use to_tokens::{ToTokens, ByteStr};
69
70pub mod __rt {
71 pub use proc_macro2::*;
72
73 pub fn parse(tokens: &mut ::Tokens, s: &str) {
74 let s: TokenStream = s.parse().expect("invalid token stream");
75 tokens.append_all(s.into_iter());
76 }
77
78 pub fn append_kind(tokens: &mut ::Tokens, kind: TokenNode) {
79 tokens.append(TokenTree {
80 span: Default::default(),
81 kind: kind,
82 })
83 }
84}
85
86#[macro_export]
88macro_rules! quote {
89 () => {
90 $crate::Tokens::new()
91 };
92
93 ($($tt:tt)+) => {
94 {
95 let mut _s = $crate::Tokens::new();
96 quote_each_token!(_s $($tt)*);
97 _s
98 }
99 };
100}
101
102#[macro_export]
107#[doc(hidden)]
108macro_rules! pounded_var_names {
109 ($finish:ident ($($found:ident)*) # ( $($inner:tt)* ) $($rest:tt)*) => {
110 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
111 };
112
113 ($finish:ident ($($found:ident)*) # [ $($inner:tt)* ] $($rest:tt)*) => {
114 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
115 };
116
117 ($finish:ident ($($found:ident)*) # { $($inner:tt)* } $($rest:tt)*) => {
118 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
119 };
120
121 ($finish:ident ($($found:ident)*) # $first:ident $($rest:tt)*) => {
122 pounded_var_names!($finish ($($found)* $first) $($rest)*)
123 };
124
125 ($finish:ident ($($found:ident)*) ( $($inner:tt)* ) $($rest:tt)*) => {
126 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
127 };
128
129 ($finish:ident ($($found:ident)*) [ $($inner:tt)* ] $($rest:tt)*) => {
130 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
131 };
132
133 ($finish:ident ($($found:ident)*) { $($inner:tt)* } $($rest:tt)*) => {
134 pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
135 };
136
137 ($finish:ident ($($found:ident)*) $ignore:tt $($rest:tt)*) => {
138 pounded_var_names!($finish ($($found)*) $($rest)*)
139 };
140
141 ($finish:ident ($($found:ident)*)) => {
142 $finish!(() $($found)*)
143 };
144}
145
146#[macro_export]
152#[doc(hidden)]
153macro_rules! nested_tuples_pat {
154 (()) => {
155 &()
156 };
157
158 (() $first:ident $($rest:ident)*) => {
159 nested_tuples_pat!(($first) $($rest)*)
160 };
161
162 (($pat:pat) $first:ident $($rest:ident)*) => {
163 nested_tuples_pat!((($pat, $first)) $($rest)*)
164 };
165
166 (($done:pat)) => {
167 $done
168 };
169}
170
171#[macro_export]
177#[doc(hidden)]
178macro_rules! multi_zip_expr {
179 (()) => {
180 &[]
181 };
182
183 (() $single:ident) => {
184 $single
185 };
186
187 (() $first:ident $($rest:ident)*) => {
188 multi_zip_expr!(($first.into_iter()) $($rest)*)
189 };
190
191 (($zips:expr) $first:ident $($rest:ident)*) => {
192 multi_zip_expr!(($zips.zip($first)) $($rest)*)
193 };
194
195 (($done:expr)) => {
196 $done
197 };
198}
199
200#[macro_export]
201#[doc(hidden)]
202macro_rules! quote_each_token {
203 ($tokens:ident) => {};
204
205 ($tokens:ident # ! $($rest:tt)*) => {
206 quote_each_token!($tokens #);
207 quote_each_token!($tokens !);
208 quote_each_token!($tokens $($rest)*);
209 };
210
211 ($tokens:ident # ( $($inner:tt)* ) * $($rest:tt)*) => {
212 for pounded_var_names!(nested_tuples_pat () $($inner)*)
213 in pounded_var_names!(multi_zip_expr () $($inner)*) {
214 quote_each_token!($tokens $($inner)*);
215 }
216 quote_each_token!($tokens $($rest)*);
217 };
218
219 ($tokens:ident # ( $($inner:tt)* ) $sep:tt * $($rest:tt)*) => {
220 for (_i, pounded_var_names!(nested_tuples_pat () $($inner)*))
221 in pounded_var_names!(multi_zip_expr () $($inner)*).into_iter().enumerate() {
222 if _i > 0 {
223 quote_each_token!($tokens $sep);
224 }
225 quote_each_token!($tokens $($inner)*);
226 }
227 quote_each_token!($tokens $($rest)*);
228 };
229
230 ($tokens:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
231 quote_each_token!($tokens #);
232 $crate::__rt::append_kind(&mut $tokens,
233 $crate::__rt::TokenNode::Group(
234 $crate::__rt::Delimiter::Bracket,
235 quote! { $($inner)* }.into()
236 ));
237 quote_each_token!($tokens $($rest)*);
238 };
239
240 ($tokens:ident # $first:ident $($rest:tt)*) => {
241 $crate::ToTokens::to_tokens(&$first, &mut $tokens);
242 quote_each_token!($tokens $($rest)*);
243 };
244
245 ($tokens:ident ( $($first:tt)* ) $($rest:tt)*) => {
246 $crate::__rt::append_kind(&mut $tokens,
247 $crate::__rt::TokenNode::Group(
248 $crate::__rt::Delimiter::Parenthesis,
249 quote! { $($first)* }.into()
250 ));
251 quote_each_token!($tokens $($rest)*);
252 };
253
254 ($tokens:ident [ $($first:tt)* ] $($rest:tt)*) => {
255 $crate::__rt::append_kind(&mut $tokens,
256 $crate::__rt::TokenNode::Group(
257 $crate::__rt::Delimiter::Bracket,
258 quote! { $($first)* }.into()
259 ));
260 quote_each_token!($tokens $($rest)*);
261 };
262
263 ($tokens:ident { $($first:tt)* } $($rest:tt)*) => {
264 $crate::__rt::append_kind(&mut $tokens,
265 $crate::__rt::TokenNode::Group(
266 $crate::__rt::Delimiter::Brace,
267 quote! { $($first)* }.into()
268 ));
269 quote_each_token!($tokens $($rest)*);
270 };
271
272 ($tokens:ident $first:tt $($rest:tt)*) => {
273 $crate::__rt::parse(&mut $tokens, stringify!($first));
275 quote_each_token!($tokens $($rest)*);
276 };
277}