Skip to content

Commit 1e96099

Browse files
committed
Add trace_macros!
1 parent 00ef541 commit 1e96099

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/libsyntax/ext/base.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ fn syntax_expander_table() -> hashmap<~str, syntax_extension> {
111111
builtin(ext::source_util::expand_mod));
112112
syntax_expanders.insert(~"proto",
113113
builtin_item_tt(ext::pipes::expand_proto));
114+
syntax_expanders.insert(
115+
~"trace_macros",
116+
builtin_expr_tt(ext::trace_macros::expand_trace_macros));
114117
return syntax_expanders;
115118
}
116119

@@ -136,14 +139,17 @@ trait ext_ctxt {
136139
fn span_bug(sp: span, msg: ~str) -> !;
137140
fn bug(msg: ~str) -> !;
138141
fn next_id() -> ast::node_id;
142+
pure fn trace_macros() -> bool;
143+
fn set_trace_macros(x: bool);
139144
}
140145

141146
fn mk_ctxt(parse_sess: parse::parse_sess,
142147
cfg: ast::crate_cfg) -> ext_ctxt {
143148
type ctxt_repr = {parse_sess: parse::parse_sess,
144149
cfg: ast::crate_cfg,
145150
mut backtrace: expn_info,
146-
mut mod_path: ~[ast::ident]};
151+
mut mod_path: ~[ast::ident],
152+
mut trace_mac: bool};
147153
impl ctxt_repr: ext_ctxt {
148154
fn codemap() -> codemap { self.parse_sess.cm }
149155
fn parse_sess() -> parse::parse_sess { self.parse_sess }
@@ -199,12 +205,19 @@ fn mk_ctxt(parse_sess: parse::parse_sess,
199205
fn next_id() -> ast::node_id {
200206
return parse::next_node_id(self.parse_sess);
201207
}
208+
pure fn trace_macros() -> bool {
209+
self.trace_mac
210+
}
211+
fn set_trace_macros(x: bool) {
212+
self.trace_mac = x
213+
}
202214
}
203215
let imp : ctxt_repr = {
204216
parse_sess: parse_sess,
205217
cfg: cfg,
206218
mut backtrace: none,
207-
mut mod_path: ~[]
219+
mut mod_path: ~[],
220+
mut trace_mac: false
208221
};
209222
return imp as ext_ctxt
210223
}

src/libsyntax/ext/trace_macros.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import codemap::span;
2+
import ext::base::ext_ctxt;
3+
import ast::tt_delim;
4+
import parse::lexer::{new_tt_reader, reader};
5+
import parse::parser::{parser, SOURCE_FILE};
6+
import parse::common::parser_common;
7+
8+
fn expand_trace_macros(cx: ext_ctxt, sp: span,
9+
tt: ~[ast::token_tree]) -> base::mac_result
10+
{
11+
let sess = cx.parse_sess();
12+
let cfg = cx.cfg();
13+
let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic,
14+
cx.parse_sess().interner, none, tt);
15+
let rdr = tt_rdr as reader;
16+
let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE);
17+
18+
let arg = rust_parser.parse_ident();
19+
match arg {
20+
@~"true" => cx.set_trace_macros(true),
21+
@~"false" => cx.set_trace_macros(false),
22+
_ => cx.span_fatal(sp, ~"trace_macros! only accepts `true` or `false`")
23+
}
24+
let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE);
25+
let result = rust_parser.parse_expr();
26+
base::mr_expr(result)
27+
}

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,17 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
4747
};
4848

4949
// Given `lhses` and `rhses`, this is the new macro we create
50-
fn generic_extension(cx: ext_ctxt, sp: span, _name: ident,
50+
fn generic_extension(cx: ext_ctxt, sp: span, name: ident,
5151
arg: ~[ast::token_tree],
5252
lhses: ~[@named_match], rhses: ~[@named_match])
5353
-> mac_result {
5454

55-
//io::println(fmt!("%s! { %s }", *name,
56-
// print::pprust::unexpanded_tt_to_str(
57-
// ast::tt_delim(arg),
58-
// cx.parse_sess().interner)));
55+
if cx.trace_macros() {
56+
io::println(fmt!("%s! { %s }", *name,
57+
print::pprust::unexpanded_tt_to_str(
58+
ast::tt_delim(arg),
59+
cx.parse_sess().interner)));
60+
}
5961

6062
// Which arm's failure should we report? (the one furthest along)
6163
let mut best_fail_spot = {lo: 0u, hi: 0u, expn_info: none};

src/libsyntax/syntax.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ mod ext {
8989
mod check;
9090
mod liveness;
9191
}
92+
93+
mod trace_macros;
9294
}

0 commit comments

Comments
 (0)