feelin 4.3.0

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