-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (114 loc) · 5.19 KB
/
index.js
File metadata and controls
129 lines (114 loc) · 5.19 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
//https://github.com/dcodeIO/Preprocessor.js/blob/master/Preprocessor.js
var fs=require('fs'), sysPath=require('path');
function process(defines,data,dirname,includedFiles){
var Preprocessor={
errorSourceAhead:50,
EXPR: /(^[ ]*)?\/\*[ ]*#(include(?:Once)?|ifn?def|ifelse|if|\/if|endif|else|el(?:se)?if|eval|value|val|setbasedir)[ ]*([^\*]*)[ ]*\*\//gm,
/**
* Indents a multi-line string.
* @param {string} str Multi-line string to indent
* @param {string} indent Indent to use
* @return {string} Indented string
* @expose
*/
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");
}
};
var match, match2, include, stack = [];
while ((match = Preprocessor.EXPR.exec(data)) !== null) {
//console.log(match,match.index,Preprocessor.EXPR.lastIndex);
var indent = match[1], instruction=match[2], content=match[3].trim();
switch (instruction) {
case 'eval':
case 'value': case 'val':
if(instruction==='eval') include = eval(content);
else include = String(defines[content]);
var removeAfterLength=0,
first5=data.substr(Preprocessor.EXPR.lastIndex,5),
first4=first5&&first5.substr(0,4),
first2=first4&&first4.substr(0,2);
if(first2){
if(first2==='0 ') removeAfterLength=2;
else if(['0;','0,','0)','0.','0+','0-'].indexOf(first2)!==-1) removeAfterLength=1;
else if(first2==="''") removeAfterLength=2;
else if(first5==='false') removeAfterLength=5;
else if(first4==='true') removeAfterLength=4;
}
data = data.substring(0, match.index)+include+data.substring(Preprocessor.EXPR.lastIndex + removeAfterLength);
Preprocessor.EXPR.lastIndex = match.index + include.length;
break;
case 'setdirname':
dirname = content.trimRight('/') + '/';
break;
case 'ifdef': case 'ifndef': case 'if': case 'ifelse':
if (instruction==='ifdef') include = defines.hasOwnProperty(content);//!!defines[match2[2]];
else if (instruction==='ifndef') include = defines.hasOwnProperty(content);//!defines[match2[2]];
else if (instruction==='ifelse') include = defines[content] ? 1 : 2;
else{
var ifThenMatch=/^(.*) then (.*)$/.exec(content);
if(ifThenMatch){
include = defines[ifThenMatch[1]] ? ifThenMatch[2] : '';
data = data.substring(0, match.index)+include+data.substring(Preprocessor.EXPR.lastIndex);
break;
}else if(content.slice(-2)==='=>'){
content=content.slice(0,-2).trim();
if(defines[ifThenMatchcontent])
data = data.substring(0, match.index)+data.substring(Preprocessor.EXPR.lastIndex);
else
data = data.substring(0, match.index)+data.substring(data.indexOf("\n",Preprocessor.EXPR.lastIndex));
}else{
if(content.charAt(0)==='!') include = !defines[content.substr(1).trim()];
else include = defines[content];
}
}
stack.push({ "include": include, "index": match.index, "lastIndex": Preprocessor.EXPR.lastIndex });
break;
case '/if': case 'endif': case 'else': case 'elif': case 'elseif':
if (stack.length == 0)
throw(new Error("Unexpected #"+instruction+": "+data.substring(match.index, match.index+Preprocessor.errorSourceAhead)+"..."));
var before = stack.pop();
include = data.substring(before["lastIndex"], match.index);
if(before.include === 1 || before.include === 2){
if(include.charAt(0)==='(' && include.slice(-1)===')') include=include.slice(1,-1);
include=include.split('||');
if(include.length !== 2) throw new Error('ifelse : '+include.length+' != 2 : '+include.join('||'));
include=include[before.include-1];
}else if(!before.include) include='';
data = data.substring(0, before["index"])+include+data.substring(Preprocessor.EXPR.lastIndex);
Preprocessor.EXPR.lastIndex = before["index"]+include.length;
if (instruction == "else" || instruction == "elif" || instruction == "elseif") {
if(instruction==='else') include=!before['include'];
else{
if(content.charAt(0)==='!') include = !defines[content.substr(1).trim()];
else include = defines[content];
}
stack.push({ "include": !before["include"], "index": Preprocessor.EXPR.lastIndex, "lastIndex": Preprocessor.EXPR.lastIndex });
}
break;
case 'include': case 'includeOnce':
if(content.slice(-1) === '/') content += sysPath.basename(content) + '.js';
else if(content.slice(-3) !== '.js') content += '.js';
if(content.substr(0,1) !== '/') content = dirname + content;
var path = fs.realpathSync(content);
if(instruction === 'includeOnce' && includedFiles.indexOf(path) !== -1) include = '';
else{
includedFiles.push(path);
include = fs.readFileSync(path);
include = module.exports(defines, content, baseDir, includedFiles);
}
data = data.substring(0, match.index) + content + data.substring(Preprocessor.EXPR.lastIndex + removeAfterLength);
break;
}
}
if(stack.length!==0) throw new Error('Still have stack : missing endif');
return data;
};
module.exports=function(defines,data,isBrowser,dirname){
defines=defines||{};
defines.NODE=!isBrowser; defines.BROWSER=!!isBrowser;
return process(defines,data,dirname,[]);
};