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 => token.is_keyword(kw::If),
109 NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
110 token.kind.close_delim().is_none()
111 }
112 }
113 }
114
115 #[inline]
118 pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
119 match kind {
124 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 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 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 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 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
209fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
212 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
213}