groove-dev 0.17.8 → 0.18.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/node_modules/@groove-dev/cli/package.json +4 -3
  2. package/node_modules/@groove-dev/daemon/google-oauth.json +5 -0
  3. package/node_modules/@groove-dev/daemon/integrations-registry.json +0 -40
  4. package/node_modules/@groove-dev/daemon/package.json +4 -3
  5. package/node_modules/@groove-dev/daemon/src/api.js +212 -21
  6. package/node_modules/@groove-dev/daemon/src/index.js +68 -1
  7. package/node_modules/@groove-dev/daemon/src/integrations.js +59 -20
  8. package/node_modules/@groove-dev/daemon/src/process.js +83 -11
  9. package/node_modules/@groove-dev/daemon/src/providers/claude-code.js +4 -0
  10. package/node_modules/@groove-dev/daemon/src/registry.js +1 -1
  11. package/node_modules/@groove-dev/gui/.groove/audit.log +1 -0
  12. package/node_modules/@groove-dev/gui/.groove/codebase-index.json +64 -0
  13. package/node_modules/@groove-dev/gui/.groove/config.json +10 -0
  14. package/node_modules/@groove-dev/gui/.groove/coordination.md +5 -0
  15. package/node_modules/@groove-dev/gui/.groove/credentials.json +6 -0
  16. package/node_modules/@groove-dev/gui/.groove/daemon.port +1 -0
  17. package/node_modules/@groove-dev/gui/.groove/federation/identity.key +3 -0
  18. package/node_modules/@groove-dev/gui/.groove/federation/identity.pub +3 -0
  19. package/node_modules/@groove-dev/gui/.groove/integrations/package.json +6 -0
  20. package/node_modules/@groove-dev/gui/.groove/state.json +3 -0
  21. package/node_modules/@groove-dev/gui/dist/assets/index-x5suAiK7.js +182 -0
  22. package/node_modules/@groove-dev/gui/dist/index.html +1 -1
  23. package/node_modules/@groove-dev/gui/package.json +5 -4
  24. package/node_modules/@groove-dev/gui/src/App.jsx +149 -76
  25. package/node_modules/@groove-dev/gui/src/components/AgentActions.jsx +130 -1
  26. package/node_modules/@groove-dev/gui/src/components/AgentChat.jsx +47 -7
  27. package/node_modules/@groove-dev/gui/src/components/AgentNode.jsx +13 -83
  28. package/node_modules/@groove-dev/gui/src/components/SpawnPanel.jsx +918 -580
  29. package/node_modules/@groove-dev/gui/src/stores/groove.js +31 -2
  30. package/node_modules/@groove-dev/gui/src/views/AgentTree.jsx +133 -67
  31. package/node_modules/@groove-dev/gui/src/views/FileEditor.jsx +85 -1
  32. package/node_modules/@groove-dev/gui/src/views/IntegrationsStore.jsx +121 -44
  33. package/package.json +1 -2
  34. package/packages/cli/package.json +4 -3
  35. package/packages/daemon/integrations-registry.json +0 -40
  36. package/packages/daemon/package.json +4 -3
  37. package/packages/daemon/src/api.js +212 -21
  38. package/packages/daemon/src/index.js +68 -1
  39. package/packages/daemon/src/integrations.js +59 -20
  40. package/packages/daemon/src/process.js +83 -11
  41. package/packages/daemon/src/providers/claude-code.js +4 -0
  42. package/packages/daemon/src/registry.js +1 -1
  43. package/packages/gui/dist/assets/index-x5suAiK7.js +182 -0
  44. package/packages/gui/dist/index.html +1 -1
  45. package/packages/gui/node_modules/.vite/deps/@codemirror_autocomplete.js +68 -0
  46. package/packages/gui/node_modules/.vite/deps/@codemirror_autocomplete.js.map +7 -0
  47. package/packages/gui/node_modules/.vite/deps/@codemirror_commands.js +1420 -0
  48. package/packages/gui/node_modules/.vite/deps/@codemirror_commands.js.map +7 -0
  49. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-css.js +17 -0
  50. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-css.js.map +7 -0
  51. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-html.js +22 -0
  52. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-html.js.map +7 -0
  53. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-javascript.js +34 -0
  54. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-javascript.js.map +7 -0
  55. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-json.js +101 -0
  56. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-json.js.map +7 -0
  57. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-markdown.js +2534 -0
  58. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-markdown.js.map +7 -0
  59. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-python.js +789 -0
  60. package/packages/gui/node_modules/.vite/deps/@codemirror_lang-python.js.map +7 -0
  61. package/packages/gui/node_modules/.vite/deps/@codemirror_language.js +115 -0
  62. package/packages/gui/node_modules/.vite/deps/@codemirror_language.js.map +7 -0
  63. package/packages/gui/node_modules/.vite/deps/@codemirror_search.js +1136 -0
  64. package/packages/gui/node_modules/.vite/deps/@codemirror_search.js.map +7 -0
  65. package/packages/gui/node_modules/.vite/deps/@codemirror_state.js +63 -0
  66. package/packages/gui/node_modules/.vite/deps/@codemirror_state.js.map +7 -0
  67. package/packages/gui/node_modules/.vite/deps/@codemirror_theme-one-dark.js +179 -0
  68. package/packages/gui/node_modules/.vite/deps/@codemirror_theme-one-dark.js.map +7 -0
  69. package/packages/gui/node_modules/.vite/deps/@codemirror_view.js +104 -0
  70. package/packages/gui/node_modules/.vite/deps/@codemirror_view.js.map +7 -0
  71. package/packages/gui/node_modules/.vite/deps/@xterm_addon-fit.js +46 -0
  72. package/packages/gui/node_modules/.vite/deps/@xterm_addon-fit.js.map +7 -0
  73. package/packages/gui/node_modules/.vite/deps/@xterm_addon-web-links.js +121 -0
  74. package/packages/gui/node_modules/.vite/deps/@xterm_addon-web-links.js.map +7 -0
  75. package/packages/gui/node_modules/.vite/deps/@xterm_xterm.js +9237 -0
  76. package/packages/gui/node_modules/.vite/deps/@xterm_xterm.js.map +7 -0
  77. package/packages/gui/node_modules/.vite/deps/@xyflow_react.js +9934 -0
  78. package/packages/gui/node_modules/.vite/deps/@xyflow_react.js.map +7 -0
  79. package/packages/gui/node_modules/.vite/deps/_metadata.json +184 -0
  80. package/packages/gui/node_modules/.vite/deps/chunk-3EE34IFC.js +5169 -0
  81. package/packages/gui/node_modules/.vite/deps/chunk-3EE34IFC.js.map +7 -0
  82. package/packages/gui/node_modules/.vite/deps/chunk-3IB5EUP7.js +2000 -0
  83. package/packages/gui/node_modules/.vite/deps/chunk-3IB5EUP7.js.map +7 -0
  84. package/packages/gui/node_modules/.vite/deps/chunk-3LBP22MX.js +1115 -0
  85. package/packages/gui/node_modules/.vite/deps/chunk-3LBP22MX.js.map +7 -0
  86. package/packages/gui/node_modules/.vite/deps/chunk-3Q7HT7ZF.js +701 -0
  87. package/packages/gui/node_modules/.vite/deps/chunk-3Q7HT7ZF.js.map +7 -0
  88. package/packages/gui/node_modules/.vite/deps/chunk-44CLUOQE.js +1776 -0
  89. package/packages/gui/node_modules/.vite/deps/chunk-44CLUOQE.js.map +7 -0
  90. package/packages/gui/node_modules/.vite/deps/chunk-5RZAEUNX.js +280 -0
  91. package/packages/gui/node_modules/.vite/deps/chunk-5RZAEUNX.js.map +7 -0
  92. package/packages/gui/node_modules/.vite/deps/chunk-5WRI5ZAA.js +30 -0
  93. package/packages/gui/node_modules/.vite/deps/chunk-5WRI5ZAA.js.map +7 -0
  94. package/packages/gui/node_modules/.vite/deps/chunk-7FYDPZIO.js +1004 -0
  95. package/packages/gui/node_modules/.vite/deps/chunk-7FYDPZIO.js.map +7 -0
  96. package/packages/gui/node_modules/.vite/deps/chunk-BX6POZPY.js +292 -0
  97. package/packages/gui/node_modules/.vite/deps/chunk-BX6POZPY.js.map +7 -0
  98. package/packages/gui/node_modules/.vite/deps/chunk-HVFOBSCQ.js +1062 -0
  99. package/packages/gui/node_modules/.vite/deps/chunk-HVFOBSCQ.js.map +7 -0
  100. package/packages/gui/node_modules/.vite/deps/chunk-RE2FU7ZU.js +10985 -0
  101. package/packages/gui/node_modules/.vite/deps/chunk-RE2FU7ZU.js.map +7 -0
  102. package/packages/gui/node_modules/.vite/deps/chunk-YYJMNVCJ.js +3459 -0
  103. package/packages/gui/node_modules/.vite/deps/chunk-YYJMNVCJ.js.map +7 -0
  104. package/packages/gui/node_modules/.vite/deps/package.json +3 -0
  105. package/packages/gui/node_modules/.vite/deps/react-dom.js +6 -0
  106. package/packages/gui/node_modules/.vite/deps/react-dom.js.map +7 -0
  107. package/packages/gui/node_modules/.vite/deps/react-dom_client.js +20217 -0
  108. package/packages/gui/node_modules/.vite/deps/react-dom_client.js.map +7 -0
  109. package/packages/gui/node_modules/.vite/deps/react.js +5 -0
  110. package/packages/gui/node_modules/.vite/deps/react.js.map +7 -0
  111. package/packages/gui/node_modules/.vite/deps/react_jsx-dev-runtime.js +278 -0
  112. package/packages/gui/node_modules/.vite/deps/react_jsx-dev-runtime.js.map +7 -0
  113. package/packages/gui/node_modules/.vite/deps/react_jsx-runtime.js +6 -0
  114. package/packages/gui/node_modules/.vite/deps/react_jsx-runtime.js.map +7 -0
  115. package/packages/gui/node_modules/.vite/deps/zustand.js +56 -0
  116. package/packages/gui/node_modules/.vite/deps/zustand.js.map +7 -0
  117. package/packages/gui/package.json +5 -4
  118. package/packages/gui/src/App.jsx +149 -76
  119. package/packages/gui/src/components/AgentActions.jsx +130 -1
  120. package/packages/gui/src/components/AgentChat.jsx +47 -7
  121. package/packages/gui/src/components/AgentNode.jsx +13 -83
  122. package/packages/gui/src/components/SpawnPanel.jsx +918 -580
  123. package/packages/gui/src/stores/groove.js +31 -2
  124. package/packages/gui/src/views/AgentTree.jsx +133 -67
  125. package/packages/gui/src/views/FileEditor.jsx +85 -1
  126. package/packages/gui/src/views/IntegrationsStore.jsx +121 -44
  127. package/docs/FILE-EDITOR-PLAN.md +0 -253
  128. package/docs/GUI_DESIGN_SPEC.md +0 -402
  129. package/docs/SKILLS-API-SPEC.md +0 -277
  130. package/node_modules/@groove-dev/gui/dist/assets/index-D5dtDQf0.js +0 -156
  131. package/packages/gui/dist/assets/index-D5dtDQf0.js +0 -156
