diff options
| author | bors <bors@rust-lang.org> | 2013-04-10 09:43:01 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-10 09:43:01 -0700 |
| commit | 2c649830be732bc0af585ba4ef54b2c6555b7c1c (patch) | |
| tree | 1d76dbaa24449d9bfc3bb745ad2979c9da5bec07 /src/libsyntax/parse | |
| parent | ac9dc69bf3c0e4c46fadeab76229ba35f61d8158 (diff) | |
| parent | 9deb2f2dfabd0ad164eedf84fe7e91b0b5dabf66 (diff) | |
| download | rust-2c649830be732bc0af585ba4ef54b2c6555b7c1c.tar.gz rust-2c649830be732bc0af585ba4ef54b2c6555b7c1c.zip | |
auto merge of #5709 : jbclements/rust/miscellaneous-cleanup, r=jbclements
There's no unifying theme here; I'm just trying to clear a bunch of small commits: removing dead code, adding comments, renaming to an upper-case type, fixing one test case.
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/eval.rs | 174 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 79 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 2 |
3 files changed, 51 insertions, 204 deletions
diff --git a/src/libsyntax/parse/eval.rs b/src/libsyntax/parse/eval.rs deleted file mode 100644 index 5d44db084d6..00000000000 --- a/src/libsyntax/parse/eval.rs +++ /dev/null @@ -1,174 +0,0 @@ -// 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. - -use parser::Parser; -use attr::parser_attr; -use codemap::{span, mk_sp}; - -type ctx = - @{sess: parse::parse_sess, - cfg: ast::crate_cfg}; - -fn eval_crate_directives(cx: ctx, - cdirs: ~[@ast::crate_directive], - prefix: &Path, - view_items: &mut~[@ast::view_item], - items: &mut~[@ast::item]) { - for cdirs.each |sub_cdir| { - eval_crate_directive(cx, *sub_cdir, prefix, view_items, items); - } -} - -pub fn eval_crate_directives_to_mod(cx: ctx, cdirs: ~[@ast::crate_directive], - prefix: &Path, suffix: &Option<Path>) - -> (ast::_mod, ~[ast::attribute]) { - let (cview_items, citems, cattrs) - = parse_companion_mod(cx, prefix, suffix); - let mut view_items: ~[@ast::view_item] = ~[]; - let mut items: ~[@ast::item] = ~[]; - eval_crate_directives(cx, cdirs, prefix, &mut view_items, &mut items); - return ({view_items: vec::append(view_items, cview_items), - items: vec::append(items, citems)}, - cattrs); -} - -/* -The 'companion mod'. So .rc crates and directory mod crate directives define -modules but not a .rs file to fill those mods with stuff. The companion mod is -a convention for location a .rs file to go with them. For .rc files the -companion mod is a .rs file with the same name; for directory mods the -companion mod is a .rs file with the same name as the directory. - -We build the path to the companion mod by combining the prefix and the -optional suffix then adding the .rs extension. -*/ -fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option<Path>) - -> (~[@ast::view_item], ~[@ast::item], ~[ast::attribute]) { - - fn companion_file(prefix: &Path, suffix: &Option<Path>) -> Path { - return match *suffix { - option::Some(s) => prefix.push_many(s.components), - option::None => copy *prefix - }.with_filetype("rs"); - } - - fn file_exists(path: &Path) -> bool { - // Crude, but there's no lib function for this and I'm not - // up to writing it just now - match io::file_reader(path) { - result::Ok(_) => true, - result::Err(_) => false - } - } - - let modpath = &companion_file(prefix, suffix); - if file_exists(modpath) { - debug!("found companion mod"); - // XXX: Using a dummy span, but this code will go away soon - let p0 = new_sub_parser_from_file(cx.sess, cx.cfg, - modpath, - codemap::dummy_sp()); - let (inner, next) = p0.parse_inner_attrs_and_next(); - let m0 = p0.parse_mod_items(token::EOF, next); - return (m0.view_items, m0.items, inner); - } else { - return (~[], ~[], ~[]); - } -} - -fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { - match ::attr::first_attr_value_str_by_name(attrs, ~"path") { - Some(d) => d, - None => default - } -} - -pub fn eval_src_mod(cx: ctx, prefix: &Path, - outer_attrs: ~[ast::attribute], - id: ast::ident, sp: span) - -> (ast::item_, ~[ast::attribute]) { - let file_path = Path(cdir_path_opt( - cx.sess.interner.get(id) + ~".rs", outer_attrs)); - eval_src_mod_from_path(cx, prefix, &file_path, outer_attrs, sp) -} - -pub fn eval_src_mod_from_path(cx: ctx, prefix: &Path, path: &Path, - outer_attrs: ~[ast::attribute], - sp: span) - -> (ast::item_, ~[ast::attribute]) { - let full_path = if path.is_absolute { - copy *path - } else { - prefix.push_many(path.components) - }; - let p0 = - new_sub_parser_from_file(cx.sess, cx.cfg, - &full_path, sp); - let (inner, next) = p0.parse_inner_attrs_and_next(); - let mod_attrs = vec::append(outer_attrs, inner); - let first_item_outer_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 (m, mod_attrs) = eval_src_mod(cx, prefix, attrs, id, cdir.span); - let i = mk_item(cx, cdir.span.lo, cdir.span.hi, - /* FIXME (#2543) */ copy id, - m, vis, mod_attrs); - items.push(i); - } - ast::cdir_dir_mod(vis, id, cdirs, attrs) => { - let path = Path(cdir_path_opt(*cx.sess.interner.get(id), attrs)); - let full_path = if path.is_absolute { - copy path - } else { - prefix.push_many(path.components) - }; - let (m0, a0) = eval_crate_directives_to_mod( - cx, cdirs, &full_path, &None); - let i = - @{ident: /* FIXME (#2543) */ copy id, - attrs: vec::append(attrs, a0), - id: cx.sess.next_id, - node: ast::item_mod(m0), - vis: vis, - span: cdir.span}; - cx.sess.next_id += 1; - items.push(i); - } - ast::cdir_view_item(vi) => view_items.push(vi), - } -} -// -// Local Variables: -// mode: rust -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: -// diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3a3597828cd..fb3e8a5ded5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -705,6 +705,7 @@ pub impl Parser { @Ty {id: self.get_id(), node: t, span: sp} } + // parse the type following a @ or a ~ fn parse_box_or_uniq_pointee( &self, sigil: ast::Sigil, @@ -897,7 +898,7 @@ pub impl Parser { // parse a path that doesn't have type parameters attached fn parse_path_without_tps(&self) - -> @ast::path { + -> @ast::Path { maybe_whole!(self, nt_path); let lo = self.span.lo; let global = self.eat(&token::MOD_SEP); @@ -917,7 +918,7 @@ pub impl Parser { break; } } - @ast::path { span: mk_sp(lo, self.last_span.hi), + @ast::Path { span: mk_sp(lo, self.last_span.hi), global: global, idents: ids, rp: None, @@ -927,7 +928,7 @@ pub impl Parser { // parse a path optionally with type parameters. If 'colons' // is true, then type parameters must be preceded by colons, // as in a::t::<t1,t2> - fn parse_path_with_tps(&self, colons: bool) -> @ast::path { + fn parse_path_with_tps(&self, colons: bool) -> @ast::Path { debug!("parse_path_with_tps(colons=%b)", colons); maybe_whole!(self, nt_path); @@ -982,18 +983,14 @@ pub impl Parser { } }; - @ast::path { span: mk_sp(lo, hi), + @ast::Path { span: mk_sp(lo, hi), rp: rp, types: tps, .. copy *path } } + /// parses 0 or 1 lifetime fn parse_opt_lifetime(&self) -> Option<@ast::Lifetime> { - /*! - * - * Parses 0 or 1 lifetime. - */ - match *self.token { token::LIFETIME(*) => { Some(@self.parse_lifetime()) @@ -1022,12 +1019,9 @@ pub impl Parser { } } + /// Parses a single lifetime + // matches lifetime = ( LIFETIME ) | ( IDENT / ) fn parse_lifetime(&self) -> ast::Lifetime { - /*! - * - * Parses a single lifetime. - */ - match *self.token { token::LIFETIME(i) => { let span = copy self.span; @@ -1147,6 +1141,9 @@ pub impl Parser { } } + // at the bottom (top?) of the precedence hierarchy, + // parse things like parenthesized exprs, + // macros, return, etc. fn parse_bottom_expr(&self) -> @expr { maybe_whole_expr!(self); @@ -1350,6 +1347,7 @@ pub impl Parser { return self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk)); } + // parse a.b or a(13) or just a fn parse_dot_or_call_expr(&self) -> @expr { let b = self.parse_bottom_expr(); self.parse_dot_or_call_expr_with(b) @@ -1618,7 +1616,7 @@ pub impl Parser { return spanned(lo, self.span.hi, m); } - + // parse a prefix-operator expr fn parse_prefix_expr(&self) -> @expr { let lo = self.span.lo; let mut hi; @@ -1629,7 +1627,6 @@ pub impl Parser { self.bump(); let e = self.parse_prefix_expr(); hi = e.span.hi; - self.get_id(); // see ast_util::op_expr_callee_id ex = expr_unary(not, e); } token::BINOP(b) => { @@ -1638,7 +1635,6 @@ pub impl Parser { self.bump(); let e = self.parse_prefix_expr(); hi = e.span.hi; - self.get_id(); // see ast_util::op_expr_callee_id ex = expr_unary(neg, e); } token::STAR => { @@ -1738,7 +1734,6 @@ pub impl Parser { self.bump(); let expr = self.parse_prefix_expr(); let rhs = self.parse_more_binops(expr, cur_prec); - self.get_id(); // see ast_util::op_expr_callee_id let bin = self.mk_expr(lhs.span.lo, rhs.span.hi, expr_binary(cur_op, lhs, rhs)); self.parse_more_binops(bin, min_prec) @@ -1789,7 +1784,6 @@ pub impl Parser { token::SHL => aop = shl, token::SHR => aop = shr } - self.get_id(); // see ast_util::op_expr_callee_id self.mk_expr(lo, rhs.span.hi, expr_assign_op(aop, lhs, rhs)) } @@ -2556,11 +2550,14 @@ pub impl Parser { } fn parse_block(&self) -> blk { + // disallow inner attrs: let (attrs, blk) = self.parse_inner_attrs_and_block(false); assert!(vec::is_empty(attrs)); return blk; } + // I claim the existence of the 'parse_attrs' flag strongly + // suggests a name-change or refactoring for this function. fn parse_inner_attrs_and_block(&self, parse_attrs: bool) -> (~[attribute], blk) { @@ -2601,6 +2598,7 @@ pub impl Parser { self.parse_block_tail_(lo, s, ~[]) } + // parse the rest of a block expression or function body fn parse_block_tail_(&self, lo: BytePos, s: blk_check_mode, +first_item_attrs: ~[attribute]) -> blk { let mut stmts = ~[]; @@ -2797,6 +2795,10 @@ pub impl Parser { ast::TyParam { ident: ident, id: self.get_id(), bounds: bounds } } + // parse a set of optional generic type parameter declarations + // matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > ) + // | ( < lifetimes , typaramseq ( , )? > ) + // where typaramseq = ( typaram ) | ( typaram , typaramseq ) fn parse_generics(&self) -> ast::Generics { if self.eat(&token::LT) { let lifetimes = self.parse_lifetimes(); @@ -2809,6 +2811,7 @@ pub impl Parser { } } + // parse a generic use site fn parse_generic_values( &self) -> (OptVec<ast::Lifetime>, ~[@Ty]) { @@ -3099,6 +3102,7 @@ pub impl Parser { } } + // parse trait Foo { ... } fn parse_item_trait(&self) -> item_info { let ident = self.parse_ident(); self.parse_region_param(); @@ -3177,6 +3181,7 @@ pub impl Parser { (ident, item_impl(generics, opt_trait, ty, meths), None) } + // parse a::B<~str,int> fn parse_trait_ref(&self) -> @trait_ref { @ast::trait_ref { path: self.parse_path_with_tps(false), @@ -3184,6 +3189,7 @@ pub impl Parser { } } + // parse B + C<~str,int> + D fn parse_trait_ref_list(&self, ket: &token::Token) -> ~[@trait_ref] { self.parse_seq_to_before_end( ket, @@ -3192,6 +3198,7 @@ pub impl Parser { ) } + // parse struct Foo { ... } fn parse_item_struct(&self) -> item_info { let class_name = self.parse_ident(); self.parse_region_param(); @@ -3441,6 +3448,7 @@ pub impl Parser { (id, item_const(ty, e), None) } + // parse a mod { ...} item fn parse_item_mod(&self, outer_attrs: ~[ast::attribute]) -> item_info { let id_span = *self.span; let id = self.parse_ident(); @@ -3697,7 +3705,7 @@ pub impl Parser { } }; - // extern mod { ... } + // extern mod foo { ... } or extern { ... } if items_allowed && self.eat(&token::LBRACE) { let abis = opt_abis.get_or_default(AbiSet::C()); @@ -3732,6 +3740,7 @@ pub impl Parser { (lo, id) } + // parse type Foo = Bar; fn parse_item_type(&self) -> item_info { let (_, ident) = self.parse_type_decl(); self.parse_region_param(); @@ -3742,6 +3751,7 @@ pub impl Parser { (ident, item_ty(ty, tps), None) } + // parse obsolete region parameter fn parse_region_param(&self) { if self.eat(&token::BINOP(token::SLASH)) { self.obsolete(*self.last_span, ObsoleteLifetimeNotation); @@ -3859,6 +3869,7 @@ pub impl Parser { let generics = self.parse_generics(); // Newtype syntax if *self.token == token::EQ { + // enum x = ty; self.bump(); let ty = self.parse_ty(false); self.expect(&token::SEMI); @@ -3883,6 +3894,7 @@ pub impl Parser { None ); } + // enum X { ... } self.expect(&token::LBRACE); let enum_definition = self.parse_enum_def(&generics); @@ -3986,7 +3998,7 @@ pub impl Parser { (self.is_keyword(&~"const") || (self.is_keyword(&~"static") && !self.token_is_keyword(&~"fn", &self.look_ahead(1)))) { - // CONST ITEM + // CONST / STATIC ITEM if self.is_keyword(&~"const") { self.obsolete(*self.span, ObsoleteConstItem); } @@ -4002,10 +4014,9 @@ pub impl Parser { let item = self.parse_item_foreign_const(visibility, attrs); return iovi_foreign_item(item); } - if items_allowed && - // FUNCTION ITEM (not sure about lookahead condition...) - self.is_keyword(&~"fn") && + if items_allowed && self.is_keyword(&~"fn") && !self.fn_expr_lookahead(self.look_ahead(1u)) { + // FUNCTION ITEM self.bump(); let (ident, item_, extra_attrs) = self.parse_item_fn(impure_fn, AbiSet::Rust()); @@ -4014,7 +4025,7 @@ pub impl Parser { maybe_append(attrs, extra_attrs))); } if items_allowed && self.eat_keyword(&~"pure") { - // PURE FUNCTION ITEM + // PURE FUNCTION ITEM (obsolete) self.obsolete(*self.last_span, ObsoletePurity); self.expect_keyword(&~"fn"); let (ident, item_, extra_attrs) = @@ -4192,6 +4203,12 @@ pub impl Parser { return view_item_use(self.parse_view_paths()); } + + // matches view_path : MOD? IDENT EQ non_global_path + // | MOD? non_global_path MOD_SEP LBRACE RBRACE + // | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE + // | MOD? non_global_path MOD_SEP STAR + // | MOD? non_global_path fn parse_view_path(&self) -> @view_path { let lo = self.span.lo; @@ -4215,7 +4232,7 @@ pub impl Parser { let id = self.parse_ident(); path.push(id); } - let path = @ast::path { span: mk_sp(lo, self.span.hi), + let path = @ast::Path { span: mk_sp(lo, self.span.hi), global: false, idents: path, rp: None, @@ -4244,7 +4261,7 @@ pub impl Parser { seq_sep_trailing_allowed(token::COMMA), |p| p.parse_path_list_ident() ); - let path = @ast::path { span: mk_sp(lo, self.span.hi), + let path = @ast::Path { span: mk_sp(lo, self.span.hi), global: false, idents: path, rp: None, @@ -4256,7 +4273,7 @@ pub impl Parser { // foo::bar::* token::BINOP(token::STAR) => { self.bump(); - let path = @ast::path { span: mk_sp(lo, self.span.hi), + let path = @ast::Path { span: mk_sp(lo, self.span.hi), global: false, idents: path, rp: None, @@ -4272,7 +4289,7 @@ pub impl Parser { _ => () } let last = path[vec::len(path) - 1u]; - let path = @ast::path { span: mk_sp(lo, self.span.hi), + let path = @ast::Path { span: mk_sp(lo, self.span.hi), global: false, idents: path, rp: None, @@ -4281,6 +4298,7 @@ pub impl Parser { view_path_simple(last, path, namespace, self.get_id())); } + // matches view_paths = view_path | view_path , view_paths fn parse_view_paths(&self) -> ~[@view_path] { let mut vp = ~[self.parse_view_path()]; while *self.token == token::COMMA { @@ -4330,6 +4348,9 @@ pub impl Parser { // Parses a sequence of items. Stops when it finds program // text that can't be parsed as an item + // - mod_items uses VIEW_ITEMS_AND_ITEMS_ALLOWED + // - block_tail_ uses IMPORTS_AND_ITEMS_ALLOWED + // - foreign_mod_items uses FOREIGN_ITEMS_ALLOWED fn parse_items_and_view_items(&self, +first_item_attrs: ~[attribute], mode: view_item_parse_mode, macros_allowed: bool) diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 713a6e89475..54b2ad85147 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -113,7 +113,7 @@ pub enum nonterminal { nt_expr(@ast::expr), nt_ty( @ast::Ty), nt_ident(ast::ident, bool), - nt_path(@ast::path), + nt_path(@ast::Path), nt_tt( @ast::token_tree), //needs @ed to break a circularity nt_matchers(~[ast::matcher]) } |
