From 4c680849638b548e7202ebe4cbce2c86fd65519a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 15:12:20 -0800 Subject: [PATCH 01/56] Convert most codemap types from records to structs --- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/codemap.rs | 53 +++++++++++++++++--------- src/libsyntax/ext/base.rs | 9 +++-- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/ext/pipes/ast_builder.rs | 4 +- src/libsyntax/ext/simplext.rs | 2 +- src/libsyntax/ext/source_util.rs | 4 +- src/libsyntax/ext/tt/macro_rules.rs | 5 ++- src/libsyntax/parse/parser.rs | 5 +-- 9 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d3b879da7dd95..d406c98d884a9 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -15,7 +15,7 @@ pure fn dummy_spanned(+t: T) -> spanned { /* assuming that we're not in macro expansion */ pure fn mk_sp(lo: uint, hi: uint) -> span { - {lo: lo, hi: hi, expn_info: None} + span {lo: lo, hi: hi, expn_info: None} } // make this a const, once the compiler supports it diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 4278e1f199a62..80c6aabdf411f 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -6,6 +6,7 @@ use std::serialization::{Serializable, export filename; export filemap; +export filemap_; export span; export file_substr; export fss_none; @@ -34,7 +35,9 @@ export new_codemap; type filename = ~str; -type file_pos = {ch: uint, byte: uint}; +struct file_pos { + ch: uint, byte: uint +} impl file_pos : cmp::Eq { pure fn eq(other: &file_pos) -> bool { @@ -55,23 +58,34 @@ enum file_substr { fss_external({filename: ~str, line: uint, col: uint}) } -type filemap = - @{name: filename, substr: file_substr, src: @~str, - start_pos: file_pos, mut lines: ~[file_pos]}; +struct filemap_ { + name: filename, substr: file_substr, src: @~str, + start_pos: file_pos, mut lines: ~[file_pos] +} + +type filemap = @filemap_; + +struct CodeMap_ { + files: DVec +} -type CodeMap = @{files: DVec}; +type CodeMap = @CodeMap_; -type loc = {file: filemap, line: uint, col: uint}; +struct loc { + file: filemap, line: uint, col: uint +} -fn new_codemap() -> CodeMap { @{files: DVec()} } +fn new_codemap() -> CodeMap { @CodeMap_ {files: DVec()} } fn new_filemap_w_substr(+filename: filename, +substr: file_substr, src: @~str, start_pos_ch: uint, start_pos_byte: uint) -> filemap { - return @{name: filename, substr: substr, src: src, - start_pos: {ch: start_pos_ch, byte: start_pos_byte}, - mut lines: ~[{ch: start_pos_ch, byte: start_pos_byte}]}; + return @filemap_ { + name: filename, substr: substr, src: src, + start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte}, + mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}] + }; } fn new_filemap(+filename: filename, src: @~str, @@ -88,7 +102,7 @@ fn mk_substr_filename(cm: CodeMap, sp: span) -> ~str } fn next_line(file: filemap, chpos: uint, byte_pos: uint) { - file.lines.push({ch: chpos, byte: byte_pos + file.start_pos.byte}); + file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte}); } type lookup_fn = pure fn(file_pos) -> uint; @@ -118,7 +132,7 @@ fn lookup_line(map: CodeMap, pos: uint, lookup: lookup_fn) fn lookup_pos(map: CodeMap, pos: uint, lookup: lookup_fn) -> loc { let {fm: f, line: a} = lookup_line(map, pos, lookup); - return {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; + return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; } fn lookup_char_pos(map: CodeMap, pos: uint) -> loc { @@ -160,9 +174,9 @@ fn adjust_span(map: CodeMap, sp: span) -> span { match (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})} + 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 } } @@ -173,7 +187,7 @@ enum expn_info_ { } type expn_info = Option<@expn_info_>; -type span = {lo: uint, hi: uint, expn_info: expn_info}; +struct span {lo: uint, hi: uint, expn_info: expn_info} impl span : cmp::Eq { pure fn eq(other: &span) -> bool { @@ -207,7 +221,10 @@ fn span_to_str(sp: span, cm: CodeMap) -> ~str { lo.line, lo.col, hi.line, hi.col) } -type file_lines = {file: filemap, lines: ~[uint]}; +struct file_lines { + file: filemap, + lines: ~[uint] +} fn span_to_filename(sp: span, cm: codemap::CodeMap) -> filename { let lo = lookup_char_pos(cm, sp.lo); @@ -221,7 +238,7 @@ fn span_to_lines(sp: span, cm: codemap::CodeMap) -> @file_lines { for uint::range(lo.line - 1u, hi.line as uint) |i| { lines.push(i); }; - return @{file: lo.file, lines: lines}; + return @file_lines {file: lo.file, lines: lines}; } fn get_line(fm: filemap, line: int) -> ~str unsafe { diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ddf58ce0fef1c..f588a6390d549 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -2,6 +2,7 @@ use std::map::HashMap; use parse::parser; use diagnostic::span_handler; use codemap::{CodeMap, span, expn_info, expanded_from}; +use ast_util::dummy_sp; // obsolete old-style #macro code: // @@ -169,15 +170,15 @@ fn mk_ctxt(parse_sess: parse::parse_sess, expanded_from({call_site: cs, callie: callie}) => { self.backtrace = Some(@expanded_from({ - call_site: {lo: cs.lo, hi: cs.hi, - expn_info: self.backtrace}, + call_site: span {lo: cs.lo, hi: cs.hi, + expn_info: self.backtrace}, callie: callie})); } } } fn bt_pop() { match self.backtrace { - Some(@expanded_from({call_site: {expn_info: prev, _}, _})) => { + Some(@expanded_from({call_site: span {expn_info: prev, _}, _})) => { self.backtrace = prev } _ => self.bug(~"tried to pop without a push") @@ -311,7 +312,7 @@ fn tt_args_to_original_flavor(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree]) // these spans won't matter, anyways fn ms(m: matcher_) -> matcher { - {node: m, span: {lo: 0u, hi: 0u, expn_info: None}} + {node: m, span: dummy_sp()} } let arg_nm = cx.parse_sess().interner.gensym(@~"arg"); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 22e2cfcde6b51..ca054255fa643 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -232,7 +232,7 @@ fn expand_item_mac(exts: HashMap<~str, syntax_extension>, fn new_span(cx: ext_ctxt, sp: span) -> span { /* this discards information in the case of macro-defining macros */ - return {lo: sp.lo, hi: sp.hi, expn_info: cx.backtrace()}; + return span {lo: sp.lo, hi: sp.hi, expn_info: cx.backtrace()}; } // FIXME (#2247): this is a terrible kludge to inject some macros into diff --git a/src/libsyntax/ext/pipes/ast_builder.rs b/src/libsyntax/ext/pipes/ast_builder.rs index f03adb90f0bcf..3d63b0ddb775d 100644 --- a/src/libsyntax/ext/pipes/ast_builder.rs +++ b/src/libsyntax/ext/pipes/ast_builder.rs @@ -24,7 +24,7 @@ fn path(ids: ~[ident], span: span) -> @ast::path { } fn empty_span() -> span { - {lo: 0, hi: 0, expn_info: None} + span {lo: 0, hi: 0, expn_info: None} } trait append_types { @@ -95,7 +95,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { } fn empty_span() -> span { - {lo: 0, hi: 0, expn_info: self.backtrace()} + span {lo: 0, hi: 0, expn_info: self.backtrace()} } fn block_expr(b: ast::blk) -> @ast::expr { diff --git a/src/libsyntax/ext/simplext.rs b/src/libsyntax/ext/simplext.rs index bec29c9a83540..df7674264ca11 100644 --- a/src/libsyntax/ext/simplext.rs +++ b/src/libsyntax/ext/simplext.rs @@ -177,7 +177,7 @@ fn transcribe(cx: ext_ctxt, b: bindings, body: @expr) -> @expr { fn new_id(_old: node_id, cx: ext_ctxt) -> node_id { return cx.next_id(); } fn new_span(cx: ext_ctxt, sp: span) -> span { /* this discards information in the case of macro-defining macros */ - return {lo: sp.lo, hi: sp.hi, expn_info: cx.backtrace()}; + return span {lo: sp.lo, hi: sp.hi, expn_info: cx.backtrace()}; } let afp = default_ast_fold(); let f_pre = diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 9b41d90e6d045..90f08095286d4 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -1,5 +1,5 @@ use base::*; -use codemap::span; +use codemap::{span, loc, filemap_}; use print::pprust; use build::{mk_base_vec_e,mk_uint,mk_u8,mk_uniq_str}; @@ -34,7 +34,7 @@ fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, 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 { file: @{ name: filename, _ }, _ } = + let loc { file: @filemap_ { name: filename, _ }, _ } = codemap::lookup_char_pos(cx.codemap(), sp.lo); return mk_uniq_str(cx, sp, filename); } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 31bc375a76d56..8bfd1c0a18d39 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -9,12 +9,13 @@ use macro_parser::{parse, parse_or_else, success, failure, named_match, matched_seq, matched_nonterminal, error}; use std::map::HashMap; use parse::token::special_idents; +use ast_util::dummy_sp; fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, arg: ~[ast::token_tree]) -> base::mac_result { // these spans won't matter, anyways fn ms(m: matcher_) -> matcher { - {node: m, span: {lo: 0u, hi: 0u, expn_info: None}} + {node: m, span: dummy_sp()} } let lhs_nm = cx.parse_sess().interner.gensym(@~"lhs"); @@ -65,7 +66,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, } // Which arm's failure should we report? (the one furthest along) - let mut best_fail_spot = {lo: 0u, hi: 0u, expn_info: None}; + let mut best_fail_spot = dummy_sp(); let mut best_fail_msg = ~"internal error: ran no matchers"; let s_d = cx.parse_sess().span_diagnostic; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2b42dcc0ed06a..9df6b7ab5d2f0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3415,9 +3415,8 @@ impl Parser { |p| p.parse_token_tree()); let m = ast::mac_invoc_tt(pth, tts); let m: ast::mac = {node: m, - span: {lo: self.span.lo, - hi: self.span.hi, - expn_info: None}}; + span: mk_sp(self.span.lo, + self.span.hi)}; let item_ = item_mac(m); return iovi_item(self.mk_item(lo, self.last_span.hi, id, item_, visibility, attrs)); From 5f881b48cb058236494ccc2f6d54cf617ae21a7f Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 16:41:20 -0800 Subject: [PATCH 02/56] Remove filemap box typedef from codemap --- src/libsyntax/codemap.rs | 24 +++++++++++------------- src/libsyntax/ext/source_util.rs | 4 ++-- src/libsyntax/parse.rs | 4 ++-- src/libsyntax/parse/comments.rs | 2 +- src/libsyntax/parse/lexer.rs | 6 +++--- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 80c6aabdf411f..f9dbfb5496dfe 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -58,21 +58,19 @@ enum file_substr { fss_external({filename: ~str, line: uint, col: uint}) } -struct filemap_ { +struct filemap { name: filename, substr: file_substr, src: @~str, start_pos: file_pos, mut lines: ~[file_pos] } -type filemap = @filemap_; - struct CodeMap_ { - files: DVec + files: DVec<@filemap> } type CodeMap = @CodeMap_; struct loc { - file: filemap, line: uint, col: uint + file: @filemap, line: uint, col: uint } fn new_codemap() -> CodeMap { @CodeMap_ {files: DVec()} } @@ -81,7 +79,7 @@ fn new_filemap_w_substr(+filename: filename, +substr: file_substr, src: @~str, start_pos_ch: uint, start_pos_byte: uint) -> filemap { - return @filemap_ { + return filemap { name: filename, substr: substr, src: src, start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte}, mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}] @@ -101,14 +99,14 @@ fn mk_substr_filename(cm: CodeMap, sp: span) -> ~str return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); } -fn next_line(file: filemap, chpos: uint, byte_pos: uint) { +fn next_line(file: @filemap, chpos: uint, byte_pos: uint) { file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte}); } type lookup_fn = pure fn(file_pos) -> uint; fn lookup_line(map: CodeMap, pos: uint, lookup: lookup_fn) - -> {fm: filemap, line: uint} + -> {fm: @filemap, line: uint} { let len = map.files.len(); let mut a = 0u; @@ -146,7 +144,7 @@ fn lookup_byte_pos(map: CodeMap, pos: uint) -> loc { } fn lookup_char_pos_adj(map: CodeMap, pos: uint) - -> {filename: ~str, line: uint, col: uint, file: Option} + -> {filename: ~str, line: uint, col: uint, file: Option<@filemap>} { let loc = lookup_char_pos(map, pos); match (loc.file.substr) { @@ -222,7 +220,7 @@ fn span_to_str(sp: span, cm: CodeMap) -> ~str { } struct file_lines { - file: filemap, + file: @filemap, lines: ~[uint] } @@ -241,7 +239,7 @@ fn span_to_lines(sp: span, cm: codemap::CodeMap) -> @file_lines { return @file_lines {file: lo.file, lines: lines}; } -fn get_line(fm: filemap, line: int) -> ~str unsafe { +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, @@ -251,7 +249,7 @@ fn get_line(fm: filemap, line: int) -> ~str unsafe { } fn lookup_byte_offset(cm: codemap::CodeMap, chpos: uint) - -> {fm: filemap, pos: 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; @@ -273,7 +271,7 @@ fn get_snippet(cm: codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str return str::slice(*fm.src, lo, hi) } -fn get_filemap(cm: CodeMap, filename: ~str) -> filemap { +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 _|_) diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 90f08095286d4..3181a6044007c 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -1,5 +1,5 @@ use base::*; -use codemap::{span, loc, filemap_}; +use codemap::{span, loc, filemap}; use print::pprust; use build::{mk_base_vec_e,mk_uint,mk_u8,mk_uniq_str}; @@ -34,7 +34,7 @@ fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, 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, _ }, _ } = + let loc { file: @filemap { name: filename, _ }, _ } = codemap::lookup_char_pos(cx.codemap(), sp.lo); return mk_uniq_str(cx, sp, filename); } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index e38ee7ff03763..94e9b1d023865 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -172,7 +172,7 @@ fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::file_substr, source: @~str) -> (Parser, string_reader) { let ftype = parser::SOURCE_FILE; - let filemap = codemap::new_filemap_w_substr + let filemap = @codemap::new_filemap_w_substr (name, ss, source, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, @@ -197,7 +197,7 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, result::Err(e) => sess.span_diagnostic.handler().fatal(e) } let src = @result::unwrap(res); - let filemap = codemap::new_filemap(path.to_str(), src, + let filemap = @codemap::new_filemap(path.to_str(), src, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 4f265e1919c2e..c691d9985492b 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -288,7 +288,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); let rdr = lexer::new_low_level_string_reader - (span_diagnostic, codemap::new_filemap(path, src, 0u, 0u), itr); + (span_diagnostic, @codemap::new_filemap(path, src, 0u, 0u), itr); let mut comments: ~[cmnt] = ~[]; let mut literals: ~[lit] = ~[]; diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 8f57d733eb51f..a56f2576ab1a0 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -25,7 +25,7 @@ type string_reader = @{ mut pos: uint, mut curr: char, mut chpos: uint, - filemap: codemap::filemap, + filemap: @codemap::filemap, interner: @token::ident_interner, /* cached: */ mut peek_tok: token::Token, @@ -33,7 +33,7 @@ type string_reader = @{ }; fn new_string_reader(span_diagnostic: span_handler, - filemap: codemap::filemap, + filemap: @codemap::filemap, itr: @token::ident_interner) -> string_reader { let r = new_low_level_string_reader(span_diagnostic, filemap, itr); string_advance_token(r); /* fill in peek_* */ @@ -42,7 +42,7 @@ fn new_string_reader(span_diagnostic: span_handler, /* For comments.rs, which hackily pokes into 'pos' and 'curr' */ fn new_low_level_string_reader(span_diagnostic: span_handler, - filemap: codemap::filemap, + filemap: @codemap::filemap, itr: @token::ident_interner) -> string_reader { let r = @{span_diagnostic: span_diagnostic, src: filemap.src, From 1f3303163c3f4a725b29de5e00eb14a5c53dd16e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 16:44:53 -0800 Subject: [PATCH 03/56] Move filemap ctors to static methods --- src/libsyntax/codemap.rs | 38 +++++++++++++++++---------------- src/libsyntax/parse.rs | 7 +++--- src/libsyntax/parse/comments.rs | 3 ++- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index f9dbfb5496dfe..ec7d3146bb954 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -63,6 +63,26 @@ struct filemap { start_pos: file_pos, mut lines: ~[file_pos] } +impl filemap { + static fn new_w_substr(+filename: filename, +substr: file_substr, + src: @~str, + start_pos_ch: uint, start_pos_byte: uint) + -> filemap { + return filemap { + name: filename, substr: substr, src: src, + start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte}, + mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}] + }; + } + + static fn new(+filename: filename, src: @~str, + start_pos_ch: uint, start_pos_byte: uint) + -> filemap { + return filemap::new_w_substr(filename, fss_none, src, + start_pos_ch, start_pos_byte); + } +} + struct CodeMap_ { files: DVec<@filemap> } @@ -75,24 +95,6 @@ struct loc { fn new_codemap() -> CodeMap { @CodeMap_ {files: DVec()} } -fn new_filemap_w_substr(+filename: filename, +substr: file_substr, - src: @~str, - start_pos_ch: uint, start_pos_byte: uint) - -> filemap { - return filemap { - name: filename, substr: substr, src: src, - start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte}, - mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}] - }; -} - -fn new_filemap(+filename: filename, src: @~str, - start_pos_ch: uint, start_pos_byte: uint) - -> filemap { - return 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); diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 94e9b1d023865..9547d7a1d130c 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -20,6 +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; type parse_sess = @{ cm: codemap::CodeMap, @@ -172,7 +173,7 @@ fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::file_substr, source: @~str) -> (Parser, string_reader) { let ftype = parser::SOURCE_FILE; - let filemap = @codemap::new_filemap_w_substr + let filemap = @filemap::new_w_substr (name, ss, source, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, @@ -197,8 +198,8 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, result::Err(e) => sess.span_diagnostic.handler().fatal(e) } let src = @result::unwrap(res); - let filemap = @codemap::new_filemap(path.to_str(), src, - sess.chpos, sess.byte_pos); + let filemap = @filemap::new(path.to_str(), src, + sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index c691d9985492b..0ed8bd12361e5 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -3,6 +3,7 @@ use io::ReaderUtil; use util::interner; use lexer::{string_reader, bump, is_eof, nextch, is_whitespace, get_str_from, reader}; +use codemap::filemap; export cmnt; export lit; @@ -288,7 +289,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); let rdr = lexer::new_low_level_string_reader - (span_diagnostic, @codemap::new_filemap(path, src, 0u, 0u), itr); + (span_diagnostic, @filemap::new(path, src, 0u, 0u), itr); let mut comments: ~[cmnt] = ~[]; let mut literals: ~[lit] = ~[]; From 9fc75e8d661e094e50f4d651c8a48b8c948c2e31 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 16:45:24 -0800 Subject: [PATCH 04/56] Move codemap doc comments to the proper place --- src/libsyntax/codemap.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index ec7d3146bb954..d8c39f111c6c7 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -1,3 +1,9 @@ +/*! 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. + */ + use dvec::DVec; use std::serialization::{Serializable, Deserializable, @@ -46,12 +52,6 @@ impl file_pos : cmp::Eq { pure fn ne(other: &file_pos) -> bool { !self.eq(other) } } -/* 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), From 385a4668e568e4379371bf065d071f6caf6d1c3a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 16:47:17 -0800 Subject: [PATCH 05/56] Reformatting in codemap --- src/libsyntax/codemap.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index d8c39f111c6c7..2583cc036a397 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -59,8 +59,11 @@ enum file_substr { } struct filemap { - name: filename, substr: file_substr, src: @~str, - start_pos: file_pos, mut lines: ~[file_pos] + name: filename, + substr: file_substr, + src: @~str, + start_pos: file_pos, + mut lines: ~[file_pos] } impl filemap { From 5b248a6ca8962249d5882375610764a6dd9561c3 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 16:56:39 -0800 Subject: [PATCH 06/56] Remove CodeMap box typedef from codemap --- src/libfuzzer/fuzzer.rs | 4 +-- src/librustc/driver/driver.rs | 4 +-- src/librustc/driver/session.rs | 2 +- src/librustc/middle/trans/debuginfo.rs | 2 +- src/librustc/rustc.rs | 2 +- src/librustdoc/astsrv.rs | 8 +++--- src/libsyntax/codemap.rs | 40 ++++++++++++++------------ src/libsyntax/diagnostic.rs | 18 ++++++------ src/libsyntax/ext/base.rs | 4 +-- src/libsyntax/parse.rs | 6 ++-- src/libsyntax/parse/lexer.rs | 2 +- src/libsyntax/print/pprust.rs | 6 ++-- 12 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/libfuzzer/fuzzer.rs b/src/libfuzzer/fuzzer.rs index a4968382cf478..86e45179cb033 100644 --- a/src/libfuzzer/fuzzer.rs +++ b/src/libfuzzer/fuzzer.rs @@ -225,7 +225,7 @@ fn as_str(f: fn@(+x: io::Writer)) -> ~str { io::with_str_writer(f) } -fn check_variants_of_ast(crate: ast::crate, codemap: codemap::CodeMap, +fn check_variants_of_ast(crate: ast::crate, codemap: @codemap::CodeMap, filename: &Path, cx: context) { let stolen = steal(crate, cx.mode); let extra_exprs = vec::filter(common_exprs(), @@ -239,7 +239,7 @@ fn check_variants_of_ast(crate: ast::crate, codemap: codemap::CodeMap, fn check_variants_T( crate: ast::crate, - codemap: codemap::CodeMap, + codemap: @codemap::CodeMap, filename: &Path, thing_label: ~str, things: ~[T], diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 934a02d6dd3e3..f1c64a7e0995f 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -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::new_codemap(); let diagnostic_handler = diagnostic::mk_handler(Some(demitter)); let span_diagnostic_handler = @@ -583,7 +583,7 @@ fn build_session(sopts: @session::options, } fn build_session_(sopts: @session::options, - cm: codemap::CodeMap, + cm: @codemap::CodeMap, demitter: diagnostic::emitter, span_diagnostic_handler: diagnostic::span_handler) -> Session { diff --git a/src/librustc/driver/session.rs b/src/librustc/driver/session.rs index ed73bcb6d7259..d2a277e82fbd5 100644 --- a/src/librustc/driver/session.rs +++ b/src/librustc/driver/session.rs @@ -131,7 +131,7 @@ type Session_ = {targ_cfg: @config, opts: @options, cstore: metadata::cstore::CStore, parse_sess: parse_sess, - codemap: codemap::CodeMap, + codemap: @codemap::CodeMap, // For a library crate, this is always none mut main_fn: Option<(node_id, codemap::span)>, span_diagnostic: diagnostic::span_handler, diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index fd18aaaf58ec6..c794b0189fc2e 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -229,7 +229,7 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata { return mdval; } -fn line_from_span(cm: codemap::CodeMap, sp: span) -> uint { +fn line_from_span(cm: @codemap::CodeMap, sp: span) -> uint { codemap::lookup_char_pos(cm, sp.lo).line } diff --git a/src/librustc/rustc.rs b/src/librustc/rustc.rs index 8b832b9ab9f87..789c7ca7073a8 100644 --- a/src/librustc/rustc.rs +++ b/src/librustc/rustc.rs @@ -193,7 +193,7 @@ fn monitor(+f: fn~(diagnostic::emitter)) { // The 'diagnostics emitter'. Every error, warning, etc. should // go through this function. - let demitter = fn@(cmsp: Option<(codemap::CodeMap, codemap::span)>, + let demitter = fn@(cmsp: Option<(@codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { if lvl == diagnostic::fatal { comm::send(ch, fatal); diff --git a/src/librustdoc/astsrv.rs b/src/librustdoc/astsrv.rs index 7b2c6fe5f0cbc..480fc6c3914b4 100644 --- a/src/librustdoc/astsrv.rs +++ b/src/librustdoc/astsrv.rs @@ -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::new_codemap(); let error_handlers = build_error_handlers(codemap); let {emitter, span_handler} = error_handlers; @@ -137,7 +137,7 @@ type ErrorHandlers = { // Build a custom error handler that will allow us to ignore non-fatal // errors fn build_error_handlers( - codemap: codemap::CodeMap + codemap: @codemap::CodeMap ) -> ErrorHandlers { type DiagnosticHandler = { @@ -156,13 +156,13 @@ fn build_error_handlers( fn note(msg: &str) { self.inner.note(msg) } fn bug(msg: &str) -> ! { self.inner.bug(msg) } fn unimpl(msg: &str) -> ! { self.inner.unimpl(msg) } - fn emit(cmsp: Option<(codemap::CodeMap, codemap::span)>, + fn emit(cmsp: Option<(@codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { self.inner.emit(cmsp, msg, lvl) } } - let emitter = fn@(cmsp: Option<(codemap::CodeMap, codemap::span)>, + let emitter = fn@(cmsp: Option<(@codemap::CodeMap, codemap::span)>, msg: &str, lvl: diagnostic::level) { diagnostic::emit(cmsp, msg, lvl); }; diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 2583cc036a397..a616e73e54948 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -86,19 +86,21 @@ impl filemap { } } -struct CodeMap_ { +struct CodeMap { files: DVec<@filemap> } -type CodeMap = @CodeMap_; - struct loc { file: @filemap, line: uint, col: uint } -fn new_codemap() -> CodeMap { @CodeMap_ {files: DVec()} } +fn new_codemap() -> CodeMap { + CodeMap { + files: DVec() + } +} -fn mk_substr_filename(cm: CodeMap, sp: span) -> ~str +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); @@ -110,7 +112,7 @@ fn next_line(file: @filemap, chpos: uint, byte_pos: uint) { type lookup_fn = pure fn(file_pos) -> uint; -fn lookup_line(map: CodeMap, pos: uint, lookup: lookup_fn) +fn lookup_line(map: @CodeMap, pos: uint, lookup: lookup_fn) -> {fm: @filemap, line: uint} { let len = map.files.len(); @@ -133,22 +135,22 @@ fn lookup_line(map: CodeMap, pos: uint, lookup: lookup_fn) return {fm: f, line: a}; } -fn lookup_pos(map: CodeMap, pos: uint, lookup: lookup_fn) -> loc { +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])}; } -fn lookup_char_pos(map: CodeMap, pos: uint) -> loc { +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 { +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); } -fn lookup_char_pos_adj(map: CodeMap, pos: uint) +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); @@ -171,7 +173,7 @@ fn lookup_char_pos_adj(map: CodeMap, pos: uint) } } -fn adjust_span(map: CodeMap, sp: span) -> span { +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) { @@ -210,14 +212,14 @@ impl span: Deserializable { } } -fn span_to_str_no_adj(sp: span, cm: CodeMap) -> ~str { +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) } -fn span_to_str(sp: span, cm: CodeMap) -> ~str { +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, @@ -229,12 +231,12 @@ struct file_lines { lines: ~[uint] } -fn span_to_filename(sp: span, cm: codemap::CodeMap) -> filename { +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; } -fn span_to_lines(sp: span, cm: codemap::CodeMap) -> @file_lines { +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 = ~[]; @@ -253,7 +255,7 @@ fn get_line(fm: @filemap, line: int) -> ~str unsafe { str::slice(*fm.src, begin, end) } -fn lookup_byte_offset(cm: codemap::CodeMap, chpos: uint) +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); @@ -263,20 +265,20 @@ fn lookup_byte_offset(cm: codemap::CodeMap, chpos: uint) {fm: fm, pos: line_offset + col_offset} } -fn span_to_snippet(sp: span, cm: codemap::CodeMap) -> ~str { +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); } -fn get_snippet(cm: codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str +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 get_filemap(cm: CodeMap, filename: ~str) -> @filemap { +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 _|_) diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 855b0ca3ef568..9335faed0c987 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -9,7 +9,7 @@ export codemap_span_handler, codemap_handler; export ice_msg; export expect; -type emitter = fn@(cmsp: Option<(codemap::CodeMap, span)>, +type emitter = fn@(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level); @@ -33,7 +33,7 @@ trait handler { fn note(msg: &str); fn bug(msg: &str) -> !; fn unimpl(msg: &str) -> !; - fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level); + fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level); } type handler_t = @{ @@ -43,7 +43,7 @@ type handler_t = @{ type codemap_t = @{ handler: handler, - cm: codemap::CodeMap + cm: @codemap::CodeMap }; impl codemap_t: span_handler { @@ -107,7 +107,7 @@ impl handler_t: handler { self.fatal(ice_msg(msg)); } fn unimpl(msg: &str) -> ! { self.bug(~"unimplemented " + msg); } - fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { + fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) { self.emit(cmsp, msg, lvl); } } @@ -116,7 +116,7 @@ fn ice_msg(msg: &str) -> ~str { fmt!("internal compiler error: %s", msg) } -fn mk_span_handler(handler: handler, cm: codemap::CodeMap) -> span_handler { +fn mk_span_handler(handler: handler, cm: @codemap::CodeMap) -> span_handler { @{ handler: handler, cm: cm } as span_handler } @@ -125,7 +125,7 @@ fn mk_handler(emitter: Option) -> handler { let emit = match emitter { Some(e) => e, None => { - let f = fn@(cmsp: Option<(codemap::CodeMap, span)>, + let f = fn@(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, t: level) { emit(cmsp, msg, t); }; @@ -189,7 +189,7 @@ fn print_diagnostic(topic: ~str, lvl: level, msg: &str) { io::stderr().write_str(fmt!(" %s\n", msg)); } -fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { +fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) { match cmsp { Some((cm, sp)) => { let sp = codemap::adjust_span(cm,sp); @@ -205,7 +205,7 @@ fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { } } -fn highlight_lines(cm: codemap::CodeMap, sp: span, +fn highlight_lines(cm: @codemap::CodeMap, sp: span, lines: @codemap::file_lines) { let fm = lines.file; @@ -260,7 +260,7 @@ fn highlight_lines(cm: codemap::CodeMap, sp: span, } } -fn print_macro_backtrace(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)); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index f588a6390d549..ffed726f1f854 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -125,7 +125,7 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { // when a macro expansion occurs, the resulting nodes have the backtrace() // -> expn_info of their expansion context stored into their span. trait ext_ctxt { - fn codemap() -> CodeMap; + fn codemap() -> @CodeMap; fn parse_sess() -> parse::parse_sess; fn cfg() -> ast::crate_cfg; fn print_backtrace(); @@ -157,7 +157,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, mut mod_path: ~[ast::ident], mut trace_mac: bool}; impl ctxt_repr: ext_ctxt { - fn codemap() -> CodeMap { self.parse_sess.cm } + fn codemap() -> @CodeMap { self.parse_sess.cm } fn parse_sess() -> parse::parse_sess { self.parse_sess } fn cfg() -> ast::crate_cfg { self.cfg } fn print_backtrace() { } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 9547d7a1d130c..f93be9c7266c8 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -23,7 +23,7 @@ use parse::token::{ident_interner, mk_ident_interner}; use codemap::filemap; type parse_sess = @{ - cm: codemap::CodeMap, + cm: @codemap::CodeMap, mut next_id: node_id, span_diagnostic: span_handler, interner: @ident_interner, @@ -33,7 +33,7 @@ type parse_sess = @{ }; fn new_parse_sess(demitter: Option) -> parse_sess { - let cm = codemap::new_codemap(); + let cm = @codemap::new_codemap(); return @{cm: cm, mut next_id: 1, span_diagnostic: mk_span_handler(mk_handler(demitter), cm), @@ -41,7 +41,7 @@ fn new_parse_sess(demitter: Option) -> parse_sess { mut chpos: 0u, mut byte_pos: 0u}; } -fn new_parse_sess_special_handler(sh: span_handler, cm: codemap::CodeMap) +fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) -> parse_sess { return @{cm: cm, mut next_id: 1, diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index a56f2576ab1a0..f3e84a0cbf8ea 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -232,7 +232,7 @@ fn consume_any_line_comment(rdr: string_reader) } } else if rdr.curr == '#' { if nextch(rdr) == '!' { - let cmap = codemap::new_codemap(); + let cmap = @codemap::new_codemap(); (*cmap).files.push(rdr.filemap); let loc = codemap::lookup_char_pos_adj(cmap, rdr.chpos); if loc.line == 1u && loc.col == 0u { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 0418f6776de6b..2bf9b826d6fae 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -25,7 +25,7 @@ fn no_ann() -> pp_ann { type ps = @{s: pp::printer, - cm: Option, + cm: Option<@CodeMap>, intr: @token::ident_interner, comments: Option<~[comments::cmnt]>, literals: Option<~[comments::lit]>, @@ -46,7 +46,7 @@ fn end(s: ps) { fn rust_printer(writer: io::Writer, intr: @ident_interner) -> ps { return @{s: pp::mk_printer(writer, default_columns), - cm: None::, + cm: None::<@CodeMap>, intr: intr, comments: None::<~[comments::cmnt]>, literals: None::<~[comments::lit]>, @@ -64,7 +64,7 @@ const default_columns: uint = 78u; // Requires you to pass an input filename and reader so that // it can scan the input text for comments and literals to // copy forward. -fn print_crate(cm: CodeMap, intr: @ident_interner, +fn print_crate(cm: @CodeMap, intr: @ident_interner, span_diagnostic: diagnostic::span_handler, crate: @ast::crate, filename: ~str, in: io::Reader, out: io::Writer, ann: pp_ann, is_expanded: bool) { From 18a825bda0a6098d3d8982784f3dffb908df5e40 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 17:14:15 -0800 Subject: [PATCH 07/56] Reorder codemap decls --- src/libsyntax/codemap.rs | 64 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index a616e73e54948..be7e2025120ef 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -39,8 +39,40 @@ export loc; export get_filemap; export new_codemap; +struct span {lo: uint, hi: uint, expn_info: expn_info} + +impl span : cmp::Eq { + pure fn eq(other: &span) -> bool { + return self.lo == (*other).lo && self.hi == (*other).hi; + } + pure fn ne(other: &span) -> bool { !self.eq(other) } +} + +impl span: Serializable { + /* Note #1972 -- spans are serialized but not deserialized */ + fn serialize(&self, _s: &S) { } +} + +impl span: Deserializable { + static fn deserialize(_d: &D) -> span { + ast_util::dummy_sp() + } +} + +enum expn_info_ { + expanded_from({call_site: span, + callie: {name: ~str, span: Option}}) +} +type expn_info = Option<@expn_info_>; + type filename = ~str; +type lookup_fn = pure fn(file_pos) -> uint; + +struct loc { + file: @filemap, line: uint, col: uint +} + struct file_pos { ch: uint, byte: uint } @@ -90,10 +122,6 @@ struct CodeMap { files: DVec<@filemap> } -struct loc { - file: @filemap, line: uint, col: uint -} - fn new_codemap() -> CodeMap { CodeMap { files: DVec() @@ -110,8 +138,6 @@ fn next_line(file: @filemap, chpos: uint, byte_pos: uint) { file.lines.push(file_pos {ch: chpos, byte: byte_pos + file.start_pos.byte}); } -type lookup_fn = pure fn(file_pos) -> uint; - fn lookup_line(map: @CodeMap, pos: uint, lookup: lookup_fn) -> {fm: @filemap, line: uint} { @@ -186,32 +212,6 @@ fn adjust_span(map: @CodeMap, sp: span) -> span { } } -enum expn_info_ { - expanded_from({call_site: span, - callie: {name: ~str, span: Option}}) -} -type expn_info = Option<@expn_info_>; - -struct span {lo: uint, hi: uint, expn_info: expn_info} - -impl span : cmp::Eq { - pure fn eq(other: &span) -> bool { - return self.lo == (*other).lo && self.hi == (*other).hi; - } - pure fn ne(other: &span) -> bool { !self.eq(other) } -} - -impl span: Serializable { - /* Note #1972 -- spans are serialized but not deserialized */ - fn serialize(&self, _s: &S) { } -} - -impl span: Deserializable { - static fn deserialize(_d: &D) -> span { - ast_util::dummy_sp() - } -} - 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); From d115944809467bd9011d769abbdfd326f8a9d619 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 17:19:56 -0800 Subject: [PATCH 08/56] Remove expn_info box typedef from codemap --- src/libsyntax/codemap.rs | 9 ++++++--- src/libsyntax/ext/base.rs | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index be7e2025120ef..c4e922628a230 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -39,7 +39,11 @@ export loc; export get_filemap; export new_codemap; -struct span {lo: uint, hi: uint, expn_info: expn_info} +struct span { + lo: uint, + hi: uint, + expn_info: Option<@expn_info> +} impl span : cmp::Eq { pure fn eq(other: &span) -> bool { @@ -59,11 +63,10 @@ impl span: Deserializable { } } -enum expn_info_ { +enum expn_info { expanded_from({call_site: span, callie: {name: ~str, span: Option}}) } -type expn_info = Option<@expn_info_>; type filename = ~str; diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ffed726f1f854..127e4a13e27ab 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -129,11 +129,11 @@ trait ext_ctxt { fn parse_sess() -> parse::parse_sess; fn cfg() -> ast::crate_cfg; fn print_backtrace(); - fn backtrace() -> expn_info; + fn backtrace() -> Option<@expn_info>; fn mod_push(mod_name: ast::ident); fn mod_pop(); fn mod_path() -> ~[ast::ident]; - fn bt_push(ei: codemap::expn_info_); + fn bt_push(ei: codemap::expn_info); fn bt_pop(); fn span_fatal(sp: span, msg: &str) -> !; fn span_err(sp: span, msg: &str); @@ -153,7 +153,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, cfg: ast::crate_cfg) -> ext_ctxt { type ctxt_repr = {parse_sess: parse::parse_sess, cfg: ast::crate_cfg, - mut backtrace: expn_info, + mut backtrace: Option<@expn_info>, mut mod_path: ~[ast::ident], mut trace_mac: bool}; impl ctxt_repr: ext_ctxt { @@ -161,11 +161,11 @@ fn mk_ctxt(parse_sess: parse::parse_sess, fn parse_sess() -> parse::parse_sess { self.parse_sess } fn cfg() -> ast::crate_cfg { self.cfg } fn print_backtrace() { } - fn backtrace() -> expn_info { self.backtrace } + fn backtrace() -> Option<@expn_info> { self.backtrace } fn mod_push(i: ast::ident) { self.mod_path.push(i); } fn mod_pop() { self.mod_path.pop(); } fn mod_path() -> ~[ast::ident] { return self.mod_path; } - fn bt_push(ei: codemap::expn_info_) { + fn bt_push(ei: codemap::expn_info) { match ei { expanded_from({call_site: cs, callie: callie}) => { self.backtrace = From 15a5d2ccbf2de7ac0bb4894a6885791fa1f23521 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 17:26:02 -0800 Subject: [PATCH 09/56] Convert codemap from legacy_exports --- src/libsyntax/codemap.rs | 83 +++++++++++++--------------------------- src/libsyntax/syntax.rc | 1 - 2 files changed, 27 insertions(+), 57 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index c4e922628a230..6a0e8063119e8 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -10,36 +10,7 @@ use std::serialization::{Serializable, Serializer, Deserializer}; -export filename; -export filemap; -export filemap_; -export span; -export file_substr; -export fss_none; -export fss_internal; -export fss_external; -export CodeMap; -export expn_info; -export expn_info_; -export expanded_from; -export new_filemap; -export new_filemap_w_substr; -export mk_substr_filename; -export lookup_char_pos; -export lookup_char_pos_adj; -export adjust_span; -export span_to_str; -export span_to_filename; -export span_to_lines; -export file_lines; -export get_line; -export next_line; -export span_to_snippet; -export loc; -export get_filemap; -export new_codemap; - -struct span { +pub struct span { lo: uint, hi: uint, expn_info: Option<@expn_info> @@ -63,20 +34,20 @@ impl span: Deserializable { } } -enum expn_info { +pub enum expn_info { expanded_from({call_site: span, callie: {name: ~str, span: Option}}) } -type filename = ~str; +pub type filename = ~str; -type lookup_fn = pure fn(file_pos) -> uint; +pub type lookup_fn = pure fn(file_pos) -> uint; -struct loc { +pub struct loc { file: @filemap, line: uint, col: uint } -struct file_pos { +pub struct file_pos { ch: uint, byte: uint } @@ -87,13 +58,13 @@ impl file_pos : cmp::Eq { pure fn ne(other: &file_pos) -> bool { !self.eq(other) } } -enum file_substr { - fss_none, - fss_internal(span), - fss_external({filename: ~str, line: uint, col: uint}) +pub enum file_substr { + pub fss_none, + pub fss_internal(span), + pub fss_external({filename: ~str, line: uint, col: uint}) } -struct filemap { +pub struct filemap { name: filename, substr: file_substr, src: @~str, @@ -101,7 +72,7 @@ struct filemap { mut lines: ~[file_pos] } -impl filemap { +pub impl filemap { static fn new_w_substr(+filename: filename, +substr: file_substr, src: @~str, start_pos_ch: uint, start_pos_byte: uint) @@ -121,23 +92,23 @@ impl filemap { } } -struct CodeMap { +pub struct CodeMap { files: DVec<@filemap> } -fn new_codemap() -> CodeMap { +pub fn new_codemap() -> CodeMap { CodeMap { files: DVec() } } -fn mk_substr_filename(cm: @CodeMap, sp: span) -> ~str +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); } -fn next_line(file: @filemap, chpos: uint, byte_pos: uint) { +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}); } @@ -169,7 +140,7 @@ fn lookup_pos(map: @CodeMap, pos: uint, lookup: lookup_fn) -> loc { return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; } -fn lookup_char_pos(map: @CodeMap, pos: uint) -> loc { +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); } @@ -179,7 +150,7 @@ fn lookup_byte_pos(map: @CodeMap, pos: uint) -> loc { return lookup_pos(map, pos, lookup); } -fn lookup_char_pos_adj(map: @CodeMap, pos: uint) +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); @@ -202,7 +173,7 @@ fn lookup_char_pos_adj(map: @CodeMap, pos: uint) } } -fn adjust_span(map: @CodeMap, sp: span) -> span { +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) { @@ -222,24 +193,24 @@ fn span_to_str_no_adj(sp: span, cm: @CodeMap) -> ~str { lo.line, lo.col, hi.line, hi.col) } -fn span_to_str(sp: span, cm: @CodeMap) -> ~str { +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) } -struct file_lines { +pub struct file_lines { file: @filemap, lines: ~[uint] } -fn span_to_filename(sp: span, cm: @codemap::CodeMap) -> filename { +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; } -fn span_to_lines(sp: span, cm: @codemap::CodeMap) -> @file_lines { +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 = ~[]; @@ -249,7 +220,7 @@ fn span_to_lines(sp: span, cm: @codemap::CodeMap) -> @file_lines { return @file_lines {file: lo.file, lines: lines}; } -fn get_line(fm: @filemap, line: int) -> ~str unsafe { +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, @@ -268,20 +239,20 @@ fn lookup_byte_offset(cm: @codemap::CodeMap, chpos: uint) {fm: fm, pos: line_offset + col_offset} } -fn span_to_snippet(sp: span, cm: @codemap::CodeMap) -> ~str { +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); } -fn get_snippet(cm: @codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str +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 get_filemap(cm: @CodeMap, filename: ~str) -> @filemap { +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 _|_) diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 7c73deed1a91f..9195f1fd8f7d9 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -25,7 +25,6 @@ use core::*; mod attr; #[legacy_exports] mod diagnostic; -#[legacy_exports] mod codemap; #[legacy_exports] mod ast; From 2ec09c4eb91b94bd68c95eaa8966d4801c3347bf Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 18:24:56 -0800 Subject: [PATCH 10/56] Objectify the codemap --- src/librustc/driver/driver.rs | 4 +- src/librustc/metadata/encoder.rs | 2 +- src/librustc/middle/liveness.rs | 8 +- src/librustc/middle/trans/base.rs | 2 +- src/librustc/middle/trans/build.rs | 2 +- src/librustc/middle/trans/controlflow.rs | 4 +- src/librustc/middle/trans/debuginfo.rs | 21 +- .../middle/typeck/infer/region_inference.rs | 2 +- src/librustc/util/ppaux.rs | 12 +- src/librustdoc/astsrv.rs | 2 +- src/libsyntax/codemap.rs | 283 +++++++++--------- src/libsyntax/diagnostic.rs | 16 +- src/libsyntax/ext/qquote.rs | 6 +- src/libsyntax/ext/source_util.rs | 8 +- src/libsyntax/parse.rs | 4 +- src/libsyntax/parse/lexer.rs | 8 +- src/libsyntax/print/pprust.rs | 4 +- 17 files changed, 195 insertions(+), 193 deletions(-) diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index f1c64a7e0995f..4879ae30699a1 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -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 = diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 07fc637a04933..5aed12dba9594 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -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(_, _) => { diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index ec7c2ffc6f1da..ed9549cedd7e6 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -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" } } diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index f509c15ab5f8d..4941885cafb54 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -919,7 +919,7 @@ fn trans_trace(bcx: block, sp_opt: Option, 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} } diff --git a/src/librustc/middle/trans/build.rs b/src/librustc/middle/trans/build.rs index f7690b7bc9300..f980990517495 100644 --- a/src/librustc/middle/trans/build.rs +++ b/src/librustc/middle/trans/build.rs @@ -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); diff --git a/src/librustc/middle/trans/controlflow.rs b/src/librustc/middle/trans/controlflow.rs index 59a733433bf22..0c09b02bb07b1 100644 --- a/src/librustc/middle/trans/controlflow.rs +++ b/src/librustc/middle/trans/controlflow.rs @@ -339,7 +339,7 @@ fn trans_fail_value(bcx: block, sp_opt: Option, 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())); diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index c794b0189fc2e..2575141f7c45b 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -230,7 +230,7 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata { } 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 { @@ -244,9 +244,9 @@ fn create_block(cx: block) -> @metadata { } 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>( 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 { 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 { 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 { diff --git a/src/librustc/middle/typeck/infer/region_inference.rs b/src/librustc/middle/typeck/infer/region_inference.rs index bdc764a8d0b09..652d99779d312 100644 --- a/src/librustc/middle/typeck/infer/region_inference.rs +++ b/src/librustc/middle/typeck/infer/region_inference.rs @@ -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; } diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index a9b67a7ff29c9..7ef71c0cc2fc6 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -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) { - 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!("", - 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!("", - codemap::span_to_str(expr.span, cx.sess.codemap)) + cx.sess.codemap.span_to_str(expr.span)) } ast::expr_match(*) => { fmt!("", - 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!("", - codemap::span_to_str(expr.span, cx.sess.codemap)) + cx.sess.codemap.span_to_str(expr.span)) } _ => { fmt!("", - codemap::span_to_str(expr.span, cx.sess.codemap)) + cx.sess.codemap.span_to_str(expr.span)) } } } diff --git a/src/librustdoc/astsrv.rs b/src/librustdoc/astsrv.rs index 480fc6c3914b4..cb97d38b20854 100644 --- a/src/librustdoc/astsrv.rs +++ b/src/librustdoc/astsrv.rs @@ -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; diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 6a0e8063119e8..f204cf89e1b57 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 9335faed0c987..66c59683813a8 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 af7ffaa73f5bf..6ae083779cd1b 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -204,13 +204,13 @@ fn finish -> @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 3181a6044007c..8c5048b4c6f7a 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 f93be9c7266c8..3f15a5b6e8864 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) -> 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 f3e84a0cbf8ea..1c984ad57b1f9 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 2bf9b826d6fae..8fa7543b2d133 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 && From b22f94148720e1ece7e16afe92f91d998ac9af34 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 18:35:17 -0800 Subject: [PATCH 11/56] Make it clearer which codemap functions use chars vs. bytes --- src/libsyntax/codemap.rs | 43 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index f204cf89e1b57..bac1c5fcf6f9e 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -10,9 +10,12 @@ use std::serialization::{Serializable, Serializer, Deserializer}; +pub type byte_pos = uint; +pub type char_pos = uint; + pub struct span { - lo: uint, - hi: uint, + lo: char_pos, + hi: char_pos, expn_info: Option<@expn_info> } @@ -34,23 +37,14 @@ impl span: Deserializable { } } -pub enum expn_info { - expanded_from({call_site: span, - callie: {name: ~str, span: Option}}) +pub struct file_pos { + ch: char_pos, byte: byte_pos } -pub type filename = ~str; - -pub type lookup_fn = pure fn(file_pos) -> uint; - pub struct loc { file: @filemap, line: uint, col: uint } -pub struct file_pos { - ch: uint, byte: uint -} - impl file_pos : cmp::Eq { pure fn eq(other: &file_pos) -> bool { self.ch == (*other).ch && self.byte == (*other).byte @@ -58,6 +52,15 @@ impl file_pos : cmp::Eq { pure fn ne(other: &file_pos) -> bool { !self.eq(other) } } +pub enum expn_info { + expanded_from({call_site: span, + callie: {name: ~str, span: Option}}) +} + +pub type filename = ~str; + +pub type lookup_fn = pure fn(file_pos) -> uint; + pub struct file_lines { file: @filemap, lines: ~[uint] @@ -90,13 +93,13 @@ pub impl filemap { } static fn new(+filename: filename, src: @~str, - start_pos_ch: uint, start_pos_byte: uint) + start_pos_ch: char_pos, start_pos_byte: byte_pos) -> 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) { + fn next_line(@self, chpos: char_pos, byte_pos: byte_pos) { self.lines.push(file_pos {ch: chpos, byte: byte_pos + self.start_pos.byte}); } @@ -127,17 +130,17 @@ pub impl CodeMap { return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); } - pub fn lookup_char_pos(@self, pos: uint) -> loc { + pub fn lookup_char_pos(@self, pos: char_pos) -> loc { pure fn lookup(pos: file_pos) -> uint { return pos.ch; } return self.lookup_pos(pos, lookup); } - pub fn lookup_byte_pos(@self, pos: uint) -> loc { + pub fn lookup_byte_pos(@self, pos: byte_pos) -> loc { pure fn lookup(pos: file_pos) -> uint { return pos.byte; } return self.lookup_pos(pos, lookup); } - pub fn lookup_char_pos_adj(@self, pos: uint) + pub fn lookup_char_pos_adj(@self, pos: char_pos) -> {filename: ~str, line: uint, col: uint, file: Option<@filemap>} { let loc = self.lookup_char_pos(pos); @@ -195,8 +198,8 @@ pub impl CodeMap { return @file_lines {file: lo.file, lines: lines}; } - fn lookup_byte_offset(@self, chpos: uint) - -> {fm: @filemap, pos: uint} { + fn lookup_byte_offset(@self, chpos: char_pos) + -> {fm: @filemap, pos: byte_pos} { 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; From 38b9740668f596989a2a714e556d59d5b49112dc Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 18:36:03 -0800 Subject: [PATCH 12/56] Remove unused get_snippet function from codemap --- src/libsyntax/codemap.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index bac1c5fcf6f9e..4f6c16a39d76c 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -215,12 +215,6 @@ pub impl CodeMap { 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 From 9ecf86343a136c71cbb2bb8da9bfd1734fec37f4 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 18:59:37 -0800 Subject: [PATCH 13/56] Camel case all the codemap types except span --- src/librustc/middle/trans/debuginfo.rs | 2 +- src/librustdoc/attr_parser.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/codemap.rs | 130 ++++++++++++------------- src/libsyntax/diagnostic.rs | 2 +- src/libsyntax/ext/base.rs | 18 ++-- src/libsyntax/ext/expand.rs | 10 +- src/libsyntax/ext/qquote.rs | 6 +- src/libsyntax/ext/source_util.rs | 4 +- src/libsyntax/parse.rs | 20 ++-- src/libsyntax/parse/comments.rs | 4 +- src/libsyntax/parse/lexer.rs | 6 +- src/libsyntax/parse/parser.rs | 2 +- 13 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index 2575141f7c45b..ecf060afc93ac 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -112,7 +112,7 @@ type compile_unit_md = {name: ~str}; type subprogram_md = {id: ast::node_id}; type local_var_md = {id: ast::node_id}; type tydesc_md = {hash: uint}; -type block_md = {start: codemap::loc, end: codemap::loc}; +type block_md = {start: codemap::Loc, end: codemap::Loc}; type argument_md = {id: ast::node_id}; type retval_md = {id: ast::node_id}; diff --git a/src/librustdoc/attr_parser.rs b/src/librustdoc/attr_parser.rs index 2b16112fe16c7..4e8b11d2ca6fa 100644 --- a/src/librustdoc/attr_parser.rs +++ b/src/librustdoc/attr_parser.rs @@ -30,7 +30,7 @@ mod test { let parse_sess = syntax::parse::new_parse_sess(None); let parser = parse::new_parser_from_source_str( - parse_sess, ~[], ~"-", codemap::fss_none, @source); + parse_sess, ~[], ~"-", codemap::FssNone, @source); parser.parse_outer_attributes() } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 7452e41fac337..4455db0882695 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -4,7 +4,7 @@ use std::serialization::{Serializable, Deserializable, Serializer, Deserializer}; -use codemap::{span, filename}; +use codemap::{span, FileName}; use parse::token; #[auto_serialize] diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 4f6c16a39d76c..87328f25ca8c1 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -10,13 +10,13 @@ use std::serialization::{Serializable, Serializer, Deserializer}; -pub type byte_pos = uint; -pub type char_pos = uint; +pub type BytePos = uint; +pub type CharPos = uint; pub struct span { - lo: char_pos, - hi: char_pos, - expn_info: Option<@expn_info> + lo: CharPos, + hi: CharPos, + expn_info: Option<@ExpnInfo> } impl span : cmp::Eq { @@ -37,70 +37,70 @@ impl span: Deserializable { } } -pub struct file_pos { - ch: char_pos, byte: byte_pos +pub struct Loc { + file: @FileMap, line: uint, col: uint } -pub struct loc { - file: @filemap, line: uint, col: uint +pub struct FilePos { + ch: CharPos, byte: BytePos } -impl file_pos : cmp::Eq { - pure fn eq(other: &file_pos) -> bool { +impl FilePos : cmp::Eq { + pure fn eq(other: &FilePos) -> bool { self.ch == (*other).ch && self.byte == (*other).byte } - pure fn ne(other: &file_pos) -> bool { !self.eq(other) } + pure fn ne(other: &FilePos) -> bool { !self.eq(other) } } -pub enum expn_info { - expanded_from({call_site: span, - callie: {name: ~str, span: Option}}) +pub enum ExpnInfo { + ExpandedFrom({call_site: span, + callie: {name: ~str, span: Option}}) } -pub type filename = ~str; +pub type FileName = ~str; -pub type lookup_fn = pure fn(file_pos) -> uint; +pub type LookupFn = pure fn(FilePos) -> uint; -pub struct file_lines { - file: @filemap, +pub struct FileLines { + file: @FileMap, lines: ~[uint] } -pub enum file_substr { - pub fss_none, - pub fss_internal(span), - pub fss_external({filename: ~str, line: uint, col: uint}) +pub enum FileSubstr { + pub FssNone, + pub FssInternal(span), + pub FssExternal({filename: ~str, line: uint, col: uint}) } -pub struct filemap { - name: filename, - substr: file_substr, +pub struct FileMap { + name: FileName, + substr: FileSubstr, src: @~str, - start_pos: file_pos, - mut lines: ~[file_pos] + start_pos: FilePos, + mut lines: ~[FilePos] } -pub impl filemap { - static fn new_w_substr(+filename: filename, +substr: file_substr, +pub impl FileMap { + static fn new_w_substr(+filename: FileName, +substr: FileSubstr, src: @~str, start_pos_ch: uint, start_pos_byte: uint) - -> filemap { - return filemap { + -> FileMap { + return FileMap { name: filename, substr: substr, src: src, - start_pos: file_pos {ch: start_pos_ch, byte: start_pos_byte}, - mut lines: ~[file_pos {ch: start_pos_ch, byte: start_pos_byte}] + start_pos: FilePos {ch: start_pos_ch, byte: start_pos_byte}, + mut lines: ~[FilePos {ch: start_pos_ch, byte: start_pos_byte}] }; } - static fn new(+filename: filename, src: @~str, - start_pos_ch: char_pos, start_pos_byte: byte_pos) - -> filemap { - return filemap::new_w_substr(filename, fss_none, src, + static fn new(+filename: FileName, src: @~str, + start_pos_ch: CharPos, start_pos_byte: BytePos) + -> FileMap { + return FileMap::new_w_substr(filename, FssNone, src, start_pos_ch, start_pos_byte); } - fn next_line(@self, chpos: char_pos, byte_pos: byte_pos) { - self.lines.push(file_pos {ch: chpos, byte: byte_pos + self.start_pos.byte}); + fn next_line(@self, chpos: CharPos, byte_pos: BytePos) { + self.lines.push(FilePos {ch: chpos, byte: byte_pos + self.start_pos.byte}); } pub fn get_line(@self, line: int) -> ~str unsafe { @@ -115,7 +115,7 @@ pub impl filemap { } pub struct CodeMap { - files: DVec<@filemap> + files: DVec<@FileMap> } pub impl CodeMap { @@ -130,31 +130,31 @@ pub impl CodeMap { return fmt!("<%s:%u:%u>", pos.file.name, pos.line, pos.col); } - pub fn lookup_char_pos(@self, pos: char_pos) -> loc { - pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + pub fn lookup_char_pos(@self, pos: CharPos) -> Loc { + pure fn lookup(pos: FilePos) -> uint { return pos.ch; } return self.lookup_pos(pos, lookup); } - pub fn lookup_byte_pos(@self, pos: byte_pos) -> loc { - pure fn lookup(pos: file_pos) -> uint { return pos.byte; } + pub fn lookup_byte_pos(@self, pos: BytePos) -> Loc { + pure fn lookup(pos: FilePos) -> uint { return pos.byte; } return self.lookup_pos(pos, lookup); } - pub fn lookup_char_pos_adj(@self, pos: char_pos) - -> {filename: ~str, line: uint, col: uint, file: Option<@filemap>} + pub fn lookup_char_pos_adj(@self, pos: CharPos) + -> {filename: ~str, line: uint, col: uint, file: Option<@FileMap>} { let loc = self.lookup_char_pos(pos); match (loc.file.substr) { - fss_none => { + FssNone => { {filename: /* FIXME (#2543) */ copy loc.file.name, line: loc.line, col: loc.col, file: Some(loc.file)} } - fss_internal(sp) => { + FssInternal(sp) => { self.lookup_char_pos_adj(sp.lo + (pos - loc.file.start_pos.ch)) } - fss_external(eloc) => { + FssExternal(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}, @@ -164,15 +164,15 @@ pub impl CodeMap { } pub fn adjust_span(@self, sp: span) -> span { - pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + pure fn lookup(pos: FilePos) -> uint { return pos.ch; } let line = self.lookup_line(sp.lo, lookup); match (line.fm.substr) { - fss_none => sp, - fss_internal(s) => { + FssNone => sp, + FssInternal(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 + FssExternal(_) => sp } } @@ -183,24 +183,24 @@ pub impl CodeMap { lo.line, lo.col, hi.line, hi.col) } - pub fn span_to_filename(@self, sp: span) -> filename { + 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 { + pub fn span_to_lines(@self, sp: span) -> @FileLines { 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}; + return @FileLines {file: lo.file, lines: lines}; } - fn lookup_byte_offset(@self, chpos: char_pos) - -> {fm: @filemap, pos: byte_pos} { - pure fn lookup(pos: file_pos) -> uint { return pos.ch; } + fn lookup_byte_offset(@self, chpos: CharPos) + -> {fm: @FileMap, pos: BytePos} { + pure fn lookup(pos: FilePos) -> 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; @@ -215,7 +215,7 @@ pub impl CodeMap { return str::slice(*begin.fm.src, begin.pos, end.pos); } - pub fn get_filemap(@self, filename: ~str) -> @filemap { + 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 _|_) @@ -225,8 +225,8 @@ pub impl CodeMap { } priv impl CodeMap { - fn lookup_line(@self, pos: uint, lookup: lookup_fn) - -> {fm: @filemap, line: uint} + fn lookup_line(@self, pos: uint, lookup: LookupFn) + -> {fm: @FileMap, line: uint} { let len = self.files.len(); let mut a = 0u; @@ -248,9 +248,9 @@ priv impl CodeMap { return {fm: f, line: a}; } - fn lookup_pos(@self, pos: uint, lookup: lookup_fn) -> loc { + fn lookup_pos(@self, pos: uint, lookup: LookupFn) -> Loc { let {fm: f, line: a} = self.lookup_line(pos, lookup); - return loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; + return Loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; } fn span_to_str_no_adj(@self, sp: span) -> ~str { diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 66c59683813a8..22bed009c9af9 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -206,7 +206,7 @@ fn emit(cmsp: Option<(@codemap::CodeMap, span)>, msg: &str, lvl: level) { } fn highlight_lines(cm: @codemap::CodeMap, sp: span, - lines: @codemap::file_lines) { + lines: @codemap::FileLines) { let fm = lines.file; diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 127e4a13e27ab..ad33e99ff047a 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -1,7 +1,7 @@ use std::map::HashMap; use parse::parser; use diagnostic::span_handler; -use codemap::{CodeMap, span, expn_info, expanded_from}; +use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom}; use ast_util::dummy_sp; // obsolete old-style #macro code: @@ -129,11 +129,11 @@ trait ext_ctxt { fn parse_sess() -> parse::parse_sess; fn cfg() -> ast::crate_cfg; fn print_backtrace(); - fn backtrace() -> Option<@expn_info>; + fn backtrace() -> Option<@ExpnInfo>; fn mod_push(mod_name: ast::ident); fn mod_pop(); fn mod_path() -> ~[ast::ident]; - fn bt_push(ei: codemap::expn_info); + fn bt_push(ei: codemap::ExpnInfo); fn bt_pop(); fn span_fatal(sp: span, msg: &str) -> !; fn span_err(sp: span, msg: &str); @@ -153,7 +153,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, cfg: ast::crate_cfg) -> ext_ctxt { type ctxt_repr = {parse_sess: parse::parse_sess, cfg: ast::crate_cfg, - mut backtrace: Option<@expn_info>, + mut backtrace: Option<@ExpnInfo>, mut mod_path: ~[ast::ident], mut trace_mac: bool}; impl ctxt_repr: ext_ctxt { @@ -161,15 +161,15 @@ fn mk_ctxt(parse_sess: parse::parse_sess, fn parse_sess() -> parse::parse_sess { self.parse_sess } fn cfg() -> ast::crate_cfg { self.cfg } fn print_backtrace() { } - fn backtrace() -> Option<@expn_info> { self.backtrace } + fn backtrace() -> Option<@ExpnInfo> { self.backtrace } fn mod_push(i: ast::ident) { self.mod_path.push(i); } fn mod_pop() { self.mod_path.pop(); } fn mod_path() -> ~[ast::ident] { return self.mod_path; } - fn bt_push(ei: codemap::expn_info) { + fn bt_push(ei: codemap::ExpnInfo) { match ei { - expanded_from({call_site: cs, callie: callie}) => { + ExpandedFrom({call_site: cs, callie: callie}) => { self.backtrace = - Some(@expanded_from({ + Some(@ExpandedFrom({ call_site: span {lo: cs.lo, hi: cs.hi, expn_info: self.backtrace}, callie: callie})); @@ -178,7 +178,7 @@ fn mk_ctxt(parse_sess: parse::parse_sess, } fn bt_pop() { match self.backtrace { - Some(@expanded_from({call_site: span {expn_info: prev, _}, _})) => { + Some(@ExpandedFrom({call_site: span {expn_info: prev, _}, _})) => { self.backtrace = prev } _ => self.bug(~"tried to pop without a push") diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index ca054255fa643..69d067f1ddb0a 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -8,7 +8,7 @@ use ext::qquote::{qq_helper}; use parse::{parser, parse_expr_from_source_str, new_parser_from_tt}; -use codemap::{span, expanded_from}; +use codemap::{span, ExpandedFrom}; fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, e: expr_, s: span, fld: ast_fold, @@ -41,7 +41,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, Some(normal({expander: exp, span: exp_sp})) => { let expanded = exp(cx, mac.span, args, body); - cx.bt_push(expanded_from({call_site: s, + cx.bt_push(ExpandedFrom({call_site: s, callie: {name: *extname, span: exp_sp}})); //keep going, outside-in let fully_expanded = fld.fold_expr(expanded).node; @@ -86,7 +86,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, *extname)) }; - cx.bt_push(expanded_from({call_site: s, + cx.bt_push(ExpandedFrom({call_site: s, callie: {name: *extname, span: exp_sp}})); //keep going, outside-in let fully_expanded = fld.fold_expr(expanded).node; @@ -100,7 +100,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt, tts); let expanded = exp(cx, mac.span, arg, None); - cx.bt_push(expanded_from({call_site: s, + cx.bt_push(ExpandedFrom({call_site: s, callie: {name: *extname, span: exp_sp}})); //keep going, outside-in let fully_expanded = fld.fold_expr(expanded).node; @@ -206,7 +206,7 @@ fn expand_item_mac(exts: HashMap<~str, syntax_extension>, } Some(item_tt(expand)) => { let expanded = expand.expander(cx, it.span, it.ident, tts); - cx.bt_push(expanded_from({call_site: it.span, + cx.bt_push(ExpandedFrom({call_site: it.span, callie: {name: *extname, span: expand.span}})); let maybe_it = match expanded { diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index 6ae083779cd1b..1d17b09329c12 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -208,7 +208,7 @@ fn finish debug!("qquote--str==%?", str); let fname = cm.mk_substr_filename(body.span); let node = parse_from_source_str - (f, fname, codemap::fss_internal(body.span), str, + (f, fname, codemap::FssInternal(body.span), str, ecx.cfg(), ecx.parse_sess()); let loc = cm.lookup_char_pos(body.span.lo); @@ -345,8 +345,8 @@ fn replace_ty(repls: ~[fragment], } fn mk_file_substr(fname: ~str, line: uint, col: uint) -> - codemap::file_substr { - codemap::fss_external({filename: fname, line: line, col: col}) + codemap::FileSubstr { + codemap::FssExternal({filename: fname, line: line, col: col}) } // Local Variables: diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 8c5048b4c6f7a..3e4f24dff0283 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -1,5 +1,5 @@ use base::*; -use codemap::{span, loc, filemap}; +use codemap::{span, Loc, FileMap}; use print::pprust; use build::{mk_base_vec_e,mk_uint,mk_u8,mk_uniq_str}; @@ -34,7 +34,7 @@ fn expand_col(cx: ext_ctxt, sp: span, arg: ast::mac_arg, 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, _ }, _ } = + let Loc { file: @FileMap { name: filename, _ }, _ } = cx.codemap().lookup_char_pos(sp.lo); return mk_uniq_str(cx, sp, filename); } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 3f15a5b6e8864..01461a8522075 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::{CodeMap, filemap}; +use codemap::{CodeMap, FileMap}; type parse_sess = @{ cm: @codemap::CodeMap, @@ -100,7 +100,7 @@ fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::fss_none, source); + codemap::FssNone, source); let r = p.parse_crate_mod(cfg); p.abort_if_errors(); sess.chpos = rdr.chpos; @@ -111,7 +111,7 @@ fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::expr { let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::fss_none, source); + codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); sess.chpos = rdr.chpos; @@ -123,7 +123,7 @@ fn parse_item_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, +attrs: ~[ast::attribute], sess: parse_sess) -> Option<@ast::item> { let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::fss_none, source); + codemap::FssNone, source); let r = p.parse_item(attrs); p.abort_if_errors(); sess.chpos = rdr.chpos; @@ -135,7 +135,7 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, +attrs: ~[ast::attribute], sess: parse_sess) -> @ast::stmt { let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::fss_none, source); + codemap::FssNone, source); let r = p.parse_stmt(attrs); p.abort_if_errors(); sess.chpos = rdr.chpos; @@ -144,7 +144,7 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, } fn parse_from_source_str(f: fn (p: Parser) -> T, - name: ~str, ss: codemap::file_substr, + name: ~str, ss: codemap::FileSubstr, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) -> T @@ -170,10 +170,10 @@ fn next_node_id(sess: parse_sess) -> node_id { } fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, - +name: ~str, +ss: codemap::file_substr, + +name: ~str, +ss: codemap::FileSubstr, source: @~str) -> (Parser, string_reader) { let ftype = parser::SOURCE_FILE; - let filemap = @filemap::new_w_substr + let filemap = @FileMap::new_w_substr (name, ss, source, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, @@ -182,7 +182,7 @@ fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, } fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, - +name: ~str, +ss: codemap::file_substr, + +name: ~str, +ss: codemap::FileSubstr, source: @~str) -> Parser { let (p, _) = new_parser_etc_from_source_str(sess, cfg, name, ss, source); move p @@ -198,7 +198,7 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, result::Err(e) => sess.span_diagnostic.handler().fatal(e) } let src = @result::unwrap(res); - let filemap = @filemap::new(path.to_str(), src, + let filemap = @FileMap::new(path.to_str(), src, sess.chpos, sess.byte_pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 0ed8bd12361e5..a2f73fd189ca5 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -3,7 +3,7 @@ use io::ReaderUtil; use util::interner; use lexer::{string_reader, bump, is_eof, nextch, is_whitespace, get_str_from, reader}; -use codemap::filemap; +use codemap::FileMap; export cmnt; export lit; @@ -289,7 +289,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); let rdr = lexer::new_low_level_string_reader - (span_diagnostic, @filemap::new(path, src, 0u, 0u), itr); + (span_diagnostic, @FileMap::new(path, src, 0u, 0u), itr); let mut comments: ~[cmnt] = ~[]; let mut literals: ~[lit] = ~[]; diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 1c984ad57b1f9..06b8b1e8eeeed 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -25,7 +25,7 @@ type string_reader = @{ mut pos: uint, mut curr: char, mut chpos: uint, - filemap: @codemap::filemap, + filemap: @codemap::FileMap, interner: @token::ident_interner, /* cached: */ mut peek_tok: token::Token, @@ -33,7 +33,7 @@ type string_reader = @{ }; fn new_string_reader(span_diagnostic: span_handler, - filemap: @codemap::filemap, + filemap: @codemap::FileMap, itr: @token::ident_interner) -> string_reader { let r = new_low_level_string_reader(span_diagnostic, filemap, itr); string_advance_token(r); /* fill in peek_* */ @@ -42,7 +42,7 @@ fn new_string_reader(span_diagnostic: span_handler, /* For comments.rs, which hackily pokes into 'pos' and 'curr' */ fn new_low_level_string_reader(span_diagnostic: span_handler, - filemap: @codemap::filemap, + filemap: @codemap::FileMap, itr: @token::ident_interner) -> string_reader { let r = @{span_diagnostic: span_diagnostic, src: filemap.src, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9df6b7ab5d2f0..43628b0908fcf 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5,7 +5,7 @@ use either::{Either, Left, Right}; use std::map::HashMap; use token::{can_begin_expr, is_ident, is_ident_or_path, is_plain_ident, INTERPOLATED, special_idents}; -use codemap::{span,fss_none}; +use codemap::{span,FssNone}; use util::interner::Interner; use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec}; use lexer::reader; From f67bfe97389a256fc95216c29a2b8a066ee16a2c Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 12 Nov 2012 19:32:48 -0800 Subject: [PATCH 14/56] Add types for character position and byte position in the codemap --- src/librustc/middle/trans/debuginfo.rs | 12 +- src/librustc/util/ppaux.rs | 3 +- src/libsyntax/ast_util.rs | 8 +- src/libsyntax/attr.rs | 4 +- src/libsyntax/codemap.rs | 192 ++++++++++++++++++++----- src/libsyntax/diagnostic.rs | 4 +- src/libsyntax/ext/base.rs | 4 +- src/libsyntax/ext/pipes/ast_builder.rs | 55 +++---- src/libsyntax/ext/pipes/check.rs | 1 - src/libsyntax/ext/pipes/liveness.rs | 2 - src/libsyntax/ext/pipes/pipec.rs | 13 +- src/libsyntax/ext/qquote.rs | 18 +-- src/libsyntax/ext/source_util.rs | 2 +- src/libsyntax/ext/tt/macro_parser.rs | 5 +- src/libsyntax/ext/tt/transcribe.rs | 2 +- src/libsyntax/parse.rs | 10 +- src/libsyntax/parse/attr.rs | 4 +- src/libsyntax/parse/comments.rs | 19 ++- src/libsyntax/parse/common.rs | 2 +- src/libsyntax/parse/lexer.rs | 50 +++---- src/libsyntax/parse/parser.rs | 22 +-- src/libsyntax/print/pprust.rs | 14 +- 22 files changed, 285 insertions(+), 161 deletions(-) diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index ecf060afc93ac..15f8b18a9f53a 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -8,7 +8,7 @@ use trans::build::B; use middle::ty; use syntax::{ast, codemap, ast_util, ast_map}; use syntax::parse::token::ident_interner; -use codemap::span; +use codemap::{span, CharPos}; use ast::Ty; use pat_util::*; use util::ppaux::ty_to_str; @@ -112,7 +112,7 @@ type compile_unit_md = {name: ~str}; type subprogram_md = {id: ast::node_id}; type local_var_md = {id: ast::node_id}; type tydesc_md = {hash: uint}; -type block_md = {start: codemap::Loc, end: codemap::Loc}; +type block_md = {start: codemap::Loc, end: codemap::Loc}; type argument_md = {id: ast::node_id}; type retval_md = {id: ast::node_id}; @@ -266,8 +266,8 @@ fn create_block(cx: block) -> @metadata { }; let lldata = ~[lltag(tg), parent, - lli32(start.line as int), - lli32(start.col as int), + lli32(start.line.to_int()), + lli32(start.col.to_int()), file_node.node, lli32(unique_id) ]; @@ -713,8 +713,8 @@ fn update_source_pos(cx: block, s: span) { let cm = cx.sess().codemap; let blockmd = create_block(cx); let loc = cm.lookup_char_pos(s.lo); - let scopedata = ~[lli32(loc.line as int), - lli32(loc.col as int), + let scopedata = ~[lli32(loc.line.to_int()), + lli32(loc.col.to_int()), blockmd.node, llnull()]; let dbgscope = llmdnode(scopedata); diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 7ef71c0cc2fc6..198b26c4ecc69 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -106,7 +106,8 @@ fn explain_region_and_span(cx: ctxt, region: ty::Region) -> (~str, Option) { let lo = cx.sess.codemap.lookup_char_pos_adj(span.lo); - (fmt!("the %s at %u:%u", heading, lo.line, lo.col), Some(span)) + (fmt!("the %s at %u:%u", heading, + lo.line, lo.col.to_uint()), Some(span)) } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d406c98d884a9..6abcef5c70808 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -1,7 +1,7 @@ -use codemap::span; +use codemap::{span, CharPos}; use ast::*; -pure fn spanned(lo: uint, hi: uint, +t: T) -> spanned { +pure fn spanned(+lo: CharPos, +hi: CharPos, +t: T) -> spanned { respan(mk_sp(lo, hi), move t) } @@ -14,12 +14,12 @@ pure fn dummy_spanned(+t: T) -> spanned { } /* assuming that we're not in macro expansion */ -pure fn mk_sp(lo: uint, hi: uint) -> span { +pure fn mk_sp(+lo: CharPos, +hi: CharPos) -> span { span {lo: lo, hi: hi, expn_info: None} } // make this a const, once the compiler supports it -pure fn dummy_sp() -> span { return mk_sp(0u, 0u); } +pure fn dummy_sp() -> span { return mk_sp(CharPos(0), CharPos(0)); } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 8c19814350ca7..3b6bae6410466 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -6,6 +6,7 @@ use either::Either; use diagnostic::span_handler; use ast_util::{spanned, dummy_spanned}; use parse::comments::{doc_comment_style, strip_doc_comment_decoration}; +use codemap::CharPos; // Constructors export mk_name_value_item_str; @@ -74,7 +75,8 @@ fn mk_attr(item: @ast::meta_item) -> ast::attribute { is_sugared_doc: false}); } -fn mk_sugared_doc_attr(text: ~str, lo: uint, hi: uint) -> ast::attribute { +fn mk_sugared_doc_attr(text: ~str, + +lo: CharPos, +hi: CharPos) -> ast::attribute { let lit = spanned(lo, hi, ast::lit_str(@text)); let attr = { style: doc_comment_style(text), diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 87328f25ca8c1..8333849b21dda 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -10,8 +10,109 @@ use std::serialization::{Serializable, Serializer, Deserializer}; -pub type BytePos = uint; -pub type CharPos = uint; +trait Pos { + static pure fn from_uint(n: uint) -> self; + pure fn to_uint(&self) -> uint; +} + +pub enum BytePos = uint; +pub enum CharPos = uint; + +impl BytePos: Pos { + static pure fn from_uint(n: uint) -> BytePos { BytePos(n) } + pure fn to_uint(&self) -> uint { **self } +} + +impl BytePos: cmp::Eq { + pure fn eq(other: &BytePos) -> bool { + *self == **other + } + pure fn ne(other: &BytePos) -> bool { !self.eq(other) } +} + +impl BytePos: cmp::Ord { + pure fn lt(other: &BytePos) -> bool { *self < **other } + pure fn le(other: &BytePos) -> bool { *self <= **other } + pure fn ge(other: &BytePos) -> bool { *self >= **other } + pure fn gt(other: &BytePos) -> bool { *self > **other } +} + +impl BytePos: Num { + pure fn add(other: &BytePos) -> BytePos { + BytePos(*self + **other) + } + pure fn sub(other: &BytePos) -> BytePos { + BytePos(*self - **other) + } + pure fn mul(other: &BytePos) -> BytePos { + BytePos(*self * (**other)) + } + pure fn div(other: &BytePos) -> BytePos { + BytePos(*self / **other) + } + pure fn modulo(other: &BytePos) -> BytePos { + BytePos(*self % **other) + } + pure fn neg() -> BytePos { + BytePos(-*self) + } + pure fn to_int() -> int { *self as int } + static pure fn from_int(+n: int) -> BytePos { BytePos(n as uint) } +} + +impl BytePos: to_bytes::IterBytes { + pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { + (*self).iter_bytes(lsb0, f) + } +} + +impl CharPos: Pos { + static pure fn from_uint(n: uint) -> CharPos { CharPos(n) } + pure fn to_uint(&self) -> uint { **self } +} + +impl CharPos: cmp::Eq { + pure fn eq(other: &CharPos) -> bool { + *self == **other + } + pure fn ne(other: &CharPos) -> bool { !self.eq(other) } +} + +impl CharPos: cmp::Ord { + pure fn lt(other: &CharPos) -> bool { *self < **other } + pure fn le(other: &CharPos) -> bool { *self <= **other } + pure fn ge(other: &CharPos) -> bool { *self >= **other } + pure fn gt(other: &CharPos) -> bool { *self > **other } +} + +impl CharPos: Num { + pure fn add(other: &CharPos) -> CharPos { + CharPos(*self + **other) + } + pure fn sub(other: &CharPos) -> CharPos { + CharPos(*self - **other) + } + pure fn mul(other: &CharPos) -> CharPos { + CharPos(*self * (**other)) + } + pure fn div(other: &CharPos) -> CharPos { + CharPos(*self / **other) + } + pure fn modulo(other: &CharPos) -> CharPos { + CharPos(*self % **other) + } + pure fn neg() -> CharPos { + CharPos(-*self) + } + pure fn to_int() -> int { *self as int } + static pure fn from_int(+n: int) -> CharPos { CharPos(n as uint) } +} + +impl CharPos: to_bytes::IterBytes { + pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { + (*self).iter_bytes(lsb0, f) + } +} pub struct span { lo: CharPos, @@ -37,8 +138,8 @@ impl span: Deserializable { } } -pub struct Loc { - file: @FileMap, line: uint, col: uint +pub struct Loc { + file: @FileMap, line: uint, col: A } pub struct FilePos { @@ -69,7 +170,7 @@ pub struct FileLines { pub enum FileSubstr { pub FssNone, pub FssInternal(span), - pub FssExternal({filename: ~str, line: uint, col: uint}) + pub FssExternal({filename: ~str, line: uint, col: CharPos}) } pub struct FileMap { @@ -83,7 +184,7 @@ pub struct FileMap { pub impl FileMap { static fn new_w_substr(+filename: FileName, +substr: FileSubstr, src: @~str, - start_pos_ch: uint, start_pos_byte: uint) + +start_pos_ch: CharPos, +start_pos_byte: BytePos) -> FileMap { return FileMap { name: filename, substr: substr, src: src, @@ -93,18 +194,22 @@ pub impl FileMap { } static fn new(+filename: FileName, src: @~str, - start_pos_ch: CharPos, start_pos_byte: BytePos) + +start_pos_ch: CharPos, +start_pos_byte: BytePos) -> FileMap { return FileMap::new_w_substr(filename, FssNone, src, start_pos_ch, start_pos_byte); } - fn next_line(@self, chpos: CharPos, byte_pos: BytePos) { - self.lines.push(FilePos {ch: chpos, byte: byte_pos + self.start_pos.byte}); + fn next_line(@self, +chpos: CharPos, +byte_pos: BytePos) { + self.lines.push(FilePos { + 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 begin: BytePos = self.lines[line].byte - self.start_pos.byte; + let begin = begin.to_uint(); let end = match str::find_char_from(*self.src, '\n', begin) { Some(e) => e, None => str::len(*self.src) @@ -127,21 +232,22 @@ pub impl CodeMap { 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 fmt!("<%s:%u:%u>", pos.file.name, + pos.line, pos.col.to_uint()); } - pub fn lookup_char_pos(@self, pos: CharPos) -> Loc { - pure fn lookup(pos: FilePos) -> uint { return pos.ch; } + pub fn lookup_char_pos(@self, +pos: CharPos) -> Loc { + pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } return self.lookup_pos(pos, lookup); } - pub fn lookup_byte_pos(@self, pos: BytePos) -> Loc { - pure fn lookup(pos: FilePos) -> uint { return pos.byte; } + pub fn lookup_byte_pos(@self, +pos: BytePos) -> Loc { + pure fn lookup(pos: FilePos) -> uint { return pos.byte.to_uint(); } return self.lookup_pos(pos, lookup); } - pub fn lookup_char_pos_adj(@self, pos: CharPos) - -> {filename: ~str, line: uint, col: uint, file: Option<@FileMap>} + pub fn lookup_char_pos_adj(@self, +pos: CharPos) + -> {filename: ~str, line: uint, col: CharPos, file: Option<@FileMap>} { let loc = self.lookup_char_pos(pos); match (loc.file.substr) { @@ -152,7 +258,8 @@ pub impl CodeMap { file: Some(loc.file)} } FssInternal(sp) => { - self.lookup_char_pos_adj(sp.lo + (pos - loc.file.start_pos.ch)) + self.lookup_char_pos_adj( + sp.lo + (pos - loc.file.start_pos.ch)) } FssExternal(eloc) => { {filename: /* FIXME (#2543) */ copy eloc.filename, @@ -164,14 +271,17 @@ pub impl CodeMap { } pub fn adjust_span(@self, sp: span) -> span { - pure fn lookup(pos: FilePos) -> uint { return pos.ch; } + pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } let line = self.lookup_line(sp.lo, lookup); match (line.fm.substr) { FssNone => sp, FssInternal(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})} + 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 + }) + } FssExternal(_) => sp } } @@ -180,7 +290,7 @@ pub impl CodeMap { 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) + lo.line, lo.col.to_uint(), hi.line, hi.col.to_uint()) } pub fn span_to_filename(@self, sp: span) -> FileName { @@ -198,21 +308,24 @@ pub impl CodeMap { return @FileLines {file: lo.file, lines: lines}; } - fn lookup_byte_offset(@self, chpos: CharPos) + fn lookup_byte_offset(@self, +chpos: CharPos) -> {fm: @FileMap, pos: BytePos} { - pure fn lookup(pos: FilePos) -> uint { return pos.ch; } + pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } 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} + let col_offset = str::count_bytes(*fm.src, + line_offset.to_uint(), + col.to_uint()); + {fm: fm, pos: line_offset + BytePos(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); + return str::slice(*begin.fm.src, + begin.pos.to_uint(), end.pos.to_uint()); } pub fn get_filemap(@self, filename: ~str) -> @FileMap { @@ -225,7 +338,7 @@ pub impl CodeMap { } priv impl CodeMap { - fn lookup_line(@self, pos: uint, lookup: LookupFn) + fn lookup_line(@self, pos: A, lookup: LookupFn) -> {fm: @FileMap, line: uint} { let len = self.files.len(); @@ -233,31 +346,40 @@ priv impl CodeMap { 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 lookup(self.files[m].start_pos) > pos.to_uint() { + b = m; + } else { + a = m; + } } if (a >= len) { - fail fmt!("position %u does not resolve to a source location", pos) + fail fmt!("position %u does not resolve to a source location", + pos.to_uint()) } 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; } + if lookup(f.lines[m]) > pos.to_uint() { b = m; } else { a = m; } } return {fm: f, line: a}; } - fn lookup_pos(@self, pos: uint, lookup: LookupFn) -> Loc { + fn lookup_pos(@self, pos: A, lookup: LookupFn) -> Loc { let {fm: f, line: a} = self.lookup_line(pos, lookup); - return Loc {file: f, line: a + 1u, col: pos - lookup(f.lines[a])}; + return Loc { + file: f, + line: a + 1u, + col: pos - from_uint(lookup(f.lines[a])) + }; } 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) + lo.line, lo.col.to_uint(), hi.line, hi.col.to_uint()) } } diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 22bed009c9af9..007100856ebc4 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -245,7 +245,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, while num > 0u { num /= 10u; digits += 1u; } // indent past |name:## | and the 0-offset column location - let mut left = str::len(fm.name) + digits + lo.col + 3u; + let mut left = str::len(fm.name) + digits + lo.col.to_uint() + 3u; let mut s = ~""; while left > 0u { str::push_char(&mut s, ' '); left -= 1u; } @@ -253,7 +253,7 @@ fn highlight_lines(cm: @codemap::CodeMap, sp: span, 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; + let mut width = hi.col.to_uint() - lo.col.to_uint() - 1u; while width > 0u { str::push_char(&mut s, '~'); width -= 1u; } } io::stderr().write_str(s + ~"\n"); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index ad33e99ff047a..66ef3bec2a747 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -178,7 +178,9 @@ fn mk_ctxt(parse_sess: parse::parse_sess, } fn bt_pop() { match self.backtrace { - Some(@ExpandedFrom({call_site: span {expn_info: prev, _}, _})) => { + Some(@ExpandedFrom({ + call_site: span {expn_info: prev, _}, _ + })) => { self.backtrace = prev } _ => self.bug(~"tried to pop without a push") diff --git a/src/libsyntax/ext/pipes/ast_builder.rs b/src/libsyntax/ext/pipes/ast_builder.rs index 3d63b0ddb775d..652ad5533c4c7 100644 --- a/src/libsyntax/ext/pipes/ast_builder.rs +++ b/src/libsyntax/ext/pipes/ast_builder.rs @@ -4,7 +4,7 @@ // something smarter. use ast::{ident, node_id}; -use ast_util::{ident_to_path, respan}; +use ast_util::{ident_to_path, respan, dummy_sp}; use codemap::span; use ext::base::mk_ctxt; @@ -23,10 +23,6 @@ fn path(ids: ~[ident], span: span) -> @ast::path { types: ~[]} } -fn empty_span() -> span { - span {lo: 0, hi: 0, expn_info: None} -} - trait append_types { fn add_ty(ty: @ast::Ty) -> @ast::path; fn add_tys(+tys: ~[@ast::Ty]) -> @ast::path; @@ -83,26 +79,21 @@ trait ext_ctxt_ast_builder { fn stmt_let(ident: ident, e: @ast::expr) -> @ast::stmt; fn stmt_expr(e: @ast::expr) -> @ast::stmt; fn block_expr(b: ast::blk) -> @ast::expr; - fn empty_span() -> span; fn ty_option(ty: @ast::Ty) -> @ast::Ty; } impl ext_ctxt: ext_ctxt_ast_builder { fn ty_option(ty: @ast::Ty) -> @ast::Ty { self.ty_path_ast_builder(path(~[self.ident_of(~"Option")], - self.empty_span()) + dummy_sp()) .add_ty(ty)) } - fn empty_span() -> span { - span {lo: 0, hi: 0, expn_info: self.backtrace()} - } - fn block_expr(b: ast::blk) -> @ast::expr { @{id: self.next_id(), callee_id: self.next_id(), node: ast::expr_block(b), - span: self.empty_span()} + span: dummy_sp()} } fn move_expr(e: @ast::expr) -> @ast::expr { @@ -114,7 +105,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn stmt_expr(e: @ast::expr) -> @ast::stmt { @{node: ast::stmt_expr(e, self.next_id()), - span: self.empty_span()} + span: dummy_sp()} } fn stmt_let(ident: ident, e: @ast::expr) -> @ast::stmt { @@ -130,43 +121,43 @@ impl ext_ctxt: ext_ctxt_ast_builder { pat: @{id: self.next_id(), node: ast::pat_ident(ast::bind_by_implicit_ref, path(~[ident], - self.empty_span()), + dummy_sp()), None), - span: self.empty_span()}, + span: dummy_sp()}, init: Some(self.move_expr(e)), id: self.next_id()}, - span: self.empty_span()}]), - span: self.empty_span()}, self.next_id()), - span: self.empty_span()} + span: dummy_sp()}]), + span: dummy_sp()}, self.next_id()), + span: dummy_sp()} } fn field_imm(name: ident, e: @ast::expr) -> ast::field { {node: {mutbl: ast::m_imm, ident: name, expr: e}, - span: self.empty_span()} + span: dummy_sp()} } fn rec(+fields: ~[ast::field]) -> @ast::expr { @{id: self.next_id(), callee_id: self.next_id(), node: ast::expr_rec(fields, None), - span: self.empty_span()} + span: dummy_sp()} } fn ty_field_imm(name: ident, ty: @ast::Ty) -> ast::ty_field { {node: {ident: name, mt: { ty: ty, mutbl: ast::m_imm } }, - span: self.empty_span()} + span: dummy_sp()} } fn ty_rec(+fields: ~[ast::ty_field]) -> @ast::Ty { @{id: self.next_id(), node: ast::ty_rec(fields), - span: self.empty_span()} + span: dummy_sp()} } fn ty_infer() -> @ast::Ty { @{id: self.next_id(), node: ast::ty_infer, - span: self.empty_span()} + span: dummy_sp()} } fn ty_param(id: ast::ident, +bounds: ~[ast::ty_param_bound]) @@ -181,9 +172,9 @@ impl ext_ctxt: ext_ctxt_ast_builder { pat: @{id: self.next_id(), node: ast::pat_ident( ast::bind_by_value, - ast_util::ident_to_path(self.empty_span(), name), + ast_util::ident_to_path(dummy_sp(), name), None), - span: self.empty_span()}, + span: dummy_sp()}, id: self.next_id()} } @@ -195,7 +186,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { rules: ast::default_blk}; {node: blk, - span: self.empty_span()} + span: dummy_sp()} } fn expr_block(e: @ast::expr) -> ast::blk { @@ -215,11 +206,11 @@ impl ext_ctxt: ext_ctxt_ast_builder { // XXX: Would be nice if our generated code didn't violate // Rust coding conventions - let non_camel_case_attribute = respan(self.empty_span(), { + let non_camel_case_attribute = respan(dummy_sp(), { style: ast::attr_outer, - value: respan(self.empty_span(), + value: respan(dummy_sp(), ast::meta_list(~"allow", ~[ - @respan(self.empty_span(), + @respan(dummy_sp(), ast::meta_word(~"non_camel_case_types")) ])), is_sugared_doc: false @@ -239,7 +230,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { +ty_params: ~[ast::ty_param], +body: ast::blk) -> @ast::item { self.item(name, - self.empty_span(), + dummy_sp(), ast::item_fn(self.fn_decl(inputs, output), ast::impure_fn, ty_params, @@ -298,7 +289,7 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn ty_nil_ast_builder() -> @ast::Ty { @{id: self.next_id(), node: ast::ty_nil, - span: self.empty_span()} + span: dummy_sp()} } fn item_ty_poly(name: ident, @@ -314,6 +305,6 @@ impl ext_ctxt: ext_ctxt_ast_builder { fn ty_vars(+ty_params: ~[ast::ty_param]) -> ~[@ast::Ty] { ty_params.map(|p| self.ty_path_ast_builder( - path(~[p.ident], self.empty_span()))) + path(~[p.ident], dummy_sp()))) } } diff --git a/src/libsyntax/ext/pipes/check.rs b/src/libsyntax/ext/pipes/check.rs index fcc0c84a4ff39..a90b679f6974c 100644 --- a/src/libsyntax/ext/pipes/check.rs +++ b/src/libsyntax/ext/pipes/check.rs @@ -22,7 +22,6 @@ that. use ext::base::ext_ctxt; use proto::{state, protocol, next_state}; -use ast_builder::empty_span; impl ext_ctxt: proto::visitor<(), (), ()> { fn visit_proto(_proto: protocol, diff --git a/src/libsyntax/ext/pipes/liveness.rs b/src/libsyntax/ext/pipes/liveness.rs index a9bfd87ab0eb3..e86b3f0ea59e4 100644 --- a/src/libsyntax/ext/pipes/liveness.rs +++ b/src/libsyntax/ext/pipes/liveness.rs @@ -29,8 +29,6 @@ updating the states using rule (2) until there are no changes. use std::bitv::{Bitv}; -use ast_builder::empty_span; - fn analyze(proto: protocol, _cx: ext_ctxt) { debug!("initializing colive analysis"); let num_states = proto.num_states(); diff --git a/src/libsyntax/ext/pipes/pipec.rs b/src/libsyntax/ext/pipes/pipec.rs index 7e1cbe9ad0dbf..d03a0fde66c97 100644 --- a/src/libsyntax/ext/pipes/pipec.rs +++ b/src/libsyntax/ext/pipes/pipec.rs @@ -5,6 +5,7 @@ use to_str::ToStr; use dvec::DVec; use ast::ident; +use ast_util::dummy_sp; use util::interner; use print::pprust; use pprust::{item_to_str, ty_to_str}; @@ -12,7 +13,7 @@ use ext::base::{mk_ctxt, ext_ctxt}; use parse::*; use proto::*; -use ast_builder::{append_types, path, empty_span}; +use ast_builder::{append_types, path}; // Transitional reexports so qquote can find the paths it is looking for mod syntax { @@ -256,11 +257,11 @@ impl state: to_type_decls { cx.ty_path_ast_builder( path(~[cx.ident_of(~"pipes"), cx.ident_of(dir.to_str() + ~"Packet")], - empty_span()) + dummy_sp()) .add_ty(cx.ty_path_ast_builder( path(~[cx.ident_of(self.proto.name), self.data_name()], - empty_span()) + dummy_sp()) .add_tys(cx.ty_vars(self.ty_params))))), self.ty_params)); } @@ -273,11 +274,11 @@ impl state: to_type_decls { path(~[cx.ident_of(~"pipes"), cx.ident_of(dir.to_str() + ~"PacketBuffered")], - empty_span()) + dummy_sp()) .add_tys(~[cx.ty_path_ast_builder( path(~[cx.ident_of(self.proto.name), self.data_name()], - empty_span()) + dummy_sp()) .add_tys(cx.ty_vars(self.ty_params))), self.proto.buffer_ty_path(cx)])), self.ty_params)); @@ -394,7 +395,7 @@ impl protocol: gen_init { cx.item_ty_poly( cx.ident_of(~"__Buffer"), - cx.empty_span(), + dummy_sp(), cx.ty_rec(fields), params) } diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index 1d17b09329c12..50d1e0033e276 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -4,6 +4,7 @@ use parse::parser; use parse::parser::{Parser, parse_from_source_str}; use dvec::DVec; use parse::token::ident_interner; +use codemap::CharPos; use fold::*; use visit::*; @@ -15,13 +16,13 @@ use io::*; use codemap::span; struct gather_item { - lo: uint, - hi: uint, + lo: CharPos, + hi: CharPos, e: @ast::expr, constr: ~str } -type aq_ctxt = @{lo: uint, gather: DVec}; +type aq_ctxt = @{lo: CharPos, gather: DVec}; enum fragment { from_expr(@ast::expr), from_ty(@ast::Ty) @@ -114,7 +115,7 @@ impl @ast::pat: qq_helper { fn get_fold_fn() -> ~str {~"fold_pat"} } -fn gather_anti_quotes(lo: uint, node: N) -> aq_ctxt +fn gather_anti_quotes(lo: CharPos, node: N) -> aq_ctxt { let v = @{visit_expr: |node, &&cx, v| visit_aq(node, ~"from_expr", cx, v), visit_ty: |node, &&cx, v| visit_aq(node, ~"from_ty", cx, v), @@ -226,7 +227,8 @@ fn finish let mut str2 = ~""; enum state {active, skip(uint), blank}; let mut state = active; - let mut i = 0u, j = 0u; + let mut i = CharPos(0u); + let mut j = 0u; let g_len = cx.gather.len(); for str::chars_each(*str) |ch| { if (j < g_len && i == cx.gather[j].lo) { @@ -242,7 +244,7 @@ fn finish blank if is_space(ch) => str::push_char(&mut str2, ch), blank => str::push_char(&mut str2, ' ') } - i += 1u; + i += CharPos(1u); if (j < g_len && i == cx.gather[j].hi) { assert ch == ')'; state = active; @@ -270,7 +272,7 @@ fn finish ~"qquote", ~"mk_file_substr"]), ~[mk_uniq_str(cx,sp, loc.file.name), mk_uint(cx,sp, loc.line), - mk_uint(cx,sp, loc.col)]), + mk_uint(cx,sp, loc.col.to_uint())]), mk_unary(cx,sp, ast::box(ast::m_imm), mk_uniq_str(cx,sp, str2)), cfg_call(), @@ -346,7 +348,7 @@ fn replace_ty(repls: ~[fragment], fn mk_file_substr(fname: ~str, line: uint, col: uint) -> codemap::FileSubstr { - codemap::FssExternal({filename: fname, line: line, col: col}) + codemap::FssExternal({filename: fname, line: line, col: CharPos(col)}) } // Local Variables: diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 3e4f24dff0283..3fed340904a7a 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -25,7 +25,7 @@ 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 = cx.codemap().lookup_char_pos(sp.lo); - return mk_uint(cx, sp, loc.col); + return mk_uint(cx, sp, loc.col.to_uint()); } /* file!(): expands to the current filename */ diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 76814a688f5b7..2f371e1f8c581 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -11,6 +11,7 @@ use dvec::DVec; use ast::{matcher, match_tok, match_seq, match_nonterminal, ident}; use ast_util::mk_sp; use std::map::HashMap; +use codemap::CharPos; /* This is an Earley-like parser, without support for in-grammar nonterminals, only by calling out to the main rust parser for named nonterminals (which it @@ -102,7 +103,7 @@ type matcher_pos = ~{ mut up: matcher_pos_up, // mutable for swapping only matches: ~[DVec<@named_match>], match_lo: uint, match_hi: uint, - sp_lo: uint, + sp_lo: CharPos, }; fn copy_up(&& mpu: matcher_pos_up) -> matcher_pos { @@ -122,7 +123,7 @@ fn count_names(ms: &[matcher]) -> uint { } #[allow(non_implicitly_copyable_typarams)] -fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: uint) +fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: CharPos) -> matcher_pos { let mut match_idx_hi = 0u; for ms.each() |elt| { diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 238f9db6ac537..78f0e4fc8f8bf 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -53,7 +53,7 @@ fn new_tt_reader(sp_diag: span_handler, itr: @ident_interner, mut repeat_len: ~[], /* dummy values, never read: */ mut cur_tok: EOF, - mut cur_span: ast_util::mk_sp(0u,0u) + mut cur_span: ast_util::dummy_sp() }; tt_next_token(r); /* get cur_tok and cur_span set up */ return r; diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 01461a8522075..27338b86bea9e 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::{CodeMap, FileMap}; +use codemap::{CodeMap, FileMap, CharPos, BytePos}; type parse_sess = @{ cm: @codemap::CodeMap, @@ -28,8 +28,8 @@ type parse_sess = @{ span_diagnostic: span_handler, interner: @ident_interner, // these two must be kept up to date - mut chpos: uint, - mut byte_pos: uint + mut chpos: CharPos, + mut byte_pos: BytePos }; fn new_parse_sess(demitter: Option) -> parse_sess { @@ -38,7 +38,7 @@ 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 chpos: 0u, mut byte_pos: 0u}; + mut chpos: CharPos(0u), mut byte_pos: BytePos(0u)}; } fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) @@ -47,7 +47,7 @@ fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) mut next_id: 1, span_diagnostic: sh, interner: mk_ident_interner(), - mut chpos: 0u, mut byte_pos: 0u}; + mut chpos: CharPos(0u), mut byte_pos: BytePos(0u)}; } fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 42101a431d6c6..31528c10fe1aa 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -14,7 +14,7 @@ trait parser_attr { -> attr_or_ext; fn parse_outer_attributes() -> ~[ast::attribute]; fn parse_attribute(style: ast::attr_style) -> ast::attribute; - fn parse_attribute_naked(style: ast::attr_style, lo: uint) -> + fn parse_attribute_naked(style: ast::attr_style, lo: CharPos) -> ast::attribute; fn parse_inner_attrs_and_next() -> {inner: ~[ast::attribute], next: ~[ast::attribute]}; @@ -85,7 +85,7 @@ impl Parser: parser_attr { return self.parse_attribute_naked(style, lo); } - fn parse_attribute_naked(style: ast::attr_style, lo: uint) -> + fn parse_attribute_naked(style: ast::attr_style, lo: CharPos) -> ast::attribute { self.expect(token::LBRACKET); let meta_item = self.parse_meta_item(); diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index a2f73fd189ca5..06b042a468a94 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -3,7 +3,7 @@ use io::ReaderUtil; use util::interner; use lexer::{string_reader, bump, is_eof, nextch, is_whitespace, get_str_from, reader}; -use codemap::FileMap; +use codemap::{FileMap, CharPos}; export cmnt; export lit; @@ -28,7 +28,7 @@ impl cmnt_style : cmp::Eq { } } -type cmnt = {style: cmnt_style, lines: ~[~str], pos: uint}; +type cmnt = {style: cmnt_style, lines: ~[~str], pos: CharPos}; fn is_doc_comment(s: ~str) -> bool { s.starts_with(~"///") || @@ -137,7 +137,7 @@ fn push_blank_line_comment(rdr: string_reader, comments: &mut ~[cmnt]) { fn consume_whitespace_counting_blank_lines(rdr: string_reader, comments: &mut ~[cmnt]) { while is_whitespace(rdr.curr) && !is_eof(rdr) { - if rdr.col == 0u && rdr.curr == '\n' { + if rdr.col == CharPos(0u) && rdr.curr == '\n' { push_blank_line_comment(rdr, comments); } bump(rdr); @@ -181,6 +181,8 @@ fn read_line_comments(rdr: string_reader, code_to_the_left: bool, } } +// FIXME #3961: This is not the right way to convert string byte +// offsets to characters. fn all_whitespace(s: ~str, begin: uint, end: uint) -> bool { let mut i: uint = begin; while i != end { @@ -190,9 +192,11 @@ fn all_whitespace(s: ~str, begin: uint, end: uint) -> bool { } fn trim_whitespace_prefix_and_push_line(lines: &mut ~[~str], - s: ~str, col: uint) { + s: ~str, col: CharPos) { let mut s1; let len = str::len(s); + // FIXME #3961: Doing bytewise comparison and slicing with CharPos + let col = col.to_uint(); if all_whitespace(s, 0u, uint::min(len, col)) { if col < len { s1 = str::slice(s, col, len); @@ -207,7 +211,7 @@ fn read_block_comment(rdr: string_reader, code_to_the_left: bool, debug!(">>> block comment"); let p = rdr.chpos; let mut lines: ~[~str] = ~[]; - let mut col: uint = rdr.col; + let mut col: CharPos = rdr.col; bump(rdr); bump(rdr); @@ -280,7 +284,7 @@ fn consume_comment(rdr: string_reader, code_to_the_left: bool, debug!("<<< consume comment"); } -type lit = {lit: ~str, pos: uint}; +type lit = {lit: ~str, pos: CharPos}; fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, path: ~str, @@ -289,7 +293,8 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); let rdr = lexer::new_low_level_string_reader - (span_diagnostic, @FileMap::new(path, src, 0u, 0u), itr); + (span_diagnostic, @FileMap::new(path, src, + CharPos(0u), BytePos(0u)), itr); let mut comments: ~[cmnt] = ~[]; let mut literals: ~[lit] = ~[]; diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 50c22c08f4f88..66930009eb896 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -205,7 +205,7 @@ impl Parser: parser_common { if self.token == token::GT { self.bump(); } else if self.token == token::BINOP(token::SHR) { - self.swap(token::GT, self.span.lo + 1u, self.span.hi); + self.swap(token::GT, self.span.lo + CharPos(1u), self.span.hi); } else { let mut s: ~str = ~"expected `"; s += token_to_str(self.reader, token::GT); diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 06b8b1e8eeeed..dbeaff2862526 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -1,5 +1,5 @@ use diagnostic::span_handler; -use codemap::{span, CodeMap}; +use codemap::{span, CodeMap, CharPos, BytePos}; use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, tt_next_token}; @@ -21,10 +21,10 @@ trait reader { type string_reader = @{ span_diagnostic: span_handler, src: @~str, - mut col: uint, - mut pos: uint, + mut col: CharPos, + mut pos: BytePos, mut curr: char, - mut chpos: uint, + mut chpos: CharPos, filemap: @codemap::FileMap, interner: @token::ident_interner, /* cached: */ @@ -46,15 +46,15 @@ fn new_low_level_string_reader(span_diagnostic: span_handler, itr: @token::ident_interner) -> string_reader { let r = @{span_diagnostic: span_diagnostic, src: filemap.src, - mut col: 0u, mut pos: 0u, mut curr: -1 as char, + mut col: CharPos(0), mut pos: BytePos(0), mut curr: -1 as char, mut chpos: filemap.start_pos.ch, filemap: filemap, interner: itr, /* dummy values; not read */ mut peek_tok: token::EOF, - mut peek_span: ast_util::mk_sp(0u,0u)}; - if r.pos < (*filemap.src).len() { - let next = str::char_range_at(*r.src, r.pos); - r.pos = next.next; + mut peek_span: ast_util::dummy_sp()}; + if r.pos.to_uint() < (*filemap.src).len() { + let next = str::char_range_at(*r.src, r.pos.to_uint()); + r.pos = BytePos(next.next); r.curr = next.ch; } return r; @@ -124,27 +124,27 @@ fn string_advance_token(&&r: string_reader) { } -fn get_str_from(rdr: string_reader, start: uint) -> ~str unsafe { +fn get_str_from(rdr: string_reader, start: BytePos) -> ~str unsafe { // I'm pretty skeptical about this subtraction. What if there's a // multi-byte character before the mark? - return str::slice(*rdr.src, start - 1u, rdr.pos - 1u); + return str::slice(*rdr.src, start.to_uint() - 1u, rdr.pos.to_uint() - 1u); } fn bump(rdr: string_reader) { - if rdr.pos < (*rdr.src).len() { - rdr.col += 1u; - rdr.chpos += 1u; + if rdr.pos.to_uint() < (*rdr.src).len() { + rdr.col += CharPos(1u); + rdr.chpos += CharPos(1u); if rdr.curr == '\n' { rdr.filemap.next_line(rdr.chpos, rdr.pos); - rdr.col = 0u; + rdr.col = CharPos(0u); } - let next = str::char_range_at(*rdr.src, rdr.pos); - rdr.pos = next.next; + let next = str::char_range_at(*rdr.src, rdr.pos.to_uint()); + rdr.pos = BytePos(next.next); rdr.curr = next.ch; } else { if (rdr.curr != -1 as char) { - rdr.col += 1u; - rdr.chpos += 1u; + rdr.col += CharPos(1u); + rdr.chpos += CharPos(1u); rdr.curr = -1 as char; } } @@ -153,8 +153,8 @@ fn is_eof(rdr: string_reader) -> bool { rdr.curr == -1 as char } fn nextch(rdr: string_reader) -> char { - if rdr.pos < (*rdr.src).len() { - return str::char_at(*rdr.src, rdr.pos); + if rdr.pos.to_uint() < (*rdr.src).len() { + return str::char_at(*rdr.src, rdr.pos.to_uint()); } else { return -1 as char; } } @@ -211,7 +211,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_chpos = rdr.chpos - 2u; + let start_chpos = rdr.chpos - CharPos(2u); let mut acc = ~"//"; while rdr.curr != '\n' && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -235,7 +235,7 @@ fn consume_any_line_comment(rdr: string_reader) let cmap = @CodeMap::new(); (*cmap).files.push(rdr.filemap); let loc = cmap.lookup_char_pos_adj(rdr.chpos); - if loc.line == 1u && loc.col == 0u { + if loc.line == 1u && loc.col == CharPos(0u) { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } return consume_whitespace_and_comments(rdr); } @@ -250,7 +250,7 @@ fn consume_block_comment(rdr: string_reader) // block comments starting with "/**" or "/*!" are doc-comments if rdr.curr == '*' || rdr.curr == '!' { - let start_chpos = rdr.chpos - 2u; + let start_chpos = rdr.chpos - CharPos(2u); let mut acc = ~"/*"; while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -584,7 +584,7 @@ fn next_token_inner(rdr: string_reader) -> token::Token { return token::LIT_INT(c2 as i64, ast::ty_char); } '"' => { - let n = rdr.chpos; + let n = rdr.pos; bump(rdr); while rdr.curr != '"' { if is_eof(rdr) { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 43628b0908fcf..bd314d6bcaf5d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5,7 +5,7 @@ use either::{Either, Left, Right}; use std::map::HashMap; use token::{can_begin_expr, is_ident, is_ident_or_path, is_plain_ident, INTERPOLATED, special_idents}; -use codemap::{span,FssNone}; +use codemap::{span,FssNone, CharPos}; use util::interner::Interner; use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec}; use lexer::reader; @@ -244,7 +244,7 @@ impl Parser { self.token = next.tok; self.span = next.sp; } - fn swap(next: token::Token, lo: uint, hi: uint) { + fn swap(next: token::Token, +lo: CharPos, +hi: CharPos) { self.token = next; self.span = mk_sp(lo, hi); } @@ -904,12 +904,12 @@ impl Parser { return spanned(lo, e.span.hi, {mutbl: m, ident: i, expr: e}); } - fn mk_expr(lo: uint, hi: uint, +node: expr_) -> @expr { + fn mk_expr(+lo: CharPos, +hi: CharPos, +node: expr_) -> @expr { return @{id: self.get_id(), callee_id: self.get_id(), node: node, span: mk_sp(lo, hi)}; } - fn mk_mac_expr(lo: uint, hi: uint, m: mac_) -> @expr { + fn mk_mac_expr(+lo: CharPos, +hi: CharPos, m: mac_) -> @expr { return @{id: self.get_id(), callee_id: self.get_id(), node: expr_mac({node: m, span: mk_sp(lo, hi)}), @@ -1134,7 +1134,7 @@ impl Parser { return self.mk_expr(lo, hi, ex); } - fn parse_block_expr(lo: uint, blk_mode: blk_check_mode) -> @expr { + fn parse_block_expr(lo: CharPos, blk_mode: blk_check_mode) -> @expr { self.expect(token::LBRACE); let blk = self.parse_block_tail(lo, blk_mode); return self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk)); @@ -1146,7 +1146,7 @@ impl Parser { return self.parse_syntax_ext_naked(lo); } - fn parse_syntax_ext_naked(lo: uint) -> @expr { + fn parse_syntax_ext_naked(lo: CharPos) -> @expr { match self.token { token::IDENT(_, _) => (), _ => self.fatal(~"expected a syntax expander name") @@ -2279,11 +2279,11 @@ impl Parser { // I guess that also means "already parsed the 'impure'" if // necessary, and this should take a qualifier. // some blocks start with "#{"... - fn parse_block_tail(lo: uint, s: blk_check_mode) -> blk { + fn parse_block_tail(lo: CharPos, s: blk_check_mode) -> blk { self.parse_block_tail_(lo, s, ~[]) } - fn parse_block_tail_(lo: uint, s: blk_check_mode, + fn parse_block_tail_(lo: CharPos, s: blk_check_mode, +first_item_attrs: ~[attribute]) -> blk { let mut stmts = ~[]; let mut expr = None; @@ -2581,7 +2581,7 @@ impl Parser { return {ident: id, tps: ty_params}; } - fn mk_item(lo: uint, hi: uint, +ident: ident, + fn mk_item(+lo: CharPos, +hi: CharPos, +ident: ident, +node: item_, vis: visibility, +attrs: ~[attribute]) -> @item { return @{ident: ident, @@ -3037,7 +3037,7 @@ impl Parser { items: items}; } - fn parse_item_foreign_mod(lo: uint, + fn parse_item_foreign_mod(lo: CharPos, visibility: visibility, attrs: ~[attribute], items_allowed: bool) @@ -3092,7 +3092,7 @@ impl Parser { }); } - fn parse_type_decl() -> {lo: uint, ident: ident} { + fn parse_type_decl() -> {lo: CharPos, ident: ident} { let lo = self.last_span.lo; let id = self.parse_ident(); return {lo: lo, ident: id}; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 8fa7543b2d133..910cb89ec7570 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1,5 +1,5 @@ use parse::{comments, lexer, token}; -use codemap::CodeMap; +use codemap::{CodeMap, CharPos}; use pp::{break_offset, word, printer, space, zerobreak, hardbreak, breaks}; use pp::{consistent, inconsistent, eof}; use ast::{required, provided}; @@ -631,7 +631,7 @@ fn print_variants(s: ps, variants: ~[ast::variant], span: ast::span) { print_variant(s, *v); word(s.s, ~","); end(s); - maybe_print_trailing_comment(s, v.span, None::); + maybe_print_trailing_comment(s, v.span, None); } bclose(s, span); } @@ -886,7 +886,7 @@ fn print_stmt(s: ps, st: ast::stmt) { } } if parse::classify::stmt_ends_with_semi(st) { word(s.s, ~";"); } - maybe_print_trailing_comment(s, st.span, None::); + maybe_print_trailing_comment(s, st.span, None); } fn print_block(s: ps, blk: ast::blk) { @@ -1898,7 +1898,7 @@ fn print_ty_fn(s: ps, } fn maybe_print_trailing_comment(s: ps, span: codemap::span, - next_pos: Option) { + next_pos: Option) { let mut cm; match s.cm { Some(ccm) => cm = ccm, _ => return } match next_comment(s) { @@ -1906,7 +1906,7 @@ fn maybe_print_trailing_comment(s: ps, span: codemap::span, if cmnt.style != comments::trailing { return; } let span_line = cm.lookup_char_pos(span.hi); let comment_line = cm.lookup_char_pos(cmnt.pos); - let mut next = cmnt.pos + 1u; + let mut next = cmnt.pos + CharPos(1u); match next_pos { None => (), Some(p) => next = p } if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { @@ -1981,7 +1981,7 @@ fn lit_to_str(l: @ast::lit) -> ~str { return to_str(l, print_literal, parse::token::mk_fake_ident_interner()); } -fn next_lit(s: ps, pos: uint) -> Option { +fn next_lit(s: ps, pos: CharPos) -> Option { match s.literals { Some(lits) => { while s.cur_lit < vec::len(lits) { @@ -1996,7 +1996,7 @@ fn next_lit(s: ps, pos: uint) -> Option { } } -fn maybe_print_comment(s: ps, pos: uint) { +fn maybe_print_comment(s: ps, pos: CharPos) { loop { match next_comment(s) { Some(cmnt) => { From 8069d2f266b5e5f21d9cc846b0b8b8daa1812dcc Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 14 Nov 2012 16:34:18 -0800 Subject: [PATCH 15/56] Track character and byte positions together in the parser --- src/libsyntax/codemap.rs | 10 ++++---- src/libsyntax/parse.rs | 42 ++++++++++++++++----------------- src/libsyntax/parse/comments.rs | 10 +++++--- src/libsyntax/parse/eval.rs | 14 +++++++---- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 8333849b21dda..fa01a15af5a4b 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -184,20 +184,20 @@ pub struct FileMap { pub impl FileMap { static fn new_w_substr(+filename: FileName, +substr: FileSubstr, src: @~str, - +start_pos_ch: CharPos, +start_pos_byte: BytePos) + +start_pos: FilePos) -> FileMap { return FileMap { name: filename, substr: substr, src: src, - start_pos: FilePos {ch: start_pos_ch, byte: start_pos_byte}, - mut lines: ~[FilePos {ch: start_pos_ch, byte: start_pos_byte}] + start_pos: start_pos, + mut lines: ~[start_pos] }; } static fn new(+filename: FileName, src: @~str, - +start_pos_ch: CharPos, +start_pos_byte: BytePos) + +start_pos: FilePos) -> FileMap { return FileMap::new_w_substr(filename, FssNone, src, - start_pos_ch, start_pos_byte); + start_pos); } fn next_line(@self, +chpos: CharPos, +byte_pos: BytePos) { diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 27338b86bea9e..fbd67e6ebce1b 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -20,16 +20,15 @@ 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::{CodeMap, FileMap, CharPos, BytePos}; +use codemap::{CodeMap, FileMap, CharPos, BytePos, FilePos}; type parse_sess = @{ cm: @codemap::CodeMap, mut next_id: node_id, span_diagnostic: span_handler, interner: @ident_interner, - // these two must be kept up to date - mut chpos: CharPos, - mut byte_pos: BytePos + // must be kept up to date + mut pos: FilePos }; fn new_parse_sess(demitter: Option) -> parse_sess { @@ -38,7 +37,10 @@ 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 chpos: CharPos(0u), mut byte_pos: BytePos(0u)}; + mut pos: FilePos { + ch: CharPos(0u), + byte: BytePos(0u) + }}; } fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) @@ -47,7 +49,10 @@ fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) mut next_id: 1, span_diagnostic: sh, interner: mk_ident_interner(), - mut chpos: CharPos(0u), mut byte_pos: BytePos(0u)}; + mut pos: FilePos { + ch: CharPos(0u), + byte: BytePos(0u) + }}; } fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, @@ -71,8 +76,7 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, let leading_attrs = p.parse_inner_attrs_and_next(); let { inner: crate_attrs, next: first_cdir_attr } = leading_attrs; let cdirs = p.parse_crate_directives(token::EOF, first_cdir_attr); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); let cx = @{sess: sess, cfg: /* FIXME (#2543) */ copy p.cfg}; let companionmod = input.filestem().map(|s| Path(*s)); let (m, attrs) = eval::eval_crate_directives_to_mod( @@ -92,8 +96,7 @@ fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, parser::SOURCE_FILE); let r = p.parse_crate_mod(cfg); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); return r; } @@ -103,8 +106,7 @@ fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_crate_mod(cfg); p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); return r; } @@ -114,8 +116,7 @@ fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); return r; } @@ -126,8 +127,7 @@ fn parse_item_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_item(attrs); p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); return r; } @@ -138,8 +138,7 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_stmt(attrs); p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); return r; } @@ -156,8 +155,7 @@ fn parse_from_source_str(f: fn (p: Parser) -> T, p.reader.fatal(~"expected end-of-string"); } p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; + eval::update_parse_sess_position(&sess, &rdr); move r } @@ -174,7 +172,7 @@ fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, source: @~str) -> (Parser, string_reader) { let ftype = parser::SOURCE_FILE; let filemap = @FileMap::new_w_substr - (name, ss, source, sess.chpos, sess.byte_pos); + (name, ss, source, sess.pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); @@ -199,7 +197,7 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, } let src = @result::unwrap(res); let filemap = @FileMap::new(path.to_str(), src, - sess.chpos, sess.byte_pos); + sess.pos); sess.cm.files.push(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 06b042a468a94..ba36e6f88e211 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -292,9 +292,13 @@ 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 rdr = lexer::new_low_level_string_reader - (span_diagnostic, @FileMap::new(path, src, - CharPos(0u), BytePos(0u)), itr); + let filemap = @FileMap::new(path, src, + FilePos { + ch: CharPos(0u), + byte: BytePos(0u) + }); + let rdr = lexer::new_low_level_string_reader( + span_diagnostic, filemap, itr); let mut comments: ~[cmnt] = ~[]; let mut literals: ~[lit] = ~[]; diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 56c9d4de9f3cd..bfbec5c530f70 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -2,6 +2,7 @@ use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; export eval_crate_directives_to_mod; +export update_parse_sess_position; type ctx = @{sess: parse::parse_sess, @@ -66,14 +67,20 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option) modpath, SOURCE_FILE); let inner_attrs = p0.parse_inner_attrs_and_next(); let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next); - cx.sess.chpos = r0.chpos; - cx.sess.byte_pos = cx.sess.byte_pos + r0.pos; + update_parse_sess_position(&cx.sess, &r0); return (m0.view_items, m0.items, inner_attrs.inner); } else { return (~[], ~[], ~[]); } } +fn update_parse_sess_position(sess: &parse_sess, r: &lexer::string_reader) { + sess.pos = FilePos { + ch: r.chpos, + byte: sess.pos.byte + r.pos + }; +} + fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { match ::attr::first_attr_value_str_by_name(attrs, ~"path") { Some(d) => d, @@ -105,8 +112,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, /* FIXME (#2543) */ copy id, ast::item_mod(m0), vis, mod_attrs); // Thread defids, chpos and byte_pos through the parsers - cx.sess.chpos = r0.chpos; - cx.sess.byte_pos = cx.sess.byte_pos + r0.pos; + update_parse_sess_position(&cx.sess, &r0); items.push(i); } ast::cdir_dir_mod(vis, id, cdirs, attrs) => { From 3a9ccd53e559be6db3300eb161951fe75b1ff962 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 14 Nov 2012 17:03:07 -0800 Subject: [PATCH 16/56] Factor out some position management code in the lexer --- src/libsyntax/codemap.rs | 2 +- src/libsyntax/parse/lexer.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index fa01a15af5a4b..e8e39ff272aba 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -189,7 +189,7 @@ pub impl FileMap { return FileMap { name: filename, substr: substr, src: src, start_pos: start_pos, - mut lines: ~[start_pos] + mut lines: ~[] }; } diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index dbeaff2862526..178d772ecc694 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -45,18 +45,17 @@ fn new_low_level_string_reader(span_diagnostic: span_handler, filemap: @codemap::FileMap, itr: @token::ident_interner) -> string_reader { + // Force the initial reader bump to start on a fresh line + let initial_char = '\n'; let r = @{span_diagnostic: span_diagnostic, src: filemap.src, - mut col: CharPos(0), mut pos: BytePos(0), mut curr: -1 as char, + mut col: CharPos(0), mut pos: BytePos(0), + mut curr: initial_char, mut chpos: filemap.start_pos.ch, filemap: filemap, interner: itr, /* dummy values; not read */ mut peek_tok: token::EOF, mut peek_span: ast_util::dummy_sp()}; - if r.pos.to_uint() < (*filemap.src).len() { - let next = str::char_range_at(*r.src, r.pos.to_uint()); - r.pos = BytePos(next.next); - r.curr = next.ch; - } + bump(r); return r; } @@ -142,9 +141,10 @@ fn bump(rdr: string_reader) { rdr.pos = BytePos(next.next); rdr.curr = next.ch; } else { + // XXX: What does this accomplish? if (rdr.curr != -1 as char) { - rdr.col += CharPos(1u); rdr.chpos += CharPos(1u); + rdr.col += CharPos(1u); rdr.curr = -1 as char; } } From b1dff40bae94dd0fd1e4846fd7b76723d0a3f9f9 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 14 Nov 2012 17:43:41 -0800 Subject: [PATCH 17/56] Create CodeMap.add_filemap --- src/libsyntax/codemap.rs | 4 ++++ src/libsyntax/parse.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index e8e39ff272aba..f3b0611e46379 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -230,6 +230,10 @@ pub impl CodeMap { } } + pub fn add_filemap(@self, filemap: @FileMap) { + self.files.push(filemap); + } + 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, diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index fbd67e6ebce1b..97ca8568cfe91 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -173,7 +173,7 @@ fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, let ftype = parser::SOURCE_FILE; let filemap = @FileMap::new_w_substr (name, ss, source, sess.pos); - sess.cm.files.push(filemap); + sess.cm.add_filemap(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); return (Parser(sess, cfg, srdr as reader, ftype), srdr); @@ -198,7 +198,7 @@ fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, let src = @result::unwrap(res); let filemap = @FileMap::new(path.to_str(), src, sess.pos); - sess.cm.files.push(filemap); + sess.cm.add_filemap(filemap); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); return (Parser(sess, cfg, srdr as reader, ftype), srdr); From 4a0f4f5e311b7443b29ea31ad0e68e6b4aa63736 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 14 Nov 2012 22:27:53 -0800 Subject: [PATCH 18/56] Refactor the lexer to use FilePos types --- src/libsyntax/codemap.rs | 19 ++++++--- src/libsyntax/ext/source_util.rs | 11 ++++-- src/libsyntax/parse.rs | 22 +++++++---- src/libsyntax/parse/comments.rs | 10 ++--- src/libsyntax/parse/eval.rs | 9 +---- src/libsyntax/parse/lexer.rs | 68 ++++++++++++++++++++------------ 6 files changed, 85 insertions(+), 54 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index f3b0611e46379..e1c5eb07eb8f5 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -200,11 +200,8 @@ pub impl FileMap { start_pos); } - fn next_line(@self, +chpos: CharPos, +byte_pos: BytePos) { - self.lines.push(FilePos { - ch: chpos, - byte: byte_pos + self.start_pos.byte - }); + fn next_line(@self, +pos: FilePos) { + self.lines.push(pos); } pub fn get_line(@self, line: int) -> ~str unsafe { @@ -231,6 +228,18 @@ pub impl CodeMap { } pub fn add_filemap(@self, filemap: @FileMap) { + let expected_byte_pos = if self.files.len() == 0 { + 0 + } else { + let last_start = self.files.last().start_pos.byte.to_uint(); + let last_len = self.files.last().src.len(); + last_start + last_len + }; + let actual_byte_pos = filemap.start_pos.byte.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); + assert expected_byte_pos == actual_byte_pos; self.files.push(filemap); } diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 3fed340904a7a..726dbbb56e2a4 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -58,10 +58,13 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args = get_mac_args(cx, sp, arg, 1u, option::Some(1u), ~"include"); let file = expr_to_str(cx, args[0], ~"#include_str requires a string"); - let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(), - &res_rel_file(cx, sp, &Path(file)), - parse::parser::SOURCE_FILE); - return p.parse_expr(); + let (p, rdr) = parse::new_parser_etc_from_file( + cx.parse_sess(), cx.cfg(), + &res_rel_file(cx, sp, &Path(file)), + parse::parser::SOURCE_FILE); + let e = p.parse_expr(); + parse::update_parse_sess_position(&cx.parse_sess(), &rdr); + return e; } fn expand_include_str(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg, diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 97ca8568cfe91..c5ffbb2ab0a39 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -11,6 +11,7 @@ export parse_crate_from_source_str; export parse_expr_from_source_str, parse_item_from_source_str; export parse_stmt_from_source_str; export parse_from_source_str; +export update_parse_sess_position; use parser::Parser; use attr::parser_attr; @@ -76,7 +77,7 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, let leading_attrs = p.parse_inner_attrs_and_next(); let { inner: crate_attrs, next: first_cdir_attr } = leading_attrs; let cdirs = p.parse_crate_directives(token::EOF, first_cdir_attr); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); let cx = @{sess: sess, cfg: /* FIXME (#2543) */ copy p.cfg}; let companionmod = input.filestem().map(|s| Path(*s)); let (m, attrs) = eval::eval_crate_directives_to_mod( @@ -96,7 +97,7 @@ fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, parser::SOURCE_FILE); let r = p.parse_crate_mod(cfg); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); return r; } @@ -106,7 +107,7 @@ fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_crate_mod(cfg); p.abort_if_errors(); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); return r; } @@ -116,7 +117,7 @@ fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); return r; } @@ -127,7 +128,7 @@ fn parse_item_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_item(attrs); p.abort_if_errors(); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); return r; } @@ -138,7 +139,7 @@ fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, codemap::FssNone, source); let r = p.parse_stmt(attrs); p.abort_if_errors(); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); return r; } @@ -155,7 +156,7 @@ fn parse_from_source_str(f: fn (p: Parser) -> T, p.reader.fatal(~"expected end-of-string"); } p.abort_if_errors(); - eval::update_parse_sess_position(&sess, &rdr); + update_parse_sess_position(&sess, &rdr); move r } @@ -216,3 +217,10 @@ fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, None, tt); return Parser(sess, cfg, trdr as reader, parser::SOURCE_FILE) } + +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 + }; +} diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index ba36e6f88e211..92736b9f36198 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.chpos}); + comments.push({style: blank_line, lines: v, pos: rdr.last_pos.ch}); } 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.chpos; + let p = rdr.last_pos.ch; 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.chpos; + let p = rdr.last_pos.ch; 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.chpos; + let p = rdr.last_pos.ch; let mut lines: ~[~str] = ~[]; let mut col: CharPos = rdr.col; bump(rdr); @@ -319,7 +319,7 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, } - let bstart = rdr.pos; + let bstart = rdr.pos.byte; 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/eval.rs b/src/libsyntax/parse/eval.rs index bfbec5c530f70..47dbc0cd6eea1 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,8 +1,8 @@ +use parse::update_parse_sess_position; use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; export eval_crate_directives_to_mod; -export update_parse_sess_position; type ctx = @{sess: parse::parse_sess, @@ -74,13 +74,6 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option) } } -fn update_parse_sess_position(sess: &parse_sess, r: &lexer::string_reader) { - sess.pos = FilePos { - ch: r.chpos, - byte: sess.pos.byte + r.pos - }; -} - fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { match ::attr::first_attr_value_str_by_name(attrs, ~"path") { Some(d) => d, diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 178d772ecc694..459935def677b 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -1,5 +1,5 @@ use diagnostic::span_handler; -use codemap::{span, CodeMap, CharPos, BytePos}; +use codemap::{span, CodeMap, CharPos, BytePos, FilePos}; use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, tt_next_token}; @@ -21,10 +21,10 @@ trait reader { type string_reader = @{ span_diagnostic: span_handler, src: @~str, + mut pos: FilePos, + mut last_pos: FilePos, mut col: CharPos, - mut pos: BytePos, mut curr: char, - mut chpos: CharPos, filemap: @codemap::FileMap, interner: @token::ident_interner, /* cached: */ @@ -48,9 +48,10 @@ fn new_low_level_string_reader(span_diagnostic: span_handler, // Force the initial reader bump to start on a fresh line let initial_char = '\n'; let r = @{span_diagnostic: span_diagnostic, src: filemap.src, - mut col: CharPos(0), mut pos: BytePos(0), + mut pos: filemap.start_pos, + mut last_pos: filemap.start_pos, + mut col: CharPos(0), mut curr: initial_char, - mut chpos: filemap.start_pos.ch, filemap: filemap, interner: itr, /* dummy values; not read */ mut peek_tok: token::EOF, @@ -61,7 +62,9 @@ fn new_low_level_string_reader(span_diagnostic: span_handler, fn dup_string_reader(&&r: string_reader) -> string_reader { @{span_diagnostic: r.span_diagnostic, src: r.src, - mut col: r.col, mut pos: r.pos, mut curr: r.curr, mut chpos: r.chpos, + mut pos: r.pos, + mut last_pos: r.last_pos, + mut col: r.col, mut curr: r.curr, filemap: r.filemap, interner: r.interner, mut peek_tok: r.peek_tok, mut peek_span: r.peek_span} } @@ -116,34 +119,48 @@ fn string_advance_token(&&r: string_reader) { if is_eof(r) { r.peek_tok = token::EOF; } else { - let start_chpos = r.chpos; + let start_chpos = r.last_pos.ch; r.peek_tok = next_token_inner(r); - r.peek_span = ast_util::mk_sp(start_chpos, r.chpos); + r.peek_span = ast_util::mk_sp(start_chpos, r.last_pos.ch); }; } +fn byte_offset(rdr: string_reader) -> BytePos { + (rdr.pos.byte - rdr.filemap.start_pos.byte) +} + fn get_str_from(rdr: string_reader, start: BytePos) -> ~str unsafe { // I'm pretty skeptical about this subtraction. What if there's a // multi-byte character before the mark? - return str::slice(*rdr.src, start.to_uint() - 1u, rdr.pos.to_uint() - 1u); + return str::slice(*rdr.src, start.to_uint() - 1u, + byte_offset(rdr).to_uint() - 1u); } fn bump(rdr: string_reader) { - if rdr.pos.to_uint() < (*rdr.src).len() { + rdr.last_pos = rdr.pos; + let current_byte_offset = byte_offset(rdr).to_uint();; + if current_byte_offset < (*rdr.src).len() { + 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.curr = next.ch; rdr.col += CharPos(1u); - rdr.chpos += CharPos(1u); - if rdr.curr == '\n' { - rdr.filemap.next_line(rdr.chpos, rdr.pos); + if last_char == '\n' { + rdr.filemap.next_line(rdr.last_pos); rdr.col = CharPos(0u); } - let next = str::char_range_at(*rdr.src, rdr.pos.to_uint()); - rdr.pos = BytePos(next.next); - rdr.curr = next.ch; } else { // XXX: What does this accomplish? if (rdr.curr != -1 as char) { - rdr.chpos += CharPos(1u); + rdr.pos = FilePos { + ch: rdr.pos.ch + CharPos(1u), + byte: rdr.pos.byte + BytePos(1u) + }; rdr.col += CharPos(1u); rdr.curr = -1 as char; } @@ -153,8 +170,9 @@ fn is_eof(rdr: string_reader) -> bool { rdr.curr == -1 as char } fn nextch(rdr: string_reader) -> char { - if rdr.pos.to_uint() < (*rdr.src).len() { - return str::char_at(*rdr.src, rdr.pos.to_uint()); + let offset = byte_offset(rdr).to_uint(); + if offset < (*rdr.src).len() { + return str::char_at(*rdr.src, offset); } else { return -1 as char; } } @@ -211,7 +229,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_chpos = rdr.chpos - CharPos(2u); + let start_chpos = rdr.pos.ch - CharPos(2u); let mut acc = ~"//"; while rdr.curr != '\n' && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -219,7 +237,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_chpos, rdr.chpos) + sp: ast_util::mk_sp(start_chpos, rdr.pos.ch) }); } else { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } @@ -234,7 +252,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.chpos); + let loc = cmap.lookup_char_pos_adj(rdr.last_pos.ch); if loc.line == 1u && loc.col == CharPos(0u) { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } return consume_whitespace_and_comments(rdr); @@ -250,7 +268,7 @@ fn consume_block_comment(rdr: string_reader) // block comments starting with "/**" or "/*!" are doc-comments if rdr.curr == '*' || rdr.curr == '!' { - let start_chpos = rdr.chpos - CharPos(2u); + let start_chpos = rdr.pos.ch - CharPos(2u); let mut acc = ~"/*"; while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -264,7 +282,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_chpos, rdr.chpos) + sp: ast_util::mk_sp(start_chpos, rdr.pos.ch) }); } } else { @@ -584,7 +602,7 @@ fn next_token_inner(rdr: string_reader) -> token::Token { return token::LIT_INT(c2 as i64, ast::ty_char); } '"' => { - let n = rdr.pos; + let n = byte_offset(rdr); bump(rdr); while rdr.curr != '"' { if is_eof(rdr) { From bcccf333ab728539bf63ed773e6182f9da33683e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 15 Nov 2012 14:34:23 -0800 Subject: [PATCH 19/56] Add some comments to codemap and lexer --- src/libsyntax/codemap.rs | 5 +++++ src/libsyntax/parse/lexer.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index e1c5eb07eb8f5..a54c41e855281 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -2,6 +2,9 @@ * in a crate. This to make it possible to represent the positions * with single-word things, rather than passing records all over the * compiler. + * + * All represented positions are *absolute* positions within the codemap, + * not relative positions within a single file. */ use dvec::DVec; @@ -142,6 +145,8 @@ pub struct Loc { file: @FileMap, line: uint, col: A } +/// An absolute offset within the CodeMap (not a relative offset within a +/// a single FileMap) pub struct FilePos { ch: CharPos, byte: BytePos } diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 459935def677b..1eadaec1fcdf6 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -21,9 +21,13 @@ trait reader { type string_reader = @{ span_diagnostic: span_handler, src: @~str, + // The absolute offset within the codemap of the next character to read mut pos: FilePos, + // The absolute offset within the codemap of the last character to be read (curr) mut last_pos: FilePos, + // The column of the next character to read mut col: CharPos, + // The last character to be read mut curr: char, filemap: @codemap::FileMap, interner: @token::ident_interner, From d5e35e3e87bfc4dc0c7e0a33177f176be20ac8cb Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 15 Nov 2012 15:00:49 -0800 Subject: [PATCH 20/56] Convert CodeMap and FileMap to use &self instead of @self --- src/libsyntax/codemap.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index a54c41e855281..cb427556e2779 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -205,11 +205,11 @@ pub impl FileMap { start_pos); } - fn next_line(@self, +pos: FilePos) { + fn next_line(&self, +pos: FilePos) { self.lines.push(pos); } - pub fn get_line(@self, line: int) -> ~str unsafe { + pub fn get_line(&self, line: int) -> ~str unsafe { let begin: BytePos = self.lines[line].byte - self.start_pos.byte; let begin = begin.to_uint(); let end = match str::find_char_from(*self.src, '\n', begin) { @@ -232,7 +232,7 @@ pub impl CodeMap { } } - pub fn add_filemap(@self, filemap: @FileMap) { + pub fn add_filemap(&self, filemap: @FileMap) { let expected_byte_pos = if self.files.len() == 0 { 0 } else { @@ -248,23 +248,23 @@ pub impl CodeMap { self.files.push(filemap); } - pub fn mk_substr_filename(@self, sp: span) -> ~str { + 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.to_uint()); } - pub fn lookup_char_pos(@self, +pos: CharPos) -> Loc { + pub fn lookup_char_pos(&self, +pos: CharPos) -> Loc { pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } return self.lookup_pos(pos, lookup); } - pub fn lookup_byte_pos(@self, +pos: BytePos) -> Loc { + pub fn lookup_byte_pos(&self, +pos: BytePos) -> Loc { pure fn lookup(pos: FilePos) -> uint { return pos.byte.to_uint(); } return self.lookup_pos(pos, lookup); } - pub fn lookup_char_pos_adj(@self, +pos: CharPos) + pub fn lookup_char_pos_adj(&self, +pos: CharPos) -> {filename: ~str, line: uint, col: CharPos, file: Option<@FileMap>} { let loc = self.lookup_char_pos(pos); @@ -288,7 +288,7 @@ pub impl CodeMap { } } - pub fn adjust_span(@self, sp: span) -> span { + pub fn adjust_span(&self, sp: span) -> span { pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } let line = self.lookup_line(sp.lo, lookup); match (line.fm.substr) { @@ -304,19 +304,19 @@ pub impl CodeMap { } } - pub fn span_to_str(@self, sp: span) -> ~str { + 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.to_uint(), hi.line, hi.col.to_uint()) } - pub fn span_to_filename(@self, sp: span) -> FileName { + 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) -> @FileLines { + pub fn span_to_lines(&self, sp: span) -> @FileLines { let lo = self.lookup_char_pos(sp.lo); let hi = self.lookup_char_pos(sp.hi); let mut lines = ~[]; @@ -326,7 +326,7 @@ pub impl CodeMap { return @FileLines {file: lo.file, lines: lines}; } - fn lookup_byte_offset(@self, +chpos: CharPos) + fn lookup_byte_offset(&self, +chpos: CharPos) -> {fm: @FileMap, pos: BytePos} { pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } let {fm, line} = self.lookup_line(chpos, lookup); @@ -338,7 +338,7 @@ pub impl CodeMap { {fm: fm, pos: line_offset + BytePos(col_offset)} } - pub fn span_to_snippet(@self, sp: span) -> ~str { + 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; @@ -346,7 +346,7 @@ pub impl CodeMap { begin.pos.to_uint(), end.pos.to_uint()); } - pub fn get_filemap(@self, filename: ~str) -> @FileMap { + 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 _|_) @@ -356,7 +356,7 @@ pub impl CodeMap { } priv impl CodeMap { - fn lookup_line(@self, pos: A, lookup: LookupFn) + fn lookup_line(&self, pos: A, lookup: LookupFn) -> {fm: @FileMap, line: uint} { let len = self.files.len(); @@ -384,7 +384,7 @@ priv impl CodeMap { return {fm: f, line: a}; } - fn lookup_pos(@self, pos: A, lookup: LookupFn) -> Loc { + fn lookup_pos(&self, pos: A, lookup: LookupFn) -> Loc { let {fm: f, line: a} = self.lookup_line(pos, lookup); return Loc { file: f, @@ -393,7 +393,7 @@ priv impl CodeMap { }; } - fn span_to_str_no_adj(@self, sp: span) -> ~str { + 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, From 8cba337cce19c71c4030f26fba2b00842172b99e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 15 Nov 2012 17:32:14 -0800 Subject: [PATCH 21/56] Remove CodeMap.lookup_byte_pos --- src/libsyntax/codemap.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index cb427556e2779..80dfb6651b947 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -259,11 +259,6 @@ pub impl CodeMap { return self.lookup_pos(pos, lookup); } - pub fn lookup_byte_pos(&self, +pos: BytePos) -> Loc { - pure fn lookup(pos: FilePos) -> uint { return pos.byte.to_uint(); } - return self.lookup_pos(pos, lookup); - } - pub fn lookup_char_pos_adj(&self, +pos: CharPos) -> {filename: ~str, line: uint, col: CharPos, file: Option<@FileMap>} { From 81d20156cd44358e47e5081635f28ea31c01a757 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 15 Nov 2012 19:37:29 -0800 Subject: [PATCH 22/56] Change spans to use byte offsets instead of char offsets --- src/librustc/middle/trans/debuginfo.rs | 2 +- src/libsyntax/ast_util.rs | 8 +- src/libsyntax/attr.rs | 4 +- src/libsyntax/codemap.rs | 145 ++++++++++++++++++------- src/libsyntax/ext/qquote.rs | 14 +-- src/libsyntax/ext/tt/macro_parser.rs | 6 +- src/libsyntax/parse/attr.rs | 4 +- src/libsyntax/parse/comments.rs | 12 +- src/libsyntax/parse/common.rs | 2 +- src/libsyntax/parse/lexer.rs | 21 ++-- src/libsyntax/parse/parser.rs | 22 ++-- src/libsyntax/print/pprust.rs | 10 +- 12 files changed, 161 insertions(+), 89 deletions(-) diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index 15f8b18a9f53a..d4d1c8d3b2ecd 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -112,7 +112,7 @@ type compile_unit_md = {name: ~str}; type subprogram_md = {id: ast::node_id}; type local_var_md = {id: ast::node_id}; type tydesc_md = {hash: uint}; -type block_md = {start: codemap::Loc, end: codemap::Loc}; +type block_md = {start: codemap::Loc, end: codemap::Loc}; type argument_md = {id: ast::node_id}; type retval_md = {id: ast::node_id}; diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 6abcef5c70808..73a1c4b7530a0 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -1,7 +1,7 @@ -use codemap::{span, CharPos}; +use codemap::{span, BytePos}; use ast::*; -pure fn spanned(+lo: CharPos, +hi: CharPos, +t: T) -> spanned { +pure fn spanned(+lo: BytePos, +hi: BytePos, +t: T) -> spanned { respan(mk_sp(lo, hi), move t) } @@ -14,12 +14,12 @@ pure fn dummy_spanned(+t: T) -> spanned { } /* assuming that we're not in macro expansion */ -pure fn mk_sp(+lo: CharPos, +hi: CharPos) -> span { +pure fn mk_sp(+lo: BytePos, +hi: BytePos) -> span { span {lo: lo, hi: hi, expn_info: None} } // make this a const, once the compiler supports it -pure fn dummy_sp() -> span { return mk_sp(CharPos(0), CharPos(0)); } +pure fn dummy_sp() -> span { return mk_sp(BytePos(0), BytePos(0)); } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 3b6bae6410466..da80e26b1afe9 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -6,7 +6,7 @@ use either::Either; use diagnostic::span_handler; use ast_util::{spanned, dummy_spanned}; use parse::comments::{doc_comment_style, strip_doc_comment_decoration}; -use codemap::CharPos; +use codemap::BytePos; // Constructors export mk_name_value_item_str; @@ -76,7 +76,7 @@ fn mk_attr(item: @ast::meta_item) -> ast::attribute { } fn mk_sugared_doc_attr(text: ~str, - +lo: CharPos, +hi: CharPos) -> ast::attribute { + +lo: BytePos, +hi: BytePos) -> ast::attribute { let lit = spanned(lo, hi, ast::lit_str(@text)); let attr = { style: doc_comment_style(text), diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 80dfb6651b947..13f2ea4e210a6 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -118,8 +118,8 @@ impl CharPos: to_bytes::IterBytes { } pub struct span { - lo: CharPos, - hi: CharPos, + lo: BytePos, + hi: BytePos, expn_info: Option<@ExpnInfo> } @@ -141,8 +141,10 @@ impl span: Deserializable { } } -pub struct Loc { - file: @FileMap, line: uint, col: A +// XXX col shouldn't be CharPos because col is not an absolute location in the +// codemap, and BytePos and CharPos always represent absolute positions +pub struct Loc { + file: @FileMap, line: uint, col: CharPos } /// An absolute offset within the CodeMap (not a relative offset within a @@ -178,12 +180,24 @@ pub enum FileSubstr { pub FssExternal({filename: ~str, line: uint, col: CharPos}) } +/// Identifies an offset of a multi-byte character in a FileMap +pub struct MultiByteChar { + /// The absolute offset of the character in the CodeMap + pos: BytePos, + /// The number of bytes, >=2 + bytes: uint, + /// The complete number of 'extra' bytes through this character in the + /// FileMap + sum: uint +} + pub struct FileMap { name: FileName, substr: FileSubstr, src: @~str, start_pos: FilePos, - mut lines: ~[FilePos] + mut lines: ~[FilePos], + multibyte_chars: DVec } pub impl FileMap { @@ -194,7 +208,8 @@ pub impl FileMap { return FileMap { name: filename, substr: substr, src: src, start_pos: start_pos, - mut lines: ~[] + mut lines: ~[], + multibyte_chars: DVec() }; } @@ -219,6 +234,21 @@ pub impl FileMap { str::slice(*self.src, begin, end) } + pub fn record_multibyte_char(&self, pos: BytePos, bytes: uint) { + assert bytes >=2 && bytes <= 4; + let sum = if self.multibyte_chars.len() > 0 { + self.multibyte_chars.last().sum + } else { + 0 + }; + let sum = sum + bytes; + let mbc = MultiByteChar { + pos: pos, + bytes: bytes, + sum: sum + }; + self.multibyte_chars.push(mbc); + } } pub struct CodeMap { @@ -254,12 +284,11 @@ pub impl CodeMap { pos.line, pos.col.to_uint()); } - pub fn lookup_char_pos(&self, +pos: CharPos) -> Loc { - pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } - return self.lookup_pos(pos, lookup); + pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc { + return self.lookup_pos(pos); } - pub fn lookup_char_pos_adj(&self, +pos: CharPos) + pub fn lookup_char_pos_adj(&self, +pos: BytePos) -> {filename: ~str, line: uint, col: CharPos, file: Option<@FileMap>} { let loc = self.lookup_char_pos(pos); @@ -272,7 +301,7 @@ pub impl CodeMap { } FssInternal(sp) => { self.lookup_char_pos_adj( - sp.lo + (pos - loc.file.start_pos.ch)) + sp.lo + (pos - loc.file.start_pos.byte)) } FssExternal(eloc) => { {filename: /* FIXME (#2543) */ copy eloc.filename, @@ -284,14 +313,13 @@ pub impl CodeMap { } pub fn adjust_span(&self, sp: span) -> span { - pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } - let line = self.lookup_line(sp.lo, lookup); + let line = self.lookup_line(sp.lo); match (line.fm.substr) { FssNone => sp, FssInternal(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), + lo: s.lo + (sp.lo - line.fm.start_pos.byte), + hi: s.lo + (sp.hi - line.fm.start_pos.byte), expn_info: sp.expn_info }) } @@ -321,18 +349,6 @@ pub impl CodeMap { return @FileLines {file: lo.file, lines: lines}; } - fn lookup_byte_offset(&self, +chpos: CharPos) - -> {fm: @FileMap, pos: BytePos} { - pure fn lookup(pos: FilePos) -> uint { return pos.ch.to_uint(); } - 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.to_uint(), - col.to_uint()); - {fm: fm, pos: line_offset + BytePos(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); @@ -351,15 +367,14 @@ pub impl CodeMap { } priv impl CodeMap { - fn lookup_line(&self, pos: A, lookup: LookupFn) - -> {fm: @FileMap, line: uint} - { + + fn lookup_filemap_idx(&self, +pos: BytePos) -> 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.to_uint() { + if self.files[m].start_pos.byte > pos { b = m; } else { a = m; @@ -369,22 +384,40 @@ priv impl CodeMap { fail fmt!("position %u does not resolve to a source location", pos.to_uint()) } - let f = self.files[a]; - a = 0u; - b = vec::len(f.lines); + + return a; + } + + fn lookup_line(&self, +pos: BytePos) + -> {fm: @FileMap, line: uint} + { + let idx = self.lookup_filemap_idx(pos); + let f = self.files[idx]; + let mut a = 0u; + let mut b = vec::len(f.lines); while b - a > 1u { let m = (a + b) / 2u; - if lookup(f.lines[m]) > pos.to_uint() { b = m; } else { a = m; } + if f.lines[m].byte > pos { b = m; } else { a = m; } } return {fm: f, line: a}; } - fn lookup_pos(&self, pos: A, lookup: LookupFn) -> Loc { - let {fm: f, line: a} = self.lookup_line(pos, lookup); + fn lookup_pos(&self, +pos: BytePos) -> Loc { + 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 linechpos = self.bytepos_to_local_charpos(linebpos); + debug!("codemap: byte pos %? is on the line at byte pos %?", + pos, linebpos); + debug!("codemap: char pos %? is on the line at char pos %?", + chpos, linechpos); + debug!("codemap: byte is on line: %?", line); + assert chpos >= linechpos; return Loc { file: f, - line: a + 1u, - col: pos - from_uint(lookup(f.lines[a])) + line: line, + col: chpos - linechpos }; } @@ -394,6 +427,40 @@ priv impl CodeMap { return fmt!("%s:%u:%u: %u:%u", lo.file.name, lo.line, lo.col.to_uint(), hi.line, hi.col.to_uint()) } + + fn lookup_byte_offset(&self, +bpos: BytePos) + -> {fm: @FileMap, pos: BytePos} { + let idx = self.lookup_filemap_idx(bpos); + let fm = self.files[idx]; + let offset = bpos - fm.start_pos.byte; + return {fm: fm, pos: offset}; + } + + // Converts an absolute BytePos to a CharPos relative to the file it is + // located in + fn bytepos_to_local_charpos(&self, +bpos: BytePos) -> CharPos { + debug!("codemap: converting %? to char pos", bpos); + let idx = self.lookup_filemap_idx(bpos); + let map = self.files[idx]; + + // The number of extra bytes due to multibyte chars in the FileMap + let mut total_extra_bytes = 0; + + for map.multibyte_chars.each |mbc| { + debug!("codemap: %?-byte char at %?", mbc.bytes, mbc.pos); + if mbc.pos < bpos { + total_extra_bytes += mbc.bytes; + // We should never see a byte position in the middle of a + // character + assert bpos == mbc.pos + || bpos.to_uint() >= mbc.pos.to_uint() + mbc.bytes; + } else { + break; + } + } + + CharPos(bpos.to_uint() - total_extra_bytes) + } } // diff --git a/src/libsyntax/ext/qquote.rs b/src/libsyntax/ext/qquote.rs index 50d1e0033e276..888932e58e713 100644 --- a/src/libsyntax/ext/qquote.rs +++ b/src/libsyntax/ext/qquote.rs @@ -4,7 +4,7 @@ use parse::parser; use parse::parser::{Parser, parse_from_source_str}; use dvec::DVec; use parse::token::ident_interner; -use codemap::CharPos; +use codemap::{CharPos, BytePos}; use fold::*; use visit::*; @@ -16,13 +16,13 @@ use io::*; use codemap::span; struct gather_item { - lo: CharPos, - hi: CharPos, + lo: BytePos, + hi: BytePos, e: @ast::expr, constr: ~str } -type aq_ctxt = @{lo: CharPos, gather: DVec}; +type aq_ctxt = @{lo: BytePos, gather: DVec}; enum fragment { from_expr(@ast::expr), from_ty(@ast::Ty) @@ -115,7 +115,7 @@ impl @ast::pat: qq_helper { fn get_fold_fn() -> ~str {~"fold_pat"} } -fn gather_anti_quotes(lo: CharPos, node: N) -> aq_ctxt +fn gather_anti_quotes(lo: BytePos, node: N) -> aq_ctxt { let v = @{visit_expr: |node, &&cx, v| visit_aq(node, ~"from_expr", cx, v), visit_ty: |node, &&cx, v| visit_aq(node, ~"from_ty", cx, v), @@ -227,7 +227,7 @@ fn finish let mut str2 = ~""; enum state {active, skip(uint), blank}; let mut state = active; - let mut i = CharPos(0u); + let mut i = BytePos(0u); let mut j = 0u; let g_len = cx.gather.len(); for str::chars_each(*str) |ch| { @@ -244,7 +244,7 @@ fn finish blank if is_space(ch) => str::push_char(&mut str2, ch), blank => str::push_char(&mut str2, ' ') } - i += CharPos(1u); + i += BytePos(1u); if (j < g_len && i == cx.gather[j].hi) { assert ch == ')'; state = active; diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 2f371e1f8c581..6779ed263d5a8 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -11,7 +11,7 @@ use dvec::DVec; use ast::{matcher, match_tok, match_seq, match_nonterminal, ident}; use ast_util::mk_sp; use std::map::HashMap; -use codemap::CharPos; +use codemap::BytePos; /* This is an Earley-like parser, without support for in-grammar nonterminals, only by calling out to the main rust parser for named nonterminals (which it @@ -103,7 +103,7 @@ type matcher_pos = ~{ mut up: matcher_pos_up, // mutable for swapping only matches: ~[DVec<@named_match>], match_lo: uint, match_hi: uint, - sp_lo: CharPos, + sp_lo: BytePos, }; fn copy_up(&& mpu: matcher_pos_up) -> matcher_pos { @@ -123,7 +123,7 @@ fn count_names(ms: &[matcher]) -> uint { } #[allow(non_implicitly_copyable_typarams)] -fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: CharPos) +fn initial_matcher_pos(ms: ~[matcher], sep: Option, lo: BytePos) -> matcher_pos { let mut match_idx_hi = 0u; for ms.each() |elt| { diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 31528c10fe1aa..f0cb1d4ba3e1c 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -14,7 +14,7 @@ trait parser_attr { -> attr_or_ext; fn parse_outer_attributes() -> ~[ast::attribute]; fn parse_attribute(style: ast::attr_style) -> ast::attribute; - fn parse_attribute_naked(style: ast::attr_style, lo: CharPos) -> + fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) -> ast::attribute; fn parse_inner_attrs_and_next() -> {inner: ~[ast::attribute], next: ~[ast::attribute]}; @@ -85,7 +85,7 @@ impl Parser: parser_attr { return self.parse_attribute_naked(style, lo); } - fn parse_attribute_naked(style: ast::attr_style, lo: CharPos) -> + fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) -> ast::attribute { self.expect(token::LBRACKET); let meta_item = self.parse_meta_item(); diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 92736b9f36198..589a5f25ecfaa 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -28,7 +28,7 @@ impl cmnt_style : cmp::Eq { } } -type cmnt = {style: cmnt_style, lines: ~[~str], pos: CharPos}; +type cmnt = {style: cmnt_style, lines: ~[~str], pos: BytePos}; fn is_doc_comment(s: ~str) -> bool { s.starts_with(~"///") || @@ -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.ch}); + comments.push({style: blank_line, lines: v, pos: rdr.last_pos.byte}); } 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.ch; + let p = rdr.last_pos.byte; 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.ch; + let p = rdr.last_pos.byte; 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.ch; + let p = rdr.last_pos.byte; let mut lines: ~[~str] = ~[]; let mut col: CharPos = rdr.col; bump(rdr); @@ -284,7 +284,7 @@ fn consume_comment(rdr: string_reader, code_to_the_left: bool, debug!("<<< consume comment"); } -type lit = {lit: ~str, pos: CharPos}; +type lit = {lit: ~str, pos: BytePos}; fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, path: ~str, diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 66930009eb896..1811951fc0e9a 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -205,7 +205,7 @@ impl Parser: parser_common { if self.token == token::GT { self.bump(); } else if self.token == token::BINOP(token::SHR) { - self.swap(token::GT, self.span.lo + CharPos(1u), self.span.hi); + self.swap(token::GT, self.span.lo + BytePos(1u), self.span.hi); } else { let mut s: ~str = ~"expected `"; s += token_to_str(self.reader, token::GT); diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 1eadaec1fcdf6..00a0b40ab65f8 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -23,7 +23,7 @@ type string_reader = @{ src: @~str, // The absolute offset within the codemap of the next character to read mut pos: FilePos, - // The absolute offset within the codemap of the last character to be read (curr) + // The absolute offset within the codemap of the last character read(curr) mut last_pos: FilePos, // The column of the next character to read mut col: CharPos, @@ -123,9 +123,9 @@ fn string_advance_token(&&r: string_reader) { if is_eof(r) { r.peek_tok = token::EOF; } else { - let start_chpos = r.last_pos.ch; + let start_bytepos = r.last_pos.byte; r.peek_tok = next_token_inner(r); - r.peek_span = ast_util::mk_sp(start_chpos, r.last_pos.ch); + r.peek_span = ast_util::mk_sp(start_bytepos, r.last_pos.byte); }; } @@ -158,6 +158,11 @@ fn bump(rdr: string_reader) { rdr.filemap.next_line(rdr.last_pos); rdr.col = CharPos(0u); } + + if byte_offset_diff > 1 { + rdr.filemap.record_multibyte_char( + BytePos(current_byte_offset), byte_offset_diff); + } } else { // XXX: What does this accomplish? if (rdr.curr != -1 as char) { @@ -233,7 +238,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_chpos = rdr.pos.ch - CharPos(2u); + let start_bpos = rdr.pos.byte - BytePos(2u); let mut acc = ~"//"; while rdr.curr != '\n' && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -241,7 +246,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_chpos, rdr.pos.ch) + sp: ast_util::mk_sp(start_bpos, rdr.pos.byte) }); } else { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } @@ -256,7 +261,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.ch); + let loc = cmap.lookup_char_pos_adj(rdr.last_pos.byte); if loc.line == 1u && loc.col == CharPos(0u) { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } return consume_whitespace_and_comments(rdr); @@ -272,7 +277,7 @@ fn consume_block_comment(rdr: string_reader) // block comments starting with "/**" or "/*!" are doc-comments if rdr.curr == '*' || rdr.curr == '!' { - let start_chpos = rdr.pos.ch - CharPos(2u); + let start_bpos = rdr.pos.byte - BytePos(2u); let mut acc = ~"/*"; while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -286,7 +291,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_chpos, rdr.pos.ch) + sp: ast_util::mk_sp(start_bpos, rdr.pos.byte) }); } } else { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index bd314d6bcaf5d..74d06789ad899 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5,7 +5,7 @@ use either::{Either, Left, Right}; use std::map::HashMap; use token::{can_begin_expr, is_ident, is_ident_or_path, is_plain_ident, INTERPOLATED, special_idents}; -use codemap::{span,FssNone, CharPos}; +use codemap::{span,FssNone, BytePos}; use util::interner::Interner; use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec}; use lexer::reader; @@ -244,7 +244,7 @@ impl Parser { self.token = next.tok; self.span = next.sp; } - fn swap(next: token::Token, +lo: CharPos, +hi: CharPos) { + fn swap(next: token::Token, +lo: BytePos, +hi: BytePos) { self.token = next; self.span = mk_sp(lo, hi); } @@ -904,12 +904,12 @@ impl Parser { return spanned(lo, e.span.hi, {mutbl: m, ident: i, expr: e}); } - fn mk_expr(+lo: CharPos, +hi: CharPos, +node: expr_) -> @expr { + fn mk_expr(+lo: BytePos, +hi: BytePos, +node: expr_) -> @expr { return @{id: self.get_id(), callee_id: self.get_id(), node: node, span: mk_sp(lo, hi)}; } - fn mk_mac_expr(+lo: CharPos, +hi: CharPos, m: mac_) -> @expr { + fn mk_mac_expr(+lo: BytePos, +hi: BytePos, m: mac_) -> @expr { return @{id: self.get_id(), callee_id: self.get_id(), node: expr_mac({node: m, span: mk_sp(lo, hi)}), @@ -1134,7 +1134,7 @@ impl Parser { return self.mk_expr(lo, hi, ex); } - fn parse_block_expr(lo: CharPos, blk_mode: blk_check_mode) -> @expr { + fn parse_block_expr(lo: BytePos, blk_mode: blk_check_mode) -> @expr { self.expect(token::LBRACE); let blk = self.parse_block_tail(lo, blk_mode); return self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk)); @@ -1146,7 +1146,7 @@ impl Parser { return self.parse_syntax_ext_naked(lo); } - fn parse_syntax_ext_naked(lo: CharPos) -> @expr { + fn parse_syntax_ext_naked(lo: BytePos) -> @expr { match self.token { token::IDENT(_, _) => (), _ => self.fatal(~"expected a syntax expander name") @@ -2279,11 +2279,11 @@ impl Parser { // I guess that also means "already parsed the 'impure'" if // necessary, and this should take a qualifier. // some blocks start with "#{"... - fn parse_block_tail(lo: CharPos, s: blk_check_mode) -> blk { + fn parse_block_tail(lo: BytePos, s: blk_check_mode) -> blk { self.parse_block_tail_(lo, s, ~[]) } - fn parse_block_tail_(lo: CharPos, s: blk_check_mode, + fn parse_block_tail_(lo: BytePos, s: blk_check_mode, +first_item_attrs: ~[attribute]) -> blk { let mut stmts = ~[]; let mut expr = None; @@ -2581,7 +2581,7 @@ impl Parser { return {ident: id, tps: ty_params}; } - fn mk_item(+lo: CharPos, +hi: CharPos, +ident: ident, + fn mk_item(+lo: BytePos, +hi: BytePos, +ident: ident, +node: item_, vis: visibility, +attrs: ~[attribute]) -> @item { return @{ident: ident, @@ -3037,7 +3037,7 @@ impl Parser { items: items}; } - fn parse_item_foreign_mod(lo: CharPos, + fn parse_item_foreign_mod(lo: BytePos, visibility: visibility, attrs: ~[attribute], items_allowed: bool) @@ -3092,7 +3092,7 @@ impl Parser { }); } - fn parse_type_decl() -> {lo: CharPos, ident: ident} { + fn parse_type_decl() -> {lo: BytePos, ident: ident} { let lo = self.last_span.lo; let id = self.parse_ident(); return {lo: lo, ident: id}; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 910cb89ec7570..949d2defa9a2a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1,5 +1,5 @@ use parse::{comments, lexer, token}; -use codemap::{CodeMap, CharPos}; +use codemap::{CodeMap, BytePos}; use pp::{break_offset, word, printer, space, zerobreak, hardbreak, breaks}; use pp::{consistent, inconsistent, eof}; use ast::{required, provided}; @@ -1898,7 +1898,7 @@ fn print_ty_fn(s: ps, } fn maybe_print_trailing_comment(s: ps, span: codemap::span, - next_pos: Option) { + next_pos: Option) { let mut cm; match s.cm { Some(ccm) => cm = ccm, _ => return } match next_comment(s) { @@ -1906,7 +1906,7 @@ fn maybe_print_trailing_comment(s: ps, span: codemap::span, if cmnt.style != comments::trailing { return; } let span_line = cm.lookup_char_pos(span.hi); let comment_line = cm.lookup_char_pos(cmnt.pos); - let mut next = cmnt.pos + CharPos(1u); + let mut next = cmnt.pos + BytePos(1u); match next_pos { None => (), Some(p) => next = p } if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { @@ -1981,7 +1981,7 @@ fn lit_to_str(l: @ast::lit) -> ~str { return to_str(l, print_literal, parse::token::mk_fake_ident_interner()); } -fn next_lit(s: ps, pos: CharPos) -> Option { +fn next_lit(s: ps, pos: BytePos) -> Option { match s.literals { Some(lits) => { while s.cur_lit < vec::len(lits) { @@ -1996,7 +1996,7 @@ fn next_lit(s: ps, pos: CharPos) -> Option { } } -fn maybe_print_comment(s: ps, pos: CharPos) { +fn maybe_print_comment(s: ps, pos: BytePos) { loop { match next_comment(s) { Some(cmnt) => { From 2374154ded19a247be0053d71f5bcdd22b95462a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 14:10:17 -0800 Subject: [PATCH 23/56] 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 13f2ea4e210a6..5e600b489654a 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 c5ffbb2ab0a39..9c8d28680ed23 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 589a5f25ecfaa..e3ac3e7dcbbb4 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 00a0b40ab65f8..10a74d620a370 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 { From 4a5b28ff0cae0e48d8e3e0cf45728e438390d7ee Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 14:22:09 -0800 Subject: [PATCH 24/56] Stop tracking CodeMap offsets in the parse session. Big simplification --- src/libsyntax/codemap.rs | 43 +++++++---------- src/libsyntax/ext/source_util.rs | 3 +- src/libsyntax/parse.rs | 81 +++++++++----------------------- src/libsyntax/parse/comments.rs | 3 +- src/libsyntax/parse/eval.rs | 14 ++---- 5 files changed, 48 insertions(+), 96 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 5e600b489654a..91aef76061e68 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -201,25 +201,6 @@ pub struct FileMap { } pub impl FileMap { - static fn new_w_substr(+filename: FileName, +substr: FileSubstr, - src: @~str, - +start_pos: BytePos) - -> FileMap { - return FileMap { - name: filename, substr: substr, src: src, - start_pos: start_pos, - mut lines: ~[], - multibyte_chars: DVec() - }; - } - - static fn new(+filename: FileName, src: @~str, - +start_pos: BytePos) - -> FileMap { - return FileMap::new_w_substr(filename, FssNone, src, - start_pos); - } - fn next_line(&self, +pos: BytePos) { self.lines.push(pos); } @@ -262,20 +243,30 @@ pub impl CodeMap { } } - pub fn add_filemap(&self, filemap: @FileMap) { - let expected_byte_pos = if self.files.len() == 0 { + fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr, + src: @~str) -> @FileMap { + let start_pos = if self.files.len() == 0 { 0 } else { 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.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); - assert expected_byte_pos == actual_byte_pos; + + let filemap = @FileMap { + name: filename, substr: substr, src: src, + start_pos: BytePos(start_pos), + mut lines: ~[], + multibyte_chars: DVec() + }; + self.files.push(filemap); + + return filemap; + } + + fn new_filemap(+filename: FileName, src: @~str) -> @FileMap { + return self.new_filemap_w_substr(filename, FssNone, src); } pub fn mk_substr_filename(&self, sp: span) -> ~str { diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 726dbbb56e2a4..93cfaadcbd378 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -58,12 +58,11 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args = get_mac_args(cx, sp, arg, 1u, option::Some(1u), ~"include"); let file = expr_to_str(cx, args[0], ~"#include_str requires a string"); - let (p, rdr) = parse::new_parser_etc_from_file( + let p = parse::new_parser_from_file( cx.parse_sess(), cx.cfg(), &res_rel_file(cx, sp, &Path(file)), parse::parser::SOURCE_FILE); let e = p.parse_expr(); - parse::update_parse_sess_position(&cx.parse_sess(), &rdr); return e; } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 9c8d28680ed23..d824f9f8a0340 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -11,7 +11,6 @@ export parse_crate_from_source_str; export parse_expr_from_source_str, parse_item_from_source_str; export parse_stmt_from_source_str; export parse_from_source_str; -export update_parse_sess_position; use parser::Parser; use attr::parser_attr; @@ -28,8 +27,6 @@ type parse_sess = @{ mut next_id: node_id, span_diagnostic: span_handler, interner: @ident_interner, - // must be kept up to date - mut pos: BytePos }; fn new_parse_sess(demitter: Option) -> parse_sess { @@ -38,7 +35,6 @@ 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: BytePos(0) }; } @@ -48,7 +44,6 @@ 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: BytePos(0) }; } @@ -66,14 +61,13 @@ fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, - parser::CRATE_FILE); + let p = new_parser_from_file(sess, cfg, input, + parser::CRATE_FILE); let lo = p.span.lo; let prefix = input.dir_path(); let leading_attrs = p.parse_inner_attrs_and_next(); let { inner: crate_attrs, next: first_cdir_attr } = leading_attrs; let cdirs = p.parse_crate_directives(token::EOF, first_cdir_attr); - update_parse_sess_position(&sess, &rdr); let cx = @{sess: sess, cfg: /* FIXME (#2543) */ copy p.cfg}; let companionmod = input.filestem().map(|s| Path(*s)); let (m, attrs) = eval::eval_crate_directives_to_mod( @@ -90,52 +84,47 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let (p, rdr) = new_parser_etc_from_file(sess, cfg, input, - parser::SOURCE_FILE); + let p = new_parser_from_file(sess, cfg, input, + parser::SOURCE_FILE); let r = p.parse_crate_mod(cfg); - update_parse_sess_position(&sess, &rdr); return r; } fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::FssNone, source); + let p = new_parser_from_source_str(sess, cfg, name, + codemap::FssNone, source); let r = p.parse_crate_mod(cfg); p.abort_if_errors(); - update_parse_sess_position(&sess, &rdr); return r; } fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::expr { - let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::FssNone, source); + let p = new_parser_from_source_str(sess, cfg, name, + codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); - update_parse_sess_position(&sess, &rdr); return r; } fn parse_item_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, +attrs: ~[ast::attribute], sess: parse_sess) -> Option<@ast::item> { - let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::FssNone, source); + let p = new_parser_from_source_str(sess, cfg, name, + codemap::FssNone, source); let r = p.parse_item(attrs); p.abort_if_errors(); - update_parse_sess_position(&sess, &rdr); return r; } fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, +attrs: ~[ast::attribute], sess: parse_sess) -> @ast::stmt { - let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, - codemap::FssNone, source); + let p = new_parser_from_source_str(sess, cfg, name, + codemap::FssNone, source); let r = p.parse_stmt(attrs); p.abort_if_errors(); - update_parse_sess_position(&sess, &rdr); return r; } @@ -145,14 +134,13 @@ fn parse_from_source_str(f: fn (p: Parser) -> T, sess: parse_sess) -> T { - let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, ss, - source); + let p = new_parser_from_source_str(sess, cfg, name, ss, + source); let r = f(p); if !p.reader.is_eof() { p.reader.fatal(~"expected end-of-string"); } p.abort_if_errors(); - update_parse_sess_position(&sess, &rdr); move r } @@ -164,47 +152,28 @@ fn next_node_id(sess: parse_sess) -> node_id { return rv; } -fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, - +name: ~str, +ss: codemap::FileSubstr, - source: @~str) -> (Parser, string_reader) { - let ftype = parser::SOURCE_FILE; - let filemap = @FileMap::new_w_substr - (name, ss, source, sess.pos); - sess.cm.add_filemap(filemap); - let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, - sess.interner); - return (Parser(sess, cfg, srdr as reader, ftype), srdr); -} - fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::FileSubstr, source: @~str) -> Parser { - let (p, _) = new_parser_etc_from_source_str(sess, cfg, name, ss, source); - move p + let ftype = parser::SOURCE_FILE; + let filemap = sess.cm.new_filemap_w_substr(name, ss, source); + let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, + sess.interner); + return Parser(sess, cfg, srdr as reader, ftype); } - -fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, ftype: parser::file_type) -> - (Parser, string_reader) { +fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, + path: &Path, ftype: parser::file_type) -> Parser { let res = io::read_whole_file_str(path); match res { result::Ok(_) => { /* Continue. */ } result::Err(e) => sess.span_diagnostic.handler().fatal(e) } let src = @result::unwrap(res); - let filemap = @FileMap::new(path.to_str(), src, - sess.pos); - sess.cm.add_filemap(filemap); + let filemap = sess.cm.new_filemap(path.to_str(), src); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - return (Parser(sess, cfg, srdr as reader, ftype), srdr); -} - -fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: &Path, - ftype: parser::file_type) -> Parser { - let (p, _) = new_parser_etc_from_file(sess, cfg, path, ftype); - move p + return Parser(sess, cfg, srdr as reader, ftype); } fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, @@ -213,7 +182,3 @@ fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, None, tt); return Parser(sess, cfg, trdr as reader, parser::SOURCE_FILE) } - -fn update_parse_sess_position(sess: &parse_sess, r: &lexer::string_reader) { - sess.pos = r.last_pos -} diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index e3ac3e7dcbbb4..2a8bbe3b6d862 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -292,7 +292,8 @@ 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, BytePos(0)); + let cm = CodeMap::new(); + let filemap = cm.new_filemap(path, src); let rdr = lexer::new_low_level_string_reader( span_diagnostic, filemap, itr); diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 47dbc0cd6eea1..f08f195446442 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,4 +1,3 @@ -use parse::update_parse_sess_position; use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; @@ -63,11 +62,10 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option) let modpath = &companion_file(prefix, suffix); if file_exists(modpath) { debug!("found companion mod"); - let (p0, r0) = new_parser_etc_from_file(cx.sess, cx.cfg, - modpath, SOURCE_FILE); + let p0 = new_parser_from_file(cx.sess, cx.cfg, + modpath, SOURCE_FILE); let inner_attrs = p0.parse_inner_attrs_and_next(); let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next); - update_parse_sess_position(&cx.sess, &r0); return (m0.view_items, m0.items, inner_attrs.inner); } else { return (~[], ~[], ~[]); @@ -93,9 +91,9 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, } else { prefix.push_many(file_path.components) }; - let (p0, r0) = - new_parser_etc_from_file(cx.sess, cx.cfg, - &full_path, SOURCE_FILE); + let p0 = + new_parser_from_file(cx.sess, cx.cfg, + &full_path, SOURCE_FILE); let inner_attrs = p0.parse_inner_attrs_and_next(); let mod_attrs = vec::append(attrs, inner_attrs.inner); let first_item_outer_attrs = inner_attrs.next; @@ -104,8 +102,6 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, let i = p0.mk_item(cdir.span.lo, cdir.span.hi, /* FIXME (#2543) */ copy id, ast::item_mod(m0), vis, mod_attrs); - // Thread defids, chpos and byte_pos through the parsers - update_parse_sess_position(&cx.sess, &r0); items.push(i); } ast::cdir_dir_mod(vis, id, cdirs, attrs) => { From 1ac28c36ac70c412445258c535486edaf6140efa Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 14:35:43 -0800 Subject: [PATCH 25/56] Remove unused types from codemap --- src/libsyntax/codemap.rs | 15 --------------- src/libsyntax/parse.rs | 2 +- src/libsyntax/parse/lexer.rs | 2 +- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 91aef76061e68..8f42e1b1a59de 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -147,19 +147,6 @@ pub struct Loc { file: @FileMap, line: uint, col: CharPos } -/// An absolute offset within the CodeMap (not a relative offset within a -/// a single FileMap) -pub struct FilePos { - ch: CharPos, byte: BytePos -} - -impl FilePos : cmp::Eq { - pure fn eq(other: &FilePos) -> bool { - self.ch == (*other).ch && self.byte == (*other).byte - } - pure fn ne(other: &FilePos) -> bool { !self.eq(other) } -} - pub enum ExpnInfo { ExpandedFrom({call_site: span, callie: {name: ~str, span: Option}}) @@ -167,8 +154,6 @@ pub enum ExpnInfo { pub type FileName = ~str; -pub type LookupFn = pure fn(FilePos) -> uint; - pub struct FileLines { file: @FileMap, lines: ~[uint] diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index d824f9f8a0340..593ff6d034ee0 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::{CodeMap, FileMap, CharPos, BytePos, FilePos}; +use codemap::{CodeMap, FileMap, CharPos, BytePos}; type parse_sess = @{ cm: @codemap::CodeMap, diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 10a74d620a370..6f1f644ab27b1 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -1,5 +1,5 @@ use diagnostic::span_handler; -use codemap::{span, CodeMap, CharPos, BytePos, FilePos}; +use codemap::{span, CodeMap, CharPos, BytePos}; use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, tt_next_token}; From 2af088529fbb4baf2fa4e71735c27ffa9edefd3d Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 14:55:52 -0800 Subject: [PATCH 26/56] Remove incorrect comment from codemap --- src/libsyntax/codemap.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 8f42e1b1a59de..cb039b25ccf61 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -141,8 +141,6 @@ impl span: Deserializable { } } -// XXX col shouldn't be CharPos because col is not an absolute location in the -// codemap, and BytePos and CharPos always represent absolute positions pub struct Loc { file: @FileMap, line: uint, col: CharPos } From 428c58b9f983d31f9c7df2d48d45f6a22996692e Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 16 Nov 2012 15:12:14 -0800 Subject: [PATCH 27/56] Forbid duplicate supertraits I actually already pushed most of this by accident before I meant to, but r=nmatsakis anyway. Closes #3953 --- src/librustc/middle/typeck/collect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/typeck/collect.rs b/src/librustc/middle/typeck/collect.rs index 7fc0b65d10cae..0c6fe58576810 100644 --- a/src/librustc/middle/typeck/collect.rs +++ b/src/librustc/middle/typeck/collect.rs @@ -264,7 +264,7 @@ fn ensure_supertraits(ccx: @crate_ctxt, for trait_refs.each |trait_ref| { let (did, tpt) = instantiate_trait_ref(ccx, *trait_ref, rp); if instantiated.any(|other_trait: &InstantiatedTraitRef| - { (*other_trait).def_id == did }) { + { other_trait.def_id == did }) { // This means a trait inherited from the same supertrait more // than once. tcx.sess.span_err(sp, ~"Duplicate supertrait in trait \ From 7c72fd89f103a1629525b87d98b3adb4d1e6bd14 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 15:14:11 -0800 Subject: [PATCH 28/56] Add some docs to codemap --- src/libsyntax/codemap.rs | 61 +++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index cb039b25ccf61..63750e6cd65fe 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -1,11 +1,15 @@ -/*! 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. - * - * All represented positions are *absolute* positions within the codemap, - * not relative positions within a single file. - */ +/*! + +The CodeMap tracks all the source code used within a single crate, mapping +from integer byte positions to the original source code location. Each bit of +source parsed during crate parsing (typically files, in-memory strings, or +various bits of macro expansion) cover a continuous range of bytes in the +CodeMap and are represented by FileMaps. Byte positions are stored in `spans` +and used pervasively in the compiler. They are absolute positions within the +CodeMap, which upon request can be converted to line and column information, +source code snippets, etc. + +*/ use dvec::DVec; use std::serialization::{Serializable, @@ -18,9 +22,16 @@ trait Pos { pure fn to_uint(&self) -> uint; } +/// A byte offset pub enum BytePos = uint; +/// A character offset. Because of multibyte utf8 characters, a byte offset +/// is not equivalent to a character offset. The CodeMap will convert BytePos +/// values to CharPos values as necessary. pub enum CharPos = uint; +// XXX: Lots of boilerplate in these impls, but so far my attempts to fix +// have been unsuccessful + impl BytePos: Pos { static pure fn from_uint(n: uint) -> BytePos { BytePos(n) } pure fn to_uint(&self) -> uint { **self } @@ -117,6 +128,12 @@ impl CharPos: to_bytes::IterBytes { } } +/** +Spans represent a region of code, used for error reporting. Positions in spans +are *absolute* positions from the beginning of the codemap, not positions +relative to FileMaps. Methods on the CodeMap can be used to relate spans back +to the original source. +*/ pub struct span { lo: BytePos, hi: BytePos, @@ -141,10 +158,17 @@ impl span: Deserializable { } } +/// A source code location used for error reporting pub struct Loc { - file: @FileMap, line: uint, col: CharPos + /// Information about the original source + file: @FileMap, + /// The (1-based) line number + line: uint, + /// The (0-based) column offset + col: CharPos } +/// Extra information for tracking macro expansion of spans pub enum ExpnInfo { ExpandedFrom({call_site: span, callie: {name: ~str, span: Option}}) @@ -174,12 +198,21 @@ pub struct MultiByteChar { sum: uint } +/// A single source in the CodeMap pub struct FileMap { + /// The name of the file that the source came from, source that doesn't + /// originate from files has names between angle brackets by convention, + /// e.g. `` name: FileName, + /// Extra information used by qquote substr: FileSubstr, + /// The complete source code src: @~str, + /// The start position of this source in the CodeMap start_pos: BytePos, + /// Locations of lines beginnings in the source code mut lines: ~[BytePos], + /// Locations of multi-byte characters in the source code multibyte_chars: DVec } @@ -226,6 +259,11 @@ pub impl CodeMap { } } + /// Add a new FileMap to the CodeMap and return it + fn new_filemap(+filename: FileName, src: @~str) -> @FileMap { + return self.new_filemap_w_substr(filename, FssNone, src); + } + fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr, src: @~str) -> @FileMap { let start_pos = if self.files.len() == 0 { @@ -248,16 +286,13 @@ pub impl CodeMap { return filemap; } - fn new_filemap(+filename: FileName, src: @~str) -> @FileMap { - return self.new_filemap_w_substr(filename, FssNone, src); - } - 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.to_uint()); } + /// Lookup source information about a BytePos pub fn lookup_char_pos(&self, +pos: BytePos) -> Loc { return self.lookup_pos(pos); } From e621e68c60d02bb33a2d808071f3f07674db871c Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 16 Nov 2012 15:52:33 -0800 Subject: [PATCH 29/56] Remove unused MultiByteChar.sum field from codemap --- src/libsyntax/codemap.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 63750e6cd65fe..d291d9545eb6a 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -193,9 +193,6 @@ pub struct MultiByteChar { pos: BytePos, /// The number of bytes, >=2 bytes: uint, - /// The complete number of 'extra' bytes through this character in the - /// FileMap - sum: uint } /// A single source in the CodeMap @@ -233,16 +230,9 @@ pub impl FileMap { pub fn record_multibyte_char(&self, pos: BytePos, bytes: uint) { assert bytes >=2 && bytes <= 4; - let sum = if self.multibyte_chars.len() > 0 { - self.multibyte_chars.last().sum - } else { - 0 - }; - let sum = sum + bytes; let mbc = MultiByteChar { pos: pos, bytes: bytes, - sum: sum }; self.multibyte_chars.push(mbc); } From ec8bfdd63c18cc986cedf8b891773e844a438586 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 08:41:47 -0800 Subject: [PATCH 30/56] Made Map.contains_key, contains_key_ref, and get pure. --- src/libcore/mutable.rs | 2 +- src/libstd/map.rs | 18 +++++++++--------- src/libstd/smallintmap.rs | 8 ++++---- .../class-impl-very-parameterized-trait.rs | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs index 56a6df2c4ddf1..eafdf58e67e9a 100644 --- a/src/libcore/mutable.rs +++ b/src/libcore/mutable.rs @@ -48,7 +48,7 @@ impl Data { } } - fn borrow_const(op: &fn(t: &const T) -> R) -> R { + pure fn borrow_const(op: &fn(t: &const T) -> R) -> R { op(&const self.value) } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 0ee7cb6fcf967..915202143a1ea 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -30,17 +30,17 @@ pub trait Map { fn insert(v: K, v: V) -> bool; /// Returns true if the map contains a value for the specified key - fn contains_key(key: K) -> bool; + pure fn contains_key(key: K) -> bool; /// Returns true if the map contains a value for the specified /// key, taking the key by reference. - fn contains_key_ref(key: &K) -> bool; + pure fn contains_key_ref(key: &K) -> bool; /** * Get the value for the specified key. Fails if the key does not exist in * the map. */ - fn get(key: K) -> V; + pure fn get(key: K) -> V; /** * Get the value for the specified key. If the key does not exist in @@ -200,11 +200,11 @@ pub mod chained { impl T: Map { pure fn size() -> uint { self.count } - fn contains_key(k: K) -> bool { + pure fn contains_key(k: K) -> bool { self.contains_key_ref(&k) } - fn contains_key_ref(k: &K) -> bool { + pure fn contains_key_ref(k: &K) -> bool { let hash = k.hash_keyed(0,0) as uint; match self.search_tbl(k, hash) { NotFound => false, @@ -264,7 +264,7 @@ pub mod chained { } } - fn get(k: K) -> V { + pure fn get(k: K) -> V { let opt_v = self.find(k); if opt_v.is_none() { fail fmt!("Key not found in table: %?", k); @@ -421,19 +421,19 @@ impl @Mut>: } } - fn contains_key(key: K) -> bool { + pure fn contains_key(key: K) -> bool { do self.borrow_const |p| { p.contains_key(&key) } } - fn contains_key_ref(key: &K) -> bool { + pure fn contains_key_ref(key: &K) -> bool { do self.borrow_const |p| { p.contains_key(key) } } - fn get(key: K) -> V { + pure fn get(key: K) -> V { do self.borrow_const |p| { p.get(&key) } diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 1582d90ce2d62..9dc216a21557a 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -60,7 +60,7 @@ pub pure fn get(self: SmallIntMap, key: uint) -> T { } /// Returns true if the map contains a value for the specified key -pub fn contains_key(self: SmallIntMap, key: uint) -> bool { +pub pure fn contains_key(self: SmallIntMap, key: uint) -> bool { return !find(self, key).is_none(); } @@ -93,13 +93,13 @@ impl SmallIntMap: map::Map { fn clear() { self.v.set(~[]); } - fn contains_key(key: uint) -> bool { + pure fn contains_key(key: uint) -> bool { contains_key(self, key) } - fn contains_key_ref(key: &uint) -> bool { + pure fn contains_key_ref(key: &uint) -> bool { contains_key(self, *key) } - fn get(key: uint) -> V { get(self, key) } + pure fn get(key: uint) -> V { get(self, key) } pure fn find(key: uint) -> Option { find(self, key) } fn rehash() { fail } diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index 8c25d94db3bed..4cfcbf67cc9aa 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -47,10 +47,10 @@ impl cat : Map { self.meows += k; true } - fn contains_key(+k: int) -> bool { k <= self.meows } - fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } + pure fn contains_key(+k: int) -> bool { k <= self.meows } + pure fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } - fn get(+k:int) -> T { match self.find(k) { + pure fn get(+k:int) -> T { match self.find(k) { Some(v) => { v } None => { fail ~"epic fail"; } } From 053ba742c5829b95c760420762b3f4d61438fc1f Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:11:57 -0800 Subject: [PATCH 31/56] Moved strptime and strftime into private helper functions. Makes the public API much easier to see and prepares the way for making them pure. --- src/libstd/time.rs | 160 ++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 76 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 75909273392f4..23ec6fef00300 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -152,6 +152,89 @@ pub fn now() -> Tm { /// Parses the time from the string according to the format string. pub fn strptime(s: &str, format: &str) -> Result { + do_strptime(s, format) +} + +pub fn strftime(format: &str, tm: Tm) -> ~str { + do_strftime(format, tm) +} + +impl Tm { + /// Convert time to the seconds from January 1, 1970 + fn to_timespec() -> Timespec { + let mut sec = 0i64; + if self.tm_gmtoff == 0_i32 { + rustrt::rust_timegm(self, &mut sec); + } else { + rustrt::rust_mktime(self, &mut sec); + } + { sec: sec, nsec: self.tm_nsec } + } + + /// Convert time to the local timezone + fn to_local() -> Tm { + at(self.to_timespec()) + } + + /// Convert time to the UTC + fn to_utc() -> Tm { + at_utc(self.to_timespec()) + } + + /** + * Return a string of the current time in the form + * "Thu Jan 1 00:00:00 1970". + */ + fn ctime() -> ~str { self.strftime(~"%c") } + + /// Formats the time according to the format string. + fn strftime(format: &str) -> ~str { strftime(format, self) } + + /** + * Returns a time string formatted according to RFC 822. + * + * local: "Thu, 22 Mar 2012 07:53:18 PST" + * utc: "Thu, 22 Mar 2012 14:53:18 UTC" + */ + fn rfc822() -> ~str { + if self.tm_gmtoff == 0_i32 { + self.strftime(~"%a, %d %b %Y %T GMT") + } else { + self.strftime(~"%a, %d %b %Y %T %Z") + } + } + + /** + * Returns a time string formatted according to RFC 822 with Zulu time. + * + * local: "Thu, 22 Mar 2012 07:53:18 -0700" + * utc: "Thu, 22 Mar 2012 14:53:18 -0000" + */ + fn rfc822z() -> ~str { + self.strftime(~"%a, %d %b %Y %T %z") + } + + /** + * Returns a time string formatted according to ISO 8601. + * + * local: "2012-02-22T07:53:18-07:00" + * utc: "2012-02-22T14:53:18Z" + */ + fn rfc3339() -> ~str { + if self.tm_gmtoff == 0_i32 { + self.strftime(~"%Y-%m-%dT%H:%M:%SZ") + } else { + let s = self.strftime(~"%Y-%m-%dT%H:%M:%S"); + let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' }; + let mut m = i32::abs(self.tm_gmtoff) / 60_i32; + let h = m / 60_i32; + m -= h * 60_i32; + s + fmt!("%c%02d:%02d", sign, h as int, m as int) + } + } +} + +priv fn do_strptime(s: &str, format: &str) -> Result { type TmMut = { mut tm_sec: i32, mut tm_min: i32, @@ -592,7 +675,7 @@ pub fn strptime(s: &str, format: &str) -> Result { } } -fn strftime(format: &str, tm: Tm) -> ~str { +priv fn do_strftime(format: &str, tm: Tm) -> ~str { fn parse_type(ch: char, tm: &Tm) -> ~str { //FIXME (#2350): Implement missing types. let die = || fmt!("strftime: can't understand this format %c ", ch); @@ -759,81 +842,6 @@ fn strftime(format: &str, tm: Tm) -> ~str { buf } -impl Tm { - /// Convert time to the seconds from January 1, 1970 - fn to_timespec() -> Timespec { - let mut sec = 0i64; - if self.tm_gmtoff == 0_i32 { - rustrt::rust_timegm(self, &mut sec); - } else { - rustrt::rust_mktime(self, &mut sec); - } - { sec: sec, nsec: self.tm_nsec } - } - - /// Convert time to the local timezone - fn to_local() -> Tm { - at(self.to_timespec()) - } - - /// Convert time to the UTC - fn to_utc() -> Tm { - at_utc(self.to_timespec()) - } - - /** - * Return a string of the current time in the form - * "Thu Jan 1 00:00:00 1970". - */ - fn ctime() -> ~str { self.strftime(~"%c") } - - /// Formats the time according to the format string. - fn strftime(format: &str) -> ~str { strftime(format, self) } - - /** - * Returns a time string formatted according to RFC 822. - * - * local: "Thu, 22 Mar 2012 07:53:18 PST" - * utc: "Thu, 22 Mar 2012 14:53:18 UTC" - */ - fn rfc822() -> ~str { - if self.tm_gmtoff == 0_i32 { - self.strftime(~"%a, %d %b %Y %T GMT") - } else { - self.strftime(~"%a, %d %b %Y %T %Z") - } - } - - /** - * Returns a time string formatted according to RFC 822 with Zulu time. - * - * local: "Thu, 22 Mar 2012 07:53:18 -0700" - * utc: "Thu, 22 Mar 2012 14:53:18 -0000" - */ - fn rfc822z() -> ~str { - self.strftime(~"%a, %d %b %Y %T %z") - } - - /** - * Returns a time string formatted according to ISO 8601. - * - * local: "2012-02-22T07:53:18-07:00" - * utc: "2012-02-22T14:53:18Z" - */ - fn rfc3339() -> ~str { - if self.tm_gmtoff == 0_i32 { - self.strftime(~"%Y-%m-%dT%H:%M:%SZ") - } else { - let s = self.strftime(~"%Y-%m-%dT%H:%M:%S"); - let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' }; - let mut m = i32::abs(self.tm_gmtoff) / 60_i32; - let h = m / 60_i32; - m -= h * 60_i32; - s + fmt!("%c%02d:%02d", sign, h as int, m as int) - } - } -} - #[cfg(test)] mod tests { #[legacy_exports]; From 7c87ff3f289195c564976ee49222487f2de836cd Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:17:43 -0800 Subject: [PATCH 32/56] Replace TmMut with inherited mutability --- src/libstd/time.rs | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 23ec6fef00300..d5a5deeae8c08 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -155,6 +155,7 @@ pub fn strptime(s: &str, format: &str) -> Result { do_strptime(s, format) } +/// Formats the time according to the format string. pub fn strftime(format: &str, tm: Tm) -> ~str { do_strftime(format, tm) } @@ -235,21 +236,6 @@ impl Tm { } priv fn do_strptime(s: &str, format: &str) -> Result { - type TmMut = { - mut tm_sec: i32, - mut tm_min: i32, - mut tm_hour: i32, - mut tm_mday: i32, - mut tm_mon: i32, - mut tm_year: i32, - mut tm_wday: i32, - mut tm_yday: i32, - mut tm_isdst: i32, - mut tm_gmtoff: i32, - mut tm_zone: ~str, - mut tm_nsec: i32, - }; - fn match_str(s: &str, pos: uint, needle: &str) -> bool { let mut i = pos; for str::each(needle) |ch| { @@ -312,7 +298,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { } } - fn parse_type(s: &str, pos: uint, ch: char, tm: &TmMut) + fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm_) -> Result { match ch { 'A' => match match_strs(s, pos, ~[ @@ -623,19 +609,19 @@ priv fn do_strptime(s: &str, format: &str) -> Result { } do io::with_str_reader(str::from_slice(format)) |rdr| { - let tm = { - mut tm_sec: 0_i32, - mut tm_min: 0_i32, - mut tm_hour: 0_i32, - mut tm_mday: 0_i32, - mut tm_mon: 0_i32, - mut tm_year: 0_i32, - mut tm_wday: 0_i32, - mut tm_yday: 0_i32, - mut tm_isdst: 0_i32, - mut tm_gmtoff: 0_i32, - mut tm_zone: ~"", - mut tm_nsec: 0_i32, + let mut tm = { + tm_sec: 0_i32, + tm_min: 0_i32, + tm_hour: 0_i32, + tm_mday: 0_i32, + tm_mon: 0_i32, + tm_year: 0_i32, + tm_wday: 0_i32, + tm_yday: 0_i32, + tm_isdst: 0_i32, + tm_gmtoff: 0_i32, + tm_zone: ~"", + tm_nsec: 0_i32, }; let mut pos = 0u; let len = str::len(s); @@ -645,7 +631,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { let {ch, next} = str::char_range_at(s, pos); match rdr.read_char() { - '%' => match parse_type(s, pos, rdr.read_char(), &tm) { + '%' => match parse_type(s, pos, rdr.read_char(), &mut tm) { Ok(next) => pos = next, Err(copy e) => { result = Err(e); break; } }, From d9995888fab34e9c67a72641020a793459237cd1 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:27:03 -0800 Subject: [PATCH 33/56] Made the time to string functions pure as well as empty_tm. This closes #3919. --- src/libstd/time.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index d5a5deeae8c08..912df9c7558dd 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -107,7 +107,7 @@ impl Tm : Eq { pure fn ne(other: &Tm) -> bool { *self != *(*other) } } -pub fn empty_tm() -> Tm { +pub pure fn empty_tm() -> Tm { Tm_({ tm_sec: 0_i32, tm_min: 0_i32, @@ -151,13 +151,17 @@ pub fn now() -> Tm { } /// Parses the time from the string according to the format string. -pub fn strptime(s: &str, format: &str) -> Result { - do_strptime(s, format) +pub pure fn strptime(s: &str, format: &str) -> Result { + // unsafe only because do_strptime is annoying to make pure + // (it does IO with a str_reader) + unsafe {do_strptime(s, format)} } /// Formats the time according to the format string. -pub fn strftime(format: &str, tm: Tm) -> ~str { - do_strftime(format, tm) +pub pure fn strftime(format: &str, tm: Tm) -> ~str { + // unsafe only because do_strftime is annoying to make pure + // (it does IO with a str_reader) + unsafe {do_strftime(format, tm)} } impl Tm { @@ -186,10 +190,10 @@ impl Tm { * Return a string of the current time in the form * "Thu Jan 1 00:00:00 1970". */ - fn ctime() -> ~str { self.strftime(~"%c") } + pure fn ctime() -> ~str { self.strftime(~"%c") } /// Formats the time according to the format string. - fn strftime(format: &str) -> ~str { strftime(format, self) } + pure fn strftime(format: &str) -> ~str { strftime(format, self) } /** * Returns a time string formatted according to RFC 822. @@ -197,7 +201,7 @@ impl Tm { * local: "Thu, 22 Mar 2012 07:53:18 PST" * utc: "Thu, 22 Mar 2012 14:53:18 UTC" */ - fn rfc822() -> ~str { + pure fn rfc822() -> ~str { if self.tm_gmtoff == 0_i32 { self.strftime(~"%a, %d %b %Y %T GMT") } else { @@ -211,7 +215,7 @@ impl Tm { * local: "Thu, 22 Mar 2012 07:53:18 -0700" * utc: "Thu, 22 Mar 2012 14:53:18 -0000" */ - fn rfc822z() -> ~str { + pure fn rfc822z() -> ~str { self.strftime(~"%a, %d %b %Y %T %z") } @@ -221,7 +225,7 @@ impl Tm { * local: "2012-02-22T07:53:18-07:00" * utc: "2012-02-22T14:53:18Z" */ - fn rfc3339() -> ~str { + pure fn rfc3339() -> ~str { if self.tm_gmtoff == 0_i32 { self.strftime(~"%Y-%m-%dT%H:%M:%SZ") } else { From a3f845db521aca3d8b8815a0c969a40446390757 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:56:05 -0800 Subject: [PATCH 34/56] Made Result.get, get_ref, is_ok, is_err, and iter methods pure. Note that the function versions were already pure. --- src/libcore/result.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 2df2974d0ba31..da7db607c8559 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -204,13 +204,13 @@ pub fn map_err(res: &Result, op: fn((&E)) -> F) } impl Result { - fn get_ref(&self) -> &self/T { get_ref(self) } + pure fn get_ref(&self) -> &self/T { get_ref(self) } - fn is_ok() -> bool { is_ok(&self) } + pure fn is_ok() -> bool { is_ok(&self) } - fn is_err() -> bool { is_err(&self) } + pure fn is_err() -> bool { is_err(&self) } - fn iter(f: fn((&T))) { + pure fn iter(f: fn((&T))) { match self { Ok(ref t) => f(t), Err(_) => () @@ -226,7 +226,7 @@ impl Result { } impl Result { - fn get() -> T { get(&self) } + pure fn get() -> T { get(&self) } fn map_err(op: fn((&E)) -> F) -> Result { match self { @@ -237,7 +237,7 @@ impl Result { } impl Result { - fn get_err() -> E { get_err(&self) } + pure fn get_err() -> E { get_err(&self) } fn map(op: fn((&T)) -> U) -> Result { match self { From e0805cb07eb9c245c4f1cc14ca7578360d271ba0 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:57:14 -0800 Subject: [PATCH 35/56] Made most of the URL functions pure. This closes #3782. --- src/libstd/net_url.rs | 74 +++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 8ea9513d15518..dd76f65a046bc 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -27,7 +27,7 @@ type UserInfo = { pub type Query = ~[(~str, ~str)]; -pub fn Url(scheme: ~str, user: Option, host: ~str, +pub pure fn Url(scheme: ~str, user: Option, host: ~str, port: Option<~str>, path: ~str, query: Query, fragment: Option<~str>) -> Url { Url { scheme: move scheme, user: move user, host: move host, @@ -35,7 +35,7 @@ pub fn Url(scheme: ~str, user: Option, host: ~str, fragment: move fragment } } -fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo { +pure fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo { {user: move user, pass: move pass} } @@ -84,8 +84,9 @@ fn encode_inner(s: &str, full_url: bool) -> ~str { * * This function is compliant with RFC 3986. */ -pub fn encode(s: &str) -> ~str { - encode_inner(s, true) +pub pure fn encode(s: &str) -> ~str { + // unsafe only because encode_inner does (string) IO + unsafe {encode_inner(s, true)} } /** @@ -95,8 +96,9 @@ pub fn encode(s: &str) -> ~str { * This function is compliant with RFC 3986. */ -pub fn encode_component(s: &str) -> ~str { - encode_inner(s, false) +pub pure fn encode_component(s: &str) -> ~str { + // unsafe only because encode_inner does (string) IO + unsafe {encode_inner(s, false)} } fn decode_inner(s: &str, full_url: bool) -> ~str { @@ -142,15 +144,17 @@ fn decode_inner(s: &str, full_url: bool) -> ~str { * * This will only decode escape sequences generated by encode_uri. */ -pub fn decode(s: &str) -> ~str { - decode_inner(s, true) +pub pure fn decode(s: &str) -> ~str { + // unsafe only because decode_inner does (string) IO + unsafe {decode_inner(s, true)} } /** * Decode a string encoded with percent encoding. */ -pub fn decode_component(s: &str) -> ~str { - decode_inner(s, false) +pub pure fn decode_component(s: &str) -> ~str { + // unsafe only because decode_inner does (string) IO + unsafe {decode_inner(s, false)} } fn encode_plus(s: &str) -> ~str { @@ -264,19 +268,21 @@ pub fn decode_form_urlencoded(s: ~[u8]) -> } -fn split_char_first(s: &str, c: char) -> (~str, ~str) { +pure fn split_char_first(s: &str, c: char) -> (~str, ~str) { let len = str::len(s); let mut index = len; let mut mat = 0; - do io::with_str_reader(s) |rdr| { - let mut ch : char; - while !rdr.eof() { - ch = rdr.read_byte() as char; - if ch == c { - // found a match, adjust markers - index = rdr.tell()-1; - mat = 1; - break; + unsafe { + do io::with_str_reader(s) |rdr| { + let mut ch : char; + while !rdr.eof() { + ch = rdr.read_byte() as char; + if ch == c { + // found a match, adjust markers + index = rdr.tell()-1; + mat = 1; + break; + } } } } @@ -288,7 +294,7 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) { } } -fn userinfo_from_str(uinfo: &str) -> UserInfo { +pure fn userinfo_from_str(uinfo: &str) -> UserInfo { let (user, p) = split_char_first(uinfo, ':'); let pass = if str::len(p) == 0 { option::None @@ -315,12 +321,12 @@ impl UserInfo : Eq { pure fn ne(other: &UserInfo) -> bool { !self.eq(other) } } -fn query_from_str(rawquery: &str) -> Query { +pure fn query_from_str(rawquery: &str) -> Query { let mut query: Query = ~[]; if str::len(rawquery) != 0 { for str::split_char(rawquery, '&').each |p| { let (k, v) = split_char_first(*p, '='); - query.push((decode_component(k), decode_component(v))); + unsafe {query.push((decode_component(k), decode_component(v)));} }; } return query; @@ -340,7 +346,7 @@ pub pure fn query_to_str(query: Query) -> ~str { } // returns the scheme and the rest of the url, or a parsing error -pub fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { +pub pure fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { for str::each_chari(rawurl) |i,c| { match c { 'A' .. 'Z' | 'a' .. 'z' => loop, @@ -387,7 +393,7 @@ impl Input : Eq { } // returns userinfo, host, port, and unparsed part, or an error -fn get_authority(rawurl: &str) -> +pure fn get_authority(rawurl: &str) -> result::Result<(Option, ~str, Option<~str>, ~str), @~str> { if !str::starts_with(rawurl, ~"//") { // there is no authority. @@ -517,7 +523,7 @@ fn get_authority(rawurl: &str) -> let end = end; // make end immutable so it can be captured - let host_is_end_plus_one: &fn() -> bool = || { + let host_is_end_plus_one: &pure fn() -> bool = || { end+1 == len && !['?', '#', '/'].contains(&(rawurl[end] as char)) }; @@ -556,7 +562,7 @@ fn get_authority(rawurl: &str) -> // returns the path and unparsed part of url, or an error -fn get_path(rawurl: &str, authority : bool) -> +pure fn get_path(rawurl: &str, authority : bool) -> result::Result<(~str, ~str), @~str> { let len = str::len(rawurl); let mut end = len; @@ -587,7 +593,7 @@ fn get_path(rawurl: &str, authority : bool) -> } // returns the parsed query and the fragment, if present -fn get_query_fragment(rawurl: &str) -> +pure fn get_query_fragment(rawurl: &str) -> result::Result<(Query, Option<~str>), @~str> { if !str::starts_with(rawurl, ~"?") { if str::starts_with(rawurl, ~"#") { @@ -619,20 +625,20 @@ fn get_query_fragment(rawurl: &str) -> * */ -pub fn from_str(rawurl: &str) -> result::Result { +pub pure fn from_str(rawurl: &str) -> result::Result { // scheme let mut schm = get_scheme(rawurl); if result::is_err(&schm) { return result::Err(copy *result::get_err(&schm)); } - let (scheme, rest) = result::unwrap(schm); + let (scheme, rest) = schm.get(); // authority let mut auth = get_authority(rest); if result::is_err(&auth) { return result::Err(copy *result::get_err(&auth)); } - let (userinfo, host, port, rest) = result::unwrap(auth); + let (userinfo, host, port, rest) = auth.get(); // path let has_authority = if host == ~"" { false } else { true }; @@ -640,21 +646,21 @@ pub fn from_str(rawurl: &str) -> result::Result { if result::is_err(&pth) { return result::Err(copy *result::get_err(&pth)); } - let (path, rest) = result::unwrap(pth); + let (path, rest) = pth.get(); // query and fragment let mut qry = get_query_fragment(rest); if result::is_err(&qry) { return result::Err(copy *result::get_err(&qry)); } - let (query, fragment) = result::unwrap(qry); + let (query, fragment) = qry.get(); return result::Ok(Url(scheme, userinfo, host, port, path, query, fragment)); } impl Url : FromStr { - static fn from_str(s: &str) -> Option { + static pure fn from_str(s: &str) -> Option { match from_str(s) { Ok(move url) => Some(url), Err(_) => None From dc5ff64abb142413156b569cc32c83e605232c43 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 10:13:11 -0800 Subject: [PATCH 36/56] Made more stuff pure. escape functions in char, io.with_str_reader, base64 and md5sum, cell.empty_cell and is_empty. --- src/libcore/char.rs | 12 +-- src/libcore/io.rs | 2 +- src/libstd/base64.rs | 174 ++++++++++++++++++++++--------------------- src/libstd/cell.rs | 4 +- src/libstd/md4.rs | 14 ++-- 5 files changed, 105 insertions(+), 101 deletions(-) diff --git a/src/libcore/char.rs b/src/libcore/char.rs index b76571864e070..802aaf12bdd48 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -126,16 +126,18 @@ pub pure fn to_digit(c: char, radix: uint) -> Option { * - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN` * - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN` */ -pub fn escape_unicode(c: char) -> ~str { +pub pure fn escape_unicode(c: char) -> ~str { let s = u32::to_str(c as u32, 16u); let (c, pad) = (if c <= '\xff' { ('x', 2u) } else if c <= '\uffff' { ('u', 4u) } else { ('U', 8u) }); assert str::len(s) <= pad; let mut out = ~"\\"; - str::push_str(&mut out, str::from_char(c)); - for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } - str::push_str(&mut out, s); + unsafe { + str::push_str(&mut out, str::from_char(c)); + for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } + str::push_str(&mut out, s); + } move out } @@ -151,7 +153,7 @@ pub fn escape_unicode(c: char) -> ~str { * - Any other chars in the range [0x20,0x7e] are not escaped. * - Any other chars are given hex unicode escapes; see `escape_unicode`. */ -pub fn escape_default(c: char) -> ~str { +pub pure fn escape_default(c: char) -> ~str { match c { '\t' => ~"\\t", '\r' => ~"\\r", diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 4629878b4b7ea..535ab883581bb 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -512,7 +512,7 @@ pub pure fn with_bytes_reader(bytes: &[u8], f: fn(Reader) -> t) -> t { f(BytesReader { bytes: bytes, pos: 0u } as Reader) } -pub fn with_str_reader(s: &str, f: fn(Reader) -> T) -> T { +pub pure fn with_str_reader(s: &str, f: fn(Reader) -> T) -> T { str::byte_slice(s, |bytes| with_bytes_reader(bytes, f)) } diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 9bad4d3975003..177074de8f65a 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -2,72 +2,73 @@ use io::Reader; pub trait ToBase64 { - fn to_base64() -> ~str; + pure fn to_base64() -> ~str; } impl &[u8]: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { let chars = str::chars( ~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ); - let len = self.len(); let mut s = ~""; - str::reserve(&mut s, ((len + 3u) / 4u) * 3u); - - let mut i = 0u; - - while i < len - (len % 3u) { - let n = (self[i] as uint) << 16u | - (self[i + 1u] as uint) << 8u | - (self[i + 2u] as uint); - - // This 24-bit number gets separated into four 6-bit numbers. - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, chars[n & 63u]); - - i += 3u; - } - - // Heh, would be cool if we knew this was exhaustive - // (the dream of bounded integer types) - match len % 3 { - 0 => (), - 1 => { - let n = (self[i] as uint) << 16u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, '='); - str::push_char(&mut s, '='); - } - 2 => { - let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, '='); - } - _ => fail ~"Algebra is broken, please alert the math police" + unsafe { + let len = self.len(); + str::reserve(&mut s, ((len + 3u) / 4u) * 3u); + + let mut i = 0u; + + while i < len - (len % 3u) { + let n = (self[i] as uint) << 16u | + (self[i + 1u] as uint) << 8u | + (self[i + 2u] as uint); + + // This 24-bit number gets separated into four 6-bit numbers. + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, chars[n & 63u]); + + i += 3u; + } + + // Heh, would be cool if we knew this was exhaustive + // (the dream of bounded integer types) + match len % 3 { + 0 => (), + 1 => { + let n = (self[i] as uint) << 16u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, '='); + str::push_char(&mut s, '='); + } + 2 => { + let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, '='); + } + _ => fail ~"Algebra is broken, please alert the math police" + } } - s } } impl &str: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { str::to_bytes(self).to_base64() } } pub trait FromBase64 { - fn from_base64() -> ~[u8]; + pure fn from_base64() -> ~[u8]; } impl ~[u8]: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { if self.len() % 4u != 0u { fail ~"invalid base64 length"; } let len = self.len(); @@ -80,55 +81,56 @@ impl ~[u8]: FromBase64 { let mut r = vec::with_capacity((len / 4u) * 3u - padding); - let mut i = 0u; - while i < len { - let mut n = 0u; - - for iter::repeat(4u) { - let ch = self[i] as char; - n <<= 6u; - - if ch >= 'A' && ch <= 'Z' { - n |= (ch as uint) - 0x41u; - } else if ch >= 'a' && ch <= 'z' { - n |= (ch as uint) - 0x47u; - } else if ch >= '0' && ch <= '9' { - n |= (ch as uint) + 0x04u; - } else if ch == '+' { - n |= 0x3Eu; - } else if ch == '/' { - n |= 0x3Fu; - } else if ch == '=' { - match len - i { - 1u => { - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - return copy r; - } - 2u => { - r.push(((n >> 10u) & 0xFFu) as u8); - return copy r; - } - _ => fail ~"invalid base64 padding" + unsafe { + let mut i = 0u; + while i < len { + let mut n = 0u; + + for iter::repeat(4u) { + let ch = self[i] as char; + n <<= 6u; + + if ch >= 'A' && ch <= 'Z' { + n |= (ch as uint) - 0x41u; + } else if ch >= 'a' && ch <= 'z' { + n |= (ch as uint) - 0x47u; + } else if ch >= '0' && ch <= '9' { + n |= (ch as uint) + 0x04u; + } else if ch == '+' { + n |= 0x3Eu; + } else if ch == '/' { + n |= 0x3Fu; + } else if ch == '=' { + match len - i { + 1u => { + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + return copy r; + } + 2u => { + r.push(((n >> 10u) & 0xFFu) as u8); + return copy r; + } + _ => fail ~"invalid base64 padding" + } + } else { + fail ~"invalid base64 character"; } - } else { - fail ~"invalid base64 character"; - } - - i += 1u; - }; - - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - r.push(((n ) & 0xFFu) as u8); + + i += 1u; + }; + + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + r.push(((n ) & 0xFFu) as u8); + } } - r } } impl ~str: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { str::to_bytes(self).from_base64() } } diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 4f79bf2b31698..78027aa890760 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -12,7 +12,7 @@ pub fn Cell(value: T) -> Cell { Cell { value: Some(move value) } } -pub fn empty_cell() -> Cell { +pub pure fn empty_cell() -> Cell { Cell { value: None } } @@ -37,7 +37,7 @@ impl Cell { } /// Returns true if the cell is empty and false if the cell is full. - fn is_empty() -> bool { + pure fn is_empty() -> bool { self.value.is_none() } diff --git a/src/libstd/md4.rs b/src/libstd/md4.rs index 581beb78bdc55..d9bc03c311de7 100644 --- a/src/libstd/md4.rs +++ b/src/libstd/md4.rs @@ -1,6 +1,6 @@ #[forbid(deprecated_mode)]; -pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { +pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { // subtle: if orig_len is merely uint, then the code below // which performs shifts by 32 bits or more has undefined // results. @@ -10,14 +10,14 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]); let mut bitlen = orig_len + 8u64; while (bitlen + 64u64) % 512u64 > 0u64 { - msg.push(0u8); + unsafe {msg.push(0u8);} bitlen += 8u64; } // append length let mut i = 0u64; while i < 8u64 { - msg.push((orig_len >> (i * 8u64)) as u8); + unsafe {msg.push((orig_len >> (i * 8u64)) as u8);} i += 1u64; } @@ -26,7 +26,7 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut c = 0x98badcfeu32; let mut d = 0x10325476u32; - fn rot(r: int, x: u32) -> u32 { + pure fn rot(r: int, x: u32) -> u32 { let r = r as u32; (x << r) | (x >> (32u32 - r)) } @@ -84,9 +84,9 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { return {a: a, b: b, c: c, d: d}; } -pub fn md4_str(msg: &[u8]) -> ~str { +pub pure fn md4_str(msg: &[u8]) -> ~str { let {a, b, c, d} = md4(msg); - fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { + pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { f(a); f(b); f(c); f(d); } let mut result = ~""; @@ -102,7 +102,7 @@ pub fn md4_str(msg: &[u8]) -> ~str { result } -pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } +pub pure fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } #[test] fn test_md4() { From 27cca5b61b5daaec6f07b9e24c368a595df58060 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 10:21:14 -0800 Subject: [PATCH 37/56] Made merge_sort pure --- src/libstd/sort.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index 4f06cc40c229c..451d5e805d051 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -13,10 +13,10 @@ type Le = pure fn(v1: &T, v2: &T) -> bool; * Has worst case O(n log n) performance, best case O(n), but * is not space efficient. This is a stable sort. */ -pub fn merge_sort(v: &[const T], le: Le) -> ~[T] { +pub pure fn merge_sort(v: &[const T], le: Le) -> ~[T] { type Slice = (uint, uint); - return merge_sort_(v, (0u, len(v)), le); + unsafe {return merge_sort_(v, (0u, len(v)), le);} fn merge_sort_(v: &[const T], slice: Slice, le: Le) -> ~[T] { From 0c11a12bdbfb804c2aef0dc164fd8ecc1f5d3eeb Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 11:00:38 -0800 Subject: [PATCH 38/56] Fixing warnings for long and blank lines --- src/libcore/char.rs | 3 ++- src/libstd/base64.rs | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 802aaf12bdd48..fb8cc274217d2 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -135,7 +135,8 @@ pub pure fn escape_unicode(c: char) -> ~str { let mut out = ~"\\"; unsafe { str::push_str(&mut out, str::from_char(c)); - for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } + for uint::range(str::len(s), pad) |_i| + { str::push_str(&mut out, ~"0"); } str::push_str(&mut out, s); } move out diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 177074de8f65a..5db71b12185bc 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -15,23 +15,23 @@ impl &[u8]: ToBase64 { unsafe { let len = self.len(); str::reserve(&mut s, ((len + 3u) / 4u) * 3u); - + let mut i = 0u; - + while i < len - (len % 3u) { let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u | (self[i + 2u] as uint); - + // This 24-bit number gets separated into four 6-bit numbers. str::push_char(&mut s, chars[(n >> 18u) & 63u]); str::push_char(&mut s, chars[(n >> 12u) & 63u]); str::push_char(&mut s, chars[(n >> 6u) & 63u]); str::push_char(&mut s, chars[n & 63u]); - + i += 3u; } - + // Heh, would be cool if we knew this was exhaustive // (the dream of bounded integer types) match len % 3 { @@ -44,7 +44,8 @@ impl &[u8]: ToBase64 { str::push_char(&mut s, '='); } 2 => { - let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; + let n = (self[i] as uint) << 16u | + (self[i + 1u] as uint) << 8u; str::push_char(&mut s, chars[(n >> 18u) & 63u]); str::push_char(&mut s, chars[(n >> 12u) & 63u]); str::push_char(&mut s, chars[(n >> 6u) & 63u]); @@ -85,11 +86,11 @@ impl ~[u8]: FromBase64 { let mut i = 0u; while i < len { let mut n = 0u; - + for iter::repeat(4u) { let ch = self[i] as char; n <<= 6u; - + if ch >= 'A' && ch <= 'Z' { n |= (ch as uint) - 0x41u; } else if ch >= 'a' && ch <= 'z' { @@ -116,10 +117,10 @@ impl ~[u8]: FromBase64 { } else { fail ~"invalid base64 character"; } - + i += 1u; }; - + r.push(((n >> 16u) & 0xFFu) as u8); r.push(((n >> 8u ) & 0xFFu) as u8); r.push(((n ) & 0xFFu) as u8); From 401093ec7d66f898338abb30b8097e06bfeb1a58 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 11:01:08 -0800 Subject: [PATCH 39/56] Made from_str pure --- src/libcore/from_str.rs | 2 +- src/libcore/int-template.rs | 9 ++++++--- src/libcore/uint-template.rs | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/libcore/from_str.rs b/src/libcore/from_str.rs index c4dd2536e2cfc..8e9cd5a022763 100644 --- a/src/libcore/from_str.rs +++ b/src/libcore/from_str.rs @@ -7,6 +7,6 @@ use option::Option; pub trait FromStr { - static fn from_str(s: &str) -> Option; + static pure fn from_str(s: &str) -> Option; } diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index db95bc46ffe8a..e5da7d6abe106 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -106,7 +106,7 @@ impl T: iter::Times { * * buf - A byte buffer * * radix - The base of the number */ -pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { +pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option { if vec::len(buf) == 0u { return None; } let mut i = vec::len(buf) - 1u; let mut start = 0u; @@ -129,10 +129,13 @@ pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { } /// Parse a string to an int -pub fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } +pub pure fn from_str(s: &str) -> Option +{ + parse_bytes(str::to_bytes(s), 10u) +} impl T : FromStr { - static fn from_str(s: &str) -> Option { from_str(s) } + static pure fn from_str(s: &str) -> Option { from_str(s) } } /// Convert to a string in a given base diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index ceb525f5f8ddd..d406e36cc0771 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -100,7 +100,7 @@ impl T: iter::Times { * * `buf` must not be empty */ -pub fn parse_bytes(buf: &[const u8], radix: uint) -> Option { +pub pure fn parse_bytes(buf: &[const u8], radix: uint) -> Option { if vec::len(buf) == 0u { return None; } let mut i = vec::len(buf) - 1u; let mut power = 1u as T; @@ -117,10 +117,13 @@ pub fn parse_bytes(buf: &[const u8], radix: uint) -> Option { } /// Parse a string to an int -pub fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } +pub pure fn from_str(s: &str) -> Option +{ + parse_bytes(str::to_bytes(s), 10u) +} impl T : FromStr { - static fn from_str(s: &str) -> Option { from_str(s) } + static pure fn from_str(s: &str) -> Option { from_str(s) } } /// Parse a string as an unsigned integer. From 340955b3a55c0707e72d64b2ce4cdcf21529fafb Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 19:43:39 -0800 Subject: [PATCH 40/56] Added support for options that take no arguments and may be repeated. Closes #3568. --- src/libstd/getopts.rs | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index 8d77b88aba230..b7d6dc697762f 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -145,6 +145,11 @@ pub fn optflag(name: &str) -> Opt { return {name: mkname(name), hasarg: No, occur: Optional}; } +/// Create an option that is optional and does not take an argument +pub fn optflagmulti(name: &str) -> Opt { + return {name: mkname(name), hasarg: No, occur: Multi}; +} + /// Create an option that is optional and takes an optional argument pub fn optflagopt(name: &str) -> Opt { return {name: mkname(name), hasarg: Maybe, occur: Optional}; @@ -417,6 +422,11 @@ pub fn opt_present(mm: Matches, nm: &str) -> bool { return vec::len::(opt_vals(mm, nm)) > 0u; } +/// Returns the number of times an option was matched +pub fn opt_count(mm: Matches, nm: &str) -> uint { + return vec::len::(opt_vals(mm, nm)); +} + /// Returns true if any of several options were matched pub fn opts_present(mm: Matches, names: &[~str]) -> bool { for vec::each(names) |nm| { @@ -1003,6 +1013,71 @@ mod tests { } } + // Tests for optflagmulti + #[test] + fn test_optflagmulti_short1() { + let args = ~[~"-v"]; + let opts = ~[optflagmulti(~"v")]; + let rs = getopts(args, opts); + match rs { + Ok(copy m) => { + assert (opt_count(m, ~"v") == 1); + } + _ => fail + } + } + + #[test] + fn test_optflagmulti_short2a() { + let args = ~[~"-v", ~"-v"]; + let opts = ~[optflagmulti(~"v")]; + let rs = getopts(args, opts); + match rs { + Ok(copy m) => { + assert (opt_count(m, ~"v") == 2); + } + _ => fail + } + } + + #[test] + fn test_optflagmulti_short2b() { + let args = ~[~"-vv"]; + let opts = ~[optflagmulti(~"v")]; + let rs = getopts(args, opts); + match rs { + Ok(copy m) => { + assert (opt_count(m, ~"v") == 2); + } + _ => fail + } + } + + #[test] + fn test_optflagmulti_long1() { + let args = ~[~"--verbose"]; + let opts = ~[optflagmulti(~"verbose")]; + let rs = getopts(args, opts); + match rs { + Ok(copy m) => { + assert (opt_count(m, ~"verbose") == 1); + } + _ => fail + } + } + + #[test] + fn test_optflagmulti_long2() { + let args = ~[~"--verbose", ~"--verbose"]; + let opts = ~[optflagmulti(~"verbose")]; + let rs = getopts(args, opts); + match rs { + Ok(copy m) => { + assert (opt_count(m, ~"verbose") == 2); + } + _ => fail + } + } // Tests for optmulti #[test] From 68c852ad3aa4b27b5295a8e7da288798afeea2c4 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 08:41:47 -0800 Subject: [PATCH 41/56] Made Map.contains_key, contains_key_ref, and get pure. --- src/libcore/mutable.rs | 2 +- src/libstd/map.rs | 18 +++++++++--------- src/libstd/smallintmap.rs | 8 ++++---- .../class-impl-very-parameterized-trait.rs | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs index 56a6df2c4ddf1..eafdf58e67e9a 100644 --- a/src/libcore/mutable.rs +++ b/src/libcore/mutable.rs @@ -48,7 +48,7 @@ impl Data { } } - fn borrow_const(op: &fn(t: &const T) -> R) -> R { + pure fn borrow_const(op: &fn(t: &const T) -> R) -> R { op(&const self.value) } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 0ee7cb6fcf967..915202143a1ea 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -30,17 +30,17 @@ pub trait Map { fn insert(v: K, v: V) -> bool; /// Returns true if the map contains a value for the specified key - fn contains_key(key: K) -> bool; + pure fn contains_key(key: K) -> bool; /// Returns true if the map contains a value for the specified /// key, taking the key by reference. - fn contains_key_ref(key: &K) -> bool; + pure fn contains_key_ref(key: &K) -> bool; /** * Get the value for the specified key. Fails if the key does not exist in * the map. */ - fn get(key: K) -> V; + pure fn get(key: K) -> V; /** * Get the value for the specified key. If the key does not exist in @@ -200,11 +200,11 @@ pub mod chained { impl T: Map { pure fn size() -> uint { self.count } - fn contains_key(k: K) -> bool { + pure fn contains_key(k: K) -> bool { self.contains_key_ref(&k) } - fn contains_key_ref(k: &K) -> bool { + pure fn contains_key_ref(k: &K) -> bool { let hash = k.hash_keyed(0,0) as uint; match self.search_tbl(k, hash) { NotFound => false, @@ -264,7 +264,7 @@ pub mod chained { } } - fn get(k: K) -> V { + pure fn get(k: K) -> V { let opt_v = self.find(k); if opt_v.is_none() { fail fmt!("Key not found in table: %?", k); @@ -421,19 +421,19 @@ impl @Mut>: } } - fn contains_key(key: K) -> bool { + pure fn contains_key(key: K) -> bool { do self.borrow_const |p| { p.contains_key(&key) } } - fn contains_key_ref(key: &K) -> bool { + pure fn contains_key_ref(key: &K) -> bool { do self.borrow_const |p| { p.contains_key(key) } } - fn get(key: K) -> V { + pure fn get(key: K) -> V { do self.borrow_const |p| { p.get(&key) } diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 1582d90ce2d62..9dc216a21557a 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -60,7 +60,7 @@ pub pure fn get(self: SmallIntMap, key: uint) -> T { } /// Returns true if the map contains a value for the specified key -pub fn contains_key(self: SmallIntMap, key: uint) -> bool { +pub pure fn contains_key(self: SmallIntMap, key: uint) -> bool { return !find(self, key).is_none(); } @@ -93,13 +93,13 @@ impl SmallIntMap: map::Map { fn clear() { self.v.set(~[]); } - fn contains_key(key: uint) -> bool { + pure fn contains_key(key: uint) -> bool { contains_key(self, key) } - fn contains_key_ref(key: &uint) -> bool { + pure fn contains_key_ref(key: &uint) -> bool { contains_key(self, *key) } - fn get(key: uint) -> V { get(self, key) } + pure fn get(key: uint) -> V { get(self, key) } pure fn find(key: uint) -> Option { find(self, key) } fn rehash() { fail } diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index 8c25d94db3bed..4cfcbf67cc9aa 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -47,10 +47,10 @@ impl cat : Map { self.meows += k; true } - fn contains_key(+k: int) -> bool { k <= self.meows } - fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } + pure fn contains_key(+k: int) -> bool { k <= self.meows } + pure fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } - fn get(+k:int) -> T { match self.find(k) { + pure fn get(+k:int) -> T { match self.find(k) { Some(v) => { v } None => { fail ~"epic fail"; } } From a9e13586a363fa539828268129b75accc085a31d Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:11:57 -0800 Subject: [PATCH 42/56] Moved strptime and strftime into private helper functions. Makes the public API much easier to see and prepares the way for making them pure. --- src/libstd/time.rs | 160 ++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 76 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 75909273392f4..23ec6fef00300 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -152,6 +152,89 @@ pub fn now() -> Tm { /// Parses the time from the string according to the format string. pub fn strptime(s: &str, format: &str) -> Result { + do_strptime(s, format) +} + +pub fn strftime(format: &str, tm: Tm) -> ~str { + do_strftime(format, tm) +} + +impl Tm { + /// Convert time to the seconds from January 1, 1970 + fn to_timespec() -> Timespec { + let mut sec = 0i64; + if self.tm_gmtoff == 0_i32 { + rustrt::rust_timegm(self, &mut sec); + } else { + rustrt::rust_mktime(self, &mut sec); + } + { sec: sec, nsec: self.tm_nsec } + } + + /// Convert time to the local timezone + fn to_local() -> Tm { + at(self.to_timespec()) + } + + /// Convert time to the UTC + fn to_utc() -> Tm { + at_utc(self.to_timespec()) + } + + /** + * Return a string of the current time in the form + * "Thu Jan 1 00:00:00 1970". + */ + fn ctime() -> ~str { self.strftime(~"%c") } + + /// Formats the time according to the format string. + fn strftime(format: &str) -> ~str { strftime(format, self) } + + /** + * Returns a time string formatted according to RFC 822. + * + * local: "Thu, 22 Mar 2012 07:53:18 PST" + * utc: "Thu, 22 Mar 2012 14:53:18 UTC" + */ + fn rfc822() -> ~str { + if self.tm_gmtoff == 0_i32 { + self.strftime(~"%a, %d %b %Y %T GMT") + } else { + self.strftime(~"%a, %d %b %Y %T %Z") + } + } + + /** + * Returns a time string formatted according to RFC 822 with Zulu time. + * + * local: "Thu, 22 Mar 2012 07:53:18 -0700" + * utc: "Thu, 22 Mar 2012 14:53:18 -0000" + */ + fn rfc822z() -> ~str { + self.strftime(~"%a, %d %b %Y %T %z") + } + + /** + * Returns a time string formatted according to ISO 8601. + * + * local: "2012-02-22T07:53:18-07:00" + * utc: "2012-02-22T14:53:18Z" + */ + fn rfc3339() -> ~str { + if self.tm_gmtoff == 0_i32 { + self.strftime(~"%Y-%m-%dT%H:%M:%SZ") + } else { + let s = self.strftime(~"%Y-%m-%dT%H:%M:%S"); + let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' }; + let mut m = i32::abs(self.tm_gmtoff) / 60_i32; + let h = m / 60_i32; + m -= h * 60_i32; + s + fmt!("%c%02d:%02d", sign, h as int, m as int) + } + } +} + +priv fn do_strptime(s: &str, format: &str) -> Result { type TmMut = { mut tm_sec: i32, mut tm_min: i32, @@ -592,7 +675,7 @@ pub fn strptime(s: &str, format: &str) -> Result { } } -fn strftime(format: &str, tm: Tm) -> ~str { +priv fn do_strftime(format: &str, tm: Tm) -> ~str { fn parse_type(ch: char, tm: &Tm) -> ~str { //FIXME (#2350): Implement missing types. let die = || fmt!("strftime: can't understand this format %c ", ch); @@ -759,81 +842,6 @@ fn strftime(format: &str, tm: Tm) -> ~str { buf } -impl Tm { - /// Convert time to the seconds from January 1, 1970 - fn to_timespec() -> Timespec { - let mut sec = 0i64; - if self.tm_gmtoff == 0_i32 { - rustrt::rust_timegm(self, &mut sec); - } else { - rustrt::rust_mktime(self, &mut sec); - } - { sec: sec, nsec: self.tm_nsec } - } - - /// Convert time to the local timezone - fn to_local() -> Tm { - at(self.to_timespec()) - } - - /// Convert time to the UTC - fn to_utc() -> Tm { - at_utc(self.to_timespec()) - } - - /** - * Return a string of the current time in the form - * "Thu Jan 1 00:00:00 1970". - */ - fn ctime() -> ~str { self.strftime(~"%c") } - - /// Formats the time according to the format string. - fn strftime(format: &str) -> ~str { strftime(format, self) } - - /** - * Returns a time string formatted according to RFC 822. - * - * local: "Thu, 22 Mar 2012 07:53:18 PST" - * utc: "Thu, 22 Mar 2012 14:53:18 UTC" - */ - fn rfc822() -> ~str { - if self.tm_gmtoff == 0_i32 { - self.strftime(~"%a, %d %b %Y %T GMT") - } else { - self.strftime(~"%a, %d %b %Y %T %Z") - } - } - - /** - * Returns a time string formatted according to RFC 822 with Zulu time. - * - * local: "Thu, 22 Mar 2012 07:53:18 -0700" - * utc: "Thu, 22 Mar 2012 14:53:18 -0000" - */ - fn rfc822z() -> ~str { - self.strftime(~"%a, %d %b %Y %T %z") - } - - /** - * Returns a time string formatted according to ISO 8601. - * - * local: "2012-02-22T07:53:18-07:00" - * utc: "2012-02-22T14:53:18Z" - */ - fn rfc3339() -> ~str { - if self.tm_gmtoff == 0_i32 { - self.strftime(~"%Y-%m-%dT%H:%M:%SZ") - } else { - let s = self.strftime(~"%Y-%m-%dT%H:%M:%S"); - let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' }; - let mut m = i32::abs(self.tm_gmtoff) / 60_i32; - let h = m / 60_i32; - m -= h * 60_i32; - s + fmt!("%c%02d:%02d", sign, h as int, m as int) - } - } -} - #[cfg(test)] mod tests { #[legacy_exports]; From c58951ea7d70bfab8c9a0a3ad6540d829c59b585 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:17:43 -0800 Subject: [PATCH 43/56] Replace TmMut with inherited mutability --- src/libstd/time.rs | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 23ec6fef00300..d5a5deeae8c08 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -155,6 +155,7 @@ pub fn strptime(s: &str, format: &str) -> Result { do_strptime(s, format) } +/// Formats the time according to the format string. pub fn strftime(format: &str, tm: Tm) -> ~str { do_strftime(format, tm) } @@ -235,21 +236,6 @@ impl Tm { } priv fn do_strptime(s: &str, format: &str) -> Result { - type TmMut = { - mut tm_sec: i32, - mut tm_min: i32, - mut tm_hour: i32, - mut tm_mday: i32, - mut tm_mon: i32, - mut tm_year: i32, - mut tm_wday: i32, - mut tm_yday: i32, - mut tm_isdst: i32, - mut tm_gmtoff: i32, - mut tm_zone: ~str, - mut tm_nsec: i32, - }; - fn match_str(s: &str, pos: uint, needle: &str) -> bool { let mut i = pos; for str::each(needle) |ch| { @@ -312,7 +298,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { } } - fn parse_type(s: &str, pos: uint, ch: char, tm: &TmMut) + fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm_) -> Result { match ch { 'A' => match match_strs(s, pos, ~[ @@ -623,19 +609,19 @@ priv fn do_strptime(s: &str, format: &str) -> Result { } do io::with_str_reader(str::from_slice(format)) |rdr| { - let tm = { - mut tm_sec: 0_i32, - mut tm_min: 0_i32, - mut tm_hour: 0_i32, - mut tm_mday: 0_i32, - mut tm_mon: 0_i32, - mut tm_year: 0_i32, - mut tm_wday: 0_i32, - mut tm_yday: 0_i32, - mut tm_isdst: 0_i32, - mut tm_gmtoff: 0_i32, - mut tm_zone: ~"", - mut tm_nsec: 0_i32, + let mut tm = { + tm_sec: 0_i32, + tm_min: 0_i32, + tm_hour: 0_i32, + tm_mday: 0_i32, + tm_mon: 0_i32, + tm_year: 0_i32, + tm_wday: 0_i32, + tm_yday: 0_i32, + tm_isdst: 0_i32, + tm_gmtoff: 0_i32, + tm_zone: ~"", + tm_nsec: 0_i32, }; let mut pos = 0u; let len = str::len(s); @@ -645,7 +631,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result { let {ch, next} = str::char_range_at(s, pos); match rdr.read_char() { - '%' => match parse_type(s, pos, rdr.read_char(), &tm) { + '%' => match parse_type(s, pos, rdr.read_char(), &mut tm) { Ok(next) => pos = next, Err(copy e) => { result = Err(e); break; } }, From 2c0dab02ad64e581ede4344401dca0db67430897 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:27:03 -0800 Subject: [PATCH 44/56] Made the time to string functions pure as well as empty_tm. This closes #3919. --- src/libstd/time.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index d5a5deeae8c08..912df9c7558dd 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -107,7 +107,7 @@ impl Tm : Eq { pure fn ne(other: &Tm) -> bool { *self != *(*other) } } -pub fn empty_tm() -> Tm { +pub pure fn empty_tm() -> Tm { Tm_({ tm_sec: 0_i32, tm_min: 0_i32, @@ -151,13 +151,17 @@ pub fn now() -> Tm { } /// Parses the time from the string according to the format string. -pub fn strptime(s: &str, format: &str) -> Result { - do_strptime(s, format) +pub pure fn strptime(s: &str, format: &str) -> Result { + // unsafe only because do_strptime is annoying to make pure + // (it does IO with a str_reader) + unsafe {do_strptime(s, format)} } /// Formats the time according to the format string. -pub fn strftime(format: &str, tm: Tm) -> ~str { - do_strftime(format, tm) +pub pure fn strftime(format: &str, tm: Tm) -> ~str { + // unsafe only because do_strftime is annoying to make pure + // (it does IO with a str_reader) + unsafe {do_strftime(format, tm)} } impl Tm { @@ -186,10 +190,10 @@ impl Tm { * Return a string of the current time in the form * "Thu Jan 1 00:00:00 1970". */ - fn ctime() -> ~str { self.strftime(~"%c") } + pure fn ctime() -> ~str { self.strftime(~"%c") } /// Formats the time according to the format string. - fn strftime(format: &str) -> ~str { strftime(format, self) } + pure fn strftime(format: &str) -> ~str { strftime(format, self) } /** * Returns a time string formatted according to RFC 822. @@ -197,7 +201,7 @@ impl Tm { * local: "Thu, 22 Mar 2012 07:53:18 PST" * utc: "Thu, 22 Mar 2012 14:53:18 UTC" */ - fn rfc822() -> ~str { + pure fn rfc822() -> ~str { if self.tm_gmtoff == 0_i32 { self.strftime(~"%a, %d %b %Y %T GMT") } else { @@ -211,7 +215,7 @@ impl Tm { * local: "Thu, 22 Mar 2012 07:53:18 -0700" * utc: "Thu, 22 Mar 2012 14:53:18 -0000" */ - fn rfc822z() -> ~str { + pure fn rfc822z() -> ~str { self.strftime(~"%a, %d %b %Y %T %z") } @@ -221,7 +225,7 @@ impl Tm { * local: "2012-02-22T07:53:18-07:00" * utc: "2012-02-22T14:53:18Z" */ - fn rfc3339() -> ~str { + pure fn rfc3339() -> ~str { if self.tm_gmtoff == 0_i32 { self.strftime(~"%Y-%m-%dT%H:%M:%SZ") } else { From c5ab47e7ba8881fb76e2266e105d373b59ed09db Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:56:05 -0800 Subject: [PATCH 45/56] Made Result.get, get_ref, is_ok, is_err, and iter methods pure. Note that the function versions were already pure. --- src/libcore/result.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 2df2974d0ba31..da7db607c8559 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -204,13 +204,13 @@ pub fn map_err(res: &Result, op: fn((&E)) -> F) } impl Result { - fn get_ref(&self) -> &self/T { get_ref(self) } + pure fn get_ref(&self) -> &self/T { get_ref(self) } - fn is_ok() -> bool { is_ok(&self) } + pure fn is_ok() -> bool { is_ok(&self) } - fn is_err() -> bool { is_err(&self) } + pure fn is_err() -> bool { is_err(&self) } - fn iter(f: fn((&T))) { + pure fn iter(f: fn((&T))) { match self { Ok(ref t) => f(t), Err(_) => () @@ -226,7 +226,7 @@ impl Result { } impl Result { - fn get() -> T { get(&self) } + pure fn get() -> T { get(&self) } fn map_err(op: fn((&E)) -> F) -> Result { match self { @@ -237,7 +237,7 @@ impl Result { } impl Result { - fn get_err() -> E { get_err(&self) } + pure fn get_err() -> E { get_err(&self) } fn map(op: fn((&T)) -> U) -> Result { match self { From 15989ecb139a4ee4a53e6657fd4fc8cee9d729de Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 09:57:14 -0800 Subject: [PATCH 46/56] Made most of the URL functions pure. This closes #3782. --- src/libstd/net_url.rs | 74 +++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 8ea9513d15518..dd76f65a046bc 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -27,7 +27,7 @@ type UserInfo = { pub type Query = ~[(~str, ~str)]; -pub fn Url(scheme: ~str, user: Option, host: ~str, +pub pure fn Url(scheme: ~str, user: Option, host: ~str, port: Option<~str>, path: ~str, query: Query, fragment: Option<~str>) -> Url { Url { scheme: move scheme, user: move user, host: move host, @@ -35,7 +35,7 @@ pub fn Url(scheme: ~str, user: Option, host: ~str, fragment: move fragment } } -fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo { +pure fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo { {user: move user, pass: move pass} } @@ -84,8 +84,9 @@ fn encode_inner(s: &str, full_url: bool) -> ~str { * * This function is compliant with RFC 3986. */ -pub fn encode(s: &str) -> ~str { - encode_inner(s, true) +pub pure fn encode(s: &str) -> ~str { + // unsafe only because encode_inner does (string) IO + unsafe {encode_inner(s, true)} } /** @@ -95,8 +96,9 @@ pub fn encode(s: &str) -> ~str { * This function is compliant with RFC 3986. */ -pub fn encode_component(s: &str) -> ~str { - encode_inner(s, false) +pub pure fn encode_component(s: &str) -> ~str { + // unsafe only because encode_inner does (string) IO + unsafe {encode_inner(s, false)} } fn decode_inner(s: &str, full_url: bool) -> ~str { @@ -142,15 +144,17 @@ fn decode_inner(s: &str, full_url: bool) -> ~str { * * This will only decode escape sequences generated by encode_uri. */ -pub fn decode(s: &str) -> ~str { - decode_inner(s, true) +pub pure fn decode(s: &str) -> ~str { + // unsafe only because decode_inner does (string) IO + unsafe {decode_inner(s, true)} } /** * Decode a string encoded with percent encoding. */ -pub fn decode_component(s: &str) -> ~str { - decode_inner(s, false) +pub pure fn decode_component(s: &str) -> ~str { + // unsafe only because decode_inner does (string) IO + unsafe {decode_inner(s, false)} } fn encode_plus(s: &str) -> ~str { @@ -264,19 +268,21 @@ pub fn decode_form_urlencoded(s: ~[u8]) -> } -fn split_char_first(s: &str, c: char) -> (~str, ~str) { +pure fn split_char_first(s: &str, c: char) -> (~str, ~str) { let len = str::len(s); let mut index = len; let mut mat = 0; - do io::with_str_reader(s) |rdr| { - let mut ch : char; - while !rdr.eof() { - ch = rdr.read_byte() as char; - if ch == c { - // found a match, adjust markers - index = rdr.tell()-1; - mat = 1; - break; + unsafe { + do io::with_str_reader(s) |rdr| { + let mut ch : char; + while !rdr.eof() { + ch = rdr.read_byte() as char; + if ch == c { + // found a match, adjust markers + index = rdr.tell()-1; + mat = 1; + break; + } } } } @@ -288,7 +294,7 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) { } } -fn userinfo_from_str(uinfo: &str) -> UserInfo { +pure fn userinfo_from_str(uinfo: &str) -> UserInfo { let (user, p) = split_char_first(uinfo, ':'); let pass = if str::len(p) == 0 { option::None @@ -315,12 +321,12 @@ impl UserInfo : Eq { pure fn ne(other: &UserInfo) -> bool { !self.eq(other) } } -fn query_from_str(rawquery: &str) -> Query { +pure fn query_from_str(rawquery: &str) -> Query { let mut query: Query = ~[]; if str::len(rawquery) != 0 { for str::split_char(rawquery, '&').each |p| { let (k, v) = split_char_first(*p, '='); - query.push((decode_component(k), decode_component(v))); + unsafe {query.push((decode_component(k), decode_component(v)));} }; } return query; @@ -340,7 +346,7 @@ pub pure fn query_to_str(query: Query) -> ~str { } // returns the scheme and the rest of the url, or a parsing error -pub fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { +pub pure fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { for str::each_chari(rawurl) |i,c| { match c { 'A' .. 'Z' | 'a' .. 'z' => loop, @@ -387,7 +393,7 @@ impl Input : Eq { } // returns userinfo, host, port, and unparsed part, or an error -fn get_authority(rawurl: &str) -> +pure fn get_authority(rawurl: &str) -> result::Result<(Option, ~str, Option<~str>, ~str), @~str> { if !str::starts_with(rawurl, ~"//") { // there is no authority. @@ -517,7 +523,7 @@ fn get_authority(rawurl: &str) -> let end = end; // make end immutable so it can be captured - let host_is_end_plus_one: &fn() -> bool = || { + let host_is_end_plus_one: &pure fn() -> bool = || { end+1 == len && !['?', '#', '/'].contains(&(rawurl[end] as char)) }; @@ -556,7 +562,7 @@ fn get_authority(rawurl: &str) -> // returns the path and unparsed part of url, or an error -fn get_path(rawurl: &str, authority : bool) -> +pure fn get_path(rawurl: &str, authority : bool) -> result::Result<(~str, ~str), @~str> { let len = str::len(rawurl); let mut end = len; @@ -587,7 +593,7 @@ fn get_path(rawurl: &str, authority : bool) -> } // returns the parsed query and the fragment, if present -fn get_query_fragment(rawurl: &str) -> +pure fn get_query_fragment(rawurl: &str) -> result::Result<(Query, Option<~str>), @~str> { if !str::starts_with(rawurl, ~"?") { if str::starts_with(rawurl, ~"#") { @@ -619,20 +625,20 @@ fn get_query_fragment(rawurl: &str) -> * */ -pub fn from_str(rawurl: &str) -> result::Result { +pub pure fn from_str(rawurl: &str) -> result::Result { // scheme let mut schm = get_scheme(rawurl); if result::is_err(&schm) { return result::Err(copy *result::get_err(&schm)); } - let (scheme, rest) = result::unwrap(schm); + let (scheme, rest) = schm.get(); // authority let mut auth = get_authority(rest); if result::is_err(&auth) { return result::Err(copy *result::get_err(&auth)); } - let (userinfo, host, port, rest) = result::unwrap(auth); + let (userinfo, host, port, rest) = auth.get(); // path let has_authority = if host == ~"" { false } else { true }; @@ -640,21 +646,21 @@ pub fn from_str(rawurl: &str) -> result::Result { if result::is_err(&pth) { return result::Err(copy *result::get_err(&pth)); } - let (path, rest) = result::unwrap(pth); + let (path, rest) = pth.get(); // query and fragment let mut qry = get_query_fragment(rest); if result::is_err(&qry) { return result::Err(copy *result::get_err(&qry)); } - let (query, fragment) = result::unwrap(qry); + let (query, fragment) = qry.get(); return result::Ok(Url(scheme, userinfo, host, port, path, query, fragment)); } impl Url : FromStr { - static fn from_str(s: &str) -> Option { + static pure fn from_str(s: &str) -> Option { match from_str(s) { Ok(move url) => Some(url), Err(_) => None From 6d99a2f8a99baedab90fb8bd6cb6e1507fad736b Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 10:13:11 -0800 Subject: [PATCH 47/56] Made more stuff pure. escape functions in char, io.with_str_reader, base64 and md5sum, cell.empty_cell and is_empty. --- src/libcore/char.rs | 12 +-- src/libcore/io.rs | 2 +- src/libstd/base64.rs | 174 ++++++++++++++++++++++--------------------- src/libstd/cell.rs | 4 +- src/libstd/md4.rs | 14 ++-- 5 files changed, 105 insertions(+), 101 deletions(-) diff --git a/src/libcore/char.rs b/src/libcore/char.rs index b76571864e070..802aaf12bdd48 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -126,16 +126,18 @@ pub pure fn to_digit(c: char, radix: uint) -> Option { * - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN` * - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN` */ -pub fn escape_unicode(c: char) -> ~str { +pub pure fn escape_unicode(c: char) -> ~str { let s = u32::to_str(c as u32, 16u); let (c, pad) = (if c <= '\xff' { ('x', 2u) } else if c <= '\uffff' { ('u', 4u) } else { ('U', 8u) }); assert str::len(s) <= pad; let mut out = ~"\\"; - str::push_str(&mut out, str::from_char(c)); - for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } - str::push_str(&mut out, s); + unsafe { + str::push_str(&mut out, str::from_char(c)); + for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } + str::push_str(&mut out, s); + } move out } @@ -151,7 +153,7 @@ pub fn escape_unicode(c: char) -> ~str { * - Any other chars in the range [0x20,0x7e] are not escaped. * - Any other chars are given hex unicode escapes; see `escape_unicode`. */ -pub fn escape_default(c: char) -> ~str { +pub pure fn escape_default(c: char) -> ~str { match c { '\t' => ~"\\t", '\r' => ~"\\r", diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 4629878b4b7ea..535ab883581bb 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -512,7 +512,7 @@ pub pure fn with_bytes_reader(bytes: &[u8], f: fn(Reader) -> t) -> t { f(BytesReader { bytes: bytes, pos: 0u } as Reader) } -pub fn with_str_reader(s: &str, f: fn(Reader) -> T) -> T { +pub pure fn with_str_reader(s: &str, f: fn(Reader) -> T) -> T { str::byte_slice(s, |bytes| with_bytes_reader(bytes, f)) } diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 9bad4d3975003..177074de8f65a 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -2,72 +2,73 @@ use io::Reader; pub trait ToBase64 { - fn to_base64() -> ~str; + pure fn to_base64() -> ~str; } impl &[u8]: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { let chars = str::chars( ~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ); - let len = self.len(); let mut s = ~""; - str::reserve(&mut s, ((len + 3u) / 4u) * 3u); - - let mut i = 0u; - - while i < len - (len % 3u) { - let n = (self[i] as uint) << 16u | - (self[i + 1u] as uint) << 8u | - (self[i + 2u] as uint); - - // This 24-bit number gets separated into four 6-bit numbers. - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, chars[n & 63u]); - - i += 3u; - } - - // Heh, would be cool if we knew this was exhaustive - // (the dream of bounded integer types) - match len % 3 { - 0 => (), - 1 => { - let n = (self[i] as uint) << 16u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, '='); - str::push_char(&mut s, '='); - } - 2 => { - let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, '='); - } - _ => fail ~"Algebra is broken, please alert the math police" + unsafe { + let len = self.len(); + str::reserve(&mut s, ((len + 3u) / 4u) * 3u); + + let mut i = 0u; + + while i < len - (len % 3u) { + let n = (self[i] as uint) << 16u | + (self[i + 1u] as uint) << 8u | + (self[i + 2u] as uint); + + // This 24-bit number gets separated into four 6-bit numbers. + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, chars[n & 63u]); + + i += 3u; + } + + // Heh, would be cool if we knew this was exhaustive + // (the dream of bounded integer types) + match len % 3 { + 0 => (), + 1 => { + let n = (self[i] as uint) << 16u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, '='); + str::push_char(&mut s, '='); + } + 2 => { + let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, '='); + } + _ => fail ~"Algebra is broken, please alert the math police" + } } - s } } impl &str: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { str::to_bytes(self).to_base64() } } pub trait FromBase64 { - fn from_base64() -> ~[u8]; + pure fn from_base64() -> ~[u8]; } impl ~[u8]: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { if self.len() % 4u != 0u { fail ~"invalid base64 length"; } let len = self.len(); @@ -80,55 +81,56 @@ impl ~[u8]: FromBase64 { let mut r = vec::with_capacity((len / 4u) * 3u - padding); - let mut i = 0u; - while i < len { - let mut n = 0u; - - for iter::repeat(4u) { - let ch = self[i] as char; - n <<= 6u; - - if ch >= 'A' && ch <= 'Z' { - n |= (ch as uint) - 0x41u; - } else if ch >= 'a' && ch <= 'z' { - n |= (ch as uint) - 0x47u; - } else if ch >= '0' && ch <= '9' { - n |= (ch as uint) + 0x04u; - } else if ch == '+' { - n |= 0x3Eu; - } else if ch == '/' { - n |= 0x3Fu; - } else if ch == '=' { - match len - i { - 1u => { - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - return copy r; - } - 2u => { - r.push(((n >> 10u) & 0xFFu) as u8); - return copy r; - } - _ => fail ~"invalid base64 padding" + unsafe { + let mut i = 0u; + while i < len { + let mut n = 0u; + + for iter::repeat(4u) { + let ch = self[i] as char; + n <<= 6u; + + if ch >= 'A' && ch <= 'Z' { + n |= (ch as uint) - 0x41u; + } else if ch >= 'a' && ch <= 'z' { + n |= (ch as uint) - 0x47u; + } else if ch >= '0' && ch <= '9' { + n |= (ch as uint) + 0x04u; + } else if ch == '+' { + n |= 0x3Eu; + } else if ch == '/' { + n |= 0x3Fu; + } else if ch == '=' { + match len - i { + 1u => { + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + return copy r; + } + 2u => { + r.push(((n >> 10u) & 0xFFu) as u8); + return copy r; + } + _ => fail ~"invalid base64 padding" + } + } else { + fail ~"invalid base64 character"; } - } else { - fail ~"invalid base64 character"; - } - - i += 1u; - }; - - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - r.push(((n ) & 0xFFu) as u8); + + i += 1u; + }; + + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + r.push(((n ) & 0xFFu) as u8); + } } - r } } impl ~str: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { str::to_bytes(self).from_base64() } } diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 4f79bf2b31698..78027aa890760 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -12,7 +12,7 @@ pub fn Cell(value: T) -> Cell { Cell { value: Some(move value) } } -pub fn empty_cell() -> Cell { +pub pure fn empty_cell() -> Cell { Cell { value: None } } @@ -37,7 +37,7 @@ impl Cell { } /// Returns true if the cell is empty and false if the cell is full. - fn is_empty() -> bool { + pure fn is_empty() -> bool { self.value.is_none() } diff --git a/src/libstd/md4.rs b/src/libstd/md4.rs index 581beb78bdc55..d9bc03c311de7 100644 --- a/src/libstd/md4.rs +++ b/src/libstd/md4.rs @@ -1,6 +1,6 @@ #[forbid(deprecated_mode)]; -pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { +pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { // subtle: if orig_len is merely uint, then the code below // which performs shifts by 32 bits or more has undefined // results. @@ -10,14 +10,14 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]); let mut bitlen = orig_len + 8u64; while (bitlen + 64u64) % 512u64 > 0u64 { - msg.push(0u8); + unsafe {msg.push(0u8);} bitlen += 8u64; } // append length let mut i = 0u64; while i < 8u64 { - msg.push((orig_len >> (i * 8u64)) as u8); + unsafe {msg.push((orig_len >> (i * 8u64)) as u8);} i += 1u64; } @@ -26,7 +26,7 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut c = 0x98badcfeu32; let mut d = 0x10325476u32; - fn rot(r: int, x: u32) -> u32 { + pure fn rot(r: int, x: u32) -> u32 { let r = r as u32; (x << r) | (x >> (32u32 - r)) } @@ -84,9 +84,9 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { return {a: a, b: b, c: c, d: d}; } -pub fn md4_str(msg: &[u8]) -> ~str { +pub pure fn md4_str(msg: &[u8]) -> ~str { let {a, b, c, d} = md4(msg); - fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { + pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { f(a); f(b); f(c); f(d); } let mut result = ~""; @@ -102,7 +102,7 @@ pub fn md4_str(msg: &[u8]) -> ~str { result } -pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } +pub pure fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } #[test] fn test_md4() { From 333d268b3ef5846fc22c73c01f69a12bf11f6b9d Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 10:21:14 -0800 Subject: [PATCH 48/56] Made merge_sort pure --- src/libstd/sort.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index 4f06cc40c229c..451d5e805d051 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -13,10 +13,10 @@ type Le = pure fn(v1: &T, v2: &T) -> bool; * Has worst case O(n log n) performance, best case O(n), but * is not space efficient. This is a stable sort. */ -pub fn merge_sort(v: &[const T], le: Le) -> ~[T] { +pub pure fn merge_sort(v: &[const T], le: Le) -> ~[T] { type Slice = (uint, uint); - return merge_sort_(v, (0u, len(v)), le); + unsafe {return merge_sort_(v, (0u, len(v)), le);} fn merge_sort_(v: &[const T], slice: Slice, le: Le) -> ~[T] { From 361aea94f2e037139e29a573ce4486c670bacb87 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 11:00:38 -0800 Subject: [PATCH 49/56] Fixing warnings for long and blank lines --- src/libcore/char.rs | 3 ++- src/libstd/base64.rs | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 802aaf12bdd48..fb8cc274217d2 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -135,7 +135,8 @@ pub pure fn escape_unicode(c: char) -> ~str { let mut out = ~"\\"; unsafe { str::push_str(&mut out, str::from_char(c)); - for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); } + for uint::range(str::len(s), pad) |_i| + { str::push_str(&mut out, ~"0"); } str::push_str(&mut out, s); } move out diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 177074de8f65a..5db71b12185bc 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -15,23 +15,23 @@ impl &[u8]: ToBase64 { unsafe { let len = self.len(); str::reserve(&mut s, ((len + 3u) / 4u) * 3u); - + let mut i = 0u; - + while i < len - (len % 3u) { let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u | (self[i + 2u] as uint); - + // This 24-bit number gets separated into four 6-bit numbers. str::push_char(&mut s, chars[(n >> 18u) & 63u]); str::push_char(&mut s, chars[(n >> 12u) & 63u]); str::push_char(&mut s, chars[(n >> 6u) & 63u]); str::push_char(&mut s, chars[n & 63u]); - + i += 3u; } - + // Heh, would be cool if we knew this was exhaustive // (the dream of bounded integer types) match len % 3 { @@ -44,7 +44,8 @@ impl &[u8]: ToBase64 { str::push_char(&mut s, '='); } 2 => { - let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; + let n = (self[i] as uint) << 16u | + (self[i + 1u] as uint) << 8u; str::push_char(&mut s, chars[(n >> 18u) & 63u]); str::push_char(&mut s, chars[(n >> 12u) & 63u]); str::push_char(&mut s, chars[(n >> 6u) & 63u]); @@ -85,11 +86,11 @@ impl ~[u8]: FromBase64 { let mut i = 0u; while i < len { let mut n = 0u; - + for iter::repeat(4u) { let ch = self[i] as char; n <<= 6u; - + if ch >= 'A' && ch <= 'Z' { n |= (ch as uint) - 0x41u; } else if ch >= 'a' && ch <= 'z' { @@ -116,10 +117,10 @@ impl ~[u8]: FromBase64 { } else { fail ~"invalid base64 character"; } - + i += 1u; }; - + r.push(((n >> 16u) & 0xFFu) as u8); r.push(((n >> 8u ) & 0xFFu) as u8); r.push(((n ) & 0xFFu) as u8); From 0fd9c9d0547c0f882465596f7cee77286cb8e56b Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 11:01:08 -0800 Subject: [PATCH 50/56] Made from_str pure --- src/libcore/from_str.rs | 2 +- src/libcore/int-template.rs | 9 ++++++--- src/libcore/uint-template.rs | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/libcore/from_str.rs b/src/libcore/from_str.rs index c4dd2536e2cfc..8e9cd5a022763 100644 --- a/src/libcore/from_str.rs +++ b/src/libcore/from_str.rs @@ -7,6 +7,6 @@ use option::Option; pub trait FromStr { - static fn from_str(s: &str) -> Option; + static pure fn from_str(s: &str) -> Option; } diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index db95bc46ffe8a..e5da7d6abe106 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -106,7 +106,7 @@ impl T: iter::Times { * * buf - A byte buffer * * radix - The base of the number */ -pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { +pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option { if vec::len(buf) == 0u { return None; } let mut i = vec::len(buf) - 1u; let mut start = 0u; @@ -129,10 +129,13 @@ pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { } /// Parse a string to an int -pub fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } +pub pure fn from_str(s: &str) -> Option +{ + parse_bytes(str::to_bytes(s), 10u) +} impl T : FromStr { - static fn from_str(s: &str) -> Option { from_str(s) } + static pure fn from_str(s: &str) -> Option { from_str(s) } } /// Convert to a string in a given base diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index ceb525f5f8ddd..d406e36cc0771 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -100,7 +100,7 @@ impl T: iter::Times { * * `buf` must not be empty */ -pub fn parse_bytes(buf: &[const u8], radix: uint) -> Option { +pub pure fn parse_bytes(buf: &[const u8], radix: uint) -> Option { if vec::len(buf) == 0u { return None; } let mut i = vec::len(buf) - 1u; let mut power = 1u as T; @@ -117,10 +117,13 @@ pub fn parse_bytes(buf: &[const u8], radix: uint) -> Option { } /// Parse a string to an int -pub fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } +pub pure fn from_str(s: &str) -> Option +{ + parse_bytes(str::to_bytes(s), 10u) +} impl T : FromStr { - static fn from_str(s: &str) -> Option { from_str(s) } + static pure fn from_str(s: &str) -> Option { from_str(s) } } /// Parse a string as an unsigned integer. From a41903d234e21bf1a4616f6f5da83a0122c41092 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 18 Nov 2012 14:58:57 -0800 Subject: [PATCH 51/56] Remove some unused code from lexer --- src/libsyntax/parse/lexer.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index d65be043f86a5..5e174f7f34f43 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -145,6 +145,7 @@ fn bump(rdr: string_reader) { rdr.last_pos = rdr.pos; let current_byte_offset = byte_offset(rdr).to_uint();; if current_byte_offset < (*rdr.src).len() { + assert rdr.curr != -1 as char; 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; @@ -161,12 +162,7 @@ fn bump(rdr: string_reader) { BytePos(current_byte_offset), byte_offset_diff); } } else { - // XXX: What does this accomplish? - if (rdr.curr != -1 as char) { - rdr.pos = rdr.pos + BytePos(1u); - rdr.col += CharPos(1u); - rdr.curr = -1 as char; - } + rdr.curr = -1 as char; } } fn is_eof(rdr: string_reader) -> bool { From 68c73dc5f29bf75d6c76054ebfaf2fbb3751a71b Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 18 Nov 2012 15:00:43 -0800 Subject: [PATCH 52/56] Whitespace --- src/libstd/getopts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index b7d6dc697762f..e5904b81d87d2 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -1028,7 +1028,7 @@ mod tests { } #[test] - fn test_optflagmulti_short2a() { + fn test_optflagmulti_short2a() { let args = ~[~"-v", ~"-v"]; let opts = ~[optflagmulti(~"v")]; let rs = getopts(args, opts); From ddbff6fd2a46b2b7073794d200140f0407208c07 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 9 Nov 2012 12:25:24 -0800 Subject: [PATCH 53/56] syntax: Remove cdir_syntax. Unused --- src/libsyntax/ast.rs | 2 -- src/libsyntax/fold.rs | 1 - src/libsyntax/parse/eval.rs | 1 - src/libsyntax/visit.rs | 1 - 4 files changed, 5 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index fa4998ef7b535..e9031a2890a06 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -276,8 +276,6 @@ enum crate_directive_ { // exists only to preserve the view items in order in case we decide to // pretty-print crates in the future. cdir_view_item(@view_item), - - cdir_syntax(@path), } type crate_directive = spanned; diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 4d51ecded015e..9d57b5ae814f6 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -170,7 +170,6 @@ fn noop_fold_crate_directive(cd: crate_directive_, fld: ast_fold) -> /* FIXME (#2543) */ copy attrs) } cdir_view_item(vi) => cdir_view_item(fld.fold_view_item(vi)), - cdir_syntax(_) => copy cd } } diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index f08f195446442..9a81d6463060d 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -124,7 +124,6 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, items.push(i); } ast::cdir_view_item(vi) => view_items.push(vi), - ast::cdir_syntax(*) => () } } // diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 3bc8c7b94209c..ae0de2add81b8 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -100,7 +100,6 @@ fn visit_crate_directive(cd: @crate_directive, e: E, v: vt) { visit_crate_directive(*cdir, e, v); }, cdir_view_item(vi) => v.visit_view_item(vi, e, v), - cdir_syntax(_) => () } } From 72cc1aca175044ceb003a8b270940bec1da85460 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 9 Nov 2012 16:31:44 -0800 Subject: [PATCH 54/56] Parse file mods from .rs files --- src/libsyntax/parse/eval.rs | 53 +++++++++++++------ src/libsyntax/parse/parser.rs | 27 +++++++--- src/test/compile-fail/mod_file_aux.rs | 3 ++ .../compile-fail/mod_file_correct_spans.rs | 7 +++ src/test/run-pass/mod_file.rs | 9 ++++ src/test/run-pass/mod_file_aux.rs | 3 ++ src/test/run-pass/mod_file_with_path_attr.rs | 10 ++++ 7 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 src/test/compile-fail/mod_file_aux.rs create mode 100644 src/test/compile-fail/mod_file_correct_spans.rs create mode 100644 src/test/run-pass/mod_file.rs create mode 100644 src/test/run-pass/mod_file_aux.rs create mode 100644 src/test/run-pass/mod_file_with_path_attr.rs diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 9a81d6463060d..660e88c7101d2 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,7 +1,9 @@ use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; +use ast_util::mk_sp; export eval_crate_directives_to_mod; +export eval_src_mod; type ctx = @{sess: parse::parse_sess, @@ -79,29 +81,46 @@ fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { } } +fn eval_src_mod(cx: ctx, prefix: &Path, id: ast::ident, + outer_attrs: ~[ast::attribute]) -> (ast::item_, ~[ast::attribute]) { + let file_path = Path(cdir_path_opt( + cx.sess.interner.get(id) + ~".rs", outer_attrs)); + let full_path = if file_path.is_absolute { + copy file_path + } else { + prefix.push_many(file_path.components) + }; + let p0 = + new_parser_from_file(cx.sess, cx.cfg, + &full_path, SOURCE_FILE); + let inner_attrs = p0.parse_inner_attrs_and_next(); + let mod_attrs = vec::append(outer_attrs, inner_attrs.inner); + let first_item_outer_attrs = inner_attrs.next; + let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs); + return (ast::item_mod(m0), mod_attrs); +} + +// XXX: Duplicated from parser.rs +fn mk_item(ctx: ctx, lo: BytePos, hi: BytePos, +ident: ast::ident, + +node: ast::item_, vis: ast::visibility, + +attrs: ~[ast::attribute]) -> @ast::item { + return @{ident: ident, + attrs: attrs, + id: next_node_id(ctx.sess), + node: node, + vis: vis, + span: mk_sp(lo, hi)}; +} + fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, view_items: &mut ~[@ast::view_item], items: &mut ~[@ast::item]) { match cdir.node { ast::cdir_src_mod(vis, id, attrs) => { - let file_path = Path(cdir_path_opt( - cx.sess.interner.get(id) + ~".rs", attrs)); - let full_path = if file_path.is_absolute { - copy file_path - } else { - prefix.push_many(file_path.components) - }; - let p0 = - new_parser_from_file(cx.sess, cx.cfg, - &full_path, SOURCE_FILE); - let inner_attrs = p0.parse_inner_attrs_and_next(); - let mod_attrs = vec::append(attrs, inner_attrs.inner); - let first_item_outer_attrs = inner_attrs.next; - let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs); - - let i = p0.mk_item(cdir.span.lo, cdir.span.hi, + let (m, mod_attrs) = eval_src_mod(cx, prefix, id, attrs); + let i = mk_item(cx, cdir.span.lo, cdir.span.hi, /* FIXME (#2543) */ copy id, - ast::item_mod(m0), vis, mod_attrs); + m, vis, mod_attrs); items.push(i); } ast::cdir_dir_mod(vis, id, cdirs, attrs) => { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2f96f6ba0a098..12ae135e52550 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2958,13 +2958,26 @@ impl Parser { (id, item_const(ty, e), None) } - fn parse_item_mod() -> item_info { + fn parse_item_mod(outer_attrs: ~[ast::attribute]) -> item_info { let id = self.parse_ident(); - self.expect(token::LBRACE); - let inner_attrs = self.parse_inner_attrs_and_next(); - let m = self.parse_mod_items(token::RBRACE, inner_attrs.next); - self.expect(token::RBRACE); - (id, item_mod(m), Some(inner_attrs.inner)) + if self.token == token::SEMI { + self.bump(); + // This mod is in an external file. Let's go get it! + let eval_ctx = @{ + sess: self.sess, + cfg: self.cfg + }; + let prefix = Path(self.sess.cm.span_to_filename(copy self.span)); + let prefix = prefix.dir_path(); + let (m, attrs) = eval::eval_src_mod(eval_ctx, &prefix, id, outer_attrs); + (id, m, Some(move attrs)) + } else { + self.expect(token::LBRACE); + let inner_attrs = self.parse_inner_attrs_and_next(); + let m = self.parse_mod_items(token::RBRACE, inner_attrs.next); + self.expect(token::RBRACE); + (id, item_mod(m), Some(inner_attrs.inner)) + } } fn parse_item_foreign_fn( +attrs: ~[attribute]) -> @foreign_item { @@ -3360,7 +3373,7 @@ impl Parser { return self.parse_item_foreign_mod(lo, visibility, attrs, items_allowed); } else if items_allowed && self.eat_keyword(~"mod") { - let (ident, item_, extra_attrs) = self.parse_item_mod(); + let (ident, item_, extra_attrs) = self.parse_item_mod(attrs); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); diff --git a/src/test/compile-fail/mod_file_aux.rs b/src/test/compile-fail/mod_file_aux.rs new file mode 100644 index 0000000000000..313efe558de32 --- /dev/null +++ b/src/test/compile-fail/mod_file_aux.rs @@ -0,0 +1,3 @@ +// xfail-test Not a test. Used by other tests + +pub fn foo() -> int { 10 } diff --git a/src/test/compile-fail/mod_file_correct_spans.rs b/src/test/compile-fail/mod_file_correct_spans.rs new file mode 100644 index 0000000000000..b34c11a07aca6 --- /dev/null +++ b/src/test/compile-fail/mod_file_correct_spans.rs @@ -0,0 +1,7 @@ +// Testing that the codemap is maintained correctly when parsing mods from external files + +mod mod_file_aux; + +fn main() { + assert mod_file_aux::bar() == 10; //~ ERROR unresolved name +} \ No newline at end of file diff --git a/src/test/run-pass/mod_file.rs b/src/test/run-pass/mod_file.rs new file mode 100644 index 0000000000000..6e8cb220dc079 --- /dev/null +++ b/src/test/run-pass/mod_file.rs @@ -0,0 +1,9 @@ +// xfail-pretty + +// Testing that a plain .rs file can load modules from other source files + +mod mod_file_aux; + +fn main() { + assert mod_file_aux::foo() == 10; +} \ No newline at end of file diff --git a/src/test/run-pass/mod_file_aux.rs b/src/test/run-pass/mod_file_aux.rs new file mode 100644 index 0000000000000..313efe558de32 --- /dev/null +++ b/src/test/run-pass/mod_file_aux.rs @@ -0,0 +1,3 @@ +// xfail-test Not a test. Used by other tests + +pub fn foo() -> int { 10 } diff --git a/src/test/run-pass/mod_file_with_path_attr.rs b/src/test/run-pass/mod_file_with_path_attr.rs new file mode 100644 index 0000000000000..e7191099e4cdd --- /dev/null +++ b/src/test/run-pass/mod_file_with_path_attr.rs @@ -0,0 +1,10 @@ +// xfail-pretty + +// Testing that a plain .rs file can load modules from other source files + +#[path = "mod_file_aux.rs"] +mod m; + +fn main() { + assert m::foo() == 10; +} \ No newline at end of file From 74b2e9979738ce413209aa4342fc35afe68c68de Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 18 Nov 2012 14:14:40 -0800 Subject: [PATCH 55/56] Report errors better when failing to open files for sub-parsers --- src/libsyntax/ext/source_util.rs | 7 ++- src/libsyntax/parse.rs | 59 ++++++++++++++----- src/libsyntax/parse/eval.rs | 16 +++-- src/libsyntax/parse/parser.rs | 4 +- src/test/compile-fail/mod_file_not_exist.rs | 5 ++ .../compile-fail/mod_file_with_path_attr.rs | 6 ++ 6 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 src/test/compile-fail/mod_file_not_exist.rs create mode 100644 src/test/compile-fail/mod_file_with_path_attr.rs diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index baf92175e7d6b..1bd3ec8423e13 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -58,9 +58,10 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, _body: ast::mac_body) -> @ast::expr { let args = get_mac_args(cx, sp, arg, 1u, option::Some(1u), ~"include"); let file = expr_to_str(cx, args[0], ~"include_str! requires a string"); - let p = parse::new_parser_from_file(cx.parse_sess(), cx.cfg(), - &res_rel_file(cx, sp, &Path(file)), - parse::parser::SOURCE_FILE); + let p = parse::new_sub_parser_from_file( + cx.parse_sess(), cx.cfg(), + &res_rel_file(cx, sp, &Path(file)), + parse::parser::SOURCE_FILE, sp); return p.parse_expr(); } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index 593ff6d034ee0..fc9474bab24ae 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -6,6 +6,7 @@ export next_node_id; export new_parser_from_file, new_parser_etc_from_file; export new_parser_from_source_str; export new_parser_from_tt; +export new_sub_parser_from_file; export parse_crate_from_file, parse_crate_from_crate_file; export parse_crate_from_source_str; export parse_expr_from_source_str, parse_item_from_source_str; @@ -20,7 +21,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::{CodeMap, FileMap, CharPos, BytePos}; +use codemap::{span, CodeMap, FileMap, CharPos, BytePos}; type parse_sess = @{ cm: @codemap::CodeMap, @@ -61,8 +62,8 @@ fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_parser_from_file(sess, cfg, input, - parser::CRATE_FILE); + let p = new_crate_parser_from_file(sess, cfg, input, + parser::CRATE_FILE); let lo = p.span.lo; let prefix = input.dir_path(); let leading_attrs = p.parse_inner_attrs_and_next(); @@ -84,8 +85,8 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_parser_from_file(sess, cfg, input, - parser::SOURCE_FILE); + let p = new_crate_parser_from_file(sess, cfg, input, + parser::SOURCE_FILE); let r = p.parse_crate_mod(cfg); return r; } @@ -163,17 +164,45 @@ fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, } fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, ftype: parser::file_type) -> Parser { - let res = io::read_whole_file_str(path); - match res { - result::Ok(_) => { /* Continue. */ } - result::Err(e) => sess.span_diagnostic.handler().fatal(e) + path: &Path, + ftype: parser::file_type) -> Result { + match io::read_whole_file_str(path) { + result::Ok(move src) => { + let filemap = sess.cm.new_filemap(path.to_str(), @move src); + let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, + sess.interner); + + Ok(Parser(sess, cfg, srdr as reader, ftype)) + + } + result::Err(move e) => Err(move e) + } +} + +/// Create a new parser for an entire crate, handling errors as appropriate +/// if the file doesn't exist +fn new_crate_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, + path: &Path, + ftype: parser::file_type) -> Parser { + match new_parser_from_file(sess, cfg, path, ftype) { + Ok(move parser) => move parser, + Err(move e) => { + sess.span_diagnostic.handler().fatal(e) + } + } +} + +/// Create a new parser based on a span from an existing parser. Handles +/// error messages correctly when the file does not exist. +fn new_sub_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, + path: &Path, ftype: parser::file_type, + sp: span) -> Parser { + match new_parser_from_file(sess, cfg, path, ftype) { + Ok(move parser) => move parser, + Err(move e) => { + sess.span_diagnostic.span_fatal(sp, e) + } } - let src = @result::unwrap(res); - let filemap = sess.cm.new_filemap(path.to_str(), src); - let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, - sess.interner); - return Parser(sess, cfg, srdr as reader, ftype); } fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 660e88c7101d2..1873a34627052 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,6 +1,7 @@ use parser::{Parser, SOURCE_FILE}; use attr::parser_attr; use ast_util::mk_sp; +use codemap::span; export eval_crate_directives_to_mod; export eval_src_mod; @@ -64,8 +65,10 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option) let modpath = &companion_file(prefix, suffix); if file_exists(modpath) { debug!("found companion mod"); - let p0 = new_parser_from_file(cx.sess, cx.cfg, - modpath, SOURCE_FILE); + // XXX: Using a dummy span, but this code will go away soon + let p0 = new_sub_parser_from_file(cx.sess, cx.cfg, + modpath, SOURCE_FILE, + ast_util::dummy_sp()); let inner_attrs = p0.parse_inner_attrs_and_next(); let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next); return (m0.view_items, m0.items, inner_attrs.inner); @@ -82,7 +85,8 @@ fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { } fn eval_src_mod(cx: ctx, prefix: &Path, id: ast::ident, - outer_attrs: ~[ast::attribute]) -> (ast::item_, ~[ast::attribute]) { + outer_attrs: ~[ast::attribute], + sp: span) -> (ast::item_, ~[ast::attribute]) { let file_path = Path(cdir_path_opt( cx.sess.interner.get(id) + ~".rs", outer_attrs)); let full_path = if file_path.is_absolute { @@ -91,8 +95,8 @@ fn eval_src_mod(cx: ctx, prefix: &Path, id: ast::ident, prefix.push_many(file_path.components) }; let p0 = - new_parser_from_file(cx.sess, cx.cfg, - &full_path, SOURCE_FILE); + new_sub_parser_from_file(cx.sess, cx.cfg, + &full_path, SOURCE_FILE, sp); let inner_attrs = p0.parse_inner_attrs_and_next(); let mod_attrs = vec::append(outer_attrs, inner_attrs.inner); let first_item_outer_attrs = inner_attrs.next; @@ -117,7 +121,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path, items: &mut ~[@ast::item]) { match cdir.node { ast::cdir_src_mod(vis, id, attrs) => { - let (m, mod_attrs) = eval_src_mod(cx, prefix, id, attrs); + let (m, mod_attrs) = eval_src_mod(cx, prefix, id, attrs, cdir.span); let i = mk_item(cx, cdir.span.lo, cdir.span.hi, /* FIXME (#2543) */ copy id, m, vis, mod_attrs); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 12ae135e52550..9d71f83b28d50 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2959,6 +2959,7 @@ impl Parser { } fn parse_item_mod(outer_attrs: ~[ast::attribute]) -> item_info { + let id_span = self.span; let id = self.parse_ident(); if self.token == token::SEMI { self.bump(); @@ -2969,7 +2970,8 @@ impl Parser { }; let prefix = Path(self.sess.cm.span_to_filename(copy self.span)); let prefix = prefix.dir_path(); - let (m, attrs) = eval::eval_src_mod(eval_ctx, &prefix, id, outer_attrs); + let (m, attrs) = eval::eval_src_mod(eval_ctx, &prefix, id, + outer_attrs, id_span); (id, m, Some(move attrs)) } else { self.expect(token::LBRACE); diff --git a/src/test/compile-fail/mod_file_not_exist.rs b/src/test/compile-fail/mod_file_not_exist.rs new file mode 100644 index 0000000000000..5e571af8955e0 --- /dev/null +++ b/src/test/compile-fail/mod_file_not_exist.rs @@ -0,0 +1,5 @@ +mod not_a_real_file; //~ ERROR not_a_real_file.rs + +fn main() { + assert mod_file_aux::bar() == 10; +} \ No newline at end of file diff --git a/src/test/compile-fail/mod_file_with_path_attr.rs b/src/test/compile-fail/mod_file_with_path_attr.rs new file mode 100644 index 0000000000000..3baa18be9f1f6 --- /dev/null +++ b/src/test/compile-fail/mod_file_with_path_attr.rs @@ -0,0 +1,6 @@ +#[path = "not_a_real_file.rs"] +mod m; //~ ERROR not_a_real_file.rs + +fn main() { + assert m::foo() == 10; +} \ No newline at end of file From 371be3c6c4c9be74ee224f74f41006fefa3dda1b Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 18 Nov 2012 15:55:03 -0800 Subject: [PATCH 56/56] Remove unused file_type enum from the parser --- src/libsyntax/ext/pipes.rs | 4 ++-- src/libsyntax/ext/source_util.rs | 3 +-- src/libsyntax/ext/trace_macros.rs | 6 +++--- src/libsyntax/ext/tt/macro_parser.rs | 4 ++-- src/libsyntax/ext/tt/macro_rules.rs | 4 ++-- src/libsyntax/parse.rs | 26 ++++++++++---------------- src/libsyntax/parse/eval.rs | 6 +++--- src/libsyntax/parse/parser.rs | 9 +-------- 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/libsyntax/ext/pipes.rs b/src/libsyntax/ext/pipes.rs index 4d04552bfa15a..b4c49b12d5936 100644 --- a/src/libsyntax/ext/pipes.rs +++ b/src/libsyntax/ext/pipes.rs @@ -37,7 +37,7 @@ use codemap::span; use ext::base::ext_ctxt; use ast::tt_delim; use parse::lexer::{new_tt_reader, reader}; -use parse::parser::{Parser, SOURCE_FILE}; +use parse::parser::Parser; use parse::common::parser_common; use pipes::parse_proto::proto_parser; @@ -52,7 +52,7 @@ fn expand_proto(cx: ext_ctxt, _sp: span, id: ast::ident, let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic, cx.parse_sess().interner, None, tt); let rdr = tt_rdr as reader; - let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup()); let proto = rust_parser.parse_proto(cx.str_of(id)); diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 1bd3ec8423e13..a105db0c08acb 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -60,8 +60,7 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg, let file = expr_to_str(cx, args[0], ~"include_str! requires a string"); let p = parse::new_sub_parser_from_file( cx.parse_sess(), cx.cfg(), - &res_rel_file(cx, sp, &Path(file)), - parse::parser::SOURCE_FILE, sp); + &res_rel_file(cx, sp, &Path(file)), sp); return p.parse_expr(); } diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index 0c7d408db7cc3..a4e768aa7dcc3 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -2,7 +2,7 @@ use codemap::span; use ext::base::ext_ctxt; use ast::tt_delim; use parse::lexer::{new_tt_reader, reader}; -use parse::parser::{Parser, SOURCE_FILE}; +use parse::parser::Parser; use parse::common::parser_common; fn expand_trace_macros(cx: ext_ctxt, sp: span, @@ -13,7 +13,7 @@ fn expand_trace_macros(cx: ext_ctxt, sp: span, let tt_rdr = new_tt_reader(cx.parse_sess().span_diagnostic, cx.parse_sess().interner, None, tt); let rdr = tt_rdr as reader; - let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup()); let arg = cx.str_of(rust_parser.parse_ident()); match arg { @@ -21,7 +21,7 @@ fn expand_trace_macros(cx: ext_ctxt, sp: span, ~"false" => cx.set_trace_macros(false), _ => cx.span_fatal(sp, ~"trace_macros! only accepts `true` or `false`") } - let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup()); let result = rust_parser.parse_expr(); base::mr_expr(result) } diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 6779ed263d5a8..86218acb5a196 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -3,7 +3,7 @@ use parse::token; use parse::token::{Token, EOF, to_str, nonterminal}; use parse::lexer::*; //resolve bug? //import parse::lexer::{reader, tt_reader, tt_reader_as_reader}; -use parse::parser::{Parser, SOURCE_FILE}; +use parse::parser::Parser; //import parse::common::parser_common; use parse::common::*; //resolve bug? use parse::parse_sess; @@ -355,7 +355,7 @@ fn parse(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader, ms: ~[matcher]) } rdr.next_token(); } else /* bb_eis.len() == 1 */ { - let rust_parser = Parser(sess, cfg, rdr.dup(), SOURCE_FILE); + let rust_parser = Parser(sess, cfg, rdr.dup()); let ei = bb_eis.pop(); match ei.elts[ei.idx].node { diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 8bfd1c0a18d39..56418989c497a 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -4,7 +4,7 @@ use ast::{ident, matcher_, matcher, match_tok, match_nonterminal, match_seq, tt_delim}; use parse::lexer::{new_tt_reader, reader}; use parse::token::{FAT_ARROW, SEMI, LBRACE, RBRACE, nt_matchers, nt_tt}; -use parse::parser::{Parser, SOURCE_FILE}; +use parse::parser::Parser; use macro_parser::{parse, parse_or_else, success, failure, named_match, matched_seq, matched_nonterminal, error}; use std::map::HashMap; @@ -88,7 +88,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, let trncbr = new_tt_reader(s_d, itr, Some(named_matches), ~[rhs]); let p = Parser(cx.parse_sess(), cx.cfg(), - trncbr as reader, SOURCE_FILE); + trncbr as reader); let e = p.parse_expr(); return mr_expr(e); } diff --git a/src/libsyntax/parse.rs b/src/libsyntax/parse.rs index fc9474bab24ae..2e0b204df8d04 100644 --- a/src/libsyntax/parse.rs +++ b/src/libsyntax/parse.rs @@ -62,8 +62,7 @@ fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_crate_parser_from_file(sess, cfg, input, - parser::CRATE_FILE); + let p = new_crate_parser_from_file(sess, cfg, input); let lo = p.span.lo; let prefix = input.dir_path(); let leading_attrs = p.parse_inner_attrs_and_next(); @@ -85,8 +84,7 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg, fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg, sess: parse_sess) -> @ast::crate { - let p = new_crate_parser_from_file(sess, cfg, input, - parser::SOURCE_FILE); + let p = new_crate_parser_from_file(sess, cfg, input); let r = p.parse_crate_mod(cfg); return r; } @@ -156,23 +154,21 @@ fn next_node_id(sess: parse_sess) -> node_id { fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, +name: ~str, +ss: codemap::FileSubstr, source: @~str) -> Parser { - let ftype = parser::SOURCE_FILE; let filemap = sess.cm.new_filemap_w_substr(name, ss, source); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - return Parser(sess, cfg, srdr as reader, ftype); + return Parser(sess, cfg, srdr as reader); } fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, - ftype: parser::file_type) -> Result { + path: &Path) -> Result { match io::read_whole_file_str(path) { result::Ok(move src) => { let filemap = sess.cm.new_filemap(path.to_str(), @move src); let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - Ok(Parser(sess, cfg, srdr as reader, ftype)) + Ok(Parser(sess, cfg, srdr as reader)) } result::Err(move e) => Err(move e) @@ -182,9 +178,8 @@ fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, /// Create a new parser for an entire crate, handling errors as appropriate /// if the file doesn't exist fn new_crate_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, - ftype: parser::file_type) -> Parser { - match new_parser_from_file(sess, cfg, path, ftype) { + path: &Path) -> Parser { + match new_parser_from_file(sess, cfg, path) { Ok(move parser) => move parser, Err(move e) => { sess.span_diagnostic.handler().fatal(e) @@ -195,9 +190,8 @@ fn new_crate_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, /// Create a new parser based on a span from an existing parser. Handles /// error messages correctly when the file does not exist. fn new_sub_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, ftype: parser::file_type, - sp: span) -> Parser { - match new_parser_from_file(sess, cfg, path, ftype) { + path: &Path, sp: span) -> Parser { + match new_parser_from_file(sess, cfg, path) { Ok(move parser) => move parser, Err(move e) => { sess.span_diagnostic.span_fatal(sp, e) @@ -209,5 +203,5 @@ fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg, tt: ~[ast::token_tree]) -> Parser { let trdr = lexer::new_tt_reader(sess.span_diagnostic, sess.interner, None, tt); - return Parser(sess, cfg, trdr as reader, parser::SOURCE_FILE) + return Parser(sess, cfg, trdr as reader) } diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs index 1873a34627052..78a47ec09c7b9 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,4 +1,4 @@ -use parser::{Parser, SOURCE_FILE}; +use parser::Parser; use attr::parser_attr; use ast_util::mk_sp; use codemap::span; @@ -67,7 +67,7 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option) debug!("found companion mod"); // XXX: Using a dummy span, but this code will go away soon let p0 = new_sub_parser_from_file(cx.sess, cx.cfg, - modpath, SOURCE_FILE, + modpath, ast_util::dummy_sp()); let inner_attrs = p0.parse_inner_attrs_and_next(); let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next); @@ -96,7 +96,7 @@ fn eval_src_mod(cx: ctx, prefix: &Path, id: ast::ident, }; let p0 = new_sub_parser_from_file(cx.sess, cx.cfg, - &full_path, SOURCE_FILE, sp); + &full_path, sp); let inner_attrs = p0.parse_inner_attrs_and_next(); let mod_attrs = vec::append(outer_attrs, inner_attrs.inner); let first_item_outer_attrs = inner_attrs.next; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9d71f83b28d50..f49447215fe3d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -70,10 +70,7 @@ use ast::{_mod, add, arg, arm, attribute, expr_vstore_fixed, expr_vstore_slice, expr_vstore_box, expr_vstore_uniq, TyFn, Onceness, Once, Many}; -export file_type; export Parser; -export CRATE_FILE; -export SOURCE_FILE; // FIXME (#3726): #ast expects to find this here but it's actually // defined in `parse` Fixing this will be easier when we have export @@ -92,8 +89,6 @@ enum restriction { RESTRICT_NO_BAR_OR_DOUBLEBAR_OP, } -enum file_type { CRATE_FILE, SOURCE_FILE, } - enum class_member { field_member(@struct_field), method_member(@method) @@ -180,7 +175,7 @@ pure fn maybe_append(+lhs: ~[attribute], rhs: Option<~[attribute]>) /* ident is handled by common.rs */ fn Parser(sess: parse_sess, cfg: ast::crate_cfg, - +rdr: reader, ftype: file_type) -> Parser { + +rdr: reader) -> Parser { let tok0 = rdr.next_token(); let span0 = tok0.sp; @@ -191,7 +186,6 @@ fn Parser(sess: parse_sess, cfg: ast::crate_cfg, interner: move interner, sess: sess, cfg: cfg, - file_type: ftype, token: tok0.tok, span: span0, last_span: span0, @@ -210,7 +204,6 @@ fn Parser(sess: parse_sess, cfg: ast::crate_cfg, struct Parser { sess: parse_sess, cfg: crate_cfg, - file_type: file_type, mut token: token::Token, mut span: span, mut last_span: span,