UNPKG

@hpcc-js/observablehq-compiler

Version:
841 lines (840 loc) 34.5 kB
import "./chunk-rwCFCtyI.js"; import { c as e, d as t, f as n, l as r, o as i, s as a, u as o } from "./dist-CecFOdLa.js"; //#region ../../node_modules/@lezer/lr/dist/index.js var s = class Stack { constructor(e, t, n, r, i, a, o, s, c, l = 0, u) { this.p = e, this.stack = t, this.state = n, this.reducePos = r, this.pos = i, this.score = a, this.buffer = o, this.bufferBase = s, this.curContext = c, this.lookAhead = l, this.parent = u; } toString() { return `[${this.stack.filter((e, t) => t % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`; } static start(e, t, n = 0) { let r = e.parser.context; return new Stack(e, [], t, n, n, 0, [], 0, r ? new StackContext(r, r.start) : null, 0, null); } get context() { return this.curContext ? this.curContext.context : null; } pushState(e, t) { this.stack.push(this.state, t, this.bufferBase + this.buffer.length), this.state = e; } reduce(e) { let t = e >> 19, n = e & 65535, { parser: r } = this.p, i = this.reducePos < this.pos - 25 && this.setLookAhead(this.pos), a = r.dynamicPrecedence(n); if (a && (this.score += a), t == 0) { n < r.minRepeatTerm && this.reducePos < this.pos && (this.reducePos = this.pos), this.pushState(r.getGoto(this.state, n, !0), this.reducePos), n < r.minRepeatTerm && this.storeNode(n, this.reducePos, this.reducePos, i ? 8 : 4, !0), this.reduceContext(n, this.reducePos); return; } let o = this.stack.length - (t - 1) * 3 - (e & 262144 ? 6 : 0), s = o ? this.stack[o - 2] : this.p.ranges[0].from; n < r.minRepeatTerm && s == this.reducePos && this.reducePos < this.pos && (this.reducePos = this.pos); let c = this.reducePos - s; c >= 2e3 && !this.p.parser.nodeSet.types[n]?.isAnonymous && (s == this.p.lastBigReductionStart ? (this.p.bigReductionCount++, this.p.lastBigReductionSize = c) : this.p.lastBigReductionSize < c && (this.p.bigReductionCount = 1, this.p.lastBigReductionStart = s, this.p.lastBigReductionSize = c)); let l = o ? this.stack[o - 1] : 0, u = this.bufferBase + this.buffer.length - l; if (n < r.minRepeatTerm || e & 131072) { let e = r.stateFlag(this.state, 1) ? this.pos : this.reducePos; this.storeNode(n, s, e, u + 4, !0); } if (e & 262144) this.state = this.stack[o]; else { let e = this.stack[o - 3]; this.state = r.getGoto(e, n, !0); } for (; this.stack.length > o;) this.stack.pop(); this.reduceContext(n, s); } storeNode(e, t, n, r = 4, i = !1) { if (e == 0 && (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) { let e = this.buffer.length; if (e > 0 && this.buffer[e - 4] == 0 && this.buffer[e - 1] > -1) { if (t == n) return; if (this.buffer[e - 2] >= t) { this.buffer[e - 2] = n; return; } } } if (!i || this.pos == n) this.buffer.push(e, t, n, r); else { let i = this.buffer.length; if (i > 0 && (this.buffer[i - 4] != 0 || this.buffer[i - 1] < 0)) { let e = !1; for (let t = i; t > 0 && this.buffer[t - 2] > n; t -= 4) if (this.buffer[t - 1] >= 0) { e = !0; break; } if (e) for (; i > 0 && this.buffer[i - 2] > n;) this.buffer[i] = this.buffer[i - 4], this.buffer[i + 1] = this.buffer[i - 3], this.buffer[i + 2] = this.buffer[i - 2], this.buffer[i + 3] = this.buffer[i - 1], i -= 4, r > 4 && (r -= 4); } this.buffer[i] = e, this.buffer[i + 1] = t, this.buffer[i + 2] = n, this.buffer[i + 3] = r; } } shift(e, t, n, r) { if (e & 131072) this.pushState(e & 65535, this.pos); else if (e & 262144) this.pos = r, this.shiftContext(t, n), t <= this.p.parser.maxNode && this.buffer.push(t, n, r, 4); else { let i = e, { parser: a } = this.p; this.pos = r; let o = a.stateFlag(i, 1); !o && (r > n || t <= a.maxNode) && (this.reducePos = r), this.pushState(i, o ? n : Math.min(n, this.reducePos)), this.shiftContext(t, n), t <= a.maxNode && this.buffer.push(t, n, r, 4); } } apply(e, t, n, r) { e & 65536 ? this.reduce(e) : this.shift(e, t, n, r); } useNode(e, t) { let n = this.p.reused.length - 1; (n < 0 || this.p.reused[n] != e) && (this.p.reused.push(e), n++); let r = this.pos; this.reducePos = this.pos = r + e.length, this.pushState(t, r), this.buffer.push(n, r, this.reducePos, -1), this.curContext && this.updateContext(this.curContext.tracker.reuse(this.curContext.context, e, this, this.p.stream.reset(this.pos - e.length))); } split() { let e = this, t = e.buffer.length; for (t && e.buffer[t - 4] == 0 && (t -= 4); t > 0 && e.buffer[t - 2] > e.reducePos;) t -= 4; let n = e.buffer.slice(t), r = e.bufferBase + t; for (; e && r == e.bufferBase;) e = e.parent; return new Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, n, r, this.curContext, this.lookAhead, e); } recoverByDelete(e, t) { let n = e <= this.p.parser.maxNode; n && this.storeNode(e, this.pos, t, 4), this.storeNode(0, this.pos, t, n ? 8 : 4), this.pos = this.reducePos = t, this.score -= 190; } canShift(e) { for (let t = new SimulatedStack(this);;) { let n = this.p.parser.stateSlot(t.state, 4) || this.p.parser.hasAction(t.state, e); if (n == 0) return !1; if (!(n & 65536)) return !0; t.reduce(n); } } recoverByInsert(e) { if (this.stack.length >= 300) return []; let t = this.p.parser.nextStates(this.state); if (t.length > 8 || this.stack.length >= 120) { let n = []; for (let r = 0, i; r < t.length; r += 2) (i = t[r + 1]) != this.state && this.p.parser.hasAction(i, e) && n.push(t[r], i); if (this.stack.length < 120) for (let e = 0; n.length < 8 && e < t.length; e += 2) { let r = t[e + 1]; n.some((e, t) => t & 1 && e == r) || n.push(t[e], r); } t = n; } let n = []; for (let e = 0; e < t.length && n.length < 4; e += 2) { let r = t[e + 1]; if (r == this.state) continue; let i = this.split(); i.pushState(r, this.pos), i.storeNode(0, i.pos, i.pos, 4, !0), i.shiftContext(t[e], this.pos), i.reducePos = this.pos, i.score -= 200, n.push(i); } return n; } forceReduce() { let { parser: e } = this.p, t = e.stateSlot(this.state, 5); if (!(t & 65536)) return !1; if (!e.validAction(this.state, t)) { let n = t >> 19, r = t & 65535, i = this.stack.length - n * 3; if (i < 0 || e.getGoto(this.stack[i], r, !1) < 0) { let e = this.findForcedReduction(); if (e == null) return !1; t = e; } this.storeNode(0, this.pos, this.pos, 4, !0), this.score -= 100; } return this.reducePos = this.pos, this.reduce(t), !0; } findForcedReduction() { let { parser: e } = this.p, t = [], explore = (n, r) => { if (!t.includes(n)) return t.push(n), e.allActions(n, (t) => { if (!(t & 393216)) if (t & 65536) { let n = (t >> 19) - r; if (n > 1) { let r = t & 65535, i = this.stack.length - n * 3; if (i >= 0 && e.getGoto(this.stack[i], r, !1) >= 0) return n << 19 | 65536 | r; } } else { let e = explore(t, r + 1); if (e != null) return e; } }); }; return explore(this.state, 0); } forceAll() { for (; !this.p.parser.stateFlag(this.state, 2);) if (!this.forceReduce()) { this.storeNode(0, this.pos, this.pos, 4, !0); break; } return this; } get deadEnd() { if (this.stack.length != 3) return !1; let { parser: e } = this.p; return e.data[e.stateSlot(this.state, 1)] == 65535 && !e.stateSlot(this.state, 4); } restart() { this.storeNode(0, this.pos, this.pos, 4, !0), this.state = this.stack[0], this.stack.length = 0; } sameState(e) { if (this.state != e.state || this.stack.length != e.stack.length) return !1; for (let t = 0; t < this.stack.length; t += 3) if (this.stack[t] != e.stack[t]) return !1; return !0; } get parser() { return this.p.parser; } dialectEnabled(e) { return this.p.parser.dialect.flags[e]; } shiftContext(e, t) { this.curContext && this.updateContext(this.curContext.tracker.shift(this.curContext.context, e, this, this.p.stream.reset(t))); } reduceContext(e, t) { this.curContext && this.updateContext(this.curContext.tracker.reduce(this.curContext.context, e, this, this.p.stream.reset(t))); } emitContext() { let e = this.buffer.length - 1; (e < 0 || this.buffer[e] != -3) && this.buffer.push(this.curContext.hash, this.pos, this.pos, -3); } emitLookAhead() { let e = this.buffer.length - 1; (e < 0 || this.buffer[e] != -4) && this.buffer.push(this.lookAhead, this.pos, this.pos, -4); } updateContext(e) { if (e != this.curContext.context) { let t = new StackContext(this.curContext.tracker, e); t.hash != this.curContext.hash && this.emitContext(), this.curContext = t; } } setLookAhead(e) { return e <= this.lookAhead ? !1 : (this.emitLookAhead(), this.lookAhead = e, !0); } close() { this.curContext && this.curContext.tracker.strict && this.emitContext(), this.lookAhead > 0 && this.emitLookAhead(); } }, StackContext = class { constructor(e, t) { this.tracker = e, this.context = t, this.hash = e.strict ? e.hash(t) : 0; } }, SimulatedStack = class { constructor(e) { this.start = e, this.state = e.state, this.stack = e.stack, this.base = this.stack.length; } reduce(e) { let t = e & 65535, n = e >> 19; n == 0 ? (this.stack == this.start.stack && (this.stack = this.stack.slice()), this.stack.push(this.state, 0, 0), this.base += 3) : this.base -= (n - 1) * 3; let r = this.start.p.parser.getGoto(this.stack[this.base - 3], t, !0); this.state = r; } }, c = class StackBufferCursor { constructor(e, t, n) { this.stack = e, this.pos = t, this.index = n, this.buffer = e.buffer, this.index == 0 && this.maybeNext(); } static create(e, t = e.bufferBase + e.buffer.length) { return new StackBufferCursor(e, t, t - e.bufferBase); } maybeNext() { let e = this.stack.parent; e != null && (this.index = this.stack.bufferBase - e.bufferBase, this.stack = e, this.buffer = e.buffer); } 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]; } next() { this.index -= 4, this.pos -= 4, this.index == 0 && this.maybeNext(); } fork() { return new StackBufferCursor(this.stack, this.pos, this.index); } }; function decodeArray(e, t = Uint16Array) { if (typeof e != "string") return e; let n = null; for (let r = 0, i = 0; r < e.length;) { let a = 0; for (;;) { let t = e.charCodeAt(r++), n = !1; if (t == 126) { a = 65535; break; } t >= 92 && t--, t >= 34 && t--; let i = t - 32; if (i >= 46 && (i -= 46, n = !0), a += i, n) break; a *= 46; } n ? n[i++] = a : n = new t(a); } return n; } var CachedToken = class { constructor() { this.start = -1, this.value = -1, this.end = -1, this.extended = -1, this.lookAhead = 0, this.mask = 0, this.context = 0; } }, l = new CachedToken(), InputStream = class { constructor(e, t) { this.input = e, this.ranges = t, this.chunk = "", this.chunkOff = 0, this.chunk2 = "", this.chunk2Pos = 0, this.next = -1, this.token = l, this.rangeIndex = 0, this.pos = this.chunkPos = t[0].from, this.range = t[0], this.end = t[t.length - 1].to, this.readNext(); } resolveOffset(e, t) { let n = this.range, r = this.rangeIndex, i = this.pos + e; for (; i < n.from;) { if (!r) return null; let e = this.ranges[--r]; i -= n.from - e.to, n = e; } for (; t < 0 ? i > n.to : i >= n.to;) { if (r == this.ranges.length - 1) return null; let e = this.ranges[++r]; i += e.from - n.to, n = e; } return i; } clipPos(e) { if (e >= this.range.from && e < this.range.to) return e; for (let t of this.ranges) if (t.to > e) return Math.max(e, t.from); return this.end; } peek(e) { let t = this.chunkOff + e, n, r; if (t >= 0 && t < this.chunk.length) n = this.pos + e, r = this.chunk.charCodeAt(t); else { let t = this.resolveOffset(e, 1); if (t == null) return -1; if (n = t, n >= this.chunk2Pos && n < this.chunk2Pos + this.chunk2.length) r = this.chunk2.charCodeAt(n - this.chunk2Pos); else { let e = this.rangeIndex, t = this.range; for (; t.to <= n;) t = this.ranges[++e]; this.chunk2 = this.input.chunk(this.chunk2Pos = n), n + this.chunk2.length > t.to && (this.chunk2 = this.chunk2.slice(0, t.to - n)), r = this.chunk2.charCodeAt(0); } } return n >= this.token.lookAhead && (this.token.lookAhead = n + 1), r; } acceptToken(e, t = 0) { let n = t ? this.resolveOffset(t, -1) : this.pos; if (n == null || n < this.token.start) throw RangeError("Token end out of bounds"); this.token.value = e, this.token.end = n; } acceptTokenTo(e, t) { this.token.value = e, this.token.end = t; } getChunk() { if (this.pos >= this.chunk2Pos && this.pos < this.chunk2Pos + this.chunk2.length) { let { chunk: e, chunkPos: t } = this; this.chunk = this.chunk2, this.chunkPos = this.chunk2Pos, this.chunk2 = e, this.chunk2Pos = t, this.chunkOff = this.pos - this.chunkPos; } else { this.chunk2 = this.chunk, this.chunk2Pos = this.chunkPos; let e = this.input.chunk(this.pos), t = this.pos + e.length; this.chunk = t > this.range.to ? e.slice(0, this.range.to - this.pos) : e, this.chunkPos = this.pos, this.chunkOff = 0; } } readNext() { return this.chunkOff >= this.chunk.length && (this.getChunk(), this.chunkOff == this.chunk.length) ? this.next = -1 : this.next = this.chunk.charCodeAt(this.chunkOff); } advance(e = 1) { for (this.chunkOff += e; this.pos + e >= this.range.to;) { if (this.rangeIndex == this.ranges.length - 1) return this.setDone(); e -= this.range.to - this.pos, this.range = this.ranges[++this.rangeIndex], this.pos = this.range.from; } return this.pos += e, this.pos >= this.token.lookAhead && (this.token.lookAhead = this.pos + 1), this.readNext(); } setDone() { return this.pos = this.chunkPos = this.end, this.range = this.ranges[this.rangeIndex = this.ranges.length - 1], this.chunk = "", this.next = -1; } reset(e, t) { if (t ? (this.token = t, t.start = e, t.lookAhead = e + 1, t.value = t.extended = -1) : this.token = l, this.pos != e) { if (this.pos = e, e == this.end) return this.setDone(), this; for (; e < this.range.from;) this.range = this.ranges[--this.rangeIndex]; for (; e >= this.range.to;) this.range = this.ranges[++this.rangeIndex]; e >= this.chunkPos && e < this.chunkPos + this.chunk.length ? this.chunkOff = e - this.chunkPos : (this.chunk = "", this.chunkOff = 0), this.readNext(); } return this; } read(e, t) { if (e >= this.chunkPos && t <= this.chunkPos + this.chunk.length) return this.chunk.slice(e - this.chunkPos, t - this.chunkPos); if (e >= this.chunk2Pos && t <= this.chunk2Pos + this.chunk2.length) return this.chunk2.slice(e - this.chunk2Pos, t - this.chunk2Pos); if (e >= this.range.from && t <= this.range.to) return this.input.read(e, t); let n = ""; for (let r of this.ranges) { if (r.from >= t) break; r.to > e && (n += this.input.read(Math.max(r.from, e), Math.min(r.to, t))); } return n; } }, TokenGroup = class { constructor(e, t) { this.data = e, this.id = t; } token(e, t) { let { parser: n } = t.p; readToken(this.data, e, t, this.id, n.data, n.tokenPrecTable); } }; TokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = !1; var LocalTokenGroup = class { constructor(e, t, n) { this.precTable = t, this.elseToken = n, this.data = typeof e == "string" ? decodeArray(e) : e; } token(e, t) { let n = e.pos, r = 0; for (;;) { let n = e.next < 0, i = e.resolveOffset(1, 1); if (readToken(this.data, e, t, 0, this.data, this.precTable), e.token.value > -1) break; if (this.elseToken == null) return; if (n || r++, i == null) break; e.reset(i, e.token); } r && (e.reset(n, e.token), e.acceptToken(this.elseToken, r)); } }; LocalTokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = !1; var ExternalTokenizer = class { constructor(e, t = {}) { this.token = e, this.contextual = !!t.contextual, this.fallback = !!t.fallback, this.extend = !!t.extend; } }; function readToken(e, t, n, r, i, a) { let o = 0, s = 1 << r, { dialect: c } = n.p.parser; scan: for (; (s & e[o]) != 0;) { let n = e[o + 1]; for (let r = o + 3; r < n; r += 2) if ((e[r + 1] & s) > 0) { let n = e[r]; if (c.allows(n) && (t.token.value == -1 || t.token.value == n || overrides(n, t.token.value, i, a))) { t.acceptToken(n); break; } } let r = t.next, l = 0, u = e[o + 2]; if (t.next < 0 && u > l && e[n + u * 3 - 3] == 65535) { o = e[n + u * 3 - 1]; continue scan; } for (; l < u;) { let i = l + u >> 1, a = n + i + (i << 1), s = e[a], c = e[a + 1] || 65536; if (r < s) u = i; else if (r >= c) l = i + 1; else { o = e[a + 2], t.advance(); continue scan; } } break; } } function findOffset(e, t, n) { for (let r = t, i; (i = e[r]) != 65535; r++) if (i == n) return r - t; return -1; } function overrides(e, t, n, r) { let i = findOffset(n, r, t); return i < 0 || findOffset(n, r, e) < i; } var u = typeof process < "u" && process.env && /\bparse\b/.test(process.env.LOG), d = null; function cutAt(e, t, n) { let r = e.cursor(a.IncludeAnonymous); for (r.moveTo(t);;) if (!(n < 0 ? r.childBefore(t) : r.childAfter(t))) for (;;) { if ((n < 0 ? r.to < t : r.from > t) && !r.type.isError) return n < 0 ? Math.max(0, Math.min(r.to - 1, t - 25)) : Math.min(e.length, Math.max(r.from + 1, t + 25)); if (n < 0 ? r.prevSibling() : r.nextSibling()) break; if (!r.parent()) return n < 0 ? 0 : e.length; } } var FragmentCursor = class { constructor(e, t) { this.fragments = e, this.nodeSet = t, this.i = 0, this.fragment = null, this.safeFrom = -1, this.safeTo = -1, this.trees = [], this.start = [], this.index = [], this.nextFragment(); } nextFragment() { let e = this.fragment = this.i == this.fragments.length ? null : this.fragments[this.i++]; if (e) { for (this.safeFrom = e.openStart ? cutAt(e.tree, e.from + e.offset, 1) - e.offset : e.from, this.safeTo = e.openEnd ? cutAt(e.tree, e.to + e.offset, -1) - e.offset : e.to; this.trees.length;) this.trees.pop(), this.start.pop(), this.index.pop(); this.trees.push(e.tree), this.start.push(-e.offset), this.index.push(0), this.nextStart = this.safeFrom; } else this.nextStart = 1e9; } nodeAt(t) { if (t < this.nextStart) return null; for (; this.fragment && this.safeTo <= t;) this.nextFragment(); if (!this.fragment) return null; for (;;) { let r = this.trees.length - 1; if (r < 0) return this.nextFragment(), null; let i = this.trees[r], a = this.index[r]; if (a == i.children.length) { this.trees.pop(), this.start.pop(), this.index.pop(); continue; } let o = i.children[a], s = this.start[r] + i.positions[a]; if (s > t) return this.nextStart = s, null; if (o instanceof n) { if (s == t) { if (s < this.safeFrom) return null; let t = s + o.length; if (t <= this.safeTo) { let n = o.prop(e.lookAhead); if (!n || t + n < this.fragment.to) return o; } } this.index[r]++, s + o.length >= Math.max(this.safeFrom, t) && (this.trees.push(o), this.start.push(s), this.index.push(0)); } else this.index[r]++, this.nextStart = s + o.length; } } }, TokenCache = class { constructor(e, t) { this.stream = t, this.tokens = [], this.mainToken = null, this.actions = [], this.tokens = e.tokenizers.map((e) => new CachedToken()); } getActions(e) { let t = 0, n = null, { parser: r } = e.p, { tokenizers: i } = r, a = r.stateSlot(e.state, 3), o = e.curContext ? e.curContext.hash : 0, s = 0; for (let r = 0; r < i.length; r++) { if (!(1 << r & a)) continue; let c = i[r], l = this.tokens[r]; if (!(n && !c.fallback) && ((c.contextual || l.start != e.pos || l.mask != a || l.context != o) && (this.updateCachedToken(l, c, e), l.mask = a, l.context = o), l.lookAhead > l.end + 25 && (s = Math.max(l.lookAhead, s)), l.value != 0)) { let r = t; if (l.extended > -1 && (t = this.addActions(e, l.extended, l.end, t)), t = this.addActions(e, l.value, l.end, t), !c.extend && (n = l, t > r)) break; } } for (; this.actions.length > t;) this.actions.pop(); return s && e.setLookAhead(s), !n && e.pos == this.stream.end && (n = new CachedToken(), n.value = e.p.parser.eofTerm, n.start = n.end = e.pos, t = this.addActions(e, n.value, n.end, t)), this.mainToken = n, this.actions; } getMainToken(e) { if (this.mainToken) return this.mainToken; let t = new CachedToken(), { pos: n, p: r } = e; return t.start = n, t.end = Math.min(n + 1, r.stream.end), t.value = n == r.stream.end ? r.parser.eofTerm : 0, t; } updateCachedToken(e, t, n) { let r = this.stream.clipPos(n.pos); if (t.token(this.stream.reset(r, e), n), e.value > -1) { let { parser: t } = n.p; for (let r = 0; r < t.specialized.length; r++) if (t.specialized[r] == e.value) { let i = t.specializers[r](this.stream.read(e.start, e.end), n); if (i >= 0 && n.p.parser.dialect.allows(i >> 1)) { i & 1 ? e.extended = i >> 1 : e.value = i >> 1; break; } } } else e.value = 0, e.end = this.stream.clipPos(r + 1); } putAction(e, t, n, r) { for (let t = 0; t < r; t += 3) if (this.actions[t] == e) return r; return this.actions[r++] = e, this.actions[r++] = t, this.actions[r++] = n, r; } addActions(e, t, n, r) { let { state: i } = e, { parser: a } = e.p, { data: o } = a; for (let e = 0; e < 2; e++) for (let s = a.stateSlot(i, e ? 2 : 1);; s += 3) { if (o[s] == 65535) if (o[s + 1] == 1) s = pair(o, s + 2); else { r == 0 && o[s + 1] == 2 && (r = this.putAction(pair(o, s + 2), t, n, r)); break; } o[s] == t && (r = this.putAction(pair(o, s + 1), t, n, r)); } return r; } }, Parse = class { constructor(e, t, n, r) { this.parser = e, this.input = t, this.ranges = r, this.recovering = 0, this.nextStackID = 9812, this.minStackPos = 0, this.reused = [], this.stoppedAt = null, this.lastBigReductionStart = -1, this.lastBigReductionSize = 0, this.bigReductionCount = 0, this.stream = new InputStream(t, r), this.tokens = new TokenCache(e, this.stream), this.topTerm = e.top[1]; let { from: i } = r[0]; this.stacks = [s.start(this, e.top[0], i)], this.fragments = n.length && this.stream.end - i > e.bufferLength * 4 ? new FragmentCursor(n, e.nodeSet) : null; } get parsedPos() { return this.minStackPos; } advance() { let e = this.stacks, t = this.minStackPos, n = this.stacks = [], r, i; if (this.bigReductionCount > 300 && e.length == 1) { let [t] = e; for (; t.forceReduce() && t.stack.length && t.stack[t.stack.length - 2] >= this.lastBigReductionStart;); this.bigReductionCount = this.lastBigReductionSize = 0; } for (let a = 0; a < e.length; a++) { let o = e[a]; for (;;) { if (this.tokens.mainToken = null, o.pos > t) n.push(o); else if (this.advanceStack(o, n, e)) continue; else { r || (r = [], i = []), r.push(o); let e = this.tokens.getMainToken(o); i.push(e.value, e.end); } break; } } if (!n.length) { let e = r && findFinished(r); if (e) return u && console.log("Finish with " + this.stackID(e)), this.stackToTree(e); if (this.parser.strict) throw u && r && console.log("Stuck with token " + (this.tokens.mainToken ? this.parser.getName(this.tokens.mainToken.value) : "none")), SyntaxError("No parse at " + t); this.recovering ||= 5; } if (this.recovering && r) { let e = this.stoppedAt != null && r[0].pos > this.stoppedAt ? r[0] : this.runRecovery(r, i, n); if (e) return u && console.log("Force-finish " + this.stackID(e)), this.stackToTree(e.forceAll()); } if (this.recovering) { let e = this.recovering == 1 ? 1 : this.recovering * 3; if (n.length > e) for (n.sort((e, t) => t.score - e.score); n.length > e;) n.pop(); n.some((e) => e.reducePos > t) && this.recovering--; } else if (n.length > 1) { outer: for (let e = 0; e < n.length - 1; e++) { let t = n[e]; for (let r = e + 1; r < n.length; r++) { let i = n[r]; if (t.sameState(i) || t.buffer.length > 500 && i.buffer.length > 500) if ((t.score - i.score || t.buffer.length - i.buffer.length) > 0) n.splice(r--, 1); else { n.splice(e--, 1); continue outer; } } } n.length > 12 && (n.sort((e, t) => t.score - e.score), n.splice(12, n.length - 12)); } this.minStackPos = n[0].pos; for (let e = 1; e < n.length; e++) n[e].pos < this.minStackPos && (this.minStackPos = n[e].pos); return null; } stopAt(e) { if (this.stoppedAt != null && this.stoppedAt < e) throw RangeError("Can't move stoppedAt forward"); this.stoppedAt = e; } advanceStack(t, r, i) { let a = t.pos, { parser: o } = this, s = u ? this.stackID(t) + " -> " : ""; if (this.stoppedAt != null && a > this.stoppedAt) return t.forceReduce() ? t : null; if (this.fragments) { let r = t.curContext && t.curContext.tracker.strict, i = r ? t.curContext.hash : 0; for (let c = this.fragments.nodeAt(a); c;) { let a = this.parser.nodeSet.types[c.type.id] == c.type ? o.getGoto(t.state, c.type.id) : -1; if (a > -1 && c.length && (!r || (c.prop(e.contextHash) || 0) == i)) return t.useNode(c, a), u && console.log(s + this.stackID(t) + ` (via reuse of ${o.getName(c.type.id)})`), !0; if (!(c instanceof n) || c.children.length == 0 || c.positions[0] > 0) break; let l = c.children[0]; if (l instanceof n && c.positions[0] == 0) c = l; else break; } } let c = o.stateSlot(t.state, 4); if (c > 0) return t.reduce(c), u && console.log(s + this.stackID(t) + ` (via always-reduce ${o.getName(c & 65535)})`), !0; if (t.stack.length >= 8400) for (; t.stack.length > 6e3 && t.forceReduce();); let l = this.tokens.getActions(t); for (let e = 0; e < l.length;) { let n = l[e++], c = l[e++], d = l[e++], f = e == l.length || !i, p = f ? t : t.split(), m = this.tokens.mainToken; if (p.apply(n, c, m ? m.start : p.pos, d), u && console.log(s + this.stackID(p) + ` (via ${n & 65536 ? `reduce of ${o.getName(n & 65535)}` : "shift"} for ${o.getName(c)} @ ${a}${p == t ? "" : ", split"})`), f) return !0; p.pos > a ? r.push(p) : i.push(p); } return !1; } advanceFully(e, t) { let n = e.pos; for (;;) { if (!this.advanceStack(e, null, null)) return !1; if (e.pos > n) return pushStackDedup(e, t), !0; } } runRecovery(e, t, n) { let r = null, i = !1; for (let a = 0; a < e.length; a++) { let o = e[a], s = t[a << 1], c = t[(a << 1) + 1], l = u ? this.stackID(o) + " -> " : ""; if (o.deadEnd && (i || (i = !0, o.restart(), u && console.log(l + this.stackID(o) + " (restarted)"), this.advanceFully(o, n)))) continue; let d = o.split(), f = l; for (let e = 0; e < 10 && d.forceReduce() && (u && console.log(f + this.stackID(d) + " (via force-reduce)"), !this.advanceFully(d, n)); e++) u && (f = this.stackID(d) + " -> "); for (let e of o.recoverByInsert(s)) u && console.log(l + this.stackID(e) + " (via recover-insert)"), this.advanceFully(e, n); this.stream.end > o.pos ? (c == o.pos && (c++, s = 0), o.recoverByDelete(s, c), u && console.log(l + this.stackID(o) + ` (via recover-delete ${this.parser.getName(s)})`), pushStackDedup(o, n)) : (!r || r.score < d.score) && (r = d); } return r; } stackToTree(e) { return e.close(), n.build({ buffer: c.create(e), nodeSet: this.parser.nodeSet, topID: this.topTerm, maxBufferLength: this.parser.bufferLength, reused: this.reused, start: this.ranges[0].from, length: e.pos - this.ranges[0].from, minRepeatType: this.parser.minRepeatTerm }); } stackID(e) { let t = (d ||= /* @__PURE__ */ new WeakMap()).get(e); return t || d.set(e, t = String.fromCodePoint(this.nextStackID++)), t + e; } }; function pushStackDedup(e, t) { for (let n = 0; n < t.length; n++) { let r = t[n]; if (r.pos == e.pos && r.sameState(e)) { t[n].score < e.score && (t[n] = e); return; } } t.push(e); } var Dialect = class { constructor(e, t, n) { this.source = e, this.flags = t, this.disabled = n; } allows(e) { return !this.disabled || this.disabled[e] == 0; } }, id = (e) => e, ContextTracker = class { constructor(e) { this.start = e.start, this.shift = e.shift || id, this.reduce = e.reduce || id, this.reuse = e.reuse || id, this.hash = e.hash || (() => 0), this.strict = e.strict !== !1; } }, f = class LRParser extends t { constructor(t) { if (super(), this.wrappers = [], t.version != 14) throw RangeError(`Parser version (${t.version}) doesn't match runtime version (14)`); let n = t.nodeNames.split(" "); this.minRepeatTerm = n.length; for (let e = 0; e < t.repeatNodeCount; e++) n.push(""); let a = Object.keys(t.topRules).map((e) => t.topRules[e][1]), s = []; for (let e = 0; e < n.length; e++) s.push([]); function setProp(e, t, n) { s[e].push([t, t.deserialize(String(n))]); } if (t.nodeProps) for (let n of t.nodeProps) { let t = n[0]; typeof t == "string" && (t = e[t]); for (let e = 1; e < n.length;) { let r = n[e++]; if (r >= 0) setProp(r, t, n[e++]); else { let i = n[e + -r]; for (let a = -r; a > 0; a--) setProp(n[e++], t, i); e++; } } } this.nodeSet = new r(n.map((e, n) => o.define({ name: n >= this.minRepeatTerm ? void 0 : e, id: n, props: s[n], top: a.indexOf(n) > -1, error: n == 0, skipped: t.skippedNodes && t.skippedNodes.indexOf(n) > -1 }))), t.propSources && (this.nodeSet = this.nodeSet.extend(...t.propSources)), this.strict = !1, this.bufferLength = i; let c = decodeArray(t.tokenData); this.context = t.context, this.specializerSpecs = t.specialized || [], this.specialized = new Uint16Array(this.specializerSpecs.length); for (let e = 0; e < this.specializerSpecs.length; e++) this.specialized[e] = this.specializerSpecs[e].term; this.specializers = this.specializerSpecs.map(getSpecializer), this.states = decodeArray(t.states, Uint32Array), this.data = decodeArray(t.stateData), this.goto = decodeArray(t.goto), this.maxTerm = t.maxTerm, this.tokenizers = t.tokenizers.map((e) => typeof e == "number" ? new TokenGroup(c, e) : e), this.topRules = t.topRules, this.dialects = t.dialects || {}, this.dynamicPrecedences = t.dynamicPrecedences || null, this.tokenPrecTable = t.tokenPrec, this.termNames = t.termNames || null, this.maxNode = this.nodeSet.types.length - 1, this.dialect = this.parseDialect(), this.top = this.topRules[Object.keys(this.topRules)[0]]; } createParse(e, t, n) { let r = new Parse(this, e, t, n); for (let i of this.wrappers) r = i(r, e, t, n); return r; } getGoto(e, t, n = !1) { let r = this.goto; if (t >= r[0]) return -1; for (let i = r[t + 1];;) { let t = r[i++], a = t & 1, o = r[i++]; if (a && n) return o; for (let n = i + (t >> 1); i < n; i++) if (r[i] == e) return o; if (a) return -1; } } hasAction(e, t) { let n = this.data; for (let r = 0; r < 2; r++) for (let i = this.stateSlot(e, r ? 2 : 1), a;; i += 3) { if ((a = n[i]) == 65535) if (n[i + 1] == 1) a = n[i = pair(n, i + 2)]; else if (n[i + 1] == 2) return pair(n, i + 2); else break; if (a == t || a == 0) return pair(n, i + 1); } return 0; } stateSlot(e, t) { return this.states[e * 6 + t]; } stateFlag(e, t) { return (this.stateSlot(e, 0) & t) > 0; } validAction(e, t) { return !!this.allActions(e, (e) => e == t ? !0 : null); } allActions(e, t) { let n = this.stateSlot(e, 4), r = n ? t(n) : void 0; for (let n = this.stateSlot(e, 1); r == null; n += 3) { if (this.data[n] == 65535) if (this.data[n + 1] == 1) n = pair(this.data, n + 2); else break; r = t(pair(this.data, n + 1)); } return r; } nextStates(e) { let t = []; for (let n = this.stateSlot(e, 1);; n += 3) { if (this.data[n] == 65535) if (this.data[n + 1] == 1) n = pair(this.data, n + 2); else break; if (!(this.data[n + 2] & 1)) { let e = this.data[n + 1]; t.some((t, n) => n & 1 && t == e) || t.push(this.data[n], e); } } return t; } configure(e) { let t = Object.assign(Object.create(LRParser.prototype), this); if (e.props && (t.nodeSet = this.nodeSet.extend(...e.props)), e.top) { let n = this.topRules[e.top]; if (!n) throw RangeError(`Invalid top rule name ${e.top}`); t.top = n; } return e.tokenizers && (t.tokenizers = this.tokenizers.map((t) => { let n = e.tokenizers.find((e) => e.from == t); return n ? n.to : t; })), e.specializers && (t.specializers = this.specializers.slice(), t.specializerSpecs = this.specializerSpecs.map((n, r) => { let i = e.specializers.find((e) => e.from == n.external); if (!i) return n; let a = Object.assign(Object.assign({}, n), { external: i.to }); return t.specializers[r] = getSpecializer(a), a; })), e.contextTracker && (t.context = e.contextTracker), e.dialect && (t.dialect = this.parseDialect(e.dialect)), e.strict != null && (t.strict = e.strict), e.wrap && (t.wrappers = t.wrappers.concat(e.wrap)), e.bufferLength != null && (t.bufferLength = e.bufferLength), t; } hasWrappers() { return this.wrappers.length > 0; } getName(e) { return this.termNames ? this.termNames[e] : String(e <= this.maxNode && this.nodeSet.types[e].name || e); } get eofTerm() { return this.maxNode + 1; } get topNode() { return this.nodeSet.types[this.top[1]]; } dynamicPrecedence(e) { let t = this.dynamicPrecedences; return t == null ? 0 : t[e] || 0; } parseDialect(e) { let t = Object.keys(this.dialects), n = t.map(() => !1); if (e) for (let r of e.split(" ")) { let e = t.indexOf(r); e >= 0 && (n[e] = !0); } let r = null; for (let e = 0; e < t.length; e++) if (!n[e]) for (let n = this.dialects[t[e]], i; (i = this.data[n++]) != 65535;) (r ||= new Uint8Array(this.maxTerm + 1))[i] = 1; return new Dialect(e, n, r); } static deserialize(e) { return new LRParser(e); } }; function pair(e, t) { return e[t] | e[t + 1] << 16; } function findFinished(e) { let t = null; for (let n of e) { let e = n.p.stoppedAt; (n.pos == n.p.stream.end || e != null && n.pos > e) && n.p.parser.stateFlag(n.state, 2) && (!t || t.score < n.score) && (t = n); } return t; } function getSpecializer(e) { if (e.external) { let t = +!!e.extend; return (n, r) => e.external(n, r) << 1 | t; } return e.get; } //#endregion export { LocalTokenGroup as i, ExternalTokenizer as n, f as r, ContextTracker as t }; //# sourceMappingURL=dist-D1t18L70.js.map