Skip to main content

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