]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/mod.rs
Make clear that `TokenTree::Token` shouldn't contain a delimiter.
[rust.git] / compiler / rustc_parse / src / parser / mod.rs
1 pub mod attr;
2 mod attr_wrapper;
3 mod diagnostics;
4 mod expr;
5 mod generics;
6 mod item;
7 mod nonterminal;
8 mod pat;
9 mod path;
10 mod stmt;
11 mod ty;
12
13 use crate::lexer::UnmatchedBrace;
14 pub use attr_wrapper::AttrWrapper;
15 pub use diagnostics::AttemptLocalParseRecovery;
16 pub(crate) use item::FnParseMode;
17 pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
18 pub use path::PathStyle;
19
20 use rustc_ast::ptr::P;
21 use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind};
22 use rustc_ast::tokenstream::AttributesData;
23 use rustc_ast::tokenstream::{self, DelimSpan, Spacing};
24 use rustc_ast::tokenstream::{TokenStream, TokenTree};
25 use rustc_ast::util::case::Case;
26 use rustc_ast::AttrId;
27 use rustc_ast::DUMMY_NODE_ID;
28 use rustc_ast::{self as ast, AnonConst, AttrStyle, Const, DelimArgs, Extern};
29 use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, MacDelimiter, Mutability, StrLit};
30 use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind};
31 use rustc_ast_pretty::pprust;
32 use rustc_data_structures::fx::FxHashMap;
33 use rustc_errors::PResult;
34 use rustc_errors::{
35     Applicability, DiagnosticBuilder, ErrorGuaranteed, FatalError, IntoDiagnostic, MultiSpan,
36 };
37 use rustc_session::parse::ParseSess;
38 use rustc_span::source_map::{Span, DUMMY_SP};
39 use rustc_span::symbol::{kw, sym, Ident, Symbol};
40
41 use std::ops::Range;
42 use std::{cmp, mem, slice};
43
44 use crate::errors::{
45     DocCommentDoesNotDocumentAnything, IncorrectVisibilityRestriction, MismatchedClosingDelimiter,
46     NonStringAbiLiteral,
47 };
48
49 bitflags::bitflags! {
50     struct Restrictions: u8 {
51         const STMT_EXPR         = 1 << 0;
52         const NO_STRUCT_LITERAL = 1 << 1;
53         const CONST_EXPR        = 1 << 2;
54         const ALLOW_LET         = 1 << 3;
55     }
56 }
57
58 #[derive(Clone, Copy, PartialEq, Debug)]
59 enum SemiColonMode {
60     Break,
61     Ignore,
62     Comma,
63 }
64
65 #[derive(Clone, Copy, PartialEq, Debug)]
66 enum BlockMode {
67     Break,
68     Ignore,
69 }
70
71 /// Whether or not we should force collection of tokens for an AST node,
72 /// regardless of whether or not it has attributes
73 #[derive(Clone, Copy, PartialEq)]
74 pub enum ForceCollect {
75     Yes,
76     No,
77 }
78
79 #[derive(Debug, Eq, PartialEq)]
80 pub enum TrailingToken {
81     None,
82     Semi,
83     Gt,
84     /// If the trailing token is a comma, then capture it
85     /// Otherwise, ignore the trailing token
86     MaybeComma,
87 }
88
89 /// Like `maybe_whole_expr`, but for things other than expressions.
90 #[macro_export]
91 macro_rules! maybe_whole {
92     ($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
93         if let token::Interpolated(nt) = &$p.token.kind {
94             if let token::$constructor(x) = &**nt {
95                 let $x = x.clone();
96                 $p.bump();
97                 return Ok($e);
98             }
99         }
100     };
101 }
102
103 /// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
104 #[macro_export]
105 macro_rules! maybe_recover_from_interpolated_ty_qpath {
106     ($self: expr, $allow_qpath_recovery: expr) => {
107         if $allow_qpath_recovery
108                     && $self.may_recover()
109                     && $self.look_ahead(1, |t| t == &token::ModSep)
110                     && let token::Interpolated(nt) = &$self.token.kind
111                     && let token::NtTy(ty) = &**nt
112                 {
113                     let ty = ty.clone();
114                     $self.bump();
115                     return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
116                 }
117     };
118 }
119
120 #[derive(Clone, Copy)]
121 pub enum Recovery {
122     Allowed,
123     Forbidden,
124 }
125
126 #[derive(Clone)]
127 pub struct Parser<'a> {
128     pub sess: &'a ParseSess,
129     /// The current token.
130     pub token: Token,
131     /// The spacing for the current token
132     pub token_spacing: Spacing,
133     /// The previous token.
134     pub prev_token: Token,
135     pub capture_cfg: bool,
136     restrictions: Restrictions,
137     expected_tokens: Vec<TokenType>,
138     // Important: This must only be advanced from `bump` to ensure that
139     // `token_cursor.num_next_calls` is updated properly.
140     token_cursor: TokenCursor,
141     desugar_doc_comments: bool,
142     /// This field is used to keep track of how many left angle brackets we have seen. This is
143     /// required in order to detect extra leading left angle brackets (`<` characters) and error
144     /// appropriately.
145     ///
146     /// See the comments in the `parse_path_segment` function for more details.
147     unmatched_angle_bracket_count: u32,
148     max_angle_bracket_count: u32,
149     /// A list of all unclosed delimiters found by the lexer. If an entry is used for error recovery
150     /// it gets removed from here. Every entry left at the end gets emitted as an independent
151     /// error.
152     pub(super) unclosed_delims: Vec<UnmatchedBrace>,
153     last_unexpected_token_span: Option<Span>,
154     /// Span pointing at the `:` for the last type ascription the parser has seen, and whether it
155     /// looked like it could have been a mistyped path or literal `Option:Some(42)`).
156     pub last_type_ascription: Option<(Span, bool /* likely path typo */)>,
157     /// If present, this `Parser` is not parsing Rust code but rather a macro call.
158     subparser_name: Option<&'static str>,
159     capture_state: CaptureState,
160     /// This allows us to recover when the user forget to add braces around
161     /// multiple statements in the closure body.
162     pub current_closure: Option<ClosureSpans>,
163     /// Whether the parser is allowed to do recovery.
164     /// This is disabled when parsing macro arguments, see #103534
165     pub recovery: Recovery,
166 }
167
168 // This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
169 // it doesn't unintentionally get bigger.
170 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
171 rustc_data_structures::static_assert_size!(Parser<'_>, 336);
172
173 /// Stores span information about a closure.
174 #[derive(Clone)]
175 pub struct ClosureSpans {
176     pub whole_closure: Span,
177     pub closing_pipe: Span,
178     pub body: Span,
179 }
180
181 /// Indicates a range of tokens that should be replaced by
182 /// the tokens in the provided vector. This is used in two
183 /// places during token collection:
184 ///
185 /// 1. During the parsing of an AST node that may have a `#[derive]`
186 /// attribute, we parse a nested AST node that has `#[cfg]` or `#[cfg_attr]`
187 /// In this case, we use a `ReplaceRange` to replace the entire inner AST node
188 /// with `FlatToken::AttrTarget`, allowing us to perform eager cfg-expansion
189 /// on an `AttrTokenStream`.
190 ///
191 /// 2. When we parse an inner attribute while collecting tokens. We
192 /// remove inner attributes from the token stream entirely, and
193 /// instead track them through the `attrs` field on the AST node.
194 /// This allows us to easily manipulate them (for example, removing
195 /// the first macro inner attribute to invoke a proc-macro).
196 /// When create a `TokenStream`, the inner attributes get inserted
197 /// into the proper place in the token stream.
198 pub type ReplaceRange = (Range<u32>, Vec<(FlatToken, Spacing)>);
199
200 /// Controls how we capture tokens. Capturing can be expensive,
201 /// so we try to avoid performing capturing in cases where
202 /// we will never need an `AttrTokenStream`.
203 #[derive(Copy, Clone)]
204 pub enum Capturing {
205     /// We aren't performing any capturing - this is the default mode.
206     No,
207     /// We are capturing tokens
208     Yes,
209 }
210
211 #[derive(Clone)]
212 struct CaptureState {
213     capturing: Capturing,
214     replace_ranges: Vec<ReplaceRange>,
215     inner_attr_ranges: FxHashMap<AttrId, ReplaceRange>,
216 }
217
218 impl<'a> Drop for Parser<'a> {
219     fn drop(&mut self) {
220         emit_unclosed_delims(&mut self.unclosed_delims, &self.sess);
221     }
222 }
223
224 #[derive(Clone)]
225 struct TokenCursor {
226     // The current (innermost) frame. `frame` and `stack` could be combined,
227     // but it's faster to keep them separate and access `frame` directly
228     // rather than via something like `stack.last().unwrap()` or
229     // `stack[stack.len() - 1]`.
230     frame: TokenCursorFrame,
231     // Additional frames that enclose `frame`.
232     stack: Vec<TokenCursorFrame>,
233     desugar_doc_comments: bool,
234     // Counts the number of calls to `{,inlined_}next`.
235     num_next_calls: usize,
236     // During parsing, we may sometimes need to 'unglue' a
237     // glued token into two component tokens
238     // (e.g. '>>' into '>' and '>), so that the parser
239     // can consume them one at a time. This process
240     // bypasses the normal capturing mechanism
241     // (e.g. `num_next_calls` will not be incremented),
242     // since the 'unglued' tokens due not exist in
243     // the original `TokenStream`.
244     //
245     // If we end up consuming both unglued tokens,
246     // then this is not an issue - we'll end up
247     // capturing the single 'glued' token.
248     //
249     // However, in certain circumstances, we may
250     // want to capture just the first 'unglued' token.
251     // For example, capturing the `Vec<u8>`
252     // in `Option<Vec<u8>>` requires us to unglue
253     // the trailing `>>` token. The `break_last_token`
254     // field is used to track this token - it gets
255     // appended to the captured stream when
256     // we evaluate a `LazyAttrTokenStream`.
257     break_last_token: bool,
258 }
259
260 #[derive(Clone)]
261 struct TokenCursorFrame {
262     // This is `None` only for the outermost frame.
263     delim_sp: Option<(Delimiter, DelimSpan)>,
264     tree_cursor: tokenstream::Cursor,
265 }
266
267 impl TokenCursorFrame {
268     fn new(delim_sp: Option<(Delimiter, DelimSpan)>, tts: TokenStream) -> Self {
269         TokenCursorFrame { delim_sp, tree_cursor: tts.into_trees() }
270     }
271 }
272
273 impl TokenCursor {
274     fn next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
275         self.inlined_next(desugar_doc_comments)
276     }
277
278     /// This always-inlined version should only be used on hot code paths.
279     #[inline(always)]
280     fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
281         loop {
282             // FIXME: we currently don't return `Delimiter` open/close delims. To fix #67062 we will
283             // need to, whereupon the `delim != Delimiter::Invisible` conditions below can be
284             // removed.
285             if let Some(tree) = self.frame.tree_cursor.next_ref() {
286                 match tree {
287                     &TokenTree::Token(ref token, spacing) => match (desugar_doc_comments, token) {
288                         (true, &Token { kind: token::DocComment(_, attr_style, data), span }) => {
289                             let desugared = self.desugar(attr_style, data, span);
290                             self.frame.tree_cursor.replace_prev_and_rewind(desugared);
291                             // Continue to get the first token of the desugared doc comment.
292                         }
293                         _ => {
294                             debug_assert!(!matches!(
295                                 token.kind,
296                                 token::OpenDelim(_) | token::CloseDelim(_)
297                             ));
298                             return (token.clone(), spacing);
299                         }
300                     },
301                     &TokenTree::Delimited(sp, delim, ref tts) => {
302                         // Set `open_delim` to true here because we deal with it immediately.
303                         let frame = TokenCursorFrame::new(Some((delim, sp)), tts.clone());
304                         self.stack.push(mem::replace(&mut self.frame, frame));
305                         if delim != Delimiter::Invisible {
306                             return (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone);
307                         }
308                         // No open delimiter to return; continue on to the next iteration.
309                     }
310                 };
311             } else if let Some(frame) = self.stack.pop() {
312                 // We have exhausted this frame. Move back to its parent frame.
313                 let (delim, span) = self.frame.delim_sp.unwrap();
314                 self.frame = frame;
315                 if delim != Delimiter::Invisible {
316                     return (Token::new(token::CloseDelim(delim), span.close), Spacing::Alone);
317                 }
318                 // No close delimiter to return; continue on to the next iteration.
319             } else {
320                 // We have exhausted the outermost frame.
321                 return (Token::new(token::Eof, DUMMY_SP), Spacing::Alone);
322             }
323         }
324     }
325
326     // Desugar a doc comment into something like `#[doc = r"foo"]`.
327     fn desugar(&mut self, attr_style: AttrStyle, data: Symbol, span: Span) -> Vec<TokenTree> {
328         // Searches for the occurrences of `"#*` and returns the minimum number of `#`s
329         // required to wrap the text. E.g.
330         // - `abc d` is wrapped as `r"abc d"` (num_of_hashes = 0)
331         // - `abc "d"` is wrapped as `r#"abc "d""#` (num_of_hashes = 1)
332         // - `abc "##d##"` is wrapped as `r###"abc ##"d"##"###` (num_of_hashes = 3)
333         let mut num_of_hashes = 0;
334         let mut count = 0;
335         for ch in data.as_str().chars() {
336             count = match ch {
337                 '"' => 1,
338                 '#' if count > 0 => count + 1,
339                 _ => 0,
340             };
341             num_of_hashes = cmp::max(num_of_hashes, count);
342         }
343
344         // `/// foo` becomes `doc = r"foo".
345         let delim_span = DelimSpan::from_single(span);
346         let body = TokenTree::Delimited(
347             delim_span,
348             Delimiter::Bracket,
349             [
350                 TokenTree::token_alone(token::Ident(sym::doc, false), span),
351                 TokenTree::token_alone(token::Eq, span),
352                 TokenTree::token_alone(
353                     TokenKind::lit(token::StrRaw(num_of_hashes), data, None),
354                     span,
355                 ),
356             ]
357             .into_iter()
358             .collect::<TokenStream>(),
359         );
360
361         if attr_style == AttrStyle::Inner {
362             vec![
363                 TokenTree::token_alone(token::Pound, span),
364                 TokenTree::token_alone(token::Not, span),
365                 body,
366             ]
367         } else {
368             vec![TokenTree::token_alone(token::Pound, span), body]
369         }
370     }
371 }
372
373 #[derive(Debug, Clone, PartialEq)]
374 enum TokenType {
375     Token(TokenKind),
376     Keyword(Symbol),
377     Operator,
378     Lifetime,
379     Ident,
380     Path,
381     Type,
382     Const,
383 }
384
385 impl TokenType {
386     fn to_string(&self) -> String {
387         match self {
388             TokenType::Token(t) => format!("`{}`", pprust::token_kind_to_string(t)),
389             TokenType::Keyword(kw) => format!("`{}`", kw),
390             TokenType::Operator => "an operator".to_string(),
391             TokenType::Lifetime => "lifetime".to_string(),
392             TokenType::Ident => "identifier".to_string(),
393             TokenType::Path => "path".to_string(),
394             TokenType::Type => "type".to_string(),
395             TokenType::Const => "a const expression".to_string(),
396         }
397     }
398 }
399
400 #[derive(Copy, Clone, Debug)]
401 enum TokenExpectType {
402     Expect,
403     NoExpect,
404 }
405
406 /// A sequence separator.
407 struct SeqSep {
408     /// The separator token.
409     sep: Option<TokenKind>,
410     /// `true` if a trailing separator is allowed.
411     trailing_sep_allowed: bool,
412 }
413
414 impl SeqSep {
415     fn trailing_allowed(t: TokenKind) -> SeqSep {
416         SeqSep { sep: Some(t), trailing_sep_allowed: true }
417     }
418
419     fn none() -> SeqSep {
420         SeqSep { sep: None, trailing_sep_allowed: false }
421     }
422 }
423
424 pub enum FollowedByType {
425     Yes,
426     No,
427 }
428
429 #[derive(Clone, Copy, PartialEq, Eq)]
430 pub enum TokenDescription {
431     ReservedIdentifier,
432     Keyword,
433     ReservedKeyword,
434     DocComment,
435 }
436
437 impl TokenDescription {
438     pub fn from_token(token: &Token) -> Option<Self> {
439         match token.kind {
440             _ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
441             _ if token.is_used_keyword() => Some(TokenDescription::Keyword),
442             _ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
443             token::DocComment(..) => Some(TokenDescription::DocComment),
444             _ => None,
445         }
446     }
447 }
448
449 pub(super) fn token_descr(token: &Token) -> String {
450     let name = pprust::token_to_string(token).to_string();
451
452     let kind = TokenDescription::from_token(token).map(|kind| match kind {
453         TokenDescription::ReservedIdentifier => "reserved identifier",
454         TokenDescription::Keyword => "keyword",
455         TokenDescription::ReservedKeyword => "reserved keyword",
456         TokenDescription::DocComment => "doc comment",
457     });
458
459     if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) }
460 }
461
462 impl<'a> Parser<'a> {
463     pub fn new(
464         sess: &'a ParseSess,
465         tokens: TokenStream,
466         desugar_doc_comments: bool,
467         subparser_name: Option<&'static str>,
468     ) -> Self {
469         let mut parser = Parser {
470             sess,
471             token: Token::dummy(),
472             token_spacing: Spacing::Alone,
473             prev_token: Token::dummy(),
474             capture_cfg: false,
475             restrictions: Restrictions::empty(),
476             expected_tokens: Vec::new(),
477             token_cursor: TokenCursor {
478                 frame: TokenCursorFrame::new(None, tokens),
479                 stack: Vec::new(),
480                 num_next_calls: 0,
481                 desugar_doc_comments,
482                 break_last_token: false,
483             },
484             desugar_doc_comments,
485             unmatched_angle_bracket_count: 0,
486             max_angle_bracket_count: 0,
487             unclosed_delims: Vec::new(),
488             last_unexpected_token_span: None,
489             last_type_ascription: None,
490             subparser_name,
491             capture_state: CaptureState {
492                 capturing: Capturing::No,
493                 replace_ranges: Vec::new(),
494                 inner_attr_ranges: Default::default(),
495             },
496             current_closure: None,
497             recovery: Recovery::Allowed,
498         };
499
500         // Make parser point to the first token.
501         parser.bump();
502
503         parser
504     }
505
506     pub fn recovery(mut self, recovery: Recovery) -> Self {
507         self.recovery = recovery;
508         self
509     }
510
511     /// Whether the parser is allowed to recover from broken code.
512     ///
513     /// If this returns false, recovering broken code into valid code (especially if this recovery does lookahead)
514     /// is not allowed. All recovery done by the parser must be gated behind this check.
515     ///
516     /// Technically, this only needs to restrict eager recovery by doing lookahead at more tokens.
517     /// But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.
518     fn may_recover(&self) -> bool {
519         matches!(self.recovery, Recovery::Allowed)
520     }
521
522     pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
523         match self.expect_one_of(&[], &[]) {
524             Err(e) => Err(e),
525             // We can get `Ok(true)` from `recover_closing_delimiter`
526             // which is called in `expected_one_of_not_found`.
527             Ok(_) => FatalError.raise(),
528         }
529     }
530
531     /// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
532     pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, bool /* recovered */> {
533         if self.expected_tokens.is_empty() {
534             if self.token == *t {
535                 self.bump();
536                 Ok(false)
537             } else {
538                 self.unexpected_try_recover(t)
539             }
540         } else {
541             self.expect_one_of(slice::from_ref(t), &[])
542         }
543     }
544
545     /// Expect next token to be edible or inedible token. If edible,
546     /// then consume it; if inedible, then return without consuming
547     /// anything. Signal a fatal error if next token is unexpected.
548     pub fn expect_one_of(
549         &mut self,
550         edible: &[TokenKind],
551         inedible: &[TokenKind],
552     ) -> PResult<'a, bool /* recovered */> {
553         if edible.contains(&self.token.kind) {
554             self.bump();
555             Ok(false)
556         } else if inedible.contains(&self.token.kind) {
557             // leave it in the input
558             Ok(false)
559         } else if self.last_unexpected_token_span == Some(self.token.span) {
560             FatalError.raise();
561         } else {
562             self.expected_one_of_not_found(edible, inedible)
563         }
564     }
565
566     // Public for rustfmt usage.
567     pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
568         self.parse_ident_common(true)
569     }
570
571     fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
572         self.token.ident().ok_or_else(|| match self.prev_token.kind {
573             TokenKind::DocComment(..) => DocCommentDoesNotDocumentAnything {
574                 span: self.prev_token.span,
575                 missing_comma: None,
576             }
577             .into_diagnostic(&self.sess.span_diagnostic),
578             _ => self.expected_ident_found(),
579         })
580     }
581
582     fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
583         let (ident, is_raw) = self.ident_or_err()?;
584         if !is_raw && ident.is_reserved() {
585             let mut err = self.expected_ident_found();
586             if recover {
587                 err.emit();
588             } else {
589                 return Err(err);
590             }
591         }
592         self.bump();
593         Ok(ident)
594     }
595
596     /// Checks if the next token is `tok`, and returns `true` if so.
597     ///
598     /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
599     /// encountered.
600     fn check(&mut self, tok: &TokenKind) -> bool {
601         let is_present = self.token == *tok;
602         if !is_present {
603             self.expected_tokens.push(TokenType::Token(tok.clone()));
604         }
605         is_present
606     }
607
608     fn check_noexpect(&self, tok: &TokenKind) -> bool {
609         self.token == *tok
610     }
611
612     /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
613     ///
614     /// the main purpose of this function is to reduce the cluttering of the suggestions list
615     /// which using the normal eat method could introduce in some cases.
616     pub fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
617         let is_present = self.check_noexpect(tok);
618         if is_present {
619             self.bump()
620         }
621         is_present
622     }
623
624     /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
625     pub fn eat(&mut self, tok: &TokenKind) -> bool {
626         let is_present = self.check(tok);
627         if is_present {
628             self.bump()
629         }
630         is_present
631     }
632
633     /// If the next token is the given keyword, returns `true` without eating it.
634     /// An expectation is also added for diagnostics purposes.
635     fn check_keyword(&mut self, kw: Symbol) -> bool {
636         self.expected_tokens.push(TokenType::Keyword(kw));
637         self.token.is_keyword(kw)
638     }
639
640     fn check_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
641         if self.check_keyword(kw) {
642             return true;
643         }
644
645         if case == Case::Insensitive
646         && let Some((ident, /* is_raw */ false)) = self.token.ident()
647         && ident.as_str().to_lowercase() == kw.as_str().to_lowercase() {
648             true
649         } else {
650             false
651         }
652     }
653
654     /// If the next token is the given keyword, eats it and returns `true`.
655     /// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
656     // Public for rustfmt usage.
657     pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
658         if self.check_keyword(kw) {
659             self.bump();
660             true
661         } else {
662             false
663         }
664     }
665
666     /// Eats a keyword, optionally ignoring the case.
667     /// If the case differs (and is ignored) an error is issued.
668     /// This is useful for recovery.
669     fn eat_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
670         if self.eat_keyword(kw) {
671             return true;
672         }
673
674         if case == Case::Insensitive
675         && let Some((ident, /* is_raw */ false)) = self.token.ident()
676         && ident.as_str().to_lowercase() == kw.as_str().to_lowercase() {
677             self
678                 .struct_span_err(ident.span, format!("keyword `{kw}` is written in a wrong case"))
679                 .span_suggestion(
680                     ident.span,
681                     "write it in the correct case",
682                     kw,
683                     Applicability::MachineApplicable
684                 ).emit();
685
686             self.bump();
687             return true;
688         }
689
690         false
691     }
692
693     fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
694         if self.token.is_keyword(kw) {
695             self.bump();
696             true
697         } else {
698             false
699         }
700     }
701
702     /// If the given word is not a keyword, signals an error.
703     /// If the next token is not the given word, signals an error.
704     /// Otherwise, eats it.
705     fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
706         if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
707     }
708
709     /// Is the given keyword `kw` followed by a non-reserved identifier?
710     fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
711         self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
712     }
713
714     fn check_or_expected(&mut self, ok: bool, typ: TokenType) -> bool {
715         if ok {
716             true
717         } else {
718             self.expected_tokens.push(typ);
719             false
720         }
721     }
722
723     fn check_ident(&mut self) -> bool {
724         self.check_or_expected(self.token.is_ident(), TokenType::Ident)
725     }
726
727     fn check_path(&mut self) -> bool {
728         self.check_or_expected(self.token.is_path_start(), TokenType::Path)
729     }
730
731     fn check_type(&mut self) -> bool {
732         self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
733     }
734
735     fn check_const_arg(&mut self) -> bool {
736         self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
737     }
738
739     fn check_const_closure(&self) -> bool {
740         self.is_keyword_ahead(0, &[kw::Const])
741             && self.look_ahead(1, |t| match &t.kind {
742                 token::Ident(kw::Move | kw::Static | kw::Async, _)
743                 | token::OrOr
744                 | token::BinOp(token::Or) => true,
745                 _ => false,
746             })
747     }
748
749     fn check_inline_const(&self, dist: usize) -> bool {
750         self.is_keyword_ahead(dist, &[kw::Const])
751             && self.look_ahead(dist + 1, |t| match &t.kind {
752                 token::Interpolated(nt) => matches!(**nt, token::NtBlock(..)),
753                 token::OpenDelim(Delimiter::Brace) => true,
754                 _ => false,
755             })
756     }
757
758     /// Checks to see if the next token is either `+` or `+=`.
759     /// Otherwise returns `false`.
760     fn check_plus(&mut self) -> bool {
761         self.check_or_expected(
762             self.token.is_like_plus(),
763             TokenType::Token(token::BinOp(token::Plus)),
764         )
765     }
766
767     /// Eats the expected token if it's present possibly breaking
768     /// compound tokens like multi-character operators in process.
769     /// Returns `true` if the token was eaten.
770     fn break_and_eat(&mut self, expected: TokenKind) -> bool {
771         if self.token.kind == expected {
772             self.bump();
773             return true;
774         }
775         match self.token.kind.break_two_token_op() {
776             Some((first, second)) if first == expected => {
777                 let first_span = self.sess.source_map().start_point(self.token.span);
778                 let second_span = self.token.span.with_lo(first_span.hi());
779                 self.token = Token::new(first, first_span);
780                 // Keep track of this token - if we end token capturing now,
781                 // we'll want to append this token to the captured stream.
782                 //
783                 // If we consume any additional tokens, then this token
784                 // is not needed (we'll capture the entire 'glued' token),
785                 // and `bump` will set this field to `None`
786                 self.token_cursor.break_last_token = true;
787                 // Use the spacing of the glued token as the spacing
788                 // of the unglued second token.
789                 self.bump_with((Token::new(second, second_span), self.token_spacing));
790                 true
791             }
792             _ => {
793                 self.expected_tokens.push(TokenType::Token(expected));
794                 false
795             }
796         }
797     }
798
799     /// Eats `+` possibly breaking tokens like `+=` in process.
800     fn eat_plus(&mut self) -> bool {
801         self.break_and_eat(token::BinOp(token::Plus))
802     }
803
804     /// Eats `&` possibly breaking tokens like `&&` in process.
805     /// Signals an error if `&` is not eaten.
806     fn expect_and(&mut self) -> PResult<'a, ()> {
807         if self.break_and_eat(token::BinOp(token::And)) { Ok(()) } else { self.unexpected() }
808     }
809
810     /// Eats `|` possibly breaking tokens like `||` in process.
811     /// Signals an error if `|` was not eaten.
812     fn expect_or(&mut self) -> PResult<'a, ()> {
813         if self.break_and_eat(token::BinOp(token::Or)) { Ok(()) } else { self.unexpected() }
814     }
815
816     /// Eats `<` possibly breaking tokens like `<<` in process.
817     fn eat_lt(&mut self) -> bool {
818         let ate = self.break_and_eat(token::Lt);
819         if ate {
820             // See doc comment for `unmatched_angle_bracket_count`.
821             self.unmatched_angle_bracket_count += 1;
822             self.max_angle_bracket_count += 1;
823             debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
824         }
825         ate
826     }
827
828     /// Eats `<` possibly breaking tokens like `<<` in process.
829     /// Signals an error if `<` was not eaten.
830     fn expect_lt(&mut self) -> PResult<'a, ()> {
831         if self.eat_lt() { Ok(()) } else { self.unexpected() }
832     }
833
834     /// Eats `>` possibly breaking tokens like `>>` in process.
835     /// Signals an error if `>` was not eaten.
836     fn expect_gt(&mut self) -> PResult<'a, ()> {
837         if self.break_and_eat(token::Gt) {
838             // See doc comment for `unmatched_angle_bracket_count`.
839             if self.unmatched_angle_bracket_count > 0 {
840                 self.unmatched_angle_bracket_count -= 1;
841                 debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
842             }
843             Ok(())
844         } else {
845             self.unexpected()
846         }
847     }
848
849     fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
850         kets.iter().any(|k| match expect {
851             TokenExpectType::Expect => self.check(k),
852             TokenExpectType::NoExpect => self.token == **k,
853         })
854     }
855
856     fn parse_seq_to_before_tokens<T>(
857         &mut self,
858         kets: &[&TokenKind],
859         sep: SeqSep,
860         expect: TokenExpectType,
861         mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
862     ) -> PResult<'a, (Vec<T>, bool /* trailing */, bool /* recovered */)> {
863         let mut first = true;
864         let mut recovered = false;
865         let mut trailing = false;
866         let mut v = vec![];
867         let unclosed_delims = !self.unclosed_delims.is_empty();
868
869         while !self.expect_any_with_type(kets, expect) {
870             if let token::CloseDelim(..) | token::Eof = self.token.kind {
871                 break;
872             }
873             if let Some(t) = &sep.sep {
874                 if first {
875                     first = false;
876                 } else {
877                     match self.expect(t) {
878                         Ok(false) => {
879                             self.current_closure.take();
880                         }
881                         Ok(true) => {
882                             self.current_closure.take();
883                             recovered = true;
884                             break;
885                         }
886                         Err(mut expect_err) => {
887                             let sp = self.prev_token.span.shrink_to_hi();
888                             let token_str = pprust::token_kind_to_string(t);
889
890                             match self.current_closure.take() {
891                                 Some(closure_spans) if self.token.kind == TokenKind::Semi => {
892                                     // Finding a semicolon instead of a comma
893                                     // after a closure body indicates that the
894                                     // closure body may be a block but the user
895                                     // forgot to put braces around its
896                                     // statements.
897
898                                     self.recover_missing_braces_around_closure_body(
899                                         closure_spans,
900                                         expect_err,
901                                     )?;
902
903                                     continue;
904                                 }
905
906                                 _ => {
907                                     // Attempt to keep parsing if it was a similar separator.
908                                     if let Some(tokens) = t.similar_tokens() {
909                                         if tokens.contains(&self.token.kind) && !unclosed_delims {
910                                             self.bump();
911                                         }
912                                     }
913                                 }
914                             }
915
916                             // If this was a missing `@` in a binding pattern
917                             // bail with a suggestion
918                             // https://github.com/rust-lang/rust/issues/72373
919                             if self.prev_token.is_ident() && self.token.kind == token::DotDot {
920                                 let msg = format!(
921                                     "if you meant to bind the contents of \
922                                     the rest of the array pattern into `{}`, use `@`",
923                                     pprust::token_to_string(&self.prev_token)
924                                 );
925                                 expect_err
926                                     .span_suggestion_verbose(
927                                         self.prev_token.span.shrink_to_hi().until(self.token.span),
928                                         &msg,
929                                         " @ ",
930                                         Applicability::MaybeIncorrect,
931                                     )
932                                     .emit();
933                                 break;
934                             }
935
936                             // Attempt to keep parsing if it was an omitted separator.
937                             match f(self) {
938                                 Ok(t) => {
939                                     // Parsed successfully, therefore most probably the code only
940                                     // misses a separator.
941                                     expect_err
942                                         .span_suggestion_short(
943                                             sp,
944                                             &format!("missing `{}`", token_str),
945                                             token_str,
946                                             Applicability::MaybeIncorrect,
947                                         )
948                                         .emit();
949
950                                     v.push(t);
951                                     continue;
952                                 }
953                                 Err(e) => {
954                                     // Parsing failed, therefore it must be something more serious
955                                     // than just a missing separator.
956                                     for xx in &e.children {
957                                         // propagate the help message from sub error 'e' to main error 'expect_err;
958                                         expect_err.children.push(xx.clone());
959                                     }
960                                     expect_err.emit();
961
962                                     e.cancel();
963                                     break;
964                                 }
965                             }
966                         }
967                     }
968                 }
969             }
970             if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
971                 trailing = true;
972                 break;
973             }
974
975             let t = f(self)?;
976             v.push(t);
977         }
978
979         Ok((v, trailing, recovered))
980     }
981
982     fn recover_missing_braces_around_closure_body(
983         &mut self,
984         closure_spans: ClosureSpans,
985         mut expect_err: DiagnosticBuilder<'_, ErrorGuaranteed>,
986     ) -> PResult<'a, ()> {
987         let initial_semicolon = self.token.span;
988
989         while self.eat(&TokenKind::Semi) {
990             let _ = self.parse_stmt(ForceCollect::Yes)?;
991         }
992
993         expect_err.set_primary_message(
994             "closure bodies that contain statements must be surrounded by braces",
995         );
996
997         let preceding_pipe_span = closure_spans.closing_pipe;
998         let following_token_span = self.token.span;
999
1000         let mut first_note = MultiSpan::from(vec![initial_semicolon]);
1001         first_note.push_span_label(
1002             initial_semicolon,
1003             "this `;` turns the preceding closure into a statement",
1004         );
1005         first_note.push_span_label(
1006             closure_spans.body,
1007             "this expression is a statement because of the trailing semicolon",
1008         );
1009         expect_err.span_note(first_note, "statement found outside of a block");
1010
1011         let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
1012         second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
1013         second_note.push_span_label(
1014             following_token_span,
1015             "...but likely you meant the closure to end here",
1016         );
1017         expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
1018
1019         expect_err.set_span(vec![preceding_pipe_span, following_token_span]);
1020
1021         let opening_suggestion_str = " {".to_string();
1022         let closing_suggestion_str = "}".to_string();
1023
1024         expect_err.multipart_suggestion(
1025             "try adding braces",
1026             vec![
1027                 (preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
1028                 (following_token_span.shrink_to_lo(), closing_suggestion_str),
1029             ],
1030             Applicability::MaybeIncorrect,
1031         );
1032
1033         expect_err.emit();
1034
1035         Ok(())
1036     }
1037
1038     /// Parses a sequence, not including the closing delimiter. The function
1039     /// `f` must consume tokens until reaching the next separator or
1040     /// closing bracket.
1041     fn parse_seq_to_before_end<T>(
1042         &mut self,
1043         ket: &TokenKind,
1044         sep: SeqSep,
1045         f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1046     ) -> PResult<'a, (Vec<T>, bool, bool)> {
1047         self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
1048     }
1049
1050     /// Parses a sequence, including the closing delimiter. The function
1051     /// `f` must consume tokens until reaching the next separator or
1052     /// closing bracket.
1053     fn parse_seq_to_end<T>(
1054         &mut self,
1055         ket: &TokenKind,
1056         sep: SeqSep,
1057         f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1058     ) -> PResult<'a, (Vec<T>, bool /* trailing */)> {
1059         let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
1060         if !recovered {
1061             self.eat(ket);
1062         }
1063         Ok((val, trailing))
1064     }
1065
1066     /// Parses a sequence, including the closing delimiter. The function
1067     /// `f` must consume tokens until reaching the next separator or
1068     /// closing bracket.
1069     fn parse_unspanned_seq<T>(
1070         &mut self,
1071         bra: &TokenKind,
1072         ket: &TokenKind,
1073         sep: SeqSep,
1074         f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1075     ) -> PResult<'a, (Vec<T>, bool)> {
1076         self.expect(bra)?;
1077         self.parse_seq_to_end(ket, sep, f)
1078     }
1079
1080     fn parse_delim_comma_seq<T>(
1081         &mut self,
1082         delim: Delimiter,
1083         f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1084     ) -> PResult<'a, (Vec<T>, bool)> {
1085         self.parse_unspanned_seq(
1086             &token::OpenDelim(delim),
1087             &token::CloseDelim(delim),
1088             SeqSep::trailing_allowed(token::Comma),
1089             f,
1090         )
1091     }
1092
1093     fn parse_paren_comma_seq<T>(
1094         &mut self,
1095         f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1096     ) -> PResult<'a, (Vec<T>, bool)> {
1097         self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
1098     }
1099
1100     /// Advance the parser by one token using provided token as the next one.
1101     fn bump_with(&mut self, next: (Token, Spacing)) {
1102         self.inlined_bump_with(next)
1103     }
1104
1105     /// This always-inlined version should only be used on hot code paths.
1106     #[inline(always)]
1107     fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
1108         // Update the current and previous tokens.
1109         self.prev_token = mem::replace(&mut self.token, next_token);
1110         self.token_spacing = next_spacing;
1111
1112         // Diagnostics.
1113         self.expected_tokens.clear();
1114     }
1115
1116     /// Advance the parser by one token.
1117     pub fn bump(&mut self) {
1118         // Note: destructuring here would give nicer code, but it was found in #96210 to be slower
1119         // than `.0`/`.1` access.
1120         let mut next = self.token_cursor.inlined_next(self.desugar_doc_comments);
1121         self.token_cursor.num_next_calls += 1;
1122         // We've retrieved an token from the underlying
1123         // cursor, so we no longer need to worry about
1124         // an unglued token. See `break_and_eat` for more details
1125         self.token_cursor.break_last_token = false;
1126         if next.0.span.is_dummy() {
1127             // Tweak the location for better diagnostics, but keep syntactic context intact.
1128             let fallback_span = self.token.span;
1129             next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
1130         }
1131         debug_assert!(!matches!(
1132             next.0.kind,
1133             token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
1134         ));
1135         self.inlined_bump_with(next)
1136     }
1137
1138     /// Look-ahead `dist` tokens of `self.token` and get access to that token there.
1139     /// When `dist == 0` then the current token is looked at.
1140     pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
1141         if dist == 0 {
1142             return looker(&self.token);
1143         }
1144
1145         let frame = &self.token_cursor.frame;
1146         if let Some((delim, span)) = frame.delim_sp && delim != Delimiter::Invisible {
1147             let all_normal = (0..dist).all(|i| {
1148                 let token = frame.tree_cursor.look_ahead(i);
1149                 !matches!(token, Some(TokenTree::Delimited(_, Delimiter::Invisible, _)))
1150             });
1151             if all_normal {
1152                 return match frame.tree_cursor.look_ahead(dist - 1) {
1153                     Some(tree) => match tree {
1154                         TokenTree::Token(token, _) => looker(token),
1155                         TokenTree::Delimited(dspan, delim, _) => {
1156                             looker(&Token::new(token::OpenDelim(*delim), dspan.open))
1157                         }
1158                     },
1159                     None => looker(&Token::new(token::CloseDelim(delim), span.close)),
1160                 };
1161             }
1162         }
1163
1164         let mut cursor = self.token_cursor.clone();
1165         let mut i = 0;
1166         let mut token = Token::dummy();
1167         while i < dist {
1168             token = cursor.next(/* desugar_doc_comments */ false).0;
1169             if matches!(
1170                 token.kind,
1171                 token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
1172             ) {
1173                 continue;
1174             }
1175             i += 1;
1176         }
1177         return looker(&token);
1178     }
1179
1180     /// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
1181     fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1182         self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
1183     }
1184
1185     /// Parses asyncness: `async` or nothing.
1186     fn parse_asyncness(&mut self, case: Case) -> Async {
1187         if self.eat_keyword_case(kw::Async, case) {
1188             let span = self.prev_token.uninterpolated_span();
1189             Async::Yes { span, closure_id: DUMMY_NODE_ID, return_impl_trait_id: DUMMY_NODE_ID }
1190         } else {
1191             Async::No
1192         }
1193     }
1194
1195     /// Parses unsafety: `unsafe` or nothing.
1196     fn parse_unsafety(&mut self, case: Case) -> Unsafe {
1197         if self.eat_keyword_case(kw::Unsafe, case) {
1198             Unsafe::Yes(self.prev_token.uninterpolated_span())
1199         } else {
1200             Unsafe::No
1201         }
1202     }
1203
1204     /// Parses constness: `const` or nothing.
1205     fn parse_constness(&mut self, case: Case) -> Const {
1206         // Avoid const blocks to be parsed as const items
1207         if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
1208             && self.eat_keyword_case(kw::Const, case)
1209         {
1210             Const::Yes(self.prev_token.uninterpolated_span())
1211         } else {
1212             Const::No
1213         }
1214     }
1215
1216     /// Parses inline const expressions.
1217     fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
1218         if pat {
1219             self.sess.gated_spans.gate(sym::inline_const_pat, span);
1220         } else {
1221             self.sess.gated_spans.gate(sym::inline_const, span);
1222         }
1223         self.eat_keyword(kw::Const);
1224         let (attrs, blk) = self.parse_inner_attrs_and_block()?;
1225         let anon_const = AnonConst {
1226             id: DUMMY_NODE_ID,
1227             value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
1228         };
1229         let blk_span = anon_const.value.span;
1230         Ok(self.mk_expr_with_attrs(span.to(blk_span), ExprKind::ConstBlock(anon_const), attrs))
1231     }
1232
1233     /// Parses mutability (`mut` or nothing).
1234     fn parse_mutability(&mut self) -> Mutability {
1235         if self.eat_keyword(kw::Mut) { Mutability::Mut } else { Mutability::Not }
1236     }
1237
1238     /// Possibly parses mutability (`const` or `mut`).
1239     fn parse_const_or_mut(&mut self) -> Option<Mutability> {
1240         if self.eat_keyword(kw::Mut) {
1241             Some(Mutability::Mut)
1242         } else if self.eat_keyword(kw::Const) {
1243             Some(Mutability::Not)
1244         } else {
1245             None
1246         }
1247     }
1248
1249     fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1250         if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
1251         {
1252             if let Some(suffix) = suffix {
1253                 self.expect_no_tuple_index_suffix(self.token.span, suffix);
1254             }
1255             self.bump();
1256             Ok(Ident::new(symbol, self.prev_token.span))
1257         } else {
1258             self.parse_ident_common(true)
1259         }
1260     }
1261
1262     fn parse_delim_args(&mut self) -> PResult<'a, P<DelimArgs>> {
1263         if let Some(args) = self.parse_delim_args_inner() { Ok(P(args)) } else { self.unexpected() }
1264     }
1265
1266     fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
1267         Ok(if let Some(args) = self.parse_delim_args_inner() {
1268             AttrArgs::Delimited(args)
1269         } else {
1270             if self.eat(&token::Eq) {
1271                 let eq_span = self.prev_token.span;
1272                 AttrArgs::Eq(eq_span, AttrArgsEq::Ast(self.parse_expr_force_collect()?))
1273             } else {
1274                 AttrArgs::Empty
1275             }
1276         })
1277     }
1278
1279     fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
1280         if self.check(&token::OpenDelim(Delimiter::Parenthesis))
1281             || self.check(&token::OpenDelim(Delimiter::Bracket))
1282             || self.check(&token::OpenDelim(Delimiter::Brace))
1283         {
1284             match self.parse_token_tree() {
1285                 // We've confirmed above that there is a delimiter so unwrapping is OK.
1286                 TokenTree::Delimited(dspan, delim, tokens) => Some(DelimArgs {
1287                     dspan,
1288                     delim: MacDelimiter::from_token(delim).unwrap(),
1289                     tokens,
1290                 }),
1291                 _ => unreachable!(),
1292             }
1293         } else {
1294             None
1295         }
1296     }
1297
1298     fn parse_or_use_outer_attributes(
1299         &mut self,
1300         already_parsed_attrs: Option<AttrWrapper>,
1301     ) -> PResult<'a, AttrWrapper> {
1302         if let Some(attrs) = already_parsed_attrs {
1303             Ok(attrs)
1304         } else {
1305             self.parse_outer_attributes()
1306         }
1307     }
1308
1309     /// Parses a single token tree from the input.
1310     pub(crate) fn parse_token_tree(&mut self) -> TokenTree {
1311         match self.token.kind {
1312             token::OpenDelim(..) => {
1313                 // Grab the tokens from this frame.
1314                 let frame = &self.token_cursor.frame;
1315                 let stream = frame.tree_cursor.stream.clone();
1316                 let (delim, span) = frame.delim_sp.unwrap();
1317
1318                 // Advance the token cursor through the entire delimited
1319                 // sequence. After getting the `OpenDelim` we are *within* the
1320                 // delimited sequence, i.e. at depth `d`. After getting the
1321                 // matching `CloseDelim` we are *after* the delimited sequence,
1322                 // i.e. at depth `d - 1`.
1323                 let target_depth = self.token_cursor.stack.len() - 1;
1324                 loop {
1325                     // Advance one token at a time, so `TokenCursor::next()`
1326                     // can capture these tokens if necessary.
1327                     self.bump();
1328                     if self.token_cursor.stack.len() == target_depth {
1329                         debug_assert!(matches!(self.token.kind, token::CloseDelim(_)));
1330                         break;
1331                     }
1332                 }
1333
1334                 // Consume close delimiter
1335                 self.bump();
1336                 TokenTree::Delimited(span, delim, stream)
1337             }
1338             token::CloseDelim(_) | token::Eof => unreachable!(),
1339             _ => {
1340                 self.bump();
1341                 TokenTree::Token(self.prev_token.clone(), Spacing::Alone)
1342             }
1343         }
1344     }
1345
1346     /// Parses a stream of tokens into a list of `TokenTree`s, up to EOF.
1347     pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
1348         let mut tts = Vec::new();
1349         while self.token != token::Eof {
1350             tts.push(self.parse_token_tree());
1351         }
1352         Ok(tts)
1353     }
1354
1355     pub fn parse_tokens(&mut self) -> TokenStream {
1356         let mut result = Vec::new();
1357         loop {
1358             match self.token.kind {
1359                 token::Eof | token::CloseDelim(..) => break,
1360                 _ => result.push(self.parse_token_tree()),
1361             }
1362         }
1363         TokenStream::new(result)
1364     }
1365
1366     /// Evaluates the closure with restrictions in place.
1367     ///
1368     /// Afters the closure is evaluated, restrictions are reset.
1369     fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
1370         let old = self.restrictions;
1371         self.restrictions = res;
1372         let res = f(self);
1373         self.restrictions = old;
1374         res
1375     }
1376
1377     /// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
1378     /// for `pub(in self)` and `pub(super)` for `pub(in super)`.
1379     /// If the following element can't be a tuple (i.e., it's a function definition), then
1380     /// it's not a tuple struct field), and the contents within the parentheses aren't valid,
1381     /// so emit a proper diagnostic.
1382     // Public for rustfmt usage.
1383     pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1384         maybe_whole!(self, NtVis, |x| x.into_inner());
1385
1386         if !self.eat_keyword(kw::Pub) {
1387             // We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
1388             // keyword to grab a span from for inherited visibility; an empty span at the
1389             // beginning of the current token would seem to be the "Schelling span".
1390             return Ok(Visibility {
1391                 span: self.token.span.shrink_to_lo(),
1392                 kind: VisibilityKind::Inherited,
1393                 tokens: None,
1394             });
1395         }
1396         let lo = self.prev_token.span;
1397
1398         if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
1399             // We don't `self.bump()` the `(` yet because this might be a struct definition where
1400             // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
1401             // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
1402             // by the following tokens.
1403             if self.is_keyword_ahead(1, &[kw::In]) {
1404                 // Parse `pub(in path)`.
1405                 self.bump(); // `(`
1406                 self.bump(); // `in`
1407                 let path = self.parse_path(PathStyle::Mod)?; // `path`
1408                 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1409                 let vis = VisibilityKind::Restricted {
1410                     path: P(path),
1411                     id: ast::DUMMY_NODE_ID,
1412                     shorthand: false,
1413                 };
1414                 return Ok(Visibility {
1415                     span: lo.to(self.prev_token.span),
1416                     kind: vis,
1417                     tokens: None,
1418                 });
1419             } else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis))
1420                 && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
1421             {
1422                 // Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
1423                 self.bump(); // `(`
1424                 let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
1425                 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1426                 let vis = VisibilityKind::Restricted {
1427                     path: P(path),
1428                     id: ast::DUMMY_NODE_ID,
1429                     shorthand: true,
1430                 };
1431                 return Ok(Visibility {
1432                     span: lo.to(self.prev_token.span),
1433                     kind: vis,
1434                     tokens: None,
1435                 });
1436             } else if let FollowedByType::No = fbt {
1437                 // Provide this diagnostic if a type cannot follow;
1438                 // in particular, if this is not a tuple struct.
1439                 self.recover_incorrect_vis_restriction()?;
1440                 // Emit diagnostic, but continue with public visibility.
1441             }
1442         }
1443
1444         Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
1445     }
1446
1447     /// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
1448     fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
1449         self.bump(); // `(`
1450         let path = self.parse_path(PathStyle::Mod)?;
1451         self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1452
1453         let path_str = pprust::path_to_string(&path);
1454         self.sess.emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
1455
1456         Ok(())
1457     }
1458
1459     /// Parses `extern string_literal?`.
1460     fn parse_extern(&mut self, case: Case) -> Extern {
1461         if self.eat_keyword_case(kw::Extern, case) {
1462             let mut extern_span = self.prev_token.span;
1463             let abi = self.parse_abi();
1464             if let Some(abi) = abi {
1465                 extern_span = extern_span.to(abi.span);
1466             }
1467             Extern::from_abi(abi, extern_span)
1468         } else {
1469             Extern::None
1470         }
1471     }
1472
1473     /// Parses a string literal as an ABI spec.
1474     fn parse_abi(&mut self) -> Option<StrLit> {
1475         match self.parse_str_lit() {
1476             Ok(str_lit) => Some(str_lit),
1477             Err(Some(lit)) => match lit.kind {
1478                 ast::LitKind::Err => None,
1479                 _ => {
1480                     self.sess.emit_err(NonStringAbiLiteral { span: lit.span });
1481                     None
1482                 }
1483             },
1484             Err(None) => None,
1485         }
1486     }
1487
1488     pub fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
1489         &mut self,
1490         f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1491     ) -> PResult<'a, R> {
1492         // The only reason to call `collect_tokens_no_attrs` is if you want tokens, so use
1493         // `ForceCollect::Yes`
1494         self.collect_tokens_trailing_token(
1495             AttrWrapper::empty(),
1496             ForceCollect::Yes,
1497             |this, _attrs| Ok((f(this)?, TrailingToken::None)),
1498         )
1499     }
1500
1501     /// `::{` or `::*`
1502     fn is_import_coupler(&mut self) -> bool {
1503         self.check(&token::ModSep)
1504             && self.look_ahead(1, |t| {
1505                 *t == token::OpenDelim(Delimiter::Brace) || *t == token::BinOp(token::Star)
1506             })
1507     }
1508
1509     pub fn clear_expected_tokens(&mut self) {
1510         self.expected_tokens.clear();
1511     }
1512
1513     pub fn approx_token_stream_pos(&self) -> usize {
1514         self.token_cursor.num_next_calls
1515     }
1516 }
1517
1518 pub(crate) fn make_unclosed_delims_error(
1519     unmatched: UnmatchedBrace,
1520     sess: &ParseSess,
1521 ) -> Option<DiagnosticBuilder<'_, ErrorGuaranteed>> {
1522     // `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
1523     // `unmatched_braces` only for error recovery in the `Parser`.
1524     let found_delim = unmatched.found_delim?;
1525     let mut spans = vec![unmatched.found_span];
1526     if let Some(sp) = unmatched.unclosed_span {
1527         spans.push(sp);
1528     };
1529     let err = MismatchedClosingDelimiter {
1530         spans,
1531         delimiter: pprust::token_kind_to_string(&token::CloseDelim(found_delim)).to_string(),
1532         unmatched: unmatched.found_span,
1533         opening_candidate: unmatched.candidate_span,
1534         unclosed: unmatched.unclosed_span,
1535     }
1536     .into_diagnostic(&sess.span_diagnostic);
1537     Some(err)
1538 }
1539
1540 pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, sess: &ParseSess) {
1541     *sess.reached_eof.borrow_mut() |=
1542         unclosed_delims.iter().any(|unmatched_delim| unmatched_delim.found_delim.is_none());
1543     for unmatched in unclosed_delims.drain(..) {
1544         if let Some(mut e) = make_unclosed_delims_error(unmatched, sess) {
1545             e.emit();
1546         }
1547     }
1548 }
1549
1550 /// A helper struct used when building an `AttrTokenStream` from
1551 /// a `LazyAttrTokenStream`. Both delimiter and non-delimited tokens
1552 /// are stored as `FlatToken::Token`. A vector of `FlatToken`s
1553 /// is then 'parsed' to build up an `AttrTokenStream` with nested
1554 /// `AttrTokenTree::Delimited` tokens.
1555 #[derive(Debug, Clone)]
1556 pub enum FlatToken {
1557     /// A token - this holds both delimiter (e.g. '{' and '}')
1558     /// and non-delimiter tokens
1559     Token(Token),
1560     /// Holds the `AttributesData` for an AST node. The
1561     /// `AttributesData` is inserted directly into the
1562     /// constructed `AttrTokenStream` as
1563     /// an `AttrTokenTree::Attributes`.
1564     AttrTarget(AttributesData),
1565     /// A special 'empty' token that is ignored during the conversion
1566     /// to an `AttrTokenStream`. This is used to simplify the
1567     /// handling of replace ranges.
1568     Empty,
1569 }
1570
1571 #[derive(Debug)]
1572 pub enum NtOrTt {
1573     Nt(Nonterminal),
1574     Tt(TokenTree),
1575 }