-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathMiniscriptErrors.cs
188 lines (143 loc) · 5.25 KB
/
MiniscriptErrors.cs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* MiniScriptErrors.cs
This file defines the exception hierarchy used by Miniscript.
The core of the tree is this:
MiniscriptException
LexerException -- any error while finding tokens from raw source
CompilerException -- any error while compiling tokens into bytecode
RuntimeException -- any error while actually executing code.
We have a number of fine-grained exception types within these,
but they will always derive from one of those three (and ultimately
from MiniscriptException).
*/
using System;
namespace Miniscript {
public class SourceLoc {
public string context; // file name, etc. (optional)
public int lineNum;
public SourceLoc(string context, int lineNum) {
this.context = context;
this.lineNum = lineNum;
}
public override string ToString() {
return string.Format("[{0}line {1}]",
string.IsNullOrEmpty(context) ? "" : context + " ",
lineNum);
}
}
public class MiniscriptException: Exception {
public SourceLoc location;
public MiniscriptException(string message) : base(message) {
}
public MiniscriptException(string context, int lineNum, string message) : base(message) {
location = new SourceLoc(context, lineNum);
}
public MiniscriptException(string message, Exception inner) : base(message, inner) {
}
/// <summary>
/// Get a standard description of this error, including type and location.
/// </summary>
public string Description() {
string desc = "Error: ";
if (this is LexerException) desc = "Lexer Error: ";
else if (this is CompilerException) desc = "Compiler Error: ";
else if (this is RuntimeException) desc = "Runtime Error: ";
desc += Message;
if (location != null) desc += " " + location;
return desc;
}
}
public class LexerException: MiniscriptException {
public LexerException() : base("Lexer Error") {
}
public LexerException(string message) : base(message) {
}
public LexerException(string message, Exception inner) : base(message, inner) {
}
}
public class CompilerException: MiniscriptException {
public CompilerException() : base("Syntax Error") {
}
public CompilerException(string message) : base(message) {
}
public CompilerException(string context, int lineNum, string message) : base(context, lineNum, message) {
}
public CompilerException(string message, Exception inner) : base(message, inner) {
}
}
public class RuntimeException: MiniscriptException {
public RuntimeException() : base("Runtime Error") {
}
public RuntimeException(string message) : base(message) {
}
public RuntimeException(string message, Exception inner) : base(message, inner) {
}
}
public class IndexException: RuntimeException {
public IndexException() : base("Index Error (index out of range)") {
}
public IndexException(string message) : base(message) {
}
public IndexException(string message, Exception inner) : base(message, inner) {
}
}
public class KeyException : RuntimeException {
public KeyException(string key) : base("Key Not Found: '" + key + "' not found in map") {
}
public KeyException(string message, Exception inner) : base(message, inner) {
}
}
public class TypeException : RuntimeException {
public TypeException() : base("Type Error (wrong type for whatever you're doing)") {
}
public TypeException(string message) : base(message) {
}
public TypeException(string message, Exception inner) : base(message, inner) {
}
}
public class TooManyArgumentsException : RuntimeException {
public TooManyArgumentsException() : base("Too Many Arguments") {
}
public TooManyArgumentsException(string message) : base(message) {
}
public TooManyArgumentsException(string message, Exception inner) : base(message, inner) {
}
}
public class LimitExceededException : RuntimeException {
public LimitExceededException() : base("Runtime Limit Exceeded") {
}
public LimitExceededException(string message) : base(message) {
}
public LimitExceededException(string message, Exception inner) : base(message, inner) {
}
}
public class UndefinedIdentifierException : RuntimeException {
public UndefinedIdentifierException(string ident) : base(
"Undefined Identifier: '" + ident + "' is unknown in this context") {
}
public UndefinedIdentifierException(string message, Exception inner) : base(message, inner) {
}
}
public class UndefinedLocalException : RuntimeException {
private UndefinedLocalException() {} // don't call this version!
public UndefinedLocalException(string ident) : base(
"Undefined Local Identifier: '" + ident + "' is unknown in this context") {
}
public UndefinedLocalException(string message, Exception inner) : base(message, inner) {
}
}
public static class Check {
public static void Range(int i, int min, int max, string desc="index") {
if (i < min || i > max) {
throw new IndexException(string.Format("Index Error: {0} ({1}) out of range ({2} to {3})",
desc, i, min, max));
}
}
public static void Type(Value val, System.Type requiredType, string desc=null) {
if (!requiredType.IsInstanceOfType(val)) {
string typeStr = val == null ? "null" : "a " + val.GetType();
throw new TypeException(string.Format("got {0} where a {1} was required{2}",
typeStr, requiredType, desc == null ? null : " (" + desc + ")"));
}
}
}
}