rewriter.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var BALANCED_PAIRS, CALL_CLOSERS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, IMPLICIT_UNSPACED_CALL, INVERSES, LINEBREAKS, SINGLE_CLOSERS, SINGLE_LINERS, generate, k, left, len, ref, rite,
  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. slice = [].slice;
  6. generate = function(tag, value, origin) {
  7. var tok;
  8. tok = [tag, value];
  9. tok.generated = true;
  10. if (origin) {
  11. tok.origin = origin;
  12. }
  13. return tok;
  14. };
  15. exports.Rewriter = (function() {
  16. function Rewriter() {}
  17. Rewriter.prototype.rewrite = function(tokens1) {
  18. this.tokens = tokens1;
  19. this.removeLeadingNewlines();
  20. this.closeOpenCalls();
  21. this.closeOpenIndexes();
  22. this.normalizeLines();
  23. this.tagPostfixConditionals();
  24. this.addImplicitBracesAndParens();
  25. this.addLocationDataToGeneratedTokens();
  26. return this.tokens;
  27. };
  28. Rewriter.prototype.scanTokens = function(block) {
  29. var i, token, tokens;
  30. tokens = this.tokens;
  31. i = 0;
  32. while (token = tokens[i]) {
  33. i += block.call(this, token, i, tokens);
  34. }
  35. return true;
  36. };
  37. Rewriter.prototype.detectEnd = function(i, condition, action) {
  38. var levels, ref, ref1, token, tokens;
  39. tokens = this.tokens;
  40. levels = 0;
  41. while (token = tokens[i]) {
  42. if (levels === 0 && condition.call(this, token, i)) {
  43. return action.call(this, token, i);
  44. }
  45. if (!token || levels < 0) {
  46. return action.call(this, token, i - 1);
  47. }
  48. if (ref = token[0], indexOf.call(EXPRESSION_START, ref) >= 0) {
  49. levels += 1;
  50. } else if (ref1 = token[0], indexOf.call(EXPRESSION_END, ref1) >= 0) {
  51. levels -= 1;
  52. }
  53. i += 1;
  54. }
  55. return i - 1;
  56. };
  57. Rewriter.prototype.removeLeadingNewlines = function() {
  58. var i, k, len, ref, tag;
  59. ref = this.tokens;
  60. for (i = k = 0, len = ref.length; k < len; i = ++k) {
  61. tag = ref[i][0];
  62. if (tag !== 'TERMINATOR') {
  63. break;
  64. }
  65. }
  66. if (i) {
  67. return this.tokens.splice(0, i);
  68. }
  69. };
  70. Rewriter.prototype.closeOpenCalls = function() {
  71. var action, condition;
  72. condition = function(token, i) {
  73. var ref;
  74. return ((ref = token[0]) === ')' || ref === 'CALL_END') || token[0] === 'OUTDENT' && this.tag(i - 1) === ')';
  75. };
  76. action = function(token, i) {
  77. return this.tokens[token[0] === 'OUTDENT' ? i - 1 : i][0] = 'CALL_END';
  78. };
  79. return this.scanTokens(function(token, i) {
  80. if (token[0] === 'CALL_START') {
  81. this.detectEnd(i + 1, condition, action);
  82. }
  83. return 1;
  84. });
  85. };
  86. Rewriter.prototype.closeOpenIndexes = function() {
  87. var action, condition;
  88. condition = function(token, i) {
  89. var ref;
  90. return (ref = token[0]) === ']' || ref === 'INDEX_END';
  91. };
  92. action = function(token, i) {
  93. return token[0] = 'INDEX_END';
  94. };
  95. return this.scanTokens(function(token, i) {
  96. if (token[0] === 'INDEX_START') {
  97. this.detectEnd(i + 1, condition, action);
  98. }
  99. return 1;
  100. });
  101. };
  102. Rewriter.prototype.indexOfTag = function() {
  103. var fuzz, i, j, k, pattern, ref, ref1;
  104. i = arguments[0], pattern = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  105. fuzz = 0;
  106. for (j = k = 0, ref = pattern.length; 0 <= ref ? k < ref : k > ref; j = 0 <= ref ? ++k : --k) {
  107. while (this.tag(i + j + fuzz) === 'HERECOMMENT') {
  108. fuzz += 2;
  109. }
  110. if (pattern[j] == null) {
  111. continue;
  112. }
  113. if (typeof pattern[j] === 'string') {
  114. pattern[j] = [pattern[j]];
  115. }
  116. if (ref1 = this.tag(i + j + fuzz), indexOf.call(pattern[j], ref1) < 0) {
  117. return -1;
  118. }
  119. }
  120. return i + j + fuzz - 1;
  121. };
  122. Rewriter.prototype.looksObjectish = function(j) {
  123. var end, index;
  124. if (this.indexOfTag(j, '@', null, ':') > -1 || this.indexOfTag(j, null, ':') > -1) {
  125. return true;
  126. }
  127. index = this.indexOfTag(j, EXPRESSION_START);
  128. if (index > -1) {
  129. end = null;
  130. this.detectEnd(index + 1, (function(token) {
  131. var ref;
  132. return ref = token[0], indexOf.call(EXPRESSION_END, ref) >= 0;
  133. }), (function(token, i) {
  134. return end = i;
  135. }));
  136. if (this.tag(end + 1) === ':') {
  137. return true;
  138. }
  139. }
  140. return false;
  141. };
  142. Rewriter.prototype.findTagsBackwards = function(i, tags) {
  143. var backStack, ref, ref1, ref2, ref3, ref4, ref5;
  144. backStack = [];
  145. while (i >= 0 && (backStack.length || (ref2 = this.tag(i), indexOf.call(tags, ref2) < 0) && ((ref3 = this.tag(i), indexOf.call(EXPRESSION_START, ref3) < 0) || this.tokens[i].generated) && (ref4 = this.tag(i), indexOf.call(LINEBREAKS, ref4) < 0))) {
  146. if (ref = this.tag(i), indexOf.call(EXPRESSION_END, ref) >= 0) {
  147. backStack.push(this.tag(i));
  148. }
  149. if ((ref1 = this.tag(i), indexOf.call(EXPRESSION_START, ref1) >= 0) && backStack.length) {
  150. backStack.pop();
  151. }
  152. i -= 1;
  153. }
  154. return ref5 = this.tag(i), indexOf.call(tags, ref5) >= 0;
  155. };
  156. Rewriter.prototype.addImplicitBracesAndParens = function() {
  157. var stack, start;
  158. stack = [];
  159. start = null;
  160. return this.scanTokens(function(token, i, tokens) {
  161. var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, ref3, ref4, ref5, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag;
  162. tag = token[0];
  163. prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0];
  164. nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0];
  165. stackTop = function() {
  166. return stack[stack.length - 1];
  167. };
  168. startIdx = i;
  169. forward = function(n) {
  170. return i - startIdx + n;
  171. };
  172. inImplicit = function() {
  173. var ref, ref1;
  174. return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0;
  175. };
  176. inImplicitCall = function() {
  177. var ref;
  178. return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '(';
  179. };
  180. inImplicitObject = function() {
  181. var ref;
  182. return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{';
  183. };
  184. inImplicitControl = function() {
  185. var ref;
  186. return inImplicit && ((ref = stackTop()) != null ? ref[0] : void 0) === 'CONTROL';
  187. };
  188. startImplicitCall = function(j) {
  189. var idx;
  190. idx = j != null ? j : i;
  191. stack.push([
  192. '(', idx, {
  193. ours: true
  194. }
  195. ]);
  196. tokens.splice(idx, 0, generate('CALL_START', '('));
  197. if (j == null) {
  198. return i += 1;
  199. }
  200. };
  201. endImplicitCall = function() {
  202. stack.pop();
  203. tokens.splice(i, 0, generate('CALL_END', ')', ['', 'end of input', token[2]]));
  204. return i += 1;
  205. };
  206. startImplicitObject = function(j, startsLine) {
  207. var idx, val;
  208. if (startsLine == null) {
  209. startsLine = true;
  210. }
  211. idx = j != null ? j : i;
  212. stack.push([
  213. '{', idx, {
  214. sameLine: true,
  215. startsLine: startsLine,
  216. ours: true
  217. }
  218. ]);
  219. val = new String('{');
  220. val.generated = true;
  221. tokens.splice(idx, 0, generate('{', val, token));
  222. if (j == null) {
  223. return i += 1;
  224. }
  225. };
  226. endImplicitObject = function(j) {
  227. j = j != null ? j : i;
  228. stack.pop();
  229. tokens.splice(j, 0, generate('}', '}', token));
  230. return i += 1;
  231. };
  232. if (inImplicitCall() && (tag === 'IF' || tag === 'TRY' || tag === 'FINALLY' || tag === 'CATCH' || tag === 'CLASS' || tag === 'SWITCH')) {
  233. stack.push([
  234. 'CONTROL', i, {
  235. ours: true
  236. }
  237. ]);
  238. return forward(1);
  239. }
  240. if (tag === 'INDENT' && inImplicit()) {
  241. if (prevTag !== '=>' && prevTag !== '->' && prevTag !== '[' && prevTag !== '(' && prevTag !== ',' && prevTag !== '{' && prevTag !== 'TRY' && prevTag !== 'ELSE' && prevTag !== '=') {
  242. while (inImplicitCall()) {
  243. endImplicitCall();
  244. }
  245. }
  246. if (inImplicitControl()) {
  247. stack.pop();
  248. }
  249. stack.push([tag, i]);
  250. return forward(1);
  251. }
  252. if (indexOf.call(EXPRESSION_START, tag) >= 0) {
  253. stack.push([tag, i]);
  254. return forward(1);
  255. }
  256. if (indexOf.call(EXPRESSION_END, tag) >= 0) {
  257. while (inImplicit()) {
  258. if (inImplicitCall()) {
  259. endImplicitCall();
  260. } else if (inImplicitObject()) {
  261. endImplicitObject();
  262. } else {
  263. stack.pop();
  264. }
  265. }
  266. start = stack.pop();
  267. }
  268. if ((indexOf.call(IMPLICIT_FUNC, tag) >= 0 && token.spaced || tag === '?' && i > 0 && !tokens[i - 1].spaced) && (indexOf.call(IMPLICIT_CALL, nextTag) >= 0 || indexOf.call(IMPLICIT_UNSPACED_CALL, nextTag) >= 0 && !((ref = tokens[i + 1]) != null ? ref.spaced : void 0) && !((ref1 = tokens[i + 1]) != null ? ref1.newLine : void 0))) {
  269. if (tag === '?') {
  270. tag = token[0] = 'FUNC_EXIST';
  271. }
  272. startImplicitCall(i + 1);
  273. return forward(2);
  274. }
  275. if (indexOf.call(IMPLICIT_FUNC, tag) >= 0 && this.indexOfTag(i + 1, 'INDENT') > -1 && this.looksObjectish(i + 2) && !this.findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH', 'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL'])) {
  276. startImplicitCall(i + 1);
  277. stack.push(['INDENT', i + 2]);
  278. return forward(3);
  279. }
  280. if (tag === ':') {
  281. s = (function() {
  282. var ref2;
  283. switch (false) {
  284. case ref2 = this.tag(i - 1), indexOf.call(EXPRESSION_END, ref2) < 0:
  285. return start[1];
  286. case this.tag(i - 2) !== '@':
  287. return i - 2;
  288. default:
  289. return i - 1;
  290. }
  291. }).call(this);
  292. while (this.tag(s - 2) === 'HERECOMMENT') {
  293. s -= 2;
  294. }
  295. this.insideForDeclaration = nextTag === 'FOR';
  296. startsLine = s === 0 || (ref2 = this.tag(s - 1), indexOf.call(LINEBREAKS, ref2) >= 0) || tokens[s - 1].newLine;
  297. if (stackTop()) {
  298. ref3 = stackTop(), stackTag = ref3[0], stackIdx = ref3[1];
  299. if ((stackTag === '{' || stackTag === 'INDENT' && this.tag(stackIdx - 1) === '{') && (startsLine || this.tag(s - 1) === ',' || this.tag(s - 1) === '{')) {
  300. return forward(1);
  301. }
  302. }
  303. startImplicitObject(s, !!startsLine);
  304. return forward(2);
  305. }
  306. if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) {
  307. stackTop()[2].sameLine = false;
  308. }
  309. newLine = prevTag === 'OUTDENT' || prevToken.newLine;
  310. if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) {
  311. while (inImplicit()) {
  312. ref4 = stackTop(), stackTag = ref4[0], stackIdx = ref4[1], (ref5 = ref4[2], sameLine = ref5.sameLine, startsLine = ref5.startsLine);
  313. if (inImplicitCall() && prevTag !== ',') {
  314. endImplicitCall();
  315. } else if (inImplicitObject() && !this.insideForDeclaration && sameLine && tag !== 'TERMINATOR' && prevTag !== ':') {
  316. endImplicitObject();
  317. } else if (inImplicitObject() && tag === 'TERMINATOR' && prevTag !== ',' && !(startsLine && this.looksObjectish(i + 1))) {
  318. if (nextTag === 'HERECOMMENT') {
  319. return forward(1);
  320. }
  321. endImplicitObject();
  322. } else {
  323. break;
  324. }
  325. }
  326. }
  327. if (tag === ',' && !this.looksObjectish(i + 1) && inImplicitObject() && !this.insideForDeclaration && (nextTag !== 'TERMINATOR' || !this.looksObjectish(i + 2))) {
  328. offset = nextTag === 'OUTDENT' ? 1 : 0;
  329. while (inImplicitObject()) {
  330. endImplicitObject(i + offset);
  331. }
  332. }
  333. return forward(1);
  334. });
  335. };
  336. Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
  337. return this.scanTokens(function(token, i, tokens) {
  338. var column, line, nextLocation, prevLocation, ref, ref1;
  339. if (token[2]) {
  340. return 1;
  341. }
  342. if (!(token.generated || token.explicit)) {
  343. return 1;
  344. }
  345. if (token[0] === '{' && (nextLocation = (ref = tokens[i + 1]) != null ? ref[2] : void 0)) {
  346. line = nextLocation.first_line, column = nextLocation.first_column;
  347. } else if (prevLocation = (ref1 = tokens[i - 1]) != null ? ref1[2] : void 0) {
  348. line = prevLocation.last_line, column = prevLocation.last_column;
  349. } else {
  350. line = column = 0;
  351. }
  352. token[2] = {
  353. first_line: line,
  354. first_column: column,
  355. last_line: line,
  356. last_column: column
  357. };
  358. return 1;
  359. });
  360. };
  361. Rewriter.prototype.normalizeLines = function() {
  362. var action, condition, indent, outdent, starter;
  363. starter = indent = outdent = null;
  364. condition = function(token, i) {
  365. var ref, ref1, ref2, ref3;
  366. return token[1] !== ';' && (ref = token[0], indexOf.call(SINGLE_CLOSERS, ref) >= 0) && !(token[0] === 'TERMINATOR' && (ref1 = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref1) >= 0)) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((ref2 = token[0]) === 'CATCH' || ref2 === 'FINALLY') && (starter === '->' || starter === '=>')) || (ref3 = token[0], indexOf.call(CALL_CLOSERS, ref3) >= 0) && this.tokens[i - 1].newLine;
  367. };
  368. action = function(token, i) {
  369. return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);
  370. };
  371. return this.scanTokens(function(token, i, tokens) {
  372. var j, k, ref, ref1, ref2, tag;
  373. tag = token[0];
  374. if (tag === 'TERMINATOR') {
  375. if (this.tag(i + 1) === 'ELSE' && this.tag(i - 1) !== 'OUTDENT') {
  376. tokens.splice.apply(tokens, [i, 1].concat(slice.call(this.indentation())));
  377. return 1;
  378. }
  379. if (ref = this.tag(i + 1), indexOf.call(EXPRESSION_CLOSE, ref) >= 0) {
  380. tokens.splice(i, 1);
  381. return 0;
  382. }
  383. }
  384. if (tag === 'CATCH') {
  385. for (j = k = 1; k <= 2; j = ++k) {
  386. if (!((ref1 = this.tag(i + j)) === 'OUTDENT' || ref1 === 'TERMINATOR' || ref1 === 'FINALLY')) {
  387. continue;
  388. }
  389. tokens.splice.apply(tokens, [i + j, 0].concat(slice.call(this.indentation())));
  390. return 2 + j;
  391. }
  392. }
  393. if (indexOf.call(SINGLE_LINERS, tag) >= 0 && this.tag(i + 1) !== 'INDENT' && !(tag === 'ELSE' && this.tag(i + 1) === 'IF')) {
  394. starter = tag;
  395. ref2 = this.indentation(tokens[i]), indent = ref2[0], outdent = ref2[1];
  396. if (starter === 'THEN') {
  397. indent.fromThen = true;
  398. }
  399. tokens.splice(i + 1, 0, indent);
  400. this.detectEnd(i + 2, condition, action);
  401. if (tag === 'THEN') {
  402. tokens.splice(i, 1);
  403. }
  404. return 1;
  405. }
  406. return 1;
  407. });
  408. };
  409. Rewriter.prototype.tagPostfixConditionals = function() {
  410. var action, condition, original;
  411. original = null;
  412. condition = function(token, i) {
  413. var prevTag, tag;
  414. tag = token[0];
  415. prevTag = this.tokens[i - 1][0];
  416. return tag === 'TERMINATOR' || (tag === 'INDENT' && indexOf.call(SINGLE_LINERS, prevTag) < 0);
  417. };
  418. action = function(token, i) {
  419. if (token[0] !== 'INDENT' || (token.generated && !token.fromThen)) {
  420. return original[0] = 'POST_' + original[0];
  421. }
  422. };
  423. return this.scanTokens(function(token, i) {
  424. if (token[0] !== 'IF') {
  425. return 1;
  426. }
  427. original = token;
  428. this.detectEnd(i + 1, condition, action);
  429. return 1;
  430. });
  431. };
  432. Rewriter.prototype.indentation = function(origin) {
  433. var indent, outdent;
  434. indent = ['INDENT', 2];
  435. outdent = ['OUTDENT', 2];
  436. if (origin) {
  437. indent.generated = outdent.generated = true;
  438. indent.origin = outdent.origin = origin;
  439. } else {
  440. indent.explicit = outdent.explicit = true;
  441. }
  442. return [indent, outdent];
  443. };
  444. Rewriter.prototype.generate = generate;
  445. Rewriter.prototype.tag = function(i) {
  446. var ref;
  447. return (ref = this.tokens[i]) != null ? ref[0] : void 0;
  448. };
  449. return Rewriter;
  450. })();
  451. BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['CALL_START', 'CALL_END'], ['PARAM_START', 'PARAM_END'], ['INDEX_START', 'INDEX_END'], ['STRING_START', 'STRING_END'], ['REGEX_START', 'REGEX_END']];
  452. exports.INVERSES = INVERSES = {};
  453. EXPRESSION_START = [];
  454. EXPRESSION_END = [];
  455. for (k = 0, len = BALANCED_PAIRS.length; k < len; k++) {
  456. ref = BALANCED_PAIRS[k], left = ref[0], rite = ref[1];
  457. EXPRESSION_START.push(INVERSES[rite] = left);
  458. EXPRESSION_END.push(INVERSES[left] = rite);
  459. }
  460. EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_END);
  461. IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS'];
  462. IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'STRING_START', 'JS', 'REGEX', 'REGEX_START', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'YIELD', 'UNARY_MATH', 'SUPER', 'THROW', '@', '->', '=>', '[', '(', '{', '--', '++'];
  463. IMPLICIT_UNSPACED_CALL = ['+', '-'];
  464. IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR'];
  465. SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN'];
  466. SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'];
  467. LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'];
  468. CALL_CLOSERS = ['.', '?.', '::', '?::'];
  469. }).call(this);