Stack.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var __read = (this && this.__read) || function (o, n) {
  14. var m = typeof Symbol === "function" && o[Symbol.iterator];
  15. if (!m) return o;
  16. var i = m.call(o), r, ar = [], e;
  17. try {
  18. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  19. }
  20. catch (error) { e = { error: error }; }
  21. finally {
  22. try {
  23. if (r && !r.done && (m = i["return"])) m.call(i);
  24. }
  25. finally { if (e) throw e.error; }
  26. }
  27. return ar;
  28. };
  29. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  30. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  31. if (ar || !(i in from)) {
  32. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  33. ar[i] = from[i];
  34. }
  35. }
  36. return to.concat(ar || Array.prototype.slice.call(from));
  37. };
  38. var __importDefault = (this && this.__importDefault) || function (mod) {
  39. return (mod && mod.__esModule) ? mod : { "default": mod };
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. var NodeUtil_js_1 = __importDefault(require("./NodeUtil.js"));
  43. var Stack = (function () {
  44. function Stack(_factory, _env, inner) {
  45. this._factory = _factory;
  46. this._env = _env;
  47. this.global = {};
  48. this.stack = [];
  49. this.global = { isInner: inner };
  50. this.stack = [this._factory.create('start', this.global)];
  51. if (_env) {
  52. this.stack[0].env = _env;
  53. }
  54. this.env = this.stack[0].env;
  55. }
  56. Object.defineProperty(Stack.prototype, "env", {
  57. get: function () {
  58. return this._env;
  59. },
  60. set: function (env) {
  61. this._env = env;
  62. },
  63. enumerable: false,
  64. configurable: true
  65. });
  66. Stack.prototype.Push = function () {
  67. var e_1, _a;
  68. var args = [];
  69. for (var _i = 0; _i < arguments.length; _i++) {
  70. args[_i] = arguments[_i];
  71. }
  72. try {
  73. for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
  74. var node = args_1_1.value;
  75. if (!node) {
  76. continue;
  77. }
  78. var item = NodeUtil_js_1.default.isNode(node) ?
  79. this._factory.create('mml', node) : node;
  80. item.global = this.global;
  81. var _b = __read(this.stack.length ? this.Top().checkItem(item) : [null, true], 2), top_1 = _b[0], success = _b[1];
  82. if (!success) {
  83. continue;
  84. }
  85. if (top_1) {
  86. this.Pop();
  87. this.Push.apply(this, __spreadArray([], __read(top_1), false));
  88. continue;
  89. }
  90. this.stack.push(item);
  91. if (item.env) {
  92. if (item.copyEnv) {
  93. Object.assign(item.env, this.env);
  94. }
  95. this.env = item.env;
  96. }
  97. else {
  98. item.env = this.env;
  99. }
  100. }
  101. }
  102. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  103. finally {
  104. try {
  105. if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
  106. }
  107. finally { if (e_1) throw e_1.error; }
  108. }
  109. };
  110. Stack.prototype.Pop = function () {
  111. var item = this.stack.pop();
  112. if (!item.isOpen) {
  113. delete item.env;
  114. }
  115. this.env = (this.stack.length ? this.Top().env : {});
  116. return item;
  117. };
  118. Stack.prototype.Top = function (n) {
  119. if (n === void 0) { n = 1; }
  120. return this.stack.length < n ? null : this.stack[this.stack.length - n];
  121. };
  122. Stack.prototype.Prev = function (noPop) {
  123. var top = this.Top();
  124. return noPop ? top.First : top.Pop();
  125. };
  126. Stack.prototype.toString = function () {
  127. return 'stack[\n ' + this.stack.join('\n ') + '\n]';
  128. };
  129. return Stack;
  130. }());
  131. exports.default = Stack;
  132. //# sourceMappingURL=Stack.js.map