]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/parse/parser.rs
c1819307928ba0fe18af88ad93b43ebbdb1a4d94
[rustc.git] / src / libsyntax / parse / parser.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use abi::{self, Abi};
12 use ast::{AngleBracketedParameterData, ParenthesizedParameterData, AttrStyle, BareFnTy};
13 use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
14 use ast::Unsafety;
15 use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind};
16 use ast::Block;
17 use ast::{BlockCheckMode, CaptureBy};
18 use ast::{Constness, Crate};
19 use ast::Defaultness;
20 use ast::EnumDef;
21 use ast::{Expr, ExprKind, RangeLimits};
22 use ast::{Field, FnDecl};
23 use ast::{ForeignItem, ForeignItemKind, FunctionRetTy};
24 use ast::{Ident, ImplItem, IsAuto, Item, ItemKind};
25 use ast::{Lifetime, LifetimeDef, Lit, LitKind, UintTy};
26 use ast::Local;
27 use ast::MacStmtStyle;
28 use ast::Mac_;
29 use ast::{MutTy, Mutability};
30 use ast::{Pat, PatKind, PathSegment};
31 use ast::{PolyTraitRef, QSelf};
32 use ast::{Stmt, StmtKind};
33 use ast::{VariantData, StructField};
34 use ast::StrStyle;
35 use ast::SelfKind;
36 use ast::{TraitItem, TraitRef, TraitObjectSyntax};
37 use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
38 use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
39 use ast::{Visibility, WhereClause, CrateSugar};
40 use ast::{BinOpKind, UnOp};
41 use ast::{RangeEnd, RangeSyntax};
42 use {ast, attr};
43 use codemap::{self, CodeMap, Spanned, respan};
44 use syntax_pos::{self, Span, BytePos};
45 use errors::{self, DiagnosticBuilder};
46 use parse::{self, classify, token};
47 use parse::common::SeqSep;
48 use parse::lexer::TokenAndSpan;
49 use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
50 use parse::obsolete::ObsoleteSyntax;
51 use parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership};
52 use util::parser::{AssocOp, Fixity};
53 use print::pprust;
54 use ptr::P;
55 use parse::PResult;
56 use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream};
57 use symbol::{Symbol, keywords};
58 use util::ThinVec;
59
60 use std::cmp;
61 use std::collections::HashSet;
62 use std::mem;
63 use std::path::{self, Path, PathBuf};
64 use std::slice;
65
66 bitflags! {
67 pub struct Restrictions: u8 {
68 const STMT_EXPR = 1 << 0;
69 const NO_STRUCT_LITERAL = 1 << 1;
70 }
71 }
72
73 type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute> >);
74
75 /// How to parse a path.
76 #[derive(Copy, Clone, PartialEq)]
77 pub enum PathStyle {
78 /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
79 /// with something else. For example, in expressions `segment < ....` can be interpreted
80 /// as a comparison and `segment ( ....` can be interpreted as a function call.
81 /// In all such contexts the non-path interpretation is preferred by default for practical
82 /// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
83 /// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
84 Expr,
85 /// In other contexts, notably in types, no ambiguity exists and paths can be written
86 /// without the disambiguator, e.g. `x<y>` - unambiguously a path.
87 /// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
88 Type,
89 /// A path with generic arguments disallowed, e.g. `foo::bar::Baz`, used in imports,
90 /// visibilities or attributes.
91 /// Technically, this variant is unnecessary and e.g. `Expr` can be used instead
92 /// (paths in "mod" contexts have to be checked later for absence of generic arguments
93 /// anyway, due to macros), but it is used to avoid weird suggestions about expected
94 /// tokens when something goes wrong.
95 Mod,
96 }
97
98 #[derive(Clone, Copy, Debug, PartialEq)]
99 pub enum SemiColonMode {
100 Break,
101 Ignore,
102 }
103
104 #[derive(Clone, Copy, Debug, PartialEq)]
105 pub enum BlockMode {
106 Break,
107 Ignore,
108 }
109
110 /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression
111 /// dropped into the token stream, which happens while parsing the result of
112 /// macro expansion). Placement of these is not as complex as I feared it would
113 /// be. The important thing is to make sure that lookahead doesn't balk at
114 /// `token::Interpolated` tokens.
115 macro_rules! maybe_whole_expr {
116 ($p:expr) => {
117 if let token::Interpolated(nt) = $p.token.clone() {
118 match nt.0 {
119 token::NtExpr(ref e) => {
120 $p.bump();
121 return Ok((*e).clone());
122 }
123 token::NtPath(ref path) => {
124 $p.bump();
125 let span = $p.span;
126 let kind = ExprKind::Path(None, (*path).clone());
127 return Ok($p.mk_expr(span, kind, ThinVec::new()));
128 }
129 token::NtBlock(ref block) => {
130 $p.bump();
131 let span = $p.span;
132 let kind = ExprKind::Block((*block).clone());
133 return Ok($p.mk_expr(span, kind, ThinVec::new()));
134 }
135 _ => {},
136 };
137 }
138 }
139 }
140
141 /// As maybe_whole_expr, but for things other than expressions
142 macro_rules! maybe_whole {
143 ($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
144 if let token::Interpolated(nt) = $p.token.clone() {
145 if let token::$constructor($x) = nt.0.clone() {
146 $p.bump();
147 return Ok($e);
148 }
149 }
150 };
151 }
152
153 fn maybe_append(mut lhs: Vec<Attribute>, rhs: Option<Vec<Attribute>>)
154 -> Vec<Attribute> {
155 if let Some(ref attrs) = rhs {
156 lhs.extend(attrs.iter().cloned())
157 }
158 lhs
159 }
160
161 #[derive(Debug, Clone, Copy, PartialEq)]
162 enum PrevTokenKind {
163 DocComment,
164 Comma,
165 Plus,
166 Interpolated,
167 Eof,
168 Ident,
169 Other,
170 }
171
172 /* ident is handled by common.rs */
173
174 #[derive(Clone)]
175 pub struct Parser<'a> {
176 pub sess: &'a ParseSess,
177 /// the current token:
178 pub token: token::Token,
179 /// the span of the current token:
180 pub span: Span,
181 /// the span of the previous token:
182 pub meta_var_span: Option<Span>,
183 pub prev_span: Span,
184 /// the previous token kind
185 prev_token_kind: PrevTokenKind,
186 pub restrictions: Restrictions,
187 /// The set of seen errors about obsolete syntax. Used to suppress
188 /// extra detail when the same error is seen twice
189 pub obsolete_set: HashSet<ObsoleteSyntax>,
190 /// Used to determine the path to externally loaded source files
191 pub directory: Directory,
192 /// Whether to parse sub-modules in other files.
193 pub recurse_into_file_modules: bool,
194 /// Name of the root module this parser originated from. If `None`, then the
195 /// name is not known. This does not change while the parser is descending
196 /// into modules, and sub-parsers have new values for this name.
197 pub root_module_name: Option<String>,
198 pub expected_tokens: Vec<TokenType>,
199 token_cursor: TokenCursor,
200 pub desugar_doc_comments: bool,
201 /// Whether we should configure out of line modules as we parse.
202 pub cfg_mods: bool,
203 }
204
205
206 #[derive(Clone)]
207 struct TokenCursor {
208 frame: TokenCursorFrame,
209 stack: Vec<TokenCursorFrame>,
210 }
211
212 #[derive(Clone)]
213 struct TokenCursorFrame {
214 delim: token::DelimToken,
215 span: Span,
216 open_delim: bool,
217 tree_cursor: tokenstream::Cursor,
218 close_delim: bool,
219 last_token: LastToken,
220 }
221
222 /// This is used in `TokenCursorFrame` above to track tokens that are consumed
223 /// by the parser, and then that's transitively used to record the tokens that
224 /// each parse AST item is created with.
225 ///
226 /// Right now this has two states, either collecting tokens or not collecting
227 /// tokens. If we're collecting tokens we just save everything off into a local
228 /// `Vec`. This should eventually though likely save tokens from the original
229 /// token stream and just use slicing of token streams to avoid creation of a
230 /// whole new vector.
231 ///
232 /// The second state is where we're passively not recording tokens, but the last
233 /// token is still tracked for when we want to start recording tokens. This
234 /// "last token" means that when we start recording tokens we'll want to ensure
235 /// that this, the first token, is included in the output.
236 ///
237 /// You can find some more example usage of this in the `collect_tokens` method
238 /// on the parser.
239 #[derive(Clone)]
240 enum LastToken {
241 Collecting(Vec<TokenTree>),
242 Was(Option<TokenTree>),
243 }
244
245 impl TokenCursorFrame {
246 fn new(sp: Span, delimited: &Delimited) -> Self {
247 TokenCursorFrame {
248 delim: delimited.delim,
249 span: sp,
250 open_delim: delimited.delim == token::NoDelim,
251 tree_cursor: delimited.stream().into_trees(),
252 close_delim: delimited.delim == token::NoDelim,
253 last_token: LastToken::Was(None),
254 }
255 }
256 }
257
258 impl TokenCursor {
259 fn next(&mut self) -> TokenAndSpan {
260 loop {
261 let tree = if !self.frame.open_delim {
262 self.frame.open_delim = true;
263 Delimited { delim: self.frame.delim, tts: TokenStream::empty().into() }
264 .open_tt(self.frame.span)
265 } else if let Some(tree) = self.frame.tree_cursor.next() {
266 tree
267 } else if !self.frame.close_delim {
268 self.frame.close_delim = true;
269 Delimited { delim: self.frame.delim, tts: TokenStream::empty().into() }
270 .close_tt(self.frame.span)
271 } else if let Some(frame) = self.stack.pop() {
272 self.frame = frame;
273 continue
274 } else {
275 return TokenAndSpan { tok: token::Eof, sp: syntax_pos::DUMMY_SP }
276 };
277
278 match self.frame.last_token {
279 LastToken::Collecting(ref mut v) => v.push(tree.clone()),
280 LastToken::Was(ref mut t) => *t = Some(tree.clone()),
281 }
282
283 match tree {
284 TokenTree::Token(sp, tok) => return TokenAndSpan { tok: tok, sp: sp },
285 TokenTree::Delimited(sp, ref delimited) => {
286 let frame = TokenCursorFrame::new(sp, delimited);
287 self.stack.push(mem::replace(&mut self.frame, frame));
288 }
289 }
290 }
291 }
292
293 fn next_desugared(&mut self) -> TokenAndSpan {
294 let (sp, name) = match self.next() {
295 TokenAndSpan { sp, tok: token::DocComment(name) } => (sp, name),
296 tok => return tok,
297 };
298
299 let stripped = strip_doc_comment_decoration(&name.as_str());
300
301 // Searches for the occurrences of `"#*` and returns the minimum number of `#`s
302 // required to wrap the text.
303 let mut num_of_hashes = 0;
304 let mut count = 0;
305 for ch in stripped.chars() {
306 count = match ch {
307 '"' => 1,
308 '#' if count > 0 => count + 1,
309 _ => 0,
310 };
311 num_of_hashes = cmp::max(num_of_hashes, count);
312 }
313
314 let body = TokenTree::Delimited(sp, Delimited {
315 delim: token::Bracket,
316 tts: [TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))),
317 TokenTree::Token(sp, token::Eq),
318 TokenTree::Token(sp, token::Literal(
319 token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))]
320 .iter().cloned().collect::<TokenStream>().into(),
321 });
322
323 self.stack.push(mem::replace(&mut self.frame, TokenCursorFrame::new(sp, &Delimited {
324 delim: token::NoDelim,
325 tts: if doc_comment_style(&name.as_str()) == AttrStyle::Inner {
326 [TokenTree::Token(sp, token::Pound), TokenTree::Token(sp, token::Not), body]
327 .iter().cloned().collect::<TokenStream>().into()
328 } else {
329 [TokenTree::Token(sp, token::Pound), body]
330 .iter().cloned().collect::<TokenStream>().into()
331 },
332 })));
333
334 self.next()
335 }
336 }
337
338 #[derive(PartialEq, Eq, Clone)]
339 pub enum TokenType {
340 Token(token::Token),
341 Keyword(keywords::Keyword),
342 Operator,
343 Lifetime,
344 Ident,
345 Path,
346 Type,
347 }
348
349 impl TokenType {
350 fn to_string(&self) -> String {
351 match *self {
352 TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
353 TokenType::Keyword(kw) => format!("`{}`", kw.name()),
354 TokenType::Operator => "an operator".to_string(),
355 TokenType::Lifetime => "lifetime".to_string(),
356 TokenType::Ident => "identifier".to_string(),
357 TokenType::Path => "path".to_string(),
358 TokenType::Type => "type".to_string(),
359 }
360 }
361 }
362
363 // Returns true if `IDENT t` can start a type - `IDENT::a::b`, `IDENT<u8, u8>`,
364 // `IDENT<<u8 as Trait>::AssocTy>`, `IDENT(u8, u8) -> u8`.
365 fn can_continue_type_after_ident(t: &token::Token) -> bool {
366 t == &token::ModSep || t == &token::Lt ||
367 t == &token::BinOp(token::Shl) || t == &token::OpenDelim(token::Paren)
368 }
369
370 /// Information about the path to a module.
371 pub struct ModulePath {
372 pub name: String,
373 pub path_exists: bool,
374 pub result: Result<ModulePathSuccess, Error>,
375 }
376
377 pub struct ModulePathSuccess {
378 pub path: PathBuf,
379 pub directory_ownership: DirectoryOwnership,
380 warn: bool,
381 }
382
383 pub struct ModulePathError {
384 pub err_msg: String,
385 pub help_msg: String,
386 }
387
388 pub enum Error {
389 FileNotFoundForModule {
390 mod_name: String,
391 default_path: String,
392 secondary_path: String,
393 dir_path: String,
394 },
395 DuplicatePaths {
396 mod_name: String,
397 default_path: String,
398 secondary_path: String,
399 },
400 UselessDocComment,
401 InclusiveRangeWithNoEnd,
402 }
403
404 impl Error {
405 pub fn span_err(self, sp: Span, handler: &errors::Handler) -> DiagnosticBuilder {
406 match self {
407 Error::FileNotFoundForModule { ref mod_name,
408 ref default_path,
409 ref secondary_path,
410 ref dir_path } => {
411 let mut err = struct_span_err!(handler, sp, E0583,
412 "file not found for module `{}`", mod_name);
413 err.help(&format!("name the file either {} or {} inside the directory {:?}",
414 default_path,
415 secondary_path,
416 dir_path));
417 err
418 }
419 Error::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
420 let mut err = struct_span_err!(handler, sp, E0584,
421 "file for module `{}` found at both {} and {}",
422 mod_name,
423 default_path,
424 secondary_path);
425 err.help("delete or rename one of them to remove the ambiguity");
426 err
427 }
428 Error::UselessDocComment => {
429 let mut err = struct_span_err!(handler, sp, E0585,
430 "found a documentation comment that doesn't document anything");
431 err.help("doc comments must come before what they document, maybe a comment was \
432 intended with `//`?");
433 err
434 }
435 Error::InclusiveRangeWithNoEnd => {
436 let mut err = struct_span_err!(handler, sp, E0586,
437 "inclusive range with no end");
438 err.help("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)");
439 err
440 }
441 }
442 }
443 }
444
445 #[derive(Debug)]
446 pub enum LhsExpr {
447 NotYetParsed,
448 AttributesParsed(ThinVec<Attribute>),
449 AlreadyParsed(P<Expr>),
450 }
451
452 impl From<Option<ThinVec<Attribute>>> for LhsExpr {
453 fn from(o: Option<ThinVec<Attribute>>) -> Self {
454 if let Some(attrs) = o {
455 LhsExpr::AttributesParsed(attrs)
456 } else {
457 LhsExpr::NotYetParsed
458 }
459 }
460 }
461
462 impl From<P<Expr>> for LhsExpr {
463 fn from(expr: P<Expr>) -> Self {
464 LhsExpr::AlreadyParsed(expr)
465 }
466 }
467
468 /// Create a placeholder argument.
469 fn dummy_arg(span: Span) -> Arg {
470 let spanned = Spanned {
471 span,
472 node: keywords::Invalid.ident()
473 };
474 let pat = P(Pat {
475 id: ast::DUMMY_NODE_ID,
476 node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), spanned, None),
477 span,
478 });
479 let ty = Ty {
480 node: TyKind::Err,
481 span,
482 id: ast::DUMMY_NODE_ID
483 };
484 Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID }
485 }
486
487 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
488 enum TokenExpectType {
489 Expect,
490 NoExpect,
491 }
492
493 impl<'a> Parser<'a> {
494 pub fn new(sess: &'a ParseSess,
495 tokens: TokenStream,
496 directory: Option<Directory>,
497 recurse_into_file_modules: bool,
498 desugar_doc_comments: bool)
499 -> Self {
500 let mut parser = Parser {
501 sess,
502 token: token::Underscore,
503 span: syntax_pos::DUMMY_SP,
504 prev_span: syntax_pos::DUMMY_SP,
505 meta_var_span: None,
506 prev_token_kind: PrevTokenKind::Other,
507 restrictions: Restrictions::empty(),
508 obsolete_set: HashSet::new(),
509 recurse_into_file_modules,
510 directory: Directory { path: PathBuf::new(), ownership: DirectoryOwnership::Owned },
511 root_module_name: None,
512 expected_tokens: Vec::new(),
513 token_cursor: TokenCursor {
514 frame: TokenCursorFrame::new(syntax_pos::DUMMY_SP, &Delimited {
515 delim: token::NoDelim,
516 tts: tokens.into(),
517 }),
518 stack: Vec::new(),
519 },
520 desugar_doc_comments,
521 cfg_mods: true,
522 };
523
524 let tok = parser.next_tok();
525 parser.token = tok.tok;
526 parser.span = tok.sp;
527
528 if let Some(directory) = directory {
529 parser.directory = directory;
530 } else if parser.span != syntax_pos::DUMMY_SP {
531 parser.directory.path = sess.codemap().span_to_unmapped_path(parser.span);
532 parser.directory.path.pop();
533 }
534
535 parser.process_potential_macro_variable();
536 parser
537 }
538
539 fn next_tok(&mut self) -> TokenAndSpan {
540 let mut next = if self.desugar_doc_comments {
541 self.token_cursor.next_desugared()
542 } else {
543 self.token_cursor.next()
544 };
545 if next.sp == syntax_pos::DUMMY_SP {
546 next.sp = self.prev_span;
547 }
548 next
549 }
550
551 /// Convert a token to a string using self's reader
552 pub fn token_to_string(token: &token::Token) -> String {
553 pprust::token_to_string(token)
554 }
555
556 /// Convert the current token to a string using self's reader
557 pub fn this_token_to_string(&self) -> String {
558 Parser::token_to_string(&self.token)
559 }
560
561 pub fn this_token_descr(&self) -> String {
562 let prefix = match &self.token {
563 t if t.is_special_ident() => "reserved identifier ",
564 t if t.is_used_keyword() => "keyword ",
565 t if t.is_unused_keyword() => "reserved keyword ",
566 _ => "",
567 };
568 format!("{}`{}`", prefix, self.this_token_to_string())
569 }
570
571 pub fn unexpected_last<T>(&self, t: &token::Token) -> PResult<'a, T> {
572 let token_str = Parser::token_to_string(t);
573 Err(self.span_fatal(self.prev_span, &format!("unexpected token: `{}`", token_str)))
574 }
575
576 pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
577 match self.expect_one_of(&[], &[]) {
578 Err(e) => Err(e),
579 Ok(_) => unreachable!(),
580 }
581 }
582
583 /// Expect and consume the token t. Signal an error if
584 /// the next token is not t.
585 pub fn expect(&mut self, t: &token::Token) -> PResult<'a, ()> {
586 if self.expected_tokens.is_empty() {
587 if self.token == *t {
588 self.bump();
589 Ok(())
590 } else {
591 let token_str = Parser::token_to_string(t);
592 let this_token_str = self.this_token_to_string();
593 Err(self.fatal(&format!("expected `{}`, found `{}`",
594 token_str,
595 this_token_str)))
596 }
597 } else {
598 self.expect_one_of(unsafe { slice::from_raw_parts(t, 1) }, &[])
599 }
600 }
601
602 /// Expect next token to be edible or inedible token. If edible,
603 /// then consume it; if inedible, then return without consuming
604 /// anything. Signal a fatal error if next token is unexpected.
605 pub fn expect_one_of(&mut self,
606 edible: &[token::Token],
607 inedible: &[token::Token]) -> PResult<'a, ()>{
608 fn tokens_to_string(tokens: &[TokenType]) -> String {
609 let mut i = tokens.iter();
610 // This might be a sign we need a connect method on Iterator.
611 let b = i.next()
612 .map_or("".to_string(), |t| t.to_string());
613 i.enumerate().fold(b, |mut b, (i, a)| {
614 if tokens.len() > 2 && i == tokens.len() - 2 {
615 b.push_str(", or ");
616 } else if tokens.len() == 2 && i == tokens.len() - 2 {
617 b.push_str(" or ");
618 } else {
619 b.push_str(", ");
620 }
621 b.push_str(&a.to_string());
622 b
623 })
624 }
625 if edible.contains(&self.token) {
626 self.bump();
627 Ok(())
628 } else if inedible.contains(&self.token) {
629 // leave it in the input
630 Ok(())
631 } else {
632 let mut expected = edible.iter()
633 .map(|x| TokenType::Token(x.clone()))
634 .chain(inedible.iter().map(|x| TokenType::Token(x.clone())))
635 .chain(self.expected_tokens.iter().cloned())
636 .collect::<Vec<_>>();
637 expected.sort_by(|a, b| a.to_string().cmp(&b.to_string()));
638 expected.dedup();
639 let expect = tokens_to_string(&expected[..]);
640 let actual = self.this_token_to_string();
641 let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
642 let short_expect = if expected.len() > 6 {
643 format!("{} possible tokens", expected.len())
644 } else {
645 expect.clone()
646 };
647 (format!("expected one of {}, found `{}`", expect, actual),
648 (self.prev_span.next_point(), format!("expected one of {} here", short_expect)))
649 } else if expected.is_empty() {
650 (format!("unexpected token: `{}`", actual),
651 (self.prev_span, "unexpected token after this".to_string()))
652 } else {
653 (format!("expected {}, found `{}`", expect, actual),
654 (self.prev_span.next_point(), format!("expected {} here", expect)))
655 };
656 let mut err = self.fatal(&msg_exp);
657 let sp = if self.token == token::Token::Eof {
658 // This is EOF, don't want to point at the following char, but rather the last token
659 self.prev_span
660 } else {
661 label_sp
662 };
663 if self.span.contains(sp) {
664 err.span_label(self.span, label_exp);
665 } else {
666 err.span_label(sp, label_exp);
667 err.span_label(self.span, "unexpected token");
668 }
669 Err(err)
670 }
671 }
672
673 /// returns the span of expr, if it was not interpolated or the span of the interpolated token
674 fn interpolated_or_expr_span(&self,
675 expr: PResult<'a, P<Expr>>)
676 -> PResult<'a, (Span, P<Expr>)> {
677 expr.map(|e| {
678 if self.prev_token_kind == PrevTokenKind::Interpolated {
679 (self.prev_span, e)
680 } else {
681 (e.span, e)
682 }
683 })
684 }
685
686 pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
687 match self.token {
688 token::Ident(i) => {
689 if self.token.is_reserved_ident() {
690 self.span_err(self.span, &format!("expected identifier, found {}",
691 self.this_token_descr()));
692 }
693 self.bump();
694 Ok(i)
695 }
696 _ => {
697 Err(if self.prev_token_kind == PrevTokenKind::DocComment {
698 self.span_fatal_err(self.prev_span, Error::UselessDocComment)
699 } else {
700 let mut err = self.fatal(&format!("expected identifier, found `{}`",
701 self.this_token_to_string()));
702 if self.token == token::Underscore {
703 err.note("`_` is a wildcard pattern, not an identifier");
704 }
705 err
706 })
707 }
708 }
709 }
710
711 /// Check if the next token is `tok`, and return `true` if so.
712 ///
713 /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
714 /// encountered.
715 pub fn check(&mut self, tok: &token::Token) -> bool {
716 let is_present = self.token == *tok;
717 if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
718 is_present
719 }
720
721 /// Consume token 'tok' if it exists. Returns true if the given
722 /// token was present, false otherwise.
723 pub fn eat(&mut self, tok: &token::Token) -> bool {
724 let is_present = self.check(tok);
725 if is_present { self.bump() }
726 is_present
727 }
728
729 pub fn check_keyword(&mut self, kw: keywords::Keyword) -> bool {
730 self.expected_tokens.push(TokenType::Keyword(kw));
731 self.token.is_keyword(kw)
732 }
733
734 /// If the next token is the given keyword, eat it and return
735 /// true. Otherwise, return false.
736 pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
737 if self.check_keyword(kw) {
738 self.bump();
739 true
740 } else {
741 false
742 }
743 }
744
745 pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool {
746 if self.token.is_keyword(kw) {
747 self.bump();
748 true
749 } else {
750 false
751 }
752 }
753
754 /// If the given word is not a keyword, signal an error.
755 /// If the next token is not the given word, signal an error.
756 /// Otherwise, eat it.
757 pub fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> {
758 if !self.eat_keyword(kw) {
759 self.unexpected()
760 } else {
761 Ok(())
762 }
763 }
764
765 fn check_ident(&mut self) -> bool {
766 if self.token.is_ident() {
767 true
768 } else {
769 self.expected_tokens.push(TokenType::Ident);
770 false
771 }
772 }
773
774 fn check_path(&mut self) -> bool {
775 if self.token.is_path_start() {
776 true
777 } else {
778 self.expected_tokens.push(TokenType::Path);
779 false
780 }
781 }
782
783 fn check_type(&mut self) -> bool {
784 if self.token.can_begin_type() {
785 true
786 } else {
787 self.expected_tokens.push(TokenType::Type);
788 false
789 }
790 }
791
792 /// Expect and consume an `&`. If `&&` is seen, replace it with a single
793 /// `&` and continue. If an `&` is not seen, signal an error.
794 fn expect_and(&mut self) -> PResult<'a, ()> {
795 self.expected_tokens.push(TokenType::Token(token::BinOp(token::And)));
796 match self.token {
797 token::BinOp(token::And) => {
798 self.bump();
799 Ok(())
800 }
801 token::AndAnd => {
802 let span = self.span.with_lo(self.span.lo() + BytePos(1));
803 Ok(self.bump_with(token::BinOp(token::And), span))
804 }
805 _ => self.unexpected()
806 }
807 }
808
809 /// Expect and consume an `|`. If `||` is seen, replace it with a single
810 /// `|` and continue. If an `|` is not seen, signal an error.
811 fn expect_or(&mut self) -> PResult<'a, ()> {
812 self.expected_tokens.push(TokenType::Token(token::BinOp(token::Or)));
813 match self.token {
814 token::BinOp(token::Or) => {
815 self.bump();
816 Ok(())
817 }
818 token::OrOr => {
819 let span = self.span.with_lo(self.span.lo() + BytePos(1));
820 Ok(self.bump_with(token::BinOp(token::Or), span))
821 }
822 _ => self.unexpected()
823 }
824 }
825
826 pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
827 match suffix {
828 None => {/* everything ok */}
829 Some(suf) => {
830 let text = suf.as_str();
831 if text.is_empty() {
832 self.span_bug(sp, "found empty literal suffix in Some")
833 }
834 self.span_err(sp, &format!("{} with a suffix is invalid", kind));
835 }
836 }
837 }
838
839 /// Attempt to consume a `<`. If `<<` is seen, replace it with a single
840 /// `<` and continue. If a `<` is not seen, return false.
841 ///
842 /// This is meant to be used when parsing generics on a path to get the
843 /// starting token.
844 fn eat_lt(&mut self) -> bool {
845 self.expected_tokens.push(TokenType::Token(token::Lt));
846 match self.token {
847 token::Lt => {
848 self.bump();
849 true
850 }
851 token::BinOp(token::Shl) => {
852 let span = self.span.with_lo(self.span.lo() + BytePos(1));
853 self.bump_with(token::Lt, span);
854 true
855 }
856 _ => false,
857 }
858 }
859
860 fn expect_lt(&mut self) -> PResult<'a, ()> {
861 if !self.eat_lt() {
862 self.unexpected()
863 } else {
864 Ok(())
865 }
866 }
867
868 /// Expect and consume a GT. if a >> is seen, replace it
869 /// with a single > and continue. If a GT is not seen,
870 /// signal an error.
871 pub fn expect_gt(&mut self) -> PResult<'a, ()> {
872 self.expected_tokens.push(TokenType::Token(token::Gt));
873 match self.token {
874 token::Gt => {
875 self.bump();
876 Ok(())
877 }
878 token::BinOp(token::Shr) => {
879 let span = self.span.with_lo(self.span.lo() + BytePos(1));
880 Ok(self.bump_with(token::Gt, span))
881 }
882 token::BinOpEq(token::Shr) => {
883 let span = self.span.with_lo(self.span.lo() + BytePos(1));
884 Ok(self.bump_with(token::Ge, span))
885 }
886 token::Ge => {
887 let span = self.span.with_lo(self.span.lo() + BytePos(1));
888 Ok(self.bump_with(token::Eq, span))
889 }
890 _ => self.unexpected()
891 }
892 }
893
894 pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
895 sep: Option<token::Token>,
896 mut f: F)
897 -> PResult<'a, (Vec<T>, bool)>
898 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
899 {
900 let mut v = Vec::new();
901 // This loop works by alternating back and forth between parsing types
902 // and commas. For example, given a string `A, B,>`, the parser would
903 // first parse `A`, then a comma, then `B`, then a comma. After that it
904 // would encounter a `>` and stop. This lets the parser handle trailing
905 // commas in generic parameters, because it can stop either after
906 // parsing a type or after parsing a comma.
907 for i in 0.. {
908 if self.check(&token::Gt)
909 || self.token == token::BinOp(token::Shr)
910 || self.token == token::Ge
911 || self.token == token::BinOpEq(token::Shr) {
912 break;
913 }
914
915 if i % 2 == 0 {
916 match f(self)? {
917 Some(result) => v.push(result),
918 None => return Ok((v, true))
919 }
920 } else {
921 if let Some(t) = sep.as_ref() {
922 self.expect(t)?;
923 }
924
925 }
926 }
927 return Ok((v, false));
928 }
929
930 /// Parse a sequence bracketed by '<' and '>', stopping
931 /// before the '>'.
932 pub fn parse_seq_to_before_gt<T, F>(&mut self,
933 sep: Option<token::Token>,
934 mut f: F)
935 -> PResult<'a, Vec<T>> where
936 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
937 {
938 let (result, returned) = self.parse_seq_to_before_gt_or_return(sep,
939 |p| Ok(Some(f(p)?)))?;
940 assert!(!returned);
941 return Ok(result);
942 }
943
944 pub fn parse_seq_to_gt<T, F>(&mut self,
945 sep: Option<token::Token>,
946 f: F)
947 -> PResult<'a, Vec<T>> where
948 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
949 {
950 let v = self.parse_seq_to_before_gt(sep, f)?;
951 self.expect_gt()?;
952 return Ok(v);
953 }
954
955 pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
956 sep: Option<token::Token>,
957 f: F)
958 -> PResult<'a, (Vec<T>, bool)> where
959 F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
960 {
961 let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?;
962 if !returned {
963 self.expect_gt()?;
964 }
965 return Ok((v, returned));
966 }
967
968 /// Eat and discard tokens until one of `kets` is encountered. Respects token trees,
969 /// passes through any errors encountered. Used for error recovery.
970 pub fn eat_to_tokens(&mut self, kets: &[&token::Token]) {
971 let handler = self.diagnostic();
972
973 if let Err(ref mut err) = self.parse_seq_to_before_tokens(kets,
974 SeqSep::none(),
975 TokenExpectType::Expect,
976 |p| Ok(p.parse_token_tree())) {
977 handler.cancel(err);
978 }
979 }
980
981 /// Parse a sequence, including the closing delimiter. The function
982 /// f must consume tokens until reaching the next separator or
983 /// closing bracket.
984 pub fn parse_seq_to_end<T, F>(&mut self,
985 ket: &token::Token,
986 sep: SeqSep,
987 f: F)
988 -> PResult<'a, Vec<T>> where
989 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
990 {
991 let val = self.parse_seq_to_before_end(ket, sep, f)?;
992 self.bump();
993 Ok(val)
994 }
995
996 /// Parse a sequence, not including the closing delimiter. The function
997 /// f must consume tokens until reaching the next separator or
998 /// closing bracket.
999 pub fn parse_seq_to_before_end<T, F>(&mut self,
1000 ket: &token::Token,
1001 sep: SeqSep,
1002 f: F)
1003 -> PResult<'a, Vec<T>>
1004 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>
1005 {
1006 self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
1007 }
1008
1009 fn parse_seq_to_before_tokens<T, F>(&mut self,
1010 kets: &[&token::Token],
1011 sep: SeqSep,
1012 expect: TokenExpectType,
1013 mut f: F)
1014 -> PResult<'a, Vec<T>>
1015 where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>
1016 {
1017 let mut first: bool = true;
1018 let mut v = vec![];
1019 while !kets.contains(&&self.token) {
1020 match self.token {
1021 token::CloseDelim(..) | token::Eof => break,
1022 _ => {}
1023 };
1024 if let Some(ref t) = sep.sep {
1025 if first {
1026 first = false;
1027 } else {
1028 if let Err(mut e) = self.expect(t) {
1029 // Attempt to keep parsing if it was a similar separator
1030 if let Some(ref tokens) = t.similar_tokens() {
1031 if tokens.contains(&self.token) {
1032 self.bump();
1033 }
1034 }
1035 e.emit();
1036 // Attempt to keep parsing if it was an omitted separator
1037 match f(self) {
1038 Ok(t) => {
1039 v.push(t);
1040 continue;
1041 },
1042 Err(mut e) => {
1043 e.cancel();
1044 break;
1045 }
1046 }
1047 }
1048 }
1049 }
1050 if sep.trailing_sep_allowed && kets.iter().any(|k| {
1051 match expect {
1052 TokenExpectType::Expect => self.check(k),
1053 TokenExpectType::NoExpect => self.token == **k,
1054 }
1055 }) {
1056 break;
1057 }
1058
1059 let t = f(self)?;
1060 v.push(t);
1061 }
1062
1063 Ok(v)
1064 }
1065
1066 /// Parse a sequence, including the closing delimiter. The function
1067 /// f must consume tokens until reaching the next separator or
1068 /// closing bracket.
1069 pub fn parse_unspanned_seq<T, F>(&mut self,
1070 bra: &token::Token,
1071 ket: &token::Token,
1072 sep: SeqSep,
1073 f: F)
1074 -> PResult<'a, Vec<T>> where
1075 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1076 {
1077 self.expect(bra)?;
1078 let result = self.parse_seq_to_before_end(ket, sep, f)?;
1079 if self.token == *ket {
1080 self.bump();
1081 }
1082 Ok(result)
1083 }
1084
1085 // NB: Do not use this function unless you actually plan to place the
1086 // spanned list in the AST.
1087 pub fn parse_seq<T, F>(&mut self,
1088 bra: &token::Token,
1089 ket: &token::Token,
1090 sep: SeqSep,
1091 f: F)
1092 -> PResult<'a, Spanned<Vec<T>>> where
1093 F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1094 {
1095 let lo = self.span;
1096 self.expect(bra)?;
1097 let result = self.parse_seq_to_before_end(ket, sep, f)?;
1098 let hi = self.span;
1099 self.bump();
1100 Ok(respan(lo.to(hi), result))
1101 }
1102
1103 /// Advance the parser by one token
1104 pub fn bump(&mut self) {
1105 if self.prev_token_kind == PrevTokenKind::Eof {
1106 // Bumping after EOF is a bad sign, usually an infinite loop.
1107 self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
1108 }
1109
1110 self.prev_span = self.meta_var_span.take().unwrap_or(self.span);
1111
1112 // Record last token kind for possible error recovery.
1113 self.prev_token_kind = match self.token {
1114 token::DocComment(..) => PrevTokenKind::DocComment,
1115 token::Comma => PrevTokenKind::Comma,
1116 token::BinOp(token::Plus) => PrevTokenKind::Plus,
1117 token::Interpolated(..) => PrevTokenKind::Interpolated,
1118 token::Eof => PrevTokenKind::Eof,
1119 token::Ident(..) => PrevTokenKind::Ident,
1120 _ => PrevTokenKind::Other,
1121 };
1122
1123 let next = self.next_tok();
1124 self.span = next.sp;
1125 self.token = next.tok;
1126 self.expected_tokens.clear();
1127 // check after each token
1128 self.process_potential_macro_variable();
1129 }
1130
1131 /// Advance the parser using provided token as a next one. Use this when
1132 /// consuming a part of a token. For example a single `<` from `<<`.
1133 pub fn bump_with(&mut self, next: token::Token, span: Span) {
1134 self.prev_span = self.span.with_hi(span.lo());
1135 // It would be incorrect to record the kind of the current token, but
1136 // fortunately for tokens currently using `bump_with`, the
1137 // prev_token_kind will be of no use anyway.
1138 self.prev_token_kind = PrevTokenKind::Other;
1139 self.span = span;
1140 self.token = next;
1141 self.expected_tokens.clear();
1142 }
1143
1144 pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
1145 F: FnOnce(&token::Token) -> R,
1146 {
1147 if dist == 0 {
1148 return f(&self.token)
1149 }
1150
1151 f(&match self.token_cursor.frame.tree_cursor.look_ahead(dist - 1) {
1152 Some(tree) => match tree {
1153 TokenTree::Token(_, tok) => tok,
1154 TokenTree::Delimited(_, delimited) => token::OpenDelim(delimited.delim),
1155 },
1156 None => token::CloseDelim(self.token_cursor.frame.delim),
1157 })
1158 }
1159 fn look_ahead_span(&self, dist: usize) -> Span {
1160 if dist == 0 {
1161 return self.span
1162 }
1163
1164 match self.token_cursor.frame.tree_cursor.look_ahead(dist - 1) {
1165 Some(TokenTree::Token(span, _)) | Some(TokenTree::Delimited(span, _)) => span,
1166 None => self.look_ahead_span(dist - 1),
1167 }
1168 }
1169 pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> {
1170 self.sess.span_diagnostic.struct_span_fatal(self.span, m)
1171 }
1172 pub fn span_fatal(&self, sp: Span, m: &str) -> DiagnosticBuilder<'a> {
1173 self.sess.span_diagnostic.struct_span_fatal(sp, m)
1174 }
1175 pub fn span_fatal_err(&self, sp: Span, err: Error) -> DiagnosticBuilder<'a> {
1176 err.span_err(sp, self.diagnostic())
1177 }
1178 pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> DiagnosticBuilder<'a> {
1179 let mut err = self.sess.span_diagnostic.struct_span_fatal(sp, m);
1180 err.help(help);
1181 err
1182 }
1183 pub fn bug(&self, m: &str) -> ! {
1184 self.sess.span_diagnostic.span_bug(self.span, m)
1185 }
1186 pub fn warn(&self, m: &str) {
1187 self.sess.span_diagnostic.span_warn(self.span, m)
1188 }
1189 pub fn span_warn(&self, sp: Span, m: &str) {
1190 self.sess.span_diagnostic.span_warn(sp, m)
1191 }
1192 pub fn span_err(&self, sp: Span, m: &str) {
1193 self.sess.span_diagnostic.span_err(sp, m)
1194 }
1195 pub fn span_err_help(&self, sp: Span, m: &str, h: &str) {
1196 let mut err = self.sess.span_diagnostic.mut_span_err(sp, m);
1197 err.help(h);
1198 err.emit();
1199 }
1200 pub fn span_bug(&self, sp: Span, m: &str) -> ! {
1201 self.sess.span_diagnostic.span_bug(sp, m)
1202 }
1203 pub fn abort_if_errors(&self) {
1204 self.sess.span_diagnostic.abort_if_errors();
1205 }
1206
1207 fn cancel(&self, err: &mut DiagnosticBuilder) {
1208 self.sess.span_diagnostic.cancel(err)
1209 }
1210
1211 pub fn diagnostic(&self) -> &'a errors::Handler {
1212 &self.sess.span_diagnostic
1213 }
1214
1215 /// Is the current token one of the keywords that signals a bare function
1216 /// type?
1217 pub fn token_is_bare_fn_keyword(&mut self) -> bool {
1218 self.check_keyword(keywords::Fn) ||
1219 self.check_keyword(keywords::Unsafe) ||
1220 self.check_keyword(keywords::Extern)
1221 }
1222
1223 fn get_label(&mut self) -> ast::Ident {
1224 match self.token {
1225 token::Lifetime(ref ident) => *ident,
1226 _ => self.bug("not a lifetime"),
1227 }
1228 }
1229
1230 /// parse a TyKind::BareFn type:
1231 pub fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<LifetimeDef>)
1232 -> PResult<'a, TyKind> {
1233 /*
1234
1235 [unsafe] [extern "ABI"] fn (S) -> T
1236 ^~~~^ ^~~~^ ^~^ ^
1237 | | | |
1238 | | | Return type
1239 | | Argument types
1240 | |
1241 | ABI
1242 Function Style
1243 */
1244
1245 let unsafety = self.parse_unsafety()?;
1246 let abi = if self.eat_keyword(keywords::Extern) {
1247 self.parse_opt_abi()?.unwrap_or(Abi::C)
1248 } else {
1249 Abi::Rust
1250 };
1251
1252 self.expect_keyword(keywords::Fn)?;
1253 let (inputs, variadic) = self.parse_fn_args(false, true)?;
1254 let ret_ty = self.parse_ret_ty()?;
1255 let decl = P(FnDecl {
1256 inputs,
1257 output: ret_ty,
1258 variadic,
1259 });
1260 Ok(TyKind::BareFn(P(BareFnTy {
1261 abi,
1262 unsafety,
1263 lifetimes: lifetime_defs,
1264 decl,
1265 })))
1266 }
1267
1268 pub fn parse_unsafety(&mut self) -> PResult<'a, Unsafety> {
1269 if self.eat_keyword(keywords::Unsafe) {
1270 return Ok(Unsafety::Unsafe);
1271 } else {
1272 return Ok(Unsafety::Normal);
1273 }
1274 }
1275
1276 /// Parse the items in a trait declaration
1277 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, TraitItem> {
1278 maybe_whole!(self, NtTraitItem, |x| x);
1279 let attrs = self.parse_outer_attributes()?;
1280 let (mut item, tokens) = self.collect_tokens(|this| {
1281 this.parse_trait_item_(at_end, attrs)
1282 })?;
1283 // See `parse_item` for why this clause is here.
1284 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
1285 item.tokens = Some(tokens);
1286 }
1287 Ok(item)
1288 }
1289
1290 fn parse_trait_item_(&mut self,
1291 at_end: &mut bool,
1292 mut attrs: Vec<Attribute>) -> PResult<'a, TraitItem> {
1293 let lo = self.span;
1294
1295 let (name, node, generics) = if self.eat_keyword(keywords::Type) {
1296 let TyParam {ident, bounds, default, ..} = self.parse_ty_param(vec![])?;
1297 self.expect(&token::Semi)?;
1298 (ident, TraitItemKind::Type(bounds, default), ast::Generics::default())
1299 } else if self.is_const_item() {
1300 self.expect_keyword(keywords::Const)?;
1301 let ident = self.parse_ident()?;
1302 self.expect(&token::Colon)?;
1303 let ty = self.parse_ty()?;
1304 let default = if self.check(&token::Eq) {
1305 self.bump();
1306 let expr = self.parse_expr()?;
1307 self.expect(&token::Semi)?;
1308 Some(expr)
1309 } else {
1310 self.expect(&token::Semi)?;
1311 None
1312 };
1313 (ident, TraitItemKind::Const(ty, default), ast::Generics::default())
1314 } else if self.token.is_path_start() {
1315 // trait item macro.
1316 // code copied from parse_macro_use_or_failure... abstraction!
1317 let prev_span = self.prev_span;
1318 let lo = self.span;
1319 let pth = self.parse_path(PathStyle::Mod)?;
1320
1321 if pth.segments.len() == 1 {
1322 if !self.eat(&token::Not) {
1323 return Err(self.missing_assoc_item_kind_err("trait", prev_span));
1324 }
1325 } else {
1326 self.expect(&token::Not)?;
1327 }
1328
1329 // eat a matched-delimiter token tree:
1330 let (delim, tts) = self.expect_delimited_token_tree()?;
1331 if delim != token::Brace {
1332 self.expect(&token::Semi)?
1333 }
1334
1335 let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
1336 (keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac), ast::Generics::default())
1337 } else {
1338 let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
1339
1340 let ident = self.parse_ident()?;
1341 let mut generics = self.parse_generics()?;
1342
1343 let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
1344 // This is somewhat dubious; We don't want to allow
1345 // argument names to be left off if there is a
1346 // definition...
1347 p.parse_arg_general(false)
1348 })?;
1349 generics.where_clause = self.parse_where_clause()?;
1350
1351 let sig = ast::MethodSig {
1352 unsafety,
1353 constness,
1354 decl: d,
1355 abi,
1356 };
1357
1358 let body = match self.token {
1359 token::Semi => {
1360 self.bump();
1361 *at_end = true;
1362 debug!("parse_trait_methods(): parsing required method");
1363 None
1364 }
1365 token::OpenDelim(token::Brace) => {
1366 debug!("parse_trait_methods(): parsing provided method");
1367 *at_end = true;
1368 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1369 attrs.extend(inner_attrs.iter().cloned());
1370 Some(body)
1371 }
1372 _ => {
1373 let token_str = self.this_token_to_string();
1374 return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
1375 }
1376 };
1377 (ident, ast::TraitItemKind::Method(sig, body), generics)
1378 };
1379
1380 Ok(TraitItem {
1381 id: ast::DUMMY_NODE_ID,
1382 ident: name,
1383 attrs,
1384 generics,
1385 node,
1386 span: lo.to(self.prev_span),
1387 tokens: None,
1388 })
1389 }
1390
1391 /// Parse optional return type [ -> TY ] in function decl
1392 pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
1393 if self.eat(&token::RArrow) {
1394 Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
1395 } else {
1396 Ok(FunctionRetTy::Default(self.span.with_hi(self.span.lo())))
1397 }
1398 }
1399
1400 // Parse a type
1401 pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
1402 self.parse_ty_common(true)
1403 }
1404
1405 /// Parse a type in restricted contexts where `+` is not permitted.
1406 /// Example 1: `&'a TYPE`
1407 /// `+` is prohibited to maintain operator priority (P(+) < P(&)).
1408 /// Example 2: `value1 as TYPE + value2`
1409 /// `+` is prohibited to avoid interactions with expression grammar.
1410 fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
1411 self.parse_ty_common(false)
1412 }
1413
1414 fn parse_ty_common(&mut self, allow_plus: bool) -> PResult<'a, P<Ty>> {
1415 maybe_whole!(self, NtTy, |x| x);
1416
1417 let lo = self.span;
1418 let node = if self.eat(&token::OpenDelim(token::Paren)) {
1419 // `(TYPE)` is a parenthesized type.
1420 // `(TYPE,)` is a tuple with a single field of type TYPE.
1421 let mut ts = vec![];
1422 let mut last_comma = false;
1423 while self.token != token::CloseDelim(token::Paren) {
1424 ts.push(self.parse_ty()?);
1425 if self.eat(&token::Comma) {
1426 last_comma = true;
1427 } else {
1428 last_comma = false;
1429 break;
1430 }
1431 }
1432 let trailing_plus = self.prev_token_kind == PrevTokenKind::Plus;
1433 self.expect(&token::CloseDelim(token::Paren))?;
1434
1435 if ts.len() == 1 && !last_comma {
1436 let ty = ts.into_iter().nth(0).unwrap().unwrap();
1437 let maybe_bounds = allow_plus && self.token == token::BinOp(token::Plus);
1438 match ty.node {
1439 // `(TY_BOUND_NOPAREN) + BOUND + ...`.
1440 TyKind::Path(None, ref path) if maybe_bounds => {
1441 self.parse_remaining_bounds(Vec::new(), path.clone(), lo, true)?
1442 }
1443 TyKind::TraitObject(ref bounds, TraitObjectSyntax::None)
1444 if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
1445 let path = match bounds[0] {
1446 TraitTyParamBound(ref pt, ..) => pt.trait_ref.path.clone(),
1447 _ => self.bug("unexpected lifetime bound"),
1448 };
1449 self.parse_remaining_bounds(Vec::new(), path, lo, true)?
1450 }
1451 // `(TYPE)`
1452 _ => TyKind::Paren(P(ty))
1453 }
1454 } else {
1455 TyKind::Tup(ts)
1456 }
1457 } else if self.eat(&token::Not) {
1458 // Never type `!`
1459 TyKind::Never
1460 } else if self.eat(&token::BinOp(token::Star)) {
1461 // Raw pointer
1462 TyKind::Ptr(self.parse_ptr()?)
1463 } else if self.eat(&token::OpenDelim(token::Bracket)) {
1464 // Array or slice
1465 let t = self.parse_ty()?;
1466 // Parse optional `; EXPR` in `[TYPE; EXPR]`
1467 let t = match self.maybe_parse_fixed_length_of_vec()? {
1468 None => TyKind::Slice(t),
1469 Some(suffix) => TyKind::Array(t, suffix),
1470 };
1471 self.expect(&token::CloseDelim(token::Bracket))?;
1472 t
1473 } else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) {
1474 // Reference
1475 self.expect_and()?;
1476 self.parse_borrowed_pointee()?
1477 } else if self.eat_keyword_noexpect(keywords::Typeof) {
1478 // `typeof(EXPR)`
1479 // In order to not be ambiguous, the type must be surrounded by parens.
1480 self.expect(&token::OpenDelim(token::Paren))?;
1481 let e = self.parse_expr()?;
1482 self.expect(&token::CloseDelim(token::Paren))?;
1483 TyKind::Typeof(e)
1484 } else if self.eat(&token::Underscore) {
1485 // A type to be inferred `_`
1486 TyKind::Infer
1487 } else if self.token_is_bare_fn_keyword() {
1488 // Function pointer type
1489 self.parse_ty_bare_fn(Vec::new())?
1490 } else if self.check_keyword(keywords::For) {
1491 // Function pointer type or bound list (trait object type) starting with a poly-trait.
1492 // `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
1493 // `for<'lt> Trait1<'lt> + Trait2 + 'a`
1494 let lo = self.span;
1495 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
1496 if self.token_is_bare_fn_keyword() {
1497 self.parse_ty_bare_fn(lifetime_defs)?
1498 } else {
1499 let path = self.parse_path(PathStyle::Type)?;
1500 let parse_plus = allow_plus && self.check(&token::BinOp(token::Plus));
1501 self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)?
1502 }
1503 } else if self.eat_keyword(keywords::Impl) {
1504 // FIXME: figure out priority of `+` in `impl Trait1 + Trait2` (#34511).
1505 TyKind::ImplTrait(self.parse_ty_param_bounds()?)
1506 } else if self.check_keyword(keywords::Dyn) &&
1507 self.look_ahead(1, |t| t.can_begin_bound() && !can_continue_type_after_ident(t)) {
1508 // FIXME: figure out priority of `+` in `dyn Trait1 + Trait2` (#34511).
1509 self.bump(); // `dyn`
1510 TyKind::TraitObject(self.parse_ty_param_bounds()?, TraitObjectSyntax::Dyn)
1511 } else if self.check(&token::Question) ||
1512 self.check_lifetime() && self.look_ahead(1, |t| t == &token::BinOp(token::Plus)) {
1513 // Bound list (trait object type)
1514 TyKind::TraitObject(self.parse_ty_param_bounds_common(allow_plus)?,
1515 TraitObjectSyntax::None)
1516 } else if self.eat_lt() {
1517 // Qualified path
1518 let (qself, path) = self.parse_qpath(PathStyle::Type)?;
1519 TyKind::Path(Some(qself), path)
1520 } else if self.token.is_path_start() {
1521 // Simple path
1522 let path = self.parse_path(PathStyle::Type)?;
1523 if self.eat(&token::Not) {
1524 // Macro invocation in type position
1525 let (_, tts) = self.expect_delimited_token_tree()?;
1526 TyKind::Mac(respan(lo.to(self.span), Mac_ { path: path, tts: tts }))
1527 } else {
1528 // Just a type path or bound list (trait object type) starting with a trait.
1529 // `Type`
1530 // `Trait1 + Trait2 + 'a`
1531 if allow_plus && self.check(&token::BinOp(token::Plus)) {
1532 self.parse_remaining_bounds(Vec::new(), path, lo, true)?
1533 } else {
1534 TyKind::Path(None, path)
1535 }
1536 }
1537 } else {
1538 let msg = format!("expected type, found {}", self.this_token_descr());
1539 return Err(self.fatal(&msg));
1540 };
1541
1542 let span = lo.to(self.prev_span);
1543 let ty = Ty { node, span, id: ast::DUMMY_NODE_ID };
1544
1545 // Try to recover from use of `+` with incorrect priority.
1546 self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?;
1547
1548 Ok(P(ty))
1549 }
1550
1551 fn parse_remaining_bounds(&mut self, lifetime_defs: Vec<LifetimeDef>, path: ast::Path,
1552 lo: Span, parse_plus: bool) -> PResult<'a, TyKind> {
1553 let poly_trait_ref = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
1554 let mut bounds = vec![TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)];
1555 if parse_plus {
1556 self.bump(); // `+`
1557 bounds.append(&mut self.parse_ty_param_bounds()?);
1558 }
1559 Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
1560 }
1561
1562 fn maybe_recover_from_bad_type_plus(&mut self, allow_plus: bool, ty: &Ty) -> PResult<'a, ()> {
1563 // Do not add `+` to expected tokens.
1564 if !allow_plus || self.token != token::BinOp(token::Plus) {
1565 return Ok(())
1566 }
1567
1568 self.bump(); // `+`
1569 let bounds = self.parse_ty_param_bounds()?;
1570 let sum_span = ty.span.to(self.prev_span);
1571
1572 let mut err = struct_span_err!(self.sess.span_diagnostic, sum_span, E0178,
1573 "expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(ty));
1574
1575 match ty.node {
1576 TyKind::Rptr(ref lifetime, ref mut_ty) => {
1577 let sum_with_parens = pprust::to_string(|s| {
1578 use print::pprust::PrintState;
1579
1580 s.s.word("&")?;
1581 s.print_opt_lifetime(lifetime)?;
1582 s.print_mutability(mut_ty.mutbl)?;
1583 s.popen()?;
1584 s.print_type(&mut_ty.ty)?;
1585 s.print_bounds(" +", &bounds)?;
1586 s.pclose()
1587 });
1588 err.span_suggestion(sum_span, "try adding parentheses", sum_with_parens);
1589 }
1590 TyKind::Ptr(..) | TyKind::BareFn(..) => {
1591 err.span_label(sum_span, "perhaps you forgot parentheses?");
1592 }
1593 _ => {
1594 err.span_label(sum_span, "expected a path");
1595 },
1596 }
1597 err.emit();
1598 Ok(())
1599 }
1600
1601 fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
1602 let opt_lifetime = if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
1603 let mutbl = self.parse_mutability();
1604 let ty = self.parse_ty_no_plus()?;
1605 return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty: ty, mutbl: mutbl }));
1606 }
1607
1608 pub fn parse_ptr(&mut self) -> PResult<'a, MutTy> {
1609 let mutbl = if self.eat_keyword(keywords::Mut) {
1610 Mutability::Mutable
1611 } else if self.eat_keyword(keywords::Const) {
1612 Mutability::Immutable
1613 } else {
1614 let span = self.prev_span;
1615 self.span_err(span,
1616 "expected mut or const in raw pointer type (use \
1617 `*mut T` or `*const T` as appropriate)");
1618 Mutability::Immutable
1619 };
1620 let t = self.parse_ty_no_plus()?;
1621 Ok(MutTy { ty: t, mutbl: mutbl })
1622 }
1623
1624 fn is_named_argument(&mut self) -> bool {
1625 let offset = match self.token {
1626 token::Interpolated(ref nt) => match nt.0 {
1627 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
1628 _ => 0,
1629 }
1630 token::BinOp(token::And) | token::AndAnd => 1,
1631 _ if self.token.is_keyword(keywords::Mut) => 1,
1632 _ => 0,
1633 };
1634
1635 self.look_ahead(offset, |t| t.is_ident() || t == &token::Underscore) &&
1636 self.look_ahead(offset + 1, |t| t == &token::Colon)
1637 }
1638
1639 /// This version of parse arg doesn't necessarily require
1640 /// identifier names.
1641 pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
1642 maybe_whole!(self, NtArg, |x| x);
1643
1644 let pat = if require_name || self.is_named_argument() {
1645 debug!("parse_arg_general parse_pat (require_name:{})",
1646 require_name);
1647 let pat = self.parse_pat()?;
1648
1649 self.expect(&token::Colon)?;
1650 pat
1651 } else {
1652 debug!("parse_arg_general ident_to_pat");
1653 let sp = self.prev_span;
1654 let spanned = Spanned { span: sp, node: keywords::Invalid.ident() };
1655 P(Pat {
1656 id: ast::DUMMY_NODE_ID,
1657 node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable),
1658 spanned, None),
1659 span: sp
1660 })
1661 };
1662
1663 let t = self.parse_ty()?;
1664
1665 Ok(Arg {
1666 ty: t,
1667 pat,
1668 id: ast::DUMMY_NODE_ID,
1669 })
1670 }
1671
1672 /// Parse a single function argument
1673 pub fn parse_arg(&mut self) -> PResult<'a, Arg> {
1674 self.parse_arg_general(true)
1675 }
1676
1677 /// Parse an argument in a lambda header e.g. |arg, arg|
1678 pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
1679 let pat = self.parse_pat()?;
1680 let t = if self.eat(&token::Colon) {
1681 self.parse_ty()?
1682 } else {
1683 P(Ty {
1684 id: ast::DUMMY_NODE_ID,
1685 node: TyKind::Infer,
1686 span: self.span,
1687 })
1688 };
1689 Ok(Arg {
1690 ty: t,
1691 pat,
1692 id: ast::DUMMY_NODE_ID
1693 })
1694 }
1695
1696 pub fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option<P<ast::Expr>>> {
1697 if self.eat(&token::Semi) {
1698 Ok(Some(self.parse_expr()?))
1699 } else {
1700 Ok(None)
1701 }
1702 }
1703
1704 /// Matches token_lit = LIT_INTEGER | ...
1705 pub fn parse_lit_token(&mut self) -> PResult<'a, LitKind> {
1706 let out = match self.token {
1707 token::Interpolated(ref nt) => match nt.0 {
1708 token::NtExpr(ref v) => match v.node {
1709 ExprKind::Lit(ref lit) => { lit.node.clone() }
1710 _ => { return self.unexpected_last(&self.token); }
1711 },
1712 _ => { return self.unexpected_last(&self.token); }
1713 },
1714 token::Literal(lit, suf) => {
1715 let diag = Some((self.span, &self.sess.span_diagnostic));
1716 let (suffix_illegal, result) = parse::lit_token(lit, suf, diag);
1717
1718 if suffix_illegal {
1719 let sp = self.span;
1720 self.expect_no_suffix(sp, &format!("{} literal", lit.short_name()), suf)
1721 }
1722
1723 result.unwrap()
1724 }
1725 _ => { return self.unexpected_last(&self.token); }
1726 };
1727
1728 self.bump();
1729 Ok(out)
1730 }
1731
1732 /// Matches lit = true | false | token_lit
1733 pub fn parse_lit(&mut self) -> PResult<'a, Lit> {
1734 let lo = self.span;
1735 let lit = if self.eat_keyword(keywords::True) {
1736 LitKind::Bool(true)
1737 } else if self.eat_keyword(keywords::False) {
1738 LitKind::Bool(false)
1739 } else {
1740 let lit = self.parse_lit_token()?;
1741 lit
1742 };
1743 Ok(codemap::Spanned { node: lit, span: lo.to(self.prev_span) })
1744 }
1745
1746 /// matches '-' lit | lit (cf. ast_validation::AstValidator::check_expr_within_pat)
1747 pub fn parse_pat_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
1748 maybe_whole_expr!(self);
1749
1750 let minus_lo = self.span;
1751 let minus_present = self.eat(&token::BinOp(token::Minus));
1752 let lo = self.span;
1753 let literal = P(self.parse_lit()?);
1754 let hi = self.prev_span;
1755 let expr = self.mk_expr(lo.to(hi), ExprKind::Lit(literal), ThinVec::new());
1756
1757 if minus_present {
1758 let minus_hi = self.prev_span;
1759 let unary = self.mk_unary(UnOp::Neg, expr);
1760 Ok(self.mk_expr(minus_lo.to(minus_hi), unary, ThinVec::new()))
1761 } else {
1762 Ok(expr)
1763 }
1764 }
1765
1766 pub fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> {
1767 match self.token {
1768 token::Ident(sid) if self.token.is_path_segment_keyword() => {
1769 self.bump();
1770 Ok(sid)
1771 }
1772 _ => self.parse_ident(),
1773 }
1774 }
1775
1776 /// Parses qualified path.
1777 /// Assumes that the leading `<` has been parsed already.
1778 ///
1779 /// `qualified_path = <type [as trait_ref]>::path`
1780 ///
1781 /// # Examples
1782 /// `<T as U>::a`
1783 /// `<T as U>::F::a<S>` (without disambiguator)
1784 /// `<T as U>::F::a::<S>` (with disambiguator)
1785 fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, ast::Path)> {
1786 let lo = self.prev_span;
1787 let ty = self.parse_ty()?;
1788 let mut path = if self.eat_keyword(keywords::As) {
1789 self.parse_path(PathStyle::Type)?
1790 } else {
1791 ast::Path { segments: Vec::new(), span: syntax_pos::DUMMY_SP }
1792 };
1793 self.expect(&token::Gt)?;
1794 self.expect(&token::ModSep)?;
1795
1796 let qself = QSelf { ty, position: path.segments.len() };
1797 self.parse_path_segments(&mut path.segments, style, true)?;
1798
1799 Ok((qself, ast::Path { segments: path.segments, span: lo.to(self.prev_span) }))
1800 }
1801
1802 /// Parses simple paths.
1803 ///
1804 /// `path = [::] segment+`
1805 /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
1806 ///
1807 /// # Examples
1808 /// `a::b::C<D>` (without disambiguator)
1809 /// `a::b::C::<D>` (with disambiguator)
1810 /// `Fn(Args)` (without disambiguator)
1811 /// `Fn::(Args)` (with disambiguator)
1812 pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> {
1813 self.parse_path_common(style, true)
1814 }
1815
1816 pub fn parse_path_common(&mut self, style: PathStyle, enable_warning: bool)
1817 -> PResult<'a, ast::Path> {
1818 maybe_whole!(self, NtPath, |path| {
1819 if style == PathStyle::Mod &&
1820 path.segments.iter().any(|segment| segment.parameters.is_some()) {
1821 self.diagnostic().span_err(path.span, "unexpected generic arguments in path");
1822 }
1823 path
1824 });
1825
1826 let lo = self.meta_var_span.unwrap_or(self.span);
1827 let mut segments = Vec::new();
1828 if self.eat(&token::ModSep) {
1829 segments.push(PathSegment::crate_root(lo));
1830 }
1831 self.parse_path_segments(&mut segments, style, enable_warning)?;
1832
1833 Ok(ast::Path { segments, span: lo.to(self.prev_span) })
1834 }
1835
1836 /// Like `parse_path`, but also supports parsing `Word` meta items into paths for back-compat.
1837 /// This is used when parsing derive macro paths in `#[derive]` attributes.
1838 pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast::Path> {
1839 let meta_ident = match self.token {
1840 token::Interpolated(ref nt) => match nt.0 {
1841 token::NtMeta(ref meta) => match meta.node {
1842 ast::MetaItemKind::Word => Some(ast::Ident::with_empty_ctxt(meta.name)),
1843 _ => None,
1844 },
1845 _ => None,
1846 },
1847 _ => None,
1848 };
1849 if let Some(ident) = meta_ident {
1850 self.bump();
1851 return Ok(ast::Path::from_ident(self.prev_span, ident));
1852 }
1853 self.parse_path(style)
1854 }
1855
1856 fn parse_path_segments(&mut self,
1857 segments: &mut Vec<PathSegment>,
1858 style: PathStyle,
1859 enable_warning: bool)
1860 -> PResult<'a, ()> {
1861 loop {
1862 segments.push(self.parse_path_segment(style, enable_warning)?);
1863
1864 if self.is_import_coupler() || !self.eat(&token::ModSep) {
1865 return Ok(());
1866 }
1867 }
1868 }
1869
1870 fn parse_path_segment(&mut self, style: PathStyle, enable_warning: bool)
1871 -> PResult<'a, PathSegment> {
1872 let ident_span = self.span;
1873 let ident = self.parse_path_segment_ident()?;
1874
1875 let is_args_start = |token: &token::Token| match *token {
1876 token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren) => true,
1877 _ => false,
1878 };
1879 let check_args_start = |this: &mut Self| {
1880 this.expected_tokens.extend_from_slice(
1881 &[TokenType::Token(token::Lt), TokenType::Token(token::OpenDelim(token::Paren))]
1882 );
1883 is_args_start(&this.token)
1884 };
1885
1886 Ok(if style == PathStyle::Type && check_args_start(self) ||
1887 style != PathStyle::Mod && self.check(&token::ModSep)
1888 && self.look_ahead(1, |t| is_args_start(t)) {
1889 // Generic arguments are found - `<`, `(`, `::<` or `::(`.
1890 let lo = self.span;
1891 if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning {
1892 self.diagnostic().struct_span_warn(self.prev_span, "unnecessary path disambiguator")
1893 .span_label(self.prev_span, "try removing `::`").emit();
1894 }
1895
1896 let parameters = if self.eat_lt() {
1897 // `<'a, T, A = U>`
1898 let (lifetimes, types, bindings) = self.parse_generic_args()?;
1899 self.expect_gt()?;
1900 let span = lo.to(self.prev_span);
1901 AngleBracketedParameterData { lifetimes, types, bindings, span }.into()
1902 } else {
1903 // `(T, U) -> R`
1904 self.bump(); // `(`
1905 let inputs = self.parse_seq_to_before_tokens(
1906 &[&token::CloseDelim(token::Paren)],
1907 SeqSep::trailing_allowed(token::Comma),
1908 TokenExpectType::Expect,
1909 |p| p.parse_ty())?;
1910 self.bump(); // `)`
1911 let output = if self.eat(&token::RArrow) {
1912 Some(self.parse_ty_no_plus()?)
1913 } else {
1914 None
1915 };
1916 let span = lo.to(self.prev_span);
1917 ParenthesizedParameterData { inputs, output, span }.into()
1918 };
1919
1920 PathSegment { identifier: ident, span: ident_span, parameters }
1921 } else {
1922 // Generic arguments are not found.
1923 PathSegment::from_ident(ident, ident_span)
1924 })
1925 }
1926
1927 fn check_lifetime(&mut self) -> bool {
1928 self.expected_tokens.push(TokenType::Lifetime);
1929 self.token.is_lifetime()
1930 }
1931
1932 /// Parse single lifetime 'a or panic.
1933 fn expect_lifetime(&mut self) -> Lifetime {
1934 match self.token {
1935 token::Lifetime(ident) => {
1936 let ident_span = self.span;
1937 self.bump();
1938 Lifetime { ident: ident, span: ident_span, id: ast::DUMMY_NODE_ID }
1939 }
1940 _ => self.span_bug(self.span, "not a lifetime")
1941 }
1942 }
1943
1944 /// Parse mutability (`mut` or nothing).
1945 fn parse_mutability(&mut self) -> Mutability {
1946 if self.eat_keyword(keywords::Mut) {
1947 Mutability::Mutable
1948 } else {
1949 Mutability::Immutable
1950 }
1951 }
1952
1953 pub fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1954 if let token::Literal(token::Integer(name), None) = self.token {
1955 self.bump();
1956 Ok(Ident::with_empty_ctxt(name))
1957 } else {
1958 self.parse_ident()
1959 }
1960 }
1961
1962 /// Parse ident (COLON expr)?
1963 pub fn parse_field(&mut self) -> PResult<'a, Field> {
1964 let attrs = self.parse_outer_attributes()?;
1965 let lo = self.span;
1966 let hi;
1967
1968 // Check if a colon exists one ahead. This means we're parsing a fieldname.
1969 let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
1970 let fieldname = self.parse_field_name()?;
1971 self.bump();
1972 hi = self.prev_span;
1973 (fieldname, self.parse_expr()?, false)
1974 } else {
1975 let fieldname = self.parse_ident()?;
1976 hi = self.prev_span;
1977
1978 // Mimic `x: x` for the `x` field shorthand.
1979 let path = ast::Path::from_ident(lo.to(hi), fieldname);
1980 (fieldname, self.mk_expr(lo.to(hi), ExprKind::Path(None, path), ThinVec::new()), true)
1981 };
1982 Ok(ast::Field {
1983 ident: respan(lo.to(hi), fieldname),
1984 span: lo.to(expr.span),
1985 expr,
1986 is_shorthand,
1987 attrs: attrs.into(),
1988 })
1989 }
1990
1991 pub fn mk_expr(&mut self, span: Span, node: ExprKind, attrs: ThinVec<Attribute>) -> P<Expr> {
1992 P(Expr {
1993 id: ast::DUMMY_NODE_ID,
1994 node,
1995 span,
1996 attrs: attrs.into(),
1997 })
1998 }
1999
2000 pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P<Expr>) -> ast::ExprKind {
2001 ExprKind::Unary(unop, expr)
2002 }
2003
2004 pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2005 ExprKind::Binary(binop, lhs, rhs)
2006 }
2007
2008 pub fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> ast::ExprKind {
2009 ExprKind::Call(f, args)
2010 }
2011
2012 pub fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> ast::ExprKind {
2013 ExprKind::Index(expr, idx)
2014 }
2015
2016 pub fn mk_range(&mut self,
2017 start: Option<P<Expr>>,
2018 end: Option<P<Expr>>,
2019 limits: RangeLimits)
2020 -> PResult<'a, ast::ExprKind> {
2021 if end.is_none() && limits == RangeLimits::Closed {
2022 Err(self.span_fatal_err(self.span, Error::InclusiveRangeWithNoEnd))
2023 } else {
2024 Ok(ExprKind::Range(start, end, limits))
2025 }
2026 }
2027
2028 pub fn mk_tup_field(&mut self, expr: P<Expr>, idx: codemap::Spanned<usize>) -> ast::ExprKind {
2029 ExprKind::TupField(expr, idx)
2030 }
2031
2032 pub fn mk_assign_op(&mut self, binop: ast::BinOp,
2033 lhs: P<Expr>, rhs: P<Expr>) -> ast::ExprKind {
2034 ExprKind::AssignOp(binop, lhs, rhs)
2035 }
2036
2037 pub fn mk_mac_expr(&mut self, span: Span, m: Mac_, attrs: ThinVec<Attribute>) -> P<Expr> {
2038 P(Expr {
2039 id: ast::DUMMY_NODE_ID,
2040 node: ExprKind::Mac(codemap::Spanned {node: m, span: span}),
2041 span,
2042 attrs,
2043 })
2044 }
2045
2046 pub fn mk_lit_u32(&mut self, i: u32, attrs: ThinVec<Attribute>) -> P<Expr> {
2047 let span = &self.span;
2048 let lv_lit = P(codemap::Spanned {
2049 node: LitKind::Int(i as u128, ast::LitIntType::Unsigned(UintTy::U32)),
2050 span: *span
2051 });
2052
2053 P(Expr {
2054 id: ast::DUMMY_NODE_ID,
2055 node: ExprKind::Lit(lv_lit),
2056 span: *span,
2057 attrs,
2058 })
2059 }
2060
2061 fn expect_delimited_token_tree(&mut self) -> PResult<'a, (token::DelimToken, ThinTokenStream)> {
2062 match self.token {
2063 token::OpenDelim(delim) => match self.parse_token_tree() {
2064 TokenTree::Delimited(_, delimited) => Ok((delim, delimited.stream().into())),
2065 _ => unreachable!(),
2066 },
2067 _ => Err(self.fatal("expected open delimiter")),
2068 }
2069 }
2070
2071 /// At the bottom (top?) of the precedence hierarchy,
2072 /// parse things like parenthesized exprs,
2073 /// macros, return, etc.
2074 ///
2075 /// NB: This does not parse outer attributes,
2076 /// and is private because it only works
2077 /// correctly if called from parse_dot_or_call_expr().
2078 fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
2079 maybe_whole_expr!(self);
2080
2081 // Outer attributes are already parsed and will be
2082 // added to the return value after the fact.
2083 //
2084 // Therefore, prevent sub-parser from parsing
2085 // attributes by giving them a empty "already parsed" list.
2086 let mut attrs = ThinVec::new();
2087
2088 let lo = self.span;
2089 let mut hi = self.span;
2090
2091 let ex: ExprKind;
2092
2093 // Note: when adding new syntax here, don't forget to adjust Token::can_begin_expr().
2094 match self.token {
2095 token::OpenDelim(token::Paren) => {
2096 self.bump();
2097
2098 attrs.extend(self.parse_inner_attributes()?);
2099
2100 // (e) is parenthesized e
2101 // (e,) is a tuple with only one field, e
2102 let mut es = vec![];
2103 let mut trailing_comma = false;
2104 while self.token != token::CloseDelim(token::Paren) {
2105 es.push(self.parse_expr()?);
2106 self.expect_one_of(&[], &[token::Comma, token::CloseDelim(token::Paren)])?;
2107 if self.check(&token::Comma) {
2108 trailing_comma = true;
2109
2110 self.bump();
2111 } else {
2112 trailing_comma = false;
2113 break;
2114 }
2115 }
2116 self.bump();
2117
2118 hi = self.prev_span;
2119 let span = lo.to(hi);
2120 return if es.len() == 1 && !trailing_comma {
2121 Ok(self.mk_expr(span, ExprKind::Paren(es.into_iter().nth(0).unwrap()), attrs))
2122 } else {
2123 Ok(self.mk_expr(span, ExprKind::Tup(es), attrs))
2124 }
2125 }
2126 token::OpenDelim(token::Brace) => {
2127 return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
2128 }
2129 token::BinOp(token::Or) | token::OrOr => {
2130 let lo = self.span;
2131 return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
2132 }
2133 token::OpenDelim(token::Bracket) => {
2134 self.bump();
2135
2136 attrs.extend(self.parse_inner_attributes()?);
2137
2138 if self.check(&token::CloseDelim(token::Bracket)) {
2139 // Empty vector.
2140 self.bump();
2141 ex = ExprKind::Array(Vec::new());
2142 } else {
2143 // Nonempty vector.
2144 let first_expr = self.parse_expr()?;
2145 if self.check(&token::Semi) {
2146 // Repeating array syntax: [ 0; 512 ]
2147 self.bump();
2148 let count = self.parse_expr()?;
2149 self.expect(&token::CloseDelim(token::Bracket))?;
2150 ex = ExprKind::Repeat(first_expr, count);
2151 } else if self.check(&token::Comma) {
2152 // Vector with two or more elements.
2153 self.bump();
2154 let remaining_exprs = self.parse_seq_to_end(
2155 &token::CloseDelim(token::Bracket),
2156 SeqSep::trailing_allowed(token::Comma),
2157 |p| Ok(p.parse_expr()?)
2158 )?;
2159 let mut exprs = vec![first_expr];
2160 exprs.extend(remaining_exprs);
2161 ex = ExprKind::Array(exprs);
2162 } else {
2163 // Vector with one element.
2164 self.expect(&token::CloseDelim(token::Bracket))?;
2165 ex = ExprKind::Array(vec![first_expr]);
2166 }
2167 }
2168 hi = self.prev_span;
2169 }
2170 _ => {
2171 if self.eat_lt() {
2172 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
2173 hi = path.span;
2174 return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs));
2175 }
2176 if self.eat_keyword(keywords::Move) {
2177 let lo = self.prev_span;
2178 return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
2179 }
2180 if self.eat_keyword(keywords::If) {
2181 return self.parse_if_expr(attrs);
2182 }
2183 if self.eat_keyword(keywords::For) {
2184 let lo = self.prev_span;
2185 return self.parse_for_expr(None, lo, attrs);
2186 }
2187 if self.eat_keyword(keywords::While) {
2188 let lo = self.prev_span;
2189 return self.parse_while_expr(None, lo, attrs);
2190 }
2191 if self.token.is_lifetime() {
2192 let label = Spanned { node: self.get_label(),
2193 span: self.span };
2194 let lo = self.span;
2195 self.bump();
2196 self.expect(&token::Colon)?;
2197 if self.eat_keyword(keywords::While) {
2198 return self.parse_while_expr(Some(label), lo, attrs)
2199 }
2200 if self.eat_keyword(keywords::For) {
2201 return self.parse_for_expr(Some(label), lo, attrs)
2202 }
2203 if self.eat_keyword(keywords::Loop) {
2204 return self.parse_loop_expr(Some(label), lo, attrs)
2205 }
2206 return Err(self.fatal("expected `while`, `for`, or `loop` after a label"))
2207 }
2208 if self.eat_keyword(keywords::Loop) {
2209 let lo = self.prev_span;
2210 return self.parse_loop_expr(None, lo, attrs);
2211 }
2212 if self.eat_keyword(keywords::Continue) {
2213 let ex = if self.token.is_lifetime() {
2214 let ex = ExprKind::Continue(Some(Spanned{
2215 node: self.get_label(),
2216 span: self.span
2217 }));
2218 self.bump();
2219 ex
2220 } else {
2221 ExprKind::Continue(None)
2222 };
2223 let hi = self.prev_span;
2224 return Ok(self.mk_expr(lo.to(hi), ex, attrs));
2225 }
2226 if self.eat_keyword(keywords::Match) {
2227 return self.parse_match_expr(attrs);
2228 }
2229 if self.eat_keyword(keywords::Unsafe) {
2230 return self.parse_block_expr(
2231 lo,
2232 BlockCheckMode::Unsafe(ast::UserProvided),
2233 attrs);
2234 }
2235 if self.is_catch_expr() {
2236 let lo = self.span;
2237 assert!(self.eat_keyword(keywords::Do));
2238 assert!(self.eat_keyword(keywords::Catch));
2239 return self.parse_catch_expr(lo, attrs);
2240 }
2241 if self.eat_keyword(keywords::Return) {
2242 if self.token.can_begin_expr() {
2243 let e = self.parse_expr()?;
2244 hi = e.span;
2245 ex = ExprKind::Ret(Some(e));
2246 } else {
2247 ex = ExprKind::Ret(None);
2248 }
2249 } else if self.eat_keyword(keywords::Break) {
2250 let lt = if self.token.is_lifetime() {
2251 let spanned_lt = Spanned {
2252 node: self.get_label(),
2253 span: self.span
2254 };
2255 self.bump();
2256 Some(spanned_lt)
2257 } else {
2258 None
2259 };
2260 let e = if self.token.can_begin_expr()
2261 && !(self.token == token::OpenDelim(token::Brace)
2262 && self.restrictions.contains(
2263 Restrictions::NO_STRUCT_LITERAL)) {
2264 Some(self.parse_expr()?)
2265 } else {
2266 None
2267 };
2268 ex = ExprKind::Break(lt, e);
2269 hi = self.prev_span;
2270 } else if self.eat_keyword(keywords::Yield) {
2271 if self.token.can_begin_expr() {
2272 let e = self.parse_expr()?;
2273 hi = e.span;
2274 ex = ExprKind::Yield(Some(e));
2275 } else {
2276 ex = ExprKind::Yield(None);
2277 }
2278 } else if self.token.is_keyword(keywords::Let) {
2279 // Catch this syntax error here, instead of in `parse_ident`, so
2280 // that we can explicitly mention that let is not to be used as an expression
2281 let mut db = self.fatal("expected expression, found statement (`let`)");
2282 db.note("variable declaration using `let` is a statement");
2283 return Err(db);
2284 } else if self.token.is_path_start() {
2285 let pth = self.parse_path(PathStyle::Expr)?;
2286
2287 // `!`, as an operator, is prefix, so we know this isn't that
2288 if self.eat(&token::Not) {
2289 // MACRO INVOCATION expression
2290 let (_, tts) = self.expect_delimited_token_tree()?;
2291 let hi = self.prev_span;
2292 return Ok(self.mk_mac_expr(lo.to(hi), Mac_ { path: pth, tts: tts }, attrs));
2293 }
2294 if self.check(&token::OpenDelim(token::Brace)) {
2295 // This is a struct literal, unless we're prohibited
2296 // from parsing struct literals here.
2297 let prohibited = self.restrictions.contains(
2298 Restrictions::NO_STRUCT_LITERAL
2299 );
2300 if !prohibited {
2301 return self.parse_struct_expr(lo, pth, attrs);
2302 }
2303 }
2304
2305 hi = pth.span;
2306 ex = ExprKind::Path(None, pth);
2307 } else {
2308 match self.parse_lit() {
2309 Ok(lit) => {
2310 hi = lit.span;
2311 ex = ExprKind::Lit(P(lit));
2312 }
2313 Err(mut err) => {
2314 self.cancel(&mut err);
2315 let msg = format!("expected expression, found {}",
2316 self.this_token_descr());
2317 return Err(self.fatal(&msg));
2318 }
2319 }
2320 }
2321 }
2322 }
2323
2324 return Ok(self.mk_expr(lo.to(hi), ex, attrs));
2325 }
2326
2327 fn parse_struct_expr(&mut self, lo: Span, pth: ast::Path, mut attrs: ThinVec<Attribute>)
2328 -> PResult<'a, P<Expr>> {
2329 self.bump();
2330 let mut fields = Vec::new();
2331 let mut base = None;
2332
2333 attrs.extend(self.parse_inner_attributes()?);
2334
2335 while self.token != token::CloseDelim(token::Brace) {
2336 if self.eat(&token::DotDot) {
2337 let exp_span = self.prev_span;
2338 match self.parse_expr() {
2339 Ok(e) => {
2340 base = Some(e);
2341 }
2342 Err(mut e) => {
2343 e.emit();
2344 self.recover_stmt();
2345 }
2346 }
2347 if self.token == token::Comma {
2348 let mut err = self.sess.span_diagnostic.mut_span_err(
2349 exp_span.to(self.prev_span),
2350 "cannot use a comma after the base struct",
2351 );
2352 err.span_suggestion_short(self.span, "remove this comma", "".to_owned());
2353 err.note("the base struct must always be the last field");
2354 err.emit();
2355 self.recover_stmt();
2356 }
2357 break;
2358 }
2359
2360 match self.parse_field() {
2361 Ok(f) => fields.push(f),
2362 Err(mut e) => {
2363 e.emit();
2364 self.recover_stmt();
2365 break;
2366 }
2367 }
2368
2369 match self.expect_one_of(&[token::Comma],
2370 &[token::CloseDelim(token::Brace)]) {
2371 Ok(()) => {}
2372 Err(mut e) => {
2373 e.emit();
2374 self.recover_stmt();
2375 break;
2376 }
2377 }
2378 }
2379
2380 let span = lo.to(self.span);
2381 self.expect(&token::CloseDelim(token::Brace))?;
2382 return Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs));
2383 }
2384
2385 fn parse_or_use_outer_attributes(&mut self,
2386 already_parsed_attrs: Option<ThinVec<Attribute>>)
2387 -> PResult<'a, ThinVec<Attribute>> {
2388 if let Some(attrs) = already_parsed_attrs {
2389 Ok(attrs)
2390 } else {
2391 self.parse_outer_attributes().map(|a| a.into())
2392 }
2393 }
2394
2395 /// Parse a block or unsafe block
2396 pub fn parse_block_expr(&mut self, lo: Span, blk_mode: BlockCheckMode,
2397 outer_attrs: ThinVec<Attribute>)
2398 -> PResult<'a, P<Expr>> {
2399 self.expect(&token::OpenDelim(token::Brace))?;
2400
2401 let mut attrs = outer_attrs;
2402 attrs.extend(self.parse_inner_attributes()?);
2403
2404 let blk = self.parse_block_tail(lo, blk_mode)?;
2405 return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), attrs));
2406 }
2407
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)?;
2413
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, attrs)
2417 }
2418
2419 pub fn parse_dot_or_call_expr_with(&mut self,
2420 e0: P<Expr>,
2421 lo: Span,
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
2426 // structure
2427 self.parse_dot_or_call_expr_with_(e0, lo)
2428 .map(|expr|
2429 expr.map(|mut expr| {
2430 attrs.extend::<Vec<_>>(expr.attrs.into());
2431 expr.attrs = attrs;
2432 match expr.node {
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;
2437
2438 self.span_err(span,
2439 "attributes are not yet allowed on `if` \
2440 expressions");
2441 }
2442 }
2443 _ => {}
2444 }
2445 expr
2446 })
2447 )
2448 }
2449
2450 // Assuming we have just parsed `.`, continue parsing into an expression.
2451 fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
2452 let segment = self.parse_path_segment(PathStyle::Expr, true)?;
2453 Ok(match self.token {
2454 token::OpenDelim(token::Paren) => {
2455 // Method call `expr.f()`
2456 let mut args = self.parse_unspanned_seq(
2457 &token::OpenDelim(token::Paren),
2458 &token::CloseDelim(token::Paren),
2459 SeqSep::trailing_allowed(token::Comma),
2460 |p| Ok(p.parse_expr()?)
2461 )?;
2462 args.insert(0, self_arg);
2463
2464 let span = lo.to(self.prev_span);
2465 self.mk_expr(span, ExprKind::MethodCall(segment, args), ThinVec::new())
2466 }
2467 _ => {
2468 // Field access `expr.f`
2469 if let Some(parameters) = segment.parameters {
2470 self.span_err(parameters.span(),
2471 "field expressions may not have generic arguments");
2472 }
2473
2474 let span = lo.to(self.prev_span);
2475 let ident = respan(segment.span, segment.identifier);
2476 self.mk_expr(span, ExprKind::Field(self_arg, ident), ThinVec::new())
2477 }
2478 })
2479 }
2480
2481 fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
2482 let mut e = e0;
2483 let mut hi;
2484 loop {
2485 // expr?
2486 while self.eat(&token::Question) {
2487 let hi = self.prev_span;
2488 e = self.mk_expr(lo.to(hi), ExprKind::Try(e), ThinVec::new());
2489 }
2490
2491 // expr.f
2492 if self.eat(&token::Dot) {
2493 match self.token {
2494 token::Ident(..) => {
2495 e = self.parse_dot_suffix(e, lo)?;
2496 }
2497 token::Literal(token::Integer(n), suf) => {
2498 let sp = self.span;
2499
2500 // A tuple index may not have a suffix
2501 self.expect_no_suffix(sp, "tuple index", suf);
2502
2503 let dot_span = self.prev_span;
2504 hi = self.span;
2505 self.bump();
2506
2507 let index = n.as_str().parse::<usize>().ok();
2508 match index {
2509 Some(n) => {
2510 let id = respan(dot_span.to(hi), n);
2511 let field = self.mk_tup_field(e, id);
2512 e = self.mk_expr(lo.to(hi), field, ThinVec::new());
2513 }
2514 None => {
2515 let prev_span = self.prev_span;
2516 self.span_err(prev_span, "invalid tuple or tuple struct index");
2517 }
2518 }
2519 }
2520 token::Literal(token::Float(n), _suf) => {
2521 self.bump();
2522 let fstr = n.as_str();
2523 let mut err = self.diagnostic().struct_span_err(self.prev_span,
2524 &format!("unexpected token: `{}`", n));
2525 err.span_label(self.prev_span, "unexpected token");
2526 if fstr.chars().all(|x| "0123456789.".contains(x)) {
2527 let float = match fstr.parse::<f64>().ok() {
2528 Some(f) => f,
2529 None => continue,
2530 };
2531 let sugg = pprust::to_string(|s| {
2532 use print::pprust::PrintState;
2533 s.popen()?;
2534 s.print_expr(&e)?;
2535 s.s.word( ".")?;
2536 s.print_usize(float.trunc() as usize)?;
2537 s.pclose()?;
2538 s.s.word(".")?;
2539 s.s.word(fstr.splitn(2, ".").last().unwrap())
2540 });
2541 err.span_suggestion(
2542 lo.to(self.prev_span),
2543 "try parenthesizing the first index",
2544 sugg);
2545 }
2546 return Err(err);
2547
2548 }
2549 _ => {
2550 // FIXME Could factor this out into non_fatal_unexpected or something.
2551 let actual = self.this_token_to_string();
2552 self.span_err(self.span, &format!("unexpected token: `{}`", actual));
2553 }
2554 }
2555 continue;
2556 }
2557 if self.expr_is_complete(&e) { break; }
2558 match self.token {
2559 // expr(...)
2560 token::OpenDelim(token::Paren) => {
2561 let es = self.parse_unspanned_seq(
2562 &token::OpenDelim(token::Paren),
2563 &token::CloseDelim(token::Paren),
2564 SeqSep::trailing_allowed(token::Comma),
2565 |p| Ok(p.parse_expr()?)
2566 )?;
2567 hi = self.prev_span;
2568
2569 let nd = self.mk_call(e, es);
2570 e = self.mk_expr(lo.to(hi), nd, ThinVec::new());
2571 }
2572
2573 // expr[...]
2574 // Could be either an index expression or a slicing expression.
2575 token::OpenDelim(token::Bracket) => {
2576 self.bump();
2577 let ix = self.parse_expr()?;
2578 hi = self.span;
2579 self.expect(&token::CloseDelim(token::Bracket))?;
2580 let index = self.mk_index(e, ix);
2581 e = self.mk_expr(lo.to(hi), index, ThinVec::new())
2582 }
2583 _ => return Ok(e)
2584 }
2585 }
2586 return Ok(e);
2587 }
2588
2589 pub fn process_potential_macro_variable(&mut self) {
2590 let ident = match self.token {
2591 token::Dollar if self.span.ctxt() != syntax_pos::hygiene::SyntaxContext::empty() &&
2592 self.look_ahead(1, |t| t.is_ident()) => {
2593 self.bump();
2594 let name = match self.token { token::Ident(ident) => ident, _ => unreachable!() };
2595 self.fatal(&format!("unknown macro variable `{}`", name)).emit();
2596 return
2597 }
2598 token::Interpolated(ref nt) => {
2599 self.meta_var_span = Some(self.span);
2600 match nt.0 {
2601 token::NtIdent(ident) => ident,
2602 _ => return,
2603 }
2604 }
2605 _ => return,
2606 };
2607 self.token = token::Ident(ident.node);
2608 self.span = ident.span;
2609 }
2610
2611 /// parse a single token tree from the input.
2612 pub fn parse_token_tree(&mut self) -> TokenTree {
2613 match self.token {
2614 token::OpenDelim(..) => {
2615 let frame = mem::replace(&mut self.token_cursor.frame,
2616 self.token_cursor.stack.pop().unwrap());
2617 self.span = frame.span;
2618 self.bump();
2619 TokenTree::Delimited(frame.span, Delimited {
2620 delim: frame.delim,
2621 tts: frame.tree_cursor.original_stream().into(),
2622 })
2623 },
2624 token::CloseDelim(_) | token::Eof => unreachable!(),
2625 _ => {
2626 let (token, span) = (mem::replace(&mut self.token, token::Underscore), self.span);
2627 self.bump();
2628 TokenTree::Token(span, token)
2629 }
2630 }
2631 }
2632
2633 // parse a stream of tokens into a list of TokenTree's,
2634 // up to EOF.
2635 pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
2636 let mut tts = Vec::new();
2637 while self.token != token::Eof {
2638 tts.push(self.parse_token_tree());
2639 }
2640 Ok(tts)
2641 }
2642
2643 pub fn parse_tokens(&mut self) -> TokenStream {
2644 let mut result = Vec::new();
2645 loop {
2646 match self.token {
2647 token::Eof | token::CloseDelim(..) => break,
2648 _ => result.push(self.parse_token_tree().into()),
2649 }
2650 }
2651 TokenStream::concat(result)
2652 }
2653
2654 /// Parse a prefix-unary-operator expr
2655 pub fn parse_prefix_expr(&mut self,
2656 already_parsed_attrs: Option<ThinVec<Attribute>>)
2657 -> PResult<'a, P<Expr>> {
2658 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
2659 let lo = self.span;
2660 // Note: when adding new unary operators, don't forget to adjust Token::can_begin_expr()
2661 let (hi, ex) = match self.token {
2662 token::Not => {
2663 self.bump();
2664 let e = self.parse_prefix_expr(None);
2665 let (span, e) = self.interpolated_or_expr_span(e)?;
2666 (lo.to(span), self.mk_unary(UnOp::Not, e))
2667 }
2668 // Suggest `!` for bitwise negation when encountering a `~`
2669 token::Tilde => {
2670 self.bump();
2671 let e = self.parse_prefix_expr(None);
2672 let (span, e) = self.interpolated_or_expr_span(e)?;
2673 let span_of_tilde = lo;
2674 let mut err = self.diagnostic().struct_span_err(span_of_tilde,
2675 "`~` can not be used as a unary operator");
2676 err.span_label(span_of_tilde, "did you mean `!`?");
2677 err.help("use `!` instead of `~` if you meant to perform bitwise negation");
2678 err.emit();
2679 (lo.to(span), self.mk_unary(UnOp::Not, e))
2680 }
2681 token::BinOp(token::Minus) => {
2682 self.bump();
2683 let e = self.parse_prefix_expr(None);
2684 let (span, e) = self.interpolated_or_expr_span(e)?;
2685 (lo.to(span), self.mk_unary(UnOp::Neg, e))
2686 }
2687 token::BinOp(token::Star) => {
2688 self.bump();
2689 let e = self.parse_prefix_expr(None);
2690 let (span, e) = self.interpolated_or_expr_span(e)?;
2691 (lo.to(span), self.mk_unary(UnOp::Deref, e))
2692 }
2693 token::BinOp(token::And) | token::AndAnd => {
2694 self.expect_and()?;
2695 let m = self.parse_mutability();
2696 let e = self.parse_prefix_expr(None);
2697 let (span, e) = self.interpolated_or_expr_span(e)?;
2698 (lo.to(span), ExprKind::AddrOf(m, e))
2699 }
2700 token::Ident(..) if self.token.is_keyword(keywords::In) => {
2701 self.bump();
2702 let place = self.parse_expr_res(
2703 Restrictions::NO_STRUCT_LITERAL,
2704 None,
2705 )?;
2706 let blk = self.parse_block()?;
2707 let span = blk.span;
2708 let blk_expr = self.mk_expr(span, ExprKind::Block(blk), ThinVec::new());
2709 (lo.to(span), ExprKind::InPlace(place, blk_expr))
2710 }
2711 token::Ident(..) if self.token.is_keyword(keywords::Box) => {
2712 self.bump();
2713 let e = self.parse_prefix_expr(None);
2714 let (span, e) = self.interpolated_or_expr_span(e)?;
2715 (lo.to(span), ExprKind::Box(e))
2716 }
2717 _ => return self.parse_dot_or_call_expr(Some(attrs))
2718 };
2719 return Ok(self.mk_expr(lo.to(hi), ex, attrs));
2720 }
2721
2722 /// Parse an associative expression
2723 ///
2724 /// This parses an expression accounting for associativity and precedence of the operators in
2725 /// the expression.
2726 pub fn parse_assoc_expr(&mut self,
2727 already_parsed_attrs: Option<ThinVec<Attribute>>)
2728 -> PResult<'a, P<Expr>> {
2729 self.parse_assoc_expr_with(0, already_parsed_attrs.into())
2730 }
2731
2732 /// Parse an associative expression with operators of at least `min_prec` precedence
2733 pub fn parse_assoc_expr_with(&mut self,
2734 min_prec: usize,
2735 lhs: LhsExpr)
2736 -> PResult<'a, P<Expr>> {
2737 let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
2738 expr
2739 } else {
2740 let attrs = match lhs {
2741 LhsExpr::AttributesParsed(attrs) => Some(attrs),
2742 _ => None,
2743 };
2744 if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token) {
2745 return self.parse_prefix_range_expr(attrs);
2746 } else {
2747 self.parse_prefix_expr(attrs)?
2748 }
2749 };
2750
2751 if self.expr_is_complete(&lhs) {
2752 // Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
2753 return Ok(lhs);
2754 }
2755 self.expected_tokens.push(TokenType::Operator);
2756 while let Some(op) = AssocOp::from_token(&self.token) {
2757
2758 // Adjust the span for interpolated LHS to point to the `$lhs` token and not to what
2759 // it refers to. Interpolated identifiers are unwrapped early and never show up here
2760 // as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process
2761 // it as "interpolated", it doesn't change the answer for non-interpolated idents.
2762 let lhs_span = match (self.prev_token_kind, &lhs.node) {
2763 (PrevTokenKind::Interpolated, _) => self.prev_span,
2764 (PrevTokenKind::Ident, &ExprKind::Path(None, ref path))
2765 if path.segments.len() == 1 => self.prev_span,
2766 _ => lhs.span,
2767 };
2768
2769 let cur_op_span = self.span;
2770 let restrictions = if op.is_assign_like() {
2771 self.restrictions & Restrictions::NO_STRUCT_LITERAL
2772 } else {
2773 self.restrictions
2774 };
2775 if op.precedence() < min_prec {
2776 break;
2777 }
2778 // Check for deprecated `...` syntax
2779 if self.token == token::DotDotDot && op == AssocOp::DotDotEq {
2780 self.err_dotdotdot_syntax(self.span);
2781 }
2782
2783 self.bump();
2784 if op.is_comparison() {
2785 self.check_no_chained_comparison(&lhs, &op);
2786 }
2787 // Special cases:
2788 if op == AssocOp::As {
2789 lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
2790 continue
2791 } else if op == AssocOp::Colon {
2792 lhs = match self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type) {
2793 Ok(lhs) => lhs,
2794 Err(mut err) => {
2795 err.span_label(self.span,
2796 "expecting a type here because of type ascription");
2797 let cm = self.sess.codemap();
2798 let cur_pos = cm.lookup_char_pos(self.span.lo());
2799 let op_pos = cm.lookup_char_pos(cur_op_span.hi());
2800 if cur_pos.line != op_pos.line {
2801 err.span_suggestion_short(cur_op_span,
2802 "did you mean to use `;` here?",
2803 ";".to_string());
2804 }
2805 return Err(err);
2806 }
2807 };
2808 continue
2809 } else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
2810 // If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to
2811 // generalise it to the Fixity::None code.
2812 //
2813 // We have 2 alternatives here: `x..y`/`x..=y` and `x..`/`x..=` The other
2814 // two variants are handled with `parse_prefix_range_expr` call above.
2815 let rhs = if self.is_at_start_of_range_notation_rhs() {
2816 Some(self.parse_assoc_expr_with(op.precedence() + 1,
2817 LhsExpr::NotYetParsed)?)
2818 } else {
2819 None
2820 };
2821 let (lhs_span, rhs_span) = (lhs.span, if let Some(ref x) = rhs {
2822 x.span
2823 } else {
2824 cur_op_span
2825 });
2826 let limits = if op == AssocOp::DotDot {
2827 RangeLimits::HalfOpen
2828 } else {
2829 RangeLimits::Closed
2830 };
2831
2832 let r = try!(self.mk_range(Some(lhs), rhs, limits));
2833 lhs = self.mk_expr(lhs_span.to(rhs_span), r, ThinVec::new());
2834 break
2835 }
2836
2837 let rhs = match op.fixity() {
2838 Fixity::Right => self.with_res(
2839 restrictions - Restrictions::STMT_EXPR,
2840 |this| {
2841 this.parse_assoc_expr_with(op.precedence(),
2842 LhsExpr::NotYetParsed)
2843 }),
2844 Fixity::Left => self.with_res(
2845 restrictions - Restrictions::STMT_EXPR,
2846 |this| {
2847 this.parse_assoc_expr_with(op.precedence() + 1,
2848 LhsExpr::NotYetParsed)
2849 }),
2850 // We currently have no non-associative operators that are not handled above by
2851 // the special cases. The code is here only for future convenience.
2852 Fixity::None => self.with_res(
2853 restrictions - Restrictions::STMT_EXPR,
2854 |this| {
2855 this.parse_assoc_expr_with(op.precedence() + 1,
2856 LhsExpr::NotYetParsed)
2857 }),
2858 }?;
2859
2860 let span = lhs_span.to(rhs.span);
2861 lhs = match op {
2862 AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide |
2863 AssocOp::Modulus | AssocOp::LAnd | AssocOp::LOr | AssocOp::BitXor |
2864 AssocOp::BitAnd | AssocOp::BitOr | AssocOp::ShiftLeft | AssocOp::ShiftRight |
2865 AssocOp::Equal | AssocOp::Less | AssocOp::LessEqual | AssocOp::NotEqual |
2866 AssocOp::Greater | AssocOp::GreaterEqual => {
2867 let ast_op = op.to_ast_binop().unwrap();
2868 let binary = self.mk_binary(codemap::respan(cur_op_span, ast_op), lhs, rhs);
2869 self.mk_expr(span, binary, ThinVec::new())
2870 }
2871 AssocOp::Assign =>
2872 self.mk_expr(span, ExprKind::Assign(lhs, rhs), ThinVec::new()),
2873 AssocOp::Inplace =>
2874 self.mk_expr(span, ExprKind::InPlace(lhs, rhs), ThinVec::new()),
2875 AssocOp::AssignOp(k) => {
2876 let aop = match k {
2877 token::Plus => BinOpKind::Add,
2878 token::Minus => BinOpKind::Sub,
2879 token::Star => BinOpKind::Mul,
2880 token::Slash => BinOpKind::Div,
2881 token::Percent => BinOpKind::Rem,
2882 token::Caret => BinOpKind::BitXor,
2883 token::And => BinOpKind::BitAnd,
2884 token::Or => BinOpKind::BitOr,
2885 token::Shl => BinOpKind::Shl,
2886 token::Shr => BinOpKind::Shr,
2887 };
2888 let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
2889 self.mk_expr(span, aopexpr, ThinVec::new())
2890 }
2891 AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotEq => {
2892 self.bug("AssocOp should have been handled by special case")
2893 }
2894 };
2895
2896 if op.fixity() == Fixity::None { break }
2897 }
2898 Ok(lhs)
2899 }
2900
2901 fn parse_assoc_op_cast(&mut self, lhs: P<Expr>, lhs_span: Span,
2902 expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind)
2903 -> PResult<'a, P<Expr>> {
2904 let mk_expr = |this: &mut Self, rhs: P<Ty>| {
2905 this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs), ThinVec::new())
2906 };
2907
2908 // Save the state of the parser before parsing type normally, in case there is a
2909 // LessThan comparison after this cast.
2910 let parser_snapshot_before_type = self.clone();
2911 match self.parse_ty_no_plus() {
2912 Ok(rhs) => {
2913 Ok(mk_expr(self, rhs))
2914 }
2915 Err(mut type_err) => {
2916 // Rewind to before attempting to parse the type with generics, to recover
2917 // from situations like `x as usize < y` in which we first tried to parse
2918 // `usize < y` as a type with generic arguments.
2919 let parser_snapshot_after_type = self.clone();
2920 mem::replace(self, parser_snapshot_before_type);
2921
2922 match self.parse_path(PathStyle::Expr) {
2923 Ok(path) => {
2924 let (op_noun, op_verb) = match self.token {
2925 token::Lt => ("comparison", "comparing"),
2926 token::BinOp(token::Shl) => ("shift", "shifting"),
2927 _ => {
2928 // We can end up here even without `<` being the next token, for
2929 // example because `parse_ty_no_plus` returns `Err` on keywords,
2930 // but `parse_path` returns `Ok` on them due to error recovery.
2931 // Return original error and parser state.
2932 mem::replace(self, parser_snapshot_after_type);
2933 return Err(type_err);
2934 }
2935 };
2936
2937 // Successfully parsed the type path leaving a `<` yet to parse.
2938 type_err.cancel();
2939
2940 // Report non-fatal diagnostics, keep `x as usize` as an expression
2941 // in AST and continue parsing.
2942 let msg = format!("`<` is interpreted as a start of generic \
2943 arguments for `{}`, not a {}", path, op_noun);
2944 let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &msg);
2945 err.span_label(self.look_ahead_span(1).to(parser_snapshot_after_type.span),
2946 "interpreted as generic arguments");
2947 err.span_label(self.span, format!("not interpreted as {}", op_noun));
2948
2949 let expr = mk_expr(self, P(Ty {
2950 span: path.span,
2951 node: TyKind::Path(None, path),
2952 id: ast::DUMMY_NODE_ID
2953 }));
2954
2955 let expr_str = self.sess.codemap().span_to_snippet(expr.span)
2956 .unwrap_or(pprust::expr_to_string(&expr));
2957 err.span_suggestion(expr.span,
2958 &format!("try {} the casted value", op_verb),
2959 format!("({})", expr_str));
2960 err.emit();
2961
2962 Ok(expr)
2963 }
2964 Err(mut path_err) => {
2965 // Couldn't parse as a path, return original error and parser state.
2966 path_err.cancel();
2967 mem::replace(self, parser_snapshot_after_type);
2968 Err(type_err)
2969 }
2970 }
2971 }
2972 }
2973 }
2974
2975 /// Produce an error if comparison operators are chained (RFC #558).
2976 /// We only need to check lhs, not rhs, because all comparison ops
2977 /// have same precedence and are left-associative
2978 fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: &AssocOp) {
2979 debug_assert!(outer_op.is_comparison(),
2980 "check_no_chained_comparison: {:?} is not comparison",
2981 outer_op);
2982 match lhs.node {
2983 ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
2984 // respan to include both operators
2985 let op_span = op.span.to(self.span);
2986 let mut err = self.diagnostic().struct_span_err(op_span,
2987 "chained comparison operators require parentheses");
2988 if op.node == BinOpKind::Lt &&
2989 *outer_op == AssocOp::Less || // Include `<` to provide this recommendation
2990 *outer_op == AssocOp::Greater // even in a case like the following:
2991 { // Foo<Bar<Baz<Qux, ()>>>
2992 err.help(
2993 "use `::<...>` instead of `<...>` if you meant to specify type arguments");
2994 err.help("or use `(...)` if you meant to specify fn arguments");
2995 }
2996 err.emit();
2997 }
2998 _ => {}
2999 }
3000 }
3001
3002 /// Parse prefix-forms of range notation: `..expr`, `..`, `..=expr`
3003 fn parse_prefix_range_expr(&mut self,
3004 already_parsed_attrs: Option<ThinVec<Attribute>>)
3005 -> PResult<'a, P<Expr>> {
3006 // Check for deprecated `...` syntax
3007 if self.token == token::DotDotDot {
3008 self.err_dotdotdot_syntax(self.span);
3009 }
3010
3011 debug_assert!([token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token),
3012 "parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq",
3013 self.token);
3014 let tok = self.token.clone();
3015 let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
3016 let lo = self.span;
3017 let mut hi = self.span;
3018 self.bump();
3019 let opt_end = if self.is_at_start_of_range_notation_rhs() {
3020 // RHS must be parsed with more associativity than the dots.
3021 let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
3022 Some(self.parse_assoc_expr_with(next_prec,
3023 LhsExpr::NotYetParsed)
3024 .map(|x|{
3025 hi = x.span;
3026 x
3027 })?)
3028 } else {
3029 None
3030 };
3031 let limits = if tok == token::DotDot {
3032 RangeLimits::HalfOpen
3033 } else {
3034 RangeLimits::Closed
3035 };
3036
3037 let r = try!(self.mk_range(None,
3038 opt_end,
3039 limits));
3040 Ok(self.mk_expr(lo.to(hi), r, attrs))
3041 }
3042
3043 fn is_at_start_of_range_notation_rhs(&self) -> bool {
3044 if self.token.can_begin_expr() {
3045 // parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
3046 if self.token == token::OpenDelim(token::Brace) {
3047 return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
3048 }
3049 true
3050 } else {
3051 false
3052 }
3053 }
3054
3055 /// Parse an 'if' or 'if let' expression ('if' token already eaten)
3056 pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3057 if self.check_keyword(keywords::Let) {
3058 return self.parse_if_let_expr(attrs);
3059 }
3060 let lo = self.prev_span;
3061 let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3062
3063 // Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
3064 // verify that the last statement is either an implicit return (no `;`) or an explicit
3065 // return. This won't catch blocks with an explicit `return`, but that would be caught by
3066 // the dead code lint.
3067 if self.eat_keyword(keywords::Else) || !cond.returns() {
3068 let sp = lo.next_point();
3069 let mut err = self.diagnostic()
3070 .struct_span_err(sp, "missing condition for `if` statemement");
3071 err.span_label(sp, "expected if condition here");
3072 return Err(err)
3073 }
3074 let thn = self.parse_block()?;
3075 let mut els: Option<P<Expr>> = None;
3076 let mut hi = thn.span;
3077 if self.eat_keyword(keywords::Else) {
3078 let elexpr = self.parse_else_expr()?;
3079 hi = elexpr.span;
3080 els = Some(elexpr);
3081 }
3082 Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs))
3083 }
3084
3085 /// Parse an 'if let' expression ('if' token already eaten)
3086 pub fn parse_if_let_expr(&mut self, attrs: ThinVec<Attribute>)
3087 -> PResult<'a, P<Expr>> {
3088 let lo = self.prev_span;
3089 self.expect_keyword(keywords::Let)?;
3090 let pat = self.parse_pat()?;
3091 self.expect(&token::Eq)?;
3092 let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3093 let thn = self.parse_block()?;
3094 let (hi, els) = if self.eat_keyword(keywords::Else) {
3095 let expr = self.parse_else_expr()?;
3096 (expr.span, Some(expr))
3097 } else {
3098 (thn.span, None)
3099 };
3100 Ok(self.mk_expr(lo.to(hi), ExprKind::IfLet(pat, expr, thn, els), attrs))
3101 }
3102
3103 // `move |args| expr`
3104 pub fn parse_lambda_expr(&mut self,
3105 lo: Span,
3106 capture_clause: CaptureBy,
3107 attrs: ThinVec<Attribute>)
3108 -> PResult<'a, P<Expr>>
3109 {
3110 let decl = self.parse_fn_block_decl()?;
3111 let decl_hi = self.prev_span;
3112 let body = match decl.output {
3113 FunctionRetTy::Default(_) => {
3114 let restrictions = self.restrictions - Restrictions::STMT_EXPR;
3115 self.parse_expr_res(restrictions, None)?
3116 },
3117 _ => {
3118 // If an explicit return type is given, require a
3119 // block to appear (RFC 968).
3120 let body_lo = self.span;
3121 self.parse_block_expr(body_lo, BlockCheckMode::Default, ThinVec::new())?
3122 }
3123 };
3124
3125 Ok(self.mk_expr(
3126 lo.to(body.span),
3127 ExprKind::Closure(capture_clause, decl, body, lo.to(decl_hi)),
3128 attrs))
3129 }
3130
3131 // `else` token already eaten
3132 pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
3133 if self.eat_keyword(keywords::If) {
3134 return self.parse_if_expr(ThinVec::new());
3135 } else {
3136 let blk = self.parse_block()?;
3137 return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), ThinVec::new()));
3138 }
3139 }
3140
3141 /// Parse a 'for' .. 'in' expression ('for' token already eaten)
3142 pub fn parse_for_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3143 span_lo: Span,
3144 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3145 // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
3146
3147 let pat = self.parse_pat()?;
3148 if !self.eat_keyword(keywords::In) {
3149 let in_span = self.prev_span.between(self.span);
3150 let mut err = self.sess.span_diagnostic
3151 .struct_span_err(in_span, "missing `in` in `for` loop");
3152 err.span_suggestion_short(in_span, "try adding `in` here", " in ".into());
3153 err.emit();
3154 }
3155 let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3156 let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
3157 attrs.extend(iattrs);
3158
3159 let hi = self.prev_span;
3160 Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_ident), attrs))
3161 }
3162
3163 /// Parse a 'while' or 'while let' expression ('while' token already eaten)
3164 pub fn parse_while_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3165 span_lo: Span,
3166 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3167 if self.token.is_keyword(keywords::Let) {
3168 return self.parse_while_let_expr(opt_ident, span_lo, attrs);
3169 }
3170 let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3171 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3172 attrs.extend(iattrs);
3173 let span = span_lo.to(body.span);
3174 return Ok(self.mk_expr(span, ExprKind::While(cond, body, opt_ident), attrs));
3175 }
3176
3177 /// Parse a 'while let' expression ('while' token already eaten)
3178 pub fn parse_while_let_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3179 span_lo: Span,
3180 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3181 self.expect_keyword(keywords::Let)?;
3182 let pat = self.parse_pat()?;
3183 self.expect(&token::Eq)?;
3184 let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3185 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3186 attrs.extend(iattrs);
3187 let span = span_lo.to(body.span);
3188 return Ok(self.mk_expr(span, ExprKind::WhileLet(pat, expr, body, opt_ident), attrs));
3189 }
3190
3191 // parse `loop {...}`, `loop` token already eaten
3192 pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
3193 span_lo: Span,
3194 mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3195 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3196 attrs.extend(iattrs);
3197 let span = span_lo.to(body.span);
3198 Ok(self.mk_expr(span, ExprKind::Loop(body, opt_ident), attrs))
3199 }
3200
3201 /// Parse a `do catch {...}` expression (`do catch` token already eaten)
3202 pub fn parse_catch_expr(&mut self, span_lo: Span, mut attrs: ThinVec<Attribute>)
3203 -> PResult<'a, P<Expr>>
3204 {
3205 let (iattrs, body) = self.parse_inner_attrs_and_block()?;
3206 attrs.extend(iattrs);
3207 Ok(self.mk_expr(span_lo.to(body.span), ExprKind::Catch(body), attrs))
3208 }
3209
3210 // `match` token already eaten
3211 fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3212 let match_span = self.prev_span;
3213 let lo = self.prev_span;
3214 let discriminant = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL,
3215 None)?;
3216 if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) {
3217 if self.token == token::Token::Semi {
3218 e.span_note(match_span, "did you mean to remove this `match` keyword?");
3219 }
3220 return Err(e)
3221 }
3222 attrs.extend(self.parse_inner_attributes()?);
3223
3224 let mut arms: Vec<Arm> = Vec::new();
3225 while self.token != token::CloseDelim(token::Brace) {
3226 match self.parse_arm() {
3227 Ok(arm) => arms.push(arm),
3228 Err(mut e) => {
3229 // Recover by skipping to the end of the block.
3230 e.emit();
3231 self.recover_stmt();
3232 let span = lo.to(self.span);
3233 if self.token == token::CloseDelim(token::Brace) {
3234 self.bump();
3235 }
3236 return Ok(self.mk_expr(span, ExprKind::Match(discriminant, arms), attrs));
3237 }
3238 }
3239 }
3240 let hi = self.span;
3241 self.bump();
3242 return Ok(self.mk_expr(lo.to(hi), ExprKind::Match(discriminant, arms), attrs));
3243 }
3244
3245 pub fn parse_arm(&mut self) -> PResult<'a, Arm> {
3246 maybe_whole!(self, NtArm, |x| x);
3247
3248 let attrs = self.parse_outer_attributes()?;
3249 // Allow a '|' before the pats (RFC 1925)
3250 let beginning_vert = if self.eat(&token::BinOp(token::Or)) {
3251 Some(self.prev_span)
3252 } else {
3253 None
3254 };
3255 let pats = self.parse_pats()?;
3256 let guard = if self.eat_keyword(keywords::If) {
3257 Some(self.parse_expr()?)
3258 } else {
3259 None
3260 };
3261 self.expect(&token::FatArrow)?;
3262 let expr = self.parse_expr_res(Restrictions::STMT_EXPR, None)?;
3263
3264 let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
3265 && self.token != token::CloseDelim(token::Brace);
3266
3267 if require_comma {
3268 self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])?;
3269 } else {
3270 self.eat(&token::Comma);
3271 }
3272
3273 Ok(ast::Arm {
3274 attrs,
3275 pats,
3276 guard,
3277 body: expr,
3278 beginning_vert,
3279 })
3280 }
3281
3282 /// Parse an expression
3283 pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
3284 self.parse_expr_res(Restrictions::empty(), None)
3285 }
3286
3287 /// Evaluate the closure with restrictions in place.
3288 ///
3289 /// After the closure is evaluated, restrictions are reset.
3290 pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
3291 where F: FnOnce(&mut Self) -> T
3292 {
3293 let old = self.restrictions;
3294 self.restrictions = r;
3295 let r = f(self);
3296 self.restrictions = old;
3297 return r;
3298
3299 }
3300
3301 /// Parse an expression, subject to the given restrictions
3302 pub fn parse_expr_res(&mut self, r: Restrictions,
3303 already_parsed_attrs: Option<ThinVec<Attribute>>)
3304 -> PResult<'a, P<Expr>> {
3305 self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
3306 }
3307
3308 /// Parse the RHS of a local variable declaration (e.g. '= 14;')
3309 fn parse_initializer(&mut self, skip_eq: bool) -> PResult<'a, Option<P<Expr>>> {
3310 if self.check(&token::Eq) {
3311 self.bump();
3312 Ok(Some(self.parse_expr()?))
3313 } else if skip_eq {
3314 Ok(Some(self.parse_expr()?))
3315 } else {
3316 Ok(None)
3317 }
3318 }
3319
3320 /// Parse patterns, separated by '|' s
3321 fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
3322 let mut pats = Vec::new();
3323 loop {
3324 pats.push(self.parse_pat()?);
3325 if self.check(&token::BinOp(token::Or)) { self.bump();}
3326 else { return Ok(pats); }
3327 };
3328 }
3329
3330 fn parse_pat_tuple_elements(&mut self, unary_needs_comma: bool)
3331 -> PResult<'a, (Vec<P<Pat>>, Option<usize>)> {
3332 let mut fields = vec![];
3333 let mut ddpos = None;
3334
3335 while !self.check(&token::CloseDelim(token::Paren)) {
3336 if ddpos.is_none() && self.eat(&token::DotDot) {
3337 ddpos = Some(fields.len());
3338 if self.eat(&token::Comma) {
3339 // `..` needs to be followed by `)` or `, pat`, `..,)` is disallowed.
3340 fields.push(self.parse_pat()?);
3341 }
3342 } else if ddpos.is_some() && self.eat(&token::DotDot) {
3343 // Emit a friendly error, ignore `..` and continue parsing
3344 self.span_err(self.prev_span, "`..` can only be used once per \
3345 tuple or tuple struct pattern");
3346 } else {
3347 fields.push(self.parse_pat()?);
3348 }
3349
3350 if !self.check(&token::CloseDelim(token::Paren)) ||
3351 (unary_needs_comma && fields.len() == 1 && ddpos.is_none()) {
3352 self.expect(&token::Comma)?;
3353 }
3354 }
3355
3356 Ok((fields, ddpos))
3357 }
3358
3359 fn parse_pat_vec_elements(
3360 &mut self,
3361 ) -> PResult<'a, (Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>)> {
3362 let mut before = Vec::new();
3363 let mut slice = None;
3364 let mut after = Vec::new();
3365 let mut first = true;
3366 let mut before_slice = true;
3367
3368 while self.token != token::CloseDelim(token::Bracket) {
3369 if first {
3370 first = false;
3371 } else {
3372 self.expect(&token::Comma)?;
3373
3374 if self.token == token::CloseDelim(token::Bracket)
3375 && (before_slice || !after.is_empty()) {
3376 break
3377 }
3378 }
3379
3380 if before_slice {
3381 if self.eat(&token::DotDot) {
3382
3383 if self.check(&token::Comma) ||
3384 self.check(&token::CloseDelim(token::Bracket)) {
3385 slice = Some(P(ast::Pat {
3386 id: ast::DUMMY_NODE_ID,
3387 node: PatKind::Wild,
3388 span: self.span,
3389 }));
3390 before_slice = false;
3391 }
3392 continue
3393 }
3394 }
3395
3396 let subpat = self.parse_pat()?;
3397 if before_slice && self.eat(&token::DotDot) {
3398 slice = Some(subpat);
3399 before_slice = false;
3400 } else if before_slice {
3401 before.push(subpat);
3402 } else {
3403 after.push(subpat);
3404 }
3405 }
3406
3407 Ok((before, slice, after))
3408 }
3409
3410 /// Parse the fields of a struct-like pattern
3411 fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<codemap::Spanned<ast::FieldPat>>, bool)> {
3412 let mut fields = Vec::new();
3413 let mut etc = false;
3414 let mut first = true;
3415 while self.token != token::CloseDelim(token::Brace) {
3416 if first {
3417 first = false;
3418 } else {
3419 self.expect(&token::Comma)?;
3420 // accept trailing commas
3421 if self.check(&token::CloseDelim(token::Brace)) { break }
3422 }
3423
3424 let attrs = self.parse_outer_attributes()?;
3425 let lo = self.span;
3426 let hi;
3427
3428 if self.check(&token::DotDot) {
3429 self.bump();
3430 if self.token != token::CloseDelim(token::Brace) {
3431 let token_str = self.this_token_to_string();
3432 return Err(self.fatal(&format!("expected `{}`, found `{}`", "}",
3433 token_str)))
3434 }
3435 etc = true;
3436 break;
3437 }
3438
3439 // Check if a colon exists one ahead. This means we're parsing a fieldname.
3440 let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
3441 // Parsing a pattern of the form "fieldname: pat"
3442 let fieldname = self.parse_field_name()?;
3443 self.bump();
3444 let pat = self.parse_pat()?;
3445 hi = pat.span;
3446 (pat, fieldname, false)
3447 } else {
3448 // Parsing a pattern of the form "(box) (ref) (mut) fieldname"
3449 let is_box = self.eat_keyword(keywords::Box);
3450 let boxed_span = self.span;
3451 let is_ref = self.eat_keyword(keywords::Ref);
3452 let is_mut = self.eat_keyword(keywords::Mut);
3453 let fieldname = self.parse_ident()?;
3454 hi = self.prev_span;
3455
3456 let bind_type = match (is_ref, is_mut) {
3457 (true, true) => BindingMode::ByRef(Mutability::Mutable),
3458 (true, false) => BindingMode::ByRef(Mutability::Immutable),
3459 (false, true) => BindingMode::ByValue(Mutability::Mutable),
3460 (false, false) => BindingMode::ByValue(Mutability::Immutable),
3461 };
3462 let fieldpath = codemap::Spanned{span:self.prev_span, node:fieldname};
3463 let fieldpat = P(ast::Pat{
3464 id: ast::DUMMY_NODE_ID,
3465 node: PatKind::Ident(bind_type, fieldpath, None),
3466 span: boxed_span.to(hi),
3467 });
3468
3469 let subpat = if is_box {
3470 P(ast::Pat{
3471 id: ast::DUMMY_NODE_ID,
3472 node: PatKind::Box(fieldpat),
3473 span: lo.to(hi),
3474 })
3475 } else {
3476 fieldpat
3477 };
3478 (subpat, fieldname, true)
3479 };
3480
3481 fields.push(codemap::Spanned { span: lo.to(hi),
3482 node: ast::FieldPat {
3483 ident: fieldname,
3484 pat: subpat,
3485 is_shorthand,
3486 attrs: attrs.into(),
3487 }
3488 });
3489 }
3490 return Ok((fields, etc));
3491 }
3492
3493 fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
3494 if self.token.is_path_start() {
3495 let lo = self.span;
3496 let (qself, path) = if self.eat_lt() {
3497 // Parse a qualified path
3498 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
3499 (Some(qself), path)
3500 } else {
3501 // Parse an unqualified path
3502 (None, self.parse_path(PathStyle::Expr)?)
3503 };
3504 let hi = self.prev_span;
3505 Ok(self.mk_expr(lo.to(hi), ExprKind::Path(qself, path), ThinVec::new()))
3506 } else {
3507 self.parse_pat_literal_maybe_minus()
3508 }
3509 }
3510
3511 // helper function to decide whether to parse as ident binding or to try to do
3512 // something more complex like range patterns
3513 fn parse_as_ident(&mut self) -> bool {
3514 self.look_ahead(1, |t| match *t {
3515 token::OpenDelim(token::Paren) | token::OpenDelim(token::Brace) |
3516 token::DotDotDot | token::DotDotEq | token::ModSep | token::Not => Some(false),
3517 // ensure slice patterns [a, b.., c] and [a, b, c..] don't go into the
3518 // range pattern branch
3519 token::DotDot => None,
3520 _ => Some(true),
3521 }).unwrap_or_else(|| self.look_ahead(2, |t| match *t {
3522 token::Comma | token::CloseDelim(token::Bracket) => true,
3523 _ => false,
3524 }))
3525 }
3526
3527 /// Parse a pattern.
3528 pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
3529 maybe_whole!(self, NtPat, |x| x);
3530
3531 let lo = self.span;
3532 let pat;
3533 match self.token {
3534 token::Underscore => {
3535 // Parse _
3536 self.bump();
3537 pat = PatKind::Wild;
3538 }
3539 token::BinOp(token::And) | token::AndAnd => {
3540 // Parse &pat / &mut pat
3541 self.expect_and()?;
3542 let mutbl = self.parse_mutability();
3543 if let token::Lifetime(ident) = self.token {
3544 return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
3545 }
3546 let subpat = self.parse_pat()?;
3547 pat = PatKind::Ref(subpat, mutbl);
3548 }
3549 token::OpenDelim(token::Paren) => {
3550 // Parse (pat,pat,pat,...) as tuple pattern
3551 self.bump();
3552 let (fields, ddpos) = self.parse_pat_tuple_elements(true)?;
3553 self.expect(&token::CloseDelim(token::Paren))?;
3554 pat = PatKind::Tuple(fields, ddpos);
3555 }
3556 token::OpenDelim(token::Bracket) => {
3557 // Parse [pat,pat,...] as slice pattern
3558 self.bump();
3559 let (before, slice, after) = self.parse_pat_vec_elements()?;
3560 self.expect(&token::CloseDelim(token::Bracket))?;
3561 pat = PatKind::Slice(before, slice, after);
3562 }
3563 // At this point, token != _, &, &&, (, [
3564 _ => if self.eat_keyword(keywords::Mut) {
3565 // Parse mut ident @ pat / mut ref ident @ pat
3566 let mutref_span = self.prev_span.to(self.span);
3567 let binding_mode = if self.eat_keyword(keywords::Ref) {
3568 self.diagnostic()
3569 .struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
3570 .span_suggestion(mutref_span, "try switching the order", "ref mut".into())
3571 .emit();
3572 BindingMode::ByRef(Mutability::Mutable)
3573 } else {
3574 BindingMode::ByValue(Mutability::Mutable)
3575 };
3576 pat = self.parse_pat_ident(binding_mode)?;
3577 } else if self.eat_keyword(keywords::Ref) {
3578 // Parse ref ident @ pat / ref mut ident @ pat
3579 let mutbl = self.parse_mutability();
3580 pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
3581 } else if self.eat_keyword(keywords::Box) {
3582 // Parse box pat
3583 let subpat = self.parse_pat()?;
3584 pat = PatKind::Box(subpat);
3585 } else if self.token.is_ident() && !self.token.is_reserved_ident() &&
3586 self.parse_as_ident() {
3587 // Parse ident @ pat
3588 // This can give false positives and parse nullary enums,
3589 // they are dealt with later in resolve
3590 let binding_mode = BindingMode::ByValue(Mutability::Immutable);
3591 pat = self.parse_pat_ident(binding_mode)?;
3592 } else if self.token.is_path_start() {
3593 // Parse pattern starting with a path
3594 let (qself, path) = if self.eat_lt() {
3595 // Parse a qualified path
3596 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
3597 (Some(qself), path)
3598 } else {
3599 // Parse an unqualified path
3600 (None, self.parse_path(PathStyle::Expr)?)
3601 };
3602 match self.token {
3603 token::Not if qself.is_none() => {
3604 // Parse macro invocation
3605 self.bump();
3606 let (_, tts) = self.expect_delimited_token_tree()?;
3607 let mac = respan(lo.to(self.prev_span), Mac_ { path: path, tts: tts });
3608 pat = PatKind::Mac(mac);
3609 }
3610 token::DotDotDot | token::DotDotEq | token::DotDot => {
3611 let end_kind = match self.token {
3612 token::DotDot => RangeEnd::Excluded,
3613 token::DotDotDot => RangeEnd::Included(RangeSyntax::DotDotDot),
3614 token::DotDotEq => RangeEnd::Included(RangeSyntax::DotDotEq),
3615 _ => panic!("can only parse `..`/`...`/`..=` for ranges \
3616 (checked above)"),
3617 };
3618 // Parse range
3619 let span = lo.to(self.prev_span);
3620 let begin = self.mk_expr(span, ExprKind::Path(qself, path), ThinVec::new());
3621 self.bump();
3622 let end = self.parse_pat_range_end()?;
3623 pat = PatKind::Range(begin, end, end_kind);
3624 }
3625 token::OpenDelim(token::Brace) => {
3626 if qself.is_some() {
3627 return Err(self.fatal("unexpected `{` after qualified path"));
3628 }
3629 // Parse struct pattern
3630 self.bump();
3631 let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
3632 e.emit();
3633 self.recover_stmt();
3634 (vec![], false)
3635 });
3636 self.bump();
3637 pat = PatKind::Struct(path, fields, etc);
3638 }
3639 token::OpenDelim(token::Paren) => {
3640 if qself.is_some() {
3641 return Err(self.fatal("unexpected `(` after qualified path"));
3642 }
3643 // Parse tuple struct or enum pattern
3644 self.bump();
3645 let (fields, ddpos) = self.parse_pat_tuple_elements(false)?;
3646 self.expect(&token::CloseDelim(token::Paren))?;
3647 pat = PatKind::TupleStruct(path, fields, ddpos)
3648 }
3649 _ => pat = PatKind::Path(qself, path),
3650 }
3651 } else {
3652 // Try to parse everything else as literal with optional minus
3653 match self.parse_pat_literal_maybe_minus() {
3654 Ok(begin) => {
3655 if self.eat(&token::DotDotDot) {
3656 let end = self.parse_pat_range_end()?;
3657 pat = PatKind::Range(begin, end,
3658 RangeEnd::Included(RangeSyntax::DotDotDot));
3659 } else if self.eat(&token::DotDotEq) {
3660 let end = self.parse_pat_range_end()?;
3661 pat = PatKind::Range(begin, end,
3662 RangeEnd::Included(RangeSyntax::DotDotEq));
3663 } else if self.eat(&token::DotDot) {
3664 let end = self.parse_pat_range_end()?;
3665 pat = PatKind::Range(begin, end, RangeEnd::Excluded);
3666 } else {
3667 pat = PatKind::Lit(begin);
3668 }
3669 }
3670 Err(mut err) => {
3671 self.cancel(&mut err);
3672 let msg = format!("expected pattern, found {}", self.this_token_descr());
3673 return Err(self.fatal(&msg));
3674 }
3675 }
3676 }
3677 }
3678
3679 Ok(P(ast::Pat {
3680 id: ast::DUMMY_NODE_ID,
3681 node: pat,
3682 span: lo.to(self.prev_span),
3683 }))
3684 }
3685
3686 /// Parse ident or ident @ pat
3687 /// used by the copy foo and ref foo patterns to give a good
3688 /// error message when parsing mistakes like ref foo(a,b)
3689 fn parse_pat_ident(&mut self,
3690 binding_mode: ast::BindingMode)
3691 -> PResult<'a, PatKind> {
3692 let ident_span = self.span;
3693 let ident = self.parse_ident()?;
3694 let name = codemap::Spanned{span: ident_span, node: ident};
3695 let sub = if self.eat(&token::At) {
3696 Some(self.parse_pat()?)
3697 } else {
3698 None
3699 };
3700
3701 // just to be friendly, if they write something like
3702 // ref Some(i)
3703 // we end up here with ( as the current token. This shortly
3704 // leads to a parse error. Note that if there is no explicit
3705 // binding mode then we do not end up here, because the lookahead
3706 // will direct us over to parse_enum_variant()
3707 if self.token == token::OpenDelim(token::Paren) {
3708 return Err(self.span_fatal(
3709 self.prev_span,
3710 "expected identifier, found enum pattern"))
3711 }
3712
3713 Ok(PatKind::Ident(binding_mode, name, sub))
3714 }
3715
3716 /// Parse a local variable declaration
3717 fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
3718 let lo = self.prev_span;
3719 let pat = self.parse_pat()?;
3720
3721 let (err, ty) = if self.eat(&token::Colon) {
3722 // Save the state of the parser before parsing type normally, in case there is a `:`
3723 // instead of an `=` typo.
3724 let parser_snapshot_before_type = self.clone();
3725 let colon_sp = self.prev_span;
3726 match self.parse_ty() {
3727 Ok(ty) => (None, Some(ty)),
3728 Err(mut err) => {
3729 // Rewind to before attempting to parse the type and continue parsing
3730 let parser_snapshot_after_type = self.clone();
3731 mem::replace(self, parser_snapshot_before_type);
3732
3733 let snippet = self.sess.codemap().span_to_snippet(pat.span).unwrap();
3734 err.span_label(pat.span, format!("while parsing the type for `{}`", snippet));
3735 (Some((parser_snapshot_after_type, colon_sp, err)), None)
3736 }
3737 }
3738 } else {
3739 (None, None)
3740 };
3741 let init = match (self.parse_initializer(err.is_some()), err) {
3742 (Ok(init), None) => { // init parsed, ty parsed
3743 init
3744 }
3745 (Ok(init), Some((_, colon_sp, mut err))) => { // init parsed, ty error
3746 // Could parse the type as if it were the initializer, it is likely there was a
3747 // typo in the code: `:` instead of `=`. Add suggestion and emit the error.
3748 err.span_suggestion_short(colon_sp,
3749 "use `=` if you meant to assign",
3750 "=".to_string());
3751 err.emit();
3752 // As this was parsed successfuly, continue as if the code has been fixed for the
3753 // rest of the file. It will still fail due to the emitted error, but we avoid
3754 // extra noise.
3755 init
3756 }
3757 (Err(mut init_err), Some((snapshot, _, ty_err))) => { // init error, ty error
3758 init_err.cancel();
3759 // Couldn't parse the type nor the initializer, only raise the type error and
3760 // return to the parser state before parsing the type as the initializer.
3761 // let x: <parse_error>;
3762 mem::replace(self, snapshot);
3763 return Err(ty_err);
3764 }
3765 (Err(err), None) => { // init error, ty parsed
3766 // Couldn't parse the initializer and we're not attempting to recover a failed
3767 // parse of the type, return the error.
3768 return Err(err);
3769 }
3770 };
3771 let hi = if self.token == token::Semi {
3772 self.span
3773 } else {
3774 self.prev_span
3775 };
3776 Ok(P(ast::Local {
3777 ty,
3778 pat,
3779 init,
3780 id: ast::DUMMY_NODE_ID,
3781 span: lo.to(hi),
3782 attrs,
3783 }))
3784 }
3785
3786 /// Parse a structure field
3787 fn parse_name_and_ty(&mut self,
3788 lo: Span,
3789 vis: Visibility,
3790 attrs: Vec<Attribute>)
3791 -> PResult<'a, StructField> {
3792 let name = self.parse_ident()?;
3793 self.expect(&token::Colon)?;
3794 let ty = self.parse_ty()?;
3795 Ok(StructField {
3796 span: lo.to(self.prev_span),
3797 ident: Some(name),
3798 vis,
3799 id: ast::DUMMY_NODE_ID,
3800 ty,
3801 attrs,
3802 })
3803 }
3804
3805 /// Emit an expected item after attributes error.
3806 fn expected_item_err(&self, attrs: &[Attribute]) {
3807 let message = match attrs.last() {
3808 Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment",
3809 _ => "expected item after attributes",
3810 };
3811
3812 self.span_err(self.prev_span, message);
3813 }
3814
3815 /// Parse a statement. This stops just before trailing semicolons on everything but items.
3816 /// e.g. a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
3817 pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
3818 Ok(self.parse_stmt_(true))
3819 }
3820
3821 // Eat tokens until we can be relatively sure we reached the end of the
3822 // statement. This is something of a best-effort heuristic.
3823 //
3824 // We terminate when we find an unmatched `}` (without consuming it).
3825 fn recover_stmt(&mut self) {
3826 self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore)
3827 }
3828
3829 // If `break_on_semi` is `Break`, then we will stop consuming tokens after
3830 // finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
3831 // approximate - it can mean we break too early due to macros, but that
3832 // shoud only lead to sub-optimal recovery, not inaccurate parsing).
3833 //
3834 // If `break_on_block` is `Break`, then we will stop consuming tokens
3835 // after finding (and consuming) a brace-delimited block.
3836 fn recover_stmt_(&mut self, break_on_semi: SemiColonMode, break_on_block: BlockMode) {
3837 let mut brace_depth = 0;
3838 let mut bracket_depth = 0;
3839 let mut in_block = false;
3840 debug!("recover_stmt_ enter loop (semi={:?}, block={:?})",
3841 break_on_semi, break_on_block);
3842 loop {
3843 debug!("recover_stmt_ loop {:?}", self.token);
3844 match self.token {
3845 token::OpenDelim(token::DelimToken::Brace) => {
3846 brace_depth += 1;
3847 self.bump();
3848 if break_on_block == BlockMode::Break &&
3849 brace_depth == 1 &&
3850 bracket_depth == 0 {
3851 in_block = true;
3852 }
3853 }
3854 token::OpenDelim(token::DelimToken::Bracket) => {
3855 bracket_depth += 1;
3856 self.bump();
3857 }
3858 token::CloseDelim(token::DelimToken::Brace) => {
3859 if brace_depth == 0 {
3860 debug!("recover_stmt_ return - close delim {:?}", self.token);
3861 return;
3862 }
3863 brace_depth -= 1;
3864 self.bump();
3865 if in_block && bracket_depth == 0 && brace_depth == 0 {
3866 debug!("recover_stmt_ return - block end {:?}", self.token);
3867 return;
3868 }
3869 }
3870 token::CloseDelim(token::DelimToken::Bracket) => {
3871 bracket_depth -= 1;
3872 if bracket_depth < 0 {
3873 bracket_depth = 0;
3874 }
3875 self.bump();
3876 }
3877 token::Eof => {
3878 debug!("recover_stmt_ return - Eof");
3879 return;
3880 }
3881 token::Semi => {
3882 self.bump();
3883 if break_on_semi == SemiColonMode::Break &&
3884 brace_depth == 0 &&
3885 bracket_depth == 0 {
3886 debug!("recover_stmt_ return - Semi");
3887 return;
3888 }
3889 }
3890 _ => {
3891 self.bump()
3892 }
3893 }
3894 }
3895 }
3896
3897 fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option<Stmt> {
3898 self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| {
3899 e.emit();
3900 self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
3901 None
3902 })
3903 }
3904
3905 fn is_catch_expr(&mut self) -> bool {
3906 self.token.is_keyword(keywords::Do) &&
3907 self.look_ahead(1, |t| t.is_keyword(keywords::Catch)) &&
3908 self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace)) &&
3909
3910 // prevent `while catch {} {}`, `if catch {} {} else {}`, etc.
3911 !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
3912 }
3913
3914 fn is_union_item(&self) -> bool {
3915 self.token.is_keyword(keywords::Union) &&
3916 self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
3917 }
3918
3919 fn eat_auto_trait(&mut self) -> bool {
3920 if self.token.is_keyword(keywords::Auto)
3921 && self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
3922 {
3923 self.eat_keyword(keywords::Auto) && self.eat_keyword(keywords::Trait)
3924 } else {
3925 false
3926 }
3927 }
3928
3929 fn is_defaultness(&self) -> bool {
3930 // `pub` is included for better error messages
3931 self.token.is_keyword(keywords::Default) &&
3932 self.look_ahead(1, |t| t.is_keyword(keywords::Impl) ||
3933 t.is_keyword(keywords::Const) ||
3934 t.is_keyword(keywords::Fn) ||
3935 t.is_keyword(keywords::Unsafe) ||
3936 t.is_keyword(keywords::Extern) ||
3937 t.is_keyword(keywords::Type) ||
3938 t.is_keyword(keywords::Pub))
3939 }
3940
3941 fn eat_defaultness(&mut self) -> bool {
3942 let is_defaultness = self.is_defaultness();
3943 if is_defaultness {
3944 self.bump()
3945 } else {
3946 self.expected_tokens.push(TokenType::Keyword(keywords::Default));
3947 }
3948 is_defaultness
3949 }
3950
3951 fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility, lo: Span)
3952 -> PResult<'a, Option<P<Item>>> {
3953 let token_lo = self.span;
3954 let (ident, def) = match self.token {
3955 token::Ident(ident) if ident.name == keywords::Macro.name() => {
3956 self.bump();
3957 let ident = self.parse_ident()?;
3958 let tokens = if self.check(&token::OpenDelim(token::Brace)) {
3959 match self.parse_token_tree() {
3960 TokenTree::Delimited(_, ref delimited) => delimited.stream(),
3961 _ => unreachable!(),
3962 }
3963 } else if self.check(&token::OpenDelim(token::Paren)) {
3964 let args = self.parse_token_tree();
3965 let body = if self.check(&token::OpenDelim(token::Brace)) {
3966 self.parse_token_tree()
3967 } else {
3968 self.unexpected()?;
3969 unreachable!()
3970 };
3971 TokenStream::concat(vec![
3972 args.into(),
3973 TokenTree::Token(token_lo.to(self.prev_span), token::FatArrow).into(),
3974 body.into(),
3975 ])
3976 } else {
3977 self.unexpected()?;
3978 unreachable!()
3979 };
3980
3981 (ident, ast::MacroDef { tokens: tokens.into(), legacy: false })
3982 }
3983 token::Ident(ident) if ident.name == "macro_rules" &&
3984 self.look_ahead(1, |t| *t == token::Not) => {
3985 let prev_span = self.prev_span;
3986 self.complain_if_pub_macro(vis, prev_span);
3987 self.bump();
3988 self.bump();
3989
3990 let ident = self.parse_ident()?;
3991 let (delim, tokens) = self.expect_delimited_token_tree()?;
3992 if delim != token::Brace {
3993 if !self.eat(&token::Semi) {
3994 let msg = "macros that expand to items must either \
3995 be surrounded with braces or followed by a semicolon";
3996 self.span_err(self.prev_span, msg);
3997 }
3998 }
3999
4000 (ident, ast::MacroDef { tokens: tokens, legacy: true })
4001 }
4002 _ => return Ok(None),
4003 };
4004
4005 let span = lo.to(self.prev_span);
4006 Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
4007 }
4008
4009 fn parse_stmt_without_recovery(&mut self,
4010 macro_legacy_warnings: bool)
4011 -> PResult<'a, Option<Stmt>> {
4012 maybe_whole!(self, NtStmt, |x| Some(x));
4013
4014 let attrs = self.parse_outer_attributes()?;
4015 let lo = self.span;
4016
4017 Ok(Some(if self.eat_keyword(keywords::Let) {
4018 Stmt {
4019 id: ast::DUMMY_NODE_ID,
4020 node: StmtKind::Local(self.parse_local(attrs.into())?),
4021 span: lo.to(self.prev_span),
4022 }
4023 } else if let Some(macro_def) = self.eat_macro_def(&attrs, &Visibility::Inherited, lo)? {
4024 Stmt {
4025 id: ast::DUMMY_NODE_ID,
4026 node: StmtKind::Item(macro_def),
4027 span: lo.to(self.prev_span),
4028 }
4029 // Starts like a simple path, but not a union item.
4030 } else if self.token.is_path_start() &&
4031 !self.token.is_qpath_start() &&
4032 !self.is_union_item() {
4033 let pth = self.parse_path(PathStyle::Expr)?;
4034
4035 if !self.eat(&token::Not) {
4036 let expr = if self.check(&token::OpenDelim(token::Brace)) {
4037 self.parse_struct_expr(lo, pth, ThinVec::new())?
4038 } else {
4039 let hi = self.prev_span;
4040 self.mk_expr(lo.to(hi), ExprKind::Path(None, pth), ThinVec::new())
4041 };
4042
4043 let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
4044 let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
4045 this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
4046 })?;
4047
4048 return Ok(Some(Stmt {
4049 id: ast::DUMMY_NODE_ID,
4050 node: StmtKind::Expr(expr),
4051 span: lo.to(self.prev_span),
4052 }));
4053 }
4054
4055 // it's a macro invocation
4056 let id = match self.token {
4057 token::OpenDelim(_) => keywords::Invalid.ident(), // no special identifier
4058 _ => self.parse_ident()?,
4059 };
4060
4061 // check that we're pointing at delimiters (need to check
4062 // again after the `if`, because of `parse_ident`
4063 // consuming more tokens).
4064 let delim = match self.token {
4065 token::OpenDelim(delim) => delim,
4066 _ => {
4067 // we only expect an ident if we didn't parse one
4068 // above.
4069 let ident_str = if id.name == keywords::Invalid.name() {
4070 "identifier, "
4071 } else {
4072 ""
4073 };
4074 let tok_str = self.this_token_to_string();
4075 return Err(self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
4076 ident_str,
4077 tok_str)))
4078 },
4079 };
4080
4081 let (_, tts) = self.expect_delimited_token_tree()?;
4082 let hi = self.prev_span;
4083
4084 let style = if delim == token::Brace {
4085 MacStmtStyle::Braces
4086 } else {
4087 MacStmtStyle::NoBraces
4088 };
4089
4090 if id.name == keywords::Invalid.name() {
4091 let mac = respan(lo.to(hi), Mac_ { path: pth, tts: tts });
4092 let node = if delim == token::Brace ||
4093 self.token == token::Semi || self.token == token::Eof {
4094 StmtKind::Mac(P((mac, style, attrs.into())))
4095 }
4096 // We used to incorrectly stop parsing macro-expanded statements here.
4097 // If the next token will be an error anyway but could have parsed with the
4098 // earlier behavior, stop parsing here and emit a warning to avoid breakage.
4099 else if macro_legacy_warnings && self.token.can_begin_expr() && match self.token {
4100 // These can continue an expression, so we can't stop parsing and warn.
4101 token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
4102 token::BinOp(token::Minus) | token::BinOp(token::Star) |
4103 token::BinOp(token::And) | token::BinOp(token::Or) |
4104 token::AndAnd | token::OrOr |
4105 token::DotDot | token::DotDotDot | token::DotDotEq => false,
4106 _ => true,
4107 } {
4108 self.warn_missing_semicolon();
4109 StmtKind::Mac(P((mac, style, attrs.into())))
4110 } else {
4111 let e = self.mk_mac_expr(lo.to(hi), mac.node, ThinVec::new());
4112 let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
4113 let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
4114 StmtKind::Expr(e)
4115 };
4116 Stmt {
4117 id: ast::DUMMY_NODE_ID,
4118 span: lo.to(hi),
4119 node,
4120 }
4121 } else {
4122 // if it has a special ident, it's definitely an item
4123 //
4124 // Require a semicolon or braces.
4125 if style != MacStmtStyle::Braces {
4126 if !self.eat(&token::Semi) {
4127 self.span_err(self.prev_span,
4128 "macros that expand to items must \
4129 either be surrounded with braces or \
4130 followed by a semicolon");
4131 }
4132 }
4133 let span = lo.to(hi);
4134 Stmt {
4135 id: ast::DUMMY_NODE_ID,
4136 span,
4137 node: StmtKind::Item({
4138 self.mk_item(
4139 span, id /*id is good here*/,
4140 ItemKind::Mac(respan(span, Mac_ { path: pth, tts: tts })),
4141 Visibility::Inherited,
4142 attrs)
4143 }),
4144 }
4145 }
4146 } else {
4147 // FIXME: Bad copy of attrs
4148 let old_directory_ownership =
4149 mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
4150 let item = self.parse_item_(attrs.clone(), false, true)?;
4151 self.directory.ownership = old_directory_ownership;
4152
4153 match item {
4154 Some(i) => Stmt {
4155 id: ast::DUMMY_NODE_ID,
4156 span: lo.to(i.span),
4157 node: StmtKind::Item(i),
4158 },
4159 None => {
4160 let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
4161 if !attrs.is_empty() {
4162 if s.prev_token_kind == PrevTokenKind::DocComment {
4163 s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
4164 } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
4165 s.span_err(s.span, "expected statement after outer attribute");
4166 }
4167 }
4168 };
4169
4170 // Do not attempt to parse an expression if we're done here.
4171 if self.token == token::Semi {
4172 unused_attrs(&attrs, self);
4173 self.bump();
4174 return Ok(None);
4175 }
4176
4177 if self.token == token::CloseDelim(token::Brace) {
4178 unused_attrs(&attrs, self);
4179 return Ok(None);
4180 }
4181
4182 // Remainder are line-expr stmts.
4183 let e = self.parse_expr_res(
4184 Restrictions::STMT_EXPR, Some(attrs.into()))?;
4185 Stmt {
4186 id: ast::DUMMY_NODE_ID,
4187 span: lo.to(e.span),
4188 node: StmtKind::Expr(e),
4189 }
4190 }
4191 }
4192 }))
4193 }
4194
4195 /// Is this expression a successfully-parsed statement?
4196 fn expr_is_complete(&mut self, e: &Expr) -> bool {
4197 self.restrictions.contains(Restrictions::STMT_EXPR) &&
4198 !classify::expr_requires_semi_to_be_stmt(e)
4199 }
4200
4201 /// Parse a block. No inner attrs are allowed.
4202 pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
4203 maybe_whole!(self, NtBlock, |x| x);
4204
4205 let lo = self.span;
4206
4207 if !self.eat(&token::OpenDelim(token::Brace)) {
4208 let sp = self.span;
4209 let tok = self.this_token_to_string();
4210 let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
4211
4212 // Check to see if the user has written something like
4213 //
4214 // if (cond)
4215 // bar;
4216 //
4217 // Which is valid in other languages, but not Rust.
4218 match self.parse_stmt_without_recovery(false) {
4219 Ok(Some(stmt)) => {
4220 let mut stmt_span = stmt.span;
4221 // expand the span to include the semicolon, if it exists
4222 if self.eat(&token::Semi) {
4223 stmt_span = stmt_span.with_hi(self.prev_span.hi());
4224 }
4225 let sugg = pprust::to_string(|s| {
4226 use print::pprust::{PrintState, INDENT_UNIT};
4227 s.ibox(INDENT_UNIT)?;
4228 s.bopen()?;
4229 s.print_stmt(&stmt)?;
4230 s.bclose_maybe_open(stmt.span, INDENT_UNIT, false)
4231 });
4232 e.span_suggestion(stmt_span, "try placing this code inside a block", sugg);
4233 }
4234 Err(mut e) => {
4235 self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
4236 self.cancel(&mut e);
4237 }
4238 _ => ()
4239 }
4240 return Err(e);
4241 }
4242
4243 self.parse_block_tail(lo, BlockCheckMode::Default)
4244 }
4245
4246 /// Parse a block. Inner attrs are allowed.
4247 fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
4248 maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
4249
4250 let lo = self.span;
4251 self.expect(&token::OpenDelim(token::Brace))?;
4252 Ok((self.parse_inner_attributes()?,
4253 self.parse_block_tail(lo, BlockCheckMode::Default)?))
4254 }
4255
4256 /// Parse the rest of a block expression or function body
4257 /// Precondition: already parsed the '{'.
4258 fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P<Block>> {
4259 let mut stmts = vec![];
4260
4261 while !self.eat(&token::CloseDelim(token::Brace)) {
4262 if let Some(stmt) = self.parse_full_stmt(false)? {
4263 stmts.push(stmt);
4264 } else if self.token == token::Eof {
4265 break;
4266 } else {
4267 // Found only `;` or `}`.
4268 continue;
4269 };
4270 }
4271
4272 Ok(P(ast::Block {
4273 stmts,
4274 id: ast::DUMMY_NODE_ID,
4275 rules: s,
4276 span: lo.to(self.prev_span),
4277 }))
4278 }
4279
4280 /// Parse a statement, including the trailing semicolon.
4281 pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option<Stmt>> {
4282 let mut stmt = match self.parse_stmt_(macro_legacy_warnings) {
4283 Some(stmt) => stmt,
4284 None => return Ok(None),
4285 };
4286
4287 match stmt.node {
4288 StmtKind::Expr(ref expr) if self.token != token::Eof => {
4289 // expression without semicolon
4290 if classify::expr_requires_semi_to_be_stmt(expr) {
4291 // Just check for errors and recover; do not eat semicolon yet.
4292 if let Err(mut e) =
4293 self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
4294 {
4295 e.emit();
4296 self.recover_stmt();
4297 }
4298 }
4299 }
4300 StmtKind::Local(..) => {
4301 // We used to incorrectly allow a macro-expanded let statement to lack a semicolon.
4302 if macro_legacy_warnings && self.token != token::Semi {
4303 self.warn_missing_semicolon();
4304 } else {
4305 self.expect_one_of(&[token::Semi], &[])?;
4306 }
4307 }
4308 _ => {}
4309 }
4310
4311 if self.eat(&token::Semi) {
4312 stmt = stmt.add_trailing_semicolon();
4313 }
4314
4315 stmt.span = stmt.span.with_hi(self.prev_span.hi());
4316 Ok(Some(stmt))
4317 }
4318
4319 fn warn_missing_semicolon(&self) {
4320 self.diagnostic().struct_span_warn(self.span, {
4321 &format!("expected `;`, found `{}`", self.this_token_to_string())
4322 }).note({
4323 "This was erroneously allowed and will become a hard error in a future release"
4324 }).emit();
4325 }
4326
4327 fn err_dotdotdot_syntax(&self, span: Span) {
4328 self.diagnostic().struct_span_err(span, {
4329 "`...` syntax cannot be used in expressions"
4330 }).help({
4331 "Use `..` if you need an exclusive range (a < b)"
4332 }).help({
4333 "or `..=` if you need an inclusive range (a <= b)"
4334 }).emit();
4335 }
4336
4337 // Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
4338 // BOUND = TY_BOUND | LT_BOUND
4339 // LT_BOUND = LIFETIME (e.g. `'a`)
4340 // TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
4341 // TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`)
4342 fn parse_ty_param_bounds_common(&mut self, allow_plus: bool) -> PResult<'a, TyParamBounds> {
4343 let mut bounds = Vec::new();
4344 loop {
4345 // This needs to be syncronized with `Token::can_begin_bound`.
4346 let is_bound_start = self.check_path() || self.check_lifetime() ||
4347 self.check(&token::Question) ||
4348 self.check_keyword(keywords::For) ||
4349 self.check(&token::OpenDelim(token::Paren));
4350 if is_bound_start {
4351 let has_parens = self.eat(&token::OpenDelim(token::Paren));
4352 let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
4353 if self.token.is_lifetime() {
4354 if let Some(question_span) = question {
4355 self.span_err(question_span,
4356 "`?` may only modify trait bounds, not lifetime bounds");
4357 }
4358 bounds.push(RegionTyParamBound(self.expect_lifetime()));
4359 } else {
4360 let lo = self.span;
4361 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
4362 let path = self.parse_path(PathStyle::Type)?;
4363 let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
4364 let modifier = if question.is_some() {
4365 TraitBoundModifier::Maybe
4366 } else {
4367 TraitBoundModifier::None
4368 };
4369 bounds.push(TraitTyParamBound(poly_trait, modifier));
4370 }
4371 if has_parens {
4372 self.expect(&token::CloseDelim(token::Paren))?;
4373 if let Some(&RegionTyParamBound(..)) = bounds.last() {
4374 self.span_err(self.prev_span,
4375 "parenthesized lifetime bounds are not supported");
4376 }
4377 }
4378 } else {
4379 break
4380 }
4381
4382 if !allow_plus || !self.eat(&token::BinOp(token::Plus)) {
4383 break
4384 }
4385 }
4386
4387 return Ok(bounds);
4388 }
4389
4390 fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds> {
4391 self.parse_ty_param_bounds_common(true)
4392 }
4393
4394 // Parse bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
4395 // BOUND = LT_BOUND (e.g. `'a`)
4396 fn parse_lt_param_bounds(&mut self) -> Vec<Lifetime> {
4397 let mut lifetimes = Vec::new();
4398 while self.check_lifetime() {
4399 lifetimes.push(self.expect_lifetime());
4400
4401 if !self.eat(&token::BinOp(token::Plus)) {
4402 break
4403 }
4404 }
4405 lifetimes
4406 }
4407
4408 /// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
4409 fn parse_ty_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, TyParam> {
4410 let span = self.span;
4411 let ident = self.parse_ident()?;
4412
4413 // Parse optional colon and param bounds.
4414 let bounds = if self.eat(&token::Colon) {
4415 self.parse_ty_param_bounds()?
4416 } else {
4417 Vec::new()
4418 };
4419
4420 let default = if self.eat(&token::Eq) {
4421 Some(self.parse_ty()?)
4422 } else {
4423 None
4424 };
4425
4426 Ok(TyParam {
4427 attrs: preceding_attrs.into(),
4428 ident,
4429 id: ast::DUMMY_NODE_ID,
4430 bounds,
4431 default,
4432 span,
4433 })
4434 }
4435
4436 /// Parses (possibly empty) list of lifetime and type parameters, possibly including
4437 /// trailing comma and erroneous trailing attributes.
4438 pub fn parse_generic_params(&mut self) -> PResult<'a, (Vec<LifetimeDef>, Vec<TyParam>)> {
4439 let mut lifetime_defs = Vec::new();
4440 let mut ty_params = Vec::new();
4441 let mut seen_ty_param = false;
4442 loop {
4443 let attrs = self.parse_outer_attributes()?;
4444 if self.check_lifetime() {
4445 let lifetime = self.expect_lifetime();
4446 // Parse lifetime parameter.
4447 let bounds = if self.eat(&token::Colon) {
4448 self.parse_lt_param_bounds()
4449 } else {
4450 Vec::new()
4451 };
4452 lifetime_defs.push(LifetimeDef {
4453 attrs: attrs.into(),
4454 lifetime,
4455 bounds,
4456 });
4457 if seen_ty_param {
4458 self.span_err(self.prev_span,
4459 "lifetime parameters must be declared prior to type parameters");
4460 }
4461 } else if self.check_ident() {
4462 // Parse type parameter.
4463 ty_params.push(self.parse_ty_param(attrs)?);
4464 seen_ty_param = true;
4465 } else {
4466 // Check for trailing attributes and stop parsing.
4467 if !attrs.is_empty() {
4468 let param_kind = if seen_ty_param { "type" } else { "lifetime" };
4469 self.span_err(attrs[0].span,
4470 &format!("trailing attribute after {} parameters", param_kind));
4471 }
4472 break
4473 }
4474
4475 if !self.eat(&token::Comma) {
4476 break
4477 }
4478 }
4479 Ok((lifetime_defs, ty_params))
4480 }
4481
4482 /// Parse a set of optional generic type parameter declarations. Where
4483 /// clauses are not parsed here, and must be added later via
4484 /// `parse_where_clause()`.
4485 ///
4486 /// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
4487 /// | ( < lifetimes , typaramseq ( , )? > )
4488 /// where typaramseq = ( typaram ) | ( typaram , typaramseq )
4489 pub fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
4490 maybe_whole!(self, NtGenerics, |x| x);
4491
4492 let span_lo = self.span;
4493 if self.eat_lt() {
4494 let (lifetime_defs, ty_params) = self.parse_generic_params()?;
4495 self.expect_gt()?;
4496 Ok(ast::Generics {
4497 lifetimes: lifetime_defs,
4498 ty_params,
4499 where_clause: WhereClause {
4500 id: ast::DUMMY_NODE_ID,
4501 predicates: Vec::new(),
4502 span: syntax_pos::DUMMY_SP,
4503 },
4504 span: span_lo.to(self.prev_span),
4505 })
4506 } else {
4507 Ok(ast::Generics::default())
4508 }
4509 }
4510
4511 /// Parses (possibly empty) list of lifetime and type arguments and associated type bindings,
4512 /// possibly including trailing comma.
4513 fn parse_generic_args(&mut self) -> PResult<'a, (Vec<Lifetime>, Vec<P<Ty>>, Vec<TypeBinding>)> {
4514 let mut lifetimes = Vec::new();
4515 let mut types = Vec::new();
4516 let mut bindings = Vec::new();
4517 let mut seen_type = false;
4518 let mut seen_binding = false;
4519 loop {
4520 if self.check_lifetime() && self.look_ahead(1, |t| t != &token::BinOp(token::Plus)) {
4521 // Parse lifetime argument.
4522 lifetimes.push(self.expect_lifetime());
4523 if seen_type || seen_binding {
4524 self.span_err(self.prev_span,
4525 "lifetime parameters must be declared prior to type parameters");
4526 }
4527 } else if self.check_ident() && self.look_ahead(1, |t| t == &token::Eq) {
4528 // Parse associated type binding.
4529 let lo = self.span;
4530 let ident = self.parse_ident()?;
4531 self.bump();
4532 let ty = self.parse_ty()?;
4533 bindings.push(TypeBinding {
4534 id: ast::DUMMY_NODE_ID,
4535 ident,
4536 ty,
4537 span: lo.to(self.prev_span),
4538 });
4539 seen_binding = true;
4540 } else if self.check_type() {
4541 // Parse type argument.
4542 types.push(self.parse_ty()?);
4543 if seen_binding {
4544 self.span_err(types[types.len() - 1].span,
4545 "type parameters must be declared prior to associated type bindings");
4546 }
4547 seen_type = true;
4548 } else {
4549 break
4550 }
4551
4552 if !self.eat(&token::Comma) {
4553 break
4554 }
4555 }
4556 Ok((lifetimes, types, bindings))
4557 }
4558
4559 /// Parses an optional `where` clause and places it in `generics`.
4560 ///
4561 /// ```ignore (only-for-syntax-highlight)
4562 /// where T : Trait<U, V> + 'b, 'a : 'b
4563 /// ```
4564 pub fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
4565 maybe_whole!(self, NtWhereClause, |x| x);
4566
4567 let mut where_clause = WhereClause {
4568 id: ast::DUMMY_NODE_ID,
4569 predicates: Vec::new(),
4570 span: syntax_pos::DUMMY_SP,
4571 };
4572
4573 if !self.eat_keyword(keywords::Where) {
4574 return Ok(where_clause);
4575 }
4576 let lo = self.prev_span;
4577
4578 // This is a temporary future proofing.
4579 //
4580 // We are considering adding generics to the `where` keyword as an alternative higher-rank
4581 // parameter syntax (as in `where<'a>` or `where<T>`. To avoid that being a breaking
4582 // change, for now we refuse to parse `where < (ident | lifetime) (> | , | :)`.
4583 if token::Lt == self.token {
4584 let ident_or_lifetime = self.look_ahead(1, |t| t.is_ident() || t.is_lifetime());
4585 if ident_or_lifetime {
4586 let gt_comma_or_colon = self.look_ahead(2, |t| {
4587 *t == token::Gt || *t == token::Comma || *t == token::Colon
4588 });
4589 if gt_comma_or_colon {
4590 self.span_err(self.span, "syntax `where<T>` is reserved for future use");
4591 }
4592 }
4593 }
4594
4595 loop {
4596 let lo = self.span;
4597 if self.check_lifetime() && self.look_ahead(1, |t| t != &token::BinOp(token::Plus)) {
4598 let lifetime = self.expect_lifetime();
4599 // Bounds starting with a colon are mandatory, but possibly empty.
4600 self.expect(&token::Colon)?;
4601 let bounds = self.parse_lt_param_bounds();
4602 where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
4603 ast::WhereRegionPredicate {
4604 span: lo.to(self.prev_span),
4605 lifetime,
4606 bounds,
4607 }
4608 ));
4609 } else if self.check_type() {
4610 // Parse optional `for<'a, 'b>`.
4611 // This `for` is parsed greedily and applies to the whole predicate,
4612 // the bounded type can have its own `for` applying only to it.
4613 // Example 1: for<'a> Trait1<'a>: Trait2<'a /*ok*/>
4614 // Example 2: (for<'a> Trait1<'a>): Trait2<'a /*not ok*/>
4615 // Example 3: for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /*ok*/, 'b /*not ok*/>
4616 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
4617
4618 // Parse type with mandatory colon and (possibly empty) bounds,
4619 // or with mandatory equality sign and the second type.
4620 let ty = self.parse_ty()?;
4621 if self.eat(&token::Colon) {
4622 let bounds = self.parse_ty_param_bounds()?;
4623 where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
4624 ast::WhereBoundPredicate {
4625 span: lo.to(self.prev_span),
4626 bound_lifetimes: lifetime_defs,
4627 bounded_ty: ty,
4628 bounds,
4629 }
4630 ));
4631 // FIXME: Decide what should be used here, `=` or `==`.
4632 } else if self.eat(&token::Eq) || self.eat(&token::EqEq) {
4633 let rhs_ty = self.parse_ty()?;
4634 where_clause.predicates.push(ast::WherePredicate::EqPredicate(
4635 ast::WhereEqPredicate {
4636 span: lo.to(self.prev_span),
4637 lhs_ty: ty,
4638 rhs_ty,
4639 id: ast::DUMMY_NODE_ID,
4640 }
4641 ));
4642 } else {
4643 return self.unexpected();
4644 }
4645 } else {
4646 break
4647 }
4648
4649 if !self.eat(&token::Comma) {
4650 break
4651 }
4652 }
4653
4654 where_clause.span = lo.to(self.prev_span);
4655 Ok(where_clause)
4656 }
4657
4658 fn parse_fn_args(&mut self, named_args: bool, allow_variadic: bool)
4659 -> PResult<'a, (Vec<Arg> , bool)> {
4660 let sp = self.span;
4661 let mut variadic = false;
4662 let args: Vec<Option<Arg>> =
4663 self.parse_unspanned_seq(
4664 &token::OpenDelim(token::Paren),
4665 &token::CloseDelim(token::Paren),
4666 SeqSep::trailing_allowed(token::Comma),
4667 |p| {
4668 if p.token == token::DotDotDot {
4669 p.bump();
4670 if allow_variadic {
4671 if p.token != token::CloseDelim(token::Paren) {
4672 let span = p.span;
4673 p.span_err(span,
4674 "`...` must be last in argument list for variadic function");
4675 }
4676 } else {
4677 let span = p.span;
4678 p.span_err(span,
4679 "only foreign functions are allowed to be variadic");
4680 }
4681 variadic = true;
4682 Ok(None)
4683 } else {
4684 match p.parse_arg_general(named_args) {
4685 Ok(arg) => Ok(Some(arg)),
4686 Err(mut e) => {
4687 e.emit();
4688 let lo = p.prev_span;
4689 // Skip every token until next possible arg or end.
4690 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
4691 // Create a placeholder argument for proper arg count (#34264).
4692 let span = lo.to(p.prev_span);
4693 Ok(Some(dummy_arg(span)))
4694 }
4695 }
4696 }
4697 }
4698 )?;
4699
4700 let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
4701
4702 if variadic && args.is_empty() {
4703 self.span_err(sp,
4704 "variadic function must be declared with at least one named argument");
4705 }
4706
4707 Ok((args, variadic))
4708 }
4709
4710 /// Parse the argument list and result type of a function declaration
4711 pub fn parse_fn_decl(&mut self, allow_variadic: bool) -> PResult<'a, P<FnDecl>> {
4712
4713 let (args, variadic) = self.parse_fn_args(true, allow_variadic)?;
4714 let ret_ty = self.parse_ret_ty()?;
4715
4716 Ok(P(FnDecl {
4717 inputs: args,
4718 output: ret_ty,
4719 variadic,
4720 }))
4721 }
4722
4723 /// Returns the parsed optional self argument and whether a self shortcut was used.
4724 fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
4725 let expect_ident = |this: &mut Self| match this.token {
4726 // Preserve hygienic context.
4727 token::Ident(ident) => { let sp = this.span; this.bump(); codemap::respan(sp, ident) }
4728 _ => unreachable!()
4729 };
4730 let isolated_self = |this: &mut Self, n| {
4731 this.look_ahead(n, |t| t.is_keyword(keywords::SelfValue)) &&
4732 this.look_ahead(n + 1, |t| t != &token::ModSep)
4733 };
4734
4735 // Parse optional self parameter of a method.
4736 // Only a limited set of initial token sequences is considered self parameters, anything
4737 // else is parsed as a normal function parameter list, so some lookahead is required.
4738 let eself_lo = self.span;
4739 let (eself, eself_ident) = match self.token {
4740 token::BinOp(token::And) => {
4741 // &self
4742 // &mut self
4743 // &'lt self
4744 // &'lt mut self
4745 // &not_self
4746 if isolated_self(self, 1) {
4747 self.bump();
4748 (SelfKind::Region(None, Mutability::Immutable), expect_ident(self))
4749 } else if self.look_ahead(1, |t| t.is_keyword(keywords::Mut)) &&
4750 isolated_self(self, 2) {
4751 self.bump();
4752 self.bump();
4753 (SelfKind::Region(None, Mutability::Mutable), expect_ident(self))
4754 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4755 isolated_self(self, 2) {
4756 self.bump();
4757 let lt = self.expect_lifetime();
4758 (SelfKind::Region(Some(lt), Mutability::Immutable), expect_ident(self))
4759 } else if self.look_ahead(1, |t| t.is_lifetime()) &&
4760 self.look_ahead(2, |t| t.is_keyword(keywords::Mut)) &&
4761 isolated_self(self, 3) {
4762 self.bump();
4763 let lt = self.expect_lifetime();
4764 self.bump();
4765 (SelfKind::Region(Some(lt), Mutability::Mutable), expect_ident(self))
4766 } else {
4767 return Ok(None);
4768 }
4769 }
4770 token::BinOp(token::Star) => {
4771 // *self
4772 // *const self
4773 // *mut self
4774 // *not_self
4775 // Emit special error for `self` cases.
4776 if isolated_self(self, 1) {
4777 self.bump();
4778 self.span_err(self.span, "cannot pass `self` by raw pointer");
4779 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4780 } else if self.look_ahead(1, |t| t.is_mutability()) &&
4781 isolated_self(self, 2) {
4782 self.bump();
4783 self.bump();
4784 self.span_err(self.span, "cannot pass `self` by raw pointer");
4785 (SelfKind::Value(Mutability::Immutable), expect_ident(self))
4786 } else {
4787 return Ok(None);
4788 }
4789 }
4790 token::Ident(..) => {
4791 if isolated_self(self, 0) {
4792 // self
4793 // self: TYPE
4794 let eself_ident = expect_ident(self);
4795 if self.eat(&token::Colon) {
4796 let ty = self.parse_ty()?;
4797 (SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
4798 } else {
4799 (SelfKind::Value(Mutability::Immutable), eself_ident)
4800 }
4801 } else if self.token.is_keyword(keywords::Mut) &&
4802 isolated_self(self, 1) {
4803 // mut self
4804 // mut self: TYPE
4805 self.bump();
4806 let eself_ident = expect_ident(self);
4807 if self.eat(&token::Colon) {
4808 let ty = self.parse_ty()?;
4809 (SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
4810 } else {
4811 (SelfKind::Value(Mutability::Mutable), eself_ident)
4812 }
4813 } else {
4814 return Ok(None);
4815 }
4816 }
4817 _ => return Ok(None),
4818 };
4819
4820 let eself = codemap::respan(eself_lo.to(self.prev_span), eself);
4821 Ok(Some(Arg::from_self(eself, eself_ident)))
4822 }
4823
4824 /// Parse the parameter list and result type of a function that may have a `self` parameter.
4825 fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
4826 where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
4827 {
4828 self.expect(&token::OpenDelim(token::Paren))?;
4829
4830 // Parse optional self argument
4831 let self_arg = self.parse_self_arg()?;
4832
4833 // Parse the rest of the function parameter list.
4834 let sep = SeqSep::trailing_allowed(token::Comma);
4835 let fn_inputs = if let Some(self_arg) = self_arg {
4836 if self.check(&token::CloseDelim(token::Paren)) {
4837 vec![self_arg]
4838 } else if self.eat(&token::Comma) {
4839 let mut fn_inputs = vec![self_arg];
4840 fn_inputs.append(&mut self.parse_seq_to_before_end(
4841 &token::CloseDelim(token::Paren), sep, parse_arg_fn)?
4842 );
4843 fn_inputs
4844 } else {
4845 return self.unexpected();
4846 }
4847 } else {
4848 self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)?
4849 };
4850
4851 // Parse closing paren and return type.
4852 self.expect(&token::CloseDelim(token::Paren))?;
4853 Ok(P(FnDecl {
4854 inputs: fn_inputs,
4855 output: self.parse_ret_ty()?,
4856 variadic: false
4857 }))
4858 }
4859
4860 // parse the |arg, arg| header on a lambda
4861 fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
4862 let inputs_captures = {
4863 if self.eat(&token::OrOr) {
4864 Vec::new()
4865 } else {
4866 self.expect(&token::BinOp(token::Or))?;
4867 let args = self.parse_seq_to_before_tokens(
4868 &[&token::BinOp(token::Or), &token::OrOr],
4869 SeqSep::trailing_allowed(token::Comma),
4870 TokenExpectType::NoExpect,
4871 |p| p.parse_fn_block_arg()
4872 )?;
4873 self.expect_or()?;
4874 args
4875 }
4876 };
4877 let output = self.parse_ret_ty()?;
4878
4879 Ok(P(FnDecl {
4880 inputs: inputs_captures,
4881 output,
4882 variadic: false
4883 }))
4884 }
4885
4886 /// Parse the name and optional generic types of a function header.
4887 fn parse_fn_header(&mut self) -> PResult<'a, (Ident, ast::Generics)> {
4888 let id = self.parse_ident()?;
4889 let generics = self.parse_generics()?;
4890 Ok((id, generics))
4891 }
4892
4893 fn mk_item(&mut self, span: Span, ident: Ident, node: ItemKind, vis: Visibility,
4894 attrs: Vec<Attribute>) -> P<Item> {
4895 P(Item {
4896 ident,
4897 attrs,
4898 id: ast::DUMMY_NODE_ID,
4899 node,
4900 vis,
4901 span,
4902 tokens: None,
4903 })
4904 }
4905
4906 /// Parse an item-position function declaration.
4907 fn parse_item_fn(&mut self,
4908 unsafety: Unsafety,
4909 constness: Spanned<Constness>,
4910 abi: abi::Abi)
4911 -> PResult<'a, ItemInfo> {
4912 let (ident, mut generics) = self.parse_fn_header()?;
4913 let decl = self.parse_fn_decl(false)?;
4914 generics.where_clause = self.parse_where_clause()?;
4915 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4916 Ok((ident, ItemKind::Fn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs)))
4917 }
4918
4919 /// true if we are looking at `const ID`, false for things like `const fn` etc
4920 pub fn is_const_item(&mut self) -> bool {
4921 self.token.is_keyword(keywords::Const) &&
4922 !self.look_ahead(1, |t| t.is_keyword(keywords::Fn)) &&
4923 !self.look_ahead(1, |t| t.is_keyword(keywords::Unsafe))
4924 }
4925
4926 /// parses all the "front matter" for a `fn` declaration, up to
4927 /// and including the `fn` keyword:
4928 ///
4929 /// - `const fn`
4930 /// - `unsafe fn`
4931 /// - `const unsafe fn`
4932 /// - `extern fn`
4933 /// - etc
4934 pub fn parse_fn_front_matter(&mut self)
4935 -> PResult<'a, (Spanned<ast::Constness>,
4936 ast::Unsafety,
4937 abi::Abi)> {
4938 let is_const_fn = self.eat_keyword(keywords::Const);
4939 let const_span = self.prev_span;
4940 let unsafety = self.parse_unsafety()?;
4941 let (constness, unsafety, abi) = if is_const_fn {
4942 (respan(const_span, Constness::Const), unsafety, Abi::Rust)
4943 } else {
4944 let abi = if self.eat_keyword(keywords::Extern) {
4945 self.parse_opt_abi()?.unwrap_or(Abi::C)
4946 } else {
4947 Abi::Rust
4948 };
4949 (respan(self.prev_span, Constness::NotConst), unsafety, abi)
4950 };
4951 self.expect_keyword(keywords::Fn)?;
4952 Ok((constness, unsafety, abi))
4953 }
4954
4955 /// Parse an impl item.
4956 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, ImplItem> {
4957 maybe_whole!(self, NtImplItem, |x| x);
4958 let attrs = self.parse_outer_attributes()?;
4959 let (mut item, tokens) = self.collect_tokens(|this| {
4960 this.parse_impl_item_(at_end, attrs)
4961 })?;
4962
4963 // See `parse_item` for why this clause is here.
4964 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
4965 item.tokens = Some(tokens);
4966 }
4967 Ok(item)
4968 }
4969
4970 fn parse_impl_item_(&mut self,
4971 at_end: &mut bool,
4972 mut attrs: Vec<Attribute>) -> PResult<'a, ImplItem> {
4973 let lo = self.span;
4974 let vis = self.parse_visibility(false)?;
4975 let defaultness = self.parse_defaultness()?;
4976 let (name, node, generics) = if self.eat_keyword(keywords::Type) {
4977 let name = self.parse_ident()?;
4978 self.expect(&token::Eq)?;
4979 let typ = self.parse_ty()?;
4980 self.expect(&token::Semi)?;
4981 (name, ast::ImplItemKind::Type(typ), ast::Generics::default())
4982 } else if self.is_const_item() {
4983 self.expect_keyword(keywords::Const)?;
4984 let name = self.parse_ident()?;
4985 self.expect(&token::Colon)?;
4986 let typ = self.parse_ty()?;
4987 self.expect(&token::Eq)?;
4988 let expr = self.parse_expr()?;
4989 self.expect(&token::Semi)?;
4990 (name, ast::ImplItemKind::Const(typ, expr), ast::Generics::default())
4991 } else {
4992 let (name, inner_attrs, generics, node) = self.parse_impl_method(&vis, at_end)?;
4993 attrs.extend(inner_attrs);
4994 (name, node, generics)
4995 };
4996
4997 Ok(ImplItem {
4998 id: ast::DUMMY_NODE_ID,
4999 span: lo.to(self.prev_span),
5000 ident: name,
5001 vis,
5002 defaultness,
5003 attrs,
5004 generics,
5005 node,
5006 tokens: None,
5007 })
5008 }
5009
5010 fn complain_if_pub_macro(&mut self, vis: &Visibility, sp: Span) {
5011 if let Err(mut err) = self.complain_if_pub_macro_diag(vis, sp) {
5012 err.emit();
5013 }
5014 }
5015
5016 fn complain_if_pub_macro_diag(&mut self, vis: &Visibility, sp: Span) -> PResult<'a, ()> {
5017 match *vis {
5018 Visibility::Inherited => Ok(()),
5019 _ => {
5020 let is_macro_rules: bool = match self.token {
5021 token::Ident(sid) => sid.name == Symbol::intern("macro_rules"),
5022 _ => false,
5023 };
5024 if is_macro_rules {
5025 let mut err = self.diagnostic()
5026 .struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
5027 err.help("did you mean #[macro_export]?");
5028 Err(err)
5029 } else {
5030 let mut err = self.diagnostic()
5031 .struct_span_err(sp, "can't qualify macro invocation with `pub`");
5032 err.help("try adjusting the macro to put `pub` inside the invocation");
5033 Err(err)
5034 }
5035 }
5036 }
5037 }
5038
5039 fn missing_assoc_item_kind_err(&mut self, item_type: &str, prev_span: Span)
5040 -> DiagnosticBuilder<'a>
5041 {
5042 // Given this code `path(`, it seems like this is not
5043 // setting the visibility of a macro invocation, but rather
5044 // a mistyped method declaration.
5045 // Create a diagnostic pointing out that `fn` is missing.
5046 //
5047 // x | pub path(&self) {
5048 // | ^ missing `fn`, `type`, or `const`
5049 // pub path(
5050 // ^^ `sp` below will point to this
5051 let sp = prev_span.between(self.prev_span);
5052 let mut err = self.diagnostic().struct_span_err(
5053 sp,
5054 &format!("missing `fn`, `type`, or `const` for {}-item declaration",
5055 item_type));
5056 err.span_label(sp, "missing `fn`, `type`, or `const`");
5057 err
5058 }
5059
5060 /// Parse a method or a macro invocation in a trait impl.
5061 fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool)
5062 -> PResult<'a, (Ident, Vec<ast::Attribute>, ast::Generics,
5063 ast::ImplItemKind)> {
5064 // code copied from parse_macro_use_or_failure... abstraction!
5065 if self.token.is_path_start() {
5066 // Method macro.
5067
5068 let prev_span = self.prev_span;
5069
5070 let lo = self.span;
5071 let pth = self.parse_path(PathStyle::Mod)?;
5072 if pth.segments.len() == 1 {
5073 if !self.eat(&token::Not) {
5074 return Err(self.missing_assoc_item_kind_err("impl", prev_span));
5075 }
5076 } else {
5077 self.expect(&token::Not)?;
5078 }
5079
5080 self.complain_if_pub_macro(vis, prev_span);
5081
5082 // eat a matched-delimiter token tree:
5083 *at_end = true;
5084 let (delim, tts) = self.expect_delimited_token_tree()?;
5085 if delim != token::Brace {
5086 self.expect(&token::Semi)?
5087 }
5088
5089 let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
5090 Ok((keywords::Invalid.ident(), vec![], ast::Generics::default(),
5091 ast::ImplItemKind::Macro(mac)))
5092 } else {
5093 let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
5094 let ident = self.parse_ident()?;
5095 let mut generics = self.parse_generics()?;
5096 let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
5097 generics.where_clause = self.parse_where_clause()?;
5098 *at_end = true;
5099 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
5100 Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method(ast::MethodSig {
5101 abi,
5102 unsafety,
5103 constness,
5104 decl,
5105 }, body)))
5106 }
5107 }
5108
5109 /// Parse trait Foo { ... }
5110 fn parse_item_trait(&mut self, is_auto: IsAuto, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
5111 let ident = self.parse_ident()?;
5112 let mut tps = self.parse_generics()?;
5113
5114 // Parse optional colon and supertrait bounds.
5115 let bounds = if self.eat(&token::Colon) {
5116 self.parse_ty_param_bounds()?
5117 } else {
5118 Vec::new()
5119 };
5120
5121 tps.where_clause = self.parse_where_clause()?;
5122
5123 self.expect(&token::OpenDelim(token::Brace))?;
5124 let mut trait_items = vec![];
5125 while !self.eat(&token::CloseDelim(token::Brace)) {
5126 let mut at_end = false;
5127 match self.parse_trait_item(&mut at_end) {
5128 Ok(item) => trait_items.push(item),
5129 Err(mut e) => {
5130 e.emit();
5131 if !at_end {
5132 self.recover_stmt_(SemiColonMode::Break, BlockMode::Break);
5133 }
5134 }
5135 }
5136 }
5137 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None))
5138 }
5139
5140 /// Parses items implementations variants
5141 /// impl<T> Foo { ... }
5142 /// impl<T> ToString for &'static T { ... }
5143 /// impl Send for .. {}
5144 fn parse_item_impl(&mut self,
5145 unsafety: ast::Unsafety,
5146 defaultness: Defaultness) -> PResult<'a, ItemInfo> {
5147 let impl_span = self.span;
5148
5149 // First, parse type parameters if necessary.
5150 let mut generics = self.parse_generics()?;
5151
5152 // Special case: if the next identifier that follows is '(', don't
5153 // allow this to be parsed as a trait.
5154 let could_be_trait = self.token != token::OpenDelim(token::Paren);
5155
5156 let neg_span = self.span;
5157 let polarity = if self.eat(&token::Not) {
5158 ast::ImplPolarity::Negative
5159 } else {
5160 ast::ImplPolarity::Positive
5161 };
5162
5163 // Parse the trait.
5164 let mut ty = self.parse_ty()?;
5165
5166 // Parse traits, if necessary.
5167 let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
5168 // New-style trait. Reinterpret the type as a trait.
5169 match ty.node {
5170 TyKind::Path(None, ref path) => {
5171 Some(TraitRef {
5172 path: (*path).clone(),
5173 ref_id: ty.id,
5174 })
5175 }
5176 _ => {
5177 self.span_err(ty.span, "not a trait");
5178 None
5179 }
5180 }
5181 } else {
5182 if polarity == ast::ImplPolarity::Negative {
5183 // This is a negated type implementation
5184 // `impl !MyType {}`, which is not allowed.
5185 self.span_err(neg_span, "inherent implementation can't be negated");
5186 }
5187 None
5188 };
5189
5190 if opt_trait.is_some() && self.eat(&token::DotDot) {
5191 if generics.is_parameterized() {
5192 self.span_err(impl_span, "auto trait implementations are not \
5193 allowed to have generics");
5194 }
5195
5196 if let ast::Defaultness::Default = defaultness {
5197 self.span_err(impl_span, "`default impl` is not allowed for \
5198 auto trait implementations");
5199 }
5200
5201 self.expect(&token::OpenDelim(token::Brace))?;
5202 self.expect(&token::CloseDelim(token::Brace))?;
5203 Ok((keywords::Invalid.ident(),
5204 ItemKind::AutoImpl(unsafety, opt_trait.unwrap()), None))
5205 } else {
5206 if opt_trait.is_some() {
5207 ty = self.parse_ty()?;
5208 }
5209 generics.where_clause = self.parse_where_clause()?;
5210
5211 self.expect(&token::OpenDelim(token::Brace))?;
5212 let attrs = self.parse_inner_attributes()?;
5213
5214 let mut impl_items = vec![];
5215 while !self.eat(&token::CloseDelim(token::Brace)) {
5216 let mut at_end = false;
5217 match self.parse_impl_item(&mut at_end) {
5218 Ok(item) => impl_items.push(item),
5219 Err(mut e) => {
5220 e.emit();
5221 if !at_end {
5222 self.recover_stmt_(SemiColonMode::Break, BlockMode::Break);
5223 }
5224 }
5225 }
5226 }
5227
5228 Ok((keywords::Invalid.ident(),
5229 ItemKind::Impl(unsafety, polarity, defaultness, generics, opt_trait, ty, impl_items),
5230 Some(attrs)))
5231 }
5232 }
5233
5234 fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<LifetimeDef>> {
5235 if self.eat_keyword(keywords::For) {
5236 self.expect_lt()?;
5237 let (lifetime_defs, ty_params) = self.parse_generic_params()?;
5238 self.expect_gt()?;
5239 if !ty_params.is_empty() {
5240 self.span_err(ty_params[0].span,
5241 "only lifetime parameters can be used in this context");
5242 }
5243 Ok(lifetime_defs)
5244 } else {
5245 Ok(Vec::new())
5246 }
5247 }
5248
5249 /// Parse struct Foo { ... }
5250 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
5251 let class_name = self.parse_ident()?;
5252
5253 let mut generics = self.parse_generics()?;
5254
5255 // There is a special case worth noting here, as reported in issue #17904.
5256 // If we are parsing a tuple struct it is the case that the where clause
5257 // should follow the field list. Like so:
5258 //
5259 // struct Foo<T>(T) where T: Copy;
5260 //
5261 // If we are parsing a normal record-style struct it is the case
5262 // that the where clause comes before the body, and after the generics.
5263 // So if we look ahead and see a brace or a where-clause we begin
5264 // parsing a record style struct.
5265 //
5266 // Otherwise if we look ahead and see a paren we parse a tuple-style
5267 // struct.
5268
5269 let vdata = if self.token.is_keyword(keywords::Where) {
5270 generics.where_clause = self.parse_where_clause()?;
5271 if self.eat(&token::Semi) {
5272 // If we see a: `struct Foo<T> where T: Copy;` style decl.
5273 VariantData::Unit(ast::DUMMY_NODE_ID)
5274 } else {
5275 // If we see: `struct Foo<T> where T: Copy { ... }`
5276 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5277 }
5278 // No `where` so: `struct Foo<T>;`
5279 } else if self.eat(&token::Semi) {
5280 VariantData::Unit(ast::DUMMY_NODE_ID)
5281 // Record-style struct definition
5282 } else if self.token == token::OpenDelim(token::Brace) {
5283 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5284 // Tuple-style struct definition with optional where-clause.
5285 } else if self.token == token::OpenDelim(token::Paren) {
5286 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, ast::DUMMY_NODE_ID);
5287 generics.where_clause = self.parse_where_clause()?;
5288 self.expect(&token::Semi)?;
5289 body
5290 } else {
5291 let token_str = self.this_token_to_string();
5292 return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
5293 name, found `{}`", token_str)))
5294 };
5295
5296 Ok((class_name, ItemKind::Struct(vdata, generics), None))
5297 }
5298
5299 /// Parse union Foo { ... }
5300 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
5301 let class_name = self.parse_ident()?;
5302
5303 let mut generics = self.parse_generics()?;
5304
5305 let vdata = if self.token.is_keyword(keywords::Where) {
5306 generics.where_clause = self.parse_where_clause()?;
5307 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5308 } else if self.token == token::OpenDelim(token::Brace) {
5309 VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
5310 } else {
5311 let token_str = self.this_token_to_string();
5312 return Err(self.fatal(&format!("expected `where` or `{{` after union \
5313 name, found `{}`", token_str)))
5314 };
5315
5316 Ok((class_name, ItemKind::Union(vdata, generics), None))
5317 }
5318
5319 pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5320 let mut fields = Vec::new();
5321 if self.eat(&token::OpenDelim(token::Brace)) {
5322 while self.token != token::CloseDelim(token::Brace) {
5323 fields.push(self.parse_struct_decl_field().map_err(|e| {
5324 self.recover_stmt();
5325 self.eat(&token::CloseDelim(token::Brace));
5326 e
5327 })?);
5328 }
5329
5330 self.bump();
5331 } else {
5332 let token_str = self.this_token_to_string();
5333 return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
5334 name, found `{}`",
5335 token_str)));
5336 }
5337
5338 Ok(fields)
5339 }
5340
5341 pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
5342 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
5343 // Unit like structs are handled in parse_item_struct function
5344 let fields = self.parse_unspanned_seq(
5345 &token::OpenDelim(token::Paren),
5346 &token::CloseDelim(token::Paren),
5347 SeqSep::trailing_allowed(token::Comma),
5348 |p| {
5349 let attrs = p.parse_outer_attributes()?;
5350 let lo = p.span;
5351 let vis = p.parse_visibility(true)?;
5352 let ty = p.parse_ty()?;
5353 Ok(StructField {
5354 span: lo.to(p.span),
5355 vis,
5356 ident: None,
5357 id: ast::DUMMY_NODE_ID,
5358 ty,
5359 attrs,
5360 })
5361 })?;
5362
5363 Ok(fields)
5364 }
5365
5366 /// Parse a structure field declaration
5367 pub fn parse_single_struct_field(&mut self,
5368 lo: Span,
5369 vis: Visibility,
5370 attrs: Vec<Attribute> )
5371 -> PResult<'a, StructField> {
5372 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
5373 match self.token {
5374 token::Comma => {
5375 self.bump();
5376 }
5377 token::CloseDelim(token::Brace) => {}
5378 token::DocComment(_) => return Err(self.span_fatal_err(self.span,
5379 Error::UselessDocComment)),
5380 _ => return Err(self.span_fatal_help(self.span,
5381 &format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
5382 "struct fields should be separated by commas")),
5383 }
5384 Ok(a_var)
5385 }
5386
5387 /// Parse an element of a struct definition
5388 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
5389 let attrs = self.parse_outer_attributes()?;
5390 let lo = self.span;
5391 let vis = self.parse_visibility(false)?;
5392 self.parse_single_struct_field(lo, vis, attrs)
5393 }
5394
5395 /// Parse `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `pub(self)` for `pub(in self)`
5396 /// and `pub(super)` for `pub(in super)`. If the following element can't be a tuple (i.e. it's
5397 /// a function definition, it's not a tuple struct field) and the contents within the parens
5398 /// isn't valid, emit a proper diagnostic.
5399 pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
5400 maybe_whole!(self, NtVis, |x| x);
5401
5402 if self.eat_keyword(keywords::Crate) {
5403 return Ok(Visibility::Crate(self.prev_span, CrateSugar::JustCrate));
5404 }
5405
5406 if !self.eat_keyword(keywords::Pub) {
5407 return Ok(Visibility::Inherited)
5408 }
5409
5410 if self.check(&token::OpenDelim(token::Paren)) {
5411 // We don't `self.bump()` the `(` yet because this might be a struct definition where
5412 // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
5413 // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
5414 // by the following tokens.
5415 if self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
5416 // `pub(crate)`
5417 self.bump(); // `(`
5418 self.bump(); // `crate`
5419 let vis = Visibility::Crate(self.prev_span, CrateSugar::PubCrate);
5420 self.expect(&token::CloseDelim(token::Paren))?; // `)`
5421 return Ok(vis)
5422 } else if self.look_ahead(1, |t| t.is_keyword(keywords::In)) {
5423 // `pub(in path)`
5424 self.bump(); // `(`
5425 self.bump(); // `in`
5426 let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `path`
5427 let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5428 self.expect(&token::CloseDelim(token::Paren))?; // `)`
5429 return Ok(vis)
5430 } else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren)) &&
5431 self.look_ahead(1, |t| t.is_keyword(keywords::Super) ||
5432 t.is_keyword(keywords::SelfValue)) {
5433 // `pub(self)` or `pub(super)`
5434 self.bump(); // `(`
5435 let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `super`/`self`
5436 let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
5437 self.expect(&token::CloseDelim(token::Paren))?; // `)`
5438 return Ok(vis)
5439 } else if !can_take_tuple { // Provide this diagnostic if this is not a tuple struct
5440 // `pub(something) fn ...` or `struct X { pub(something) y: Z }`
5441 self.bump(); // `(`
5442 let msg = "incorrect visibility restriction";
5443 let suggestion = r##"some possible visibility restrictions are:
5444 `pub(crate)`: visible only on the current crate
5445 `pub(super)`: visible only in the current module's parent
5446 `pub(in path::to::module)`: visible only on the specified path"##;
5447 let path = self.parse_path(PathStyle::Mod)?;
5448 let path_span = self.prev_span;
5449 let help_msg = format!("make this visible only to module `{}` with `in`", path);
5450 self.expect(&token::CloseDelim(token::Paren))?; // `)`
5451 let mut err = self.span_fatal_help(path_span, msg, suggestion);
5452 err.span_suggestion(path_span, &help_msg, format!("in {}", path));
5453 err.emit(); // emit diagnostic, but continue with public visibility
5454 }
5455 }
5456
5457 Ok(Visibility::Public)
5458 }
5459
5460 /// Parse defaultness: DEFAULT or nothing
5461 fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
5462 if self.eat_defaultness() {
5463 Ok(Defaultness::Default)
5464 } else {
5465 Ok(Defaultness::Final)
5466 }
5467 }
5468
5469 /// Given a termination token, parse all of the items in a module
5470 fn parse_mod_items(&mut self, term: &token::Token, inner_lo: Span) -> PResult<'a, Mod> {
5471 let mut items = vec![];
5472 while let Some(item) = self.parse_item()? {
5473 items.push(item);
5474 }
5475
5476 if !self.eat(term) {
5477 let token_str = self.this_token_to_string();
5478 return Err(self.fatal(&format!("expected item, found `{}`", token_str)));
5479 }
5480
5481 let hi = if self.span == syntax_pos::DUMMY_SP {
5482 inner_lo
5483 } else {
5484 self.prev_span
5485 };
5486
5487 Ok(ast::Mod {
5488 inner: inner_lo.to(hi),
5489 items,
5490 })
5491 }
5492
5493 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
5494 let id = self.parse_ident()?;
5495 self.expect(&token::Colon)?;
5496 let ty = self.parse_ty()?;
5497 self.expect(&token::Eq)?;
5498 let e = self.parse_expr()?;
5499 self.expect(&token::Semi)?;
5500 let item = match m {
5501 Some(m) => ItemKind::Static(ty, m, e),
5502 None => ItemKind::Const(ty, e),
5503 };
5504 Ok((id, item, None))
5505 }
5506
5507 /// Parse a `mod <foo> { ... }` or `mod <foo>;` item
5508 fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
5509 let (in_cfg, outer_attrs) = {
5510 let mut strip_unconfigured = ::config::StripUnconfigured {
5511 sess: self.sess,
5512 should_test: false, // irrelevant
5513 features: None, // don't perform gated feature checking
5514 };
5515 let outer_attrs = strip_unconfigured.process_cfg_attrs(outer_attrs.to_owned());
5516 (!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
5517 };
5518
5519 let id_span = self.span;
5520 let id = self.parse_ident()?;
5521 if self.check(&token::Semi) {
5522 self.bump();
5523 if in_cfg && self.recurse_into_file_modules {
5524 // This mod is in an external file. Let's go get it!
5525 let ModulePathSuccess { path, directory_ownership, warn } =
5526 self.submod_path(id, &outer_attrs, id_span)?;
5527 let (module, mut attrs) =
5528 self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
5529 if warn {
5530 let attr = ast::Attribute {
5531 id: attr::mk_attr_id(),
5532 style: ast::AttrStyle::Outer,
5533 path: ast::Path::from_ident(syntax_pos::DUMMY_SP,
5534 Ident::from_str("warn_directory_ownership")),
5535 tokens: TokenStream::empty(),
5536 is_sugared_doc: false,
5537 span: syntax_pos::DUMMY_SP,
5538 };
5539 attr::mark_known(&attr);
5540 attrs.push(attr);
5541 }
5542 Ok((id, module, Some(attrs)))
5543 } else {
5544 let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
5545 Ok((id, ItemKind::Mod(placeholder), None))
5546 }
5547 } else {
5548 let old_directory = self.directory.clone();
5549 self.push_directory(id, &outer_attrs);
5550
5551 self.expect(&token::OpenDelim(token::Brace))?;
5552 let mod_inner_lo = self.span;
5553 let attrs = self.parse_inner_attributes()?;
5554 let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
5555
5556 self.directory = old_directory;
5557 Ok((id, ItemKind::Mod(module), Some(attrs)))
5558 }
5559 }
5560
5561 fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
5562 if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
5563 self.directory.path.push(&path.as_str());
5564 self.directory.ownership = DirectoryOwnership::Owned;
5565 } else {
5566 self.directory.path.push(&id.name.as_str());
5567 }
5568 }
5569
5570 pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
5571 attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&d.as_str()))
5572 }
5573
5574 /// Returns either a path to a module, or .
5575 pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath {
5576 let mod_name = id.to_string();
5577 let default_path_str = format!("{}.rs", mod_name);
5578 let secondary_path_str = format!("{}{}mod.rs", mod_name, path::MAIN_SEPARATOR);
5579 let default_path = dir_path.join(&default_path_str);
5580 let secondary_path = dir_path.join(&secondary_path_str);
5581 let default_exists = codemap.file_exists(&default_path);
5582 let secondary_exists = codemap.file_exists(&secondary_path);
5583
5584 let result = match (default_exists, secondary_exists) {
5585 (true, false) => Ok(ModulePathSuccess {
5586 path: default_path,
5587 directory_ownership: DirectoryOwnership::UnownedViaMod(false),
5588 warn: false,
5589 }),
5590 (false, true) => Ok(ModulePathSuccess {
5591 path: secondary_path,
5592 directory_ownership: DirectoryOwnership::Owned,
5593 warn: false,
5594 }),
5595 (false, false) => Err(Error::FileNotFoundForModule {
5596 mod_name: mod_name.clone(),
5597 default_path: default_path_str,
5598 secondary_path: secondary_path_str,
5599 dir_path: format!("{}", dir_path.display()),
5600 }),
5601 (true, true) => Err(Error::DuplicatePaths {
5602 mod_name: mod_name.clone(),
5603 default_path: default_path_str,
5604 secondary_path: secondary_path_str,
5605 }),
5606 };
5607
5608 ModulePath {
5609 name: mod_name,
5610 path_exists: default_exists || secondary_exists,
5611 result,
5612 }
5613 }
5614
5615 fn submod_path(&mut self,
5616 id: ast::Ident,
5617 outer_attrs: &[ast::Attribute],
5618 id_sp: Span)
5619 -> PResult<'a, ModulePathSuccess> {
5620 if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) {
5621 return Ok(ModulePathSuccess {
5622 directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
5623 Some("mod.rs") => DirectoryOwnership::Owned,
5624 _ => DirectoryOwnership::UnownedViaMod(true),
5625 },
5626 path,
5627 warn: false,
5628 });
5629 }
5630
5631 let paths = Parser::default_submod_path(id, &self.directory.path, self.sess.codemap());
5632
5633 if let DirectoryOwnership::UnownedViaBlock = self.directory.ownership {
5634 let msg =
5635 "Cannot declare a non-inline module inside a block unless it has a path attribute";
5636 let mut err = self.diagnostic().struct_span_err(id_sp, msg);
5637 if paths.path_exists {
5638 let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
5639 paths.name);
5640 err.span_note(id_sp, &msg);
5641 }
5642 Err(err)
5643 } else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
5644 if warn {
5645 if let Ok(result) = paths.result {
5646 return Ok(ModulePathSuccess { warn: true, ..result });
5647 }
5648 }
5649 let mut err = self.diagnostic().struct_span_err(id_sp,
5650 "cannot declare a new module at this location");
5651 if id_sp != syntax_pos::DUMMY_SP {
5652 let src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
5653 if let Some(stem) = src_path.file_stem() {
5654 let mut dest_path = src_path.clone();
5655 dest_path.set_file_name(stem);
5656 dest_path.push("mod.rs");
5657 err.span_note(id_sp,
5658 &format!("maybe move this module `{}` to its own \
5659 directory via `{}`", src_path.to_string_lossy(),
5660 dest_path.to_string_lossy()));
5661 }
5662 }
5663 if paths.path_exists {
5664 err.span_note(id_sp,
5665 &format!("... or maybe `use` the module `{}` instead \
5666 of possibly redeclaring it",
5667 paths.name));
5668 }
5669 Err(err)
5670 } else {
5671 paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
5672 }
5673 }
5674
5675 /// Read a module from a source file.
5676 fn eval_src_mod(&mut self,
5677 path: PathBuf,
5678 directory_ownership: DirectoryOwnership,
5679 name: String,
5680 id_sp: Span)
5681 -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
5682 let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5683 if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
5684 let mut err = String::from("circular modules: ");
5685 let len = included_mod_stack.len();
5686 for p in &included_mod_stack[i.. len] {
5687 err.push_str(&p.to_string_lossy());
5688 err.push_str(" -> ");
5689 }
5690 err.push_str(&path.to_string_lossy());
5691 return Err(self.span_fatal(id_sp, &err[..]));
5692 }
5693 included_mod_stack.push(path.clone());
5694 drop(included_mod_stack);
5695
5696 let mut p0 =
5697 new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp);
5698 p0.cfg_mods = self.cfg_mods;
5699 let mod_inner_lo = p0.span;
5700 let mod_attrs = p0.parse_inner_attributes()?;
5701 let m0 = p0.parse_mod_items(&token::Eof, mod_inner_lo)?;
5702 self.sess.included_mod_stack.borrow_mut().pop();
5703 Ok((ast::ItemKind::Mod(m0), mod_attrs))
5704 }
5705
5706 /// Parse a function declaration from a foreign module
5707 fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
5708 -> PResult<'a, ForeignItem> {
5709 self.expect_keyword(keywords::Fn)?;
5710
5711 let (ident, mut generics) = self.parse_fn_header()?;
5712 let decl = self.parse_fn_decl(true)?;
5713 generics.where_clause = self.parse_where_clause()?;
5714 let hi = self.span;
5715 self.expect(&token::Semi)?;
5716 Ok(ast::ForeignItem {
5717 ident,
5718 attrs,
5719 node: ForeignItemKind::Fn(decl, generics),
5720 id: ast::DUMMY_NODE_ID,
5721 span: lo.to(hi),
5722 vis,
5723 })
5724 }
5725
5726 /// Parse a static item from a foreign module.
5727 /// Assumes that the `static` keyword is already parsed.
5728 fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
5729 -> PResult<'a, ForeignItem> {
5730 let mutbl = self.eat_keyword(keywords::Mut);
5731 let ident = self.parse_ident()?;
5732 self.expect(&token::Colon)?;
5733 let ty = self.parse_ty()?;
5734 let hi = self.span;
5735 self.expect(&token::Semi)?;
5736 Ok(ForeignItem {
5737 ident,
5738 attrs,
5739 node: ForeignItemKind::Static(ty, mutbl),
5740 id: ast::DUMMY_NODE_ID,
5741 span: lo.to(hi),
5742 vis,
5743 })
5744 }
5745
5746 /// Parse a type from a foreign module
5747 fn parse_item_foreign_type(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
5748 -> PResult<'a, ForeignItem> {
5749 self.expect_keyword(keywords::Type)?;
5750
5751 let ident = self.parse_ident()?;
5752 let hi = self.span;
5753 self.expect(&token::Semi)?;
5754 Ok(ast::ForeignItem {
5755 ident: ident,
5756 attrs: attrs,
5757 node: ForeignItemKind::Ty,
5758 id: ast::DUMMY_NODE_ID,
5759 span: lo.to(hi),
5760 vis: vis
5761 })
5762 }
5763
5764 /// Parse extern crate links
5765 ///
5766 /// # Examples
5767 ///
5768 /// extern crate foo;
5769 /// extern crate bar as foo;
5770 fn parse_item_extern_crate(&mut self,
5771 lo: Span,
5772 visibility: Visibility,
5773 attrs: Vec<Attribute>)
5774 -> PResult<'a, P<Item>> {
5775
5776 let crate_name = self.parse_ident()?;
5777 let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
5778 (Some(crate_name.name), ident)
5779 } else {
5780 (None, crate_name)
5781 };
5782 self.expect(&token::Semi)?;
5783
5784 let prev_span = self.prev_span;
5785 Ok(self.mk_item(lo.to(prev_span),
5786 ident,
5787 ItemKind::ExternCrate(maybe_path),
5788 visibility,
5789 attrs))
5790 }
5791
5792 /// Parse `extern` for foreign ABIs
5793 /// modules.
5794 ///
5795 /// `extern` is expected to have been
5796 /// consumed before calling this method
5797 ///
5798 /// # Examples:
5799 ///
5800 /// extern "C" {}
5801 /// extern {}
5802 fn parse_item_foreign_mod(&mut self,
5803 lo: Span,
5804 opt_abi: Option<abi::Abi>,
5805 visibility: Visibility,
5806 mut attrs: Vec<Attribute>)
5807 -> PResult<'a, P<Item>> {
5808 self.expect(&token::OpenDelim(token::Brace))?;
5809
5810 let abi = opt_abi.unwrap_or(Abi::C);
5811
5812 attrs.extend(self.parse_inner_attributes()?);
5813
5814 let mut foreign_items = vec![];
5815 while let Some(item) = self.parse_foreign_item()? {
5816 foreign_items.push(item);
5817 }
5818 self.expect(&token::CloseDelim(token::Brace))?;
5819
5820 let prev_span = self.prev_span;
5821 let m = ast::ForeignMod {
5822 abi,
5823 items: foreign_items
5824 };
5825 let invalid = keywords::Invalid.ident();
5826 Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs))
5827 }
5828
5829 /// Parse type Foo = Bar;
5830 fn parse_item_type(&mut self) -> PResult<'a, ItemInfo> {
5831 let ident = self.parse_ident()?;
5832 let mut tps = self.parse_generics()?;
5833 tps.where_clause = self.parse_where_clause()?;
5834 self.expect(&token::Eq)?;
5835 let ty = self.parse_ty()?;
5836 self.expect(&token::Semi)?;
5837 Ok((ident, ItemKind::Ty(ty, tps), None))
5838 }
5839
5840 /// Parse the part of an "enum" decl following the '{'
5841 fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> {
5842 let mut variants = Vec::new();
5843 let mut all_nullary = true;
5844 let mut any_disr = None;
5845 while self.token != token::CloseDelim(token::Brace) {
5846 let variant_attrs = self.parse_outer_attributes()?;
5847 let vlo = self.span;
5848
5849 let struct_def;
5850 let mut disr_expr = None;
5851 let ident = self.parse_ident()?;
5852 if self.check(&token::OpenDelim(token::Brace)) {
5853 // Parse a struct variant.
5854 all_nullary = false;
5855 struct_def = VariantData::Struct(self.parse_record_struct_body()?,
5856 ast::DUMMY_NODE_ID);
5857 } else if self.check(&token::OpenDelim(token::Paren)) {
5858 all_nullary = false;
5859 struct_def = VariantData::Tuple(self.parse_tuple_struct_body()?,
5860 ast::DUMMY_NODE_ID);
5861 } else if self.eat(&token::Eq) {
5862 disr_expr = Some(self.parse_expr()?);
5863 any_disr = disr_expr.as_ref().map(|expr| expr.span);
5864 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5865 } else {
5866 struct_def = VariantData::Unit(ast::DUMMY_NODE_ID);
5867 }
5868
5869 let vr = ast::Variant_ {
5870 name: ident,
5871 attrs: variant_attrs,
5872 data: struct_def,
5873 disr_expr,
5874 };
5875 variants.push(respan(vlo.to(self.prev_span), vr));
5876
5877 if !self.eat(&token::Comma) { break; }
5878 }
5879 self.expect(&token::CloseDelim(token::Brace))?;
5880 match any_disr {
5881 Some(disr_span) if !all_nullary =>
5882 self.span_err(disr_span,
5883 "discriminator values can only be used with a c-like enum"),
5884 _ => ()
5885 }
5886
5887 Ok(ast::EnumDef { variants: variants })
5888 }
5889
5890 /// Parse an "enum" declaration
5891 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
5892 let id = self.parse_ident()?;
5893 let mut generics = self.parse_generics()?;
5894 generics.where_clause = self.parse_where_clause()?;
5895 self.expect(&token::OpenDelim(token::Brace))?;
5896
5897 let enum_definition = self.parse_enum_def(&generics).map_err(|e| {
5898 self.recover_stmt();
5899 self.eat(&token::CloseDelim(token::Brace));
5900 e
5901 })?;
5902 Ok((id, ItemKind::Enum(enum_definition, generics), None))
5903 }
5904
5905 /// Parses a string as an ABI spec on an extern type or module. Consumes
5906 /// the `extern` keyword, if one is found.
5907 fn parse_opt_abi(&mut self) -> PResult<'a, Option<abi::Abi>> {
5908 match self.token {
5909 token::Literal(token::Str_(s), suf) | token::Literal(token::StrRaw(s, _), suf) => {
5910 let sp = self.span;
5911 self.expect_no_suffix(sp, "ABI spec", suf);
5912 self.bump();
5913 match abi::lookup(&s.as_str()) {
5914 Some(abi) => Ok(Some(abi)),
5915 None => {
5916 let prev_span = self.prev_span;
5917 self.span_err(
5918 prev_span,
5919 &format!("invalid ABI: expected one of [{}], \
5920 found `{}`",
5921 abi::all_names().join(", "),
5922 s));
5923 Ok(None)
5924 }
5925 }
5926 }
5927
5928 _ => Ok(None),
5929 }
5930 }
5931
5932 /// Parse one of the items allowed by the flags.
5933 /// NB: this function no longer parses the items inside an
5934 /// extern crate.
5935 fn parse_item_(&mut self, attrs: Vec<Attribute>,
5936 macros_allowed: bool, attributes_allowed: bool) -> PResult<'a, Option<P<Item>>> {
5937 maybe_whole!(self, NtItem, |item| {
5938 let mut item = item.unwrap();
5939 let mut attrs = attrs;
5940 mem::swap(&mut item.attrs, &mut attrs);
5941 item.attrs.extend(attrs);
5942 Some(P(item))
5943 });
5944
5945 let lo = self.span;
5946
5947 let visibility = self.parse_visibility(false)?;
5948
5949 if self.eat_keyword(keywords::Use) {
5950 // USE ITEM
5951 let item_ = ItemKind::Use(self.parse_view_path()?);
5952 self.expect(&token::Semi)?;
5953
5954 let prev_span = self.prev_span;
5955 let invalid = keywords::Invalid.ident();
5956 let item = self.mk_item(lo.to(prev_span), invalid, item_, visibility, attrs);
5957 return Ok(Some(item));
5958 }
5959
5960 if self.eat_keyword(keywords::Extern) {
5961 if self.eat_keyword(keywords::Crate) {
5962 return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?));
5963 }
5964
5965 let opt_abi = self.parse_opt_abi()?;
5966
5967 if self.eat_keyword(keywords::Fn) {
5968 // EXTERN FUNCTION ITEM
5969 let fn_span = self.prev_span;
5970 let abi = opt_abi.unwrap_or(Abi::C);
5971 let (ident, item_, extra_attrs) =
5972 self.parse_item_fn(Unsafety::Normal,
5973 respan(fn_span, Constness::NotConst),
5974 abi)?;
5975 let prev_span = self.prev_span;
5976 let item = self.mk_item(lo.to(prev_span),
5977 ident,
5978 item_,
5979 visibility,
5980 maybe_append(attrs, extra_attrs));
5981 return Ok(Some(item));
5982 } else if self.check(&token::OpenDelim(token::Brace)) {
5983 return Ok(Some(self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs)?));
5984 }
5985
5986 self.unexpected()?;
5987 }
5988
5989 if self.eat_keyword(keywords::Static) {
5990 // STATIC ITEM
5991 let m = if self.eat_keyword(keywords::Mut) {
5992 Mutability::Mutable
5993 } else {
5994 Mutability::Immutable
5995 };
5996 let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?;
5997 let prev_span = self.prev_span;
5998 let item = self.mk_item(lo.to(prev_span),
5999 ident,
6000 item_,
6001 visibility,
6002 maybe_append(attrs, extra_attrs));
6003 return Ok(Some(item));
6004 }
6005 if self.eat_keyword(keywords::Const) {
6006 let const_span = self.prev_span;
6007 if self.check_keyword(keywords::Fn)
6008 || (self.check_keyword(keywords::Unsafe)
6009 && self.look_ahead(1, |t| t.is_keyword(keywords::Fn))) {
6010 // CONST FUNCTION ITEM
6011 let unsafety = if self.eat_keyword(keywords::Unsafe) {
6012 Unsafety::Unsafe
6013 } else {
6014 Unsafety::Normal
6015 };
6016 self.bump();
6017 let (ident, item_, extra_attrs) =
6018 self.parse_item_fn(unsafety,
6019 respan(const_span, Constness::Const),
6020 Abi::Rust)?;
6021 let prev_span = self.prev_span;
6022 let item = self.mk_item(lo.to(prev_span),
6023 ident,
6024 item_,
6025 visibility,
6026 maybe_append(attrs, extra_attrs));
6027 return Ok(Some(item));
6028 }
6029
6030 // CONST ITEM
6031 if self.eat_keyword(keywords::Mut) {
6032 let prev_span = self.prev_span;
6033 self.diagnostic().struct_span_err(prev_span, "const globals cannot be mutable")
6034 .help("did you mean to declare a static?")
6035 .emit();
6036 }
6037 let (ident, item_, extra_attrs) = self.parse_item_const(None)?;
6038 let prev_span = self.prev_span;
6039 let item = self.mk_item(lo.to(prev_span),
6040 ident,
6041 item_,
6042 visibility,
6043 maybe_append(attrs, extra_attrs));
6044 return Ok(Some(item));
6045 }
6046 if self.check_keyword(keywords::Unsafe) &&
6047 (self.look_ahead(1, |t| t.is_keyword(keywords::Trait)) ||
6048 self.look_ahead(1, |t| t.is_keyword(keywords::Auto)))
6049 {
6050 // UNSAFE TRAIT ITEM
6051 self.expect_keyword(keywords::Unsafe)?;
6052 let is_auto = if self.eat_keyword(keywords::Trait) {
6053 IsAuto::No
6054 } else {
6055 self.eat_auto_trait();
6056 IsAuto::Yes
6057 };
6058 let (ident, item_, extra_attrs) =
6059 self.parse_item_trait(is_auto, ast::Unsafety::Unsafe)?;
6060 let prev_span = self.prev_span;
6061 let item = self.mk_item(lo.to(prev_span),
6062 ident,
6063 item_,
6064 visibility,
6065 maybe_append(attrs, extra_attrs));
6066 return Ok(Some(item));
6067 }
6068 if (self.check_keyword(keywords::Unsafe) &&
6069 self.look_ahead(1, |t| t.is_keyword(keywords::Impl))) ||
6070 (self.check_keyword(keywords::Default) &&
6071 self.look_ahead(1, |t| t.is_keyword(keywords::Unsafe)) &&
6072 self.look_ahead(2, |t| t.is_keyword(keywords::Impl)))
6073 {
6074 // IMPL ITEM
6075 let defaultness = self.parse_defaultness()?;
6076 self.expect_keyword(keywords::Unsafe)?;
6077 self.expect_keyword(keywords::Impl)?;
6078 let (ident,
6079 item_,
6080 extra_attrs) = self.parse_item_impl(ast::Unsafety::Unsafe, defaultness)?;
6081 let prev_span = self.prev_span;
6082 let item = self.mk_item(lo.to(prev_span),
6083 ident,
6084 item_,
6085 visibility,
6086 maybe_append(attrs, extra_attrs));
6087 return Ok(Some(item));
6088 }
6089 if self.check_keyword(keywords::Fn) {
6090 // FUNCTION ITEM
6091 self.bump();
6092 let fn_span = self.prev_span;
6093 let (ident, item_, extra_attrs) =
6094 self.parse_item_fn(Unsafety::Normal,
6095 respan(fn_span, Constness::NotConst),
6096 Abi::Rust)?;
6097 let prev_span = self.prev_span;
6098 let item = self.mk_item(lo.to(prev_span),
6099 ident,
6100 item_,
6101 visibility,
6102 maybe_append(attrs, extra_attrs));
6103 return Ok(Some(item));
6104 }
6105 if self.check_keyword(keywords::Unsafe)
6106 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) {
6107 // UNSAFE FUNCTION ITEM
6108 self.bump();
6109 let abi = if self.eat_keyword(keywords::Extern) {
6110 self.parse_opt_abi()?.unwrap_or(Abi::C)
6111 } else {
6112 Abi::Rust
6113 };
6114 self.expect_keyword(keywords::Fn)?;
6115 let fn_span = self.prev_span;
6116 let (ident, item_, extra_attrs) =
6117 self.parse_item_fn(Unsafety::Unsafe,
6118 respan(fn_span, Constness::NotConst),
6119 abi)?;
6120 let prev_span = self.prev_span;
6121 let item = self.mk_item(lo.to(prev_span),
6122 ident,
6123 item_,
6124 visibility,
6125 maybe_append(attrs, extra_attrs));
6126 return Ok(Some(item));
6127 }
6128 if self.eat_keyword(keywords::Mod) {
6129 // MODULE ITEM
6130 let (ident, item_, extra_attrs) =
6131 self.parse_item_mod(&attrs[..])?;
6132 let prev_span = self.prev_span;
6133 let item = self.mk_item(lo.to(prev_span),
6134 ident,
6135 item_,
6136 visibility,
6137 maybe_append(attrs, extra_attrs));
6138 return Ok(Some(item));
6139 }
6140 if self.eat_keyword(keywords::Type) {
6141 // TYPE ITEM
6142 let (ident, item_, extra_attrs) = self.parse_item_type()?;
6143 let prev_span = self.prev_span;
6144 let item = self.mk_item(lo.to(prev_span),
6145 ident,
6146 item_,
6147 visibility,
6148 maybe_append(attrs, extra_attrs));
6149 return Ok(Some(item));
6150 }
6151 if self.eat_keyword(keywords::Enum) {
6152 // ENUM ITEM
6153 let (ident, item_, extra_attrs) = self.parse_item_enum()?;
6154 let prev_span = self.prev_span;
6155 let item = self.mk_item(lo.to(prev_span),
6156 ident,
6157 item_,
6158 visibility,
6159 maybe_append(attrs, extra_attrs));
6160 return Ok(Some(item));
6161 }
6162 if self.check_keyword(keywords::Trait)
6163 || (self.check_keyword(keywords::Auto)
6164 && self.look_ahead(1, |t| t.is_keyword(keywords::Trait)))
6165 {
6166 let is_auto = if self.eat_keyword(keywords::Trait) {
6167 IsAuto::No
6168 } else {
6169 self.eat_auto_trait();
6170 IsAuto::Yes
6171 };
6172 // TRAIT ITEM
6173 let (ident, item_, extra_attrs) =
6174 self.parse_item_trait(is_auto, ast::Unsafety::Normal)?;
6175 let prev_span = self.prev_span;
6176 let item = self.mk_item(lo.to(prev_span),
6177 ident,
6178 item_,
6179 visibility,
6180 maybe_append(attrs, extra_attrs));
6181 return Ok(Some(item));
6182 }
6183 if (self.check_keyword(keywords::Impl)) ||
6184 (self.check_keyword(keywords::Default) &&
6185 self.look_ahead(1, |t| t.is_keyword(keywords::Impl)))
6186 {
6187 // IMPL ITEM
6188 let defaultness = self.parse_defaultness()?;
6189 self.expect_keyword(keywords::Impl)?;
6190 let (ident,
6191 item_,
6192 extra_attrs) = self.parse_item_impl(ast::Unsafety::Normal, defaultness)?;
6193 let prev_span = self.prev_span;
6194 let item = self.mk_item(lo.to(prev_span),
6195 ident,
6196 item_,
6197 visibility,
6198 maybe_append(attrs, extra_attrs));
6199 return Ok(Some(item));
6200 }
6201 if self.eat_keyword(keywords::Struct) {
6202 // STRUCT ITEM
6203 let (ident, item_, extra_attrs) = self.parse_item_struct()?;
6204 let prev_span = self.prev_span;
6205 let item = self.mk_item(lo.to(prev_span),
6206 ident,
6207 item_,
6208 visibility,
6209 maybe_append(attrs, extra_attrs));
6210 return Ok(Some(item));
6211 }
6212 if self.is_union_item() {
6213 // UNION ITEM
6214 self.bump();
6215 let (ident, item_, extra_attrs) = self.parse_item_union()?;
6216 let prev_span = self.prev_span;
6217 let item = self.mk_item(lo.to(prev_span),
6218 ident,
6219 item_,
6220 visibility,
6221 maybe_append(attrs, extra_attrs));
6222 return Ok(Some(item));
6223 }
6224 if let Some(macro_def) = self.eat_macro_def(&attrs, &visibility, lo)? {
6225 return Ok(Some(macro_def));
6226 }
6227
6228 self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
6229 }
6230
6231 /// Parse a foreign item.
6232 fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
6233 let attrs = self.parse_outer_attributes()?;
6234 let lo = self.span;
6235 let visibility = self.parse_visibility(false)?;
6236
6237 // FOREIGN STATIC ITEM
6238 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
6239 if self.check_keyword(keywords::Static) || self.token.is_keyword(keywords::Const) {
6240 if self.token.is_keyword(keywords::Const) {
6241 self.diagnostic()
6242 .struct_span_err(self.span, "extern items cannot be `const`")
6243 .span_suggestion(self.span, "instead try using", "static".to_owned())
6244 .emit();
6245 }
6246 self.bump(); // `static` or `const`
6247 return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
6248 }
6249 // FOREIGN FUNCTION ITEM
6250 if self.check_keyword(keywords::Fn) {
6251 return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
6252 }
6253 // FOREIGN TYPE ITEM
6254 if self.check_keyword(keywords::Type) {
6255 return Ok(Some(self.parse_item_foreign_type(visibility, lo, attrs)?));
6256 }
6257
6258 // FIXME #5668: this will occur for a macro invocation:
6259 match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
6260 Some(item) => {
6261 return Err(self.span_fatal(item.span, "macros cannot expand to foreign items"));
6262 }
6263 None => Ok(None)
6264 }
6265 }
6266
6267 /// This is the fall-through for parsing items.
6268 fn parse_macro_use_or_failure(
6269 &mut self,
6270 attrs: Vec<Attribute> ,
6271 macros_allowed: bool,
6272 attributes_allowed: bool,
6273 lo: Span,
6274 visibility: Visibility
6275 ) -> PResult<'a, Option<P<Item>>> {
6276 if macros_allowed && self.token.is_path_start() {
6277 // MACRO INVOCATION ITEM
6278
6279 let prev_span = self.prev_span;
6280 self.complain_if_pub_macro(&visibility, prev_span);
6281
6282 let mac_lo = self.span;
6283
6284 // item macro.
6285 let pth = self.parse_path(PathStyle::Mod)?;
6286 self.expect(&token::Not)?;
6287
6288 // a 'special' identifier (like what `macro_rules!` uses)
6289 // is optional. We should eventually unify invoc syntax
6290 // and remove this.
6291 let id = if self.token.is_ident() {
6292 self.parse_ident()?
6293 } else {
6294 keywords::Invalid.ident() // no special identifier
6295 };
6296 // eat a matched-delimiter token tree:
6297 let (delim, tts) = self.expect_delimited_token_tree()?;
6298 if delim != token::Brace {
6299 if !self.eat(&token::Semi) {
6300 self.span_err(self.prev_span,
6301 "macros that expand to items must either \
6302 be surrounded with braces or followed by \
6303 a semicolon");
6304 }
6305 }
6306
6307 let hi = self.prev_span;
6308 let mac = respan(mac_lo.to(hi), Mac_ { path: pth, tts: tts });
6309 let item = self.mk_item(lo.to(hi), id, ItemKind::Mac(mac), visibility, attrs);
6310 return Ok(Some(item));
6311 }
6312
6313 // FAILURE TO PARSE ITEM
6314 match visibility {
6315 Visibility::Inherited => {}
6316 _ => {
6317 return Err(self.span_fatal(self.prev_span, "unmatched visibility `pub`"));
6318 }
6319 }
6320
6321 if !attributes_allowed && !attrs.is_empty() {
6322 self.expected_item_err(&attrs);
6323 }
6324 Ok(None)
6325 }
6326
6327 fn collect_tokens<F, R>(&mut self, f: F) -> PResult<'a, (R, TokenStream)>
6328 where F: FnOnce(&mut Self) -> PResult<'a, R>
6329 {
6330 // Record all tokens we parse when parsing this item.
6331 let mut tokens = Vec::new();
6332 match self.token_cursor.frame.last_token {
6333 LastToken::Collecting(_) => {
6334 panic!("cannot collect tokens recursively yet")
6335 }
6336 LastToken::Was(ref mut last) => tokens.extend(last.take()),
6337 }
6338 self.token_cursor.frame.last_token = LastToken::Collecting(tokens);
6339 let prev = self.token_cursor.stack.len();
6340 let ret = f(self);
6341 let last_token = if self.token_cursor.stack.len() == prev {
6342 &mut self.token_cursor.frame.last_token
6343 } else {
6344 &mut self.token_cursor.stack[prev].last_token
6345 };
6346 let mut tokens = match *last_token {
6347 LastToken::Collecting(ref mut v) => mem::replace(v, Vec::new()),
6348 LastToken::Was(_) => panic!("our vector went away?"),
6349 };
6350
6351 // If we're not at EOF our current token wasn't actually consumed by
6352 // `f`, but it'll still be in our list that we pulled out. In that case
6353 // put it back.
6354 if self.token == token::Eof {
6355 *last_token = LastToken::Was(None);
6356 } else {
6357 *last_token = LastToken::Was(tokens.pop());
6358 }
6359
6360 Ok((ret?, tokens.into_iter().collect()))
6361 }
6362
6363 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
6364 let attrs = self.parse_outer_attributes()?;
6365
6366 let (ret, tokens) = self.collect_tokens(|this| {
6367 this.parse_item_(attrs, true, false)
6368 })?;
6369
6370 // Once we've parsed an item and recorded the tokens we got while
6371 // parsing we may want to store `tokens` into the item we're about to
6372 // return. Note, though, that we specifically didn't capture tokens
6373 // related to outer attributes. The `tokens` field here may later be
6374 // used with procedural macros to convert this item back into a token
6375 // stream, but during expansion we may be removing attributes as we go
6376 // along.
6377 //
6378 // If we've got inner attributes then the `tokens` we've got above holds
6379 // these inner attributes. If an inner attribute is expanded we won't
6380 // actually remove it from the token stream, so we'll just keep yielding
6381 // it (bad!). To work around this case for now we just avoid recording
6382 // `tokens` if we detect any inner attributes. This should help keep
6383 // expansion correct, but we should fix this bug one day!
6384 Ok(ret.map(|item| {
6385 item.map(|mut i| {
6386 if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
6387 i.tokens = Some(tokens);
6388 }
6389 i
6390 })
6391 }))
6392 }
6393
6394 fn parse_path_list_items(&mut self) -> PResult<'a, Vec<ast::PathListItem>> {
6395 self.parse_unspanned_seq(&token::OpenDelim(token::Brace),
6396 &token::CloseDelim(token::Brace),
6397 SeqSep::trailing_allowed(token::Comma), |this| {
6398 let lo = this.span;
6399 let ident = if this.eat_keyword(keywords::SelfValue) {
6400 keywords::SelfValue.ident()
6401 } else {
6402 this.parse_ident()?
6403 };
6404 let rename = this.parse_rename()?;
6405 let node = ast::PathListItem_ {
6406 name: ident,
6407 rename,
6408 id: ast::DUMMY_NODE_ID
6409 };
6410 Ok(respan(lo.to(this.prev_span), node))
6411 })
6412 }
6413
6414 /// `::{` or `::*`
6415 fn is_import_coupler(&mut self) -> bool {
6416 self.check(&token::ModSep) &&
6417 self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
6418 *t == token::BinOp(token::Star))
6419 }
6420
6421 /// Matches ViewPath:
6422 /// MOD_SEP? non_global_path
6423 /// MOD_SEP? non_global_path as IDENT
6424 /// MOD_SEP? non_global_path MOD_SEP STAR
6425 /// MOD_SEP? non_global_path MOD_SEP LBRACE item_seq RBRACE
6426 /// MOD_SEP? LBRACE item_seq RBRACE
6427 fn parse_view_path(&mut self) -> PResult<'a, P<ViewPath>> {
6428 let lo = self.span;
6429 if self.check(&token::OpenDelim(token::Brace)) || self.check(&token::BinOp(token::Star)) ||
6430 self.is_import_coupler() {
6431 // `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`.
6432 self.eat(&token::ModSep);
6433 let prefix = ast::Path {
6434 segments: vec![PathSegment::crate_root(lo)],
6435 span: lo.to(self.span),
6436 };
6437 let view_path_kind = if self.eat(&token::BinOp(token::Star)) {
6438 ViewPathGlob(prefix)
6439 } else {
6440 ViewPathList(prefix, self.parse_path_list_items()?)
6441 };
6442 Ok(P(respan(lo.to(self.span), view_path_kind)))
6443 } else {
6444 let prefix = self.parse_path(PathStyle::Mod)?.default_to_global();
6445 if self.is_import_coupler() {
6446 // `foo::bar::{a, b}` or `foo::bar::*`
6447 self.bump();
6448 if self.check(&token::BinOp(token::Star)) {
6449 self.bump();
6450 Ok(P(respan(lo.to(self.span), ViewPathGlob(prefix))))
6451 } else {
6452 let items = self.parse_path_list_items()?;
6453 Ok(P(respan(lo.to(self.span), ViewPathList(prefix, items))))
6454 }
6455 } else {
6456 // `foo::bar` or `foo::bar as baz`
6457 let rename = self.parse_rename()?.
6458 unwrap_or(prefix.segments.last().unwrap().identifier);
6459 Ok(P(respan(lo.to(self.prev_span), ViewPathSimple(rename, prefix))))
6460 }
6461 }
6462 }
6463
6464 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
6465 if self.eat_keyword(keywords::As) {
6466 self.parse_ident().map(Some)
6467 } else {
6468 Ok(None)
6469 }
6470 }
6471
6472 /// Parses a source module as a crate. This is the main
6473 /// entry point for the parser.
6474 pub fn parse_crate_mod(&mut self) -> PResult<'a, Crate> {
6475 let lo = self.span;
6476 Ok(ast::Crate {
6477 attrs: self.parse_inner_attributes()?,
6478 module: self.parse_mod_items(&token::Eof, lo)?,
6479 span: lo.to(self.span),
6480 })
6481 }
6482
6483 pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
6484 let ret = match self.token {
6485 token::Literal(token::Str_(s), suf) => (s, ast::StrStyle::Cooked, suf),
6486 token::Literal(token::StrRaw(s, n), suf) => (s, ast::StrStyle::Raw(n), suf),
6487 _ => return None
6488 };
6489 self.bump();
6490 Some(ret)
6491 }
6492
6493 pub fn parse_str(&mut self) -> PResult<'a, (Symbol, StrStyle)> {
6494 match self.parse_optional_str() {
6495 Some((s, style, suf)) => {
6496 let sp = self.prev_span;
6497 self.expect_no_suffix(sp, "string literal", suf);
6498 Ok((s, style))
6499 }
6500 _ => Err(self.fatal("expected string literal"))
6501 }
6502 }
6503 }