rustc_parse/parser/
nonterminal.rs

1use rustc_ast::token::NtExprKind::*;
2use rustc_ast::token::NtPatKind::*;
3use rustc_ast::token::{self, InvisibleOrigin, MetaVarKind, NonterminalKind, Token};
4use rustc_errors::PResult;
5use rustc_span::{Ident, kw};
6
7use crate::errors::UnexpectedNonterminal;
8use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
9use crate::parser::{FollowedByType, ForceCollect, ParseNtResult, Parser, PathStyle};
10
11impl<'a> Parser<'a> {
12    /// Checks whether a non-terminal may begin with a particular token.
13    ///
14    /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with
15    /// that token. Be conservative (return true) if not sure. Inlined because it has a single call
16    /// site.
17    #[inline]
18    pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
19        /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
20        fn may_be_ident(kind: MetaVarKind) -> bool {
21            match kind {
22                MetaVarKind::Stmt
23                | MetaVarKind::Pat(_)
24                | MetaVarKind::Expr { .. }
25                | MetaVarKind::Ty { .. }
26                | MetaVarKind::Literal // `true`, `false`
27                | MetaVarKind::Meta { .. }
28                | MetaVarKind::Path => true,
29
30                MetaVarKind::Item
31                | MetaVarKind::Block
32                | MetaVarKind::Vis => false,
33
34                MetaVarKind::Ident
35                | MetaVarKind::Lifetime
36                | MetaVarKind::TT => unreachable!(),
37            }
38        }
39
40        match kind {
41            // `expr_2021` and earlier
42            NonterminalKind::Expr(Expr2021 { .. }) => {
43                token.can_begin_expr()
44                // This exception is here for backwards compatibility.
45                && !token.is_keyword(kw::Let)
46                // This exception is here for backwards compatibility.
47                && !token.is_keyword(kw::Const)
48            }
49            // Current edition expressions
50            NonterminalKind::Expr(Expr) => {
51                // In Edition 2024, `_` is considered an expression, so we
52                // need to allow it here because `token.can_begin_expr()` does
53                // not consider `_` to be an expression.
54                //
55                // Because `can_begin_expr` is used elsewhere, we need to reduce
56                // the scope of where the `_` is considered an expression to
57                // just macro parsing code.
58                (token.can_begin_expr() || token.is_keyword(kw::Underscore))
59                // This exception is here for backwards compatibility.
60                && !token.is_keyword(kw::Let)
61            }
62            NonterminalKind::Ty => token.can_begin_type(),
63            NonterminalKind::Ident => get_macro_ident(token).is_some(),
64            NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
65            NonterminalKind::Vis => match token.kind {
66                // The follow-set of :vis + "priv" keyword + interpolated/metavar-expansion.
67                token::Comma
68                | token::Ident(..)
69                | token::NtIdent(..)
70                | token::NtLifetime(..)
71                | token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => true,
72                _ => token.can_begin_type(),
73            },
74            NonterminalKind::Block => match &token.kind {
75                token::OpenBrace => true,
76                token::NtLifetime(..) => true,
77                token::OpenInvisible(InvisibleOrigin::MetaVar(k)) => match k {
78                    MetaVarKind::Block
79                    | MetaVarKind::Stmt
80                    | MetaVarKind::Expr { .. }
81                    | MetaVarKind::Literal => true,
82                    MetaVarKind::Item
83                    | MetaVarKind::Pat(_)
84                    | MetaVarKind::Ty { .. }
85                    | MetaVarKind::Meta { .. }
86                    | MetaVarKind::Path
87                    | MetaVarKind::Vis => false,
88                    MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
89                        unreachable!()
90                    }
91                },
92                _ => false,
93            },
94            NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
95                token::PathSep | token::Ident(..) | token::NtIdent(..) => true,
96                token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => may_be_ident(*kind),
97                _ => false,
98            },
99            NonterminalKind::Pat(pat_kind) => token.can_begin_pattern(pat_kind),
100            NonterminalKind::Lifetime => match &token.kind {
101                token::Lifetime(..) | token::NtLifetime(..) => true,
102                _ => false,
103            },
104            NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
105                token.kind.close_delim().is_none()
106            }
107        }
108    }
109
110    /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). Inlined because there is only one call
111    /// site.
112    #[inline]
113    pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
114        // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
115        // which requires having captured tokens available. Since we cannot determine
116        // in advance whether or not a proc-macro will be (transitively) invoked,
117        // we always capture tokens for any nonterminal that needs them.
118        match kind {
119            // Note that TT is treated differently to all the others.
120            NonterminalKind::TT => Ok(ParseNtResult::Tt(self.parse_token_tree())),
121            NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
122                Some(item) => Ok(ParseNtResult::Item(item)),
123                None => Err(self.dcx().create_err(UnexpectedNonterminal::Item(self.token.span))),
124            },
125            NonterminalKind::Block => {
126                // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
127                // the ':block' matcher does not support them
128                Ok(ParseNtResult::Block(self.collect_tokens_no_attrs(|this| this.parse_block())?))
129            }
130            NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
131                Some(stmt) => Ok(ParseNtResult::Stmt(Box::new(stmt))),
132                None => {
133                    Err(self.dcx().create_err(UnexpectedNonterminal::Statement(self.token.span)))
134                }
135            },
136            NonterminalKind::Pat(pat_kind) => Ok(ParseNtResult::Pat(
137                self.collect_tokens_no_attrs(|this| match pat_kind {
138                    PatParam { .. } => this.parse_pat_no_top_alt(None, None),
139                    PatWithOr => this.parse_pat_no_top_guard(
140                        None,
141                        RecoverComma::No,
142                        RecoverColon::No,
143                        CommaRecoveryMode::EitherTupleOrPipe,
144                    ),
145                })?,
146                pat_kind,
147            )),
148            NonterminalKind::Expr(expr_kind) => {
149                Ok(ParseNtResult::Expr(self.parse_expr_force_collect()?, expr_kind))
150            }
151            NonterminalKind::Literal => {
152                // The `:literal` matcher does not support attributes.
153                Ok(ParseNtResult::Literal(
154                    self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
155                ))
156            }
157            NonterminalKind::Ty => Ok(ParseNtResult::Ty(
158                self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
159            )),
160            // This could be handled like a token, since it is one.
161            NonterminalKind::Ident => {
162                if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
163                    self.bump();
164                    Ok(ParseNtResult::Ident(ident, is_raw))
165                } else {
166                    Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
167                        span: self.token.span,
168                        token: self.token,
169                    }))
170                }
171            }
172            NonterminalKind::Path => Ok(ParseNtResult::Path(Box::new(
173                self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
174            ))),
175            NonterminalKind::Meta => {
176                Ok(ParseNtResult::Meta(Box::new(self.parse_attr_item(ForceCollect::Yes)?)))
177            }
178            NonterminalKind::Vis => Ok(ParseNtResult::Vis(Box::new(
179                self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
180            ))),
181            NonterminalKind::Lifetime => {
182                // We want to keep `'keyword` parsing, just like `keyword` is still
183                // an ident for nonterminal purposes.
184                if let Some((ident, is_raw)) = self.token.lifetime() {
185                    self.bump();
186                    Ok(ParseNtResult::Lifetime(ident, is_raw))
187                } else {
188                    Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
189                        span: self.token.span,
190                        token: self.token,
191                    }))
192                }
193            }
194        }
195    }
196}
197
198/// The token is an identifier, but not `_`.
199/// We prohibit passing `_` to macros expecting `ident` for now.
200fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
201    token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
202}