-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrunch.js
330 lines (295 loc) · 9.18 KB
/
crunch.js
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
const cppOperators = [
"=", "==", "!=", "<", ">", "<=", ">=", "+", "-", "*", "/", "%",
"++", "--", "&&", "||", "!", "&", "|", "^", "~", "<<", ">>",
"?", ":", "::", ".", "->", "[]", "()", "{}", "[]=", "->*", ".*", "+=", "-=", "*=", "/=", "%=",
"&=", "|=", "^=", "<<=", ">>=", ",", ";", "."
];
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function removeSpaces(input) {
const cppOperators = ["+", "-", "*", "/", "=", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "!", "&", "|", "^", "%", "<<", ">>", "++", "--"];
const symbols = ["{", "}", "(", ")", "[", "]", ",", ":", ";", "\"", "\\"];
for (let op of cppOperators) {
let parts = input.split(op);
input = parts.map(part => part.trim()).join(op);
}
for (let symbol of symbols) {
let parts = input.split(symbol);
input = parts.map(part => part.trim()).join(symbol);
}
let result = '';
let previousChar = '';
for (let i = 0; i < input.length; i++) {
let currentChar = input[i];
if (symbols.includes(currentChar) || symbols.includes(previousChar)) {
if (currentChar !== ' ') {
result += currentChar;
}
} else {
result += currentChar;
}
previousChar = currentChar;
}
return result;
}
function filterString(input) {
let left = '';
let quoted = '';
let right = '';
let i = 0;
for (i = 0; i < input.length; ++i) {
if(input[i] == '/' && i + 1 < input.length && input[i+1] == '/') {
// eat comment
while(i < input.length && input[i] != '\n')
i++;
continue;
}
if (input[i] == '\"')
break;
else
left += input[i];
}
let rightPart = input.substring(i);
let quotedResult = filterQuote(rightPart);
if (quotedResult) {
quoted = quotedResult[0];
let quoteEndIndex = quotedResult[1];
right = rightPart.substring(quoteEndIndex);
return [left, quoted, right];
}
}
function filterQuote(input) {
let quoted = '\"';
for (let i = 1; i < input.length; ++i) {
if (input[i] == '\\' && i < input.length - 1) {
quoted += input[i];
quoted += input[i + 1];
i += 1;
continue;
}
if (input[i] == '\"') {
quoted += '\"';
return [quoted, i + 1];
} else {
quoted += input[i];
}
}
return null;
}
function parseInput(input) {
let arr = [];
let pos = input.indexOf("\"");
while (input.length > 0) {
if(pos > 0 && input[pos-1] == '\\') {
pos = input.indexOf("\"", pos + 1);
}
let pos2 = input.indexOf("#");
if(pos != -1) {
let dbl_chk = input.indexOf("\"", pos+1);
if(dbl_chk == -1) {
return arr;
}
}
if (pos === -1) {
let lines = input.split('\n');
for (let line of lines) {
if (line.trim() === '') continue;
if (line.startsWith('#')) {
arr.push(line);
} else {
let words = line.split('\n').filter(word => word.trim() !== '');
arr.push(...words);
}
}
break;
} else if(pos2 != -1 && pos2 < pos) {;
if(input.trim().startsWith("#")) {
let posx = input.indexOf("\n");
if(posx != -1) {
let left = input.substring(0, posx);
arr.push(left);
let right = input.substring(posx + 1);
input = right;
pos = input.indexOf("\"");
continue;
} else {
arr.push(input);
return arr;
}
}
else {
let nl = input.indexOf("\n");
if(nl != -1 && nl < pos2) {
let left = input.substring(0, nl);
let right = input.substring(nl+1);
let words = left.split('\n').filter(word => word.trim() !== '');
arr.push(...words);
input = right;
pos = input.indexOf("\"");
continue;
} else {
console.log(input);
}
}
}
else {
let filter_str = filterString(input);
let left = filter_str[0];
let quotedValue = filter_str[1];
let remaining = filter_str[2];
if (left.length > 0) {
let lines = left.split('\n');
for (let line of lines) {
if (line.trim() === '') continue;
if (line.trim().startsWith('#')) {
arr.push(line);
} else {
let words = line.split('\n').filter(word => word.trim() !== '');
arr.push(...words);
}
}
}
arr.push(quotedValue.trim());
input = remaining;
}
pos = input.indexOf("\"");
}
return arr;
}
let regular = false;
function procLine(line) {
let output = '';
if (line.trim() === '') {
regular = false;
return '';
}
if (line.trim().startsWith("\"")) {
output += line;
regular = false;
} else if (line.trim().startsWith('#')) {
if(regular == true) {
output += "\n";
}
regular = false;
output += line + "\n";
} else {
let linez = crunchLine(line) + ' ';
linez = linez.replace(/\s\s+/g, ' ');
linez = removeSpaces(linez);
output += linez;
if(linez.length > 0)
regular = true;
}
return output;
}
function crunch(input) {
let data = input;
if(data.indexOf("\n") == -1) {
return procLine(data);
}
data = removeMlComment(data);
data = removeComment(data);
const lines = parseInput(data);
let output = '';
for (let i = 0; i < lines.length; ++i) {
let line = lines[i];
if(line.trim() == '') continue;
output += procLine(line);
}
return output;
}
function removeMlComment(text) {
let temp = '';
for (let i = 0; i < text.length; i++) {
if (chkChr(text, i, '/') && chkChr(text, i + 1, '*')) {
i++;
do {
i++;
} while (!(chkChr(text, i, '*') && chkChr(text, i + 1, '/')));
i++;
} else {
temp += text[i];
}
}
return temp;
}
function removeComment(input) {
let output = '';
let i = 0;
let inDoubleQuote = false;
let inSingleQuote = false;
while (i < input.length) {
if (input[i] === '\"' && (i === 0 || input[i - 1] !== '\\') && !inSingleQuote) {
inDoubleQuote = !inDoubleQuote;
output += input[i];
i++;
} else if (input[i] === '\'' && (i === 0 || input[i - 1] !== '\\') && !inDoubleQuote) {
inSingleQuote = !inSingleQuote;
output += input[i];
i++;
} else if (!inDoubleQuote && !inSingleQuote && input[i] === '/' && i + 1 < input.length && input[i + 1] === '/') {
while (i < input.length && input[i] !== '\n') {
i++;
}
} else {
output += input[i];
i++;
}
}
console.log(output);
return output;
}
function chkChr(text, i, c) {
return i < text.length && text[i] === c;
}
function crunchLine(s) {
let output = '';
let grabString = false;
let grabChar = false;
for (let i = 0; i < s.length; i++) {
if (!grabString && !grabChar) {
if (!testchr(s, i)) {
continue;
}
if (s[i] === '/' && i + 1 < s.length && s[i + 1] === '/') {
if (output.endsWith(' ')) {
output = output.slice(0, -1);
}
return output;
}
}
if (s[i] === '\\' && (grabString || grabChar)) {
output += s[i];
if (i + 1 < s.length) {
output += s[i + 1];
i++;
}
continue;
} else if (s[i] === '\"') {
grabString = !grabString;
} else if (s[i] === '\'' && !grabString) {
grabChar = !grabChar;
}
output += s[i];
}
return output;
}
function testchr(s, i) {
if (s[i] === '\t' || s[i] === '\r' || s[i] === '\n') {
return false;
}
if (s[i] === ' ' && i + 1 < s.length && s[i + 1] === ' ') {
i++;
return false;
}
if (s[i] === '\\' && i + 1 < s.length && s[i + 1] !== '\\') {
return false;
}
return true;
}
function crunchText() {
const inputText = document.getElementById('inputText').value;
const outputText = crunch(inputText);
document.getElementById('output').innerText = outputText;
}