@makano/rew 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. package/bin/rew +9 -0
  2. package/bin/ui +0 -0
  3. package/bin/webkit_app +0 -0
  4. package/lib/coffeescript/browser.js +151 -0
  5. package/lib/coffeescript/cake.js +135 -0
  6. package/lib/coffeescript/coffeescript.js +409 -0
  7. package/lib/coffeescript/command.js +750 -0
  8. package/lib/coffeescript/grammar.js +2496 -0
  9. package/lib/coffeescript/helpers.js +477 -0
  10. package/lib/coffeescript/index.js +217 -0
  11. package/lib/coffeescript/lexer.js +1943 -0
  12. package/lib/coffeescript/nodes.js +9204 -0
  13. package/lib/coffeescript/optparse.js +230 -0
  14. package/lib/coffeescript/parser.js +1344 -0
  15. package/lib/coffeescript/register.js +100 -0
  16. package/lib/coffeescript/repl.js +305 -0
  17. package/lib/coffeescript/rewriter.js +1138 -0
  18. package/lib/coffeescript/scope.js +187 -0
  19. package/lib/coffeescript/sourcemap.js +229 -0
  20. package/lib/rew/cli/cli.js +117 -0
  21. package/lib/rew/cli/log.js +40 -0
  22. package/lib/rew/cli/run.js +20 -0
  23. package/lib/rew/cli/utils.js +122 -0
  24. package/lib/rew/const/default.js +35 -0
  25. package/lib/rew/const/files.js +15 -0
  26. package/lib/rew/css/theme.css +3 -0
  27. package/lib/rew/functions/core.js +85 -0
  28. package/lib/rew/functions/emitter.js +57 -0
  29. package/lib/rew/functions/export.js +9 -0
  30. package/lib/rew/functions/future.js +22 -0
  31. package/lib/rew/functions/id.js +13 -0
  32. package/lib/rew/functions/import.js +57 -0
  33. package/lib/rew/functions/map.js +17 -0
  34. package/lib/rew/functions/match.js +34 -0
  35. package/lib/rew/functions/sleep.js +5 -0
  36. package/lib/rew/functions/types.js +96 -0
  37. package/lib/rew/html/ui.html +223 -0
  38. package/lib/rew/main.js +17 -0
  39. package/lib/rew/models/enum.js +14 -0
  40. package/lib/rew/models/struct.js +41 -0
  41. package/lib/rew/modules/compiler.js +17 -0
  42. package/lib/rew/modules/context.js +50 -0
  43. package/lib/rew/modules/fs.js +19 -0
  44. package/lib/rew/modules/runtime.js +24 -0
  45. package/lib/rew/modules/utils.js +0 -0
  46. package/lib/rew/modules/yaml.js +36 -0
  47. package/lib/rew/pkgs/conf.js +92 -0
  48. package/lib/rew/pkgs/data.js +8 -0
  49. package/lib/rew/pkgs/date.js +98 -0
  50. package/lib/rew/pkgs/modules/data/bintree.js +66 -0
  51. package/lib/rew/pkgs/modules/data/doublylinked.js +100 -0
  52. package/lib/rew/pkgs/modules/data/linkedList.js +88 -0
  53. package/lib/rew/pkgs/modules/data/queue.js +28 -0
  54. package/lib/rew/pkgs/modules/data/stack.js +27 -0
  55. package/lib/rew/pkgs/modules/ui/classes.js +171 -0
  56. package/lib/rew/pkgs/pkgs.js +13 -0
  57. package/lib/rew/pkgs/ui.js +108 -0
  58. package/package.json +41 -0
