You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3109 lines
74 KiB

  1. import katex from '../katex.mjs';
  2. /* eslint-disable */
  3. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  4. /* vim: set ts=2 et sw=2 tw=80: */
  5. /*************************************************************
  6. *
  7. * KaTeX mhchem.js
  8. *
  9. * This file implements a KaTeX version of mhchem version 3.3.0.
  10. * It is adapted from MathJax/extensions/TeX/mhchem.js
  11. * It differs from the MathJax version as follows:
  12. * 1. The interface is changed so that it can be called from KaTeX, not MathJax.
  13. * 2. \rlap and \llap are replaced with \mathrlap and \mathllap.
  14. * 3. Four lines of code are edited in order to use \raisebox instead of \raise.
  15. * 4. The reaction arrow code is simplified. All reaction arrows are rendered
  16. * using KaTeX extensible arrows instead of building non-extensible arrows.
  17. * 5. \tripledash vertical alignment is slightly adjusted.
  18. *
  19. * This code, as other KaTeX code, is released under the MIT license.
  20. *
  21. * /*************************************************************
  22. *
  23. * MathJax/extensions/TeX/mhchem.js
  24. *
  25. * Implements the \ce command for handling chemical formulas
  26. * from the mhchem LaTeX package.
  27. *
  28. * ---------------------------------------------------------------------
  29. *
  30. * Copyright (c) 2011-2015 The MathJax Consortium
  31. * Copyright (c) 2015-2018 Martin Hensel
  32. *
  33. * Licensed under the Apache License, Version 2.0 (the "License");
  34. * you may not use this file except in compliance with the License.
  35. * You may obtain a copy of the License at
  36. *
  37. * http://www.apache.org/licenses/LICENSE-2.0
  38. *
  39. * Unless required by applicable law or agreed to in writing, software
  40. * distributed under the License is distributed on an "AS IS" BASIS,
  41. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  42. * See the License for the specific language governing permissions and
  43. * limitations under the License.
  44. */
  45. //
  46. // Coding Style
  47. // - use '' for identifiers that can by minified/uglified
  48. // - use "" for strings that need to stay untouched
  49. // version: "3.3.0" for MathJax and KaTeX
  50. // Add \ce, \pu, and \tripledash to the KaTeX macros.
  51. katex.__defineMacro("\\ce", function (context) {
  52. return chemParse(context.consumeArgs(1)[0], "ce");
  53. });
  54. katex.__defineMacro("\\pu", function (context) {
  55. return chemParse(context.consumeArgs(1)[0], "pu");
  56. }); // Needed for \bond for the ~ forms
  57. // Raise by 2.56mu, not 2mu. We're raising a hyphen-minus, U+002D, not
  58. // a mathematical minus, U+2212. So we need that extra 0.56.
  59. katex.__defineMacro("\\tripledash", "{\\vphantom{-}\\raisebox{2.56mu}{$\\mkern2mu" + "\\tiny\\text{-}\\mkern1mu\\text{-}\\mkern1mu\\text{-}\\mkern2mu$}}");
  60. // This is the main function for handing the \ce and \pu commands.
  61. // It takes the argument to \ce or \pu and returns the corresponding TeX string.
  62. //
  63. var chemParse = function chemParse(tokens, stateMachine) {
  64. // Recreate the argument string from KaTeX's array of tokens.
  65. var str = "";
  66. var expectedLoc = tokens[tokens.length - 1].loc.start;
  67. for (var i = tokens.length - 1; i >= 0; i--) {
  68. if (tokens[i].loc.start > expectedLoc) {
  69. // context.consumeArgs has eaten a space.
  70. str += " ";
  71. expectedLoc = tokens[i].loc.start;
  72. }
  73. str += tokens[i].text;
  74. expectedLoc += tokens[i].text.length;
  75. }
  76. var tex = texify.go(mhchemParser.go(str, stateMachine));
  77. return tex;
  78. }; //
  79. // Core parser for mhchem syntax (recursive)
  80. //
  81. /** @type {MhchemParser} */
  82. var mhchemParser = {
  83. //
  84. // Parses mchem \ce syntax
  85. //
  86. // Call like
  87. // go("H2O");
  88. //
  89. go: function go(input, stateMachine) {
  90. if (!input) {
  91. return [];
  92. }
  93. if (stateMachine === undefined) {
  94. stateMachine = 'ce';
  95. }
  96. var state = '0'; //
  97. // String buffers for parsing:
  98. //
  99. // buffer.a == amount
  100. // buffer.o == element
  101. // buffer.b == left-side superscript
  102. // buffer.p == left-side subscript
  103. // buffer.q == right-side subscript
  104. // buffer.d == right-side superscript
  105. //
  106. // buffer.r == arrow
  107. // buffer.rdt == arrow, script above, type
  108. // buffer.rd == arrow, script above, content
  109. // buffer.rqt == arrow, script below, type
  110. // buffer.rq == arrow, script below, content
  111. //
  112. // buffer.text_
  113. // buffer.rm
  114. // etc.
  115. //
  116. // buffer.parenthesisLevel == int, starting at 0
  117. // buffer.sb == bool, space before
  118. // buffer.beginsWithBond == bool
  119. //
  120. // These letters are also used as state names.
  121. //
  122. // Other states:
  123. // 0 == begin of main part (arrow/operator unlikely)
  124. // 1 == next entity
  125. // 2 == next entity (arrow/operator unlikely)
  126. // 3 == next atom
  127. // c == macro
  128. //
  129. /** @type {Buffer} */
  130. var buffer = {};
  131. buffer['parenthesisLevel'] = 0;
  132. input = input.replace(/\n/g, " ");
  133. input = input.replace(/[\u2212\u2013\u2014\u2010]/g, "-");
  134. input = input.replace(/[\u2026]/g, "..."); //
  135. // Looks through mhchemParser.transitions, to execute a matching action
  136. // (recursive)
  137. //
  138. var lastInput;
  139. var watchdog = 10;
  140. /** @type {ParserOutput[]} */
  141. var output = [];
  142. while (true) {
  143. if (lastInput !== input) {
  144. watchdog = 10;
  145. lastInput = input;
  146. } else {
  147. watchdog--;
  148. } //
  149. // Find actions in transition table
  150. //
  151. var machine = mhchemParser.stateMachines[stateMachine];
  152. var t = machine.transitions[state] || machine.transitions['*'];
  153. iterateTransitions: for (var i = 0; i < t.length; i++) {
  154. var matches = mhchemParser.patterns.match_(t[i].pattern, input);
  155. if (matches) {
  156. //
  157. // Execute actions
  158. //
  159. var task = t[i].task;
  160. for (var iA = 0; iA < task.action_.length; iA++) {
  161. var o; //
  162. // Find and execute action
  163. //
  164. if (machine.actions[task.action_[iA].type_]) {
  165. o = machine.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
  166. } else if (mhchemParser.actions[task.action_[iA].type_]) {
  167. o = mhchemParser.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
  168. } else {
  169. throw ["MhchemBugA", "mhchem bug A. Please report. (" + task.action_[iA].type_ + ")"]; // Trying to use non-existing action
  170. } //
  171. // Add output
  172. //
  173. mhchemParser.concatArray(output, o);
  174. } //
  175. // Set next state,
  176. // Shorten input,
  177. // Continue with next character
  178. // (= apply only one transition per position)
  179. //
  180. state = task.nextState || state;
  181. if (input.length > 0) {
  182. if (!task.revisit) {
  183. input = matches.remainder;
  184. }
  185. if (!task.toContinue) {
  186. break iterateTransitions;
  187. }
  188. } else {
  189. return output;
  190. }
  191. }
  192. } //
  193. // Prevent infinite loop
  194. //
  195. if (watchdog <= 0) {
  196. throw ["MhchemBugU", "mhchem bug U. Please report."]; // Unexpected character
  197. }
  198. }
  199. },
  200. concatArray: function concatArray(a, b) {
  201. if (b) {
  202. if (Array.isArray(b)) {
  203. for (var iB = 0; iB < b.length; iB++) {
  204. a.push(b[iB]);
  205. }
  206. } else {
  207. a.push(b);
  208. }
  209. }
  210. },
  211. patterns: {
  212. //
  213. // Matching patterns
  214. // either regexps or function that return null or {match_:"a", remainder:"bc"}
  215. //
  216. patterns: {
  217. // property names must not look like integers ("2") for correct property traversal order, later on
  218. 'empty': /^$/,
  219. 'else': /^./,
  220. 'else2': /^./,
  221. 'space': /^\s/,
  222. 'space A': /^\s(?=[A-Z\\$])/,
  223. 'space$': /^\s$/,
  224. 'a-z': /^[a-z]/,
  225. 'x': /^x/,
  226. 'x$': /^x$/,
  227. 'i$': /^i$/,
  228. 'letters': /^(?:[a-zA-Z\u03B1-\u03C9\u0391-\u03A9?@]|(?:\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)(?:\s+|\{\}|(?![a-zA-Z]))))+/,
  229. '\\greek': /^\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)(?:\s+|\{\}|(?![a-zA-Z]))/,
  230. 'one lowercase latin letter $': /^(?:([a-z])(?:$|[^a-zA-Z]))$/,
  231. '$one lowercase latin letter$ $': /^\$(?:([a-z])(?:$|[^a-zA-Z]))\$$/,
  232. 'one lowercase greek letter $': /^(?:\$?[\u03B1-\u03C9]\$?|\$?\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega)\s*\$?)(?:\s+|\{\}|(?![a-zA-Z]))$/,
  233. 'digits': /^[0-9]+/,
  234. '-9.,9': /^[+\-]?(?:[0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))/,
  235. '-9.,9 no missing 0': /^[+\-]?[0-9]+(?:[.,][0-9]+)?/,
  236. '(-)(9.,9)(e)(99)': function e99(input) {
  237. var m = input.match(/^(\+\-|\+\/\-|\+|\-|\\pm\s?)?([0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))?(\((?:[0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))\))?(?:([eE]|\s*(\*|x|\\times|\u00D7)\s*10\^)([+\-]?[0-9]+|\{[+\-]?[0-9]+\}))?/);
  238. if (m && m[0]) {
  239. return {
  240. match_: m.splice(1),
  241. remainder: input.substr(m[0].length)
  242. };
  243. }
  244. return null;
  245. },
  246. '(-)(9)^(-9)': function _(input) {
  247. var m = input.match(/^(\+\-|\+\/\-|\+|\-|\\pm\s?)?([0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+)?)\^([+\-]?[0-9]+|\{[+\-]?[0-9]+\})/);
  248. if (m && m[0]) {
  249. return {
  250. match_: m.splice(1),
  251. remainder: input.substr(m[0].length)
  252. };
  253. }
  254. return null;
  255. },
  256. 'state of aggregation $': function stateOfAggregation$(input) {
  257. // ... or crystal system
  258. var a = mhchemParser.patterns.findObserveGroups(input, "", /^\([a-z]{1,3}(?=[\),])/, ")", ""); // (aq), (aq,$\infty$), (aq, sat)
  259. if (a && a.remainder.match(/^($|[\s,;\)\]\}])/)) {
  260. return a;
  261. } // AND end of 'phrase'
  262. var m = input.match(/^(?:\((?:\\ca\s?)?\$[amothc]\$\))/); // OR crystal system ($o$) (\ca$c$)
  263. if (m) {
  264. return {
  265. match_: m[0],
  266. remainder: input.substr(m[0].length)
  267. };
  268. }
  269. return null;
  270. },
  271. '_{(state of aggregation)}$': /^_\{(\([a-z]{1,3}\))\}/,
  272. '{[(': /^(?:\\\{|\[|\()/,
  273. ')]}': /^(?:\)|\]|\\\})/,
  274. ', ': /^[,;]\s*/,
  275. ',': /^[,;]/,
  276. '.': /^[.]/,
  277. '. ': /^([.\u22C5\u00B7\u2022])\s*/,
  278. '...': /^\.\.\.(?=$|[^.])/,
  279. '* ': /^([*])\s*/,
  280. '^{(...)}': function _(input) {
  281. return mhchemParser.patterns.findObserveGroups(input, "^{", "", "", "}");
  282. },
  283. '^($...$)': function $$(input) {
  284. return mhchemParser.patterns.findObserveGroups(input, "^", "$", "$", "");
  285. },
  286. '^a': /^\^([0-9]+|[^\\_])/,
  287. '^\\x{}{}': function x(input) {
  288. return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true);
  289. },
  290. '^\\x{}': function x(input) {
  291. return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", "");
  292. },
  293. '^\\x': /^\^(\\[a-zA-Z]+)\s*/,
  294. '^(-1)': /^\^(-?\d+)/,
  295. '\'': /^'/,
  296. '_{(...)}': function _(input) {
  297. return mhchemParser.patterns.findObserveGroups(input, "_{", "", "", "}");
  298. },
  299. '_($...$)': function _$$(input) {
  300. return mhchemParser.patterns.findObserveGroups(input, "_", "$", "$", "");
  301. },
  302. '_9': /^_([+\-]?[0-9]+|[^\\])/,
  303. '_\\x{}{}': function _X(input) {
  304. return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true);
  305. },
  306. '_\\x{}': function _X(input) {
  307. return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", "");
  308. },
  309. '_\\x': /^_(\\[a-zA-Z]+)\s*/,
  310. '^_': /^(?:\^(?=_)|\_(?=\^)|[\^_]$)/,
  311. '{}': /^\{\}/,
  312. '{...}': function _(input) {
  313. return mhchemParser.patterns.findObserveGroups(input, "", "{", "}", "");
  314. },
  315. '{(...)}': function _(input) {
  316. return mhchemParser.patterns.findObserveGroups(input, "{", "", "", "}");
  317. },
  318. '$...$': function $$(input) {
  319. return mhchemParser.patterns.findObserveGroups(input, "", "$", "$", "");
  320. },
  321. '${(...)}$': function $$(input) {
  322. return mhchemParser.patterns.findObserveGroups(input, "${", "", "", "}$");
  323. },
  324. '$(...)$': function $$(input) {
  325. return mhchemParser.patterns.findObserveGroups(input, "$", "", "", "$");
  326. },
  327. '=<>': /^[=<>]/,
  328. '#': /^[#\u2261]/,
  329. '+': /^\+/,
  330. '-$': /^-(?=[\s_},;\]/]|$|\([a-z]+\))/,
  331. // -space -, -; -] -/ -$ -state-of-aggregation
  332. '-9': /^-(?=[0-9])/,
  333. '- orbital overlap': /^-(?=(?:[spd]|sp)(?:$|[\s,;\)\]\}]))/,
  334. '-': /^-/,
  335. 'pm-operator': /^(?:\\pm|\$\\pm\$|\+-|\+\/-)/,
  336. 'operator': /^(?:\+|(?:[\-=<>]|<<|>>|\\approx|\$\\approx\$)(?=\s|$|-?[0-9]))/,
  337. 'arrowUpDown': /^(?:v|\(v\)|\^|\(\^\))(?=$|[\s,;\)\]\}])/,
  338. '\\bond{(...)}': function bond(input) {
  339. return mhchemParser.patterns.findObserveGroups(input, "\\bond{", "", "", "}");
  340. },
  341. '->': /^(?:<->|<-->|->|<-|<=>>|<<=>|<=>|[\u2192\u27F6\u21CC])/,
  342. 'CMT': /^[CMT](?=\[)/,
  343. '[(...)]': function _(input) {
  344. return mhchemParser.patterns.findObserveGroups(input, "[", "", "", "]");
  345. },
  346. '1st-level escape': /^(&|\\\\|\\hline)\s*/,
  347. '\\,': /^(?:\\[,\ ;:])/,
  348. // \\x - but output no space before
  349. '\\x{}{}': function x(input) {
  350. return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true);
  351. },
  352. '\\x{}': function x(input) {
  353. return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", "");
  354. },
  355. '\\ca': /^\\ca(?:\s+|(?![a-zA-Z]))/,
  356. '\\x': /^(?:\\[a-zA-Z]+\s*|\\[_&{}%])/,
  357. 'orbital': /^(?:[0-9]{1,2}[spdfgh]|[0-9]{0,2}sp)(?=$|[^a-zA-Z])/,
  358. // only those with numbers in front, because the others will be formatted correctly anyway
  359. 'others': /^[\/~|]/,
  360. '\\frac{(...)}': function frac(input) {
  361. return mhchemParser.patterns.findObserveGroups(input, "\\frac{", "", "", "}", "{", "", "", "}");
  362. },
  363. '\\overset{(...)}': function overset(input) {
  364. return mhchemParser.patterns.findObserveGroups(input, "\\overset{", "", "", "}", "{", "", "", "}");
  365. },
  366. '\\underset{(...)}': function underset(input) {
  367. return mhchemParser.patterns.findObserveGroups(input, "\\underset{", "", "", "}", "{", "", "", "}");
  368. },
  369. '\\underbrace{(...)}': function underbrace(input) {
  370. return mhchemParser.patterns.findObserveGroups(input, "\\underbrace{", "", "", "}_", "{", "", "", "}");
  371. },
  372. '\\color{(...)}0': function color0(input) {
  373. return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}");
  374. },
  375. '\\color{(...)}{(...)}1': function color1(input) {
  376. return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}", "{", "", "", "}");
  377. },
  378. '\\color(...){(...)}2': function color2(input) {
  379. return mhchemParser.patterns.findObserveGroups(input, "\\color", "\\", "", /^(?=\{)/, "{", "", "", "}");
  380. },
  381. '\\ce{(...)}': function ce(input) {
  382. return mhchemParser.patterns.findObserveGroups(input, "\\ce{", "", "", "}");
  383. },
  384. 'oxidation$': /^(?:[+-][IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/,
  385. 'd-oxidation$': /^(?:[+-]?\s?[IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/,
  386. // 0 could be oxidation or charge
  387. 'roman numeral': /^[IVX]+/,
  388. '1/2$': /^[+\-]?(?:[0-9]+|\$[a-z]\$|[a-z])\/[0-9]+(?:\$[a-z]\$|[a-z])?$/,
  389. 'amount': function amount(input) {
  390. var match; // e.g. 2, 0.5, 1/2, -2, n/2, +; $a$ could be added later in parsing
  391. match = input.match(/^(?:(?:(?:\([+\-]?[0-9]+\/[0-9]+\)|[+\-]?(?:[0-9]+|\$[a-z]\$|[a-z])\/[0-9]+|[+\-]?[0-9]+[.,][0-9]+|[+\-]?\.[0-9]+|[+\-]?[0-9]+)(?:[a-z](?=\s*[A-Z]))?)|[+\-]?[a-z](?=\s*[A-Z])|\+(?!\s))/);
  392. if (match) {
  393. return {
  394. match_: match[0],
  395. remainder: input.substr(match[0].length)
  396. };
  397. }
  398. var a = mhchemParser.patterns.findObserveGroups(input, "", "$", "$", "");
  399. if (a) {
  400. // e.g. $2n-1$, $-$
  401. match = a.match_.match(/^\$(?:\(?[+\-]?(?:[0-9]*[a-z]?[+\-])?[0-9]*[a-z](?:[+\-][0-9]*[a-z]?)?\)?|\+|-)\$$/);
  402. if (match) {
  403. return {
  404. match_: match[0],
  405. remainder: input.substr(match[0].length)
  406. };
  407. }
  408. }
  409. return null;
  410. },
  411. 'amount2': function amount2(input) {
  412. return this['amount'](input);
  413. },
  414. '(KV letters),': /^(?:[A-Z][a-z]{0,2}|i)(?=,)/,
  415. 'formula$': function formula$(input) {
  416. if (input.match(/^\([a-z]+\)$/)) {
  417. return null;
  418. } // state of aggregation = no formula
  419. var match = input.match(/^(?:[a-z]|(?:[0-9\ \+\-\,\.\(\)]+[a-z])+[0-9\ \+\-\,\.\(\)]*|(?:[a-z][0-9\ \+\-\,\.\(\)]+)+[a-z]?)$/);
  420. if (match) {
  421. return {
  422. match_: match[0],
  423. remainder: input.substr(match[0].length)
  424. };
  425. }
  426. return null;
  427. },
  428. 'uprightEntities': /^(?:pH|pOH|pC|pK|iPr|iBu)(?=$|[^a-zA-Z])/,
  429. '/': /^\s*(\/)\s*/,
  430. '//': /^\s*(\/\/)\s*/,
  431. '*': /^\s*[*.]\s*/
  432. },
  433. findObserveGroups: function findObserveGroups(input, begExcl, begIncl, endIncl, endExcl, beg2Excl, beg2Incl, end2Incl, end2Excl, combine) {
  434. /** @type {{(input: string, pattern: string | RegExp): string | string[] | null;}} */
  435. var _match = function _match(input, pattern) {
  436. if (typeof pattern === "string") {
  437. if (input.indexOf(pattern) !== 0) {
  438. return null;
  439. }
  440. return pattern;
  441. } else {
  442. var match = input.match(pattern);
  443. if (!match) {
  444. return null;
  445. }
  446. return match[0];
  447. }
  448. };
  449. /** @type {{(input: string, i: number, endChars: string | RegExp): {endMatchBegin: number, endMatchEnd: number} | null;}} */
  450. var _findObserveGroups = function _findObserveGroups(input, i, endChars) {
  451. var braces = 0;
  452. while (i < input.length) {
  453. var a = input.charAt(i);
  454. var match = _match(input.substr(i), endChars);
  455. if (match !== null && braces === 0) {
  456. return {
  457. endMatchBegin: i,
  458. endMatchEnd: i + match.length
  459. };
  460. } else if (a === "{") {
  461. braces++;
  462. } else if (a === "}") {
  463. if (braces === 0) {
  464. throw ["ExtraCloseMissingOpen", "Extra close brace or missing open brace"];
  465. } else {
  466. braces--;
  467. }
  468. }
  469. i++;
  470. }
  471. if (braces > 0) {
  472. return null;
  473. }
  474. return null;
  475. };
  476. var match = _match(input, begExcl);
  477. if (match === null) {
  478. return null;
  479. }
  480. input = input.substr(match.length);
  481. match = _match(input, begIncl);
  482. if (match === null) {
  483. return null;
  484. }
  485. var e = _findObserveGroups(input, match.length, endIncl || endExcl);
  486. if (e === null) {
  487. return null;
  488. }
  489. var match1 = input.substring(0, endIncl ? e.endMatchEnd : e.endMatchBegin);
  490. if (!(beg2Excl || beg2Incl)) {
  491. return {
  492. match_: match1,
  493. remainder: input.substr(e.endMatchEnd)
  494. };
  495. } else {
  496. var group2 = this.findObserveGroups(input.substr(e.endMatchEnd), beg2Excl, beg2Incl, end2Incl, end2Excl);
  497. if (group2 === null) {
  498. return null;
  499. }
  500. /** @type {string[]} */
  501. var matchRet = [match1, group2.match_];
  502. return {
  503. match_: combine ? matchRet.join("") : matchRet,
  504. remainder: group2.remainder
  505. };
  506. }
  507. },
  508. //
  509. // Matching function
  510. // e.g. match("a", input) will look for the regexp called "a" and see if it matches
  511. // returns null or {match_:"a", remainder:"bc"}
  512. //
  513. match_: function match_(m, input) {
  514. var pattern = mhchemParser.patterns.patterns[m];
  515. if (pattern === undefined) {
  516. throw ["MhchemBugP", "mhchem bug P. Please report. (" + m + ")"]; // Trying to use non-existing pattern
  517. } else if (typeof pattern === "function") {
  518. return mhchemParser.patterns.patterns[m](input); // cannot use cached var pattern here, because some pattern functions need this===mhchemParser
  519. } else {
  520. // RegExp
  521. var match = input.match(pattern);
  522. if (match) {
  523. var mm;
  524. if (match[2]) {
  525. mm = [match[1], match[2]];
  526. } else if (match[1]) {
  527. mm = match[1];
  528. } else {
  529. mm = match[0];
  530. }
  531. return {
  532. match_: mm,
  533. remainder: input.substr(match[0].length)
  534. };
  535. }
  536. return null;
  537. }
  538. }
  539. },
  540. //
  541. // Generic state machine actions
  542. //
  543. actions: {
  544. 'a=': function a(buffer, m) {
  545. buffer.a = (buffer.a || "") + m;
  546. },
  547. 'b=': function b(buffer, m) {
  548. buffer.b = (buffer.b || "") + m;
  549. },
  550. 'p=': function p(buffer, m) {
  551. buffer.p = (buffer.p || "") + m;
  552. },
  553. 'o=': function o(buffer, m) {
  554. buffer.o = (buffer.o || "") + m;
  555. },
  556. 'q=': function q(buffer, m) {
  557. buffer.q = (buffer.q || "") + m;
  558. },
  559. 'd=': function d(buffer, m) {
  560. buffer.d = (buffer.d || "") + m;
  561. },
  562. 'rm=': function rm(buffer, m) {
  563. buffer.rm = (buffer.rm || "") + m;
  564. },
  565. 'text=': function text(buffer, m) {
  566. buffer.text_ = (buffer.text_ || "") + m;
  567. },
  568. 'insert': function insert(buffer, m, a) {
  569. return {
  570. type_: a
  571. };
  572. },
  573. 'insert+p1': function insertP1(buffer, m, a) {
  574. return {
  575. type_: a,
  576. p1: m
  577. };
  578. },
  579. 'insert+p1+p2': function insertP1P2(buffer, m, a) {
  580. return {
  581. type_: a,
  582. p1: m[0],
  583. p2: m[1]
  584. };
  585. },
  586. 'copy': function copy(buffer, m) {
  587. return m;
  588. },
  589. 'rm': function rm(buffer, m) {
  590. return {
  591. type_: 'rm',
  592. p1: m || ""
  593. };
  594. },
  595. 'text': function text(buffer, m) {
  596. return mhchemParser.go(m, 'text');
  597. },
  598. '{text}': function text(buffer, m) {
  599. var ret = ["{"];
  600. mhchemParser.concatArray(ret, mhchemParser.go(m, 'text'));
  601. ret.push("}");
  602. return ret;
  603. },
  604. 'tex-math': function texMath(buffer, m) {
  605. return mhchemParser.go(m, 'tex-math');
  606. },
  607. 'tex-math tight': function texMathTight(buffer, m) {
  608. return mhchemParser.go(m, 'tex-math tight');
  609. },
  610. 'bond': function bond(buffer, m, k) {
  611. return {
  612. type_: 'bond',
  613. kind_: k || m
  614. };
  615. },
  616. 'color0-output': function color0Output(buffer, m) {
  617. return {
  618. type_: 'color0',
  619. color: m[0]
  620. };
  621. },
  622. 'ce': function ce(buffer, m) {
  623. return mhchemParser.go(m);
  624. },
  625. '1/2': function _(buffer, m) {
  626. /** @type {ParserOutput[]} */
  627. var ret = [];
  628. if (m.match(/^[+\-]/)) {
  629. ret.push(m.substr(0, 1));
  630. m = m.substr(1);
  631. }
  632. var n = m.match(/^([0-9]+|\$[a-z]\$|[a-z])\/([0-9]+)(\$[a-z]\$|[a-z])?$/);
  633. n[1] = n[1].replace(/\$/g, "");
  634. ret.push({
  635. type_: 'frac',
  636. p1: n[1],
  637. p2: n[2]
  638. });
  639. if (n[3]) {
  640. n[3] = n[3].replace(/\$/g, "");
  641. ret.push({
  642. type_: 'tex-math',
  643. p1: n[3]
  644. });
  645. }
  646. return ret;
  647. },
  648. '9,9': function _(buffer, m) {
  649. return mhchemParser.go(m, '9,9');
  650. }
  651. },
  652. //
  653. // createTransitions
  654. // convert { 'letter': { 'state': { action_: 'output' } } } to { 'state' => [ { pattern: 'letter', task: { action_: [{type_: 'output'}] } } ] }
  655. // with expansion of 'a|b' to 'a' and 'b' (at 2 places)
  656. //
  657. createTransitions: function createTransitions(o) {
  658. var pattern, state;
  659. /** @type {string[]} */
  660. var stateArray;
  661. var i; //
  662. // 1. Collect all states
  663. //
  664. /** @type {Transitions} */
  665. var transitions = {};
  666. for (pattern in o) {
  667. for (state in o[pattern]) {
  668. stateArray = state.split("|");
  669. o[pattern][state].stateArray = stateArray;
  670. for (i = 0; i < stateArray.length; i++) {
  671. transitions[stateArray[i]] = [];
  672. }
  673. }
  674. } //
  675. // 2. Fill states
  676. //
  677. for (pattern in o) {
  678. for (state in o[pattern]) {
  679. stateArray = o[pattern][state].stateArray || [];
  680. for (i = 0; i < stateArray.length; i++) {
  681. //
  682. // 2a. Normalize actions into array: 'text=' ==> [{type_:'text='}]
  683. // (Note to myself: Resolving the function here would be problematic. It would need .bind (for *this*) and currying (for *option*).)
  684. //
  685. /** @type {any} */
  686. var p = o[pattern][state];
  687. if (p.action_) {
  688. p.action_ = [].concat(p.action_);
  689. for (var k = 0; k < p.action_.length; k++) {
  690. if (typeof p.action_[k] === "string") {
  691. p.action_[k] = {
  692. type_: p.action_[k]
  693. };
  694. }
  695. }
  696. } else {
  697. p.action_ = [];
  698. } //
  699. // 2.b Multi-insert
  700. //
  701. var patternArray = pattern.split("|");
  702. for (var j = 0; j < patternArray.length; j++) {
  703. if (stateArray[i] === '*') {
  704. // insert into all
  705. for (var t in transitions) {
  706. transitions[t].push({
  707. pattern: patternArray[j],
  708. task: p
  709. });
  710. }
  711. } else {
  712. transitions[stateArray[i]].push({
  713. pattern: patternArray[j],
  714. task: p
  715. });
  716. }
  717. }
  718. }
  719. }
  720. }
  721. return transitions;
  722. },
  723. stateMachines: {}
  724. }; //
  725. // Definition of state machines
  726. //
  727. mhchemParser.stateMachines = {
  728. //
  729. // \ce state machines
  730. //
  731. //#region ce
  732. 'ce': {
  733. // main parser
  734. transitions: mhchemParser.createTransitions({
  735. 'empty': {
  736. '*': {
  737. action_: 'output'
  738. }
  739. },
  740. 'else': {
  741. '0|1|2': {
  742. action_: 'beginsWithBond=false',
  743. revisit: true,
  744. toContinue: true
  745. }
  746. },
  747. 'oxidation$': {
  748. '0': {
  749. action_: 'oxidation-output'
  750. }
  751. },
  752. 'CMT': {
  753. 'r': {
  754. action_: 'rdt=',
  755. nextState: 'rt'
  756. },
  757. 'rd': {
  758. action_: 'rqt=',
  759. nextState: 'rdt'
  760. }
  761. },
  762. 'arrowUpDown': {
  763. '0|1|2|as': {
  764. action_: ['sb=false', 'output', 'operator'],
  765. nextState: '1'
  766. }
  767. },
  768. 'uprightEntities': {
  769. '0|1|2': {
  770. action_: ['o=', 'output'],
  771. nextState: '1'
  772. }
  773. },
  774. 'orbital': {
  775. '0|1|2|3': {
  776. action_: 'o=',
  777. nextState: 'o'
  778. }
  779. },
  780. '->': {
  781. '0|1|2|3': {
  782. action_: 'r=',
  783. nextState: 'r'
  784. },
  785. 'a|as': {
  786. action_: ['output', 'r='],
  787. nextState: 'r'
  788. },
  789. '*': {
  790. action_: ['output', 'r='],
  791. nextState: 'r'
  792. }
  793. },
  794. '+': {
  795. 'o': {
  796. action_: 'd= kv',
  797. nextState: 'd'
  798. },
  799. 'd|D': {
  800. action_: 'd=',
  801. nextState: 'd'
  802. },
  803. 'q': {
  804. action_: 'd=',
  805. nextState: 'qd'
  806. },
  807. 'qd|qD': {
  808. action_: 'd=',
  809. nextState: 'qd'
  810. },
  811. 'dq': {
  812. action_: ['output', 'd='],
  813. nextState: 'd'
  814. },
  815. '3': {
  816. action_: ['sb=false', 'output', 'operator'],
  817. nextState: '0'
  818. }
  819. },
  820. 'amount': {
  821. '0|2': {
  822. action_: 'a=',
  823. nextState: 'a'
  824. }
  825. },
  826. 'pm-operator': {
  827. '0|1|2|a|as': {
  828. action_: ['sb=false', 'output', {
  829. type_: 'operator',
  830. option: '\\pm'
  831. }],
  832. nextState: '0'
  833. }
  834. },
  835. 'operator': {
  836. '0|1|2|a|as': {
  837. action_: ['sb=false', 'output', 'operator'],
  838. nextState: '0'
  839. }
  840. },
  841. '-$': {
  842. 'o|q': {
  843. action_: ['charge or bond', 'output'],
  844. nextState: 'qd'
  845. },
  846. 'd': {
  847. action_: 'd=',
  848. nextState: 'd'
  849. },
  850. 'D': {
  851. action_: ['output', {
  852. type_: 'bond',
  853. option: "-"
  854. }],
  855. nextState: '3'
  856. },
  857. 'q': {
  858. action_: 'd=',
  859. nextState: 'qd'
  860. },
  861. 'qd': {
  862. action_: 'd=',
  863. nextState: 'qd'
  864. },
  865. 'qD|dq': {
  866. action_: ['output', {
  867. type_: 'bond',
  868. option: "-"
  869. }],
  870. nextState: '3'
  871. }
  872. },
  873. '-9': {
  874. '3|o': {
  875. action_: ['output', {
  876. type_: 'insert',
  877. option: 'hyphen'
  878. }],
  879. nextState: '3'
  880. }
  881. },
  882. '- orbital overlap': {
  883. 'o': {
  884. action_: ['output', {
  885. type_: 'insert',
  886. option: 'hyphen'
  887. }],
  888. nextState: '2'
  889. },
  890. 'd': {
  891. action_: ['output', {
  892. type_: 'insert',
  893. option: 'hyphen'
  894. }],
  895. nextState: '2'
  896. }
  897. },
  898. '-': {
  899. '0|1|2': {
  900. action_: [{
  901. type_: 'output',
  902. option: 1
  903. }, 'beginsWithBond=true', {
  904. type_: 'bond',
  905. option: "-"
  906. }],
  907. nextState: '3'
  908. },
  909. '3': {
  910. action_: {
  911. type_: 'bond',
  912. option: "-"
  913. }
  914. },
  915. 'a': {
  916. action_: ['output', {
  917. type_: 'insert',
  918. option: 'hyphen'
  919. }],
  920. nextState: '2'
  921. },
  922. 'as': {
  923. action_: [{
  924. type_: 'output',
  925. option: 2
  926. }, {
  927. type_: 'bond',
  928. option: "-"
  929. }],
  930. nextState: '3'
  931. },
  932. 'b': {
  933. action_: 'b='
  934. },
  935. 'o': {
  936. action_: {
  937. type_: '- after o/d',
  938. option: false
  939. },
  940. nextState: '2'
  941. },
  942. 'q': {
  943. action_: {
  944. type_: '- after o/d',
  945. option: false
  946. },
  947. nextState: '2'
  948. },
  949. 'd|qd|dq': {
  950. action_: {
  951. type_: '- after o/d',
  952. option: true
  953. },
  954. nextState: '2'
  955. },
  956. 'D|qD|p': {
  957. action_: ['output', {
  958. type_: 'bond',
  959. option: "-"
  960. }],
  961. nextState: '3'
  962. }
  963. },
  964. 'amount2': {
  965. '1|3': {
  966. action_: 'a=',
  967. nextState: 'a'
  968. }
  969. },
  970. 'letters': {
  971. '0|1|2|3|a|as|b|p|bp|o': {
  972. action_: 'o=',
  973. nextState: 'o'
  974. },
  975. 'q|dq': {
  976. action_: ['output', 'o='],
  977. nextState: 'o'
  978. },
  979. 'd|D|qd|qD': {
  980. action_: 'o after d',
  981. nextState: 'o'
  982. }
  983. },
  984. 'digits': {
  985. 'o': {
  986. action_: 'q=',
  987. nextState: 'q'
  988. },
  989. 'd|D': {
  990. action_: 'q=',
  991. nextState: 'dq'
  992. },
  993. 'q': {
  994. action_: ['output', 'o='],
  995. nextState: 'o'
  996. },
  997. 'a': {
  998. action_: 'o=',
  999. nextState: 'o'
  1000. }
  1001. },
  1002. 'space A': {
  1003. 'b|p|bp': {}
  1004. },
  1005. 'space': {
  1006. 'a': {
  1007. nextState: 'as'
  1008. },
  1009. '0': {
  1010. action_: 'sb=false'
  1011. },
  1012. '1|2': {
  1013. action_: 'sb=true'
  1014. },
  1015. 'r|rt|rd|rdt|rdq': {
  1016. action_: 'output',
  1017. nextState: '0'
  1018. },
  1019. '*': {
  1020. action_: ['output', 'sb=true'],
  1021. nextState: '1'
  1022. }
  1023. },
  1024. '1st-level escape': {
  1025. '1|2': {
  1026. action_: ['output', {
  1027. type_: 'insert+p1',
  1028. option: '1st-level escape'
  1029. }]
  1030. },
  1031. '*': {
  1032. action_: ['output', {
  1033. type_: 'insert+p1',
  1034. option: '1st-level escape'
  1035. }],
  1036. nextState: '0'
  1037. }
  1038. },
  1039. '[(...)]': {
  1040. 'r|rt': {
  1041. action_: 'rd=',
  1042. nextState: 'rd'
  1043. },
  1044. 'rd|rdt': {
  1045. action_: 'rq=',
  1046. nextState: 'rdq'
  1047. }
  1048. },
  1049. '...': {
  1050. 'o|d|D|dq|qd|qD': {
  1051. action_: ['output', {
  1052. type_: 'bond',
  1053. option: "..."
  1054. }],
  1055. nextState: '3'
  1056. },
  1057. '*': {
  1058. action_: [{
  1059. type_: 'output',
  1060. option: 1
  1061. }, {
  1062. type_: 'insert',
  1063. option: 'ellipsis'
  1064. }],
  1065. nextState: '1'
  1066. }
  1067. },
  1068. '. |* ': {
  1069. '*': {
  1070. action_: ['output', {
  1071. type_: 'insert',
  1072. option: 'addition compound'
  1073. }],
  1074. nextState: '1'
  1075. }
  1076. },
  1077. 'state of aggregation $': {
  1078. '*': {
  1079. action_: ['output', 'state of aggregation'],
  1080. nextState: '1'
  1081. }
  1082. },
  1083. '{[(': {
  1084. 'a|as|o': {
  1085. action_: ['o=', 'output', 'parenthesisLevel++'],
  1086. nextState: '2'
  1087. },
  1088. '0|1|2|3': {
  1089. action_: ['o=', 'output', 'parenthesisLevel++'],
  1090. nextState: '2'
  1091. },
  1092. '*': {
  1093. action_: ['output', 'o=', 'output', 'parenthesisLevel++'],
  1094. nextState: '2'
  1095. }
  1096. },
  1097. ')]}': {
  1098. '0|1|2|3|b|p|bp|o': {
  1099. action_: ['o=', 'parenthesisLevel--'],
  1100. nextState: 'o'
  1101. },
  1102. 'a|as|d|D|q|qd|qD|dq': {
  1103. action_: ['output', 'o=', 'parenthesisLevel--'],
  1104. nextState: 'o'
  1105. }
  1106. },
  1107. ', ': {
  1108. '*': {
  1109. action_: ['output', 'comma'],
  1110. nextState: '0'
  1111. }
  1112. },
  1113. '^_': {
  1114. // ^ and _ without a sensible argument
  1115. '*': {}
  1116. },
  1117. '^{(...)}|^($...$)': {
  1118. '0|1|2|as': {
  1119. action_: 'b=',
  1120. nextState: 'b'
  1121. },
  1122. 'p': {
  1123. action_: 'b=',
  1124. nextState: 'bp'
  1125. },
  1126. '3|o': {
  1127. action_: 'd= kv',
  1128. nextState: 'D'
  1129. },
  1130. 'q': {
  1131. action_: 'd=',
  1132. nextState: 'qD'
  1133. },
  1134. 'd|D|qd|qD|dq': {
  1135. action_: ['output', 'd='],
  1136. nextState: 'D'
  1137. }
  1138. },
  1139. '^a|^\\x{}{}|^\\x{}|^\\x|\'': {
  1140. '0|1|2|as': {
  1141. action_: 'b=',
  1142. nextState: 'b'
  1143. },
  1144. 'p': {
  1145. action_: 'b=',
  1146. nextState: 'bp'
  1147. },
  1148. '3|o': {
  1149. action_: 'd= kv',
  1150. nextState: 'd'
  1151. },
  1152. 'q': {
  1153. action_: 'd=',
  1154. nextState: 'qd'
  1155. },
  1156. 'd|qd|D|qD': {
  1157. action_: 'd='
  1158. },
  1159. 'dq': {
  1160. action_: ['output', 'd='],
  1161. nextState: 'd'
  1162. }
  1163. },
  1164. '_{(state of aggregation)}$': {
  1165. 'd|D|q|qd|qD|dq': {
  1166. action_: ['output', 'q='],
  1167. nextState: 'q'
  1168. }
  1169. },
  1170. '_{(...)}|_($...$)|_9|_\\x{}{}|_\\x{}|_\\x': {
  1171. '0|1|2|as': {
  1172. action_: 'p=',
  1173. nextState: 'p'
  1174. },
  1175. 'b': {
  1176. action_: 'p=',
  1177. nextState: 'bp'
  1178. },
  1179. '3|o': {
  1180. action_: 'q=',
  1181. nextState: 'q'
  1182. },
  1183. 'd|D': {
  1184. action_: 'q=',
  1185. nextState: 'dq'
  1186. },
  1187. 'q|qd|qD|dq': {
  1188. action_: ['output', 'q='],
  1189. nextState: 'q'
  1190. }
  1191. },
  1192. '=<>': {
  1193. '0|1|2|3|a|as|o|q|d|D|qd|qD|dq': {
  1194. action_: [{
  1195. type_: 'output',
  1196. option: 2
  1197. }, 'bond'],
  1198. nextState: '3'
  1199. }
  1200. },
  1201. '#': {
  1202. '0|1|2|3|a|as|o': {
  1203. action_: [{
  1204. type_: 'output',
  1205. option: 2
  1206. }, {
  1207. type_: 'bond',
  1208. option: "#"
  1209. }],
  1210. nextState: '3'
  1211. }
  1212. },
  1213. '{}': {
  1214. '*': {
  1215. action_: {
  1216. type_: 'output',
  1217. option: 1
  1218. },
  1219. nextState: '1'
  1220. }
  1221. },
  1222. '{...}': {
  1223. '0|1|2|3|a|as|b|p|bp': {
  1224. action_: 'o=',
  1225. nextState: 'o'
  1226. },
  1227. 'o|d|D|q|qd|qD|dq': {
  1228. action_: ['output', 'o='],
  1229. nextState: 'o'
  1230. }
  1231. },
  1232. '$...$': {
  1233. 'a': {
  1234. action_: 'a='
  1235. },
  1236. // 2$n$
  1237. '0|1|2|3|as|b|p|bp|o': {
  1238. action_: 'o=',
  1239. nextState: 'o'
  1240. },
  1241. // not 'amount'
  1242. 'as|o': {
  1243. action_: 'o='
  1244. },
  1245. 'q|d|D|qd|qD|dq': {
  1246. action_: ['output', 'o='],
  1247. nextState: 'o'
  1248. }
  1249. },
  1250. '\\bond{(...)}': {
  1251. '*': {
  1252. action_: [{
  1253. type_: 'output',
  1254. option: 2
  1255. }, 'bond'],
  1256. nextState: "3"
  1257. }
  1258. },
  1259. '\\frac{(...)}': {
  1260. '*': {
  1261. action_: [{
  1262. type_: 'output',
  1263. option: 1
  1264. }, 'frac-output'],
  1265. nextState: '3'
  1266. }
  1267. },
  1268. '\\overset{(...)}': {
  1269. '*': {
  1270. action_: [{
  1271. type_: 'output',
  1272. option: 2
  1273. }, 'overset-output'],
  1274. nextState: '3'
  1275. }
  1276. },
  1277. '\\underset{(...)}': {
  1278. '*': {
  1279. action_: [{
  1280. type_: 'output',
  1281. option: 2
  1282. }, 'underset-output'],
  1283. nextState: '3'
  1284. }
  1285. },
  1286. '\\underbrace{(...)}': {
  1287. '*': {
  1288. action_: [{
  1289. type_: 'output',
  1290. option: 2
  1291. }, 'underbrace-output'],
  1292. nextState: '3'
  1293. }
  1294. },
  1295. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  1296. '*': {
  1297. action_: [{
  1298. type_: 'output',
  1299. option: 2
  1300. }, 'color-output'],
  1301. nextState: '3'
  1302. }
  1303. },
  1304. '\\color{(...)}0': {
  1305. '*': {
  1306. action_: [{
  1307. type_: 'output',
  1308. option: 2
  1309. }, 'color0-output']
  1310. }
  1311. },
  1312. '\\ce{(...)}': {
  1313. '*': {
  1314. action_: [{
  1315. type_: 'output',
  1316. option: 2
  1317. }, 'ce'],
  1318. nextState: '3'
  1319. }
  1320. },
  1321. '\\,': {
  1322. '*': {
  1323. action_: [{
  1324. type_: 'output',
  1325. option: 1
  1326. }, 'copy'],
  1327. nextState: '1'
  1328. }
  1329. },
  1330. '\\x{}{}|\\x{}|\\x': {
  1331. '0|1|2|3|a|as|b|p|bp|o|c0': {
  1332. action_: ['o=', 'output'],
  1333. nextState: '3'
  1334. },
  1335. '*': {
  1336. action_: ['output', 'o=', 'output'],
  1337. nextState: '3'
  1338. }
  1339. },
  1340. 'others': {
  1341. '*': {
  1342. action_: [{
  1343. type_: 'output',
  1344. option: 1
  1345. }, 'copy'],
  1346. nextState: '3'
  1347. }
  1348. },
  1349. 'else2': {
  1350. 'a': {
  1351. action_: 'a to o',
  1352. nextState: 'o',
  1353. revisit: true
  1354. },
  1355. 'as': {
  1356. action_: ['output', 'sb=true'],
  1357. nextState: '1',
  1358. revisit: true
  1359. },
  1360. 'r|rt|rd|rdt|rdq': {
  1361. action_: ['output'],
  1362. nextState: '0',
  1363. revisit: true
  1364. },
  1365. '*': {
  1366. action_: ['output', 'copy'],
  1367. nextState: '3'
  1368. }
  1369. }
  1370. }),
  1371. actions: {
  1372. 'o after d': function oAfterD(buffer, m) {
  1373. var ret;
  1374. if ((buffer.d || "").match(/^[0-9]+$/)) {
  1375. var tmp = buffer.d;
  1376. buffer.d = undefined;
  1377. ret = this['output'](buffer);
  1378. buffer.b = tmp;
  1379. } else {
  1380. ret = this['output'](buffer);
  1381. }
  1382. mhchemParser.actions['o='](buffer, m);
  1383. return ret;
  1384. },
  1385. 'd= kv': function dKv(buffer, m) {
  1386. buffer.d = m;
  1387. buffer.dType = 'kv';
  1388. },
  1389. 'charge or bond': function chargeOrBond(buffer, m) {
  1390. if (buffer['beginsWithBond']) {
  1391. /** @type {ParserOutput[]} */
  1392. var ret = [];
  1393. mhchemParser.concatArray(ret, this['output'](buffer));
  1394. mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
  1395. return ret;
  1396. } else {
  1397. buffer.d = m;
  1398. }
  1399. },
  1400. '- after o/d': function afterOD(buffer, m, isAfterD) {
  1401. var c1 = mhchemParser.patterns.match_('orbital', buffer.o || "");
  1402. var c2 = mhchemParser.patterns.match_('one lowercase greek letter $', buffer.o || "");
  1403. var c3 = mhchemParser.patterns.match_('one lowercase latin letter $', buffer.o || "");
  1404. var c4 = mhchemParser.patterns.match_('$one lowercase latin letter$ $', buffer.o || "");
  1405. var hyphenFollows = m === "-" && (c1 && c1.remainder === "" || c2 || c3 || c4);
  1406. if (hyphenFollows && !buffer.a && !buffer.b && !buffer.p && !buffer.d && !buffer.q && !c1 && c3) {
  1407. buffer.o = '$' + buffer.o + '$';
  1408. }
  1409. /** @type {ParserOutput[]} */
  1410. var ret = [];
  1411. if (hyphenFollows) {
  1412. mhchemParser.concatArray(ret, this['output'](buffer));
  1413. ret.push({
  1414. type_: 'hyphen'
  1415. });
  1416. } else {
  1417. c1 = mhchemParser.patterns.match_('digits', buffer.d || "");
  1418. if (isAfterD && c1 && c1.remainder === '') {
  1419. mhchemParser.concatArray(ret, mhchemParser.actions['d='](buffer, m));
  1420. mhchemParser.concatArray(ret, this['output'](buffer));
  1421. } else {
  1422. mhchemParser.concatArray(ret, this['output'](buffer));
  1423. mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
  1424. }
  1425. }
  1426. return ret;
  1427. },
  1428. 'a to o': function aToO(buffer) {
  1429. buffer.o = buffer.a;
  1430. buffer.a = undefined;
  1431. },
  1432. 'sb=true': function sbTrue(buffer) {
  1433. buffer.sb = true;
  1434. },
  1435. 'sb=false': function sbFalse(buffer) {
  1436. buffer.sb = false;
  1437. },
  1438. 'beginsWithBond=true': function beginsWithBondTrue(buffer) {
  1439. buffer['beginsWithBond'] = true;
  1440. },
  1441. 'beginsWithBond=false': function beginsWithBondFalse(buffer) {
  1442. buffer['beginsWithBond'] = false;
  1443. },
  1444. 'parenthesisLevel++': function parenthesisLevel(buffer) {
  1445. buffer['parenthesisLevel']++;
  1446. },
  1447. 'parenthesisLevel--': function parenthesisLevel(buffer) {
  1448. buffer['parenthesisLevel']--;
  1449. },
  1450. 'state of aggregation': function stateOfAggregation(buffer, m) {
  1451. return {
  1452. type_: 'state of aggregation',
  1453. p1: mhchemParser.go(m, 'o')
  1454. };
  1455. },
  1456. 'comma': function comma(buffer, m) {
  1457. var a = m.replace(/\s*$/, '');
  1458. var withSpace = a !== m;
  1459. if (withSpace && buffer['parenthesisLevel'] === 0) {
  1460. return {
  1461. type_: 'comma enumeration L',
  1462. p1: a
  1463. };
  1464. } else {
  1465. return {
  1466. type_: 'comma enumeration M',
  1467. p1: a
  1468. };
  1469. }
  1470. },
  1471. 'output': function output(buffer, m, entityFollows) {
  1472. // entityFollows:
  1473. // undefined = if we have nothing else to output, also ignore the just read space (buffer.sb)
  1474. // 1 = an entity follows, never omit the space if there was one just read before (can only apply to state 1)
  1475. // 2 = 1 + the entity can have an amount, so output a\, instead of converting it to o (can only apply to states a|as)
  1476. /** @type {ParserOutput | ParserOutput[]} */
  1477. var ret;
  1478. if (!buffer.r) {
  1479. ret = [];
  1480. if (!buffer.a && !buffer.b && !buffer.p && !buffer.o && !buffer.q && !buffer.d && !entityFollows) ; else {
  1481. if (buffer.sb) {
  1482. ret.push({
  1483. type_: 'entitySkip'
  1484. });
  1485. }
  1486. if (!buffer.o && !buffer.q && !buffer.d && !buffer.b && !buffer.p && entityFollows !== 2) {
  1487. buffer.o = buffer.a;
  1488. buffer.a = undefined;
  1489. } else if (!buffer.o && !buffer.q && !buffer.d && (buffer.b || buffer.p)) {
  1490. buffer.o = buffer.a;
  1491. buffer.d = buffer.b;
  1492. buffer.q = buffer.p;
  1493. buffer.a = buffer.b = buffer.p = undefined;
  1494. } else {
  1495. if (buffer.o && buffer.dType === 'kv' && mhchemParser.patterns.match_('d-oxidation$', buffer.d || "")) {
  1496. buffer.dType = 'oxidation';
  1497. } else if (buffer.o && buffer.dType === 'kv' && !buffer.q) {
  1498. buffer.dType = undefined;
  1499. }
  1500. }
  1501. ret.push({
  1502. type_: 'chemfive',
  1503. a: mhchemParser.go(buffer.a, 'a'),
  1504. b: mhchemParser.go(buffer.b, 'bd'),
  1505. p: mhchemParser.go(buffer.p, 'pq'),
  1506. o: mhchemParser.go(buffer.o, 'o'),
  1507. q: mhchemParser.go(buffer.q, 'pq'),
  1508. d: mhchemParser.go(buffer.d, buffer.dType === 'oxidation' ? 'oxidation' : 'bd'),
  1509. dType: buffer.dType
  1510. });
  1511. }
  1512. } else {
  1513. // r
  1514. /** @type {ParserOutput[]} */
  1515. var rd;
  1516. if (buffer.rdt === 'M') {
  1517. rd = mhchemParser.go(buffer.rd, 'tex-math');
  1518. } else if (buffer.rdt === 'T') {
  1519. rd = [{
  1520. type_: 'text',
  1521. p1: buffer.rd || ""
  1522. }];
  1523. } else {
  1524. rd = mhchemParser.go(buffer.rd);
  1525. }
  1526. /** @type {ParserOutput[]} */
  1527. var rq;
  1528. if (buffer.rqt === 'M') {
  1529. rq = mhchemParser.go(buffer.rq, 'tex-math');
  1530. } else if (buffer.rqt === 'T') {
  1531. rq = [{
  1532. type_: 'text',
  1533. p1: buffer.rq || ""
  1534. }];
  1535. } else {
  1536. rq = mhchemParser.go(buffer.rq);
  1537. }
  1538. ret = {
  1539. type_: 'arrow',
  1540. r: buffer.r,
  1541. rd: rd,
  1542. rq: rq
  1543. };
  1544. }
  1545. for (var p in buffer) {
  1546. if (p !== 'parenthesisLevel' && p !== 'beginsWithBond') {
  1547. delete buffer[p];
  1548. }
  1549. }
  1550. return ret;
  1551. },
  1552. 'oxidation-output': function oxidationOutput(buffer, m) {
  1553. var ret = ["{"];
  1554. mhchemParser.concatArray(ret, mhchemParser.go(m, 'oxidation'));
  1555. ret.push("}");
  1556. return ret;
  1557. },
  1558. 'frac-output': function fracOutput(buffer, m) {
  1559. return {
  1560. type_: 'frac-ce',
  1561. p1: mhchemParser.go(m[0]),
  1562. p2: mhchemParser.go(m[1])
  1563. };
  1564. },
  1565. 'overset-output': function oversetOutput(buffer, m) {
  1566. return {
  1567. type_: 'overset',
  1568. p1: mhchemParser.go(m[0]),
  1569. p2: mhchemParser.go(m[1])
  1570. };
  1571. },
  1572. 'underset-output': function undersetOutput(buffer, m) {
  1573. return {
  1574. type_: 'underset',
  1575. p1: mhchemParser.go(m[0]),
  1576. p2: mhchemParser.go(m[1])
  1577. };
  1578. },
  1579. 'underbrace-output': function underbraceOutput(buffer, m) {
  1580. return {
  1581. type_: 'underbrace',
  1582. p1: mhchemParser.go(m[0]),
  1583. p2: mhchemParser.go(m[1])
  1584. };
  1585. },
  1586. 'color-output': function colorOutput(buffer, m) {
  1587. return {
  1588. type_: 'color',
  1589. color1: m[0],
  1590. color2: mhchemParser.go(m[1])
  1591. };
  1592. },
  1593. 'r=': function r(buffer, m) {
  1594. buffer.r = m;
  1595. },
  1596. 'rdt=': function rdt(buffer, m) {
  1597. buffer.rdt = m;
  1598. },
  1599. 'rd=': function rd(buffer, m) {
  1600. buffer.rd = m;
  1601. },
  1602. 'rqt=': function rqt(buffer, m) {
  1603. buffer.rqt = m;
  1604. },
  1605. 'rq=': function rq(buffer, m) {
  1606. buffer.rq = m;
  1607. },
  1608. 'operator': function operator(buffer, m, p1) {
  1609. return {
  1610. type_: 'operator',
  1611. kind_: p1 || m
  1612. };
  1613. }
  1614. }
  1615. },
  1616. 'a': {
  1617. transitions: mhchemParser.createTransitions({
  1618. 'empty': {
  1619. '*': {}
  1620. },
  1621. '1/2$': {
  1622. '0': {
  1623. action_: '1/2'
  1624. }
  1625. },
  1626. 'else': {
  1627. '0': {
  1628. nextState: '1',
  1629. revisit: true
  1630. }
  1631. },
  1632. '$(...)$': {
  1633. '*': {
  1634. action_: 'tex-math tight',
  1635. nextState: '1'
  1636. }
  1637. },
  1638. ',': {
  1639. '*': {
  1640. action_: {
  1641. type_: 'insert',
  1642. option: 'commaDecimal'
  1643. }
  1644. }
  1645. },
  1646. 'else2': {
  1647. '*': {
  1648. action_: 'copy'
  1649. }
  1650. }
  1651. }),
  1652. actions: {}
  1653. },
  1654. 'o': {
  1655. transitions: mhchemParser.createTransitions({
  1656. 'empty': {
  1657. '*': {}
  1658. },
  1659. '1/2$': {
  1660. '0': {
  1661. action_: '1/2'
  1662. }
  1663. },
  1664. 'else': {
  1665. '0': {
  1666. nextState: '1',
  1667. revisit: true
  1668. }
  1669. },
  1670. 'letters': {
  1671. '*': {
  1672. action_: 'rm'
  1673. }
  1674. },
  1675. '\\ca': {
  1676. '*': {
  1677. action_: {
  1678. type_: 'insert',
  1679. option: 'circa'
  1680. }
  1681. }
  1682. },
  1683. '\\x{}{}|\\x{}|\\x': {
  1684. '*': {
  1685. action_: 'copy'
  1686. }
  1687. },
  1688. '${(...)}$|$(...)$': {
  1689. '*': {
  1690. action_: 'tex-math'
  1691. }
  1692. },
  1693. '{(...)}': {
  1694. '*': {
  1695. action_: '{text}'
  1696. }
  1697. },
  1698. 'else2': {
  1699. '*': {
  1700. action_: 'copy'
  1701. }
  1702. }
  1703. }),
  1704. actions: {}
  1705. },
  1706. 'text': {
  1707. transitions: mhchemParser.createTransitions({
  1708. 'empty': {
  1709. '*': {
  1710. action_: 'output'
  1711. }
  1712. },
  1713. '{...}': {
  1714. '*': {
  1715. action_: 'text='
  1716. }
  1717. },
  1718. '${(...)}$|$(...)$': {
  1719. '*': {
  1720. action_: 'tex-math'
  1721. }
  1722. },
  1723. '\\greek': {
  1724. '*': {
  1725. action_: ['output', 'rm']
  1726. }
  1727. },
  1728. '\\,|\\x{}{}|\\x{}|\\x': {
  1729. '*': {
  1730. action_: ['output', 'copy']
  1731. }
  1732. },
  1733. 'else': {
  1734. '*': {
  1735. action_: 'text='
  1736. }
  1737. }
  1738. }),
  1739. actions: {
  1740. 'output': function output(buffer) {
  1741. if (buffer.text_) {
  1742. /** @type {ParserOutput} */
  1743. var ret = {
  1744. type_: 'text',
  1745. p1: buffer.text_
  1746. };
  1747. for (var p in buffer) {
  1748. delete buffer[p];
  1749. }
  1750. return ret;
  1751. }
  1752. }
  1753. }
  1754. },
  1755. 'pq': {
  1756. transitions: mhchemParser.createTransitions({
  1757. 'empty': {
  1758. '*': {}
  1759. },
  1760. 'state of aggregation $': {
  1761. '*': {
  1762. action_: 'state of aggregation'
  1763. }
  1764. },
  1765. 'i$': {
  1766. '0': {
  1767. nextState: '!f',
  1768. revisit: true
  1769. }
  1770. },
  1771. '(KV letters),': {
  1772. '0': {
  1773. action_: 'rm',
  1774. nextState: '0'
  1775. }
  1776. },
  1777. 'formula$': {
  1778. '0': {
  1779. nextState: 'f',
  1780. revisit: true
  1781. }
  1782. },
  1783. '1/2$': {
  1784. '0': {
  1785. action_: '1/2'
  1786. }
  1787. },
  1788. 'else': {
  1789. '0': {
  1790. nextState: '!f',
  1791. revisit: true
  1792. }
  1793. },
  1794. '${(...)}$|$(...)$': {
  1795. '*': {
  1796. action_: 'tex-math'
  1797. }
  1798. },
  1799. '{(...)}': {
  1800. '*': {
  1801. action_: 'text'
  1802. }
  1803. },
  1804. 'a-z': {
  1805. 'f': {
  1806. action_: 'tex-math'
  1807. }
  1808. },
  1809. 'letters': {
  1810. '*': {
  1811. action_: 'rm'
  1812. }
  1813. },
  1814. '-9.,9': {
  1815. '*': {
  1816. action_: '9,9'
  1817. }
  1818. },
  1819. ',': {
  1820. '*': {
  1821. action_: {
  1822. type_: 'insert+p1',
  1823. option: 'comma enumeration S'
  1824. }
  1825. }
  1826. },
  1827. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  1828. '*': {
  1829. action_: 'color-output'
  1830. }
  1831. },
  1832. '\\color{(...)}0': {
  1833. '*': {
  1834. action_: 'color0-output'
  1835. }
  1836. },
  1837. '\\ce{(...)}': {
  1838. '*': {
  1839. action_: 'ce'
  1840. }
  1841. },
  1842. '\\,|\\x{}{}|\\x{}|\\x': {
  1843. '*': {
  1844. action_: 'copy'
  1845. }
  1846. },
  1847. 'else2': {
  1848. '*': {
  1849. action_: 'copy'
  1850. }
  1851. }
  1852. }),
  1853. actions: {
  1854. 'state of aggregation': function stateOfAggregation(buffer, m) {
  1855. return {
  1856. type_: 'state of aggregation subscript',
  1857. p1: mhchemParser.go(m, 'o')
  1858. };
  1859. },
  1860. 'color-output': function colorOutput(buffer, m) {
  1861. return {
  1862. type_: 'color',
  1863. color1: m[0],
  1864. color2: mhchemParser.go(m[1], 'pq')
  1865. };
  1866. }
  1867. }
  1868. },
  1869. 'bd': {
  1870. transitions: mhchemParser.createTransitions({
  1871. 'empty': {
  1872. '*': {}
  1873. },
  1874. 'x$': {
  1875. '0': {
  1876. nextState: '!f',
  1877. revisit: true
  1878. }
  1879. },
  1880. 'formula$': {
  1881. '0': {
  1882. nextState: 'f',
  1883. revisit: true
  1884. }
  1885. },
  1886. 'else': {
  1887. '0': {
  1888. nextState: '!f',
  1889. revisit: true
  1890. }
  1891. },
  1892. '-9.,9 no missing 0': {
  1893. '*': {
  1894. action_: '9,9'
  1895. }
  1896. },
  1897. '.': {
  1898. '*': {
  1899. action_: {
  1900. type_: 'insert',
  1901. option: 'electron dot'
  1902. }
  1903. }
  1904. },
  1905. 'a-z': {
  1906. 'f': {
  1907. action_: 'tex-math'
  1908. }
  1909. },
  1910. 'x': {
  1911. '*': {
  1912. action_: {
  1913. type_: 'insert',
  1914. option: 'KV x'
  1915. }
  1916. }
  1917. },
  1918. 'letters': {
  1919. '*': {
  1920. action_: 'rm'
  1921. }
  1922. },
  1923. '\'': {
  1924. '*': {
  1925. action_: {
  1926. type_: 'insert',
  1927. option: 'prime'
  1928. }
  1929. }
  1930. },
  1931. '${(...)}$|$(...)$': {
  1932. '*': {
  1933. action_: 'tex-math'
  1934. }
  1935. },
  1936. '{(...)}': {
  1937. '*': {
  1938. action_: 'text'
  1939. }
  1940. },
  1941. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  1942. '*': {
  1943. action_: 'color-output'
  1944. }
  1945. },
  1946. '\\color{(...)}0': {
  1947. '*': {
  1948. action_: 'color0-output'
  1949. }
  1950. },
  1951. '\\ce{(...)}': {
  1952. '*': {
  1953. action_: 'ce'
  1954. }
  1955. },
  1956. '\\,|\\x{}{}|\\x{}|\\x': {
  1957. '*': {
  1958. action_: 'copy'
  1959. }
  1960. },
  1961. 'else2': {
  1962. '*': {
  1963. action_: 'copy'
  1964. }
  1965. }
  1966. }),
  1967. actions: {
  1968. 'color-output': function colorOutput(buffer, m) {
  1969. return {
  1970. type_: 'color',
  1971. color1: m[0],
  1972. color2: mhchemParser.go(m[1], 'bd')
  1973. };
  1974. }
  1975. }
  1976. },
  1977. 'oxidation': {
  1978. transitions: mhchemParser.createTransitions({
  1979. 'empty': {
  1980. '*': {}
  1981. },
  1982. 'roman numeral': {
  1983. '*': {
  1984. action_: 'roman-numeral'
  1985. }
  1986. },
  1987. '${(...)}$|$(...)$': {
  1988. '*': {
  1989. action_: 'tex-math'
  1990. }
  1991. },
  1992. 'else': {
  1993. '*': {
  1994. action_: 'copy'
  1995. }
  1996. }
  1997. }),
  1998. actions: {
  1999. 'roman-numeral': function romanNumeral(buffer, m) {
  2000. return {
  2001. type_: 'roman numeral',
  2002. p1: m || ""
  2003. };
  2004. }
  2005. }
  2006. },
  2007. 'tex-math': {
  2008. transitions: mhchemParser.createTransitions({
  2009. 'empty': {
  2010. '*': {
  2011. action_: 'output'
  2012. }
  2013. },
  2014. '\\ce{(...)}': {
  2015. '*': {
  2016. action_: ['output', 'ce']
  2017. }
  2018. },
  2019. '{...}|\\,|\\x{}{}|\\x{}|\\x': {
  2020. '*': {
  2021. action_: 'o='
  2022. }
  2023. },
  2024. 'else': {
  2025. '*': {
  2026. action_: 'o='
  2027. }
  2028. }
  2029. }),
  2030. actions: {
  2031. 'output': function output(buffer) {
  2032. if (buffer.o) {
  2033. /** @type {ParserOutput} */
  2034. var ret = {
  2035. type_: 'tex-math',
  2036. p1: buffer.o
  2037. };
  2038. for (var p in buffer) {
  2039. delete buffer[p];
  2040. }
  2041. return ret;
  2042. }
  2043. }
  2044. }
  2045. },
  2046. 'tex-math tight': {
  2047. transitions: mhchemParser.createTransitions({
  2048. 'empty': {
  2049. '*': {
  2050. action_: 'output'
  2051. }
  2052. },
  2053. '\\ce{(...)}': {
  2054. '*': {
  2055. action_: ['output', 'ce']
  2056. }
  2057. },
  2058. '{...}|\\,|\\x{}{}|\\x{}|\\x': {
  2059. '*': {
  2060. action_: 'o='
  2061. }
  2062. },
  2063. '-|+': {
  2064. '*': {
  2065. action_: 'tight operator'
  2066. }
  2067. },
  2068. 'else': {
  2069. '*': {
  2070. action_: 'o='
  2071. }
  2072. }
  2073. }),
  2074. actions: {
  2075. 'tight operator': function tightOperator(buffer, m) {
  2076. buffer.o = (buffer.o || "") + "{" + m + "}";
  2077. },
  2078. 'output': function output(buffer) {
  2079. if (buffer.o) {
  2080. /** @type {ParserOutput} */
  2081. var ret = {
  2082. type_: 'tex-math',
  2083. p1: buffer.o
  2084. };
  2085. for (var p in buffer) {
  2086. delete buffer[p];
  2087. }
  2088. return ret;
  2089. }
  2090. }
  2091. }
  2092. },
  2093. '9,9': {
  2094. transitions: mhchemParser.createTransitions({
  2095. 'empty': {
  2096. '*': {}
  2097. },
  2098. ',': {
  2099. '*': {
  2100. action_: 'comma'
  2101. }
  2102. },
  2103. 'else': {
  2104. '*': {
  2105. action_: 'copy'
  2106. }
  2107. }
  2108. }),
  2109. actions: {
  2110. 'comma': function comma() {
  2111. return {
  2112. type_: 'commaDecimal'
  2113. };
  2114. }
  2115. }
  2116. },
  2117. //#endregion
  2118. //
  2119. // \pu state machines
  2120. //
  2121. //#region pu
  2122. 'pu': {
  2123. transitions: mhchemParser.createTransitions({
  2124. 'empty': {
  2125. '*': {
  2126. action_: 'output'
  2127. }
  2128. },
  2129. 'space$': {
  2130. '*': {
  2131. action_: ['output', 'space']
  2132. }
  2133. },
  2134. '{[(|)]}': {
  2135. '0|a': {
  2136. action_: 'copy'
  2137. }
  2138. },
  2139. '(-)(9)^(-9)': {
  2140. '0': {
  2141. action_: 'number^',
  2142. nextState: 'a'
  2143. }
  2144. },
  2145. '(-)(9.,9)(e)(99)': {
  2146. '0': {
  2147. action_: 'enumber',
  2148. nextState: 'a'
  2149. }
  2150. },
  2151. 'space': {
  2152. '0|a': {}
  2153. },
  2154. 'pm-operator': {
  2155. '0|a': {
  2156. action_: {
  2157. type_: 'operator',
  2158. option: '\\pm'
  2159. },
  2160. nextState: '0'
  2161. }
  2162. },
  2163. 'operator': {
  2164. '0|a': {
  2165. action_: 'copy',
  2166. nextState: '0'
  2167. }
  2168. },
  2169. '//': {
  2170. 'd': {
  2171. action_: 'o=',
  2172. nextState: '/'
  2173. }
  2174. },
  2175. '/': {
  2176. 'd': {
  2177. action_: 'o=',
  2178. nextState: '/'
  2179. }
  2180. },
  2181. '{...}|else': {
  2182. '0|d': {
  2183. action_: 'd=',
  2184. nextState: 'd'
  2185. },
  2186. 'a': {
  2187. action_: ['space', 'd='],
  2188. nextState: 'd'
  2189. },
  2190. '/|q': {
  2191. action_: 'q=',
  2192. nextState: 'q'
  2193. }
  2194. }
  2195. }),
  2196. actions: {
  2197. 'enumber': function enumber(buffer, m) {
  2198. /** @type {ParserOutput[]} */
  2199. var ret = [];
  2200. if (m[0] === "+-" || m[0] === "+/-") {
  2201. ret.push("\\pm ");
  2202. } else if (m[0]) {
  2203. ret.push(m[0]);
  2204. }
  2205. if (m[1]) {
  2206. mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
  2207. if (m[2]) {
  2208. if (m[2].match(/[,.]/)) {
  2209. mhchemParser.concatArray(ret, mhchemParser.go(m[2], 'pu-9,9'));
  2210. } else {
  2211. ret.push(m[2]);
  2212. }
  2213. }
  2214. m[3] = m[4] || m[3];
  2215. if (m[3]) {
  2216. m[3] = m[3].trim();
  2217. if (m[3] === "e" || m[3].substr(0, 1) === "*") {
  2218. ret.push({
  2219. type_: 'cdot'
  2220. });
  2221. } else {
  2222. ret.push({
  2223. type_: 'times'
  2224. });
  2225. }
  2226. }
  2227. }
  2228. if (m[3]) {
  2229. ret.push("10^{" + m[5] + "}");
  2230. }
  2231. return ret;
  2232. },
  2233. 'number^': function number(buffer, m) {
  2234. /** @type {ParserOutput[]} */
  2235. var ret = [];
  2236. if (m[0] === "+-" || m[0] === "+/-") {
  2237. ret.push("\\pm ");
  2238. } else if (m[0]) {
  2239. ret.push(m[0]);
  2240. }
  2241. mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
  2242. ret.push("^{" + m[2] + "}");
  2243. return ret;
  2244. },
  2245. 'operator': function operator(buffer, m, p1) {
  2246. return {
  2247. type_: 'operator',
  2248. kind_: p1 || m
  2249. };
  2250. },
  2251. 'space': function space() {
  2252. return {
  2253. type_: 'pu-space-1'
  2254. };
  2255. },
  2256. 'output': function output(buffer) {
  2257. /** @type {ParserOutput | ParserOutput[]} */
  2258. var ret;
  2259. var md = mhchemParser.patterns.match_('{(...)}', buffer.d || "");
  2260. if (md && md.remainder === '') {
  2261. buffer.d = md.match_;
  2262. }
  2263. var mq = mhchemParser.patterns.match_('{(...)}', buffer.q || "");
  2264. if (mq && mq.remainder === '') {
  2265. buffer.q = mq.match_;
  2266. }
  2267. if (buffer.d) {
  2268. buffer.d = buffer.d.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
  2269. buffer.d = buffer.d.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
  2270. }
  2271. if (buffer.q) {
  2272. // fraction
  2273. buffer.q = buffer.q.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
  2274. buffer.q = buffer.q.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
  2275. var b5 = {
  2276. d: mhchemParser.go(buffer.d, 'pu'),
  2277. q: mhchemParser.go(buffer.q, 'pu')
  2278. };
  2279. if (buffer.o === '//') {
  2280. ret = {
  2281. type_: 'pu-frac',
  2282. p1: b5.d,
  2283. p2: b5.q
  2284. };
  2285. } else {
  2286. ret = b5.d;
  2287. if (b5.d.length > 1 || b5.q.length > 1) {
  2288. ret.push({
  2289. type_: ' / '
  2290. });
  2291. } else {
  2292. ret.push({
  2293. type_: '/'
  2294. });
  2295. }
  2296. mhchemParser.concatArray(ret, b5.q);
  2297. }
  2298. } else {
  2299. // no fraction
  2300. ret = mhchemParser.go(buffer.d, 'pu-2');
  2301. }
  2302. for (var p in buffer) {
  2303. delete buffer[p];
  2304. }
  2305. return ret;
  2306. }
  2307. }
  2308. },
  2309. 'pu-2': {
  2310. transitions: mhchemParser.createTransitions({
  2311. 'empty': {
  2312. '*': {
  2313. action_: 'output'
  2314. }
  2315. },
  2316. '*': {
  2317. '*': {
  2318. action_: ['output', 'cdot'],
  2319. nextState: '0'
  2320. }
  2321. },
  2322. '\\x': {
  2323. '*': {
  2324. action_: 'rm='
  2325. }
  2326. },
  2327. 'space': {
  2328. '*': {
  2329. action_: ['output', 'space'],
  2330. nextState: '0'
  2331. }
  2332. },
  2333. '^{(...)}|^(-1)': {
  2334. '1': {
  2335. action_: '^(-1)'
  2336. }
  2337. },
  2338. '-9.,9': {
  2339. '0': {
  2340. action_: 'rm=',
  2341. nextState: '0'
  2342. },
  2343. '1': {
  2344. action_: '^(-1)',
  2345. nextState: '0'
  2346. }
  2347. },
  2348. '{...}|else': {
  2349. '*': {
  2350. action_: 'rm=',
  2351. nextState: '1'
  2352. }
  2353. }
  2354. }),
  2355. actions: {
  2356. 'cdot': function cdot() {
  2357. return {
  2358. type_: 'tight cdot'
  2359. };
  2360. },
  2361. '^(-1)': function _(buffer, m) {
  2362. buffer.rm += "^{" + m + "}";
  2363. },
  2364. 'space': function space() {
  2365. return {
  2366. type_: 'pu-space-2'
  2367. };
  2368. },
  2369. 'output': function output(buffer) {
  2370. /** @type {ParserOutput | ParserOutput[]} */
  2371. var ret = [];
  2372. if (buffer.rm) {
  2373. var mrm = mhchemParser.patterns.match_('{(...)}', buffer.rm || "");
  2374. if (mrm && mrm.remainder === '') {
  2375. ret = mhchemParser.go(mrm.match_, 'pu');
  2376. } else {
  2377. ret = {
  2378. type_: 'rm',
  2379. p1: buffer.rm
  2380. };
  2381. }
  2382. }
  2383. for (var p in buffer) {
  2384. delete buffer[p];
  2385. }
  2386. return ret;
  2387. }
  2388. }
  2389. },
  2390. 'pu-9,9': {
  2391. transitions: mhchemParser.createTransitions({
  2392. 'empty': {
  2393. '0': {
  2394. action_: 'output-0'
  2395. },
  2396. 'o': {
  2397. action_: 'output-o'
  2398. }
  2399. },
  2400. ',': {
  2401. '0': {
  2402. action_: ['output-0', 'comma'],
  2403. nextState: 'o'
  2404. }
  2405. },
  2406. '.': {
  2407. '0': {
  2408. action_: ['output-0', 'copy'],
  2409. nextState: 'o'
  2410. }
  2411. },
  2412. 'else': {
  2413. '*': {
  2414. action_: 'text='
  2415. }
  2416. }
  2417. }),
  2418. actions: {
  2419. 'comma': function comma() {
  2420. return {
  2421. type_: 'commaDecimal'
  2422. };
  2423. },
  2424. 'output-0': function output0(buffer) {
  2425. /** @type {ParserOutput[]} */
  2426. var ret = [];
  2427. buffer.text_ = buffer.text_ || "";
  2428. if (buffer.text_.length > 4) {
  2429. var a = buffer.text_.length % 3;
  2430. if (a === 0) {
  2431. a = 3;
  2432. }
  2433. for (var i = buffer.text_.length - 3; i > 0; i -= 3) {
  2434. ret.push(buffer.text_.substr(i, 3));
  2435. ret.push({
  2436. type_: '1000 separator'
  2437. });
  2438. }
  2439. ret.push(buffer.text_.substr(0, a));
  2440. ret.reverse();
  2441. } else {
  2442. ret.push(buffer.text_);
  2443. }
  2444. for (var p in buffer) {
  2445. delete buffer[p];
  2446. }
  2447. return ret;
  2448. },
  2449. 'output-o': function outputO(buffer) {
  2450. /** @type {ParserOutput[]} */
  2451. var ret = [];
  2452. buffer.text_ = buffer.text_ || "";
  2453. if (buffer.text_.length > 4) {
  2454. var a = buffer.text_.length - 3;
  2455. for (var i = 0; i < a; i += 3) {
  2456. ret.push(buffer.text_.substr(i, 3));
  2457. ret.push({
  2458. type_: '1000 separator'
  2459. });
  2460. }
  2461. ret.push(buffer.text_.substr(i));
  2462. } else {
  2463. ret.push(buffer.text_);
  2464. }
  2465. for (var p in buffer) {
  2466. delete buffer[p];
  2467. }
  2468. return ret;
  2469. }
  2470. }
  2471. } //#endregion
  2472. }; //
  2473. // texify: Take MhchemParser output and convert it to TeX
  2474. //
  2475. /** @type {Texify} */
  2476. var texify = {
  2477. go: function go(input, isInner) {
  2478. // (recursive, max 4 levels)
  2479. if (!input) {
  2480. return "";
  2481. }
  2482. var res = "";
  2483. var cee = false;
  2484. for (var i = 0; i < input.length; i++) {
  2485. var inputi = input[i];
  2486. if (typeof inputi === "string") {
  2487. res += inputi;
  2488. } else {
  2489. res += texify._go2(inputi);
  2490. if (inputi.type_ === '1st-level escape') {
  2491. cee = true;
  2492. }
  2493. }
  2494. }
  2495. if (!isInner && !cee && res) {
  2496. res = "{" + res + "}";
  2497. }
  2498. return res;
  2499. },
  2500. _goInner: function _goInner(input) {
  2501. if (!input) {
  2502. return input;
  2503. }
  2504. return texify.go(input, true);
  2505. },
  2506. _go2: function _go2(buf) {
  2507. /** @type {undefined | string} */
  2508. var res;
  2509. switch (buf.type_) {
  2510. case 'chemfive':
  2511. res = "";
  2512. var b5 = {
  2513. a: texify._goInner(buf.a),
  2514. b: texify._goInner(buf.b),
  2515. p: texify._goInner(buf.p),
  2516. o: texify._goInner(buf.o),
  2517. q: texify._goInner(buf.q),
  2518. d: texify._goInner(buf.d)
  2519. }; //
  2520. // a
  2521. //
  2522. if (b5.a) {
  2523. if (b5.a.match(/^[+\-]/)) {
  2524. b5.a = "{" + b5.a + "}";
  2525. }
  2526. res += b5.a + "\\,";
  2527. } //
  2528. // b and p
  2529. //
  2530. if (b5.b || b5.p) {
  2531. res += "{\\vphantom{X}}";
  2532. res += "^{\\hphantom{" + (b5.b || "") + "}}_{\\hphantom{" + (b5.p || "") + "}}";
  2533. res += "{\\vphantom{X}}";
  2534. res += "^{\\smash[t]{\\vphantom{2}}\\mathllap{" + (b5.b || "") + "}}";
  2535. res += "_{\\vphantom{2}\\mathllap{\\smash[t]{" + (b5.p || "") + "}}}";
  2536. } //
  2537. // o
  2538. //
  2539. if (b5.o) {
  2540. if (b5.o.match(/^[+\-]/)) {
  2541. b5.o = "{" + b5.o + "}";
  2542. }
  2543. res += b5.o;
  2544. } //
  2545. // q and d
  2546. //
  2547. if (buf.dType === 'kv') {
  2548. if (b5.d || b5.q) {
  2549. res += "{\\vphantom{X}}";
  2550. }
  2551. if (b5.d) {
  2552. res += "^{" + b5.d + "}";
  2553. }
  2554. if (b5.q) {
  2555. res += "_{\\smash[t]{" + b5.q + "}}";
  2556. }
  2557. } else if (buf.dType === 'oxidation') {
  2558. if (b5.d) {
  2559. res += "{\\vphantom{X}}";
  2560. res += "^{" + b5.d + "}";
  2561. }
  2562. if (b5.q) {
  2563. res += "{\\vphantom{X}}";
  2564. res += "_{\\smash[t]{" + b5.q + "}}";
  2565. }
  2566. } else {
  2567. if (b5.q) {
  2568. res += "{\\vphantom{X}}";
  2569. res += "_{\\smash[t]{" + b5.q + "}}";
  2570. }
  2571. if (b5.d) {
  2572. res += "{\\vphantom{X}}";
  2573. res += "^{" + b5.d + "}";
  2574. }
  2575. }
  2576. break;
  2577. case 'rm':
  2578. res = "\\mathrm{" + buf.p1 + "}";
  2579. break;
  2580. case 'text':
  2581. if (buf.p1.match(/[\^_]/)) {
  2582. buf.p1 = buf.p1.replace(" ", "~").replace("-", "\\text{-}");
  2583. res = "\\mathrm{" + buf.p1 + "}";
  2584. } else {
  2585. res = "\\text{" + buf.p1 + "}";
  2586. }
  2587. break;
  2588. case 'roman numeral':
  2589. res = "\\mathrm{" + buf.p1 + "}";
  2590. break;
  2591. case 'state of aggregation':
  2592. res = "\\mskip2mu " + texify._goInner(buf.p1);
  2593. break;
  2594. case 'state of aggregation subscript':
  2595. res = "\\mskip1mu " + texify._goInner(buf.p1);
  2596. break;
  2597. case 'bond':
  2598. res = texify._getBond(buf.kind_);
  2599. if (!res) {
  2600. throw ["MhchemErrorBond", "mhchem Error. Unknown bond type (" + buf.kind_ + ")"];
  2601. }
  2602. break;
  2603. case 'frac':
  2604. var c = "\\frac{" + buf.p1 + "}{" + buf.p2 + "}";
  2605. res = "\\mathchoice{\\textstyle" + c + "}{" + c + "}{" + c + "}{" + c + "}";
  2606. break;
  2607. case 'pu-frac':
  2608. var d = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  2609. res = "\\mathchoice{\\textstyle" + d + "}{" + d + "}{" + d + "}{" + d + "}";
  2610. break;
  2611. case 'tex-math':
  2612. res = buf.p1 + " ";
  2613. break;
  2614. case 'frac-ce':
  2615. res = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  2616. break;
  2617. case 'overset':
  2618. res = "\\overset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  2619. break;
  2620. case 'underset':
  2621. res = "\\underset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  2622. break;
  2623. case 'underbrace':
  2624. res = "\\underbrace{" + texify._goInner(buf.p1) + "}_{" + texify._goInner(buf.p2) + "}";
  2625. break;
  2626. case 'color':
  2627. res = "{\\color{" + buf.color1 + "}{" + texify._goInner(buf.color2) + "}}";
  2628. break;
  2629. case 'color0':
  2630. res = "\\color{" + buf.color + "}";
  2631. break;
  2632. case 'arrow':
  2633. var b6 = {
  2634. rd: texify._goInner(buf.rd),
  2635. rq: texify._goInner(buf.rq)
  2636. };
  2637. var arrow = "\\x" + texify._getArrow(buf.r);
  2638. if (b6.rq) {
  2639. arrow += "[{" + b6.rq + "}]";
  2640. }
  2641. if (b6.rd) {
  2642. arrow += "{" + b6.rd + "}";
  2643. } else {
  2644. arrow += "{}";
  2645. }
  2646. res = arrow;
  2647. break;
  2648. case 'operator':
  2649. res = texify._getOperator(buf.kind_);
  2650. break;
  2651. case '1st-level escape':
  2652. res = buf.p1 + " "; // &, \\\\, \\hlin
  2653. break;
  2654. case 'space':
  2655. res = " ";
  2656. break;
  2657. case 'entitySkip':
  2658. res = "~";
  2659. break;
  2660. case 'pu-space-1':
  2661. res = "~";
  2662. break;
  2663. case 'pu-space-2':
  2664. res = "\\mkern3mu ";
  2665. break;
  2666. case '1000 separator':
  2667. res = "\\mkern2mu ";
  2668. break;
  2669. case 'commaDecimal':
  2670. res = "{,}";
  2671. break;
  2672. case 'comma enumeration L':
  2673. res = "{" + buf.p1 + "}\\mkern6mu ";
  2674. break;
  2675. case 'comma enumeration M':
  2676. res = "{" + buf.p1 + "}\\mkern3mu ";
  2677. break;
  2678. case 'comma enumeration S':
  2679. res = "{" + buf.p1 + "}\\mkern1mu ";
  2680. break;
  2681. case 'hyphen':
  2682. res = "\\text{-}";
  2683. break;
  2684. case 'addition compound':
  2685. res = "\\,{\\cdot}\\,";
  2686. break;
  2687. case 'electron dot':
  2688. res = "\\mkern1mu \\bullet\\mkern1mu ";
  2689. break;
  2690. case 'KV x':
  2691. res = "{\\times}";
  2692. break;
  2693. case 'prime':
  2694. res = "\\prime ";
  2695. break;
  2696. case 'cdot':
  2697. res = "\\cdot ";
  2698. break;
  2699. case 'tight cdot':
  2700. res = "\\mkern1mu{\\cdot}\\mkern1mu ";
  2701. break;
  2702. case 'times':
  2703. res = "\\times ";
  2704. break;
  2705. case 'circa':
  2706. res = "{\\sim}";
  2707. break;
  2708. case '^':
  2709. res = "uparrow";
  2710. break;
  2711. case 'v':
  2712. res = "downarrow";
  2713. break;
  2714. case 'ellipsis':
  2715. res = "\\ldots ";
  2716. break;
  2717. case '/':
  2718. res = "/";
  2719. break;
  2720. case ' / ':
  2721. res = "\\,/\\,";
  2722. break;
  2723. default:
  2724. throw ["MhchemBugT", "mhchem bug T. Please report."];
  2725. // Missing texify rule or unknown MhchemParser output
  2726. }
  2727. return res;
  2728. },
  2729. _getArrow: function _getArrow(a) {
  2730. switch (a) {
  2731. case "->":
  2732. return "rightarrow";
  2733. case "\u2192":
  2734. return "rightarrow";
  2735. case "\u27F6":
  2736. return "rightarrow";
  2737. case "<-":
  2738. return "leftarrow";
  2739. case "<->":
  2740. return "leftrightarrow";
  2741. case "<-->":
  2742. return "rightleftarrows";
  2743. case "<=>":
  2744. return "rightleftharpoons";
  2745. case "\u21CC":
  2746. return "rightleftharpoons";
  2747. case "<=>>":
  2748. return "rightequilibrium";
  2749. case "<<=>":
  2750. return "leftequilibrium";
  2751. default:
  2752. throw ["MhchemBugT", "mhchem bug T. Please report."];
  2753. }
  2754. },
  2755. _getBond: function _getBond(a) {
  2756. switch (a) {
  2757. case "-":
  2758. return "{-}";
  2759. case "1":
  2760. return "{-}";
  2761. case "=":
  2762. return "{=}";
  2763. case "2":
  2764. return "{=}";
  2765. case "#":
  2766. return "{\\equiv}";
  2767. case "3":
  2768. return "{\\equiv}";
  2769. case "~":
  2770. return "{\\tripledash}";
  2771. case "~-":
  2772. return "{\\mathrlap{\\raisebox{-.1em}{$-$}}\\raisebox{.1em}{$\\tripledash$}}";
  2773. case "~=":
  2774. return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  2775. case "~--":
  2776. return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  2777. case "-~-":
  2778. return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$-$}}\\tripledash}";
  2779. case "...":
  2780. return "{{\\cdot}{\\cdot}{\\cdot}}";
  2781. case "....":
  2782. return "{{\\cdot}{\\cdot}{\\cdot}{\\cdot}}";
  2783. case "->":
  2784. return "{\\rightarrow}";
  2785. case "<-":
  2786. return "{\\leftarrow}";
  2787. case "<":
  2788. return "{<}";
  2789. case ">":
  2790. return "{>}";
  2791. default:
  2792. throw ["MhchemBugT", "mhchem bug T. Please report."];
  2793. }
  2794. },
  2795. _getOperator: function _getOperator(a) {
  2796. switch (a) {
  2797. case "+":
  2798. return " {}+{} ";
  2799. case "-":
  2800. return " {}-{} ";
  2801. case "=":
  2802. return " {}={} ";
  2803. case "<":
  2804. return " {}<{} ";
  2805. case ">":
  2806. return " {}>{} ";
  2807. case "<<":
  2808. return " {}\\ll{} ";
  2809. case ">>":
  2810. return " {}\\gg{} ";
  2811. case "\\pm":
  2812. return " {}\\pm{} ";
  2813. case "\\approx":
  2814. return " {}\\approx{} ";
  2815. case "$\\approx$":
  2816. return " {}\\approx{} ";
  2817. case "v":
  2818. return " \\downarrow{} ";
  2819. case "(v)":
  2820. return " \\downarrow{} ";
  2821. case "^":
  2822. return " \\uparrow{} ";
  2823. case "(^)":
  2824. return " \\uparrow{} ";
  2825. default:
  2826. throw ["MhchemBugT", "mhchem bug T. Please report."];
  2827. }
  2828. }
  2829. }; //