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