Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
// Generated by CoffeeScript 1.12.6
2
(function() {
3
var BALANCED_PAIRS, CALL_CLOSERS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, generate, k, left, len, ref, rite,
4
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
5
slice = [].slice;
6
7
generate = function(tag, value, origin) {
8
var tok;
9
tok = [tag, value];
10
tok.generated = true;
11
if (origin) {
12
tok.origin = origin;
13
}
14
return tok;
15
};
16
17
exports.Rewriter = Rewriter = (function() {
18
function Rewriter() {}
19
20
Rewriter.prototype.rewrite = function(tokens1) {
21
this.tokens = tokens1;
22
this.removeLeadingNewlines();
23
this.closeOpenCalls();
24
this.closeOpenIndexes();
25
this.normalizeLines();
26
this.tagPostfixConditionals();
27
this.addImplicitBracesAndParens();
28
this.addLocationDataToGeneratedTokens();
29
this.fixOutdentLocationData();
30
return this.tokens;
31
};
32
33
Rewriter.prototype.scanTokens = function(block) {
34
var i, token, tokens;
35
tokens = this.tokens;
36
i = 0;
37
while (token = tokens[i]) {
38
i += block.call(this, token, i, tokens);
39
}
40
return true;
41
};
42
43
Rewriter.prototype.detectEnd = function(i, condition, action) {
44
var levels, ref, ref1, token, tokens;
45
tokens = this.tokens;
46
levels = 0;
47
while (token = tokens[i]) {
48
if (levels === 0 && condition.call(this, token, i)) {
49
return action.call(this, token, i);
50
}
51
if (!token || levels < 0) {
52
return action.call(this, token, i - 1);
53
}
54
if (ref = token[0], indexOf.call(EXPRESSION_START, ref) >= 0) {
55
levels += 1;
56
} else if (ref1 = token[0], indexOf.call(EXPRESSION_END, ref1) >= 0) {
57
levels -= 1;
58
}
59
i += 1;
60
}
61
return i - 1;
62
};
63
64
Rewriter.prototype.removeLeadingNewlines = function() {
65
var i, k, len, ref, tag;
66
ref = this.tokens;
67
for (i = k = 0, len = ref.length; k < len; i = ++k) {
68
tag = ref[i][0];
69
if (tag !== 'TERMINATOR') {
70
break;
71
}
72
}
73
if (i) {
74
return this.tokens.splice(0, i);
75
}
76
};
77
78
Rewriter.prototype.closeOpenCalls = function() {
79
var action, condition;
80
condition = function(token, i) {
81
var ref;
82
return ((ref = token[0]) === ')' || ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';
83
};
84
action = function(token, i) {
85
return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
86
};
87
return this.scanTokens(function(token, i) {
88
if (token[0] === 'CALL_START') {
89
this.detectEnd(i + 1, condition, action);
90
}
91
return 1;
92
});
93
};
94
95
Rewriter.prototype.closeOpenIndexes = function() {
96
var action, condition;
97
condition = function(token, i) {
98
var ref;
99
return (ref = token[0]) === ']' || ref === 'INDEX_END';
100
};
101
action = function(token, i) {
102
return token[0] = 'INDEX_END';
103
};
104
return this.scanTokens(function(token, i) {
105
if (token[0] === 'INDEX_START') {
106
this.detectEnd(i + 1, condition, action);
107
}
108
return 1;
109
});
110
};
111
112
Rewriter.prototype.indexOfTag = function() {
113
var fuzz, i, j, k, pattern, ref, ref1;
114
i = arguments[0], pattern = 2 <= arguments.length ? slice.call(arguments, 1) : [];
115
fuzz = 0;
116
for (j = k = 0, ref = pattern.length; 0 <= ref ? k < ref : k > ref; j = 0 <= ref ? ++k : --k) {
117
while (this.tag(i + j + fuzz) === 'HERECOMMENT') {
118
fuzz += 2;
119
}
120
if (pattern[j] == null) {
121
continue;
122
}
123
if (typeof pattern[j] === 'string') {
124
pattern[j] = [pattern[j]];
125
}
126
if (ref1 = this.tag(i + j + fuzz), indexOf.call(pattern[j], ref1) < 0) {
127
return -1;
128
}
129
}
130
return i + j + fuzz - 1;
131
};
132
133
Rewriter.prototype.looksObjectish = function(j) {
134
var end, index;
135
if (this.indexOfTag(j, '@', null, ':') > -1 || this.indexOfTag(j, null, ':') > -1) {
136
return true;
137
}
138
index = this.indexOfTag(j, EXPRESSION_START);
139
if (index > -1) {
140
end = null;
141
this.detectEnd(index + 1, (function(token) {
142
var ref;
143
return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;
144
}), (function(token, i) {
145
return end = i;
146
}));
147
if (this.tag(end + 1) === ':') {
148
return true;
149
}
150
}
151
return false;
152
};
153
154
Rewriter.prototype.findTagsBackwards = function(i, tags) {
155
var backStack, ref, ref1, ref2, ref3, ref4, ref5;
156
backStack = [];
157
while (i >= 0 && (backStack.length || (ref2 = this.tag(i), indexOf.call(tags, ref2) < 0) && ((ref3 = this.tag(i), indexOf.call(EXPRESSION_START, ref3) < 0) || this.tokens[i].generated) && (ref4 = this.tag(i), indexOf.call(LINEBREAKS, ref4) < 0))) {
158
if (ref = this.tag(i), indexOf.call(EXPRESSION_END, ref) >= 0) {
159
backStack.push(this.tag(i));
160
}
161
if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {
162
backStack.pop();
163
}
164
i -= 1;
165
}
166
return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;
167
};
168
169
Rewriter.prototype.addImplicitBracesAndParens = function() {
170
var stack, start;
171
stack = [];
172
start = null;
173
return this.scanTokens(function(token, i, tokens) {
174
var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, isImplicit, isImplicitCall, isImplicitObject, k, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, ref3, ref4, ref5, s, sameLine, stackIdx, stackItem, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;
175
tag = token[0];
176
prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0];
177
nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];
178
stackTop = function() {
179
return stack[stack.length - 1];
180
};
181
startIdx = i;
182
forward = function(n) {
183
return i - startIdx + n;
184
};
185
isImplicit = function(stackItem) {
186
var ref;
187
return stackItem != null ? (ref = stackItem[2]) != null ? ref.ours : void 0 : void 0;
188
};
189
isImplicitObject = function(stackItem) {
190
return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '{';
191
};
192
isImplicitCall = function(stackItem) {
193
return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '(';
194
};
195
inImplicit = function() {
196
return isImplicit(stackTop());
197
};
198
inImplicitCall = function() {
199
return isImplicitCall(stackTop());
200
};
201
inImplicitObject = function() {
202
return isImplicitObject(stackTop());
203
};
204
inImplicitControl = function() {
205
var ref;
206
return inImplicit && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';
207
};
208
startImplicitCall = function(j) {
209
var idx;
210
idx = j != null ? j : i;
211
stack.push([
212
'(', idx, {
213
ours: true
214
}
215
]);
216
tokens.splice(idx, 0, generate('CALL_START', '('));
217
if (j == null) {
218
return i += 1;
219
}
220
};
221
endImplicitCall = function() {
222
stack.pop();
223
tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]]));
224
return i += 1;
225
};
226
startImplicitObject = function(j, startsLine) {
227
var idx, val;
228
if (startsLine == null) {
229
startsLine = true;
230
}
231
idx = j != null ? j : i;
232
stack.push([
233
'{', idx, {
234
sameLine: true,
235
startsLine: startsLine,
236
ours: true
237
}
238
]);
239
val = new String('{');
240
val.generated = true;
241
tokens.splice(idx, 0, generate('{', val, token));
242
if (j == null) {
243
return i += 1;
244
}
245
};
246
endImplicitObject = function(j) {
247
j = j != null ? j : i;
248
stack.pop();
249
tokens.splice(j, 0, generate('}', '}', token));
250
return i += 1;
251
};
252
if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {
253
stack.push([
254
'CONTROL', i, {
255
ours: true
256
}
257
]);
258
return forward(1);
259
}
260
if (tag === 'INDENT' && inImplicit()) {
261
if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {
262
while (inImplicitCall()) {
263
endImplicitCall();
264
}
265
}
266
if (inImplicitControl()) {
267
stack.pop();
268
}
269
stack.push([tag, i]);
270
return forward(1);
271
}
272
if (indexOf.call(EXPRESSION_START, tag) >= 0) {
273
stack.push([tag, i]);
274
return forward(1);
275
}
276
if (indexOf.call(EXPRESSION_END, tag) >= 0) {
277
while (inImplicit()) {
278
if (inImplicitCall()) {
279
endImplicitCall();
280
} else if (inImplicitObject()) {
281
endImplicitObject();
282
} else {
283
stack.pop();
284
}
285
}
286
start = stack.pop();
287
}
288
if ((indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced || tag === '?' && i > 0 && !tokens[i - 1].spaced) && (indexOf.call(IMPLICIT_CALL, nextTag) >= 0 || indexOf.call(IMPLICIT_UNSPACED_CALL, nextTag) >= 0 && !((ref = tokens[i + 1]) != null ? ref.spaced : void 0) && !((ref1 = tokens[i + 1]) != null ? ref1.newLine : void 0))) {
289
if (tag === '?') {
290
tag = token[0] = 'FUNC_EXIST';
291
}
292
startImplicitCall(i + 1);
293
return forward(2);
294
}
295
if (indexOf.call(IMPLICIT_FUNC, tag) >= 0 && this.indexOfTag(i + 1, 'INDENT') > -1 && this.looksObjectish(i + 2) && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {
296
startImplicitCall(i + 1);
297
stack.push(['INDENT', i + 2]);
298
return forward(3);
299
}
300
if (tag === ':') {
301
s = (function() {
302
var ref2;
303
switch (false) {
304
case ref2 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref2) < 0:
305
return start[1];
306
case this.tag(i - 2) !== '@':
307
return i - 2;
308
default:
309
return i - 1;
310
}
311
}).call(this);
312
while (this.tag(s - 2) === 'HERECOMMENT') {
313
s -= 2;
314
}
315
this.insideForDeclaration = nextTag === 'FOR';
316
startsLine = s === 0 || (ref2 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref2) >= 0) || tokens[s - 1].newLine;
317
if (stackTop()) {
318
ref3 = stackTop(), stackTag = ref3[0], stackIdx = ref3[1];
319
if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {
320
return forward(1);
321
}
322
}
323
startImplicitObject(s, !!startsLine);
324
return forward(2);
325
}
326
if (indexOf.call(LINEBREAKS, tag) >= 0) {
327
for (k = stack.length - 1; k >= 0; k += -1) {
328
stackItem = stack[k];
329
if (isImplicitObject(stackItem)) {
330
stackItem[2].sameLine = false;
331
}
332
}
333
}
334
newLine = prevTag === 'OUTDENT' || prevToken.newLine;
335
if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {
336
while (inImplicit()) {
337
ref4 = stackTop(), stackTag = ref4[0], stackIdx = ref4[1], (ref5 = ref4[2], sameLine = ref5.sameLine, startsLine = ref5.startsLine);
338
if (inImplicitCall() && prevTag !== ',') {
339
endImplicitCall();
340
} else if (inImplicitObject() && !this.insideForDeclaration && sameLine && tag !== 'TERMINATOR' && prevTag !== ':') {
341
endImplicitObject();
342
} else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
343
if (nextTag === 'HERECOMMENT') {
344
return forward(1);
345
}
346
endImplicitObject();
347
} else {
348
break;
349
}
350
}
351
}
352
if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && !this.insideForDeclaration && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {
353
offset = nextTag === 'OUTDENT' ? 1 : 0;
354
while (inImplicitObject()) {
355
endImplicitObject(i + offset);
356
}
357
}
358
return forward(1);
359
});
360
};
361
362
Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
363
return this.scanTokens(function(token, i, tokens) {
364
var column, line, nextLocation, prevLocation, ref, ref1;
365
if (token[2]) {
366
return 1;
367
}
368
if (!(token.generated || token.explicit)) {
369
return 1;
370
}
371
if (token[0] === '{' && (nextLocation = (ref = tokens[i + 1]) != null ? ref[2] : void 0)) {
372
line = nextLocation.first_line, column = nextLocation.first_column;
373
} else if (prevLocation = (ref1 = tokens[i - 1]) != null ? ref1[2] : void 0) {
374
line = prevLocation.last_line, column = prevLocation.last_column;
375
} else {
376
line = column = 0;
377
}
378
token[2] = {
379
first_line: line,
380
first_column: column,
381
last_line: line,
382
last_column: column
383
};
384
return 1;
385
});
386
};
387
388
Rewriter.prototype.fixOutdentLocationData = function() {
389
return this.scanTokens(function(token, i, tokens) {
390
var prevLocationData;
391
if (!(token[0] === 'OUTDENT' || (token.generated && token[0] === 'CALL_END') || (token.generated && token[0] === '}'))) {
392
return 1;
393
}
394
prevLocationData = tokens[i - 1][2];
395
token[2] = {
396
first_line: prevLocationData.last_line,
397
first_column: prevLocationData.last_column,
398
last_line: prevLocationData.last_line,
399
last_column: prevLocationData.last_column
400
};
401
return 1;
402
});
403
};
404
405
Rewriter.prototype.normalizeLines = function() {
406
var action, condition, indent, outdent, starter;
407
starter = indent = outdent = null;
408
condition = function(token, i) {
409
var ref, ref1, ref2, ref3;
410
return token[1] !== ';' && (ref = token[0], indexOf.call(SINGLE_CLOSERS, ref) >= 0) && !(token[0] === 'TERMINATOR' && (ref1 = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref1) >= 0)) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((ref2 = token[0]) === 'CATCH' || ref2 === 'FINALLY') && (starter === '->' || starter === '=>')) || (ref3 = token[0], indexOf.call(CALL_CLOSERS, ref3) >= 0) && (this.tokens[i - 1].newLine || this.tokens[i - 1][0] === 'OUTDENT');
411
};
412
action = function(token, i) {
413
return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
414
};
415
return this.scanTokens(function(token, i, tokens) {
416
var j, k, ref, ref1, ref2, tag;
417
tag = token[0];
418
if (tag === 'TERMINATOR') {
419
if (this.tag(i + 1) === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
420
tokens.splice.apply(tokens, [i, 1].concat(slice.call(this.indentation())));
421
return 1;
422
}
423
if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {
424
tokens.splice(i, 1);
425
return 0;
426
}
427
}
428
if (tag === 'CATCH') {
429
for (j = k = 1; k <= 2; j = ++k) {
430
if (!((ref1 = this.tag(i + j)) === 'OUTDENT' || ref1 === 'TERMINATOR' || ref1 === 'FINALLY')) {
431
continue;
432
}
433
tokens.splice.apply(tokens, [i + j, 0].concat(slice.call(this.indentation())));
434
return 2 + j;
435
}
436
}
437
if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
438
starter = tag;
439
ref2 = this.indentation(tokens[i]), indent = ref2[0], outdent = ref2[1];
440
if (starter === 'THEN') {
441
indent.fromThen = true;
442
}
443
tokens.splice(i + 1, 0, indent);
444
this.detectEnd(i + 2, condition, action);
445
if (tag === 'THEN') {
446
tokens.splice(i, 1);
447
}
448
return 1;
449
}
450
return 1;
451
});
452
};
453
454
Rewriter.prototype.tagPostfixConditionals = function() {
455
var action, condition, original;
456
original = null;
457
condition = function(token, i) {
458
var prevTag, tag;
459
tag = token[0];
460
prevTag = this.tokens[i - 1][0];
461
return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);
462
};
463
action = function(token, i) {
464
if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
465
return original[0] = 'POST_' + original[0];
466
}
467
};
468
return this.scanTokens(function(token, i) {
469
if (token[0] !== 'IF') {
470
return 1;
471
}
472
original = token;
473
this.detectEnd(i + 1, condition, action);
474
return 1;
475
});
476
};
477
478
Rewriter.prototype.indentation = function(origin) {
479
var indent, outdent;
480
indent = ['INDENT', 2];
481
outdent = ['OUTDENT', 2];
482
if (origin) {
483
indent.generated = outdent.generated = true;
484
indent.origin = outdent.origin = origin;
485
} else {
486
indent.explicit = outdent.explicit = true;
487
}
488
return [indent, outdent];
489
};
490
491
Rewriter.prototype.generate = generate;
492
493
Rewriter.prototype.tag = function(i) {
494
var ref;
495
return (ref = this.tokens[i]) != null ? ref[0] : void 0;
496
};
497
498
return Rewriter;
499
500
})();
501
502
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END'], ['STRING_START', 'STRING_END'], ['REGEX_START', 'REGEX_END']];
503
504
exports.INVERSES = INVERSES = {};
505
506
EXPRESSION_START = [];
507
508
EXPRESSION_END = [];
509
510
for (k = 0, len = BALANCED_PAIRS.length; k < len; k++) {
511
ref = BALANCED_PAIRS[k], left = ref[0], rite = ref[1];
512
EXPRESSION_START.push(INVERSES[rite] = left);
513
EXPRESSION_END.push(INVERSES[left] = rite);
514
}
515
516
EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
517
518
IMPLICIT_FUNC = ['IDENTIFIER', 'PROPERTY', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
519
520
IMPLICIT_CALL = ['IDENTIFIER', 'PROPERTY', 'NUMBER', 'INFINITY', 'NAN', 'STRING', 'STRING_START', 'REGEX', 'REGEX_START', 'JS', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'UNDEFINED', 'NULL', 'BOOL', 'UNARY', 'YIELD', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
521
522
IMPLICIT_UNSPACED_CALL = ['+', '-'];
523
524
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
525
526
SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
527
528
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
529
530
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
531
532
CALL_CLOSERS = ['.', '?.', '::', '?::'];
533
534
}).call(this);
535
536