forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
191 lines (157 loc) · 6.91 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
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.
"""
if 'location' in self.arguments:
return self.arguments['location']
if 'line_number' in self.arguments:
return [self.arguments['line_number']]
return None
class FtfyException(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(FtfyException):
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 UndefinedVarException(HedyException):
def __init__(self, name):
super().__init__('Var Undefined',
name=name)
class CyclicVariableDefinitionException(HedyException):
def __init__(self, variable):
super().__init__('Cyclic Var Definition',
variable=variable)
class InvalidArgumentTypeException(HedyException):
def __init__(self, command, invalid_type, allowed_types, invalid_argument):
super().__init__('Invalid Argument Type',
command=command,
invalid_type=invalid_type,
allowed_types=allowed_types,
invalid_argument=invalid_argument)
class InvalidTypeCombinationException(HedyException):
def __init__(self, operation, arg1, arg2, type1, type2):
super().__init__('Invalid Type Combination',
operation=operation,
invalid_argument=arg1,
invalid_argument_2=arg2,
invalid_type=type1,
invalid_type_2=type2)
class InvalidArgumentException(HedyException):
def __init__(self, command, allowed_types, invalid_argument):
super().__init__('Invalid Argument',
command=command,
allowed_types=allowed_types,
invalid_argument=invalid_argument)
class WrongLevelException(HedyException):
def __init__(self, working_level, offending_keyword, tip):
super().__init__('Wrong Level',
working_level=working_level,
offending_keyword=offending_keyword,
tip=tip)
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(FtfyException):
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 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):
super().__init__('Unquoted Text', level=level)
class UnquotedAssignTextException(HedyException):
def __init__(self, text):
super().__init__('Unquoted Assignment', text=text)
class EmptyProgramException(HedyException):
def __init__(self):
super().__init__('Empty Program')
class LonelyEchoException(HedyException):
def __init__(self):
super().__init__('Lonely Echo')
class CodePlaceholdersPresentException(HedyException):
def __init__(self):
super().__init__('Has Blanks')
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)