TexParser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 ParseUtil_js_1 = __importDefault(require("./ParseUtil.js"));
  43. var Stack_js_1 = __importDefault(require("./Stack.js"));
  44. var TexError_js_1 = __importDefault(require("./TexError.js"));
  45. var MmlNode_js_1 = require("../../core/MmlTree/MmlNode.js");
  46. var TexParser = (function () {
  47. function TexParser(_string, env, configuration) {
  48. var e_1, _a;
  49. this._string = _string;
  50. this.configuration = configuration;
  51. this.macroCount = 0;
  52. this.i = 0;
  53. this.currentCS = '';
  54. var inner = env.hasOwnProperty('isInner');
  55. var isInner = env['isInner'];
  56. delete env['isInner'];
  57. var ENV;
  58. if (env) {
  59. ENV = {};
  60. try {
  61. for (var _b = __values(Object.keys(env)), _c = _b.next(); !_c.done; _c = _b.next()) {
  62. var id = _c.value;
  63. ENV[id] = env[id];
  64. }
  65. }
  66. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  67. finally {
  68. try {
  69. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  70. }
  71. finally { if (e_1) throw e_1.error; }
  72. }
  73. }
  74. this.configuration.pushParser(this);
  75. this.stack = new Stack_js_1.default(this.itemFactory, ENV, inner ? isInner : true);
  76. this.Parse();
  77. this.Push(this.itemFactory.create('stop'));
  78. }
  79. Object.defineProperty(TexParser.prototype, "options", {
  80. get: function () {
  81. return this.configuration.options;
  82. },
  83. enumerable: false,
  84. configurable: true
  85. });
  86. Object.defineProperty(TexParser.prototype, "itemFactory", {
  87. get: function () {
  88. return this.configuration.itemFactory;
  89. },
  90. enumerable: false,
  91. configurable: true
  92. });
  93. Object.defineProperty(TexParser.prototype, "tags", {
  94. get: function () {
  95. return this.configuration.tags;
  96. },
  97. enumerable: false,
  98. configurable: true
  99. });
  100. Object.defineProperty(TexParser.prototype, "string", {
  101. get: function () {
  102. return this._string;
  103. },
  104. set: function (str) {
  105. this._string = str;
  106. },
  107. enumerable: false,
  108. configurable: true
  109. });
  110. TexParser.prototype.parse = function (kind, input) {
  111. return this.configuration.handlers.get(kind).parse(input);
  112. };
  113. TexParser.prototype.lookup = function (kind, symbol) {
  114. return this.configuration.handlers.get(kind).lookup(symbol);
  115. };
  116. TexParser.prototype.contains = function (kind, symbol) {
  117. return this.configuration.handlers.get(kind).contains(symbol);
  118. };
  119. TexParser.prototype.toString = function () {
  120. var e_2, _a;
  121. var str = '';
  122. try {
  123. for (var _b = __values(Array.from(this.configuration.handlers.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
  124. var config = _c.value;
  125. str += config + ': ' +
  126. this.configuration.handlers.get(config) + '\n';
  127. }
  128. }
  129. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  130. finally {
  131. try {
  132. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  133. }
  134. finally { if (e_2) throw e_2.error; }
  135. }
  136. return str;
  137. };
  138. TexParser.prototype.Parse = function () {
  139. var c;
  140. while (this.i < this.string.length) {
  141. c = this.getCodePoint();
  142. this.i += c.length;
  143. this.parse('character', [this, c]);
  144. }
  145. };
  146. TexParser.prototype.Push = function (arg) {
  147. if (arg instanceof MmlNode_js_1.AbstractMmlNode && arg.isInferred) {
  148. this.PushAll(arg.childNodes);
  149. }
  150. else {
  151. this.stack.Push(arg);
  152. }
  153. };
  154. TexParser.prototype.PushAll = function (args) {
  155. var e_3, _a;
  156. try {
  157. for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
  158. var arg = args_1_1.value;
  159. this.stack.Push(arg);
  160. }
  161. }
  162. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  163. finally {
  164. try {
  165. if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
  166. }
  167. finally { if (e_3) throw e_3.error; }
  168. }
  169. };
  170. TexParser.prototype.mml = function () {
  171. if (!this.stack.Top().isKind('mml')) {
  172. return null;
  173. }
  174. var node = this.stack.Top().First;
  175. this.configuration.popParser();
  176. return node;
  177. };
  178. TexParser.prototype.convertDelimiter = function (c) {
  179. var symbol = this.lookup('delimiter', c);
  180. return symbol ? symbol.char : null;
  181. };
  182. TexParser.prototype.getCodePoint = function () {
  183. var code = this.string.codePointAt(this.i);
  184. return code === undefined ? '' : String.fromCodePoint(code);
  185. };
  186. TexParser.prototype.nextIsSpace = function () {
  187. return !!this.string.charAt(this.i).match(/\s/);
  188. };
  189. TexParser.prototype.GetNext = function () {
  190. while (this.nextIsSpace()) {
  191. this.i++;
  192. }
  193. return this.getCodePoint();
  194. };
  195. TexParser.prototype.GetCS = function () {
  196. var CS = this.string.slice(this.i).match(/^(([a-z]+) ?|[\uD800-\uDBFF].|.)/i);
  197. if (CS) {
  198. this.i += CS[0].length;
  199. return CS[2] || CS[1];
  200. }
  201. else {
  202. this.i++;
  203. return ' ';
  204. }
  205. };
  206. TexParser.prototype.GetArgument = function (_name, noneOK) {
  207. switch (this.GetNext()) {
  208. case '':
  209. if (!noneOK) {
  210. throw new TexError_js_1.default('MissingArgFor', 'Missing argument for %1', this.currentCS);
  211. }
  212. return null;
  213. case '}':
  214. if (!noneOK) {
  215. throw new TexError_js_1.default('ExtraCloseMissingOpen', 'Extra close brace or missing open brace');
  216. }
  217. return null;
  218. case '\\':
  219. this.i++;
  220. return '\\' + this.GetCS();
  221. case '{':
  222. var j = ++this.i, parens = 1;
  223. while (this.i < this.string.length) {
  224. switch (this.string.charAt(this.i++)) {
  225. case '\\':
  226. this.i++;
  227. break;
  228. case '{':
  229. parens++;
  230. break;
  231. case '}':
  232. if (--parens === 0) {
  233. return this.string.slice(j, this.i - 1);
  234. }
  235. break;
  236. }
  237. }
  238. throw new TexError_js_1.default('MissingCloseBrace', 'Missing close brace');
  239. }
  240. var c = this.getCodePoint();
  241. this.i += c.length;
  242. return c;
  243. };
  244. TexParser.prototype.GetBrackets = function (_name, def) {
  245. if (this.GetNext() !== '[') {
  246. return def;
  247. }
  248. var j = ++this.i, parens = 0;
  249. while (this.i < this.string.length) {
  250. switch (this.string.charAt(this.i++)) {
  251. case '{':
  252. parens++;
  253. break;
  254. case '\\':
  255. this.i++;
  256. break;
  257. case '}':
  258. if (parens-- <= 0) {
  259. throw new TexError_js_1.default('ExtraCloseLooking', 'Extra close brace while looking for %1', '\']\'');
  260. }
  261. break;
  262. case ']':
  263. if (parens === 0) {
  264. return this.string.slice(j, this.i - 1);
  265. }
  266. break;
  267. }
  268. }
  269. throw new TexError_js_1.default('MissingCloseBracket', 'Could not find closing \']\' for argument to %1', this.currentCS);
  270. };
  271. TexParser.prototype.GetDelimiter = function (name, braceOK) {
  272. var c = this.GetNext();
  273. this.i += c.length;
  274. if (this.i <= this.string.length) {
  275. if (c === '\\') {
  276. c += this.GetCS();
  277. }
  278. else if (c === '{' && braceOK) {
  279. this.i--;
  280. c = this.GetArgument(name).trim();
  281. }
  282. if (this.contains('delimiter', c)) {
  283. return this.convertDelimiter(c);
  284. }
  285. }
  286. throw new TexError_js_1.default('MissingOrUnrecognizedDelim', 'Missing or unrecognized delimiter for %1', this.currentCS);
  287. };
  288. TexParser.prototype.GetDimen = function (name) {
  289. if (this.GetNext() === '{') {
  290. var dimen = this.GetArgument(name);
  291. var _a = __read(ParseUtil_js_1.default.matchDimen(dimen), 2), value = _a[0], unit = _a[1];
  292. if (value) {
  293. return value + unit;
  294. }
  295. }
  296. else {
  297. var dimen = this.string.slice(this.i);
  298. var _b = __read(ParseUtil_js_1.default.matchDimen(dimen, true), 3), value = _b[0], unit = _b[1], length_1 = _b[2];
  299. if (value) {
  300. this.i += length_1;
  301. return value + unit;
  302. }
  303. }
  304. throw new TexError_js_1.default('MissingDimOrUnits', 'Missing dimension or its units for %1', this.currentCS);
  305. };
  306. TexParser.prototype.GetUpTo = function (_name, token) {
  307. while (this.nextIsSpace()) {
  308. this.i++;
  309. }
  310. var j = this.i;
  311. var parens = 0;
  312. while (this.i < this.string.length) {
  313. var k = this.i;
  314. var c = this.GetNext();
  315. this.i += c.length;
  316. switch (c) {
  317. case '\\':
  318. c += this.GetCS();
  319. break;
  320. case '{':
  321. parens++;
  322. break;
  323. case '}':
  324. if (parens === 0) {
  325. throw new TexError_js_1.default('ExtraCloseLooking', 'Extra close brace while looking for %1', token);
  326. }
  327. parens--;
  328. break;
  329. }
  330. if (parens === 0 && c === token) {
  331. return this.string.slice(j, k);
  332. }
  333. }
  334. throw new TexError_js_1.default('TokenNotFoundForCommand', 'Could not find %1 for %2', token, this.currentCS);
  335. };
  336. TexParser.prototype.ParseArg = function (name) {
  337. return new TexParser(this.GetArgument(name), this.stack.env, this.configuration).mml();
  338. };
  339. TexParser.prototype.ParseUpTo = function (name, token) {
  340. return new TexParser(this.GetUpTo(name, token), this.stack.env, this.configuration).mml();
  341. };
  342. TexParser.prototype.GetDelimiterArg = function (name) {
  343. var c = ParseUtil_js_1.default.trimSpaces(this.GetArgument(name));
  344. if (c === '') {
  345. return null;
  346. }
  347. if (this.contains('delimiter', c)) {
  348. return c;
  349. }
  350. throw new TexError_js_1.default('MissingOrUnrecognizedDelim', 'Missing or unrecognized delimiter for %1', this.currentCS);
  351. };
  352. TexParser.prototype.GetStar = function () {
  353. var star = (this.GetNext() === '*');
  354. if (star) {
  355. this.i++;
  356. }
  357. return star;
  358. };
  359. TexParser.prototype.create = function (kind) {
  360. var _a;
  361. var rest = [];
  362. for (var _i = 1; _i < arguments.length; _i++) {
  363. rest[_i - 1] = arguments[_i];
  364. }
  365. return (_a = this.configuration.nodeFactory).create.apply(_a, __spreadArray([kind], __read(rest), false));
  366. };
  367. return TexParser;
  368. }());
  369. exports.default = TexParser;
  370. //# sourceMappingURL=TexParser.js.map