From 5b9e110eab9c30428e99995a0adbec82857e3a1a Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 25 Feb 2013 06:19:44 -0800 Subject: libsyntax: Convert ast::attribute_ to store a @meta_item --- src/libsyntax/parse/attr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index c0c97a0b9eb..87ecf6a9567 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -72,7 +72,7 @@ impl parser_attr for Parser { self.expect(token::RBRACKET); let mut hi = self.span.hi; return spanned(lo, hi, ast::attribute_ { style: style, - value: *meta_item, + value: meta_item, is_sugared_doc: false }); } -- cgit 1.4.1-3-g733a5 From d20438695e133586d230e7e687473ad39acc52d3 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 09:06:13 -0800 Subject: libsyntax: make lexer vecs_implicitly_copyable-free --- src/libsyntax/parse/lexer.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 92c4f1e828f..54fdcc647ea 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -103,8 +103,8 @@ fn dup_string_reader(r: @mut StringReader) -> @mut StringReader { curr: r.curr, filemap: r.filemap, interner: r.interner, - peek_tok: r.peek_tok, - peek_span: r.peek_span + peek_tok: copy r.peek_tok, + peek_span: copy r.peek_span } } @@ -112,9 +112,12 @@ impl reader for StringReader { fn is_eof(@mut self) -> bool { is_eof(self) } // return the next token. EFFECT: advances the string_reader. fn next_token(@mut self) -> TokenAndSpan { - let ret_val = TokenAndSpan {tok: self.peek_tok, sp: self.peek_span}; + let ret_val = TokenAndSpan { + tok: copy self.peek_tok, + sp: copy self.peek_span, + }; string_advance_token(self); - return ret_val; + ret_val } fn fatal(@mut self, m: ~str) -> ! { self.span_diagnostic.span_fatal(copy self.peek_span, m) @@ -122,7 +125,10 @@ impl reader for StringReader { fn span_diag(@mut self) -> span_handler { self.span_diagnostic } pure fn interner(@mut self) -> @token::ident_interner { self.interner } fn peek(@mut self) -> TokenAndSpan { - TokenAndSpan {tok: self.peek_tok, sp: self.peek_span} + TokenAndSpan { + tok: copy self.peek_tok, + sp: copy self.peek_span, + } } fn dup(@mut self) -> reader { dup_string_reader(self) as reader } } @@ -136,7 +142,10 @@ pub impl reader for TtReader { fn span_diag(@mut self) -> span_handler { self.sp_diag } pure fn interner(@mut self) -> @token::ident_interner { self.interner } fn peek(@mut self) -> TokenAndSpan { - TokenAndSpan { tok: self.cur_tok, sp: self.cur_span } + TokenAndSpan { + tok: copy self.cur_tok, + sp: copy self.cur_span, + } } fn dup(@mut self) -> reader { dup_tt_reader(self) as reader } } @@ -145,8 +154,8 @@ pub impl reader for TtReader { fn string_advance_token(r: @mut StringReader) { match (consume_whitespace_and_comments(r)) { Some(comment) => { - r.peek_tok = comment.tok; - r.peek_span = comment.sp; + r.peek_tok = copy comment.tok; + r.peek_span = copy comment.sp; }, None => { if is_eof(r) { -- cgit 1.4.1-3-g733a5 From 8b94ef0302c898b0643810c9a31978684b607a37 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 20 Feb 2013 21:04:05 -0800 Subject: libsyntax: fix the span in parse_bottom_expr's INTERPOLATED handler --- src/libsyntax/parse/parser.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9bac163dab6..69790e00779 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -127,18 +127,23 @@ enum view_item_parse_mode { The important thing is to make sure that lookahead doesn't balk at INTERPOLATED tokens */ macro_rules! maybe_whole_expr ( - ($p:expr) => ( match *$p.token { - INTERPOLATED(token::nt_expr(e)) => { - $p.bump(); - return e; - } - INTERPOLATED(token::nt_path(pt)) => { - $p.bump(); - return $p.mk_expr($p.span.lo, $p.span.lo, - expr_path(pt)); - } - _ => () - }) + ($p:expr) => ( + match *$p.token { + INTERPOLATED(token::nt_expr(e)) => { + $p.bump(); + return e; + } + INTERPOLATED(token::nt_path(pt)) => { + $p.bump(); + return $p.mk_expr( + $p.span.lo, + $p.span.hi, + expr_path(pt) + ); + } + _ => () + } + ) ) macro_rules! maybe_whole ( -- cgit 1.4.1-3-g733a5 From 1deb858b2217cd9107bef4d64d35f1489f77f2ff Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 09:39:29 -0800 Subject: libsyntax: is_keyword should take a &~str --- src/libsyntax/ext/trace_macros.rs | 4 +- src/libsyntax/parse/common.rs | 42 ++++---- src/libsyntax/parse/obsolete.rs | 4 +- src/libsyntax/parse/parser.rs | 210 +++++++++++++++++++------------------- 4 files changed, 132 insertions(+), 128 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index 842727f092a..fb7b41be2d6 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -31,9 +31,9 @@ pub fn expand_trace_macros(cx: ext_ctxt, sp: span, let rdr = tt_rdr as reader; let rust_parser = Parser(sess, cfg, rdr.dup()); - if rust_parser.is_keyword(~"true") { + if rust_parser.is_keyword(&~"true") { cx.set_trace_macros(true); - } else if rust_parser.is_keyword(~"false") { + } else if rust_parser.is_keyword(&~"false") { cx.set_trace_macros(false); } else { cx.span_fatal(sp, ~"trace_macros! only accepts `true` or `false`") diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 57d62d628dc..0317ae14e8c 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -110,25 +110,25 @@ pub impl Parser { // Storing keywords as interned idents instead of strings would be nifty. // A sanity check that the word we are asking for is a known keyword - fn require_keyword(word: ~str) { - if !self.keywords.contains_key(&word) { - self.bug(fmt!("unknown keyword: %s", word)); + fn require_keyword(word: &~str) { + if !self.keywords.contains_key(word) { + self.bug(fmt!("unknown keyword: %s", *word)); } } - fn token_is_word(word: ~str, ++tok: token::Token) -> bool { + fn token_is_word(word: &~str, tok: token::Token) -> bool { match tok { - token::IDENT(sid, false) => { *self.id_to_str(sid) == word } + token::IDENT(sid, false) => { *self.id_to_str(sid) == *word } _ => { false } } } - fn token_is_keyword(word: ~str, ++tok: token::Token) -> bool { + fn token_is_keyword(word: &~str, ++tok: token::Token) -> bool { self.require_keyword(word); self.token_is_word(word, tok) } - fn is_keyword(word: ~str) -> bool { + fn is_keyword(word: &~str) -> bool { self.token_is_keyword(word, *self.token) } @@ -141,62 +141,62 @@ pub impl Parser { } } - fn eat_keyword(word: ~str) -> bool { + fn eat_keyword(word: &~str) -> bool { self.require_keyword(word); let is_kw = match *self.token { - token::IDENT(sid, false) => (word == *self.id_to_str(sid)), + token::IDENT(sid, false) => *word == *self.id_to_str(sid), _ => false }; if is_kw { self.bump() } is_kw } - fn expect_keyword(word: ~str) { + fn expect_keyword(word: &~str) { self.require_keyword(word); if !self.eat_keyword(word) { - self.fatal(~"expected `" + word + ~"`, found `" + + self.fatal(~"expected `" + *word + ~"`, found `" + token_to_str(self.reader, *self.token) + ~"`"); } } - fn is_strict_keyword(word: ~str) -> bool { - self.strict_keywords.contains_key(&word) + fn is_strict_keyword(word: &~str) -> bool { + self.strict_keywords.contains_key(word) } fn check_strict_keywords() { match *self.token { token::IDENT(_, false) => { let w = token_to_str(self.reader, *self.token); - self.check_strict_keywords_(w); + self.check_strict_keywords_(&w); } _ => () } } - fn check_strict_keywords_(w: ~str) { + fn check_strict_keywords_(w: &~str) { if self.is_strict_keyword(w) { - self.fatal(~"found `" + w + ~"` in ident position"); + self.fatal(~"found `" + *w + ~"` in ident position"); } } - fn is_reserved_keyword(word: ~str) -> bool { - self.reserved_keywords.contains_key(&word) + fn is_reserved_keyword(word: &~str) -> bool { + self.reserved_keywords.contains_key(word) } fn check_reserved_keywords() { match *self.token { token::IDENT(_, false) => { let w = token_to_str(self.reader, *self.token); - self.check_reserved_keywords_(w); + self.check_reserved_keywords_(&w); } _ => () } } - fn check_reserved_keywords_(w: ~str) { + fn check_reserved_keywords_(w: &~str) { if self.is_reserved_keyword(w) { - self.fatal(~"`" + w + ~"` is a reserved keyword"); + self.fatal(~"`" + *w + ~"` is a reserved keyword"); } } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 1ae8786e09b..02c2fb404c2 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -197,9 +197,9 @@ pub impl Parser { } fn try_parse_obsolete_priv_section() -> bool { - if self.is_keyword(~"priv") && self.look_ahead(1) == token::LBRACE { + if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE { self.obsolete(*self.span, ObsoletePrivSection); - self.eat_keyword(~"priv"); + self.eat_keyword(&~"priv"); self.bump(); while *self.token != token::RBRACE { self.parse_single_class_item(ast::private); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 69790e00779..ffc8a28a545 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -310,10 +310,10 @@ pub impl Parser { pure fn id_to_str(id: ident) -> @~str { self.sess.interner.get(id) } fn token_is_closure_keyword(+tok: token::Token) -> bool { - self.token_is_keyword(~"pure", tok) || - self.token_is_keyword(~"unsafe", tok) || - self.token_is_keyword(~"once", tok) || - self.token_is_keyword(~"fn", tok) + self.token_is_keyword(&~"pure", tok) || + self.token_is_keyword(&~"unsafe", tok) || + self.token_is_keyword(&~"once", tok) || + self.token_is_keyword(&~"fn", tok) } fn parse_ty_bare_fn() -> ty_ @@ -333,7 +333,7 @@ pub impl Parser { */ let purity = self.parse_purity(); - self.expect_keyword(~"fn"); + self.expect_keyword(&~"fn"); return ty_bare_fn(@TyBareFn { abi: RustAbi, purity: purity, @@ -364,7 +364,7 @@ pub impl Parser { let purity = self.parse_purity(); let onceness = parse_onceness(&self); - self.expect_keyword(~"fn"); + self.expect_keyword(&~"fn"); let post_sigil = self.parse_fn_ty_sigil(); let sigil = match (pre_sigil, post_sigil) { @@ -392,14 +392,14 @@ pub impl Parser { }); fn parse_onceness(self: &Parser) -> Onceness { - if self.eat_keyword(~"once") {Once} else {Many} + if self.eat_keyword(&~"once") { Once } else { Many } } } fn parse_purity() -> purity { - if self.eat_keyword(~"pure") { + if self.eat_keyword(&~"pure") { return pure_fn; - } else if self.eat_keyword(~"unsafe") { + } else if self.eat_keyword(&~"unsafe") { return unsafe_fn; } else { return impure_fn; @@ -640,7 +640,7 @@ pub impl Parser { } else if *self.token == token::BINOP(token::AND) { self.bump(); self.parse_borrowed_pointee() - } else if self.eat_keyword(~"extern") { + } else if self.eat_keyword(&~"extern") { self.parse_ty_bare_fn() } else if self.token_is_closure_keyword(*self.token) { self.parse_ty_closure(None, None) @@ -648,7 +648,9 @@ pub impl Parser { || is_ident_or_path(*self.token) { let path = self.parse_path_with_tps(colons_before_params); ty_path(path, self.get_id()) - } else { self.fatal(~"expected type"); }; + } else { + self.fatal(~"expected type"); + }; let sp = mk_sp(lo, self.last_span.hi); @Ty {id: self.get_id(), node: t, span: sp} @@ -667,8 +669,10 @@ pub impl Parser { token::IDENT(rname, _) => { if self.look_ahead(1u) == token::BINOP(token::SLASH) && - self.token_is_closure_keyword(self.look_ahead(2u)) { - self.bump(); self.bump(); + self.token_is_closure_keyword(self.look_ahead(2u)) + { + self.bump(); + self.bump(); return self.parse_ty_closure(Some(sigil), Some(rname)); } else if self.token_is_closure_keyword(*self.token) { return self.parse_ty_closure(Some(sigil), None); @@ -756,7 +760,7 @@ pub impl Parser { fn parse_capture_item_or(parse_arg_fn: fn(Parser) -> arg_or_capture_item) -> arg_or_capture_item { - if self.eat_keyword(~"copy") { + if self.eat_keyword(&~"copy") { // XXX outdated syntax now that moves-based-on-type has gone in self.parse_ident(); either::Right(()) @@ -772,7 +776,7 @@ pub impl Parser { let mut is_mutbl = false; let pat = if require_name || self.is_named_argument() { m = self.parse_arg_mode(); - is_mutbl = self.eat_keyword(~"mut"); + is_mutbl = self.eat_keyword(&~"mut"); let pat = self.parse_pat(false); self.expect(token::COLON); pat @@ -800,7 +804,7 @@ pub impl Parser { fn parse_fn_block_arg() -> arg_or_capture_item { do self.parse_capture_item_or |p| { let m = p.parse_arg_mode(); - let is_mutbl = self.eat_keyword(~"mut"); + let is_mutbl = self.eat_keyword(&~"mut"); let pat = p.parse_pat(false); let t = if p.eat(token::COLON) { p.parse_ty(false) @@ -856,9 +860,9 @@ pub impl Parser { fn parse_lit() -> lit { let lo = self.span.lo; - let lit = if self.eat_keyword(~"true") { + let lit = if self.eat_keyword(&~"true") { lit_bool(true) - } else if self.eat_keyword(~"false") { + } else if self.eat_keyword(&~"false") { lit_bool(false) } else { // XXX: This is a really bad copy! @@ -1024,9 +1028,9 @@ pub impl Parser { } fn parse_mutability() -> mutability { - if self.eat_keyword(~"mut") { + if self.eat_keyword(&~"mut") { m_mutbl - } else if self.eat_keyword(~"const") { + } else if self.eat_keyword(&~"const") { m_const } else { m_imm @@ -1124,21 +1128,21 @@ pub impl Parser { } } else if token::is_bar(*self.token) { return self.parse_lambda_expr(); - } else if self.eat_keyword(~"if") { + } else if self.eat_keyword(&~"if") { return self.parse_if_expr(); - } else if self.eat_keyword(~"for") { + } else if self.eat_keyword(&~"for") { return self.parse_sugary_call_expr(~"for", ForSugar, expr_loop_body); - } else if self.eat_keyword(~"do") { + } else if self.eat_keyword(&~"do") { return self.parse_sugary_call_expr(~"do", DoSugar, expr_do_body); - } else if self.eat_keyword(~"while") { + } else if self.eat_keyword(&~"while") { return self.parse_while_expr(); - } else if self.eat_keyword(~"loop") { + } else if self.eat_keyword(&~"loop") { return self.parse_loop_expr(); - } else if self.eat_keyword(~"match") { + } else if self.eat_keyword(&~"match") { return self.parse_match_expr(); - } else if self.eat_keyword(~"fn") { + } else if self.eat_keyword(&~"fn") { let opt_sigil = self.parse_fn_ty_sigil(); let sigil = match opt_sigil { None => { @@ -1147,7 +1151,7 @@ pub impl Parser { Some(p) => { p } }; return self.parse_fn_expr(sigil); - } else if self.eat_keyword(~"unsafe") { + } else if self.eat_keyword(&~"unsafe") { return self.parse_block_expr(lo, unsafe_blk); } else if *self.token == token::LBRACKET { self.bump(); @@ -1182,7 +1186,7 @@ pub impl Parser { } } hi = self.span.hi; - } else if self.eat_keyword(~"log") { + } else if self.eat_keyword(&~"log") { self.expect(token::LPAREN); let lvl = self.parse_expr(); self.expect(token::COMMA); @@ -1190,30 +1194,30 @@ pub impl Parser { ex = expr_log(ast::log_other, lvl, e); hi = self.span.hi; self.expect(token::RPAREN); - } else if self.eat_keyword(~"assert") { + } else if self.eat_keyword(&~"assert") { let e = self.parse_expr(); ex = expr_assert(e); hi = e.span.hi; - } else if self.eat_keyword(~"return") { + } else if self.eat_keyword(&~"return") { if can_begin_expr(*self.token) { let e = self.parse_expr(); hi = e.span.hi; ex = expr_ret(Some(e)); } else { ex = expr_ret(None); } - } else if self.eat_keyword(~"break") { + } else if self.eat_keyword(&~"break") { if is_ident(*self.token) { ex = expr_break(Some(self.parse_ident())); } else { ex = expr_break(None); } hi = self.span.hi; - } else if self.eat_keyword(~"copy") { + } else if self.eat_keyword(&~"copy") { let e = self.parse_expr(); ex = expr_copy(e); hi = e.span.hi; } else if *self.token == token::MOD_SEP || - is_ident(*self.token) && !self.is_keyword(~"true") && - !self.is_keyword(~"false") { + is_ident(*self.token) && !self.is_keyword(&~"true") && + !self.is_keyword(&~"false") { let pth = self.parse_path_with_tps(true); /* `!`, as an operator, is prefix, so we know this isn't that */ @@ -1642,7 +1646,7 @@ pub impl Parser { } } None => { - if as_prec > min_prec && self.eat_keyword(~"as") { + if as_prec > min_prec && self.eat_keyword(&~"as") { let rhs = self.parse_ty(true); let _as = self.mk_expr(lhs.span.lo, rhs.span.hi, @@ -1714,7 +1718,7 @@ pub impl Parser { let thn = self.parse_block(); let mut els: Option<@expr> = None; let mut hi = thn.span.hi; - if self.eat_keyword(~"else") { + if self.eat_keyword(&~"else") { let elexpr = self.parse_else_expr(); els = Some(elexpr); hi = elexpr.span.hi; @@ -1788,7 +1792,7 @@ pub impl Parser { } fn parse_else_expr() -> @expr { - if self.eat_keyword(~"if") { + if self.eat_keyword(&~"if") { return self.parse_if_expr(); } else { let blk = self.parse_block(); @@ -1904,7 +1908,7 @@ pub impl Parser { fn looking_at_record_literal() -> bool { let lookahead = self.look_ahead(1); *self.token == token::LBRACE && - (self.token_is_keyword(~"mut", lookahead) || + (self.token_is_keyword(&~"mut", lookahead) || (is_plain_ident(lookahead) && self.look_ahead(2) == token::COLON)) } @@ -1945,7 +1949,7 @@ pub impl Parser { while *self.token != token::RBRACE { let pats = self.parse_pats(); let mut guard = None; - if self.eat_keyword(~"if") { guard = Some(self.parse_expr()); } + if self.eat_keyword(&~"if") { guard = Some(self.parse_expr()); } self.expect(token::FAT_ARROW); let expr = self.parse_expr_res(RESTRICT_STMT_EXPR); @@ -2210,8 +2214,8 @@ pub impl Parser { } copy tok => { if !is_ident_or_path(tok) - || self.is_keyword(~"true") - || self.is_keyword(~"false") + || self.is_keyword(&~"true") + || self.is_keyword(&~"false") { let val = self.parse_expr_res(RESTRICT_NO_BAR_OP); if self.eat(token::DOTDOT) { @@ -2220,10 +2224,10 @@ pub impl Parser { } else { pat = pat_lit(val); } - } else if self.eat_keyword(~"ref") { + } else if self.eat_keyword(&~"ref") { let mutbl = self.parse_mutability(); pat = self.parse_pat_ident(refutable, bind_by_ref(mutbl)); - } else if self.eat_keyword(~"copy") { + } else if self.eat_keyword(&~"copy") { pat = self.parse_pat_ident(refutable, bind_by_copy); } else { // XXX---refutable match bindings should work same as let @@ -2355,7 +2359,7 @@ pub impl Parser { } fn parse_let() -> @decl { - let is_mutbl = self.eat_keyword(~"mut"); + let is_mutbl = self.eat_keyword(&~"mut"); let lo = self.span.lo; let mut locals = ~[self.parse_local(is_mutbl, true)]; while self.eat(token::COMMA) { @@ -2368,7 +2372,7 @@ pub impl Parser { fn parse_instance_var(pr: visibility) -> @struct_field { let mut is_mutbl = struct_immutable; let lo = self.span.lo; - if self.eat_keyword(~"mut") { + if self.eat_keyword(&~"mut") { is_mutbl = struct_mutable; } if !is_plain_ident(*self.token) { @@ -2395,9 +2399,9 @@ pub impl Parser { } let lo = self.span.lo; - if self.is_keyword(~"let") { + if self.is_keyword(&~"let") { check_expected_item(self, first_item_attrs); - self.expect_keyword(~"let"); + self.expect_keyword(&~"let"); let decl = self.parse_let(); return @spanned(lo, decl.span.hi, stmt_decl(decl, self.get_id())); } else if is_ident(*self.token) @@ -2491,7 +2495,7 @@ pub impl Parser { } let lo = self.span.lo; - if self.eat_keyword(~"unsafe") { + if self.eat_keyword(&~"unsafe") { self.obsolete(*self.span, ObsoleteUnsafeBlock); } self.expect(token::LBRACE); @@ -2629,9 +2633,9 @@ pub impl Parser { } fn parse_optional_purity() -> ast::purity { - if self.eat_keyword(~"pure") { + if self.eat_keyword(&~"pure") { ast::pure_fn - } else if self.eat_keyword(~"unsafe") { + } else if self.eat_keyword(&~"unsafe") { ast::unsafe_fn } else { ast::impure_fn @@ -2639,7 +2643,7 @@ pub impl Parser { } fn parse_optional_onceness() -> ast::Onceness { - if self.eat_keyword(~"once") { ast::Once } else { ast::Many } + if self.eat_keyword(&~"once") { ast::Once } else { ast::Many } } fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] { @@ -2647,7 +2651,7 @@ pub impl Parser { if self.eat(token::COLON) { loop { if self.eat(token::BINOP(token::AND)) { - if self.eat_keyword(~"static") { + if self.eat_keyword(&~"static") { bounds.push(RegionTyParamBound); } else { self.span_err(*self.span, @@ -2760,10 +2764,10 @@ pub impl Parser { fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_, p: Parser) -> ast::self_ty_ { // We need to make sure it isn't a mode or a type - if p.token_is_keyword(~"self", p.look_ahead(1)) || - ((p.token_is_keyword(~"const", p.look_ahead(1)) || - p.token_is_keyword(~"mut", p.look_ahead(1))) && - p.token_is_keyword(~"self", p.look_ahead(2))) { + if p.token_is_keyword(&~"self", p.look_ahead(1)) || + ((p.token_is_keyword(&~"const", p.look_ahead(1)) || + p.token_is_keyword(&~"mut", p.look_ahead(1))) && + p.token_is_keyword(&~"self", p.look_ahead(2))) { p.bump(); let mutability = p.parse_mutability(); @@ -2975,7 +2979,7 @@ pub impl Parser { let mut ty = self.parse_ty(false); // Parse traits, if necessary. - let opt_trait = if self.eat_keyword(~"for") { + let opt_trait = if self.eat_keyword(&~"for") { // New-style trait. Reinterpret the type as a trait. let opt_trait_ref = match ty.node { ty_path(path, node_id) => { @@ -3184,11 +3188,11 @@ pub impl Parser { let attrs = self.parse_outer_attributes(); - if self.eat_keyword(~"priv") { + if self.eat_keyword(&~"priv") { return members(~[self.parse_single_class_item(private)]) } - if self.eat_keyword(~"pub") { + if self.eat_keyword(&~"pub") { return members(~[self.parse_single_class_item(public)]); } @@ -3196,7 +3200,7 @@ pub impl Parser { return members(~[]); } - if self.eat_keyword(~"drop") { + if self.eat_keyword(&~"drop") { return self.parse_dtor(attrs); } else { @@ -3205,12 +3209,12 @@ pub impl Parser { } fn parse_visibility() -> visibility { - if self.eat_keyword(~"pub") { public } - else if self.eat_keyword(~"priv") { private } + if self.eat_keyword(&~"pub") { public } + else if self.eat_keyword(&~"priv") { private } else { inherited } } fn parse_staticness() -> bool { - self.eat_keyword(~"static") + self.eat_keyword(&~"static") } // given a termination token and a vector of already-parsed @@ -3412,7 +3416,7 @@ pub impl Parser { fn parse_item_foreign_const(vis: ast::visibility, +attrs: ~[attribute]) -> @foreign_item { let lo = self.span.lo; - self.expect_keyword(~"const"); + self.expect_keyword(&~"const"); let ident = self.parse_ident(); self.expect(token::COLON); let ty = self.parse_ty(false); @@ -3427,12 +3431,12 @@ pub impl Parser { } fn parse_fn_purity() -> purity { - if self.eat_keyword(~"fn") { impure_fn } - else if self.eat_keyword(~"pure") { - self.expect_keyword(~"fn"); + if self.eat_keyword(&~"fn") { impure_fn } + else if self.eat_keyword(&~"pure") { + self.expect_keyword(&~"fn"); pure_fn - } else if self.eat_keyword(~"unsafe") { - self.expect_keyword(~"fn"); + } else if self.eat_keyword(&~"unsafe") { + self.expect_keyword(&~"fn"); unsafe_fn } else { self.unexpected(); } @@ -3440,7 +3444,7 @@ pub impl Parser { fn parse_foreign_item(+attrs: ~[attribute]) -> @foreign_item { let vis = self.parse_visibility(); - if self.is_keyword(~"const") { + if self.is_keyword(&~"const") { self.parse_item_foreign_const(vis, attrs) } else { self.parse_item_foreign_fn(attrs) @@ -3496,9 +3500,9 @@ pub impl Parser { } let mut must_be_named_mod = false; - if self.is_keyword(~"mod") { + if self.is_keyword(&~"mod") { must_be_named_mod = true; - self.expect_keyword(~"mod"); + self.expect_keyword(&~"mod"); } else if *self.token != token::LBRACE { self.span_fatal(*self.span, fmt!("expected `{` or `mod` but found %s", @@ -3633,7 +3637,7 @@ pub impl Parser { let vlo = self.span.lo; // Is this a common field declaration? - if self.eat_keyword(~"struct") { + if self.eat_keyword(&~"struct") { if common_fields.is_some() { self.fatal(~"duplicate declaration of shared fields"); } @@ -3647,7 +3651,7 @@ pub impl Parser { // Is this a nested enum declaration? let ident, needs_comma, kind; let mut args = ~[], disr_expr = None; - if self.eat_keyword(~"enum") { + if self.eat_keyword(&~"enum") { ident = self.parse_ident(); self.expect(token::LBRACE); let nested_enum_def = self.parse_enum_def(ty_params); @@ -3778,57 +3782,57 @@ pub impl Parser { let lo = self.span.lo; let visibility; - if self.eat_keyword(~"pub") { + if self.eat_keyword(&~"pub") { visibility = public; - } else if self.eat_keyword(~"priv") { + } else if self.eat_keyword(&~"priv") { visibility = private; } else { visibility = inherited; } - if items_allowed && self.eat_keyword(~"const") { + if items_allowed && self.eat_keyword(&~"const") { // CONST ITEM let (ident, item_, extra_attrs) = self.parse_item_const(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if foreign_items_allowed && self.is_keyword(~"const") { + } else if foreign_items_allowed && self.is_keyword(&~"const") { // FOREIGN CONST ITEM let item = self.parse_item_foreign_const(visibility, attrs); return iovi_foreign_item(item); } else if items_allowed && // FUNCTION ITEM (not sure about lookahead condition...) - self.is_keyword(~"fn") && + self.is_keyword(&~"fn") && !self.fn_expr_lookahead(self.look_ahead(1u)) { self.bump(); let (ident, item_, extra_attrs) = self.parse_item_fn(impure_fn); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(~"pure") { + } else if items_allowed && self.eat_keyword(&~"pure") { // PURE FUNCTION ITEM - self.expect_keyword(~"fn"); + self.expect_keyword(&~"fn"); let (ident, item_, extra_attrs) = self.parse_item_fn(pure_fn); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); } else if foreign_items_allowed && - (self.is_keyword(~"fn") || self.is_keyword(~"pure") || - self.is_keyword(~"unsafe")) { + (self.is_keyword(&~"fn") || self.is_keyword(&~"pure") || + self.is_keyword(&~"unsafe")) { // FOREIGN FUNCTION ITEM (no items allowed) let item = self.parse_item_foreign_fn(attrs); return iovi_foreign_item(item); - } else if items_allowed && self.is_keyword(~"unsafe") + } else if items_allowed && self.is_keyword(&~"unsafe") && self.look_ahead(1u) != token::LBRACE { // UNSAFE FUNCTION ITEM (where items are allowed) self.bump(); - self.expect_keyword(~"fn"); + self.expect_keyword(&~"fn"); let (ident, item_, extra_attrs) = self.parse_item_fn(unsafe_fn); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if self.eat_keyword(~"extern") { - if items_allowed && self.eat_keyword(~"fn") { + } else if self.eat_keyword(&~"extern") { + if items_allowed && self.eat_keyword(&~"fn") { // EXTERN FUNCTION ITEM let (ident, item_, extra_attrs) = self.parse_item_fn(extern_fn); @@ -3840,43 +3844,43 @@ pub impl Parser { // EXTERN MODULE ITEM return self.parse_item_foreign_mod(lo, visibility, attrs, items_allowed); - } else if items_allowed && self.eat_keyword(~"mod") { + } else if items_allowed && self.eat_keyword(&~"mod") { // MODULE ITEM 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))); - } else if items_allowed && self.eat_keyword(~"type") { + } else if items_allowed && self.eat_keyword(&~"type") { // TYPE ITEM let (ident, item_, extra_attrs) = self.parse_item_type(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(~"enum") { + } else if items_allowed && self.eat_keyword(&~"enum") { // ENUM ITEM let (ident, item_, extra_attrs) = self.parse_item_enum(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(~"trait") { + } else if items_allowed && self.eat_keyword(&~"trait") { // TRAIT ITEM let (ident, item_, extra_attrs) = self.parse_item_trait(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(~"impl") { + } else if items_allowed && self.eat_keyword(&~"impl") { // IMPL ITEM let (ident, item_, extra_attrs) = self.parse_item_impl(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(~"struct") { + } else if items_allowed && self.eat_keyword(&~"struct") { // STRUCT ITEM let (ident, item_, extra_attrs) = self.parse_item_struct(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if self.eat_keyword(~"use") { + } else if self.eat_keyword(&~"use") { // USE ITEM let view_item = self.parse_use(); self.expect(token::SEMI); @@ -3959,7 +3963,7 @@ pub impl Parser { let lo = self.span.lo; let namespace; - if self.eat_keyword(~"mod") { + if self.eat_keyword(&~"mod") { namespace = module_ns; } else { namespace = type_value_ns; @@ -4053,25 +4057,25 @@ pub impl Parser { fn is_view_item() -> bool { let tok, next_tok; - if !self.is_keyword(~"pub") && !self.is_keyword(~"priv") { + if !self.is_keyword(&~"pub") && !self.is_keyword(&~"priv") { tok = *self.token; next_tok = self.look_ahead(1); } else { tok = self.look_ahead(1); next_tok = self.look_ahead(2); }; - self.token_is_keyword(~"use", tok) - || (self.token_is_keyword(~"extern", tok) && - self.token_is_keyword(~"mod", next_tok)) + self.token_is_keyword(&~"use", tok) + || (self.token_is_keyword(&~"extern", tok) && + self.token_is_keyword(&~"mod", next_tok)) } // parse a view item. fn parse_view_item(+attrs: ~[attribute], vis: visibility) -> @view_item { let lo = self.span.lo; - let node = if self.eat_keyword(~"use") { + let node = if self.eat_keyword(&~"use") { self.parse_use() - } else if self.eat_keyword(~"extern") { - self.expect_keyword(~"mod"); + } else if self.eat_keyword(&~"extern") { + self.expect_keyword(&~"mod"); let ident = self.parse_ident(); let metadata = self.parse_optional_meta(); view_item_extern_mod(ident, metadata, self.get_id()) -- cgit 1.4.1-3-g733a5 From 4650da5888427b60c6d38308bcdd76da40f1b181 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 09:54:41 -0800 Subject: libsyntax: change eat to take a &token --- src/libsyntax/ext/base.rs | 2 +- src/libsyntax/parse/common.rs | 4 +-- src/libsyntax/parse/parser.rs | 76 +++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 41 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 8d99c43d43d..b7641e8b19b 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -341,7 +341,7 @@ pub fn get_exprs_from_tts(cx: ext_ctxt, tts: &[ast::token_tree]) let mut es = ~[]; while *p.token != token::EOF { if es.len() != 0 { - p.eat(token::COMMA); + p.eat(&token::COMMA); } es.push(p.parse_expr()); } diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 0317ae14e8c..e336b2bb814 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -103,8 +103,8 @@ pub impl Parser { // consume token 'tok' if it exists. Returns true if the given // token was present, false otherwise. - fn eat(tok: token::Token) -> bool { - return if *self.token == tok { self.bump(); true } else { false }; + fn eat(tok: &token::Token) -> bool { + return if *self.token == *tok { self.bump(); true } else { false }; } // Storing keywords as interned idents instead of strings would be nifty. diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ffc8a28a545..a6834d1b886 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -417,7 +417,7 @@ pub impl Parser { Lifetimes */ - if self.eat(token::LT) { + if self.eat(&token::LT) { let _lifetimes = self.parse_lifetimes(); self.expect(token::GT); } @@ -525,9 +525,9 @@ pub impl Parser { } fn parse_ret_ty() -> (ret_style, @Ty) { - return if self.eat(token::RARROW) { + return if self.eat(&token::RARROW) { let lo = self.span.lo; - if self.eat(token::NOT) { + if self.eat(&token::NOT) { ( noreturn, @Ty { @@ -719,12 +719,12 @@ pub impl Parser { } fn parse_arg_mode() -> mode { - if self.eat(token::BINOP(token::MINUS)) { + if self.eat(&token::BINOP(token::MINUS)) { expl(by_copy) // NDM outdated syntax - } else if self.eat(token::ANDAND) { + } else if self.eat(&token::ANDAND) { expl(by_ref) - } else if self.eat(token::BINOP(token::PLUS)) { - if self.eat(token::BINOP(token::PLUS)) { + } else if self.eat(&token::BINOP(token::PLUS)) { + if self.eat(&token::BINOP(token::PLUS)) { expl(by_val) } else { expl(by_copy) @@ -806,7 +806,7 @@ pub impl Parser { let m = p.parse_arg_mode(); let is_mutbl = self.eat_keyword(&~"mut"); let pat = p.parse_pat(false); - let t = if p.eat(token::COLON) { + let t = if p.eat(&token::COLON) { p.parse_ty(false) } else { @Ty { @@ -826,7 +826,7 @@ pub impl Parser { } fn maybe_parse_fixed_vstore_with_star() -> Option { - if self.eat(token::BINOP(token::STAR)) { + if self.eat(&token::BINOP(token::STAR)) { match *self.token { token::LIT_INT_UNSUFFIXED(i) if i >= 0i64 => { self.bump(); @@ -884,7 +884,7 @@ pub impl Parser { maybe_whole!(self, nt_path); let lo = self.span.lo; - let global = self.eat(token::MOD_SEP); + let global = self.eat(&token::MOD_SEP); let mut ids = ~[]; loop { let is_not_last = @@ -917,7 +917,7 @@ pub impl Parser { maybe_whole!(self, nt_path); let lo = self.span.lo; let path = self.parse_path_without_tps(); - if colons && !self.eat(token::MOD_SEP) { + if colons && !self.eat(&token::MOD_SEP) { return path; } @@ -940,7 +940,7 @@ pub impl Parser { // Parse any lifetime or type parameters which may appear: let tps = { - if !self.eat(token::LT) { + if !self.eat(&token::LT) { ~[] } else { // First consume lifetimes. @@ -1251,7 +1251,7 @@ pub impl Parser { self.expect(token::COMMA); - if self.eat(token::DOTDOT) { + if self.eat(&token::DOTDOT) { base = Some(self.parse_expr()); break; } @@ -1302,12 +1302,12 @@ pub impl Parser { let mut hi; loop { // expr.f - if self.eat(token::DOT) { + if self.eat(&token::DOT) { match *self.token { token::IDENT(i, _) => { hi = self.span.hi; self.bump(); - let tys = if self.eat(token::MOD_SEP) { + let tys = if self.eat(&token::MOD_SEP) { self.expect(token::LT); self.parse_seq_to_gt(Some(token::COMMA), |p| p.parse_ty(false)) @@ -1960,7 +1960,7 @@ pub impl Parser { if require_comma { self.expect(token::COMMA); } else { - self.eat(token::COMMA); + self.eat(&token::COMMA); } let blk = codemap::spanned { @@ -2218,7 +2218,7 @@ pub impl Parser { || self.is_keyword(&~"false") { let val = self.parse_expr_res(RESTRICT_NO_BAR_OP); - if self.eat(token::DOTDOT) { + if self.eat(&token::DOTDOT) { let end = self.parse_expr_res(RESTRICT_NO_BAR_OP); pat = pat_range(val, end); } else { @@ -2246,7 +2246,7 @@ pub impl Parser { if is_plain_ident(*self.token) && cannot_be_enum_or_struct { let name = self.parse_value_path(); let sub; - if self.eat(token::AT) { + if self.eat(&token::AT) { sub = Some(self.parse_pat(refutable)); } else { sub = None; @@ -2315,7 +2315,7 @@ pub impl Parser { ~"expected identifier, found path"); } let name = self.parse_value_path(); - let sub = if self.eat(token::AT) { + let sub = if self.eat(&token::AT) { Some(self.parse_pat(refutable)) } else { None }; @@ -2343,7 +2343,7 @@ pub impl Parser { node: ty_infer, span: mk_sp(lo, lo), }; - if self.eat(token::COLON) { ty = self.parse_ty(false); } + if self.eat(&token::COLON) { ty = self.parse_ty(false); } let init = if allow_init { self.parse_initializer() } else { None }; @spanned( lo, @@ -2362,7 +2362,7 @@ pub impl Parser { let is_mutbl = self.eat_keyword(&~"mut"); let lo = self.span.lo; let mut locals = ~[self.parse_local(is_mutbl, true)]; - while self.eat(token::COMMA) { + while self.eat(&token::COMMA) { locals.push(self.parse_local(is_mutbl, true)); } return @spanned(lo, self.last_span.hi, decl_local(locals)); @@ -2648,9 +2648,9 @@ pub impl Parser { fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] { let mut bounds = ~[]; - if self.eat(token::COLON) { + if self.eat(&token::COLON) { loop { - if self.eat(token::BINOP(token::AND)) { + if self.eat(&token::BINOP(token::AND)) { if self.eat_keyword(&~"static") { bounds.push(RegionTyParamBound); } else { @@ -2695,7 +2695,7 @@ pub impl Parser { break; } - if self.eat(token::BINOP(token::PLUS)) { + if self.eat(&token::BINOP(token::PLUS)) { loop; } @@ -2715,7 +2715,7 @@ pub impl Parser { } fn parse_ty_params() -> ~[ty_param] { - if self.eat(token::LT) { + if self.eat(&token::LT) { let _lifetimes = self.parse_lifetimes(); self.parse_seq_to_gt( Some(token::COMMA), @@ -2849,7 +2849,7 @@ pub impl Parser { fn parse_fn_block_decl() -> fn_decl { let inputs_captures = { - if self.eat(token::OROR) { + if self.eat(&token::OROR) { ~[] } else { self.parse_unspanned_seq( @@ -2858,7 +2858,7 @@ pub impl Parser { |p| p.parse_fn_block_arg()) } }; - let output = if self.eat(token::RARROW) { + let output = if self.eat(&token::RARROW) { self.parse_ty(false) } else { @Ty { id: self.get_id(), node: ty_infer, span: *self.span } @@ -2996,7 +2996,7 @@ pub impl Parser { ty = self.parse_ty(false); opt_trait_ref - } else if self.eat(token::COLON) { + } else if self.eat(&token::COLON) { self.obsolete(*self.span, ObsoleteImplSyntax); Some(self.parse_trait_ref()) } else { @@ -3004,9 +3004,9 @@ pub impl Parser { }; let mut meths = ~[]; - if !self.eat(token::SEMI) { + if !self.eat(&token::SEMI) { self.expect(token::LBRACE); - while !self.eat(token::RBRACE) { + while !self.eat(&token::RBRACE) { meths.push(self.parse_method()); } } @@ -3061,7 +3061,7 @@ pub impl Parser { let class_name = self.parse_value_ident(); self.parse_region_param(); let ty_params = self.parse_ty_params(); - if self.eat(token::COLON) { + if self.eat(&token::COLON) { self.obsolete(*self.span, ObsoleteClassTraits); let _ = self.parse_trait_ref_list(token::LBRACE); } @@ -3070,7 +3070,7 @@ pub impl Parser { let mut the_dtor: Option<(blk, ~[attribute], codemap::span)> = None; let is_tuple_like; - if self.eat(token::LBRACE) { + if self.eat(&token::LBRACE) { // It's a record-like struct. is_tuple_like = false; fields = ~[]; @@ -3113,7 +3113,7 @@ pub impl Parser { @spanned(lo, p.span.hi, struct_field_) }; self.expect(token::SEMI); - } else if self.eat(token::SEMI) { + } else if self.eat(&token::SEMI) { // It's a unit-like struct. is_tuple_like = true; fields = ~[]; @@ -3526,7 +3526,7 @@ pub impl Parser { }; // extern mod { ... } - if items_allowed && self.eat(token::LBRACE) { + if items_allowed && self.eat(&token::LBRACE) { let abi; match abi_opt { Some(found_abi) => abi = found_abi, @@ -3578,7 +3578,7 @@ pub impl Parser { } fn parse_region_param() { - if self.eat(token::BINOP(token::SLASH)) { + if self.eat(&token::BINOP(token::SLASH)) { self.expect(token::BINOP(token::AND)); } } @@ -3659,7 +3659,7 @@ pub impl Parser { needs_comma = false; } else { ident = self.parse_value_ident(); - if self.eat(token::LBRACE) { + if self.eat(&token::LBRACE) { // Parse a struct variant. all_nullary = false; kind = struct_variant_kind(self.parse_struct_def()); @@ -3676,7 +3676,7 @@ pub impl Parser { }); } kind = tuple_variant_kind(args); - } else if self.eat(token::EQ) { + } else if self.eat(&token::EQ) { have_disr = true; disr_expr = Some(self.parse_expr()); kind = tuple_variant_kind(args); @@ -3696,7 +3696,7 @@ pub impl Parser { }; variants.push(spanned(vlo, self.last_span.hi, vr)); - if needs_comma && !self.eat(token::COMMA) { break; } + if needs_comma && !self.eat(&token::COMMA) { break; } } self.expect(token::RBRACE); if (have_disr && !all_nullary) { -- cgit 1.4.1-3-g733a5 From f3965829ffd1e2eb4e2b8c4c8f151379f504ed7f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 10:00:25 -0800 Subject: libsyntax: change flip_delimiter to take a &token::Token --- src/libsyntax/parse/parser.rs | 8 ++++---- src/libsyntax/parse/token.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a6834d1b886..969f5cdb436 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1228,7 +1228,7 @@ pub impl Parser { _ => self.fatal(~"expected open delimiter") }; - let ket = token::flip_delimiter(*self.token); + let ket = token::flip_delimiter(&*self.token); let tts = self.parse_unspanned_seq(*self.token, ket, seq_sep_none(), @@ -1433,7 +1433,7 @@ pub impl Parser { } token::LPAREN | token::LBRACE | token::LBRACKET => { // tjc: ?????? - let ket = token::flip_delimiter(*self.token); + let ket = token::flip_delimiter(&*self.token); tt_delim(vec::append( // the open delimiter: ~[parse_any_tt_tok(self)], @@ -1465,7 +1465,7 @@ pub impl Parser { token::LBRACE | token::LPAREN | token::LBRACKET => { self.parse_matcher_subseq(name_idx, *self.token, // tjc: not sure why we need a copy - token::flip_delimiter(*self.token)) + token::flip_delimiter(&*self.token)) } _ => self.fatal(~"expected open delimiter") } @@ -3915,7 +3915,7 @@ pub impl Parser { // eat a matched-delimiter token tree: let tts = match *self.token { token::LPAREN | token::LBRACE => { - let ket = token::flip_delimiter(*self.token); + let ket = token::flip_delimiter(&*self.token); self.parse_unspanned_seq(*self.token, ket, seq_sep_none(), |p| p.parse_token_tree()) diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index f145e433fa7..8e88ac1d525 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -257,8 +257,8 @@ pub pure fn can_begin_expr(t: Token) -> bool { } /// what's the opposite delimiter? -pub fn flip_delimiter(t: token::Token) -> token::Token { - match t { +pub fn flip_delimiter(t: &token::Token) -> token::Token { + match *t { token::LPAREN => token::RPAREN, token::LBRACE => token::RBRACE, token::LBRACKET => token::RBRACKET, -- cgit 1.4.1-3-g733a5 From 3635480b157389ce3e65bb5b9ccd0ced8e21e1c7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 10:20:24 -0800 Subject: libsyntax: change expect to take &token::Token --- src/libsyntax/ext/pipes/parse_proto.rs | 4 +- src/libsyntax/parse/attr.rs | 6 +- src/libsyntax/parse/common.rs | 25 ++++--- src/libsyntax/parse/parser.rs | 131 +++++++++++++++++---------------- 4 files changed, 84 insertions(+), 82 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/pipes/parse_proto.rs b/src/libsyntax/ext/pipes/parse_proto.rs index 66feb7cc753..07db67d3173 100644 --- a/src/libsyntax/ext/pipes/parse_proto.rs +++ b/src/libsyntax/ext/pipes/parse_proto.rs @@ -39,7 +39,7 @@ pub impl proto_parser for parser::Parser { let id = self.parse_ident(); let name = *self.interner.get(id); - self.expect(token::COLON); + self.expect(&token::COLON); let dir = match *self.token { token::IDENT(n, _) => self.interner.get(n), _ => fail!() @@ -79,7 +79,7 @@ pub impl proto_parser for parser::Parser { } else { ~[] }; - self.expect(token::RARROW); + self.expect(&token::RARROW); let next = match *self.token { token::IDENT(_, _) => { diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 87ecf6a9567..e5487eaac38 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -61,15 +61,15 @@ impl parser_attr for Parser { fn parse_attribute(style: ast::attr_style) -> ast::attribute { let lo = self.span.lo; - self.expect(token::POUND); + self.expect(&token::POUND); return self.parse_attribute_naked(style, lo); } fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) -> ast::attribute { - self.expect(token::LBRACKET); + self.expect(&token::LBRACKET); let meta_item = self.parse_meta_item(); - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); let mut hi = self.span.hi; return spanned(lo, hi, ast::attribute_ { style: style, value: meta_item, diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index e336b2bb814..aa4ffb7fc7a 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -64,15 +64,16 @@ pub impl Parser { // expect and consume the token t. Signal an error if // the next token is not t. - fn expect(t: token::Token) { - if *self.token == t { + fn expect(t: &token::Token) { + if *self.token == *t { self.bump(); } else { - let mut s: ~str = ~"expected `"; - s += token_to_str(self.reader, t); - s += ~"` but found `"; - s += token_to_str(self.reader, *self.token); - self.fatal(s + ~"`"); + self.fatal( + fmt!("expected `%s` but found `%s`", + token_to_str(self.reader, *t), + token_to_str(self.reader, *self.token) + ) + ) } } @@ -230,7 +231,7 @@ pub impl Parser { match sep { Some(ref t) => { if first { first = false; } - else { self.expect(*t); } + else { self.expect(t); } } _ => () } @@ -252,7 +253,7 @@ pub impl Parser { fn parse_seq_lt_gt(sep: Option, f: fn(Parser) -> T) -> spanned<~[T]> { let lo = self.span.lo; - self.expect(token::LT); + self.expect(&token::LT); let result = self.parse_seq_to_before_gt::(sep, f); let hi = self.span.hi; self.expect_gt(); @@ -280,7 +281,7 @@ pub impl Parser { match sep.sep { Some(ref t) => { if first { first = false; } - else { self.expect(*t); } + else { self.expect(t); } } _ => () } @@ -297,7 +298,7 @@ pub impl Parser { +ket: token::Token, sep: SeqSep, f: fn(Parser) -> T) -> ~[T] { - self.expect(bra); + self.expect(&bra); let result = self.parse_seq_to_before_end::(ket, sep, f); self.bump(); return result; @@ -308,7 +309,7 @@ pub impl Parser { fn parse_seq(bra: token::Token, ket: token::Token, sep: SeqSep, f: fn(Parser) -> T) -> spanned<~[T]> { let lo = self.span.lo; - self.expect(bra); + self.expect(&bra); let result = self.parse_seq_to_before_end::(ket, sep, f); let hi = self.span.hi; self.bump(); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 969f5cdb436..da7a0e0e4d3 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -419,7 +419,7 @@ pub impl Parser { */ if self.eat(&token::LT) { let _lifetimes = self.parse_lifetimes(); - self.expect(token::GT); + self.expect(&token::GT); } let inputs = self.parse_unspanned_seq( token::LPAREN, token::RPAREN, @@ -512,7 +512,7 @@ pub impl Parser { let lo = self.span.lo; let mutbl = self.parse_mutability(); let id = self.parse_ident(); - self.expect(token::COLON); + self.expect(&token::COLON); let ty = self.parse_ty(false); spanned( lo, @@ -565,7 +565,7 @@ pub impl Parser { // Parses something like "&x" fn parse_region() -> @region { - self.expect(token::BINOP(token::AND)); + self.expect(&token::BINOP(token::AND)); match *self.token { token::IDENT(sid, _) => { @@ -605,7 +605,7 @@ pub impl Parser { } let t = if ts.len() == 1 && !one_tuple { ts[0].node } else { ty_tup(ts) }; - self.expect(token::RPAREN); + self.expect(&token::RPAREN); t } } else if *self.token == token::AT { @@ -627,7 +627,7 @@ pub impl Parser { } ty_rec(elems) } else if *self.token == token::LBRACKET { - self.expect(token::LBRACKET); + self.expect(&token::LBRACKET); let mt = self.parse_mt(); // Parse the `* 3` in `[ int * 3 ]` @@ -635,7 +635,7 @@ pub impl Parser { None => ty_vec(mt), Some(suffix) => ty_fixed_length_vec(mt, suffix) }; - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); t } else if *self.token == token::BINOP(token::AND) { self.bump(); @@ -778,7 +778,7 @@ pub impl Parser { m = self.parse_arg_mode(); is_mutbl = self.eat_keyword(&~"mut"); let pat = self.parse_pat(false); - self.expect(token::COLON); + self.expect(&token::COLON); pat } else { m = infer(self.get_id()); @@ -853,7 +853,7 @@ pub impl Parser { token::LIT_FLOAT_UNSUFFIXED(s) => lit_float_unsuffixed(self.id_to_str(s)), token::LIT_STR(s) => lit_str(self.id_to_str(s)), - token::LPAREN => { self.expect(token::RPAREN); lit_nil }, + token::LPAREN => { self.expect(&token::RPAREN); lit_nil }, _ => { self.unexpected_last(tok); } } } @@ -893,7 +893,7 @@ pub impl Parser { if is_not_last { ids.push(parse_ident(self)); - self.expect(token::MOD_SEP); + self.expect(&token::MOD_SEP); } else { ids.push(parse_last_ident(self)); break; @@ -931,7 +931,8 @@ pub impl Parser { // ought to and have to sort it out later. if *self.token == token::BINOP(token::SLASH) && self.look_ahead(1u) == token::BINOP(token::AND) { - self.expect(token::BINOP(token::SLASH)); + + self.expect(&token::BINOP(token::SLASH)); Some(self.parse_region()) } else { None @@ -1041,7 +1042,7 @@ pub impl Parser { let lo = self.span.lo; let m = self.parse_mutability(); let i = self.parse_ident(); - self.expect(sep); + self.expect(&sep); let e = self.parse_expr(); spanned(lo, e.span.hi, ast::field_ { mutbl: m, ident: i, expr: e }) } @@ -1108,7 +1109,7 @@ pub impl Parser { } } hi = self.span.hi; - self.expect(token::RPAREN); + self.expect(&token::RPAREN); return if es.len() == 1 && !one_tuple { self.mk_expr(lo, self.span.hi, expr_paren(es[0])) @@ -1169,7 +1170,7 @@ pub impl Parser { self.bump(); self.bump(); let count = self.parse_expr(); - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); ex = expr_repeat(first_expr, count, mutbl); } else if *self.token == token::COMMA { // Vector with two or more elements. @@ -1181,19 +1182,19 @@ pub impl Parser { ex = expr_vec(~[first_expr] + remaining_exprs, mutbl); } else { // Vector with one element. - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); ex = expr_vec(~[first_expr], mutbl); } } hi = self.span.hi; } else if self.eat_keyword(&~"log") { - self.expect(token::LPAREN); + self.expect(&token::LPAREN); let lvl = self.parse_expr(); - self.expect(token::COMMA); + self.expect(&token::COMMA); let e = self.parse_expr(); ex = expr_log(ast::log_other, lvl, e); hi = self.span.hi; - self.expect(token::RPAREN); + self.expect(&token::RPAREN); } else if self.eat_keyword(&~"assert") { let e = self.parse_expr(); ex = expr_assert(e); @@ -1249,7 +1250,7 @@ pub impl Parser { break; } - self.expect(token::COMMA); + self.expect(&token::COMMA); if self.eat(&token::DOTDOT) { base = Some(self.parse_expr()); @@ -1264,7 +1265,7 @@ pub impl Parser { } hi = pth.span.hi; - self.expect(token::RBRACE); + self.expect(&token::RBRACE); ex = expr_struct(pth, fields, base); return self.mk_expr(lo, hi, ex); } @@ -1282,7 +1283,7 @@ pub impl Parser { } fn parse_block_expr(lo: BytePos, blk_mode: blk_check_mode) -> @expr { - self.expect(token::LBRACE); + 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)); } @@ -1308,7 +1309,7 @@ pub impl Parser { hi = self.span.hi; self.bump(); let tys = if self.eat(&token::MOD_SEP) { - self.expect(token::LT); + self.expect(&token::LT); self.parse_seq_to_gt(Some(token::COMMA), |p| p.parse_ty(false)) } else { @@ -1355,7 +1356,7 @@ pub impl Parser { self.bump(); let ix = self.parse_expr(); hi = ix.span.hi; - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); e = self.mk_expr(lo, hi, expr_index(e, ix)); } @@ -1480,7 +1481,7 @@ pub impl Parser { let mut ret_val = ~[]; let mut lparens = 0u; - self.expect(bra); + self.expect(&bra); while *self.token != ket || lparens > 0u { if *self.token == token::LPAREN { lparens += 1u; } @@ -1510,7 +1511,7 @@ pub impl Parser { match_seq(ms, sep, zerok, name_idx_lo, *name_idx) } else { let bound_to = self.parse_ident(); - self.expect(token::COLON); + self.expect(&token::COLON); let nt_name = self.parse_ident(); let m = match_nonterminal(bound_to, nt_name, *name_idx); *name_idx += 1u; @@ -1882,7 +1883,7 @@ pub impl Parser { let opt_ident; if is_labeled_loop_header { opt_ident = Some(self.parse_ident()); - self.expect(token::COLON); + self.expect(&token::COLON); } else { opt_ident = None; } @@ -1914,7 +1915,7 @@ pub impl Parser { } fn parse_record_literal() -> expr_ { - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let mut fields = ~[self.parse_field(token::COLON)]; let mut base = None; while *self.token != token::RBRACE { @@ -1929,14 +1930,14 @@ pub impl Parser { break; } - self.expect(token::COMMA); + self.expect(&token::COMMA); if *self.token == token::RBRACE { // record ends by an optional trailing comma break; } fields.push(self.parse_field(token::COLON)); } - self.expect(token::RBRACE); + self.expect(&token::RBRACE); self.warn(~"REC"); return expr_rec(fields, base); } @@ -1944,13 +1945,13 @@ pub impl Parser { fn parse_match_expr() -> @expr { let lo = self.last_span.lo; let discriminant = self.parse_expr(); - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let mut arms: ~[arm] = ~[]; while *self.token != token::RBRACE { let pats = self.parse_pats(); let mut guard = None; if self.eat_keyword(&~"if") { guard = Some(self.parse_expr()); } - self.expect(token::FAT_ARROW); + self.expect(&token::FAT_ARROW); let expr = self.parse_expr_res(RESTRICT_STMT_EXPR); let require_comma = @@ -1958,7 +1959,7 @@ pub impl Parser { && *self.token != token::RBRACE; if require_comma { - self.expect(token::COMMA); + self.expect(&token::COMMA); } else { self.eat(&token::COMMA); } @@ -2029,7 +2030,7 @@ pub impl Parser { while *self.token != token::RBRACKET { if first { first = false; } - else { self.expect(token::COMMA); } + else { self.expect(&token::COMMA); } let mut is_tail = false; if *self.token == token::DOTDOT { @@ -2061,7 +2062,7 @@ pub impl Parser { let mut first = true; while *self.token != token::RBRACE { if first { first = false; } - else { self.expect(token::COMMA); } + else { self.expect(&token::COMMA); } if *self.token == token::UNDERSCORE { self.bump(); @@ -2199,9 +2200,9 @@ pub impl Parser { fields.push(self.parse_pat(refutable)); } } - if fields.len() == 1 { self.expect(token::COMMA); } + if fields.len() == 1 { self.expect(&token::COMMA); } hi = self.span.hi; - self.expect(token::RPAREN); + self.expect(&token::RPAREN); pat = pat_tup(fields); } } @@ -2209,7 +2210,7 @@ pub impl Parser { self.bump(); let (elements, tail) = self.parse_pat_vec_elements(refutable); hi = self.span.hi; - self.expect(token::RBRACKET); + self.expect(&token::RBRACKET); pat = ast::pat_vec(elements, tail); } copy tok => { @@ -2271,7 +2272,7 @@ pub impl Parser { // This is a "top constructor only" pat self.bump(); self.bump(); star_pat = true; - self.expect(token::RPAREN); + self.expect(&token::RPAREN); } _ => { args = self.parse_unspanned_seq( @@ -2379,7 +2380,7 @@ pub impl Parser { self.fatal(~"expected ident"); } let name = self.parse_ident(); - self.expect(token::COLON); + self.expect(&token::COLON); let ty = self.parse_ty(false); @spanned(lo, self.last_span.hi, ast::struct_field_ { kind: named_field(name, is_mutbl, pr), @@ -2498,7 +2499,7 @@ pub impl Parser { if self.eat_keyword(&~"unsafe") { self.obsolete(*self.span, ObsoleteUnsafeBlock); } - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let (inner, next) = maybe_parse_inner_attrs_and_next(self, parse_attrs); return (inner, self.parse_block_tail_(lo, default_blk, next)); @@ -2603,7 +2604,7 @@ pub impl Parser { stmts.push(stmt); if classify::stmt_ends_with_semi(*stmt) { - self.expect(token::SEMI); + self.expect(&token::SEMI); } } } @@ -2778,7 +2779,7 @@ pub impl Parser { } } - self.expect(token::LPAREN); + self.expect(&token::LPAREN); // A bit of complexity and lookahead is needed here in order to to be // backwards compatible. @@ -2831,7 +2832,7 @@ pub impl Parser { parse_arg_fn); } - self.expect(token::RPAREN); + self.expect(&token::RPAREN); let hi = self.span.hi; @@ -3005,7 +3006,7 @@ pub impl Parser { let mut meths = ~[]; if !self.eat(&token::SEMI) { - self.expect(token::LBRACE); + self.expect(&token::LBRACE); while !self.eat(&token::RBRACE) { meths.push(self.parse_method()); } @@ -3112,7 +3113,7 @@ pub impl Parser { }; @spanned(lo, p.span.hi, struct_field_) }; - self.expect(token::SEMI); + self.expect(&token::SEMI); } else if self.eat(&token::SEMI) { // It's a unit-like struct. is_tuple_like = true; @@ -3270,11 +3271,11 @@ pub impl Parser { fn parse_item_const() -> item_info { let id = self.parse_value_ident(); - self.expect(token::COLON); + self.expect(&token::COLON); let ty = self.parse_ty(false); - self.expect(token::EQ); + self.expect(&token::EQ); let e = self.parse_expr(); - self.expect(token::SEMI); + self.expect(&token::SEMI); (id, item_const(ty, e), None) } @@ -3288,10 +3289,10 @@ pub impl Parser { (id, m, Some(attrs)) } else { self.push_mod_path(id, outer_attrs); - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let (inner, next) = self.parse_inner_attrs_and_next(); let m = self.parse_mod_items(token::RBRACE, next); - self.expect(token::RBRACE); + self.expect(&token::RBRACE); self.pop_mod_path(); (id, item_mod(m), Some(inner)) }; @@ -3404,7 +3405,7 @@ pub impl Parser { let (ident, tps) = self.parse_fn_header(); let decl = self.parse_fn_decl(|p| p.parse_arg()); let mut hi = self.span.hi; - self.expect(token::SEMI); + self.expect(&token::SEMI); @ast::foreign_item { ident: ident, attrs: attrs, node: foreign_item_fn(decl, purity, tps), @@ -3418,10 +3419,10 @@ pub impl Parser { let lo = self.span.lo; self.expect_keyword(&~"const"); let ident = self.parse_ident(); - self.expect(token::COLON); + self.expect(&token::COLON); let ty = self.parse_ty(false); let hi = self.span.hi; - self.expect(token::SEMI); + self.expect(&token::SEMI); @ast::foreign_item { ident: ident, attrs: attrs, node: foreign_item_const(ty), @@ -3535,7 +3536,7 @@ pub impl Parser { let (inner, next) = self.parse_inner_attrs_and_next(); let m = self.parse_foreign_mod_items(sort, abi, next); - self.expect(token::RBRACE); + self.expect(&token::RBRACE); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_foreign_mod(m), visibility, @@ -3552,7 +3553,7 @@ pub impl Parser { // extern mod foo; let metadata = self.parse_optional_meta(); - self.expect(token::SEMI); + self.expect(&token::SEMI); iovi_view_item(@ast::view_item { node: view_item_extern_mod(ident, metadata, self.get_id()), attrs: attrs, @@ -3571,15 +3572,15 @@ pub impl Parser { let (_, ident) = self.parse_type_decl(); self.parse_region_param(); let tps = self.parse_ty_params(); - self.expect(token::EQ); + self.expect(&token::EQ); let ty = self.parse_ty(false); - self.expect(token::SEMI); + self.expect(&token::SEMI); (ident, item_ty(ty, tps), None) } fn parse_region_param() { if self.eat(&token::BINOP(token::SLASH)) { - self.expect(token::BINOP(token::AND)); + self.expect(&token::BINOP(token::AND)); } } @@ -3641,7 +3642,7 @@ pub impl Parser { if common_fields.is_some() { self.fatal(~"duplicate declaration of shared fields"); } - self.expect(token::LBRACE); + self.expect(&token::LBRACE); common_fields = Some(self.parse_struct_def()); loop; } @@ -3653,7 +3654,7 @@ pub impl Parser { let mut args = ~[], disr_expr = None; if self.eat_keyword(&~"enum") { ident = self.parse_ident(); - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let nested_enum_def = self.parse_enum_def(ty_params); kind = enum_variant_kind(nested_enum_def); needs_comma = false; @@ -3698,7 +3699,7 @@ pub impl Parser { if needs_comma && !self.eat(&token::COMMA) { break; } } - self.expect(token::RBRACE); + self.expect(&token::RBRACE); if (have_disr && !all_nullary) { self.fatal(~"discriminator values can only be used with a c-like \ enum"); @@ -3715,7 +3716,7 @@ pub impl Parser { if *self.token == token::EQ { self.bump(); let ty = self.parse_ty(false); - self.expect(token::SEMI); + self.expect(&token::SEMI); let variant = spanned(ty.span.lo, ty.span.hi, ast::variant_ { name: id, attrs: ~[], @@ -3737,7 +3738,7 @@ pub impl Parser { None ); } - self.expect(token::LBRACE); + self.expect(&token::LBRACE); let enum_definition = self.parse_enum_def(ty_params); (id, item_enum(enum_definition, ty_params), None) @@ -3883,7 +3884,7 @@ pub impl Parser { } else if self.eat_keyword(&~"use") { // USE ITEM let view_item = self.parse_use(); - self.expect(token::SEMI); + self.expect(&token::SEMI); return iovi_view_item(@ast::view_item { node: view_item, attrs: attrs, @@ -3902,7 +3903,7 @@ pub impl Parser { // item macro. let pth = self.parse_path_without_tps(); - self.expect(token::NOT); + self.expect(&token::NOT); // a 'special' identifier (like what `macro_rules!` uses) // is optional. We should eventually unify invoc syntax @@ -4082,7 +4083,7 @@ pub impl Parser { } else { self.bug(~"expected view item"); }; - self.expect(token::SEMI); + self.expect(&token::SEMI); @ast::view_item { node: node, attrs: attrs, vis: vis, -- cgit 1.4.1-3-g733a5 From 272c25e9383be490c5fc67cb09773fe2563ef3a7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 15:41:54 -0800 Subject: libsyntax: minor cleanup --- src/libsyntax/parse/attr.rs | 9 +- src/libsyntax/parse/common.rs | 133 +++++++++++++++-------- src/libsyntax/parse/parser.rs | 242 ++++++++++++++++++++++++++---------------- 3 files changed, 246 insertions(+), 138 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index e5487eaac38..f7b115912da 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -151,9 +151,12 @@ impl parser_attr for Parser { } fn parse_meta_seq() -> ~[@ast::meta_item] { - return self.parse_seq(token::LPAREN, token::RPAREN, - seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_meta_item()).node; + self.parse_seq( + token::LPAREN, + token::RPAREN, + seq_sep_trailing_disallowed(token::COMMA), + |p| p.parse_meta_item() + ).node } fn parse_optional_meta() -> ~[@ast::meta_item] { diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index aa4ffb7fc7a..c7f029e9733 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -29,20 +29,20 @@ pub struct SeqSep { pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep { SeqSep { - sep: option::Some(t), - trailing_sep_allowed: false + sep: Some(t), + trailing_sep_allowed: false, } } pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep { SeqSep { - sep: option::Some(t), - trailing_sep_allowed: true + sep: Some(t), + trailing_sep_allowed: true, } } pub fn seq_sep_none() -> SeqSep { SeqSep { - sep: option::None, - trailing_sep_allowed: false + sep: None, + trailing_sep_allowed: false, } } @@ -54,12 +54,20 @@ pub impl Parser { fn unexpected_last(t: token::Token) -> ! { self.span_fatal( *self.last_span, - ~"unexpected token: `" + token_to_str(self.reader, t) + ~"`"); + fmt!( + "unexpected token: `%s`", + token_to_str(self.reader, t) + ) + ); } fn unexpected() -> ! { - self.fatal(~"unexpected token: `" - + token_to_str(self.reader, *self.token) + ~"`"); + self.fatal( + fmt!( + "unexpected token: `%s`", + token_to_str(self.reader, *self.token) + ) + ); } // expect and consume the token t. Signal an error if @@ -81,12 +89,23 @@ pub impl Parser { self.check_strict_keywords(); self.check_reserved_keywords(); match *self.token { - token::IDENT(i, _) => { self.bump(); return i; } - token::INTERPOLATED(token::nt_ident(*)) => { self.bug( - ~"ident interpolation not converted to real token"); } - _ => { self.fatal(~"expected ident, found `" - + token_to_str(self.reader, *self.token) - + ~"`"); } + token::IDENT(i, _) => { + self.bump(); + i + } + token::INTERPOLATED(token::nt_ident(*)) => { + self.bug( + ~"ident interpolation not converted to real token" + ); + } + _ => { + self.fatal( + fmt!( + "expected ident, found `%s`", + token_to_str(self.reader, *self.token) + ) + ); + } } } @@ -155,9 +174,13 @@ pub impl Parser { fn expect_keyword(word: &~str) { self.require_keyword(word); if !self.eat_keyword(word) { - self.fatal(~"expected `" + *word + ~"`, found `" + - token_to_str(self.reader, *self.token) + - ~"`"); + self.fatal( + fmt!( + "expected `%s`, found `%s`", + *word, + token_to_str(self.reader, *self.token) + ) + ); } } @@ -177,7 +200,7 @@ pub impl Parser { fn check_strict_keywords_(w: &~str) { if self.is_strict_keyword(w) { - self.fatal(~"found `" + *w + ~"` in ident position"); + self.fatal(fmt!("found `%s` in ident position", *w)); } } @@ -197,7 +220,7 @@ pub impl Parser { fn check_reserved_keywords_(w: &~str) { if self.is_reserved_keyword(w) { - self.fatal(~"`" + *w + ~"` is a reserved keyword"); + self.fatal(fmt!("`%s` is a reserved keyword", *w)); } } @@ -207,9 +230,11 @@ pub impl Parser { if *self.token == token::GT { self.bump(); } else if *self.token == token::BINOP(token::SHR) { - self.replace_token(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); @@ -222,8 +247,10 @@ pub impl Parser { // parse a sequence bracketed by '<' and '>', stopping // before the '>'. - fn parse_seq_to_before_gt(sep: Option, - f: fn(Parser) -> T) -> ~[T] { + fn parse_seq_to_before_gt( + sep: Option, + f: fn(Parser) -> T + ) -> ~[T] { let mut first = true; let mut v = ~[]; while *self.token != token::GT @@ -241,8 +268,10 @@ pub impl Parser { return v; } - fn parse_seq_to_gt(sep: Option, - f: fn(Parser) -> T) -> ~[T] { + fn parse_seq_to_gt( + sep: Option, + f: fn(Parser) -> T + ) -> ~[T] { let v = self.parse_seq_to_before_gt(sep, f); self.expect_gt(); @@ -250,8 +279,10 @@ pub impl Parser { } // parse a sequence bracketed by '<' and '>' - fn parse_seq_lt_gt(sep: Option, - f: fn(Parser) -> T) -> spanned<~[T]> { + fn parse_seq_lt_gt( + sep: Option, + f: fn(Parser) -> T + ) -> spanned<~[T]> { let lo = self.span.lo; self.expect(&token::LT); let result = self.parse_seq_to_before_gt::(sep, f); @@ -263,18 +294,24 @@ pub impl Parser { // parse a sequence, including the closing delimiter. The function // f must consume tokens until reaching the next separator or // closing bracket. - fn parse_seq_to_end(ket: token::Token, sep: SeqSep, - f: fn(Parser) -> T) -> ~[T] { + fn parse_seq_to_end( + ket: token::Token, + sep: SeqSep, + f: fn(Parser) -> T + ) -> ~[T] { let val = self.parse_seq_to_before_end(ket, sep, f); self.bump(); - return val; + val } // parse a sequence, not including the closing delimiter. The function // f must consume tokens until reaching the next separator or // closing bracket. - fn parse_seq_to_before_end(ket: token::Token, sep: SeqSep, - f: fn(Parser) -> T) -> ~[T] { + fn parse_seq_to_before_end( + ket: token::Token, + sep: SeqSep, + f: fn(Parser) -> T + ) -> ~[T] { let mut first: bool = true; let mut v: ~[T] = ~[]; while *self.token != ket { @@ -288,31 +325,37 @@ pub impl Parser { if sep.trailing_sep_allowed && *self.token == ket { break; } v.push(f(self)); } - return v; + v } // parse a sequence, including the closing delimiter. The function // f must consume tokens until reaching the next separator or // closing bracket. - fn parse_unspanned_seq(+bra: token::Token, - +ket: token::Token, - sep: SeqSep, - f: fn(Parser) -> T) -> ~[T] { + fn parse_unspanned_seq( + +bra: token::Token, + +ket: token::Token, + sep: SeqSep, + f: fn(Parser) -> T + ) -> ~[T] { self.expect(&bra); - let result = self.parse_seq_to_before_end::(ket, sep, f); + let result = self.parse_seq_to_before_end(ket, sep, f); self.bump(); - return result; + result } // NB: Do not use this function unless you actually plan to place the // spanned list in the AST. - fn parse_seq(bra: token::Token, ket: token::Token, sep: SeqSep, - f: fn(Parser) -> T) -> spanned<~[T]> { + fn parse_seq( + +bra: token::Token, + +ket: token::Token, + sep: SeqSep, + f: fn(Parser) -> T + ) -> spanned<~[T]> { let lo = self.span.lo; self.expect(&bra); - let result = self.parse_seq_to_before_end::(ket, sep, f); + let result = self.parse_seq_to_before_end(ket, sep, f); let hi = self.span.hi; self.bump(); - return spanned(lo, hi, result); + spanned(lo, hi, result) } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index da7a0e0e4d3..36729dcb5ac 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -422,16 +422,21 @@ pub impl Parser { self.expect(&token::GT); } let inputs = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, + token::LPAREN, + token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_arg_general(false)); + |p| p.parse_arg_general(false) + ); let (ret_style, ret_ty) = self.parse_ret_ty(); ast::fn_decl { inputs: inputs, output: ret_ty, cf: ret_style } } fn parse_trait_methods() -> ~[trait_method] { - do self.parse_unspanned_seq(token::LBRACE, token::RBRACE, - seq_sep_none()) |p| { + do self.parse_unspanned_seq( + token::LBRACE, + token::RBRACE, + seq_sep_none() + ) |p| { let attrs = p.parse_outer_attributes(); let lo = p.span.lo; let is_static = p.parse_staticness(); @@ -619,9 +624,11 @@ pub impl Parser { ty_ptr(self.parse_mt()) } else if *self.token == token::LBRACE { let elems = self.parse_unspanned_seq( - token::LBRACE, token::RBRACE, + token::LBRACE, + token::RBRACE, seq_sep_trailing_allowed(token::COMMA), - |p| p.parse_ty_field()); + |p| p.parse_ty_field() + ); if vec::len(elems) == 0u { self.unexpected_last(token::RBRACE); } @@ -1175,10 +1182,11 @@ pub impl Parser { } else if *self.token == token::COMMA { // Vector with two or more elements. self.bump(); - let remaining_exprs = - self.parse_seq_to_end(token::RBRACKET, - seq_sep_trailing_allowed(token::COMMA), - |p| p.parse_expr()); + let remaining_exprs = self.parse_seq_to_end( + token::RBRACKET, + seq_sep_trailing_allowed(token::COMMA), + |p| p.parse_expr() + ); ex = expr_vec(~[first_expr] + remaining_exprs, mutbl); } else { // Vector with one element. @@ -1230,10 +1238,12 @@ pub impl Parser { }; let ket = token::flip_delimiter(&*self.token); - let tts = self.parse_unspanned_seq(*self.token, - ket, - seq_sep_none(), - |p| p.parse_token_tree()); + let tts = self.parse_unspanned_seq( + *self.token, + ket, + seq_sep_none(), + |p| p.parse_token_tree() + ); let hi = self.span.hi; return self.mk_mac_expr(lo, hi, mac_invoc_tt(pth, tts)); @@ -1310,8 +1320,10 @@ pub impl Parser { self.bump(); let tys = if self.eat(&token::MOD_SEP) { self.expect(&token::LT); - self.parse_seq_to_gt(Some(token::COMMA), - |p| p.parse_ty(false)) + self.parse_seq_to_gt( + Some(token::COMMA), + |p| p.parse_ty(false) + ) } else { ~[] }; @@ -1320,9 +1332,11 @@ pub impl Parser { match *self.token { token::LPAREN if self.permits_call() => { let es = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, + token::LPAREN, + token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_expr()); + |p| p.parse_expr() + ); hi = self.span.hi; let nd = expr_method_call(e, i, tys, es, NoSugar); @@ -1342,9 +1356,11 @@ pub impl Parser { // expr(...) token::LPAREN if self.permits_call() => { let es = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, + token::LPAREN, + token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_expr()); + |p| p.parse_expr() + ); hi = self.span.hi; let nd = expr_call(e, es, NoSugar); @@ -1373,7 +1389,7 @@ pub impl Parser { || *self.token == token::BINOP(token::PLUS) { let zerok = *self.token == token::BINOP(token::STAR); self.bump(); - return (None, zerok); + (None, zerok) } else { let sep = *self.token; self.bump(); @@ -1381,7 +1397,7 @@ pub impl Parser { || *self.token == token::BINOP(token::PLUS) { let zerok = *self.token == token::BINOP(token::STAR); self.bump(); - return (Some(sep), zerok); + (Some(sep), zerok) } else { self.fatal(~"expected `*` or `+`"); } @@ -1397,8 +1413,12 @@ pub impl Parser { match *p.token { token::RPAREN | token::RBRACE | token::RBRACKET => { - p.fatal(~"incorrect close delimiter: `" - + token_to_str(p.reader, *p.token) + ~"`"); + p.fatal( + fmt!( + "incorrect close delimiter: `%s`", + token_to_str(p.reader, *p.token) + ) + ); } /* we ought to allow different depths of unquotation */ token::DOLLAR if *p.quote_depth > 0u => { @@ -1406,9 +1426,12 @@ pub impl Parser { let sp = *p.span; if *p.token == token::LPAREN { - let seq = p.parse_seq(token::LPAREN, token::RPAREN, - seq_sep_none(), - |p| p.parse_token_tree()); + let seq = p.parse_seq( + token::LPAREN, + token::RPAREN, + seq_sep_none(), + |p| p.parse_token_tree() + ); let (s, z) = p.parse_sep_and_zerok(); tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z) } else { @@ -1429,23 +1452,29 @@ pub impl Parser { } match *self.token { - token::EOF => { + token::EOF => { self.fatal(~"file ended in the middle of a macro invocation"); - } - token::LPAREN | token::LBRACE | token::LBRACKET => { - // tjc: ?????? - let ket = token::flip_delimiter(&*self.token); - tt_delim(vec::append( - // the open delimiter: - ~[parse_any_tt_tok(self)], - vec::append( - self.parse_seq_to_before_end( - ket, seq_sep_none(), - |p| p.parse_token_tree()), - // the close delimiter: - ~[parse_any_tt_tok(self)]))) - } - _ => parse_non_delim_tt_tok(self) + } + token::LPAREN | token::LBRACE | token::LBRACKET => { + // tjc: ?????? + let ket = token::flip_delimiter(&*self.token); + tt_delim( + vec::append( + // the open delimiter: + ~[parse_any_tt_tok(self)], + vec::append( + self.parse_seq_to_before_end( + ket, + seq_sep_none(), + |p| p.parse_token_tree() + ), + // the close delimiter: + ~[parse_any_tt_tok(self)] + ) + ) + ) + } + _ => parse_non_delim_tt_tok(self) } } @@ -1462,13 +1491,16 @@ pub impl Parser { // the interpolation of matchers maybe_whole!(self, nt_matchers); let name_idx = @mut 0u; - return match *self.token { - token::LBRACE | token::LPAREN | token::LBRACKET => { - self.parse_matcher_subseq(name_idx, *self.token, - // tjc: not sure why we need a copy - token::flip_delimiter(&*self.token)) - } - _ => self.fatal(~"expected open delimiter") + match *self.token { + token::LBRACE | token::LPAREN | token::LBRACKET => { + self.parse_matcher_subseq( + name_idx, + *self.token, + // tjc: not sure why we need a copy + token::flip_delimiter(&*self.token) + ) + } + _ => self.fatal(~"expected open delimiter") } } @@ -1476,8 +1508,11 @@ pub impl Parser { // This goofy function is necessary to correctly match parens in matchers. // Otherwise, `$( ( )` would be a valid matcher, and `$( () )` would be // invalid. It's similar to common::parse_seq. - fn parse_matcher_subseq(name_idx: @mut uint, bra: token::Token, - ket: token::Token) -> ~[matcher] { + fn parse_matcher_subseq( + name_idx: @mut uint, + bra: token::Token, + ket: token::Token + ) -> ~[matcher] { let mut ret_val = ~[]; let mut lparens = 0u; @@ -1501,9 +1536,11 @@ pub impl Parser { self.bump(); if *self.token == token::LPAREN { let name_idx_lo = *name_idx; - let ms = self.parse_matcher_subseq(name_idx, - token::LPAREN, - token::RPAREN); + let ms = self.parse_matcher_subseq( + name_idx, + token::LPAREN, + token::RPAREN + ); if ms.len() == 0u { self.fatal(~"repetition body must be nonempty"); } @@ -2276,10 +2313,13 @@ pub impl Parser { } _ => { args = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, - seq_sep_trailing_disallowed - (token::COMMA), - |p| p.parse_pat(refutable)); + token::LPAREN, + token::RPAREN, + seq_sep_trailing_disallowed( + token::COMMA + ), + |p| p.parse_pat(refutable) + ); } }, _ => () @@ -2423,8 +2463,11 @@ pub impl Parser { }; let tts = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, seq_sep_none(), - |p| p.parse_token_tree()); + token::LPAREN, + token::RPAREN, + seq_sep_none(), + |p| p.parse_token_tree() + ); let hi = self.span.hi; if id == token::special_idents::invalid { @@ -2720,7 +2763,8 @@ pub impl Parser { let _lifetimes = self.parse_lifetimes(); self.parse_seq_to_gt( Some(token::COMMA), - |p| p.parse_ty_param()) + |p| p.parse_ty_param() + ) } else { ~[] } } @@ -2729,8 +2773,11 @@ pub impl Parser { { let args_or_capture_items: ~[arg_or_capture_item] = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, - seq_sep_trailing_disallowed(token::COMMA), parse_arg_fn); + token::LPAREN, + token::RPAREN, + seq_sep_trailing_disallowed(token::COMMA), + parse_arg_fn + ); let inputs = either::lefts(args_or_capture_items); @@ -2810,10 +2857,11 @@ pub impl Parser { token::COMMA => { self.bump(); let sep = seq_sep_trailing_disallowed(token::COMMA); - args_or_capture_items = - self.parse_seq_to_before_end(token::RPAREN, - sep, - parse_arg_fn); + args_or_capture_items = self.parse_seq_to_before_end( + token::RPAREN, + sep, + parse_arg_fn + ); } token::RPAREN => { args_or_capture_items = ~[]; @@ -2826,10 +2874,11 @@ pub impl Parser { } } else { let sep = seq_sep_trailing_disallowed(token::COMMA); - args_or_capture_items = - self.parse_seq_to_before_end(token::RPAREN, - sep, - parse_arg_fn); + args_or_capture_items = self.parse_seq_to_before_end( + token::RPAREN, + sep, + parse_arg_fn + ); } self.expect(&token::RPAREN); @@ -2854,9 +2903,11 @@ pub impl Parser { ~[] } else { self.parse_unspanned_seq( - token::BINOP(token::OR), token::BINOP(token::OR), + token::BINOP(token::OR), + token::BINOP(token::OR), seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_fn_block_arg()) + |p| p.parse_fn_block_arg() + ) } }; let output = if self.eat(&token::RARROW) { @@ -3054,8 +3105,10 @@ pub impl Parser { fn parse_trait_ref_list(ket: token::Token) -> ~[@trait_ref] { self.parse_seq_to_before_end( - ket, seq_sep_none(), - |p| p.parse_trait_ref()) + ket, + seq_sep_none(), + |p| p.parse_trait_ref() + ) } fn parse_item_struct() -> item_info { @@ -3102,9 +3155,11 @@ pub impl Parser { } else if *self.token == token::LPAREN { // It's a tuple-like struct. is_tuple_like = true; - fields = do self.parse_unspanned_seq(token::LPAREN, token::RPAREN, - seq_sep_trailing_allowed - (token::COMMA)) |p| { + fields = do self.parse_unspanned_seq( + token::LPAREN, + token::RPAREN, + seq_sep_trailing_allowed(token::COMMA) + ) |p| { let lo = p.span.lo; let struct_field_ = ast::struct_field_ { kind: unnamed_field, @@ -3667,9 +3722,11 @@ pub impl Parser { } else if *self.token == token::LPAREN { all_nullary = false; let arg_tys = self.parse_unspanned_seq( - token::LPAREN, token::RPAREN, + token::LPAREN, + token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), - |p| p.parse_ty(false)); + |p| p.parse_ty(false) + ); for arg_tys.each |ty| { args.push(ast::variant_arg { ty: *ty, @@ -3915,13 +3972,16 @@ pub impl Parser { }; // eat a matched-delimiter token tree: let tts = match *self.token { - token::LPAREN | token::LBRACE => { - let ket = token::flip_delimiter(&*self.token); - self.parse_unspanned_seq(*self.token, ket, - seq_sep_none(), - |p| p.parse_token_tree()) - } - _ => self.fatal(~"expected open delimiter") + token::LPAREN | token::LBRACE => { + let ket = token::flip_delimiter(&*self.token); + self.parse_unspanned_seq( + *self.token, + ket, + seq_sep_none(), + |p| p.parse_token_tree() + ) + } + _ => self.fatal(~"expected open delimiter") }; // single-variant-enum... : let m = ast::mac_invoc_tt(pth, tts); @@ -4007,9 +4067,11 @@ pub impl Parser { // foo::bar::{a,b,c} token::LBRACE => { let idents = self.parse_unspanned_seq( - token::LBRACE, token::RBRACE, + token::LBRACE, + token::RBRACE, seq_sep_trailing_allowed(token::COMMA), - |p| p.parse_path_list_ident()); + |p| p.parse_path_list_ident() + ); let path = @ast::path { span: mk_sp(lo, self.span.hi), global: false, idents: path, -- cgit 1.4.1-3-g733a5 From 752befe2a6401108f27ff0141bdd73baac44c41c Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 10:52:06 -0800 Subject: libsyntax: change token_is_{word,keyword} to take &Token --- src/libsyntax/parse/common.rs | 12 ++++++------ src/libsyntax/parse/parser.rs | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index c7f029e9733..91abd804fb3 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -136,24 +136,24 @@ pub impl Parser { } } - fn token_is_word(word: &~str, tok: token::Token) -> bool { - match tok { + fn token_is_word(word: &~str, tok: &token::Token) -> bool { + match *tok { token::IDENT(sid, false) => { *self.id_to_str(sid) == *word } _ => { false } } } - fn token_is_keyword(word: &~str, ++tok: token::Token) -> bool { + fn token_is_keyword(word: &~str, tok: &token::Token) -> bool { self.require_keyword(word); self.token_is_word(word, tok) } fn is_keyword(word: &~str) -> bool { - self.token_is_keyword(word, *self.token) + self.token_is_keyword(word, &*self.token) } - fn is_any_keyword(tok: token::Token) -> bool { - match tok { + fn is_any_keyword(tok: &token::Token) -> bool { + match *tok { token::IDENT(sid, false) => { self.keywords.contains_key(self.id_to_str(sid)) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 36729dcb5ac..7a0f248cd3d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -309,7 +309,7 @@ pub impl Parser { pure fn id_to_str(id: ident) -> @~str { self.sess.interner.get(id) } - fn token_is_closure_keyword(+tok: token::Token) -> bool { + fn token_is_closure_keyword(tok: &token::Token) -> bool { self.token_is_keyword(&~"pure", tok) || self.token_is_keyword(&~"unsafe", tok) || self.token_is_keyword(&~"once", tok) || @@ -649,7 +649,7 @@ pub impl Parser { self.parse_borrowed_pointee() } else if self.eat_keyword(&~"extern") { self.parse_ty_bare_fn() - } else if self.token_is_closure_keyword(*self.token) { + } else if self.token_is_closure_keyword(&*self.token) { self.parse_ty_closure(None, None) } else if *self.token == token::MOD_SEP || is_ident_or_path(*self.token) { @@ -676,12 +676,12 @@ pub impl Parser { token::IDENT(rname, _) => { if self.look_ahead(1u) == token::BINOP(token::SLASH) && - self.token_is_closure_keyword(self.look_ahead(2u)) + self.token_is_closure_keyword(&self.look_ahead(2u)) { self.bump(); self.bump(); return self.parse_ty_closure(Some(sigil), Some(rname)); - } else if self.token_is_closure_keyword(*self.token) { + } else if self.token_is_closure_keyword(&*self.token) { return self.parse_ty_closure(Some(sigil), None); } } @@ -716,7 +716,7 @@ pub impl Parser { _ => { None } }; - if self.token_is_closure_keyword(*self.token) { + if self.token_is_closure_keyword(&*self.token) { return self.parse_ty_closure(Some(BorrowedSigil), rname); } @@ -1912,7 +1912,7 @@ pub impl Parser { // labeled loop headers look like 'loop foo: {' let is_labeled_loop_header = is_ident(*self.token) - && !self.is_any_keyword(*self.token) + && !self.is_any_keyword(&*self.token) && self.look_ahead(1) == token::COLON; if is_loop_header || is_labeled_loop_header { @@ -1946,7 +1946,7 @@ pub impl Parser { fn looking_at_record_literal() -> bool { let lookahead = self.look_ahead(1); *self.token == token::LBRACE && - (self.token_is_keyword(&~"mut", lookahead) || + (self.token_is_keyword(&~"mut", &lookahead) || (is_plain_ident(lookahead) && self.look_ahead(2) == token::COLON)) } @@ -2446,7 +2446,7 @@ pub impl Parser { let decl = self.parse_let(); return @spanned(lo, decl.span.hi, stmt_decl(decl, self.get_id())); } else if is_ident(*self.token) - && !self.is_any_keyword(*self.token) + && !self.is_any_keyword(&*self.token) && self.look_ahead(1) == token::NOT { check_expected_item(self, first_item_attrs); @@ -2812,10 +2812,10 @@ pub impl Parser { fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_, p: Parser) -> ast::self_ty_ { // We need to make sure it isn't a mode or a type - if p.token_is_keyword(&~"self", p.look_ahead(1)) || - ((p.token_is_keyword(&~"const", p.look_ahead(1)) || - p.token_is_keyword(&~"mut", p.look_ahead(1))) && - p.token_is_keyword(&~"self", p.look_ahead(2))) { + if p.token_is_keyword(&~"self", &p.look_ahead(1)) || + ((p.token_is_keyword(&~"const", &p.look_ahead(1)) || + p.token_is_keyword(&~"mut", &p.look_ahead(1))) && + p.token_is_keyword(&~"self", &p.look_ahead(2))) { p.bump(); let mutability = p.parse_mutability(); @@ -3948,7 +3948,7 @@ pub impl Parser { vis: visibility, span: mk_sp(lo, self.last_span.hi) }); - } else if macros_allowed && !self.is_any_keyword(*self.token) + } else if macros_allowed && !self.is_any_keyword(&*self.token) && self.look_ahead(1) == token::NOT && (is_plain_ident(self.look_ahead(2)) || self.look_ahead(2) == token::LPAREN @@ -4127,9 +4127,9 @@ pub impl Parser { tok = self.look_ahead(1); next_tok = self.look_ahead(2); }; - self.token_is_keyword(&~"use", tok) - || (self.token_is_keyword(&~"extern", tok) && - self.token_is_keyword(&~"mod", next_tok)) + self.token_is_keyword(&~"use", &tok) + || (self.token_is_keyword(&~"extern", &tok) && + self.token_is_keyword(&~"mod", &next_tok)) } // parse a view item. -- cgit 1.4.1-3-g733a5 From bff22cf1665e98a1c3feb60e1c23fc30a4120934 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 18:32:02 -0800 Subject: libsyntax: add some explicit copies --- src/libsyntax/ast_map.rs | 15 ++++--- src/libsyntax/ast_util.rs | 4 +- src/libsyntax/ext/pipes/parse_proto.rs | 56 ++++++++++++++++--------- src/libsyntax/ext/tt/macro_parser.rs | 2 +- src/libsyntax/fold.rs | 2 +- src/libsyntax/parse/common.rs | 4 +- src/libsyntax/parse/obsolete.rs | 2 +- src/libsyntax/parse/parser.rs | 77 ++++++++++++++++++---------------- 8 files changed, 94 insertions(+), 68 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs index b56dfeffd7a..48fe0ca5b2d 100644 --- a/src/libsyntax/ast_map.rs +++ b/src/libsyntax/ast_map.rs @@ -60,8 +60,8 @@ pub fn path_to_str_with_sep(p: &[path_elt], sep: ~str, itr: @ident_interner) -> ~str { let strs = do p.map |e| { match *e { - path_mod(s) => *itr.get(s), - path_name(s) => *itr.get(s) + path_mod(s) => copy *itr.get(s), + path_name(s) => copy *itr.get(s) } }; str::connect(strs, sep) @@ -70,7 +70,7 @@ pub fn path_to_str_with_sep(p: &[path_elt], sep: ~str, itr: @ident_interner) pub fn path_ident_to_str(p: path, i: ident, itr: @ident_interner) -> ~str { if vec::is_empty(p) { //FIXME /* FIXME (#2543) */ copy *i - *itr.get(i) + copy *itr.get(i) } else { fmt!("%s::%s", path_to_str(p, itr), *itr.get(i)) } @@ -82,8 +82,8 @@ pub fn path_to_str(p: &[path_elt], itr: @ident_interner) -> ~str { pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str { match pe { - path_mod(s) => *itr.get(s), - path_name(s) => *itr.get(s) + path_mod(s) => copy *itr.get(s), + path_name(s) => copy *itr.get(s) } } @@ -310,7 +310,10 @@ pub fn map_item(i: @item, &&cx: @mut Ctx, v: visit::vt<@mut Ctx>) { for methods.each |tm| { let id = ast_util::trait_method_to_ty_method(tm).id; let d_id = ast_util::local_def(i.id); - cx.map.insert(id, node_trait_method(@*tm, d_id, item_path)); + cx.map.insert( + id, + node_trait_method(@copy *tm, d_id, item_path) + ); } } _ => () diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 7bf08aaaf76..59f25024c82 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -27,7 +27,7 @@ use core::vec; pub pure fn path_name_i(idents: &[ident], intr: @token::ident_interner) -> ~str { // FIXME: Bad copies (#2543 -- same for everything else that says "bad") - str::connect(idents.map(|i| *intr.get(*i)), ~"::") + str::connect(idents.map(|i| copy *intr.get(*i)), ~"::") } @@ -283,7 +283,7 @@ pub fn split_trait_methods(trait_methods: &[trait_method]) let mut reqd = ~[], provd = ~[]; for trait_methods.each |trt_method| { match *trt_method { - required(ref tm) => reqd.push((*tm)), + required(ref tm) => reqd.push(copy *tm), provided(m) => provd.push(m) } }; diff --git a/src/libsyntax/ext/pipes/parse_proto.rs b/src/libsyntax/ext/pipes/parse_proto.rs index 07db67d3173..fae5b1b49af 100644 --- a/src/libsyntax/ext/pipes/parse_proto.rs +++ b/src/libsyntax/ext/pipes/parse_proto.rs @@ -27,10 +27,14 @@ pub impl proto_parser for parser::Parser { fn parse_proto(&self, id: ~str) -> protocol { let proto = protocol(id, *self.span); - self.parse_seq_to_before_end(token::EOF, SeqSep { - sep: None, - trailing_sep_allowed: false - }, |self| self.parse_state(proto)); + self.parse_seq_to_before_end( + token::EOF, + SeqSep { + sep: None, + trailing_sep_allowed: false, + }, + |self| self.parse_state(proto) + ); return proto; } @@ -40,9 +44,9 @@ pub impl proto_parser for parser::Parser { let name = *self.interner.get(id); self.expect(&token::COLON); - let dir = match *self.token { - token::IDENT(n, _) => self.interner.get(n), - _ => fail!() + let dir = match copy *self.token { + token::IDENT(n, _) => self.interner.get(n), + _ => fail!() }; self.bump(); let dir = match dir { @@ -61,21 +65,29 @@ pub impl proto_parser for parser::Parser { // parse the messages self.parse_unspanned_seq( - token::LBRACE, token::RBRACE, SeqSep { + token::LBRACE, + token::RBRACE, + SeqSep { sep: Some(token::COMMA), - trailing_sep_allowed: true - }, |self| self.parse_message(state)); + trailing_sep_allowed: true, + }, + |self| self.parse_message(state) + ); } fn parse_message(&self, state: state) { let mname = *self.interner.get(self.parse_ident()); let args = if *self.token == token::LPAREN { - self.parse_unspanned_seq(token::LPAREN, - token::RPAREN, SeqSep { - sep: Some(token::COMMA), - trailing_sep_allowed: true - }, |p| p.parse_ty(false)) + self.parse_unspanned_seq( + token::LPAREN, + token::RPAREN, + SeqSep { + sep: Some(token::COMMA), + trailing_sep_allowed: true, + }, + |p| p.parse_ty(false) + ) } else { ~[] }; @@ -85,11 +97,15 @@ pub impl proto_parser for parser::Parser { token::IDENT(_, _) => { let name = *self.interner.get(self.parse_ident()); let ntys = if *self.token == token::LT { - self.parse_unspanned_seq(token::LT, - token::GT, SeqSep { - sep: Some(token::COMMA), - trailing_sep_allowed: true - }, |p| p.parse_ty(false)) + self.parse_unspanned_seq( + token::LT, + token::GT, + SeqSep { + sep: Some(token::COMMA), + trailing_sep_allowed: true, + }, + |p| p.parse_ty(false) + ) } else { ~[] }; Some(next_state {state: name, tys: ntys}) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 890420edf6d..713bf9afcd0 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -424,7 +424,7 @@ pub fn parse_nt(p: Parser, name: ~str) -> nonterminal { ~"ident" => match *p.token { token::IDENT(sn,b) => { p.bump(); token::nt_ident(sn,b) } _ => p.fatal(~"expected ident, found " - + token::to_str(p.reader.interner(), *p.token)) + + token::to_str(p.reader.interner(), copy *p.token)) }, ~"path" => token::nt_path(p.parse_path_with_tps(false)), ~"tt" => { diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index e74376afb08..d67596e100f 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -280,7 +280,7 @@ pub fn noop_fold_item_underscore(i: &item_, fld: ast_fold) -> item_ { } item_mac(ref m) => { // FIXME #2888: we might actually want to do something here. - item_mac((*m)) + item_mac(copy *m) } } } diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 91abd804fb3..78527062515 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -27,13 +27,13 @@ pub struct SeqSep { trailing_sep_allowed: bool } -pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep { +pub fn seq_sep_trailing_disallowed(+t: token::Token) -> SeqSep { SeqSep { sep: Some(t), trailing_sep_allowed: false, } } -pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep { +pub fn seq_sep_trailing_allowed(+t: token::Token) -> SeqSep { SeqSep { sep: Some(t), trailing_sep_allowed: true, diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 02c2fb404c2..96ed81e476e 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -198,7 +198,7 @@ pub impl Parser { fn try_parse_obsolete_priv_section() -> bool { if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE { - self.obsolete(*self.span, ObsoletePrivSection); + self.obsolete(copy *self.span, ObsoletePrivSection); self.eat_keyword(&~"priv"); self.bump(); while *self.token != token::RBRACE { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7a0f248cd3d..9fe53fe50e2 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -195,11 +195,10 @@ struct ParsedItemsAndViewItems { /* ident is handled by common.rs */ pub fn Parser(sess: @mut ParseSess, - cfg: ast::crate_cfg, + +cfg: ast::crate_cfg, +rdr: reader) -> Parser { - let tok0 = rdr.next_token(); - let span0 = tok0.sp; + let tok0 = copy rdr.next_token(); let interner = rdr.interner(); Parser { @@ -207,15 +206,15 @@ pub fn Parser(sess: @mut ParseSess, interner: interner, sess: sess, cfg: cfg, - token: @mut tok0.tok, - span: @mut span0, - last_span: @mut span0, - buffer: @mut [TokenAndSpan {tok: tok0.tok, sp: span0}, ..4], + token: @mut copy tok0.tok, + span: @mut copy tok0.sp, + last_span: @mut copy tok0.sp, + buffer: @mut [copy tok0, .. 4], buffer_start: @mut 0, buffer_end: @mut 0, - tokens_consumed: @mut 0u, + tokens_consumed: @mut 0, restriction: @mut UNRESTRICTED, - quote_depth: @mut 0u, + quote_depth: @mut 0, keywords: token::keyword_table(), strict_keywords: token::strict_keyword_table(), reserved_keywords: token::reserved_keyword_table(), @@ -253,20 +252,20 @@ pub struct Parser { pub impl Parser { // advance the parser by one token fn bump() { - *self.last_span = *self.span; + *self.last_span = copy *self.span; let next = if *self.buffer_start == *self.buffer_end { self.reader.next_token() } else { - let next = self.buffer[*self.buffer_start]; + let next = copy self.buffer[*self.buffer_start]; *self.buffer_start = (*self.buffer_start + 1) & 3; next }; - *self.token = next.tok; - *self.span = next.sp; + *self.token = copy next.tok; + *self.span = copy next.sp; *self.tokens_consumed += 1u; } // EFFECT: replace the current token and span with the given one - fn replace_token(next: token::Token, +lo: BytePos, +hi: BytePos) { + fn replace_token(+next: token::Token, +lo: BytePos, +hi: BytePos) { *self.token = next; *self.span = mk_sp(lo, hi); } @@ -461,7 +460,7 @@ pub impl Parser { let hi = p.last_span.hi; debug!("parse_trait_methods(): trait method signature ends in \ `%s`", - token_to_str(p.reader, *p.token)); + token_to_str(p.reader, copy *p.token)); match *p.token { token::SEMI => { p.bump(); @@ -499,8 +498,13 @@ pub impl Parser { }) } - _ => { p.fatal(~"expected `;` or `}` but found `" + - token_to_str(p.reader, *p.token) + ~"`"); + _ => { + p.fatal( + fmt!( + "expected `;` or `}` but found `%s`", + token_to_str(p.reader, copy *p.token) + ) + ); } } } @@ -649,7 +653,7 @@ pub impl Parser { self.parse_borrowed_pointee() } else if self.eat_keyword(&~"extern") { self.parse_ty_bare_fn() - } else if self.token_is_closure_keyword(&*self.token) { + } else if self.token_is_closure_keyword(© *self.token) { self.parse_ty_closure(None, None) } else if *self.token == token::MOD_SEP || is_ident_or_path(*self.token) { @@ -681,7 +685,7 @@ pub impl Parser { self.bump(); self.bump(); return self.parse_ty_closure(Some(sigil), Some(rname)); - } else if self.token_is_closure_keyword(&*self.token) { + } else if self.token_is_closure_keyword(© *self.token) { return self.parse_ty_closure(Some(sigil), None); } } @@ -716,7 +720,7 @@ pub impl Parser { _ => { None } }; - if self.token_is_closure_keyword(&*self.token) { + if self.token_is_closure_keyword(© *self.token) { return self.parse_ty_closure(Some(BorrowedSigil), rname); } @@ -841,9 +845,12 @@ pub impl Parser { } _ => { self.fatal( - fmt!("expected integral vector length \ - but found `%s`", - token_to_str(self.reader, *self.token))); + fmt!( + "expected integral vector length \ + but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } } } else { @@ -873,7 +880,7 @@ pub impl Parser { lit_bool(false) } else { // XXX: This is a really bad copy! - let tok = *self.token; + let tok = copy *self.token; self.bump(); self.lit_from_token(tok) }; @@ -1063,7 +1070,7 @@ pub impl Parser { } } - fn mk_mac_expr(+lo: BytePos, +hi: BytePos, m: mac_) -> @expr { + fn mk_mac_expr(+lo: BytePos, +hi: BytePos, +m: mac_) -> @expr { @expr { id: self.get_id(), callee_id: self.get_id(), @@ -1391,7 +1398,7 @@ pub impl Parser { self.bump(); (None, zerok) } else { - let sep = *self.token; + let sep = copy *self.token; self.bump(); if *self.token == token::BINOP(token::STAR) || *self.token == token::BINOP(token::PLUS) { @@ -1416,7 +1423,7 @@ pub impl Parser { p.fatal( fmt!( "incorrect close delimiter: `%s`", - token_to_str(p.reader, *p.token) + token_to_str(p.reader, copy *p.token) ) ); } @@ -1912,7 +1919,7 @@ pub impl Parser { // labeled loop headers look like 'loop foo: {' let is_labeled_loop_header = is_ident(*self.token) - && !self.is_any_keyword(&*self.token) + && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::COLON; if is_loop_header || is_labeled_loop_header { @@ -2143,7 +2150,7 @@ pub impl Parser { let lo = self.span.lo; let mut hi = self.span.hi; let mut pat; - match *self.token { + match copy *self.token { token::UNDERSCORE => { self.bump(); pat = pat_wild; } token::AT => { self.bump(); @@ -2446,7 +2453,7 @@ pub impl Parser { let decl = self.parse_let(); return @spanned(lo, decl.span.hi, stmt_decl(decl, self.get_id())); } else if is_ident(*self.token) - && !self.is_any_keyword(&*self.token) + && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT { check_expected_item(self, first_item_attrs); @@ -2540,7 +2547,7 @@ pub impl Parser { let lo = self.span.lo; if self.eat_keyword(&~"unsafe") { - self.obsolete(*self.span, ObsoleteUnsafeBlock); + self.obsolete(copy *self.span, ObsoleteUnsafeBlock); } self.expect(&token::LBRACE); let (inner, next) = @@ -3049,7 +3056,7 @@ pub impl Parser { ty = self.parse_ty(false); opt_trait_ref } else if self.eat(&token::COLON) { - self.obsolete(*self.span, ObsoleteImplSyntax); + self.obsolete(copy *self.span, ObsoleteImplSyntax); Some(self.parse_trait_ref()) } else { None @@ -3116,7 +3123,7 @@ pub impl Parser { self.parse_region_param(); let ty_params = self.parse_ty_params(); if self.eat(&token::COLON) { - self.obsolete(*self.span, ObsoleteClassTraits); + self.obsolete(copy *self.span, ObsoleteClassTraits); let _ = self.parse_trait_ref_list(token::LBRACE); } @@ -3948,7 +3955,7 @@ pub impl Parser { vis: visibility, span: mk_sp(lo, self.last_span.hi) }); - } else if macros_allowed && !self.is_any_keyword(&*self.token) + } else if macros_allowed && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT && (is_plain_ident(self.look_ahead(2)) || self.look_ahead(2) == token::LPAREN @@ -4121,7 +4128,7 @@ pub impl Parser { fn is_view_item() -> bool { let tok, next_tok; if !self.is_keyword(&~"pub") && !self.is_keyword(&~"priv") { - tok = *self.token; + tok = copy *self.token; next_tok = self.look_ahead(1); } else { tok = self.look_ahead(1); -- cgit 1.4.1-3-g733a5 From 3180d22dde253c86ff42eb8a3e936a7856477ca4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 18:51:04 -0800 Subject: libsyntax: change parse_trait_ref_list to take a &Token --- src/libsyntax/parse/parser.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9fe53fe50e2..ef274cb94ab 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3001,7 +3001,7 @@ pub impl Parser { let traits; if *self.token == token::COLON { self.bump(); - traits = self.parse_trait_ref_list(token::LBRACE); + traits = self.parse_trait_ref_list(&token::LBRACE); } else { traits = ~[]; } @@ -3110,9 +3110,9 @@ pub impl Parser { } } - fn parse_trait_ref_list(ket: token::Token) -> ~[@trait_ref] { + fn parse_trait_ref_list(ket: &token::Token) -> ~[@trait_ref] { self.parse_seq_to_before_end( - ket, + *ket, seq_sep_none(), |p| p.parse_trait_ref() ) @@ -3124,7 +3124,7 @@ pub impl Parser { let ty_params = self.parse_ty_params(); if self.eat(&token::COLON) { self.obsolete(copy *self.span, ObsoleteClassTraits); - let _ = self.parse_trait_ref_list(token::LBRACE); + let _ = self.parse_trait_ref_list(&token::LBRACE); } let mut fields: ~[@struct_field]; -- cgit 1.4.1-3-g733a5 From cf6e21a17f04ddc766633f1f22144f7e6c59008c Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 18:55:24 -0800 Subject: libsyntax: change attr::parse_seq_* to take &Token --- src/libsyntax/ext/pipes/parse_proto.rs | 14 +++---- src/libsyntax/parse/attr.rs | 4 +- src/libsyntax/parse/common.rs | 22 +++++------ src/libsyntax/parse/parser.rs | 70 +++++++++++++++++----------------- 4 files changed, 55 insertions(+), 55 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/pipes/parse_proto.rs b/src/libsyntax/ext/pipes/parse_proto.rs index fae5b1b49af..a5d2a1783d6 100644 --- a/src/libsyntax/ext/pipes/parse_proto.rs +++ b/src/libsyntax/ext/pipes/parse_proto.rs @@ -28,7 +28,7 @@ pub impl proto_parser for parser::Parser { let proto = protocol(id, *self.span); self.parse_seq_to_before_end( - token::EOF, + &token::EOF, SeqSep { sep: None, trailing_sep_allowed: false, @@ -65,8 +65,8 @@ pub impl proto_parser for parser::Parser { // parse the messages self.parse_unspanned_seq( - token::LBRACE, - token::RBRACE, + &token::LBRACE, + &token::RBRACE, SeqSep { sep: Some(token::COMMA), trailing_sep_allowed: true, @@ -80,8 +80,8 @@ pub impl proto_parser for parser::Parser { let args = if *self.token == token::LPAREN { self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, SeqSep { sep: Some(token::COMMA), trailing_sep_allowed: true, @@ -98,8 +98,8 @@ pub impl proto_parser for parser::Parser { let name = *self.interner.get(self.parse_ident()); let ntys = if *self.token == token::LT { self.parse_unspanned_seq( - token::LT, - token::GT, + &token::LT, + &token::GT, SeqSep { sep: Some(token::COMMA), trailing_sep_allowed: true, diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index f7b115912da..bad6c76c36a 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -152,8 +152,8 @@ impl parser_attr for Parser { fn parse_meta_seq() -> ~[@ast::meta_item] { self.parse_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_meta_item() ).node diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 78527062515..93d3b8b3364 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -295,7 +295,7 @@ pub impl Parser { // f must consume tokens until reaching the next separator or // closing bracket. fn parse_seq_to_end( - ket: token::Token, + ket: &token::Token, sep: SeqSep, f: fn(Parser) -> T ) -> ~[T] { @@ -308,13 +308,13 @@ pub impl Parser { // f must consume tokens until reaching the next separator or // closing bracket. fn parse_seq_to_before_end( - ket: token::Token, + ket: &token::Token, sep: SeqSep, f: fn(Parser) -> T ) -> ~[T] { let mut first: bool = true; let mut v: ~[T] = ~[]; - while *self.token != ket { + while *self.token != *ket { match sep.sep { Some(ref t) => { if first { first = false; } @@ -322,22 +322,22 @@ pub impl Parser { } _ => () } - if sep.trailing_sep_allowed && *self.token == ket { break; } + if sep.trailing_sep_allowed && *self.token == *ket { break; } v.push(f(self)); } - v + return v; } // parse a sequence, including the closing delimiter. The function // f must consume tokens until reaching the next separator or // closing bracket. fn parse_unspanned_seq( - +bra: token::Token, - +ket: token::Token, + bra: &token::Token, + ket: &token::Token, sep: SeqSep, f: fn(Parser) -> T ) -> ~[T] { - self.expect(&bra); + self.expect(bra); let result = self.parse_seq_to_before_end(ket, sep, f); self.bump(); result @@ -346,13 +346,13 @@ pub impl Parser { // NB: Do not use this function unless you actually plan to place the // spanned list in the AST. fn parse_seq( - +bra: token::Token, - +ket: token::Token, + bra: &token::Token, + ket: &token::Token, sep: SeqSep, f: fn(Parser) -> T ) -> spanned<~[T]> { let lo = self.span.lo; - self.expect(&bra); + self.expect(bra); let result = self.parse_seq_to_before_end(ket, sep, f); let hi = self.span.hi; self.bump(); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ef274cb94ab..1e6473c3cb1 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -421,8 +421,8 @@ pub impl Parser { self.expect(&token::GT); } let inputs = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_arg_general(false) ); @@ -432,8 +432,8 @@ pub impl Parser { fn parse_trait_methods() -> ~[trait_method] { do self.parse_unspanned_seq( - token::LBRACE, - token::RBRACE, + &token::LBRACE, + &token::RBRACE, seq_sep_none() ) |p| { let attrs = p.parse_outer_attributes(); @@ -628,8 +628,8 @@ pub impl Parser { ty_ptr(self.parse_mt()) } else if *self.token == token::LBRACE { let elems = self.parse_unspanned_seq( - token::LBRACE, - token::RBRACE, + &token::LBRACE, + &token::RBRACE, seq_sep_trailing_allowed(token::COMMA), |p| p.parse_ty_field() ); @@ -1190,7 +1190,7 @@ pub impl Parser { // Vector with two or more elements. self.bump(); let remaining_exprs = self.parse_seq_to_end( - token::RBRACKET, + &token::RBRACKET, seq_sep_trailing_allowed(token::COMMA), |p| p.parse_expr() ); @@ -1246,8 +1246,8 @@ pub impl Parser { let ket = token::flip_delimiter(&*self.token); let tts = self.parse_unspanned_seq( - *self.token, - ket, + © *self.token, + &ket, seq_sep_none(), |p| p.parse_token_tree() ); @@ -1339,8 +1339,8 @@ pub impl Parser { match *self.token { token::LPAREN if self.permits_call() => { let es = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_expr() ); @@ -1363,8 +1363,8 @@ pub impl Parser { // expr(...) token::LPAREN if self.permits_call() => { let es = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_expr() ); @@ -1434,8 +1434,8 @@ pub impl Parser { if *p.token == token::LPAREN { let seq = p.parse_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_none(), |p| p.parse_token_tree() ); @@ -1471,7 +1471,7 @@ pub impl Parser { ~[parse_any_tt_tok(self)], vec::append( self.parse_seq_to_before_end( - ket, + &ket, seq_sep_none(), |p| p.parse_token_tree() ), @@ -2320,8 +2320,8 @@ pub impl Parser { } _ => { args = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed( token::COMMA ), @@ -2470,8 +2470,8 @@ pub impl Parser { }; let tts = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_none(), |p| p.parse_token_tree() ); @@ -2780,8 +2780,8 @@ pub impl Parser { { let args_or_capture_items: ~[arg_or_capture_item] = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), parse_arg_fn ); @@ -2865,7 +2865,7 @@ pub impl Parser { self.bump(); let sep = seq_sep_trailing_disallowed(token::COMMA); args_or_capture_items = self.parse_seq_to_before_end( - token::RPAREN, + &token::RPAREN, sep, parse_arg_fn ); @@ -2882,7 +2882,7 @@ pub impl Parser { } else { let sep = seq_sep_trailing_disallowed(token::COMMA); args_or_capture_items = self.parse_seq_to_before_end( - token::RPAREN, + &token::RPAREN, sep, parse_arg_fn ); @@ -2910,8 +2910,8 @@ pub impl Parser { ~[] } else { self.parse_unspanned_seq( - token::BINOP(token::OR), - token::BINOP(token::OR), + &token::BINOP(token::OR), + &token::BINOP(token::OR), seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_fn_block_arg() ) @@ -3112,7 +3112,7 @@ pub impl Parser { fn parse_trait_ref_list(ket: &token::Token) -> ~[@trait_ref] { self.parse_seq_to_before_end( - *ket, + ket, seq_sep_none(), |p| p.parse_trait_ref() ) @@ -3163,8 +3163,8 @@ pub impl Parser { // It's a tuple-like struct. is_tuple_like = true; fields = do self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_allowed(token::COMMA) ) |p| { let lo = p.span.lo; @@ -3729,8 +3729,8 @@ pub impl Parser { } else if *self.token == token::LPAREN { all_nullary = false; let arg_tys = self.parse_unspanned_seq( - token::LPAREN, - token::RPAREN, + &token::LPAREN, + &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), |p| p.parse_ty(false) ); @@ -3982,8 +3982,8 @@ pub impl Parser { token::LPAREN | token::LBRACE => { let ket = token::flip_delimiter(&*self.token); self.parse_unspanned_seq( - *self.token, - ket, + © *self.token, + &ket, seq_sep_none(), |p| p.parse_token_tree() ) @@ -4074,8 +4074,8 @@ pub impl Parser { // foo::bar::{a,b,c} token::LBRACE => { let idents = self.parse_unspanned_seq( - token::LBRACE, - token::RBRACE, + &token::LBRACE, + &token::RBRACE, seq_sep_trailing_allowed(token::COMMA), |p| p.parse_path_list_ident() ); -- cgit 1.4.1-3-g733a5 From 380597eba35be0eb12c59180e1eea97264cd3cc7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 19:04:17 -0800 Subject: libsyntax: change parse_matcher_subseq to take &Token --- src/libsyntax/parse/parser.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1e6473c3cb1..fc69605a927 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1502,9 +1502,9 @@ pub impl Parser { token::LBRACE | token::LPAREN | token::LBRACKET => { self.parse_matcher_subseq( name_idx, - *self.token, + &*self.token, // tjc: not sure why we need a copy - token::flip_delimiter(&*self.token) + &token::flip_delimiter(&*self.token) ) } _ => self.fatal(~"expected open delimiter") @@ -1517,15 +1517,15 @@ pub impl Parser { // invalid. It's similar to common::parse_seq. fn parse_matcher_subseq( name_idx: @mut uint, - bra: token::Token, - ket: token::Token + bra: &token::Token, + ket: &token::Token ) -> ~[matcher] { let mut ret_val = ~[]; let mut lparens = 0u; - self.expect(&bra); + self.expect(bra); - while *self.token != ket || lparens > 0u { + while *self.token != *ket || lparens > 0u { if *self.token == token::LPAREN { lparens += 1u; } if *self.token == token::RPAREN { lparens -= 1u; } ret_val.push(self.parse_matcher(name_idx)); @@ -1545,8 +1545,8 @@ pub impl Parser { let name_idx_lo = *name_idx; let ms = self.parse_matcher_subseq( name_idx, - token::LPAREN, - token::RPAREN + &token::LPAREN, + &token::RPAREN ); if ms.len() == 0u { self.fatal(~"repetition body must be nonempty"); -- cgit 1.4.1-3-g733a5 From 194f29c20fb86fa163f35e9cd0540ae2b0d41b9d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 19:27:43 -0800 Subject: libsyntax: minor cleanup --- src/libsyntax/parse/parser.rs | 99 +++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 37 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index fc69605a927..05f6a3e7517 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2111,9 +2111,12 @@ pub impl Parser { if *self.token == token::UNDERSCORE { self.bump(); if *self.token != token::RBRACE { - self.fatal(~"expected `}`, found `" + - token_to_str(self.reader, *self.token) + - ~"`"); + self.fatal( + fmt!( + "expected `}`, found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } etc = true; break; @@ -2195,7 +2198,6 @@ pub impl Parser { } _ => pat_uniq(sub) }; - } token::BINOP(token::AND) => { let lo = self.span.lo; @@ -2806,8 +2808,12 @@ pub impl Parser { fn expect_self_ident() { if !self.is_self_ident() { - self.fatal(fmt!("expected `self` but found `%s`", - token_to_str(self.reader, *self.token))); + self.fatal( + fmt!( + "expected `self` but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } self.bump(); } @@ -2874,9 +2880,12 @@ pub impl Parser { args_or_capture_items = ~[]; } _ => { - self.fatal(~"expected `,` or `)`, found `" + - token_to_str(self.reader, *self.token) + - ~"`"); + self.fatal( + fmt!( + "expected `,` or `)`, found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } } } else { @@ -3181,9 +3190,13 @@ pub impl Parser { is_tuple_like = true; fields = ~[]; } else { - self.fatal(fmt!("expected `{`, `(`, or `;` after struct name \ - but found `%s`", - token_to_str(self.reader, *self.token))); + self.fatal( + fmt!( + "expected `{`, `(`, or `;` after struct name \ + but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } let actual_dtor = do the_dtor.map |dtor| { @@ -3218,21 +3231,23 @@ pub impl Parser { let a_var = self.parse_instance_var(vis); match *self.token { - token::SEMI => { - self.obsolete(*self.span, ObsoleteFieldTerminator); - self.bump(); - } - token::COMMA => { - self.bump(); - } - token::RBRACE => {} - _ => { - self.span_fatal(*self.span, - fmt!("expected `;`, `,`, or '}' but \ - found `%s`", - token_to_str(self.reader, - *self.token))); - } + token::SEMI => { + self.obsolete(copy *self.span, ObsoleteFieldTerminator); + self.bump(); + } + token::COMMA => { + self.bump(); + } + token::RBRACE => {} + _ => { + self.span_fatal( + copy *self.span, + fmt!( + "expected `;`, `,`, or '}' but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); + } } a_var } @@ -3316,8 +3331,12 @@ pub impl Parser { module"); } _ => { - self.fatal(~"expected item but found `" + - token_to_str(self.reader, *self.token) + ~"`"); + self.fatal( + fmt!( + "expected item but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } } debug!("parse_mod_items: attrs=%?", attrs); @@ -3567,20 +3586,26 @@ pub impl Parser { must_be_named_mod = true; self.expect_keyword(&~"mod"); } else if *self.token != token::LBRACE { - self.span_fatal(*self.span, - fmt!("expected `{` or `mod` but found %s", - token_to_str(self.reader, *self.token))); + self.span_fatal( + copy *self.span, + fmt!( + "expected `{` or `mod` but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } let (sort, ident) = match *self.token { token::IDENT(*) => (ast::named, self.parse_ident()), _ => { if must_be_named_mod { - self.span_fatal(*self.span, - fmt!("expected foreign module name but \ - found %s", - token_to_str(self.reader, - *self.token))); + self.span_fatal( + copy *self.span, + fmt!( + "expected foreign module name but found `%s`", + token_to_str(self.reader, copy *self.token) + ) + ); } (ast::anonymous, -- cgit 1.4.1-3-g733a5 From d346b51997a4a5d9f2e85aa41fc3113338b8a83b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 19:54:37 -0800 Subject: libsyntax: change token::to_str to take &Token --- src/libsyntax/ext/tt/macro_parser.rs | 4 ++-- src/libsyntax/parse/comments.rs | 2 +- src/libsyntax/parse/common.rs | 43 ++++++++++++++++++------------------ src/libsyntax/parse/parser.rs | 37 +++++++++++++++++-------------- src/libsyntax/parse/token.rs | 4 ++-- src/libsyntax/print/pprust.rs | 4 ++-- 6 files changed, 50 insertions(+), 44 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 713bf9afcd0..82e7de08d65 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -378,7 +378,7 @@ pub fn parse(sess: @mut ParseSess, nts, next_eis.len())); } else if (bb_eis.len() == 0u && next_eis.len() == 0u) { return failure(sp, ~"No rules expected the token: " - + to_str(rdr.interner(), tok)); + + to_str(rdr.interner(), &tok)); } else if (next_eis.len() > 0u) { /* Now process the next token */ while(next_eis.len() > 0u) { @@ -424,7 +424,7 @@ pub fn parse_nt(p: Parser, name: ~str) -> nonterminal { ~"ident" => match *p.token { token::IDENT(sn,b) => { p.bump(); token::nt_ident(sn,b) } _ => p.fatal(~"expected ident, found " - + token::to_str(p.reader.interner(), copy *p.token)) + + token::to_str(p.reader.interner(), © *p.token)) }, ~"path" => token::nt_path(p.parse_path_with_tps(false)), ~"tt" => { diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 152bd9b0ce4..377b089c532 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -359,7 +359,7 @@ pub fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, literals.push(lit {lit: s, pos: sp.lo}); log(debug, ~"tok lit: " + s); } else { - log(debug, ~"tok: " + token::to_str(rdr.interner, tok)); + log(debug, ~"tok: " + token::to_str(rdr.interner, &tok)); } first_read = false; } diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 93d3b8b3364..764ff52a303 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -46,7 +46,7 @@ pub fn seq_sep_none() -> SeqSep { } } -pub fn token_to_str(reader: reader, ++token: token::Token) -> ~str { +pub fn token_to_str(reader: reader, token: &token::Token) -> ~str { token::to_str(reader.interner(), token) } @@ -56,7 +56,7 @@ pub impl Parser { *self.last_span, fmt!( "unexpected token: `%s`", - token_to_str(self.reader, t) + token_to_str(self.reader, &t) ) ); } @@ -65,7 +65,7 @@ pub impl Parser { self.fatal( fmt!( "unexpected token: `%s`", - token_to_str(self.reader, *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -77,9 +77,10 @@ pub impl Parser { self.bump(); } else { self.fatal( - fmt!("expected `%s` but found `%s`", - token_to_str(self.reader, *t), - token_to_str(self.reader, *self.token) + fmt!( + "expected `%s` but found `%s`", + token_to_str(self.reader, t), + token_to_str(self.reader, © *self.token) ) ) } @@ -102,7 +103,7 @@ pub impl Parser { self.fatal( fmt!( "expected ident, found `%s`", - token_to_str(self.reader, *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -149,7 +150,7 @@ pub impl Parser { } fn is_keyword(word: &~str) -> bool { - self.token_is_keyword(word, &*self.token) + self.token_is_keyword(word, © *self.token) } fn is_any_keyword(tok: &token::Token) -> bool { @@ -178,7 +179,7 @@ pub impl Parser { fmt!( "expected `%s`, found `%s`", *word, - token_to_str(self.reader, *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -190,11 +191,11 @@ pub impl Parser { fn check_strict_keywords() { match *self.token { - token::IDENT(_, false) => { - let w = token_to_str(self.reader, *self.token); - self.check_strict_keywords_(&w); - } - _ => () + token::IDENT(_, false) => { + let w = token_to_str(self.reader, © *self.token); + self.check_strict_keywords_(&w); + } + _ => () } } @@ -210,11 +211,11 @@ pub impl Parser { fn check_reserved_keywords() { match *self.token { - token::IDENT(_, false) => { - let w = token_to_str(self.reader, *self.token); - self.check_reserved_keywords_(&w); - } - _ => () + token::IDENT(_, false) => { + let w = token_to_str(self.reader, © *self.token); + self.check_reserved_keywords_(&w); + } + _ => () } } @@ -237,9 +238,9 @@ pub impl Parser { ); } else { let mut s: ~str = ~"expected `"; - s += token_to_str(self.reader, token::GT); + s += token_to_str(self.reader, &token::GT); s += ~"`, found `"; - s += token_to_str(self.reader, *self.token); + s += token_to_str(self.reader, © *self.token); s += ~"`"; self.fatal(s); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 05f6a3e7517..d0c6019e09a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -460,7 +460,7 @@ pub impl Parser { let hi = p.last_span.hi; debug!("parse_trait_methods(): trait method signature ends in \ `%s`", - token_to_str(p.reader, copy *p.token)); + token_to_str(p.reader, © *p.token)); match *p.token { token::SEMI => { p.bump(); @@ -502,7 +502,7 @@ pub impl Parser { p.fatal( fmt!( "expected `;` or `}` but found `%s`", - token_to_str(p.reader, copy *p.token) + token_to_str(p.reader, © *p.token) ) ); } @@ -848,7 +848,7 @@ pub impl Parser { fmt!( "expected integral vector length \ but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -1423,7 +1423,7 @@ pub impl Parser { p.fatal( fmt!( "incorrect close delimiter: `%s`", - token_to_str(p.reader, copy *p.token) + token_to_str(p.reader, © *p.token) ) ); } @@ -2114,7 +2114,7 @@ pub impl Parser { self.fatal( fmt!( "expected `}`, found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -2621,10 +2621,15 @@ pub impl Parser { copy t => { if classify::stmt_ends_with_semi(*stmt) { self.fatal( - ~"expected `;` or `}` after \ - expression but found `" - + token_to_str(self.reader, t) - + ~"`"); + fmt!( + "expected `;` or `}` after \ + expression but found `%s`", + token_to_str( + self.reader, + &t + ) + ) + ); } stmts.push(stmt); } @@ -2811,7 +2816,7 @@ pub impl Parser { self.fatal( fmt!( "expected `self` but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -2883,7 +2888,7 @@ pub impl Parser { self.fatal( fmt!( "expected `,` or `)`, found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -3194,7 +3199,7 @@ pub impl Parser { fmt!( "expected `{`, `(`, or `;` after struct name \ but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -3244,7 +3249,7 @@ pub impl Parser { copy *self.span, fmt!( "expected `;`, `,`, or '}' but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -3334,7 +3339,7 @@ pub impl Parser { self.fatal( fmt!( "expected item but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -3590,7 +3595,7 @@ pub impl Parser { copy *self.span, fmt!( "expected `{` or `mod` but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } @@ -3603,7 +3608,7 @@ pub impl Parser { copy *self.span, fmt!( "expected foreign module name but found `%s`", - token_to_str(self.reader, copy *self.token) + token_to_str(self.reader, © *self.token) ) ); } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 8e88ac1d525..bc61ce9e7fc 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -130,8 +130,8 @@ pub fn binop_to_str(o: binop) -> ~str { } } -pub fn to_str(in: @ident_interner, t: Token) -> ~str { - match t { +pub fn to_str(in: @ident_interner, t: &Token) -> ~str { + match *t { EQ => ~"=", LT => ~"<", LE => ~"<=", diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b4773fe2f97..7cf297b324b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -777,14 +777,14 @@ pub fn print_tt(s: @ps, tt: ast::token_tree) { match tt { ast::tt_delim(ref tts) => print_tts(s, *tts), ast::tt_tok(_, ref tk) => { - word(s.s, parse::token::to_str(s.intr, (*tk))); + word(s.s, parse::token::to_str(s.intr, tk)); } ast::tt_seq(_, ref tts, ref sep, zerok) => { word(s.s, ~"$("); for (*tts).each() |tt_elt| { print_tt(s, *tt_elt); } word(s.s, ~")"); match (*sep) { - Some(ref tk) => word(s.s, parse::token::to_str(s.intr, (*tk))), + Some(ref tk) => word(s.s, parse::token::to_str(s.intr, tk)), None => () } word(s.s, if zerok { ~"*" } else { ~"+" }); -- cgit 1.4.1-3-g733a5 From ff36986fa490917bcacfb4e5010e304d5e82f3bb Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 17:24:28 -0800 Subject: libsyntax: change token fns to take &Token --- src/libsyntax/parse/comments.rs | 2 +- src/libsyntax/parse/parser.rs | 40 ++++++++++++++++++++-------------------- src/libsyntax/parse/token.rs | 36 ++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 39 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 377b089c532..4960563db88 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -354,7 +354,7 @@ pub fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, rdr.next_token(); //discard, and look ahead; we're working with internal state let TokenAndSpan {tok: tok, sp: sp} = rdr.peek(); - if token::is_lit(tok) { + if token::is_lit(&tok) { let s = get_str_from(rdr, bstart); literals.push(lit {lit: s, pos: sp.lo}); log(debug, ~"tok lit: " + s); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d0c6019e09a..a5226127947 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -656,7 +656,7 @@ pub impl Parser { } else if self.token_is_closure_keyword(© *self.token) { self.parse_ty_closure(None, None) } else if *self.token == token::MOD_SEP - || is_ident_or_path(*self.token) { + || is_ident_or_path(&*self.token) { let path = self.parse_path_with_tps(colons_before_params); ty_path(path, self.get_id()) } else { @@ -760,10 +760,10 @@ pub impl Parser { } } else { 0 }; if offset == 0 { - is_plain_ident(*self.token) + is_plain_ident(&*self.token) && self.look_ahead(1) == token::COLON } else { - is_plain_ident(self.look_ahead(offset)) + is_plain_ident(&self.look_ahead(offset)) && self.look_ahead(offset + 1) == token::COLON } } @@ -1141,7 +1141,7 @@ pub impl Parser { return self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk)); } - } else if token::is_bar(*self.token) { + } else if token::is_bar(&*self.token) { return self.parse_lambda_expr(); } else if self.eat_keyword(&~"if") { return self.parse_if_expr(); @@ -1215,13 +1215,13 @@ pub impl Parser { ex = expr_assert(e); hi = e.span.hi; } else if self.eat_keyword(&~"return") { - if can_begin_expr(*self.token) { + if can_begin_expr(&*self.token) { let e = self.parse_expr(); hi = e.span.hi; ex = expr_ret(Some(e)); } else { ex = expr_ret(None); } } else if self.eat_keyword(&~"break") { - if is_ident(*self.token) { + if is_ident(&*self.token) { ex = expr_break(Some(self.parse_ident())); } else { ex = expr_break(None); @@ -1232,7 +1232,7 @@ pub impl Parser { ex = expr_copy(e); hi = e.span.hi; } else if *self.token == token::MOD_SEP || - is_ident(*self.token) && !self.is_keyword(&~"true") && + is_ident(&*self.token) && !self.is_keyword(&~"true") && !self.is_keyword(&~"false") { let pth = self.parse_path_with_tps(true); @@ -1914,11 +1914,11 @@ pub impl Parser { // loop headers look like 'loop {' or 'loop unsafe {' let is_loop_header = *self.token == token::LBRACE - || (is_ident(*self.token) + || (is_ident(&*self.token) && self.look_ahead(1) == token::LBRACE); // labeled loop headers look like 'loop foo: {' let is_labeled_loop_header = - is_ident(*self.token) + is_ident(&*self.token) && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::COLON; @@ -1939,7 +1939,7 @@ pub impl Parser { } else { // This is a 'continue' expression let lo = self.span.lo; - let ex = if is_ident(*self.token) { + let ex = if is_ident(&*self.token) { expr_again(Some(self.parse_ident())) } else { expr_again(None) @@ -1954,7 +1954,7 @@ pub impl Parser { let lookahead = self.look_ahead(1); *self.token == token::LBRACE && (self.token_is_keyword(&~"mut", &lookahead) || - (is_plain_ident(lookahead) && + (is_plain_ident(&lookahead) && self.look_ahead(2) == token::COLON)) } @@ -2260,7 +2260,7 @@ pub impl Parser { pat = ast::pat_vec(elements, tail); } copy tok => { - if !is_ident_or_path(tok) + if !is_ident_or_path(&tok) || self.is_keyword(&~"true") || self.is_keyword(&~"false") { @@ -2290,7 +2290,7 @@ pub impl Parser { cannot_be_enum_or_struct = true } - if is_plain_ident(*self.token) && cannot_be_enum_or_struct { + if is_plain_ident(&*self.token) && cannot_be_enum_or_struct { let name = self.parse_value_path(); let sub; if self.eat(&token::AT) { @@ -2359,7 +2359,7 @@ pub impl Parser { fn parse_pat_ident(refutable: bool, binding_mode: ast::binding_mode) -> ast::pat_ { - if !is_plain_ident(*self.token) { + if !is_plain_ident(&*self.token) { self.span_fatal( *self.last_span, ~"expected identifier, found path"); @@ -2425,7 +2425,7 @@ pub impl Parser { if self.eat_keyword(&~"mut") { is_mutbl = struct_mutable; } - if !is_plain_ident(*self.token) { + if !is_plain_ident(&*self.token) { self.fatal(~"expected ident"); } let name = self.parse_ident(); @@ -2454,7 +2454,7 @@ pub impl Parser { self.expect_keyword(&~"let"); let decl = self.parse_let(); return @spanned(lo, decl.span.hi, stmt_decl(decl, self.get_id())); - } else if is_ident(*self.token) + } else if is_ident(&*self.token) && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT { @@ -2716,7 +2716,7 @@ pub impl Parser { ~"`&static` is the only permissible \ region bound here"); } - } else if is_ident(*self.token) { + } else if is_ident(&*self.token) { let maybe_bound = match *self.token { token::IDENT(copy sid, _) => { match *self.id_to_str(sid) { @@ -2757,7 +2757,7 @@ pub impl Parser { loop; } - if is_ident_or_path(*self.token) { + if is_ident_or_path(&*self.token) { self.obsolete(*self.span, ObsoleteTraitBoundSeparator); } @@ -3987,7 +3987,7 @@ pub impl Parser { }); } else if macros_allowed && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT - && (is_plain_ident(self.look_ahead(2)) + && (is_plain_ident(&self.look_ahead(2)) || self.look_ahead(2) == token::LPAREN || self.look_ahead(2) == token::LBRACE) { // MACRO INVOCATION ITEM @@ -4002,7 +4002,7 @@ pub impl Parser { // a 'special' identifier (like what `macro_rules!` uses) // is optional. We should eventually unify invoc syntax // and remove this. - let id = if is_plain_ident(*self.token) { + let id = if is_plain_ident(&*self.token) { self.parse_ident() } else { token::special_idents::invalid // no special identifier diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index bc61ce9e7fc..bb1f8f1d1d9 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -225,8 +225,8 @@ pub fn to_str(in: @ident_interner, t: &Token) -> ~str { } } -pub pure fn can_begin_expr(t: Token) -> bool { - match t { +pub pure fn can_begin_expr(t: &Token) -> bool { + match *t { LPAREN => true, LBRACE => true, LBRACKET => true, @@ -259,20 +259,20 @@ pub pure fn can_begin_expr(t: Token) -> bool { /// what's the opposite delimiter? pub fn flip_delimiter(t: &token::Token) -> token::Token { match *t { - token::LPAREN => token::RPAREN, - token::LBRACE => token::RBRACE, - token::LBRACKET => token::RBRACKET, - token::RPAREN => token::LPAREN, - token::RBRACE => token::LBRACE, - token::RBRACKET => token::LBRACKET, + LPAREN => RPAREN, + LBRACE => RBRACE, + LBRACKET => RBRACKET, + RPAREN => LPAREN, + RBRACE => LBRACE, + RBRACKET => LBRACKET, _ => fail!() } } -pub fn is_lit(t: Token) -> bool { - match t { +pub fn is_lit(t: &Token) -> bool { + match *t { LIT_INT(_, _) => true, LIT_UINT(_, _) => true, LIT_INT_UNSUFFIXED(_) => true, @@ -283,23 +283,23 @@ pub fn is_lit(t: Token) -> bool { } } -pub pure fn is_ident(t: Token) -> bool { - match t { IDENT(_, _) => true, _ => false } +pub pure fn is_ident(t: &Token) -> bool { + match *t { IDENT(_, _) => true, _ => false } } -pub pure fn is_ident_or_path(t: Token) -> bool { - match t { +pub pure fn is_ident_or_path(t: &Token) -> bool { + match *t { IDENT(_, _) | INTERPOLATED(nt_path(*)) => true, _ => false } } -pub pure fn is_plain_ident(t: Token) -> bool { - match t { IDENT(_, false) => true, _ => false } +pub pure fn is_plain_ident(t: &Token) -> bool { + match *t { IDENT(_, false) => true, _ => false } } -pub pure fn is_bar(t: Token) -> bool { - match t { BINOP(OR) | OROR => true, _ => false } +pub pure fn is_bar(t: &Token) -> bool { + match *t { BINOP(OR) | OROR => true, _ => false } } -- cgit 1.4.1-3-g733a5 From 28691a0852854a9996d07ab92bb55e92beef2c98 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 20:51:56 -0800 Subject: libsyntax: more minor cleanup --- src/libsyntax/parse/attr.rs | 4 ++-- src/libsyntax/parse/common.rs | 8 +++---- src/libsyntax/parse/parser.rs | 52 +++++++++++++++++++++---------------------- 3 files changed, 32 insertions(+), 32 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index bad6c76c36a..56e350db47b 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -161,8 +161,8 @@ impl parser_attr for Parser { fn parse_optional_meta() -> ~[@ast::meta_item] { match *self.token { - token::LPAREN => return self.parse_meta_seq(), - _ => return ~[] + token::LPAREN => self.parse_meta_seq(), + _ => ~[] } } } diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 764ff52a303..4632ceb6e7d 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -139,8 +139,8 @@ pub impl Parser { fn token_is_word(word: &~str, tok: &token::Token) -> bool { match *tok { - token::IDENT(sid, false) => { *self.id_to_str(sid) == *word } - _ => { false } + token::IDENT(sid, false) => { *self.id_to_str(sid) == *word } + _ => { false } } } @@ -165,8 +165,8 @@ pub impl Parser { fn eat_keyword(word: &~str) -> bool { self.require_keyword(word); let is_kw = match *self.token { - token::IDENT(sid, false) => *word == *self.id_to_str(sid), - _ => false + token::IDENT(sid, false) => *word == *self.id_to_str(sid), + _ => false }; if is_kw { self.bump() } is_kw diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a5226127947..9b825512c39 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -633,7 +633,7 @@ pub impl Parser { seq_sep_trailing_allowed(token::COMMA), |p| p.parse_ty_field() ); - if vec::len(elems) == 0u { + if elems.len() == 0 { self.unexpected_last(token::RBRACE); } ty_rec(elems) @@ -858,17 +858,17 @@ pub impl Parser { } } - fn lit_from_token(tok: token::Token) -> lit_ { - match tok { - token::LIT_INT(i, it) => lit_int(i, it), - token::LIT_UINT(u, ut) => lit_uint(u, ut), - token::LIT_INT_UNSUFFIXED(i) => lit_int_unsuffixed(i), - token::LIT_FLOAT(s, ft) => lit_float(self.id_to_str(s), ft), - token::LIT_FLOAT_UNSUFFIXED(s) => - lit_float_unsuffixed(self.id_to_str(s)), - token::LIT_STR(s) => lit_str(self.id_to_str(s)), - token::LPAREN => { self.expect(&token::RPAREN); lit_nil }, - _ => { self.unexpected_last(tok); } + fn lit_from_token(tok: &token::Token) -> lit_ { + match *tok { + token::LIT_INT(i, it) => lit_int(i, it), + token::LIT_UINT(u, ut) => lit_uint(u, ut), + token::LIT_INT_UNSUFFIXED(i) => lit_int_unsuffixed(i), + token::LIT_FLOAT(s, ft) => lit_float(self.id_to_str(s), ft), + token::LIT_FLOAT_UNSUFFIXED(s) => + lit_float_unsuffixed(self.id_to_str(s)), + token::LIT_STR(s) => lit_str(self.id_to_str(s)), + token::LPAREN => { self.expect(&token::RPAREN); lit_nil }, + _ => { self.unexpected_last(*tok); } } } @@ -882,7 +882,7 @@ pub impl Parser { // XXX: This is a really bad copy! let tok = copy *self.token; self.bump(); - self.lit_from_token(tok) + self.lit_from_token(&tok) }; codemap::spanned { node: lit, span: mk_sp(lo, self.last_span.hi) } } @@ -1240,8 +1240,8 @@ pub impl Parser { if *self.token == token::NOT { self.bump(); match *self.token { - token::LPAREN | token::LBRACE => {} - _ => self.fatal(~"expected open delimiter") + token::LPAREN | token::LBRACE => {} + _ => self.fatal(~"expected open delimiter") }; let ket = token::flip_delimiter(&*self.token); @@ -2554,7 +2554,8 @@ pub impl Parser { self.expect(&token::LBRACE); let (inner, next) = maybe_parse_inner_attrs_and_next(self, parse_attrs); - return (inner, self.parse_block_tail_(lo, default_blk, next)); + + (inner, self.parse_block_tail_(lo, default_blk, next)) } fn parse_block_no_value() -> blk { @@ -2624,10 +2625,7 @@ pub impl Parser { fmt!( "expected `;` or `}` after \ expression but found `%s`", - token_to_str( - self.reader, - &t - ) + token_to_str(self.reader, &t) ) ); } @@ -2823,12 +2821,14 @@ pub impl Parser { self.bump(); } - fn parse_fn_decl_with_self(parse_arg_fn: - fn(Parser) -> arg_or_capture_item) - -> (self_ty, fn_decl) { - - fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_, - p: Parser) -> ast::self_ty_ { + fn parse_fn_decl_with_self( + parse_arg_fn: + fn(Parser) -> arg_or_capture_item + ) -> (self_ty, fn_decl) { + fn maybe_parse_self_ty( + cnstr: fn(+v: mutability) -> ast::self_ty_, + p: Parser + ) -> ast::self_ty_ { // We need to make sure it isn't a mode or a type if p.token_is_keyword(&~"self", &p.look_ahead(1)) || ((p.token_is_keyword(&~"const", &p.look_ahead(1)) || -- cgit 1.4.1-3-g733a5 From 34c02a6c0ead2896ecc68aa92dd39cd62aee5aea Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 21:20:50 -0800 Subject: libsyntax: change Parser::unexpected_last to take &Token --- src/libsyntax/parse/common.rs | 4 ++-- src/libsyntax/parse/parser.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 4632ceb6e7d..af3779b3f39 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -51,12 +51,12 @@ pub fn token_to_str(reader: reader, token: &token::Token) -> ~str { } pub impl Parser { - fn unexpected_last(t: token::Token) -> ! { + fn unexpected_last(t: &token::Token) -> ! { self.span_fatal( *self.last_span, fmt!( "unexpected token: `%s`", - token_to_str(self.reader, &t) + token_to_str(self.reader, t) ) ); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9b825512c39..7f49cf4ae18 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -634,7 +634,7 @@ pub impl Parser { |p| p.parse_ty_field() ); if elems.len() == 0 { - self.unexpected_last(token::RBRACE); + self.unexpected_last(&token::RBRACE); } ty_rec(elems) } else if *self.token == token::LBRACKET { @@ -868,7 +868,7 @@ pub impl Parser { lit_float_unsuffixed(self.id_to_str(s)), token::LIT_STR(s) => lit_str(self.id_to_str(s)), token::LPAREN => { self.expect(&token::RPAREN); lit_nil }, - _ => { self.unexpected_last(*tok); } + _ => { self.unexpected_last(tok); } } } -- cgit 1.4.1-3-g733a5 From de6d9f66b514d6a0e6cc436a942218c3ab4c8e57 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 21:22:43 -0800 Subject: libsyntax: change token_is_word to take &Token --- src/libsyntax/parse/common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index af3779b3f39..a426d6bba05 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -137,7 +137,7 @@ pub impl Parser { } } - fn token_is_word(word: &~str, tok: &token::Token) -> bool { + pure fn token_is_word(word: &~str, tok: &token::Token) -> bool { match *tok { token::IDENT(sid, false) => { *self.id_to_str(sid) == *word } _ => { false } -- cgit 1.4.1-3-g733a5 From 375c2982971662a26afda5e2aac437ccf81a9872 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 24 Feb 2013 17:02:04 -0800 Subject: libsyntax: change binop_to_str to be pure --- src/libsyntax/parse/token.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index bb1f8f1d1d9..2b9df61120e 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -115,7 +115,7 @@ pub enum nonterminal { nt_matchers(~[ast::matcher]) } -pub fn binop_to_str(o: binop) -> ~str { +pub pure fn binop_to_str(o: binop) -> ~str { match o { PLUS => ~"+", MINUS => ~"-", -- cgit 1.4.1-3-g733a5 From 8d239a256d39eb2527ddd40e34d14a2e1801fb61 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 26 Feb 2013 06:28:14 -0800 Subject: libsyntax: change closures to take fn(&Parser) --- src/libsyntax/parse/common.rs | 18 ++++++++-------- src/libsyntax/parse/parser.rs | 48 +++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 33 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index a426d6bba05..28c5bf721a1 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -250,7 +250,7 @@ pub impl Parser { // before the '>'. fn parse_seq_to_before_gt( sep: Option, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> ~[T] { let mut first = true; let mut v = ~[]; @@ -263,7 +263,7 @@ pub impl Parser { } _ => () } - v.push(f(self)); + v.push(f(&self)); } return v; @@ -271,7 +271,7 @@ pub impl Parser { fn parse_seq_to_gt( sep: Option, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> ~[T] { let v = self.parse_seq_to_before_gt(sep, f); self.expect_gt(); @@ -282,7 +282,7 @@ pub impl Parser { // parse a sequence bracketed by '<' and '>' fn parse_seq_lt_gt( sep: Option, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> spanned<~[T]> { let lo = self.span.lo; self.expect(&token::LT); @@ -298,7 +298,7 @@ pub impl Parser { fn parse_seq_to_end( ket: &token::Token, sep: SeqSep, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> ~[T] { let val = self.parse_seq_to_before_end(ket, sep, f); self.bump(); @@ -311,7 +311,7 @@ pub impl Parser { fn parse_seq_to_before_end( ket: &token::Token, sep: SeqSep, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> ~[T] { let mut first: bool = true; let mut v: ~[T] = ~[]; @@ -324,7 +324,7 @@ pub impl Parser { _ => () } if sep.trailing_sep_allowed && *self.token == *ket { break; } - v.push(f(self)); + v.push(f(&self)); } return v; } @@ -336,7 +336,7 @@ pub impl Parser { bra: &token::Token, ket: &token::Token, sep: SeqSep, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> ~[T] { self.expect(bra); let result = self.parse_seq_to_before_end(ket, sep, f); @@ -350,7 +350,7 @@ pub impl Parser { bra: &token::Token, ket: &token::Token, sep: SeqSep, - f: fn(Parser) -> T + f: fn(&Parser) -> T ) -> spanned<~[T]> { let lo = self.span.lo; self.expect(bra); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7f49cf4ae18..694e8387958 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -768,7 +768,7 @@ pub impl Parser { } } - fn parse_capture_item_or(parse_arg_fn: fn(Parser) -> arg_or_capture_item) + fn parse_capture_item_or(parse_arg_fn: fn(&Parser) -> arg_or_capture_item) -> arg_or_capture_item { if self.eat_keyword(&~"copy") { @@ -776,7 +776,7 @@ pub impl Parser { self.parse_ident(); either::Right(()) } else { - parse_arg_fn(self) + parse_arg_fn(&self) } } @@ -893,8 +893,8 @@ pub impl Parser { } fn parse_path_without_tps_( - parse_ident: fn(Parser) -> ident, - parse_last_ident: fn(Parser) -> ident) -> @path { + parse_ident: fn(&Parser) -> ident, + parse_last_ident: fn(&Parser) -> ident) -> @path { maybe_whole!(self, nt_path); let lo = self.span.lo; @@ -906,10 +906,10 @@ pub impl Parser { && self.look_ahead(1u) == token::MOD_SEP; if is_not_last { - ids.push(parse_ident(self)); + ids.push(parse_ident(&self)); self.expect(&token::MOD_SEP); } else { - ids.push(parse_last_ident(self)); + ids.push(parse_last_ident(&self)); break; } } @@ -1415,7 +1415,7 @@ pub impl Parser { fn parse_token_tree() -> token_tree { maybe_whole!(deref self, nt_tt); - fn parse_non_delim_tt_tok(p: Parser) -> token_tree { + fn parse_non_delim_tt_tok(p: &Parser) -> token_tree { maybe_whole!(deref p, nt_tt); match *p.token { token::RPAREN | token::RBRACE | token::RBRACKET @@ -1452,7 +1452,7 @@ pub impl Parser { } // turn the next token into a tt_tok: - fn parse_any_tt_tok(p: Parser) -> token_tree{ + fn parse_any_tt_tok(p: &Parser) -> token_tree{ let res = tt_tok(*p.span, *p.token); p.bump(); res @@ -1468,7 +1468,7 @@ pub impl Parser { tt_delim( vec::append( // the open delimiter: - ~[parse_any_tt_tok(self)], + ~[parse_any_tt_tok(&self)], vec::append( self.parse_seq_to_before_end( &ket, @@ -1476,12 +1476,12 @@ pub impl Parser { |p| p.parse_token_tree() ), // the close delimiter: - ~[parse_any_tt_tok(self)] + ~[parse_any_tt_tok(&self)] ) ) ) } - _ => parse_non_delim_tt_tok(self) + _ => parse_non_delim_tt_tok(&self) } } @@ -2441,7 +2441,7 @@ pub impl Parser { fn parse_stmt(+first_item_attrs: ~[attribute]) -> @stmt { maybe_whole!(self, nt_stmt); - fn check_expected_item(p: Parser, current_attrs: ~[attribute]) { + fn check_expected_item(p: &Parser, current_attrs: ~[attribute]) { // If we have attributes then we should have an item if !current_attrs.is_empty() { p.fatal(~"expected item after attrs"); @@ -2450,7 +2450,7 @@ pub impl Parser { let lo = self.span.lo; if self.is_keyword(&~"let") { - check_expected_item(self, first_item_attrs); + check_expected_item(&self, first_item_attrs); self.expect_keyword(&~"let"); let decl = self.parse_let(); return @spanned(lo, decl.span.hi, stmt_decl(decl, self.get_id())); @@ -2458,7 +2458,7 @@ pub impl Parser { && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT { - check_expected_item(self, first_item_attrs); + check_expected_item(&self, first_item_attrs); // Potential trouble: if we allow macros with paths instead of // idents, we'd need to look ahead past the whole path here... @@ -2514,7 +2514,7 @@ pub impl Parser { iovi_none() => { /* fallthrough */ } } - check_expected_item(self, item_attrs); + check_expected_item(&self, item_attrs); // Remainder are line-expr stmts. let e = self.parse_expr_res(RESTRICT_STMT_EXPR); @@ -2538,7 +2538,7 @@ pub impl Parser { maybe_whole!(pair_empty self, nt_block); - fn maybe_parse_inner_attrs_and_next(p: Parser, parse_attrs: bool) -> + fn maybe_parse_inner_attrs_and_next(p: &Parser, parse_attrs: bool) -> (~[attribute], ~[attribute]) { if parse_attrs { p.parse_inner_attrs_and_next() @@ -2553,7 +2553,7 @@ pub impl Parser { } self.expect(&token::LBRACE); let (inner, next) = - maybe_parse_inner_attrs_and_next(self, parse_attrs); + maybe_parse_inner_attrs_and_next(&self, parse_attrs); (inner, self.parse_block_tail_(lo, default_blk, next)) } @@ -2780,7 +2780,7 @@ pub impl Parser { } else { ~[] } } - fn parse_fn_decl(parse_arg_fn: fn(Parser) -> arg_or_capture_item) + fn parse_fn_decl(parse_arg_fn: fn(&Parser) -> arg_or_capture_item) -> fn_decl { let args_or_capture_items: ~[arg_or_capture_item] = @@ -2823,11 +2823,11 @@ pub impl Parser { fn parse_fn_decl_with_self( parse_arg_fn: - fn(Parser) -> arg_or_capture_item + fn(&Parser) -> arg_or_capture_item ) -> (self_ty, fn_decl) { fn maybe_parse_self_ty( cnstr: fn(+v: mutability) -> ast::self_ty_, - p: Parser + p: &Parser ) -> ast::self_ty_ { // We need to make sure it isn't a mode or a type if p.token_is_keyword(&~"self", &p.look_ahead(1)) || @@ -2851,13 +2851,13 @@ pub impl Parser { let lo = self.span.lo; let self_ty = match *self.token { token::BINOP(token::AND) => { - maybe_parse_self_ty(sty_region, self) + maybe_parse_self_ty(sty_region, &self) } token::AT => { - maybe_parse_self_ty(sty_box, self) + maybe_parse_self_ty(sty_box, &self) } token::TILDE => { - maybe_parse_self_ty(sty_uniq, self) + maybe_parse_self_ty(sty_uniq, &self) } token::IDENT(*) if self.is_self_ident() => { self.bump(); @@ -3028,7 +3028,7 @@ pub impl Parser { // impl ~[T] : to_str { ... } // impl to_str for ~[T] { ... } fn parse_item_impl() -> item_info { - fn wrap_path(p: Parser, pt: @path) -> @Ty { + fn wrap_path(p: &Parser, pt: @path) -> @Ty { @Ty { id: p.get_id(), node: ty_path(pt, p.get_id()), -- cgit 1.4.1-3-g733a5 From 4ae91e2961ac7be50a346a6b0d724601878a9cd0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 26 Feb 2013 06:35:36 -0800 Subject: libsyntax: add explicit copies --- src/libsyntax/parse/attr.rs | 12 +++-- src/libsyntax/parse/comments.rs | 10 ++-- src/libsyntax/parse/mod.rs | 21 +++++--- src/libsyntax/parse/parser.rs | 105 +++++++++++++++++++++++++--------------- src/libsyntax/parse/token.rs | 14 +++--- 5 files changed, 101 insertions(+), 61 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 56e350db47b..a52a9f3ba5e 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -46,7 +46,10 @@ impl parser_attr for Parser { } token::DOC_COMMENT(s) => { let attr = ::attr::mk_sugared_doc_attr( - *self.id_to_str(s), self.span.lo, self.span.hi); + copy *self.id_to_str(s), + self.span.lo, + self.span.hi + ); if attr.node.style != ast::attr_outer { self.fatal(~"expected outer comment"); } @@ -113,7 +116,10 @@ impl parser_attr for Parser { } token::DOC_COMMENT(s) => { let attr = ::attr::mk_sugared_doc_attr( - *self.id_to_str(s), self.span.lo, self.span.hi); + copy *self.id_to_str(s), + self.span.lo, + self.span.hi + ); self.bump(); if attr.node.style == ast::attr_inner { inner_attrs += ~[attr]; @@ -151,7 +157,7 @@ impl parser_attr for Parser { } fn parse_meta_seq() -> ~[@ast::meta_item] { - self.parse_seq( + copy self.parse_seq( &token::LPAREN, &token::RPAREN, seq_sep_trailing_disallowed(token::COMMA), diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 4960563db88..714ae9a0fd5 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -221,7 +221,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut ~[~str], if col < len { s1 = str::slice(s, col, len); } else { s1 = ~""; } - } else { s1 = s; } + } else { s1 = /*bad*/ copy s; } log(debug, ~"pushing line: " + s1); lines.push(s1); } @@ -321,7 +321,7 @@ pub struct lit { } pub fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, - path: ~str, + +path: ~str, srdr: io::Reader) -> (~[cmnt], ~[lit]) { let src = @str::from_bytes(srdr.read_whole_stream()); let itr = parse::token::mk_fake_ident_interner(); @@ -356,10 +356,10 @@ pub fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler, let TokenAndSpan {tok: tok, sp: sp} = rdr.peek(); if token::is_lit(&tok) { let s = get_str_from(rdr, bstart); - literals.push(lit {lit: s, pos: sp.lo}); - log(debug, ~"tok lit: " + s); + literals.push(lit {lit: /*bad*/ copy s, pos: sp.lo}); + debug!("tok lit: %s", s); } else { - log(debug, ~"tok: " + token::to_str(rdr.interner, &tok)); + debug!("tok: %s", token::to_str(rdr.interner, &tok)); } first_read = false; } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 5fa61159385..cf5cb847018 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -92,7 +92,7 @@ pub fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: @mut ParseSess) -> @ast::crate { - let p = new_parser_from_source_str(sess, cfg, name, + let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, codemap::FssNone, source); let r = p.parse_crate_mod(cfg); p.abort_if_errors(); @@ -103,7 +103,7 @@ pub fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: @mut ParseSess) -> @ast::expr { - let p = new_parser_from_source_str(sess, cfg, name, + let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, codemap::FssNone, source); let r = p.parse_expr(); p.abort_if_errors(); @@ -116,7 +116,7 @@ pub fn parse_item_from_source_str(name: ~str, +attrs: ~[ast::attribute], sess: @mut ParseSess) -> Option<@ast::item> { - let p = new_parser_from_source_str(sess, cfg, name, + let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, codemap::FssNone, source); let r = p.parse_item(attrs); p.abort_if_errors(); @@ -128,7 +128,7 @@ pub fn parse_stmt_from_source_str(name: ~str, cfg: ast::crate_cfg, +attrs: ~[ast::attribute], sess: @mut ParseSess) -> @ast::stmt { - let p = new_parser_from_source_str(sess, cfg, name, + let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, codemap::FssNone, source); let r = p.parse_stmt(attrs); p.abort_if_errors(); @@ -139,7 +139,7 @@ pub fn parse_tts_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg, sess: @mut ParseSess) -> ~[ast::token_tree] { - let p = new_parser_from_source_str(sess, cfg, name, + let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, codemap::FssNone, source); *p.quote_depth += 1u; let r = p.parse_all_token_trees(); @@ -153,8 +153,13 @@ pub fn parse_from_source_str(f: fn (p: Parser) -> T, sess: @mut ParseSess) -> T { - let p = new_parser_from_source_str(sess, cfg, name, ss, - source); + let p = new_parser_from_source_str( + sess, + cfg, + /*bad*/ copy name, + /*bad*/ copy ss, + source + ); let r = f(p); if !p.reader.is_eof() { p.reader.fatal(~"expected end-of-string"); @@ -226,7 +231,7 @@ pub fn new_sub_parser_from_file(sess: @mut ParseSess, cfg: ast::crate_cfg, } pub fn new_parser_from_tts(sess: @mut ParseSess, cfg: ast::crate_cfg, - tts: ~[ast::token_tree]) -> Parser { + +tts: ~[ast::token_tree]) -> Parser { let trdr = lexer::new_tt_reader(copy sess.span_diagnostic, sess.interner, None, tts); return Parser(sess, cfg, trdr as reader) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 694e8387958..2c7947090c2 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -147,16 +147,25 @@ macro_rules! maybe_whole_expr ( ) macro_rules! maybe_whole ( - ($p:expr, $constructor:ident) => ( match *$p.token { - INTERPOLATED(token::$constructor(x)) => { $p.bump(); return x; } + ($p:expr, $constructor:ident) => ( match copy *$p.token { + INTERPOLATED(token::$constructor(x)) => { + $p.bump(); + return x; + } _ => () }) ; - (deref $p:expr, $constructor:ident) => ( match *$p.token { - INTERPOLATED(token::$constructor(x)) => { $p.bump(); return *x; } + (deref $p:expr, $constructor:ident) => ( match copy *$p.token { + INTERPOLATED(token::$constructor(x)) => { + $p.bump(); + return copy *x; + } _ => () }) ; - (Some $p:expr, $constructor:ident) => ( match *$p.token { - INTERPOLATED(token::$constructor(x)) => { $p.bump(); return Some(x); } + (Some $p:expr, $constructor:ident) => ( match copy *$p.token { + INTERPOLATED(token::$constructor(x)) => { + $p.bump(); + return Some(x); + } _ => () }) ; (iovi $p:expr, $constructor:ident) => ( match *$p.token { @@ -166,9 +175,10 @@ macro_rules! maybe_whole ( } _ => () }) ; - (pair_empty $p:expr, $constructor:ident) => ( match *$p.token { + (pair_empty $p:expr, $constructor:ident) => ( match copy *$p.token { INTERPOLATED(token::$constructor(x)) => { - $p.bump(); return (~[], x); + $p.bump(); + return (~[], x); } _ => () }) @@ -612,8 +622,11 @@ pub impl Parser { one_tuple = true; } } - let t = if ts.len() == 1 && !one_tuple { ts[0].node } - else { ty_tup(ts) }; + let t = if ts.len() == 1 && !one_tuple { + copy ts[0].node + } else { + ty_tup(ts) + }; self.expect(&token::RPAREN); t } @@ -972,7 +985,7 @@ pub impl Parser { @ast::path { span: mk_sp(lo, hi), rp: rp, types: tps, - .. *path } + .. copy *path } } fn parse_opt_lifetime() -> Option { @@ -1440,7 +1453,12 @@ pub impl Parser { |p| p.parse_token_tree() ); let (s, z) = p.parse_sep_and_zerok(); - tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z) + tt_seq( + mk_sp(sp.lo ,p.span.hi), + /*bad*/ copy seq.node, + s, + z + ) } else { tt_nonterminal(sp, p.parse_ident()) } @@ -1453,7 +1471,7 @@ pub impl Parser { // turn the next token into a tt_tok: fn parse_any_tt_tok(p: &Parser) -> token_tree{ - let res = tt_tok(*p.span, *p.token); + let res = tt_tok(*p.span, copy *p.token); p.bump(); res } @@ -1562,7 +1580,7 @@ pub impl Parser { m } } else { - let m = match_tok(*self.token); + let m = match_tok(copy *self.token); self.bump(); m }; @@ -1665,7 +1683,7 @@ pub impl Parser { fn parse_more_binops(lhs: @expr, min_prec: uint) -> @expr { if self.expr_is_complete(lhs) { return lhs; } - let peeked = *self.token; + let peeked = copy *self.token; if peeked == token::BINOP(token::OR) && (*self.restriction == RESTRICT_NO_BAR_OP || *self.restriction == RESTRICT_NO_BAR_OR_DOUBLEBAR_OP) { @@ -1859,7 +1877,7 @@ pub impl Parser { // Turn on the restriction to stop at | or || so we can parse // them as the lambda arguments let e = self.parse_expr_res(RESTRICT_NO_BAR_OR_DOUBLEBAR_OP); - match e.node { + match /*bad*/ copy e.node { expr_call(f, args, NoSugar) => { let block = self.parse_lambda_block_expr(); let last_arg = self.mk_expr(block.span.lo, block.span.hi, @@ -2441,7 +2459,7 @@ pub impl Parser { fn parse_stmt(+first_item_attrs: ~[attribute]) -> @stmt { maybe_whole!(self, nt_stmt); - fn check_expected_item(p: &Parser, current_attrs: ~[attribute]) { + fn check_expected_item(p: &Parser, current_attrs: &[attribute]) { // If we have attributes then we should have an item if !current_attrs.is_empty() { p.fatal(~"expected item after attrs"); @@ -2497,7 +2515,7 @@ pub impl Parser { let item_attrs = vec::append(first_item_attrs, self.parse_outer_attributes()); - match self.parse_item_or_view_item(item_attrs, + match self.parse_item_or_view_item(/*bad*/ copy item_attrs, true, false, false) { iovi_item(i) => { let mut hi = i.span.hi; @@ -2614,7 +2632,7 @@ pub impl Parser { self.bump(); stmts.push(@codemap::spanned { node: stmt_semi(e, stmt_id), - .. *stmt}); + .. copy *stmt}); } token::RBRACE => { expr = Some(e); @@ -2640,8 +2658,8 @@ pub impl Parser { token::SEMI => { self.bump(); stmts.push(@codemap::spanned { - node: stmt_mac((*m), true), - .. *stmt}); + node: stmt_mac(copy *m, true), + .. copy *stmt}); } token::RBRACE => { // if a block ends in `m!(arg)` without @@ -2649,7 +2667,7 @@ pub impl Parser { expr = Some( self.mk_mac_expr(stmt.span.lo, stmt.span.hi, - (*m).node)); + copy m.node)); } _ => { stmts.push(stmt); } } @@ -2990,6 +3008,7 @@ pub impl Parser { let self_ty = if is_static { static_sty} else { self_ty }; let (inner_attrs, body) = self.parse_inner_attrs_and_block(true); + let hi = body.span.hi; let attrs = vec::append(attrs, inner_attrs); @ast::method { ident: ident, @@ -3000,7 +3019,7 @@ pub impl Parser { decl: decl, body: body, id: self.get_id(), - span: mk_sp(lo, body.span.hi), + span: mk_sp(lo, hi), self_id: self.get_id(), vis: visa, } @@ -3161,7 +3180,7 @@ pub impl Parser { declared here"); } None => { - the_dtor = Some(((*blk), (*attrs), s)); + the_dtor = Some((copy *blk, copy *attrs, s)); } } } @@ -3205,7 +3224,7 @@ pub impl Parser { } let actual_dtor = do the_dtor.map |dtor| { - let (d_body, d_attrs, d_s) = *dtor; + let (d_body, d_attrs, d_s) = copy *dtor; codemap::spanned { node: ast::struct_dtor_ { id: self.get_id(), attrs: d_attrs, self_id: self.get_id(), @@ -3257,7 +3276,7 @@ pub impl Parser { a_var } - fn parse_dtor(attrs: ~[attribute]) -> class_contents { + fn parse_dtor(+attrs: ~[attribute]) -> class_contents { let lo = self.last_span.lo; let body = self.parse_block(); dtor_decl(body, attrs, mk_sp(lo, self.last_span.hi)) @@ -3323,12 +3342,17 @@ pub impl Parser { while *self.token != term { let mut attrs = self.parse_outer_attributes(); if first { - attrs = vec::append(attrs_remaining, attrs); + attrs = vec::append(/*bad*/ copy attrs_remaining, attrs); first = false; } debug!("parse_mod_items: parse_item_or_view_item(attrs=%?)", attrs); - match self.parse_item_or_view_item(attrs, true, false, true) { + match self.parse_item_or_view_item( + /*bad*/ copy attrs, + true, + false, + true + ) { iovi_item(item) => items.push(item), iovi_view_item(view_item) => { self.span_fatal(view_item.span, ~"view items must be \ @@ -3456,7 +3480,7 @@ pub impl Parser { outer_attrs, id_sp) } - fn eval_src_mod_from_path(prefix: Path, path: Path, + fn eval_src_mod_from_path(prefix: Path, +path: Path, outer_attrs: ~[ast::attribute], id_sp: span ) -> (ast::item_, ~[ast::attribute]) { @@ -3471,12 +3495,15 @@ pub impl Parser { new_sub_parser_from_file(self.sess, self.cfg, &full_path, id_sp); let (inner, next) = p0.parse_inner_attrs_and_next(); - let mod_attrs = vec::append(outer_attrs, inner); + let mod_attrs = vec::append( + /*bad*/ copy 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); - fn cdir_path_opt(default: ~str, attrs: ~[ast::attribute]) -> ~str { + fn cdir_path_opt(+default: ~str, attrs: ~[ast::attribute]) -> ~str { match ::attr::first_attr_value_str_by_name(attrs, ~"path") { Some(d) => copy *d, None => default @@ -3631,8 +3658,9 @@ pub impl Parser { self.expect(&token::RBRACE); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, - item_foreign_mod(m), visibility, - maybe_append(attrs, Some(inner)))); + item_foreign_mod(m), visibility, + maybe_append(/*bad*/ copy attrs, + Some(inner)))); } match abi_opt { @@ -3648,7 +3676,7 @@ pub impl Parser { self.expect(&token::SEMI); iovi_view_item(@ast::view_item { node: view_item_extern_mod(ident, metadata, self.get_id()), - attrs: attrs, + attrs: copy attrs, vis: visibility, span: mk_sp(lo, self.last_span.hi) }) @@ -3691,7 +3719,7 @@ pub impl Parser { declared here"); } None => { - the_dtor = Some(((*blk), (*attrs), s)); + the_dtor = Some((copy *blk, copy *attrs, s)); } } } @@ -3704,7 +3732,7 @@ pub impl Parser { } self.bump(); let mut actual_dtor = do the_dtor.map |dtor| { - let (d_body, d_attrs, d_s) = *dtor; + let (d_body, d_attrs, d_s) = copy *dtor; codemap::spanned { node: ast::struct_dtor_ { id: self.get_id(), attrs: d_attrs, self_id: self.get_id(), @@ -4216,7 +4244,8 @@ pub impl Parser { let mut (view_items, items, foreign_items) = (~[], ~[], ~[]); loop { - match self.parse_item_or_view_item(attrs, items_allowed, + match self.parse_item_or_view_item(/*bad*/ copy attrs, + items_allowed, foreign_items_allowed, macros_allowed) { iovi_none => @@ -4265,7 +4294,7 @@ pub impl Parser { @spanned(lo, self.span.lo, ast::crate_ { module: m, attrs: inner, - config: self.cfg }) + config: copy self.cfg }) } fn parse_str() -> @~str { diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 2b9df61120e..8b063314c9b 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -179,14 +179,14 @@ pub fn to_str(in: @ident_interner, t: &Token) -> ~str { } LIT_INT_UNSUFFIXED(i) => { i.to_str() } LIT_FLOAT(s, t) => { - let mut body = *in.get(s); + let mut body = copy *in.get(s); if body.ends_with(~".") { body = body + ~"0"; // `10.f` is not a float literal } body + ast_util::float_ty_to_str(t) } LIT_FLOAT_UNSUFFIXED(s) => { - let mut body = *in.get(s); + let mut body = copy *in.get(s); if body.ends_with(~".") { body = body + ~"0"; // `10.f` is not a float literal } @@ -195,12 +195,12 @@ pub fn to_str(in: @ident_interner, t: &Token) -> ~str { LIT_STR(s) => { ~"\"" + str::escape_default(*in.get(s)) + ~"\"" } /* Name components */ - IDENT(s, _) => *in.get(s), + IDENT(s, _) => copy *in.get(s), LIFETIME(s) => fmt!("'%s", *in.get(s)), UNDERSCORE => ~"_", /* Other */ - DOC_COMMENT(s) => *in.get(s), + DOC_COMMENT(s) => copy *in.get(s), EOF => ~"", INTERPOLATED(ref nt) => { match nt { @@ -476,7 +476,7 @@ pub fn temporary_keyword_table() -> HashMap<~str, ()> { ~"self", ~"static", ]; for keys.each |word| { - words.insert(*word, ()); + words.insert(copy *word, ()); } words } @@ -503,7 +503,7 @@ pub fn strict_keyword_table() -> HashMap<~str, ()> { ~"while" ]; for keys.each |word| { - words.insert(*word, ()); + words.insert(copy *word, ()); } words } @@ -514,7 +514,7 @@ pub fn reserved_keyword_table() -> HashMap<~str, ()> { ~"be" ]; for keys.each |word| { - words.insert(*word, ()); + words.insert(copy *word, ()); } words } -- cgit 1.4.1-3-g733a5 From ea36a0dee1630e24ba2889ca13550026b1af4f9d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 26 Feb 2013 20:18:01 -0800 Subject: libsyntax: add some more explicit copies --- src/libsyntax/ext/deriving.rs | 2 +- src/libsyntax/ext/trace_macros.rs | 6 +- src/libsyntax/ext/tt/macro_parser.rs | 21 ++-- src/libsyntax/ext/tt/transcribe.rs | 4 +- src/libsyntax/parse/mod.rs | 216 ++++++++++++++++++++++------------- src/libsyntax/parse/parser.rs | 2 +- 6 files changed, 160 insertions(+), 91 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ext/deriving.rs b/src/libsyntax/ext/deriving.rs index 54fb6bd3bb6..81d2aee46a6 100644 --- a/src/libsyntax/ext/deriving.rs +++ b/src/libsyntax/ext/deriving.rs @@ -838,7 +838,7 @@ fn expand_deriving_eq_struct_tuple_method(cx: ext_ctxt, let self_str = ~"self"; let other_str = ~"__other"; let type_path = build::mk_raw_path(span, ~[type_ident]); - let fields = struct_def.fields; + let fields = copy struct_def.fields; // Create comparison expression, comparing each of the fields let mut match_body = None; diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index fb7b41be2d6..bb6d656d5cc 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -29,7 +29,11 @@ pub fn expand_trace_macros(cx: ext_ctxt, sp: span, vec::from_slice(tt) ); let rdr = tt_rdr as reader; - let rust_parser = Parser(sess, cfg, rdr.dup()); + let rust_parser = Parser( + sess, + copy cfg, + rdr.dup() + ); if rust_parser.is_keyword(&~"true") { cx.set_trace_macros(true); diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 86c4cbee04b..419c051ea97 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -224,8 +224,12 @@ pub enum parse_result { error(codemap::span, ~str) } -pub fn parse_or_else(sess: @mut ParseSess, cfg: ast::crate_cfg, rdr: reader, - ms: ~[matcher]) -> HashMap { +pub fn parse_or_else( + sess: @mut ParseSess, + +cfg: ast::crate_cfg, + rdr: reader, + ms: ~[matcher] +) -> HashMap { match parse(sess, cfg, rdr, ms) { success(m) => m, failure(sp, ref str) => sess.span_diagnostic.span_fatal(sp, (*str)), @@ -233,11 +237,12 @@ pub fn parse_or_else(sess: @mut ParseSess, cfg: ast::crate_cfg, rdr: reader, } } -pub fn parse(sess: @mut ParseSess, - cfg: ast::crate_cfg, - rdr: reader, - ms: ~[matcher]) - -> parse_result { +pub fn parse( + sess: @mut ParseSess, + cfg: ast::crate_cfg, + rdr: reader, + ms: ~[matcher] +) -> parse_result { let mut cur_eis = ~[]; cur_eis.push(initial_matcher_pos(copy ms, None, rdr.peek().sp.lo)); @@ -387,7 +392,7 @@ pub fn parse(sess: @mut ParseSess, } rdr.next_token(); } else /* bb_eis.len() == 1 */ { - let rust_parser = Parser(sess, cfg, rdr.dup()); + let rust_parser = Parser(sess, copy cfg, rdr.dup()); let mut ei = bb_eis.pop(); match ei.elts[ei.idx].node { diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 4aa6236bf5a..99afd7958e9 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -202,9 +202,9 @@ pub fn tt_next_token(r: @mut TtReader) -> TokenAndSpan { loop { /* because it's easiest, this handles `tt_delim` not starting with a `tt_tok`, even though it won't happen */ match r.cur.readme[r.cur.idx] { - tt_delim(tts) => { + tt_delim(copy tts) => { r.cur = @mut TtFrame { - readme: @mut copy tts, + readme: @mut tts, idx: 0u, dotdotdoted: false, sep: None, diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index cf5cb847018..d8c3ca06d76 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -81,78 +81,116 @@ pub fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap) // this appears to be the main entry point for rust parsing by // rustc and crate: -pub fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg, - sess: @mut ParseSess) -> @ast::crate { - let p = new_parser_from_file(sess, cfg, input); - p.parse_crate_mod(cfg) +pub fn parse_crate_from_file( + input: &Path, + cfg: ast::crate_cfg, + sess: @mut ParseSess +) -> @ast::crate { + let p = new_parser_from_file(sess, /*bad*/ copy cfg, input); + p.parse_crate_mod(/*bad*/ copy cfg) // why is there no p.abort_if_errors here? } -pub fn parse_crate_from_source_str(name: ~str, - source: @~str, - cfg: ast::crate_cfg, - sess: @mut ParseSess) -> @ast::crate { - let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, - codemap::FssNone, source); - let r = p.parse_crate_mod(cfg); +pub fn parse_crate_from_source_str( + name: ~str, + source: @~str, + cfg: ast::crate_cfg, + sess: @mut ParseSess +) -> @ast::crate { + let p = new_parser_from_source_str( + sess, + /*bad*/ copy cfg, + /*bad*/ copy name, + codemap::FssNone, + source + ); + let r = p.parse_crate_mod(/*bad*/ copy cfg); p.abort_if_errors(); - return r; + r } -pub fn parse_expr_from_source_str(name: ~str, - source: @~str, - cfg: ast::crate_cfg, - sess: @mut ParseSess) -> @ast::expr { - let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, - codemap::FssNone, source); +pub fn parse_expr_from_source_str( + name: ~str, + source: @~str, + +cfg: ast::crate_cfg, + sess: @mut ParseSess +) -> @ast::expr { + let p = new_parser_from_source_str( + sess, + cfg, + /*bad*/ copy name, + codemap::FssNone, + source + ); let r = p.parse_expr(); p.abort_if_errors(); - return r; + r } -pub fn parse_item_from_source_str(name: ~str, - source: @~str, - cfg: ast::crate_cfg, - +attrs: ~[ast::attribute], - sess: @mut ParseSess) - -> Option<@ast::item> { - let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, - codemap::FssNone, source); +pub fn parse_item_from_source_str( + name: ~str, + source: @~str, + +cfg: ast::crate_cfg, + +attrs: ~[ast::attribute], + sess: @mut ParseSess +) -> Option<@ast::item> { + let p = new_parser_from_source_str( + sess, + cfg, + /*bad*/ copy name, + codemap::FssNone, + source + ); let r = p.parse_item(attrs); p.abort_if_errors(); - return r; + r } -pub fn parse_stmt_from_source_str(name: ~str, - source: @~str, - cfg: ast::crate_cfg, - +attrs: ~[ast::attribute], - sess: @mut ParseSess) -> @ast::stmt { - let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, - codemap::FssNone, source); +pub fn parse_stmt_from_source_str( + name: ~str, + source: @~str, + +cfg: ast::crate_cfg, + +attrs: ~[ast::attribute], + sess: @mut ParseSess +) -> @ast::stmt { + let p = new_parser_from_source_str( + sess, + cfg, + /*bad*/ copy name, + codemap::FssNone, + source + ); let r = p.parse_stmt(attrs); p.abort_if_errors(); - return r; + r } -pub fn parse_tts_from_source_str(name: ~str, - source: @~str, - cfg: ast::crate_cfg, - sess: @mut ParseSess) -> ~[ast::token_tree] { - let p = new_parser_from_source_str(sess, cfg, /*bad*/ copy name, - codemap::FssNone, source); +pub fn parse_tts_from_source_str( + name: ~str, + source: @~str, + +cfg: ast::crate_cfg, + sess: @mut ParseSess +) -> ~[ast::token_tree] { + let p = new_parser_from_source_str( + sess, + cfg, + /*bad*/ copy name, + codemap::FssNone, + source + ); *p.quote_depth += 1u; let r = p.parse_all_token_trees(); p.abort_if_errors(); - return r; + r } -pub fn parse_from_source_str(f: fn (p: Parser) -> T, - name: ~str, ss: codemap::FileSubstr, - source: @~str, cfg: ast::crate_cfg, - sess: @mut ParseSess) - -> T -{ +pub fn parse_from_source_str( + f: fn (Parser) -> T, + name: ~str, ss: codemap::FileSubstr, + source: @~str, + +cfg: ast::crate_cfg, + sess: @mut ParseSess +) -> T { let p = new_parser_from_source_str( sess, cfg, @@ -176,40 +214,51 @@ pub fn next_node_id(sess: @mut ParseSess) -> node_id { return rv; } -pub fn new_parser_from_source_str(sess: @mut ParseSess, cfg: ast::crate_cfg, - +name: ~str, +ss: codemap::FileSubstr, - source: @~str) -> Parser { +pub fn new_parser_from_source_str( + sess: @mut ParseSess, + +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(copy sess.span_diagnostic, - filemap, - sess.interner); - return Parser(sess, cfg, srdr as reader); + let srdr = lexer::new_string_reader( + copy sess.span_diagnostic, + filemap, + sess.interner + ); + Parser(sess, cfg, srdr as reader) } // Read the entire source file, return a parser // that draws from that string -pub fn new_parser_result_from_file(sess: @mut ParseSess, - cfg: ast::crate_cfg, - path: &Path) - -> Result { +pub fn new_parser_result_from_file( + sess: @mut ParseSess, + +cfg: ast::crate_cfg, + path: &Path +) -> Result { match io::read_whole_file_str(path) { - result::Ok(src) => { - - let filemap = sess.cm.new_filemap(path.to_str(), @src); - let srdr = lexer::new_string_reader(copy sess.span_diagnostic, - filemap, - sess.interner); - Ok(Parser(sess, cfg, srdr as reader)) + Ok(src) => { + let filemap = sess.cm.new_filemap(path.to_str(), @src); + let srdr = lexer::new_string_reader( + copy sess.span_diagnostic, + filemap, + sess.interner + ); + Ok(Parser(sess, cfg, srdr as reader)) - } - result::Err(e) => Err(e) + } + Err(e) => Err(e) } } /// Create a new parser for an entire crate, handling errors as appropriate /// if the file doesn't exist -pub fn new_parser_from_file(sess: @mut ParseSess, cfg: ast::crate_cfg, - path: &Path) -> Parser { +pub fn new_parser_from_file( + sess: @mut ParseSess, + +cfg: ast::crate_cfg, + path: &Path +) -> Parser { match new_parser_result_from_file(sess, cfg, path) { Ok(parser) => parser, Err(e) => { @@ -220,8 +269,12 @@ pub fn new_parser_from_file(sess: @mut ParseSess, cfg: ast::crate_cfg, /// Create a new parser based on a span from an existing parser. Handles /// error messages correctly when the file does not exist. -pub fn new_sub_parser_from_file(sess: @mut ParseSess, cfg: ast::crate_cfg, - path: &Path, sp: span) -> Parser { +pub fn new_sub_parser_from_file( + sess: @mut ParseSess, + +cfg: ast::crate_cfg, + path: &Path, + sp: span +) -> Parser { match new_parser_result_from_file(sess, cfg, path) { Ok(parser) => parser, Err(e) => { @@ -230,11 +283,18 @@ pub fn new_sub_parser_from_file(sess: @mut ParseSess, cfg: ast::crate_cfg, } } -pub fn new_parser_from_tts(sess: @mut ParseSess, cfg: ast::crate_cfg, - +tts: ~[ast::token_tree]) -> Parser { - let trdr = lexer::new_tt_reader(copy sess.span_diagnostic, sess.interner, - None, tts); - return Parser(sess, cfg, trdr as reader) +pub fn new_parser_from_tts( + sess: @mut ParseSess, + +cfg: ast::crate_cfg, + +tts: ~[ast::token_tree] +) -> Parser { + let trdr = lexer::new_tt_reader( + copy sess.span_diagnostic, + sess.interner, + None, + tts + ); + Parser(sess, cfg, trdr as reader) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2c7947090c2..dffa04ac1ca 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3492,7 +3492,7 @@ pub impl Parser { }; let full_path = full_path.normalize(); let p0 = - new_sub_parser_from_file(self.sess, self.cfg, + new_sub_parser_from_file(self.sess, copy self.cfg, &full_path, id_sp); let (inner, next) = p0.parse_inner_attrs_and_next(); let mod_attrs = vec::append( -- cgit 1.4.1-3-g733a5