This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.py
83 lines (49 loc) · 1.38 KB
/
errors.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
import sys
import threading
from colr import Colr
class Error:
"""Base class for all errors."""
def __init__(self, msg, pos):
"""Construct an Error object.
Args:
msg (str): Message of the error.
pos (int): 1D position of the error.
"""
self.msg = msg
self.pos = pos
# Builtin errors
class UnexpectedTokenError(Error):
pass
class UnknownTokenError(Error):
pass
class UndefinedSymbolError(Error):
pass
class MultipleDefinitionError(Error):
pass
class IncompatibleTypesError(Error):
pass
class ArgumentMismatchError(Error):
pass
class NoGiveError(Error):
pass
class GiveNotAllowedError(Error):
pass
class ErrorHandler:
"""Data structure to hold a list of errors."""
def __init__(self):
"""Construct an ErrorHandler."""
self.errors = []
def error(self, err):
"""Add an error to the list.
Args:
err (Error): Error to be added.
"""
self.errors.append(Colr(
f"{err.__class__.__name__}: {err.msg}. Error Position: {self.lexer.convert_pos_to_line(err.pos)}",
fore="red",
style="bold"))
def log_all(self):
"""Log all errors in the list with a header."""
print(Colr("ERRORS: ", fore="red", style="bold"))
for err in self.errors:
print(err)