function mkStex(mathMode){function pushCommand(state,command){state.cmdState.push(command)}function peekCommand(state){return state.cmdState.length>0?state.cmdState[state.cmdState.length-1]:null}function addPluginPattern(pluginName,cmdStyle,styles){return function(){this.name=pluginName,this.bracketNo=0,this.style=cmdStyle,this.styles=styles,this.argument=null,this.styleIdentifier=function(){return this.styles[this.bracketNo-1]||null},this.openBracket=function(){return this.bracketNo++,"bracket"},this.closeBracket=function(){}}}var plugins={};function setState(state,f){state.f=f}function normal(source,state){var plug;if(source.match(/^\\[a-zA-Z@\xc0-\u1fff\u2060-\uffff]+/)){var cmdName=source.current().slice(1);return pushCommand(state,plug=new(plug=plugins.hasOwnProperty(cmdName)?plugins[cmdName]:plugins.DEFAULT)),setState(state,beginParams),plug.style}if(source.match(/^\\[$&%#{}_]/))return"tag";if(source.match(/^\\[,;!\/\\]/))return"tag";if(source.match("\\["))return setState(state,function(source2,state2){return inMathMode(source2,state2,"\\]")}),"keyword";if(source.match("\\("))return setState(state,function(source2,state2){return inMathMode(source2,state2,"\\)")}),"keyword";if(source.match("$$"))return setState(state,function(source2,state2){return inMathMode(source2,state2,"$$")}),"keyword";if(source.match("$"))return setState(state,function(source2,state2){return inMathMode(source2,state2,"$")}),"keyword";var ch=source.next();return"%"==ch?(source.skipToEnd(),"comment"):"}"==ch||"]"==ch?(plug=peekCommand(state))?(plug.closeBracket(ch),setState(state,beginParams),"bracket"):"error":"{"==ch||"["==ch?(pushCommand(state,plug=new(plug=plugins.DEFAULT)),"bracket"):/\d/.test(ch)?(source.eatWhile(/[\w.%]/),"atom"):(source.eatWhile(/[\w\-_]/),plug=function(state){for(var context=state.cmdState,i=context.length-1;i>=0;i--){var plug=context[i];if("DEFAULT"!=plug.name)return plug}return{styleIdentifier:function(){return null}}}(state),"begin"==plug.name&&(plug.argument=source.current()),plug.styleIdentifier())}function inMathMode(source,state,endModeSeq){if(source.eatSpace())return null;if(endModeSeq&&source.match(endModeSeq))return setState(state,normal),"keyword";if(source.match(/^\\[a-zA-Z@]+/))return"tag";if(source.match(/^[a-zA-Z]+/))return"variableName.special";if(source.match(/^\\[$&%#{}_]/))return"tag";if(source.match(/^\\[,;!\/]/))return"tag";if(source.match(/^[\^_&]/))return"tag";if(source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/))return null;if(source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/))return"number";var ch=source.next();return"{"==ch||"}"==ch||"["==ch||"]"==ch||"("==ch||")"==ch?"bracket":"%"==ch?(source.skipToEnd(),"comment"):"error"}function beginParams(source,state){var ch=source.peek();return"{"==ch||"["==ch?(peekCommand(state).openBracket(ch),source.eat(ch),setState(state,normal),"bracket"):/[ \t\r]/.test(ch)?(source.eat(ch),null):(setState(state,normal),function(state){var plug=state.cmdState.pop();plug&&plug.closeBracket()}(state),normal(source,state))}return plugins.importmodule=addPluginPattern("importmodule","tag",["string","builtin"]),plugins.documentclass=addPluginPattern("documentclass","tag",["","atom"]),plugins.usepackage=addPluginPattern("usepackage","tag",["atom"]),plugins.begin=addPluginPattern("begin","tag",["atom"]),plugins.end=addPluginPattern("end","tag",["atom"]),plugins.label=addPluginPattern("label","tag",["atom"]),plugins.ref=addPluginPattern("ref","tag",["atom"]),plugins.eqref=addPluginPattern("eqref","tag",["atom"]),plugins.cite=addPluginPattern("cite","tag",["atom"]),plugins.bibitem=addPluginPattern("bibitem","tag",["atom"]),plugins.Bibitem=addPluginPattern("Bibitem","tag",["atom"]),plugins.RBibitem=addPluginPattern("RBibitem","tag",["atom"]),plugins.DEFAULT=function(){this.name="DEFAULT",this.style="tag",this.styleIdentifier=this.openBracket=this.closeBracket=function(){}},{name:"stex",startState:function(){return{cmdState:[],f:mathMode?function(source,state){return inMathMode(source,state)}:normal}},copyState:function(s){return{cmdState:s.cmdState.slice(),f:s.f}},token:function(stream,state){return state.f(stream,state)},blankLine:function(state){state.f=normal,state.cmdState.length=0},languageData:{commentTokens:{line:"%"}}}}const stex=mkStex(!1),stexMath=mkStex(!0);export{stex,stexMath};
