This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Preprocessor.min.js
17 lines (17 loc) · 5.67 KB
/
Preprocessor.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
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
*/
var l=null;
(function(m){function a(a,c,b){this.source=""+a;this.baseDir="string"==typeof c?c:".";this.e="object"==typeof c?c:{};this.i="boolean"==typeof b?b:!1;this.isNode=("undefined"==typeof window||!window.window)&&"function"==typeof require;this.errorSourceAhead=50;this.d=[]}a.a=/([ ]*)\/\/[ ]+#(include_once|include|ifn?def|if|endif|else|elif|put|define)/g;a.g=/(include_once|include)[ ]+"([^"\\]*(\\.[^"\\]*)*)"[ ]*\r?(?:\n|$)/g;a.f=/(ifdef|ifndef|if)[ ]*([^\r\n]+)\r?\n/g;a.b=/(endif|else|elif)([ ]+[^\r\n]+)?\r?(?:\n|$)/g;a.h=
/put[ ]+([^\n]+)[ ]*/g;a.c=/define[ ]+([^\n]+)\r?(?:\n|$)/g;var p=/(?:^|[^\\])\*/,n=/[^\r\n]/g;a.stripSlashes=function(a){return(a+"").replace(/\\(.?)/g,function(a,b){switch(b){case "\\":return"\\";case "0":return"\x00";case "":return"";default:return b}})};a.addSlashes=function(a){return(a+"").replace(/([\\"'])/g,"\\$1").replace(/\0/g,"\\0")};a.indent=function(a,c){for(var b=a.split("\n"),e=0;e<b.length;e++)b[e]=c+b[e];return b.join("\n")};a.nlToStr=function(a){return"["+a.replace(/\r/g,"").replace(/\n/g,
"\\n")+"]"};a.evaluate=function(h,c,b){"string"===typeof c&&(b=c,c=[]);var e=a.addSlashes;return function(a,b,c){for(var g in a)a.hasOwnProperty(g)&&eval("var "+g+' = "'+e(""+a[g])+'";');for(a=0;a<b.length;a++)g=b[a],"function "!=g.substring(0,9)&&"var "!=g.substring(0,4)&&(g="var "+g),eval(g);return eval(c)}.call(l,h,c,b)};a.prototype.process=function(h,c){h=h||{};c="function"==typeof c?c:function(){};c("Defines: "+JSON.stringify(h));for(var b,e,d,k=[];(b=a.a.exec(this.source))!==l;){c(b[2]+" @ "+
b.index+"-"+a.a.lastIndex);var f=b[1];switch(b[2]){case "include_once":case "include":a.g.lastIndex=b.index;if((e=a.g.exec(this.source))===l)throw Error("Illegal #"+b[2]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+"...");d=a.stripSlashes(e[2]);if("undefined"!==typeof this.e[d])"include_once"===e[1]?(c(" skip incl: "+d),d=""):(c(" incl: "+d),d=this.e[d]);else{if(!this.isNode)throw Error("Failed to resolve include: "+this.baseDir+"/"+d);try{var g=d,m=require("fs");if(p.test(d)){var q=
require("glob");c(" glob incl: "+this.baseDir+"/"+d);var r=this;q(this.baseDir+"/"+d,{sync:!0},function(a,b){if(a)throw a;d="";for(var e=0;e<b.length;e++){c(" incl: "+b[e]);var f=m.readFileSync(b[e])+"";r.e[g]=f;d+=f}})}else c(" incl: "+d),d=m.readFileSync(this.baseDir+"/"+d)+"",this.e[g]=d}catch(s){throw Error("Include failed: "+d+" ("+s+")");}}this.source=this.source.substring(0,b.index)+a.indent(d,f)+this.source.substring(a.g.lastIndex);a.a.lastIndex=0<k.length?k[k.length-1].lastIndex:0;c(" continue at "+
a.a.lastIndex);break;case "put":a.h.lastIndex=b.index;if((e=a.h.exec(this.source))===l)throw Error("Illegal #"+b[2]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+"...");d=e[1];c(" expr: "+e[1]);d=a.evaluate(h,this.d,e[1]);c(" value: "+a.nlToStr(d));this.source=this.source.substring(0,b.index)+f+d+this.source.substring(a.h.lastIndex);a.a.lastIndex=b.index+d.length;c(" continue at "+a.a.lastIndex);break;case "ifdef":case "ifndef":case "if":a.f.lastIndex=b.index;if((e=a.f.exec(this.source))===
l)throw Error("Illegal #"+b[2]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+"...");c(" test: "+e[2]);d="ifdef"==e[1]?!!h[e[2]]:"ifndef"==e[1]?!h[e[2]]:a.evaluate(h,this.d,e[2]);c(" value: "+d);k.push(b={include:d,index:b.index,lastIndex:a.f.lastIndex});c(" push: "+JSON.stringify(b));break;case "endif":case "else":case "elif":a.b.lastIndex=b.index;if((e=a.b.exec(this.source))===l)throw Error("Illegal #"+b[2]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+
"...");if(0==k.length)throw Error("Unexpected #"+e[1]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+"...");f=k.pop();c(" pop: "+JSON.stringify(f));d=this.i?this.source.substring(f.index,f.lastIndex).replace(n,"")+this.source.substring(f.lastIndex,b.index)+this.source.substring(b.index,a.b.lastIndex).replace(n,""):this.source.substring(f.lastIndex,b.index);f.include?(c(" incl: "+a.nlToStr(d)+", 0-"+f.index+" + "+d.length+" bytes + "+a.b.lastIndex+"-"+this.source.length),this.source=
this.source.substring(0,f.index)+d+this.source.substring(a.b.lastIndex)):this.i?(c(" excl(\\n): "+a.nlToStr(d)+", 0-"+f.index+" + "+a.b.lastIndex+"-"+this.source.length),d=d.replace(n,""),this.source=this.source.substring(0,f.index)+d+this.source.substring(a.b.lastIndex)):(c(" excl: "+a.nlToStr(d)+", 0-"+f.index+" + "+a.b.lastIndex+"-"+this.source.length),d="",this.source=this.source.substring(0,f.index)+this.source.substring(a.b.lastIndex));""==this.source&&c(" result empty");a.a.lastIndex=f.index+
d.length;c(" continue at "+a.a.lastIndex);if("else"==e[1]||"elif"==e[1])d="else"==e[1]?!f.include:a.evaluate(h,this.d,e[2]),k.push(b={include:!f.include,index:a.a.lastIndex,lastIndex:a.a.lastIndex}),c(" push: "+JSON.stringify(b));break;case "define":a.c.lastIndex=b.index;if((e=a.c.exec(this.source))===l)throw Error("Illegal #"+b[2]+": "+this.source.substring(b.index,b.index+this.errorSourceAhead)+"...");var t=e[1];c(" def: "+e[1]);this.d.push(t);e="";this.i&&(e=this.source.substring(b.index,a.c.lastIndex).replace(n,
""));this.source=this.source.substring(0,b.index)+f+e+this.source.substring(a.c.lastIndex);a.a.lastIndex=b.index;c(" continue at "+a.a.lastIndex)}}0<k.length&&(f=k.pop(),c("Still on stack: "+JSON.stringify(f)));return this.source};a.prototype.toString=function(){return"Preprocessor"};"undefined"!=typeof module&&module.exports?module.exports=a:"undefined"!=typeof define&&define.amd?define("Preprocessor",[],function(){return a}):(m.dcodeIO||(m.dcodeIO={}),m.dcodeIO.Preprocessor=a)})(this);