Skip to content

Commit 5f6a115

Browse files
committed
Fix q-q so that non-expression q-q actually work when there is embed anti-q.
1 parent a2dde9a commit 5f6a115

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/comp/syntax/ext/qquote.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import codemap::span;
1818

1919
type aq_ctxt = @{lo: uint,
2020
mutable gather: [{lo: uint, hi: uint,
21-
e: @ast::expr, constr: str}]};
21+
e: @ast::expr,
22+
constr: str}]};
2223
enum fragment {
2324
from_expr(@ast::expr),
2425
from_ty(@ast::ty)
@@ -29,6 +30,7 @@ iface qq_helper {
2930
fn visit(aq_ctxt, vt<aq_ctxt>);
3031
fn extract_mac() -> option<ast::mac_>;
3132
fn mk_parse_fn(ext_ctxt,span) -> @ast::expr;
33+
fn get_fold_fn() -> str;
3234
}
3335
impl of qq_helper for @ast::expr {
3436
fn span() -> span {self.span}
@@ -42,6 +44,7 @@ impl of qq_helper for @ast::expr {
4244
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
4345
mk_path(cx, sp, ["syntax", "parse", "parser", "parse_expr"])
4446
}
47+
fn get_fold_fn() -> str {"fold_expr"}
4548
}
4649
impl of qq_helper for @ast::ty {
4750
fn span() -> span {self.span}
@@ -55,6 +58,7 @@ impl of qq_helper for @ast::ty {
5558
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
5659
mk_path(cx, sp, ["syntax", "ext", "qquote", "parse_ty"])
5760
}
61+
fn get_fold_fn() -> str {"fold_ty"}
5862
}
5963
impl of qq_helper for @ast::item {
6064
fn span() -> span {self.span}
@@ -63,6 +67,7 @@ impl of qq_helper for @ast::item {
6367
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
6468
mk_path(cx, sp, ["syntax", "ext", "qquote", "parse_item"])
6569
}
70+
fn get_fold_fn() -> str {"fold_item"}
6671
}
6772
impl of qq_helper for @ast::stmt {
6873
fn span() -> span {self.span}
@@ -71,6 +76,7 @@ impl of qq_helper for @ast::stmt {
7176
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
7277
mk_path(cx, sp, ["syntax", "ext", "qquote", "parse_stmt"])
7378
}
79+
fn get_fold_fn() -> str {"fold_stmt"}
7480
}
7581
impl of qq_helper for @ast::pat {
7682
fn span() -> span {self.span}
@@ -79,6 +85,7 @@ impl of qq_helper for @ast::pat {
7985
fn mk_parse_fn(cx: ext_ctxt, sp: span) -> @ast::expr {
8086
mk_path(cx, sp, ["syntax", "parse", "parser", "parse_pat"])
8187
}
88+
fn get_fold_fn() -> str {"fold_pat"}
8289
}
8390

8491
fn gather_anti_quotes<N: qq_helper>(lo: uint, node: N) -> aq_ctxt
@@ -244,23 +251,30 @@ fn expand_qquote<N: qq_helper>
244251
mk_vec_e(cx,sp, vec::map(qcx.gather) {|g|
245252
mk_call(cx,sp,
246253
["syntax", "ext", "qquote", g.constr],
247-
[g.e])
248-
})]);
254+
[g.e])}),
255+
mk_path(cx,sp,
256+
["syntax", "ext", "qquote",
257+
node.get_fold_fn()])]);
249258
}
250-
251259
ret rcall;
252260
}
253261

254-
fn replace(e: @ast::expr, repls: [fragment]) -> @ast::expr {
262+
fn replace<T>(node: T, repls: [fragment], ff: fn (ast_fold, T) -> T)
263+
-> T
264+
{
255265
let aft = default_ast_fold();
256266
let f_pre = {fold_expr: bind replace_expr(repls, _, _, _,
257267
aft.fold_expr),
258268
fold_ty: bind replace_ty(repls, _, _, _,
259269
aft.fold_ty)
260270
with *aft};
261-
let f = make_fold(f_pre);
262-
ret f.fold_expr(e);
271+
ret ff(make_fold(f_pre), node);
263272
}
273+
fn fold_expr(f: ast_fold, &&n: @ast::expr) -> @ast::expr {f.fold_expr(n)}
274+
fn fold_ty(f: ast_fold, &&n: @ast::ty) -> @ast::ty {f.fold_ty(n)}
275+
fn fold_item(f: ast_fold, &&n: @ast::item) -> @ast::item {f.fold_item(n)}
276+
fn fold_stmt(f: ast_fold, &&n: @ast::stmt) -> @ast::stmt {f.fold_stmt(n)}
277+
fn fold_pat(f: ast_fold, &&n: @ast::pat) -> @ast::pat {f.fold_pat(n)}
264278

265279
fn replace_expr(repls: [fragment],
266280
e: ast::expr_, s: span, fld: ast_fold,

src/test/run-pass/qquote.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ fn main() {
6262
let item = #ast(item){const x : int = 10;};
6363
check_pp(item, pprust::print_item, "const x: int = 10;");
6464

65-
//let item2: @ast::item = #ast(item){const x : int = $(abc);};
66-
//check_pp(item2, pprust::print_item, "const x: int = 23;");
65+
let item2: @ast::item = #ast(item){const x : int = $(abc);};
66+
check_pp(item2, pprust::print_item, "const x: int = 23;");
6767

6868
let stmt = #ast(stmt){let x = 20;};
6969
check_pp(*stmt, pprust::print_stmt, "let x = 20;");

0 commit comments

Comments
 (0)