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::{FollowedByType, ForceCollect, ParseNtResult, Parser, PathStyle};
10
11impl<'a> Parser<'a> {
12 #[inline]
18 pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
19 fn may_be_ident(kind: MetaVarKind) -> bool {
21 match kind {
22 MetaVarKind::Stmt
23 | MetaVarKind::Pat(_)
24 | MetaVarKind::Expr { .. }
25 | MetaVarKind::Ty { .. }
26 | MetaVarKind::Literal | MetaVarKind::Meta { .. }
28 | MetaVarKind::Path => true,
29
30 MetaVarKind::Item
31 | MetaVarKind::Block
32 | MetaVarKind::Vis => false,
33
34 MetaVarKind::Ident
35 | MetaVarKind::Lifetime
36 | MetaVarKind::TT => unreachable!(),
37 }
38 }
39
40 match kind {
41 NonterminalKind::Expr(Expr2021 { .. }) => {
43 token.can_begin_expr()
44 && !token.is_keyword(kw::Let)
46 && !token.is_keyword(kw::Const)
48 }
49 NonterminalKind::Expr(Expr) => {
51 (token.can_begin_expr() || token.is_keyword(kw::Underscore))
59 && !token.is_keyword(kw::Let)
61 }
62 NonterminalKind::Ty => token.can_begin_type(),
63 NonterminalKind::Ident => get_macro_ident(token).is_some(),
64 NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
65 NonterminalKind::Vis => match token.kind {
66 token::Comma
68 | token::Ident(..)
69 | token::NtIdent(..)
70 | token::NtLifetime(..)
71 | token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => true,
72 _ => token.can_begin_type(),
73 },
74 NonterminalKind::Block => match &token.kind {
75 token::OpenBrace => true,
76 token::NtLifetime(..) => true,
77 token::OpenInvisible(InvisibleOrigin::MetaVar(k)) => match k {
78 MetaVarKind::Block
79 | MetaVarKind::Stmt
80 | MetaVarKind::Expr { .. }
81 | MetaVarKind::Literal => true,
82 MetaVarKind::Item
83 | MetaVarKind::Pat(_)
84 | MetaVarKind::Ty { .. }
85 | MetaVarKind::Meta { .. }
86 | MetaVarKind::Path
87 | MetaVarKind::Vis => false,
88 MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
89 unreachable!()
90 }
91 },
92 _ => false,
93 },
94 NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
95 token::PathSep | token::Ident(..) | token::NtIdent(..) => true,
96 token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => may_be_ident(*kind),
97 _ => false,
98 },
99 NonterminalKind::Pat(pat_kind) => token.can_begin_pattern(pat_kind),
100 NonterminalKind::Lifetime => match &token.kind {
101 token::Lifetime(..) | token::NtLifetime(..) => true,
102 _ => false,
103 },
104 NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
105 token.kind.close_delim().is_none()
106 }
107 }
108 }
109
110 #[inline]
113 pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, ParseNtResult> {
114 match kind {
119 NonterminalKind::TT => Ok(ParseNtResult::Tt(self.parse_token_tree())),
121 NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
122 Some(item) => Ok(ParseNtResult::Item(item)),
123 None => Err(self.dcx().create_err(UnexpectedNonterminal::Item(self.token.span))),
124 },
125 NonterminalKind::Block => {
126 Ok(ParseNtResult::Block(self.collect_tokens_no_attrs(|this| this.parse_block())?))
129 }
130 NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
131 Some(stmt) => Ok(ParseNtResult::Stmt(Box::new(stmt))),
132 None => {
133 Err(self.dcx().create_err(UnexpectedNonterminal::Statement(self.token.span)))
134 }
135 },
136 NonterminalKind::Pat(pat_kind) => Ok(ParseNtResult::Pat(
137 self.collect_tokens_no_attrs(|this| match pat_kind {
138 PatParam { .. } => this.parse_pat_no_top_alt(None, None),
139 PatWithOr => this.parse_pat_no_top_guard(
140 None,
141 RecoverComma::No,
142 RecoverColon::No,
143 CommaRecoveryMode::EitherTupleOrPipe,
144 ),
145 })?,
146 pat_kind,
147 )),
148 NonterminalKind::Expr(expr_kind) => {
149 Ok(ParseNtResult::Expr(self.parse_expr_force_collect()?, expr_kind))
150 }
151 NonterminalKind::Literal => {
152 Ok(ParseNtResult::Literal(
154 self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
155 ))
156 }
157 NonterminalKind::Ty => Ok(ParseNtResult::Ty(
158 self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
159 )),
160 NonterminalKind::Ident => {
162 if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
163 self.bump();
164 Ok(ParseNtResult::Ident(ident, is_raw))
165 } else {
166 Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
167 span: self.token.span,
168 token: self.token,
169 }))
170 }
171 }
172 NonterminalKind::Path => Ok(ParseNtResult::Path(Box::new(
173 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
174 ))),
175 NonterminalKind::Meta => {
176 Ok(ParseNtResult::Meta(Box::new(self.parse_attr_item(ForceCollect::Yes)?)))
177 }
178 NonterminalKind::Vis => Ok(ParseNtResult::Vis(Box::new(
179 self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
180 ))),
181 NonterminalKind::Lifetime => {
182 if let Some((ident, is_raw)) = self.token.lifetime() {
185 self.bump();
186 Ok(ParseNtResult::Lifetime(ident, is_raw))
187 } else {
188 Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
189 span: self.token.span,
190 token: self.token,
191 }))
192 }
193 }
194 }
195 }
196}
197
198fn get_macro_ident(token: &Token) -> Option<(Ident, token::IdentIsRaw)> {
201 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
202}