/
codemap.rs
214 lines (185 loc) · 6.64 KB
/
codemap.rs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
type filename = str;
type file_pos = {ch: uint, byte: uint};
/* 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.
*/
enum file_substr {
fss_none,
fss_internal(span),
fss_external({filename: str, line: uint, col: uint})
}
type filemap =
@{name: filename, substr: file_substr, src: @str,
start_pos: file_pos, mutable lines: [file_pos]};
type codemap = @{mutable files: [filemap]};
type loc = {file: filemap, line: uint, col: uint};
fn new_codemap() -> codemap { @{mutable files: [] } }
fn new_filemap_w_substr(filename: filename, substr: file_substr,
src: @str,
start_pos_ch: uint, start_pos_byte: uint)
-> filemap {
ret @{name: filename, substr: substr, src: src,
start_pos: {ch: start_pos_ch, byte: start_pos_byte},
mutable lines: [{ch: start_pos_ch, byte: start_pos_byte}]};
}
fn new_filemap(filename: filename, src: @str,
start_pos_ch: uint, start_pos_byte: uint)
-> filemap {
ret new_filemap_w_substr(filename, fss_none, src,
start_pos_ch, start_pos_byte);
}
fn mk_substr_filename(cm: codemap, sp: span) -> str
{
let pos = lookup_char_pos(cm, sp.lo);
ret #fmt("<%s:%u:%u>", pos.file.name, pos.line, pos.col);
}
fn next_line(file: filemap, chpos: uint, byte_pos: uint) {
file.lines += [{ch: chpos, byte: byte_pos}];
}
type lookup_fn = fn@(file_pos) -> uint;
fn lookup_line(map: codemap, pos: uint, lookup: lookup_fn)
-> {fm: filemap, line: uint}
{
let len = vec::len(map.files);
let a = 0u;
let b = len;
while b - a > 1u {
let m = (a + b) / 2u;
if lookup(map.files[m].start_pos) > pos { b = m; } else { a = m; }
}
if (a >= len) {
fail #fmt("position %u does not resolve to a source location", pos)
}
let f = map.files[a];
a = 0u;
b = vec::len(f.lines);
while b - a > 1u {
let m = (a + b) / 2u;
if lookup(f.lines[m]) > pos { b = m; } else { a = m; }
}
ret {fm: f, line: a};
}
fn lookup_pos(map: codemap, pos: uint, lookup: lookup_fn) -> loc {
let {fm: f, line: a} = lookup_line(map, pos, lookup);
ret {file: f, line: a + 1u, col: pos - lookup(f.lines[a])};
}
fn lookup_char_pos(map: codemap, pos: uint) -> loc {
fn lookup(pos: file_pos) -> uint { ret pos.ch; }
ret lookup_pos(map, pos, lookup);
}
fn lookup_byte_pos(map: codemap, pos: uint) -> loc {
fn lookup(pos: file_pos) -> uint { ret pos.byte; }
ret lookup_pos(map, pos, lookup);
}
fn lookup_char_pos_adj(map: codemap, pos: uint)
-> {filename: str, line: uint, col: uint, file: option<filemap>}
{
let loc = lookup_char_pos(map, pos);
alt (loc.file.substr) {
fss_none {
{filename: loc.file.name, line: loc.line, col: loc.col,
file: some(loc.file)}
}
fss_internal(sp) {
lookup_char_pos_adj(map, sp.lo + (pos - loc.file.start_pos.ch))
}
fss_external(eloc) {
{filename: eloc.filename,
line: eloc.line + loc.line - 1u,
col: if loc.line == 1u {eloc.col + loc.col} else {loc.col},
file: none}
}
}
}
fn adjust_span(map: codemap, sp: span) -> span {
fn lookup(pos: file_pos) -> uint { ret pos.ch; }
let line = lookup_line(map, sp.lo, lookup);
alt (line.fm.substr) {
fss_none {sp}
fss_internal(s) {
adjust_span(map, {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})}
fss_external(_) {sp}
}
}
enum expn_info_ {
expanded_from({call_site: span,
callie: {name: str, span: option<span>}})
}
type expn_info = option<@expn_info_>;
type span = {lo: uint, hi: uint, expn_info: expn_info};
fn span_to_str_no_adj(sp: span, cm: codemap) -> str {
let lo = lookup_char_pos(cm, sp.lo);
let hi = lookup_char_pos(cm, sp.hi);
ret #fmt("%s:%u:%u: %u:%u", lo.file.name,
lo.line, lo.col, hi.line, hi.col)
}
fn span_to_str(sp: span, cm: codemap) -> str {
let lo = lookup_char_pos_adj(cm, sp.lo);
let hi = lookup_char_pos_adj(cm, sp.hi);
ret #fmt("%s:%u:%u: %u:%u", lo.filename,
lo.line, lo.col, hi.line, hi.col)
}
type file_lines = {file: filemap, lines: [uint]};
fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
let lo = lookup_char_pos(cm, sp.lo);
let hi = lookup_char_pos(cm, sp.hi);
let lines = [];
uint::range(lo.line - 1u, hi.line as uint) {|i| lines += [i]; };
ret @{file: lo.file, lines: lines};
}
fn get_line(fm: filemap, line: int) -> str unsafe {
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
let end: uint;
if line as uint < vec::len(fm.lines) - 1u {
end = fm.lines[line + 1].byte - fm.start_pos.byte;
} else {
// If we're not done parsing the file, we're at the limit of what's
// parsed. If we just slice the rest of the string, we'll print out
// the remainder of the file, which is undesirable.
end = str::byte_len(*fm.src);
let rest = str::unsafe::slice_bytes(*fm.src, begin, end);
let newline = str::index(rest, '\n' as u8);
if newline != -1 { end = begin + (newline as uint); }
}
ret str::unsafe::slice_bytes(*fm.src, begin, end);
}
fn lookup_byte_offset(cm: codemap::codemap, chpos: uint)
-> {fm: filemap, pos: uint}
{
fn lookup(pos: file_pos) -> uint { ret pos.ch; }
let {fm,line} = lookup_line(cm,chpos,lookup);
let line_offset = fm.lines[line].byte - fm.start_pos.byte;
let col = chpos - fm.lines[line].ch;
let col_offset = str::byte_len_range(*fm.src, line_offset, col);
ret {fm: fm, pos: line_offset + col_offset};
}
fn span_to_snippet(sp: span, cm: codemap::codemap) -> str {
let begin = lookup_byte_offset(cm,sp.lo);
let end = lookup_byte_offset(cm,sp.hi);
assert begin.fm == end.fm;
ret str::slice(*begin.fm.src, begin.pos, end.pos);
}
fn get_snippet(cm: codemap::codemap, fidx: uint, lo: uint, hi: uint) -> str
{
let fm = cm.files[fidx];
ret str::slice(*fm.src, lo, hi)
}
fn get_filemap(cm: codemap, filename: str) -> filemap {
for fm: filemap in cm.files { if fm.name == filename { ret fm; } }
//XXjdm the following triggers a mismatched type bug
// (or expected function, found _|_)
fail; // ("asking for " + filename + " which we don't know about");
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//