Skip to content

Commit 3dc2aee

Browse files
committed
internal: parser cleanup
1 parent af9fd37 commit 3dc2aee

12 files changed

+175
-116
lines changed

crates/parser/src/grammar/items.rs

Lines changed: 1 addition & 1 deletion
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
}

crates/parser/src/grammar/type_params.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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();
@@ -15,9 +16,8 @@ fn generic_param_list(p: &mut Parser) {
1516
while !p.at(EOF) && !p.at(T![>]) {
1617
let m = p.start();
1718

18-
// test generic_lifetime_type_attribute
19-
// fn foo<#[derive(Lifetime)] 'a, #[derive(Type)] T>(_: &'a T) {
20-
// }
19+
// test generic_param_list_param_attribute
20+
// fn foo<#[lt_attr] 'a, #[t_attr] T>() {}
2121
attributes::outer_attrs(p);
2222

2323
match p.current() {
@@ -37,6 +37,8 @@ fn generic_param_list(p: &mut Parser) {
3737
m.complete(p, GENERIC_PARAM_LIST);
3838
}
3939

40+
// test lifetime_param
41+
// fn f<'a: 'b>() {}
4042
fn lifetime_param(p: &mut Parser, m: Marker) {
4143
assert!(p.at(LIFETIME_IDENT));
4244
lifetime(p);
@@ -46,15 +48,17 @@ fn lifetime_param(p: &mut Parser, m: Marker) {
4648
m.complete(p, LIFETIME_PARAM);
4749
}
4850

51+
// test type_param
52+
// fn f<T: Clone>() {}
4953
fn type_param(p: &mut Parser, m: Marker) {
5054
assert!(p.at(IDENT));
5155
name(p);
5256
if p.at(T![:]) {
5357
bounds(p);
5458
}
55-
// test type_param_default
56-
// struct S<T = i32>;
5759
if p.at(T![=]) {
60+
// test type_param_default
61+
// struct S<T = i32>;
5862
p.bump(T![=]);
5963
types::type_(p)
6064
}
@@ -64,7 +68,6 @@ fn type_param(p: &mut Parser, m: Marker) {
6468
// test const_param
6569
// struct S<const N: u32>;
6670
fn const_param(p: &mut Parser, m: Marker) {
67-
assert!(p.at(T![const]));
6871
p.bump(T![const]);
6972
name(p);
7073
if p.at(T![:]) {
@@ -73,26 +76,18 @@ fn const_param(p: &mut Parser, m: Marker) {
7376
p.error("missing type for const parameter");
7477
}
7578

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>;
8079
if p.at(T![=]) {
80+
// test const_param_defaults
81+
// struct A<const N: i32 = -1>;
82+
// struct B<const N: i32 = {}>;
83+
// struct C<const N: i32 = some::CONST>;
8184
p.bump(T![=]);
8285
type_args::const_arg(p);
8386
}
8487

8588
m.complete(p, CONST_PARAM);
8689
}
8790

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-
9691
fn lifetime_bounds(p: &mut Parser) {
9792
assert!(p.at(T![:]));
9893
p.bump(T![:]);
@@ -104,21 +99,28 @@ fn lifetime_bounds(p: &mut Parser) {
10499
}
105100
}
106101

102+
// test type_param_bounds
103+
// struct S<T: 'a + ?Sized + (Copy)>;
104+
pub(super) fn bounds(p: &mut Parser) {
105+
assert!(p.at(T![:]));
106+
p.bump(T![:]);
107+
bounds_without_colon(p);
108+
}
109+
110+
pub(super) fn bounds_without_colon(p: &mut Parser) {
111+
let m = p.start();
112+
bounds_without_colon_m(p, m);
113+
}
114+
107115
pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker {
108116
while type_bound(p) {
109117
if !p.eat(T![+]) {
110118
break;
111119
}
112120
}
113-
114121
marker.complete(p, TYPE_BOUND_LIST)
115122
}
116123

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

161163
let comma = p.eat(T![,]);
162164

163-
if is_where_clause_end(p) {
164-
break;
165+
match p.current() {
166+
T!['{'] | T![;] | T![=] => break,
167+
_ => (),
165168
}
166169

167170
if !comma {
@@ -170,20 +173,16 @@ pub(super) fn opt_where_clause(p: &mut Parser) {
170173
}
171174

172175
m.complete(p, WHERE_CLAUSE);
173-
}
174176

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),
177+
fn is_where_predicate(p: &mut Parser) -> bool {
178+
match p.current() {
179+
LIFETIME_IDENT => true,
180+
T![impl] => false,
181+
token => types::TYPE_FIRST.contains(token),
182+
}
180183
}
181184
}
182185

183-
fn is_where_clause_end(p: &mut Parser) -> bool {
184-
matches!(p.current(), T!['{'] | T![;] | T![=])
185-
}
186-
187186
fn where_predicate(p: &mut Parser) {
188187
let m = p.start();
189188
match p.current() {
@@ -199,12 +198,12 @@ fn where_predicate(p: &mut Parser) {
199198
p.error("expected lifetime or type");
200199
}
201200
_ => {
202-
// test where_pred_for
203-
// fn for_trait<F>()
204-
// where
205-
// for<'a> F: Fn(&'a str)
206-
// { }
207201
if p.at(T![for]) {
202+
// test where_pred_for
203+
// fn for_trait<F>()
204+
// where
205+
// for<'a> F: Fn(&'a str)
206+
// { }
208207
types::for_binder(p);
209208
}
210209

crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rast

Lines changed: 0 additions & 69 deletions
This file was deleted.

crates/syntax/test_data/parser/inline/ok/0122_generic_lifetime_type_attribute.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo<#[lt_attr] 'a, #[t_attr] T>() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f<'a: 'b>() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f<T: Clone>() {}

0 commit comments

Comments
 (0)