-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.cpp
More file actions
134 lines (118 loc) · 4.06 KB
/
exception.cpp
File metadata and controls
134 lines (118 loc) · 4.06 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
#include <string>
#include <sstream>
// Base exception for all interpreter errors
class InterpreterException : public std::runtime_error {
protected:
size_t line_;
size_t column_;
std::string file_;
public:
InterpreterException(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: std::runtime_error(buildMessage(message, line, column, file)),
line_(line), column_(column), file_(file) {}
size_t line() const noexcept { return line_; }
size_t column() const noexcept { return column_; }
const std::string& file() const noexcept { return file_; }
private:
static std::string buildMessage(const std::string& msg,
size_t line,
size_t column,
const std::string& file) {
std::ostringstream oss;
if (!file.empty()) oss << file << ":";
if (line > 0) {
oss << line;
if (column > 0) oss << ":" << column;
oss << ": ";
}
oss << msg;
return oss.str();
}
};
// Syntax errors (parsing phase)
class SyntaxError : public InterpreterException {
public:
SyntaxError(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: InterpreterException("Syntax Error: " + message, line, column, file) {}
};
// Lexical errors (tokenization phase)
class LexicalError : public InterpreterException {
public:
LexicalError(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: InterpreterException("Lexical Error: " + message, line, column, file) {}
};
// Runtime errors (execution phase)
class RuntimeError : public InterpreterException {
public:
RuntimeError(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: InterpreterException("Runtime Error: " + message, line, column, file) {}
};
// Specific runtime errors
class NameError : public RuntimeError {
public:
NameError(const std::string& name,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: RuntimeError("Undefined variable '" + name + "'", line, column, file) {}
};
class TypeError : public RuntimeError {
public:
TypeError(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: RuntimeError("Type Error: " + message, line, column, file) {}
};
class DivisionByZeroError : public RuntimeError {
public:
DivisionByZeroError(size_t line = 0,
size_t column = 0,
const std::string& file = "")
: RuntimeError("Division by zero", line, column, file) {}
};
class IndexError : public RuntimeError {
public:
IndexError(const std::string& message,
size_t line = 0,
size_t column = 0,
const std::string& file = "")
: RuntimeError("Index Error: " + message, line, column, file) {}
};
// Usage example:
/*
try {
// Lexer phase
if (invalidToken) {
throw LexicalError("Unexpected character '$'", 5, 12, "script.lang");
}
// Parser phase
if (unexpectedToken) {
throw SyntaxError("Expected ';' after statement", 10, 25, "script.lang");
}
// Runtime phase
if (undefinedVar) {
throw NameError("myVariable", 15, 8, "script.lang");
}
if (denominator == 0) {
throw DivisionByZeroError(20, 15, "script.lang");
}
} catch (const InterpreterException& e) {
std::cerr << e.what() << std::endl;
// Can also access e.line(), e.column(), e.file() for custom handling
} catch (const std::exception& e) {
std::cerr << "Unexpected error: " << e.what() << std::endl;
}
*/