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
mod tokens; pub use tokens::Tokens; mod to_tokens; pub use to_tokens::{ToTokens, ByteStr}; #[macro_export] macro_rules! quote { ($($tt:tt)+) => { { #[allow(unused_imports)] use $crate::ToTokens; let mut _s = $crate::Tokens::new(); quote_each_token!(_s $($tt)*); _s } }; } #[macro_export] #[doc(hidden)] macro_rules! pounded_var_names { ($finish:ident ($($found:ident)*) # ( $($inner:tt)* ) $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) # [ $($inner:tt)* ] $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) # { $($inner:tt)* } $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) # $first:ident $($rest:tt)*) => { pounded_var_names!($finish ($($found)* $first) $($rest)*) }; ($finish:ident ($($found:ident)*) ( $($inner:tt)* ) $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) [ $($inner:tt)* ] $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) { $($inner:tt)* } $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*) }; ($finish:ident ($($found:ident)*) $ignore:tt $($rest:tt)*) => { pounded_var_names!($finish ($($found)*) $($rest)*) }; ($finish:ident ()) => { #"no variables in #(...)* repetition" }; ($finish:ident ($($found:ident)*)) => { $finish!(() $($found)*) }; } #[macro_export] #[doc(hidden)] macro_rules! nested_tuples_pat { (() $first:ident $($rest:ident)*) => { nested_tuples_pat!(($first) $($rest)*) }; (($pat:pat) $first:ident $($rest:ident)*) => { nested_tuples_pat!((($pat, $first)) $($rest)*) }; (($done:pat)) => { $done }; } #[macro_export] #[doc(hidden)] macro_rules! multi_zip_expr { (() $single:ident) => { $single }; (() $first:ident $($rest:ident)*) => { multi_zip_expr!(($first.into_iter()) $($rest)*) }; (($zips:expr) $first:ident $($rest:ident)*) => { multi_zip_expr!(($zips.zip($first)) $($rest)*) }; (($done:expr)) => { $done }; } #[macro_export] #[doc(hidden)] macro_rules! quote_each_token { ($tokens:ident) => {}; ($tokens:ident # ! $($rest:tt)*) => { $tokens.append("#"); $tokens.append("!"); quote_each_token!($tokens $($rest)*); }; ($tokens:ident # ( $($inner:tt)* ) * $($rest:tt)*) => { for pounded_var_names!(nested_tuples_pat () $($inner)*) in pounded_var_names!(multi_zip_expr () $($inner)*) { quote_each_token!($tokens $($inner)*); } quote_each_token!($tokens $($rest)*); }; ($tokens:ident # ( $($inner:tt)* ) $sep:tt * $($rest:tt)*) => { for (i, pounded_var_names!(nested_tuples_pat () $($inner)*)) in pounded_var_names!(multi_zip_expr () $($inner)*).into_iter().enumerate() { if i > 0 { $tokens.append(stringify!($sep)); } quote_each_token!($tokens $($inner)*); } quote_each_token!($tokens $($rest)*); }; ($tokens:ident # [ $($inner:tt)* ] $($rest:tt)*) => { $tokens.append("#"); $tokens.append("["); quote_each_token!($tokens $($inner)*); $tokens.append("]"); quote_each_token!($tokens $($rest)*); }; ($tokens:ident # $first:ident $($rest:tt)*) => { $first.to_tokens(&mut $tokens); quote_each_token!($tokens $($rest)*); }; ($tokens:ident ( $($first:tt)* ) $($rest:tt)*) => { $tokens.append("("); quote_each_token!($tokens $($first)*); $tokens.append(")"); quote_each_token!($tokens $($rest)*); }; ($tokens:ident [ $($first:tt)* ] $($rest:tt)*) => { $tokens.append("["); quote_each_token!($tokens $($first)*); $tokens.append("]"); quote_each_token!($tokens $($rest)*); }; ($tokens:ident { $($first:tt)* } $($rest:tt)*) => { $tokens.append("{"); quote_each_token!($tokens $($first)*); $tokens.append("}"); quote_each_token!($tokens $($rest)*); }; ($tokens:ident $first:tt $($rest:tt)*) => { $tokens.append(stringify!($first)); quote_each_token!($tokens $($rest)*); }; }