Skip to content

Commit c41c05c

Browse files
authored
Merge pull request #10 from Deekshith-M13/improve-error-system
2 parents 551a12d + 33001e0 commit c41c05c

6 files changed

Lines changed: 115 additions & 6 deletions

File tree

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/stroll-script-python.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stroll/src/errors.py

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,99 @@
11
import os
2-
from colorama import Style, Back, Fore, init
3-
init()
2+
from colorama import Style, Fore, init
3+
init(autoreset=True)
4+
5+
6+
# Legacy error classes (old system)
7+
# Kept so old code does not break.
8+
9+
410
class Syntaxerror:
511
def __init__(self, msg):
612
self.msg = msg
713
def __repr__(self):
8-
return Fore.RED + f'Syntax error -> {self.msg}!' + Style.RESET_ALL
14+
return Fore.RED + f'Syntax error -> {self.msg}!' + Style.RESET_ALL
15+
916
class Extensionerror:
1017
def __init__(self, filename, msg):
1118
self.filename = filename
1219
self.msg = msg
1320
def __repr__(self):
14-
return Fore.RED + f'File extension error for file: {self.filename}, expected extension: ".bwt" -> {self.msg}!' + Style.RESET_ALL
21+
return (
22+
Fore.RED +
23+
f'File extension error for: {self.filename}, expected .bwt -> {self.msg}!'
24+
+ Style.RESET_ALL
25+
)
26+
1527
class Miscerror:
1628
def __init__(self, msg):
1729
self.msg = msg
1830
def __repr__(self):
19-
return Fore.RED + f'Misc error -> {self.msg}!' + Style.RESET_ALL
31+
return Fore.RED + f'Misc error -> {self.msg}!' + Style.RESET_ALL
32+
33+
34+
35+
# New improved error system
36+
# Real Python exceptions with
37+
# optional line/column/token info.
38+
39+
40+
class StrollError(Exception):
41+
def __init__(self, message, *, line=None, column=None, filename=None, token=None):
42+
super().__init__(message)
43+
self.message = message
44+
self.line = line
45+
self.column = column
46+
self.filename = filename
47+
self.token = token
48+
49+
def __str__(self):
50+
text = Fore.RED + f"{self.__class__.__name__}: {self.message}"
51+
# Show position info if available
52+
if self.line is not None:
53+
text += f" (line {self.line}"
54+
if self.column is not None:
55+
text += f", col {self.column}"
56+
text += ")"
57+
# Show token if available
58+
if self.token:
59+
text += f" [token: {self.token}]"
60+
return text + Style.RESET_ALL
61+
62+
__repr__ = __str__
63+
64+
65+
# Simple subclasses so we can distinguish types of errors
66+
class SyntaxErrorStroll(StrollError): pass
67+
class ExtensionErrorStroll(StrollError): pass
68+
class MiscErrorStroll(StrollError): pass
69+
class NameErrorStroll(StrollError): pass
70+
class TypeErrorStroll(StrollError): pass
71+
class ArgumentErrorStroll(StrollError): pass
72+
class RuntimeErrorStroll(StrollError): pass
73+
74+
75+
76+
# Helper to raise syntax errors
77+
# Parser/runtime can call this.
78+
79+
80+
def raise_syntax(message, token=None, *, filename=None):
81+
line = getattr(token, "line", None)
82+
col = getattr(token, "column", None)
83+
val = getattr(token, "value", token)
84+
raise SyntaxErrorStroll(
85+
message,
86+
line=line,
87+
column=col,
88+
filename=filename,
89+
token=val
90+
)
91+
92+
93+
# Old terminate() function
94+
# Still kept for compatibility.
95+
96+
2097
def terminate(token):
2198
print(Fore.YELLOW + f"On token: [ {token} ]" + Style.RESET_ALL)
22-
os._exit(1)
99+
os._exit(1)

0 commit comments

Comments
 (0)