-
Notifications
You must be signed in to change notification settings - Fork 291
/
exceptions.py
396 lines (290 loc) · 14 KB
/
exceptions.py
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
import inspect
"""
Any exception added in this file must be also added to error-messages.txt
So we can translate the error message. The exception must also be assigned
an Exception Type in the exception_types dictionary in statistics.py
"""
class HedyException(Exception):
def __init__(self, error_code, **arguments):
"""Create a new HedyException.
You should not create a HedyException directly. Instead, use any of
the subclasses of HedyException below.
The keyword arguments passed into this constructor become available
in exception translation strings. In those arguments, the keywords
'location' and 'line_number' are special: they will be used to indicate
the error location in the client.
"""
super().__init__(error_code)
self.error_code = error_code
self.arguments = arguments
@property
def error_location(self):
"""Return the location where the error was found.
Returns either an array of [row, col] or just [row].
If 'location' is part of the keyword arguments, return that.
Otherwise, if 'line_number' is part of the keyword arguments, return that instead
wrapped in a list so we are sure the return type is always a list.
"""
if 'location' in self.arguments:
return self.arguments['location']
if 'line_number' in self.arguments:
return [self.arguments['line_number']]
return None
class WarningException(HedyException):
"""Fixed That For You warning/exception.
Not really a failure case: instead it represents a warning to
the user that they made a mistake we recovered for them.
'fixed_code' and 'fixed_result' will contain the repaired
code, and the result of compiling that repaired code.
"""
def __init__(self, error_code, fixed_code, fixed_result, **arguments):
super().__init__(error_code, **arguments)
self.fixed_code = fixed_code
self.fixed_result = fixed_result
class InvalidSpaceException(WarningException):
def __init__(self, level, line_number, fixed_code, fixed_result):
super().__init__('Invalid Space',
level=level,
line_number=line_number,
fixed_code=fixed_code,
fixed_result=fixed_result) # what is the difference??
class UnusedVariableException(WarningException):
def __init__(self, level, line_number, variable_name, fixed_code, fixed_result):
super().__init__('Unused Variable',
level=level,
line_number=line_number,
variable_name=variable_name,
fixed_code=fixed_code,
fixed_result=fixed_result)
class ParseException(HedyException):
def __init__(self, level, location, found, fixed_code=None):
super().__init__('Parse',
level=level,
location=location,
found=found,
# 'character_found' for backwards compatibility
character_found=found)
# TODO (FH, 8 dec 21) many exceptions now support fixed code maybe we
# should move it to hedyexception?
self.fixed_code = fixed_code
class UnquotedEqualityCheckException(HedyException):
def __init__(self, line_number):
super().__init__('Unquoted Equality Check',
line_number=line_number)
self.location = [line_number]
class AccessBeforeAssignException(HedyException):
def __init__(self, name, access_line_number, definition_line_number):
super().__init__('Access Before Assign',
name=name,
access_line_number=access_line_number,
line_number=access_line_number,
definition_line_number=definition_line_number)
class UndefinedVarException(HedyException):
def __init__(self, name, line_number):
super().__init__('Var Undefined',
name=name,
line_number=line_number)
class UndefinedFunctionException(HedyException):
def __init__(self, name, line_number):
super().__init__('Function Undefined',
name=name,
line_number=line_number)
class CyclicVariableDefinitionException(HedyException):
def __init__(self, variable, line_number):
super().__init__('Cyclic Var Definition',
variable=variable,
line_number=line_number)
class InvalidArgumentTypeException(HedyException):
def __init__(self, command, invalid_type, allowed_types, invalid_argument, line_number):
super().__init__('Invalid Argument Type',
command=command,
invalid_type=invalid_type,
allowed_types=allowed_types,
invalid_argument=invalid_argument,
line_number=line_number)
class InvalidTypeCombinationException(HedyException):
def __init__(self, command, arg1, arg2, type1, type2, line_number):
super().__init__('Invalid Type Combination',
command=command,
invalid_argument=arg1,
invalid_argument_2=arg2,
invalid_type=type1,
invalid_type_2=type2,
line_number=line_number)
class InvalidArgumentException(HedyException):
def __init__(self, command, allowed_types, invalid_argument, line_number):
super().__init__('Invalid Argument',
command=command,
allowed_types=allowed_types,
invalid_argument=invalid_argument,
line_number=line_number)
class WrongLevelException(HedyException):
def __init__(self, working_level, offending_keyword, tip, line_number):
super().__init__('Wrong Level',
working_level=working_level,
offending_keyword=offending_keyword,
tip=tip,
line_number=line_number)
class InputTooBigException(HedyException):
def __init__(self, lines_of_code, max_lines):
super().__init__('Too Big',
lines_of_code=lines_of_code,
max_lines=max_lines)
class InvalidCommandException(WarningException):
def __init__(
self,
level,
invalid_command,
guessed_command,
line_number,
fixed_code,
fixed_result):
super().__init__('Invalid',
invalid_command=invalid_command,
level=level,
guessed_command=guessed_command,
line_number=line_number,
fixed_code=fixed_code,
fixed_result=fixed_result)
self.location = [line_number]
class MissingCommandException(HedyException):
def __init__(self, level, line_number):
super().__init__('Missing Command',
level=level,
line_number=line_number)
class MissingInnerCommandException(HedyException):
def __init__(self, command, level, line_number):
super().__init__('Missing Inner Command',
command=command,
level=level,
line_number=line_number)
class MissingVariableException(HedyException):
def __init__(self, command, level, line_number):
super().__init__('Missing Variable',
command=command,
level=level,
line_number=line_number)
class InvalidAtCommandException(HedyException):
def __init__(self, command, level, line_number):
super().__init__('Invalid At Command',
command=command,
level=level,
line_number=line_number)
class IncompleteRepeatException(HedyException):
def __init__(self, command, level, line_number):
super().__init__('Incomplete Repeat',
command=command,
level=level,
line_number=line_number)
class LonelyTextException(HedyException):
def __init__(self, level, line_number):
super().__init__('Lonely Text',
level=level,
line_number=line_number)
class IncompleteCommandException(HedyException):
def __init__(self, incomplete_command, level, line_number):
super().__init__('Incomplete',
incomplete_command=incomplete_command,
level=level,
line_number=line_number)
# Location is copied here so that 'hedy_error_to_response' will find it
# Location can be either [row, col] or just [row]
self.location = [line_number]
class UnquotedTextException(HedyException):
def __init__(self, level, line_number, unquotedtext=None):
super().__init__('Unquoted Text',
level=level,
unquotedtext=unquotedtext,
line_number=line_number)
class MissingAdditionalCommand(HedyException):
def __init__(self, command, missing_command, line_number):
super().__init__('Missing Additional Command',
command=command,
missing_command=missing_command,
line_number=line_number)
class MisspelledAtCommand(HedyException):
def __init__(self, command, arg1, line_number):
super().__init__('Misspelled At Command',
command=command,
invalid_argument=arg1,
line_number=line_number)
class NonDecimalVariable(HedyException):
def __init__(self, line_number):
super().__init__('Non Decimal Variable',
line_number=line_number)
class UnquotedAssignTextException(HedyException):
def __init__(self, text, line_number):
super().__init__('Unquoted Assignment', text=text, line_number=line_number)
class MissingBracketsException(HedyException):
def __init__(self, level, line_number):
super().__init__('Missing Square Brackets',
line_number=line_number)
class LonelyEchoException(HedyException):
def __init__(self):
super().__init__('Lonely Echo')
class CodePlaceholdersPresentException(HedyException):
def __init__(self, line_number):
super().__init__('Has Blanks', line_number=line_number)
class TooManyIndentsStartLevelException(HedyException):
def __init__(self, line_number, leading_spaces, fixed_code=None):
super().__init__('Too Many Indents', line_number=line_number, leading_spaces=leading_spaces)
self.fixed_code = fixed_code
class TooFewIndentsStartLevelException(HedyException):
def __init__(self, line_number, leading_spaces, fixed_code=None):
super().__init__('Too Few Indents', line_number=line_number, leading_spaces=leading_spaces)
self.fixed_code = fixed_code
class NoIndentationException(HedyException):
def __init__(self, line_number, leading_spaces, indent_size, fixed_code=None):
super().__init__('No Indentation',
line_number=line_number,
leading_spaces=leading_spaces,
indent_size=indent_size)
self.fixed_code = fixed_code
class IndentationException(HedyException):
def __init__(self, line_number, leading_spaces, indent_size, fixed_code=None):
super().__init__('Unexpected Indentation',
line_number=line_number,
leading_spaces=leading_spaces,
indent_size=indent_size)
self.fixed_code = fixed_code
class UnsupportedFloatException(HedyException):
def __init__(self, value):
super().__init__('Unsupported Float', value=value)
class UnsupportedStringValue(HedyException):
def __init__(self, invalid_value):
super().__init__('Unsupported String Value', invalid_value=invalid_value)
class MissingElseForPressitException(HedyException):
def __init__(self, command, level, line_number):
super().__init__('Pressit Missing Else',
command=command,
level=level,
line_number=line_number)
class NestedFunctionException(HedyException):
def __init__(self):
super().__init__('Nested Function')
class WrongNumberofArguments(HedyException):
def __init__(self, name, defined_number, used_number, line_number):
super().__init__('Wrong Number of Arguments',
name=name,
defined_number=defined_number,
used_number=used_number,
line_number=line_number)
class InvalidErrorSkippedException(HedyException):
def __init__(self):
super().__init__('Invalid Error Skipped')
class RuntimeValueException(HedyException):
def __init__(self, command, value, tip):
super().__init__('Runtime Value Error', command=command, value=value, tip=tip)
class RuntimeValuesException(HedyException):
def __init__(self, command, value, tip):
super().__init__('Runtime Values Error', command=command, value=value, tip=tip)
class RuntimeIndexException(HedyException):
def __init__(self, name):
super().__init__('Runtime Index Error', name=name)
class ElseWithoutIfException(HedyException):
def __init__(self, line_number):
super().__init__('Else Without If Error', line_number=line_number)
class MissingColonException(HedyException):
def __init__(self, command, line_number):
super().__init__('Missing Colon Error', command=command, line_number=line_number)
HEDY_EXCEPTIONS = {name: cls for name, cls in globals().items() if inspect.isclass(cls)}