diff --git a/src/libcore/char.rs b/src/libcore/char.rs index b76571864e070..fb8cc274217d2 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -126,16 +126,19 @@ 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 +154,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/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/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/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/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 { 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. 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 120a11c0eba26..6476b1bb6d4f4 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -366,7 +366,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, @@ -586,7 +586,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 = @@ -595,7 +595,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/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 98c6688f03cd1..6c5a3d0470c78 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 0790208244e9a..2465017f545ce 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 21ad53371afd6..548018438ca3d 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 fd18aaaf58ec6..d4d1c8d3b2ecd 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}; @@ -229,8 +229,8 @@ fn create_file(cx: @crate_ctxt, full_path: ~str) -> @metadata { return mdval; } -fn line_from_span(cm: codemap::CodeMap, sp: span) -> uint { - codemap::lookup_char_pos(cm, sp.lo).line +fn line_from_span(cm: @codemap::CodeMap, sp: span) -> uint { + 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, @@ -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) ]; @@ -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,9 +712,9 @@ 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 scopedata = ~[lli32(loc.line as int), - lli32(loc.col as int), + let loc = cm.lookup_char_pos(s.lo); + let scopedata = ~[lli32(loc.line.to_int()), + lli32(loc.col.to_int()), blockmd.node, llnull()]; let dbgscope = llmdnode(scopedata); @@ -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/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 \ 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/rustc.rs b/src/librustc/rustc.rs index 63e632d16a36b..c959f12863d60 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/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index a9b67a7ff29c9..198b26c4ecc69 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -105,8 +105,9 @@ 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); - (fmt!("the %s at %u:%u", heading, lo.line, lo.col), Some(span)) + let lo = cx.sess.codemap.lookup_char_pos_adj(span.lo); + (fmt!("the %s at %u:%u", heading, + lo.line, lo.col.to_uint()), Some(span)) } } @@ -131,17 +132,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 +150,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 7b2c6fe5f0cbc..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; @@ -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/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/libstd/base64.rs b/src/libstd/base64.rs index 9bad4d3975003..5db71b12185bc 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -2,72 +2,74 @@ 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; + 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" + } } - - // 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 +82,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; - }; + i += 1u; + }; - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - r.push(((n ) & 0xFFu) as u8); + 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/getopts.rs b/src/libstd/getopts.rs index 8d77b88aba230..e5904b81d87d2 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] 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/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() { 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 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/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] { diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 75909273392f4..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,22 +151,95 @@ pub fn now() -> Tm { } /// Parses the time from the string according to the format string. -pub fn 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, - }; +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 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 { + /// 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". + */ + pure fn ctime() -> ~str { self.strftime(~"%c") } + + /// Formats the time according to the format string. + pure 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" + */ + pure 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" + */ + pure 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" + */ + pure 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 { fn match_str(s: &str, pos: uint, needle: &str) -> bool { let mut i = pos; for str::each(needle) |ch| { @@ -229,7 +302,7 @@ pub fn 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, ~[ @@ -540,19 +613,19 @@ pub fn 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); @@ -562,7 +635,7 @@ pub fn 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; } }, @@ -592,7 +665,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 +832,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]; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 148600d8d21e0..e9031a2890a06 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] @@ -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/ast_util.rs b/src/libsyntax/ast_util.rs index d3b879da7dd95..73a1c4b7530a0 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -1,7 +1,7 @@ -use codemap::span; +use codemap::{span, BytePos}; use ast::*; -pure fn spanned(lo: uint, hi: uint, +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: uint, hi: uint) -> span { - {lo: lo, hi: hi, expn_info: None} +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(0u, 0u); } +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 8c19814350ca7..da80e26b1afe9 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::BytePos; // 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: 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 4278e1f199a62..d291d9545eb6a 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -1,179 +1,144 @@ +/*! + +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, Deserializable, Serializer, Deserializer}; -export filename; -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; - -type filename = ~str; - -type 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 - } - pure fn ne(other: &file_pos) -> bool { !self.eq(other) } +trait Pos { + static pure fn from_uint(n: uint) -> self; + pure fn to_uint(&self) -> uint; } -/* 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), - fss_external({filename: ~str, line: uint, col: 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; -type filemap = - @{name: filename, substr: file_substr, src: @~str, - start_pos: file_pos, mut lines: ~[file_pos]}; +// XXX: Lots of boilerplate in these impls, but so far my attempts to fix +// have been unsuccessful -type CodeMap = @{files: DVec}; - -type loc = {file: filemap, line: uint, col: uint}; - -fn new_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}]}; +impl BytePos: Pos { + static pure fn from_uint(n: uint) -> BytePos { BytePos(n) } + pure fn to_uint(&self) -> uint { **self } } -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); +impl BytePos: cmp::Eq { + pure fn eq(other: &BytePos) -> bool { + *self == **other + } + pure fn ne(other: &BytePos) -> bool { !self.eq(other) } } -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); +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 } } -fn next_line(file: filemap, chpos: uint, byte_pos: uint) { - file.lines.push({ch: chpos, byte: byte_pos + file.start_pos.byte}); +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) } } -type lookup_fn = pure fn(file_pos) -> uint; - -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) - } - 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; } - } - return {fm: f, line: a}; +impl BytePos: to_bytes::IterBytes { + pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { + (*self).iter_bytes(lsb0, f) + } } -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])}; +impl CharPos: Pos { + static pure fn from_uint(n: uint) -> CharPos { CharPos(n) } + pure fn to_uint(&self) -> uint { **self } } -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); +impl CharPos: cmp::Eq { + pure fn eq(other: &CharPos) -> bool { + *self == **other + } + pure fn ne(other: &CharPos) -> bool { !self.eq(other) } } -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); +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 } } -fn lookup_char_pos_adj(map: CodeMap, pos: uint) - -> {filename: ~str, line: uint, col: uint, file: Option} -{ - 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} - } +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) } } -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, {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 +impl CharPos: to_bytes::IterBytes { + pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { + (*self).iter_bytes(lsb0, f) } } -enum expn_info_ { - expanded_from({call_site: span, - callie: {name: ~str, span: Option}}) +/** +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, + expn_info: Option<@ExpnInfo> } -type expn_info = Option<@expn_info_>; - -type span = {lo: uint, hi: uint, expn_info: expn_info}; impl span : cmp::Eq { pure fn eq(other: &span) -> bool { @@ -193,74 +158,308 @@ impl span: Deserializable { } } -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) +/// A source code location used for error reporting +pub struct Loc { + /// Information about the original source + file: @FileMap, + /// The (1-based) line number + line: uint, + /// The (0-based) column offset + col: CharPos } -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) +/// Extra information for tracking macro expansion of spans +pub enum ExpnInfo { + ExpandedFrom({call_site: span, + callie: {name: ~str, span: Option}}) } -type file_lines = {file: filemap, lines: ~[uint]}; +pub type FileName = ~str; -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 struct FileLines { + file: @FileMap, + lines: ~[uint] } -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: lo.file, lines: lines}; +pub enum FileSubstr { + pub FssNone, + pub FssInternal(span), + pub FssExternal({filename: ~str, line: uint, col: CharPos}) } -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) +/// 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, } -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} +/// 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 } -fn span_to_snippet(sp: span, cm: codemap::CodeMap) -> ~str { - let begin = lookup_byte_offset(cm, sp.lo); - let end = lookup_byte_offset(cm, sp.hi); - assert begin.fm.start_pos == end.fm.start_pos; - return str::slice(*begin.fm.src, begin.pos, end.pos); +pub impl FileMap { + 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] - self.start_pos; + let begin = begin.to_uint(); + 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 record_multibyte_char(&self, pos: BytePos, bytes: uint) { + assert bytes >=2 && bytes <= 4; + let mbc = MultiByteChar { + pos: pos, + bytes: bytes, + }; + self.multibyte_chars.push(mbc); + } } -fn get_snippet(cm: codemap::CodeMap, fidx: uint, lo: uint, hi: uint) -> ~str -{ - let fm = cm.files[fidx]; - return str::slice(*fm.src, lo, hi) +pub struct CodeMap { + files: DVec<@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 _|_) - fail; // ("asking for " + filename + " which we don't know about"); +pub impl CodeMap { + static pub fn new() -> CodeMap { + CodeMap { + files: DVec() + } + } + + /// 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 { + 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 filemap = @FileMap { + name: filename, substr: substr, src: src, + start_pos: BytePos(start_pos), + mut lines: ~[], + multibyte_chars: DVec() + }; + + self.files.push(filemap); + + return 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, + 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); + } + + 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); + match (loc.file.substr) { + FssNone => { + {filename: /* FIXME (#2543) */ copy loc.file.name, + line: loc.line, + col: loc.col, + file: Some(loc.file)} + } + FssInternal(sp) => { + self.lookup_char_pos_adj( + sp.lo + (pos - loc.file.start_pos)) + } + 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}, + file: None} + } + } + } + + pub fn adjust_span(&self, sp: span) -> span { + 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), + hi: s.lo + (sp.hi - line.fm.start_pos), + expn_info: sp.expn_info + }) + } + FssExternal(_) => 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.to_uint(), hi.line, hi.col.to_uint()) + } + + 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 { + 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 @FileLines {file: lo.file, lines: lines}; + } + + 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.to_uint(), end.pos.to_uint()); + } + + 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"); + } + +} + +priv impl CodeMap { + + 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 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.to_uint()) + } + + 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 f.lines[m] > pos { b = m; } else { a = m; } + } + return {fm: f, line: a}; + } + + 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]; + 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: line, + col: chpos - linechpos + }; + } + + 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.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; + 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/diagnostic.rs b/src/libsyntax/diagnostic.rs index 855b0ca3ef568..007100856ebc4 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,12 +189,12 @@ 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); - 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); @@ -205,8 +205,8 @@ fn emit(cmsp: Option<(codemap::CodeMap, span)>, msg: &str, lvl: level) { } } -fn highlight_lines(cm: codemap::CodeMap, sp: span, - lines: @codemap::file_lines) { +fn highlight_lines(cm: @codemap::CodeMap, sp: span, + lines: @codemap::FileLines) { let fm = lines.file; @@ -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; @@ -245,28 +245,28 @@ 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; } 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; + 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"); } } -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)); + |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/base.rs b/src/libsyntax/ext/base.rs index eb4026c08d90c..c4611074b14fe 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -1,7 +1,8 @@ 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: // @@ -139,15 +140,15 @@ 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(); - fn backtrace() -> 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); @@ -167,32 +168,34 @@ 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<@ExpnInfo>, 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() { } - fn backtrace() -> 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({ - call_site: {lo: cs.lo, hi: cs.hi, - expn_info: self.backtrace}, + Some(@ExpandedFrom({ + 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(@ExpandedFrom({ + call_site: span {expn_info: prev, _}, _ + })) => { self.backtrace = prev } _ => self.bug(~"tried to pop without a push") @@ -326,7 +329,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..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 { @@ -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.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/pipes/ast_builder.rs b/src/libsyntax/ext/pipes/ast_builder.rs index f03adb90f0bcf..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 { - {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 { - {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 af7ffaa73f5bf..888932e58e713 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, BytePos}; use fold::*; use visit::*; @@ -15,13 +16,13 @@ use io::*; use codemap::span; struct gather_item { - lo: uint, - hi: uint, + lo: BytePos, + hi: BytePos, e: @ast::expr, constr: ~str } -type aq_ctxt = @{lo: uint, gather: DVec}; +type aq_ctxt = @{lo: BytePos, 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: 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), @@ -204,13 +205,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, + (f, fname, codemap::FssInternal(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); @@ -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 = BytePos(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 += BytePos(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(), @@ -345,8 +347,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: CharPos(col)}) } // Local Variables: diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index db77166bfe316..3cca48c750891 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -1,7 +1,7 @@ use mod ast; use mod parse::token; -use codemap::span; +use codemap::{span, BytePos}; use ext::base::ext_ctxt; use token::*; @@ -20,6 +20,8 @@ pub mod rt { pub use ast::*; pub use parse::token::*; pub use parse::new_parser_from_tt; + pub use codemap::BytePos; + pub use codemap::span; } pub fn expand_quote_tokens(cx: ext_ctxt, @@ -92,7 +94,7 @@ fn mk_span(cx: ext_ctxt, qsp: span, sp: span) -> @ast::expr { let e_expn_info = match sp.expn_info { None => build::mk_path(cx, qsp, ids_ext(cx, ~[~"None"])), - Some(@codemap::expanded_from(cr)) => { + Some(@codemap::ExpandedFrom(cr)) => { let e_callee = build::mk_rec_e( cx, qsp, @@ -119,12 +121,16 @@ fn mk_span(cx: ext_ctxt, qsp: span, sp: span) -> @ast::expr { } }; - build::mk_rec_e(cx, qsp, + let span_path = ids_ext( + cx, ~[~"syntax", ~"ext", ~"quote", ~"rt", ~"span"]); + + build::mk_struct_e(cx, qsp, + span_path, ~[{ident: id_ext(cx, ~"lo"), - ex: build::mk_uint(cx, qsp, sp.lo) }, + ex: mk_bytepos(cx, qsp, sp.lo) }, {ident: id_ext(cx, ~"hi"), - ex: build::mk_uint(cx, qsp, sp.hi) }, + ex: mk_bytepos(cx, qsp, sp.hi) }, {ident: id_ext(cx, ~"expn_info"), ex: e_expn_info}]) @@ -143,6 +149,11 @@ fn mk_ident(cx: ext_ctxt, sp: span, ident: ast::ident) -> @ast::expr { ex: build::mk_uint(cx, sp, ident.repr) }]) } +fn mk_bytepos(cx: ext_ctxt, sp: span, bpos: BytePos) -> @ast::expr { + let path = ids_ext(cx, ~[~"syntax", ~"ext", ~"quote", ~"rt", ~"BytePos"]); + let arg = build::mk_uint(cx, sp, bpos.to_uint()); + build::mk_call(cx, sp, path, ~[arg]) +} fn mk_binop(cx: ext_ctxt, sp: span, bop: token::binop) -> @ast::expr { let name = match bop { 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 45c8fd76e4f2c..a105db0c08acb 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}; @@ -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,8 +24,8 @@ 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); - return mk_uint(cx, sp, loc.col); + let loc = cx.codemap().lookup_char_pos(sp.lo); + return mk_uint(cx, sp, loc.col.to_uint()); } /* file!(): expands to the current filename */ @@ -34,8 +34,8 @@ 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, _ }, _ } = - codemap::lookup_char_pos(cx.codemap(), sp.lo); + let Loc { file: @FileMap { name: filename, _ }, _ } = + cx.codemap().lookup_char_pos(sp.lo); return mk_uniq_str(cx, sp, filename); } @@ -58,9 +58,9 @@ 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)), sp); return p.parse_expr(); } @@ -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/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 76814a688f5b7..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; @@ -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::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 @@ -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: BytePos, }; 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: BytePos) -> matcher_pos { let mut match_idx_hi = 0u; for ms.each() |elt| { @@ -354,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 31bc375a76d56..56418989c497a 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -4,17 +4,18 @@ 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; 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; @@ -87,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/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/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.rs b/src/libsyntax/parse.rs index e38ee7ff03763..2e0b204df8d04 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,33 +21,31 @@ 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::{span, CodeMap, FileMap, CharPos, BytePos}; type parse_sess = @{ - cm: codemap::CodeMap, + 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: uint, - mut byte_pos: uint }; 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), interner: mk_ident_interner(), - 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, span_diagnostic: sh, interner: mk_ident_interner(), - mut chpos: 0u, mut byte_pos: 0u}; + }; } fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, @@ -63,15 +62,12 @@ 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_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(); 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; 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( @@ -88,75 +84,62 @@ 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_crate_parser_from_file(sess, cfg, input); let r = p.parse_crate_mod(cfg); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; 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::fss_none, 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(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; 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::fss_none, source); + let p = new_parser_from_source_str(sess, cfg, name, + codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; 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::fss_none, source); + let p = new_parser_from_source_str(sess, cfg, name, + 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; 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::fss_none, source); + let p = new_parser_from_source_str(sess, cfg, name, + 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; return r; } 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 { - 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(); - sess.chpos = rdr.chpos; - sess.byte_pos = sess.byte_pos + rdr.pos; move r } @@ -168,52 +151,57 @@ 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::file_substr, - source: @~str) -> (Parser, string_reader) { - let ftype = parser::SOURCE_FILE; - let filemap = codemap::new_filemap_w_substr - (name, ss, source, sess.chpos, sess.byte_pos); - sess.cm.files.push(filemap); +fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, + +name: ~str, +ss: codemap::FileSubstr, + source: @~str) -> Parser { + 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), srdr); + return Parser(sess, cfg, srdr as reader); } -fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg, - +name: ~str, +ss: codemap::file_substr, - source: @~str) -> Parser { - let (p, _) = new_parser_etc_from_source_str(sess, cfg, name, ss, source); - move p -} +fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, + 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)) -fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg, - path: &Path, ftype: parser::file_type) -> - (Parser, string_reader) { - let res = io::read_whole_file_str(path); - match res { - result::Ok(_) => { /* Continue. */ } - result::Err(e) => sess.span_diagnostic.handler().fatal(e) + } + result::Err(move e) => Err(move e) } - let src = @result::unwrap(res); - 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, - 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 +/// 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) -> Parser { + match new_parser_from_file(sess, cfg, path) { + 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, 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) + } + } } 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/attr.rs b/src/libsyntax/parse/attr.rs index 42101a431d6c6..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: uint) -> + 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: uint) -> + 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 4f265e1919c2e..2a8bbe3b6d862 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, CharPos}; export cmnt; export lit; @@ -27,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: BytePos}; fn is_doc_comment(s: ~str) -> bool { s.starts_with(~"///") || @@ -130,13 +131,13 @@ 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}); } 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); @@ -147,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; debug!("<<< shebang comment"); comments.push({ style: if code_to_the_left { trailing } else { isolated }, @@ -159,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; let mut lines: ~[~str] = ~[]; while rdr.curr == '/' && nextch(rdr) == '/' { let line = read_one_line_comment(rdr); @@ -180,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 { @@ -189,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); @@ -204,9 +209,9 @@ 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; let mut lines: ~[~str] = ~[]; - let mut col: uint = rdr.col; + let mut col: CharPos = rdr.col; bump(rdr); bump(rdr); @@ -279,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: BytePos}; fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, path: ~str, @@ -287,8 +292,10 @@ 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, codemap::new_filemap(path, src, 0u, 0u), itr); + let cm = CodeMap::new(); + let filemap = cm.new_filemap(path, src); + 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/common.rs b/src/libsyntax/parse/common.rs index 50c22c08f4f88..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 + 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/eval.rs b/src/libsyntax/parse/eval.rs index 56c9d4de9f3cd..78a47ec09c7b9 100644 --- a/src/libsyntax/parse/eval.rs +++ b/src/libsyntax/parse/eval.rs @@ -1,7 +1,10 @@ -use parser::{Parser, SOURCE_FILE}; +use parser::Parser; use attr::parser_attr; +use ast_util::mk_sp; +use codemap::span; export eval_crate_directives_to_mod; +export eval_src_mod; type ctx = @{sess: parse::parse_sess, @@ -62,12 +65,12 @@ 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); + // XXX: Using a dummy span, but this code will go away soon + let p0 = new_sub_parser_from_file(cx.sess, cx.cfg, + 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); - cx.sess.chpos = r0.chpos; - cx.sess.byte_pos = cx.sess.byte_pos + r0.pos; return (m0.view_items, m0.items, inner_attrs.inner); } else { return (~[], ~[], ~[]); @@ -81,32 +84,47 @@ 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], + 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 { + copy file_path + } else { + prefix.push_many(file_path.components) + }; + let p0 = + new_sub_parser_from_file(cx.sess, cx.cfg, + &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; + 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, r0) = - new_parser_etc_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, cdir.span); + let i = mk_item(cx, 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 - cx.sess.chpos = r0.chpos; - cx.sess.byte_pos = cx.sess.byte_pos + r0.pos; + m, vis, mod_attrs); items.push(i); } ast::cdir_dir_mod(vis, id, cdirs, attrs) => { @@ -129,7 +147,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/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 482813f3fd02e..5e174f7f34f43 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, CharPos, BytePos}; use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader, tt_next_token}; @@ -21,11 +21,15 @@ trait reader { type string_reader = @{ span_diagnostic: span_handler, src: @~str, - mut col: uint, - mut pos: uint, + // The absolute offset within the codemap of the next character to read + mut pos: BytePos, + // The absolute offset within the codemap of the last character read(curr) + mut last_pos: BytePos, + // The column of the next character to read + mut col: CharPos, + // The last character to be read mut curr: char, - mut chpos: uint, - filemap: codemap::filemap, + filemap: @codemap::FileMap, interner: @token::ident_interner, /* cached: */ mut peek_tok: token::Token, @@ -33,7 +37,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,27 +46,29 @@ 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 { + // 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: 0u, mut pos: 0u, mut curr: -1 as char, - mut chpos: filemap.start_pos.ch, + mut pos: filemap.start_pos, + mut last_pos: filemap.start_pos, + mut col: CharPos(0), + mut curr: initial_char, 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; - r.curr = next.ch; - } + mut peek_span: ast_util::dummy_sp()}; + bump(r); return r; } 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} } @@ -117,44 +123,55 @@ fn string_advance_token(&&r: string_reader) { if is_eof(r) { r.peek_tok = token::EOF; } else { - let start_chpos = r.chpos; + let start_bytepos = r.last_pos; 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_bytepos, r.last_pos); }; } -fn get_str_from(rdr: string_reader, start: uint) -> ~str unsafe { +fn byte_offset(rdr: string_reader) -> BytePos { + (rdr.pos - rdr.filemap.start_pos) +} + +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, + byte_offset(rdr).to_uint() - 1u); } fn bump(rdr: string_reader) { - if rdr.pos < (*rdr.src).len() { - rdr.col += 1u; - rdr.chpos += 1u; - if rdr.curr == '\n' { - codemap::next_line(rdr.filemap, rdr.chpos, rdr.pos); - rdr.col = 0u; - } - let next = str::char_range_at(*rdr.src, rdr.pos); - rdr.pos = next.next; + 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; + rdr.pos = rdr.pos + BytePos(byte_offset_diff); rdr.curr = next.ch; - } else { - if (rdr.curr != -1 as char) { - rdr.col += 1u; - rdr.chpos += 1u; - rdr.curr = -1 as char; + rdr.col += CharPos(1u); + if last_char == '\n' { + 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 { + rdr.curr = -1 as char; } } 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); + 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 +228,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_bpos = rdr.pos - BytePos(2u); let mut acc = ~"//"; while rdr.curr != '\n' && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -219,7 +236,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_bpos, rdr.pos) }); } else { while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); } @@ -232,10 +249,10 @@ 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); - if loc.line == 1u && loc.col == 0u { + 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); } @@ -250,7 +267,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_bpos = rdr.pos - BytePos(2u); let mut acc = ~"/*"; while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) { str::push_char(&mut acc, rdr.curr); @@ -264,7 +281,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_bpos, rdr.pos) }); } } else { @@ -590,7 +607,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 = byte_offset(rdr); bump(rdr); while rdr.curr != '"' { if is_eof(rdr) { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 49c3d38ce554b..f49447215fe3d 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, BytePos}; use util::interner::Interner; use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec}; use lexer::reader; @@ -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, @@ -244,7 +237,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: BytePos, +hi: BytePos) { self.token = next; self.span = mk_sp(lo, hi); } @@ -906,12 +899,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: 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: uint, hi: uint, 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)}), @@ -1141,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: 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)); @@ -1153,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: BytePos) -> @expr { match self.token { token::IDENT(_, _) => (), _ => self.fatal(~"expected a syntax expander name") @@ -2287,11 +2280,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: BytePos, 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: BytePos, s: blk_check_mode, +first_item_attrs: ~[attribute]) -> blk { let mut stmts = ~[]; let mut expr = None; @@ -2589,7 +2582,7 @@ impl Parser { return {ident: id, tps: ty_params}; } - fn mk_item(lo: uint, hi: uint, +ident: ident, + fn mk_item(+lo: BytePos, +hi: BytePos, +ident: ident, +node: item_, vis: visibility, +attrs: ~[attribute]) -> @item { return @{ident: ident, @@ -2958,13 +2951,28 @@ 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_span = self.span; 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_span); + (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 { @@ -3041,7 +3049,7 @@ impl Parser { items: items}; } - fn parse_item_foreign_mod(lo: uint, + fn parse_item_foreign_mod(lo: BytePos, visibility: visibility, attrs: ~[attribute], items_allowed: bool) @@ -3096,7 +3104,7 @@ impl Parser { }); } - fn parse_type_decl() -> {lo: uint, 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}; @@ -3360,7 +3368,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))); @@ -3425,9 +3433,8 @@ impl Parser { }; 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)); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 267f0e7d5f2a1..b4a5407c00249 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, BytePos}; use pp::{break_offset, word, printer, space, zerobreak, hardbreak, breaks}; use pp::{consistent, inconsistent, eof}; use ast::{required, provided}; @@ -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) { @@ -628,7 +628,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); } @@ -883,7 +883,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) { @@ -1895,15 +1895,15 @@ 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) { 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 mut next = cmnt.pos + 1u; + let span_line = cm.lookup_char_pos(span.hi); + let comment_line = cm.lookup_char_pos(cmnt.pos); + 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 { @@ -1979,7 +1979,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: BytePos) -> Option { match s.literals { Some(lits) => { while s.cur_lit < vec::len(lits) { @@ -1994,7 +1994,7 @@ fn next_lit(s: ps, pos: uint) -> Option { } } -fn maybe_print_comment(s: ps, pos: uint) { +fn maybe_print_comment(s: ps, pos: BytePos) { loop { match next_comment(s) { Some(cmnt) => { diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index d0a9154b00e27..718c916df46c4 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; 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(_) => () } } 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/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 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"; } } 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