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