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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
addToLibrary({
// Removes all C++ '//' and '/* */' comments from the given source string.
// N.b. will also eat comments inside strings.
$remove_cpp_comments_in_shaders: (code) => {
var i = 0, out = '', ch, next, len = code.length;
for (; i < len; ++i) {
ch = code[i];
if (ch == '/') {
next = code[i+1];
if (next == '/') {
while (i < len && code[i+1] != '\n') ++i;
} else if (next == '*') {
while (i < len && (code[i-1] != '*' || code[i] != '/')) ++i;
} else {
out += ch;
}
} else {
out += ch;
}
}
return out;
},
// Finds the index of closing parens from the opening parens at arr[i].
// Used polymorphically for strings ("foo") and token arrays (['(', 'foo', ')']) as input.
$find_closing_parens_index: (arr, i, opening='(', closing=')') => {
for (var nesting = 0; i < arr.length; ++i) {
if (arr[i] == opening) ++nesting;
if (arr[i] == closing && --nesting == 0) {
return i;
}
}
},
// Runs C preprocessor algorithm on the given string 'code'.
// Supported preprocessor directives: #if, #ifdef, #ifndef, #else, #elif, #endif, #define and #undef.
// predefs: Specifies a dictionary of { 'key1': function(arg0, arg1) {...}, 'key2': ... } of predefined preprocessing variables
$preprocess_c_code__deps: ['$jstoi_q', '$find_closing_parens_index'],
$preprocess_c_code: function(code, defs = {}) {
var i = 0, // iterator over the input string
len = code.length, // cache input length
out = '', // generates the preprocessed output string
stack = [1]; // preprocessing stack (state of active/inactive #ifdef/#else blocks we are currently inside)
// a mapping 'symbolname' -> function(args) which evaluates the given cpp macro, e.g. #define FOO(x) x+10.
defs['defined'] = (args) => { // built-in "#if defined(x)"" macro.
#if ASSERTIONS
assert(args.length == 1);
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)
#endif
return defs[args[0].trim()] ? 1 : 0;
};
// Returns true if str[i] is whitespace.
function isWhitespace(str, i) {
return !(str.charCodeAt(i) > 32); // Compare as negation to treat end-of-string undefined as whitespace
}
// Returns index to the next whitespace character starting at str[i].
function nextWhitespace(str, i) {
while (!isWhitespace(str, i)) ++i;
return i;
}
// Returns an integer ID classification of the character at str[idx], used for tokenization purposes.
function classifyChar(str, idx) {
var cc = str.charCodeAt(idx);
#if ASSERTIONS
assert(!(cc > 127), "Only 7-bit ASCII can be used in preprocessor #if/#ifdef/#define statements!");
#endif
if (cc > 32) {
if (cc < 48) return 1; // an operator symbol, any of !"#$%&'()*+,-./
if (cc < 58) return 2; // a number 0123456789
if (cc < 65) return 1; // an operator symbol, any of :;<=>?@
if (cc < 91 || cc == 95/*_*/) return 3; // a character, any of A-Z or _
if (cc < 97) return 1; // an operator symbol, any of [\]^`
if (cc < 123) return 3; // a character, any of a-z
return 1; // an operator symbol, any of {|}~
}
return cc < 33 ? 0 : 4; // 0=whitespace, 4=end-of-string
}
// Returns a tokenized array of the given string expression, i.e. "FOO > BAR && BAZ" -> ["FOO", ">", "BAR", "&&", "BAZ"]
// Optionally keeps whitespace as tokens to be able to reconstruct the original input string.
function tokenize(exprString, keepWhitespace) {
var out = [], len = exprString.length;
for (var i = 0; i <= len; ++i) {
var kind = classifyChar(exprString, i);
if (kind == 2/*0-9*/ || kind == 3/*a-z*/) { // a character or a number
for (var j = i+1; j <= len; ++j) {
var kind2 = classifyChar(exprString, j);
if (kind2 != kind && (kind2 != 2/*0-9*/ || kind != 3/*a-z*/)) { // parse number sequence "423410", and identifier sequence "FOO32BAR"
out.push(exprString.substring(i, j));
i = j-1;
break;
}
}
} else if (kind == 1/*operator symbol*/) {
// Lookahead for two-character operators.
var op2 = exprString.substr(i, 2);
if (['<=', '>=', '==', '!=', '&&', '||'].includes(op2)) {
out.push(op2);
++i;
} else {
out.push(exprString[i]);
}
}
}
return out;
}
// Expands preprocessing macros on substring str[lineStart...lineEnd]
function expandMacros(str, lineStart, lineEnd) {
if (lineEnd === undefined) lineEnd = str.length;
var len = str.length;
var out = '';
for (var i = lineStart; i < lineEnd; ++i) {
var kind = classifyChar(str, i);
if (kind == 3/*a-z*/) {
for (var j = i + 1; j <= lineEnd; ++j) {
var kind2 = classifyChar(str, j);
if (kind2 != 2/*0-9*/ && kind2 != 3/*a-z*/) {
var symbol = str.substring(i, j);
var pp = defs[symbol];
if (pp) {
var expanded = str.substring(lineStart, i);
if (pp.length) { // Expanding a macro? (#define FOO(X) ...)
while (isWhitespace(str, j)) ++j;
if (str[j] == '(') {
var closeParens = find_closing_parens_index(str, j);
// N.b. this has a limitation that multiparameter macros cannot nest with other multiparameter macros
// e.g. FOO(a, BAR(b, c)) is not supported.
expanded += pp(str.substring(j+1, closeParens).split(',')) + str.substring(closeParens+1, lineEnd);
} else {
var j2 = nextWhitespace(str, j);
expanded += pp([str.substring(j, j2)]) + str.substring(j2, lineEnd);
}
} else { // Expanding a non-macro (#define FOO BAR)
expanded += pp() + str.substring(j, lineEnd);
}
return expandMacros(expanded, 0);
}
out += symbol;
i = j-1;
break;
}
}
} else {
out += str[i];
}
}
return out;
}
// Given a token list e.g. ['2', '>', '1'], returns a function that evaluates that token list.
function buildExprTree(tokens) {
// Consume tokens array into a function tree until the tokens array is exhausted
// to a single root node that evaluates it.
while (tokens.length > 1 || typeof tokens[0] != 'function') {
tokens = ((tokens) => {
// Find the index 'i' of the operator we should evaluate next:
var i, j, p, operatorAndPriority = -2;
for (j = 0; j < tokens.length; ++j) {
if ((p = ['*', '/', '+', '-', '!', '<', '<=', '>', '>=', '==', '!=', '&&', '||', '('].indexOf(tokens[j])) > operatorAndPriority) {
i = j;
operatorAndPriority = p;
}
}
if (operatorAndPriority == 13 /* parens '(' */) {
// Find the closing parens position
var j = find_closing_parens_index(tokens, i);
if (j) {
tokens.splice(i, j+1-i, buildExprTree(tokens.slice(i+1, j)));
return tokens;
}
}
if (operatorAndPriority == 4 /* unary ! */) {
// Special case: the unary operator ! needs to evaluate right-to-left.
i = tokens.lastIndexOf('!');
var innerExpr = buildExprTree(tokens.slice(i+1, i+2));
tokens.splice(i, 2, function() { return !innerExpr(); })
return tokens;
}
// A binary operator:
if (operatorAndPriority >= 0) {
var left = buildExprTree(tokens.slice(0, i));
var right = buildExprTree(tokens.slice(i+1));
switch(tokens[i]) {
case '&&': return [function() { return left() && right(); }];
case '||': return [function() { return left() || right(); }];
case '==': return [function() { return left() == right(); }];
case '!=': return [function() { return left() != right(); }];
case '<' : return [function() { return left() < right(); }];
case '<=': return [function() { return left() <= right(); }];
case '>' : return [function() { return left() > right(); }];
case '>=': return [function() { return left() >= right(); }];
case '+': return [function() { return left() + right(); }];
case '-': return [function() { return left() - right(); }];
case '*': return [function() { return left() * right(); }];
case '/': return [function() { return Math.floor(left() / right()); }];
}
}
// else a number:
#if ASSERTIONS
if (tokens[i] == ')') throw 'Parsing failure, mismatched parentheses in parsing!' + tokens.toString();
assert(operatorAndPriority == -1);
#endif
var num = jstoi_q(tokens[i]);
return [function() { return num; }]
})(tokens);
}
return tokens[0];
}
// Preprocess the input one line at a time.
for (; i < len; ++i) {
// Find the start of the current line.
var lineStart = i;
// Seek iterator to end of current line.
i = code.indexOf('\n', i);
if (i < 0) i = len;
// Find the first non-whitespace character on the line.
for (var j = lineStart; j < i && isWhitespace(code, j); ++j);
// Is this a non-preprocessor directive line?
var thisLineIsInActivePreprocessingBlock = stack[stack.length-1];
if (code[j] != '#') { // non-preprocessor line?
if (thisLineIsInActivePreprocessingBlock) {
out += expandMacros(code, lineStart, i) + '\n';
}
continue;
}
// This is a preprocessor directive line, e.g. #ifdef or #define.
// Parse the line as #<directive> <expression>
var space = nextWhitespace(code, j);
var directive = code.substring(j+1, space);
var expression = code.substring(space, i).trim();
switch(directive) {
case 'if':
var tokens = tokenize(expandMacros(expression, 0));
var exprTree = buildExprTree(tokens);
var evaluated = exprTree();
stack.push(!!evaluated * stack[stack.length-1]);
break;
case 'ifdef': stack.push(!!defs[expression] * stack[stack.length-1]); break;
case 'ifndef': stack.push(!defs[expression] * stack[stack.length-1]); break;
case 'else': stack[stack.length-1] = (1-stack[stack.length-1]) * stack[stack.length-2]; break;
case 'endif': stack.pop(); break;
case 'define':
if (thisLineIsInActivePreprocessingBlock) {
// This could either be a macro with input args (#define MACRO(x,y) x+y), or a direct expansion #define FOO 2,
// figure out which.
var macroStart = expression.indexOf('(');
var firstWs = nextWhitespace(expression, 0);
if (firstWs < macroStart) macroStart = 0;
if (macroStart > 0) { // #define MACRO( x , y , z ) <statement of x,y and z>
var macroEnd = expression.indexOf(')', macroStart);
let params = expression.substring(macroStart+1, macroEnd).split(',').map(x => x.trim());
let value = tokenize(expression.substring(macroEnd+1).trim())
defs[expression.substring(0, macroStart)] = (args) => {
var ret = '';
value.forEach((x) => {
var argIndex = params.indexOf(x);
ret += (argIndex >= 0) ? args[argIndex] : x;
});
return ret;
};
} else { // #define FOO (x + y + z)
let value = expandMacros(expression.substring(firstWs+1).trim(), 0);
defs[expression.substring(0, firstWs)] = () => value;
}
}
break;
case 'undef': if (thisLineIsInActivePreprocessingBlock) delete defs[expression]; break;
default:
if (directive != 'version' && directive != 'pragma' && directive != 'extension' && directive != 'line') { // GLSL shader compiler specific #directives.
#if ASSERTIONS
err('Unrecognized preprocessor directive #' + directive + '!');
#endif
}
// Unknown preprocessor macro, just pass through the line to output.
out += expandMacros(code, lineStart, i) + '\n';
}
}
return out;
}
});
|