mhchem.js 77 KB

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