@onereach/ui-components 6.4.2 → 6.5.0-beta.3403.0

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 (77) hide show
  1. package/dist/bundled/v2/components/OrCode/OrCode.js +2 -2
  2. package/dist/bundled/v2/components/OrCode/index.js +5 -3
  3. package/dist/bundled/v2/components/OrCode/lang.js +5 -3
  4. package/dist/bundled/v2/components/OrCode/libs.js +5 -3
  5. package/dist/bundled/v2/components/OrCode/theme.js +1 -1
  6. package/dist/bundled/v2/components/OrCodeV3/OrCode.js +52 -30
  7. package/dist/bundled/v2/components/OrCodeV3/OrCode.vue.d.ts +4 -3
  8. package/dist/bundled/v2/components/OrCodeV3/index.d.ts +1 -0
  9. package/dist/bundled/v2/components/OrCodeV3/index.js +5 -0
  10. package/dist/bundled/v2/components/OrCodeV3/libs.d.ts +6 -0
  11. package/dist/bundled/v2/components/OrCodeV3/libs.js +5 -0
  12. package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.js +2 -2
  13. package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.js +1 -1
  14. package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/theme.js +1 -1
  15. package/dist/bundled/v2/components/index.js +6 -3
  16. package/dist/bundled/v2/index-317a90b7.js +361 -0
  17. package/dist/bundled/v2/index-5465a04e.js +825 -0
  18. package/dist/bundled/v2/index-5df3b1d8.js +94 -0
  19. package/dist/bundled/v2/index-5fa53f66.js +759 -0
  20. package/dist/bundled/{v3/index-ceea3e83.js → v2/index-7caa3ac5.js} +3 -3
  21. package/dist/bundled/v2/{index-eca317e0.js → index-bd08fe6f.js} +27 -465
  22. package/dist/bundled/v2/index-d017a622.js +1892 -0
  23. package/dist/bundled/v2/{index-62bb1bac.js → index-d4e4f8a8.js} +10 -831
  24. package/dist/bundled/v2/{index-3cae2d25.js → index-d5c13979.js} +1 -1
  25. package/dist/bundled/v2/index-f949d42c.js +1598 -0
  26. package/dist/bundled/v2/index.js +6 -3
  27. package/dist/bundled/v3/{OrCode.vue_vue_type_script_lang-e88b2b44.js → OrCode.vue_vue_type_script_lang-9be03ab0.js} +2 -2
  28. package/dist/bundled/v3/{OrCode.vue_vue_type_script_lang-c9522cd5.js → OrCode.vue_vue_type_script_lang-f6022ce5.js} +52 -30
  29. package/dist/bundled/v3/components/OrCode/OrCode.js +2 -2
  30. package/dist/bundled/v3/components/OrCode/index.js +6 -4
  31. package/dist/bundled/v3/components/OrCode/lang.js +5 -3
  32. package/dist/bundled/v3/components/OrCode/libs.js +5 -3
  33. package/dist/bundled/v3/components/OrCode/theme.js +1 -1
  34. package/dist/bundled/v3/components/OrCodeV3/OrCode.js +2 -2
  35. package/dist/bundled/v3/components/OrCodeV3/OrCode.vue.d.ts +3 -2
  36. package/dist/bundled/v3/components/OrCodeV3/index.d.ts +1 -0
  37. package/dist/bundled/v3/components/OrCodeV3/index.js +6 -1
  38. package/dist/bundled/v3/components/OrCodeV3/libs.d.ts +6 -0
  39. package/dist/bundled/v3/components/OrCodeV3/libs.js +5 -0
  40. package/dist/bundled/v3/components/OrRichTextEditorV3/OrRichTextEditor.js +2 -2
  41. package/dist/bundled/v3/components/OrRichTextEditorV3/index.js +1 -1
  42. package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.js +2 -2
  43. package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.js +1 -1
  44. package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/theme.js +1 -1
  45. package/dist/bundled/v3/components/index.js +9 -6
  46. package/dist/bundled/v3/{index-62bb1bac.js → index-16f605c7.js} +10 -831
  47. package/dist/bundled/v3/index-1f2974f3.js +1892 -0
  48. package/dist/bundled/v3/index-1fc9d52b.js +759 -0
  49. package/dist/bundled/v3/index-210fd8da.js +825 -0
  50. package/dist/bundled/{v2/index-ceea3e83.js → v3/index-71514f0d.js} +3 -3
  51. package/dist/bundled/v3/{index-eca317e0.js → index-766903ad.js} +27 -465
  52. package/dist/bundled/v3/index-a4b02e4e.js +1598 -0
  53. package/dist/bundled/v3/index-a8d259c3.js +361 -0
  54. package/dist/bundled/v3/{index-3cae2d25.js → index-e81a9bf9.js} +1 -1
  55. package/dist/bundled/v3/index-f6290750.js +94 -0
  56. package/dist/bundled/v3/index.js +9 -6
  57. package/dist/esm/v2/{OrCode-f5c25b63.js → OrCode-e2222cf7.js} +54 -34
  58. package/dist/esm/v2/components/index.js +3 -3
  59. package/dist/esm/v2/components/or-code-v3/OrCode.vue.d.ts +4 -3
  60. package/dist/esm/v2/components/or-code-v3/index.d.ts +1 -0
  61. package/dist/esm/v2/components/or-code-v3/index.js +9 -10
  62. package/dist/esm/v2/components/or-code-v3/libs.d.ts +6 -0
  63. package/dist/esm/v2/index.js +3 -3
  64. package/dist/esm/v3/{OrCode-9662d0da.js → OrCode-f6a8e9f2.js} +54 -34
  65. package/dist/esm/v3/components/index.js +3 -3
  66. package/dist/esm/v3/components/or-code-v3/OrCode.vue.d.ts +3 -2
  67. package/dist/esm/v3/components/or-code-v3/index.d.ts +1 -0
  68. package/dist/esm/v3/components/or-code-v3/index.js +9 -10
  69. package/dist/esm/v3/components/or-code-v3/libs.d.ts +6 -0
  70. package/dist/esm/v3/index.js +3 -3
  71. package/package.json +2 -3
  72. package/src/components/or-code-v3/OrCode.vue +70 -50
  73. package/src/components/or-code-v3/index.ts +1 -0
  74. package/src/components/or-code-v3/libs.ts +10 -0
  75. package/dist/bundled/v2/index-60e4eacf.js +0 -4048
  76. package/dist/bundled/v3/index-60e4eacf.js +0 -4048
  77. /package/dist/bundled/v3/{OrRichTextEditor.vue_vue_type_script_lang-5a2318e5.js → OrRichTextEditor.vue_vue_type_script_lang-63edf53c.js} +0 -0
