2012-11-12 18:45:24 -06:00
|
|
|
/*! A codemap is a thing that maps uints to file/line/column positions
|
|
|
|
* in a crate. This to make it possible to represent the positions
|
|
|
|
* with single-word things, rather than passing records all over the
|
|
|
|
* compiler.
|
|
|
|
*/
|
|
|
|
|
2012-09-04 13:37:29 -05:00
|
|
|
use dvec::DVec;
|
2012-10-18 15:29:34 -05:00
|
|
|
use std::serialization::{Serializable,
|
|
|
|
Deserializable,
|
|
|
|
Serializer,
|
|
|
|
Deserializer};
|
2012-05-15 15:40:18 -05:00
|
|
|
|
2012-04-15 06:34:51 -05:00
|
|
|
export filename;
|
|
|
|
export filemap;
|
2012-11-12 17:12:20 -06:00
|
|
|
export filemap_;
|
2012-04-15 06:34:51 -05:00
|
|
|
export span;
|
|
|
|
export file_substr;
|
2012-05-22 12:54:12 -05:00
|
|
|
export fss_none;
|
|
|
|
export fss_internal;
|
|
|
|
export fss_external;
|
2012-10-15 16:56:42 -05:00
|
|
|
export CodeMap;
|
2012-04-15 06:34:51 -05:00
|
|
|
export expn_info;
|
|
|
|
export expn_info_;
|
|
|
|
export expanded_from;
|
|
|
|
export new_filemap;
|
|
|
|
export new_filemap_w_substr;
|
|
|
|
export mk_substr_filename;
|
|
|
|
export lookup_char_pos;
|
2012-05-22 19:27:20 -05:00
|
|
|
export lookup_char_pos_adj;
|
2012-04-15 06:34:51 -05:00
|
|
|
export adjust_span;
|
|
|
|
export span_to_str;
|
2012-05-10 19:18:04 -05:00
|
|
|
export span_to_filename;
|
2012-04-15 06:34:51 -05:00
|
|
|
export span_to_lines;
|
|
|
|
export file_lines;
|
|
|
|
export get_line;
|
|
|
|
export next_line;
|
|
|
|
export span_to_snippet;
|
|
|
|
export loc;
|
|
|
|
export get_filemap;
|
|
|
|
export new_codemap;
|
|
|
|
|
2012-11-12 19:14:15 -06:00
|
|
|
struct span {lo: uint, hi: uint, expn_info: expn_info}
|
|
|
|
|
|
|
|
impl span : cmp::Eq {
|
|
|
|
pure fn eq(other: &span) -> bool {
|
|
|
|
return self.lo == (*other).lo && self.hi == (*other).hi;
|
|
|
|
}
|
|
|
|
pure fn ne(other: &span) -> bool { !self.eq(other) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Serializer> span: Serializable<S> {
|
|
|
|
/* Note #1972 -- spans are serialized but not deserialized */
|
|
|
|
fn serialize(&self, _s: &S) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Deserializer> span: Deserializable<D> {
|
|
|
|
static fn deserialize(_d: &D) -> span {
|
|
|
|
ast_util::dummy_sp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum expn_info_ {
|
|
|
|
expanded_from({call_site: span,
|
|
|
|
callie: {name: ~str, span: Option<span>}})
|
|
|
|
}
|
|
|
|
type expn_info = Option<@expn_info_>;
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
type filename = ~str;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-11-12 19:14:15 -06:00
|
|
|
type lookup_fn = pure fn(file_pos) -> uint;
|
|
|
|
|
|
|
|
struct loc {
|
|
|
|
file: @filemap, line: uint, col: uint
|
|
|
|
}
|
|
|
|
|
2012-11-12 17:12:20 -06:00
|
|
|
struct file_pos {
|
|
|
|
ch: uint, byte: uint
|
|
|
|
}
|
2011-07-16 01:01:10 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl file_pos : cmp::Eq {
|
|
|
|
pure fn eq(other: &file_pos) -> bool {
|
|
|
|
self.ch == (*other).ch && self.byte == (*other).byte
|
|
|
|
}
|
|
|
|
pure fn ne(other: &file_pos) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-02-10 04:03:21 -06:00
|
|
|
enum file_substr {
|
|
|
|
fss_none,
|
|
|
|
fss_internal(span),
|
2012-07-14 00:57:48 -05:00
|
|
|
fss_external({filename: ~str, line: uint, col: uint})
|
2012-02-10 04:03:21 -06:00
|
|
|
}
|
2012-02-01 04:37:53 -06:00
|
|
|
|
2012-11-12 18:41:20 -06:00
|
|
|
struct filemap {
|
2012-11-12 18:47:17 -06:00
|
|
|
name: filename,
|
|
|
|
substr: file_substr,
|
|
|
|
src: @~str,
|
|
|
|
start_pos: file_pos,
|
|
|
|
mut lines: ~[file_pos]
|
2012-11-12 17:12:20 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:44:53 -06:00
|
|
|
impl filemap {
|
|
|
|
static fn new_w_substr(+filename: filename, +substr: file_substr,
|
|
|
|
src: @~str,
|
|
|
|
start_pos_ch: uint, start_pos_byte: uint)
|
|
|
|
-> filemap {
|
|
|
|
return filemap {
|
|
|
|
name: filename, substr: substr, src: src,
|
|
|
|
start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte},
|
|
|
|
mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn new(+filename: filename, src: @~str,
|
|
|
|
start_pos_ch: uint, start_pos_byte: uint)
|
|
|
|
-> filemap {
|
|
|
|
return filemap::new_w_substr(filename, fss_none, src,
|
|
|
|
start_pos_ch, start_pos_byte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
struct CodeMap {
|
2012-11-12 18:41:20 -06:00
|
|
|
files: DVec<@filemap>
|
2012-11-12 17:12:20 -06:00
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn new_codemap() -> CodeMap {
|
|
|
|
CodeMap {
|
|
|
|
files: DVec()
|
|
|
|
}
|
|
|
|
}
|
2011-07-16 01:01:10 -05:00
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn mk_substr_filename(cm: @CodeMap, sp: span) -> ~str
|
2012-02-01 04:37:53 -06:00
|
|
|
{
|
2012-02-10 04:03:21 -06:00
|
|
|
let pos = lookup_char_pos(cm, sp.lo);
|
2012-08-22 19:24:52 -05:00
|
|
|
return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
|
2012-02-01 04:37:53 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:41:20 -06:00
|
|
|
fn next_line(file: @filemap, chpos: uint, byte_pos: uint) {
|
2012-11-12 17:12:20 -06:00
|
|
|
file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte});
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_line(map: @CodeMap, pos: uint, lookup: lookup_fn)
|
2012-11-12 18:41:20 -06:00
|
|
|
-> {fm: @filemap, line: uint}
|
2012-01-25 15:22:10 -06:00
|
|
|
{
|
2012-05-15 15:40:18 -05:00
|
|
|
let len = map.files.len();
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut a = 0u;
|
|
|
|
let mut b = len;
|
2011-07-27 07:19:39 -05:00
|
|
|
while b - a > 1u {
|
|
|
|
let m = (a + b) / 2u;
|
2011-08-19 17:16:48 -05:00
|
|
|
if lookup(map.files[m].start_pos) > pos { b = m; } else { a = m; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2012-01-21 03:00:06 -06:00
|
|
|
if (a >= len) {
|
2012-08-22 19:24:52 -05:00
|
|
|
fail fmt!("position %u does not resolve to a source location", pos)
|
2012-01-21 03:00:06 -06:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
let f = map.files[a];
|
2011-07-05 04:48:19 -05:00
|
|
|
a = 0u;
|
2011-08-15 18:38:23 -05:00
|
|
|
b = vec::len(f.lines);
|
2011-07-27 07:19:39 -05:00
|
|
|
while b - a > 1u {
|
|
|
|
let m = (a + b) / 2u;
|
2011-08-19 17:16:48 -05:00
|
|
|
if lookup(f.lines[m]) > pos { b = m; } else { a = m; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return {fm: f, line: a};
|
2012-01-25 15:22:10 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_pos(map: @CodeMap, pos: uint, lookup: lookup_fn) -> loc {
|
2012-02-03 19:39:39 -06:00
|
|
|
let {fm: f, line: a} = lookup_line(map, pos, lookup);
|
2012-11-12 17:12:20 -06:00
|
|
|
return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])};
|
2011-07-16 01:01:10 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_char_pos(map: @CodeMap, pos: uint) -> loc {
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
|
|
|
|
return lookup_pos(map, pos, lookup);
|
2011-07-16 01:01:10 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_byte_pos(map: @CodeMap, pos: uint) -> loc {
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn lookup(pos: file_pos) -> uint { return pos.byte; }
|
|
|
|
return lookup_pos(map, pos, lookup);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_char_pos_adj(map: @CodeMap, pos: uint)
|
2012-11-12 18:41:20 -06:00
|
|
|
-> {filename: ~str, line: uint, col: uint, file: Option<@filemap>}
|
2012-02-10 12:28:43 -06:00
|
|
|
{
|
|
|
|
let loc = lookup_char_pos(map, pos);
|
2012-08-06 14:34:08 -05:00
|
|
|
match (loc.file.substr) {
|
2012-08-03 21:59:04 -05:00
|
|
|
fss_none => {
|
2012-06-21 18:44:10 -05:00
|
|
|
{filename: /* FIXME (#2543) */ copy loc.file.name,
|
2012-06-07 22:12:05 -05:00
|
|
|
line: loc.line,
|
|
|
|
col: loc.col,
|
2012-08-20 14:23:37 -05:00
|
|
|
file: Some(loc.file)}
|
2012-02-10 12:28:43 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
fss_internal(sp) => {
|
2012-02-10 12:28:43 -06:00
|
|
|
lookup_char_pos_adj(map, sp.lo + (pos - loc.file.start_pos.ch))
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
fss_external(eloc) => {
|
2012-06-21 18:44:10 -05:00
|
|
|
{filename: /* FIXME (#2543) */ copy eloc.filename,
|
2012-02-10 12:28:43 -06:00
|
|
|
line: eloc.line + loc.line - 1u,
|
|
|
|
col: if loc.line == 1u {eloc.col + loc.col} else {loc.col},
|
2012-08-20 14:23:37 -05:00
|
|
|
file: None}
|
2012-02-10 12:28:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn adjust_span(map: @CodeMap, sp: span) -> span {
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
|
2012-02-10 12:28:43 -06:00
|
|
|
let line = lookup_line(map, sp.lo, lookup);
|
2012-08-06 14:34:08 -05:00
|
|
|
match (line.fm.substr) {
|
2012-08-03 21:59:04 -05:00
|
|
|
fss_none => sp,
|
|
|
|
fss_internal(s) => {
|
2012-11-12 17:12:20 -06:00
|
|
|
adjust_span(map, span {lo: s.lo + (sp.lo - line.fm.start_pos.ch),
|
|
|
|
hi: s.lo + (sp.hi - line.fm.start_pos.ch),
|
|
|
|
expn_info: sp.expn_info})}
|
2012-08-03 21:59:04 -05:00
|
|
|
fss_external(_) => sp
|
2012-02-10 12:28:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn span_to_str_no_adj(sp: span, cm: @CodeMap) -> ~str {
|
2012-02-04 19:37:24 -06:00
|
|
|
let lo = lookup_char_pos(cm, sp.lo);
|
|
|
|
let hi = lookup_char_pos(cm, sp.hi);
|
2012-08-22 19:24:52 -05:00
|
|
|
return fmt!("%s:%u:%u: %u:%u", lo.file.name,
|
|
|
|
lo.line, lo.col, hi.line, hi.col)
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn span_to_str(sp: span, cm: @CodeMap) -> ~str {
|
2012-02-10 12:28:43 -06:00
|
|
|
let lo = lookup_char_pos_adj(cm, sp.lo);
|
|
|
|
let hi = lookup_char_pos_adj(cm, sp.hi);
|
2012-08-22 19:24:52 -05:00
|
|
|
return fmt!("%s:%u:%u: %u:%u", lo.filename,
|
|
|
|
lo.line, lo.col, hi.line, hi.col)
|
2012-02-10 12:28:43 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 17:12:20 -06:00
|
|
|
struct file_lines {
|
2012-11-12 18:41:20 -06:00
|
|
|
file: @filemap,
|
2012-11-12 17:12:20 -06:00
|
|
|
lines: ~[uint]
|
|
|
|
}
|
2011-07-11 01:27:03 -05:00
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn span_to_filename(sp: span, cm: @codemap::CodeMap) -> filename {
|
2012-05-10 19:18:04 -05:00
|
|
|
let lo = lookup_char_pos(cm, sp.lo);
|
2012-08-01 19:30:05 -05:00
|
|
|
return /* FIXME (#2543) */ copy lo.file.name;
|
2012-05-10 19:18:04 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn span_to_lines(sp: span, cm: @codemap::CodeMap) -> @file_lines {
|
2011-07-27 07:19:39 -05:00
|
|
|
let lo = lookup_char_pos(cm, sp.lo);
|
|
|
|
let hi = lookup_char_pos(cm, sp.hi);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut lines = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(lo.line - 1u, hi.line as uint) |i| {
|
2012-09-26 19:33:34 -05:00
|
|
|
lines.push(i);
|
2012-06-26 02:39:18 -05:00
|
|
|
};
|
2012-11-12 17:12:20 -06:00
|
|
|
return @file_lines {file: lo.file, lines: lines};
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:41:20 -06:00
|
|
|
fn get_line(fm: @filemap, line: int) -> ~str unsafe {
|
2011-08-19 17:16:48 -05:00
|
|
|
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
|
2012-08-06 14:34:08 -05:00
|
|
|
let end = match str::find_char_from(*fm.src, '\n', begin) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(e) => e,
|
|
|
|
None => str::len(*fm.src)
|
2012-02-15 03:19:51 -06:00
|
|
|
};
|
2012-02-23 02:45:25 -06:00
|
|
|
str::slice(*fm.src, begin, end)
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn lookup_byte_offset(cm: @codemap::CodeMap, chpos: uint)
|
2012-11-12 18:41:20 -06:00
|
|
|
-> {fm: @filemap, pos: uint} {
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
|
2012-06-02 22:50:21 -05:00
|
|
|
let {fm, line} = lookup_line(cm, chpos, lookup);
|
2012-02-01 02:35:11 -06:00
|
|
|
let line_offset = fm.lines[line].byte - fm.start_pos.byte;
|
2012-01-25 15:22:10 -06:00
|
|
|
let col = chpos - fm.lines[line].ch;
|
2012-02-23 09:59:30 -06:00
|
|
|
let col_offset = str::count_bytes(*fm.src, line_offset, col);
|
|
|
|
{fm: fm, pos: line_offset + col_offset}
|
2012-01-25 15:22:10 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn span_to_snippet(sp: span, cm: @codemap::CodeMap) -> ~str {
|
2012-02-23 06:41:10 -06:00
|
|
|
let begin = lookup_byte_offset(cm, sp.lo);
|
|
|
|
let end = lookup_byte_offset(cm, sp.hi);
|
2012-08-27 18:26:35 -05:00
|
|
|
assert begin.fm.start_pos == end.fm.start_pos;
|
2012-08-01 19:30:05 -05:00
|
|
|
return str::slice(*begin.fm.src, begin.pos, end.pos);
|
2012-01-25 15:22:10 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn get_snippet(cm: @codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str
|
2012-01-25 15:22:10 -06:00
|
|
|
{
|
|
|
|
let fm = cm.files[fidx];
|
2012-08-01 19:30:05 -05:00
|
|
|
return str::slice(*fm.src, lo, hi)
|
2012-01-25 15:22:10 -06:00
|
|
|
}
|
|
|
|
|
2012-11-12 18:56:39 -06:00
|
|
|
fn get_filemap(cm: @CodeMap, filename: ~str) -> @filemap {
|
2012-09-19 18:55:01 -05:00
|
|
|
for cm.files.each |fm| { if fm.name == filename { return *fm; } }
|
2011-07-11 01:27:03 -05:00
|
|
|
//XXjdm the following triggers a mismatched type bug
|
|
|
|
// (or expected function, found _|_)
|
2011-07-27 07:19:39 -05:00
|
|
|
fail; // ("asking for " + filename + " which we don't know about");
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2012-01-26 03:52:08 -06:00
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|