]>
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
1 // Generated by CoffeeScript 1.8.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
, 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; },
7 generate = function(tag
, value
, origin
) {
17 exports
.Rewriter
= (function() {
18 function Rewriter() {}
20 Rewriter
.prototype.rewrite = function(tokens
) {
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
, token
, tokens
, _ref
, _ref1
;
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
, tag
, _i
, _len
, _ref
;
66 for (i
= _i
= 0, _len
= _ref
.length
; _i
< _len
; i
= ++_i
) {
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.matchTags = function() {
112 var fuzz
, i
, j
, pattern
, _i
, _ref
, _ref1
;
113 i
= arguments
[0], pattern
= 2 <= arguments
.length
? __slice
.call(arguments
, 1) : [];
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') {
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) {
132 Rewriter
.prototype.looksObjectish = function(j
) {
133 return this.matchTags(j
, '@', null, ':') || this.matchTags(j
, null, ':');
136 Rewriter
.prototype.findTagsBackwards = function(i
, tags
) {
137 var backStack
, _ref
, _ref1
, _ref2
, _ref3
, _ref4
, _ref5
;
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
));
143 if ((_ref1
= this.tag(i
), __indexOf
.call(EXPRESSION_START
, _ref1
) >= 0) && backStack
.length
) {
148 return _ref5
= this.tag(i
), __indexOf
.call(tags
, _ref5
) >= 0;
151 Rewriter
.prototype.addImplicitBracesAndParens = function() {
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
;
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];
163 forward = function(n
) {
164 return i
- startIdx
+ n
;
166 inImplicit = function() {
168 return (_ref
= stackTop()) != null ? (_ref1
= _ref
[2]) != null ? _ref1
.ours : void 0 : void 0;
170 inImplicitCall = function() {
172 return inImplicit() && ((_ref
= stackTop()) != null ? _ref
[0] : void 0) === '(';
174 inImplicitObject = function() {
176 return inImplicit() && ((_ref
= stackTop()) != null ? _ref
[0] : void 0) === '{';
178 inImplicitControl = function() {
180 return inImplicit
&& ((_ref
= stackTop()) != null ? _ref
[0] : void 0) === 'CONTROL';
182 startImplicitCall = function(j
) {
184 idx
= j
!= null ? j : i
;
190 tokens
.splice(idx
, 0, generate('CALL_START', '('));
195 endImplicitCall = function() {
197 tokens
.splice(i
, 0, generate('CALL_END', ')'));
200 startImplicitObject = function(j
, startsLine
) {
202 if (startsLine
== null) {
205 idx
= j
!= null ? j : i
;
209 startsLine: startsLine
,
213 tokens
.splice(idx
, 0, generate('{', generate(new String('{')), token
));
218 endImplicitObject = function(j
) {
219 j
= j
!= null ? j : i
;
221 tokens
.splice(j
, 0, generate('}', '}', token
));
224 if (inImplicitCall() && (tag
=== 'IF' || tag
=== 'TRY' || tag
=== 'FINALLY' || tag
=== 'CATCH' || tag
=== 'CLASS' || tag
=== 'SWITCH')) {
232 if (tag
=== 'INDENT' && inImplicit()) {
233 if (prevTag
!== '=>' && prevTag
!== '->' && prevTag
!== '[' && prevTag
!== '(' && prevTag
!== ',' && prevTag
!== '{' && prevTag
!== 'TRY' && prevTag
!== 'ELSE' && prevTag
!== '=') {
234 while (inImplicitCall()) {
238 if (inImplicitControl()) {
241 stack
.push([tag
, i
]);
244 if (__indexOf
.call(EXPRESSION_START
, tag
) >= 0) {
245 stack
.push([tag
, i
]);
248 if (__indexOf
.call(EXPRESSION_END
, tag
) >= 0) {
249 while (inImplicit()) {
250 if (inImplicitCall()) {
252 } else if (inImplicitObject()) {
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))) {
262 tag
= token
[0] = 'FUNC_EXIST';
264 startImplicitCall(i
+ 1);
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]);
273 if (this.tag(i
- 2) === '@') {
278 while (this.tag(s
- 2) === 'HERECOMMENT') {
281 this.insideForDeclaration
= nextTag
=== 'FOR';
282 startsLine
= s
=== 0 || (_ref2
= this.tag(s
- 1), __indexOf
.call(LINEBREAKS
, _ref2
) >= 0) || tokens
[s
- 1].newLine
;
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) === '{')) {
289 startImplicitObject(s
, !!startsLine
);
292 if (inImplicitObject() && __indexOf
.call(LINEBREAKS
, tag
) >= 0) {
293 stackTop()[2].sameLine
= false;
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
!== ',') {
301 } else if (inImplicitObject() && !this.insideForDeclaration
&& sameLine
&& tag
!== 'TERMINATOR' && prevTag
!== ':' && endImplicitObject()) {
303 } else if (inImplicitObject() && tag
=== 'TERMINATOR' && prevTag
!== ',' && !(startsLine
&& this.looksObjectish(i
+ 1))) {
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
);
320 Rewriter
.prototype.addLocationDataToGeneratedTokens = function() {
321 return this.scanTokens(function(token
, i
, tokens
) {
322 var column
, line
, nextLocation
, prevLocation
, _ref
, _ref1
;
326 if (!(token
.generated
|| token
.explicit
)) {
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
;
338 first_column: column
,
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
;
353 action = function(token
, i
) {
354 return this.tokens
.splice((this.tag(i
- 1) === ',' ? i
- 1 : i
), 0, outdent
);
356 return this.scanTokens(function(token
, i
, tokens
) {
357 var j
, tag
, _i
, _ref
, _ref1
, _ref2
;
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())));
364 if (_ref
= this.tag(i
+ 1), __indexOf
.call(EXPRESSION_CLOSE
, _ref
) >= 0) {
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')) {
374 tokens
.splice
.apply(tokens
, [i
+ j
, 0].concat(__slice
.call(this.indentation())));
378 if (__indexOf
.call(SINGLE_LINERS
, tag
) >= 0 && this.tag(i
+ 1) !== 'INDENT' && !(tag
=== 'ELSE' && this.tag(i
+ 1) === 'IF')) {
380 _ref2
= this.indentation(tokens
[i
]), indent
= _ref2
[0], outdent
= _ref2
[1];
381 if (starter
=== 'THEN') {
382 indent
.fromThen
= true;
384 tokens
.splice(i
+ 1, 0, indent
);
385 this.detectEnd(i
+ 2, condition
, action
);
386 if (tag
=== 'THEN') {
395 Rewriter
.prototype.tagPostfixConditionals = function() {
396 var action
, condition
, original
;
398 condition = function(token
, i
) {
401 prevTag
= this.tokens
[i
- 1][0];
402 return tag
=== 'TERMINATOR' || (tag
=== 'INDENT' && __indexOf
.call(SINGLE_LINERS
, prevTag
) < 0);
404 action = function(token
, i
) {
405 if (token
[0] !== 'INDENT' || (token
.generated
&& !token
.fromThen
)) {
406 return original
[0] = 'POST_' + original
[0];
409 return this.scanTokens(function(token
, i
) {
410 if (token
[0] !== 'IF') {
414 this.detectEnd(i
+ 1, condition
, action
);
419 Rewriter
.prototype.indentation = function(origin
) {
421 indent
= ['INDENT', 2];
422 outdent
= ['OUTDENT', 2];
424 indent
.generated
= outdent
.generated
= true;
425 indent
.origin
= outdent
.origin
= origin
;
427 indent
.explicit
= outdent
.explicit
= true;
429 return [indent
, outdent
];
432 Rewriter
.prototype.generate
= generate
;
434 Rewriter
.prototype.tag = function(i
) {
436 return (_ref
= this.tokens
[i
]) != null ? _ref
[0] : void 0;
443 BALANCED_PAIRS
= [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
445 exports
.INVERSES
= INVERSES
= {};
447 EXPRESSION_START
= [];
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
);
457 EXPRESSION_CLOSE
= ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END
);
459 IMPLICIT_FUNC
= ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
461 IMPLICIT_CALL
= ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
463 IMPLICIT_UNSPACED_CALL
= ['+', '-'];
465 IMPLICIT_END
= ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
467 SINGLE_LINERS
= ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
469 SINGLE_CLOSERS
= ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
471 LINEBREAKS
= ['TERMINATOR', 'INDENT', 'OUTDENT'];
473 CALL_CLOSERS
= ['.', '?.', '::', '?::'];