@@ -0,0 +1,1598 @@
1
+ import { an as Parser, ao as NodeSet, ap as NodeType, aq as DefaultBufferLength, N as NodeProp, ar as Tree, ae as IterMode } from './index-d5c13979.js';
2
+
3
+ /// A parse stack. These are used internally by the parser to track
4
+ /// parsing progress. They also provide some properties and methods
5
+ /// that external code such as a tokenizer can use to get information
6
+ /// about the parse state.
7
+ class Stack {
8
+ /// @internal
9
+ constructor(
10
+ /// The parse that this stack is part of @internal
11
+ p,
12
+ /// Holds state, input pos, buffer index triplets for all but the
13
+ /// top state @internal
14
+ stack,
15
+ /// The current parse state @internal
16
+ state,
17
+ // The position at which the next reduce should take place. This
18
+ // can be less than `this.pos` when skipped expressions have been
19
+ // added to the stack (which should be moved outside of the next
20
+ // reduction)
21
+ /// @internal
22
+ reducePos,
23
+ /// The input position up to which this stack has parsed.
24
+ pos,
25
+ /// The dynamic score of the stack, including dynamic precedence
26
+ /// and error-recovery penalties
27
+ /// @internal
28
+ score,
29
+ // The output buffer. Holds (type, start, end, size) quads
30
+ // representing nodes created by the parser, where `size` is
31
+ // amount of buffer array entries covered by this node.
32
+ /// @internal
33
+ buffer,
34
+ // The base offset of the buffer. When stacks are split, the split
35
+ // instance shared the buffer history with its parent up to
36
+ // `bufferBase`, which is the absolute offset (including the
37
+ // offset of previous splits) into the buffer at which this stack
38
+ // starts writing.
39
+ /// @internal
40
+ bufferBase,
41
+ /// @internal
42
+ curContext) {
43
+ let lookAhead = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : 0;
44
+ let
45
+ // A parent stack from which this was split off, if any. This is
46
+ // set up so that it always points to a stack that has some
47
+ // additional buffer content, never to a stack with an equal
48
+ // `bufferBase`.
49
+ /// @internal
50
+ parent = arguments.length > 10 ? arguments[10] : undefined;
51
+ this.p = p;
52
+ this.stack = stack;
53
+ this.state = state;
54
+ this.reducePos = reducePos;
55
+ this.pos = pos;
56
+ this.score = score;
57
+ this.buffer = buffer;
58
+ this.bufferBase = bufferBase;
59
+ this.curContext = curContext;
60
+ this.lookAhead = lookAhead;
61
+ this.parent = parent;
62
+ }
63
+ /// @internal
64
+ toString() {
65
+ return `[${this.stack.filter((_, i) => i % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`;
66
+ }
67
+ // Start an empty stack
68
+ /// @internal
69
+ static start(p, state) {
70
+ let pos = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
71
+ let cx = p.parser.context;
72
+ return new Stack(p, [], state, pos, pos, 0, [], 0, cx ? new StackContext(cx, cx.start) : null, 0, null);
73
+ }
74
+ /// The stack's current [context](#lr.ContextTracker) value, if
75
+ /// any. Its type will depend on the context tracker's type
76
+ /// parameter, or it will be `null` if there is no context
77
+ /// tracker.
78
+ get context() {
79
+ return this.curContext ? this.curContext.context : null;
80
+ }
81
+ // Push a state onto the stack, tracking its start position as well
82
+ // as the buffer base at that point.
83
+ /// @internal
84
+ pushState(state, start) {
85
+ this.stack.push(this.state, start, this.bufferBase + this.buffer.length);
86
+ this.state = state;
87
+ }
88
+ // Apply a reduce action
89
+ /// @internal
90
+ reduce(action) {
91
+ var _a;
92
+ let depth = action >> 19 /* Action.ReduceDepthShift */,
93
+ type = action & 65535 /* Action.ValueMask */;
94
+ let {
95
+ parser
96
+ } = this.p;
97
+ let dPrec = parser.dynamicPrecedence(type);
98
+ if (dPrec) this.score += dPrec;
99
+ if (depth == 0) {
100
+ this.pushState(parser.getGoto(this.state, type, true), this.reducePos);
101
+ // Zero-depth reductions are a special case—they add stuff to
102
+ // the stack without popping anything off.
103
+ if (type < parser.minRepeatTerm) this.storeNode(type, this.reducePos, this.reducePos, 4, true);
104
+ this.reduceContext(type, this.reducePos);
105
+ return;
106
+ }
107
+ // Find the base index into `this.stack`, content after which will
108
+ // be dropped. Note that with `StayFlag` reductions we need to
109
+ // consume two extra frames (the dummy parent node for the skipped
110
+ // expression and the state that we'll be staying in, which should
111
+ // be moved to `this.state`).
112
+ let base = this.stack.length - (depth - 1) * 3 - (action & 262144 /* Action.StayFlag */ ? 6 : 0);
113
+ let start = base ? this.stack[base - 2] : this.p.ranges[0].from,
114
+ size = this.reducePos - start;
115
+ // This is a kludge to try and detect overly deep left-associative
116
+ // trees, which will not increase the parse stack depth and thus
117
+ // won't be caught by the regular stack-depth limit check.
118
+ if (size >= 2000 /* Recover.MinBigReduction */ && !((_a = this.p.parser.nodeSet.types[type]) === null || _a === void 0 ? void 0 : _a.isAnonymous)) {
119
+ if (start == this.p.lastBigReductionStart) {
120
+ this.p.bigReductionCount++;
121
+ this.p.lastBigReductionSize = size;
122
+ } else if (this.p.lastBigReductionSize < size) {
123
+ this.p.bigReductionCount = 1;
124
+ this.p.lastBigReductionStart = start;
125
+ this.p.lastBigReductionSize = size;
126
+ }
127
+ }
128
+ let bufferBase = base ? this.stack[base - 1] : 0,
129
+ count = this.bufferBase + this.buffer.length - bufferBase;
130
+ // Store normal terms or `R -> R R` repeat reductions
131
+ if (type < parser.minRepeatTerm || action & 131072 /* Action.RepeatFlag */) {
132
+ let pos = parser.stateFlag(this.state, 1 /* StateFlag.Skipped */) ? this.pos : this.reducePos;
133
+ this.storeNode(type, start, pos, count + 4, true);
134
+ }
135
+ if (action & 262144 /* Action.StayFlag */) {
136
+ this.state = this.stack[base];
137
+ } else {
138
+ let baseStateID = this.stack[base - 3];
139
+ this.state = parser.getGoto(baseStateID, type, true);
140
+ }
141
+ while (this.stack.length > base) this.stack.pop();
142
+ this.reduceContext(type, start);
143
+ }
144
+ // Shift a value into the buffer
145
+ /// @internal
146
+ storeNode(term, start, end) {
147
+ let size = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 4;
148
+ let isReduce = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
149
+ if (term == 0 /* Term.Err */ && (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) {
150
+ // Try to omit/merge adjacent error nodes
151
+ let cur = this,
152
+ top = this.buffer.length;
153
+ if (top == 0 && cur.parent) {
154
+ top = cur.bufferBase - cur.parent.bufferBase;
155
+ cur = cur.parent;
156
+ }
157
+ if (top > 0 && cur.buffer[top - 4] == 0 /* Term.Err */ && cur.buffer[top - 1] > -1) {
158
+ if (start == end) return;
159
+ if (cur.buffer[top - 2] >= start) {
160
+ cur.buffer[top - 2] = end;
161
+ return;
162
+ }
163
+ }
164
+ }
165
+ if (!isReduce || this.pos == end) {
166
+ // Simple case, just append
167
+ this.buffer.push(term, start, end, size);
168
+ } else {
169
+ // There may be skipped nodes that have to be moved forward
170
+ let index = this.buffer.length;
171
+ if (index > 0 && this.buffer[index - 4] != 0 /* Term.Err */) while (index > 0 && this.buffer[index - 2] > end) {
172
+ // Move this record forward
173
+ this.buffer[index] = this.buffer[index - 4];
174
+ this.buffer[index + 1] = this.buffer[index - 3];
175
+ this.buffer[index + 2] = this.buffer[index - 2];
176
+ this.buffer[index + 3] = this.buffer[index - 1];
177
+ index -= 4;
178
+ if (size > 4) size -= 4;
179
+ }
180
+ this.buffer[index] = term;
181
+ this.buffer[index + 1] = start;
182
+ this.buffer[index + 2] = end;
183
+ this.buffer[index + 3] = size;
184
+ }
185
+ }
186
+ // Apply a shift action
187
+ /// @internal
188
+ shift(action, next, nextEnd) {
189
+ let start = this.pos;
190
+ if (action & 131072 /* Action.GotoFlag */) {
191
+ this.pushState(action & 65535 /* Action.ValueMask */, this.pos);
192
+ } else if ((action & 262144 /* Action.StayFlag */) == 0) {
193
+ // Regular shift
194
+ let nextState = action,
195
+ {
196
+ parser
197
+ } = this.p;
198
+ if (nextEnd > this.pos || next <= parser.maxNode) {
199
+ this.pos = nextEnd;
200
+ if (!parser.stateFlag(nextState, 1 /* StateFlag.Skipped */)) this.reducePos = nextEnd;
201
+ }
202
+ this.pushState(nextState, start);
203
+ this.shiftContext(next, start);
204
+ if (next <= parser.maxNode) this.buffer.push(next, start, nextEnd, 4);
205
+ } else {
206
+ // Shift-and-stay, which means this is a skipped token
207
+ this.pos = nextEnd;
208
+ this.shiftContext(next, start);
209
+ if (next <= this.p.parser.maxNode) this.buffer.push(next, start, nextEnd, 4);
210
+ }
211
+ }
212
+ // Apply an action
213
+ /// @internal
214
+ apply(action, next, nextEnd) {
215
+ if (action & 65536 /* Action.ReduceFlag */) this.reduce(action);else this.shift(action, next, nextEnd);
216
+ }
217
+ // Add a prebuilt (reused) node into the buffer.
218
+ /// @internal
219
+ useNode(value, next) {
220
+ let index = this.p.reused.length - 1;
221
+ if (index < 0 || this.p.reused[index] != value) {
222
+ this.p.reused.push(value);
223
+ index++;
224
+ }
225
+ let start = this.pos;
226
+ this.reducePos = this.pos = start + value.length;
227
+ this.pushState(next, start);
228
+ this.buffer.push(index, start, this.reducePos, -1 /* size == -1 means this is a reused value */);
229
+ if (this.curContext) this.updateContext(this.curContext.tracker.reuse(this.curContext.context, value, this, this.p.stream.reset(this.pos - value.length)));
230
+ }
231
+ // Split the stack. Due to the buffer sharing and the fact
232
+ // that `this.stack` tends to stay quite shallow, this isn't very
233
+ // expensive.
234
+ /// @internal
235
+ split() {
236
+ let parent = this;
237
+ let off = parent.buffer.length;
238
+ // Because the top of the buffer (after this.pos) may be mutated
239
+ // to reorder reductions and skipped tokens, and shared buffers
240
+ // should be immutable, this copies any outstanding skipped tokens
241
+ // to the new buffer, and puts the base pointer before them.
242
+ while (off > 0 && parent.buffer[off - 2] > parent.reducePos) off -= 4;
243
+ let buffer = parent.buffer.slice(off),
244
+ base = parent.bufferBase + off;
245
+ // Make sure parent points to an actual parent with content, if there is such a parent.
246
+ while (parent && base == parent.bufferBase) parent = parent.parent;
247
+ return new Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, buffer, base, this.curContext, this.lookAhead, parent);
248
+ }
249
+ // Try to recover from an error by 'deleting' (ignoring) one token.
250
+ /// @internal
251
+ recoverByDelete(next, nextEnd) {
252
+ let isNode = next <= this.p.parser.maxNode;
253
+ if (isNode) this.storeNode(next, this.pos, nextEnd, 4);
254
+ this.storeNode(0 /* Term.Err */, this.pos, nextEnd, isNode ? 8 : 4);
255
+ this.pos = this.reducePos = nextEnd;
256
+ this.score -= 190 /* Recover.Delete */;
257
+ }
258
+ /// Check if the given term would be able to be shifted (optionally
259
+ /// after some reductions) on this stack. This can be useful for
260
+ /// external tokenizers that want to make sure they only provide a
261
+ /// given token when it applies.
262
+ canShift(term) {
263
+ for (let sim = new SimulatedStack(this);;) {
264
+ let action = this.p.parser.stateSlot(sim.state, 4 /* ParseState.DefaultReduce */) || this.p.parser.hasAction(sim.state, term);
265
+ if (action == 0) return false;
266
+ if ((action & 65536 /* Action.ReduceFlag */) == 0) return true;
267
+ sim.reduce(action);
268
+ }
269
+ }
270
+ // Apply up to Recover.MaxNext recovery actions that conceptually
271
+ // inserts some missing token or rule.
272
+ /// @internal
273
+ recoverByInsert(next) {
274
+ if (this.stack.length >= 300 /* Recover.MaxInsertStackDepth */) return [];
275
+ let nextStates = this.p.parser.nextStates(this.state);
276
+ if (nextStates.length > 4 /* Recover.MaxNext */ << 1 || this.stack.length >= 120 /* Recover.DampenInsertStackDepth */) {
277
+ let best = [];
278
+ for (let i = 0, s; i < nextStates.length; i += 2) {
279
+ if ((s = nextStates[i + 1]) != this.state && this.p.parser.hasAction(s, next)) best.push(nextStates[i], s);
280
+ }
281
+ if (this.stack.length < 120 /* Recover.DampenInsertStackDepth */) for (let i = 0; best.length < 4 /* Recover.MaxNext */ << 1 && i < nextStates.length; i += 2) {
282
+ let s = nextStates[i + 1];
283
+ if (!best.some((v, i) => i & 1 && v == s)) best.push(nextStates[i], s);
284
+ }
285
+ nextStates = best;
286
+ }
287
+ let result = [];
288
+ for (let i = 0; i < nextStates.length && result.length < 4 /* Recover.MaxNext */; i += 2) {
289
+ let s = nextStates[i + 1];
290
+ if (s == this.state) continue;
291
+ let stack = this.split();
292
+ stack.pushState(s, this.pos);
293
+ stack.storeNode(0 /* Term.Err */, stack.pos, stack.pos, 4, true);
294
+ stack.shiftContext(nextStates[i], this.pos);
295
+ stack.score -= 200 /* Recover.Insert */;
296
+ result.push(stack);
297
+ }
298
+ return result;
299
+ }
300
+ // Force a reduce, if possible. Return false if that can't
301
+ // be done.
302
+ /// @internal
303
+ forceReduce() {
304
+ let reduce = this.p.parser.stateSlot(this.state, 5 /* ParseState.ForcedReduce */);
305
+ if ((reduce & 65536 /* Action.ReduceFlag */) == 0) return false;
306
+ let {
307
+ parser
308
+ } = this.p;
309
+ if (!parser.validAction(this.state, reduce)) {
310
+ let depth = reduce >> 19 /* Action.ReduceDepthShift */,
311
+ term = reduce & 65535 /* Action.ValueMask */;
312
+ let target = this.stack.length - depth * 3;
313
+ if (target < 0 || parser.getGoto(this.stack[target], term, false) < 0) return false;
314
+ this.storeNode(0 /* Term.Err */, this.reducePos, this.reducePos, 4, true);
315
+ this.score -= 100 /* Recover.Reduce */;
316
+ }
317
+
318
+ this.reducePos = this.pos;
319
+ this.reduce(reduce);
320
+ return true;
321
+ }
322
+ /// @internal
323
+ forceAll() {
324
+ while (!this.p.parser.stateFlag(this.state, 2 /* StateFlag.Accepting */)) {
325
+ if (!this.forceReduce()) {
326
+ this.storeNode(0 /* Term.Err */, this.pos, this.pos, 4, true);
327
+ break;
328
+ }
329
+ }
330
+ return this;
331
+ }
332
+ /// Check whether this state has no further actions (assumed to be a direct descendant of the
333
+ /// top state, since any other states must be able to continue
334
+ /// somehow). @internal
335
+ get deadEnd() {
336
+ if (this.stack.length != 3) return false;
337
+ let {
338
+ parser
339
+ } = this.p;
340
+ return parser.data[parser.stateSlot(this.state, 1 /* ParseState.Actions */)] == 65535 /* Seq.End */ && !parser.stateSlot(this.state, 4 /* ParseState.DefaultReduce */);
341
+ }
342
+ /// Restart the stack (put it back in its start state). Only safe
343
+ /// when this.stack.length == 3 (state is directly below the top
344
+ /// state). @internal
345
+ restart() {
346
+ this.state = this.stack[0];
347
+ this.stack.length = 0;
348
+ }
349
+ /// @internal
350
+ sameState(other) {
351
+ if (this.state != other.state || this.stack.length != other.stack.length) return false;
352
+ for (let i = 0; i < this.stack.length; i += 3) if (this.stack[i] != other.stack[i]) return false;
353
+ return true;
354
+ }
355
+ /// Get the parser used by this stack.
356
+ get parser() {
357
+ return this.p.parser;
358
+ }
359
+ /// Test whether a given dialect (by numeric ID, as exported from
360
+ /// the terms file) is enabled.
361
+ dialectEnabled(dialectID) {
362
+ return this.p.parser.dialect.flags[dialectID];
363
+ }
364
+ shiftContext(term, start) {
365
+ if (this.curContext) this.updateContext(this.curContext.tracker.shift(this.curContext.context, term, this, this.p.stream.reset(start)));
366
+ }
367
+ reduceContext(term, start) {
368
+ if (this.curContext) this.updateContext(this.curContext.tracker.reduce(this.curContext.context, term, this, this.p.stream.reset(start)));
369
+ }
370
+ /// @internal
371
+ emitContext() {
372
+ let last = this.buffer.length - 1;
373
+ if (last < 0 || this.buffer[last] != -3) this.buffer.push(this.curContext.hash, this.reducePos, this.reducePos, -3);
374
+ }
375
+ /// @internal
376
+ emitLookAhead() {
377
+ let last = this.buffer.length - 1;
378
+ if (last < 0 || this.buffer[last] != -4) this.buffer.push(this.lookAhead, this.reducePos, this.reducePos, -4);
379
+ }
380
+ updateContext(context) {
381
+ if (context != this.curContext.context) {
382
+ let newCx = new StackContext(this.curContext.tracker, context);
383
+ if (newCx.hash != this.curContext.hash) this.emitContext();
384
+ this.curContext = newCx;
385
+ }
386
+ }
387
+ /// @internal
388
+ setLookAhead(lookAhead) {
389
+ if (lookAhead > this.lookAhead) {
390
+ this.emitLookAhead();
391
+ this.lookAhead = lookAhead;
392
+ }
393
+ }
394
+ /// @internal
395
+ close() {
396
+ if (this.curContext && this.curContext.tracker.strict) this.emitContext();
397
+ if (this.lookAhead > 0) this.emitLookAhead();
398
+ }
399
+ }
400
+ class StackContext {
401
+ constructor(tracker, context) {
402
+ this.tracker = tracker;
403
+ this.context = context;
404
+ this.hash = tracker.strict ? tracker.hash(context) : 0;
405
+ }
406
+ }
407
+ var Recover;
408
+ (function (Recover) {
409
+ Recover[Recover["Insert"] = 200] = "Insert";
410
+ Recover[Recover["Delete"] = 190] = "Delete";
411
+ Recover[Recover["Reduce"] = 100] = "Reduce";
412
+ Recover[Recover["MaxNext"] = 4] = "MaxNext";
413
+ Recover[Recover["MaxInsertStackDepth"] = 300] = "MaxInsertStackDepth";
414
+ Recover[Recover["DampenInsertStackDepth"] = 120] = "DampenInsertStackDepth";
415
+ Recover[Recover["MinBigReduction"] = 2000] = "MinBigReduction";
416
+ })(Recover || (Recover = {}));
417
+ // Used to cheaply run some reductions to scan ahead without mutating
418
+ // an entire stack
419
+ class SimulatedStack {
420
+ constructor(start) {
421
+ this.start = start;
422
+ this.state = start.state;
423
+ this.stack = start.stack;
424
+ this.base = this.stack.length;
425
+ }
426
+ reduce(action) {
427
+ let term = action & 65535 /* Action.ValueMask */,
428
+ depth = action >> 19 /* Action.ReduceDepthShift */;
429
+ if (depth == 0) {
430
+ if (this.stack == this.start.stack) this.stack = this.stack.slice();
431
+ this.stack.push(this.state, 0, 0);
432
+ this.base += 3;
433
+ } else {
434
+ this.base -= (depth - 1) * 3;
435
+ }
436
+ let goto = this.start.p.parser.getGoto(this.stack[this.base - 3], term, true);
437
+ this.state = goto;
438
+ }
439
+ }
440
+ // This is given to `Tree.build` to build a buffer, and encapsulates
441
+ // the parent-stack-walking necessary to read the nodes.
442
+ class StackBufferCursor {
443
+ constructor(stack, pos, index) {
444
+ this.stack = stack;
445
+ this.pos = pos;
446
+ this.index = index;
447
+ this.buffer = stack.buffer;
448
+ if (this.index == 0) this.maybeNext();
449
+ }
450
+ static create(stack) {
451
+ let pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : stack.bufferBase + stack.buffer.length;
452
+ return new StackBufferCursor(stack, pos, pos - stack.bufferBase);
453
+ }
454
+ maybeNext() {
455
+ let next = this.stack.parent;
456
+ if (next != null) {
457
+ this.index = this.stack.bufferBase - next.bufferBase;
458
+ this.stack = next;
459
+ this.buffer = next.buffer;
460
+ }
461
+ }
462
+ get id() {
463
+ return this.buffer[this.index - 4];
464
+ }
465
+ get start() {
466
+ return this.buffer[this.index - 3];
467
+ }
468
+ get end() {
469
+ return this.buffer[this.index - 2];
470
+ }
471
+ get size() {
472
+ return this.buffer[this.index - 1];
473
+ }
474
+ next() {
475
+ this.index -= 4;
476
+ this.pos -= 4;
477
+ if (this.index == 0) this.maybeNext();
478
+ }
479
+ fork() {
480
+ return new StackBufferCursor(this.stack, this.pos, this.index);
481
+ }
482
+ }
483
+
484
+ // See lezer-generator/src/encode.ts for comments about the encoding
485
+ // used here
486
+ function decodeArray(input) {
487
+ let Type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Uint16Array;
488
+ if (typeof input != "string") return input;
489
+ let array = null;
490
+ for (let pos = 0, out = 0; pos < input.length;) {
491
+ let value = 0;
492
+ for (;;) {
493
+ let next = input.charCodeAt(pos++),
494
+ stop = false;
495
+ if (next == 126 /* Encode.BigValCode */) {
496
+ value = 65535 /* Encode.BigVal */;
497
+ break;
498
+ }
499
+ if (next >= 92 /* Encode.Gap2 */) next--;
500
+ if (next >= 34 /* Encode.Gap1 */) next--;
501
+ let digit = next - 32 /* Encode.Start */;
502
+ if (digit >= 46 /* Encode.Base */) {
503
+ digit -= 46 /* Encode.Base */;
504
+ stop = true;
505
+ }
506
+ value += digit;
507
+ if (stop) break;
508
+ value *= 46 /* Encode.Base */;
509
+ }
510
+
511
+ if (array) array[out++] = value;else array = new Type(value);
512
+ }
513
+ return array;
514
+ }
515
+ class CachedToken {
516
+ constructor() {
517
+ this.start = -1;
518
+ this.value = -1;
519
+ this.end = -1;
520
+ this.extended = -1;
521
+ this.lookAhead = 0;
522
+ this.mask = 0;
523
+ this.context = 0;
524
+ }
525
+ }
526
+ const nullToken = new CachedToken();
527
+ /// [Tokenizers](#lr.ExternalTokenizer) interact with the input
528
+ /// through this interface. It presents the input as a stream of
529
+ /// characters, tracking lookahead and hiding the complexity of
530
+ /// [ranges](#common.Parser.parse^ranges) from tokenizer code.
531
+ class InputStream {
532
+ /// @internal
533
+ constructor(
534
+ /// @internal
535
+ input,
536
+ /// @internal
537
+ ranges) {
538
+ this.input = input;
539
+ this.ranges = ranges;
540
+ /// @internal
541
+ this.chunk = "";
542
+ /// @internal
543
+ this.chunkOff = 0;
544
+ /// Backup chunk
545
+ this.chunk2 = "";
546
+ this.chunk2Pos = 0;
547
+ /// The character code of the next code unit in the input, or -1
548
+ /// when the stream is at the end of the input.
549
+ this.next = -1;
550
+ /// @internal
551
+ this.token = nullToken;
552
+ this.rangeIndex = 0;
553
+ this.pos = this.chunkPos = ranges[0].from;
554
+ this.range = ranges[0];
555
+ this.end = ranges[ranges.length - 1].to;
556
+ this.readNext();
557
+ }
558
+ /// @internal
559
+ resolveOffset(offset, assoc) {
560
+ let range = this.range,
561
+ index = this.rangeIndex;
562
+ let pos = this.pos + offset;
563
+ while (pos < range.from) {
564
+ if (!index) return null;
565
+ let next = this.ranges[--index];
566
+ pos -= range.from - next.to;
567
+ range = next;
568
+ }
569
+ while (assoc < 0 ? pos > range.to : pos >= range.to) {
570
+ if (index == this.ranges.length - 1) return null;
571
+ let next = this.ranges[++index];
572
+ pos += next.from - range.to;
573
+ range = next;
574
+ }
575
+ return pos;
576
+ }
577
+ /// @internal
578
+ clipPos(pos) {
579
+ if (pos >= this.range.from && pos < this.range.to) return pos;
580
+ for (let range of this.ranges) if (range.to > pos) return Math.max(pos, range.from);
581
+ return this.end;
582
+ }
583
+ /// Look at a code unit near the stream position. `.peek(0)` equals
584
+ /// `.next`, `.peek(-1)` gives you the previous character, and so
585
+ /// on.
586
+ ///
587
+ /// Note that looking around during tokenizing creates dependencies
588
+ /// on potentially far-away content, which may reduce the
589
+ /// effectiveness incremental parsing—when looking forward—or even
590
+ /// cause invalid reparses when looking backward more than 25 code
591
+ /// units, since the library does not track lookbehind.
592
+ peek(offset) {
593
+ let idx = this.chunkOff + offset,
594
+ pos,
595
+ result;
596
+ if (idx >= 0 && idx < this.chunk.length) {
597
+ pos = this.pos + offset;
598
+ result = this.chunk.charCodeAt(idx);
599
+ } else {
600
+ let resolved = this.resolveOffset(offset, 1);
601
+ if (resolved == null) return -1;
602
+ pos = resolved;
603
+ if (pos >= this.chunk2Pos && pos < this.chunk2Pos + this.chunk2.length) {
604
+ result = this.chunk2.charCodeAt(pos - this.chunk2Pos);
605
+ } else {
606
+ let i = this.rangeIndex,
607
+ range = this.range;
608
+ while (range.to <= pos) range = this.ranges[++i];
609
+ this.chunk2 = this.input.chunk(this.chunk2Pos = pos);
610
+ if (pos + this.chunk2.length > range.to) this.chunk2 = this.chunk2.slice(0, range.to - pos);
611
+ result = this.chunk2.charCodeAt(0);
612
+ }
613
+ }
614
+ if (pos >= this.token.lookAhead) this.token.lookAhead = pos + 1;
615
+ return result;
616
+ }
617
+ /// Accept a token. By default, the end of the token is set to the
618
+ /// current stream position, but you can pass an offset (relative to
619
+ /// the stream position) to change that.
620
+ acceptToken(token) {
621
+ let endOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
622
+ let end = endOffset ? this.resolveOffset(endOffset, -1) : this.pos;
623
+ if (end == null || end < this.token.start) throw new RangeError("Token end out of bounds");
624
+ this.token.value = token;
625
+ this.token.end = end;
626
+ }
627
+ getChunk() {
628
+ if (this.pos >= this.chunk2Pos && this.pos < this.chunk2Pos + this.chunk2.length) {
629
+ let {
630
+ chunk,
631
+ chunkPos
632
+ } = this;
633
+ this.chunk = this.chunk2;
634
+ this.chunkPos = this.chunk2Pos;
635
+ this.chunk2 = chunk;
636
+ this.chunk2Pos = chunkPos;
637
+ this.chunkOff = this.pos - this.chunkPos;
638
+ } else {
639
+ this.chunk2 = this.chunk;
640
+ this.chunk2Pos = this.chunkPos;
641
+ let nextChunk = this.input.chunk(this.pos);
642
+ let end = this.pos + nextChunk.length;
643
+ this.chunk = end > this.range.to ? nextChunk.slice(0, this.range.to - this.pos) : nextChunk;
644
+ this.chunkPos = this.pos;
645
+ this.chunkOff = 0;
646
+ }
647
+ }
648
+ readNext() {
649
+ if (this.chunkOff >= this.chunk.length) {
650
+ this.getChunk();
651
+ if (this.chunkOff == this.chunk.length) return this.next = -1;
652
+ }
653
+ return this.next = this.chunk.charCodeAt(this.chunkOff);
654
+ }
655
+ /// Move the stream forward N (defaults to 1) code units. Returns
656
+ /// the new value of [`next`](#lr.InputStream.next).
657
+ advance() {
658
+ let n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
659
+ this.chunkOff += n;
660
+ while (this.pos + n >= this.range.to) {
661
+ if (this.rangeIndex == this.ranges.length - 1) return this.setDone();
662
+ n -= this.range.to - this.pos;
663
+ this.range = this.ranges[++this.rangeIndex];
664
+ this.pos = this.range.from;
665
+ }
666
+ this.pos += n;
667
+ if (this.pos >= this.token.lookAhead) this.token.lookAhead = this.pos + 1;
668
+ return this.readNext();
669
+ }
670
+ setDone() {
671
+ this.pos = this.chunkPos = this.end;
672
+ this.range = this.ranges[this.rangeIndex = this.ranges.length - 1];
673
+ this.chunk = "";
674
+ return this.next = -1;
675
+ }
676
+ /// @internal
677
+ reset(pos, token) {
678
+ if (token) {
679
+ this.token = token;
680
+ token.start = pos;
681
+ token.lookAhead = pos + 1;
682
+ token.value = token.extended = -1;
683
+ } else {
684
+ this.token = nullToken;
685
+ }
686
+ if (this.pos != pos) {
687
+ this.pos = pos;
688
+ if (pos == this.end) {
689
+ this.setDone();
690
+ return this;
691
+ }
692
+ while (pos < this.range.from) this.range = this.ranges[--this.rangeIndex];
693
+ while (pos >= this.range.to) this.range = this.ranges[++this.rangeIndex];
694
+ if (pos >= this.chunkPos && pos < this.chunkPos + this.chunk.length) {
695
+ this.chunkOff = pos - this.chunkPos;
696
+ } else {
697
+ this.chunk = "";
698
+ this.chunkOff = 0;
699
+ }
700
+ this.readNext();
701
+ }
702
+ return this;
703
+ }
704
+ /// @internal
705
+ read(from, to) {
706
+ if (from >= this.chunkPos && to <= this.chunkPos + this.chunk.length) return this.chunk.slice(from - this.chunkPos, to - this.chunkPos);
707
+ if (from >= this.chunk2Pos && to <= this.chunk2Pos + this.chunk2.length) return this.chunk2.slice(from - this.chunk2Pos, to - this.chunk2Pos);
708
+ if (from >= this.range.from && to <= this.range.to) return this.input.read(from, to);
709
+ let result = "";
710
+ for (let r of this.ranges) {
711
+ if (r.from >= to) break;
712
+ if (r.to > from) result += this.input.read(Math.max(r.from, from), Math.min(r.to, to));
713
+ }
714
+ return result;
715
+ }
716
+ }
717
+ /// @internal
718
+ class TokenGroup {
719
+ constructor(data, id) {
720
+ this.data = data;
721
+ this.id = id;
722
+ }
723
+ token(input, stack) {
724
+ let {
725
+ parser
726
+ } = stack.p;
727
+ readToken(this.data, input, stack, this.id, parser.data, parser.tokenPrecTable);
728
+ }
729
+ }
730
+ TokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
731
+ /// @hide
732
+ class LocalTokenGroup {
733
+ constructor(data, precTable, elseToken) {
734
+ this.precTable = precTable;
735
+ this.elseToken = elseToken;
736
+ this.data = typeof data == "string" ? decodeArray(data) : data;
737
+ }
738
+ token(input, stack) {
739
+ let start = input.pos,
740
+ skipped = 0;
741
+ for (;;) {
742
+ readToken(this.data, input, stack, 0, this.data, this.precTable);
743
+ if (input.token.value > -1) break;
744
+ if (this.elseToken == null) return;
745
+ if (input.next < 0) break;
746
+ input.advance();
747
+ input.reset(input.pos, input.token);
748
+ skipped++;
749
+ }
750
+ if (skipped) {
751
+ input.reset(start, input.token);
752
+ input.acceptToken(this.elseToken, skipped);
753
+ }
754
+ }
755
+ }
756
+ LocalTokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
757
+ /// `@external tokens` declarations in the grammar should resolve to
758
+ /// an instance of this class.
759
+ class ExternalTokenizer {
760
+ /// Create a tokenizer. The first argument is the function that,
761
+ /// given an input stream, scans for the types of tokens it
762
+ /// recognizes at the stream's position, and calls
763
+ /// [`acceptToken`](#lr.InputStream.acceptToken) when it finds
764
+ /// one.
765
+ constructor(
766
+ /// @internal
767
+ token) {
768
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
769
+ this.token = token;
770
+ this.contextual = !!options.contextual;
771
+ this.fallback = !!options.fallback;
772
+ this.extend = !!options.extend;
773
+ }
774
+ }
775
+ // Tokenizer data is stored a big uint16 array containing, for each
776
+ // state:
777
+ //
778
+ // - A group bitmask, indicating what token groups are reachable from
779
+ // this state, so that paths that can only lead to tokens not in
780
+ // any of the current groups can be cut off early.
781
+ //
782
+ // - The position of the end of the state's sequence of accepting
783
+ // tokens
784
+ //
785
+ // - The number of outgoing edges for the state
786
+ //
787
+ // - The accepting tokens, as (token id, group mask) pairs
788
+ //
789
+ // - The outgoing edges, as (start character, end character, state
790
+ // index) triples, with end character being exclusive
791
+ //
792
+ // This function interprets that data, running through a stream as
793
+ // long as new states with the a matching group mask can be reached,
794
+ // and updating `input.token` when it matches a token.
795
+ function readToken(data, input, stack, group, precTable, precOffset) {
796
+ let state = 0,
797
+ groupMask = 1 << group,
798
+ {
799
+ dialect
800
+ } = stack.p.parser;
801
+ scan: for (;;) {
802
+ if ((groupMask & data[state]) == 0) break;
803
+ let accEnd = data[state + 1];
804
+ // Check whether this state can lead to a token in the current group
805
+ // Accept tokens in this state, possibly overwriting
806
+ // lower-precedence / shorter tokens
807
+ for (let i = state + 3; i < accEnd; i += 2) if ((data[i + 1] & groupMask) > 0) {
808
+ let term = data[i];
809
+ if (dialect.allows(term) && (input.token.value == -1 || input.token.value == term || overrides(term, input.token.value, precTable, precOffset))) {
810
+ input.acceptToken(term);
811
+ break;
812
+ }
813
+ }
814
+ let next = input.next,
815
+ low = 0,
816
+ high = data[state + 2];
817
+ // Special case for EOF
818
+ if (input.next < 0 && high > low && data[accEnd + high * 3 - 3] == 65535 /* Seq.End */ && data[accEnd + high * 3 - 3] == 65535 /* Seq.End */) {
819
+ state = data[accEnd + high * 3 - 1];
820
+ continue scan;
821
+ }
822
+ // Do a binary search on the state's edges
823
+ for (; low < high;) {
824
+ let mid = low + high >> 1;
825
+ let index = accEnd + mid + (mid << 1);
826
+ let from = data[index],
827
+ to = data[index + 1] || 0x10000;
828
+ if (next < from) high = mid;else if (next >= to) low = mid + 1;else {
829
+ state = data[index + 2];
830
+ input.advance();
831
+ continue scan;
832
+ }
833
+ }
834
+ break;
835
+ }
836
+ }
837
+ function findOffset(data, start, term) {
838
+ for (let i = start, next; (next = data[i]) != 65535 /* Seq.End */; i++) if (next == term) return i - start;
839
+ return -1;
840
+ }
841
+ function overrides(token, prev, tableData, tableOffset) {
842
+ let iPrev = findOffset(tableData, tableOffset, prev);
843
+ return iPrev < 0 || findOffset(tableData, tableOffset, token) < iPrev;
844
+ }
845
+
846
+ // Environment variable used to control console output
847
+ const verbose = typeof process != "undefined" && process.env && /\bparse\b/.test(process.env.LOG);
848
+ let stackIDs = null;
849
+ var Safety;
850
+ (function (Safety) {
851
+ Safety[Safety["Margin"] = 25] = "Margin";
852
+ })(Safety || (Safety = {}));
853
+ function cutAt(tree, pos, side) {
854
+ let cursor = tree.cursor(IterMode.IncludeAnonymous);
855
+ cursor.moveTo(pos);
856
+ for (;;) {
857
+ if (!(side < 0 ? cursor.childBefore(pos) : cursor.childAfter(pos))) for (;;) {
858
+ if ((side < 0 ? cursor.to < pos : cursor.from > pos) && !cursor.type.isError) return side < 0 ? Math.max(0, Math.min(cursor.to - 1, pos - 25 /* Safety.Margin */)) : Math.min(tree.length, Math.max(cursor.from + 1, pos + 25 /* Safety.Margin */));
859
+ if (side < 0 ? cursor.prevSibling() : cursor.nextSibling()) break;
860
+ if (!cursor.parent()) return side < 0 ? 0 : tree.length;
861
+ }
862
+ }
863
+ }
864
+ class FragmentCursor {
865
+ constructor(fragments, nodeSet) {
866
+ this.fragments = fragments;
867
+ this.nodeSet = nodeSet;
868
+ this.i = 0;
869
+ this.fragment = null;
870
+ this.safeFrom = -1;
871
+ this.safeTo = -1;
872
+ this.trees = [];
873
+ this.start = [];
874
+ this.index = [];
875
+ this.nextFragment();
876
+ }
877
+ nextFragment() {
878
+ let fr = this.fragment = this.i == this.fragments.length ? null : this.fragments[this.i++];
879
+ if (fr) {
880
+ this.safeFrom = fr.openStart ? cutAt(fr.tree, fr.from + fr.offset, 1) - fr.offset : fr.from;
881
+ this.safeTo = fr.openEnd ? cutAt(fr.tree, fr.to + fr.offset, -1) - fr.offset : fr.to;
882
+ while (this.trees.length) {
883
+ this.trees.pop();
884
+ this.start.pop();
885
+ this.index.pop();
886
+ }
887
+ this.trees.push(fr.tree);
888
+ this.start.push(-fr.offset);
889
+ this.index.push(0);
890
+ this.nextStart = this.safeFrom;
891
+ } else {
892
+ this.nextStart = 1e9;
893
+ }
894
+ }
895
+ // `pos` must be >= any previously given `pos` for this cursor
896
+ nodeAt(pos) {
897
+ if (pos < this.nextStart) return null;
898
+ while (this.fragment && this.safeTo <= pos) this.nextFragment();
899
+ if (!this.fragment) return null;
900
+ for (;;) {
901
+ let last = this.trees.length - 1;
902
+ if (last < 0) {
903
+ // End of tree
904
+ this.nextFragment();
905
+ return null;
906
+ }
907
+ let top = this.trees[last],
908
+ index = this.index[last];
909
+ if (index == top.children.length) {
910
+ this.trees.pop();
911
+ this.start.pop();
912
+ this.index.pop();
913
+ continue;
914
+ }
915
+ let next = top.children[index];
916
+ let start = this.start[last] + top.positions[index];
917
+ if (start > pos) {
918
+ this.nextStart = start;
919
+ return null;
920
+ }
921
+ if (next instanceof Tree) {
922
+ if (start == pos) {
923
+ if (start < this.safeFrom) return null;
924
+ let end = start + next.length;
925
+ if (end <= this.safeTo) {
926
+ let lookAhead = next.prop(NodeProp.lookAhead);
927
+ if (!lookAhead || end + lookAhead < this.fragment.to) return next;
928
+ }
929
+ }
930
+ this.index[last]++;
931
+ if (start + next.length >= Math.max(this.safeFrom, pos)) {
932
+ // Enter this node
933
+ this.trees.push(next);
934
+ this.start.push(start);
935
+ this.index.push(0);
936
+ }
937
+ } else {
938
+ this.index[last]++;
939
+ this.nextStart = start + next.length;
940
+ }
941
+ }
942
+ }
943
+ }
944
+ class TokenCache {
945
+ constructor(parser, stream) {
946
+ this.stream = stream;
947
+ this.tokens = [];
948
+ this.mainToken = null;
949
+ this.actions = [];
950
+ this.tokens = parser.tokenizers.map(_ => new CachedToken());
951
+ }
952
+ getActions(stack) {
953
+ let actionIndex = 0;
954
+ let main = null;
955
+ let {
956
+ parser
957
+ } = stack.p,
958
+ {
959
+ tokenizers
960
+ } = parser;
961
+ let mask = parser.stateSlot(stack.state, 3 /* ParseState.TokenizerMask */);
962
+ let context = stack.curContext ? stack.curContext.hash : 0;
963
+ let lookAhead = 0;
964
+ for (let i = 0; i < tokenizers.length; i++) {
965
+ if ((1 << i & mask) == 0) continue;
966
+ let tokenizer = tokenizers[i],
967
+ token = this.tokens[i];
968
+ if (main && !tokenizer.fallback) continue;
969
+ if (tokenizer.contextual || token.start != stack.pos || token.mask != mask || token.context != context) {
970
+ this.updateCachedToken(token, tokenizer, stack);
971
+ token.mask = mask;
972
+ token.context = context;
973
+ }
974
+ if (token.lookAhead > token.end + 25 /* Safety.Margin */) lookAhead = Math.max(token.lookAhead, lookAhead);
975
+ if (token.value != 0 /* Term.Err */) {
976
+ let startIndex = actionIndex;
977
+ if (token.extended > -1) actionIndex = this.addActions(stack, token.extended, token.end, actionIndex);
978
+ actionIndex = this.addActions(stack, token.value, token.end, actionIndex);
979
+ if (!tokenizer.extend) {
980
+ main = token;
981
+ if (actionIndex > startIndex) break;
982
+ }
983
+ }
984
+ }
985
+ while (this.actions.length > actionIndex) this.actions.pop();
986
+ if (lookAhead) stack.setLookAhead(lookAhead);
987
+ if (!main && stack.pos == this.stream.end) {
988
+ main = new CachedToken();
989
+ main.value = stack.p.parser.eofTerm;
990
+ main.start = main.end = stack.pos;
991
+ actionIndex = this.addActions(stack, main.value, main.end, actionIndex);
992
+ }
993
+ this.mainToken = main;
994
+ return this.actions;
995
+ }
996
+ getMainToken(stack) {
997
+ if (this.mainToken) return this.mainToken;
998
+ let main = new CachedToken(),
999
+ {
1000
+ pos,
1001
+ p
1002
+ } = stack;
1003
+ main.start = pos;
1004
+ main.end = Math.min(pos + 1, p.stream.end);
1005
+ main.value = pos == p.stream.end ? p.parser.eofTerm : 0 /* Term.Err */;
1006
+ return main;
1007
+ }
1008
+ updateCachedToken(token, tokenizer, stack) {
1009
+ let start = this.stream.clipPos(stack.pos);
1010
+ tokenizer.token(this.stream.reset(start, token), stack);
1011
+ if (token.value > -1) {
1012
+ let {
1013
+ parser
1014
+ } = stack.p;
1015
+ for (let i = 0; i < parser.specialized.length; i++) if (parser.specialized[i] == token.value) {
1016
+ let result = parser.specializers[i](this.stream.read(token.start, token.end), stack);
1017
+ if (result >= 0 && stack.p.parser.dialect.allows(result >> 1)) {
1018
+ if ((result & 1) == 0 /* Specialize.Specialize */) token.value = result >> 1;else token.extended = result >> 1;
1019
+ break;
1020
+ }
1021
+ }
1022
+ } else {
1023
+ token.value = 0 /* Term.Err */;
1024
+ token.end = this.stream.clipPos(start + 1);
1025
+ }
1026
+ }
1027
+ putAction(action, token, end, index) {
1028
+ // Don't add duplicate actions
1029
+ for (let i = 0; i < index; i += 3) if (this.actions[i] == action) return index;
1030
+ this.actions[index++] = action;
1031
+ this.actions[index++] = token;
1032
+ this.actions[index++] = end;
1033
+ return index;
1034
+ }
1035
+ addActions(stack, token, end, index) {
1036
+ let {
1037
+ state
1038
+ } = stack,
1039
+ {
1040
+ parser
1041
+ } = stack.p,
1042
+ {
1043
+ data
1044
+ } = parser;
1045
+ for (let set = 0; set < 2; set++) {
1046
+ for (let i = parser.stateSlot(state, set ? 2 /* ParseState.Skip */ : 1 /* ParseState.Actions */);; i += 3) {
1047
+ if (data[i] == 65535 /* Seq.End */) {
1048
+ if (data[i + 1] == 1 /* Seq.Next */) {
1049
+ i = pair(data, i + 2);
1050
+ } else {
1051
+ if (index == 0 && data[i + 1] == 2 /* Seq.Other */) index = this.putAction(pair(data, i + 2), token, end, index);
1052
+ break;
1053
+ }
1054
+ }
1055
+ if (data[i] == token) index = this.putAction(pair(data, i + 1), token, end, index);
1056
+ }
1057
+ }
1058
+ return index;
1059
+ }
1060
+ }
1061
+ var Rec;
1062
+ (function (Rec) {
1063
+ Rec[Rec["Distance"] = 5] = "Distance";
1064
+ Rec[Rec["MaxRemainingPerStep"] = 3] = "MaxRemainingPerStep";
1065
+ // When two stacks have been running independently long enough to
1066
+ // add this many elements to their buffers, prune one.
1067
+ Rec[Rec["MinBufferLengthPrune"] = 500] = "MinBufferLengthPrune";
1068
+ Rec[Rec["ForceReduceLimit"] = 10] = "ForceReduceLimit";
1069
+ // Once a stack reaches this depth (in .stack.length) force-reduce
1070
+ // it back to CutTo to avoid creating trees that overflow the stack
1071
+ // on recursive traversal.
1072
+ Rec[Rec["CutDepth"] = 15000] = "CutDepth";
1073
+ Rec[Rec["CutTo"] = 9000] = "CutTo";
1074
+ Rec[Rec["MaxLeftAssociativeReductionCount"] = 300] = "MaxLeftAssociativeReductionCount";
1075
+ // The maximum number of non-recovering stacks to explore (to avoid
1076
+ // getting bogged down with exponentially multiplying stacks in
1077
+ // ambiguous content)
1078
+ Rec[Rec["MaxStackCount"] = 12] = "MaxStackCount";
1079
+ })(Rec || (Rec = {}));
1080
+ class Parse {
1081
+ constructor(parser, input, fragments, ranges) {
1082
+ this.parser = parser;
1083
+ this.input = input;
1084
+ this.ranges = ranges;
1085
+ this.recovering = 0;
1086
+ this.nextStackID = 0x2654; // ♔, ♕, ♖, ♗, ♘, ♙, ♠, ♡, ♢, ♣, ♤, ♥, ♦, ♧
1087
+ this.minStackPos = 0;
1088
+ this.reused = [];
1089
+ this.stoppedAt = null;
1090
+ this.lastBigReductionStart = -1;
1091
+ this.lastBigReductionSize = 0;
1092
+ this.bigReductionCount = 0;
1093
+ this.stream = new InputStream(input, ranges);
1094
+ this.tokens = new TokenCache(parser, this.stream);
1095
+ this.topTerm = parser.top[1];
1096
+ let {
1097
+ from
1098
+ } = ranges[0];
1099
+ this.stacks = [Stack.start(this, parser.top[0], from)];
1100
+ this.fragments = fragments.length && this.stream.end - from > parser.bufferLength * 4 ? new FragmentCursor(fragments, parser.nodeSet) : null;
1101
+ }
1102
+ get parsedPos() {
1103
+ return this.minStackPos;
1104
+ }
1105
+ // Move the parser forward. This will process all parse stacks at
1106
+ // `this.pos` and try to advance them to a further position. If no
1107
+ // stack for such a position is found, it'll start error-recovery.
1108
+ //
1109
+ // When the parse is finished, this will return a syntax tree. When
1110
+ // not, it returns `null`.
1111
+ advance() {
1112
+ let stacks = this.stacks,
1113
+ pos = this.minStackPos;
1114
+ // This will hold stacks beyond `pos`.
1115
+ let newStacks = this.stacks = [];
1116
+ let stopped, stoppedTokens;
1117
+ // If a large amount of reductions happened with the same start
1118
+ // position, force the stack out of that production in order to
1119
+ // avoid creating a tree too deep to recurse through.
1120
+ // (This is an ugly kludge, because unfortunately there is no
1121
+ // straightforward, cheap way to check for this happening, due to
1122
+ // the history of reductions only being available in an
1123
+ // expensive-to-access format in the stack buffers.)
1124
+ if (this.bigReductionCount > 300 /* Rec.MaxLeftAssociativeReductionCount */ && stacks.length == 1) {
1125
+ let [s] = stacks;
1126
+ while (s.forceReduce() && s.stack.length && s.stack[s.stack.length - 2] >= this.lastBigReductionStart) {}
1127
+ this.bigReductionCount = this.lastBigReductionSize = 0;
1128
+ }
1129
+ // Keep advancing any stacks at `pos` until they either move
1130
+ // forward or can't be advanced. Gather stacks that can't be
1131
+ // advanced further in `stopped`.
1132
+ for (let i = 0; i < stacks.length; i++) {
1133
+ let stack = stacks[i];
1134
+ for (;;) {
1135
+ this.tokens.mainToken = null;
1136
+ if (stack.pos > pos) {
1137
+ newStacks.push(stack);
1138
+ } else if (this.advanceStack(stack, newStacks, stacks)) {
1139
+ continue;
1140
+ } else {
1141
+ if (!stopped) {
1142
+ stopped = [];
1143
+ stoppedTokens = [];
1144
+ }
1145
+ stopped.push(stack);
1146
+ let tok = this.tokens.getMainToken(stack);
1147
+ stoppedTokens.push(tok.value, tok.end);
1148
+ }
1149
+ break;
1150
+ }
1151
+ }
1152
+ if (!newStacks.length) {
1153
+ let finished = stopped && findFinished(stopped);
1154
+ if (finished) return this.stackToTree(finished);
1155
+ if (this.parser.strict) {
1156
+ if (verbose && stopped) console.log("Stuck with token " + (this.tokens.mainToken ? this.parser.getName(this.tokens.mainToken.value) : "none"));
1157
+ throw new SyntaxError("No parse at " + pos);
1158
+ }
1159
+ if (!this.recovering) this.recovering = 5 /* Rec.Distance */;
1160
+ }
1161
+
1162
+ if (this.recovering && stopped) {
1163
+ let finished = this.stoppedAt != null && stopped[0].pos > this.stoppedAt ? stopped[0] : this.runRecovery(stopped, stoppedTokens, newStacks);
1164
+ if (finished) return this.stackToTree(finished.forceAll());
1165
+ }
1166
+ if (this.recovering) {
1167
+ let maxRemaining = this.recovering == 1 ? 1 : this.recovering * 3 /* Rec.MaxRemainingPerStep */;
1168
+ if (newStacks.length > maxRemaining) {
1169
+ newStacks.sort((a, b) => b.score - a.score);
1170
+ while (newStacks.length > maxRemaining) newStacks.pop();
1171
+ }
1172
+ if (newStacks.some(s => s.reducePos > pos)) this.recovering--;
1173
+ } else if (newStacks.length > 1) {
1174
+ // Prune stacks that are in the same state, or that have been
1175
+ // running without splitting for a while, to avoid getting stuck
1176
+ // with multiple successful stacks running endlessly on.
1177
+ outer: for (let i = 0; i < newStacks.length - 1; i++) {
1178
+ let stack = newStacks[i];
1179
+ for (let j = i + 1; j < newStacks.length; j++) {
1180
+ let other = newStacks[j];
1181
+ if (stack.sameState(other) || stack.buffer.length > 500 /* Rec.MinBufferLengthPrune */ && other.buffer.length > 500 /* Rec.MinBufferLengthPrune */) {
1182
+ if ((stack.score - other.score || stack.buffer.length - other.buffer.length) > 0) {
1183
+ newStacks.splice(j--, 1);
1184
+ } else {
1185
+ newStacks.splice(i--, 1);
1186
+ continue outer;
1187
+ }
1188
+ }
1189
+ }
1190
+ }
1191
+ if (newStacks.length > 12 /* Rec.MaxStackCount */) newStacks.splice(12 /* Rec.MaxStackCount */, newStacks.length - 12 /* Rec.MaxStackCount */);
1192
+ }
1193
+
1194
+ this.minStackPos = newStacks[0].pos;
1195
+ for (let i = 1; i < newStacks.length; i++) if (newStacks[i].pos < this.minStackPos) this.minStackPos = newStacks[i].pos;
1196
+ return null;
1197
+ }
1198
+ stopAt(pos) {
1199
+ if (this.stoppedAt != null && this.stoppedAt < pos) throw new RangeError("Can't move stoppedAt forward");
1200
+ this.stoppedAt = pos;
1201
+ }
1202
+ // Returns an updated version of the given stack, or null if the
1203
+ // stack can't advance normally. When `split` and `stacks` are
1204
+ // given, stacks split off by ambiguous operations will be pushed to
1205
+ // `split`, or added to `stacks` if they move `pos` forward.
1206
+ advanceStack(stack, stacks, split) {
1207
+ let start = stack.pos,
1208
+ {
1209
+ parser
1210
+ } = this;
1211
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1212
+ if (this.stoppedAt != null && start > this.stoppedAt) return stack.forceReduce() ? stack : null;
1213
+ if (this.fragments) {
1214
+ let strictCx = stack.curContext && stack.curContext.tracker.strict,
1215
+ cxHash = strictCx ? stack.curContext.hash : 0;
1216
+ for (let cached = this.fragments.nodeAt(start); cached;) {
1217
+ let match = this.parser.nodeSet.types[cached.type.id] == cached.type ? parser.getGoto(stack.state, cached.type.id) : -1;
1218
+ if (match > -1 && cached.length && (!strictCx || (cached.prop(NodeProp.contextHash) || 0) == cxHash)) {
1219
+ stack.useNode(cached, match);
1220
+ if (verbose) console.log(base + this.stackID(stack) + ` (via reuse of ${parser.getName(cached.type.id)})`);
1221
+ return true;
1222
+ }
1223
+ if (!(cached instanceof Tree) || cached.children.length == 0 || cached.positions[0] > 0) break;
1224
+ let inner = cached.children[0];
1225
+ if (inner instanceof Tree && cached.positions[0] == 0) cached = inner;else break;
1226
+ }
1227
+ }
1228
+ let defaultReduce = parser.stateSlot(stack.state, 4 /* ParseState.DefaultReduce */);
1229
+ if (defaultReduce > 0) {
1230
+ stack.reduce(defaultReduce);
1231
+ if (verbose) console.log(base + this.stackID(stack) + ` (via always-reduce ${parser.getName(defaultReduce & 65535 /* Action.ValueMask */)})`);
1232
+ return true;
1233
+ }
1234
+ if (stack.stack.length >= 15000 /* Rec.CutDepth */) {
1235
+ while (stack.stack.length > 9000 /* Rec.CutTo */ && stack.forceReduce()) {}
1236
+ }
1237
+ let actions = this.tokens.getActions(stack);
1238
+ for (let i = 0; i < actions.length;) {
1239
+ let action = actions[i++],
1240
+ term = actions[i++],
1241
+ end = actions[i++];
1242
+ let last = i == actions.length || !split;
1243
+ let localStack = last ? stack : stack.split();
1244
+ localStack.apply(action, term, end);
1245
+ if (verbose) console.log(base + this.stackID(localStack) + ` (via ${(action & 65536 /* Action.ReduceFlag */) == 0 ? "shift" : `reduce of ${parser.getName(action & 65535 /* Action.ValueMask */)}`} for ${parser.getName(term)} @ ${start}${localStack == stack ? "" : ", split"})`);
1246
+ if (last) return true;else if (localStack.pos > start) stacks.push(localStack);else split.push(localStack);
1247
+ }
1248
+ return false;
1249
+ }
1250
+ // Advance a given stack forward as far as it will go. Returns the
1251
+ // (possibly updated) stack if it got stuck, or null if it moved
1252
+ // forward and was given to `pushStackDedup`.
1253
+ advanceFully(stack, newStacks) {
1254
+ let pos = stack.pos;
1255
+ for (;;) {
1256
+ if (!this.advanceStack(stack, null, null)) return false;
1257
+ if (stack.pos > pos) {
1258
+ pushStackDedup(stack, newStacks);
1259
+ return true;
1260
+ }
1261
+ }
1262
+ }
1263
+ runRecovery(stacks, tokens, newStacks) {
1264
+ let finished = null,
1265
+ restarted = false;
1266
+ for (let i = 0; i < stacks.length; i++) {
1267
+ let stack = stacks[i],
1268
+ token = tokens[i << 1],
1269
+ tokenEnd = tokens[(i << 1) + 1];
1270
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1271
+ if (stack.deadEnd) {
1272
+ if (restarted) continue;
1273
+ restarted = true;
1274
+ stack.restart();
1275
+ if (verbose) console.log(base + this.stackID(stack) + " (restarted)");
1276
+ let done = this.advanceFully(stack, newStacks);
1277
+ if (done) continue;
1278
+ }
1279
+ let force = stack.split(),
1280
+ forceBase = base;
1281
+ for (let j = 0; force.forceReduce() && j < 10 /* Rec.ForceReduceLimit */; j++) {
1282
+ if (verbose) console.log(forceBase + this.stackID(force) + " (via force-reduce)");
1283
+ let done = this.advanceFully(force, newStacks);
1284
+ if (done) break;
1285
+ if (verbose) forceBase = this.stackID(force) + " -> ";
1286
+ }
1287
+ for (let insert of stack.recoverByInsert(token)) {
1288
+ if (verbose) console.log(base + this.stackID(insert) + " (via recover-insert)");
1289
+ this.advanceFully(insert, newStacks);
1290
+ }
1291
+ if (this.stream.end > stack.pos) {
1292
+ if (tokenEnd == stack.pos) {
1293
+ tokenEnd++;
1294
+ token = 0 /* Term.Err */;
1295
+ }
1296
+
1297
+ stack.recoverByDelete(token, tokenEnd);
1298
+ if (verbose) console.log(base + this.stackID(stack) + ` (via recover-delete ${this.parser.getName(token)})`);
1299
+ pushStackDedup(stack, newStacks);
1300
+ } else if (!finished || finished.score < stack.score) {
1301
+ finished = stack;
1302
+ }
1303
+ }
1304
+ return finished;
1305
+ }
1306
+ // Convert the stack's buffer to a syntax tree.
1307
+ stackToTree(stack) {
1308
+ stack.close();
1309
+ return Tree.build({
1310
+ buffer: StackBufferCursor.create(stack),
1311
+ nodeSet: this.parser.nodeSet,
1312
+ topID: this.topTerm,
1313
+ maxBufferLength: this.parser.bufferLength,
1314
+ reused: this.reused,
1315
+ start: this.ranges[0].from,
1316
+ length: stack.pos - this.ranges[0].from,
1317
+ minRepeatType: this.parser.minRepeatTerm
1318
+ });
1319
+ }
1320
+ stackID(stack) {
1321
+ let id = (stackIDs || (stackIDs = new WeakMap())).get(stack);
1322
+ if (!id) stackIDs.set(stack, id = String.fromCodePoint(this.nextStackID++));
1323
+ return id + stack;
1324
+ }
1325
+ }
1326
+ function pushStackDedup(stack, newStacks) {
1327
+ for (let i = 0; i < newStacks.length; i++) {
1328
+ let other = newStacks[i];
1329
+ if (other.pos == stack.pos && other.sameState(stack)) {
1330
+ if (newStacks[i].score < stack.score) newStacks[i] = stack;
1331
+ return;
1332
+ }
1333
+ }
1334
+ newStacks.push(stack);
1335
+ }
1336
+ class Dialect {
1337
+ constructor(source, flags, disabled) {
1338
+ this.source = source;
1339
+ this.flags = flags;
1340
+ this.disabled = disabled;
1341
+ }
1342
+ allows(term) {
1343
+ return !this.disabled || this.disabled[term] == 0;
1344
+ }
1345
+ }
1346
+ const id = x => x;
1347
+ /// Context trackers are used to track stateful context (such as
1348
+ /// indentation in the Python grammar, or parent elements in the XML
1349
+ /// grammar) needed by external tokenizers. You declare them in a
1350
+ /// grammar file as `@context exportName from "module"`.
1351
+ ///
1352
+ /// Context values should be immutable, and can be updated (replaced)
1353
+ /// on shift or reduce actions.
1354
+ ///
1355
+ /// The export used in a `@context` declaration should be of this
1356
+ /// type.
1357
+ class ContextTracker {
1358
+ /// Define a context tracker.
1359
+ constructor(spec) {
1360
+ this.start = spec.start;
1361
+ this.shift = spec.shift || id;
1362
+ this.reduce = spec.reduce || id;
1363
+ this.reuse = spec.reuse || id;
1364
+ this.hash = spec.hash || (() => 0);
1365
+ this.strict = spec.strict !== false;
1366
+ }
1367
+ }
1368
+ /// Holds the parse tables for a given grammar, as generated by
1369
+ /// `lezer-generator`, and provides [methods](#common.Parser) to parse
1370
+ /// content with.
1371
+ class LRParser extends Parser {
1372
+ /// @internal
1373
+ constructor(spec) {
1374
+ super();
1375
+ /// @internal
1376
+ this.wrappers = [];
1377
+ if (spec.version != 14 /* File.Version */) throw new RangeError(`Parser version (${spec.version}) doesn't match runtime version (${14 /* File.Version */})`);
1378
+ let nodeNames = spec.nodeNames.split(" ");
1379
+ this.minRepeatTerm = nodeNames.length;
1380
+ for (let i = 0; i < spec.repeatNodeCount; i++) nodeNames.push("");
1381
+ let topTerms = Object.keys(spec.topRules).map(r => spec.topRules[r][1]);
1382
+ let nodeProps = [];
1383
+ for (let i = 0; i < nodeNames.length; i++) nodeProps.push([]);
1384
+ function setProp(nodeID, prop, value) {
1385
+ nodeProps[nodeID].push([prop, prop.deserialize(String(value))]);
1386
+ }
1387
+ if (spec.nodeProps) for (let propSpec of spec.nodeProps) {
1388
+ let prop = propSpec[0];
1389
+ if (typeof prop == "string") prop = NodeProp[prop];
1390
+ for (let i = 1; i < propSpec.length;) {
1391
+ let next = propSpec[i++];
1392
+ if (next >= 0) {
1393
+ setProp(next, prop, propSpec[i++]);
1394
+ } else {
1395
+ let value = propSpec[i + -next];
1396
+ for (let j = -next; j > 0; j--) setProp(propSpec[i++], prop, value);
1397
+ i++;
1398
+ }
1399
+ }
1400
+ }
1401
+ this.nodeSet = new NodeSet(nodeNames.map((name, i) => NodeType.define({
1402
+ name: i >= this.minRepeatTerm ? undefined : name,
1403
+ id: i,
1404
+ props: nodeProps[i],
1405
+ top: topTerms.indexOf(i) > -1,
1406
+ error: i == 0,
1407
+ skipped: spec.skippedNodes && spec.skippedNodes.indexOf(i) > -1
1408
+ })));
1409
+ if (spec.propSources) this.nodeSet = this.nodeSet.extend(...spec.propSources);
1410
+ this.strict = false;
1411
+ this.bufferLength = DefaultBufferLength;
1412
+ let tokenArray = decodeArray(spec.tokenData);
1413
+ this.context = spec.context;
1414
+ this.specializerSpecs = spec.specialized || [];
1415
+ this.specialized = new Uint16Array(this.specializerSpecs.length);
1416
+ for (let i = 0; i < this.specializerSpecs.length; i++) this.specialized[i] = this.specializerSpecs[i].term;
1417
+ this.specializers = this.specializerSpecs.map(getSpecializer);
1418
+ this.states = decodeArray(spec.states, Uint32Array);
1419
+ this.data = decodeArray(spec.stateData);
1420
+ this.goto = decodeArray(spec.goto);
1421
+ this.maxTerm = spec.maxTerm;
1422
+ this.tokenizers = spec.tokenizers.map(value => typeof value == "number" ? new TokenGroup(tokenArray, value) : value);
1423
+ this.topRules = spec.topRules;
1424
+ this.dialects = spec.dialects || {};
1425
+ this.dynamicPrecedences = spec.dynamicPrecedences || null;
1426
+ this.tokenPrecTable = spec.tokenPrec;
1427
+ this.termNames = spec.termNames || null;
1428
+ this.maxNode = this.nodeSet.types.length - 1;
1429
+ this.dialect = this.parseDialect();
1430
+ this.top = this.topRules[Object.keys(this.topRules)[0]];
1431
+ }
1432
+ createParse(input, fragments, ranges) {
1433
+ let parse = new Parse(this, input, fragments, ranges);
1434
+ for (let w of this.wrappers) parse = w(parse, input, fragments, ranges);
1435
+ return parse;
1436
+ }
1437
+ /// Get a goto table entry @internal
1438
+ getGoto(state, term) {
1439
+ let loose = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1440
+ let table = this.goto;
1441
+ if (term >= table[0]) return -1;
1442
+ for (let pos = table[term + 1];;) {
1443
+ let groupTag = table[pos++],
1444
+ last = groupTag & 1;
1445
+ let target = table[pos++];
1446
+ if (last && loose) return target;
1447
+ for (let end = pos + (groupTag >> 1); pos < end; pos++) if (table[pos] == state) return target;
1448
+ if (last) return -1;
1449
+ }
1450
+ }
1451
+ /// Check if this state has an action for a given terminal @internal
1452
+ hasAction(state, terminal) {
1453
+ let data = this.data;
1454
+ for (let set = 0; set < 2; set++) {
1455
+ for (let i = this.stateSlot(state, set ? 2 /* ParseState.Skip */ : 1 /* ParseState.Actions */), next;; i += 3) {
1456
+ if ((next = data[i]) == 65535 /* Seq.End */) {
1457
+ if (data[i + 1] == 1 /* Seq.Next */) next = data[i = pair(data, i + 2)];else if (data[i + 1] == 2 /* Seq.Other */) return pair(data, i + 2);else break;
1458
+ }
1459
+ if (next == terminal || next == 0 /* Term.Err */) return pair(data, i + 1);
1460
+ }
1461
+ }
1462
+ return 0;
1463
+ }
1464
+ /// @internal
1465
+ stateSlot(state, slot) {
1466
+ return this.states[state * 6 /* ParseState.Size */ + slot];
1467
+ }
1468
+ /// @internal
1469
+ stateFlag(state, flag) {
1470
+ return (this.stateSlot(state, 0 /* ParseState.Flags */) & flag) > 0;
1471
+ }
1472
+ /// @internal
1473
+ validAction(state, action) {
1474
+ if (action == this.stateSlot(state, 4 /* ParseState.DefaultReduce */)) return true;
1475
+ for (let i = this.stateSlot(state, 1 /* ParseState.Actions */);; i += 3) {
1476
+ if (this.data[i] == 65535 /* Seq.End */) {
1477
+ if (this.data[i + 1] == 1 /* Seq.Next */) i = pair(this.data, i + 2);else return false;
1478
+ }
1479
+ if (action == pair(this.data, i + 1)) return true;
1480
+ }
1481
+ }
1482
+ /// Get the states that can follow this one through shift actions or
1483
+ /// goto jumps. @internal
1484
+ nextStates(state) {
1485
+ let result = [];
1486
+ for (let i = this.stateSlot(state, 1 /* ParseState.Actions */);; i += 3) {
1487
+ if (this.data[i] == 65535 /* Seq.End */) {
1488
+ if (this.data[i + 1] == 1 /* Seq.Next */) i = pair(this.data, i + 2);else break;
1489
+ }
1490
+ if ((this.data[i + 2] & 65536 /* Action.ReduceFlag */ >> 16) == 0) {
1491
+ let value = this.data[i + 1];
1492
+ if (!result.some((v, i) => i & 1 && v == value)) result.push(this.data[i], value);
1493
+ }
1494
+ }
1495
+ return result;
1496
+ }
1497
+ /// Configure the parser. Returns a new parser instance that has the
1498
+ /// given settings modified. Settings not provided in `config` are
1499
+ /// kept from the original parser.
1500
+ configure(config) {
1501
+ // Hideous reflection-based kludge to make it easy to create a
1502
+ // slightly modified copy of a parser.
1503
+ let copy = Object.assign(Object.create(LRParser.prototype), this);
1504
+ if (config.props) copy.nodeSet = this.nodeSet.extend(...config.props);
1505
+ if (config.top) {
1506
+ let info = this.topRules[config.top];
1507
+ if (!info) throw new RangeError(`Invalid top rule name ${config.top}`);
1508
+ copy.top = info;
1509
+ }
1510
+ if (config.tokenizers) copy.tokenizers = this.tokenizers.map(t => {
1511
+ let found = config.tokenizers.find(r => r.from == t);
1512
+ return found ? found.to : t;
1513
+ });
1514
+ if (config.specializers) {
1515
+ copy.specializers = this.specializers.slice();
1516
+ copy.specializerSpecs = this.specializerSpecs.map((s, i) => {
1517
+ let found = config.specializers.find(r => r.from == s.external);
1518
+ if (!found) return s;
1519
+ let spec = Object.assign(Object.assign({}, s), {
1520
+ external: found.to
1521
+ });
1522
+ copy.specializers[i] = getSpecializer(spec);
1523
+ return spec;
1524
+ });
1525
+ }
1526
+ if (config.contextTracker) copy.context = config.contextTracker;
1527
+ if (config.dialect) copy.dialect = this.parseDialect(config.dialect);
1528
+ if (config.strict != null) copy.strict = config.strict;
1529
+ if (config.wrap) copy.wrappers = copy.wrappers.concat(config.wrap);
1530
+ if (config.bufferLength != null) copy.bufferLength = config.bufferLength;
1531
+ return copy;
1532
+ }
1533
+ /// Tells you whether any [parse wrappers](#lr.ParserConfig.wrap)
1534
+ /// are registered for this parser.
1535
+ hasWrappers() {
1536
+ return this.wrappers.length > 0;
1537
+ }
1538
+ /// Returns the name associated with a given term. This will only
1539
+ /// work for all terms when the parser was generated with the
1540
+ /// `--names` option. By default, only the names of tagged terms are
1541
+ /// stored.
1542
+ getName(term) {
1543
+ return this.termNames ? this.termNames[term] : String(term <= this.maxNode && this.nodeSet.types[term].name || term);
1544
+ }
1545
+ /// The eof term id is always allocated directly after the node
1546
+ /// types. @internal
1547
+ get eofTerm() {
1548
+ return this.maxNode + 1;
1549
+ }
1550
+ /// The type of top node produced by the parser.
1551
+ get topNode() {
1552
+ return this.nodeSet.types[this.top[1]];
1553
+ }
1554
+ /// @internal
1555
+ dynamicPrecedence(term) {
1556
+ let prec = this.dynamicPrecedences;
1557
+ return prec == null ? 0 : prec[term] || 0;
1558
+ }
1559
+ /// @internal
1560
+ parseDialect(dialect) {
1561
+ let values = Object.keys(this.dialects),
1562
+ flags = values.map(() => false);
1563
+ if (dialect) for (let part of dialect.split(" ")) {
1564
+ let id = values.indexOf(part);
1565
+ if (id >= 0) flags[id] = true;
1566
+ }
1567
+ let disabled = null;
1568
+ for (let i = 0; i < values.length; i++) if (!flags[i]) {
1569
+ for (let j = this.dialects[values[i]], id; (id = this.data[j++]) != 65535 /* Seq.End */;) (disabled || (disabled = new Uint8Array(this.maxTerm + 1)))[id] = 1;
1570
+ }
1571
+ return new Dialect(dialect, flags, disabled);
1572
+ }
1573
+ /// Used by the output of the parser generator. Not available to
1574
+ /// user code. @hide
1575
+ static deserialize(spec) {
1576
+ return new LRParser(spec);
1577
+ }
1578
+ }
1579
+ function pair(data, off) {
1580
+ return data[off] | data[off + 1] << 16;
1581
+ }
1582
+ function findFinished(stacks) {
1583
+ let best = null;
1584
+ for (let stack of stacks) {
1585
+ let stopped = stack.p.stoppedAt;
1586
+ if ((stack.pos == stack.p.stream.end || stopped != null && stack.pos > stopped) && stack.p.parser.stateFlag(stack.state, 2 /* StateFlag.Accepting */) && (!best || best.score < stack.score)) best = stack;
1587
+ }
1588
+ return best;
1589
+ }
1590
+ function getSpecializer(spec) {
1591
+ if (spec.external) {
1592
+ let mask = spec.extend ? 1 /* Specialize.Extend */ : 0 /* Specialize.Specialize */;
1593
+ return (value, stack) => spec.external(value, stack) << 1 | mask;
1594
+ }
1595
+ return spec.get;
1596
+ }
1597
+
1598
+ export { ContextTracker as C, ExternalTokenizer as E, LRParser as L, LocalTokenGroup as a };