2011-07-05 16:23:07 -07:00
|
|
|
import std::ivec;
|
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-07-06 19:00:00 -07:00
|
|
|
fn(&ext_ctxt, span, &(@ast::expr)[], option::t[str]) -> @ast::expr;
|
2011-07-26 14:06:02 +02:00
|
|
|
type macro_def = rec(str ident, syntax_extension ext);
|
2011-07-06 19:00:00 -07:00
|
|
|
type macro_definer = fn(&ext_ctxt, span, &(@ast::expr)[],
|
2011-07-26 14:06:02 +02:00
|
|
|
option::t[str]) -> 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
|
|
|
|
fn syntax_expander_table() -> hashmap[str, syntax_extension] {
|
|
|
|
auto syntax_expanders = new_str_hash[syntax_extension]();
|
2011-07-05 11:48:19 +02:00
|
|
|
syntax_expanders.insert("fmt", normal(ext::fmt::expand_syntax_ext));
|
|
|
|
syntax_expanders.insert("env", normal(ext::env::expand_syntax_ext));
|
2011-07-13 15:44:09 -07:00
|
|
|
syntax_expanders.insert("macro",
|
2011-07-05 11:48:19 +02:00
|
|
|
macro_defining(ext::simplext::add_new_extension));
|
2011-06-04 15:41:45 -04:00
|
|
|
ret syntax_expanders;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
type span_msg_fn = fn(span, str) -> ! ;
|
2011-07-21 16:47:47 -07:00
|
|
|
type msg_fn = fn(str) -> ! ;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-19 22:41:21 +02:00
|
|
|
type next_id_fn = fn() -> ast::node_id ;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-06-04 15:41:45 -04:00
|
|
|
|
|
|
|
// Provides a limited set of services necessary for syntax extensions
|
|
|
|
// to do their thing
|
2011-06-15 11:19:50 -07:00
|
|
|
type ext_ctxt =
|
2011-07-10 17:00:28 -07:00
|
|
|
rec(str crate_file_name_hack,
|
|
|
|
span_msg_fn span_fatal,
|
2011-06-18 22:41:20 -07:00
|
|
|
span_msg_fn span_unimpl,
|
2011-07-21 16:47:47 -07:00
|
|
|
span_msg_fn span_bug,
|
|
|
|
msg_fn bug,
|
2011-06-19 22:41:21 +02:00
|
|
|
next_id_fn next_id);
|
2011-06-04 17:31:44 -04:00
|
|
|
|
2011-07-06 15:22:23 -07:00
|
|
|
fn mk_ctxt(&session sess) -> ext_ctxt {
|
|
|
|
fn ext_span_fatal_(&session sess, span sp, str msg) -> ! {
|
|
|
|
sess.span_err(sp, msg);
|
2011-07-05 11:48:19 +02:00
|
|
|
fail;
|
2011-06-04 15:41:45 -04:00
|
|
|
}
|
2011-07-06 15:22:23 -07:00
|
|
|
auto ext_span_fatal = bind ext_span_fatal_(sess, _, _);
|
|
|
|
fn ext_span_unimpl_(&session sess, span sp, str msg) -> ! {
|
|
|
|
sess.span_err(sp, "unimplemented " + msg);
|
2011-07-05 11:48:19 +02:00
|
|
|
fail;
|
2011-06-04 16:55:32 -04:00
|
|
|
}
|
2011-07-21 16:47:47 -07:00
|
|
|
auto ext_span_unimpl = bind ext_span_unimpl_(sess, _, _);
|
|
|
|
fn ext_span_bug_(&session sess, span sp, str msg) -> ! {
|
|
|
|
sess.span_bug(sp, msg);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
auto ext_span_bug = bind ext_span_bug_(sess, _, _);
|
|
|
|
fn ext_bug_(&session sess, str msg) -> ! {
|
|
|
|
sess.bug(msg);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
auto ext_bug = bind ext_bug_(sess, _);
|
|
|
|
|
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-07-06 15:22:23 -07:00
|
|
|
auto crate_file_name_hack = sess.get_codemap().files.(0).name;
|
2011-07-21 16:47:47 -07:00
|
|
|
|
2011-07-06 15:22:23 -07:00
|
|
|
fn ext_next_id_(&session sess) -> ast::node_id {
|
|
|
|
ret sess.next_node_id(); // temporary, until bind works better
|
|
|
|
}
|
|
|
|
auto ext_next_id = bind ext_next_id_(sess);
|
2011-07-10 17:00:28 -07:00
|
|
|
ret rec(crate_file_name_hack=crate_file_name_hack,
|
|
|
|
span_fatal=ext_span_fatal,
|
2011-06-15 11:19:50 -07:00
|
|
|
span_unimpl=ext_span_unimpl,
|
2011-07-21 16:47:47 -07:00
|
|
|
span_bug=ext_span_bug,
|
|
|
|
bug=ext_bug,
|
2011-06-19 22:41:21 +02:00
|
|
|
next_id=ext_next_id);
|
2011-06-04 15:41:45 -04:00
|
|
|
}
|
2011-06-20 17:26:17 -07:00
|
|
|
|
|
|
|
fn expr_to_str(&ext_ctxt cx, @ast::expr expr, str error) -> str {
|
|
|
|
alt (expr.node) {
|
|
|
|
case (ast::expr_lit(?l)) {
|
|
|
|
alt (l.node) {
|
|
|
|
case (ast::lit_str(?s, _)) { ret s; }
|
|
|
|
case (_) { cx.span_fatal(l.span, error); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) { cx.span_fatal(expr.span, error); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_to_ident(&ext_ctxt cx, @ast::expr expr, str error) -> ast::ident {
|
|
|
|
alt(expr.node) {
|
|
|
|
case (ast::expr_path(?p)) {
|
2011-07-13 15:44:09 -07:00
|
|
|
if (ivec::len(p.node.types) > 0u
|
2011-07-05 16:23:07 -07:00
|
|
|
|| ivec::len(p.node.idents) != 1u) {
|
2011-06-20 17:26:17 -07:00
|
|
|
cx.span_fatal(expr.span, error);
|
|
|
|
} else {
|
|
|
|
ret p.node.idents.(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
cx.span_fatal(expr.span, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
//
|