-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
52 lines (39 loc) · 1.72 KB
/
errors.py
File metadata and controls
52 lines (39 loc) · 1.72 KB
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
###################
# ERRORS
####################
from helpers import string_with_arrows
class Error:
def __init__(self, pos_start, pos_end, err_name, err_details):
self._pos_start = pos_start
self._pos_end = pos_end
self._err_name = err_name
self._err_details = err_details
def as_string(self):
string = f"{self._err_name}: {self._err_details}."
string += f"\nFile {self._pos_start._f_name}, line {self._pos_start._ln_num + 1}"
string += "\n" + string_with_arrows(self._pos_start._f_txt, self._pos_start, self._pos_end)
return string
class IllegalCharError(Error):
def __init__(self, pos_start, pos_end, details):
super().__init__(pos_start, pos_end, "Illegal Character", details)
class InvalidSyntaxError(Error):
def __init__(self, pos_start, pos_end, details=""):
super().__init__(pos_start, pos_end, "Invalid Syntax", details)
class RTError(Error):
def __init__(self, pos_start, pos_end, details, context):
self._context = context
super().__init__(pos_start, pos_end, "Runtime Error", details)
def generate_traceback(self):
result = ""
pos = self._pos_start
cntxt = self._context
while cntxt:
result = f" File {pos._f_name}, line {str(pos._ln_num + 1)}, in {cntxt._display_name}\n" + result
pos = cntxt._parent_entry_pos
cntxt = cntxt._parent
return "Traceback (most recent call last):\n" + result
def as_string(self):
string = self.generate_traceback()
string += f"{self._err_name}: {self._err_details}."
string += "\n" + string_with_arrows(self._pos_start._f_txt, self._pos_start, self._pos_end)
return string