var Ie = Object.defineProperty;
var be = (s, e, t) => e in s ? Ie(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var l = (s, e, t) => be(s, typeof e != "symbol" ? e + "" : e, t);
function Ae(s) {
  return Z(s);
}
function Z(s) {
  return Array.isArray(s) ? Pe(s) : s instanceof RegExp ? s : typeof s == "object" ? ke(s) : s;
}
function Pe(s) {
  let e = [];
  for (let t = 0, n = s.length; t < n; t++)
    e[t] = Z(s[t]);
  return e;
}
function ke(s) {
  let e = {};
  for (let t in s)
    e[t] = Z(s[t]);
  return e;
}
function ce(s, ...e) {
  return e.forEach((t) => {
    for (let n in t)
      s[n] = t[n];
  }), s;
}
function le(s) {
  const e = ~s.lastIndexOf("/") || ~s.lastIndexOf("\\");
  return e === 0 ? s : ~e === s.length - 1 ? le(s.substring(0, s.length - 1)) : s.substr(~e + 1);
}
var q = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g, M = class {
  static hasCaptures(s) {
    return s === null ? !1 : (q.lastIndex = 0, q.test(s));
  }
  static replaceCaptures(s, e, t) {
    return s.replace(q, (n, r, i, a) => {
      let c = t[parseInt(r || i, 10)];
      if (c) {
        let o = e.substring(c.start, c.end);
        for (; o[0] === "."; )
          o = o.substring(1);
        switch (a) {
          case "downcase":
            return o.toLowerCase();
          case "upcase":
            return o.toUpperCase();
          default:
            return o;
        }
      } else
        return n;
    });
  }
};
function he(s, e) {
  return s < e ? -1 : s > e ? 1 : 0;
}
function ue(s, e) {
  if (s === null && e === null)
    return 0;
  if (!s)
    return -1;
  if (!e)
    return 1;
  let t = s.length, n = e.length;
  if (t === n) {
    for (let r = 0; r < t; r++) {
      let i = he(s[r], e[r]);
      if (i !== 0)
        return i;
    }
    return 0;
  }
  return t - n;
}
function ee(s) {
  return !!(/^#[0-9a-f]{6}$/i.test(s) || /^#[0-9a-f]{8}$/i.test(s) || /^#[0-9a-f]{3}$/i.test(s) || /^#[0-9a-f]{4}$/i.test(s));
}
function de(s) {
  return s.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, "\\$&");
}
var pe = class {
  constructor(s) {
    l(this, "cache", /* @__PURE__ */ new Map());
    this.fn = s;
  }
  get(s) {
    if (this.cache.has(s))
      return this.cache.get(s);
    const e = this.fn(s);
    return this.cache.set(s, e), e;
  }
}, H = class {
  constructor(s, e, t) {
    l(this, "_cachedMatchRoot", new pe(
      (s) => this._root.match(s)
    ));
    this._colorMap = s, this._defaults = e, this._root = t;
  }
  static createFromRawTheme(s, e) {
    return this.createFromParsedTheme(Ge(s), e);
  }
  static createFromParsedTheme(s, e) {
    return Be(s, e);
  }
  getColorMap() {
    return this._colorMap.getColorMap();
  }
  getDefaults() {
    return this._defaults;
  }
  match(s) {
    if (s === null)
      return this._defaults;
    const e = s.scopeName, n = this._cachedMatchRoot.get(e).find(
      (r) => Ne(s.parent, r.parentScopes)
    );
    return n ? new fe(
      n.fontStyle,
      n.foreground,
      n.background
    ) : null;
  }
}, U = class L {
  constructor(e, t) {
    this.parent = e, this.scopeName = t;
  }
  static push(e, t) {
    for (const n of t)
      e = new L(e, n);
    return e;
  }
  static from(...e) {
    let t = null;
    for (let n = 0; n < e.length; n++)
      t = new L(t, e[n]);
    return t;
  }
  push(e) {
    return new L(this, e);
  }
  getSegments() {
    let e = this;
    const t = [];
    for (; e; )
      t.push(e.scopeName), e = e.parent;
    return t.reverse(), t;
  }
  toString() {
    return this.getSegments().join(" ");
  }
  extends(e) {
    return this === e ? !0 : this.parent === null ? !1 : this.parent.extends(e);
  }
  getExtensionIfDefined(e) {
    const t = [];
    let n = this;
    for (; n && n !== e; )
      t.push(n.scopeName), n = n.parent;
    return n === e ? t.reverse() : void 0;
  }
};
function Ne(s, e) {
  if (e.length === 0)
    return !0;
  for (let t = 0; t < e.length; t++) {
    let n = e[t], r = !1;
    if (n === ">") {
      if (t === e.length - 1)
        return !1;
      n = e[++t], r = !0;
    }
    for (; s && !ve(s.scopeName, n); ) {
      if (r)
        return !1;
      s = s.parent;
    }
    if (!s)
      return !1;
    s = s.parent;
  }
  return !0;
}
function ve(s, e) {
  return e === s || s.startsWith(e) && s[e.length] === ".";
}
var fe = class {
  constructor(s, e, t) {
    this.fontStyle = s, this.foregroundId = e, this.backgroundId = t;
  }
};
function Ge(s) {
  if (!s)
    return [];
  if (!s.settings || !Array.isArray(s.settings))
    return [];
  let e = s.settings, t = [], n = 0;
  for (let r = 0, i = e.length; r < i; r++) {
    let a = e[r];
    if (!a.settings)
      continue;
    let c;
    if (typeof a.scope == "string") {
      let d = a.scope;
      d = d.replace(/^[,]+/, ""), d = d.replace(/[,]+$/, ""), c = d.split(",");
    } else Array.isArray(a.scope) ? c = a.scope : c = [""];
    let o = -1;
    if (typeof a.settings.fontStyle == "string") {
      o = 0;
      let d = a.settings.fontStyle.split(" ");
      for (let m = 0, f = d.length; m < f; m++)
        switch (d[m]) {
          case "italic":
            o = o | 1;
            break;
          case "bold":
            o = o | 2;
            break;
          case "underline":
            o = o | 4;
            break;
          case "strikethrough":
            o = o | 8;
            break;
        }
    }
    let h = null;
    typeof a.settings.foreground == "string" && ee(a.settings.foreground) && (h = a.settings.foreground);
    let u = null;
    typeof a.settings.background == "string" && ee(a.settings.background) && (u = a.settings.background);
    for (let d = 0, m = c.length; d < m; d++) {
      let p = c[d].trim().split(" "), w = p[p.length - 1], g = null;
      p.length > 1 && (g = p.slice(0, p.length - 1), g.reverse()), t[n++] = new Ee(
        w,
        g,
        r,
        o,
        h,
        u
      );
    }
  }
  return t;
}
var Ee = class {
  constructor(s, e, t, n, r, i) {
    this.scope = s, this.parentScopes = e, this.index = t, this.fontStyle = n, this.foreground = r, this.background = i;
  }
}, Te = /* @__PURE__ */ ((s) => (s[s.NotSet = -1] = "NotSet", s[s.None = 0] = "None", s[s.Italic = 1] = "Italic", s[s.Bold = 2] = "Bold", s[s.Underline = 4] = "Underline", s[s.Strikethrough = 8] = "Strikethrough", s))(Te || {});
function Be(s, e) {
  s.sort((o, h) => {
    let u = he(o.scope, h.scope);
    return u !== 0 || (u = ue(o.parentScopes, h.parentScopes), u !== 0) ? u : o.index - h.index;
  });
  let t = 0, n = "#000000", r = "#ffffff";
  for (; s.length >= 1 && s[0].scope === ""; ) {
    let o = s.shift();
    o.fontStyle !== -1 && (t = o.fontStyle), o.foreground !== null && (n = o.foreground), o.background !== null && (r = o.background);
  }
  let i = new Me(e), a = new fe(t, i.getId(n), i.getId(r)), c = new xe(new Q(0, null, -1, 0, 0), []);
  for (let o = 0, h = s.length; o < h; o++) {
    let u = s[o];
    c.insert(0, u.scope, u.parentScopes, u.fontStyle, i.getId(u.foreground), i.getId(u.background));
  }
  return new H(i, a, c);
}
var Me = class {
  constructor(s) {
    l(this, "_isFrozen");
    l(this, "_lastColorId");
    l(this, "_id2color");
    l(this, "_color2id");
    if (this._lastColorId = 0, this._id2color = [], this._color2id = /* @__PURE__ */ Object.create(null), Array.isArray(s)) {
      this._isFrozen = !0;
      for (let e = 0, t = s.length; e < t; e++)
        this._color2id[s[e]] = e, this._id2color[e] = s[e];
    } else
      this._isFrozen = !1;
  }
  getId(s) {
    if (s === null)
      return 0;
    s = s.toUpperCase();
    let e = this._color2id[s];
    if (e)
      return e;
    if (this._isFrozen)
      throw new Error(`Missing color in color map - ${s}`);
    return e = ++this._lastColorId, this._color2id[s] = e, this._id2color[e] = s, e;
  }
  getColorMap() {
    return this._id2color.slice(0);
  }
}, Le = Object.freeze([]), Q = class me {
  constructor(e, t, n, r, i) {
    l(this, "scopeDepth");
    l(this, "parentScopes");
    l(this, "fontStyle");
    l(this, "foreground");
    l(this, "background");
    this.scopeDepth = e, this.parentScopes = t || Le, this.fontStyle = n, this.foreground = r, this.background = i;
  }
  clone() {
    return new me(this.scopeDepth, this.parentScopes, this.fontStyle, this.foreground, this.background);
  }
  static cloneArr(e) {
    let t = [];
    for (let n = 0, r = e.length; n < r; n++)
      t[n] = e[n].clone();
    return t;
  }
  acceptOverwrite(e, t, n, r) {
    this.scopeDepth > e ? console.log("how did this happen?") : this.scopeDepth = e, t !== -1 && (this.fontStyle = t), n !== 0 && (this.foreground = n), r !== 0 && (this.background = r);
  }
}, xe = class X {
  constructor(e, t = [], n = {}) {
    l(this, "_rulesWithParentScopes");
    this._mainRule = e, this._children = n, this._rulesWithParentScopes = t;
  }
  static _cmpBySpecificity(e, t) {
    if (e.scopeDepth !== t.scopeDepth)
      return t.scopeDepth - e.scopeDepth;
    let n = 0, r = 0;
    for (; e.parentScopes[n] === ">" && n++, t.parentScopes[r] === ">" && r++, !(n >= e.parentScopes.length || r >= t.parentScopes.length); ) {
      const i = t.parentScopes[r].length - e.parentScopes[n].length;
      if (i !== 0)
        return i;
      n++, r++;
    }
    return t.parentScopes.length - e.parentScopes.length;
  }
  match(e) {
    if (e !== "") {
      let n = e.indexOf("."), r, i;
      if (n === -1 ? (r = e, i = "") : (r = e.substring(0, n), i = e.substring(n + 1)), this._children.hasOwnProperty(r))
        return this._children[r].match(i);
    }
    const t = this._rulesWithParentScopes.concat(this._mainRule);
    return t.sort(X._cmpBySpecificity), t;
  }
  insert(e, t, n, r, i, a) {
    if (t === "") {
      this._doInsertHere(e, n, r, i, a);
      return;
    }
    let c = t.indexOf("."), o, h;
    c === -1 ? (o = t, h = "") : (o = t.substring(0, c), h = t.substring(c + 1));
    let u;
    this._children.hasOwnProperty(o) ? u = this._children[o] : (u = new X(this._mainRule.clone(), Q.cloneArr(this._rulesWithParentScopes)), this._children[o] = u), u.insert(e + 1, h, n, r, i, a);
  }
  _doInsertHere(e, t, n, r, i) {
    if (t === null) {
      this._mainRule.acceptOverwrite(e, n, r, i);
      return;
    }
    for (let a = 0, c = this._rulesWithParentScopes.length; a < c; a++) {
      let o = this._rulesWithParentScopes[a];
      if (ue(o.parentScopes, t) === 0) {
        o.acceptOverwrite(e, n, r, i);
        return;
      }
    }
    n === -1 && (n = this._mainRule.fontStyle), r === 0 && (r = this._mainRule.foreground), i === 0 && (i = this._mainRule.background), this._rulesWithParentScopes.push(new Q(e, t, n, r, i));
  }
}, O = class S {
  static toBinaryStr(e) {
    return e.toString(2).padStart(32, "0");
  }
  static print(e) {
    const t = S.getLanguageId(e), n = S.getTokenType(e), r = S.getFontStyle(e), i = S.getForeground(e), a = S.getBackground(e);
    console.log({
      languageId: t,
      tokenType: n,
      fontStyle: r,
      foreground: i,
      background: a
    });
  }
  static getLanguageId(e) {
    return (e & 255) >>> 0;
  }
  static getTokenType(e) {
    return (e & 768) >>> 8;
  }
  static containsBalancedBrackets(e) {
    return (e & 1024) !== 0;
  }
  static getFontStyle(e) {
    return (e & 30720) >>> 11;
  }
  static getForeground(e) {
    return (e & 16744448) >>> 15;
  }
  static getBackground(e) {
    return (e & 4278190080) >>> 24;
  }
  /**
   * Updates the fields in `metadata`.
   * A value of `0`, `NotSet` or `null` indicates that the corresponding field should be left as is.
   */
  static set(e, t, n, r, i, a, c) {
    let o = S.getLanguageId(e), h = S.getTokenType(e), u = S.containsBalancedBrackets(e) ? 1 : 0, d = S.getFontStyle(e), m = S.getForeground(e), f = S.getBackground(e);
    return t !== 0 && (o = t), n !== 8 && (h = n), r !== null && (u = r ? 1 : 0), i !== -1 && (d = i), a !== 0 && (m = a), c !== 0 && (f = c), (o << 0 | h << 8 | u << 10 | d << 11 | m << 15 | f << 24) >>> 0;
  }
};
function j(s, e) {
  const t = [], n = Oe(s);
  let r = n.next();
  for (; r !== null; ) {
    let o = 0;
    if (r.length === 2 && r.charAt(1) === ":") {
      switch (r.charAt(0)) {
        case "R":
          o = 1;
          break;
        case "L":
          o = -1;
          break;
        default:
          console.log(`Unknown priority ${r} in scope selector`);
      }
      r = n.next();
    }
    let h = a();
    if (t.push({ matcher: h, priority: o }), r !== ",")
      break;
    r = n.next();
  }
  return t;
  function i() {
    if (r === "-") {
      r = n.next();
      const o = i();
      return (h) => !!o && !o(h);
    }
    if (r === "(") {
      r = n.next();
      const o = c();
      return r === ")" && (r = n.next()), o;
    }
    if (te(r)) {
      const o = [];
      do
        o.push(r), r = n.next();
      while (te(r));
      return (h) => e(o, h);
    }
    return null;
  }
  function a() {
    const o = [];
    let h = i();
    for (; h; )
      o.push(h), h = i();
    return (u) => o.every((d) => d(u));
  }
  function c() {
    const o = [];
    let h = a();
    for (; h && (o.push(h), r === "|" || r === ","); ) {
      do
        r = n.next();
      while (r === "|" || r === ",");
      h = a();
    }
    return (u) => o.some((d) => d(u));
  }
}
function te(s) {
  return !!s && !!s.match(/[\w\.:]+/);
}
function Oe(s) {
  let e = /([LR]:|[\w\.:][\w\.:\-]*|[\,\|\-\(\)])/g, t = e.exec(s);
  return {
    next: () => {
      if (!t)
        return null;
      const n = t[0];
      return t = e.exec(s), n;
    }
  };
}
function _e(s) {
  typeof s.dispose == "function" && s.dispose();
}
var G = class {
  constructor(s) {
    this.scopeName = s;
  }
  toKey() {
    return this.scopeName;
  }
}, je = class {
  constructor(s, e) {
    this.scopeName = s, this.ruleName = e;
  }
  toKey() {
    return `${this.scopeName}#${this.ruleName}`;
  }
}, $e = class {
  constructor() {
    l(this, "_references", []);
    l(this, "_seenReferenceKeys", /* @__PURE__ */ new Set());
    l(this, "visitedRule", /* @__PURE__ */ new Set());
  }
  get references() {
    return this._references;
  }
  add(s) {
    const e = s.toKey();
    this._seenReferenceKeys.has(e) || (this._seenReferenceKeys.add(e), this._references.push(s));
  }
}, Fe = class {
  constructor(s, e) {
    l(this, "seenFullScopeRequests", /* @__PURE__ */ new Set());
    l(this, "seenPartialScopeRequests", /* @__PURE__ */ new Set());
    l(this, "Q");
    this.repo = s, this.initialScopeName = e, this.seenFullScopeRequests.add(this.initialScopeName), this.Q = [new G(this.initialScopeName)];
  }
  processQueue() {
    const s = this.Q;
    this.Q = [];
    const e = new $e();
    for (const t of s)
      We(t, this.initialScopeName, this.repo, e);
    for (const t of e.references)
      if (t instanceof G) {
        if (this.seenFullScopeRequests.has(t.scopeName))
          continue;
        this.seenFullScopeRequests.add(t.scopeName), this.Q.push(t);
      } else {
        if (this.seenFullScopeRequests.has(t.scopeName) || this.seenPartialScopeRequests.has(t.toKey()))
          continue;
        this.seenPartialScopeRequests.add(t.toKey()), this.Q.push(t);
      }
  }
};
function We(s, e, t, n) {
  const r = t.lookup(s.scopeName);
  if (!r) {
    if (s.scopeName === e)
      throw new Error(`No grammar provided for <${e}>`);
    return;
  }
  const i = t.lookup(e);
  s instanceof G ? x({ baseGrammar: i, selfGrammar: r }, n) : V(
    s.ruleName,
    { baseGrammar: i, selfGrammar: r, repository: r.repository },
    n
  );
  const a = t.injections(s.scopeName);
  if (a)
    for (const c of a)
      n.add(new G(c));
}
function V(s, e, t) {
  if (e.repository && e.repository[s]) {
    const n = e.repository[s];
    $([n], e, t);
  }
}
function x(s, e) {
  s.selfGrammar.patterns && Array.isArray(s.selfGrammar.patterns) && $(
    s.selfGrammar.patterns,
    { ...s, repository: s.selfGrammar.repository },
    e
  ), s.selfGrammar.injections && $(
    Object.values(s.selfGrammar.injections),
    { ...s, repository: s.selfGrammar.repository },
    e
  );
}
function $(s, e, t) {
  for (const n of s) {
    if (t.visitedRule.has(n))
      continue;
    t.visitedRule.add(n);
    const r = n.repository ? ce({}, e.repository, n.repository) : e.repository;
    Array.isArray(n.patterns) && $(n.patterns, { ...e, repository: r }, t);
    const i = n.include;
    if (!i)
      continue;
    const a = ge(i);
    switch (a.kind) {
      case 0:
        x({ ...e, selfGrammar: e.baseGrammar }, t);
        break;
      case 1:
        x(e, t);
        break;
      case 2:
        V(a.ruleName, { ...e, repository: r }, t);
        break;
      case 3:
      case 4:
        const c = a.scopeName === e.selfGrammar.scopeName ? e.selfGrammar : a.scopeName === e.baseGrammar.scopeName ? e.baseGrammar : void 0;
        if (c) {
          const o = { baseGrammar: e.baseGrammar, selfGrammar: c, repository: r };
          a.kind === 4 ? V(a.ruleName, o, t) : x(o, t);
        } else
          a.kind === 4 ? t.add(new je(a.scopeName, a.ruleName)) : t.add(new G(a.scopeName));
        break;
    }
  }
}
var De = class {
  constructor() {
    l(this, "kind", 0);
  }
}, ze = class {
  constructor() {
    l(this, "kind", 1);
  }
}, qe = class {
  constructor(s) {
    l(this, "kind", 2);
    this.ruleName = s;
  }
}, Ue = class {
  constructor(s) {
    l(this, "kind", 3);
    this.scopeName = s;
  }
}, Ke = class {
  constructor(s, e) {
    l(this, "kind", 4);
    this.scopeName = s, this.ruleName = e;
  }
};
function ge(s) {
  if (s === "$base")
    return new De();
  if (s === "$self")
    return new ze();
  const e = s.indexOf("#");
  if (e === -1)
    return new Ue(s);
  if (e === 0)
    return new qe(s.substring(1));
  {
    const t = s.substring(0, e), n = s.substring(e + 1);
    return new Ke(t, n);
  }
}
var He = /\\(\d+)/, se = /\\(\d+)/g, Qe = -1, Re = -2;
var B = class {
  constructor(s, e, t, n) {
    l(this, "$location");
    l(this, "id");
    l(this, "_nameIsCapturing");
    l(this, "_name");
    l(this, "_contentNameIsCapturing");
    l(this, "_contentName");
    this.$location = s, this.id = e, this._name = t || null, this._nameIsCapturing = M.hasCaptures(this._name), this._contentName = n || null, this._contentNameIsCapturing = M.hasCaptures(this._contentName);
  }
  get debugName() {
    const s = this.$location ? `${le(this.$location.filename)}:${this.$location.line}` : "unknown";
    return `${this.constructor.name}#${this.id} @ ${s}`;
  }
  getName(s, e) {
    return !this._nameIsCapturing || this._name === null || s === null || e === null ? this._name : M.replaceCaptures(this._name, s, e);
  }
  getContentName(s, e) {
    return !this._contentNameIsCapturing || this._contentName === null ? this._contentName : M.replaceCaptures(this._contentName, s, e);
  }
}, Xe = class extends B {
  constructor(e, t, n, r, i) {
    super(e, t, n, r);
    l(this, "retokenizeCapturedWithRuleId");
    this.retokenizeCapturedWithRuleId = i;
  }
  dispose() {
  }
  collectPatterns(e, t) {
    throw new Error("Not supported!");
  }
  compile(e, t) {
    throw new Error("Not supported!");
  }
  compileAG(e, t, n, r) {
    throw new Error("Not supported!");
  }
}, Ve = class extends B {
  constructor(e, t, n, r, i) {
    super(e, t, n, null);
    l(this, "_match");
    l(this, "captures");
    l(this, "_cachedCompiledPatterns");
    this._match = new E(r, this.id), this.captures = i, this._cachedCompiledPatterns = null;
  }
  dispose() {
    this._cachedCompiledPatterns && (this._cachedCompiledPatterns.dispose(), this._cachedCompiledPatterns = null);
  }
  get debugMatchRegExp() {
    return `${this._match.source}`;
  }
  collectPatterns(e, t) {
    t.push(this._match);
  }
  compile(e, t) {
    return this._getCachedCompiledPatterns(e).compile(e);
  }
  compileAG(e, t, n, r) {
    return this._getCachedCompiledPatterns(e).compileAG(e, n, r);
  }
  _getCachedCompiledPatterns(e) {
    return this._cachedCompiledPatterns || (this._cachedCompiledPatterns = new T(), this.collectPatterns(e, this._cachedCompiledPatterns)), this._cachedCompiledPatterns;
  }
}, ne = class extends B {
  constructor(e, t, n, r, i) {
    super(e, t, n, r);
    l(this, "hasMissingPatterns");
    l(this, "patterns");
    l(this, "_cachedCompiledPatterns");
    this.patterns = i.patterns, this.hasMissingPatterns = i.hasMissingPatterns, this._cachedCompiledPatterns = null;
  }
  dispose() {
    this._cachedCompiledPatterns && (this._cachedCompiledPatterns.dispose(), this._cachedCompiledPatterns = null);
  }
  collectPatterns(e, t) {
    for (const n of this.patterns)
      e.getRule(n).collectPatterns(e, t);
  }
  compile(e, t) {
    return this._getCachedCompiledPatterns(e).compile(e);
  }
  compileAG(e, t, n, r) {
    return this._getCachedCompiledPatterns(e).compileAG(e, n, r);
  }
  _getCachedCompiledPatterns(e) {
    return this._cachedCompiledPatterns || (this._cachedCompiledPatterns = new T(), this.collectPatterns(e, this._cachedCompiledPatterns)), this._cachedCompiledPatterns;
  }
}, J = class extends B {
  constructor(e, t, n, r, i, a, c, o, h, u) {
    super(e, t, n, r);
    l(this, "_begin");
    l(this, "beginCaptures");
    l(this, "_end");
    l(this, "endHasBackReferences");
    l(this, "endCaptures");
    l(this, "applyEndPatternLast");
    l(this, "hasMissingPatterns");
    l(this, "patterns");
    l(this, "_cachedCompiledPatterns");
    this._begin = new E(i, this.id), this.beginCaptures = a, this._end = new E(c || "", -1), this.endHasBackReferences = this._end.hasBackReferences, this.endCaptures = o, this.applyEndPatternLast = h || !1, this.patterns = u.patterns, this.hasMissingPatterns = u.hasMissingPatterns, this._cachedCompiledPatterns = null;
  }
  dispose() {
    this._cachedCompiledPatterns && (this._cachedCompiledPatterns.dispose(), this._cachedCompiledPatterns = null);
  }
  get debugBeginRegExp() {
    return `${this._begin.source}`;
  }
  get debugEndRegExp() {
    return `${this._end.source}`;
  }
  getEndWithResolvedBackReferences(e, t) {
    return this._end.resolveBackReferences(e, t);
  }
  collectPatterns(e, t) {
    t.push(this._begin);
  }
  compile(e, t) {
    return this._getCachedCompiledPatterns(e, t).compile(e);
  }
  compileAG(e, t, n, r) {
    return this._getCachedCompiledPatterns(e, t).compileAG(e, n, r);
  }
  _getCachedCompiledPatterns(e, t) {
    if (!this._cachedCompiledPatterns) {
      this._cachedCompiledPatterns = new T();
      for (const n of this.patterns)
        e.getRule(n).collectPatterns(e, this._cachedCompiledPatterns);
      this.applyEndPatternLast ? this._cachedCompiledPatterns.push(this._end.hasBackReferences ? this._end.clone() : this._end) : this._cachedCompiledPatterns.unshift(this._end.hasBackReferences ? this._end.clone() : this._end);
    }
    return this._end.hasBackReferences && (this.applyEndPatternLast ? this._cachedCompiledPatterns.setSource(this._cachedCompiledPatterns.length() - 1, t) : this._cachedCompiledPatterns.setSource(0, t)), this._cachedCompiledPatterns;
  }
}, F = class extends B {
  constructor(e, t, n, r, i, a, c, o, h) {
    super(e, t, n, r);
    l(this, "_begin");
    l(this, "beginCaptures");
    l(this, "whileCaptures");
    l(this, "_while");
    l(this, "whileHasBackReferences");
    l(this, "hasMissingPatterns");
    l(this, "patterns");
    l(this, "_cachedCompiledPatterns");
    l(this, "_cachedCompiledWhilePatterns");
    this._begin = new E(i, this.id), this.beginCaptures = a, this.whileCaptures = o, this._while = new E(c, Re), this.whileHasBackReferences = this._while.hasBackReferences, this.patterns = h.patterns, this.hasMissingPatterns = h.hasMissingPatterns, this._cachedCompiledPatterns = null, this._cachedCompiledWhilePatterns = null;
  }
  dispose() {
    this._cachedCompiledPatterns && (this._cachedCompiledPatterns.dispose(), this._cachedCompiledPatterns = null), this._cachedCompiledWhilePatterns && (this._cachedCompiledWhilePatterns.dispose(), this._cachedCompiledWhilePatterns = null);
  }
  get debugBeginRegExp() {
    return `${this._begin.source}`;
  }
  get debugWhileRegExp() {
    return `${this._while.source}`;
  }
  getWhileWithResolvedBackReferences(e, t) {
    return this._while.resolveBackReferences(e, t);
  }
  collectPatterns(e, t) {
    t.push(this._begin);
  }
  compile(e, t) {
    return this._getCachedCompiledPatterns(e).compile(e);
  }
  compileAG(e, t, n, r) {
    return this._getCachedCompiledPatterns(e).compileAG(e, n, r);
  }
  _getCachedCompiledPatterns(e) {
    if (!this._cachedCompiledPatterns) {
      this._cachedCompiledPatterns = new T();
      for (const t of this.patterns)
        e.getRule(t).collectPatterns(e, this._cachedCompiledPatterns);
    }
    return this._cachedCompiledPatterns;
  }
  compileWhile(e, t) {
    return this._getCachedCompiledWhilePatterns(e, t).compile(e);
  }
  compileWhileAG(e, t, n, r) {
    return this._getCachedCompiledWhilePatterns(e, t).compileAG(e, n, r);
  }
  _getCachedCompiledWhilePatterns(e, t) {
    return this._cachedCompiledWhilePatterns || (this._cachedCompiledWhilePatterns = new T(), this._cachedCompiledWhilePatterns.push(this._while.hasBackReferences ? this._while.clone() : this._while)), this._while.hasBackReferences && this._cachedCompiledWhilePatterns.setSource(0, t || ""), this._cachedCompiledWhilePatterns;
  }
}, Ce = class _ {
  static createCaptureRule(e, t, n, r, i) {
    return e.registerRule((a) => new Xe(t, a, n, r, i));
  }
  static getCompiledRuleId(e, t, n) {
    return e.id || t.registerRule((r) => {
      if (e.id = r, e.match)
        return new Ve(
          e.$vscodeTextmateLocation,
          e.id,
          e.name,
          e.match,
          _._compileCaptures(e.captures, t, n)
        );
      if (typeof e.begin > "u") {
        e.repository && (n = ce({}, n, e.repository));
        let i = e.patterns;
        return typeof i > "u" && e.include && (i = [{ include: e.include }]), new ne(
          e.$vscodeTextmateLocation,
          e.id,
          e.name,
          e.contentName,
          _._compilePatterns(i, t, n)
        );
      }
      return e.while ? new F(
        e.$vscodeTextmateLocation,
        e.id,
        e.name,
        e.contentName,
        e.begin,
        _._compileCaptures(e.beginCaptures || e.captures, t, n),
        e.while,
        _._compileCaptures(e.whileCaptures || e.captures, t, n),
        _._compilePatterns(e.patterns, t, n)
      ) : new J(
        e.$vscodeTextmateLocation,
        e.id,
        e.name,
        e.contentName,
        e.begin,
        _._compileCaptures(e.beginCaptures || e.captures, t, n),
        e.end,
        _._compileCaptures(e.endCaptures || e.captures, t, n),
        e.applyEndPatternLast,
        _._compilePatterns(e.patterns, t, n)
      );
    }), e.id;
  }
  static _compileCaptures(e, t, n) {
    let r = [];
    if (e) {
      let i = 0;
      for (const a in e) {
        if (a === "$vscodeTextmateLocation")
          continue;
        const c = parseInt(a, 10);
        c > i && (i = c);
      }
      for (let a = 0; a <= i; a++)
        r[a] = null;
      for (const a in e) {
        if (a === "$vscodeTextmateLocation")
          continue;
        const c = parseInt(a, 10);
        let o = 0;
        e[a].patterns && (o = _.getCompiledRuleId(e[a], t, n)), r[c] = _.createCaptureRule(t, e[a].$vscodeTextmateLocation, e[a].name, e[a].contentName, o);
      }
    }
    return r;
  }
  static _compilePatterns(e, t, n) {
    let r = [];
    if (e)
      for (let i = 0, a = e.length; i < a; i++) {
        const c = e[i];
        let o = -1;
        if (c.include) {
          const h = ge(c.include);
          switch (h.kind) {
            case 0:
            case 1:
              o = _.getCompiledRuleId(n[c.include], t, n);
              break;
            case 2:
              let u = n[h.ruleName];
              u && (o = _.getCompiledRuleId(u, t, n));
              break;
            case 3:
            case 4:
              const d = h.scopeName, m = h.kind === 4 ? h.ruleName : null, f = t.getExternalGrammar(d, n);
              if (f)
                if (m) {
                  let p = f.repository[m];
                  p && (o = _.getCompiledRuleId(p, t, f.repository));
                } else
                  o = _.getCompiledRuleId(f.repository.$self, t, f.repository);
              break;
          }
        } else
          o = _.getCompiledRuleId(c, t, n);
        if (o !== -1) {
          const h = t.getRule(o);
          let u = !1;
          if ((h instanceof ne || h instanceof J || h instanceof F) && h.hasMissingPatterns && h.patterns.length === 0 && (u = !0), u)
            continue;
          r.push(o);
        }
      }
    return {
      patterns: r,
      hasMissingPatterns: (e ? e.length : 0) !== r.length
    };
  }
}, E = class Se {
  constructor(e, t) {
    l(this, "source");
    l(this, "ruleId");
    l(this, "hasAnchor");
    l(this, "hasBackReferences");
    l(this, "_anchorCache");
    if (e && typeof e == "string") {
      const n = e.length;
      let r = 0, i = [], a = !1;
      for (let c = 0; c < n; c++)
        if (e.charAt(c) === "\\" && c + 1 < n) {
          const h = e.charAt(c + 1);
          h === "z" ? (i.push(e.substring(r, c)), i.push("$(?!\\n)(?<!\\n)"), r = c + 2) : (h === "A" || h === "G") && (a = !0), c++;
        }
      this.hasAnchor = a, r === 0 ? this.source = e : (i.push(e.substring(r, n)), this.source = i.join(""));
    } else
      this.hasAnchor = !1, this.source = e;
    this.hasAnchor ? this._anchorCache = this._buildAnchorCache() : this._anchorCache = null, this.ruleId = t, typeof this.source == "string" ? this.hasBackReferences = He.test(this.source) : this.hasBackReferences = !1;
  }
  clone() {
    return new Se(this.source, this.ruleId);
  }
  setSource(e) {
    this.source !== e && (this.source = e, this.hasAnchor && (this._anchorCache = this._buildAnchorCache()));
  }
  resolveBackReferences(e, t) {
    if (typeof this.source != "string")
      throw new Error("This method should only be called if the source is a string");
    let n = t.map((r) => e.substring(r.start, r.end));
    return se.lastIndex = 0, this.source.replace(se, (r, i) => de(n[parseInt(i, 10)] || ""));
  }
  _buildAnchorCache() {
    if (typeof this.source != "string")
      throw new Error("This method should only be called if the source is a string");
    let e = [], t = [], n = [], r = [], i, a, c, o;
    for (i = 0, a = this.source.length; i < a; i++)
      c = this.source.charAt(i), e[i] = c, t[i] = c, n[i] = c, r[i] = c, c === "\\" && i + 1 < a && (o = this.source.charAt(i + 1), o === "A" ? (e[i + 1] = "", t[i + 1] = "", n[i + 1] = "A", r[i + 1] = "A") : o === "G" ? (e[i + 1] = "", t[i + 1] = "G", n[i + 1] = "", r[i + 1] = "G") : (e[i + 1] = o, t[i + 1] = o, n[i + 1] = o, r[i + 1] = o), i++);
    return {
      A0_G0: e.join(""),
      A0_G1: t.join(""),
      A1_G0: n.join(""),
      A1_G1: r.join("")
    };
  }
  resolveAnchors(e, t) {
    return !this.hasAnchor || !this._anchorCache || typeof this.source != "string" ? this.source : e ? t ? this._anchorCache.A1_G1 : this._anchorCache.A1_G0 : t ? this._anchorCache.A0_G1 : this._anchorCache.A0_G0;
  }
}, T = class {
  constructor() {
    l(this, "_items");
    l(this, "_hasAnchors");
    l(this, "_cached");
    l(this, "_anchorCache");
    this._items = [], this._hasAnchors = !1, this._cached = null, this._anchorCache = {
      A0_G0: null,
      A0_G1: null,
      A1_G0: null,
      A1_G1: null
    };
  }
  dispose() {
    this._disposeCaches();
  }
  _disposeCaches() {
    this._cached && (this._cached.dispose(), this._cached = null), this._anchorCache.A0_G0 && (this._anchorCache.A0_G0.dispose(), this._anchorCache.A0_G0 = null), this._anchorCache.A0_G1 && (this._anchorCache.A0_G1.dispose(), this._anchorCache.A0_G1 = null), this._anchorCache.A1_G0 && (this._anchorCache.A1_G0.dispose(), this._anchorCache.A1_G0 = null), this._anchorCache.A1_G1 && (this._anchorCache.A1_G1.dispose(), this._anchorCache.A1_G1 = null);
  }
  push(s) {
    this._items.push(s), this._hasAnchors = this._hasAnchors || s.hasAnchor;
  }
  unshift(s) {
    this._items.unshift(s), this._hasAnchors = this._hasAnchors || s.hasAnchor;
  }
  length() {
    return this._items.length;
  }
  setSource(s, e) {
    this._items[s].source !== e && (this._disposeCaches(), this._items[s].setSource(e));
  }
  compile(s) {
    if (!this._cached) {
      let e = this._items.map((t) => t.source);
      this._cached = new re(s, e, this._items.map((t) => t.ruleId));
    }
    return this._cached;
  }
  compileAG(s, e, t) {
    return this._hasAnchors ? e ? t ? (this._anchorCache.A1_G1 || (this._anchorCache.A1_G1 = this._resolveAnchors(s, e, t)), this._anchorCache.A1_G1) : (this._anchorCache.A1_G0 || (this._anchorCache.A1_G0 = this._resolveAnchors(s, e, t)), this._anchorCache.A1_G0) : t ? (this._anchorCache.A0_G1 || (this._anchorCache.A0_G1 = this._resolveAnchors(s, e, t)), this._anchorCache.A0_G1) : (this._anchorCache.A0_G0 || (this._anchorCache.A0_G0 = this._resolveAnchors(s, e, t)), this._anchorCache.A0_G0) : this.compile(s);
  }
  _resolveAnchors(s, e, t) {
    let n = this._items.map((r) => r.resolveAnchors(e, t));
    return new re(s, n, this._items.map((r) => r.ruleId));
  }
}, re = class {
  constructor(s, e, t) {
    l(this, "scanner");
    this.regExps = e, this.rules = t, this.scanner = s.createOnigScanner(e);
  }
  dispose() {
    typeof this.scanner.dispose == "function" && this.scanner.dispose();
  }
  toString() {
    const s = [];
    for (let e = 0, t = this.rules.length; e < t; e++)
      s.push("   - " + this.rules[e] + ": " + this.regExps[e]);
    return s.join(`
`);
  }
  findNextMatchSync(s, e, t) {
    const n = this.scanner.findNextMatchSync(s, e, t);
    return n ? {
      ruleId: this.rules[n.index],
      captureIndices: n.captureIndices
    } : null;
  }
}, K = class {
  constructor(s, e) {
    this.languageId = s, this.tokenType = e;
  }
}, A, Je = (A = class {
  constructor(e, t) {
    l(this, "_defaultAttributes");
    l(this, "_embeddedLanguagesMatcher");
    l(this, "_getBasicScopeAttributes", new pe((e) => {
      const t = this._scopeToLanguage(e), n = this._toStandardTokenType(e);
      return new K(t, n);
    }));
    this._defaultAttributes = new K(
      e,
      8
      /* NotSet */
    ), this._embeddedLanguagesMatcher = new Ye(Object.entries(t || {}));
  }
  getDefaultAttributes() {
    return this._defaultAttributes;
  }
  getBasicScopeAttributes(e) {
    return e === null ? A._NULL_SCOPE_METADATA : this._getBasicScopeAttributes.get(e);
  }
  /**
   * Given a produced TM scope, return the language that token describes or null if unknown.
   * e.g. source.html => html, source.css.embedded.html => css, punctuation.definition.tag.html => null
   */
  _scopeToLanguage(e) {
    return this._embeddedLanguagesMatcher.match(e) || 0;
  }
  _toStandardTokenType(e) {
    const t = e.match(A.STANDARD_TOKEN_TYPE_REGEXP);
    if (!t)
      return 8;
    switch (t[1]) {
      case "comment":
        return 1;
      case "string":
        return 2;
      case "regex":
        return 3;
      case "meta.embedded":
        return 0;
    }
    throw new Error("Unexpected match for standard token type!");
  }
}, l(A, "_NULL_SCOPE_METADATA", new K(0, 0)), l(A, "STANDARD_TOKEN_TYPE_REGEXP", /\b(comment|string|regex|meta\.embedded)\b/), A), Ye = class {
  constructor(s) {
    l(this, "values");
    l(this, "scopesRegExp");
    if (s.length === 0)
      this.values = null, this.scopesRegExp = null;
    else {
      this.values = new Map(s);
      const e = s.map(
        ([t, n]) => de(t)
      );
      e.sort(), e.reverse(), this.scopesRegExp = new RegExp(
        `^((${e.join(")|(")}))($|\\.)`,
        ""
      );
    }
  }
  match(s) {
    if (!this.scopesRegExp)
      return;
    const e = s.match(this.scopesRegExp);
    if (e)
      return this.values.get(e[1]);
  }
};
typeof process < "u" && process.env.VSCODE_TEXTMATE_DEBUG;
var ie = class {
  constructor(s, e) {
    this.stack = s, this.stoppedEarly = e;
  }
};
function ye(s, e, t, n, r, i, a, c) {
  const o = e.content.length;
  let h = !1, u = -1;
  if (a) {
    const f = Ze(
      s,
      e,
      t,
      n,
      r,
      i
    );
    r = f.stack, n = f.linePos, t = f.isFirstLine, u = f.anchorPosition;
  }
  const d = Date.now();
  for (; !h; ) {
    if (c !== 0 && Date.now() - d > c)
      return new ie(r, !0);
    m();
  }
  return new ie(r, !1);
  function m() {
    const f = et(
      s,
      e,
      t,
      n,
      r,
      u
    );
    if (!f) {
      i.produce(r, o), h = !0;
      return;
    }
    const p = f.captureIndices, w = f.matchedRuleId, g = p && p.length > 0 ? p[0].end > n : !1;
    if (w === Qe) {
      const C = r.getRule(s);
      i.produce(r, p[0].start), r = r.withContentNameScopesList(r.nameScopesList), N(
        s,
        e,
        t,
        r,
        i,
        C.endCaptures,
        p
      ), i.produce(r, p[0].end);
      const R = r;
      if (r = r.parent, u = R.getAnchorPos(), !g && R.getEnterPos() === n) {
        r = R, i.produce(r, o), h = !0;
        return;
      }
    } else {
      const C = s.getRule(w);
      i.produce(r, p[0].start);
      const R = r, k = C.getName(e.content, p), P = r.contentNameScopesList.pushAttributed(
        k,
        s
      );
      if (r = r.push(
        w,
        n,
        u,
        p[0].end === o,
        null,
        P,
        P
      ), C instanceof J) {
        const I = C;
        N(
          s,
          e,
          t,
          r,
          i,
          I.beginCaptures,
          p
        ), i.produce(r, p[0].end), u = p[0].end;
        const D = I.getContentName(
          e.content,
          p
        ), z = P.pushAttributed(
          D,
          s
        );
        if (r = r.withContentNameScopesList(z), I.endHasBackReferences && (r = r.withEndRule(
          I.getEndWithResolvedBackReferences(
            e.content,
            p
          )
        )), !g && R.hasSameRuleAs(r)) {
          r = r.pop(), i.produce(r, o), h = !0;
          return;
        }
      } else if (C instanceof F) {
        const I = C;
        N(
          s,
          e,
          t,
          r,
          i,
          I.beginCaptures,
          p
        ), i.produce(r, p[0].end), u = p[0].end;
        const D = I.getContentName(
          e.content,
          p
        ), z = P.pushAttributed(
          D,
          s
        );
        if (r = r.withContentNameScopesList(z), I.whileHasBackReferences && (r = r.withEndRule(
          I.getWhileWithResolvedBackReferences(
            e.content,
            p
          )
        )), !g && R.hasSameRuleAs(r)) {
          r = r.pop(), i.produce(r, o), h = !0;
          return;
        }
      } else if (N(
        s,
        e,
        t,
        r,
        i,
        C.captures,
        p
      ), i.produce(r, p[0].end), r = r.pop(), !g) {
        r = r.safePop(), i.produce(r, o), h = !0;
        return;
      }
    }
    p[0].end > n && (n = p[0].end, t = !1);
  }
}
function Ze(s, e, t, n, r, i) {
  let a = r.beginRuleCapturedEOL ? 0 : -1;
  const c = [];
  for (let o = r; o; o = o.pop()) {
    const h = o.getRule(s);
    h instanceof F && c.push({
      rule: h,
      stack: o
    });
  }
  for (let o = c.pop(); o; o = c.pop()) {
    const { ruleScanner: h, findOptions: u } = nt(o.rule, s, o.stack.endRule, t, n === a), d = h.findNextMatchSync(e, n, u);
    if (d) {
      if (d.ruleId !== Re) {
        r = o.stack.pop();
        break;
      }
      d.captureIndices && d.captureIndices.length && (i.produce(o.stack, d.captureIndices[0].start), N(s, e, t, o.stack, i, o.rule.whileCaptures, d.captureIndices), i.produce(o.stack, d.captureIndices[0].end), a = d.captureIndices[0].end, d.captureIndices[0].end > n && (n = d.captureIndices[0].end, t = !1));
    } else {
      r = o.stack.pop();
      break;
    }
  }
  return { stack: r, linePos: n, anchorPosition: a, isFirstLine: t };
}
function et(s, e, t, n, r, i) {
  const a = tt(s, e, t, n, r, i), c = s.getInjections();
  if (c.length === 0)
    return a;
  const o = st(c, s, e, t, n, r, i);
  if (!o)
    return a;
  if (!a)
    return o;
  const h = a.captureIndices[0].start, u = o.captureIndices[0].start;
  return u < h || o.priorityMatch && u === h ? o : a;
}
function tt(s, e, t, n, r, i) {
  const a = r.getRule(s), { ruleScanner: c, findOptions: o } = we(a, s, r.endRule, t, n === i), h = c.findNextMatchSync(e, n, o);
  return h ? {
    captureIndices: h.captureIndices,
    matchedRuleId: h.ruleId
  } : null;
}
function st(s, e, t, n, r, i, a) {
  let c = Number.MAX_VALUE, o = null, h, u = 0;
  const d = i.contentNameScopesList.getScopeNames();
  for (let m = 0, f = s.length; m < f; m++) {
    const p = s[m];
    if (!p.matcher(d))
      continue;
    const w = e.getRule(p.ruleId), { ruleScanner: g, findOptions: C } = we(w, e, null, n, r === a), R = g.findNextMatchSync(t, r, C);
    if (!R)
      continue;
    const k = R.captureIndices[0].start;
    if (!(k >= c) && (c = k, o = R.captureIndices, h = R.ruleId, u = p.priority, c === r))
      break;
  }
  return o ? {
    priorityMatch: u === -1,
    captureIndices: o,
    matchedRuleId: h
  } : null;
}
function we(s, e, t, n, r) {
  return {
    ruleScanner: s.compileAG(e, t, n, r),
    findOptions: 0
    /* None */
  };
}
function nt(s, e, t, n, r) {
  return {
    ruleScanner: s.compileWhileAG(e, t, n, r),
    findOptions: 0
    /* None */
  };
}
function N(s, e, t, n, r, i, a) {
  if (i.length === 0)
    return;
  const c = e.content, o = Math.min(i.length, a.length), h = [], u = a[0].end;
  for (let d = 0; d < o; d++) {
    const m = i[d];
    if (m === null)
      continue;
    const f = a[d];
    if (f.length === 0)
      continue;
    if (f.start > u)
      break;
    for (; h.length > 0 && h[h.length - 1].endPos <= f.start; )
      r.produceFromScopes(h[h.length - 1].scopes, h[h.length - 1].endPos), h.pop();
    if (h.length > 0 ? r.produceFromScopes(h[h.length - 1].scopes, f.start) : r.produce(n, f.start), m.retokenizeCapturedWithRuleId) {
      const w = m.getName(c, a), g = n.contentNameScopesList.pushAttributed(w, s), C = m.getContentName(c, a), R = g.pushAttributed(C, s), k = n.push(m.retokenizeCapturedWithRuleId, f.start, -1, !1, null, g, R), P = s.createOnigString(c.substring(0, f.end));
      ye(
        s,
        P,
        t && f.start === 0,
        f.start,
        k,
        r,
        !1,
        /* no time limit */
        0
      ), _e(P);
      continue;
    }
    const p = m.getName(c, a);
    if (p !== null) {
      const g = (h.length > 0 ? h[h.length - 1].scopes : n.contentNameScopesList).pushAttributed(p, s);
      h.push(new rt(g, f.end));
    }
  }
  for (; h.length > 0; )
    r.produceFromScopes(h[h.length - 1].scopes, h[h.length - 1].endPos), h.pop();
}
var rt = class {
  constructor(s, e) {
    l(this, "scopes");
    l(this, "endPos");
    this.scopes = s, this.endPos = e;
  }
};
function it(s, e, t, n, r, i, a, c) {
  return new at(
    s,
    e,
    t,
    n,
    r,
    i,
    a,
    c
  );
}
function oe(s, e, t, n, r) {
  const i = j(e, W), a = Ce.getCompiledRuleId(t, n, r.repository);
  for (const c of i)
    s.push({
      debugSelector: e,
      matcher: c.matcher,
      ruleId: a,
      grammar: r,
      priority: c.priority
    });
}
function W(s, e) {
  if (e.length < s.length)
    return !1;
  let t = 0;
  return s.every((n) => {
    for (let r = t; r < e.length; r++)
      if (ot(e[r], n))
        return t = r + 1, !0;
    return !1;
  });
}
function ot(s, e) {
  if (!s)
    return !1;
  if (s === e)
    return !0;
  const t = e.length;
  return s.length > t && s.substr(0, t) === e && s[t] === ".";
}
var at = class {
  constructor(s, e, t, n, r, i, a, c) {
    l(this, "_rootId");
    l(this, "_lastRuleId");
    l(this, "_ruleId2desc");
    l(this, "_includedGrammars");
    l(this, "_grammarRepository");
    l(this, "_grammar");
    l(this, "_injections");
    l(this, "_basicScopeAttributesProvider");
    l(this, "_tokenTypeMatchers");
    if (this._rootScopeName = s, this.balancedBracketSelectors = i, this._onigLib = c, this._basicScopeAttributesProvider = new Je(
      t,
      n
    ), this._rootId = -1, this._lastRuleId = 0, this._ruleId2desc = [null], this._includedGrammars = {}, this._grammarRepository = a, this._grammar = ae(e, null), this._injections = null, this._tokenTypeMatchers = [], r)
      for (const o of Object.keys(r)) {
        const h = j(o, W);
        for (const u of h)
          this._tokenTypeMatchers.push({
            matcher: u.matcher,
            type: r[o]
          });
      }
  }
  get themeProvider() {
    return this._grammarRepository;
  }
  dispose() {
    for (const s of this._ruleId2desc)
      s && s.dispose();
  }
  createOnigScanner(s) {
    return this._onigLib.createOnigScanner(s);
  }
  createOnigString(s) {
    return this._onigLib.createOnigString(s);
  }
  getMetadataForScope(s) {
    return this._basicScopeAttributesProvider.getBasicScopeAttributes(s);
  }
  _collectInjections() {
    const s = {
      lookup: (r) => r === this._rootScopeName ? this._grammar : this.getExternalGrammar(r),
      injections: (r) => this._grammarRepository.injections(r)
    }, e = [], t = this._rootScopeName, n = s.lookup(t);
    if (n) {
      const r = n.injections;
      if (r)
        for (let a in r)
          oe(
            e,
            a,
            r[a],
            this,
            n
          );
      const i = this._grammarRepository.injections(t);
      i && i.forEach((a) => {
        const c = this.getExternalGrammar(a);
        if (c) {
          const o = c.injectionSelector;
          o && oe(
            e,
            o,
            c,
            this,
            c
          );
        }
      });
    }
    return e.sort((r, i) => r.priority - i.priority), e;
  }
  getInjections() {
    return this._injections === null && (this._injections = this._collectInjections()), this._injections;
  }
  registerRule(s) {
    const e = ++this._lastRuleId, t = s(e);
    return this._ruleId2desc[e] = t, t;
  }
  getRule(s) {
    return this._ruleId2desc[s];
  }
  getExternalGrammar(s, e) {
    if (this._includedGrammars[s])
      return this._includedGrammars[s];
    if (this._grammarRepository) {
      const t = this._grammarRepository.lookup(s);
      if (t)
        return this._includedGrammars[s] = ae(
          t,
          e && e.$base
        ), this._includedGrammars[s];
    }
  }
  tokenizeLine(s, e, t = 0) {
    const n = this._tokenize(s, e, !1, t);
    return {
      tokens: n.lineTokens.getResult(n.ruleStack, n.lineLength),
      ruleStack: n.ruleStack,
      stoppedEarly: n.stoppedEarly
    };
  }
  tokenizeLine2(s, e, t = 0) {
    const n = this._tokenize(s, e, !0, t);
    return {
      tokens: n.lineTokens.getBinaryResult(n.ruleStack, n.lineLength),
      ruleStack: n.ruleStack,
      stoppedEarly: n.stoppedEarly
    };
  }
  _tokenize(s, e, t, n) {
    this._rootId === -1 && (this._rootId = Ce.getCompiledRuleId(
      this._grammar.repository.$self,
      this,
      this._grammar.repository
    ), this.getInjections());
    let r;
    if (!e || e === Y.NULL) {
      r = !0;
      const h = this._basicScopeAttributesProvider.getDefaultAttributes(), u = this.themeProvider.getDefaults(), d = O.set(
        0,
        h.languageId,
        h.tokenType,
        null,
        u.fontStyle,
        u.foregroundId,
        u.backgroundId
      ), m = this.getRule(this._rootId).getName(
        null,
        null
      );
      let f;
      m ? f = v.createRootAndLookUpScopeName(
        m,
        d,
        this
      ) : f = v.createRoot(
        "unknown",
        d
      ), e = new Y(
        null,
        this._rootId,
        -1,
        -1,
        !1,
        null,
        f,
        f
      );
    } else
      r = !1, e.reset();
    s = s + `
`;
    const i = this.createOnigString(s), a = i.content.length, c = new lt(
      t,
      s,
      this._tokenTypeMatchers,
      this.balancedBracketSelectors
    ), o = ye(
      this,
      i,
      r,
      0,
      e,
      c,
      !0,
      n
    );
    return _e(i), {
      lineLength: a,
      lineTokens: c,
      ruleStack: o.stack,
      stoppedEarly: o.stoppedEarly
    };
  }
};
function ae(s, e) {
  return s = Ae(s), s.repository = s.repository || {}, s.repository.$self = {
    $vscodeTextmateLocation: s.$vscodeTextmateLocation,
    patterns: s.patterns,
    name: s.scopeName
  }, s.repository.$base = e || s.repository.$self, s;
}
var v = class b {
  /**
   * Invariant:
   * ```
   * if (parent && !scopePath.extends(parent.scopePath)) {
   * 	throw new Error();
   * }
   * ```
   */
  constructor(e, t, n) {
    this.parent = e, this.scopePath = t, this.tokenAttributes = n;
  }
  static fromExtension(e, t) {
    let n = e, r = (e == null ? void 0 : e.scopePath) ?? null;
    for (const i of t)
      r = U.push(r, i.scopeNames), n = new b(n, r, i.encodedTokenAttributes);
    return n;
  }
  static createRoot(e, t) {
    return new b(null, new U(null, e), t);
  }
  static createRootAndLookUpScopeName(e, t, n) {
    const r = n.getMetadataForScope(e), i = new U(null, e), a = n.themeProvider.themeMatch(i), c = b.mergeAttributes(
      t,
      r,
      a
    );
    return new b(null, i, c);
  }
  get scopeName() {
    return this.scopePath.scopeName;
  }
  toString() {
    return this.getScopeNames().join(" ");
  }
  equals(e) {
    return b.equals(this, e);
  }
  static equals(e, t) {
    do {
      if (e === t || !e && !t)
        return !0;
      if (!e || !t || e.scopeName !== t.scopeName || e.tokenAttributes !== t.tokenAttributes)
        return !1;
      e = e.parent, t = t.parent;
    } while (!0);
  }
  static mergeAttributes(e, t, n) {
    let r = -1, i = 0, a = 0;
    return n !== null && (r = n.fontStyle, i = n.foregroundId, a = n.backgroundId), O.set(
      e,
      t.languageId,
      t.tokenType,
      null,
      r,
      i,
      a
    );
  }
  pushAttributed(e, t) {
    if (e === null)
      return this;
    if (e.indexOf(" ") === -1)
      return b._pushAttributed(this, e, t);
    const n = e.split(/ /g);
    let r = this;
    for (const i of n)
      r = b._pushAttributed(r, i, t);
    return r;
  }
  static _pushAttributed(e, t, n) {
    const r = n.getMetadataForScope(t), i = e.scopePath.push(t), a = n.themeProvider.themeMatch(i), c = b.mergeAttributes(
      e.tokenAttributes,
      r,
      a
    );
    return new b(e, i, c);
  }
  getScopeNames() {
    return this.scopePath.getSegments();
  }
  getExtensionIfDefined(e) {
    var r;
    const t = [];
    let n = this;
    for (; n && n !== e; )
      t.push({
        encodedTokenAttributes: n.tokenAttributes,
        scopeNames: n.scopePath.getExtensionIfDefined(((r = n.parent) == null ? void 0 : r.scopePath) ?? null)
      }), n = n.parent;
    return n === e ? t.reverse() : void 0;
  }
}, y, Y = (y = class {
  /**
   * Invariant:
   * ```
   * if (contentNameScopesList !== nameScopesList && contentNameScopesList?.parent !== nameScopesList) {
   * 	throw new Error();
   * }
   * if (this.parent && !nameScopesList.extends(this.parent.contentNameScopesList)) {
   * 	throw new Error();
   * }
   * ```
   */
  constructor(e, t, n, r, i, a, c, o) {
    l(this, "_stackElementBrand");
    /**
     * The position on the current line where this state was pushed.
     * This is relevant only while tokenizing a line, to detect endless loops.
     * Its value is meaningless across lines.
     */
    l(this, "_enterPos");
    /**
     * The captured anchor position when this stack element was pushed.
     * This is relevant only while tokenizing a line, to restore the anchor position when popping.
     * Its value is meaningless across lines.
     */
    l(this, "_anchorPos");
    /**
     * The depth of the stack.
     */
    l(this, "depth");
    this.parent = e, this.ruleId = t, this.beginRuleCapturedEOL = i, this.endRule = a, this.nameScopesList = c, this.contentNameScopesList = o, this.depth = this.parent ? this.parent.depth + 1 : 1, this._enterPos = n, this._anchorPos = r;
  }
  equals(e) {
    return e === null ? !1 : y._equals(this, e);
  }
  static _equals(e, t) {
    return e === t ? !0 : this._structuralEquals(e, t) ? v.equals(e.contentNameScopesList, t.contentNameScopesList) : !1;
  }
  /**
   * A structural equals check. Does not take into account `scopes`.
   */
  static _structuralEquals(e, t) {
    do {
      if (e === t || !e && !t)
        return !0;
      if (!e || !t || e.depth !== t.depth || e.ruleId !== t.ruleId || e.endRule !== t.endRule)
        return !1;
      e = e.parent, t = t.parent;
    } while (!0);
  }
  clone() {
    return this;
  }
  static _reset(e) {
    for (; e; )
      e._enterPos = -1, e._anchorPos = -1, e = e.parent;
  }
  reset() {
    y._reset(this);
  }
  pop() {
    return this.parent;
  }
  safePop() {
    return this.parent ? this.parent : this;
  }
  push(e, t, n, r, i, a, c) {
    return new y(
      this,
      e,
      t,
      n,
      r,
      i,
      a,
      c
    );
  }
  getEnterPos() {
    return this._enterPos;
  }
  getAnchorPos() {
    return this._anchorPos;
  }
  getRule(e) {
    return e.getRule(this.ruleId);
  }
  toString() {
    const e = [];
    return this._writeString(e, 0), "[" + e.join(",") + "]";
  }
  _writeString(e, t) {
    var n, r;
    return this.parent && (t = this.parent._writeString(e, t)), e[t++] = `(${this.ruleId}, ${(n = this.nameScopesList) == null ? void 0 : n.toString()}, ${(r = this.contentNameScopesList) == null ? void 0 : r.toString()})`, t;
  }
  withContentNameScopesList(e) {
    return this.contentNameScopesList === e ? this : this.parent.push(
      this.ruleId,
      this._enterPos,
      this._anchorPos,
      this.beginRuleCapturedEOL,
      this.endRule,
      this.nameScopesList,
      e
    );
  }
  withEndRule(e) {
    return this.endRule === e ? this : new y(
      this.parent,
      this.ruleId,
      this._enterPos,
      this._anchorPos,
      this.beginRuleCapturedEOL,
      e,
      this.nameScopesList,
      this.contentNameScopesList
    );
  }
  // Used to warn of endless loops
  hasSameRuleAs(e) {
    let t = this;
    for (; t && t._enterPos === e._enterPos; ) {
      if (t.ruleId === e.ruleId)
        return !0;
      t = t.parent;
    }
    return !1;
  }
  toStateStackFrame() {
    var e, t, n;
    return {
      ruleId: this.ruleId,
      beginRuleCapturedEOL: this.beginRuleCapturedEOL,
      endRule: this.endRule,
      nameScopesList: ((t = this.nameScopesList) == null ? void 0 : t.getExtensionIfDefined(((e = this.parent) == null ? void 0 : e.nameScopesList) ?? null)) ?? [],
      contentNameScopesList: ((n = this.contentNameScopesList) == null ? void 0 : n.getExtensionIfDefined(this.nameScopesList)) ?? []
    };
  }
  static pushFrame(e, t) {
    const n = v.fromExtension((e == null ? void 0 : e.nameScopesList) ?? null, t.nameScopesList);
    return new y(
      e,
      t.ruleId,
      t.enterPos ?? -1,
      t.anchorPos ?? -1,
      t.beginRuleCapturedEOL,
      t.endRule,
      n,
      v.fromExtension(n, t.contentNameScopesList)
    );
  }
}, // TODO remove me
l(y, "NULL", new y(
  null,
  0,
  0,
  0,
  !1,
  null,
  null,
  null
)), y), ct = class {
  constructor(s, e) {
    l(this, "balancedBracketScopes");
    l(this, "unbalancedBracketScopes");
    l(this, "allowAny", !1);
    this.balancedBracketScopes = s.flatMap(
      (t) => t === "*" ? (this.allowAny = !0, []) : j(t, W).map((n) => n.matcher)
    ), this.unbalancedBracketScopes = e.flatMap(
      (t) => j(t, W).map((n) => n.matcher)
    );
  }
  get matchesAlways() {
    return this.allowAny && this.unbalancedBracketScopes.length === 0;
  }
  get matchesNever() {
    return this.balancedBracketScopes.length === 0 && !this.allowAny;
  }
  match(s) {
    for (const e of this.unbalancedBracketScopes)
      if (e(s))
        return !1;
    for (const e of this.balancedBracketScopes)
      if (e(s))
        return !0;
    return this.allowAny;
  }
}, lt = class {
  constructor(s, e, t, n) {
    l(this, "_emitBinaryTokens");
    /**
     * defined only if `false`.
     */
    l(this, "_lineText");
    /**
     * used only if `_emitBinaryTokens` is false.
     */
    l(this, "_tokens");
    /**
     * used only if `_emitBinaryTokens` is true.
     */
    l(this, "_binaryTokens");
    l(this, "_lastTokenEndIndex");
    l(this, "_tokenTypeOverrides");
    this.balancedBracketSelectors = n, this._emitBinaryTokens = s, this._tokenTypeOverrides = t, this._lineText = null, this._tokens = [], this._binaryTokens = [], this._lastTokenEndIndex = 0;
  }
  produce(s, e) {
    this.produceFromScopes(s.contentNameScopesList, e);
  }
  produceFromScopes(s, e) {
    var n;
    if (this._lastTokenEndIndex >= e)
      return;
    if (this._emitBinaryTokens) {
      let r = (s == null ? void 0 : s.tokenAttributes) ?? 0, i = !1;
      if ((n = this.balancedBracketSelectors) != null && n.matchesAlways && (i = !0), this._tokenTypeOverrides.length > 0 || this.balancedBracketSelectors && !this.balancedBracketSelectors.matchesAlways && !this.balancedBracketSelectors.matchesNever) {
        const a = (s == null ? void 0 : s.getScopeNames()) ?? [];
        for (const c of this._tokenTypeOverrides)
          c.matcher(a) && (r = O.set(
            r,
            0,
            c.type,
            null,
            -1,
            0,
            0
          ));
        this.balancedBracketSelectors && (i = this.balancedBracketSelectors.match(a));
      }
      if (i && (r = O.set(
        r,
        0,
        8,
        i,
        -1,
        0,
        0
      )), this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 1] === r) {
        this._lastTokenEndIndex = e;
        return;
      }
      this._binaryTokens.push(this._lastTokenEndIndex), this._binaryTokens.push(r), this._lastTokenEndIndex = e;
      return;
    }
    const t = (s == null ? void 0 : s.getScopeNames()) ?? [];
    this._tokens.push({
      startIndex: this._lastTokenEndIndex,
      endIndex: e,
      // value: lineText.substring(lastTokenEndIndex, endIndex),
      scopes: t
    }), this._lastTokenEndIndex = e;
  }
  getResult(s, e) {
    return this._tokens.length > 0 && this._tokens[this._tokens.length - 1].startIndex === e - 1 && this._tokens.pop(), this._tokens.length === 0 && (this._lastTokenEndIndex = -1, this.produce(s, e), this._tokens[this._tokens.length - 1].startIndex = 0), this._tokens;
  }
  getBinaryResult(s, e) {
    this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 2] === e - 1 && (this._binaryTokens.pop(), this._binaryTokens.pop()), this._binaryTokens.length === 0 && (this._lastTokenEndIndex = -1, this.produce(s, e), this._binaryTokens[this._binaryTokens.length - 2] = 0);
    const t = new Uint32Array(this._binaryTokens.length);
    for (let n = 0, r = this._binaryTokens.length; n < r; n++)
      t[n] = this._binaryTokens[n];
    return t;
  }
}, ht = class {
  constructor(s, e) {
    l(this, "_grammars", /* @__PURE__ */ new Map());
    l(this, "_rawGrammars", /* @__PURE__ */ new Map());
    l(this, "_injectionGrammars", /* @__PURE__ */ new Map());
    l(this, "_theme");
    this._onigLib = e, this._theme = s;
  }
  dispose() {
    for (const s of this._grammars.values())
      s.dispose();
  }
  setTheme(s) {
    this._theme = s;
  }
  getColorMap() {
    return this._theme.getColorMap();
  }
  /**
   * Add `grammar` to registry and return a list of referenced scope names
   */
  addGrammar(s, e) {
    this._rawGrammars.set(s.scopeName, s), e && this._injectionGrammars.set(s.scopeName, e);
  }
  /**
   * Lookup a raw grammar.
   */
  lookup(s) {
    return this._rawGrammars.get(s);
  }
  /**
   * Returns the injections for the given grammar
   */
  injections(s) {
    return this._injectionGrammars.get(s);
  }
  /**
   * Get the default theme settings
   */
  getDefaults() {
    return this._theme.getDefaults();
  }
  /**
   * Match a scope in the theme.
   */
  themeMatch(s) {
    return this._theme.match(s);
  }
  /**
   * Lookup a grammar.
   */
  grammarForScopeName(s, e, t, n, r) {
    if (!this._grammars.has(s)) {
      let i = this._rawGrammars.get(s);
      if (!i)
        return null;
      this._grammars.set(s, it(
        s,
        i,
        e,
        t,
        n,
        r,
        this,
        this._onigLib
      ));
    }
    return this._grammars.get(s);
  }
}, dt = class {
  constructor(e) {
    l(this, "_options");
    l(this, "_syncRegistry");
    l(this, "_ensureGrammarCache");
    this._options = e, this._syncRegistry = new ht(
      H.createFromRawTheme(e.theme, e.colorMap),
      e.onigLib
    ), this._ensureGrammarCache = /* @__PURE__ */ new Map();
  }
  dispose() {
    this._syncRegistry.dispose();
  }
  /**
   * Change the theme. Once called, no previous `ruleStack` should be used anymore.
   */
  setTheme(e, t) {
    this._syncRegistry.setTheme(H.createFromRawTheme(e, t));
  }
  /**
   * Returns a lookup array for color ids.
   */
  getColorMap() {
    return this._syncRegistry.getColorMap();
  }
  /**
   * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
   * Please do not use language id 0.
   */
  loadGrammarWithEmbeddedLanguages(e, t, n) {
    return this.loadGrammarWithConfiguration(e, t, { embeddedLanguages: n });
  }
  /**
   * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
   * Please do not use language id 0.
   */
  loadGrammarWithConfiguration(e, t, n) {
    return this._loadGrammar(
      e,
      t,
      n.embeddedLanguages,
      n.tokenTypes,
      new ct(
        n.balancedBracketSelectors || [],
        n.unbalancedBracketSelectors || []
      )
    );
  }
  /**
   * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
   */
  loadGrammar(e) {
    return this._loadGrammar(e, 0, null, null, null);
  }
  _loadGrammar(e, t, n, r, i) {
    const a = new Fe(this._syncRegistry, e);
    for (; a.Q.length > 0; )
      a.Q.map((c) => this._loadSingleGrammar(c.scopeName)), a.processQueue();
    return this._grammarForScopeName(
      e,
      t,
      n,
      r,
      i
    );
  }
  _loadSingleGrammar(e) {
    this._ensureGrammarCache.has(e) || (this._doLoadSingleGrammar(e), this._ensureGrammarCache.set(e, !0));
  }
  _doLoadSingleGrammar(e) {
    const t = this._options.loadGrammar(e);
    if (t) {
      const n = typeof this._options.getInjections == "function" ? this._options.getInjections(e) : void 0;
      this._syncRegistry.addGrammar(t, n);
    }
  }
  /**
   * Adds a rawGrammar.
   */
  addGrammar(e, t = [], n = 0, r = null) {
    return this._syncRegistry.addGrammar(e, t), this._grammarForScopeName(e.scopeName, n, r);
  }
  /**
   * Get the grammar for `scopeName`. The grammar must first be created via `loadGrammar` or `addGrammar`.
   */
  _grammarForScopeName(e, t = 0, n = null, r = null, i = null) {
    return this._syncRegistry.grammarForScopeName(
      e,
      t,
      n,
      r,
      i
    );
  }
}, ft = Y.NULL;
export {
  O as EncodedTokenMetadata,
  Te as FontStyle,
  ft as INITIAL,
  dt as Registry,
  H as Theme,
  _e as disposeOnigString
};
