1#[macro_export]
2macro_rules! wtflip {
3 (@as_statement mut $ident:ident := $($expr:tt)+) => {
5 let mut $ident = wtflip!(@as_expr $($expr)+);
6 };
7
8 (@as_statement $ident:ident $(: $(@$($_:tt)* $declaration:tt)?)?= $($expr:tt)+) => {
10 $($($declaration)? let)? $ident = wtflip!(@as_expr $($expr)+);
11 };
12
13 (@as_statement @{ $($compat:tt)* }) => ($($compat)*);
15
16 (@as_expr @ $(:: $(@$($_:tt)* $prefixed:tt)?)? $ident:ident $(:: $path:ident)* $(($($args:tt)*))?) => (
18 $crate::args_splitter!(
19 [$($($prefixed)? ::)? $ident $(:: $path)*!]
20 [$crate::wtflip!]
21 []
22 []
23 [$($($args)*)?]
24 );
25 );
26
27 (@as_expr $($tokens:tt)*) => (wtflip!(@callback [] [] @as_expr $($tokens)*));
29
30 (@callback [$($cb:tt)*] [$($args:tt)*]
32 @as_expr $lit:literal $($tail:tt)*
33 ) => (wtflip!(@callback [$($cb)*] [$($args)* [$lit]] $($tail)*));
34
35 (@callback [$($cb:tt)*] [$($args:tt)*]
37 @as_expr @{ $($tokens:tt)* } $($tail:tt)*
38 ) => (wtflip!(@callback [$($cb)*] [$($args)* [{ $($tokens)* }]]));
39
40 (@callback [$($cb:tt)*] [$([$($arg:tt)*])*]) => ($($cb)*($($($arg)*),*));
42
43 (@statement_accumulator [$($buffer:tt)*] ; $($tail:tt)*) => {
45 wtflip!(@as_statement $($buffer)*);
46 wtflip!($($tail)*);
47 };
48
49 (@statement_accumulator [$($buffer:tt)*] $token:tt $($tail:tt)*) => (wtflip!(
51 @statement_accumulator [$($buffer)* $token] $($tail)*
52 ));
53
54 () => ();
56
57 ($($tokens:tt)+) => (wtflip!(
59 @statement_accumulator [] $($tokens)*
60 ));
61}
62
63#[macro_export]
64macro_rules! args_splitter {
65 (
66 [$($wrap:tt)*]
67 [$($processor:tt)*]
68 []
69 [$([ $($out:tt)* ])*]
70 []
71 ) => ($($processor)*(@callback [$($wrap)*] [] $(@as_expr $($out)*)*));
72 (
73 [$($wrap:tt)*]
74 [$($processor:tt)*]
75 [$($current:tt)*]
76 [$($out:tt)*]
77 [$(, $($rest:tt)*)?]
78 ) => ($crate::args_splitter!(
79 [$($wrap)*]
80 [$($processor)*]
81 []
82 [$($out)* [$($current)*]]
83 [$($($rest)*)?]
84 ));
85 (
86 [$($wrap:tt)*]
87 [$($processor:tt)*]
88 [$($current:tt)*]
89 $out:tt
90 [$no_comma:tt $($rest:tt)*]
91 ) => ($crate::args_splitter!(
92 [$($wrap)*]
93 [$($processor)*]
94 [$($current)* $no_comma]
95 $out
96 [$($rest)*]
97 ));
98}