dmutil 0.1.0

A collection of utility macros to ease the creation of other macros.
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
///
/// Emulates eager expansion of macros.
///
/// # Example
/// ```
/// #[macro_use]
/// extern crate dmutil;
///
/// eager_macro_rules!{
///     plus_1 $eager_1 $eager_2
///     ()=>{
///         + 1
///     };
/// }
///
/// fn main(){
/// 	assert_eq!(4, eager!{2 plus_1!() plus_1!()});
/// }
/// ```
///
/// # Usage
///
/// `eager!` can wrap any code, and if that code contains a macro call, that macro will be
/// expanded before its consumer. This means:
///
/// * If a macro call is given as an argument to another macro, the first macro will be expanded
/// first.
/// * All macros will be fully expanded before `eager!` expands, meaning otherwise illegal
/// intermediate expansion steps can be made possible.
///
/// `eager!` does not work with any macro. Only macros declared using [eager_macro_rules!] may be
/// used inside `eager!`. Such macros are said to be `eager!`-enabled.
///
/// [eager_macro_rules!]: macro.eager_macro_rules.html
///
/// # Cons
///
/// * Because of the way `eager!` is implemented; being a hack of recursive macroes, the compiler's
/// default macro recursion limit is quickly exceeded. Therefore, `#![recursion_limit="256"]`
/// must be used in most situations (potentially with a higher limit)
/// such that expansion can happen.
///
/// * Debugging an eagerly expanded macro is very difficult and requires intimate knowledge
/// of the implementation of `eager!`. There is no way to mitigate this, except to try and
/// recreate the bug without using `eager!`. Likewise, the error messages the compiler will
/// emit are exponentially more cryptic than they already would have been.
///
/// * Only works with `eager!`-enabled macros. Additionally, none of the macros may
/// expand to something containing a non-`eager!`-enabled macro, not even as an intermediate
/// expansion.
///
/// ---
/// # Macro expansions
///
/// Rust is lazy when it comes to macro expansion. When the compiler sees a macro call, it will
/// try to expand the macro without looking at its arguments or what the expansion becomes.
/// Using `eager!`, previously illegal macro expansions can be made possible.
///
/// ## Macro restrictions
/// This puts a few restrictions on what can and cannot be done with macros:
///
/// ### The arguments to a macro usually cannot be the resulting expansion of another macro call:
/// Say you have a macro that adds two numbers:
/// ```ignore
/// macro_rules! add{
///     ($e1:expr, $e2:expr)=> {$e1 + $e2}
/// }
/// ```
/// And a macro that expands to two comma-separated numbers:
///
/// ```ignore
/// macro_rules! two{
///     ()=>{2,3}
/// }
/// ```
///
/// You cannot use the expansion of `two!` as an argument to `add!`:
/// ```ignore
/// let x = add!(two!()); // error
/// ```
/// The compiler will complain about no rule in `add!` accepting `two`, since `two!()` does not
/// get expanded before the `add!` who requires two expression and not just one.
///
/// With eager expansion, this can be made possible:
/// ```
/// #[macro_use]
/// extern crate dmutil;
///
/// eager_macro_rules!{
///     add $eager_1 $eager_2
///     ($e1:expr, $e2:expr)=> {$e1 + $e2}
/// }
///
/// eager_macro_rules!{
///     two $eager_1 $eager_2
///     ()=>{2,3}
/// }
///
/// fn main(){
/// 	let x = eager!{add!(two!())};
/// 	assert_eq!(5, x);
/// }
/// ```
///
/// ### An intermediate expansion step cannot result in invalid syntax
///
/// Say you have a macro that expands to an identifier:
/// ```ignore
/// macro_rules! id{
///     ()=> {SomeStruct}
/// }
/// ```
/// And want a macro that, using the previous macro, expands to a struct declaration:
/// ```ignore
/// macro_rules! some_struct{
///     ()=> {struct id!(){}}
/// }
/// ```
/// Calling this macro will not compile. Looking at the result of the first expansion
/// step we can see why:
/// ```ignore
/// struct id!() {}
/// ```
/// Since macro calls are illegal in identifier positions, the compiler will refuse to continue
/// expanding.
///
///  With eager expansion, this can be made possible:
/// ```
/// #[macro_use]
/// extern crate dmutil;
///
/// eager_macro_rules!{
///     id $eager_1 $eager_2
///     ()=> {SomeStruct}
/// }
///
/// eager_macro_rules!{
///     some_struct $eager_1 $eager_2
///     ()=>{struct id!(){}}
/// }
///
/// eager!{some_struct!{}}
///
/// fn main(){}
/// ```
///
/// # Trivia
///
/// Ironically, `eager!` is not itself `eager!`-enabled,
/// though it does ignore itself if it is nested.
///
///
#[macro_export]
macro_rules! eager{
	(
		$($all:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[][]]
			]
			$($all)*
		}
	};
}

#[macro_export]
#[doc(hidden)]
macro_rules! eager_internal{
	(	// From macro expansion
		@from_macro[ $prefix:tt $($rest:tt)* ]
		$($input:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[$prefix []]
				$($rest)*
			]
			$($input)*
		}
	};
