about summary refs log tree commit diff
path: root/src/comp/syntax/ext/base.rs
blob: 55294000f66f9a032d6db22b35966eec3495bc05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import core::{vec, option};
import std::map::hashmap;
import driver::session::session;
import codemap::span;
import std::map::new_str_hash;
import codemap;

type syntax_expander =
    fn@(ext_ctxt, span, @ast::expr, option::t<str>) -> @ast::expr;
type macro_def = {ident: str, ext: syntax_extension};
type macro_definer =
    fn@(ext_ctxt, span, @ast::expr, option::t<str>) -> macro_def;

tag syntax_extension {
    normal(syntax_expander);
    macro_defining(macro_definer);
}

// A temporary hard-coded map of methods for expanding syntax extension
// AST nodes into full ASTs
fn syntax_expander_table() -> hashmap<str, syntax_extension> {
    let syntax_expanders = new_str_hash::<syntax_extension>();
    syntax_expanders.insert("fmt", normal(ext::fmt::expand_syntax_ext));
    syntax_expanders.insert("env", normal(ext::env::expand_syntax_ext));
    syntax_expanders.insert("macro",
                            macro_defining(ext::simplext::add_new_extension));
    syntax_expanders.insert("concat_idents",
                            normal(ext::concat_idents::expand_syntax_ext));
    syntax_expanders.insert("ident_to_str",
                            normal(ext::ident_to_str::expand_syntax_ext));
    syntax_expanders.insert("log_syntax",
                            normal(ext::log_syntax::expand_syntax_ext));
    ret syntax_expanders;
}

obj ext_ctxt(sess: @session,
             crate_file_name_hack: str,
             mutable backtrace: codemap::opt_span) {
    fn crate_file_name() -> str { ret crate_file_name_hack; }

    fn session() -> @session { ret sess; }

    fn print_backtrace() { }

    fn backtrace() -> codemap::opt_span { ret backtrace; }

    fn bt_push(sp: span) {
        backtrace =
            codemap::os_some(@{lo: sp.lo,
                               hi: sp.hi,
                               expanded_from: backtrace});
    }
    fn bt_pop() {
        alt backtrace {
          codemap::os_some(@{expanded_from: pre, _}) {
            let tmp = pre;
            backtrace = tmp;
          }
          _ { self.bug("tried to pop without a push"); }
        }
    }

    fn span_fatal(sp: span, msg: str) -> ! {
        self.print_backtrace();
        sess.span_fatal(sp, msg);
    }
    fn span_err(sp: span, msg: str) {
        self.print_backtrace();
        sess.span_err(sp, msg);
    }
    fn span_unimpl(sp: span, msg: str) -> ! {
        self.print_backtrace();
        sess.span_unimpl(sp, msg);
    }
    fn span_bug(sp: span, msg: str) -> ! {
        self.print_backtrace();
        sess.span_bug(sp, msg);
    }
    fn bug(msg: str) -> ! { self.print_backtrace(); sess.bug(msg); }
    fn next_id() -> ast::node_id { ret sess.next_node_id(); }

}

fn mk_ctxt(sess: session) -> ext_ctxt {
    // 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.
    let crate_file_name_hack = sess.get_codemap().files[0].name;

    ret ext_ctxt(@sess, crate_file_name_hack, codemap::os_none);
}

fn expr_to_str(cx: ext_ctxt, expr: @ast::expr, error: str) -> str {
    alt expr.node {
      ast::expr_lit(l) {
        alt l.node {
          ast::lit_str(s) { ret s; }
          _ { cx.span_fatal(l.span, error); }
        }
      }
      _ { cx.span_fatal(expr.span, error); }
    }
}

fn expr_to_ident(cx: ext_ctxt, expr: @ast::expr, error: str) -> ast::ident {
    alt expr.node {
      ast::expr_path(p) {
        if vec::len(p.node.types) > 0u || vec::len(p.node.idents) != 1u {
            cx.span_fatal(expr.span, error);
        } else { ret p.node.idents[0]; }
      }
      _ { cx.span_fatal(expr.span, error); }
    }
}

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};
}



//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//