diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-02-11 09:58:32 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-02-11 09:58:32 +0000 |
commit | 7176029803fe2e00f29ab7d20a384e3ee6f53ba3 (patch) | |
tree | 51e7251231162d5d31b6e18fabdbb466ffb94d19 /src/parser | |
parent | e19d038a0e1d9af8270450c5fe8fbbdf0f15cb24 (diff) | |
parent | 96437b050f2743fef2e37eaab7259f2f98f9b473 (diff) |
Merge #48
48: Types r=matklad a=matklad
bors r+
Diffstat (limited to 'src/parser')
-rw-r--r-- | src/parser/grammar/items/consts.rs | 2 | ||||
-rw-r--r-- | src/parser/grammar/items/mod.rs | 19 | ||||
-rw-r--r-- | src/parser/grammar/items/structs.rs | 4 | ||||
-rw-r--r-- | src/parser/grammar/mod.rs | 24 | ||||
-rw-r--r-- | src/parser/grammar/type_params.rs | 2 | ||||
-rw-r--r-- | src/parser/grammar/types.rs | 131 |
6 files changed, 156 insertions, 26 deletions
diff --git a/src/parser/grammar/items/consts.rs b/src/parser/grammar/items/consts.rs index 5f3cf58c2..d6c3753b3 100644 --- a/src/parser/grammar/items/consts.rs +++ b/src/parser/grammar/items/consts.rs | |||
@@ -14,7 +14,7 @@ fn const_or_static(p: &mut Parser, kw: SyntaxKind) { | |||
14 | p.eat(MUT_KW); // TODO: validator to forbid const mut | 14 | p.eat(MUT_KW); // TODO: validator to forbid const mut |
15 | name(p); | 15 | name(p); |
16 | p.expect(COLON); | 16 | p.expect(COLON); |
17 | types::ty(p); | 17 | types::type_(p); |
18 | p.expect(EQ); | 18 | p.expect(EQ); |
19 | expressions::expr(p); | 19 | expressions::expr(p); |
20 | p.expect(SEMI); | 20 | p.expect(SEMI); |
diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index f1776e0e2..18ee8af86 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs | |||
@@ -222,12 +222,6 @@ fn fn_item(p: &mut Parser) { | |||
222 | p.expect(L_CURLY); | 222 | p.expect(L_CURLY); |
223 | p.expect(R_CURLY); | 223 | p.expect(R_CURLY); |
224 | } | 224 | } |
225 | |||
226 | fn fn_value_parameters(p: &mut Parser) { | ||
227 | assert!(p.at(L_PAREN)); | ||
228 | p.bump(); | ||
229 | p.expect(R_PAREN); | ||
230 | } | ||
231 | } | 225 | } |
232 | 226 | ||
233 | // test type_item | 227 | // test type_item |
@@ -247,7 +241,7 @@ fn type_item(p: &mut Parser) { | |||
247 | type_params::where_clause(p); | 241 | type_params::where_clause(p); |
248 | 242 | ||
249 | p.expect(EQ); | 243 | p.expect(EQ); |
250 | types::ty(p); | 244 | types::type_(p); |
251 | p.expect(SEMI); | 245 | p.expect(SEMI); |
252 | } | 246 | } |
253 | 247 | ||
@@ -263,14 +257,3 @@ fn mod_item(p: &mut Parser) { | |||
263 | } | 257 | } |
264 | } | 258 | } |
265 | } | 259 | } |
266 | |||
267 | fn abi(p: &mut Parser) { | ||
268 | assert!(p.at(EXTERN_KW)); | ||
269 | let abi = p.start(); | ||
270 | p.bump(); | ||
271 | match p.current() { | ||
272 | STRING | RAW_STRING => p.bump(), | ||
273 | _ => (), | ||
274 | } | ||
275 | abi.complete(p, ABI); | ||
276 | } | ||
diff --git a/src/parser/grammar/items/structs.rs b/src/parser/grammar/items/structs.rs index ad18fd270..c72b50808 100644 --- a/src/parser/grammar/items/structs.rs +++ b/src/parser/grammar/items/structs.rs | |||
@@ -89,7 +89,7 @@ fn named_fields(p: &mut Parser) { | |||
89 | if p.at(IDENT) { | 89 | if p.at(IDENT) { |
90 | name(p); | 90 | name(p); |
91 | p.expect(COLON); | 91 | p.expect(COLON); |
92 | types::ty(p); | 92 | types::type_(p); |
93 | field.complete(p, NAMED_FIELD); | 93 | field.complete(p, NAMED_FIELD); |
94 | } else { | 94 | } else { |
95 | field.abandon(p); | 95 | field.abandon(p); |
@@ -105,7 +105,7 @@ fn pos_fields(p: &mut Parser) { | |||
105 | while !p.at(R_PAREN) && !p.at(EOF) { | 105 | while !p.at(R_PAREN) && !p.at(EOF) { |
106 | let pos_field = p.start(); | 106 | let pos_field = p.start(); |
107 | visibility(p); | 107 | visibility(p); |
108 | types::ty(p); | 108 | types::type_(p); |
109 | pos_field.complete(p, POS_FIELD); | 109 | pos_field.complete(p, POS_FIELD); |
110 | 110 | ||
111 | if !p.at(R_PAREN) { | 111 | if !p.at(R_PAREN) { |
diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index abf9fe86c..5266354c1 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs | |||
@@ -50,6 +50,30 @@ fn alias(p: &mut Parser) -> bool { | |||
50 | true //FIXME: return false if three are errors | 50 | true //FIXME: return false if three are errors |
51 | } | 51 | } |
52 | 52 | ||
53 | fn abi(p: &mut Parser) { | ||
54 | assert!(p.at(EXTERN_KW)); | ||
55 | let abi = p.start(); | ||
56 | p.bump(); | ||
57 | match p.current() { | ||
58 | STRING | RAW_STRING => p.bump(), | ||
59 | _ => (), | ||
60 | } | ||
61 | abi.complete(p, ABI); | ||
62 | } | ||
63 | |||
64 | fn fn_value_parameters(p: &mut Parser) { | ||
65 | assert!(p.at(L_PAREN)); | ||
66 | p.bump(); | ||
67 | p.expect(R_PAREN); | ||
68 | } | ||
69 | |||
70 | fn fn_ret_type(p: &mut Parser) { | ||
71 | if p.at(THIN_ARROW) { | ||
72 | p.bump(); | ||
73 | types::type_(p); | ||
74 | } | ||
75 | } | ||
76 | |||
53 | fn name(p: &mut Parser) { | 77 | fn name(p: &mut Parser) { |
54 | if p.at(IDENT) { | 78 | if p.at(IDENT) { |
55 | let m = p.start(); | 79 | let m = p.start(); |
diff --git a/src/parser/grammar/type_params.rs b/src/parser/grammar/type_params.rs index 2462b260e..9ea08a55c 100644 --- a/src/parser/grammar/type_params.rs +++ b/src/parser/grammar/type_params.rs | |||
@@ -62,7 +62,7 @@ pub(super) fn list(p: &mut Parser) { | |||
62 | } | 62 | } |
63 | } | 63 | } |
64 | if p.at(EQ) { | 64 | if p.at(EQ) { |
65 | types::ty(p) | 65 | types::type_(p) |
66 | } | 66 | } |
67 | m.complete(p, TYPE_PARAM); | 67 | m.complete(p, TYPE_PARAM); |
68 | } | 68 | } |
diff --git a/src/parser/grammar/types.rs b/src/parser/grammar/types.rs index 71801d8ef..a4967a00a 100644 --- a/src/parser/grammar/types.rs +++ b/src/parser/grammar/types.rs | |||
@@ -1,8 +1,14 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | 2 | ||
3 | pub(super) fn ty(p: &mut Parser) { | 3 | pub(super) fn type_(p: &mut Parser) { |
4 | match p.current() { | 4 | match p.current() { |
5 | L_PAREN => paren_or_tuple_ty(p), | 5 | L_PAREN => paren_or_tuple_type(p), |
6 | EXCL => never_type(p), | ||
7 | STAR => pointer_type(p), | ||
8 | L_BRACK => array_or_slice_type(p), | ||
9 | AMPERSAND => reference_type(p), | ||
10 | UNDERSCORE => placeholder_type(p), | ||
11 | FN_KW | UNSAFE_KW | EXTERN_KW => fn_pointer_type(p), | ||
6 | IDENT => path_type(p), | 12 | IDENT => path_type(p), |
7 | _ => { | 13 | _ => { |
8 | p.error("expected type"); | 14 | p.error("expected type"); |
@@ -10,7 +16,11 @@ pub(super) fn ty(p: &mut Parser) { | |||
10 | } | 16 | } |
11 | } | 17 | } |
12 | 18 | ||
13 | fn paren_or_tuple_ty(p: &mut Parser) { | 19 | fn type_no_plus(p: &mut Parser) { |
20 | type_(p); | ||
21 | } | ||
22 | |||
23 | fn paren_or_tuple_type(p: &mut Parser) { | ||
14 | assert!(p.at(L_PAREN)); | 24 | assert!(p.at(L_PAREN)); |
15 | let m = p.start(); | 25 | let m = p.start(); |
16 | p.bump(); | 26 | p.bump(); |
@@ -18,7 +28,7 @@ fn paren_or_tuple_ty(p: &mut Parser) { | |||
18 | let mut trailing_comma: bool = false; | 28 | let mut trailing_comma: bool = false; |
19 | while !p.at(EOF) && !p.at(R_PAREN) { | 29 | while !p.at(EOF) && !p.at(R_PAREN) { |
20 | n_types += 1; | 30 | n_types += 1; |
21 | ty(p); | 31 | type_(p); |
22 | if p.eat(COMMA) { | 32 | if p.eat(COMMA) { |
23 | trailing_comma = true; | 33 | trailing_comma = true; |
24 | } else { | 34 | } else { |
@@ -43,6 +53,119 @@ fn paren_or_tuple_ty(p: &mut Parser) { | |||
43 | m.complete(p, kind); | 53 | m.complete(p, kind); |
44 | } | 54 | } |
45 | 55 | ||
56 | // test never_type | ||
57 | // type Never = !; | ||
58 | fn never_type(p: &mut Parser) { | ||
59 | assert!(p.at(EXCL)); | ||
60 | let m = p.start(); | ||
61 | p.bump(); | ||
62 | m.complete(p, NEVER_TYPE); | ||
63 | } | ||
64 | |||
65 | fn pointer_type(p: &mut Parser) { | ||
66 | assert!(p.at(STAR)); | ||
67 | let m = p.start(); | ||
68 | p.bump(); | ||
69 | |||
70 | match p.current() { | ||
71 | // test pointer_type_mut | ||
72 | // type M = *mut (); | ||
73 | // type C = *mut (); | ||
74 | MUT_KW | CONST_KW => p.bump(), | ||
75 | _ => { | ||
76 | // test pointer_type_no_mutability | ||
77 | // type T = *(); | ||
78 | p.error( | ||
79 | "expected mut or const in raw pointer type \ | ||
80 | (use `*mut T` or `*const T` as appropriate)", | ||
81 | ); | ||
82 | } | ||
83 | }; | ||
84 | |||
85 | type_no_plus(p); | ||
86 | m.complete(p, POINTER_TYPE); | ||
87 | } | ||
88 | |||
89 | fn array_or_slice_type(p: &mut Parser) { | ||
90 | assert!(p.at(L_BRACK)); | ||
91 | let m = p.start(); | ||
92 | p.bump(); | ||
93 | |||
94 | type_(p); | ||
95 | let kind = match p.current() { | ||
96 | // test slice_type | ||
97 | // type T = [()]; | ||
98 | R_BRACK => { | ||
99 | p.bump(); | ||
100 | SLICE_TYPE | ||
101 | } | ||
102 | |||
103 | // test array_type | ||
104 | // type T = [(); 92]; | ||
105 | SEMI => { | ||
106 | p.bump(); | ||
107 | expressions::expr(p); | ||
108 | p.expect(R_BRACK); | ||
109 | ARRAY_TYPE | ||
110 | } | ||
111 | // test array_type_missing_semi | ||
112 | // type T = [() 92]; | ||
113 | _ => { | ||
114 | p.error("expected `;` or `]`"); | ||
115 | SLICE_TYPE | ||
116 | } | ||
117 | }; | ||
118 | m.complete(p, kind); | ||
119 | } | ||
120 | |||
121 | // test reference_type; | ||
122 | // type A = &(); | ||
123 | // type B = &'static (); | ||
124 | // type C = &mut (); | ||
125 | fn reference_type(p: &mut Parser) { | ||
126 | assert!(p.at(AMPERSAND)); | ||
127 | let m = p.start(); | ||
128 | p.bump(); | ||
129 | p.eat(LIFETIME); | ||
130 | p.eat(MUT_KW); | ||
131 | type_no_plus(p); | ||
132 | m.complete(p, REFERENCE_TYPE); | ||
133 | } | ||
134 | |||
135 | // test placeholder_type | ||
136 | // type Placeholder = _; | ||
137 | fn placeholder_type(p: &mut Parser) { | ||
138 | assert!(p.at(UNDERSCORE)); | ||
139 | let m = p.start(); | ||
140 | p.bump(); | ||
141 | m.complete(p, PLACEHOLDER_TYPE); | ||
142 | } | ||
143 | |||
144 | // test fn_pointer_type | ||
145 | // type A = fn(); | ||
146 | // type B = unsafe fn(); | ||
147 | // type C = unsafe extern "C" fn(); | ||
148 | fn fn_pointer_type(p: &mut Parser) { | ||
149 | let m = p.start(); | ||
150 | p.eat(UNSAFE_KW); | ||
151 | if p.at(EXTERN_KW) { | ||
152 | abi(p); | ||
153 | } | ||
154 | // test fn_pointer_type_missing_fn | ||
155 | // type F = unsafe (); | ||
156 | if !p.eat(FN_KW) { | ||
157 | m.abandon(p); | ||
158 | p.error("expected `fn`"); | ||
159 | return; | ||
160 | } | ||
161 | |||
162 | fn_value_parameters(p); | ||
163 | // test fn_pointer_type_with_ret | ||
164 | // type F = fn() -> (); | ||
165 | fn_ret_type(p); | ||
166 | m.complete(p, FN_POINTER_TYPE); | ||
167 | } | ||
168 | |||
46 | fn path_type(p: &mut Parser) { | 169 | fn path_type(p: &mut Parser) { |
47 | assert!(p.at(IDENT)); | 170 | assert!(p.at(IDENT)); |
48 | let m = p.start(); | 171 | let m = p.start(); |