Skip to content

Commit 1ecc1e5

Browse files
committed
quote_* macros no longer need to be capturing
This is actually almost a problem, because those were my poster-child macros for "here's how to implement a capturing macro." Following this change, there will be no macros that use capturing; this will probably make life unpleasant for the first person that wants to implement a capturing macro. I should probably create a dummy_capturing macro, just to show how it works.
1 parent 4664d33 commit 1ecc1e5

File tree

3 files changed

+19
-91
lines changed

3 files changed

+19
-91
lines changed

src/libsyntax/ext/base.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,18 @@ pub fn syntax_expander_table() -> SyntaxEnv {
185185

186186
// Quasi-quoting expanders
187187
syntax_expanders.insert(intern(&"quote_tokens"),
188-
@SE(NormalTT(ext::quote::expand_quote_tokens, None)));
188+
builtin_normal_tt_no_ctxt(
189+
ext::quote::expand_quote_tokens));
189190
syntax_expanders.insert(intern(&"quote_expr"),
190-
@SE(NormalTT(ext::quote::expand_quote_expr, None)));
191+
builtin_normal_tt_no_ctxt(ext::quote::expand_quote_expr));
191192
syntax_expanders.insert(intern(&"quote_ty"),
192-
@SE(NormalTT(ext::quote::expand_quote_ty, None)));
193+
builtin_normal_tt_no_ctxt(ext::quote::expand_quote_ty));
193194
syntax_expanders.insert(intern(&"quote_item"),
194-
@SE(NormalTT(ext::quote::expand_quote_item, None)));
195+
builtin_normal_tt_no_ctxt(ext::quote::expand_quote_item));
195196
syntax_expanders.insert(intern(&"quote_pat"),
196-
@SE(NormalTT(ext::quote::expand_quote_pat, None)));
197+
builtin_normal_tt_no_ctxt(ext::quote::expand_quote_pat));
197198
syntax_expanders.insert(intern(&"quote_stmt"),
198-
@SE(NormalTT(ext::quote::expand_quote_stmt, None)));
199+
builtin_normal_tt_no_ctxt(ext::quote::expand_quote_stmt));
199200

200201
syntax_expanders.insert(intern(&"line"),
201202
builtin_normal_tt_no_ctxt(

src/libsyntax/ext/expand.rs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,58 +1856,6 @@ mod test {
18561856
}
18571857
}
18581858

1859-
#[test] fn quote_expr_test() {
1860-
quote_ext_cx_test(@"fn main(){let ext_cx = 13; quote_expr!(dontcare);}");
1861-
}
1862-
#[test] fn quote_item_test() {
1863-
quote_ext_cx_test(@"fn main(){let ext_cx = 13; quote_item!(dontcare);}");
1864-
}
1865-
#[test] fn quote_pat_test() {
1866-
quote_ext_cx_test(@"fn main(){let ext_cx = 13; quote_pat!(dontcare);}");
1867-
}
1868-
#[test] fn quote_ty_test() {
1869-
quote_ext_cx_test(@"fn main(){let ext_cx = 13; quote_ty!(dontcare);}");
1870-
}
1871-
#[test] fn quote_tokens_test() {
1872-
quote_ext_cx_test(@"fn main(){let ext_cx = 13; quote_tokens!(dontcare);}");
1873-
}
1874-
1875-
fn quote_ext_cx_test(crate_str : @str) {
1876-
let crate = expand_crate_str(crate_str);
1877-
// find the ext_cx binding
1878-
let bindings = @mut ~[];
1879-
visit::walk_crate(&mut new_name_finder(bindings), crate, ());
1880-
let cxbinds : ~[&ast::Ident] =
1881-
bindings.iter().filter(|b|{@"ext_cx" == (ident_to_str(*b))}).collect();
1882-
let cxbind = match cxbinds {
1883-
[b] => b,
1884-
_ => fail!("expected just one binding for ext_cx")
1885-
};
1886-
let resolved_binding = mtwt_resolve(*cxbind);
1887-
// find all the ext_cx varrefs:
1888-
let varrefs = @mut ~[];
1889-
visit::walk_crate(&mut new_path_finder(varrefs), crate, ());
1890-
// the ext_cx binding should bind all of the ext_cx varrefs:
1891-
for (idx,v) in varrefs.iter().filter(|p|{ p.segments.len() == 1
1892-
&& (@"ext_cx" == (ident_to_str(&p.segments[0].identifier)))
1893-
}).enumerate() {
1894-
if (mtwt_resolve(v.segments[0].identifier) != resolved_binding) {
1895-
std::io::println("uh oh, ext_cx binding didn't match ext_cx varref:");
1896-
std::io::println(fmt!("this is varref # %?",idx));
1897-
std::io::println(fmt!("binding: %?",cxbind));
1898-
std::io::println(fmt!("resolves to: %?",resolved_binding));
1899-
std::io::println(fmt!("varref: %?",v.segments[0]));
1900-
std::io::println(fmt!("resolves to: %?",mtwt_resolve(v.segments[0].identifier)));
1901-
let table = get_sctable();
1902-
std::io::println("SC table:");
1903-
for (idx,val) in table.table.iter().enumerate() {
1904-
std::io::println(fmt!("%4u : %?",idx,val));
1905-
}
1906-
}
1907-
assert_eq!(mtwt_resolve(v.segments[0].identifier),resolved_binding);
1908-
};
1909-
}
1910-
19111859
#[test] fn fmt_in_macro_used_inside_module_macro() {
19121860
let crate_str = @"macro_rules! fmt_wrap(($b:expr)=>(fmt!(\"left: %?\", $b)))
19131861
macro_rules! foo_module (() => (mod generated { fn a() { let xx = 147; fmt_wrap!(xx);}}))

