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