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.
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.
13 pub use self::PathParsingMode::*;
14 use self::ItemOrViewItem::*;
17 use ast::{AssociatedType, BareFnTy, ClosureTy};
18 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
19 use ast::{ProvidedMethod, Public, Unsafety};
20 use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue};
21 use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, Block};
22 use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause};
23 use ast::{Crate, CrateConfig, Decl, DeclItem};
24 use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf};
25 use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain};
26 use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox};
27 use ast::{ExprBreak, ExprCall, ExprCast};
28 use ast::{ExprField, ExprTupField, ExprClosure, ExprIf, ExprIfLet, ExprIndex};
29 use ast::{ExprLit, ExprLoop, ExprMac, ExprRange};
30 use ast::{ExprMethodCall, ExprParen, ExprPath};
31 use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary};
32 use ast::{ExprVec, ExprWhile, ExprWhileLet, ExprForLoop, Field, FnDecl};
34 use ast::{FnUnboxedClosureKind, FnMutUnboxedClosureKind};
35 use ast::{FnOnceUnboxedClosureKind};
36 use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod, FunctionRetTy};
37 use ast::{Ident, Inherited, ImplItem, Item, Item_, ItemStatic};
38 use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemConst};
39 use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy};
40 use ast::{LifetimeDef, Lit, Lit_};
41 use ast::{LitBool, LitChar, LitByte, LitBinary};
42 use ast::{LitStr, LitInt, Local, LocalLet};
43 use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
44 use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchSource};
45 use ast::{Method, MutTy, BiMul, Mutability};
46 use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, NodeId, UnNot};
47 use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
48 use ast::{PatTup, PatBox, PatWild, PatWildMulti, PatWildSingle};
49 use ast::{PolyTraitRef};
50 use ast::{QPath, RequiredMethod};
51 use ast::{Return, BiShl, BiShr, Stmt, StmtDecl};
52 use ast::{StmtExpr, StmtSemi, StmtMac, StructDef, StructField};
53 use ast::{StructVariantKind, BiSub, StrStyle};
54 use ast::{SelfExplicit, SelfRegion, SelfStatic, SelfValue};
55 use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
56 use ast::{TtDelimited, TtSequence, TtToken};
57 use ast::{TupleVariantKind, Ty, Ty_, TypeBinding};
58 use ast::{TypeField, TyFixedLengthVec, TyClosure, TyBareFn};
59 use ast::{TyTypeof, TyInfer, TypeMethod};
60 use ast::{TyParam, TyParamBound, TyParen, TyPath, TyPolyTraitRef, TyPtr, TyQPath};
61 use ast::{TyRptr, TyTup, TyU32, TyVec, UnUniq};
62 use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind};
63 use ast::{UnnamedField, UnsafeBlock};
64 use ast::{ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse};
65 use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
66 use ast::{Visibility, WhereClause};
68 use ast_util::{mod, as_prec, ident_to_path, operator_prec};
69 use codemap::{mod, Span, BytePos, Spanned, spanned, mk_sp, DUMMY_SP};
71 use ext::tt::macro_parser;
73 use parse::attr::ParserAttr;
75 use parse::common::{SeqSep, seq_sep_none, seq_sep_trailing_allowed};
76 use parse::lexer::{Reader, TokenAndSpan};
77 use parse::obsolete::*;
78 use parse::token::{mod, MatchNt, SubstNt, InternedString};
79 use parse::token::{keywords, special_idents};
80 use parse::{new_sub_parser_from_file, ParseSess};
83 use owned_slice::OwnedSlice;
85 use std::collections::HashSet;
86 use std::io::fs::PathExtensions;
94 flags Restrictions: u8 {
95 const UNRESTRICTED = 0b0000,
96 const RESTRICTION_STMT_EXPR = 0b0001,
97 const RESTRICTION_NO_BAR_OP = 0b0010,
98 const RESTRICTION_NO_STRUCT_LITERAL = 0b0100,
99 const RESTRICTION_NO_DOTS = 0b1000,
104 type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
106 /// How to parse a path. There are four different kinds of paths, all of which
107 /// are parsed somewhat differently.
108 #[deriving(Copy, PartialEq)]
109 pub enum PathParsingMode {
110 /// A path with no type parameters; e.g. `foo::bar::Baz`
112 /// A path with a lifetime and type parameters, with no double colons
113 /// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`
114 LifetimeAndTypesWithoutColons,
115 /// A path with a lifetime and type parameters with double colons before
116 /// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`
117 LifetimeAndTypesWithColons,
120 /// How to parse a bound, whether to allow bound modifiers such as `?`.
121 #[deriving(Copy, PartialEq)]
122 pub enum BoundParsingMode {
127 enum ItemOrViewItem {
128 /// Indicates a failure to parse any kind of item. The attributes are
130 IoviNone(Vec<Attribute>),
132 IoviForeignItem(P<ForeignItem>),
133 IoviViewItem(ViewItem)
137 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
138 /// dropped into the token stream, which happens while parsing the result of
139 /// macro expansion). Placement of these is not as complex as I feared it would
140 /// be. The important thing is to make sure that lookahead doesn't balk at
141 /// `token::Interpolated` tokens.
142 macro_rules! maybe_whole_expr {
145 let found = match $p.token {
146 token::Interpolated(token::NtExpr(ref e)) => {
149 token::Interpolated(token::NtPath(_)) => {
150 // FIXME: The following avoids an issue with lexical borrowck scopes,
151 // but the clone is unfortunate.
152 let pt = match $p.token {
153 token::Interpolated(token::NtPath(ref pt)) => (**pt).clone(),
157 Some($p.mk_expr(span.lo, span.hi, ExprPath(pt)))
159 token::Interpolated(token::NtBlock(_)) => {
160 // FIXME: The following avoids an issue with lexical borrowck scopes,
161 // but the clone is unfortunate.
162 let b = match $p.token {
163 token::Interpolated(token::NtBlock(ref b)) => (*b).clone(),
167 Some($p.mk_expr(span.lo, span.hi, ExprBlock(b)))
182 /// As maybe_whole_expr, but for things other than expressions
183 macro_rules! maybe_whole {
184 ($p:expr, $constructor:ident) => (
186 let found = match ($p).token {
187 token::Interpolated(token::$constructor(_)) => {
188 Some(($p).bump_and_get())
192 if let Some(token::Interpolated(token::$constructor(x))) = found {
197 (no_clone $p:expr, $constructor:ident) => (
199 let found = match ($p).token {
200 token::Interpolated(token::$constructor(_)) => {
201 Some(($p).bump_and_get())
205 if let Some(token::Interpolated(token::$constructor(x))) = found {
210 (deref $p:expr, $constructor:ident) => (
212 let found = match ($p).token {
213 token::Interpolated(token::$constructor(_)) => {
214 Some(($p).bump_and_get())
218 if let Some(token::Interpolated(token::$constructor(x))) = found {
223 (Some $p:expr, $constructor:ident) => (
225 let found = match ($p).token {
226 token::Interpolated(token::$constructor(_)) => {
227 Some(($p).bump_and_get())
231 if let Some(token::Interpolated(token::$constructor(x))) = found {
232 return Some(x.clone());
236 (iovi $p:expr, $constructor:ident) => (
238 let found = match ($p).token {
239 token::Interpolated(token::$constructor(_)) => {
240 Some(($p).bump_and_get())
244 if let Some(token::Interpolated(token::$constructor(x))) = found {
245 return IoviItem(x.clone());
249 (pair_empty $p:expr, $constructor:ident) => (
251 let found = match ($p).token {
252 token::Interpolated(token::$constructor(_)) => {
253 Some(($p).bump_and_get())
257 if let Some(token::Interpolated(token::$constructor(x))) = found {
258 return (Vec::new(), x);
265 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
268 Some(ref attrs) => lhs.extend(attrs.iter().map(|a| a.clone())),
275 struct ParsedItemsAndViewItems {
276 attrs_remaining: Vec<Attribute>,
277 view_items: Vec<ViewItem>,
278 items: Vec<P<Item>> ,
279 foreign_items: Vec<P<ForeignItem>>
282 /* ident is handled by common.rs */
284 pub struct Parser<'a> {
285 pub sess: &'a ParseSess,
286 /// the current token:
287 pub token: token::Token,
288 /// the span of the current token:
290 /// the span of the prior token:
292 pub cfg: CrateConfig,
293 /// the previous token or None (only stashed sometimes).
294 pub last_token: Option<Box<token::Token>>,
295 pub buffer: [TokenAndSpan, ..4],
296 pub buffer_start: int,
298 pub tokens_consumed: uint,
299 pub restrictions: Restrictions,
300 pub quote_depth: uint, // not (yet) related to the quasiquoter
301 pub reader: Box<Reader+'a>,
302 pub interner: Rc<token::IdentInterner>,
303 /// The set of seen errors about obsolete syntax. Used to suppress
304 /// extra detail when the same error is seen twice
305 pub obsolete_set: HashSet<ObsoleteSyntax>,
306 /// Used to determine the path to externally loaded source files
307 pub mod_path_stack: Vec<InternedString>,
308 /// Stack of spans of open delimiters. Used for error message.
309 pub open_braces: Vec<Span>,
310 /// Flag if this parser "owns" the directory that it is currently parsing
311 /// in. This will affect how nested files are looked up.
312 pub owns_directory: bool,
313 /// Name of the root module this parser originated from. If `None`, then the
314 /// name is not known. This does not change while the parser is descending
315 /// into modules, and sub-parsers have new values for this name.
316 pub root_module_name: Option<String>,
317 pub expected_tokens: Vec<TokenType>,
320 #[deriving(PartialEq, Eq, Clone)]
327 fn to_string(&self) -> String {
329 TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
330 TokenType::Operator => "an operator".to_string(),
335 fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
336 t.is_plain_ident() || *t == token::Underscore
339 impl<'a> Parser<'a> {
340 pub fn new(sess: &'a ParseSess,
341 cfg: ast::CrateConfig,
342 mut rdr: Box<Reader+'a>)
345 let tok0 = rdr.real_token();
347 let placeholder = TokenAndSpan {
348 tok: token::Underscore,
354 interner: token::get_ident_interner(),
370 restrictions: UNRESTRICTED,
372 obsolete_set: HashSet::new(),
373 mod_path_stack: Vec::new(),
374 open_braces: Vec::new(),
375 owns_directory: true,
376 root_module_name: None,
377 expected_tokens: Vec::new(),
381 /// Convert a token to a string using self's reader
382 pub fn token_to_string(token: &token::Token) -> String {
383 pprust::token_to_string(token)
386 /// Convert the current token to a string using self's reader
387 pub fn this_token_to_string(&mut self) -> String {
388 Parser::token_to_string(&self.token)
391 pub fn unexpected_last(&mut self, t: &token::Token) -> ! {
392 let token_str = Parser::token_to_string(t);
393 let last_span = self.last_span;
394 self.span_fatal(last_span, format!("unexpected token: `{}`",
398 pub fn unexpected(&mut self) -> ! {
399 let this_token = self.this_token_to_string();
400 self.fatal(format!("unexpected token: `{}`", this_token)[]);
403 /// Expect and consume the token t. Signal an error if
404 /// the next token is not t.
405 pub fn expect(&mut self, t: &token::Token) {
406 if self.expected_tokens.is_empty() {
407 if self.token == *t {
410 let token_str = Parser::token_to_string(t);
411 let this_token_str = self.this_token_to_string();
412 self.fatal(format!("expected `{}`, found `{}`",
417 self.expect_one_of(slice::ref_slice(t), &[]);
421 /// Expect next token to be edible or inedible token. If edible,
422 /// then consume it; if inedible, then return without consuming
423 /// anything. Signal a fatal error if next token is unexpected.
424 pub fn expect_one_of(&mut self,
425 edible: &[token::Token],
426 inedible: &[token::Token]) {
427 fn tokens_to_string(tokens: &[TokenType]) -> String {
428 let mut i = tokens.iter();
429 // This might be a sign we need a connect method on Iterator.
431 .map_or("".to_string(), |t| t.to_string());
432 i.enumerate().fold(b, |mut b, (i, ref a)| {
433 if tokens.len() > 2 && i == tokens.len() - 2 {
435 } else if tokens.len() == 2 && i == tokens.len() - 2 {
440 b.push_str(&*a.to_string());
444 if edible.contains(&self.token) {
446 } else if inedible.contains(&self.token) {
447 // leave it in the input
449 let mut expected = edible.iter().map(|x| TokenType::Token(x.clone()))
450 .collect::<Vec<_>>();
451 expected.extend(inedible.iter().map(|x| TokenType::Token(x.clone())));
452 expected.push_all(&*self.expected_tokens);
453 expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
455 let expect = tokens_to_string(expected[]);
456 let actual = self.this_token_to_string();
458 (if expected.len() != 1 {
459 (format!("expected one of {}, found `{}`",
463 (format!("expected {}, found `{}`",
471 /// Check for erroneous `ident { }`; if matches, signal error and
472 /// recover (without consuming any expected input token). Returns
473 /// true if and only if input was consumed for recovery.
474 pub fn check_for_erroneous_unit_struct_expecting(&mut self, expected: &[token::Token]) -> bool {
475 if self.token == token::OpenDelim(token::Brace)
476 && expected.iter().all(|t| *t != token::OpenDelim(token::Brace))
477 && self.look_ahead(1, |t| *t == token::CloseDelim(token::Brace)) {
478 // matched; signal non-fatal error and recover.
479 let span = self.span;
481 "unit-like struct construction is written with no trailing `{ }`");
482 self.eat(&token::OpenDelim(token::Brace));
483 self.eat(&token::CloseDelim(token::Brace));
490 /// Commit to parsing a complete expression `e` expected to be
491 /// followed by some token from the set edible + inedible. Recover
492 /// from anticipated input errors, discarding erroneous characters.
493 pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[token::Token]) {
494 debug!("commit_expr {}", e);
495 if let ExprPath(..) = e.node {
496 // might be unit-struct construction; check for recoverableinput error.
497 let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
498 expected.push_all(inedible);
499 self.check_for_erroneous_unit_struct_expecting(expected[]);
501 self.expect_one_of(edible, inedible)
504 pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) {
505 self.commit_expr(e, &[edible], &[])
508 /// Commit to parsing a complete statement `s`, which expects to be
509 /// followed by some token from the set edible + inedible. Check
510 /// for recoverable input errors, discarding erroneous characters.
511 pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token]) {
514 .map_or(false, |t| t.is_ident() || t.is_path()) {
515 let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
516 expected.push_all(inedible[]);
517 self.check_for_erroneous_unit_struct_expecting(
520 self.expect_one_of(edible, inedible)
523 pub fn commit_stmt_expecting(&mut self, edible: token::Token) {
524 self.commit_stmt(&[edible], &[])
527 pub fn parse_ident(&mut self) -> ast::Ident {
528 self.check_strict_keywords();
529 self.check_reserved_keywords();
531 token::Ident(i, _) => {
535 token::Interpolated(token::NtIdent(..)) => {
536 self.bug("ident interpolation not converted to real token");
539 let token_str = self.this_token_to_string();
540 self.fatal((format!("expected ident, found `{}`",
546 pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
547 let lo = self.span.lo;
548 let node = if self.eat_keyword(keywords::Mod) {
549 ast::PathListMod { id: ast::DUMMY_NODE_ID }
551 let ident = self.parse_ident();
552 ast::PathListIdent { name: ident, id: ast::DUMMY_NODE_ID }
554 let hi = self.last_span.hi;
555 spanned(lo, hi, node)
558 /// Check if the next token is `tok`, and return `true` if so.
560 /// This method is will automatically add `tok` to `expected_tokens` if `tok` is not
562 pub fn check(&mut self, tok: &token::Token) -> bool {
563 let is_present = self.token == *tok;
564 if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
568 /// Consume token 'tok' if it exists. Returns true if the given
569 /// token was present, false otherwise.
570 pub fn eat(&mut self, tok: &token::Token) -> bool {
571 let is_present = self.check(tok);
572 if is_present { self.bump() }
576 /// If the next token is the given keyword, eat it and return
577 /// true. Otherwise, return false.
578 pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
579 if self.token.is_keyword(kw) {
587 /// If the given word is not a keyword, signal an error.
588 /// If the next token is not the given word, signal an error.
589 /// Otherwise, eat it.
590 pub fn expect_keyword(&mut self, kw: keywords::Keyword) {
591 if !self.eat_keyword(kw) {
592 let id_interned_str = token::get_name(kw.to_name());
593 let token_str = self.this_token_to_string();
594 self.fatal(format!("expected `{}`, found `{}`",
595 id_interned_str, token_str)[])
599 /// Signal an error if the given string is a strict keyword
600 pub fn check_strict_keywords(&mut self) {
601 if self.token.is_strict_keyword() {
602 let token_str = self.this_token_to_string();
603 let span = self.span;
605 format!("expected identifier, found keyword `{}`",
610 /// Signal an error if the current token is a reserved keyword
611 pub fn check_reserved_keywords(&mut self) {
612 if self.token.is_reserved_keyword() {
613 let token_str = self.this_token_to_string();
614 self.fatal(format!("`{}` is a reserved keyword",
619 /// Expect and consume an `&`. If `&&` is seen, replace it with a single
620 /// `&` and continue. If an `&` is not seen, signal an error.
621 fn expect_and(&mut self) {
623 token::BinOp(token::And) => self.bump(),
625 let span = self.span;
626 let lo = span.lo + BytePos(1);
627 self.replace_token(token::BinOp(token::And), lo, span.hi)
630 let token_str = self.this_token_to_string();
632 Parser::token_to_string(&token::BinOp(token::And));
633 self.fatal(format!("expected `{}`, found `{}`",
640 /// Expect and consume a `|`. If `||` is seen, replace it with a single
641 /// `|` and continue. If a `|` is not seen, signal an error.
642 fn expect_or(&mut self) {
644 token::BinOp(token::Or) => self.bump(),
646 let span = self.span;
647 let lo = span.lo + BytePos(1);
648 self.replace_token(token::BinOp(token::Or), lo, span.hi)
651 let found_token = self.this_token_to_string();
653 Parser::token_to_string(&token::BinOp(token::Or));
654 self.fatal(format!("expected `{}`, found `{}`",
661 pub fn expect_no_suffix(&mut self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
663 None => {/* everything ok */}
665 let text = suf.as_str();
667 self.span_bug(sp, "found empty literal suffix in Some")
669 self.span_err(sp, &*format!("{} with a suffix is illegal", kind));
675 /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
676 /// `<` and continue. If a `<` is not seen, return false.
678 /// This is meant to be used when parsing generics on a path to get the
679 /// starting token. The `force` parameter is used to forcefully break up a
680 /// `<<` token. If `force` is false, then `<<` is only broken when a lifetime
681 /// shows up next. For example, consider the expression:
683 /// foo as bar << test
685 /// The parser needs to know if `bar <<` is the start of a generic path or if
686 /// it's a left-shift token. If `test` were a lifetime, then it's impossible
687 /// for the token to be a left-shift, but if it's not a lifetime, then it's
688 /// considered a left-shift.
690 /// The reason for this is that the only current ambiguity with `<<` is when
691 /// parsing closure types:
693 /// foo::<<'a> ||>();
694 /// impl Foo<<'a> ||>() { ... }
695 fn eat_lt(&mut self, force: bool) -> bool {
697 token::Lt => { self.bump(); true }
698 token::BinOp(token::Shl) => {
699 let next_lifetime = self.look_ahead(1, |t| match *t {
700 token::Lifetime(..) => true,
703 if force || next_lifetime {
704 let span = self.span;
705 let lo = span.lo + BytePos(1);
706 self.replace_token(token::Lt, lo, span.hi);
716 fn expect_lt(&mut self) {
717 if !self.eat_lt(true) {
718 let found_token = self.this_token_to_string();
719 let token_str = Parser::token_to_string(&token::Lt);
720 self.fatal(format!("expected `{}`, found `{}`",
726 /// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
727 fn parse_seq_to_before_or<T, F>(&mut self,
731 F: FnMut(&mut Parser) -> T,
733 let mut first = true;
734 let mut vector = Vec::new();
735 while self.token != token::BinOp(token::Or) &&
736 self.token != token::OrOr {
748 /// Expect and consume a GT. if a >> is seen, replace it
749 /// with a single > and continue. If a GT is not seen,
751 pub fn expect_gt(&mut self) {
753 token::Gt => self.bump(),
754 token::BinOp(token::Shr) => {
755 let span = self.span;
756 let lo = span.lo + BytePos(1);
757 self.replace_token(token::Gt, lo, span.hi)
759 token::BinOpEq(token::Shr) => {
760 let span = self.span;
761 let lo = span.lo + BytePos(1);
762 self.replace_token(token::Ge, lo, span.hi)
765 let span = self.span;
766 let lo = span.lo + BytePos(1);
767 self.replace_token(token::Eq, lo, span.hi)
770 let gt_str = Parser::token_to_string(&token::Gt);
771 let this_token_str = self.this_token_to_string();
772 self.fatal(format!("expected `{}`, found `{}`",
779 pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
780 sep: Option<token::Token>,
782 -> (OwnedSlice<T>, bool) where
783 F: FnMut(&mut Parser) -> Option<T>,
785 let mut v = Vec::new();
786 // This loop works by alternating back and forth between parsing types
787 // and commas. For example, given a string `A, B,>`, the parser would
788 // first parse `A`, then a comma, then `B`, then a comma. After that it
789 // would encounter a `>` and stop. This lets the parser handle trailing
790 // commas in generic parameters, because it can stop either after
791 // parsing a type or after parsing a comma.
792 for i in iter::count(0u, 1) {
793 if self.check(&token::Gt)
794 || self.token == token::BinOp(token::Shr)
795 || self.token == token::Ge
796 || self.token == token::BinOpEq(token::Shr) {
802 Some(result) => v.push(result),
803 None => return (OwnedSlice::from_vec(v), true)
806 sep.as_ref().map(|t| self.expect(t));
809 return (OwnedSlice::from_vec(v), false);
812 /// Parse a sequence bracketed by '<' and '>', stopping
814 pub fn parse_seq_to_before_gt<T, F>(&mut self,
815 sep: Option<token::Token>,
817 -> OwnedSlice<T> where
818 F: FnMut(&mut Parser) -> T,
820 let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, |p| Some(f(p)));
825 pub fn parse_seq_to_gt<T, F>(&mut self,
826 sep: Option<token::Token>,
828 -> OwnedSlice<T> where
829 F: FnMut(&mut Parser) -> T,
831 let v = self.parse_seq_to_before_gt(sep, f);
836 pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
837 sep: Option<token::Token>,
839 -> (OwnedSlice<T>, bool) where
840 F: FnMut(&mut Parser) -> Option<T>,
842 let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f);
846 return (v, returned);
849 /// Parse a sequence, including the closing delimiter. The function
850 /// f must consume tokens until reaching the next separator or
852 pub fn parse_seq_to_end<T, F>(&mut self,
857 F: FnMut(&mut Parser) -> T,
859 let val = self.parse_seq_to_before_end(ket, sep, f);
864 /// Parse a sequence, not including the closing delimiter. The function
865 /// f must consume tokens until reaching the next separator or
867 pub fn parse_seq_to_before_end<T, F>(&mut self,
872 F: FnMut(&mut Parser) -> T,
874 let mut first: bool = true;
876 while self.token != *ket {
879 if first { first = false; }
880 else { self.expect(t); }
884 if sep.trailing_sep_allowed && self.check(ket) { break; }
890 /// Parse a sequence, including the closing delimiter. The function
891 /// f must consume tokens until reaching the next separator or
893 pub fn parse_unspanned_seq<T, F>(&mut self,
899 F: FnMut(&mut Parser) -> T,
902 let result = self.parse_seq_to_before_end(ket, sep, f);
907 /// Parse a sequence parameter of enum variant. For consistency purposes,
908 /// these should not be empty.
909 pub fn parse_enum_variant_seq<T, F>(&mut self,
915 F: FnMut(&mut Parser) -> T,
917 let result = self.parse_unspanned_seq(bra, ket, sep, f);
918 if result.is_empty() {
919 let last_span = self.last_span;
920 self.span_err(last_span,
921 "nullary enum variants are written with no trailing `( )`");
926 // NB: Do not use this function unless you actually plan to place the
927 // spanned list in the AST.
928 pub fn parse_seq<T, F>(&mut self,
933 -> Spanned<Vec<T>> where
934 F: FnMut(&mut Parser) -> T,
936 let lo = self.span.lo;
938 let result = self.parse_seq_to_before_end(ket, sep, f);
939 let hi = self.span.hi;
941 spanned(lo, hi, result)
944 /// Advance the parser by one token
945 pub fn bump(&mut self) {
946 self.last_span = self.span;
947 // Stash token for error recovery (sometimes; clone is not necessarily cheap).
948 self.last_token = if self.token.is_ident() || self.token.is_path() {
949 Some(box self.token.clone())
953 let next = if self.buffer_start == self.buffer_end {
954 self.reader.real_token()
956 // Avoid token copies with `replace`.
957 let buffer_start = self.buffer_start as uint;
958 let next_index = (buffer_start + 1) & 3 as uint;
959 self.buffer_start = next_index as int;
961 let placeholder = TokenAndSpan {
962 tok: token::Underscore,
965 mem::replace(&mut self.buffer[buffer_start], placeholder)
968 self.token = next.tok;
969 self.tokens_consumed += 1u;
970 self.expected_tokens.clear();
973 /// Advance the parser by one token and return the bumped token.
974 pub fn bump_and_get(&mut self) -> token::Token {
975 let old_token = mem::replace(&mut self.token, token::Underscore);
980 /// EFFECT: replace the current token and span with the given one
981 pub fn replace_token(&mut self,
985 self.last_span = mk_sp(self.span.lo, lo);
987 self.span = mk_sp(lo, hi);
989 pub fn buffer_length(&mut self) -> int {
990 if self.buffer_start <= self.buffer_end {
991 return self.buffer_end - self.buffer_start;
993 return (4 - self.buffer_start) + self.buffer_end;
995 pub fn look_ahead<R, F>(&mut self, distance: uint, f: F) -> R where
996 F: FnOnce(&token::Token) -> R,
998 let dist = distance as int;
999 while self.buffer_length() < dist {
1000 self.buffer[self.buffer_end as uint] = self.reader.real_token();
1001 self.buffer_end = (self.buffer_end + 1) & 3;
1003 f(&self.buffer[((self.buffer_start + dist - 1) & 3) as uint].tok)
1005 pub fn fatal(&mut self, m: &str) -> ! {
1006 self.sess.span_diagnostic.span_fatal(self.span, m)
1008 pub fn span_fatal(&mut self, sp: Span, m: &str) -> ! {
1009 self.sess.span_diagnostic.span_fatal(sp, m)
1011 pub fn span_fatal_help(&mut self, sp: Span, m: &str, help: &str) -> ! {
1012 self.span_err(sp, m);
1013 self.span_help(sp, help);
1014 panic!(diagnostic::FatalError);
1016 pub fn span_note(&mut self, sp: Span, m: &str) {
1017 self.sess.span_diagnostic.span_note(sp, m)
1019 pub fn span_help(&mut self, sp: Span, m: &str) {
1020 self.sess.span_diagnostic.span_help(sp, m)
1022 pub fn bug(&mut self, m: &str) -> ! {
1023 self.sess.span_diagnostic.span_bug(self.span, m)
1025 pub fn warn(&mut self, m: &str) {
1026 self.sess.span_diagnostic.span_warn(self.span, m)
1028 pub fn span_warn(&mut self, sp: Span, m: &str) {
1029 self.sess.span_diagnostic.span_warn(sp, m)
1031 pub fn span_err(&mut self, sp: Span, m: &str) {
1032 self.sess.span_diagnostic.span_err(sp, m)
1034 pub fn span_bug(&mut self, sp: Span, m: &str) -> ! {
1035 self.sess.span_diagnostic.span_bug(sp, m)
1037 pub fn abort_if_errors(&mut self) {
1038 self.sess.span_diagnostic.handler().abort_if_errors();
1041 pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString {
1042 token::get_ident(id)
1045 /// Is the current token one of the keywords that signals a bare function
1047 pub fn token_is_bare_fn_keyword(&mut self) -> bool {
1048 self.token.is_keyword(keywords::Fn) ||
1049 self.token.is_keyword(keywords::Unsafe) ||
1050 self.token.is_keyword(keywords::Extern)
1053 /// Is the current token one of the keywords that signals a closure type?
1054 pub fn token_is_closure_keyword(&mut self) -> bool {
1055 self.token.is_keyword(keywords::Unsafe)
1058 pub fn get_lifetime(&mut self) -> ast::Ident {
1060 token::Lifetime(ref ident) => *ident,
1061 _ => self.bug("not a lifetime"),
1065 pub fn parse_for_in_type(&mut self) -> Ty_ {
1067 Parses whatever can come after a `for` keyword in a type.
1068 The `for` has already been consumed.
1072 - for <'lt> |S| -> T
1076 - for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
1077 - for <'lt> path::foo(a, b)
1082 let lifetime_defs = self.parse_late_bound_lifetime_defs();
1084 // examine next token to decide to do
1085 if self.eat_keyword(keywords::Proc) {
1086 self.parse_proc_type(lifetime_defs)
1087 } else if self.token_is_bare_fn_keyword() || self.token_is_closure_keyword() {
1088 self.parse_ty_bare_fn_or_ty_closure(lifetime_defs)
1089 } else if self.check(&token::ModSep) ||
1090 self.token.is_ident() ||
1091 self.token.is_path()
1093 let trait_ref = self.parse_trait_ref();
1094 let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
1095 trait_ref: trait_ref };
1096 let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
1097 self.parse_ty_param_bounds(BoundParsingMode::Bare)
1102 Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
1103 .chain(other_bounds.into_vec().into_iter())
1105 ast::TyPolyTraitRef(all_bounds)
1107 self.parse_ty_closure(lifetime_defs)
1111 pub fn parse_ty_path(&mut self) -> Ty_ {
1112 let path = self.parse_path(LifetimeAndTypesWithoutColons);
1113 TyPath(path, ast::DUMMY_NODE_ID)
1116 /// parse a TyBareFn type:
1117 pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1120 [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1121 ^~~~^ ^~~~^ ^~~~^ ^~^ ^
1124 | | | Argument types
1130 let unsafety = self.parse_unsafety();
1131 let abi = if self.eat_keyword(keywords::Extern) {
1132 self.parse_opt_abi().unwrap_or(abi::C)
1137 self.expect_keyword(keywords::Fn);
1138 let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1139 let (inputs, variadic) = self.parse_fn_args(false, true);
1140 let ret_ty = self.parse_ret_ty();
1141 let decl = P(FnDecl {
1146 TyBareFn(P(BareFnTy {
1149 lifetimes: lifetime_defs,
1154 /// Parses a procedure type (`proc`). The initial `proc` keyword must
1155 /// already have been parsed.
1156 pub fn parse_proc_type(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1159 proc <'lt> (S) [:Bounds] -> T
1160 ^~~^ ^~~~^ ^ ^~~~~~~~^ ^
1166 the `proc` keyword (already consumed)
1170 let proc_span = self.last_span;
1172 // To be helpful, parse the proc as ever
1173 let _ = self.parse_legacy_lifetime_defs(lifetime_defs);
1174 let _ = self.parse_fn_args(false, false);
1175 let _ = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1176 let _ = self.parse_ret_ty();
1178 self.obsolete(proc_span, ObsoleteProcType);
1183 /// Parses an optional unboxed closure kind (`&:`, `&mut:`, or `:`).
1184 pub fn parse_optional_unboxed_closure_kind(&mut self)
1185 -> Option<UnboxedClosureKind> {
1186 if self.check(&token::BinOp(token::And)) &&
1187 self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
1188 self.look_ahead(2, |t| *t == token::Colon) {
1192 return Some(FnMutUnboxedClosureKind)
1195 if self.token == token::BinOp(token::And) &&
1196 self.look_ahead(1, |t| *t == token::Colon) {
1199 return Some(FnUnboxedClosureKind)
1202 if self.eat(&token::Colon) {
1203 return Some(FnOnceUnboxedClosureKind)
1209 pub fn parse_ty_bare_fn_or_ty_closure(&mut self, lifetime_defs: Vec<LifetimeDef>) -> Ty_ {
1210 // Both bare fns and closures can begin with stuff like unsafe
1211 // and extern. So we just scan ahead a few tokens to see if we see
1214 // Closure: [unsafe] <'lt> |S| [:Bounds] -> T
1215 // Fn: [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1217 if self.token.is_keyword(keywords::Fn) {
1218 self.parse_ty_bare_fn(lifetime_defs)
1219 } else if self.token.is_keyword(keywords::Extern) {
1220 self.parse_ty_bare_fn(lifetime_defs)
1221 } else if self.token.is_keyword(keywords::Unsafe) {
1222 if self.look_ahead(1, |t| t.is_keyword(keywords::Fn) ||
1223 t.is_keyword(keywords::Extern)) {
1224 self.parse_ty_bare_fn(lifetime_defs)
1226 self.parse_ty_closure(lifetime_defs)
1229 self.parse_ty_closure(lifetime_defs)
1233 /// Parse a TyClosure type
1234 pub fn parse_ty_closure(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1237 [unsafe] <'lt> |S| [:Bounds] -> T
1238 ^~~~~~~^ ^~~~^ ^ ^~~~~~~~^ ^
1241 | | | Closure bounds
1243 | Deprecated lifetime defs
1249 let unsafety = self.parse_unsafety();
1251 let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1253 let inputs = if self.eat(&token::OrOr) {
1258 let inputs = self.parse_seq_to_before_or(
1260 |p| p.parse_arg_general(false));
1265 let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1267 let output = self.parse_ret_ty();
1268 let decl = P(FnDecl {
1274 TyClosure(P(ClosureTy {
1279 lifetimes: lifetime_defs,
1283 pub fn parse_unsafety(&mut self) -> Unsafety {
1284 if self.eat_keyword(keywords::Unsafe) {
1285 return Unsafety::Unsafe;
1287 return Unsafety::Normal;
1291 /// Parses `[ 'for' '<' lifetime_defs '>' ]'
1292 fn parse_legacy_lifetime_defs(&mut self,
1293 lifetime_defs: Vec<ast::LifetimeDef>)
1294 -> Vec<ast::LifetimeDef>
1296 if self.token == token::Lt {
1298 if lifetime_defs.is_empty() {
1299 self.warn("deprecated syntax; use the `for` keyword now \
1300 (e.g. change `fn<'a>` to `for<'a> fn`)");
1301 let lifetime_defs = self.parse_lifetime_defs();
1305 self.fatal("cannot use new `for` keyword and older syntax together");
1312 /// Parses `type Foo;` in a trait declaration only. The `type` keyword has
1313 /// already been parsed.
1314 fn parse_associated_type(&mut self, attrs: Vec<Attribute>)
1317 let ty_param = self.parse_ty_param();
1318 self.expect(&token::Semi);
1325 /// Parses `type Foo = TYPE;` in an implementation declaration only. The
1326 /// `type` keyword has already been parsed.
1327 fn parse_typedef(&mut self, attrs: Vec<Attribute>, vis: Visibility)
1329 let lo = self.span.lo;
1330 let ident = self.parse_ident();
1331 self.expect(&token::Eq);
1332 let typ = self.parse_ty_sum();
1333 let hi = self.span.hi;
1334 self.expect(&token::Semi);
1336 id: ast::DUMMY_NODE_ID,
1337 span: mk_sp(lo, hi),
1345 /// Parse the items in a trait declaration
1346 pub fn parse_trait_items(&mut self) -> Vec<TraitItem> {
1347 self.parse_unspanned_seq(
1348 &token::OpenDelim(token::Brace),
1349 &token::CloseDelim(token::Brace),
1352 let attrs = p.parse_outer_attributes();
1354 if p.eat_keyword(keywords::Type) {
1355 TypeTraitItem(P(p.parse_associated_type(attrs)))
1359 let vis = p.parse_visibility();
1360 let style = p.parse_unsafety();
1361 let abi = if p.eat_keyword(keywords::Extern) {
1362 p.parse_opt_abi().unwrap_or(abi::C)
1366 p.expect_keyword(keywords::Fn);
1368 let ident = p.parse_ident();
1369 let mut generics = p.parse_generics();
1371 let (explicit_self, d) = p.parse_fn_decl_with_self(|p| {
1372 // This is somewhat dubious; We don't want to allow
1373 // argument names to be left off if there is a
1375 p.parse_arg_general(false)
1378 p.parse_where_clause(&mut generics);
1380 let hi = p.last_span.hi;
1384 debug!("parse_trait_methods(): parsing required method");
1385 RequiredMethod(TypeMethod {
1392 explicit_self: explicit_self,
1393 id: ast::DUMMY_NODE_ID,
1394 span: mk_sp(lo, hi),
1398 token::OpenDelim(token::Brace) => {
1399 debug!("parse_trait_methods(): parsing provided method");
1400 let (inner_attrs, body) =
1401 p.parse_inner_attrs_and_block();
1402 let mut attrs = attrs;
1403 attrs.push_all(inner_attrs[]);
1404 ProvidedMethod(P(ast::Method {
1406 id: ast::DUMMY_NODE_ID,
1407 span: mk_sp(lo, hi),
1408 node: ast::MethDecl(ident,
1420 let token_str = p.this_token_to_string();
1421 p.fatal((format!("expected `;` or `{{`, found `{}`",
1429 /// Parse a possibly mutable type
1430 pub fn parse_mt(&mut self) -> MutTy {
1431 let mutbl = self.parse_mutability();
1432 let t = self.parse_ty();
1433 MutTy { ty: t, mutbl: mutbl }
1436 /// Parse [mut/const/imm] ID : TY
1437 /// now used only by obsolete record syntax parser...
1438 pub fn parse_ty_field(&mut self) -> TypeField {
1439 let lo = self.span.lo;
1440 let mutbl = self.parse_mutability();
1441 let id = self.parse_ident();
1442 self.expect(&token::Colon);
1443 let ty = self.parse_ty_sum();
1444 let hi = ty.span.hi;
1447 mt: MutTy { ty: ty, mutbl: mutbl },
1448 span: mk_sp(lo, hi),
1452 /// Parse optional return type [ -> TY ] in function decl
1453 pub fn parse_ret_ty(&mut self) -> FunctionRetTy {
1454 if self.eat(&token::RArrow) {
1455 if self.eat(&token::Not) {
1458 let t = self.parse_ty();
1460 // We used to allow `fn foo() -> &T + U`, but don't
1461 // anymore. If we see it, report a useful error. This
1462 // only makes sense because `parse_ret_ty` is only
1463 // used in fn *declarations*, not fn types or where
1464 // clauses (i.e., not when parsing something like
1465 // `FnMut() -> T + Send`, where the `+` is legal).
1466 if self.token == token::BinOp(token::Plus) {
1467 self.warn("deprecated syntax: `()` are required, see RFC 438 for details");
1473 let pos = self.span.lo;
1475 id: ast::DUMMY_NODE_ID,
1476 node: TyTup(vec![]),
1477 span: mk_sp(pos, pos),
1482 /// Parse a type in a context where `T1+T2` is allowed.
1483 pub fn parse_ty_sum(&mut self) -> P<Ty> {
1484 let lo = self.span.lo;
1485 let lhs = self.parse_ty();
1487 if !self.eat(&token::BinOp(token::Plus)) {
1491 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
1493 // In type grammar, `+` is treated like a binary operator,
1494 // and hence both L and R side are required.
1495 if bounds.len() == 0 {
1496 let last_span = self.last_span;
1497 self.span_err(last_span,
1498 "at least one type parameter bound \
1499 must be specified");
1502 let sp = mk_sp(lo, self.last_span.hi);
1503 let sum = ast::TyObjectSum(lhs, bounds);
1504 P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})
1508 pub fn parse_ty(&mut self) -> P<Ty> {
1509 maybe_whole!(no_clone self, NtTy);
1511 let lo = self.span.lo;
1513 let t = if self.check(&token::OpenDelim(token::Paren)) {
1516 // (t) is a parenthesized ty
1517 // (t,) is the type of a tuple with only one field,
1519 let mut ts = vec![];
1520 let mut last_comma = false;
1521 while self.token != token::CloseDelim(token::Paren) {
1522 ts.push(self.parse_ty_sum());
1523 if self.check(&token::Comma) {
1532 self.expect(&token::CloseDelim(token::Paren));
1533 if ts.len() == 1 && !last_comma {
1534 TyParen(ts.into_iter().nth(0).unwrap())
1538 } else if self.token == token::Tilde {
1541 let last_span = self.last_span;
1543 token::OpenDelim(token::Bracket) => self.obsolete(last_span, ObsoleteOwnedVector),
1544 _ => self.obsolete(last_span, ObsoleteOwnedType)
1546 TyTup(vec![self.parse_ty()])
1547 } else if self.check(&token::BinOp(token::Star)) {
1548 // STAR POINTER (bare pointer?)
1550 TyPtr(self.parse_ptr())
1551 } else if self.check(&token::OpenDelim(token::Bracket)) {
1553 self.expect(&token::OpenDelim(token::Bracket));
1554 let t = self.parse_ty_sum();
1556 // Parse the `; e` in `[ int; e ]`
1557 // where `e` is a const expression
1558 let t = match self.maybe_parse_fixed_length_of_vec() {
1560 Some(suffix) => TyFixedLengthVec(t, suffix)
1562 self.expect(&token::CloseDelim(token::Bracket));
1564 } else if self.check(&token::BinOp(token::And)) ||
1565 self.token == token::AndAnd {
1568 self.parse_borrowed_pointee()
1569 } else if self.token.is_keyword(keywords::For) {
1570 self.parse_for_in_type()
1571 } else if self.token_is_bare_fn_keyword() ||
1572 self.token_is_closure_keyword() {
1573 // BARE FUNCTION OR CLOSURE
1574 self.parse_ty_bare_fn_or_ty_closure(Vec::new())
1575 } else if self.check(&token::BinOp(token::Or)) ||
1576 self.token == token::OrOr ||
1577 (self.token == token::Lt &&
1578 self.look_ahead(1, |t| {
1579 *t == token::Gt || t.is_lifetime()
1582 self.parse_ty_closure(Vec::new())
1583 } else if self.eat_keyword(keywords::Typeof) {
1585 // In order to not be ambiguous, the type must be surrounded by parens.
1586 self.expect(&token::OpenDelim(token::Paren));
1587 let e = self.parse_expr();
1588 self.expect(&token::CloseDelim(token::Paren));
1590 } else if self.eat_keyword(keywords::Proc) {
1591 self.parse_proc_type(Vec::new())
1592 } else if self.check(&token::Lt) {
1593 // QUALIFIED PATH `<TYPE as TRAIT_REF>::item`
1595 let self_type = self.parse_ty_sum();
1596 self.expect_keyword(keywords::As);
1597 let trait_ref = self.parse_trait_ref();
1598 self.expect(&token::Gt);
1599 self.expect(&token::ModSep);
1600 let item_name = self.parse_ident();
1602 self_type: self_type,
1603 trait_ref: P(trait_ref),
1604 item_name: item_name,
1606 } else if self.check(&token::ModSep) ||
1607 self.token.is_ident() ||
1608 self.token.is_path() {
1610 self.parse_ty_path()
1611 } else if self.eat(&token::Underscore) {
1612 // TYPE TO BE INFERRED
1615 let this_token_str = self.this_token_to_string();
1616 let msg = format!("expected type, found `{}`", this_token_str);
1620 let sp = mk_sp(lo, self.last_span.hi);
1621 P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp})
1624 pub fn parse_borrowed_pointee(&mut self) -> Ty_ {
1625 // look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
1626 let opt_lifetime = self.parse_opt_lifetime();
1628 let mt = self.parse_mt();
1629 return TyRptr(opt_lifetime, mt);
1632 pub fn parse_ptr(&mut self) -> MutTy {
1633 let mutbl = if self.eat_keyword(keywords::Mut) {
1635 } else if self.eat_keyword(keywords::Const) {
1638 let span = self.last_span;
1640 "bare raw pointers are no longer allowed, you should \
1641 likely use `*mut T`, but otherwise `*T` is now \
1642 known as `*const T`");
1645 let t = self.parse_ty();
1646 MutTy { ty: t, mutbl: mutbl }
1649 pub fn is_named_argument(&mut self) -> bool {
1650 let offset = match self.token {
1651 token::BinOp(token::And) => 1,
1653 _ if self.token.is_keyword(keywords::Mut) => 1,
1657 debug!("parser is_named_argument offset:{}", offset);
1660 is_plain_ident_or_underscore(&self.token)
1661 && self.look_ahead(1, |t| *t == token::Colon)
1663 self.look_ahead(offset, |t| is_plain_ident_or_underscore(t))
1664 && self.look_ahead(offset + 1, |t| *t == token::Colon)
1668 /// This version of parse arg doesn't necessarily require
1669 /// identifier names.
1670 pub fn parse_arg_general(&mut self, require_name: bool) -> Arg {
1671 let pat = if require_name || self.is_named_argument() {
1672 debug!("parse_arg_general parse_pat (require_name:{})",
1674 let pat = self.parse_pat();
1676 self.expect(&token::Colon);
1679 debug!("parse_arg_general ident_to_pat");
1680 ast_util::ident_to_pat(ast::DUMMY_NODE_ID,
1682 special_idents::invalid)
1685 let t = self.parse_ty_sum();
1690 id: ast::DUMMY_NODE_ID,
1694 /// Parse a single function argument
1695 pub fn parse_arg(&mut self) -> Arg {
1696 self.parse_arg_general(true)
1699 /// Parse an argument in a lambda header e.g. |arg, arg|
1700 pub fn parse_fn_block_arg(&mut self) -> Arg {
1701 let pat = self.parse_pat();
1702 let t = if self.eat(&token::Colon) {
1706 id: ast::DUMMY_NODE_ID,
1708 span: mk_sp(self.span.lo, self.span.hi),
1714 id: ast::DUMMY_NODE_ID
1718 pub fn maybe_parse_fixed_length_of_vec(&mut self) -> Option<P<ast::Expr>> {
1719 if self.check(&token::Comma) &&
1720 self.look_ahead(1, |t| *t == token::DotDot) {
1723 Some(self.parse_expr_res(RESTRICTION_NO_DOTS))
1724 } else if self.check(&token::Semi) {
1726 Some(self.parse_expr())
1732 /// Matches token_lit = LIT_INTEGER | ...
1733 pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ {
1735 token::Interpolated(token::NtExpr(ref v)) => {
1737 ExprLit(ref lit) => { lit.node.clone() }
1738 _ => { self.unexpected_last(tok); }
1741 token::Literal(lit, suf) => {
1742 let (suffix_illegal, out) = match lit {
1743 token::Byte(i) => (true, LitByte(parse::byte_lit(i.as_str()).0)),
1744 token::Char(i) => (true, LitChar(parse::char_lit(i.as_str()).0)),
1746 // there are some valid suffixes for integer and
1747 // float literals, so all the handling is done
1749 token::Integer(s) => {
1750 (false, parse::integer_lit(s.as_str(),
1751 suf.as_ref().map(|s| s.as_str()),
1752 &self.sess.span_diagnostic,
1755 token::Float(s) => {
1756 (false, parse::float_lit(s.as_str(),
1757 suf.as_ref().map(|s| s.as_str()),
1758 &self.sess.span_diagnostic,
1764 LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str())[]),
1767 token::StrRaw(s, n) => {
1770 token::intern_and_get_ident(
1771 parse::raw_str_lit(s.as_str())[]),
1775 (true, LitBinary(parse::binary_lit(i.as_str()))),
1776 token::BinaryRaw(i, _) =>
1778 LitBinary(Rc::new(i.as_str().as_bytes().iter().map(|&x| x).collect()))),
1782 let sp = self.last_span;
1783 self.expect_no_suffix(sp, &*format!("{} literal", lit.short_name()), suf)
1788 _ => { self.unexpected_last(tok); }
1792 /// Matches lit = true | false | token_lit
1793 pub fn parse_lit(&mut self) -> Lit {
1794 let lo = self.span.lo;
1795 let lit = if self.eat_keyword(keywords::True) {
1797 } else if self.eat_keyword(keywords::False) {
1800 let token = self.bump_and_get();
1801 let lit = self.lit_from_token(&token);
1804 codemap::Spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
1807 /// matches '-' lit | lit
1808 pub fn parse_literal_maybe_minus(&mut self) -> P<Expr> {
1809 let minus_lo = self.span.lo;
1810 let minus_present = self.eat(&token::BinOp(token::Minus));
1812 let lo = self.span.lo;
1813 let literal = P(self.parse_lit());
1814 let hi = self.span.hi;
1815 let expr = self.mk_expr(lo, hi, ExprLit(literal));
1818 let minus_hi = self.span.hi;
1819 let unary = self.mk_unary(UnNeg, expr);
1820 self.mk_expr(minus_lo, minus_hi, unary)
1826 /// Parses a path and optional type parameter bounds, depending on the
1827 /// mode. The `mode` parameter determines whether lifetimes, types, and/or
1828 /// bounds are permitted and whether `::` must precede type parameter
1830 pub fn parse_path(&mut self, mode: PathParsingMode) -> ast::Path {
1831 // Check for a whole path...
1832 let found = match self.token {
1833 token::Interpolated(token::NtPath(_)) => Some(self.bump_and_get()),
1836 if let Some(token::Interpolated(token::NtPath(box path))) = found {
1840 let lo = self.span.lo;
1841 let is_global = self.eat(&token::ModSep);
1843 // Parse any number of segments and bound sets. A segment is an
1844 // identifier followed by an optional lifetime and a set of types.
1845 // A bound set is a set of type parameter bounds.
1846 let segments = match mode {
1847 LifetimeAndTypesWithoutColons => {
1848 self.parse_path_segments_without_colons()
1850 LifetimeAndTypesWithColons => {
1851 self.parse_path_segments_with_colons()
1854 self.parse_path_segments_without_types()
1858 // Assemble the span.
1859 let span = mk_sp(lo, self.last_span.hi);
1861 // Assemble the result.
1870 /// - `a::b<T,U>::c<V,W>`
1871 /// - `a::b<T,U>::c(V) -> W`
1872 /// - `a::b<T,U>::c(V)`
1873 pub fn parse_path_segments_without_colons(&mut self) -> Vec<ast::PathSegment> {
1874 let mut segments = Vec::new();
1876 // First, parse an identifier.
1877 let identifier = self.parse_ident();
1879 // Parse types, optionally.
1880 let parameters = if self.eat_lt(false) {
1881 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1883 ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1884 lifetimes: lifetimes,
1885 types: OwnedSlice::from_vec(types),
1886 bindings: OwnedSlice::from_vec(bindings),
1888 } else if self.eat(&token::OpenDelim(token::Paren)) {
1889 let inputs = self.parse_seq_to_end(
1890 &token::CloseDelim(token::Paren),
1891 seq_sep_trailing_allowed(token::Comma),
1892 |p| p.parse_ty_sum());
1894 let output_ty = if self.eat(&token::RArrow) {
1895 Some(self.parse_ty())
1900 ast::ParenthesizedParameters(ast::ParenthesizedParameterData {
1905 ast::PathParameters::none()
1908 // Assemble and push the result.
1909 segments.push(ast::PathSegment { identifier: identifier,
1910 parameters: parameters });
1912 // Continue only if we see a `::`
1913 if !self.eat(&token::ModSep) {
1920 /// - `a::b::<T,U>::c`
1921 pub fn parse_path_segments_with_colons(&mut self) -> Vec<ast::PathSegment> {
1922 let mut segments = Vec::new();
1924 // First, parse an identifier.
1925 let identifier = self.parse_ident();
1927 // If we do not see a `::`, stop.
1928 if !self.eat(&token::ModSep) {
1929 segments.push(ast::PathSegment {
1930 identifier: identifier,
1931 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1932 lifetimes: Vec::new(),
1933 types: OwnedSlice::empty(),
1934 bindings: OwnedSlice::empty(),
1940 // Check for a type segment.
1941 if self.eat_lt(false) {
1942 // Consumed `a::b::<`, go look for types
1943 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1944 segments.push(ast::PathSegment {
1945 identifier: identifier,
1946 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1947 lifetimes: lifetimes,
1948 types: OwnedSlice::from_vec(types),
1949 bindings: OwnedSlice::from_vec(bindings),
1953 // Consumed `a::b::<T,U>`, check for `::` before proceeding
1954 if !self.eat(&token::ModSep) {
1958 // Consumed `a::`, go look for `b`
1959 segments.push(ast::PathSegment {
1960 identifier: identifier,
1961 parameters: ast::PathParameters::none(),
1970 pub fn parse_path_segments_without_types(&mut self) -> Vec<ast::PathSegment> {
1971 let mut segments = Vec::new();
1973 // First, parse an identifier.
1974 let identifier = self.parse_ident();
1976 // Assemble and push the result.
1977 segments.push(ast::PathSegment {
1978 identifier: identifier,
1979 parameters: ast::PathParameters::none()
1982 // If we do not see a `::`, stop.
1983 if !self.eat(&token::ModSep) {
1989 /// parses 0 or 1 lifetime
1990 pub fn parse_opt_lifetime(&mut self) -> Option<ast::Lifetime> {
1992 token::Lifetime(..) => {
1993 Some(self.parse_lifetime())
2001 /// Parses a single lifetime
2002 /// Matches lifetime = LIFETIME
2003 pub fn parse_lifetime(&mut self) -> ast::Lifetime {
2005 token::Lifetime(i) => {
2006 let span = self.span;
2008 return ast::Lifetime {
2009 id: ast::DUMMY_NODE_ID,
2015 self.fatal(format!("expected a lifetime name")[]);
2020 /// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
2021 /// lifetime [':' lifetimes]`
2022 pub fn parse_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
2024 let mut res = Vec::new();
2027 token::Lifetime(_) => {
2028 let lifetime = self.parse_lifetime();
2030 if self.eat(&token::Colon) {
2031 self.parse_lifetimes(token::BinOp(token::Plus))
2035 res.push(ast::LifetimeDef { lifetime: lifetime,
2045 token::Comma => { self.bump(); }
2046 token::Gt => { return res; }
2047 token::BinOp(token::Shr) => { return res; }
2049 let this_token_str = self.this_token_to_string();
2050 let msg = format!("expected `,` or `>` after lifetime \
2059 /// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
2060 /// one too, but putting that in there messes up the grammar....
2062 /// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
2063 /// either a comma or `>`. Used when parsing type parameter lists, where we expect something
2064 /// like `<'a, 'b, T>`.
2065 pub fn parse_lifetimes(&mut self, sep: token::Token) -> Vec<ast::Lifetime> {
2067 let mut res = Vec::new();
2070 token::Lifetime(_) => {
2071 res.push(self.parse_lifetime());
2078 if self.token != sep {
2086 /// Parse mutability declaration (mut/const/imm)
2087 pub fn parse_mutability(&mut self) -> Mutability {
2088 if self.eat_keyword(keywords::Mut) {
2095 /// Parse ident COLON expr
2096 pub fn parse_field(&mut self) -> Field {
2097 let lo = self.span.lo;
2098 let i = self.parse_ident();
2099 let hi = self.last_span.hi;
2100 self.expect(&token::Colon);
2101 let e = self.parse_expr();
2103 ident: spanned(lo, hi, i),
2104 span: mk_sp(lo, e.span.hi),
2109 pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr> {
2111 id: ast::DUMMY_NODE_ID,
2113 span: mk_sp(lo, hi),
2117 pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::Expr_ {
2118 ExprUnary(unop, expr)
2121 pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2122 ExprBinary(binop, lhs, rhs)
2125 pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::Expr_ {
2129 fn mk_method_call(&mut self,
2130 ident: ast::SpannedIdent,
2134 ExprMethodCall(ident, tps, args)
2137 pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::Expr_ {
2138 ExprIndex(expr, idx)
2141 pub fn mk_slice(&mut self,
2143 start: Option<P<Expr>>,
2144 end: Option<P<Expr>>,
2147 // FIXME: we could give more accurate span info here.
2148 let (lo, hi) = match (&start, &end) {
2149 (&Some(ref s), &Some(ref e)) => (s.span.lo, e.span.hi),
2150 (&Some(ref s), &None) => (s.span.lo, s.span.hi),
2151 (&None, &Some(ref e)) => (e.span.lo, e.span.hi),
2152 (&None, &None) => (DUMMY_SP.lo, DUMMY_SP.hi),
2154 ExprIndex(expr, self.mk_expr(lo, hi, ExprRange(start, end)))
2157 pub fn mk_range(&mut self,
2159 end: Option<P<Expr>>)
2161 ExprRange(Some(start), end)
2164 pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::Expr_ {
2165 ExprField(expr, ident)
2168 pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<uint>) -> ast::Expr_ {
2169 ExprTupField(expr, idx)
2172 pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2173 lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2174 ExprAssignOp(binop, lhs, rhs)
2177 pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr> {
2179 id: ast::DUMMY_NODE_ID,
2180 node: ExprMac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
2181 span: mk_sp(lo, hi),
2185 pub fn mk_lit_u32(&mut self, i: u32) -> P<Expr> {
2186 let span = &self.span;
2187 let lv_lit = P(codemap::Spanned {
2188 node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)),
2193 id: ast::DUMMY_NODE_ID,
2194 node: ExprLit(lv_lit),
2199 fn expect_open_delim(&mut self) -> token::DelimToken {
2201 token::OpenDelim(delim) => {
2205 _ => self.fatal("expected open delimiter"),
2209 /// At the bottom (top?) of the precedence hierarchy,
2210 /// parse things like parenthesized exprs,
2211 /// macros, return, etc.
2212 pub fn parse_bottom_expr(&mut self) -> P<Expr> {
2213 maybe_whole_expr!(self);
2215 let lo = self.span.lo;
2216 let mut hi = self.span.hi;
2221 token::OpenDelim(token::Paren) => {
2224 // (e) is parenthesized e
2225 // (e,) is a tuple with only one field, e
2226 let mut es = vec![];
2227 let mut trailing_comma = false;
2228 while self.token != token::CloseDelim(token::Paren) {
2229 es.push(self.parse_expr());
2230 self.commit_expr(&**es.last().unwrap(), &[],
2231 &[token::Comma, token::CloseDelim(token::Paren)]);
2232 if self.check(&token::Comma) {
2233 trailing_comma = true;
2237 trailing_comma = false;
2244 return if es.len() == 1 && !trailing_comma {
2245 self.mk_expr(lo, hi, ExprParen(es.into_iter().nth(0).unwrap()))
2247 self.mk_expr(lo, hi, ExprTup(es))
2250 token::OpenDelim(token::Brace) => {
2252 let blk = self.parse_block_tail(lo, DefaultBlock);
2253 return self.mk_expr(blk.span.lo, blk.span.hi,
2256 token::BinOp(token::Or) | token::OrOr => {
2257 return self.parse_lambda_expr(CaptureByRef);
2259 // FIXME #13626: Should be able to stick in
2260 // token::SELF_KEYWORD_NAME
2261 token::Ident(id @ ast::Ident {
2262 name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
2264 }, token::Plain) => {
2266 let path = ast_util::ident_to_path(mk_sp(lo, hi), id);
2267 ex = ExprPath(path);
2268 hi = self.last_span.hi;
2270 token::OpenDelim(token::Bracket) => {
2273 if self.check(&token::CloseDelim(token::Bracket)) {
2276 ex = ExprVec(Vec::new());
2279 let first_expr = self.parse_expr();
2280 if self.check(&token::Comma) &&
2281 self.look_ahead(1, |t| *t == token::DotDot) {
2282 // Repeating vector syntax: [ 0, ..512 ]
2285 let count = self.parse_expr();
2286 self.expect(&token::CloseDelim(token::Bracket));
2287 ex = ExprRepeat(first_expr, count);
2288 } else if self.check(&token::Semi) {
2289 // Repeating vector syntax: [ 0; 512 ]
2291 let count = self.parse_expr();
2292 self.expect(&token::CloseDelim(token::Bracket));
2293 ex = ExprRepeat(first_expr, count);
2294 } else if self.check(&token::Comma) {
2295 // Vector with two or more elements.
2297 let remaining_exprs = self.parse_seq_to_end(
2298 &token::CloseDelim(token::Bracket),
2299 seq_sep_trailing_allowed(token::Comma),
2302 let mut exprs = vec!(first_expr);
2303 exprs.extend(remaining_exprs.into_iter());
2304 ex = ExprVec(exprs);
2306 // Vector with one element.
2307 self.expect(&token::CloseDelim(token::Bracket));
2308 ex = ExprVec(vec!(first_expr));
2311 hi = self.last_span.hi;
2314 if self.eat_keyword(keywords::Move) {
2315 return self.parse_lambda_expr(CaptureByValue);
2317 if self.eat_keyword(keywords::Proc) {
2318 let span = self.last_span;
2319 let _ = self.parse_proc_decl();
2320 let _ = self.parse_expr();
2321 return self.obsolete_expr(span, ObsoleteProcExpr);
2323 if self.eat_keyword(keywords::If) {
2324 return self.parse_if_expr();
2326 if self.eat_keyword(keywords::For) {
2327 return self.parse_for_expr(None);
2329 if self.eat_keyword(keywords::While) {
2330 return self.parse_while_expr(None);
2332 if self.token.is_lifetime() {
2333 let lifetime = self.get_lifetime();
2335 self.expect(&token::Colon);
2336 if self.eat_keyword(keywords::While) {
2337 return self.parse_while_expr(Some(lifetime))
2339 if self.eat_keyword(keywords::For) {
2340 return self.parse_for_expr(Some(lifetime))
2342 if self.eat_keyword(keywords::Loop) {
2343 return self.parse_loop_expr(Some(lifetime))
2345 self.fatal("expected `while`, `for`, or `loop` after a label")
2347 if self.eat_keyword(keywords::Loop) {
2348 return self.parse_loop_expr(None);
2350 if self.eat_keyword(keywords::Continue) {
2351 let lo = self.span.lo;
2352 let ex = if self.token.is_lifetime() {
2353 let lifetime = self.get_lifetime();
2355 ExprAgain(Some(lifetime))
2359 let hi = self.span.hi;
2360 return self.mk_expr(lo, hi, ex);
2362 if self.eat_keyword(keywords::Match) {
2363 return self.parse_match_expr();
2365 if self.eat_keyword(keywords::Unsafe) {
2366 return self.parse_block_expr(
2368 UnsafeBlock(ast::UserProvided));
2370 if self.eat_keyword(keywords::Return) {
2371 // RETURN expression
2372 if self.token.can_begin_expr() {
2373 let e = self.parse_expr();
2375 ex = ExprRet(Some(e));
2379 } else if self.eat_keyword(keywords::Break) {
2381 if self.token.is_lifetime() {
2382 let lifetime = self.get_lifetime();
2384 ex = ExprBreak(Some(lifetime));
2386 ex = ExprBreak(None);
2389 } else if self.check(&token::ModSep) ||
2390 self.token.is_ident() &&
2391 !self.token.is_keyword(keywords::True) &&
2392 !self.token.is_keyword(keywords::False) {
2394 self.parse_path(LifetimeAndTypesWithColons);
2396 // `!`, as an operator, is prefix, so we know this isn't that
2397 if self.check(&token::Not) {
2398 // MACRO INVOCATION expression
2401 let delim = self.expect_open_delim();
2402 let tts = self.parse_seq_to_end(
2403 &token::CloseDelim(delim),
2405 |p| p.parse_token_tree());
2406 let hi = self.span.hi;
2408 return self.mk_mac_expr(lo,
2414 if self.check(&token::OpenDelim(token::Brace)) {
2415 // This is a struct literal, unless we're prohibited
2416 // from parsing struct literals here.
2417 if !self.restrictions.contains(RESTRICTION_NO_STRUCT_LITERAL) {
2418 // It's a struct literal.
2420 let mut fields = Vec::new();
2421 let mut base = None;
2423 while self.token != token::CloseDelim(token::Brace) {
2424 if self.eat(&token::DotDot) {
2425 base = Some(self.parse_expr());
2429 fields.push(self.parse_field());
2430 self.commit_expr(&*fields.last().unwrap().expr,
2432 &[token::CloseDelim(token::Brace)]);
2435 if fields.len() == 0 && base.is_none() {
2436 let last_span = self.last_span;
2437 self.span_err(last_span,
2438 "structure literal must either \
2439 have at least one field or use \
2440 functional structure update \
2445 self.expect(&token::CloseDelim(token::Brace));
2446 ex = ExprStruct(pth, fields, base);
2447 return self.mk_expr(lo, hi, ex);
2454 // other literal expression
2455 let lit = self.parse_lit();
2457 ex = ExprLit(P(lit));
2462 return self.mk_expr(lo, hi, ex);
2465 /// Parse a block or unsafe block
2466 pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode)
2468 self.expect(&token::OpenDelim(token::Brace));
2469 let blk = self.parse_block_tail(lo, blk_mode);
2470 return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
2473 /// parse a.b or a(13) or a[4] or just a
2474 pub fn parse_dot_or_call_expr(&mut self) -> P<Expr> {
2475 let b = self.parse_bottom_expr();
2476 self.parse_dot_or_call_expr_with(b)
2479 pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
2485 if self.eat(&token::Dot) {
2487 token::Ident(i, _) => {
2488 let dot = self.last_span.hi;
2491 let (_, tys, bindings) = if self.eat(&token::ModSep) {
2493 self.parse_generic_values_after_lt()
2495 (Vec::new(), Vec::new(), Vec::new())
2498 if bindings.len() > 0 {
2499 let last_span = self.last_span;
2500 self.span_err(last_span, "type bindings are only permitted on trait paths");
2503 // expr.f() method call
2505 token::OpenDelim(token::Paren) => {
2506 let mut es = self.parse_unspanned_seq(
2507 &token::OpenDelim(token::Paren),
2508 &token::CloseDelim(token::Paren),
2509 seq_sep_trailing_allowed(token::Comma),
2512 hi = self.last_span.hi;
2515 let id = spanned(dot, hi, i);
2516 let nd = self.mk_method_call(id, tys, es);
2517 e = self.mk_expr(lo, hi, nd);
2520 if !tys.is_empty() {
2521 let last_span = self.last_span;
2522 self.span_err(last_span,
2523 "field expressions may not \
2524 have type parameters");
2527 let id = spanned(dot, hi, i);
2528 let field = self.mk_field(e, id);
2529 e = self.mk_expr(lo, hi, field);
2533 token::Literal(token::Integer(n), suf) => {
2536 // A tuple index may not have a suffix
2537 self.expect_no_suffix(sp, "tuple index", suf);
2539 let dot = self.last_span.hi;
2543 let index = n.as_str().parse::<uint>();
2546 let id = spanned(dot, hi, n);
2547 let field = self.mk_tup_field(e, id);
2548 e = self.mk_expr(lo, hi, field);
2551 let last_span = self.last_span;
2552 self.span_err(last_span, "invalid tuple or tuple struct index");
2556 token::Literal(token::Float(n), _suf) => {
2558 let last_span = self.last_span;
2559 let fstr = n.as_str();
2560 self.span_err(last_span,
2561 format!("unexpected token: `{}`", n.as_str())[]);
2562 if fstr.chars().all(|x| "0123456789.".contains_char(x)) {
2563 let float = match fstr.parse::<f64>() {
2567 self.span_help(last_span,
2568 format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
2569 float.trunc() as uint,
2570 float.fract().to_string()[1..])[]);
2572 self.abort_if_errors();
2575 _ => self.unexpected()
2579 if self.expr_is_complete(&*e) { break; }
2582 token::OpenDelim(token::Paren) => {
2583 let es = self.parse_unspanned_seq(
2584 &token::OpenDelim(token::Paren),
2585 &token::CloseDelim(token::Paren),
2586 seq_sep_trailing_allowed(token::Comma),
2589 hi = self.last_span.hi;
2591 let nd = self.mk_call(e, es);
2592 e = self.mk_expr(lo, hi, nd);
2596 // Could be either an index expression or a slicing expression.
2597 // Any slicing non-terminal can have a mutable version with `mut`
2598 // after the opening square bracket.
2599 token::OpenDelim(token::Bracket) => {
2601 let mutbl = if self.eat_keyword(keywords::Mut) {
2608 token::CloseDelim(token::Bracket) => {
2611 let slice = self.mk_slice(e, None, None, mutbl);
2612 e = self.mk_expr(lo, hi, slice)
2619 token::CloseDelim(token::Bracket) => {
2622 let slice = self.mk_slice(e, None, None, mutbl);
2623 e = self.mk_expr(lo, hi, slice);
2625 self.span_err(e.span, "incorrect slicing expression: `[..]`");
2626 self.span_note(e.span,
2627 "use `expr[]` to construct a slice of the whole of expr");
2632 let e2 = self.parse_expr();
2633 self.commit_expr_expecting(&*e2, token::CloseDelim(token::Bracket));
2634 let slice = self.mk_slice(e, None, Some(e2), mutbl);
2635 e = self.mk_expr(lo, hi, slice)
2639 // e[e] | e[e..] | e[e..e]
2641 let ix = self.parse_expr_res(RESTRICTION_NO_DOTS);
2646 let e2 = match self.token {
2648 token::CloseDelim(token::Bracket) => {
2654 let e2 = self.parse_expr_res(RESTRICTION_NO_DOTS);
2655 self.commit_expr_expecting(&*e2,
2656 token::CloseDelim(token::Bracket));
2661 let slice = self.mk_slice(e, Some(ix), e2, mutbl);
2662 e = self.mk_expr(lo, hi, slice)
2666 if mutbl == ast::MutMutable {
2667 self.span_err(e.span,
2668 "`mut` keyword is invalid in index expressions");
2671 self.commit_expr_expecting(&*ix, token::CloseDelim(token::Bracket));
2672 let index = self.mk_index(e, ix);
2673 e = self.mk_expr(lo, hi, index)
2680 // A range expression, either `expr..expr` or `expr..`.
2681 token::DotDot if !self.restrictions.contains(RESTRICTION_NO_DOTS) => {
2684 let opt_end = if self.token.can_begin_expr() {
2685 let end = self.parse_expr_res(RESTRICTION_NO_DOTS);
2691 let hi = self.span.hi;
2692 let range = self.mk_range(e, opt_end);
2693 return self.mk_expr(lo, hi, range);
2701 /// Parse an optional separator followed by a Kleene-style
2702 /// repetition token (+ or *).
2703 pub fn parse_sep_and_kleene_op(&mut self) -> (Option<token::Token>, ast::KleeneOp) {
2704 fn parse_kleene_op(parser: &mut Parser) -> Option<ast::KleeneOp> {
2705 match parser.token {
2706 token::BinOp(token::Star) => {
2708 Some(ast::ZeroOrMore)
2710 token::BinOp(token::Plus) => {
2712 Some(ast::OneOrMore)
2718 match parse_kleene_op(self) {
2719 Some(kleene_op) => return (None, kleene_op),
2723 let separator = self.bump_and_get();
2724 match parse_kleene_op(self) {
2725 Some(zerok) => (Some(separator), zerok),
2726 None => self.fatal("expected `*` or `+`")
2730 /// parse a single token tree from the input.
2731 pub fn parse_token_tree(&mut self) -> TokenTree {
2732 // FIXME #6994: currently, this is too eager. It
2733 // parses token trees but also identifies TtSequence's
2734 // and token::SubstNt's; it's too early to know yet
2735 // whether something will be a nonterminal or a seq
2737 maybe_whole!(deref self, NtTT);
2739 // this is the fall-through for the 'match' below.
2740 // invariants: the current token is not a left-delimiter,
2741 // not an EOF, and not the desired right-delimiter (if
2742 // it were, parse_seq_to_before_end would have prevented
2743 // reaching this point.
2744 fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree {
2745 maybe_whole!(deref p, NtTT);
2747 token::CloseDelim(_) => {
2748 // This is a conservative error: only report the last unclosed delimiter. The
2749 // previous unclosed delimiters could actually be closed! The parser just hasn't
2750 // gotten to them yet.
2751 match p.open_braces.last() {
2753 Some(&sp) => p.span_note(sp, "unclosed delimiter"),
2755 let token_str = p.this_token_to_string();
2756 p.fatal(format!("incorrect close delimiter: `{}`",
2759 /* we ought to allow different depths of unquotation */
2760 token::Dollar if p.quote_depth > 0u => {
2764 if p.token == token::OpenDelim(token::Paren) {
2765 let seq = p.parse_seq(
2766 &token::OpenDelim(token::Paren),
2767 &token::CloseDelim(token::Paren),
2769 |p| p.parse_token_tree()
2771 let (sep, repeat) = p.parse_sep_and_kleene_op();
2772 let seq = match seq {
2773 Spanned { node, .. } => node,
2775 let name_num = macro_parser::count_names(seq[]);
2776 TtSequence(mk_sp(sp.lo, p.span.hi),
2777 Rc::new(SequenceRepetition {
2781 num_captures: name_num
2784 // A nonterminal that matches or not
2785 let namep = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2786 let name = p.parse_ident();
2787 if p.token == token::Colon && p.look_ahead(1, |t| t.is_ident()) {
2789 let kindp = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2790 let nt_kind = p.parse_ident();
2791 let m = TtToken(sp, MatchNt(name, nt_kind, namep, kindp));
2794 TtToken(sp, SubstNt(name, namep))
2799 TtToken(p.span, p.bump_and_get())
2806 let open_braces = self.open_braces.clone();
2807 for sp in open_braces.iter() {
2808 self.span_help(*sp, "did you mean to close this delimiter?");
2810 // There shouldn't really be a span, but it's easier for the test runner
2811 // if we give it one
2812 self.fatal("this file contains an un-closed delimiter ");
2814 token::OpenDelim(delim) => {
2815 // The span for beginning of the delimited section
2816 let pre_span = self.span;
2818 // Parse the open delimiter.
2819 self.open_braces.push(self.span);
2820 let open_span = self.span;
2823 // Parse the token trees within the delimeters
2824 let tts = self.parse_seq_to_before_end(
2825 &token::CloseDelim(delim),
2827 |p| p.parse_token_tree()
2830 // Parse the close delimiter.
2831 let close_span = self.span;
2833 self.open_braces.pop().unwrap();
2835 // Expand to cover the entire delimited token tree
2836 let span = Span { hi: self.span.hi, ..pre_span };
2838 TtDelimited(span, Rc::new(Delimited {
2840 open_span: open_span,
2842 close_span: close_span,
2845 _ => parse_non_delim_tt_tok(self),
2849 // parse a stream of tokens into a list of TokenTree's,
2851 pub fn parse_all_token_trees(&mut self) -> Vec<TokenTree> {
2852 let mut tts = Vec::new();
2853 while self.token != token::Eof {
2854 tts.push(self.parse_token_tree());
2859 /// Parse a prefix-operator expr
2860 pub fn parse_prefix_expr(&mut self) -> P<Expr> {
2861 let lo = self.span.lo;
2868 let e = self.parse_prefix_expr();
2870 ex = self.mk_unary(UnNot, e);
2872 token::BinOp(token::Minus) => {
2874 let e = self.parse_prefix_expr();
2876 ex = self.mk_unary(UnNeg, e);
2878 token::BinOp(token::Star) => {
2880 let e = self.parse_prefix_expr();
2882 ex = self.mk_unary(UnDeref, e);
2884 token::BinOp(token::And) | token::AndAnd => {
2886 let m = self.parse_mutability();
2887 let e = self.parse_prefix_expr();
2889 ex = ExprAddrOf(m, e);
2893 let last_span = self.last_span;
2895 token::OpenDelim(token::Bracket) => {
2896 self.obsolete(last_span, ObsoleteOwnedVector)
2898 _ => self.obsolete(last_span, ObsoleteOwnedExpr)
2901 let e = self.parse_prefix_expr();
2903 ex = self.mk_unary(UnUniq, e);
2905 token::Ident(_, _) => {
2906 if !self.token.is_keyword(keywords::Box) {
2907 return self.parse_dot_or_call_expr();
2910 let lo = self.span.lo;
2914 // Check for a place: `box(PLACE) EXPR`.
2915 if self.eat(&token::OpenDelim(token::Paren)) {
2916 // Support `box() EXPR` as the default.
2917 if !self.eat(&token::CloseDelim(token::Paren)) {
2918 let place = self.parse_expr();
2919 self.expect(&token::CloseDelim(token::Paren));
2920 // Give a suggestion to use `box()` when a parenthesised expression is used
2921 if !self.token.can_begin_expr() {
2922 let span = self.span;
2923 let this_token_to_string = self.this_token_to_string();
2925 format!("expected expression, found `{}`",
2926 this_token_to_string)[]);
2927 let box_span = mk_sp(lo, self.last_span.hi);
2928 self.span_help(box_span,
2929 "perhaps you meant `box() (foo)` instead?");
2930 self.abort_if_errors();
2932 let subexpression = self.parse_prefix_expr();
2933 hi = subexpression.span.hi;
2934 ex = ExprBox(Some(place), subexpression);
2935 return self.mk_expr(lo, hi, ex);
2939 // Otherwise, we use the unique pointer default.
2940 let subexpression = self.parse_prefix_expr();
2941 hi = subexpression.span.hi;
2942 // FIXME (pnkfelix): After working out kinks with box
2943 // desugaring, should be `ExprBox(None, subexpression)`
2945 ex = self.mk_unary(UnUniq, subexpression);
2947 _ => return self.parse_dot_or_call_expr()
2949 return self.mk_expr(lo, hi, ex);
2952 /// Parse an expression of binops
2953 pub fn parse_binops(&mut self) -> P<Expr> {
2954 let prefix_expr = self.parse_prefix_expr();
2955 self.parse_more_binops(prefix_expr, 0)
2958 /// Parse an expression of binops of at least min_prec precedence
2959 pub fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: uint) -> P<Expr> {
2960 if self.expr_is_complete(&*lhs) { return lhs; }
2962 // Prevent dynamic borrow errors later on by limiting the
2963 // scope of the borrows.
2964 if self.token == token::BinOp(token::Or) &&
2965 self.restrictions.contains(RESTRICTION_NO_BAR_OP) {
2968 self.expected_tokens.push(TokenType::Operator);
2970 let cur_opt = self.token.to_binop();
2973 let cur_prec = operator_prec(cur_op);
2974 if cur_prec > min_prec {
2976 let expr = self.parse_prefix_expr();
2977 let rhs = self.parse_more_binops(expr, cur_prec);
2978 let lhs_span = lhs.span;
2979 let rhs_span = rhs.span;
2980 let binary = self.mk_binary(cur_op, lhs, rhs);
2981 let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary);
2982 self.parse_more_binops(bin, min_prec)
2988 if as_prec > min_prec && self.eat_keyword(keywords::As) {
2989 let rhs = self.parse_ty();
2990 let _as = self.mk_expr(lhs.span.lo,
2992 ExprCast(lhs, rhs));
2993 self.parse_more_binops(_as, min_prec)
3001 /// Parse an assignment expression....
3002 /// actually, this seems to be the main entry point for
3003 /// parsing an arbitrary expression.
3004 pub fn parse_assign_expr(&mut self) -> P<Expr> {
3005 let lo = self.span.lo;
3006 let lhs = self.parse_binops();
3007 let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL;
3011 let rhs = self.parse_expr_res(restrictions);
3012 self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
3014 token::BinOpEq(op) => {
3016 let rhs = self.parse_expr_res(restrictions);
3017 let aop = match op {
3018 token::Plus => BiAdd,
3019 token::Minus => BiSub,
3020 token::Star => BiMul,
3021 token::Slash => BiDiv,
3022 token::Percent => BiRem,
3023 token::Caret => BiBitXor,
3024 token::And => BiBitAnd,
3025 token::Or => BiBitOr,
3026 token::Shl => BiShl,
3029 let rhs_span = rhs.span;
3030 let assign_op = self.mk_assign_op(aop, lhs, rhs);
3031 self.mk_expr(lo, rhs_span.hi, assign_op)
3039 /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3040 pub fn parse_if_expr(&mut self) -> P<Expr> {
3041 if self.token.is_keyword(keywords::Let) {
3042 return self.parse_if_let_expr();
3044 let lo = self.last_span.lo;
3045 let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3046 let thn = self.parse_block();
3047 let mut els: Option<P<Expr>> = None;
3048 let mut hi = thn.span.hi;
3049 if self.eat_keyword(keywords::Else) {
3050 let elexpr = self.parse_else_expr();
3051 hi = elexpr.span.hi;
3054 self.mk_expr(lo, hi, ExprIf(cond, thn, els))
3057 /// Parse an 'if let' expression ('if' token already eaten)
3058 pub fn parse_if_let_expr(&mut self) -> P<Expr> {
3059 let lo = self.last_span.lo;
3060 self.expect_keyword(keywords::Let);
3061 let pat = self.parse_pat();
3062 self.expect(&token::Eq);
3063 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3064 let thn = self.parse_block();
3065 let (hi, els) = if self.eat_keyword(keywords::Else) {
3066 let expr = self.parse_else_expr();
3067 (expr.span.hi, Some(expr))
3071 self.mk_expr(lo, hi, ExprIfLet(pat, expr, thn, els))
3075 pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
3078 let lo = self.span.lo;
3079 let (decl, optional_unboxed_closure_kind) =
3080 self.parse_fn_block_decl();
3081 let body = self.parse_expr();
3082 let fakeblock = P(ast::Block {
3083 id: ast::DUMMY_NODE_ID,
3084 view_items: Vec::new(),
3088 rules: DefaultBlock,
3094 ExprClosure(capture_clause, optional_unboxed_closure_kind, decl, fakeblock))
3097 pub fn parse_else_expr(&mut self) -> P<Expr> {
3098 if self.eat_keyword(keywords::If) {
3099 return self.parse_if_expr();
3101 let blk = self.parse_block();
3102 return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
3106 /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3107 pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3108 // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3110 let lo = self.last_span.lo;
3111 let pat = self.parse_pat();
3112 self.expect_keyword(keywords::In);
3113 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3114 let loop_block = self.parse_block();
3115 let hi = self.span.hi;
3117 self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident))
3120 /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3121 pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3122 if self.token.is_keyword(keywords::Let) {
3123 return self.parse_while_let_expr(opt_ident);
3125 let lo = self.last_span.lo;
3126 let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3127 let body = self.parse_block();
3128 let hi = body.span.hi;
3129 return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
3132 /// Parse a 'while let' expression ('while' token already eaten)
3133 pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3134 let lo = self.last_span.lo;
3135 self.expect_keyword(keywords::Let);
3136 let pat = self.parse_pat();
3137 self.expect(&token::Eq);
3138 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3139 let body = self.parse_block();
3140 let hi = body.span.hi;
3141 return self.mk_expr(lo, hi, ExprWhileLet(pat, expr, body, opt_ident));
3144 pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3145 let lo = self.last_span.lo;
3146 let body = self.parse_block();
3147 let hi = body.span.hi;
3148 self.mk_expr(lo, hi, ExprLoop(body, opt_ident))
3151 fn parse_match_expr(&mut self) -> P<Expr> {
3152 let lo = self.last_span.lo;
3153 let discriminant = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3154 self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace));
3155 let mut arms: Vec<Arm> = Vec::new();
3156 while self.token != token::CloseDelim(token::Brace) {
3157 arms.push(self.parse_arm());
3159 let hi = self.span.hi;
3161 return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchSource::Normal));
3164 pub fn parse_arm(&mut self) -> Arm {
3165 let attrs = self.parse_outer_attributes();
3166 let pats = self.parse_pats();
3167 let mut guard = None;
3168 if self.eat_keyword(keywords::If) {
3169 guard = Some(self.parse_expr());
3171 self.expect(&token::FatArrow);
3172 let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3175 !classify::expr_is_simple_block(&*expr)
3176 && self.token != token::CloseDelim(token::Brace);
3179 self.commit_expr(&*expr, &[token::Comma], &[token::CloseDelim(token::Brace)]);
3181 self.eat(&token::Comma);
3192 /// Parse an expression
3193 pub fn parse_expr(&mut self) -> P<Expr> {
3194 return self.parse_expr_res(UNRESTRICTED);
3197 /// Parse an expression, subject to the given restrictions
3198 pub fn parse_expr_res(&mut self, r: Restrictions) -> P<Expr> {
3199 let old = self.restrictions;
3200 self.restrictions = r;
3201 let e = self.parse_assign_expr();
3202 self.restrictions = old;
3206 /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3207 fn parse_initializer(&mut self) -> Option<P<Expr>> {
3208 if self.check(&token::Eq) {
3210 Some(self.parse_expr())
3216 /// Parse patterns, separated by '|' s
3217 fn parse_pats(&mut self) -> Vec<P<Pat>> {
3218 let mut pats = Vec::new();
3220 pats.push(self.parse_pat());
3221 if self.check(&token::BinOp(token::Or)) { self.bump(); }
3222 else { return pats; }
3226 fn parse_pat_vec_elements(
3228 ) -> (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>) {
3229 let mut before = Vec::new();
3230 let mut slice = None;
3231 let mut after = Vec::new();
3232 let mut first = true;
3233 let mut before_slice = true;
3235 while self.token != token::CloseDelim(token::Bracket) {
3239 self.expect(&token::Comma);
3241 if self.token == token::CloseDelim(token::Bracket)
3242 && (before_slice || after.len() != 0) {
3248 if self.check(&token::DotDot) {
3251 if self.check(&token::Comma) ||
3252 self.check(&token::CloseDelim(token::Bracket)) {
3253 slice = Some(P(ast::Pat {
3254 id: ast::DUMMY_NODE_ID,
3255 node: PatWild(PatWildMulti),
3258 before_slice = false;
3260 let _ = self.parse_pat();
3261 let span = self.span;
3262 self.obsolete(span, ObsoleteSubsliceMatch);
3268 let subpat = self.parse_pat();
3269 if before_slice && self.check(&token::DotDot) {
3271 slice = Some(subpat);
3272 before_slice = false;
3273 } else if before_slice {
3274 before.push(subpat);
3280 (before, slice, after)
3283 /// Parse the fields of a struct-like pattern
3284 fn parse_pat_fields(&mut self) -> (Vec<codemap::Spanned<ast::FieldPat>> , bool) {
3285 let mut fields = Vec::new();
3286 let mut etc = false;
3287 let mut first = true;
3288 while self.token != token::CloseDelim(token::Brace) {
3292 self.expect(&token::Comma);
3293 // accept trailing commas
3294 if self.check(&token::CloseDelim(token::Brace)) { break }
3297 let lo = self.span.lo;
3300 if self.check(&token::DotDot) {
3302 if self.token != token::CloseDelim(token::Brace) {
3303 let token_str = self.this_token_to_string();
3304 self.fatal(format!("expected `{}`, found `{}`", "}",
3311 let bind_type = if self.eat_keyword(keywords::Mut) {
3312 BindByValue(MutMutable)
3313 } else if self.eat_keyword(keywords::Ref) {
3314 BindByRef(self.parse_mutability())
3316 BindByValue(MutImmutable)
3319 let fieldname = self.parse_ident();
3321 let (subpat, is_shorthand) = if self.check(&token::Colon) {
3323 BindByRef(..) | BindByValue(MutMutable) => {
3324 let token_str = self.this_token_to_string();
3325 self.fatal(format!("unexpected `{}`",
3332 let pat = self.parse_pat();
3336 hi = self.last_span.hi;
3337 let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname};
3339 id: ast::DUMMY_NODE_ID,
3340 node: PatIdent(bind_type, fieldpath, None),
3341 span: self.last_span
3344 fields.push(codemap::Spanned { span: mk_sp(lo, hi),
3345 node: ast::FieldPat { ident: fieldname,
3347 is_shorthand: is_shorthand }});
3349 return (fields, etc);
3352 /// Parse a pattern.
3353 pub fn parse_pat(&mut self) -> P<Pat> {
3354 maybe_whole!(self, NtPat);
3356 let lo = self.span.lo;
3361 token::Underscore => {
3363 pat = PatWild(PatWildSingle);
3364 hi = self.last_span.hi;
3366 id: ast::DUMMY_NODE_ID,
3374 let sub = self.parse_pat();
3376 let last_span = self.last_span;
3378 self.obsolete(last_span, ObsoleteOwnedPattern);
3380 id: ast::DUMMY_NODE_ID,
3385 token::BinOp(token::And) | token::AndAnd => {
3387 let lo = self.span.lo;
3389 let sub = self.parse_pat();
3390 pat = PatRegion(sub);
3391 hi = self.last_span.hi;
3393 id: ast::DUMMY_NODE_ID,
3398 token::OpenDelim(token::Paren) => {
3399 // parse (pat,pat,pat,...) as tuple
3401 if self.check(&token::CloseDelim(token::Paren)) {
3403 pat = PatTup(vec![]);
3405 let mut fields = vec!(self.parse_pat());
3406 if self.look_ahead(1, |t| *t != token::CloseDelim(token::Paren)) {
3407 while self.check(&token::Comma) {
3409 if self.check(&token::CloseDelim(token::Paren)) { break; }
3410 fields.push(self.parse_pat());
3413 if fields.len() == 1 { self.expect(&token::Comma); }
3414 self.expect(&token::CloseDelim(token::Paren));
3415 pat = PatTup(fields);
3417 hi = self.last_span.hi;
3419 id: ast::DUMMY_NODE_ID,
3424 token::OpenDelim(token::Bracket) => {
3425 // parse [pat,pat,...] as vector pattern
3427 let (before, slice, after) =
3428 self.parse_pat_vec_elements();
3430 self.expect(&token::CloseDelim(token::Bracket));
3431 pat = ast::PatVec(before, slice, after);
3432 hi = self.last_span.hi;
3434 id: ast::DUMMY_NODE_ID,
3441 // at this point, token != _, ~, &, &&, (, [
3443 if (!(self.token.is_ident() || self.token.is_path())
3444 && self.token != token::ModSep)
3445 || self.token.is_keyword(keywords::True)
3446 || self.token.is_keyword(keywords::False) {
3447 // Parse an expression pattern or exp .. exp.
3449 // These expressions are limited to literals (possibly
3450 // preceded by unary-minus) or identifiers.
3451 let val = self.parse_literal_maybe_minus();
3452 if (self.check(&token::DotDotDot)) &&
3453 self.look_ahead(1, |t| {
3454 *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3457 let end = if self.token.is_ident() || self.token.is_path() {
3458 let path = self.parse_path(LifetimeAndTypesWithColons);
3459 let hi = self.span.hi;
3460 self.mk_expr(lo, hi, ExprPath(path))
3462 self.parse_literal_maybe_minus()
3464 pat = PatRange(val, end);
3468 } else if self.eat_keyword(keywords::Mut) {
3469 pat = self.parse_pat_ident(BindByValue(MutMutable));
3470 } else if self.eat_keyword(keywords::Ref) {
3472 let mutbl = self.parse_mutability();
3473 pat = self.parse_pat_ident(BindByRef(mutbl));
3474 } else if self.eat_keyword(keywords::Box) {
3477 // FIXME(#13910): Rename to `PatBox` and extend to full DST
3479 let sub = self.parse_pat();
3481 hi = self.last_span.hi;
3483 id: ast::DUMMY_NODE_ID,
3488 let can_be_enum_or_struct = self.look_ahead(1, |t| {
3490 token::OpenDelim(_) | token::Lt | token::ModSep => true,
3495 if self.look_ahead(1, |t| *t == token::DotDotDot) &&
3496 self.look_ahead(2, |t| {
3497 *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3499 let start = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3500 self.eat(&token::DotDotDot);
3501 let end = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3502 pat = PatRange(start, end);
3503 } else if self.token.is_plain_ident() && !can_be_enum_or_struct {
3504 let id = self.parse_ident();
3505 let id_span = self.last_span;
3506 let pth1 = codemap::Spanned{span:id_span, node: id};
3507 if self.eat(&token::Not) {
3509 let delim = self.expect_open_delim();
3510 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
3512 |p| p.parse_token_tree());
3514 let mac = MacInvocTT(ident_to_path(id_span,id), tts, EMPTY_CTXT);
3515 pat = ast::PatMac(codemap::Spanned {node: mac, span: self.span});
3517 let sub = if self.eat(&token::At) {
3519 Some(self.parse_pat())
3524 pat = PatIdent(BindByValue(MutImmutable), pth1, sub);
3527 // parse an enum pat
3528 let enum_path = self.parse_path(LifetimeAndTypesWithColons);
3530 token::OpenDelim(token::Brace) => {
3533 self.parse_pat_fields();
3535 pat = PatStruct(enum_path, fields, etc);
3538 let mut args: Vec<P<Pat>> = Vec::new();
3540 token::OpenDelim(token::Paren) => {
3541 let is_dotdot = self.look_ahead(1, |t| {
3543 token::DotDot => true,
3548 // This is a "top constructor only" pat
3551 self.expect(&token::CloseDelim(token::Paren));
3552 pat = PatEnum(enum_path, None);
3554 args = self.parse_enum_variant_seq(
3555 &token::OpenDelim(token::Paren),
3556 &token::CloseDelim(token::Paren),
3557 seq_sep_trailing_allowed(token::Comma),
3560 pat = PatEnum(enum_path, Some(args));
3564 if !enum_path.global &&
3565 enum_path.segments.len() == 1 &&
3566 enum_path.segments[0].parameters.is_empty()
3568 // it could still be either an enum
3569 // or an identifier pattern, resolve
3570 // will sort it out:
3571 pat = PatIdent(BindByValue(MutImmutable),
3573 span: enum_path.span,
3574 node: enum_path.segments[0]
3578 pat = PatEnum(enum_path, Some(args));
3586 hi = self.last_span.hi;
3588 id: ast::DUMMY_NODE_ID,
3590 span: mk_sp(lo, hi),
3594 /// Parse ident or ident @ pat
3595 /// used by the copy foo and ref foo patterns to give a good
3596 /// error message when parsing mistakes like ref foo(a,b)
3597 fn parse_pat_ident(&mut self,
3598 binding_mode: ast::BindingMode)
3600 if !self.token.is_plain_ident() {
3601 let span = self.span;
3602 let tok_str = self.this_token_to_string();
3603 self.span_fatal(span,
3604 format!("expected identifier, found `{}`", tok_str)[]);
3606 let ident = self.parse_ident();
3607 let last_span = self.last_span;
3608 let name = codemap::Spanned{span: last_span, node: ident};
3609 let sub = if self.eat(&token::At) {
3610 Some(self.parse_pat())
3615 // just to be friendly, if they write something like
3617 // we end up here with ( as the current token. This shortly
3618 // leads to a parse error. Note that if there is no explicit
3619 // binding mode then we do not end up here, because the lookahead
3620 // will direct us over to parse_enum_variant()
3621 if self.token == token::OpenDelim(token::Paren) {
3622 let last_span = self.last_span;
3625 "expected identifier, found enum pattern");
3628 PatIdent(binding_mode, name, sub)
3631 /// Parse a local variable declaration
3632 fn parse_local(&mut self) -> P<Local> {
3633 let lo = self.span.lo;
3634 let pat = self.parse_pat();
3637 id: ast::DUMMY_NODE_ID,
3639 span: mk_sp(lo, lo),
3641 if self.eat(&token::Colon) {
3642 ty = self.parse_ty_sum();
3644 let init = self.parse_initializer();
3649 id: ast::DUMMY_NODE_ID,
3650 span: mk_sp(lo, self.last_span.hi),
3655 /// Parse a "let" stmt
3656 fn parse_let(&mut self) -> P<Decl> {
3657 let lo = self.span.lo;
3658 let local = self.parse_local();
3659 P(spanned(lo, self.last_span.hi, DeclLocal(local)))
3662 /// Parse a structure field
3663 fn parse_name_and_ty(&mut self, pr: Visibility,
3664 attrs: Vec<Attribute> ) -> StructField {
3665 let lo = self.span.lo;
3666 if !self.token.is_plain_ident() {
3667 self.fatal("expected ident");
3669 let name = self.parse_ident();
3670 self.expect(&token::Colon);
3671 let ty = self.parse_ty_sum();
3672 spanned(lo, self.last_span.hi, ast::StructField_ {
3673 kind: NamedField(name, pr),
3674 id: ast::DUMMY_NODE_ID,
3680 /// Get an expected item after attributes error message.
3681 fn expected_item_err(attrs: &[Attribute]) -> &'static str {
3682 match attrs.last() {
3683 Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
3684 "expected item after doc comment"
3686 _ => "expected item after attributes",
3690 /// Parse a statement. may include decl.
3691 /// Precondition: any attributes are parsed already
3692 pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> P<Stmt> {
3693 maybe_whole!(self, NtStmt);
3695 fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) {
3696 // If we have attributes then we should have an item
3697 if !attrs.is_empty() {
3698 let last_span = p.last_span;
3699 p.span_err(last_span, Parser::expected_item_err(attrs));
3703 let lo = self.span.lo;
3704 if self.token.is_keyword(keywords::Let) {
3705 check_expected_item(self, item_attrs[]);
3706 self.expect_keyword(keywords::Let);
3707 let decl = self.parse_let();
3708 P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3709 } else if self.token.is_ident()
3710 && !self.token.is_any_keyword()
3711 && self.look_ahead(1, |t| *t == token::Not) {
3712 // it's a macro invocation:
3714 check_expected_item(self, item_attrs[]);
3716 // Potential trouble: if we allow macros with paths instead of
3717 // idents, we'd need to look ahead past the whole path here...
3718 let pth = self.parse_path(NoTypesAllowed);
3721 let id = match self.token {
3722 token::OpenDelim(_) => token::special_idents::invalid, // no special identifier
3723 _ => self.parse_ident(),
3726 // check that we're pointing at delimiters (need to check
3727 // again after the `if`, because of `parse_ident`
3728 // consuming more tokens).
3729 let delim = match self.token {
3730 token::OpenDelim(delim) => delim,
3732 // we only expect an ident if we didn't parse one
3734 let ident_str = if id.name == token::special_idents::invalid.name {
3739 let tok_str = self.this_token_to_string();
3740 self.fatal(format!("expected {}`(` or `{{`, found `{}`",
3746 let tts = self.parse_unspanned_seq(
3747 &token::OpenDelim(delim),
3748 &token::CloseDelim(delim),
3750 |p| p.parse_token_tree()
3752 let hi = self.span.hi;
3754 let style = if delim == token::Brace {
3757 MacStmtWithoutBraces
3760 if id.name == token::special_idents::invalid.name {
3763 StmtMac(P(spanned(lo,
3765 MacInvocTT(pth, tts, EMPTY_CTXT))),
3768 // if it has a special ident, it's definitely an item
3770 // Require a semicolon or braces.
3771 if style != MacStmtWithBraces {
3772 if !self.eat(&token::Semi) {
3773 let last_span = self.last_span;
3774 self.span_err(last_span,
3775 "macros that expand to items must \
3776 either be surrounded with braces or \
3777 followed by a semicolon");
3780 P(spanned(lo, hi, StmtDecl(
3781 P(spanned(lo, hi, DeclItem(
3783 lo, hi, id /*id is good here*/,
3784 ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))),
3785 Inherited, Vec::new(/*no attrs*/))))),
3786 ast::DUMMY_NODE_ID)))
3789 let found_attrs = !item_attrs.is_empty();
3790 let item_err = Parser::expected_item_err(item_attrs[]);
3791 match self.parse_item_or_view_item(item_attrs, false) {
3794 let decl = P(spanned(lo, hi, DeclItem(i)));
3795 P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3797 IoviViewItem(vi) => {
3798 self.span_fatal(vi.span,
3799 "view items must be declared at the top of the block");
3801 IoviForeignItem(_) => {
3802 self.fatal("foreign items are not allowed here");
3806 let last_span = self.last_span;
3807 self.span_err(last_span, item_err);
3810 // Remainder are line-expr stmts.
3811 let e = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3812 P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
3818 /// Is this expression a successfully-parsed statement?
3819 fn expr_is_complete(&mut self, e: &Expr) -> bool {
3820 self.restrictions.contains(RESTRICTION_STMT_EXPR) &&
3821 !classify::expr_requires_semi_to_be_stmt(e)
3824 /// Parse a block. No inner attrs are allowed.
3825 pub fn parse_block(&mut self) -> P<Block> {
3826 maybe_whole!(no_clone self, NtBlock);
3828 let lo = self.span.lo;
3830 if !self.eat(&token::OpenDelim(token::Brace)) {
3832 let tok = self.this_token_to_string();
3833 self.span_fatal_help(sp,
3834 format!("expected `{{`, found `{}`", tok)[],
3835 "place this code inside a block");
3838 return self.parse_block_tail_(lo, DefaultBlock, Vec::new());
3841 /// Parse a block. Inner attrs are allowed.
3842 fn parse_inner_attrs_and_block(&mut self)
3843 -> (Vec<Attribute> , P<Block>) {
3845 maybe_whole!(pair_empty self, NtBlock);
3847 let lo = self.span.lo;
3848 self.expect(&token::OpenDelim(token::Brace));
3849 let (inner, next) = self.parse_inner_attrs_and_next();
3851 (inner, self.parse_block_tail_(lo, DefaultBlock, next))
3854 /// Precondition: already parsed the '{' or '#{'
3855 /// I guess that also means "already parsed the 'impure'" if
3856 /// necessary, and this should take a qualifier.
3857 /// Some blocks start with "#{"...
3858 fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> P<Block> {
3859 self.parse_block_tail_(lo, s, Vec::new())
3862 /// Parse the rest of a block expression or function body
3863 fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode,
3864 first_item_attrs: Vec<Attribute> ) -> P<Block> {
3865 let mut stmts = Vec::new();
3866 let mut expr = None;
3868 // wouldn't it be more uniform to parse view items only, here?
3869 let ParsedItemsAndViewItems {
3874 } = self.parse_items_and_view_items(first_item_attrs,
3877 for item in items.into_iter() {
3878 let span = item.span;
3879 let decl = P(spanned(span.lo, span.hi, DeclItem(item)));
3880 stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))));
3883 let mut attributes_box = attrs_remaining;
3885 while self.token != token::CloseDelim(token::Brace) {
3886 // parsing items even when they're not allowed lets us give
3887 // better error messages and recover more gracefully.
3888 attributes_box.push_all(self.parse_outer_attributes()[]);
3891 if !attributes_box.is_empty() {
3892 let last_span = self.last_span;
3893 self.span_err(last_span,
3894 Parser::expected_item_err(attributes_box[]));
3895 attributes_box = Vec::new();
3897 self.bump(); // empty
3899 token::CloseDelim(token::Brace) => {
3900 // fall through and out.
3903 let stmt = self.parse_stmt(attributes_box);
3904 attributes_box = Vec::new();
3905 stmt.and_then(|Spanned {node, span}| match node {
3906 StmtExpr(e, stmt_id) => {
3907 self.handle_expression_like_statement(e,
3913 StmtMac(macro, MacStmtWithoutBraces) => {
3914 // statement macro without braces; might be an
3915 // expr depending on whether a semicolon follows
3918 stmts.push(P(Spanned {
3919 node: StmtMac(macro,
3920 MacStmtWithSemicolon),
3926 let e = self.mk_mac_expr(span.lo,
3928 macro.and_then(|m| m.node));
3930 self.parse_dot_or_call_expr_with(e);
3931 self.handle_expression_like_statement(
3940 StmtMac(m, style) => {
3941 // statement macro; might be an expr
3944 stmts.push(P(Spanned {
3946 MacStmtWithSemicolon),
3951 token::CloseDelim(token::Brace) => {
3952 // if a block ends in `m!(arg)` without
3953 // a `;`, it must be an expr
3955 self.mk_mac_expr(span.lo,
3957 m.and_then(|x| x.node)));
3960 stmts.push(P(Spanned {
3961 node: StmtMac(m, style),
3967 _ => { // all other kinds of statements:
3968 if classify::stmt_ends_with_semi(&node) {
3969 self.commit_stmt_expecting(token::Semi);
3972 stmts.push(P(Spanned {
3982 if !attributes_box.is_empty() {
3983 let last_span = self.last_span;
3984 self.span_err(last_span,
3985 Parser::expected_item_err(attributes_box[]));
3988 let hi = self.span.hi;
3991 view_items: view_items,
3994 id: ast::DUMMY_NODE_ID,
3996 span: mk_sp(lo, hi),
4000 fn handle_expression_like_statement(
4005 stmts: &mut Vec<P<Stmt>>,
4006 last_block_expr: &mut Option<P<Expr>>) {
4007 // expression without semicolon
4008 if classify::expr_requires_semi_to_be_stmt(&*e) {
4009 // Just check for errors and recover; do not eat semicolon yet.
4010 self.commit_stmt(&[],
4011 &[token::Semi, token::CloseDelim(token::Brace)]);
4017 let span_with_semi = Span {
4019 hi: self.last_span.hi,
4020 expn_id: span.expn_id,
4022 stmts.push(P(Spanned {
4023 node: StmtSemi(e, stmt_id),
4024 span: span_with_semi,
4027 token::CloseDelim(token::Brace) => *last_block_expr = Some(e),
4029 stmts.push(P(Spanned {
4030 node: StmtExpr(e, stmt_id),
4037 // Parses a sequence of bounds if a `:` is found,
4038 // otherwise returns empty list.
4039 fn parse_colon_then_ty_param_bounds(&mut self,
4040 mode: BoundParsingMode)
4041 -> OwnedSlice<TyParamBound>
4043 if !self.eat(&token::Colon) {
4046 self.parse_ty_param_bounds(mode)
4050 // matches bounds = ( boundseq )?
4051 // where boundseq = ( polybound + boundseq ) | polybound
4052 // and polybound = ( 'for' '<' 'region '>' )? bound
4053 // and bound = 'region | trait_ref
4054 fn parse_ty_param_bounds(&mut self,
4055 mode: BoundParsingMode)
4056 -> OwnedSlice<TyParamBound>
4058 let mut result = vec!();
4060 let question_span = self.span;
4061 let ate_question = self.eat(&token::Question);
4063 token::Lifetime(lifetime) => {
4065 self.span_err(question_span,
4066 "`?` may only modify trait bounds, not lifetime bounds");
4068 result.push(RegionTyParamBound(ast::Lifetime {
4069 id: ast::DUMMY_NODE_ID,
4075 token::ModSep | token::Ident(..) => {
4076 let poly_trait_ref = self.parse_poly_trait_ref();
4077 let modifier = if ate_question {
4078 if mode == BoundParsingMode::Modified {
4079 TraitBoundModifier::Maybe
4081 self.span_err(question_span,
4083 TraitBoundModifier::None
4086 TraitBoundModifier::None
4088 result.push(TraitTyParamBound(poly_trait_ref, modifier))
4093 if !self.eat(&token::BinOp(token::Plus)) {
4098 return OwnedSlice::from_vec(result);
4101 fn trait_ref_from_ident(ident: Ident, span: Span) -> TraitRef {
4102 let segment = ast::PathSegment {
4104 parameters: ast::PathParameters::none()
4106 let path = ast::Path {
4109 segments: vec![segment],
4113 ref_id: ast::DUMMY_NODE_ID,
4117 /// Matches typaram = (unbound `?`)? IDENT (`?` unbound)? optbounds ( EQ ty )?
4118 fn parse_ty_param(&mut self) -> TyParam {
4119 // This is a bit hacky. Currently we are only interested in a single
4120 // unbound, and it may only be `Sized`. To avoid backtracking and other
4121 // complications, we parse an ident, then check for `?`. If we find it,
4122 // we use the ident as the unbound, otherwise, we use it as the name of
4123 // type param. Even worse, for now, we need to check for `?` before or
4125 let mut span = self.span;
4126 let mut ident = self.parse_ident();
4127 let mut unbound = None;
4128 if self.eat(&token::Question) {
4129 let tref = Parser::trait_ref_from_ident(ident, span);
4130 unbound = Some(tref);
4132 ident = self.parse_ident();
4135 let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Modified);
4136 if let Some(unbound) = unbound {
4137 let mut bounds_as_vec = bounds.into_vec();
4138 bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4139 trait_ref: unbound },
4140 TraitBoundModifier::Maybe));
4141 bounds = OwnedSlice::from_vec(bounds_as_vec);
4144 let default = if self.check(&token::Eq) {
4146 Some(self.parse_ty_sum())
4152 id: ast::DUMMY_NODE_ID,
4159 /// Parse a set of optional generic type parameter declarations. Where
4160 /// clauses are not parsed here, and must be added later via
4161 /// `parse_where_clause()`.
4163 /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4164 /// | ( < lifetimes , typaramseq ( , )? > )
4165 /// where typaramseq = ( typaram ) | ( typaram , typaramseq )
4166 pub fn parse_generics(&mut self) -> ast::Generics {
4167 if self.eat(&token::Lt) {
4168 let lifetime_defs = self.parse_lifetime_defs();
4169 let mut seen_default = false;
4170 let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
4171 p.forbid_lifetime();
4172 let ty_param = p.parse_ty_param();
4173 if ty_param.default.is_some() {
4174 seen_default = true;
4175 } else if seen_default {
4176 let last_span = p.last_span;
4177 p.span_err(last_span,
4178 "type parameters with a default must be trailing");
4183 lifetimes: lifetime_defs,
4184 ty_params: ty_params,
4185 where_clause: WhereClause {
4186 id: ast::DUMMY_NODE_ID,
4187 predicates: Vec::new(),
4191 ast_util::empty_generics()
4195 fn parse_generic_values_after_lt(&mut self)
4196 -> (Vec<ast::Lifetime>, Vec<P<Ty>>, Vec<P<TypeBinding>>) {
4197 let lifetimes = self.parse_lifetimes(token::Comma);
4199 // First parse types.
4200 let (types, returned) = self.parse_seq_to_gt_or_return(
4203 p.forbid_lifetime();
4204 if p.look_ahead(1, |t| t == &token::Eq) {
4207 Some(p.parse_ty_sum())
4212 // If we found the `>`, don't continue.
4214 return (lifetimes, types.into_vec(), Vec::new());
4217 // Then parse type bindings.
4218 let bindings = self.parse_seq_to_gt(
4221 p.forbid_lifetime();
4223 let ident = p.parse_ident();
4224 let found_eq = p.eat(&token::Eq);
4227 p.span_warn(span, "whoops, no =?");
4229 let ty = p.parse_ty();
4231 let span = mk_sp(lo, hi);
4232 return P(TypeBinding{id: ast::DUMMY_NODE_ID,
4239 (lifetimes, types.into_vec(), bindings.into_vec())
4242 fn forbid_lifetime(&mut self) {
4243 if self.token.is_lifetime() {
4244 let span = self.span;
4245 self.span_fatal(span, "lifetime parameters must be declared \
4246 prior to type parameters");
4250 /// Parses an optional `where` clause and places it in `generics`.
4253 /// where T : Trait<U, V> + 'b, 'a : 'b
4255 fn parse_where_clause(&mut self, generics: &mut ast::Generics) {
4256 if !self.eat_keyword(keywords::Where) {
4260 let mut parsed_something = false;
4262 let lo = self.span.lo;
4264 token::OpenDelim(token::Brace) => {
4268 token::Lifetime(..) => {
4269 let bounded_lifetime =
4270 self.parse_lifetime();
4272 self.eat(&token::Colon);
4275 self.parse_lifetimes(token::BinOp(token::Plus));
4277 let hi = self.span.hi;
4278 let span = mk_sp(lo, hi);
4280 generics.where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4281 ast::WhereRegionPredicate {
4283 lifetime: bounded_lifetime,
4288 parsed_something = true;
4292 let bounded_ty = self.parse_ty();
4294 if self.eat(&token::Colon) {
4295 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
4296 let hi = self.span.hi;
4297 let span = mk_sp(lo, hi);
4299 if bounds.len() == 0 {
4301 "each predicate in a `where` clause must have \
4302 at least one bound in it");
4305 generics.where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4306 ast::WhereBoundPredicate {
4308 bounded_ty: bounded_ty,
4312 parsed_something = true;
4313 } else if self.eat(&token::Eq) {
4314 // let ty = self.parse_ty();
4315 let hi = self.span.hi;
4316 let span = mk_sp(lo, hi);
4317 // generics.where_clause.predicates.push(
4318 // ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
4319 // id: ast::DUMMY_NODE_ID,
4321 // path: panic!("NYI"), //bounded_ty,
4324 // parsed_something = true;
4327 "equality constraints are not yet supported \
4328 in where clauses (#20041)");
4330 let last_span = self.last_span;
4331 self.span_err(last_span,
4332 "unexpected token in `where` clause");
4337 if !self.eat(&token::Comma) {
4342 if !parsed_something {
4343 let last_span = self.last_span;
4344 self.span_err(last_span,
4345 "a `where` clause must have at least one predicate \
4350 fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4351 -> (Vec<Arg> , bool) {
4353 let mut args: Vec<Option<Arg>> =
4354 self.parse_unspanned_seq(
4355 &token::OpenDelim(token::Paren),
4356 &token::CloseDelim(token::Paren),
4357 seq_sep_trailing_allowed(token::Comma),
4359 if p.token == token::DotDotDot {
4362 if p.token != token::CloseDelim(token::Paren) {
4365 "`...` must be last in argument list for variadic function");
4370 "only foreign functions are allowed to be variadic");
4374 Some(p.parse_arg_general(named_args))
4379 let variadic = match args.pop() {
4382 // Need to put back that last arg
4389 if variadic && args.is_empty() {
4391 "variadic function must be declared with at least one named argument");
4394 let args = args.into_iter().map(|x| x.unwrap()).collect();
4399 /// Parse the argument list and result type of a function declaration
4400 pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> P<FnDecl> {
4402 let (args, variadic) = self.parse_fn_args(true, allow_variadic);
4403 let ret_ty = self.parse_ret_ty();
4412 fn is_self_ident(&mut self) -> bool {
4414 token::Ident(id, token::Plain) => id.name == special_idents::self_.name,
4419 fn expect_self_ident(&mut self) -> ast::Ident {
4421 token::Ident(id, token::Plain) if id.name == special_idents::self_.name => {
4426 let token_str = self.this_token_to_string();
4427 self.fatal(format!("expected `self`, found `{}`",
4433 /// Parse the argument list and result type of a function
4434 /// that may have a self type.
4435 fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
4436 F: FnMut(&mut Parser) -> Arg,
4438 fn maybe_parse_borrowed_explicit_self(this: &mut Parser)
4439 -> ast::ExplicitSelf_ {
4440 // The following things are possible to see here:
4445 // fn(&'lt mut self)
4447 // We already know that the current token is `&`.
4449 if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4451 SelfRegion(None, MutImmutable, this.expect_self_ident())
4452 } else if this.look_ahead(1, |t| t.is_mutability()) &&
4453 this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4455 let mutability = this.parse_mutability();
4456 SelfRegion(None, mutability, this.expect_self_ident())
4457 } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4458 this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4460 let lifetime = this.parse_lifetime();
4461 SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
4462 } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4463 this.look_ahead(2, |t| t.is_mutability()) &&
4464 this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
4466 let lifetime = this.parse_lifetime();
4467 let mutability = this.parse_mutability();
4468 SelfRegion(Some(lifetime), mutability, this.expect_self_ident())
4474 self.expect(&token::OpenDelim(token::Paren));
4476 // A bit of complexity and lookahead is needed here in order to be
4477 // backwards compatible.
4478 let lo = self.span.lo;
4479 let mut self_ident_lo = self.span.lo;
4480 let mut self_ident_hi = self.span.hi;
4482 let mut mutbl_self = MutImmutable;
4483 let explicit_self = match self.token {
4484 token::BinOp(token::And) => {
4485 let eself = maybe_parse_borrowed_explicit_self(self);
4486 self_ident_lo = self.last_span.lo;
4487 self_ident_hi = self.last_span.hi;
4491 // We need to make sure it isn't a type
4492 if self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4494 drop(self.expect_self_ident());
4495 let last_span = self.last_span;
4496 self.obsolete(last_span, ObsoleteOwnedSelf)
4500 token::BinOp(token::Star) => {
4501 // Possibly "*self" or "*mut self" -- not supported. Try to avoid
4502 // emitting cryptic "unexpected token" errors.
4504 let _mutability = if self.token.is_mutability() {
4505 self.parse_mutability()
4509 if self.is_self_ident() {
4510 let span = self.span;
4511 self.span_err(span, "cannot pass self by unsafe pointer");
4514 // error case, making bogus self ident:
4515 SelfValue(special_idents::self_)
4517 token::Ident(..) => {
4518 if self.is_self_ident() {
4519 let self_ident = self.expect_self_ident();
4521 // Determine whether this is the fully explicit form, `self:
4523 if self.eat(&token::Colon) {
4524 SelfExplicit(self.parse_ty_sum(), self_ident)
4526 SelfValue(self_ident)
4528 } else if self.token.is_mutability() &&
4529 self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4530 mutbl_self = self.parse_mutability();
4531 let self_ident = self.expect_self_ident();
4533 // Determine whether this is the fully explicit form,
4535 if self.eat(&token::Colon) {
4536 SelfExplicit(self.parse_ty_sum(), self_ident)
4538 SelfValue(self_ident)
4540 } else if self.token.is_mutability() &&
4541 self.look_ahead(1, |t| *t == token::Tilde) &&
4542 self.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4543 mutbl_self = self.parse_mutability();
4545 drop(self.expect_self_ident());
4546 let last_span = self.last_span;
4547 self.obsolete(last_span, ObsoleteOwnedSelf);
4556 let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
4558 // shared fall-through for the three cases below. borrowing prevents simply
4559 // writing this as a closure
4560 macro_rules! parse_remaining_arguments {
4563 // If we parsed a self type, expect a comma before the argument list.
4567 let sep = seq_sep_trailing_allowed(token::Comma);
4568 let mut fn_inputs = self.parse_seq_to_before_end(
4569 &token::CloseDelim(token::Paren),
4573 fn_inputs.insert(0, Arg::new_self(explicit_self_sp, mutbl_self, $self_id));
4576 token::CloseDelim(token::Paren) => {
4577 vec!(Arg::new_self(explicit_self_sp, mutbl_self, $self_id))
4580 let token_str = self.this_token_to_string();
4581 self.fatal(format!("expected `,` or `)`, found `{}`",
4588 let fn_inputs = match explicit_self {
4590 let sep = seq_sep_trailing_allowed(token::Comma);
4591 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
4593 SelfValue(id) => parse_remaining_arguments!(id),
4594 SelfRegion(_,_,id) => parse_remaining_arguments!(id),
4595 SelfExplicit(_,id) => parse_remaining_arguments!(id),
4599 self.expect(&token::CloseDelim(token::Paren));
4601 let hi = self.span.hi;
4603 let ret_ty = self.parse_ret_ty();
4605 let fn_decl = P(FnDecl {
4611 (spanned(lo, hi, explicit_self), fn_decl)
4614 // parse the |arg, arg| header on a lambda
4615 fn parse_fn_block_decl(&mut self)
4616 -> (P<FnDecl>, Option<UnboxedClosureKind>) {
4617 let (optional_unboxed_closure_kind, inputs_captures) = {
4618 if self.eat(&token::OrOr) {
4621 self.expect(&token::BinOp(token::Or));
4622 let optional_unboxed_closure_kind =
4623 self.parse_optional_unboxed_closure_kind();
4624 let args = self.parse_seq_to_before_end(
4625 &token::BinOp(token::Or),
4626 seq_sep_trailing_allowed(token::Comma),
4627 |p| p.parse_fn_block_arg()
4630 (optional_unboxed_closure_kind, args)
4633 let output = if self.check(&token::RArrow) {
4637 id: ast::DUMMY_NODE_ID,
4644 inputs: inputs_captures,
4647 }), optional_unboxed_closure_kind)
4650 /// Parses the `(arg, arg) -> return_type` header on a procedure.
4651 fn parse_proc_decl(&mut self) -> P<FnDecl> {
4653 self.parse_unspanned_seq(&token::OpenDelim(token::Paren),
4654 &token::CloseDelim(token::Paren),
4655 seq_sep_trailing_allowed(token::Comma),
4656 |p| p.parse_fn_block_arg());
4658 let output = if self.check(&token::RArrow) {
4662 id: ast::DUMMY_NODE_ID,
4675 /// Parse the name and optional generic types of a function header.
4676 fn parse_fn_header(&mut self) -> (Ident, ast::Generics) {
4677 let id = self.parse_ident();
4678 let generics = self.parse_generics();
4682 fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
4683 node: Item_, vis: Visibility,
4684 attrs: Vec<Attribute>) -> P<Item> {
4688 id: ast::DUMMY_NODE_ID,
4695 /// Parse an item-position function declaration.
4696 fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> ItemInfo {
4697 let (ident, mut generics) = self.parse_fn_header();
4698 let decl = self.parse_fn_decl(false);
4699 self.parse_where_clause(&mut generics);
4700 let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4701 (ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))
4704 /// Parse a method in a trait impl
4705 pub fn parse_method_with_outer_attributes(&mut self) -> P<Method> {
4706 let attrs = self.parse_outer_attributes();
4707 let visa = self.parse_visibility();
4708 self.parse_method(attrs, visa)
4711 /// Parse a method in a trait impl, starting with `attrs` attributes.
4712 pub fn parse_method(&mut self,
4713 attrs: Vec<Attribute>,
4716 let lo = self.span.lo;
4718 // code copied from parse_macro_use_or_failure... abstraction!
4719 let (method_, hi, new_attrs) = {
4720 if !self.token.is_any_keyword()
4721 && self.look_ahead(1, |t| *t == token::Not)
4722 && (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
4723 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
4725 let pth = self.parse_path(NoTypesAllowed);
4726 self.expect(&token::Not);
4728 // eat a matched-delimiter token tree:
4729 let delim = self.expect_open_delim();
4730 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
4732 |p| p.parse_token_tree());
4733 let m_ = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
4734 let m: ast::Mac = codemap::Spanned { node: m_,
4735 span: mk_sp(self.span.lo,
4737 if delim != token::Brace {
4738 self.expect(&token::Semi)
4740 (ast::MethMac(m), self.span.hi, attrs)
4742 let unsafety = self.parse_unsafety();
4743 let abi = if self.eat_keyword(keywords::Extern) {
4744 self.parse_opt_abi().unwrap_or(abi::C)
4748 self.expect_keyword(keywords::Fn);
4749 let ident = self.parse_ident();
4750 let mut generics = self.parse_generics();
4751 let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
4754 self.parse_where_clause(&mut generics);
4755 let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4756 let body_span = body.span;
4757 let mut new_attrs = attrs;
4758 new_attrs.push_all(inner_attrs[]);
4759 (ast::MethDecl(ident,
4767 body_span.hi, new_attrs)
4772 id: ast::DUMMY_NODE_ID,
4773 span: mk_sp(lo, hi),
4778 /// Parse trait Foo { ... }
4779 fn parse_item_trait(&mut self, unsafety: Unsafety) -> ItemInfo {
4780 let ident = self.parse_ident();
4781 let mut tps = self.parse_generics();
4782 let unbound = self.parse_for_sized();
4784 // Parse supertrait bounds.
4785 let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
4787 if let Some(unbound) = unbound {
4788 let mut bounds_as_vec = bounds.into_vec();
4789 bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4790 trait_ref: unbound },
4791 TraitBoundModifier::Maybe));
4792 bounds = OwnedSlice::from_vec(bounds_as_vec);
4795 self.parse_where_clause(&mut tps);
4797 let meths = self.parse_trait_items();
4798 (ident, ItemTrait(unsafety, tps, bounds, meths), None)
4801 fn parse_impl_items(&mut self) -> (Vec<ImplItem>, Vec<Attribute>) {
4802 let mut impl_items = Vec::new();
4803 self.expect(&token::OpenDelim(token::Brace));
4804 let (inner_attrs, mut method_attrs) =
4805 self.parse_inner_attrs_and_next();
4806 while !self.eat(&token::CloseDelim(token::Brace)) {
4807 method_attrs.extend(self.parse_outer_attributes().into_iter());
4808 let vis = self.parse_visibility();
4809 if self.eat_keyword(keywords::Type) {
4810 impl_items.push(TypeImplItem(P(self.parse_typedef(
4814 impl_items.push(MethodImplItem(self.parse_method(
4818 method_attrs = self.parse_outer_attributes();
4820 (impl_items, inner_attrs)
4823 /// Parses two variants (with the region/type params always optional):
4824 /// impl<T> Foo { ... }
4825 /// impl<T> ToString for ~[T] { ... }
4826 fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> ItemInfo {
4827 // First, parse type parameters if necessary.
4828 let mut generics = self.parse_generics();
4830 // Special case: if the next identifier that follows is '(', don't
4831 // allow this to be parsed as a trait.
4832 let could_be_trait = self.token != token::OpenDelim(token::Paren);
4835 let mut ty = self.parse_ty_sum();
4837 // Parse traits, if necessary.
4838 let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
4839 // New-style trait. Reinterpret the type as a trait.
4840 let opt_trait_ref = match ty.node {
4841 TyPath(ref path, node_id) => {
4843 path: (*path).clone(),
4848 self.span_err(ty.span, "not a trait");
4853 ty = self.parse_ty_sum();
4859 self.parse_where_clause(&mut generics);
4860 let (impl_items, attrs) = self.parse_impl_items();
4862 let ident = ast_util::impl_pretty_name(&opt_trait, &*ty);
4865 ItemImpl(unsafety, generics, opt_trait, ty, impl_items),
4869 /// Parse a::B<String,int>
4870 fn parse_trait_ref(&mut self) -> TraitRef {
4872 path: self.parse_path(LifetimeAndTypesWithoutColons),
4873 ref_id: ast::DUMMY_NODE_ID,
4877 fn parse_late_bound_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
4878 if self.eat_keyword(keywords::For) {
4879 self.expect(&token::Lt);
4880 let lifetime_defs = self.parse_lifetime_defs();
4888 /// Parse for<'l> a::B<String,int>
4889 fn parse_poly_trait_ref(&mut self) -> PolyTraitRef {
4890 let lifetime_defs = self.parse_late_bound_lifetime_defs();
4893 bound_lifetimes: lifetime_defs,
4894 trait_ref: self.parse_trait_ref()
4898 /// Parse struct Foo { ... }
4899 fn parse_item_struct(&mut self) -> ItemInfo {
4900 let class_name = self.parse_ident();
4901 let mut generics = self.parse_generics();
4903 if self.eat(&token::Colon) {
4904 let ty = self.parse_ty_sum();
4905 self.span_err(ty.span, "`virtual` structs have been removed from the language");
4908 self.parse_where_clause(&mut generics);
4910 let mut fields: Vec<StructField>;
4913 if self.eat(&token::OpenDelim(token::Brace)) {
4914 // It's a record-like struct.
4915 is_tuple_like = false;
4916 fields = Vec::new();
4917 while self.token != token::CloseDelim(token::Brace) {
4918 fields.push(self.parse_struct_decl_field(true));
4920 if fields.len() == 0 {
4921 self.fatal(format!("unit-like struct definition should be \
4922 written as `struct {};`",
4923 token::get_ident(class_name))[]);
4926 } else if self.check(&token::OpenDelim(token::Paren)) {
4927 // It's a tuple-like struct.
4928 is_tuple_like = true;
4929 fields = self.parse_unspanned_seq(
4930 &token::OpenDelim(token::Paren),
4931 &token::CloseDelim(token::Paren),
4932 seq_sep_trailing_allowed(token::Comma),
4934 let attrs = p.parse_outer_attributes();
4936 let struct_field_ = ast::StructField_ {
4937 kind: UnnamedField(p.parse_visibility()),
4938 id: ast::DUMMY_NODE_ID,
4939 ty: p.parse_ty_sum(),
4942 spanned(lo, p.span.hi, struct_field_)
4944 if fields.len() == 0 {
4945 self.fatal(format!("unit-like struct definition should be \
4946 written as `struct {};`",
4947 token::get_ident(class_name))[]);
4949 self.expect(&token::Semi);
4950 } else if self.eat(&token::Semi) {
4951 // It's a unit-like struct.
4952 is_tuple_like = true;
4953 fields = Vec::new();
4955 let token_str = self.this_token_to_string();
4956 self.fatal(format!("expected `{}`, `(`, or `;` after struct \
4957 name, found `{}`", "{",
4961 let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug.
4962 let new_id = ast::DUMMY_NODE_ID;
4964 ItemStruct(P(ast::StructDef {
4966 ctor_id: if is_tuple_like { Some(new_id) } else { None },
4971 /// Parse a structure field declaration
4972 pub fn parse_single_struct_field(&mut self,
4974 attrs: Vec<Attribute> )
4976 let a_var = self.parse_name_and_ty(vis, attrs);
4981 token::CloseDelim(token::Brace) => {}
4983 let span = self.span;
4984 let token_str = self.this_token_to_string();
4985 self.span_fatal_help(span,
4986 format!("expected `,`, or `}}`, found `{}`",
4988 "struct fields should be separated by commas")
4994 /// Parse an element of a struct definition
4995 fn parse_struct_decl_field(&mut self, allow_pub: bool) -> StructField {
4997 let attrs = self.parse_outer_attributes();
4999 if self.eat_keyword(keywords::Pub) {
5001 let span = self.last_span;
5002 self.span_err(span, "`pub` is not allowed here");
5004 return self.parse_single_struct_field(Public, attrs);
5007 return self.parse_single_struct_field(Inherited, attrs);
5010 /// Parse visibility: PUB, PRIV, or nothing
5011 fn parse_visibility(&mut self) -> Visibility {
5012 if self.eat_keyword(keywords::Pub) { Public }
5016 fn parse_for_sized(&mut self) -> Option<ast::TraitRef> {
5017 // FIXME, this should really use TraitBoundModifier, but it will get
5018 // re-jigged shortly in any case, so leaving the hacky version for now.
5019 if self.eat_keyword(keywords::For) {
5020 let span = self.span;
5021 let mut ate_question = false;
5022 if self.eat(&token::Question) {
5023 ate_question = true;
5025 let ident = self.parse_ident();
5026 if self.eat(&token::Question) {
5031 ate_question = true;
5035 "expected `?Sized` after `for` in trait item");
5038 let tref = Parser::trait_ref_from_ident(ident, span);
5045 /// Given a termination token and a vector of already-parsed
5046 /// attributes (of length 0 or 1), parse all of the items in a module
5047 fn parse_mod_items(&mut self,
5049 first_item_attrs: Vec<Attribute>,
5052 // parse all of the items up to closing or an attribute.
5053 // view items are legal here.
5054 let ParsedItemsAndViewItems {
5057 items: starting_items,
5059 } = self.parse_items_and_view_items(first_item_attrs, true, true);
5060 let mut items: Vec<P<Item>> = starting_items;
5061 let attrs_remaining_len = attrs_remaining.len();
5063 // don't think this other loop is even necessary....
5065 let mut first = true;
5066 while self.token != term {
5067 let mut attrs = self.parse_outer_attributes();
5069 let mut tmp = attrs_remaining.clone();
5070 tmp.push_all(attrs[]);
5074 debug!("parse_mod_items: parse_item_or_view_item(attrs={})",
5076 match self.parse_item_or_view_item(attrs,
5077 true /* macros allowed */) {
5078 IoviItem(item) => items.push(item),
5079 IoviViewItem(view_item) => {
5080 self.span_fatal(view_item.span,
5081 "view items must be declared at the top of \
5085 let token_str = self.this_token_to_string();
5086 self.fatal(format!("expected item, found `{}`",
5092 if first && attrs_remaining_len > 0u {
5093 // We parsed attributes for the first item but didn't find it
5094 let last_span = self.last_span;
5095 self.span_err(last_span,
5096 Parser::expected_item_err(attrs_remaining[]));
5100 inner: mk_sp(inner_lo, self.span.lo),
5101 view_items: view_items,
5106 fn parse_item_const(&mut self, m: Option<Mutability>) -> ItemInfo {
5107 let id = self.parse_ident();
5108 self.expect(&token::Colon);
5109 let ty = self.parse_ty_sum();
5110 self.expect(&token::Eq);
5111 let e = self.parse_expr();
5112 self.commit_expr_expecting(&*e, token::Semi);
5113 let item = match m {
5114 Some(m) => ItemStatic(ty, m, e),
5115 None => ItemConst(ty, e),
5120 /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5121 fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> ItemInfo {
5122 let id_span = self.span;
5123 let id = self.parse_ident();
5124 if self.check(&token::Semi) {
5126 // This mod is in an external file. Let's go get it!
5127 let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
5128 (id, m, Some(attrs))
5130 self.push_mod_path(id, outer_attrs);
5131 self.expect(&token::OpenDelim(token::Brace));
5132 let mod_inner_lo = self.span.lo;
5133 let old_owns_directory = self.owns_directory;
5134 self.owns_directory = true;
5135 let (inner, next) = self.parse_inner_attrs_and_next();
5136 let m = self.parse_mod_items(token::CloseDelim(token::Brace), next, mod_inner_lo);
5137 self.expect(&token::CloseDelim(token::Brace));
5138 self.owns_directory = old_owns_directory;
5139 self.pop_mod_path();
5140 (id, ItemMod(m), Some(inner))
5144 fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
5145 let default_path = self.id_to_interned_str(id);
5146 let file_path = match ::attr::first_attr_value_str_by_name(attrs,
5149 None => default_path,
5151 self.mod_path_stack.push(file_path)
5154 fn pop_mod_path(&mut self) {
5155 self.mod_path_stack.pop().unwrap();
5158 /// Read a module from a source file.
5159 fn eval_src_mod(&mut self,
5161 outer_attrs: &[ast::Attribute],
5163 -> (ast::Item_, Vec<ast::Attribute> ) {
5164 let mut prefix = Path::new(self.sess.span_diagnostic.cm.span_to_filename(self.span));
5166 let mod_path = Path::new(".").join_many(self.mod_path_stack[]);
5167 let dir_path = prefix.join(&mod_path);
5168 let mod_string = token::get_ident(id);
5169 let (file_path, owns_directory) = match ::attr::first_attr_value_str_by_name(
5170 outer_attrs, "path") {
5171 Some(d) => (dir_path.join(d), true),
5173 let mod_name = mod_string.get().to_string();
5174 let default_path_str = format!("{}.rs", mod_name);
5175 let secondary_path_str = format!("{}/mod.rs", mod_name);
5176 let default_path = dir_path.join(default_path_str[]);
5177 let secondary_path = dir_path.join(secondary_path_str[]);
5178 let default_exists = default_path.exists();
5179 let secondary_exists = secondary_path.exists();
5181 if !self.owns_directory {
5182 self.span_err(id_sp,
5183 "cannot declare a new module at this location");
5184 let this_module = match self.mod_path_stack.last() {
5185 Some(name) => name.get().to_string(),
5186 None => self.root_module_name.as_ref().unwrap().clone(),
5188 self.span_note(id_sp,
5189 format!("maybe move this module `{0}` \
5190 to its own directory via \
5193 if default_exists || secondary_exists {
5194 self.span_note(id_sp,
5195 format!("... or maybe `use` the module \
5196 `{}` instead of possibly \
5200 self.abort_if_errors();
5203 match (default_exists, secondary_exists) {
5204 (true, false) => (default_path, false),
5205 (false, true) => (secondary_path, true),
5207 self.span_fatal_help(id_sp,
5208 format!("file not found for module `{}`",
5210 format!("name the file either {} or {} inside \
5214 dir_path.display())[]);
5217 self.span_fatal_help(
5219 format!("file for module `{}` found at both {} \
5223 secondary_path_str)[],
5224 "delete or rename one of them to remove the ambiguity");
5230 self.eval_src_mod_from_path(file_path, owns_directory,
5231 mod_string.get().to_string(), id_sp)
5234 fn eval_src_mod_from_path(&mut self,
5236 owns_directory: bool,
5238 id_sp: Span) -> (ast::Item_, Vec<ast::Attribute> ) {
5239 let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5240 match included_mod_stack.iter().position(|p| *p == path) {
5242 let mut err = String::from_str("circular modules: ");
5243 let len = included_mod_stack.len();
5244 for p in included_mod_stack.slice(i, len).iter() {
5245 err.push_str(p.display().as_cow()[]);
5246 err.push_str(" -> ");
5248 err.push_str(path.display().as_cow()[]);
5249 self.span_fatal(id_sp, err[]);
5253 included_mod_stack.push(path.clone());
5254 drop(included_mod_stack);
5257 new_sub_parser_from_file(self.sess,
5263 let mod_inner_lo = p0.span.lo;
5264 let (mod_attrs, next) = p0.parse_inner_attrs_and_next();
5265 let first_item_outer_attrs = next;
5266 let m0 = p0.parse_mod_items(token::Eof, first_item_outer_attrs, mod_inner_lo);
5267 self.sess.included_mod_stack.borrow_mut().pop();
5268 return (ast::ItemMod(m0), mod_attrs);
5271 /// Parse a function declaration from a foreign module
5272 fn parse_item_foreign_fn(&mut self, vis: ast::Visibility,
5273 attrs: Vec<Attribute>) -> P<ForeignItem> {
5274 let lo = self.span.lo;
5275 self.expect_keyword(keywords::Fn);
5277 let (ident, mut generics) = self.parse_fn_header();
5278 let decl = self.parse_fn_decl(true);
5279 self.parse_where_clause(&mut generics);
5280 let hi = self.span.hi;
5281 self.expect(&token::Semi);
5282 P(ast::ForeignItem {
5285 node: ForeignItemFn(decl, generics),
5286 id: ast::DUMMY_NODE_ID,
5287 span: mk_sp(lo, hi),
5292 /// Parse a static item from a foreign module
5293 fn parse_item_foreign_static(&mut self, vis: ast::Visibility,
5294 attrs: Vec<Attribute>) -> P<ForeignItem> {
5295 let lo = self.span.lo;
5297 self.expect_keyword(keywords::Static);
5298 let mutbl = self.eat_keyword(keywords::Mut);
5300 let ident = self.parse_ident();
5301 self.expect(&token::Colon);
5302 let ty = self.parse_ty_sum();
5303 let hi = self.span.hi;
5304 self.expect(&token::Semi);
5308 node: ForeignItemStatic(ty, mutbl),
5309 id: ast::DUMMY_NODE_ID,
5310 span: mk_sp(lo, hi),
5315 /// At this point, this is essentially a wrapper for
5316 /// parse_foreign_items.
5317 fn parse_foreign_mod_items(&mut self,
5319 first_item_attrs: Vec<Attribute> )
5321 let ParsedItemsAndViewItems {
5326 } = self.parse_foreign_items(first_item_attrs, true);
5327 if !attrs_remaining.is_empty() {
5328 let last_span = self.last_span;
5329 self.span_err(last_span,
5330 Parser::expected_item_err(attrs_remaining[]));
5332 assert!(self.token == token::CloseDelim(token::Brace));
5335 view_items: view_items,
5336 items: foreign_items
5340 /// Parse extern crate links
5344 /// extern crate url;
5345 /// extern crate foo = "bar"; //deprecated
5346 /// extern crate "bar" as foo;
5347 fn parse_item_extern_crate(&mut self,
5349 visibility: Visibility,
5350 attrs: Vec<Attribute> )
5353 let span = self.span;
5354 let (maybe_path, ident) = match self.token {
5355 token::Ident(..) => {
5356 let the_ident = self.parse_ident();
5357 let path = if self.token == token::Eq {
5359 let path = self.parse_str();
5360 let span = self.span;
5361 self.obsolete(span, ObsoleteExternCrateRenaming);
5363 } else if self.eat_keyword(keywords::As) {
5364 // skip the ident if there is one
5365 if self.token.is_ident() { self.bump(); }
5367 self.span_err(span, "expected `;`, found `as`");
5368 self.span_help(span,
5369 format!("perhaps you meant to enclose the crate name `{}` in \
5371 the_ident.as_str())[]);
5376 self.expect(&token::Semi);
5379 token::Literal(token::Str_(..), suf) | token::Literal(token::StrRaw(..), suf) => {
5381 self.expect_no_suffix(sp, "extern crate name", suf);
5382 // forgo the internal suffix check of `parse_str` to
5383 // avoid repeats (this unwrap will always succeed due
5384 // to the restriction of the `match`)
5385 let (s, style, _) = self.parse_optional_str().unwrap();
5386 self.expect_keyword(keywords::As);
5387 let the_ident = self.parse_ident();
5388 self.expect(&token::Semi);
5389 (Some((s, style)), the_ident)
5392 let span = self.span;
5393 let token_str = self.this_token_to_string();
5394 self.span_fatal(span,
5395 format!("expected extern crate name but \
5401 IoviViewItem(ast::ViewItem {
5402 node: ViewItemExternCrate(ident, maybe_path, ast::DUMMY_NODE_ID),
5405 span: mk_sp(lo, self.last_span.hi)
5409 /// Parse `extern` for foreign ABIs
5412 /// `extern` is expected to have been
5413 /// consumed before calling this method
5419 fn parse_item_foreign_mod(&mut self,
5421 opt_abi: Option<abi::Abi>,
5422 visibility: Visibility,
5423 attrs: Vec<Attribute> )
5426 self.expect(&token::OpenDelim(token::Brace));
5428 let abi = opt_abi.unwrap_or(abi::C);
5430 let (inner, next) = self.parse_inner_attrs_and_next();
5431 let m = self.parse_foreign_mod_items(abi, next);
5432 self.expect(&token::CloseDelim(token::Brace));
5434 let last_span = self.last_span;
5435 let item = self.mk_item(lo,
5437 special_idents::invalid,
5440 maybe_append(attrs, Some(inner)));
5441 return IoviItem(item);
5444 /// Parse type Foo = Bar;
5445 fn parse_item_type(&mut self) -> ItemInfo {
5446 let ident = self.parse_ident();
5447 let mut tps = self.parse_generics();
5448 self.parse_where_clause(&mut tps);
5449 self.expect(&token::Eq);
5450 let ty = self.parse_ty_sum();
5451 self.expect(&token::Semi);
5452 (ident, ItemTy(ty, tps), None)
5455 /// Parse a structure-like enum variant definition
5456 /// this should probably be renamed or refactored...
5457 fn parse_struct_def(&mut self) -> P<StructDef> {
5458 let mut fields: Vec<StructField> = Vec::new();
5459 while self.token != token::CloseDelim(token::Brace) {
5460 fields.push(self.parse_struct_decl_field(false));
5470 /// Parse the part of an "enum" decl following the '{'
5471 fn parse_enum_def(&mut self, _generics: &ast::Generics) -> EnumDef {
5472 let mut variants = Vec::new();
5473 let mut all_nullary = true;
5474 let mut any_disr = None;
5475 while self.token != token::CloseDelim(token::Brace) {
5476 let variant_attrs = self.parse_outer_attributes();
5477 let vlo = self.span.lo;
5479 let vis = self.parse_visibility();
5483 let mut args = Vec::new();
5484 let mut disr_expr = None;
5485 ident = self.parse_ident();
5486 if self.eat(&token::OpenDelim(token::Brace)) {
5487 // Parse a struct variant.
5488 all_nullary = false;
5489 let start_span = self.span;
5490 let struct_def = self.parse_struct_def();
5491 if struct_def.fields.len() == 0 {
5492 self.span_err(start_span,
5493 format!("unit-like struct variant should be written \
5494 without braces, as `{},`",
5495 token::get_ident(ident))[]);
5497 kind = StructVariantKind(struct_def);
5498 } else if self.check(&token::OpenDelim(token::Paren)) {
5499 all_nullary = false;
5500 let arg_tys = self.parse_enum_variant_seq(
5501 &token::OpenDelim(token::Paren),
5502 &token::CloseDelim(token::Paren),
5503 seq_sep_trailing_allowed(token::Comma),
5504 |p| p.parse_ty_sum()
5506 for ty in arg_tys.into_iter() {
5507 args.push(ast::VariantArg {
5509 id: ast::DUMMY_NODE_ID,
5512 kind = TupleVariantKind(args);
5513 } else if self.eat(&token::Eq) {
5514 disr_expr = Some(self.parse_expr());
5515 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5516 kind = TupleVariantKind(args);
5518 kind = TupleVariantKind(Vec::new());
5521 let vr = ast::Variant_ {
5523 attrs: variant_attrs,
5525 id: ast::DUMMY_NODE_ID,
5526 disr_expr: disr_expr,
5529 variants.push(P(spanned(vlo, self.last_span.hi, vr)));
5531 if !self.eat(&token::Comma) { break; }
5533 self.expect(&token::CloseDelim(token::Brace));
5535 Some(disr_span) if !all_nullary =>
5536 self.span_err(disr_span,
5537 "discriminator values can only be used with a c-like enum"),
5541 ast::EnumDef { variants: variants }
5544 /// Parse an "enum" declaration
5545 fn parse_item_enum(&mut self) -> ItemInfo {
5546 let id = self.parse_ident();
5547 let mut generics = self.parse_generics();
5548 self.parse_where_clause(&mut generics);
5549 self.expect(&token::OpenDelim(token::Brace));
5551 let enum_definition = self.parse_enum_def(&generics);
5552 (id, ItemEnum(enum_definition, generics), None)
5555 fn fn_expr_lookahead(tok: &token::Token) -> bool {
5557 token::OpenDelim(token::Paren) | token::At | token::Tilde | token::BinOp(_) => true,
5562 /// Parses a string as an ABI spec on an extern type or module. Consumes
5563 /// the `extern` keyword, if one is found.
5564 fn parse_opt_abi(&mut self) -> Option<abi::Abi> {
5566 token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5568 self.expect_no_suffix(sp, "ABI spec", suf);
5570 let the_string = s.as_str();
5571 match abi::lookup(the_string) {
5572 Some(abi) => Some(abi),
5574 let last_span = self.last_span;
5577 format!("illegal ABI: expected one of [{}], \
5579 abi::all_names().connect(", "),
5590 /// Parse one of the items or view items allowed by the
5591 /// flags; on failure, return IoviNone.
5592 /// NB: this function no longer parses the items inside an
5594 fn parse_item_or_view_item(&mut self,
5595 attrs: Vec<Attribute> ,
5596 macros_allowed: bool)
5598 let nt_item = match self.token {
5599 token::Interpolated(token::NtItem(ref item)) => {
5600 Some((**item).clone())
5607 let mut attrs = attrs;
5608 mem::swap(&mut item.attrs, &mut attrs);
5609 item.attrs.extend(attrs.into_iter());
5610 return IoviItem(P(item));
5615 let lo = self.span.lo;
5617 let visibility = self.parse_visibility();
5619 // must be a view item:
5620 if self.eat_keyword(keywords::Use) {
5621 // USE ITEM (IoviViewItem)
5622 let view_item = self.parse_use();
5623 self.expect(&token::Semi);
5624 return IoviViewItem(ast::ViewItem {
5628 span: mk_sp(lo, self.last_span.hi)
5631 // either a view item or an item:
5632 if self.eat_keyword(keywords::Extern) {
5633 let next_is_mod = self.eat_keyword(keywords::Mod);
5635 if next_is_mod || self.eat_keyword(keywords::Crate) {
5637 let last_span = self.last_span;
5638 self.span_err(mk_sp(lo, last_span.hi),
5639 format!("`extern mod` is obsolete, use \
5640 `extern crate` instead \
5641 to refer to external \
5644 return self.parse_item_extern_crate(lo, visibility, attrs);
5647 let opt_abi = self.parse_opt_abi();
5649 if self.eat_keyword(keywords::Fn) {
5650 // EXTERN FUNCTION ITEM
5651 let abi = opt_abi.unwrap_or(abi::C);
5652 let (ident, item_, extra_attrs) =
5653 self.parse_item_fn(Unsafety::Normal, abi);
5654 let last_span = self.last_span;
5655 let item = self.mk_item(lo,
5660 maybe_append(attrs, extra_attrs));
5661 return IoviItem(item);
5662 } else if self.check(&token::OpenDelim(token::Brace)) {
5663 return self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs);
5666 let span = self.span;
5667 let token_str = self.this_token_to_string();
5668 self.span_fatal(span,
5669 format!("expected `{}` or `fn`, found `{}`", "{",
5673 if self.eat_keyword(keywords::Virtual) {
5674 let span = self.span;
5675 self.span_err(span, "`virtual` structs have been removed from the language");
5678 // the rest are all guaranteed to be items:
5679 if self.token.is_keyword(keywords::Static) {
5682 let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
5683 let (ident, item_, extra_attrs) = self.parse_item_const(Some(m));
5684 let last_span = self.last_span;
5685 let item = self.mk_item(lo,
5690 maybe_append(attrs, extra_attrs));
5691 return IoviItem(item);
5693 if self.token.is_keyword(keywords::Const) {
5696 if self.eat_keyword(keywords::Mut) {
5697 let last_span = self.last_span;
5698 self.span_err(last_span, "const globals cannot be mutable");
5699 self.span_help(last_span, "did you mean to declare a static?");
5701 let (ident, item_, extra_attrs) = self.parse_item_const(None);
5702 let last_span = self.last_span;
5703 let item = self.mk_item(lo,
5708 maybe_append(attrs, extra_attrs));
5709 return IoviItem(item);
5711 if self.token.is_keyword(keywords::Unsafe) &&
5712 self.look_ahead(1u, |t| t.is_keyword(keywords::Trait))
5714 // UNSAFE TRAIT ITEM
5715 self.expect_keyword(keywords::Unsafe);
5716 self.expect_keyword(keywords::Trait);
5717 let (ident, item_, extra_attrs) =
5718 self.parse_item_trait(ast::Unsafety::Unsafe);
5719 let last_span = self.last_span;
5720 let item = self.mk_item(lo,
5725 maybe_append(attrs, extra_attrs));
5726 return IoviItem(item);
5728 if self.token.is_keyword(keywords::Unsafe) &&
5729 self.look_ahead(1u, |t| t.is_keyword(keywords::Impl))
5732 self.expect_keyword(keywords::Unsafe);
5733 self.expect_keyword(keywords::Impl);
5734 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe);
5735 let last_span = self.last_span;
5736 let item = self.mk_item(lo,
5741 maybe_append(attrs, extra_attrs));
5742 return IoviItem(item);
5744 if self.token.is_keyword(keywords::Fn) &&
5745 self.look_ahead(1, |f| !Parser::fn_expr_lookahead(f)) {
5748 let (ident, item_, extra_attrs) =
5749 self.parse_item_fn(Unsafety::Normal, abi::Rust);
5750 let last_span = self.last_span;
5751 let item = self.mk_item(lo,
5756 maybe_append(attrs, extra_attrs));
5757 return IoviItem(item);
5759 if self.token.is_keyword(keywords::Unsafe)
5760 && self.look_ahead(1u, |t| *t != token::OpenDelim(token::Brace)) {
5761 // UNSAFE FUNCTION ITEM
5763 let abi = if self.eat_keyword(keywords::Extern) {
5764 self.parse_opt_abi().unwrap_or(abi::C)
5768 self.expect_keyword(keywords::Fn);
5769 let (ident, item_, extra_attrs) =
5770 self.parse_item_fn(Unsafety::Unsafe, abi);
5771 let last_span = self.last_span;
5772 let item = self.mk_item(lo,
5777 maybe_append(attrs, extra_attrs));
5778 return IoviItem(item);
5780 if self.eat_keyword(keywords::Mod) {
5782 let (ident, item_, extra_attrs) =
5783 self.parse_item_mod(attrs[]);
5784 let last_span = self.last_span;
5785 let item = self.mk_item(lo,
5790 maybe_append(attrs, extra_attrs));
5791 return IoviItem(item);
5793 if self.eat_keyword(keywords::Type) {
5795 let (ident, item_, extra_attrs) = self.parse_item_type();
5796 let last_span = self.last_span;
5797 let item = self.mk_item(lo,
5802 maybe_append(attrs, extra_attrs));
5803 return IoviItem(item);
5805 if self.eat_keyword(keywords::Enum) {
5807 let (ident, item_, extra_attrs) = self.parse_item_enum();
5808 let last_span = self.last_span;
5809 let item = self.mk_item(lo,
5814 maybe_append(attrs, extra_attrs));
5815 return IoviItem(item);
5817 if self.eat_keyword(keywords::Trait) {
5819 let (ident, item_, extra_attrs) =
5820 self.parse_item_trait(ast::Unsafety::Normal);
5821 let last_span = self.last_span;
5822 let item = self.mk_item(lo,
5827 maybe_append(attrs, extra_attrs));
5828 return IoviItem(item);
5830 if self.eat_keyword(keywords::Impl) {
5832 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal);
5833 let last_span = self.last_span;
5834 let item = self.mk_item(lo,
5839 maybe_append(attrs, extra_attrs));
5840 return IoviItem(item);
5842 if self.eat_keyword(keywords::Struct) {
5844 let (ident, item_, extra_attrs) = self.parse_item_struct();
5845 let last_span = self.last_span;
5846 let item = self.mk_item(lo,
5851 maybe_append(attrs, extra_attrs));
5852 return IoviItem(item);
5854 self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5857 /// Parse a foreign item; on failure, return IoviNone.
5858 fn parse_foreign_item(&mut self,
5859 attrs: Vec<Attribute> ,
5860 macros_allowed: bool)
5862 maybe_whole!(iovi self, NtItem);
5863 let lo = self.span.lo;
5865 let visibility = self.parse_visibility();
5867 if self.token.is_keyword(keywords::Static) {
5868 // FOREIGN STATIC ITEM
5869 let item = self.parse_item_foreign_static(visibility, attrs);
5870 return IoviForeignItem(item);
5872 if self.token.is_keyword(keywords::Fn) || self.token.is_keyword(keywords::Unsafe) {
5873 // FOREIGN FUNCTION ITEM
5874 let item = self.parse_item_foreign_fn(visibility, attrs);
5875 return IoviForeignItem(item);
5877 self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5880 /// This is the fall-through for parsing items.
5881 fn parse_macro_use_or_failure(
5883 attrs: Vec<Attribute> ,
5884 macros_allowed: bool,
5886 visibility: Visibility
5887 ) -> ItemOrViewItem {
5888 if macros_allowed && !self.token.is_any_keyword()
5889 && self.look_ahead(1, |t| *t == token::Not)
5890 && (self.look_ahead(2, |t| t.is_plain_ident())
5891 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
5892 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
5893 // MACRO INVOCATION ITEM
5896 let pth = self.parse_path(NoTypesAllowed);
5897 self.expect(&token::Not);
5899 // a 'special' identifier (like what `macro_rules!` uses)
5900 // is optional. We should eventually unify invoc syntax
5902 let id = if self.token.is_plain_ident() {
5905 token::special_idents::invalid // no special identifier
5907 // eat a matched-delimiter token tree:
5908 let delim = self.expect_open_delim();
5909 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
5911 |p| p.parse_token_tree());
5912 // single-variant-enum... :
5913 let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
5914 let m: ast::Mac = codemap::Spanned { node: m,
5915 span: mk_sp(self.span.lo,
5918 if delim != token::Brace {
5919 if !self.eat(&token::Semi) {
5920 let last_span = self.last_span;
5921 self.span_err(last_span,
5922 "macros that expand to items must either \
5923 be surrounded with braces or followed by \
5928 let item_ = ItemMac(m);
5929 let last_span = self.last_span;
5930 let item = self.mk_item(lo,
5936 return IoviItem(item);
5939 // FAILURE TO PARSE ITEM
5943 let last_span = self.last_span;
5944 self.span_fatal(last_span, "unmatched visibility `pub`");
5947 return IoviNone(attrs);
5950 pub fn parse_item_with_outer_attributes(&mut self) -> Option<P<Item>> {
5951 let attrs = self.parse_outer_attributes();
5952 self.parse_item(attrs)
5955 pub fn parse_item(&mut self, attrs: Vec<Attribute>) -> Option<P<Item>> {
5956 match self.parse_item_or_view_item(attrs, true) {
5957 IoviNone(_) => None,
5959 self.fatal("view items are not allowed here"),
5960 IoviForeignItem(_) =>
5961 self.fatal("foreign items are not allowed here"),
5962 IoviItem(item) => Some(item)
5966 /// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo`
5967 pub fn parse_view_item(&mut self, attrs: Vec<Attribute>) -> ViewItem {
5968 match self.parse_item_or_view_item(attrs, false) {
5969 IoviViewItem(vi) => vi,
5970 _ => self.fatal("expected `use` or `extern crate`"),
5974 /// Parse, e.g., "use a::b::{z,y}"
5975 fn parse_use(&mut self) -> ViewItem_ {
5976 return ViewItemUse(self.parse_view_path());
5980 /// Matches view_path : MOD? non_global_path as IDENT
5981 /// | MOD? non_global_path MOD_SEP LBRACE RBRACE
5982 /// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE
5983 /// | MOD? non_global_path MOD_SEP STAR
5984 /// | MOD? non_global_path
5985 fn parse_view_path(&mut self) -> P<ViewPath> {
5986 let lo = self.span.lo;
5988 if self.check(&token::OpenDelim(token::Brace)) {
5990 let idents = self.parse_unspanned_seq(
5991 &token::OpenDelim(token::Brace),
5992 &token::CloseDelim(token::Brace),
5993 seq_sep_trailing_allowed(token::Comma),
5994 |p| p.parse_path_list_item());
5995 let path = ast::Path {
5996 span: mk_sp(lo, self.span.hi),
5998 segments: Vec::new()
6000 return P(spanned(lo, self.span.hi,
6001 ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
6004 let first_ident = self.parse_ident();
6005 let mut path = vec!(first_ident);
6010 let path_lo = self.span.lo;
6011 path = vec!(self.parse_ident());
6012 while self.check(&token::ModSep) {
6014 let id = self.parse_ident();
6017 let span = mk_sp(path_lo, self.span.hi);
6018 self.obsolete(span, ObsoleteImportRenaming);
6019 let path = ast::Path {
6022 segments: path.into_iter().map(|identifier| {
6024 identifier: identifier,
6025 parameters: ast::PathParameters::none(),
6029 return P(spanned(lo, self.span.hi,
6030 ViewPathSimple(first_ident, path,
6031 ast::DUMMY_NODE_ID)));
6035 // foo::bar or foo::{a,b,c} or foo::*
6036 while self.check(&token::ModSep) {
6040 token::Ident(i, _) => {
6045 // foo::bar::{a,b,c}
6046 token::OpenDelim(token::Brace) => {
6047 let idents = self.parse_unspanned_seq(
6048 &token::OpenDelim(token::Brace),
6049 &token::CloseDelim(token::Brace),
6050 seq_sep_trailing_allowed(token::Comma),
6051 |p| p.parse_path_list_item()
6053 let path = ast::Path {
6054 span: mk_sp(lo, self.span.hi),
6056 segments: path.into_iter().map(|identifier| {
6058 identifier: identifier,
6059 parameters: ast::PathParameters::none(),
6063 return P(spanned(lo, self.span.hi,
6064 ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
6068 token::BinOp(token::Star) => {
6070 let path = ast::Path {
6071 span: mk_sp(lo, self.span.hi),
6073 segments: path.into_iter().map(|identifier| {
6075 identifier: identifier,
6076 parameters: ast::PathParameters::none(),
6080 return P(spanned(lo, self.span.hi,
6081 ViewPathGlob(path, ast::DUMMY_NODE_ID)));
6090 let mut rename_to = path[path.len() - 1u];
6091 let path = ast::Path {
6092 span: mk_sp(lo, self.last_span.hi),
6094 segments: path.into_iter().map(|identifier| {
6096 identifier: identifier,
6097 parameters: ast::PathParameters::none(),
6101 if self.eat_keyword(keywords::As) {
6102 rename_to = self.parse_ident()
6106 ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID)))
6109 /// Parses a sequence of items. Stops when it finds program
6110 /// text that can't be parsed as an item
6111 /// - mod_items uses extern_mod_allowed = true
6112 /// - block_tail_ uses extern_mod_allowed = false
6113 fn parse_items_and_view_items(&mut self,
6114 first_item_attrs: Vec<Attribute> ,
6115 mut extern_mod_allowed: bool,
6116 macros_allowed: bool)
6117 -> ParsedItemsAndViewItems {
6118 let mut attrs = first_item_attrs;
6119 attrs.push_all(self.parse_outer_attributes()[]);
6120 // First, parse view items.
6121 let mut view_items : Vec<ast::ViewItem> = Vec::new();
6122 let mut items = Vec::new();
6124 // I think this code would probably read better as a single
6125 // loop with a mutable three-state-variable (for extern crates,
6126 // view items, and regular items) ... except that because
6127 // of macros, I'd like to delay that entire check until later.
6129 match self.parse_item_or_view_item(attrs, macros_allowed) {
6130 IoviNone(attrs) => {
6131 return ParsedItemsAndViewItems {
6132 attrs_remaining: attrs,
6133 view_items: view_items,
6135 foreign_items: Vec::new()
6138 IoviViewItem(view_item) => {
6139 match view_item.node {
6140 ViewItemUse(..) => {
6141 // `extern crate` must precede `use`.
6142 extern_mod_allowed = false;
6144 ViewItemExternCrate(..) if !extern_mod_allowed => {
6145 self.span_err(view_item.span,
6146 "\"extern crate\" declarations are \
6149 ViewItemExternCrate(..) => {}
6151 view_items.push(view_item);
6155 attrs = self.parse_outer_attributes();
6158 IoviForeignItem(_) => {
6162 attrs = self.parse_outer_attributes();
6165 // Next, parse items.
6167 match self.parse_item_or_view_item(attrs, macros_allowed) {
6168 IoviNone(returned_attrs) => {
6169 attrs = returned_attrs;
6172 IoviViewItem(view_item) => {
6173 attrs = self.parse_outer_attributes();
6174 self.span_err(view_item.span,
6175 "`use` and `extern crate` declarations must precede items");
6178 attrs = self.parse_outer_attributes();
6181 IoviForeignItem(_) => {
6187 ParsedItemsAndViewItems {
6188 attrs_remaining: attrs,
6189 view_items: view_items,
6191 foreign_items: Vec::new()
6195 /// Parses a sequence of foreign items. Stops when it finds program
6196 /// text that can't be parsed as an item
6197 fn parse_foreign_items(&mut self, first_item_attrs: Vec<Attribute> ,
6198 macros_allowed: bool)
6199 -> ParsedItemsAndViewItems {
6200 let mut attrs = first_item_attrs;
6201 attrs.push_all(self.parse_outer_attributes()[]);
6202 let mut foreign_items = Vec::new();
6204 match self.parse_foreign_item(attrs, macros_allowed) {
6205 IoviNone(returned_attrs) => {
6206 if self.check(&token::CloseDelim(token::Brace)) {
6207 attrs = returned_attrs;
6212 IoviViewItem(view_item) => {
6213 // I think this can't occur:
6214 self.span_err(view_item.span,
6215 "`use` and `extern crate` declarations must precede items");
6218 // FIXME #5668: this will occur for a macro invocation:
6219 self.span_fatal(item.span, "macros cannot expand to foreign items");
6221 IoviForeignItem(foreign_item) => {
6222 foreign_items.push(foreign_item);
6225 attrs = self.parse_outer_attributes();
6228 ParsedItemsAndViewItems {
6229 attrs_remaining: attrs,
6230 view_items: Vec::new(),
6232 foreign_items: foreign_items
6236 /// Parses a source module as a crate. This is the main
6237 /// entry point for the parser.
6238 pub fn parse_crate_mod(&mut self) -> Crate {
6239 let lo = self.span.lo;
6240 // parse the crate's inner attrs, maybe (oops) one
6241 // of the attrs of an item:
6242 let (inner, next) = self.parse_inner_attrs_and_next();
6243 let first_item_outer_attrs = next;
6244 // parse the items inside the crate:
6245 let m = self.parse_mod_items(token::Eof, first_item_outer_attrs, lo);
6250 config: self.cfg.clone(),
6251 span: mk_sp(lo, self.span.lo),
6252 exported_macros: Vec::new(),
6256 pub fn parse_optional_str(&mut self)
6257 -> Option<(InternedString, ast::StrStyle, Option<ast::Name>)> {
6258 let ret = match self.token {
6259 token::Literal(token::Str_(s), suf) => {
6260 (self.id_to_interned_str(s.ident()), ast::CookedStr, suf)
6262 token::Literal(token::StrRaw(s, n), suf) => {
6263 (self.id_to_interned_str(s.ident()), ast::RawStr(n), suf)
6271 pub fn parse_str(&mut self) -> (InternedString, StrStyle) {
6272 match self.parse_optional_str() {
6273 Some((s, style, suf)) => {
6274 let sp = self.last_span;
6275 self.expect_no_suffix(sp, "str literal", suf);
6278 _ => self.fatal("expected string literal")