src/libsyntax/ext/quote.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use ast;
1212
use codemap::{BytePos, Pos, Span};
1313
use ext::base::ExtCtxt;
1414
use ext::base;
15-
use ext::expand;
1615
use ext::build::AstBuilder;
1716
use parse::token::*;
1817
use parse::token;
@@ -292,73 +291,53 @@ pub mod rt {
292291
293292
pub fn expand_quote_tokens(cx: @ExtCtxt,
294293
sp: Span,
295-
tts: &[ast::token_tree],
296-
ctxt: ast::SyntaxContext) -> base::MacResult {
294+
tts: &[ast::token_tree]) -> base::MacResult {
297295
let (cx_expr, expr) = expand_tts(cx, sp, tts);
298296
let expanded = expand_wrapper(cx, sp, cx_expr, expr);
299-
// repaint the expanded code so it's as though it was the original text.
300-
let repainted = expand::replace_ctxts(expanded,ctxt);
301-
base::MRExpr(repainted)
297+
base::MRExpr(expanded)
302298
}
303299
304300
pub fn expand_quote_expr(cx: @ExtCtxt,
305301
sp: Span,
306-
tts: &[ast::token_tree],
307-
ctxt: ast::SyntaxContext) -> base::MacResult {
302+
tts: &[ast::token_tree]) -> base::MacResult {
308303
let expanded = expand_parse_call(cx, sp, "parse_expr", ~[], tts);
309-
// repaint the expanded code so it's as though it was the original text.
310-
let repainted = expand::replace_ctxts(expanded,ctxt);
311-
base::MRExpr(repainted)
304+
base::MRExpr(expanded)
312305
}
313306

314-
// these probably need to be capturing, too...
315-
316307
pub fn expand_quote_item(cx: @ExtCtxt,
317308
sp: Span,
318-
tts: &[ast::token_tree],
319-
ctxt: ast::SyntaxContext) -> base::MacResult {
309+
tts: &[ast::token_tree]) -> base::MacResult {
320310
let e_attrs = cx.expr_vec_uniq(sp, ~[]);
321311
let expanded = expand_parse_call(cx, sp, "parse_item",
322312
~[e_attrs], tts);
323-
// repaint the expanded code so it's as though it was the original text.
324-
let repainted = expand::replace_ctxts(expanded,ctxt);
325-
base::MRExpr(repainted)
313+
base::MRExpr(expanded)
326314
}
327315

328316
pub fn expand_quote_pat(cx: @ExtCtxt,
329317
sp: Span,
330-
tts: &[ast::token_tree],
331-
ctxt: ast::SyntaxContext) -> base::MacResult {
318+
tts: &[ast::token_tree]) -> base::MacResult {
332319
let e_refutable = cx.expr_lit(sp, ast::lit_bool(true));
333320
let expanded = expand_parse_call(cx, sp, "parse_pat",
334321
~[e_refutable], tts);
335-
// repaint the expanded code so it's as though it was the original text.
336-
let repainted = expand::replace_ctxts(expanded,ctxt);
337-
base::MRExpr(repainted)
322+
base::MRExpr(expanded)
338323
}
339324

340325
pub fn expand_quote_ty(cx: @ExtCtxt,
341326
sp: Span,
342-
tts: &[ast::token_tree],
343-
ctxt: ast::SyntaxContext) -> base::MacResult {
327+
tts: &[ast::token_tree]) -> base::MacResult {
344328
let e_param_colons = cx.expr_lit(sp, ast::lit_bool(false));
345329
let expanded = expand_parse_call(cx, sp, "parse_ty",
346330
~[e_param_colons], tts);
347-
// repaint the expanded code so it's as though it was the original text.
348-
let repainted = expand::replace_ctxts(expanded,ctxt);
349-
base::MRExpr(repainted)
331+
base::MRExpr(expanded)
350332
}
351333

352334
pub fn expand_quote_stmt(cx: @ExtCtxt,
353335
sp: Span,
354-
tts: &[ast::token_tree],
355-
ctxt: ast::SyntaxContext) -> base::MacResult {
336+
tts: &[ast::token_tree]) -> base::MacResult {
356337
let e_attrs = cx.expr_vec_uniq(sp, ~[]);
357338
let expanded = expand_parse_call(cx, sp, "parse_stmt",
358339
~[e_attrs], tts);
359-
// repaint the expanded code so it's as though it was the original text.
360-
let repainted = expand::replace_ctxts(expanded,ctxt);
361-
base::MRExpr(repainted)
340+
base::MRExpr(expanded)
362341
}
363342

364343
fn ids_ext(strs: ~[~str]) -> ~[ast::Ident] {

0 commit comments

Comments
 (0)