Skip to content

Commit c082b8e

Browse files
committed
WIP: adding context to macros
1 parent 3b88946 commit c082b8e

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pub enum BlockCheckMode {
461461
UnsafeBlock,
462462
}
463463

464-
#[deriving(Eq, Encodable, Decodable,IterBytes)]
464+
#[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]
465465
pub struct Expr {
466466
id: NodeId,
467467
node: Expr_,

src/libsyntax/ext/expand.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ fn expand_non_macro_stmt (exts: SyntaxEnv,
603603
}
604604

605605
// a visitor that extracts the pat_ident paths
606-
// from a given pattern and puts them in a mutable
606+
// from a given thingy and puts them in a mutable
607607
// array (passed in to the traversal)
608608
#[deriving(Clone)]
609609
struct NewNameFinderContext {
@@ -748,15 +748,14 @@ impl Visitor<()> for NewNameFinderContext {
748748
}
749749
}
750750

751-
// a visitor that extracts the path exprs from
752-
// a crate/expression/whatever and puts them in a mutable
751+
// a visitor that extracts the paths
752+
// from a given thingy and puts them in a mutable
753753
// array (passed in to the traversal)
754754
#[deriving(Clone)]
755755
struct NewPathExprFinderContext {
756756
path_accumulator: @mut ~[ast::Path],
757757
}
758758

759-
760759
// XXX : YIKES a lot of boilerplate again....
761760
impl Visitor<()> for NewPathExprFinderContext {
762761

@@ -875,7 +874,7 @@ impl Visitor<()> for NewPathExprFinderContext {
875874
}
876875

877876
// return a visitor that extracts the pat_ident paths
878-
// from a given pattern and puts them in a mutable
877+
// from a given thingy and puts them in a mutable
879878
// array (passed in to the traversal)
880879
pub fn new_name_finder(idents: @mut ~[ast::Ident]) -> @mut Visitor<()> {
881880
let context = @mut NewNameFinderContext {
@@ -913,6 +912,7 @@ pub fn renames_to_fold(renames : @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold {
913912
make_fold(f_pre)
914913
}
915914

915+
// expand a block. pushes a new exts_frame, then calls expand_block_elts
916916
pub fn expand_block(extsbox: @mut SyntaxEnv,
917917
_cx: @ExtCtxt,
918918
blk: &Block,
@@ -924,7 +924,7 @@ pub fn expand_block(extsbox: @mut SyntaxEnv,
924924
expand_block_elts(*extsbox, blk, fld))
925925
}
926926

927-
927+
// expand the elements of a block.
928928
pub fn expand_block_elts(exts: SyntaxEnv, b: &Block, fld: @ast_fold) -> Block {
929929
let block_info = get_block_info(exts);
930930
let pending_renames = block_info.pending_renames;
@@ -949,6 +949,7 @@ pub fn expand_block_elts(exts: SyntaxEnv, b: &Block, fld: @ast_fold) -> Block {
949949
}
950950

951951
// rename_fold should never return "None".
952+
// (basically, just .get() with a better message...)
952953
fn mustbesome<T>(val : Option<T>) -> T {
953954
match val {
954955
Some(v) => v,
@@ -971,8 +972,8 @@ pub fn new_span(cx: @ExtCtxt, sp: Span) -> Span {
971972
}
972973

973974
// FIXME (#2247): this is a moderately bad kludge to inject some macros into
974-
// the default compilation environment. It would be much nicer to use
975-
// a mechanism like syntax_quote to ensure hygiene.
975+
// the default compilation environment in that it injects strings, rather than
976+
// syntax elements.
976977

977978
pub fn std_macros() -> @str {
978979
return
@@ -1453,6 +1454,10 @@ pub fn fun_to_ctxt_folder<T : 'static + CtxtFn>(cf: @T) -> @AstFoldFns {
14531454
|ast::Ident{name, ctxt}, _| {
14541455
ast::Ident{name:name,ctxt:cf.f(ctxt)}
14551456
};
1457+
// we've also got to pick up macro invocations; they can
1458+
// appear as exprs, stmts, items, and types. urg, it's going
1459+
// to be easier just to add a fold_mac, I think.
1460+
//let fold_ex : @
14561461
@AstFoldFns{
14571462
fold_ident : fi,
14581463
// check that it works, then add the fold_expr clause....
@@ -1631,14 +1636,15 @@ mod test {
16311636

16321637
// try a double-rename, with pending_renames.
16331638
let a3_name = gensym("a3");
1639+
// a context that renames from ("a",empty) to "a2" :
16341640
let ctxt2 = new_rename(ast::Ident::new(a_name),a2_name,EMPTY_CTXT);
16351641
let pending_renames = @mut ~[(ast::Ident::new(a_name),a2_name),
16361642
(ast::Ident{name:a_name,ctxt:ctxt2},a3_name)];
16371643
let double_renamed = renames_to_fold(pending_renames).fold_crate(item_ast);
16381644
let varrefs = @mut ~[];
16391645
visit::walk_crate(&mut new_path_finder(varrefs), &double_renamed, ());
16401646
match varrefs {
1641-
@[Path{segments:[ref seg],_}] => assert_eq!(mtwt_resolve(seg.identifier),a2_name),
1647+
@[Path{segments:[ref seg],_}] => assert_eq!(mtwt_resolve(seg.identifier),a3_name),
16421648
_ => assert_eq!(0,1)
16431649
}
16441650
}

0 commit comments

Comments
 (0)