UNPKG

prong-editor

Version:

Prong (PRojectional jsON Gui) is an editor framework for creating bespoke in-browser editors for JSON-based domain-specific languages (such as [Vega](https://vega.github.io/vega/), [Vega-Lite](https://vega.github.io/vega-lite/), [Tracery](https://tracery.

1,502 lines 1.16 MB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import React, { useState, useRef, useEffect, createElement, Component } from "react"; import * as ReactDOM from "react-dom"; import ReactDOM__default from "react-dom"; const DefaultBufferLength = 1024; let nextPropID = 0; class Range$1 { constructor(from, to) { this.from = from; this.to = to; } } class NodeProp { constructor(config2 = {}) { this.id = nextPropID++; this.perNode = !!config2.perNode; this.deserialize = config2.deserialize || (() => { throw new Error("This node type doesn't define a deserialize function"); }); } add(match) { if (this.perNode) throw new RangeError("Can't add per-node props to node types"); if (typeof match != "function") match = NodeType.match(match); return (type) => { let result = match(type); return result === void 0 ? null : [this, result]; }; } } NodeProp.closedBy = new NodeProp({ deserialize: (str) => str.split(" ") }); NodeProp.openedBy = new NodeProp({ deserialize: (str) => str.split(" ") }); NodeProp.group = new NodeProp({ deserialize: (str) => str.split(" ") }); NodeProp.contextHash = new NodeProp({ perNode: true }); NodeProp.lookAhead = new NodeProp({ perNode: true }); NodeProp.mounted = new NodeProp({ perNode: true }); const noProps = /* @__PURE__ */ Object.create(null); class NodeType { constructor(name2, props, id, flags = 0) { this.name = name2; this.props = props; this.id = id; this.flags = flags; } static define(spec) { let props = spec.props && spec.props.length ? /* @__PURE__ */ Object.create(null) : noProps; let flags = (spec.top ? 1 : 0) | (spec.skipped ? 2 : 0) | (spec.error ? 4 : 0) | (spec.name == null ? 8 : 0); let type = new NodeType(spec.name || "", props, spec.id, flags); if (spec.props) for (let src of spec.props) { if (!Array.isArray(src)) src = src(type); if (src) { if (src[0].perNode) throw new RangeError("Can't store a per-node prop on a node type"); props[src[0].id] = src[1]; } } return type; } prop(prop) { return this.props[prop.id]; } get isTop() { return (this.flags & 1) > 0; } get isSkipped() { return (this.flags & 2) > 0; } get isError() { return (this.flags & 4) > 0; } get isAnonymous() { return (this.flags & 8) > 0; } is(name2) { if (typeof name2 == "string") { if (this.name == name2) return true; let group = this.prop(NodeProp.group); return group ? group.indexOf(name2) > -1 : false; } return this.id == name2; } static match(map) { let direct = /* @__PURE__ */ Object.create(null); for (let prop in map) for (let name2 of prop.split(" ")) direct[name2] = map[prop]; return (node2) => { for (let groups = node2.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) { let found = direct[i < 0 ? node2.name : groups[i]]; if (found) return found; } }; } } NodeType.none = new NodeType("", /* @__PURE__ */ Object.create(null), 0, 8); class NodeSet { constructor(types2) { this.types = types2; for (let i = 0; i < types2.length; i++) if (types2[i].id != i) throw new RangeError("Node type ids should correspond to array positions when creating a node set"); } extend(...props) { let newTypes = []; for (let type of this.types) { let newProps = null; for (let source of props) { let add2 = source(type); if (add2) { if (!newProps) newProps = Object.assign({}, type.props); newProps[add2[0].id] = add2[1]; } } newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type); } return new NodeSet(newTypes); } } const CachedNode = /* @__PURE__ */ new WeakMap(), CachedInnerNode = /* @__PURE__ */ new WeakMap(); var IterMode; (function(IterMode2) { IterMode2[IterMode2["ExcludeBuffers"] = 1] = "ExcludeBuffers"; IterMode2[IterMode2["IncludeAnonymous"] = 2] = "IncludeAnonymous"; IterMode2[IterMode2["IgnoreMounts"] = 4] = "IgnoreMounts"; IterMode2[IterMode2["IgnoreOverlays"] = 8] = "IgnoreOverlays"; })(IterMode || (IterMode = {})); class Tree { constructor(type, children, positions, length, props) { this.type = type; this.children = children; this.positions = positions; this.length = length; this.props = null; if (props && props.length) { this.props = /* @__PURE__ */ Object.create(null); for (let [prop, value] of props) this.props[typeof prop == "number" ? prop : prop.id] = value; } } toString() { let mounted = this.prop(NodeProp.mounted); if (mounted && !mounted.overlay) return mounted.tree.toString(); let children = ""; for (let ch of this.children) { let str = ch.toString(); if (str) { if (children) children += ","; children += str; } } return !this.type.name ? children : (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) + (children.length ? "(" + children + ")" : ""); } cursor(mode = 0) { return new TreeCursor(this.topNode, mode); } cursorAt(pos, side = 0, mode = 0) { let scope = CachedNode.get(this) || this.topNode; let cursor = new TreeCursor(scope); cursor.moveTo(pos, side); CachedNode.set(this, cursor._tree); return cursor; } get topNode() { return new TreeNode(this, 0, 0, null); } resolve(pos, side = 0) { let node2 = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false); CachedNode.set(this, node2); return node2; } resolveInner(pos, side = 0) { let node2 = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true); CachedInnerNode.set(this, node2); return node2; } iterate(spec) { let { enter, leave, from = 0, to = this.length } = spec; let mode = spec.mode || 0, anon = (mode & IterMode.IncludeAnonymous) > 0; for (let c2 = this.cursor(mode | IterMode.IncludeAnonymous); ; ) { let entered = false; if (c2.from <= to && c2.to >= from && (!anon && c2.type.isAnonymous || enter(c2) !== false)) { if (c2.firstChild()) continue; entered = true; } for (; ; ) { if (entered && leave && (anon || !c2.type.isAnonymous)) leave(c2); if (c2.nextSibling()) break; if (!c2.parent()) return; entered = true; } } } prop(prop) { return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : void 0; } get propValues() { let result = []; if (this.props) for (let id in this.props) result.push([+id, this.props[id]]); return result; } balance(config2 = {}) { return this.children.length <= 8 ? this : balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config2.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length))); } static build(data) { return buildTree(data); } } Tree.empty = new Tree(NodeType.none, [], [], 0); class FlatBufferCursor { constructor(buffer2, index2) { this.buffer = buffer2; this.index = index2; } 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 FlatBufferCursor(this.buffer, this.index); } } class TreeBuffer { constructor(buffer2, length, set) { this.buffer = buffer2; this.length = length; this.set = set; } get type() { return NodeType.none; } toString() { let result = []; for (let index2 = 0; index2 < this.buffer.length; ) { result.push(this.childString(index2)); index2 = this.buffer[index2 + 3]; } return result.join(","); } childString(index2) { let id = this.buffer[index2], endIndex = this.buffer[index2 + 3]; let type = this.set.types[id], result = type.name; if (/\W/.test(result) && !type.isError) result = JSON.stringify(result); index2 += 4; if (endIndex == index2) return result; let children = []; while (index2 < endIndex) { children.push(this.childString(index2)); index2 = this.buffer[index2 + 3]; } return result + "(" + children.join(",") + ")"; } findChild(startIndex, endIndex, dir, pos, side) { let { buffer: buffer2 } = this, pick = -1; for (let i = startIndex; i != endIndex; i = buffer2[i + 3]) { if (checkSide(side, pos, buffer2[i + 1], buffer2[i + 2])) { pick = i; if (dir > 0) break; } } return pick; } slice(startI, endI, from) { let b2 = this.buffer; let copy = new Uint16Array(endI - startI), len = 0; for (let i = startI, j = 0; i < endI; ) { copy[j++] = b2[i++]; copy[j++] = b2[i++] - from; let to = copy[j++] = b2[i++] - from; copy[j++] = b2[i++] - startI; len = Math.max(len, to); } return new TreeBuffer(copy, len, this.set); } } function checkSide(side, pos, from, to) { switch (side) { case -2: return from < pos; case -1: return to >= pos && from < pos; case 0: return from < pos && to > pos; case 1: return from <= pos && to > pos; case 2: return to > pos; case 4: return true; } } function enterUnfinishedNodesBefore(node2, pos) { let scan = node2.childBefore(pos); while (scan) { let last = scan.lastChild; if (!last || last.to != scan.to) break; if (last.type.isError && last.from == last.to) { node2 = scan; scan = last.prevSibling; } else { scan = last; } } return node2; } function resolveNode(node2, pos, side, overlays) { var _a2; while (node2.from == node2.to || (side < 1 ? node2.from >= pos : node2.from > pos) || (side > -1 ? node2.to <= pos : node2.to < pos)) { let parent = !overlays && node2 instanceof TreeNode && node2.index < 0 ? null : node2.parent; if (!parent) return node2; node2 = parent; } let mode = overlays ? 0 : IterMode.IgnoreOverlays; if (overlays) for (let scan = node2, parent = scan.parent; parent; scan = parent, parent = scan.parent) { if (scan instanceof TreeNode && scan.index < 0 && ((_a2 = parent.enter(pos, side, mode)) === null || _a2 === void 0 ? void 0 : _a2.from) != scan.from) node2 = parent; } for (; ; ) { let inner = node2.enter(pos, side, mode); if (!inner) return node2; node2 = inner; } } class TreeNode { constructor(_tree, from, index2, _parent) { this._tree = _tree; this.from = from; this.index = index2; this._parent = _parent; } get type() { return this._tree.type; } get name() { return this._tree.type.name; } get to() { return this.from + this._tree.length; } nextChild(i, dir, pos, side, mode = 0) { for (let parent = this; ; ) { for (let { children, positions } = parent._tree, e2 = dir > 0 ? children.length : -1; i != e2; i += dir) { let next = children[i], start = positions[i] + parent.from; if (!checkSide(side, pos, start, start + next.length)) continue; if (next instanceof TreeBuffer) { if (mode & IterMode.ExcludeBuffers) continue; let index2 = next.findChild(0, next.buffer.length, dir, pos - start, side); if (index2 > -1) return new BufferNode(new BufferContext(parent, next, i, start), null, index2); } else if (mode & IterMode.IncludeAnonymous || (!next.type.isAnonymous || hasChild(next))) { let mounted; if (!(mode & IterMode.IgnoreMounts) && next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay) return new TreeNode(mounted.tree, start, i, parent); let inner = new TreeNode(next, start, i, parent); return mode & IterMode.IncludeAnonymous || !inner.type.isAnonymous ? inner : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side); } } if (mode & IterMode.IncludeAnonymous || !parent.type.isAnonymous) return null; if (parent.index >= 0) i = parent.index + dir; else i = dir < 0 ? -1 : parent._parent._tree.children.length; parent = parent._parent; if (!parent) return null; } } get firstChild() { return this.nextChild(0, 1, 0, 4); } get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4); } childAfter(pos) { return this.nextChild(0, 1, pos, 2); } childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2); } enter(pos, side, mode = 0) { let mounted; if (!(mode & IterMode.IgnoreOverlays) && (mounted = this._tree.prop(NodeProp.mounted)) && mounted.overlay) { let rPos = pos - this.from; for (let { from, to } of mounted.overlay) { if ((side > 0 ? from <= rPos : from < rPos) && (side < 0 ? to >= rPos : to > rPos)) return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this); } } return this.nextChild(0, 1, pos, side, mode); } nextSignificantParent() { let val = this; while (val.type.isAnonymous && val._parent) val = val._parent; return val; } 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) : null; } get prevSibling() { return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4) : null; } cursor(mode = 0) { return new TreeCursor(this, mode); } get tree() { return this._tree; } toTree() { return this._tree; } resolve(pos, side = 0) { return resolveNode(this, pos, side, false); } resolveInner(pos, side = 0) { return resolveNode(this, pos, side, true); } enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); } getChild(type, before = null, after = null) { let r2 = getChildren(this, type, before, after); return r2.length ? r2[0] : null; } getChildren(type, before = null, after = null) { return getChildren(this, type, before, after); } toString() { return this._tree.toString(); } get node() { return this; } matchContext(context) { return matchNodeContext(this, context); } } function getChildren(node2, type, before, after) { let cur2 = node2.cursor(), result = []; if (!cur2.firstChild()) return result; if (before != null) { while (!cur2.type.is(before)) if (!cur2.nextSibling()) return result; } for (; ; ) { if (after != null && cur2.type.is(after)) return result; if (cur2.type.is(type)) result.push(cur2.node); if (!cur2.nextSibling()) return after == null ? result : []; } } function matchNodeContext(node2, context, i = context.length - 1) { for (let p2 = node2.parent; i >= 0; p2 = p2.parent) { if (!p2) return false; if (!p2.type.isAnonymous) { if (context[i] && context[i] != p2.name) return false; i--; } } return true; } class BufferContext { constructor(parent, buffer2, index2, start) { this.parent = parent; this.buffer = buffer2; this.index = index2; this.start = start; } } class 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(context, _parent, index2) { this.context = context; this._parent = _parent; this.index = index2; this.type = context.buffer.set.types[context.buffer.buffer[index2]]; } child(dir, pos, side) { let { buffer: buffer2 } = this.context; let index2 = buffer2.findChild(this.index + 4, buffer2.buffer[this.index + 3], dir, pos - this.context.start, side); return index2 < 0 ? null : new BufferNode(this.context, this, index2); } get firstChild() { return this.child(1, 0, 4); } get lastChild() { return this.child(-1, 0, 4); } childAfter(pos) { return this.child(1, pos, 2); } childBefore(pos) { return this.child(-1, pos, -2); } enter(pos, side, mode = 0) { if (mode & IterMode.ExcludeBuffers) return null; let { buffer: buffer2 } = this.context; let index2 = buffer2.findChild(this.index + 4, buffer2.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side); return index2 < 0 ? null : new BufferNode(this.context, this, index2); } get parent() { return this._parent || this.context.parent.nextSignificantParent(); } externalSibling(dir) { return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4); } get nextSibling() { let { buffer: buffer2 } = this.context; let after = buffer2.buffer[this.index + 3]; if (after < (this._parent ? buffer2.buffer[this._parent.index + 3] : buffer2.buffer.length)) return new BufferNode(this.context, this._parent, after); return this.externalSibling(1); } get prevSibling() { let { buffer: buffer2 } = this.context; let parentStart = this._parent ? this._parent.index + 4 : 0; if (this.index == parentStart) return this.externalSibling(-1); return new BufferNode(this.context, this._parent, buffer2.findChild(parentStart, this.index, -1, 0, 4)); } cursor(mode = 0) { return new TreeCursor(this, mode); } get tree() { return null; } toTree() { let children = [], positions = []; let { buffer: buffer2 } = this.context; let startI = this.index + 4, endI = buffer2.buffer[this.index + 3]; if (endI > startI) { let from = buffer2.buffer[this.index + 1]; children.push(buffer2.slice(startI, endI, from)); positions.push(0); } return new Tree(this.type, children, positions, this.to - this.from); } resolve(pos, side = 0) { return resolveNode(this, pos, side, false); } resolveInner(pos, side = 0) { return resolveNode(this, pos, side, true); } enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); } toString() { return this.context.buffer.childString(this.index); } getChild(type, before = null, after = null) { let r2 = getChildren(this, type, before, after); return r2.length ? r2[0] : null; } getChildren(type, before = null, after = null) { return getChildren(this, type, before, after); } get node() { return this; } matchContext(context) { return matchNodeContext(this, context); } } class TreeCursor { get name() { return this.type.name; } constructor(node2, mode = 0) { this.mode = mode; this.buffer = null; this.stack = []; this.index = 0; this.bufferNode = null; if (node2 instanceof TreeNode) { this.yieldNode(node2); } else { this._tree = node2.context.parent; this.buffer = node2.context; for (let n2 = node2._parent; n2; n2 = n2._parent) this.stack.unshift(n2.index); this.bufferNode = node2; this.yieldBuf(node2.index); } } yieldNode(node2) { if (!node2) return false; this._tree = node2; this.type = node2.type; this.from = node2.from; this.to = node2.to; return true; } yieldBuf(index2, type) { this.index = index2; let { start, buffer: buffer2 } = this.buffer; this.type = type || buffer2.set.types[buffer2.buffer[index2]]; this.from = start + buffer2.buffer[index2 + 1]; this.to = start + buffer2.buffer[index2 + 2]; return true; } yield(node2) { if (!node2) return false; if (node2 instanceof TreeNode) { this.buffer = null; return this.yieldNode(node2); } this.buffer = node2.context; return this.yieldBuf(node2.index, node2.type); } toString() { return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString(); } enterChild(dir, pos, side) { if (!this.buffer) return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode)); let { buffer: buffer2 } = this.buffer; let index2 = buffer2.findChild(this.index + 4, buffer2.buffer[this.index + 3], dir, pos - this.buffer.start, side); if (index2 < 0) return false; this.stack.push(this.index); return this.yieldBuf(index2); } firstChild() { return this.enterChild(1, 0, 4); } lastChild() { return this.enterChild(-1, 0, 4); } childAfter(pos) { return this.enterChild(1, pos, 2); } childBefore(pos) { return this.enterChild(-1, pos, -2); } enter(pos, side, mode = this.mode) { if (!this.buffer) return this.yield(this._tree.enter(pos, side, mode)); return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side); } parent() { if (!this.buffer) return this.yieldNode(this.mode & IterMode.IncludeAnonymous ? this._tree._parent : this._tree.parent); if (this.stack.length) return this.yieldBuf(this.stack.pop()); let parent = this.mode & IterMode.IncludeAnonymous ? this.buffer.parent : this.buffer.parent.nextSignificantParent(); this.buffer = null; return this.yieldNode(parent); } sibling(dir) { if (!this.buffer) return !this._tree._parent ? false : this.yield(this._tree.index < 0 ? null : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4, this.mode)); let { buffer: buffer2 } = this.buffer, d2 = this.stack.length - 1; if (dir < 0) { let parentStart = d2 < 0 ? 0 : this.stack[d2] + 4; if (this.index != parentStart) return this.yieldBuf(buffer2.findChild(parentStart, this.index, -1, 0, 4)); } else { let after = buffer2.buffer[this.index + 3]; if (after < (d2 < 0 ? buffer2.buffer.length : buffer2.buffer[this.stack[d2] + 3])) return this.yieldBuf(after); } return d2 < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4, this.mode)) : false; } nextSibling() { return this.sibling(1); } prevSibling() { return this.sibling(-1); } atLastNode(dir) { let index2, parent, { buffer: buffer2 } = this; if (buffer2) { if (dir > 0) { if (this.index < buffer2.buffer.buffer.length) return false; } else { for (let i = 0; i < this.index; i++) if (buffer2.buffer.buffer[i + 3] < this.index) return false; } ({ index: index2, parent } = buffer2); } else { ({ index: index2, _parent: parent } = this._tree); } for (; parent; { index: index2, _parent: parent } = parent) { if (index2 > -1) for (let i = index2 + dir, e2 = dir < 0 ? -1 : parent._tree.children.length; i != e2; i += dir) { let child = parent._tree.children[i]; if (this.mode & IterMode.IncludeAnonymous || child instanceof TreeBuffer || !child.type.isAnonymous || hasChild(child)) return false; } } return true; } move(dir, enter) { if (enter && this.enterChild(dir, 0, 4)) return true; for (; ; ) { if (this.sibling(dir)) return true; if (this.atLastNode(dir) || !this.parent()) return false; } } next(enter = true) { return this.move(1, enter); } prev(enter = true) { return this.move(-1, enter); } moveTo(pos, side = 0) { while (this.from == this.to || (side < 1 ? this.from >= pos : this.from > pos) || (side > -1 ? this.to <= pos : this.to < pos)) if (!this.parent()) break; while (this.enterChild(1, pos, side)) { } return this; } get node() { if (!this.buffer) return this._tree; let cache2 = this.bufferNode, result = null, depth = 0; if (cache2 && cache2.context == this.buffer) { scan: for (let index2 = this.index, d2 = this.stack.length; d2 >= 0; ) { for (let c2 = cache2; c2; c2 = c2._parent) if (c2.index == index2) { if (index2 == this.index) return c2; result = c2; depth = d2 + 1; break scan; } index2 = this.stack[--d2]; } } for (let i = depth; i < this.stack.length; i++) result = new BufferNode(this.buffer, result, this.stack[i]); return this.bufferNode = new BufferNode(this.buffer, result, this.index); } get tree() { return this.buffer ? null : this._tree._tree; } iterate(enter, leave) { for (let depth = 0; ; ) { let mustLeave = false; if (this.type.isAnonymous || enter(this) !== false) { if (this.firstChild()) { depth++; continue; } if (!this.type.isAnonymous) mustLeave = true; } for (; ; ) { if (mustLeave && leave) leave(this); mustLeave = this.type.isAnonymous; if (this.nextSibling()) break; if (!depth) return; this.parent(); depth--; mustLeave = true; } } } matchContext(context) { if (!this.buffer) return matchNodeContext(this.node, context); let { buffer: buffer2 } = this.buffer, { types: types2 } = buffer2.set; for (let i = context.length - 1, d2 = this.stack.length - 1; i >= 0; d2--) { if (d2 < 0) return matchNodeContext(this.node, context, i); let type = types2[buffer2.buffer[this.stack[d2]]]; if (!type.isAnonymous) { if (context[i] && context[i] != type.name) return false; i--; } } return true; } } function hasChild(tree) { return tree.children.some((ch) => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch)); } function buildTree(data) { var _a2; let { buffer: buffer2, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data; let cursor = Array.isArray(buffer2) ? new FlatBufferCursor(buffer2, buffer2.length) : buffer2; let types2 = nodeSet.types; let contextHash = 0, lookAhead = 0; function takeNode(parentStart, minPos, children2, positions2, inRepeat) { let { id, start, end, size } = cursor; let lookAheadAtStart = lookAhead; while (size < 0) { cursor.next(); if (size == -1) { let node3 = reused[id]; children2.push(node3); positions2.push(start - parentStart); return; } else if (size == -3) { contextHash = id; return; } else if (size == -4) { lookAhead = id; return; } else { throw new RangeError(`Unrecognized record size: ${size}`); } } let type = types2[id], node2, buffer3; let startPos = start - parentStart; if (end - start <= maxBufferLength && (buffer3 = findBufferSize(cursor.pos - minPos, inRepeat))) { let data2 = new Uint16Array(buffer3.size - buffer3.skip); let endPos = cursor.pos - buffer3.size, index2 = data2.length; while (cursor.pos > endPos) index2 = copyToBuffer(buffer3.start, data2, index2); node2 = new TreeBuffer(data2, end - buffer3.start, nodeSet); startPos = buffer3.start - parentStart; } else { let endPos = cursor.pos - size; cursor.next(); let localChildren = [], localPositions = []; let localInRepeat = id >= minRepeatType ? id : -1; let lastGroup = 0, lastEnd = end; while (cursor.pos > endPos) { if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) { if (cursor.end <= lastEnd - maxBufferLength) { makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart); lastGroup = localChildren.length; lastEnd = cursor.end; } cursor.next(); } else { takeNode(start, endPos, localChildren, localPositions, localInRepeat); } } if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length) makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart); localChildren.reverse(); localPositions.reverse(); if (localInRepeat > -1 && lastGroup > 0) { let make = makeBalanced(type); node2 = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make); } else { node2 = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end); } } children2.push(node2); positions2.push(startPos); } function makeBalanced(type) { return (children2, positions2, length2) => { let lookAhead2 = 0, lastI = children2.length - 1, last, lookAheadProp; if (lastI >= 0 && (last = children2[lastI]) instanceof Tree) { if (!lastI && last.type == type && last.length == length2) return last; if (lookAheadProp = last.prop(NodeProp.lookAhead)) lookAhead2 = positions2[lastI] + last.length + lookAheadProp; } return makeTree(type, children2, positions2, length2, lookAhead2); }; } function makeRepeatLeaf(children2, positions2, base2, i, from, to, type, lookAhead2) { let localChildren = [], localPositions = []; while (children2.length > i) { localChildren.push(children2.pop()); localPositions.push(positions2.pop() + base2 - from); } children2.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead2 - to)); positions2.push(from - base2); } function makeTree(type, children2, positions2, length2, lookAhead2 = 0, props) { if (contextHash) { let pair2 = [NodeProp.contextHash, contextHash]; props = props ? [pair2].concat(props) : [pair2]; } if (lookAhead2 > 25) { let pair2 = [NodeProp.lookAhead, lookAhead2]; props = props ? [pair2].concat(props) : [pair2]; } return new Tree(type, children2, positions2, length2, props); } function findBufferSize(maxSize, inRepeat) { let fork = cursor.fork(); let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength; let result = { size: 0, start: 0, skip: 0 }; scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos; ) { let nodeSize2 = fork.size; if (fork.id == inRepeat && nodeSize2 >= 0) { result.size = size; result.start = start; result.skip = skip; skip += 4; size += 4; fork.next(); continue; } let startPos = fork.pos - nodeSize2; if (nodeSize2 < 0 || startPos < minPos || fork.start < minStart) break; let localSkipped = fork.id >= minRepeatType ? 4 : 0; let nodeStart2 = fork.start; fork.next(); while (fork.pos > startPos) { if (fork.size < 0) { if (fork.size == -3) localSkipped += 4; else break scan; } else if (fork.id >= minRepeatType) { localSkipped += 4; } fork.next(); } start = nodeStart2; size += nodeSize2; skip += localSkipped; } if (inRepeat < 0 || size == maxSize) { result.size = size; result.start = start; result.skip = skip; } return result.size > 4 ? result : void 0; } function copyToBuffer(bufferStart, buffer3, index2) { let { id, start, end, size } = cursor; cursor.next(); if (size >= 0 && id < minRepeatType) { let startIndex = index2; if (size > 4) { let endPos = cursor.pos - (size - 4); while (cursor.pos > endPos) index2 = copyToBuffer(bufferStart, buffer3, index2); } buffer3[--index2] = startIndex; buffer3[--index2] = end - bufferStart; buffer3[--index2] = start - bufferStart; buffer3[--index2] = id; } else if (size == -3) { contextHash = id; } else if (size == -4) { lookAhead = id; } return index2; } let children = [], positions = []; while (cursor.pos > 0) takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1); let length = (_a2 = data.length) !== null && _a2 !== void 0 ? _a2 : children.length ? positions[0] + children[0].length : 0; return new Tree(types2[data.topID], children.reverse(), positions.reverse(), length); } const nodeSizeCache = /* @__PURE__ */ new WeakMap(); function nodeSize(balanceType, node2) { if (!balanceType.isAnonymous || node2 instanceof TreeBuffer || node2.type != balanceType) return 1; let size = nodeSizeCache.get(node2); if (size == null) { size = 1; for (let child of node2.children) { if (child.type != balanceType || !(child instanceof Tree)) { size = 1; break; } size += nodeSize(balanceType, child); } nodeSizeCache.set(node2, size); } return size; } function balanceRange(balanceType, children, positions, from, to, start, length, mkTop, mkTree) { let total = 0; for (let i = from; i < to; i++) total += nodeSize(balanceType, children[i]); let maxChild = Math.ceil(total * 1.5 / 8); let localChildren = [], localPositions = []; function divide(children2, positions2, from2, to2, offset) { for (let i = from2; i < to2; ) { let groupFrom = i, groupStart = positions2[i], groupSize = nodeSize(balanceType, children2[i]); i++; for (; i < to2; i++) { let nextSize = nodeSize(balanceType, children2[i]); if (groupSize + nextSize >= maxChild) break; groupSize += nextSize; } if (i == groupFrom + 1) { if (groupSize > maxChild) { let only = children2[groupFrom]; divide(only.children, only.positions, 0, only.children.length, positions2[groupFrom] + offset); continue; } localChildren.push(children2[groupFrom]); } else { let length2 = positions2[i - 1] + children2[i - 1].length - groupStart; localChildren.push(balanceRange(balanceType, children2, positions2, groupFrom, i, groupStart, length2, null, mkTree)); } localPositions.push(groupStart + offset - start); } } divide(children, positions, from, to, 0); return (mkTop || mkTree)(localChildren, localPositions, length); } class TreeFragment { constructor(from, to, tree, offset, openStart = false, openEnd = false) { this.from = from; this.to = to; this.tree = tree; this.offset = offset; this.open = (openStart ? 1 : 0) | (openEnd ? 2 : 0); } get openStart() { return (this.open & 1) > 0; } get openEnd() { return (this.open & 2) > 0; } static addTree(tree, fragments = [], partial = false) { let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)]; for (let f2 of fragments) if (f2.to > tree.length) result.push(f2); return result; } static applyChanges(fragments, changes, minGap = 128) { if (!changes.length) return fragments; let result = []; let fI = 1, nextF = fragments.length ? fragments[0] : null; for (let cI = 0, pos = 0, off = 0; ; cI++) { let nextC = cI < changes.length ? changes[cI] : null; let nextPos = nextC ? nextC.fromA : 1e9; if (nextPos - pos >= minGap) while (nextF && nextF.from < nextPos) { let cut = nextF; if (pos >= cut.from || nextPos <= cut.to || off) { let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off; cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC); } if (cut) result.push(cut); if (nextF.to > nextPos) break; nextF = fI < fragments.length ? fragments[fI++] : null; } if (!nextC) break; pos = nextC.toA; off = nextC.toA - nextC.toB; } return result; } } class Parser { startParse(input, fragments, ranges) { if (typeof input == "string") input = new StringInput(input); ranges = !ranges ? [new Range$1(0, input.length)] : ranges.length ? ranges.map((r2) => new Range$1(r2.from, r2.to)) : [new Range$1(0, 0)]; return this.createParse(input, fragments || [], ranges); } parse(input, fragments, ranges) { let parse2 = this.startParse(input, fragments, ranges); for (; ; ) { let done = parse2.advance(); if (done) return done; } } } class StringInput { constructor(string2) { this.string = string2; } get length() { return this.string.length; } chunk(from) { return this.string.slice(from); } get lineChunks() { return false; } read(from, to) { return this.string.slice(from, to); } } new NodeProp({ perNode: true }); class Stack { constructor(p2, stack, state, reducePos, pos, score2, buffer2, bufferBase, curContext, lookAhead = 0, parent) { this.p = p2; this.stack = stack; this.state = state; this.reducePos = reducePos; this.pos = pos; this.score = score2; this.buffer = buffer2; this.bufferBase = bufferBase; this.curContext = curContext; this.lookAhead = lookAhead; this.parent = parent; } toString() { return `[${this.stack.filter((_, i) => i % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`; } static start(p2, state, pos = 0) { let cx = p2.parser.context; return new Stack(p2, [], state, pos, pos, 0, [], 0, cx ? new StackContext(cx, cx.start) : null, 0, null); } get context() { return this.curContext ? this.curContext.context : null; } pushState(state, start) { this.stack.push(this.state, start, this.bufferBase + this.buffer.length); this.state = state; } reduce(action) { var _a2; let depth = action >> 19, type = action & 65535; let { parser: parser2 } = this.p; let dPrec = parser2.dynamicPrecedence(type); if (dPrec) this.score += dPrec; if (depth == 0) { this.pushState(parser2.getGoto(this.state, type, true), this.reducePos); if (type < parser2.minRepeatTerm) this.storeNode(type, this.reducePos, this.reducePos, 4, true); this.reduceContext(type, this.reducePos); return; } let base2 = this.stack.length - (depth - 1) * 3 - (action & 262144 ? 6 : 0); let start = base2 ? this.stack[base2 - 2] : this.p.ranges[0].from, size = this.reducePos - start; if (size >= 2e3 && !((_a2 = this.p.parser.nodeSet.types[type]) === null || _a2 === void 0 ? void 0 : _a2.isAnonymous)) { if (start == this.p.lastBigReductionStart) { this.p.bigReductionCount++; this.p.lastBigReductionSize = size; } else if (this.p.lastBigReductionSize < size) { this.p.bigReductionCount = 1; this.p.lastBigReductionStart = start; this.p.lastBigReductionSize = size; } } let bufferBase = base2 ? this.stack[base2 - 1] : 0, count = this.bufferBase + this.buffer.length - bufferBase; if (type < parser2.minRepeatTerm || action & 131072) { let pos = parser2.stateFlag(this.state, 1) ? this.pos : this.reducePos; this.storeNode(type, start, pos, count + 4, true); } if (action & 262144) { this.state = this.stack[base2]; } else { let baseStateID = this.stack[base2 - 3]; this.state = parser2.getGoto(baseStateID, type, true); } while (this.stack.length > base2) this.stack.pop(); this.reduceContext(type, start); } storeNode(term, start, end, size = 4, isReduce = false) { if (term == 0 && (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) { let cur2 = this, top2 = this.buffer.length; if (top2 == 0 && cur2.parent) { top2 = cur2.bufferBase - cur2.parent.bufferBase; cur2 = cur2.parent; } if (top2 > 0 && cur2.buffer[top2 - 4] == 0 && cur2.buffer[top2 - 1] > -1) { if (start == end) return; if (cur2.buffer[top2 - 2] >= start) { cur2.buffer[top2 - 2] = end; return; } } } if (!isReduce || this.pos == end) { this.buffer.push(term, start, end, size); } else { let index2 = this.buffer.length; if (index2 > 0 && this.buffer[index2 - 4] != 0) while (index2 > 0 && this.buffer[index2 - 2] > end) { this.buffer[index2] = this.buffer[index2 - 4]; this.buffer[index2 + 1] = this.buffer[index2 - 3]; this.buffer[index2 + 2] = this.buffer[index2 - 2]; this.buffer[index2 + 3] = this.buffer[index2 - 1]; index2 -= 4; if (size > 4) size -= 4; } this.buffer[index2] = term; this.buffer[index2 + 1] = start; this.buffer[index2 + 2] = end; this.buffer[index2 + 3] = size; } } shift(action, next, nextEnd) { let start = this.pos; if (action & 131072) { this.pushState(action & 65535, this.pos); } else if ((action & 262144) == 0) { let nextState = action, { parser: parser2 } = this.p; if (nextEnd > this.pos || next <= parser2.maxNode) { this.pos = nextEnd; if (!parser2.stateFlag(nextState, 1)) this.reducePos = nextEnd; } this.pushState(nextState, start); this.shiftContext(next, start); if (next <= parser2.maxNode) this.buffer.push(next, start, nextEnd, 4); } else { this.pos = nextEnd; this.shiftContext(next, start); if (next <= this.p.parser.maxNode) this.buffer.push(next, start, nextEnd, 4); } } apply(action, next, nextEnd) { if (action & 65536) this.reduce(action); else this.shift(action, next, nextEnd); } useNode(value, next) { let index2 = this.p.reused.length - 1; if (index2 < 0 || this.p.reused[index2] != value) { this.p.reused.push(value); index2++; } let start = this.pos; this.reducePos = this.pos = start + value.length; this.pushState(next, start); this.buffer.push(index2, start, this.reducePos, -1); if (this.curContext) this.updateContext(this.curContext.tracker.reuse(this.curContext.context, value, this, this.p.stream.reset(this.pos - value.length))); } split() { let parent = this; let off = parent.buffer.length; while (off > 0 && parent.buffer[off - 2] > parent.reducePos) off -= 4; let buffer2 = parent.buffer.slice(off), base2 = parent.bufferBase + off; while (parent && base2 == parent.bufferBase) parent = parent.parent; return new Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, buffer2, base2, this.curContext, this.lookAhead, parent); } recoverByDelete(next, nextEnd) { let isNode = next <= this.p.parser.maxNode; if (isNode) this.storeNode(next, this.pos, nextEnd, 4); this.storeNode(0, this.pos, nextEnd, isNode ? 8 : 4); this.pos = this.reducePos = nextEnd; this.score -= 190; } canShift(term) { for (let sim = new SimulatedStack(this); ; ) { let action = this.p.parser.stateSlot(sim.state, 4) || this.p.parser.hasAction(sim.state, term); if (action == 0) return false; if ((action & 65536) == 0) return true; sim.reduce(action); } } recoverByInsert(next) { if (this.stack.length >= 300) return []; let nextStates = this.p.parser.nextStates(this.state); if (nextStates.length > 4 << 1 || this.stack.length >= 120) { let best = []; for (let i = 0, s; i < nextStates.length; i += 2) { if ((s = nextStates[i + 1]) != this.state && this.p.parser.hasAction(s, next)) best.push(nextStates[i], s); } if (this.stack.length < 120) for (let i = 0; best.length < 4 << 1 && i < nextStates.length; i += 2) { let s = nextStates[i + 1]; if (!best.some((v2, i2) => i2 & 1 && v2 == s)) best.push(nextStates[i], s); } nextStates = best; } let result = []; for (let i = 0; i < nextStates.length && result.length < 4; i += 2) { let s = nextStates[i + 1]; if (s == this.state) continue; let stack = this.split(); stack.pushState(s, this.pos); stack.storeNode(0, stack.pos, stack.pos, 4, true); stack.shiftContext(nextStates[i], this.pos); stack.score -= 200; result.push(stack); } return result; } forceReduce() { let { parser: parser2 } = this.p; let reduce = parser2.stateSlot(this.state, 5); if ((reduce & 65536) == 0) return false; if (!parser2.validAction(this.state, reduce)) { let depth = reduce >> 19, term = reduce & 65535; let target = this.stack.length - depth * 3; if (target < 0 || parser2.getGoto(this.stack[target], term, false) < 0) { let backup = this.findForcedReduction(); if (backup == null) return false; reduce = backup; } this.storeNode(0, this.pos, this.pos, 4, true); this.score -= 100; } this.reducePos = this.pos; this.reduce(reduce); return true; } findForcedReduction() { let { parser: parser2 } = this.p, seen = []; let explore = (state, depth) => { if (seen.includes(state)) return; seen.push(state); return parser2.allActions(state, (action) => { if (action & (262144 | 131072)) ; else if (action & 65536) { let rDepth = (action >> 19) - depth; if (rDepth > 1) { let term = action & 65535, target = this.stack.length - rDepth * 3; if (target >= 0 && parser2.getGoto(this.stack[target], term, false) >= 0) return rDepth << 19 | 65536 | term; } } else { let found = explore(action, depth + 1); if (found != null) return found; } }); }; return explore(this.state, 0); } forceAll() { while (!this.p.parser.stateFlag(this.state, 2)) { if (!this.forceReduce()) { this.storeNode(0, this.pos, this.pos, 4, true); break; } } return this; } get deadEnd() { if (this.stack.length != 3) return false; let { parser: parser2 } = this.p; return parser2.data[parser2.stateSlot(this.state, 1)] == 65535 && !parser2.stateSlot(this.state, 4); } restart() { this.state = this.stack[0]; this.stack.length = 0; } sameState(other) { if (this.state != other.state || this.stack.length != other.stack.length) return false; for (let i = 0; i < this.stack.length; i += 3) if (this.stack[i] != other.stack[i]) return false; return true; } get parser() { return this.p.parser; } dialectEnabled(dialectID) { return this.p.parser.dialect.flags[dialectID]; } shiftContext(term, start) { if (this.curContext) this.updateContext(this.curContext.tracker.shift(this.curContext.context, term, this, this.p.stream.reset(start))); } reduceContext(term, start) { if (this.curContext) this.updateContext(this.curContext.tracker.reduce(this.curContext.context, term, this, this.p.stream.reset(start))); } emitContext() { let last = this.buffer.length - 1; if (last < 0 || this.buffer[last] != -3) this.buffer.push(this.curContext.hash, this.pos, this.pos, -3); } emitLookAhead() { let last = this.buffer.length - 1; if (last < 0 || this.buffer[last] != -4) this.buffer.push(this.lookAhead, this.pos, this.pos, -4); } updateContext(context) { if (context != this.curContext.context) { let newCx = new StackContext(this.curContext.tracker, context); if (newCx.hash != this.curContext.hash) this.emitContext(); this.curContext = newCx; } } setLookAhead(lookAhead) { if (lookAhead > this.lookAhead) { this.emitLookAhead(); this.lookAhead = lookAhead; } } close() { if (this.curContext && this.curContext.tracker.strict) this.emitContext(); if (this.lookAhead > 0) this.emitLookAhead(); } } class StackContext { constructor(tracker, context) { this.tracker = tracker; this.context = context; this.hash = tracker.strict ? tracker.hash(context) : 0; } } var Recover; (function(Recover2) { Recover2[Recover2["Insert"] = 200] = "Insert"; Recover2[Recover2["Delete"] = 190] = "Delete"; Recover2[Recover2["Reduce"] = 100] = "Reduce"; Recover2[Recover2["MaxNext"] = 4] = "MaxNext"; Recover2[Recover2["MaxInsertStackDepth"] = 300] = "MaxInsertStackDepth"; Recover2[Recover2["DampenInsertStackDepth"] = 120] = "DampenInsertStackDepth"; Recover2[Recover2["MinBigReduction"] = 2e3] = "MinBigReduction"; })(Recover || (Recover = {})); class SimulatedStack { constructor(start) { this.start = start; this.state = start.state; this.stack = start.stack; this.base = this.stack.l