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};
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::{TraitItem, TraitRef};
37 use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
38 use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
39 use ast::{Visibility, WhereClause};
40 use ast::{BinOpKind, UnOp};
42 use codemap::{self, CodeMap, Spanned, spanned, respan};
43 use syntax_pos::{self, Span, Pos, BytePos, mk_sp};
44 use errors::{self, DiagnosticBuilder};
45 use ext::tt::macro_parser;
48 use parse::common::SeqSep;
49 use parse::lexer::TokenAndSpan;
50 use parse::obsolete::ObsoleteSyntax;
51 use parse::token::{self, MatchNt, SubstNt};
52 use parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership};
53 use util::parser::{AssocOp, Fixity};
57 use tokenstream::{self, Delimited, SequenceRepetition, TokenTree};
58 use symbol::{Symbol, keywords};
61 use std::collections::HashSet;
63 use std::path::{Path, PathBuf};
70 flags Restrictions: u8 {
71 const RESTRICTION_STMT_EXPR = 1 << 0,
72 const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
76 type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute> >);
78 /// How to parse a path. There are three different kinds of paths, all of which
79 /// are parsed somewhat differently.
80 #[derive(Copy, Clone, PartialEq)]
82 /// A path with no type parameters, e.g. `foo::bar::Baz`, used in imports or visibilities.
84 /// A path with a lifetime and type parameters, with no double colons
85 /// before the type parameters; e.g. `foo::bar<'a>::Baz<T>`, used in types.
86 /// Paths using this style can be passed into macros expecting `path` nonterminals.
88 /// A path with a lifetime and type parameters with double colons before
89 /// the type parameters; e.g. `foo::bar::<'a>::Baz::<T>`, used in expressions or patterns.
93 #[derive(Clone, Copy, PartialEq)]
94 pub enum SemiColonMode {
99 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
100 /// dropped into the token stream, which happens while parsing the result of
101 /// macro expansion). Placement of these is not as complex as I feared it would
102 /// be. The important thing is to make sure that lookahead doesn't balk at
103 /// `token::Interpolated` tokens.
104 macro_rules! maybe_whole_expr {
106 if let token::Interpolated(nt) = $p.token.clone() {
108 token::NtExpr(ref e) => {
110 return Ok((*e).clone());
112 token::NtPath(ref path) => {
115 let kind = ExprKind::Path(None, (*path).clone());
116 return Ok($p.mk_expr(span.lo, span.hi, kind, ThinVec::new()));
118 token::NtBlock(ref block) => {
121 let kind = ExprKind::Block((*block).clone());
122 return Ok($p.mk_expr(span.lo, span.hi, kind, ThinVec::new()));
130 /// As maybe_whole_expr, but for things other than expressions
131 macro_rules! maybe_whole {
132 ($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
133 if let token::Interpolated(nt) = $p.token.clone() {
134 if let token::$constructor($x) = (*nt).clone() {
142 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
144 if let Some(ref attrs) = rhs {
145 lhs.extend(attrs.iter().cloned())
159 /* ident is handled by common.rs */
161 pub struct Parser<'a> {
162 pub sess: &'a ParseSess,
163 /// the current token:
164 pub token: token::Token,
165 /// the span of the current token:
167 /// the span of the previous token:
169 /// the previous token kind
170 prev_token_kind: PrevTokenKind,
171 pub restrictions: Restrictions,
172 pub quote_depth: usize, // not (yet) related to the quasiquoter
173 parsing_token_tree: bool,
174 /// The set of seen errors about obsolete syntax. Used to suppress
175 /// extra detail when the same error is seen twice
176 pub obsolete_set: HashSet<ObsoleteSyntax>,
177 /// Used to determine the path to externally loaded source files
178 pub directory: Directory,
179 /// Name of the root module this parser originated from. If `None`, then the
180 /// name is not known. This does not change while the parser is descending
181 /// into modules, and sub-parsers have new values for this name.
182 pub root_module_name: Option<String>,
183 pub expected_tokens: Vec<TokenType>,
184 pub tts: Vec<(TokenTree, usize)>,
185 pub desugar_doc_comments: bool,
188 #[derive(PartialEq, Eq, Clone)]
191 Keyword(keywords::Keyword),
196 fn to_string(&self) -> String {
198 TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
199 TokenType::Operator => "an operator".to_string(),
200 TokenType::Keyword(kw) => format!("`{}`", kw.name()),
205 fn is_ident_or_underscore(t: &token::Token) -> bool {
206 t.is_ident() || *t == token::Underscore
209 /// Information about the path to a module.
210 pub struct ModulePath {
212 pub path_exists: bool,
213 pub result: Result<ModulePathSuccess, ModulePathError>,
216 pub struct ModulePathSuccess {
218 pub directory_ownership: DirectoryOwnership,
222 pub struct ModulePathError {
224 pub help_msg: String,
229 AttributesParsed(ThinVec<Attribute>),
230 AlreadyParsed(P<Expr>),
233 impl From<Option<ThinVec<Attribute>>> for LhsExpr {
234 fn from(o: Option<ThinVec<Attribute>>) -> Self {
235 if let Some(attrs) = o {
236 LhsExpr::AttributesParsed(attrs)
238 LhsExpr::NotYetParsed
243 impl From<P<Expr>> for LhsExpr {
244 fn from(expr: P<Expr>) -> Self {
245 LhsExpr::AlreadyParsed(expr)
249 impl<'a> Parser<'a> {
250 pub fn new(sess: &'a ParseSess,
251 tokens: Vec<TokenTree>,
252 directory: Option<Directory>,
253 desugar_doc_comments: bool)
255 let tt = TokenTree::Delimited(syntax_pos::DUMMY_SP, Rc::new(Delimited {
256 delim: token::NoDelim,
257 open_span: syntax_pos::DUMMY_SP,
259 close_span: syntax_pos::DUMMY_SP,
261 let mut parser = Parser {
263 token: token::Underscore,
264 span: syntax_pos::DUMMY_SP,
265 prev_span: syntax_pos::DUMMY_SP,
266 prev_token_kind: PrevTokenKind::Other,
267 restrictions: Restrictions::empty(),
269 parsing_token_tree: false,
270 obsolete_set: HashSet::new(),
271 directory: Directory { path: PathBuf::new(), ownership: DirectoryOwnership::Owned },
272 root_module_name: None,
273 expected_tokens: Vec::new(),
274 tts: if tt.len() > 0 { vec![(tt, 0)] } else { Vec::new() },
275 desugar_doc_comments: desugar_doc_comments,
278 let tok = parser.next_tok();
279 parser.token = tok.tok;
280 parser.span = tok.sp;
281 if let Some(directory) = directory {
282 parser.directory = directory;
283 } else if parser.span != syntax_pos::DUMMY_SP {
284 parser.directory.path = PathBuf::from(sess.codemap().span_to_filename(parser.span));
285 parser.directory.path.pop();
290 fn next_tok(&mut self) -> TokenAndSpan {
292 let tok = if let Some((tts, i)) = self.tts.pop() {
293 let tt = tts.get_tt(i);
294 if i + 1 < tts.len() {
295 self.tts.push((tts, i + 1));
297 if let TokenTree::Token(sp, tok) = tt {
298 TokenAndSpan { tok: tok, sp: sp }
300 self.tts.push((tt, 0));
304 TokenAndSpan { tok: token::Eof, sp: self.span }
308 token::DocComment(name) if self.desugar_doc_comments => {
309 self.tts.push((TokenTree::Token(tok.sp, token::DocComment(name)), 0));
316 /// Convert a token to a string using self's reader
317 pub fn token_to_string(token: &token::Token) -> String {
318 pprust::token_to_string(token)
321 /// Convert the current token to a string using self's reader
322 pub fn this_token_to_string(&self) -> String {
323 Parser::token_to_string(&self.token)
326 pub fn this_token_descr(&self) -> String {
327 let s = self.this_token_to_string();
328 if self.token.is_strict_keyword() {
329 format!("keyword `{}`", s)
330 } else if self.token.is_reserved_keyword() {
331 format!("reserved keyword `{}`", s)
337 pub fn unexpected_last<T>(&self, t: &token::Token) -> PResult<'a, T> {
338 let token_str = Parser::token_to_string(t);
339 Err(self.span_fatal(self.prev_span, &format!("unexpected token: `{}`", token_str)))
342 pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
343 match self.expect_one_of(&[], &[]) {
345 Ok(_) => unreachable!(),
349 /// Expect and consume the token t. Signal an error if
350 /// the next token is not t.
351 pub fn expect(&mut self, t: &token::Token) -> PResult<'a, ()> {
352 if self.expected_tokens.is_empty() {
353 if self.token == *t {
357 let token_str = Parser::token_to_string(t);
358 let this_token_str = self.this_token_to_string();
359 Err(self.fatal(&format!("expected `{}`, found `{}`",
364 self.expect_one_of(unsafe { slice::from_raw_parts(t, 1) }, &[])
368 /// Expect next token to be edible or inedible token. If edible,
369 /// then consume it; if inedible, then return without consuming
370 /// anything. Signal a fatal error if next token is unexpected.
371 pub fn expect_one_of(&mut self,
372 edible: &[token::Token],
373 inedible: &[token::Token]) -> PResult<'a, ()>{
374 fn tokens_to_string(tokens: &[TokenType]) -> String {
375 let mut i = tokens.iter();
376 // This might be a sign we need a connect method on Iterator.
378 .map_or("".to_string(), |t| t.to_string());
379 i.enumerate().fold(b, |mut b, (i, ref a)| {
380 if tokens.len() > 2 && i == tokens.len() - 2 {
382 } else if tokens.len() == 2 && i == tokens.len() - 2 {
387 b.push_str(&a.to_string());
391 if edible.contains(&self.token) {
394 } else if inedible.contains(&self.token) {
395 // leave it in the input
398 let mut expected = edible.iter()
399 .map(|x| TokenType::Token(x.clone()))
400 .chain(inedible.iter().map(|x| TokenType::Token(x.clone())))
401 .chain(self.expected_tokens.iter().cloned())
402 .collect::<Vec<_>>();
403 expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
405 let expect = tokens_to_string(&expected[..]);
406 let actual = self.this_token_to_string();
408 &(if expected.len() > 1 {
409 (format!("expected one of {}, found `{}`",
412 } else if expected.is_empty() {
413 (format!("unexpected token: `{}`",
416 (format!("expected {}, found `{}`",
424 /// returns the span of expr, if it was not interpolated or the span of the interpolated token
425 fn interpolated_or_expr_span(&self,
426 expr: PResult<'a, P<Expr>>)
427 -> PResult<'a, (Span, P<Expr>)> {
429 if self.prev_token_kind == PrevTokenKind::Interpolated {
437 pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
438 self.check_strict_keywords();
439 self.check_reserved_keywords();
446 Err(if self.prev_token_kind == PrevTokenKind::DocComment {
447 self.span_fatal_help(self.prev_span,
448 "found a documentation comment that doesn't document anything",
449 "doc comments must come before what they document, maybe a comment was \
450 intended with `//`?")
452 let mut err = self.fatal(&format!("expected identifier, found `{}`",
453 self.this_token_to_string()));
454 if self.token == token::Underscore {
455 err.note("`_` is a wildcard pattern, not an identifier");
463 /// Check if the next token is `tok`, and return `true` if so.
465 /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
467 pub fn check(&mut self, tok: &token::Token) -> bool {
468 let is_present = self.token == *tok;
469 if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
473 /// Consume token 'tok' if it exists. Returns true if the given
474 /// token was present, false otherwise.
475 pub fn eat(&mut self, tok: &token::Token) -> bool {
476 let is_present = self.check(tok);
477 if is_present { self.bump() }
481 pub fn check_keyword(&mut self, kw: keywords::Keyword) -> bool {
482 self.expected_tokens.push(TokenType::Keyword(kw));
483 self.token.is_keyword(kw)
486 /// If the next token is the given keyword, eat it and return
487 /// true. Otherwise, return false.
488 pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
489 if self.check_keyword(kw) {
497 pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool {
498 if self.token.is_keyword(kw) {
506 pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
507 self.expected_tokens.push(TokenType::Token(token::Ident(ident)));
508 if let token::Ident(ref cur_ident) = self.token {
509 cur_ident.name == ident.name
515 pub fn eat_contextual_keyword(&mut self, ident: Ident) -> bool {
516 if self.check_contextual_keyword(ident) {
524 /// If the given word is not a keyword, signal an error.
525 /// If the next token is not the given word, signal an error.
526 /// Otherwise, eat it.
527 pub fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> {
528 if !self.eat_keyword(kw) {
535 /// Signal an error if the given string is a strict keyword
536 pub fn check_strict_keywords(&mut self) {
537 if self.token.is_strict_keyword() {
538 let token_str = self.this_token_to_string();
539 let span = self.span;
541 &format!("expected identifier, found keyword `{}`",
546 /// Signal an error if the current token is a reserved keyword
547 pub fn check_reserved_keywords(&mut self) {
548 if self.token.is_reserved_keyword() {
549 let token_str = self.this_token_to_string();
550 self.fatal(&format!("`{}` is a reserved keyword", token_str)).emit()
554 /// Expect and consume an `&`. If `&&` is seen, replace it with a single
555 /// `&` and continue. If an `&` is not seen, signal an error.
556 fn expect_and(&mut self) -> PResult<'a, ()> {
557 self.expected_tokens.push(TokenType::Token(token::BinOp(token::And)));
559 token::BinOp(token::And) => {
564 let span = self.span;
565 let lo = span.lo + BytePos(1);
566 Ok(self.bump_with(token::BinOp(token::And), lo, span.hi))
568 _ => self.unexpected()
572 pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
574 None => {/* everything ok */}
576 let text = suf.as_str();
578 self.span_bug(sp, "found empty literal suffix in Some")
580 self.span_err(sp, &format!("{} with a suffix is invalid", kind));
585 /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
586 /// `<` and continue. If a `<` is not seen, return false.
588 /// This is meant to be used when parsing generics on a path to get the
590 fn eat_lt(&mut self) -> bool {
591 self.expected_tokens.push(TokenType::Token(token::Lt));
597 token::BinOp(token::Shl) => {
598 let span = self.span;
599 let lo = span.lo + BytePos(1);
600 self.bump_with(token::Lt, lo, span.hi);
607 fn expect_lt(&mut self) -> PResult<'a, ()> {
615 /// Expect and consume a GT. if a >> is seen, replace it
616 /// with a single > and continue. If a GT is not seen,
618 pub fn expect_gt(&mut self) -> PResult<'a, ()> {
619 self.expected_tokens.push(TokenType::Token(token::Gt));
625 token::BinOp(token::Shr) => {
626 let span = self.span;
627 let lo = span.lo + BytePos(1);
628 Ok(self.bump_with(token::Gt, lo, span.hi))
630 token::BinOpEq(token::Shr) => {
631 let span = self.span;
632 let lo = span.lo + BytePos(1);
633 Ok(self.bump_with(token::Ge, lo, span.hi))
636 let span = self.span;
637 let lo = span.lo + BytePos(1);
638 Ok(self.bump_with(token::Eq, lo, span.hi))
641 let gt_str = Parser::token_to_string(&token::Gt);
642 let this_token_str = self.this_token_to_string();
643 Err(self.fatal(&format!("expected `{}`, found `{}`",
650 pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
651 sep: Option<token::Token>,
653 -> PResult<'a, (Vec<T>, bool)>
654 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
656 let mut v = Vec::new();
657 // This loop works by alternating back and forth between parsing types
658 // and commas. For example, given a string `A, B,>`, the parser would
659 // first parse `A`, then a comma, then `B`, then a comma. After that it
660 // would encounter a `>` and stop. This lets the parser handle trailing
661 // commas in generic parameters, because it can stop either after
662 // parsing a type or after parsing a comma.
664 if self.check(&token::Gt)
665 || self.token == token::BinOp(token::Shr)
666 || self.token == token::Ge
667 || self.token == token::BinOpEq(token::Shr) {
673 Some(result) => v.push(result),
674 None => return Ok((v, true))
677 if let Some(t) = sep.as_ref() {
683 return Ok((v, false));
686 /// Parse a sequence bracketed by '<' and '>', stopping
688 pub fn parse_seq_to_before_gt<T, F>(&mut self,
689 sep: Option<token::Token>,
691 -> PResult<'a, Vec<T>> where
692 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
694 let (result, returned) = self.parse_seq_to_before_gt_or_return(sep,
695 |p| Ok(Some(f(p)?)))?;
700 pub fn parse_seq_to_gt<T, F>(&mut self,
701 sep: Option<token::Token>,
703 -> PResult<'a, Vec<T>> where
704 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
706 let v = self.parse_seq_to_before_gt(sep, f)?;
711 pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
712 sep: Option<token::Token>,
714 -> PResult<'a, (Vec<T>, bool)> where
715 F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
717 let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?;
721 return Ok((v, returned));
724 /// Eat and discard tokens until one of `kets` is encountered. Respects token trees,
725 /// passes through any errors encountered. Used for error recovery.
726 pub fn eat_to_tokens(&mut self, kets: &[&token::Token]) {
727 let handler = self.diagnostic();
729 self.parse_seq_to_before_tokens(kets,
731 |p| p.parse_token_tree(),
732 |mut e| handler.cancel(&mut e));
735 /// Parse a sequence, including the closing delimiter. The function
736 /// f must consume tokens until reaching the next separator or
738 pub fn parse_seq_to_end<T, F>(&mut self,
742 -> PResult<'a, Vec<T>> where
743 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
745 let val = self.parse_seq_to_before_end(ket, sep, f);
750 /// Parse a sequence, not including the closing delimiter. The function
751 /// f must consume tokens until reaching the next separator or
753 pub fn parse_seq_to_before_end<T, F>(&mut self,
758 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>
760 self.parse_seq_to_before_tokens(&[ket], sep, f, |mut e| e.emit())
763 // `fe` is an error handler.
764 fn parse_seq_to_before_tokens<T, F, Fe>(&mut self,
765 kets: &[&token::Token],
770 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
771 Fe: FnMut(DiagnosticBuilder)
773 let mut first: bool = true;
775 while !kets.contains(&&self.token) {
781 if let Err(e) = self.expect(t) {
789 if sep.trailing_sep_allowed && kets.iter().any(|k| self.check(k)) {
805 /// Parse a sequence, including the closing delimiter. The function
806 /// f must consume tokens until reaching the next separator or
808 pub fn parse_unspanned_seq<T, F>(&mut self,
813 -> PResult<'a, Vec<T>> where
814 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
817 let result = self.parse_seq_to_before_end(ket, sep, f);
818 if self.token == *ket {
824 // NB: Do not use this function unless you actually plan to place the
825 // spanned list in the AST.
826 pub fn parse_seq<T, F>(&mut self,
831 -> PResult<'a, Spanned<Vec<T>>> where
832 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
834 let lo = self.span.lo;
836 let result = self.parse_seq_to_before_end(ket, sep, f);
837 let hi = self.span.hi;
839 Ok(spanned(lo, hi, result))
842 /// Advance the parser by one token
843 pub fn bump(&mut self) {
844 if self.prev_token_kind == PrevTokenKind::Eof {
845 // Bumping after EOF is a bad sign, usually an infinite loop.
846 self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
849 self.prev_span = self.span;
851 // Record last token kind for possible error recovery.
852 self.prev_token_kind = match self.token {
853 token::DocComment(..) => PrevTokenKind::DocComment,
854 token::Comma => PrevTokenKind::Comma,
855 token::Interpolated(..) => PrevTokenKind::Interpolated,
856 token::Eof => PrevTokenKind::Eof,
857 _ => PrevTokenKind::Other,
860 let next = self.next_tok();
862 self.token = next.tok;
863 self.expected_tokens.clear();
864 // check after each token
865 self.check_unknown_macro_variable();
868 /// Advance the parser by one token and return the bumped token.
869 pub fn bump_and_get(&mut self) -> token::Token {
870 let old_token = mem::replace(&mut self.token, token::Underscore);
875 /// Advance the parser using provided token as a next one. Use this when
876 /// consuming a part of a token. For example a single `<` from `<<`.
877 pub fn bump_with(&mut self,
881 self.prev_span = mk_sp(self.span.lo, lo);
882 // It would be incorrect to record the kind of the current token, but
883 // fortunately for tokens currently using `bump_with`, the
884 // prev_token_kind will be of no use anyway.
885 self.prev_token_kind = PrevTokenKind::Other;
886 self.span = mk_sp(lo, hi);
888 self.expected_tokens.clear();
891 pub fn look_ahead<R, F>(&mut self, dist: usize, f: F) -> R where
892 F: FnOnce(&token::Token) -> R,
895 return f(&self.token);
897 let mut tok = token::Eof;
898 if let Some(&(ref tts, mut i)) = self.tts.last() {
901 tok = match tts.get_tt(i) {
902 TokenTree::Token(_, tok) => tok,
903 TokenTree::Delimited(_, delimited) => token::OpenDelim(delimited.delim),
904 TokenTree::Sequence(..) => token::Dollar,
910 pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> {
911 self.sess.span_diagnostic.struct_span_fatal(self.span, m)
913 pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
914 self.sess.span_diagnostic.struct_span_fatal(sp, m)
916 pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
917 let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
921 pub fn bug(&self, m: &str) -> ! {
922 self.sess.span_diagnostic.span_bug(self.span, m)
924 pub fn warn(&self, m: &str) {
925 self.sess.span_diagnostic.span_warn(self.span, m)
927 pub fn span_warn(&self, sp: Span, m: &str) {
928 self.sess.span_diagnostic.span_warn(sp, m)
930 pub fn span_err(&self, sp: Span, m: &str) {
931 self.sess.span_diagnostic.span_err(sp, m)
933 pub fn span_err_help(&self, sp: Span, m: &str, h: &str) {
934 let mut err = self.sess.span_diagnostic.mut_span_err(sp, m);
938 pub fn span_bug(&self, sp: Span, m: &str) -> ! {
939 self.sess.span_diagnostic.span_bug(sp, m)
941 pub fn abort_if_errors(&self) {
942 self.sess.span_diagnostic.abort_if_errors();
945 fn cancel(&self, err: &mut DiagnosticBuilder) {
946 self.sess.span_diagnostic.cancel(err)
949 pub fn diagnostic(&self) -> &'a errors::Handler {
950 &self.sess.span_diagnostic
953 /// Is the current token one of the keywords that signals a bare function
955 pub fn token_is_bare_fn_keyword(&mut self) -> bool {
956 self.check_keyword(keywords::Fn) ||
957 self.check_keyword(keywords::Unsafe) ||
958 self.check_keyword(keywords::Extern)
961 pub fn get_lifetime(&mut self) -> ast::Ident {
963 token::Lifetime(ref ident) => *ident,
964 _ => self.bug("not a lifetime"),
968 pub fn parse_for_in_type(&mut self) -> PResult<'a, TyKind> {
970 Parses whatever can come after a `for` keyword in a type.
971 The `for` hasn't been consumed.
979 - for <'lt> [unsafe] [extern "ABI"] fn (S) -> T
980 - for <'lt> path::foo(a, b)
985 let lo = self.span.lo;
987 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
989 // examine next token to decide to do
990 if self.token_is_bare_fn_keyword() {
991 self.parse_ty_bare_fn(lifetime_defs)
993 let hi = self.span.hi;
994 let trait_ref = self.parse_trait_ref()?;
995 let poly_trait_ref = ast::PolyTraitRef { bound_lifetimes: lifetime_defs,
996 trait_ref: trait_ref,
997 span: mk_sp(lo, hi)};
998 let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
999 self.parse_ty_param_bounds()?
1004 Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
1005 .chain(other_bounds)
1007 Ok(ast::TyKind::TraitObject(all_bounds))
1011 pub fn parse_impl_trait_type(&mut self) -> PResult<'a, TyKind> {
1013 Parses whatever can come after a `impl` keyword in a type.
1014 The `impl` has already been consumed.
1017 let bounds = self.parse_ty_param_bounds()?;
1019 if !bounds.iter().any(|b| if let TraitTyParamBound(..) = *b { true } else { false }) {
1020 self.span_err(self.prev_span, "at least one trait must be specified");
1023 Ok(ast::TyKind::ImplTrait(bounds))
1026 pub fn parse_ty_path(&mut self) -> PResult<'a, TyKind> {
1027 Ok(TyKind::Path(None, self.parse_path(PathStyle::Type)?))
1030 /// parse a TyKind::BareFn type:
1031 pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<ast::LifetimeDef>)
1032 -> PResult<'a, TyKind> {
1035 [unsafe] [extern "ABI"] fn (S) -> T
1045 let unsafety = self.parse_unsafety()?;
1046 let abi = if self.eat_keyword(keywords::Extern) {
1047 self.parse_opt_abi()?.unwrap_or(Abi::C)
1052 self.expect_keyword(keywords::Fn)?;
1053 let (inputs, variadic) = self.parse_fn_args(false, true)?;
1054 let ret_ty = self.parse_ret_ty()?;
1055 let decl = P(FnDecl {
1060 Ok(TyKind::BareFn(P(BareFnTy {
1063 lifetimes: lifetime_defs,
1068 pub fn parse_unsafety(&mut self) -> PResult<'a, Unsafety> {
1069 if self.eat_keyword(keywords::Unsafe) {
1070 return Ok(Unsafety::Unsafe);
1072 return Ok(Unsafety::Normal);
1076 /// Parse the items in a trait declaration
1077 pub fn parse_trait_item(&mut self) -> PResult<'a, TraitItem> {
1078 maybe_whole!(self, NtTraitItem, |x| x);
1079 let mut attrs = self.parse_outer_attributes()?;
1080 let lo = self.span.lo;
1082 let (name, node) = if self.eat_keyword(keywords::Type) {
1083 let TyParam {ident, bounds, default, ..} = self.parse_ty_param(vec![])?;
1084 self.expect(&token::Semi)?;
1085 (ident, TraitItemKind::Type(bounds, default))
1086 } else if self.is_const_item() {
1087 self.expect_keyword(keywords::Const)?;
1088 let ident = self.parse_ident()?;
1089 self.expect(&token::Colon)?;
1090 let ty = self.parse_ty()?;
1091 let default = if self.check(&token::Eq) {
1093 let expr = self.parse_expr()?;
1094 self.expect(&token::Semi)?;
1097 self.expect(&token::Semi)?;
1100 (ident, TraitItemKind::Const(ty, default))
1101 } else if self.token.is_path_start() {
1102 // trait item macro.
1103 // code copied from parse_macro_use_or_failure... abstraction!
1104 let lo = self.span.lo;
1105 let pth = self.parse_path(PathStyle::Mod)?;
1106 self.expect(&token::Not)?;
1108 // eat a matched-delimiter token tree:
1109 let delim = self.expect_open_delim()?;
1110 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
1112 |pp| pp.parse_token_tree())?;
1113 if delim != token::Brace {
1114 self.expect(&token::Semi)?
1117 let mac = spanned(lo, self.prev_span.hi, Mac_ { path: pth, tts: tts });
1118 (keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac))
1120 let (constness, unsafety, abi) = match self.parse_fn_front_matter() {
1125 token::Eof => break,
1126 token::CloseDelim(token::Brace) |
1131 token::OpenDelim(token::Brace) => {
1132 self.parse_token_tree()?;
1143 let ident = self.parse_ident()?;
1144 let mut generics = self.parse_generics()?;
1146 let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
1147 // This is somewhat dubious; We don't want to allow
1148 // argument names to be left off if there is a
1150 p.parse_arg_general(false)
1153 generics.where_clause = self.parse_where_clause()?;
1154 let sig = ast::MethodSig {
1156 constness: constness,
1162 let body = match self.token {
1165 debug!("parse_trait_methods(): parsing required method");
1168 token::OpenDelim(token::Brace) => {
1169 debug!("parse_trait_methods(): parsing provided method");
1170 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1171 attrs.extend(inner_attrs.iter().cloned());
1175 let token_str = self.this_token_to_string();
1176 return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
1179 (ident, ast::TraitItemKind::Method(sig, body))
1183 id: ast::DUMMY_NODE_ID,
1187 span: mk_sp(lo, self.prev_span.hi),
1192 /// Parse the items in a trait declaration
1193 pub fn parse_trait_items(&mut self) -> PResult<'a, Vec<TraitItem>> {
1194 self.parse_unspanned_seq(
1195 &token::OpenDelim(token::Brace),
1196 &token::CloseDelim(token::Brace),
1198 |p| -> PResult<'a, TraitItem> {
1199 p.parse_trait_item()
1203 /// Parse a possibly mutable type
1204 pub fn parse_mt(&mut self) -> PResult<'a, MutTy> {
1205 let mutbl = self.parse_mutability()?;
1206 let t = self.parse_ty_no_plus()?;
1207 Ok(MutTy { ty: t, mutbl: mutbl })
1210 /// Parse optional return type [ -> TY ] in function decl
1211 pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
1212 if self.eat(&token::RArrow) {
1213 Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
1215 let pos = self.span.lo;
1216 Ok(FunctionRetTy::Default(mk_sp(pos, pos)))
1221 pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
1222 let lo = self.span.lo;
1223 let lhs = self.parse_ty_no_plus()?;
1225 if !self.eat(&token::BinOp(token::Plus)) {
1229 let mut bounds = self.parse_ty_param_bounds()?;
1231 // In type grammar, `+` is treated like a binary operator,
1232 // and hence both L and R side are required.
1233 if bounds.is_empty() {
1234 let prev_span = self.prev_span;
1235 self.span_err(prev_span,
1236 "at least one type parameter bound \
1237 must be specified");
1240 let mut lhs = lhs.unwrap();
1241 if let TyKind::Paren(ty) = lhs.node {
1242 // We have to accept the first bound in parens for backward compatibility.
1243 // Example: `(Bound) + Bound + Bound`
1246 if let TyKind::Path(None, path) = lhs.node {
1247 let poly_trait_ref = PolyTraitRef {
1248 bound_lifetimes: Vec::new(),
1249 trait_ref: TraitRef { path: path, ref_id: lhs.id },
1252 let poly_trait_ref = TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None);
1253 bounds.insert(0, poly_trait_ref);
1255 let mut err = struct_span_err!(self.sess.span_diagnostic, lhs.span, E0178,
1256 "expected a path on the left-hand side \
1258 pprust::ty_to_string(&lhs));
1259 err.span_label(lhs.span, &format!("expected a path"));
1260 let hi = bounds.iter().map(|x| match *x {
1261 ast::TraitTyParamBound(ref tr, _) => tr.span.hi,
1262 ast::RegionTyParamBound(ref r) => r.span.hi,
1263 }).max_by_key(|x| x.to_usize());
1264 let full_span = hi.map(|hi| Span {
1267 expn_id: lhs.span.expn_id,
1269 match (&lhs.node, full_span) {
1270 (&TyKind::Rptr(ref lifetime, ref mut_ty), Some(full_span)) => {
1271 let ty_str = pprust::to_string(|s| {
1272 use print::pp::word;
1273 use print::pprust::PrintState;
1275 word(&mut s.s, "&")?;
1276 s.print_opt_lifetime(lifetime)?;
1277 s.print_mutability(mut_ty.mutbl)?;
1279 s.print_type(&mut_ty.ty)?;
1280 s.print_bounds(" +", &bounds)?;
1283 err.span_suggestion(full_span, "try adding parentheses (per RFC 438):",
1289 "perhaps you forgot parentheses? (per RFC 438)");
1295 let sp = mk_sp(lo, self.prev_span.hi);
1296 let sum = TyKind::TraitObject(bounds);
1297 Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp}))
1300 /// Parse a type in restricted contexts where `+` is not permitted.
1301 /// Example 1: `&'a TYPE`
1302 /// `+` is prohibited to maintain operator priority (P(+) < P(&)).
1303 /// Example 2: `value1 as TYPE + value2`
1304 /// `+` is prohibited to avoid interactions with expression grammar.
1305 pub fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
1306 maybe_whole!(self, NtTy, |x| x);
1308 let lo = self.span.lo;
1310 let t = if self.check(&token::OpenDelim(token::Paren)) {
1313 // (t) is a parenthesized ty
1314 // (t,) is the type of a tuple with only one field,
1316 let mut ts = vec![];
1317 let mut last_comma = false;
1318 while self.token != token::CloseDelim(token::Paren) {
1319 ts.push(self.parse_ty()?);
1320 if self.check(&token::Comma) {
1329 self.expect(&token::CloseDelim(token::Paren))?;
1330 if ts.len() == 1 && !last_comma {
1331 TyKind::Paren(ts.into_iter().nth(0).unwrap())
1335 } else if self.eat(&token::Not) {
1337 } else if self.check(&token::BinOp(token::Star)) {
1338 // STAR POINTER (bare pointer?)
1340 TyKind::Ptr(self.parse_ptr()?)
1341 } else if self.check(&token::OpenDelim(token::Bracket)) {
1343 self.expect(&token::OpenDelim(token::Bracket))?;
1344 let t = self.parse_ty()?;
1346 // Parse the `; e` in `[ i32; e ]`
1347 // where `e` is a const expression
1348 let t = match self.maybe_parse_fixed_length_of_vec()? {
1349 None => TyKind::Slice(t),
1350 Some(suffix) => TyKind::Array(t, suffix)
1352 self.expect(&token::CloseDelim(token::Bracket))?;
1354 } else if self.check(&token::BinOp(token::And)) ||
1355 self.token == token::AndAnd {
1358 self.parse_borrowed_pointee()?
1359 } else if self.check_keyword(keywords::For) {
1360 self.parse_for_in_type()?
1361 } else if self.eat_keyword(keywords::Impl) {
1362 self.parse_impl_trait_type()?
1363 } else if self.token_is_bare_fn_keyword() {
1365 self.parse_ty_bare_fn(Vec::new())?
1366 } else if self.eat_keyword_noexpect(keywords::Typeof) {
1368 // In order to not be ambiguous, the type must be surrounded by parens.
1369 self.expect(&token::OpenDelim(token::Paren))?;
1370 let e = self.parse_expr()?;
1371 self.expect(&token::CloseDelim(token::Paren))?;
1373 } else if self.eat_lt() {
1376 self.parse_qualified_path(PathStyle::Type)?;
1378 TyKind::Path(Some(qself), path)
1379 } else if self.token.is_path_start() {
1380 let path = self.parse_path(PathStyle::Type)?;
1381 if self.eat(&token::Not) {
1383 let delim = self.expect_open_delim()?;
1384 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
1386 |p| p.parse_token_tree())?;
1387 let hi = self.span.hi;
1388 TyKind::Mac(spanned(lo, hi, Mac_ { path: path, tts: tts }))
1391 TyKind::Path(None, path)
1393 } else if self.eat(&token::Underscore) {
1394 // TYPE TO BE INFERRED
1397 let msg = format!("expected type, found {}", self.this_token_descr());
1398 return Err(self.fatal(&msg));
1401 let sp = mk_sp(lo, self.prev_span.hi);
1402 Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: t, span: sp}))
1405 pub fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
1406 // look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
1407 let opt_lifetime = self.parse_opt_lifetime()?;
1409 let mt = self.parse_mt()?;
1410 return Ok(TyKind::Rptr(opt_lifetime, mt));
1413 pub fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
1414 let mutbl = if self.eat_keyword(keywords::Mut) {
1416 } else if self.eat_keyword(keywords::Const) {
1417 Mutability::Immutable
1419 let span = self.prev_span;
1421 "expected mut or const in raw pointer type (use \
1422 `*mut T` or `*const T` as appropriate)");
1423 Mutability::Immutable
1425 let t = self.parse_ty_no_plus()?;
1426 Ok(MutTy { ty: t, mutbl: mutbl })
1429 pub fn is_named_argument(&mut self) -> bool {
1430 let offset = match self.token {
1431 token::BinOp(token::And) => 1,
1433 _ if self.token.is_keyword(keywords::Mut) => 1,
1437 debug!("parser is_named_argument offset:{}", offset);
1440 is_ident_or_underscore(&self.token)
1441 && self.look_ahead(1, |t| *t == token::Colon)
1443 self.look_ahead(offset, |t| is_ident_or_underscore(t))
1444 && self.look_ahead(offset + 1, |t| *t == token::Colon)
1448 /// This version of parse arg doesn't necessarily require
1449 /// identifier names.
1450 pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
1451 maybe_whole!(self, NtArg, |x| x);
1453 let pat = if require_name || self.is_named_argument() {
1454 debug!("parse_arg_general parse_pat (require_name:{})",
1456 let pat = self.parse_pat()?;
1458 self.expect(&token::Colon)?;
1461 debug!("parse_arg_general ident_to_pat");
1462 let sp = self.prev_span;
1463 let spanned = Spanned { span: sp, node: keywords::Invalid.ident() };
1465 id: ast::DUMMY_NODE_ID,
1466 node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable),
1472 let t = self.parse_ty()?;
1477 id: ast::DUMMY_NODE_ID,
1481 /// Parse a single function argument
1482 pub fn parse_arg(&mut self) -> PResult<'a, Arg> {
1483 self.parse_arg_general(true)
1486 /// Parse an argument in a lambda header e.g. |arg, arg|
1487 pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
1488 let pat = self.parse_pat()?;
1489 let t = if self.eat(&token::Colon) {
1493 id: ast::DUMMY_NODE_ID,
1494 node: TyKind::Infer,
1495 span: mk_sp(self.span.lo, self.span.hi),
1501 id: ast::DUMMY_NODE_ID
1505 pub fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
1506 if self.check(&token::Semi) {
1508 Ok(Some(self.parse_expr()?))
1514 /// Matches token_lit = LIT_INTEGER | ...
1515 pub fn parse_lit_token(&mut self) -> PResult<'a, LitKind> {
1516 let out = match self.token {
1517 token::Interpolated(ref nt) => match **nt {
1518 token::NtExpr(ref v) => match v.node {
1519 ExprKind::Lit(ref lit) => { lit.node.clone() }
1520 _ => { return self.unexpected_last(&self.token); }
1522 _ => { return self.unexpected_last(&self.token); }
1524 token::Literal(lit, suf) => {
1525 let (suffix_illegal, out) = match lit {
1526 token::Byte(i) => (true, LitKind::Byte(parse::byte_lit(&i.as_str()).0)),
1527 token::Char(i) => (true, LitKind::Char(parse::char_lit(&i.as_str()).0)),
1529 // there are some valid suffixes for integer and
1530 // float literals, so all the handling is done
1532 token::Integer(s) => {
1533 let diag = &self.sess.span_diagnostic;
1534 (false, parse::integer_lit(&s.as_str(), suf, diag, self.span))
1536 token::Float(s) => {
1537 let diag = &self.sess.span_diagnostic;
1538 (false, parse::float_lit(&s.as_str(), suf, diag, self.span))
1542 let s = Symbol::intern(&parse::str_lit(&s.as_str()));
1543 (true, LitKind::Str(s, ast::StrStyle::Cooked))
1545 token::StrRaw(s, n) => {
1546 let s = Symbol::intern(&parse::raw_str_lit(&s.as_str()));
1547 (true, LitKind::Str(s, ast::StrStyle::Raw(n)))
1549 token::ByteStr(i) => {
1550 (true, LitKind::ByteStr(parse::byte_str_lit(&i.as_str())))
1552 token::ByteStrRaw(i, _) => {
1553 (true, LitKind::ByteStr(Rc::new(i.to_string().into_bytes())))
1559 self.expect_no_suffix(sp, &format!("{} literal", lit.short_name()), suf)
1564 _ => { return self.unexpected_last(&self.token); }
1571 /// Matches lit = true | false | token_lit
1572 pub fn parse_lit(&mut self) -> PResult<'a, Lit> {
1573 let lo = self.span.lo;
1574 let lit = if self.eat_keyword(keywords::True) {
1576 } else if self.eat_keyword(keywords::False) {
1577 LitKind::Bool(false)
1579 let lit = self.parse_lit_token()?;
1582 Ok(codemap::Spanned { node: lit, span: mk_sp(lo, self.prev_span.hi) })
1585 /// matches '-' lit | lit
1586 pub fn parse_pat_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
1587 let minus_lo = self.span.lo;
1588 let minus_present = self.eat(&token::BinOp(token::Minus));
1589 let lo = self.span.lo;
1590 let literal = P(self.parse_lit()?);
1591 let hi = self.prev_span.hi;
1592 let expr = self.mk_expr(lo, hi, ExprKind::Lit(literal), ThinVec::new());
1595 let minus_hi = self.prev_span.hi;
1596 let unary = self.mk_unary(UnOp::Neg, expr);
1597 Ok(self.mk_expr(minus_lo, minus_hi, unary, ThinVec::new()))
1603 pub fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> {
1605 token::Ident(sid) if self.token.is_path_segment_keyword() => {
1609 _ => self.parse_ident(),
1613 /// Parses qualified path.
1615 /// Assumes that the leading `<` has been parsed already.
1617 /// Qualifed paths are a part of the universal function call
1620 /// `qualified_path = <type [as trait_ref]>::path`
1622 /// See `parse_path` for `mode` meaning.
1627 /// `<T as U>::F::a::<S>`
1628 pub fn parse_qualified_path(&mut self, mode: PathStyle)
1629 -> PResult<'a, (QSelf, ast::Path)> {
1630 let span = self.prev_span;
1631 let self_type = self.parse_ty()?;
1632 let mut path = if self.eat_keyword(keywords::As) {
1633 self.parse_path(PathStyle::Type)?
1643 position: path.segments.len()
1646 self.expect(&token::Gt)?;
1647 self.expect(&token::ModSep)?;
1649 let segments = match mode {
1650 PathStyle::Type => {
1651 self.parse_path_segments_without_colons()?
1653 PathStyle::Expr => {
1654 self.parse_path_segments_with_colons()?
1657 self.parse_path_segments_without_types()?
1660 path.segments.extend(segments);
1662 path.span.hi = self.prev_span.hi;
1667 /// Parses a path and optional type parameter bounds, depending on the
1668 /// mode. The `mode` parameter determines whether lifetimes, types, and/or
1669 /// bounds are permitted and whether `::` must precede type parameter
1671 pub fn parse_path(&mut self, mode: PathStyle) -> PResult<'a, ast::Path> {
1672 maybe_whole!(self, NtPath, |x| x);
1674 let lo = self.span.lo;
1675 let is_global = self.eat(&token::ModSep);
1677 // Parse any number of segments and bound sets. A segment is an
1678 // identifier followed by an optional lifetime and a set of types.
1679 // A bound set is a set of type parameter bounds.
1680 let mut segments = match mode {
1681 PathStyle::Type => {
1682 self.parse_path_segments_without_colons()?
1684 PathStyle::Expr => {
1685 self.parse_path_segments_with_colons()?
1688 self.parse_path_segments_without_types()?
1693 segments.insert(0, ast::PathSegment::crate_root());
1696 // Assemble the span.
1697 let span = mk_sp(lo, self.prev_span.hi);
1699 // Assemble the result.
1707 /// - `a::b<T,U>::c<V,W>`
1708 /// - `a::b<T,U>::c(V) -> W`
1709 /// - `a::b<T,U>::c(V)`
1710 pub fn parse_path_segments_without_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
1711 let mut segments = Vec::new();
1713 // First, parse an identifier.
1714 let identifier = self.parse_path_segment_ident()?;
1716 if self.check(&token::ModSep) && self.look_ahead(1, |t| *t == token::Lt) {
1718 let prev_span = self.prev_span;
1720 let mut err = self.diagnostic().struct_span_err(prev_span,
1721 "unexpected token: `::`");
1723 "use `<...>` instead of `::<...>` if you meant to specify type arguments");
1727 // Parse types, optionally.
1728 let parameters = if self.eat_lt() {
1729 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?;
1730 ast::AngleBracketedParameterData {
1731 lifetimes: lifetimes,
1735 } else if self.eat(&token::OpenDelim(token::Paren)) {
1736 let lo = self.prev_span.lo;
1738 let inputs = self.parse_seq_to_end(
1739 &token::CloseDelim(token::Paren),
1740 SeqSep::trailing_allowed(token::Comma),
1743 let output_ty = if self.eat(&token::RArrow) {
1744 Some(self.parse_ty_no_plus()?)
1749 let hi = self.prev_span.hi;
1751 Some(P(ast::PathParameters::Parenthesized(ast::ParenthesizedParameterData {
1752 span: mk_sp(lo, hi),
1760 // Assemble and push the result.
1761 segments.push(ast::PathSegment { identifier: identifier, parameters: parameters });
1763 // Continue only if we see a `::`
1764 if !self.eat(&token::ModSep) {
1765 return Ok(segments);
1771 /// - `a::b::<T,U>::c`
1772 pub fn parse_path_segments_with_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> {
1773 let mut segments = Vec::new();
1775 // First, parse an identifier.
1776 let identifier = self.parse_path_segment_ident()?;
1778 // If we do not see a `::`, stop.
1779 if !self.eat(&token::ModSep) {
1780 segments.push(identifier.into());
1781 return Ok(segments);
1784 // Check for a type segment.
1786 // Consumed `a::b::<`, go look for types
1787 let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?;
1788 segments.push(ast::PathSegment {
1789 identifier: identifier,
1790 parameters: ast::AngleBracketedParameterData {
1791 lifetimes: lifetimes,
1797 // Consumed `a::b::<T,U>`, check for `::` before proceeding
1798 if !self.eat(&token::ModSep) {
1799 return Ok(segments);
1802 // Consumed `a::`, go look for `b`
1803 segments.push(identifier.into());
1810 pub fn parse_path_segments_without_types(&mut self)
1811 -> PResult<'a, Vec<ast::PathSegment>> {
1812 let mut segments = Vec::new();
1814 // First, parse an identifier.
1815 let identifier = self.parse_path_segment_ident()?;
1817 // Assemble and push the result.
1818 segments.push(identifier.into());
1820 // If we do not see a `::` or see `::{`/`::*`, stop.
1821 if !self.check(&token::ModSep) || self.is_import_coupler() {
1822 return Ok(segments);
1829 /// parses 0 or 1 lifetime
1830 pub fn parse_opt_lifetime(&mut self) -> PResult<'a, Option<ast::Lifetime>> {
1832 token::Lifetime(..) => {
1833 Ok(Some(self.parse_lifetime()?))
1841 /// Parses a single lifetime
1842 /// Matches lifetime = LIFETIME
1843 pub fn parse_lifetime(&mut self) -> PResult<'a, ast::Lifetime> {
1845 token::Lifetime(i) => {
1846 let span = self.span;
1848 return Ok(ast::Lifetime {
1849 id: ast::DUMMY_NODE_ID,
1855 return Err(self.fatal("expected a lifetime name"));
1860 /// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
1861 /// lifetime [':' lifetimes]`
1863 /// If `followed_by_ty_params` is None, then we are in a context
1864 /// where only lifetime parameters are allowed, and thus we should
1865 /// error if we encounter attributes after the bound lifetimes.
1867 /// If `followed_by_ty_params` is Some(r), then there may be type
1868 /// parameter bindings after the lifetimes, so we should pass
1869 /// along the parsed attributes to be attached to the first such
1871 pub fn parse_lifetime_defs(&mut self,
1872 followed_by_ty_params: Option<&mut Vec<ast::Attribute>>)
1873 -> PResult<'a, Vec<ast::LifetimeDef>>
1875 let mut res = Vec::new();
1877 let attrs = self.parse_outer_attributes()?;
1879 token::Lifetime(_) => {
1880 let lifetime = self.parse_lifetime()?;
1882 if self.eat(&token::Colon) {
1883 self.parse_lifetimes(token::BinOp(token::Plus))?
1887 res.push(ast::LifetimeDef { attrs: attrs.into(),
1893 if let Some(recv) = followed_by_ty_params {
1894 assert!(recv.is_empty());
1896 debug!("parse_lifetime_defs ret {:?}", res);
1898 } else if !attrs.is_empty() {
1899 let msg = "trailing attribute after lifetime parameters";
1900 return Err(self.fatal(msg));
1906 token::Comma => { self.bump();}
1907 token::Gt => { return Ok(res); }
1908 token::BinOp(token::Shr) => { return Ok(res); }
1910 let this_token_str = self.this_token_to_string();
1911 let msg = format!("expected `,` or `>` after lifetime \
1914 return Err(self.fatal(&msg[..]));
1920 /// matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty
1921 /// one too, but putting that in there messes up the grammar....
1923 /// Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by
1924 /// either a comma or `>`. Used when parsing type parameter lists, where we expect something
1925 /// like `<'a, 'b, T>`.
1926 pub fn parse_lifetimes(&mut self, sep: token::Token) -> PResult<'a, Vec<ast::Lifetime>> {
1928 let mut res = Vec::new();
1931 token::Lifetime(_) => {
1932 res.push(self.parse_lifetime()?);
1939 if self.token != sep {
1947 /// Parse mutability (`mut` or nothing).
1948 pub fn parse_mutability(&mut self) -> PResult<'a, Mutability> {
1949 if self.eat_keyword(keywords::Mut) {
1950 Ok(Mutability::Mutable)
1952 Ok(Mutability::Immutable)
1956 pub fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1957 if let token::Literal(token::Integer(name), None) = self.token {
1959 Ok(Ident::with_empty_ctxt(name))
1965 /// Parse ident (COLON expr)?
1966 pub fn parse_field(&mut self) -> PResult<'a, Field> {
1967 let attrs = self.parse_outer_attributes()?;
1968 let lo = self.span.lo;
1971 // Check if a colon exists one ahead. This means we're parsing a fieldname.
1972 let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
1973 let fieldname = self.parse_field_name()?;
1975 hi = self.prev_span.hi;
1976 (fieldname, self.parse_expr()?, false)
1978 let fieldname = self.parse_ident()?;
1979 hi = self.prev_span.hi;
1981 // Mimic `x: x` for the `x` field shorthand.
1982 let path = ast::Path::from_ident(mk_sp(lo, hi), fieldname);
1983 (fieldname, self.mk_expr(lo, hi, ExprKind::Path(None, path), ThinVec::new()), true)
1986 ident: spanned(lo, hi, fieldname),
1987 span: mk_sp(lo, expr.span.hi),
1989 is_shorthand: is_shorthand,
1990 attrs: attrs.into(),
1994 pub fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: ExprKind, attrs: ThinVec<Attribute>)
1997 id: ast::DUMMY_NODE_ID,
1999 span: mk_sp(lo, hi),
2000 attrs: attrs.into(),
2004 pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::ExprKind {
2005 ExprKind::Unary(unop, expr)
2008 pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2009 ExprKind::Binary(binop, lhs, rhs)
2012 pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::ExprKind {
2013 ExprKind::Call(f, args)
2016 fn mk_method_call(&mut self,
2017 ident: ast::SpannedIdent,
2021 ExprKind::MethodCall(ident, tps, args)
2024 pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::ExprKind {
2025 ExprKind::Index(expr, idx)
2028 pub fn mk_range(&mut self,
2029 start: Option<P<Expr>>,
2030 end: Option<P<Expr>>,
2031 limits: RangeLimits)
2032 -> PResult<'a, ast::ExprKind> {
2033 if end.is_none() && limits == RangeLimits::Closed {
2034 Err(self.span_fatal_help(self.span,
2035 "inclusive range with no end",
2036 "inclusive ranges must be bounded at the end \
2037 (`...b` or `a...b`)"))
2039 Ok(ExprKind::Range(start, end, limits))
2043 pub fn mk_field(&mut self, expr: P<Expr>, ident: ast::SpannedIdent) -> ast::ExprKind {
2044 ExprKind::Field(expr, ident)
2047 pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<usize>) -> ast::ExprKind {
2048 ExprKind::TupField(expr, idx)
2051 pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2052 lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2053 ExprKind::AssignOp(binop, lhs, rhs)
2056 pub fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos,
2057 m: Mac_, attrs: ThinVec<Attribute>) -> P<Expr> {
2059 id: ast::DUMMY_NODE_ID,
2060 node: ExprKind::Mac(codemap::Spanned {node: m, span: mk_sp(lo, hi)}),
2061 span: mk_sp(lo, hi),
2066 pub fn mk_lit_u32(&mut self, i: u32, attrs: ThinVec<Attribute>) -> P<Expr> {
2067 let span = &self.span;
2068 let lv_lit = P(codemap::Spanned {
2069 node: LitKind::Int(i as u128, ast::LitIntType::Unsigned(UintTy::U32)),
2074 id: ast::DUMMY_NODE_ID,
2075 node: ExprKind::Lit(lv_lit),
2081 fn expect_open_delim(&mut self) -> PResult<'a, token::DelimToken> {
2082 self.expected_tokens.push(TokenType::Token(token::Gt));
2084 token::OpenDelim(delim) => {
2088 _ => Err(self.fatal("expected open delimiter")),
2092 /// At the bottom (top?) of the precedence hierarchy,
2093 /// parse things like parenthesized exprs,
2094 /// macros, return, etc.
2096 /// NB: This does not parse outer attributes,
2097 /// and is private because it only works
2098 /// correctly if called from parse_dot_or_call_expr().
2099 fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
2100 maybe_whole_expr!(self);
2102 // Outer attributes are already parsed and will be
2103 // added to the return value after the fact.
2105 // Therefore, prevent sub-parser from parsing
2106 // attributes by giving them a empty "already parsed" list.
2107 let mut attrs = ThinVec::new();
2109 let lo = self.span.lo;
2110 let mut hi = self.span.hi;
2114 // Note: when adding new syntax here, don't forget to adjust Token::can_begin_expr().
2116 token::OpenDelim(token::Paren) => {
2119 attrs.extend(self.parse_inner_attributes()?);
2121 // (e) is parenthesized e
2122 // (e,) is a tuple with only one field, e
2123 let mut es = vec![];
2124 let mut trailing_comma = false;
2125 while self.token != token::CloseDelim(token::Paren) {
2126 es.push(self.parse_expr()?);
2127 self.expect_one_of(&[], &[token::Comma, token::CloseDelim(token::Paren)])?;
2128 if self.check(&token::Comma) {
2129 trailing_comma = true;
2133 trailing_comma = false;
2139 hi = self.prev_span.hi;
2140 return if es.len() == 1 && !trailing_comma {
2141 Ok(self.mk_expr(lo, hi, ExprKind::Paren(es.into_iter().nth(0).unwrap()), attrs))
2143 Ok(self.mk_expr(lo, hi, ExprKind::Tup(es), attrs))
2146 token::OpenDelim(token::Brace) => {
2147 return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
2149 token::BinOp(token::Or) | token::OrOr => {
2150 let lo = self.span.lo;
2151 return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
2153 token::OpenDelim(token::Bracket) => {
2156 attrs.extend(self.parse_inner_attributes()?);
2158 if self.check(&token::CloseDelim(token::Bracket)) {
2161 ex = ExprKind::Array(Vec::new());
2164 let first_expr = self.parse_expr()?;
2165 if self.check(&token::Semi) {
2166 // Repeating array syntax: [ 0; 512 ]
2168 let count = self.parse_expr()?;
2169 self.expect(&token::CloseDelim(token::Bracket))?;
2170 ex = ExprKind::Repeat(first_expr, count);
2171 } else if self.check(&token::Comma) {
2172 // Vector with two or more elements.
2174 let remaining_exprs = self.parse_seq_to_end(
2175 &token::CloseDelim(token::Bracket),
2176 SeqSep::trailing_allowed(token::Comma),
2177 |p| Ok(p.parse_expr()?)
2179 let mut exprs = vec![first_expr];
2180 exprs.extend(remaining_exprs);
2181 ex = ExprKind::Array(exprs);
2183 // Vector with one element.
2184 self.expect(&token::CloseDelim(token::Bracket))?;
2185 ex = ExprKind::Array(vec![first_expr]);
2188 hi = self.prev_span.hi;
2193 self.parse_qualified_path(PathStyle::Expr)?;
2195 return Ok(self.mk_expr(lo, hi, ExprKind::Path(Some(qself), path), attrs));
2197 if self.eat_keyword(keywords::Move) {
2198 let lo = self.prev_span.lo;
2199 return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
2201 if self.eat_keyword(keywords::If) {
2202 return self.parse_if_expr(attrs);
2204 if self.eat_keyword(keywords::For) {
2205 let lo = self.prev_span.lo;
2206 return self.parse_for_expr(None, lo, attrs);
2208 if self.eat_keyword(keywords::While) {
2209 let lo = self.prev_span.lo;
2210 return self.parse_while_expr(None, lo, attrs);
2212 if self.token.is_lifetime() {
2213 let label = Spanned { node: self.get_lifetime(),
2215 let lo = self.span.lo;
2217 self.expect(&token::Colon)?;
2218 if self.eat_keyword(keywords::While) {
2219 return self.parse_while_expr(Some(label), lo, attrs)
2221 if self.eat_keyword(keywords::For) {
2222 return self.parse_for_expr(Some(label), lo, attrs)
2224 if self.eat_keyword(keywords::Loop) {
2225 return self.parse_loop_expr(Some(label), lo, attrs)
2227 return Err(self.fatal("expected `while`, `for`, or `loop` after a label"))
2229 if self.eat_keyword(keywords::Loop) {
2230 let lo = self.prev_span.lo;
2231 return self.parse_loop_expr(None, lo, attrs);
2233 if self.eat_keyword(keywords::Continue) {
2234 let ex = if self.token.is_lifetime() {
2235 let ex = ExprKind::Continue(Some(Spanned{
2236 node: self.get_lifetime(),
2242 ExprKind::Continue(None)
2244 let hi = self.prev_span.hi;
2245 return Ok(self.mk_expr(lo, hi, ex, attrs));
2247 if self.eat_keyword(keywords::Match) {
2248 return self.parse_match_expr(attrs);
2250 if self.eat_keyword(keywords::Unsafe) {
2251 return self.parse_block_expr(
2253 BlockCheckMode::Unsafe(ast::UserProvided),
2256 if self.eat_keyword(keywords::Return) {
2257 if self.token.can_begin_expr() {
2258 let e = self.parse_expr()?;
2260 ex = ExprKind::Ret(Some(e));
2262 ex = ExprKind::Ret(None);
2264 } else if self.eat_keyword(keywords::Break) {
2265 let lt = if self.token.is_lifetime() {
2266 let spanned_lt = Spanned {
2267 node: self.get_lifetime(),
2275 let e = if self.token.can_begin_expr()
2276 && !(self.token == token::OpenDelim(token::Brace)
2277 && self.restrictions.contains(
2278 Restrictions::RESTRICTION_NO_STRUCT_LITERAL)) {
2279 Some(self.parse_expr()?)
2283 ex = ExprKind::Break(lt, e);
2284 hi = self.prev_span.hi;
2285 } else if self.token.is_keyword(keywords::Let) {
2286 // Catch this syntax error here, instead of in `check_strict_keywords`, so
2287 // that we can explicitly mention that let is not to be used as an expression
2288 let mut db = self.fatal("expected expression, found statement (`let`)");
2289 db.note("variable declaration using `let` is a statement");
2291 } else if self.token.is_path_start() {
2292 let pth = self.parse_path(PathStyle::Expr)?;
2294 // `!`, as an operator, is prefix, so we know this isn't that
2295 if self.eat(&token::Not) {
2296 // MACRO INVOCATION expression
2297 let delim = self.expect_open_delim()?;
2298 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
2300 |p| p.parse_token_tree())?;
2301 let hi = self.prev_span.hi;
2302 return Ok(self.mk_mac_expr(lo, hi, Mac_ { path: pth, tts: tts }, attrs));
2304 if self.check(&token::OpenDelim(token::Brace)) {
2305 // This is a struct literal, unless we're prohibited
2306 // from parsing struct literals here.
2307 let prohibited = self.restrictions.contains(
2308 Restrictions::RESTRICTION_NO_STRUCT_LITERAL
2311 return self.parse_struct_expr(lo, pth, attrs);
2316 ex = ExprKind::Path(None, pth);
2318 match self.parse_lit() {
2321 ex = ExprKind::Lit(P(lit));
2324 self.cancel(&mut err);
2325 let msg = format!("expected expression, found {}",
2326 self.this_token_descr());
2327 return Err(self.fatal(&msg));
2334 return Ok(self.mk_expr(lo, hi, ex, attrs));
2337 fn parse_struct_expr(&mut self, lo: BytePos, pth: ast::Path, mut attrs: ThinVec<Attribute>)
2338 -> PResult<'a, P<Expr>> {
2340 let mut fields = Vec::new();
2341 let mut base = None;
2343 attrs.extend(self.parse_inner_attributes()?);
2345 while self.token != token::CloseDelim(token::Brace) {
2346 if self.eat(&token::DotDot) {
2347 match self.parse_expr() {
2353 self.recover_stmt();
2359 match self.parse_field() {
2360 Ok(f) => fields.push(f),
2363 self.recover_stmt();
2368 match self.expect_one_of(&[token::Comma],
2369 &[token::CloseDelim(token::Brace)]) {
2373 self.recover_stmt();
2379 let hi = self.span.hi;
2380 self.expect(&token::CloseDelim(token::Brace))?;
2381 return Ok(self.mk_expr(lo, hi, ExprKind::Struct(pth, fields, base), attrs));
2384 fn parse_or_use_outer_attributes(&mut self,
2385 already_parsed_attrs: Option<ThinVec<Attribute>>)
2386 -> PResult<'a, ThinVec<Attribute>> {
2387 if let Some(attrs) = already_parsed_attrs {
2390 self.parse_outer_attributes().map(|a| a.into())
2394 /// Parse a block or unsafe block
2395 pub fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode,
2396 outer_attrs: ThinVec<Attribute>)
2397 -> PResult<'a, P<Expr>> {
2399 self.expect(&token::OpenDelim(token::Brace))?;
2401 let mut attrs = outer_attrs;
2402 attrs.extend(self.parse_inner_attributes()?);
2404 let blk = self.parse_block_tail(lo, blk_mode)?;
2405 return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), attrs));
2408 /// parse a.b or a(13) or a[4] or just a
2409 pub fn parse_dot_or_call_expr(&mut self,
2410 already_parsed_attrs: Option<ThinVec<Attribute>>)
2411 -> PResult<'a, P<Expr>> {
2412 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
2414 let b = self.parse_bottom_expr();
2415 let (span, b) = self.interpolated_or_expr_span(b)?;
2416 self.parse_dot_or_call_expr_with(b, span.lo, attrs)
2419 pub fn parse_dot_or_call_expr_with(&mut self,
2422 mut attrs: ThinVec<Attribute>)
2423 -> PResult<'a, P<Expr>> {
2424 // Stitch the list of outer attributes onto the return value.
2425 // A little bit ugly, but the best way given the current code
2427 self.parse_dot_or_call_expr_with_(e0, lo)
2429 expr.map(|mut expr| {
2430 attrs.extend::<Vec<_>>(expr.attrs.into());
2433 ExprKind::If(..) | ExprKind::IfLet(..) => {
2434 if !expr.attrs.is_empty() {
2435 // Just point to the first attribute in there...
2436 let span = expr.attrs[0].span;
2439 "attributes are not yet allowed on `if` \
2450 // Assuming we have just parsed `.foo` (i.e., a dot and an ident), continue
2451 // parsing into an expression.
2452 fn parse_dot_suffix(&mut self,
2455 self_value: P<Expr>,
2457 -> PResult<'a, P<Expr>> {
2458 let (_, tys, bindings) = if self.eat(&token::ModSep) {
2460 self.parse_generic_values_after_lt()?
2462 (Vec::new(), Vec::new(), Vec::new())
2465 if !bindings.is_empty() {
2466 let prev_span = self.prev_span;
2467 self.span_err(prev_span, "type bindings are only permitted on trait paths");
2470 Ok(match self.token {
2471 // expr.f() method call.
2472 token::OpenDelim(token::Paren) => {
2473 let mut es = self.parse_unspanned_seq(
2474 &token::OpenDelim(token::Paren),
2475 &token::CloseDelim(token::Paren),
2476 SeqSep::trailing_allowed(token::Comma),
2477 |p| Ok(p.parse_expr()?)
2479 let hi = self.prev_span.hi;
2481 es.insert(0, self_value);
2482 let id = spanned(ident_span.lo, ident_span.hi, ident);
2483 let nd = self.mk_method_call(id, tys, es);
2484 self.mk_expr(lo, hi, nd, ThinVec::new())
2488 if !tys.is_empty() {
2489 let prev_span = self.prev_span;
2490 self.span_err(prev_span,
2491 "field expressions may not \
2492 have type parameters");
2495 let id = spanned(ident_span.lo, ident_span.hi, ident);
2496 let field = self.mk_field(self_value, id);
2497 self.mk_expr(lo, ident_span.hi, field, ThinVec::new())
2502 fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: BytePos) -> PResult<'a, P<Expr>> {
2507 while self.eat(&token::Question) {
2508 let hi = self.prev_span.hi;
2509 e = self.mk_expr(lo, hi, ExprKind::Try(e), ThinVec::new());
2513 if self.eat(&token::Dot) {
2515 token::Ident(i) => {
2516 let dot_pos = self.prev_span.hi;
2520 e = self.parse_dot_suffix(i, mk_sp(dot_pos, hi), e, lo)?;
2522 token::Literal(token::Integer(n), suf) => {
2525 // A tuple index may not have a suffix
2526 self.expect_no_suffix(sp, "tuple index", suf);
2528 let dot = self.prev_span.hi;
2532 let index = n.as_str().parse::<usize>().ok();
2535 let id = spanned(dot, hi, n);
2536 let field = self.mk_tup_field(e, id);
2537 e = self.mk_expr(lo, hi, field, ThinVec::new());
2540 let prev_span = self.prev_span;
2541 self.span_err(prev_span, "invalid tuple or tuple struct index");
2545 token::Literal(token::Float(n), _suf) => {
2547 let prev_span = self.prev_span;
2548 let fstr = n.as_str();
2549 let mut err = self.diagnostic().struct_span_err(prev_span,
2550 &format!("unexpected token: `{}`", n));
2551 if fstr.chars().all(|x| "0123456789.".contains(x)) {
2552 let float = match fstr.parse::<f64>().ok() {
2556 err.help(&format!("try parenthesizing the first index; e.g., `(foo.{}){}`",
2557 float.trunc() as usize,
2558 format!(".{}", fstr.splitn(2, ".").last().unwrap())));
2564 // FIXME Could factor this out into non_fatal_unexpected or something.
2565 let actual = self.this_token_to_string();
2566 self.span_err(self.span, &format!("unexpected token: `{}`", actual));
2568 let dot_pos = self.prev_span.hi;
2569 e = self.parse_dot_suffix(keywords::Invalid.ident(),
2570 mk_sp(dot_pos, dot_pos),
2576 if self.expr_is_complete(&e) { break; }
2579 token::OpenDelim(token::Paren) => {
2580 let es = self.parse_unspanned_seq(
2581 &token::OpenDelim(token::Paren),
2582 &token::CloseDelim(token::Paren),
2583 SeqSep::trailing_allowed(token::Comma),
2584 |p| Ok(p.parse_expr()?)
2586 hi = self.prev_span.hi;
2588 let nd = self.mk_call(e, es);
2589 e = self.mk_expr(lo, hi, nd, ThinVec::new());
2593 // Could be either an index expression or a slicing expression.
2594 token::OpenDelim(token::Bracket) => {
2596 let ix = self.parse_expr()?;
2598 self.expect(&token::CloseDelim(token::Bracket))?;
2599 let index = self.mk_index(e, ix);
2600 e = self.mk_expr(lo, hi, index, ThinVec::new())
2608 // Parse unquoted tokens after a `$` in a token tree
2609 fn parse_unquoted(&mut self) -> PResult<'a, TokenTree> {
2610 let mut sp = self.span;
2611 let name = match self.token {
2615 if self.token == token::OpenDelim(token::Paren) {
2616 let Spanned { node: seq, span: seq_span } = self.parse_seq(
2617 &token::OpenDelim(token::Paren),
2618 &token::CloseDelim(token::Paren),
2620 |p| p.parse_token_tree()
2622 let (sep, repeat) = self.parse_sep_and_kleene_op()?;
2623 let name_num = macro_parser::count_names(&seq);
2624 return Ok(TokenTree::Sequence(mk_sp(sp.lo, seq_span.hi),
2625 Rc::new(SequenceRepetition {
2629 num_captures: name_num
2631 } else if self.token.is_keyword(keywords::Crate) {
2632 let ident = match self.token {
2633 token::Ident(id) => ast::Ident { name: Symbol::intern("$crate"), ..id },
2634 _ => unreachable!(),
2637 return Ok(TokenTree::Token(sp, token::Ident(ident)));
2639 sp = mk_sp(sp.lo, self.span.hi);
2640 self.parse_ident().unwrap_or_else(|mut e| {
2642 keywords::Invalid.ident()
2646 token::SubstNt(name) => {
2652 // continue by trying to parse the `:ident` after `$name`
2653 if self.token == token::Colon &&
2654 self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword()) {
2656 sp = mk_sp(sp.lo, self.span.hi);
2657 let nt_kind = self.parse_ident()?;
2658 Ok(TokenTree::Token(sp, MatchNt(name, nt_kind)))
2660 Ok(TokenTree::Token(sp, SubstNt(name)))
2664 pub fn check_unknown_macro_variable(&mut self) {
2665 if self.quote_depth == 0 && !self.parsing_token_tree {
2667 token::SubstNt(name) =>
2668 self.fatal(&format!("unknown macro variable `{}`", name)).emit(),
2674 /// Parse an optional separator followed by a Kleene-style
2675 /// repetition token (+ or *).
2676 pub fn parse_sep_and_kleene_op(&mut self)
2677 -> PResult<'a, (Option<token::Token>, tokenstream::KleeneOp)> {
2678 fn parse_kleene_op<'a>(parser: &mut Parser<'a>) ->
2679 PResult<'a, Option<tokenstream::KleeneOp>> {
2680 match parser.token {
2681 token::BinOp(token::Star) => {
2683 Ok(Some(tokenstream::KleeneOp::ZeroOrMore))
2685 token::BinOp(token::Plus) => {
2687 Ok(Some(tokenstream::KleeneOp::OneOrMore))
2693 if let Some(kleene_op) = parse_kleene_op(self)? {
2694 return Ok((None, kleene_op));
2697 let separator = self.bump_and_get();
2698 match parse_kleene_op(self)? {
2699 Some(zerok) => Ok((Some(separator), zerok)),
2700 None => return Err(self.fatal("expected `*` or `+`"))
2704 /// parse a single token tree from the input.
2705 pub fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> {
2706 // FIXME #6994: currently, this is too eager. It
2707 // parses token trees but also identifies TokenType::Sequence's
2708 // and token::SubstNt's; it's too early to know yet
2709 // whether something will be a nonterminal or a seq
2712 token::OpenDelim(delim) => {
2713 if self.quote_depth == 0 && self.tts.last().map(|&(_, i)| i == 1).unwrap_or(false) {
2714 let tt = self.tts.pop().unwrap().0;
2719 let parsing_token_tree = ::std::mem::replace(&mut self.parsing_token_tree, true);
2720 let open_span = self.span;
2722 let tts = self.parse_seq_to_before_tokens(&[&token::CloseDelim(token::Brace),
2723 &token::CloseDelim(token::Paren),
2724 &token::CloseDelim(token::Bracket)],
2726 |p| p.parse_token_tree(),
2728 self.parsing_token_tree = parsing_token_tree;
2730 let close_span = self.span;
2733 let span = Span { lo: open_span.lo, ..close_span };
2734 Ok(TokenTree::Delimited(span, Rc::new(Delimited {
2736 open_span: open_span,
2738 close_span: close_span,
2741 token::CloseDelim(_) | token::Eof => unreachable!(),
2742 token::Dollar | token::SubstNt(..) if self.quote_depth > 0 => self.parse_unquoted(),
2743 _ => Ok(TokenTree::Token(self.span, self.bump_and_get())),
2747 // parse a stream of tokens into a list of TokenTree's,
2749 pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
2750 let mut tts = Vec::new();
2751 while self.token != token::Eof {
2752 tts.push(self.parse_token_tree()?);
2757 /// Parse a prefix-unary-operator expr
2758 pub fn parse_prefix_expr(&mut self,
2759 already_parsed_attrs: Option<ThinVec<Attribute>>)
2760 -> PResult<'a, P<Expr>> {
2761 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
2762 let lo = self.span.lo;
2764 // Note: when adding new unary operators, don't forget to adjust Token::can_begin_expr()
2765 let ex = match self.token {
2768 let e = self.parse_prefix_expr(None);
2769 let (span, e) = self.interpolated_or_expr_span(e)?;
2771 self.mk_unary(UnOp::Not, e)
2773 token::BinOp(token::Minus) => {
2775 let e = self.parse_prefix_expr(None);
2776 let (span, e) = self.interpolated_or_expr_span(e)?;
2778 self.mk_unary(UnOp::Neg, e)
2780 token::BinOp(token::Star) => {
2782 let e = self.parse_prefix_expr(None);
2783 let (span, e) = self.interpolated_or_expr_span(e)?;
2785 self.mk_unary(UnOp::Deref, e)
2787 token::BinOp(token::And) | token::AndAnd => {
2789 let m = self.parse_mutability()?;
2790 let e = self.parse_prefix_expr(None);
2791 let (span, e) = self.interpolated_or_expr_span(e)?;
2793 ExprKind::AddrOf(m, e)
2795 token::Ident(..) if self.token.is_keyword(keywords::In) => {
2797 let place = self.parse_expr_res(
2798 Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
2801 let blk = self.parse_block()?;
2802 let span = blk.span;
2804 let blk_expr = self.mk_expr(span.lo, hi, ExprKind::Block(blk), ThinVec::new());
2805 ExprKind::InPlace(place, blk_expr)
2807 token::Ident(..) if self.token.is_keyword(keywords::Box) => {
2809 let e = self.parse_prefix_expr(None);
2810 let (span, e) = self.interpolated_or_expr_span(e)?;
2814 _ => return self.parse_dot_or_call_expr(Some(attrs))
2816 return Ok(self.mk_expr(lo, hi, ex, attrs));
2819 /// Parse an associative expression
2821 /// This parses an expression accounting for associativity and precedence of the operators in
2823 pub fn parse_assoc_expr(&mut self,
2824 already_parsed_attrs: Option<ThinVec<Attribute>>)
2825 -> PResult<'a, P<Expr>> {
2826 self.parse_assoc_expr_with(0, already_parsed_attrs.into())
2829 /// Parse an associative expression with operators of at least `min_prec` precedence
2830 pub fn parse_assoc_expr_with(&mut self,
2833 -> PResult<'a, P<Expr>> {
2834 let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
2837 let attrs = match lhs {
2838 LhsExpr::AttributesParsed(attrs) => Some(attrs),
2841 if self.token == token::DotDot || self.token == token::DotDotDot {
2842 return self.parse_prefix_range_expr(attrs);
2844 self.parse_prefix_expr(attrs)?
2848 if self.expr_is_complete(&lhs) {
2849 // Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
2852 self.expected_tokens.push(TokenType::Operator);
2853 while let Some(op) = AssocOp::from_token(&self.token) {
2855 let lhs_span = if self.prev_token_kind == PrevTokenKind::Interpolated {
2861 let cur_op_span = self.span;
2862 let restrictions = if op.is_assign_like() {
2863 self.restrictions & Restrictions::RESTRICTION_NO_STRUCT_LITERAL
2867 if op.precedence() < min_prec {
2871 if op.is_comparison() {
2872 self.check_no_chained_comparison(&lhs, &op);
2875 if op == AssocOp::As {
2876 let rhs = self.parse_ty_no_plus()?;
2877 let (lo, hi) = (lhs_span.lo, rhs.span.hi);
2878 lhs = self.mk_expr(lo, hi, ExprKind::Cast(lhs, rhs), ThinVec::new());
2880 } else if op == AssocOp::Colon {
2881 let rhs = self.parse_ty_no_plus()?;
2882 let (lo, hi) = (lhs_span.lo, rhs.span.hi);
2883 lhs = self.mk_expr(lo, hi, ExprKind::Type(lhs, rhs), ThinVec::new());
2885 } else if op == AssocOp::DotDot || op == AssocOp::DotDotDot {
2886 // If we didn’t have to handle `x..`/`x...`, it would be pretty easy to
2887 // generalise it to the Fixity::None code.
2889 // We have 2 alternatives here: `x..y`/`x...y` and `x..`/`x...` The other
2890 // two variants are handled with `parse_prefix_range_expr` call above.
2891 let rhs = if self.is_at_start_of_range_notation_rhs() {
2892 Some(self.parse_assoc_expr_with(op.precedence() + 1,
2893 LhsExpr::NotYetParsed)?)
2897 let (lhs_span, rhs_span) = (lhs.span, if let Some(ref x) = rhs {
2902 let limits = if op == AssocOp::DotDot {
2903 RangeLimits::HalfOpen
2908 let r = try!(self.mk_range(Some(lhs), rhs, limits));
2909 lhs = self.mk_expr(lhs_span.lo, rhs_span.hi, r, ThinVec::new());
2913 let rhs = match op.fixity() {
2914 Fixity::Right => self.with_res(
2915 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2917 this.parse_assoc_expr_with(op.precedence(),
2918 LhsExpr::NotYetParsed)
2920 Fixity::Left => self.with_res(
2921 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2923 this.parse_assoc_expr_with(op.precedence() + 1,
2924 LhsExpr::NotYetParsed)
2926 // We currently have no non-associative operators that are not handled above by
2927 // the special cases. The code is here only for future convenience.
2928 Fixity::None => self.with_res(
2929 restrictions - Restrictions::RESTRICTION_STMT_EXPR,
2931 this.parse_assoc_expr_with(op.precedence() + 1,
2932 LhsExpr::NotYetParsed)
2936 let (lo, hi) = (lhs_span.lo, rhs.span.hi);
2938 AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide |
2939 AssocOp::Modulus | AssocOp::LAnd | AssocOp::LOr | AssocOp::BitXor |
2940 AssocOp::BitAnd | AssocOp::BitOr | AssocOp::ShiftLeft | AssocOp::ShiftRight |
2941 AssocOp::Equal | AssocOp::Less | AssocOp::LessEqual | AssocOp::NotEqual |
2942 AssocOp::Greater | AssocOp::GreaterEqual => {
2943 let ast_op = op.to_ast_binop().unwrap();
2944 let binary = self.mk_binary(codemap::respan(cur_op_span, ast_op), lhs, rhs);
2945 self.mk_expr(lo, hi, binary, ThinVec::new())
2948 self.mk_expr(lo, hi, ExprKind::Assign(lhs, rhs), ThinVec::new()),
2950 self.mk_expr(lo, hi, ExprKind::InPlace(lhs, rhs), ThinVec::new()),
2951 AssocOp::AssignOp(k) => {
2953 token::Plus => BinOpKind::Add,
2954 token::Minus => BinOpKind::Sub,
2955 token::Star => BinOpKind::Mul,
2956 token::Slash => BinOpKind::Div,
2957 token::Percent => BinOpKind::Rem,
2958 token::Caret => BinOpKind::BitXor,
2959 token::And => BinOpKind::BitAnd,
2960 token::Or => BinOpKind::BitOr,
2961 token::Shl => BinOpKind::Shl,
2962 token::Shr => BinOpKind::Shr,
2964 let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
2965 self.mk_expr(lo, hi, aopexpr, ThinVec::new())
2967 AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotDot => {
2968 self.bug("As, Colon, DotDot or DotDotDot branch reached")
2972 if op.fixity() == Fixity::None { break }
2977 /// Produce an error if comparison operators are chained (RFC #558).
2978 /// We only need to check lhs, not rhs, because all comparison ops
2979 /// have same precedence and are left-associative
2980 fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: &AssocOp) {
2981 debug_assert!(outer_op.is_comparison());
2983 ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
2984 // respan to include both operators
2985 let op_span = mk_sp(op.span.lo, self.span.hi);
2986 let mut err = self.diagnostic().struct_span_err(op_span,
2987 "chained comparison operators require parentheses");
2988 if op.node == BinOpKind::Lt && *outer_op == AssocOp::Greater {
2990 "use `::<...>` instead of `<...>` if you meant to specify type arguments");
2998 /// Parse prefix-forms of range notation: `..expr`, `..`, `...expr`
2999 fn parse_prefix_range_expr(&mut self,
3000 already_parsed_attrs: Option<ThinVec<Attribute>>)
3001 -> PResult<'a, P<Expr>> {
3002 debug_assert!(self.token == token::DotDot || self.token == token::DotDotDot);
3003 let tok = self.token.clone();
3004 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
3005 let lo = self.span.lo;
3006 let mut hi = self.span.hi;
3008 let opt_end = if self.is_at_start_of_range_notation_rhs() {
3009 // RHS must be parsed with more associativity than the dots.
3010 let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
3011 Some(self.parse_assoc_expr_with(next_prec,
3012 LhsExpr::NotYetParsed)
3020 let limits = if tok == token::DotDot {
3021 RangeLimits::HalfOpen
3026 let r = try!(self.mk_range(None,
3029 Ok(self.mk_expr(lo, hi, r, attrs))
3032 fn is_at_start_of_range_notation_rhs(&self) -> bool {
3033 if self.token.can_begin_expr() {
3034 // parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
3035 if self.token == token::OpenDelim(token::Brace) {
3036 return !self.restrictions.contains(Restrictions::RESTRICTION_NO_STRUCT_LITERAL);
3044 /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3045 pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3046 if self.check_keyword(keywords::Let) {
3047 return self.parse_if_let_expr(attrs);
3049 let lo = self.prev_span.lo;
3050 let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3051 let thn = self.parse_block()?;
3052 let mut els: Option<P<Expr>> = None;
3053 let mut hi = thn.span.hi;
3054 if self.eat_keyword(keywords::Else) {
3055 let elexpr = self.parse_else_expr()?;
3056 hi = elexpr.span.hi;
3059 Ok(self.mk_expr(lo, hi, ExprKind::If(cond, thn, els), attrs))
3062 /// Parse an 'if let' expression ('if' token already eaten)
3063 pub fn parse_if_let_expr(&mut self, attrs: ThinVec<Attribute>)
3064 -> PResult<'a, P<Expr>> {
3065 let lo = self.prev_span.lo;
3066 self.expect_keyword(keywords::Let)?;
3067 let pat = self.parse_pat()?;
3068 self.expect(&token::Eq)?;
3069 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3070 let thn = self.parse_block()?;
3071 let (hi, els) = if self.eat_keyword(keywords::Else) {
3072 let expr = self.parse_else_expr()?;
3073 (expr.span.hi, Some(expr))
3077 Ok(self.mk_expr(lo, hi, ExprKind::IfLet(pat, expr, thn, els), attrs))
3080 // `move |args| expr`
3081 pub fn parse_lambda_expr(&mut self,
3083 capture_clause: CaptureBy,
3084 attrs: ThinVec<Attribute>)
3085 -> PResult<'a, P<Expr>>
3087 let decl = self.parse_fn_block_decl()?;
3088 let decl_hi = self.prev_span.hi;
3089 let body = match decl.output {
3090 FunctionRetTy::Default(_) => self.parse_expr()?,
3092 // If an explicit return type is given, require a
3093 // block to appear (RFC 968).
3094 let body_lo = self.span.lo;
3095 self.parse_block_expr(body_lo, BlockCheckMode::Default, ThinVec::new())?
3102 ExprKind::Closure(capture_clause, decl, body, mk_sp(lo, decl_hi)),
3106 // `else` token already eaten
3107 pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
3108 if self.eat_keyword(keywords::If) {
3109 return self.parse_if_expr(ThinVec::new());
3111 let blk = self.parse_block()?;
3112 return Ok(self.mk_expr(blk.span.lo, blk.span.hi, ExprKind::Block(blk), ThinVec::new()));
3116 /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3117 pub fn parse_for_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3119 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3120 // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3122 let pat = self.parse_pat()?;
3123 self.expect_keyword(keywords::In)?;
3124 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3125 let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
3126 attrs.extend(iattrs);
3128 let hi = self.prev_span.hi;
3130 Ok(self.mk_expr(span_lo, hi,
3131 ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
3135 /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3136 pub fn parse_while_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3138 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3139 if self.token.is_keyword(keywords::Let) {
3140 return self.parse_while_let_expr(opt_ident, span_lo, attrs);
3142 let cond = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3143 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3144 attrs.extend(iattrs);
3145 let hi = body.span.hi;
3146 return Ok(self.mk_expr(span_lo, hi, ExprKind::While(cond, body, opt_ident),
3150 /// Parse a 'while let' expression ('while' token already eaten)
3151 pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3153 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3154 self.expect_keyword(keywords::Let)?;
3155 let pat = self.parse_pat()?;
3156 self.expect(&token::Eq)?;
3157 let expr = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)?;
3158 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3159 attrs.extend(iattrs);
3160 let hi = body.span.hi;
3161 return Ok(self.mk_expr(span_lo, hi, ExprKind::WhileLet(pat, expr, body, opt_ident), attrs));
3164 // parse `loop {...}`, `loop` token already eaten
3165 pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3167 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3168 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3169 attrs.extend(iattrs);
3170 let hi = body.span.hi;
3171 Ok(self.mk_expr(span_lo, hi, ExprKind::Loop(body, opt_ident), attrs))
3174 // `match` token already eaten
3175 fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3176 let match_span = self.prev_span;
3177 let lo = self.prev_span.lo;
3178 let discriminant = self.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL,
3180 if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) {
3181 if self.token == token::Token::Semi {
3182 e.span_note(match_span, "did you mean to remove this `match` keyword?");
3186 attrs.extend(self.parse_inner_attributes()?);
3188 let mut arms: Vec<Arm> = Vec::new();
3189 while self.token != token::CloseDelim(token::Brace) {
3190 match self.parse_arm() {
3191 Ok(arm) => arms.push(arm),
3193 // Recover by skipping to the end of the block.
3195 self.recover_stmt();
3196 let hi = self.span.hi;
3197 if self.token == token::CloseDelim(token::Brace) {
3200 return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
3204 let hi = self.span.hi;
3206 return Ok(self.mk_expr(lo, hi, ExprKind::Match(discriminant, arms), attrs));
3209 pub fn parse_arm(&mut self) -> PResult<'a, Arm> {
3210 maybe_whole!(self, NtArm, |x| x);
3212 let attrs = self.parse_outer_attributes()?;
3213 let pats = self.parse_pats()?;
3214 let mut guard = None;
3215 if self.eat_keyword(keywords::If) {
3216 guard = Some(self.parse_expr()?);
3218 self.expect(&token::FatArrow)?;
3219 let expr = self.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR, None)?;
3222 !classify::expr_is_simple_block(&expr)
3223 && self.token != token::CloseDelim(token::Brace);
3226 self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])?;
3228 self.eat(&token::Comma);
3239 /// Parse an expression
3240 pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
3241 self.parse_expr_res(Restrictions::empty(), None)
3244 /// Evaluate the closure with restrictions in place.
3246 /// After the closure is evaluated, restrictions are reset.
3247 pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
3248 where F: FnOnce(&mut Self) -> T
3250 let old = self.restrictions;
3251 self.restrictions = r;
3253 self.restrictions = old;
3258 /// Parse an expression, subject to the given restrictions
3259 pub fn parse_expr_res(&mut self, r: Restrictions,
3260 already_parsed_attrs: Option<ThinVec<Attribute>>)
3261 -> PResult<'a, P<Expr>> {
3262 self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
3265 /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3266 fn parse_initializer(&mut self) -> PResult<'a, Option<P<Expr>>> {
3267 if self.check(&token::Eq) {
3269 Ok(Some(self.parse_expr()?))
3275 /// Parse patterns, separated by '|' s
3276 fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
3277 let mut pats = Vec::new();
3279 pats.push(self.parse_pat()?);
3280 if self.check(&token::BinOp(token::Or)) { self.bump();}
3281 else { return Ok(pats); }
3285 fn parse_pat_tuple_elements(&mut self, unary_needs_comma: bool)
3286 -> PResult<'a, (Vec<P<Pat>>, Option<usize>)> {
3287 let mut fields = vec![];
3288 let mut ddpos = None;
3290 while !self.check(&token::CloseDelim(token::Paren)) {
3291 if ddpos.is_none() && self.eat(&token::DotDot) {
3292 ddpos = Some(fields.len());
3293 if self.eat(&token::Comma) {
3294 // `..` needs to be followed by `)` or `, pat`, `..,)` is disallowed.
3295 fields.push(self.parse_pat()?);
3297 } else if ddpos.is_some() && self.eat(&token::DotDot) {
3298 // Emit a friendly error, ignore `..` and continue parsing
3299 self.span_err(self.prev_span, "`..` can only be used once per \
3300 tuple or tuple struct pattern");
3302 fields.push(self.parse_pat()?);
3305 if !self.check(&token::CloseDelim(token::Paren)) ||
3306 (unary_needs_comma && fields.len() == 1 && ddpos.is_none()) {
3307 self.expect(&token::Comma)?;
3314 fn parse_pat_vec_elements(
3316 ) -> PResult<'a, (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>)> {
3317 let mut before = Vec::new();
3318 let mut slice = None;
3319 let mut after = Vec::new();
3320 let mut first = true;
3321 let mut before_slice = true;
3323 while self.token != token::CloseDelim(token::Bracket) {
3327 self.expect(&token::Comma)?;
3329 if self.token == token::CloseDelim(token::Bracket)
3330 && (before_slice || !after.is_empty()) {
3336 if self.check(&token::DotDot) {
3339 if self.check(&token::Comma) ||
3340 self.check(&token::CloseDelim(token::Bracket)) {
3341 slice = Some(P(ast::Pat {
3342 id: ast::DUMMY_NODE_ID,
3343 node: PatKind::Wild,
3346 before_slice = false;
3352 let subpat = self.parse_pat()?;
3353 if before_slice && self.check(&token::DotDot) {
3355 slice = Some(subpat);
3356 before_slice = false;
3357 } else if before_slice {
3358 before.push(subpat);
3364 Ok((before, slice, after))
3367 /// Parse the fields of a struct-like pattern
3368 fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<codemap::Spanned<ast::FieldPat>>, bool)> {
3369 let mut fields = Vec::new();
3370 let mut etc = false;
3371 let mut first = true;
3372 while self.token != token::CloseDelim(token::Brace) {
3376 self.expect(&token::Comma)?;
3377 // accept trailing commas
3378 if self.check(&token::CloseDelim(token::Brace)) { break }
3381 let attrs = self.parse_outer_attributes()?;
3382 let lo = self.span.lo;
3385 if self.check(&token::DotDot) {
3387 if self.token != token::CloseDelim(token::Brace) {
3388 let token_str = self.this_token_to_string();
3389 return Err(self.fatal(&format!("expected `{}`, found `{}`", "}",
3396 // Check if a colon exists one ahead. This means we're parsing a fieldname.
3397 let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
3398 // Parsing a pattern of the form "fieldname: pat"
3399 let fieldname = self.parse_field_name()?;
3401 let pat = self.parse_pat()?;
3403 (pat, fieldname, false)
3405 // Parsing a pattern of the form "(box) (ref) (mut) fieldname"
3406 let is_box = self.eat_keyword(keywords::Box);
3407 let boxed_span_lo = self.span.lo;
3408 let is_ref = self.eat_keyword(keywords::Ref);
3409 let is_mut = self.eat_keyword(keywords::Mut);
3410 let fieldname = self.parse_ident()?;
3411 hi = self.prev_span.hi;
3413 let bind_type = match (is_ref, is_mut) {
3414 (true, true) => BindingMode::ByRef(Mutability::Mutable),
3415 (true, false) => BindingMode::ByRef(Mutability::Immutable),
3416 (false, true) => BindingMode::ByValue(Mutability::Mutable),
3417 (false, false) => BindingMode::ByValue(Mutability::Immutable),
3419 let fieldpath = codemap::Spanned{span:self.prev_span, node:fieldname};
3420 let fieldpat = P(ast::Pat{
3421 id: ast::DUMMY_NODE_ID,
3422 node: PatKind::Ident(bind_type, fieldpath, None),
3423 span: mk_sp(boxed_span_lo, hi),
3426 let subpat = if is_box {
3428 id: ast::DUMMY_NODE_ID,
3429 node: PatKind::Box(fieldpat),
3430 span: mk_sp(lo, hi),
3435 (subpat, fieldname, true)
3438 fields.push(codemap::Spanned { span: mk_sp(lo, hi),
3439 node: ast::FieldPat {
3442 is_shorthand: is_shorthand,
3443 attrs: attrs.into(),
3447 return Ok((fields, etc));
3450 fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
3451 if self.token.is_path_start() {
3452 let lo = self.span.lo;
3453 let (qself, path) = if self.eat_lt() {
3454 // Parse a qualified path
3456 self.parse_qualified_path(PathStyle::Expr)?;
3459 // Parse an unqualified path
3460 (None, self.parse_path(PathStyle::Expr)?)
3462 let hi = self.prev_span.hi;
3463 Ok(self.mk_expr(lo, hi, ExprKind::Path(qself, path), ThinVec::new()))
3465 self.parse_pat_literal_maybe_minus()
3469 /// Parse a pattern.
3470 pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
3471 maybe_whole!(self, NtPat, |x| x);
3473 let lo = self.span.lo;
3476 token::Underscore => {
3479 pat = PatKind::Wild;
3481 token::BinOp(token::And) | token::AndAnd => {
3482 // Parse &pat / &mut pat
3484 let mutbl = self.parse_mutability()?;
3485 if let token::Lifetime(ident) = self.token {
3486 return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
3488 let subpat = self.parse_pat()?;
3489 pat = PatKind::Ref(subpat, mutbl);
3491 token::OpenDelim(token::Paren) => {
3492 // Parse (pat,pat,pat,...) as tuple pattern
3494 let (fields, ddpos) = self.parse_pat_tuple_elements(true)?;
3495 self.expect(&token::CloseDelim(token::Paren))?;
3496 pat = PatKind::Tuple(fields, ddpos);
3498 token::OpenDelim(token::Bracket) => {
3499 // Parse [pat,pat,...] as slice pattern
3501 let (before, slice, after) = self.parse_pat_vec_elements()?;
3502 self.expect(&token::CloseDelim(token::Bracket))?;
3503 pat = PatKind::Slice(before, slice, after);
3505 // At this point, token != _, &, &&, (, [
3506 _ => if self.eat_keyword(keywords::Mut) {
3507 // Parse mut ident @ pat
3508 pat = self.parse_pat_ident(BindingMode::ByValue(Mutability::Mutable))?;
3509 } else if self.eat_keyword(keywords::Ref) {
3510 // Parse ref ident @ pat / ref mut ident @ pat
3511 let mutbl = self.parse_mutability()?;
3512 pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
3513 } else if self.eat_keyword(keywords::Box) {
3515 let subpat = self.parse_pat()?;
3516 pat = PatKind::Box(subpat);
3517 } else if self.token.is_ident() && !self.token.is_any_keyword() &&
3518 self.look_ahead(1, |t| match *t {
3519 token::OpenDelim(token::Paren) | token::OpenDelim(token::Brace) |
3520 token::DotDotDot | token::ModSep | token::Not => false,
3523 // Parse ident @ pat
3524 // This can give false positives and parse nullary enums,
3525 // they are dealt with later in resolve
3526 let binding_mode = BindingMode::ByValue(Mutability::Immutable);
3527 pat = self.parse_pat_ident(binding_mode)?;
3528 } else if self.token.is_path_start() {
3529 // Parse pattern starting with a path
3530 let (qself, path) = if self.eat_lt() {
3531 // Parse a qualified path
3532 let (qself, path) = self.parse_qualified_path(PathStyle::Expr)?;
3535 // Parse an unqualified path
3536 (None, self.parse_path(PathStyle::Expr)?)
3539 token::Not if qself.is_none() => {
3540 // Parse macro invocation
3542 let delim = self.expect_open_delim()?;
3543 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
3545 |p| p.parse_token_tree())?;
3546 let mac = spanned(lo, self.prev_span.hi, Mac_ { path: path, tts: tts });
3547 pat = PatKind::Mac(mac);
3549 token::DotDotDot => {
3551 let hi = self.prev_span.hi;
3553 self.mk_expr(lo, hi, ExprKind::Path(qself, path), ThinVec::new());
3555 let end = self.parse_pat_range_end()?;
3556 pat = PatKind::Range(begin, end);
3558 token::OpenDelim(token::Brace) => {
3559 if qself.is_some() {
3560 return Err(self.fatal("unexpected `{` after qualified path"));
3562 // Parse struct pattern
3564 let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
3566 self.recover_stmt();
3570 pat = PatKind::Struct(path, fields, etc);
3572 token::OpenDelim(token::Paren) => {
3573 if qself.is_some() {
3574 return Err(self.fatal("unexpected `(` after qualified path"));
3576 // Parse tuple struct or enum pattern
3578 let (fields, ddpos) = self.parse_pat_tuple_elements(false)?;
3579 self.expect(&token::CloseDelim(token::Paren))?;
3580 pat = PatKind::TupleStruct(path, fields, ddpos)
3582 _ => pat = PatKind::Path(qself, path),
3585 // Try to parse everything else as literal with optional minus
3586 match self.parse_pat_literal_maybe_minus() {
3588 if self.eat(&token::DotDotDot) {
3589 let end = self.parse_pat_range_end()?;
3590 pat = PatKind::Range(begin, end);
3592 pat = PatKind::Lit(begin);
3596 self.cancel(&mut err);
3597 let msg = format!("expected pattern, found {}", self.this_token_descr());
3598 return Err(self.fatal(&msg));
3604 let hi = self.prev_span.hi;
3606 id: ast::DUMMY_NODE_ID,
3608 span: mk_sp(lo, hi),
3612 /// Parse ident or ident @ pat
3613 /// used by the copy foo and ref foo patterns to give a good
3614 /// error message when parsing mistakes like ref foo(a,b)
3615 fn parse_pat_ident(&mut self,
3616 binding_mode: ast::BindingMode)
3617 -> PResult<'a, PatKind> {
3618 let ident = self.parse_ident()?;
3619 let prev_span = self.prev_span;
3620 let name = codemap::Spanned{span: prev_span, node: ident};
3621 let sub = if self.eat(&token::At) {
3622 Some(self.parse_pat()?)
3627 // just to be friendly, if they write something like
3629 // we end up here with ( as the current token. This shortly
3630 // leads to a parse error. Note that if there is no explicit
3631 // binding mode then we do not end up here, because the lookahead
3632 // will direct us over to parse_enum_variant()
3633 if self.token == token::OpenDelim(token::Paren) {
3634 return Err(self.span_fatal(
3636 "expected identifier, found enum pattern"))
3639 Ok(PatKind::Ident(binding_mode, name, sub))
3642 /// Parse a local variable declaration
3643 fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
3644 let lo = self.span.lo;
3645 let pat = self.parse_pat()?;
3648 if self.eat(&token::Colon) {
3649 ty = Some(self.parse_ty()?);
3651 let init = self.parse_initializer()?;
3656 id: ast::DUMMY_NODE_ID,
3657 span: mk_sp(lo, self.prev_span.hi),
3662 /// Parse a structure field
3663 fn parse_name_and_ty(&mut self,
3666 attrs: Vec<Attribute>)
3667 -> PResult<'a, StructField> {
3668 let name = self.parse_ident()?;
3669 self.expect(&token::Colon)?;
3670 let ty = self.parse_ty()?;
3672 span: mk_sp(lo, self.prev_span.hi),
3675 id: ast::DUMMY_NODE_ID,
3681 /// Emit an expected item after attributes error.
3682 fn expected_item_err(&self, attrs: &[Attribute]) {
3683 let message = match attrs.last() {
3684 Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment",
3685 _ => "expected item after attributes",
3688 self.span_err(self.prev_span, message);
3691 /// Parse a statement. This stops just before trailing semicolons on everything but items.
3692 /// e.g. a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
3693 pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
3694 Ok(self.parse_stmt_(true))
3697 // Eat tokens until we can be relatively sure we reached the end of the
3698 // statement. This is something of a best-effort heuristic.
3700 // We terminate when we find an unmatched `}` (without consuming it).
3701 fn recover_stmt(&mut self) {
3702 self.recover_stmt_(SemiColonMode::Ignore)
3704 // If `break_on_semi` is `Break`, then we will stop consuming tokens after
3705 // finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
3706 // approximate - it can mean we break too early due to macros, but that
3707 // shoud only lead to sub-optimal recovery, not inaccurate parsing).
3708 fn recover_stmt_(&mut self, break_on_semi: SemiColonMode) {
3709 let mut brace_depth = 0;
3710 let mut bracket_depth = 0;
3711 debug!("recover_stmt_ enter loop");
3713 debug!("recover_stmt_ loop {:?}", self.token);
3715 token::OpenDelim(token::DelimToken::Brace) => {
3719 token::OpenDelim(token::DelimToken::Bracket) => {
3723 token::CloseDelim(token::DelimToken::Brace) => {
3724 if brace_depth == 0 {
3725 debug!("recover_stmt_ return - close delim {:?}", self.token);
3731 token::CloseDelim(token::DelimToken::Bracket) => {
3733 if bracket_depth < 0 {
3739 debug!("recover_stmt_ return - Eof");
3744 if break_on_semi == SemiColonMode::Break &&
3746 bracket_depth == 0 {
3747 debug!("recover_stmt_ return - Semi");
3758 fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option<Stmt> {
3759 self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| {
3761 self.recover_stmt_(SemiColonMode::Break);
3766 fn is_union_item(&mut self) -> bool {
3767 self.token.is_keyword(keywords::Union) &&
3768 self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
3771 fn parse_stmt_without_recovery(&mut self,
3772 macro_legacy_warnings: bool)
3773 -> PResult<'a, Option<Stmt>> {
3774 maybe_whole!(self, NtStmt, |x| Some(x));
3776 let attrs = self.parse_outer_attributes()?;
3777 let lo = self.span.lo;
3779 Ok(Some(if self.eat_keyword(keywords::Let) {
3781 id: ast::DUMMY_NODE_ID,
3782 node: StmtKind::Local(self.parse_local(attrs.into())?),
3783 span: mk_sp(lo, self.prev_span.hi),
3785 // Starts like a simple path, but not a union item.
3786 } else if self.token.is_path_start() &&
3787 !self.token.is_qpath_start() &&
3788 !self.is_union_item() {
3789 let pth = self.parse_path(PathStyle::Expr)?;
3791 if !self.eat(&token::Not) {
3792 let expr = if self.check(&token::OpenDelim(token::Brace)) {
3793 self.parse_struct_expr(lo, pth, ThinVec::new())?
3795 let hi = self.prev_span.hi;
3796 self.mk_expr(lo, hi, ExprKind::Path(None, pth), ThinVec::new())
3799 let expr = self.with_res(Restrictions::RESTRICTION_STMT_EXPR, |this| {
3800 let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
3801 this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
3804 return Ok(Some(Stmt {
3805 id: ast::DUMMY_NODE_ID,
3806 node: StmtKind::Expr(expr),
3807 span: mk_sp(lo, self.prev_span.hi),
3811 // it's a macro invocation
3812 let id = match self.token {
3813 token::OpenDelim(_) => keywords::Invalid.ident(), // no special identifier
3814 _ => self.parse_ident()?,
3817 // check that we're pointing at delimiters (need to check
3818 // again after the `if`, because of `parse_ident`
3819 // consuming more tokens).
3820 let delim = match self.token {
3821 token::OpenDelim(delim) => delim,
3823 // we only expect an ident if we didn't parse one
3825 let ident_str = if id.name == keywords::Invalid.name() {
3830 let tok_str = self.this_token_to_string();
3831 return Err(self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
3837 let tts = self.parse_unspanned_seq(
3838 &token::OpenDelim(delim),
3839 &token::CloseDelim(delim),
3841 |p| p.parse_token_tree()
3843 let hi = self.prev_span.hi;
3845 let style = if delim == token::Brace {
3846 MacStmtStyle::Braces
3848 MacStmtStyle::NoBraces
3851 if id.name == keywords::Invalid.name() {
3852 let mac = spanned(lo, hi, Mac_ { path: pth, tts: tts });
3853 let node = if delim == token::Brace ||
3854 self.token == token::Semi || self.token == token::Eof {
3855 StmtKind::Mac(P((mac, style, attrs.into())))
3857 // We used to incorrectly stop parsing macro-expanded statements here.
3858 // If the next token will be an error anyway but could have parsed with the
3859 // earlier behavior, stop parsing here and emit a warning to avoid breakage.
3860 else if macro_legacy_warnings && self.token.can_begin_expr() && match self.token {
3861 // These can continue an expression, so we can't stop parsing and warn.
3862 token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
3863 token::BinOp(token::Minus) | token::BinOp(token::Star) |
3864 token::BinOp(token::And) | token::BinOp(token::Or) |
3865 token::AndAnd | token::OrOr |
3866 token::DotDot | token::DotDotDot => false,
3869 self.warn_missing_semicolon();
3870 StmtKind::Mac(P((mac, style, attrs.into())))
3872 let e = self.mk_mac_expr(lo, hi, mac.node, ThinVec::new());
3873 let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
3874 let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
3878 id: ast::DUMMY_NODE_ID,
3879 span: mk_sp(lo, hi),
3883 // if it has a special ident, it's definitely an item
3885 // Require a semicolon or braces.
3886 if style != MacStmtStyle::Braces {
3887 if !self.eat(&token::Semi) {
3888 self.span_err(self.prev_span,
3889 "macros that expand to items must \
3890 either be surrounded with braces or \
3891 followed by a semicolon");
3895 id: ast::DUMMY_NODE_ID,
3896 span: mk_sp(lo, hi),
3897 node: StmtKind::Item({
3899 lo, hi, id /*id is good here*/,
3900 ItemKind::Mac(spanned(lo, hi, Mac_ { path: pth, tts: tts })),
3901 Visibility::Inherited,
3907 // FIXME: Bad copy of attrs
3908 let old_directory_ownership =
3909 mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
3910 let item = self.parse_item_(attrs.clone(), false, true)?;
3911 self.directory.ownership = old_directory_ownership;
3914 id: ast::DUMMY_NODE_ID,
3915 span: mk_sp(lo, i.span.hi),
3916 node: StmtKind::Item(i),
3919 let unused_attrs = |attrs: &[_], s: &mut Self| {
3920 if attrs.len() > 0 {
3921 if s.prev_token_kind == PrevTokenKind::DocComment {
3922 s.span_err_help(s.prev_span,
3923 "found a documentation comment that doesn't document anything",
3924 "doc comments must come before what they document, maybe a \
3925 comment was intended with `//`?");
3927 s.span_err(s.span, "expected statement after outer attribute");
3932 // Do not attempt to parse an expression if we're done here.
3933 if self.token == token::Semi {
3934 unused_attrs(&attrs, self);
3939 if self.token == token::CloseDelim(token::Brace) {
3940 unused_attrs(&attrs, self);
3944 // Remainder are line-expr stmts.
3945 let e = self.parse_expr_res(
3946 Restrictions::RESTRICTION_STMT_EXPR, Some(attrs.into()))?;
3948 id: ast::DUMMY_NODE_ID,
3949 span: mk_sp(lo, e.span.hi),
3950 node: StmtKind::Expr(e),
3957 /// Is this expression a successfully-parsed statement?
3958 fn expr_is_complete(&mut self, e: &Expr) -> bool {
3959 self.restrictions.contains(Restrictions::RESTRICTION_STMT_EXPR) &&
3960 !classify::expr_requires_semi_to_be_stmt(e)
3963 /// Parse a block. No inner attrs are allowed.
3964 pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
3965 maybe_whole!(self, NtBlock, |x| x);
3967 let lo = self.span.lo;
3969 if !self.eat(&token::OpenDelim(token::Brace)) {
3971 let tok = self.this_token_to_string();
3972 let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
3974 // Check to see if the user has written something like
3979 // Which is valid in other languages, but not Rust.
3980 match self.parse_stmt_without_recovery(false) {
3982 let mut stmt_span = stmt.span;
3983 // expand the span to include the semicolon, if it exists
3984 if self.eat(&token::Semi) {
3985 stmt_span.hi = self.prev_span.hi;
3987 e.span_help(stmt_span, "try placing this code inside a block");
3990 self.recover_stmt_(SemiColonMode::Break);
3991 self.cancel(&mut e);
3998 self.parse_block_tail(lo, BlockCheckMode::Default)
4001 /// Parse a block. Inner attrs are allowed.
4002 fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
4003 maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
4005 let lo = self.span.lo;
4006 self.expect(&token::OpenDelim(token::Brace))?;
4007 Ok((self.parse_inner_attributes()?,
4008 self.parse_block_tail(lo, BlockCheckMode::Default)?))
4011 /// Parse the rest of a block expression or function body
4012 /// Precondition: already parsed the '{'.
4013 fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> PResult<'a, P<Block>> {
4014 let mut stmts = vec![];
4016 while !self.eat(&token::CloseDelim(token::Brace)) {
4017 if let Some(stmt) = self.parse_full_stmt(false)? {
4019 } else if self.token == token::Eof {
4022 // Found only `;` or `}`.
4029 id: ast::DUMMY_NODE_ID,
4031 span: mk_sp(lo, self.prev_span.hi),
4035 /// Parse a statement, including the trailing semicolon.
4036 pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option<Stmt>> {
4037 let mut stmt = match self.parse_stmt_(macro_legacy_warnings) {
4039 None => return Ok(None),
4043 StmtKind::Expr(ref expr) if self.token != token::Eof => {
4044 // expression without semicolon
4045 if classify::expr_requires_semi_to_be_stmt(expr) {
4046 // Just check for errors and recover; do not eat semicolon yet.
4048 self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
4051 self.recover_stmt();
4055 StmtKind::Local(..) => {
4056 // We used to incorrectly allow a macro-expanded let statement to lack a semicolon.
4057 if macro_legacy_warnings && self.token != token::Semi {
4058 self.warn_missing_semicolon();
4060 self.expect_one_of(&[token::Semi], &[])?;
4066 if self.eat(&token::Semi) {
4067 stmt = stmt.add_trailing_semicolon();
4070 stmt.span.hi = self.prev_span.hi;
4074 fn warn_missing_semicolon(&self) {
4075 self.diagnostic().struct_span_warn(self.span, {
4076 &format!("expected `;`, found `{}`", self.this_token_to_string())
4078 "This was erroneously allowed and will become a hard error in a future release"
4082 // Parses a sequence of bounds if a `:` is found,
4083 // otherwise returns empty list.
4084 fn parse_colon_then_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
4086 if !self.eat(&token::Colon) {
4089 self.parse_ty_param_bounds()
4093 // matches bounds = ( boundseq )?
4094 // where boundseq = ( polybound + boundseq ) | polybound
4095 // and polybound = ( 'for' '<' 'region '>' )? bound
4096 // and bound = 'region | trait_ref
4097 fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
4099 let mut result = vec![];
4101 let question_span = self.span;
4102 let ate_question = self.eat(&token::Question);
4104 token::Lifetime(lifetime) => {
4106 self.span_err(question_span,
4107 "`?` may only modify trait bounds, not lifetime bounds");
4109 result.push(RegionTyParamBound(ast::Lifetime {
4110 id: ast::DUMMY_NODE_ID,
4116 _ if self.token.is_path_start() || self.token.is_keyword(keywords::For) => {
4117 let poly_trait_ref = self.parse_poly_trait_ref()?;
4118 let modifier = if ate_question {
4119 TraitBoundModifier::Maybe
4121 TraitBoundModifier::None
4123 result.push(TraitTyParamBound(poly_trait_ref, modifier))
4128 if !self.eat(&token::BinOp(token::Plus)) {
4136 /// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
4137 fn parse_ty_param(&mut self, preceding_attrs: Vec<ast::Attribute>) -> PResult<'a, TyParam> {
4138 let span = self.span;
4139 let ident = self.parse_ident()?;
4141 let bounds = self.parse_colon_then_ty_param_bounds()?;
4143 let default = if self.check(&token::Eq) {
4145 Some(self.parse_ty()?)
4151 attrs: preceding_attrs.into(),
4153 id: ast::DUMMY_NODE_ID,
4160 /// Parse a set of optional generic type parameter declarations. Where
4161 /// clauses are not parsed here, and must be added later via
4162 /// `parse_where_clause()`.
4164 /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4165 /// | ( < lifetimes , typaramseq ( , )? > )
4166 /// where typaramseq = ( typaram ) | ( typaram , typaramseq )
4167 pub fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
4168 maybe_whole!(self, NtGenerics, |x| x);
4169 let span_lo = self.span.lo;
4171 if self.eat(&token::Lt) {
4172 // Upon encountering attribute in generics list, we do not
4173 // know if it is attached to lifetime or to type param.
4175 // Solution: 1. eagerly parse attributes in tandem with
4176 // lifetime defs, 2. store last set of parsed (and unused)
4177 // attributes in `attrs`, and 3. pass in those attributes
4178 // when parsing formal type param after lifetime defs.
4179 let mut attrs = vec![];
4180 let lifetime_defs = self.parse_lifetime_defs(Some(&mut attrs))?;
4181 let mut seen_default = false;
4182 let mut post_lifetime_attrs = Some(attrs);
4183 let ty_params = self.parse_seq_to_gt(Some(token::Comma), |p| {
4184 p.forbid_lifetime()?;
4185 // Move out of `post_lifetime_attrs` if present. O/w
4186 // not first type param: parse attributes anew.
4187 let attrs = match post_lifetime_attrs.as_mut() {
4188 None => p.parse_outer_attributes()?,
4189 Some(attrs) => mem::replace(attrs, vec![]),
4191 post_lifetime_attrs = None;
4192 let ty_param = p.parse_ty_param(attrs)?;
4193 if ty_param.default.is_some() {
4194 seen_default = true;
4195 } else if seen_default {
4196 let prev_span = p.prev_span;
4197 p.span_err(prev_span,
4198 "type parameters with a default must be trailing");
4202 if let Some(attrs) = post_lifetime_attrs {
4203 if !attrs.is_empty() {
4204 self.span_err(attrs[0].span,
4205 "trailing attribute after lifetime parameters");
4209 lifetimes: lifetime_defs,
4210 ty_params: ty_params,
4211 where_clause: WhereClause {
4212 id: ast::DUMMY_NODE_ID,
4213 predicates: Vec::new(),
4215 span: mk_sp(span_lo, self.prev_span.hi),
4218 Ok(ast::Generics::default())
4222 fn parse_generic_values_after_lt(&mut self) -> PResult<'a, (Vec<ast::Lifetime>,
4224 Vec<TypeBinding>)> {
4225 let span_lo = self.span.lo;
4226 let lifetimes = self.parse_lifetimes(token::Comma)?;
4228 let missing_comma = !lifetimes.is_empty() &&
4229 !self.token.is_like_gt() &&
4230 self.prev_token_kind != PrevTokenKind::Comma;
4234 let msg = format!("expected `,` or `>` after lifetime \
4236 self.this_token_to_string());
4237 let mut err = self.diagnostic().struct_span_err(self.span, &msg);
4239 let span_hi = self.span.hi;
4240 let span_hi = match self.parse_ty_no_plus() {
4241 Ok(..) => self.span.hi,
4242 Err(ref mut err) => {
4248 let msg = format!("did you mean a single argument type &'a Type, \
4249 or did you mean the comma-separated arguments \
4251 err.span_note(mk_sp(span_lo, span_hi), &msg);
4255 // First parse types.
4256 let (types, returned) = self.parse_seq_to_gt_or_return(
4259 p.forbid_lifetime()?;
4260 if p.look_ahead(1, |t| t == &token::Eq) {
4263 Ok(Some(p.parse_ty()?))
4268 // If we found the `>`, don't continue.
4270 return Ok((lifetimes, types, Vec::new()));
4273 // Then parse type bindings.
4274 let bindings = self.parse_seq_to_gt(
4277 p.forbid_lifetime()?;
4279 let ident = p.parse_ident()?;
4280 p.expect(&token::Eq)?;
4281 let ty = p.parse_ty_no_plus()?;
4282 let hi = ty.span.hi;
4283 let span = mk_sp(lo, hi);
4284 return Ok(TypeBinding{id: ast::DUMMY_NODE_ID,
4291 Ok((lifetimes, types, bindings))
4294 fn forbid_lifetime(&mut self) -> PResult<'a, ()> {
4295 if self.token.is_lifetime() {
4296 let span = self.span;
4297 return Err(self.diagnostic().struct_span_err(span, "lifetime parameters must be \
4298 declared prior to type parameters"))
4303 /// Parses an optional `where` clause and places it in `generics`.
4306 /// where T : Trait<U, V> + 'b, 'a : 'b
4308 pub fn parse_where_clause(&mut self) -> PResult<'a, ast::WhereClause> {
4309 maybe_whole!(self, NtWhereClause, |x| x);
4311 let mut where_clause = WhereClause {
4312 id: ast::DUMMY_NODE_ID,
4313 predicates: Vec::new(),
4316 if !self.eat_keyword(keywords::Where) {
4317 return Ok(where_clause);
4320 // This is a temporary hack.
4322 // We are considering adding generics to the `where` keyword as an alternative higher-rank
4323 // parameter syntax (as in `where<'a>` or `where<T>`. To avoid that being a breaking
4324 // change, for now we refuse to parse `where < (ident | lifetime) (> | , | :)`.
4325 if token::Lt == self.token {
4326 let ident_or_lifetime = self.look_ahead(1, |t| t.is_ident() || t.is_lifetime());
4327 if ident_or_lifetime {
4328 let gt_comma_or_colon = self.look_ahead(2, |t| {
4329 *t == token::Gt || *t == token::Comma || *t == token::Colon
4331 if gt_comma_or_colon {
4332 self.span_err(self.span, "syntax `where<T>` is reserved for future use");
4337 let mut parsed_something = false;
4339 let lo = self.span.lo;
4341 token::OpenDelim(token::Brace) => {
4345 token::Lifetime(..) => {
4346 let bounded_lifetime =
4347 self.parse_lifetime()?;
4349 self.expect(&token::Colon)?;
4352 self.parse_lifetimes(token::BinOp(token::Plus))?;
4354 let hi = self.prev_span.hi;
4355 let span = mk_sp(lo, hi);
4357 where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4358 ast::WhereRegionPredicate {
4360 lifetime: bounded_lifetime,
4365 parsed_something = true;
4369 let bound_lifetimes = if self.eat_keyword(keywords::For) {
4370 // Higher ranked constraint.
4371 self.expect(&token::Lt)?;
4372 let lifetime_defs = self.parse_lifetime_defs(None)?;
4379 let bounded_ty = self.parse_ty_no_plus()?;
4381 if self.eat(&token::Colon) {
4382 let bounds = self.parse_ty_param_bounds()?;
4383 let hi = self.prev_span.hi;
4384 let span = mk_sp(lo, hi);
4386 if bounds.is_empty() {
4388 "each predicate in a `where` clause must have \
4389 at least one bound in it");
4392 where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4393 ast::WhereBoundPredicate {
4395 bound_lifetimes: bound_lifetimes,
4396 bounded_ty: bounded_ty,
4400 parsed_something = true;
4401 } else if self.eat(&token::Eq) {
4402 // let ty = try!(self.parse_ty_no_plus());
4403 let hi = self.prev_span.hi;
4404 let span = mk_sp(lo, hi);
4405 // where_clause.predicates.push(
4406 // ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
4407 // id: ast::DUMMY_NODE_ID,
4409 // path: panic!("NYI"), //bounded_ty,
4412 // parsed_something = true;
4415 "equality constraints are not yet supported \
4416 in where clauses (#20041)");
4418 let prev_span = self.prev_span;
4419 self.span_err(prev_span,
4420 "unexpected token in `where` clause");
4425 if !self.eat(&token::Comma) {
4430 if !parsed_something {
4431 let prev_span = self.prev_span;
4432 self.span_err(prev_span,
4433 "a `where` clause must have at least one predicate \
4440 fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4441 -> PResult<'a, (Vec<Arg> , bool)> {
4443 let mut variadic = false;
4444 let args: Vec<Option<Arg>> =
4445 self.parse_unspanned_seq(
4446 &token::OpenDelim(token::Paren),
4447 &token::CloseDelim(token::Paren),
4448 SeqSep::trailing_allowed(token::Comma),
4450 if p.token == token::DotDotDot {
4453 if p.token != token::CloseDelim(token::Paren) {
4456 "`...` must be last in argument list for variadic function");
4461 "only foreign functions are allowed to be variadic");
4466 match p.parse_arg_general(named_args) {
4467 Ok(arg) => Ok(Some(arg)),
4470 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
4478 let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
4480 if variadic && args.is_empty() {
4482 "variadic function must be declared with at least one named argument");
4485 Ok((args, variadic))
4488 /// Parse the argument list and result type of a function declaration
4489 pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> PResult<'a, P<FnDecl>> {
4491 let (args, variadic) = self.parse_fn_args(true, allow_variadic)?;
4492 let ret_ty = self.parse_ret_ty()?;
4501 /// Returns the parsed optional self argument and whether a self shortcut was used.
4502 fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
4503 let expect_ident = |this: &mut Self| match this.token {
4504 // Preserve hygienic context.
4505 token::Ident(ident) => { this.bump(); codemap::respan(this.prev_span, ident) }
4508 let isolated_self = |this: &mut Self, n| {
4509 this.look_ahead(n, |t| t.is_keyword(keywords::SelfValue)) &&
4510 this.look_ahead(n + 1, |t| t != &token::ModSep)
4513 // Parse optional self parameter of a method.
4514 // Only a limited set of initial token sequences is considered self parameters, anything
4515 // else is parsed as a normal function parameter list, so some lookahead is required.
4516 let eself_lo = self.span.lo;
4517 let (eself, eself_ident) = match self.token {
4518 token::BinOp(token::And) => {
4524 if isolated_self(self, 1) {
4526 (SelfKind::Region(None, Mutability::Immutable), expect_ident(self))
4527 } else if self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
4528 isolated_self(self, 2) {
4531 (SelfKind::Region(None, Mutability::Mutable), expect_ident(self))
4532 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4533 isolated_self(self, 2) {
4535 let lt = self.parse_lifetime()?;
4536 (SelfKind::Region(Some(lt), Mutability::Immutable), expect_ident(self))
4537 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4538 self.look_ahead(2, |t| t.is_keyword(keywords::Mut)) &&
4539 isolated_self(self, 3) {
4541 let lt = self.parse_lifetime()?;
4543 (SelfKind::Region(Some(lt), Mutability::Mutable), expect_ident(self))
4548 token::BinOp(token::Star) => {
4553 // Emit special error for `self` cases.
4554 if isolated_self(self, 1) {
4556 self.span_err(self.span, "cannot pass `self` by raw pointer");
4557 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4558 } else if self.look_ahead(1, |t| t.is_mutability()) &&
4559 isolated_self(self, 2) {
4562 self.span_err(self.span, "cannot pass `self` by raw pointer");
4563 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4568 token::Ident(..) => {
4569 if isolated_self(self, 0) {
4572 let eself_ident = expect_ident(self);
4573 if self.eat(&token::Colon) {
4574 let ty = self.parse_ty()?;
4575 (SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
4577 (SelfKind::Value(Mutability::Immutable), eself_ident)
4579 } else if self.token.is_keyword(keywords::Mut) &&
4580 isolated_self(self, 1) {
4584 let eself_ident = expect_ident(self);
4585 if self.eat(&token::Colon) {
4586 let ty = self.parse_ty()?;
4587 (SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
4589 (SelfKind::Value(Mutability::Mutable), eself_ident)
4595 _ => return Ok(None),
4598 let eself = codemap::respan(mk_sp(eself_lo, self.prev_span.hi), eself);
4599 Ok(Some(Arg::from_self(eself, eself_ident)))
4602 /// Parse the parameter list and result type of a function that may have a `self` parameter.
4603 fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
4604 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
4606 self.expect(&token::OpenDelim(token::Paren))?;
4608 // Parse optional self argument
4609 let self_arg = self.parse_self_arg()?;
4611 // Parse the rest of the function parameter list.
4612 let sep = SeqSep::trailing_allowed(token::Comma);
4613 let fn_inputs = if let Some(self_arg) = self_arg {
4614 if self.check(&token::CloseDelim(token::Paren)) {
4616 } else if self.eat(&token::Comma) {
4617 let mut fn_inputs = vec![self_arg];
4618 fn_inputs.append(&mut self.parse_seq_to_before_end(
4619 &token::CloseDelim(token::Paren), sep, parse_arg_fn)
4623 return self.unexpected();
4626 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)
4629 // Parse closing paren and return type.
4630 self.expect(&token::CloseDelim(token::Paren))?;
4633 output: self.parse_ret_ty()?,
4638 // parse the |arg, arg| header on a lambda
4639 fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
4640 let inputs_captures = {
4641 if self.eat(&token::OrOr) {
4644 self.expect(&token::BinOp(token::Or))?;
4645 let args = self.parse_seq_to_before_end(
4646 &token::BinOp(token::Or),
4647 SeqSep::trailing_allowed(token::Comma),
4648 |p| p.parse_fn_block_arg()
4654 let output = self.parse_ret_ty()?;
4657 inputs: inputs_captures,
4663 /// Parse the name and optional generic types of a function header.
4664 fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> {
4665 let id = self.parse_ident()?;
4666 let generics = self.parse_generics()?;
4670 fn mk_item(&mut self, lo: BytePos, hi: BytePos, ident: Ident,
4671 node: ItemKind, vis: Visibility,
4672 attrs: Vec<Attribute>) -> P<Item> {
4676 id: ast::DUMMY_NODE_ID,
4683 /// Parse an item-position function declaration.
4684 fn parse_item_fn(&mut self,
4686 constness: Spanned<Constness>,
4688 -> PResult<'a, ItemInfo> {
4689 let (ident, mut generics) = self.parse_fn_header()?;
4690 let decl = self.parse_fn_decl(false)?;
4691 generics.where_clause = self.parse_where_clause()?;
4692 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4693 Ok((ident, ItemKind::Fn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs)))
4696 /// true if we are looking at `const ID`, false for things like `const fn` etc
4697 pub fn is_const_item(&mut self) -> bool {
4698 self.token.is_keyword(keywords::Const) &&
4699 !self.look_ahead(1, |t| t.is_keyword(keywords::Fn)) &&
4700 !self.look_ahead(1, |t| t.is_keyword(keywords::Unsafe))
4703 /// parses all the "front matter" for a `fn` declaration, up to
4704 /// and including the `fn` keyword:
4708 /// - `const unsafe fn`
4711 pub fn parse_fn_front_matter(&mut self)
4712 -> PResult<'a, (Spanned<ast::Constness>,
4715 let is_const_fn = self.eat_keyword(keywords::Const);
4716 let const_span = self.prev_span;
4717 let unsafety = self.parse_unsafety()?;
4718 let (constness, unsafety, abi) = if is_const_fn {
4719 (respan(const_span, Constness::Const), unsafety, Abi::Rust)
4721 let abi = if self.eat_keyword(keywords::Extern) {
4722 self.parse_opt_abi()?.unwrap_or(Abi::C)
4726 (respan(self.prev_span, Constness::NotConst), unsafety, abi)
4728 self.expect_keyword(keywords::Fn)?;
4729 Ok((constness, unsafety, abi))
4732 /// Parse an impl item.
4733 pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> {
4734 maybe_whole!(self, NtImplItem, |x| x);
4736 let mut attrs = self.parse_outer_attributes()?;
4737 let lo = self.span.lo;
4738 let vis = self.parse_visibility(true)?;
4739 let defaultness = self.parse_defaultness()?;
4740 let (name, node) = if self.eat_keyword(keywords::Type) {
4741 let name = self.parse_ident()?;
4742 self.expect(&token::Eq)?;
4743 let typ = self.parse_ty()?;
4744 self.expect(&token::Semi)?;
4745 (name, ast::ImplItemKind::Type(typ))
4746 } else if self.is_const_item() {
4747 self.expect_keyword(keywords::Const)?;
4748 let name = self.parse_ident()?;
4749 self.expect(&token::Colon)?;
4750 let typ = self.parse_ty()?;
4751 self.expect(&token::Eq)?;
4752 let expr = self.parse_expr()?;
4753 self.expect(&token::Semi)?;
4754 (name, ast::ImplItemKind::Const(typ, expr))
4756 let (name, inner_attrs, node) = self.parse_impl_method(&vis)?;
4757 attrs.extend(inner_attrs);
4762 id: ast::DUMMY_NODE_ID,
4763 span: mk_sp(lo, self.prev_span.hi),
4766 defaultness: defaultness,
4772 fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
4774 Visibility::Inherited => (),
4776 let is_macro_rules: bool = match self.token {
4777 token::Ident(sid) => sid.name == Symbol::intern("macro_rules"),
4781 self.diagnostic().struct_span_err(span, "can't qualify macro_rules \
4782 invocation with `pub`")
4783 .help("did you mean #[macro_export]?")
4786 self.diagnostic().struct_span_err(span, "can't qualify macro \
4787 invocation with `pub`")
4788 .help("try adjusting the macro to put `pub` \
4789 inside the invocation")
4796 /// Parse a method or a macro invocation in a trait impl.
4797 fn parse_impl_method(&mut self, vis: &Visibility)
4798 -> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
4799 // code copied from parse_macro_use_or_failure... abstraction!
4800 if self.token.is_path_start() {
4803 let prev_span = self.prev_span;
4804 self.complain_if_pub_macro(&vis, prev_span);
4806 let lo = self.span.lo;
4807 let pth = self.parse_path(PathStyle::Mod)?;
4808 self.expect(&token::Not)?;
4810 // eat a matched-delimiter token tree:
4811 let delim = self.expect_open_delim()?;
4812 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
4814 |p| p.parse_token_tree())?;
4815 if delim != token::Brace {
4816 self.expect(&token::Semi)?
4819 let mac = spanned(lo, self.prev_span.hi, Mac_ { path: pth, tts: tts });
4820 Ok((keywords::Invalid.ident(), vec![], ast::ImplItemKind::Macro(mac)))
4822 let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
4823 let ident = self.parse_ident()?;
4824 let mut generics = self.parse_generics()?;
4825 let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
4826 generics.where_clause = self.parse_where_clause()?;
4827 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4828 Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
4832 constness: constness,
4838 /// Parse trait Foo { ... }
4839 fn parse_item_trait(&mut self, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
4840 let ident = self.parse_ident()?;
4841 let mut tps = self.parse_generics()?;
4843 // Parse supertrait bounds.
4844 let bounds = self.parse_colon_then_ty_param_bounds()?;
4846 tps.where_clause = self.parse_where_clause()?;
4848 let meths = self.parse_trait_items()?;
4849 Ok((ident, ItemKind::Trait(unsafety, tps, bounds, meths), None))
4852 /// Parses items implementations variants
4853 /// impl<T> Foo { ... }
4854 /// impl<T> ToString for &'static T { ... }
4855 /// impl Send for .. {}
4856 fn parse_item_impl(&mut self, unsafety: ast::Unsafety) -> PResult<'a, ItemInfo> {
4857 let impl_span = self.span;
4859 // First, parse type parameters if necessary.
4860 let mut generics = self.parse_generics()?;
4862 // Special case: if the next identifier that follows is '(', don't
4863 // allow this to be parsed as a trait.
4864 let could_be_trait = self.token != token::OpenDelim(token::Paren);
4866 let neg_span = self.span;
4867 let polarity = if self.eat(&token::Not) {
4868 ast::ImplPolarity::Negative
4870 ast::ImplPolarity::Positive
4874 let mut ty = self.parse_ty()?;
4876 // Parse traits, if necessary.
4877 let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
4878 // New-style trait. Reinterpret the type as a trait.
4880 TyKind::Path(None, ref path) => {
4882 path: (*path).clone(),
4887 self.span_err(ty.span, "not a trait");
4893 ast::ImplPolarity::Negative => {
4894 // This is a negated type implementation
4895 // `impl !MyType {}`, which is not allowed.
4896 self.span_err(neg_span, "inherent implementation can't be negated");
4903 if opt_trait.is_some() && self.eat(&token::DotDot) {
4904 if generics.is_parameterized() {
4905 self.span_err(impl_span, "default trait implementations are not \
4906 allowed to have generics");
4909 self.expect(&token::OpenDelim(token::Brace))?;
4910 self.expect(&token::CloseDelim(token::Brace))?;
4911 Ok((keywords::Invalid.ident(),
4912 ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
4914 if opt_trait.is_some() {
4915 ty = self.parse_ty()?;
4917 generics.where_clause = self.parse_where_clause()?;
4919 self.expect(&token::OpenDelim(token::Brace))?;
4920 let attrs = self.parse_inner_attributes()?;
4922 let mut impl_items = vec![];
4923 while !self.eat(&token::CloseDelim(token::Brace)) {
4924 impl_items.push(self.parse_impl_item()?);
4927 Ok((keywords::Invalid.ident(),
4928 ItemKind::Impl(unsafety, polarity, generics, opt_trait, ty, impl_items),
4933 /// Parse a::B<String,i32>
4934 fn parse_trait_ref(&mut self) -> PResult<'a, TraitRef> {
4936 path: self.parse_path(PathStyle::Type)?,
4937 ref_id: ast::DUMMY_NODE_ID,
4941 fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<ast::LifetimeDef>> {
4942 if self.eat_keyword(keywords::For) {
4943 self.expect(&token::Lt)?;
4944 let lifetime_defs = self.parse_lifetime_defs(None)?;
4952 /// Parse for<'l> a::B<String,i32>
4953 fn parse_poly_trait_ref(&mut self) -> PResult<'a, PolyTraitRef> {
4954 let lo = self.span.lo;
4955 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
4957 Ok(ast::PolyTraitRef {
4958 bound_lifetimes: lifetime_defs,
4959 trait_ref: self.parse_trait_ref()?,
4960 span: mk_sp(lo, self.prev_span.hi),
4964 /// Parse struct Foo { ... }
4965 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
4966 let class_name = self.parse_ident()?;
4967 let mut generics = self.parse_generics()?;
4969 // There is a special case worth noting here, as reported in issue #17904.
4970 // If we are parsing a tuple struct it is the case that the where clause
4971 // should follow the field list. Like so:
4973 // struct Foo<T>(T) where T: Copy;
4975 // If we are parsing a normal record-style struct it is the case
4976 // that the where clause comes before the body, and after the generics.
4977 // So if we look ahead and see a brace or a where-clause we begin
4978 // parsing a record style struct.
4980 // Otherwise if we look ahead and see a paren we parse a tuple-style
4983 let vdata = if self.token.is_keyword(keywords::Where) {
4984 generics.where_clause = self.parse_where_clause()?;
4985 if self.eat(&token::Semi) {
4986 // If we see a: `struct Foo<T> where T: Copy;` style decl.
4987 VariantData::Unit(ast::DUMMY_NODE_ID)
4989 // If we see: `struct Foo<T> where T: Copy { ... }`
4990 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
4992 // No `where` so: `struct Foo<T>;`
4993 } else if self.eat(&token::Semi) {
4994 VariantData::Unit(ast::DUMMY_NODE_ID)
4995 // Record-style struct definition
4996 } else if self.token == token::OpenDelim(token::Brace) {
4997 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
4998 // Tuple-style struct definition with optional where-clause.
4999 } else if self.token == token::OpenDelim(token::Paren) {
5000 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID);
5001 generics.where_clause = self.parse_where_clause()?;
5002 self.expect(&token::Semi)?;
5005 let token_str = self.this_token_to_string();
5006 return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
5007 name, found `{}`", token_str)))
5010 Ok((class_name, ItemKind::Struct(vdata, generics), None))
5013 /// Parse union Foo { ... }
5014 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
5015 let class_name = self.parse_ident()?;
5016 let mut generics = self.parse_generics()?;
5018 let vdata = if self.token.is_keyword(keywords::Where) {
5019 generics.where_clause = self.parse_where_clause()?;
5020 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5021 } else if self.token == token::OpenDelim(token::Brace) {
5022 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5024 let token_str = self.this_token_to_string();
5025 return Err(self.fatal(&format!("expected `where` or `{{` after union \
5026 name, found `{}`", token_str)))
5029 Ok((class_name, ItemKind::Union(vdata, generics), None))
5032 pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5033 let mut fields = Vec::new();
5034 if self.eat(&token::OpenDelim(token::Brace)) {
5035 while self.token != token::CloseDelim(token::Brace) {
5036 fields.push(self.parse_struct_decl_field().map_err(|e| {
5037 self.recover_stmt();
5038 self.eat(&token::CloseDelim(token::Brace));
5045 let token_str = self.this_token_to_string();
5046 return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
5054 pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5055 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
5056 // Unit like structs are handled in parse_item_struct function
5057 let fields = self.parse_unspanned_seq(
5058 &token::OpenDelim(token::Paren),
5059 &token::CloseDelim(token::Paren),
5060 SeqSep::trailing_allowed(token::Comma),
5062 let attrs = p.parse_outer_attributes()?;
5064 let mut vis = p.parse_visibility(false)?;
5065 let ty_is_interpolated =
5066 p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
5067 let mut ty = p.parse_ty()?;
5069 // Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
5070 if vis == Visibility::Public && !ty_is_interpolated &&
5071 p.token != token::Comma && p.token != token::CloseDelim(token::Paren) {
5072 ty = if let TyKind::Paren(ref path_ty) = ty.node {
5073 if let TyKind::Path(None, ref path) = path_ty.node {
5074 vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
5084 span: mk_sp(lo, p.span.hi),
5087 id: ast::DUMMY_NODE_ID,
5096 /// Parse a structure field declaration
5097 pub fn parse_single_struct_field(&mut self,
5100 attrs: Vec<Attribute> )
5101 -> PResult<'a, StructField> {
5102 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
5107 token::CloseDelim(token::Brace) => {}
5108 token::DocComment(_) => return Err(self.span_fatal_help(self.span,
5109 "found a documentation comment that doesn't document anything",
5110 "doc comments must come before what they document, maybe a comment was \
5111 intended with `//`?")),
5112 _ => return Err(self.span_fatal_help(self.span,
5113 &format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
5114 "struct fields should be separated by commas")),
5119 /// Parse an element of a struct definition
5120 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
5121 let attrs = self.parse_outer_attributes()?;
5122 let lo = self.span.lo;
5123 let vis = self.parse_visibility(true)?;
5124 self.parse_single_struct_field(lo, vis, attrs)
5127 // If `allow_path` is false, just parse the `pub` in `pub(path)` (but still parse `pub(crate)`)
5128 fn parse_visibility(&mut self, allow_path: bool) -> PResult<'a, Visibility> {
5129 let pub_crate = |this: &mut Self| {
5130 let span = this.prev_span;
5131 this.expect(&token::CloseDelim(token::Paren))?;
5132 Ok(Visibility::Crate(span))
5135 if !self.eat_keyword(keywords::Pub) {
5136 Ok(Visibility::Inherited)
5137 } else if !allow_path {
5138 // Look ahead to avoid eating the `(` in `pub(path)` while still parsing `pub(crate)`
5139 if self.token == token::OpenDelim(token::Paren) &&
5140 self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5141 self.bump(); self.bump();
5144 Ok(Visibility::Public)
5146 } else if !self.eat(&token::OpenDelim(token::Paren)) {
5147 Ok(Visibility::Public)
5148 } else if self.eat_keyword(keywords::Crate) {
5151 let path = self.parse_path(PathStyle::Mod)?.default_to_global();
5152 self.expect(&token::CloseDelim(token::Paren))?;
5153 Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
5157 /// Parse defaultness: DEFAULT or nothing
5158 fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
5159 if self.eat_contextual_keyword(keywords::Default.ident()) {
5160 Ok(Defaultness::Default)
5162 Ok(Defaultness::Final)
5166 /// Given a termination token, parse all of the items in a module
5167 fn parse_mod_items(&mut self, term: &token::Token, inner_lo: BytePos) -> PResult<'a, Mod> {
5168 let mut items = vec![];
5169 while let Some(item) = self.parse_item()? {
5173 if !self.eat(term) {
5174 let token_str = self.this_token_to_string();
5175 return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
5178 let hi = if self.span == syntax_pos::DUMMY_SP {
5185 inner: mk_sp(inner_lo, hi),
5190 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
5191 let id = self.parse_ident()?;
5192 self.expect(&token::Colon)?;
5193 let ty = self.parse_ty()?;
5194 self.expect(&token::Eq)?;
5195 let e = self.parse_expr()?;
5196 self.expect(&token::Semi)?;
5197 let item = match m {
5198 Some(m) => ItemKind::Static(ty, m, e),
5199 None => ItemKind::Const(ty, e),
5201 Ok((id, item, None))
5204 /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5205 fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
5206 let (in_cfg, outer_attrs) = {
5207 let mut strip_unconfigured = ::config::StripUnconfigured {
5209 should_test: false, // irrelevant
5210 features: None, // don't perform gated feature checking
5212 let outer_attrs = strip_unconfigured.process_cfg_attrs(outer_attrs.to_owned());
5213 (strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
5216 let id_span = self.span;
5217 let id = self.parse_ident()?;
5218 if self.check(&token::Semi) {
5221 // This mod is in an external file. Let's go get it!
5222 let ModulePathSuccess { path, directory_ownership, warn } =
5223 self.submod_path(id, &outer_attrs, id_span)?;
5224 let (module, mut attrs) =
5225 self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
5227 let attr = ast::Attribute {
5228 id: attr::mk_attr_id(),
5229 style: ast::AttrStyle::Outer,
5230 value: ast::MetaItem {
5231 name: Symbol::intern("warn_directory_ownership"),
5232 node: ast::MetaItemKind::Word,
5233 span: syntax_pos::DUMMY_SP,
5235 is_sugared_doc: false,
5236 span: syntax_pos::DUMMY_SP,
5238 attr::mark_known(&attr);
5241 Ok((id, module, Some(attrs)))
5243 let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
5244 Ok((id, ItemKind::Mod(placeholder), None))
5247 let old_directory = self.directory.clone();
5248 self.push_directory(id, &outer_attrs);
5249 self.expect(&token::OpenDelim(token::Brace))?;
5250 let mod_inner_lo = self.span.lo;
5251 let attrs = self.parse_inner_attributes()?;
5252 let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
5253 self.directory = old_directory;
5254 Ok((id, ItemKind::Mod(module), Some(attrs)))
5258 fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
5259 if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
5260 self.directory.path.push(&*path.as_str());
5261 self.directory.ownership = DirectoryOwnership::Owned;
5263 self.directory.path.push(&*id.name.as_str());
5267 pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
5268 attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&*d.as_str()))
5271 /// Returns either a path to a module, or .
5272 pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath
5274 let mod_name = id.to_string();
5275 let default_path_str = format!("{}.rs", mod_name);
5276 let secondary_path_str = format!("{}/mod.rs", mod_name);
5277 let default_path = dir_path.join(&default_path_str);
5278 let secondary_path = dir_path.join(&secondary_path_str);
5279 let default_exists = codemap.file_exists(&default_path);
5280 let secondary_exists = codemap.file_exists(&secondary_path);
5282 let result = match (default_exists, secondary_exists) {
5283 (true, false) => Ok(ModulePathSuccess {
5285 directory_ownership: DirectoryOwnership::UnownedViaMod(false),
5288 (false, true) => Ok(ModulePathSuccess {
5289 path: secondary_path,
5290 directory_ownership: DirectoryOwnership::Owned,
5293 (false, false) => Err(ModulePathError {
5294 err_msg: format!("file not found for module `{}`", mod_name),
5295 help_msg: format!("name the file either {} or {} inside the directory {:?}",
5298 dir_path.display()),
5300 (true, true) => Err(ModulePathError {
5301 err_msg: format!("file for module `{}` found at both {} and {}",
5304 secondary_path_str),
5305 help_msg: "delete or rename one of them to remove the ambiguity".to_owned(),
5311 path_exists: default_exists || secondary_exists,
5316 fn submod_path(&mut self,
5318 outer_attrs: &[ast::Attribute],
5319 id_sp: Span) -> PResult<'a, ModulePathSuccess> {
5320 if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) {
5321 return Ok(ModulePathSuccess {
5322 directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
5323 Some("mod.rs") => DirectoryOwnership::Owned,
5324 _ => DirectoryOwnership::UnownedViaMod(true),
5331 let paths = Parser::default_submod_path(id, &self.directory.path, self.sess.codemap());
5333 if let DirectoryOwnership::UnownedViaBlock = self.directory.ownership {
5335 "Cannot declare a non-inline module inside a block unless it has a path attribute";
5336 let mut err = self.diagnostic().struct_span_err(id_sp, msg);
5337 if paths.path_exists {
5338 let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
5340 err.span_note(id_sp, &msg);
5343 } else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
5345 if let Ok(result) = paths.result {
5346 return Ok(ModulePathSuccess { warn: true, ..result });
5349 let mut err = self.diagnostic().struct_span_err(id_sp,
5350 "cannot declare a new module at this location");
5351 let this_module = match self.directory.path.file_name() {
5352 Some(file_name) => file_name.to_str().unwrap().to_owned(),
5353 None => self.root_module_name.as_ref().unwrap().clone(),
5355 err.span_note(id_sp,
5356 &format!("maybe move this module `{0}` to its own directory \
5359 if paths.path_exists {
5360 err.span_note(id_sp,
5361 &format!("... or maybe `use` the module `{}` instead \
5362 of possibly redeclaring it",
5370 match paths.result {
5371 Ok(succ) => Ok(succ),
5372 Err(err) => Err(self.span_fatal_help(id_sp, &err.err_msg, &err.help_msg)),
5376 /// Read a module from a source file.
5377 fn eval_src_mod(&mut self,
5379 directory_ownership: DirectoryOwnership,
5382 -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
5383 let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5384 if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
5385 let mut err = String::from("circular modules: ");
5386 let len = included_mod_stack.len();
5387 for p in &included_mod_stack[i.. len] {
5388 err.push_str(&p.to_string_lossy());
5389 err.push_str(" -> ");
5391 err.push_str(&path.to_string_lossy());
5392 return Err(self.span_fatal(id_sp, &err[..]));
5394 included_mod_stack.push(path.clone());
5395 drop(included_mod_stack);
5398 new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp);
5399 let mod_inner_lo = p0.span.lo;
5400 let mod_attrs = p0.parse_inner_attributes()?;
5401 let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
5402 self.sess.included_mod_stack.borrow_mut().pop();
5403 Ok((ast::ItemKind::Mod(m0), mod_attrs))
5406 /// Parse a function declaration from a foreign module
5407 fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: BytePos,
5408 attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
5409 self.expect_keyword(keywords::Fn)?;
5411 let (ident, mut generics) = self.parse_fn_header()?;
5412 let decl = self.parse_fn_decl(true)?;
5413 generics.where_clause = self.parse_where_clause()?;
5414 let hi = self.span.hi;
5415 self.expect(&token::Semi)?;
5416 Ok(ast::ForeignItem {
5419 node: ForeignItemKind::Fn(decl, generics),
5420 id: ast::DUMMY_NODE_ID,
5421 span: mk_sp(lo, hi),
5426 /// Parse a static item from a foreign module
5427 fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: BytePos,
5428 attrs: Vec<Attribute>) -> PResult<'a, ForeignItem> {
5429 self.expect_keyword(keywords::Static)?;
5430 let mutbl = self.eat_keyword(keywords::Mut);
5432 let ident = self.parse_ident()?;
5433 self.expect(&token::Colon)?;
5434 let ty = self.parse_ty()?;
5435 let hi = self.span.hi;
5436 self.expect(&token::Semi)?;
5440 node: ForeignItemKind::Static(ty, mutbl),
5441 id: ast::DUMMY_NODE_ID,
5442 span: mk_sp(lo, hi),
5447 /// Parse extern crate links
5451 /// extern crate foo;
5452 /// extern crate bar as foo;
5453 fn parse_item_extern_crate(&mut self,
5455 visibility: Visibility,
5456 attrs: Vec<Attribute>)
5457 -> PResult<'a, P<Item>> {
5459 let crate_name = self.parse_ident()?;
5460 let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
5461 (Some(crate_name.name), ident)
5465 self.expect(&token::Semi)?;
5467 let prev_span = self.prev_span;
5471 ItemKind::ExternCrate(maybe_path),
5476 /// Parse `extern` for foreign ABIs
5479 /// `extern` is expected to have been
5480 /// consumed before calling this method
5486 fn parse_item_foreign_mod(&mut self,
5488 opt_abi: Option<abi::Abi>,
5489 visibility: Visibility,
5490 mut attrs: Vec<Attribute>)
5491 -> PResult<'a, P<Item>> {
5492 self.expect(&token::OpenDelim(token::Brace))?;
5494 let abi = opt_abi.unwrap_or(Abi::C);
5496 attrs.extend(self.parse_inner_attributes()?);
5498 let mut foreign_items = vec![];
5499 while let Some(item) = self.parse_foreign_item()? {
5500 foreign_items.push(item);
5502 self.expect(&token::CloseDelim(token::Brace))?;
5504 let prev_span = self.prev_span;
5505 let m = ast::ForeignMod {
5507 items: foreign_items
5511 keywords::Invalid.ident(),
5512 ItemKind::ForeignMod(m),
5517 /// Parse type Foo = Bar;
5518 fn parse_item_type(&mut self) -> PResult<'a, ItemInfo> {
5519 let ident = self.parse_ident()?;
5520 let mut tps = self.parse_generics()?;
5521 tps.where_clause = self.parse_where_clause()?;
5522 self.expect(&token::Eq)?;
5523 let ty = self.parse_ty()?;
5524 self.expect(&token::Semi)?;
5525 Ok((ident, ItemKind::Ty(ty, tps), None))
5528 /// Parse the part of an "enum" decl following the '{'
5529 fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> {
5530 let mut variants = Vec::new();
5531 let mut all_nullary = true;
5532 let mut any_disr = None;
5533 while self.token != token::CloseDelim(token::Brace) {
5534 let variant_attrs = self.parse_outer_attributes()?;
5535 let vlo = self.span.lo;
5538 let mut disr_expr = None;
5539 let ident = self.parse_ident()?;
5540 if self.check(&token::OpenDelim(token::Brace)) {
5541 // Parse a struct variant.
5542 all_nullary = false;
5543 struct_def = VariantData::Struct(self.parse_record_struct_body()?,
5544 ast::DUMMY_NODE_ID);
5545 } else if self.check(&token::OpenDelim(token::Paren)) {
5546 all_nullary = false;
5547 struct_def = VariantData::Tuple(self.parse_tuple_struct_body()?,
5548 ast::DUMMY_NODE_ID);
5549 } else if self.eat(&token::Eq) {
5550 disr_expr = Some(self.parse_expr()?);
5551 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5552 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5554 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5557 let vr = ast::Variant_ {
5559 attrs: variant_attrs,
5561 disr_expr: disr_expr,
5563 variants.push(spanned(vlo, self.prev_span.hi, vr));
5565 if !self.eat(&token::Comma) { break; }
5567 self.expect(&token::CloseDelim(token::Brace))?;
5569 Some(disr_span) if !all_nullary =>
5570 self.span_err(disr_span,
5571 "discriminator values can only be used with a c-like enum"),
5575 Ok(ast::EnumDef { variants: variants })
5578 /// Parse an "enum" declaration
5579 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
5580 let id = self.parse_ident()?;
5581 let mut generics = self.parse_generics()?;
5582 generics.where_clause = self.parse_where_clause()?;
5583 self.expect(&token::OpenDelim(token::Brace))?;
5585 let enum_definition = self.parse_enum_def(&generics).map_err(|e| {
5586 self.recover_stmt();
5587 self.eat(&token::CloseDelim(token::Brace));
5590 Ok((id, ItemKind::Enum(enum_definition, generics), None))
5593 /// Parses a string as an ABI spec on an extern type or module. Consumes
5594 /// the `extern` keyword, if one is found.
5595 fn parse_opt_abi(&mut self) -> PResult<'a, Option<abi::Abi>> {
5597 token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5599 self.expect_no_suffix(sp, "ABI spec", suf);
5601 match abi::lookup(&s.as_str()) {
5602 Some(abi) => Ok(Some(abi)),
5604 let prev_span = self.prev_span;
5607 &format!("invalid ABI: expected one of [{}], \
5609 abi::all_names().join(", "),
5620 /// Parse one of the items allowed by the flags.
5621 /// NB: this function no longer parses the items inside an
5623 fn parse_item_(&mut self, attrs: Vec<Attribute>,
5624 macros_allowed: bool, attributes_allowed: bool) -> PResult<'a, Option<P<Item>>> {
5625 maybe_whole!(self, NtItem, |item| {
5626 let mut item = item.unwrap();
5627 let mut attrs = attrs;
5628 mem::swap(&mut item.attrs, &mut attrs);
5629 item.attrs.extend(attrs);
5633 let lo = self.span.lo;
5635 let visibility = self.parse_visibility(true)?;
5637 if self.eat_keyword(keywords::Use) {
5639 let item_ = ItemKind::Use(self.parse_view_path()?);
5640 self.expect(&token::Semi)?;
5642 let prev_span = self.prev_span;
5643 let item = self.mk_item(lo,
5645 keywords::Invalid.ident(),
5649 return Ok(Some(item));
5652 if self.eat_keyword(keywords::Extern) {
5653 if self.eat_keyword(keywords::Crate) {
5654 return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?));
5657 let opt_abi = self.parse_opt_abi()?;
5659 if self.eat_keyword(keywords::Fn) {
5660 // EXTERN FUNCTION ITEM
5661 let fn_span = self.prev_span;
5662 let abi = opt_abi.unwrap_or(Abi::C);
5663 let (ident, item_, extra_attrs) =
5664 self.parse_item_fn(Unsafety::Normal,
5665 respan(fn_span, Constness::NotConst),
5667 let prev_span = self.prev_span;
5668 let item = self.mk_item(lo,
5673 maybe_append(attrs, extra_attrs));
5674 return Ok(Some(item));
5675 } else if self.check(&token::OpenDelim(token::Brace)) {
5676 return Ok(Some(self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs)?));
5682 if self.eat_keyword(keywords::Static) {
5684 let m = if self.eat_keyword(keywords::Mut) {
5687 Mutability::Immutable
5689 let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?;
5690 let prev_span = self.prev_span;
5691 let item = self.mk_item(lo,
5696 maybe_append(attrs, extra_attrs));
5697 return Ok(Some(item));
5699 if self.eat_keyword(keywords::Const) {
5700 let const_span = self.prev_span;
5701 if self.check_keyword(keywords::Fn)
5702 || (self.check_keyword(keywords::Unsafe)
5703 && self.look_ahead(1, |t| t.is_keyword(keywords::Fn))) {
5704 // CONST FUNCTION ITEM
5705 let unsafety = if self.eat_keyword(keywords::Unsafe) {
5711 let (ident, item_, extra_attrs) =
5712 self.parse_item_fn(unsafety,
5713 respan(const_span, Constness::Const),
5715 let prev_span = self.prev_span;
5716 let item = self.mk_item(lo,
5721 maybe_append(attrs, extra_attrs));
5722 return Ok(Some(item));
5726 if self.eat_keyword(keywords::Mut) {
5727 let prev_span = self.prev_span;
5728 self.diagnostic().struct_span_err(prev_span, "const globals cannot be mutable")
5729 .help("did you mean to declare a static?")
5732 let (ident, item_, extra_attrs) = self.parse_item_const(None)?;
5733 let prev_span = self.prev_span;
5734 let item = self.mk_item(lo,
5739 maybe_append(attrs, extra_attrs));
5740 return Ok(Some(item));
5742 if self.check_keyword(keywords::Unsafe) &&
5743 self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
5745 // UNSAFE TRAIT ITEM
5746 self.expect_keyword(keywords::Unsafe)?;
5747 self.expect_keyword(keywords::Trait)?;
5748 let (ident, item_, extra_attrs) =
5749 self.parse_item_trait(ast::Unsafety::Unsafe)?;
5750 let prev_span = self.prev_span;
5751 let item = self.mk_item(lo,
5756 maybe_append(attrs, extra_attrs));
5757 return Ok(Some(item));
5759 if self.check_keyword(keywords::Unsafe) &&
5760 self.look_ahead(1, |t| t.is_keyword(keywords::Impl))
5763 self.expect_keyword(keywords::Unsafe)?;
5764 self.expect_keyword(keywords::Impl)?;
5765 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe)?;
5766 let prev_span = self.prev_span;
5767 let item = self.mk_item(lo,
5772 maybe_append(attrs, extra_attrs));
5773 return Ok(Some(item));
5775 if self.check_keyword(keywords::Fn) {
5778 let fn_span = self.prev_span;
5779 let (ident, item_, extra_attrs) =
5780 self.parse_item_fn(Unsafety::Normal,
5781 respan(fn_span, Constness::NotConst),
5783 let prev_span = self.prev_span;
5784 let item = self.mk_item(lo,
5789 maybe_append(attrs, extra_attrs));
5790 return Ok(Some(item));
5792 if self.check_keyword(keywords::Unsafe)
5793 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) {
5794 // UNSAFE FUNCTION ITEM
5796 let abi = if self.eat_keyword(keywords::Extern) {
5797 self.parse_opt_abi()?.unwrap_or(Abi::C)
5801 self.expect_keyword(keywords::Fn)?;
5802 let fn_span = self.prev_span;
5803 let (ident, item_, extra_attrs) =
5804 self.parse_item_fn(Unsafety::Unsafe,
5805 respan(fn_span, Constness::NotConst),
5807 let prev_span = self.prev_span;
5808 let item = self.mk_item(lo,
5813 maybe_append(attrs, extra_attrs));
5814 return Ok(Some(item));
5816 if self.eat_keyword(keywords::Mod) {
5818 let (ident, item_, extra_attrs) =
5819 self.parse_item_mod(&attrs[..])?;
5820 let prev_span = self.prev_span;
5821 let item = self.mk_item(lo,
5826 maybe_append(attrs, extra_attrs));
5827 return Ok(Some(item));
5829 if self.eat_keyword(keywords::Type) {
5831 let (ident, item_, extra_attrs) = self.parse_item_type()?;
5832 let prev_span = self.prev_span;
5833 let item = self.mk_item(lo,
5838 maybe_append(attrs, extra_attrs));
5839 return Ok(Some(item));
5841 if self.eat_keyword(keywords::Enum) {
5843 let (ident, item_, extra_attrs) = self.parse_item_enum()?;
5844 let prev_span = self.prev_span;
5845 let item = self.mk_item(lo,
5850 maybe_append(attrs, extra_attrs));
5851 return Ok(Some(item));
5853 if self.eat_keyword(keywords::Trait) {
5855 let (ident, item_, extra_attrs) =
5856 self.parse_item_trait(ast::Unsafety::Normal)?;
5857 let prev_span = self.prev_span;
5858 let item = self.mk_item(lo,
5863 maybe_append(attrs, extra_attrs));
5864 return Ok(Some(item));
5866 if self.eat_keyword(keywords::Impl) {
5868 let (ident, item_, extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal)?;
5869 let prev_span = self.prev_span;
5870 let item = self.mk_item(lo,
5875 maybe_append(attrs, extra_attrs));
5876 return Ok(Some(item));
5878 if self.eat_keyword(keywords::Struct) {
5880 let (ident, item_, extra_attrs) = self.parse_item_struct()?;
5881 let prev_span = self.prev_span;
5882 let item = self.mk_item(lo,
5887 maybe_append(attrs, extra_attrs));
5888 return Ok(Some(item));
5890 if self.is_union_item() {
5893 let (ident, item_, extra_attrs) = self.parse_item_union()?;
5894 let prev_span = self.prev_span;
5895 let item = self.mk_item(lo,
5900 maybe_append(attrs, extra_attrs));
5901 return Ok(Some(item));
5903 self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
5906 /// Parse a foreign item.
5907 fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
5908 let attrs = self.parse_outer_attributes()?;
5909 let lo = self.span.lo;
5910 let visibility = self.parse_visibility(true)?;
5912 if self.check_keyword(keywords::Static) {
5913 // FOREIGN STATIC ITEM
5914 return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
5916 if self.check_keyword(keywords::Fn) {
5917 // FOREIGN FUNCTION ITEM
5918 return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
5921 // FIXME #5668: this will occur for a macro invocation:
5922 match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
5924 return Err(self.span_fatal(item.span, "macros cannot expand to foreign items"));
5930 /// This is the fall-through for parsing items.
5931 fn parse_macro_use_or_failure(
5933 attrs: Vec<Attribute> ,
5934 macros_allowed: bool,
5935 attributes_allowed: bool,
5937 visibility: Visibility
5938 ) -> PResult<'a, Option<P<Item>>> {
5939 if macros_allowed && self.token.is_path_start() {
5940 // MACRO INVOCATION ITEM
5942 let prev_span = self.prev_span;
5943 self.complain_if_pub_macro(&visibility, prev_span);
5945 let mac_lo = self.span.lo;
5948 let pth = self.parse_path(PathStyle::Mod)?;
5949 self.expect(&token::Not)?;
5951 // a 'special' identifier (like what `macro_rules!` uses)
5952 // is optional. We should eventually unify invoc syntax
5954 let id = if self.token.is_ident() {
5957 keywords::Invalid.ident() // no special identifier
5959 // eat a matched-delimiter token tree:
5960 let delim = self.expect_open_delim()?;
5961 let tts = self.parse_seq_to_end(&token::CloseDelim(delim),
5963 |p| p.parse_token_tree())?;
5964 if delim != token::Brace {
5965 if !self.eat(&token::Semi) {
5966 let prev_span = self.prev_span;
5967 self.span_err(prev_span,
5968 "macros that expand to items must either \
5969 be surrounded with braces or followed by \
5974 let hi = self.prev_span.hi;
5975 let mac = spanned(mac_lo, hi, Mac_ { path: pth, tts: tts });
5976 let item = self.mk_item(lo, hi, id, ItemKind::Mac(mac), visibility, attrs);
5977 return Ok(Some(item));
5980 // FAILURE TO PARSE ITEM
5982 Visibility::Inherited => {}
5984 let prev_span = self.prev_span;
5985 return Err(self.span_fatal(prev_span, "unmatched visibility `pub`"));
5989 if !attributes_allowed && !attrs.is_empty() {
5990 self.expected_item_err(&attrs);
5995 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
5996 let attrs = self.parse_outer_attributes()?;
5997 self.parse_item_(attrs, true, false)
6000 fn parse_path_list_items(&mut self) -> PResult<'a, Vec<ast::PathListItem>> {
6001 self.parse_unspanned_seq(&token::OpenDelim(token::Brace),
6002 &token::CloseDelim(token::Brace),
6003 SeqSep::trailing_allowed(token::Comma), |this| {
6004 let lo = this.span.lo;
6005 let ident = if this.eat_keyword(keywords::SelfValue) {
6006 keywords::SelfValue.ident()
6010 let rename = this.parse_rename()?;
6011 let node = ast::PathListItem_ {
6014 id: ast::DUMMY_NODE_ID
6016 let hi = this.prev_span.hi;
6017 Ok(spanned(lo, hi, node))
6022 fn is_import_coupler(&mut self) -> bool {
6023 self.check(&token::ModSep) &&
6024 self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
6025 *t == token::BinOp(token::Star))
6028 /// Matches ViewPath:
6029 /// MOD_SEP? non_global_path
6030 /// MOD_SEP? non_global_path as IDENT
6031 /// MOD_SEP? non_global_path MOD_SEP STAR
6032 /// MOD_SEP? non_global_path MOD_SEP LBRACE item_seq RBRACE
6033 /// MOD_SEP? LBRACE item_seq RBRACE
6034 fn parse_view_path(&mut self) -> PResult<'a, P<ViewPath>> {
6035 let lo = self.span.lo;
6036 if self.check(&token::OpenDelim(token::Brace)) || self.check(&token::BinOp(token::Star)) ||
6037 self.is_import_coupler() {
6038 // `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`.
6039 self.eat(&token::ModSep);
6040 let prefix = ast::Path {
6041 segments: vec![ast::PathSegment::crate_root()],
6042 span: mk_sp(lo, self.span.hi),
6044 let view_path_kind = if self.eat(&token::BinOp(token::Star)) {
6045 ViewPathGlob(prefix)
6047 ViewPathList(prefix, self.parse_path_list_items()?)
6049 Ok(P(spanned(lo, self.span.hi, view_path_kind)))
6051 let prefix = self.parse_path(PathStyle::Mod)?.default_to_global();
6052 if self.is_import_coupler() {
6053 // `foo::bar::{a, b}` or `foo::bar::*`
6055 if self.check(&token::BinOp(token::Star)) {
6057 Ok(P(spanned(lo, self.span.hi, ViewPathGlob(prefix))))
6059 let items = self.parse_path_list_items()?;
6060 Ok(P(spanned(lo, self.span.hi, ViewPathList(prefix, items))))
6063 // `foo::bar` or `foo::bar as baz`
6064 let rename = self.parse_rename()?.
6065 unwrap_or(prefix.segments.last().unwrap().identifier);
6066 Ok(P(spanned(lo, self.prev_span.hi, ViewPathSimple(rename, prefix))))
6071 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
6072 if self.eat_keyword(keywords::As) {
6073 self.parse_ident().map(Some)
6079 /// Parses a source module as a crate. This is the main
6080 /// entry point for the parser.
6081 pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> {
6082 let lo = self.span.lo;
6084 attrs: self.parse_inner_attributes()?,
6085 module: self.parse_mod_items(&token::Eof, lo)?,
6086 span: mk_sp(lo, self.span.lo),
6087 exported_macros: Vec::new(),
6091 pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
6092 let ret = match self.token {
6093 token::Literal(token::Str_(s), suf) => (s, ast::StrStyle::Cooked, suf),
6094 token::Literal(token::StrRaw(s, n), suf) => (s, ast::StrStyle::Raw(n), suf),
6101 pub fn parse_str(&mut self) -> PResult<'a, (Symbol, StrStyle)> {
6102 match self.parse_optional_str() {
6103 Some((s, style, suf)) => {
6104 let sp = self.prev_span;
6105 self.expect_no_suffix(sp, "string literal", suf);
6108 _ => Err(self.fatal("expected string literal"))