Skip to content

Commit

Permalink
Use file_substr rather than <anon> when re-parsing quasi-quotes
Browse files Browse the repository at this point in the history
for better error messages.
  • Loading branch information
kevina authored and nikomatsakis committed Feb 15, 2012
1 parent 0e44133 commit 3791947
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
8 changes: 2 additions & 6 deletions src/comp/syntax/codemap.rs
Expand Up @@ -40,14 +40,10 @@ fn new_filemap(filename: filename, src: @str,
start_pos_ch, start_pos_byte);
}

fn get_substr_info(cm: codemap, sp: span)
-> (filename, file_substr)
fn mk_substr_filename(cm: codemap, sp: span) -> str
{
let pos = lookup_char_pos(cm, sp.lo);
let name = #fmt("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
ret (name, fss_internal(sp));
//ret (name, fss_external({filename: pos.file.name,
// line: pos.line, col: pos.col}));
ret #fmt("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
}

fn next_line(file: filemap, chpos: uint, byte_pos: uint) {
Expand Down
2 changes: 1 addition & 1 deletion src/comp/syntax/ext/expand.rs
Expand Up @@ -5,7 +5,7 @@ import std::map::hashmap;
import syntax::ast::{crate, expr_, expr_mac, mac_invoc};
import syntax::fold::*;
import syntax::ext::base::*;
import syntax::ext::qquote::{expand_qquote,qq_helper};
import syntax::ext::qquote::{qq_helper};
import syntax::parse::parser::parse_expr_from_source_str;

import codemap::{span, expanded_from};
Expand Down
39 changes: 22 additions & 17 deletions src/comp/syntax/ext/qquote.rs
Expand Up @@ -143,18 +143,6 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
}
}
let body = get_mac_body(ecx,_sp,body);
fn finish<T: qq_helper>(ecx: ext_ctxt, body: ast::mac_body_,
f: fn (p: parser) -> T)
-> @ast::expr
{
let cm = ecx.session().parse_sess.cm;
let str = @codemap::span_to_snippet(body.span, cm);
let (fname, ss) = codemap::get_substr_info(cm, body.span);
let node = parse_from_source_str
(f, fname, ss, str,
ecx.session().opts.cfg, ecx.session().parse_sess);
ret expand_qquote(ecx, node.span(), *str, node);
}

ret alt what {
"expr" {finish(ecx, body, parser::parse_expr)}
Expand All @@ -181,10 +169,19 @@ fn parse_item(p: parser) -> @ast::item {
}
}

fn expand_qquote<N: qq_helper>
(ecx: ext_ctxt, sp: span, str: str, node: N)
fn finish<T: qq_helper>
(ecx: ext_ctxt, body: ast::mac_body_, f: fn (p: parser) -> T)
-> @ast::expr
{
let cm = ecx.session().parse_sess.cm;
let str = @codemap::span_to_snippet(body.span, cm);
let fname = codemap::mk_substr_filename(cm, body.span);
let node = parse_from_source_str
(f, fname, codemap::fss_internal(body.span), str,
ecx.session().opts.cfg, ecx.session().parse_sess);
let loc = codemap::lookup_char_pos(cm, body.span.lo);

let sp = node.span();
let qcx = gather_anti_quotes(sp.lo, node);
let cx = qcx;
let prev = 0u;
Expand All @@ -197,7 +194,7 @@ fn expand_qquote<N: qq_helper>
let state = active;
let i = 0u, j = 0u;
let g_len = vec::len(cx.gather);
str::chars_iter(str) {|ch|
str::chars_iter(*str) {|ch|
if (j < g_len && i == cx.gather[j].lo) {
assert ch == '$';
let repl = #fmt("$%u ", j);
Expand Down Expand Up @@ -227,8 +224,12 @@ fn expand_qquote<N: qq_helper>
["syntax", "parse", "parser",
"parse_from_source_str"],
[node.mk_parse_fn(cx,sp),
mk_str(cx,sp, "<anon>"),
mk_path(cx,sp, ["syntax", "codemap", "fss_none"]),
mk_str(cx,sp, fname),
mk_call(cx,sp,
["syntax","ext","qquote", "mk_file_substr"],
[mk_str(cx,sp, loc.file.name),
mk_uint(cx,sp, loc.line),
mk_uint(cx,sp, loc.col)]),
mk_unary(cx,sp, ast::box(ast::imm),
mk_str(cx,sp, str2)),
mk_access_(cx,sp,
Expand Down Expand Up @@ -305,6 +306,10 @@ fn print_expr(expr: @ast::expr) {
stdout.write_str("\n");
}

fn mk_file_substr(fname: str, line: uint, col: uint) -> codemap::file_substr {
codemap::fss_external({filename: fname, line: line, col: col})
}

// Local Variables:
// mode: rust
// fill-column: 78;
Expand Down

0 comments on commit 3791947

Please sign in to comment.