Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
// Generated by CoffeeScript 1.12.6
2
(function() {
3
var BOM, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HERECOMMENT_ILLEGAL, HEREDOC_DOUBLE, HEREDOC_INDENT, HEREDOC_SINGLE, HEREGEX, HEREGEX_OMIT, HERE_JSTOKEN, IDENTIFIER, INDENTABLE_CLOSERS, INDEXABLE, INVERSES, JSTOKEN, JS_KEYWORDS, LEADING_BLANK_LINE, LINE_BREAK, LINE_CONTINUER, Lexer, MATH, MULTI_DENT, NOT_REGEX, NUMBER, OPERATOR, POSSIBLY_DIVISION, REGEX, REGEX_FLAGS, REGEX_ILLEGAL, REGEX_INVALID_ESCAPE, RELATION, RESERVED, Rewriter, SHIFT, SIMPLE_STRING_OMIT, STRICT_PROSCRIBED, STRING_DOUBLE, STRING_INVALID_ESCAPE, STRING_OMIT, STRING_SINGLE, STRING_START, TRAILING_BLANK_LINE, TRAILING_SPACES, UNARY, UNARY_MATH, UNICODE_CODE_POINT_ESCAPE, VALID_FLAGS, WHITESPACE, compact, count, invertLiterate, isForFrom, isUnassignable, key, locationDataToString, ref, ref1, repeat, starts, throwSyntaxError,
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
ref = require('./rewriter'), Rewriter = ref.Rewriter, INVERSES = ref.INVERSES;
8
9
ref1 = require('./helpers'), count = ref1.count, starts = ref1.starts, compact = ref1.compact, repeat = ref1.repeat, invertLiterate = ref1.invertLiterate, locationDataToString = ref1.locationDataToString, throwSyntaxError = ref1.throwSyntaxError;
10
11
exports.Lexer = Lexer = (function() {
12
function Lexer() {}
13
14
Lexer.prototype.tokenize = function(code, opts) {
15
var consumed, end, i, ref2;
16
if (opts == null) {
17
opts = {};
18
}
19
this.literate = opts.literate;
20
this.indent = 0;
21
this.baseIndent = 0;
22
this.indebt = 0;
23
this.outdebt = 0;
24
this.indents = [];
25
this.ends = [];
26
this.tokens = [];
27
this.seenFor = false;
28
this.seenImport = false;
29
this.seenExport = false;
30
this.importSpecifierList = false;
31
this.exportSpecifierList = false;
32
this.chunkLine = opts.line || 0;
33
this.chunkColumn = opts.column || 0;
34
code = this.clean(code);
35
i = 0;
36
while (this.chunk = code.slice(i)) {
37
consumed = this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
38
ref2 = this.getLineAndColumnFromChunk(consumed), this.chunkLine = ref2[0], this.chunkColumn = ref2[1];
39
i += consumed;
40
if (opts.untilBalanced && this.ends.length === 0) {
41
return {
42
tokens: this.tokens,
43
index: i
44
};
45
}
46
}
47
this.closeIndentation();
48
if (end = this.ends.pop()) {
49
this.error("missing " + end.tag, end.origin[2]);
50
}
51
if (opts.rewrite === false) {
52
return this.tokens;
53
}
54
return (new Rewriter).rewrite(this.tokens);
55
};
56
57
Lexer.prototype.clean = function(code) {
58
if (code.charCodeAt(0) === BOM) {
59
code = code.slice(1);
60
}
61
code = code.replace(/\r/g, '').replace(TRAILING_SPACES, '');
62
if (WHITESPACE.test(code)) {
63
code = "\n" + code;
64
this.chunkLine--;
65
}
66
if (this.literate) {
67
code = invertLiterate(code);
68
}
69
return code;
70
};
71
72
Lexer.prototype.identifierToken = function() {
73
var alias, colon, colonOffset, id, idLength, input, match, poppedToken, prev, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, tag, tagToken;
74
if (!(match = IDENTIFIER.exec(this.chunk))) {
75
return 0;
76
}
77
input = match[0], id = match[1], colon = match[2];
78
idLength = id.length;
79
poppedToken = void 0;
80
if (id === 'own' && this.tag() === 'FOR') {
81
this.token('OWN', id);
82
return id.length;
83
}
84
if (id === 'from' && this.tag() === 'YIELD') {
85
this.token('FROM', id);
86
return id.length;
87
}
88
if (id === 'as' && this.seenImport) {
89
if (this.value() === '*') {
90
this.tokens[this.tokens.length - 1][0] = 'IMPORT_ALL';
91
} else if (ref2 = this.value(), indexOf.call(COFFEE_KEYWORDS, ref2) >= 0) {
92
this.tokens[this.tokens.length - 1][0] = 'IDENTIFIER';
93
}
94
if ((ref3 = this.tag()) === 'DEFAULT' || ref3 === 'IMPORT_ALL' || ref3 === 'IDENTIFIER') {
95
this.token('AS', id);
96
return id.length;
97
}
98
}
99
if (id === 'as' && this.seenExport && ((ref4 = this.tag()) === 'IDENTIFIER' || ref4 === 'DEFAULT')) {
100
this.token('AS', id);
101
return id.length;
102
}
103
if (id === 'default' && this.seenExport && ((ref5 = this.tag()) === 'EXPORT' || ref5 === 'AS')) {
104
this.token('DEFAULT', id);
105
return id.length;
106
}
107
ref6 = this.tokens, prev = ref6[ref6.length - 1];
108
tag = colon || (prev != null) && (((ref7 = prev[0]) === '.' || ref7 === '?.' || ref7 === '::' || ref7 === '?::') || !prev.spaced && prev[0] === '@') ? 'PROPERTY' : 'IDENTIFIER';
109
if (tag === 'IDENTIFIER' && (indexOf.call(JS_KEYWORDS, id) >= 0 || indexOf.call(COFFEE_KEYWORDS, id) >= 0) && !(this.exportSpecifierList && indexOf.call(COFFEE_KEYWORDS, id) >= 0)) {
110
tag = id.toUpperCase();
111
if (tag === 'WHEN' && (ref8 = this.tag(), indexOf.call(LINE_BREAK, ref8) >= 0)) {
112
tag = 'LEADING_WHEN';
113
} else if (tag === 'FOR') {
114
this.seenFor = true;
115
} else if (tag === 'UNLESS') {
116
tag = 'IF';
117
} else if (tag === 'IMPORT') {
118
this.seenImport = true;
119
} else if (tag === 'EXPORT') {
120
this.seenExport = true;
121
} else if (indexOf.call(UNARY, tag) >= 0) {
122
tag = 'UNARY';
123
} else if (indexOf.call(RELATION, tag) >= 0) {
124
if (tag !== 'INSTANCEOF' && this.seenFor) {
125
tag = 'FOR' + tag;
126
this.seenFor = false;
127
} else {
128
tag = 'RELATION';
129
if (this.value() === '!') {
130
poppedToken = this.tokens.pop();
131
id = '!' + id;
132
}
133
}
134
}
135
} else if (tag === 'IDENTIFIER' && this.seenFor && id === 'from' && isForFrom(prev)) {
136
tag = 'FORFROM';
137
this.seenFor = false;
138
}
139
if (tag === 'IDENTIFIER' && indexOf.call(RESERVED, id) >= 0) {
140
this.error("reserved word '" + id + "'", {
141
length: id.length
142
});
143
}
144
if (tag !== 'PROPERTY') {
145
if (indexOf.call(COFFEE_ALIASES, id) >= 0) {
146
alias = id;
147
id = COFFEE_ALIAS_MAP[id];
148
}
149
tag = (function() {
150
switch (id) {
151
case '!':
152
return 'UNARY';
153
case '==':
154
case '!=':
155
return 'COMPARE';
156
case 'true':
157
case 'false':
158
return 'BOOL';
159
case 'break':
160
case 'continue':
161
case 'debugger':
162
return 'STATEMENT';
163
case '&&':
164
case '||':
165
return id;
166
default:
167
return tag;
168
}
169
})();
170
}
171
tagToken = this.token(tag, id, 0, idLength);
172
if (alias) {
173
tagToken.origin = [tag, alias, tagToken[2]];
174
}
175
if (poppedToken) {
176
ref9 = [poppedToken[2].first_line, poppedToken[2].first_column], tagToken[2].first_line = ref9[0], tagToken[2].first_column = ref9[1];
177
}
178
if (colon) {
179
colonOffset = input.lastIndexOf(':');
180
this.token(':', ':', colonOffset, colon.length);
181
}
182
return input.length;
183
};
184
185
Lexer.prototype.numberToken = function() {
186
var base, lexedLength, match, number, numberValue, ref2, tag;
187
if (!(match = NUMBER.exec(this.chunk))) {
188
return 0;
189
}
190
number = match[0];
191
lexedLength = number.length;
192
switch (false) {
193
case !/^0[BOX]/.test(number):
194
this.error("radix prefix in '" + number + "' must be lowercase", {
195
offset: 1
196
});
197
break;
198
case !/^(?!0x).*E/.test(number):
199
this.error("exponential notation in '" + number + "' must be indicated with a lowercase 'e'", {
200
offset: number.indexOf('E')
201
});
202
break;
203
case !/^0\d*[89]/.test(number):
204
this.error("decimal literal '" + number + "' must not be prefixed with '0'", {
205
length: lexedLength
206
});
207
break;
208
case !/^0\d+/.test(number):
209
this.error("octal literal '" + number + "' must be prefixed with '0o'", {
210
length: lexedLength
211
});
212
}
213
base = (function() {
214
switch (number.charAt(1)) {
215
case 'b':
216
return 2;
217
case 'o':
218
return 8;
219
case 'x':
220
return 16;
221
default:
222
return null;
223
}
224
})();
225
numberValue = base != null ? parseInt(number.slice(2), base) : parseFloat(number);
226
if ((ref2 = number.charAt(1)) === 'b' || ref2 === 'o') {
227
number = "0x" + (numberValue.toString(16));
228
}
229
tag = numberValue === 2e308 ? 'INFINITY' : 'NUMBER';
230
this.token(tag, number, 0, lexedLength);
231
return lexedLength;
232
};
233
234
Lexer.prototype.stringToken = function() {
235
var $, attempt, delimiter, doc, end, heredoc, i, indent, indentRegex, match, quote, ref2, ref3, regex, token, tokens;
236
quote = (STRING_START.exec(this.chunk) || [])[0];
237
if (!quote) {
238
return 0;
239
}
240
if (this.tokens.length && this.value() === 'from' && (this.seenImport || this.seenExport)) {
241
this.tokens[this.tokens.length - 1][0] = 'FROM';
242
}
243
regex = (function() {
244
switch (quote) {
245
case "'":
246
return STRING_SINGLE;
247
case '"':
248
return STRING_DOUBLE;
249
case "'''":
250
return HEREDOC_SINGLE;
251
case '"""':
252
return HEREDOC_DOUBLE;
253
}
254
})();
255
heredoc = quote.length === 3;
256
ref2 = this.matchWithInterpolations(regex, quote), tokens = ref2.tokens, end = ref2.index;
257
$ = tokens.length - 1;
258
delimiter = quote.charAt(0);
259
if (heredoc) {
260
indent = null;
261
doc = ((function() {
262
var j, len, results;
263
results = [];
264
for (i = j = 0, len = tokens.length; j < len; i = ++j) {
265
token = tokens[i];
266
if (token[0] === 'NEOSTRING') {
267
results.push(token[1]);
268
}
269
}
270
return results;
271
})()).join('#{}');
272
while (match = HEREDOC_INDENT.exec(doc)) {
273
attempt = match[1];
274
if (indent === null || (0 < (ref3 = attempt.length) && ref3 < indent.length)) {
275
indent = attempt;
276
}
277
}
278
if (indent) {
279
indentRegex = RegExp("\\n" + indent, "g");
280
}
281
this.mergeInterpolationTokens(tokens, {
282
delimiter: delimiter
283
}, (function(_this) {
284
return function(value, i) {
285
value = _this.formatString(value, {
286
delimiter: quote
287
});
288
if (indentRegex) {
289
value = value.replace(indentRegex, '\n');
290
}
291
if (i === 0) {
292
value = value.replace(LEADING_BLANK_LINE, '');
293
}
294
if (i === $) {
295
value = value.replace(TRAILING_BLANK_LINE, '');
296
}
297
return value;
298
};
299
})(this));
300
} else {
301
this.mergeInterpolationTokens(tokens, {
302
delimiter: delimiter
303
}, (function(_this) {
304
return function(value, i) {
305
value = _this.formatString(value, {
306
delimiter: quote
307
});
308
value = value.replace(SIMPLE_STRING_OMIT, function(match, offset) {
309
if ((i === 0 && offset === 0) || (i === $ && offset + match.length === value.length)) {
310
return '';
311
} else {
312
return ' ';
313
}
314
});
315
return value;
316
};
317
})(this));
318
}
319
return end;
320
};
321
322
Lexer.prototype.commentToken = function() {
323
var comment, here, match;
324
if (!(match = this.chunk.match(COMMENT))) {
325
return 0;
326
}
327
comment = match[0], here = match[1];
328
if (here) {
329
if (match = HERECOMMENT_ILLEGAL.exec(comment)) {
330
this.error("block comments cannot contain " + match[0], {
331
offset: match.index,
332
length: match[0].length
333
});
334
}
335
if (here.indexOf('\n') >= 0) {
336
here = here.replace(RegExp("\\n" + (repeat(' ', this.indent)), "g"), '\n');
337
}
338
this.token('HERECOMMENT', here, 0, comment.length);
339
}
340
return comment.length;
341
};
342
343
Lexer.prototype.jsToken = function() {
344
var match, script;
345
if (!(this.chunk.charAt(0) === '`' && (match = HERE_JSTOKEN.exec(this.chunk) || JSTOKEN.exec(this.chunk)))) {
346
return 0;
347
}
348
script = match[1].replace(/\\+(`|$)/g, function(string) {
349
return string.slice(-Math.ceil(string.length / 2));
350
});
351
this.token('JS', script, 0, match[0].length);
352
return match[0].length;
353
};
354
355
Lexer.prototype.regexToken = function() {
356
var body, closed, end, flags, index, match, origin, prev, ref2, ref3, ref4, regex, tokens;
357
switch (false) {
358
case !(match = REGEX_ILLEGAL.exec(this.chunk)):
359
this.error("regular expressions cannot begin with " + match[2], {
360
offset: match.index + match[1].length
361
});
362
break;
363
case !(match = this.matchWithInterpolations(HEREGEX, '///')):
364
tokens = match.tokens, index = match.index;
365
break;
366
case !(match = REGEX.exec(this.chunk)):
367
regex = match[0], body = match[1], closed = match[2];
368
this.validateEscapes(body, {
369
isRegex: true,
370
offsetInChunk: 1
371
});
372
body = this.formatRegex(body, {
373
delimiter: '/'
374
});
375
index = regex.length;
376
ref2 = this.tokens, prev = ref2[ref2.length - 1];
377
if (prev) {
378
if (prev.spaced && (ref3 = prev[0], indexOf.call(CALLABLE, ref3) >= 0)) {
379
if (!closed || POSSIBLY_DIVISION.test(regex)) {
380
return 0;
381
}
382
} else if (ref4 = prev[0], indexOf.call(NOT_REGEX, ref4) >= 0) {
383
return 0;
384
}
385
}
386
if (!closed) {
387
this.error('missing / (unclosed regex)');
388
}
389
break;
390
default:
391
return 0;
392
}
393
flags = REGEX_FLAGS.exec(this.chunk.slice(index))[0];
394
end = index + flags.length;
395
origin = this.makeToken('REGEX', null, 0, end);
396
switch (false) {
397
case !!VALID_FLAGS.test(flags):
398
this.error("invalid regular expression flags " + flags, {
399
offset: index,
400
length: flags.length
401
});
402
break;
403
case !(regex || tokens.length === 1):
404
if (body == null) {
405
body = this.formatHeregex(tokens[0][1]);
406
}
407
this.token('REGEX', "" + (this.makeDelimitedLiteral(body, {
408
delimiter: '/'
409
})) + flags, 0, end, origin);
410
break;
411
default:
412
this.token('REGEX_START', '(', 0, 0, origin);
413
this.token('IDENTIFIER', 'RegExp', 0, 0);
414
this.token('CALL_START', '(', 0, 0);
415
this.mergeInterpolationTokens(tokens, {
416
delimiter: '"',
417
double: true
418
}, this.formatHeregex);
419
if (flags) {
420
this.token(',', ',', index - 1, 0);
421
this.token('STRING', '"' + flags + '"', index - 1, flags.length);
422
}
423
this.token(')', ')', end - 1, 0);
424
this.token('REGEX_END', ')', end - 1, 0);
425
}
426
return end;
427
};
428
429
Lexer.prototype.lineToken = function() {
430
var diff, indent, match, noNewlines, size;
431
if (!(match = MULTI_DENT.exec(this.chunk))) {
432
return 0;
433
}
434
indent = match[0];
435
this.seenFor = false;
436
if (!this.importSpecifierList) {
437
this.seenImport = false;
438
}
439
if (!this.exportSpecifierList) {
440
this.seenExport = false;
441
}
442
size = indent.length - 1 - indent.lastIndexOf('\n');
443
noNewlines = this.unfinished();
444
if (size - this.indebt === this.indent) {
445
if (noNewlines) {
446
this.suppressNewlines();
447
} else {
448
this.newlineToken(0);
449
}
450
return indent.length;
451
}
452
if (size > this.indent) {
453
if (noNewlines || this.tag() === 'RETURN') {
454
this.indebt = size - this.indent;
455
this.suppressNewlines();
456
return indent.length;
457
}
458
if (!this.tokens.length) {
459
this.baseIndent = this.indent = size;
460
return indent.length;
461
}
462
diff = size - this.indent + this.outdebt;
463
this.token('INDENT', diff, indent.length - size, size);
464
this.indents.push(diff);
465
this.ends.push({
466
tag: 'OUTDENT'
467
});
468
this.outdebt = this.indebt = 0;
469
this.indent = size;
470
} else if (size < this.baseIndent) {
471
this.error('missing indentation', {
472
offset: indent.length
473
});
474
} else {
475
this.indebt = 0;
476
this.outdentToken(this.indent - size, noNewlines, indent.length);
477
}
478
return indent.length;
479
};
480
481
Lexer.prototype.outdentToken = function(moveOut, noNewlines, outdentLength) {
482
var decreasedIndent, dent, lastIndent, ref2;
483
decreasedIndent = this.indent - moveOut;
484
while (moveOut > 0) {
485
lastIndent = this.indents[this.indents.length - 1];
486
if (!lastIndent) {
487
moveOut = 0;
488
} else if (lastIndent === this.outdebt) {
489
moveOut -= this.outdebt;
490
this.outdebt = 0;
491
} else if (lastIndent < this.outdebt) {
492
this.outdebt -= lastIndent;
493
moveOut -= lastIndent;
494
} else {
495
dent = this.indents.pop() + this.outdebt;
496
if (outdentLength && (ref2 = this.chunk[outdentLength], indexOf.call(INDENTABLE_CLOSERS, ref2) >= 0)) {
497
decreasedIndent -= dent - moveOut;
498
moveOut = dent;
499
}
500
this.outdebt = 0;
501
this.pair('OUTDENT');
502
this.token('OUTDENT', moveOut, 0, outdentLength);
503
moveOut -= dent;
504
}
505
}
506
if (dent) {
507
this.outdebt -= moveOut;
508
}
509
while (this.value() === ';') {
510
this.tokens.pop();
511
}
512
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
513
this.token('TERMINATOR', '\n', outdentLength, 0);
514
}
515
this.indent = decreasedIndent;
516
return this;
517
};
518
519
Lexer.prototype.whitespaceToken = function() {
520
var match, nline, prev, ref2;
521
if (!((match = WHITESPACE.exec(this.chunk)) || (nline = this.chunk.charAt(0) === '\n'))) {
522
return 0;
523
}
524
ref2 = this.tokens, prev = ref2[ref2.length - 1];
525
if (prev) {
526
prev[match ? 'spaced' : 'newLine'] = true;
527
}
528
if (match) {
529
return match[0].length;
530
} else {
531
return 0;
532
}
533
};
534
535
Lexer.prototype.newlineToken = function(offset) {
536
while (this.value() === ';') {
537
this.tokens.pop();
538
}
539
if (this.tag() !== 'TERMINATOR') {
540
this.token('TERMINATOR', '\n', offset, 0);
541
}
542
return this;
543
};
544
545
Lexer.prototype.suppressNewlines = function() {
546
if (this.value() === '\\') {
547
this.tokens.pop();
548
}
549
return this;
550
};
551
552
Lexer.prototype.literalToken = function() {
553
var match, message, origin, prev, ref2, ref3, ref4, ref5, ref6, skipToken, tag, token, value;
554
if (match = OPERATOR.exec(this.chunk)) {
555
value = match[0];
556
if (CODE.test(value)) {
557
this.tagParameters();
558
}
559
} else {
560
value = this.chunk.charAt(0);
561
}
562
tag = value;
563
ref2 = this.tokens, prev = ref2[ref2.length - 1];
564
if (prev && indexOf.call(['='].concat(slice.call(COMPOUND_ASSIGN)), value) >= 0) {
565
skipToken = false;
566
if (value === '=' && ((ref3 = prev[1]) === '||' || ref3 === '&&') && !prev.spaced) {
567
prev[0] = 'COMPOUND_ASSIGN';
568
prev[1] += '=';
569
prev = this.tokens[this.tokens.length - 2];
570
skipToken = true;
571
}
572
if (prev && prev[0] !== 'PROPERTY') {
573
origin = (ref4 = prev.origin) != null ? ref4 : prev;
574
message = isUnassignable(prev[1], origin[1]);
575
if (message) {
576
this.error(message, origin[2]);
577
}
578
}
579
if (skipToken) {
580
return value.length;
581
}
582
}
583
if (value === '{' && this.seenImport) {
584
this.importSpecifierList = true;
585
} else if (this.importSpecifierList && value === '}') {
586
this.importSpecifierList = false;
587
} else if (value === '{' && (prev != null ? prev[0] : void 0) === 'EXPORT') {
588
this.exportSpecifierList = true;
589
} else if (this.exportSpecifierList && value === '}') {
590
this.exportSpecifierList = false;
591
}
592
if (value === ';') {
593
this.seenFor = this.seenImport = this.seenExport = false;
594
tag = 'TERMINATOR';
595
} else if (value === '*' && prev[0] === 'EXPORT') {
596
tag = 'EXPORT_ALL';
597
} else if (indexOf.call(MATH, value) >= 0) {
598
tag = 'MATH';
599
} else if (indexOf.call(COMPARE, value) >= 0) {
600
tag = 'COMPARE';
601
} else if (indexOf.call(COMPOUND_ASSIGN, value) >= 0) {
602
tag = 'COMPOUND_ASSIGN';
603
} else if (indexOf.call(UNARY, value) >= 0) {
604
tag = 'UNARY';
605
} else if (indexOf.call(UNARY_MATH, value) >= 0) {
606
tag = 'UNARY_MATH';
607
} else if (indexOf.call(SHIFT, value) >= 0) {
608
tag = 'SHIFT';
609
} else if (value === '?' && (prev != null ? prev.spaced : void 0)) {
610
tag = 'BIN?';
611
} else if (prev && !prev.spaced) {
612
if (value === '(' && (ref5 = prev[0], indexOf.call(CALLABLE, ref5) >= 0)) {
613
if (prev[0] === '?') {
614
prev[0] = 'FUNC_EXIST';
615
}
616
tag = 'CALL_START';
617
} else if (value === '[' && (ref6 = prev[0], indexOf.call(INDEXABLE, ref6) >= 0)) {
618
tag = 'INDEX_START';
619
switch (prev[0]) {
620
case '?':
621
prev[0] = 'INDEX_SOAK';
622
}
623
}
624
}
625
token = this.makeToken(tag, value);
626
switch (value) {
627
case '(':
628
case '{':
629
case '[':
630
this.ends.push({
631
tag: INVERSES[value],
632
origin: token
633
});
634
break;
635
case ')':
636
case '}':
637
case ']':
638
this.pair(value);
639
}
640
this.tokens.push(token);
641
return value.length;
642
};
643
644
Lexer.prototype.tagParameters = function() {
645
var i, stack, tok, tokens;
646
if (this.tag() !== ')') {
647
return this;
648
}
649
stack = [];
650
tokens = this.tokens;
651
i = tokens.length;
652
tokens[--i][0] = 'PARAM_END';
653
while (tok = tokens[--i]) {
654
switch (tok[0]) {
655
case ')':
656
stack.push(tok);
657
break;
658
case '(':
659
case 'CALL_START':
660
if (stack.length) {
661
stack.pop();
662
} else if (tok[0] === '(') {
663
tok[0] = 'PARAM_START';
664
return this;
665
} else {
666
return this;
667
}
668
}
669
}
670
return this;
671
};
672
673
Lexer.prototype.closeIndentation = function() {
674
return this.outdentToken(this.indent);
675
};
676
677
Lexer.prototype.matchWithInterpolations = function(regex, delimiter) {
678
var close, column, firstToken, index, lastToken, line, nested, offsetInChunk, open, ref2, ref3, ref4, str, strPart, tokens;
679
tokens = [];
680
offsetInChunk = delimiter.length;
681
if (this.chunk.slice(0, offsetInChunk) !== delimiter) {
682
return null;
683
}
684
str = this.chunk.slice(offsetInChunk);
685
while (true) {
686
strPart = regex.exec(str)[0];
687
this.validateEscapes(strPart, {
688
isRegex: delimiter.charAt(0) === '/',
689
offsetInChunk: offsetInChunk
690
});
691
tokens.push(this.makeToken('NEOSTRING', strPart, offsetInChunk));
692
str = str.slice(strPart.length);
693
offsetInChunk += strPart.length;
694
if (str.slice(0, 2) !== '#{') {
695
break;
696
}
697
ref2 = this.getLineAndColumnFromChunk(offsetInChunk + 1), line = ref2[0], column = ref2[1];
698
ref3 = new Lexer().tokenize(str.slice(1), {
699
line: line,
700
column: column,
701
untilBalanced: true
702
}), nested = ref3.tokens, index = ref3.index;
703
index += 1;
704
open = nested[0], close = nested[nested.length - 1];
705
open[0] = open[1] = '(';
706
close[0] = close[1] = ')';
707
close.origin = ['', 'end of interpolation', close[2]];
708
if (((ref4 = nested[1]) != null ? ref4[0] : void 0) === 'TERMINATOR') {
709
nested.splice(1, 1);
710
}
711
tokens.push(['TOKENS', nested]);
712
str = str.slice(index);
713
offsetInChunk += index;
714
}
715
if (str.slice(0, delimiter.length) !== delimiter) {
716
this.error("missing " + delimiter, {
717
length: delimiter.length
718
});
719
}
720
firstToken = tokens[0], lastToken = tokens[tokens.length - 1];
721
firstToken[2].first_column -= delimiter.length;
722
if (lastToken[1].substr(-1) === '\n') {
723
lastToken[2].last_line += 1;
724
lastToken[2].last_column = delimiter.length - 1;
725
} else {
726
lastToken[2].last_column += delimiter.length;
727
}
728
if (lastToken[1].length === 0) {
729
lastToken[2].last_column -= 1;
730
}
731
return {
732
tokens: tokens,
733
index: offsetInChunk + delimiter.length
734
};
735
};
736
737
Lexer.prototype.mergeInterpolationTokens = function(tokens, options, fn) {
738
var converted, firstEmptyStringIndex, firstIndex, i, j, lastToken, len, locationToken, lparen, plusToken, ref2, rparen, tag, token, tokensToPush, value;
739
if (tokens.length > 1) {
740
lparen = this.token('STRING_START', '(', 0, 0);
741
}
742
firstIndex = this.tokens.length;
743
for (i = j = 0, len = tokens.length; j < len; i = ++j) {
744
token = tokens[i];
745
tag = token[0], value = token[1];
746
switch (tag) {
747
case 'TOKENS':
748
if (value.length === 2) {
749
continue;
750
}
751
locationToken = value[0];
752
tokensToPush = value;
753
break;
754
case 'NEOSTRING':
755
converted = fn.call(this, token[1], i);
756
if (converted.length === 0) {
757
if (i === 0) {
758
firstEmptyStringIndex = this.tokens.length;
759
} else {
760
continue;
761
}
762
}
763
if (i === 2 && (firstEmptyStringIndex != null)) {
764
this.tokens.splice(firstEmptyStringIndex, 2);
765
}
766
token[0] = 'STRING';
767
token[1] = this.makeDelimitedLiteral(converted, options);
768
locationToken = token;
769
tokensToPush = [token];
770
}
771
if (this.tokens.length > firstIndex) {
772
plusToken = this.token('+', '+');
773
plusToken[2] = {
774
first_line: locationToken[2].first_line,
775
first_column: locationToken[2].first_column,
776
last_line: locationToken[2].first_line,
777
last_column: locationToken[2].first_column
778
};
779
}
780
(ref2 = this.tokens).push.apply(ref2, tokensToPush);
781
}
782
if (lparen) {
783
lastToken = tokens[tokens.length - 1];
784
lparen.origin = [
785
'STRING', null, {
786
first_line: lparen[2].first_line,
787
first_column: lparen[2].first_column,
788
last_line: lastToken[2].last_line,
789
last_column: lastToken[2].last_column
790
}
791
];
792
rparen = this.token('STRING_END', ')');
793
return rparen[2] = {
794
first_line: lastToken[2].last_line,
795
first_column: lastToken[2].last_column,
796
last_line: lastToken[2].last_line,
797
last_column: lastToken[2].last_column
798
};
799
}
800
};
801
802
Lexer.prototype.pair = function(tag) {
803
var lastIndent, prev, ref2, ref3, wanted;
804
ref2 = this.ends, prev = ref2[ref2.length - 1];
805
if (tag !== (wanted = prev != null ? prev.tag : void 0)) {
806
if ('OUTDENT' !== wanted) {
807
this.error("unmatched " + tag);
808
}
809
ref3 = this.indents, lastIndent = ref3[ref3.length - 1];
810
this.outdentToken(lastIndent, true);
811
return this.pair(tag);
812
}
813
return this.ends.pop();
814
};
815
816
Lexer.prototype.getLineAndColumnFromChunk = function(offset) {
817
var column, lastLine, lineCount, ref2, string;
818
if (offset === 0) {
819
return [this.chunkLine, this.chunkColumn];
820
}
821
if (offset >= this.chunk.length) {
822
string = this.chunk;
823
} else {
824
string = this.chunk.slice(0, +(offset - 1) + 1 || 9e9);
825
}
826
lineCount = count(string, '\n');
827
column = this.chunkColumn;
828
if (lineCount > 0) {
829
ref2 = string.split('\n'), lastLine = ref2[ref2.length - 1];
830
column = lastLine.length;
831
} else {
832
column += string.length;
833
}
834
return [this.chunkLine + lineCount, column];
835
};
836
837
Lexer.prototype.makeToken = function(tag, value, offsetInChunk, length) {
838
var lastCharacter, locationData, ref2, ref3, token;
839
if (offsetInChunk == null) {
840
offsetInChunk = 0;
841
}
842
if (length == null) {
843
length = value.length;
844
}
845
locationData = {};
846
ref2 = this.getLineAndColumnFromChunk(offsetInChunk), locationData.first_line = ref2[0], locationData.first_column = ref2[1];
847
lastCharacter = length > 0 ? length - 1 : 0;
848
ref3 = this.getLineAndColumnFromChunk(offsetInChunk + lastCharacter), locationData.last_line = ref3[0], locationData.last_column = ref3[1];
849
token = [tag, value, locationData];
850
return token;
851
};
852
853
Lexer.prototype.token = function(tag, value, offsetInChunk, length, origin) {
854
var token;
855
token = this.makeToken(tag, value, offsetInChunk, length);
856
if (origin) {
857
token.origin = origin;
858
}
859
this.tokens.push(token);
860
return token;
861
};
862
863
Lexer.prototype.tag = function() {
864
var ref2, token;
865
ref2 = this.tokens, token = ref2[ref2.length - 1];
866
return token != null ? token[0] : void 0;
867
};
868
869
Lexer.prototype.value = function() {
870
var ref2, token;
871
ref2 = this.tokens, token = ref2[ref2.length - 1];
872
return token != null ? token[1] : void 0;
873
};
874
875
Lexer.prototype.unfinished = function() {
876
var ref2;
877
return LINE_CONTINUER.test(this.chunk) || ((ref2 = this.tag()) === '\\' || ref2 === '.' || ref2 === '?.' || ref2 === '?::' || ref2 === 'UNARY' || ref2 === 'MATH' || ref2 === 'UNARY_MATH' || ref2 === '+' || ref2 === '-' || ref2 === '**' || ref2 === 'SHIFT' || ref2 === 'RELATION' || ref2 === 'COMPARE' || ref2 === '&' || ref2 === '^' || ref2 === '|' || ref2 === '&&' || ref2 === '||' || ref2 === 'BIN?' || ref2 === 'THROW' || ref2 === 'EXTENDS' || ref2 === 'DEFAULT');
878
};
879
880
Lexer.prototype.formatString = function(str, options) {
881
return this.replaceUnicodeCodePointEscapes(str.replace(STRING_OMIT, '$1'), options);
882
};
883
884
Lexer.prototype.formatHeregex = function(str) {
885
return this.formatRegex(str.replace(HEREGEX_OMIT, '$1$2'), {
886
delimiter: '///'
887
});
888
};
889
890
Lexer.prototype.formatRegex = function(str, options) {
891
return this.replaceUnicodeCodePointEscapes(str, options);
892
};
893
894
Lexer.prototype.unicodeCodePointToUnicodeEscapes = function(codePoint) {
895
var high, low, toUnicodeEscape;
896
toUnicodeEscape = function(val) {
897
var str;
898
str = val.toString(16);
899
return "\\u" + (repeat('0', 4 - str.length)) + str;
900
};
901
if (codePoint < 0x10000) {
902
return toUnicodeEscape(codePoint);
903
}
904
high = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
905
low = (codePoint - 0x10000) % 0x400 + 0xDC00;
906
return "" + (toUnicodeEscape(high)) + (toUnicodeEscape(low));
907
};
908
909
Lexer.prototype.replaceUnicodeCodePointEscapes = function(str, options) {
910
return str.replace(UNICODE_CODE_POINT_ESCAPE, (function(_this) {
911
return function(match, escapedBackslash, codePointHex, offset) {
912
var codePointDecimal;
913
if (escapedBackslash) {
914
return escapedBackslash;
915
}
916
codePointDecimal = parseInt(codePointHex, 16);
917
if (codePointDecimal > 0x10ffff) {
918
_this.error("unicode code point escapes greater than \\u{10ffff} are not allowed", {
919
offset: offset + options.delimiter.length,
920
length: codePointHex.length + 4
921
});
922
}
923
return _this.unicodeCodePointToUnicodeEscapes(codePointDecimal);
924
};
925
})(this));
926
};
927
928
Lexer.prototype.validateEscapes = function(str, options) {
929
var before, hex, invalidEscape, invalidEscapeRegex, match, message, octal, ref2, unicode, unicodeCodePoint;
930
if (options == null) {
931
options = {};
932
}
933
invalidEscapeRegex = options.isRegex ? REGEX_INVALID_ESCAPE : STRING_INVALID_ESCAPE;
934
match = invalidEscapeRegex.exec(str);
935
if (!match) {
936
return;
937
}
938
match[0], before = match[1], octal = match[2], hex = match[3], unicodeCodePoint = match[4], unicode = match[5];
939
message = octal ? "octal escape sequences are not allowed" : "invalid escape sequence";
940
invalidEscape = "\\" + (octal || hex || unicodeCodePoint || unicode);
941
return this.error(message + " " + invalidEscape, {
942
offset: ((ref2 = options.offsetInChunk) != null ? ref2 : 0) + match.index + before.length,
943
length: invalidEscape.length
944
});
945
};
946
947
Lexer.prototype.makeDelimitedLiteral = function(body, options) {
948
var regex;
949
if (options == null) {
950
options = {};
951
}
952
if (body === '' && options.delimiter === '/') {
953
body = '(?:)';
954
}
955
regex = RegExp("(\\\\\\\\)|(\\\\0(?=[1-7]))|\\\\?(" + options.delimiter + ")|\\\\?(?:(\\n)|(\\r)|(\\u2028)|(\\u2029))|(\\\\.)", "g");
956
body = body.replace(regex, function(match, backslash, nul, delimiter, lf, cr, ls, ps, other) {
957
switch (false) {
958
case !backslash:
959
if (options.double) {
960
return backslash + backslash;
961
} else {
962
return backslash;
963
}
964
case !nul:
965
return '\\x00';
966
case !delimiter:
967
return "\\" + delimiter;
968
case !lf:
969
return '\\n';
970
case !cr:
971
return '\\r';
972
case !ls:
973
return '\\u2028';
974
case !ps:
975
return '\\u2029';
976
case !other:
977
if (options.double) {
978
return "\\" + other;
979
} else {
980
return other;
981
}
982
}
983
});
984
return "" + options.delimiter + body + options.delimiter;
985
};
986
987
Lexer.prototype.error = function(message, options) {
988
var first_column, first_line, location, ref2, ref3, ref4;
989
if (options == null) {
990
options = {};
991
}
992
location = 'first_line' in options ? options : ((ref3 = this.getLineAndColumnFromChunk((ref2 = options.offset) != null ? ref2 : 0), first_line = ref3[0], first_column = ref3[1], ref3), {
993
first_line: first_line,
994
first_column: first_column,
995
last_column: first_column + ((ref4 = options.length) != null ? ref4 : 1) - 1
996
});
997
return throwSyntaxError(message, location);
998
};
999
1000
return Lexer;
1001
1002
})();
1003
1004
isUnassignable = function(name, displayName) {
1005
if (displayName == null) {
1006
displayName = name;
1007
}
1008
switch (false) {
1009
case indexOf.call(slice.call(JS_KEYWORDS).concat(slice.call(COFFEE_KEYWORDS)), name) < 0:
1010
return "keyword '" + displayName + "' can't be assigned";
1011
case indexOf.call(STRICT_PROSCRIBED, name) < 0:
1012
return "'" + displayName + "' can't be assigned";
1013
case indexOf.call(RESERVED, name) < 0:
1014
return "reserved word '" + displayName + "' can't be assigned";
1015
default:
1016
return false;
1017
}
1018
};
1019
1020
exports.isUnassignable = isUnassignable;
1021
1022
isForFrom = function(prev) {
1023
var ref2;
1024
if (prev[0] === 'IDENTIFIER') {
1025
if (prev[1] === 'from') {
1026
prev[1][0] = 'IDENTIFIER';
1027
true;
1028
}
1029
return true;
1030
} else if (prev[0] === 'FOR') {
1031
return false;
1032
} else if ((ref2 = prev[1]) === '{' || ref2 === '[' || ref2 === ',' || ref2 === ':') {
1033
return false;
1034
} else {
1035
return true;
1036
}
1037
};
1038
1039
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'yield', 'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally', 'class', 'extends', 'super', 'import', 'export', 'default'];
1040
1041
COFFEE_KEYWORDS = ['undefined', 'Infinity', 'NaN', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when'];
1042
1043
COFFEE_ALIAS_MAP = {
1044
and: '&&',
1045
or: '||',
1046
is: '==',
1047
isnt: '!=',
1048
not: '!',
1049
yes: 'true',
1050
no: 'false',
1051
on: 'true',
1052
off: 'false'
1053
};
1054
1055
COFFEE_ALIASES = (function() {
1056
var results;
1057
results = [];
1058
for (key in COFFEE_ALIAS_MAP) {
1059
results.push(key);
1060
}
1061
return results;
1062
})();
1063
1064
COFFEE_KEYWORDS = COFFEE_KEYWORDS.concat(COFFEE_ALIASES);
1065
1066
RESERVED = ['case', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'native', 'implements', 'interface', 'package', 'private', 'protected', 'public', 'static'];
1067
1068
STRICT_PROSCRIBED = ['arguments', 'eval'];
1069
1070
exports.JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED).concat(STRICT_PROSCRIBED);
1071
1072
BOM = 65279;
1073
1074
IDENTIFIER = /^(?!\d)((?:(?!\s)[$\w\x7f-\uffff])+)([^\n\S]*:(?!:))?/;
1075
1076
NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
1077
1078
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>*\/%])\2=?|\?(\.|::)|\.{2,3})/;
1079
1080
WHITESPACE = /^[^\n\S]+/;
1081
1082
COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|###$)|^(?:\s*#(?!##[^#]).*)+/;
1083
1084
CODE = /^[-=]>/;
1085
1086
MULTI_DENT = /^(?:\n[^\n\S]*)+/;
1087
1088
JSTOKEN = /^`(?!``)((?:[^`\\]|\\[\s\S])*)`/;
1089
1090
HERE_JSTOKEN = /^```((?:[^`\\]|\\[\s\S]|`(?!``))*)```/;
1091
1092
STRING_START = /^(?:'''|"""|'|")/;
1093
1094
STRING_SINGLE = /^(?:[^\\']|\\[\s\S])*/;
1095
1096
STRING_DOUBLE = /^(?:[^\\"#]|\\[\s\S]|\#(?!\{))*/;
1097
1098
HEREDOC_SINGLE = /^(?:[^\\']|\\[\s\S]|'(?!''))*/;
1099
1100
HEREDOC_DOUBLE = /^(?:[^\\"#]|\\[\s\S]|"(?!"")|\#(?!\{))*/;
1101
1102
STRING_OMIT = /((?:\\\\)+)|\\[^\S\n]*\n\s*/g;
1103
1104
SIMPLE_STRING_OMIT = /\s*\n\s*/g;
1105
1106
HEREDOC_INDENT = /\n+([^\n\S]*)(?=\S)/g;
1107
1108
REGEX = /^\/(?!\/)((?:[^[\/\n\\]|\\[^\n]|\[(?:\\[^\n]|[^\]\n\\])*\])*)(\/)?/;
1109
1110
REGEX_FLAGS = /^\w*/;
1111
1112
VALID_FLAGS = /^(?!.*(.).*\1)[imguy]*$/;
1113
1114
HEREGEX = /^(?:[^\\\/#]|\\[\s\S]|\/(?!\/\/)|\#(?!\{))*/;
1115
1116
HEREGEX_OMIT = /((?:\\\\)+)|\\(\s)|\s+(?:#.*)?/g;
1117
1118
REGEX_ILLEGAL = /^(\/|\/{3}\s*)(\*)/;
1119
1120
POSSIBLY_DIVISION = /^\/=?\s/;
1121
1122
HERECOMMENT_ILLEGAL = /\*\//;
1123
1124
LINE_CONTINUER = /^\s*(?:,|\??\.(?![.\d])|::)/;
1125
1126
STRING_INVALID_ESCAPE = /((?:^|[^\\])(?:\\\\)*)\\(?:(0[0-7]|[1-7])|(x(?![\da-fA-F]{2}).{0,2})|(u\{(?![\da-fA-F]{1,}\})[^}]*\}?)|(u(?!\{|[\da-fA-F]{4}).{0,4}))/;
1127
1128
REGEX_INVALID_ESCAPE = /((?:^|[^\\])(?:\\\\)*)\\(?:(0[0-7])|(x(?![\da-fA-F]{2}).{0,2})|(u\{(?![\da-fA-F]{1,}\})[^}]*\}?)|(u(?!\{|[\da-fA-F]{4}).{0,4}))/;
1129
1130
UNICODE_CODE_POINT_ESCAPE = /(\\\\)|\\u\{([\da-fA-F]+)\}/g;
1131
1132
LEADING_BLANK_LINE = /^[^\n\S]*\n/;
1133
1134
TRAILING_BLANK_LINE = /\n[^\n\S]*$/;
1135
1136
TRAILING_SPACES = /\s+$/;
1137
1138
COMPOUND_ASSIGN = ['-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>=', '&=', '^=', '|=', '**=', '//=', '%%='];
1139
1140
UNARY = ['NEW', 'TYPEOF', 'DELETE', 'DO'];
1141
1142
UNARY_MATH = ['!', '~'];
1143
1144
SHIFT = ['<<', '>>', '>>>'];
1145
1146
COMPARE = ['==', '!=', '<', '>', '<=', '>='];
1147
1148
MATH = ['*', '/', '%', '//', '%%'];
1149
1150
RELATION = ['IN', 'OF', 'INSTANCEOF'];
1151
1152
BOOL = ['TRUE', 'FALSE'];
1153
1154
CALLABLE = ['IDENTIFIER', 'PROPERTY', ')', ']', '?', '@', 'THIS', 'SUPER'];
1155
1156
INDEXABLE = CALLABLE.concat(['NUMBER', 'INFINITY', 'NAN', 'STRING', 'STRING_END', 'REGEX', 'REGEX_END', 'BOOL', 'NULL', 'UNDEFINED', '}', '::']);
1157
1158
NOT_REGEX = INDEXABLE.concat(['++', '--']);
1159
1160
LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'];
1161
1162
INDENTABLE_CLOSERS = [')', '}', ']'];
1163
1164
}).call(this);
1165
1166