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