| 1 | /// A macro that generates code for working with variables based on input data. |
| 2 | /// |
| 3 | /// (usually for internal use only) |
| 4 | /// |
| 5 | /// ## Support |
| 6 | /// |
| 7 | /// ### `.clone()` operation |
| 8 | /// ```code |
| 9 | /// // (a => mut b: String) will be converted to (let mut b: String = a.clone();) |
| 10 | /// // (b => mut b) |
| 11 | /// // (a => b: usize) |
| 12 | /// // (a => b) |
| 13 | /// // (mut a: String) |
| 14 | /// // (mut a) |
| 15 | /// // (a: String) |
| 16 | /// // (a) |
| 17 | /// ``` |
| 18 | /// |
| 19 | /// ### `*copy` operation |
| 20 | /// ```code |
| 21 | /// // (*a => mut b: &str) will be converted to (let mut b: &str = *a;) |
| 22 | /// // (*a => mut b) |
| 23 | /// // (*a => b: &str) |
| 24 | /// // (*a => b) |
| 25 | /// // (mut *a: &str) |
| 26 | /// // (mut *a) |
| 27 | /// // (*a: &str) |
| 28 | /// // (*a) |
| 29 | /// ``` |
| 30 | /// |
| 31 | /// ### `let ref mut a = b;`(`ref`) operation |
| 32 | /// ```code |
| 33 | /// // (ref mut a: String) will be converted to (let ref mut a: String = a;) |
| 34 | /// // (ref mut a) |
| 35 | /// // (ref a: String) |
| 36 | /// // (ref a) |
| 37 | /// // (ref a => mut b: String) |
| 38 | /// // (ref a => mut b) |
| 39 | /// // (ref a => b: String) |
| 40 | /// // (ref a => b) |
| 41 | /// ``` |
| 42 | /// |
| 43 | /// ### `(1+1)` (expr) operation |
| 44 | /// ```code |
| 45 | /// // (@(1+1) => mut b: usize) will be converted to (let mut b: usize = (1+1);) |
| 46 | /// // (@(1+1) => mut b) |
| 47 | /// // (@(1+1) => b: usize) |
| 48 | /// // (@(1+1) => b) |
| 49 | /// ``` |
| 50 | /// |
| 51 | /// ### `let a = b;` (move) operation |
| 52 | /// ```code |
| 53 | /// // (move a => mut b: String) will be converted to (let mut b: String = a;) |
| 54 | /// // (move a => mut b) |
| 55 | /// // (move a => mut b) |
| 56 | /// // (move a => b: String) |
| 57 | /// // (move a => b) |
| 58 | /// // (move mut a: String) |
| 59 | /// // (move mut a) |
| 60 | /// // (move a: String) |
| 61 | /// // (move a) |
| 62 | /// ``` |
| 63 | /// |
| 64 | /// ### `{println!("test");}` (run) operation |
| 65 | /// ```code |
| 66 | /// // { panic!("12"); } |
| 67 | /// ``` |
| 68 | #[macro_export] |
| 69 | macro_rules! enclose_var { |
| 70 | [ $(,)? ] => {}; |
| 71 | [ |
| 72 | // ref operation |
| 73 | // (ref a => mut b: String) |
| 74 | // (ref a => mut b) |
| 75 | ref $a: expr => mut $b: ident $(: $ty:ty)? |
| 76 | |
| 77 | $(, $($tt:tt)* )? |
| 78 | ] => { |
| 79 | let ref mut $b $(: $ty)? = $a; |
| 80 | |
| 81 | $( |
| 82 | $crate::enclose_var! { |
| 83 | $($tt)* |
| 84 | } |
| 85 | )? |
| 86 | }; |
| 87 | [ |
| 88 | // ref operation |
| 89 | // (ref a => b: String) |
| 90 | // (ref a => b) |
| 91 | ref $a: expr => $b: ident $(: $ty:ty)? |
| 92 | |
| 93 | $(, $($tt:tt)* )? |
| 94 | ] => { |
| 95 | let ref $b $(: $ty)? = $a; |
| 96 | |
| 97 | $( |
| 98 | $crate::enclose_var! { |
| 99 | $($tt)* |
| 100 | } |
| 101 | )? |
| 102 | }; |
| 103 | // |
| 104 | |
| 105 | [ |
| 106 | // move operation |
| 107 | // (move a => mut b: String) |
| 108 | // (move a => mut b) |
| 109 | move $a: expr => mut $b: ident $(: $ty:ty)? |
| 110 | |
| 111 | $(, $($tt:tt)* )? |
| 112 | ] => { |
| 113 | let mut $b $(: $ty)? = $a; |
| 114 | |
| 115 | $( |
| 116 | $crate::enclose_var! { |
| 117 | $($tt)* |
| 118 | } |
| 119 | )? |
| 120 | }; |
| 121 | |
| 122 | [ |
| 123 | // move operation |
| 124 | // (move a => b: String) |
| 125 | // (move a => b) |
| 126 | move $a: expr => $b: ident $(: $ty:ty)? |
| 127 | |
| 128 | $(, $($tt:tt)* )? |
| 129 | ] => { |
| 130 | let $b $(: $ty)? = $a; |
| 131 | |
| 132 | $( |
| 133 | $crate::enclose_var! { |
| 134 | $($tt)* |
| 135 | } |
| 136 | )? |
| 137 | }; |
| 138 | // |
| 139 | |
| 140 | [ |
| 141 | // copy operation |
| 142 | // (*a => mut b: &str) |
| 143 | // (*a => mut b) |
| 144 | *$a: expr => mut $b: ident $(: $ty:ty)? |
| 145 | |
| 146 | $(, $($tt:tt)* )? |
| 147 | ] => { |
| 148 | let mut $b $(: $ty)? = *$a; |
| 149 | |
| 150 | $( |
| 151 | $crate::enclose_var! { |
| 152 | $($tt)* |
| 153 | } |
| 154 | )? |
| 155 | }; |
| 156 | |
| 157 | [ |
| 158 | // copy operation |
| 159 | // (*a => b: String) |
| 160 | // (*a => b) |
| 161 | *$a: expr => $b: ident $(: $ty:ty)? |
| 162 | |
| 163 | $(, $($tt:tt)* )? |
| 164 | ] => { |
| 165 | let $b $(: $ty)? = *$a; |
| 166 | |
| 167 | $( |
| 168 | $crate::enclose_var! { |
| 169 | $($tt)* |
| 170 | } |
| 171 | )? |
| 172 | }; |
| 173 | // |
| 174 | |
| 175 | [ |
| 176 | // expr |
| 177 | // (@(1+1) => mut b: usize) |
| 178 | // (@(1+1) => mut b) |
| 179 | @$a: expr => mut $b: ident $(: $ty:ty)? |
| 180 | |
| 181 | $(, $($tt:tt)* )? |
| 182 | ] => { |
| 183 | let mut $b $(: $ty)? = $a; |
| 184 | |
| 185 | $( |
| 186 | $crate::enclose_var! { |
| 187 | $($tt)* |
| 188 | } |
| 189 | )? |
| 190 | }; |
| 191 | |
| 192 | [ |
| 193 | // expr |
| 194 | // (@(1+1) => b: usize) |
| 195 | // (@(1+1) => b) |
| 196 | @$a: expr => $b: ident $(: $ty:ty)? |
| 197 | |
| 198 | $(, $($tt:tt)* )? |
| 199 | ] => { |
| 200 | let $b $(: $ty)? = $a; |
| 201 | |
| 202 | $( |
| 203 | $crate::enclose_var! { |
| 204 | $($tt)* |
| 205 | } |
| 206 | )? |
| 207 | }; |
| 208 | // |
| 209 | |
| 210 | [ |
| 211 | // ref |
| 212 | // (ref mut a: String) |
| 213 | // (ref mut a) |
| 214 | ref mut $a: ident $(: $ty:ty)? |
| 215 | |
| 216 | $(, $($tt:tt)*)? |
| 217 | ] => { |
| 218 | let ref mut $a $(: $ty)? = $a; |
| 219 | |
| 220 | $( |
| 221 | $crate::enclose_var! { |
| 222 | $($tt)* |
| 223 | } |
| 224 | )? |
| 225 | }; |
| 226 | |
| 227 | [ |
| 228 | // ref |
| 229 | // (ref a: String) |
| 230 | // (ref a) |
| 231 | ref $a: ident $(: $ty:ty)? |
| 232 | |
| 233 | $(, $($tt:tt)*)? |
| 234 | ] => { |
| 235 | let ref $a $(: $ty)? = $a; |
| 236 | |
| 237 | $( |
| 238 | $crate::enclose_var! { |
| 239 | $($tt)* |
| 240 | } |
| 241 | )? |
| 242 | }; |
| 243 | // end |
| 244 | |
| 245 | [ |
| 246 | // move |
| 247 | // (move mut a: String) |
| 248 | // (move mut a) |
| 249 | move mut $a: ident $(: $ty:ty)? |
| 250 | |
| 251 | $(, $($tt:tt)*)? |
| 252 | ] => { |
| 253 | let mut $a $(: $ty)? = $a; |
| 254 | |
| 255 | $( |
| 256 | $crate::enclose_var! { |
| 257 | $($tt)* |
| 258 | } |
| 259 | )? |
| 260 | }; |
| 261 | [ |
| 262 | // move |
| 263 | // (move a: String) |
| 264 | // (move a) |
| 265 | move $a: ident $(: $ty:ty)? |
| 266 | |
| 267 | $(, $($tt:tt)*)? |
| 268 | ] => { |
| 269 | let $a $(: $ty)? = $a; |
| 270 | |
| 271 | $( |
| 272 | $crate::enclose_var! { |
| 273 | $($tt)* |
| 274 | } |
| 275 | )? |
| 276 | }; |
| 277 | // end |
| 278 | |
| 279 | [ |
| 280 | // clone |
| 281 | // (a => mut b: usize) |
| 282 | // (a => mut b) |
| 283 | $a: expr => mut $b: ident $(: $ty:ty)? |
| 284 | |
| 285 | $(, $($tt:tt)* )? |
| 286 | ] => { |
| 287 | let mut $b $(: $ty)? = $a.clone(); |
| 288 | |
| 289 | $( |
| 290 | $crate::enclose_var! { |
| 291 | $($tt)* |
| 292 | } |
| 293 | )? |
| 294 | }; |
| 295 | [ |
| 296 | // clone |
| 297 | // (a => b: usize) |
| 298 | // (b => b) |
| 299 | $a: expr => $b: ident $(: $ty:ty)? |
| 300 | |
| 301 | $(, $($tt:tt)* )? |
| 302 | ] => { |
| 303 | let $b $(: $ty)? = $a.clone(); |
| 304 | |
| 305 | $( |
| 306 | $crate::enclose_var! { |
| 307 | $($tt)* |
| 308 | } |
| 309 | )? |
| 310 | }; |
| 311 | // end |
| 312 | |
| 313 | [ |
| 314 | // deref |
| 315 | // (mut *a: String) |
| 316 | // (mut *a) |
| 317 | mut *$a: ident $(: $ty:ty)? |
| 318 | |
| 319 | $(, $($tt:tt)*)? |
| 320 | ] => { |
| 321 | let mut $a $(: $ty)? = *$a; |
| 322 | |
| 323 | $( |
| 324 | $crate::enclose_var! { |
| 325 | $($tt)* |
| 326 | } |
| 327 | )? |
| 328 | }; |
| 329 | [ |
| 330 | // deref |
| 331 | // (*a: String) |
| 332 | // (*a) |
| 333 | *$a: ident $(: $ty:ty)? |
| 334 | |
| 335 | $(, $($tt:tt)*)? |
| 336 | ] => { |
| 337 | let $a $(: $ty)? = *$a; |
| 338 | |
| 339 | $( |
| 340 | $crate::enclose_var! { |
| 341 | $($tt)* |
| 342 | } |
| 343 | )? |
| 344 | }; |
| 345 | // end deref |
| 346 | |
| 347 | [ |
| 348 | // clone |
| 349 | // (mut a: String) |
| 350 | // (mut a) |
| 351 | mut $a: ident $(: $ty:ty)? |
| 352 | |
| 353 | $(, $($tt:tt)*)? |
| 354 | ] => { |
| 355 | let mut $a $(: $ty)? = $a.clone(); |
| 356 | |
| 357 | $( |
| 358 | $crate::enclose_var! { |
| 359 | $($tt)* |
| 360 | } |
| 361 | )? |
| 362 | }; |
| 363 | [ |
| 364 | // clone |
| 365 | // (a: String) |
| 366 | // (a) |
| 367 | $a: ident $(: $ty:ty)? |
| 368 | |
| 369 | $(, $($tt:tt)*)? |
| 370 | ] => { |
| 371 | let $a $(: $ty)? = $a.clone(); |
| 372 | |
| 373 | $( |
| 374 | $crate::enclose_var! { |
| 375 | $($tt)* |
| 376 | } |
| 377 | )? |
| 378 | }; |
| 379 | // end clone |
| 380 | |
| 381 | [ |
| 382 | // run_block |
| 383 | // { panic!("12"); } |
| 384 | {$($b:tt)*} |
| 385 | |
| 386 | $(, $($tt:tt)*)? |
| 387 | ] => { |
| 388 | let _hidden = { |
| 389 | $($b)* |
| 390 | }; |
| 391 | |
| 392 | $( |
| 393 | $crate::enclose_var! { |
| 394 | $($tt)* |
| 395 | } |
| 396 | )? |
| 397 | }; |
| 398 | // end block |
| 399 | |
| 400 | /*[ $($unk:tt)+ ] => { |
| 401 | compile_error!("Undefined entry or unsupported arguments. Please check your input again.") |
| 402 | };*/ |
| 403 | } |