1 // Generated by CoffeeScript 1.10.0
3 var BALANCED_PAIRS, CALL_CLOSERS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, 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; },
7 generate = function(tag, value, origin) {
17 exports.Rewriter = (function() {
18 function Rewriter() {}
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();
32 Rewriter.prototype.scanTokens = function(block) {
36 while (token = tokens[i]) {
37 i += block.call(this, token, i, tokens);
42 Rewriter.prototype.detectEnd = function(i, condition, action) {
43 var levels, ref, ref1, token, tokens;
46 while (token = tokens[i]) {
47 if (levels === 0 && condition.call(this, token, i)) {
48 return action.call(this, token, i);
50 if (!token || levels < 0) {
51 return action.call(this, token, i - 1);
53 if (ref = token[0], indexOf.call(EXPRESSION_START, ref) >= 0) {
55 } else if (ref1 = token[0], indexOf.call(EXPRESSION_END, ref1) >= 0) {
63 Rewriter.prototype.removeLeadingNewlines = function() {
64 var i, k, len, ref, tag;
66 for (i = k = 0, len = ref.length; k < len; i = ++k) {
68 if (tag !== 'TERMINATOR') {
73 return this.tokens.splice(0, i);
77 Rewriter.prototype.closeOpenCalls = function() {
78 var action, condition;
79 condition = function(token, i) {
81 return ((ref = token[0]) === ')' || ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';
83 action = function(token, i) {
84 return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
86 return this.scanTokens(function(token, i) {
87 if (token[0] === 'CALL_START') {
88 this.detectEnd(i + 1, condition, action);
94 Rewriter.prototype.closeOpenIndexes = function() {
95 var action, condition;
96 condition = function(token, i) {
98 return (ref = token[0]) === ']' || ref === 'INDEX_END';
100 action = function(token, i) {
101 return token[0] = 'INDEX_END';
103 return this.scanTokens(function(token, i) {
104 if (token[0] === 'INDEX_START') {
105 this.detectEnd(i + 1, condition, action);
111 Rewriter.prototype.indexOfTag = function() {
112 var fuzz, i, j, k, pattern, ref, ref1;
113 i = arguments[0], pattern = 2 <= arguments.length ? slice.call(arguments, 1) : [];
115 for (j = k = 0, ref = pattern.length; 0 <= ref ? k < ref : k > ref; j = 0 <= ref ? ++k : --k) {
116 while (this.tag(i + j + fuzz) === 'HERECOMMENT') {
119 if (pattern[j] == null) {
122 if (typeof pattern[j] === 'string') {
123 pattern[j] = [pattern[j]];
125 if (ref1 = this.tag(i + j + fuzz), indexOf.call(pattern[j], ref1) < 0) {
129 return i + j + fuzz - 1;
132 Rewriter.prototype.looksObjectish = function(j) {
134 if (this.indexOfTag(j, '@', null, ':') > -1 || this.indexOfTag(j, null, ':') > -1) {
137 index = this.indexOfTag(j, EXPRESSION_START);
140 this.detectEnd(index + 1, (function(token) {
142 return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;
143 }), (function(token, i) {
146 if (this.tag(end + 1) === ':') {
153 Rewriter.prototype.findTagsBackwards = function(i, tags) {
154 var backStack, ref, ref1, ref2, ref3, ref4, ref5;
156 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))) {
157 if (ref = this.tag(i), indexOf.call(EXPRESSION_END, ref) >= 0) {
158 backStack.push(this.tag(i));
160 if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {
165 return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;
168 Rewriter.prototype.addImplicitBracesAndParens = function() {
172 return this.scanTokens(function(token, i, tokens) {
173 var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, ref3, ref4, ref5, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;
175 prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0];
176 nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];
177 stackTop = function() {
178 return stack[stack.length - 1];
181 forward = function(n) {
182 return i - startIdx + n;
184 inImplicit = function() {
186 return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;
188 inImplicitCall = function() {
190 return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';
192 inImplicitObject = function() {
194 return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';
196 inImplicitControl = function() {
198 return inImplicit && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';
200 startImplicitCall = function(j) {
202 idx = j != null ? j : i;
208 tokens.splice(idx, 0, generate('CALL_START', '('));
213 endImplicitCall = function() {
215 tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]]));
218 startImplicitObject = function(j, startsLine) {
220 if (startsLine == null) {
223 idx = j != null ? j : i;
227 startsLine: startsLine,
231 val = new String('{');
232 val.generated = true;
233 tokens.splice(idx, 0, generate('{', val, token));
238 endImplicitObject = function(j) {
239 j = j != null ? j : i;
241 tokens.splice(j, 0, generate('}', '}', token));
244 if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {
252 if (tag === 'INDENT' && inImplicit()) {
253 if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {
254 while (inImplicitCall()) {
258 if (inImplicitControl()) {
261 stack.push([tag, i]);
264 if (indexOf.call(EXPRESSION_START, tag) >= 0) {
265 stack.push([tag, i]);
268 if (indexOf.call(EXPRESSION_END, tag) >= 0) {
269 while (inImplicit()) {
270 if (inImplicitCall()) {
272 } else if (inImplicitObject()) {
280 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))) {
282 tag = token[0] = 'FUNC_EXIST';
284 startImplicitCall(i + 1);
287 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'])) {
288 startImplicitCall(i + 1);
289 stack.push(['INDENT', i + 2]);
296 case ref2 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref2) < 0:
298 case this.tag(i - 2) !== '@':
304 while (this.tag(s - 2) === 'HERECOMMENT') {
307 this.insideForDeclaration = nextTag === 'FOR';
308 startsLine = s === 0 || (ref2 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref2) >= 0) || tokens[s - 1].newLine;
310 ref3 = stackTop(), stackTag = ref3[0], stackIdx = ref3[1];
311 if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {
315 startImplicitObject(s, !!startsLine);
318 if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) {
319 stackTop()[2].sameLine = false;
321 newLine = prevTag === 'OUTDENT' || prevToken.newLine;
322 if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {
323 while (inImplicit()) {
324 ref4 = stackTop(), stackTag = ref4[0], stackIdx = ref4[1], (ref5 = ref4[2], sameLine = ref5.sameLine, startsLine = ref5.startsLine);
325 if (inImplicitCall() && prevTag !== ',') {
327 } else if (inImplicitObject() && !this.insideForDeclaration && sameLine && tag !== 'TERMINATOR' && prevTag !== ':') {
329 } else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
330 if (nextTag === 'HERECOMMENT') {
339 if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && !this.insideForDeclaration && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {
340 offset = nextTag === 'OUTDENT' ? 1 : 0;
341 while (inImplicitObject()) {
342 endImplicitObject(i + offset);
349 Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
350 return this.scanTokens(function(token, i, tokens) {
351 var column, line, nextLocation, prevLocation, ref, ref1;
355 if (!(token.generated || token.explicit)) {
358 if (token[0] === '{' && (nextLocation = (ref = tokens[i + 1]) != null ? ref[2] : void 0)) {
359 line = nextLocation.first_line, column = nextLocation.first_column;
360 } else if (prevLocation = (ref1 = tokens[i - 1]) != null ? ref1[2] : void 0) {
361 line = prevLocation.last_line, column = prevLocation.last_column;
367 first_column: column,
375 Rewriter.prototype.normalizeLines = function() {
376 var action, condition, indent, outdent, starter;
377 starter = indent = outdent = null;
378 condition = function(token, i) {
379 var ref, ref1, ref2, ref3;
380 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;
382 action = function(token, i) {
383 return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
385 return this.scanTokens(function(token, i, tokens) {
386 var j, k, ref, ref1, ref2, tag;
388 if (tag === 'TERMINATOR') {
389 if (this.tag(i + 1) === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
390 tokens.splice.apply(tokens, [i, 1].concat(slice.call(this.indentation())));
393 if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {
398 if (tag === 'CATCH') {
399 for (j = k = 1; k <= 2; j = ++k) {
400 if (!((ref1 = this.tag(i + j)) === 'OUTDENT' || ref1 === 'TERMINATOR' || ref1 === 'FINALLY')) {
403 tokens.splice.apply(tokens, [i + j, 0].concat(slice.call(this.indentation())));
407 if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
409 ref2 = this.indentation(tokens[i]), indent = ref2[0], outdent = ref2[1];
410 if (starter === 'THEN') {
411 indent.fromThen = true;
413 tokens.splice(i + 1, 0, indent);
414 this.detectEnd(i + 2, condition, action);
415 if (tag === 'THEN') {
424 Rewriter.prototype.tagPostfixConditionals = function() {
425 var action, condition, original;
427 condition = function(token, i) {
430 prevTag = this.tokens[i - 1][0];
431 return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);
433 action = function(token, i) {
434 if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
435 return original[0] = 'POST_' + original[0];
438 return this.scanTokens(function(token, i) {
439 if (token[0] !== 'IF') {
443 this.detectEnd(i + 1, condition, action);
448 Rewriter.prototype.indentation = function(origin) {
450 indent = ['INDENT', 2];
451 outdent = ['OUTDENT', 2];
453 indent.generated = outdent.generated = true;
454 indent.origin = outdent.origin = origin;
456 indent.explicit = outdent.explicit = true;
458 return [indent, outdent];
461 Rewriter.prototype.generate = generate;
463 Rewriter.prototype.tag = function(i) {
465 return (ref = this.tokens[i]) != null ? ref[0] : void 0;
472 BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END'], ['STRING_START', 'STRING_END'], ['REGEX_START', 'REGEX_END']];
474 exports.INVERSES = INVERSES = {};
476 EXPRESSION_START = [];
480 for (k = 0, len = BALANCED_PAIRS.length; k < len; k++) {
481 ref = BALANCED_PAIRS[k], left = ref[0], rite = ref[1];
482 EXPRESSION_START.push(INVERSES[rite] = left);
483 EXPRESSION_END.push(INVERSES[left] = rite);
486 EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
488 IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
490 IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'STRING_START', 'JS', 'REGEX', 'REGEX_START', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'YIELD', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
492 IMPLICIT_UNSPACED_CALL = ['+', '-'];
494 IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
496 SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
498 SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
500 LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
502 CALL_CLOSERS = ['.', '?.', '::', '?::'];