aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-02-02 20:23:39 +0000
committerAleksey Kladov <[email protected]>2018-02-02 20:23:39 +0000
commitbe20b014d9fd4df64a8b6b88aaf6950d44ca6f39 (patch)
treec15234d193badb99117c3f4a4b53a4d76c243f90 /src/parser
parent7cdf990c40e932a466e8799ca3fa582467d32c91 (diff)
Move type parameter parsing to a separate file
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/event_parser/grammar/items/mod.rs70
-rw-r--r--src/parser/event_parser/grammar/items/structs.rs8
-rw-r--r--src/parser/event_parser/grammar/mod.rs1
-rw-r--r--src/parser/event_parser/grammar/type_params.rs75
4 files changed, 80 insertions, 74 deletions
diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs
index 1c092779b..35825e7c4 100644
--- a/src/parser/event_parser/grammar/items/mod.rs
+++ b/src/parser/event_parser/grammar/items/mod.rs
@@ -83,76 +83,6 @@ fn item(p: &mut Parser) {
83 item.complete(p, item_kind); 83 item.complete(p, item_kind);
84} 84}
85 85
86fn 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
154fn where_clause(_: &mut Parser) {}
155
156fn extern_crate_item(p: &mut Parser) { 86fn extern_crate_item(p: &mut Parser) {
157 assert!(p.at(EXTERN_KW)); 87 assert!(p.at(EXTERN_KW));
158 p.bump(); 88 p.bump();
diff --git a/src/parser/event_parser/grammar/items/structs.rs b/src/parser/event_parser/grammar/items/structs.rs
index 6e438413b..69d95c698 100644
--- a/src/parser/event_parser/grammar/items/structs.rs
+++ b/src/parser/event_parser/grammar/items/structs.rs
@@ -7,10 +7,10 @@ pub(super) fn struct_item(p: &mut Parser) {
7 if !p.expect(IDENT) { 7 if !p.expect(IDENT) {
8 return; 8 return;
9 } 9 }
10 type_param_list(p); 10 type_params::list(p);
11 match p.current() { 11 match p.current() {
12 WHERE_KW => { 12 WHERE_KW => {
13 where_clause(p); 13 type_params::where_clause(p);
14 match p.current() { 14 match p.current() {
15 SEMI => { 15 SEMI => {
16 p.bump(); 16 p.bump();
@@ -44,8 +44,8 @@ pub(super) fn enum_item(p: &mut Parser) {
44 assert!(p.at(ENUM_KW)); 44 assert!(p.at(ENUM_KW));
45 p.bump(); 45 p.bump();
46 p.expect(IDENT); 46 p.expect(IDENT);
47 type_param_list(p); 47 type_params::list(p);
48 where_clause(p); 48 type_params::where_clause(p);
49 if p.expect(L_CURLY) { 49 if p.expect(L_CURLY) {
50 while !p.at(EOF) && !p.at(R_CURLY) { 50 while !p.at(EOF) && !p.at(R_CURLY) {
51 let var = p.start(); 51 let var = p.start();
diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs
index e7f1915d2..afce308d0 100644
--- a/src/parser/event_parser/grammar/mod.rs
+++ b/src/parser/event_parser/grammar/mod.rs
@@ -7,6 +7,7 @@ mod attributes;
7mod expressions; 7mod expressions;
8mod types; 8mod types;
9mod paths; 9mod paths;
10mod type_params;
10 11
11pub(crate) fn file(p: &mut Parser) { 12pub(crate) fn file(p: &mut Parser) {
12 let file = p.start(); 13 let file = p.start();
diff --git a/src/parser/event_parser/grammar/type_params.rs b/src/parser/event_parser/grammar/type_params.rs
new file mode 100644
index 000000000..12c9a5362
--- /dev/null
+++ b/src/parser/event_parser/grammar/type_params.rs
@@ -0,0 +1,75 @@
1use super::*;
2
3pub(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
71pub(super) fn where_clause(p: &mut Parser) {
72 if p.at(WHERE_KW) {
73 p.bump();
74 }
75}