-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.py
More file actions
31 lines (28 loc) · 1001 Bytes
/
error.py
File metadata and controls
31 lines (28 loc) · 1001 Bytes
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
class CompilerError(Exception):
def __init__(
self,
message: str,
line: int,
col: int,
source_line: str | None = None,
file_name: str | None = None,
) -> None:
self.message = message
self.line = line
self.col = col
self.source_line = source_line
self.file_name = file_name
super().__init__(message)
def format(self) -> str:
header = self._format_header()
if self.source_line is None:
return header
pointer = self._format_pointer()
return f"{header}\n {self.source_line}\n {pointer}"
def _format_header(self) -> str:
location = self.file_name if self.file_name else "<input>"
return f"{location}:{self.line}:{self.col}: {self.message}"
def _format_pointer(self) -> str:
# col is 1-based; subtract 1 so the caret sits under the right character
spaces = " " * (self.col - 1)
return f"{spaces}^"