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