var xe = Object.defineProperty;
var o = (l, e) => xe(l, "name", { value: e, configurable: !0 });
const _e = 1024;
let ye = 0;
class ee {
  static {
    o(this, "Range");
  }
  constructor(e, t) {
    this.from = e, this.to = t;
  }
}
class C {
  static {
    o(this, "NodeProp");
  }
  /**
  Create a new node prop type.
  */
  constructor(e = {}) {
    this.id = ye++, this.perNode = !!e.perNode, this.deserialize = e.deserialize || (() => {
      throw new Error("This node type doesn't define a deserialize function");
    });
  }
  /**
  This is meant to be used with
  [`NodeSet.extend`](#common.NodeSet.extend) or
  [`LRParser.configure`](#lr.ParserConfig.props) to compute
  prop values for each node type in the set. Takes a [match
  object](#common.NodeType^match) or function that returns undefined
  if the node type doesn't get this prop, and the prop's value if
  it does.
  */
  add(e) {
    if (this.perNode)
      throw new RangeError("Can't add per-node props to node types");
    return typeof e != "function" && (e = R.match(e)), (t) => {
      let r = e(t);
      return r === void 0 ? null : [this, r];
    };
  }
}
C.closedBy = new C({ deserialize: /* @__PURE__ */ o((l) => l.split(" "), "deserialize") });
C.openedBy = new C({ deserialize: /* @__PURE__ */ o((l) => l.split(" "), "deserialize") });
C.group = new C({ deserialize: /* @__PURE__ */ o((l) => l.split(" "), "deserialize") });
C.isolate = new C({ deserialize: /* @__PURE__ */ o((l) => {
  if (l && l != "rtl" && l != "ltr" && l != "auto")
    throw new RangeError("Invalid value for isolate: " + l);
  return l || "auto";
}, "deserialize") });
C.contextHash = new C({ perNode: !0 });
C.lookAhead = new C({ perNode: !0 });
C.mounted = new C({ perNode: !0 });
class Y {
  static {
    o(this, "MountedTree");
  }
  constructor(e, t, r) {
    this.tree = e, this.overlay = t, this.parser = r;
  }
  /**
  @internal
  */
  static get(e) {
    return e && e.props && e.props[C.mounted.id];
  }
}
const be = /* @__PURE__ */ Object.create(null);
class R {
  static {
    o(this, "NodeType");
  }
  /**
  @internal
  */
  constructor(e, t, r, i = 0) {
    this.name = e, this.props = t, this.id = r, this.flags = i;
  }
  /**
  Define a node type.
  */
  static define(e) {
    let t = e.props && e.props.length ? /* @__PURE__ */ Object.create(null) : be, r = (e.top ? 1 : 0) | (e.skipped ? 2 : 0) | (e.error ? 4 : 0) | (e.name == null ? 8 : 0), i = new R(e.name || "", t, e.id, r);
    if (e.props) {
      for (let n of e.props)
        if (Array.isArray(n) || (n = n(i)), n) {
          if (n[0].perNode)
            throw new RangeError("Can't store a per-node prop on a node type");
          t[n[0].id] = n[1];
        }
    }
    return i;
  }
  /**
  Retrieves a node prop for this type. Will return `undefined` if
  the prop isn't present on this node.
  */
  prop(e) {
    return this.props[e.id];
  }
  /**
  True when this is the top node of a grammar.
  */
  get isTop() {
    return (this.flags & 1) > 0;
  }
  /**
  True when this node is produced by a skip rule.
  */
  get isSkipped() {
    return (this.flags & 2) > 0;
  }
  /**
  Indicates whether this is an error node.
  */
  get isError() {
    return (this.flags & 4) > 0;
  }
  /**
  When true, this node type doesn't correspond to a user-declared
  named node, for example because it is used to cache repetition.
  */
  get isAnonymous() {
    return (this.flags & 8) > 0;
  }
  /**
  Returns true when this node's name or one of its
  [groups](#common.NodeProp^group) matches the given string.
  */
  is(e) {
    if (typeof e == "string") {
      if (this.name == e)
        return !0;
      let t = this.prop(C.group);
      return t ? t.indexOf(e) > -1 : !1;
    }
    return this.id == e;
  }
  /**
  Create a function from node types to arbitrary values by
  specifying an object whose property names are node or
  [group](#common.NodeProp^group) names. Often useful with
  [`NodeProp.add`](#common.NodeProp.add). You can put multiple
  names, separated by spaces, in a single property name to map
  multiple node names to a single value.
  */
  static match(e) {
    let t = /* @__PURE__ */ Object.create(null);
    for (let r in e)
      for (let i of r.split(" "))
        t[i] = e[r];
    return (r) => {
      for (let i = r.prop(C.group), n = -1; n < (i ? i.length : 0); n++) {
        let s = t[n < 0 ? r.name : i[n]];
        if (s)
          return s;
      }
    };
  }
}
R.none = new R(
  "",
  /* @__PURE__ */ Object.create(null),
  0,
  8
  /* NodeFlag.Anonymous */
);
class pe {
  static {
    o(this, "NodeSet");
  }
  /**
  Create a set with the given types. The `id` property of each
  type should correspond to its position within the array.
  */
  constructor(e) {
    this.types = e;
    for (let t = 0; t < e.length; t++)
      if (e[t].id != t)
        throw new RangeError("Node type ids should correspond to array positions when creating a node set");
  }
  /**
  Create a copy of this set with some node properties added. The
  arguments to this method can be created with
  [`NodeProp.add`](#common.NodeProp.add).
  */
  extend(...e) {
    let t = [];
    for (let r of this.types) {
      let i = null;
      for (let n of e) {
        let s = n(r);
        s && (i || (i = Object.assign({}, r.props)), i[s[0].id] = s[1]);
      }
      t.push(i ? new R(r.name, i, r.id, r.flags) : r);
    }
    return new pe(t);
  }
}
const Q = /* @__PURE__ */ new WeakMap(), ue = /* @__PURE__ */ new WeakMap();
var B;
(function(l) {
  l[l.ExcludeBuffers = 1] = "ExcludeBuffers", l[l.IncludeAnonymous = 2] = "IncludeAnonymous", l[l.IgnoreMounts = 4] = "IgnoreMounts", l[l.IgnoreOverlays = 8] = "IgnoreOverlays";
})(B || (B = {}));
class j {
  static {
    o(this, "Tree");
  }
  /**
  Construct a new tree. See also [`Tree.build`](#common.Tree^build).
  */
  constructor(e, t, r, i, n) {
    if (this.type = e, this.children = t, this.positions = r, this.length = i, this.props = null, n && n.length) {
      this.props = /* @__PURE__ */ Object.create(null);
      for (let [s, h] of n)
        this.props[typeof s == "number" ? s : s.id] = h;
    }
  }
  /**
  @internal
  */
  toString() {
    let e = Y.get(this);
    if (e && !e.overlay)
      return e.tree.toString();
    let t = "";
    for (let r of this.children) {
      let i = r.toString();
      i && (t && (t += ","), t += i);
    }
    return this.type.name ? (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) + (t.length ? "(" + t + ")" : "") : t;
  }
  /**
  Get a [tree cursor](#common.TreeCursor) positioned at the top of
  the tree. Mode can be used to [control](#common.IterMode) which
  nodes the cursor visits.
  */
  cursor(e = 0) {
    return new re(this.topNode, e);
  }
  /**
  Get a [tree cursor](#common.TreeCursor) pointing into this tree
  at the given position and side (see
  [`moveTo`](#common.TreeCursor.moveTo).
  */
  cursorAt(e, t = 0, r = 0) {
    let i = Q.get(this) || this.topNode, n = new re(i);
    return n.moveTo(e, t), Q.set(this, n._tree), n;
  }
  /**
  Get a [syntax node](#common.SyntaxNode) object for the top of the
  tree.
  */
  get topNode() {
    return new z(this, 0, 0, null);
  }
  /**
  Get the [syntax node](#common.SyntaxNode) at the given position.
  If `side` is -1, this will move into nodes that end at the
  position. If 1, it'll move into nodes that start at the
  position. With 0, it'll only enter nodes that cover the position
  from both sides.
  
  Note that this will not enter
  [overlays](#common.MountedTree.overlay), and you often want
  [`resolveInner`](#common.Tree.resolveInner) instead.
  */
  resolve(e, t = 0) {
    let r = $(Q.get(this) || this.topNode, e, t, !1);
    return Q.set(this, r), r;
  }
  /**
  Like [`resolve`](#common.Tree.resolve), but will enter
  [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
  pointing into the innermost overlaid tree at the given position
  (with parent links going through all parent structure, including
  the host trees).
  */
  resolveInner(e, t = 0) {
    let r = $(ue.get(this) || this.topNode, e, t, !0);
    return ue.set(this, r), r;
  }
  /**
  In some situations, it can be useful to iterate through all
  nodes around a position, including those in overlays that don't
  directly cover the position. This method gives you an iterator
  that will produce all nodes, from small to big, around the given
  position.
  */
  resolveStack(e, t = 0) {
    return ke(this, e, t);
  }
  /**
  Iterate over the tree and its children, calling `enter` for any
  node that touches the `from`/`to` region (if given) before
  running over such a node's children, and `leave` (if given) when
  leaving the node. When `enter` returns `false`, that node will
  not have its children iterated over (or `leave` called).
  */
  iterate(e) {
    let { enter: t, leave: r, from: i = 0, to: n = this.length } = e, s = e.mode || 0, h = (s & B.IncludeAnonymous) > 0;
    for (let a = this.cursor(s | B.IncludeAnonymous); ; ) {
      let S = !1;
      if (a.from <= n && a.to >= i && (!h && a.type.isAnonymous || t(a) !== !1)) {
        if (a.firstChild())
          continue;
        S = !0;
      }
      for (; S && r && (h || !a.type.isAnonymous) && r(a), !a.nextSibling(); ) {
        if (!a.parent())
          return;
        S = !0;
      }
    }
  }
  /**
  Get the value of the given [node prop](#common.NodeProp) for this
  node. Works with both per-node and per-type props.
  */
  prop(e) {
    return e.perNode ? this.props ? this.props[e.id] : void 0 : this.type.prop(e);
  }
  /**
  Returns the node's [per-node props](#common.NodeProp.perNode) in a
  format that can be passed to the [`Tree`](#common.Tree)
  constructor.
  */
  get propValues() {
    let e = [];
    if (this.props)
      for (let t in this.props)
        e.push([+t, this.props[t]]);
    return e;
  }
  /**
  Balance the direct children of this tree, producing a copy of
  which may have children grouped into subtrees with type
  [`NodeType.none`](#common.NodeType^none).
  */
  balance(e = {}) {
    return this.children.length <= 8 ? this : le(R.none, this.children, this.positions, 0, this.children.length, 0, this.length, (t, r, i) => new j(this.type, t, r, i, this.propValues), e.makeTree || ((t, r, i) => new j(R.none, t, r, i)));
  }
  /**
  Build a tree from a postfix-ordered buffer of node information,
  or a cursor over such a buffer.
  */
  static build(e) {
    return Ce(e);
  }
}
j.empty = new j(R.none, [], [], 0);
class ne {
  static {
    o(this, "FlatBufferCursor");
  }
  constructor(e, t) {
    this.buffer = e, this.index = t;
  }
  get id() {
    return this.buffer[this.index - 4];
  }
  get start() {
    return this.buffer[this.index - 3];
  }
  get end() {
    return this.buffer[this.index - 2];
  }
  get size() {
    return this.buffer[this.index - 1];
  }
  get pos() {
    return this.index;
  }
  next() {
    this.index -= 4;
  }
  fork() {
    return new ne(this.buffer, this.index);
  }
}
class T {
  static {
    o(this, "TreeBuffer");
  }
  /**
  Create a tree buffer.
  */
  constructor(e, t, r) {
    this.buffer = e, this.length = t, this.set = r;
  }
  /**
  @internal
  */
  get type() {
    return R.none;
  }
  /**
  @internal
  */
  toString() {
    let e = [];
    for (let t = 0; t < this.buffer.length; )
      e.push(this.childString(t)), t = this.buffer[t + 3];
    return e.join(",");
  }
  /**
  @internal
  */
  childString(e) {
    let t = this.buffer[e], r = this.buffer[e + 3], i = this.set.types[t], n = i.name;
    if (/\W/.test(n) && !i.isError && (n = JSON.stringify(n)), e += 4, r == e)
      return n;
    let s = [];
    for (; e < r; )
      s.push(this.childString(e)), e = this.buffer[e + 3];
    return n + "(" + s.join(",") + ")";
  }
  /**
  @internal
  */
  findChild(e, t, r, i, n) {
    let { buffer: s } = this, h = -1;
    for (let a = e; a != t && !(de(n, i, s[a + 1], s[a + 2]) && (h = a, r > 0)); a = s[a + 3])
      ;
    return h;
  }
  /**
  @internal
  */
  slice(e, t, r) {
    let i = this.buffer, n = new Uint16Array(t - e), s = 0;
    for (let h = e, a = 0; h < t; ) {
      n[a++] = i[h++], n[a++] = i[h++] - r;
      let S = n[a++] = i[h++] - r;
      n[a++] = i[h++] - e, s = Math.max(s, S);
    }
    return new T(n, s, this.set);
  }
}
function de(l, e, t, r) {
  switch (l) {
    case -2:
      return t < e;
    case -1:
      return r >= e && t < e;
    case 0:
      return t < e && r > e;
    case 1:
      return t <= e && r > e;
    case 2:
      return r > e;
    case 4:
      return !0;
  }
}
o(de, "checkSide");
function $(l, e, t, r) {
  for (var i; l.from == l.to || (t < 1 ? l.from >= e : l.from > e) || (t > -1 ? l.to <= e : l.to < e); ) {
    let s = !r && l instanceof z && l.index < 0 ? null : l.parent;
    if (!s)
      return l;
    l = s;
  }
  let n = r ? 0 : B.IgnoreOverlays;
  if (r)
    for (let s = l, h = s.parent; h; s = h, h = s.parent)
      s instanceof z && s.index < 0 && ((i = h.enter(e, t, n)) === null || i === void 0 ? void 0 : i.from) != s.from && (l = h);
  for (; ; ) {
    let s = l.enter(e, t, n);
    if (!s)
      return l;
    l = s;
  }
}
o($, "resolveNode");
class ce {
  static {
    o(this, "BaseNode");
  }
  cursor(e = 0) {
    return new re(this, e);
  }
  getChild(e, t = null, r = null) {
    let i = oe(this, e, t, r);
    return i.length ? i[0] : null;
  }
  getChildren(e, t = null, r = null) {
    return oe(this, e, t, r);
  }
  resolve(e, t = 0) {
    return $(this, e, t, !1);
  }
  resolveInner(e, t = 0) {
    return $(this, e, t, !0);
  }
  matchContext(e) {
    return te(this.parent, e);
  }
  enterUnfinishedNodesBefore(e) {
    let t = this.childBefore(e), r = this;
    for (; t; ) {
      let i = t.lastChild;
      if (!i || i.to != t.to)
        break;
      i.type.isError && i.from == i.to ? (r = t, t = i.prevSibling) : t = i;
    }
    return r;
  }
  get node() {
    return this;
  }
  get next() {
    return this.parent;
  }
}
class z extends ce {
  static {
    o(this, "TreeNode");
  }
  constructor(e, t, r, i) {
    super(), this._tree = e, this.from = t, this.index = r, this._parent = i;
  }
  get type() {
    return this._tree.type;
  }
  get name() {
    return this._tree.type.name;
  }
  get to() {
    return this.from + this._tree.length;
  }
  nextChild(e, t, r, i, n = 0) {
    for (let s = this; ; ) {
      for (let { children: h, positions: a } = s._tree, S = t > 0 ? h.length : -1; e != S; e += t) {
        let c = h[e], A = a[e] + s.from;
        if (de(i, r, A, A + c.length)) {
          if (c instanceof T) {
            if (n & B.ExcludeBuffers)
              continue;
            let k = c.findChild(0, c.buffer.length, t, r - A, i);
            if (k > -1)
              return new O(new me(s, c, e, A), null, k);
          } else if (n & B.IncludeAnonymous || !c.type.isAnonymous || se(c)) {
            let k;
            if (!(n & B.IgnoreMounts) && (k = Y.get(c)) && !k.overlay)
              return new z(k.tree, A, e, s);
            let P = new z(c, A, e, s);
            return n & B.IncludeAnonymous || !P.type.isAnonymous ? P : P.nextChild(t < 0 ? c.children.length - 1 : 0, t, r, i);
          }
        }
      }
      if (n & B.IncludeAnonymous || !s.type.isAnonymous || (s.index >= 0 ? e = s.index + t : e = t < 0 ? -1 : s._parent._tree.children.length, s = s._parent, !s))
        return null;
    }
  }
  get firstChild() {
    return this.nextChild(
      0,
      1,
      0,
      4
      /* Side.DontCare */
    );
  }
  get lastChild() {
    return this.nextChild(
      this._tree.children.length - 1,
      -1,
      0,
      4
      /* Side.DontCare */
    );
  }
  childAfter(e) {
    return this.nextChild(
      0,
      1,
      e,
      2
      /* Side.After */
    );
  }
  childBefore(e) {
    return this.nextChild(
      this._tree.children.length - 1,
      -1,
      e,
      -2
      /* Side.Before */
    );
  }
  enter(e, t, r = 0) {
    let i;
    if (!(r & B.IgnoreOverlays) && (i = Y.get(this._tree)) && i.overlay) {
      let n = e - this.from;
      for (let { from: s, to: h } of i.overlay)
        if ((t > 0 ? s <= n : s < n) && (t < 0 ? h >= n : h > n))
          return new z(i.tree, i.overlay[0].from + this.from, -1, this);
    }
    return this.nextChild(0, 1, e, t, r);
  }
  nextSignificantParent() {
    let e = this;
    for (; e.type.isAnonymous && e._parent; )
      e = e._parent;
    return e;
  }
  get parent() {
    return this._parent ? this._parent.nextSignificantParent() : null;
  }
  get nextSibling() {
    return this._parent && this.index >= 0 ? this._parent.nextChild(
      this.index + 1,
      1,
      0,
      4
      /* Side.DontCare */
    ) : null;
  }
  get prevSibling() {
    return this._parent && this.index >= 0 ? this._parent.nextChild(
      this.index - 1,
      -1,
      0,
      4
      /* Side.DontCare */
    ) : null;
  }
  get tree() {
    return this._tree;
  }
  toTree() {
    return this._tree;
  }
  /**
  @internal
  */
  toString() {
    return this._tree.toString();
  }
}
function oe(l, e, t, r) {
  let i = l.cursor(), n = [];
  if (!i.firstChild())
    return n;
  if (t != null) {
    for (let s = !1; !s; )
      if (s = i.type.is(t), !i.nextSibling())
        return n;
  }
  for (; ; ) {
    if (r != null && i.type.is(r))
      return n;
    if (i.type.is(e) && n.push(i.node), !i.nextSibling())
      return r == null ? n : [];
  }
}
o(oe, "getChildren");
function te(l, e, t = e.length - 1) {
  for (let r = l; t >= 0; r = r.parent) {
    if (!r)
      return !1;
    if (!r.type.isAnonymous) {
      if (e[t] && e[t] != r.name)
        return !1;
      t--;
    }
  }
  return !0;
}
o(te, "matchNodeContext");
class me {
  static {
    o(this, "BufferContext");
  }
  constructor(e, t, r, i) {
    this.parent = e, this.buffer = t, this.index = r, this.start = i;
  }
}
class O extends ce {
  static {
    o(this, "BufferNode");
  }
  get name() {
    return this.type.name;
  }
  get from() {
    return this.context.start + this.context.buffer.buffer[this.index + 1];
  }
  get to() {
    return this.context.start + this.context.buffer.buffer[this.index + 2];
  }
  constructor(e, t, r) {
    super(), this.context = e, this._parent = t, this.index = r, this.type = e.buffer.set.types[e.buffer.buffer[r]];
  }
  child(e, t, r) {
    let { buffer: i } = this.context, n = i.findChild(this.index + 4, i.buffer[this.index + 3], e, t - this.context.start, r);
    return n < 0 ? null : new O(this.context, this, n);
  }
  get firstChild() {
    return this.child(
      1,
      0,
      4
      /* Side.DontCare */
    );
  }
  get lastChild() {
    return this.child(
      -1,
      0,
      4
      /* Side.DontCare */
    );
  }
  childAfter(e) {
    return this.child(
      1,
      e,
      2
      /* Side.After */
    );
  }
  childBefore(e) {
    return this.child(
      -1,
      e,
      -2
      /* Side.Before */
    );
  }
  enter(e, t, r = 0) {
    if (r & B.ExcludeBuffers)
      return null;
    let { buffer: i } = this.context, n = i.findChild(this.index + 4, i.buffer[this.index + 3], t > 0 ? 1 : -1, e - this.context.start, t);
    return n < 0 ? null : new O(this.context, this, n);
  }
  get parent() {
    return this._parent || this.context.parent.nextSignificantParent();
  }
  externalSibling(e) {
    return this._parent ? null : this.context.parent.nextChild(
      this.context.index + e,
      e,
      0,
      4
      /* Side.DontCare */
    );
  }
  get nextSibling() {
    let { buffer: e } = this.context, t = e.buffer[this.index + 3];
    return t < (this._parent ? e.buffer[this._parent.index + 3] : e.buffer.length) ? new O(this.context, this._parent, t) : this.externalSibling(1);
  }
  get prevSibling() {
    let { buffer: e } = this.context, t = this._parent ? this._parent.index + 4 : 0;
    return this.index == t ? this.externalSibling(-1) : new O(this.context, this._parent, e.findChild(
      t,
      this.index,
      -1,
      0,
      4
      /* Side.DontCare */
    ));
  }
  get tree() {
    return null;
  }
  toTree() {
    let e = [], t = [], { buffer: r } = this.context, i = this.index + 4, n = r.buffer[this.index + 3];
    if (n > i) {
      let s = r.buffer[this.index + 1];
      e.push(r.slice(i, n, s)), t.push(0);
    }
    return new j(this.type, e, t, this.to - this.from);
  }
  /**
  @internal
  */
  toString() {
    return this.context.buffer.childString(this.index);
  }
}
function ge(l) {
  if (!l.length)
    return null;
  let e = 0, t = l[0];
  for (let n = 1; n < l.length; n++) {
    let s = l[n];
    (s.from > t.from || s.to < t.to) && (t = s, e = n);
  }
  let r = t instanceof z && t.index < 0 ? null : t.parent, i = l.slice();
  return r ? i[e] = r : i.splice(e, 1), new we(i, t);
}
o(ge, "iterStack");
class we {
  static {
    o(this, "StackIterator");
  }
  constructor(e, t) {
    this.heads = e, this.node = t;
  }
  get next() {
    return ge(this.heads);
  }
}
function ke(l, e, t) {
  let r = l.resolveInner(e, t), i = null;
  for (let n = r instanceof z ? r : r.context.parent; n; n = n.parent)
    if (n.index < 0) {
      let s = n.parent;
      (i || (i = [r])).push(s.resolve(e, t)), n = s;
    } else {
      let s = Y.get(n.tree);
      if (s && s.overlay && s.overlay[0].from <= e && s.overlay[s.overlay.length - 1].to >= e) {
        let h = new z(s.tree, s.overlay[0].from + n.from, -1, n);
        (i || (i = [r])).push($(h, e, t, !1));
      }
    }
  return i ? ge(i) : r;
}
o(ke, "stackIterator");
class re {
  static {
    o(this, "TreeCursor");
  }
  /**
  Shorthand for `.type.name`.
  */
  get name() {
    return this.type.name;
  }
  /**
  @internal
  */
  constructor(e, t = 0) {
    if (this.mode = t, this.buffer = null, this.stack = [], this.index = 0, this.bufferNode = null, e instanceof z)
      this.yieldNode(e);
    else {
      this._tree = e.context.parent, this.buffer = e.context;
      for (let r = e._parent; r; r = r._parent)
        this.stack.unshift(r.index);
      this.bufferNode = e, this.yieldBuf(e.index);
    }
  }
  yieldNode(e) {
    return e ? (this._tree = e, this.type = e.type, this.from = e.from, this.to = e.to, !0) : !1;
  }
  yieldBuf(e, t) {
    this.index = e;
    let { start: r, buffer: i } = this.buffer;
    return this.type = t || i.set.types[i.buffer[e]], this.from = r + i.buffer[e + 1], this.to = r + i.buffer[e + 2], !0;
  }
  /**
  @internal
  */
  yield(e) {
    return e ? e instanceof z ? (this.buffer = null, this.yieldNode(e)) : (this.buffer = e.context, this.yieldBuf(e.index, e.type)) : !1;
  }
  /**
  @internal
  */
  toString() {
    return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
  }
  /**
  @internal
  */
  enterChild(e, t, r) {
    if (!this.buffer)
      return this.yield(this._tree.nextChild(e < 0 ? this._tree._tree.children.length - 1 : 0, e, t, r, this.mode));
    let { buffer: i } = this.buffer, n = i.findChild(this.index + 4, i.buffer[this.index + 3], e, t - this.buffer.start, r);
    return n < 0 ? !1 : (this.stack.push(this.index), this.yieldBuf(n));
  }
  /**
  Move the cursor to this node's first child. When this returns
  false, the node has no child, and the cursor has not been moved.
  */
  firstChild() {
    return this.enterChild(
      1,
      0,
      4
      /* Side.DontCare */
    );
  }
  /**
  Move the cursor to this node's last child.
  */
  lastChild() {
    return this.enterChild(
      -1,
      0,
      4
      /* Side.DontCare */
    );
  }
  /**
  Move the cursor to the first child that ends after `pos`.
  */
  childAfter(e) {
    return this.enterChild(
      1,
      e,
      2
      /* Side.After */
    );
  }
  /**
  Move to the last child that starts before `pos`.
  */
  childBefore(e) {
    return this.enterChild(
      -1,
      e,
      -2
      /* Side.Before */
    );
  }
  /**
  Move the cursor to the child around `pos`. If side is -1 the
  child may end at that position, when 1 it may start there. This
  will also enter [overlaid](#common.MountedTree.overlay)
  [mounted](#common.NodeProp^mounted) trees unless `overlays` is
  set to false.
  */
  enter(e, t, r = this.mode) {
    return this.buffer ? r & B.ExcludeBuffers ? !1 : this.enterChild(1, e, t) : this.yield(this._tree.enter(e, t, r));
  }
  /**
  Move to the node's parent node, if this isn't the top node.
  */
  parent() {
    if (!this.buffer)
      return this.yieldNode(this.mode & B.IncludeAnonymous ? this._tree._parent : this._tree.parent);
    if (this.stack.length)
      return this.yieldBuf(this.stack.pop());
    let e = this.mode & B.IncludeAnonymous ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
    return this.buffer = null, this.yieldNode(e);
  }
  /**
  @internal
  */
  sibling(e) {
    if (!this.buffer)
      return this._tree._parent ? this.yield(this._tree.index < 0 ? null : this._tree._parent.nextChild(this._tree.index + e, e, 0, 4, this.mode)) : !1;
    let { buffer: t } = this.buffer, r = this.stack.length - 1;
    if (e < 0) {
      let i = r < 0 ? 0 : this.stack[r] + 4;
      if (this.index != i)
        return this.yieldBuf(t.findChild(
          i,
          this.index,
          -1,
          0,
          4
          /* Side.DontCare */
        ));
    } else {
      let i = t.buffer[this.index + 3];
      if (i < (r < 0 ? t.buffer.length : t.buffer[this.stack[r] + 3]))
        return this.yieldBuf(i);
    }
    return r < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + e, e, 0, 4, this.mode)) : !1;
  }
  /**
  Move to this node's next sibling, if any.
  */
  nextSibling() {
    return this.sibling(1);
  }
  /**
  Move to this node's previous sibling, if any.
  */
  prevSibling() {
    return this.sibling(-1);
  }
  atLastNode(e) {
    let t, r, { buffer: i } = this;
    if (i) {
      if (e > 0) {
        if (this.index < i.buffer.buffer.length)
          return !1;
      } else
        for (let n = 0; n < this.index; n++)
          if (i.buffer.buffer[n + 3] < this.index)
            return !1;
      ({ index: t, parent: r } = i);
    } else
      ({ index: t, _parent: r } = this._tree);
    for (; r; { index: t, _parent: r } = r)
      if (t > -1)
        for (let n = t + e, s = e < 0 ? -1 : r._tree.children.length; n != s; n += e) {
          let h = r._tree.children[n];
          if (this.mode & B.IncludeAnonymous || h instanceof T || !h.type.isAnonymous || se(h))
            return !1;
        }
    return !0;
  }
  move(e, t) {
    if (t && this.enterChild(
      e,
      0,
      4
      /* Side.DontCare */
    ))
      return !0;
    for (; ; ) {
      if (this.sibling(e))
        return !0;
      if (this.atLastNode(e) || !this.parent())
        return !1;
    }
  }
  /**
  Move to the next node in a
  [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
  traversal, going from a node to its first child or, if the
  current node is empty or `enter` is false, its next sibling or
  the next sibling of the first parent node that has one.
  */
  next(e = !0) {
    return this.move(1, e);
  }
  /**
  Move to the next node in a last-to-first pre-order traversal. A
  node is followed by its last child or, if it has none, its
  previous sibling or the previous sibling of the first parent
  node that has one.
  */
  prev(e = !0) {
    return this.move(-1, e);
  }
  /**
  Move the cursor to the innermost node that covers `pos`. If
  `side` is -1, it will enter nodes that end at `pos`. If it is 1,
  it will enter nodes that start at `pos`.
  */
  moveTo(e, t = 0) {
    for (; (this.from == this.to || (t < 1 ? this.from >= e : this.from > e) || (t > -1 ? this.to <= e : this.to < e)) && this.parent(); )
      ;
    for (; this.enterChild(1, e, t); )
      ;
    return this;
  }
  /**
  Get a [syntax node](#common.SyntaxNode) at the cursor's current
  position.
  */
  get node() {
    if (!this.buffer)
      return this._tree;
    let e = this.bufferNode, t = null, r = 0;
    if (e && e.context == this.buffer)
      e: for (let i = this.index, n = this.stack.length; n >= 0; ) {
        for (let s = e; s; s = s._parent)
          if (s.index == i) {
            if (i == this.index)
              return s;
            t = s, r = n + 1;
            break e;
          }
        i = this.stack[--n];
      }
    for (let i = r; i < this.stack.length; i++)
      t = new O(this.buffer, t, this.stack[i]);
    return this.bufferNode = new O(this.buffer, t, this.index);
  }
  /**
  Get the [tree](#common.Tree) that represents the current node, if
  any. Will return null when the node is in a [tree
  buffer](#common.TreeBuffer).
  */
  get tree() {
    return this.buffer ? null : this._tree._tree;
  }
  /**
  Iterate over the current node and all its descendants, calling
  `enter` when entering a node and `leave`, if given, when leaving
  one. When `enter` returns `false`, any children of that node are
  skipped, and `leave` isn't called for it.
  */
  iterate(e, t) {
    for (let r = 0; ; ) {
      let i = !1;
      if (this.type.isAnonymous || e(this) !== !1) {
        if (this.firstChild()) {
          r++;
          continue;
        }
        this.type.isAnonymous || (i = !0);
      }
      for (; ; ) {
        if (i && t && t(this), i = this.type.isAnonymous, !r)
          return;
        if (this.nextSibling())
          break;
        this.parent(), r--, i = !0;
      }
    }
  }
  /**
  Test whether the current node matches a given context—a sequence
  of direct parent node names. Empty strings in the context array
  are treated as wildcards.
  */
  matchContext(e) {
    if (!this.buffer)
      return te(this.node.parent, e);
    let { buffer: t } = this.buffer, { types: r } = t.set;
    for (let i = e.length - 1, n = this.stack.length - 1; i >= 0; n--) {
      if (n < 0)
        return te(this._tree, e, i);
      let s = r[t.buffer[this.stack[n]]];
      if (!s.isAnonymous) {
        if (e[i] && e[i] != s.name)
          return !1;
        i--;
      }
    }
    return !0;
  }
}
function se(l) {
  return l.children.some((e) => e instanceof T || !e.type.isAnonymous || se(e));
}
o(se, "hasChild");
function Ce(l) {
  var e;
  let { buffer: t, nodeSet: r, maxBufferLength: i = 1024, reused: n = [], minRepeatType: s = r.types.length } = l, h = Array.isArray(t) ? new ne(t, t.length) : t, a = r.types, S = 0, c = 0;
  function A(g, y, f, m, d, w) {
    let { id: p, start: u, end: x, size: b } = h, _ = c, W = S;
    for (; b < 0; )
      if (h.next(), b == -1) {
        let M = n[p];
        f.push(M), m.push(u - g);
        return;
      } else if (b == -3) {
        S = p;
        return;
      } else if (b == -4) {
        c = p;
        return;
      } else
        throw new RangeError(`Unrecognized record size: ${b}`);
    let V = a[p], q, F, he = u - g;
    if (x - u <= i && (F = Z(h.pos - y, d))) {
      let M = new Uint16Array(F.size - F.skip), I = h.pos - F.size, E = M.length;
      for (; h.pos > I; )
        E = J(F.start, M, E);
      q = new T(M, x - F.start, r), he = F.start - g;
    } else {
      let M = h.pos - b;
      h.next();
      let I = [], E = [], H = p >= s ? p : -1, G = 0, K = x;
      for (; h.pos > M; )
        H >= 0 && h.id == H && h.size >= 0 ? (h.end <= K - i && (v(I, E, u, G, h.end, K, H, _, W), G = I.length, K = h.end), h.next()) : w > 2500 ? k(u, M, I, E) : A(u, M, I, E, H, w + 1);
      if (H >= 0 && G > 0 && G < I.length && v(I, E, u, G, u, K, H, _, W), I.reverse(), E.reverse(), H > -1 && G > 0) {
        let fe = P(V, W);
        q = le(V, I, E, 0, I.length, 0, x - u, fe, fe);
      } else
        q = L(V, I, E, x - u, _ - x, W);
    }
    f.push(q), m.push(he);
  }
  o(A, "takeNode");
  function k(g, y, f, m) {
    let d = [], w = 0, p = -1;
    for (; h.pos > y; ) {
      let { id: u, start: x, end: b, size: _ } = h;
      if (_ > 4)
        h.next();
      else {
        if (p > -1 && x < p)
          break;
        p < 0 && (p = b - i), d.push(u, x, b), w++, h.next();
      }
    }
    if (w) {
      let u = new Uint16Array(w * 4), x = d[d.length - 2];
      for (let b = d.length - 3, _ = 0; b >= 0; b -= 3)
        u[_++] = d[b], u[_++] = d[b + 1] - x, u[_++] = d[b + 2] - x, u[_++] = _;
      f.push(new T(u, d[2] - x, r)), m.push(x - g);
    }
  }
  o(k, "takeFlatNode");
  function P(g, y) {
    return (f, m, d) => {
      let w = 0, p = f.length - 1, u, x;
      if (p >= 0 && (u = f[p]) instanceof j) {
        if (!p && u.type == g && u.length == d)
          return u;
        (x = u.prop(C.lookAhead)) && (w = m[p] + u.length + x);
      }
      return L(g, f, m, d, w, y);
    };
  }
  o(P, "makeBalanced");
  function v(g, y, f, m, d, w, p, u, x) {
    let b = [], _ = [];
    for (; g.length > m; )
      b.push(g.pop()), _.push(y.pop() + f - d);
    g.push(L(r.types[p], b, _, w - d, u - w, x)), y.push(d - f);
  }
  o(v, "makeRepeatLeaf");
  function L(g, y, f, m, d, w, p) {
    if (w) {
      let u = [C.contextHash, w];
      p = p ? [u].concat(p) : [u];
    }
    if (d > 25) {
      let u = [C.lookAhead, d];
      p = p ? [u].concat(p) : [u];
    }
    return new j(g, y, f, m, p);
  }
  o(L, "makeTree");
  function Z(g, y) {
    let f = h.fork(), m = 0, d = 0, w = 0, p = f.end - i, u = { size: 0, start: 0, skip: 0 };
    e: for (let x = f.pos - g; f.pos > x; ) {
      let b = f.size;
      if (f.id == y && b >= 0) {
        u.size = m, u.start = d, u.skip = w, w += 4, m += 4, f.next();
        continue;
      }
      let _ = f.pos - b;
      if (b < 0 || _ < x || f.start < p)
        break;
      let W = f.id >= s ? 4 : 0, V = f.start;
      for (f.next(); f.pos > _; ) {
        if (f.size < 0)
          if (f.size == -3)
            W += 4;
          else
            break e;
        else f.id >= s && (W += 4);
        f.next();
      }
      d = V, m += b, w += W;
    }
    return (y < 0 || m == g) && (u.size = m, u.start = d, u.skip = w), u.size > 4 ? u : void 0;
  }
  o(Z, "findBufferSize");
  function J(g, y, f) {
    let { id: m, start: d, end: w, size: p } = h;
    if (h.next(), p >= 0 && m < s) {
      let u = f;
      if (p > 4) {
        let x = h.pos - (p - 4);
        for (; h.pos > x; )
          f = J(g, y, f);
      }
      y[--f] = u, y[--f] = w - g, y[--f] = d - g, y[--f] = m;
    } else p == -3 ? S = m : p == -4 && (c = m);
    return f;
  }
  o(J, "copyToBuffer");
  let U = [], N = [];
  for (; h.pos > 0; )
    A(l.start || 0, l.bufferStart || 0, U, N, -1, 0);
  let D = (e = l.length) !== null && e !== void 0 ? e : U.length ? N[0] + U[0].length : 0;
  return new j(a[l.topID], U.reverse(), N.reverse(), D);
}
o(Ce, "buildTree");
const ae = /* @__PURE__ */ new WeakMap();
function X(l, e) {
  if (!l.isAnonymous || e instanceof T || e.type != l)
    return 1;
  let t = ae.get(e);
  if (t == null) {
    t = 1;
    for (let r of e.children) {
      if (r.type != l || !(r instanceof j)) {
        t = 1;
        break;
      }
      t += X(l, r);
    }
    ae.set(e, t);
  }
  return t;
}
o(X, "nodeSize");
function le(l, e, t, r, i, n, s, h, a) {
  let S = 0;
  for (let v = r; v < i; v++)
    S += X(l, e[v]);
  let c = Math.ceil(
    S * 1.5 / 8
    /* Balance.BranchFactor */
  ), A = [], k = [];
  function P(v, L, Z, J, U) {
    for (let N = Z; N < J; ) {
      let D = N, g = L[N], y = X(l, v[N]);
      for (N++; N < J; N++) {
        let f = X(l, v[N]);
        if (y + f >= c)
          break;
        y += f;
      }
      if (N == D + 1) {
        if (y > c) {
          let f = v[D];
          P(f.children, f.positions, 0, f.children.length, L[D] + U);
          continue;
        }
        A.push(v[D]);
      } else {
        let f = L[N - 1] + v[N - 1].length - g;
        A.push(le(l, v, L, D, N, g, f, null, a));
      }
      k.push(g + U - n);
    }
  }
  return o(P, "divide"), P(e, t, r, i, 0), (h || a)(A, k, s);
}
o(le, "balanceRange");
class ve {
  static {
    o(this, "NodeWeakMap");
  }
  constructor() {
    this.map = /* @__PURE__ */ new WeakMap();
  }
  setBuffer(e, t, r) {
    let i = this.map.get(e);
    i || this.map.set(e, i = /* @__PURE__ */ new Map()), i.set(t, r);
  }
  getBuffer(e, t) {
    let r = this.map.get(e);
    return r && r.get(t);
  }
  /**
  Set the value for this syntax node.
  */
  set(e, t) {
    e instanceof O ? this.setBuffer(e.context.buffer, e.index, t) : e instanceof z && this.map.set(e.tree, t);
  }
  /**
  Retrieve value for this syntax node, if it exists in the map.
  */
  get(e) {
    return e instanceof O ? this.getBuffer(e.context.buffer, e.index) : e instanceof z ? this.map.get(e.tree) : void 0;
  }
  /**
  Set the value for the node that a cursor currently points to.
  */
  cursorSet(e, t) {
    e.buffer ? this.setBuffer(e.buffer.buffer, e.index, t) : this.map.set(e.tree, t);
  }
  /**
  Retrieve the value for the node that a cursor currently points
  to.
  */
  cursorGet(e) {
    return e.buffer ? this.getBuffer(e.buffer.buffer, e.index) : this.map.get(e.tree);
  }
}
class ie {
  static {
    o(this, "TreeFragment");
  }
  /**
  Construct a tree fragment. You'll usually want to use
  [`addTree`](#common.TreeFragment^addTree) and
  [`applyChanges`](#common.TreeFragment^applyChanges) instead of
  calling this directly.
  */
  constructor(e, t, r, i, n = !1, s = !1) {
    this.from = e, this.to = t, this.tree = r, this.offset = i, this.open = (n ? 1 : 0) | (s ? 2 : 0);
  }
  /**
  Whether the start of the fragment represents the start of a
  parse, or the end of a change. (In the second case, it may not
  be safe to reuse some nodes at the start, depending on the
  parsing algorithm.)
  */
  get openStart() {
    return (this.open & 1) > 0;
  }
  /**
  Whether the end of the fragment represents the end of a
  full-document parse, or the start of a change.
  */
  get openEnd() {
    return (this.open & 2) > 0;
  }
  /**
  Create a set of fragments from a freshly parsed tree, or update
  an existing set of fragments by replacing the ones that overlap
  with a tree with content from the new tree. When `partial` is
  true, the parse is treated as incomplete, and the resulting
  fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
  true.
  */
  static addTree(e, t = [], r = !1) {
    let i = [new ie(0, e.length, e, 0, !1, r)];
    for (let n of t)
      n.to > e.length && i.push(n);
    return i;
  }
  /**
  Apply a set of edits to an array of fragments, removing or
  splitting fragments as necessary to remove edited ranges, and
  adjusting offsets for fragments that moved.
  */
  static applyChanges(e, t, r = 128) {
    if (!t.length)
      return e;
    let i = [], n = 1, s = e.length ? e[0] : null;
    for (let h = 0, a = 0, S = 0; ; h++) {
      let c = h < t.length ? t[h] : null, A = c ? c.fromA : 1e9;
      if (A - a >= r)
        for (; s && s.from < A; ) {
          let k = s;
          if (a >= k.from || A <= k.to || S) {
            let P = Math.max(k.from, a) - S, v = Math.min(k.to, A) - S;
            k = P >= v ? null : new ie(P, v, k.tree, k.offset + S, h > 0, !!c);
          }
          if (k && i.push(k), s.to > A)
            break;
          s = n < e.length ? e[n++] : null;
        }
      if (!c)
        break;
      a = c.toA, S = c.toA - c.toB;
    }
    return i;
  }
}
class Ne {
  static {
    o(this, "Parser");
  }
  /**
  Start a parse, returning a [partial parse](#common.PartialParse)
  object. [`fragments`](#common.TreeFragment) can be passed in to
  make the parse incremental.
  
  By default, the entire input is parsed. You can pass `ranges`,
  which should be a sorted array of non-empty, non-overlapping
  ranges, to parse only those ranges. The tree returned in that
  case will start at `ranges[0].from`.
  */
  startParse(e, t, r) {
    return typeof e == "string" && (e = new Se(e)), r = r ? r.length ? r.map((i) => new ee(i.from, i.to)) : [new ee(0, 0)] : [new ee(0, e.length)], this.createParse(e, t || [], r);
  }
  /**
  Run a full parse, returning the resulting tree.
  */
  parse(e, t, r) {
    let i = this.startParse(e, t, r);
    for (; ; ) {
      let n = i.advance();
      if (n)
        return n;
    }
  }
}
class Se {
  static {
    o(this, "StringInput");
  }
  constructor(e) {
    this.string = e;
  }
  get length() {
    return this.string.length;
  }
  chunk(e) {
    return this.string.slice(e);
  }
  get lineChunks() {
    return !1;
  }
  read(e, t) {
    return this.string.slice(e, t);
  }
}
new C({ perNode: !0 });
export {
  _e as DefaultBufferLength,
  B as IterMode,
  Y as MountedTree,
  C as NodeProp,
  pe as NodeSet,
  R as NodeType,
  ve as NodeWeakMap,
  Ne as Parser,
  j as Tree,
  T as TreeBuffer,
  re as TreeCursor,
  ie as TreeFragment
};
