Skip to content

Commit f9ecc68

Browse files
committed
Merge #35
35: Move use r=matklad a=matklad
2 parents a1fcead + be20b01 commit f9ecc68

File tree

6 files changed

+153
-146
lines changed

6 files changed

+153
-146
lines changed

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

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22

33
mod structs;
4+
mod use_item;
45

56
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
67
attributes::inner_attributes(p);
@@ -20,7 +21,7 @@ fn item(p: &mut Parser) {
2021
let la = p.nth(1);
2122
let item_kind = match p.current() {
2223
USE_KW => {
23-
use_item(p);
24+
use_item::use_item(p);
2425
USE_ITEM
2526
}
2627
EXTERN_KW if la == CRATE_KW => {
@@ -82,76 +83,6 @@ fn item(p: &mut Parser) {
8283
item.complete(p, item_kind);
8384
}
8485

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

182-
pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool {
183-
kind == STAR || kind == L_CURLY
184-
}
185-
186-
fn use_item(p: &mut Parser) {
187-
assert!(p.at(USE_KW));
188-
p.bump();
189-
190-
use_tree(p);
191-
p.expect(SEMI);
192-
193-
fn use_tree(p: &mut Parser) {
194-
let la = p.nth(1);
195-
let m = p.start();
196-
match (p.current(), la) {
197-
(STAR, _) => p.bump(),
198-
(COLONCOLON, STAR) => {
199-
p.bump();
200-
p.bump();
201-
}
202-
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
203-
if p.at(COLONCOLON) {
204-
p.bump();
205-
}
206-
nested_trees(p);
207-
}
208-
_ if paths::is_path_start(p) => {
209-
paths::use_path(p);
210-
match p.current() {
211-
AS_KW => {
212-
alias(p);
213-
}
214-
COLONCOLON => {
215-
p.bump();
216-
match p.current() {
217-
STAR => {
218-
p.bump();
219-
}
220-
L_CURLY => nested_trees(p),
221-
_ => {
222-
// is this unreachable?
223-
p.error().message("expected `{` or `*`").emit();
224-
}
225-
}
226-
}
227-
_ => (),
228-
}
229-
}
230-
_ => {
231-
m.abandon(p);
232-
p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
233-
return;
234-
}
235-
}
236-
m.complete(p, USE_TREE);
237-
}
238-
239-
fn nested_trees(p: &mut Parser) {
240-
assert!(p.at(L_CURLY));
241-
p.bump();
242-
while !p.at(EOF) && !p.at(R_CURLY) {
243-
use_tree(p);
244-
if !p.at(R_CURLY) {
245-
p.expect(COMMA);
246-
}
247-
}
248-
p.expect(R_CURLY);
249-
}
250-
}
251-
252113
fn abi(p: &mut Parser) {
253114
assert!(p.at(EXTERN_KW));
254115
let abi = p.start();

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();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use super::*;
2+
3+
pub(super) fn use_item(p: &mut Parser) {
4+
assert!(p.at(USE_KW));
5+
p.bump();
6+
use_tree(p);
7+
p.expect(SEMI);
8+
}
9+
10+
fn use_tree(p: &mut Parser) {
11+
let la = p.nth(1);
12+
let m = p.start();
13+
match (p.current(), la) {
14+
(STAR, _) => p.bump(),
15+
(COLONCOLON, STAR) => {
16+
p.bump();
17+
p.bump();
18+
}
19+
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
20+
if p.at(COLONCOLON) {
21+
p.bump();
22+
}
23+
nested_trees(p);
24+
}
25+
_ if paths::is_path_start(p) => {
26+
paths::use_path(p);
27+
match p.current() {
28+
AS_KW => {
29+
alias(p);
30+
}
31+
COLONCOLON => {
32+
p.bump();
33+
match p.current() {
34+
STAR => {
35+
p.bump();
36+
}
37+
L_CURLY => nested_trees(p),
38+
_ => {
39+
// is this unreachable?
40+
p.error().message("expected `{` or `*`").emit();
41+
}
42+
}
43+
}
44+
_ => (),
45+
}
46+
}
47+
_ => {
48+
m.abandon(p);
49+
p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
50+
return;
51+
}
52+
}
53+
m.complete(p, USE_TREE);
54+
}
55+
56+
fn nested_trees(p: &mut Parser) {
57+
assert!(p.at(L_CURLY));
58+
p.bump();
59+
while !p.at(EOF) && !p.at(R_CURLY) {
60+
use_tree(p);
61+
if !p.at(R_CURLY) {
62+
p.expect(COMMA);
63+
}
64+
}
65+
p.expect(R_CURLY);
66+
}

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();

src/parser/event_parser/grammar/paths.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ fn path(p: &mut Parser) {
2020
path_segment(p, true);
2121
let mut qual = path.complete(p, PATH);
2222
loop {
23-
if p.at(COLONCOLON) && !items::is_use_tree_start(p.nth(1)) {
23+
let use_tree = match p.nth(1) {
24+
STAR | L_CURLY => true,
25+
_ => false,
26+
};
27+
if p.at(COLONCOLON) && !use_tree {
2428
let path = qual.precede(p);
2529
p.bump();
2630
path_segment(p, false);
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)