1use super::{MAX_TUPLE_ARITY, ParamMode, Parser};
2use crate::EcoString;
3use crate::ast::{Annotation, Attribute, Expression, Generic, Span, Visibility};
4use crate::lex::Token;
5use crate::lex::TokenKind::*;
6use crate::types::Type;
7
8impl<'source> Parser<'source> {
9 pub fn parse_annotation(&mut self) -> Annotation {
10 if !self.enter_recursion() {
11 self.resync_on_error();
12 return Annotation::Unknown;
13 }
14 let result = self.parse_annotation_inner();
15 self.leave_recursion();
16 result
17 }
18
19 fn parse_annotation_inner(&mut self) -> Annotation {
20 match self.current_token().kind {
21 Function => self.parse_function_annotation(),
22 LeftParen => self.parse_tuple_annotation(),
23 LeftSquareBracket => {
24 let start = self.current_token();
25 self.next();
26 if self.advance_if(RightSquareBracket) {
27 let type_token = self.current_token();
28 let type_name = if type_token.kind == Identifier {
29 type_token.text.to_string()
30 } else {
31 "T".to_string()
32 };
33 let span_end = if type_token.kind == Identifier {
34 type_token.byte_offset + type_token.byte_length
35 } else {
36 start.byte_offset + 2
37 };
38 let error_span = Span::new(
39 self.file_id,
40 start.byte_offset,
41 span_end - start.byte_offset,
42 );
43 self.track_error_at(
44 error_span,
45 "invalid syntax for `Slice`",
46 format!("Use `Slice<{}>` instead of `[]{}`", type_name, type_name),
47 );
48 if self.current_token().kind == Identifier {
49 return self.parse_named_annotation();
50 }
51 return Annotation::Constructor {
52 name: "Slice".into(),
53 params: vec![],
54 span: error_span,
55 };
56 }
57 let span = self.span_from_tokens(start);
58 self.track_error("unexpected `[` in type", "Use `Slice<T>` for slice types.");
59 Annotation::Constructor {
60 name: "".into(),
61 params: vec![],
62 span,
63 }
64 }
65 _ => self.parse_named_annotation(),
66 }
67 }
68
69 fn parse_named_annotation(&mut self) -> Annotation {
70 let start = self.current_token();
71 let name = self.read_identifier_sequence();
72
73 if self.advance_if(LeftAngleBracket) {
74 let mut type_params = vec![];
75
76 while self.can_start_annotation() {
77 type_params.push(self.parse_annotation());
78 match self.current_token().kind {
79 RightAngleBracket | ShiftRight => break,
80 Comma => self.next(),
81 _ => break,
82 }
83 if self.is_right_angle_like() {
84 self.track_error("expected type", "Add a type or remove the trailing comma.");
85 }
86 }
87
88 if !self.advance_if_right_angle() {
89 self.track_error("expected `>`", "Add `>` to close the type arguments.");
90 }
91
92 return Annotation::Constructor {
93 name,
94 params: type_params,
95 span: self.span_from_tokens(start),
96 };
97 }
98
99 if matches!(self.current_token().kind, LeftParen | LeftSquareBracket) {
100 return self.parse_misdelimited_type_args(name, start);
101 }
102
103 Annotation::Constructor {
104 name,
105 params: vec![],
106 span: self.span_from_tokens(start),
107 }
108 }
109
110 fn parse_misdelimited_type_args(
111 &mut self,
112 name: EcoString,
113 start: Token<'source>,
114 ) -> Annotation {
115 let open = self.current_token();
116 let close_kind = if open.kind == LeftParen {
117 RightParen
118 } else {
119 RightSquareBracket
120 };
121 let open_end = (open.byte_offset + open.byte_length) as usize;
122 self.next();
123
124 let mut type_params = vec![];
125 while self.current_token().kind != close_kind && !self.at_eof() {
126 if !self.can_start_annotation() {
127 break;
128 }
129 type_params.push(self.parse_annotation());
130 if !self.advance_if(Comma) {
131 break;
132 }
133 }
134
135 while self.current_token().kind != close_kind && !self.at_eof() {
136 self.next();
137 }
138
139 let close = self.current_token();
140 let close_start = close.byte_offset as usize;
141 let consumed = self.advance_if(close_kind);
142 let close_end = if consumed {
143 (close.byte_offset + close.byte_length) as usize
144 } else {
145 close_start
146 };
147
148 let inner = self.source[open_end..close_start].trim();
149 let corrected = format!("{}<{}>", name, inner);
150 let original = &self.source[start.byte_offset as usize..close_end];
151
152 let label_span = Span::new(
153 self.file_id,
154 open.byte_offset,
155 (close_end as u32).saturating_sub(open.byte_offset),
156 );
157 self.error_angle_brackets_for_generics(
158 label_span,
159 format!("Write `{corrected}` instead of `{original}`"),
160 );
161
162 Annotation::Constructor {
163 name,
164 params: type_params,
165 span: self.span_from_tokens(start),
166 }
167 }
168
169 fn parse_function_annotation(&mut self) -> Annotation {
170 let start = self.current_token();
171 self.ensure(Function);
172 self.ensure(LeftParen);
173
174 let mut params = vec![];
175
176 while self.is_not(RightParen) {
177 params.push(self.parse_annotation());
178 self.expect_comma_or(RightParen);
179 }
180
181 self.ensure(RightParen);
182
183 let return_type = self.parse_function_return_annotation();
184
185 Annotation::Function {
186 params,
187 return_type: return_type.into(),
188 span: self.span_from_tokens(start),
189 }
190 }
191
192 fn parse_tuple_annotation(&mut self) -> Annotation {
193 let start = self.current_token();
194 self.ensure(LeftParen);
195
196 let mut annotations = vec![];
197 let mut has_trailing_comma = false;
198
199 while self.is_not(RightParen) {
200 annotations.push(self.parse_annotation());
201 has_trailing_comma = self.is(Comma);
202 self.expect_comma_or(RightParen);
203 }
204
205 self.ensure(RightParen);
206
207 let span = self.span_from_tokens(start);
208
209 if annotations.is_empty() {
210 return Annotation::unit();
211 }
212
213 if annotations.len() == 1 {
214 if has_trailing_comma {
215 self.error_tuple_arity(1, span);
216 }
217 return annotations.into_iter().next().expect("len is 1");
218 }
219
220 if annotations.len() > MAX_TUPLE_ARITY {
221 self.error_tuple_arity(annotations.len(), span);
222 }
223
224 Annotation::Tuple {
225 elements: annotations,
226 span,
227 }
228 }
229
230 pub fn parse_generics(&mut self) -> Vec<Generic> {
231 if !self.advance_if(LeftAngleBracket) {
232 return vec![];
233 }
234
235 let mut generics = vec![];
236
237 while !self.is_right_angle_like() && !self.at_eof() {
238 generics.push(self.parse_generic());
239 self.expect_comma_or(RightAngleBracket);
240 }
241
242 if !self.advance_if_right_angle() {
243 self.ensure(RightAngleBracket);
244 }
245
246 generics
247 }
248
249 fn parse_generic(&mut self) -> Generic {
250 let start = self.current_token();
251
252 Generic {
253 name: self.read_identifier(),
254 bounds: self.parse_generic_bounds(),
255 span: self.span_from_tokens(start),
256 }
257 }
258
259 fn parse_generic_bounds(&mut self) -> Vec<Annotation> {
260 if !self.advance_if(Colon) {
261 return vec![];
262 }
263
264 if self.is_right_angle_like() || self.is(Comma) {
265 self.track_error(
266 "expected bound after `:`",
267 "Provide a bound like `T: Display`.",
268 );
269 return vec![];
270 }
271
272 let mut bounds = vec![];
273
274 while !self.is_right_angle_like() && self.is_not(Comma) {
275 bounds.push(self.parse_annotation());
276 if self.is_right_angle_like() || self.is(Comma) {
277 break;
278 }
279 if !self.advance_if(Plus) {
280 self.track_error(
281 "missing `+` between bounds",
282 "Use `+` to separate multiple bounds.",
283 );
284 break;
285 }
286 if self.is_right_angle_like() || self.is(Comma) {
287 self.track_error(
288 "expected bound after `+`",
289 "Provide a bound or remove the trailing `+`.",
290 );
291 }
292 }
293
294 bounds
295 }
296
297 pub fn parse_function_return_annotation(&mut self) -> Annotation {
298 if self.advance_if(Arrow) {
299 return self.parse_annotation();
300 }
301
302 Annotation::Unknown
303 }
304
305 pub fn parse_interface_method(
306 &mut self,
307 doc: Option<std::string::String>,
308 attributes: Vec<crate::ast::Attribute>,
309 ) -> Expression {
310 self.ensure(Function);
311
312 let start = self.current_token();
313 let name_token = self.current_token();
314 let name_span = Span::new(self.file_id, name_token.byte_offset, name_token.byte_length);
315 let name = self.read_identifier();
316
317 if self.is(LeftAngleBracket) {
318 let generics_start = self.current_token();
319 let generics = self.parse_generics(); let generics_span = self.span_from_tokens(generics_start);
321 self.error_interface_method_with_type_parameters(generics_span, generics.len());
322 }
323
324 Expression::Function {
325 doc,
326 attributes,
327 name,
328 name_span,
329 generics: vec![],
330 params: self.parse_function_params(ParamMode::Strict),
331 return_annotation: self.parse_function_return_annotation(),
332 return_type: Type::uninferred(),
333 visibility: Visibility::Private,
334 body: Expression::NoOp.into(),
335 ty: Type::uninferred(),
336 span: self.span_from_tokens(start),
337 }
338 }
339
340 pub fn parse_type_alias_with_doc(
341 &mut self,
342 doc: Option<std::string::String>,
343 attributes: Vec<Attribute>,
344 ) -> Expression {
345 let start = self.current_token();
346
347 self.ensure(Type);
348
349 let name_token = self.current_token();
350 let name_span = Span::new(self.file_id, name_token.byte_offset, name_token.byte_length);
351 let name = self.read_identifier();
352 let generics = self.parse_generics();
353
354 let annotation = if self.advance_if(Equal) {
355 self.parse_annotation()
356 } else {
357 Annotation::Opaque {
358 span: self.span_from_tokens(start),
359 }
360 };
361
362 Expression::TypeAlias {
363 doc,
364 attributes,
365 name,
366 name_span,
367 generics,
368 annotation,
369 ty: Type::uninferred(),
370 visibility: Visibility::Private,
371 span: self.span_from_tokens(start),
372 }
373 }
374}