Skip to content

Commit f8a21a5

Browse files
committed
Use Nonterminal::* in nonterminal.rs.
It makes the code more readable.
1 parent acd3a5e commit f8a21a5

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::ptr::P;
2-
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token};
2+
use rustc_ast::token::{self, Delimiter, Nonterminal::*, NonterminalKind, Token};
33
use rustc_ast::HasTokens;
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::IntoDiagnostic;
@@ -20,10 +20,7 @@ impl<'a> Parser<'a> {
2020
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
2121
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
2222
fn may_be_ident(nt: &token::Nonterminal) -> bool {
23-
!matches!(
24-
*nt,
25-
token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_)
26-
)
23+
!matches!(*nt, NtItem(_) | NtBlock(_) | NtVis(_) | NtLifetime(_))
2724
}
2825

2926
match kind {
@@ -46,20 +43,14 @@ impl<'a> Parser<'a> {
4643
token::OpenDelim(Delimiter::Brace) => true,
4744
token::Interpolated(nt) => !matches!(
4845
**nt,
49-
token::NtItem(_)
50-
| token::NtPat(_)
51-
| token::NtTy(_)
52-
| token::NtIdent(..)
53-
| token::NtMeta(_)
54-
| token::NtPath(_)
55-
| token::NtVis(_)
46+
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_) | NtVis(_)
5647
),
5748
_ => false,
5849
},
5950
NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
6051
token::ModSep | token::Ident(..) => true,
6152
token::Interpolated(nt) => match **nt {
62-
token::NtPath(_) | token::NtMeta(_) => true,
53+
NtPath(_) | NtMeta(_) => true,
6354
_ => may_be_ident(&nt),
6455
},
6556
_ => false,
@@ -87,7 +78,7 @@ impl<'a> Parser<'a> {
8778
NonterminalKind::Lifetime => match &token.kind {
8879
token::Lifetime(_) => true,
8980
token::Interpolated(nt) => {
90-
matches!(**nt, token::NtLifetime(_))
81+
matches!(**nt, NtLifetime(_))
9182
}
9283
_ => false,
9384
},
@@ -109,7 +100,7 @@ impl<'a> Parser<'a> {
109100
// Note that TT is treated differently to all the others.
110101
NonterminalKind::TT => return Ok(NtOrTt::Tt(self.parse_token_tree())),
111102
NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
112-
Some(item) => token::NtItem(item),
103+
Some(item) => NtItem(item),
113104
None => {
114105
return Err(UnexpectedNonterminal::Item(self.token.span)
115106
.into_diagnostic(&self.sess.span_diagnostic));
@@ -118,17 +109,17 @@ impl<'a> Parser<'a> {
118109
NonterminalKind::Block => {
119110
// While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
120111
// the ':block' matcher does not support them
121-
token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
112+
NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
122113
}
123114
NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
124-
Some(s) => token::NtStmt(P(s)),
115+
Some(s) => NtStmt(P(s)),
125116
None => {
126117
return Err(UnexpectedNonterminal::Statement(self.token.span)
127118
.into_diagnostic(&self.sess.span_diagnostic));
128119
}
129120
},
130121
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
131-
token::NtPat(self.collect_tokens_no_attrs(|this| match kind {
122+
NtPat(self.collect_tokens_no_attrs(|this| match kind {
132123
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
133124
NonterminalKind::PatWithOr => this.parse_pat_allow_top_alt(
134125
None,
@@ -140,15 +131,15 @@ impl<'a> Parser<'a> {
140131
})?)
141132
}
142133

143-
NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
134+
NonterminalKind::Expr => NtExpr(self.parse_expr_force_collect()?),
144135
NonterminalKind::Literal => {
145136
// The `:literal` matcher does not support attributes
146-
token::NtLiteral(
137+
NtLiteral(
147138
self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
148139
)
149140
}
150141

151-
NonterminalKind::Ty => token::NtTy(
142+
NonterminalKind::Ty => NtTy(
152143
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
153144
),
154145

@@ -157,24 +148,24 @@ impl<'a> Parser<'a> {
157148
if let Some((ident, is_raw)) = get_macro_ident(&self.token) =>
158149
{
159150
self.bump();
160-
token::NtIdent(ident, is_raw)
151+
NtIdent(ident, is_raw)
161152
}
162153
NonterminalKind::Ident => {
163154
return Err(UnexpectedNonterminal::Ident {
164155
span: self.token.span,
165156
token: self.token.clone(),
166157
}.into_diagnostic(&self.sess.span_diagnostic));
167158
}
168-
NonterminalKind::Path => token::NtPath(
159+
NonterminalKind::Path => NtPath(
169160
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
170161
),
171-
NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
172-
NonterminalKind::Vis => token::NtVis(
162+
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
163+
NonterminalKind::Vis => NtVis(
173164
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
174165
),
175166
NonterminalKind::Lifetime => {
176167
if self.check_lifetime() {
177-
token::NtLifetime(self.expect_lifetime().ident)
168+
NtLifetime(self.expect_lifetime().ident)
178169
} else {
179170
return Err(UnexpectedNonterminal::Lifetime {
180171
span: self.token.span,

0 commit comments

Comments
 (0)