/lib/coffee-script/nodes.js

http://github.com/jashkenas/coffee-script · JavaScript · 3485 lines · 3036 code · 448 blank · 1 comment · 812 complexity · a514c6d377b241ff5302069b9659a7d1 MD5 · raw file

Large files are truncated click here to view the full file

  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var Access, Arr, Assign, Base, Block, BooleanLiteral, Call, Class, Code, CodeFragment, Comment, Existence, Expansion, Extends, For, IdentifierLiteral, If, In, Index, InfinityLiteral, JS_FORBIDDEN, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, NEGATE, NO, NaNLiteral, NullLiteral, NumberLiteral, Obj, Op, Param, Parens, PassthroughLiteral, PropertyName, Range, RegexLiteral, RegexWithInterpolations, Return, SIMPLENUM, Scope, Slice, Splat, StatementLiteral, StringLiteral, StringWithInterpolations, SuperCall, Switch, TAB, THIS, ThisLiteral, Throw, Try, UTILITIES, UndefinedLiteral, Value, While, YES, YieldReturn, addLocationDataFn, compact, del, ends, extend, flatten, fragmentsToText, isComplexOrAssignable, isLiteralArguments, isLiteralThis, isUnassignable, locationDataToString, merge, multident, ref1, ref2, some, starts, throwSyntaxError, unfoldSoak, utility,
  4. extend1 = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. hasProp = {}.hasOwnProperty,
  6. 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; },
  7. slice = [].slice;
  8. Error.stackTraceLimit = 2e308;
  9. Scope = require('./scope').Scope;
  10. ref1 = require('./lexer'), isUnassignable = ref1.isUnassignable, JS_FORBIDDEN = ref1.JS_FORBIDDEN;
  11. ref2 = require('./helpers'), compact = ref2.compact, flatten = ref2.flatten, extend = ref2.extend, merge = ref2.merge, del = ref2.del, starts = ref2.starts, ends = ref2.ends, some = ref2.some, addLocationDataFn = ref2.addLocationDataFn, locationDataToString = ref2.locationDataToString, throwSyntaxError = ref2.throwSyntaxError;
  12. exports.extend = extend;
  13. exports.addLocationDataFn = addLocationDataFn;
  14. YES = function() {
  15. return true;
  16. };
  17. NO = function() {
  18. return false;
  19. };
  20. THIS = function() {
  21. return this;
  22. };
  23. NEGATE = function() {
  24. this.negated = !this.negated;
  25. return this;
  26. };
  27. exports.CodeFragment = CodeFragment = (function() {
  28. function CodeFragment(parent, code) {
  29. var ref3;
  30. this.code = "" + code;
  31. this.locationData = parent != null ? parent.locationData : void 0;
  32. this.type = (parent != null ? (ref3 = parent.constructor) != null ? ref3.name : void 0 : void 0) || 'unknown';
  33. }
  34. CodeFragment.prototype.toString = function() {
  35. return "" + this.code + (this.locationData ? ": " + locationDataToString(this.locationData) : '');
  36. };
  37. return CodeFragment;
  38. })();
  39. fragmentsToText = function(fragments) {
  40. var fragment;
  41. return ((function() {
  42. var j, len1, results;
  43. results = [];
  44. for (j = 0, len1 = fragments.length; j < len1; j++) {
  45. fragment = fragments[j];
  46. results.push(fragment.code);
  47. }
  48. return results;
  49. })()).join('');
  50. };
  51. exports.Base = Base = (function() {
  52. function Base() {}
  53. Base.prototype.compile = function(o, lvl) {
  54. return fragmentsToText(this.compileToFragments(o, lvl));
  55. };
  56. Base.prototype.compileToFragments = function(o, lvl) {
  57. var node;
  58. o = extend({}, o);
  59. if (lvl) {
  60. o.level = lvl;
  61. }
  62. node = this.unfoldSoak(o) || this;
  63. node.tab = o.indent;
  64. if (o.level === LEVEL_TOP || !node.isStatement(o)) {
  65. return node.compileNode(o);
  66. } else {
  67. return node.compileClosure(o);
  68. }
  69. };
  70. Base.prototype.compileClosure = function(o) {
  71. var args, argumentsNode, func, jumpNode, meth, parts, ref3;
  72. if (jumpNode = this.jumps()) {
  73. jumpNode.error('cannot use a pure statement in an expression');
  74. }
  75. o.sharedScope = true;
  76. func = new Code([], Block.wrap([this]));
  77. args = [];
  78. if ((argumentsNode = this.contains(isLiteralArguments)) || this.contains(isLiteralThis)) {
  79. args = [new ThisLiteral];
  80. if (argumentsNode) {
  81. meth = 'apply';
  82. args.push(new IdentifierLiteral('arguments'));
  83. } else {
  84. meth = 'call';
  85. }
  86. func = new Value(func, [new Access(new PropertyName(meth))]);
  87. }
  88. parts = (new Call(func, args)).compileNode(o);
  89. if (func.isGenerator || ((ref3 = func.base) != null ? ref3.isGenerator : void 0)) {
  90. parts.unshift(this.makeCode("(yield* "));
  91. parts.push(this.makeCode(")"));
  92. }
  93. return parts;
  94. };
  95. Base.prototype.cache = function(o, level, isComplex) {
  96. var complex, ref, sub;
  97. complex = isComplex != null ? isComplex(this) : this.isComplex();
  98. if (complex) {
  99. ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
  100. sub = new Assign(ref, this);
  101. if (level) {
  102. return [sub.compileToFragments(o, level), [this.makeCode(ref.value)]];
  103. } else {
  104. return [sub, ref];
  105. }
  106. } else {
  107. ref = level ? this.compileToFragments(o, level) : this;
  108. return [ref, ref];
  109. }
  110. };
  111. Base.prototype.cacheToCodeFragments = function(cacheValues) {
  112. return [fragmentsToText(cacheValues[0]), fragmentsToText(cacheValues[1])];
  113. };
  114. Base.prototype.makeReturn = function(res) {
  115. var me;
  116. me = this.unwrapAll();
  117. if (res) {
  118. return new Call(new Literal(res + ".push"), [me]);
  119. } else {
  120. return new Return(me);
  121. }
  122. };
  123. Base.prototype.contains = function(pred) {
  124. var node;
  125. node = void 0;
  126. this.traverseChildren(false, function(n) {
  127. if (pred(n)) {
  128. node = n;
  129. return false;
  130. }
  131. });
  132. return node;
  133. };
  134. Base.prototype.lastNonComment = function(list) {
  135. var i;
  136. i = list.length;
  137. while (i--) {
  138. if (!(list[i] instanceof Comment)) {
  139. return list[i];
  140. }
  141. }
  142. return null;
  143. };
  144. Base.prototype.toString = function(idt, name) {
  145. var tree;
  146. if (idt == null) {
  147. idt = '';
  148. }
  149. if (name == null) {
  150. name = this.constructor.name;
  151. }
  152. tree = '\n' + idt + name;
  153. if (this.soak) {
  154. tree += '?';
  155. }
  156. this.eachChild(function(node) {
  157. return tree += node.toString(idt + TAB);
  158. });
  159. return tree;
  160. };
  161. Base.prototype.eachChild = function(func) {
  162. var attr, child, j, k, len1, len2, ref3, ref4;
  163. if (!this.children) {
  164. return this;
  165. }
  166. ref3 = this.children;
  167. for (j = 0, len1 = ref3.length; j < len1; j++) {
  168. attr = ref3[j];
  169. if (this[attr]) {
  170. ref4 = flatten([this[attr]]);
  171. for (k = 0, len2 = ref4.length; k < len2; k++) {
  172. child = ref4[k];
  173. if (func(child) === false) {
  174. return this;
  175. }
  176. }
  177. }
  178. }
  179. return this;
  180. };
  181. Base.prototype.traverseChildren = function(crossScope, func) {
  182. return this.eachChild(function(child) {
  183. var recur;
  184. recur = func(child);
  185. if (recur !== false) {
  186. return child.traverseChildren(crossScope, func);
  187. }
  188. });
  189. };
  190. Base.prototype.invert = function() {
  191. return new Op('!', this);
  192. };
  193. Base.prototype.unwrapAll = function() {
  194. var node;
  195. node = this;
  196. while (node !== (node = node.unwrap())) {
  197. continue;
  198. }
  199. return node;
  200. };
  201. Base.prototype.children = [];
  202. Base.prototype.isStatement = NO;
  203. Base.prototype.jumps = NO;
  204. Base.prototype.isComplex = YES;
  205. Base.prototype.isChainable = NO;
  206. Base.prototype.isAssignable = NO;
  207. Base.prototype.isNumber = NO;
  208. Base.prototype.unwrap = THIS;
  209. Base.prototype.unfoldSoak = NO;
  210. Base.prototype.assigns = NO;
  211. Base.prototype.updateLocationDataIfMissing = function(locationData) {
  212. if (this.locationData) {
  213. return this;
  214. }
  215. this.locationData = locationData;
  216. return this.eachChild(function(child) {
  217. return child.updateLocationDataIfMissing(locationData);
  218. });
  219. };
  220. Base.prototype.error = function(message) {
  221. return throwSyntaxError(message, this.locationData);
  222. };
  223. Base.prototype.makeCode = function(code) {
  224. return new CodeFragment(this, code);
  225. };
  226. Base.prototype.wrapInBraces = function(fragments) {
  227. return [].concat(this.makeCode('('), fragments, this.makeCode(')'));
  228. };
  229. Base.prototype.joinFragmentArrays = function(fragmentsList, joinStr) {
  230. var answer, fragments, i, j, len1;
  231. answer = [];
  232. for (i = j = 0, len1 = fragmentsList.length; j < len1; i = ++j) {
  233. fragments = fragmentsList[i];
  234. if (i) {
  235. answer.push(this.makeCode(joinStr));
  236. }
  237. answer = answer.concat(fragments);
  238. }
  239. return answer;
  240. };
  241. return Base;
  242. })();
  243. exports.Block = Block = (function(superClass1) {
  244. extend1(Block, superClass1);
  245. function Block(nodes) {
  246. this.expressions = compact(flatten(nodes || []));
  247. }
  248. Block.prototype.children = ['expressions'];
  249. Block.prototype.push = function(node) {
  250. this.expressions.push(node);
  251. return this;
  252. };
  253. Block.prototype.pop = function() {
  254. return this.expressions.pop();
  255. };
  256. Block.prototype.unshift = function(node) {
  257. this.expressions.unshift(node);
  258. return this;
  259. };
  260. Block.prototype.unwrap = function() {
  261. if (this.expressions.length === 1) {
  262. return this.expressions[0];
  263. } else {
  264. return this;
  265. }
  266. };
  267. Block.prototype.isEmpty = function() {
  268. return !this.expressions.length;
  269. };
  270. Block.prototype.isStatement = function(o) {
  271. var exp, j, len1, ref3;
  272. ref3 = this.expressions;
  273. for (j = 0, len1 = ref3.length; j < len1; j++) {
  274. exp = ref3[j];
  275. if (exp.isStatement(o)) {
  276. return true;
  277. }
  278. }
  279. return false;
  280. };
  281. Block.prototype.jumps = function(o) {
  282. var exp, j, jumpNode, len1, ref3;
  283. ref3 = this.expressions;
  284. for (j = 0, len1 = ref3.length; j < len1; j++) {
  285. exp = ref3[j];
  286. if (jumpNode = exp.jumps(o)) {
  287. return jumpNode;
  288. }
  289. }
  290. };
  291. Block.prototype.makeReturn = function(res) {
  292. var expr, len;
  293. len = this.expressions.length;
  294. while (len--) {
  295. expr = this.expressions[len];
  296. if (!(expr instanceof Comment)) {
  297. this.expressions[len] = expr.makeReturn(res);
  298. if (expr instanceof Return && !expr.expression) {
  299. this.expressions.splice(len, 1);
  300. }
  301. break;
  302. }
  303. }
  304. return this;
  305. };
  306. Block.prototype.compileToFragments = function(o, level) {
  307. if (o == null) {
  308. o = {};
  309. }
  310. if (o.scope) {
  311. return Block.__super__.compileToFragments.call(this, o, level);
  312. } else {
  313. return this.compileRoot(o);
  314. }
  315. };
  316. Block.prototype.compileNode = function(o) {
  317. var answer, compiledNodes, fragments, index, j, len1, node, ref3, top;
  318. this.tab = o.indent;
  319. top = o.level === LEVEL_TOP;
  320. compiledNodes = [];
  321. ref3 = this.expressions;
  322. for (index = j = 0, len1 = ref3.length; j < len1; index = ++j) {
  323. node = ref3[index];
  324. node = node.unwrapAll();
  325. node = node.unfoldSoak(o) || node;
  326. if (node instanceof Block) {
  327. compiledNodes.push(node.compileNode(o));
  328. } else if (top) {
  329. node.front = true;
  330. fragments = node.compileToFragments(o);
  331. if (!node.isStatement(o)) {
  332. fragments.unshift(this.makeCode("" + this.tab));
  333. fragments.push(this.makeCode(";"));
  334. }
  335. compiledNodes.push(fragments);
  336. } else {
  337. compiledNodes.push(node.compileToFragments(o, LEVEL_LIST));
  338. }
  339. }
  340. if (top) {
  341. if (this.spaced) {
  342. return [].concat(this.joinFragmentArrays(compiledNodes, '\n\n'), this.makeCode("\n"));
  343. } else {
  344. return this.joinFragmentArrays(compiledNodes, '\n');
  345. }
  346. }
  347. if (compiledNodes.length) {
  348. answer = this.joinFragmentArrays(compiledNodes, ', ');
  349. } else {
  350. answer = [this.makeCode("void 0")];
  351. }
  352. if (compiledNodes.length > 1 && o.level >= LEVEL_LIST) {
  353. return this.wrapInBraces(answer);
  354. } else {
  355. return answer;
  356. }
  357. };
  358. Block.prototype.compileRoot = function(o) {
  359. var exp, fragments, i, j, len1, name, prelude, preludeExps, ref3, ref4, rest;
  360. o.indent = o.bare ? '' : TAB;
  361. o.level = LEVEL_TOP;
  362. this.spaced = true;
  363. o.scope = new Scope(null, this, null, (ref3 = o.referencedVars) != null ? ref3 : []);
  364. ref4 = o.locals || [];
  365. for (j = 0, len1 = ref4.length; j < len1; j++) {
  366. name = ref4[j];
  367. o.scope.parameter(name);
  368. }
  369. prelude = [];
  370. if (!o.bare) {
  371. preludeExps = (function() {
  372. var k, len2, ref5, results;
  373. ref5 = this.expressions;
  374. results = [];
  375. for (i = k = 0, len2 = ref5.length; k < len2; i = ++k) {
  376. exp = ref5[i];
  377. if (!(exp.unwrap() instanceof Comment)) {
  378. break;
  379. }
  380. results.push(exp);
  381. }
  382. return results;
  383. }).call(this);
  384. rest = this.expressions.slice(preludeExps.length);
  385. this.expressions = preludeExps;
  386. if (preludeExps.length) {
  387. prelude = this.compileNode(merge(o, {
  388. indent: ''
  389. }));
  390. prelude.push(this.makeCode("\n"));
  391. }
  392. this.expressions = rest;
  393. }
  394. fragments = this.compileWithDeclarations(o);
  395. if (o.bare) {
  396. return fragments;
  397. }
  398. return [].concat(prelude, this.makeCode("(function() {\n"), fragments, this.makeCode("\n}).call(this);\n"));
  399. };
  400. Block.prototype.compileWithDeclarations = function(o) {
  401. var assigns, declars, exp, fragments, i, j, len1, post, ref3, ref4, ref5, rest, scope, spaced;
  402. fragments = [];
  403. post = [];
  404. ref3 = this.expressions;
  405. for (i = j = 0, len1 = ref3.length; j < len1; i = ++j) {
  406. exp = ref3[i];
  407. exp = exp.unwrap();
  408. if (!(exp instanceof Comment || exp instanceof Literal)) {
  409. break;
  410. }
  411. }
  412. o = merge(o, {
  413. level: LEVEL_TOP
  414. });
  415. if (i) {
  416. rest = this.expressions.splice(i, 9e9);
  417. ref4 = [this.spaced, false], spaced = ref4[0], this.spaced = ref4[1];
  418. ref5 = [this.compileNode(o), spaced], fragments = ref5[0], this.spaced = ref5[1];
  419. this.expressions = rest;
  420. }
  421. post = this.compileNode(o);
  422. scope = o.scope;
  423. if (scope.expressions === this) {
  424. declars = o.scope.hasDeclarations();
  425. assigns = scope.hasAssignments;
  426. if (declars || assigns) {
  427. if (i) {
  428. fragments.push(this.makeCode('\n'));
  429. }
  430. fragments.push(this.makeCode(this.tab + "var "));
  431. if (declars) {
  432. fragments.push(this.makeCode(scope.declaredVariables().join(', ')));
  433. }
  434. if (assigns) {
  435. if (declars) {
  436. fragments.push(this.makeCode(",\n" + (this.tab + TAB)));
  437. }
  438. fragments.push(this.makeCode(scope.assignedVariables().join(",\n" + (this.tab + TAB))));
  439. }
  440. fragments.push(this.makeCode(";\n" + (this.spaced ? '\n' : '')));
  441. } else if (fragments.length && post.length) {
  442. fragments.push(this.makeCode("\n"));
  443. }
  444. }
  445. return fragments.concat(post);
  446. };
  447. Block.wrap = function(nodes) {
  448. if (nodes.length === 1 && nodes[0] instanceof Block) {
  449. return nodes[0];
  450. }
  451. return new Block(nodes);
  452. };
  453. return Block;
  454. })(Base);
  455. exports.Literal = Literal = (function(superClass1) {
  456. extend1(Literal, superClass1);
  457. function Literal(value1) {
  458. this.value = value1;
  459. }
  460. Literal.prototype.isComplex = NO;
  461. Literal.prototype.assigns = function(name) {
  462. return name === this.value;
  463. };
  464. Literal.prototype.compileNode = function(o) {
  465. return [this.makeCode(this.value)];
  466. };
  467. Literal.prototype.toString = function() {
  468. return " " + (this.isStatement() ? Literal.__super__.toString.apply(this, arguments) : this.constructor.name) + ": " + this.value;
  469. };
  470. return Literal;
  471. })(Base);
  472. exports.NumberLiteral = NumberLiteral = (function(superClass1) {
  473. extend1(NumberLiteral, superClass1);
  474. function NumberLiteral() {
  475. return NumberLiteral.__super__.constructor.apply(this, arguments);
  476. }
  477. return NumberLiteral;
  478. })(Literal);
  479. exports.InfinityLiteral = InfinityLiteral = (function(superClass1) {
  480. extend1(InfinityLiteral, superClass1);
  481. function InfinityLiteral() {
  482. return InfinityLiteral.__super__.constructor.apply(this, arguments);
  483. }
  484. InfinityLiteral.prototype.compileNode = function() {
  485. return [this.makeCode('2e308')];
  486. };
  487. return InfinityLiteral;
  488. })(NumberLiteral);
  489. exports.NaNLiteral = NaNLiteral = (function(superClass1) {
  490. extend1(NaNLiteral, superClass1);
  491. function NaNLiteral() {
  492. NaNLiteral.__super__.constructor.call(this, 'NaN');
  493. }
  494. NaNLiteral.prototype.compileNode = function(o) {
  495. var code;
  496. code = [this.makeCode('0/0')];
  497. if (o.level >= LEVEL_OP) {
  498. return this.wrapInBraces(code);
  499. } else {
  500. return code;
  501. }
  502. };
  503. return NaNLiteral;
  504. })(NumberLiteral);
  505. exports.StringLiteral = StringLiteral = (function(superClass1) {
  506. extend1(StringLiteral, superClass1);
  507. function StringLiteral() {
  508. return StringLiteral.__super__.constructor.apply(this, arguments);
  509. }
  510. return StringLiteral;
  511. })(Literal);
  512. exports.RegexLiteral = RegexLiteral = (function(superClass1) {
  513. extend1(RegexLiteral, superClass1);
  514. function RegexLiteral() {
  515. return RegexLiteral.__super__.constructor.apply(this, arguments);
  516. }
  517. return RegexLiteral;
  518. })(Literal);
  519. exports.PassthroughLiteral = PassthroughLiteral = (function(superClass1) {
  520. extend1(PassthroughLiteral, superClass1);
  521. function PassthroughLiteral() {
  522. return PassthroughLiteral.__super__.constructor.apply(this, arguments);
  523. }
  524. return PassthroughLiteral;
  525. })(Literal);
  526. exports.IdentifierLiteral = IdentifierLiteral = (function(superClass1) {
  527. extend1(IdentifierLiteral, superClass1);
  528. function IdentifierLiteral() {
  529. return IdentifierLiteral.__super__.constructor.apply(this, arguments);
  530. }
  531. IdentifierLiteral.prototype.isAssignable = YES;
  532. return IdentifierLiteral;
  533. })(Literal);
  534. exports.PropertyName = PropertyName = (function(superClass1) {
  535. extend1(PropertyName, superClass1);
  536. function PropertyName() {
  537. return PropertyName.__super__.constructor.apply(this, arguments);
  538. }
  539. PropertyName.prototype.isAssignable = YES;
  540. return PropertyName;
  541. })(Literal);
  542. exports.StatementLiteral = StatementLiteral = (function(superClass1) {
  543. extend1(StatementLiteral, superClass1);
  544. function StatementLiteral() {
  545. return StatementLiteral.__super__.constructor.apply(this, arguments);
  546. }
  547. StatementLiteral.prototype.isStatement = YES;
  548. StatementLiteral.prototype.makeReturn = THIS;
  549. StatementLiteral.prototype.jumps = function(o) {
  550. if (this.value === 'break' && !((o != null ? o.loop : void 0) || (o != null ? o.block : void 0))) {
  551. return this;
  552. }
  553. if (this.value === 'continue' && !(o != null ? o.loop : void 0)) {
  554. return this;
  555. }
  556. };
  557. StatementLiteral.prototype.compileNode = function(o) {
  558. return [this.makeCode("" + this.tab + this.value + ";")];
  559. };
  560. return StatementLiteral;
  561. })(Literal);
  562. exports.ThisLiteral = ThisLiteral = (function(superClass1) {
  563. extend1(ThisLiteral, superClass1);
  564. function ThisLiteral() {
  565. ThisLiteral.__super__.constructor.call(this, 'this');
  566. }
  567. ThisLiteral.prototype.compileNode = function(o) {
  568. var code, ref3;
  569. code = ((ref3 = o.scope.method) != null ? ref3.bound : void 0) ? o.scope.method.context : this.value;
  570. return [this.makeCode(code)];
  571. };
  572. return ThisLiteral;
  573. })(Literal);
  574. exports.UndefinedLiteral = UndefinedLiteral = (function(superClass1) {
  575. extend1(UndefinedLiteral, superClass1);
  576. function UndefinedLiteral() {
  577. UndefinedLiteral.__super__.constructor.call(this, 'undefined');
  578. }
  579. UndefinedLiteral.prototype.compileNode = function(o) {
  580. return [this.makeCode(o.level >= LEVEL_ACCESS ? '(void 0)' : 'void 0')];
  581. };
  582. return UndefinedLiteral;
  583. })(Literal);
  584. exports.NullLiteral = NullLiteral = (function(superClass1) {
  585. extend1(NullLiteral, superClass1);
  586. function NullLiteral() {
  587. NullLiteral.__super__.constructor.call(this, 'null');
  588. }
  589. return NullLiteral;
  590. })(Literal);
  591. exports.BooleanLiteral = BooleanLiteral = (function(superClass1) {
  592. extend1(BooleanLiteral, superClass1);
  593. function BooleanLiteral() {
  594. return BooleanLiteral.__super__.constructor.apply(this, arguments);
  595. }
  596. return BooleanLiteral;
  597. })(Literal);
  598. exports.Return = Return = (function(superClass1) {
  599. extend1(Return, superClass1);
  600. function Return(expression) {
  601. this.expression = expression;
  602. }
  603. Return.prototype.children = ['expression'];
  604. Return.prototype.isStatement = YES;
  605. Return.prototype.makeReturn = THIS;
  606. Return.prototype.jumps = THIS;
  607. Return.prototype.compileToFragments = function(o, level) {
  608. var expr, ref3;
  609. expr = (ref3 = this.expression) != null ? ref3.makeReturn() : void 0;
  610. if (expr && !(expr instanceof Return)) {
  611. return expr.compileToFragments(o, level);
  612. } else {
  613. return Return.__super__.compileToFragments.call(this, o, level);
  614. }
  615. };
  616. Return.prototype.compileNode = function(o) {
  617. var answer;
  618. answer = [];
  619. answer.push(this.makeCode(this.tab + ("return" + (this.expression ? " " : ""))));
  620. if (this.expression) {
  621. answer = answer.concat(this.expression.compileToFragments(o, LEVEL_PAREN));
  622. }
  623. answer.push(this.makeCode(";"));
  624. return answer;
  625. };
  626. return Return;
  627. })(Base);
  628. exports.YieldReturn = YieldReturn = (function(superClass1) {
  629. extend1(YieldReturn, superClass1);
  630. function YieldReturn() {
  631. return YieldReturn.__super__.constructor.apply(this, arguments);
  632. }
  633. YieldReturn.prototype.compileNode = function(o) {
  634. if (o.scope.parent == null) {
  635. this.error('yield can only occur inside functions');
  636. }
  637. return YieldReturn.__super__.compileNode.apply(this, arguments);
  638. };
  639. return YieldReturn;
  640. })(Return);
  641. exports.Value = Value = (function(superClass1) {
  642. extend1(Value, superClass1);
  643. function Value(base, props, tag) {
  644. if (!props && base instanceof Value) {
  645. return base;
  646. }
  647. this.base = base;
  648. this.properties = props || [];
  649. if (tag) {
  650. this[tag] = true;
  651. }
  652. return this;
  653. }
  654. Value.prototype.children = ['base', 'properties'];
  655. Value.prototype.add = function(props) {
  656. this.properties = this.properties.concat(props);
  657. return this;
  658. };
  659. Value.prototype.hasProperties = function() {
  660. return !!this.properties.length;
  661. };
  662. Value.prototype.bareLiteral = function(type) {
  663. return !this.properties.length && this.base instanceof type;
  664. };
  665. Value.prototype.isArray = function() {
  666. return this.bareLiteral(Arr);
  667. };
  668. Value.prototype.isRange = function() {
  669. return this.bareLiteral(Range);
  670. };
  671. Value.prototype.isComplex = function() {
  672. return this.hasProperties() || this.base.isComplex();
  673. };
  674. Value.prototype.isAssignable = function() {
  675. return this.hasProperties() || this.base.isAssignable();
  676. };
  677. Value.prototype.isNumber = function() {
  678. return this.bareLiteral(NumberLiteral);
  679. };
  680. Value.prototype.isString = function() {
  681. return this.bareLiteral(StringLiteral);
  682. };
  683. Value.prototype.isRegex = function() {
  684. return this.bareLiteral(RegexLiteral);
  685. };
  686. Value.prototype.isUndefined = function() {
  687. return this.bareLiteral(UndefinedLiteral);
  688. };
  689. Value.prototype.isNull = function() {
  690. return this.bareLiteral(NullLiteral);
  691. };
  692. Value.prototype.isBoolean = function() {
  693. return this.bareLiteral(BooleanLiteral);
  694. };
  695. Value.prototype.isAtomic = function() {
  696. var j, len1, node, ref3;
  697. ref3 = this.properties.concat(this.base);
  698. for (j = 0, len1 = ref3.length; j < len1; j++) {
  699. node = ref3[j];
  700. if (node.soak || node instanceof Call) {
  701. return false;
  702. }
  703. }
  704. return true;
  705. };
  706. Value.prototype.isNotCallable = function() {
  707. return this.isNumber() || this.isString() || this.isRegex() || this.isArray() || this.isRange() || this.isSplice() || this.isObject() || this.isUndefined() || this.isNull() || this.isBoolean();
  708. };
  709. Value.prototype.isStatement = function(o) {
  710. return !this.properties.length && this.base.isStatement(o);
  711. };
  712. Value.prototype.assigns = function(name) {
  713. return !this.properties.length && this.base.assigns(name);
  714. };
  715. Value.prototype.jumps = function(o) {
  716. return !this.properties.length && this.base.jumps(o);
  717. };
  718. Value.prototype.isObject = function(onlyGenerated) {
  719. if (this.properties.length) {
  720. return false;
  721. }
  722. return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
  723. };
  724. Value.prototype.isSplice = function() {
  725. var lastProp, ref3;
  726. ref3 = this.properties, lastProp = ref3[ref3.length - 1];
  727. return lastProp instanceof Slice;
  728. };
  729. Value.prototype.looksStatic = function(className) {
  730. var ref3;
  731. return this.base.value === className && this.properties.length === 1 && ((ref3 = this.properties[0].name) != null ? ref3.value : void 0) !== 'prototype';
  732. };
  733. Value.prototype.unwrap = function() {
  734. if (this.properties.length) {
  735. return this;
  736. } else {
  737. return this.base;
  738. }
  739. };
  740. Value.prototype.cacheReference = function(o) {
  741. var base, bref, name, nref, ref3;
  742. ref3 = this.properties, name = ref3[ref3.length - 1];
  743. if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
  744. return [this, this];
  745. }
  746. base = new Value(this.base, this.properties.slice(0, -1));
  747. if (base.isComplex()) {
  748. bref = new IdentifierLiteral(o.scope.freeVariable('base'));
  749. base = new Value(new Parens(new Assign(bref, base)));
  750. }
  751. if (!name) {
  752. return [base, bref];
  753. }
  754. if (name.isComplex()) {
  755. nref = new IdentifierLiteral(o.scope.freeVariable('name'));
  756. name = new Index(new Assign(nref, name.index));
  757. nref = new Index(nref);
  758. }
  759. return [base.add(name), new Value(bref || base.base, [nref || name])];
  760. };
  761. Value.prototype.compileNode = function(o) {
  762. var fragments, j, len1, prop, props;
  763. this.base.front = this.front;
  764. props = this.properties;
  765. fragments = this.base.compileToFragments(o, (props.length ? LEVEL_ACCESS : null));
  766. if (props.length && SIMPLENUM.test(fragmentsToText(fragments))) {
  767. fragments.push(this.makeCode('.'));
  768. }
  769. for (j = 0, len1 = props.length; j < len1; j++) {
  770. prop = props[j];
  771. fragments.push.apply(fragments, prop.compileToFragments(o));
  772. }
  773. return fragments;
  774. };
  775. Value.prototype.unfoldSoak = function(o) {
  776. return this.unfoldedSoak != null ? this.unfoldedSoak : this.unfoldedSoak = (function(_this) {
  777. return function() {
  778. var fst, i, ifn, j, len1, prop, ref, ref3, ref4, snd;
  779. if (ifn = _this.base.unfoldSoak(o)) {
  780. (ref3 = ifn.body.properties).push.apply(ref3, _this.properties);
  781. return ifn;
  782. }
  783. ref4 = _this.properties;
  784. for (i = j = 0, len1 = ref4.length; j < len1; i = ++j) {
  785. prop = ref4[i];
  786. if (!prop.soak) {
  787. continue;
  788. }
  789. prop.soak = false;
  790. fst = new Value(_this.base, _this.properties.slice(0, i));
  791. snd = new Value(_this.base, _this.properties.slice(i));
  792. if (fst.isComplex()) {
  793. ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
  794. fst = new Parens(new Assign(ref, fst));
  795. snd.base = ref;
  796. }
  797. return new If(new Existence(fst), snd, {
  798. soak: true
  799. });
  800. }
  801. return false;
  802. };
  803. })(this)();
  804. };
  805. return Value;
  806. })(Base);
  807. exports.Comment = Comment = (function(superClass1) {
  808. extend1(Comment, superClass1);
  809. function Comment(comment1) {
  810. this.comment = comment1;
  811. }
  812. Comment.prototype.isStatement = YES;
  813. Comment.prototype.makeReturn = THIS;
  814. Comment.prototype.compileNode = function(o, level) {
  815. var code, comment;
  816. comment = this.comment.replace(/^(\s*)#(?=\s)/gm, "$1 *");
  817. code = "/*" + (multident(comment, this.tab)) + (indexOf.call(comment, '\n') >= 0 ? "\n" + this.tab : '') + " */";
  818. if ((level || o.level) === LEVEL_TOP) {
  819. code = o.indent + code;
  820. }
  821. return [this.makeCode("\n"), this.makeCode(code)];
  822. };
  823. return Comment;
  824. })(Base);
  825. exports.Call = Call = (function(superClass1) {
  826. extend1(Call, superClass1);
  827. function Call(variable1, args1, soak) {
  828. this.variable = variable1;
  829. this.args = args1 != null ? args1 : [];
  830. this.soak = soak;
  831. this.isNew = false;
  832. if (this.variable instanceof Value && this.variable.isNotCallable()) {
  833. this.variable.error("literal is not a function");
  834. }
  835. }
  836. Call.prototype.children = ['variable', 'args'];
  837. Call.prototype.newInstance = function() {
  838. var base, ref3;
  839. base = ((ref3 = this.variable) != null ? ref3.base : void 0) || this.variable;
  840. if (base instanceof Call && !base.isNew) {
  841. base.newInstance();
  842. } else {
  843. this.isNew = true;
  844. }
  845. return this;
  846. };
  847. Call.prototype.unfoldSoak = function(o) {
  848. var call, ifn, j, left, len1, list, ref3, ref4, rite;
  849. if (this.soak) {
  850. if (this instanceof SuperCall) {
  851. left = new Literal(this.superReference(o));
  852. rite = new Value(left);
  853. } else {
  854. if (ifn = unfoldSoak(o, this, 'variable')) {
  855. return ifn;
  856. }
  857. ref3 = new Value(this.variable).cacheReference(o), left = ref3[0], rite = ref3[1];
  858. }
  859. rite = new Call(rite, this.args);
  860. rite.isNew = this.isNew;
  861. left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
  862. return new If(left, new Value(rite), {
  863. soak: true
  864. });
  865. }
  866. call = this;
  867. list = [];
  868. while (true) {
  869. if (call.variable instanceof Call) {
  870. list.push(call);
  871. call = call.variable;
  872. continue;
  873. }
  874. if (!(call.variable instanceof Value)) {
  875. break;
  876. }
  877. list.push(call);
  878. if (!((call = call.variable.base) instanceof Call)) {
  879. break;
  880. }
  881. }
  882. ref4 = list.reverse();
  883. for (j = 0, len1 = ref4.length; j < len1; j++) {
  884. call = ref4[j];
  885. if (ifn) {
  886. if (call.variable instanceof Call) {
  887. call.variable = ifn;
  888. } else {
  889. call.variable.base = ifn;
  890. }
  891. }
  892. ifn = unfoldSoak(o, call, 'variable');
  893. }
  894. return ifn;
  895. };
  896. Call.prototype.compileNode = function(o) {
  897. var arg, argIndex, compiledArgs, compiledArray, fragments, j, len1, preface, ref3, ref4;
  898. if ((ref3 = this.variable) != null) {
  899. ref3.front = this.front;
  900. }
  901. compiledArray = Splat.compileSplattedArray(o, this.args, true);
  902. if (compiledArray.length) {
  903. return this.compileSplat(o, compiledArray);
  904. }
  905. compiledArgs = [];
  906. ref4 = this.args;
  907. for (argIndex = j = 0, len1 = ref4.length; j < len1; argIndex = ++j) {
  908. arg = ref4[argIndex];
  909. if (argIndex) {
  910. compiledArgs.push(this.makeCode(", "));
  911. }
  912. compiledArgs.push.apply(compiledArgs, arg.compileToFragments(o, LEVEL_LIST));
  913. }
  914. fragments = [];
  915. if (this instanceof SuperCall) {
  916. preface = this.superReference(o) + (".call(" + (this.superThis(o)));
  917. if (compiledArgs.length) {
  918. preface += ", ";
  919. }
  920. fragments.push(this.makeCode(preface));
  921. } else {
  922. if (this.isNew) {
  923. fragments.push(this.makeCode('new '));
  924. }
  925. fragments.push.apply(fragments, this.variable.compileToFragments(o, LEVEL_ACCESS));
  926. fragments.push(this.makeCode("("));
  927. }
  928. fragments.push.apply(fragments, compiledArgs);
  929. fragments.push(this.makeCode(")"));
  930. return fragments;
  931. };
  932. Call.prototype.compileSplat = function(o, splatArgs) {
  933. var answer, base, fun, idt, name, ref;
  934. if (this instanceof SuperCall) {
  935. return [].concat(this.makeCode((this.superReference(o)) + ".apply(" + (this.superThis(o)) + ", "), splatArgs, this.makeCode(")"));
  936. }
  937. if (this.isNew) {
  938. idt = this.tab + TAB;
  939. return [].concat(this.makeCode("(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return Object(result) === result ? result : child;\n" + this.tab + "})("), this.variable.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), splatArgs, this.makeCode(", function(){})"));
  940. }
  941. answer = [];
  942. base = new Value(this.variable);
  943. if ((name = base.properties.pop()) && base.isComplex()) {
  944. ref = o.scope.freeVariable('ref');
  945. answer = answer.concat(this.makeCode("(" + ref + " = "), base.compileToFragments(o, LEVEL_LIST), this.makeCode(")"), name.compileToFragments(o));
  946. } else {
  947. fun = base.compileToFragments(o, LEVEL_ACCESS);
  948. if (SIMPLENUM.test(fragmentsToText(fun))) {
  949. fun = this.wrapInBraces(fun);
  950. }
  951. if (name) {
  952. ref = fragmentsToText(fun);
  953. fun.push.apply(fun, name.compileToFragments(o));
  954. } else {
  955. ref = 'null';
  956. }
  957. answer = answer.concat(fun);
  958. }
  959. return answer = answer.concat(this.makeCode(".apply(" + ref + ", "), splatArgs, this.makeCode(")"));
  960. };
  961. return Call;
  962. })(Base);
  963. exports.SuperCall = SuperCall = (function(superClass1) {
  964. extend1(SuperCall, superClass1);
  965. function SuperCall(args) {
  966. SuperCall.__super__.constructor.call(this, null, args != null ? args : [new Splat(new IdentifierLiteral('arguments'))]);
  967. this.isBare = args != null;
  968. }
  969. SuperCall.prototype.superReference = function(o) {
  970. var accesses, base, bref, klass, method, name, nref, variable;
  971. method = o.scope.namedMethod();
  972. if (method != null ? method.klass : void 0) {
  973. klass = method.klass, name = method.name, variable = method.variable;
  974. if (klass.isComplex()) {
  975. bref = new IdentifierLiteral(o.scope.parent.freeVariable('base'));
  976. base = new Value(new Parens(new Assign(bref, klass)));
  977. variable.base = base;
  978. variable.properties.splice(0, klass.properties.length);
  979. }
  980. if (name.isComplex() || (name instanceof Index && name.index.isAssignable())) {
  981. nref = new IdentifierLiteral(o.scope.parent.freeVariable('name'));
  982. name = new Index(new Assign(nref, name.index));
  983. variable.properties.pop();
  984. variable.properties.push(name);
  985. }
  986. accesses = [new Access(new PropertyName('__super__'))];
  987. if (method["static"]) {
  988. accesses.push(new Access(new PropertyName('constructor')));
  989. }
  990. accesses.push(nref != null ? new Index(nref) : name);
  991. return (new Value(bref != null ? bref : klass, accesses)).compile(o);
  992. } else if (method != null ? method.ctor : void 0) {
  993. return method.name + ".__super__.constructor";
  994. } else {
  995. return this.error('cannot call super outside of an instance method.');
  996. }
  997. };
  998. SuperCall.prototype.superThis = function(o) {
  999. var method;
  1000. method = o.scope.method;
  1001. return (method && !method.klass && method.context) || "this";
  1002. };
  1003. return SuperCall;
  1004. })(Call);
  1005. exports.RegexWithInterpolations = RegexWithInterpolations = (function(superClass1) {
  1006. extend1(RegexWithInterpolations, superClass1);
  1007. function RegexWithInterpolations(args) {
  1008. if (args == null) {
  1009. args = [];
  1010. }
  1011. RegexWithInterpolations.__super__.constructor.call(this, new Value(new IdentifierLiteral('RegExp')), args, false);
  1012. }
  1013. return RegexWithInterpolations;
  1014. })(Call);
  1015. exports.Extends = Extends = (function(superClass1) {
  1016. extend1(Extends, superClass1);
  1017. function Extends(child1, parent1) {
  1018. this.child = child1;
  1019. this.parent = parent1;
  1020. }
  1021. Extends.prototype.children = ['child', 'parent'];
  1022. Extends.prototype.compileToFragments = function(o) {
  1023. return new Call(new Value(new Literal(utility('extend', o))), [this.child, this.parent]).compileToFragments(o);
  1024. };
  1025. return Extends;
  1026. })(Base);
  1027. exports.Access = Access = (function(superClass1) {
  1028. extend1(Access, superClass1);
  1029. function Access(name1, tag) {
  1030. this.name = name1;
  1031. this.name.asKey = true;
  1032. this.soak = tag === 'soak';
  1033. }
  1034. Access.prototype.children = ['name'];
  1035. Access.prototype.compileToFragments = function(o) {
  1036. var name, node, ref3;
  1037. name = this.name.compileToFragments(o);
  1038. node = this.name.unwrap();
  1039. if (node instanceof PropertyName) {
  1040. if (ref3 = node.value, indexOf.call(JS_FORBIDDEN, ref3) >= 0) {
  1041. return [this.makeCode('["')].concat(slice.call(name), [this.makeCode('"]')]);
  1042. } else {
  1043. return [this.makeCode('.')].concat(slice.call(name));
  1044. }
  1045. } else {
  1046. return [this.makeCode('[')].concat(slice.call(name), [this.makeCode(']')]);
  1047. }
  1048. };
  1049. Access.prototype.isComplex = NO;
  1050. return Access;
  1051. })(Base);
  1052. exports.Index = Index = (function(superClass1) {
  1053. extend1(Index, superClass1);
  1054. function Index(index1) {
  1055. this.index = index1;
  1056. }
  1057. Index.prototype.children = ['index'];
  1058. Index.prototype.compileToFragments = function(o) {
  1059. return [].concat(this.makeCode("["), this.index.compileToFragments(o, LEVEL_PAREN), this.makeCode("]"));
  1060. };
  1061. Index.prototype.isComplex = function() {
  1062. return this.index.isComplex();
  1063. };
  1064. return Index;
  1065. })(Base);
  1066. exports.Range = Range = (function(superClass1) {
  1067. extend1(Range, superClass1);
  1068. Range.prototype.children = ['from', 'to'];
  1069. function Range(from1, to1, tag) {
  1070. this.from = from1;
  1071. this.to = to1;
  1072. this.exclusive = tag === 'exclusive';
  1073. this.equals = this.exclusive ? '' : '=';
  1074. }
  1075. Range.prototype.compileVariables = function(o) {
  1076. var isComplex, ref3, ref4, ref5, step;
  1077. o = merge(o, {
  1078. top: true
  1079. });
  1080. isComplex = del(o, 'isComplex');
  1081. ref3 = this.cacheToCodeFragments(this.from.cache(o, LEVEL_LIST, isComplex)), this.fromC = ref3[0], this.fromVar = ref3[1];
  1082. ref4 = this.cacheToCodeFragments(this.to.cache(o, LEVEL_LIST, isComplex)), this.toC = ref4[0], this.toVar = ref4[1];
  1083. if (step = del(o, 'step')) {
  1084. ref5 = this.cacheToCodeFragments(step.cache(o, LEVEL_LIST, isComplex)), this.step = ref5[0], this.stepVar = ref5[1];
  1085. }
  1086. this.fromNum = this.from.isNumber() ? Number(this.fromVar) : null;
  1087. this.toNum = this.to.isNumber() ? Number(this.toVar) : null;
  1088. return this.stepNum = (step != null ? step.isNumber() : void 0) ? Number(this.stepVar) : null;
  1089. };
  1090. Range.prototype.compileNode = function(o) {
  1091. var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, ref3, ref4, stepPart, to, varPart;
  1092. if (!this.fromVar) {
  1093. this.compileVariables(o);
  1094. }
  1095. if (!o.index) {
  1096. return this.compileArray(o);
  1097. }
  1098. known = (this.fromNum != null) && (this.toNum != null);
  1099. idx = del(o, 'index');
  1100. idxName = del(o, 'name');
  1101. namedIndex = idxName && idxName !== idx;
  1102. varPart = idx + " = " + this.fromC;
  1103. if (this.toC !== this.toVar) {
  1104. varPart += ", " + this.toC;
  1105. }
  1106. if (this.step !== this.stepVar) {
  1107. varPart += ", " + this.step;
  1108. }
  1109. ref3 = [idx + " <" + this.equals, idx + " >" + this.equals], lt = ref3[0], gt = ref3[1];
  1110. condPart = this.stepNum != null ? this.stepNum > 0 ? lt + " " + this.toVar : gt + " " + this.toVar : known ? ((ref4 = [this.fromNum, this.toNum], from = ref4[0], to = ref4[1], ref4), from <= to ? lt + " " + to : gt + " " + to) : (cond = this.stepVar ? this.stepVar + " > 0" : this.fromVar + " <= " + this.toVar, cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
  1111. stepPart = this.stepVar ? idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? idx + "++" : idx + "--" : namedIndex ? cond + " ? ++" + idx + " : --" + idx : cond + " ? " + idx + "++ : " + idx + "--";
  1112. if (namedIndex) {
  1113. varPart = idxName + " = " + varPart;
  1114. }
  1115. if (namedIndex) {
  1116. stepPart = idxName + " = " + stepPart;
  1117. }
  1118. return [this.makeCode(varPart + "; " + condPart + "; " + stepPart)];
  1119. };
  1120. Range.prototype.compileArray = function(o) {
  1121. var args, body, cond, hasArgs, i, idt, j, known, post, pre, range, ref3, ref4, result, results, vars;
  1122. known = (this.fromNum != null) && (this.toNum != null);
  1123. if (known && Math.abs(this.fromNum - this.toNum) <= 20) {
  1124. range = (function() {
  1125. results = [];
  1126. for (var j = ref3 = this.fromNum, ref4 = this.toNum; ref3 <= ref4 ? j <= ref4 : j >= ref4; ref3 <= ref4 ? j++ : j--){ results.push(j); }
  1127. return results;
  1128. }).apply(this);
  1129. if (this.exclusive) {
  1130. range.pop();
  1131. }
  1132. return [this.makeCode("[" + (range.join(', ')) + "]")];
  1133. }
  1134. idt = this.tab + TAB;
  1135. i = o.scope.freeVariable('i', {
  1136. single: true
  1137. });
  1138. result = o.scope.freeVariable('results');
  1139. pre = "\n" + idt + result + " = [];";
  1140. if (known) {
  1141. o.index = i;
  1142. body = fragmentsToText(this.compileNode(o));
  1143. } else {
  1144. vars = (i + " = " + this.fromC) + (this.toC !== this.toVar ? ", " + this.toC : '');
  1145. cond = this.fromVar + " <= " + this.toVar;
  1146. body = "var " + vars + "; " + cond + " ? " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + cond + " ? " + i + "++ : " + i + "--";
  1147. }
  1148. post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
  1149. hasArgs = function(node) {
  1150. return node != null ? node.contains(isLiteralArguments) : void 0;
  1151. };
  1152. if (hasArgs(this.from) || hasArgs(this.to)) {
  1153. args = ', arguments';
  1154. }
  1155. return [this.makeCode("(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this" + (args != null ? args : '') + ")")];
  1156. };
  1157. return Range;
  1158. })(Base);
  1159. exports.Slice = Slice = (function(superClass1) {
  1160. extend1(Slice, superClass1);
  1161. Slice.prototype.children = ['range'];
  1162. function Slice(range1) {
  1163. this.range = range1;
  1164. Slice.__super__.constructor.call(this);
  1165. }
  1166. Slice.prototype.compileNode = function(o) {
  1167. var compiled, compiledText, from, fromCompiled, ref3, to, toStr;
  1168. ref3 = this.range, to = ref3.to, from = ref3.from;
  1169. fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
  1170. if (to) {
  1171. compiled = to.compileToFragments(o, LEVEL_PAREN);
  1172. compiledText = fragmentsToText(compiled);
  1173. if (!(!this.range.exclusive && +compiledText === -1)) {
  1174. toStr = ', ' + (this.range.exclusive ? compiledText : to.isNumber() ? "" + (+compiledText + 1) : (compiled = to.compileToFragments(o, LEVEL_ACCESS), "+" + (fragmentsToText(compiled)) + " + 1 || 9e9"));
  1175. }
  1176. }
  1177. return [this.makeCode(".slice(" + (fragmentsToText(fromCompiled)) + (toStr || '') + ")")];
  1178. };
  1179. return Slice;
  1180. })(Base);
  1181. exports.Obj = Obj = (function(superClass1) {
  1182. extend1(Obj, superClass1);
  1183. function Obj(props, generated) {
  1184. this.generated = generated != null ? generated : false;
  1185. this.objects = this.properties = props || [];
  1186. }
  1187. Obj.prototype.children = ['properties'];
  1188. Obj.prototype.compileNode = function(o) {
  1189. var answer, dynamicIndex, hasDynamic, i, idt, indent, j, join, k, key, l, lastNoncom, len1, len2, len3, node, oref, prop, props, ref3, value;
  1190. props = this.properties;
  1191. if (this.generated) {
  1192. for (j = 0, len1 = props.length; j < len1; j++) {
  1193. node = props[j];
  1194. if (node instanceof Value) {
  1195. node.error('cannot have an implicit value in an implicit object');
  1196. }
  1197. }
  1198. }
  1199. for (dynamicIndex = k = 0, len2 = props.length; k < len2; dynamicIndex = ++k) {
  1200. prop = props[dynamicIndex];
  1201. if ((prop.variable || prop).base instanceof Parens) {
  1202. break;
  1203. }
  1204. }
  1205. hasDynamic = dynamicIndex < props.length;
  1206. idt = o.indent += TAB;
  1207. lastNoncom = this.lastNonComment(this.properties);
  1208. answer = [];
  1209. if (hasDynamic) {
  1210. oref = o.scope.freeVariable('obj');
  1211. answer.push(this.makeCode("(\n" + idt + oref + " = "));
  1212. }
  1213. answer.push(this.makeCode("{" + (props.length === 0 || dynamicIndex === 0 ? '}' : '\n')));
  1214. for (i = l = 0, len3 = props.length; l < len3; i = ++l) {
  1215. prop = props[i];
  1216. if (i === dynamicIndex) {
  1217. if (i !== 0) {
  1218. answer.push(this.makeCode("\n" + idt + "}"));
  1219. }
  1220. answer.push(this.makeCode(',\n'));
  1221. }
  1222. join = i === props.length - 1 || i === dynamicIndex - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
  1223. indent = prop instanceof Comment ? '' : idt;
  1224. if (hasDynamic && i < dynamicIndex) {
  1225. indent += TAB;
  1226. }
  1227. if (prop instanceof Assign) {
  1228. if (prop.context !== 'object') {
  1229. prop.operatorToken.error("unexpected " + prop.operatorToken.value);
  1230. }
  1231. if (prop.variable instanceof Value && prop.variable.hasProperties()) {
  1232. prop.variable.error('invalid object key');
  1233. }
  1234. }
  1235. if (prop instanceof Value && prop["this"]) {
  1236. prop = new Assign(prop.properties[0].name, prop, 'object');
  1237. }
  1238. if (!(prop instanceof Comment)) {
  1239. if (i < dynamicIndex) {
  1240. if (!(prop instanceof Assign)) {
  1241. prop = new Assign(prop, prop, 'object');
  1242. }
  1243. (prop.variable.base || prop.variable).asKey = true;
  1244. } else {
  1245. if (prop instanceof Assign) {
  1246. key = prop.variable;
  1247. value = prop.value;
  1248. } else {
  1249. ref3 = prop.base.cache(o), key = ref3[0], value = ref3[1];
  1250. }
  1251. prop = new Assign(new Value(new IdentifierLiteral(oref), [new Access(key)]), value);
  1252. }
  1253. }
  1254. if (indent) {
  1255. answer.push(this.makeCode(indent));
  1256. }
  1257. answer.push.apply(answer, prop.compileToFragments(o, LEVEL_TOP));
  1258. if (join) {
  1259. answer.push(this.makeCode(join));
  1260. }
  1261. }
  1262. if (hasDynamic) {
  1263. answer.push(this.makeCode(",\n" + idt + oref + "\n" + this.tab + ")"));
  1264. } else {
  1265. if (props.length !== 0) {
  1266. answer.push(this.makeCode("\n" + this.tab + "}"));
  1267. }
  1268. }
  1269. if (this.front && !hasDynamic) {
  1270. return this.wrapInBraces(answer);
  1271. } else {
  1272. return answer;
  1273. }
  1274. };
  1275. Obj.prototype.assigns = function(name) {
  1276. var j, len1, prop, ref3;
  1277. ref3 = this.properties;
  1278. for (j = 0, len1 = ref3.length; j < len1; j++) {
  1279. prop = ref3[j];
  1280. if (prop.assigns(name)) {
  1281. return true;
  1282. }
  1283. }
  1284. return false;
  1285. };
  1286. return Obj;
  1287. })(Base);
  1288. exports.Arr = Arr = (function(superClass1) {
  1289. extend1(Arr, superClass1);
  1290. function Arr(objs) {
  1291. this.objects = objs || [];
  1292. }
  1293. Arr.prototype.children = ['objects'];
  1294. Arr.prototype.compileNode = function(o) {
  1295. var answer, compiledObjs, fragments, index, j, len1, obj;
  1296. if (!this.objects.length) {
  1297. return [this.makeCode('[]')];
  1298. }
  1299. o.indent += TAB;
  1300. answer = Splat.compileSplattedArray(o, this.objects);
  1301. if (answer.length) {
  1302. return answer;
  1303. }
  1304. answer = [];
  1305. compiledObjs = (function() {
  1306. var j, len1, ref3, results;
  1307. ref3 = this.objects;
  1308. results = [];
  1309. for (j = 0, len1 = ref3.length; j < len1; j++) {
  1310. obj = ref3[j];
  1311. results.push(obj.compileToFragments(o, LEVEL_LIST));
  1312. }
  1313. return results;
  1314. }).call(this);
  1315. for (index = j = 0, len1 = compiledObjs.length; j < len1; index = ++j) {
  1316. fragments = compiledObjs[index];
  1317. if (index) {
  1318. answer.push(this.makeCode(", "));
  1319. }
  1320. answer.push.apply(answer, fragments);
  1321. }
  1322. if (fragmentsToText(answer).indexOf('\n') >= 0) {
  1323. answer.unshift(this.makeCode("[\n" + o.indent));
  1324. answer.push(this.makeCode("\n" + this.tab + "]"));
  1325. } else {
  1326. answer.unshift(this.makeCode("["));