Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.

Commit 1b5ee36

Browse files
committed
Inline defines, fixes #5
1 parent 9c71c1e commit 1b5ee36

File tree

9 files changed

+230
-38
lines changed

9 files changed

+230
-38
lines changed

Preprocessor.js

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@
6565
* @expose
6666
*/
6767
this.errorSourceAhead = 50;
68+
69+
/**
70+
* Runtime defines.
71+
* @type {Array.<string>}
72+
*/
73+
this.defines = [];
6874
};
6975

7076
/**
7177
* Definition expression
7278
* @type {RegExp}
7379
*/
74-
Preprocessor.EXPR = /([ ]*)\/\/[ ]+#(include|ifn?def|if|endif|else|elif|put)/g;
80+
Preprocessor.EXPR = /([ ]*)\/\/[ ]+#(include|ifn?def|if|endif|else|elif|put|define)/g;
7581

7682
/**
7783
* #include "path/to/file". Requires node.js' "fs" module.
@@ -97,6 +103,12 @@
97103
*/
98104
Preprocessor.PUT = /put[ ]+([^\n]+)[ ]*/g;
99105

106+
/**
107+
* #define EXPRESSION
108+
* @type {RegExp}
109+
*/
110+
Preprocessor.DEFINE = /define[ ]+([^\n]+)\r?(?:\n|$)/g;
111+
100112
/**
101113
* Strips slashes from an escaped string.
102114
* @param {string} str Escaped string
@@ -152,27 +164,38 @@
152164

153165
/**
154166
* Evaluates an expression.
155-
* @param {object.<strin,string>} defines Defines
156-
* @param {string} expr Expression to evaluate
167+
* @param {object.<string,string>} runtimeDefines Runtime defines
168+
* @param {Array.<string>|string} inlineDefines Inline defines (optional for backward compatibility)
169+
* @param {string=} expr Expression to evaluate
157170
* @return {*} Expression result
158171
* @throws {Error} If the expression cannot be evaluated
159172
* @expose
160173
*/
161-
Preprocessor.evaluate = function(defines, expr) {
174+
Preprocessor.evaluate = function(runtimeDefines, inlineDefines, expr) {
175+
if (typeof inlineDefines === 'string') {
176+
expr = inlineDefines;
177+
inlineDefines = [];
178+
}
162179
var addSlashes = Preprocessor.addSlashes;
163-
return (function(defines, expr) {
164-
var Preprocessor = null;
165-
for (var key in defines) {
166-
if (defines.hasOwnProperty(key)) {
167-
eval("var "+key+" = \""+addSlashes(""+defines[key])+"\";");
180+
return (function(runtimeDefines, inlineDefines, expr) {
181+
for (var key in runtimeDefines) {
182+
if (runtimeDefines.hasOwnProperty(key)) {
183+
eval("var "+key+" = \""+addSlashes(""+runtimeDefines[key])+"\";");
184+
}
185+
}
186+
for (var i=0; i<inlineDefines.length; i++) {
187+
var def = inlineDefines[i];
188+
if (def.substring(0,9) != 'function ' && def.substring(0,4) != 'var ') {
189+
def = "var "+def; // Enforce local
168190
}
191+
eval(def);
169192
}
170193
return eval(expr);
171-
}).bind(null)(defines, expr);
194+
}).bind(null)(runtimeDefines, inlineDefines, expr);
172195
};
173196

174197
/**
175-
* Runs the Preprocesses.
198+
* Preprocesses.
176199
* @param {object.<string,string>} defines Defines
177200
* @param {function(string)=} verbose Print verbose processing information to the specified function as the first parameter. Defaults to not print debug information.
178201
* @return {string} Processed source
@@ -221,7 +244,7 @@
221244
}
222245
include = match2[1];
223246
verbose(" expr: "+match2[1]);
224-
include = Preprocessor.evaluate(defines, match2[1]);
247+
include = Preprocessor.evaluate(defines, this.defines, match2[1]);
225248
verbose(" value: "+Preprocessor.nlToStr(include));
226249
this.source = this.source.substring(0, match.index)+indent+include+this.source.substring(Preprocessor.PUT.lastIndex);
227250
Preprocessor.EXPR.lastIndex = match.index + include.length;
@@ -240,7 +263,7 @@
240263
} else if (match2[1] == "ifndef") {
241264
include = !defines[match2[2]];
242265
} else {
243-
include = Preprocessor.evaluate(defines, match2[2]);
266+
include = Preprocessor.evaluate(defines, this.defines, match2[2]);
244267
}
245268
verbose(" value: "+include);
246269
stack.push(p={
@@ -280,7 +303,7 @@
280303
if (match2[1] == 'else') {
281304
include = !before["include"];
282305
} else {
283-
include = Preprocessor.evaluate(defines, match2[2]);
306+
include = Preprocessor.evaluate(defines, this.defines, match2[2]);
284307
}
285308
stack.push(p={
286309
"include": !before["include"],
@@ -290,6 +313,18 @@
290313
verbose(" push: "+JSON.stringify(p));
291314
}
292315
break;
316+
case 'define':
317+
// https://github.com/dcodeIO/Preprocessor.js/issues/5
318+
Preprocessor.DEFINE.lastIndex = match.index;
319+
if ((match2 = Preprocessor.DEFINE.exec(this.source)) === null) {
320+
throw(new Error("Illegal #"+match[2]+": "+this.source.substring(match.index, match.index+this.errorSourceAhead)+"..."));
321+
}
322+
var define = match2[1];
323+
verbose(" def: "+match2[1]);
324+
this.defines.push(define);
325+
this.source = this.source.substring(0, match.index)+indent+this.source.substring(Preprocessor.DEFINE.lastIndex);
326+
Preprocessor.EXPR.lastIndex = match.index;
327+
verbose(" continue at "+Preprocessor.EXPR.lastIndex);
293328
}
294329
}
295330
if (stack.length > 0) {

Preprocessor.min.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)