Add trace_macros!
This commit is contained in:
parent
00ef5418d4
commit
1e96099918
@ -111,6 +111,9 @@ fn syntax_expander_table() -> hashmap<~str, syntax_extension> {
|
||||
builtin(ext::source_util::expand_mod));
|
||||
syntax_expanders.insert(~"proto",
|
||||
builtin_item_tt(ext::pipes::expand_proto));
|
||||
syntax_expanders.insert(
|
||||
~"trace_macros",
|
||||
builtin_expr_tt(ext::trace_macros::expand_trace_macros));
|
||||
return syntax_expanders;
|
||||
}
|
||||
|
||||
@ -136,6 +139,8 @@ trait ext_ctxt {
|
||||
fn span_bug(sp: span, msg: ~str) -> !;
|
||||
fn bug(msg: ~str) -> !;
|
||||
fn next_id() -> ast::node_id;
|
||||
pure fn trace_macros() -> bool;
|
||||
fn set_trace_macros(x: bool);
|
||||
}
|
||||
|
||||
fn mk_ctxt(parse_sess: parse::parse_sess,
|
||||
@ -143,7 +148,8 @@ fn mk_ctxt(parse_sess: parse::parse_sess,
|
||||
type ctxt_repr = {parse_sess: parse::parse_sess,
|
||||
cfg: ast::crate_cfg,
|
||||
mut backtrace: expn_info,
|
||||
mut mod_path: ~[ast::ident]};
|
||||
mut mod_path: ~[ast::ident],
|
||||
mut trace_mac: bool};
|
||||
impl ctxt_repr: ext_ctxt {
|
||||
fn codemap() -> codemap { self.parse_sess.cm }
|
||||
fn parse_sess() -> parse::parse_sess { self.parse_sess }
|
||||
@ -199,12 +205,19 @@ fn mk_ctxt(parse_sess: parse::parse_sess,
|
||||
fn next_id() -> ast::node_id {
|
||||
return parse::next_node_id(self.parse_sess);
|
||||
}
|
||||
pure fn trace_macros() -> bool {
|
||||
self.trace_mac
|
||||
}
|
||||
fn set_trace_macros(x: bool) {
|
||||
self.trace_mac = x
|
||||
}
|
||||
}
|
||||
let imp : ctxt_repr = {
|
||||
parse_sess: parse_sess,
|
||||
cfg: cfg,
|
||||
mut backtrace: none,
|
||||
mut mod_path: ~[]
|
||||
mut mod_path: ~[],
|
||||
mut trace_mac: false
|
||||
};
|
||||
return imp as ext_ctxt
|
||||
}
|
||||
|
27
src/libsyntax/ext/trace_macros.rs
Normal file
27
src/libsyntax/ext/trace_macros.rs
Normal file
@ -0,0 +1,27 @@
|
||||
import codemap::span;
|
||||
import ext::base::ext_ctxt;
|
||||
import ast::tt_delim;
|
||||
import parse::lexer::{new_tt_reader, reader};
|
||||
import parse::parser::{parser, SOURCE_FILE};
|
||||
import parse::common::parser_common;
|
||||
|
||||
fn expand_trace_macros(cx: ext_ctxt, sp: span,
|
||||
tt: ~[ast::token_tree]) -> base::mac_result
|
||||
{
|
||||
let sess = cx.parse_sess();
|
||||
let cfg = cx.cfg();
|
||||
let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic,
|
||||
cx.parse_sess().interner, none, tt);
|
||||
let rdr = tt_rdr as reader;
|
||||
let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE);
|
||||
|
||||
let arg = rust_parser.parse_ident();
|
||||
match arg {
|
||||
@~"true" => cx.set_trace_macros(true),
|
||||
@~"false" => cx.set_trace_macros(false),
|
||||
_ => cx.span_fatal(sp, ~"trace_macros! only accepts `true` or `false`")
|
||||
}
|
||||
let rust_parser = parser(sess, cfg, rdr.dup(), SOURCE_FILE);
|
||||
let result = rust_parser.parse_expr();
|
||||
base::mr_expr(result)
|
||||
}
|
@ -47,15 +47,17 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
|
||||
};
|
||||
|
||||
// Given `lhses` and `rhses`, this is the new macro we create
|
||||
fn generic_extension(cx: ext_ctxt, sp: span, _name: ident,
|
||||
fn generic_extension(cx: ext_ctxt, sp: span, name: ident,
|
||||
arg: ~[ast::token_tree],
|
||||
lhses: ~[@named_match], rhses: ~[@named_match])
|
||||
-> mac_result {
|
||||
|
||||
//io::println(fmt!("%s! { %s }", *name,
|
||||
// print::pprust::unexpanded_tt_to_str(
|
||||
// ast::tt_delim(arg),
|
||||
// cx.parse_sess().interner)));
|
||||
if cx.trace_macros() {
|
||||
io::println(fmt!("%s! { %s }", *name,
|
||||
print::pprust::unexpanded_tt_to_str(
|
||||
ast::tt_delim(arg),
|
||||
cx.parse_sess().interner)));
|
||||
}
|
||||
|
||||
// Which arm's failure should we report? (the one furthest along)
|
||||
let mut best_fail_spot = {lo: 0u, hi: 0u, expn_info: none};
|
||||
|
@ -89,4 +89,6 @@ mod ext {
|
||||
mod check;
|
||||
mod liveness;
|
||||
}
|
||||
|
||||
mod trace_macros;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user