diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-11-12 18:24:56 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-11-12 18:24:56 -0800 |
| commit | 2ec09c4eb91b94bd68c95eaa8966d4801c3347bf (patch) | |
| tree | 74966da5517e5ea005bda5429ff9462f20f4abf3 /src/libsyntax | |
| parent | 15a5d2ccbf2de7ac0bb4894a6885791fa1f23521 (diff) | |
| download | rust-2ec09c4eb91b94bd68c95eaa8966d4801c3347bf.tar.gz rust-2ec09c4eb91b94bd68c95eaa8966d4801c3347bf.zip | |
Objectify the codemap
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/codemap.rs | 283 | ||||
| -rw-r--r-- | src/libsyntax/diagnostic.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/ext/qquote.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/source_util.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/lexer.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 4 |
7 files changed, 167 insertions, 162 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 6a0e8063119..f204cf89e1b 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -58,6 +58,11 @@ impl file_pos : cmp::Eq { pure fn ne(other: &file_pos) -> bool { !self.eq(other) } } +pub struct file_lines { + file: @filemap, + lines: ~[uint] +} + pub enum file_substr { pub fss_none, pub fss_internal(span), @@ -90,173 +95,173 @@ pub impl filemap { return filemap::new_w_substr(filename, fss_none, src, start_pos_ch, start_pos_byte); } -} -pub struct CodeMap { - files: DVec<@filemap> -} + fn next_line(@self, chpos: uint, byte_pos: uint) { + self.lines.push(file_pos {ch: chpos, byte: byte_pos + self.start_pos.byte}); + } -pub fn new_codemap() -> CodeMap { - CodeMap { - files: DVec() + pub fn get_line(@self, line: int) -> ~str unsafe { + let begin: uint = self.lines[line].byte - self.start_pos.byte; + let end = match str::find_char_from(*self.src, '\n', begin) { + Some(e) => e, + None => str::len(*self.src) + }; + str::slice(*self.src, begin, end) } -} -pub fn mk_substr_filename(cm: @CodeMap, sp: span) -> ~str -{ - let pos = lookup_char_pos(cm, sp.lo); - return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); } -pub fn next_line(file: @filemap, chpos: uint, byte_pos: uint) { - file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte}); +pub struct CodeMap { + files: DVec<@filemap> } -fn lookup_line(map: @CodeMap, pos: uint, lookup: lookup_fn) - -> {fm: @filemap, line: uint} -{ - let len = map.files.len(); - let mut a = 0u; - let mut 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) +pub impl CodeMap { + static pub fn new() -> CodeMap { + CodeMap { + files: DVec() + } } - 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; } + + pub fn mk_substr_filename(@self, sp: span) -> ~str { + let pos = self.lookup_char_pos(sp.lo); + return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); } - return {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); - return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; -} + pub fn lookup_char_pos(@self, pos: uint) -> loc { + pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + return self.lookup_pos(pos, lookup); + } -pub fn lookup_char_pos(map: @CodeMap, pos: uint) -> loc { - pure fn lookup(pos: file_pos) -> uint { return pos.ch; } - return lookup_pos(map, pos, lookup); -} + pub fn lookup_byte_pos(@self, pos: uint) -> loc { + pure fn lookup(pos: file_pos) -> uint { return pos.byte; } + return self.lookup_pos(pos, lookup); + } -fn lookup_byte_pos(map: @CodeMap, pos: uint) -> loc { - pure fn lookup(pos: file_pos) -> uint { return pos.byte; } - return lookup_pos(map, pos, lookup); -} + pub fn lookup_char_pos_adj(@self, pos: uint) + -> {filename: ~str, line: uint, col: uint, file: Option<@filemap>} + { + let loc = self.lookup_char_pos(pos); + match (loc.file.substr) { + fss_none => { + {filename: /* FIXME (#2543) */ copy loc.file.name, + line: loc.line, + col: loc.col, + file: Some(loc.file)} + } + fss_internal(sp) => { + self.lookup_char_pos_adj(sp.lo + (pos - loc.file.start_pos.ch)) + } + fss_external(eloc) => { + {filename: /* FIXME (#2543) */ copy eloc.filename, + line: eloc.line + loc.line - 1u, + col: if loc.line == 1u {eloc.col + loc.col} else {loc.col}, + file: None} + } + } + } -pub 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); - match (loc.file.substr) { - fss_none => { - {filename: /* FIXME (#2543) */ copy 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: /* FIXME (#2543) */ copy eloc.filename, - line: eloc.line + loc.line - 1u, - col: if loc.line == 1u {eloc.col + loc.col} else {loc.col}, - file: None} - } + pub fn adjust_span(@self, sp: span) -> span { + pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + let line = self.lookup_line(sp.lo, lookup); + match (line.fm.substr) { + fss_none => sp, + fss_internal(s) => { + self.adjust_span(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})} + fss_external(_) => sp + } } -} -pub fn adjust_span(map: @CodeMap, sp: span) -> span { - pure fn lookup(pos: file_pos) -> uint { return pos.ch; } - let line = lookup_line(map, sp.lo, lookup); - match (line.fm.substr) { - fss_none => sp, - fss_internal(s) => { - 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})} - fss_external(_) => sp + pub fn span_to_str(@self, sp: span) -> ~str { + let lo = self.lookup_char_pos_adj(sp.lo); + let hi = self.lookup_char_pos_adj(sp.hi); + return fmt!("%s:%u:%u: %u:%u", lo.filename, + lo.line, lo.col, hi.line, hi.col) } -} -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); - return fmt!("%s:%u:%u: %u:%u", lo.file.name, - lo.line, lo.col, hi.line, hi.col) -} + pub fn span_to_filename(@self, sp: span) -> filename { + let lo = self.lookup_char_pos(sp.lo); + return /* FIXME (#2543) */ copy lo.file.name; + } -pub 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); - return fmt!("%s:%u:%u: %u:%u", lo.filename, - lo.line, lo.col, hi.line, hi.col) -} + pub fn span_to_lines(@self, sp: span) -> @file_lines { + let lo = self.lookup_char_pos(sp.lo); + let hi = self.lookup_char_pos(sp.hi); + let mut lines = ~[]; + for uint::range(lo.line - 1u, hi.line as uint) |i| { + lines.push(i); + }; + return @file_lines {file: lo.file, lines: lines}; + } -pub struct file_lines { - file: @filemap, - lines: ~[uint] -} + fn lookup_byte_offset(@self, chpos: uint) + -> {fm: @filemap, pos: uint} { + pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + let {fm, line} = self.lookup_line(chpos, lookup); + let line_offset = fm.lines[line].byte - fm.start_pos.byte; + let col = chpos - fm.lines[line].ch; + let col_offset = str::count_bytes(*fm.src, line_offset, col); + {fm: fm, pos: line_offset + col_offset} + } -pub fn span_to_filename(sp: span, cm: @codemap::CodeMap) -> filename { - let lo = lookup_char_pos(cm, sp.lo); - return /* FIXME (#2543) */ copy lo.file.name; -} + pub fn span_to_snippet(@self, sp: span) -> ~str { + let begin = self.lookup_byte_offset(sp.lo); + let end = self.lookup_byte_offset(sp.hi); + assert begin.fm.start_pos == end.fm.start_pos; + return str::slice(*begin.fm.src, begin.pos, end.pos); + } -pub 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 mut lines = ~[]; - for uint::range(lo.line - 1u, hi.line as uint) |i| { - lines.push(i); - }; - return @file_lines {file: lo.file, lines: lines}; -} + pub fn get_snippet(@self, fidx: uint, lo: uint, hi: uint) -> ~str + { + let fm = self.files[fidx]; + return str::slice(*fm.src, lo, hi) + } -pub fn get_line(fm: @filemap, line: int) -> ~str unsafe { - let begin: uint = fm.lines[line].byte - fm.start_pos.byte; - let end = match str::find_char_from(*fm.src, '\n', begin) { - Some(e) => e, - None => str::len(*fm.src) - }; - str::slice(*fm.src, begin, end) -} + pub fn get_filemap(@self, filename: ~str) -> @filemap { + for self.files.each |fm| { if fm.name == filename { return *fm; } } + //XXjdm the following triggers a mismatched type bug + // (or expected function, found _|_) + fail; // ("asking for " + filename + " which we don't know about"); + } -fn lookup_byte_offset(cm: @codemap::CodeMap, chpos: uint) - -> {fm: @filemap, pos: uint} { - pure fn lookup(pos: file_pos) -> uint { return 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::count_bytes(*fm.src, line_offset, col); - {fm: fm, pos: line_offset + col_offset} } -pub 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.start_pos == end.fm.start_pos; - return str::slice(*begin.fm.src, begin.pos, end.pos); -} +priv impl CodeMap { + fn lookup_line(@self, pos: uint, lookup: lookup_fn) + -> {fm: @filemap, line: uint} + { + let len = self.files.len(); + let mut a = 0u; + let mut b = len; + while b - a > 1u { + let m = (a + b) / 2u; + if lookup(self.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 = self.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; } + } + return {fm: f, line: a}; + } -pub fn get_snippet(cm: @codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str -{ - let fm = cm.files[fidx]; - return str::slice(*fm.src, lo, hi) -} + fn lookup_pos(@self, pos: uint, lookup: lookup_fn) -> loc { + let {fm: f, line: a} = self.lookup_line(pos, lookup); + return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; + } -pub fn get_filemap(cm: @CodeMap, filename: ~str) -> @filemap { - for cm.files.each |fm| { if fm.name == filename { return *fm; } } - //XXjdm the following triggers a mismatched type bug - // (or expected function, found _|_) - fail; // ("asking for " + filename + " which we don't know about"); + fn span_to_str_no_adj(@self, sp: span) -> ~str { + let lo = self.lookup_char_pos(sp.lo); + let hi = self.lookup_char_pos(sp.hi); + return fmt!("%s:%u:%u: %u:%u", lo.file.name, + lo.line, lo.col, hi.line, hi.col) + } } // diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 9335faed0c9..66c59683813 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -192,9 +192,9 @@ fn print_diagnostic(topic: ~str, lvl: level, msg: &str) { fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) { match cmsp { Some((cm, sp)) => { - let sp = codemap::adjust_span(cm,sp); - let ss = codemap::span_to_str(sp, cm); - let lines = codemap::span_to_lines(sp, cm); + let sp = cm.adjust_span(sp); + let ss = cm.span_to_str(sp); + let lines = cm.span_to_lines(sp); print_diagnostic(ss, lvl, msg); highlight_lines(cm, sp, lines); print_macro_backtrace(cm, sp); @@ -221,7 +221,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, // Print the offending lines for display_lines.each |line| { io::stderr().write_str(fmt!("%s:%u ", fm.name, *line + 1u)); - let s = codemap::get_line(fm, *line as int) + ~"\n"; + let s = fm.get_line(*line as int) + ~"\n"; io::stderr().write_str(s); } if elided { @@ -237,7 +237,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, // If there's one line at fault we can easily point to the problem if vec::len(lines.lines) == 1u { - let lo = codemap::lookup_char_pos(cm, sp.lo); + let lo = cm.lookup_char_pos(sp.lo); let mut digits = 0u; let mut num = (lines.lines[0] + 1u) / 10u; @@ -250,7 +250,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, while left > 0u { str::push_char(&mut s, ' '); left -= 1u; } s += ~"^"; - let hi = codemap::lookup_char_pos(cm, sp.hi); + let hi = cm.lookup_char_pos(sp.hi); if hi.col != lo.col { // the ^ already takes up one space let mut width = hi.col - lo.col - 1u; @@ -263,10 +263,10 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, fn print_macro_backtrace(cm: @codemap::CodeMap, sp: span) { do option::iter(&sp.expn_info) |ei| { let ss = option::map_default(&ei.callie.span, @~"", - |span| @codemap::span_to_str(*span, cm)); + |span| @cm.span_to_str(*span)); print_diagnostic(*ss, note, fmt!("in expansion of %s!", ei.callie.name)); - let ss = codemap::span_to_str(ei.call_site, cm); + let ss = cm.span_to_str(ei.call_site); print_diagnostic(ss, note, ~"expansion site"); print_macro_backtrace(cm, ei.call_site); } diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index af7ffaa73f5..6ae083779cd 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -204,13 +204,13 @@ fn finish<T: qq_helper> -> @ast::expr { let cm = ecx.codemap(); - let str = @codemap::span_to_snippet(body.span, cm); + let str = @cm.span_to_snippet(body.span); debug!("qquote--str==%?", str); - let fname = codemap::mk_substr_filename(cm, body.span); + let fname = cm.mk_substr_filename(body.span); let node = parse_from_source_str (f, fname, codemap::fss_internal(body.span), str, ecx.cfg(), ecx.parse_sess()); - let loc = codemap::lookup_char_pos(cm, body.span.lo); + let loc = cm.lookup_char_pos(body.span.lo); let sp = node.span(); let qcx = gather_anti_quotes(sp.lo, node); diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 3181a604400..8c5048b4c6f 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -16,7 +16,7 @@ export expand_include_bin; fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"line"); - let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); + let loc = cx.codemap().lookup_char_pos(sp.lo); return mk_uint(cx, sp, loc.line); } @@ -24,7 +24,7 @@ fn expand_line(cx: ext_ctxt, sp: span, arg: ast::mac_arg, fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"col"); - let loc = codemap::lookup_char_pos(cx.codemap(), sp.lo); + let loc = cx.codemap().lookup_char_pos(sp.lo); return mk_uint(cx, sp, loc.col); } @@ -35,7 +35,7 @@ fn expand_file(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { get_mac_args(cx, sp, arg, 0u, option::Some(0u), ~"file"); let loc { file: @filemap { name: filename, _ }, _ } = - codemap::lookup_char_pos(cx.codemap(), sp.lo); + cx.codemap().lookup_char_pos(sp.lo); return mk_uniq_str(cx, sp, filename); } @@ -103,7 +103,7 @@ fn expand_include_bin(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, fn res_rel_file(cx: ext_ctxt, sp: codemap::span, arg: &Path) -> Path { // NB: relative paths are resolved relative to the compilation unit if !arg.is_absolute { - let cu = Path(codemap::span_to_filename(sp, cx.codemap())); + let cu = Path(cx.codemap().span_to_filename(sp)); cu.dir_path().push_many(arg.components) } else { copy *arg diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index f93be9c7266..3f15a5b6e88 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -20,7 +20,7 @@ use util::interner; use diagnostic::{span_handler, mk_span_handler, mk_handler, emitter}; use lexer::{reader, string_reader}; use parse::token::{ident_interner, mk_ident_interner}; -use codemap::filemap; +use codemap::{CodeMap, filemap}; type parse_sess = @{ cm: @codemap::CodeMap, @@ -33,7 +33,7 @@ type parse_sess = @{ }; fn new_parse_sess(demitter: Option<emitter>) -> parse_sess { - let cm = @codemap::new_codemap(); + let cm = @CodeMap::new(); return @{cm: cm, mut next_id: 1, span_diagnostic: mk_span_handler(mk_handler(demitter), cm), diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index f3e84a0cbf8..1c984ad57b1 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -1,5 +1,5 @@ use diagnostic::span_handler; -use codemap::span; +use codemap::{span, CodeMap}; use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, tt_next_token}; @@ -135,7 +135,7 @@ fn bump(rdr: string_reader) { rdr.col += 1u; rdr.chpos += 1u; if rdr.curr == '\n' { - codemap::next_line(rdr.filemap, rdr.chpos, rdr.pos); + rdr.filemap.next_line(rdr.chpos, rdr.pos); rdr.col = 0u; } let next = str::char_range_at(*rdr.src, rdr.pos); @@ -232,9 +232,9 @@ fn consume_any_line_comment(rdr: string_reader) } } else if rdr.curr == '#' { if nextch(rdr) == '!' { - let cmap = @codemap::new_codemap(); + let cmap = @CodeMap::new(); (*cmap).files.push(rdr.filemap); - let loc = codemap::lookup_char_pos_adj(cmap, rdr.chpos); + let loc = cmap.lookup_char_pos_adj(rdr.chpos); if loc.line == 1u && loc.col == 0u { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } return consume_whitespace_and_comments(rdr); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 2bf9b826d6f..8fa7543b2d1 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1904,8 +1904,8 @@ fn maybe_print_trailing_comment(s: ps, span: codemap::span, match next_comment(s) { Some(cmnt) => { if cmnt.style != comments::trailing { return; } - let span_line = codemap::lookup_char_pos(cm, span.hi); - let comment_line = codemap::lookup_char_pos(cm, cmnt.pos); + let span_line = cm.lookup_char_pos(span.hi); + let comment_line = cm.lookup_char_pos(cmnt.pos); let mut next = cmnt.pos + 1u; match next_pos { None => (), Some(p) => next = p } if span.hi < cmnt.pos && cmnt.pos < next && |
