]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/parser.rs
doc: remove incomplete sentence
[rust.git] / src / libsyntax / parse / parser.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![macro_escape]
12
13 pub use self::PathParsingMode::*;
14 use self::ItemOrViewItem::*;
15
16 use abi;
17 use ast::{AssociatedType, BareFnTy, ClosureTy};
18 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
19 use ast::{ProvidedMethod, Public, Unsafety};
20 use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue};
21 use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, Block};
22 use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause};
23 use ast::{Crate, CrateConfig, Decl, DeclItem};
24 use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf};
25 use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain};
26 use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox};
27 use ast::{ExprBreak, ExprCall, ExprCast};
28 use ast::{ExprField, ExprTupField, ExprClosure, ExprIf, ExprIfLet, ExprIndex};
29 use ast::{ExprLit, ExprLoop, ExprMac, ExprRange};
30 use ast::{ExprMethodCall, ExprParen, ExprPath};
31 use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary};
32 use ast::{ExprVec, ExprWhile, ExprWhileLet, ExprForLoop, Field, FnDecl};
33 use ast::{Many};
34 use ast::{FnUnboxedClosureKind, FnMutUnboxedClosureKind};
35 use ast::{FnOnceUnboxedClosureKind};
36 use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod, FunctionRetTy};
37 use ast::{Ident, Inherited, ImplItem, Item, Item_, ItemStatic};
38 use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemConst};
39 use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy};
40 use ast::{LifetimeDef, Lit, Lit_};
41 use ast::{LitBool, LitChar, LitByte, LitBinary};
42 use ast::{LitStr, LitInt, Local, LocalLet};
43 use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
44 use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchSource};
45 use ast::{Method, MutTy, BiMul, Mutability};
46 use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, NodeId, UnNot};
47 use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
48 use ast::{PatTup, PatBox, PatWild, PatWildMulti, PatWildSingle};
49 use ast::{PolyTraitRef};
50 use ast::{QPath, RequiredMethod};
51 use ast::{Return, BiShl, BiShr, Stmt, StmtDecl};
52 use ast::{StmtExpr, StmtSemi, StmtMac, StructDef, StructField};
53 use ast::{StructVariantKind, BiSub, StrStyle};
54 use ast::{SelfExplicit, SelfRegion, SelfStatic, SelfValue};
55 use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
56 use ast::{TtDelimited, TtSequence, TtToken};
57 use ast::{TupleVariantKind, Ty, Ty_, TypeBinding};
58 use ast::{TypeField, TyFixedLengthVec, TyClosure, TyBareFn};
59 use ast::{TyTypeof, TyInfer, TypeMethod};
60 use ast::{TyParam, TyParamBound, TyParen, TyPath, TyPolyTraitRef, TyPtr, TyQPath};
61 use ast::{TyRptr, TyTup, TyU32, TyVec, UnUniq};
62 use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind};
63 use ast::{UnnamedField, UnsafeBlock};
64 use ast::{ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse};
65 use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
66 use ast::{Visibility, WhereClause};
67 use ast;
68 use ast_util::{mod, as_prec, ident_to_path, operator_prec};
69 use codemap::{mod, Span, BytePos, Spanned, spanned, mk_sp, DUMMY_SP};
70 use diagnostic;
71 use ext::tt::macro_parser;
72 use parse;
73 use parse::attr::ParserAttr;
74 use parse::classify;
75 use parse::common::{SeqSep, seq_sep_none, seq_sep_trailing_allowed};
76 use parse::lexer::{Reader, TokenAndSpan};
77 use parse::obsolete::*;
78 use parse::token::{mod, MatchNt, SubstNt, InternedString};
79 use parse::token::{keywords, special_idents};
80 use parse::{new_sub_parser_from_file, ParseSess};
81 use print::pprust;
82 use ptr::P;
83 use owned_slice::OwnedSlice;
84
85 use std::collections::HashSet;
86 use std::io::fs::PathExtensions;
87 use std::iter;
88 use std::mem;
89 use std::num::Float;
90 use std::rc::Rc;
91 use std::slice;
92 use std::str::from_str;
93
94 bitflags! {
95     flags Restrictions: u8 {
96         const UNRESTRICTED                  = 0b0000,
97         const RESTRICTION_STMT_EXPR         = 0b0001,
98         const RESTRICTION_NO_BAR_OP         = 0b0010,
99         const RESTRICTION_NO_STRUCT_LITERAL = 0b0100,
100         const RESTRICTION_NO_DOTS           = 0b1000,
101     }
102 }
103
104
105 type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
106
107 /// How to parse a path. There are four different kinds of paths, all of which
108 /// are parsed somewhat differently.
109 #[deriving(Copy, PartialEq)]
110 pub enum PathParsingMode {
111     /// A path with no type parameters; e.g. `foo::bar::Baz`
112     NoTypesAllowed,
113     /// A path with a lifetime and type parameters, with no double colons
114     /// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`
115     LifetimeAndTypesWithoutColons,
116     /// A path with a lifetime and type parameters with double colons before
117     /// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`
118     LifetimeAndTypesWithColons,
119 }
120
121 /// How to parse a bound, whether to allow bound modifiers such as `?`.
122 #[deriving(Copy, PartialEq)]
123 pub enum BoundParsingMode {
124     Bare,
125     Modified,
126 }
127
128 enum ItemOrViewItem {
129     /// Indicates a failure to parse any kind of item. The attributes are
130     /// returned.
131     IoviNone(Vec<Attribute>),
132     IoviItem(P<Item>),
133     IoviForeignItem(P<ForeignItem>),
134     IoviViewItem(ViewItem)
135 }
136
137
138 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
139 /// dropped into the token stream, which happens while parsing the result of
140 /// macro expansion). Placement of these is not as complex as I feared it would
141 /// be. The important thing is to make sure that lookahead doesn't balk at
142 /// `token::Interpolated` tokens.
143 macro_rules! maybe_whole_expr {
144     ($p:expr) => (
145         {
146             let found = match $p.token {
147                 token::Interpolated(token::NtExpr(ref e)) => {
148                     Some((*e).clone())
149                 }
150                 token::Interpolated(token::NtPath(_)) => {
151                     // FIXME: The following avoids an issue with lexical borrowck scopes,
152                     // but the clone is unfortunate.
153                     let pt = match $p.token {
154                         token::Interpolated(token::NtPath(ref pt)) => (**pt).clone(),
155                         _ => unreachable!()
156                     };
157                     let span = $p.span;
158                     Some($p.mk_expr(span.lo, span.hi, ExprPath(pt)))
159                 }
160                 token::Interpolated(token::NtBlock(_)) => {
161                     // FIXME: The following avoids an issue with lexical borrowck scopes,
162                     // but the clone is unfortunate.
163                     let b = match $p.token {
164                         token::Interpolated(token::NtBlock(ref b)) => (*b).clone(),
165                         _ => unreachable!()
166                     };
167                     let span = $p.span;
168                     Some($p.mk_expr(span.lo, span.hi, ExprBlock(b)))
169                 }
170                 _ => None
171             };
172             match found {
173                 Some(e) => {
174                     $p.bump();
175                     return e;
176                 }
177                 None => ()
178             }
179         }
180     )
181 }
182
183 /// As maybe_whole_expr, but for things other than expressions
184 macro_rules! maybe_whole {
185     ($p:expr, $constructor:ident) => (
186         {
187             let found = match ($p).token {
188                 token::Interpolated(token::$constructor(_)) => {
189                     Some(($p).bump_and_get())
190                 }
191                 _ => None
192             };
193             if let Some(token::Interpolated(token::$constructor(x))) = found {
194                 return x.clone();
195             }
196         }
197     );
198     (no_clone $p:expr, $constructor:ident) => (
199         {
200             let found = match ($p).token {
201                 token::Interpolated(token::$constructor(_)) => {
202                     Some(($p).bump_and_get())
203                 }
204                 _ => None
205             };
206             if let Some(token::Interpolated(token::$constructor(x))) = found {
207                 return x;
208             }
209         }
210     );
211     (deref $p:expr, $constructor:ident) => (
212         {
213             let found = match ($p).token {
214                 token::Interpolated(token::$constructor(_)) => {
215                     Some(($p).bump_and_get())
216                 }
217                 _ => None
218             };
219             if let Some(token::Interpolated(token::$constructor(x))) = found {
220                 return (*x).clone();
221             }
222         }
223     );
224     (Some $p:expr, $constructor:ident) => (
225         {
226             let found = match ($p).token {
227                 token::Interpolated(token::$constructor(_)) => {
228                     Some(($p).bump_and_get())
229                 }
230                 _ => None
231             };
232             if let Some(token::Interpolated(token::$constructor(x))) = found {
233                 return Some(x.clone());
234             }
235         }
236     );
237     (iovi $p:expr, $constructor:ident) => (
238         {
239             let found = match ($p).token {
240                 token::Interpolated(token::$constructor(_)) => {
241                     Some(($p).bump_and_get())
242                 }
243                 _ => None
244             };
245             if let Some(token::Interpolated(token::$constructor(x))) = found {
246                 return IoviItem(x.clone());
247             }
248         }
249     );
250     (pair_empty $p:expr, $constructor:ident) => (
251         {
252             let found = match ($p).token {
253                 token::Interpolated(token::$constructor(_)) => {
254                     Some(($p).bump_and_get())
255                 }
256                 _ => None
257             };
258             if let Some(token::Interpolated(token::$constructor(x))) = found {
259                 return (Vec::new(), x);
260             }
261         }
262     )
263 }
264
265
266 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
267                 -> Vec<Attribute> {
268     match rhs {
269         Some(ref attrs) => lhs.extend(attrs.iter().map(|a| a.clone())),
270         None => {}
271     }
272     lhs
273 }
274
275
276 struct ParsedItemsAndViewItems {
277     attrs_remaining: Vec<Attribute>,
278     view_items: Vec<ViewItem>,
279     items: Vec<P<Item>> ,
280     foreign_items: Vec<P<ForeignItem>>
281 }
282
283 /* ident is handled by common.rs */
284
285 pub struct Parser<'a> {
286     pub sess: &'a ParseSess,
287     /// the current token:
288     pub token: token::Token,
289     /// the span of the current token:
290     pub span: Span,
291     /// the span of the prior token:
292     pub last_span: Span,
293     pub cfg: CrateConfig,
294     /// the previous token or None (only stashed sometimes).
295     pub last_token: Option<Box<token::Token>>,
296     pub buffer: [TokenAndSpan; 4],
297     pub buffer_start: int,
298     pub buffer_end: int,
299     pub tokens_consumed: uint,
300     pub restrictions: Restrictions,
301     pub quote_depth: uint, // not (yet) related to the quasiquoter
302     pub reader: Box<Reader+'a>,
303     pub interner: Rc<token::IdentInterner>,
304     /// The set of seen errors about obsolete syntax. Used to suppress
305     /// extra detail when the same error is seen twice
306     pub obsolete_set: HashSet<ObsoleteSyntax>,
307     /// Used to determine the path to externally loaded source files
308     pub mod_path_stack: Vec<InternedString>,
309     /// Stack of spans of open delimiters. Used for error message.
310     pub open_braces: Vec<Span>,
311     /// Flag if this parser "owns" the directory that it is currently parsing
312     /// in. This will affect how nested files are looked up.
313     pub owns_directory: bool,
314     /// Name of the root module this parser originated from. If `None`, then the
315     /// name is not known. This does not change while the parser is descending
316     /// into modules, and sub-parsers have new values for this name.
317     pub root_module_name: Option<String>,
318     pub expected_tokens: Vec<TokenType>,
319 }
320
321 #[deriving(PartialEq, Eq, Clone)]
322 pub enum TokenType {
323     Token(token::Token),
324     Operator,
325 }
326
327 impl TokenType {
328     fn to_string(&self) -> String {
329         match *self {
330             TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
331             TokenType::Operator => "an operator".to_string(),
332         }
333     }
334 }
335
336 fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
337     t.is_plain_ident() || *t == token::Underscore
338 }
339
340 impl<'a> Parser<'a> {
341     pub fn new(sess: &'a ParseSess,
342                cfg: ast::CrateConfig,
343                mut rdr: Box<Reader+'a>)
344                -> Parser<'a>
345     {
346         let tok0 = rdr.real_token();
347         let span = tok0.sp;
348         let placeholder = TokenAndSpan {
349             tok: token::Underscore,
350             sp: span,
351         };
352
353         Parser {
354             reader: rdr,
355             interner: token::get_ident_interner(),
356             sess: sess,
357             cfg: cfg,
358             token: tok0.tok,
359             span: span,
360             last_span: span,
361             last_token: None,
362             buffer: [
363                 placeholder.clone(),
364                 placeholder.clone(),
365                 placeholder.clone(),
366                 placeholder.clone(),
367             ],
368             buffer_start: 0,
369             buffer_end: 0,
370             tokens_consumed: 0,
371             restrictions: UNRESTRICTED,
372             quote_depth: 0,
373             obsolete_set: HashSet::new(),
374             mod_path_stack: Vec::new(),
375             open_braces: Vec::new(),
376             owns_directory: true,
377             root_module_name: None,
378             expected_tokens: Vec::new(),
379         }
380     }
381
382     /// Convert a token to a string using self's reader
383     pub fn token_to_string(token: &token::Token) -> String {
384         pprust::token_to_string(token)
385     }
386
387     /// Convert the current token to a string using self's reader
388     pub fn this_token_to_string(&mut self) -> String {
389         Parser::token_to_string(&self.token)
390     }
391
392     pub fn unexpected_last(&mut self, t: &token::Token) -> ! {
393         let token_str = Parser::token_to_string(t);
394         let last_span = self.last_span;
395         self.span_fatal(last_span, format!("unexpected token: `{}`",
396                                                 token_str)[]);
397     }
398
399     pub fn unexpected(&mut self) -> ! {
400         let this_token = self.this_token_to_string();
401         self.fatal(format!("unexpected token: `{}`", this_token)[]);
402     }
403
404     /// Expect and consume the token t. Signal an error if
405     /// the next token is not t.
406     pub fn expect(&mut self, t: &token::Token) {
407         if self.expected_tokens.is_empty() {
408             if self.token == *t {
409                 self.bump();
410             } else {
411                 let token_str = Parser::token_to_string(t);
412                 let this_token_str = self.this_token_to_string();
413                 self.fatal(format!("expected `{}`, found `{}`",
414                                    token_str,
415                                    this_token_str)[])
416             }
417         } else {
418             self.expect_one_of(slice::ref_slice(t), &[]);
419         }
420     }
421
422     /// Expect next token to be edible or inedible token.  If edible,
423     /// then consume it; if inedible, then return without consuming
424     /// anything.  Signal a fatal error if next token is unexpected.
425     pub fn expect_one_of(&mut self,
426                          edible: &[token::Token],
427                          inedible: &[token::Token]) {
428         fn tokens_to_string(tokens: &[TokenType]) -> String {
429             let mut i = tokens.iter();
430             // This might be a sign we need a connect method on Iterator.
431             let b = i.next()
432                      .map_or("".to_string(), |t| t.to_string());
433             i.enumerate().fold(b, |mut b, (i, ref a)| {
434                 if tokens.len() > 2 && i == tokens.len() - 2 {
435                     b.push_str(", or ");
436                 } else if tokens.len() == 2 && i == tokens.len() - 2 {
437                     b.push_str(" or ");
438                 } else {
439                     b.push_str(", ");
440                 }
441                 b.push_str(&*a.to_string());
442                 b
443             })
444         }
445         if edible.contains(&self.token) {
446             self.bump();
447         } else if inedible.contains(&self.token) {
448             // leave it in the input
449         } else {
450             let mut expected = edible.iter().map(|x| TokenType::Token(x.clone()))
451                                             .collect::<Vec<_>>();
452             expected.extend(inedible.iter().map(|x| TokenType::Token(x.clone())));
453             expected.push_all(&*self.expected_tokens);
454             expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
455             expected.dedup();
456             let expect = tokens_to_string(expected[]);
457             let actual = self.this_token_to_string();
458             self.fatal(
459                 (if expected.len() != 1 {
460                     (format!("expected one of {}, found `{}`",
461                              expect,
462                              actual))
463                 } else {
464                     (format!("expected {}, found `{}`",
465                              expect,
466                              actual))
467                 })[]
468             )
469         }
470     }
471
472     /// Check for erroneous `ident { }`; if matches, signal error and
473     /// recover (without consuming any expected input token).  Returns
474     /// true if and only if input was consumed for recovery.
475     pub fn check_for_erroneous_unit_struct_expecting(&mut self, expected: &[token::Token]) -> bool {
476         if self.token == token::OpenDelim(token::Brace)
477             && expected.iter().all(|t| *t != token::OpenDelim(token::Brace))
478             && self.look_ahead(1, |t| *t == token::CloseDelim(token::Brace)) {
479             // matched; signal non-fatal error and recover.
480             let span = self.span;
481             self.span_err(span,
482                           "unit-like struct construction is written with no trailing `{ }`");
483             self.eat(&token::OpenDelim(token::Brace));
484             self.eat(&token::CloseDelim(token::Brace));
485             true
486         } else {
487             false
488         }
489     }
490
491     /// Commit to parsing a complete expression `e` expected to be
492     /// followed by some token from the set edible + inedible.  Recover
493     /// from anticipated input errors, discarding erroneous characters.
494     pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[token::Token]) {
495         debug!("commit_expr {}", e);
496         if let ExprPath(..) = e.node {
497             // might be unit-struct construction; check for recoverableinput error.
498             let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
499             expected.push_all(inedible);
500             self.check_for_erroneous_unit_struct_expecting(expected[]);
501         }
502         self.expect_one_of(edible, inedible)
503     }
504
505     pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) {
506         self.commit_expr(e, &[edible], &[])
507     }
508
509     /// Commit to parsing a complete statement `s`, which expects to be
510     /// followed by some token from the set edible + inedible.  Check
511     /// for recoverable input errors, discarding erroneous characters.
512     pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token]) {
513         if self.last_token
514                .as_ref()
515                .map_or(false, |t| t.is_ident() || t.is_path()) {
516             let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
517             expected.push_all(inedible[]);
518             self.check_for_erroneous_unit_struct_expecting(
519                 expected[]);
520         }
521         self.expect_one_of(edible, inedible)
522     }
523
524     pub fn commit_stmt_expecting(&mut self, edible: token::Token) {
525         self.commit_stmt(&[edible], &[])
526     }
527
528     pub fn parse_ident(&mut self) -> ast::Ident {
529         self.check_strict_keywords();
530         self.check_reserved_keywords();
531         match self.token {
532             token::Ident(i, _) => {
533                 self.bump();
534                 i
535             }
536             token::Interpolated(token::NtIdent(..)) => {
537                 self.bug("ident interpolation not converted to real token");
538             }
539             _ => {
540                 let token_str = self.this_token_to_string();
541                 self.fatal((format!("expected ident, found `{}`",
542                                     token_str))[])
543             }
544         }
545     }
546
547     pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
548         let lo = self.span.lo;
549         let node = if self.eat_keyword(keywords::Mod) {
550             let span = self.last_span;
551             self.span_warn(span, "deprecated syntax; use the `self` keyword now");
552             ast::PathListMod { id: ast::DUMMY_NODE_ID }
553         } else if self.eat_keyword(keywords::Self) {
554             ast::PathListMod { id: ast::DUMMY_NODE_ID }
555         } else {
556             let ident = self.parse_ident();
557             ast::PathListIdent { name: ident, id: ast::DUMMY_NODE_ID }
558         };
559         let hi = self.last_span.hi;
560         spanned(lo, hi, node)
561     }
562
563     /// Check if the next token is `tok`, and return `true` if so.
564     ///
565     /// This method is will automatically add `tok` to `expected_tokens` if `tok` is not
566     /// encountered.
567     pub fn check(&mut self, tok: &token::Token) -> bool {
568         let is_present = self.token == *tok;
569         if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
570         is_present
571     }
572
573     /// Consume token 'tok' if it exists. Returns true if the given
574     /// token was present, false otherwise.
575     pub fn eat(&mut self, tok: &token::Token) -> bool {
576         let is_present = self.check(tok);
577         if is_present { self.bump() }
578         is_present
579     }
580
581     /// If the next token is the given keyword, eat it and return
582     /// true. Otherwise, return false.
583     pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
584         if self.token.is_keyword(kw) {
585             self.bump();
586             true
587         } else {
588             false
589         }
590     }
591
592     /// If the given word is not a keyword, signal an error.
593     /// If the next token is not the given word, signal an error.
594     /// Otherwise, eat it.
595     pub fn expect_keyword(&mut self, kw: keywords::Keyword) {
596         if !self.eat_keyword(kw) {
597             let id_interned_str = token::get_name(kw.to_name());
598             let token_str = self.this_token_to_string();
599             self.fatal(format!("expected `{}`, found `{}`",
600                                id_interned_str, token_str)[])
601         }
602     }
603
604     /// Signal an error if the given string is a strict keyword
605     pub fn check_strict_keywords(&mut self) {
606         if self.token.is_strict_keyword() {
607             let token_str = self.this_token_to_string();
608             let span = self.span;
609             self.span_err(span,
610                           format!("expected identifier, found keyword `{}`",
611                                   token_str)[]);
612         }
613     }
614
615     /// Signal an error if the current token is a reserved keyword
616     pub fn check_reserved_keywords(&mut self) {
617         if self.token.is_reserved_keyword() {
618             let token_str = self.this_token_to_string();
619             self.fatal(format!("`{}` is a reserved keyword",
620                                token_str)[])
621         }
622     }
623
624     /// Expect and consume an `&`. If `&&` is seen, replace it with a single
625     /// `&` and continue. If an `&` is not seen, signal an error.
626     fn expect_and(&mut self) {
627         match self.token {
628             token::BinOp(token::And) => self.bump(),
629             token::AndAnd => {
630                 let span = self.span;
631                 let lo = span.lo + BytePos(1);
632                 self.replace_token(token::BinOp(token::And), lo, span.hi)
633             }
634             _ => {
635                 let token_str = self.this_token_to_string();
636                 let found_token =
637                     Parser::token_to_string(&token::BinOp(token::And));
638                 self.fatal(format!("expected `{}`, found `{}`",
639                                    found_token,
640                                    token_str)[])
641             }
642         }
643     }
644
645     /// Expect and consume a `|`. If `||` is seen, replace it with a single
646     /// `|` and continue. If a `|` is not seen, signal an error.
647     fn expect_or(&mut self) {
648         match self.token {
649             token::BinOp(token::Or) => self.bump(),
650             token::OrOr => {
651                 let span = self.span;
652                 let lo = span.lo + BytePos(1);
653                 self.replace_token(token::BinOp(token::Or), lo, span.hi)
654             }
655             _ => {
656                 let found_token = self.this_token_to_string();
657                 let token_str =
658                     Parser::token_to_string(&token::BinOp(token::Or));
659                 self.fatal(format!("expected `{}`, found `{}`",
660                                    token_str,
661                                    found_token)[])
662             }
663         }
664     }
665
666     pub fn expect_no_suffix(&mut self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
667         match suffix {
668             None => {/* everything ok */}
669             Some(suf) => {
670                 let text = suf.as_str();
671                 if text.is_empty() {
672                     self.span_bug(sp, "found empty literal suffix in Some")
673                 }
674                 self.span_err(sp, &*format!("{} with a suffix is illegal", kind));
675             }
676         }
677     }
678
679
680     /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
681     /// `<` and continue. If a `<` is not seen, return false.
682     ///
683     /// This is meant to be used when parsing generics on a path to get the
684     /// starting token.
685     fn eat_lt(&mut self) -> bool {
686         match self.token {
687             token::Lt => { self.bump(); true }
688             token::BinOp(token::Shl) => {
689                 let span = self.span;
690                 let lo = span.lo + BytePos(1);
691                 self.replace_token(token::Lt, lo, span.hi);
692                 true
693             }
694             _ => false,
695         }
696     }
697
698     fn expect_lt(&mut self) {
699         if !self.eat_lt() {
700             let found_token = self.this_token_to_string();
701             let token_str = Parser::token_to_string(&token::Lt);
702             self.fatal(format!("expected `{}`, found `{}`",
703                                token_str,
704                                found_token)[])
705         }
706     }
707
708     /// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
709     fn parse_seq_to_before_or<T, F>(&mut self,
710                                     sep: &token::Token,
711                                     mut f: F)
712                                     -> Vec<T> where
713         F: FnMut(&mut Parser) -> T,
714     {
715         let mut first = true;
716         let mut vector = Vec::new();
717         while self.token != token::BinOp(token::Or) &&
718                 self.token != token::OrOr {
719             if first {
720                 first = false
721             } else {
722                 self.expect(sep)
723             }
724
725             vector.push(f(self))
726         }
727         vector
728     }
729
730     /// Expect and consume a GT. if a >> is seen, replace it
731     /// with a single > and continue. If a GT is not seen,
732     /// signal an error.
733     pub fn expect_gt(&mut self) {
734         match self.token {
735             token::Gt => self.bump(),
736             token::BinOp(token::Shr) => {
737                 let span = self.span;
738                 let lo = span.lo + BytePos(1);
739                 self.replace_token(token::Gt, lo, span.hi)
740             }
741             token::BinOpEq(token::Shr) => {
742                 let span = self.span;
743                 let lo = span.lo + BytePos(1);
744                 self.replace_token(token::Ge, lo, span.hi)
745             }
746             token::Ge => {
747                 let span = self.span;
748                 let lo = span.lo + BytePos(1);
749                 self.replace_token(token::Eq, lo, span.hi)
750             }
751             _ => {
752                 let gt_str = Parser::token_to_string(&token::Gt);
753                 let this_token_str = self.this_token_to_string();
754                 self.fatal(format!("expected `{}`, found `{}`",
755                                    gt_str,
756                                    this_token_str)[])
757             }
758         }
759     }
760
761     pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
762                                                   sep: Option<token::Token>,
763                                                   mut f: F)
764                                                   -> (OwnedSlice<T>, bool) where
765         F: FnMut(&mut Parser) -> Option<T>,
766     {
767         let mut v = Vec::new();
768         // This loop works by alternating back and forth between parsing types
769         // and commas.  For example, given a string `A, B,>`, the parser would
770         // first parse `A`, then a comma, then `B`, then a comma. After that it
771         // would encounter a `>` and stop. This lets the parser handle trailing
772         // commas in generic parameters, because it can stop either after
773         // parsing a type or after parsing a comma.
774         for i in iter::count(0u, 1) {
775             if self.check(&token::Gt)
776                 || self.token == token::BinOp(token::Shr)
777                 || self.token == token::Ge
778                 || self.token == token::BinOpEq(token::Shr) {
779                 break;
780             }
781
782             if i % 2 == 0 {
783                 match f(self) {
784                     Some(result) => v.push(result),
785                     None => return (OwnedSlice::from_vec(v), true)
786                 }
787             } else {
788                 sep.as_ref().map(|t| self.expect(t));
789             }
790         }
791         return (OwnedSlice::from_vec(v), false);
792     }
793
794     /// Parse a sequence bracketed by '<' and '>', stopping
795     /// before the '>'.
796     pub fn parse_seq_to_before_gt<T, F>(&mut self,
797                                         sep: Option<token::Token>,
798                                         mut f: F)
799                                         -> OwnedSlice<T> where
800         F: FnMut(&mut Parser) -> T,
801     {
802         let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, |p| Some(f(p)));
803         assert!(!returned);
804         return result;
805     }
806
807     pub fn parse_seq_to_gt<T, F>(&mut self,
808                                  sep: Option<token::Token>,
809                                  f: F)
810                                  -> OwnedSlice<T> where
811         F: FnMut(&mut Parser) -> T,
812     {
813         let v = self.parse_seq_to_before_gt(sep, f);
814         self.expect_gt();
815         return v;
816     }
817
818     pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
819                                            sep: Option<token::Token>,
820                                            f: F)
821                                            -> (OwnedSlice<T>, bool) where
822         F: FnMut(&mut Parser) -> Option<T>,
823     {
824         let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f);
825         if !returned {
826             self.expect_gt();
827         }
828         return (v, returned);
829     }
830
831     /// Parse a sequence, including the closing delimiter. The function
832     /// f must consume tokens until reaching the next separator or
833     /// closing bracket.
834     pub fn parse_seq_to_end<T, F>(&mut self,
835                                   ket: &token::Token,
836                                   sep: SeqSep,
837                                   f: F)
838                                   -> Vec<T> where
839         F: FnMut(&mut Parser) -> T,
840     {
841         let val = self.parse_seq_to_before_end(ket, sep, f);
842         self.bump();
843         val
844     }
845
846     /// Parse a sequence, not including the closing delimiter. The function
847     /// f must consume tokens until reaching the next separator or
848     /// closing bracket.
849     pub fn parse_seq_to_before_end<T, F>(&mut self,
850                                          ket: &token::Token,
851                                          sep: SeqSep,
852                                          mut f: F)
853                                          -> Vec<T> where
854         F: FnMut(&mut Parser) -> T,
855     {
856         let mut first: bool = true;
857         let mut v = vec!();
858         while self.token != *ket {
859             match sep.sep {
860               Some(ref t) => {
861                 if first { first = false; }
862                 else { self.expect(t); }
863               }
864               _ => ()
865             }
866             if sep.trailing_sep_allowed && self.check(ket) { break; }
867             v.push(f(self));
868         }
869         return v;
870     }
871
872     /// Parse a sequence, including the closing delimiter. The function
873     /// f must consume tokens until reaching the next separator or
874     /// closing bracket.
875     pub fn parse_unspanned_seq<T, F>(&mut self,
876                                      bra: &token::Token,
877                                      ket: &token::Token,
878                                      sep: SeqSep,
879                                      f: F)
880                                      -> Vec<T> where
881         F: FnMut(&mut Parser) -> T,
882     {
883         self.expect(bra);
884         let result = self.parse_seq_to_before_end(ket, sep, f);
885         self.bump();
886         result
887     }
888
889     /// Parse a sequence parameter of enum variant. For consistency purposes,
890     /// these should not be empty.
891     pub fn parse_enum_variant_seq<T, F>(&mut self,
892                                         bra: &token::Token,
893                                         ket: &token::Token,
894                                         sep: SeqSep,
895                                         f: F)
896                                         -> Vec<T> where
897         F: FnMut(&mut Parser) -> T,
898     {
899         let result = self.parse_unspanned_seq(bra, ket, sep, f);
900         if result.is_empty() {
901             let last_span = self.last_span;
902             self.span_err(last_span,
903             "nullary enum variants are written with no trailing `( )`");
904         }
905         result
906     }
907
908     // NB: Do not use this function unless you actually plan to place the
909     // spanned list in the AST.
910     pub fn parse_seq<T, F>(&mut self,
911                            bra: &token::Token,
912                            ket: &token::Token,
913                            sep: SeqSep,
914                            f: F)
915                            -> Spanned<Vec<T>> where
916         F: FnMut(&mut Parser) -> T,
917     {
918         let lo = self.span.lo;
919         self.expect(bra);
920         let result = self.parse_seq_to_before_end(ket, sep, f);
921         let hi = self.span.hi;
922         self.bump();
923         spanned(lo, hi, result)
924     }
925
926     /// Advance the parser by one token
927     pub fn bump(&mut self) {
928         self.last_span = self.span;
929         // Stash token for error recovery (sometimes; clone is not necessarily cheap).
930         self.last_token = if self.token.is_ident() || self.token.is_path() {
931             Some(box self.token.clone())
932         } else {
933             None
934         };
935         let next = if self.buffer_start == self.buffer_end {
936             self.reader.real_token()
937         } else {
938             // Avoid token copies with `replace`.
939             let buffer_start = self.buffer_start as uint;
940             let next_index = (buffer_start + 1) & 3 as uint;
941             self.buffer_start = next_index as int;
942
943             let placeholder = TokenAndSpan {
944                 tok: token::Underscore,
945                 sp: self.span,
946             };
947             mem::replace(&mut self.buffer[buffer_start], placeholder)
948         };
949         self.span = next.sp;
950         self.token = next.tok;
951         self.tokens_consumed += 1u;
952         self.expected_tokens.clear();
953     }
954
955     /// Advance the parser by one token and return the bumped token.
956     pub fn bump_and_get(&mut self) -> token::Token {
957         let old_token = mem::replace(&mut self.token, token::Underscore);
958         self.bump();
959         old_token
960     }
961
962     /// EFFECT: replace the current token and span with the given one
963     pub fn replace_token(&mut self,
964                          next: token::Token,
965                          lo: BytePos,
966                          hi: BytePos) {
967         self.last_span = mk_sp(self.span.lo, lo);
968         self.token = next;
969         self.span = mk_sp(lo, hi);
970     }
971     pub fn buffer_length(&mut self) -> int {
972         if self.buffer_start <= self.buffer_end {
973             return self.buffer_end - self.buffer_start;
974         }
975         return (4 - self.buffer_start) + self.buffer_end;
976     }
977     pub fn look_ahead<R, F>(&mut self, distance: uint, f: F) -> R where
978         F: FnOnce(&token::Token) -> R,
979     {
980         let dist = distance as int;
981         while self.buffer_length() < dist {
982             self.buffer[self.buffer_end as uint] = self.reader.real_token();
983             self.buffer_end = (self.buffer_end + 1) & 3;
984         }
985         f(&self.buffer[((self.buffer_start + dist - 1) & 3) as uint].tok)
986     }
987     pub fn fatal(&mut self, m: &str) -> ! {
988         self.sess.span_diagnostic.span_fatal(self.span, m)
989     }
990     pub fn span_fatal(&mut self, sp: Span, m: &str) -> ! {
991         self.sess.span_diagnostic.span_fatal(sp, m)
992     }
993     pub fn span_fatal_help(&mut self, sp: Span, m: &str, help: &str) -> ! {
994         self.span_err(sp, m);
995         self.span_help(sp, help);
996         panic!(diagnostic::FatalError);
997     }
998     pub fn span_note(&mut self, sp: Span, m: &str) {
999         self.sess.span_diagnostic.span_note(sp, m)
1000     }
1001     pub fn span_help(&mut self, sp: Span, m: &str) {
1002         self.sess.span_diagnostic.span_help(sp, m)
1003     }
1004     pub fn bug(&mut self, m: &str) -> ! {
1005         self.sess.span_diagnostic.span_bug(self.span, m)
1006     }
1007     pub fn warn(&mut self, m: &str) {
1008         self.sess.span_diagnostic.span_warn(self.span, m)
1009     }
1010     pub fn span_warn(&mut self, sp: Span, m: &str) {
1011         self.sess.span_diagnostic.span_warn(sp, m)
1012     }
1013     pub fn span_err(&mut self, sp: Span, m: &str) {
1014         self.sess.span_diagnostic.span_err(sp, m)
1015     }
1016     pub fn span_bug(&mut self, sp: Span, m: &str) -> ! {
1017         self.sess.span_diagnostic.span_bug(sp, m)
1018     }
1019     pub fn abort_if_errors(&mut self) {
1020         self.sess.span_diagnostic.handler().abort_if_errors();
1021     }
1022
1023     pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString {
1024         token::get_ident(id)
1025     }
1026
1027     /// Is the current token one of the keywords that signals a bare function
1028     /// type?
1029     pub fn token_is_bare_fn_keyword(&mut self) -> bool {
1030         self.token.is_keyword(keywords::Fn) ||
1031             self.token.is_keyword(keywords::Unsafe) ||
1032             self.token.is_keyword(keywords::Extern)
1033     }
1034
1035     /// Is the current token one of the keywords that signals a closure type?
1036     pub fn token_is_closure_keyword(&mut self) -> bool {
1037         self.token.is_keyword(keywords::Unsafe)
1038     }
1039
1040     pub fn get_lifetime(&mut self) -> ast::Ident {
1041         match self.token {
1042             token::Lifetime(ref ident) => *ident,
1043             _ => self.bug("not a lifetime"),
1044         }
1045     }
1046
1047     pub fn parse_for_in_type(&mut self) -> Ty_ {
1048         /*
1049         Parses whatever can come after a `for` keyword in a type.
1050         The `for` has already been consumed.
1051
1052         Deprecated:
1053
1054         - for <'lt> |S| -> T
1055
1056         Eventually:
1057
1058         - for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
1059         - for <'lt> path::foo(a, b)
1060
1061         */
1062
1063         // parse <'lt>
1064         let lifetime_defs = self.parse_late_bound_lifetime_defs();
1065
1066         // examine next token to decide to do
1067         if self.eat_keyword(keywords::Proc) {
1068             self.parse_proc_type(lifetime_defs)
1069         } else if self.token_is_bare_fn_keyword() || self.token_is_closure_keyword() {
1070             self.parse_ty_bare_fn_or_ty_closure(lifetime_defs)
1071         } else if self.check(&token::ModSep) ||
1072                   self.token.is_ident() ||
1073                   self.token.is_path()
1074         {
1075             let trait_ref = self.parse_trait_ref();
1076             let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
1077                                                      trait_ref: trait_ref };
1078             let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
1079                 self.parse_ty_param_bounds(BoundParsingMode::Bare)
1080             } else {
1081                 OwnedSlice::empty()
1082             };
1083             let all_bounds =
1084                 Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
1085                 .chain(other_bounds.into_vec().into_iter())
1086                 .collect();
1087             ast::TyPolyTraitRef(all_bounds)
1088         } else {
1089             self.parse_ty_closure(lifetime_defs)
1090         }
1091     }
1092
1093     pub fn parse_ty_path(&mut self) -> Ty_ {
1094         let path = self.parse_path(LifetimeAndTypesWithoutColons);
1095         TyPath(path, ast::DUMMY_NODE_ID)
1096     }
1097
1098     /// parse a TyBareFn type:
1099     pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1100         /*
1101
1102         [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1103          ^~~~^           ^~~~^     ^~~~^ ^~^    ^
1104            |               |         |    |     |
1105            |               |         |    |   Return type
1106            |               |         |  Argument types
1107            |               |     Lifetimes
1108            |              ABI
1109         Function Style
1110         */
1111
1112         let unsafety = self.parse_unsafety();
1113         let abi = if self.eat_keyword(keywords::Extern) {
1114             self.parse_opt_abi().unwrap_or(abi::C)
1115         } else {
1116             abi::Rust
1117         };
1118
1119         self.expect_keyword(keywords::Fn);
1120         let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1121         let (inputs, variadic) = self.parse_fn_args(false, true);
1122         let ret_ty = self.parse_ret_ty();
1123         let decl = P(FnDecl {
1124             inputs: inputs,
1125             output: ret_ty,
1126             variadic: variadic
1127         });
1128         TyBareFn(P(BareFnTy {
1129             abi: abi,
1130             unsafety: unsafety,
1131             lifetimes: lifetime_defs,
1132             decl: decl
1133         }))
1134     }
1135
1136     /// Parses a procedure type (`proc`). The initial `proc` keyword must
1137     /// already have been parsed.
1138     pub fn parse_proc_type(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1139         /*
1140
1141         proc <'lt> (S) [:Bounds] -> T
1142         ^~~^ ^~~~^  ^  ^~~~~~~~^    ^
1143          |     |    |      |        |
1144          |     |    |      |      Return type
1145          |     |    |    Bounds
1146          |     |  Argument types
1147          |   Legacy lifetimes
1148         the `proc` keyword (already consumed)
1149
1150         */
1151
1152         let proc_span = self.last_span;
1153
1154         // To be helpful, parse the proc as ever
1155         let _ = self.parse_legacy_lifetime_defs(lifetime_defs);
1156         let _ = self.parse_fn_args(false, false);
1157         let _ = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1158         let _ = self.parse_ret_ty();
1159
1160         self.obsolete(proc_span, ObsoleteProcType);
1161
1162         TyInfer
1163     }
1164
1165     /// Parses an optional unboxed closure kind (`&:`, `&mut:`, or `:`).
1166     pub fn parse_optional_unboxed_closure_kind(&mut self)
1167                                                -> Option<UnboxedClosureKind> {
1168         if self.check(&token::BinOp(token::And)) &&
1169                 self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
1170                 self.look_ahead(2, |t| *t == token::Colon) {
1171             self.bump();
1172             self.bump();
1173             self.bump();
1174             return Some(FnMutUnboxedClosureKind)
1175         }
1176
1177         if self.token == token::BinOp(token::And) &&
1178                     self.look_ahead(1, |t| *t == token::Colon) {
1179             self.bump();
1180             self.bump();
1181             return Some(FnUnboxedClosureKind)
1182         }
1183
1184         if self.eat(&token::Colon) {
1185             return Some(FnOnceUnboxedClosureKind)
1186         }
1187
1188         return None
1189     }
1190
1191     pub fn parse_ty_bare_fn_or_ty_closure(&mut self, lifetime_defs: Vec<LifetimeDef>) -> Ty_ {
1192         // Both bare fns and closures can begin with stuff like unsafe
1193         // and extern. So we just scan ahead a few tokens to see if we see
1194         // a `fn`.
1195         //
1196         // Closure:  [unsafe] <'lt> |S| [:Bounds] -> T
1197         // Fn:       [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1198
1199         if self.token.is_keyword(keywords::Fn) {
1200             self.parse_ty_bare_fn(lifetime_defs)
1201         } else if self.token.is_keyword(keywords::Extern) {
1202             self.parse_ty_bare_fn(lifetime_defs)
1203         } else if self.token.is_keyword(keywords::Unsafe) {
1204             if self.look_ahead(1, |t| t.is_keyword(keywords::Fn) ||
1205                                       t.is_keyword(keywords::Extern)) {
1206                 self.parse_ty_bare_fn(lifetime_defs)
1207             } else {
1208                 self.parse_ty_closure(lifetime_defs)
1209             }
1210         } else {
1211             self.parse_ty_closure(lifetime_defs)
1212         }
1213     }
1214
1215     /// Parse a TyClosure type
1216     pub fn parse_ty_closure(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1217         /*
1218
1219         [unsafe] <'lt> |S| [:Bounds] -> T
1220         ^~~~~~~^ ^~~~^  ^  ^~~~~~~~^    ^
1221           |        |       |      |        |
1222           |        |       |      |      Return type
1223           |        |       |  Closure bounds
1224           |        |     Argument types
1225           |      Deprecated lifetime defs
1226           |
1227         Function Style
1228
1229         */
1230
1231         let unsafety = self.parse_unsafety();
1232
1233         let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1234
1235         let inputs = if self.eat(&token::OrOr) {
1236             Vec::new()
1237         } else {
1238             self.expect_or();
1239
1240             let inputs = self.parse_seq_to_before_or(
1241                 &token::Comma,
1242                 |p| p.parse_arg_general(false));
1243             self.expect_or();
1244             inputs
1245         };
1246
1247         let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1248
1249         let output = self.parse_ret_ty();
1250         let decl = P(FnDecl {
1251             inputs: inputs,
1252             output: output,
1253             variadic: false
1254         });
1255
1256         TyClosure(P(ClosureTy {
1257             unsafety: unsafety,
1258             onceness: Many,
1259             bounds: bounds,
1260             decl: decl,
1261             lifetimes: lifetime_defs,
1262         }))
1263     }
1264
1265     pub fn parse_unsafety(&mut self) -> Unsafety {
1266         if self.eat_keyword(keywords::Unsafe) {
1267             return Unsafety::Unsafe;
1268         } else {
1269             return Unsafety::Normal;
1270         }
1271     }
1272
1273     /// Parses `[ 'for' '<' lifetime_defs '>' ]'
1274     fn parse_legacy_lifetime_defs(&mut self,
1275                                   lifetime_defs: Vec<ast::LifetimeDef>)
1276                                   -> Vec<ast::LifetimeDef>
1277     {
1278         if self.token == token::Lt {
1279             self.bump();
1280             if lifetime_defs.is_empty() {
1281                 self.warn("deprecated syntax; use the `for` keyword now \
1282                             (e.g. change `fn<'a>` to `for<'a> fn`)");
1283                 let lifetime_defs = self.parse_lifetime_defs();
1284                 self.expect_gt();
1285                 lifetime_defs
1286             } else {
1287                 self.fatal("cannot use new `for` keyword and older syntax together");
1288             }
1289         } else {
1290             lifetime_defs
1291         }
1292     }
1293
1294     /// Parses `type Foo;` in a trait declaration only. The `type` keyword has
1295     /// already been parsed.
1296     fn parse_associated_type(&mut self, attrs: Vec<Attribute>)
1297                              -> AssociatedType
1298     {
1299         let ty_param = self.parse_ty_param();
1300         self.expect(&token::Semi);
1301         AssociatedType {
1302             attrs: attrs,
1303             ty_param: ty_param,
1304         }
1305     }
1306
1307     /// Parses `type Foo = TYPE;` in an implementation declaration only. The
1308     /// `type` keyword has already been parsed.
1309     fn parse_typedef(&mut self, attrs: Vec<Attribute>, vis: Visibility)
1310                      -> Typedef {
1311         let lo = self.span.lo;
1312         let ident = self.parse_ident();
1313         self.expect(&token::Eq);
1314         let typ = self.parse_ty_sum();
1315         let hi = self.span.hi;
1316         self.expect(&token::Semi);
1317         Typedef {
1318             id: ast::DUMMY_NODE_ID,
1319             span: mk_sp(lo, hi),
1320             ident: ident,
1321             vis: vis,
1322             attrs: attrs,
1323             typ: typ,
1324         }
1325     }
1326
1327     /// Parse the items in a trait declaration
1328     pub fn parse_trait_items(&mut self) -> Vec<TraitItem> {
1329         self.parse_unspanned_seq(
1330             &token::OpenDelim(token::Brace),
1331             &token::CloseDelim(token::Brace),
1332             seq_sep_none(),
1333             |p| {
1334             let attrs = p.parse_outer_attributes();
1335
1336             if p.eat_keyword(keywords::Type) {
1337                 TypeTraitItem(P(p.parse_associated_type(attrs)))
1338             } else {
1339                 let lo = p.span.lo;
1340
1341                 let vis = p.parse_visibility();
1342                 let style = p.parse_unsafety();
1343                 let abi = if p.eat_keyword(keywords::Extern) {
1344                     p.parse_opt_abi().unwrap_or(abi::C)
1345                 } else {
1346                     abi::Rust
1347                 };
1348                 p.expect_keyword(keywords::Fn);
1349
1350                 let ident = p.parse_ident();
1351                 let mut generics = p.parse_generics();
1352
1353                 let (explicit_self, d) = p.parse_fn_decl_with_self(|p| {
1354                     // This is somewhat dubious; We don't want to allow
1355                     // argument names to be left off if there is a
1356                     // definition...
1357                     p.parse_arg_general(false)
1358                 });
1359
1360                 p.parse_where_clause(&mut generics);
1361
1362                 let hi = p.last_span.hi;
1363                 match p.token {
1364                   token::Semi => {
1365                     p.bump();
1366                     debug!("parse_trait_methods(): parsing required method");
1367                     RequiredMethod(TypeMethod {
1368                         ident: ident,
1369                         attrs: attrs,
1370                         unsafety: style,
1371                         decl: d,
1372                         generics: generics,
1373                         abi: abi,
1374                         explicit_self: explicit_self,
1375                         id: ast::DUMMY_NODE_ID,
1376                         span: mk_sp(lo, hi),
1377                         vis: vis,
1378                     })
1379                   }
1380                   token::OpenDelim(token::Brace) => {
1381                     debug!("parse_trait_methods(): parsing provided method");
1382                     let (inner_attrs, body) =
1383                         p.parse_inner_attrs_and_block();
1384                     let mut attrs = attrs;
1385                     attrs.push_all(inner_attrs[]);
1386                     ProvidedMethod(P(ast::Method {
1387                         attrs: attrs,
1388                         id: ast::DUMMY_NODE_ID,
1389                         span: mk_sp(lo, hi),
1390                         node: ast::MethDecl(ident,
1391                                             generics,
1392                                             abi,
1393                                             explicit_self,
1394                                             style,
1395                                             d,
1396                                             body,
1397                                             vis)
1398                     }))
1399                   }
1400
1401                   _ => {
1402                       let token_str = p.this_token_to_string();
1403                       p.fatal((format!("expected `;` or `{{`, found `{}`",
1404                                        token_str))[])
1405                   }
1406                 }
1407             }
1408         })
1409     }
1410
1411     /// Parse a possibly mutable type
1412     pub fn parse_mt(&mut self) -> MutTy {
1413         let mutbl = self.parse_mutability();
1414         let t = self.parse_ty();
1415         MutTy { ty: t, mutbl: mutbl }
1416     }
1417
1418     /// Parse [mut/const/imm] ID : TY
1419     /// now used only by obsolete record syntax parser...
1420     pub fn parse_ty_field(&mut self) -> TypeField {
1421         let lo = self.span.lo;
1422         let mutbl = self.parse_mutability();
1423         let id = self.parse_ident();
1424         self.expect(&token::Colon);
1425         let ty = self.parse_ty_sum();
1426         let hi = ty.span.hi;
1427         ast::TypeField {
1428             ident: id,
1429             mt: MutTy { ty: ty, mutbl: mutbl },
1430             span: mk_sp(lo, hi),
1431         }
1432     }
1433
1434     /// Parse optional return type [ -> TY ] in function decl
1435     pub fn parse_ret_ty(&mut self) -> FunctionRetTy {
1436         if self.eat(&token::RArrow) {
1437             if self.eat(&token::Not) {
1438                 NoReturn(self.span)
1439             } else {
1440                 let t = self.parse_ty();
1441
1442                 // We used to allow `fn foo() -> &T + U`, but don't
1443                 // anymore. If we see it, report a useful error.  This
1444                 // only makes sense because `parse_ret_ty` is only
1445                 // used in fn *declarations*, not fn types or where
1446                 // clauses (i.e., not when parsing something like
1447                 // `FnMut() -> T + Send`, where the `+` is legal).
1448                 if self.token == token::BinOp(token::Plus) {
1449                     self.warn("deprecated syntax: `()` are required, see RFC 438 for details");
1450                 }
1451
1452                 Return(t)
1453             }
1454         } else {
1455             let pos = self.span.lo;
1456             Return(P(Ty {
1457                 id: ast::DUMMY_NODE_ID,
1458                 node: TyTup(vec![]),
1459                 span: mk_sp(pos, pos),
1460             }))
1461         }
1462     }
1463
1464     /// Parse a type in a context where `T1+T2` is allowed.
1465     pub fn parse_ty_sum(&mut self) -> P<Ty> {
1466         let lo = self.span.lo;
1467         let lhs = self.parse_ty();
1468
1469         if !self.eat(&token::BinOp(token::Plus)) {
1470             return lhs;
1471         }
1472
1473         let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
1474
1475         // In type grammar, `+` is treated like a binary operator,
1476         // and hence both L and R side are required.
1477         if bounds.len() == 0 {
1478             let last_span = self.last_span;
1479             self.span_err(last_span,
1480                           "at least one type parameter bound \
1481                           must be specified");
1482         }
1483
1484         let sp = mk_sp(lo, self.last_span.hi);
1485         let sum = ast::TyObjectSum(lhs, bounds);
1486         P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})
1487     }
1488
1489     /// Parse a type.
1490     pub fn parse_ty(&mut self) -> P<Ty> {
1491         maybe_whole!(no_clone self, NtTy);
1492
1493         let lo = self.span.lo;
1494
1495         let t = if self.check(&token::OpenDelim(token::Paren)) {
1496             self.bump();
1497
1498             // (t) is a parenthesized ty
1499             // (t,) is the type of a tuple with only one field,
1500             // of type t
1501             let mut ts = vec![];
1502             let mut last_comma = false;
1503             while self.token != token::CloseDelim(token::Paren) {
1504                 ts.push(self.parse_ty_sum());
1505                 if self.check(&token::Comma) {
1506                     last_comma = true;
1507                     self.bump();
1508                 } else {
1509                     last_comma = false;
1510                     break;
1511                 }
1512             }
1513
1514             self.expect(&token::CloseDelim(token::Paren));
1515             if ts.len() == 1 && !last_comma {
1516                 TyParen(ts.into_iter().nth(0).unwrap())
1517             } else {
1518                 TyTup(ts)
1519             }
1520         } else if self.token == token::Tilde {
1521             // OWNED POINTER
1522             self.bump();
1523             let last_span = self.last_span;
1524             match self.token {
1525                 token::OpenDelim(token::Bracket) => self.obsolete(last_span, ObsoleteOwnedVector),
1526                 _ => self.obsolete(last_span, ObsoleteOwnedType)
1527             }
1528             TyTup(vec![self.parse_ty()])
1529         } else if self.check(&token::BinOp(token::Star)) {
1530             // STAR POINTER (bare pointer?)
1531             self.bump();
1532             TyPtr(self.parse_ptr())
1533         } else if self.check(&token::OpenDelim(token::Bracket)) {
1534             // VECTOR
1535             self.expect(&token::OpenDelim(token::Bracket));
1536             let t = self.parse_ty_sum();
1537
1538             // Parse the `; e` in `[ int; e ]`
1539             // where `e` is a const expression
1540             let t = match self.maybe_parse_fixed_length_of_vec() {
1541                 None => TyVec(t),
1542                 Some(suffix) => TyFixedLengthVec(t, suffix)
1543             };
1544             self.expect(&token::CloseDelim(token::Bracket));
1545             t
1546         } else if self.check(&token::BinOp(token::And)) ||
1547                   self.token == token::AndAnd {
1548             // BORROWED POINTER
1549             self.expect_and();
1550             self.parse_borrowed_pointee()
1551         } else if self.token.is_keyword(keywords::For) {
1552             self.parse_for_in_type()
1553         } else if self.token_is_bare_fn_keyword() ||
1554                   self.token_is_closure_keyword() {
1555             // BARE FUNCTION OR CLOSURE
1556             self.parse_ty_bare_fn_or_ty_closure(Vec::new())
1557         } else if self.check(&token::BinOp(token::Or)) ||
1558                   self.token == token::OrOr ||
1559                   (self.token == token::Lt &&
1560                    self.look_ahead(1, |t| {
1561                        *t == token::Gt || t.is_lifetime()
1562                    })) {
1563             // CLOSURE
1564             self.parse_ty_closure(Vec::new())
1565         } else if self.eat_keyword(keywords::Typeof) {
1566             // TYPEOF
1567             // In order to not be ambiguous, the type must be surrounded by parens.
1568             self.expect(&token::OpenDelim(token::Paren));
1569             let e = self.parse_expr();
1570             self.expect(&token::CloseDelim(token::Paren));
1571             TyTypeof(e)
1572         } else if self.eat_keyword(keywords::Proc) {
1573             self.parse_proc_type(Vec::new())
1574         } else if self.eat_lt() {
1575             // QUALIFIED PATH `<TYPE as TRAIT_REF>::item`
1576             let self_type = self.parse_ty_sum();
1577             self.expect_keyword(keywords::As);
1578             let trait_ref = self.parse_trait_ref();
1579             self.expect(&token::Gt);
1580             self.expect(&token::ModSep);
1581             let item_name = self.parse_ident();
1582             TyQPath(P(QPath {
1583                 self_type: self_type,
1584                 trait_ref: P(trait_ref),
1585                 item_name: item_name,
1586             }))
1587         } else if self.check(&token::ModSep) ||
1588                   self.token.is_ident() ||
1589                   self.token.is_path() {
1590             // NAMED TYPE
1591             self.parse_ty_path()
1592         } else if self.eat(&token::Underscore) {
1593             // TYPE TO BE INFERRED
1594             TyInfer
1595         } else {
1596             let this_token_str = self.this_token_to_string();
1597             let msg = format!("expected type, found `{}`", this_token_str);
1598             self.fatal(msg[]);
1599         };
1600
1601         let sp = mk_sp(lo, self.last_span.hi);
1602         P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp})
1603     }
1604
1605     pub fn parse_borrowed_pointee(&mut self) -> Ty_ {
1606         // look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
1607         let opt_lifetime = self.parse_opt_lifetime();
1608
1609         let mt = self.parse_mt();
1610         return TyRptr(opt_lifetime, mt);
1611     }
1612
1613     pub fn parse_ptr(&mut self) -> MutTy {
1614         let mutbl = if self.eat_keyword(keywords::Mut) {
1615             MutMutable
1616         } else if self.eat_keyword(keywords::Const) {
1617             MutImmutable
1618         } else {
1619             let span = self.last_span;
1620             self.span_err(span,
1621                           "bare raw pointers are no longer allowed, you should \
1622                            likely use `*mut T`, but otherwise `*T` is now \
1623                            known as `*const T`");
1624             MutImmutable
1625         };
1626         let t = self.parse_ty();
1627         MutTy { ty: t, mutbl: mutbl }
1628     }
1629
1630     pub fn is_named_argument(&mut self) -> bool {
1631         let offset = match self.token {
1632             token::BinOp(token::And) => 1,
1633             token::AndAnd => 1,
1634             _ if self.token.is_keyword(keywords::Mut) => 1,
1635             _ => 0
1636         };
1637
1638         debug!("parser is_named_argument offset:{}", offset);
1639
1640         if offset == 0 {
1641             is_plain_ident_or_underscore(&self.token)
1642                 && self.look_ahead(1, |t| *t == token::Colon)
1643         } else {
1644             self.look_ahead(offset, |t| is_plain_ident_or_underscore(t))
1645                 && self.look_ahead(offset + 1, |t| *t == token::Colon)
1646         }
1647     }
1648
1649     /// This version of parse arg doesn't necessarily require
1650     /// identifier names.
1651     pub fn parse_arg_general(&mut self, require_name: bool) -> Arg {
1652         let pat = if require_name || self.is_named_argument() {
1653             debug!("parse_arg_general parse_pat (require_name:{})",
1654                    require_name);
1655             let pat = self.parse_pat();
1656
1657             self.expect(&token::Colon);
1658             pat
1659         } else {
1660             debug!("parse_arg_general ident_to_pat");
1661             ast_util::ident_to_pat(ast::DUMMY_NODE_ID,
1662                                    self.last_span,
1663                                    special_idents::invalid)
1664         };
1665
1666         let t = self.parse_ty_sum();
1667
1668         Arg {
1669             ty: t,
1670             pat: pat,
1671             id: ast::DUMMY_NODE_ID,
1672         }
1673     }
1674
1675     /// Parse a single function argument
1676     pub fn parse_arg(&mut self) -> Arg {
1677         self.parse_arg_general(true)
1678     }
1679
1680     /// Parse an argument in a lambda header e.g. |arg, arg|
1681     pub fn parse_fn_block_arg(&mut self) -> Arg {
1682         let pat = self.parse_pat();
1683         let t = if self.eat(&token::Colon) {
1684             self.parse_ty_sum()
1685         } else {
1686             P(Ty {
1687                 id: ast::DUMMY_NODE_ID,
1688                 node: TyInfer,
1689                 span: mk_sp(self.span.lo, self.span.hi),
1690             })
1691         };
1692         Arg {
1693             ty: t,
1694             pat: pat,
1695             id: ast::DUMMY_NODE_ID
1696         }
1697     }
1698
1699     pub fn maybe_parse_fixed_length_of_vec(&mut self) -> Option<P<ast::Expr>> {
1700         if self.check(&token::Semi) {
1701             self.bump();
1702             Some(self.parse_expr())
1703         } else {
1704             None
1705         }
1706     }
1707
1708     /// Matches token_lit = LIT_INTEGER | ...
1709     pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ {
1710         match *tok {
1711             token::Interpolated(token::NtExpr(ref v)) => {
1712                 match v.node {
1713                     ExprLit(ref lit) => { lit.node.clone() }
1714                     _ => { self.unexpected_last(tok); }
1715                 }
1716             }
1717             token::Literal(lit, suf) => {
1718                 let (suffix_illegal, out) = match lit {
1719                     token::Byte(i) => (true, LitByte(parse::byte_lit(i.as_str()).0)),
1720                     token::Char(i) => (true, LitChar(parse::char_lit(i.as_str()).0)),
1721
1722                     // there are some valid suffixes for integer and
1723                     // float literals, so all the handling is done
1724                     // internally.
1725                     token::Integer(s) => {
1726                         (false, parse::integer_lit(s.as_str(),
1727                                                    suf.as_ref().map(|s| s.as_str()),
1728                                                    &self.sess.span_diagnostic,
1729                                                    self.last_span))
1730                     }
1731                     token::Float(s) => {
1732                         (false, parse::float_lit(s.as_str(),
1733                                                  suf.as_ref().map(|s| s.as_str()),
1734                                                   &self.sess.span_diagnostic,
1735                                                  self.last_span))
1736                     }
1737
1738                     token::Str_(s) => {
1739                         (true,
1740                          LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str())[]),
1741                                 ast::CookedStr))
1742                     }
1743                     token::StrRaw(s, n) => {
1744                         (true,
1745                          LitStr(
1746                             token::intern_and_get_ident(
1747                                 parse::raw_str_lit(s.as_str())[]),
1748                             ast::RawStr(n)))
1749                     }
1750                     token::Binary(i) =>
1751                         (true, LitBinary(parse::binary_lit(i.as_str()))),
1752                     token::BinaryRaw(i, _) =>
1753                         (true,
1754                          LitBinary(Rc::new(i.as_str().as_bytes().iter().map(|&x| x).collect()))),
1755                 };
1756
1757                 if suffix_illegal {
1758                     let sp = self.last_span;
1759                     self.expect_no_suffix(sp, &*format!("{} literal", lit.short_name()), suf)
1760                 }
1761
1762                 out
1763             }
1764             _ => { self.unexpected_last(tok); }
1765         }
1766     }
1767
1768     /// Matches lit = true | false | token_lit
1769     pub fn parse_lit(&mut self) -> Lit {
1770         let lo = self.span.lo;
1771         let lit = if self.eat_keyword(keywords::True) {
1772             LitBool(true)
1773         } else if self.eat_keyword(keywords::False) {
1774             LitBool(false)
1775         } else {
1776             let token = self.bump_and_get();
1777             let lit = self.lit_from_token(&token);
1778             lit
1779         };
1780         codemap::Spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
1781     }
1782
1783     /// matches '-' lit | lit
1784     pub fn parse_literal_maybe_minus(&mut self) -> P<Expr> {
1785         let minus_lo = self.span.lo;
1786         let minus_present = self.eat(&token::BinOp(token::Minus));
1787
1788         let lo = self.span.lo;
1789         let literal = P(self.parse_lit());
1790         let hi = self.span.hi;
1791         let expr = self.mk_expr(lo, hi, ExprLit(literal));
1792
1793         if minus_present {
1794             let minus_hi = self.span.hi;
1795             let unary = self.mk_unary(UnNeg, expr);
1796             self.mk_expr(minus_lo, minus_hi, unary)
1797         } else {
1798             expr
1799         }
1800     }
1801
1802     /// Parses a path and optional type parameter bounds, depending on the
1803     /// mode. The `mode` parameter determines whether lifetimes, types, and/or
1804     /// bounds are permitted and whether `::` must precede type parameter
1805     /// groups.
1806     pub fn parse_path(&mut self, mode: PathParsingMode) -> ast::Path {
1807         // Check for a whole path...
1808         let found = match self.token {
1809             token::Interpolated(token::NtPath(_)) => Some(self.bump_and_get()),
1810             _ => None,
1811         };
1812         if let Some(token::Interpolated(token::NtPath(box path))) = found {
1813             return path;
1814         }
1815
1816         let lo = self.span.lo;
1817         let is_global = self.eat(&token::ModSep);
1818
1819         // Parse any number of segments and bound sets. A segment is an
1820         // identifier followed by an optional lifetime and a set of types.
1821         // A bound set is a set of type parameter bounds.
1822         let segments = match mode {
1823             LifetimeAndTypesWithoutColons => {
1824                 self.parse_path_segments_without_colons()
1825             }
1826             LifetimeAndTypesWithColons => {
1827                 self.parse_path_segments_with_colons()
1828             }
1829             NoTypesAllowed => {
1830                 self.parse_path_segments_without_types()
1831             }
1832         };
1833
1834         // Assemble the span.
1835         let span = mk_sp(lo, self.last_span.hi);
1836
1837         // Assemble the result.
1838         ast::Path {
1839             span: span,
1840             global: is_global,
1841             segments: segments,
1842         }
1843     }
1844
1845     /// Examples:
1846     /// - `a::b<T,U>::c<V,W>`
1847     /// - `a::b<T,U>::c(V) -> W`
1848     /// - `a::b<T,U>::c(V)`
1849     pub fn parse_path_segments_without_colons(&mut self) -> Vec<ast::PathSegment> {
1850         let mut segments = Vec::new();
1851         loop {
1852             // First, parse an identifier.
1853             let identifier = self.parse_ident();
1854
1855             // Parse types, optionally.
1856             let parameters = if self.eat_lt() {
1857                 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1858
1859                 ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1860                     lifetimes: lifetimes,
1861                     types: OwnedSlice::from_vec(types),
1862                     bindings: OwnedSlice::from_vec(bindings),
1863                 })
1864             } else if self.eat(&token::OpenDelim(token::Paren)) {
1865                 let inputs = self.parse_seq_to_end(
1866                     &token::CloseDelim(token::Paren),
1867                     seq_sep_trailing_allowed(token::Comma),
1868                     |p| p.parse_ty_sum());
1869
1870                 let output_ty = if self.eat(&token::RArrow) {
1871                     Some(self.parse_ty())
1872                 } else {
1873                     None
1874                 };
1875
1876                 ast::ParenthesizedParameters(ast::ParenthesizedParameterData {
1877                     inputs: inputs,
1878                     output: output_ty
1879                 })
1880             } else {
1881                 ast::PathParameters::none()
1882             };
1883
1884             // Assemble and push the result.
1885             segments.push(ast::PathSegment { identifier: identifier,
1886                                              parameters: parameters });
1887
1888             // Continue only if we see a `::`
1889             if !self.eat(&token::ModSep) {
1890                 return segments;
1891             }
1892         }
1893     }
1894
1895     /// Examples:
1896     /// - `a::b::<T,U>::c`
1897     pub fn parse_path_segments_with_colons(&mut self) -> Vec<ast::PathSegment> {
1898         let mut segments = Vec::new();
1899         loop {
1900             // First, parse an identifier.
1901             let identifier = self.parse_ident();
1902
1903             // If we do not see a `::`, stop.
1904             if !self.eat(&token::ModSep) {
1905                 segments.push(ast::PathSegment {
1906                     identifier: identifier,
1907                     parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1908                         lifetimes: Vec::new(),
1909                         types: OwnedSlice::empty(),
1910                         bindings: OwnedSlice::empty(),
1911                     })
1912                 });
1913                 return segments;
1914             }
1915
1916             // Check for a type segment.
1917             if self.eat_lt() {
1918                 // Consumed `a::b::<`, go look for types
1919                 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1920                 segments.push(ast::PathSegment {
1921                     identifier: identifier,
1922                     parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1923                         lifetimes: lifetimes,
1924                         types: OwnedSlice::from_vec(types),
1925                         bindings: OwnedSlice::from_vec(bindings),
1926                     }),
1927                 });
1928
1929                 // Consumed `a::b::<T,U>`, check for `::` before proceeding
1930                 if !self.eat(&token::ModSep) {
1931                     return segments;
1932                 }
1933             } else {
1934                 // Consumed `a::`, go look for `b`
1935                 segments.push(ast::PathSegment {
1936                     identifier: identifier,
1937                     parameters: ast::PathParameters::none(),
1938                 });
1939             }
1940         }
1941     }
1942
1943
1944     /// Examples:
1945     /// - `a::b::c`
1946     pub fn parse_path_segments_without_types(&mut self) -> Vec<ast::PathSegment> {
1947         let mut segments = Vec::new();
1948         loop {
1949             // First, parse an identifier.
1950             let identifier = self.parse_ident();
1951
1952             // Assemble and push the result.
1953             segments.push(ast::PathSegment {
1954                 identifier: identifier,
1955                 parameters: ast::PathParameters::none()
1956             });
1957
1958             // If we do not see a `::`, stop.
1959             if !self.eat(&token::ModSep) {
1960                 return segments;
1961             }
1962         }
1963     }
1964
1965     /// parses 0 or 1 lifetime
1966     pub fn parse_opt_lifetime(&mut self) -> Option<ast::Lifetime> {
1967         match self.token {
1968             token::Lifetime(..) => {
1969                 Some(self.parse_lifetime())
1970             }
1971             _ => {
1972                 None
1973             }
1974         }
1975     }
1976
1977     /// Parses a single lifetime
1978     /// Matches lifetime = LIFETIME
1979     pub fn parse_lifetime(&mut self) -> ast::Lifetime {
1980         match self.token {
1981             token::Lifetime(i) => {
1982                 let span = self.span;
1983                 self.bump();
1984                 return ast::Lifetime {
1985                     id: ast::DUMMY_NODE_ID,
1986                     span: span,
1987                     name: i.name
1988                 };
1989             }
1990             _ => {
1991                 self.fatal(format!("expected a lifetime name")[]);
1992             }
1993         }
1994     }
1995
1996     /// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def  =
1997     /// lifetime [':' lifetimes]`
1998     pub fn parse_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
1999
2000         let mut res = Vec::new();
2001         loop {
2002             match self.token {
2003                 token::Lifetime(_) => {
2004                     let lifetime = self.parse_lifetime();
2005                     let bounds =
2006                         if self.eat(&token::Colon) {
2007                             self.parse_lifetimes(token::BinOp(token::Plus))
2008                         } else {
2009                             Vec::new()
2010                         };
2011                     res.push(ast::LifetimeDef { lifetime: lifetime,
2012                                                 bounds: bounds });
2013                 }
2014
2015                 _ => {
2016                     return res;
2017                 }
2018             }
2019
2020             match self.token {
2021                 token::Comma => { self.bump(); }
2022                 token::Gt => { return res; }
2023                 token::BinOp(token::Shr) => { return res; }
2024                 _ => {
2025                     let this_token_str = self.this_token_to_string();
2026                     let msg = format!("expected `,` or `>` after lifetime \
2027                                       name, found `{}`",
2028                                       this_token_str);
2029                     self.fatal(msg[]);
2030                 }
2031             }
2032         }
2033     }
2034
2035     /// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
2036     /// one too, but putting that in there messes up the grammar....
2037     ///
2038     /// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
2039     /// either a comma or `>`.  Used when parsing type parameter lists, where we expect something
2040     /// like `<'a, 'b, T>`.
2041     pub fn parse_lifetimes(&mut self, sep: token::Token) -> Vec<ast::Lifetime> {
2042
2043         let mut res = Vec::new();
2044         loop {
2045             match self.token {
2046                 token::Lifetime(_) => {
2047                     res.push(self.parse_lifetime());
2048                 }
2049                 _ => {
2050                     return res;
2051                 }
2052             }
2053
2054             if self.token != sep {
2055                 return res;
2056             }
2057
2058             self.bump();
2059         }
2060     }
2061
2062     /// Parse mutability declaration (mut/const/imm)
2063     pub fn parse_mutability(&mut self) -> Mutability {
2064         if self.eat_keyword(keywords::Mut) {
2065             MutMutable
2066         } else {
2067             MutImmutable
2068         }
2069     }
2070
2071     /// Parse ident COLON expr
2072     pub fn parse_field(&mut self) -> Field {
2073         let lo = self.span.lo;
2074         let i = self.parse_ident();
2075         let hi = self.last_span.hi;
2076         self.expect(&token::Colon);
2077         let e = self.parse_expr();
2078         ast::Field {
2079             ident: spanned(lo, hi, i),
2080             span: mk_sp(lo, e.span.hi),
2081             expr: e,
2082         }
2083     }
2084
2085     pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr> {
2086         P(Expr {
2087             id: ast::DUMMY_NODE_ID,
2088             node: node,
2089             span: mk_sp(lo, hi),
2090         })
2091     }
2092
2093     pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::Expr_ {
2094         ExprUnary(unop, expr)
2095     }
2096
2097     pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2098         ExprBinary(binop, lhs, rhs)
2099     }
2100
2101     pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::Expr_ {
2102         ExprCall(f, args)
2103     }
2104
2105     fn mk_method_call(&mut self,
2106                       ident: ast::SpannedIdent,
2107                       tps: Vec<P<Ty>>,
2108                       args: Vec<P<Expr>>)
2109                       -> ast::Expr_ {
2110         ExprMethodCall(ident, tps, args)
2111     }
2112
2113     pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::Expr_ {
2114         ExprIndex(expr, idx)
2115     }
2116
2117     pub fn mk_slice(&mut self,
2118                     expr: P<Expr>,
2119                     start: Option<P<Expr>>,
2120                     end: Option<P<Expr>>,
2121                     _mutbl: Mutability)
2122                     -> ast::Expr_ {
2123         // FIXME: we could give more accurate span info here.
2124         let (lo, hi) = match (&start, &end) {
2125             (&Some(ref s), &Some(ref e)) => (s.span.lo, e.span.hi),
2126             (&Some(ref s), &None) => (s.span.lo, s.span.hi),
2127             (&None, &Some(ref e)) => (e.span.lo, e.span.hi),
2128             (&None, &None) => (DUMMY_SP.lo, DUMMY_SP.hi),
2129         };
2130         ExprIndex(expr, self.mk_expr(lo, hi, ExprRange(start, end)))
2131     }
2132
2133     pub fn mk_range(&mut self,
2134                     start: Option<P<Expr>>,
2135                     end: Option<P<Expr>>)
2136                     -> ast::Expr_ {
2137         ExprRange(start, end)
2138     }
2139
2140     pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::Expr_ {
2141         ExprField(expr, ident)
2142     }
2143
2144     pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<uint>) -> ast::Expr_ {
2145         ExprTupField(expr, idx)
2146     }
2147
2148     pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2149                         lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2150         ExprAssignOp(binop, lhs, rhs)
2151     }
2152
2153     pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr> {
2154         P(Expr {
2155             id: ast::DUMMY_NODE_ID,
2156             node: ExprMac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
2157             span: mk_sp(lo, hi),
2158         })
2159     }
2160
2161     pub fn mk_lit_u32(&mut self, i: u32) -> P<Expr> {
2162         let span = &self.span;
2163         let lv_lit = P(codemap::Spanned {
2164             node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)),
2165             span: *span
2166         });
2167
2168         P(Expr {
2169             id: ast::DUMMY_NODE_ID,
2170             node: ExprLit(lv_lit),
2171             span: *span,
2172         })
2173     }
2174
2175     fn expect_open_delim(&mut self) -> token::DelimToken {
2176         match self.token {
2177             token::OpenDelim(delim) => {
2178                 self.bump();
2179                 delim
2180             },
2181             _ => self.fatal("expected open delimiter"),
2182         }
2183     }
2184
2185     /// At the bottom (top?) of the precedence hierarchy,
2186     /// parse things like parenthesized exprs,
2187     /// macros, return, etc.
2188     pub fn parse_bottom_expr(&mut self) -> P<Expr> {
2189         maybe_whole_expr!(self);
2190
2191         let lo = self.span.lo;
2192         let mut hi = self.span.hi;
2193
2194         let ex: Expr_;
2195
2196         match self.token {
2197             token::OpenDelim(token::Paren) => {
2198                 self.bump();
2199
2200                 // (e) is parenthesized e
2201                 // (e,) is a tuple with only one field, e
2202                 let mut es = vec![];
2203                 let mut trailing_comma = false;
2204                 while self.token != token::CloseDelim(token::Paren) {
2205                     es.push(self.parse_expr());
2206                     self.commit_expr(&**es.last().unwrap(), &[],
2207                                      &[token::Comma, token::CloseDelim(token::Paren)]);
2208                     if self.check(&token::Comma) {
2209                         trailing_comma = true;
2210
2211                         self.bump();
2212                     } else {
2213                         trailing_comma = false;
2214                         break;
2215                     }
2216                 }
2217                 self.bump();
2218
2219                 hi = self.span.hi;
2220                 return if es.len() == 1 && !trailing_comma {
2221                     self.mk_expr(lo, hi, ExprParen(es.into_iter().nth(0).unwrap()))
2222                 } else {
2223                     self.mk_expr(lo, hi, ExprTup(es))
2224                 }
2225             },
2226             token::OpenDelim(token::Brace) => {
2227                 self.bump();
2228                 let blk = self.parse_block_tail(lo, DefaultBlock);
2229                 return self.mk_expr(blk.span.lo, blk.span.hi,
2230                                     ExprBlock(blk));
2231             },
2232             token::BinOp(token::Or) |  token::OrOr => {
2233                 return self.parse_lambda_expr(CaptureByRef);
2234             },
2235             // FIXME #13626: Should be able to stick in
2236             // token::SELF_KEYWORD_NAME
2237             token::Ident(id @ ast::Ident {
2238                             name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
2239                             ctxt: _
2240                          }, token::Plain) => {
2241                 self.bump();
2242                 let path = ast_util::ident_to_path(mk_sp(lo, hi), id);
2243                 ex = ExprPath(path);
2244                 hi = self.last_span.hi;
2245             }
2246             token::OpenDelim(token::Bracket) => {
2247                 self.bump();
2248
2249                 if self.check(&token::CloseDelim(token::Bracket)) {
2250                     // Empty vector.
2251                     self.bump();
2252                     ex = ExprVec(Vec::new());
2253                 } else {
2254                     // Nonempty vector.
2255                     let first_expr = self.parse_expr();
2256                     if self.check(&token::Semi) {
2257                         // Repeating vector syntax: [ 0; 512 ]
2258                         self.bump();
2259                         let count = self.parse_expr();
2260                         self.expect(&token::CloseDelim(token::Bracket));
2261                         ex = ExprRepeat(first_expr, count);
2262                     } else if self.check(&token::Comma) {
2263                         // Vector with two or more elements.
2264                         self.bump();
2265                         let remaining_exprs = self.parse_seq_to_end(
2266                             &token::CloseDelim(token::Bracket),
2267                             seq_sep_trailing_allowed(token::Comma),
2268                             |p| p.parse_expr()
2269                                 );
2270                         let mut exprs = vec!(first_expr);
2271                         exprs.extend(remaining_exprs.into_iter());
2272                         ex = ExprVec(exprs);
2273                     } else {
2274                         // Vector with one element.
2275                         self.expect(&token::CloseDelim(token::Bracket));
2276                         ex = ExprVec(vec!(first_expr));
2277                     }
2278                 }
2279                 hi = self.last_span.hi;
2280             }
2281             _ => {
2282                 if self.eat_keyword(keywords::Move) {
2283                     return self.parse_lambda_expr(CaptureByValue);
2284                 }
2285                 if self.eat_keyword(keywords::Proc) {
2286                     let span = self.last_span;
2287                     let _ = self.parse_proc_decl();
2288                     let _ = self.parse_expr();
2289                     return self.obsolete_expr(span, ObsoleteProcExpr);
2290                 }
2291                 if self.eat_keyword(keywords::If) {
2292                     return self.parse_if_expr();
2293                 }
2294                 if self.eat_keyword(keywords::For) {
2295                     return self.parse_for_expr(None);
2296                 }
2297                 if self.eat_keyword(keywords::While) {
2298                     return self.parse_while_expr(None);
2299                 }
2300                 if self.token.is_lifetime() {
2301                     let lifetime = self.get_lifetime();
2302                     self.bump();
2303                     self.expect(&token::Colon);
2304                     if self.eat_keyword(keywords::While) {
2305                         return self.parse_while_expr(Some(lifetime))
2306                     }
2307                     if self.eat_keyword(keywords::For) {
2308                         return self.parse_for_expr(Some(lifetime))
2309                     }
2310                     if self.eat_keyword(keywords::Loop) {
2311                         return self.parse_loop_expr(Some(lifetime))
2312                     }
2313                     self.fatal("expected `while`, `for`, or `loop` after a label")
2314                 }
2315                 if self.eat_keyword(keywords::Loop) {
2316                     return self.parse_loop_expr(None);
2317                 }
2318                 if self.eat_keyword(keywords::Continue) {
2319                     let lo = self.span.lo;
2320                     let ex = if self.token.is_lifetime() {
2321                         let lifetime = self.get_lifetime();
2322                         self.bump();
2323                         ExprAgain(Some(lifetime))
2324                     } else {
2325                         ExprAgain(None)
2326                     };
2327                     let hi = self.span.hi;
2328                     return self.mk_expr(lo, hi, ex);
2329                 }
2330                 if self.eat_keyword(keywords::Match) {
2331                     return self.parse_match_expr();
2332                 }
2333                 if self.eat_keyword(keywords::Unsafe) {
2334                     return self.parse_block_expr(
2335                         lo,
2336                         UnsafeBlock(ast::UserProvided));
2337                 }
2338                 if self.eat_keyword(keywords::Return) {
2339                     // RETURN expression
2340                     if self.token.can_begin_expr() {
2341                         let e = self.parse_expr();
2342                         hi = e.span.hi;
2343                         ex = ExprRet(Some(e));
2344                     } else {
2345                         ex = ExprRet(None);
2346                     }
2347                 } else if self.eat_keyword(keywords::Break) {
2348                     // BREAK expression
2349                     if self.token.is_lifetime() {
2350                         let lifetime = self.get_lifetime();
2351                         self.bump();
2352                         ex = ExprBreak(Some(lifetime));
2353                     } else {
2354                         ex = ExprBreak(None);
2355                     }
2356                     hi = self.span.hi;
2357                 } else if self.check(&token::ModSep) ||
2358                         self.token.is_ident() &&
2359                         !self.token.is_keyword(keywords::True) &&
2360                         !self.token.is_keyword(keywords::False) {
2361                     let pth =
2362                         self.parse_path(LifetimeAndTypesWithColons);
2363
2364                     // `!`, as an operator, is prefix, so we know this isn't that
2365                     if self.check(&token::Not) {
2366                         // MACRO INVOCATION expression
2367                         self.bump();
2368
2369                         let delim = self.expect_open_delim();
2370                         let tts = self.parse_seq_to_end(
2371                             &token::CloseDelim(delim),
2372                             seq_sep_none(),
2373                             |p| p.parse_token_tree());
2374                         let hi = self.span.hi;
2375
2376                         return self.mk_mac_expr(lo,
2377                                                 hi,
2378                                                 MacInvocTT(pth,
2379                                                            tts,
2380                                                            EMPTY_CTXT));
2381                     }
2382                     if self.check(&token::OpenDelim(token::Brace)) {
2383                         // This is a struct literal, unless we're prohibited
2384                         // from parsing struct literals here.
2385                         if !self.restrictions.contains(RESTRICTION_NO_STRUCT_LITERAL) {
2386                             // It's a struct literal.
2387                             self.bump();
2388                             let mut fields = Vec::new();
2389                             let mut base = None;
2390
2391                             while self.token != token::CloseDelim(token::Brace) {
2392                                 if self.eat(&token::DotDot) {
2393                                     base = Some(self.parse_expr());
2394                                     break;
2395                                 }
2396
2397                                 fields.push(self.parse_field());
2398                                 self.commit_expr(&*fields.last().unwrap().expr,
2399                                                  &[token::Comma],
2400                                                  &[token::CloseDelim(token::Brace)]);
2401                             }
2402
2403                             if fields.len() == 0 && base.is_none() {
2404                                 let last_span = self.last_span;
2405                                 self.span_err(last_span,
2406                                               "structure literal must either \
2407                                               have at least one field or use \
2408                                               functional structure update \
2409                                               syntax");
2410                             }
2411
2412                             hi = self.span.hi;
2413                             self.expect(&token::CloseDelim(token::Brace));
2414                             ex = ExprStruct(pth, fields, base);
2415                             return self.mk_expr(lo, hi, ex);
2416                         }
2417                     }
2418
2419                     hi = pth.span.hi;
2420                     ex = ExprPath(pth);
2421                 } else {
2422                     // other literal expression
2423                     let lit = self.parse_lit();
2424                     hi = lit.span.hi;
2425                     ex = ExprLit(P(lit));
2426                 }
2427             }
2428         }
2429
2430         return self.mk_expr(lo, hi, ex);
2431     }
2432
2433     /// Parse a block or unsafe block
2434     pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode)
2435                             -> P<Expr> {
2436         self.expect(&token::OpenDelim(token::Brace));
2437         let blk = self.parse_block_tail(lo, blk_mode);
2438         return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
2439     }
2440
2441     /// parse a.b or a(13) or a[4] or just a
2442     pub fn parse_dot_or_call_expr(&mut self) -> P<Expr> {
2443         let b = self.parse_bottom_expr();
2444         self.parse_dot_or_call_expr_with(b)
2445     }
2446
2447     pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
2448         let mut e = e0;
2449         let lo = e.span.lo;
2450         let mut hi;
2451         loop {
2452             // expr.f
2453             if self.eat(&token::Dot) {
2454                 match self.token {
2455                   token::Ident(i, _) => {
2456                     let dot = self.last_span.hi;
2457                     hi = self.span.hi;
2458                     self.bump();
2459                     let (_, tys, bindings) = if self.eat(&token::ModSep) {
2460                         self.expect_lt();
2461                         self.parse_generic_values_after_lt()
2462                     } else {
2463                         (Vec::new(), Vec::new(), Vec::new())
2464                     };
2465
2466                     if bindings.len() > 0 {
2467                         let last_span = self.last_span;
2468                         self.span_err(last_span, "type bindings are only permitted on trait paths");
2469                     }
2470
2471                     // expr.f() method call
2472                     match self.token {
2473                         token::OpenDelim(token::Paren) => {
2474                             let mut es = self.parse_unspanned_seq(
2475                                 &token::OpenDelim(token::Paren),
2476                                 &token::CloseDelim(token::Paren),
2477                                 seq_sep_trailing_allowed(token::Comma),
2478                                 |p| p.parse_expr()
2479                             );
2480                             hi = self.last_span.hi;
2481
2482                             es.insert(0, e);
2483                             let id = spanned(dot, hi, i);
2484                             let nd = self.mk_method_call(id, tys, es);
2485                             e = self.mk_expr(lo, hi, nd);
2486                         }
2487                         _ => {
2488                             if !tys.is_empty() {
2489                                 let last_span = self.last_span;
2490                                 self.span_err(last_span,
2491                                               "field expressions may not \
2492                                                have type parameters");
2493                             }
2494
2495                             let id = spanned(dot, hi, i);
2496                             let field = self.mk_field(e, id);
2497                             e = self.mk_expr(lo, hi, field);
2498                         }
2499                     }
2500                   }
2501                   token::Literal(token::Integer(n), suf) => {
2502                     let sp = self.span;
2503
2504                     // A tuple index may not have a suffix
2505                     self.expect_no_suffix(sp, "tuple index", suf);
2506
2507                     let dot = self.last_span.hi;
2508                     hi = self.span.hi;
2509                     self.bump();
2510
2511                     let index = n.as_str().parse::<uint>();
2512                     match index {
2513                         Some(n) => {
2514                             let id = spanned(dot, hi, n);
2515                             let field = self.mk_tup_field(e, id);
2516                             e = self.mk_expr(lo, hi, field);
2517                         }
2518                         None => {
2519                             let last_span = self.last_span;
2520                             self.span_err(last_span, "invalid tuple or tuple struct index");
2521                         }
2522                     }
2523                   }
2524                   token::Literal(token::Float(n), _suf) => {
2525                     self.bump();
2526                     let last_span = self.last_span;
2527                     let fstr = n.as_str();
2528                     self.span_err(last_span,
2529                                   format!("unexpected token: `{}`", n.as_str())[]);
2530                     if fstr.chars().all(|x| "0123456789.".contains_char(x)) {
2531                         let float = match fstr.parse::<f64>() {
2532                             Some(f) => f,
2533                             None => continue,
2534                         };
2535                         self.span_help(last_span,
2536                             format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
2537                                     float.trunc() as uint,
2538                                     float.fract().to_string()[1..])[]);
2539                     }
2540                     self.abort_if_errors();
2541
2542                   }
2543                   _ => self.unexpected()
2544                 }
2545                 continue;
2546             }
2547             if self.expr_is_complete(&*e) { break; }
2548             match self.token {
2549               // expr(...)
2550               token::OpenDelim(token::Paren) => {
2551                 let es = self.parse_unspanned_seq(
2552                     &token::OpenDelim(token::Paren),
2553                     &token::CloseDelim(token::Paren),
2554                     seq_sep_trailing_allowed(token::Comma),
2555                     |p| p.parse_expr()
2556                 );
2557                 hi = self.last_span.hi;
2558
2559                 let nd = self.mk_call(e, es);
2560                 e = self.mk_expr(lo, hi, nd);
2561               }
2562
2563               // expr[...]
2564               // Could be either an index expression or a slicing expression.
2565               // Any slicing non-terminal can have a mutable version with `mut`
2566               // after the opening square bracket.
2567               token::OpenDelim(token::Bracket) => {
2568                 self.bump();
2569                 let mutbl = if self.eat_keyword(keywords::Mut) {
2570                     MutMutable
2571                 } else {
2572                     MutImmutable
2573                 };
2574                 match self.token {
2575                     // e[]
2576                     token::CloseDelim(token::Bracket) => {
2577                         self.bump();
2578                         hi = self.span.hi;
2579                         let slice = self.mk_slice(e, None, None, mutbl);
2580                         e = self.mk_expr(lo, hi, slice)
2581                     }
2582                     // e[..e]
2583                     token::DotDot => {
2584                         self.bump();
2585                         match self.token {
2586                             // e[..]
2587                             token::CloseDelim(token::Bracket) => {
2588                                 self.bump();
2589                                 hi = self.span.hi;
2590                                 let slice = self.mk_slice(e, None, None, mutbl);
2591                                 e = self.mk_expr(lo, hi, slice);
2592
2593                                 self.span_err(e.span, "incorrect slicing expression: `[..]`");
2594                                 self.span_note(e.span,
2595                                     "use `expr[]` to construct a slice of the whole of expr");
2596                             }
2597                             // e[..e]
2598                             _ => {
2599                                 hi = self.span.hi;
2600                                 let e2 = self.parse_expr();
2601                                 self.commit_expr_expecting(&*e2, token::CloseDelim(token::Bracket));
2602                                 let slice = self.mk_slice(e, None, Some(e2), mutbl);
2603                                 e = self.mk_expr(lo, hi, slice)
2604                             }
2605                         }
2606                     }
2607                     // e[e] | e[e..] | e[e..e]
2608                     _ => {
2609                         let ix = self.parse_expr_res(RESTRICTION_NO_DOTS);
2610                         match self.token {
2611                             // e[e..] | e[e..e]
2612                             token::DotDot => {
2613                                 self.bump();
2614                                 let e2 = match self.token {
2615                                     // e[e..]
2616                                     token::CloseDelim(token::Bracket) => {
2617                                         self.bump();
2618                                         None
2619                                     }
2620                                     // e[e..e]
2621                                     _ => {
2622                                         let e2 = self.parse_expr_res(RESTRICTION_NO_DOTS);
2623                                         self.commit_expr_expecting(&*e2,
2624                                             token::CloseDelim(token::Bracket));
2625                                         Some(e2)
2626                                     }
2627                                 };
2628                                 hi = self.span.hi;
2629                                 let slice = self.mk_slice(e, Some(ix), e2, mutbl);
2630                                 e = self.mk_expr(lo, hi, slice)
2631                             }
2632                             // e[e]
2633                             _ => {
2634                                 if mutbl == ast::MutMutable {
2635                                     self.span_err(e.span,
2636                                                   "`mut` keyword is invalid in index expressions");
2637                                 }
2638                                 hi = self.span.hi;
2639                                 self.commit_expr_expecting(&*ix, token::CloseDelim(token::Bracket));
2640                                 let index = self.mk_index(e, ix);
2641                                 e = self.mk_expr(lo, hi, index)
2642                             }
2643                         }
2644                     }
2645                 }
2646               }
2647
2648               // A range expression, either `expr..expr` or `expr..`.
2649               token::DotDot if !self.restrictions.contains(RESTRICTION_NO_DOTS) => {
2650                 self.bump();
2651
2652                 let opt_end = if self.token.can_begin_expr() {
2653                     let end = self.parse_expr_res(RESTRICTION_NO_DOTS);
2654                     Some(end)
2655                 } else {
2656                     None
2657                 };
2658
2659                 let hi = self.span.hi;
2660                 let range = self.mk_range(Some(e), opt_end);
2661                 return self.mk_expr(lo, hi, range);
2662               }
2663               _ => return e
2664             }
2665         }
2666         return e;
2667     }
2668
2669     /// Parse an optional separator followed by a Kleene-style
2670     /// repetition token (+ or *).
2671     pub fn parse_sep_and_kleene_op(&mut self) -> (Option<token::Token>, ast::KleeneOp) {
2672         fn parse_kleene_op(parser: &mut Parser) -> Option<ast::KleeneOp> {
2673             match parser.token {
2674                 token::BinOp(token::Star) => {
2675                     parser.bump();
2676                     Some(ast::ZeroOrMore)
2677                 },
2678                 token::BinOp(token::Plus) => {
2679                     parser.bump();
2680                     Some(ast::OneOrMore)
2681                 },
2682                 _ => None
2683             }
2684         };
2685
2686         match parse_kleene_op(self) {
2687             Some(kleene_op) => return (None, kleene_op),
2688             None => {}
2689         }
2690
2691         let separator = self.bump_and_get();
2692         match parse_kleene_op(self) {
2693             Some(zerok) => (Some(separator), zerok),
2694             None => self.fatal("expected `*` or `+`")
2695         }
2696     }
2697
2698     /// parse a single token tree from the input.
2699     pub fn parse_token_tree(&mut self) -> TokenTree {
2700         // FIXME #6994: currently, this is too eager. It
2701         // parses token trees but also identifies TtSequence's
2702         // and token::SubstNt's; it's too early to know yet
2703         // whether something will be a nonterminal or a seq
2704         // yet.
2705         maybe_whole!(deref self, NtTT);
2706
2707         // this is the fall-through for the 'match' below.
2708         // invariants: the current token is not a left-delimiter,
2709         // not an EOF, and not the desired right-delimiter (if
2710         // it were, parse_seq_to_before_end would have prevented
2711         // reaching this point.
2712         fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree {
2713             maybe_whole!(deref p, NtTT);
2714             match p.token {
2715               token::CloseDelim(_) => {
2716                   // This is a conservative error: only report the last unclosed delimiter. The
2717                   // previous unclosed delimiters could actually be closed! The parser just hasn't
2718                   // gotten to them yet.
2719                   match p.open_braces.last() {
2720                       None => {}
2721                       Some(&sp) => p.span_note(sp, "unclosed delimiter"),
2722                   };
2723                   let token_str = p.this_token_to_string();
2724                   p.fatal(format!("incorrect close delimiter: `{}`",
2725                                   token_str)[])
2726               },
2727               /* we ought to allow different depths of unquotation */
2728               token::Dollar if p.quote_depth > 0u => {
2729                 p.bump();
2730                 let sp = p.span;
2731
2732                 if p.token == token::OpenDelim(token::Paren) {
2733                     let seq = p.parse_seq(
2734                         &token::OpenDelim(token::Paren),
2735                         &token::CloseDelim(token::Paren),
2736                         seq_sep_none(),
2737                         |p| p.parse_token_tree()
2738                     );
2739                     let (sep, repeat) = p.parse_sep_and_kleene_op();
2740                     let seq = match seq {
2741                         Spanned { node, .. } => node,
2742                     };
2743                     let name_num = macro_parser::count_names(seq[]);
2744                     TtSequence(mk_sp(sp.lo, p.span.hi),
2745                                Rc::new(SequenceRepetition {
2746                                    tts: seq,
2747                                    separator: sep,
2748                                    op: repeat,
2749                                    num_captures: name_num
2750                                }))
2751                 } else {
2752                     // A nonterminal that matches or not
2753                     let namep = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2754                     let name = p.parse_ident();
2755                     if p.token == token::Colon && p.look_ahead(1, |t| t.is_ident()) {
2756                         p.bump();
2757                         let kindp = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2758                         let nt_kind = p.parse_ident();
2759                         let m = TtToken(sp, MatchNt(name, nt_kind, namep, kindp));
2760                         m
2761                     } else {
2762                         TtToken(sp, SubstNt(name, namep))
2763                     }
2764                 }
2765               }
2766               _ => {
2767                   TtToken(p.span, p.bump_and_get())
2768               }
2769             }
2770         }
2771
2772         match self.token {
2773             token::Eof => {
2774                 let open_braces = self.open_braces.clone();
2775                 for sp in open_braces.iter() {
2776                     self.span_help(*sp, "did you mean to close this delimiter?");
2777                 }
2778                 // There shouldn't really be a span, but it's easier for the test runner
2779                 // if we give it one
2780                 self.fatal("this file contains an un-closed delimiter ");
2781             },
2782             token::OpenDelim(delim) => {
2783                 // The span for beginning of the delimited section
2784                 let pre_span = self.span;
2785
2786                 // Parse the open delimiter.
2787                 self.open_braces.push(self.span);
2788                 let open_span = self.span;
2789                 self.bump();
2790
2791                 // Parse the token trees within the delimeters
2792                 let tts = self.parse_seq_to_before_end(
2793                     &token::CloseDelim(delim),
2794                     seq_sep_none(),
2795                     |p| p.parse_token_tree()
2796                 );
2797
2798                 // Parse the close delimiter.
2799                 let close_span = self.span;
2800                 self.bump();
2801                 self.open_braces.pop().unwrap();
2802
2803                 // Expand to cover the entire delimited token tree
2804                 let span = Span { hi: self.span.hi, ..pre_span };
2805
2806                 TtDelimited(span, Rc::new(Delimited {
2807                     delim: delim,
2808                     open_span: open_span,
2809                     tts: tts,
2810                     close_span: close_span,
2811                 }))
2812             },
2813             _ => parse_non_delim_tt_tok(self),
2814         }
2815     }
2816
2817     // parse a stream of tokens into a list of TokenTree's,
2818     // up to EOF.
2819     pub fn parse_all_token_trees(&mut self) -> Vec<TokenTree> {
2820         let mut tts = Vec::new();
2821         while self.token != token::Eof {
2822             tts.push(self.parse_token_tree());
2823         }
2824         tts
2825     }
2826
2827     /// Parse a prefix-operator expr
2828     pub fn parse_prefix_expr(&mut self) -> P<Expr> {
2829         let lo = self.span.lo;
2830         let hi;
2831
2832         let ex;
2833         match self.token {
2834           token::Not => {
2835             self.bump();
2836             let e = self.parse_prefix_expr();
2837             hi = e.span.hi;
2838             ex = self.mk_unary(UnNot, e);
2839           }
2840           token::BinOp(token::Minus) => {
2841             self.bump();
2842             let e = self.parse_prefix_expr();
2843             hi = e.span.hi;
2844             ex = self.mk_unary(UnNeg, e);
2845           }
2846           token::BinOp(token::Star) => {
2847             self.bump();
2848             let e = self.parse_prefix_expr();
2849             hi = e.span.hi;
2850             ex = self.mk_unary(UnDeref, e);
2851           }
2852           token::BinOp(token::And) | token::AndAnd => {
2853             self.expect_and();
2854             let m = self.parse_mutability();
2855             let e = self.parse_prefix_expr();
2856             hi = e.span.hi;
2857             ex = ExprAddrOf(m, e);
2858           }
2859           token::Tilde => {
2860             self.bump();
2861             let last_span = self.last_span;
2862             match self.token {
2863                 token::OpenDelim(token::Bracket) => {
2864                     self.obsolete(last_span, ObsoleteOwnedVector)
2865                 },
2866                 _ => self.obsolete(last_span, ObsoleteOwnedExpr)
2867             }
2868
2869             let e = self.parse_prefix_expr();
2870             hi = e.span.hi;
2871             ex = self.mk_unary(UnUniq, e);
2872           }
2873           token::DotDot if !self.restrictions.contains(RESTRICTION_NO_DOTS) => {
2874             // A range, closed above: `..expr`.
2875             self.bump();
2876             let e = self.parse_prefix_expr();
2877             hi = e.span.hi;
2878             ex = self.mk_range(None, Some(e));
2879           }
2880           token::Ident(_, _) => {
2881             if !self.token.is_keyword(keywords::Box) {
2882                 return self.parse_dot_or_call_expr();
2883             }
2884
2885             let lo = self.span.lo;
2886
2887             self.bump();
2888
2889             // Check for a place: `box(PLACE) EXPR`.
2890             if self.eat(&token::OpenDelim(token::Paren)) {
2891                 // Support `box() EXPR` as the default.
2892                 if !self.eat(&token::CloseDelim(token::Paren)) {
2893                     let place = self.parse_expr();
2894                     self.expect(&token::CloseDelim(token::Paren));
2895                     // Give a suggestion to use `box()` when a parenthesised expression is used
2896                     if !self.token.can_begin_expr() {
2897                         let span = self.span;
2898                         let this_token_to_string = self.this_token_to_string();
2899                         self.span_err(span,
2900                                       format!("expected expression, found `{}`",
2901                                               this_token_to_string)[]);
2902                         let box_span = mk_sp(lo, self.last_span.hi);
2903                         self.span_help(box_span,
2904                                        "perhaps you meant `box() (foo)` instead?");
2905                         self.abort_if_errors();
2906                     }
2907                     let subexpression = self.parse_prefix_expr();
2908                     hi = subexpression.span.hi;
2909                     ex = ExprBox(Some(place), subexpression);
2910                     return self.mk_expr(lo, hi, ex);
2911                 }
2912             }
2913
2914             // Otherwise, we use the unique pointer default.
2915             let subexpression = self.parse_prefix_expr();
2916             hi = subexpression.span.hi;
2917             // FIXME (pnkfelix): After working out kinks with box
2918             // desugaring, should be `ExprBox(None, subexpression)`
2919             // instead.
2920             ex = self.mk_unary(UnUniq, subexpression);
2921           }
2922           _ => return self.parse_dot_or_call_expr()
2923         }
2924         return self.mk_expr(lo, hi, ex);
2925     }
2926
2927     /// Parse an expression of binops
2928     pub fn parse_binops(&mut self) -> P<Expr> {
2929         let prefix_expr = self.parse_prefix_expr();
2930         self.parse_more_binops(prefix_expr, 0)
2931     }
2932
2933     /// Parse an expression of binops of at least min_prec precedence
2934     pub fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: uint) -> P<Expr> {
2935         if self.expr_is_complete(&*lhs) { return lhs; }
2936
2937         // Prevent dynamic borrow errors later on by limiting the
2938         // scope of the borrows.
2939         if self.token == token::BinOp(token::Or) &&
2940             self.restrictions.contains(RESTRICTION_NO_BAR_OP) {
2941             return lhs;
2942         }
2943         self.expected_tokens.push(TokenType::Operator);
2944
2945         let cur_opt = self.token.to_binop();
2946         match cur_opt {
2947             Some(cur_op) => {
2948                 let cur_prec = operator_prec(cur_op);
2949                 if cur_prec > min_prec {
2950                     self.bump();
2951                     let expr = self.parse_prefix_expr();
2952                     let rhs = self.parse_more_binops(expr, cur_prec);
2953                     let lhs_span = lhs.span;
2954                     let rhs_span = rhs.span;
2955                     let binary = self.mk_binary(cur_op, lhs, rhs);
2956                     let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary);
2957                     self.parse_more_binops(bin, min_prec)
2958                 } else {
2959                     lhs
2960                 }
2961             }
2962             None => {
2963                 if as_prec > min_prec && self.eat_keyword(keywords::As) {
2964                     let rhs = self.parse_ty();
2965                     let _as = self.mk_expr(lhs.span.lo,
2966                                            rhs.span.hi,
2967                                            ExprCast(lhs, rhs));
2968                     self.parse_more_binops(_as, min_prec)
2969                 } else {
2970                     lhs
2971                 }
2972             }
2973         }
2974     }
2975
2976     /// Parse an assignment expression....
2977     /// actually, this seems to be the main entry point for
2978     /// parsing an arbitrary expression.
2979     pub fn parse_assign_expr(&mut self) -> P<Expr> {
2980         let lo = self.span.lo;
2981         let lhs = self.parse_binops();
2982         let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL;
2983         match self.token {
2984           token::Eq => {
2985               self.bump();
2986               let rhs = self.parse_expr_res(restrictions);
2987               self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
2988           }
2989           token::BinOpEq(op) => {
2990               self.bump();
2991               let rhs = self.parse_expr_res(restrictions);
2992               let aop = match op {
2993                   token::Plus =>    BiAdd,
2994                   token::Minus =>   BiSub,
2995                   token::Star =>    BiMul,
2996                   token::Slash =>   BiDiv,
2997                   token::Percent => BiRem,
2998                   token::Caret =>   BiBitXor,
2999                   token::And =>     BiBitAnd,
3000                   token::Or =>      BiBitOr,
3001                   token::Shl =>     BiShl,
3002                   token::Shr =>     BiShr
3003               };
3004               let rhs_span = rhs.span;
3005               let assign_op = self.mk_assign_op(aop, lhs, rhs);
3006               self.mk_expr(lo, rhs_span.hi, assign_op)
3007           }
3008           _ => {
3009               lhs
3010           }
3011         }
3012     }
3013
3014     /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3015     pub fn parse_if_expr(&mut self) -> P<Expr> {
3016         if self.token.is_keyword(keywords::Let) {
3017             return self.parse_if_let_expr();
3018         }
3019         let lo = self.last_span.lo;
3020         let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3021         let thn = self.parse_block();
3022         let mut els: Option<P<Expr>> = None;
3023         let mut hi = thn.span.hi;
3024         if self.eat_keyword(keywords::Else) {
3025             let elexpr = self.parse_else_expr();
3026             hi = elexpr.span.hi;
3027             els = Some(elexpr);
3028         }
3029         self.mk_expr(lo, hi, ExprIf(cond, thn, els))
3030     }
3031
3032     /// Parse an 'if let' expression ('if' token already eaten)
3033     pub fn parse_if_let_expr(&mut self) -> P<Expr> {
3034         let lo = self.last_span.lo;
3035         self.expect_keyword(keywords::Let);
3036         let pat = self.parse_pat();
3037         self.expect(&token::Eq);
3038         let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3039         let thn = self.parse_block();
3040         let (hi, els) = if self.eat_keyword(keywords::Else) {
3041             let expr = self.parse_else_expr();
3042             (expr.span.hi, Some(expr))
3043         } else {
3044             (thn.span.hi, None)
3045         };
3046         self.mk_expr(lo, hi, ExprIfLet(pat, expr, thn, els))
3047     }
3048
3049     // `|args| expr`
3050     pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
3051                              -> P<Expr>
3052     {
3053         let lo = self.span.lo;
3054         let (decl, optional_unboxed_closure_kind) =
3055             self.parse_fn_block_decl();
3056         let body = self.parse_expr();
3057         let fakeblock = P(ast::Block {
3058             id: ast::DUMMY_NODE_ID,
3059             view_items: Vec::new(),
3060             stmts: Vec::new(),
3061             span: body.span,
3062             expr: Some(body),
3063             rules: DefaultBlock,
3064         });
3065
3066         self.mk_expr(
3067             lo,
3068             fakeblock.span.hi,
3069             ExprClosure(capture_clause, optional_unboxed_closure_kind, decl, fakeblock))
3070     }
3071
3072     pub fn parse_else_expr(&mut self) -> P<Expr> {
3073         if self.eat_keyword(keywords::If) {
3074             return self.parse_if_expr();
3075         } else {
3076             let blk = self.parse_block();
3077             return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
3078         }
3079     }
3080
3081     /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3082     pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3083         // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3084
3085         let lo = self.last_span.lo;
3086         let pat = self.parse_pat();
3087         self.expect_keyword(keywords::In);
3088         let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3089         let loop_block = self.parse_block();
3090         let hi = self.span.hi;
3091
3092         self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident))
3093     }
3094
3095     /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3096     pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3097         if self.token.is_keyword(keywords::Let) {
3098             return self.parse_while_let_expr(opt_ident);
3099         }
3100         let lo = self.last_span.lo;
3101         let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3102         let body = self.parse_block();
3103         let hi = body.span.hi;
3104         return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
3105     }
3106
3107     /// Parse a 'while let' expression ('while' token already eaten)
3108     pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3109         let lo = self.last_span.lo;
3110         self.expect_keyword(keywords::Let);
3111         let pat = self.parse_pat();
3112         self.expect(&token::Eq);
3113         let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3114         let body = self.parse_block();
3115         let hi = body.span.hi;
3116         return self.mk_expr(lo, hi, ExprWhileLet(pat, expr, body, opt_ident));
3117     }
3118
3119     pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3120         let lo = self.last_span.lo;
3121         let body = self.parse_block();
3122         let hi = body.span.hi;
3123         self.mk_expr(lo, hi, ExprLoop(body, opt_ident))
3124     }
3125
3126     fn parse_match_expr(&mut self) -> P<Expr> {
3127         let lo = self.last_span.lo;
3128         let discriminant = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3129         self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace));
3130         let mut arms: Vec<Arm> = Vec::new();
3131         while self.token != token::CloseDelim(token::Brace) {
3132             arms.push(self.parse_arm());
3133         }
3134         let hi = self.span.hi;
3135         self.bump();
3136         return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchSource::Normal));
3137     }
3138
3139     pub fn parse_arm(&mut self) -> Arm {
3140         let attrs = self.parse_outer_attributes();
3141         let pats = self.parse_pats();
3142         let mut guard = None;
3143         if self.eat_keyword(keywords::If) {
3144             guard = Some(self.parse_expr());
3145         }
3146         self.expect(&token::FatArrow);
3147         let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3148
3149         let require_comma =
3150             !classify::expr_is_simple_block(&*expr)
3151             && self.token != token::CloseDelim(token::Brace);
3152
3153         if require_comma {
3154             self.commit_expr(&*expr, &[token::Comma], &[token::CloseDelim(token::Brace)]);
3155         } else {
3156             self.eat(&token::Comma);
3157         }
3158
3159         ast::Arm {
3160             attrs: attrs,
3161             pats: pats,
3162             guard: guard,
3163             body: expr,
3164         }
3165     }
3166
3167     /// Parse an expression
3168     pub fn parse_expr(&mut self) -> P<Expr> {
3169         return self.parse_expr_res(UNRESTRICTED);
3170     }
3171
3172     /// Parse an expression, subject to the given restrictions
3173     pub fn parse_expr_res(&mut self, r: Restrictions) -> P<Expr> {
3174         let old = self.restrictions;
3175         self.restrictions = r;
3176         let e = self.parse_assign_expr();
3177         self.restrictions = old;
3178         return e;
3179     }
3180
3181     /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3182     fn parse_initializer(&mut self) -> Option<P<Expr>> {
3183         if self.check(&token::Eq) {
3184             self.bump();
3185             Some(self.parse_expr())
3186         } else {
3187             None
3188         }
3189     }
3190
3191     /// Parse patterns, separated by '|' s
3192     fn parse_pats(&mut self) -> Vec<P<Pat>> {
3193         let mut pats = Vec::new();
3194         loop {
3195             pats.push(self.parse_pat());
3196             if self.check(&token::BinOp(token::Or)) { self.bump(); }
3197             else { return pats; }
3198         };
3199     }
3200
3201     fn parse_pat_vec_elements(
3202         &mut self,
3203     ) -> (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>) {
3204         let mut before = Vec::new();
3205         let mut slice = None;
3206         let mut after = Vec::new();
3207         let mut first = true;
3208         let mut before_slice = true;
3209
3210         while self.token != token::CloseDelim(token::Bracket) {
3211             if first {
3212                 first = false;
3213             } else {
3214                 self.expect(&token::Comma);
3215
3216                 if self.token == token::CloseDelim(token::Bracket)
3217                         && (before_slice || after.len() != 0) {
3218                     break
3219                 }
3220             }
3221
3222             if before_slice {
3223                 if self.check(&token::DotDot) {
3224                     self.bump();
3225
3226                     if self.check(&token::Comma) ||
3227                             self.check(&token::CloseDelim(token::Bracket)) {
3228                         slice = Some(P(ast::Pat {
3229                             id: ast::DUMMY_NODE_ID,
3230                             node: PatWild(PatWildMulti),
3231                             span: self.span,
3232                         }));
3233                         before_slice = false;
3234                     } else {
3235                         let _ = self.parse_pat();
3236                         let span = self.span;
3237                         self.obsolete(span, ObsoleteSubsliceMatch);
3238                     }
3239                     continue
3240                 }
3241             }
3242
3243             let subpat = self.parse_pat();
3244             if before_slice && self.check(&token::DotDot) {
3245                 self.bump();
3246                 slice = Some(subpat);
3247                 before_slice = false;
3248             } else if before_slice {
3249                 before.push(subpat);
3250             } else {
3251                 after.push(subpat);
3252             }
3253         }
3254
3255         (before, slice, after)
3256     }
3257
3258     /// Parse the fields of a struct-like pattern
3259     fn parse_pat_fields(&mut self) -> (Vec<codemap::Spanned<ast::FieldPat>> , bool) {
3260         let mut fields = Vec::new();
3261         let mut etc = false;
3262         let mut first = true;
3263         while self.token != token::CloseDelim(token::Brace) {
3264             if first {
3265                 first = false;
3266             } else {
3267                 self.expect(&token::Comma);
3268                 // accept trailing commas
3269                 if self.check(&token::CloseDelim(token::Brace)) { break }
3270             }
3271
3272             let lo = self.span.lo;
3273             let hi;
3274
3275             if self.check(&token::DotDot) {
3276                 self.bump();
3277                 if self.token != token::CloseDelim(token::Brace) {
3278                     let token_str = self.this_token_to_string();
3279                     self.fatal(format!("expected `{}`, found `{}`", "}",
3280                                        token_str)[])
3281                 }
3282                 etc = true;
3283                 break;
3284             }
3285
3286             let bind_type = if self.eat_keyword(keywords::Mut) {
3287                 BindByValue(MutMutable)
3288             } else if self.eat_keyword(keywords::Ref) {
3289                 BindByRef(self.parse_mutability())
3290             } else {
3291                 BindByValue(MutImmutable)
3292             };
3293
3294             let fieldname = self.parse_ident();
3295
3296             let (subpat, is_shorthand) = if self.check(&token::Colon) {
3297                 match bind_type {
3298                     BindByRef(..) | BindByValue(MutMutable) => {
3299                         let token_str = self.this_token_to_string();
3300                         self.fatal(format!("unexpected `{}`",
3301                                            token_str)[])
3302                     }
3303                     _ => {}
3304                 }
3305
3306                 self.bump();
3307                 let pat = self.parse_pat();
3308                 hi = pat.span.hi;
3309                 (pat, false)
3310             } else {
3311                 hi = self.last_span.hi;
3312                 let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname};
3313                 (P(ast::Pat {
3314                     id: ast::DUMMY_NODE_ID,
3315                     node: PatIdent(bind_type, fieldpath, None),
3316                     span: self.last_span
3317                 }), true)
3318             };
3319             fields.push(codemap::Spanned { span: mk_sp(lo, hi),
3320                                            node: ast::FieldPat { ident: fieldname,
3321                                                                  pat: subpat,
3322                                                                  is_shorthand: is_shorthand }});
3323         }
3324         return (fields, etc);
3325     }
3326
3327     /// Parse a pattern.
3328     pub fn parse_pat(&mut self) -> P<Pat> {
3329         maybe_whole!(self, NtPat);
3330
3331         let lo = self.span.lo;
3332         let mut hi;
3333         let pat;
3334         match self.token {
3335             // parse _
3336           token::Underscore => {
3337             self.bump();
3338             pat = PatWild(PatWildSingle);
3339             hi = self.last_span.hi;
3340             return P(ast::Pat {
3341                 id: ast::DUMMY_NODE_ID,
3342                 node: pat,
3343                 span: mk_sp(lo, hi)
3344             })
3345           }
3346           token::Tilde => {
3347             // parse ~pat
3348             self.bump();
3349             let sub = self.parse_pat();
3350             pat = PatBox(sub);
3351             let last_span = self.last_span;
3352             hi = last_span.hi;
3353             self.obsolete(last_span, ObsoleteOwnedPattern);
3354             return P(ast::Pat {
3355                 id: ast::DUMMY_NODE_ID,
3356                 node: pat,
3357                 span: mk_sp(lo, hi)
3358             })
3359           }
3360           token::BinOp(token::And) | token::AndAnd => {
3361             // parse &pat
3362             let lo = self.span.lo;
3363             self.expect_and();
3364             let sub = self.parse_pat();
3365             pat = PatRegion(sub);
3366             hi = self.last_span.hi;
3367             return P(ast::Pat {
3368                 id: ast::DUMMY_NODE_ID,
3369                 node: pat,
3370                 span: mk_sp(lo, hi)
3371             })
3372           }
3373           token::OpenDelim(token::Paren) => {
3374             // parse (pat,pat,pat,...) as tuple
3375             self.bump();
3376             if self.check(&token::CloseDelim(token::Paren)) {
3377                 self.bump();
3378                 pat = PatTup(vec![]);
3379             } else {
3380                 let mut fields = vec!(self.parse_pat());
3381                 if self.look_ahead(1, |t| *t != token::CloseDelim(token::Paren)) {
3382                     while self.check(&token::Comma) {
3383                         self.bump();
3384                         if self.check(&token::CloseDelim(token::Paren)) { break; }
3385                         fields.push(self.parse_pat());
3386                     }
3387                 }
3388                 if fields.len() == 1 { self.expect(&token::Comma); }
3389                 self.expect(&token::CloseDelim(token::Paren));
3390                 pat = PatTup(fields);
3391             }
3392             hi = self.last_span.hi;
3393             return P(ast::Pat {
3394                 id: ast::DUMMY_NODE_ID,
3395                 node: pat,
3396                 span: mk_sp(lo, hi)
3397             })
3398           }
3399           token::OpenDelim(token::Bracket) => {
3400             // parse [pat,pat,...] as vector pattern
3401             self.bump();
3402             let (before, slice, after) =
3403                 self.parse_pat_vec_elements();
3404
3405             self.expect(&token::CloseDelim(token::Bracket));
3406             pat = ast::PatVec(before, slice, after);
3407             hi = self.last_span.hi;
3408             return P(ast::Pat {
3409                 id: ast::DUMMY_NODE_ID,
3410                 node: pat,
3411                 span: mk_sp(lo, hi)
3412             })
3413           }
3414           _ => {}
3415         }
3416         // at this point, token != _, ~, &, &&, (, [
3417
3418         if (!(self.token.is_ident() || self.token.is_path())
3419               && self.token != token::ModSep)
3420                 || self.token.is_keyword(keywords::True)
3421                 || self.token.is_keyword(keywords::False) {
3422             // Parse an expression pattern or exp .. exp.
3423             //
3424             // These expressions are limited to literals (possibly
3425             // preceded by unary-minus) or identifiers.
3426             let val = self.parse_literal_maybe_minus();
3427             if (self.check(&token::DotDotDot)) &&
3428                     self.look_ahead(1, |t| {
3429                         *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3430                     }) {
3431                 self.bump();
3432                 let end = if self.token.is_ident() || self.token.is_path() {
3433                     let path = self.parse_path(LifetimeAndTypesWithColons);
3434                     let hi = self.span.hi;
3435                     self.mk_expr(lo, hi, ExprPath(path))
3436                 } else {
3437                     self.parse_literal_maybe_minus()
3438                 };
3439                 pat = PatRange(val, end);
3440             } else {
3441                 pat = PatLit(val);
3442             }
3443         } else if self.eat_keyword(keywords::Mut) {
3444             pat = self.parse_pat_ident(BindByValue(MutMutable));
3445         } else if self.eat_keyword(keywords::Ref) {
3446             // parse ref pat
3447             let mutbl = self.parse_mutability();
3448             pat = self.parse_pat_ident(BindByRef(mutbl));
3449         } else if self.eat_keyword(keywords::Box) {
3450             // `box PAT`
3451             //
3452             // FIXME(#13910): Rename to `PatBox` and extend to full DST
3453             // support.
3454             let sub = self.parse_pat();
3455             pat = PatBox(sub);
3456             hi = self.last_span.hi;
3457             return P(ast::Pat {
3458                 id: ast::DUMMY_NODE_ID,
3459                 node: pat,
3460                 span: mk_sp(lo, hi)
3461             })
3462         } else {
3463             let can_be_enum_or_struct = self.look_ahead(1, |t| {
3464                 match *t {
3465                     token::OpenDelim(_) | token::Lt | token::ModSep => true,
3466                     _ => false,
3467                 }
3468             });
3469
3470             if self.look_ahead(1, |t| *t == token::DotDotDot) &&
3471                     self.look_ahead(2, |t| {
3472                         *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3473                     }) {
3474                 let start = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3475                 self.eat(&token::DotDotDot);
3476                 let end = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3477                 pat = PatRange(start, end);
3478             } else if self.token.is_plain_ident() && !can_be_enum_or_struct {
3479                 let id = self.parse_ident();
3480                 let id_span = self.last_span;
3481                 let pth1 = codemap::Spanned{span:id_span, node: id};
3482                 if self.eat(&token::Not) {
3483                     // macro invocation
3484                     let delim = self.expect_open_delim();
3485                     let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
3486                                                     seq_sep_none(),
3487                                                     |p| p.parse_token_tree());
3488
3489                     let mac = MacInvocTT(ident_to_path(id_span,id), tts, EMPTY_CTXT);
3490                     pat = ast::PatMac(codemap::Spanned {node: mac, span: self.span});
3491                 } else {
3492                     let sub = if self.eat(&token::At) {
3493                         // parse foo @ pat
3494                         Some(self.parse_pat())
3495                     } else {
3496                         // or just foo
3497                         None
3498                     };
3499                     pat = PatIdent(BindByValue(MutImmutable), pth1, sub);
3500                 }
3501             } else {
3502                 // parse an enum pat
3503                 let enum_path = self.parse_path(LifetimeAndTypesWithColons);
3504                 match self.token {
3505                     token::OpenDelim(token::Brace) => {
3506                         self.bump();
3507                         let (fields, etc) =
3508                             self.parse_pat_fields();
3509                         self.bump();
3510                         pat = PatStruct(enum_path, fields, etc);
3511                     }
3512                     _ => {
3513                         let mut args: Vec<P<Pat>> = Vec::new();
3514                         match self.token {
3515                           token::OpenDelim(token::Paren) => {
3516                             let is_dotdot = self.look_ahead(1, |t| {
3517                                 match *t {
3518                                     token::DotDot => true,
3519                                     _ => false,
3520                                 }
3521                             });
3522                             if is_dotdot {
3523                                 // This is a "top constructor only" pat
3524                                 self.bump();
3525                                 self.bump();
3526                                 self.expect(&token::CloseDelim(token::Paren));
3527                                 pat = PatEnum(enum_path, None);
3528                             } else {
3529                                 args = self.parse_enum_variant_seq(
3530                                     &token::OpenDelim(token::Paren),
3531                                     &token::CloseDelim(token::Paren),
3532                                     seq_sep_trailing_allowed(token::Comma),
3533                                     |p| p.parse_pat()
3534                                 );
3535                                 pat = PatEnum(enum_path, Some(args));
3536                             }
3537                           },
3538                           _ => {
3539                               if !enum_path.global &&
3540                                   enum_path.segments.len() == 1 &&
3541                                   enum_path.segments[0].parameters.is_empty()
3542                               {
3543                                   // it could still be either an enum
3544                                   // or an identifier pattern, resolve
3545                                   // will sort it out:
3546                                   pat = PatIdent(BindByValue(MutImmutable),
3547                                                  codemap::Spanned{
3548                                                     span: enum_path.span,
3549                                                     node: enum_path.segments[0]
3550                                                            .identifier},
3551                                                  None);
3552                               } else {
3553                                   pat = PatEnum(enum_path, Some(args));
3554                               }
3555                           }
3556                         }
3557                     }
3558                 }
3559             }
3560         }
3561         hi = self.last_span.hi;
3562         P(ast::Pat {
3563             id: ast::DUMMY_NODE_ID,
3564             node: pat,
3565             span: mk_sp(lo, hi),
3566         })
3567     }
3568
3569     /// Parse ident or ident @ pat
3570     /// used by the copy foo and ref foo patterns to give a good
3571     /// error message when parsing mistakes like ref foo(a,b)
3572     fn parse_pat_ident(&mut self,
3573                        binding_mode: ast::BindingMode)
3574                        -> ast::Pat_ {
3575         if !self.token.is_plain_ident() {
3576             let span = self.span;
3577             let tok_str = self.this_token_to_string();
3578             self.span_fatal(span,
3579                             format!("expected identifier, found `{}`", tok_str)[]);
3580         }
3581         let ident = self.parse_ident();
3582         let last_span = self.last_span;
3583         let name = codemap::Spanned{span: last_span, node: ident};
3584         let sub = if self.eat(&token::At) {
3585             Some(self.parse_pat())
3586         } else {
3587             None
3588         };
3589
3590         // just to be friendly, if they write something like
3591         //   ref Some(i)
3592         // we end up here with ( as the current token.  This shortly
3593         // leads to a parse error.  Note that if there is no explicit
3594         // binding mode then we do not end up here, because the lookahead
3595         // will direct us over to parse_enum_variant()
3596         if self.token == token::OpenDelim(token::Paren) {
3597             let last_span = self.last_span;
3598             self.span_fatal(
3599                 last_span,
3600                 "expected identifier, found enum pattern");
3601         }
3602
3603         PatIdent(binding_mode, name, sub)
3604     }
3605
3606     /// Parse a local variable declaration
3607     fn parse_local(&mut self) -> P<Local> {
3608         let lo = self.span.lo;
3609         let pat = self.parse_pat();
3610
3611         let mut ty = None;
3612         if self.eat(&token::Colon) {
3613             ty = Some(self.parse_ty_sum());
3614         }
3615         let init = self.parse_initializer();
3616         P(ast::Local {
3617             ty: ty,
3618             pat: pat,
3619             init: init,
3620             id: ast::DUMMY_NODE_ID,
3621             span: mk_sp(lo, self.last_span.hi),
3622             source: LocalLet,
3623         })
3624     }
3625
3626     /// Parse a "let" stmt
3627     fn parse_let(&mut self) -> P<Decl> {
3628         let lo = self.span.lo;
3629         let local = self.parse_local();
3630         P(spanned(lo, self.last_span.hi, DeclLocal(local)))
3631     }
3632
3633     /// Parse a structure field
3634     fn parse_name_and_ty(&mut self, pr: Visibility,
3635                          attrs: Vec<Attribute> ) -> StructField {
3636         let lo = self.span.lo;
3637         if !self.token.is_plain_ident() {
3638             self.fatal("expected ident");
3639         }
3640         let name = self.parse_ident();
3641         self.expect(&token::Colon);
3642         let ty = self.parse_ty_sum();
3643         spanned(lo, self.last_span.hi, ast::StructField_ {
3644             kind: NamedField(name, pr),
3645             id: ast::DUMMY_NODE_ID,
3646             ty: ty,
3647             attrs: attrs,
3648         })
3649     }
3650
3651     /// Get an expected item after attributes error message.
3652     fn expected_item_err(attrs: &[Attribute]) -> &'static str {
3653         match attrs.last() {
3654             Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
3655                 "expected item after doc comment"
3656             }
3657             _ => "expected item after attributes",
3658         }
3659     }
3660
3661     /// Parse a statement. may include decl.
3662     /// Precondition: any attributes are parsed already
3663     pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> P<Stmt> {
3664         maybe_whole!(self, NtStmt);
3665
3666         fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) {
3667             // If we have attributes then we should have an item
3668             if !attrs.is_empty() {
3669                 let last_span = p.last_span;
3670                 p.span_err(last_span, Parser::expected_item_err(attrs));
3671             }
3672         }
3673
3674         let lo = self.span.lo;
3675         if self.token.is_keyword(keywords::Let) {
3676             check_expected_item(self, item_attrs[]);
3677             self.expect_keyword(keywords::Let);
3678             let decl = self.parse_let();
3679             P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3680         } else if self.token.is_ident()
3681             && !self.token.is_any_keyword()
3682             && self.look_ahead(1, |t| *t == token::Not) {
3683             // it's a macro invocation:
3684
3685             check_expected_item(self, item_attrs[]);
3686
3687             // Potential trouble: if we allow macros with paths instead of
3688             // idents, we'd need to look ahead past the whole path here...
3689             let pth = self.parse_path(NoTypesAllowed);
3690             self.bump();
3691
3692             let id = match self.token {
3693                 token::OpenDelim(_) => token::special_idents::invalid, // no special identifier
3694                 _ => self.parse_ident(),
3695             };
3696
3697             // check that we're pointing at delimiters (need to check
3698             // again after the `if`, because of `parse_ident`
3699             // consuming more tokens).
3700             let delim = match self.token {
3701                 token::OpenDelim(delim) => delim,
3702                 _ => {
3703                     // we only expect an ident if we didn't parse one
3704                     // above.
3705                     let ident_str = if id.name == token::special_idents::invalid.name {
3706                         "identifier, "
3707                     } else {
3708                         ""
3709                     };
3710                     let tok_str = self.this_token_to_string();
3711                     self.fatal(format!("expected {}`(` or `{{`, found `{}`",
3712                                        ident_str,
3713                                        tok_str)[])
3714                 },
3715             };
3716
3717             let tts = self.parse_unspanned_seq(
3718                 &token::OpenDelim(delim),
3719                 &token::CloseDelim(delim),
3720                 seq_sep_none(),
3721                 |p| p.parse_token_tree()
3722             );
3723             let hi = self.span.hi;
3724
3725             let style = if delim == token::Brace {
3726                 MacStmtWithBraces
3727             } else {
3728                 MacStmtWithoutBraces
3729             };
3730
3731             if id.name == token::special_idents::invalid.name {
3732                 P(spanned(lo,
3733                           hi,
3734                           StmtMac(P(spanned(lo,
3735                                           hi,
3736                                           MacInvocTT(pth, tts, EMPTY_CTXT))),
3737                                   style)))
3738             } else {
3739                 // if it has a special ident, it's definitely an item
3740                 //
3741                 // Require a semicolon or braces.
3742                 if style != MacStmtWithBraces {
3743                     if !self.eat(&token::Semi) {
3744                         let last_span = self.last_span;
3745                         self.span_err(last_span,
3746                                       "macros that expand to items must \
3747                                        either be surrounded with braces or \
3748                                        followed by a semicolon");
3749                     }
3750                 }
3751                 P(spanned(lo, hi, StmtDecl(
3752                     P(spanned(lo, hi, DeclItem(
3753                         self.mk_item(
3754                             lo, hi, id /*id is good here*/,
3755                             ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))),
3756                             Inherited, Vec::new(/*no attrs*/))))),
3757                     ast::DUMMY_NODE_ID)))
3758             }
3759         } else {
3760             let found_attrs = !item_attrs.is_empty();
3761             let item_err = Parser::expected_item_err(item_attrs[]);
3762             match self.parse_item_or_view_item(item_attrs, false) {
3763                 IoviItem(i) => {
3764                     let hi = i.span.hi;
3765                     let decl = P(spanned(lo, hi, DeclItem(i)));
3766                     P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3767                 }
3768                 IoviViewItem(vi) => {
3769                     self.span_fatal(vi.span,
3770                                     "view items must be declared at the top of the block");
3771                 }
3772                 IoviForeignItem(_) => {
3773                     self.fatal("foreign items are not allowed here");
3774                 }
3775                 IoviNone(_) => {
3776                     if found_attrs {
3777                         let last_span = self.last_span;
3778                         self.span_err(last_span, item_err);
3779                     }
3780
3781                     // Remainder are line-expr stmts.
3782                     let e = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3783                     P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
3784                 }
3785             }
3786         }
3787     }
3788
3789     /// Is this expression a successfully-parsed statement?
3790     fn expr_is_complete(&mut self, e: &Expr) -> bool {
3791         self.restrictions.contains(RESTRICTION_STMT_EXPR) &&
3792             !classify::expr_requires_semi_to_be_stmt(e)
3793     }
3794
3795     /// Parse a block. No inner attrs are allowed.
3796     pub fn parse_block(&mut self) -> P<Block> {
3797         maybe_whole!(no_clone self, NtBlock);
3798
3799         let lo = self.span.lo;
3800
3801         if !self.eat(&token::OpenDelim(token::Brace)) {
3802             let sp = self.span;
3803             let tok = self.this_token_to_string();
3804             self.span_fatal_help(sp,
3805                                  format!("expected `{{`, found `{}`", tok)[],
3806                                  "place this code inside a block");
3807         }
3808
3809         return self.parse_block_tail_(lo, DefaultBlock, Vec::new());
3810     }
3811
3812     /// Parse a block. Inner attrs are allowed.
3813     fn parse_inner_attrs_and_block(&mut self)
3814         -> (Vec<Attribute> , P<Block>) {
3815
3816         maybe_whole!(pair_empty self, NtBlock);
3817
3818         let lo = self.span.lo;
3819         self.expect(&token::OpenDelim(token::Brace));
3820         let (inner, next) = self.parse_inner_attrs_and_next();
3821
3822         (inner, self.parse_block_tail_(lo, DefaultBlock, next))
3823     }
3824
3825     /// Precondition: already parsed the '{' or '#{'
3826     /// I guess that also means "already parsed the 'impure'" if
3827     /// necessary, and this should take a qualifier.
3828     /// Some blocks start with "#{"...
3829     fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> P<Block> {
3830         self.parse_block_tail_(lo, s, Vec::new())
3831     }
3832
3833     /// Parse the rest of a block expression or function body
3834     fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode,
3835                          first_item_attrs: Vec<Attribute> ) -> P<Block> {
3836         let mut stmts = Vec::new();
3837         let mut expr = None;
3838
3839         // wouldn't it be more uniform to parse view items only, here?
3840         let ParsedItemsAndViewItems {
3841             attrs_remaining,
3842             view_items,
3843             items,
3844             ..
3845         } = self.parse_items_and_view_items(first_item_attrs,
3846                                             false, false);
3847
3848         for item in items.into_iter() {
3849             let span = item.span;
3850             let decl = P(spanned(span.lo, span.hi, DeclItem(item)));
3851             stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))));
3852         }
3853
3854         let mut attributes_box = attrs_remaining;
3855
3856         while self.token != token::CloseDelim(token::Brace) {
3857             // parsing items even when they're not allowed lets us give
3858             // better error messages and recover more gracefully.
3859             attributes_box.push_all(self.parse_outer_attributes()[]);
3860             match self.token {
3861                 token::Semi => {
3862                     if !attributes_box.is_empty() {
3863                         let last_span = self.last_span;
3864                         self.span_err(last_span,
3865                                       Parser::expected_item_err(attributes_box[]));
3866                         attributes_box = Vec::new();
3867                     }
3868                     self.bump(); // empty
3869                 }
3870                 token::CloseDelim(token::Brace) => {
3871                     // fall through and out.
3872                 }
3873                 _ => {
3874                     let stmt = self.parse_stmt(attributes_box);
3875                     attributes_box = Vec::new();
3876                     stmt.and_then(|Spanned {node, span}| match node {
3877                         StmtExpr(e, stmt_id) => {
3878                             self.handle_expression_like_statement(e,
3879                                                                   stmt_id,
3880                                                                   span,
3881                                                                   &mut stmts,
3882                                                                   &mut expr);
3883                         }
3884                         StmtMac(macro, MacStmtWithoutBraces) => {
3885                             // statement macro without braces; might be an
3886                             // expr depending on whether a semicolon follows
3887                             match self.token {
3888                                 token::Semi => {
3889                                     stmts.push(P(Spanned {
3890                                         node: StmtMac(macro,
3891                                                       MacStmtWithSemicolon),
3892                                         span: span,
3893                                     }));
3894                                     self.bump();
3895                                 }
3896                                 _ => {
3897                                     let e = self.mk_mac_expr(span.lo,
3898                                                              span.hi,
3899                                                              macro.and_then(|m| m.node));
3900                                     let e =
3901                                         self.parse_dot_or_call_expr_with(e);
3902                                     self.handle_expression_like_statement(
3903                                         e,
3904                                         ast::DUMMY_NODE_ID,
3905                                         span,
3906                                         &mut stmts,
3907                                         &mut expr);
3908                                 }
3909                             }
3910                         }
3911                         StmtMac(m, style) => {
3912                             // statement macro; might be an expr
3913                             match self.token {
3914                                 token::Semi => {
3915                                     stmts.push(P(Spanned {
3916                                         node: StmtMac(m,
3917                                                       MacStmtWithSemicolon),
3918                                         span: span,
3919                                     }));
3920                                     self.bump();
3921                                 }
3922                                 token::CloseDelim(token::Brace) => {
3923                                     // if a block ends in `m!(arg)` without
3924                                     // a `;`, it must be an expr
3925                                     expr = Some(
3926                                         self.mk_mac_expr(span.lo,
3927                                                          span.hi,
3928                                                          m.and_then(|x| x.node)));
3929                                 }
3930                                 _ => {
3931                                     stmts.push(P(Spanned {
3932                                         node: StmtMac(m, style),
3933                                         span: span
3934                                     }));
3935                                 }
3936                             }
3937                         }
3938                         _ => { // all other kinds of statements:
3939                             if classify::stmt_ends_with_semi(&node) {
3940                                 self.commit_stmt_expecting(token::Semi);
3941                             }
3942
3943                             stmts.push(P(Spanned {
3944                                 node: node,
3945                                 span: span
3946                             }));
3947                         }
3948                     })
3949                 }
3950             }
3951         }
3952
3953         if !attributes_box.is_empty() {
3954             let last_span = self.last_span;
3955             self.span_err(last_span,
3956                           Parser::expected_item_err(attributes_box[]));
3957         }
3958
3959         let hi = self.span.hi;
3960         self.bump();
3961         P(ast::Block {
3962             view_items: view_items,
3963             stmts: stmts,
3964             expr: expr,
3965             id: ast::DUMMY_NODE_ID,
3966             rules: s,
3967             span: mk_sp(lo, hi),
3968         })
3969     }
3970
3971     fn handle_expression_like_statement(
3972             &mut self,
3973             e: P<Expr>,
3974             stmt_id: NodeId,
3975             span: Span,
3976             stmts: &mut Vec<P<Stmt>>,
3977             last_block_expr: &mut Option<P<Expr>>) {
3978         // expression without semicolon
3979         if classify::expr_requires_semi_to_be_stmt(&*e) {
3980             // Just check for errors and recover; do not eat semicolon yet.
3981             self.commit_stmt(&[],
3982                              &[token::Semi, token::CloseDelim(token::Brace)]);
3983         }
3984
3985         match self.token {
3986             token::Semi => {
3987                 self.bump();
3988                 let span_with_semi = Span {
3989                     lo: span.lo,
3990                     hi: self.last_span.hi,
3991                     expn_id: span.expn_id,
3992                 };
3993                 stmts.push(P(Spanned {
3994                     node: StmtSemi(e, stmt_id),
3995                     span: span_with_semi,
3996                 }));
3997             }
3998             token::CloseDelim(token::Brace) => *last_block_expr = Some(e),
3999             _ => {
4000                 stmts.push(P(Spanned {
4001                     node: StmtExpr(e, stmt_id),
4002                     span: span
4003                 }));
4004             }
4005         }
4006     }
4007
4008     // Parses a sequence of bounds if a `:` is found,
4009     // otherwise returns empty list.
4010     fn parse_colon_then_ty_param_bounds(&mut self,
4011                                         mode: BoundParsingMode)
4012                                         -> OwnedSlice<TyParamBound>
4013     {
4014         if !self.eat(&token::Colon) {
4015             OwnedSlice::empty()
4016         } else {
4017             self.parse_ty_param_bounds(mode)
4018         }
4019     }
4020
4021     // matches bounds    = ( boundseq )?
4022     // where   boundseq  = ( polybound + boundseq ) | polybound
4023     // and     polybound = ( 'for' '<' 'region '>' )? bound
4024     // and     bound     = 'region | trait_ref
4025     fn parse_ty_param_bounds(&mut self,
4026                              mode: BoundParsingMode)
4027                              -> OwnedSlice<TyParamBound>
4028     {
4029         let mut result = vec!();
4030         loop {
4031             let question_span = self.span;
4032             let ate_question = self.eat(&token::Question);
4033             match self.token {
4034                 token::Lifetime(lifetime) => {
4035                     if ate_question {
4036                         self.span_err(question_span,
4037                                       "`?` may only modify trait bounds, not lifetime bounds");
4038                     }
4039                     result.push(RegionTyParamBound(ast::Lifetime {
4040                         id: ast::DUMMY_NODE_ID,
4041                         span: self.span,
4042                         name: lifetime.name
4043                     }));
4044                     self.bump();
4045                 }
4046                 token::ModSep | token::Ident(..) => {
4047                     let poly_trait_ref = self.parse_poly_trait_ref();
4048                     let modifier = if ate_question {
4049                         if mode == BoundParsingMode::Modified {
4050                             TraitBoundModifier::Maybe
4051                         } else {
4052                             self.span_err(question_span,
4053                                           "unexpected `?`");
4054                             TraitBoundModifier::None
4055                         }
4056                     } else {
4057                         TraitBoundModifier::None
4058                     };
4059                     result.push(TraitTyParamBound(poly_trait_ref, modifier))
4060                 }
4061                 _ => break,
4062             }
4063
4064             if !self.eat(&token::BinOp(token::Plus)) {
4065                 break;
4066             }
4067         }
4068
4069         return OwnedSlice::from_vec(result);
4070     }
4071
4072     fn trait_ref_from_ident(ident: Ident, span: Span) -> TraitRef {
4073         let segment = ast::PathSegment {
4074             identifier: ident,
4075             parameters: ast::PathParameters::none()
4076         };
4077         let path = ast::Path {
4078             span: span,
4079             global: false,
4080             segments: vec![segment],
4081         };
4082         ast::TraitRef {
4083             path: path,
4084             ref_id: ast::DUMMY_NODE_ID,
4085         }
4086     }
4087
4088     /// Matches typaram = (unbound `?`)? IDENT (`?` unbound)? optbounds ( EQ ty )?
4089     fn parse_ty_param(&mut self) -> TyParam {
4090         // This is a bit hacky. Currently we are only interested in a single
4091         // unbound, and it may only be `Sized`. To avoid backtracking and other
4092         // complications, we parse an ident, then check for `?`. If we find it,
4093         // we use the ident as the unbound, otherwise, we use it as the name of
4094         // type param. Even worse, for now, we need to check for `?` before or
4095         // after the bound.
4096         let mut span = self.span;
4097         let mut ident = self.parse_ident();
4098         let mut unbound = None;
4099         if self.eat(&token::Question) {
4100             let tref = Parser::trait_ref_from_ident(ident, span);
4101             unbound = Some(tref);
4102             span = self.span;
4103             ident = self.parse_ident();
4104         }
4105
4106         let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Modified);
4107         if let Some(unbound) = unbound {
4108             let mut bounds_as_vec = bounds.into_vec();
4109             bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4110                                                                 trait_ref: unbound },
4111                                                  TraitBoundModifier::Maybe));
4112             bounds = OwnedSlice::from_vec(bounds_as_vec);
4113         };
4114
4115         let default = if self.check(&token::Eq) {
4116             self.bump();
4117             Some(self.parse_ty_sum())
4118         }
4119         else { None };
4120
4121         TyParam {
4122             ident: ident,
4123             id: ast::DUMMY_NODE_ID,
4124             bounds: bounds,
4125             default: default,
4126             span: span,
4127         }
4128     }
4129
4130     /// Parse a set of optional generic type parameter declarations. Where
4131     /// clauses are not parsed here, and must be added later via
4132     /// `parse_where_clause()`.
4133     ///
4134     /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4135     ///                  | ( < lifetimes , typaramseq ( , )? > )
4136     /// where   typaramseq = ( typaram ) | ( typaram , typaramseq )
4137     pub fn parse_generics(&mut self) -> ast::Generics {
4138         if self.eat(&token::Lt) {
4139             let lifetime_defs = self.parse_lifetime_defs();
4140             let mut seen_default = false;
4141             let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
4142                 p.forbid_lifetime();
4143                 let ty_param = p.parse_ty_param();
4144                 if ty_param.default.is_some() {
4145                     seen_default = true;
4146                 } else if seen_default {
4147                     let last_span = p.last_span;
4148                     p.span_err(last_span,
4149                                "type parameters with a default must be trailing");
4150                 }
4151                 ty_param
4152             });
4153             ast::Generics {
4154                 lifetimes: lifetime_defs,
4155                 ty_params: ty_params,
4156                 where_clause: WhereClause {
4157                     id: ast::DUMMY_NODE_ID,
4158                     predicates: Vec::new(),
4159                 }
4160             }
4161         } else {
4162             ast_util::empty_generics()
4163         }
4164     }
4165
4166     fn parse_generic_values_after_lt(&mut self)
4167                                      -> (Vec<ast::Lifetime>, Vec<P<Ty>>, Vec<P<TypeBinding>>) {
4168         let lifetimes = self.parse_lifetimes(token::Comma);
4169
4170         // First parse types.
4171         let (types, returned) = self.parse_seq_to_gt_or_return(
4172             Some(token::Comma),
4173             |p| {
4174                 p.forbid_lifetime();
4175                 if p.look_ahead(1, |t| t == &token::Eq) {
4176                     None
4177                 } else {
4178                     Some(p.parse_ty_sum())
4179                 }
4180             }
4181         );
4182
4183         // If we found the `>`, don't continue.
4184         if !returned {
4185             return (lifetimes, types.into_vec(), Vec::new());
4186         }
4187
4188         // Then parse type bindings.
4189         let bindings = self.parse_seq_to_gt(
4190             Some(token::Comma),
4191             |p| {
4192                 p.forbid_lifetime();
4193                 let lo = p.span.lo;
4194                 let ident = p.parse_ident();
4195                 let found_eq = p.eat(&token::Eq);
4196                 if !found_eq {
4197                     let span = p.span;
4198                     p.span_warn(span, "whoops, no =?");
4199                 }
4200                 let ty = p.parse_ty();
4201                 let hi = p.span.hi;
4202                 let span = mk_sp(lo, hi);
4203                 return P(TypeBinding{id: ast::DUMMY_NODE_ID,
4204                     ident: ident,
4205                     ty: ty,
4206                     span: span,
4207                 });
4208             }
4209         );
4210         (lifetimes, types.into_vec(), bindings.into_vec())
4211     }
4212
4213     fn forbid_lifetime(&mut self) {
4214         if self.token.is_lifetime() {
4215             let span = self.span;
4216             self.span_fatal(span, "lifetime parameters must be declared \
4217                                         prior to type parameters");
4218         }
4219     }
4220
4221     /// Parses an optional `where` clause and places it in `generics`.
4222     ///
4223     /// ```
4224     /// where T : Trait<U, V> + 'b, 'a : 'b
4225     /// ```
4226     fn parse_where_clause(&mut self, generics: &mut ast::Generics) {
4227         if !self.eat_keyword(keywords::Where) {
4228             return
4229         }
4230
4231         let mut parsed_something = false;
4232         loop {
4233             let lo = self.span.lo;
4234             match self.token {
4235                 token::OpenDelim(token::Brace) => {
4236                     break
4237                 }
4238
4239                 token::Lifetime(..) => {
4240                     let bounded_lifetime =
4241                         self.parse_lifetime();
4242
4243                     self.eat(&token::Colon);
4244
4245                     let bounds =
4246                         self.parse_lifetimes(token::BinOp(token::Plus));
4247
4248                     let hi = self.span.hi;
4249                     let span = mk_sp(lo, hi);
4250
4251                     generics.where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4252                         ast::WhereRegionPredicate {
4253                             span: span,
4254                             lifetime: bounded_lifetime,
4255                             bounds: bounds
4256                         }
4257                     ));
4258
4259                     parsed_something = true;
4260                 }
4261
4262                 _ => {
4263                     let bounded_ty = self.parse_ty();
4264
4265                     if self.eat(&token::Colon) {
4266                         let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
4267                         let hi = self.span.hi;
4268                         let span = mk_sp(lo, hi);
4269
4270                         if bounds.len() == 0 {
4271                             self.span_err(span,
4272                                           "each predicate in a `where` clause must have \
4273                                    at least one bound in it");
4274                         }
4275
4276                         generics.where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4277                                 ast::WhereBoundPredicate {
4278                                     span: span,
4279                                     bounded_ty: bounded_ty,
4280                                     bounds: bounds,
4281                         }));
4282
4283                         parsed_something = true;
4284                     } else if self.eat(&token::Eq) {
4285                         // let ty = self.parse_ty();
4286                         let hi = self.span.hi;
4287                         let span = mk_sp(lo, hi);
4288                         // generics.where_clause.predicates.push(
4289                         //     ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
4290                         //         id: ast::DUMMY_NODE_ID,
4291                         //         span: span,
4292                         //         path: panic!("NYI"), //bounded_ty,
4293                         //         ty: ty,
4294                         // }));
4295                         // parsed_something = true;
4296                         // // FIXME(#18433)
4297                         self.span_err(span,
4298                                      "equality constraints are not yet supported \
4299                                      in where clauses (#20041)");
4300                     } else {
4301                         let last_span = self.last_span;
4302                         self.span_err(last_span,
4303                               "unexpected token in `where` clause");
4304                     }
4305                 }
4306             };
4307
4308             if !self.eat(&token::Comma) {
4309                 break
4310             }
4311         }
4312
4313         if !parsed_something {
4314             let last_span = self.last_span;
4315             self.span_err(last_span,
4316                           "a `where` clause must have at least one predicate \
4317                            in it");
4318         }
4319     }
4320
4321     fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4322                      -> (Vec<Arg> , bool) {
4323         let sp = self.span;
4324         let mut args: Vec<Option<Arg>> =
4325             self.parse_unspanned_seq(
4326                 &token::OpenDelim(token::Paren),
4327                 &token::CloseDelim(token::Paren),
4328                 seq_sep_trailing_allowed(token::Comma),
4329                 |p| {
4330                     if p.token == token::DotDotDot {
4331                         p.bump();
4332                         if allow_variadic {
4333                             if p.token != token::CloseDelim(token::Paren) {
4334                                 let span = p.span;
4335                                 p.span_fatal(span,
4336                                     "`...` must be last in argument list for variadic function");
4337                             }
4338                         } else {
4339                             let span = p.span;
4340                             p.span_fatal(span,
4341                                          "only foreign functions are allowed to be variadic");
4342                         }
4343                         None
4344                     } else {
4345                         Some(p.parse_arg_general(named_args))
4346                     }
4347                 }
4348             );
4349
4350         let variadic = match args.pop() {
4351             Some(None) => true,
4352             Some(x) => {
4353                 // Need to put back that last arg
4354                 args.push(x);
4355                 false
4356             }
4357             None => false
4358         };
4359
4360         if variadic && args.is_empty() {
4361             self.span_err(sp,
4362                           "variadic function must be declared with at least one named argument");
4363         }
4364
4365         let args = args.into_iter().map(|x| x.unwrap()).collect();
4366
4367         (args, variadic)
4368     }
4369
4370     /// Parse the argument list and result type of a function declaration
4371     pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> P<FnDecl> {
4372
4373         let (args, variadic) = self.parse_fn_args(true, allow_variadic);
4374         let ret_ty = self.parse_ret_ty();
4375
4376         P(FnDecl {
4377             inputs: args,
4378             output: ret_ty,
4379             variadic: variadic
4380         })
4381     }
4382
4383     fn is_self_ident(&mut self) -> bool {
4384         match self.token {
4385           token::Ident(id, token::Plain) => id.name == special_idents::self_.name,
4386           _ => false
4387         }
4388     }
4389
4390     fn expect_self_ident(&mut self) -> ast::Ident {
4391         match self.token {
4392             token::Ident(id, token::Plain) if id.name == special_idents::self_.name => {
4393                 self.bump();
4394                 id
4395             },
4396             _ => {
4397                 let token_str = self.this_token_to_string();
4398                 self.fatal(format!("expected `self`, found `{}`",
4399                                    token_str)[])
4400             }
4401         }
4402     }
4403
4404     /// Parse the argument list and result type of a function
4405     /// that may have a self type.
4406     fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
4407         F: FnMut(&mut Parser) -> Arg,
4408     {
4409         fn maybe_parse_borrowed_explicit_self(this: &mut Parser)
4410                                               -> ast::ExplicitSelf_ {
4411             // The following things are possible to see here:
4412             //
4413             //     fn(&mut self)
4414             //     fn(&mut self)
4415             //     fn(&'lt self)
4416             //     fn(&'lt mut self)
4417             //
4418             // We already know that the current token is `&`.
4419
4420             if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4421                 this.bump();
4422                 SelfRegion(None, MutImmutable, this.expect_self_ident())
4423             } else if this.look_ahead(1, |t| t.is_mutability()) &&
4424                       this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4425                 this.bump();
4426                 let mutability = this.parse_mutability();
4427                 SelfRegion(None, mutability, this.expect_self_ident())
4428             } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4429                       this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4430                 this.bump();
4431                 let lifetime = this.parse_lifetime();
4432                 SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
4433             } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4434                       this.look_ahead(2, |t| t.is_mutability()) &&
4435                       this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
4436                 this.bump();
4437                 let lifetime = this.parse_lifetime();
4438                 let mutability = this.parse_mutability();
4439                 SelfRegion(Some(lifetime), mutability, this.expect_self_ident())
4440             } else {
4441                 SelfStatic
4442             }
4443         }
4444
4445         self.expect(&token::OpenDelim(token::Paren));
4446
4447         // A bit of complexity and lookahead is needed here in order to be
4448         // backwards compatible.
4449         let lo = self.span.lo;
4450         let mut self_ident_lo = self.span.lo;
4451         let mut self_ident_hi = self.span.hi;
4452
4453         let mut mutbl_self = MutImmutable;
4454         let explicit_self = match self.token {
4455             token::BinOp(token::And) => {
4456                 let eself = maybe_parse_borrowed_explicit_self(self);
4457                 self_ident_lo = self.last_span.lo;
4458                 self_ident_hi = self.last_span.hi;
4459                 eself
4460             }
4461             token::Tilde => {
4462                 // We need to make sure it isn't a type
4463                 if self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4464                     self.bump();
4465                     drop(self.expect_self_ident());
4466                     let last_span = self.last_span;
4467                     self.obsolete(last_span, ObsoleteOwnedSelf)
4468                 }
4469                 SelfStatic
4470             }
4471             token::BinOp(token::Star) => {
4472                 // Possibly "*self" or "*mut self" -- not supported. Try to avoid
4473                 // emitting cryptic "unexpected token" errors.
4474                 self.bump();
4475                 let _mutability = if self.token.is_mutability() {
4476                     self.parse_mutability()
4477                 } else {
4478                     MutImmutable
4479                 };
4480                 if self.is_self_ident() {
4481                     let span = self.span;
4482                     self.span_err(span, "cannot pass self by unsafe pointer");
4483                     self.bump();
4484                 }
4485                 // error case, making bogus self ident:
4486                 SelfValue(special_idents::self_)
4487             }
4488             token::Ident(..) => {
4489                 if self.is_self_ident() {
4490                     let self_ident = self.expect_self_ident();
4491
4492                     // Determine whether this is the fully explicit form, `self:
4493                     // TYPE`.
4494                     if self.eat(&token::Colon) {
4495                         SelfExplicit(self.parse_ty_sum(), self_ident)
4496                     } else {
4497                         SelfValue(self_ident)
4498                     }
4499                 } else if self.token.is_mutability() &&
4500                         self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4501                     mutbl_self = self.parse_mutability();
4502                     let self_ident = self.expect_self_ident();
4503
4504                     // Determine whether this is the fully explicit form,
4505                     // `self: TYPE`.
4506                     if self.eat(&token::Colon) {
4507                         SelfExplicit(self.parse_ty_sum(), self_ident)
4508                     } else {
4509                         SelfValue(self_ident)
4510                     }
4511                 } else if self.token.is_mutability() &&
4512                         self.look_ahead(1, |t| *t == token::Tilde) &&
4513                         self.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4514                     mutbl_self = self.parse_mutability();
4515                     self.bump();
4516                     drop(self.expect_self_ident());
4517                     let last_span = self.last_span;
4518                     self.obsolete(last_span, ObsoleteOwnedSelf);
4519                     SelfStatic
4520                 } else {
4521                     SelfStatic
4522                 }
4523             }
4524             _ => SelfStatic,
4525         };
4526
4527         let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
4528
4529         // shared fall-through for the three cases below. borrowing prevents simply
4530         // writing this as a closure
4531         macro_rules! parse_remaining_arguments {
4532             ($self_id:ident) =>
4533             {
4534             // If we parsed a self type, expect a comma before the argument list.
4535             match self.token {
4536                 token::Comma => {
4537                     self.bump();
4538                     let sep = seq_sep_trailing_allowed(token::Comma);
4539                     let mut fn_inputs = self.parse_seq_to_before_end(
4540                         &token::CloseDelim(token::Paren),
4541                         sep,
4542                         parse_arg_fn
4543                     );
4544                     fn_inputs.insert(0, Arg::new_self(explicit_self_sp, mutbl_self, $self_id));
4545                     fn_inputs
4546                 }
4547                 token::CloseDelim(token::Paren) => {
4548                     vec!(Arg::new_self(explicit_self_sp, mutbl_self, $self_id))
4549                 }
4550                 _ => {
4551                     let token_str = self.this_token_to_string();
4552                     self.fatal(format!("expected `,` or `)`, found `{}`",
4553                                        token_str)[])
4554                 }
4555             }
4556             }
4557         }
4558
4559         let fn_inputs = match explicit_self {
4560             SelfStatic =>  {
4561                 let sep = seq_sep_trailing_allowed(token::Comma);
4562                 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
4563             }
4564             SelfValue(id) => parse_remaining_arguments!(id),
4565             SelfRegion(_,_,id) => parse_remaining_arguments!(id),
4566             SelfExplicit(_,id) => parse_remaining_arguments!(id),
4567         };
4568
4569
4570         self.expect(&token::CloseDelim(token::Paren));
4571
4572         let hi = self.span.hi;
4573
4574         let ret_ty = self.parse_ret_ty();
4575
4576         let fn_decl = P(FnDecl {
4577             inputs: fn_inputs,
4578             output: ret_ty,
4579             variadic: false
4580         });
4581
4582         (spanned(lo, hi, explicit_self), fn_decl)
4583     }
4584
4585     // parse the |arg, arg| header on a lambda
4586     fn parse_fn_block_decl(&mut self)
4587                            -> (P<FnDecl>, Option<UnboxedClosureKind>) {
4588         let (optional_unboxed_closure_kind, inputs_captures) = {
4589             if self.eat(&token::OrOr) {
4590                 (None, Vec::new())
4591             } else {
4592                 self.expect(&token::BinOp(token::Or));
4593                 let optional_unboxed_closure_kind =
4594                     self.parse_optional_unboxed_closure_kind();
4595                 let args = self.parse_seq_to_before_end(
4596                     &token::BinOp(token::Or),
4597                     seq_sep_trailing_allowed(token::Comma),
4598                     |p| p.parse_fn_block_arg()
4599                 );
4600                 self.bump();
4601                 (optional_unboxed_closure_kind, args)
4602             }
4603         };
4604         let output = if self.check(&token::RArrow) {
4605             self.parse_ret_ty()
4606         } else {
4607             Return(P(Ty {
4608                 id: ast::DUMMY_NODE_ID,
4609                 node: TyInfer,
4610                 span: self.span,
4611             }))
4612         };
4613
4614         (P(FnDecl {
4615             inputs: inputs_captures,
4616             output: output,
4617             variadic: false
4618         }), optional_unboxed_closure_kind)
4619     }
4620
4621     /// Parses the `(arg, arg) -> return_type` header on a procedure.
4622     fn parse_proc_decl(&mut self) -> P<FnDecl> {
4623         let inputs =
4624             self.parse_unspanned_seq(&token::OpenDelim(token::Paren),
4625                                      &token::CloseDelim(token::Paren),
4626                                      seq_sep_trailing_allowed(token::Comma),
4627                                      |p| p.parse_fn_block_arg());
4628
4629         let output = if self.check(&token::RArrow) {
4630             self.parse_ret_ty()
4631         } else {
4632             Return(P(Ty {
4633                 id: ast::DUMMY_NODE_ID,
4634                 node: TyInfer,
4635                 span: self.span,
4636             }))
4637         };
4638
4639         P(FnDecl {
4640             inputs: inputs,
4641             output: output,
4642             variadic: false
4643         })
4644     }
4645
4646     /// Parse the name and optional generic types of a function header.
4647     fn parse_fn_header(&mut self) -> (Ident, ast::Generics) {
4648         let id = self.parse_ident();
4649         let generics = self.parse_generics();
4650         (id, generics)
4651     }
4652
4653     fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
4654                node: Item_, vis: Visibility,
4655                attrs: Vec<Attribute>) -> P<Item> {
4656         P(Item {
4657             ident: ident,
4658             attrs: attrs,
4659             id: ast::DUMMY_NODE_ID,
4660             node: node,
4661             vis: vis,
4662             span: mk_sp(lo, hi)
4663         })
4664     }
4665
4666     /// Parse an item-position function declaration.
4667     fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> ItemInfo {
4668         let (ident, mut generics) = self.parse_fn_header();
4669         let decl = self.parse_fn_decl(false);
4670         self.parse_where_clause(&mut generics);
4671         let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4672         (ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))
4673     }
4674
4675     /// Parse a method in a trait impl
4676     pub fn parse_method_with_outer_attributes(&mut self) -> P<Method> {
4677         let attrs = self.parse_outer_attributes();
4678         let visa = self.parse_visibility();
4679         self.parse_method(attrs, visa)
4680     }
4681
4682     /// Parse a method in a trait impl, starting with `attrs` attributes.
4683     pub fn parse_method(&mut self,
4684                         attrs: Vec<Attribute>,
4685                         visa: Visibility)
4686                         -> P<Method> {
4687         let lo = self.span.lo;
4688
4689         // code copied from parse_macro_use_or_failure... abstraction!
4690         let (method_, hi, new_attrs) = {
4691             if !self.token.is_any_keyword()
4692                 && self.look_ahead(1, |t| *t == token::Not)
4693                 && (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
4694                     || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
4695                 // method macro.
4696                 let pth = self.parse_path(NoTypesAllowed);
4697                 self.expect(&token::Not);
4698
4699                 // eat a matched-delimiter token tree:
4700                 let delim = self.expect_open_delim();
4701                 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
4702                                                 seq_sep_none(),
4703                                                 |p| p.parse_token_tree());
4704                 let m_ = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
4705                 let m: ast::Mac = codemap::Spanned { node: m_,
4706                                                  span: mk_sp(self.span.lo,
4707                                                              self.span.hi) };
4708                 if delim != token::Brace {
4709                     self.expect(&token::Semi)
4710                 }
4711                 (ast::MethMac(m), self.span.hi, attrs)
4712             } else {
4713                 let unsafety = self.parse_unsafety();
4714                 let abi = if self.eat_keyword(keywords::Extern) {
4715                     self.parse_opt_abi().unwrap_or(abi::C)
4716                 } else {
4717                     abi::Rust
4718                 };
4719                 self.expect_keyword(keywords::Fn);
4720                 let ident = self.parse_ident();
4721                 let mut generics = self.parse_generics();
4722                 let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
4723                         p.parse_arg()
4724                     });
4725                 self.parse_where_clause(&mut generics);
4726                 let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4727                 let body_span = body.span;
4728                 let mut new_attrs = attrs;
4729                 new_attrs.push_all(inner_attrs[]);
4730                 (ast::MethDecl(ident,
4731                                generics,
4732                                abi,
4733                                explicit_self,
4734                                unsafety,
4735                                decl,
4736                                body,
4737                                visa),
4738                  body_span.hi, new_attrs)
4739             }
4740         };
4741         P(ast::Method {
4742             attrs: new_attrs,
4743             id: ast::DUMMY_NODE_ID,
4744             span: mk_sp(lo, hi),
4745             node: method_,
4746         })
4747     }
4748
4749     /// Parse trait Foo { ... }
4750     fn parse_item_trait(&mut self, unsafety: Unsafety) -> ItemInfo {
4751         let ident = self.parse_ident();
4752         let mut tps = self.parse_generics();
4753         let unbound = self.parse_for_sized();
4754
4755         // Parse supertrait bounds.
4756         let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
4757
4758         if let Some(unbound) = unbound {
4759             let mut bounds_as_vec = bounds.into_vec();
4760             bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4761                                                                 trait_ref: unbound },
4762                                                  TraitBoundModifier::Maybe));
4763             bounds = OwnedSlice::from_vec(bounds_as_vec);
4764         };
4765
4766         self.parse_where_clause(&mut tps);
4767
4768         let meths = self.parse_trait_items();
4769         (ident, ItemTrait(unsafety, tps, bounds, meths), None)
4770     }
4771
4772     fn parse_impl_items(&mut self) -> (Vec<ImplItem>, Vec<Attribute>) {
4773         let mut impl_items = Vec::new();
4774         self.expect(&token::OpenDelim(token::Brace));
4775         let (inner_attrs, mut method_attrs) =
4776             self.parse_inner_attrs_and_next();
4777         while !self.eat(&token::CloseDelim(token::Brace)) {
4778             method_attrs.extend(self.parse_outer_attributes().into_iter());
4779             let vis = self.parse_visibility();
4780             if self.eat_keyword(keywords::Type) {
4781                 impl_items.push(TypeImplItem(P(self.parse_typedef(
4782                             method_attrs,
4783                             vis))))
4784             } else {
4785                 impl_items.push(MethodImplItem(self.parse_method(
4786                             method_attrs,
4787                             vis)));
4788             }
4789             method_attrs = self.parse_outer_attributes();
4790         }
4791         (impl_items, inner_attrs)
4792     }
4793
4794     /// Parses two variants (with the region/type params always optional):
4795     ///    impl<T> Foo { ... }
4796     ///    impl<T> ToString for ~[T] { ... }
4797     fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> ItemInfo {
4798         // First, parse type parameters if necessary.
4799         let mut generics = self.parse_generics();
4800
4801         // Special case: if the next identifier that follows is '(', don't
4802         // allow this to be parsed as a trait.
4803         let could_be_trait = self.token != token::OpenDelim(token::Paren);
4804
4805         // Parse the trait.
4806         let mut ty = self.parse_ty_sum();
4807
4808         // Parse traits, if necessary.
4809         let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
4810             // New-style trait. Reinterpret the type as a trait.
4811             let opt_trait_ref = match ty.node {
4812                 TyPath(ref path, node_id) => {
4813                     Some(TraitRef {
4814                         path: (*path).clone(),
4815                         ref_id: node_id,
4816                     })
4817                 }
4818                 _ => {
4819                     self.span_err(ty.span, "not a trait");
4820                     None
4821                 }
4822             };
4823
4824             ty = self.parse_ty_sum();
4825             opt_trait_ref
4826         } else {
4827             None
4828         };
4829
4830         self.parse_where_clause(&mut generics);
4831         let (impl_items, attrs) = self.parse_impl_items();
4832
4833         let ident = ast_util::impl_pretty_name(&opt_trait, &*ty);
4834
4835         (ident,
4836          ItemImpl(unsafety, generics, opt_trait, ty, impl_items),
4837          Some(attrs))
4838     }
4839
4840     /// Parse a::B<String,int>
4841     fn parse_trait_ref(&mut self) -> TraitRef {
4842         ast::TraitRef {
4843             path: self.parse_path(LifetimeAndTypesWithoutColons),
4844             ref_id: ast::DUMMY_NODE_ID,
4845         }
4846     }
4847
4848     fn parse_late_bound_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
4849         if self.eat_keyword(keywords::For) {
4850             self.expect(&token::Lt);
4851             let lifetime_defs = self.parse_lifetime_defs();
4852             self.expect_gt();
4853             lifetime_defs
4854         } else {
4855             Vec::new()
4856         }
4857     }
4858
4859     /// Parse for<'l> a::B<String,int>
4860     fn parse_poly_trait_ref(&mut self) -> PolyTraitRef {
4861         let lifetime_defs = self.parse_late_bound_lifetime_defs();
4862
4863         ast::PolyTraitRef {
4864             bound_lifetimes: lifetime_defs,
4865             trait_ref: self.parse_trait_ref()
4866         }
4867     }
4868
4869     /// Parse struct Foo { ... }
4870     fn parse_item_struct(&mut self) -> ItemInfo {
4871         let class_name = self.parse_ident();
4872         let mut generics = self.parse_generics();
4873
4874         if self.eat(&token::Colon) {
4875             let ty = self.parse_ty_sum();
4876             self.span_err(ty.span, "`virtual` structs have been removed from the language");
4877         }
4878
4879         self.parse_where_clause(&mut generics);
4880
4881         let mut fields: Vec<StructField>;
4882         let is_tuple_like;
4883
4884         if self.eat(&token::OpenDelim(token::Brace)) {
4885             // It's a record-like struct.
4886             is_tuple_like = false;
4887             fields = Vec::new();
4888             while self.token != token::CloseDelim(token::Brace) {
4889                 fields.push(self.parse_struct_decl_field(true));
4890             }
4891             if fields.len() == 0 {
4892                 self.fatal(format!("unit-like struct definition should be \
4893                                     written as `struct {};`",
4894                                    token::get_ident(class_name))[]);
4895             }
4896             self.bump();
4897         } else if self.check(&token::OpenDelim(token::Paren)) {
4898             // It's a tuple-like struct.
4899             is_tuple_like = true;
4900             fields = self.parse_unspanned_seq(
4901                 &token::OpenDelim(token::Paren),
4902                 &token::CloseDelim(token::Paren),
4903                 seq_sep_trailing_allowed(token::Comma),
4904                 |p| {
4905                 let attrs = p.parse_outer_attributes();
4906                 let lo = p.span.lo;
4907                 let struct_field_ = ast::StructField_ {
4908                     kind: UnnamedField(p.parse_visibility()),
4909                     id: ast::DUMMY_NODE_ID,
4910                     ty: p.parse_ty_sum(),
4911                     attrs: attrs,
4912                 };
4913                 spanned(lo, p.span.hi, struct_field_)
4914             });
4915             if fields.len() == 0 {
4916                 self.fatal(format!("unit-like struct definition should be \
4917                                     written as `struct {};`",
4918                                    token::get_ident(class_name))[]);
4919             }
4920             self.expect(&token::Semi);
4921         } else if self.eat(&token::Semi) {
4922             // It's a unit-like struct.
4923             is_tuple_like = true;
4924             fields = Vec::new();
4925         } else {
4926             let token_str = self.this_token_to_string();
4927             self.fatal(format!("expected `{}`, `(`, or `;` after struct \
4928                                 name, found `{}`", "{",
4929                                token_str)[])
4930         }
4931
4932         let _ = ast::DUMMY_NODE_ID;  // FIXME: Workaround for crazy bug.
4933         let new_id = ast::DUMMY_NODE_ID;
4934         (class_name,
4935          ItemStruct(P(ast::StructDef {
4936              fields: fields,
4937              ctor_id: if is_tuple_like { Some(new_id) } else { None },
4938          }), generics),
4939          None)
4940     }
4941
4942     /// Parse a structure field declaration
4943     pub fn parse_single_struct_field(&mut self,
4944                                      vis: Visibility,
4945                                      attrs: Vec<Attribute> )
4946                                      -> StructField {
4947         let a_var = self.parse_name_and_ty(vis, attrs);
4948         match self.token {
4949             token::Comma => {
4950                 self.bump();
4951             }
4952             token::CloseDelim(token::Brace) => {}
4953             _ => {
4954                 let span = self.span;
4955                 let token_str = self.this_token_to_string();
4956                 self.span_fatal_help(span,
4957                                      format!("expected `,`, or `}}`, found `{}`",
4958                                              token_str)[],
4959                                      "struct fields should be separated by commas")
4960             }
4961         }
4962         a_var
4963     }
4964
4965     /// Parse an element of a struct definition
4966     fn parse_struct_decl_field(&mut self, allow_pub: bool) -> StructField {
4967
4968         let attrs = self.parse_outer_attributes();
4969
4970         if self.eat_keyword(keywords::Pub) {
4971             if !allow_pub {
4972                 let span = self.last_span;
4973                 self.span_err(span, "`pub` is not allowed here");
4974             }
4975             return self.parse_single_struct_field(Public, attrs);
4976         }
4977
4978         return self.parse_single_struct_field(Inherited, attrs);
4979     }
4980
4981     /// Parse visibility: PUB, PRIV, or nothing
4982     fn parse_visibility(&mut self) -> Visibility {
4983         if self.eat_keyword(keywords::Pub) { Public }
4984         else { Inherited }
4985     }
4986
4987     fn parse_for_sized(&mut self) -> Option<ast::TraitRef> {
4988         // FIXME, this should really use TraitBoundModifier, but it will get
4989         // re-jigged shortly in any case, so leaving the hacky version for now.
4990         if self.eat_keyword(keywords::For) {
4991             let span = self.span;
4992             let mut ate_question = false;
4993             if self.eat(&token::Question) {
4994                 ate_question = true;
4995             }
4996             let ident = self.parse_ident();
4997             if self.eat(&token::Question) {
4998                 if ate_question {
4999                     self.span_err(span,
5000                         "unexpected `?`");
5001                 }
5002                 ate_question = true;
5003             }
5004             if !ate_question {
5005                 self.span_err(span,
5006                     "expected `?Sized` after `for` in trait item");
5007                 return None;
5008             }
5009             let tref = Parser::trait_ref_from_ident(ident, span);
5010             Some(tref)
5011         } else {
5012             None
5013         }
5014     }
5015
5016     /// Given a termination token and a vector of already-parsed
5017     /// attributes (of length 0 or 1), parse all of the items in a module
5018     fn parse_mod_items(&mut self,
5019                        term: token::Token,
5020                        first_item_attrs: Vec<Attribute>,
5021                        inner_lo: BytePos)
5022                        -> Mod {
5023         // parse all of the items up to closing or an attribute.
5024         // view items are legal here.
5025         let ParsedItemsAndViewItems {
5026             attrs_remaining,
5027             view_items,
5028             items: starting_items,
5029             ..
5030         } = self.parse_items_and_view_items(first_item_attrs, true, true);
5031         let mut items: Vec<P<Item>> = starting_items;
5032         let attrs_remaining_len = attrs_remaining.len();
5033
5034         // don't think this other loop is even necessary....
5035
5036         let mut first = true;
5037         while self.token != term {
5038             let mut attrs = self.parse_outer_attributes();
5039             if first {
5040                 let mut tmp = attrs_remaining.clone();
5041                 tmp.push_all(attrs[]);
5042                 attrs = tmp;
5043                 first = false;
5044             }
5045             debug!("parse_mod_items: parse_item_or_view_item(attrs={})",
5046                    attrs);
5047             match self.parse_item_or_view_item(attrs,
5048                                                true /* macros allowed */) {
5049               IoviItem(item) => items.push(item),
5050               IoviViewItem(view_item) => {
5051                 self.span_fatal(view_item.span,
5052                                 "view items must be declared at the top of \
5053                                  the module");
5054               }
5055               _ => {
5056                   let token_str = self.this_token_to_string();
5057                   self.fatal(format!("expected item, found `{}`",
5058                                      token_str)[])
5059               }
5060             }
5061         }
5062
5063         if first && attrs_remaining_len > 0u {
5064             // We parsed attributes for the first item but didn't find it
5065             let last_span = self.last_span;
5066             self.span_err(last_span,
5067                           Parser::expected_item_err(attrs_remaining[]));
5068         }
5069
5070         ast::Mod {
5071             inner: mk_sp(inner_lo, self.span.lo),
5072             view_items: view_items,
5073             items: items
5074         }
5075     }
5076
5077     fn parse_item_const(&mut self, m: Option<Mutability>) -> ItemInfo {
5078         let id = self.parse_ident();
5079         self.expect(&token::Colon);
5080         let ty = self.parse_ty_sum();
5081         self.expect(&token::Eq);
5082         let e = self.parse_expr();
5083         self.commit_expr_expecting(&*e, token::Semi);
5084         let item = match m {
5085             Some(m) => ItemStatic(ty, m, e),
5086             None => ItemConst(ty, e),
5087         };
5088         (id, item, None)
5089     }
5090
5091     /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5092     fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> ItemInfo {
5093         let id_span = self.span;
5094         let id = self.parse_ident();
5095         if self.check(&token::Semi) {
5096             self.bump();
5097             // This mod is in an external file. Let's go get it!
5098             let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
5099             (id, m, Some(attrs))
5100         } else {
5101             self.push_mod_path(id, outer_attrs);
5102             self.expect(&token::OpenDelim(token::Brace));
5103             let mod_inner_lo = self.span.lo;
5104             let old_owns_directory = self.owns_directory;
5105             self.owns_directory = true;
5106             let (inner, next) = self.parse_inner_attrs_and_next();
5107             let m = self.parse_mod_items(token::CloseDelim(token::Brace), next, mod_inner_lo);
5108             self.expect(&token::CloseDelim(token::Brace));
5109             self.owns_directory = old_owns_directory;
5110             self.pop_mod_path();
5111             (id, ItemMod(m), Some(inner))
5112         }
5113     }
5114
5115     fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
5116         let default_path = self.id_to_interned_str(id);
5117         let file_path = match ::attr::first_attr_value_str_by_name(attrs,
5118                                                                    "path") {
5119             Some(d) => d,
5120             None => default_path,
5121         };
5122         self.mod_path_stack.push(file_path)
5123     }
5124
5125     fn pop_mod_path(&mut self) {
5126         self.mod_path_stack.pop().unwrap();
5127     }
5128
5129     /// Read a module from a source file.
5130     fn eval_src_mod(&mut self,
5131                     id: ast::Ident,
5132                     outer_attrs: &[ast::Attribute],
5133                     id_sp: Span)
5134                     -> (ast::Item_, Vec<ast::Attribute> ) {
5135         let mut prefix = Path::new(self.sess.span_diagnostic.cm.span_to_filename(self.span));
5136         prefix.pop();
5137         let mod_path = Path::new(".").join_many(self.mod_path_stack[]);
5138         let dir_path = prefix.join(&mod_path);
5139         let mod_string = token::get_ident(id);
5140         let (file_path, owns_directory) = match ::attr::first_attr_value_str_by_name(
5141                 outer_attrs, "path") {
5142             Some(d) => (dir_path.join(d), true),
5143             None => {
5144                 let mod_name = mod_string.get().to_string();
5145                 let default_path_str = format!("{}.rs", mod_name);
5146                 let secondary_path_str = format!("{}/mod.rs", mod_name);
5147                 let default_path = dir_path.join(default_path_str[]);
5148                 let secondary_path = dir_path.join(secondary_path_str[]);
5149                 let default_exists = default_path.exists();
5150                 let secondary_exists = secondary_path.exists();
5151
5152                 if !self.owns_directory {
5153                     self.span_err(id_sp,
5154                                   "cannot declare a new module at this location");
5155                     let this_module = match self.mod_path_stack.last() {
5156                         Some(name) => name.get().to_string(),
5157                         None => self.root_module_name.as_ref().unwrap().clone(),
5158                     };
5159                     self.span_note(id_sp,
5160                                    format!("maybe move this module `{0}` \
5161                                             to its own directory via \
5162                                             `{0}/mod.rs`",
5163                                            this_module)[]);
5164                     if default_exists || secondary_exists {
5165                         self.span_note(id_sp,
5166                                        format!("... or maybe `use` the module \
5167                                                 `{}` instead of possibly \
5168                                                 redeclaring it",
5169                                                mod_name)[]);
5170                     }
5171                     self.abort_if_errors();
5172                 }
5173
5174                 match (default_exists, secondary_exists) {
5175                     (true, false) => (default_path, false),
5176                     (false, true) => (secondary_path, true),
5177                     (false, false) => {
5178                         self.span_fatal_help(id_sp,
5179                                              format!("file not found for module `{}`",
5180                                                      mod_name)[],
5181                                              format!("name the file either {} or {} inside \
5182                                                      the directory {}",
5183                                                      default_path_str,
5184                                                      secondary_path_str,
5185                                                      dir_path.display())[]);
5186                     }
5187                     (true, true) => {
5188                         self.span_fatal_help(
5189                             id_sp,
5190                             format!("file for module `{}` found at both {} \
5191                                      and {}",
5192                                     mod_name,
5193                                     default_path_str,
5194                                     secondary_path_str)[],
5195                             "delete or rename one of them to remove the ambiguity");
5196                     }
5197                 }
5198             }
5199         };
5200
5201         self.eval_src_mod_from_path(file_path, owns_directory,
5202                                     mod_string.get().to_string(), id_sp)
5203     }
5204
5205     fn eval_src_mod_from_path(&mut self,
5206                               path: Path,
5207                               owns_directory: bool,
5208                               name: String,
5209                               id_sp: Span) -> (ast::Item_, Vec<ast::Attribute> ) {
5210         let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5211         match included_mod_stack.iter().position(|p| *p == path) {
5212             Some(i) => {
5213                 let mut err = String::from_str("circular modules: ");
5214                 let len = included_mod_stack.len();
5215                 for p in included_mod_stack.slice(i, len).iter() {
5216                     err.push_str(p.display().as_cow()[]);
5217                     err.push_str(" -> ");
5218                 }
5219                 err.push_str(path.display().as_cow()[]);
5220                 self.span_fatal(id_sp, err[]);
5221             }
5222             None => ()
5223         }
5224         included_mod_stack.push(path.clone());
5225         drop(included_mod_stack);
5226
5227         let mut p0 =
5228             new_sub_parser_from_file(self.sess,
5229                                      self.cfg.clone(),
5230                                      &path,
5231                                      owns_directory,
5232                                      Some(name),
5233                                      id_sp);
5234         let mod_inner_lo = p0.span.lo;
5235         let (mod_attrs, next) = p0.parse_inner_attrs_and_next();
5236         let first_item_outer_attrs = next;
5237         let m0 = p0.parse_mod_items(token::Eof, first_item_outer_attrs, mod_inner_lo);
5238         self.sess.included_mod_stack.borrow_mut().pop();
5239         return (ast::ItemMod(m0), mod_attrs);
5240     }
5241
5242     /// Parse a function declaration from a foreign module
5243     fn parse_item_foreign_fn(&mut self, vis: ast::Visibility,
5244                              attrs: Vec<Attribute>) -> P<ForeignItem> {
5245         let lo = self.span.lo;
5246         self.expect_keyword(keywords::Fn);
5247
5248         let (ident, mut generics) = self.parse_fn_header();
5249         let decl = self.parse_fn_decl(true);
5250         self.parse_where_clause(&mut generics);
5251         let hi = self.span.hi;
5252         self.expect(&token::Semi);
5253         P(ast::ForeignItem {
5254             ident: ident,
5255             attrs: attrs,
5256             node: ForeignItemFn(decl, generics),
5257             id: ast::DUMMY_NODE_ID,
5258             span: mk_sp(lo, hi),
5259             vis: vis
5260         })
5261     }
5262
5263     /// Parse a static item from a foreign module
5264     fn parse_item_foreign_static(&mut self, vis: ast::Visibility,
5265                                  attrs: Vec<Attribute>) -> P<ForeignItem> {
5266         let lo = self.span.lo;
5267
5268         self.expect_keyword(keywords::Static);
5269         let mutbl = self.eat_keyword(keywords::Mut);
5270
5271         let ident = self.parse_ident();
5272         self.expect(&token::Colon);
5273         let ty = self.parse_ty_sum();
5274         let hi = self.span.hi;
5275         self.expect(&token::Semi);
5276         P(ForeignItem {
5277             ident: ident,
5278             attrs: attrs,
5279             node: ForeignItemStatic(ty, mutbl),
5280             id: ast::DUMMY_NODE_ID,
5281             span: mk_sp(lo, hi),
5282             vis: vis
5283         })
5284     }
5285
5286     /// At this point, this is essentially a wrapper for
5287     /// parse_foreign_items.
5288     fn parse_foreign_mod_items(&mut self,
5289                                abi: abi::Abi,
5290                                first_item_attrs: Vec<Attribute> )
5291                                -> ForeignMod {
5292         let ParsedItemsAndViewItems {
5293             attrs_remaining,
5294             view_items,
5295             items: _,
5296             foreign_items,
5297         } = self.parse_foreign_items(first_item_attrs, true);
5298         if !attrs_remaining.is_empty() {
5299             let last_span = self.last_span;
5300             self.span_err(last_span,
5301                           Parser::expected_item_err(attrs_remaining[]));
5302         }
5303         assert!(self.token == token::CloseDelim(token::Brace));
5304         ast::ForeignMod {
5305             abi: abi,
5306             view_items: view_items,
5307             items: foreign_items
5308         }
5309     }
5310
5311     /// Parse extern crate links
5312     ///
5313     /// # Example
5314     ///
5315     /// extern crate url;
5316     /// extern crate foo = "bar"; //deprecated
5317     /// extern crate "bar" as foo;
5318     fn parse_item_extern_crate(&mut self,
5319                                 lo: BytePos,
5320                                 visibility: Visibility,
5321                                 attrs: Vec<Attribute> )
5322                                 -> ItemOrViewItem {
5323
5324         let span = self.span;
5325         let (maybe_path, ident) = match self.token {
5326             token::Ident(..) => {
5327                 let the_ident = self.parse_ident();
5328                 let path = if self.token == token::Eq {
5329                     self.bump();
5330                     let path = self.parse_str();
5331                     let span = self.span;
5332                     self.obsolete(span, ObsoleteExternCrateRenaming);
5333                     Some(path)
5334                 } else if self.eat_keyword(keywords::As) {
5335                     // skip the ident if there is one
5336                     if self.token.is_ident() { self.bump(); }
5337
5338                     self.span_err(span, "expected `;`, found `as`");
5339                     self.span_help(span,
5340                                    format!("perhaps you meant to enclose the crate name `{}` in \
5341                                            a string?",
5342                                           the_ident.as_str())[]);
5343                     None
5344                 } else {
5345                     None
5346                 };
5347                 self.expect(&token::Semi);
5348                 (path, the_ident)
5349             },
5350             token::Literal(token::Str_(..), suf) | token::Literal(token::StrRaw(..), suf) => {
5351                 let sp = self.span;
5352                 self.expect_no_suffix(sp, "extern crate name", suf);
5353                 // forgo the internal suffix check of `parse_str` to
5354                 // avoid repeats (this unwrap will always succeed due
5355                 // to the restriction of the `match`)
5356                 let (s, style, _) = self.parse_optional_str().unwrap();
5357                 self.expect_keyword(keywords::As);
5358                 let the_ident = self.parse_ident();
5359                 self.expect(&token::Semi);
5360                 (Some((s, style)), the_ident)
5361             },
5362             _ => {
5363                 let span = self.span;
5364                 let token_str = self.this_token_to_string();
5365                 self.span_fatal(span,
5366                                 format!("expected extern crate name but \
5367                                          found `{}`",
5368                                         token_str)[]);
5369             }
5370         };
5371
5372         IoviViewItem(ast::ViewItem {
5373                 node: ViewItemExternCrate(ident, maybe_path, ast::DUMMY_NODE_ID),
5374                 attrs: attrs,
5375                 vis: visibility,
5376                 span: mk_sp(lo, self.last_span.hi)
5377             })
5378     }
5379
5380     /// Parse `extern` for foreign ABIs
5381     /// modules.
5382     ///
5383     /// `extern` is expected to have been
5384     /// consumed before calling this method
5385     ///
5386     /// # Examples:
5387     ///
5388     /// extern "C" {}
5389     /// extern {}
5390     fn parse_item_foreign_mod(&mut self,
5391                               lo: BytePos,
5392                               opt_abi: Option<abi::Abi>,
5393                               visibility: Visibility,
5394                               attrs: Vec<Attribute> )
5395                               -> ItemOrViewItem {
5396
5397         self.expect(&token::OpenDelim(token::Brace));
5398
5399         let abi = opt_abi.unwrap_or(abi::C);
5400
5401         let (inner, next) = self.parse_inner_attrs_and_next();
5402         let m = self.parse_foreign_mod_items(abi, next);
5403         self.expect(&token::CloseDelim(token::Brace));
5404
5405         let last_span = self.last_span;
5406         let item = self.mk_item(lo,
5407                                 last_span.hi,
5408                                 special_idents::invalid,
5409                                 ItemForeignMod(m),
5410                                 visibility,
5411                                 maybe_append(attrs, Some(inner)));
5412         return IoviItem(item);
5413     }
5414
5415     /// Parse type Foo = Bar;
5416     fn parse_item_type(&mut self) -> ItemInfo {
5417         let ident = self.parse_ident();
5418         let mut tps = self.parse_generics();
5419         self.parse_where_clause(&mut tps);
5420         self.expect(&token::Eq);
5421         let ty = self.parse_ty_sum();
5422         self.expect(&token::Semi);
5423         (ident, ItemTy(ty, tps), None)
5424     }
5425
5426     /// Parse a structure-like enum variant definition
5427     /// this should probably be renamed or refactored...
5428     fn parse_struct_def(&mut self) -> P<StructDef> {
5429         let mut fields: Vec<StructField> = Vec::new();
5430         while self.token != token::CloseDelim(token::Brace) {
5431             fields.push(self.parse_struct_decl_field(false));
5432         }
5433         self.bump();
5434
5435         P(StructDef {
5436             fields: fields,
5437             ctor_id: None,
5438         })
5439     }
5440
5441     /// Parse the part of an "enum" decl following the '{'
5442     fn parse_enum_def(&mut self, _generics: &ast::Generics) -> EnumDef {
5443         let mut variants = Vec::new();
5444         let mut all_nullary = true;
5445         let mut any_disr = None;
5446         while self.token != token::CloseDelim(token::Brace) {
5447             let variant_attrs = self.parse_outer_attributes();
5448             let vlo = self.span.lo;
5449
5450             let vis = self.parse_visibility();
5451
5452             let ident;
5453             let kind;
5454             let mut args = Vec::new();
5455             let mut disr_expr = None;
5456             ident = self.parse_ident();
5457             if self.eat(&token::OpenDelim(token::Brace)) {
5458                 // Parse a struct variant.
5459                 all_nullary = false;
5460                 let start_span = self.span;
5461                 let struct_def = self.parse_struct_def();
5462                 if struct_def.fields.len() == 0 {
5463                     self.span_err(start_span,
5464                         format!("unit-like struct variant should be written \
5465                                  without braces, as `{},`",
5466                                 token::get_ident(ident))[]);
5467                 }
5468                 kind = StructVariantKind(struct_def);
5469             } else if self.check(&token::OpenDelim(token::Paren)) {
5470                 all_nullary = false;
5471                 let arg_tys = self.parse_enum_variant_seq(
5472                     &token::OpenDelim(token::Paren),
5473                     &token::CloseDelim(token::Paren),
5474                     seq_sep_trailing_allowed(token::Comma),
5475                     |p| p.parse_ty_sum()
5476                 );
5477                 for ty in arg_tys.into_iter() {
5478                     args.push(ast::VariantArg {
5479                         ty: ty,
5480                         id: ast::DUMMY_NODE_ID,
5481                     });
5482                 }
5483                 kind = TupleVariantKind(args);
5484             } else if self.eat(&token::Eq) {
5485                 disr_expr = Some(self.parse_expr());
5486                 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5487                 kind = TupleVariantKind(args);
5488             } else {
5489                 kind = TupleVariantKind(Vec::new());
5490             }
5491
5492             let vr = ast::Variant_ {
5493                 name: ident,
5494                 attrs: variant_attrs,
5495                 kind: kind,
5496                 id: ast::DUMMY_NODE_ID,
5497                 disr_expr: disr_expr,
5498                 vis: vis,
5499             };
5500             variants.push(P(spanned(vlo, self.last_span.hi, vr)));
5501
5502             if !self.eat(&token::Comma) { break; }
5503         }
5504         self.expect(&token::CloseDelim(token::Brace));
5505         match any_disr {
5506             Some(disr_span) if !all_nullary =>
5507                 self.span_err(disr_span,
5508                     "discriminator values can only be used with a c-like enum"),
5509             _ => ()
5510         }
5511
5512         ast::EnumDef { variants: variants }
5513     }
5514
5515     /// Parse an "enum" declaration
5516     fn parse_item_enum(&mut self) -> ItemInfo {
5517         let id = self.parse_ident();
5518         let mut generics = self.parse_generics();
5519         self.parse_where_clause(&mut generics);
5520         self.expect(&token::OpenDelim(token::Brace));
5521
5522         let enum_definition = self.parse_enum_def(&generics);
5523         (id, ItemEnum(enum_definition, generics), None)
5524     }
5525
5526     fn fn_expr_lookahead(tok: &token::Token) -> bool {
5527         match *tok {
5528           token::OpenDelim(token::Paren) | token::At | token::Tilde | token::BinOp(_) => true,
5529           _ => false
5530         }
5531     }
5532
5533     /// Parses a string as an ABI spec on an extern type or module. Consumes
5534     /// the `extern` keyword, if one is found.
5535     fn parse_opt_abi(&mut self) -> Option<abi::Abi> {
5536         match self.token {
5537             token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5538                 let sp = self.span;
5539                 self.expect_no_suffix(sp, "ABI spec", suf);
5540                 self.bump();
5541                 let the_string = s.as_str();
5542                 match abi::lookup(the_string) {
5543                     Some(abi) => Some(abi),
5544                     None => {
5545                         let last_span = self.last_span;
5546                         self.span_err(
5547                             last_span,
5548                             format!("illegal ABI: expected one of [{}], \
5549                                      found `{}`",
5550                                     abi::all_names().connect(", "),
5551                                     the_string)[]);
5552                         None
5553                     }
5554                 }
5555             }
5556
5557             _ => None,
5558         }
5559     }
5560
5561     /// Parse one of the items or view items allowed by the
5562     /// flags; on failure, return IoviNone.
5563     /// NB: this function no longer parses the items inside an
5564     /// extern crate.
5565     fn parse_item_or_view_item(&mut self,
5566                                attrs: Vec<Attribute> ,
5567                                macros_allowed: bool)
5568                                -> ItemOrViewItem {
5569         let nt_item = match self.token {
5570             token::Interpolated(token::NtItem(ref item)) => {
5571                 Some((**item).clone())
5572             }
5573             _ => None
5574         };
5575         match nt_item {
5576             Some(mut item) => {
5577                 self.bump();
5578                 let mut attrs = attrs;
5579                 mem::swap(&mut item.attrs, &mut attrs);
5580                 item.attrs.extend(attrs.into_iter());
5581                 return IoviItem(P(item));
5582             }
5583             None => {}
5584         }
5585
5586         let lo = self.span.lo;
5587
5588         let visibility = self.parse_visibility();
5589
5590         // must be a view item:
5591         if self.eat_keyword(keywords::Use) {
5592             // USE ITEM (IoviViewItem)
5593             let view_item = self.parse_use();
5594             self.expect(&token::Semi);
5595             return IoviViewItem(ast::ViewItem {
5596                 node: view_item,
5597                 attrs: attrs,
5598                 vis: visibility,
5599                 span: mk_sp(lo, self.last_span.hi)
5600             });
5601         }
5602         // either a view item or an item:
5603         if self.eat_keyword(keywords::Extern) {
5604             let next_is_mod = self.eat_keyword(keywords::Mod);
5605
5606             if next_is_mod || self.eat_keyword(keywords::Crate) {
5607                 if next_is_mod {
5608                     let last_span = self.last_span;
5609                     self.span_err(mk_sp(lo, last_span.hi),
5610                                  format!("`extern mod` is obsolete, use \
5611                                           `extern crate` instead \
5612                                           to refer to external \
5613                                           crates.")[])
5614                 }
5615                 return self.parse_item_extern_crate(lo, visibility, attrs);
5616             }
5617
5618             let opt_abi = self.parse_opt_abi();
5619
5620             if self.eat_keyword(keywords::Fn) {
5621                 // EXTERN FUNCTION ITEM
5622                 let abi = opt_abi.unwrap_or(abi::C);
5623                 let (ident, item_, extra_attrs) =
5624                     self.parse_item_fn(Unsafety::Normal, abi);
5625                 let last_span = self.last_span;
5626                 let item = self.mk_item(lo,
5627                                         last_span.hi,
5628                                         ident,
5629                                         item_,
5630                                         visibility,
5631                                         maybe_append(attrs, extra_attrs));
5632                 return IoviItem(item);
5633             } else if self.check(&token::OpenDelim(token::Brace)) {
5634                 return self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs);
5635             }
5636
5637             let span = self.span;
5638             let token_str = self.this_token_to_string();
5639             self.span_fatal(span,
5640                             format!("expected `{}` or `fn`, found `{}`", "{",
5641                                     token_str)[]);
5642         }
5643
5644         if self.eat_keyword(keywords::Virtual) {
5645             let span = self.span;
5646             self.span_err(span, "`virtual` structs have been removed from the language");
5647         }
5648
5649         // the rest are all guaranteed to be items:
5650         if self.token.is_keyword(keywords::Static) {
5651             // STATIC ITEM
5652             self.bump();
5653             let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
5654             let (ident, item_, extra_attrs) = self.parse_item_const(Some(m));
5655             let last_span = self.last_span;
5656             let item = self.mk_item(lo,
5657                                     last_span.hi,
5658                                     ident,
5659                                     item_,
5660                                     visibility,
5661                                     maybe_append(attrs, extra_attrs));
5662             return IoviItem(item);
5663         }
5664         if self.token.is_keyword(keywords::Const) {
5665             // CONST ITEM
5666             self.bump();
5667             if self.eat_keyword(keywords::Mut) {
5668                 let last_span = self.last_span;
5669                 self.span_err(last_span, "const globals cannot be mutable");
5670                 self.span_help(last_span, "did you mean to declare a static?");
5671             }
5672             let (ident, item_, extra_attrs) = self.parse_item_const(None);
5673             let last_span = self.last_span;
5674             let item = self.mk_item(lo,
5675                                     last_span.hi,
5676                                     ident,
5677                                     item_,
5678                                     visibility,
5679                                     maybe_append(attrs, extra_attrs));
5680             return IoviItem(item);
5681         }
5682         if self.token.is_keyword(keywords::Unsafe) &&
5683             self.look_ahead(1u, |t| t.is_keyword(keywords::Trait))
5684         {
5685             // UNSAFE TRAIT ITEM
5686             self.expect_keyword(keywords::Unsafe);
5687             self.expect_keyword(keywords::Trait);
5688             let (ident, item_, extra_attrs) =
5689                 self.parse_item_trait(ast::Unsafety::Unsafe);
5690             let last_span = self.last_span;
5691             let item = self.mk_item(lo,
5692                                     last_span.hi,
5693                                     ident,
5694                                     item_,
5695                                     visibility,
5696                                     maybe_append(attrs, extra_attrs));
5697             return IoviItem(item);
5698         }
5699         if self.token.is_keyword(keywords::Unsafe) &&
5700             self.look_ahead(1u, |t| t.is_keyword(keywords::Impl))
5701         {
5702             // IMPL ITEM
5703             self.expect_keyword(keywords::Unsafe);
5704             self.expect_keyword(keywords::Impl);
5705             let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe);
5706             let last_span = self.last_span;
5707             let item = self.mk_item(lo,
5708                                     last_span.hi,
5709                                     ident,
5710                                     item_,
5711                                     visibility,
5712                                     maybe_append(attrs, extra_attrs));
5713             return IoviItem(item);
5714         }
5715         if self.token.is_keyword(keywords::Fn) &&
5716                 self.look_ahead(1, |f| !Parser::fn_expr_lookahead(f)) {
5717             // FUNCTION ITEM
5718             self.bump();
5719             let (ident, item_, extra_attrs) =
5720                 self.parse_item_fn(Unsafety::Normal, abi::Rust);
5721             let last_span = self.last_span;
5722             let item = self.mk_item(lo,
5723                                     last_span.hi,
5724                                     ident,
5725                                     item_,
5726                                     visibility,
5727                                     maybe_append(attrs, extra_attrs));
5728             return IoviItem(item);
5729         }
5730         if self.token.is_keyword(keywords::Unsafe)
5731             && self.look_ahead(1u, |t| *t != token::OpenDelim(token::Brace)) {
5732             // UNSAFE FUNCTION ITEM
5733             self.bump();
5734             let abi = if self.eat_keyword(keywords::Extern) {
5735                 self.parse_opt_abi().unwrap_or(abi::C)
5736             } else {
5737                 abi::Rust
5738             };
5739             self.expect_keyword(keywords::Fn);
5740             let (ident, item_, extra_attrs) =
5741                 self.parse_item_fn(Unsafety::Unsafe, abi);
5742             let last_span = self.last_span;
5743             let item = self.mk_item(lo,
5744                                     last_span.hi,
5745                                     ident,
5746                                     item_,
5747                                     visibility,
5748                                     maybe_append(attrs, extra_attrs));
5749             return IoviItem(item);
5750         }
5751         if self.eat_keyword(keywords::Mod) {
5752             // MODULE ITEM
5753             let (ident, item_, extra_attrs) =
5754                 self.parse_item_mod(attrs[]);
5755             let last_span = self.last_span;
5756             let item = self.mk_item(lo,
5757                                     last_span.hi,
5758                                     ident,
5759                                     item_,
5760                                     visibility,
5761                                     maybe_append(attrs, extra_attrs));
5762             return IoviItem(item);
5763         }
5764         if self.eat_keyword(keywords::Type) {
5765             // TYPE ITEM
5766             let (ident, item_, extra_attrs) = self.parse_item_type();
5767             let last_span = self.last_span;
5768             let item = self.mk_item(lo,
5769                                     last_span.hi,
5770                                     ident,
5771                                     item_,
5772                                     visibility,
5773                                     maybe_append(attrs, extra_attrs));
5774             return IoviItem(item);
5775         }
5776         if self.eat_keyword(keywords::Enum) {
5777             // ENUM ITEM
5778             let (ident, item_, extra_attrs) = self.parse_item_enum();
5779             let last_span = self.last_span;
5780             let item = self.mk_item(lo,
5781                                     last_span.hi,
5782                                     ident,
5783                                     item_,
5784                                     visibility,
5785                                     maybe_append(attrs, extra_attrs));
5786             return IoviItem(item);
5787         }
5788         if self.eat_keyword(keywords::Trait) {
5789             // TRAIT ITEM
5790             let (ident, item_, extra_attrs) =
5791                 self.parse_item_trait(ast::Unsafety::Normal);
5792             let last_span = self.last_span;
5793             let item = self.mk_item(lo,
5794                                     last_span.hi,
5795                                     ident,
5796                                     item_,
5797                                     visibility,
5798                                     maybe_append(attrs, extra_attrs));
5799             return IoviItem(item);
5800         }
5801         if self.eat_keyword(keywords::Impl) {
5802             // IMPL ITEM
5803             let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal);
5804             let last_span = self.last_span;
5805             let item = self.mk_item(lo,
5806                                     last_span.hi,
5807                                     ident,
5808                                     item_,
5809                                     visibility,
5810                                     maybe_append(attrs, extra_attrs));
5811             return IoviItem(item);
5812         }
5813         if self.eat_keyword(keywords::Struct) {
5814             // STRUCT ITEM
5815             let (ident, item_, extra_attrs) = self.parse_item_struct();
5816             let last_span = self.last_span;
5817             let item = self.mk_item(lo,
5818                                     last_span.hi,
5819                                     ident,
5820                                     item_,
5821                                     visibility,
5822                                     maybe_append(attrs, extra_attrs));
5823             return IoviItem(item);
5824         }
5825         self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5826     }
5827
5828     /// Parse a foreign item; on failure, return IoviNone.
5829     fn parse_foreign_item(&mut self,
5830                           attrs: Vec<Attribute> ,
5831                           macros_allowed: bool)
5832                           -> ItemOrViewItem {
5833         maybe_whole!(iovi self, NtItem);
5834         let lo = self.span.lo;
5835
5836         let visibility = self.parse_visibility();
5837
5838         if self.token.is_keyword(keywords::Static) {
5839             // FOREIGN STATIC ITEM
5840             let item = self.parse_item_foreign_static(visibility, attrs);
5841             return IoviForeignItem(item);
5842         }
5843         if self.token.is_keyword(keywords::Fn) || self.token.is_keyword(keywords::Unsafe) {
5844             // FOREIGN FUNCTION ITEM
5845             let item = self.parse_item_foreign_fn(visibility, attrs);
5846             return IoviForeignItem(item);
5847         }
5848         self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5849     }
5850
5851     /// This is the fall-through for parsing items.
5852     fn parse_macro_use_or_failure(
5853         &mut self,
5854         attrs: Vec<Attribute> ,
5855         macros_allowed: bool,
5856         lo: BytePos,
5857         visibility: Visibility
5858     ) -> ItemOrViewItem {
5859         if macros_allowed && !self.token.is_any_keyword()
5860                 && self.look_ahead(1, |t| *t == token::Not)
5861                 && (self.look_ahead(2, |t| t.is_plain_ident())
5862                     || self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
5863                     || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
5864             // MACRO INVOCATION ITEM
5865
5866             // item macro.
5867             let pth = self.parse_path(NoTypesAllowed);
5868             self.expect(&token::Not);
5869
5870             // a 'special' identifier (like what `macro_rules!` uses)
5871             // is optional. We should eventually unify invoc syntax
5872             // and remove this.
5873             let id = if self.token.is_plain_ident() {
5874                 self.parse_ident()
5875             } else {
5876                 token::special_idents::invalid // no special identifier
5877             };
5878             // eat a matched-delimiter token tree:
5879             let delim = self.expect_open_delim();
5880             let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
5881                                             seq_sep_none(),
5882                                             |p| p.parse_token_tree());
5883             // single-variant-enum... :
5884             let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
5885             let m: ast::Mac = codemap::Spanned { node: m,
5886                                              span: mk_sp(self.span.lo,
5887                                                          self.span.hi) };
5888
5889             if delim != token::Brace {
5890                 if !self.eat(&token::Semi) {
5891                     let last_span = self.last_span;
5892                     self.span_err(last_span,
5893                                   "macros that expand to items must either \
5894                                    be surrounded with braces or followed by \
5895                                    a semicolon");
5896                 }
5897             }
5898
5899             let item_ = ItemMac(m);
5900             let last_span = self.last_span;
5901             let item = self.mk_item(lo,
5902                                     last_span.hi,
5903                                     id,
5904                                     item_,
5905                                     visibility,
5906                                     attrs);
5907             return IoviItem(item);
5908         }
5909
5910         // FAILURE TO PARSE ITEM
5911         match visibility {
5912             Inherited => {}
5913             Public => {
5914                 let last_span = self.last_span;
5915                 self.span_fatal(last_span, "unmatched visibility `pub`");
5916             }
5917         }
5918         return IoviNone(attrs);
5919     }
5920
5921     pub fn parse_item_with_outer_attributes(&mut self) -> Option<P<Item>> {
5922         let attrs = self.parse_outer_attributes();
5923         self.parse_item(attrs)
5924     }
5925
5926     pub fn parse_item(&mut self, attrs: Vec<Attribute>) -> Option<P<Item>> {
5927         match self.parse_item_or_view_item(attrs, true) {
5928             IoviNone(_) => None,
5929             IoviViewItem(_) =>
5930                 self.fatal("view items are not allowed here"),
5931             IoviForeignItem(_) =>
5932                 self.fatal("foreign items are not allowed here"),
5933             IoviItem(item) => Some(item)
5934         }
5935     }
5936
5937     /// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo`
5938     pub fn parse_view_item(&mut self, attrs: Vec<Attribute>) -> ViewItem {
5939         match self.parse_item_or_view_item(attrs, false) {
5940             IoviViewItem(vi) => vi,
5941             _ => self.fatal("expected `use` or `extern crate`"),
5942         }
5943     }
5944
5945     /// Parse, e.g., "use a::b::{z,y}"
5946     fn parse_use(&mut self) -> ViewItem_ {
5947         return ViewItemUse(self.parse_view_path());
5948     }
5949
5950
5951     /// Matches view_path : MOD? non_global_path as IDENT
5952     /// | MOD? non_global_path MOD_SEP LBRACE RBRACE
5953     /// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE
5954     /// | MOD? non_global_path MOD_SEP STAR
5955     /// | MOD? non_global_path
5956     fn parse_view_path(&mut self) -> P<ViewPath> {
5957         let lo = self.span.lo;
5958
5959         if self.check(&token::OpenDelim(token::Brace)) {
5960             // use {foo,bar}
5961             let idents = self.parse_unspanned_seq(
5962                 &token::OpenDelim(token::Brace),
5963                 &token::CloseDelim(token::Brace),
5964                 seq_sep_trailing_allowed(token::Comma),
5965                 |p| p.parse_path_list_item());
5966             let path = ast::Path {
5967                 span: mk_sp(lo, self.span.hi),
5968                 global: false,
5969                 segments: Vec::new()
5970             };
5971             return P(spanned(lo, self.span.hi,
5972                              ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
5973         }
5974
5975         let first_ident = self.parse_ident();
5976         let mut path = vec!(first_ident);
5977         match self.token {
5978           token::Eq => {
5979             // x = foo::bar
5980             self.bump();
5981             let path_lo = self.span.lo;
5982             path = vec!(self.parse_ident());
5983             while self.check(&token::ModSep) {
5984                 self.bump();
5985                 let id = self.parse_ident();
5986                 path.push(id);
5987             }
5988             let span = mk_sp(path_lo, self.span.hi);
5989             self.obsolete(span, ObsoleteImportRenaming);
5990             let path = ast::Path {
5991                 span: span,
5992                 global: false,
5993                 segments: path.into_iter().map(|identifier| {
5994                     ast::PathSegment {
5995                         identifier: identifier,
5996                         parameters: ast::PathParameters::none(),
5997                     }
5998                 }).collect()
5999             };
6000             return P(spanned(lo, self.span.hi,
6001                              ViewPathSimple(first_ident, path,
6002                                            ast::DUMMY_NODE_ID)));
6003           }
6004
6005           token::ModSep => {
6006             // foo::bar or foo::{a,b,c} or foo::*
6007             while self.check(&token::ModSep) {
6008                 self.bump();
6009
6010                 match self.token {
6011                   token::Ident(i, _) => {
6012                     self.bump();
6013                     path.push(i);
6014                   }
6015
6016                   // foo::bar::{a,b,c}
6017                   token::OpenDelim(token::Brace) => {
6018                     let idents = self.parse_unspanned_seq(
6019                         &token::OpenDelim(token::Brace),
6020                         &token::CloseDelim(token::Brace),
6021                         seq_sep_trailing_allowed(token::Comma),
6022                         |p| p.parse_path_list_item()
6023                     );
6024                     let path = ast::Path {
6025                         span: mk_sp(lo, self.span.hi),
6026                         global: false,
6027                         segments: path.into_iter().map(|identifier| {
6028                             ast::PathSegment {
6029                                 identifier: identifier,
6030                                 parameters: ast::PathParameters::none(),
6031                             }
6032                         }).collect()
6033                     };
6034                     return P(spanned(lo, self.span.hi,
6035                                      ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
6036                   }
6037
6038                   // foo::bar::*
6039                   token::BinOp(token::Star) => {
6040                     self.bump();
6041                     let path = ast::Path {
6042                         span: mk_sp(lo, self.span.hi),
6043                         global: false,
6044                         segments: path.into_iter().map(|identifier| {
6045                             ast::PathSegment {
6046                                 identifier: identifier,
6047                                 parameters: ast::PathParameters::none(),
6048                             }
6049                         }).collect()
6050                     };
6051                     return P(spanned(lo, self.span.hi,
6052                                      ViewPathGlob(path, ast::DUMMY_NODE_ID)));
6053                   }
6054
6055                   _ => break
6056                 }
6057             }
6058           }
6059           _ => ()
6060         }
6061         let mut rename_to = path[path.len() - 1u];
6062         let path = ast::Path {
6063             span: mk_sp(lo, self.last_span.hi),
6064             global: false,
6065             segments: path.into_iter().map(|identifier| {
6066                 ast::PathSegment {
6067                     identifier: identifier,
6068                     parameters: ast::PathParameters::none(),
6069                 }
6070             }).collect()
6071         };
6072         if self.eat_keyword(keywords::As) {
6073             rename_to = self.parse_ident()
6074         }
6075         P(spanned(lo,
6076                   self.last_span.hi,
6077                   ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID)))
6078     }
6079
6080     /// Parses a sequence of items. Stops when it finds program
6081     /// text that can't be parsed as an item
6082     /// - mod_items uses extern_mod_allowed = true
6083     /// - block_tail_ uses extern_mod_allowed = false
6084     fn parse_items_and_view_items(&mut self,
6085                                   first_item_attrs: Vec<Attribute> ,
6086                                   mut extern_mod_allowed: bool,
6087                                   macros_allowed: bool)
6088                                   -> ParsedItemsAndViewItems {
6089         let mut attrs = first_item_attrs;
6090         attrs.push_all(self.parse_outer_attributes()[]);
6091         // First, parse view items.
6092         let mut view_items : Vec<ast::ViewItem> = Vec::new();
6093         let mut items = Vec::new();
6094
6095         // I think this code would probably read better as a single
6096         // loop with a mutable three-state-variable (for extern crates,
6097         // view items, and regular items) ... except that because
6098         // of macros, I'd like to delay that entire check until later.
6099         loop {
6100             match self.parse_item_or_view_item(attrs, macros_allowed) {
6101                 IoviNone(attrs) => {
6102                     return ParsedItemsAndViewItems {
6103                         attrs_remaining: attrs,
6104                         view_items: view_items,
6105                         items: items,
6106                         foreign_items: Vec::new()
6107                     }
6108                 }
6109                 IoviViewItem(view_item) => {
6110                     match view_item.node {
6111                         ViewItemUse(..) => {
6112                             // `extern crate` must precede `use`.
6113                             extern_mod_allowed = false;
6114                         }
6115                         ViewItemExternCrate(..) if !extern_mod_allowed => {
6116                             self.span_err(view_item.span,
6117                                           "\"extern crate\" declarations are \
6118                                            not allowed here");
6119                         }
6120                         ViewItemExternCrate(..) => {}
6121                     }
6122                     view_items.push(view_item);
6123                 }
6124                 IoviItem(item) => {
6125                     items.push(item);
6126                     attrs = self.parse_outer_attributes();
6127                     break;
6128                 }
6129                 IoviForeignItem(_) => {
6130                     panic!();
6131                 }
6132             }
6133             attrs = self.parse_outer_attributes();
6134         }
6135
6136         // Next, parse items.
6137         loop {
6138             match self.parse_item_or_view_item(attrs, macros_allowed) {
6139                 IoviNone(returned_attrs) => {
6140                     attrs = returned_attrs;
6141                     break
6142                 }
6143                 IoviViewItem(view_item) => {
6144                     attrs = self.parse_outer_attributes();
6145                     self.span_err(view_item.span,
6146                                   "`use` and `extern crate` declarations must precede items");
6147                 }
6148                 IoviItem(item) => {
6149                     attrs = self.parse_outer_attributes();
6150                     items.push(item)
6151                 }
6152                 IoviForeignItem(_) => {
6153                     panic!();
6154                 }
6155             }
6156         }
6157
6158         ParsedItemsAndViewItems {
6159             attrs_remaining: attrs,
6160             view_items: view_items,
6161             items: items,
6162             foreign_items: Vec::new()
6163         }
6164     }
6165
6166     /// Parses a sequence of foreign items. Stops when it finds program
6167     /// text that can't be parsed as an item
6168     fn parse_foreign_items(&mut self, first_item_attrs: Vec<Attribute> ,
6169                            macros_allowed: bool)
6170         -> ParsedItemsAndViewItems {
6171         let mut attrs = first_item_attrs;
6172         attrs.push_all(self.parse_outer_attributes()[]);
6173         let mut foreign_items = Vec::new();
6174         loop {
6175             match self.parse_foreign_item(attrs, macros_allowed) {
6176                 IoviNone(returned_attrs) => {
6177                     if self.check(&token::CloseDelim(token::Brace)) {
6178                         attrs = returned_attrs;
6179                         break
6180                     }
6181                     self.unexpected();
6182                 },
6183                 IoviViewItem(view_item) => {
6184                     // I think this can't occur:
6185                     self.span_err(view_item.span,
6186                                   "`use` and `extern crate` declarations must precede items");
6187                 }
6188                 IoviItem(item) => {
6189                     // FIXME #5668: this will occur for a macro invocation:
6190                     self.span_fatal(item.span, "macros cannot expand to foreign items");
6191                 }
6192                 IoviForeignItem(foreign_item) => {
6193                     foreign_items.push(foreign_item);
6194                 }
6195             }
6196             attrs = self.parse_outer_attributes();
6197         }
6198
6199         ParsedItemsAndViewItems {
6200             attrs_remaining: attrs,
6201             view_items: Vec::new(),
6202             items: Vec::new(),
6203             foreign_items: foreign_items
6204         }
6205     }
6206
6207     /// Parses a source module as a crate. This is the main
6208     /// entry point for the parser.
6209     pub fn parse_crate_mod(&mut self) -> Crate {
6210         let lo = self.span.lo;
6211         // parse the crate's inner attrs, maybe (oops) one
6212         // of the attrs of an item:
6213         let (inner, next) = self.parse_inner_attrs_and_next();
6214         let first_item_outer_attrs = next;
6215         // parse the items inside the crate:
6216         let m = self.parse_mod_items(token::Eof, first_item_outer_attrs, lo);
6217
6218         ast::Crate {
6219             module: m,
6220             attrs: inner,
6221             config: self.cfg.clone(),
6222             span: mk_sp(lo, self.span.lo),
6223             exported_macros: Vec::new(),
6224         }
6225     }
6226
6227     pub fn parse_optional_str(&mut self)
6228                               -> Option<(InternedString, ast::StrStyle, Option<ast::Name>)> {
6229         let ret = match self.token {
6230             token::Literal(token::Str_(s), suf) => {
6231                 (self.id_to_interned_str(s.ident()), ast::CookedStr, suf)
6232             }
6233             token::Literal(token::StrRaw(s, n), suf) => {
6234                 (self.id_to_interned_str(s.ident()), ast::RawStr(n), suf)
6235             }
6236             _ => return None
6237         };
6238         self.bump();
6239         Some(ret)
6240     }
6241
6242     pub fn parse_str(&mut self) -> (InternedString, StrStyle) {
6243         match self.parse_optional_str() {
6244             Some((s, style, suf)) => {
6245                 let sp = self.last_span;
6246                 self.expect_no_suffix(sp, "str literal", suf);
6247                 (s, style)
6248             }
6249             _ =>  self.fatal("expected string literal")
6250         }
6251     }
6252 }