Security update to Drupal 8.4.6
[yaffs-website] / node_modules / coffee-script / lib / coffee-script / rewriter.js
1 // Generated by CoffeeScript 1.10.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, 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; },
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(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();
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, ref, ref1, token, tokens;
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, k, len, ref, tag;
65       ref = this.tokens;
66       for (i = k = 0, len = ref.length; k < len; i = ++k) {
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.indexOfTag = function() {
112       var fuzz, i, j, k, pattern, ref, ref1;
113       i = arguments[0], pattern = 2 <= arguments.length ? slice.call(arguments, 1) : [];
114       fuzz = 0;
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') {
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 -1;
127         }
128       }
129       return i + j + fuzz - 1;
130     };
131
132     Rewriter.prototype.looksObjectish = function(j) {
133       var end, index;
134       if (this.indexOfTag(j, '@', null, ':') > -1 || this.indexOfTag(j, null, ':') > -1) {
135         return true;
136       }
137       index = this.indexOfTag(j, EXPRESSION_START);
138       if (index > -1) {
139         end = null;
140         this.detectEnd(index + 1, (function(token) {
141           var ref;
142           return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;
143         }), (function(token, i) {
144           return end = i;
145         }));
146         if (this.tag(end + 1) === ':') {
147           return true;
148         }
149       }
150       return false;
151     };
152
153     Rewriter.prototype.findTagsBackwards = function(i, tags) {
154       var backStack, ref, ref1, ref2, ref3, ref4, ref5;
155       backStack = [];
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));
159         }
160         if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {
161           backStack.pop();
162         }
163         i -= 1;
164       }
165       return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;
166     };
167
168     Rewriter.prototype.addImplicitBracesAndParens = function() {
169       var stack, start;
170       stack = [];
171       start = null;
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;
174         tag = token[0];
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];
179         };
180         startIdx = i;
181         forward = function(n) {
182           return i - startIdx + n;
183         };
184         inImplicit = function() {
185           var ref, ref1;
186           return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;
187         };
188         inImplicitCall = function() {
189           var ref;
190           return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';
191         };
192         inImplicitObject = function() {
193           var ref;
194           return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';
195         };
196         inImplicitControl = function() {
197           var ref;
198           return inImplicit && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';
199         };
200         startImplicitCall = function(j) {
201           var idx;
202           idx = j != null ? j : i;
203           stack.push([
204             '(', idx, {
205               ours: true
206             }
207           ]);
208           tokens.splice(idx, 0, generate('CALL_START', '('));
209           if (j == null) {
210             return i += 1;
211           }
212         };
213         endImplicitCall = function() {
214           stack.pop();
215           tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]]));
216           return i += 1;
217         };
218         startImplicitObject = function(j, startsLine) {
219           var idx, val;
220           if (startsLine == null) {
221             startsLine = true;
222           }
223           idx = j != null ? j : i;
224           stack.push([
225             '{', idx, {
226               sameLine: true,
227               startsLine: startsLine,
228               ours: true
229             }
230           ]);
231           val = new String('{');
232           val.generated = true;
233           tokens.splice(idx, 0, generate('{', val, token));
234           if (j == null) {
235             return i += 1;
236           }
237         };
238         endImplicitObject = function(j) {
239           j = j != null ? j : i;
240           stack.pop();
241           tokens.splice(j, 0, generate('}', '}', token));
242           return i += 1;
243         };
244         if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {
245           stack.push([
246             'CONTROL', i, {
247               ours: true
248             }
249           ]);
250           return forward(1);
251         }
252         if (tag === 'INDENT' && inImplicit()) {
253           if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {
254             while (inImplicitCall()) {
255               endImplicitCall();
256             }
257           }
258           if (inImplicitControl()) {
259             stack.pop();
260           }
261           stack.push([tag, i]);
262           return forward(1);
263         }
264         if (indexOf.call(EXPRESSION_START, tag) >= 0) {
265           stack.push([tag, i]);
266           return forward(1);
267         }
268         if (indexOf.call(EXPRESSION_END, tag) >= 0) {
269           while (inImplicit()) {
270             if (inImplicitCall()) {
271               endImplicitCall();
272             } else if (inImplicitObject()) {
273               endImplicitObject();
274             } else {
275               stack.pop();
276             }
277           }
278           start = stack.pop();
279         }
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))) {
281           if (tag === '?') {
282             tag = token[0] = 'FUNC_EXIST';
283           }
284           startImplicitCall(i + 1);
285           return forward(2);
286         }
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]);
290           return forward(3);
291         }
292         if (tag === ':') {
293           s = (function() {
294             var ref2;
295             switch (false) {
296               case ref2 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref2) < 0:
297                 return start[1];
298               case this.tag(i - 2) !== '@':
299                 return i - 2;
300               default:
301                 return i - 1;
302             }
303           }).call(this);
304           while (this.tag(s - 2) === 'HERECOMMENT') {
305             s -= 2;
306           }
307           this.insideForDeclaration = nextTag === 'FOR';
308           startsLine = s === 0 || (ref2 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref2) >= 0) || tokens[s - 1].newLine;
309           if (stackTop()) {
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) === '{')) {
312               return forward(1);
313             }
314           }
315           startImplicitObject(s, !!startsLine);
316           return forward(2);
317         }
318         if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) {
319           stackTop()[2].sameLine = false;
320         }
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 !== ',') {
326               endImplicitCall();
327             } else if (inImplicitObject() && !this.insideForDeclaration && sameLine && tag !== 'TERMINATOR' && prevTag !== ':') {
328               endImplicitObject();
329             } else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
330               if (nextTag === 'HERECOMMENT') {
331                 return forward(1);
332               }
333               endImplicitObject();
334             } else {
335               break;
336             }
337           }
338         }
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);
343           }
344         }
345         return forward(1);
346       });
347     };
348
349     Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
350       return this.scanTokens(function(token, i, tokens) {
351         var column, line, nextLocation, prevLocation, ref, ref1;
352         if (token[2]) {
353           return 1;
354         }
355         if (!(token.generated || token.explicit)) {
356           return 1;
357         }
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;
362         } else {
363           line = column = 0;
364         }
365         token[2] = {
366           first_line: line,
367           first_column: column,
368           last_line: line,
369           last_column: column
370         };
371         return 1;
372       });
373     };
374
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;
381       };
382       action = function(token, i) {
383         return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
384       };
385       return this.scanTokens(function(token, i, tokens) {
386         var j, k, ref, ref1, ref2, tag;
387         tag = token[0];
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())));
391             return 1;
392           }
393           if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {
394             tokens.splice(i, 1);
395             return 0;
396           }
397         }
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')) {
401               continue;
402             }
403             tokens.splice.apply(tokens, [i + j, 0].concat(slice.call(this.indentation())));
404             return 2 + j;
405           }
406         }
407         if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
408           starter = tag;
409           ref2 = this.indentation(tokens[i]), indent = ref2[0], outdent = ref2[1];
410           if (starter === 'THEN') {
411             indent.fromThen = true;
412           }
413           tokens.splice(i + 1, 0, indent);
414           this.detectEnd(i + 2, condition, action);
415           if (tag === 'THEN') {
416             tokens.splice(i, 1);
417           }
418           return 1;
419         }
420         return 1;
421       });
422     };
423
424     Rewriter.prototype.tagPostfixConditionals = function() {
425       var action, condition, original;
426       original = null;
427       condition = function(token, i) {
428         var prevTag, tag;
429         tag = token[0];
430         prevTag = this.tokens[i - 1][0];
431         return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);
432       };
433       action = function(token, i) {
434         if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
435           return original[0] = 'POST_' + original[0];
436         }
437       };
438       return this.scanTokens(function(token, i) {
439         if (token[0] !== 'IF') {
440           return 1;
441         }
442         original = token;
443         this.detectEnd(i + 1, condition, action);
444         return 1;
445       });
446     };
447
448     Rewriter.prototype.indentation = function(origin) {
449       var indent, outdent;
450       indent = ['INDENT', 2];
451       outdent = ['OUTDENT', 2];
452       if (origin) {
453         indent.generated = outdent.generated = true;
454         indent.origin = outdent.origin = origin;
455       } else {
456         indent.explicit = outdent.explicit = true;
457       }
458       return [indent, outdent];
459     };
460
461     Rewriter.prototype.generate = generate;
462
463     Rewriter.prototype.tag = function(i) {
464       var ref;
465       return (ref = this.tokens[i]) != null ? ref[0] : void 0;
466     };
467
468     return Rewriter;
469
470   })();
471
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']];
473
474   exports.INVERSES = INVERSES = {};
475
476   EXPRESSION_START = [];
477
478   EXPRESSION_END = [];
479
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);
484   }
485
486   EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
487
488   IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
489
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', '@', '->', '=>', '[', '(', '{', '--', '++'];
491
492   IMPLICIT_UNSPACED_CALL = ['+', '-'];
493
494   IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
495
496   SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
497
498   SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
499
500   LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
501
502   CALL_CLOSERS = ['.', '?.', '::', '?::'];
503
504 }).call(this);