-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrors.ts
More file actions
79 lines (71 loc) · 1.83 KB
/
Errors.ts
File metadata and controls
79 lines (71 loc) · 1.83 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
import { Position } from './Position'
export class LangError {
posStart: Position | null
posEnd: Position | null
errorName: string
details: string
constructor(
posStart: Position | null,
posEnd: Position | null,
errorName: string,
details: string
) {
this.posStart = posStart
this.posEnd = posEnd
this.errorName = errorName
this.details = details
}
asString() {
let result = `${this.errorName}: ${this.details}\n`
result += `File ${this.posStart?.fileName}, line ${
this.posStart && this.posStart.line + 1
}`
return result
}
}
export class IllegalCharError extends LangError {
constructor(posStart: Position, posEnd: Position, details: string) {
super(posStart, posEnd, 'Illegal Character', details)
}
}
export class InvalidSyntaxError extends LangError {
constructor(posStart: Position, posEnd: Position, details: string = '') {
super(posStart, posEnd, 'Invalid Syntax', details)
}
}
export class ExpectedCharError extends LangError {
constructor(posStart: Position, posEnd: Position, details: string) {
super(posStart, posEnd, 'Expected Character', details)
}
}
export class RunTimeError extends LangError {
context: any
constructor(
posStart: Position | null,
posEnd: Position | null,
details: string,
context: any
) {
super(posStart, posEnd, 'Runtime Error', details)
this.context = context
}
asString() {
let result = this.generateStackTrace()
result += `${this.errorName}: ${this.details}`
return result
}
generateStackTrace() {
let result = ''
let pos = this.posStart
let context = this.context
while (context) {
result =
`\nat ${pos && pos.fileName}, line ${(
(pos as Position).line + 1
).toString()}\nat ${context.displayName}` + result
pos = context.parentEntryPosition
context = context.parent
}
return `Error: Something went wrong\n${result}`
}
}