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