mhchem.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /* eslint-disable */
  2. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  3. /* vim: set ts=2 et sw=2 tw=80: */
  4. /*************************************************************
  5. *
  6. * KaTeX mhchem.js
  7. *
  8. * This file implements a KaTeX version of mhchem version 3.3.0.
  9. * It is adapted from MathJax/extensions/TeX/mhchem.js
  10. * It differs from the MathJax version as follows:
  11. * 1. The interface is changed so that it can be called from KaTeX, not MathJax.
  12. * 2. \rlap and \llap are replaced with \mathrlap and \mathllap.
  13. * 3. Four lines of code are edited in order to use \raisebox instead of \raise.
  14. * 4. The reaction arrow code is simplified. All reaction arrows are rendered
  15. * using KaTeX extensible arrows instead of building non-extensible arrows.
  16. * 5. \tripledash vertical alignment is slightly adjusted.
  17. *
  18. * This code, as other KaTeX code, is released under the MIT license.
  19. *
  20. * /*************************************************************
  21. *
  22. * MathJax/extensions/TeX/mhchem.js
  23. *
  24. * Implements the \ce command for handling chemical formulas
  25. * from the mhchem LaTeX package.
  26. *
  27. * ---------------------------------------------------------------------
  28. *
  29. * Copyright (c) 2011-2015 The MathJax Consortium
  30. * Copyright (c) 2015-2018 Martin Hensel
  31. *
  32. * Licensed under the Apache License, Version 2.0 (the "License");
  33. * you may not use this file except in compliance with the License.
  34. * You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing, software
  39. * distributed under the License is distributed on an "AS IS" BASIS,
  40. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. * See the License for the specific language governing permissions and
  42. * limitations under the License.
  43. */
  44. //
  45. // Coding Style
  46. // - use '' for identifiers that can by minified/uglified
  47. // - use "" for strings that need to stay untouched
  48. // version: "3.3.0" for MathJax and KaTeX
  49. //
  50. // This is the main function for handing the \ce and \pu commands.
  51. // It takes the argument to \ce or \pu and returns the corresponding TeX string.
  52. //
  53. module.exports = function (tokens, stateMachine) {
  54. // Recreate the argument string from KaTeX's array of tokens.
  55. var str = "";
  56. var expectedLoc = tokens[tokens.length - 1].loc.start
  57. for (var i = tokens.length - 1; i >= 0; i--) {
  58. if(tokens[i].loc.start > expectedLoc) {
  59. // context.consumeArgs has eaten a space.
  60. str += " ";
  61. expectedLoc = tokens[i].loc.start;
  62. }
  63. str += tokens[i].text;
  64. expectedLoc += tokens[i].text.length;
  65. }
  66. var tex = texify.go(mhchemParser.go(str, stateMachine));
  67. return tex;
  68. };
  69. //
  70. // Core parser for mhchem syntax (recursive)
  71. //
  72. /** @type {MhchemParser} */
  73. var mhchemParser = {
  74. //
  75. // Parses mchem \ce syntax
  76. //
  77. // Call like
  78. // go("H2O");
  79. //
  80. go: function (input, stateMachine) {
  81. if (!input) { return []; }
  82. if (stateMachine === undefined) { stateMachine = 'ce'; }
  83. var state = '0';
  84. //
  85. // String buffers for parsing:
  86. //
  87. // buffer.a == amount
  88. // buffer.o == element
  89. // buffer.b == left-side superscript
  90. // buffer.p == left-side subscript
  91. // buffer.q == right-side subscript
  92. // buffer.d == right-side superscript
  93. //
  94. // buffer.r == arrow
  95. // buffer.rdt == arrow, script above, type
  96. // buffer.rd == arrow, script above, content
  97. // buffer.rqt == arrow, script below, type
  98. // buffer.rq == arrow, script below, content
  99. //
  100. // buffer.text_
  101. // buffer.rm
  102. // etc.
  103. //
  104. // buffer.parenthesisLevel == int, starting at 0
  105. // buffer.sb == bool, space before
  106. // buffer.beginsWithBond == bool
  107. //
  108. // These letters are also used as state names.
  109. //
  110. // Other states:
  111. // 0 == begin of main part (arrow/operator unlikely)
  112. // 1 == next entity
  113. // 2 == next entity (arrow/operator unlikely)
  114. // 3 == next atom
  115. // c == macro
  116. //
  117. /** @type {Buffer} */
  118. var buffer = {};
  119. buffer['parenthesisLevel'] = 0;
  120. input = input.replace(/\n/g, " ");
  121. input = input.replace(/[\u2212\u2013\u2014\u2010]/g, "-");
  122. input = input.replace(/[\u2026]/g, "...");
  123. //
  124. // Looks through mhchemParser.transitions, to execute a matching action
  125. // (recursive)
  126. //
  127. var lastInput;
  128. var watchdog = 10;
  129. /** @type {ParserOutput[]} */
  130. var output = [];
  131. while (true) {
  132. if (lastInput !== input) {
  133. watchdog = 10;
  134. lastInput = input;
  135. } else {
  136. watchdog--;
  137. }
  138. //
  139. // Find actions in transition table
  140. //
  141. var machine = mhchemParser.stateMachines[stateMachine];
  142. var t = machine.transitions[state] || machine.transitions['*'];
  143. iterateTransitions:
  144. for (var i=0; i<t.length; i++) {
  145. var matches = mhchemParser.patterns.match_(t[i].pattern, input);
  146. if (matches) {
  147. //
  148. // Execute actions
  149. //
  150. var task = t[i].task;
  151. for (var iA=0; iA<task.action_.length; iA++) {
  152. var o;
  153. //
  154. // Find and execute action
  155. //
  156. if (machine.actions[task.action_[iA].type_]) {
  157. o = machine.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
  158. } else if (mhchemParser.actions[task.action_[iA].type_]) {
  159. o = mhchemParser.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
  160. } else {
  161. throw ["MhchemBugA", "mhchem bug A. Please report. (" + task.action_[iA].type_ + ")"]; // Trying to use non-existing action
  162. }
  163. //
  164. // Add output
  165. //
  166. mhchemParser.concatArray(output, o);
  167. }
  168. //
  169. // Set next state,
  170. // Shorten input,
  171. // Continue with next character
  172. // (= apply only one transition per position)
  173. //
  174. state = task.nextState || state;
  175. if (input.length > 0) {
  176. if (!task.revisit) {
  177. input = matches.remainder;
  178. }
  179. if (!task.toContinue) {
  180. break iterateTransitions;
  181. }
  182. } else {
  183. return output;
  184. }
  185. }
  186. }
  187. //
  188. // Prevent infinite loop
  189. //
  190. if (watchdog <= 0) {
  191. throw ["MhchemBugU", "mhchem bug U. Please report."]; // Unexpected character
  192. }
  193. }
  194. },
  195. concatArray: function (a, b) {
  196. if (b) {
  197. if (Array.isArray(b)) {
  198. for (var iB=0; iB<b.length; iB++) {
  199. a.push(b[iB]);
  200. }
  201. } else {
  202. a.push(b);
  203. }
  204. }
  205. },
  206. patterns: {
  207. //
  208. // Matching patterns
  209. // either regexps or function that return null or {match_:"a", remainder:"bc"}
  210. //
  211. patterns: {
  212. // property names must not look like integers ("2") for correct property traversal order, later on
  213. 'empty': /^$/,
  214. 'else': /^./,
  215. 'else2': /^./,
  216. 'space': /^\s/,
  217. 'space A': /^\s(?=[A-Z\\$])/,
  218. 'space$': /^\s$/,
  219. 'a-z': /^[a-z]/,
  220. 'x': /^x/,
  221. 'x$': /^x$/,
  222. 'i$': /^i$/,
  223. '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]))))+/,
  224. '\\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]))/,
  225. 'one lowercase latin letter $': /^(?:([a-z])(?:$|[^a-zA-Z]))$/,
  226. '$one lowercase latin letter$ $': /^\$(?:([a-z])(?:$|[^a-zA-Z]))\$$/,
  227. '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]))$/,
  228. 'digits': /^[0-9]+/,
  229. '-9.,9': /^[+\-]?(?:[0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))/,
  230. '-9.,9 no missing 0': /^[+\-]?[0-9]+(?:[.,][0-9]+)?/,
  231. '(-)(9.,9)(e)(99)': function (input) {
  232. 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]+\}))?/);
  233. if (m && m[0]) {
  234. return { match_: m.splice(1), remainder: input.substr(m[0].length) };
  235. }
  236. return null;
  237. },
  238. '(-)(9)^(-9)': function (input) {
  239. var m = input.match(/^(\+\-|\+\/\-|\+|\-|\\pm\s?)?([0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+)?)\^([+\-]?[0-9]+|\{[+\-]?[0-9]+\})/);
  240. if (m && m[0]) {
  241. return { match_: m.splice(1), remainder: input.substr(m[0].length) };
  242. }
  243. return null;
  244. },
  245. 'state of aggregation $': function (input) { // ... or crystal system
  246. var a = mhchemParser.patterns.findObserveGroups(input, "", /^\([a-z]{1,3}(?=[\),])/, ")", ""); // (aq), (aq,$\infty$), (aq, sat)
  247. if (a && a.remainder.match(/^($|[\s,;\)\]\}])/)) { return a; } // AND end of 'phrase'
  248. var m = input.match(/^(?:\((?:\\ca\s?)?\$[amothc]\$\))/); // OR crystal system ($o$) (\ca$c$)
  249. if (m) {
  250. return { match_: m[0], remainder: input.substr(m[0].length) };
  251. }
  252. return null;
  253. },
  254. '_{(state of aggregation)}$': /^_\{(\([a-z]{1,3}\))\}/,
  255. '{[(': /^(?:\\\{|\[|\()/,
  256. ')]}': /^(?:\)|\]|\\\})/,
  257. ', ': /^[,;]\s*/,
  258. ',': /^[,;]/,
  259. '.': /^[.]/,
  260. '. ': /^([.\u22C5\u00B7\u2022])\s*/,
  261. '...': /^\.\.\.(?=$|[^.])/,
  262. '* ': /^([*])\s*/,
  263. '^{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^{", "", "", "}"); },
  264. '^($...$)': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", "$", "$", ""); },
  265. '^a': /^\^([0-9]+|[^\\_])/,
  266. '^\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
  267. '^\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", ""); },
  268. '^\\x': /^\^(\\[a-zA-Z]+)\s*/,
  269. '^(-1)': /^\^(-?\d+)/,
  270. '\'': /^'/,
  271. '_{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_{", "", "", "}"); },
  272. '_($...$)': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", "$", "$", ""); },
  273. '_9': /^_([+\-]?[0-9]+|[^\\])/,
  274. '_\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
  275. '_\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", ""); },
  276. '_\\x': /^_(\\[a-zA-Z]+)\s*/,
  277. '^_': /^(?:\^(?=_)|\_(?=\^)|[\^_]$)/,
  278. '{}': /^\{\}/,
  279. '{...}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", "{", "}", ""); },
  280. '{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "{", "", "", "}"); },
  281. '$...$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", "$", "$", ""); },
  282. '${(...)}$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "${", "", "", "}$"); },
  283. '$(...)$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "$", "", "", "$"); },
  284. '=<>': /^[=<>]/,
  285. '#': /^[#\u2261]/,
  286. '+': /^\+/,
  287. '-$': /^-(?=[\s_},;\]/]|$|\([a-z]+\))/, // -space -, -; -] -/ -$ -state-of-aggregation
  288. '-9': /^-(?=[0-9])/,
  289. '- orbital overlap': /^-(?=(?:[spd]|sp)(?:$|[\s,;\)\]\}]))/,
  290. '-': /^-/,
  291. 'pm-operator': /^(?:\\pm|\$\\pm\$|\+-|\+\/-)/,
  292. 'operator': /^(?:\+|(?:[\-=<>]|<<|>>|\\approx|\$\\approx\$)(?=\s|$|-?[0-9]))/,
  293. 'arrowUpDown': /^(?:v|\(v\)|\^|\(\^\))(?=$|[\s,;\)\]\}])/,
  294. '\\bond{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\bond{", "", "", "}"); },
  295. '->': /^(?:<->|<-->|->|<-|<=>>|<<=>|<=>|[\u2192\u27F6\u21CC])/,
  296. 'CMT': /^[CMT](?=\[)/,
  297. '[(...)]': function (input) { return mhchemParser.patterns.findObserveGroups(input, "[", "", "", "]"); },
  298. '1st-level escape': /^(&|\\\\|\\hline)\s*/,
  299. '\\,': /^(?:\\[,\ ;:])/, // \\x - but output no space before
  300. '\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
  301. '\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", ""); },
  302. '\\ca': /^\\ca(?:\s+|(?![a-zA-Z]))/,
  303. '\\x': /^(?:\\[a-zA-Z]+\s*|\\[_&{}%])/,
  304. 'orbital': /^(?:[0-9]{1,2}[spdfgh]|[0-9]{0,2}sp)(?=$|[^a-zA-Z])/, // only those with numbers in front, because the others will be formatted correctly anyway
  305. 'others': /^[\/~|]/,
  306. '\\frac{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\frac{", "", "", "}", "{", "", "", "}"); },
  307. '\\overset{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\overset{", "", "", "}", "{", "", "", "}"); },
  308. '\\underset{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\underset{", "", "", "}", "{", "", "", "}"); },
  309. '\\underbrace{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\underbrace{", "", "", "}_", "{", "", "", "}"); },
  310. '\\color{(...)}0': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}"); },
  311. '\\color{(...)}{(...)}1': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}", "{", "", "", "}"); },
  312. '\\color(...){(...)}2': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color", "\\", "", /^(?=\{)/, "{", "", "", "}"); },
  313. '\\ce{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\ce{", "", "", "}"); },
  314. 'oxidation$': /^(?:[+-][IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/,
  315. 'd-oxidation$': /^(?:[+-]?\s?[IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/, // 0 could be oxidation or charge
  316. 'roman numeral': /^[IVX]+/,
  317. '1/2$': /^[+\-]?(?:[0-9]+|\$[a-z]\$|[a-z])\/[0-9]+(?:\$[a-z]\$|[a-z])?$/,
  318. 'amount': function (input) {
  319. var match;
  320. // e.g. 2, 0.5, 1/2, -2, n/2, +; $a$ could be added later in parsing
  321. 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))/);
  322. if (match) {
  323. return { match_: match[0], remainder: input.substr(match[0].length) };
  324. }
  325. var a = mhchemParser.patterns.findObserveGroups(input, "", "$", "$", "");
  326. if (a) { // e.g. $2n-1$, $-$
  327. match = a.match_.match(/^\$(?:\(?[+\-]?(?:[0-9]*[a-z]?[+\-])?[0-9]*[a-z](?:[+\-][0-9]*[a-z]?)?\)?|\+|-)\$$/);
  328. if (match) {
  329. return { match_: match[0], remainder: input.substr(match[0].length) };
  330. }
  331. }
  332. return null;
  333. },
  334. 'amount2': function (input) { return this['amount'](input); },
  335. '(KV letters),': /^(?:[A-Z][a-z]{0,2}|i)(?=,)/,
  336. 'formula$': function (input) {
  337. if (input.match(/^\([a-z]+\)$/)) { return null; } // state of aggregation = no formula
  338. var match = input.match(/^(?:[a-z]|(?:[0-9\ \+\-\,\.\(\)]+[a-z])+[0-9\ \+\-\,\.\(\)]*|(?:[a-z][0-9\ \+\-\,\.\(\)]+)+[a-z]?)$/);
  339. if (match) {
  340. return { match_: match[0], remainder: input.substr(match[0].length) };
  341. }
  342. return null;
  343. },
  344. 'uprightEntities': /^(?:pH|pOH|pC|pK|iPr|iBu)(?=$|[^a-zA-Z])/,
  345. '/': /^\s*(\/)\s*/,
  346. '//': /^\s*(\/\/)\s*/,
  347. '*': /^\s*[*.]\s*/
  348. },
  349. findObserveGroups: function (input, begExcl, begIncl, endIncl, endExcl, beg2Excl, beg2Incl, end2Incl, end2Excl, combine) {
  350. /** @type {{(input: string, pattern: string | RegExp): string | string[] | null;}} */
  351. var _match = function (input, pattern) {
  352. if (typeof pattern === "string") {
  353. if (input.indexOf(pattern) !== 0) { return null; }
  354. return pattern;
  355. } else {
  356. var match = input.match(pattern);
  357. if (!match) { return null; }
  358. return match[0];
  359. }
  360. };
  361. /** @type {{(input: string, i: number, endChars: string | RegExp): {endMatchBegin: number, endMatchEnd: number} | null;}} */
  362. var _findObserveGroups = function (input, i, endChars) {
  363. var braces = 0;
  364. while (i < input.length) {
  365. var a = input.charAt(i);
  366. var match = _match(input.substr(i), endChars);
  367. if (match !== null && braces === 0) {
  368. return { endMatchBegin: i, endMatchEnd: i + match.length };
  369. } else if (a === "{") {
  370. braces++;
  371. } else if (a === "}") {
  372. if (braces === 0) {
  373. throw ["ExtraCloseMissingOpen", "Extra close brace or missing open brace"];
  374. } else {
  375. braces--;
  376. }
  377. }
  378. i++;
  379. }
  380. if (braces > 0) {
  381. return null;
  382. }
  383. return null;
  384. };
  385. var match = _match(input, begExcl);
  386. if (match === null) { return null; }
  387. input = input.substr(match.length);
  388. match = _match(input, begIncl);
  389. if (match === null) { return null; }
  390. var e = _findObserveGroups(input, match.length, endIncl || endExcl);
  391. if (e === null) { return null; }
  392. var match1 = input.substring(0, (endIncl ? e.endMatchEnd : e.endMatchBegin));
  393. if (!(beg2Excl || beg2Incl)) {
  394. return {
  395. match_: match1,
  396. remainder: input.substr(e.endMatchEnd)
  397. };
  398. } else {
  399. var group2 = this.findObserveGroups(input.substr(e.endMatchEnd), beg2Excl, beg2Incl, end2Incl, end2Excl);
  400. if (group2 === null) { return null; }
  401. /** @type {string[]} */
  402. var matchRet = [match1, group2.match_];
  403. return {
  404. match_: (combine ? matchRet.join("") : matchRet),
  405. remainder: group2.remainder
  406. };
  407. }
  408. },
  409. //
  410. // Matching function
  411. // e.g. match("a", input) will look for the regexp called "a" and see if it matches
  412. // returns null or {match_:"a", remainder:"bc"}
  413. //
  414. match_: function (m, input) {
  415. var pattern = mhchemParser.patterns.patterns[m];
  416. if (pattern === undefined) {
  417. throw ["MhchemBugP", "mhchem bug P. Please report. (" + m + ")"]; // Trying to use non-existing pattern
  418. } else if (typeof pattern === "function") {
  419. return mhchemParser.patterns.patterns[m](input); // cannot use cached var pattern here, because some pattern functions need this===mhchemParser
  420. } else { // RegExp
  421. var match = input.match(pattern);
  422. if (match) {
  423. var mm;
  424. if (match[2]) {
  425. mm = [ match[1], match[2] ];
  426. } else if (match[1]) {
  427. mm = match[1];
  428. } else {
  429. mm = match[0];
  430. }
  431. return { match_: mm, remainder: input.substr(match[0].length) };
  432. }
  433. return null;
  434. }
  435. }
  436. },
  437. //
  438. // Generic state machine actions
  439. //
  440. actions: {
  441. 'a=': function (buffer, m) { buffer.a = (buffer.a || "") + m; },
  442. 'b=': function (buffer, m) { buffer.b = (buffer.b || "") + m; },
  443. 'p=': function (buffer, m) { buffer.p = (buffer.p || "") + m; },
  444. 'o=': function (buffer, m) { buffer.o = (buffer.o || "") + m; },
  445. 'q=': function (buffer, m) { buffer.q = (buffer.q || "") + m; },
  446. 'd=': function (buffer, m) { buffer.d = (buffer.d || "") + m; },
  447. 'rm=': function (buffer, m) { buffer.rm = (buffer.rm || "") + m; },
  448. 'text=': function (buffer, m) { buffer.text_ = (buffer.text_ || "") + m; },
  449. 'insert': function (buffer, m, a) { return { type_: a }; },
  450. 'insert+p1': function (buffer, m, a) { return { type_: a, p1: m }; },
  451. 'insert+p1+p2': function (buffer, m, a) { return { type_: a, p1: m[0], p2: m[1] }; },
  452. 'copy': function (buffer, m) { return m; },
  453. 'rm': function (buffer, m) { return { type_: 'rm', p1: m || ""}; },
  454. 'text': function (buffer, m) { return mhchemParser.go(m, 'text'); },
  455. '{text}': function (buffer, m) {
  456. var ret = [ "{" ];
  457. mhchemParser.concatArray(ret, mhchemParser.go(m, 'text'));
  458. ret.push("}");
  459. return ret;
  460. },
  461. 'tex-math': function (buffer, m) { return mhchemParser.go(m, 'tex-math'); },
  462. 'tex-math tight': function (buffer, m) { return mhchemParser.go(m, 'tex-math tight'); },
  463. 'bond': function (buffer, m, k) { return { type_: 'bond', kind_: k || m }; },
  464. 'color0-output': function (buffer, m) { return { type_: 'color0', color: m[0] }; },
  465. 'ce': function (buffer, m) { return mhchemParser.go(m); },
  466. '1/2': function (buffer, m) {
  467. /** @type {ParserOutput[]} */
  468. var ret = [];
  469. if (m.match(/^[+\-]/)) {
  470. ret.push(m.substr(0, 1));
  471. m = m.substr(1);
  472. }
  473. var n = m.match(/^([0-9]+|\$[a-z]\$|[a-z])\/([0-9]+)(\$[a-z]\$|[a-z])?$/);
  474. n[1] = n[1].replace(/\$/g, "");
  475. ret.push({ type_: 'frac', p1: n[1], p2: n[2] });
  476. if (n[3]) {
  477. n[3] = n[3].replace(/\$/g, "");
  478. ret.push({ type_: 'tex-math', p1: n[3] });
  479. }
  480. return ret;
  481. },
  482. '9,9': function (buffer, m) { return mhchemParser.go(m, '9,9'); }
  483. },
  484. //
  485. // createTransitions
  486. // convert { 'letter': { 'state': { action_: 'output' } } } to { 'state' => [ { pattern: 'letter', task: { action_: [{type_: 'output'}] } } ] }
  487. // with expansion of 'a|b' to 'a' and 'b' (at 2 places)
  488. //
  489. createTransitions: function (o) {
  490. var pattern, state;
  491. /** @type {string[]} */
  492. var stateArray;
  493. var i;
  494. //
  495. // 1. Collect all states
  496. //
  497. /** @type {Transitions} */
  498. var transitions = {};
  499. for (pattern in o) {
  500. for (state in o[pattern]) {
  501. stateArray = state.split("|");
  502. o[pattern][state].stateArray = stateArray;
  503. for (i=0; i<stateArray.length; i++) {
  504. transitions[stateArray[i]] = [];
  505. }
  506. }
  507. }
  508. //
  509. // 2. Fill states
  510. //
  511. for (pattern in o) {
  512. for (state in o[pattern]) {
  513. stateArray = o[pattern][state].stateArray || [];
  514. for (i=0; i<stateArray.length; i++) {
  515. //
  516. // 2a. Normalize actions into array: 'text=' ==> [{type_:'text='}]
  517. // (Note to myself: Resolving the function here would be problematic. It would need .bind (for *this*) and currying (for *option*).)
  518. //
  519. /** @type {any} */
  520. var p = o[pattern][state];
  521. if (p.action_) {
  522. p.action_ = [].concat(p.action_);
  523. for (var k=0; k<p.action_.length; k++) {
  524. if (typeof p.action_[k] === "string") {
  525. p.action_[k] = { type_: p.action_[k] };
  526. }
  527. }
  528. } else {
  529. p.action_ = [];
  530. }
  531. //
  532. // 2.b Multi-insert
  533. //
  534. var patternArray = pattern.split("|");
  535. for (var j=0; j<patternArray.length; j++) {
  536. if (stateArray[i] === '*') { // insert into all
  537. for (var t in transitions) {
  538. transitions[t].push({ pattern: patternArray[j], task: p });
  539. }
  540. } else {
  541. transitions[stateArray[i]].push({ pattern: patternArray[j], task: p });
  542. }
  543. }
  544. }
  545. }
  546. }
  547. return transitions;
  548. },
  549. stateMachines: {}
  550. };
  551. //
  552. // Definition of state machines
  553. //
  554. mhchemParser.stateMachines = {
  555. //
  556. // \ce state machines
  557. //
  558. //#region ce
  559. 'ce': { // main parser
  560. transitions: mhchemParser.createTransitions({
  561. 'empty': {
  562. '*': { action_: 'output' } },
  563. 'else': {
  564. '0|1|2': { action_: 'beginsWithBond=false', revisit: true, toContinue: true } },
  565. 'oxidation$': {
  566. '0': { action_: 'oxidation-output' } },
  567. 'CMT': {
  568. 'r': { action_: 'rdt=', nextState: 'rt' },
  569. 'rd': { action_: 'rqt=', nextState: 'rdt' } },
  570. 'arrowUpDown': {
  571. '0|1|2|as': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '1' } },
  572. 'uprightEntities': {
  573. '0|1|2': { action_: [ 'o=', 'output' ], nextState: '1' } },
  574. 'orbital': {
  575. '0|1|2|3': { action_: 'o=', nextState: 'o' } },
  576. '->': {
  577. '0|1|2|3': { action_: 'r=', nextState: 'r' },
  578. 'a|as': { action_: [ 'output', 'r=' ], nextState: 'r' },
  579. '*': { action_: [ 'output', 'r=' ], nextState: 'r' } },
  580. '+': {
  581. 'o': { action_: 'd= kv', nextState: 'd' },
  582. 'd|D': { action_: 'd=', nextState: 'd' },
  583. 'q': { action_: 'd=', nextState: 'qd' },
  584. 'qd|qD': { action_: 'd=', nextState: 'qd' },
  585. 'dq': { action_: [ 'output', 'd=' ], nextState: 'd' },
  586. '3': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '0' } },
  587. 'amount': {
  588. '0|2': { action_: 'a=', nextState: 'a' } },
  589. 'pm-operator': {
  590. '0|1|2|a|as': { action_: [ 'sb=false', 'output', { type_: 'operator', option: '\\pm' } ], nextState: '0' } },
  591. 'operator': {
  592. '0|1|2|a|as': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '0' } },
  593. '-$': {
  594. 'o|q': { action_: [ 'charge or bond', 'output' ], nextState: 'qd' },
  595. 'd': { action_: 'd=', nextState: 'd' },
  596. 'D': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' },
  597. 'q': { action_: 'd=', nextState: 'qd' },
  598. 'qd': { action_: 'd=', nextState: 'qd' },
  599. 'qD|dq': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' } },
  600. '-9': {
  601. '3|o': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '3' } },
  602. '- orbital overlap': {
  603. 'o': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' },
  604. 'd': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' } },
  605. '-': {
  606. '0|1|2': { action_: [ { type_: 'output', option: 1 }, 'beginsWithBond=true', { type_: 'bond', option: "-" } ], nextState: '3' },
  607. '3': { action_: { type_: 'bond', option: "-" } },
  608. 'a': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' },
  609. 'as': { action_: [ { type_: 'output', option: 2 }, { type_: 'bond', option: "-" } ], nextState: '3' },
  610. 'b': { action_: 'b=' },
  611. 'o': { action_: { type_: '- after o/d', option: false }, nextState: '2' },
  612. 'q': { action_: { type_: '- after o/d', option: false }, nextState: '2' },
  613. 'd|qd|dq': { action_: { type_: '- after o/d', option: true }, nextState: '2' },
  614. 'D|qD|p': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' } },
  615. 'amount2': {
  616. '1|3': { action_: 'a=', nextState: 'a' } },
  617. 'letters': {
  618. '0|1|2|3|a|as|b|p|bp|o': { action_: 'o=', nextState: 'o' },
  619. 'q|dq': { action_: ['output', 'o='], nextState: 'o' },
  620. 'd|D|qd|qD': { action_: 'o after d', nextState: 'o' } },
  621. 'digits': {
  622. 'o': { action_: 'q=', nextState: 'q' },
  623. 'd|D': { action_: 'q=', nextState: 'dq' },
  624. 'q': { action_: [ 'output', 'o=' ], nextState: 'o' },
  625. 'a': { action_: 'o=', nextState: 'o' } },
  626. 'space A': {
  627. 'b|p|bp': {} },
  628. 'space': {
  629. 'a': { nextState: 'as' },
  630. '0': { action_: 'sb=false' },
  631. '1|2': { action_: 'sb=true' },
  632. 'r|rt|rd|rdt|rdq': { action_: 'output', nextState: '0' },
  633. '*': { action_: [ 'output', 'sb=true' ], nextState: '1'} },
  634. '1st-level escape': {
  635. '1|2': { action_: [ 'output', { type_: 'insert+p1', option: '1st-level escape' } ] },
  636. '*': { action_: [ 'output', { type_: 'insert+p1', option: '1st-level escape' } ], nextState: '0' } },
  637. '[(...)]': {
  638. 'r|rt': { action_: 'rd=', nextState: 'rd' },
  639. 'rd|rdt': { action_: 'rq=', nextState: 'rdq' } },
  640. '...': {
  641. 'o|d|D|dq|qd|qD': { action_: [ 'output', { type_: 'bond', option: "..." } ], nextState: '3' },
  642. '*': { action_: [ { type_: 'output', option: 1 }, { type_: 'insert', option: 'ellipsis' } ], nextState: '1' } },
  643. '. |* ': {
  644. '*': { action_: [ 'output', { type_: 'insert', option: 'addition compound' } ], nextState: '1' } },
  645. 'state of aggregation $': {
  646. '*': { action_: [ 'output', 'state of aggregation' ], nextState: '1' } },
  647. '{[(': {
  648. 'a|as|o': { action_: [ 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' },
  649. '0|1|2|3': { action_: [ 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' },
  650. '*': { action_: [ 'output', 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' } },
  651. ')]}': {
  652. '0|1|2|3|b|p|bp|o': { action_: [ 'o=', 'parenthesisLevel--' ], nextState: 'o' },
  653. 'a|as|d|D|q|qd|qD|dq': { action_: [ 'output', 'o=', 'parenthesisLevel--' ], nextState: 'o' } },
  654. ', ': {
  655. '*': { action_: [ 'output', 'comma' ], nextState: '0' } },
  656. '^_': { // ^ and _ without a sensible argument
  657. '*': { } },
  658. '^{(...)}|^($...$)': {
  659. '0|1|2|as': { action_: 'b=', nextState: 'b' },
  660. 'p': { action_: 'b=', nextState: 'bp' },
  661. '3|o': { action_: 'd= kv', nextState: 'D' },
  662. 'q': { action_: 'd=', nextState: 'qD' },
  663. 'd|D|qd|qD|dq': { action_: [ 'output', 'd=' ], nextState: 'D' } },
  664. '^a|^\\x{}{}|^\\x{}|^\\x|\'': {
  665. '0|1|2|as': { action_: 'b=', nextState: 'b' },
  666. 'p': { action_: 'b=', nextState: 'bp' },
  667. '3|o': { action_: 'd= kv', nextState: 'd' },
  668. 'q': { action_: 'd=', nextState: 'qd' },
  669. 'd|qd|D|qD': { action_: 'd=' },
  670. 'dq': { action_: [ 'output', 'd=' ], nextState: 'd' } },
  671. '_{(state of aggregation)}$': {
  672. 'd|D|q|qd|qD|dq': { action_: [ 'output', 'q=' ], nextState: 'q' } },
  673. '_{(...)}|_($...$)|_9|_\\x{}{}|_\\x{}|_\\x': {
  674. '0|1|2|as': { action_: 'p=', nextState: 'p' },
  675. 'b': { action_: 'p=', nextState: 'bp' },
  676. '3|o': { action_: 'q=', nextState: 'q' },
  677. 'd|D': { action_: 'q=', nextState: 'dq' },
  678. 'q|qd|qD|dq': { action_: [ 'output', 'q=' ], nextState: 'q' } },
  679. '=<>': {
  680. '0|1|2|3|a|as|o|q|d|D|qd|qD|dq': { action_: [ { type_: 'output', option: 2 }, 'bond' ], nextState: '3' } },
  681. '#': {
  682. '0|1|2|3|a|as|o': { action_: [ { type_: 'output', option: 2 }, { type_: 'bond', option: "#" } ], nextState: '3' } },
  683. '{}': {
  684. '*': { action_: { type_: 'output', option: 1 }, nextState: '1' } },
  685. '{...}': {
  686. '0|1|2|3|a|as|b|p|bp': { action_: 'o=', nextState: 'o' },
  687. 'o|d|D|q|qd|qD|dq': { action_: [ 'output', 'o=' ], nextState: 'o' } },
  688. '$...$': {
  689. 'a': { action_: 'a=' }, // 2$n$
  690. '0|1|2|3|as|b|p|bp|o': { action_: 'o=', nextState: 'o' }, // not 'amount'
  691. 'as|o': { action_: 'o=' },
  692. 'q|d|D|qd|qD|dq': { action_: [ 'output', 'o=' ], nextState: 'o' } },
  693. '\\bond{(...)}': {
  694. '*': { action_: [ { type_: 'output', option: 2 }, 'bond' ], nextState: "3" } },
  695. '\\frac{(...)}': {
  696. '*': { action_: [ { type_: 'output', option: 1 }, 'frac-output' ], nextState: '3' } },
  697. '\\overset{(...)}': {
  698. '*': { action_: [ { type_: 'output', option: 2 }, 'overset-output' ], nextState: '3' } },
  699. '\\underset{(...)}': {
  700. '*': { action_: [ { type_: 'output', option: 2 }, 'underset-output' ], nextState: '3' } },
  701. '\\underbrace{(...)}': {
  702. '*': { action_: [ { type_: 'output', option: 2 }, 'underbrace-output' ], nextState: '3' } },
  703. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  704. '*': { action_: [ { type_: 'output', option: 2 }, 'color-output' ], nextState: '3' } },
  705. '\\color{(...)}0': {
  706. '*': { action_: [ { type_: 'output', option: 2 }, 'color0-output' ] } },
  707. '\\ce{(...)}': {
  708. '*': { action_: [ { type_: 'output', option: 2 }, 'ce' ], nextState: '3' } },
  709. '\\,': {
  710. '*': { action_: [ { type_: 'output', option: 1 }, 'copy' ], nextState: '1' } },
  711. '\\x{}{}|\\x{}|\\x': {
  712. '0|1|2|3|a|as|b|p|bp|o|c0': { action_: [ 'o=', 'output' ], nextState: '3' },
  713. '*': { action_: ['output', 'o=', 'output' ], nextState: '3' } },
  714. 'others': {
  715. '*': { action_: [ { type_: 'output', option: 1 }, 'copy' ], nextState: '3' } },
  716. 'else2': {
  717. 'a': { action_: 'a to o', nextState: 'o', revisit: true },
  718. 'as': { action_: [ 'output', 'sb=true' ], nextState: '1', revisit: true },
  719. 'r|rt|rd|rdt|rdq': { action_: [ 'output' ], nextState: '0', revisit: true },
  720. '*': { action_: [ 'output', 'copy' ], nextState: '3' } }
  721. }),
  722. actions: {
  723. 'o after d': function (buffer, m) {
  724. var ret;
  725. if ((buffer.d || "").match(/^[0-9]+$/)) {
  726. var tmp = buffer.d;
  727. buffer.d = undefined;
  728. ret = this['output'](buffer);
  729. buffer.b = tmp;
  730. } else {
  731. ret = this['output'](buffer);
  732. }
  733. mhchemParser.actions['o='](buffer, m);
  734. return ret;
  735. },
  736. 'd= kv': function (buffer, m) {
  737. buffer.d = m;
  738. buffer.dType = 'kv';
  739. },
  740. 'charge or bond': function (buffer, m) {
  741. if (buffer['beginsWithBond']) {
  742. /** @type {ParserOutput[]} */
  743. var ret = [];
  744. mhchemParser.concatArray(ret, this['output'](buffer));
  745. mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
  746. return ret;
  747. } else {
  748. buffer.d = m;
  749. }
  750. },
  751. '- after o/d': function (buffer, m, isAfterD) {
  752. var c1 = mhchemParser.patterns.match_('orbital', buffer.o || "");
  753. var c2 = mhchemParser.patterns.match_('one lowercase greek letter $', buffer.o || "");
  754. var c3 = mhchemParser.patterns.match_('one lowercase latin letter $', buffer.o || "");
  755. var c4 = mhchemParser.patterns.match_('$one lowercase latin letter$ $', buffer.o || "");
  756. var hyphenFollows = m==="-" && ( c1 && c1.remainder==="" || c2 || c3 || c4 );
  757. if (hyphenFollows && !buffer.a && !buffer.b && !buffer.p && !buffer.d && !buffer.q && !c1 && c3) {
  758. buffer.o = '$' + buffer.o + '$';
  759. }
  760. /** @type {ParserOutput[]} */
  761. var ret = [];
  762. if (hyphenFollows) {
  763. mhchemParser.concatArray(ret, this['output'](buffer));
  764. ret.push({ type_: 'hyphen' });
  765. } else {
  766. c1 = mhchemParser.patterns.match_('digits', buffer.d || "");
  767. if (isAfterD && c1 && c1.remainder==='') {
  768. mhchemParser.concatArray(ret, mhchemParser.actions['d='](buffer, m));
  769. mhchemParser.concatArray(ret, this['output'](buffer));
  770. } else {
  771. mhchemParser.concatArray(ret, this['output'](buffer));
  772. mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
  773. }
  774. }
  775. return ret;
  776. },
  777. 'a to o': function (buffer) {
  778. buffer.o = buffer.a;
  779. buffer.a = undefined;
  780. },
  781. 'sb=true': function (buffer) { buffer.sb = true; },
  782. 'sb=false': function (buffer) { buffer.sb = false; },
  783. 'beginsWithBond=true': function (buffer) { buffer['beginsWithBond'] = true; },
  784. 'beginsWithBond=false': function (buffer) { buffer['beginsWithBond'] = false; },
  785. 'parenthesisLevel++': function (buffer) { buffer['parenthesisLevel']++; },
  786. 'parenthesisLevel--': function (buffer) { buffer['parenthesisLevel']--; },
  787. 'state of aggregation': function (buffer, m) {
  788. return { type_: 'state of aggregation', p1: mhchemParser.go(m, 'o') };
  789. },
  790. 'comma': function (buffer, m) {
  791. var a = m.replace(/\s*$/, '');
  792. var withSpace = (a !== m);
  793. if (withSpace && buffer['parenthesisLevel'] === 0) {
  794. return { type_: 'comma enumeration L', p1: a };
  795. } else {
  796. return { type_: 'comma enumeration M', p1: a };
  797. }
  798. },
  799. 'output': function (buffer, m, entityFollows) {
  800. // entityFollows:
  801. // undefined = if we have nothing else to output, also ignore the just read space (buffer.sb)
  802. // 1 = an entity follows, never omit the space if there was one just read before (can only apply to state 1)
  803. // 2 = 1 + the entity can have an amount, so output a\, instead of converting it to o (can only apply to states a|as)
  804. /** @type {ParserOutput | ParserOutput[]} */
  805. var ret;
  806. if (!buffer.r) {
  807. ret = [];
  808. if (!buffer.a && !buffer.b && !buffer.p && !buffer.o && !buffer.q && !buffer.d && !entityFollows) {
  809. //ret = [];
  810. } else {
  811. if (buffer.sb) {
  812. ret.push({ type_: 'entitySkip' });
  813. }
  814. if (!buffer.o && !buffer.q && !buffer.d && !buffer.b && !buffer.p && entityFollows!==2) {
  815. buffer.o = buffer.a;
  816. buffer.a = undefined;
  817. } else if (!buffer.o && !buffer.q && !buffer.d && (buffer.b || buffer.p)) {
  818. buffer.o = buffer.a;
  819. buffer.d = buffer.b;
  820. buffer.q = buffer.p;
  821. buffer.a = buffer.b = buffer.p = undefined;
  822. } else {
  823. if (buffer.o && buffer.dType==='kv' && mhchemParser.patterns.match_('d-oxidation$', buffer.d || "")) {
  824. buffer.dType = 'oxidation';
  825. } else if (buffer.o && buffer.dType==='kv' && !buffer.q) {
  826. buffer.dType = undefined;
  827. }
  828. }
  829. ret.push({
  830. type_: 'chemfive',
  831. a: mhchemParser.go(buffer.a, 'a'),
  832. b: mhchemParser.go(buffer.b, 'bd'),
  833. p: mhchemParser.go(buffer.p, 'pq'),
  834. o: mhchemParser.go(buffer.o, 'o'),
  835. q: mhchemParser.go(buffer.q, 'pq'),
  836. d: mhchemParser.go(buffer.d, (buffer.dType === 'oxidation' ? 'oxidation' : 'bd')),
  837. dType: buffer.dType
  838. });
  839. }
  840. } else { // r
  841. /** @type {ParserOutput[]} */
  842. var rd;
  843. if (buffer.rdt === 'M') {
  844. rd = mhchemParser.go(buffer.rd, 'tex-math');
  845. } else if (buffer.rdt === 'T') {
  846. rd = [ { type_: 'text', p1: buffer.rd || "" } ];
  847. } else {
  848. rd = mhchemParser.go(buffer.rd);
  849. }
  850. /** @type {ParserOutput[]} */
  851. var rq;
  852. if (buffer.rqt === 'M') {
  853. rq = mhchemParser.go(buffer.rq, 'tex-math');
  854. } else if (buffer.rqt === 'T') {
  855. rq = [ { type_: 'text', p1: buffer.rq || ""} ];
  856. } else {
  857. rq = mhchemParser.go(buffer.rq);
  858. }
  859. ret = {
  860. type_: 'arrow',
  861. r: buffer.r,
  862. rd: rd,
  863. rq: rq
  864. };
  865. }
  866. for (var p in buffer) {
  867. if (p !== 'parenthesisLevel' && p !== 'beginsWithBond') {
  868. delete buffer[p];
  869. }
  870. }
  871. return ret;
  872. },
  873. 'oxidation-output': function (buffer, m) {
  874. var ret = [ "{" ];
  875. mhchemParser.concatArray(ret, mhchemParser.go(m, 'oxidation'));
  876. ret.push("}");
  877. return ret;
  878. },
  879. 'frac-output': function (buffer, m) {
  880. return { type_: 'frac-ce', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
  881. },
  882. 'overset-output': function (buffer, m) {
  883. return { type_: 'overset', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
  884. },
  885. 'underset-output': function (buffer, m) {
  886. return { type_: 'underset', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
  887. },
  888. 'underbrace-output': function (buffer, m) {
  889. return { type_: 'underbrace', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
  890. },
  891. 'color-output': function (buffer, m) {
  892. return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1]) };
  893. },
  894. 'r=': function (buffer, m) { buffer.r = m; },
  895. 'rdt=': function (buffer, m) { buffer.rdt = m; },
  896. 'rd=': function (buffer, m) { buffer.rd = m; },
  897. 'rqt=': function (buffer, m) { buffer.rqt = m; },
  898. 'rq=': function (buffer, m) { buffer.rq = m; },
  899. 'operator': function (buffer, m, p1) { return { type_: 'operator', kind_: (p1 || m) }; }
  900. }
  901. },
  902. 'a': {
  903. transitions: mhchemParser.createTransitions({
  904. 'empty': {
  905. '*': {} },
  906. '1/2$': {
  907. '0': { action_: '1/2' } },
  908. 'else': {
  909. '0': { nextState: '1', revisit: true } },
  910. '$(...)$': {
  911. '*': { action_: 'tex-math tight', nextState: '1' } },
  912. ',': {
  913. '*': { action_: { type_: 'insert', option: 'commaDecimal' } } },
  914. 'else2': {
  915. '*': { action_: 'copy' } }
  916. }),
  917. actions: {}
  918. },
  919. 'o': {
  920. transitions: mhchemParser.createTransitions({
  921. 'empty': {
  922. '*': {} },
  923. '1/2$': {
  924. '0': { action_: '1/2' } },
  925. 'else': {
  926. '0': { nextState: '1', revisit: true } },
  927. 'letters': {
  928. '*': { action_: 'rm' } },
  929. '\\ca': {
  930. '*': { action_: { type_: 'insert', option: 'circa' } } },
  931. '\\x{}{}|\\x{}|\\x': {
  932. '*': { action_: 'copy' } },
  933. '${(...)}$|$(...)$': {
  934. '*': { action_: 'tex-math' } },
  935. '{(...)}': {
  936. '*': { action_: '{text}' } },
  937. 'else2': {
  938. '*': { action_: 'copy' } }
  939. }),
  940. actions: {}
  941. },
  942. 'text': {
  943. transitions: mhchemParser.createTransitions({
  944. 'empty': {
  945. '*': { action_: 'output' } },
  946. '{...}': {
  947. '*': { action_: 'text=' } },
  948. '${(...)}$|$(...)$': {
  949. '*': { action_: 'tex-math' } },
  950. '\\greek': {
  951. '*': { action_: [ 'output', 'rm' ] } },
  952. '\\,|\\x{}{}|\\x{}|\\x': {
  953. '*': { action_: [ 'output', 'copy' ] } },
  954. 'else': {
  955. '*': { action_: 'text=' } }
  956. }),
  957. actions: {
  958. 'output': function (buffer) {
  959. if (buffer.text_) {
  960. /** @type {ParserOutput} */
  961. var ret = { type_: 'text', p1: buffer.text_ };
  962. for (var p in buffer) { delete buffer[p]; }
  963. return ret;
  964. }
  965. }
  966. }
  967. },
  968. 'pq': {
  969. transitions: mhchemParser.createTransitions({
  970. 'empty': {
  971. '*': {} },
  972. 'state of aggregation $': {
  973. '*': { action_: 'state of aggregation' } },
  974. 'i$': {
  975. '0': { nextState: '!f', revisit: true } },
  976. '(KV letters),': {
  977. '0': { action_: 'rm', nextState: '0' } },
  978. 'formula$': {
  979. '0': { nextState: 'f', revisit: true } },
  980. '1/2$': {
  981. '0': { action_: '1/2' } },
  982. 'else': {
  983. '0': { nextState: '!f', revisit: true } },
  984. '${(...)}$|$(...)$': {
  985. '*': { action_: 'tex-math' } },
  986. '{(...)}': {
  987. '*': { action_: 'text' } },
  988. 'a-z': {
  989. 'f': { action_: 'tex-math' } },
  990. 'letters': {
  991. '*': { action_: 'rm' } },
  992. '-9.,9': {
  993. '*': { action_: '9,9' } },
  994. ',': {
  995. '*': { action_: { type_: 'insert+p1', option: 'comma enumeration S' } } },
  996. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  997. '*': { action_: 'color-output' } },
  998. '\\color{(...)}0': {
  999. '*': { action_: 'color0-output' } },
  1000. '\\ce{(...)}': {
  1001. '*': { action_: 'ce' } },
  1002. '\\,|\\x{}{}|\\x{}|\\x': {
  1003. '*': { action_: 'copy' } },
  1004. 'else2': {
  1005. '*': { action_: 'copy' } }
  1006. }),
  1007. actions: {
  1008. 'state of aggregation': function (buffer, m) {
  1009. return { type_: 'state of aggregation subscript', p1: mhchemParser.go(m, 'o') };
  1010. },
  1011. 'color-output': function (buffer, m) {
  1012. return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1], 'pq') };
  1013. }
  1014. }
  1015. },
  1016. 'bd': {
  1017. transitions: mhchemParser.createTransitions({
  1018. 'empty': {
  1019. '*': {} },
  1020. 'x$': {
  1021. '0': { nextState: '!f', revisit: true } },
  1022. 'formula$': {
  1023. '0': { nextState: 'f', revisit: true } },
  1024. 'else': {
  1025. '0': { nextState: '!f', revisit: true } },
  1026. '-9.,9 no missing 0': {
  1027. '*': { action_: '9,9' } },
  1028. '.': {
  1029. '*': { action_: { type_: 'insert', option: 'electron dot' } } },
  1030. 'a-z': {
  1031. 'f': { action_: 'tex-math' } },
  1032. 'x': {
  1033. '*': { action_: { type_: 'insert', option: 'KV x' } } },
  1034. 'letters': {
  1035. '*': { action_: 'rm' } },
  1036. '\'': {
  1037. '*': { action_: { type_: 'insert', option: 'prime' } } },
  1038. '${(...)}$|$(...)$': {
  1039. '*': { action_: 'tex-math' } },
  1040. '{(...)}': {
  1041. '*': { action_: 'text' } },
  1042. '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
  1043. '*': { action_: 'color-output' } },
  1044. '\\color{(...)}0': {
  1045. '*': { action_: 'color0-output' } },
  1046. '\\ce{(...)}': {
  1047. '*': { action_: 'ce' } },
  1048. '\\,|\\x{}{}|\\x{}|\\x': {
  1049. '*': { action_: 'copy' } },
  1050. 'else2': {
  1051. '*': { action_: 'copy' } }
  1052. }),
  1053. actions: {
  1054. 'color-output': function (buffer, m) {
  1055. return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1], 'bd') };
  1056. }
  1057. }
  1058. },
  1059. 'oxidation': {
  1060. transitions: mhchemParser.createTransitions({
  1061. 'empty': {
  1062. '*': {} },
  1063. 'roman numeral': {
  1064. '*': { action_: 'roman-numeral' } },
  1065. '${(...)}$|$(...)$': {
  1066. '*': { action_: 'tex-math' } },
  1067. 'else': {
  1068. '*': { action_: 'copy' } }
  1069. }),
  1070. actions: {
  1071. 'roman-numeral': function (buffer, m) { return { type_: 'roman numeral', p1: m || "" }; }
  1072. }
  1073. },
  1074. 'tex-math': {
  1075. transitions: mhchemParser.createTransitions({
  1076. 'empty': {
  1077. '*': { action_: 'output' } },
  1078. '\\ce{(...)}': {
  1079. '*': { action_: [ 'output', 'ce' ] } },
  1080. '{...}|\\,|\\x{}{}|\\x{}|\\x': {
  1081. '*': { action_: 'o=' } },
  1082. 'else': {
  1083. '*': { action_: 'o=' } }
  1084. }),
  1085. actions: {
  1086. 'output': function (buffer) {
  1087. if (buffer.o) {
  1088. /** @type {ParserOutput} */
  1089. var ret = { type_: 'tex-math', p1: buffer.o };
  1090. for (var p in buffer) { delete buffer[p]; }
  1091. return ret;
  1092. }
  1093. }
  1094. }
  1095. },
  1096. 'tex-math tight': {
  1097. transitions: mhchemParser.createTransitions({
  1098. 'empty': {
  1099. '*': { action_: 'output' } },
  1100. '\\ce{(...)}': {
  1101. '*': { action_: [ 'output', 'ce' ] } },
  1102. '{...}|\\,|\\x{}{}|\\x{}|\\x': {
  1103. '*': { action_: 'o=' } },
  1104. '-|+': {
  1105. '*': { action_: 'tight operator' } },
  1106. 'else': {
  1107. '*': { action_: 'o=' } }
  1108. }),
  1109. actions: {
  1110. 'tight operator': function (buffer, m) { buffer.o = (buffer.o || "") + "{"+m+"}"; },
  1111. 'output': function (buffer) {
  1112. if (buffer.o) {
  1113. /** @type {ParserOutput} */
  1114. var ret = { type_: 'tex-math', p1: buffer.o };
  1115. for (var p in buffer) { delete buffer[p]; }
  1116. return ret;
  1117. }
  1118. }
  1119. }
  1120. },
  1121. '9,9': {
  1122. transitions: mhchemParser.createTransitions({
  1123. 'empty': {
  1124. '*': {} },
  1125. ',': {
  1126. '*': { action_: 'comma' } },
  1127. 'else': {
  1128. '*': { action_: 'copy' } }
  1129. }),
  1130. actions: {
  1131. 'comma': function () { return { type_: 'commaDecimal' }; }
  1132. }
  1133. },
  1134. //#endregion
  1135. //
  1136. // \pu state machines
  1137. //
  1138. //#region pu
  1139. 'pu': {
  1140. transitions: mhchemParser.createTransitions({
  1141. 'empty': {
  1142. '*': { action_: 'output' } },
  1143. 'space$': {
  1144. '*': { action_: [ 'output', 'space' ] } },
  1145. '{[(|)]}': {
  1146. '0|a': { action_: 'copy' } },
  1147. '(-)(9)^(-9)': {
  1148. '0': { action_: 'number^', nextState: 'a' } },
  1149. '(-)(9.,9)(e)(99)': {
  1150. '0': { action_: 'enumber', nextState: 'a' } },
  1151. 'space': {
  1152. '0|a': {} },
  1153. 'pm-operator': {
  1154. '0|a': { action_: { type_: 'operator', option: '\\pm' }, nextState: '0' } },
  1155. 'operator': {
  1156. '0|a': { action_: 'copy', nextState: '0' } },
  1157. '//': {
  1158. 'd': { action_: 'o=', nextState: '/' } },
  1159. '/': {
  1160. 'd': { action_: 'o=', nextState: '/' } },
  1161. '{...}|else': {
  1162. '0|d': { action_: 'd=', nextState: 'd' },
  1163. 'a': { action_: [ 'space', 'd=' ], nextState: 'd' },
  1164. '/|q': { action_: 'q=', nextState: 'q' } }
  1165. }),
  1166. actions: {
  1167. 'enumber': function (buffer, m) {
  1168. /** @type {ParserOutput[]} */
  1169. var ret = [];
  1170. if (m[0] === "+-" || m[0] === "+/-") {
  1171. ret.push("\\pm ");
  1172. } else if (m[0]) {
  1173. ret.push(m[0]);
  1174. }
  1175. if (m[1]) {
  1176. mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
  1177. if (m[2]) {
  1178. if (m[2].match(/[,.]/)) {
  1179. mhchemParser.concatArray(ret, mhchemParser.go(m[2], 'pu-9,9'));
  1180. } else {
  1181. ret.push(m[2]);
  1182. }
  1183. }
  1184. m[3] = m[4] || m[3];
  1185. if (m[3]) {
  1186. m[3] = m[3].trim();
  1187. if (m[3] === "e" || m[3].substr(0, 1) === "*") {
  1188. ret.push({ type_: 'cdot' });
  1189. } else {
  1190. ret.push({ type_: 'times' });
  1191. }
  1192. }
  1193. }
  1194. if (m[3]) {
  1195. ret.push("10^{"+m[5]+"}");
  1196. }
  1197. return ret;
  1198. },
  1199. 'number^': function (buffer, m) {
  1200. /** @type {ParserOutput[]} */
  1201. var ret = [];
  1202. if (m[0] === "+-" || m[0] === "+/-") {
  1203. ret.push("\\pm ");
  1204. } else if (m[0]) {
  1205. ret.push(m[0]);
  1206. }
  1207. mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
  1208. ret.push("^{"+m[2]+"}");
  1209. return ret;
  1210. },
  1211. 'operator': function (buffer, m, p1) { return { type_: 'operator', kind_: (p1 || m) }; },
  1212. 'space': function () { return { type_: 'pu-space-1' }; },
  1213. 'output': function (buffer) {
  1214. /** @type {ParserOutput | ParserOutput[]} */
  1215. var ret;
  1216. var md = mhchemParser.patterns.match_('{(...)}', buffer.d || "");
  1217. if (md && md.remainder === '') { buffer.d = md.match_; }
  1218. var mq = mhchemParser.patterns.match_('{(...)}', buffer.q || "");
  1219. if (mq && mq.remainder === '') { buffer.q = mq.match_; }
  1220. if (buffer.d) {
  1221. buffer.d = buffer.d.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
  1222. buffer.d = buffer.d.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
  1223. }
  1224. if (buffer.q) { // fraction
  1225. buffer.q = buffer.q.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
  1226. buffer.q = buffer.q.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
  1227. var b5 = {
  1228. d: mhchemParser.go(buffer.d, 'pu'),
  1229. q: mhchemParser.go(buffer.q, 'pu')
  1230. };
  1231. if (buffer.o === '//') {
  1232. ret = { type_: 'pu-frac', p1: b5.d, p2: b5.q };
  1233. } else {
  1234. ret = b5.d;
  1235. if (b5.d.length > 1 || b5.q.length > 1) {
  1236. ret.push({ type_: ' / ' });
  1237. } else {
  1238. ret.push({ type_: '/' });
  1239. }
  1240. mhchemParser.concatArray(ret, b5.q);
  1241. }
  1242. } else { // no fraction
  1243. ret = mhchemParser.go(buffer.d, 'pu-2');
  1244. }
  1245. for (var p in buffer) { delete buffer[p]; }
  1246. return ret;
  1247. }
  1248. }
  1249. },
  1250. 'pu-2': {
  1251. transitions: mhchemParser.createTransitions({
  1252. 'empty': {
  1253. '*': { action_: 'output' } },
  1254. '*': {
  1255. '*': { action_: [ 'output', 'cdot' ], nextState: '0' } },
  1256. '\\x': {
  1257. '*': { action_: 'rm=' } },
  1258. 'space': {
  1259. '*': { action_: [ 'output', 'space' ], nextState: '0' } },
  1260. '^{(...)}|^(-1)': {
  1261. '1': { action_: '^(-1)' } },
  1262. '-9.,9': {
  1263. '0': { action_: 'rm=', nextState: '0' },
  1264. '1': { action_: '^(-1)', nextState: '0' } },
  1265. '{...}|else': {
  1266. '*': { action_: 'rm=', nextState: '1' } }
  1267. }),
  1268. actions: {
  1269. 'cdot': function () { return { type_: 'tight cdot' }; },
  1270. '^(-1)': function (buffer, m) { buffer.rm += "^{"+m+"}"; },
  1271. 'space': function () { return { type_: 'pu-space-2' }; },
  1272. 'output': function (buffer) {
  1273. /** @type {ParserOutput | ParserOutput[]} */
  1274. var ret = [];
  1275. if (buffer.rm) {
  1276. var mrm = mhchemParser.patterns.match_('{(...)}', buffer.rm || "");
  1277. if (mrm && mrm.remainder === '') {
  1278. ret = mhchemParser.go(mrm.match_, 'pu');
  1279. } else {
  1280. ret = { type_: 'rm', p1: buffer.rm };
  1281. }
  1282. }
  1283. for (var p in buffer) { delete buffer[p]; }
  1284. return ret;
  1285. }
  1286. }
  1287. },
  1288. 'pu-9,9': {
  1289. transitions: mhchemParser.createTransitions({
  1290. 'empty': {
  1291. '0': { action_: 'output-0' },
  1292. 'o': { action_: 'output-o' } },
  1293. ',': {
  1294. '0': { action_: [ 'output-0', 'comma' ], nextState: 'o' } },
  1295. '.': {
  1296. '0': { action_: [ 'output-0', 'copy' ], nextState: 'o' } },
  1297. 'else': {
  1298. '*': { action_: 'text=' } }
  1299. }),
  1300. actions: {
  1301. 'comma': function () { return { type_: 'commaDecimal' }; },
  1302. 'output-0': function (buffer) {
  1303. /** @type {ParserOutput[]} */
  1304. var ret = [];
  1305. buffer.text_ = buffer.text_ || "";
  1306. if (buffer.text_.length > 4) {
  1307. var a = buffer.text_.length % 3;
  1308. if (a === 0) { a = 3; }
  1309. for (var i=buffer.text_.length-3; i>0; i-=3) {
  1310. ret.push(buffer.text_.substr(i, 3));
  1311. ret.push({ type_: '1000 separator' });
  1312. }
  1313. ret.push(buffer.text_.substr(0, a));
  1314. ret.reverse();
  1315. } else {
  1316. ret.push(buffer.text_);
  1317. }
  1318. for (var p in buffer) { delete buffer[p]; }
  1319. return ret;
  1320. },
  1321. 'output-o': function (buffer) {
  1322. /** @type {ParserOutput[]} */
  1323. var ret = [];
  1324. buffer.text_ = buffer.text_ || "";
  1325. if (buffer.text_.length > 4) {
  1326. var a = buffer.text_.length - 3;
  1327. for (var i=0; i<a; i+=3) {
  1328. ret.push(buffer.text_.substr(i, 3));
  1329. ret.push({ type_: '1000 separator' });
  1330. }
  1331. ret.push(buffer.text_.substr(i));
  1332. } else {
  1333. ret.push(buffer.text_);
  1334. }
  1335. for (var p in buffer) { delete buffer[p]; }
  1336. return ret;
  1337. }
  1338. }
  1339. }
  1340. //#endregion
  1341. };
  1342. //
  1343. // texify: Take MhchemParser output and convert it to TeX
  1344. //
  1345. /** @type {Texify} */
  1346. var texify = {
  1347. go: function (input, isInner) { // (recursive, max 4 levels)
  1348. if (!input) { return ""; }
  1349. var res = "";
  1350. var cee = false;
  1351. for (var i=0; i < input.length; i++) {
  1352. var inputi = input[i];
  1353. if (typeof inputi === "string") {
  1354. res += inputi;
  1355. } else {
  1356. res += texify._go2(inputi);
  1357. if (inputi.type_ === '1st-level escape') { cee = true; }
  1358. }
  1359. }
  1360. if (!isInner && !cee && res) {
  1361. res = "{" + res + "}";
  1362. }
  1363. return res;
  1364. },
  1365. _goInner: function (input) {
  1366. if (!input) { return input; }
  1367. return texify.go(input, true);
  1368. },
  1369. _go2: function (buf) {
  1370. /** @type {undefined | string} */
  1371. var res;
  1372. switch (buf.type_) {
  1373. case 'chemfive':
  1374. res = "";
  1375. var b5 = {
  1376. a: texify._goInner(buf.a),
  1377. b: texify._goInner(buf.b),
  1378. p: texify._goInner(buf.p),
  1379. o: texify._goInner(buf.o),
  1380. q: texify._goInner(buf.q),
  1381. d: texify._goInner(buf.d)
  1382. };
  1383. //
  1384. // a
  1385. //
  1386. if (b5.a) {
  1387. if (b5.a.match(/^[+\-]/)) { b5.a = "{"+b5.a+"}"; }
  1388. res += b5.a + "\\,";
  1389. }
  1390. //
  1391. // b and p
  1392. //
  1393. if (b5.b || b5.p) {
  1394. res += "{\\vphantom{X}}";
  1395. res += "^{\\hphantom{"+(b5.b||"")+"}}_{\\hphantom{"+(b5.p||"")+"}}";
  1396. res += "{\\vphantom{X}}";
  1397. res += "^{\\smash[t]{\\vphantom{2}}\\mathllap{"+(b5.b||"")+"}}";
  1398. res += "_{\\vphantom{2}\\mathllap{\\smash[t]{"+(b5.p||"")+"}}}";
  1399. }
  1400. //
  1401. // o
  1402. //
  1403. if (b5.o) {
  1404. if (b5.o.match(/^[+\-]/)) { b5.o = "{"+b5.o+"}"; }
  1405. res += b5.o;
  1406. }
  1407. //
  1408. // q and d
  1409. //
  1410. if (buf.dType === 'kv') {
  1411. if (b5.d || b5.q) {
  1412. res += "{\\vphantom{X}}";
  1413. }
  1414. if (b5.d) {
  1415. res += "^{"+b5.d+"}";
  1416. }
  1417. if (b5.q) {
  1418. res += "_{\\smash[t]{"+b5.q+"}}";
  1419. }
  1420. } else if (buf.dType === 'oxidation') {
  1421. if (b5.d) {
  1422. res += "{\\vphantom{X}}";
  1423. res += "^{"+b5.d+"}";
  1424. }
  1425. if (b5.q) {
  1426. res += "{\\vphantom{X}}";
  1427. res += "_{\\smash[t]{"+b5.q+"}}";
  1428. }
  1429. } else {
  1430. if (b5.q) {
  1431. res += "{\\vphantom{X}}";
  1432. res += "_{\\smash[t]{"+b5.q+"}}";
  1433. }
  1434. if (b5.d) {
  1435. res += "{\\vphantom{X}}";
  1436. res += "^{"+b5.d+"}";
  1437. }
  1438. }
  1439. break;
  1440. case 'rm':
  1441. res = "\\mathrm{"+buf.p1+"}";
  1442. break;
  1443. case 'text':
  1444. if (buf.p1.match(/[\^_]/)) {
  1445. buf.p1 = buf.p1.replace(" ", "~").replace("-", "\\text{-}");
  1446. res = "\\mathrm{"+buf.p1+"}";
  1447. } else {
  1448. res = "\\text{"+buf.p1+"}";
  1449. }
  1450. break;
  1451. case 'roman numeral':
  1452. res = "\\mathrm{"+buf.p1+"}";
  1453. break;
  1454. case 'state of aggregation':
  1455. res = "\\mskip2mu "+texify._goInner(buf.p1);
  1456. break;
  1457. case 'state of aggregation subscript':
  1458. res = "\\mskip1mu "+texify._goInner(buf.p1);
  1459. break;
  1460. case 'bond':
  1461. res = texify._getBond(buf.kind_);
  1462. if (!res) {
  1463. throw ["MhchemErrorBond", "mhchem Error. Unknown bond type (" + buf.kind_ + ")"];
  1464. }
  1465. break;
  1466. case 'frac':
  1467. var c = "\\frac{" + buf.p1 + "}{" + buf.p2 + "}";
  1468. res = "\\mathchoice{\\textstyle"+c+"}{"+c+"}{"+c+"}{"+c+"}";
  1469. break;
  1470. case 'pu-frac':
  1471. var d = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  1472. res = "\\mathchoice{\\textstyle"+d+"}{"+d+"}{"+d+"}{"+d+"}";
  1473. break;
  1474. case 'tex-math':
  1475. res = buf.p1 + " ";
  1476. break;
  1477. case 'frac-ce':
  1478. res = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  1479. break;
  1480. case 'overset':
  1481. res = "\\overset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  1482. break;
  1483. case 'underset':
  1484. res = "\\underset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
  1485. break;
  1486. case 'underbrace':
  1487. res = "\\underbrace{" + texify._goInner(buf.p1) + "}_{" + texify._goInner(buf.p2) + "}";
  1488. break;
  1489. case 'color':
  1490. res = "{\\color{" + buf.color1 + "}{" + texify._goInner(buf.color2) + "}}";
  1491. break;
  1492. case 'color0':
  1493. res = "\\color{" + buf.color + "}";
  1494. break;
  1495. case 'arrow':
  1496. var b6 = {
  1497. rd: texify._goInner(buf.rd),
  1498. rq: texify._goInner(buf.rq)
  1499. };
  1500. var arrow = "\\x" + texify._getArrow(buf.r);
  1501. if (b6.rq) { arrow += "[{" + b6.rq + "}]"; }
  1502. if (b6.rd) {
  1503. arrow += "{" + b6.rd + "}";
  1504. } else {
  1505. arrow += "{}";
  1506. }
  1507. res = arrow;
  1508. break;
  1509. case 'operator':
  1510. res = texify._getOperator(buf.kind_);
  1511. break;
  1512. case '1st-level escape':
  1513. res = buf.p1+" "; // &, \\\\, \\hlin
  1514. break;
  1515. case 'space':
  1516. res = " ";
  1517. break;
  1518. case 'entitySkip':
  1519. res = "~";
  1520. break;
  1521. case 'pu-space-1':
  1522. res = "~";
  1523. break;
  1524. case 'pu-space-2':
  1525. res = "\\mkern3mu ";
  1526. break;
  1527. case '1000 separator':
  1528. res = "\\mkern2mu ";
  1529. break;
  1530. case 'commaDecimal':
  1531. res = "{,}";
  1532. break;
  1533. case 'comma enumeration L':
  1534. res = "{"+buf.p1+"}\\mkern6mu ";
  1535. break;
  1536. case 'comma enumeration M':
  1537. res = "{"+buf.p1+"}\\mkern3mu ";
  1538. break;
  1539. case 'comma enumeration S':
  1540. res = "{"+buf.p1+"}\\mkern1mu ";
  1541. break;
  1542. case 'hyphen':
  1543. res = "\\text{-}";
  1544. break;
  1545. case 'addition compound':
  1546. res = "\\,{\\cdot}\\,";
  1547. break;
  1548. case 'electron dot':
  1549. res = "\\mkern1mu \\bullet\\mkern1mu ";
  1550. break;
  1551. case 'KV x':
  1552. res = "{\\times}";
  1553. break;
  1554. case 'prime':
  1555. res = "\\prime ";
  1556. break;
  1557. case 'cdot':
  1558. res = "\\cdot ";
  1559. break;
  1560. case 'tight cdot':
  1561. res = "\\mkern1mu{\\cdot}\\mkern1mu ";
  1562. break;
  1563. case 'times':
  1564. res = "\\times ";
  1565. break;
  1566. case 'circa':
  1567. res = "{\\sim}";
  1568. break;
  1569. case '^':
  1570. res = "uparrow";
  1571. break;
  1572. case 'v':
  1573. res = "downarrow";
  1574. break;
  1575. case 'ellipsis':
  1576. res = "\\ldots ";
  1577. break;
  1578. case '/':
  1579. res = "/";
  1580. break;
  1581. case ' / ':
  1582. res = "\\,/\\,";
  1583. break;
  1584. default:
  1585. assertNever(buf);
  1586. throw ["MhchemBugT", "mhchem bug T. Please report."]; // Missing texify rule or unknown MhchemParser output
  1587. }
  1588. assertString(res);
  1589. return res;
  1590. },
  1591. _getArrow: function (a) {
  1592. switch (a) {
  1593. case "->": return "rightarrow";
  1594. case "\u2192": return "rightarrow";
  1595. case "\u27F6": return "rightarrow";
  1596. case "<-": return "leftarrow";
  1597. case "<->": return "leftrightarrow";
  1598. case "<-->": return "rightleftarrows";
  1599. case "<=>": return "rightleftharpoons";
  1600. case "\u21CC": return "rightleftharpoons";
  1601. case "<=>>": return "rightequilibrium";
  1602. case "<<=>": return "leftequilibrium";
  1603. default:
  1604. assertNever(a);
  1605. throw ["MhchemBugT", "mhchem bug T. Please report."];
  1606. }
  1607. },
  1608. _getBond: function (a) {
  1609. switch (a) {
  1610. case "-": return "{-}";
  1611. case "1": return "{-}";
  1612. case "=": return "{=}";
  1613. case "2": return "{=}";
  1614. case "#": return "{\\equiv}";
  1615. case "3": return "{\\equiv}";
  1616. case "~": return "{\\tripledash}";
  1617. case "~-": return "{\\mathrlap{\\raisebox{-.1em}{$-$}}\\raisebox{.1em}{$\\tripledash$}}";
  1618. case "~=": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  1619. case "~--": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  1620. case "-~-": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$-$}}\\tripledash}";
  1621. case "...": return "{{\\cdot}{\\cdot}{\\cdot}}";
  1622. case "....": return "{{\\cdot}{\\cdot}{\\cdot}{\\cdot}}";
  1623. case "->": return "{\\rightarrow}";
  1624. case "<-": return "{\\leftarrow}";
  1625. case "<": return "{<}";
  1626. case ">": return "{>}";
  1627. default:
  1628. assertNever(a);
  1629. throw ["MhchemBugT", "mhchem bug T. Please report."];
  1630. }
  1631. },
  1632. _getOperator: function (a) {
  1633. switch (a) {
  1634. case "+": return " {}+{} ";
  1635. case "-": return " {}-{} ";
  1636. case "=": return " {}={} ";
  1637. case "<": return " {}<{} ";
  1638. case ">": return " {}>{} ";
  1639. case "<<": return " {}\\ll{} ";
  1640. case ">>": return " {}\\gg{} ";
  1641. case "\\pm": return " {}\\pm{} ";
  1642. case "\\approx": return " {}\\approx{} ";
  1643. case "$\\approx$": return " {}\\approx{} ";
  1644. case "v": return " \\downarrow{} ";
  1645. case "(v)": return " \\downarrow{} ";
  1646. case "^": return " \\uparrow{} ";
  1647. case "(^)": return " \\uparrow{} ";
  1648. default:
  1649. assertNever(a);
  1650. throw ["MhchemBugT", "mhchem bug T. Please report."];
  1651. }
  1652. }
  1653. };
  1654. //
  1655. // Helpers for code anaylsis
  1656. // Will show type error at calling position
  1657. //
  1658. /** @param {number} a */
  1659. function assertNever(a) {}
  1660. /** @param {string} a */
  1661. function assertString(a) {}