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