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 => false,
35
36 MetaVarKind::Ident
37 | MetaVarKind::Lifetime
38 | MetaVarKind::TT => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
39 }
40 }
41
42 match kind {
43 NonterminalKind::Expr(Expr2021 { .. }) => {
45 token.can_begin_expr()
46 && !token.is_keyword(kw::Let)
48 && !token.is_keyword(kw::Const)
50 }
51 NonterminalKind::Expr(Expr) => {
53 (token.can_begin_expr() || token.is_keyword(kw::Underscore))
61 && !token.is_keyword(kw::Let)
63 }
64 NonterminalKind::Ty => token.can_begin_type(),
65 NonterminalKind::Ident => get_macro_ident(token).is_some(),
66 NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
67 NonterminalKind::Vis => match token.kind {
68 token::Comma
70 | token::Ident(..)
71 | token::NtIdent(..)
72 | token::NtLifetime(..)
73 | token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => true,
74 _ => token.can_begin_type(),
75 },
76 NonterminalKind::Block => match &token.kind {
77 token::OpenBrace => true,
78 token::NtLifetime(..) => true,
79 token::OpenInvisible(InvisibleOrigin::MetaVar(k)) => match k {
80 MetaVarKind::Block
81 | MetaVarKind::Stmt
82 | MetaVarKind::Expr { .. }
83 | MetaVarKind::Literal => true,
84 MetaVarKind::Item
85 | MetaVarKind::Pat(_)
86 | MetaVarKind::Ty { .. }
87 | MetaVarKind::Meta { .. }
88 | MetaVarKind::Path
89 | MetaVarKind::Vis => false,
90 MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
91 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
92 }
93 },
94 _ => false,
95 },
96 NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
97 token::PathSep | token::Ident(..) | token::NtIdent(..) => true,
98 token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => may_be_ident(*kind),
99 _ => false,
100 },
101 NonterminalKind::Pat(pat_kind) => token.can_begin_pattern(pat_kind),
102 NonterminalKind::Lifetime => match &token.kind {
103 token::Lifetime(..) | token::NtLifetime(..) => true,
104 _ => false,
105 },
106 NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
107 token.kind.close_delim().is_none()
108 }
109 }
110 }
111
112 #[inline]
115 pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
116 match kind {
121 NonterminalKind::TT => Ok(ParseNtResult::Tt(self.parse_token_tree())),
123 NonterminalKind::Item => match self
124 .parse_item(ForceCollect::Yes, AllowConstBlockItems::Yes)?
125 {
126 Some(item) => Ok(ParseNtResult::Item(item)),
127 None => Err(self.dcx().create_err(UnexpectedNonterminal::Item(self.token.span))),
128 },
129 NonterminalKind::Block => {
130 Ok(ParseNtResult::Block(self.collect_tokens_no_attrs(|this| this.parse_block())?))
133 }
134 NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
135 Some(stmt) => Ok(ParseNtResult::Stmt(Box::new(stmt))),
136 None => {
137 Err(self.dcx().create_err(UnexpectedNonterminal::Statement(self.token.span)))
138 }
139 },
140 NonterminalKind::Pat(pat_kind) => Ok(ParseNtResult::Pat(
141 self.collect_tokens_no_attrs(|this| match pat_kind {
142 PatParam { .. } => this.parse_pat_no_top_alt(None, None),
143 PatWithOr => this.parse_pat_no_top_guard(
144 None,
145 RecoverComma::No,
146 RecoverColon::No,
147 CommaRecoveryMode::EitherTupleOrPipe,
148 ),
149 })
150 .map(Box::new)?,
151 pat_kind,
152 )),
153 NonterminalKind::Expr(expr_kind) => {
154 Ok(ParseNtResult::Expr(self.parse_expr_force_collect()?, expr_kind))
155 }
156 NonterminalKind::Literal => {
157 Ok(ParseNtResult::Literal(
159 self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
160 ))
161 }
162 NonterminalKind::Ty => Ok(ParseNtResult::Ty(
163 self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
164 )),
165 NonterminalKind::Ident => {
167 if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
168 self.bump();
169 Ok(ParseNtResult::Ident(ident, is_raw))
170 } else {
171 Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
172 span: self.token.span,
173 token: self.token,
174 }))
175 }
176 }
177 NonterminalKind::Path => Ok(ParseNtResult::Path(Box::new(
178 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
179 ))),
180 NonterminalKind::Meta => {
181 Ok(ParseNtResult::Meta(Box::new(self.parse_attr_item(ForceCollect::Yes)?)))
182 }
183 NonterminalKind::Vis => Ok(ParseNtResult::Vis(Box::new(
184 self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
185 ))),
186 NonterminalKind::Lifetime => {
187 if let Some((ident, is_raw)) = self.token.lifetime() {
190 self.bump();
191 Ok(ParseNtResult::Lifetime(ident, is_raw))
192 } else {
193 Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
194 span: self.token.span,
195 token: self.token,
196 }))
197 }
198 }
199 }
200 }
201}
202
203fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
206 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
207}