forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
273 lines (205 loc) · 9.75 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
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)
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 UnquotedEqualityCheck(HedyException):
def __init__(self, line_number):
super().__init__('Unquoted Equality Check',
line_number=line_number)
self.location = [line_number]
class AccessBeforeAssign(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 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 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 UnquotedAssignTextException(HedyException):
def __init__(self, text, line_number):
super().__init__('Unquoted Assignment', text=text, 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 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 LockedLanguageFeatureException(HedyException):
def __init__(self, concept):
super().__init__('Locked Language Feature', concept=concept)
class UnsupportedStringValue(HedyException):
def __init__(self, invalid_value):
super().__init__('Unsupported String Value', invalid_value=invalid_value)