lexer.js 34 KB

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