UNPKG

13.2 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.4.0
2(function() {
3 var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, 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 exports.Rewriter = (function() {
8
9 function Rewriter() {}
10
11 Rewriter.prototype.rewrite = function(tokens) {
12 this.tokens = tokens;
13 this.removeLeadingNewlines();
14 this.removeMidExpressionNewlines();
15 this.closeOpenCalls();
16 this.closeOpenIndexes();
17 this.addImplicitIndentation();
18 this.tagPostfixConditionals();
19 this.addImplicitBraces();
20 this.addImplicitParentheses();
21 return this.tokens;
22 };
23
24 Rewriter.prototype.scanTokens = function(block) {
25 var i, token, tokens;
26 tokens = this.tokens;
27 i = 0;
28 while (token = tokens[i]) {
29 i += block.call(this, token, i, tokens);
30 }
31 return true;
32 };
33
34 Rewriter.prototype.detectEnd = function(i, condition, action) {
35 var levels, token, tokens, _ref, _ref1;
36 tokens = this.tokens;
37 levels = 0;
38 while (token = tokens[i]) {
39 if (levels === 0 && condition.call(this, token, i)) {
40 return action.call(this, token, i);
41 }
42 if (!token || levels < 0) {
43 return action.call(this, token, i - 1);
44 }
45 if (_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) {
46 levels += 1;
47 } else if (_ref1 = token[0], __indexOf.call(EXPRESSION_END, _ref1) >= 0) {
48 levels -= 1;
49 }
50 i += 1;
51 }
52 return i - 1;
53 };
54
55 Rewriter.prototype.removeLeadingNewlines = function() {
56 var i, tag, _i, _len, _ref;
57 _ref = this.tokens;
58 for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
59 tag = _ref[i][0];
60 if (tag !== 'TERMINATOR') {
61 break;
62 }
63 }
64 if (i) {
65 return this.tokens.splice(0, i);
66 }
67 };
68
69 Rewriter.prototype.removeMidExpressionNewlines = function() {
70 return this.scanTokens(function(token, i, tokens) {
71 var _ref;
72 if (!(token[0] === 'TERMINATOR' && (_ref = this.tag(i + 1), __indexOf.call(EXPRESSION_CLOSE, _ref) >= 0))) {
73 return 1;
74 }
75 tokens.splice(i, 1);
76 return 0;
77 });
78 };
79
80 Rewriter.prototype.closeOpenCalls = function() {
81 var action, condition;
82 condition = function(token, i) {
83 var _ref;
84 return ((_ref = token[0]) === ')' || _ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';
85 };
86 action = function(token, i) {
87 return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
88 };
89 return this.scanTokens(function(token, i) {
90 if (token[0] === 'CALL_START') {
91 this.detectEnd(i + 1, condition, action);
92 }
93 return 1;
94 });
95 };
96
97 Rewriter.prototype.closeOpenIndexes = function() {
98 var action, condition;
99 condition = function(token, i) {
100 var _ref;
101 return (_ref = token[0]) === ']' || _ref === 'INDEX_END';
102 };
103 action = function(token, i) {
104 return token[0] = 'INDEX_END';
105 };
106 return this.scanTokens(function(token, i) {
107 if (token[0] === 'INDEX_START') {
108 this.detectEnd(i + 1, condition, action);
109 }
110 return 1;
111 });
112 };
113
114 Rewriter.prototype.addImplicitBraces = function() {
115 var action, condition, sameLine, stack, start, startIndent, startIndex, startsLine;
116 stack = [];
117 start = null;
118 startsLine = null;
119 sameLine = true;
120 startIndent = 0;
121 startIndex = 0;
122 condition = function(token, i) {
123 var one, tag, three, two, _ref, _ref1;
124 _ref = this.tokens.slice(i + 1, +(i + 3) + 1 || 9e9), one = _ref[0], two = _ref[1], three = _ref[2];
125 if ('HERECOMMENT' === (one != null ? one[0] : void 0)) {
126 return false;
127 }
128 tag = token[0];
129 if (__indexOf.call(LINEBREAKS, tag) >= 0) {
130 sameLine = false;
131 }
132 return (((tag === 'TERMINATOR' || tag === 'OUTDENT') || (__indexOf.call(IMPLICIT_END, tag) >= 0 && sameLine && !(i - startIndex === 1))) && ((!startsLine && this.tag(i - 1) !== ',') || !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':'))) || (tag === ',' && one && ((_ref1 = one[0]) !== 'IDENTIFIER' && _ref1 !== 'NUMBER' && _ref1 !== 'STRING' && _ref1 !== '@' && _ref1 !== 'TERMINATOR' && _ref1 !== 'OUTDENT'));
133 };
134 action = function(token, i) {
135 var tok;
136 tok = this.generate('}', '}', token[2]);
137 return this.tokens.splice(i, 0, tok);
138 };
139 return this.scanTokens(function(token, i, tokens) {
140 var ago, idx, prevTag, tag, tok, value, _ref, _ref1;
141 if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
142 stack.push([(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag), i]);
143 return 1;
144 }
145 if (__indexOf.call(EXPRESSION_END, tag) >= 0) {
146 start = stack.pop();
147 return 1;
148 }
149 if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref1 = stack[stack.length - 1]) != null ? _ref1[0] : void 0) !== '{'))) {
150 return 1;
151 }
152 sameLine = true;
153 startIndex = i + 1;
154 stack.push(['{']);
155 idx = ago === '@' ? i - 2 : i - 1;
156 while (this.tag(idx - 2) === 'HERECOMMENT') {
157 idx -= 2;
158 }
159 prevTag = this.tag(idx - 1);
160 startsLine = !prevTag || (__indexOf.call(LINEBREAKS, prevTag) >= 0);
161 value = new String('{');
162 value.generated = true;
163 tok = this.generate('{', value, token[2]);
164 tokens.splice(idx, 0, tok);
165 this.detectEnd(i + 2, condition, action);
166 return 2;
167 });
168 };
169
170 Rewriter.prototype.addImplicitParentheses = function() {
171 var action, condition, noCall, seenControl, seenSingle;
172 noCall = seenSingle = seenControl = false;
173 condition = function(token, i) {
174 var post, tag, _ref, _ref1;
175 tag = token[0];
176 if (!seenSingle && token.fromThen) {
177 return true;
178 }
179 if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>' || tag === 'CLASS') {
180 seenSingle = true;
181 }
182 if (tag === 'IF' || tag === 'ELSE' || tag === 'SWITCH' || tag === 'TRY' || tag === '=') {
183 seenControl = true;
184 }
185 if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') {
186 return true;
187 }
188 return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (((_ref = this.tag(i - 2)) !== 'CLASS' && _ref !== 'EXTENDS') && (_ref1 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref1) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{')));
189 };
190 action = function(token, i) {
191 return this.tokens.splice(i, 0, this.generate('CALL_END', ')', token[2]));
192 };
193 return this.scanTokens(function(token, i, tokens) {
194 var callObject, current, next, prev, tag, _ref, _ref1, _ref2;
195 tag = token[0];
196 if (tag === 'CLASS' || tag === 'IF' || tag === 'FOR' || tag === 'WHILE') {
197 noCall = true;
198 }
199 _ref = tokens.slice(i - 1, +(i + 1) + 1 || 9e9), prev = _ref[0], current = _ref[1], next = _ref[2];
200 callObject = !noCall && tag === 'INDENT' && next && next.generated && next[0] === '{' && prev && (_ref1 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref1) >= 0);
201 seenSingle = false;
202 seenControl = false;
203 if (__indexOf.call(LINEBREAKS, tag) >= 0) {
204 noCall = false;
205 }
206 if (prev && !prev.spaced && tag === '?') {
207 token.call = true;
208 }
209 if (token.fromThen) {
210 return 1;
211 }
212 if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) {
213 return 1;
214 }
215 tokens.splice(i, 0, this.generate('CALL_START', '(', token[2]));
216 this.detectEnd(i + 1, condition, action);
217 if (prev[0] === '?') {
218 prev[0] = 'FUNC_EXIST';
219 }
220 return 2;
221 });
222 };
223
224 Rewriter.prototype.addImplicitIndentation = function() {
225 var action, condition, indent, outdent, starter;
226 starter = indent = outdent = null;
227 condition = function(token, i) {
228 var _ref;
229 return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'ELSE' && (starter !== 'IF' && starter !== 'THEN'));
230 };
231 action = function(token, i) {
232 return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
233 };
234 return this.scanTokens(function(token, i, tokens) {
235 var tag, _ref, _ref1;
236 tag = token[0];
237 if (tag === 'TERMINATOR' && this.tag(i + 1) === 'THEN') {
238 tokens.splice(i, 1);
239 return 0;
240 }
241 if (tag === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
242 tokens.splice.apply(tokens, [i, 0].concat(__slice.call(this.indentation(token))));
243 return 2;
244 }
245 if (tag === 'CATCH' && ((_ref = this.tag(i + 2)) === 'OUTDENT' || _ref === 'TERMINATOR' || _ref === 'FINALLY')) {
246 tokens.splice.apply(tokens, [i + 2, 0].concat(__slice.call(this.indentation(token))));
247 return 4;
248 }
249 if (__indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
250 starter = tag;
251 _ref1 = this.indentation(token, true), indent = _ref1[0], outdent = _ref1[1];
252 if (starter === 'THEN') {
253 indent.fromThen = true;
254 }
255 tokens.splice(i + 1, 0, indent);
256 this.detectEnd(i + 2, condition, action);
257 if (tag === 'THEN') {
258 tokens.splice(i, 1);
259 }
260 return 1;
261 }
262 return 1;
263 });
264 };
265
266 Rewriter.prototype.tagPostfixConditionals = function() {
267 var action, condition, original;
268 original = null;
269 condition = function(token, i) {
270 var _ref;
271 return (_ref = token[0]) === 'TERMINATOR' || _ref === 'INDENT';
272 };
273 action = function(token, i) {
274 if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
275 return original[0] = 'POST_' + original[0];
276 }
277 };
278 return this.scanTokens(function(token, i) {
279 if (token[0] !== 'IF') {
280 return 1;
281 }
282 original = token;
283 this.detectEnd(i + 1, condition, action);
284 return 1;
285 });
286 };
287
288 Rewriter.prototype.indentation = function(token, implicit) {
289 var indent, outdent;
290 if (implicit == null) {
291 implicit = false;
292 }
293 indent = ['INDENT', 2, token[2]];
294 outdent = ['OUTDENT', 2, token[2]];
295 if (implicit) {
296 indent.generated = outdent.generated = true;
297 }
298 return [indent, outdent];
299 };
300
301 Rewriter.prototype.generate = function(tag, value, line) {
302 var tok;
303 tok = [tag, value, line];
304 tok.generated = true;
305 return tok;
306 };
307
308 Rewriter.prototype.tag = function(i) {
309 var _ref;
310 return (_ref = this.tokens[i]) != null ? _ref[0] : void 0;
311 };
312
313 return Rewriter;
314
315 })();
316
317 BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END']];
318
319 exports.INVERSES = INVERSES = {};
320
321 EXPRESSION_START = [];
322
323 EXPRESSION_END = [];
324
325 for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
326 _ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];
327 EXPRESSION_START.push(INVERSES[rite] = left);
328 EXPRESSION_END.push(INVERSES[left] = rite);
329 }
330
331 EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
332
333 IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
334
335 IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'SUPER', '@', '->', '=>', '[', '(', '{', '--', '++'];
336
337 IMPLICIT_UNSPACED_CALL = ['+', '-'];
338
339 IMPLICIT_BLOCK = ['->', '=>', '{', '[', ','];
340
341 IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
342
343 SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
344
345 SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
346
347 LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
348
349}).call(this);