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_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 #[inline]
20 pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
21 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 | 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 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.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| this.parse_block())?))
139 }
140 NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
141 Some(stmt) => Ok(ParseNtResult::Stmt(Box::new(stmt))),
142 None => {
143 Err(self.dcx().create_err(UnexpectedNonterminal::Statement(self.token.span)))
144 }
145 },
146 NonterminalKind::Pat(pat_kind) => Ok(ParseNtResult::Pat(
147 self.collect_tokens_no_attrs(|this| match pat_kind {
148 PatParam { .. } => this.parse_pat_no_top_alt(None, None),
149 PatWithOr => this.parse_pat_no_top_guard(
150 None,
151 RecoverComma::No,
152 RecoverColon::No,
153 CommaRecoveryMode::EitherTupleOrPipe,
154 ),
155 })
156 .map(Box::new)?,
157 pat_kind,
158 )),
159 NonterminalKind::Expr(expr_kind) => {
160 Ok(ParseNtResult::Expr(self.parse_expr_force_collect()?, expr_kind))
161 }
162 NonterminalKind::Literal => {
163 Ok(ParseNtResult::Literal(
165 self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
166 ))
167 }
168 NonterminalKind::Ty => Ok(ParseNtResult::Ty(
169 self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
170 )),
171 NonterminalKind::Ident => {
173 if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
174 self.bump();
175 Ok(ParseNtResult::Ident(ident, is_raw))
176 } else {
177 Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
178 span: self.token.span,
179 token: self.token,
180 }))
181 }
182 }
183 NonterminalKind::Path => Ok(ParseNtResult::Path(Box::new(
184 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
185 ))),
186 NonterminalKind::Meta => {
187 Ok(ParseNtResult::Meta(Box::new(self.parse_attr_item(ForceCollect::Yes)?)))
188 }
189 NonterminalKind::Vis => Ok(ParseNtResult::Vis(Box::new(
190 self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
191 ))),
192 NonterminalKind::Lifetime => {
193 if let Some((ident, is_raw)) = self.token.lifetime() {
196 self.bump();
197 Ok(ParseNtResult::Lifetime(ident, is_raw))
198 } else {
199 Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
200 span: self.token.span,
201 token: self.token,
202 }))
203 }
204 }
205 NonterminalKind::Guard => {
206 Ok(ParseNtResult::Guard(self.expect_match_arm_guard(ForceCollect::Yes)?))
207 }
208 }
209 }
210}
211
212fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
215 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
216}