test cases, cleanup

This commit is contained in:
John Clements 2013-01-30 09:56:33 -08:00
parent e343abd0ed
commit 53688addaa
55 changed files with 398 additions and 246 deletions

View File

@ -851,6 +851,9 @@ mod quux {
In this example, the module `quux` re-exports all of the public names defined in `foo`.
Also note that the paths contained in `use` items are relative to the crate root; so, in the previous
example, the use refers to `quux::foo::*`, and not simply to `foo::*`.
### Functions
A _function item_ defines a sequence of [statements](#statements) and an optional final [expression](#expressions), along with a name and a set of parameters.

View File

@ -346,12 +346,12 @@ pub mod test {
use syntax::ast_util;
pub fn make_crate_type_attr(+t: ~str) -> ast::attribute {
ast_util::respan(ast_util::dummy_sp(), ast::attribute_ {
ast_util::respan(codemap::dummy_sp(), ast::attribute_ {
style: ast::attr_outer,
value: ast_util::respan(ast_util::dummy_sp(),
value: ast_util::respan(codemap::dummy_sp(),
ast::meta_name_value(
~"crate_type",
ast_util::respan(ast_util::dummy_sp(),
ast_util::respan(codemap::dummy_sp(),
ast::lit_str(@t)))),
is_sugared_doc: false
})
@ -361,7 +361,7 @@ pub mod test {
let mut attrs = ~[];
if with_bin { attrs += ~[make_crate_type_attr(~"bin")]; }
if with_lib { attrs += ~[make_crate_type_attr(~"lib")]; }
@ast_util::respan(ast_util::dummy_sp(), ast::crate_ {
@ast_util::respan(codemap::dummy_sp(), ast::crate_ {
module: ast::_mod { view_items: ~[], items: ~[] },
attrs: attrs,
config: ~[]

View File

@ -17,6 +17,7 @@ use syntax::ast;
use syntax::ast_util::*;
use syntax::attr;
use syntax::codemap;
use syntax::codemap::dummy_sp;
use syntax::fold;
const CORE_VERSION: &static/str = "0.6";
@ -36,8 +37,8 @@ fn use_core(crate: @ast::crate) -> bool {
fn inject_libcore_ref(sess: Session,
crate: @ast::crate) -> @ast::crate {
fn spanned<T: Copy>(x: T) -> ast::spanned<T> {
ast::spanned { node: x, span: dummy_sp() }
fn spanned<T: Copy>(x: T) -> codemap::spanned<T> {
codemap::spanned { node: x, span: dummy_sp() }
}
let precursor = @fold::AstFoldFns {

View File

@ -13,6 +13,7 @@ use core::prelude::*;
use driver::session::Session;
use syntax::parse;
use syntax::ast;
use syntax::codemap::spanned;
use core::vec;
@ -34,7 +35,7 @@ pub fn inject_intrinsic(sess: Session, crate: @ast::crate) -> @ast::crate {
let items = vec::append(~[item], crate.node.module.items);
@ast::spanned {
@spanned {
node: ast::crate_ {
module: ast::_mod {
items: items,

View File

@ -21,7 +21,8 @@ use core::option;
use core::vec;
use syntax::ast_util::*;
use syntax::attr;
use syntax::codemap::span;
use syntax::codemap::{dummy_sp, span};
use syntax::codemap;
use syntax::fold;
use syntax::print::pprust;
use syntax::{ast, ast_util};
@ -237,8 +238,8 @@ fn mk_test_module(cx: test_ctxt) -> @ast::item {
return @item;
}
fn nospan<T: Copy>(t: T) -> ast::spanned<T> {
ast::spanned { node: t, span: dummy_sp() }
fn nospan<T: Copy>(t: T) -> codemap::spanned<T> {
codemap::spanned { node: t, span: dummy_sp() }
}
fn path_node(+ids: ~[ast::ident]) -> @ast::path {
@ -535,7 +536,7 @@ fn mk_main(cx: test_ctxt) -> @ast::item {
let body_: ast::blk_ =
default_block(~[], option::Some(test_main_call_expr),
cx.sess.next_node_id());
let body = ast::spanned { node: body_, span: dummy_sp() };
let body = codemap::spanned { node: body_, span: dummy_sp() };
let item_ = ast::item_fn(decl, ast::impure_fn, ~[], body);
let item = ast::item {

View File

@ -24,7 +24,7 @@ use core::either;
use core::option;
use core::vec;
use syntax::attr;
use syntax::codemap::span;
use syntax::codemap::{span, dummy_sp};
use syntax::diagnostic::span_handler;
use syntax::parse::token::ident_interner;
use syntax::print::pprust;
@ -294,7 +294,7 @@ fn resolve_crate_deps(e: env, cdata: @~[u8]) -> cstore::cnum_map {
// This is a new one so we've got to load it
// FIXME (#2404): Need better error reporting than just a bogus
// span.
let fake_span = ast_util::dummy_sp();
let fake_span = dummy_sp();
let local_cnum = resolve_crate(e, cname, cmetas,
/*bad*/copy dep.hash, fake_span);
cnum_map.insert(extrn_cnum, local_cnum);

View File

@ -26,7 +26,7 @@ use std::ebml;
use std::map::HashMap;
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::dummy_sp;
use syntax::codemap::dummy_sp;
use syntax::ast_util;
use syntax::diagnostic::expect;
use syntax::diagnostic::span_handler;

View File

@ -45,6 +45,7 @@ use syntax::diagnostic::span_handler;
use syntax::parse::token::ident_interner;
use syntax::print::pprust;
use syntax::{ast, ast_util};
use syntax::codemap;
// A function that takes a def_id relative to the crate being searched and
// returns a def_id relative to the compilation environment, i.e. if we hit a
@ -981,13 +982,13 @@ fn get_attributes(md: ebml::Doc) -> ~[ast::attribute] {
assert (vec::len(meta_items) == 1u);
let meta_item = meta_items[0];
attrs.push(
ast::spanned {
codemap::spanned {
node: ast::attribute_ {
style: ast::attr_outer,
value: /*bad*/copy *meta_item,
is_sugared_doc: false,
},
span: ast_util::dummy_sp()
span: codemap::dummy_sp()
});
};
}

View File

@ -26,8 +26,7 @@ use core::uint;
use core::vec;
use syntax::ast;
use syntax::ast::*;
use syntax::ast_util;
use syntax::ast_util::respan;
use syntax::codemap::{respan, dummy_sp};
use std::map::HashMap;
// Compact string representation for ty::t values. API ty_str &
@ -114,7 +113,7 @@ fn parse_path(st: @pstate) -> @ast::path {
':' => { next(st); next(st); }
c => {
if c == '(' {
return @ast::path { span: ast_util::dummy_sp(),
return @ast::path { span: dummy_sp(),
global: false,
idents: idents,
rp: None,

View File

@ -220,7 +220,7 @@ impl extended_decode_ctxt {
ast::def_id { crate: ast::local_crate, node: self.tr_id(did.node) }
}
fn tr_span(_span: span) -> span {
ast_util::dummy_sp() // FIXME (#1972): handle span properly
codemap::dummy_sp() // FIXME (#1972): handle span properly
}
}
@ -300,9 +300,9 @@ fn simplify_ast(ii: ast::inlined_item) -> ast::inlined_item {
let stmts_sans_items = do blk.stmts.filtered |stmt| {
match stmt.node {
ast::stmt_expr(_, _) | ast::stmt_semi(_, _) |
ast::stmt_decl(@ast::spanned { node: ast::decl_local(_),
ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_),
span: _}, _) => true,
ast::stmt_decl(@ast::spanned { node: ast::decl_item(_),
ast::stmt_decl(@codemap::spanned { node: ast::decl_item(_),
span: _}, _) => false,
ast::stmt_mac(*) => die!(~"unexpanded macro in astencode")
}
@ -336,7 +336,7 @@ fn simplify_ast(ii: ast::inlined_item) -> ast::inlined_item {
ast::ii_dtor(ref dtor, nm, ref tps, parent_id) => {
let dtor_body = fld.fold_block((*dtor).node.body);
ast::ii_dtor(
ast::spanned {
codemap::spanned {
node: ast::struct_dtor_ { body: dtor_body,
.. /*bad*/copy (*dtor).node },
.. (/*bad*/copy *dtor) },
@ -377,7 +377,7 @@ fn renumber_ast(xcx: extended_decode_ctxt, ii: ast::inlined_item)
let new_parent = xcx.tr_def_id(parent_id);
let new_self = fld.new_id((*dtor).node.self_id);
ast::ii_dtor(
ast::spanned {
codemap::spanned {
node: ast::struct_dtor_ { id: dtor_id,
attrs: dtor_attrs,
self_id: new_self,

View File

@ -20,6 +20,7 @@ use core::dvec::DVec;
use core::option;
use std::map::HashMap;
use syntax::ast::*;
use syntax::codemap;
use syntax::{visit, ast_util, ast_map};
pub fn check_crate(sess: Session,
@ -64,7 +65,10 @@ pub fn check_pat(p: @pat, &&_is_const: bool, v: visit::vt<bool>) {
fn is_str(e: @expr) -> bool {
match e.node {
expr_vstore(
@expr { node: expr_lit(@spanned { node: lit_str(_), _}), _ },
@expr { node: expr_lit(@codemap::spanned {
node: lit_str(_),
_}),
_ },
expr_vstore_uniq
) => true,
_ => false
@ -96,7 +100,7 @@ pub fn check_expr(sess: Session,
~"disallowed operator in constant expression");
return;
}
expr_lit(@spanned {node: lit_str(_), _}) => { }
expr_lit(@codemap::spanned {node: lit_str(_), _}) => { }
expr_binary(_, _, _) | expr_unary(_, _) => {
if method_map.contains_key(e.id) {
sess.span_err(e.span, ~"user-defined operators are not \
@ -183,7 +187,7 @@ pub fn check_expr(sess: Session,
}
}
match e.node {
expr_lit(@spanned {node: lit_int(v, t), _}) => {
expr_lit(@codemap::spanned {node: lit_int(v, t), _}) => {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
@ -191,7 +195,7 @@ pub fn check_expr(sess: Session,
}
}
}
expr_lit(@spanned {node: lit_uint(v, t), _}) => {
expr_lit(@codemap::spanned {node: lit_uint(v, t), _}) => {
if v > ast_util::uint_ty_max(
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
sess.span_err(e.span, ~"literal out of range for its type");

View File

@ -26,9 +26,8 @@ use core::vec;
use std::map::HashMap;
use std::sort;
use syntax::ast::*;
use syntax::ast_util::{variant_def_ids, dummy_sp, unguarded_pat, walk_pat};
use syntax::ast_util;
use syntax::codemap::span;
use syntax::ast_util::{variant_def_ids, unguarded_pat, walk_pat};
use syntax::codemap::{span, dummy_sp, spanned};
use syntax::print::pprust::pat_to_str;
use syntax::visit;
@ -464,7 +463,7 @@ pub fn ctor_arity(cx: @MatchCheckCtxt, ctor: ctor, ty: ty::t) -> uint {
}
pub fn wild() -> @pat {
@pat {id: 0, node: pat_wild, span: ast_util::dummy_sp()}
@pat {id: 0, node: pat_wild, span: dummy_sp()}
}
pub fn specialize(cx: @MatchCheckCtxt,

View File

@ -26,7 +26,7 @@ use core::str;
use core::vec;
use std::map::HashMap;
use syntax::ast::*;
use syntax::codemap::span;
use syntax::codemap::{span, spanned};
use syntax::print::pprust::expr_to_str;
use syntax::{visit, ast_util};

View File

@ -39,6 +39,7 @@ use std::oldsmallintmap;
use syntax::ast_util::{path_to_ident};
use syntax::attr;
use syntax::codemap::span;
use syntax::codemap;
use syntax::print::pprust::{expr_to_str, mode_to_str, pat_to_str};
use syntax::{ast, ast_util, visit};
@ -453,7 +454,7 @@ fn check_item_while_true(cx: ty::ctxt, it: @ast::item) {
match e.node {
ast::expr_while(cond, _) => {
match cond.node {
ast::expr_lit(@ast::spanned {
ast::expr_lit(@codemap::spanned {
node: ast::lit_bool(true), _}) =>
{
cx.sess.span_lint(

View File

@ -14,11 +14,10 @@ use middle::resolve;
use middle::ty;
use syntax::ast::*;
use syntax::ast_util;
use syntax::ast_util::{path_to_ident, respan, walk_pat};
use syntax::ast_util::{path_to_ident, walk_pat};
use syntax::fold;
use syntax::fold::*;
use syntax::codemap::span;
use syntax::codemap::{span, respan};
use std::map::HashMap;
pub type PatIdMap = HashMap<ident, node_id>;

View File

@ -58,7 +58,7 @@ use syntax::ast::{variant, view_item, view_item_import};
use syntax::ast::{view_item_use, view_path_glob, view_path_list};
use syntax::ast::{view_path_simple, visibility, anonymous, named, not};
use syntax::ast::{unsafe_fn};
use syntax::ast_util::{def_id_of_def, dummy_sp, local_def};
use syntax::ast_util::{def_id_of_def, local_def};
use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
use syntax::ast_util::{Privacy, Public, Private};
use syntax::ast_util::{variant_visibility_to_privacy, visibility_to_privacy};
@ -66,7 +66,7 @@ use syntax::attr::{attr_metas, contains_name, attrs_contains_name};
use syntax::parse::token::ident_interner;
use syntax::parse::token::special_idents;
use syntax::print::pprust::{pat_to_str, path_to_str};
use syntax::codemap::span;
use syntax::codemap::{span, dummy_sp};
use syntax::visit::{default_visitor, fk_method, mk_vt, Visitor, visit_block};
use syntax::visit::{visit_crate, visit_expr, visit_expr_opt, visit_fn};
use syntax::visit::{visit_foreign_item, visit_item, visit_method_helper};

View File

@ -1850,7 +1850,7 @@ pub fn trans_enum_variant(ccx: @crate_ctxt,
ty: varg.ty,
pat: ast_util::ident_to_pat(
ccx.tcx.sess.next_node_id(),
ast_util::dummy_sp(),
codemap::dummy_sp(),
special_idents::arg),
id: varg.id,
}
@ -1913,7 +1913,7 @@ pub fn trans_tuple_struct(ccx: @crate_ctxt,
is_mutbl: false,
ty: field.node.ty,
pat: ast_util::ident_to_pat(ccx.tcx.sess.next_node_id(),
ast_util::dummy_sp(),
codemap::dummy_sp(),
special_idents::arg),
id: field.node.id
}
@ -2044,7 +2044,7 @@ pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
} else {
for vec::each((*body).node.stmts) |stmt| {
match stmt.node {
ast::stmt_decl(@ast::spanned { node: ast::decl_item(i),
ast::stmt_decl(@codemap::spanned { node: ast::decl_item(i),
_ }, _) => {
trans_item(ccx, *i);
}

View File

@ -142,7 +142,7 @@ use util::ppaux::ty_to_str;
use syntax::print::pprust::{expr_to_str};
use syntax::ast;
use syntax::ast::spanned;
use syntax::codemap::spanned;
// Destinations
@ -552,7 +552,7 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
ast::expr_tup(ref args) => {
return trans_tup(bcx, *args, dest);
}
ast::expr_lit(@ast::spanned {node: ast::lit_str(s), _}) => {
ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), _}) => {
return tvec::trans_lit_str(bcx, expr, s, dest);
}
ast::expr_vstore(contents, ast::expr_vstore_slice) |

View File

@ -223,7 +223,7 @@ fn traverse_inline_body(cx: ctx, body: blk) {
fn traverse_all_resources_and_impls(cx: ctx, crate_mod: _mod) {
visit::visit_mod(
crate_mod, ast_util::dummy_sp(), 0, cx,
crate_mod, codemap::dummy_sp(), 0, cx,
visit::mk_vt(@visit::Visitor {
visit_expr: |_e, _cx, _v| { },
visit_item: |i, cx, v| {

View File

@ -28,7 +28,7 @@ use core::option::is_some;
use core::vec;
use std::map::HashMap;
use syntax::ast;
use syntax::ast_util::dummy_sp;
use syntax::codemap::dummy_sp;
use syntax::codemap::span;
use syntax::util::interner;

View File

@ -201,7 +201,7 @@ pub fn trans_slice_vstore(bcx: block,
// Handle the &"..." case:
match content_expr.node {
ast::expr_lit(@ast::spanned {node: ast::lit_str(s), span: _}) => {
ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), span: _}) => {
return trans_lit_str(bcx, content_expr, s, dest);
}
_ => {}
@ -296,7 +296,7 @@ pub fn trans_uniq_or_managed_vstore(bcx: block,
match heap {
heap_exchange => {
match content_expr.node {
ast::expr_lit(@ast::spanned {
ast::expr_lit(@codemap::spanned {
node: ast::lit_str(s), _
}) => {
let llptrval = C_cstr(bcx.ccx(), copy *s);
@ -354,7 +354,7 @@ pub fn write_content(bcx: block,
let _indenter = indenter();
match /*bad*/copy content_expr.node {
ast::expr_lit(@ast::spanned { node: ast::lit_str(s), _ }) => {
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
match dest {
Ignore => {
return bcx;
@ -461,7 +461,7 @@ pub fn elements_required(bcx: block, content_expr: @ast::expr) -> uint {
//! Figure out the number of elements we need to store this content
match /*bad*/copy content_expr.node {
ast::expr_lit(@ast::spanned { node: ast::lit_str(s), _ }) => {
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
s.len() + 1
},
ast::expr_vec(es, _) => es.len(),

View File

@ -47,6 +47,7 @@ use syntax::ast::*;
use syntax::ast_util::{is_local, local_def};
use syntax::ast_util;
use syntax::codemap::span;
use syntax::codemap;
use syntax::print::pprust;
use syntax::{ast, ast_map};
use syntax;
@ -3166,7 +3167,7 @@ pub fn expr_kind(tcx: ctxt,
ast::expr_block(*) |
ast::expr_copy(*) |
ast::expr_repeat(*) |
ast::expr_lit(@ast::spanned {node: lit_str(_), _}) |
ast::expr_lit(@codemap::spanned {node: lit_str(_), _}) |
ast::expr_vstore(_, ast::expr_vstore_slice) |
ast::expr_vstore(_, ast::expr_vstore_mut_slice) |
ast::expr_vstore(_, ast::expr_vstore_fixed(_)) |

View File

@ -108,7 +108,7 @@ use syntax::ast::{m_const, m_mutbl, m_imm};
use syntax::ast;
use syntax::ast_map;
use syntax::ast_map::node_id_to_str;
use syntax::ast_util::dummy_sp;
use syntax::codemap::dummy_sp;
use syntax::codemap::span;
pub fn lookup(

View File

@ -117,13 +117,13 @@ use core::vec;
use std::list::Nil;
use std::map::HashMap;
use std::map;
use syntax::ast::{provided, required, spanned, ty_i};
use syntax::ast::{provided, required, ty_i};
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util::{Private, Public, is_local, local_def, respan};
use syntax::ast_util::{Private, Public, is_local, local_def};
use syntax::ast_util::{visibility_to_privacy};
use syntax::ast_util;
use syntax::codemap::span;
use syntax::codemap::{span, spanned, respan};
use syntax::codemap;
use syntax::parse::token::special_idents;
use syntax::print::pprust;
@ -528,7 +528,7 @@ pub fn check_struct(ccx: @crate_ctxt, struct_def: @ast::struct_def,
def_id: local_def(id),
explicit_self:
spanned { node: ast::sty_by_ref,
span: ast_util::dummy_sp() } };
span: codemap::dummy_sp() } };
// typecheck the dtor
let dtor_dec = ast_util::dtor_dec();
check_bare_fn(ccx, &dtor_dec,
@ -1919,7 +1919,7 @@ pub fn check_expr_with_unifier(fcx: @fn_ctxt,
match /*bad*/copy expr.node {
ast::expr_vstore(ev, vst) => {
let typ = match /*bad*/copy ev.node {
ast::expr_lit(@ast::spanned { node: ast::lit_str(s), _ }) => {
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
let tt = ast_expr_vstore_to_vstore(fcx, ev, str::len(*s), vst);
ty::mk_estr(tcx, tt)
}
@ -2608,7 +2608,7 @@ pub fn check_block_with_expected(fcx0: @fn_ctxt,
for blk.node.stmts.each |s| {
if bot && !warned &&
match s.node {
ast::stmt_decl(@ast::spanned { node: ast::decl_local(_),
ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_),
_}, _) |
ast::stmt_expr(_, _) | ast::stmt_semi(_, _) => {
true

View File

@ -22,6 +22,7 @@ use util::ppaux;
use std::list::Cons;
use syntax::ast;
use syntax::codemap;
use syntax::print::pprust::{expr_to_str};
// Helper functions related to manipulating region types.
@ -57,7 +58,7 @@ pub fn replace_bound_regions_in_fn_sig(
let mut all_tys = ty::tys_in_fn_sig(fn_sig);
match self_info {
Some({explicit_self: ast::spanned { node: ast::sty_region(m),
Some({explicit_self: codemap::spanned { node: ast::sty_region(m),
_}, _}) => {
let region = ty::re_bound(ty::br_self);
let ty = ty::mk_rptr(tcx, region,

View File

@ -46,9 +46,9 @@ use syntax::ast::{trait_ref};
use syntax::ast;
use syntax::ast_map::node_item;
use syntax::ast_map;
use syntax::ast_util::{def_id_of_def, dummy_sp, local_def};
use syntax::ast_util::{def_id_of_def, local_def};
use syntax::attr;
use syntax::codemap::span;
use syntax::codemap::{span, dummy_sp};
use syntax::parse;
use syntax::visit::{default_simple_visitor, default_visitor};
use syntax::visit::{mk_simple_visitor, mk_vt, visit_crate, visit_item};

View File

@ -286,7 +286,8 @@ use syntax::ast::{ret_style, purity};
use syntax::ast::{m_const, m_imm, m_mutbl};
use syntax::ast::{unsafe_fn, impure_fn, pure_fn, extern_fn};
use syntax::ast;
use syntax::ast_util::dummy_sp;
use syntax::codemap::dummy_sp;
use syntax::codemap;
use syntax::ast_util;
use syntax::codemap::span;
@ -389,7 +390,7 @@ pub fn can_mk_subty(cx: @InferCtxt, a: ty::t, b: ty::t) -> ures {
debug!("can_mk_subty(%s <: %s)", a.inf_str(cx), b.inf_str(cx));
do indent {
do cx.probe {
cx.sub(true, ast_util::dummy_sp()).tys(a, b)
cx.sub(true, codemap::dummy_sp()).tys(a, b)
}
}.to_ures()
}
@ -429,7 +430,7 @@ pub fn can_mk_coercety(cx: @InferCtxt, a: ty::t, b: ty::t) -> ures {
debug!("can_mk_coercety(%s -> %s)", a.inf_str(cx), b.inf_str(cx));
do indent {
do cx.probe {
let span = ast_util::dummy_sp();
let span = codemap::dummy_sp();
Coerce(cx.combine_fields(true, span)).tys(a, b)
}
}.to_ures()

View File

@ -31,7 +31,7 @@ use std::getopts::{opt_present};
use std::getopts;
use std::getopts;
use std::map::HashMap;
use syntax::ast_util::dummy_sp;
use syntax::codemap::dummy_sp;
use syntax::parse::parse_crate_from_source_str;
use syntax::{ast, attr, parse};

View File

@ -70,10 +70,10 @@ use std::list;
use std::map::HashMap;
use std::map;
use std::oldsmallintmap;
use syntax::ast::{provided, required, spanned};
use syntax::ast::{provided, required};
use syntax::ast_map::node_id_to_str;
use syntax::ast_util::{local_def, respan, split_trait_methods};
use syntax::codemap::span;
use syntax::ast_util::{local_def, split_trait_methods};
use syntax::codemap::{span, spanned, respan};
use syntax::print::pprust::*;
use syntax::visit;
use syntax::{ast, ast_util, ast_map};

View File

@ -10,7 +10,7 @@
// The Rust abstract syntax tree.
use codemap::{span, FileName};
use codemap::{span, FileName, spanned};
use core::cast;
use core::cmp;
@ -21,10 +21,6 @@ use core::to_bytes;
use core::to_str::ToStr;
use std::serialize::{Encodable, Decodable, Encoder, Decoder};
#[auto_encode]
#[auto_decode]
pub struct spanned<T> { node: T, span: span }
/* can't import macros yet, so this is copied from token.rs. See its comment
* there. */
macro_rules! interner_key (

View File

@ -13,7 +13,7 @@ use core::prelude::*;
use ast::*;
use ast;
use ast_util;
use codemap::{span, BytePos};
use codemap::{span, BytePos, dummy_sp};
use parse::token;
use visit;
@ -24,28 +24,6 @@ use core::str;
use core::to_bytes;
use core::vec;
pub pure fn spanned<T>(+lo: BytePos, +hi: BytePos, +t: T) -> spanned<T> {
respan(mk_sp(lo, hi), move t)
}
pub pure fn respan<T>(sp: span, +t: T) -> spanned<T> {
spanned {node: t, span: sp}
}
pub pure fn dummy_spanned<T>(+t: T) -> spanned<T> {
respan(dummy_sp(), move t)
}
/* assuming that we're not in macro expansion */
pub 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
pub pure fn dummy_sp() -> span { return mk_sp(BytePos(0), BytePos(0)); }
pub pure fn path_name_i(idents: &[ident], intr: @token::ident_interner)
-> ~str {
// FIXME: Bad copies (#2543 -- same for everything else that says "bad")

View File

@ -13,7 +13,7 @@
use core::prelude::*;
use ast;
use ast_util::{spanned, dummy_spanned};
use codemap::{spanned, dummy_spanned};
use attr;
use codemap::BytePos;
use diagnostic::span_handler;

View File

@ -23,8 +23,6 @@ source code snippets, etc.
use core::prelude::*;
use ast_util;
use core::cmp;
use core::dvec::DVec;
use core::str;
@ -130,6 +128,10 @@ pub struct span {
expn_info: Option<@ExpnInfo>
}
#[auto_encode]
#[auto_decode]
pub struct spanned<T> { node: T, span: span }
pub impl span : cmp::Eq {
pure fn eq(&self, other: &span) -> bool {
return (*self).lo == (*other).lo && (*self).hi == (*other).hi;
@ -144,10 +146,32 @@ pub impl<S: Encoder> span: Encodable<S> {
pub impl<D: Decoder> span: Decodable<D> {
static fn decode(_d: &D) -> span {
ast_util::dummy_sp()
dummy_sp()
}
}
pub pure fn spanned<T>(+lo: BytePos, +hi: BytePos, +t: T) -> spanned<T> {
respan(mk_sp(lo, hi), move t)
}
pub pure fn respan<T>(sp: span, +t: T) -> spanned<T> {
spanned {node: t, span: sp}
}
pub pure fn dummy_spanned<T>(+t: T) -> spanned<T> {
respan(dummy_sp(), move t)
}
/* assuming that we're not in macro expansion */
pub 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
pub pure fn dummy_sp() -> span { return mk_sp(BytePos(0), BytePos(0)); }
/// A source code location used for error reporting
pub struct Loc {
/// Information about the original source
@ -158,6 +182,20 @@ pub struct Loc {
col: CharPos
}
/// A source code location used as the result of lookup_char_pos_adj
// Actually, *none* of the clients use the filename *or* file field;
// perhaps they should just be removed.
pub struct LocWithOpt {
filename: ~str,
line: uint,
col: CharPos,
file: Option<@FileMap>,
}
// used to be structural records. Better names, anyone?
pub struct FileMapAndLine {fm: @FileMap, line: uint}
pub struct FileMapAndBytePos {fm: @FileMap, pos: BytePos}
/// Extra information for tracking macro expansion of spans
pub enum ExpnInfo {
ExpandedFrom({call_site: span,
@ -204,10 +242,19 @@ pub struct FileMap {
}
pub impl FileMap {
// EFFECT: register a start-of-line offset in the
// table of line-beginnings.
// UNCHECKED INVARIANT: these offsets must be added in the right
// order and must be in the right places; there is shared knowledge
// about what ends a line between this file and parse.rs
fn next_line(&self, +pos: BytePos) {
// the new charpos must be > the last one (or it's the first one).
assert ((self.lines.len() == 0)
|| (self.lines[self.lines.len() - 1] < pos));
self.lines.push(pos);
}
// get a line from the list of pre-computed line-beginnings
pub fn get_line(&self, line: int) -> ~str {
unsafe {
let begin: BytePos = self.lines[line] - self.start_pos;
@ -279,27 +326,25 @@ pub impl CodeMap {
return self.lookup_pos(pos);
}
pub fn lookup_char_pos_adj(&self, +pos: BytePos)
-> {filename: ~str, line: uint, col: CharPos, file: Option<@FileMap>}
pub fn lookup_char_pos_adj(&self, +pos: BytePos) -> LocWithOpt
{
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(ref eloc) => {
{filename: /* FIXME (#2543) */ copy (*eloc).filename,
line: (*eloc).line + loc.line - 1u,
col: if loc.line == 1 {eloc.col + loc.col} else {loc.col},
file: None}
}
FssNone =>
LocWithOpt {
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(ref eloc) =>
LocWithOpt {
filename: /* FIXME (#2543) */ copy (*eloc).filename,
line: (*eloc).line + loc.line - 1u,
col: if loc.line == 1 {eloc.col + loc.col} else {loc.col},
file: None}
}
}
@ -319,7 +364,7 @@ pub impl CodeMap {
}
pub fn span_to_str(&self, sp: span) -> ~str {
if self.files.len() == 0 && sp == ast_util::dummy_sp() {
if self.files.len() == 0 && sp == dummy_sp() {
return ~"no-location";
}
@ -383,8 +428,7 @@ priv impl CodeMap {
return a;
}
fn lookup_line(&self, +pos: BytePos)
-> {fm: @FileMap, line: uint}
fn lookup_line(&self, pos: BytePos) -> FileMapAndLine
{
let idx = self.lookup_filemap_idx(pos);
let f = self.files[idx];
@ -394,11 +438,11 @@ priv impl CodeMap {
let m = (a + b) / 2u;
if f.lines[m] > pos { b = m; } else { a = m; }
}
return {fm: f, line: a};
return FileMapAndLine {fm: f, line: a};
}
fn lookup_pos(&self, +pos: BytePos) -> Loc {
let {fm: f, line: a} = self.lookup_line(pos);
let FileMapAndLine {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];
@ -424,11 +468,11 @@ priv impl CodeMap {
}
fn lookup_byte_offset(&self, +bpos: BytePos)
-> {fm: @FileMap, pos: BytePos} {
-> FileMapAndBytePos {
let idx = self.lookup_filemap_idx(bpos);
let fm = self.files[idx];
let offset = bpos - fm.start_pos;
return {fm: fm, pos: offset};
return FileMapAndBytePos {fm: fm, pos: offset};
}
// Converts an absolute BytePos to a CharPos relative to the file it is
@ -458,6 +502,36 @@ priv impl CodeMap {
}
}
#[cfg(test)]
mod test {
use super::*;
use util::testing::check_equal;
#[test]
fn t1 () {
let cm = CodeMap::new();
let fm = cm.new_filemap(~"blork.rs",@~"first line.\nsecond line");
fm.next_line(BytePos(0));
check_equal(&fm.get_line(0),&~"first line.");
// TESTING BROKEN BEHAVIOR:
fm.next_line(BytePos(10));
check_equal(&fm.get_line(1),&~".");
}
#[test]
#[should_fail]
fn t2 () {
let cm = CodeMap::new();
let fm = cm.new_filemap(~"blork.rs",@~"first line.\nsecond line");
// TESTING *REALLY* BROKEN BEHAVIOR:
fm.next_line(BytePos(0));
fm.next_line(BytePos(10));
fm.next_line(BytePos(2));
}
}
//
// Local Variables:
// mode: rust

View File

@ -311,7 +311,7 @@ priv impl ext_ctxt {
}
fn stmt(expr: @ast::expr) -> @ast::stmt {
@ast::spanned { node: ast::stmt_semi(expr, self.next_id()),
@codemap::spanned { node: ast::stmt_semi(expr, self.next_id()),
span: expr.span }
}
@ -322,7 +322,7 @@ priv impl ext_ctxt {
self.expr(
span,
ast::expr_lit(
@ast::spanned { node: ast::lit_str(s),
@codemap::spanned { node: ast::lit_str(s),
span: span})),
ast::expr_vstore_uniq))
}
@ -331,7 +331,7 @@ priv impl ext_ctxt {
self.expr(
span,
ast::expr_lit(
@ast::spanned { node: ast::lit_uint(i as u64, ast::ty_u),
@codemap::spanned { node: ast::lit_uint(i as u64, ast::ty_u),
span: span}))
}
@ -342,7 +342,7 @@ priv impl ext_ctxt {
}
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk {
ast::spanned {
codemap::spanned {
node: ast::blk_ {
view_items: ~[],
stmts: stmts,
@ -355,7 +355,7 @@ priv impl ext_ctxt {
}
fn expr_blk(expr: @ast::expr) -> ast::blk {
ast::spanned {
codemap::spanned {
node: ast::blk_ {
view_items: ~[],
stmts: ~[],
@ -593,7 +593,7 @@ fn mk_ser_method(
ident: cx.ident_of(~"encode"),
attrs: ~[],
tps: ~[],
self_ty: ast::spanned { node: ast::sty_region(ast::m_imm),
self_ty: codemap::spanned { node: ast::sty_region(ast::m_imm),
span: span },
purity: ast::impure_fn,
decl: ser_decl,
@ -651,7 +651,7 @@ fn mk_deser_method(
ident: cx.ident_of(~"decode"),
attrs: ~[],
tps: ~[],
self_ty: ast::spanned { node: ast::sty_static, span: span },
self_ty: codemap::spanned { node: ast::sty_static, span: span },
purity: ast::impure_fn,
decl: deser_decl,
body: deser_body,
@ -762,7 +762,7 @@ fn mk_struct_deser_impl(
]
);
ast::spanned {
codemap::spanned {
node: ast::field_ {
mutbl: field.mutbl,
ident: field.ident,

View File

@ -11,9 +11,8 @@
use core::prelude::*;
use ast;
use ast_util::dummy_sp;
use codemap;
use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom};
use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom, dummy_sp};
use diagnostic::span_handler;
use ext;
use parse;

View File

@ -32,7 +32,7 @@ pub fn mk_expr(cx: ext_ctxt,
}
pub fn mk_lit(cx: ext_ctxt, sp: span, lit: ast::lit_) -> @ast::expr {
let sp_lit = @ast::spanned { node: lit, span: sp };
let sp_lit = @codemap::spanned { node: lit, span: sp };
mk_expr(cx, sp, ast::expr_lit(sp_lit))
}
pub fn mk_int(cx: ext_ctxt, sp: span, i: int) -> @ast::expr {
@ -149,7 +149,7 @@ pub fn mk_uniq_str(cx: ext_ctxt, sp: span, s: ~str) -> @ast::expr {
}
pub fn mk_field(sp: span, f: &{ident: ast::ident, ex: @ast::expr})
-> ast::field {
ast::spanned {
codemap::spanned {
node: ast::field_ { mutbl: ast::m_imm, ident: f.ident, expr: f.ex },
span: sp,
}
@ -188,7 +188,7 @@ pub fn mk_global_struct_e(cx: ext_ctxt,
pub fn mk_glob_use(cx: ext_ctxt,
sp: span,
path: ~[ast::ident]) -> @ast::view_item {
let glob = @ast::spanned {
let glob = @codemap::spanned {
node: ast::view_path_glob(mk_raw_path(sp, path), cx.next_id()),
span: sp,
};
@ -209,7 +209,7 @@ pub fn mk_local(cx: ext_ctxt, sp: span, mutbl: bool,
span: sp,
};
let ty = @ast::Ty { id: cx.next_id(), node: ast::ty_infer, span: sp };
let local = @ast::spanned {
let local = @codemap::spanned {
node: ast::local_ {
is_mutbl: mutbl,
ty: ty,
@ -219,14 +219,14 @@ pub fn mk_local(cx: ext_ctxt, sp: span, mutbl: bool,
},
span: sp,
};
let decl = ast::spanned {node: ast::decl_local(~[local]), span: sp};
@ast::spanned { node: ast::stmt_decl(@decl, cx.next_id()), span: sp }
let decl = codemap::spanned {node: ast::decl_local(~[local]), span: sp};
@codemap::spanned { node: ast::stmt_decl(@decl, cx.next_id()), span: sp }
}
pub fn mk_block(cx: ext_ctxt, span: span,
view_items: ~[@ast::view_item],
stmts: ~[@ast::stmt],
expr: Option<@ast::expr>) -> @ast::expr {
let blk = ast::spanned {
let blk = codemap::spanned {
node: ast::blk_ {
view_items: view_items,
stmts: stmts,
@ -242,7 +242,7 @@ pub fn mk_block_(cx: ext_ctxt,
span: span,
+stmts: ~[@ast::stmt])
-> ast::blk {
ast::spanned {
codemap::spanned {
node: ast::blk_ {
view_items: ~[],
stmts: stmts,
@ -257,7 +257,7 @@ pub fn mk_simple_block(cx: ext_ctxt,
span: span,
expr: @ast::expr)
-> ast::blk {
ast::spanned {
codemap::spanned {
node: ast::blk_ {
view_items: ~[],
stmts: ~[],
@ -307,13 +307,14 @@ pub fn mk_pat_struct(cx: ext_ctxt,
mk_pat(cx, span, move pat)
}
pub fn mk_bool(cx: ext_ctxt, span: span, value: bool) -> @ast::expr {
let lit_expr = ast::expr_lit(@ast::spanned { node: ast::lit_bool(value),
span: span });
let lit_expr = ast::expr_lit(@codemap::spanned {
node: ast::lit_bool(value),
span: span });
build::mk_expr(cx, span, move lit_expr)
}
pub fn mk_stmt(cx: ext_ctxt, span: span, expr: @ast::expr) -> @ast::stmt {
let stmt_ = ast::stmt_semi(expr, cx.next_id());
@ast::spanned { node: move stmt_, span: span }
@codemap::spanned { node: move stmt_, span: span }
}
pub fn mk_ty_path(cx: ext_ctxt,
span: span,

View File

@ -18,12 +18,12 @@ use ast::{TraitTyParamBound, Ty, and, bind_by_ref, binop, deref, enum_def};
use ast::{enum_variant_kind, expr, expr_match, ident, item, item_};
use ast::{item_enum, item_impl, item_struct, m_imm, meta_item, method};
use ast::{named_field, or, pat, pat_ident, pat_wild, public, pure_fn};
use ast::{re_anon, spanned, stmt, struct_def, struct_variant_kind};
use ast::{re_anon, stmt, struct_def, struct_variant_kind};
use ast::{sty_by_ref, sty_region, tuple_variant_kind, ty_nil, ty_param};
use ast::{ty_param_bound, ty_path, ty_rptr, unnamed_field, variant};
use ext::base::ext_ctxt;
use ext::build;
use codemap::span;
use codemap::{span, spanned};
use parse::token::special_idents::clownshoes_extensions;
use core::dvec;

View File

@ -154,7 +154,7 @@ pub fn expand_item_mac(exts: HashMap<~str, SyntaxExtension>,
fld: ast_fold) -> Option<@ast::item> {
let (pth, tts) = match it.node {
item_mac(ast::spanned { node: mac_invoc_tt(pth, ref tts), _}) => {
item_mac(codemap::spanned { node: mac_invoc_tt(pth, ref tts), _}) => {
(pth, (*tts))
}
_ => cx.span_bug(it.span, ~"invalid item macro invocation")
@ -234,7 +234,7 @@ pub fn expand_stmt(exts: HashMap<~str, SyntaxExtension>, cx: ext_ctxt,
{call_site: sp, callie: {name: *extname, span: exp_sp}}));
let expanded = match exp(cx, mac.span, tts) {
MRExpr(e) =>
@ast::spanned { node: stmt_expr(e, cx.next_id()),
@codemap::spanned { node: stmt_expr(e, cx.next_id()),
span: e.span},
MRAny(_,_,stmt_mkr) => stmt_mkr(),
_ => cx.span_fatal(

View File

@ -17,10 +17,11 @@ use core::prelude::*;
use ast::{ident, node_id};
use ast;
use ast_util::{ident_to_path, respan, dummy_sp};
use ast_util::{ident_to_path};
use ast_util;
use attr;
use codemap::span;
use codemap::{span, respan, dummy_sp};
use codemap;
use ext::base::{ext_ctxt, mk_ctxt};
use ext::quote::rt::*;
@ -310,7 +311,7 @@ pub impl ext_ctxt: ext_ctxt_ast_builder {
// XXX: Total hack: import `core::kinds::Owned` to work around a
// parser bug whereby `fn f<T: ::kinds::Owned>` doesn't parse.
let vi = ast::view_item_import(~[
@ast::spanned {
@codemap::spanned {
node: ast::view_path_simple(
self.ident_of(~"Owned"),
path(
@ -319,19 +320,19 @@ pub impl ext_ctxt: ext_ctxt_ast_builder {
self.ident_of(~"kinds"),
self.ident_of(~"Owned")
],
ast_util::dummy_sp()
codemap::dummy_sp()
),
ast::type_value_ns,
self.next_id()
),
span: ast_util::dummy_sp()
span: codemap::dummy_sp()
}
]);
let vi = @ast::view_item {
node: vi,
attrs: ~[],
vis: ast::private,
span: ast_util::dummy_sp()
span: codemap::dummy_sp()
};
self.item(

View File

@ -11,7 +11,7 @@
// A protocol compiler for Rust.
use ast::ident;
use ast_util::dummy_sp;
use codemap::dummy_sp;
use ext::base::ext_ctxt;
use ext::pipes::ast_builder::{append_types, ext_ctxt_ast_builder, path};
use ext::pipes::ast_builder::{path_global};

View File

@ -10,8 +10,7 @@
// Earley-like parser for macros.
use ast::{matcher, match_tok, match_seq, match_nonterminal, ident};
use ast_util::mk_sp;
use codemap::BytePos;
use codemap::{BytePos, mk_sp};
use codemap;
use parse::common::*; //resolve bug?
use parse::lexer::*; //resolve bug?
@ -189,13 +188,13 @@ pub fn nameize(p_s: parse_sess, ms: ~[matcher], res: ~[@named_match])
fn n_rec(p_s: parse_sess, m: matcher, res: ~[@named_match],
ret_val: HashMap<ident, @named_match>) {
match m {
ast::spanned {node: match_tok(_), _} => (),
ast::spanned {node: match_seq(ref more_ms, _, _, _, _), _} => {
codemap::spanned {node: match_tok(_), _} => (),
codemap::spanned {node: match_seq(ref more_ms, _, _, _, _), _} => {
for (*more_ms).each() |next_m| {
n_rec(p_s, *next_m, res, ret_val)
};
}
ast::spanned {
codemap::spanned {
node: match_nonterminal(bind_name, _, idx), span: sp
} => {
if ret_val.contains_key(bind_name) {
@ -239,7 +238,7 @@ pub fn parse(sess: parse_sess,
let mut next_eis = ~[]; // or proceed normally
let mut eof_eis = ~[];
let {tok: tok, sp: sp} = rdr.peek();
let TokenAndSpan {tok: tok, sp: sp} = rdr.peek();
/* we append new items to this while we go */
while cur_eis.len() > 0u { /* for each Earley Item */

View File

@ -13,8 +13,7 @@ use core::prelude::*;
use ast::{ident, matcher_, matcher, match_tok, match_nonterminal, match_seq};
use ast::{tt_delim};
use ast;
use ast_util::dummy_sp;
use codemap::span;
use codemap::{span, spanned, dummy_sp};
use ext::base::{ext_ctxt, MacResult, MRAny, MRDef, MacroDef, NormalTT};
use ext::base;
use ext::tt::macro_parser::{error};
@ -33,7 +32,7 @@ pub fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
arg: ~[ast::token_tree]) -> base::MacResult {
// these spans won't matter, anyways
fn ms(m: matcher_) -> matcher {
ast::spanned { node: m, span: dummy_sp() }
spanned { node: m, span: dummy_sp() }
}
let lhs_nm = cx.parse_sess().interner.gensym(@~"lhs");

View File

@ -13,10 +13,11 @@ use core::prelude::*;
use ast;
use ast::{token_tree, tt_delim, tt_tok, tt_seq, tt_nonterminal,ident};
use ast_util;
use codemap::span;
use codemap::{span, dummy_sp};
use diagnostic::span_handler;
use ext::tt::macro_parser::{named_match, matched_seq, matched_nonterminal};
use parse::token::{EOF, INTERPOLATED, IDENT, Token, nt_ident, ident_interner};
use parse::lexer::TokenAndSpan;
use core::option;
use core::vec;
@ -69,7 +70,7 @@ pub 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::dummy_sp()
mut cur_span: dummy_sp()
};
tt_next_token(r); /* get cur_tok and cur_span set up */
return r;
@ -149,8 +150,8 @@ fn lockstep_iter_size(t: token_tree, r: tt_reader) -> lis {
}
pub fn tt_next_token(&&r: tt_reader) -> {tok: Token, sp: span} {
let ret_val = { tok: r.cur_tok, sp: r.cur_span };
pub fn tt_next_token(&&r: tt_reader) -> TokenAndSpan {
let ret_val = TokenAndSpan { tok: r.cur_tok, sp: r.cur_span };
while r.cur.idx >= r.cur.readme.len() {
/* done with this set; pop or repeat? */
if ! r.cur.dotdotdoted

View File

@ -11,7 +11,7 @@
use core::prelude::*;
use ast;
use ast_util::spanned;
use codemap::spanned;
use codemap::BytePos;
use parse::common::*; //resolve bug?
use parse::token;

View File

@ -13,6 +13,7 @@
*/
use ast;
use codemap;
use ast_util::operator_prec;
pub fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
@ -31,7 +32,8 @@ pub fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
pub fn expr_is_simple_block(e: @ast::expr) -> bool {
match e.node {
ast::expr_block(
ast::spanned { node: ast::blk_ { rules: ast::default_blk, _ }, _ }
codemap::spanned {
node: ast::blk_ { rules: ast::default_blk, _ }, _ }
) => true,
_ => false
}

View File

@ -14,7 +14,7 @@ use ast;
use codemap::{BytePos, CharPos, CodeMap, FileMap, Pos};
use diagnostic;
use parse::lexer::{is_whitespace, get_str_from, reader};
use parse::lexer::{string_reader, bump, is_eof, nextch};
use parse::lexer::{string_reader, bump, is_eof, nextch, TokenAndSpan};
use parse::lexer;
use parse::token;
use parse;
@ -334,7 +334,7 @@ pub fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler,
let bstart = rdr.pos;
rdr.next_token();
//discard, and look ahead; we're working with internal state
let {tok: tok, sp: sp} = rdr.peek();
let TokenAndSpan {tok: tok, sp: sp} = rdr.peek();
if token::is_lit(tok) {
let s = get_str_from(rdr, bstart);
literals.push({lit: s, pos: sp.lo});

View File

@ -11,8 +11,7 @@
use core::prelude::*;
use ast;
use ast_util::spanned;
use codemap::BytePos;
use codemap::{BytePos, spanned};
use parse::lexer::reader;
use parse::parser::Parser;
use parse::token;
@ -190,7 +189,9 @@ pub impl Parser {
if self.token == token::GT {
self.bump();
} else if self.token == token::BINOP(token::SHR) {
self.swap(token::GT, self.span.lo + BytePos(1u), self.span.hi);
self.replace_token(token::GT,
self.span.lo + BytePos(1u),
self.span.hi);
} else {
let mut s: ~str = ~"expected `";
s += token_to_str(self.reader, token::GT);
@ -229,7 +230,7 @@ pub impl Parser {
}
fn parse_seq_lt_gt<T: Copy>(sep: Option<token::Token>,
f: fn(Parser) -> T) -> ast::spanned<~[T]> {
f: fn(Parser) -> T) -> spanned<~[T]> {
let lo = self.span.lo;
self.expect(token::LT);
let result = self.parse_seq_to_before_gt::<T>(sep, f);
@ -277,7 +278,7 @@ pub impl Parser {
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
fn parse_seq<T: Copy>(bra: token::Token, ket: token::Token, sep: seq_sep,
f: fn(Parser) -> T) -> ast::spanned<~[T]> {
f: fn(Parser) -> T) -> spanned<~[T]> {
let lo = self.span.lo;
self.expect(bra);
let result = self.parse_seq_to_before_end::<T>(ket, sep, f);

View File

@ -10,8 +10,7 @@
use parser::Parser;
use attr::parser_attr;
use ast_util::mk_sp;
use codemap::span;
use codemap::{span, mk_sp};
type ctx =
@{sess: parse::parse_sess,
@ -75,7 +74,7 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option<Path>)
// 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());
codemap::dummy_sp());
let inner_attrs = p0.parse_inner_attrs_and_next();
let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next);
return (m0.view_items, m0.items, inner_attrs.inner);

View File

@ -30,14 +30,17 @@ use std;
pub trait reader {
fn is_eof() -> bool;
fn next_token() -> {tok: token::Token, sp: span};
fn next_token() -> TokenAndSpan;
fn fatal(~str) -> !;
fn span_diag() -> span_handler;
pure fn interner() -> @token::ident_interner;
fn peek() -> {tok: token::Token, sp: span};
fn peek() -> TokenAndSpan;
fn dup() -> reader;
}
#[deriving_eq]
pub struct TokenAndSpan {tok: token::Token, sp: span}
pub type string_reader = @{
span_diagnostic: span_handler,
src: @~str,
@ -79,11 +82,14 @@ pub fn new_low_level_string_reader(span_diagnostic: span_handler,
filemap: filemap, interner: itr,
/* dummy values; not read */
mut peek_tok: token::EOF,
mut peek_span: ast_util::dummy_sp()};
mut peek_span: codemap::dummy_sp()};
bump(r);
return r;
}
// duplicating the string reader is probably a bad idea, in
// that using them will cause interleaved pushes of line
// offsets to the underlying filemap...
fn dup_string_reader(&&r: string_reader) -> string_reader {
@{span_diagnostic: r.span_diagnostic, src: r.src,
mut pos: r.pos,
@ -95,8 +101,9 @@ fn dup_string_reader(&&r: string_reader) -> string_reader {
impl string_reader: reader {
fn is_eof() -> bool { is_eof(self) }
fn next_token() -> {tok: token::Token, sp: span} {
let ret_val = {tok: self.peek_tok, sp: self.peek_span};
// return the next token. EFFECT: advances the string_reader.
fn next_token() -> TokenAndSpan {
let ret_val = TokenAndSpan {tok: self.peek_tok, sp: self.peek_span};
string_advance_token(self);
return ret_val;
}
@ -105,15 +112,15 @@ impl string_reader: reader {
}
fn span_diag() -> span_handler { self.span_diagnostic }
pure fn interner() -> @token::ident_interner { self.interner }
fn peek() -> {tok: token::Token, sp: span} {
{tok: self.peek_tok, sp: self.peek_span}
fn peek() -> TokenAndSpan {
TokenAndSpan {tok: self.peek_tok, sp: self.peek_span}
}
fn dup() -> reader { dup_string_reader(self) as reader }
}
pub impl tt_reader: reader {
fn is_eof() -> bool { self.cur_tok == token::EOF }
fn next_token() -> {tok: token::Token, sp: span} {
fn next_token() -> TokenAndSpan {
/* weird resolve bug: if the following `if`, or any of its
statements are removed, we get resolution errors */
if false {
@ -127,27 +134,29 @@ pub impl tt_reader: reader {
}
fn span_diag() -> span_handler { self.sp_diag }
pure fn interner() -> @token::ident_interner { self.interner }
fn peek() -> {tok: token::Token, sp: span} {
{ tok: self.cur_tok, sp: self.cur_span }
fn peek() -> TokenAndSpan {
TokenAndSpan { tok: self.cur_tok, sp: self.cur_span }
}
fn dup() -> reader { dup_tt_reader(self) as reader }
}
// EFFECT: advance peek_tok and peek_span to refer to the next token.
fn string_advance_token(&&r: string_reader) {
for consume_whitespace_and_comments(r).each |comment| {
r.peek_tok = comment.tok;
r.peek_span = comment.sp;
return;
match (consume_whitespace_and_comments(r)) {
Some(comment) => {
r.peek_tok = comment.tok;
r.peek_span = comment.sp;
},
None => {
if is_eof(r) {
r.peek_tok = token::EOF;
} else {
let start_bytepos = r.last_pos;
r.peek_tok = next_token_inner(r);
r.peek_span = codemap::mk_sp(start_bytepos, r.last_pos);
};
}
}
if is_eof(r) {
r.peek_tok = token::EOF;
} else {
let start_bytepos = r.last_pos;
r.peek_tok = next_token_inner(r);
r.peek_span = ast_util::mk_sp(start_bytepos, r.last_pos);
};
}
fn byte_offset(rdr: string_reader) -> BytePos {
@ -163,6 +172,8 @@ pub fn get_str_from(rdr: string_reader, start: BytePos) -> ~str {
}
}
// EFFECT: advance the StringReader by one character. If a newline is
// discovered, add it to the FileMap's list of line start offsets.
pub fn bump(rdr: string_reader) {
rdr.last_pos = rdr.pos;
let current_byte_offset = byte_offset(rdr).to_uint();;
@ -233,16 +244,19 @@ fn is_hex_digit(c: char) -> bool {
fn is_bin_digit(c: char) -> bool { return c == '0' || c == '1'; }
// might return a sugared-doc-attr
// EFFECT: eats whitespace and comments.
// returns a Some(sugared-doc-attr) if one exists, None otherwise.
fn consume_whitespace_and_comments(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
-> Option<TokenAndSpan> {
while is_whitespace(rdr.curr) { bump(rdr); }
return consume_any_line_comment(rdr);
}
// might return a sugared-doc-attr
// PRECONDITION: rdr.curr is not whitespace
// EFFECT: eats any kind of comment.
// returns a Some(sugared-doc-attr) if one exists, None otherwise
fn consume_any_line_comment(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
-> Option<TokenAndSpan> {
if rdr.curr == '/' {
match nextch(rdr) {
'/' => {
@ -256,9 +270,9 @@ fn consume_any_line_comment(rdr: string_reader)
str::push_char(&mut acc, rdr.curr);
bump(rdr);
}
return Some({
return Some(TokenAndSpan{
tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
sp: ast_util::mk_sp(start_bpos, rdr.pos)
sp: codemap::mk_sp(start_bpos, rdr.pos)
});
} else {
while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); }
@ -285,7 +299,7 @@ fn consume_any_line_comment(rdr: string_reader)
// might return a sugared-doc-attr
fn consume_block_comment(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
-> Option<TokenAndSpan> {
// block comments starting with "/**" or "/*!" are doc-comments
if rdr.curr == '*' || rdr.curr == '!' {
@ -301,9 +315,9 @@ fn consume_block_comment(rdr: string_reader)
acc += ~"*/";
bump(rdr);
bump(rdr);
return Some({
return Some(TokenAndSpan{
tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
sp: ast_util::mk_sp(start_bpos, rdr.pos)
sp: codemap::mk_sp(start_bpos, rdr.pos)
});
}
} else {
@ -702,6 +716,41 @@ fn consume_whitespace(rdr: string_reader) {
while is_whitespace(rdr.curr) && !is_eof(rdr) { bump(rdr); }
}
#[cfg(test)]
pub mod test {
use super::*;
use util::interner;
use diagnostic;
use util::testing::{check_equal, check_equal_ptr};
#[test] fn t1 () {
let teststr =
@~"/* my source file */
fn main() { io::println(~\"zebra\"); }\n";
let cm = CodeMap::new();
let fm = cm.new_filemap(~"zebra.rs",teststr);
let ident_interner = token::mk_ident_interner(); // interner::mk();
let id = ident_interner.intern(@~"fn");
let span_handler =
diagnostic::mk_span_handler(diagnostic::mk_handler(None),@cm);
let string_reader = new_string_reader(span_handler,fm,ident_interner);
let tok1 = string_reader.next_token();
let tok2 = TokenAndSpan{
tok:token::IDENT(id, false),
sp:span {lo:BytePos(21),hi:BytePos(23),expn_info: None}};
check_equal (tok1,tok2);
// the 'main' id is already read:
check_equal (string_reader.last_pos,BytePos(28));
// read another token:
let tok3 = string_reader.next_token();
let tok4 = TokenAndSpan{
tok:token::IDENT(ident_interner.intern (@~"main"), false),
sp:span {lo:BytePos(24),hi:BytePos(28),expn_info: None}};
check_equal (tok3,tok4);
// the lparen is already read:
check_equal (string_reader.last_pos,BytePos(29))
}
}
//
// Local Variables:

View File

@ -10,6 +10,7 @@
//! The main parser interface
use ast::node_id;
use ast;
use codemap::{span, CodeMap, FileMap, CharPos, BytePos};
@ -33,6 +34,7 @@ pub mod token;
pub mod comments;
pub mod attr;
/// Common routines shared by parser mods
pub mod common;
@ -215,3 +217,4 @@ pub fn new_parser_from_tts(sess: parse_sess, cfg: ast::crate_cfg,
None, tts);
return Parser(sess, cfg, trdr as reader)
}

View File

@ -21,8 +21,7 @@ use core::prelude::*;
use ast::{expr, expr_lit, lit_nil};
use ast;
use ast_util::{respan};
use codemap::span;
use codemap::{span, respan};
use parse::parser::Parser;
use parse::token::Token;
use parse::token;

View File

@ -55,15 +55,16 @@ use ast::{view_path, view_path_glob, view_path_list, view_path_simple};
use ast::{visibility, vstore, vstore_box, vstore_fixed, vstore_slice};
use ast::{vstore_uniq};
use ast;
use ast_util::{spanned, respan, mk_sp, ident_to_path, operator_prec};
use ast_util::{ident_to_path, operator_prec};
use ast_util;
use classify;
use codemap::{span,FssNone, BytePos};
use codemap::{span,FssNone, BytePos, spanned, respan, mk_sp};
use codemap;
use parse::attr::parser_attr;
use parse::common::{seq_sep_none, token_to_str};
use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed};
use parse::lexer::reader;
use parse::lexer::TokenAndSpan;
use parse::obsolete::{ObsoleteClassTraits, ObsoleteModeInFnType};
use parse::obsolete::{ObsoleteLet, ObsoleteFieldTerminator};
use parse::obsolete::{ObsoleteMoveInit, ObsoleteBinaryMove};
@ -193,7 +194,7 @@ pub fn Parser(sess: parse_sess,
token: tok0.tok,
span: span0,
last_span: span0,
buffer: [mut {tok: tok0.tok, sp: span0}, ..4],
buffer: [mut TokenAndSpan {tok: tok0.tok, sp: span0}, ..4],
buffer_start: 0,
buffer_end: 0,
tokens_consumed: 0u,
@ -213,7 +214,7 @@ pub struct Parser {
mut token: token::Token,
mut span: span,
mut last_span: span,
mut buffer: [mut {tok: token::Token, sp: span} * 4],
mut buffer: [mut TokenAndSpan * 4],
mut buffer_start: int,
mut buffer_end: int,
mut tokens_consumed: uint,
@ -234,6 +235,7 @@ pub struct Parser {
}
pub impl Parser {
// advance the parser by one token
fn bump() {
self.last_span = self.span;
let next = if self.buffer_start == self.buffer_end {
@ -247,7 +249,8 @@ pub impl Parser {
self.span = next.sp;
self.tokens_consumed += 1u;
}
fn swap(next: token::Token, +lo: BytePos, +hi: BytePos) {
// EFFECT: replace the current token and span with the given one
fn replace_token(next: token::Token, +lo: BytePos, +hi: BytePos) {
self.token = next;
self.span = mk_sp(lo, hi);
}
@ -800,7 +803,7 @@ pub impl Parser {
self.bump();
self.lit_from_token(tok)
};
ast::spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
codemap::spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
}
fn parse_path_without_tps() -> @path {
@ -875,7 +878,7 @@ pub impl Parser {
self.parse_seq_lt_gt(Some(token::COMMA),
|p| p.parse_ty(false))
} else {
ast::spanned {node: ~[], span: path.span}
codemap::spanned {node: ~[], span: path.span}
}
};
@ -917,14 +920,14 @@ pub impl Parser {
@expr {
id: self.get_id(),
callee_id: self.get_id(),
node: expr_mac(ast::spanned {node: m, span: mk_sp(lo, hi)}),
node: expr_mac(codemap::spanned {node: m, span: mk_sp(lo, hi)}),
span: mk_sp(lo, hi),
}
}
fn mk_lit_u32(i: u32) -> @expr {
let span = self.span;
let lv_lit = @ast::spanned { node: lit_uint(i as u64, ty_u32),
let lv_lit = @codemap::spanned { node: lit_uint(i as u64, ty_u32),
span: span };
@expr {
@ -1404,7 +1407,7 @@ pub impl Parser {
hi = e.span.hi;
// HACK: turn &[...] into a &-evec
ex = match e.node {
expr_vec(*) | expr_lit(@ast::spanned {
expr_vec(*) | expr_lit(@codemap::spanned {
node: lit_str(_), span: _
})
if m == m_imm => {
@ -1429,7 +1432,7 @@ pub impl Parser {
expr_vec(*) if m == m_mutbl =>
expr_vstore(e, expr_vstore_mut_box),
expr_vec(*) if m == m_imm => expr_vstore(e, expr_vstore_box),
expr_lit(@ast::spanned {
expr_lit(@codemap::spanned {
node: lit_str(_), span: _}) if m == m_imm =>
expr_vstore(e, expr_vstore_box),
_ => expr_unary(box(m), e)
@ -1442,7 +1445,7 @@ pub impl Parser {
hi = e.span.hi;
// HACK: turn ~[...] into a ~-evec
ex = match e.node {
expr_vec(*) | expr_lit(@ast::spanned {
expr_vec(*) | expr_lit(@codemap::spanned {
node: lit_str(_), span: _})
if m == m_imm => expr_vstore(e, expr_vstore_uniq),
_ => expr_unary(uniq(m), e)
@ -1496,6 +1499,7 @@ pub impl Parser {
return lhs;
}
// parse an arbitrary expression.
fn parse_assign_expr() -> @expr {
let lo = self.span.lo;
let lhs = self.parse_binops();
@ -1794,7 +1798,7 @@ pub impl Parser {
self.eat(token::COMMA);
}
let blk = ast::spanned {
let blk = codemap::spanned {
node: ast::blk_ {
view_items: ~[],
stmts: ~[],
@ -1812,10 +1816,12 @@ pub impl Parser {
return self.mk_expr(lo, hi, expr_match(discriminant, arms));
}
// parse an expression
fn parse_expr() -> @expr {
return self.parse_expr_res(UNRESTRICTED);
}
// parse an expression, subject to the given restriction
fn parse_expr_res(r: restriction) -> @expr {
let old = self.restriction;
self.restriction = r;
@ -1943,7 +1949,9 @@ pub impl Parser {
// HACK: parse @"..." as a literal of a vstore @str
pat = match sub.node {
pat_lit(e@@expr {
node: expr_lit(@ast::spanned {node: lit_str(_), span: _}), _
node: expr_lit(@codemap::spanned {
node: lit_str(_),
span: _}), _
}) => {
let vst = @expr {
id: self.get_id(),
@ -1963,7 +1971,9 @@ pub impl Parser {
// HACK: parse ~"..." as a literal of a vstore ~str
pat = match sub.node {
pat_lit(e@@expr {
node: expr_lit(@ast::spanned {node: lit_str(_), span: _}), _
node: expr_lit(@codemap::spanned {
node: lit_str(_),
span: _}), _
}) => {
let vst = @expr {
id: self.get_id(),
@ -1985,7 +1995,7 @@ pub impl Parser {
// HACK: parse &"..." as a literal of a borrowed str
pat = match sub.node {
pat_lit(e@@expr {
node: expr_lit(@ast::spanned {
node: expr_lit(@codemap::spanned {
node: lit_str(_), span: _}), _
}) => {
let vst = @expr {
@ -2011,7 +2021,9 @@ pub impl Parser {
if self.token == token::RPAREN {
hi = self.span.hi;
self.bump();
let lit = @ast::spanned {node: lit_nil, span: mk_sp(lo, hi)};
let lit = @codemap::spanned {
node: lit_nil,
span: mk_sp(lo, hi)};
let expr = self.mk_expr(lo, hi, expr_lit(lit));
pat = pat_lit(expr);
} else {
@ -2381,7 +2393,7 @@ pub impl Parser {
match self.token {
token::SEMI => {
self.bump();
stmts.push(@ast::spanned {
stmts.push(@codemap::spanned {
node: stmt_semi(e, stmt_id),
.. *stmt});
}
@ -2406,7 +2418,7 @@ pub impl Parser {
match self.token {
token::SEMI => {
self.bump();
stmts.push(@ast::spanned {
stmts.push(@codemap::spanned {
node: stmt_mac((*m), true),
.. *stmt});
}
@ -2940,7 +2952,7 @@ pub impl Parser {
let actual_dtor = do the_dtor.map |dtor| {
let (d_body, d_attrs, d_s) = *dtor;
ast::spanned { node: ast::struct_dtor_ { id: self.get_id(),
codemap::spanned { node: ast::struct_dtor_ { id: self.get_id(),
attrs: d_attrs,
self_id: self.get_id(),
body: d_body},
@ -3445,7 +3457,7 @@ pub impl Parser {
self.bump();
let mut actual_dtor = do the_dtor.map |dtor| {
let (d_body, d_attrs, d_s) = *dtor;
ast::spanned { node: ast::struct_dtor_ { id: self.get_id(),
codemap::spanned { node: ast::struct_dtor_ { id: self.get_id(),
attrs: d_attrs,
self_id: self.get_id(),
body: d_body },
@ -3737,7 +3749,7 @@ pub impl Parser {
_ => self.fatal(~"expected open delimiter")
};
let m = ast::mac_invoc_tt(pth, tts);
let m: ast::mac = ast::spanned { node: m,
let m: ast::mac = codemap::spanned { node: m,
span: mk_sp(self.span.lo,
self.span.hi) };
let item_ = item_mac(m);

View File

@ -575,7 +575,7 @@ pub fn print_item(s: ps, &&item: @ast::item) {
}
bclose(s, item.span);
}
ast::item_mac(ast::spanned { node: ast::mac_invoc_tt(pth, ref tts),
ast::item_mac(codemap::spanned { node: ast::mac_invoc_tt(pth, ref tts),
_}) => {
print_visibility(s, item.vis);
print_path(s, pth, false);
@ -2241,6 +2241,7 @@ pub mod test {
use parse;
use super::*;
//use util;
use util::testing::check_equal;
fn string_check<T : Eq> (given : &T, expected: &T) {
if !(given == expected) {
@ -2257,11 +2258,11 @@ pub mod test {
inputs: ~[],
output: @ast::Ty {id: 0,
node: ast::ty_nil,
span: ast_util::dummy_sp()},
span: codemap::dummy_sp()},
cf: ast::return_val
};
assert fun_to_str(decl, abba_ident, ~[],mock_interner)
== ~"fn abba()";
check_equal (&fun_to_str(decl, abba_ident, ~[],mock_interner),
&~"fn abba()");
}
#[test]
@ -2269,7 +2270,7 @@ pub mod test {
let mock_interner = parse::token::mk_fake_ident_interner();
let ident = mock_interner.intern(@~"principal_skinner");
let var = ast_util::respan(ast_util::dummy_sp(), ast::variant_ {
let var = codemap::respan(codemap::dummy_sp(), ast::variant_ {
name: ident,
attrs: ~[],
// making this up as I go.... ?
@ -2280,7 +2281,7 @@ pub mod test {
});
let varstr = variant_to_str(var,mock_interner);
string_check(&varstr,&~"pub principal_skinner");
check_equal(&varstr,&~"pub principal_skinner");
}
}

View File

@ -46,10 +46,11 @@ pub mod ast_map;
pub mod visit;
pub mod fold;
pub mod util {
#[path = "interner.rs"]
pub mod interner;
pub mod testing;
}
#[path = "parse/mod.rs"]
pub mod parse;
@ -86,3 +87,4 @@ pub mod ext {
pub mod trace_macros;
}

View File

@ -0,0 +1,24 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// support for test cases.
use core::cmp;
pub pure fn check_equal_ptr<T : cmp::Eq> (given : &T, expected: &T) {
if !((given == expected) && (expected == given )) {
fail (fmt!("given %?, expected %?",given,expected));
}
}
pub pure fn check_equal<T : cmp::Eq> (given : T, expected: T) {
if !((given == expected) && (expected == given )) {
fail (fmt!("given %?, expected %?",given,expected));
}
}