forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderCodeCursor.ts
75 lines (60 loc) · 2.19 KB
/
shaderCodeCursor.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
/** @internal */
export class ShaderCodeCursor {
private _lines: string[] = [];
lineIndex: number;
get currentLine(): string {
return this._lines[this.lineIndex];
}
get canRead(): boolean {
return this.lineIndex < this._lines.length - 1;
}
set lines(value: string[]) {
this._lines.length = 0;
for (const line of value) {
// Skip empty lines
if (!line || line === "\r") {
continue;
}
// Prevent removing line break in macros.
if (line[0] === "#") {
this._lines.push(line);
continue;
}
// Do not split single line comments
const trimmedLine = line.trim();
if (!trimmedLine) {
continue;
}
if (trimmedLine.startsWith("//")) {
this._lines.push(line);
continue;
}
// Work with semicolon in the line
const semicolonIndex = trimmedLine.indexOf(";");
if (semicolonIndex === -1) {
// No semicolon in the line
this._lines.push(trimmedLine);
} else if (semicolonIndex === trimmedLine.length - 1) {
// Single semicolon at the end of the line
// If trimmedLine == ";", we must not push, to be backward compatible with the old code!
if (trimmedLine.length > 1) {
this._lines.push(trimmedLine);
}
} else {
// Semicolon in the middle of the line
const split = line.split(";");
for (let index = 0; index < split.length; index++) {
let subLine = split[index];
if (!subLine) {
continue;
}
subLine = subLine.trim();
if (!subLine) {
continue;
}
this._lines.push(subLine + (index !== split.length - 1 ? ";" : ""));
}
}
}
}
}