@@ -0,0 +1,1138 @@
1
+ // Generated by CoffeeScript 2.7.0
2
+ (function() {
3
+ // The CoffeeScript language has a good deal of optional syntax, implicit syntax,
4
+ // and shorthand syntax. This can greatly complicate a grammar and bloat
5
+ // the resulting parse table. Instead of making the parser handle it all, we take
6
+ // a series of passes over the token stream, using this **Rewriter** to convert
7
+ // shorthand into the unambiguous long form, add implicit indentation and
8
+ // parentheses, and generally clean things up.
9
+ var BALANCED_PAIRS, CALL_CLOSERS, CONTROL_IN_IMPLICIT, DISCARDED, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, UNFINISHED, extractAllCommentTokens, generate, k, left, len, moveComments, right, throwSyntaxError,
10
+ indexOf = [].indexOf,
11
+ hasProp = {}.hasOwnProperty;
12
+
13
+ ({throwSyntaxError, extractAllCommentTokens} = require('./helpers'));
14
+
15
+ // Move attached comments from one token to another.
16
+ moveComments = function(fromToken, toToken) {
17
+ var comment, k, len, ref, unshiftedComments;
18
+ if (!fromToken.comments) {
19
+ return;
20
+ }
21
+ if (toToken.comments && toToken.comments.length !== 0) {
22
+ unshiftedComments = [];
23
+ ref = fromToken.comments;
24
+ for (k = 0, len = ref.length; k < len; k++) {
25
+ comment = ref[k];
26
+ if (comment.unshift) {
27
+ unshiftedComments.push(comment);
28
+ } else {
29
+ toToken.comments.push(comment);
30
+ }
31
+ }
32
+ toToken.comments = unshiftedComments.concat(toToken.comments);
33
+ } else {
34
+ toToken.comments = fromToken.comments;
35
+ }
36
+ return delete fromToken.comments;
37
+ };
38
+
39
+ // Create a generated token: one that exists due to a use of implicit syntax.
40
+ // Optionally have this new token take the attached comments from another token.
41
+ generate = function(tag, value, origin, commentsToken) {
42
+ var token;
43
+ token = [tag, value];
44
+ token.generated = true;
45
+ if (origin) {
46
+ token.origin = origin;
47
+ }
48
+ if (commentsToken) {
49
+ moveComments(commentsToken, token);
50
+ }
51
+ return token;
52
+ };
53
+
54
+ // The **Rewriter** class is used by the [Lexer](lexer.html), directly against
55
+ // its internal array of tokens.
56
+ exports.Rewriter = Rewriter = (function() {
57
+ class Rewriter {
58
+ // Rewrite the token stream in multiple passes, one logical filter at
59
+ // a time. This could certainly be changed into a single pass through the
60
+ // stream, with a big ol’ efficient switch, but it’s much nicer to work with
61
+ // like this. The order of these passes matters—indentation must be
62
+ // corrected before implicit parentheses can be wrapped around blocks of code.
63
+ rewrite(tokens1) {
64
+ var ref, ref1, t;
65
+ this.tokens = tokens1;
66
+ // Set environment variable `DEBUG_TOKEN_STREAM` to `true` to output token
67
+ // debugging info. Also set `DEBUG_REWRITTEN_TOKEN_STREAM` to `true` to
68
+ // output the token stream after it has been rewritten by this file.
69
+ if (typeof process !== "undefined" && process !== null ? (ref = process.env) != null ? ref.DEBUG_TOKEN_STREAM : void 0 : void 0) {
70
+ if (process.env.DEBUG_REWRITTEN_TOKEN_STREAM) {
71
+ console.log('Initial token stream:');
72
+ }
73
+ console.log(((function() {
74
+ var k, len, ref1, results;
75
+ ref1 = this.tokens;
76
+ results = [];
77
+ for (k = 0, len = ref1.length; k < len; k++) {
78
+ t = ref1[k];
79
+ results.push(t[0] + '/' + t[1] + (t.comments ? '*' : ''));
80
+ }
81
+ return results;
82
+ }).call(this)).join(' '));
83
+ }
84
+ this.removeLeadingNewlines();
85
+ this.closeOpenCalls();
86
+ this.closeOpenIndexes();
87
+ this.normalizeLines();
88
+ this.tagPostfixConditionals();
89
+ this.addImplicitBracesAndParens();
90
+ this.rescueStowawayComments();
91
+ this.addLocationDataToGeneratedTokens();
92
+ this.enforceValidJSXAttributes();
93
+ this.fixIndentationLocationData();
94
+ this.exposeTokenDataToGrammar();
95
+ if (typeof process !== "undefined" && process !== null ? (ref1 = process.env) != null ? ref1.DEBUG_REWRITTEN_TOKEN_STREAM : void 0 : void 0) {
96
+ if (process.env.DEBUG_TOKEN_STREAM) {
97
+ console.log('Rewritten token stream:');
98
+ }
99
+ console.log(((function() {
100
+ var k, len, ref2, results;
101
+ ref2 = this.tokens;
102
+ results = [];
103
+ for (k = 0, len = ref2.length; k < len; k++) {
104
+ t = ref2[k];
105
+ results.push(t[0] + '/' + t[1] + (t.comments ? '*' : ''));
106
+ }
107
+ return results;
108
+ }).call(this)).join(' '));
109
+ }
110
+ return this.tokens;
111
+ }
112
+
113
+ // Rewrite the token stream, looking one token ahead and behind.
114
+ // Allow the return value of the block to tell us how many tokens to move
115
+ // forwards (or backwards) in the stream, to make sure we don’t miss anything
116
+ // as tokens are inserted and removed, and the stream changes length under
117
+ // our feet.
118
+ scanTokens(block) {
119
+ var i, token, tokens;
120
+ ({tokens} = this);
121
+ i = 0;
122
+ while (token = tokens[i]) {
123
+ i += block.call(this, token, i, tokens);
124
+ }
125
+ return true;
126
+ }
127
+
128
+ detectEnd(i, condition, action, opts = {}) {
129
+ var levels, ref, ref1, token, tokens;
130
+ ({tokens} = this);
131
+ levels = 0;
132
+ while (token = tokens[i]) {
133
+ if (levels === 0 && condition.call(this, token, i)) {
134
+ return action.call(this, token, i);
135
+ }
136
+ if (ref = token[0], indexOf.call(EXPRESSION_START, ref) >= 0) {
137
+ levels += 1;
138
+ } else if (ref1 = token[0], indexOf.call(EXPRESSION_END, ref1) >= 0) {
139
+ levels -= 1;
140
+ }
141
+ if (levels < 0) {
142
+ if (opts.returnOnNegativeLevel) {
143
+ return;
144
+ }
145
+ return action.call(this, token, i);
146
+ }
147
+ i += 1;
148
+ }
149
+ return i - 1;
150
+ }
151
+
152
+ // Leading newlines would introduce an ambiguity in the grammar, so we
153
+ // dispatch them here.
154
+ removeLeadingNewlines() {
155
+ var i, k, l, leadingNewlineToken, len, len1, ref, ref1, tag;
156
+ ref = this.tokens;
157
+ for (i = k = 0, len = ref.length; k < len; i = ++k) {
158
+ [tag] = ref[i];
159
+ if (tag !== 'TERMINATOR') {
160
+ // Find the index of the first non-`TERMINATOR` token.
161
+ break;
162
+ }
163
+ }
164
+ if (i === 0) {
165
+ return;
166
+ }
167
+ ref1 = this.tokens.slice(0, i);
168
+ // If there are any comments attached to the tokens we’re about to discard,
169
+ // shift them forward to what will become the new first token.
170
+ for (l = 0, len1 = ref1.length; l < len1; l++) {
171
+ leadingNewlineToken = ref1[l];
172
+ moveComments(leadingNewlineToken, this.tokens[i]);
173
+ }
174
+ // Discard all the leading newline tokens.
175
+ return this.tokens.splice(0, i);
176
+ }
177
+
178
+ // The lexer has tagged the opening parenthesis of a method call. Match it with
179
+ // its paired close.
180
+ closeOpenCalls() {
181
+ var action, condition;
182
+ condition = function(token, i) {
183
+ var ref;
184
+ return (ref = token[0]) === ')' || ref === 'CALL_END';
185
+ };
186
+ action = function(token, i) {
187
+ return token[0] = 'CALL_END';
188
+ };
189
+ return this.scanTokens(function(token, i) {
190
+ if (token[0] === 'CALL_START') {
191
+ this.detectEnd(i + 1, condition, action);
192
+ }
193
+ return 1;
194
+ });
195
+ }
196
+
197
+ // The lexer has tagged the opening bracket of an indexing operation call.
198
+ // Match it with its paired close.
199
+ closeOpenIndexes() {
200
+ var action, condition, startToken;
201
+ startToken = null;
202
+ condition = function(token, i) {
203
+ var ref;
204
+ return (ref = token[0]) === ']' || ref === 'INDEX_END';
205
+ };
206
+ action = function(token, i) {
207
+ if (this.tokens.length >= i && this.tokens[i + 1][0] === ':') {
208
+ startToken[0] = '[';
209
+ return token[0] = ']';
210
+ } else {
211
+ return token[0] = 'INDEX_END';
212
+ }
213
+ };
214
+ return this.scanTokens(function(token, i) {
215
+ if (token[0] === 'INDEX_START') {
216
+ startToken = token;
217
+ this.detectEnd(i + 1, condition, action);
218
+ }
219
+ return 1;
220
+ });
221
+ }
222
+
223
+ // Match tags in token stream starting at `i` with `pattern`.
224
+ // `pattern` may consist of strings (equality), an array of strings (one of)
225
+ // or null (wildcard). Returns the index of the match or -1 if no match.
226
+ indexOfTag(i, ...pattern) {
227
+ var fuzz, j, k, ref, ref1;
228
+ fuzz = 0;
229
+ for (j = k = 0, ref = pattern.length; (0 <= ref ? k < ref : k > ref); j = 0 <= ref ? ++k : --k) {
230
+ if (pattern[j] == null) {
231
+ continue;
232
+ }
233
+ if (typeof pattern[j] === 'string') {
234
+ pattern[j] = [pattern[j]];
235
+ }
236
+ if (ref1 = this.tag(i + j + fuzz), indexOf.call(pattern[j], ref1) < 0) {
237
+ return -1;
238
+ }
239
+ }
240
+ return i + j + fuzz - 1;
241
+ }
242
+
243
+ // Returns `yes` if standing in front of something looking like
244
+ // `@<x>:`, `<x>:` or `<EXPRESSION_START><x>...<EXPRESSION_END>:`.
245
+ looksObjectish(j) {
246
+ var end, index;
247
+ if (this.indexOfTag(j, '@', null, ':') !== -1 || this.indexOfTag(j, null, ':') !== -1) {
248
+ return true;
249
+ }
250
+ index = this.indexOfTag(j, EXPRESSION_START);
251
+ if (index !== -1) {
252
+ end = null;
253
+ this.detectEnd(index + 1, (function(token) {
254
+ var ref;
255
+ return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;
256
+ }), (function(token, i) {
257
+ return end = i;
258
+ }));
259
+ if (this.tag(end + 1) === ':') {
260
+ return true;
261
+ }
262
+ }
263
+ return false;
264
+ }
265
+
266
+ // Returns `yes` if current line of tokens contain an element of tags on same
267
+ // expression level. Stop searching at `LINEBREAKS` or explicit start of
268
+ // containing balanced expression.
269
+ findTagsBackwards(i, tags) {
270
+ var backStack, ref, ref1, ref2, ref3, ref4, ref5;
271
+ backStack = [];
272
+ 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))) {
273
+ if (ref = this.tag(i), indexOf.call(EXPRESSION_END, ref) >= 0) {
274
+ backStack.push(this.tag(i));
275
+ }
276
+ if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {
277
+ backStack.pop();
278
+ }
279
+ i -= 1;
280
+ }
281
+ return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;
282
+ }
283
+
284
+ // Look for signs of implicit calls and objects in the token stream and
285
+ // add them.
286
+ addImplicitBracesAndParens() {
287
+ var stack, start;
288
+ // Track current balancing depth (both implicit and explicit) on stack.
289
+ stack = [];
290
+ start = null;
291
+ return this.scanTokens(function(token, i, tokens) {
292
+ var endImplicitCall, endImplicitObject, forward, implicitObjectContinues, implicitObjectIndent, inControlFlow, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, isImplicit, isImplicitCall, isImplicitObject, k, newLine, nextTag, nextToken, offset, preContinuationLineIndent, preObjectToken, prevTag, prevToken, ref, ref1, ref2, ref3, ref4, ref5, s, sameLine, stackIdx, stackItem, stackNext, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startIndex, startTag, startsLine, tag;
293
+ [tag] = token;
294
+ [prevTag] = prevToken = i > 0 ? tokens[i - 1] : [];
295
+ [nextTag] = nextToken = i < tokens.length - 1 ? tokens[i + 1] : [];
296
+ stackTop = function() {
297
+ return stack[stack.length - 1];
298
+ };
299
+ startIdx = i;
300
+ // Helper function, used for keeping track of the number of tokens consumed
301
+ // and spliced, when returning for getting a new token.
302
+ forward = function(n) {
303
+ return i - startIdx + n;
304
+ };
305
+ // Helper functions
306
+ isImplicit = function(stackItem) {
307
+ var ref;
308
+ return stackItem != null ? (ref = stackItem[2]) != null ? ref.ours : void 0 : void 0;
309
+ };
310
+ isImplicitObject = function(stackItem) {
311
+ return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '{';
312
+ };
313
+ isImplicitCall = function(stackItem) {
314
+ return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '(';
315
+ };
316
+ inImplicit = function() {
317
+ return isImplicit(stackTop());
318
+ };
319
+ inImplicitCall = function() {
320
+ return isImplicitCall(stackTop());
321
+ };
322
+ inImplicitObject = function() {
323
+ return isImplicitObject(stackTop());
324
+ };
325
+ // Unclosed control statement inside implicit parens (like
326
+ // class declaration or if-conditionals).
327
+ inImplicitControl = function() {
328
+ var ref;
329
+ return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';
330
+ };
331
+ startImplicitCall = function(idx) {
332
+ stack.push([
333
+ '(',
334
+ idx,
335
+ {
336
+ ours: true
337
+ }
338
+ ]);
339
+ return tokens.splice(idx, 0, generate('CALL_START', '(', ['', 'implicit function call', token[2]], prevToken));
340
+ };
341
+ endImplicitCall = function() {
342
+ stack.pop();
343
+ tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]], prevToken));
344
+ return i += 1;
345
+ };
346
+ startImplicitObject = function(idx, {startsLine = true, continuationLineIndent} = {}) {
347
+ var val;
348
+ stack.push([
349
+ '{',
350
+ idx,
351
+ {
352
+ sameLine: true,
353
+ startsLine: startsLine,
354
+ ours: true,
355
+ continuationLineIndent: continuationLineIndent
356
+ }
357
+ ]);
358
+ val = new String('{');
359
+ val.generated = true;
360
+ return tokens.splice(idx, 0, generate('{', val, token, prevToken));
361
+ };
362
+ endImplicitObject = function(j) {
363
+ j = j != null ? j : i;
364
+ stack.pop();
365
+ tokens.splice(j, 0, generate('}', '}', token, prevToken));
366
+ return i += 1;
367
+ };
368
+ implicitObjectContinues = (j) => {
369
+ var nextTerminatorIdx;
370
+ nextTerminatorIdx = null;
371
+ this.detectEnd(j, function(token) {
372
+ return token[0] === 'TERMINATOR';
373
+ }, function(token, i) {
374
+ return nextTerminatorIdx = i;
375
+ }, {
376
+ returnOnNegativeLevel: true
377
+ });
378
+ if (nextTerminatorIdx == null) {
379
+ return false;
380
+ }
381
+ return this.looksObjectish(nextTerminatorIdx + 1);
382
+ };
383
+ // Don’t end an implicit call/object on next indent if any of these are in an argument/value.
384
+ if ((inImplicitCall() || inImplicitObject()) && indexOf.call(CONTROL_IN_IMPLICIT, tag) >= 0 || inImplicitObject() && prevTag === ':' && tag === 'FOR') {
385
+ stack.push([
386
+ 'CONTROL',
387
+ i,
388
+ {
389
+ ours: true
390
+ }
391
+ ]);
392
+ return forward(1);
393
+ }
394
+ if (tag === 'INDENT' && inImplicit()) {
395
+ // An `INDENT` closes an implicit call unless
396
+
397
+ // 1. We have seen a `CONTROL` argument on the line.
398
+ // 2. The last token before the indent is part of the list below.
399
+ if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'ELSE' && prevTag !== '=') {
400
+ while (inImplicitCall() || inImplicitObject() && prevTag !== ':') {
401
+ if (inImplicitCall()) {
402
+ endImplicitCall();
403
+ } else {
404
+ endImplicitObject();
405
+ }
406
+ }
407
+ }
408
+ if (inImplicitControl()) {
409
+ stack.pop();
410
+ }
411
+ stack.push([tag, i]);
412
+ return forward(1);
413
+ }
414
+ // Straightforward start of explicit expression.
415
+ if (indexOf.call(EXPRESSION_START, tag) >= 0) {
416
+ stack.push([tag, i]);
417
+ return forward(1);
418
+ }
419
+ // Close all implicit expressions inside of explicitly closed expressions.
420
+ if (indexOf.call(EXPRESSION_END, tag) >= 0) {
421
+ while (inImplicit()) {
422
+ if (inImplicitCall()) {
423
+ endImplicitCall();
424
+ } else if (inImplicitObject()) {
425
+ endImplicitObject();
426
+ } else {
427
+ stack.pop();
428
+ }
429
+ }
430
+ start = stack.pop();
431
+ }
432
+ inControlFlow = () => {
433
+ var controlFlow, isFunc, seenFor, tagCurrentLine;
434
+ seenFor = this.findTagsBackwards(i, ['FOR']) && this.findTagsBackwards(i, ['FORIN', 'FOROF', 'FORFROM']);
435
+ controlFlow = seenFor || this.findTagsBackwards(i, ['WHILE', 'UNTIL', 'LOOP', 'LEADING_WHEN']);
436
+ if (!controlFlow) {
437
+ return false;
438
+ }
439
+ isFunc = false;
440
+ tagCurrentLine = token[2].first_line;
441
+ this.detectEnd(i, function(token, i) {
442
+ var ref;
443
+ return ref = token[0], indexOf.call(LINEBREAKS, ref) >= 0;
444
+ }, function(token, i) {
445
+ var first_line;
446
+ [prevTag, , {first_line}] = tokens[i - 1] || [];
447
+ return isFunc = tagCurrentLine === first_line && (prevTag === '->' || prevTag === '=>');
448
+ }, {
449
+ returnOnNegativeLevel: true
450
+ });
451
+ return isFunc;
452
+ };
453
+ // Recognize standard implicit calls like
454
+ // f a, f() b, f? c, h[0] d etc.
455
+ // Added support for spread dots on the left side: f ...a
456
+ if ((indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced || tag === '?' && i > 0 && !tokens[i - 1].spaced) && (indexOf.call(IMPLICIT_CALL, nextTag) >= 0 || (nextTag === '...' && (ref = this.tag(i + 2), indexOf.call(IMPLICIT_CALL, ref) >= 0) && !this.findTagsBackwards(i, ['INDEX_START', '['])) || indexOf.call(IMPLICIT_UNSPACED_CALL, nextTag) >= 0 && !nextToken.spaced && !nextToken.newLine) && !inControlFlow()) {
457
+ if (tag === '?') {
458
+ tag = token[0] = 'FUNC_EXIST';
459
+ }
460
+ startImplicitCall(i + 1);
461
+ return forward(2);
462
+ }
463
+ // Implicit call taking an implicit indented object as first argument.
464
+
465
+ // f
466
+ // a: b
467
+ // c: d
468
+
469
+ // Don’t accept implicit calls of this type, when on the same line
470
+ // as the control structures below as that may misinterpret constructs like:
471
+
472
+ // if f
473
+ // a: 1
474
+ // as
475
+
476
+ // if f(a: 1)
477
+
478
+ // which is probably always unintended.
479
+ // Furthermore don’t allow this in the first line of a literal array
480
+ // or explicit object, as that creates grammatical ambiguities (#5368).
481
+ 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']) && !(((ref1 = (s = (ref2 = stackTop()) != null ? ref2[0] : void 0)) === '{' || ref1 === '[') && !isImplicit(stackTop()) && this.findTagsBackwards(i, s))) {
482
+ startImplicitCall(i + 1);
483
+ stack.push(['INDENT', i + 2]);
484
+ return forward(3);
485
+ }
486
+ // Implicit objects start here.
487
+ if (tag === ':') {
488
+ // Go back to the (implicit) start of the object.
489
+ s = (function() {
490
+ var ref3;
491
+ switch (false) {
492
+ case ref3 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref3) < 0:
493
+ [startTag, startIndex] = start;
494
+ if (startTag === '[' && startIndex > 0 && this.tag(startIndex - 1) === '@' && !tokens[startIndex - 1].spaced) {
495
+ return startIndex - 1;
496
+ } else {
497
+ return startIndex;
498
+ }
499
+ break;
500
+ case this.tag(i - 2) !== '@':
501
+ return i - 2;
502
+ default:
503
+ return i - 1;
504
+ }
505
+ }).call(this);
506
+ startsLine = s <= 0 || (ref3 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref3) >= 0) || tokens[s - 1].newLine;
507
+ // Are we just continuing an already declared object?
508
+ // Including the case where we indent on the line after an explicit '{'.
509
+ if (stackTop()) {
510
+ [stackTag, stackIdx] = stackTop();
511
+ stackNext = stack[stack.length - 2];
512
+ if ((stackTag === '{' || stackTag === 'INDENT' && (stackNext != null ? stackNext[0] : void 0) === '{' && !isImplicit(stackNext) && this.findTagsBackwards(stackIdx - 1, ['{'])) && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{') && (ref4 = this.tag(s - 1), indexOf.call(UNFINISHED, ref4) < 0)) {
513
+ return forward(1);
514
+ }
515
+ }
516
+ preObjectToken = i > 1 ? tokens[i - 2] : [];
517
+ startImplicitObject(s, {
518
+ startsLine: !!startsLine,
519
+ continuationLineIndent: preObjectToken.continuationLineIndent
520
+ });
521
+ return forward(2);
522
+ }
523
+ // End implicit calls when chaining method calls
524
+ // like e.g.:
525
+
526
+ // f ->
527
+ // a
528
+ // .g b, ->
529
+ // c
530
+ // .h a
531
+
532
+ // and also
533
+
534
+ // f a
535
+ // .g b
536
+ // .h a
537
+
538
+ // Mark all enclosing objects as not sameLine
539
+ if (indexOf.call(LINEBREAKS, tag) >= 0) {
540
+ for (k = stack.length - 1; k >= 0; k += -1) {
541
+ stackItem = stack[k];
542
+ if (!isImplicit(stackItem)) {
543
+ break;
544
+ }
545
+ if (isImplicitObject(stackItem)) {
546
+ stackItem[2].sameLine = false;
547
+ }
548
+ }
549
+ }
550
+ // End indented-continuation-line implicit objects once that indentation is over.
551
+ if (tag === 'TERMINATOR' && token.endsContinuationLineIndentation) {
552
+ ({preContinuationLineIndent} = token.endsContinuationLineIndentation);
553
+ while (inImplicitObject() && ((implicitObjectIndent = stackTop()[2].continuationLineIndent) != null) && implicitObjectIndent > preContinuationLineIndent) {
554
+ endImplicitObject();
555
+ }
556
+ }
557
+ newLine = prevTag === 'OUTDENT' || prevToken.newLine;
558
+ if (indexOf.call(IMPLICIT_END, tag) >= 0 || (indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) || ((tag === '..' || tag === '...') && this.findTagsBackwards(i, ["INDEX_START"]))) {
559
+ while (inImplicit()) {
560
+ [stackTag, stackIdx, {sameLine, startsLine}] = stackTop();
561
+ // Close implicit calls when reached end of argument list
562
+ if (inImplicitCall() && prevTag !== ',' || (prevTag === ',' && tag === 'TERMINATOR' && (nextTag == null))) {
563
+ endImplicitCall();
564
+ // Close implicit objects such as:
565
+ // return a: 1, b: 2 unless true
566
+ } else if (inImplicitObject() && sameLine && tag !== 'TERMINATOR' && prevTag !== ':' && !((tag === 'POST_IF' || tag === 'FOR' || tag === 'WHILE' || tag === 'UNTIL') && startsLine && implicitObjectContinues(i + 1))) {
567
+ endImplicitObject();
568
+ // Close implicit objects when at end of line, line didn't end with a comma
569
+ // and the implicit object didn't start the line or the next line doesn’t look like
570
+ // the continuation of an object.
571
+ } else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
572
+ endImplicitObject();
573
+ } else if (inImplicitControl() && tokens[stackTop()[1]][0] === 'CLASS' && tag === 'TERMINATOR') {
574
+ stack.pop();
575
+ } else {
576
+ break;
577
+ }
578
+ }
579
+ }
580
+ // Close implicit object if comma is the last character
581
+ // and what comes after doesn’t look like it belongs.
582
+ // This is used for trailing commas and calls, like:
583
+
584
+ // x =
585
+ // a: b,
586
+ // c: d,
587
+ // e = 2
588
+
589
+ // and
590
+
591
+ // f a, b: c, d: e, f, g: h: i, j
592
+
593
+ if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && !((ref5 = this.tag(i + 2)) === 'FOROF' || ref5 === 'FORIN') && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {
594
+ // When nextTag is OUTDENT the comma is insignificant and
595
+ // should just be ignored so embed it in the implicit object.
596
+
597
+ // When it isn’t the comma go on to play a role in a call or
598
+ // array further up the stack, so give it a chance.
599
+ offset = nextTag === 'OUTDENT' ? 1 : 0;
600
+ while (inImplicitObject()) {
601
+ endImplicitObject(i + offset);
602
+ }
603
+ }
604
+ return forward(1);
605
+ });
606
+ }
607
+
608
+ // Make sure only strings and wrapped expressions are used in JSX attributes.
609
+ enforceValidJSXAttributes() {
610
+ return this.scanTokens(function(token, i, tokens) {
611
+ var next, ref;
612
+ if (token.jsxColon) {
613
+ next = tokens[i + 1];
614
+ if ((ref = next[0]) !== 'STRING_START' && ref !== 'STRING' && ref !== '(') {
615
+ throwSyntaxError('expected wrapped or quoted JSX attribute', next[2]);
616
+ }
617
+ }
618
+ return 1;
619
+ });
620
+ }
621
+
622
+ // Not all tokens survive processing by the parser. To avoid comments getting
623
+ // lost into the ether, find comments attached to doomed tokens and move them
624
+ // to a token that will make it to the other side.
625
+ rescueStowawayComments() {
626
+ var dontShiftForward, insertPlaceholder, shiftCommentsBackward, shiftCommentsForward;
627
+ insertPlaceholder = function(token, j, tokens, method) {
628
+ if (tokens[j][0] !== 'TERMINATOR') {
629
+ tokens[method](generate('TERMINATOR', '\n', tokens[j]));
630
+ }
631
+ return tokens[method](generate('JS', '', tokens[j], token));
632
+ };
633
+ dontShiftForward = function(i, tokens) {
634
+ var j, ref;
635
+ j = i + 1;
636
+ while (j !== tokens.length && (ref = tokens[j][0], indexOf.call(DISCARDED, ref) >= 0)) {
637
+ if (tokens[j][0] === 'INTERPOLATION_END') {
638
+ return true;
639
+ }
640
+ j++;
641
+ }
642
+ return false;
643
+ };
644
+ shiftCommentsForward = function(token, i, tokens) {
645
+ var comment, j, k, len, ref, ref1, ref2;
646
+ // Find the next surviving token and attach this token’s comments to it,
647
+ // with a flag that we know to output such comments *before* that
648
+ // token’s own compilation. (Otherwise comments are output following
649
+ // the token they’re attached to.)
650
+ j = i;
651
+ while (j !== tokens.length && (ref = tokens[j][0], indexOf.call(DISCARDED, ref) >= 0)) {
652
+ j++;
653
+ }
654
+ if (!(j === tokens.length || (ref1 = tokens[j][0], indexOf.call(DISCARDED, ref1) >= 0))) {
655
+ ref2 = token.comments;
656
+ for (k = 0, len = ref2.length; k < len; k++) {
657
+ comment = ref2[k];
658
+ comment.unshift = true;
659
+ }
660
+ moveComments(token, tokens[j]);
661
+ return 1; // All following tokens are doomed!
662
+ } else {
663
+ j = tokens.length - 1;
664
+ insertPlaceholder(token, j, tokens, 'push');
665
+ // The generated tokens were added to the end, not inline, so we don’t skip.
666
+ return 1;
667
+ }
668
+ };
669
+ shiftCommentsBackward = function(token, i, tokens) {
670
+ var j, ref, ref1;
671
+ // Find the last surviving token and attach this token’s comments to it.
672
+ j = i;
673
+ while (j !== -1 && (ref = tokens[j][0], indexOf.call(DISCARDED, ref) >= 0)) {
674
+ j--;
675
+ }
676
+ if (!(j === -1 || (ref1 = tokens[j][0], indexOf.call(DISCARDED, ref1) >= 0))) {
677
+ moveComments(token, tokens[j]);
678
+ return 1; // All previous tokens are doomed!
679
+ } else {
680
+ insertPlaceholder(token, 0, tokens, 'unshift');
681
+ // We added two tokens, so shift forward to account for the insertion.
682
+ return 3;
683
+ }
684
+ };
685
+ return this.scanTokens(function(token, i, tokens) {
686
+ var dummyToken, j, ref, ref1, ret;
687
+ if (!token.comments) {
688
+ return 1;
689
+ }
690
+ ret = 1;
691
+ if (ref = token[0], indexOf.call(DISCARDED, ref) >= 0) {
692
+ // This token won’t survive passage through the parser, so we need to
693
+ // rescue its attached tokens and redistribute them to nearby tokens.
694
+ // Comments that don’t start a new line can shift backwards to the last
695
+ // safe token, while other tokens should shift forward.
696
+ dummyToken = {
697
+ comments: []
698
+ };
699
+ j = token.comments.length - 1;
700
+ while (j !== -1) {
701
+ if (token.comments[j].newLine === false && token.comments[j].here === false) {
702
+ dummyToken.comments.unshift(token.comments[j]);
703
+ token.comments.splice(j, 1);
704
+ }
705
+ j--;
706
+ }
707
+ if (dummyToken.comments.length !== 0) {
708
+ ret = shiftCommentsBackward(dummyToken, i - 1, tokens);
709
+ }
710
+ if (token.comments.length !== 0) {
711
+ shiftCommentsForward(token, i, tokens);
712
+ }
713
+ } else if (!dontShiftForward(i, tokens)) {
714
+ // If any of this token’s comments start a line—there’s only
715
+ // whitespace between the preceding newline and the start of the
716
+ // comment—and this isn’t one of the special `JS` tokens, then
717
+ // shift this comment forward to precede the next valid token.
718
+ // `Block.compileComments` also has logic to make sure that
719
+ // “starting new line” comments follow or precede the nearest
720
+ // newline relative to the token that the comment is attached to,
721
+ // but that newline might be inside a `}` or `)` or other generated
722
+ // token that we really want this comment to output after. Therefore
723
+ // we need to shift the comments here, avoiding such generated and
724
+ // discarded tokens.
725
+ dummyToken = {
726
+ comments: []
727
+ };
728
+ j = token.comments.length - 1;
729
+ while (j !== -1) {
730
+ if (token.comments[j].newLine && !token.comments[j].unshift && !(token[0] === 'JS' && token.generated)) {
731
+ dummyToken.comments.unshift(token.comments[j]);
732
+ token.comments.splice(j, 1);
733
+ }
734
+ j--;
735
+ }
736
+ if (dummyToken.comments.length !== 0) {
737
+ ret = shiftCommentsForward(dummyToken, i + 1, tokens);
738
+ }
739
+ }
740
+ if (((ref1 = token.comments) != null ? ref1.length : void 0) === 0) {
741
+ delete token.comments;
742
+ }
743
+ return ret;
744
+ });
745
+ }
746
+
747
+ // Add location data to all tokens generated by the rewriter.
748
+ addLocationDataToGeneratedTokens() {
749
+ return this.scanTokens(function(token, i, tokens) {
750
+ var column, line, nextLocation, prevLocation, rangeIndex, ref, ref1;
751
+ if (token[2]) {
752
+ return 1;
753
+ }
754
+ if (!(token.generated || token.explicit)) {
755
+ return 1;
756
+ }
757
+ if (token.fromThen && token[0] === 'INDENT') {
758
+ token[2] = token.origin[2];
759
+ return 1;
760
+ }
761
+ if (token[0] === '{' && (nextLocation = (ref = tokens[i + 1]) != null ? ref[2] : void 0)) {
762
+ ({
763
+ first_line: line,
764
+ first_column: column,
765
+ range: [rangeIndex]
766
+ } = nextLocation);
767
+ } else if (prevLocation = (ref1 = tokens[i - 1]) != null ? ref1[2] : void 0) {
768
+ ({
769
+ last_line: line,
770
+ last_column: column,
771
+ range: [, rangeIndex]
772
+ } = prevLocation);
773
+ column += 1;
774
+ } else {
775
+ line = column = 0;
776
+ rangeIndex = 0;
777
+ }
778
+ token[2] = {
779
+ first_line: line,
780
+ first_column: column,
781
+ last_line: line,
782
+ last_column: column,
783
+ last_line_exclusive: line,
784
+ last_column_exclusive: column,
785
+ range: [rangeIndex, rangeIndex]
786
+ };
787
+ return 1;
788
+ });
789
+ }
790
+
791
+ // `OUTDENT` tokens should always be positioned at the last character of the
792
+ // previous token, so that AST nodes ending in an `OUTDENT` token end up with a
793
+ // location corresponding to the last “real” token under the node.
794
+ fixIndentationLocationData() {
795
+ var findPrecedingComment;
796
+ if (this.allComments == null) {
797
+ this.allComments = extractAllCommentTokens(this.tokens);
798
+ }
799
+ findPrecedingComment = (token, {afterPosition, indentSize, first, indented}) => {
800
+ var comment, k, l, lastMatching, matches, ref, ref1, tokenStart;
801
+ tokenStart = token[2].range[0];
802
+ matches = function(comment) {
803
+ if (comment.outdented) {
804
+ if (!((indentSize != null) && comment.indentSize > indentSize)) {
805
+ return false;
806
+ }
807
+ }
808
+ if (indented && !comment.indented) {
809
+ return false;
810
+ }
811
+ if (!(comment.locationData.range[0] < tokenStart)) {
812
+ return false;
813
+ }
814
+ if (!(comment.locationData.range[0] > afterPosition)) {
815
+ return false;
816
+ }
817
+ return true;
818
+ };
819
+ if (first) {
820
+ lastMatching = null;
821
+ ref = this.allComments;
822
+ for (k = ref.length - 1; k >= 0; k += -1) {
823
+ comment = ref[k];
824
+ if (matches(comment)) {
825
+ lastMatching = comment;
826
+ } else if (lastMatching) {
827
+ return lastMatching;
828
+ }
829
+ }
830
+ return lastMatching;
831
+ }
832
+ ref1 = this.allComments;
833
+ for (l = ref1.length - 1; l >= 0; l += -1) {
834
+ comment = ref1[l];
835
+ if (matches(comment)) {
836
+ return comment;
837
+ }
838
+ }
839
+ return null;
840
+ };
841
+ return this.scanTokens(function(token, i, tokens) {
842
+ var isIndent, nextToken, nextTokenIndex, precedingComment, prevLocationData, prevToken, ref, ref1, ref2, useNextToken;
843
+ if (!(((ref = token[0]) === 'INDENT' || ref === 'OUTDENT') || (token.generated && token[0] === 'CALL_END' && !((ref1 = token.data) != null ? ref1.closingTagNameToken : void 0)) || (token.generated && token[0] === '}'))) {
844
+ return 1;
845
+ }
846
+ isIndent = token[0] === 'INDENT';
847
+ prevToken = (ref2 = token.prevToken) != null ? ref2 : tokens[i - 1];
848
+ prevLocationData = prevToken[2];
849
+ // addLocationDataToGeneratedTokens() set the outdent’s location data
850
+ // to the preceding token’s, but in order to detect comments inside an
851
+ // empty "block" we want to look for comments preceding the next token.
852
+ useNextToken = token.explicit || token.generated;
853
+ if (useNextToken) {
854
+ nextToken = token;
855
+ nextTokenIndex = i;
856
+ while ((nextToken.explicit || nextToken.generated) && nextTokenIndex !== tokens.length - 1) {
857
+ nextToken = tokens[nextTokenIndex++];
858
+ }
859
+ }
860
+ precedingComment = findPrecedingComment(useNextToken ? nextToken : token, {
861
+ afterPosition: prevLocationData.range[0],
862
+ indentSize: token.indentSize,
863
+ first: isIndent,
864
+ indented: useNextToken
865
+ });
866
+ if (isIndent) {
867
+ if (!(precedingComment != null ? precedingComment.newLine : void 0)) {
868
+ return 1;
869
+ }
870
+ }
871
+ if (token.generated && token[0] === 'CALL_END' && (precedingComment != null ? precedingComment.indented : void 0)) {
872
+ // We don’t want e.g. an implicit call at the end of an `if` condition to
873
+ // include a following indented comment.
874
+ return 1;
875
+ }
876
+ if (precedingComment != null) {
877
+ prevLocationData = precedingComment.locationData;
878
+ }
879
+ token[2] = {
880
+ first_line: precedingComment != null ? prevLocationData.first_line : prevLocationData.last_line,
881
+ first_column: precedingComment != null ? isIndent ? 0 : prevLocationData.first_column : prevLocationData.last_column,
882
+ last_line: prevLocationData.last_line,
883
+ last_column: prevLocationData.last_column,
884
+ last_line_exclusive: prevLocationData.last_line_exclusive,
885
+ last_column_exclusive: prevLocationData.last_column_exclusive,
886
+ range: isIndent && (precedingComment != null) ? [prevLocationData.range[0] - precedingComment.indentSize, prevLocationData.range[1]] : prevLocationData.range
887
+ };
888
+ return 1;
889
+ });
890
+ }
891
+
892
+ // Because our grammar is LALR(1), it can’t handle some single-line
893
+ // expressions that lack ending delimiters. The **Rewriter** adds the implicit
894
+ // blocks, so it doesn’t need to. To keep the grammar clean and tidy, trailing
895
+ // newlines within expressions are removed and the indentation tokens of empty
896
+ // blocks are added.
897
+ normalizeLines() {
898
+ var action, closeElseTag, condition, ifThens, indent, leading_if_then, leading_switch_when, outdent, starter;
899
+ starter = indent = outdent = null;
900
+ leading_switch_when = null;
901
+ leading_if_then = null;
902
+ // Count `THEN` tags
903
+ ifThens = [];
904
+ condition = function(token, i) {
905
+ var ref, ref1, ref2, ref3;
906
+ 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' || (leading_if_then || leading_switch_when))) && !(((ref2 = token[0]) === 'CATCH' || ref2 === 'FINALLY') && (starter === '->' || starter === '=>')) || (ref3 = token[0], indexOf.call(CALL_CLOSERS, ref3) >= 0) && (this.tokens[i - 1].newLine || this.tokens[i - 1][0] === 'OUTDENT');
907
+ };
908
+ action = function(token, i) {
909
+ if (token[0] === 'ELSE' && starter === 'THEN') {
910
+ ifThens.pop();
911
+ }
912
+ return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
913
+ };
914
+ closeElseTag = (tokens, i) => {
915
+ var lastThen, outdentElse, tlen;
916
+ tlen = ifThens.length;
917
+ if (!(tlen > 0)) {
918
+ return i;
919
+ }
920
+ lastThen = ifThens.pop();
921
+ [, outdentElse] = this.indentation(tokens[lastThen]);
922
+ // Insert `OUTDENT` to close inner `IF`.
923
+ outdentElse[1] = tlen * 2;
924
+ tokens.splice(i, 0, outdentElse);
925
+ // Insert `OUTDENT` to close outer `IF`.
926
+ outdentElse[1] = 2;
927
+ tokens.splice(i + 1, 0, outdentElse);
928
+ // Remove outdents from the end.
929
+ this.detectEnd(i + 2, function(token, i) {
930
+ var ref;
931
+ return (ref = token[0]) === 'OUTDENT' || ref === 'TERMINATOR';
932
+ }, function(token, i) {
933
+ if (this.tag(i) === 'OUTDENT' && this.tag(i + 1) === 'OUTDENT') {
934
+ return tokens.splice(i, 2);
935
+ }
936
+ });
937
+ return i + 2;
938
+ };
939
+ return this.scanTokens(function(token, i, tokens) {
940
+ var conditionTag, j, k, ref, ref1, ref2, tag;
941
+ [tag] = token;
942
+ conditionTag = (tag === '->' || tag === '=>') && this.findTagsBackwards(i, ['IF', 'WHILE', 'FOR', 'UNTIL', 'SWITCH', 'WHEN', 'LEADING_WHEN', '[', 'INDEX_START']) && !(this.findTagsBackwards(i, ['THEN', '..', '...']));
943
+ if (tag === 'TERMINATOR') {
944
+ if (this.tag(i + 1) === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
945
+ tokens.splice(i, 1, ...this.indentation());
946
+ return 1;
947
+ }
948
+ if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {
949
+ if (token[1] === ';' && this.tag(i + 1) === 'OUTDENT') {
950
+ tokens[i + 1].prevToken = token;
951
+ moveComments(token, tokens[i + 1]);
952
+ }
953
+ tokens.splice(i, 1);
954
+ return 0;
955
+ }
956
+ }
957
+ if (tag === 'CATCH') {
958
+ for (j = k = 1; k <= 2; j = ++k) {
959
+ if (!((ref1 = this.tag(i + j)) === 'OUTDENT' || ref1 === 'TERMINATOR' || ref1 === 'FINALLY')) {
960
+ continue;
961
+ }
962
+ tokens.splice(i + j, 0, ...this.indentation());
963
+ return 2 + j;
964
+ }
965
+ }
966
+ if ((tag === '->' || tag === '=>') && (((ref2 = this.tag(i + 1)) === ',' || ref2 === ']') || this.tag(i + 1) === '.' && token.newLine)) {
967
+ [indent, outdent] = this.indentation(tokens[i]);
968
+ tokens.splice(i + 1, 0, indent, outdent);
969
+ return 1;
970
+ }
971
+ if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF') && !conditionTag) {
972
+ starter = tag;
973
+ [indent, outdent] = this.indentation(tokens[i]);
974
+ if (starter === 'THEN') {
975
+ indent.fromThen = true;
976
+ }
977
+ if (tag === 'THEN') {
978
+ leading_switch_when = this.findTagsBackwards(i, ['LEADING_WHEN']) && this.tag(i + 1) === 'IF';
979
+ leading_if_then = this.findTagsBackwards(i, ['IF']) && this.tag(i + 1) === 'IF';
980
+ }
981
+ if (tag === 'THEN' && this.findTagsBackwards(i, ['IF'])) {
982
+ ifThens.push(i);
983
+ }
984
+ // `ELSE` tag is not closed.
985
+ if (tag === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
986
+ i = closeElseTag(tokens, i);
987
+ }
988
+ tokens.splice(i + 1, 0, indent);
989
+ this.detectEnd(i + 2, condition, action);
990
+ if (tag === 'THEN') {
991
+ tokens.splice(i, 1);
992
+ }
993
+ return 1;
994
+ }
995
+ return 1;
996
+ });
997
+ }
998
+
999
+ // Tag postfix conditionals as such, so that we can parse them with a
1000
+ // different precedence.
1001
+ tagPostfixConditionals() {
1002
+ var action, condition, original;
1003
+ original = null;
1004
+ condition = function(token, i) {
1005
+ var prevTag, tag;
1006
+ [tag] = token;
1007
+ [prevTag] = this.tokens[i - 1];
1008
+ return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);
1009
+ };
1010
+ action = function(token, i) {
1011
+ if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
1012
+ return original[0] = 'POST_' + original[0];
1013
+ }
1014
+ };
1015
+ return this.scanTokens(function(token, i) {
1016
+ if (token[0] !== 'IF') {
1017
+ return 1;
1018
+ }
1019
+ original = token;
1020
+ this.detectEnd(i + 1, condition, action);
1021
+ return 1;
1022
+ });
1023
+ }
1024
+
1025
+ // For tokens with extra data, we want to make that data visible to the grammar
1026
+ // by wrapping the token value as a String() object and setting the data as
1027
+ // properties of that object. The grammar should then be responsible for
1028
+ // cleaning this up for the node constructor: unwrapping the token value to a
1029
+ // primitive string and separately passing any expected token data properties
1030
+ exposeTokenDataToGrammar() {
1031
+ return this.scanTokens(function(token, i) {
1032
+ var key, ref, ref1, val;
1033
+ if (token.generated || (token.data && Object.keys(token.data).length !== 0)) {
1034
+ token[1] = new String(token[1]);
1035
+ ref1 = (ref = token.data) != null ? ref : {};
1036
+ for (key in ref1) {
1037
+ if (!hasProp.call(ref1, key)) continue;
1038
+ val = ref1[key];
1039
+ token[1][key] = val;
1040
+ }
1041
+ if (token.generated) {
1042
+ token[1].generated = true;
1043
+ }
1044
+ }
1045
+ return 1;
1046
+ });
1047
+ }
1048
+
1049
+ // Generate the indentation tokens, based on another token on the same line.
1050
+ indentation(origin) {
1051
+ var indent, outdent;
1052
+ indent = ['INDENT', 2];
1053
+ outdent = ['OUTDENT', 2];
1054
+ if (origin) {
1055
+ indent.generated = outdent.generated = true;
1056
+ indent.origin = outdent.origin = origin;
1057
+ } else {
1058
+ indent.explicit = outdent.explicit = true;
1059
+ }
1060
+ return [indent, outdent];
1061
+ }
1062
+
1063
+ // Look up a tag by token index.
1064
+ tag(i) {
1065
+ var ref;
1066
+ return (ref = this.tokens[i]) != null ? ref[0] : void 0;
1067
+ }
1068
+
1069
+ };
1070
+
1071
+ Rewriter.prototype.generate = generate;
1072
+
1073
+ return Rewriter;
1074
+
1075
+ }).call(this);
1076
+
1077
+ // Constants
1078
+ // ---------
1079
+
1080
+ // List of the token pairs that must be balanced.
1081
+ BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END'], ['STRING_START', 'STRING_END'], ['INTERPOLATION_START', 'INTERPOLATION_END'], ['REGEX_START', 'REGEX_END']];
1082
+
1083
+ // The inverse mappings of `BALANCED_PAIRS` we’re trying to fix up, so we can
1084
+ // look things up from either end.
1085
+ exports.INVERSES = INVERSES = {};
1086
+
1087
+ // The tokens that signal the start/end of a balanced pair.
1088
+ EXPRESSION_START = [];
1089
+
1090
+ EXPRESSION_END = [];
1091
+
1092
+ for (k = 0, len = BALANCED_PAIRS.length; k < len; k++) {
1093
+ [left, right] = BALANCED_PAIRS[k];
1094
+ EXPRESSION_START.push(INVERSES[right] = left);
1095
+ EXPRESSION_END.push(INVERSES[left] = right);
1096
+ }
1097
+
1098
+ // Tokens that indicate the close of a clause of an expression.
1099
+ EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
1100
+
1101
+ // Tokens that, if followed by an `IMPLICIT_CALL`, indicate a function invocation.
1102
+ IMPLICIT_FUNC = ['IDENTIFIER', 'PROPERTY', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
1103
+
1104
+ // If preceded by an `IMPLICIT_FUNC`, indicates a function invocation.
1105
+ IMPLICIT_CALL = ['IDENTIFIER', 'JSX_TAG', 'PROPERTY', 'NUMBER', 'INFINITY', 'NAN', 'STRING', 'STRING_START', 'REGEX', 'REGEX_START', 'JS', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'DYNAMIC_IMPORT', 'IMPORT_META', 'NEW_TARGET', 'UNDEFINED', 'NULL', 'BOOL', 'UNARY', 'DO', 'DO_IIFE', 'YIELD', 'AWAIT', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
1106
+
1107
+ IMPLICIT_UNSPACED_CALL = ['+', '-'];
1108
+
1109
+ // Tokens that always mark the end of an implicit call for single-liners.
1110
+ IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
1111
+
1112
+ // Single-line flavors of block expressions that have unclosed endings.
1113
+ // The grammar can’t disambiguate them, so we insert the implicit indentation.
1114
+ SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
1115
+
1116
+ SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
1117
+
1118
+ // Tokens that end a line.
1119
+ LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
1120
+
1121
+ // Tokens that close open calls when they follow a newline.
1122
+ CALL_CLOSERS = ['.', '?.', '::', '?::'];
1123
+
1124
+ // Tokens that prevent a subsequent indent from ending implicit calls/objects
1125
+ CONTROL_IN_IMPLICIT = ['IF', 'TRY', 'FINALLY', 'CATCH', 'CLASS', 'SWITCH'];
1126
+
1127
+ // Tokens that are swallowed up by the parser, never leading to code generation.
1128
+ // You can spot these in `grammar.coffee` because the `o` function second
1129
+ // argument doesn’t contain a `new` call for these tokens.
1130
+ // `STRING_START` isn’t on this list because its `locationData` matches that of
1131
+ // the node that becomes `StringWithInterpolations`, and therefore
1132
+ // `addDataToNode` attaches `STRING_START`’s tokens to that node.
1133
+ DISCARDED = ['(', ')', '[', ']', '{', '}', ':', '.', '..', '...', ',', '=', '++', '--', '?', 'AS', 'AWAIT', 'CALL_START', 'CALL_END', 'DEFAULT', 'DO', 'DO_IIFE', 'ELSE', 'EXTENDS', 'EXPORT', 'FORIN', 'FOROF', 'FORFROM', 'IMPORT', 'INDENT', 'INDEX_SOAK', 'INTERPOLATION_START', 'INTERPOLATION_END', 'LEADING_WHEN', 'OUTDENT', 'PARAM_END', 'REGEX_START', 'REGEX_END', 'RETURN', 'STRING_END', 'THROW', 'UNARY', 'YIELD'].concat(IMPLICIT_UNSPACED_CALL.concat(IMPLICIT_END.concat(CALL_CLOSERS.concat(CONTROL_IN_IMPLICIT))));
1134
+
1135
+ // Tokens that, when appearing at the end of a line, suppress a following TERMINATOR/INDENT token
1136
+ exports.UNFINISHED = UNFINISHED = ['\\', '.', '?.', '?::', 'UNARY', 'DO', 'DO_IIFE', 'MATH', 'UNARY_MATH', '+', '-', '**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||', 'BIN?', 'EXTENDS'];
1137
+
1138
+ }).call(this);