forked from galacean/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSError.ts
68 lines (57 loc) · 1.79 KB
/
GSError.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
// #if _VERBOSE
import { ShaderPosition } from "./common/ShaderPosition";
import { ShaderRange } from "./common/ShaderRange";
export class GSError extends Error {
static wrappingLineCount = 2;
constructor(
name: GSErrorName,
message: string,
public readonly location: ShaderRange | ShaderPosition,
public readonly source: string,
public readonly file?: string
) {
super(message);
this.name = name;
}
override toString(): string {
let start: ShaderPosition, end: ShaderPosition;
const { message, location, source } = this;
if (!source) {
return message;
}
if (location instanceof ShaderPosition) {
start = end = location;
} else {
start = location.start;
end = location.end;
}
const lines = source.split("\n");
let diagnosticMessage = `${this.name}: ${message}\n\n`;
const lineSplit = "|···";
const wrappingLineCount = GSError.wrappingLineCount;
for (let i = start.line - wrappingLineCount, n = end.line + wrappingLineCount; i <= n; i++) {
const line = lines[i];
diagnosticMessage += lineSplit + `${line}\n`;
if (i < start.line || i > end.line) continue;
let remarkStart = 0;
let remarkEnd = line.length;
let paddingLength = lineSplit.length;
if (i === start.line) {
remarkStart = start.column;
paddingLength += start.column;
}
if (i === end.line) {
remarkEnd = end.column;
}
const remarkLength = Math.max(remarkEnd - remarkStart, 1);
diagnosticMessage += " ".repeat(paddingLength) + "^".repeat(remarkLength) + "\n";
}
return diagnosticMessage;
}
}
// #endif
export enum GSErrorName {
PreprocessorError = "PreprocessorError",
CompilationError = "CompilationError",
ScannerError = "ScannerError"
}