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 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
15 use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind};
17 use ast::{BlockCheckMode, CaptureBy};
18 use ast::{Constness, Crate, CrateConfig};
19 use ast::{Decl, DeclKind, Defaultness};
20 use ast::{EMPTY_CTXT, EnumDef};
21 use ast::{Expr, ExprKind, RangeLimits};
22 use ast::{Field, FnDecl};
23 use ast::{ForeignItem, ForeignItemKind, FunctionRetTy};
24 use ast::{Ident, ImplItem, Item, ItemKind};
25 use ast::{Lit, LitKind, UintTy};
27 use ast::MacStmtStyle;
29 use ast::{MutTy, Mutability};
30 use ast::{Pat, PatKind};
31 use ast::{PolyTraitRef, QSelf};
32 use ast::{Stmt, StmtKind};
33 use ast::{VariantData, StructField};
36 use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
37 use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
38 use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
39 use ast::{Visibility, WhereClause};
40 use attr::{ThinAttributes, ThinAttributesExt, AttributesExt};
41 use ast::{BinOpKind, UnOp};
43 use codemap::{self, Span, BytePos, Spanned, spanned, mk_sp, CodeMap};
44 use errors::{self, DiagnosticBuilder};
45 use ext::tt::macro_parser;
48 use parse::common::SeqSep;
49 use parse::lexer::{Reader, TokenAndSpan};
50 use parse::obsolete::{ParserObsoleteMethods, ObsoleteSyntax};
51 use parse::token::{self, intern, MatchNt, SubstNt, SpecialVarNt, InternedString};
52 use parse::token::{keywords, SpecialMacroVar};
53 use parse::{new_sub_parser_from_file, ParseSess};
54 use util::parser::{AssocOp, Fixity};
59 use std::collections::HashSet;
61 use std::path::{Path, PathBuf};
66 flags Restrictions: u8 {
67 const RESTRICTION_STMT_EXPR = 1 << 0,
68 const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
69 const NO_NONINLINE_MOD = 1 << 2,
73 type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute> >);
75 /// How to parse a path. There are three different kinds of paths, all of which
76 /// are parsed somewhat differently.
77 #[derive(Copy, Clone, PartialEq)]
79 /// A path with no type parameters, e.g. `foo::bar::Baz`, used in imports or visibilities.
81 /// A path with a lifetime and type parameters, with no double colons
82 /// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`, used in types.
83 /// Paths using this style can be passed into macros expecting `path` nonterminals.
85 /// A path with a lifetime and type parameters with double colons before
86 /// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`, used in expressions or patterns.
90 /// How to parse a bound, whether to allow bound modifiers such as `?`.
91 #[derive(Copy, Clone, PartialEq)]
92 pub enum BoundParsingMode {
97 #[derive(Clone, Copy, PartialEq)]
98 pub enum SemiColonMode {
103 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
104 /// dropped into the token stream, which happens while parsing the result of
105 /// macro expansion). Placement of these is not as complex as I feared it would
106 /// be. The important thing is to make sure that lookahead doesn't balk at
107 /// `token::Interpolated` tokens.
108 macro_rules! maybe_whole_expr {
111 let found = match $p.token {
112 token::Interpolated(token::NtExpr(ref e)) => {
115 token::Interpolated(token::NtPath(_)) => {
116 // FIXME: The following avoids an issue with lexical borrowck scopes,
117 // but the clone is unfortunate.
118 let pt = match $p.token {
119 token::Interpolated(token::NtPath(ref pt)) => (**pt).clone(),
123 Some($p.mk_expr(span.lo, span.hi, ExprKind::Path(None, pt), None))
125 token::Interpolated(token::NtBlock(_)) => {
126 // FIXME: The following avoids an issue with lexical borrowck scopes,
127 // but the clone is unfortunate.
128 let b = match $p.token {
129 token::Interpolated(token::NtBlock(ref b)) => (*b).clone(),
133 Some($p.mk_expr(span.lo, span.hi, ExprKind::Block(b), None))
148 /// As maybe_whole_expr, but for things other than expressions
149 macro_rules! maybe_whole {
150 ($p:expr, $constructor:ident) => (
152 let found = match ($p).token {
153 token::Interpolated(token::$constructor(_)) => {
154 Some(($p).bump_and_get())
158 if let Some(token::Interpolated(token::$constructor(x))) = found {
159 return Ok(x.clone());
163 (no_clone $p:expr, $constructor:ident) => (
165 let found = match ($p).token {
166 token::Interpolated(token::$constructor(_)) => {
167 Some(($p).bump_and_get())
171 if let Some(token::Interpolated(token::$constructor(x))) = found {
176 (no_clone_from_p $p:expr, $constructor:ident) => (
178 let found = match ($p).token {
179 token::Interpolated(token::$constructor(_)) => {
180 Some(($p).bump_and_get())
184 if let Some(token::Interpolated(token::$constructor(x))) = found {
185 return Ok(x.unwrap());
189 (deref $p:expr, $constructor:ident) => (
191 let found = match ($p).token {
192 token::Interpolated(token::$constructor(_)) => {
193 Some(($p).bump_and_get())
197 if let Some(token::Interpolated(token::$constructor(x))) = found {
198 return Ok((*x).clone());
202 (Some deref $p:expr, $constructor:ident) => (
204 let found = match ($p).token {
205 token::Interpolated(token::$constructor(_)) => {
206 Some(($p).bump_and_get())
210 if let Some(token::Interpolated(token::$constructor(x))) = found {
211 return Ok(Some((*x).clone()));
215 (pair_empty $p:expr, $constructor:ident) => (
217 let found = match ($p).token {
218 token::Interpolated(token::$constructor(_)) => {
219 Some(($p).bump_and_get())
223 if let Some(token::Interpolated(token::$constructor(x))) = found {
224 return Ok((Vec::new(), x));
230 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
232 if let Some(ref attrs) = rhs {
233 lhs.extend(attrs.iter().cloned())
238 /* ident is handled by common.rs */
240 pub struct Parser<'a> {
241 pub sess: &'a ParseSess,
242 /// the current token:
243 pub token: token::Token,
244 /// the span of the current token:
246 /// the span of the prior token:
248 pub cfg: CrateConfig,
249 /// the previous token or None (only stashed sometimes).
250 pub last_token: Option<Box<token::Token>>,
251 last_token_interpolated: bool,
252 last_token_eof: bool,
253 pub buffer: [TokenAndSpan; 4],
254 pub buffer_start: isize,
255 pub buffer_end: isize,
256 pub tokens_consumed: usize,
257 pub restrictions: Restrictions,
258 pub quote_depth: usize, // not (yet) related to the quasiquoter
259 pub reader: Box<Reader+'a>,
260 pub interner: Rc<token::IdentInterner>,
261 /// The set of seen errors about obsolete syntax. Used to suppress
262 /// extra detail when the same error is seen twice
263 pub obsolete_set: HashSet<ObsoleteSyntax>,
264 /// Used to determine the path to externally loaded source files
265 pub filename: Option<String>,
266 pub mod_path_stack: Vec<InternedString>,
267 /// Stack of open delimiters and their spans. Used for error message.
268 pub open_braces: Vec<(token::DelimToken, Span)>,
269 /// Flag if this parser "owns" the directory that it is currently parsing
270 /// in. This will affect how nested files are looked up.
271 pub owns_directory: bool,
272 /// Name of the root module this parser originated from. If `None`, then the
273 /// name is not known. This does not change while the parser is descending
274 /// into modules, and sub-parsers have new values for this name.
275 pub root_module_name: Option<String>,
276 pub expected_tokens: Vec<TokenType>,
279 #[derive(PartialEq, Eq, Clone)]
282 Keyword(keywords::Keyword),
287 fn to_string(&self) -> String {
289 TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
290 TokenType::Operator => "an operator".to_string(),
291 TokenType::Keyword(kw) => format!("`{}`", kw.name()),
296 fn is_ident_or_underscore(t: &token::Token) -> bool {
297 t.is_ident() || *t == token::Underscore
300 /// Information about the path to a module.
301 pub struct ModulePath {
303 pub path_exists: bool,
304 pub result: Result<ModulePathSuccess, ModulePathError>,
307 pub struct ModulePathSuccess {
308 pub path: ::std::path::PathBuf,
309 pub owns_directory: bool,
312 pub struct ModulePathError {
314 pub help_msg: String,
319 AttributesParsed(ThinAttributes),
320 AlreadyParsed(P<Expr>),
323 impl From<Option<ThinAttributes>> for LhsExpr {
324 fn from(o: Option<ThinAttributes>) -> Self {
325 if let Some(attrs) = o {
326 LhsExpr::AttributesParsed(attrs)
328 LhsExpr::NotYetParsed
333 impl From<P<Expr>> for LhsExpr {
334 fn from(expr: P<Expr>) -> Self {
335 LhsExpr::AlreadyParsed(expr)
339 impl<'a> Parser<'a> {
340 pub fn new(sess: &'a ParseSess,
341 cfg: ast::CrateConfig,
342 mut rdr: Box<Reader+'a>)
345 let tok0 = rdr.real_token();
347 let filename = if span != codemap::DUMMY_SP {
348 Some(sess.codemap().span_to_filename(span))
350 let placeholder = TokenAndSpan {
351 tok: token::Underscore,
357 interner: token::get_ident_interner(),
364 last_token_interpolated: false,
365 last_token_eof: false,
375 restrictions: Restrictions::empty(),
377 obsolete_set: HashSet::new(),
378 mod_path_stack: Vec::new(),
380 open_braces: Vec::new(),
381 owns_directory: true,
382 root_module_name: None,
383 expected_tokens: Vec::new(),
387 /// Convert a token to a string using self's reader
388 pub fn token_to_string(token: &token::Token) -> String {
389 pprust::token_to_string(token)
392 /// Convert the current token to a string using self's reader
393 pub fn this_token_to_string(&self) -> String {
394 Parser::token_to_string(&self.token)
397 pub fn this_token_descr(&self) -> String {
398 let s = self.this_token_to_string();
399 if self.token.is_strict_keyword() {
400 format!("keyword `{}`", s)
401 } else if self.token.is_reserved_keyword() {
402 format!("reserved keyword `{}`", s)
408 pub fn unexpected_last<T>(&self, t: &token::Token) -> PResult<'a, T> {
409 let token_str = Parser::token_to_string(t);
410 let last_span = self.last_span;
411 Err(self.span_fatal(last_span, &format!("unexpected token: `{}`", token_str)))
414 pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
415 match self.expect_one_of(&[], &[]) {
417 Ok(_) => unreachable!(),
421 /// Expect and consume the token t. Signal an error if
422 /// the next token is not t.
423 pub fn expect(&mut self, t: &token::Token) -> PResult<'a, ()> {
424 if self.expected_tokens.is_empty() {
425 if self.token == *t {
429 let token_str = Parser::token_to_string(t);
430 let this_token_str = self.this_token_to_string();
431 Err(self.fatal(&format!("expected `{}`, found `{}`",
436 self.expect_one_of(unsafe { slice::from_raw_parts(t, 1) }, &[])
440 /// Expect next token to be edible or inedible token. If edible,
441 /// then consume it; if inedible, then return without consuming
442 /// anything. Signal a fatal error if next token is unexpected.
443 pub fn expect_one_of(&mut self,
444 edible: &[token::Token],
445 inedible: &[token::Token]) -> PResult<'a, ()>{
446 fn tokens_to_string(tokens: &[TokenType]) -> String {
447 let mut i = tokens.iter();
448 // This might be a sign we need a connect method on Iterator.
450 .map_or("".to_string(), |t| t.to_string());
451 i.enumerate().fold(b, |mut b, (i, ref a)| {
452 if tokens.len() > 2 && i == tokens.len() - 2 {
454 } else if tokens.len() == 2 && i == tokens.len() - 2 {
459 b.push_str(&a.to_string());
463 if edible.contains(&self.token) {
466 } else if inedible.contains(&self.token) {
467 // leave it in the input
470 let mut expected = edible.iter()
471 .map(|x| TokenType::Token(x.clone()))
472 .chain(inedible.iter().map(|x| TokenType::Token(x.clone())))
473 .chain(self.expected_tokens.iter().cloned())
474 .collect::<Vec<_>>();
475 expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
477 let expect = tokens_to_string(&expected[..]);
478 let actual = self.this_token_to_string();
480 &(if expected.len() > 1 {
481 (format!("expected one of {}, found `{}`",
484 } else if expected.is_empty() {
485 (format!("unexpected token: `{}`",
488 (format!("expected {}, found `{}`",
496 /// Check for erroneous `ident { }`; if matches, signal error and
497 /// recover (without consuming any expected input token). Returns
498 /// true if and only if input was consumed for recovery.
499 pub fn check_for_erroneous_unit_struct_expecting(&mut self,
500 expected: &[token::Token])
502 if self.token == token::OpenDelim(token::Brace)
503 && expected.iter().all(|t| *t != token::OpenDelim(token::Brace))
504 && self.look_ahead(1, |t| *t == token::CloseDelim(token::Brace)) {
505 // matched; signal non-fatal error and recover.
506 let span = self.span;
507 self.span_err(span, "unit-like struct construction is written with no trailing `{ }`");
508 self.eat(&token::OpenDelim(token::Brace));
509 self.eat(&token::CloseDelim(token::Brace));
516 /// Commit to parsing a complete expression `e` expected to be
517 /// followed by some token from the set edible + inedible. Recover
518 /// from anticipated input errors, discarding erroneous characters.
519 pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token],
520 inedible: &[token::Token]) -> PResult<'a, ()> {
521 debug!("commit_expr {:?}", e);
522 if let ExprKind::Path(..) = e.node {
523 // might be unit-struct construction; check for recoverableinput error.
524 let expected = edible.iter()
526 .chain(inedible.iter().cloned())
527 .collect::<Vec<_>>();
528 self.check_for_erroneous_unit_struct_expecting(&expected[..]);
530 self.expect_one_of(edible, inedible)
533 pub fn commit_expr_expecting(&mut self, e: &Expr, edible: token::Token) -> PResult<'a, ()> {
534 self.commit_expr(e, &[edible], &[])
537 /// Commit to parsing a complete statement `s`, which expects to be
538 /// followed by some token from the set edible + inedible. Check
539 /// for recoverable input errors, discarding erroneous characters.
540 pub fn commit_stmt(&mut self, edible: &[token::Token],
541 inedible: &[token::Token]) -> PResult<'a, ()> {
544 .map_or(false, |t| t.is_ident() || t.is_path()) {
545 let expected = edible.iter()
547 .chain(inedible.iter().cloned())
548 .collect::<Vec<_>>();
549 self.check_for_erroneous_unit_struct_expecting(&expected);
551 self.expect_one_of(edible, inedible)
554 pub fn commit_stmt_expecting(&mut self, edible: token::Token) -> PResult<'a, ()> {
555 self.commit_stmt(&[edible], &[])
558 /// returns the span of expr, if it was not interpolated or the span of the interpolated token
559 fn interpolated_or_expr_span(&self,
560 expr: PResult<'a, P<Expr>>)
561 -> PResult<'a, (Span, P<Expr>)> {
563 if self.last_token_interpolated {
571 pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
572 self.check_strict_keywords();
573 self.check_reserved_keywords();
579 token::Interpolated(token::NtIdent(..)) => {
580 self.bug("ident interpolation not converted to real token");
583 let mut err = self.fatal(&format!("expected identifier, found `{}`",
584 self.this_token_to_string()));
585 if self.token == token::Underscore {
586 err.note("`_` is a wildcard pattern, not an identifier");
593 fn parse_ident_into_path(&mut self) -> PResult<'a, ast::Path> {
594 let ident = self.parse_ident()?;
595 Ok(ast::Path::from_ident(self.last_span, ident))
598 /// Check if the next token is `tok`, and return `true` if so.
600 /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
602 pub fn check(&mut self, tok: &token::Token) -> bool {
603 let is_present = self.token == *tok;
604 if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
608 /// Consume token 'tok' if it exists. Returns true if the given
609 /// token was present, false otherwise.
610 pub fn eat(&mut self, tok: &token::Token) -> bool {
611 let is_present = self.check(tok);
612 if is_present { self.bump() }
616 pub fn check_keyword(&mut self, kw: keywords::Keyword) -> bool {
617 self.expected_tokens.push(TokenType::Keyword(kw));
618 self.token.is_keyword(kw)
621 /// If the next token is the given keyword, eat it and return
622 /// true. Otherwise, return false.
623 pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
624 if self.check_keyword(kw) {
632 pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool {
633 if self.token.is_keyword(kw) {
641 pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
642 self.expected_tokens.push(TokenType::Token(token::Ident(ident)));
643 if let token::Ident(ref cur_ident) = self.token {
644 cur_ident.name == ident.name
650 pub fn eat_contextual_keyword(&mut self, ident: Ident) -> bool {
651 if self.check_contextual_keyword(ident) {
659 /// If the given word is not a keyword, signal an error.
660 /// If the next token is not the given word, signal an error.
661 /// Otherwise, eat it.
662 pub fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> {
663 if !self.eat_keyword(kw) {
670 /// Signal an error if the given string is a strict keyword
671 pub fn check_strict_keywords(&mut self) {
672 if self.token.is_strict_keyword() {
673 let token_str = self.this_token_to_string();
674 let span = self.span;
676 &format!("expected identifier, found keyword `{}`",
681 /// Signal an error if the current token is a reserved keyword
682 pub fn check_reserved_keywords(&mut self) {
683 if self.token.is_reserved_keyword() {
684 let token_str = self.this_token_to_string();
685 self.fatal(&format!("`{}` is a reserved keyword", token_str)).emit()
689 /// Expect and consume an `&`. If `&&` is seen, replace it with a single
690 /// `&` and continue. If an `&` is not seen, signal an error.
691 fn expect_and(&mut self) -> PResult<'a, ()> {
692 self.expected_tokens.push(TokenType::Token(token::BinOp(token::And)));
694 token::BinOp(token::And) => {
699 let span = self.span;
700 let lo = span.lo + BytePos(1);
701 Ok(self.bump_with(token::BinOp(token::And), lo, span.hi))
703 _ => self.unexpected()
707 pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
709 None => {/* everything ok */}
711 let text = suf.as_str();
713 self.span_bug(sp, "found empty literal suffix in Some")
715 self.span_err(sp, &format!("{} with a suffix is invalid", kind));
720 /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
721 /// `<` and continue. If a `<` is not seen, return false.
723 /// This is meant to be used when parsing generics on a path to get the
725 fn eat_lt(&mut self) -> bool {
726 self.expected_tokens.push(TokenType::Token(token::Lt));
732 token::BinOp(token::Shl) => {
733 let span = self.span;
734 let lo = span.lo + BytePos(1);
735 self.bump_with(token::Lt, lo, span.hi);
742 fn expect_lt(&mut self) -> PResult<'a, ()> {
750 /// Expect and consume a GT. if a >> is seen, replace it
751 /// with a single > and continue. If a GT is not seen,
753 pub fn expect_gt(&mut self) -> PResult<'a, ()> {
754 self.expected_tokens.push(TokenType::Token(token::Gt));
760 token::BinOp(token::Shr) => {
761 let span = self.span;
762 let lo = span.lo + BytePos(1);
763 Ok(self.bump_with(token::Gt, lo, span.hi))
765 token::BinOpEq(token::Shr) => {
766 let span = self.span;
767 let lo = span.lo + BytePos(1);
768 Ok(self.bump_with(token::Ge, lo, span.hi))
771 let span = self.span;
772 let lo = span.lo + BytePos(1);
773 Ok(self.bump_with(token::Eq, lo, span.hi))
776 let gt_str = Parser::token_to_string(&token::Gt);
777 let this_token_str = self.this_token_to_string();
778 Err(self.fatal(&format!("expected `{}`, found `{}`",
785 pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
786 sep: Option<token::Token>,
788 -> PResult<'a, (P<[T]>, bool)>
789 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
791 let mut v = Vec::new();
792 // This loop works by alternating back and forth between parsing types
793 // and commas. For example, given a string `A, B,>`, the parser would
794 // first parse `A`, then a comma, then `B`, then a comma. After that it
795 // would encounter a `>` and stop. This lets the parser handle trailing
796 // commas in generic parameters, because it can stop either after
797 // parsing a type or after parsing a comma.
799 if self.check(&token::Gt)
800 || self.token == token::BinOp(token::Shr)
801 || self.token == token::Ge
802 || self.token == token::BinOpEq(token::Shr) {
808 Some(result) => v.push(result),
809 None => return Ok((P::from_vec(v), true))
812 if let Some(t) = sep.as_ref() {
818 return Ok((P::from_vec(v), false));
821 /// Parse a sequence bracketed by '<' and '>', stopping
823 pub fn parse_seq_to_before_gt<T, F>(&mut self,
824 sep: Option<token::Token>,
826 -> PResult<'a, P<[T]>> where
827 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
829 let (result, returned) = self.parse_seq_to_before_gt_or_return(sep,
830 |p| Ok(Some(f(p)?)))?;
835 pub fn parse_seq_to_gt<T, F>(&mut self,
836 sep: Option<token::Token>,
838 -> PResult<'a, P<[T]>> where
839 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
841 let v = self.parse_seq_to_before_gt(sep, f)?;
846 pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
847 sep: Option<token::Token>,
849 -> PResult<'a, (P<[T]>, bool)> where
850 F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
852 let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?;
856 return Ok((v, returned));
859 /// Eat and discard tokens until one of `kets` is encountered. Respects token trees,
860 /// passes through any errors encountered. Used for error recovery.
861 pub fn eat_to_tokens(&mut self, kets: &[&token::Token]) {
862 self.parse_seq_to_before_tokens(kets,
864 |p| p.parse_token_tree(),
868 /// Parse a sequence, including the closing delimiter. The function
869 /// f must consume tokens until reaching the next separator or
871 pub fn parse_seq_to_end<T, F>(&mut self,
875 -> PResult<'a, Vec<T>> where
876 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
878 let val = self.parse_seq_to_before_end(ket, sep, f);
883 /// Parse a sequence, not including the closing delimiter. The function
884 /// f must consume tokens until reaching the next separator or
886 pub fn parse_seq_to_before_end<T, F>(&mut self,
891 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>
893 self.parse_seq_to_before_tokens(&[ket], sep, f, |mut e| e.emit())
896 // `fe` is an error handler.
897 fn parse_seq_to_before_tokens<T, F, Fe>(&mut self,
898 kets: &[&token::Token],
903 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
904 Fe: FnMut(DiagnosticBuilder)
906 let mut first: bool = true;
908 while !kets.contains(&&self.token) {
914 if let Err(e) = self.expect(t) {
922 if sep.trailing_sep_allowed && kets.iter().any(|k| self.check(k)) {
938 /// Parse a sequence, including the closing delimiter. The function
939 /// f must consume tokens until reaching the next separator or
941 pub fn parse_unspanned_seq<T, F>(&mut self,
946 -> PResult<'a, Vec<T>> where
947 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
950 let result = self.parse_seq_to_before_end(ket, sep, f);
951 if self.token == *ket {
957 // NB: Do not use this function unless you actually plan to place the
958 // spanned list in the AST.
959 pub fn parse_seq<T, F>(&mut self,
964 -> PResult<'a, Spanned<Vec<T>>> where
965 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
967 let lo = self.span.lo;
969 let result = self.parse_seq_to_before_end(ket, sep, f);
970 let hi = self.span.hi;
972 Ok(spanned(lo, hi, result))
975 /// Advance the parser by one token
976 pub fn bump(&mut self) {
977 if self.last_token_eof {
978 // Bumping after EOF is a bad sign, usually an infinite loop.
979 self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
982 if self.token == token::Eof {
983 self.last_token_eof = true;
986 self.last_span = self.span;
987 // Stash token for error recovery (sometimes; clone is not necessarily cheap).
988 self.last_token = if self.token.is_ident() ||
989 self.token.is_path() ||
990 self.token == token::Comma {
991 Some(Box::new(self.token.clone()))
995 self.last_token_interpolated = self.token.is_interpolated();
996 let next = if self.buffer_start == self.buffer_end {
997 self.reader.real_token()
999 // Avoid token copies with `replace`.
1000 let buffer_start = self.buffer_start as usize;
1001 let next_index = (buffer_start + 1) & 3;
1002 self.buffer_start = next_index as isize;
1004 let placeholder = TokenAndSpan {
1005 tok: token::Underscore,
1008 mem::replace(&mut self.buffer[buffer_start], placeholder)
1010 self.span = next.sp;
1011 self.token = next.tok;
1012 self.tokens_consumed += 1;
1013 self.expected_tokens.clear();
1014 // check after each token
1015 self.check_unknown_macro_variable();
1018 /// Advance the parser by one token and return the bumped token.
1019 pub fn bump_and_get(&mut self) -> token::Token {
1020 let old_token = mem::replace(&mut self.token, token::Underscore);
1025 /// Advance the parser using provided token as a next one. Use this when
1026 /// consuming a part of a token. For example a single `<` from `<<`.
1027 pub fn bump_with(&mut self,
1031 self.last_span = mk_sp(self.span.lo, lo);
1032 // It would be incorrect to just stash current token, but fortunately
1033 // for tokens currently using `bump_with`, last_token will be of no
1035 self.last_token = None;
1036 self.last_token_interpolated = false;
1037 self.span = mk_sp(lo, hi);
1039 self.expected_tokens.clear();
1042 pub fn buffer_length(&mut self) -> isize {
1043 if self.buffer_start <= self.buffer_end {
1044 return self.buffer_end - self.buffer_start;
1046 return (4 - self.buffer_start) + self.buffer_end;
1048 pub fn look_ahead<R, F>(&mut self, distance: usize, f: F) -> R where
1049 F: FnOnce(&token::Token) -> R,
1051 let dist = distance as isize;
1052 while self.buffer_length() < dist {
1053 self.buffer[self.buffer_end as usize] = self.reader.real_token();
1054 self.buffer_end = (self.buffer_end + 1) & 3;
1056 f(&self.buffer[((self.buffer_start + dist - 1) & 3) as usize].tok)
1058 pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> {
1059 self.sess.span_diagnostic.struct_span_fatal(self.span, m)
1061 pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
1062 self.sess.span_diagnostic.struct_span_fatal(sp, m)
1064 pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
1065 let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
1069 pub fn bug(&self, m: &str) -> ! {
1070 self.sess.span_diagnostic.span_bug(self.span, m)
1072 pub fn warn(&self, m: &str) {
1073 self.sess.span_diagnostic.span_warn(self.span, m)
1075 pub fn span_warn(&self, sp: Span, m: &str) {
1076 self.sess.span_diagnostic.span_warn(sp, m)
1078 pub fn span_err(&self, sp: Span, m: &str) {
1079 self.sess.span_diagnostic.span_err(sp, m)
1081 pub fn span_bug(&self, sp: Span, m: &str) -> ! {
1082 self.sess.span_diagnostic.span_bug(sp, m)
1084 pub fn abort_if_errors(&self) {
1085 self.sess.span_diagnostic.abort_if_errors();
1088 pub fn diagnostic(&self) -> &'a errors::Handler {
1089 &self.sess.span_diagnostic
1092 pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString {
1096 /// Is the current token one of the keywords that signals a bare function
1098 pub fn token_is_bare_fn_keyword(&mut self) -> bool {
1099 self.check_keyword(keywords::Fn) ||
1100 self.check_keyword(keywords::Unsafe) ||
1101 self.check_keyword(keywords::Extern)
1104 pub fn get_lifetime(&mut self) -> ast::Ident {
1106 token::Lifetime(ref ident) => *ident,
1107 _ => self.bug("not a lifetime"),
1111 pub fn parse_for_in_type(&mut self) -> PResult<'a, TyKind> {
1113 Parses whatever can come after a `for` keyword in a type.
1114 The `for` has already been consumed.
1118 - for <'lt> |S| -> T
1122 - for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
1123 - for <'lt> path::foo(a, b)
1128 let lo = self.span.lo;
1130 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
1132 // examine next token to decide to do
1133 if self.token_is_bare_fn_keyword() {
1134 self.parse_ty_bare_fn(lifetime_defs)
1136 let hi = self.span.hi;
1137 let trait_ref = self.parse_trait_ref()?;
1138 let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
1139 trait_ref: trait_ref,
1140 span: mk_sp(lo, hi)};
1141 let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
1142 self.parse_ty_param_bounds(BoundParsingMode::Bare)?
1147 Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
1148 .chain(other_bounds.into_vec())
1150 Ok(ast::TyKind::PolyTraitRef(all_bounds))
1154 pub fn parse_ty_path(&mut self) -> PResult<'a, TyKind> {
1155 Ok(TyKind::Path(None, self.parse_path(PathStyle::Type)?))
1158 /// parse a TyKind::BareFn type:
1159 pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>)
1160 -> PResult<'a, TyKind> {
1163 [unsafe] [extern "ABI"] fn (S) -> T
1173 let unsafety = self.parse_unsafety()?;
1174 let abi = if self.eat_keyword(keywords::Extern) {
1175 self.parse_opt_abi()?.unwrap_or(Abi::C)
1180 self.expect_keyword(keywords::Fn)?;
1181 let (inputs, variadic) = self.parse_fn_args(false, true)?;
1182 let ret_ty = self.parse_ret_ty()?;
1183 let decl = P(FnDecl {
1188 Ok(TyKind::BareFn(P(BareFnTy {
1191 lifetimes: lifetime_defs,
1196 /// Parses an obsolete closure kind (`&:`, `&mut:`, or `:`).
1197 pub fn parse_obsolete_closure_kind(&mut self) -> PResult<'a, ()> {
1198 let lo = self.span.lo;
1200 self.check(&token::BinOp(token::And)) &&
1201 self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
1202 self.look_ahead(2, |t| *t == token::Colon)
1208 self.token == token::BinOp(token::And) &&
1209 self.look_ahead(1, |t| *t == token::Colon)
1214 self.eat(&token::Colon)
1221 let span = mk_sp(lo, self.span.hi);
1222 self.obsolete(span, ObsoleteSyntax::ClosureKind);
1226 pub fn parse_unsafety(&mut self) -> PResult<'a, Unsafety> {
1227 if self.eat_keyword(keywords::Unsafe) {
1228 return Ok(Unsafety::Unsafe);
1230 return Ok(Unsafety::Normal);
1234 /// Parse the items in a trait declaration
1235 pub fn parse_trait_items(&mut self) -> PResult<'a, Vec<TraitItem>> {
1236 self.parse_unspanned_seq(
1237 &token::OpenDelim(token::Brace),
1238 &token::CloseDelim(token::Brace),
1240 |p| -> PResult<'a, TraitItem> {
1241 maybe_whole!(no_clone_from_p p, NtTraitItem);
1242 let mut attrs = p.parse_outer_attributes()?;
1245 let (name, node) = if p.eat_keyword(keywords::Type) {
1246 let TyParam {ident, bounds, default, ..} = p.parse_ty_param()?;
1247 p.expect(&token::Semi)?;
1248 (ident, TraitItemKind::Type(bounds, default))
1249 } else if p.is_const_item() {
1250 p.expect_keyword(keywords::Const)?;
1251 let ident = p.parse_ident()?;
1252 p.expect(&token::Colon)?;
1253 let ty = p.parse_ty_sum()?;
1254 let default = if p.check(&token::Eq) {
1256 let expr = p.parse_expr()?;
1257 p.commit_expr_expecting(&expr, token::Semi)?;
1260 p.expect(&token::Semi)?;
1263 (ident, TraitItemKind::Const(ty, default))
1265 let (constness, unsafety, abi) = match p.parse_fn_front_matter() {
1270 token::Eof => break,
1272 token::CloseDelim(token::Brace) |
1278 token::OpenDelim(token::Brace) => {
1279 p.parse_token_tree()?;
1291 let ident = p.parse_ident()?;
1292 let mut generics = p.parse_generics()?;
1294 let d = p.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
1295 // This is somewhat dubious; We don't want to allow
1296 // argument names to be left off if there is a
1298 p.parse_arg_general(false)
1301 generics.where_clause = p.parse_where_clause()?;
1302 let sig = ast::MethodSig {
1304 constness: constness,
1310 let body = match p.token {
1313 debug!("parse_trait_methods(): parsing required method");
1316 token::OpenDelim(token::Brace) => {
1317 debug!("parse_trait_methods(): parsing provided method");
1318 let (inner_attrs, body) =
1319 p.parse_inner_attrs_and_block()?;
1320 attrs.extend(inner_attrs.iter().cloned());
1325 let token_str = p.this_token_to_string();
1326 return Err(p.fatal(&format!("expected `;` or `{{`, found `{}`",
1330 (ident, ast::TraitItemKind::Method(sig, body))
1334 id: ast::DUMMY_NODE_ID,
1338 span: mk_sp(lo, p.last_span.hi),
1343 /// Parse a possibly mutable type
1344 pub fn parse_mt(&mut self) -> PResult<'a, MutTy> {
1345 let mutbl = self.parse_mutability()?;
1346 let t = self.parse_ty()?;
1347 Ok(MutTy { ty: t, mutbl: mutbl })
1350 /// Parse optional return type [ -> TY ] in function decl
1351 pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
1352 if self.eat(&token::RArrow) {
1353 if self.eat(&token::Not) {
1354 Ok(FunctionRetTy::None(self.last_span))
1356 Ok(FunctionRetTy::Ty(self.parse_ty()?))
1359 let pos = self.span.lo;
1360 Ok(FunctionRetTy::Default(mk_sp(pos, pos)))
1364 /// Parse a type in a context where `T1+T2` is allowed.
1365 pub fn parse_ty_sum(&mut self) -> PResult<'a, P<Ty>> {
1366 let lo = self.span.lo;
1367 let lhs = self.parse_ty()?;
1369 if !self.eat(&token::BinOp(token::Plus)) {
1373 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare)?;
1375 // In type grammar, `+` is treated like a binary operator,
1376 // and hence both L and R side are required.
1377 if bounds.is_empty() {
1378 let last_span = self.last_span;
1379 self.span_err(last_span,
1380 "at least one type parameter bound \
1381 must be specified");
1384 let sp = mk_sp(lo, self.last_span.hi);
1385 let sum = ast::TyKind::ObjectSum(lhs, bounds);
1386 Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp}))
1390 pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
1391 maybe_whole!(no_clone self, NtTy);
1393 let lo = self.span.lo;
1395 let t = if self.check(&token::OpenDelim(token::Paren)) {
1398 // (t) is a parenthesized ty
1399 // (t,) is the type of a tuple with only one field,
1401 let mut ts = vec![];
1402 let mut last_comma = false;
1403 while self.token != token::CloseDelim(token::Paren) {
1404 ts.push(self.parse_ty_sum()?);
1405 if self.check(&token::Comma) {
1414 self.expect(&token::CloseDelim(token::Paren))?;
1415 if ts.len() == 1 && !last_comma {
1416 TyKind::Paren(ts.into_iter().nth(0).unwrap())
1420 } else if self.check(&token::BinOp(token::Star)) {
1421 // STAR POINTER (bare pointer?)
1423 TyKind::Ptr(self.parse_ptr()?)
1424 } else if self.check(&token::OpenDelim(token::Bracket)) {
1426 self.expect(&token::OpenDelim(token::Bracket))?;
1427 let t = self.parse_ty_sum()?;
1429 // Parse the `; e` in `[ i32; e ]`
1430 // where `e` is a const expression
1431 let t = match self.maybe_parse_fixed_length_of_vec()? {
1432 None => TyKind::Vec(t),
1433 Some(suffix) => TyKind::FixedLengthVec(t, suffix)
1435 self.expect(&token::CloseDelim(token::Bracket))?;
1437 } else if self.check(&token::BinOp(token::And)) ||
1438 self.token == token::AndAnd {
1441 self.parse_borrowed_pointee()?
1442 } else if self.check_keyword(keywords::For) {
1443 self.parse_for_in_type()?
1444 } else if self.token_is_bare_fn_keyword() {
1446 self.parse_ty_bare_fn(Vec::new())?
1447 } else if self.eat_keyword_noexpect(keywords::Typeof) {
1449 // In order to not be ambiguous, the type must be surrounded by parens.
1450 self.expect(&token::OpenDelim(token::Paren))?;
1451 let e = self.parse_expr()?;
1452 self.expect(&token::CloseDelim(token::Paren))?;
1454 } else if self.eat_lt() {
1457 self.parse_qualified_path(PathStyle::Type)?;
1459 TyKind::Path(Some(qself), path)
1460 } else if self.token.is_path_start() {
1461 let path = self.parse_path(PathStyle::Type)?;
1462 if self.check(&token::Not) {
1465 let delim = self.expect_open_delim()?;
1466 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
1468 |p| p.parse_token_tree())?;
1469 let hi = self.span.hi;
1470 TyKind::Mac(spanned(lo, hi, Mac_ { path: path, tts: tts, ctxt: EMPTY_CTXT }))
1473 TyKind::Path(None, path)
1475 } else if self.eat(&token::Underscore) {
1476 // TYPE TO BE INFERRED
1479 let msg = format!("expected type, found {}", self.this_token_descr());
1480 return Err(self.fatal(&msg));
1483 let sp = mk_sp(lo, self.last_span.hi);
1484 Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp}))
1487 pub fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
1488 // look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
1489 let opt_lifetime = self.parse_opt_lifetime()?;
1491 let mt = self.parse_mt()?;
1492 return Ok(TyKind::Rptr(opt_lifetime, mt));
1495 pub fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
1496 let mutbl = if self.eat_keyword(keywords::Mut) {
1498 } else if self.eat_keyword(keywords::Const) {
1499 Mutability::Immutable
1501 let span = self.last_span;
1503 "expected mut or const in raw pointer type (use \
1504 `*mut T` or `*const T` as appropriate)");
1505 Mutability::Immutable
1507 let t = self.parse_ty()?;
1508 Ok(MutTy { ty: t, mutbl: mutbl })
1511 pub fn is_named_argument(&mut self) -> bool {
1512 let offset = match self.token {
1513 token::BinOp(token::And) => 1,
1515 _ if self.token.is_keyword(keywords::Mut) => 1,
1519 debug!("parser is_named_argument offset:{}", offset);
1522 is_ident_or_underscore(&self.token)
1523 && self.look_ahead(1, |t| *t == token::Colon)
1525 self.look_ahead(offset, |t| is_ident_or_underscore(t))
1526 && self.look_ahead(offset + 1, |t| *t == token::Colon)
1530 /// This version of parse arg doesn't necessarily require
1531 /// identifier names.
1532 pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
1533 maybe_whole!(no_clone self, NtArg);
1535 let pat = if require_name || self.is_named_argument() {
1536 debug!("parse_arg_general parse_pat (require_name:{})",
1538 let pat = self.parse_pat()?;
1540 self.expect(&token::Colon)?;
1543 debug!("parse_arg_general ident_to_pat");
1544 let sp = self.last_span;
1545 let spanned = Spanned { span: sp, node: keywords::Invalid.ident() };
1547 id: ast::DUMMY_NODE_ID,
1548 node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable),
1554 let t = self.parse_ty_sum()?;
1559 id: ast::DUMMY_NODE_ID,
1563 /// Parse a single function argument
1564 pub fn parse_arg(&mut self) -> PResult<'a, Arg> {
1565 self.parse_arg_general(true)
1568 /// Parse an argument in a lambda header e.g. |arg, arg|
1569 pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
1570 let pat = self.parse_pat()?;
1571 let t = if self.eat(&token::Colon) {
1572 self.parse_ty_sum()?
1575 id: ast::DUMMY_NODE_ID,
1576 node: TyKind::Infer,
1577 span: mk_sp(self.span.lo, self.span.hi),
1583 id: ast::DUMMY_NODE_ID
1587 pub fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
1588 if self.check(&token::Semi) {
1590 Ok(Some(self.parse_expr()?))
1596 /// Matches token_lit = LIT_INTEGER | ...
1597 pub fn parse_lit_token(&mut self) -> PResult<'a, LitKind> {
1598 let out = match self.token {
1599 token::Interpolated(token::NtExpr(ref v)) => {
1601 ExprKind::Lit(ref lit) => { lit.node.clone() }
1602 _ => { return self.unexpected_last(&self.token); }
1605 token::Literal(lit, suf) => {
1606 let (suffix_illegal, out) = match lit {
1607 token::Byte(i) => (true, LitKind::Byte(parse::byte_lit(&i.as_str()).0)),
1608 token::Char(i) => (true, LitKind::Char(parse::char_lit(&i.as_str()).0)),
1610 // there are some valid suffixes for integer and
1611 // float literals, so all the handling is done
1613 token::Integer(s) => {
1614 (false, parse::integer_lit(&s.as_str(),
1615 suf.as_ref().map(|s| s.as_str()),
1616 &self.sess.span_diagnostic,
1619 token::Float(s) => {
1620 (false, parse::float_lit(&s.as_str(),
1621 suf.as_ref().map(|s| s.as_str()),
1622 &self.sess.span_diagnostic,
1628 LitKind::Str(token::intern_and_get_ident(&parse::str_lit(&s.as_str())),
1629 ast::StrStyle::Cooked))
1631 token::StrRaw(s, n) => {
1634 token::intern_and_get_ident(&parse::raw_str_lit(&s.as_str())),
1635 ast::StrStyle::Raw(n)))
1637 token::ByteStr(i) =>
1638 (true, LitKind::ByteStr(parse::byte_str_lit(&i.as_str()))),
1639 token::ByteStrRaw(i, _) =>
1641 LitKind::ByteStr(Rc::new(i.to_string().into_bytes()))),
1646 self.expect_no_suffix(sp, &format!("{} literal", lit.short_name()), suf)
1651 _ => { return self.unexpected_last(&self.token); }
1658 /// Matches lit = true | false | token_lit
1659 pub fn parse_lit(&mut self) -> PResult<'a, Lit> {
1660 let lo = self.span.lo;
1661 let lit = if self.eat_keyword(keywords::True) {
1663 } else if self.eat_keyword(keywords::False) {
1664 LitKind::Bool(false)
1666 let lit = self.parse_lit_token()?;
1669 Ok(codemap::Spanned { node: lit, span: mk_sp(lo, self.last_span.hi) })
1672 /// matches '-' lit | lit
1673 pub fn parse_pat_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
1674 let minus_lo = self.span.lo;
1675 let minus_present = self.eat(&token::BinOp(token::Minus));
1676 let lo = self.span.lo;
1677 let literal = P(self.parse_lit()?);
1678 let hi = self.last_span.hi;
1679 let expr = self.mk_expr(lo, hi, ExprKind::Lit(literal), None);
1682 let minus_hi = self.last_span.hi;
1683 let unary = self.mk_unary(UnOp::Neg, expr);
1684 Ok(self.mk_expr(minus_lo, minus_hi, unary, None))
1690 pub fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> {
1692 token::Ident(sid) if self.token.is_path_segment_keyword() => {
1696 _ => self.parse_ident(),
1700 /// Parses qualified path.
1702 /// Assumes that the leading `<` has been parsed already.
1704 /// Qualifed paths are a part of the universal function call
1707 /// `qualified_path = <type [as trait_ref]>::path`
1709 /// See `parse_path` for `mode` meaning.
1714 /// `<T as U>::F::a::<S>`
1715 pub fn parse_qualified_path(&mut self, mode: PathStyle)
1716 -> PResult<'a, (QSelf, ast::Path)> {
1717 let span = self.last_span;
1718 let self_type = self.parse_ty_sum()?;
1719 let mut path = if self.eat_keyword(keywords::As) {
1720 self.parse_path(PathStyle::Type)?
1731 position: path.segments.len()
1734 self.expect(&token::Gt)?;
1735 self.expect(&token::ModSep)?;
1737 let segments = match mode {
1738 PathStyle::Type => {
1739 self.parse_path_segments_without_colons()?
1741 PathStyle::Expr => {
1742 self.parse_path_segments_with_colons()?
1745 self.parse_path_segments_without_types()?
1748 path.segments.extend(segments);
1750 path.span.hi = self.last_span.hi;
1755 /// Parses a path and optional type parameter bounds, depending on the
1756 /// mode. The `mode` parameter determines whether lifetimes, types, and/or
1757 /// bounds are permitted and whether `::` must precede type parameter
1759 pub fn parse_path(&mut self, mode: PathStyle) -> PResult<'a, ast::Path> {
1760 // Check for a whole path...
1761 let found = match self.token {
1762 token::Interpolated(token::NtPath(_)) => Some(self.bump_and_get()),
1765 if let Some(token::Interpolated(token::NtPath(path))) = found {
1769 let lo = self.span.lo;
1770 let is_global = self.eat(&token::ModSep);
1772 // Parse any number of segments and bound sets. A segment is an
1773 // identifier followed by an optional lifetime and a set of types.
1774 // A bound set is a set of type parameter bounds.
1775 let segments = match mode {
1776 PathStyle::Type => {
1777 self.parse_path_segments_without_colons()?
1779 PathStyle::Expr => {
1780 self.parse_path_segments_with_colons()?
1783 self.parse_path_segments_without_types()?
1787 // Assemble the span.
1788 let span = mk_sp(lo, self.last_span.hi);
1790 // Assemble the result.
1799 /// - `a::b<T,U>::c<V,W>`
1800 /// - `a::b<T,U>::c(V) -> W`
1801 /// - `a::b<T,U>::c(V)`
1802 pub fn parse_path_segments_without_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
1803 let mut segments = Vec::new();
1805 // First, parse an identifier.
1806 let identifier = self.parse_path_segment_ident()?;
1808 // Parse types, optionally.
1809 let parameters = if self.eat_lt() {
1810 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?;
1812 ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
1813 lifetimes: lifetimes,
1814 types: P::from_vec(types),
1815 bindings: P::from_vec(bindings),
1817 } else if self.eat(&token::OpenDelim(token::Paren)) {
1818 let lo = self.last_span.lo;
1820 let inputs = self.parse_seq_to_end(
1821 &token::CloseDelim(token::Paren),
1822 SeqSep::trailing_allowed(token::Comma),
1823 |p| p.parse_ty_sum())?;
1825 let output_ty = if self.eat(&token::RArrow) {
1826 Some(self.parse_ty()?)
1831 let hi = self.last_span.hi;
1833 ast::PathParameters::Parenthesized(ast::ParenthesizedParameterData {
1834 span: mk_sp(lo, hi),
1839 ast::PathParameters::none()
1842 // Assemble and push the result.
1843 segments.push(ast::PathSegment { identifier: identifier,
1844 parameters: parameters });
1846 // Continue only if we see a `::`
1847 if !self.eat(&token::ModSep) {
1848 return Ok(segments);
1854 /// - `a::b::<T,U>::c`
1855 pub fn parse_path_segments_with_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
1856 let mut segments = Vec::new();
1858 // First, parse an identifier.
1859 let identifier = self.parse_path_segment_ident()?;
1861 // If we do not see a `::`, stop.
1862 if !self.eat(&token::ModSep) {
1863 segments.push(ast::PathSegment {
1864 identifier: identifier,
1865 parameters: ast::PathParameters::none()
1867 return Ok(segments);
1870 // Check for a type segment.
1872 // Consumed `a::b::<`, go look for types
1873 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?;
1874 let parameters = ast::AngleBracketedParameterData {
1875 lifetimes: lifetimes,
1876 types: P::from_vec(types),
1877 bindings: P::from_vec(bindings),
1879 segments.push(ast::PathSegment {
1880 identifier: identifier,
1881 parameters: ast::PathParameters::AngleBracketed(parameters),
1884 // Consumed `a::b::<T,U>`, check for `::` before proceeding
1885 if !self.eat(&token::ModSep) {
1886 return Ok(segments);
1889 // Consumed `a::`, go look for `b`
1890 segments.push(ast::PathSegment {
1891 identifier: identifier,
1892 parameters: ast::PathParameters::none(),
1900 pub fn parse_path_segments_without_types(&mut self)
1901 -> PResult<'a, Vec<ast::PathSegment>> {
1902 let mut segments = Vec::new();
1904 // First, parse an identifier.
1905 let identifier = self.parse_path_segment_ident()?;
1907 // Assemble and push the result.
1908 segments.push(ast::PathSegment {
1909 identifier: identifier,
1910 parameters: ast::PathParameters::none()
1913 // If we do not see a `::` or see `::{`/`::*`, stop.
1914 if !self.check(&token::ModSep) || self.is_import_coupler() {
1915 return Ok(segments);
1922 /// parses 0 or 1 lifetime
1923 pub fn parse_opt_lifetime(&mut self) -> PResult<'a, Option<ast::Lifetime>> {
1925 token::Lifetime(..) => {
1926 Ok(Some(self.parse_lifetime()?))
1934 /// Parses a single lifetime
1935 /// Matches lifetime = LIFETIME
1936 pub fn parse_lifetime(&mut self) -> PResult<'a, ast::Lifetime> {
1938 token::Lifetime(i) => {
1939 let span = self.span;
1941 return Ok(ast::Lifetime {
1942 id: ast::DUMMY_NODE_ID,
1948 return Err(self.fatal("expected a lifetime name"));
1953 /// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
1954 /// lifetime [':' lifetimes]`
1955 pub fn parse_lifetime_defs(&mut self) -> PResult<'a, Vec<ast::LifetimeDef>> {
1957 let mut res = Vec::new();
1960 token::Lifetime(_) => {
1961 let lifetime = self.parse_lifetime()?;
1963 if self.eat(&token::Colon) {
1964 self.parse_lifetimes(token::BinOp(token::Plus))?
1968 res.push(ast::LifetimeDef { lifetime: lifetime,
1978 token::Comma => { self.bump();}
1979 token::Gt => { return Ok(res); }
1980 token::BinOp(token::Shr) => { return Ok(res); }
1982 let this_token_str = self.this_token_to_string();
1983 let msg = format!("expected `,` or `>` after lifetime \
1986 return Err(self.fatal(&msg[..]));
1992 /// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
1993 /// one too, but putting that in there messes up the grammar....
1995 /// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
1996 /// either a comma or `>`. Used when parsing type parameter lists, where we expect something
1997 /// like `<'a, 'b, T>`.
1998 pub fn parse_lifetimes(&mut self, sep: token::Token) -> PResult<'a, Vec<ast::Lifetime>> {
2000 let mut res = Vec::new();
2003 token::Lifetime(_) => {
2004 res.push(self.parse_lifetime()?);
2011 if self.token != sep {
2019 /// Parse mutability (`mut` or nothing).
2020 pub fn parse_mutability(&mut self) -> PResult<'a, Mutability> {
2021 if self.eat_keyword(keywords::Mut) {
2022 Ok(Mutability::Mutable)
2024 Ok(Mutability::Immutable)
2028 /// Parse ident COLON expr
2029 pub fn parse_field(&mut self) -> PResult<'a, Field> {
2030 let lo = self.span.lo;
2031 let i = self.parse_ident()?;
2032 let hi = self.last_span.hi;
2033 self.expect(&token::Colon)?;
2034 let e = self.parse_expr()?;
2036 ident: spanned(lo, hi, i),
2037 span: mk_sp(lo, e.span.hi),
2042 pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos,
2043 node: ExprKind, attrs: ThinAttributes) -> P<Expr> {
2045 id: ast::DUMMY_NODE_ID,
2047 span: mk_sp(lo, hi),
2052 pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::ExprKind {
2053 ExprKind::Unary(unop, expr)
2056 pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2057 ExprKind::Binary(binop, lhs, rhs)
2060 pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::ExprKind {
2061 ExprKind::Call(f, args)
2064 fn mk_method_call(&mut self,
2065 ident: ast::SpannedIdent,
2069 ExprKind::MethodCall(ident, tps, args)
2072 pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::ExprKind {
2073 ExprKind::Index(expr, idx)
2076 pub fn mk_range(&mut self,
2077 start: Option<P<Expr>>,
2078 end: Option<P<Expr>>,
2079 limits: RangeLimits)
2080 -> PResult<'a, ast::ExprKind> {
2081 if end.is_none() && limits == RangeLimits::Closed {
2082 Err(self.span_fatal_help(self.span,
2083 "inclusive range with no end",
2084 "inclusive ranges must be bounded at the end \
2085 (`...b` or `a...b`)"))
2087 Ok(ExprKind::Range(start, end, limits))
2091 pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::ExprKind {
2092 ExprKind::Field(expr, ident)
2095 pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<usize>) -> ast::ExprKind {
2096 ExprKind::TupField(expr, idx)
2099 pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2100 lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2101 ExprKind::AssignOp(binop, lhs, rhs)
2104 pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos,
2105 m: Mac_, attrs: ThinAttributes) -> P<Expr> {
2107 id: ast::DUMMY_NODE_ID,
2108 node: ExprKind::Mac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
2109 span: mk_sp(lo, hi),
2114 pub fn mk_lit_u32(&mut self, i: u32, attrs: ThinAttributes) -> P<Expr> {
2115 let span = &self.span;
2116 let lv_lit = P(codemap::Spanned {
2117 node: LitKind::Int(i as u64, ast::LitIntType::Unsigned(UintTy::U32)),
2122 id: ast::DUMMY_NODE_ID,
2123 node: ExprKind::Lit(lv_lit),
2129 fn expect_open_delim(&mut self) -> PResult<'a, token::DelimToken> {
2130 self.expected_tokens.push(TokenType::Token(token::Gt));
2132 token::OpenDelim(delim) => {
2136 _ => Err(self.fatal("expected open delimiter")),
2140 /// At the bottom (top?) of the precedence hierarchy,
2141 /// parse things like parenthesized exprs,
2142 /// macros, return, etc.
2144 /// NB: This does not parse outer attributes,
2145 /// and is private because it only works
2146 /// correctly if called from parse_dot_or_call_expr().
2147 fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
2148 maybe_whole_expr!(self);
2150 // Outer attributes are already parsed and will be
2151 // added to the return value after the fact.
2153 // Therefore, prevent sub-parser from parsing
2154 // attributes by giving them a empty "already parsed" list.
2155 let mut attrs = None;
2157 let lo = self.span.lo;
2158 let mut hi = self.span.hi;
2162 // Note: when adding new syntax here, don't forget to adjust Token::can_begin_expr().
2164 token::OpenDelim(token::Paren) => {
2167 let attrs = self.parse_inner_attributes()?
2171 // (e) is parenthesized e
2172 // (e,) is a tuple with only one field, e
2173 let mut es = vec![];
2174 let mut trailing_comma = false;
2175 while self.token != token::CloseDelim(token::Paren) {
2176 es.push(self.parse_expr()?);
2177 self.commit_expr(&es.last().unwrap(), &[],
2178 &[token::Comma, token::CloseDelim(token::Paren)])?;
2179 if self.check(&token::Comma) {
2180 trailing_comma = true;
2184 trailing_comma = false;
2190 hi = self.last_span.hi;
2191 return if es.len() == 1 && !trailing_comma {
2192 Ok(self.mk_expr(lo, hi, ExprKind::Paren(es.into_iter().nth(0).unwrap()), attrs))
2194 Ok(self.mk_expr(lo, hi, ExprKind::Tup(es), attrs))
2197 token::OpenDelim(token::Brace) => {
2198 return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
2200 token::BinOp(token::Or) | token::OrOr => {
2201 let lo = self.span.lo;
2202 return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
2204 token::OpenDelim(token::Bracket) => {
2207 let inner_attrs = self.parse_inner_attributes()?
2209 attrs.update(|attrs| attrs.append(inner_attrs));
2211 if self.check(&token::CloseDelim(token::Bracket)) {
2214 ex = ExprKind::Vec(Vec::new());
2217 let first_expr = self.parse_expr()?;
2218 if self.check(&token::Semi) {
2219 // Repeating array syntax: [ 0; 512 ]
2221 let count = self.parse_expr()?;
2222 self.expect(&token::CloseDelim(token::Bracket))?;
2223 ex = ExprKind::Repeat(first_expr, count);
2224 } else if self.check(&token::Comma) {
2225 // Vector with two or more elements.
2227 let remaining_exprs = self.parse_seq_to_end(
2228 &token::CloseDelim(token::Bracket),
2229 SeqSep::trailing_allowed(token::Comma),
2230 |p| Ok(p.parse_expr()?)
2232 let mut exprs = vec!(first_expr);
2233 exprs.extend(remaining_exprs);
2234 ex = ExprKind::Vec(exprs);
2236 // Vector with one element.
2237 self.expect(&token::CloseDelim(token::Bracket))?;
2238 ex = ExprKind::Vec(vec!(first_expr));
2241 hi = self.last_span.hi;
2246 self.parse_qualified_path(PathStyle::Expr)?;
2248 return Ok(self.mk_expr(lo, hi, ExprKind::Path(Some(qself), path), attrs));
2250 if self.eat_keyword(keywords::Move) {
2251 let lo = self.last_span.lo;
2252 return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
2254 if self.eat_keyword(keywords::If) {
2255 return self.parse_if_expr(attrs);
2257 if self.eat_keyword(keywords::For) {
2258 let lo = self.last_span.lo;
2259 return self.parse_for_expr(None, lo, attrs);
2261 if self.eat_keyword(keywords::While) {
2262 let lo = self.last_span.lo;
2263 return self.parse_while_expr(None, lo, attrs);
2265 if self.token.is_lifetime() {
2266 let label = Spanned { node: self.get_lifetime(),
2268 let lo = self.span.lo;
2270 self.expect(&token::Colon)?;
2271 if self.eat_keyword(keywords::While) {
2272 return self.parse_while_expr(Some(label), lo, attrs)
2274 if self.eat_keyword(keywords::For) {
2275 return self.parse_for_expr(Some(label), lo, attrs)
2277 if self.eat_keyword(keywords::Loop) {
2278 return self.parse_loop_expr(Some(label), lo, attrs)
2280 return Err(self.fatal("expected `while`, `for`, or `loop` after a label"))
2282 if self.eat_keyword(keywords::Loop) {
2283 let lo = self.last_span.lo;
2284 return self.parse_loop_expr(None, lo, attrs);
2286 if self.eat_keyword(keywords::Continue) {
2287 let ex = if self.token.is_lifetime() {
2288 let ex = ExprKind::Again(Some(Spanned{
2289 node: self.get_lifetime(),
2295 ExprKind::Again(None)
2297 let hi = self.last_span.hi;
2298 return Ok(self.mk_expr(lo, hi, ex, attrs));
2300 if self.eat_keyword(keywords::Match) {
2301 return self.parse_match_expr(attrs);
2303 if self.eat_keyword(keywords::Unsafe) {
2304 return self.parse_block_expr(
2306 BlockCheckMode::Unsafe(ast::UserProvided),
2309 if self.eat_keyword(keywords::Return) {
2310 if self.token.can_begin_expr() {
2311 let e = self.parse_expr()?;
2313 ex = ExprKind::Ret(Some(e));
2315 ex = ExprKind::Ret(None);
2317 } else if self.eat_keyword(keywords::Break) {
2318 if self.token.is_lifetime() {
2319 ex = ExprKind::Break(Some(Spanned {
2320 node: self.get_lifetime(),
2325 ex = ExprKind::Break(None);
2327 hi = self.last_span.hi;
2328 } else if self.token.is_keyword(keywords::Let) {
2329 // Catch this syntax error here, instead of in `check_strict_keywords`, so
2330 // that we can explicitly mention that let is not to be used as an expression
2331 let mut db = self.fatal("expected expression, found statement (`let`)");
2332 db.note("variable declaration using `let` is a statement");
2334 } else if self.token.is_path_start() {
2335 let pth = self.parse_path(PathStyle::Expr)?;
2337 // `!`, as an operator, is prefix, so we know this isn't that
2338 if self.check(&token::Not) {
2339 // MACRO INVOCATION expression
2342 let delim = self.expect_open_delim()?;
2343 let tts = self.parse_seq_to_end(
2344 &token::CloseDelim(delim),
2346 |p| p.parse_token_tree())?;
2347 let hi = self.last_span.hi;
2349 return Ok(self.mk_mac_expr(lo,
2351 Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT },
2354 if self.check(&token::OpenDelim(token::Brace)) {
2355 // This is a struct literal, unless we're prohibited
2356 // from parsing struct literals here.
2357 let prohibited = self.restrictions.contains(
2358 Restrictions::RESTRICTION_NO_STRUCT_LITERAL
2361 // It's a struct literal.
2363 let mut fields = Vec::new();
2364 let mut base = None;
2366 let attrs = attrs.append(
2367 self.parse_inner_attributes()?
2368 .into_thin_attrs());
2370 while self.token != token::CloseDelim(token::Brace) {
2371 if self.eat(&token::DotDot) {
2372 match self.parse_expr() {
2378 self.recover_stmt();
2384 match self.parse_field() {
2385 Ok(f) => fields.push(f),
2388 self.recover_stmt();
2393 match self.commit_expr(&fields.last().unwrap().expr,
2395 &[token::CloseDelim(token::Brace)]) {
2399 self.recover_stmt();
2406 self.expect(&token::CloseDelim(token::Brace))?;
2407 ex = ExprKind::Struct(pth, fields, base);
2408 return Ok(self.mk_expr(lo, hi, ex, attrs));
2413 ex = ExprKind::Path(None, pth);
2415 match self.parse_lit() {
2418 ex = ExprKind::Lit(P(lit));
2422 let msg = format!("expected expression, found {}",
2423 self.this_token_descr());
2424 return Err(self.fatal(&msg));
2431 return Ok(self.mk_expr(lo, hi, ex, attrs));
2434 fn parse_or_use_outer_attributes(&mut self,
2435 already_parsed_attrs: Option<ThinAttributes>)
2436 -> PResult<'a, ThinAttributes> {
2437 if let Some(attrs) = already_parsed_attrs {
2440 self.parse_outer_attributes().map(|a| a.into_thin_attrs())
2444 /// Parse a block or unsafe block
2445 pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode,
2446 attrs: ThinAttributes)
2447 -> PResult<'a, P<Expr>> {
2449 let outer_attrs = attrs;
2450 self.expect(&token::OpenDelim(token::Brace))?;
2452 let inner_attrs = self.parse_inner_attributes()?.into_thin_attrs();
2453 let attrs = outer_attrs.append(inner_attrs);
2455 let blk = self.parse_block_tail(lo, blk_mode)?;
2456 return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), attrs));
2459 /// parse a.b or a(13) or a[4] or just a
2460 pub fn parse_dot_or_call_expr(&mut self,
2461 already_parsed_attrs: Option<ThinAttributes>)
2462 -> PResult<'a, P<Expr>> {
2463 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
2465 let b = self.parse_bottom_expr();
2466 let (span, b) = self.interpolated_or_expr_span(b)?;
2467 self.parse_dot_or_call_expr_with(b, span.lo, attrs)
2470 pub fn parse_dot_or_call_expr_with(&mut self,
2473 attrs: ThinAttributes)
2474 -> PResult<'a, P<Expr>> {
2475 // Stitch the list of outer attributes onto the return value.
2476 // A little bit ugly, but the best way given the current code
2478 self.parse_dot_or_call_expr_with_(e0, lo)
2480 expr.map(|mut expr| {
2481 expr.attrs.update(|a| a.prepend(attrs));
2483 ExprKind::If(..) | ExprKind::IfLet(..) => {
2484 if !expr.attrs.as_attr_slice().is_empty() {
2485 // Just point to the first attribute in there...
2486 let span = expr.attrs.as_attr_slice()[0].span;
2489 "attributes are not yet allowed on `if` \
2500 // Assuming we have just parsed `.foo` (i.e., a dot and an ident), continue
2501 // parsing into an expression.
2502 fn parse_dot_suffix(&mut self,
2505 self_value: P<Expr>,
2507 -> PResult<'a, P<Expr>> {
2508 let (_, tys, bindings) = if self.eat(&token::ModSep) {
2510 self.parse_generic_values_after_lt()?
2512 (Vec::new(), Vec::new(), Vec::new())
2515 if !bindings.is_empty() {
2516 let last_span = self.last_span;
2517 self.span_err(last_span, "type bindings are only permitted on trait paths");
2520 Ok(match self.token {
2521 // expr.f() method call.
2522 token::OpenDelim(token::Paren) => {
2523 let mut es = self.parse_unspanned_seq(
2524 &token::OpenDelim(token::Paren),
2525 &token::CloseDelim(token::Paren),
2526 SeqSep::trailing_allowed(token::Comma),
2527 |p| Ok(p.parse_expr()?)
2529 let hi = self.last_span.hi;
2531 es.insert(0, self_value);
2532 let id = spanned(ident_span.lo, ident_span.hi, ident);
2533 let nd = self.mk_method_call(id, tys, es);
2534 self.mk_expr(lo, hi, nd, None)
2538 if !tys.is_empty() {
2539 let last_span = self.last_span;
2540 self.span_err(last_span,
2541 "field expressions may not \
2542 have type parameters");
2545 let id = spanned(ident_span.lo, ident_span.hi, ident);
2546 let field = self.mk_field(self_value, id);
2547 self.mk_expr(lo, ident_span.hi, field, None)
2552 fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: BytePos) -> PResult<'a, P<Expr>> {
2557 while self.eat(&token::Question) {
2558 let hi = self.last_span.hi;
2559 e = self.mk_expr(lo, hi, ExprKind::Try(e), None);
2563 if self.eat(&token::Dot) {
2565 token::Ident(i) => {
2566 let dot_pos = self.last_span.hi;
2570 e = self.parse_dot_suffix(i, mk_sp(dot_pos, hi), e, lo)?;
2572 token::Literal(token::Integer(n), suf) => {
2575 // A tuple index may not have a suffix
2576 self.expect_no_suffix(sp, "tuple index", suf);
2578 let dot = self.last_span.hi;
2582 let index = n.as_str().parse::<usize>().ok();
2585 let id = spanned(dot, hi, n);
2586 let field = self.mk_tup_field(e, id);
2587 e = self.mk_expr(lo, hi, field, None);
2590 let last_span = self.last_span;
2591 self.span_err(last_span, "invalid tuple or tuple struct index");
2595 token::Literal(token::Float(n), _suf) => {
2597 let last_span = self.last_span;
2598 let fstr = n.as_str();
2599 let mut err = self.diagnostic().struct_span_err(last_span,
2600 &format!("unexpected token: `{}`", n.as_str()));
2601 if fstr.chars().all(|x| "0123456789.".contains(x)) {
2602 let float = match fstr.parse::<f64>().ok() {
2606 err.help(&format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
2607 float.trunc() as usize,
2608 format!(".{}", fstr.splitn(2, ".").last().unwrap())));
2614 // FIXME Could factor this out into non_fatal_unexpected or something.
2615 let actual = self.this_token_to_string();
2616 self.span_err(self.span, &format!("unexpected token: `{}`", actual));
2618 let dot_pos = self.last_span.hi;
2619 e = self.parse_dot_suffix(keywords::Invalid.ident(),
2620 mk_sp(dot_pos, dot_pos),
2626 if self.expr_is_complete(&e) { break; }
2629 token::OpenDelim(token::Paren) => {
2630 let es = self.parse_unspanned_seq(
2631 &token::OpenDelim(token::Paren),
2632 &token::CloseDelim(token::Paren),
2633 SeqSep::trailing_allowed(token::Comma),
2634 |p| Ok(p.parse_expr()?)
2636 hi = self.last_span.hi;
2638 let nd = self.mk_call(e, es);
2639 e = self.mk_expr(lo, hi, nd, None);
2643 // Could be either an index expression or a slicing expression.
2644 token::OpenDelim(token::Bracket) => {
2646 let ix = self.parse_expr()?;
2648 self.commit_expr_expecting(&ix, token::CloseDelim(token::Bracket))?;
2649 let index = self.mk_index(e, ix);
2650 e = self.mk_expr(lo, hi, index, None)
2658 // Parse unquoted tokens after a `$` in a token tree
2659 fn parse_unquoted(&mut self) -> PResult<'a, TokenTree> {
2660 let mut sp = self.span;
2661 let name = match self.token {
2665 if self.token == token::OpenDelim(token::Paren) {
2666 let Spanned { node: seq, span: seq_span } = self.parse_seq(
2667 &token::OpenDelim(token::Paren),
2668 &token::CloseDelim(token::Paren),
2670 |p| p.parse_token_tree()
2672 let (sep, repeat) = self.parse_sep_and_kleene_op()?;
2673 let name_num = macro_parser::count_names(&seq);
2674 return Ok(TokenTree::Sequence(mk_sp(sp.lo, seq_span.hi),
2675 Rc::new(SequenceRepetition {
2679 num_captures: name_num
2681 } else if self.token.is_keyword(keywords::Crate) {
2683 return Ok(TokenTree::Token(sp, SpecialVarNt(SpecialMacroVar::CrateMacroVar)));
2685 sp = mk_sp(sp.lo, self.span.hi);
2686 self.parse_ident().unwrap_or_else(|mut e| {
2688 keywords::Invalid.ident()
2692 token::SubstNt(name) => {
2698 // continue by trying to parse the `:ident` after `$name`
2699 if self.token == token::Colon &&
2700 self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword()) {
2702 sp = mk_sp(sp.lo, self.span.hi);
2703 let nt_kind = self.parse_ident()?;
2704 Ok(TokenTree::Token(sp, MatchNt(name, nt_kind)))
2706 Ok(TokenTree::Token(sp, SubstNt(name)))
2710 pub fn check_unknown_macro_variable(&mut self) {
2711 if self.quote_depth == 0 {
2713 token::SubstNt(name) =>
2714 self.fatal(&format!("unknown macro variable `{}`", name)).emit(),
2720 /// Parse an optional separator followed by a Kleene-style
2721 /// repetition token (+ or *).
2722 pub fn parse_sep_and_kleene_op(&mut self)
2723 -> PResult<'a, (Option<token::Token>, ast::KleeneOp)> {
2724 fn parse_kleene_op<'a>(parser: &mut Parser<'a>) -> PResult<'a, Option<ast::KleeneOp>> {
2725 match parser.token {
2726 token::BinOp(token::Star) => {
2728 Ok(Some(ast::KleeneOp::ZeroOrMore))
2730 token::BinOp(token::Plus) => {
2732 Ok(Some(ast::KleeneOp::OneOrMore))
2738 match parse_kleene_op(self)? {
2739 Some(kleene_op) => return Ok((None, kleene_op)),
2743 let separator = self.bump_and_get();
2744 match parse_kleene_op(self)? {
2745 Some(zerok) => Ok((Some(separator), zerok)),
2746 None => return Err(self.fatal("expected `*` or `+`"))
2750 /// parse a single token tree from the input.
2751 pub fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> {
2752 // FIXME #6994: currently, this is too eager. It
2753 // parses token trees but also identifies TokenType::Sequence's
2754 // and token::SubstNt's; it's too early to know yet
2755 // whether something will be a nonterminal or a seq
2757 maybe_whole!(deref self, NtTT);
2761 let mut err: DiagnosticBuilder<'a> =
2762 self.diagnostic().struct_span_err(self.span,
2763 "this file contains an un-closed delimiter");
2764 for &(_, sp) in &self.open_braces {
2765 err.span_help(sp, "did you mean to close this delimiter?");
2770 token::OpenDelim(delim) => {
2771 // The span for beginning of the delimited section
2772 let pre_span = self.span;
2774 // Parse the open delimiter.
2775 self.open_braces.push((delim, self.span));
2776 let open_span = self.span;
2779 // Parse the token trees within the delimiters.
2780 // We stop at any delimiter so we can try to recover if the user
2781 // uses an incorrect delimiter.
2782 let tts = self.parse_seq_to_before_tokens(&[&token::CloseDelim(token::Brace),
2783 &token::CloseDelim(token::Paren),
2784 &token::CloseDelim(token::Bracket)],
2786 |p| p.parse_token_tree(),
2789 let close_span = self.span;
2790 // Expand to cover the entire delimited token tree
2791 let span = Span { hi: close_span.hi, ..pre_span };
2794 // Correct delimiter.
2795 token::CloseDelim(d) if d == delim => {
2796 self.open_braces.pop().unwrap();
2798 // Parse the close delimiter.
2801 // Incorrect delimiter.
2802 token::CloseDelim(other) => {
2803 let token_str = self.this_token_to_string();
2804 let mut err = self.diagnostic().struct_span_err(self.span,
2805 &format!("incorrect close delimiter: `{}`", token_str));
2806 // This is a conservative error: only report the last unclosed delimiter.
2807 // The previous unclosed delimiters could actually be closed! The parser
2808 // just hasn't gotten to them yet.
2809 if let Some(&(_, sp)) = self.open_braces.last() {
2810 err.span_note(sp, "unclosed delimiter");
2814 self.open_braces.pop().unwrap();
2816 // If the incorrect delimiter matches an earlier opening
2817 // delimiter, then don't consume it (it can be used to
2818 // close the earlier one). Otherwise, consume it.
2819 // E.g., we try to recover from:
2822 // } // Incorrect delimiter but matches the earlier `{`
2823 if !self.open_braces.iter().any(|&(b, _)| b == other) {
2828 // Silently recover, the EOF token will be seen again
2829 // and an error emitted then. Thus we don't pop from
2830 // self.open_braces here.
2835 Ok(TokenTree::Delimited(span, Rc::new(Delimited {
2837 open_span: open_span,
2839 close_span: close_span,
2843 // invariants: the current token is not a left-delimiter,
2844 // not an EOF, and not the desired right-delimiter (if
2845 // it were, parse_seq_to_before_end would have prevented
2846 // reaching this point).
2847 maybe_whole!(deref self, NtTT);
2849 token::CloseDelim(_) => {
2850 // An unexpected closing delimiter (i.e., there is no
2851 // matching opening delimiter).
2852 let token_str = self.this_token_to_string();
2853 let err = self.diagnostic().struct_span_err(self.span,
2854 &format!("unexpected close delimiter: `{}`", token_str));
2857 /* we ought to allow different depths of unquotation */
2858 token::Dollar | token::SubstNt(..) if self.quote_depth > 0 => {
2859 self.parse_unquoted()
2862 Ok(TokenTree::Token(self.span, self.bump_and_get()))
2869 // parse a stream of tokens into a list of TokenTree's,
2871 pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
2872 let mut tts = Vec::new();
2873 while self.token != token::Eof {
2874 tts.push(self.parse_token_tree()?);
2879 /// Parse a prefix-unary-operator expr
2880 pub fn parse_prefix_expr(&mut self,
2881 already_parsed_attrs: Option<ThinAttributes>)
2882 -> PResult<'a, P<Expr>> {
2883 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
2884 let lo = self.span.lo;
2886 // Note: when adding new unary operators, don't forget to adjust Token::can_begin_expr()
2887 let ex = match self.token {
2890 let e = self.parse_prefix_expr(None);
2891 let (span, e) = self.interpolated_or_expr_span(e)?;
2893 self.mk_unary(UnOp::Not, e)
2895 token::BinOp(token::Minus) => {
2897 let e = self.parse_prefix_expr(None);
2898 let (span, e) = self.interpolated_or_expr_span(e)?;
2900 self.mk_unary(UnOp::Neg, e)
2902 token::BinOp(token::Star) => {
2904 let e = self.parse_prefix_expr(None);
2905 let (span, e) = self.interpolated_or_expr_span(e)?;
2907 self.mk_unary(UnOp::Deref, e)
2909 token::BinOp(token::And) | token::AndAnd => {
2911 let m = self.parse_mutability()?;
2912 let e = self.parse_prefix_expr(None);
2913 let (span, e) = self.interpolated_or_expr_span(e)?;
2915 ExprKind::AddrOf(m, e)
2917 token::Ident(..) if self.token.is_keyword(keywords::In) => {
2919 let place = self.parse_expr_res(
2920 Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
2923 let blk = self.parse_block()?;
2924 let span = blk.span;
2926 let blk_expr = self.mk_expr(span.lo, span.hi, ExprKind::Block(blk),
2928 ExprKind::InPlace(place, blk_expr)
2930 token::Ident(..) if self.token.is_keyword(keywords::Box) => {
2932 let e = self.parse_prefix_expr(None);
2933 let (span, e) = self.interpolated_or_expr_span(e)?;
2937 _ => return self.parse_dot_or_call_expr(Some(attrs))
2939 return Ok(self.mk_expr(lo, hi, ex, attrs));
2942 /// Parse an associative expression
2944 /// This parses an expression accounting for associativity and precedence of the operators in
2946 pub fn parse_assoc_expr(&mut self,
2947 already_parsed_attrs: Option<ThinAttributes>)
2948 -> PResult<'a, P<Expr>> {
2949 self.parse_assoc_expr_with(0, already_parsed_attrs.into())
2952 /// Parse an associative expression with operators of at least `min_prec` precedence
2953 pub fn parse_assoc_expr_with(&mut self,
2956 -> PResult<'a, P<Expr>> {
2957 let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
2960 let attrs = match lhs {
2961 LhsExpr::AttributesParsed(attrs) => Some(attrs),
2964 if self.token == token::DotDot || self.token == token::DotDotDot {
2965 return self.parse_prefix_range_expr(attrs);
2967 self.parse_prefix_expr(attrs)?
2971 if self.expr_is_complete(&lhs) {
2972 // Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
2975 self.expected_tokens.push(TokenType::Operator);
2976 while let Some(op) = AssocOp::from_token(&self.token) {
2978 let lhs_span = if self.last_token_interpolated {
2984 let cur_op_span = self.span;
2985 let restrictions = if op.is_assign_like() {
2986 self.restrictions & Restrictions::RESTRICTION_NO_STRUCT_LITERAL
2990 if op.precedence() < min_prec {
2994 if op.is_comparison() {
2995 self.check_no_chained_comparison(&lhs, &op);
2998 if op == AssocOp::As {
2999 let rhs = self.parse_ty()?;
3000 lhs = self.mk_expr(lhs_span.lo, rhs.span.hi,
3001 ExprKind::Cast(lhs, rhs), None);
3003 } else if op == AssocOp::Colon {
3004 let rhs = self.parse_ty()?;
3005 lhs = self.mk_expr(lhs_span.lo, rhs.span.hi,
3006 ExprKind::Type(lhs, rhs), None);
3008 } else if op == AssocOp::DotDot || op == AssocOp::DotDotDot {
3009 // If we didn’t have to handle `x..`/`x...`, it would be pretty easy to
3010 // generalise it to the Fixity::None code.
3012 // We have 2 alternatives here: `x..y`/`x...y` and `x..`/`x...` The other
3013 // two variants are handled with `parse_prefix_range_expr` call above.
3014 let rhs = if self.is_at_start_of_range_notation_rhs() {
3015 Some(self.parse_assoc_expr_with(op.precedence() + 1,
3016 LhsExpr::NotYetParsed)?)
3020 let (lhs_span, rhs_span) = (lhs.span, if let Some(ref x) = rhs {
3025 let limits = if op == AssocOp::DotDot {
3026 RangeLimits::HalfOpen
3031 let r = try!(self.mk_range(Some(lhs), rhs, limits));
3032 lhs = self.mk_expr(lhs_span.lo, rhs_span.hi, r, None);
3036 let rhs = match op.fixity() {
3037 Fixity::Right => self.with_res(
3038 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
3040 this.parse_assoc_expr_with(op.precedence(),
3041 LhsExpr::NotYetParsed)
3043 Fixity::Left => self.with_res(
3044 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
3046 this.parse_assoc_expr_with(op.precedence() + 1,
3047 LhsExpr::NotYetParsed)
3049 // We currently have no non-associative operators that are not handled above by
3050 // the special cases. The code is here only for future convenience.
3051 Fixity::None => self.with_res(
3052 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
3054 this.parse_assoc_expr_with(op.precedence() + 1,
3055 LhsExpr::NotYetParsed)
3060 AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide |
3061 AssocOp::Modulus | AssocOp::LAnd | AssocOp::LOr | AssocOp::BitXor |
3062 AssocOp::BitAnd | AssocOp::BitOr | AssocOp::ShiftLeft | AssocOp::ShiftRight |
3063 AssocOp::Equal | AssocOp::Less | AssocOp::LessEqual | AssocOp::NotEqual |
3064 AssocOp::Greater | AssocOp::GreaterEqual => {
3065 let ast_op = op.to_ast_binop().unwrap();
3066 let (lhs_span, rhs_span) = (lhs_span, rhs.span);
3067 let binary = self.mk_binary(codemap::respan(cur_op_span, ast_op), lhs, rhs);
3068 self.mk_expr(lhs_span.lo, rhs_span.hi, binary, None)
3071 self.mk_expr(lhs_span.lo, rhs.span.hi, ExprKind::Assign(lhs, rhs), None),
3073 self.mk_expr(lhs_span.lo, rhs.span.hi, ExprKind::InPlace(lhs, rhs), None),
3074 AssocOp::AssignOp(k) => {
3076 token::Plus => BinOpKind::Add,
3077 token::Minus => BinOpKind::Sub,
3078 token::Star => BinOpKind::Mul,
3079 token::Slash => BinOpKind::Div,
3080 token::Percent => BinOpKind::Rem,
3081 token::Caret => BinOpKind::BitXor,
3082 token::And => BinOpKind::BitAnd,
3083 token::Or => BinOpKind::BitOr,
3084 token::Shl => BinOpKind::Shl,
3085 token::Shr => BinOpKind::Shr,
3087 let (lhs_span, rhs_span) = (lhs_span, rhs.span);
3088 let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
3089 self.mk_expr(lhs_span.lo, rhs_span.hi, aopexpr, None)
3091 AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotDot => {
3092 self.bug("As, Colon, DotDot or DotDotDot branch reached")
3096 if op.fixity() == Fixity::None { break }
3101 /// Produce an error if comparison operators are chained (RFC #558).
3102 /// We only need to check lhs, not rhs, because all comparison ops
3103 /// have same precedence and are left-associative
3104 fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: &AssocOp) {
3105 debug_assert!(outer_op.is_comparison());
3107 ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
3108 // respan to include both operators
3109 let op_span = mk_sp(op.span.lo, self.span.hi);
3110 let mut err = self.diagnostic().struct_span_err(op_span,
3111 "chained comparison operators require parentheses");
3112 if op.node == BinOpKind::Lt && *outer_op == AssocOp::Greater {
3114 "use `::<...>` instead of `<...>` if you meant to specify type arguments");
3122 /// Parse prefix-forms of range notation: `..expr`, `..`, `...expr`
3123 fn parse_prefix_range_expr(&mut self,
3124 already_parsed_attrs: Option<ThinAttributes>)
3125 -> PResult<'a, P<Expr>> {
3126 debug_assert!(self.token == token::DotDot || self.token == token::DotDotDot);
3127 let tok = self.token.clone();
3128 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
3129 let lo = self.span.lo;
3130 let mut hi = self.span.hi;
3132 let opt_end = if self.is_at_start_of_range_notation_rhs() {
3133 // RHS must be parsed with more associativity than the dots.
3134 let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
3135 Some(self.parse_assoc_expr_with(next_prec,
3136 LhsExpr::NotYetParsed)
3144 let limits = if tok == token::DotDot {
3145 RangeLimits::HalfOpen
3150 let r = try!(self.mk_range(None,
3153 Ok(self.mk_expr(lo, hi, r, attrs))
3156 fn is_at_start_of_range_notation_rhs(&self) -> bool {
3157 if self.token.can_begin_expr() {
3158 // parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
3159 if self.token == token::OpenDelim(token::Brace) {
3160 return !self.restrictions.contains(Restrictions::RESTRICTION_NO_STRUCT_LITERAL);
3168 /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3169 pub fn parse_if_expr(&mut self, attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3170 if self.check_keyword(keywords::Let) {
3171 return self.parse_if_let_expr(attrs);
3173 let lo = self.last_span.lo;
3174 let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3175 let thn = self.parse_block()?;
3176 let mut els: Option<P<Expr>> = None;
3177 let mut hi = thn.span.hi;
3178 if self.eat_keyword(keywords::Else) {
3179 let elexpr = self.parse_else_expr()?;
3180 hi = elexpr.span.hi;
3183 Ok(self.mk_expr(lo, hi, ExprKind::If(cond, thn, els), attrs))
3186 /// Parse an 'if let' expression ('if' token already eaten)
3187 pub fn parse_if_let_expr(&mut self, attrs: ThinAttributes)
3188 -> PResult<'a, P<Expr>> {
3189 let lo = self.last_span.lo;
3190 self.expect_keyword(keywords::Let)?;
3191 let pat = self.parse_pat()?;
3192 self.expect(&token::Eq)?;
3193 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3194 let thn = self.parse_block()?;
3195 let (hi, els) = if self.eat_keyword(keywords::Else) {
3196 let expr = self.parse_else_expr()?;
3197 (expr.span.hi, Some(expr))
3201 Ok(self.mk_expr(lo, hi, ExprKind::IfLet(pat, expr, thn, els), attrs))
3204 // `move |args| expr`
3205 pub fn parse_lambda_expr(&mut self,
3207 capture_clause: CaptureBy,
3208 attrs: ThinAttributes)
3209 -> PResult<'a, P<Expr>>
3211 let decl = self.parse_fn_block_decl()?;
3212 let decl_hi = self.last_span.hi;
3213 let body = match decl.output {
3214 FunctionRetTy::Default(_) => {
3215 // If no explicit return type is given, parse any
3216 // expr and wrap it up in a dummy block:
3217 let body_expr = self.parse_expr()?;
3219 id: ast::DUMMY_NODE_ID,
3220 span: body_expr.span,
3221 stmts: vec![Spanned {
3222 span: body_expr.span,
3223 node: StmtKind::Expr(body_expr, ast::DUMMY_NODE_ID),
3225 rules: BlockCheckMode::Default,
3229 // If an explicit return type is given, require a
3230 // block to appear (RFC 968).
3238 ExprKind::Closure(capture_clause, decl, body, mk_sp(lo, decl_hi)),
3242 // `else` token already eaten
3243 pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
3244 if self.eat_keyword(keywords::If) {
3245 return self.parse_if_expr(None);
3247 let blk = self.parse_block()?;
3248 return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), None));
3252 /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3253 pub fn parse_for_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3255 attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3256 // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3258 let pat = self.parse_pat()?;
3259 self.expect_keyword(keywords::In)?;
3260 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3261 let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
3262 let attrs = attrs.append(iattrs.into_thin_attrs());
3264 let hi = self.last_span.hi;
3266 Ok(self.mk_expr(span_lo, hi,
3267 ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
3271 /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3272 pub fn parse_while_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3274 attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3275 if self.token.is_keyword(keywords::Let) {
3276 return self.parse_while_let_expr(opt_ident, span_lo, attrs);
3278 let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3279 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3280 let attrs = attrs.append(iattrs.into_thin_attrs());
3281 let hi = body.span.hi;
3282 return Ok(self.mk_expr(span_lo, hi, ExprKind::While(cond, body, opt_ident),
3286 /// Parse a 'while let' expression ('while' token already eaten)
3287 pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3289 attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3290 self.expect_keyword(keywords::Let)?;
3291 let pat = self.parse_pat()?;
3292 self.expect(&token::Eq)?;
3293 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3294 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3295 let attrs = attrs.append(iattrs.into_thin_attrs());
3296 let hi = body.span.hi;
3297 return Ok(self.mk_expr(span_lo, hi, ExprKind::WhileLet(pat, expr, body, opt_ident), attrs));
3300 // parse `loop {...}`, `loop` token already eaten
3301 pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3303 attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3304 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3305 let attrs = attrs.append(iattrs.into_thin_attrs());
3306 let hi = body.span.hi;
3307 Ok(self.mk_expr(span_lo, hi, ExprKind::Loop(body, opt_ident), attrs))
3310 // `match` token already eaten
3311 fn parse_match_expr(&mut self, attrs: ThinAttributes) -> PResult<'a, P<Expr>> {
3312 let match_span = self.last_span;
3313 let lo = self.last_span.lo;
3314 let discriminant = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
3316 if let Err(mut e) = self.commit_expr_expecting(&discriminant,
3317 token::OpenDelim(token::Brace)) {
3318 if self.token == token::Token::Semi {
3319 e.span_note(match_span, "did you mean to remove this `match` keyword?");
3323 let attrs = attrs.append(
3324 self.parse_inner_attributes()?.into_thin_attrs());
3325 let mut arms: Vec<Arm> = Vec::new();
3326 while self.token != token::CloseDelim(token::Brace) {
3327 match self.parse_arm() {
3328 Ok(arm) => arms.push(arm),
3330 // Recover by skipping to the end of the block.
3332 self.recover_stmt();
3333 let hi = self.span.hi;
3334 if self.token == token::CloseDelim(token::Brace) {
3337 return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
3341 let hi = self.span.hi;
3343 return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
3346 pub fn parse_arm(&mut self) -> PResult<'a, Arm> {
3347 maybe_whole!(no_clone self, NtArm);
3349 let attrs = self.parse_outer_attributes()?;
3350 let pats = self.parse_pats()?;
3351 let mut guard = None;
3352 if self.eat_keyword(keywords::If) {
3353 guard = Some(self.parse_expr()?);
3355 self.expect(&token::FatArrow)?;
3356 let expr = self.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR, None)?;
3359 !classify::expr_is_simple_block(&expr)
3360 && self.token != token::CloseDelim(token::Brace);
3363 self.commit_expr(&expr, &[token::Comma], &[token::CloseDelim(token::Brace)])?;
3365 self.eat(&token::Comma);
3376 /// Parse an expression
3377 pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
3378 self.parse_expr_res(Restrictions::empty(), None)
3381 /// Evaluate the closure with restrictions in place.
3383 /// After the closure is evaluated, restrictions are reset.
3384 pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
3385 where F: FnOnce(&mut Self) -> T
3387 let old = self.restrictions;
3388 self.restrictions = r;
3390 self.restrictions = old;
3395 /// Parse an expression, subject to the given restrictions
3396 pub fn parse_expr_res(&mut self, r: Restrictions,
3397 already_parsed_attrs: Option<ThinAttributes>)
3398 -> PResult<'a, P<Expr>> {
3399 self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
3402 /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3403 fn parse_initializer(&mut self) -> PResult<'a, Option<P<Expr>>> {
3404 if self.check(&token::Eq) {
3406 Ok(Some(self.parse_expr()?))
3412 /// Parse patterns, separated by '|' s
3413 fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
3414 let mut pats = Vec::new();
3416 pats.push(self.parse_pat()?);
3417 if self.check(&token::BinOp(token::Or)) { self.bump();}
3418 else { return Ok(pats); }
3422 fn parse_pat_tuple_elements(&mut self, unary_needs_comma: bool)
3423 -> PResult<'a, (Vec<P<Pat>>, Option<usize>)> {
3424 let mut fields = vec![];
3425 let mut ddpos = None;
3427 while !self.check(&token::CloseDelim(token::Paren)) {
3428 if ddpos.is_none() && self.eat(&token::DotDot) {
3429 ddpos = Some(fields.len());
3430 if self.eat(&token::Comma) {
3431 // `..` needs to be followed by `)` or `, pat`, `..,)` is disallowed.
3432 fields.push(self.parse_pat()?);
3434 } else if ddpos.is_some() && self.eat(&token::DotDot) {
3435 // Emit a friendly error, ignore `..` and continue parsing
3436 self.span_err(self.last_span, "`..` can only be used once per \
3437 tuple or tuple struct pattern");
3439 fields.push(self.parse_pat()?);
3442 if !self.check(&token::CloseDelim(token::Paren)) ||
3443 (unary_needs_comma && fields.len() == 1 && ddpos.is_none()) {
3444 self.expect(&token::Comma)?;
3451 fn parse_pat_vec_elements(
3453 ) -> PResult<'a, (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>)> {
3454 let mut before = Vec::new();
3455 let mut slice = None;
3456 let mut after = Vec::new();
3457 let mut first = true;
3458 let mut before_slice = true;
3460 while self.token != token::CloseDelim(token::Bracket) {
3464 self.expect(&token::Comma)?;
3466 if self.token == token::CloseDelim(token::Bracket)
3467 && (before_slice || !after.is_empty()) {
3473 if self.check(&token::DotDot) {
3476 if self.check(&token::Comma) ||
3477 self.check(&token::CloseDelim(token::Bracket)) {
3478 slice = Some(P(ast::Pat {
3479 id: ast::DUMMY_NODE_ID,
3480 node: PatKind::Wild,
3483 before_slice = false;
3489 let subpat = self.parse_pat()?;
3490 if before_slice && self.check(&token::DotDot) {
3492 slice = Some(subpat);
3493 before_slice = false;
3494 } else if before_slice {
3495 before.push(subpat);
3501 Ok((before, slice, after))
3504 /// Parse the fields of a struct-like pattern
3505 fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<codemap::Spanned<ast::FieldPat>> , bool)> {
3506 let mut fields = Vec::new();
3507 let mut etc = false;
3508 let mut first = true;
3509 while self.token != token::CloseDelim(token::Brace) {
3513 self.expect(&token::Comma)?;
3514 // accept trailing commas
3515 if self.check(&token::CloseDelim(token::Brace)) { break }
3518 let lo = self.span.lo;
3521 if self.check(&token::DotDot) {
3523 if self.token != token::CloseDelim(token::Brace) {
3524 let token_str = self.this_token_to_string();
3525 return Err(self.fatal(&format!("expected `{}`, found `{}`", "}",
3532 // Check if a colon exists one ahead. This means we're parsing a fieldname.
3533 let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
3534 // Parsing a pattern of the form "fieldname: pat"
3535 let fieldname = self.parse_ident()?;
3537 let pat = self.parse_pat()?;
3539 (pat, fieldname, false)
3541 // Parsing a pattern of the form "(box) (ref) (mut) fieldname"
3542 let is_box = self.eat_keyword(keywords::Box);
3543 let boxed_span_lo = self.span.lo;
3544 let is_ref = self.eat_keyword(keywords::Ref);
3545 let is_mut = self.eat_keyword(keywords::Mut);
3546 let fieldname = self.parse_ident()?;
3547 hi = self.last_span.hi;
3549 let bind_type = match (is_ref, is_mut) {
3550 (true, true) => BindingMode::ByRef(Mutability::Mutable),
3551 (true, false) => BindingMode::ByRef(Mutability::Immutable),
3552 (false, true) => BindingMode::ByValue(Mutability::Mutable),
3553 (false, false) => BindingMode::ByValue(Mutability::Immutable),
3555 let fieldpath = codemap::Spanned{span:self.last_span, node:fieldname};
3556 let fieldpat = P(ast::Pat{
3557 id: ast::DUMMY_NODE_ID,
3558 node: PatKind::Ident(bind_type, fieldpath, None),
3559 span: mk_sp(boxed_span_lo, hi),
3562 let subpat = if is_box {
3564 id: ast::DUMMY_NODE_ID,
3565 node: PatKind::Box(fieldpat),
3566 span: mk_sp(lo, hi),
3571 (subpat, fieldname, true)
3574 fields.push(codemap::Spanned { span: mk_sp(lo, hi),
3575 node: ast::FieldPat { ident: fieldname,
3577 is_shorthand: is_shorthand }});
3579 return Ok((fields, etc));
3582 fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
3583 if self.token.is_path_start() {
3584 let lo = self.span.lo;
3585 let (qself, path) = if self.eat_lt() {
3586 // Parse a qualified path
3588 self.parse_qualified_path(PathStyle::Expr)?;
3591 // Parse an unqualified path
3592 (None, self.parse_path(PathStyle::Expr)?)
3594 let hi = self.last_span.hi;
3595 Ok(self.mk_expr(lo, hi, ExprKind::Path(qself, path), None))
3597 self.parse_pat_literal_maybe_minus()
3601 /// Parse a pattern.
3602 pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
3603 maybe_whole!(self, NtPat);
3605 let lo = self.span.lo;
3608 token::Underscore => {
3611 pat = PatKind::Wild;
3613 token::BinOp(token::And) | token::AndAnd => {
3614 // Parse &pat / &mut pat
3616 let mutbl = self.parse_mutability()?;
3617 if let token::Lifetime(ident) = self.token {
3618 return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
3621 let subpat = self.parse_pat()?;
3622 pat = PatKind::Ref(subpat, mutbl);
3624 token::OpenDelim(token::Paren) => {
3625 // Parse (pat,pat,pat,...) as tuple pattern
3627 let (fields, ddpos) = self.parse_pat_tuple_elements(true)?;
3628 self.expect(&token::CloseDelim(token::Paren))?;
3629 pat = PatKind::Tuple(fields, ddpos);
3631 token::OpenDelim(token::Bracket) => {
3632 // Parse [pat,pat,...] as slice pattern
3634 let (before, slice, after) = self.parse_pat_vec_elements()?;
3635 self.expect(&token::CloseDelim(token::Bracket))?;
3636 pat = PatKind::Vec(before, slice, after);
3639 // At this point, token != _, &, &&, (, [
3640 if self.eat_keyword(keywords::Mut) {
3641 // Parse mut ident @ pat
3642 pat = self.parse_pat_ident(BindingMode::ByValue(Mutability::Mutable))?;
3643 } else if self.eat_keyword(keywords::Ref) {
3644 // Parse ref ident @ pat / ref mut ident @ pat
3645 let mutbl = self.parse_mutability()?;
3646 pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
3647 } else if self.eat_keyword(keywords::Box) {
3649 let subpat = self.parse_pat()?;
3650 pat = PatKind::Box(subpat);
3651 } else if self.token.is_path_start() {
3652 // Parse pattern starting with a path
3653 if self.token.is_ident() && self.look_ahead(1, |t| *t != token::DotDotDot &&
3654 *t != token::OpenDelim(token::Brace) &&
3655 *t != token::OpenDelim(token::Paren) &&
3656 *t != token::ModSep) {
3657 // Plain idents have some extra abilities here compared to general paths
3658 if self.look_ahead(1, |t| *t == token::Not) {
3659 // Parse macro invocation
3660 let path = self.parse_ident_into_path()?;
3662 let delim = self.expect_open_delim()?;
3663 let tts = self.parse_seq_to_end(
3664 &token::CloseDelim(delim),
3665 SeqSep::none(), |p| p.parse_token_tree())?;
3666 let mac = Mac_ { path: path, tts: tts, ctxt: EMPTY_CTXT };
3667 pat = PatKind::Mac(codemap::Spanned {node: mac,
3668 span: mk_sp(lo, self.last_span.hi)});
3670 // Parse ident @ pat
3671 // This can give false positives and parse nullary enums,
3672 // they are dealt with later in resolve
3673 let binding_mode = BindingMode::ByValue(Mutability::Immutable);
3674 pat = self.parse_pat_ident(binding_mode)?;
3677 let (qself, path) = if self.eat_lt() {
3678 // Parse a qualified path
3680 self.parse_qualified_path(PathStyle::Expr)?;
3683 // Parse an unqualified path
3684 (None, self.parse_path(PathStyle::Expr)?)
3687 token::DotDotDot => {
3689 let hi = self.last_span.hi;
3690 let begin = self.mk_expr(lo, hi, ExprKind::Path(qself, path), None);
3692 let end = self.parse_pat_range_end()?;
3693 pat = PatKind::Range(begin, end);
3695 token::OpenDelim(token::Brace) => {
3696 if qself.is_some() {
3697 return Err(self.fatal("unexpected `{` after qualified path"));
3699 // Parse struct pattern
3701 let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
3703 self.recover_stmt();
3707 pat = PatKind::Struct(path, fields, etc);
3709 token::OpenDelim(token::Paren) => {
3710 if qself.is_some() {
3711 return Err(self.fatal("unexpected `(` after qualified path"));
3713 // Parse tuple struct or enum pattern
3715 let (fields, ddpos) = self.parse_pat_tuple_elements(false)?;
3716 self.expect(&token::CloseDelim(token::Paren))?;
3717 pat = PatKind::TupleStruct(path, fields, ddpos)
3721 // Parse qualified path
3722 Some(qself) => PatKind::QPath(qself, path),
3723 // Parse nullary enum
3724 None => PatKind::Path(path)
3730 // Try to parse everything else as literal with optional minus
3731 match self.parse_pat_literal_maybe_minus() {
3733 if self.eat(&token::DotDotDot) {
3734 let end = self.parse_pat_range_end()?;
3735 pat = PatKind::Range(begin, end);
3737 pat = PatKind::Lit(begin);
3742 let msg = format!("expected pattern, found {}", self.this_token_descr());
3743 return Err(self.fatal(&msg));
3750 let hi = self.last_span.hi;
3752 id: ast::DUMMY_NODE_ID,
3754 span: mk_sp(lo, hi),
3758 /// Parse ident or ident @ pat
3759 /// used by the copy foo and ref foo patterns to give a good
3760 /// error message when parsing mistakes like ref foo(a,b)
3761 fn parse_pat_ident(&mut self,
3762 binding_mode: ast::BindingMode)
3763 -> PResult<'a, PatKind> {
3764 let ident = self.parse_ident()?;
3765 let last_span = self.last_span;
3766 let name = codemap::Spanned{span: last_span, node: ident};
3767 let sub = if self.eat(&token::At) {
3768 Some(self.parse_pat()?)
3773 // just to be friendly, if they write something like
3775 // we end up here with ( as the current token. This shortly
3776 // leads to a parse error. Note that if there is no explicit
3777 // binding mode then we do not end up here, because the lookahead
3778 // will direct us over to parse_enum_variant()
3779 if self.token == token::OpenDelim(token::Paren) {
3780 let last_span = self.last_span;
3781 return Err(self.span_fatal(
3783 "expected identifier, found enum pattern"))
3786 Ok(PatKind::Ident(binding_mode, name, sub))
3789 /// Parse a local variable declaration
3790 fn parse_local(&mut self, attrs: ThinAttributes) -> PResult<'a, P<Local>> {
3791 let lo = self.span.lo;
3792 let pat = self.parse_pat()?;
3795 if self.eat(&token::Colon) {
3796 ty = Some(self.parse_ty_sum()?);
3798 let init = self.parse_initializer()?;
3803 id: ast::DUMMY_NODE_ID,
3804 span: mk_sp(lo, self.last_span.hi),
3809 /// Parse a "let" stmt
3810 fn parse_let(&mut self, attrs: ThinAttributes) -> PResult<'a, P<Decl>> {
3811 let lo = self.span.lo;
3812 let local = self.parse_local(attrs)?;
3813 Ok(P(spanned(lo, self.last_span.hi, DeclKind::Local(local))))
3816 /// Parse a structure field
3817 fn parse_name_and_ty(&mut self, pr: Visibility,
3818 attrs: Vec<Attribute> ) -> PResult<'a, StructField> {
3820 Visibility::Inherited => self.span.lo,
3821 _ => self.last_span.lo,
3823 let name = self.parse_ident()?;
3824 self.expect(&token::Colon)?;
3825 let ty = self.parse_ty_sum()?;
3827 span: mk_sp(lo, self.last_span.hi),
3830 id: ast::DUMMY_NODE_ID,
3836 /// Emit an expected item after attributes error.
3837 fn expected_item_err(&self, attrs: &[Attribute]) {
3838 let message = match attrs.last() {
3839 Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
3840 "expected item after doc comment"
3842 _ => "expected item after attributes",
3845 self.span_err(self.last_span, message);
3848 /// Parse a statement. may include decl.
3849 pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
3850 Ok(self.parse_stmt_())
3853 // Eat tokens until we can be relatively sure we reached the end of the
3854 // statement. This is something of a best-effort heuristic.
3856 // We terminate when we find an unmatched `}` (without consuming it).
3857 fn recover_stmt(&mut self) {
3858 self.recover_stmt_(SemiColonMode::Ignore)
3860 // If `break_on_semi` is `Break`, then we will stop consuming tokens after
3861 // finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
3862 // approximate - it can mean we break too early due to macros, but that
3863 // shoud only lead to sub-optimal recovery, not inaccurate parsing).
3864 fn recover_stmt_(&mut self, break_on_semi: SemiColonMode) {
3865 let mut brace_depth = 0;
3866 let mut bracket_depth = 0;
3867 debug!("recover_stmt_ enter loop");
3869 debug!("recover_stmt_ loop {:?}", self.token);
3871 token::OpenDelim(token::DelimToken::Brace) => {
3875 token::OpenDelim(token::DelimToken::Bracket) => {
3879 token::CloseDelim(token::DelimToken::Brace) => {
3880 if brace_depth == 0 {
3881 debug!("recover_stmt_ return - close delim {:?}", self.token);
3887 token::CloseDelim(token::DelimToken::Bracket) => {
3889 if bracket_depth < 0 {
3895 debug!("recover_stmt_ return - Eof");
3900 if break_on_semi == SemiColonMode::Break &&
3902 bracket_depth == 0 {
3903 debug!("recover_stmt_ return - Semi");
3914 fn parse_stmt_(&mut self) -> Option<Stmt> {
3915 self.parse_stmt_without_recovery().unwrap_or_else(|mut e| {
3917 self.recover_stmt_(SemiColonMode::Break);
3922 fn parse_stmt_without_recovery(&mut self) -> PResult<'a, Option<Stmt>> {
3923 maybe_whole!(Some deref self, NtStmt);
3925 let attrs = self.parse_outer_attributes()?;
3926 let lo = self.span.lo;
3928 Ok(Some(if self.check_keyword(keywords::Let) {
3929 self.expect_keyword(keywords::Let)?;
3930 let decl = self.parse_let(attrs.into_thin_attrs())?;
3931 let hi = decl.span.hi;
3932 let stmt = StmtKind::Decl(decl, ast::DUMMY_NODE_ID);
3933 spanned(lo, hi, stmt)
3934 } else if self.token.is_ident()
3935 && !self.token.is_any_keyword()
3936 && self.look_ahead(1, |t| *t == token::Not) {
3937 // it's a macro invocation:
3939 // Potential trouble: if we allow macros with paths instead of
3940 // idents, we'd need to look ahead past the whole path here...
3941 let pth = self.parse_ident_into_path()?;
3944 let id = match self.token {
3945 token::OpenDelim(_) => keywords::Invalid.ident(), // no special identifier
3946 _ => self.parse_ident()?,
3949 // check that we're pointing at delimiters (need to check
3950 // again after the `if`, because of `parse_ident`
3951 // consuming more tokens).
3952 let delim = match self.token {
3953 token::OpenDelim(delim) => delim,
3955 // we only expect an ident if we didn't parse one
3957 let ident_str = if id.name == keywords::Invalid.name() {
3962 let tok_str = self.this_token_to_string();
3963 return Err(self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
3969 let tts = self.parse_unspanned_seq(
3970 &token::OpenDelim(delim),
3971 &token::CloseDelim(delim),
3973 |p| p.parse_token_tree()
3975 let hi = self.last_span.hi;
3977 let style = if delim == token::Brace {
3978 MacStmtStyle::Braces
3980 MacStmtStyle::NoBraces
3983 if id.name == keywords::Invalid.name() {
3984 let mac = P(spanned(lo, hi, Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT }));
3985 let stmt = StmtKind::Mac(mac, style, attrs.into_thin_attrs());
3986 spanned(lo, hi, stmt)
3988 // if it has a special ident, it's definitely an item
3990 // Require a semicolon or braces.
3991 if style != MacStmtStyle::Braces {
3992 if !self.eat(&token::Semi) {
3993 let last_span = self.last_span;
3994 self.span_err(last_span,
3995 "macros that expand to items must \
3996 either be surrounded with braces or \
3997 followed by a semicolon");
4000 spanned(lo, hi, StmtKind::Decl(
4001 P(spanned(lo, hi, DeclKind::Item(
4003 lo, hi, id /*id is good here*/,
4004 ItemKind::Mac(spanned(lo, hi,
4005 Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT })),
4006 Visibility::Inherited, attrs)))),
4007 ast::DUMMY_NODE_ID))
4010 // FIXME: Bad copy of attrs
4011 let restrictions = self.restrictions | Restrictions::NO_NONINLINE_MOD;
4012 match self.with_res(restrictions,
4013 |this| this.parse_item_(attrs.clone(), false, true))? {
4016 let decl = P(spanned(lo, hi, DeclKind::Item(i)));
4017 spanned(lo, hi, StmtKind::Decl(decl, ast::DUMMY_NODE_ID))
4020 let unused_attrs = |attrs: &[_], s: &mut Self| {
4021 if attrs.len() > 0 {
4023 "expected statement after outer attribute");
4027 // Do not attempt to parse an expression if we're done here.
4028 if self.token == token::Semi {
4029 unused_attrs(&attrs, self);
4034 if self.token == token::CloseDelim(token::Brace) {
4035 unused_attrs(&attrs, self);
4039 // Remainder are line-expr stmts.
4040 let e = self.parse_expr_res(
4041 Restrictions::RESTRICTION_STMT_EXPR, Some(attrs.into_thin_attrs()))?;
4043 let stmt = StmtKind::Expr(e, ast::DUMMY_NODE_ID);
4044 spanned(lo, hi, stmt)
4050 /// Is this expression a successfully-parsed statement?
4051 fn expr_is_complete(&mut self, e: &Expr) -> bool {
4052 self.restrictions.contains(Restrictions::RESTRICTION_STMT_EXPR) &&
4053 !classify::expr_requires_semi_to_be_stmt(e)
4056 /// Parse a block. No inner attrs are allowed.
4057 pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
4058 maybe_whole!(no_clone self, NtBlock);
4060 let lo = self.span.lo;
4062 if !self.eat(&token::OpenDelim(token::Brace)) {
4064 let tok = self.this_token_to_string();
4065 return Err(self.span_fatal_help(sp,
4066 &format!("expected `{{`, found `{}`", tok),
4067 "place this code inside a block"));
4070 self.parse_block_tail(lo, BlockCheckMode::Default)
4073 /// Parse a block. Inner attrs are allowed.
4074 fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
4075 maybe_whole!(pair_empty self, NtBlock);
4077 let lo = self.span.lo;
4078 self.expect(&token::OpenDelim(token::Brace))?;
4079 Ok((self.parse_inner_attributes()?,
4080 self.parse_block_tail(lo, BlockCheckMode::Default)?))
4083 /// Parse the rest of a block expression or function body
4084 /// Precondition: already parsed the '{'.
4085 fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> PResult<'a, P<Block>> {
4086 let mut stmts = vec![];
4088 while !self.eat(&token::CloseDelim(token::Brace)) {
4089 let Spanned {node, span} = if let Some(s) = self.parse_stmt_() {
4091 } else if self.token == token::Eof {
4094 // Found only `;` or `}`.
4098 StmtKind::Expr(e, _) => {
4099 self.handle_expression_like_statement(e, span, &mut stmts)?;
4101 StmtKind::Mac(mac, MacStmtStyle::NoBraces, attrs) => {
4102 // statement macro without braces
4105 stmts.push(Spanned {
4106 node: StmtKind::Mac(mac, MacStmtStyle::Semicolon, attrs),
4107 span: mk_sp(span.lo, self.span.hi),
4112 let e = self.mk_mac_expr(span.lo, span.hi,
4113 mac.and_then(|m| m.node),
4116 let e = self.parse_dot_or_call_expr_with(e, lo, attrs)?;
4117 let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
4118 self.handle_expression_like_statement(e, span, &mut stmts)?;
4122 StmtKind::Mac(m, style, attrs) => {
4123 // statement macro; might be an expr
4126 stmts.push(Spanned {
4127 node: StmtKind::Mac(m, MacStmtStyle::Semicolon, attrs),
4128 span: mk_sp(span.lo, self.span.hi),
4133 stmts.push(Spanned {
4134 node: StmtKind::Mac(m, style, attrs),
4140 _ => { // all other kinds of statements:
4141 let mut hi = span.hi;
4142 if classify::stmt_ends_with_semi(&node) {
4143 self.commit_stmt_expecting(token::Semi)?;
4144 hi = self.last_span.hi;
4147 stmts.push(Spanned {
4149 span: mk_sp(span.lo, hi)
4157 id: ast::DUMMY_NODE_ID,
4159 span: mk_sp(lo, self.last_span.hi),
4163 fn handle_expression_like_statement(&mut self,
4166 stmts: &mut Vec<Stmt>)
4167 -> PResult<'a, ()> {
4168 // expression without semicolon
4169 if classify::expr_requires_semi_to_be_stmt(&e) {
4170 // Just check for errors and recover; do not eat semicolon yet.
4172 self.commit_stmt(&[], &[token::Semi, token::CloseDelim(token::Brace)])
4175 self.recover_stmt();
4182 let span_with_semi = Span {
4184 hi: self.last_span.hi,
4185 expn_id: span.expn_id,
4187 stmts.push(Spanned {
4188 node: StmtKind::Semi(e, ast::DUMMY_NODE_ID),
4189 span: span_with_semi,
4193 stmts.push(Spanned {
4194 node: StmtKind::Expr(e, ast::DUMMY_NODE_ID),
4202 // Parses a sequence of bounds if a `:` is found,
4203 // otherwise returns empty list.
4204 fn parse_colon_then_ty_param_bounds(&mut self,
4205 mode: BoundParsingMode)
4206 -> PResult<'a, TyParamBounds>
4208 if !self.eat(&token::Colon) {
4211 self.parse_ty_param_bounds(mode)
4215 // matches bounds = ( boundseq )?
4216 // where boundseq = ( polybound + boundseq ) | polybound
4217 // and polybound = ( 'for' '<' 'region '>' )? bound
4218 // and bound = 'region | trait_ref
4219 fn parse_ty_param_bounds(&mut self,
4220 mode: BoundParsingMode)
4221 -> PResult<'a, TyParamBounds>
4223 let mut result = vec!();
4225 let question_span = self.span;
4226 let ate_question = self.eat(&token::Question);
4228 token::Lifetime(lifetime) => {
4230 self.span_err(question_span,
4231 "`?` may only modify trait bounds, not lifetime bounds");
4233 result.push(RegionTyParamBound(ast::Lifetime {
4234 id: ast::DUMMY_NODE_ID,
4240 token::ModSep | token::Ident(..) => {
4241 let poly_trait_ref = self.parse_poly_trait_ref()?;
4242 let modifier = if ate_question {
4243 if mode == BoundParsingMode::Modified {
4244 TraitBoundModifier::Maybe
4246 self.span_err(question_span,
4248 TraitBoundModifier::None
4251 TraitBoundModifier::None
4253 result.push(TraitTyParamBound(poly_trait_ref, modifier))
4258 if !self.eat(&token::BinOp(token::Plus)) {
4263 return Ok(P::from_vec(result));
4266 /// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
4267 fn parse_ty_param(&mut self) -> PResult<'a, TyParam> {
4268 let span = self.span;
4269 let ident = self.parse_ident()?;
4271 let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Modified)?;
4273 let default = if self.check(&token::Eq) {
4275 Some(self.parse_ty_sum()?)
4282 id: ast::DUMMY_NODE_ID,
4289 /// Parse a set of optional generic type parameter declarations. Where
4290 /// clauses are not parsed here, and must be added later via
4291 /// `parse_where_clause()`.
4293 /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4294 /// | ( < lifetimes , typaramseq ( , )? > )
4295 /// where typaramseq = ( typaram ) | ( typaram , typaramseq )
4296 pub fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
4297 maybe_whole!(self, NtGenerics);
4299 if self.eat(&token::Lt) {
4300 let lifetime_defs = self.parse_lifetime_defs()?;
4301 let mut seen_default = false;
4302 let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
4303 p.forbid_lifetime()?;
4304 let ty_param = p.parse_ty_param()?;
4305 if ty_param.default.is_some() {
4306 seen_default = true;
4307 } else if seen_default {
4308 let last_span = p.last_span;
4309 p.span_err(last_span,
4310 "type parameters with a default must be trailing");
4315 lifetimes: lifetime_defs,
4316 ty_params: ty_params,
4317 where_clause: WhereClause {
4318 id: ast::DUMMY_NODE_ID,
4319 predicates: Vec::new(),
4323 Ok(ast::Generics::default())
4327 fn parse_generic_values_after_lt(&mut self) -> PResult<'a, (Vec<ast::Lifetime>,
4329 Vec<TypeBinding>)> {
4330 let span_lo = self.span.lo;
4331 let lifetimes = self.parse_lifetimes(token::Comma)?;
4333 let missing_comma = !lifetimes.is_empty() &&
4334 !self.token.is_like_gt() &&
4336 .as_ref().map_or(true,
4337 |x| &**x != &token::Comma);
4341 let msg = format!("expected `,` or `>` after lifetime \
4343 self.this_token_to_string());
4344 let mut err = self.diagnostic().struct_span_err(self.span, &msg);
4346 let span_hi = self.span.hi;
4347 let span_hi = match self.parse_ty() {
4348 Ok(..) => self.span.hi,
4349 Err(ref mut err) => {
4355 let msg = format!("did you mean a single argument type &'a Type, \
4356 or did you mean the comma-separated arguments \
4358 err.span_note(mk_sp(span_lo, span_hi), &msg);
4362 // First parse types.
4363 let (types, returned) = self.parse_seq_to_gt_or_return(
4366 p.forbid_lifetime()?;
4367 if p.look_ahead(1, |t| t == &token::Eq) {
4370 Ok(Some(p.parse_ty_sum()?))
4375 // If we found the `>`, don't continue.
4377 return Ok((lifetimes, types.into_vec(), Vec::new()));
4380 // Then parse type bindings.
4381 let bindings = self.parse_seq_to_gt(
4384 p.forbid_lifetime()?;
4386 let ident = p.parse_ident()?;
4387 p.expect(&token::Eq)?;
4388 let ty = p.parse_ty()?;
4389 let hi = ty.span.hi;
4390 let span = mk_sp(lo, hi);
4391 return Ok(TypeBinding{id: ast::DUMMY_NODE_ID,
4398 Ok((lifetimes, types.into_vec(), bindings.into_vec()))
4401 fn forbid_lifetime(&mut self) -> PResult<'a, ()> {
4402 if self.token.is_lifetime() {
4403 let span = self.span;
4404 return Err(self.diagnostic().struct_span_err(span, "lifetime parameters must be \
4405 declared prior to type parameters"))
4410 /// Parses an optional `where` clause and places it in `generics`.
4413 /// where T : Trait<U, V> + 'b, 'a : 'b
4415 pub fn parse_where_clause(&mut self) -> PResult<'a, ast::WhereClause> {
4416 maybe_whole!(self, NtWhereClause);
4418 let mut where_clause = WhereClause {
4419 id: ast::DUMMY_NODE_ID,
4420 predicates: Vec::new(),
4423 if !self.eat_keyword(keywords::Where) {
4424 return Ok(where_clause);
4427 let mut parsed_something = false;
4429 let lo = self.span.lo;
4431 token::OpenDelim(token::Brace) => {
4435 token::Lifetime(..) => {
4436 let bounded_lifetime =
4437 self.parse_lifetime()?;
4439 self.eat(&token::Colon);
4442 self.parse_lifetimes(token::BinOp(token::Plus))?;
4444 let hi = self.last_span.hi;
4445 let span = mk_sp(lo, hi);
4447 where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4448 ast::WhereRegionPredicate {
4450 lifetime: bounded_lifetime,
4455 parsed_something = true;
4459 let bound_lifetimes = if self.eat_keyword(keywords::For) {
4460 // Higher ranked constraint.
4461 self.expect(&token::Lt)?;
4462 let lifetime_defs = self.parse_lifetime_defs()?;
4469 let bounded_ty = self.parse_ty()?;
4471 if self.eat(&token::Colon) {
4472 let bounds = self.parse_ty_param_bounds(BoundParsingMode::Bare)?;
4473 let hi = self.last_span.hi;
4474 let span = mk_sp(lo, hi);
4476 if bounds.is_empty() {
4478 "each predicate in a `where` clause must have \
4479 at least one bound in it");
4482 where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4483 ast::WhereBoundPredicate {
4485 bound_lifetimes: bound_lifetimes,
4486 bounded_ty: bounded_ty,
4490 parsed_something = true;
4491 } else if self.eat(&token::Eq) {
4492 // let ty = try!(self.parse_ty());
4493 let hi = self.last_span.hi;
4494 let span = mk_sp(lo, hi);
4495 // where_clause.predicates.push(
4496 // ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
4497 // id: ast::DUMMY_NODE_ID,
4499 // path: panic!("NYI"), //bounded_ty,
4502 // parsed_something = true;
4505 "equality constraints are not yet supported \
4506 in where clauses (#20041)");
4508 let last_span = self.last_span;
4509 self.span_err(last_span,
4510 "unexpected token in `where` clause");
4515 if !self.eat(&token::Comma) {
4520 if !parsed_something {
4521 let last_span = self.last_span;
4522 self.span_err(last_span,
4523 "a `where` clause must have at least one predicate \
4530 fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4531 -> PResult<'a, (Vec<Arg> , bool)> {
4533 let mut variadic = false;
4534 let args: Vec<Option<Arg>> =
4535 self.parse_unspanned_seq(
4536 &token::OpenDelim(token::Paren),
4537 &token::CloseDelim(token::Paren),
4538 SeqSep::trailing_allowed(token::Comma),
4540 if p.token == token::DotDotDot {
4543 if p.token != token::CloseDelim(token::Paren) {
4546 "`...` must be last in argument list for variadic function");
4551 "only foreign functions are allowed to be variadic");
4556 match p.parse_arg_general(named_args) {
4557 Ok(arg) => Ok(Some(arg)),
4560 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
4568 let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
4570 if variadic && args.is_empty() {
4572 "variadic function must be declared with at least one named argument");
4575 Ok((args, variadic))
4578 /// Parse the argument list and result type of a function declaration
4579 pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> PResult<'a, P<FnDecl>> {
4581 let (args, variadic) = self.parse_fn_args(true, allow_variadic)?;
4582 let ret_ty = self.parse_ret_ty()?;
4591 /// Returns the parsed optional self argument and whether a self shortcut was used.
4592 fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
4593 let expect_ident = |this: &mut Self| match this.token {
4594 // Preserve hygienic context.
4595 token::Ident(ident) => { this.bump(); codemap::respan(this.last_span, ident) }
4599 // Parse optional self parameter of a method.
4600 // Only a limited set of initial token sequences is considered self parameters, anything
4601 // else is parsed as a normal function parameter list, so some lookahead is required.
4602 let eself_lo = self.span.lo;
4603 let (eself, eself_ident) = match self.token {
4604 token::BinOp(token::And) => {
4610 if self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
4612 (SelfKind::Region(None, Mutability::Immutable), expect_ident(self))
4613 } else if self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
4614 self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
4617 (SelfKind::Region(None, Mutability::Mutable), expect_ident(self))
4618 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4619 self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
4621 let lt = self.parse_lifetime()?;
4622 (SelfKind::Region(Some(lt), Mutability::Immutable), expect_ident(self))
4623 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4624 self.look_ahead(2, |t| t.is_keyword(keywords::Mut)) &&
4625 self.look_ahead(3, |t| t.is_keyword(keywords::SelfValue)) {
4627 let lt = self.parse_lifetime()?;
4629 (SelfKind::Region(Some(lt), Mutability::Mutable), expect_ident(self))
4634 token::BinOp(token::Star) => {
4639 // Emit special error for `self` cases.
4640 if self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
4642 self.span_err(self.span, "cannot pass `self` by raw pointer");
4643 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4644 } else if self.look_ahead(1, |t| t.is_mutability()) &&
4645 self.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
4648 self.span_err(self.span, "cannot pass `self` by raw pointer");
4649 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4654 token::Ident(..) => {
4655 if self.token.is_keyword(keywords::SelfValue) {
4658 let eself_ident = expect_ident(self);
4659 if self.eat(&token::Colon) {
4660 let ty = self.parse_ty_sum()?;
4661 (SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
4663 (SelfKind::Value(Mutability::Immutable), eself_ident)
4665 } else if self.token.is_keyword(keywords::Mut) &&
4666 self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
4670 let eself_ident = expect_ident(self);
4671 if self.eat(&token::Colon) {
4672 let ty = self.parse_ty_sum()?;
4673 (SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
4675 (SelfKind::Value(Mutability::Mutable), eself_ident)
4681 _ => return Ok(None),
4684 let eself = codemap::respan(mk_sp(eself_lo, self.last_span.hi), eself);
4685 Ok(Some(Arg::from_self(eself, eself_ident)))
4688 /// Parse the parameter list and result type of a function that may have a `self` parameter.
4689 fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
4690 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
4692 self.expect(&token::OpenDelim(token::Paren))?;
4694 // Parse optional self argument
4695 let self_arg = self.parse_self_arg()?;
4697 // Parse the rest of the function parameter list.
4698 let sep = SeqSep::trailing_allowed(token::Comma);
4699 let fn_inputs = if let Some(self_arg) = self_arg {
4700 if self.check(&token::CloseDelim(token::Paren)) {
4702 } else if self.eat(&token::Comma) {
4703 let mut fn_inputs = vec![self_arg];
4704 fn_inputs.append(&mut self.parse_seq_to_before_end(
4705 &token::CloseDelim(token::Paren), sep, parse_arg_fn)
4709 return self.unexpected();
4712 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
4715 // Parse closing paren and return type.
4716 self.expect(&token::CloseDelim(token::Paren))?;
4719 output: self.parse_ret_ty()?,
4724 // parse the |arg, arg| header on a lambda
4725 fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
4726 let inputs_captures = {
4727 if self.eat(&token::OrOr) {
4730 self.expect(&token::BinOp(token::Or))?;
4731 self.parse_obsolete_closure_kind()?;
4732 let args = self.parse_seq_to_before_end(
4733 &token::BinOp(token::Or),
4734 SeqSep::trailing_allowed(token::Comma),
4735 |p| p.parse_fn_block_arg()
4741 let output = self.parse_ret_ty()?;
4744 inputs: inputs_captures,
4750 /// Parse the name and optional generic types of a function header.
4751 fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> {
4752 let id = self.parse_ident()?;
4753 let generics = self.parse_generics()?;
4757 fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
4758 node: ItemKind, vis: Visibility,
4759 attrs: Vec<Attribute>) -> P<Item> {
4763 id: ast::DUMMY_NODE_ID,
4770 /// Parse an item-position function declaration.
4771 fn parse_item_fn(&mut self,
4773 constness: Constness,
4775 -> PResult<'a, ItemInfo> {
4776 let (ident, mut generics) = self.parse_fn_header()?;
4777 let decl = self.parse_fn_decl(false)?;
4778 generics.where_clause = self.parse_where_clause()?;
4779 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4780 Ok((ident, ItemKind::Fn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs)))
4783 /// true if we are looking at `const ID`, false for things like `const fn` etc
4784 pub fn is_const_item(&mut self) -> bool {
4785 self.token.is_keyword(keywords::Const) &&
4786 !self.look_ahead(1, |t| t.is_keyword(keywords::Fn)) &&
4787 !self.look_ahead(1, |t| t.is_keyword(keywords::Unsafe))
4790 /// parses all the "front matter" for a `fn` declaration, up to
4791 /// and including the `fn` keyword:
4795 /// - `const unsafe fn`
4798 pub fn parse_fn_front_matter(&mut self)
4799 -> PResult<'a, (ast::Constness, ast::Unsafety, abi::Abi)> {
4800 let is_const_fn = self.eat_keyword(keywords::Const);
4801 let unsafety = self.parse_unsafety()?;
4802 let (constness, unsafety, abi) = if is_const_fn {
4803 (Constness::Const, unsafety, Abi::Rust)
4805 let abi = if self.eat_keyword(keywords::Extern) {
4806 self.parse_opt_abi()?.unwrap_or(Abi::C)
4810 (Constness::NotConst, unsafety, abi)
4812 self.expect_keyword(keywords::Fn)?;
4813 Ok((constness, unsafety, abi))
4816 /// Parse an impl item.
4817 pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> {
4818 maybe_whole!(no_clone_from_p self, NtImplItem);
4820 let mut attrs = self.parse_outer_attributes()?;
4821 let lo = self.span.lo;
4822 let vis = self.parse_visibility(true)?;
4823 let defaultness = self.parse_defaultness()?;
4824 let (name, node) = if self.eat_keyword(keywords::Type) {
4825 let name = self.parse_ident()?;
4826 self.expect(&token::Eq)?;
4827 let typ = self.parse_ty_sum()?;
4828 self.expect(&token::Semi)?;
4829 (name, ast::ImplItemKind::Type(typ))
4830 } else if self.is_const_item() {
4831 self.expect_keyword(keywords::Const)?;
4832 let name = self.parse_ident()?;
4833 self.expect(&token::Colon)?;
4834 let typ = self.parse_ty_sum()?;
4835 self.expect(&token::Eq)?;
4836 let expr = self.parse_expr()?;
4837 self.commit_expr_expecting(&expr, token::Semi)?;
4838 (name, ast::ImplItemKind::Const(typ, expr))
4840 let (name, inner_attrs, node) = self.parse_impl_method(&vis)?;
4841 attrs.extend(inner_attrs);
4846 id: ast::DUMMY_NODE_ID,
4847 span: mk_sp(lo, self.last_span.hi),
4850 defaultness: defaultness,
4856 fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
4858 Visibility::Inherited => (),
4860 let is_macro_rules: bool = match self.token {
4861 token::Ident(sid) => sid.name == intern("macro_rules"),
4865 self.diagnostic().struct_span_err(span, "can't qualify macro_rules \
4866 invocation with `pub`")
4867 .help("did you mean #[macro_export]?")
4870 self.diagnostic().struct_span_err(span, "can't qualify macro \
4871 invocation with `pub`")
4872 .help("try adjusting the macro to put `pub` \
4873 inside the invocation")
4880 /// Parse a method or a macro invocation in a trait impl.
4881 fn parse_impl_method(&mut self, vis: &Visibility)
4882 -> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
4883 // code copied from parse_macro_use_or_failure... abstraction!
4884 if !self.token.is_any_keyword()
4885 && self.look_ahead(1, |t| *t == token::Not)
4886 && (self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
4887 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
4890 let last_span = self.last_span;
4891 self.complain_if_pub_macro(&vis, last_span);
4893 let lo = self.span.lo;
4894 let pth = self.parse_ident_into_path()?;
4895 self.expect(&token::Not)?;
4897 // eat a matched-delimiter token tree:
4898 let delim = self.expect_open_delim()?;
4899 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
4901 |p| p.parse_token_tree())?;
4902 let m_ = Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT };
4903 let m: ast::Mac = codemap::Spanned { node: m_,
4905 self.last_span.hi) };
4906 if delim != token::Brace {
4907 self.expect(&token::Semi)?
4909 Ok((keywords::Invalid.ident(), vec![], ast::ImplItemKind::Macro(m)))
4911 let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
4912 let ident = self.parse_ident()?;
4913 let mut generics = self.parse_generics()?;
4914 let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
4915 generics.where_clause = self.parse_where_clause()?;
4916 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4917 Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
4921 constness: constness,
4927 /// Parse trait Foo { ... }
4928 fn parse_item_trait(&mut self, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
4930 let ident = self.parse_ident()?;
4931 let mut tps = self.parse_generics()?;
4933 // Parse supertrait bounds.
4934 let bounds = self.parse_colon_then_ty_param_bounds(BoundParsingMode::Bare)?;
4936 tps.where_clause = self.parse_where_clause()?;
4938 let meths = self.parse_trait_items()?;
4939 Ok((ident, ItemKind::Trait(unsafety, tps, bounds, meths), None))
4942 /// Parses items implementations variants
4943 /// impl<T> Foo { ... }
4944 /// impl<T> ToString for &'static T { ... }
4945 /// impl Send for .. {}
4946 fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> PResult<'a, ItemInfo> {
4947 let impl_span = self.span;
4949 // First, parse type parameters if necessary.
4950 let mut generics = self.parse_generics()?;
4952 // Special case: if the next identifier that follows is '(', don't
4953 // allow this to be parsed as a trait.
4954 let could_be_trait = self.token != token::OpenDelim(token::Paren);
4956 let neg_span = self.span;
4957 let polarity = if self.eat(&token::Not) {
4958 ast::ImplPolarity::Negative
4960 ast::ImplPolarity::Positive
4964 let mut ty = self.parse_ty_sum()?;
4966 // Parse traits, if necessary.
4967 let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
4968 // New-style trait. Reinterpret the type as a trait.
4970 TyKind::Path(None, ref path) => {
4972 path: (*path).clone(),
4977 self.span_err(ty.span, "not a trait");
4983 ast::ImplPolarity::Negative => {
4984 // This is a negated type implementation
4985 // `impl !MyType {}`, which is not allowed.
4986 self.span_err(neg_span, "inherent implementation can't be negated");
4993 if opt_trait.is_some() && self.eat(&token::DotDot) {
4994 if generics.is_parameterized() {
4995 self.span_err(impl_span, "default trait implementations are not \
4996 allowed to have generics");
4999 self.expect(&token::OpenDelim(token::Brace))?;
5000 self.expect(&token::CloseDelim(token::Brace))?;
5001 Ok((keywords::Invalid.ident(),
5002 ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
5004 if opt_trait.is_some() {
5005 ty = self.parse_ty_sum()?;
5007 generics.where_clause = self.parse_where_clause()?;
5009 self.expect(&token::OpenDelim(token::Brace))?;
5010 let attrs = self.parse_inner_attributes()?;
5012 let mut impl_items = vec![];
5013 while !self.eat(&token::CloseDelim(token::Brace)) {
5014 impl_items.push(self.parse_impl_item()?);
5017 Ok((keywords::Invalid.ident(),
5018 ItemKind::Impl(unsafety, polarity, generics, opt_trait, ty, impl_items),
5023 /// Parse a::B<String,i32>
5024 fn parse_trait_ref(&mut self) -> PResult<'a, TraitRef> {
5026 path: self.parse_path(PathStyle::Type)?,
5027 ref_id: ast::DUMMY_NODE_ID,
5031 fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<ast::LifetimeDef>> {
5032 if self.eat_keyword(keywords::For) {
5033 self.expect(&token::Lt)?;
5034 let lifetime_defs = self.parse_lifetime_defs()?;
5042 /// Parse for<'l> a::B<String,i32>
5043 fn parse_poly_trait_ref(&mut self) -> PResult<'a, PolyTraitRef> {
5044 let lo = self.span.lo;
5045 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
5047 Ok(ast::PolyTraitRef {
5048 bound_lifetimes: lifetime_defs,
5049 trait_ref: self.parse_trait_ref()?,
5050 span: mk_sp(lo, self.last_span.hi),
5054 /// Parse struct Foo { ... }
5055 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
5056 let class_name = self.parse_ident()?;
5057 let mut generics = self.parse_generics()?;
5059 // There is a special case worth noting here, as reported in issue #17904.
5060 // If we are parsing a tuple struct it is the case that the where clause
5061 // should follow the field list. Like so:
5063 // struct Foo<T>(T) where T: Copy;
5065 // If we are parsing a normal record-style struct it is the case
5066 // that the where clause comes before the body, and after the generics.
5067 // So if we look ahead and see a brace or a where-clause we begin
5068 // parsing a record style struct.
5070 // Otherwise if we look ahead and see a paren we parse a tuple-style
5073 let vdata = if self.token.is_keyword(keywords::Where) {
5074 generics.where_clause = self.parse_where_clause()?;
5075 if self.eat(&token::Semi) {
5076 // If we see a: `struct Foo<T> where T: Copy;` style decl.
5077 VariantData::Unit(ast::DUMMY_NODE_ID)
5079 // If we see: `struct Foo<T> where T: Copy { ... }`
5080 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5082 // No `where` so: `struct Foo<T>;`
5083 } else if self.eat(&token::Semi) {
5084 VariantData::Unit(ast::DUMMY_NODE_ID)
5085 // Record-style struct definition
5086 } else if self.token == token::OpenDelim(token::Brace) {
5087 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5088 // Tuple-style struct definition with optional where-clause.
5089 } else if self.token == token::OpenDelim(token::Paren) {
5090 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID);
5091 generics.where_clause = self.parse_where_clause()?;
5092 self.expect(&token::Semi)?;
5095 let token_str = self.this_token_to_string();
5096 return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
5097 name, found `{}`", token_str)))
5100 Ok((class_name, ItemKind::Struct(vdata, generics), None))
5103 pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5104 let mut fields = Vec::new();
5105 if self.eat(&token::OpenDelim(token::Brace)) {
5106 while self.token != token::CloseDelim(token::Brace) {
5107 fields.push(self.parse_struct_decl_field()?);
5112 let token_str = self.this_token_to_string();
5113 return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
5121 pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5122 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
5123 // Unit like structs are handled in parse_item_struct function
5124 let fields = self.parse_unspanned_seq(
5125 &token::OpenDelim(token::Paren),
5126 &token::CloseDelim(token::Paren),
5127 SeqSep::trailing_allowed(token::Comma),
5129 let attrs = p.parse_outer_attributes()?;
5131 let mut vis = p.parse_visibility(false)?;
5132 let ty_is_interpolated =
5133 p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
5134 let mut ty = p.parse_ty_sum()?;
5136 // Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
5137 if vis == Visibility::Public && !ty_is_interpolated &&
5138 p.token != token::Comma && p.token != token::CloseDelim(token::Paren) {
5139 ty = if let TyKind::Paren(ref path_ty) = ty.node {
5140 if let TyKind::Path(None, ref path) = path_ty.node {
5141 vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
5142 Some(p.parse_ty_sum()?)
5151 span: mk_sp(lo, p.span.hi),
5154 id: ast::DUMMY_NODE_ID,
5163 /// Parse a structure field declaration
5164 pub fn parse_single_struct_field(&mut self,
5166 attrs: Vec<Attribute> )
5167 -> PResult<'a, StructField> {
5168 let a_var = self.parse_name_and_ty(vis, attrs)?;
5173 token::CloseDelim(token::Brace) => {}
5175 let span = self.span;
5176 let token_str = self.this_token_to_string();
5177 return Err(self.span_fatal_help(span,
5178 &format!("expected `,`, or `}}`, found `{}`",
5180 "struct fields should be separated by commas"))
5186 /// Parse an element of a struct definition
5187 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
5188 let attrs = self.parse_outer_attributes()?;
5189 let vis = self.parse_visibility(true)?;
5190 self.parse_single_struct_field(vis, attrs)
5193 // If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
5194 fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
5195 let pub_crate = |this: &mut Self| {
5196 let span = this.last_span;
5197 this.expect(&token::CloseDelim(token::Paren))?;
5198 Ok(Visibility::Crate(span))
5201 if !self.eat_keyword(keywords::Pub) {
5202 Ok(Visibility::Inherited)
5203 } else if !allow_path {
5204 // Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
5205 if self.token == token::OpenDelim(token::Paren) &&
5206 self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5207 self.bump(); self.bump();
5210 Ok(Visibility::Public)
5212 } else if !self.eat(&token::OpenDelim(token::Paren)) {
5213 Ok(Visibility::Public)
5214 } else if self.eat_keyword(keywords::Crate) {
5217 let path = self.parse_path(PathStyle::Mod)?;
5218 self.expect(&token::CloseDelim(token::Paren))?;
5219 Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
5223 /// Parse defaultness: DEFAULT or nothing
5224 fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
5225 if self.eat_contextual_keyword(keywords::Default.ident()) {
5226 Ok(Defaultness::Default)
5228 Ok(Defaultness::Final)
5232 /// Given a termination token, parse all of the items in a module
5233 fn parse_mod_items(&mut self, term: &token::Token, inner_lo: BytePos) -> PResult<'a, Mod> {
5234 let mut items = vec![];
5235 while let Some(item) = self.parse_item()? {
5239 if !self.eat(term) {
5240 let token_str = self.this_token_to_string();
5241 return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
5244 let hi = if self.span == codemap::DUMMY_SP {
5251 inner: mk_sp(inner_lo, hi),
5256 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
5257 let id = self.parse_ident()?;
5258 self.expect(&token::Colon)?;
5259 let ty = self.parse_ty_sum()?;
5260 self.expect(&token::Eq)?;
5261 let e = self.parse_expr()?;
5262 self.commit_expr_expecting(&e, token::Semi)?;
5263 let item = match m {
5264 Some(m) => ItemKind::Static(ty, m, e),
5265 None => ItemKind::Const(ty, e),
5267 Ok((id, item, None))
5270 /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5271 fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
5272 let id_span = self.span;
5273 let id = self.parse_ident()?;
5274 if self.check(&token::Semi) {
5276 // This mod is in an external file. Let's go get it!
5277 let (m, attrs) = self.eval_src_mod(id, outer_attrs, id_span)?;
5278 Ok((id, m, Some(attrs)))
5280 self.push_mod_path(id, outer_attrs);
5281 self.expect(&token::OpenDelim(token::Brace))?;
5282 let mod_inner_lo = self.span.lo;
5283 let attrs = self.parse_inner_attributes()?;
5284 let m = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
5285 self.pop_mod_path();
5286 Ok((id, ItemKind::Mod(m), Some(attrs)))
5290 fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
5291 let default_path = self.id_to_interned_str(id);
5292 let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
5294 None => default_path,
5296 self.mod_path_stack.push(file_path)
5299 fn pop_mod_path(&mut self) {
5300 self.mod_path_stack.pop().unwrap();
5303 pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
5304 ::attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&*d))
5307 /// Returns either a path to a module, or .
5308 pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath
5310 let mod_name = id.to_string();
5311 let default_path_str = format!("{}.rs", mod_name);
5312 let secondary_path_str = format!("{}/mod.rs", mod_name);
5313 let default_path = dir_path.join(&default_path_str);
5314 let secondary_path = dir_path.join(&secondary_path_str);
5315 let default_exists = codemap.file_exists(&default_path);
5316 let secondary_exists = codemap.file_exists(&secondary_path);
5318 let result = match (default_exists, secondary_exists) {
5319 (true, false) => Ok(ModulePathSuccess { path: default_path, owns_directory: false }),
5320 (false, true) => Ok(ModulePathSuccess { path: secondary_path, owns_directory: true }),
5321 (false, false) => Err(ModulePathError {
5322 err_msg: format!("file not found for module `{}`", mod_name),
5323 help_msg: format!("name the file either {} or {} inside the directory {:?}",
5326 dir_path.display()),
5328 (true, true) => Err(ModulePathError {
5329 err_msg: format!("file for module `{}` found at both {} and {}",
5332 secondary_path_str),
5333 help_msg: "delete or rename one of them to remove the ambiguity".to_owned(),
5339 path_exists: default_exists || secondary_exists,
5344 fn submod_path(&mut self,
5346 outer_attrs: &[ast::Attribute],
5347 id_sp: Span) -> PResult<'a, ModulePathSuccess> {
5348 let mut prefix = PathBuf::from(self.filename.as_ref().unwrap());
5350 let mut dir_path = prefix;
5351 for part in &self.mod_path_stack {
5352 dir_path.push(&**part);
5355 if let Some(p) = Parser::submod_path_from_attr(outer_attrs, &dir_path) {
5356 return Ok(ModulePathSuccess { path: p, owns_directory: true });
5359 let paths = Parser::default_submod_path(id, &dir_path, self.sess.codemap());
5361 if self.restrictions.contains(Restrictions::NO_NONINLINE_MOD) {
5363 "Cannot declare a non-inline module inside a block unless it has a path attribute";
5364 let mut err = self.diagnostic().struct_span_err(id_sp, msg);
5365 if paths.path_exists {
5366 let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
5368 err.span_note(id_sp, &msg);
5371 } else if !self.owns_directory {
5372 let mut err = self.diagnostic().struct_span_err(id_sp,
5373 "cannot declare a new module at this location");
5374 let this_module = match self.mod_path_stack.last() {
5375 Some(name) => name.to_string(),
5376 None => self.root_module_name.as_ref().unwrap().clone(),
5378 err.span_note(id_sp,
5379 &format!("maybe move this module `{0}` to its own directory \
5382 if paths.path_exists {
5383 err.span_note(id_sp,
5384 &format!("... or maybe `use` the module `{}` instead \
5385 of possibly redeclaring it",
5391 match paths.result {
5392 Ok(succ) => Ok(succ),
5393 Err(err) => Err(self.span_fatal_help(id_sp, &err.err_msg, &err.help_msg)),
5397 /// Read a module from a source file.
5398 fn eval_src_mod(&mut self,
5400 outer_attrs: &[ast::Attribute],
5402 -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
5403 let ModulePathSuccess { path, owns_directory } = self.submod_path(id,
5407 self.eval_src_mod_from_path(path,
5413 fn eval_src_mod_from_path(&mut self,
5415 owns_directory: bool,
5417 id_sp: Span) -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
5418 let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5419 if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
5420 let mut err = String::from("circular modules: ");
5421 let len = included_mod_stack.len();
5422 for p in &included_mod_stack[i.. len] {
5423 err.push_str(&p.to_string_lossy());
5424 err.push_str(" -> ");
5426 err.push_str(&path.to_string_lossy());
5427 return Err(self.span_fatal(id_sp, &err[..]));
5429 included_mod_stack.push(path.clone());
5430 drop(included_mod_stack);
5432 let mut p0 = new_sub_parser_from_file(self.sess,
5438 let mod_inner_lo = p0.span.lo;
5439 let mod_attrs = p0.parse_inner_attributes()?;
5440 let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
5441 self.sess.included_mod_stack.borrow_mut().pop();
5442 Ok((ast::ItemKind::Mod(m0), mod_attrs))
5445 /// Parse a function declaration from a foreign module
5446 fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: BytePos,
5447 attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
5448 self.expect_keyword(keywords::Fn)?;
5450 let (ident, mut generics) = self.parse_fn_header()?;
5451 let decl = self.parse_fn_decl(true)?;
5452 generics.where_clause = self.parse_where_clause()?;
5453 let hi = self.span.hi;
5454 self.expect(&token::Semi)?;
5455 Ok(ast::ForeignItem {
5458 node: ForeignItemKind::Fn(decl, generics),
5459 id: ast::DUMMY_NODE_ID,
5460 span: mk_sp(lo, hi),
5465 /// Parse a static item from a foreign module
5466 fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: BytePos,
5467 attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
5468 self.expect_keyword(keywords::Static)?;
5469 let mutbl = self.eat_keyword(keywords::Mut);
5471 let ident = self.parse_ident()?;
5472 self.expect(&token::Colon)?;
5473 let ty = self.parse_ty_sum()?;
5474 let hi = self.span.hi;
5475 self.expect(&token::Semi)?;
5479 node: ForeignItemKind::Static(ty, mutbl),
5480 id: ast::DUMMY_NODE_ID,
5481 span: mk_sp(lo, hi),
5486 /// Parse extern crate links
5490 /// extern crate foo;
5491 /// extern crate bar as foo;
5492 fn parse_item_extern_crate(&mut self,
5494 visibility: Visibility,
5495 attrs: Vec<Attribute>)
5496 -> PResult<'a, P<Item>> {
5498 let crate_name = self.parse_ident()?;
5499 let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
5500 (Some(crate_name.name), ident)
5504 self.expect(&token::Semi)?;
5506 let last_span = self.last_span;
5510 ItemKind::ExternCrate(maybe_path),
5515 /// Parse `extern` for foreign ABIs
5518 /// `extern` is expected to have been
5519 /// consumed before calling this method
5525 fn parse_item_foreign_mod(&mut self,
5527 opt_abi: Option<abi::Abi>,
5528 visibility: Visibility,
5529 mut attrs: Vec<Attribute>)
5530 -> PResult<'a, P<Item>> {
5531 self.expect(&token::OpenDelim(token::Brace))?;
5533 let abi = opt_abi.unwrap_or(Abi::C);
5535 attrs.extend(self.parse_inner_attributes()?);
5537 let mut foreign_items = vec![];
5538 while let Some(item) = self.parse_foreign_item()? {
5539 foreign_items.push(item);
5541 self.expect(&token::CloseDelim(token::Brace))?;
5543 let last_span = self.last_span;
5544 let m = ast::ForeignMod {
5546 items: foreign_items
5550 keywords::Invalid.ident(),
5551 ItemKind::ForeignMod(m),
5556 /// Parse type Foo = Bar;
5557 fn parse_item_type(&mut self) -> PResult<'a, ItemInfo> {
5558 let ident = self.parse_ident()?;
5559 let mut tps = self.parse_generics()?;
5560 tps.where_clause = self.parse_where_clause()?;
5561 self.expect(&token::Eq)?;
5562 let ty = self.parse_ty_sum()?;
5563 self.expect(&token::Semi)?;
5564 Ok((ident, ItemKind::Ty(ty, tps), None))
5567 /// Parse the part of an "enum" decl following the '{'
5568 fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> {
5569 let mut variants = Vec::new();
5570 let mut all_nullary = true;
5571 let mut any_disr = None;
5572 while self.token != token::CloseDelim(token::Brace) {
5573 let variant_attrs = self.parse_outer_attributes()?;
5574 let vlo = self.span.lo;
5577 let mut disr_expr = None;
5578 let ident = self.parse_ident()?;
5579 if self.check(&token::OpenDelim(token::Brace)) {
5580 // Parse a struct variant.
5581 all_nullary = false;
5582 struct_def = VariantData::Struct(self.parse_record_struct_body()?,
5583 ast::DUMMY_NODE_ID);
5584 } else if self.check(&token::OpenDelim(token::Paren)) {
5585 all_nullary = false;
5586 struct_def = VariantData::Tuple(self.parse_tuple_struct_body()?,
5587 ast::DUMMY_NODE_ID);
5588 } else if self.eat(&token::Eq) {
5589 disr_expr = Some(self.parse_expr()?);
5590 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5591 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5593 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5596 let vr = ast::Variant_ {
5598 attrs: variant_attrs,
5600 disr_expr: disr_expr,
5602 variants.push(spanned(vlo, self.last_span.hi, vr));
5604 if !self.eat(&token::Comma) { break; }
5606 self.expect(&token::CloseDelim(token::Brace))?;
5608 Some(disr_span) if !all_nullary =>
5609 self.span_err(disr_span,
5610 "discriminator values can only be used with a c-like enum"),
5614 Ok(ast::EnumDef { variants: variants })
5617 /// Parse an "enum" declaration
5618 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
5619 let id = self.parse_ident()?;
5620 let mut generics = self.parse_generics()?;
5621 generics.where_clause = self.parse_where_clause()?;
5622 self.expect(&token::OpenDelim(token::Brace))?;
5624 let enum_definition = self.parse_enum_def(&generics)?;
5625 Ok((id, ItemKind::Enum(enum_definition, generics), None))
5628 /// Parses a string as an ABI spec on an extern type or module. Consumes
5629 /// the `extern` keyword, if one is found.
5630 fn parse_opt_abi(&mut self) -> PResult<'a, Option<abi::Abi>> {
5632 token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5634 self.expect_no_suffix(sp, "ABI spec", suf);
5636 match abi::lookup(&s.as_str()) {
5637 Some(abi) => Ok(Some(abi)),
5639 let last_span = self.last_span;
5642 &format!("invalid ABI: expected one of [{}], \
5644 abi::all_names().join(", "),
5655 /// Parse one of the items allowed by the flags.
5656 /// NB: this function no longer parses the items inside an
5658 fn parse_item_(&mut self, attrs: Vec<Attribute>,
5659 macros_allowed: bool, attributes_allowed: bool) -> PResult<'a, Option<P<Item>>> {
5660 let nt_item = match self.token {
5661 token::Interpolated(token::NtItem(ref item)) => {
5662 Some((**item).clone())
5669 let mut attrs = attrs;
5670 mem::swap(&mut item.attrs, &mut attrs);
5671 item.attrs.extend(attrs);
5672 return Ok(Some(P(item)));
5677 let lo = self.span.lo;
5679 let visibility = self.parse_visibility(true)?;
5681 if self.eat_keyword(keywords::Use) {
5683 let item_ = ItemKind::Use(self.parse_view_path()?);
5684 self.expect(&token::Semi)?;
5686 let last_span = self.last_span;
5687 let item = self.mk_item(lo,
5689 keywords::Invalid.ident(),
5693 return Ok(Some(item));
5696 if self.eat_keyword(keywords::Extern) {
5697 if self.eat_keyword(keywords::Crate) {
5698 return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?));
5701 let opt_abi = self.parse_opt_abi()?;
5703 if self.eat_keyword(keywords::Fn) {
5704 // EXTERN FUNCTION ITEM
5705 let abi = opt_abi.unwrap_or(Abi::C);
5706 let (ident, item_, extra_attrs) =
5707 self.parse_item_fn(Unsafety::Normal, Constness::NotConst, abi)?;
5708 let last_span = self.last_span;
5709 let item = self.mk_item(lo,
5714 maybe_append(attrs, extra_attrs));
5715 return Ok(Some(item));
5716 } else if self.check(&token::OpenDelim(token::Brace)) {
5717 return Ok(Some(self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs)?));
5723 if self.eat_keyword(keywords::Static) {
5725 let m = if self.eat_keyword(keywords::Mut) {
5728 Mutability::Immutable
5730 let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?;
5731 let last_span = self.last_span;
5732 let item = self.mk_item(lo,
5737 maybe_append(attrs, extra_attrs));
5738 return Ok(Some(item));
5740 if self.eat_keyword(keywords::Const) {
5741 if self.check_keyword(keywords::Fn)
5742 || (self.check_keyword(keywords::Unsafe)
5743 && self.look_ahead(1, |t| t.is_keyword(keywords::Fn))) {
5744 // CONST FUNCTION ITEM
5745 let unsafety = if self.eat_keyword(keywords::Unsafe) {
5751 let (ident, item_, extra_attrs) =
5752 self.parse_item_fn(unsafety, Constness::Const, Abi::Rust)?;
5753 let last_span = self.last_span;
5754 let item = self.mk_item(lo,
5759 maybe_append(attrs, extra_attrs));
5760 return Ok(Some(item));
5764 if self.eat_keyword(keywords::Mut) {
5765 let last_span = self.last_span;
5766 self.diagnostic().struct_span_err(last_span, "const globals cannot be mutable")
5767 .help("did you mean to declare a static?")
5770 let (ident, item_, extra_attrs) = self.parse_item_const(None)?;
5771 let last_span = self.last_span;
5772 let item = self.mk_item(lo,
5777 maybe_append(attrs, extra_attrs));
5778 return Ok(Some(item));
5780 if self.check_keyword(keywords::Unsafe) &&
5781 self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
5783 // UNSAFE TRAIT ITEM
5784 self.expect_keyword(keywords::Unsafe)?;
5785 self.expect_keyword(keywords::Trait)?;
5786 let (ident, item_, extra_attrs) =
5787 self.parse_item_trait(ast::Unsafety::Unsafe)?;
5788 let last_span = self.last_span;
5789 let item = self.mk_item(lo,
5794 maybe_append(attrs, extra_attrs));
5795 return Ok(Some(item));
5797 if self.check_keyword(keywords::Unsafe) &&
5798 self.look_ahead(1, |t| t.is_keyword(keywords::Impl))
5801 self.expect_keyword(keywords::Unsafe)?;
5802 self.expect_keyword(keywords::Impl)?;
5803 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe)?;
5804 let last_span = self.last_span;
5805 let item = self.mk_item(lo,
5810 maybe_append(attrs, extra_attrs));
5811 return Ok(Some(item));
5813 if self.check_keyword(keywords::Fn) {
5816 let (ident, item_, extra_attrs) =
5817 self.parse_item_fn(Unsafety::Normal, Constness::NotConst, Abi::Rust)?;
5818 let last_span = self.last_span;
5819 let item = self.mk_item(lo,
5824 maybe_append(attrs, extra_attrs));
5825 return Ok(Some(item));
5827 if self.check_keyword(keywords::Unsafe)
5828 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) {
5829 // UNSAFE FUNCTION ITEM
5831 let abi = if self.eat_keyword(keywords::Extern) {
5832 self.parse_opt_abi()?.unwrap_or(Abi::C)
5836 self.expect_keyword(keywords::Fn)?;
5837 let (ident, item_, extra_attrs) =
5838 self.parse_item_fn(Unsafety::Unsafe, Constness::NotConst, abi)?;
5839 let last_span = self.last_span;
5840 let item = self.mk_item(lo,
5845 maybe_append(attrs, extra_attrs));
5846 return Ok(Some(item));
5848 if self.eat_keyword(keywords::Mod) {
5850 let (ident, item_, extra_attrs) =
5851 self.parse_item_mod(&attrs[..])?;
5852 let last_span = self.last_span;
5853 let item = self.mk_item(lo,
5858 maybe_append(attrs, extra_attrs));
5859 return Ok(Some(item));
5861 if self.eat_keyword(keywords::Type) {
5863 let (ident, item_, extra_attrs) = self.parse_item_type()?;
5864 let last_span = self.last_span;
5865 let item = self.mk_item(lo,
5870 maybe_append(attrs, extra_attrs));
5871 return Ok(Some(item));
5873 if self.eat_keyword(keywords::Enum) {
5875 let (ident, item_, extra_attrs) = self.parse_item_enum()?;
5876 let last_span = self.last_span;
5877 let item = self.mk_item(lo,
5882 maybe_append(attrs, extra_attrs));
5883 return Ok(Some(item));
5885 if self.eat_keyword(keywords::Trait) {
5887 let (ident, item_, extra_attrs) =
5888 self.parse_item_trait(ast::Unsafety::Normal)?;
5889 let last_span = self.last_span;
5890 let item = self.mk_item(lo,
5895 maybe_append(attrs, extra_attrs));
5896 return Ok(Some(item));
5898 if self.eat_keyword(keywords::Impl) {
5900 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal)?;
5901 let last_span = self.last_span;
5902 let item = self.mk_item(lo,
5907 maybe_append(attrs, extra_attrs));
5908 return Ok(Some(item));
5910 if self.eat_keyword(keywords::Struct) {
5912 let (ident, item_, extra_attrs) = self.parse_item_struct()?;
5913 let last_span = self.last_span;
5914 let item = self.mk_item(lo,
5919 maybe_append(attrs, extra_attrs));
5920 return Ok(Some(item));
5922 self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
5925 /// Parse a foreign item.
5926 fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
5927 let attrs = self.parse_outer_attributes()?;
5928 let lo = self.span.lo;
5929 let visibility = self.parse_visibility(true)?;
5931 if self.check_keyword(keywords::Static) {
5932 // FOREIGN STATIC ITEM
5933 return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
5935 if self.check_keyword(keywords::Fn) {
5936 // FOREIGN FUNCTION ITEM
5937 return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
5940 // FIXME #5668: this will occur for a macro invocation:
5941 match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
5943 return Err(self.span_fatal(item.span, "macros cannot expand to foreign items"));
5949 /// This is the fall-through for parsing items.
5950 fn parse_macro_use_or_failure(
5952 attrs: Vec<Attribute> ,
5953 macros_allowed: bool,
5954 attributes_allowed: bool,
5956 visibility: Visibility
5957 ) -> PResult<'a, Option<P<Item>>> {
5958 if macros_allowed && !self.token.is_any_keyword()
5959 && self.look_ahead(1, |t| *t == token::Not)
5960 && (self.look_ahead(2, |t| t.is_ident())
5961 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Paren))
5962 || self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))) {
5963 // MACRO INVOCATION ITEM
5965 let last_span = self.last_span;
5966 self.complain_if_pub_macro(&visibility, last_span);
5968 let mac_lo = self.span.lo;
5971 let pth = self.parse_ident_into_path()?;
5972 self.expect(&token::Not)?;
5974 // a 'special' identifier (like what `macro_rules!` uses)
5975 // is optional. We should eventually unify invoc syntax
5977 let id = if self.token.is_ident() {
5980 keywords::Invalid.ident() // no special identifier
5982 // eat a matched-delimiter token tree:
5983 let delim = self.expect_open_delim()?;
5984 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
5986 |p| p.parse_token_tree())?;
5987 // single-variant-enum... :
5988 let m = Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT };
5989 let m: ast::Mac = codemap::Spanned { node: m,
5991 self.last_span.hi) };
5993 if delim != token::Brace {
5994 if !self.eat(&token::Semi) {
5995 let last_span = self.last_span;
5996 self.span_err(last_span,
5997 "macros that expand to items must either \
5998 be surrounded with braces or followed by \
6003 let item_ = ItemKind::Mac(m);
6004 let last_span = self.last_span;
6005 let item = self.mk_item(lo,
6011 return Ok(Some(item));
6014 // FAILURE TO PARSE ITEM
6016 Visibility::Inherited => {}
6018 let last_span = self.last_span;
6019 return Err(self.span_fatal(last_span, "unmatched visibility `pub`"));
6023 if !attributes_allowed && !attrs.is_empty() {
6024 self.expected_item_err(&attrs);
6029 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
6030 let attrs = self.parse_outer_attributes()?;
6031 self.parse_item_(attrs, true, false)
6034 fn parse_path_list_items(&mut self) -> PResult<'a, Vec<ast::PathListItem>> {
6035 self.parse_unspanned_seq(&token::OpenDelim(token::Brace),
6036 &token::CloseDelim(token::Brace),
6037 SeqSep::trailing_allowed(token::Comma), |this| {
6038 let lo = this.span.lo;
6039 let node = if this.eat_keyword(keywords::SelfValue) {
6040 let rename = this.parse_rename()?;
6041 ast::PathListItemKind::Mod { id: ast::DUMMY_NODE_ID, rename: rename }
6043 let ident = this.parse_ident()?;
6044 let rename = this.parse_rename()?;
6045 ast::PathListItemKind::Ident { name: ident, rename: rename, id: ast::DUMMY_NODE_ID }
6047 let hi = this.last_span.hi;
6048 Ok(spanned(lo, hi, node))
6053 fn is_import_coupler(&mut self) -> bool {
6054 self.check(&token::ModSep) &&
6055 self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
6056 *t == token::BinOp(token::Star))
6059 /// Matches ViewPath:
6060 /// MOD_SEP? non_global_path
6061 /// MOD_SEP? non_global_path as IDENT
6062 /// MOD_SEP? non_global_path MOD_SEP STAR
6063 /// MOD_SEP? non_global_path MOD_SEP LBRACE item_seq RBRACE
6064 /// MOD_SEP? LBRACE item_seq RBRACE
6065 fn parse_view_path(&mut self) -> PResult<'a, P<ViewPath>> {
6066 let lo = self.span.lo;
6067 if self.check(&token::OpenDelim(token::Brace)) || self.is_import_coupler() {
6068 // `{foo, bar}` or `::{foo, bar}`
6069 let prefix = ast::Path {
6070 global: self.eat(&token::ModSep),
6071 segments: Vec::new(),
6072 span: mk_sp(lo, self.span.hi),
6074 let items = self.parse_path_list_items()?;
6075 Ok(P(spanned(lo, self.span.hi, ViewPathList(prefix, items))))
6077 let prefix = self.parse_path(PathStyle::Mod)?;
6078 if self.is_import_coupler() {
6079 // `foo::bar::{a, b}` or `foo::bar::*`
6081 if self.check(&token::BinOp(token::Star)) {
6083 Ok(P(spanned(lo, self.span.hi, ViewPathGlob(prefix))))
6085 let items = self.parse_path_list_items()?;
6086 Ok(P(spanned(lo, self.span.hi, ViewPathList(prefix, items))))
6089 // `foo::bar` or `foo::bar as baz`
6090 let rename = self.parse_rename()?.
6091 unwrap_or(prefix.segments.last().unwrap().identifier);
6092 Ok(P(spanned(lo, self.last_span.hi, ViewPathSimple(rename, prefix))))
6097 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
6098 if self.eat_keyword(keywords::As) {
6099 self.parse_ident().map(Some)
6105 /// Parses a source module as a crate. This is the main
6106 /// entry point for the parser.
6107 pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> {
6108 let lo = self.span.lo;
6110 attrs: self.parse_inner_attributes()?,
6111 module: self.parse_mod_items(&token::Eof, lo)?,
6112 config: self.cfg.clone(),
6113 span: mk_sp(lo, self.span.lo),
6114 exported_macros: Vec::new(),
6118 pub fn parse_optional_str(&mut self)
6119 -> Option<(InternedString,
6121 Option<ast::Name>)> {
6122 let ret = match self.token {
6123 token::Literal(token::Str_(s), suf) => {
6124 let s = self.id_to_interned_str(ast::Ident::with_empty_ctxt(s));
6125 (s, ast::StrStyle::Cooked, suf)
6127 token::Literal(token::StrRaw(s, n), suf) => {
6128 let s = self.id_to_interned_str(ast::Ident::with_empty_ctxt(s));
6129 (s, ast::StrStyle::Raw(n), suf)
6137 pub fn parse_str(&mut self) -> PResult<'a, (InternedString, StrStyle)> {
6138 match self.parse_optional_str() {
6139 Some((s, style, suf)) => {
6140 let sp = self.last_span;
6141 self.expect_no_suffix(sp, "string literal", suf);
6144 _ => Err(self.fatal("expected string literal"))