forked from dobkeratops/rustfind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codemaput.rs
403 lines (356 loc) · 13 KB
/
codemaput.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use rf_common::*;
use rustfindctx::RustFindCtx;
use std::from_str::FromStr;
use syntax::ast;
use syntax::codemap;
use syntax::codemap::Pos;
use rustc::middle::ty;
use util::text_offset_to_line_pos;//todo - why is qualifying manually not working?!
// TODO:
// we've done many permuations of how to represent code position,
// 'codemap.rs' uses Loc { file:@FileMap, line:uint /* one based*/ col: CharPos
// we use zero-based indices, and a file index requiring caller passes a ty::cx around
// and a version where we propogate a filename instead.
//
// should we simplify out by reusing the codemap version ?
// or keep going with code here that is more indexed / non shared-ptr based?
//
// also codemap.rs has 'span' which contains extra info , out use of span is really a subset
// Extents<BytePos> could replace it here ?
pub macro_rules! if_some {
($b:ident in $a:expr then $c:expr)=>(
match $a {
Some($b) => $c,
None => {}
}
);
}
//pub type ZeroBasedIndex=uint;
pub struct ZTextFilePos {
pub name: StrBuf,
pub line: u32,
pub col: u32
}
pub trait ToZTextFilePos {
fn to_text_file_pos(self, cx: &ty::ctxt) -> Option<ZTextFilePos>;
}
impl ZIndexFilePos {
pub fn to_scalar(&self) -> u64 {
// TODO - safety assert
(self.file_index as u64 << 48) | (self.line as u64 << 23) | (self.col as u64)
}
}
impl Eq for ZIndexFilePos {
fn eq(&self, other:&ZIndexFilePos)->bool { self.file_index==other.file_index && self.line==other.line && self.col==other.col }
fn ne(&self, other:&ZIndexFilePos)->bool { self.file_index!=other.file_index || self.line!=other.line || self.col!=other.col}
}
impl Ord for ZIndexFilePos {
// todo: as fixed width bignum? or int64 from int32 components?
fn lt(&self, other: &ZIndexFilePos) -> bool {
self.to_scalar() < other.to_scalar()
}
fn gt(&self, other: &ZIndexFilePos) -> bool {
self.to_scalar() > other.to_scalar()
}
fn le(&self, other: &ZIndexFilePos) -> bool {
self.to_scalar() <= other.to_scalar()
}
fn ge(&self, other: &ZIndexFilePos) -> bool {
self.to_scalar() >= other.to_scalar()
}
}
impl ToZTextFilePos for codemap::BytePos {
fn to_text_file_pos(self, cx: &ty::ctxt) -> Option<ZTextFilePos> {
let files = cx.sess.codemap().files.borrow();
let mut i = files.len();
while i > 0 {
i -= 1;
let fm = &files.get(i);
if fm.start_pos <= self {
let lines = fm.lines.borrow();
let mut line = lines.len() as u32;
while line > 0 {
line -= 1;
let line_start = lines.get(line as uint);
if line_start <= &self {
return Some(ZTextFilePos::new(fm.name.to_owned(), line, (self-*line_start).to_uint() as u32))
}
}
}
}
None
}
}
impl FromStr for ZTextFilePos {
fn from_str(file_pos_str: &str) -> Option<ZTextFilePos> {
let toks: Vec<&str> = file_pos_str.split(':').collect();
if toks.len() <= 0 {
None
} else if toks.len() == 1 {
Some(ZTextFilePos::new(*toks.get(0), 0, 0))
} else {
match from_str::<u32>(*toks.get(1)) {
None => None,
Some(editor_line_number) => match FromStr::from_str(*toks.get(2)) {
None => Some(ZTextFilePos::new(*toks.get(0), editor_line_number - 1, 0)),
Some(col) => Some(ZTextFilePos::new(*toks.get(0), editor_line_number, col))
}
}
}
}
}
impl ZTextFilePos {
pub fn new(filename: &str, _line: u32, _col: u32) -> ZTextFilePos {
ZTextFilePos {name: filename.to_strbuf(), line: _line, col: _col}
}
pub fn to_str(&self) -> StrBuf {
StrBuf::new().append(self.name.as_slice()).append(":")
.append( (self.line + 1).to_str().as_slice())
.append(":")
.append(self.col.to_str().as_slice())
.append(":")
}
pub fn to_byte_pos(&self, tc: &ty::ctxt) -> Option<codemap::BytePos> {
let files = tc.sess.codemap().files.borrow();
let mut i = files.len();
while i > 0 { // caution, need loop because we return, wait for new foreach ..in..
i -= 1;
let fm = &files.get(i);
let filemap_filename: &str = fm.name.as_slice();
if filemap_filename == self.name.as_slice() {
let lines = fm.lines.borrow();
if self.line as uint >= lines.len() {
return None;
}
return Some(codemap::BytePos(lines.get(self.line as uint).to_uint() as u32 + self.col));
}
}
return None;
}
pub fn to_byte_pos_len(&self, tc: &ty::ctxt, len: u32)
-> Option<(codemap::BytePos, codemap::BytePos)> {
match self.to_byte_pos(tc) {
None => None,
Some(lo) => {
// hmm, we dont know about clipping TODO: check its in range? clamp it?
// is it line length, or length in file?
Some((lo, lo + codemap::BytePos(len)))
}
}
}
pub fn get_str_at(&self, tc: &ty::ctxt, len: u32) -> StrBuf {
let a = self.to_byte_pos_len(tc, len);
match a {
Some((bp_lo, bp_hi)) => get_span_str(tc,
&codemap::Span {lo: bp_lo, hi: bp_hi, expn_info: None}),
None => StrBuf::from_str("")
}
}
}
pub struct ZTextFilePosLen {
pub tfp: ZTextFilePos,
pub len: u32
}
impl ZTextFilePosLen {
pub fn new(file_name: &str, _line: u32, _col: u32, _len: u32) -> ZTextFilePosLen {
ZTextFilePosLen{ tfp: ZTextFilePos::new(file_name, _line, _col), len: _len }
}
pub fn to_byte_pos(&self, tc: &ty::ctxt) -> Option<(codemap::BytePos, codemap::BytePos)> {
// text_file_pos_len_to_byte_pos(tc,&self.tfp, self.len)
self.tfp.to_byte_pos_len(tc, self.len)
}
pub fn get_str(&self, tc: &ty::ctxt) -> StrBuf {
self.tfp.get_str_at(tc, self.len)
}
}
pub fn get_span_str(tc :&ty::ctxt, sp: &codemap::Span) -> StrBuf {
let loc_lo = tc.sess.codemap().lookup_char_pos(sp.lo);
// TODO-assert both in same file!
let file_org = loc_lo.file.start_pos;
let slice = loc_lo.file.src.as_slice().slice((sp.lo - file_org).to_uint(), (sp.hi - file_org).to_uint());
slice.to_strbuf()
}
// fn get_str_at_text_file_pos_len(cx:ty::ctxt, tfp:&ZTextFilePos,len:uint)->StrBuf {
//}
/*
fn text_file_pos_len_to_byte_pos(c:ty::ctxt,tfp:&ZTextFilePos,len:uint=0 )->Option<(codemap::BytePos,codemap
::BytePos)>
{
// for c.sess.codemap.files.rev_iter().advance |fm:&codemap::FileMap| {
let mut i=c.sess.codemap.files.len();
while i>0 { // caution, need loop because we return, wait for new foreach ..in..
i-=1;
let fm=&c.sess.codemap.files[i];
let filemap_filename:&str=fm.name;
if filemap_filename==tfp.name {
let line_pos=*fm.lines[tfp.line];
let bp_start=*fm.lines[tfp.line]+tfp.col;
let bp_end=(bp_start+len).min(&(*fm.start_pos+fm.src.len()));
return Some((codemap::BytePos(bp_start), codemap::BytePos(bp_end)))
}
}
return None;
}
*/
/*
pub fn byte_pos_to_text_file_pos(c:ty::ctxt, pos:codemap::BytePos)->Option<ZTextFilePos> {
// TODO: cleanup with byte_pos_to_index_file_pos, one in terms of the other.
// TODO - functional, and with binary search or something ..
let mut i=c.sess.codemap.files.len();
while i>0 {
// caution, need loop because we return, wait for new foreach ..in..
i-=1;
let fm=&c.sess.codemap.files[i];
let filemap_filename:&str=fm.name;
if *pos >= *fm.start_pos && *pos < *fm.start_pos+fm.src.len(){
let mut line=fm.lines.len();
while line>0 {
line-=1;
let lstart=*fm.lines[line];
if lstart < *pos {
return Some(ZTextFilePos::new(fm.name, line, *pos-lstart))
}
}
}
}
None
// TextFilePos::new(c.sess.codemap.files[0].name,0,0)
}
*/
pub struct ZIndexFilePos {
pub file_index: u32,
pub line: u32,
pub col: u32
}
pub trait ToZIndexFilePos {
fn to_index_file_pos(&self, c: &ty::ctxt) -> Option<ZIndexFilePos>;
}
impl ToZIndexFilePos for codemap::BytePos {
fn to_index_file_pos(&self, c: &ty::ctxt) -> Option<ZIndexFilePos> {
// TODO: cleanup with byte_pos_to_text_file_pos, one in terms of the other.
// TODO - functional, and with binary search or something ..
let files = c.sess.codemap().files.borrow();
let mut i = files.len() as u32;
while i > 0 {
// caution, need loop because we return, wait for new foreach ..in..
i -= 1;
let fm = &files.get(i as uint);
if *self >= fm.start_pos && self.to_uint() < fm.start_pos.to_uint() + fm.src.len() {
let lines = fm.lines.borrow();
let mut line = lines.len() as u32;
while line > 0 {
line -= 1;
let lstart = lines.get(line as uint);
if lstart < self {
return Some(
ZIndexFilePos{ file_index: i, line: line, col: (*self - *lstart).to_uint() as u32});
}
}
}
}
None
}
}
pub fn get_crate_name(tc: &ty::ctxt, i: ast::CrateNum) -> StrBuf {
if i > 0 {
let cd = tc.sess.cstore.get_crate_data(i);
cd.name.to_strbuf()
} else {
StrBuf::from_str("")
}
}
pub fn text_span<'a, 'b>(text: &'a [u8], s: &'b codemap::Span) -> &'a[u8] {
text.slice(s.lo.to_uint(), s.hi.to_uint())
}
pub fn dump_cstore_info(tc: &ty::ctxt) {
//struct ctxt_ {
// cstore: @mut metadata::cstore::CStore,
// def_map: resolve::DefMap,
// tc.cstore.
// home/walter/gplsrc/rust/src/librustc/metadata/cstore.rs:37:
//pub struct CStore {
// priv metas: HashMap <ast::CrateNum, @crate_metadata>,
// priv extern_mod_crate_map: extern_mod_crate_map,
// priv used_crate_files: ~[Path],
// priv used_libraries: ~[@str],
// priv used_link_args: ~[@str],
// intr: @ident_interner
//}
// home/walter/gplsrc/rust/src/librustc/metadata/cstore.rs:30:
//pub struct crate_metadata {
// name: @str,
// data: @~[u8],
// cnum_map: cnum_map,
// cnum: ast::CrateNum
//}
//println!("crate files");
//let ucf = tc.cstore.get_used_crate_source();
//for x in ucf.iter() {
// dump!(x);
//}
/* println!("crate metadata");
for i in range(1,num_crates) {
let cd= cstore::get_crate_data(tc.cstore, i as int);
dump!(i, cd.name, cd.data.len(), cd.cnum_map, cd.cnum);
}
*/
println!("crate metadata");
tc.sess.cstore.iter_crate_data(|i,md| {
dump!(i, md.name, md.data.as_slice().len(), md.cnum);
});
}
/*
pub fn flatten_to_str<T,U:ToStr>(xs:&[T],f:&fn(x:&T)->U, sep:&str)->StrBuf {
let mut acc=~"";
let mut i=0; // TODO - functional way.
while i<xs.len() {
if i>0 {acc.push_str(sep);}
acc.push_str( f(&xs[i]).to_str() );
i+=1;
}
acc
}
*/
pub fn loc_to_str(loc:codemap::Loc) -> StrBuf {
StrBuf::from_str(loc.file.name.as_slice()).append(":").append(loc.line.to_str().as_slice()).append(":").append( loc.col.to_uint().to_str().as_slice() ).append( ":" )
}
pub fn zget_file_line_str(_: &ty::ctxt, _: &str, _: u32) -> StrBuf {
// for c.sess.codemap.files.rev_iter().advance |fm:&codemap::FileMap| {
// let mut i = cx.sess.codemap.files.len();
// while i > 0 { // caution, need loop because we return, wait for new foreach ..in..
// i -= 1;
// let fm = &cx.sess.codemap.files[i];
// let filemap_filename: &str = fm.name;
// if filename == filemap_filename {
// let s = *fm.lines[src_line];
// let e = if (src_line + 1) as uint >= fm.lines.len() {
// *fm.start_pos + fm.src.len() as u32
// } else {
// *fm.lines[src_line + 1]
// };
// }
// }
return StrBuf::from_str("");
}
pub fn dump_span(text: &[u8], sp: &codemap::Span) {
let line_col = text_offset_to_line_pos(text, sp.lo.to_uint() as u32);
logi!(" line,ofs=", line_col.to_str(), " text=\'", str::from_utf8(text_span(text,sp)),"\'");
}
pub fn byte_pos_from_text_file_pos_str(dc:&RustFindCtx,filepos:&str)->Option<codemap::BytePos> {
let toks=filepos.split(':').collect::<Vec<_> >();
if toks.len()<3 { return None; }
// let t0:()=toks[0];
// let line:Option<uint> = FromStr::from_str(toks[1]);
// if_some!(line in FromStr::from_str(toks[1]) then {
// if_some!(col in FromStr::from_str(toks[2]) then {
let line: Option<u32> = from_str(*toks.get(1));
let col:Option<u32> = from_str(*toks.get(2));
if line.is_some() && col.is_some() {
//todo - if no column specified, just lookup everything on that line!
let l0 = line.unwrap()-1;
let c0= col.unwrap()-1;
let foo= ZTextFilePos::new(*toks.get(0),l0,c0).to_byte_pos(dc.tycx_ref());
return foo;
}
return None;
}