mhchemParser.js 76 KB

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