From 2374154ded19a247be0053d71f5bcdd22b95462a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 14:10:17 -0800 Subject: [PATCH] Stop storing char positions in CodeMap --- src/libsyntax/codemap.rs | 30 +++++++++++++++--------------- src/libsyntax/parse.rs | 19 ++++++------------- src/libsyntax/parse/comments.rs | 16 ++++++---------- src/libsyntax/parse/lexer.rs | 30 ++++++++++++------------------ 4 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 13f2ea4e210..5e600b48965 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -195,15 +195,15 @@ pub struct FileMap { name: FileName, substr: FileSubstr, src: @~str, - start_pos: FilePos, - mut lines: ~[FilePos], + start_pos: BytePos, + mut lines: ~[BytePos], multibyte_chars: DVec } pub impl FileMap { static fn new_w_substr(+filename: FileName, +substr: FileSubstr, src: @~str, - +start_pos: FilePos) + +start_pos: BytePos) -> FileMap { return FileMap { name: filename, substr: substr, src: src, @@ -214,18 +214,18 @@ pub impl FileMap { } static fn new(+filename: FileName, src: @~str, - +start_pos: FilePos) + +start_pos: BytePos) -> FileMap { return FileMap::new_w_substr(filename, FssNone, src, start_pos); } - fn next_line(&self, +pos: FilePos) { + fn next_line(&self, +pos: BytePos) { self.lines.push(pos); } pub fn get_line(&self, line: int) -> ~str unsafe { - let begin: BytePos = self.lines[line].byte - self.start_pos.byte; + let begin: BytePos = self.lines[line] - self.start_pos; let begin = begin.to_uint(); let end = match str::find_char_from(*self.src, '\n', begin) { Some(e) => e, @@ -266,11 +266,11 @@ pub impl CodeMap { let expected_byte_pos = if self.files.len() == 0 { 0 } else { - let last_start = self.files.last().start_pos.byte.to_uint(); + let last_start = self.files.last().start_pos.to_uint(); let last_len = self.files.last().src.len(); last_start + last_len }; - let actual_byte_pos = filemap.start_pos.byte.to_uint(); + let actual_byte_pos = filemap.start_pos.to_uint(); debug!("codemap: adding filemap: %s", filemap.name); debug!("codemap: expected offset: %u", expected_byte_pos); debug!("codemap: actual offset: %u", actual_byte_pos); @@ -301,7 +301,7 @@ pub impl CodeMap { } FssInternal(sp) => { self.lookup_char_pos_adj( - sp.lo + (pos - loc.file.start_pos.byte)) + sp.lo + (pos - loc.file.start_pos)) } FssExternal(eloc) => { {filename: /* FIXME (#2543) */ copy eloc.filename, @@ -318,8 +318,8 @@ pub impl CodeMap { FssNone => sp, FssInternal(s) => { self.adjust_span(span { - lo: s.lo + (sp.lo - line.fm.start_pos.byte), - hi: s.lo + (sp.hi - line.fm.start_pos.byte), + lo: s.lo + (sp.lo - line.fm.start_pos), + hi: s.lo + (sp.hi - line.fm.start_pos), expn_info: sp.expn_info }) } @@ -374,7 +374,7 @@ priv impl CodeMap { let mut b = len; while b - a > 1u { let m = (a + b) / 2u; - if self.files[m].start_pos.byte > pos { + if self.files[m].start_pos > pos { b = m; } else { a = m; @@ -397,7 +397,7 @@ priv impl CodeMap { let mut b = vec::len(f.lines); while b - a > 1u { let m = (a + b) / 2u; - if f.lines[m].byte > pos { b = m; } else { a = m; } + if f.lines[m] > pos { b = m; } else { a = m; } } return {fm: f, line: a}; } @@ -406,7 +406,7 @@ priv impl CodeMap { let {fm: f, line: a} = self.lookup_line(pos); let line = a + 1u; // Line numbers start at 1 let chpos = self.bytepos_to_local_charpos(pos); - let linebpos = f.lines[a].byte; + let linebpos = f.lines[a]; let linechpos = self.bytepos_to_local_charpos(linebpos); debug!("codemap: byte pos %? is on the line at byte pos %?", pos, linebpos); @@ -432,7 +432,7 @@ priv impl CodeMap { -> {fm: @FileMap, pos: BytePos} { let idx = self.lookup_filemap_idx(bpos); let fm = self.files[idx]; - let offset = bpos - fm.start_pos.byte; + let offset = bpos - fm.start_pos; return {fm: fm, pos: offset}; } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index c5ffbb2ab0a..9c8d28680ed 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -29,7 +29,7 @@ type parse_sess = @{ span_diagnostic: span_handler, interner: @ident_interner, // must be kept up to date - mut pos: FilePos + mut pos: BytePos }; fn new_parse_sess(demitter: Option) -> parse_sess { @@ -38,10 +38,8 @@ fn new_parse_sess(demitter: Option) -> parse_sess { mut next_id: 1, span_diagnostic: mk_span_handler(mk_handler(demitter), cm), interner: mk_ident_interner(), - mut pos: FilePos { - ch: CharPos(0u), - byte: BytePos(0u) - }}; + mut pos: BytePos(0) + }; } fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) @@ -50,10 +48,8 @@ fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) mut next_id: 1, span_diagnostic: sh, interner: mk_ident_interner(), - mut pos: FilePos { - ch: CharPos(0u), - byte: BytePos(0u) - }}; + mut pos: BytePos(0) + }; } fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, @@ -219,8 +215,5 @@ fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, } fn update_parse_sess_position(sess: &parse_sess, r: &lexer::string_reader) { - sess.pos = FilePos { - ch: r.last_pos.ch, - byte: r.last_pos.byte - }; + sess.pos = r.last_pos } diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 589a5f25ecf..e3ac3e7dcbb 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -131,7 +131,7 @@ fn consume_non_eol_whitespace(rdr: string_reader) { fn push_blank_line_comment(rdr: string_reader, comments: &mut ~[cmnt]) { debug!(">>> blank-line comment"); let v: ~[~str] = ~[]; - comments.push({style: blank_line, lines: v, pos: rdr.last_pos.byte}); + comments.push({style: blank_line, lines: v, pos: rdr.last_pos}); } fn consume_whitespace_counting_blank_lines(rdr: string_reader, @@ -148,7 +148,7 @@ fn consume_whitespace_counting_blank_lines(rdr: string_reader, fn read_shebang_comment(rdr: string_reader, code_to_the_left: bool, comments: &mut ~[cmnt]) { debug!(">>> shebang comment"); - let p = rdr.last_pos.byte; + let p = rdr.last_pos; debug!("<<< shebang comment"); comments.push({ style: if code_to_the_left { trailing } else { isolated }, @@ -160,7 +160,7 @@ fn read_shebang_comment(rdr: string_reader, code_to_the_left: bool, fn read_line_comments(rdr: string_reader, code_to_the_left: bool, comments: &mut ~[cmnt]) { debug!(">>> line comments"); - let p = rdr.last_pos.byte; + let p = rdr.last_pos; let mut lines: ~[~str] = ~[]; while rdr.curr == '/' && nextch(rdr) == '/' { let line = read_one_line_comment(rdr); @@ -209,7 +209,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut ~[~str], fn read_block_comment(rdr: string_reader, code_to_the_left: bool, comments: &mut ~[cmnt]) { debug!(">>> block comment"); - let p = rdr.last_pos.byte; + let p = rdr.last_pos; let mut lines: ~[~str] = ~[]; let mut col: CharPos = rdr.col; bump(rdr); @@ -292,11 +292,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, {cmnts: ~[cmnt], lits: ~[lit]} { let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); - let filemap = @FileMap::new(path, src, - FilePos { - ch: CharPos(0u), - byte: BytePos(0u) - }); + let filemap = @FileMap::new(path, src, BytePos(0)); let rdr = lexer::new_low_level_string_reader( span_diagnostic, filemap, itr); @@ -319,7 +315,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, } - let bstart = rdr.pos.byte; + let bstart = rdr.pos; rdr.next_token(); //discard, and look ahead; we're working with internal state let {tok: tok, sp: sp} = rdr.peek(); diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 00a0b40ab65..10a74d620a3 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -22,9 +22,9 @@ type string_reader = @{ span_diagnostic: span_handler, src: @~str, // The absolute offset within the codemap of the next character to read - mut pos: FilePos, + mut pos: BytePos, // The absolute offset within the codemap of the last character read(curr) - mut last_pos: FilePos, + mut last_pos: BytePos, // The column of the next character to read mut col: CharPos, // The last character to be read @@ -123,15 +123,15 @@ fn string_advance_token(&&r: string_reader) { if is_eof(r) { r.peek_tok = token::EOF; } else { - let start_bytepos = r.last_pos.byte; + let start_bytepos = r.last_pos; r.peek_tok = next_token_inner(r); - r.peek_span = ast_util::mk_sp(start_bytepos, r.last_pos.byte); + r.peek_span = ast_util::mk_sp(start_bytepos, r.last_pos); }; } fn byte_offset(rdr: string_reader) -> BytePos { - (rdr.pos.byte - rdr.filemap.start_pos.byte) + (rdr.pos - rdr.filemap.start_pos) } fn get_str_from(rdr: string_reader, start: BytePos) -> ~str unsafe { @@ -148,10 +148,7 @@ fn bump(rdr: string_reader) { let last_char = rdr.curr; let next = str::char_range_at(*rdr.src, current_byte_offset); let byte_offset_diff = next.next - current_byte_offset; - rdr.pos = FilePos { - ch: rdr.pos.ch + CharPos(1u), - byte: rdr.pos.byte + BytePos(byte_offset_diff) - }; + rdr.pos = rdr.pos + BytePos(byte_offset_diff); rdr.curr = next.ch; rdr.col += CharPos(1u); if last_char == '\n' { @@ -166,10 +163,7 @@ fn bump(rdr: string_reader) { } else { // XXX: What does this accomplish? if (rdr.curr != -1 as char) { - rdr.pos = FilePos { - ch: rdr.pos.ch + CharPos(1u), - byte: rdr.pos.byte + BytePos(1u) - }; + rdr.pos = rdr.pos + BytePos(1u); rdr.col += CharPos(1u); rdr.curr = -1 as char; } @@ -238,7 +232,7 @@ fn consume_any_line_comment(rdr: string_reader) bump(rdr); // line comments starting with "///" or "//!" are doc-comments if rdr.curr == '/' || rdr.curr == '!' { - let start_bpos = rdr.pos.byte - BytePos(2u); + let start_bpos = rdr.pos - BytePos(2u); let mut acc = ~"//"; while rdr.curr != '\n' && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -246,7 +240,7 @@ fn consume_any_line_comment(rdr: string_reader) } return Some({ tok: token::DOC_COMMENT(rdr.interner.intern(@acc)), - sp: ast_util::mk_sp(start_bpos, rdr.pos.byte) + sp: ast_util::mk_sp(start_bpos, rdr.pos) }); } else { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } @@ -261,7 +255,7 @@ fn consume_any_line_comment(rdr: string_reader) if nextch(rdr) == '!' { let cmap = @CodeMap::new(); (*cmap).files.push(rdr.filemap); - let loc = cmap.lookup_char_pos_adj(rdr.last_pos.byte); + let loc = cmap.lookup_char_pos_adj(rdr.last_pos); if loc.line == 1u && loc.col == CharPos(0u) { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } return consume_whitespace_and_comments(rdr); @@ -277,7 +271,7 @@ fn consume_block_comment(rdr: string_reader) // block comments starting with "/**" or "/*!" are doc-comments if rdr.curr == '*' || rdr.curr == '!' { - let start_bpos = rdr.pos.byte - BytePos(2u); + let start_bpos = rdr.pos - BytePos(2u); let mut acc = ~"/*"; while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -291,7 +285,7 @@ fn consume_block_comment(rdr: string_reader) bump(rdr); return Some({ tok: token::DOC_COMMENT(rdr.interner.intern(@acc)), - sp: ast_util::mk_sp(start_bpos, rdr.pos.byte) + sp: ast_util::mk_sp(start_bpos, rdr.pos) }); } } else {