/
Errors.ooc
171 lines (108 loc) · 3.35 KB
/
Errors.ooc
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
import Trail
import ../../frontend/[CommandLine, Token, BuildParams]
import ../Node
/**
* Handle errors and warnings from the compiler
*/
ErrorHandler: interface {
onError: func (e: Error)
}
DevNullErrorHandler: class implements ErrorHandler {
init: func
onError: func (e: Error) { /* To the bit bucket! */ }
}
DefaultErrorHandler: class implements ErrorHandler {
params: BuildParams
init: func ~withParams (=params) {}
onError: func (e: Error) {
e print()
println()
if(e fatal?() && params fatalError) {
CommandLine failure(params)
}
}
}
/**
* An error thrown.
*/
Error: abstract class {
token: Token
message: String
// errors can be chained, to provide context
next: Error = null
init: func ~tokenMessage (=token, =message) {}
fatal?: func -> Bool { true }
prepareMessage: func -> String {
message + "\n"
}
format: func -> String {
result := token formatMessage(prepareMessage(), getType())
if (next) {
return result + next format()
}
result
}
print: func {
token printMessage(prepareMessage(), getType())
if (next) {
next print()
}
}
getType: func -> String {
"error"
}
}
InternalError: class extends Error {
init: func (.token, .message) {
super(token, message)
}
}
Warning: class extends Error {
init: super func ~tokenMessage
fatal?: func -> Bool { false }
getType: func -> String { "warning" }
}
InfoError: class extends Error {
init: super func ~tokenMessage
fatal?: func -> Bool { false }
getType: func -> String { "info" }
}
/*
* A small collection of often-used internal errors:
*/
CouldntAdd: class extends InternalError {
mark, newcomer: Node
init: func (.token, =mark, =newcomer, trail: Trail) {
super(token, "Couldn't add a %s before a %s, trail = %s" format(newcomer toString(), mark toString(), trail toString()))
}
}
CouldntAddBefore: class extends InternalError {
mark, newcomer: Node
init: func (.token, =mark, =newcomer, trail: Trail) {
super(token, "Couldn't add %s before %s. trail = %s" format(newcomer toString(), mark toString(), trail toString()))
}
}
CouldntAddAfter: class extends InternalError {
mark, newcomer: Node
init: func (.token, =mark, =newcomer, trail: Trail) {
super(token, "Couldn't add %s after %s. trail = %s" format(newcomer toString(), mark toString(), trail toString()))
}
}
CouldntAddBeforeInScope: class extends InternalError {
mark, newcomer: Node
init: func (.token, =mark, =newcomer, trail: Trail) {
super(token, "Couldn't add %s before %s in scope. trail = %s" format(newcomer toString(), mark toString(), trail toString()))
}
}
CouldntAddAfterInScope: class extends InternalError {
mark, newcomer: Node
init: func (.token, =mark, =newcomer, trail: Trail) {
super(token, "Couldn't add %s after %s in scope. trail = %s" format(newcomer toString(), mark toString(), trail toString()))
}
}
CouldntReplace: class extends InternalError {
oldie, kiddo: Node
init: func (.token, =oldie, =kiddo, trail: Trail) {
super(token, "Couldn't replace %s with %s, trail = %s" format(oldie toString(), kiddo toString(), trail toString()))
}
}