StackItem.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 __read = (this && this.__read) || function (o, n) {
  18. var m = typeof Symbol === "function" && o[Symbol.iterator];
  19. if (!m) return o;
  20. var i = m.call(o), r, ar = [], e;
  21. try {
  22. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  23. }
  24. catch (error) { e = { error: error }; }
  25. finally {
  26. try {
  27. if (r && !r.done && (m = i["return"])) m.call(i);
  28. }
  29. finally { if (e) throw e.error; }
  30. }
  31. return ar;
  32. };
  33. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  34. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  35. if (ar || !(i in from)) {
  36. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  37. ar[i] = from[i];
  38. }
  39. }
  40. return to.concat(ar || Array.prototype.slice.call(from));
  41. };
  42. var __values = (this && this.__values) || function(o) {
  43. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  44. if (m) return m.call(o);
  45. if (o && typeof o.length === "number") return {
  46. next: function () {
  47. if (o && i >= o.length) o = void 0;
  48. return { value: o && o[i++], done: !o };
  49. }
  50. };
  51. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  52. };
  53. var __importDefault = (this && this.__importDefault) || function (mod) {
  54. return (mod && mod.__esModule) ? mod : { "default": mod };
  55. };
  56. Object.defineProperty(exports, "__esModule", { value: true });
  57. exports.BaseItem = exports.MmlStack = void 0;
  58. var TexError_js_1 = __importDefault(require("./TexError.js"));
  59. var MmlStack = (function () {
  60. function MmlStack(_nodes) {
  61. this._nodes = _nodes;
  62. }
  63. Object.defineProperty(MmlStack.prototype, "nodes", {
  64. get: function () {
  65. return this._nodes;
  66. },
  67. enumerable: false,
  68. configurable: true
  69. });
  70. MmlStack.prototype.Push = function () {
  71. var _a;
  72. var nodes = [];
  73. for (var _i = 0; _i < arguments.length; _i++) {
  74. nodes[_i] = arguments[_i];
  75. }
  76. (_a = this._nodes).push.apply(_a, __spreadArray([], __read(nodes), false));
  77. };
  78. MmlStack.prototype.Pop = function () {
  79. return this._nodes.pop();
  80. };
  81. Object.defineProperty(MmlStack.prototype, "First", {
  82. get: function () {
  83. return this._nodes[this.Size() - 1];
  84. },
  85. set: function (node) {
  86. this._nodes[this.Size() - 1] = node;
  87. },
  88. enumerable: false,
  89. configurable: true
  90. });
  91. Object.defineProperty(MmlStack.prototype, "Last", {
  92. get: function () {
  93. return this._nodes[0];
  94. },
  95. set: function (node) {
  96. this._nodes[0] = node;
  97. },
  98. enumerable: false,
  99. configurable: true
  100. });
  101. MmlStack.prototype.Peek = function (n) {
  102. if (n == null) {
  103. n = 1;
  104. }
  105. return this._nodes.slice(this.Size() - n);
  106. };
  107. MmlStack.prototype.Size = function () {
  108. return this._nodes.length;
  109. };
  110. MmlStack.prototype.Clear = function () {
  111. this._nodes = [];
  112. };
  113. MmlStack.prototype.toMml = function (inferred, forceRow) {
  114. if (inferred === void 0) { inferred = true; }
  115. if (this._nodes.length === 1 && !forceRow) {
  116. return this.First;
  117. }
  118. return this.create('node', inferred ? 'inferredMrow' : 'mrow', this._nodes, {});
  119. };
  120. MmlStack.prototype.create = function (kind) {
  121. var _a;
  122. var rest = [];
  123. for (var _i = 1; _i < arguments.length; _i++) {
  124. rest[_i - 1] = arguments[_i];
  125. }
  126. return (_a = this.factory.configuration.nodeFactory).create.apply(_a, __spreadArray([kind], __read(rest), false));
  127. };
  128. return MmlStack;
  129. }());
  130. exports.MmlStack = MmlStack;
  131. var BaseItem = (function (_super) {
  132. __extends(BaseItem, _super);
  133. function BaseItem(factory) {
  134. var nodes = [];
  135. for (var _i = 1; _i < arguments.length; _i++) {
  136. nodes[_i - 1] = arguments[_i];
  137. }
  138. var _this = _super.call(this, nodes) || this;
  139. _this.factory = factory;
  140. _this.global = {};
  141. _this._properties = {};
  142. if (_this.isOpen) {
  143. _this._env = {};
  144. }
  145. return _this;
  146. }
  147. Object.defineProperty(BaseItem.prototype, "kind", {
  148. get: function () {
  149. return 'base';
  150. },
  151. enumerable: false,
  152. configurable: true
  153. });
  154. Object.defineProperty(BaseItem.prototype, "env", {
  155. get: function () {
  156. return this._env;
  157. },
  158. set: function (value) {
  159. this._env = value;
  160. },
  161. enumerable: false,
  162. configurable: true
  163. });
  164. Object.defineProperty(BaseItem.prototype, "copyEnv", {
  165. get: function () {
  166. return true;
  167. },
  168. enumerable: false,
  169. configurable: true
  170. });
  171. BaseItem.prototype.getProperty = function (key) {
  172. return this._properties[key];
  173. };
  174. BaseItem.prototype.setProperty = function (key, value) {
  175. this._properties[key] = value;
  176. return this;
  177. };
  178. Object.defineProperty(BaseItem.prototype, "isOpen", {
  179. get: function () {
  180. return false;
  181. },
  182. enumerable: false,
  183. configurable: true
  184. });
  185. Object.defineProperty(BaseItem.prototype, "isClose", {
  186. get: function () {
  187. return false;
  188. },
  189. enumerable: false,
  190. configurable: true
  191. });
  192. Object.defineProperty(BaseItem.prototype, "isFinal", {
  193. get: function () {
  194. return false;
  195. },
  196. enumerable: false,
  197. configurable: true
  198. });
  199. BaseItem.prototype.isKind = function (kind) {
  200. return kind === this.kind;
  201. };
  202. BaseItem.prototype.checkItem = function (item) {
  203. if (item.isKind('over') && this.isOpen) {
  204. item.setProperty('num', this.toMml(false));
  205. this.Clear();
  206. }
  207. if (item.isKind('cell') && this.isOpen) {
  208. if (item.getProperty('linebreak')) {
  209. return BaseItem.fail;
  210. }
  211. throw new TexError_js_1.default('Misplaced', 'Misplaced %1', item.getName());
  212. }
  213. if (item.isClose && this.getErrors(item.kind)) {
  214. var _a = __read(this.getErrors(item.kind), 2), id = _a[0], message = _a[1];
  215. throw new TexError_js_1.default(id, message, item.getName());
  216. }
  217. if (!item.isFinal) {
  218. return BaseItem.success;
  219. }
  220. this.Push(item.First);
  221. return BaseItem.fail;
  222. };
  223. BaseItem.prototype.clearEnv = function () {
  224. var e_1, _a;
  225. try {
  226. for (var _b = __values(Object.keys(this.env)), _c = _b.next(); !_c.done; _c = _b.next()) {
  227. var id = _c.value;
  228. delete this.env[id];
  229. }
  230. }
  231. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  232. finally {
  233. try {
  234. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  235. }
  236. finally { if (e_1) throw e_1.error; }
  237. }
  238. };
  239. BaseItem.prototype.setProperties = function (def) {
  240. Object.assign(this._properties, def);
  241. return this;
  242. };
  243. BaseItem.prototype.getName = function () {
  244. return this.getProperty('name');
  245. };
  246. BaseItem.prototype.toString = function () {
  247. return this.kind + '[' + this.nodes.join('; ') + ']';
  248. };
  249. BaseItem.prototype.getErrors = function (kind) {
  250. var CLASS = this.constructor;
  251. return (CLASS.errors || {})[kind] || BaseItem.errors[kind];
  252. };
  253. BaseItem.fail = [null, false];
  254. BaseItem.success = [null, true];
  255. BaseItem.errors = {
  256. end: ['MissingBeginExtraEnd', 'Missing \\begin{%1} or extra \\end{%1}'],
  257. close: ['ExtraCloseMissingOpen', 'Extra close brace or missing open brace'],
  258. right: ['MissingLeftExtraRight', 'Missing \\left or extra \\right'],
  259. middle: ['ExtraMiddle', 'Extra \\middle']
  260. };
  261. return BaseItem;
  262. }(MmlStack));
  263. exports.BaseItem = BaseItem;
  264. //# sourceMappingURL=StackItem.js.map