Skip to content

Commit 977b380

Browse files
committed
cleanup and shiny new more-functional interface
1 parent a18a631 commit 977b380

File tree

1 file changed

+36
-55
lines changed

1 file changed

+36
-55
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,10 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
614614
// oh dear heaven... this is going to include the enum
615615
// names, as well... but that should be okay, as long as
616616
// 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,());
619617
// 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();
625621
let rewritten_pat = {
626622
let mut rename_fld =
627623
IdentRenamer{renames: &mut new_pending_renames};
@@ -668,10 +664,8 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
668664
// code duplicated from 'let', above. Perhaps this can be lifted
669665
// into a separate function:
670666
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,());
673667
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() {
675669
let new_name = fresh_name(ident);
676670
new_pending_renames.push((*ident,new_name));
677671
}
@@ -681,8 +675,6 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
681675
// ones have already been applied):
682676
rename_fld.fold_pat(expanded_pat)
683677
};
684-
/*
685-
*/
686678
ast::Arm {
687679
attrs: arm.attrs.iter().map(|x| fld.fold_attribute(*x)).collect(),
688680
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 {
695687

696688
// a visitor that extracts the pat_ident (binding) paths
697689
// from a given thingy and puts them in a mutable
698-
// array (passed in to the traversal).
690+
// array
699691
#[deriving(Clone)]
700692
struct NameFinderContext {
701693
ident_accumulator: Vec<ast::Ident> ,
@@ -735,13 +727,11 @@ impl Visitor<()> for NameFinderContext {
735727

736728
}
737729

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
745735
}
746736

747737
// 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> {
10461036

10471037
#[cfg(test)]
10481038
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};
10501041
use ast;
10511042
use ast::{Attribute_, AttrOuter, MetaWord};
10521043
use attr;
@@ -1076,22 +1067,22 @@ mod test {
10761067
match *expr {
10771068
ast::Expr{id:_,span:_,node:ast::ExprPath(ref p)} => {
10781069
self.path_accumulator.push(p.clone());
1079-
// not calling visit_path, should be fine.
1070+
// not calling visit_path, but it should be fine.
10801071
}
10811072
_ => visit::walk_expr(self,expr,())
10821073
}
10831074
}
10841075
}
10851076

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
10931082
}
10941083

1084+
1085+
10951086
// these following tests are quite fragile, in that they don't test what
10961087
// *kind* of failure occurs.
10971088

@@ -1183,6 +1174,14 @@ mod test {
11831174
expand_crate(&ps,cfg,vec!(),vec!(),crate_ast)
11841175
}
11851176

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+
11861185
//fn expand_and_resolve(crate_str: @str) -> ast::crate {
11871186
//let expanded_ast = expand_crate_str(crate_str);
11881187
// println!("expanded: {:?}\n",expanded_ast);
@@ -1315,15 +1314,8 @@ mod test {
13151314
(ref str,ref conns, bic) => (str.to_owned(), conns.clone(), bic)
13161315
};
13171316
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);
13271319

13281320
// must be one check clause for each binding:
13291321
assert_eq!(bindings.len(),bound_connections.len());
@@ -1392,10 +1384,7 @@ foo_module!()
13921384
".to_string();
13931385
let cr = expand_crate_str(crate_str);
13941386
// 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);
13991388
let cxbinds: Vec<&ast::Ident> =
14001389
bindings.iter().filter(|b| {
14011390
let ident = token::get_ident(**b);
@@ -1408,10 +1397,7 @@ foo_module!()
14081397
_ => fail!("expected just one binding for ext_cx")
14091398
};
14101399
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);
14151401

14161402
// the xx binding should bind all of the xx varrefs:
14171403
for (idx,v) in varrefs.iter().filter(|p| {
@@ -1437,10 +1423,8 @@ foo_module!()
14371423
fn pat_idents(){
14381424
let pat = string_to_pat(
14391425
"(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")));
14441428
}
14451429

14461430
// test the list of identifier patterns gathered by the visitor. Note that
@@ -1450,11 +1434,8 @@ foo_module!()
14501434
fn crate_idents(){
14511435
let the_crate = string_to_crate("fn main (a : int) -> int {|b| {
14521436
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")));
14581439
}
14591440

14601441

0 commit comments

Comments
 (0)