function mkStex(e) {
  function t(e2, t2) {
    e2.cmdState.push(t2);
  }
  function n(e2) {
    return e2.cmdState.length > 0 ? e2.cmdState[e2.cmdState.length - 1] : null;
  }
  function r(e2) {
    var t2 = e2.cmdState.pop();
    t2 && t2.closeBracket();
  }
  function i(e2) {
    for (var t2 = e2.cmdState, n2 = t2.length - 1; n2 >= 0; n2--) {
      var r2 = t2[n2];
      if (r2.name != "DEFAULT") return r2;
    }
    return { styleIdentifier: function() {
      return null;
    } };
  }
  function a(e2, t2, n2) {
    return function() {
      this.name = e2, this.bracketNo = 0, this.style = t2, this.styles = n2, this.argument = null, this.styleIdentifier = function() {
        return this.styles[this.bracketNo - 1] || null;
      }, this.openBracket = function() {
        return this.bracketNo++, "bracket";
      }, this.closeBracket = function() {
      };
    };
  }
  var o = {};
  o.importmodule = a("importmodule", "tag", ["string", "builtin"]), o.documentclass = a("documentclass", "tag", ["", "atom"]), o.usepackage = a("usepackage", "tag", ["atom"]), o.begin = a("begin", "tag", ["atom"]), o.end = a("end", "tag", ["atom"]), o.label = a("label", "tag", ["atom"]), o.ref = a("ref", "tag", ["atom"]), o.eqref = a("eqref", "tag", ["atom"]), o.cite = a("cite", "tag", ["atom"]), o.bibitem = a("bibitem", "tag", ["atom"]), o.Bibitem = a("Bibitem", "tag", ["atom"]), o.RBibitem = a("RBibitem", "tag", ["atom"]), o.DEFAULT = function() {
    this.name = "DEFAULT", this.style = "tag", this.styleIdentifier = this.openBracket = this.closeBracket = function() {
    };
  };
  function s(e2, t2) {
    e2.f = t2;
  }
  function c(e2, r2) {
    var a2;
    if (e2.match(/^\\[a-zA-Z@\xc0-\u1fff\u2060-\uffff]+/)) {
      var c2 = e2.current().slice(1);
      return a2 = o.hasOwnProperty(c2) ? o[c2] : o.DEFAULT, a2 = new a2(), t(r2, a2), s(r2, u), a2.style;
    }
    if (e2.match(/^\\[$&%#{}_]/) || e2.match(/^\\[,;!\/\\]/)) return "tag";
    if (e2.match("\\[")) return s(r2, function(e3, t2) {
      return l(e3, t2, "\\]");
    }), "keyword";
    if (e2.match("\\(")) return s(r2, function(e3, t2) {
      return l(e3, t2, "\\)");
    }), "keyword";
    if (e2.match("$$")) return s(r2, function(e3, t2) {
      return l(e3, t2, "$$");
    }), "keyword";
    if (e2.match("$")) return s(r2, function(e3, t2) {
      return l(e3, t2, "$");
    }), "keyword";
    var d = e2.next();
    if (d == "%") return e2.skipToEnd(), "comment";
    if (d == "}" || d == "]") {
      if (a2 = n(r2), a2) a2.closeBracket(d), s(r2, u);
      else return "error";
      return "bracket";
    } else if (d == "{" || d == "[") return a2 = o.DEFAULT, a2 = new a2(), t(r2, a2), "bracket";
    else if (/\d/.test(d)) return e2.eatWhile(/[\w.%]/), "atom";
    else return e2.eatWhile(/[\w\-_]/), a2 = i(r2), a2.name == "begin" && (a2.argument = e2.current()), a2.styleIdentifier();
  }
  function l(e2, t2, n2) {
    if (e2.eatSpace()) return null;
    if (n2 && e2.match(n2)) return s(t2, c), "keyword";
    if (e2.match(/^\\[a-zA-Z@]+/)) return "tag";
    if (e2.match(/^[a-zA-Z]+/)) return "variableName.special";
    if (e2.match(/^\\[$&%#{}_]/) || e2.match(/^\\[,;!\/]/) || e2.match(/^[\^_&]/)) return "tag";
    if (e2.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) return null;
    if (e2.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) return "number";
    var r2 = e2.next();
    return r2 == "{" || r2 == "}" || r2 == "[" || r2 == "]" || r2 == "(" || r2 == ")" ? "bracket" : r2 == "%" ? (e2.skipToEnd(), "comment") : "error";
  }
  function u(e2, t2) {
    var i2 = e2.peek(), a2;
    return i2 == "{" || i2 == "[" ? (a2 = n(t2), a2.openBracket(i2), e2.eat(i2), s(t2, c), "bracket") : /[ \t\r]/.test(i2) ? (e2.eat(i2), null) : (s(t2, c), r(t2), c(e2, t2));
  }
  return {
    name: "stex",
    startState: function() {
      return {
        cmdState: [],
        f: e ? function(e2, t2) {
          return l(e2, t2);
        } : c
      };
    },
    copyState: function(e2) {
      return {
        cmdState: e2.cmdState.slice(),
        f: e2.f
      };
    },
    token: function(e2, t2) {
      return t2.f(e2, t2);
    },
    blankLine: function(e2) {
      e2.f = c, e2.cmdState.length = 0;
    },
    languageData: { commentTokens: { line: "%" } }
  };
}
const stex = mkStex(false), stexMath = mkStex(true);
export {
  stexMath as n,
  stex as t
};