@@ -0,0 +1,1776 @@
1
+ import {
2
+ DefaultBufferLength,
3
+ IterMode,
4
+ NodeProp,
5
+ NodeSet,
6
+ NodeType,
7
+ Parser,
8
+ Tree
9
+ } from "./chunk-3EE34IFC.js";
10
+
11
+ // ../../node_modules/@lezer/lr/dist/index.js
12
+ var Stack = class _Stack {
13
+ /**
14
+ @internal
15
+ */
16
+ constructor(p, stack, state, reducePos, pos, score, buffer, bufferBase, curContext, lookAhead = 0, parent) {
17
+ this.p = p;
18
+ this.stack = stack;
19
+ this.state = state;
20
+ this.reducePos = reducePos;
21
+ this.pos = pos;
22
+ this.score = score;
23
+ this.buffer = buffer;
24
+ this.bufferBase = bufferBase;
25
+ this.curContext = curContext;
26
+ this.lookAhead = lookAhead;
27
+ this.parent = parent;
28
+ }
29
+ /**
30
+ @internal
31
+ */
32
+ toString() {
33
+ return `[${this.stack.filter((_, i) => i % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`;
34
+ }
35
+ // Start an empty stack
36
+ /**
37
+ @internal
38
+ */
39
+ static start(p, state, pos = 0) {
40
+ let cx = p.parser.context;
41
+ return new _Stack(p, [], state, pos, pos, 0, [], 0, cx ? new StackContext(cx, cx.start) : null, 0, null);
42
+ }
43
+ /**
44
+ The stack's current [context](#lr.ContextTracker) value, if
45
+ any. Its type will depend on the context tracker's type
46
+ parameter, or it will be `null` if there is no context
47
+ tracker.
48
+ */
49
+ get context() {
50
+ return this.curContext ? this.curContext.context : null;
51
+ }
52
+ // Push a state onto the stack, tracking its start position as well
53
+ // as the buffer base at that point.
54
+ /**
55
+ @internal
56
+ */
57
+ pushState(state, start) {
58
+ this.stack.push(this.state, start, this.bufferBase + this.buffer.length);
59
+ this.state = state;
60
+ }
61
+ // Apply a reduce action
62
+ /**
63
+ @internal
64
+ */
65
+ reduce(action) {
66
+ var _a;
67
+ let depth = action >> 19, type = action & 65535;
68
+ let { parser } = this.p;
69
+ let lookaheadRecord = this.reducePos < this.pos - 25 && this.setLookAhead(this.pos);
70
+ let dPrec = parser.dynamicPrecedence(type);
71
+ if (dPrec)
72
+ this.score += dPrec;
73
+ if (depth == 0) {
74
+ this.pushState(parser.getGoto(this.state, type, true), this.reducePos);
75
+ if (type < parser.minRepeatTerm)
76
+ this.storeNode(type, this.reducePos, this.reducePos, lookaheadRecord ? 8 : 4, true);
77
+ this.reduceContext(type, this.reducePos);
78
+ return;
79
+ }
80
+ let base = this.stack.length - (depth - 1) * 3 - (action & 262144 ? 6 : 0);
81
+ let start = base ? this.stack[base - 2] : this.p.ranges[0].from, size = this.reducePos - start;
82
+ if (size >= 2e3 && !((_a = this.p.parser.nodeSet.types[type]) === null || _a === void 0 ? void 0 : _a.isAnonymous)) {
83
+ if (start == this.p.lastBigReductionStart) {
84
+ this.p.bigReductionCount++;
85
+ this.p.lastBigReductionSize = size;
86
+ } else if (this.p.lastBigReductionSize < size) {
87
+ this.p.bigReductionCount = 1;
88
+ this.p.lastBigReductionStart = start;
89
+ this.p.lastBigReductionSize = size;
90
+ }
91
+ }
92
+ let bufferBase = base ? this.stack[base - 1] : 0, count = this.bufferBase + this.buffer.length - bufferBase;
93
+ if (type < parser.minRepeatTerm || action & 131072) {
94
+ let pos = parser.stateFlag(
95
+ this.state,
96
+ 1
97
+ /* StateFlag.Skipped */
98
+ ) ? this.pos : this.reducePos;
99
+ this.storeNode(type, start, pos, count + 4, true);
100
+ }
101
+ if (action & 262144) {
102
+ this.state = this.stack[base];
103
+ } else {
104
+ let baseStateID = this.stack[base - 3];
105
+ this.state = parser.getGoto(baseStateID, type, true);
106
+ }
107
+ while (this.stack.length > base)
108
+ this.stack.pop();
109
+ this.reduceContext(type, start);
110
+ }
111
+ // Shift a value into the buffer
112
+ /**
113
+ @internal
114
+ */
115
+ storeNode(term, start, end, size = 4, mustSink = false) {
116
+ if (term == 0 && (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) {
117
+ let cur = this, top = this.buffer.length;
118
+ if (top == 0 && cur.parent) {
119
+ top = cur.bufferBase - cur.parent.bufferBase;
120
+ cur = cur.parent;
121
+ }
122
+ if (top > 0 && cur.buffer[top - 4] == 0 && cur.buffer[top - 1] > -1) {
123
+ if (start == end)
124
+ return;
125
+ if (cur.buffer[top - 2] >= start) {
126
+ cur.buffer[top - 2] = end;
127
+ return;
128
+ }
129
+ }
130
+ }
131
+ if (!mustSink || this.pos == end) {
132
+ this.buffer.push(term, start, end, size);
133
+ } else {
134
+ let index = this.buffer.length;
135
+ if (index > 0 && (this.buffer[index - 4] != 0 || this.buffer[index - 1] < 0)) {
136
+ let mustMove = false;
137
+ for (let scan = index; scan > 0 && this.buffer[scan - 2] > end; scan -= 4) {
138
+ if (this.buffer[scan - 1] >= 0) {
139
+ mustMove = true;
140
+ break;
141
+ }
142
+ }
143
+ if (mustMove)
144
+ while (index > 0 && this.buffer[index - 2] > end) {
145
+ this.buffer[index] = this.buffer[index - 4];
146
+ this.buffer[index + 1] = this.buffer[index - 3];
147
+ this.buffer[index + 2] = this.buffer[index - 2];
148
+ this.buffer[index + 3] = this.buffer[index - 1];
149
+ index -= 4;
150
+ if (size > 4)
151
+ size -= 4;
152
+ }
153
+ }
154
+ this.buffer[index] = term;
155
+ this.buffer[index + 1] = start;
156
+ this.buffer[index + 2] = end;
157
+ this.buffer[index + 3] = size;
158
+ }
159
+ }
160
+ // Apply a shift action
161
+ /**
162
+ @internal
163
+ */
164
+ shift(action, type, start, end) {
165
+ if (action & 131072) {
166
+ this.pushState(action & 65535, this.pos);
167
+ } else if ((action & 262144) == 0) {
168
+ let nextState = action, { parser } = this.p;
169
+ this.pos = end;
170
+ let skipped = parser.stateFlag(
171
+ nextState,
172
+ 1
173
+ /* StateFlag.Skipped */
174
+ );
175
+ if (!skipped && (end > start || type <= parser.maxNode))
176
+ this.reducePos = end;
177
+ this.pushState(nextState, skipped ? start : Math.min(start, this.reducePos));
178
+ this.shiftContext(type, start);
179
+ if (type <= parser.maxNode)
180
+ this.buffer.push(type, start, end, 4);
181
+ } else {
182
+ this.pos = end;
183
+ this.shiftContext(type, start);
184
+ if (type <= this.p.parser.maxNode)
185
+ this.buffer.push(type, start, end, 4);
186
+ }
187
+ }
188
+ // Apply an action
189
+ /**
190
+ @internal
191
+ */
192
+ apply(action, next, nextStart, nextEnd) {
193
+ if (action & 65536)
194
+ this.reduce(action);
195
+ else
196
+ this.shift(action, next, nextStart, nextEnd);
197
+ }
198
+ // Add a prebuilt (reused) node into the buffer.
199
+ /**
200
+ @internal
201
+ */
202
+ useNode(value, next) {
203
+ let index = this.p.reused.length - 1;
204
+ if (index < 0 || this.p.reused[index] != value) {
205
+ this.p.reused.push(value);
206
+ index++;
207
+ }
208
+ let start = this.pos;
209
+ this.reducePos = this.pos = start + value.length;
210
+ this.pushState(next, start);
211
+ this.buffer.push(
212
+ index,
213
+ start,
214
+ this.reducePos,
215
+ -1
216
+ /* size == -1 means this is a reused value */
217
+ );
218
+ if (this.curContext)
219
+ this.updateContext(this.curContext.tracker.reuse(this.curContext.context, value, this, this.p.stream.reset(this.pos - value.length)));
220
+ }
221
+ // Split the stack. Due to the buffer sharing and the fact
222
+ // that `this.stack` tends to stay quite shallow, this isn't very
223
+ // expensive.
224
+ /**
225
+ @internal
226
+ */
227
+ split() {
228
+ let parent = this;
229
+ let off = parent.buffer.length;
230
+ while (off > 0 && parent.buffer[off - 2] > parent.reducePos)
231
+ off -= 4;
232
+ let buffer = parent.buffer.slice(off), base = parent.bufferBase + off;
233
+ while (parent && base == parent.bufferBase)
234
+ parent = parent.parent;
235
+ return new _Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, buffer, base, this.curContext, this.lookAhead, parent);
236
+ }
237
+ // Try to recover from an error by 'deleting' (ignoring) one token.
238
+ /**
239
+ @internal
240
+ */
241
+ recoverByDelete(next, nextEnd) {
242
+ let isNode = next <= this.p.parser.maxNode;
243
+ if (isNode)
244
+ this.storeNode(next, this.pos, nextEnd, 4);
245
+ this.storeNode(0, this.pos, nextEnd, isNode ? 8 : 4);
246
+ this.pos = this.reducePos = nextEnd;
247
+ this.score -= 190;
248
+ }
249
+ /**
250
+ Check if the given term would be able to be shifted (optionally
251
+ after some reductions) on this stack. This can be useful for
252
+ external tokenizers that want to make sure they only provide a
253
+ given token when it applies.
254
+ */
255
+ canShift(term) {
256
+ for (let sim = new SimulatedStack(this); ; ) {
257
+ let action = this.p.parser.stateSlot(
258
+ sim.state,
259
+ 4
260
+ /* ParseState.DefaultReduce */
261
+ ) || this.p.parser.hasAction(sim.state, term);
262
+ if (action == 0)
263
+ return false;
264
+ if ((action & 65536) == 0)
265
+ return true;
266
+ sim.reduce(action);
267
+ }
268
+ }
269
+ // Apply up to Recover.MaxNext recovery actions that conceptually
270
+ // inserts some missing token or rule.
271
+ /**
272
+ @internal
273
+ */
274
+ recoverByInsert(next) {
275
+ if (this.stack.length >= 300)
276
+ return [];
277
+ let nextStates = this.p.parser.nextStates(this.state);
278
+ if (nextStates.length > 4 << 1 || this.stack.length >= 120) {
279
+ let best = [];
280
+ for (let i = 0, s; i < nextStates.length; i += 2) {
281
+ if ((s = nextStates[i + 1]) != this.state && this.p.parser.hasAction(s, next))
282
+ best.push(nextStates[i], s);
283
+ }
284
+ if (this.stack.length < 120)
285
+ for (let i = 0; best.length < 4 << 1 && i < nextStates.length; i += 2) {
286
+ let s = nextStates[i + 1];
287
+ if (!best.some((v, i2) => i2 & 1 && v == s))
288
+ best.push(nextStates[i], s);
289
+ }
290
+ nextStates = best;
291
+ }
292
+ let result = [];
293
+ for (let i = 0; i < nextStates.length && result.length < 4; i += 2) {
294
+ let s = nextStates[i + 1];
295
+ if (s == this.state)
296
+ continue;
297
+ let stack = this.split();
298
+ stack.pushState(s, this.pos);
299
+ stack.storeNode(0, stack.pos, stack.pos, 4, true);
300
+ stack.shiftContext(nextStates[i], this.pos);
301
+ stack.reducePos = this.pos;
302
+ stack.score -= 200;
303
+ result.push(stack);
304
+ }
305
+ return result;
306
+ }
307
+ // Force a reduce, if possible. Return false if that can't
308
+ // be done.
309
+ /**
310
+ @internal
311
+ */
312
+ forceReduce() {
313
+ let { parser } = this.p;
314
+ let reduce = parser.stateSlot(
315
+ this.state,
316
+ 5
317
+ /* ParseState.ForcedReduce */
318
+ );
319
+ if ((reduce & 65536) == 0)
320
+ return false;
321
+ if (!parser.validAction(this.state, reduce)) {
322
+ let depth = reduce >> 19, term = reduce & 65535;
323
+ let target = this.stack.length - depth * 3;
324
+ if (target < 0 || parser.getGoto(this.stack[target], term, false) < 0) {
325
+ let backup = this.findForcedReduction();
326
+ if (backup == null)
327
+ return false;
328
+ reduce = backup;
329
+ }
330
+ this.storeNode(0, this.pos, this.pos, 4, true);
331
+ this.score -= 100;
332
+ }
333
+ this.reducePos = this.pos;
334
+ this.reduce(reduce);
335
+ return true;
336
+ }
337
+ /**
338
+ Try to scan through the automaton to find some kind of reduction
339
+ that can be applied. Used when the regular ForcedReduce field
340
+ isn't a valid action. @internal
341
+ */
342
+ findForcedReduction() {
343
+ let { parser } = this.p, seen = [];
344
+ let explore = (state, depth) => {
345
+ if (seen.includes(state))
346
+ return;
347
+ seen.push(state);
348
+ return parser.allActions(state, (action) => {
349
+ if (action & (262144 | 131072)) ;
350
+ else if (action & 65536) {
351
+ let rDepth = (action >> 19) - depth;
352
+ if (rDepth > 1) {
353
+ let term = action & 65535, target = this.stack.length - rDepth * 3;
354
+ if (target >= 0 && parser.getGoto(this.stack[target], term, false) >= 0)
355
+ return rDepth << 19 | 65536 | term;
356
+ }
357
+ } else {
358
+ let found = explore(action, depth + 1);
359
+ if (found != null)
360
+ return found;
361
+ }
362
+ });
363
+ };
364
+ return explore(this.state, 0);
365
+ }
366
+ /**
367
+ @internal
368
+ */
369
+ forceAll() {
370
+ while (!this.p.parser.stateFlag(
371
+ this.state,
372
+ 2
373
+ /* StateFlag.Accepting */
374
+ )) {
375
+ if (!this.forceReduce()) {
376
+ this.storeNode(0, this.pos, this.pos, 4, true);
377
+ break;
378
+ }
379
+ }
380
+ return this;
381
+ }
382
+ /**
383
+ Check whether this state has no further actions (assumed to be a direct descendant of the
384
+ top state, since any other states must be able to continue
385
+ somehow). @internal
386
+ */
387
+ get deadEnd() {
388
+ if (this.stack.length != 3)
389
+ return false;
390
+ let { parser } = this.p;
391
+ return parser.data[parser.stateSlot(
392
+ this.state,
393
+ 1
394
+ /* ParseState.Actions */
395
+ )] == 65535 && !parser.stateSlot(
396
+ this.state,
397
+ 4
398
+ /* ParseState.DefaultReduce */
399
+ );
400
+ }
401
+ /**
402
+ Restart the stack (put it back in its start state). Only safe
403
+ when this.stack.length == 3 (state is directly below the top
404
+ state). @internal
405
+ */
406
+ restart() {
407
+ this.storeNode(0, this.pos, this.pos, 4, true);
408
+ this.state = this.stack[0];
409
+ this.stack.length = 0;
410
+ }
411
+ /**
412
+ @internal
413
+ */
414
+ sameState(other) {
415
+ if (this.state != other.state || this.stack.length != other.stack.length)
416
+ return false;
417
+ for (let i = 0; i < this.stack.length; i += 3)
418
+ if (this.stack[i] != other.stack[i])
419
+ return false;
420
+ return true;
421
+ }
422
+ /**
423
+ Get the parser used by this stack.
424
+ */
425
+ get parser() {
426
+ return this.p.parser;
427
+ }
428
+ /**
429
+ Test whether a given dialect (by numeric ID, as exported from
430
+ the terms file) is enabled.
431
+ */
432
+ dialectEnabled(dialectID) {
433
+ return this.p.parser.dialect.flags[dialectID];
434
+ }
435
+ shiftContext(term, start) {
436
+ if (this.curContext)
437
+ this.updateContext(this.curContext.tracker.shift(this.curContext.context, term, this, this.p.stream.reset(start)));
438
+ }
439
+ reduceContext(term, start) {
440
+ if (this.curContext)
441
+ this.updateContext(this.curContext.tracker.reduce(this.curContext.context, term, this, this.p.stream.reset(start)));
442
+ }
443
+ /**
444
+ @internal
445
+ */
446
+ emitContext() {
447
+ let last = this.buffer.length - 1;
448
+ if (last < 0 || this.buffer[last] != -3)
449
+ this.buffer.push(this.curContext.hash, this.pos, this.pos, -3);
450
+ }
451
+ /**
452
+ @internal
453
+ */
454
+ emitLookAhead() {
455
+ let last = this.buffer.length - 1;
456
+ if (last < 0 || this.buffer[last] != -4)
457
+ this.buffer.push(this.lookAhead, this.pos, this.pos, -4);
458
+ }
459
+ updateContext(context) {
460
+ if (context != this.curContext.context) {
461
+ let newCx = new StackContext(this.curContext.tracker, context);
462
+ if (newCx.hash != this.curContext.hash)
463
+ this.emitContext();
464
+ this.curContext = newCx;
465
+ }
466
+ }
467
+ /**
468
+ @internal
469
+ */
470
+ setLookAhead(lookAhead) {
471
+ if (lookAhead <= this.lookAhead)
472
+ return false;
473
+ this.emitLookAhead();
474
+ this.lookAhead = lookAhead;
475
+ return true;
476
+ }
477
+ /**
478
+ @internal
479
+ */
480
+ close() {
481
+ if (this.curContext && this.curContext.tracker.strict)
482
+ this.emitContext();
483
+ if (this.lookAhead > 0)
484
+ this.emitLookAhead();
485
+ }
486
+ };
487
+ var StackContext = class {
488
+ constructor(tracker, context) {
489
+ this.tracker = tracker;
490
+ this.context = context;
491
+ this.hash = tracker.strict ? tracker.hash(context) : 0;
492
+ }
493
+ };
494
+ var SimulatedStack = class {
495
+ constructor(start) {
496
+ this.start = start;
497
+ this.state = start.state;
498
+ this.stack = start.stack;
499
+ this.base = this.stack.length;
500
+ }
501
+ reduce(action) {
502
+ let term = action & 65535, depth = action >> 19;
503
+ if (depth == 0) {
504
+ if (this.stack == this.start.stack)
505
+ this.stack = this.stack.slice();
506
+ this.stack.push(this.state, 0, 0);
507
+ this.base += 3;
508
+ } else {
509
+ this.base -= (depth - 1) * 3;
510
+ }
511
+ let goto = this.start.p.parser.getGoto(this.stack[this.base - 3], term, true);
512
+ this.state = goto;
513
+ }
514
+ };
515
+ var StackBufferCursor = class _StackBufferCursor {
516
+ constructor(stack, pos, index) {
517
+ this.stack = stack;
518
+ this.pos = pos;
519
+ this.index = index;
520
+ this.buffer = stack.buffer;
521
+ if (this.index == 0)
522
+ this.maybeNext();
523
+ }
524
+ static create(stack, pos = stack.bufferBase + stack.buffer.length) {
525
+ return new _StackBufferCursor(stack, pos, pos - stack.bufferBase);
526
+ }
527
+ maybeNext() {
528
+ let next = this.stack.parent;
529
+ if (next != null) {
530
+ this.index = this.stack.bufferBase - next.bufferBase;
531
+ this.stack = next;
532
+ this.buffer = next.buffer;
533
+ }
534
+ }
535
+ get id() {
536
+ return this.buffer[this.index - 4];
537
+ }
538
+ get start() {
539
+ return this.buffer[this.index - 3];
540
+ }
541
+ get end() {
542
+ return this.buffer[this.index - 2];
543
+ }
544
+ get size() {
545
+ return this.buffer[this.index - 1];
546
+ }
547
+ next() {
548
+ this.index -= 4;
549
+ this.pos -= 4;
550
+ if (this.index == 0)
551
+ this.maybeNext();
552
+ }
553
+ fork() {
554
+ return new _StackBufferCursor(this.stack, this.pos, this.index);
555
+ }
556
+ };
557
+ function decodeArray(input, Type = Uint16Array) {
558
+ if (typeof input != "string")
559
+ return input;
560
+ let array = null;
561
+ for (let pos = 0, out = 0; pos < input.length; ) {
562
+ let value = 0;
563
+ for (; ; ) {
564
+ let next = input.charCodeAt(pos++), stop = false;
565
+ if (next == 126) {
566
+ value = 65535;
567
+ break;
568
+ }
569
+ if (next >= 92)
570
+ next--;
571
+ if (next >= 34)
572
+ next--;
573
+ let digit = next - 32;
574
+ if (digit >= 46) {
575
+ digit -= 46;
576
+ stop = true;
577
+ }
578
+ value += digit;
579
+ if (stop)
580
+ break;
581
+ value *= 46;
582
+ }
583
+ if (array)
584
+ array[out++] = value;
585
+ else
586
+ array = new Type(value);
587
+ }
588
+ return array;
589
+ }
590
+ var CachedToken = class {
591
+ constructor() {
592
+ this.start = -1;
593
+ this.value = -1;
594
+ this.end = -1;
595
+ this.extended = -1;
596
+ this.lookAhead = 0;
597
+ this.mask = 0;
598
+ this.context = 0;
599
+ }
600
+ };
601
+ var nullToken = new CachedToken();
602
+ var InputStream = class {
603
+ /**
604
+ @internal
605
+ */
606
+ constructor(input, ranges) {
607
+ this.input = input;
608
+ this.ranges = ranges;
609
+ this.chunk = "";
610
+ this.chunkOff = 0;
611
+ this.chunk2 = "";
612
+ this.chunk2Pos = 0;
613
+ this.next = -1;
614
+ this.token = nullToken;
615
+ this.rangeIndex = 0;
616
+ this.pos = this.chunkPos = ranges[0].from;
617
+ this.range = ranges[0];
618
+ this.end = ranges[ranges.length - 1].to;
619
+ this.readNext();
620
+ }
621
+ /**
622
+ @internal
623
+ */
624
+ resolveOffset(offset, assoc) {
625
+ let range = this.range, index = this.rangeIndex;
626
+ let pos = this.pos + offset;
627
+ while (pos < range.from) {
628
+ if (!index)
629
+ return null;
630
+ let next = this.ranges[--index];
631
+ pos -= range.from - next.to;
632
+ range = next;
633
+ }
634
+ while (assoc < 0 ? pos > range.to : pos >= range.to) {
635
+ if (index == this.ranges.length - 1)
636
+ return null;
637
+ let next = this.ranges[++index];
638
+ pos += next.from - range.to;
639
+ range = next;
640
+ }
641
+ return pos;
642
+ }
643
+ /**
644
+ @internal
645
+ */
646
+ clipPos(pos) {
647
+ if (pos >= this.range.from && pos < this.range.to)
648
+ return pos;
649
+ for (let range of this.ranges)
650
+ if (range.to > pos)
651
+ return Math.max(pos, range.from);
652
+ return this.end;
653
+ }
654
+ /**
655
+ Look at a code unit near the stream position. `.peek(0)` equals
656
+ `.next`, `.peek(-1)` gives you the previous character, and so
657
+ on.
658
+
659
+ Note that looking around during tokenizing creates dependencies
660
+ on potentially far-away content, which may reduce the
661
+ effectiveness incremental parsing—when looking forward—or even
662
+ cause invalid reparses when looking backward more than 25 code
663
+ units, since the library does not track lookbehind.
664
+ */
665
+ peek(offset) {
666
+ let idx = this.chunkOff + offset, pos, result;
667
+ if (idx >= 0 && idx < this.chunk.length) {
668
+ pos = this.pos + offset;
669
+ result = this.chunk.charCodeAt(idx);
670
+ } else {
671
+ let resolved = this.resolveOffset(offset, 1);
672
+ if (resolved == null)
673
+ return -1;
674
+ pos = resolved;
675
+ if (pos >= this.chunk2Pos && pos < this.chunk2Pos + this.chunk2.length) {
676
+ result = this.chunk2.charCodeAt(pos - this.chunk2Pos);
677
+ } else {
678
+ let i = this.rangeIndex, range = this.range;
679
+ while (range.to <= pos)
680
+ range = this.ranges[++i];
681
+ this.chunk2 = this.input.chunk(this.chunk2Pos = pos);
682
+ if (pos + this.chunk2.length > range.to)
683
+ this.chunk2 = this.chunk2.slice(0, range.to - pos);
684
+ result = this.chunk2.charCodeAt(0);
685
+ }
686
+ }
687
+ if (pos >= this.token.lookAhead)
688
+ this.token.lookAhead = pos + 1;
689
+ return result;
690
+ }
691
+ /**
692
+ Accept a token. By default, the end of the token is set to the
693
+ current stream position, but you can pass an offset (relative to
694
+ the stream position) to change that.
695
+ */
696
+ acceptToken(token, endOffset = 0) {
697
+ let end = endOffset ? this.resolveOffset(endOffset, -1) : this.pos;
698
+ if (end == null || end < this.token.start)
699
+ throw new RangeError("Token end out of bounds");
700
+ this.token.value = token;
701
+ this.token.end = end;
702
+ }
703
+ /**
704
+ Accept a token ending at a specific given position.
705
+ */
706
+ acceptTokenTo(token, endPos) {
707
+ this.token.value = token;
708
+ this.token.end = endPos;
709
+ }
710
+ getChunk() {
711
+ if (this.pos >= this.chunk2Pos && this.pos < this.chunk2Pos + this.chunk2.length) {
712
+ let { chunk, chunkPos } = this;
713
+ this.chunk = this.chunk2;
714
+ this.chunkPos = this.chunk2Pos;
715
+ this.chunk2 = chunk;
716
+ this.chunk2Pos = chunkPos;
717
+ this.chunkOff = this.pos - this.chunkPos;
718
+ } else {
719
+ this.chunk2 = this.chunk;
720
+ this.chunk2Pos = this.chunkPos;
721
+ let nextChunk = this.input.chunk(this.pos);
722
+ let end = this.pos + nextChunk.length;
723
+ this.chunk = end > this.range.to ? nextChunk.slice(0, this.range.to - this.pos) : nextChunk;
724
+ this.chunkPos = this.pos;
725
+ this.chunkOff = 0;
726
+ }
727
+ }
728
+ readNext() {
729
+ if (this.chunkOff >= this.chunk.length) {
730
+ this.getChunk();
731
+ if (this.chunkOff == this.chunk.length)
732
+ return this.next = -1;
733
+ }
734
+ return this.next = this.chunk.charCodeAt(this.chunkOff);
735
+ }
736
+ /**
737
+ Move the stream forward N (defaults to 1) code units. Returns
738
+ the new value of [`next`](#lr.InputStream.next).
739
+ */
740
+ advance(n = 1) {
741
+ this.chunkOff += n;
742
+ while (this.pos + n >= this.range.to) {
743
+ if (this.rangeIndex == this.ranges.length - 1)
744
+ return this.setDone();
745
+ n -= this.range.to - this.pos;
746
+ this.range = this.ranges[++this.rangeIndex];
747
+ this.pos = this.range.from;
748
+ }
749
+ this.pos += n;
750
+ if (this.pos >= this.token.lookAhead)
751
+ this.token.lookAhead = this.pos + 1;
752
+ return this.readNext();
753
+ }
754
+ setDone() {
755
+ this.pos = this.chunkPos = this.end;
756
+ this.range = this.ranges[this.rangeIndex = this.ranges.length - 1];
757
+ this.chunk = "";
758
+ return this.next = -1;
759
+ }
760
+ /**
761
+ @internal
762
+ */
763
+ reset(pos, token) {
764
+ if (token) {
765
+ this.token = token;
766
+ token.start = pos;
767
+ token.lookAhead = pos + 1;
768
+ token.value = token.extended = -1;
769
+ } else {
770
+ this.token = nullToken;
771
+ }
772
+ if (this.pos != pos) {
773
+ this.pos = pos;
774
+ if (pos == this.end) {
775
+ this.setDone();
776
+ return this;
777
+ }
778
+ while (pos < this.range.from)
779
+ this.range = this.ranges[--this.rangeIndex];
780
+ while (pos >= this.range.to)
781
+ this.range = this.ranges[++this.rangeIndex];
782
+ if (pos >= this.chunkPos && pos < this.chunkPos + this.chunk.length) {
783
+ this.chunkOff = pos - this.chunkPos;
784
+ } else {
785
+ this.chunk = "";
786
+ this.chunkOff = 0;
787
+ }
788
+ this.readNext();
789
+ }
790
+ return this;
791
+ }
792
+ /**
793
+ @internal
794
+ */
795
+ read(from, to) {
796
+ if (from >= this.chunkPos && to <= this.chunkPos + this.chunk.length)
797
+ return this.chunk.slice(from - this.chunkPos, to - this.chunkPos);
798
+ if (from >= this.chunk2Pos && to <= this.chunk2Pos + this.chunk2.length)
799
+ return this.chunk2.slice(from - this.chunk2Pos, to - this.chunk2Pos);
800
+ if (from >= this.range.from && to <= this.range.to)
801
+ return this.input.read(from, to);
802
+ let result = "";
803
+ for (let r of this.ranges) {
804
+ if (r.from >= to)
805
+ break;
806
+ if (r.to > from)
807
+ result += this.input.read(Math.max(r.from, from), Math.min(r.to, to));
808
+ }
809
+ return result;
810
+ }
811
+ };
812
+ var TokenGroup = class {
813
+ constructor(data, id2) {
814
+ this.data = data;
815
+ this.id = id2;
816
+ }
817
+ token(input, stack) {
818
+ let { parser } = stack.p;
819
+ readToken(this.data, input, stack, this.id, parser.data, parser.tokenPrecTable);
820
+ }
821
+ };
822
+ TokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
823
+ var LocalTokenGroup = class {
824
+ constructor(data, precTable, elseToken) {
825
+ this.precTable = precTable;
826
+ this.elseToken = elseToken;
827
+ this.data = typeof data == "string" ? decodeArray(data) : data;
828
+ }
829
+ token(input, stack) {
830
+ let start = input.pos, skipped = 0;
831
+ for (; ; ) {
832
+ let atEof = input.next < 0, nextPos = input.resolveOffset(1, 1);
833
+ readToken(this.data, input, stack, 0, this.data, this.precTable);
834
+ if (input.token.value > -1)
835
+ break;
836
+ if (this.elseToken == null)
837
+ return;
838
+ if (!atEof)
839
+ skipped++;
840
+ if (nextPos == null)
841
+ break;
842
+ input.reset(nextPos, input.token);
843
+ }
844
+ if (skipped) {
845
+ input.reset(start, input.token);
846
+ input.acceptToken(this.elseToken, skipped);
847
+ }
848
+ }
849
+ };
850
+ LocalTokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
851
+ var ExternalTokenizer = class {
852
+ /**
853
+ Create a tokenizer. The first argument is the function that,
854
+ given an input stream, scans for the types of tokens it
855
+ recognizes at the stream's position, and calls
856
+ [`acceptToken`](#lr.InputStream.acceptToken) when it finds
857
+ one.
858
+ */
859
+ constructor(token, options = {}) {
860
+ this.token = token;
861
+ this.contextual = !!options.contextual;
862
+ this.fallback = !!options.fallback;
863
+ this.extend = !!options.extend;
864
+ }
865
+ };
866
+ function readToken(data, input, stack, group, precTable, precOffset) {
867
+ let state = 0, groupMask = 1 << group, { dialect } = stack.p.parser;
868
+ scan: for (; ; ) {
869
+ if ((groupMask & data[state]) == 0)
870
+ break;
871
+ let accEnd = data[state + 1];
872
+ for (let i = state + 3; i < accEnd; i += 2)
873
+ if ((data[i + 1] & groupMask) > 0) {
874
+ let term = data[i];
875
+ if (dialect.allows(term) && (input.token.value == -1 || input.token.value == term || overrides(term, input.token.value, precTable, precOffset))) {
876
+ input.acceptToken(term);
877
+ break;
878
+ }
879
+ }
880
+ let next = input.next, low = 0, high = data[state + 2];
881
+ if (input.next < 0 && high > low && data[accEnd + high * 3 - 3] == 65535) {
882
+ state = data[accEnd + high * 3 - 1];
883
+ continue scan;
884
+ }
885
+ for (; low < high; ) {
886
+ let mid = low + high >> 1;
887
+ let index = accEnd + mid + (mid << 1);
888
+ let from = data[index], to = data[index + 1] || 65536;
889
+ if (next < from)
890
+ high = mid;
891
+ else if (next >= to)
892
+ low = mid + 1;
893
+ else {
894
+ state = data[index + 2];
895
+ input.advance();
896
+ continue scan;
897
+ }
898
+ }
899
+ break;
900
+ }
901
+ }
902
+ function findOffset(data, start, term) {
903
+ for (let i = start, next; (next = data[i]) != 65535; i++)
904
+ if (next == term)
905
+ return i - start;
906
+ return -1;
907
+ }
908
+ function overrides(token, prev, tableData, tableOffset) {
909
+ let iPrev = findOffset(tableData, tableOffset, prev);
910
+ return iPrev < 0 || findOffset(tableData, tableOffset, token) < iPrev;
911
+ }
912
+ var verbose = typeof process != "undefined" && process.env && /\bparse\b/.test(process.env.LOG);
913
+ var stackIDs = null;
914
+ function cutAt(tree, pos, side) {
915
+ let cursor = tree.cursor(IterMode.IncludeAnonymous);
916
+ cursor.moveTo(pos);
917
+ for (; ; ) {
918
+ if (!(side < 0 ? cursor.childBefore(pos) : cursor.childAfter(pos)))
919
+ for (; ; ) {
920
+ if ((side < 0 ? cursor.to < pos : cursor.from > pos) && !cursor.type.isError)
921
+ return side < 0 ? Math.max(0, Math.min(
922
+ cursor.to - 1,
923
+ pos - 25
924
+ /* Lookahead.Margin */
925
+ )) : Math.min(tree.length, Math.max(
926
+ cursor.from + 1,
927
+ pos + 25
928
+ /* Lookahead.Margin */
929
+ ));
930
+ if (side < 0 ? cursor.prevSibling() : cursor.nextSibling())
931
+ break;
932
+ if (!cursor.parent())
933
+ return side < 0 ? 0 : tree.length;
934
+ }
935
+ }
936
+ }
937
+ var FragmentCursor = class {
938
+ constructor(fragments, nodeSet) {
939
+ this.fragments = fragments;
940
+ this.nodeSet = nodeSet;
941
+ this.i = 0;
942
+ this.fragment = null;
943
+ this.safeFrom = -1;
944
+ this.safeTo = -1;
945
+ this.trees = [];
946
+ this.start = [];
947
+ this.index = [];
948
+ this.nextFragment();
949
+ }
950
+ nextFragment() {
951
+ let fr = this.fragment = this.i == this.fragments.length ? null : this.fragments[this.i++];
952
+ if (fr) {
953
+ this.safeFrom = fr.openStart ? cutAt(fr.tree, fr.from + fr.offset, 1) - fr.offset : fr.from;
954
+ this.safeTo = fr.openEnd ? cutAt(fr.tree, fr.to + fr.offset, -1) - fr.offset : fr.to;
955
+ while (this.trees.length) {
956
+ this.trees.pop();
957
+ this.start.pop();
958
+ this.index.pop();
959
+ }
960
+ this.trees.push(fr.tree);
961
+ this.start.push(-fr.offset);
962
+ this.index.push(0);
963
+ this.nextStart = this.safeFrom;
964
+ } else {
965
+ this.nextStart = 1e9;
966
+ }
967
+ }
968
+ // `pos` must be >= any previously given `pos` for this cursor
969
+ nodeAt(pos) {
970
+ if (pos < this.nextStart)
971
+ return null;
972
+ while (this.fragment && this.safeTo <= pos)
973
+ this.nextFragment();
974
+ if (!this.fragment)
975
+ return null;
976
+ for (; ; ) {
977
+ let last = this.trees.length - 1;
978
+ if (last < 0) {
979
+ this.nextFragment();
980
+ return null;
981
+ }
982
+ let top = this.trees[last], index = this.index[last];
983
+ if (index == top.children.length) {
984
+ this.trees.pop();
985
+ this.start.pop();
986
+ this.index.pop();
987
+ continue;
988
+ }
989
+ let next = top.children[index];
990
+ let start = this.start[last] + top.positions[index];
991
+ if (start > pos) {
992
+ this.nextStart = start;
993
+ return null;
994
+ }
995
+ if (next instanceof Tree) {
996
+ if (start == pos) {
997
+ if (start < this.safeFrom)
998
+ return null;
999
+ let end = start + next.length;
1000
+ if (end <= this.safeTo) {
1001
+ let lookAhead = next.prop(NodeProp.lookAhead);
1002
+ if (!lookAhead || end + lookAhead < this.fragment.to)
1003
+ return next;
1004
+ }
1005
+ }
1006
+ this.index[last]++;
1007
+ if (start + next.length >= Math.max(this.safeFrom, pos)) {
1008
+ this.trees.push(next);
1009
+ this.start.push(start);
1010
+ this.index.push(0);
1011
+ }
1012
+ } else {
1013
+ this.index[last]++;
1014
+ this.nextStart = start + next.length;
1015
+ }
1016
+ }
1017
+ }
1018
+ };
1019
+ var TokenCache = class {
1020
+ constructor(parser, stream) {
1021
+ this.stream = stream;
1022
+ this.tokens = [];
1023
+ this.mainToken = null;
1024
+ this.actions = [];
1025
+ this.tokens = parser.tokenizers.map((_) => new CachedToken());
1026
+ }
1027
+ getActions(stack) {
1028
+ let actionIndex = 0;
1029
+ let main = null;
1030
+ let { parser } = stack.p, { tokenizers } = parser;
1031
+ let mask = parser.stateSlot(
1032
+ stack.state,
1033
+ 3
1034
+ /* ParseState.TokenizerMask */
1035
+ );
1036
+ let context = stack.curContext ? stack.curContext.hash : 0;
1037
+ let lookAhead = 0;
1038
+ for (let i = 0; i < tokenizers.length; i++) {
1039
+ if ((1 << i & mask) == 0)
1040
+ continue;
1041
+ let tokenizer = tokenizers[i], token = this.tokens[i];
1042
+ if (main && !tokenizer.fallback)
1043
+ continue;
1044
+ if (tokenizer.contextual || token.start != stack.pos || token.mask != mask || token.context != context) {
1045
+ this.updateCachedToken(token, tokenizer, stack);
1046
+ token.mask = mask;
1047
+ token.context = context;
1048
+ }
1049
+ if (token.lookAhead > token.end + 25)
1050
+ lookAhead = Math.max(token.lookAhead, lookAhead);
1051
+ if (token.value != 0) {
1052
+ let startIndex = actionIndex;
1053
+ if (token.extended > -1)
1054
+ actionIndex = this.addActions(stack, token.extended, token.end, actionIndex);
1055
+ actionIndex = this.addActions(stack, token.value, token.end, actionIndex);
1056
+ if (!tokenizer.extend) {
1057
+ main = token;
1058
+ if (actionIndex > startIndex)
1059
+ break;
1060
+ }
1061
+ }
1062
+ }
1063
+ while (this.actions.length > actionIndex)
1064
+ this.actions.pop();
1065
+ if (lookAhead)
1066
+ stack.setLookAhead(lookAhead);
1067
+ if (!main && stack.pos == this.stream.end) {
1068
+ main = new CachedToken();
1069
+ main.value = stack.p.parser.eofTerm;
1070
+ main.start = main.end = stack.pos;
1071
+ actionIndex = this.addActions(stack, main.value, main.end, actionIndex);
1072
+ }
1073
+ this.mainToken = main;
1074
+ return this.actions;
1075
+ }
1076
+ getMainToken(stack) {
1077
+ if (this.mainToken)
1078
+ return this.mainToken;
1079
+ let main = new CachedToken(), { pos, p } = stack;
1080
+ main.start = pos;
1081
+ main.end = Math.min(pos + 1, p.stream.end);
1082
+ main.value = pos == p.stream.end ? p.parser.eofTerm : 0;
1083
+ return main;
1084
+ }
1085
+ updateCachedToken(token, tokenizer, stack) {
1086
+ let start = this.stream.clipPos(stack.pos);
1087
+ tokenizer.token(this.stream.reset(start, token), stack);
1088
+ if (token.value > -1) {
1089
+ let { parser } = stack.p;
1090
+ for (let i = 0; i < parser.specialized.length; i++)
1091
+ if (parser.specialized[i] == token.value) {
1092
+ let result = parser.specializers[i](this.stream.read(token.start, token.end), stack);
1093
+ if (result >= 0 && stack.p.parser.dialect.allows(result >> 1)) {
1094
+ if ((result & 1) == 0)
1095
+ token.value = result >> 1;
1096
+ else
1097
+ token.extended = result >> 1;
1098
+ break;
1099
+ }
1100
+ }
1101
+ } else {
1102
+ token.value = 0;
1103
+ token.end = this.stream.clipPos(start + 1);
1104
+ }
1105
+ }
1106
+ putAction(action, token, end, index) {
1107
+ for (let i = 0; i < index; i += 3)
1108
+ if (this.actions[i] == action)
1109
+ return index;
1110
+ this.actions[index++] = action;
1111
+ this.actions[index++] = token;
1112
+ this.actions[index++] = end;
1113
+ return index;
1114
+ }
1115
+ addActions(stack, token, end, index) {
1116
+ let { state } = stack, { parser } = stack.p, { data } = parser;
1117
+ for (let set = 0; set < 2; set++) {
1118
+ for (let i = parser.stateSlot(
1119
+ state,
1120
+ set ? 2 : 1
1121
+ /* ParseState.Actions */
1122
+ ); ; i += 3) {
1123
+ if (data[i] == 65535) {
1124
+ if (data[i + 1] == 1) {
1125
+ i = pair(data, i + 2);
1126
+ } else {
1127
+ if (index == 0 && data[i + 1] == 2)
1128
+ index = this.putAction(pair(data, i + 2), token, end, index);
1129
+ break;
1130
+ }
1131
+ }
1132
+ if (data[i] == token)
1133
+ index = this.putAction(pair(data, i + 1), token, end, index);
1134
+ }
1135
+ }
1136
+ return index;
1137
+ }
1138
+ };
1139
+ var Parse = class {
1140
+ constructor(parser, input, fragments, ranges) {
1141
+ this.parser = parser;
1142
+ this.input = input;
1143
+ this.ranges = ranges;
1144
+ this.recovering = 0;
1145
+ this.nextStackID = 9812;
1146
+ this.minStackPos = 0;
1147
+ this.reused = [];
1148
+ this.stoppedAt = null;
1149
+ this.lastBigReductionStart = -1;
1150
+ this.lastBigReductionSize = 0;
1151
+ this.bigReductionCount = 0;
1152
+ this.stream = new InputStream(input, ranges);
1153
+ this.tokens = new TokenCache(parser, this.stream);
1154
+ this.topTerm = parser.top[1];
1155
+ let { from } = ranges[0];
1156
+ this.stacks = [Stack.start(this, parser.top[0], from)];
1157
+ this.fragments = fragments.length && this.stream.end - from > parser.bufferLength * 4 ? new FragmentCursor(fragments, parser.nodeSet) : null;
1158
+ }
1159
+ get parsedPos() {
1160
+ return this.minStackPos;
1161
+ }
1162
+ // Move the parser forward. This will process all parse stacks at
1163
+ // `this.pos` and try to advance them to a further position. If no
1164
+ // stack for such a position is found, it'll start error-recovery.
1165
+ //
1166
+ // When the parse is finished, this will return a syntax tree. When
1167
+ // not, it returns `null`.
1168
+ advance() {
1169
+ let stacks = this.stacks, pos = this.minStackPos;
1170
+ let newStacks = this.stacks = [];
1171
+ let stopped, stoppedTokens;
1172
+ if (this.bigReductionCount > 300 && stacks.length == 1) {
1173
+ let [s] = stacks;
1174
+ while (s.forceReduce() && s.stack.length && s.stack[s.stack.length - 2] >= this.lastBigReductionStart) {
1175
+ }
1176
+ this.bigReductionCount = this.lastBigReductionSize = 0;
1177
+ }
1178
+ for (let i = 0; i < stacks.length; i++) {
1179
+ let stack = stacks[i];
1180
+ for (; ; ) {
1181
+ this.tokens.mainToken = null;
1182
+ if (stack.pos > pos) {
1183
+ newStacks.push(stack);
1184
+ } else if (this.advanceStack(stack, newStacks, stacks)) {
1185
+ continue;
1186
+ } else {
1187
+ if (!stopped) {
1188
+ stopped = [];
1189
+ stoppedTokens = [];
1190
+ }
1191
+ stopped.push(stack);
1192
+ let tok = this.tokens.getMainToken(stack);
1193
+ stoppedTokens.push(tok.value, tok.end);
1194
+ }
1195
+ break;
1196
+ }
1197
+ }
1198
+ if (!newStacks.length) {
1199
+ let finished = stopped && findFinished(stopped);
1200
+ if (finished) {
1201
+ if (verbose)
1202
+ console.log("Finish with " + this.stackID(finished));
1203
+ return this.stackToTree(finished);
1204
+ }
1205
+ if (this.parser.strict) {
1206
+ if (verbose && stopped)
1207
+ console.log("Stuck with token " + (this.tokens.mainToken ? this.parser.getName(this.tokens.mainToken.value) : "none"));
1208
+ throw new SyntaxError("No parse at " + pos);
1209
+ }
1210
+ if (!this.recovering)
1211
+ this.recovering = 5;
1212
+ }
1213
+ if (this.recovering && stopped) {
1214
+ let finished = this.stoppedAt != null && stopped[0].pos > this.stoppedAt ? stopped[0] : this.runRecovery(stopped, stoppedTokens, newStacks);
1215
+ if (finished) {
1216
+ if (verbose)
1217
+ console.log("Force-finish " + this.stackID(finished));
1218
+ return this.stackToTree(finished.forceAll());
1219
+ }
1220
+ }
1221
+ if (this.recovering) {
1222
+ let maxRemaining = this.recovering == 1 ? 1 : this.recovering * 3;
1223
+ if (newStacks.length > maxRemaining) {
1224
+ newStacks.sort((a, b) => b.score - a.score);
1225
+ while (newStacks.length > maxRemaining)
1226
+ newStacks.pop();
1227
+ }
1228
+ if (newStacks.some((s) => s.reducePos > pos))
1229
+ this.recovering--;
1230
+ } else if (newStacks.length > 1) {
1231
+ outer: for (let i = 0; i < newStacks.length - 1; i++) {
1232
+ let stack = newStacks[i];
1233
+ for (let j = i + 1; j < newStacks.length; j++) {
1234
+ let other = newStacks[j];
1235
+ if (stack.sameState(other) || stack.buffer.length > 500 && other.buffer.length > 500) {
1236
+ if ((stack.score - other.score || stack.buffer.length - other.buffer.length) > 0) {
1237
+ newStacks.splice(j--, 1);
1238
+ } else {
1239
+ newStacks.splice(i--, 1);
1240
+ continue outer;
1241
+ }
1242
+ }
1243
+ }
1244
+ }
1245
+ if (newStacks.length > 12) {
1246
+ newStacks.sort((a, b) => b.score - a.score);
1247
+ newStacks.splice(
1248
+ 12,
1249
+ newStacks.length - 12
1250
+ /* Rec.MaxStackCount */
1251
+ );
1252
+ }
1253
+ }
1254
+ this.minStackPos = newStacks[0].pos;
1255
+ for (let i = 1; i < newStacks.length; i++)
1256
+ if (newStacks[i].pos < this.minStackPos)
1257
+ this.minStackPos = newStacks[i].pos;
1258
+ return null;
1259
+ }
1260
+ stopAt(pos) {
1261
+ if (this.stoppedAt != null && this.stoppedAt < pos)
1262
+ throw new RangeError("Can't move stoppedAt forward");
1263
+ this.stoppedAt = pos;
1264
+ }
1265
+ // Returns an updated version of the given stack, or null if the
1266
+ // stack can't advance normally. When `split` and `stacks` are
1267
+ // given, stacks split off by ambiguous operations will be pushed to
1268
+ // `split`, or added to `stacks` if they move `pos` forward.
1269
+ advanceStack(stack, stacks, split) {
1270
+ let start = stack.pos, { parser } = this;
1271
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1272
+ if (this.stoppedAt != null && start > this.stoppedAt)
1273
+ return stack.forceReduce() ? stack : null;
1274
+ if (this.fragments) {
1275
+ let strictCx = stack.curContext && stack.curContext.tracker.strict, cxHash = strictCx ? stack.curContext.hash : 0;
1276
+ for (let cached = this.fragments.nodeAt(start); cached; ) {
1277
+ let match = this.parser.nodeSet.types[cached.type.id] == cached.type ? parser.getGoto(stack.state, cached.type.id) : -1;
1278
+ if (match > -1 && cached.length && (!strictCx || (cached.prop(NodeProp.contextHash) || 0) == cxHash)) {
1279
+ stack.useNode(cached, match);
1280
+ if (verbose)
1281
+ console.log(base + this.stackID(stack) + ` (via reuse of ${parser.getName(cached.type.id)})`);
1282
+ return true;
1283
+ }
1284
+ if (!(cached instanceof Tree) || cached.children.length == 0 || cached.positions[0] > 0)
1285
+ break;
1286
+ let inner = cached.children[0];
1287
+ if (inner instanceof Tree && cached.positions[0] == 0)
1288
+ cached = inner;
1289
+ else
1290
+ break;
1291
+ }
1292
+ }
1293
+ let defaultReduce = parser.stateSlot(
1294
+ stack.state,
1295
+ 4
1296
+ /* ParseState.DefaultReduce */
1297
+ );
1298
+ if (defaultReduce > 0) {
1299
+ stack.reduce(defaultReduce);
1300
+ if (verbose)
1301
+ console.log(base + this.stackID(stack) + ` (via always-reduce ${parser.getName(
1302
+ defaultReduce & 65535
1303
+ /* Action.ValueMask */
1304
+ )})`);
1305
+ return true;
1306
+ }
1307
+ if (stack.stack.length >= 8400) {
1308
+ while (stack.stack.length > 6e3 && stack.forceReduce()) {
1309
+ }
1310
+ }
1311
+ let actions = this.tokens.getActions(stack);
1312
+ for (let i = 0; i < actions.length; ) {
1313
+ let action = actions[i++], term = actions[i++], end = actions[i++];
1314
+ let last = i == actions.length || !split;
1315
+ let localStack = last ? stack : stack.split();
1316
+ let main = this.tokens.mainToken;
1317
+ localStack.apply(action, term, main ? main.start : localStack.pos, end);
1318
+ if (verbose)
1319
+ console.log(base + this.stackID(localStack) + ` (via ${(action & 65536) == 0 ? "shift" : `reduce of ${parser.getName(
1320
+ action & 65535
1321
+ /* Action.ValueMask */
1322
+ )}`} for ${parser.getName(term)} @ ${start}${localStack == stack ? "" : ", split"})`);
1323
+ if (last)
1324
+ return true;
1325
+ else if (localStack.pos > start)
1326
+ stacks.push(localStack);
1327
+ else
1328
+ split.push(localStack);
1329
+ }
1330
+ return false;
1331
+ }
1332
+ // Advance a given stack forward as far as it will go. Returns the
1333
+ // (possibly updated) stack if it got stuck, or null if it moved
1334
+ // forward and was given to `pushStackDedup`.
1335
+ advanceFully(stack, newStacks) {
1336
+ let pos = stack.pos;
1337
+ for (; ; ) {
1338
+ if (!this.advanceStack(stack, null, null))
1339
+ return false;
1340
+ if (stack.pos > pos) {
1341
+ pushStackDedup(stack, newStacks);
1342
+ return true;
1343
+ }
1344
+ }
1345
+ }
1346
+ runRecovery(stacks, tokens, newStacks) {
1347
+ let finished = null, restarted = false;
1348
+ for (let i = 0; i < stacks.length; i++) {
1349
+ let stack = stacks[i], token = tokens[i << 1], tokenEnd = tokens[(i << 1) + 1];
1350
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1351
+ if (stack.deadEnd) {
1352
+ if (restarted)
1353
+ continue;
1354
+ restarted = true;
1355
+ stack.restart();
1356
+ if (verbose)
1357
+ console.log(base + this.stackID(stack) + " (restarted)");
1358
+ let done = this.advanceFully(stack, newStacks);
1359
+ if (done)
1360
+ continue;
1361
+ }
1362
+ let force = stack.split(), forceBase = base;
1363
+ for (let j = 0; j < 10 && force.forceReduce(); j++) {
1364
+ if (verbose)
1365
+ console.log(forceBase + this.stackID(force) + " (via force-reduce)");
1366
+ let done = this.advanceFully(force, newStacks);
1367
+ if (done)
1368
+ break;
1369
+ if (verbose)
1370
+ forceBase = this.stackID(force) + " -> ";
1371
+ }
1372
+ for (let insert of stack.recoverByInsert(token)) {
1373
+ if (verbose)
1374
+ console.log(base + this.stackID(insert) + " (via recover-insert)");
1375
+ this.advanceFully(insert, newStacks);
1376
+ }
1377
+ if (this.stream.end > stack.pos) {
1378
+ if (tokenEnd == stack.pos) {
1379
+ tokenEnd++;
1380
+ token = 0;
1381
+ }
1382
+ stack.recoverByDelete(token, tokenEnd);
1383
+ if (verbose)
1384
+ console.log(base + this.stackID(stack) + ` (via recover-delete ${this.parser.getName(token)})`);
1385
+ pushStackDedup(stack, newStacks);
1386
+ } else if (!finished || finished.score < force.score) {
1387
+ finished = force;
1388
+ }
1389
+ }
1390
+ return finished;
1391
+ }
1392
+ // Convert the stack's buffer to a syntax tree.
1393
+ stackToTree(stack) {
1394
+ stack.close();
1395
+ return Tree.build({
1396
+ buffer: StackBufferCursor.create(stack),
1397
+ nodeSet: this.parser.nodeSet,
1398
+ topID: this.topTerm,
1399
+ maxBufferLength: this.parser.bufferLength,
1400
+ reused: this.reused,
1401
+ start: this.ranges[0].from,
1402
+ length: stack.pos - this.ranges[0].from,
1403
+ minRepeatType: this.parser.minRepeatTerm
1404
+ });
1405
+ }
1406
+ stackID(stack) {
1407
+ let id2 = (stackIDs || (stackIDs = /* @__PURE__ */ new WeakMap())).get(stack);
1408
+ if (!id2)
1409
+ stackIDs.set(stack, id2 = String.fromCodePoint(this.nextStackID++));
1410
+ return id2 + stack;
1411
+ }
1412
+ };
1413
+ function pushStackDedup(stack, newStacks) {
1414
+ for (let i = 0; i < newStacks.length; i++) {
1415
+ let other = newStacks[i];
1416
+ if (other.pos == stack.pos && other.sameState(stack)) {
1417
+ if (newStacks[i].score < stack.score)
1418
+ newStacks[i] = stack;
1419
+ return;
1420
+ }
1421
+ }
1422
+ newStacks.push(stack);
1423
+ }
1424
+ var Dialect = class {
1425
+ constructor(source, flags, disabled) {
1426
+ this.source = source;
1427
+ this.flags = flags;
1428
+ this.disabled = disabled;
1429
+ }
1430
+ allows(term) {
1431
+ return !this.disabled || this.disabled[term] == 0;
1432
+ }
1433
+ };
1434
+ var id = (x) => x;
1435
+ var ContextTracker = class {
1436
+ /**
1437
+ Define a context tracker.
1438
+ */
1439
+ constructor(spec) {
1440
+ this.start = spec.start;
1441
+ this.shift = spec.shift || id;
1442
+ this.reduce = spec.reduce || id;
1443
+ this.reuse = spec.reuse || id;
1444
+ this.hash = spec.hash || (() => 0);
1445
+ this.strict = spec.strict !== false;
1446
+ }
1447
+ };
1448
+ var LRParser = class _LRParser extends Parser {
1449
+ /**
1450
+ @internal
1451
+ */
1452
+ constructor(spec) {
1453
+ super();
1454
+ this.wrappers = [];
1455
+ if (spec.version != 14)
1456
+ throw new RangeError(`Parser version (${spec.version}) doesn't match runtime version (${14})`);
1457
+ let nodeNames = spec.nodeNames.split(" ");
1458
+ this.minRepeatTerm = nodeNames.length;
1459
+ for (let i = 0; i < spec.repeatNodeCount; i++)
1460
+ nodeNames.push("");
1461
+ let topTerms = Object.keys(spec.topRules).map((r) => spec.topRules[r][1]);
1462
+ let nodeProps = [];
1463
+ for (let i = 0; i < nodeNames.length; i++)
1464
+ nodeProps.push([]);
1465
+ function setProp(nodeID, prop, value) {
1466
+ nodeProps[nodeID].push([prop, prop.deserialize(String(value))]);
1467
+ }
1468
+ if (spec.nodeProps)
1469
+ for (let propSpec of spec.nodeProps) {
1470
+ let prop = propSpec[0];
1471
+ if (typeof prop == "string")
1472
+ prop = NodeProp[prop];
1473
+ for (let i = 1; i < propSpec.length; ) {
1474
+ let next = propSpec[i++];
1475
+ if (next >= 0) {
1476
+ setProp(next, prop, propSpec[i++]);
1477
+ } else {
1478
+ let value = propSpec[i + -next];
1479
+ for (let j = -next; j > 0; j--)
1480
+ setProp(propSpec[i++], prop, value);
1481
+ i++;
1482
+ }
1483
+ }
1484
+ }
1485
+ this.nodeSet = new NodeSet(nodeNames.map((name, i) => NodeType.define({
1486
+ name: i >= this.minRepeatTerm ? void 0 : name,
1487
+ id: i,
1488
+ props: nodeProps[i],
1489
+ top: topTerms.indexOf(i) > -1,
1490
+ error: i == 0,
1491
+ skipped: spec.skippedNodes && spec.skippedNodes.indexOf(i) > -1
1492
+ })));
1493
+ if (spec.propSources)
1494
+ this.nodeSet = this.nodeSet.extend(...spec.propSources);
1495
+ this.strict = false;
1496
+ this.bufferLength = DefaultBufferLength;
1497
+ let tokenArray = decodeArray(spec.tokenData);
1498
+ this.context = spec.context;
1499
+ this.specializerSpecs = spec.specialized || [];
1500
+ this.specialized = new Uint16Array(this.specializerSpecs.length);
1501
+ for (let i = 0; i < this.specializerSpecs.length; i++)
1502
+ this.specialized[i] = this.specializerSpecs[i].term;
1503
+ this.specializers = this.specializerSpecs.map(getSpecializer);
1504
+ this.states = decodeArray(spec.states, Uint32Array);
1505
+ this.data = decodeArray(spec.stateData);
1506
+ this.goto = decodeArray(spec.goto);
1507
+ this.maxTerm = spec.maxTerm;
1508
+ this.tokenizers = spec.tokenizers.map((value) => typeof value == "number" ? new TokenGroup(tokenArray, value) : value);
1509
+ this.topRules = spec.topRules;
1510
+ this.dialects = spec.dialects || {};
1511
+ this.dynamicPrecedences = spec.dynamicPrecedences || null;
1512
+ this.tokenPrecTable = spec.tokenPrec;
1513
+ this.termNames = spec.termNames || null;
1514
+ this.maxNode = this.nodeSet.types.length - 1;
1515
+ this.dialect = this.parseDialect();
1516
+ this.top = this.topRules[Object.keys(this.topRules)[0]];
1517
+ }
1518
+ createParse(input, fragments, ranges) {
1519
+ let parse = new Parse(this, input, fragments, ranges);
1520
+ for (let w of this.wrappers)
1521
+ parse = w(parse, input, fragments, ranges);
1522
+ return parse;
1523
+ }
1524
+ /**
1525
+ Get a goto table entry @internal
1526
+ */
1527
+ getGoto(state, term, loose = false) {
1528
+ let table = this.goto;
1529
+ if (term >= table[0])
1530
+ return -1;
1531
+ for (let pos = table[term + 1]; ; ) {
1532
+ let groupTag = table[pos++], last = groupTag & 1;
1533
+ let target = table[pos++];
1534
+ if (last && loose)
1535
+ return target;
1536
+ for (let end = pos + (groupTag >> 1); pos < end; pos++)
1537
+ if (table[pos] == state)
1538
+ return target;
1539
+ if (last)
1540
+ return -1;
1541
+ }
1542
+ }
1543
+ /**
1544
+ Check if this state has an action for a given terminal @internal
1545
+ */
1546
+ hasAction(state, terminal) {
1547
+ let data = this.data;
1548
+ for (let set = 0; set < 2; set++) {
1549
+ for (let i = this.stateSlot(
1550
+ state,
1551
+ set ? 2 : 1
1552
+ /* ParseState.Actions */
1553
+ ), next; ; i += 3) {
1554
+ if ((next = data[i]) == 65535) {
1555
+ if (data[i + 1] == 1)
1556
+ next = data[i = pair(data, i + 2)];
1557
+ else if (data[i + 1] == 2)
1558
+ return pair(data, i + 2);
1559
+ else
1560
+ break;
1561
+ }
1562
+ if (next == terminal || next == 0)
1563
+ return pair(data, i + 1);
1564
+ }
1565
+ }
1566
+ return 0;
1567
+ }
1568
+ /**
1569
+ @internal
1570
+ */
1571
+ stateSlot(state, slot) {
1572
+ return this.states[state * 6 + slot];
1573
+ }
1574
+ /**
1575
+ @internal
1576
+ */
1577
+ stateFlag(state, flag) {
1578
+ return (this.stateSlot(
1579
+ state,
1580
+ 0
1581
+ /* ParseState.Flags */
1582
+ ) & flag) > 0;
1583
+ }
1584
+ /**
1585
+ @internal
1586
+ */
1587
+ validAction(state, action) {
1588
+ return !!this.allActions(state, (a) => a == action ? true : null);
1589
+ }
1590
+ /**
1591
+ @internal
1592
+ */
1593
+ allActions(state, action) {
1594
+ let deflt = this.stateSlot(
1595
+ state,
1596
+ 4
1597
+ /* ParseState.DefaultReduce */
1598
+ );
1599
+ let result = deflt ? action(deflt) : void 0;
1600
+ for (let i = this.stateSlot(
1601
+ state,
1602
+ 1
1603
+ /* ParseState.Actions */
1604
+ ); result == null; i += 3) {
1605
+ if (this.data[i] == 65535) {
1606
+ if (this.data[i + 1] == 1)
1607
+ i = pair(this.data, i + 2);
1608
+ else
1609
+ break;
1610
+ }
1611
+ result = action(pair(this.data, i + 1));
1612
+ }
1613
+ return result;
1614
+ }
1615
+ /**
1616
+ Get the states that can follow this one through shift actions or
1617
+ goto jumps. @internal
1618
+ */
1619
+ nextStates(state) {
1620
+ let result = [];
1621
+ for (let i = this.stateSlot(
1622
+ state,
1623
+ 1
1624
+ /* ParseState.Actions */
1625
+ ); ; i += 3) {
1626
+ if (this.data[i] == 65535) {
1627
+ if (this.data[i + 1] == 1)
1628
+ i = pair(this.data, i + 2);
1629
+ else
1630
+ break;
1631
+ }
1632
+ if ((this.data[i + 2] & 65536 >> 16) == 0) {
1633
+ let value = this.data[i + 1];
1634
+ if (!result.some((v, i2) => i2 & 1 && v == value))
1635
+ result.push(this.data[i], value);
1636
+ }
1637
+ }
1638
+ return result;
1639
+ }
1640
+ /**
1641
+ Configure the parser. Returns a new parser instance that has the
1642
+ given settings modified. Settings not provided in `config` are
1643
+ kept from the original parser.
1644
+ */
1645
+ configure(config) {
1646
+ let copy = Object.assign(Object.create(_LRParser.prototype), this);
1647
+ if (config.props)
1648
+ copy.nodeSet = this.nodeSet.extend(...config.props);
1649
+ if (config.top) {
1650
+ let info = this.topRules[config.top];
1651
+ if (!info)
1652
+ throw new RangeError(`Invalid top rule name ${config.top}`);
1653
+ copy.top = info;
1654
+ }
1655
+ if (config.tokenizers)
1656
+ copy.tokenizers = this.tokenizers.map((t) => {
1657
+ let found = config.tokenizers.find((r) => r.from == t);
1658
+ return found ? found.to : t;
1659
+ });
1660
+ if (config.specializers) {
1661
+ copy.specializers = this.specializers.slice();
1662
+ copy.specializerSpecs = this.specializerSpecs.map((s, i) => {
1663
+ let found = config.specializers.find((r) => r.from == s.external);
1664
+ if (!found)
1665
+ return s;
1666
+ let spec = Object.assign(Object.assign({}, s), { external: found.to });
1667
+ copy.specializers[i] = getSpecializer(spec);
1668
+ return spec;
1669
+ });
1670
+ }
1671
+ if (config.contextTracker)
1672
+ copy.context = config.contextTracker;
1673
+ if (config.dialect)
1674
+ copy.dialect = this.parseDialect(config.dialect);
1675
+ if (config.strict != null)
1676
+ copy.strict = config.strict;
1677
+ if (config.wrap)
1678
+ copy.wrappers = copy.wrappers.concat(config.wrap);
1679
+ if (config.bufferLength != null)
1680
+ copy.bufferLength = config.bufferLength;
1681
+ return copy;
1682
+ }
1683
+ /**
1684
+ Tells you whether any [parse wrappers](#lr.ParserConfig.wrap)
1685
+ are registered for this parser.
1686
+ */
1687
+ hasWrappers() {
1688
+ return this.wrappers.length > 0;
1689
+ }
1690
+ /**
1691
+ Returns the name associated with a given term. This will only
1692
+ work for all terms when the parser was generated with the
1693
+ `--names` option. By default, only the names of tagged terms are
1694
+ stored.
1695
+ */
1696
+ getName(term) {
1697
+ return this.termNames ? this.termNames[term] : String(term <= this.maxNode && this.nodeSet.types[term].name || term);
1698
+ }
1699
+ /**
1700
+ The eof term id is always allocated directly after the node
1701
+ types. @internal
1702
+ */
1703
+ get eofTerm() {
1704
+ return this.maxNode + 1;
1705
+ }
1706
+ /**
1707
+ The type of top node produced by the parser.
1708
+ */
1709
+ get topNode() {
1710
+ return this.nodeSet.types[this.top[1]];
1711
+ }
1712
+ /**
1713
+ @internal
1714
+ */
1715
+ dynamicPrecedence(term) {
1716
+ let prec = this.dynamicPrecedences;
1717
+ return prec == null ? 0 : prec[term] || 0;
1718
+ }
1719
+ /**
1720
+ @internal
1721
+ */
1722
+ parseDialect(dialect) {
1723
+ let values = Object.keys(this.dialects), flags = values.map(() => false);
1724
+ if (dialect)
1725
+ for (let part of dialect.split(" ")) {
1726
+ let id2 = values.indexOf(part);
1727
+ if (id2 >= 0)
1728
+ flags[id2] = true;
1729
+ }
1730
+ let disabled = null;
1731
+ for (let i = 0; i < values.length; i++)
1732
+ if (!flags[i]) {
1733
+ for (let j = this.dialects[values[i]], id2; (id2 = this.data[j++]) != 65535; )
1734
+ (disabled || (disabled = new Uint8Array(this.maxTerm + 1)))[id2] = 1;
1735
+ }
1736
+ return new Dialect(dialect, flags, disabled);
1737
+ }
1738
+ /**
1739
+ Used by the output of the parser generator. Not available to
1740
+ user code. @hide
1741
+ */
1742
+ static deserialize(spec) {
1743
+ return new _LRParser(spec);
1744
+ }
1745
+ };
1746
+ function pair(data, off) {
1747
+ return data[off] | data[off + 1] << 16;
1748
+ }
1749
+ function findFinished(stacks) {
1750
+ let best = null;
1751
+ for (let stack of stacks) {
1752
+ let stopped = stack.p.stoppedAt;
1753
+ if ((stack.pos == stack.p.stream.end || stopped != null && stack.pos > stopped) && stack.p.parser.stateFlag(
1754
+ stack.state,
1755
+ 2
1756
+ /* StateFlag.Accepting */
1757
+ ) && (!best || best.score < stack.score))
1758
+ best = stack;
1759
+ }
1760
+ return best;
1761
+ }
1762
+ function getSpecializer(spec) {
1763
+ if (spec.external) {
1764
+ let mask = spec.extend ? 1 : 0;
1765
+ return (value, stack) => spec.external(value, stack) << 1 | mask;
1766
+ }
1767
+ return spec.get;
1768
+ }
1769
+
1770
+ export {
1771
+ LocalTokenGroup,
1772
+ ExternalTokenizer,
1773
+ ContextTracker,
1774
+ LRParser
1775
+ };
1776
+ //# sourceMappingURL=chunk-44CLUOQE.js.map