@@ -400,6 +400,28 @@ pub fn rewrite_macro_def(
400
400
Some ( result)
401
401
}
402
402
403
+ fn register_metavariable (
404
+ map : & mut HashMap < String , String > ,
405
+ result : & mut String ,
406
+ name : & str ,
407
+ dollar_count : usize ,
408
+ ) {
409
+ let mut new_name = String :: new ( ) ;
410
+ let mut old_name = String :: new ( ) ;
411
+
412
+ old_name. push ( '$' ) ;
413
+ for _ in 0 ..( dollar_count - 1 ) {
414
+ new_name. push ( '$' ) ;
415
+ old_name. push ( '$' ) ;
416
+ }
417
+ new_name. push ( 'z' ) ;
418
+ new_name. push_str ( & name) ;
419
+ old_name. push_str ( & name) ;
420
+
421
+ result. push_str ( & new_name) ;
422
+ map. insert ( old_name, new_name) ;
423
+ }
424
+
403
425
// Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
404
426
// aren't causing problems.
405
427
// This should also work for escaped `$` variables, where we leave earlier `$`s.
@@ -419,24 +441,11 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
419
441
result. push ( c) ;
420
442
} else if !c. is_alphanumeric ( ) && !cur_name. is_empty ( ) {
421
443
// Terminates a name following one or more dollars.
422
- let mut new_name = String :: new ( ) ;
423
- let mut old_name = String :: new ( ) ;
424
- old_name. push ( '$' ) ;
425
- for _ in 0 ..( dollar_count - 1 ) {
426
- new_name. push ( '$' ) ;
427
- old_name. push ( '$' ) ;
428
- }
429
- new_name. push ( 'z' ) ;
430
- new_name. push_str ( & cur_name) ;
431
- old_name. push_str ( & cur_name) ;
432
-
433
- result. push_str ( & new_name) ;
434
- substs. insert ( old_name, new_name) ;
444
+ register_metavariable ( & mut substs, & mut result, & cur_name, dollar_count) ;
435
445
436
446
result. push ( c) ;
437
-
438
447
dollar_count = 0 ;
439
- cur_name = String :: new ( ) ;
448
+ cur_name. clear ( ) ;
440
449
} else if c == '(' && cur_name. is_empty ( ) {
441
450
// FIXME: Support macro def with repeat.
442
451
return None ;
@@ -445,21 +454,8 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
445
454
}
446
455
}
447
456
448
- // FIXME: duplicate code
449
457
if !cur_name. is_empty ( ) {
450
- let mut new_name = String :: new ( ) ;
451
- let mut old_name = String :: new ( ) ;
452
- old_name. push ( '$' ) ;
453
- for _ in 0 ..( dollar_count - 1 ) {
454
- new_name. push ( '$' ) ;
455
- old_name. push ( '$' ) ;
456
- }
457
- new_name. push ( 'z' ) ;
458
- new_name. push_str ( & cur_name) ;
459
- old_name. push_str ( & cur_name) ;
460
-
461
- result. push_str ( & new_name) ;
462
- substs. insert ( old_name, new_name) ;
458
+ register_metavariable ( & mut substs, & mut result, & cur_name, dollar_count) ;
463
459
}
464
460
465
461
debug ! ( "replace_names `{}` {:?}" , result, substs) ;
0 commit comments