Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libc_preprocessor.js
4150 views
1
addToLibrary({
2
// Removes all C++ '//' and '/* */' comments from the given source string.
3
// N.b. will also eat comments inside strings.
4
$remove_cpp_comments_in_shaders: (code) => {
5
var i = 0, out = '', ch, next, len = code.length;
6
for (; i < len; ++i) {
7
ch = code[i];
8
if (ch == '/') {
9
next = code[i+1];
10
if (next == '/') {
11
while (i < len && code[i+1] != '\n') ++i;
12
} else if (next == '*') {
13
while (i < len && (code[i-1] != '*' || code[i] != '/')) ++i;
14
} else {
15
out += ch;
16
}
17
} else {
18
out += ch;
19
}
20
}
21
return out;
22
},
23
24
// Finds the index of closing parens from the opening parens at arr[i].
25
// Used polymorphically for strings ("foo") and token arrays (['(', 'foo', ')']) as input.
26
$find_closing_parens_index: (arr, i, opening='(', closing=')') => {
27
for (var nesting = 0; i < arr.length; ++i) {
28
if (arr[i] == opening) ++nesting;
29
if (arr[i] == closing && --nesting == 0) {
30
return i;
31
}
32
}
33
},
34
35
// Runs C preprocessor algorithm on the given string 'code'.
36
// Supported preprocessor directives: #if, #ifdef, #ifndef, #else, #elif, #endif, #define and #undef.
37
// predefs: Specifies a dictionary of { 'key1': function(arg0, arg1) {...}, 'key2': ... } of predefined preprocessing variables
38
$preprocess_c_code__deps: ['$find_closing_parens_index'],
39
$preprocess_c_code: function(code, defs = {}) {
40
var i = 0, // iterator over the input string
41
len = code.length, // cache input length
42
out = '', // generates the preprocessed output string
43
stack = [1]; // preprocessing stack (state of active/inactive #ifdef/#else blocks we are currently inside)
44
// a mapping 'symbolname' -> function(args) which evaluates the given cpp macro, e.g. #define FOO(x) x+10.
45
defs['defined'] = (args) => { // built-in "#if defined(x)"" macro.
46
#if ASSERTIONS
47
assert(args.length == 1);
48
assert(/^[A-Za-z0-9_$]+$/.test(args[0].trim())); // Test that a C preprocessor identifier contains only valid characters (we likely parsed wrong if this fails)
49
#endif
50
return defs[args[0].trim()] ? 1 : 0;
51
};
52
53
// Returns true if str[i] is whitespace.
54
function isWhitespace(str, i) {
55
return !(str.charCodeAt(i) > 32); // Compare as negation to treat end-of-string undefined as whitespace
56
}
57
58
// Returns index to the next whitespace character starting at str[i].
59
function nextWhitespace(str, i) {
60
while (!isWhitespace(str, i)) ++i;
61
return i;
62
}
63
64
// Returns an integer ID classification of the character at str[idx], used for tokenization purposes.
65
function classifyChar(str, idx) {
66
var cc = str.charCodeAt(idx);
67
#if ASSERTIONS
68
assert(!(cc > 127), "Only 7-bit ASCII can be used in preprocessor #if/#ifdef/#define statements!");
69
#endif
70
if (cc > 32) {
71
if (cc < 48) return 1; // an operator symbol, any of !"#$%&'()*+,-./
72
if (cc < 58) return 2; // a number 0123456789
73
if (cc < 65) return 1; // an operator symbol, any of :;<=>?@
74
if (cc < 91 || cc == 95/*_*/) return 3; // a character, any of A-Z or _
75
if (cc < 97) return 1; // an operator symbol, any of [\]^`
76
if (cc < 123) return 3; // a character, any of a-z
77
return 1; // an operator symbol, any of {|}~
78
}
79
return cc < 33 ? 0 : 4; // 0=whitespace, 4=end-of-string
80
}
81
82
// Returns a tokenized array of the given string expression, i.e. "FOO > BAR && BAZ" -> ["FOO", ">", "BAR", "&&", "BAZ"]
83
// Optionally keeps whitespace as tokens to be able to reconstruct the original input string.
84
function tokenize(exprString, keepWhitespace) {
85
var out = [], len = exprString.length;
86
for (var i = 0; i <= len; ++i) {
87
var kind = classifyChar(exprString, i);
88
if (kind == 2/*0-9*/ || kind == 3/*a-z*/) { // a character or a number
89
for (var j = i+1; j <= len; ++j) {
90
var kind2 = classifyChar(exprString, j);
91
if (kind2 != kind && (kind2 != 2/*0-9*/ || kind != 3/*a-z*/)) { // parse number sequence "423410", and identifier sequence "FOO32BAR"
92
out.push(exprString.substring(i, j));
93
i = j-1;
94
break;
95
}
96
}
97
} else if (kind == 1/*operator symbol*/) {
98
// Lookahead for two-character operators.
99
var op2 = exprString.slice(i, i + 2);
100
if (['<=', '>=', '==', '!=', '&&', '||'].includes(op2)) {
101
out.push(op2);
102
++i;
103
} else {
104
out.push(exprString[i]);
105
}
106
}
107
}
108
return out;
109
}
110
111
// Expands preprocessing macros on substring str[lineStart...lineEnd]
112
function expandMacros(str, lineStart, lineEnd) {
113
if (lineEnd === undefined) lineEnd = str.length;
114
var len = str.length;
115
var out = '';
116
for (var i = lineStart; i < lineEnd; ++i) {
117
var kind = classifyChar(str, i);
118
if (kind == 3/*a-z*/) {
119
for (var j = i + 1; j <= lineEnd; ++j) {
120
var kind2 = classifyChar(str, j);
121
if (kind2 != 2/*0-9*/ && kind2 != 3/*a-z*/) {
122
var symbol = str.substring(i, j);
123
var pp = defs[symbol];
124
if (pp) {
125
var expanded = str.substring(lineStart, i);
126
if (pp.length) { // Expanding a macro? (#define FOO(X) ...)
127
while (isWhitespace(str, j)) ++j;
128
if (str[j] == '(') {
129
var closeParens = find_closing_parens_index(str, j);
130
// N.b. this has a limitation that multiparameter macros cannot nest with other multiparameter macros
131
// e.g. FOO(a, BAR(b, c)) is not supported.
132
expanded += pp(str.substring(j+1, closeParens).split(',')) + str.substring(closeParens+1, lineEnd);
133
} else {
134
var j2 = nextWhitespace(str, j);
135
expanded += pp([str.substring(j, j2)]) + str.substring(j2, lineEnd);
136
}
137
} else { // Expanding a non-macro (#define FOO BAR)
138
expanded += pp() + str.substring(j, lineEnd);
139
}
140
return expandMacros(expanded, 0);
141
}
142
out += symbol;
143
i = j-1;
144
break;
145
}
146
}
147
} else {
148
out += str[i];
149
}
150
}
151
return out;
152
}
153
154
// Given a token list e.g. ['2', '>', '1'], returns a function that evaluates that token list.
155
function buildExprTree(tokens) {
156
// Consume tokens array into a function tree until the tokens array is exhausted
157
// to a single root node that evaluates it.
158
while (tokens.length > 1 || typeof tokens[0] != 'function') {
159
tokens = ((tokens) => {
160
// Find the index 'i' of the operator we should evaluate next:
161
var i, j, p, operatorAndPriority = -2;
162
for (j = 0; j < tokens.length; ++j) {
163
if ((p = ['*', '/', '+', '-', '!', '<', '<=', '>', '>=', '==', '!=', '&&', '||', '('].indexOf(tokens[j])) > operatorAndPriority) {
164
i = j;
165
operatorAndPriority = p;
166
}
167
}
168
169
if (operatorAndPriority == 13 /* parens '(' */) {
170
// Find the closing parens position
171
var j = find_closing_parens_index(tokens, i);
172
if (j) {
173
tokens.splice(i, j+1-i, buildExprTree(tokens.slice(i+1, j)));
174
return tokens;
175
}
176
}
177
178
if (operatorAndPriority == 4 /* unary ! */) {
179
// Special case: the unary operator ! needs to evaluate right-to-left.
180
i = tokens.lastIndexOf('!');
181
var innerExpr = buildExprTree(tokens.slice(i+1, i+2));
182
tokens.splice(i, 2, function() { return !innerExpr(); })
183
return tokens;
184
}
185
186
// A binary operator:
187
if (operatorAndPriority >= 0) {
188
var left = buildExprTree(tokens.slice(0, i));
189
var right = buildExprTree(tokens.slice(i+1));
190
switch(tokens[i]) {
191
case '&&': return [function() { return left() && right(); }];
192
case '||': return [function() { return left() || right(); }];
193
case '==': return [function() { return left() == right(); }];
194
case '!=': return [function() { return left() != right(); }];
195
case '<' : return [function() { return left() < right(); }];
196
case '<=': return [function() { return left() <= right(); }];
197
case '>' : return [function() { return left() > right(); }];
198
case '>=': return [function() { return left() >= right(); }];
199
case '+': return [function() { return left() + right(); }];
200
case '-': return [function() { return left() - right(); }];
201
case '*': return [function() { return left() * right(); }];
202
case '/': return [function() { return Math.floor(left() / right()); }];
203
}
204
}
205
// else a number:
206
#if ASSERTIONS
207
if (tokens[i] == ')') throw 'Parsing failure, mismatched parentheses in parsing!' + tokens.toString();
208
assert(operatorAndPriority == -1);
209
#endif
210
var num = Number(tokens[i]);
211
return [function() { return num; }]
212
})(tokens);
213
}
214
return tokens[0];
215
}
216
217
// Preprocess the input one line at a time.
218
for (; i < len; ++i) {
219
// Find the start of the current line.
220
var lineStart = i;
221
222
// Seek iterator to end of current line.
223
i = code.indexOf('\n', i);
224
if (i < 0) i = len;
225
226
// Find the first non-whitespace character on the line.
227
for (var j = lineStart; j < i && isWhitespace(code, j); ++j);
228
229
// Is this a non-preprocessor directive line?
230
var thisLineIsInActivePreprocessingBlock = stack[stack.length-1];
231
if (code[j] != '#') { // non-preprocessor line?
232
if (thisLineIsInActivePreprocessingBlock) {
233
out += expandMacros(code, lineStart, i) + '\n';
234
}
235
continue;
236
}
237
// This is a preprocessor directive line, e.g. #ifdef or #define.
238
239
// Parse the line as #<directive> <expression>
240
var space = nextWhitespace(code, j);
241
var directive = code.substring(j+1, space);
242
var expression = code.substring(space, i).trim();
243
switch(directive) {
244
case 'if':
245
var tokens = tokenize(expandMacros(expression, 0));
246
var exprTree = buildExprTree(tokens);
247
var evaluated = exprTree();
248
stack.push(!!evaluated * stack[stack.length-1]);
249
break;
250
case 'ifdef': stack.push(!!defs[expression] * stack[stack.length-1]); break;
251
case 'ifndef': stack.push(!defs[expression] * stack[stack.length-1]); break;
252
case 'else': stack[stack.length-1] = (1-stack[stack.length-1]) * stack[stack.length-2]; break;
253
case 'endif': stack.pop(); break;
254
case 'define':
255
if (thisLineIsInActivePreprocessingBlock) {
256
// This could either be a macro with input args (#define MACRO(x,y) x+y), or a direct expansion #define FOO 2,
257
// figure out which.
258
var macroStart = expression.indexOf('(');
259
var firstWs = nextWhitespace(expression, 0);
260
if (firstWs < macroStart) macroStart = 0;
261
if (macroStart > 0) { // #define MACRO( x , y , z ) <statement of x,y and z>
262
var macroEnd = expression.indexOf(')', macroStart);
263
let params = expression.substring(macroStart+1, macroEnd).split(',').map(x => x.trim());
264
let value = tokenize(expression.substring(macroEnd+1).trim())
265
defs[expression.substring(0, macroStart)] = (args) => {
266
var ret = '';
267
value.forEach((x) => {
268
var argIndex = params.indexOf(x);
269
ret += (argIndex >= 0) ? args[argIndex] : x;
270
});
271
return ret;
272
};
273
} else { // #define FOO (x + y + z)
274
let value = expandMacros(expression.substring(firstWs+1).trim(), 0);
275
defs[expression.substring(0, firstWs)] = () => value;
276
}
277
}
278
break;
279
case 'undef': if (thisLineIsInActivePreprocessingBlock) delete defs[expression]; break;
280
default:
281
if (directive != 'version' && directive != 'pragma' && directive != 'extension' && directive != 'line') { // GLSL shader compiler specific #directives.
282
#if ASSERTIONS
283
err('Unrecognized preprocessor directive #' + directive + '!');
284
#endif
285
}
286
287
// Unknown preprocessor macro, just pass through the line to output.
288
out += expandMacros(code, lineStart, i) + '\n';
289
}
290
}
291
return out;
292
}
293
});
294
295