-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexceptions.ts
172 lines (157 loc) · 4.53 KB
/
exceptions.ts
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
import { KeywordToken, TokenType, SymbolToken } from "./tokenizer";
function tokenStringToTypeString(tokenString: string): string | null {
if (Object.values<string>(KeywordToken).includes(tokenString)) {
return tokenTypeToString(TokenType.KEYWORD);
} else if (Object.values<string>(SymbolToken).includes(tokenString)) {
return tokenTypeToString(TokenType.SYMBOL);
} else {
return "token";
}
}
function tokenTypeToString(tokenType: TokenType): string {
switch (tokenType) {
case TokenType.KEYWORD:
return "keyword";
case TokenType.SYMBOL:
return "symbol";
case TokenType.IDENTIFIER:
return "identifier";
case TokenType.INT_CONST:
return "integer constant";
case TokenType.STRING_CONST:
return "string constant";
default:
throw new Error("Invalid token type");
}
}
function expectedTokenToString(tokenTypeOrString: string | TokenType): string {
if (typeof tokenTypeOrString === "string") {
return `'${tokenTypeOrString}'`;
} else {
return tokenTypeToString(tokenTypeOrString);
}
}
function joinExpectedTokensWithOr(arr: (string | TokenType)[]): string {
if (arr.length === 0) {
return "";
} else if (arr.length === 1) {
return expectedTokenToString(arr[0]);
} else if (arr.length === 2) {
return `${expectedTokenToString(arr[0])} or ${expectedTokenToString(
arr[1]
)}`;
} else {
return `${arr
.slice(0, -1)
.map(expectedTokenToString)
.join(", ")}, or ${expectedTokenToString(arr.slice(-1)[0])}`;
}
}
export abstract class CompilerError {
protected fileName: string;
protected line: string;
protected lineNumber: number;
protected lineIndex: number;
protected message!: string;
private errorType: string;
constructor(
fileName: string,
line: string,
lineNumber: number,
lineIndex: number,
errorType: string
) {
this.fileName = fileName;
this.line = line;
this.lineNumber = lineNumber;
// Math.max can lead to confusing error messages if on new lines
// but i dont care
this.lineIndex = Math.max(lineIndex, 1);
this.errorType = errorType;
}
public toString(): string {
return `
${this.line}
${" ".repeat(this.lineIndex - 1)}^
${this.errorType}: ${this.message}
at ${this.fileName}.jack:${this.lineNumber}:${this.lineIndex}
`;
}
public getLineNumber() {
return this.lineNumber;
}
public getFileName() {
return this.fileName;
}
}
export class SyntaxError extends CompilerError {
constructor(
fileName: string,
line: string,
lineNumber: number,
lineIndex: number,
expectedToken: null | string | TokenType | (string | TokenType)[],
info?: string
) {
super(fileName, line, lineNumber, lineIndex, "SyntaxError");
if (expectedToken === null) {
this.message = "expected token";
} else if (expectedToken === "") {
this.message = "unexpected token";
} else {
this.message = `expected ${this.expectedTokenToEnglish(expectedToken)}`;
}
if (info) {
this.message += ` (${info})`;
}
}
private expectedTokenToEnglish(
expectedToken: string | TokenType | (string | TokenType)[]
): string {
if (typeof expectedToken === "string") {
return `${tokenStringToTypeString(expectedToken)} '${expectedToken}'`;
} else if (Array.isArray(expectedToken)) {
return `token ${joinExpectedTokensWithOr(expectedToken)}`;
} else if (expectedToken in TokenType) {
const ret = tokenTypeToString(expectedToken);
return "aeiou".includes(ret[0].toLowerCase()) ? `an ${ret}` : `a ${ret}`;
}
throw new Error("Invalid expected token");
}
}
export class NameError extends CompilerError {
constructor(
fileName: string,
line: string,
lineNumber: number,
lineIndex: number,
expectedName: string,
message: string
) {
super(fileName, line, lineNumber, lineIndex, "NameError");
this.message = `${message} (expected ${tokenStringToTypeString(
expectedName
)} '${expectedName}')`;
}
}
export class ReferenceError extends CompilerError {
constructor(
fileName: string,
line: string,
lineNumber: number,
lineIndex: number,
message: string
) {
super(fileName, line, lineNumber, lineIndex, "ReferenceError");
this.message = message;
}
}
export class BroadCompilerError extends CompilerError {
constructor(activeTab: string, private stringified: string) {
super(activeTab, "", 1, 1, "BroadCompilerError");
this.stringified = stringified;
}
public toString(): string {
return this.stringified;
}
}