visitor.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.ComplexityVisitor = void 0;
  30. var MmlVisitor_js_1 = require("../../core/MmlTree/MmlVisitor.js");
  31. var collapse_js_1 = require("./collapse.js");
  32. var Options_js_1 = require("../../util/Options.js");
  33. var ComplexityVisitor = (function (_super) {
  34. __extends(ComplexityVisitor, _super);
  35. function ComplexityVisitor(factory, options) {
  36. var _this = _super.call(this, factory) || this;
  37. _this.complexity = {
  38. text: .5,
  39. token: .5,
  40. child: 1,
  41. script: .8,
  42. sqrt: 2,
  43. subsup: 2,
  44. underover: 2,
  45. fraction: 2,
  46. enclose: 2,
  47. action: 2,
  48. phantom: 0,
  49. xml: 2,
  50. glyph: 2
  51. };
  52. var CLASS = _this.constructor;
  53. _this.options = (0, Options_js_1.userOptions)((0, Options_js_1.defaultOptions)({}, CLASS.OPTIONS), options);
  54. _this.collapse = new _this.options.Collapse(_this);
  55. _this.factory = factory;
  56. return _this;
  57. }
  58. ComplexityVisitor.prototype.visitTree = function (node) {
  59. _super.prototype.visitTree.call(this, node, true);
  60. if (this.options.makeCollapsible) {
  61. this.collapse.makeCollapse(node);
  62. }
  63. };
  64. ComplexityVisitor.prototype.visitNode = function (node, save) {
  65. if (node.attributes.get('data-semantic-complexity'))
  66. return;
  67. return _super.prototype.visitNode.call(this, node, save);
  68. };
  69. ComplexityVisitor.prototype.visitDefault = function (node, save) {
  70. var complexity;
  71. if (node.isToken) {
  72. var text = node.getText();
  73. complexity = this.complexity.text * text.length + this.complexity.token;
  74. }
  75. else {
  76. complexity = this.childrenComplexity(node);
  77. }
  78. return this.setComplexity(node, complexity, save);
  79. };
  80. ComplexityVisitor.prototype.visitMfracNode = function (node, save) {
  81. var complexity = this.childrenComplexity(node) * this.complexity.script + this.complexity.fraction;
  82. return this.setComplexity(node, complexity, save);
  83. };
  84. ComplexityVisitor.prototype.visitMsqrtNode = function (node, save) {
  85. var complexity = this.childrenComplexity(node) + this.complexity.sqrt;
  86. return this.setComplexity(node, complexity, save);
  87. };
  88. ComplexityVisitor.prototype.visitMrootNode = function (node, save) {
  89. var complexity = this.childrenComplexity(node) + this.complexity.sqrt
  90. - (1 - this.complexity.script) * this.getComplexity(node.childNodes[1]);
  91. return this.setComplexity(node, complexity, save);
  92. };
  93. ComplexityVisitor.prototype.visitMphantomNode = function (node, save) {
  94. return this.setComplexity(node, this.complexity.phantom, save);
  95. };
  96. ComplexityVisitor.prototype.visitMsNode = function (node, save) {
  97. var text = node.attributes.get('lquote')
  98. + node.getText()
  99. + node.attributes.get('rquote');
  100. var complexity = text.length * this.complexity.text;
  101. return this.setComplexity(node, complexity, save);
  102. };
  103. ComplexityVisitor.prototype.visitMsubsupNode = function (node, save) {
  104. _super.prototype.visitDefault.call(this, node, true);
  105. var sub = node.childNodes[node.sub];
  106. var sup = node.childNodes[node.sup];
  107. var base = node.childNodes[node.base];
  108. var complexity = Math.max(sub ? this.getComplexity(sub) : 0, sup ? this.getComplexity(sup) : 0) * this.complexity.script;
  109. complexity += this.complexity.child * ((sub ? 1 : 0) + (sup ? 1 : 0));
  110. complexity += (base ? this.getComplexity(base) + this.complexity.child : 0);
  111. complexity += this.complexity.subsup;
  112. return this.setComplexity(node, complexity, save);
  113. };
  114. ComplexityVisitor.prototype.visitMsubNode = function (node, save) {
  115. return this.visitMsubsupNode(node, save);
  116. };
  117. ComplexityVisitor.prototype.visitMsupNode = function (node, save) {
  118. return this.visitMsubsupNode(node, save);
  119. };
  120. ComplexityVisitor.prototype.visitMunderoverNode = function (node, save) {
  121. _super.prototype.visitDefault.call(this, node, true);
  122. var under = node.childNodes[node.under];
  123. var over = node.childNodes[node.over];
  124. var base = node.childNodes[node.base];
  125. var complexity = Math.max(under ? this.getComplexity(under) : 0, over ? this.getComplexity(over) : 0) * this.complexity.script;
  126. if (base) {
  127. complexity = Math.max(this.getComplexity(base), complexity);
  128. }
  129. complexity += this.complexity.child * ((under ? 1 : 0) + (over ? 1 : 0) + (base ? 1 : 0));
  130. complexity += this.complexity.underover;
  131. return this.setComplexity(node, complexity, save);
  132. };
  133. ComplexityVisitor.prototype.visitMunderNode = function (node, save) {
  134. return this.visitMunderoverNode(node, save);
  135. };
  136. ComplexityVisitor.prototype.visitMoverNode = function (node, save) {
  137. return this.visitMunderoverNode(node, save);
  138. };
  139. ComplexityVisitor.prototype.visitMencloseNode = function (node, save) {
  140. var complexity = this.childrenComplexity(node) + this.complexity.enclose;
  141. return this.setComplexity(node, complexity, save);
  142. };
  143. ComplexityVisitor.prototype.visitMactionNode = function (node, save) {
  144. this.childrenComplexity(node);
  145. var complexity = this.getComplexity(node.selected);
  146. return this.setComplexity(node, complexity, save);
  147. };
  148. ComplexityVisitor.prototype.visitMsemanticsNode = function (node, save) {
  149. var child = node.childNodes[0];
  150. var complexity = 0;
  151. if (child) {
  152. this.visitNode(child, true);
  153. complexity = this.getComplexity(child);
  154. }
  155. return this.setComplexity(node, complexity, save);
  156. };
  157. ComplexityVisitor.prototype.visitAnnotationNode = function (node, save) {
  158. return this.setComplexity(node, this.complexity.xml, save);
  159. };
  160. ComplexityVisitor.prototype.visitAnnotation_xmlNode = function (node, save) {
  161. return this.setComplexity(node, this.complexity.xml, save);
  162. };
  163. ComplexityVisitor.prototype.visitMglyphNode = function (node, save) {
  164. return this.setComplexity(node, this.complexity.glyph, save);
  165. };
  166. ComplexityVisitor.prototype.getComplexity = function (node) {
  167. var collapsed = node.getProperty('collapsedComplexity');
  168. return (collapsed != null ? collapsed : node.attributes.get('data-semantic-complexity'));
  169. };
  170. ComplexityVisitor.prototype.setComplexity = function (node, complexity, save) {
  171. if (save) {
  172. if (this.options.identifyCollapsible) {
  173. complexity = this.collapse.check(node, complexity);
  174. }
  175. node.attributes.set('data-semantic-complexity', complexity);
  176. }
  177. return complexity;
  178. };
  179. ComplexityVisitor.prototype.childrenComplexity = function (node) {
  180. var e_1, _a;
  181. _super.prototype.visitDefault.call(this, node, true);
  182. var complexity = 0;
  183. try {
  184. for (var _b = __values(node.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  185. var child = _c.value;
  186. complexity += this.getComplexity(child);
  187. }
  188. }
  189. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  190. finally {
  191. try {
  192. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  193. }
  194. finally { if (e_1) throw e_1.error; }
  195. }
  196. if (node.childNodes.length > 1) {
  197. complexity += node.childNodes.length * this.complexity.child;
  198. }
  199. return complexity;
  200. };
  201. ComplexityVisitor.OPTIONS = {
  202. identifyCollapsible: true,
  203. makeCollapsible: true,
  204. Collapse: collapse_js_1.Collapse
  205. };
  206. return ComplexityVisitor;
  207. }(MmlVisitor_js_1.MmlVisitor));
  208. exports.ComplexityVisitor = ComplexityVisitor;
  209. //# sourceMappingURL=visitor.js.map