mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Objectify the codemap
This commit is contained in:
parent
15a5d2ccbf
commit
2ec09c4eb9
@ -354,7 +354,7 @@ fn pretty_print_input(sess: Session, cfg: ast::crate_cfg, input: input,
|
||||
ppm_expanded | ppm_normal => pprust::no_ann()
|
||||
};
|
||||
let is_expanded = upto != cu_parse;
|
||||
let src = codemap::get_filemap(sess.codemap, source_name(input)).src;
|
||||
let src = sess.codemap.get_filemap(source_name(input)).src;
|
||||
do io::with_str_reader(*src) |rdr| {
|
||||
pprust::print_crate(sess.codemap, sess.parse_sess.interner,
|
||||
sess.span_diagnostic, crate,
|
||||
@ -574,7 +574,7 @@ fn build_session_options(binary: ~str,
|
||||
|
||||
fn build_session(sopts: @session::options,
|
||||
demitter: diagnostic::emitter) -> Session {
|
||||
let codemap = @codemap::new_codemap();
|
||||
let codemap = @codemap::CodeMap::new();
|
||||
let diagnostic_handler =
|
||||
diagnostic::mk_handler(Some(demitter));
|
||||
let span_diagnostic_handler =
|
||||
|
@ -557,7 +557,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
|
||||
let add_to_index = |copy ebml_w| add_to_index_(item, ebml_w, index);
|
||||
|
||||
debug!("encoding info for item at %s",
|
||||
syntax::codemap::span_to_str(item.span, ecx.tcx.sess.codemap));
|
||||
ecx.tcx.sess.codemap.span_to_str(item.span));
|
||||
|
||||
match item.node {
|
||||
item_const(_, _) => {
|
||||
|
@ -97,7 +97,7 @@ use std::map::HashMap;
|
||||
use syntax::{visit, ast_util};
|
||||
use syntax::print::pprust::{expr_to_str, block_to_str};
|
||||
use visit::vt;
|
||||
use syntax::codemap::{span, span_to_str};
|
||||
use syntax::codemap::span;
|
||||
use syntax::ast::*;
|
||||
use io::WriterUtil;
|
||||
use capture::{cap_move, cap_drop, cap_copy, cap_ref};
|
||||
@ -170,9 +170,9 @@ impl LiveNodeKind : cmp::Eq {
|
||||
fn live_node_kind_to_str(lnk: LiveNodeKind, cx: ty::ctxt) -> ~str {
|
||||
let cm = cx.sess.codemap;
|
||||
match lnk {
|
||||
FreeVarNode(s) => fmt!("Free var node [%s]", span_to_str(s, cm)),
|
||||
ExprNode(s) => fmt!("Expr node [%s]", span_to_str(s, cm)),
|
||||
VarDefNode(s) => fmt!("Var def node [%s]", span_to_str(s, cm)),
|
||||
FreeVarNode(s) => fmt!("Free var node [%s]", cm.span_to_str(s)),
|
||||
ExprNode(s) => fmt!("Expr node [%s]", cm.span_to_str(s)),
|
||||
VarDefNode(s) => fmt!("Var def node [%s]", cm.span_to_str(s)),
|
||||
ExitNode => ~"Exit node"
|
||||
}
|
||||
}
|
||||
|
@ -919,7 +919,7 @@ fn trans_trace(bcx: block, sp_opt: Option<span>, trace_str: ~str) {
|
||||
let {V_filename, V_line} = match sp_opt {
|
||||
Some(sp) => {
|
||||
let sess = bcx.sess();
|
||||
let loc = codemap::lookup_char_pos(sess.parse_sess.cm, sp.lo);
|
||||
let loc = sess.parse_sess.cm.lookup_char_pos(sp.lo);
|
||||
{V_filename: C_cstr(bcx.ccx(), loc.file.name),
|
||||
V_line: loc.line as int}
|
||||
}
|
||||
|
@ -645,7 +645,7 @@ fn _UndefReturn(cx: block, Fn: ValueRef) -> ValueRef {
|
||||
fn add_span_comment(bcx: block, sp: span, text: ~str) {
|
||||
let ccx = bcx.ccx();
|
||||
if !ccx.sess.no_asm_comments() {
|
||||
let s = text + ~" (" + codemap::span_to_str(sp, ccx.sess.codemap)
|
||||
let s = text + ~" (" + ccx.sess.codemap.span_to_str(sp)
|
||||
+ ~")";
|
||||
log(debug, s);
|
||||
add_comment(bcx, s);
|
||||
|
@ -339,7 +339,7 @@ fn trans_fail_value(bcx: block, sp_opt: Option<span>, V_fail_str: ValueRef)
|
||||
let {V_filename, V_line} = match sp_opt {
|
||||
Some(sp) => {
|
||||
let sess = bcx.sess();
|
||||
let loc = codemap::lookup_char_pos(sess.parse_sess.cm, sp.lo);
|
||||
let loc = sess.parse_sess.cm.lookup_char_pos(sp.lo);
|
||||
{V_filename: C_cstr(bcx.ccx(), loc.file.name),
|
||||
V_line: loc.line as int}
|
||||
}
|
||||
@ -361,7 +361,7 @@ fn trans_fail_bounds_check(bcx: block, sp: span,
|
||||
let _icx = bcx.insn_ctxt("trans_fail_bounds_check");
|
||||
let ccx = bcx.ccx();
|
||||
|
||||
let loc = codemap::lookup_char_pos(bcx.sess().parse_sess.cm, sp.lo);
|
||||
let loc = bcx.sess().parse_sess.cm.lookup_char_pos(sp.lo);
|
||||
let line = C_int(ccx, loc.line as int);
|
||||
let filename_cstr = C_cstr(bcx.ccx(), loc.file.name);
|
||||
let filename = PointerCast(bcx, filename_cstr, T_ptr(T_i8()));
|
||||
|
@ -230,7 +230,7 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata<file_md> {
|
||||
}
|
||||
|
||||
fn line_from_span(cm: @codemap::CodeMap, sp: span) -> uint {
|
||||
codemap::lookup_char_pos(cm, sp.lo).line
|
||||
cm.lookup_char_pos(sp.lo).line
|
||||
}
|
||||
|
||||
fn create_block(cx: block) -> @metadata<block_md> {
|
||||
@ -244,9 +244,9 @@ fn create_block(cx: block) -> @metadata<block_md> {
|
||||
}
|
||||
let sp = cx.node_info.get().span;
|
||||
|
||||
let start = codemap::lookup_char_pos(cx.sess().codemap, sp.lo);
|
||||
let start = cx.sess().codemap.lookup_char_pos(sp.lo);
|
||||
let fname = start.file.name;
|
||||
let end = codemap::lookup_char_pos(cx.sess().codemap, sp.hi);
|
||||
let end = cx.sess().codemap.lookup_char_pos(sp.hi);
|
||||
let tg = LexicalBlockTag;
|
||||
/*alt cached_metadata::<@metadata<block_md>>(
|
||||
cache, tg,
|
||||
@ -597,7 +597,7 @@ fn create_ty(_cx: @crate_ctxt, _t: ty::t, _ty: @ast::Ty)
|
||||
}
|
||||
|
||||
fn filename_from_span(cx: @crate_ctxt, sp: codemap::span) -> ~str {
|
||||
codemap::lookup_char_pos(cx.sess.codemap, sp.lo).file.name
|
||||
cx.sess.codemap.lookup_char_pos(sp.lo).file.name
|
||||
}
|
||||
|
||||
fn create_var(type_tag: int, context: ValueRef, name: ~str, file: ValueRef,
|
||||
@ -629,8 +629,7 @@ fn create_local_var(bcx: block, local: @ast::local)
|
||||
// FIXME this should be handled (#2533)
|
||||
_ => fail ~"no single variable name for local"
|
||||
};
|
||||
let loc = codemap::lookup_char_pos(cx.sess.codemap,
|
||||
local.span.lo);
|
||||
let loc = cx.sess.codemap.lookup_char_pos(local.span.lo);
|
||||
let ty = node_id_type(bcx, local.node.id);
|
||||
let tymd = create_ty(cx, ty, local.node.ty);
|
||||
let filemd = create_file(cx, loc.file.name);
|
||||
@ -674,8 +673,7 @@ fn create_arg(bcx: block, arg: ast::arg, sp: span)
|
||||
option::None => ()
|
||||
}
|
||||
|
||||
let loc = codemap::lookup_char_pos(cx.sess.codemap,
|
||||
sp.lo);
|
||||
let loc = cx.sess.codemap.lookup_char_pos(sp.lo);
|
||||
let ty = node_id_type(bcx, arg.id);
|
||||
let tymd = create_ty(cx, ty, arg.ty);
|
||||
let filemd = create_file(cx, loc.file.name);
|
||||
@ -714,7 +712,7 @@ fn update_source_pos(cx: block, s: span) {
|
||||
}
|
||||
let cm = cx.sess().codemap;
|
||||
let blockmd = create_block(cx);
|
||||
let loc = codemap::lookup_char_pos(cm, s.lo);
|
||||
let loc = cm.lookup_char_pos(s.lo);
|
||||
let scopedata = ~[lli32(loc.line as int),
|
||||
lli32(loc.col as int),
|
||||
blockmd.node,
|
||||
@ -731,7 +729,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
|
||||
log(debug, fcx.id);
|
||||
|
||||
let sp = fcx.span.get();
|
||||
log(debug, codemap::span_to_str(sp, cx.sess.codemap));
|
||||
log(debug, cx.sess.codemap.span_to_str(sp));
|
||||
|
||||
let (ident, ret_ty, id) = match cx.tcx.items.get(fcx.id) {
|
||||
ast_map::node_item(item, _) => {
|
||||
@ -773,8 +771,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
|
||||
option::None => ()
|
||||
}
|
||||
|
||||
let loc = codemap::lookup_char_pos(cx.sess.codemap,
|
||||
sp.lo);
|
||||
let loc = cx.sess.codemap.lookup_char_pos(sp.lo);
|
||||
let file_node = create_file(cx, loc.file.name).node;
|
||||
let ty_node = if cx.sess.opts.extra_debuginfo {
|
||||
match ret_ty.node {
|
||||
|
@ -507,7 +507,7 @@ impl RegionVarBindings {
|
||||
self.undo_log.push(AddVar(vid));
|
||||
}
|
||||
debug!("created new region variable %? with span %?",
|
||||
vid, codemap::span_to_str(span, self.tcx.sess.codemap));
|
||||
vid, self.tcx.sess.codemap.span_to_str(span));
|
||||
return vid;
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ fn explain_region_and_span(cx: ctxt, region: ty::Region)
|
||||
fn explain_span(cx: ctxt, heading: ~str, span: span)
|
||||
-> (~str, Option<span>)
|
||||
{
|
||||
let lo = codemap::lookup_char_pos_adj(cx.sess.codemap, span.lo);
|
||||
let lo = cx.sess.codemap.lookup_char_pos_adj(span.lo);
|
||||
(fmt!("the %s at %u:%u", heading, lo.line, lo.col), Some(span))
|
||||
}
|
||||
}
|
||||
@ -131,17 +131,17 @@ fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str {
|
||||
match cx.items.find(node_id) {
|
||||
Some(ast_map::node_block(blk)) => {
|
||||
fmt!("<block at %s>",
|
||||
codemap::span_to_str(blk.span, cx.sess.codemap))
|
||||
cx.sess.codemap.span_to_str(blk.span))
|
||||
}
|
||||
Some(ast_map::node_expr(expr)) => {
|
||||
match expr.node {
|
||||
ast::expr_call(*) => {
|
||||
fmt!("<call at %s>",
|
||||
codemap::span_to_str(expr.span, cx.sess.codemap))
|
||||
cx.sess.codemap.span_to_str(expr.span))
|
||||
}
|
||||
ast::expr_match(*) => {
|
||||
fmt!("<alt at %s>",
|
||||
codemap::span_to_str(expr.span, cx.sess.codemap))
|
||||
cx.sess.codemap.span_to_str(expr.span))
|
||||
}
|
||||
ast::expr_assign_op(*) |
|
||||
ast::expr_field(*) |
|
||||
@ -149,11 +149,11 @@ fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str {
|
||||
ast::expr_binary(*) |
|
||||
ast::expr_index(*) => {
|
||||
fmt!("<method at %s>",
|
||||
codemap::span_to_str(expr.span, cx.sess.codemap))
|
||||
cx.sess.codemap.span_to_str(expr.span))
|
||||
}
|
||||
_ => {
|
||||
fmt!("<expression at %s>",
|
||||
codemap::span_to_str(expr.span, cx.sess.codemap))
|
||||
cx.sess.codemap.span_to_str(expr.span))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ fn build_ctxt(sess: Session,
|
||||
|
||||
fn build_session() -> Session {
|
||||
let sopts: @options = basic_options();
|
||||
let codemap = @codemap::new_codemap();
|
||||
let codemap = @codemap::CodeMap::new();
|
||||
let error_handlers = build_error_handlers(codemap);
|
||||
let {emitter, span_handler} = error_handlers;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
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 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 struct CodeMap {
|
||||
files: DVec<@filemap>
|
||||
}
|
||||
|
||||
pub fn new_codemap() -> CodeMap {
|
||||
CodeMap {
|
||||
files: DVec()
|
||||
pub impl CodeMap {
|
||||
static pub fn new() -> CodeMap {
|
||||
CodeMap {
|
||||
files: DVec()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
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; }
|
||||
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);
|
||||
}
|
||||
if (a >= len) {
|
||||
fail fmt!("position %u does not resolve to a source location", pos)
|
||||
|
||||
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);
|
||||
}
|
||||
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 lookup_byte_pos(@self, pos: uint) -> loc {
|
||||
pure fn lookup(pos: file_pos) -> uint { return pos.byte; }
|
||||
return self.lookup_pos(pos, lookup);
|
||||
}
|
||||
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(map: @CodeMap, pos: uint) -> loc {
|
||||
pure fn lookup(pos: file_pos) -> uint { return pos.ch; }
|
||||
return lookup_pos(map, 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(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 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 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 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 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)
|
||||
}
|
||||
|
||||
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_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};
|
||||
}
|
||||
|
||||
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_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 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_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 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)
|
||||
}
|
||||
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 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)
|
||||
}
|
||||
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 struct file_lines {
|
||||
file: @filemap,
|
||||
lines: ~[uint]
|
||||
}
|
||||
|
||||
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_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_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)
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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),
|
||||
|
@ -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);
|
||||
|
@ -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 &&
|
||||
|
Loading…
Reference in New Issue
Block a user