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