Repository navigation
Expand file tree
/
Copy patherror_handler.py
More file actions
135 lines (102 loc) · 4.23 KB
/
Copy patherror_handler.py
File metadata and controls
135 lines (102 loc) · 4.23 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
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
# Copyright (C) 2026-Present IndianCoder3
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Error handling and formatting for ICTL interpreter.
Provides friendly error messages with visual pointers to problem locations.
"""
class ICTLError(Exception):
"""Base exception for ICTL errors."""
def __init__(self, message, line=None, col=None, context=None):
"""
Initialize ICTL error.
Args:
message (str): Error description
line (int): Line number (1-indexed)
col (int): Column number (1-indexed) of error location
context (str): The actual line content
"""
self.message = message
self.line = line
self.col = col
self.context = context
super().__init__(self.message)
def format_error(self, show_context=True):
"""
Format error message with visual pointer.
Args:
show_context (bool): Whether to show the line and pointer
Returns:
str: Formatted error message
"""
if not show_context or self.context is None:
return f"❌ {self.message}"
output = f" {self.context}\n"
if self.col is not None and self.col > 0:
# Create caret pointer
pointer = " " * (self.col - 1) + "^"
output += pointer + "\n"
output += f"❌ {self.message}"
return output
class SyntaxErrorICTL(ICTLError):
"""ICTL syntax error."""
pass
class RuntimeErrorICTL(ICTLError):
"""ICTL runtime error."""
pass
class VariableErrorICTL(ICTLError):
"""ICTL variable error."""
pass
class MathErrorICTL(ICTLError):
"""ICTL math/expression error."""
pass
def find_variable_position(line, var_name):
"""
Find the position of a variable reference in a line.
Args:
line (str): The full command line
var_name (str): The variable name to find
Returns:
int: Column number (1-indexed) or None if not found
"""
# Look for Variables.var_name pattern
search_str = f"Variables.{var_name}"
pos = line.find(search_str)
if pos >= 0:
return pos + 1 # Convert to 1-indexed
return None
def find_math_position(line, func_name="Math"):
"""
Find the position of a math function call.
Args:
line (str): The full command line
func_name (str): The function name to find
Returns:
int: Column number (1-indexed) or None if not found
"""
pos = line.find(func_name)
if pos >= 0:
return pos + 1 # Convert to 1-indexed
return None
def format_command_help(command):
"""
Provide help text for common command errors.
Args:
command (str): The command that failed
Returns:
str: Helpful suggestion
"""
suggestions = {
"Math.Eval": "Math.Eval() is an expression. Use: Terminal.Echo(Math.Eval(...))",
"Math.Compare": "Math.Compare() is an expression. Use in: Variable = Math.Compare(...)",
"Data.Compare": "Data.Compare() is an expression. Use in: Variable = Data.Compare(...)",
"Variables.New": "Syntax: Variables.New(MyVar) # No quotes around variable name",
"Terminal.Echo": "Syntax: Terminal.Echo(\"text\") or Terminal.Echo(Variables.MyVar)",
"Terminal.Ask": "Syntax: Variables.Name = Terminal.Ask(\"Prompt: \")",
"Terminal.Style": "Syntax: Terminal.Style(\"red\") # Colors: red, green, blue, yellow, cyan, magenta",
}
for cmd, help_text in suggestions.items():
if cmd in command:
return f"💡 Hint: {help_text}"
return None