Skip to content

Commit d808df8

Browse files
committed
Use file_substr rather than <anon> when re-parsing quasi-quotes
for better error messages.
1 parent 4026053 commit d808df8

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/comp/syntax/codemap.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,10 @@ fn new_filemap(filename: filename, src: @str,
4040
start_pos_ch, start_pos_byte);
4141
}
4242

43-
fn get_substr_info(cm: codemap, sp: span)
44-
-> (filename, file_substr)
43+
fn mk_substr_filename(cm: codemap, sp: span) -> str
4544
{
4645
let pos = lookup_char_pos(cm, sp.lo);
47-
let name = #fmt("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
48-
ret (name, fss_internal(sp));
49-
//ret (name, fss_external({filename: pos.file.name,
50-
// line: pos.line, col: pos.col}));
46+
ret #fmt("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
5147
}
5248

5349
fn next_line(file: filemap, chpos: uint, byte_pos: uint) {

src/comp/syntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import std::map::hashmap;
55
import syntax::ast::{crate, expr_, expr_mac, mac_invoc};
66
import syntax::fold::*;
77
import syntax::ext::base::*;
8-
import syntax::ext::qquote::{expand_qquote,qq_helper};
8+
import syntax::ext::qquote::{qq_helper};
99
import syntax::parse::parser::parse_expr_from_source_str;
1010

1111
import codemap::{span, expanded_from};

src/comp/syntax/ext/qquote.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,6 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
143143
}
144144
}
145145
let body = get_mac_body(ecx,_sp,body);
146-
fn finish<T: qq_helper>(ecx: ext_ctxt, body: ast::mac_body_,
147-
f: fn (p: parser) -> T)
148-
-> @ast::expr
149-
{
150-
let cm = ecx.session().parse_sess.cm;
151-
let str = @codemap::span_to_snippet(body.span, cm);
152-
let (fname, ss) = codemap::get_substr_info(cm, body.span);
153-
let node = parse_from_source_str
154-
(f, fname, ss, str,
155-
ecx.session().opts.cfg, ecx.session().parse_sess);
156-
ret expand_qquote(ecx, node.span(), *str, node);
157-
}
158146

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

184-
fn expand_qquote<N: qq_helper>
185-
(ecx: ext_ctxt, sp: span, str: str, node: N)
172+
fn finish<T: qq_helper>
173+
(ecx: ext_ctxt, body: ast::mac_body_, f: fn (p: parser) -> T)
186174
-> @ast::expr
187175
{
176+
let cm = ecx.session().parse_sess.cm;
177+
let str = @codemap::span_to_snippet(body.span, cm);
178+
let fname = codemap::mk_substr_filename(cm, body.span);
179+
let node = parse_from_source_str
180+
(f, fname, codemap::fss_internal(body.span), str,
181+
ecx.session().opts.cfg, ecx.session().parse_sess);
182+
let loc = codemap::lookup_char_pos(cm, body.span.lo);
183+
184+
let sp = node.span();
188185
let qcx = gather_anti_quotes(sp.lo, node);
189186
let cx = qcx;
190187
let prev = 0u;
@@ -197,7 +194,7 @@ fn expand_qquote<N: qq_helper>
197194
let state = active;
198195
let i = 0u, j = 0u;
199196
let g_len = vec::len(cx.gather);
200-
str::chars_iter(str) {|ch|
197+
str::chars_iter(*str) {|ch|
201198
if (j < g_len && i == cx.gather[j].lo) {
202199
assert ch == '$';
203200
let repl = #fmt("$%u ", j);
@@ -227,8 +224,12 @@ fn expand_qquote<N: qq_helper>
227224
["syntax", "parse", "parser",
228225
"parse_from_source_str"],
229226
[node.mk_parse_fn(cx,sp),
230-
mk_str(cx,sp, "<anon>"),
231-
mk_path(cx,sp, ["syntax", "codemap", "fss_none"]),
227+
mk_str(cx,sp, fname),
228+
mk_call(cx,sp,
229+
["syntax","ext","qquote", "mk_file_substr"],
230+
[mk_str(cx,sp, loc.file.name),
231+
mk_uint(cx,sp, loc.line),
232+
mk_uint(cx,sp, loc.col)]),
232233
mk_unary(cx,sp, ast::box(ast::imm),
233234
mk_str(cx,sp, str2)),
234235
mk_access_(cx,sp,
@@ -305,6 +306,10 @@ fn print_expr(expr: @ast::expr) {
305306
stdout.write_str("\n");
306307
}
307308

309+
fn mk_file_substr(fname: str, line: uint, col: uint) -> codemap::file_substr {
310+
codemap::fss_external({filename: fname, line: line, col: col})
311+
}
312+
308313
// Local Variables:
309314
// mode: rust
310315
// fill-column: 78;

0 commit comments

Comments
 (0)