Add a syntax extension to log syntax, for debugging macros.

This commit is contained in:
Paul Stansifer 2011-08-08 14:17:33 -07:00
parent e7139e28f4
commit f50a582126
3 changed files with 22 additions and 3 deletions

View File

@ -49,6 +49,7 @@ mod middle {
mod syntax { mod syntax {
mod ast; mod ast;
mod fold; mod fold;
mod visit; mod visit;
mod codemap; mod codemap;
@ -67,6 +68,7 @@ mod syntax {
mod simplext; mod simplext;
mod concat_idents; mod concat_idents;
mod ident_to_str; mod ident_to_str;
mod log_syntax;
} }
mod print { mod print {
mod pprust; mod pprust;

View File

@ -29,19 +29,23 @@ fn syntax_expander_table() -> hashmap[str, syntax_extension] {
normal(ext::concat_idents::expand_syntax_ext)); normal(ext::concat_idents::expand_syntax_ext));
syntax_expanders.insert("ident_to_str", syntax_expanders.insert("ident_to_str",
normal(ext::ident_to_str::expand_syntax_ext)); normal(ext::ident_to_str::expand_syntax_ext));
syntax_expanders.insert("log_syntax",
normal(ext::log_syntax::expand_syntax_ext));
ret syntax_expanders; ret syntax_expanders;
} }
obj ext_ctxt(sess: @session, crate_file_name_hack: str, obj ext_ctxt(sess: @session, crate_file_name_hack: str,
mutable backtrace: span[]) { mutable backtrace: span[]) {
fn crate_file_name() -> str { ret crate_file_name_hack; } fn crate_file_name() -> str { ret crate_file_name_hack; }
fn session() -> @session { ret sess; }
fn print_backtrace() { fn print_backtrace() {
for sp: span in backtrace { for sp: span in backtrace {
sess.span_note(sp, "(while expanding this)") sess.span_note(sp, "(while expanding this)")
} }
} }
fn bt_push(sp: span) { backtrace += ~[sp]; } fn bt_push(sp: span) { backtrace += ~[sp]; }
fn bt_pop() { ivec::pop(backtrace); } fn bt_pop() { ivec::pop(backtrace); }
@ -59,7 +63,7 @@ obj ext_ctxt(sess: @session, crate_file_name_hack: str,
} }
fn span_bug(sp:span, msg: str) -> ! { fn span_bug(sp:span, msg: str) -> ! {
self.print_backtrace(); self.print_backtrace();
sess.span_bug(sp, msg); sess.span_bug(sp, msg);
} }
fn bug(msg: str) -> ! { fn bug(msg: str) -> ! {
self.print_backtrace(); self.print_backtrace();

View File

@ -0,0 +1,13 @@
import std::option;
import base::*;
import syntax::ast;
fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
body: option::t[str]) -> @ast::expr {
cx.print_backtrace();
std::io::stdout().write_line(print::pprust::expr_to_str(arg));
//trivial expression
ret @{id: cx.next_id(), node: ast::expr_rec(~[], option::none), span: sp};
}