-
Notifications
You must be signed in to change notification settings - Fork 621
/
GLSLPreprocessor.ts
212 lines (199 loc) · 8.62 KB
/
GLSLPreprocessor.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Reader } from './Reader';
/**
* @internal
*/
class MacroSubstitution {
public name: string = '';
public value: string = '';
public args: Array<string> = [];
}
/**
* @internal
* GLSL code preprocessor
* @group GFX
*/
export class GLSLPreprocessor extends Reader {
private _result: string;
private _skipLine: boolean;
private _definitionTables: Map<string, any>;
constructor(source: string) {
super(source);
this._result = '';
this._skipLine = false;
this._definitionTables = new Map<string, any>();
this.parse();
}
private parse() {
this.readChar();
while (this._char !== '\0') {
this.skipWhitespace();
// Skip the comments
if (this._char === '/') {
// Skip single-line comments
if (this.peekChar() === '/') {
this.skipComment();
continue;
}
// Skip multi-line comments
if (this.peekChar() === '*') {
this.skipMultilineComment();
continue;
}
}
// Preprocessing command
if (this._char === '#') {
this.readCharAndSkipWhitespace();
var name = this.readIdentifier();
switch (name) {
case 'version':
let version = this.readLine().trim();
break;
case 'define':
this.readCharAndSkipWhitespace();
var defineName = this.readIdentifier();
if (this.getChar() === '(') {
let macro = new MacroSubstitution();
this.readCharAndSkipWhitespace();
if (this.getChar() !== ')') {
do {
var argName = this.readIdentifier();
macro.args.push(argName);
this.skipWhitespace();
if (this.getChar() === ',') {
this.readCharAndSkipWhitespace();
continue;
}
} while (this.getChar() !== ')');
}
this.readCharAndSkipWhitespace();
macro.name = defineName;
macro.value = this.readLine().trim();
this._definitionTables.set(defineName, macro);
this.readCharAndSkipWhitespace();
} else {
let defineValue = this.readLine().trim();
if (defineValue[0] == '=') {
defineValue = defineValue.substring(1);
}
this._definitionTables.set(defineName, defineValue);
}
break;
case 'if':
let condition = this.readLine().trim();
if (condition == '0' || condition == 'false') {
this._skipLine = true;
break;
}
if (this._definitionTables.has(condition)) {
condition = this._definitionTables.get(condition);
if (condition == '0' || condition == 'false') {
this._skipLine = true;
break;
}
}
break;
case 'ifdef':
this.readCharAndSkipWhitespace();
var value = this.readIdentifier();
this._skipLine = !this._definitionTables.has(value);
break;
case 'else':
this._skipLine = !this._skipLine;
break;
case 'endif':
this._skipLine = false;
break;
default:
throw 'Unknown preprocessing command:' + name;
}
} else {
var line = this.readLine();
if (!this._skipLine) {
for (let key of this._definitionTables.keys()) {
let index = line.indexOf(key);
if (index != -1) {
let value = this._definitionTables.get(key);
if (typeof value === 'string') {
line = line.replace(key, value);
} else {
let macro = value as MacroSubstitution;
let reader = new Reader(line.substring(index + key.length));
reader.readCharAndSkipWhitespace();
if (reader.getChar() === '(') {
reader.readCharAndSkipWhitespace();
for (let count = 1; reader.getChar() !== '\0' && count > 0;) {
switch (reader.getChar()) {
case '(':
count++;
break;
case ')':
count--;
break;
}
reader.readCharAndSkipWhitespace();
}
}
let nBeginPos = index;
let nEndPos = nBeginPos + key.length + reader.currPosition;
let expr = line.substring(nBeginPos, nEndPos).trim();
if (macro.args.length > 0) {
let args: string[] = [];
let argsStr = expr.substring(expr.indexOf('(') + 1, expr.lastIndexOf(')')).trim();
if (argsStr.length > 0) {
args = this.parseArgs(argsStr);
}
let macroValue = macro.value.substring(macro.value.indexOf('('));
for (let i = 0; i < macro.args.length; i++) {
macroValue = macroValue.replace(macro.args[i], args[i]);
}
macroValue = macro.value.substring(0, macro.value.indexOf('(')) + macroValue;
line = line.replace(expr, macroValue);
} else {
line = line.replace(expr, macro.value);
}
}
}
}
this._result += line;
}
this.readChar();
}
}
}
public get source(): string {
return this._result;
}
private parseArgs(str: string): string[] {
let result: string[] = [];
let count = 0;
let reader = new Reader(str);
let position = reader.currPosition;
reader.readCharAndSkipWhitespace();
if (reader.getChar() !== '\0') {
reader.readCharAndSkipWhitespace();
for (; reader.getChar() !== '\0';) {
switch (reader.getChar()) {
case '(':
count++;
break;
case ')':
count--;
break;
case ',':
if (count == 0) {
let arg = str.substring(position, reader.currPosition);
result.push(arg);
position = reader.currPosition + 1;
}
break;
}
reader.readCharAndSkipWhitespace();
}
}
if (position < reader.currPosition) {
let arg = str.substring(position, reader.currPosition);
result.push(arg);
}
return result;
}
}