-
Notifications
You must be signed in to change notification settings - Fork 0
/
named_args.rs
373 lines (308 loc) · 9.75 KB
/
named_args.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::{Brace, Paren};
use syn::*;
pub fn parse(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
let f: SynFn = syn::parse(ts).unwrap();
let call = &f.call;
let fn_named = &f.named;
let call_named = &call.named;
let in_brace = &f.in_brace;
let flag_fix_order = call_named.len() <= fn_named.len();
let ts_f = {
let SynFn {
fn_name,
bind,
unnamed,
named,
..
} = &f;
let ts_unnamed = {
quote! { #unnamed }
};
let ts_named = {
quote! { #named }
};
let ts_in_brace = {
if named.is_empty() {
quote! { #in_brace }
} else {
let mut tmp: Punctuated<&Ident, Token![,]> = Punctuated::new();
for SynFnArg { ident, .. } in fn_named.iter() {
tmp.push(ident);
}
quote! {
let #bind @ ( #tmp ) = ( #tmp );
#in_brace
}
}
};
quote! {
fn #fn_name( #ts_unnamed #ts_named ) {
#ts_in_brace
}
}
};
let ts_call = {
let SynCall {
call_name,
trait_call,
unnamed,
..
} = &call;
let flag_fix_trait = trait_call.is_some() && f.named_impl_trait.is_some();
let ts_unnamed = {
quote! { #unnamed }
};
let ts_named = {
let mut fixed_named: Punctuated<TokenStream, Token![,]> = Punctuated::new();
// fixed order
if flag_fix_order && !flag_fix_trait {
'l1: for fn_named in fn_named.iter() {
for call_named in call_named.iter() {
if fn_named.ident == call_named.ident {
let expr = &call_named.expr;
fixed_named.push(quote! { #expr });
continue 'l1;
}
}
}
quote! { #fixed_named }
}
// fixed order
// fixed trait
else if flag_fix_order && flag_fix_trait {
'l1: for fn_named in fn_named.iter() {
for call_named in call_named.iter() {
if fn_named.ident == call_named.ident {
let expr = &call_named.expr;
fixed_named.push(quote! { #expr });
continue 'l1;
}
}
let ty = &fn_named.ty;
// fixed trait
let SynCallNamedTrait { ident, fn_ident } = trait_call.as_ref().unwrap();
let fn_trait_name = f.named_impl_trait.as_ref().unwrap();
let call_trait_name = ident;
assert_eq!(fn_trait_name, call_trait_name);
let call_trait_fn = fn_ident;
fixed_named.push(quote! {
<#ty as #call_trait_name>::#call_trait_fn()
})
}
//dbg!(&fixed_named);
quote! { #fixed_named }
} else {
panic!()
}
};
quote! {
#call_name( #ts_unnamed #ts_named )
}
};
let ts = quote! {
{
#ts_f
#ts_call
}
};
//dbg!(&ts);
ts.into()
}
#[derive(Debug)]
struct SynFn {
_kw: Token![fn],
fn_name: Ident,
_p: Paren,
unnamed: Punctuated<SynFnArg, Token![,]>,
bind: Option<Ident>,
named: Punctuated<SynFnArg, Token![,]>,
named_impl_trait: Option<Ident>,
_b: Brace,
in_brace: TokenStream,
call: SynCall,
}
#[derive(Debug)]
struct SynCall {
call_name: Ident,
_p: Paren,
unnamed: Punctuated<SynCallUnnamed, Token![,]>,
named: Punctuated<SynCallNamed, Token![,]>,
trait_call: Option<SynCallNamedTrait>,
}
#[derive(Debug)]
struct SynFnArg {
ident: Ident,
ty: Path,
}
#[derive(Debug)]
struct SynCallUnnamed {
expr: Expr,
}
#[derive(Debug)]
struct SynCallNamed {
ident: Ident,
expr: Expr,
}
#[derive(Debug)]
struct SynCallNamedTrait {
ident: Ident,
fn_ident: Ident,
}
impl Parse for SynFn {
fn parse(input: ParseStream) -> Result<Self> {
let ts_paren; // ( ... )
let body; //
let mut bind = None;
Ok(Self {
_kw: input.parse()?,
fn_name: input.parse()?,
_p: parenthesized!(ts_paren in input),
unnamed: {
let mut res = Punctuated::new();
// Ident : Path ,
while let Ok(ident) = ts_paren.parse() {
if ts_paren.peek(Token![:]) {
let _: Token![:] = ts_paren.parse()?;
let ty: Path = ts_paren.parse()?;
res.push_value(SynFnArg { ident, ty });
if ts_paren.peek(Token![,]) {
res.push_punct(ts_paren.parse()?);
}
} else if ts_paren.peek(Token![@]) {
let _: Token![@] = ts_paren.parse()?;
bind = Some(ident);
break;
}
}
res
},
named: {
let mut res: Punctuated<SynFnArg, Token![,]> = Punctuated::new();
if bind.is_none() {
} else {
let ts_brace;
braced!(ts_brace in ts_paren);
// Ident : Path ,
while let Ok(i) = ts_brace.parse() {
let _: Token![:] = ts_brace.parse()?;
let ty: Path = ts_brace.parse()?;
res.push_value(SynFnArg { ident: i, ty });
if ts_brace.peek(Token![,]) {
res.push_punct(ts_brace.parse()?);
}
}
}
res
},
named_impl_trait: {
if bind.is_none() {
None
} else {
if !ts_paren.peek(Token![:]) {
panic!()
}
let _: Token![:] = ts_paren.parse()?;
let _: Token![impl] = ts_paren.parse()?;
let ident = ts_paren.parse()?;
if ts_paren.peek(Token![,]) {
let _: Token![,] = ts_paren.parse()?;
}
Some(ident)
}
},
bind,
_b: braced!(body in input),
in_brace: body.parse()?,
call: input.parse()?,
})
}
}
impl Parse for SynFnArg {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
ident: input.parse()?,
ty: input.parse()?,
})
}
}
impl Parse for SynCall {
fn parse(input: ParseStream) -> syn::Result<Self> {
let args;
let mut trait_call = None;
Ok(SynCall {
call_name: input.parse()?,
_p: parenthesized!(args in input),
unnamed: {
let mut res = Punctuated::new();
while let Ok(expr) = args.parse() {
res.push_value(SynCallUnnamed { expr });
if args.peek(Token![,]) {
res.push_punct(args.parse()?);
} else {
break;
}
}
res
},
// e.g.
// `@ { a: 1, b: 2, }: impl Default`
named: {
let mut res = Punctuated::new();
if args.peek(Token![@]) {
let _: Token![@] = args.parse()?;
let brace_args;
let _ = braced!(brace_args in args);
while let Ok(ident) = brace_args.parse() {
let _: Token![:] = brace_args.parse()?;
let expr: Expr = brace_args.parse()?;
res.push_value(SynCallNamed { ident, expr });
res.push_punct(brace_args.parse()?);
}
if brace_args.peek(Token![..]) {
let _: Token![..] = brace_args.parse()?;
let ident = brace_args.parse()?;
let _: Token![::] = brace_args.parse()?;
let fn_ident = brace_args.parse()?;
let _v;
parenthesized!(_v in brace_args);
trait_call = Some(SynCallNamedTrait { ident, fn_ident });
}
}
res
},
trait_call,
})
}
}
impl ToTokens for SynFnArg {
fn to_tokens(&self, tokens: &mut TokenStream) {
let l = &self.ident;
let r = &self.ty;
tokens.extend(quote! {
#l: #r
})
}
}
impl ToTokens for SynCallUnnamed {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self { expr } = self;
tokens.extend(quote! { #expr })
}
}
impl Parse for SynCallUnnamed {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
expr: input.parse()?,
})
}
}
impl ToTokens for SynCallNamed {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self { expr, .. } = self;
tokens.extend(quote! { #expr })
}
}