Skip to content

Commit be20b01

Browse files
committed
Move type parameter parsing to a separate file
1 parent 7cdf990 commit be20b01

File tree

4 files changed

+80
-74
lines changed

4 files changed

+80
-74
lines changed

src/parser/event_parser/grammar/items/mod.rs

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -83,76 +83,6 @@ fn item(p: &mut Parser) {
8383
item.complete(p, item_kind);
8484
}
8585

86-
fn type_param_list(p: &mut Parser) {
87-
if !p.at(L_ANGLE) {
88-
return;
89-
}
90-
let m = p.start();
91-
p.bump();
92-
93-
while !p.at(EOF) && !p.at(R_ANGLE) {
94-
match p.current() {
95-
LIFETIME => lifetime_param(p),
96-
IDENT => type_param(p),
97-
_ => p.err_and_bump("expected type parameter"),
98-
}
99-
if !p.at(R_ANGLE) && !p.expect(COMMA) {
100-
break;
101-
}
102-
}
103-
p.expect(R_ANGLE);
104-
m.complete(p, TYPE_PARAM_LIST);
105-
106-
fn lifetime_param(p: &mut Parser) {
107-
assert!(p.at(LIFETIME));
108-
let m = p.start();
109-
p.bump();
110-
if p.eat(COLON) {
111-
while p.at(LIFETIME) {
112-
p.bump();
113-
if !p.eat(PLUS) {
114-
break;
115-
}
116-
}
117-
}
118-
m.complete(p, LIFETIME_PARAM);
119-
}
120-
121-
fn type_param(p: &mut Parser) {
122-
assert!(p.at(IDENT));
123-
let m = p.start();
124-
p.bump();
125-
if p.eat(COLON) {
126-
loop {
127-
let has_paren = p.eat(L_PAREN);
128-
p.eat(QUESTION);
129-
if p.at(FOR_KW) {
130-
//TODO
131-
}
132-
if p.at(LIFETIME) {
133-
p.bump();
134-
} else if paths::is_path_start(p) {
135-
paths::type_path(p);
136-
} else {
137-
break;
138-
}
139-
if has_paren {
140-
p.expect(R_PAREN);
141-
}
142-
if !p.eat(PLUS) {
143-
break;
144-
}
145-
}
146-
}
147-
if p.at(EQ) {
148-
types::type_ref(p)
149-
}
150-
m.complete(p, TYPE_PARAM);
151-
}
152-
}
153-
154-
fn where_clause(_: &mut Parser) {}
155-
15686
fn extern_crate_item(p: &mut Parser) {
15787
assert!(p.at(EXTERN_KW));
15888
p.bump();

src/parser/event_parser/grammar/items/structs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ pub(super) fn struct_item(p: &mut Parser) {
77
if !p.expect(IDENT) {
88
return;
99
}
10-
type_param_list(p);
10+
type_params::list(p);
1111
match p.current() {
1212
WHERE_KW => {
13-
where_clause(p);
13+
type_params::where_clause(p);
1414
match p.current() {
1515
SEMI => {
1616
p.bump();
@@ -44,8 +44,8 @@ pub(super) fn enum_item(p: &mut Parser) {
4444
assert!(p.at(ENUM_KW));
4545
p.bump();
4646
p.expect(IDENT);
47-
type_param_list(p);
48-
where_clause(p);
47+
type_params::list(p);
48+
type_params::where_clause(p);
4949
if p.expect(L_CURLY) {
5050
while !p.at(EOF) && !p.at(R_CURLY) {
5151
let var = p.start();

src/parser/event_parser/grammar/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod attributes;
77
mod expressions;
88
mod types;
99
mod paths;
10+
mod type_params;
1011

1112
pub(crate) fn file(p: &mut Parser) {
1213
let file = p.start();
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use super::*;
2+
3+
pub(super) fn list(p: &mut Parser) {
4+
if !p.at(L_ANGLE) {
5+
return;
6+
}
7+
let m = p.start();
8+
p.bump();
9+
10+
while !p.at(EOF) && !p.at(R_ANGLE) {
11+
match p.current() {
12+
LIFETIME => lifetime_param(p),
13+
IDENT => type_param(p),
14+
_ => p.err_and_bump("expected type parameter"),
15+
}
16+
if !p.at(R_ANGLE) && !p.expect(COMMA) {
17+
break;
18+
}
19+
}
20+
p.expect(R_ANGLE);
21+
m.complete(p, TYPE_PARAM_LIST);
22+
23+
fn lifetime_param(p: &mut Parser) {
24+
assert!(p.at(LIFETIME));
25+
let m = p.start();
26+
p.bump();
27+
if p.eat(COLON) {
28+
while p.at(LIFETIME) {
29+
p.bump();
30+
if !p.eat(PLUS) {
31+
break;
32+
}
33+
}
34+
}
35+
m.complete(p, LIFETIME_PARAM);
36+
}
37+
38+
fn type_param(p: &mut Parser) {
39+
assert!(p.at(IDENT));
40+
let m = p.start();
41+
p.bump();
42+
if p.eat(COLON) {
43+
loop {
44+
let has_paren = p.eat(L_PAREN);
45+
p.eat(QUESTION);
46+
if p.at(FOR_KW) {
47+
//TODO
48+
}
49+
if p.at(LIFETIME) {
50+
p.bump();
51+
} else if paths::is_path_start(p) {
52+
paths::type_path(p);
53+
} else {
54+
break;
55+
}
56+
if has_paren {
57+
p.expect(R_PAREN);
58+
}
59+
if !p.eat(PLUS) {
60+
break;
61+
}
62+
}
63+
}
64+
if p.at(EQ) {
65+
types::type_ref(p)
66+
}
67+
m.complete(p, TYPE_PARAM);
68+
}
69+
}
70+
71+
pub(super) fn where_clause(p: &mut Parser) {
72+
if p.at(WHERE_KW) {
73+
p.bump();
74+
}
75+
}

0 commit comments

Comments
 (0)