Skip to content

Commit 3bc4da9

Browse files
committed
Inject "core macros" into default syntax-expansion environment. Bit of a kludge but enough to work on logging-via-macros.
1 parent a24c19e commit 3bc4da9

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

src/comp/syntax/codemap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ fn next_line(file: filemap, chpos: uint, byte_pos: uint) {
3434
type lookup_fn = fn(file_pos) -> uint;
3535

3636
fn lookup_pos(map: codemap, pos: uint, lookup: lookup_fn) -> loc {
37+
let len = vec::len(map.files);
3738
let a = 0u;
38-
let b = vec::len(map.files);
39+
let b = len;
3940
while b - a > 1u {
4041
let m = (a + b) / 2u;
4142
if lookup(map.files[m].start_pos) > pos { b = m; } else { a = m; }
4243
}
44+
if (a >= len) {
45+
ret { filename: "-", line: 0u, col: 0u };
46+
}
4347
let f = map.files[a];
4448
a = 0u;
4549
b = vec::len(f.lines);

src/comp/syntax/ext/base.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ fn syntax_expander_table() -> hashmap<str, syntax_extension> {
3434
}
3535

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

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

@@ -82,16 +80,7 @@ obj ext_ctxt(sess: @session,
8280
}
8381

8482
fn mk_ctxt(sess: session) -> ext_ctxt {
85-
// FIXME: Some extensions work by building ASTs with paths to functions
86-
// they need to call at runtime. As those functions live in the std crate,
87-
// the paths are prefixed with "std::". Unfortunately, these paths can't
88-
// work for code called from inside the stdard library, so here we pass
89-
// the extensions the file name of the crate being compiled so they can
90-
// use it to guess whether paths should be prepended with "std::". This is
91-
// super-ugly and needs a better solution.
92-
let crate_file_name_hack = sess.get_codemap().files[0].name;
93-
94-
ret ext_ctxt(@sess, crate_file_name_hack, codemap::os_none);
83+
ret ext_ctxt(@sess, codemap::os_none);
9584
}
9685

9786
fn expr_to_str(cx: ext_ctxt, expr: @ast::expr, error: str) -> str {

src/comp/syntax/ext/expand.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import vec;
88
import syntax::ast::{crate, expr_, expr_mac, mac_invoc};
99
import syntax::fold::*;
1010
import syntax::ext::base::*;
11-
11+
import syntax::parse::parser::parse_expr_from_source_str;
1212

1313
fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
1414
fld: ast_fold, orig: fn@(expr_, ast_fold) -> expr_) -> expr_ {
@@ -47,6 +47,21 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt, e: expr_,
4747
};
4848
}
4949

50+
// FIXME: this is a terrible kludge to inject some macros into the default
51+
// compilation environment. When the macro-definition system is substantially
52+
// more mature, these should move from here, into a compiled part of libcore
53+
// at very least.
54+
55+
fn core_macros() -> str {
56+
ret
57+
"{
58+
#macro[[#error[f, ...], log_err #fmt[f, ...]]];
59+
#macro[[#warn[f, ...], log_err #fmt[f, ...]]];
60+
#macro[[#info[f, ...], log_err #fmt[f, ...]]];
61+
#macro[[#debug[f, ...], log_err #fmt[f, ...]]];
62+
}";
63+
}
64+
5065
fn expand_crate(sess: session::session, c: @crate) -> @crate {
5166
let exts = syntax_expander_table();
5267
let afp = default_ast_fold();
@@ -55,9 +70,16 @@ fn expand_crate(sess: session::session, c: @crate) -> @crate {
5570
{fold_expr: bind expand_expr(exts, cx, _, _, afp.fold_expr)
5671
with *afp};
5772
let f = make_fold(f_pre);
73+
let cm = parse_expr_from_source_str("-", core_macros(),
74+
sess.get_opts().cfg,
75+
sess.get_parse_sess());
76+
77+
// This is run for its side-effects on the expander env,
78+
// as it registers all the core macros as expanders.
79+
f.fold_expr(cm);
80+
5881
let res = @f.fold_crate(*c);
5982
ret res;
60-
6183
}
6284
// Local Variables:
6385
// mode: rust

src/comp/syntax/parse/parser.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: str,
7171
ret new_parser(sess, cfg, rdr, ftype);
7272
}
7373

74+
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
75+
name: str, source: str) -> parser {
76+
let ftype = SOURCE_FILE;
77+
let filemap = codemap::new_filemap(name, 0u, 0u);
78+
sess.cm.files += [filemap];
79+
let itr = @interner::mk(str::hash, str::eq);
80+
let rdr = lexer::new_reader(sess.cm, source, filemap, itr);
81+
ret new_parser(sess, cfg, rdr, ftype);
82+
}
83+
7484
fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader,
7585
ftype: file_type) -> parser {
7686
obj stdio_parser(sess: parse_sess,
@@ -2417,14 +2427,16 @@ fn parse_crate_from_source_file(input: str, cfg: ast::crate_cfg,
24172427
ret parse_crate_mod(p, cfg);
24182428
}
24192429

2430+
2431+
fn parse_expr_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
2432+
sess: parse_sess) -> @ast::expr {
2433+
let p = new_parser_from_source_str(sess, cfg, name, source);
2434+
ret parse_expr(p);
2435+
}
2436+
24202437
fn parse_crate_from_source_str(name: str, source: str, cfg: ast::crate_cfg,
24212438
sess: parse_sess) -> @ast::crate {
2422-
let ftype = SOURCE_FILE;
2423-
let filemap = codemap::new_filemap(name, 0u, 0u);
2424-
sess.cm.files += [filemap];
2425-
let itr = @interner::mk(str::hash, str::eq);
2426-
let rdr = lexer::new_reader(sess.cm, source, filemap, itr);
2427-
let p = new_parser(sess, cfg, rdr, ftype);
2439+
let p = new_parser_from_source_str(sess, cfg, name, source);
24282440
ret parse_crate_mod(p, cfg);
24292441
}
24302442

0 commit comments

Comments
 (0)