// Decode input stream
	(	// If the next token is a block, check it (brace type)
		@check_expansion[
			[[$($prefix:tt)*][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
		{$($body:tt)*} $($rest:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[][]]
				[[$($prefix)*][$($rest)*]{}]
				$([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)*
		}
	};
	(	// If the next token is a block, check it (parenthesis type)
		@check_expansion[
			[[$($prefix:tt)*][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
		($($body:tt)*) $($rest:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[][]]
				[[$($prefix)*][$($rest)*]()]
				$([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)*
		}
	};
	(	// If the next token is a n 'eager!' macro call, ignore it,
		// extracting the body. (brace type)
		@check_expansion[
			[[$($prefix:tt)*][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
		eager!{$($body:tt)*} $($rest:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[$($prefix)*][]]
				$([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)* $($rest)*
		}
	};
	(	// If the next token is a n 'eager!' macro call, ignore it,
		// extracting the body. (brace type)
		@check_expansion[
			[[$($prefix:tt)*][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
		eager!($($body:tt)*) $($rest:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[$($prefix)*][]]
				$([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)* $($rest)*
		}
	};
	(	// If the next token isn't any of the above
		// it is safe to add it to the prefix
		@check_expansion[
			[[$($prefix:tt)*][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
		$next:tt $($rest:tt)*
	)=>{
		eager_internal!{
			@check_expansion[
				[[$next $($prefix)*][]]
				$([$prefix_rest $postfix_rest $block_type])*
			]
			$($rest)*
		}
	};
	
// Done decoding input
	(	// When there is no more input and the last input was a macro call
		// (brace type)
		@check_expansion[
			[[! $macro_name:tt $($prefix:tt)*][$($postfix:tt)*]{$($body:tt)*}]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
	)=>{
		$macro_name!{
			@eager[
				 [$($postfix)*] [$($prefix)*]
				 $([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)*
		}
	};
	(	// When there is no more input and the last input was a macro call
		// (parenthesis type)
		@check_expansion[
			[[! $macro_name:tt $($prefix:tt)*][$($postfix:tt)*]($($body:tt)*)]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
	)=>{
		$macro_name!{
			@eager[
				 [$($postfix)*] [$($prefix)*]
				 $([$prefix_rest $postfix_rest $block_type])*
			]
			$($body)*
		}
	};
	(	// When there is no more input and the last input wasn't a macro call
		// insert it into the previous block (brace type)
		@check_expansion[
			[[$last:tt $($last_rest:tt)*][]]
			[$prefix:tt $postfix:tt {$($body:tt)*}]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
	)=>{
		eager_internal!{
			@check_expansion[
				[[$($last_rest)*][]]
				[$prefix $postfix {$last $($body)*}]
				$([$prefix_rest $postfix_rest $block_type])*
			]
		}
	};
	(	// When there is no more input and the last input wasn't a macro call
		// insert it into the previous block (parenthesis type)
		@check_expansion[
			[[$last:tt $($last_rest:tt)*][]]
			[$prefix:tt $postfix:tt ($($body:tt)*)]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])*
		]
	)=>{
		eager_internal!{
			@check_expansion[
				[[$($last_rest)*][]]
				[$prefix $postfix ($last $($body)*)]
				$([$prefix_rest $postfix_rest $block_type])*
			]
		}
	};
	(	// When all input has been promoted to the previous block
		// remove the input catcher
		@check_expansion[
			[[][]]
			$([$prefix_rest:tt $postfix_rest:tt $block_type:tt])+
		]
	)=>{
		eager_internal!{
			@check_expansion[
				$([$prefix_rest $postfix_rest $block_type])+
			]
		}
	};
	(	// When there is no more input and no block
		// output the result, reversing it to ensure correct order
		@check_expansion[
			[[$($result:tt)*][]]
		]
	)=>{
		reverse_tt!{ [$($result)*] }
	};
	(	// When there is no more input but a block,
		// the block must have already been checked,
		// therefore, begin promoting to prefix (brace type)
		@check_expansion[
			[[$($prefix:tt)*][$($postfix:tt)*]{$($body:tt)*}]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@promote[
				[[{} $($prefix)*][$($postfix)*]{$($body)*}]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
		}
	};
	(	// When there is no more input and but a block
		// the block must have already been checked,
		// so output everything (parenthesis type)
		@check_expansion[
			[[$($prefix:tt)*][$($postfix:tt)*]($($body:tt)*)]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@promote[
				[[() $($prefix)*][$($postfix)*]($($body)*)]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
		}
	};

// Promoting blocks
	(	// promote a checked block to prefix (brace type)
		// We dont reverse the order in the block when
		// it is promoted since the revert_tt! called later
		// wont touch it.
		@promote[
			[[{$($other:tt)*} $($prefix:tt)*][$($postfix:tt)*]{$next:tt $($body:tt)*}]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@promote[
				[[{$($other)* $next} $($prefix)*][$($postfix)*]{$($body)*}]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
		}
	};
	(	// promote a checked block to prefix (paren type)
		// We dont reverse the order in the block when
		// it is promoted since the revert_tt! called later
		// wont touch it.
		@promote[
			[[($($other:tt)*) $($prefix:tt)*][$($postfix:tt)*]($next:tt $($body:tt)*)]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@promote[
				[[($($other)* $next) $($prefix)*][$($postfix)*]($($body)*)]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
		}
	};
	(	// done promoting a checked block to prefix (brace type)
		@promote[
			[[$promoted:tt $($prefix:tt)*][$($postfix:tt)*]{}]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@check_expansion[
				[[$promoted $($prefix)*][]]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
			$($postfix)*
		}
	};
	(	// done promoting a checked block to prefix (paren type)
		@promote[
			[[$promoted:tt $($prefix:tt)*][$($postfix:tt)*]()]
			$([$prefix_rest:tt $postfix_rest:tt $block_type_rest:tt])*
		]
	)=>{
		eager_internal!{
			@check_expansion[
				[[$promoted $($prefix)*][]]
				$([$prefix_rest $postfix_rest $block_type_rest])*
			]
			$($postfix)*
		}
	};
}