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