mhchemParser.js 76 KB

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