1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Diagnostics for the Amp compiler.
use crate::codemap::Span;
/// The severity level of a [Label] in a [Diag].
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
Error,
Note,
}
/// A label in a [Diag].
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Label {
pub level: Level,
pub message: String,
pub highlight: Option<Span>,
}
/// A diagnostic, which generally consists of one or more [Label]s.
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Diag {
pub labels: Vec<Label>,
}
impl Diag {
/// Creates a blank [Diag].
#[inline]
pub fn new() -> Self {
Self { labels: Vec::new() }
}
/// Adds a label to the diagnostic.
#[inline]
pub fn label(
mut self,
level: Level,
message: impl Into<String>,
highlight: Option<Span>,
) -> Self {
self.labels.push(Label {
level,
message: message.into(),
highlight,
});
self
}
/// Adds an error label to the diagnostic.
#[inline]
pub fn error(self, message: impl Into<String>, highlight: Option<Span>) -> Self {
self.label(Level::Error, message, highlight)
}
/// Adds a note label to the diagnostic.
#[inline]
pub fn note(self, message: impl Into<String>, highlight: Option<Span>) -> Self {
self.label(Level::Note, message, highlight)
}
}
/// A trait for contexts which [Diag]s can be reported to.
pub trait Report {
/// Reports a diagnostic to this context.
fn report(&mut self, diag: Diag);
}
/// A trait which allows
pub trait SyntaxDiagnostics: Report {
/// Reports that an invalid character was found during tokenization.
///
/// # Params
/// 1. The invalid character which was found.
/// 2. The location where the invalid character was found.
fn invalid_character(&mut self, offending_char: char, offending_span: Span) {
self.report(Diag::new().error(
format!(
"invalid character '{}'",
offending_char.escape_debug().collect::<String>()
),
Some(offending_span),
))
}
/// Reports that an unterminated string was found during tokenization.
///
/// # Params
/// 1. The location of the unterminated string.
fn unterminated_string(&mut self, offending_span: Span) {
self.report(Diag::new().error("unterminated string", Some(offending_span)));
}
/// Reports that an unclosed delimiter was found.
///
/// # Params
/// 1. The span of the unclosed delimiter.
fn unclosed_delimiter(&mut self, offending_span: Span) {
self.report(Diag::new().error("unclosed delimiter", Some(offending_span)));
}
/// Reports that an unclosed delimiter was found.
///
/// # Params
/// 1. The span of the unmatched delimiter.
fn unmatched_closing_delimiter(&mut self, offending_delimiter: &str, offending_span: Span) {
self.report(Diag::new().error(
format!("unmatched closing delimiter '{}'", offending_delimiter),
Some(offending_span),
));
}
/// Reports that a too large integer literal token was found.
///
/// # Params
/// 1. The span of the integer.
fn integer_too_large(&mut self, offending_span: Span) {
self.report(
Diag::new()
.error("integer is too large", Some(offending_span))
.note(format!("currently, {} is the max", u64::MAX), None),
)
}
/// Reports that an invalid parameter was found in an argument list.
///
/// # Params
/// 1. The span of the invalid parameter.
/// 2. The span of the end of the argument list.
fn invalid_arglist_param(&mut self, offending_span: Span, arglist_end: Span) {
self.report(
Diag::new()
.error(
"expected parameter or `)` in argument list",
Some(offending_span),
)
.note("argument list actually ends here", Some(arglist_end)),
)
}
/// Reports that an argument list expected a comma or closing delimiter.
///
/// # Params
/// 1. The span where a comma/delimiter was expected.
/// 2. The span of the end of the argument list.
fn arglist_expected_comma_or_close(&mut self, offending_span: Span, arglist_end: Span) {
self.report(
Diag::new()
.error("expected `,` or `)` here", Some(offending_span))
.note("argument list actually ends here", Some(arglist_end)),
)
}
/// Reports that a binding declaration was missing a valid name identifier.
///
/// # Params
/// 1. The span of the binding starting keyword (`const` or `var`).
fn expected_binding_decl_name(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected a binding identifier", Some(offending_span)))
}
/// Reports that a type annotation was started but no type was found.
///
/// # Params
/// 1. The span of the `:` that started the type annotation.
fn expected_type_annotation_type(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected type in type annotation", Some(offending_span)))
}
/// Reports that a `const` binding was missing a value.
///
/// # Params
/// 1. The span of the `const` keyword to the end of the type annotation/name.
fn expected_const_binding_value(&mut self, offending_span: Span) {
self.report(
Diag::new()
.error("expected value for `const` binding", Some(offending_span))
.note(
"`const` bindings must be declared with a value known at compile time",
None,
),
)
}
/// Reports that an invalid statement was found.
///
/// # Params
/// 1. The span of the statement.
fn invalid_stmnt(&mut self, offending_span: Span) {
self.report(Diag::new().error("invalid statement", Some(offending_span)))
}
/// Reports that a semicolon was expected after an expression.
///
/// # Params
/// 1. The span of the statement a semicolon was expected to follow.
fn expected_semicolon(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected a semicolon", Some(offending_span)))
}
/// Reports that an argument list was expected in a function declaration, such as
/// `func(my_param: i32): i32`.
///
/// # Params
/// 1. The span of the offending `func` declaration, up to the name.
fn expected_function_args(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected function argument list", Some(offending_span)))
}
/// Reports that a return type was expected in a function declaration.
///
/// # Params
/// 1. The span of the function declaration up until the type annotation.
fn expected_function_return_type(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected function return type", Some(offending_span)))
}
/// Reports that an operator was found but no valid operand followed.
///
/// # Params
/// 1. The span of the expression up until the operator.
fn expected_operand_expression(&mut self, offending_span: Span) {
self.report(Diag::new().error("expected expression as operand", Some(offending_span)))
}
}
impl<T: Report> SyntaxDiagnostics for T {}
/// Semantic analysis diagnostics.
pub trait SemaDiagnostics: Report {
/// Reports that there are two symbols in this scope with the same name.
///
/// # Params
/// 1. The name of the problematic declaration.
/// 2. The span of the offending declaration.
/// 3. The span of the first declaration, if any.
fn duplicate_declaration(
&mut self,
name: &str,
offending_span: Span,
original_span: Option<Span>,
) {
let mut diag = Diag::new().error(
format!("'{}' is already declared in this scope", name),
Some(offending_span),
);
if let Some(original_span) = original_span {
diag = diag.note("originally declared here", Some(original_span));
}
self.report(diag)
}
/// Reports that a named value could not be found.
///
/// # Params
/// 1. The name of the requested value.
/// 2. The span of the requested value.
fn could_not_resolve_name(&mut self, name: &str, offending_span: Span) {
self.report(Diag::new().error(
format!("could not resolve '{}'", name),
Some(offending_span),
))
}
/// Creates the base for a type mismatch error.
///
/// # Params
/// 1. The name of the expected type.
/// 2. The name of the type which was found.
/// 3. The span where the value of the invalid type was found.
fn type_mismatch_base(
&mut self,
expected_type: &str,
got_type: &str,
offending_span: Span,
) -> Diag {
Diag::new().error(
format!(
"expected value of type `{}`, got `{}`",
expected_type, got_type
),
Some(offending_span),
)
}
/// A value was used in a type's position.
///
/// # Params
/// 1. The name of the type which was found.
/// 2. The span where the type value was expected.
fn invalid_type_in_type_position(&mut self, got_type: &str, offending_span: Span) {
let diag = self
.type_mismatch_base("type", got_type, offending_span)
.note("values cannot be used in a type's position", None);
self.report(diag);
}
/// Reports that a general type mismatch was found.
///
/// # Params
/// 1. The name of the expected type.
/// 2. The name of the found type.
/// 3. The span of the mismatched value.
fn type_mismatch(&mut self, expected_type: &str, got_type: &str, offending_span: Span) {
let diag = self.type_mismatch_base(expected_type, got_type, offending_span);
self.report(diag);
}
/// Reports that a type mismatch was found in a return statement.
///
/// # Params
/// 1. The name of the expected type.
/// 2. The name of the found type.
/// 3. The span of the mismatched value.
/// 4. The span of the function signature, if applicable (from the `func` keyword to the end
/// of the return type).
fn return_type_mismatch(
&mut self,
expected_type: &str,
got_type: &str,
offending_span: Span,
signature_span: Option<Span>,
) {
let mut diag = self.type_mismatch_base(expected_type, got_type, offending_span);
if let Some(span) = signature_span {
diag = diag.note("function signature declared here", Some(span));
}
self.report(diag.note("return must match function's signature", None));
}
/// Reports that an invalid argument was found.
///
/// # Params
/// 1. The name of the type that was expected.
/// 2. The name of the type which was found.
/// 3. The type of the called function.
/// 4. The span of the invalid argument.
fn function_argument_type_mismatch(
&mut self,
expected_type: &str,
got_type: &str,
callee_type: &str,
offending_span: Span,
) {
let diag = self
.type_mismatch_base(expected_type, got_type, offending_span)
.note(format!("arguments must match `{}`", callee_type), None);
self.report(diag);
}
/// Reports that a function call was invalid for the provided argument length.
///
/// # Params
/// 1. The expected number of arguments.
/// 2. The number of arguments found.
/// 3. The type of the callee (the function being called).
/// 4. The span of the call expression.
fn function_call_argument_length_mismatch(
&mut self,
expected: usize,
got: usize,
callee_type: &str,
offending_span: Span,
) {
self.report(
Diag::new()
.error(
format!("expected {} argument(s), got {}", expected, got),
Some(offending_span),
)
.note(format!("callee is of type `{}`", callee_type), None),
)
}
/// Reports that an attempt was made to call a non-function value.
///
/// # Params
/// 1. The name of the invalid callee's type.
/// 2. The span of the function call expression.
fn calling_non_function_type(&mut self, got_type: &str, offending_span: Span) {
self.report(Diag::new().error(
format!("attempt to call non-function type `{}`", got_type),
Some(offending_span),
))
}
/// Reports that a runtime value was used when a constant value was expected.
///
/// # Params
/// 1. The span of the offending runtime value.
fn value_not_known_at_compile_time(&mut self, offending_span: Span) {
self.report(Diag::new().error(
"this value is not known at compile time",
Some(offending_span),
))
}
/// Reports that an invalid statement was found at the root of a module.
///
/// # Params
/// 1. The span of the invalid statement.
fn invalid_statement_at_root(&mut self, offending_span: Span) {
self.report(Diag::new().error("invalid statement at root of module", Some(offending_span)))
}
/// Reports that an invalid statement was found.
///
/// # Params
/// 1. The span of the invalid statement.
fn invalid_statement(&mut self, offending_span: Span) {
self.report(Diag::new().error("invalid statement", Some(offending_span)))
}
/// Reports that an invalid expression was found.
///
/// # Params
/// 1. The span of the invalid expression.
fn invalid_expression(&mut self, offending_span: Span) {
self.report(Diag::new().error("invalid expression", Some(offending_span)))
}
/// Reports that a `type` cannot be recieved from an argument.
///
/// # Params
/// 1. The span of the invalid argument.
fn cannot_use_type_as_argument(&mut self, offending_span: Span) {
self.report(Diag::new().error(
"functions cannot recieve `type`s as arguments",
Some(offending_span),
))
}
/// Reports that a `type` cannot be outputted as a return value.
///
/// # Params
/// 1. The span of the invalid return type.
fn cannot_use_type_as_return(&mut self, offending_span: Span) {
self.report(Diag::new().error(
"functions cannot output `type`s as return values",
Some(offending_span),
))
}
}
impl<T: Report> SemaDiagnostics for T {}