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