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
35                | MetaVarKind::Guard => false,
36
37                MetaVarKind::Ident
38                | MetaVarKind::Lifetime
39                | 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 => token.is_keyword(kw::If),
109            NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
110                token.kind.close_delim().is_none()
111            }
112        }
113    }
114
115    /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). Inlined because there is only one call
116    /// site.
117    #[inline]
118    pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
119        // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
120        // which requires having captured tokens available. Since we cannot determine
121        // in advance whether or not a proc-macro will be (transitively) invoked,
122        // we always capture tokens for any nonterminal that needs them.
123        match kind {
124            // Note that TT is treated differently to all the others.
125            NonterminalKind::TT => Ok(ParseNtResult::Tt(self.parse_token_tree())),
126            NonterminalKind::Item => match self
127                .parse_item(ForceCollect::Yes, AllowConstBlockItems::Yes)?
128            {
129                Some(item) => Ok(ParseNtResult::Item(item)),
130                None => Err(self.dcx().create_err(UnexpectedNonterminal::Item(self.token.span))),
131            },
132            NonterminalKind::Block => {
133                // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
134                // the ':block' matcher does not support them
135                Ok(ParseNtResult::Block(self.collect_tokens_no_attrs(|this| this.parse_block())?))
136            }
137            NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
138                Some(stmt) => Ok(ParseNtResult::Stmt(Box::new(stmt))),
139                None => {
140                    Err(self.dcx().create_err(UnexpectedNonterminal::Statement(self.token.span)))
141                }
142            },
143            NonterminalKind::Pat(pat_kind) => Ok(ParseNtResult::Pat(
144                self.collect_tokens_no_attrs(|this| match pat_kind {
145                    PatParam { .. } => this.parse_pat_no_top_alt(None, None),
146                    PatWithOr => this.parse_pat_no_top_guard(
147                        None,
148                        RecoverComma::No,
149                        RecoverColon::No,
150                        CommaRecoveryMode::EitherTupleOrPipe,
151                    ),
152                })
153                .map(Box::new)?,
154                pat_kind,
155            )),
156            NonterminalKind::Expr(expr_kind) => {
157                Ok(ParseNtResult::Expr(self.parse_expr_force_collect()?, expr_kind))
158            }
159            NonterminalKind::Literal => {
160                // The `:literal` matcher does not support attributes.
161                Ok(ParseNtResult::Literal(
162                    self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
163                ))
164            }
165            NonterminalKind::Ty => Ok(ParseNtResult::Ty(
166                self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
167            )),
168            // This could be handled like a token, since it is one.
169            NonterminalKind::Ident => {
170                if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
171                    self.bump();
172                    Ok(ParseNtResult::Ident(ident, is_raw))
173                } else {
174                    Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
175                        span: self.token.span,
176                        token: self.token,
177                    }))
178                }
179            }
180            NonterminalKind::Path => Ok(ParseNtResult::Path(Box::new(
181                self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
182            ))),
183            NonterminalKind::Meta => {
184                Ok(ParseNtResult::Meta(Box::new(self.parse_attr_item(ForceCollect::Yes)?)))
185            }
186            NonterminalKind::Vis => Ok(ParseNtResult::Vis(Box::new(
187                self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
188            ))),
189            NonterminalKind::Lifetime => {
190                // We want to keep `'keyword` parsing, just like `keyword` is still
191                // an ident for nonterminal purposes.
192                if let Some((ident, is_raw)) = self.token.lifetime() {
193                    self.bump();
194                    Ok(ParseNtResult::Lifetime(ident, is_raw))
195                } else {
196                    Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
197                        span: self.token.span,
198                        token: self.token,
199                    }))
200                }
201            }
202            NonterminalKind::Guard => {
203                Ok(ParseNtResult::Guard(self.expect_match_arm_guard(ForceCollect::Yes)?))
204            }
205        }
206    }
207}
208
209/// The token is an identifier, but not `_`.
210/// We prohibit passing `_` to macros expecting `ident` for now.
211fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
212    token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
213}