Skip to content

Commit 2b7b44b

Browse files
committed
Auto merge of #16786 - pksunkara:improve-readability, r=lnicola
internal: Improve readability of the parser code The code is basically equivalent to the previous version, but it improves the readability by making it much more simpler and concise.
2 parents 1487bc2 + 2a41b2c commit 2b7b44b

File tree

10 files changed

+39
-64
lines changed

10 files changed

+39
-64
lines changed

crates/parser/src/grammar.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,12 @@ fn delimited(
416416
if !parser(p) {
417417
break;
418418
}
419-
if !p.at(delim) {
419+
if !p.eat(delim) {
420420
if p.at_ts(first_set) {
421421
p.error(format!("expected {:?}", delim));
422422
} else {
423423
break;
424424
}
425-
} else {
426-
p.bump(delim);
427425
}
428426
}
429427
p.expect(ket);

crates/parser/src/grammar/expressions.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ fn current_op(p: &Parser<'_>) -> (u8, SyntaxKind, Associativity) {
211211
T![>] if p.at(T![>>]) => (9, T![>>], Left),
212212
T![>] if p.at(T![>=]) => (5, T![>=], Left),
213213
T![>] => (5, T![>], Left),
214-
T![=] if p.at(T![=>]) => NOT_AN_OP,
215214
T![=] if p.at(T![==]) => (5, T![==], Left),
216-
T![=] => (1, T![=], Right),
215+
T![=] if !p.at(T![=>]) => (1, T![=], Right),
217216
T![<] if p.at(T![<=]) => (5, T![<=], Left),
218217
T![<] if p.at(T![<<=]) => (1, T![<<=], Right),
219218
T![<] if p.at(T![<<]) => (9, T![<<], Left),
@@ -247,7 +246,7 @@ fn current_op(p: &Parser<'_>) -> (u8, SyntaxKind, Associativity) {
247246
fn expr_bp(
248247
p: &mut Parser<'_>,
249248
m: Option<Marker>,
250-
mut r: Restrictions,
249+
r: Restrictions,
251250
bp: u8,
252251
) -> Option<(CompletedMarker, BlockLike)> {
253252
let m = m.unwrap_or_else(|| {
@@ -295,10 +294,6 @@ fn expr_bp(
295294
let m = lhs.precede(p);
296295
p.bump(op);
297296

298-
// test binop_resets_statementness
299-
// fn f() { v = {1}&2; }
300-
r = Restrictions { prefer_stmt: false, ..r };
301-
302297
if is_range {
303298
// test postfix_range
304299
// fn foo() {
@@ -319,6 +314,9 @@ fn expr_bp(
319314
Associativity::Left => op_bp + 1,
320315
Associativity::Right => op_bp,
321316
};
317+
318+
// test binop_resets_statementness
319+
// fn f() { v = {1}&2; }
322320
expr_bp(p, None, Restrictions { prefer_stmt: false, ..r }, op_bp);
323321
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
324322
}
@@ -345,7 +343,7 @@ fn lhs(p: &mut Parser<'_>, r: Restrictions) -> Option<(CompletedMarker, BlockLik
345343
T![&] => {
346344
m = p.start();
347345
p.bump(T![&]);
348-
if p.at_contextual_kw(T![raw]) && (p.nth_at(1, T![mut]) || p.nth_at(1, T![const])) {
346+
if p.at_contextual_kw(T![raw]) && [T![mut], T![const]].contains(&p.nth(1)) {
349347
p.bump_remap(T![raw]);
350348
p.bump_any();
351349
} else {

crates/parser/src/grammar/expressions/atom.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub(super) fn atom_expr(
147147
T![async] if la == T![move] && p.nth(2) == T!['{'] => {
148148
let m = p.start();
149149
p.bump(T![async]);
150-
p.eat(T![move]);
150+
p.bump(T![move]);
151151
stmt_list(p);
152152
m.complete(p, BLOCK_EXPR)
153153
}
@@ -390,8 +390,7 @@ fn if_expr(p: &mut Parser<'_>) -> CompletedMarker {
390390
p.bump(T![if]);
391391
expr_no_struct(p);
392392
block_expr(p);
393-
if p.at(T![else]) {
394-
p.bump(T![else]);
393+
if p.eat(T![else]) {
395394
if p.at(T![if]) {
396395
if_expr(p);
397396
} else {

crates/parser/src/grammar/generic_params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn type_bound(p: &mut Parser<'_>) -> bool {
170170
_ => (),
171171
}
172172
if paths::is_use_path_start(p) {
173-
types::path_type_(p, false);
173+
types::path_type_bounds(p, false);
174174
} else {
175175
m.abandon(p);
176176
return false;

crates/parser/src/grammar/items.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,19 @@ pub(super) fn opt_item(p: &mut Parser<'_>, m: Marker) -> Result<(), Marker> {
156156
// impl T for Foo {
157157
// default async fn foo() {}
158158
// }
159-
T![async] => {
160-
let mut maybe_fn = p.nth(2);
161-
let is_unsafe = if matches!(maybe_fn, T![unsafe]) {
162-
// test default_async_unsafe_fn
163-
// impl T for Foo {
164-
// default async unsafe fn foo() {}
165-
// }
166-
maybe_fn = p.nth(3);
167-
true
168-
} else {
169-
false
170-
};
171-
172-
if matches!(maybe_fn, T![fn]) {
173-
p.bump_remap(T![default]);
174-
p.bump(T![async]);
175-
if is_unsafe {
176-
p.bump(T![unsafe]);
177-
}
178-
has_mods = true;
179-
}
159+
T![async]
160+
if p.nth_at(2, T![fn]) || (p.nth_at(2, T![unsafe]) && p.nth_at(3, T![fn])) =>
161+
{
162+
p.bump_remap(T![default]);
163+
p.bump(T![async]);
164+
165+
// test default_async_unsafe_fn
166+
// impl T for Foo {
167+
// default async unsafe fn foo() {}
168+
// }
169+
p.eat(T![unsafe]);
170+
171+
has_mods = true;
180172
}
181173
_ => (),
182174
}
@@ -419,11 +411,9 @@ fn fn_(p: &mut Parser<'_>, m: Marker) {
419411
// fn foo<T>() where T: Copy {}
420412
generic_params::opt_where_clause(p);
421413

422-
if p.at(T![;]) {
423-
// test fn_decl
424-
// trait T { fn foo(); }
425-
p.bump(T![;]);
426-
} else {
414+
// test fn_decl
415+
// trait T { fn foo(); }
416+
if !p.eat(T![;]) {
427417
expressions::block_expr(p);
428418
}
429419
m.complete(p, FN);

crates/parser/src/grammar/items/traits.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ fn not_a_qualified_path(p: &Parser<'_>) -> bool {
119119
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
120120
// because this is what almost always expected in practice, qualified paths in impls
121121
// (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment.
122-
if p.nth(1) == T![#] || p.nth(1) == T![>] || p.nth(1) == T![const] {
122+
if [T![#], T![>], T![const]].contains(&p.nth(1)) {
123123
return true;
124124
}
125-
(p.nth(1) == LIFETIME_IDENT || p.nth(1) == IDENT)
126-
&& (p.nth(2) == T![>] || p.nth(2) == T![,] || p.nth(2) == T![:] || p.nth(2) == T![=])
125+
([LIFETIME_IDENT, IDENT].contains(&p.nth(1)))
126+
&& ([T![>], T![,], T![:], T![=]].contains(&p.nth(2)))
127127
}
128128

129129
// test_err impl_type

crates/parser/src/grammar/params.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,16 @@ fn list_(p: &mut Parser<'_>, flavor: Flavor) {
7676
m.abandon(p);
7777
if p.eat(T![,]) {
7878
continue;
79-
} else {
80-
break;
8179
}
80+
break;
8281
}
8382
param(p, m, flavor);
84-
if !p.at(T![,]) {
83+
if !p.eat(T![,]) {
8584
if p.at_ts(PARAM_FIRST.union(ATTRIBUTE_FIRST)) {
8685
p.error("expected `,`");
8786
} else {
8887
break;
8988
}
90-
} else {
91-
p.bump(T![,]);
9289
}
9390
}
9491

crates/parser/src/grammar/patterns.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ fn is_literal_pat_start(p: &Parser<'_>) -> bool {
255255
fn literal_pat(p: &mut Parser<'_>) -> CompletedMarker {
256256
assert!(is_literal_pat_start(p));
257257
let m = p.start();
258-
if p.at(T![-]) {
259-
p.bump(T![-]);
260-
}
258+
p.eat(T![-]);
261259
expressions::literal(p);
262260
m.complete(p, LITERAL_PAT)
263261
}
@@ -468,14 +466,12 @@ fn slice_pat(p: &mut Parser<'_>) -> CompletedMarker {
468466
fn pat_list(p: &mut Parser<'_>, ket: SyntaxKind) {
469467
while !p.at(EOF) && !p.at(ket) {
470468
pattern_top(p);
471-
if !p.at(T![,]) {
469+
if !p.eat(T![,]) {
472470
if p.at_ts(PAT_TOP_FIRST) {
473471
p.error(format!("expected {:?}, got {:?}", T![,], p.current()));
474472
} else {
475473
break;
476474
}
477-
} else {
478-
p.bump(T![,]);
479475
}
480476
}
481477
}

crates/parser/src/grammar/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn type_with_bounds_cond(p: &mut Parser<'_>, allow_bounds: bool) {
4848
T![impl] => impl_trait_type(p),
4949
T![dyn] => dyn_trait_type(p),
5050
// Some path types are not allowed to have bounds (no plus)
51-
T![<] => path_type_(p, allow_bounds),
51+
T![<] => path_type_bounds(p, allow_bounds),
5252
_ if paths::is_path_start(p) => path_or_macro_type_(p, allow_bounds),
5353
LIFETIME_IDENT if p.nth_at(1, T![+]) => bare_dyn_trait_type(p),
5454
_ => {
@@ -294,7 +294,7 @@ fn bare_dyn_trait_type(p: &mut Parser<'_>) {
294294
// type C = self::Foo;
295295
// type D = super::Foo;
296296
pub(super) fn path_type(p: &mut Parser<'_>) {
297-
path_type_(p, true);
297+
path_type_bounds(p, true);
298298
}
299299

300300
// test macro_call_type
@@ -323,7 +323,7 @@ fn path_or_macro_type_(p: &mut Parser<'_>, allow_bounds: bool) {
323323
}
324324
}
325325

326-
pub(super) fn path_type_(p: &mut Parser<'_>, allow_bounds: bool) {
326+
pub(super) fn path_type_bounds(p: &mut Parser<'_>, allow_bounds: bool) {
327327
assert!(paths::is_path_start(p));
328328
let m = p.start();
329329
paths::type_path(p);

crates/parser/src/parser.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,9 @@ impl<'t> Parser<'t> {
250250

251251
/// Create an error node and consume the next token.
252252
pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
253-
match self.current() {
254-
T!['{'] | T!['}'] => {
255-
self.error(message);
256-
return;
257-
}
258-
_ => (),
253+
if matches!(self.current(), T!['{'] | T!['}']) {
254+
self.error(message);
255+
return;
259256
}
260257

261258
if self.at_ts(recovery) {

0 commit comments

Comments
 (0)