@@ -614,14 +614,10 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
614
614
// oh dear heaven... this is going to include the enum
615
615
// names, as well... but that should be okay, as long as
616
616
// the new names are gensyms for the old ones.
617
- let mut name_finder = new_name_finder ( Vec :: new ( ) ) ;
618
- name_finder. visit_pat ( & * expanded_pat, ( ) ) ;
619
617
// generate fresh names, push them to a new pending list
620
- let mut new_pending_renames = Vec :: new ( ) ;
621
- for ident in name_finder. ident_accumulator . iter ( ) {
622
- let new_name = fresh_name ( ident) ;
623
- new_pending_renames. push ( ( * ident, new_name) ) ;
624
- }
618
+ let idents = pattern_bindings ( expanded_pat) ;
619
+ let mut new_pending_renames =
620
+ idents. iter ( ) . map ( |ident| ( * ident, fresh_name ( ident) ) ) . collect ( ) ;
625
621
let rewritten_pat = {
626
622
let mut rename_fld =
627
623
IdentRenamer { renames : & mut new_pending_renames} ;
@@ -668,10 +664,8 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
668
664
// code duplicated from 'let', above. Perhaps this can be lifted
669
665
// into a separate function:
670
666
let expanded_pat = fld. fold_pat ( * first_pat) ;
671
- let mut name_finder = new_name_finder ( Vec :: new ( ) ) ;
672
- name_finder. visit_pat ( & * expanded_pat, ( ) ) ;
673
667
let mut new_pending_renames = Vec :: new ( ) ;
674
- for ident in name_finder . ident_accumulator . iter ( ) {
668
+ for ident in pattern_bindings ( expanded_pat ) . iter ( ) {
675
669
let new_name = fresh_name ( ident) ;
676
670
new_pending_renames. push ( ( * ident, new_name) ) ;
677
671
}
@@ -681,8 +675,6 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
681
675
// ones have already been applied):
682
676
rename_fld. fold_pat ( expanded_pat)
683
677
} ;
684
- /*
685
- */
686
678
ast:: Arm {
687
679
attrs : arm. attrs . iter ( ) . map ( |x| fld. fold_attribute ( * x) ) . collect ( ) ,
688
680
pats : arm. pats . iter ( ) . map ( |x| fld. fold_pat ( * x) ) . collect ( ) ,
@@ -695,7 +687,7 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
695
687
696
688
// a visitor that extracts the pat_ident (binding) paths
697
689
// from a given thingy and puts them in a mutable
698
- // array (passed in to the traversal).
690
+ // array
699
691
#[ deriving( Clone ) ]
700
692
struct NameFinderContext {
701
693
ident_accumulator : Vec < ast:: Ident > ,
@@ -735,13 +727,11 @@ impl Visitor<()> for NameFinderContext {
735
727
736
728
}
737
729
738
- // return a visitor that extracts the pat_ident paths
739
- // from a given thingy and puts them in a mutable
740
- // array (passed in to the traversal)
741
- fn new_name_finder ( idents : Vec < ast:: Ident > ) -> NameFinderContext {
742
- NameFinderContext {
743
- ident_accumulator : idents,
744
- }
730
+ // find the pat_ident paths in a pattern
731
+ fn pattern_bindings ( pat : & ast:: Pat ) -> Vec < ast:: Ident > {
732
+ let mut name_finder = NameFinderContext { ident_accumulator : Vec :: new ( ) } ;
733
+ name_finder. visit_pat ( pat, ( ) ) ;
734
+ name_finder. ident_accumulator
745
735
}
746
736
747
737
// expand a block. pushes a new exts_frame, then calls expand_block_elts
@@ -1046,7 +1036,8 @@ fn original_span(cx: &ExtCtxt) -> Gc<codemap::ExpnInfo> {
1046
1036
1047
1037
#[ cfg( test) ]
1048
1038
mod test {
1049
- use super :: { new_name_finder, expand_crate, contains_macro_escape} ;
1039
+ use super :: { pattern_bindings, expand_crate, contains_macro_escape} ;
1040
+ use super :: { NameFinderContext } ;
1050
1041
use ast;
1051
1042
use ast:: { Attribute_ , AttrOuter , MetaWord } ;
1052
1043
use attr;
@@ -1076,22 +1067,22 @@ mod test {
1076
1067
match * expr {
1077
1068
ast:: Expr { id : _, span : _, node : ast:: ExprPath ( ref p) } => {
1078
1069
self . path_accumulator . push ( p. clone ( ) ) ;
1079
- // not calling visit_path, should be fine.
1070
+ // not calling visit_path, but it should be fine.
1080
1071
}
1081
1072
_ => visit:: walk_expr ( self , expr, ( ) )
1082
1073
}
1083
1074
}
1084
1075
}
1085
1076
1086
- // return a visitor that extracts the paths
1087
- // from a given thingy and puts them in a mutable
1088
- // array (passed in to the traversal)
1089
- fn new_path_finder ( paths : Vec < ast:: Path > ) -> PathExprFinderContext {
1090
- PathExprFinderContext {
1091
- path_accumulator : paths
1092
- }
1077
+ // find the variable references in a crate
1078
+ fn crate_varrefs ( the_crate : & ast:: Crate ) -> Vec < ast:: Path > {
1079
+ let mut path_finder = PathExprFinderContext { path_accumulator : Vec :: new ( ) } ;
1080
+ visit:: walk_crate ( & mut path_finder, the_crate, ( ) ) ;
1081
+ path_finder. path_accumulator
1093
1082
}
1094
1083
1084
+
1085
+
1095
1086
// these following tests are quite fragile, in that they don't test what
1096
1087
// *kind* of failure occurs.
1097
1088
@@ -1183,6 +1174,14 @@ mod test {
1183
1174
expand_crate ( & ps, cfg, vec ! ( ) , vec ! ( ) , crate_ast)
1184
1175
}
1185
1176
1177
+ // find the pat_ident paths in a crate
1178
+ fn crate_bindings ( the_crate : & ast:: Crate ) -> Vec < ast:: Ident > {
1179
+ let mut name_finder = NameFinderContext { ident_accumulator : Vec :: new ( ) } ;
1180
+ visit:: walk_crate ( & mut name_finder, the_crate, ( ) ) ;
1181
+ name_finder. ident_accumulator
1182
+ }
1183
+
1184
+
1186
1185
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
1187
1186
//let expanded_ast = expand_crate_str(crate_str);
1188
1187
// println!("expanded: {:?}\n",expanded_ast);
@@ -1315,15 +1314,8 @@ mod test {
1315
1314
( ref str, ref conns, bic) => ( str. to_owned ( ) , conns. clone ( ) , bic)
1316
1315
} ;
1317
1316
let cr = expand_crate_str ( teststr. to_string ( ) ) ;
1318
- // find the bindings:
1319
- let mut name_finder = new_name_finder ( Vec :: new ( ) ) ;
1320
- visit:: walk_crate ( & mut name_finder, & cr, ( ) ) ;
1321
- let bindings = name_finder. ident_accumulator ;
1322
-
1323
- // find the varrefs:
1324
- let mut path_finder = new_path_finder ( Vec :: new ( ) ) ;
1325
- visit:: walk_crate ( & mut path_finder, & cr, ( ) ) ;
1326
- let varrefs = path_finder. path_accumulator ;
1317
+ let bindings = crate_bindings ( & cr) ;
1318
+ let varrefs = crate_varrefs ( & cr) ;
1327
1319
1328
1320
// must be one check clause for each binding:
1329
1321
assert_eq ! ( bindings. len( ) , bound_connections. len( ) ) ;
@@ -1392,10 +1384,7 @@ foo_module!()
1392
1384
" . to_string ( ) ;
1393
1385
let cr = expand_crate_str ( crate_str) ;
1394
1386
// find the xx binding
1395
- let mut name_finder = new_name_finder ( Vec :: new ( ) ) ;
1396
- visit:: walk_crate ( & mut name_finder, & cr, ( ) ) ;
1397
- let bindings = name_finder. ident_accumulator ;
1398
-
1387
+ let bindings = crate_bindings ( & cr) ;
1399
1388
let cxbinds: Vec < & ast:: Ident > =
1400
1389
bindings. iter ( ) . filter ( |b| {
1401
1390
let ident = token:: get_ident ( * * b) ;
@@ -1408,10 +1397,7 @@ foo_module!()
1408
1397
_ => fail ! ( "expected just one binding for ext_cx" )
1409
1398
} ;
1410
1399
let resolved_binding = mtwt:: resolve ( * cxbind) ;
1411
- // find all the xx varrefs:
1412
- let mut path_finder = new_path_finder ( Vec :: new ( ) ) ;
1413
- visit:: walk_crate ( & mut path_finder, & cr, ( ) ) ;
1414
- let varrefs = path_finder. path_accumulator ;
1400
+ let varrefs = crate_varrefs ( & cr) ;
1415
1401
1416
1402
// the xx binding should bind all of the xx varrefs:
1417
1403
for ( idx, v) in varrefs. iter ( ) . filter ( |p| {
@@ -1437,10 +1423,8 @@ foo_module!()
1437
1423
fn pat_idents ( ) {
1438
1424
let pat = string_to_pat (
1439
1425
"(a,Foo{x:c @ (b,9),y:Bar(4,d)})" . to_string ( ) ) ;
1440
- let mut pat_idents = new_name_finder ( Vec :: new ( ) ) ;
1441
- pat_idents. visit_pat ( pat, ( ) ) ;
1442
- assert_eq ! ( pat_idents. ident_accumulator,
1443
- strs_to_idents( vec!( "a" , "c" , "b" , "d" ) ) ) ;
1426
+ let idents = pattern_bindings ( pat) ;
1427
+ assert_eq ! ( idents, strs_to_idents( vec!( "a" , "c" , "b" , "d" ) ) ) ;
1444
1428
}
1445
1429
1446
1430
// test the list of identifier patterns gathered by the visitor. Note that
@@ -1450,11 +1434,8 @@ foo_module!()
1450
1434
fn crate_idents ( ) {
1451
1435
let the_crate = string_to_crate ( "fn main (a : int) -> int {|b| {
1452
1436
match 34 {None => 3, Some(i) | i => j, Foo{k:z,l:y} => \" banana\" }} }" . to_string ( ) ) ;
1453
- let mut idents = new_name_finder ( Vec :: new ( ) ) ;
1454
- //visit::walk_crate(&mut idents, &the_crate, ());
1455
- idents. visit_mod ( & the_crate. module , the_crate. span , ast:: CRATE_NODE_ID , ( ) ) ;
1456
- assert_eq ! ( idents. ident_accumulator,
1457
- strs_to_idents( vec!( "a" , "b" , "None" , "i" , "i" , "z" , "y" ) ) ) ;
1437
+ let idents = crate_bindings ( & the_crate) ;
1438
+ assert_eq ! ( idents, strs_to_idents( vec!( "a" , "b" , "None" , "i" , "i" , "z" , "y" ) ) ) ;
1458
1439
}
1459
1440
1460
1441
0 commit comments