Skip to content

Commit 7729473

Browse files
bors[bot]matklad
andauthored
Merge #10276
10276: internal: parser cleanup r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents dd12521 + fbb6a6a commit 7729473

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+555
-759
lines changed

crates/parser/src/grammar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ mod items;
3434
mod params;
3535
mod paths;
3636
mod patterns;
37-
mod type_args;
38-
mod type_params;
37+
mod generic_args;
38+
mod generic_params;
3939
mod types;
4040

4141
use crate::{

crates/parser/src/grammar/expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
486486
let m = lhs.precede(p);
487487
p.bump_any();
488488
name_ref(p);
489-
type_args::opt_generic_arg_list(p, true);
489+
generic_args::opt_generic_arg_list(p, true);
490490
if p.at(T!['(']) {
491491
arg_list(p);
492492
}

crates/parser/src/grammar/type_args.rs renamed to crates/parser/src/grammar/generic_args.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,6 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) {
2323
m.complete(p, GENERIC_ARG_LIST);
2424
}
2525

26-
pub(super) fn const_arg(p: &mut Parser) {
27-
let m = p.start();
28-
// FIXME: duplicates the code below
29-
match p.current() {
30-
T!['{'] => {
31-
expressions::block_expr(p);
32-
m.complete(p, CONST_ARG);
33-
}
34-
k if k.is_literal() => {
35-
expressions::literal(p);
36-
m.complete(p, CONST_ARG);
37-
}
38-
T![true] | T![false] => {
39-
expressions::literal(p);
40-
m.complete(p, CONST_ARG);
41-
}
42-
T![-] => {
43-
let lm = p.start();
44-
p.bump(T![-]);
45-
expressions::literal(p);
46-
lm.complete(p, PREFIX_EXPR);
47-
m.complete(p, CONST_ARG);
48-
}
49-
_ => {
50-
let lm = p.start();
51-
paths::use_path(p);
52-
lm.complete(p, PATH_EXPR);
53-
m.complete(p, CONST_ARG);
54-
}
55-
}
56-
}
57-
5826
// test type_arg
5927
// type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>;
6028
fn generic_arg(p: &mut Parser) {
@@ -83,7 +51,7 @@ fn generic_arg(p: &mut Parser) {
8351
path_ty.abandon(p);
8452
m.complete(p, ASSOC_TYPE_ARG);
8553
}
86-
T![:] if p.nth(1) == T![:] => {
54+
T![:] if p.at(T![::]) => {
8755
// NameRef::, this is a path type
8856
path_seg.complete(p, PATH_SEGMENT);
8957
let qual = path.complete(p, PATH);
@@ -94,7 +62,7 @@ fn generic_arg(p: &mut Parser) {
9462
}
9563
// NameRef<...>:
9664
T![:] => {
97-
type_params::bounds(p);
65+
generic_params::bounds(p);
9866

9967
path_seg.abandon(p);
10068
path.abandon(p);
@@ -137,3 +105,35 @@ fn generic_arg(p: &mut Parser) {
137105
}
138106
}
139107
}
108+
109+
pub(super) fn const_arg(p: &mut Parser) {
110+
let m = p.start();
111+
// FIXME: duplicates the code above
112+
match p.current() {
113+
T!['{'] => {
114+
expressions::block_expr(p);
115+
m.complete(p, CONST_ARG);
116+
}
117+
k if k.is_literal() => {
118+
expressions::literal(p);
119+
m.complete(p, CONST_ARG);
120+
}
121+
T![true] | T![false] => {
122+
expressions::literal(p);
123+
m.complete(p, CONST_ARG);
124+
}
125+
T![-] => {
126+
let lm = p.start();
127+
p.bump(T![-]);
128+
expressions::literal(p);
129+
lm.complete(p, PREFIX_EXPR);
130+
m.complete(p, CONST_ARG);
131+
}
132+
_ => {
133+
let lm = p.start();
134+
paths::use_path(p);
135+
lm.complete(p, PATH_EXPR);
136+
m.complete(p, CONST_ARG);
137+
}
138+
}
139+
}

crates/parser/src/grammar/type_params.rs renamed to crates/parser/src/grammar/generic_params.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
use super::*;
22

33
pub(super) fn opt_generic_param_list(p: &mut Parser) {
4-
if !p.at(T![<]) {
5-
return;
4+
if p.at(T![<]) {
5+
generic_param_list(p);
66
}
7-
generic_param_list(p);
87
}
98

9+
// test generic_param_list
10+
// fn f<T: Clone>() {}
1011
fn generic_param_list(p: &mut Parser) {
1112
assert!(p.at(T![<]));
1213
let m = p.start();
1314
p.bump(T![<]);
1415

1516
while !p.at(EOF) && !p.at(T![>]) {
16-
let m = p.start();
17-
18-
// test generic_lifetime_type_attribute
19-
// fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) {
20-
// }
21-
attributes::outer_attrs(p);
22-
23-
match p.current() {
24-
LIFETIME_IDENT => lifetime_param(p, m),
25-
IDENT => type_param(p, m),
26-
T![const] => const_param(p, m),
27-
_ => {
28-
m.abandon(p);
29-
p.err_and_bump("expected type parameter")
30-
}
31-
}
17+
generic_param(p);
3218
if !p.at(T![>]) && !p.expect(T![,]) {
3319
break;
3420
}
@@ -37,6 +23,24 @@ fn generic_param_list(p: &mut Parser) {
3723
m.complete(p, GENERIC_PARAM_LIST);
3824
}
3925

26+
fn generic_param(p: &mut Parser) {
27+
let m = p.start();
28+
// test generic_param_attribute
29+
// fn foo<#[lt_attr] 'a, #[t_attr] T>() {}
30+
attributes::outer_attrs(p);
31+
match p.current() {
32+
LIFETIME_IDENT => lifetime_param(p, m),
33+
IDENT => type_param(p, m),
34+
T![const] => const_param(p, m),
35+
_ => {
36+
m.abandon(p);
37+
p.err_and_bump("expected type parameter")
38+
}
39+
}
40+
}
41+
42+
// test lifetime_param
43+
// fn f<'a: 'b>() {}
4044
fn lifetime_param(p: &mut Parser, m: Marker) {
4145
assert!(p.at(LIFETIME_IDENT));
4246
lifetime(p);
@@ -46,15 +50,17 @@ fn lifetime_param(p: &mut Parser, m: Marker) {
4650
m.complete(p, LIFETIME_PARAM);
4751
}
4852

53+
// test type_param
54+
// fn f<T: Clone>() {}
4955
fn type_param(p: &mut Parser, m: Marker) {
5056
assert!(p.at(IDENT));
5157
name(p);
5258
if p.at(T![:]) {
5359
bounds(p);
5460
}
55-
// test type_param_default
56-
// struct S<T = i32>;
5761
if p.at(T![=]) {
62+
// test type_param_default
63+
// struct S<T = i32>;
5864
p.bump(T![=]);
5965
types::type_(p)
6066
}
@@ -64,7 +70,6 @@ fn type_param(p: &mut Parser, m: Marker) {
6470
// test const_param
6571
// struct S<const N: u32>;
6672
fn const_param(p: &mut Parser, m: Marker) {
67-
assert!(p.at(T![const]));
6873
p.bump(T![const]);
6974
name(p);
7075
if p.at(T![:]) {
@@ -73,26 +78,18 @@ fn const_param(p: &mut Parser, m: Marker) {
7378
p.error("missing type for const parameter");
7479
}
7580

76-
// test const_param_defaults
77-
// struct A<const N: i32 = -1>;
78-
// struct B<const N: i32 = {}>;
79-
// struct C<const N: i32 = some::CONST>;
8081
if p.at(T![=]) {
82+
// test const_param_defaults
83+
// struct A<const N: i32 = -1>;
84+
// struct B<const N: i32 = {}>;
85+
// struct C<const N: i32 = some::CONST>;
8186
p.bump(T![=]);
82-
type_args::const_arg(p);
87+
generic_args::const_arg(p);
8388
}
8489

8590
m.complete(p, CONST_PARAM);
8691
}
8792

88-
// test type_param_bounds
89-
// struct S<T: 'a + ?Sized + (Copy)>;
90-
pub(super) fn bounds(p: &mut Parser) {
91-
assert!(p.at(T![:]));
92-
p.bump(T![:]);
93-
bounds_without_colon(p);
94-
}
95-
9693
fn lifetime_bounds(p: &mut Parser) {
9794
assert!(p.at(T![:]));
9895
p.bump(T![:]);
@@ -104,21 +101,28 @@ fn lifetime_bounds(p: &mut Parser) {
104101
}
105102
}
106103

104+
// test type_param_bounds
105+
// struct S<T: 'a + ?Sized + (Copy)>;
106+
pub(super) fn bounds(p: &mut Parser) {
107+
assert!(p.at(T![:]));
108+
p.bump(T![:]);
109+
bounds_without_colon(p);
110+
}
111+
112+
pub(super) fn bounds_without_colon(p: &mut Parser) {
113+
let m = p.start();
114+
bounds_without_colon_m(p, m);
115+
}
116+
107117
pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker {
108118
while type_bound(p) {
109119
if !p.eat(T![+]) {
110120
break;
111121
}
112122
}
113-
114123
marker.complete(p, TYPE_BOUND_LIST)
115124
}
116125

117-
pub(super) fn bounds_without_colon(p: &mut Parser) {
118-
let m = p.start();
119-
bounds_without_colon_m(p, m);
120-
}
121-
122126
fn type_bound(p: &mut Parser) -> bool {
123127
let m = p.start();
124128
let has_paren = p.eat(T!['(']);
@@ -160,8 +164,9 @@ pub(super) fn opt_where_clause(p: &mut Parser) {
160164

161165
let comma = p.eat(T![,]);
162166

163-
if is_where_clause_end(p) {
164-
break;
167+
match p.current() {
168+
T!['{'] | T![;] | T![=] => break,
169+
_ => (),
165170
}
166171

167172
if !comma {
@@ -170,20 +175,16 @@ pub(super) fn opt_where_clause(p: &mut Parser) {
170175
}
171176

172177
m.complete(p, WHERE_CLAUSE);
173-
}
174178

175-
fn is_where_predicate(p: &mut Parser) -> bool {
176-
match p.current() {
177-
LIFETIME_IDENT => true,
178-
T![impl] => false,
179-
token => types::TYPE_FIRST.contains(token),
179+
fn is_where_predicate(p: &mut Parser) -> bool {
180+
match p.current() {
181+
LIFETIME_IDENT => true,
182+
T![impl] => false,
183+
token => types::TYPE_FIRST.contains(token),
184+
}
180185
}
181186
}
182187

183-
fn is_where_clause_end(p: &mut Parser) -> bool {
184-
matches!(p.current(), T!['{'] | T![;] | T![=])
185-
}
186-
187188
fn where_predicate(p: &mut Parser) {
188189
let m = p.start();
189190
match p.current() {
@@ -199,12 +200,12 @@ fn where_predicate(p: &mut Parser) {
199200
p.error("expected lifetime or type");
200201
}
201202
_ => {
202-
// test where_pred_for
203-
// fn for_trait<F>()
204-
// where
205-
// for<'a> F: Fn(&'a str)
206-
// { }
207203
if p.at(T![for]) {
204+
// test where_pred_for
205+
// fn for_trait<F>()
206+
// where
207+
// for<'a> F: Fn(&'a str)
208+
// { }
208209
types::for_binder(p);
209210
}
210211

crates/parser/src/grammar/items.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::*;
1919
// struct S;
2020
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
2121
attributes::inner_attrs(p);
22-
while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) {
22+
while !p.at(EOF) && !(p.at(T!['}']) && stop_on_r_curly) {
2323
item_or_macro(p, stop_on_r_curly)
2424
}
2525
}
@@ -284,15 +284,15 @@ fn type_alias(p: &mut Parser, m: Marker) {
284284

285285
// test type_item_type_params
286286
// type Result<T> = ();
287-
type_params::opt_generic_param_list(p);
287+
generic_params::opt_generic_param_list(p);
288288

289289
if p.at(T![:]) {
290-
type_params::bounds(p);
290+
generic_params::bounds(p);
291291
}
292292

293293
// test type_item_where_clause
294294
// type Foo where Foo: Copy = ();
295-
type_params::opt_where_clause(p);
295+
generic_params::opt_where_clause(p);
296296
if p.eat(T![=]) {
297297
types::type_(p);
298298
}
@@ -383,7 +383,7 @@ fn fn_(p: &mut Parser, m: Marker) {
383383
name_r(p, ITEM_RECOVERY_SET);
384384
// test function_type_params
385385
// fn foo<T: Clone + Copy>(){}
386-
type_params::opt_generic_param_list(p);
386+
generic_params::opt_generic_param_list(p);
387387

388388
if p.at(T!['(']) {
389389
params::param_list_fn_def(p);
@@ -397,7 +397,7 @@ fn fn_(p: &mut Parser, m: Marker) {
397397

398398
// test function_where_clause
399399
// fn foo<T>() where T: Copy {}
400-
type_params::opt_where_clause(p);
400+
generic_params::opt_where_clause(p);
401401

402402
if p.at(T![;]) {
403403
// test fn_decl

0 commit comments

Comments
 (0)