-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvariables-parser.js
More file actions
253 lines (199 loc) · 8.64 KB
/
Copy pathvariables-parser.js
File metadata and controls
253 lines (199 loc) · 8.64 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
VariablesParser = function() {
this.parse = function(data, verbose, noRename, entry) {
function parseConst(code) {
var comments = '';
var match;
if (match = /((?:\s*'.*?\n)+)(?=\bconst\b\s+)/gi.exec(code)) {
comments = match[1];
code = code.replace(match[1], '');
}
// Sanity check to make sure we don't have any commented out variables
if (code.search(/^\s*'.*?\bdim\b.*$/gmi) != -1)
return null;
if (match = /(private|public)?\s*const\s+(\w+)(?:\s*as\s*(\w+))?\s*=\s*(".*"|[\S\w]+)*\s*('.*)*/gi.exec(code)) {
var visibility = match[1];
if (visibility === undefined)
visibility = "Public";
var result = {
'visibility': visibility,
'name': match[2],
'var': match[2],
'type': match[3],
'value': match[4],
'comment': ( match[5] != undefined ? match[5] : comments ).replace(/^'/gm, '').split('\n'),
'hits': 0
};
result['class'] = entry.class;
// Prevent short consts like 'rs' making into the wild
if (result.name.length < 4) {
if (verbose)
console.log('WARNING: Const => ' + result.name + ' is less than 4 chars, marking it Private!');
result.visibility = 'Private';
}
}
else {
throw "unable to parse const => " + code;
}
return result;
}
function isReservedWord( w ){
w = w.toLowerCase();
if (w === 'cdate') return true;
if (w === 'char') return true;
if (w === 'ctype') return true;
if (w === 'when') return true;
if (w === 'return') return true;
return false;
}
function parseDim(code) {
var comments;
var match;
var start = code.search(/^\s*((public|private)\s+)?\b(?:dim)?\b/mi);
comments = code.substr(0, start);
code = code.substr(start);
// Sanity check to make sure we don't have any commented out variables
if (code.search(/^\s*'.*?\b(?:dim)?\b.*$/gmi) != -1)
return null;
if (comments.trim().length > 0) {
comments = comments.replace(/^'/gm, '').split('\n');
// If there are 2 previous blank lines before the comments, assume its junk and scrap the comment
if (comments.length > 2 && comments[comments.length - 1].trim().length == 0 && comments[comments.length - 2].trim().length == 0)
comments = [];
} else
comments = [];
if (match = /('.*)/gi.exec(code)) {
comments = match[1].replace(/^'/gm, '').split('\n');
code = code.replace(match[1], '');
}
code = code.replace(/[\n]/g, '');
var results = [];
var visibility;
if (match = /(\s*(?:(public|private)?\s*(?:dim)?)\s*)/gi.exec(code)) {
code = code.replace(match[1], '');
visibility = match[2];
}
if (visibility === undefined)
visibility = "Public";
code = code.replace(/^dim\s+/i, '');
var regEx = /(\w+(?:\s*\(.*?\))*)\s*:*\s*(?:set\s+)?(\w+\s*=\s*(.*|".*"))*\s*('.*)*/gi;
while (( match = regEx.exec(code)) != null) {
var thisVar = noRename ? match[1] : match[1].replace('g_', '').replace(/^x+_/gmi, '').replace(/_x+$/gmi, '').replace(/_/g, ' ').replace(/(\b[a-z](?!\s))/g, function (x) {
return x.toUpperCase();
}).replace(/ /g, '');
if ( isReservedWord(thisVar) ){
thisVar = '_' + thisVar;
}
var result = {
'visibility': visibility,
'name': match[1],
'var': thisVar,
'init': match[2],
'value': match[3],
'comment': ( results.length == 0 ? comments : undefined ),
'hits': 0
};
// Prevent short variables like 'rs' making into the wild
if (result.name.length < 4) {
/*
if (isNumeric(entry.name)) {
entry.name = '_' + entry.name;
if (verbose)
console.log('BIG WARNING: Variable => ' + result.name + ' has a numeric name!. I\'ll rename with and underscore to prevent mass updates');
}
*/
if (verbose)
console.log('WARNING: Variable => ' + result.name + ' is less than 4 chars, marking it Private!');
result.visibility = 'Private';
}
results.push(result);
}
if (results.length == 0) {
throw "unable to parse dim => " + code;
}
return results;
}
// Strip out all DIM and CONST declarations
//var regEx = /^(((?:'.*?\n){0,}?)(?:private|public)?\s*\b(const+|dim?)\b\s+(?:[\s\S]+?))\s*$/gmi;
var regEx = /^(((?:'.*?\n){0,}?)\s*(?:(?:private|public)?\s*\b(const|dim)|((?:private|public)+))\b\s+(?:[\s\S]+?))\s*$/gmi;
var remainingData = data;
var globalBlocks = '';
var constDecls = [];
var variableDecls = [];
var match;
while (( match = regEx.exec(data) ) != null) {
var codeBlock = match[0];
var endPos = match[0].length;
if (codeBlock.replace('', '').trim().endsWith(',')) {
codeBlock += '\n';
var offset = match.index + match[0].length + 1;
var pos;
while ((pos = data.indexOf('\n', offset)) != -1) {
var nextLine = data.substr(offset, pos - offset);
offset += nextLine.length + 1;
endPos = offset;
codeBlock += nextLine + '\n';
if (!nextLine.replace('', '').trim().endsWith(','))
break;
}
}
remainingData = remainingData.replace(codeBlock, "");
switch (match[3] !== undefined ? match[3].toLowerCase() : '' ) {
case 'dim':
var variableDecl = parseDim(codeBlock);
if (variableDecl != null)
variableDecls.push(variableDecl);
else
globalBlocks += '\t' + codeBlock;
break;
case 'const':
var constDecl = parseConst(codeBlock);
if (constDecl != null)
constDecls.push(constDecl);
else
globalBlocks += '\t' + codeBlock;
break;
default:
var variableDecl = parseDim(codeBlock);
if (variableDecl != null)
variableDecls.push(variableDecl);
else
globalBlocks += '\t' + codeBlock;
break;
//globalBlocks += '\t' + codeBlock;
}
}
variableDecls.forEach = function (f){
for (var i = 0 ; i < this.length ; i++ ){
for( var x = 0 ; x < this[i].length ; x++ ){
f( this[i][x], i, x );
}
}
};
constDecls.forEach = function (f){
for (var i = 0 ; i < this.length ; i++ ){
f( this[i], i );
}
};
variableDecls.contains = function (_what){
if ( this.length == 0 )
return false;
if ( _what === undefined )
return false;
_what = _what.toLowerCase();
for (var i = 0 ; i < this.length ; i++ ){
for( var x = 0 ; x < this[i].length ; x++ ){
if ( this[i][x].name.toLowerCase() === _what )
return true;
}
}
return false;
};
return {
'data': remainingData.replace(/\n\n/gi, '\n').trim(),
'consts': constDecls,
'vars': variableDecls,
'unmatched': globalBlocks
}
}
}
exports = module.exports = new VariablesParser();