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