-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
585 lines (511 loc) · 13.8 KB
/
errors.go
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package main
// WACC Group 34
//
// errors.go: Handles the different types of errors.
//
// File contains functions that return errors given a *token32 and supporting
// information
import (
"fmt"
)
// WACCError is the base error type with filename, line and column number
type WACCError struct {
filename string
line, column int
}
func (e *WACCError) Error() string {
return fmt.Sprintf(
"%s:%d:%d",
e.filename,
e.line,
e.column,
)
}
// CreateWACCError pulls the position information from a *token32
func CreateWACCError(token *token32) WACCError {
return WACCError{
filename: token.filename,
line: token.line,
column: token.column,
}
}
// SyntaxError is the base type for syntax errors
type SyntaxError struct {
WACCError
}
// CreateSyntaxError initializes the WACCError position information
func CreateSyntaxError(token *token32) SyntaxError {
return SyntaxError{
WACCError: CreateWACCError(token),
}
}
func (e *SyntaxError) Error() string {
return fmt.Sprintf(
"%s:syntax error",
e.WACCError.Error(),
)
}
// BigIntError is a syntax error when a number cannot fit the integer size
type BigIntError struct {
SyntaxError
number string
}
// CreateBigIntError creates an error from the token and number as a string
func CreateBigIntError(token *token32, number string) error {
return &BigIntError{
SyntaxError: CreateSyntaxError(token),
number: number,
}
}
func (m *BigIntError) Error() string {
return fmt.Sprintf(
"%s: number '%s' does not fit in integer",
m.SyntaxError.Error(),
m.number,
)
}
// ForLoopError is a syntax error when the for loop isn't in the form
// (DeclareAssignStatement; Cond; AssignStatement)
type ForLoopError struct {
SyntaxError
err string
}
// CreateForLoopError creates an error from the token and err as a string
func CreateForLoopError(token *token32, err string) error {
return &ForLoopError{
SyntaxError: CreateSyntaxError(token),
err: err,
}
}
// Error creates the error string from the ForLoopError
func (m *ForLoopError) Error() string {
return fmt.Sprintf(
"%s: %s",
m.SyntaxError.Error(),
m.err,
)
}
// MissingReturnError is a syntax error when a function does not return
type MissingReturnError struct {
SyntaxError
ident string
}
// CreateMissingReturnError creates an error from the token and the function
// identifier
func CreateMissingReturnError(token *token32, ident string) error {
return &MissingReturnError{
SyntaxError: CreateSyntaxError(token),
ident: ident,
}
}
func (e *MissingReturnError) Error() string {
return fmt.Sprintf(
"%s: expected function '%s' to return",
e.SyntaxError.Error(),
e.ident,
)
}
// UnreachableStatementError is a syntax error when a statement is present after
// return
type UnreachableStatementError struct {
SyntaxError
}
func (e *UnreachableStatementError) Error() string {
return fmt.Sprintf(
"%s: unreachable statement",
e.SyntaxError.Error(),
)
}
// CreateUnreachableStatementError creates an error from the token
func CreateUnreachableStatementError(token *token32) error {
return &UnreachableStatementError{
SyntaxError: CreateSyntaxError(token),
}
}
// SemanticError is the base type for semantic errors
type SemanticError struct {
WACCError
}
// CreateSemanticError initializes the WACCError position information
func CreateSemanticError(token *token32) SemanticError {
return SemanticError{
WACCError: CreateWACCError(token),
}
}
func (e *SemanticError) Error() string {
return fmt.Sprintf(
"%s:semantic error",
e.WACCError.Error(),
)
}
// ContinueNotInLoopError is a semantic error when a continue
// statement is used outside a loop
type ContinueNotInLoopError struct {
SemanticError
}
func (e *ContinueNotInLoopError) Error() string {
return fmt.Sprintf(
"%s: Continue cannot be used outside a loop",
e.SemanticError.Error(),
)
}
// CreateContinueNotInLoopError creates an error from the token
func CreateContinueNotInLoopError(token *token32) error {
return &ContinueNotInLoopError{
SemanticError: CreateSemanticError(token),
}
}
// BreakNotInLoopError is a semantic error when a break
// statement is used outside a loop
type BreakNotInLoopError struct {
SemanticError
}
func (e *BreakNotInLoopError) Error() string {
return fmt.Sprintf(
"%s: Break cannot be used outside a loop",
e.SemanticError.Error(),
)
}
// CreateBreakNotInLoopError creates an error from the token
func CreateBreakNotInLoopError(token *token32) error {
return &BreakNotInLoopError{
SemanticError: CreateSemanticError(token),
}
}
// VariableRedeclarationError is a semantic error when a variable is declared
// again within the same scope
type VariableRedeclarationError struct {
SemanticError
ident string
prev Type
new Type
}
func (e *VariableRedeclarationError) Error() string {
return fmt.Sprintf(
"%s: '%s' redaclared from '%s' to '%s'",
e.SemanticError.Error(),
e.ident,
e.prev.String(),
e.new.String(),
)
}
// CreateVariableRedeclarationError creates an error from the token, variable
// identifier, previous and new type
func CreateVariableRedeclarationError(token *token32, ident string, oldt, newt Type) error {
return &VariableRedeclarationError{
SemanticError: CreateSemanticError(token),
ident: ident,
prev: oldt,
new: newt,
}
}
// UndeclaredVariableError is a semantic error when trying to access an
// undeclared variable
type UndeclaredVariableError struct {
SemanticError
ident string
}
func (e *UndeclaredVariableError) Error() string {
return fmt.Sprintf(
"%s: '%s' is undeclared",
e.SemanticError.Error(),
e.ident,
)
}
// EnumRedeclarationError is a semantic error when a variable is declared
// again within the same scope
type EnumRedeclarationError struct {
SemanticError
ident string
}
func (e *EnumRedeclarationError) Error() string {
return fmt.Sprintf(
"%s: class '%s' already declared",
e.SemanticError.Error(),
e.ident,
)
}
// CreateEnumRedeclarationError creates an error from the token, variable
// identifier, previous and new type
func CreateEnumRedeclarationError(token *token32, ident string) error {
return &EnumRedeclarationError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// CreateUndeclaredEnumError creates an error from the token and variable
// identifier
func CreateUndeclaredEnumError(token *token32, ident string) error {
return &UndeclaredEnumError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// UndeclaredEnumError is a semantic error when trying to access an
// undeclared variable
type UndeclaredEnumError struct {
SemanticError
ident string
}
func (e *UndeclaredEnumError) Error() string {
return fmt.Sprintf(
"%s: class '%s' is undeclared",
e.SemanticError.Error(),
e.ident,
)
}
// ClassRedeclarationError is a semantic error when a variable is declared
// again within the same scope
type ClassRedeclarationError struct {
SemanticError
ident string
}
func (e *ClassRedeclarationError) Error() string {
return fmt.Sprintf(
"%s: class '%s' already declared",
e.SemanticError.Error(),
e.ident,
)
}
// CreateClassRedeclarationError creates an error from the token, variable
// identifier, previous and new type
func CreateClassRedeclarationError(token *token32, ident string) error {
return &ClassRedeclarationError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// CreateUndeclaredClassError creates an error from the token and variable
// identifier
func CreateUndeclaredClassError(token *token32, ident string) error {
return &UndeclaredClassError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// UndeclaredClassError is a semantic error when trying to access an
// undeclared variable
type UndeclaredClassError struct {
SemanticError
ident string
}
func (e *UndeclaredClassError) Error() string {
return fmt.Sprintf(
"%s: class '%s' is undeclared",
e.SemanticError.Error(),
e.ident,
)
}
// CreateUndeclaredVariableError creates an error from the token and variable
// identifier
func CreateUndeclaredVariableError(token *token32, ident string) error {
return &UndeclaredVariableError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// TypeMismatchError is a semantic error when trying to operate on incompatible
// types
type TypeMismatchError struct {
SemanticError
expected Type
got Type
}
func (e *TypeMismatchError) Error() string {
return fmt.Sprintf(
"%s: type mismatch expected '%s' got '%s'",
e.SemanticError.Error(),
e.expected.String(),
e.got.String(),
)
}
// CreateTypeMismatchError creates an error from the token, the expected and the
// received type
func CreateTypeMismatchError(token *token32, expected, got Type) error {
return &TypeMismatchError{
SemanticError: CreateSemanticError(token),
expected: expected,
got: got,
}
}
// CallingNonFunctionError is a semantic error trying to call an undeclared
// function
type CallingNonFunctionError struct {
SemanticError
ident string
}
func (e *CallingNonFunctionError) Error() string {
return fmt.Sprintf(
"%s: calling non function '%s'",
e.SemanticError.Error(),
e.ident,
)
}
// CreateCallingNonFunctionError creates an error from a token and a function
// identifier
func CreateCallingNonFunctionError(token *token32, ident string) error {
return &CallingNonFunctionError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// FunctionCallWrongArityError is a semantic error trying to call a function
// with the wrong number of arguments
type FunctionCallWrongArityError struct {
SemanticError
ident string
expected int
got int
}
func (e *FunctionCallWrongArityError) Error() string {
return fmt.Sprintf(
"%s: '%s' called with '%d' arguments expected '%d'",
e.SemanticError.Error(),
e.ident,
e.got,
e.expected,
)
}
// CreateFunctionCallWrongArityError creates and error from a token, function
// identifier, expected and received number of parameters
func CreateFunctionCallWrongArityError(
token *token32,
ident string,
expected, got int) error {
return &FunctionCallWrongArityError{
SemanticError: CreateSemanticError(token),
ident: ident,
expected: expected,
got: got,
}
}
// FunctionCallOnNonObjectError is a semantic error trying to call a function
// on a non class instance
type FunctionCallOnNonObjectError struct {
SemanticError
ident string
wtype Type
}
func (e *FunctionCallOnNonObjectError) Error() string {
return fmt.Sprintf(
"%s: trying to call '%s' on non object of type '%s'",
e.SemanticError.Error(),
e.ident,
e.wtype,
)
}
// CreateFunctionCallOnNonObjectError creates and error from a token, function
// identifier, and type
func CreateFunctionCallOnNonObjectError(
token *token32,
ident string,
wtype Type) error {
return &FunctionCallOnNonObjectError{
SemanticError: CreateSemanticError(token),
ident: ident,
wtype: wtype,
}
}
// FunctionRedeclarationError is a semantic error when trying to declare a
// function again after it has been declared
type FunctionRedeclarationError struct {
SemanticError
ident string
}
func (e *FunctionRedeclarationError) Error() string {
return fmt.Sprintf(
"%s: function '%s' already declared",
e.SemanticError.Error(),
e.ident,
)
}
// CreateFunctionRedelarationError creates an error from a token and a function
// identifier
func CreateFunctionRedelarationError(token *token32, ident string) error {
return &FunctionRedeclarationError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// AmbigousFunctionCallError is a semantic error when multiple overloads of a
// function match the provided parameters
type AmbigousFunctionCallError struct {
SemanticError
ident string
}
func (e *AmbigousFunctionCallError) Error() string {
return fmt.Sprintf(
"%s: calling function '%s' where multiple overloads match",
e.SemanticError.Error(),
e.ident,
)
}
// CreateAmbigousFunctionCallError creates an error from a token and a function
// identifier
func CreateAmbigousFunctionCallError(token *token32, ident string) error {
return &AmbigousFunctionCallError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// NoSuchOverloadError is a semantic error trying to call a non-existing variant
// of an overloaded function
type NoSuchOverloadError struct {
SemanticError
ident string
}
func (e *NoSuchOverloadError) Error() string {
return fmt.Sprintf(
"%s: calling function '%s' where no overload matches",
e.SemanticError.Error(),
e.ident,
)
}
// CreateNoSuchOverloadError creates an error from a token and a function
// identifier
func CreateNoSuchOverloadError(token *token32, ident string) error {
return &NoSuchOverloadError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// InvalidVoidTypeError is a semantic error declaring a variable with void type
type InvalidVoidTypeError struct {
SemanticError
ident string
}
func (e *InvalidVoidTypeError) Error() string {
return fmt.Sprintf(
"%s: void type for variable '%s' is invalid",
e.SemanticError.Error(),
e.ident,
)
}
// CreateInvalidVoidTypeError creates an error from a token and an identifier
func CreateInvalidVoidTypeError(token *token32, ident string) error {
return &InvalidVoidTypeError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}
// VoidAssignmentError is a semantic error when trying to assign a void return
// value
type VoidAssignmentError struct {
SemanticError
ident string
}
func (e *VoidAssignmentError) Error() string {
return fmt.Sprintf(
"%s: assigning result of void function '%s' is invalid",
e.SemanticError.Error(),
e.ident,
)
}
// CreateVoidAssignmentError creates an error from a token and a function
// identifier
func CreateVoidAssignmentError(token *token32, ident string) error {
return &VoidAssignmentError{
SemanticError: CreateSemanticError(token),
ident: ident,
}
}