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