@vuu-ui/vuu-data-local 0.8.33 → 0.8.34

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