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