Skip to content

Commit 477714f

Browse files
committed
Implement "replace" function.
1 parent 485e489 commit 477714f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/comp/syntax/ext/qquote.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import driver::session;
22

33
import option::{none, some};
44

5-
import syntax::ast::{crate, expr_, expr_mac, mac_invoc, mac_qq};
5+
import syntax::ast::{crate, expr_, expr_mac, mac_invoc, mac_qq, mac_var};
66
import syntax::fold::*;
77
import syntax::ext::base::*;
88
import syntax::ext::build::*;
@@ -29,6 +29,28 @@ fn expand_qquote(cx: ext_ctxt, sp: span, _e: @ast::expr) -> ast::expr_ {
2929
ret call.node;
3030
}
3131

32+
fn replace(e: @ast::expr, repls: [@ast::expr]) -> @ast::expr {
33+
let aft = default_ast_fold();
34+
let f_pre = {fold_expr: bind replace_expr(repls, _, _, _,
35+
aft.fold_expr)
36+
with *aft};
37+
let f = make_fold(f_pre);
38+
ret f.fold_expr(e);
39+
}
40+
41+
fn replace_expr(repls: [@ast::expr],
42+
e: ast::expr_, s: span, fld: ast_fold,
43+
orig: fn@(ast::expr_, span, ast_fold)->(ast::expr_, span))
44+
-> (ast::expr_, span)
45+
{
46+
// note: nested enum matching will be really nice here so I can jusy say
47+
// expr_mac(mac_var(i))
48+
alt e {
49+
expr_mac({node: mac_var(i), _}) {let r = repls[i]; (r.node, r.span)}
50+
_ {orig(e,s,fld)}
51+
}
52+
}
53+
3254
// Local Variables:
3355
// mode: rust
3456
// fill-column: 78;

0 commit comments

Comments
 (0)