rustc_parse/parser/
nonterminal.rs1use 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 #[inline]
22 pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
23 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 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 NonterminalKind::Expr(Expr2021 { .. }) => {
46 token.can_begin_expr()
47 && !token.is_keyword(kw::Let)
49 && !token.is_keyword(kw::Const)
51 }
52 NonterminalKind::Expr(Expr) => {
54 (token.can_begin_expr() || token.is_keyword(kw::Underscore))
62 && !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 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 #[inline]
121 pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
122 match kind {
127 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 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 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 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 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
221fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
224 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
225}