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