2011-09-01 17:27:58 -07:00
|
|
|
import std::str;
|
2011-08-15 16:38:23 -07:00
|
|
|
import std::vec;
|
2011-06-04 15:41:45 -04:00
|
|
|
import std::option;
|
|
|
|
import std::map::hashmap;
|
2011-07-06 15:22:23 -07:00
|
|
|
import driver::session::session;
|
2011-07-05 11:48:19 +02:00
|
|
|
import codemap::span;
|
2011-07-06 16:46:17 +02:00
|
|
|
import std::map::new_str_hash;
|
2011-07-05 11:48:19 +02:00
|
|
|
import codemap;
|
2011-06-04 15:41:45 -04:00
|
|
|
|
2011-07-13 15:44:09 -07:00
|
|
|
type syntax_expander =
|
2011-08-26 18:48:08 -07:00
|
|
|
fn(&ext_ctxt, span, @ast::expr, &option::t<istr>) -> @ast::expr;
|
2011-08-27 16:52:00 -07:00
|
|
|
type macro_def = {ident: istr, ext: syntax_extension};
|
2011-07-27 14:19:39 +02:00
|
|
|
type macro_definer =
|
2011-08-26 18:48:08 -07:00
|
|
|
fn(&ext_ctxt, span, @ast::expr, &option::t<istr>) -> macro_def;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-20 17:26:17 -07:00
|
|
|
tag syntax_extension {
|
|
|
|
normal(syntax_expander);
|
|
|
|
macro_defining(macro_definer);
|
|
|
|
}
|
2011-06-04 15:41:45 -04:00
|
|
|
|
|
|
|
// A temporary hard-coded map of methods for expanding syntax extension
|
|
|
|
// AST nodes into full ASTs
|
2011-08-25 15:12:54 -07:00
|
|
|
fn syntax_expander_table() -> hashmap<istr, syntax_extension> {
|
2011-08-13 00:09:25 -07:00
|
|
|
let syntax_expanders = new_str_hash::<syntax_extension>();
|
2011-09-01 17:49:29 -07:00
|
|
|
syntax_expanders.insert(~"fmt", normal(ext::fmt::expand_syntax_ext));
|
2011-08-25 15:12:54 -07:00
|
|
|
syntax_expanders.insert(~"env", normal(ext::env::expand_syntax_ext));
|
|
|
|
syntax_expanders.insert(~"macro",
|
2011-07-05 11:48:19 +02:00
|
|
|
macro_defining(ext::simplext::add_new_extension));
|
2011-08-25 15:12:54 -07:00
|
|
|
syntax_expanders.insert(~"concat_idents",
|
2011-08-03 11:46:32 -07:00
|
|
|
normal(ext::concat_idents::expand_syntax_ext));
|
2011-08-25 15:12:54 -07:00
|
|
|
syntax_expanders.insert(~"ident_to_str",
|
2011-08-03 11:46:32 -07:00
|
|
|
normal(ext::ident_to_str::expand_syntax_ext));
|
2011-08-25 15:12:54 -07:00
|
|
|
syntax_expanders.insert(~"log_syntax",
|
2011-08-08 14:17:33 -07:00
|
|
|
normal(ext::log_syntax::expand_syntax_ext));
|
2011-06-04 15:41:45 -04:00
|
|
|
ret syntax_expanders;
|
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
obj ext_ctxt(sess: @session,
|
2011-08-27 16:52:00 -07:00
|
|
|
crate_file_name_hack: istr,
|
2011-08-15 13:33:12 -07:00
|
|
|
mutable backtrace: codemap::opt_span) {
|
2011-08-27 16:52:00 -07:00
|
|
|
fn crate_file_name() -> istr { ret crate_file_name_hack; }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-08-08 14:17:33 -07:00
|
|
|
fn session() -> @session { ret sess; }
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn print_backtrace() { }
|
2011-08-08 14:17:33 -07:00
|
|
|
|
2011-08-15 13:33:12 -07:00
|
|
|
fn backtrace() -> codemap::opt_span { ret backtrace; }
|
|
|
|
|
|
|
|
fn bt_push(sp: span) {
|
2011-08-19 15:16:48 -07:00
|
|
|
backtrace =
|
|
|
|
codemap::os_some(@{lo: sp.lo,
|
|
|
|
hi: sp.hi,
|
|
|
|
expanded_from: backtrace});
|
2011-08-15 13:33:12 -07:00
|
|
|
}
|
|
|
|
fn bt_pop() {
|
|
|
|
alt backtrace {
|
|
|
|
codemap::os_some(@{expanded_from: pre, _}) {
|
|
|
|
let tmp = pre;
|
|
|
|
backtrace = tmp;
|
|
|
|
}
|
2011-08-27 16:52:00 -07:00
|
|
|
_ { self.bug(~"tried to pop without a push"); }
|
2011-08-15 13:33:12 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-08-27 16:52:00 -07:00
|
|
|
fn span_fatal(sp: span, msg: istr) -> ! {
|
2011-08-05 13:06:11 -07:00
|
|
|
self.print_backtrace();
|
2011-08-27 16:52:00 -07:00
|
|
|
sess.span_fatal(sp, msg);
|
2011-08-05 13:06:11 -07:00
|
|
|
}
|
2011-08-27 16:52:00 -07:00
|
|
|
fn span_err(sp: span, msg: istr) {
|
2011-08-05 13:06:11 -07:00
|
|
|
self.print_backtrace();
|
2011-08-27 16:52:00 -07:00
|
|
|
sess.span_err(sp, msg);
|
2011-06-04 15:41:45 -04:00
|
|
|
}
|
2011-08-27 16:52:00 -07:00
|
|
|
fn span_unimpl(sp: span, msg: istr) -> ! {
|
2011-08-05 13:06:11 -07:00
|
|
|
self.print_backtrace();
|
2011-08-27 16:52:00 -07:00
|
|
|
sess.span_unimpl(sp, msg);
|
2011-06-04 16:55:32 -04:00
|
|
|
}
|
2011-08-27 16:52:00 -07:00
|
|
|
fn span_bug(sp: span, msg: istr) -> ! {
|
2011-08-05 13:06:11 -07:00
|
|
|
self.print_backtrace();
|
2011-08-27 16:52:00 -07:00
|
|
|
sess.span_bug(sp, msg);
|
2011-08-27 16:36:48 -07:00
|
|
|
}
|
2011-08-27 16:52:00 -07:00
|
|
|
fn bug(msg: istr) -> ! {
|
2011-08-27 16:36:48 -07:00
|
|
|
self.print_backtrace();
|
2011-08-27 16:52:00 -07:00
|
|
|
sess.bug(msg);
|
2011-08-05 13:06:11 -07:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
fn next_id() -> ast::node_id { ret sess.next_node_id(); }
|
2011-07-21 16:47:47 -07:00
|
|
|
|
2011-08-05 13:06:11 -07:00
|
|
|
}
|
2011-07-10 17:00:28 -07:00
|
|
|
|
2011-08-05 13:06:11 -07:00
|
|
|
fn mk_ctxt(sess: &session) -> ext_ctxt {
|
2011-07-10 17:00:28 -07:00
|
|
|
// FIXME: Some extensions work by building ASTs with paths to functions
|
|
|
|
// they need to call at runtime. As those functions live in the std crate,
|
|
|
|
// the paths are prefixed with "std::". Unfortunately, these paths can't
|
|
|
|
// work for code called from inside the stdard library, so here we pass
|
|
|
|
// the extensions the file name of the crate being compiled so they can
|
|
|
|
// use it to guess whether paths should be prepended with "std::". This is
|
|
|
|
// super-ugly and needs a better solution.
|
2011-08-19 15:16:48 -07:00
|
|
|
let crate_file_name_hack = sess.get_codemap().files[0].name;
|
2011-07-21 16:47:47 -07:00
|
|
|
|
2011-08-27 16:52:00 -07:00
|
|
|
ret ext_ctxt(@sess, crate_file_name_hack,
|
2011-08-27 00:43:22 -07:00
|
|
|
codemap::os_none);
|
2011-06-04 15:41:45 -04:00
|
|
|
}
|
2011-06-20 17:26:17 -07:00
|
|
|
|
2011-08-27 16:52:00 -07:00
|
|
|
fn expr_to_str(cx: &ext_ctxt, expr: @ast::expr, error: &istr) -> istr {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt expr.node {
|
|
|
|
ast::expr_lit(l) {
|
|
|
|
alt l.node {
|
2011-09-01 22:08:59 -07:00
|
|
|
ast::lit_str(s) { ret s; }
|
2011-07-27 14:19:39 +02:00
|
|
|
_ { cx.span_fatal(l.span, error); }
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { cx.span_fatal(expr.span, error); }
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-27 16:52:00 -07:00
|
|
|
fn expr_to_ident(cx: &ext_ctxt, expr: @ast::expr,
|
|
|
|
error: &istr) -> ast::ident {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt expr.node {
|
|
|
|
ast::expr_path(p) {
|
2011-08-15 16:38:23 -07:00
|
|
|
if vec::len(p.node.types) > 0u || vec::len(p.node.idents) != 1u {
|
2011-06-20 17:26:17 -07:00
|
|
|
cx.span_fatal(expr.span, error);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { ret p.node.idents[0]; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { cx.span_fatal(expr.span, error); }
|
2011-06-20 17:26:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 11:46:32 -07:00
|
|
|
fn make_new_lit(cx: &ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
|
|
|
|
@ast::expr {
|
|
|
|
let sp_lit = @{node: lit, span: sp};
|
|
|
|
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
|
|
|
|
}
|
|
|
|
|
2011-06-20 17:26:17 -07:00
|
|
|
|
|
|
|
|
2011-06-04 15:41:45 -04:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|