ParseOptions.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  19. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  20. if (ar || !(i in from)) {
  21. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  22. ar[i] = from[i];
  23. }
  24. }
  25. return to.concat(ar || Array.prototype.slice.call(from));
  26. };
  27. var __values = (this && this.__values) || function(o) {
  28. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  29. if (m) return m.call(o);
  30. if (o && typeof o.length === "number") return {
  31. next: function () {
  32. if (o && i >= o.length) o = void 0;
  33. return { value: o && o[i++], done: !o };
  34. }
  35. };
  36. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  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 StackItemFactory_js_1 = __importDefault(require("./StackItemFactory.js"));
  43. var NodeFactory_js_1 = require("./NodeFactory.js");
  44. var NodeUtil_js_1 = __importDefault(require("./NodeUtil.js"));
  45. var Options_js_1 = require("../../util/Options.js");
  46. var ParseOptions = (function () {
  47. function ParseOptions(configuration, options) {
  48. if (options === void 0) { options = []; }
  49. this.options = {};
  50. this.packageData = new Map();
  51. this.parsers = [];
  52. this.root = null;
  53. this.nodeLists = {};
  54. this.error = false;
  55. this.handlers = configuration.handlers;
  56. this.nodeFactory = new NodeFactory_js_1.NodeFactory();
  57. this.nodeFactory.configuration = this;
  58. this.nodeFactory.setCreators(configuration.nodes);
  59. this.itemFactory = new StackItemFactory_js_1.default(configuration.items);
  60. this.itemFactory.configuration = this;
  61. Options_js_1.defaultOptions.apply(void 0, __spreadArray([this.options], __read(options), false));
  62. (0, Options_js_1.defaultOptions)(this.options, configuration.options);
  63. }
  64. ParseOptions.prototype.pushParser = function (parser) {
  65. this.parsers.unshift(parser);
  66. };
  67. ParseOptions.prototype.popParser = function () {
  68. this.parsers.shift();
  69. };
  70. Object.defineProperty(ParseOptions.prototype, "parser", {
  71. get: function () {
  72. return this.parsers[0];
  73. },
  74. enumerable: false,
  75. configurable: true
  76. });
  77. ParseOptions.prototype.clear = function () {
  78. this.parsers = [];
  79. this.root = null;
  80. this.nodeLists = {};
  81. this.error = false;
  82. this.tags.resetTag();
  83. };
  84. ParseOptions.prototype.addNode = function (property, node) {
  85. var list = this.nodeLists[property];
  86. if (!list) {
  87. list = this.nodeLists[property] = [];
  88. }
  89. list.push(node);
  90. if (node.kind !== property) {
  91. var inlists = (NodeUtil_js_1.default.getProperty(node, 'in-lists') || '');
  92. var lists = (inlists ? inlists.split(/,/) : []).concat(property).join(',');
  93. NodeUtil_js_1.default.setProperty(node, 'in-lists', lists);
  94. }
  95. };
  96. ParseOptions.prototype.getList = function (property) {
  97. var e_1, _a;
  98. var list = this.nodeLists[property] || [];
  99. var result = [];
  100. try {
  101. for (var list_1 = __values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) {
  102. var node = list_1_1.value;
  103. if (this.inTree(node)) {
  104. result.push(node);
  105. }
  106. }
  107. }
  108. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  109. finally {
  110. try {
  111. if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
  112. }
  113. finally { if (e_1) throw e_1.error; }
  114. }
  115. this.nodeLists[property] = result;
  116. return result;
  117. };
  118. ParseOptions.prototype.removeFromList = function (property, nodes) {
  119. var e_2, _a;
  120. var list = this.nodeLists[property] || [];
  121. try {
  122. for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
  123. var node = nodes_1_1.value;
  124. var i = list.indexOf(node);
  125. if (i >= 0) {
  126. list.splice(i, 1);
  127. }
  128. }
  129. }
  130. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  131. finally {
  132. try {
  133. if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
  134. }
  135. finally { if (e_2) throw e_2.error; }
  136. }
  137. };
  138. ParseOptions.prototype.inTree = function (node) {
  139. while (node && node !== this.root) {
  140. node = node.parent;
  141. }
  142. return !!node;
  143. };
  144. return ParseOptions;
  145. }());
  146. exports.default = ParseOptions;
  147. //# sourceMappingURL=ParseOptions.js.map