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;
92 use std::str::from_str;
95 flags Restrictions: u8 {
96 const UNRESTRICTED = 0b0000,
97 const RESTRICTION_STMT_EXPR = 0b0001,
98 const RESTRICTION_NO_BAR_OP = 0b0010,
99 const RESTRICTION_NO_STRUCT_LITERAL = 0b0100,
100 const RESTRICTION_NO_DOTS = 0b1000,
105 type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
107 /// How to parse a path. There are four different kinds of paths, all of which
108 /// are parsed somewhat differently.
109 #[deriving(Copy, PartialEq)]
110 pub enum PathParsingMode {
111 /// A path with no type parameters; e.g. `foo::bar::Baz`
113 /// A path with a lifetime and type parameters, with no double colons
114 /// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`
115 LifetimeAndTypesWithoutColons,
116 /// A path with a lifetime and type parameters with double colons before
117 /// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`
118 LifetimeAndTypesWithColons,
121 /// How to parse a bound, whether to allow bound modifiers such as `?`.
122 #[deriving(Copy, PartialEq)]
123 pub enum BoundParsingMode {
128 enum ItemOrViewItem {
129 /// Indicates a failure to parse any kind of item. The attributes are
131 IoviNone(Vec<Attribute>),
133 IoviForeignItem(P<ForeignItem>),
134 IoviViewItem(ViewItem)
138 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
139 /// dropped into the token stream, which happens while parsing the result of
140 /// macro expansion). Placement of these is not as complex as I feared it would
141 /// be. The important thing is to make sure that lookahead doesn't balk at
142 /// `token::Interpolated` tokens.
143 macro_rules! maybe_whole_expr {
146 let found = match $p.token {
147 token::Interpolated(token::NtExpr(ref e)) => {
150 token::Interpolated(token::NtPath(_)) => {
151 // FIXME: The following avoids an issue with lexical borrowck scopes,
152 // but the clone is unfortunate.
153 let pt = match $p.token {
154 token::Interpolated(token::NtPath(ref pt)) => (**pt).clone(),
158 Some($p.mk_expr(span.lo, span.hi, ExprPath(pt)))
160 token::Interpolated(token::NtBlock(_)) => {
161 // FIXME: The following avoids an issue with lexical borrowck scopes,
162 // but the clone is unfortunate.
163 let b = match $p.token {
164 token::Interpolated(token::NtBlock(ref b)) => (*b).clone(),
168 Some($p.mk_expr(span.lo, span.hi, ExprBlock(b)))
183 /// As maybe_whole_expr, but for things other than expressions
184 macro_rules! maybe_whole {
185 ($p:expr, $constructor:ident) => (
187 let found = match ($p).token {
188 token::Interpolated(token::$constructor(_)) => {
189 Some(($p).bump_and_get())
193 if let Some(token::Interpolated(token::$constructor(x))) = found {
198 (no_clone $p:expr, $constructor:ident) => (
200 let found = match ($p).token {
201 token::Interpolated(token::$constructor(_)) => {
202 Some(($p).bump_and_get())
206 if let Some(token::Interpolated(token::$constructor(x))) = found {
211 (deref $p:expr, $constructor:ident) => (
213 let found = match ($p).token {
214 token::Interpolated(token::$constructor(_)) => {
215 Some(($p).bump_and_get())
219 if let Some(token::Interpolated(token::$constructor(x))) = found {
224 (Some $p:expr, $constructor:ident) => (
226 let found = match ($p).token {
227 token::Interpolated(token::$constructor(_)) => {
228 Some(($p).bump_and_get())
232 if let Some(token::Interpolated(token::$constructor(x))) = found {
233 return Some(x.clone());
237 (iovi $p:expr, $constructor:ident) => (
239 let found = match ($p).token {
240 token::Interpolated(token::$constructor(_)) => {
241 Some(($p).bump_and_get())
245 if let Some(token::Interpolated(token::$constructor(x))) = found {
246 return IoviItem(x.clone());
250 (pair_empty $p:expr, $constructor:ident) => (
252 let found = match ($p).token {
253 token::Interpolated(token::$constructor(_)) => {
254 Some(($p).bump_and_get())
258 if let Some(token::Interpolated(token::$constructor(x))) = found {
259 return (Vec::new(), x);
266 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
269 Some(ref attrs) => lhs.extend(attrs.iter().map(|a| a.clone())),
276 struct ParsedItemsAndViewItems {
277 attrs_remaining: Vec<Attribute>,
278 view_items: Vec<ViewItem>,
279 items: Vec<P<Item>> ,
280 foreign_items: Vec<P<ForeignItem>>
283 /* ident is handled by common.rs */
285 pub struct Parser<'a> {
286 pub sess: &'a ParseSess,
287 /// the current token:
288 pub token: token::Token,
289 /// the span of the current token:
291 /// the span of the prior token:
293 pub cfg: CrateConfig,
294 /// the previous token or None (only stashed sometimes).
295 pub last_token: Option<Box<token::Token>>,
296 pub buffer: [TokenAndSpan; 4],
297 pub buffer_start: int,
299 pub tokens_consumed: uint,
300 pub restrictions: Restrictions,
301 pub quote_depth: uint, // not (yet) related to the quasiquoter
302 pub reader: Box<Reader+'a>,
303 pub interner: Rc<token::IdentInterner>,
304 /// The set of seen errors about obsolete syntax. Used to suppress
305 /// extra detail when the same error is seen twice
306 pub obsolete_set: HashSet<ObsoleteSyntax>,
307 /// Used to determine the path to externally loaded source files
308 pub mod_path_stack: Vec<InternedString>,
309 /// Stack of spans of open delimiters. Used for error message.
310 pub open_braces: Vec<Span>,
311 /// Flag if this parser "owns" the directory that it is currently parsing
312 /// in. This will affect how nested files are looked up.
313 pub owns_directory: bool,
314 /// Name of the root module this parser originated from. If `None`, then the
315 /// name is not known. This does not change while the parser is descending
316 /// into modules, and sub-parsers have new values for this name.
317 pub root_module_name: Option<String>,
318 pub expected_tokens: Vec<TokenType>,
321 #[deriving(PartialEq, Eq, Clone)]
328 fn to_string(&self) -> String {
330 TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
331 TokenType::Operator => "an operator".to_string(),
336 fn is_plain_ident_or_underscore(t: &token::Token) -> bool {
337 t.is_plain_ident() || *t == token::Underscore
340 impl<'a> Parser<'a> {
341 pub fn new(sess: &'a ParseSess,
342 cfg: ast::CrateConfig,
343 mut rdr: Box<Reader+'a>)
346 let tok0 = rdr.real_token();
348 let placeholder = TokenAndSpan {
349 tok: token::Underscore,
355 interner: token::get_ident_interner(),
371 restrictions: UNRESTRICTED,
373 obsolete_set: HashSet::new(),
374 mod_path_stack: Vec::new(),
375 open_braces: Vec::new(),
376 owns_directory: true,
377 root_module_name: None,
378 expected_tokens: Vec::new(),
382 /// Convert a token to a string using self's reader
383 pub fn token_to_string(token: &token::Token) -> String {
384 pprust::token_to_string(token)
387 /// Convert the current token to a string using self's reader
388 pub fn this_token_to_string(&mut self) -> String {
389 Parser::token_to_string(&self.token)
392 pub fn unexpected_last(&mut self, t: &token::Token) -> ! {
393 let token_str = Parser::token_to_string(t);
394 let last_span = self.last_span;
395 self.span_fatal(last_span, format!("unexpected token: `{}`",
399 pub fn unexpected(&mut self) -> ! {
400 let this_token = self.this_token_to_string();
401 self.fatal(format!("unexpected token: `{}`", this_token)[]);
404 /// Expect and consume the token t. Signal an error if
405 /// the next token is not t.
406 pub fn expect(&mut self, t: &token::Token) {
407 if self.expected_tokens.is_empty() {
408 if self.token == *t {
411 let token_str = Parser::token_to_string(t);
412 let this_token_str = self.this_token_to_string();
413 self.fatal(format!("expected `{}`, found `{}`",
418 self.expect_one_of(slice::ref_slice(t), &[]);
422 /// Expect next token to be edible or inedible token. If edible,
423 /// then consume it; if inedible, then return without consuming
424 /// anything. Signal a fatal error if next token is unexpected.
425 pub fn expect_one_of(&mut self,
426 edible: &[token::Token],
427 inedible: &[token::Token]) {
428 fn tokens_to_string(tokens: &[TokenType]) -> String {
429 let mut i = tokens.iter();
430 // This might be a sign we need a connect method on Iterator.
432 .map_or("".to_string(), |t| t.to_string());
433 i.enumerate().fold(b, |mut b, (i, ref a)| {
434 if tokens.len() > 2 && i == tokens.len() - 2 {
436 } else if tokens.len() == 2 && i == tokens.len() - 2 {
441 b.push_str(&*a.to_string());
445 if edible.contains(&self.token) {
447 } else if inedible.contains(&self.token) {
448 // leave it in the input
450 let mut expected = edible.iter().map(|x| TokenType::Token(x.clone()))
451 .collect::<Vec<_>>();
452 expected.extend(inedible.iter().map(|x| TokenType::Token(x.clone())));
453 expected.push_all(&*self.expected_tokens);
454 expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
456 let expect = tokens_to_string(expected[]);
457 let actual = self.this_token_to_string();
459 (if expected.len() != 1 {
460 (format!("expected one of {}, found `{}`",
464 (format!("expected {}, found `{}`",
472 /// Check for erroneous `ident { }`; if matches, signal error and
473 /// recover (without consuming any expected input token). Returns
474 /// true if and only if input was consumed for recovery.
475 pub fn check_for_erroneous_unit_struct_expecting(&mut self, expected: &[token::Token]) -> bool {
476 if self.token == token::OpenDelim(token::Brace)
477 && expected.iter().all(|t| *t != token::OpenDelim(token::Brace))
478 && self.look_ahead(1, |t| *t == token::CloseDelim(token::Brace)) {
479 // matched; signal non-fatal error and recover.
480 let span = self.span;
482 "unit-like struct construction is written with no trailing `{ }`");
483 self.eat(&token::OpenDelim(token::Brace));
484 self.eat(&token::CloseDelim(token::Brace));
491 /// Commit to parsing a complete expression `e` expected to be
492 /// followed by some token from the set edible + inedible. Recover
493 /// from anticipated input errors, discarding erroneous characters.
494 pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[token::Token]) {
495 debug!("commit_expr {}", e);
496 if let ExprPath(..) = e.node {
497 // might be unit-struct construction; check for recoverableinput error.
498 let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
499 expected.push_all(inedible);
500 self.check_for_erroneous_unit_struct_expecting(expected[]);
502 self.expect_one_of(edible, inedible)
505 pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) {
506 self.commit_expr(e, &[edible], &[])
509 /// Commit to parsing a complete statement `s`, which expects to be
510 /// followed by some token from the set edible + inedible. Check
511 /// for recoverable input errors, discarding erroneous characters.
512 pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token]) {
515 .map_or(false, |t| t.is_ident() || t.is_path()) {
516 let mut expected = edible.iter().map(|x| x.clone()).collect::<Vec<_>>();
517 expected.push_all(inedible[]);
518 self.check_for_erroneous_unit_struct_expecting(
521 self.expect_one_of(edible, inedible)
524 pub fn commit_stmt_expecting(&mut self, edible: token::Token) {
525 self.commit_stmt(&[edible], &[])
528 pub fn parse_ident(&mut self) -> ast::Ident {
529 self.check_strict_keywords();
530 self.check_reserved_keywords();
532 token::Ident(i, _) => {
536 token::Interpolated(token::NtIdent(..)) => {
537 self.bug("ident interpolation not converted to real token");
540 let token_str = self.this_token_to_string();
541 self.fatal((format!("expected ident, found `{}`",
547 pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
548 let lo = self.span.lo;
549 let node = if self.eat_keyword(keywords::Mod) {
550 let span = self.last_span;
551 self.span_warn(span, "deprecated syntax; use the `self` keyword now");
552 ast::PathListMod { id: ast::DUMMY_NODE_ID }
553 } else if self.eat_keyword(keywords::Self) {
554 ast::PathListMod { id: ast::DUMMY_NODE_ID }
556 let ident = self.parse_ident();
557 ast::PathListIdent { name: ident, id: ast::DUMMY_NODE_ID }
559 let hi = self.last_span.hi;
560 spanned(lo, hi, node)
563 /// Check if the next token is `tok`, and return `true` if so.
565 /// This method is will automatically add `tok` to `expected_tokens` if `tok` is not
567 pub fn check(&mut self, tok: &token::Token) -> bool {
568 let is_present = self.token == *tok;
569 if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
573 /// Consume token 'tok' if it exists. Returns true if the given
574 /// token was present, false otherwise.
575 pub fn eat(&mut self, tok: &token::Token) -> bool {
576 let is_present = self.check(tok);
577 if is_present { self.bump() }
581 /// If the next token is the given keyword, eat it and return
582 /// true. Otherwise, return false.
583 pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
584 if self.token.is_keyword(kw) {
592 /// If the given word is not a keyword, signal an error.
593 /// If the next token is not the given word, signal an error.
594 /// Otherwise, eat it.
595 pub fn expect_keyword(&mut self, kw: keywords::Keyword) {
596 if !self.eat_keyword(kw) {
597 let id_interned_str = token::get_name(kw.to_name());
598 let token_str = self.this_token_to_string();
599 self.fatal(format!("expected `{}`, found `{}`",
600 id_interned_str, token_str)[])
604 /// Signal an error if the given string is a strict keyword
605 pub fn check_strict_keywords(&mut self) {
606 if self.token.is_strict_keyword() {
607 let token_str = self.this_token_to_string();
608 let span = self.span;
610 format!("expected identifier, found keyword `{}`",
615 /// Signal an error if the current token is a reserved keyword
616 pub fn check_reserved_keywords(&mut self) {
617 if self.token.is_reserved_keyword() {
618 let token_str = self.this_token_to_string();
619 self.fatal(format!("`{}` is a reserved keyword",
624 /// Expect and consume an `&`. If `&&` is seen, replace it with a single
625 /// `&` and continue. If an `&` is not seen, signal an error.
626 fn expect_and(&mut self) {
628 token::BinOp(token::And) => self.bump(),
630 let span = self.span;
631 let lo = span.lo + BytePos(1);
632 self.replace_token(token::BinOp(token::And), lo, span.hi)
635 let token_str = self.this_token_to_string();
637 Parser::token_to_string(&token::BinOp(token::And));
638 self.fatal(format!("expected `{}`, found `{}`",
645 /// Expect and consume a `|`. If `||` is seen, replace it with a single
646 /// `|` and continue. If a `|` is not seen, signal an error.
647 fn expect_or(&mut self) {
649 token::BinOp(token::Or) => self.bump(),
651 let span = self.span;
652 let lo = span.lo + BytePos(1);
653 self.replace_token(token::BinOp(token::Or), lo, span.hi)
656 let found_token = self.this_token_to_string();
658 Parser::token_to_string(&token::BinOp(token::Or));
659 self.fatal(format!("expected `{}`, found `{}`",
666 pub fn expect_no_suffix(&mut self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
668 None => {/* everything ok */}
670 let text = suf.as_str();
672 self.span_bug(sp, "found empty literal suffix in Some")
674 self.span_err(sp, &*format!("{} with a suffix is illegal", kind));
680 /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
681 /// `<` and continue. If a `<` is not seen, return false.
683 /// This is meant to be used when parsing generics on a path to get the
685 fn eat_lt(&mut self) -> bool {
687 token::Lt => { self.bump(); true }
688 token::BinOp(token::Shl) => {
689 let span = self.span;
690 let lo = span.lo + BytePos(1);
691 self.replace_token(token::Lt, lo, span.hi);
698 fn expect_lt(&mut self) {
700 let found_token = self.this_token_to_string();
701 let token_str = Parser::token_to_string(&token::Lt);
702 self.fatal(format!("expected `{}`, found `{}`",
708 /// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
709 fn parse_seq_to_before_or<T, F>(&mut self,
713 F: FnMut(&mut Parser) -> T,
715 let mut first = true;
716 let mut vector = Vec::new();
717 while self.token != token::BinOp(token::Or) &&
718 self.token != token::OrOr {
730 /// Expect and consume a GT. if a >> is seen, replace it
731 /// with a single > and continue. If a GT is not seen,
733 pub fn expect_gt(&mut self) {
735 token::Gt => self.bump(),
736 token::BinOp(token::Shr) => {
737 let span = self.span;
738 let lo = span.lo + BytePos(1);
739 self.replace_token(token::Gt, lo, span.hi)
741 token::BinOpEq(token::Shr) => {
742 let span = self.span;
743 let lo = span.lo + BytePos(1);
744 self.replace_token(token::Ge, lo, span.hi)
747 let span = self.span;
748 let lo = span.lo + BytePos(1);
749 self.replace_token(token::Eq, lo, span.hi)
752 let gt_str = Parser::token_to_string(&token::Gt);
753 let this_token_str = self.this_token_to_string();
754 self.fatal(format!("expected `{}`, found `{}`",
761 pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
762 sep: Option<token::Token>,
764 -> (OwnedSlice<T>, bool) where
765 F: FnMut(&mut Parser) -> Option<T>,
767 let mut v = Vec::new();
768 // This loop works by alternating back and forth between parsing types
769 // and commas. For example, given a string `A, B,>`, the parser would
770 // first parse `A`, then a comma, then `B`, then a comma. After that it
771 // would encounter a `>` and stop. This lets the parser handle trailing
772 // commas in generic parameters, because it can stop either after
773 // parsing a type or after parsing a comma.
774 for i in iter::count(0u, 1) {
775 if self.check(&token::Gt)
776 || self.token == token::BinOp(token::Shr)
777 || self.token == token::Ge
778 || self.token == token::BinOpEq(token::Shr) {
784 Some(result) => v.push(result),
785 None => return (OwnedSlice::from_vec(v), true)
788 sep.as_ref().map(|t| self.expect(t));
791 return (OwnedSlice::from_vec(v), false);
794 /// Parse a sequence bracketed by '<' and '>', stopping
796 pub fn parse_seq_to_before_gt<T, F>(&mut self,
797 sep: Option<token::Token>,
799 -> OwnedSlice<T> where
800 F: FnMut(&mut Parser) -> T,
802 let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, |p| Some(f(p)));
807 pub fn parse_seq_to_gt<T, F>(&mut self,
808 sep: Option<token::Token>,
810 -> OwnedSlice<T> where
811 F: FnMut(&mut Parser) -> T,
813 let v = self.parse_seq_to_before_gt(sep, f);
818 pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
819 sep: Option<token::Token>,
821 -> (OwnedSlice<T>, bool) where
822 F: FnMut(&mut Parser) -> Option<T>,
824 let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f);
828 return (v, returned);
831 /// Parse a sequence, including the closing delimiter. The function
832 /// f must consume tokens until reaching the next separator or
834 pub fn parse_seq_to_end<T, F>(&mut self,
839 F: FnMut(&mut Parser) -> T,
841 let val = self.parse_seq_to_before_end(ket, sep, f);
846 /// Parse a sequence, not including the closing delimiter. The function
847 /// f must consume tokens until reaching the next separator or
849 pub fn parse_seq_to_before_end<T, F>(&mut self,
854 F: FnMut(&mut Parser) -> T,
856 let mut first: bool = true;
858 while self.token != *ket {
861 if first { first = false; }
862 else { self.expect(t); }
866 if sep.trailing_sep_allowed && self.check(ket) { break; }
872 /// Parse a sequence, including the closing delimiter. The function
873 /// f must consume tokens until reaching the next separator or
875 pub fn parse_unspanned_seq<T, F>(&mut self,
881 F: FnMut(&mut Parser) -> T,
884 let result = self.parse_seq_to_before_end(ket, sep, f);
889 /// Parse a sequence parameter of enum variant. For consistency purposes,
890 /// these should not be empty.
891 pub fn parse_enum_variant_seq<T, F>(&mut self,
897 F: FnMut(&mut Parser) -> T,
899 let result = self.parse_unspanned_seq(bra, ket, sep, f);
900 if result.is_empty() {
901 let last_span = self.last_span;
902 self.span_err(last_span,
903 "nullary enum variants are written with no trailing `( )`");
908 // NB: Do not use this function unless you actually plan to place the
909 // spanned list in the AST.
910 pub fn parse_seq<T, F>(&mut self,
915 -> Spanned<Vec<T>> where
916 F: FnMut(&mut Parser) -> T,
918 let lo = self.span.lo;
920 let result = self.parse_seq_to_before_end(ket, sep, f);
921 let hi = self.span.hi;
923 spanned(lo, hi, result)
926 /// Advance the parser by one token
927 pub fn bump(&mut self) {
928 self.last_span = self.span;
929 // Stash token for error recovery (sometimes; clone is not necessarily cheap).
930 self.last_token = if self.token.is_ident() || self.token.is_path() {
931 Some(box self.token.clone())
935 let next = if self.buffer_start == self.buffer_end {
936 self.reader.real_token()
938 // Avoid token copies with `replace`.
939 let buffer_start = self.buffer_start as uint;
940 let next_index = (buffer_start + 1) & 3 as uint;
941 self.buffer_start = next_index as int;
943 let placeholder = TokenAndSpan {
944 tok: token::Underscore,
947 mem::replace(&mut self.buffer[buffer_start], placeholder)
950 self.token = next.tok;
951 self.tokens_consumed += 1u;
952 self.expected_tokens.clear();
955 /// Advance the parser by one token and return the bumped token.
956 pub fn bump_and_get(&mut self) -> token::Token {
957 let old_token = mem::replace(&mut self.token, token::Underscore);
962 /// EFFECT: replace the current token and span with the given one
963 pub fn replace_token(&mut self,
967 self.last_span = mk_sp(self.span.lo, lo);
969 self.span = mk_sp(lo, hi);
971 pub fn buffer_length(&mut self) -> int {
972 if self.buffer_start <= self.buffer_end {
973 return self.buffer_end - self.buffer_start;
975 return (4 - self.buffer_start) + self.buffer_end;
977 pub fn look_ahead<R, F>(&mut self, distance: uint, f: F) -> R where
978 F: FnOnce(&token::Token) -> R,
980 let dist = distance as int;
981 while self.buffer_length() < dist {
982 self.buffer[self.buffer_end as uint] = self.reader.real_token();
983 self.buffer_end = (self.buffer_end + 1) & 3;
985 f(&self.buffer[((self.buffer_start + dist - 1) & 3) as uint].tok)
987 pub fn fatal(&mut self, m: &str) -> ! {
988 self.sess.span_diagnostic.span_fatal(self.span, m)
990 pub fn span_fatal(&mut self, sp: Span, m: &str) -> ! {
991 self.sess.span_diagnostic.span_fatal(sp, m)
993 pub fn span_fatal_help(&mut self, sp: Span, m: &str, help: &str) -> ! {
994 self.span_err(sp, m);
995 self.span_help(sp, help);
996 panic!(diagnostic::FatalError);
998 pub fn span_note(&mut self, sp: Span, m: &str) {
999 self.sess.span_diagnostic.span_note(sp, m)
1001 pub fn span_help(&mut self, sp: Span, m: &str) {
1002 self.sess.span_diagnostic.span_help(sp, m)
1004 pub fn bug(&mut self, m: &str) -> ! {
1005 self.sess.span_diagnostic.span_bug(self.span, m)
1007 pub fn warn(&mut self, m: &str) {
1008 self.sess.span_diagnostic.span_warn(self.span, m)
1010 pub fn span_warn(&mut self, sp: Span, m: &str) {
1011 self.sess.span_diagnostic.span_warn(sp, m)
1013 pub fn span_err(&mut self, sp: Span, m: &str) {
1014 self.sess.span_diagnostic.span_err(sp, m)
1016 pub fn span_bug(&mut self, sp: Span, m: &str) -> ! {
1017 self.sess.span_diagnostic.span_bug(sp, m)
1019 pub fn abort_if_errors(&mut self) {
1020 self.sess.span_diagnostic.handler().abort_if_errors();
1023 pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString {
1024 token::get_ident(id)
1027 /// Is the current token one of the keywords that signals a bare function
1029 pub fn token_is_bare_fn_keyword(&mut self) -> bool {
1030 self.token.is_keyword(keywords::Fn) ||
1031 self.token.is_keyword(keywords::Unsafe) ||
1032 self.token.is_keyword(keywords::Extern)
1035 /// Is the current token one of the keywords that signals a closure type?
1036 pub fn token_is_closure_keyword(&mut self) -> bool {
1037 self.token.is_keyword(keywords::Unsafe)
1040 pub fn get_lifetime(&mut self) -> ast::Ident {
1042 token::Lifetime(ref ident) => *ident,
1043 _ => self.bug("not a lifetime"),
1047 pub fn parse_for_in_type(&mut self) -> Ty_ {
1049 Parses whatever can come after a `for` keyword in a type.
1050 The `for` has already been consumed.
1054 - for <'lt> |S| -> T
1058 - for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
1059 - for <'lt> path::foo(a, b)
1064 let lifetime_defs = self.parse_late_bound_lifetime_defs();
1066 // examine next token to decide to do
1067 if self.eat_keyword(keywords::Proc) {
1068 self.parse_proc_type(lifetime_defs)
1069 } else if self.token_is_bare_fn_keyword() || self.token_is_closure_keyword() {
1070 self.parse_ty_bare_fn_or_ty_closure(lifetime_defs)
1071 } else if self.check(&token::ModSep) ||
1072 self.token.is_ident() ||
1073 self.token.is_path()
1075 let trait_ref = self.parse_trait_ref();
1076 let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
1077 trait_ref: trait_ref };
1078 let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
1079 self.parse_ty_param_bounds(BoundParsingMode::Bare)
1084 Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
1085 .chain(other_bounds.into_vec().into_iter())
1087 ast::TyPolyTraitRef(all_bounds)
1089 self.parse_ty_closure(lifetime_defs)
1093 pub fn parse_ty_path(&mut self) -> Ty_ {
1094 let path = self.parse_path(LifetimeAndTypesWithoutColons);
1095 TyPath(path, ast::DUMMY_NODE_ID)
1098 /// parse a TyBareFn type:
1099 pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1102 [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1103 ^~~~^ ^~~~^ ^~~~^ ^~^ ^
1106 | | | Argument types
1112 let unsafety = self.parse_unsafety();
1113 let abi = if self.eat_keyword(keywords::Extern) {
1114 self.parse_opt_abi().unwrap_or(abi::C)
1119 self.expect_keyword(keywords::Fn);
1120 let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1121 let (inputs, variadic) = self.parse_fn_args(false, true);
1122 let ret_ty = self.parse_ret_ty();
1123 let decl = P(FnDecl {
1128 TyBareFn(P(BareFnTy {
1131 lifetimes: lifetime_defs,
1136 /// Parses a procedure type (`proc`). The initial `proc` keyword must
1137 /// already have been parsed.
1138 pub fn parse_proc_type(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1141 proc <'lt> (S) [:Bounds] -> T
1142 ^~~^ ^~~~^ ^ ^~~~~~~~^ ^
1148 the `proc` keyword (already consumed)
1152 let proc_span = self.last_span;
1154 // To be helpful, parse the proc as ever
1155 let _ = self.parse_legacy_lifetime_defs(lifetime_defs);
1156 let _ = self.parse_fn_args(false, false);
1157 let _ = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1158 let _ = self.parse_ret_ty();
1160 self.obsolete(proc_span, ObsoleteProcType);
1165 /// Parses an optional unboxed closure kind (`&:`, `&mut:`, or `:`).
1166 pub fn parse_optional_unboxed_closure_kind(&mut self)
1167 -> Option<UnboxedClosureKind> {
1168 if self.check(&token::BinOp(token::And)) &&
1169 self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
1170 self.look_ahead(2, |t| *t == token::Colon) {
1174 return Some(FnMutUnboxedClosureKind)
1177 if self.token == token::BinOp(token::And) &&
1178 self.look_ahead(1, |t| *t == token::Colon) {
1181 return Some(FnUnboxedClosureKind)
1184 if self.eat(&token::Colon) {
1185 return Some(FnOnceUnboxedClosureKind)
1191 pub fn parse_ty_bare_fn_or_ty_closure(&mut self, lifetime_defs: Vec<LifetimeDef>) -> Ty_ {
1192 // Both bare fns and closures can begin with stuff like unsafe
1193 // and extern. So we just scan ahead a few tokens to see if we see
1196 // Closure: [unsafe] <'lt> |S| [:Bounds] -> T
1197 // Fn: [unsafe] [extern "ABI"] fn <'lt> (S) -> T
1199 if self.token.is_keyword(keywords::Fn) {
1200 self.parse_ty_bare_fn(lifetime_defs)
1201 } else if self.token.is_keyword(keywords::Extern) {
1202 self.parse_ty_bare_fn(lifetime_defs)
1203 } else if self.token.is_keyword(keywords::Unsafe) {
1204 if self.look_ahead(1, |t| t.is_keyword(keywords::Fn) ||
1205 t.is_keyword(keywords::Extern)) {
1206 self.parse_ty_bare_fn(lifetime_defs)
1208 self.parse_ty_closure(lifetime_defs)
1211 self.parse_ty_closure(lifetime_defs)
1215 /// Parse a TyClosure type
1216 pub fn parse_ty_closure(&mut self, lifetime_defs: Vec<ast::LifetimeDef>) -> Ty_ {
1219 [unsafe] <'lt> |S| [:Bounds] -> T
1220 ^~~~~~~^ ^~~~^ ^ ^~~~~~~~^ ^
1223 | | | Closure bounds
1225 | Deprecated lifetime defs
1231 let unsafety = self.parse_unsafety();
1233 let lifetime_defs = self.parse_legacy_lifetime_defs(lifetime_defs);
1235 let inputs = if self.eat(&token::OrOr) {
1240 let inputs = self.parse_seq_to_before_or(
1242 |p| p.parse_arg_general(false));
1247 let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
1249 let output = self.parse_ret_ty();
1250 let decl = P(FnDecl {
1256 TyClosure(P(ClosureTy {
1261 lifetimes: lifetime_defs,
1265 pub fn parse_unsafety(&mut self) -> Unsafety {
1266 if self.eat_keyword(keywords::Unsafe) {
1267 return Unsafety::Unsafe;
1269 return Unsafety::Normal;
1273 /// Parses `[ 'for' '<' lifetime_defs '>' ]'
1274 fn parse_legacy_lifetime_defs(&mut self,
1275 lifetime_defs: Vec<ast::LifetimeDef>)
1276 -> Vec<ast::LifetimeDef>
1278 if self.token == token::Lt {
1280 if lifetime_defs.is_empty() {
1281 self.warn("deprecated syntax; use the `for` keyword now \
1282 (e.g. change `fn<'a>` to `for<'a> fn`)");
1283 let lifetime_defs = self.parse_lifetime_defs();
1287 self.fatal("cannot use new `for` keyword and older syntax together");
1294 /// Parses `type Foo;` in a trait declaration only. The `type` keyword has
1295 /// already been parsed.
1296 fn parse_associated_type(&mut self, attrs: Vec<Attribute>)
1299 let ty_param = self.parse_ty_param();
1300 self.expect(&token::Semi);
1307 /// Parses `type Foo = TYPE;` in an implementation declaration only. The
1308 /// `type` keyword has already been parsed.
1309 fn parse_typedef(&mut self, attrs: Vec<Attribute>, vis: Visibility)
1311 let lo = self.span.lo;
1312 let ident = self.parse_ident();
1313 self.expect(&token::Eq);
1314 let typ = self.parse_ty_sum();
1315 let hi = self.span.hi;
1316 self.expect(&token::Semi);
1318 id: ast::DUMMY_NODE_ID,
1319 span: mk_sp(lo, hi),
1327 /// Parse the items in a trait declaration
1328 pub fn parse_trait_items(&mut self) -> Vec<TraitItem> {
1329 self.parse_unspanned_seq(
1330 &token::OpenDelim(token::Brace),
1331 &token::CloseDelim(token::Brace),
1334 let attrs = p.parse_outer_attributes();
1336 if p.eat_keyword(keywords::Type) {
1337 TypeTraitItem(P(p.parse_associated_type(attrs)))
1341 let vis = p.parse_visibility();
1342 let style = p.parse_unsafety();
1343 let abi = if p.eat_keyword(keywords::Extern) {
1344 p.parse_opt_abi().unwrap_or(abi::C)
1348 p.expect_keyword(keywords::Fn);
1350 let ident = p.parse_ident();
1351 let mut generics = p.parse_generics();
1353 let (explicit_self, d) = p.parse_fn_decl_with_self(|p| {
1354 // This is somewhat dubious; We don't want to allow
1355 // argument names to be left off if there is a
1357 p.parse_arg_general(false)
1360 p.parse_where_clause(&mut generics);
1362 let hi = p.last_span.hi;
1366 debug!("parse_trait_methods(): parsing required method");
1367 RequiredMethod(TypeMethod {
1374 explicit_self: explicit_self,
1375 id: ast::DUMMY_NODE_ID,
1376 span: mk_sp(lo, hi),
1380 token::OpenDelim(token::Brace) => {
1381 debug!("parse_trait_methods(): parsing provided method");
1382 let (inner_attrs, body) =
1383 p.parse_inner_attrs_and_block();
1384 let mut attrs = attrs;
1385 attrs.push_all(inner_attrs[]);
1386 ProvidedMethod(P(ast::Method {
1388 id: ast::DUMMY_NODE_ID,
1389 span: mk_sp(lo, hi),
1390 node: ast::MethDecl(ident,
1402 let token_str = p.this_token_to_string();
1403 p.fatal((format!("expected `;` or `{{`, found `{}`",
1411 /// Parse a possibly mutable type
1412 pub fn parse_mt(&mut self) -> MutTy {
1413 let mutbl = self.parse_mutability();
1414 let t = self.parse_ty();
1415 MutTy { ty: t, mutbl: mutbl }
1418 /// Parse [mut/const/imm] ID : TY
1419 /// now used only by obsolete record syntax parser...
1420 pub fn parse_ty_field(&mut self) -> TypeField {
1421 let lo = self.span.lo;
1422 let mutbl = self.parse_mutability();
1423 let id = self.parse_ident();
1424 self.expect(&token::Colon);
1425 let ty = self.parse_ty_sum();
1426 let hi = ty.span.hi;
1429 mt: MutTy { ty: ty, mutbl: mutbl },
1430 span: mk_sp(lo, hi),
1434 /// Parse optional return type [ -> TY ] in function decl
1435 pub fn parse_ret_ty(&mut self) -> FunctionRetTy {
1436 if self.eat(&token::RArrow) {
1437 if self.eat(&token::Not) {
1440 let t = self.parse_ty();
1442 // We used to allow `fn foo() -> &T + U`, but don't
1443 // anymore. If we see it, report a useful error. This
1444 // only makes sense because `parse_ret_ty` is only
1445 // used in fn *declarations*, not fn types or where
1446 // clauses (i.e., not when parsing something like
1447 // `FnMut() -> T + Send`, where the `+` is legal).
1448 if self.token == token::BinOp(token::Plus) {
1449 self.warn("deprecated syntax: `()` are required, see RFC 438 for details");
1455 let pos = self.span.lo;
1457 id: ast::DUMMY_NODE_ID,
1458 node: TyTup(vec![]),
1459 span: mk_sp(pos, pos),
1464 /// Parse a type in a context where `T1+T2` is allowed.
1465 pub fn parse_ty_sum(&mut self) -> P<Ty> {
1466 let lo = self.span.lo;
1467 let lhs = self.parse_ty();
1469 if !self.eat(&token::BinOp(token::Plus)) {
1473 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
1475 // In type grammar, `+` is treated like a binary operator,
1476 // and hence both L and R side are required.
1477 if bounds.len() == 0 {
1478 let last_span = self.last_span;
1479 self.span_err(last_span,
1480 "at least one type parameter bound \
1481 must be specified");
1484 let sp = mk_sp(lo, self.last_span.hi);
1485 let sum = ast::TyObjectSum(lhs, bounds);
1486 P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})
1490 pub fn parse_ty(&mut self) -> P<Ty> {
1491 maybe_whole!(no_clone self, NtTy);
1493 let lo = self.span.lo;
1495 let t = if self.check(&token::OpenDelim(token::Paren)) {
1498 // (t) is a parenthesized ty
1499 // (t,) is the type of a tuple with only one field,
1501 let mut ts = vec![];
1502 let mut last_comma = false;
1503 while self.token != token::CloseDelim(token::Paren) {
1504 ts.push(self.parse_ty_sum());
1505 if self.check(&token::Comma) {
1514 self.expect(&token::CloseDelim(token::Paren));
1515 if ts.len() == 1 && !last_comma {
1516 TyParen(ts.into_iter().nth(0).unwrap())
1520 } else if self.token == token::Tilde {
1523 let last_span = self.last_span;
1525 token::OpenDelim(token::Bracket) => self.obsolete(last_span, ObsoleteOwnedVector),
1526 _ => self.obsolete(last_span, ObsoleteOwnedType)
1528 TyTup(vec![self.parse_ty()])
1529 } else if self.check(&token::BinOp(token::Star)) {
1530 // STAR POINTER (bare pointer?)
1532 TyPtr(self.parse_ptr())
1533 } else if self.check(&token::OpenDelim(token::Bracket)) {
1535 self.expect(&token::OpenDelim(token::Bracket));
1536 let t = self.parse_ty_sum();
1538 // Parse the `; e` in `[ int; e ]`
1539 // where `e` is a const expression
1540 let t = match self.maybe_parse_fixed_length_of_vec() {
1542 Some(suffix) => TyFixedLengthVec(t, suffix)
1544 self.expect(&token::CloseDelim(token::Bracket));
1546 } else if self.check(&token::BinOp(token::And)) ||
1547 self.token == token::AndAnd {
1550 self.parse_borrowed_pointee()
1551 } else if self.token.is_keyword(keywords::For) {
1552 self.parse_for_in_type()
1553 } else if self.token_is_bare_fn_keyword() ||
1554 self.token_is_closure_keyword() {
1555 // BARE FUNCTION OR CLOSURE
1556 self.parse_ty_bare_fn_or_ty_closure(Vec::new())
1557 } else if self.check(&token::BinOp(token::Or)) ||
1558 self.token == token::OrOr ||
1559 (self.token == token::Lt &&
1560 self.look_ahead(1, |t| {
1561 *t == token::Gt || t.is_lifetime()
1564 self.parse_ty_closure(Vec::new())
1565 } else if self.eat_keyword(keywords::Typeof) {
1567 // In order to not be ambiguous, the type must be surrounded by parens.
1568 self.expect(&token::OpenDelim(token::Paren));
1569 let e = self.parse_expr();
1570 self.expect(&token::CloseDelim(token::Paren));
1572 } else if self.eat_keyword(keywords::Proc) {
1573 self.parse_proc_type(Vec::new())
1574 } else if self.eat_lt() {
1575 // QUALIFIED PATH `<TYPE as TRAIT_REF>::item`
1576 let self_type = self.parse_ty_sum();
1577 self.expect_keyword(keywords::As);
1578 let trait_ref = self.parse_trait_ref();
1579 self.expect(&token::Gt);
1580 self.expect(&token::ModSep);
1581 let item_name = self.parse_ident();
1583 self_type: self_type,
1584 trait_ref: P(trait_ref),
1585 item_name: item_name,
1587 } else if self.check(&token::ModSep) ||
1588 self.token.is_ident() ||
1589 self.token.is_path() {
1591 self.parse_ty_path()
1592 } else if self.eat(&token::Underscore) {
1593 // TYPE TO BE INFERRED
1596 let this_token_str = self.this_token_to_string();
1597 let msg = format!("expected type, found `{}`", this_token_str);
1601 let sp = mk_sp(lo, self.last_span.hi);
1602 P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp})
1605 pub fn parse_borrowed_pointee(&mut self) -> Ty_ {
1606 // look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
1607 let opt_lifetime = self.parse_opt_lifetime();
1609 let mt = self.parse_mt();
1610 return TyRptr(opt_lifetime, mt);
1613 pub fn parse_ptr(&mut self) -> MutTy {
1614 let mutbl = if self.eat_keyword(keywords::Mut) {
1616 } else if self.eat_keyword(keywords::Const) {
1619 let span = self.last_span;
1621 "bare raw pointers are no longer allowed, you should \
1622 likely use `*mut T`, but otherwise `*T` is now \
1623 known as `*const T`");
1626 let t = self.parse_ty();
1627 MutTy { ty: t, mutbl: mutbl }
1630 pub fn is_named_argument(&mut self) -> bool {
1631 let offset = match self.token {
1632 token::BinOp(token::And) => 1,
1634 _ if self.token.is_keyword(keywords::Mut) => 1,
1638 debug!("parser is_named_argument offset:{}", offset);
1641 is_plain_ident_or_underscore(&self.token)
1642 && self.look_ahead(1, |t| *t == token::Colon)
1644 self.look_ahead(offset, |t| is_plain_ident_or_underscore(t))
1645 && self.look_ahead(offset + 1, |t| *t == token::Colon)
1649 /// This version of parse arg doesn't necessarily require
1650 /// identifier names.
1651 pub fn parse_arg_general(&mut self, require_name: bool) -> Arg {
1652 let pat = if require_name || self.is_named_argument() {
1653 debug!("parse_arg_general parse_pat (require_name:{})",
1655 let pat = self.parse_pat();
1657 self.expect(&token::Colon);
1660 debug!("parse_arg_general ident_to_pat");
1661 ast_util::ident_to_pat(ast::DUMMY_NODE_ID,
1663 special_idents::invalid)
1666 let t = self.parse_ty_sum();
1671 id: ast::DUMMY_NODE_ID,
1675 /// Parse a single function argument
1676 pub fn parse_arg(&mut self) -> Arg {
1677 self.parse_arg_general(true)
1680 /// Parse an argument in a lambda header e.g. |arg, arg|
1681 pub fn parse_fn_block_arg(&mut self) -> Arg {
1682 let pat = self.parse_pat();
1683 let t = if self.eat(&token::Colon) {
1687 id: ast::DUMMY_NODE_ID,
1689 span: mk_sp(self.span.lo, self.span.hi),
1695 id: ast::DUMMY_NODE_ID
1699 pub fn maybe_parse_fixed_length_of_vec(&mut self) -> Option<P<ast::Expr>> {
1700 if self.check(&token::Semi) {
1702 Some(self.parse_expr())
1708 /// Matches token_lit = LIT_INTEGER | ...
1709 pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ {
1711 token::Interpolated(token::NtExpr(ref v)) => {
1713 ExprLit(ref lit) => { lit.node.clone() }
1714 _ => { self.unexpected_last(tok); }
1717 token::Literal(lit, suf) => {
1718 let (suffix_illegal, out) = match lit {
1719 token::Byte(i) => (true, LitByte(parse::byte_lit(i.as_str()).0)),
1720 token::Char(i) => (true, LitChar(parse::char_lit(i.as_str()).0)),
1722 // there are some valid suffixes for integer and
1723 // float literals, so all the handling is done
1725 token::Integer(s) => {
1726 (false, parse::integer_lit(s.as_str(),
1727 suf.as_ref().map(|s| s.as_str()),
1728 &self.sess.span_diagnostic,
1731 token::Float(s) => {
1732 (false, parse::float_lit(s.as_str(),
1733 suf.as_ref().map(|s| s.as_str()),
1734 &self.sess.span_diagnostic,
1740 LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str())[]),
1743 token::StrRaw(s, n) => {
1746 token::intern_and_get_ident(
1747 parse::raw_str_lit(s.as_str())[]),
1751 (true, LitBinary(parse::binary_lit(i.as_str()))),
1752 token::BinaryRaw(i, _) =>
1754 LitBinary(Rc::new(i.as_str().as_bytes().iter().map(|&x| x).collect()))),
1758 let sp = self.last_span;
1759 self.expect_no_suffix(sp, &*format!("{} literal", lit.short_name()), suf)
1764 _ => { self.unexpected_last(tok); }
1768 /// Matches lit = true | false | token_lit
1769 pub fn parse_lit(&mut self) -> Lit {
1770 let lo = self.span.lo;
1771 let lit = if self.eat_keyword(keywords::True) {
1773 } else if self.eat_keyword(keywords::False) {
1776 let token = self.bump_and_get();
1777 let lit = self.lit_from_token(&token);
1780 codemap::Spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
1783 /// matches '-' lit | lit
1784 pub fn parse_literal_maybe_minus(&mut self) -> P<Expr> {
1785 let minus_lo = self.span.lo;
1786 let minus_present = self.eat(&token::BinOp(token::Minus));
1788 let lo = self.span.lo;
1789 let literal = P(self.parse_lit());
1790 let hi = self.span.hi;
1791 let expr = self.mk_expr(lo, hi, ExprLit(literal));
1794 let minus_hi = self.span.hi;
1795 let unary = self.mk_unary(UnNeg, expr);
1796 self.mk_expr(minus_lo, minus_hi, unary)
1802 /// Parses a path and optional type parameter bounds, depending on the
1803 /// mode. The `mode` parameter determines whether lifetimes, types, and/or
1804 /// bounds are permitted and whether `::` must precede type parameter
1806 pub fn parse_path(&mut self, mode: PathParsingMode) -> ast::Path {
1807 // Check for a whole path...
1808 let found = match self.token {
1809 token::Interpolated(token::NtPath(_)) => Some(self.bump_and_get()),
1812 if let Some(token::Interpolated(token::NtPath(box path))) = found {
1816 let lo = self.span.lo;
1817 let is_global = self.eat(&token::ModSep);
1819 // Parse any number of segments and bound sets. A segment is an
1820 // identifier followed by an optional lifetime and a set of types.
1821 // A bound set is a set of type parameter bounds.
1822 let segments = match mode {
1823 LifetimeAndTypesWithoutColons => {
1824 self.parse_path_segments_without_colons()
1826 LifetimeAndTypesWithColons => {
1827 self.parse_path_segments_with_colons()
1830 self.parse_path_segments_without_types()
1834 // Assemble the span.
1835 let span = mk_sp(lo, self.last_span.hi);
1837 // Assemble the result.
1846 /// - `a::b<T,U>::c<V,W>`
1847 /// - `a::b<T,U>::c(V) -> W`
1848 /// - `a::b<T,U>::c(V)`
1849 pub fn parse_path_segments_without_colons(&mut self) -> Vec<ast::PathSegment> {
1850 let mut segments = Vec::new();
1852 // First, parse an identifier.
1853 let identifier = self.parse_ident();
1855 // Parse types, optionally.
1856 let parameters = if self.eat_lt() {
1857 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1859 ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1860 lifetimes: lifetimes,
1861 types: OwnedSlice::from_vec(types),
1862 bindings: OwnedSlice::from_vec(bindings),
1864 } else if self.eat(&token::OpenDelim(token::Paren)) {
1865 let inputs = self.parse_seq_to_end(
1866 &token::CloseDelim(token::Paren),
1867 seq_sep_trailing_allowed(token::Comma),
1868 |p| p.parse_ty_sum());
1870 let output_ty = if self.eat(&token::RArrow) {
1871 Some(self.parse_ty())
1876 ast::ParenthesizedParameters(ast::ParenthesizedParameterData {
1881 ast::PathParameters::none()
1884 // Assemble and push the result.
1885 segments.push(ast::PathSegment { identifier: identifier,
1886 parameters: parameters });
1888 // Continue only if we see a `::`
1889 if !self.eat(&token::ModSep) {
1896 /// - `a::b::<T,U>::c`
1897 pub fn parse_path_segments_with_colons(&mut self) -> Vec<ast::PathSegment> {
1898 let mut segments = Vec::new();
1900 // First, parse an identifier.
1901 let identifier = self.parse_ident();
1903 // If we do not see a `::`, stop.
1904 if !self.eat(&token::ModSep) {
1905 segments.push(ast::PathSegment {
1906 identifier: identifier,
1907 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1908 lifetimes: Vec::new(),
1909 types: OwnedSlice::empty(),
1910 bindings: OwnedSlice::empty(),
1916 // Check for a type segment.
1918 // Consumed `a::b::<`, go look for types
1919 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt();
1920 segments.push(ast::PathSegment {
1921 identifier: identifier,
1922 parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
1923 lifetimes: lifetimes,
1924 types: OwnedSlice::from_vec(types),
1925 bindings: OwnedSlice::from_vec(bindings),
1929 // Consumed `a::b::<T,U>`, check for `::` before proceeding
1930 if !self.eat(&token::ModSep) {
1934 // Consumed `a::`, go look for `b`
1935 segments.push(ast::PathSegment {
1936 identifier: identifier,
1937 parameters: ast::PathParameters::none(),
1946 pub fn parse_path_segments_without_types(&mut self) -> Vec<ast::PathSegment> {
1947 let mut segments = Vec::new();
1949 // First, parse an identifier.
1950 let identifier = self.parse_ident();
1952 // Assemble and push the result.
1953 segments.push(ast::PathSegment {
1954 identifier: identifier,
1955 parameters: ast::PathParameters::none()
1958 // If we do not see a `::`, stop.
1959 if !self.eat(&token::ModSep) {
1965 /// parses 0 or 1 lifetime
1966 pub fn parse_opt_lifetime(&mut self) -> Option<ast::Lifetime> {
1968 token::Lifetime(..) => {
1969 Some(self.parse_lifetime())
1977 /// Parses a single lifetime
1978 /// Matches lifetime = LIFETIME
1979 pub fn parse_lifetime(&mut self) -> ast::Lifetime {
1981 token::Lifetime(i) => {
1982 let span = self.span;
1984 return ast::Lifetime {
1985 id: ast::DUMMY_NODE_ID,
1991 self.fatal(format!("expected a lifetime name")[]);
1996 /// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
1997 /// lifetime [':' lifetimes]`
1998 pub fn parse_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
2000 let mut res = Vec::new();
2003 token::Lifetime(_) => {
2004 let lifetime = self.parse_lifetime();
2006 if self.eat(&token::Colon) {
2007 self.parse_lifetimes(token::BinOp(token::Plus))
2011 res.push(ast::LifetimeDef { lifetime: lifetime,
2021 token::Comma => { self.bump(); }
2022 token::Gt => { return res; }
2023 token::BinOp(token::Shr) => { return res; }
2025 let this_token_str = self.this_token_to_string();
2026 let msg = format!("expected `,` or `>` after lifetime \
2035 /// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
2036 /// one too, but putting that in there messes up the grammar....
2038 /// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
2039 /// either a comma or `>`. Used when parsing type parameter lists, where we expect something
2040 /// like `<'a, 'b, T>`.
2041 pub fn parse_lifetimes(&mut self, sep: token::Token) -> Vec<ast::Lifetime> {
2043 let mut res = Vec::new();
2046 token::Lifetime(_) => {
2047 res.push(self.parse_lifetime());
2054 if self.token != sep {
2062 /// Parse mutability declaration (mut/const/imm)
2063 pub fn parse_mutability(&mut self) -> Mutability {
2064 if self.eat_keyword(keywords::Mut) {
2071 /// Parse ident COLON expr
2072 pub fn parse_field(&mut self) -> Field {
2073 let lo = self.span.lo;
2074 let i = self.parse_ident();
2075 let hi = self.last_span.hi;
2076 self.expect(&token::Colon);
2077 let e = self.parse_expr();
2079 ident: spanned(lo, hi, i),
2080 span: mk_sp(lo, e.span.hi),
2085 pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr> {
2087 id: ast::DUMMY_NODE_ID,
2089 span: mk_sp(lo, hi),
2093 pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::Expr_ {
2094 ExprUnary(unop, expr)
2097 pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2098 ExprBinary(binop, lhs, rhs)
2101 pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::Expr_ {
2105 fn mk_method_call(&mut self,
2106 ident: ast::SpannedIdent,
2110 ExprMethodCall(ident, tps, args)
2113 pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::Expr_ {
2114 ExprIndex(expr, idx)
2117 pub fn mk_slice(&mut self,
2119 start: Option<P<Expr>>,
2120 end: Option<P<Expr>>,
2123 // FIXME: we could give more accurate span info here.
2124 let (lo, hi) = match (&start, &end) {
2125 (&Some(ref s), &Some(ref e)) => (s.span.lo, e.span.hi),
2126 (&Some(ref s), &None) => (s.span.lo, s.span.hi),
2127 (&None, &Some(ref e)) => (e.span.lo, e.span.hi),
2128 (&None, &None) => (DUMMY_SP.lo, DUMMY_SP.hi),
2130 ExprIndex(expr, self.mk_expr(lo, hi, ExprRange(start, end)))
2133 pub fn mk_range(&mut self,
2134 start: Option<P<Expr>>,
2135 end: Option<P<Expr>>)
2137 ExprRange(start, end)
2140 pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::Expr_ {
2141 ExprField(expr, ident)
2144 pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<uint>) -> ast::Expr_ {
2145 ExprTupField(expr, idx)
2148 pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2149 lhs: P<Expr>, rhs: P<Expr>) -> ast::Expr_ {
2150 ExprAssignOp(binop, lhs, rhs)
2153 pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr> {
2155 id: ast::DUMMY_NODE_ID,
2156 node: ExprMac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
2157 span: mk_sp(lo, hi),
2161 pub fn mk_lit_u32(&mut self, i: u32) -> P<Expr> {
2162 let span = &self.span;
2163 let lv_lit = P(codemap::Spanned {
2164 node: LitInt(i as u64, ast::UnsignedIntLit(TyU32)),
2169 id: ast::DUMMY_NODE_ID,
2170 node: ExprLit(lv_lit),
2175 fn expect_open_delim(&mut self) -> token::DelimToken {
2177 token::OpenDelim(delim) => {
2181 _ => self.fatal("expected open delimiter"),
2185 /// At the bottom (top?) of the precedence hierarchy,
2186 /// parse things like parenthesized exprs,
2187 /// macros, return, etc.
2188 pub fn parse_bottom_expr(&mut self) -> P<Expr> {
2189 maybe_whole_expr!(self);
2191 let lo = self.span.lo;
2192 let mut hi = self.span.hi;
2197 token::OpenDelim(token::Paren) => {
2200 // (e) is parenthesized e
2201 // (e,) is a tuple with only one field, e
2202 let mut es = vec![];
2203 let mut trailing_comma = false;
2204 while self.token != token::CloseDelim(token::Paren) {
2205 es.push(self.parse_expr());
2206 self.commit_expr(&**es.last().unwrap(), &[],
2207 &[token::Comma, token::CloseDelim(token::Paren)]);
2208 if self.check(&token::Comma) {
2209 trailing_comma = true;
2213 trailing_comma = false;
2220 return if es.len() == 1 && !trailing_comma {
2221 self.mk_expr(lo, hi, ExprParen(es.into_iter().nth(0).unwrap()))
2223 self.mk_expr(lo, hi, ExprTup(es))
2226 token::OpenDelim(token::Brace) => {
2228 let blk = self.parse_block_tail(lo, DefaultBlock);
2229 return self.mk_expr(blk.span.lo, blk.span.hi,
2232 token::BinOp(token::Or) | token::OrOr => {
2233 return self.parse_lambda_expr(CaptureByRef);
2235 // FIXME #13626: Should be able to stick in
2236 // token::SELF_KEYWORD_NAME
2237 token::Ident(id @ ast::Ident {
2238 name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
2240 }, token::Plain) => {
2242 let path = ast_util::ident_to_path(mk_sp(lo, hi), id);
2243 ex = ExprPath(path);
2244 hi = self.last_span.hi;
2246 token::OpenDelim(token::Bracket) => {
2249 if self.check(&token::CloseDelim(token::Bracket)) {
2252 ex = ExprVec(Vec::new());
2255 let first_expr = self.parse_expr();
2256 if self.check(&token::Semi) {
2257 // Repeating vector syntax: [ 0; 512 ]
2259 let count = self.parse_expr();
2260 self.expect(&token::CloseDelim(token::Bracket));
2261 ex = ExprRepeat(first_expr, count);
2262 } else if self.check(&token::Comma) {
2263 // Vector with two or more elements.
2265 let remaining_exprs = self.parse_seq_to_end(
2266 &token::CloseDelim(token::Bracket),
2267 seq_sep_trailing_allowed(token::Comma),
2270 let mut exprs = vec!(first_expr);
2271 exprs.extend(remaining_exprs.into_iter());
2272 ex = ExprVec(exprs);
2274 // Vector with one element.
2275 self.expect(&token::CloseDelim(token::Bracket));
2276 ex = ExprVec(vec!(first_expr));
2279 hi = self.last_span.hi;
2282 if self.eat_keyword(keywords::Move) {
2283 return self.parse_lambda_expr(CaptureByValue);
2285 if self.eat_keyword(keywords::Proc) {
2286 let span = self.last_span;
2287 let _ = self.parse_proc_decl();
2288 let _ = self.parse_expr();
2289 return self.obsolete_expr(span, ObsoleteProcExpr);
2291 if self.eat_keyword(keywords::If) {
2292 return self.parse_if_expr();
2294 if self.eat_keyword(keywords::For) {
2295 return self.parse_for_expr(None);
2297 if self.eat_keyword(keywords::While) {
2298 return self.parse_while_expr(None);
2300 if self.token.is_lifetime() {
2301 let lifetime = self.get_lifetime();
2303 self.expect(&token::Colon);
2304 if self.eat_keyword(keywords::While) {
2305 return self.parse_while_expr(Some(lifetime))
2307 if self.eat_keyword(keywords::For) {
2308 return self.parse_for_expr(Some(lifetime))
2310 if self.eat_keyword(keywords::Loop) {
2311 return self.parse_loop_expr(Some(lifetime))
2313 self.fatal("expected `while`, `for`, or `loop` after a label")
2315 if self.eat_keyword(keywords::Loop) {
2316 return self.parse_loop_expr(None);
2318 if self.eat_keyword(keywords::Continue) {
2319 let lo = self.span.lo;
2320 let ex = if self.token.is_lifetime() {
2321 let lifetime = self.get_lifetime();
2323 ExprAgain(Some(lifetime))
2327 let hi = self.span.hi;
2328 return self.mk_expr(lo, hi, ex);
2330 if self.eat_keyword(keywords::Match) {
2331 return self.parse_match_expr();
2333 if self.eat_keyword(keywords::Unsafe) {
2334 return self.parse_block_expr(
2336 UnsafeBlock(ast::UserProvided));
2338 if self.eat_keyword(keywords::Return) {
2339 // RETURN expression
2340 if self.token.can_begin_expr() {
2341 let e = self.parse_expr();
2343 ex = ExprRet(Some(e));
2347 } else if self.eat_keyword(keywords::Break) {
2349 if self.token.is_lifetime() {
2350 let lifetime = self.get_lifetime();
2352 ex = ExprBreak(Some(lifetime));
2354 ex = ExprBreak(None);
2357 } else if self.check(&token::ModSep) ||
2358 self.token.is_ident() &&
2359 !self.token.is_keyword(keywords::True) &&
2360 !self.token.is_keyword(keywords::False) {
2362 self.parse_path(LifetimeAndTypesWithColons);
2364 // `!`, as an operator, is prefix, so we know this isn't that
2365 if self.check(&token::Not) {
2366 // MACRO INVOCATION expression
2369 let delim = self.expect_open_delim();
2370 let tts = self.parse_seq_to_end(
2371 &token::CloseDelim(delim),
2373 |p| p.parse_token_tree());
2374 let hi = self.span.hi;
2376 return self.mk_mac_expr(lo,
2382 if self.check(&token::OpenDelim(token::Brace)) {
2383 // This is a struct literal, unless we're prohibited
2384 // from parsing struct literals here.
2385 if !self.restrictions.contains(RESTRICTION_NO_STRUCT_LITERAL) {
2386 // It's a struct literal.
2388 let mut fields = Vec::new();
2389 let mut base = None;
2391 while self.token != token::CloseDelim(token::Brace) {
2392 if self.eat(&token::DotDot) {
2393 base = Some(self.parse_expr());
2397 fields.push(self.parse_field());
2398 self.commit_expr(&*fields.last().unwrap().expr,
2400 &[token::CloseDelim(token::Brace)]);
2403 if fields.len() == 0 && base.is_none() {
2404 let last_span = self.last_span;
2405 self.span_err(last_span,
2406 "structure literal must either \
2407 have at least one field or use \
2408 functional structure update \
2413 self.expect(&token::CloseDelim(token::Brace));
2414 ex = ExprStruct(pth, fields, base);
2415 return self.mk_expr(lo, hi, ex);
2422 // other literal expression
2423 let lit = self.parse_lit();
2425 ex = ExprLit(P(lit));
2430 return self.mk_expr(lo, hi, ex);
2433 /// Parse a block or unsafe block
2434 pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode)
2436 self.expect(&token::OpenDelim(token::Brace));
2437 let blk = self.parse_block_tail(lo, blk_mode);
2438 return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
2441 /// parse a.b or a(13) or a[4] or just a
2442 pub fn parse_dot_or_call_expr(&mut self) -> P<Expr> {
2443 let b = self.parse_bottom_expr();
2444 self.parse_dot_or_call_expr_with(b)
2447 pub fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> P<Expr> {
2453 if self.eat(&token::Dot) {
2455 token::Ident(i, _) => {
2456 let dot = self.last_span.hi;
2459 let (_, tys, bindings) = if self.eat(&token::ModSep) {
2461 self.parse_generic_values_after_lt()
2463 (Vec::new(), Vec::new(), Vec::new())
2466 if bindings.len() > 0 {
2467 let last_span = self.last_span;
2468 self.span_err(last_span, "type bindings are only permitted on trait paths");
2471 // expr.f() method call
2473 token::OpenDelim(token::Paren) => {
2474 let mut es = self.parse_unspanned_seq(
2475 &token::OpenDelim(token::Paren),
2476 &token::CloseDelim(token::Paren),
2477 seq_sep_trailing_allowed(token::Comma),
2480 hi = self.last_span.hi;
2483 let id = spanned(dot, hi, i);
2484 let nd = self.mk_method_call(id, tys, es);
2485 e = self.mk_expr(lo, hi, nd);
2488 if !tys.is_empty() {
2489 let last_span = self.last_span;
2490 self.span_err(last_span,
2491 "field expressions may not \
2492 have type parameters");
2495 let id = spanned(dot, hi, i);
2496 let field = self.mk_field(e, id);
2497 e = self.mk_expr(lo, hi, field);
2501 token::Literal(token::Integer(n), suf) => {
2504 // A tuple index may not have a suffix
2505 self.expect_no_suffix(sp, "tuple index", suf);
2507 let dot = self.last_span.hi;
2511 let index = n.as_str().parse::<uint>();
2514 let id = spanned(dot, hi, n);
2515 let field = self.mk_tup_field(e, id);
2516 e = self.mk_expr(lo, hi, field);
2519 let last_span = self.last_span;
2520 self.span_err(last_span, "invalid tuple or tuple struct index");
2524 token::Literal(token::Float(n), _suf) => {
2526 let last_span = self.last_span;
2527 let fstr = n.as_str();
2528 self.span_err(last_span,
2529 format!("unexpected token: `{}`", n.as_str())[]);
2530 if fstr.chars().all(|x| "0123456789.".contains_char(x)) {
2531 let float = match fstr.parse::<f64>() {
2535 self.span_help(last_span,
2536 format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
2537 float.trunc() as uint,
2538 float.fract().to_string()[1..])[]);
2540 self.abort_if_errors();
2543 _ => self.unexpected()
2547 if self.expr_is_complete(&*e) { break; }
2550 token::OpenDelim(token::Paren) => {
2551 let es = self.parse_unspanned_seq(
2552 &token::OpenDelim(token::Paren),
2553 &token::CloseDelim(token::Paren),
2554 seq_sep_trailing_allowed(token::Comma),
2557 hi = self.last_span.hi;
2559 let nd = self.mk_call(e, es);
2560 e = self.mk_expr(lo, hi, nd);
2564 // Could be either an index expression or a slicing expression.
2565 // Any slicing non-terminal can have a mutable version with `mut`
2566 // after the opening square bracket.
2567 token::OpenDelim(token::Bracket) => {
2569 let mutbl = if self.eat_keyword(keywords::Mut) {
2576 token::CloseDelim(token::Bracket) => {
2579 let slice = self.mk_slice(e, None, None, mutbl);
2580 e = self.mk_expr(lo, hi, slice)
2587 token::CloseDelim(token::Bracket) => {
2590 let slice = self.mk_slice(e, None, None, mutbl);
2591 e = self.mk_expr(lo, hi, slice);
2593 self.span_err(e.span, "incorrect slicing expression: `[..]`");
2594 self.span_note(e.span,
2595 "use `expr[]` to construct a slice of the whole of expr");
2600 let e2 = self.parse_expr();
2601 self.commit_expr_expecting(&*e2, token::CloseDelim(token::Bracket));
2602 let slice = self.mk_slice(e, None, Some(e2), mutbl);
2603 e = self.mk_expr(lo, hi, slice)
2607 // e[e] | e[e..] | e[e..e]
2609 let ix = self.parse_expr_res(RESTRICTION_NO_DOTS);
2614 let e2 = match self.token {
2616 token::CloseDelim(token::Bracket) => {
2622 let e2 = self.parse_expr_res(RESTRICTION_NO_DOTS);
2623 self.commit_expr_expecting(&*e2,
2624 token::CloseDelim(token::Bracket));
2629 let slice = self.mk_slice(e, Some(ix), e2, mutbl);
2630 e = self.mk_expr(lo, hi, slice)
2634 if mutbl == ast::MutMutable {
2635 self.span_err(e.span,
2636 "`mut` keyword is invalid in index expressions");
2639 self.commit_expr_expecting(&*ix, token::CloseDelim(token::Bracket));
2640 let index = self.mk_index(e, ix);
2641 e = self.mk_expr(lo, hi, index)
2648 // A range expression, either `expr..expr` or `expr..`.
2649 token::DotDot if !self.restrictions.contains(RESTRICTION_NO_DOTS) => {
2652 let opt_end = if self.token.can_begin_expr() {
2653 let end = self.parse_expr_res(RESTRICTION_NO_DOTS);
2659 let hi = self.span.hi;
2660 let range = self.mk_range(Some(e), opt_end);
2661 return self.mk_expr(lo, hi, range);
2669 /// Parse an optional separator followed by a Kleene-style
2670 /// repetition token (+ or *).
2671 pub fn parse_sep_and_kleene_op(&mut self) -> (Option<token::Token>, ast::KleeneOp) {
2672 fn parse_kleene_op(parser: &mut Parser) -> Option<ast::KleeneOp> {
2673 match parser.token {
2674 token::BinOp(token::Star) => {
2676 Some(ast::ZeroOrMore)
2678 token::BinOp(token::Plus) => {
2680 Some(ast::OneOrMore)
2686 match parse_kleene_op(self) {
2687 Some(kleene_op) => return (None, kleene_op),
2691 let separator = self.bump_and_get();
2692 match parse_kleene_op(self) {
2693 Some(zerok) => (Some(separator), zerok),
2694 None => self.fatal("expected `*` or `+`")
2698 /// parse a single token tree from the input.
2699 pub fn parse_token_tree(&mut self) -> TokenTree {
2700 // FIXME #6994: currently, this is too eager. It
2701 // parses token trees but also identifies TtSequence's
2702 // and token::SubstNt's; it's too early to know yet
2703 // whether something will be a nonterminal or a seq
2705 maybe_whole!(deref self, NtTT);
2707 // this is the fall-through for the 'match' below.
2708 // invariants: the current token is not a left-delimiter,
2709 // not an EOF, and not the desired right-delimiter (if
2710 // it were, parse_seq_to_before_end would have prevented
2711 // reaching this point.
2712 fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree {
2713 maybe_whole!(deref p, NtTT);
2715 token::CloseDelim(_) => {
2716 // This is a conservative error: only report the last unclosed delimiter. The
2717 // previous unclosed delimiters could actually be closed! The parser just hasn't
2718 // gotten to them yet.
2719 match p.open_braces.last() {
2721 Some(&sp) => p.span_note(sp, "unclosed delimiter"),
2723 let token_str = p.this_token_to_string();
2724 p.fatal(format!("incorrect close delimiter: `{}`",
2727 /* we ought to allow different depths of unquotation */
2728 token::Dollar if p.quote_depth > 0u => {
2732 if p.token == token::OpenDelim(token::Paren) {
2733 let seq = p.parse_seq(
2734 &token::OpenDelim(token::Paren),
2735 &token::CloseDelim(token::Paren),
2737 |p| p.parse_token_tree()
2739 let (sep, repeat) = p.parse_sep_and_kleene_op();
2740 let seq = match seq {
2741 Spanned { node, .. } => node,
2743 let name_num = macro_parser::count_names(seq[]);
2744 TtSequence(mk_sp(sp.lo, p.span.hi),
2745 Rc::new(SequenceRepetition {
2749 num_captures: name_num
2752 // A nonterminal that matches or not
2753 let namep = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2754 let name = p.parse_ident();
2755 if p.token == token::Colon && p.look_ahead(1, |t| t.is_ident()) {
2757 let kindp = match p.token { token::Ident(_, p) => p, _ => token::Plain };
2758 let nt_kind = p.parse_ident();
2759 let m = TtToken(sp, MatchNt(name, nt_kind, namep, kindp));
2762 TtToken(sp, SubstNt(name, namep))
2767 TtToken(p.span, p.bump_and_get())
2774 let open_braces = self.open_braces.clone();
2775 for sp in open_braces.iter() {
2776 self.span_help(*sp, "did you mean to close this delimiter?");
2778 // There shouldn't really be a span, but it's easier for the test runner
2779 // if we give it one
2780 self.fatal("this file contains an un-closed delimiter ");
2782 token::OpenDelim(delim) => {
2783 // The span for beginning of the delimited section
2784 let pre_span = self.span;
2786 // Parse the open delimiter.
2787 self.open_braces.push(self.span);
2788 let open_span = self.span;
2791 // Parse the token trees within the delimeters
2792 let tts = self.parse_seq_to_before_end(
2793 &token::CloseDelim(delim),
2795 |p| p.parse_token_tree()
2798 // Parse the close delimiter.
2799 let close_span = self.span;
2801 self.open_braces.pop().unwrap();
2803 // Expand to cover the entire delimited token tree
2804 let span = Span { hi: self.span.hi, ..pre_span };
2806 TtDelimited(span, Rc::new(Delimited {
2808 open_span: open_span,
2810 close_span: close_span,
2813 _ => parse_non_delim_tt_tok(self),
2817 // parse a stream of tokens into a list of TokenTree's,
2819 pub fn parse_all_token_trees(&mut self) -> Vec<TokenTree> {
2820 let mut tts = Vec::new();
2821 while self.token != token::Eof {
2822 tts.push(self.parse_token_tree());
2827 /// Parse a prefix-operator expr
2828 pub fn parse_prefix_expr(&mut self) -> P<Expr> {
2829 let lo = self.span.lo;
2836 let e = self.parse_prefix_expr();
2838 ex = self.mk_unary(UnNot, e);
2840 token::BinOp(token::Minus) => {
2842 let e = self.parse_prefix_expr();
2844 ex = self.mk_unary(UnNeg, e);
2846 token::BinOp(token::Star) => {
2848 let e = self.parse_prefix_expr();
2850 ex = self.mk_unary(UnDeref, e);
2852 token::BinOp(token::And) | token::AndAnd => {
2854 let m = self.parse_mutability();
2855 let e = self.parse_prefix_expr();
2857 ex = ExprAddrOf(m, e);
2861 let last_span = self.last_span;
2863 token::OpenDelim(token::Bracket) => {
2864 self.obsolete(last_span, ObsoleteOwnedVector)
2866 _ => self.obsolete(last_span, ObsoleteOwnedExpr)
2869 let e = self.parse_prefix_expr();
2871 ex = self.mk_unary(UnUniq, e);
2873 token::DotDot if !self.restrictions.contains(RESTRICTION_NO_DOTS) => {
2874 // A range, closed above: `..expr`.
2876 let e = self.parse_prefix_expr();
2878 ex = self.mk_range(None, Some(e));
2880 token::Ident(_, _) => {
2881 if !self.token.is_keyword(keywords::Box) {
2882 return self.parse_dot_or_call_expr();
2885 let lo = self.span.lo;
2889 // Check for a place: `box(PLACE) EXPR`.
2890 if self.eat(&token::OpenDelim(token::Paren)) {
2891 // Support `box() EXPR` as the default.
2892 if !self.eat(&token::CloseDelim(token::Paren)) {
2893 let place = self.parse_expr();
2894 self.expect(&token::CloseDelim(token::Paren));
2895 // Give a suggestion to use `box()` when a parenthesised expression is used
2896 if !self.token.can_begin_expr() {
2897 let span = self.span;
2898 let this_token_to_string = self.this_token_to_string();
2900 format!("expected expression, found `{}`",
2901 this_token_to_string)[]);
2902 let box_span = mk_sp(lo, self.last_span.hi);
2903 self.span_help(box_span,
2904 "perhaps you meant `box() (foo)` instead?");
2905 self.abort_if_errors();
2907 let subexpression = self.parse_prefix_expr();
2908 hi = subexpression.span.hi;
2909 ex = ExprBox(Some(place), subexpression);
2910 return self.mk_expr(lo, hi, ex);
2914 // Otherwise, we use the unique pointer default.
2915 let subexpression = self.parse_prefix_expr();
2916 hi = subexpression.span.hi;
2917 // FIXME (pnkfelix): After working out kinks with box
2918 // desugaring, should be `ExprBox(None, subexpression)`
2920 ex = self.mk_unary(UnUniq, subexpression);
2922 _ => return self.parse_dot_or_call_expr()
2924 return self.mk_expr(lo, hi, ex);
2927 /// Parse an expression of binops
2928 pub fn parse_binops(&mut self) -> P<Expr> {
2929 let prefix_expr = self.parse_prefix_expr();
2930 self.parse_more_binops(prefix_expr, 0)
2933 /// Parse an expression of binops of at least min_prec precedence
2934 pub fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: uint) -> P<Expr> {
2935 if self.expr_is_complete(&*lhs) { return lhs; }
2937 // Prevent dynamic borrow errors later on by limiting the
2938 // scope of the borrows.
2939 if self.token == token::BinOp(token::Or) &&
2940 self.restrictions.contains(RESTRICTION_NO_BAR_OP) {
2943 self.expected_tokens.push(TokenType::Operator);
2945 let cur_opt = self.token.to_binop();
2948 let cur_prec = operator_prec(cur_op);
2949 if cur_prec > min_prec {
2951 let expr = self.parse_prefix_expr();
2952 let rhs = self.parse_more_binops(expr, cur_prec);
2953 let lhs_span = lhs.span;
2954 let rhs_span = rhs.span;
2955 let binary = self.mk_binary(cur_op, lhs, rhs);
2956 let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary);
2957 self.parse_more_binops(bin, min_prec)
2963 if as_prec > min_prec && self.eat_keyword(keywords::As) {
2964 let rhs = self.parse_ty();
2965 let _as = self.mk_expr(lhs.span.lo,
2967 ExprCast(lhs, rhs));
2968 self.parse_more_binops(_as, min_prec)
2976 /// Parse an assignment expression....
2977 /// actually, this seems to be the main entry point for
2978 /// parsing an arbitrary expression.
2979 pub fn parse_assign_expr(&mut self) -> P<Expr> {
2980 let lo = self.span.lo;
2981 let lhs = self.parse_binops();
2982 let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL;
2986 let rhs = self.parse_expr_res(restrictions);
2987 self.mk_expr(lo, rhs.span.hi, ExprAssign(lhs, rhs))
2989 token::BinOpEq(op) => {
2991 let rhs = self.parse_expr_res(restrictions);
2992 let aop = match op {
2993 token::Plus => BiAdd,
2994 token::Minus => BiSub,
2995 token::Star => BiMul,
2996 token::Slash => BiDiv,
2997 token::Percent => BiRem,
2998 token::Caret => BiBitXor,
2999 token::And => BiBitAnd,
3000 token::Or => BiBitOr,
3001 token::Shl => BiShl,
3004 let rhs_span = rhs.span;
3005 let assign_op = self.mk_assign_op(aop, lhs, rhs);
3006 self.mk_expr(lo, rhs_span.hi, assign_op)
3014 /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3015 pub fn parse_if_expr(&mut self) -> P<Expr> {
3016 if self.token.is_keyword(keywords::Let) {
3017 return self.parse_if_let_expr();
3019 let lo = self.last_span.lo;
3020 let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3021 let thn = self.parse_block();
3022 let mut els: Option<P<Expr>> = None;
3023 let mut hi = thn.span.hi;
3024 if self.eat_keyword(keywords::Else) {
3025 let elexpr = self.parse_else_expr();
3026 hi = elexpr.span.hi;
3029 self.mk_expr(lo, hi, ExprIf(cond, thn, els))
3032 /// Parse an 'if let' expression ('if' token already eaten)
3033 pub fn parse_if_let_expr(&mut self) -> P<Expr> {
3034 let lo = self.last_span.lo;
3035 self.expect_keyword(keywords::Let);
3036 let pat = self.parse_pat();
3037 self.expect(&token::Eq);
3038 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3039 let thn = self.parse_block();
3040 let (hi, els) = if self.eat_keyword(keywords::Else) {
3041 let expr = self.parse_else_expr();
3042 (expr.span.hi, Some(expr))
3046 self.mk_expr(lo, hi, ExprIfLet(pat, expr, thn, els))
3050 pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
3053 let lo = self.span.lo;
3054 let (decl, optional_unboxed_closure_kind) =
3055 self.parse_fn_block_decl();
3056 let body = self.parse_expr();
3057 let fakeblock = P(ast::Block {
3058 id: ast::DUMMY_NODE_ID,
3059 view_items: Vec::new(),
3063 rules: DefaultBlock,
3069 ExprClosure(capture_clause, optional_unboxed_closure_kind, decl, fakeblock))
3072 pub fn parse_else_expr(&mut self) -> P<Expr> {
3073 if self.eat_keyword(keywords::If) {
3074 return self.parse_if_expr();
3076 let blk = self.parse_block();
3077 return self.mk_expr(blk.span.lo, blk.span.hi, ExprBlock(blk));
3081 /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3082 pub fn parse_for_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3083 // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3085 let lo = self.last_span.lo;
3086 let pat = self.parse_pat();
3087 self.expect_keyword(keywords::In);
3088 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3089 let loop_block = self.parse_block();
3090 let hi = self.span.hi;
3092 self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident))
3095 /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3096 pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3097 if self.token.is_keyword(keywords::Let) {
3098 return self.parse_while_let_expr(opt_ident);
3100 let lo = self.last_span.lo;
3101 let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3102 let body = self.parse_block();
3103 let hi = body.span.hi;
3104 return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
3107 /// Parse a 'while let' expression ('while' token already eaten)
3108 pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3109 let lo = self.last_span.lo;
3110 self.expect_keyword(keywords::Let);
3111 let pat = self.parse_pat();
3112 self.expect(&token::Eq);
3113 let expr = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3114 let body = self.parse_block();
3115 let hi = body.span.hi;
3116 return self.mk_expr(lo, hi, ExprWhileLet(pat, expr, body, opt_ident));
3119 pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
3120 let lo = self.last_span.lo;
3121 let body = self.parse_block();
3122 let hi = body.span.hi;
3123 self.mk_expr(lo, hi, ExprLoop(body, opt_ident))
3126 fn parse_match_expr(&mut self) -> P<Expr> {
3127 let lo = self.last_span.lo;
3128 let discriminant = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL);
3129 self.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace));
3130 let mut arms: Vec<Arm> = Vec::new();
3131 while self.token != token::CloseDelim(token::Brace) {
3132 arms.push(self.parse_arm());
3134 let hi = self.span.hi;
3136 return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchSource::Normal));
3139 pub fn parse_arm(&mut self) -> Arm {
3140 let attrs = self.parse_outer_attributes();
3141 let pats = self.parse_pats();
3142 let mut guard = None;
3143 if self.eat_keyword(keywords::If) {
3144 guard = Some(self.parse_expr());
3146 self.expect(&token::FatArrow);
3147 let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3150 !classify::expr_is_simple_block(&*expr)
3151 && self.token != token::CloseDelim(token::Brace);
3154 self.commit_expr(&*expr, &[token::Comma], &[token::CloseDelim(token::Brace)]);
3156 self.eat(&token::Comma);
3167 /// Parse an expression
3168 pub fn parse_expr(&mut self) -> P<Expr> {
3169 return self.parse_expr_res(UNRESTRICTED);
3172 /// Parse an expression, subject to the given restrictions
3173 pub fn parse_expr_res(&mut self, r: Restrictions) -> P<Expr> {
3174 let old = self.restrictions;
3175 self.restrictions = r;
3176 let e = self.parse_assign_expr();
3177 self.restrictions = old;
3181 /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3182 fn parse_initializer(&mut self) -> Option<P<Expr>> {
3183 if self.check(&token::Eq) {
3185 Some(self.parse_expr())
3191 /// Parse patterns, separated by '|' s
3192 fn parse_pats(&mut self) -> Vec<P<Pat>> {
3193 let mut pats = Vec::new();
3195 pats.push(self.parse_pat());
3196 if self.check(&token::BinOp(token::Or)) { self.bump(); }
3197 else { return pats; }
3201 fn parse_pat_vec_elements(
3203 ) -> (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>) {
3204 let mut before = Vec::new();
3205 let mut slice = None;
3206 let mut after = Vec::new();
3207 let mut first = true;
3208 let mut before_slice = true;
3210 while self.token != token::CloseDelim(token::Bracket) {
3214 self.expect(&token::Comma);
3216 if self.token == token::CloseDelim(token::Bracket)
3217 && (before_slice || after.len() != 0) {
3223 if self.check(&token::DotDot) {
3226 if self.check(&token::Comma) ||
3227 self.check(&token::CloseDelim(token::Bracket)) {
3228 slice = Some(P(ast::Pat {
3229 id: ast::DUMMY_NODE_ID,
3230 node: PatWild(PatWildMulti),
3233 before_slice = false;
3235 let _ = self.parse_pat();
3236 let span = self.span;
3237 self.obsolete(span, ObsoleteSubsliceMatch);
3243 let subpat = self.parse_pat();
3244 if before_slice && self.check(&token::DotDot) {
3246 slice = Some(subpat);
3247 before_slice = false;
3248 } else if before_slice {
3249 before.push(subpat);
3255 (before, slice, after)
3258 /// Parse the fields of a struct-like pattern
3259 fn parse_pat_fields(&mut self) -> (Vec<codemap::Spanned<ast::FieldPat>> , bool) {
3260 let mut fields = Vec::new();
3261 let mut etc = false;
3262 let mut first = true;
3263 while self.token != token::CloseDelim(token::Brace) {
3267 self.expect(&token::Comma);
3268 // accept trailing commas
3269 if self.check(&token::CloseDelim(token::Brace)) { break }
3272 let lo = self.span.lo;
3275 if self.check(&token::DotDot) {
3277 if self.token != token::CloseDelim(token::Brace) {
3278 let token_str = self.this_token_to_string();
3279 self.fatal(format!("expected `{}`, found `{}`", "}",
3286 let bind_type = if self.eat_keyword(keywords::Mut) {
3287 BindByValue(MutMutable)
3288 } else if self.eat_keyword(keywords::Ref) {
3289 BindByRef(self.parse_mutability())
3291 BindByValue(MutImmutable)
3294 let fieldname = self.parse_ident();
3296 let (subpat, is_shorthand) = if self.check(&token::Colon) {
3298 BindByRef(..) | BindByValue(MutMutable) => {
3299 let token_str = self.this_token_to_string();
3300 self.fatal(format!("unexpected `{}`",
3307 let pat = self.parse_pat();
3311 hi = self.last_span.hi;
3312 let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname};
3314 id: ast::DUMMY_NODE_ID,
3315 node: PatIdent(bind_type, fieldpath, None),
3316 span: self.last_span
3319 fields.push(codemap::Spanned { span: mk_sp(lo, hi),
3320 node: ast::FieldPat { ident: fieldname,
3322 is_shorthand: is_shorthand }});
3324 return (fields, etc);
3327 /// Parse a pattern.
3328 pub fn parse_pat(&mut self) -> P<Pat> {
3329 maybe_whole!(self, NtPat);
3331 let lo = self.span.lo;
3336 token::Underscore => {
3338 pat = PatWild(PatWildSingle);
3339 hi = self.last_span.hi;
3341 id: ast::DUMMY_NODE_ID,
3349 let sub = self.parse_pat();
3351 let last_span = self.last_span;
3353 self.obsolete(last_span, ObsoleteOwnedPattern);
3355 id: ast::DUMMY_NODE_ID,
3360 token::BinOp(token::And) | token::AndAnd => {
3362 let lo = self.span.lo;
3364 let sub = self.parse_pat();
3365 pat = PatRegion(sub);
3366 hi = self.last_span.hi;
3368 id: ast::DUMMY_NODE_ID,
3373 token::OpenDelim(token::Paren) => {
3374 // parse (pat,pat,pat,...) as tuple
3376 if self.check(&token::CloseDelim(token::Paren)) {
3378 pat = PatTup(vec![]);
3380 let mut fields = vec!(self.parse_pat());
3381 if self.look_ahead(1, |t| *t != token::CloseDelim(token::Paren)) {
3382 while self.check(&token::Comma) {
3384 if self.check(&token::CloseDelim(token::Paren)) { break; }
3385 fields.push(self.parse_pat());
3388 if fields.len() == 1 { self.expect(&token::Comma); }
3389 self.expect(&token::CloseDelim(token::Paren));
3390 pat = PatTup(fields);
3392 hi = self.last_span.hi;
3394 id: ast::DUMMY_NODE_ID,
3399 token::OpenDelim(token::Bracket) => {
3400 // parse [pat,pat,...] as vector pattern
3402 let (before, slice, after) =
3403 self.parse_pat_vec_elements();
3405 self.expect(&token::CloseDelim(token::Bracket));
3406 pat = ast::PatVec(before, slice, after);
3407 hi = self.last_span.hi;
3409 id: ast::DUMMY_NODE_ID,
3416 // at this point, token != _, ~, &, &&, (, [
3418 if (!(self.token.is_ident() || self.token.is_path())
3419 && self.token != token::ModSep)
3420 || self.token.is_keyword(keywords::True)
3421 || self.token.is_keyword(keywords::False) {
3422 // Parse an expression pattern or exp .. exp.
3424 // These expressions are limited to literals (possibly
3425 // preceded by unary-minus) or identifiers.
3426 let val = self.parse_literal_maybe_minus();
3427 if (self.check(&token::DotDotDot)) &&
3428 self.look_ahead(1, |t| {
3429 *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3432 let end = if self.token.is_ident() || self.token.is_path() {
3433 let path = self.parse_path(LifetimeAndTypesWithColons);
3434 let hi = self.span.hi;
3435 self.mk_expr(lo, hi, ExprPath(path))
3437 self.parse_literal_maybe_minus()
3439 pat = PatRange(val, end);
3443 } else if self.eat_keyword(keywords::Mut) {
3444 pat = self.parse_pat_ident(BindByValue(MutMutable));
3445 } else if self.eat_keyword(keywords::Ref) {
3447 let mutbl = self.parse_mutability();
3448 pat = self.parse_pat_ident(BindByRef(mutbl));
3449 } else if self.eat_keyword(keywords::Box) {
3452 // FIXME(#13910): Rename to `PatBox` and extend to full DST
3454 let sub = self.parse_pat();
3456 hi = self.last_span.hi;
3458 id: ast::DUMMY_NODE_ID,
3463 let can_be_enum_or_struct = self.look_ahead(1, |t| {
3465 token::OpenDelim(_) | token::Lt | token::ModSep => true,
3470 if self.look_ahead(1, |t| *t == token::DotDotDot) &&
3471 self.look_ahead(2, |t| {
3472 *t != token::Comma && *t != token::CloseDelim(token::Bracket)
3474 let start = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3475 self.eat(&token::DotDotDot);
3476 let end = self.parse_expr_res(RESTRICTION_NO_BAR_OP);
3477 pat = PatRange(start, end);
3478 } else if self.token.is_plain_ident() && !can_be_enum_or_struct {
3479 let id = self.parse_ident();
3480 let id_span = self.last_span;
3481 let pth1 = codemap::Spanned{span:id_span, node: id};
3482 if self.eat(&token::Not) {
3484 let delim = self.expect_open_delim();
3485 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
3487 |p| p.parse_token_tree());
3489 let mac = MacInvocTT(ident_to_path(id_span,id), tts, EMPTY_CTXT);
3490 pat = ast::PatMac(codemap::Spanned {node: mac, span: self.span});
3492 let sub = if self.eat(&token::At) {
3494 Some(self.parse_pat())
3499 pat = PatIdent(BindByValue(MutImmutable), pth1, sub);
3502 // parse an enum pat
3503 let enum_path = self.parse_path(LifetimeAndTypesWithColons);
3505 token::OpenDelim(token::Brace) => {
3508 self.parse_pat_fields();
3510 pat = PatStruct(enum_path, fields, etc);
3513 let mut args: Vec<P<Pat>> = Vec::new();
3515 token::OpenDelim(token::Paren) => {
3516 let is_dotdot = self.look_ahead(1, |t| {
3518 token::DotDot => true,
3523 // This is a "top constructor only" pat
3526 self.expect(&token::CloseDelim(token::Paren));
3527 pat = PatEnum(enum_path, None);
3529 args = self.parse_enum_variant_seq(
3530 &token::OpenDelim(token::Paren),
3531 &token::CloseDelim(token::Paren),
3532 seq_sep_trailing_allowed(token::Comma),
3535 pat = PatEnum(enum_path, Some(args));
3539 if !enum_path.global &&
3540 enum_path.segments.len() == 1 &&
3541 enum_path.segments[0].parameters.is_empty()
3543 // it could still be either an enum
3544 // or an identifier pattern, resolve
3545 // will sort it out:
3546 pat = PatIdent(BindByValue(MutImmutable),
3548 span: enum_path.span,
3549 node: enum_path.segments[0]
3553 pat = PatEnum(enum_path, Some(args));
3561 hi = self.last_span.hi;
3563 id: ast::DUMMY_NODE_ID,
3565 span: mk_sp(lo, hi),
3569 /// Parse ident or ident @ pat
3570 /// used by the copy foo and ref foo patterns to give a good
3571 /// error message when parsing mistakes like ref foo(a,b)
3572 fn parse_pat_ident(&mut self,
3573 binding_mode: ast::BindingMode)
3575 if !self.token.is_plain_ident() {
3576 let span = self.span;
3577 let tok_str = self.this_token_to_string();
3578 self.span_fatal(span,
3579 format!("expected identifier, found `{}`", tok_str)[]);
3581 let ident = self.parse_ident();
3582 let last_span = self.last_span;
3583 let name = codemap::Spanned{span: last_span, node: ident};
3584 let sub = if self.eat(&token::At) {
3585 Some(self.parse_pat())
3590 // just to be friendly, if they write something like
3592 // we end up here with ( as the current token. This shortly
3593 // leads to a parse error. Note that if there is no explicit
3594 // binding mode then we do not end up here, because the lookahead
3595 // will direct us over to parse_enum_variant()
3596 if self.token == token::OpenDelim(token::Paren) {
3597 let last_span = self.last_span;
3600 "expected identifier, found enum pattern");
3603 PatIdent(binding_mode, name, sub)
3606 /// Parse a local variable declaration
3607 fn parse_local(&mut self) -> P<Local> {
3608 let lo = self.span.lo;
3609 let pat = self.parse_pat();
3612 if self.eat(&token::Colon) {
3613 ty = Some(self.parse_ty_sum());
3615 let init = self.parse_initializer();
3620 id: ast::DUMMY_NODE_ID,
3621 span: mk_sp(lo, self.last_span.hi),
3626 /// Parse a "let" stmt
3627 fn parse_let(&mut self) -> P<Decl> {
3628 let lo = self.span.lo;
3629 let local = self.parse_local();
3630 P(spanned(lo, self.last_span.hi, DeclLocal(local)))
3633 /// Parse a structure field
3634 fn parse_name_and_ty(&mut self, pr: Visibility,
3635 attrs: Vec<Attribute> ) -> StructField {
3636 let lo = self.span.lo;
3637 if !self.token.is_plain_ident() {
3638 self.fatal("expected ident");
3640 let name = self.parse_ident();
3641 self.expect(&token::Colon);
3642 let ty = self.parse_ty_sum();
3643 spanned(lo, self.last_span.hi, ast::StructField_ {
3644 kind: NamedField(name, pr),
3645 id: ast::DUMMY_NODE_ID,
3651 /// Get an expected item after attributes error message.
3652 fn expected_item_err(attrs: &[Attribute]) -> &'static str {
3653 match attrs.last() {
3654 Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
3655 "expected item after doc comment"
3657 _ => "expected item after attributes",
3661 /// Parse a statement. may include decl.
3662 /// Precondition: any attributes are parsed already
3663 pub fn parse_stmt(&mut self, item_attrs: Vec<Attribute>) -> P<Stmt> {
3664 maybe_whole!(self, NtStmt);
3666 fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) {
3667 // If we have attributes then we should have an item
3668 if !attrs.is_empty() {
3669 let last_span = p.last_span;
3670 p.span_err(last_span, Parser::expected_item_err(attrs));
3674 let lo = self.span.lo;
3675 if self.token.is_keyword(keywords::Let) {
3676 check_expected_item(self, item_attrs[]);
3677 self.expect_keyword(keywords::Let);
3678 let decl = self.parse_let();
3679 P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3680 } else if self.token.is_ident()
3681 && !self.token.is_any_keyword()
3682 && self.look_ahead(1, |t| *t == token::Not) {
3683 // it's a macro invocation:
3685 check_expected_item(self, item_attrs[]);
3687 // Potential trouble: if we allow macros with paths instead of
3688 // idents, we'd need to look ahead past the whole path here...
3689 let pth = self.parse_path(NoTypesAllowed);
3692 let id = match self.token {
3693 token::OpenDelim(_) => token::special_idents::invalid, // no special identifier
3694 _ => self.parse_ident(),
3697 // check that we're pointing at delimiters (need to check
3698 // again after the `if`, because of `parse_ident`
3699 // consuming more tokens).
3700 let delim = match self.token {
3701 token::OpenDelim(delim) => delim,
3703 // we only expect an ident if we didn't parse one
3705 let ident_str = if id.name == token::special_idents::invalid.name {
3710 let tok_str = self.this_token_to_string();
3711 self.fatal(format!("expected {}`(` or `{{`, found `{}`",
3717 let tts = self.parse_unspanned_seq(
3718 &token::OpenDelim(delim),
3719 &token::CloseDelim(delim),
3721 |p| p.parse_token_tree()
3723 let hi = self.span.hi;
3725 let style = if delim == token::Brace {
3728 MacStmtWithoutBraces
3731 if id.name == token::special_idents::invalid.name {
3734 StmtMac(P(spanned(lo,
3736 MacInvocTT(pth, tts, EMPTY_CTXT))),
3739 // if it has a special ident, it's definitely an item
3741 // Require a semicolon or braces.
3742 if style != MacStmtWithBraces {
3743 if !self.eat(&token::Semi) {
3744 let last_span = self.last_span;
3745 self.span_err(last_span,
3746 "macros that expand to items must \
3747 either be surrounded with braces or \
3748 followed by a semicolon");
3751 P(spanned(lo, hi, StmtDecl(
3752 P(spanned(lo, hi, DeclItem(
3754 lo, hi, id /*id is good here*/,
3755 ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))),
3756 Inherited, Vec::new(/*no attrs*/))))),
3757 ast::DUMMY_NODE_ID)))
3760 let found_attrs = !item_attrs.is_empty();
3761 let item_err = Parser::expected_item_err(item_attrs[]);
3762 match self.parse_item_or_view_item(item_attrs, false) {
3765 let decl = P(spanned(lo, hi, DeclItem(i)));
3766 P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))
3768 IoviViewItem(vi) => {
3769 self.span_fatal(vi.span,
3770 "view items must be declared at the top of the block");
3772 IoviForeignItem(_) => {
3773 self.fatal("foreign items are not allowed here");
3777 let last_span = self.last_span;
3778 self.span_err(last_span, item_err);
3781 // Remainder are line-expr stmts.
3782 let e = self.parse_expr_res(RESTRICTION_STMT_EXPR);
3783 P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
3789 /// Is this expression a successfully-parsed statement?
3790 fn expr_is_complete(&mut self, e: &Expr) -> bool {
3791 self.restrictions.contains(RESTRICTION_STMT_EXPR) &&
3792 !classify::expr_requires_semi_to_be_stmt(e)
3795 /// Parse a block. No inner attrs are allowed.
3796 pub fn parse_block(&mut self) -> P<Block> {
3797 maybe_whole!(no_clone self, NtBlock);
3799 let lo = self.span.lo;
3801 if !self.eat(&token::OpenDelim(token::Brace)) {
3803 let tok = self.this_token_to_string();
3804 self.span_fatal_help(sp,
3805 format!("expected `{{`, found `{}`", tok)[],
3806 "place this code inside a block");
3809 return self.parse_block_tail_(lo, DefaultBlock, Vec::new());
3812 /// Parse a block. Inner attrs are allowed.
3813 fn parse_inner_attrs_and_block(&mut self)
3814 -> (Vec<Attribute> , P<Block>) {
3816 maybe_whole!(pair_empty self, NtBlock);
3818 let lo = self.span.lo;
3819 self.expect(&token::OpenDelim(token::Brace));
3820 let (inner, next) = self.parse_inner_attrs_and_next();
3822 (inner, self.parse_block_tail_(lo, DefaultBlock, next))
3825 /// Precondition: already parsed the '{' or '#{'
3826 /// I guess that also means "already parsed the 'impure'" if
3827 /// necessary, and this should take a qualifier.
3828 /// Some blocks start with "#{"...
3829 fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> P<Block> {
3830 self.parse_block_tail_(lo, s, Vec::new())
3833 /// Parse the rest of a block expression or function body
3834 fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode,
3835 first_item_attrs: Vec<Attribute> ) -> P<Block> {
3836 let mut stmts = Vec::new();
3837 let mut expr = None;
3839 // wouldn't it be more uniform to parse view items only, here?
3840 let ParsedItemsAndViewItems {
3845 } = self.parse_items_and_view_items(first_item_attrs,
3848 for item in items.into_iter() {
3849 let span = item.span;
3850 let decl = P(spanned(span.lo, span.hi, DeclItem(item)));
3851 stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))));
3854 let mut attributes_box = attrs_remaining;
3856 while self.token != token::CloseDelim(token::Brace) {
3857 // parsing items even when they're not allowed lets us give
3858 // better error messages and recover more gracefully.
3859 attributes_box.push_all(self.parse_outer_attributes()[]);
3862 if !attributes_box.is_empty() {
3863 let last_span = self.last_span;
3864 self.span_err(last_span,
3865 Parser::expected_item_err(attributes_box[]));
3866 attributes_box = Vec::new();
3868 self.bump(); // empty
3870 token::CloseDelim(token::Brace) => {
3871 // fall through and out.
3874 let stmt = self.parse_stmt(attributes_box);
3875 attributes_box = Vec::new();
3876 stmt.and_then(|Spanned {node, span}| match node {
3877 StmtExpr(e, stmt_id) => {
3878 self.handle_expression_like_statement(e,
3884 StmtMac(macro, MacStmtWithoutBraces) => {
3885 // statement macro without braces; might be an
3886 // expr depending on whether a semicolon follows
3889 stmts.push(P(Spanned {
3890 node: StmtMac(macro,
3891 MacStmtWithSemicolon),
3897 let e = self.mk_mac_expr(span.lo,
3899 macro.and_then(|m| m.node));
3901 self.parse_dot_or_call_expr_with(e);
3902 self.handle_expression_like_statement(
3911 StmtMac(m, style) => {
3912 // statement macro; might be an expr
3915 stmts.push(P(Spanned {
3917 MacStmtWithSemicolon),
3922 token::CloseDelim(token::Brace) => {
3923 // if a block ends in `m!(arg)` without
3924 // a `;`, it must be an expr
3926 self.mk_mac_expr(span.lo,
3928 m.and_then(|x| x.node)));
3931 stmts.push(P(Spanned {
3932 node: StmtMac(m, style),
3938 _ => { // all other kinds of statements:
3939 if classify::stmt_ends_with_semi(&node) {
3940 self.commit_stmt_expecting(token::Semi);
3943 stmts.push(P(Spanned {
3953 if !attributes_box.is_empty() {
3954 let last_span = self.last_span;
3955 self.span_err(last_span,
3956 Parser::expected_item_err(attributes_box[]));
3959 let hi = self.span.hi;
3962 view_items: view_items,
3965 id: ast::DUMMY_NODE_ID,
3967 span: mk_sp(lo, hi),
3971 fn handle_expression_like_statement(
3976 stmts: &mut Vec<P<Stmt>>,
3977 last_block_expr: &mut Option<P<Expr>>) {
3978 // expression without semicolon
3979 if classify::expr_requires_semi_to_be_stmt(&*e) {
3980 // Just check for errors and recover; do not eat semicolon yet.
3981 self.commit_stmt(&[],
3982 &[token::Semi, token::CloseDelim(token::Brace)]);
3988 let span_with_semi = Span {
3990 hi: self.last_span.hi,
3991 expn_id: span.expn_id,
3993 stmts.push(P(Spanned {
3994 node: StmtSemi(e, stmt_id),
3995 span: span_with_semi,
3998 token::CloseDelim(token::Brace) => *last_block_expr = Some(e),
4000 stmts.push(P(Spanned {
4001 node: StmtExpr(e, stmt_id),
4008 // Parses a sequence of bounds if a `:` is found,
4009 // otherwise returns empty list.
4010 fn parse_colon_then_ty_param_bounds(&mut self,
4011 mode: BoundParsingMode)
4012 -> OwnedSlice<TyParamBound>
4014 if !self.eat(&token::Colon) {
4017 self.parse_ty_param_bounds(mode)
4021 // matches bounds = ( boundseq )?
4022 // where boundseq = ( polybound + boundseq ) | polybound
4023 // and polybound = ( 'for' '<' 'region '>' )? bound
4024 // and bound = 'region | trait_ref
4025 fn parse_ty_param_bounds(&mut self,
4026 mode: BoundParsingMode)
4027 -> OwnedSlice<TyParamBound>
4029 let mut result = vec!();
4031 let question_span = self.span;
4032 let ate_question = self.eat(&token::Question);
4034 token::Lifetime(lifetime) => {
4036 self.span_err(question_span,
4037 "`?` may only modify trait bounds, not lifetime bounds");
4039 result.push(RegionTyParamBound(ast::Lifetime {
4040 id: ast::DUMMY_NODE_ID,
4046 token::ModSep | token::Ident(..) => {
4047 let poly_trait_ref = self.parse_poly_trait_ref();
4048 let modifier = if ate_question {
4049 if mode == BoundParsingMode::Modified {
4050 TraitBoundModifier::Maybe
4052 self.span_err(question_span,
4054 TraitBoundModifier::None
4057 TraitBoundModifier::None
4059 result.push(TraitTyParamBound(poly_trait_ref, modifier))
4064 if !self.eat(&token::BinOp(token::Plus)) {
4069 return OwnedSlice::from_vec(result);
4072 fn trait_ref_from_ident(ident: Ident, span: Span) -> TraitRef {
4073 let segment = ast::PathSegment {
4075 parameters: ast::PathParameters::none()
4077 let path = ast::Path {
4080 segments: vec![segment],
4084 ref_id: ast::DUMMY_NODE_ID,
4088 /// Matches typaram = (unbound `?`)? IDENT (`?` unbound)? optbounds ( EQ ty )?
4089 fn parse_ty_param(&mut self) -> TyParam {
4090 // This is a bit hacky. Currently we are only interested in a single
4091 // unbound, and it may only be `Sized`. To avoid backtracking and other
4092 // complications, we parse an ident, then check for `?`. If we find it,
4093 // we use the ident as the unbound, otherwise, we use it as the name of
4094 // type param. Even worse, for now, we need to check for `?` before or
4096 let mut span = self.span;
4097 let mut ident = self.parse_ident();
4098 let mut unbound = None;
4099 if self.eat(&token::Question) {
4100 let tref = Parser::trait_ref_from_ident(ident, span);
4101 unbound = Some(tref);
4103 ident = self.parse_ident();
4106 let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Modified);
4107 if let Some(unbound) = unbound {
4108 let mut bounds_as_vec = bounds.into_vec();
4109 bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4110 trait_ref: unbound },
4111 TraitBoundModifier::Maybe));
4112 bounds = OwnedSlice::from_vec(bounds_as_vec);
4115 let default = if self.check(&token::Eq) {
4117 Some(self.parse_ty_sum())
4123 id: ast::DUMMY_NODE_ID,
4130 /// Parse a set of optional generic type parameter declarations. Where
4131 /// clauses are not parsed here, and must be added later via
4132 /// `parse_where_clause()`.
4134 /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4135 /// | ( < lifetimes , typaramseq ( , )? > )
4136 /// where typaramseq = ( typaram ) | ( typaram , typaramseq )
4137 pub fn parse_generics(&mut self) -> ast::Generics {
4138 if self.eat(&token::Lt) {
4139 let lifetime_defs = self.parse_lifetime_defs();
4140 let mut seen_default = false;
4141 let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
4142 p.forbid_lifetime();
4143 let ty_param = p.parse_ty_param();
4144 if ty_param.default.is_some() {
4145 seen_default = true;
4146 } else if seen_default {
4147 let last_span = p.last_span;
4148 p.span_err(last_span,
4149 "type parameters with a default must be trailing");
4154 lifetimes: lifetime_defs,
4155 ty_params: ty_params,
4156 where_clause: WhereClause {
4157 id: ast::DUMMY_NODE_ID,
4158 predicates: Vec::new(),
4162 ast_util::empty_generics()
4166 fn parse_generic_values_after_lt(&mut self)
4167 -> (Vec<ast::Lifetime>, Vec<P<Ty>>, Vec<P<TypeBinding>>) {
4168 let lifetimes = self.parse_lifetimes(token::Comma);
4170 // First parse types.
4171 let (types, returned) = self.parse_seq_to_gt_or_return(
4174 p.forbid_lifetime();
4175 if p.look_ahead(1, |t| t == &token::Eq) {
4178 Some(p.parse_ty_sum())
4183 // If we found the `>`, don't continue.
4185 return (lifetimes, types.into_vec(), Vec::new());
4188 // Then parse type bindings.
4189 let bindings = self.parse_seq_to_gt(
4192 p.forbid_lifetime();
4194 let ident = p.parse_ident();
4195 let found_eq = p.eat(&token::Eq);
4198 p.span_warn(span, "whoops, no =?");
4200 let ty = p.parse_ty();
4202 let span = mk_sp(lo, hi);
4203 return P(TypeBinding{id: ast::DUMMY_NODE_ID,
4210 (lifetimes, types.into_vec(), bindings.into_vec())
4213 fn forbid_lifetime(&mut self) {
4214 if self.token.is_lifetime() {
4215 let span = self.span;
4216 self.span_fatal(span, "lifetime parameters must be declared \
4217 prior to type parameters");
4221 /// Parses an optional `where` clause and places it in `generics`.
4224 /// where T : Trait<U, V> + 'b, 'a : 'b
4226 fn parse_where_clause(&mut self, generics: &mut ast::Generics) {
4227 if !self.eat_keyword(keywords::Where) {
4231 let mut parsed_something = false;
4233 let lo = self.span.lo;
4235 token::OpenDelim(token::Brace) => {
4239 token::Lifetime(..) => {
4240 let bounded_lifetime =
4241 self.parse_lifetime();
4243 self.eat(&token::Colon);
4246 self.parse_lifetimes(token::BinOp(token::Plus));
4248 let hi = self.span.hi;
4249 let span = mk_sp(lo, hi);
4251 generics.where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4252 ast::WhereRegionPredicate {
4254 lifetime: bounded_lifetime,
4259 parsed_something = true;
4263 let bounded_ty = self.parse_ty();
4265 if self.eat(&token::Colon) {
4266 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare);
4267 let hi = self.span.hi;
4268 let span = mk_sp(lo, hi);
4270 if bounds.len() == 0 {
4272 "each predicate in a `where` clause must have \
4273 at least one bound in it");
4276 generics.where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4277 ast::WhereBoundPredicate {
4279 bounded_ty: bounded_ty,
4283 parsed_something = true;
4284 } else if self.eat(&token::Eq) {
4285 // let ty = self.parse_ty();
4286 let hi = self.span.hi;
4287 let span = mk_sp(lo, hi);
4288 // generics.where_clause.predicates.push(
4289 // ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
4290 // id: ast::DUMMY_NODE_ID,
4292 // path: panic!("NYI"), //bounded_ty,
4295 // parsed_something = true;
4298 "equality constraints are not yet supported \
4299 in where clauses (#20041)");
4301 let last_span = self.last_span;
4302 self.span_err(last_span,
4303 "unexpected token in `where` clause");
4308 if !self.eat(&token::Comma) {
4313 if !parsed_something {
4314 let last_span = self.last_span;
4315 self.span_err(last_span,
4316 "a `where` clause must have at least one predicate \
4321 fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4322 -> (Vec<Arg> , bool) {
4324 let mut args: Vec<Option<Arg>> =
4325 self.parse_unspanned_seq(
4326 &token::OpenDelim(token::Paren),
4327 &token::CloseDelim(token::Paren),
4328 seq_sep_trailing_allowed(token::Comma),
4330 if p.token == token::DotDotDot {
4333 if p.token != token::CloseDelim(token::Paren) {
4336 "`...` must be last in argument list for variadic function");
4341 "only foreign functions are allowed to be variadic");
4345 Some(p.parse_arg_general(named_args))
4350 let variadic = match args.pop() {
4353 // Need to put back that last arg
4360 if variadic && args.is_empty() {
4362 "variadic function must be declared with at least one named argument");
4365 let args = args.into_iter().map(|x| x.unwrap()).collect();
4370 /// Parse the argument list and result type of a function declaration
4371 pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> P<FnDecl> {
4373 let (args, variadic) = self.parse_fn_args(true, allow_variadic);
4374 let ret_ty = self.parse_ret_ty();
4383 fn is_self_ident(&mut self) -> bool {
4385 token::Ident(id, token::Plain) => id.name == special_idents::self_.name,
4390 fn expect_self_ident(&mut self) -> ast::Ident {
4392 token::Ident(id, token::Plain) if id.name == special_idents::self_.name => {
4397 let token_str = self.this_token_to_string();
4398 self.fatal(format!("expected `self`, found `{}`",
4404 /// Parse the argument list and result type of a function
4405 /// that may have a self type.
4406 fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
4407 F: FnMut(&mut Parser) -> Arg,
4409 fn maybe_parse_borrowed_explicit_self(this: &mut Parser)
4410 -> ast::ExplicitSelf_ {
4411 // The following things are possible to see here:
4416 // fn(&'lt mut self)
4418 // We already know that the current token is `&`.
4420 if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4422 SelfRegion(None, MutImmutable, this.expect_self_ident())
4423 } else if this.look_ahead(1, |t| t.is_mutability()) &&
4424 this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4426 let mutability = this.parse_mutability();
4427 SelfRegion(None, mutability, this.expect_self_ident())
4428 } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4429 this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4431 let lifetime = this.parse_lifetime();
4432 SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
4433 } else if this.look_ahead(1, |t| t.is_lifetime()) &&
4434 this.look_ahead(2, |t| t.is_mutability()) &&
4435 this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
4437 let lifetime = this.parse_lifetime();
4438 let mutability = this.parse_mutability();
4439 SelfRegion(Some(lifetime), mutability, this.expect_self_ident())
4445 self.expect(&token::OpenDelim(token::Paren));
4447 // A bit of complexity and lookahead is needed here in order to be
4448 // backwards compatible.
4449 let lo = self.span.lo;
4450 let mut self_ident_lo = self.span.lo;
4451 let mut self_ident_hi = self.span.hi;
4453 let mut mutbl_self = MutImmutable;
4454 let explicit_self = match self.token {
4455 token::BinOp(token::And) => {
4456 let eself = maybe_parse_borrowed_explicit_self(self);
4457 self_ident_lo = self.last_span.lo;
4458 self_ident_hi = self.last_span.hi;
4462 // We need to make sure it isn't a type
4463 if self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4465 drop(self.expect_self_ident());
4466 let last_span = self.last_span;
4467 self.obsolete(last_span, ObsoleteOwnedSelf)
4471 token::BinOp(token::Star) => {
4472 // Possibly "*self" or "*mut self" -- not supported. Try to avoid
4473 // emitting cryptic "unexpected token" errors.
4475 let _mutability = if self.token.is_mutability() {
4476 self.parse_mutability()
4480 if self.is_self_ident() {
4481 let span = self.span;
4482 self.span_err(span, "cannot pass self by unsafe pointer");
4485 // error case, making bogus self ident:
4486 SelfValue(special_idents::self_)
4488 token::Ident(..) => {
4489 if self.is_self_ident() {
4490 let self_ident = self.expect_self_ident();
4492 // Determine whether this is the fully explicit form, `self:
4494 if self.eat(&token::Colon) {
4495 SelfExplicit(self.parse_ty_sum(), self_ident)
4497 SelfValue(self_ident)
4499 } else if self.token.is_mutability() &&
4500 self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4501 mutbl_self = self.parse_mutability();
4502 let self_ident = self.expect_self_ident();
4504 // Determine whether this is the fully explicit form,
4506 if self.eat(&token::Colon) {
4507 SelfExplicit(self.parse_ty_sum(), self_ident)
4509 SelfValue(self_ident)
4511 } else if self.token.is_mutability() &&
4512 self.look_ahead(1, |t| *t == token::Tilde) &&
4513 self.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4514 mutbl_self = self.parse_mutability();
4516 drop(self.expect_self_ident());
4517 let last_span = self.last_span;
4518 self.obsolete(last_span, ObsoleteOwnedSelf);
4527 let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
4529 // shared fall-through for the three cases below. borrowing prevents simply
4530 // writing this as a closure
4531 macro_rules! parse_remaining_arguments {
4534 // If we parsed a self type, expect a comma before the argument list.
4538 let sep = seq_sep_trailing_allowed(token::Comma);
4539 let mut fn_inputs = self.parse_seq_to_before_end(
4540 &token::CloseDelim(token::Paren),
4544 fn_inputs.insert(0, Arg::new_self(explicit_self_sp, mutbl_self, $self_id));
4547 token::CloseDelim(token::Paren) => {
4548 vec!(Arg::new_self(explicit_self_sp, mutbl_self, $self_id))
4551 let token_str = self.this_token_to_string();
4552 self.fatal(format!("expected `,` or `)`, found `{}`",
4559 let fn_inputs = match explicit_self {
4561 let sep = seq_sep_trailing_allowed(token::Comma);
4562 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
4564 SelfValue(id) => parse_remaining_arguments!(id),
4565 SelfRegion(_,_,id) => parse_remaining_arguments!(id),
4566 SelfExplicit(_,id) => parse_remaining_arguments!(id),
4570 self.expect(&token::CloseDelim(token::Paren));
4572 let hi = self.span.hi;
4574 let ret_ty = self.parse_ret_ty();
4576 let fn_decl = P(FnDecl {
4582 (spanned(lo, hi, explicit_self), fn_decl)
4585 // parse the |arg, arg| header on a lambda
4586 fn parse_fn_block_decl(&mut self)
4587 -> (P<FnDecl>, Option<UnboxedClosureKind>) {
4588 let (optional_unboxed_closure_kind, inputs_captures) = {
4589 if self.eat(&token::OrOr) {
4592 self.expect(&token::BinOp(token::Or));
4593 let optional_unboxed_closure_kind =
4594 self.parse_optional_unboxed_closure_kind();
4595 let args = self.parse_seq_to_before_end(
4596 &token::BinOp(token::Or),
4597 seq_sep_trailing_allowed(token::Comma),
4598 |p| p.parse_fn_block_arg()
4601 (optional_unboxed_closure_kind, args)
4604 let output = if self.check(&token::RArrow) {
4608 id: ast::DUMMY_NODE_ID,
4615 inputs: inputs_captures,
4618 }), optional_unboxed_closure_kind)
4621 /// Parses the `(arg, arg) -> return_type` header on a procedure.
4622 fn parse_proc_decl(&mut self) -> P<FnDecl> {
4624 self.parse_unspanned_seq(&token::OpenDelim(token::Paren),
4625 &token::CloseDelim(token::Paren),
4626 seq_sep_trailing_allowed(token::Comma),
4627 |p| p.parse_fn_block_arg());
4629 let output = if self.check(&token::RArrow) {
4633 id: ast::DUMMY_NODE_ID,
4646 /// Parse the name and optional generic types of a function header.
4647 fn parse_fn_header(&mut self) -> (Ident, ast::Generics) {
4648 let id = self.parse_ident();
4649 let generics = self.parse_generics();
4653 fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
4654 node: Item_, vis: Visibility,
4655 attrs: Vec<Attribute>) -> P<Item> {
4659 id: ast::DUMMY_NODE_ID,
4666 /// Parse an item-position function declaration.
4667 fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> ItemInfo {
4668 let (ident, mut generics) = self.parse_fn_header();
4669 let decl = self.parse_fn_decl(false);
4670 self.parse_where_clause(&mut generics);
4671 let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4672 (ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))
4675 /// Parse a method in a trait impl
4676 pub fn parse_method_with_outer_attributes(&mut self) -> P<Method> {
4677 let attrs = self.parse_outer_attributes();
4678 let visa = self.parse_visibility();
4679 self.parse_method(attrs, visa)
4682 /// Parse a method in a trait impl, starting with `attrs` attributes.
4683 pub fn parse_method(&mut self,
4684 attrs: Vec<Attribute>,
4687 let lo = self.span.lo;
4689 // code copied from parse_macro_use_or_failure... abstraction!
4690 let (method_, hi, new_attrs) = {
4691 if !self.token.is_any_keyword()
4692 && self.look_ahead(1, |t| *t == token::Not)
4693 && (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
4694 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
4696 let pth = self.parse_path(NoTypesAllowed);
4697 self.expect(&token::Not);
4699 // eat a matched-delimiter token tree:
4700 let delim = self.expect_open_delim();
4701 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
4703 |p| p.parse_token_tree());
4704 let m_ = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
4705 let m: ast::Mac = codemap::Spanned { node: m_,
4706 span: mk_sp(self.span.lo,
4708 if delim != token::Brace {
4709 self.expect(&token::Semi)
4711 (ast::MethMac(m), self.span.hi, attrs)
4713 let unsafety = self.parse_unsafety();
4714 let abi = if self.eat_keyword(keywords::Extern) {
4715 self.parse_opt_abi().unwrap_or(abi::C)
4719 self.expect_keyword(keywords::Fn);
4720 let ident = self.parse_ident();
4721 let mut generics = self.parse_generics();
4722 let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
4725 self.parse_where_clause(&mut generics);
4726 let (inner_attrs, body) = self.parse_inner_attrs_and_block();
4727 let body_span = body.span;
4728 let mut new_attrs = attrs;
4729 new_attrs.push_all(inner_attrs[]);
4730 (ast::MethDecl(ident,
4738 body_span.hi, new_attrs)
4743 id: ast::DUMMY_NODE_ID,
4744 span: mk_sp(lo, hi),
4749 /// Parse trait Foo { ... }
4750 fn parse_item_trait(&mut self, unsafety: Unsafety) -> ItemInfo {
4751 let ident = self.parse_ident();
4752 let mut tps = self.parse_generics();
4753 let unbound = self.parse_for_sized();
4755 // Parse supertrait bounds.
4756 let mut bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare);
4758 if let Some(unbound) = unbound {
4759 let mut bounds_as_vec = bounds.into_vec();
4760 bounds_as_vec.push(TraitTyParamBound(PolyTraitRef { bound_lifetimes: vec![],
4761 trait_ref: unbound },
4762 TraitBoundModifier::Maybe));
4763 bounds = OwnedSlice::from_vec(bounds_as_vec);
4766 self.parse_where_clause(&mut tps);
4768 let meths = self.parse_trait_items();
4769 (ident, ItemTrait(unsafety, tps, bounds, meths), None)
4772 fn parse_impl_items(&mut self) -> (Vec<ImplItem>, Vec<Attribute>) {
4773 let mut impl_items = Vec::new();
4774 self.expect(&token::OpenDelim(token::Brace));
4775 let (inner_attrs, mut method_attrs) =
4776 self.parse_inner_attrs_and_next();
4777 while !self.eat(&token::CloseDelim(token::Brace)) {
4778 method_attrs.extend(self.parse_outer_attributes().into_iter());
4779 let vis = self.parse_visibility();
4780 if self.eat_keyword(keywords::Type) {
4781 impl_items.push(TypeImplItem(P(self.parse_typedef(
4785 impl_items.push(MethodImplItem(self.parse_method(
4789 method_attrs = self.parse_outer_attributes();
4791 (impl_items, inner_attrs)
4794 /// Parses two variants (with the region/type params always optional):
4795 /// impl<T> Foo { ... }
4796 /// impl<T> ToString for ~[T] { ... }
4797 fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> ItemInfo {
4798 // First, parse type parameters if necessary.
4799 let mut generics = self.parse_generics();
4801 // Special case: if the next identifier that follows is '(', don't
4802 // allow this to be parsed as a trait.
4803 let could_be_trait = self.token != token::OpenDelim(token::Paren);
4806 let mut ty = self.parse_ty_sum();
4808 // Parse traits, if necessary.
4809 let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
4810 // New-style trait. Reinterpret the type as a trait.
4811 let opt_trait_ref = match ty.node {
4812 TyPath(ref path, node_id) => {
4814 path: (*path).clone(),
4819 self.span_err(ty.span, "not a trait");
4824 ty = self.parse_ty_sum();
4830 self.parse_where_clause(&mut generics);
4831 let (impl_items, attrs) = self.parse_impl_items();
4833 let ident = ast_util::impl_pretty_name(&opt_trait, &*ty);
4836 ItemImpl(unsafety, generics, opt_trait, ty, impl_items),
4840 /// Parse a::B<String,int>
4841 fn parse_trait_ref(&mut self) -> TraitRef {
4843 path: self.parse_path(LifetimeAndTypesWithoutColons),
4844 ref_id: ast::DUMMY_NODE_ID,
4848 fn parse_late_bound_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
4849 if self.eat_keyword(keywords::For) {
4850 self.expect(&token::Lt);
4851 let lifetime_defs = self.parse_lifetime_defs();
4859 /// Parse for<'l> a::B<String,int>
4860 fn parse_poly_trait_ref(&mut self) -> PolyTraitRef {
4861 let lifetime_defs = self.parse_late_bound_lifetime_defs();
4864 bound_lifetimes: lifetime_defs,
4865 trait_ref: self.parse_trait_ref()
4869 /// Parse struct Foo { ... }
4870 fn parse_item_struct(&mut self) -> ItemInfo {
4871 let class_name = self.parse_ident();
4872 let mut generics = self.parse_generics();
4874 if self.eat(&token::Colon) {
4875 let ty = self.parse_ty_sum();
4876 self.span_err(ty.span, "`virtual` structs have been removed from the language");
4879 self.parse_where_clause(&mut generics);
4881 let mut fields: Vec<StructField>;
4884 if self.eat(&token::OpenDelim(token::Brace)) {
4885 // It's a record-like struct.
4886 is_tuple_like = false;
4887 fields = Vec::new();
4888 while self.token != token::CloseDelim(token::Brace) {
4889 fields.push(self.parse_struct_decl_field(true));
4891 if fields.len() == 0 {
4892 self.fatal(format!("unit-like struct definition should be \
4893 written as `struct {};`",
4894 token::get_ident(class_name))[]);
4897 } else if self.check(&token::OpenDelim(token::Paren)) {
4898 // It's a tuple-like struct.
4899 is_tuple_like = true;
4900 fields = self.parse_unspanned_seq(
4901 &token::OpenDelim(token::Paren),
4902 &token::CloseDelim(token::Paren),
4903 seq_sep_trailing_allowed(token::Comma),
4905 let attrs = p.parse_outer_attributes();
4907 let struct_field_ = ast::StructField_ {
4908 kind: UnnamedField(p.parse_visibility()),
4909 id: ast::DUMMY_NODE_ID,
4910 ty: p.parse_ty_sum(),
4913 spanned(lo, p.span.hi, struct_field_)
4915 if fields.len() == 0 {
4916 self.fatal(format!("unit-like struct definition should be \
4917 written as `struct {};`",
4918 token::get_ident(class_name))[]);
4920 self.expect(&token::Semi);
4921 } else if self.eat(&token::Semi) {
4922 // It's a unit-like struct.
4923 is_tuple_like = true;
4924 fields = Vec::new();
4926 let token_str = self.this_token_to_string();
4927 self.fatal(format!("expected `{}`, `(`, or `;` after struct \
4928 name, found `{}`", "{",
4932 let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug.
4933 let new_id = ast::DUMMY_NODE_ID;
4935 ItemStruct(P(ast::StructDef {
4937 ctor_id: if is_tuple_like { Some(new_id) } else { None },
4942 /// Parse a structure field declaration
4943 pub fn parse_single_struct_field(&mut self,
4945 attrs: Vec<Attribute> )
4947 let a_var = self.parse_name_and_ty(vis, attrs);
4952 token::CloseDelim(token::Brace) => {}
4954 let span = self.span;
4955 let token_str = self.this_token_to_string();
4956 self.span_fatal_help(span,
4957 format!("expected `,`, or `}}`, found `{}`",
4959 "struct fields should be separated by commas")
4965 /// Parse an element of a struct definition
4966 fn parse_struct_decl_field(&mut self, allow_pub: bool) -> StructField {
4968 let attrs = self.parse_outer_attributes();
4970 if self.eat_keyword(keywords::Pub) {
4972 let span = self.last_span;
4973 self.span_err(span, "`pub` is not allowed here");
4975 return self.parse_single_struct_field(Public, attrs);
4978 return self.parse_single_struct_field(Inherited, attrs);
4981 /// Parse visibility: PUB, PRIV, or nothing
4982 fn parse_visibility(&mut self) -> Visibility {
4983 if self.eat_keyword(keywords::Pub) { Public }
4987 fn parse_for_sized(&mut self) -> Option<ast::TraitRef> {
4988 // FIXME, this should really use TraitBoundModifier, but it will get
4989 // re-jigged shortly in any case, so leaving the hacky version for now.
4990 if self.eat_keyword(keywords::For) {
4991 let span = self.span;
4992 let mut ate_question = false;
4993 if self.eat(&token::Question) {
4994 ate_question = true;
4996 let ident = self.parse_ident();
4997 if self.eat(&token::Question) {
5002 ate_question = true;
5006 "expected `?Sized` after `for` in trait item");
5009 let tref = Parser::trait_ref_from_ident(ident, span);
5016 /// Given a termination token and a vector of already-parsed
5017 /// attributes (of length 0 or 1), parse all of the items in a module
5018 fn parse_mod_items(&mut self,
5020 first_item_attrs: Vec<Attribute>,
5023 // parse all of the items up to closing or an attribute.
5024 // view items are legal here.
5025 let ParsedItemsAndViewItems {
5028 items: starting_items,
5030 } = self.parse_items_and_view_items(first_item_attrs, true, true);
5031 let mut items: Vec<P<Item>> = starting_items;
5032 let attrs_remaining_len = attrs_remaining.len();
5034 // don't think this other loop is even necessary....
5036 let mut first = true;
5037 while self.token != term {
5038 let mut attrs = self.parse_outer_attributes();
5040 let mut tmp = attrs_remaining.clone();
5041 tmp.push_all(attrs[]);
5045 debug!("parse_mod_items: parse_item_or_view_item(attrs={})",
5047 match self.parse_item_or_view_item(attrs,
5048 true /* macros allowed */) {
5049 IoviItem(item) => items.push(item),
5050 IoviViewItem(view_item) => {
5051 self.span_fatal(view_item.span,
5052 "view items must be declared at the top of \
5056 let token_str = self.this_token_to_string();
5057 self.fatal(format!("expected item, found `{}`",
5063 if first && attrs_remaining_len > 0u {
5064 // We parsed attributes for the first item but didn't find it
5065 let last_span = self.last_span;
5066 self.span_err(last_span,
5067 Parser::expected_item_err(attrs_remaining[]));
5071 inner: mk_sp(inner_lo, self.span.lo),
5072 view_items: view_items,
5077 fn parse_item_const(&mut self, m: Option<Mutability>) -> ItemInfo {
5078 let id = self.parse_ident();
5079 self.expect(&token::Colon);
5080 let ty = self.parse_ty_sum();
5081 self.expect(&token::Eq);
5082 let e = self.parse_expr();
5083 self.commit_expr_expecting(&*e, token::Semi);
5084 let item = match m {
5085 Some(m) => ItemStatic(ty, m, e),
5086 None => ItemConst(ty, e),
5091 /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5092 fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> ItemInfo {
5093 let id_span = self.span;
5094 let id = self.parse_ident();
5095 if self.check(&token::Semi) {
5097 // This mod is in an external file. Let's go get it!
5098 let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span);
5099 (id, m, Some(attrs))
5101 self.push_mod_path(id, outer_attrs);
5102 self.expect(&token::OpenDelim(token::Brace));
5103 let mod_inner_lo = self.span.lo;
5104 let old_owns_directory = self.owns_directory;
5105 self.owns_directory = true;
5106 let (inner, next) = self.parse_inner_attrs_and_next();
5107 let m = self.parse_mod_items(token::CloseDelim(token::Brace), next, mod_inner_lo);
5108 self.expect(&token::CloseDelim(token::Brace));
5109 self.owns_directory = old_owns_directory;
5110 self.pop_mod_path();
5111 (id, ItemMod(m), Some(inner))
5115 fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
5116 let default_path = self.id_to_interned_str(id);
5117 let file_path = match ::attr::first_attr_value_str_by_name(attrs,
5120 None => default_path,
5122 self.mod_path_stack.push(file_path)
5125 fn pop_mod_path(&mut self) {
5126 self.mod_path_stack.pop().unwrap();
5129 /// Read a module from a source file.
5130 fn eval_src_mod(&mut self,
5132 outer_attrs: &[ast::Attribute],
5134 -> (ast::Item_, Vec<ast::Attribute> ) {
5135 let mut prefix = Path::new(self.sess.span_diagnostic.cm.span_to_filename(self.span));
5137 let mod_path = Path::new(".").join_many(self.mod_path_stack[]);
5138 let dir_path = prefix.join(&mod_path);
5139 let mod_string = token::get_ident(id);
5140 let (file_path, owns_directory) = match ::attr::first_attr_value_str_by_name(
5141 outer_attrs, "path") {
5142 Some(d) => (dir_path.join(d), true),
5144 let mod_name = mod_string.get().to_string();
5145 let default_path_str = format!("{}.rs", mod_name);
5146 let secondary_path_str = format!("{}/mod.rs", mod_name);
5147 let default_path = dir_path.join(default_path_str[]);
5148 let secondary_path = dir_path.join(secondary_path_str[]);
5149 let default_exists = default_path.exists();
5150 let secondary_exists = secondary_path.exists();
5152 if !self.owns_directory {
5153 self.span_err(id_sp,
5154 "cannot declare a new module at this location");
5155 let this_module = match self.mod_path_stack.last() {
5156 Some(name) => name.get().to_string(),
5157 None => self.root_module_name.as_ref().unwrap().clone(),
5159 self.span_note(id_sp,
5160 format!("maybe move this module `{0}` \
5161 to its own directory via \
5164 if default_exists || secondary_exists {
5165 self.span_note(id_sp,
5166 format!("... or maybe `use` the module \
5167 `{}` instead of possibly \
5171 self.abort_if_errors();
5174 match (default_exists, secondary_exists) {
5175 (true, false) => (default_path, false),
5176 (false, true) => (secondary_path, true),
5178 self.span_fatal_help(id_sp,
5179 format!("file not found for module `{}`",
5181 format!("name the file either {} or {} inside \
5185 dir_path.display())[]);
5188 self.span_fatal_help(
5190 format!("file for module `{}` found at both {} \
5194 secondary_path_str)[],
5195 "delete or rename one of them to remove the ambiguity");
5201 self.eval_src_mod_from_path(file_path, owns_directory,
5202 mod_string.get().to_string(), id_sp)
5205 fn eval_src_mod_from_path(&mut self,
5207 owns_directory: bool,
5209 id_sp: Span) -> (ast::Item_, Vec<ast::Attribute> ) {
5210 let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5211 match included_mod_stack.iter().position(|p| *p == path) {
5213 let mut err = String::from_str("circular modules: ");
5214 let len = included_mod_stack.len();
5215 for p in included_mod_stack.slice(i, len).iter() {
5216 err.push_str(p.display().as_cow()[]);
5217 err.push_str(" -> ");
5219 err.push_str(path.display().as_cow()[]);
5220 self.span_fatal(id_sp, err[]);
5224 included_mod_stack.push(path.clone());
5225 drop(included_mod_stack);
5228 new_sub_parser_from_file(self.sess,
5234 let mod_inner_lo = p0.span.lo;
5235 let (mod_attrs, next) = p0.parse_inner_attrs_and_next();
5236 let first_item_outer_attrs = next;
5237 let m0 = p0.parse_mod_items(token::Eof, first_item_outer_attrs, mod_inner_lo);
5238 self.sess.included_mod_stack.borrow_mut().pop();
5239 return (ast::ItemMod(m0), mod_attrs);
5242 /// Parse a function declaration from a foreign module
5243 fn parse_item_foreign_fn(&mut self, vis: ast::Visibility,
5244 attrs: Vec<Attribute>) -> P<ForeignItem> {
5245 let lo = self.span.lo;
5246 self.expect_keyword(keywords::Fn);
5248 let (ident, mut generics) = self.parse_fn_header();
5249 let decl = self.parse_fn_decl(true);
5250 self.parse_where_clause(&mut generics);
5251 let hi = self.span.hi;
5252 self.expect(&token::Semi);
5253 P(ast::ForeignItem {
5256 node: ForeignItemFn(decl, generics),
5257 id: ast::DUMMY_NODE_ID,
5258 span: mk_sp(lo, hi),
5263 /// Parse a static item from a foreign module
5264 fn parse_item_foreign_static(&mut self, vis: ast::Visibility,
5265 attrs: Vec<Attribute>) -> P<ForeignItem> {
5266 let lo = self.span.lo;
5268 self.expect_keyword(keywords::Static);
5269 let mutbl = self.eat_keyword(keywords::Mut);
5271 let ident = self.parse_ident();
5272 self.expect(&token::Colon);
5273 let ty = self.parse_ty_sum();
5274 let hi = self.span.hi;
5275 self.expect(&token::Semi);
5279 node: ForeignItemStatic(ty, mutbl),
5280 id: ast::DUMMY_NODE_ID,
5281 span: mk_sp(lo, hi),
5286 /// At this point, this is essentially a wrapper for
5287 /// parse_foreign_items.
5288 fn parse_foreign_mod_items(&mut self,
5290 first_item_attrs: Vec<Attribute> )
5292 let ParsedItemsAndViewItems {
5297 } = self.parse_foreign_items(first_item_attrs, true);
5298 if !attrs_remaining.is_empty() {
5299 let last_span = self.last_span;
5300 self.span_err(last_span,
5301 Parser::expected_item_err(attrs_remaining[]));
5303 assert!(self.token == token::CloseDelim(token::Brace));
5306 view_items: view_items,
5307 items: foreign_items
5311 /// Parse extern crate links
5315 /// extern crate url;
5316 /// extern crate foo = "bar"; //deprecated
5317 /// extern crate "bar" as foo;
5318 fn parse_item_extern_crate(&mut self,
5320 visibility: Visibility,
5321 attrs: Vec<Attribute> )
5324 let span = self.span;
5325 let (maybe_path, ident) = match self.token {
5326 token::Ident(..) => {
5327 let the_ident = self.parse_ident();
5328 let path = if self.token == token::Eq {
5330 let path = self.parse_str();
5331 let span = self.span;
5332 self.obsolete(span, ObsoleteExternCrateRenaming);
5334 } else if self.eat_keyword(keywords::As) {
5335 // skip the ident if there is one
5336 if self.token.is_ident() { self.bump(); }
5338 self.span_err(span, "expected `;`, found `as`");
5339 self.span_help(span,
5340 format!("perhaps you meant to enclose the crate name `{}` in \
5342 the_ident.as_str())[]);
5347 self.expect(&token::Semi);
5350 token::Literal(token::Str_(..), suf) | token::Literal(token::StrRaw(..), suf) => {
5352 self.expect_no_suffix(sp, "extern crate name", suf);
5353 // forgo the internal suffix check of `parse_str` to
5354 // avoid repeats (this unwrap will always succeed due
5355 // to the restriction of the `match`)
5356 let (s, style, _) = self.parse_optional_str().unwrap();
5357 self.expect_keyword(keywords::As);
5358 let the_ident = self.parse_ident();
5359 self.expect(&token::Semi);
5360 (Some((s, style)), the_ident)
5363 let span = self.span;
5364 let token_str = self.this_token_to_string();
5365 self.span_fatal(span,
5366 format!("expected extern crate name but \
5372 IoviViewItem(ast::ViewItem {
5373 node: ViewItemExternCrate(ident, maybe_path, ast::DUMMY_NODE_ID),
5376 span: mk_sp(lo, self.last_span.hi)
5380 /// Parse `extern` for foreign ABIs
5383 /// `extern` is expected to have been
5384 /// consumed before calling this method
5390 fn parse_item_foreign_mod(&mut self,
5392 opt_abi: Option<abi::Abi>,
5393 visibility: Visibility,
5394 attrs: Vec<Attribute> )
5397 self.expect(&token::OpenDelim(token::Brace));
5399 let abi = opt_abi.unwrap_or(abi::C);
5401 let (inner, next) = self.parse_inner_attrs_and_next();
5402 let m = self.parse_foreign_mod_items(abi, next);
5403 self.expect(&token::CloseDelim(token::Brace));
5405 let last_span = self.last_span;
5406 let item = self.mk_item(lo,
5408 special_idents::invalid,
5411 maybe_append(attrs, Some(inner)));
5412 return IoviItem(item);
5415 /// Parse type Foo = Bar;
5416 fn parse_item_type(&mut self) -> ItemInfo {
5417 let ident = self.parse_ident();
5418 let mut tps = self.parse_generics();
5419 self.parse_where_clause(&mut tps);
5420 self.expect(&token::Eq);
5421 let ty = self.parse_ty_sum();
5422 self.expect(&token::Semi);
5423 (ident, ItemTy(ty, tps), None)
5426 /// Parse a structure-like enum variant definition
5427 /// this should probably be renamed or refactored...
5428 fn parse_struct_def(&mut self) -> P<StructDef> {
5429 let mut fields: Vec<StructField> = Vec::new();
5430 while self.token != token::CloseDelim(token::Brace) {
5431 fields.push(self.parse_struct_decl_field(false));
5441 /// Parse the part of an "enum" decl following the '{'
5442 fn parse_enum_def(&mut self, _generics: &ast::Generics) -> EnumDef {
5443 let mut variants = Vec::new();
5444 let mut all_nullary = true;
5445 let mut any_disr = None;
5446 while self.token != token::CloseDelim(token::Brace) {
5447 let variant_attrs = self.parse_outer_attributes();
5448 let vlo = self.span.lo;
5450 let vis = self.parse_visibility();
5454 let mut args = Vec::new();
5455 let mut disr_expr = None;
5456 ident = self.parse_ident();
5457 if self.eat(&token::OpenDelim(token::Brace)) {
5458 // Parse a struct variant.
5459 all_nullary = false;
5460 let start_span = self.span;
5461 let struct_def = self.parse_struct_def();
5462 if struct_def.fields.len() == 0 {
5463 self.span_err(start_span,
5464 format!("unit-like struct variant should be written \
5465 without braces, as `{},`",
5466 token::get_ident(ident))[]);
5468 kind = StructVariantKind(struct_def);
5469 } else if self.check(&token::OpenDelim(token::Paren)) {
5470 all_nullary = false;
5471 let arg_tys = self.parse_enum_variant_seq(
5472 &token::OpenDelim(token::Paren),
5473 &token::CloseDelim(token::Paren),
5474 seq_sep_trailing_allowed(token::Comma),
5475 |p| p.parse_ty_sum()
5477 for ty in arg_tys.into_iter() {
5478 args.push(ast::VariantArg {
5480 id: ast::DUMMY_NODE_ID,
5483 kind = TupleVariantKind(args);
5484 } else if self.eat(&token::Eq) {
5485 disr_expr = Some(self.parse_expr());
5486 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5487 kind = TupleVariantKind(args);
5489 kind = TupleVariantKind(Vec::new());
5492 let vr = ast::Variant_ {
5494 attrs: variant_attrs,
5496 id: ast::DUMMY_NODE_ID,
5497 disr_expr: disr_expr,
5500 variants.push(P(spanned(vlo, self.last_span.hi, vr)));
5502 if !self.eat(&token::Comma) { break; }
5504 self.expect(&token::CloseDelim(token::Brace));
5506 Some(disr_span) if !all_nullary =>
5507 self.span_err(disr_span,
5508 "discriminator values can only be used with a c-like enum"),
5512 ast::EnumDef { variants: variants }
5515 /// Parse an "enum" declaration
5516 fn parse_item_enum(&mut self) -> ItemInfo {
5517 let id = self.parse_ident();
5518 let mut generics = self.parse_generics();
5519 self.parse_where_clause(&mut generics);
5520 self.expect(&token::OpenDelim(token::Brace));
5522 let enum_definition = self.parse_enum_def(&generics);
5523 (id, ItemEnum(enum_definition, generics), None)
5526 fn fn_expr_lookahead(tok: &token::Token) -> bool {
5528 token::OpenDelim(token::Paren) | token::At | token::Tilde | token::BinOp(_) => true,
5533 /// Parses a string as an ABI spec on an extern type or module. Consumes
5534 /// the `extern` keyword, if one is found.
5535 fn parse_opt_abi(&mut self) -> Option<abi::Abi> {
5537 token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5539 self.expect_no_suffix(sp, "ABI spec", suf);
5541 let the_string = s.as_str();
5542 match abi::lookup(the_string) {
5543 Some(abi) => Some(abi),
5545 let last_span = self.last_span;
5548 format!("illegal ABI: expected one of [{}], \
5550 abi::all_names().connect(", "),
5561 /// Parse one of the items or view items allowed by the
5562 /// flags; on failure, return IoviNone.
5563 /// NB: this function no longer parses the items inside an
5565 fn parse_item_or_view_item(&mut self,
5566 attrs: Vec<Attribute> ,
5567 macros_allowed: bool)
5569 let nt_item = match self.token {
5570 token::Interpolated(token::NtItem(ref item)) => {
5571 Some((**item).clone())
5578 let mut attrs = attrs;
5579 mem::swap(&mut item.attrs, &mut attrs);
5580 item.attrs.extend(attrs.into_iter());
5581 return IoviItem(P(item));
5586 let lo = self.span.lo;
5588 let visibility = self.parse_visibility();
5590 // must be a view item:
5591 if self.eat_keyword(keywords::Use) {
5592 // USE ITEM (IoviViewItem)
5593 let view_item = self.parse_use();
5594 self.expect(&token::Semi);
5595 return IoviViewItem(ast::ViewItem {
5599 span: mk_sp(lo, self.last_span.hi)
5602 // either a view item or an item:
5603 if self.eat_keyword(keywords::Extern) {
5604 let next_is_mod = self.eat_keyword(keywords::Mod);
5606 if next_is_mod || self.eat_keyword(keywords::Crate) {
5608 let last_span = self.last_span;
5609 self.span_err(mk_sp(lo, last_span.hi),
5610 format!("`extern mod` is obsolete, use \
5611 `extern crate` instead \
5612 to refer to external \
5615 return self.parse_item_extern_crate(lo, visibility, attrs);
5618 let opt_abi = self.parse_opt_abi();
5620 if self.eat_keyword(keywords::Fn) {
5621 // EXTERN FUNCTION ITEM
5622 let abi = opt_abi.unwrap_or(abi::C);
5623 let (ident, item_, extra_attrs) =
5624 self.parse_item_fn(Unsafety::Normal, abi);
5625 let last_span = self.last_span;
5626 let item = self.mk_item(lo,
5631 maybe_append(attrs, extra_attrs));
5632 return IoviItem(item);
5633 } else if self.check(&token::OpenDelim(token::Brace)) {
5634 return self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs);
5637 let span = self.span;
5638 let token_str = self.this_token_to_string();
5639 self.span_fatal(span,
5640 format!("expected `{}` or `fn`, found `{}`", "{",
5644 if self.eat_keyword(keywords::Virtual) {
5645 let span = self.span;
5646 self.span_err(span, "`virtual` structs have been removed from the language");
5649 // the rest are all guaranteed to be items:
5650 if self.token.is_keyword(keywords::Static) {
5653 let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
5654 let (ident, item_, extra_attrs) = self.parse_item_const(Some(m));
5655 let last_span = self.last_span;
5656 let item = self.mk_item(lo,
5661 maybe_append(attrs, extra_attrs));
5662 return IoviItem(item);
5664 if self.token.is_keyword(keywords::Const) {
5667 if self.eat_keyword(keywords::Mut) {
5668 let last_span = self.last_span;
5669 self.span_err(last_span, "const globals cannot be mutable");
5670 self.span_help(last_span, "did you mean to declare a static?");
5672 let (ident, item_, extra_attrs) = self.parse_item_const(None);
5673 let last_span = self.last_span;
5674 let item = self.mk_item(lo,
5679 maybe_append(attrs, extra_attrs));
5680 return IoviItem(item);
5682 if self.token.is_keyword(keywords::Unsafe) &&
5683 self.look_ahead(1u, |t| t.is_keyword(keywords::Trait))
5685 // UNSAFE TRAIT ITEM
5686 self.expect_keyword(keywords::Unsafe);
5687 self.expect_keyword(keywords::Trait);
5688 let (ident, item_, extra_attrs) =
5689 self.parse_item_trait(ast::Unsafety::Unsafe);
5690 let last_span = self.last_span;
5691 let item = self.mk_item(lo,
5696 maybe_append(attrs, extra_attrs));
5697 return IoviItem(item);
5699 if self.token.is_keyword(keywords::Unsafe) &&
5700 self.look_ahead(1u, |t| t.is_keyword(keywords::Impl))
5703 self.expect_keyword(keywords::Unsafe);
5704 self.expect_keyword(keywords::Impl);
5705 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe);
5706 let last_span = self.last_span;
5707 let item = self.mk_item(lo,
5712 maybe_append(attrs, extra_attrs));
5713 return IoviItem(item);
5715 if self.token.is_keyword(keywords::Fn) &&
5716 self.look_ahead(1, |f| !Parser::fn_expr_lookahead(f)) {
5719 let (ident, item_, extra_attrs) =
5720 self.parse_item_fn(Unsafety::Normal, abi::Rust);
5721 let last_span = self.last_span;
5722 let item = self.mk_item(lo,
5727 maybe_append(attrs, extra_attrs));
5728 return IoviItem(item);
5730 if self.token.is_keyword(keywords::Unsafe)
5731 && self.look_ahead(1u, |t| *t != token::OpenDelim(token::Brace)) {
5732 // UNSAFE FUNCTION ITEM
5734 let abi = if self.eat_keyword(keywords::Extern) {
5735 self.parse_opt_abi().unwrap_or(abi::C)
5739 self.expect_keyword(keywords::Fn);
5740 let (ident, item_, extra_attrs) =
5741 self.parse_item_fn(Unsafety::Unsafe, abi);
5742 let last_span = self.last_span;
5743 let item = self.mk_item(lo,
5748 maybe_append(attrs, extra_attrs));
5749 return IoviItem(item);
5751 if self.eat_keyword(keywords::Mod) {
5753 let (ident, item_, extra_attrs) =
5754 self.parse_item_mod(attrs[]);
5755 let last_span = self.last_span;
5756 let item = self.mk_item(lo,
5761 maybe_append(attrs, extra_attrs));
5762 return IoviItem(item);
5764 if self.eat_keyword(keywords::Type) {
5766 let (ident, item_, extra_attrs) = self.parse_item_type();
5767 let last_span = self.last_span;
5768 let item = self.mk_item(lo,
5773 maybe_append(attrs, extra_attrs));
5774 return IoviItem(item);
5776 if self.eat_keyword(keywords::Enum) {
5778 let (ident, item_, extra_attrs) = self.parse_item_enum();
5779 let last_span = self.last_span;
5780 let item = self.mk_item(lo,
5785 maybe_append(attrs, extra_attrs));
5786 return IoviItem(item);
5788 if self.eat_keyword(keywords::Trait) {
5790 let (ident, item_, extra_attrs) =
5791 self.parse_item_trait(ast::Unsafety::Normal);
5792 let last_span = self.last_span;
5793 let item = self.mk_item(lo,
5798 maybe_append(attrs, extra_attrs));
5799 return IoviItem(item);
5801 if self.eat_keyword(keywords::Impl) {
5803 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal);
5804 let last_span = self.last_span;
5805 let item = self.mk_item(lo,
5810 maybe_append(attrs, extra_attrs));
5811 return IoviItem(item);
5813 if self.eat_keyword(keywords::Struct) {
5815 let (ident, item_, extra_attrs) = self.parse_item_struct();
5816 let last_span = self.last_span;
5817 let item = self.mk_item(lo,
5822 maybe_append(attrs, extra_attrs));
5823 return IoviItem(item);
5825 self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5828 /// Parse a foreign item; on failure, return IoviNone.
5829 fn parse_foreign_item(&mut self,
5830 attrs: Vec<Attribute> ,
5831 macros_allowed: bool)
5833 maybe_whole!(iovi self, NtItem);
5834 let lo = self.span.lo;
5836 let visibility = self.parse_visibility();
5838 if self.token.is_keyword(keywords::Static) {
5839 // FOREIGN STATIC ITEM
5840 let item = self.parse_item_foreign_static(visibility, attrs);
5841 return IoviForeignItem(item);
5843 if self.token.is_keyword(keywords::Fn) || self.token.is_keyword(keywords::Unsafe) {
5844 // FOREIGN FUNCTION ITEM
5845 let item = self.parse_item_foreign_fn(visibility, attrs);
5846 return IoviForeignItem(item);
5848 self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
5851 /// This is the fall-through for parsing items.
5852 fn parse_macro_use_or_failure(
5854 attrs: Vec<Attribute> ,
5855 macros_allowed: bool,
5857 visibility: Visibility
5858 ) -> ItemOrViewItem {
5859 if macros_allowed && !self.token.is_any_keyword()
5860 && self.look_ahead(1, |t| *t == token::Not)
5861 && (self.look_ahead(2, |t| t.is_plain_ident())
5862 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
5863 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
5864 // MACRO INVOCATION ITEM
5867 let pth = self.parse_path(NoTypesAllowed);
5868 self.expect(&token::Not);
5870 // a 'special' identifier (like what `macro_rules!` uses)
5871 // is optional. We should eventually unify invoc syntax
5873 let id = if self.token.is_plain_ident() {
5876 token::special_idents::invalid // no special identifier
5878 // eat a matched-delimiter token tree:
5879 let delim = self.expect_open_delim();
5880 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
5882 |p| p.parse_token_tree());
5883 // single-variant-enum... :
5884 let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
5885 let m: ast::Mac = codemap::Spanned { node: m,
5886 span: mk_sp(self.span.lo,
5889 if delim != token::Brace {
5890 if !self.eat(&token::Semi) {
5891 let last_span = self.last_span;
5892 self.span_err(last_span,
5893 "macros that expand to items must either \
5894 be surrounded with braces or followed by \
5899 let item_ = ItemMac(m);
5900 let last_span = self.last_span;
5901 let item = self.mk_item(lo,
5907 return IoviItem(item);
5910 // FAILURE TO PARSE ITEM
5914 let last_span = self.last_span;
5915 self.span_fatal(last_span, "unmatched visibility `pub`");
5918 return IoviNone(attrs);
5921 pub fn parse_item_with_outer_attributes(&mut self) -> Option<P<Item>> {
5922 let attrs = self.parse_outer_attributes();
5923 self.parse_item(attrs)
5926 pub fn parse_item(&mut self, attrs: Vec<Attribute>) -> Option<P<Item>> {
5927 match self.parse_item_or_view_item(attrs, true) {
5928 IoviNone(_) => None,
5930 self.fatal("view items are not allowed here"),
5931 IoviForeignItem(_) =>
5932 self.fatal("foreign items are not allowed here"),
5933 IoviItem(item) => Some(item)
5937 /// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo`
5938 pub fn parse_view_item(&mut self, attrs: Vec<Attribute>) -> ViewItem {
5939 match self.parse_item_or_view_item(attrs, false) {
5940 IoviViewItem(vi) => vi,
5941 _ => self.fatal("expected `use` or `extern crate`"),
5945 /// Parse, e.g., "use a::b::{z,y}"
5946 fn parse_use(&mut self) -> ViewItem_ {
5947 return ViewItemUse(self.parse_view_path());
5951 /// Matches view_path : MOD? non_global_path as IDENT
5952 /// | MOD? non_global_path MOD_SEP LBRACE RBRACE
5953 /// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE
5954 /// | MOD? non_global_path MOD_SEP STAR
5955 /// | MOD? non_global_path
5956 fn parse_view_path(&mut self) -> P<ViewPath> {
5957 let lo = self.span.lo;
5959 if self.check(&token::OpenDelim(token::Brace)) {
5961 let idents = self.parse_unspanned_seq(
5962 &token::OpenDelim(token::Brace),
5963 &token::CloseDelim(token::Brace),
5964 seq_sep_trailing_allowed(token::Comma),
5965 |p| p.parse_path_list_item());
5966 let path = ast::Path {
5967 span: mk_sp(lo, self.span.hi),
5969 segments: Vec::new()
5971 return P(spanned(lo, self.span.hi,
5972 ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
5975 let first_ident = self.parse_ident();
5976 let mut path = vec!(first_ident);
5981 let path_lo = self.span.lo;
5982 path = vec!(self.parse_ident());
5983 while self.check(&token::ModSep) {
5985 let id = self.parse_ident();
5988 let span = mk_sp(path_lo, self.span.hi);
5989 self.obsolete(span, ObsoleteImportRenaming);
5990 let path = ast::Path {
5993 segments: path.into_iter().map(|identifier| {
5995 identifier: identifier,
5996 parameters: ast::PathParameters::none(),
6000 return P(spanned(lo, self.span.hi,
6001 ViewPathSimple(first_ident, path,
6002 ast::DUMMY_NODE_ID)));
6006 // foo::bar or foo::{a,b,c} or foo::*
6007 while self.check(&token::ModSep) {
6011 token::Ident(i, _) => {
6016 // foo::bar::{a,b,c}
6017 token::OpenDelim(token::Brace) => {
6018 let idents = self.parse_unspanned_seq(
6019 &token::OpenDelim(token::Brace),
6020 &token::CloseDelim(token::Brace),
6021 seq_sep_trailing_allowed(token::Comma),
6022 |p| p.parse_path_list_item()
6024 let path = ast::Path {
6025 span: mk_sp(lo, self.span.hi),
6027 segments: path.into_iter().map(|identifier| {
6029 identifier: identifier,
6030 parameters: ast::PathParameters::none(),
6034 return P(spanned(lo, self.span.hi,
6035 ViewPathList(path, idents, ast::DUMMY_NODE_ID)));
6039 token::BinOp(token::Star) => {
6041 let path = ast::Path {
6042 span: mk_sp(lo, self.span.hi),
6044 segments: path.into_iter().map(|identifier| {
6046 identifier: identifier,
6047 parameters: ast::PathParameters::none(),
6051 return P(spanned(lo, self.span.hi,
6052 ViewPathGlob(path, ast::DUMMY_NODE_ID)));
6061 let mut rename_to = path[path.len() - 1u];
6062 let path = ast::Path {
6063 span: mk_sp(lo, self.last_span.hi),
6065 segments: path.into_iter().map(|identifier| {
6067 identifier: identifier,
6068 parameters: ast::PathParameters::none(),
6072 if self.eat_keyword(keywords::As) {
6073 rename_to = self.parse_ident()
6077 ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID)))
6080 /// Parses a sequence of items. Stops when it finds program
6081 /// text that can't be parsed as an item
6082 /// - mod_items uses extern_mod_allowed = true
6083 /// - block_tail_ uses extern_mod_allowed = false
6084 fn parse_items_and_view_items(&mut self,
6085 first_item_attrs: Vec<Attribute> ,
6086 mut extern_mod_allowed: bool,
6087 macros_allowed: bool)
6088 -> ParsedItemsAndViewItems {
6089 let mut attrs = first_item_attrs;
6090 attrs.push_all(self.parse_outer_attributes()[]);
6091 // First, parse view items.
6092 let mut view_items : Vec<ast::ViewItem> = Vec::new();
6093 let mut items = Vec::new();
6095 // I think this code would probably read better as a single
6096 // loop with a mutable three-state-variable (for extern crates,
6097 // view items, and regular items) ... except that because
6098 // of macros, I'd like to delay that entire check until later.
6100 match self.parse_item_or_view_item(attrs, macros_allowed) {
6101 IoviNone(attrs) => {
6102 return ParsedItemsAndViewItems {
6103 attrs_remaining: attrs,
6104 view_items: view_items,
6106 foreign_items: Vec::new()
6109 IoviViewItem(view_item) => {
6110 match view_item.node {
6111 ViewItemUse(..) => {
6112 // `extern crate` must precede `use`.
6113 extern_mod_allowed = false;
6115 ViewItemExternCrate(..) if !extern_mod_allowed => {
6116 self.span_err(view_item.span,
6117 "\"extern crate\" declarations are \
6120 ViewItemExternCrate(..) => {}
6122 view_items.push(view_item);
6126 attrs = self.parse_outer_attributes();
6129 IoviForeignItem(_) => {
6133 attrs = self.parse_outer_attributes();
6136 // Next, parse items.
6138 match self.parse_item_or_view_item(attrs, macros_allowed) {
6139 IoviNone(returned_attrs) => {
6140 attrs = returned_attrs;
6143 IoviViewItem(view_item) => {
6144 attrs = self.parse_outer_attributes();
6145 self.span_err(view_item.span,
6146 "`use` and `extern crate` declarations must precede items");
6149 attrs = self.parse_outer_attributes();
6152 IoviForeignItem(_) => {
6158 ParsedItemsAndViewItems {
6159 attrs_remaining: attrs,
6160 view_items: view_items,
6162 foreign_items: Vec::new()
6166 /// Parses a sequence of foreign items. Stops when it finds program
6167 /// text that can't be parsed as an item
6168 fn parse_foreign_items(&mut self, first_item_attrs: Vec<Attribute> ,
6169 macros_allowed: bool)
6170 -> ParsedItemsAndViewItems {
6171 let mut attrs = first_item_attrs;
6172 attrs.push_all(self.parse_outer_attributes()[]);
6173 let mut foreign_items = Vec::new();
6175 match self.parse_foreign_item(attrs, macros_allowed) {
6176 IoviNone(returned_attrs) => {
6177 if self.check(&token::CloseDelim(token::Brace)) {
6178 attrs = returned_attrs;
6183 IoviViewItem(view_item) => {
6184 // I think this can't occur:
6185 self.span_err(view_item.span,
6186 "`use` and `extern crate` declarations must precede items");
6189 // FIXME #5668: this will occur for a macro invocation:
6190 self.span_fatal(item.span, "macros cannot expand to foreign items");
6192 IoviForeignItem(foreign_item) => {
6193 foreign_items.push(foreign_item);
6196 attrs = self.parse_outer_attributes();
6199 ParsedItemsAndViewItems {
6200 attrs_remaining: attrs,
6201 view_items: Vec::new(),
6203 foreign_items: foreign_items
6207 /// Parses a source module as a crate. This is the main
6208 /// entry point for the parser.
6209 pub fn parse_crate_mod(&mut self) -> Crate {
6210 let lo = self.span.lo;
6211 // parse the crate's inner attrs, maybe (oops) one
6212 // of the attrs of an item:
6213 let (inner, next) = self.parse_inner_attrs_and_next();
6214 let first_item_outer_attrs = next;
6215 // parse the items inside the crate:
6216 let m = self.parse_mod_items(token::Eof, first_item_outer_attrs, lo);
6221 config: self.cfg.clone(),
6222 span: mk_sp(lo, self.span.lo),
6223 exported_macros: Vec::new(),
6227 pub fn parse_optional_str(&mut self)
6228 -> Option<(InternedString, ast::StrStyle, Option<ast::Name>)> {
6229 let ret = match self.token {
6230 token::Literal(token::Str_(s), suf) => {
6231 (self.id_to_interned_str(s.ident()), ast::CookedStr, suf)
6233 token::Literal(token::StrRaw(s, n), suf) => {
6234 (self.id_to_interned_str(s.ident()), ast::RawStr(n), suf)
6242 pub fn parse_str(&mut self) -> (InternedString, StrStyle) {
6243 match self.parse_optional_str() {
6244 Some((s, style, suf)) => {
6245 let sp = self.last_span;
6246 self.expect_no_suffix(sp, "str literal", suf);
6249 _ => self.fatal("expected string literal")