This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPreprocessor.js
More file actions
418 lines (386 loc) · 17.7 KB
/
Copy pathPreprocessor.js
File metadata and controls
418 lines (386 loc) · 17.7 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
Copyright 2013 Daniel Wirtz <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @license Preprocessor.js (c) 2013 Daniel Wirtz <[email protected]>
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/Preprocessor.js for details
*/
(function(global) {
/**
* Constructs a new Preprocessor.
* @exports Preprocessor
* @class Provides pre-processing of JavaScript source files, e.g. to build different versions of a library.
* @param {string} source Source to process
* @param {string|Object.<string,string>=} baseDirOrIncludes Source base directory used for includes (node.js only)
* or an object containing all the included sources by filename. Defaults to the current working directory.
* @param {boolean} preserveLineNumbers When removing blocks of code, replace the block with blank lines so that
* line numbers are preserved, as long as #include is not used
* @constructor
*/
var Preprocessor = function Preprocessor(source, baseDirOrIncludes, preserveLineNumbers) {
/**
* Source code to pre-process.
* @type {string}
* @expose
*/
this.source = ""+source;
/**
* Source base directory.
* @type {string}
* @expose
*/
this.baseDir = typeof baseDirOrIncludes == 'string' ? baseDirOrIncludes : ".";
/**
* Included sources by filename.
* @type {Object.<string, string>}
*/
this.includes = typeof baseDirOrIncludes == 'object' ? baseDirOrIncludes : {};
/**
* Preserve line numbers when removing blocks of code
* @type {boolean}
*/
this.preserveLineNumbers = typeof preserveLineNumbers == 'boolean' ? preserveLineNumbers : false;
/**
* Whether running inside of node.js or not.
* @type {boolean}
* @expose
*/
this.isNode = (typeof window == 'undefined' || !window.window) && typeof require == 'function';
/**
* Error reporting source ahead length.
* @type {number}
* @expose
*/
this.errorSourceAhead = 50;
/**
* Runtime defines.
* @type {Array.<string>}
*/
this.defines = [];
};
/**
* Definition expression
* @type {!RegExp}
*/
Preprocessor.EXPR = /([ ]*)\/\/[ ]+#(include_once|include|ifn?def|if|endif|else|elif|put|define)/g;
/**
* #include "path/to/file". Requires node.js' "fs" module.
* @type {!RegExp}
*/
Preprocessor.INCLUDE = /(include_once|include)[ ]+"([^"\\]*(\\.[^"\\]*)*)"[ ]*\r?(?:\n|$)/g;
/**
* #ifdef/#ifndef SOMEDEFINE, #if EXPRESSION
* @type {!RegExp}
*/
Preprocessor.IF = /(ifdef|ifndef|if)[ ]*([^\r\n]+)\r?\n/g;
/**
* #endif/#else, #elif EXPRESSION
* @type {!RegExp}
*/
Preprocessor.ENDIF = /(endif|else|elif)([ ]+[^\r\n]+)?\r?(?:\n|$)/g;
/**
* #put EXPRESSION
* @type {!RegExp}
*/
Preprocessor.PUT = /put[ ]+([^\n]+)[ ]*/g;
/**
* #define EXPRESSION
* @type {!RegExp}
*/
Preprocessor.DEFINE = /define[ ]+([^\n]+)\r?(?:\n|$)/g;
/**
* @type {!RegExp}
* @inner
*/
var GLOB_EXP = /(?:^|[^\\])\*/;
/**
* @type {!RegExp}
* @inner
*/
var NOT_LINE_ENDING = /[^\r\n]/g;
/**
* Strips slashes from an escaped string.
* @param {string} str Escaped string
* @return {string} Unescaped string
* @expose
*/
Preprocessor.stripSlashes = function(str) {
// ref: http://phpjs.org/functions/stripslashes/
return (str + '').replace(/\\(.?)/g, function (s, n1) {
switch (n1) {
case '\\': return '\\';
case '0': return '\u0000';
case '': return '';
default: return n1;
}
});
};
/**
* Adds slashes to an unescaped string.
* @param {string} str Unescaped string
* @return {string} Escaped string
* @expose
*/
Preprocessor.addSlashes = function(str) {
return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
};
/**
* Indents a multi-line string.
* @param {string} str Multi-line string to indent
* @param {string} indent Indent to use
* @return {string} Indented string
* @expose
*/
Preprocessor.indent = function(str, indent) {
var lines = str.split("\n");
for (var i=0; i<lines.length; i++) {
lines[i] = indent + lines[i];
}
return lines.join("\n");
};
/**
* Transforms a string for display in error messages.
* @param {string} str String to transform
* @return {string}
* @expose
*/
Preprocessor.nlToStr = function(str) {
return '['+str.replace(/\r/g, "").replace(/\n/g, "\\n")+']';
};
/**
* Evaluates an expression.
* @param {object.<string,string>} runtimeDefines Runtime defines
* @param {Array.<string>|string} inlineDefines Inline defines (optional for backward compatibility)
* @param {string=} expr Expression to evaluate
* @return {*} Expression result
* @throws {Error} If the expression cannot be evaluated
* @expose
*/
Preprocessor.evaluate = function(runtimeDefines, inlineDefines, expr) {
if (typeof inlineDefines === 'string') {
expr = inlineDefines;
inlineDefines = [];
}
var addSlashes = Preprocessor.addSlashes;
return (function(runtimeDefines, inlineDefines, expr) {
for (var key in runtimeDefines) {
if (runtimeDefines.hasOwnProperty(key)) {
eval("var "+key+" = \""+addSlashes(""+runtimeDefines[key])+"\";");
}
}
for (var i=0; i<inlineDefines.length; i++) {
var def = inlineDefines[i];
if (def.substring(0,9) != 'function ' && def.substring(0,4) != 'var ') {
def = "var "+def; // Enforce local
}
eval(def);
}
return eval(expr);
}).bind(null)(runtimeDefines, inlineDefines, expr);
};
/**
* Preprocesses.
* @param {object.<string,string>} defines Defines
* @param {function(string)=} verbose Print verbose processing information to the specified function as the first parameter. Defaults to not print debug information.
* @return {string} Processed source
* @throws {Error} If the source cannot be pre-processed
* @expose
*/
Preprocessor.prototype.process = function(defines, verbose) {
defines = defines || {};
verbose = typeof verbose == 'function' ? verbose : function() {};
verbose("Defines: "+JSON.stringify(defines));
var match, match2, include, p, stack = [];
while ((match = Preprocessor.EXPR.exec(this.source)) !== null) {
verbose(match[2]+" @ "+match.index+"-"+Preprocessor.EXPR.lastIndex);
var indent = match[1];
switch (match[2]) {
case 'include_once':
case 'include':
Preprocessor.INCLUDE.lastIndex = match.index;
if ((match2 = Preprocessor.INCLUDE.exec(this.source)) === null) {
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
include = Preprocessor.stripSlashes(match2[2]);
if (typeof this.includes[include] !== 'undefined') { // Do we already know it?
if (match2[1] === "include_once") {
verbose(" skip incl: "+include);
include = "";
} else {
verbose(" incl: "+include);
include = this.includes[include];
}
} else { // Load it if in node.js...
if (!this.isNode) {
throw(new Error("Failed to resolve include: "+this.baseDir+"/"+include));
}
try {
var key = include,
fs = require("fs");
if (GLOB_EXP.test(include)) {
var glob = require("glob");
verbose(' glob incl: '+this.baseDir+"/"+include);
var _this = this;
glob(this.baseDir+"/"+include, {"sync": true}, function(err, files) {
if (err) throw(err);
include = '';
for (var i=0; i<files.length; i++) {
verbose(' incl: '+files[i]);
var contents = fs.readFileSync(files[i])+"";
_this.includes[key] = contents;
include += contents;
}
});
} else {
verbose(' incl: '+include);
include = fs.readFileSync(this.baseDir+"/"+include)+"";
this.includes[key] = include;
}
} catch (e) {
throw(new Error("Include failed: "+include+" ("+e+")"));
}
}
this.source = this.source.substring(0, match.index)+Preprocessor.indent(include, indent)+this.source.substring(Preprocessor.INCLUDE.lastIndex);
Preprocessor.EXPR.lastIndex = stack.length > 0 ? stack[stack.length-1].lastIndex : 0; // Start over again
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
break;
case 'put':
Preprocessor.PUT.lastIndex = match.index;
if ((match2 = Preprocessor.PUT.exec(this.source)) === null) {
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
include = match2[1];
verbose(" expr: "+match2[1]);
include = Preprocessor.evaluate(defines, this.defines, match2[1]);
verbose(" value: "+Preprocessor.nlToStr(include));
this.source = this.source.substring(0, match.index)+indent+include+this.source.substring(Preprocessor.PUT.lastIndex);
Preprocessor.EXPR.lastIndex = match.index + include.length;
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
break;
case 'ifdef':
case 'ifndef':
case 'if':
Preprocessor.IF.lastIndex = match.index;
if ((match2 = Preprocessor.IF.exec(this.source)) === null) {
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
verbose(" test: "+match2[2]);
if (match2[1] == "ifdef") {
include = !!defines[match2[2]];
} else if (match2[1] == "ifndef") {
include = !defines[match2[2]];
} else {
include = Preprocessor.evaluate(defines, this.defines, match2[2]);
}
verbose(" value: "+include);
stack.push(p={
"include": include,
"index": match.index,
"lastIndex": Preprocessor.IF.lastIndex
});
verbose(" push: "+JSON.stringify(p));
break;
case 'endif':
case 'else':
case 'elif':
Preprocessor.ENDIF.lastIndex = match.index;
if ((match2 = Preprocessor.ENDIF.exec(this.source)) === null) {
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
if (stack.length == 0) {
throw(new Error("Unexpected #"+match2[1]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
var before = stack.pop();
verbose(" pop: "+JSON.stringify(before));
if (this.preserveLineNumbers) {
include = this.source.substring(before["index"], before["lastIndex"]).replace(NOT_LINE_ENDING, "")+
this.source.substring(before["lastIndex"], match.index)+
this.source.substring(match.index, Preprocessor.ENDIF.lastIndex).replace(NOT_LINE_ENDING, "");
} else {
include = this.source.substring(before["lastIndex"], match.index);
}
if (before["include"]) {
verbose(" incl: "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+include.length+" bytes + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
this.source = this.source.substring(0, before["index"])+include+this.source.substring(Preprocessor.ENDIF.lastIndex);
} else if (this.preserveLineNumbers) {
verbose(" excl(\\n): "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
include = include.replace(NOT_LINE_ENDING, "");
this.source = this.source.substring(0, before["index"])+include+this.source.substring(Preprocessor.ENDIF.lastIndex);
} else {
verbose(" excl: "+Preprocessor.nlToStr(include)+", 0-"+before['index']+" + "+Preprocessor.ENDIF.lastIndex+"-"+this.source.length);
include = "";
this.source = this.source.substring(0, before["index"])+this.source.substring(Preprocessor.ENDIF.lastIndex);
}
if (this.source == "") {
verbose(" result empty");
}
Preprocessor.EXPR.lastIndex = before["index"]+include.length;
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
if (match2[1] == "else" || match2[1] == "elif") {
if (match2[1] == 'else') {
include = !before["include"];
} else {
include = Preprocessor.evaluate(defines, this.defines, match2[2]);
}
stack.push(p={
"include": !before["include"],
"index": Preprocessor.EXPR.lastIndex,
"lastIndex": Preprocessor.EXPR.lastIndex
});
verbose(" push: "+JSON.stringify(p));
}
break;
case 'define':
// https://github.com/dcodeIO/Preprocessor.js/issues/5
Preprocessor.DEFINE.lastIndex = match.index;
if ((match2 = Preprocessor.DEFINE.exec(this.source)) === null) {
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
}
var define = match2[1];
verbose(" def: "+match2[1]);
this.defines.push(define);
var lineEnding = ""
if (this.preserveLineNumbers) {
lineEnding = this.source.substring(match.index, Preprocessor.DEFINE.lastIndex).replace(NOT_LINE_ENDING, "");
}
this.source = this.source.substring(0, match.index)+indent+lineEnding+this.source.substring(Preprocessor.DEFINE.lastIndex);
Preprocessor.EXPR.lastIndex = match.index;
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
}
}
if (stack.length > 0) {
before = stack.pop();
verbose("Still on stack: "+JSON.stringify(before));
}
return this.source;
};
/**
* Returns a string representation of this object.
* @return {string} String representation as of "Preprocessor"
* @expose
*/
Preprocessor.prototype.toString = function() {
return "Preprocessor";
};
// Enable module loading if available
if (typeof module != 'undefined' && module["exports"]) { // CommonJS
module["exports"] = Preprocessor;
} else if (typeof define != 'undefined' && define["amd"]) { // AMD
define("Preprocessor", [], function() { return Preprocessor; });
} else { // Shim
if (!global["dcodeIO"]) {
global["dcodeIO"] = {};
}
global["dcodeIO"]["Preprocessor"] = Preprocessor;
}
})(this);