@aleph-alpha/chat-kit 5.13.0 → 6.1.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 (199) hide show
  1. package/dist/{CkHistory.vue_vue_type_script_setup_true_lang-BNcJURkq.js → CkHistory.vue_vue_type_script_setup_true_lang-Cr8vF94e.js} +221 -115
  2. package/dist/adapters/responses-api.d.ts +1 -1
  3. package/dist/adapters/responses-api.d.ts.map +1 -1
  4. package/dist/adapters/responses-api.js +9 -7
  5. package/dist/apl-fqmucPXA.js +140 -0
  6. package/dist/asciiarmor-DucZyvP0.js +56 -0
  7. package/dist/asn1-BnOEsgAm.js +144 -0
  8. package/dist/asterisk-QAlztEwS.js +345 -0
  9. package/dist/brainfuck-DZVCuF_t.js +53 -0
  10. package/dist/chat-kit.css +1177 -12
  11. package/dist/clike-CTYGlS6x.js +799 -0
  12. package/dist/clojure-BhXMqnxz.js +849 -0
  13. package/dist/cmake-BGaNd9E7.js +71 -0
  14. package/dist/cobol-4yqQntpt.js +120 -0
  15. package/dist/coffeescript-D2dXvhEc.js +308 -0
  16. package/dist/commonlisp-CF_VNHQR.js +130 -0
  17. package/dist/components/base/CkHistoryItem/CkHistoryItem.vue.d.ts.map +1 -1
  18. package/dist/components/base/CkHistoryWrapper/CkHistoryWrapper.vue.d.ts.map +1 -1
  19. package/dist/components/base/CkMarkdownRenderer/CkMarkdownRenderer.stories.d.ts.map +1 -1
  20. package/dist/components/base/CkMilkdownEditor/CkMilkdownEditor.stories.d.ts +41 -0
  21. package/dist/components/base/CkMilkdownEditor/CkMilkdownEditor.stories.d.ts.map +1 -0
  22. package/dist/components/base/CkMilkdownEditor/CkMilkdownEditor.vue.d.ts +26 -0
  23. package/dist/components/base/CkMilkdownEditor/CkMilkdownEditor.vue.d.ts.map +1 -0
  24. package/dist/components/base/CkMilkdownEditor/codeMirrorTheme.d.ts +20 -0
  25. package/dist/components/base/CkMilkdownEditor/codeMirrorTheme.d.ts.map +1 -0
  26. package/dist/components/base/CkMilkdownEditor/createCrepeEditor.d.ts +42 -0
  27. package/dist/components/base/CkMilkdownEditor/createCrepeEditor.d.ts.map +1 -0
  28. package/dist/components/base/CkMilkdownEditor/crepeIcons.d.ts +33 -0
  29. package/dist/components/base/CkMilkdownEditor/crepeIcons.d.ts.map +1 -0
  30. package/dist/components/base/CkMilkdownEditor/index.d.ts +3 -0
  31. package/dist/components/base/CkMilkdownEditor/index.d.ts.map +1 -0
  32. package/dist/components/base/CkMilkdownEditor/markdownClassAttrs.d.ts +16 -0
  33. package/dist/components/base/CkMilkdownEditor/markdownClassAttrs.d.ts.map +1 -0
  34. package/dist/components/base/CkMilkdownEditor/types.d.ts +98 -0
  35. package/dist/components/base/CkMilkdownEditor/types.d.ts.map +1 -0
  36. package/dist/components/base/index.d.ts +2 -0
  37. package/dist/components/base/index.d.ts.map +1 -1
  38. package/dist/components/index.d.ts +2 -2
  39. package/dist/components/index.d.ts.map +1 -1
  40. package/dist/components/index.js +12 -11
  41. package/dist/composables/fileUpload/useFileUpload.d.ts.map +1 -1
  42. package/dist/composables/index.js +1 -1
  43. package/dist/composables/useChat.d.ts.map +1 -1
  44. package/dist/composables/useConversations.d.ts.map +1 -1
  45. package/dist/createCrepeEditor-CFbvSH3s.js +74141 -0
  46. package/dist/crystal-DyuLTqLs.js +398 -0
  47. package/dist/css-DkSyTW67.js +1772 -0
  48. package/dist/cypher-Dlu_3r4V.js +121 -0
  49. package/dist/d-UURgV0Ux.js +179 -0
  50. package/dist/diff-B_Bi2Crb.js +25 -0
  51. package/dist/dockerfile-Bvk733Ga.js +201 -0
  52. package/dist/dtd-Dy74G54E.js +114 -0
  53. package/dist/dylan-TSb-Nfix.js +314 -0
  54. package/dist/ebnf-4fKAGW3a.js +140 -0
  55. package/dist/ecl-B59qGGVg.js +178 -0
  56. package/dist/eiffel-Dze7nlu3.js +134 -0
  57. package/dist/elm-DG7jkhNZ.js +176 -0
  58. package/dist/erlang-BO6gOnGA.js +674 -0
  59. package/dist/factor-CMxFHDqz.js +65 -0
  60. package/dist/fcl-CDDUNjTj.js +141 -0
  61. package/dist/forth-B9D2JCeE.js +116 -0
  62. package/dist/fortran-CAG2BFbe.js +467 -0
  63. package/dist/gas-0Aw8zDr5.js +293 -0
  64. package/dist/gherkin-DhZlEZiy.js +115 -0
  65. package/dist/groovy-CpwJiBl7.js +223 -0
  66. package/dist/handlers/index.js +1 -1
  67. package/dist/handlers/useChatKitLabels.d.ts +62 -1
  68. package/dist/handlers/useChatKitLabels.d.ts.map +1 -1
  69. package/dist/haskell-ySd-OUo8.js +459 -0
  70. package/dist/haxe-7MlzfeYV.js +514 -0
  71. package/dist/http-BqypyemW.js +79 -0
  72. package/dist/idl-4HIGJlDI.js +985 -0
  73. package/dist/index-A5rWjRtZ.js +689 -0
  74. package/dist/index-B0ldhzaj.js +385 -0
  75. package/dist/index-BVOjyn3y.js +2490 -0
  76. package/dist/index-BWvkps_G.js +309 -0
  77. package/dist/index-BaS8OW8V.js +151 -0
  78. package/dist/index-BlHHEbV-.js +406 -0
  79. package/dist/index-BoqzHVNB.js +82 -0
  80. package/dist/index-C6zYjwIQ.js +704 -0
  81. package/dist/{index-Dn4ctxk8.js → index-C72e57-5.js} +48 -24
  82. package/dist/index-CMJGuXap.js +330 -0
  83. package/dist/index-CRfGWVdQ.js +116 -0
  84. package/dist/index-C_gTb50u.js +81 -0
  85. package/dist/index-DSU7cBGz.js +178 -0
  86. package/dist/index-DSq3gvqU.js +1767 -0
  87. package/dist/index-Da3777l2.js +289 -0
  88. package/dist/index-DdlzabmR.js +1021 -0
  89. package/dist/index-DhY_9bdS.js +61 -0
  90. package/dist/index-Dm6C1blN.js +1052 -0
  91. package/dist/index-Tj3pzx-E.js +327 -0
  92. package/dist/index-ZeRS5rER.js +97 -0
  93. package/dist/index-gzleljO6.js +157 -0
  94. package/dist/index-naWlQpaL.js +642 -0
  95. package/dist/index-sKO1lHHC.js +312 -0
  96. package/dist/index.d.ts +2 -2
  97. package/dist/index.d.ts.map +1 -1
  98. package/dist/index.js +15 -14
  99. package/dist/javascript-Bt8B7yTi.js +992 -0
  100. package/dist/julia-Bs6JJhYG.js +407 -0
  101. package/dist/livescript-DmzgM3Yt.js +296 -0
  102. package/dist/lua-Cvg8VrAA.js +256 -0
  103. package/dist/mathematica-DNLOL9PQ.js +110 -0
  104. package/dist/mbox-Ga7d4MMN.js +117 -0
  105. package/dist/mirc-Dma3B8rS.js +107 -0
  106. package/dist/mllike-DHn7xckP.js +334 -0
  107. package/dist/modelica-0d55jYY0.js +147 -0
  108. package/dist/mscgen-DdqZYINH.js +135 -0
  109. package/dist/mumps-Btr8VblO.js +93 -0
  110. package/dist/nginx-DTDtBDVN.js +141 -0
  111. package/dist/nsis-CyKBdPWp.js +62 -0
  112. package/dist/ntriples-CvgOYMpL.js +153 -0
  113. package/dist/octave-DYBj3-tl.js +200 -0
  114. package/dist/oz-R_e8WMIi.js +231 -0
  115. package/dist/pascal-GD8iposT.js +105 -0
  116. package/dist/perl-DL9mHpoi.js +1105 -0
  117. package/dist/pig-C_4T4YIV.js +101 -0
  118. package/dist/powershell-B0suO7Vd.js +328 -0
  119. package/dist/properties-BR-vP1aU.js +58 -0
  120. package/dist/protobuf-BxgpyhoW.js +77 -0
  121. package/dist/pug-vHnWR0UE.js +405 -0
  122. package/dist/puppet-Bdao66PW.js +137 -0
  123. package/dist/python-BFGRmuZ9.js +426 -0
  124. package/dist/q-CrbCVq4a.js +131 -0
  125. package/dist/r-V7nswm59.js +170 -0
  126. package/dist/rpm-C-DLY-If.js +109 -0
  127. package/dist/ruby-CQTpZw6f.js +331 -0
  128. package/dist/sas-D2UG-yhZ.js +207 -0
  129. package/dist/scheme-BKzrkGJD.js +222 -0
  130. package/dist/shell-BlsXDxCn.js +222 -0
  131. package/dist/sieve-CjwBwOY5.js +135 -0
  132. package/dist/simple-mode-DMneyfDu.js +130 -0
  133. package/dist/smalltalk-BOIGQuhN.js +121 -0
  134. package/dist/solr-CwD7U71z.js +69 -0
  135. package/dist/sparql-DYskk2vE.js +249 -0
  136. package/dist/spreadsheet-Bgtt3oLP.js +87 -0
  137. package/dist/sql-Cei9CMfk.js +342 -0
  138. package/dist/stex-C1nZSzAw.js +230 -0
  139. package/dist/stylus-BkS-boTH.js +565 -0
  140. package/dist/swift-FRZi1uvB.js +291 -0
  141. package/dist/tcl-CUcaCdmq.js +114 -0
  142. package/dist/test-utils/createTextPlugin.d.ts +7 -1
  143. package/dist/test-utils/createTextPlugin.d.ts.map +1 -1
  144. package/dist/textile-BnFpjsrl.js +414 -0
  145. package/dist/tiddlywiki-CjprD-Qp.js +218 -0
  146. package/dist/tiki-DK9DOeWn.js +268 -0
  147. package/dist/toml-BOuWGMcf.js +76 -0
  148. package/dist/troff-E1bJ0PPL.js +61 -0
  149. package/dist/ttcn-cfg-Dc39-fIP.js +133 -0
  150. package/dist/ttcn-tKd4HLu4.js +192 -0
  151. package/dist/turtle-Dq7-1WAf.js +124 -0
  152. package/dist/{useChatKitLabels-CMOr3Kjq.js → useChatKitLabels-Ww1Q7xUr.js} +26 -3
  153. package/dist/vb-Dp90gtsv.js +196 -0
  154. package/dist/vbscript-Bfn8O8I7.js +478 -0
  155. package/dist/velocity-BwIZK1TH.js +149 -0
  156. package/dist/verilog-CnT9bMk0.js +429 -0
  157. package/dist/vhdl-DCkMIyT9.js +158 -0
  158. package/dist/webidl-BTLTThCm.js +204 -0
  159. package/dist/xquery-BrBUuxMR.js +525 -0
  160. package/dist/yacas-b5lAVEIl.js +130 -0
  161. package/dist/z80-ClgwfNdB.js +92 -0
  162. package/package.json +11 -5
  163. package/src/adapters/responses-api.spec.ts +86 -0
  164. package/src/adapters/responses-api.ts +13 -9
  165. package/src/components/base/CkCitationCard/CkCitationCard.vue +1 -1
  166. package/src/components/base/CkCitationLabel/CkCitationLabel.vue +1 -1
  167. package/src/components/base/CkConversationLayout/CkConversationLayout.spec.ts +1 -5
  168. package/src/components/base/CkCopyMessageButton/CkCopyMessageButton.spec.ts +1 -1
  169. package/src/components/base/CkFeedbackButton/CkFeedbackButton.vue +3 -3
  170. package/src/components/base/CkFeedbackDialog/CkFeedbackDialog.vue +9 -9
  171. package/src/components/base/CkHistoryItem/CkHistoryItem.vue +5 -7
  172. package/src/components/base/CkHistoryWrapper/CkHistoryWrapper.vue +2 -1
  173. package/src/components/base/CkMarkdownRenderer/CkMarkdownRenderer.stories.ts +9 -4
  174. package/src/components/base/CkMarkdownRenderer/CkMarkdownRenderer.vue +1 -1
  175. package/src/components/base/CkMilkdownEditor/CkMilkdownEditor.spec.ts +221 -0
  176. package/src/components/base/CkMilkdownEditor/CkMilkdownEditor.stories.ts +274 -0
  177. package/src/components/base/CkMilkdownEditor/CkMilkdownEditor.vue +151 -0
  178. package/src/components/base/CkMilkdownEditor/codeMirrorTheme.ts +132 -0
  179. package/src/components/base/CkMilkdownEditor/createCrepeEditor.ts +192 -0
  180. package/src/components/base/CkMilkdownEditor/crepeIcons.ts +77 -0
  181. package/src/components/base/CkMilkdownEditor/index.ts +2 -0
  182. package/src/components/base/CkMilkdownEditor/markdownClassAttrs.ts +64 -0
  183. package/src/components/base/CkMilkdownEditor/milkdown-theme.css +441 -0
  184. package/src/components/base/CkMilkdownEditor/types.ts +100 -0
  185. package/src/components/base/CkModelSelector/CkModelSelector.vue +3 -3
  186. package/src/components/base/CkPromptInputDisclaimer/CkPromptInputDisclaimer.stories.ts +2 -2
  187. package/src/components/base/index.ts +6 -0
  188. package/src/components/index.ts +4 -0
  189. package/src/composables/fileUpload/useFileUpload.ts +45 -30
  190. package/src/composables/fileUpload/useFileUploadWrapper.spec.ts +1 -1
  191. package/src/composables/fileUpload/useFileUploadWrapper.ts +3 -3
  192. package/src/composables/useChat.ts +3 -1
  193. package/src/composables/useConversations.ts +8 -5
  194. package/src/handlers/useChatKitLabels.spec.ts +0 -1
  195. package/src/handlers/useChatKitLabels.ts +29 -2
  196. package/src/handlers/useMarkdownParser.ts +1 -1
  197. package/src/index.ts +4 -0
  198. package/src/test-setup.ts +3 -3
  199. package/src/test-utils/createTextPlugin.ts +11 -3
@@ -0,0 +1,1767 @@
1
+ import { P as Parser, N as NodeSet, k as NodeType, D as DefaultBufferLength, l as NodeProp, T as Tree, I as IterMode } from "./createCrepeEditor-CFbvSH3s.js";
2
+ class Stack {
3
+ /**
4
+ @internal
5
+ */
6
+ constructor(p, stack, state, reducePos, pos, score, buffer, bufferBase, curContext, lookAhead = 0, parent) {
7
+ this.p = p;
8
+ this.stack = stack;
9
+ this.state = state;
10
+ this.reducePos = reducePos;
11
+ this.pos = pos;
12
+ this.score = score;
13
+ this.buffer = buffer;
14
+ this.bufferBase = bufferBase;
15
+ this.curContext = curContext;
16
+ this.lookAhead = lookAhead;
17
+ this.parent = parent;
18
+ }
19
+ /**
20
+ @internal
21
+ */
22
+ toString() {
23
+ return `[${this.stack.filter((_, i) => i % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`;
24
+ }
25
+ // Start an empty stack
26
+ /**
27
+ @internal
28
+ */
29
+ static start(p, state, pos = 0) {
30
+ let cx = p.parser.context;
31
+ return new Stack(p, [], state, pos, pos, 0, [], 0, cx ? new StackContext(cx, cx.start) : null, 0, null);
32
+ }
33
+ /**
34
+ The stack's current [context](#lr.ContextTracker) value, if
35
+ any. Its type will depend on the context tracker's type
36
+ parameter, or it will be `null` if there is no context
37
+ tracker.
38
+ */
39
+ get context() {
40
+ return this.curContext ? this.curContext.context : null;
41
+ }
42
+ // Push a state onto the stack, tracking its start position as well
43
+ // as the buffer base at that point.
44
+ /**
45
+ @internal
46
+ */
47
+ pushState(state, start) {
48
+ this.stack.push(this.state, start, this.bufferBase + this.buffer.length);
49
+ this.state = state;
50
+ }
51
+ // Apply a reduce action
52
+ /**
53
+ @internal
54
+ */
55
+ reduce(action) {
56
+ var _a;
57
+ let depth = action >> 19, type = action & 65535;
58
+ let { parser } = this.p;
59
+ let lookaheadRecord = this.reducePos < this.pos - 25 && this.setLookAhead(this.pos);
60
+ let dPrec = parser.dynamicPrecedence(type);
61
+ if (dPrec)
62
+ this.score += dPrec;
63
+ if (depth == 0) {
64
+ if (type < parser.minRepeatTerm && this.reducePos < this.pos)
65
+ this.reducePos = this.pos;
66
+ this.pushState(parser.getGoto(this.state, type, true), this.reducePos);
67
+ if (type < parser.minRepeatTerm)
68
+ this.storeNode(type, this.reducePos, this.reducePos, lookaheadRecord ? 8 : 4, true);
69
+ this.reduceContext(type, this.reducePos);
70
+ return;
71
+ }
72
+ let base = this.stack.length - (depth - 1) * 3 - (action & 262144 ? 6 : 0);
73
+ let start = base ? this.stack[base - 2] : this.p.ranges[0].from;
74
+ if (type < parser.minRepeatTerm && start == this.reducePos && this.reducePos < this.pos)
75
+ this.reducePos = this.pos;
76
+ let size = this.reducePos - start;
77
+ if (size >= 2e3 && !((_a = this.p.parser.nodeSet.types[type]) === null || _a === void 0 ? void 0 : _a.isAnonymous)) {
78
+ if (start == this.p.lastBigReductionStart) {
79
+ this.p.bigReductionCount++;
80
+ this.p.lastBigReductionSize = size;
81
+ } else if (this.p.lastBigReductionSize < size) {
82
+ this.p.bigReductionCount = 1;
83
+ this.p.lastBigReductionStart = start;
84
+ this.p.lastBigReductionSize = size;
85
+ }
86
+ }
87
+ let bufferBase = base ? this.stack[base - 1] : 0, count = this.bufferBase + this.buffer.length - bufferBase;
88
+ if (type < parser.minRepeatTerm || action & 131072) {
89
+ let pos = parser.stateFlag(
90
+ this.state,
91
+ 1
92
+ /* StateFlag.Skipped */
93
+ ) ? this.pos : this.reducePos;
94
+ this.storeNode(type, start, pos, count + 4, true);
95
+ }
96
+ if (action & 262144) {
97
+ this.state = this.stack[base];
98
+ } else {
99
+ let baseStateID = this.stack[base - 3];
100
+ this.state = parser.getGoto(baseStateID, type, true);
101
+ }
102
+ while (this.stack.length > base)
103
+ this.stack.pop();
104
+ this.reduceContext(type, start);
105
+ }
106
+ // Shift a value into the buffer
107
+ /**
108
+ @internal
109
+ */
110
+ storeNode(term, start, end, size = 4, mustSink = false) {
111
+ if (term == 0 && (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) {
112
+ let top = this.buffer.length;
113
+ if (top > 0 && this.buffer[top - 4] == 0 && this.buffer[top - 1] > -1) {
114
+ if (start == end)
115
+ return;
116
+ if (this.buffer[top - 2] >= start) {
117
+ this.buffer[top - 2] = end;
118
+ return;
119
+ }
120
+ }
121
+ }
122
+ if (!mustSink || this.pos == end) {
123
+ this.buffer.push(term, start, end, size);
124
+ } else {
125
+ let index = this.buffer.length;
126
+ if (index > 0 && (this.buffer[index - 4] != 0 || this.buffer[index - 1] < 0)) {
127
+ let mustMove = false;
128
+ for (let scan = index; scan > 0 && this.buffer[scan - 2] > end; scan -= 4) {
129
+ if (this.buffer[scan - 1] >= 0) {
130
+ mustMove = true;
131
+ break;
132
+ }
133
+ }
134
+ if (mustMove)
135
+ while (index > 0 && this.buffer[index - 2] > end) {
136
+ this.buffer[index] = this.buffer[index - 4];
137
+ this.buffer[index + 1] = this.buffer[index - 3];
138
+ this.buffer[index + 2] = this.buffer[index - 2];
139
+ this.buffer[index + 3] = this.buffer[index - 1];
140
+ index -= 4;
141
+ if (size > 4)
142
+ size -= 4;
143
+ }
144
+ }
145
+ this.buffer[index] = term;
146
+ this.buffer[index + 1] = start;
147
+ this.buffer[index + 2] = end;
148
+ this.buffer[index + 3] = size;
149
+ }
150
+ }
151
+ // Apply a shift action
152
+ /**
153
+ @internal
154
+ */
155
+ shift(action, type, start, end) {
156
+ if (action & 131072) {
157
+ this.pushState(action & 65535, this.pos);
158
+ } else if ((action & 262144) == 0) {
159
+ let nextState = action, { parser } = this.p;
160
+ this.pos = end;
161
+ let skipped = parser.stateFlag(
162
+ nextState,
163
+ 1
164
+ /* StateFlag.Skipped */
165
+ );
166
+ if (!skipped && (end > start || type <= parser.maxNode))
167
+ this.reducePos = end;
168
+ this.pushState(nextState, skipped ? start : Math.min(start, this.reducePos));
169
+ this.shiftContext(type, start);
170
+ if (type <= parser.maxNode)
171
+ this.buffer.push(type, start, end, 4);
172
+ } else {
173
+ this.pos = end;
174
+ this.shiftContext(type, start);
175
+ if (type <= this.p.parser.maxNode)
176
+ this.buffer.push(type, start, end, 4);
177
+ }
178
+ }
179
+ // Apply an action
180
+ /**
181
+ @internal
182
+ */
183
+ apply(action, next, nextStart, nextEnd) {
184
+ if (action & 65536)
185
+ this.reduce(action);
186
+ else
187
+ this.shift(action, next, nextStart, nextEnd);
188
+ }
189
+ // Add a prebuilt (reused) node into the buffer.
190
+ /**
191
+ @internal
192
+ */
193
+ useNode(value, next) {
194
+ let index = this.p.reused.length - 1;
195
+ if (index < 0 || this.p.reused[index] != value) {
196
+ this.p.reused.push(value);
197
+ index++;
198
+ }
199
+ let start = this.pos;
200
+ this.reducePos = this.pos = start + value.length;
201
+ this.pushState(next, start);
202
+ this.buffer.push(
203
+ index,
204
+ start,
205
+ this.reducePos,
206
+ -1
207
+ /* size == -1 means this is a reused value */
208
+ );
209
+ if (this.curContext)
210
+ this.updateContext(this.curContext.tracker.reuse(this.curContext.context, value, this, this.p.stream.reset(this.pos - value.length)));
211
+ }
212
+ // Split the stack. Due to the buffer sharing and the fact
213
+ // that `this.stack` tends to stay quite shallow, this isn't very
214
+ // expensive.
215
+ /**
216
+ @internal
217
+ */
218
+ split() {
219
+ let parent = this;
220
+ let off = parent.buffer.length;
221
+ if (off && parent.buffer[off - 4] == 0)
222
+ off -= 4;
223
+ while (off > 0 && parent.buffer[off - 2] > parent.reducePos)
224
+ off -= 4;
225
+ let buffer = parent.buffer.slice(off), base = parent.bufferBase + off;
226
+ while (parent && base == parent.bufferBase)
227
+ parent = parent.parent;
228
+ return new Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, buffer, base, this.curContext, this.lookAhead, parent);
229
+ }
230
+ // Try to recover from an error by 'deleting' (ignoring) one token.
231
+ /**
232
+ @internal
233
+ */
234
+ recoverByDelete(next, nextEnd) {
235
+ let isNode = next <= this.p.parser.maxNode;
236
+ if (isNode)
237
+ this.storeNode(next, this.pos, nextEnd, 4);
238
+ this.storeNode(0, this.pos, nextEnd, isNode ? 8 : 4);
239
+ this.pos = this.reducePos = nextEnd;
240
+ this.score -= 190;
241
+ }
242
+ /**
243
+ Check if the given term would be able to be shifted (optionally
244
+ after some reductions) on this stack. This can be useful for
245
+ external tokenizers that want to make sure they only provide a
246
+ given token when it applies.
247
+ */
248
+ canShift(term) {
249
+ for (let sim = new SimulatedStack(this); ; ) {
250
+ let action = this.p.parser.stateSlot(
251
+ sim.state,
252
+ 4
253
+ /* ParseState.DefaultReduce */
254
+ ) || this.p.parser.hasAction(sim.state, term);
255
+ if (action == 0)
256
+ return false;
257
+ if ((action & 65536) == 0)
258
+ return true;
259
+ sim.reduce(action);
260
+ }
261
+ }
262
+ // Apply up to Recover.MaxNext recovery actions that conceptually
263
+ // inserts some missing token or rule.
264
+ /**
265
+ @internal
266
+ */
267
+ recoverByInsert(next) {
268
+ if (this.stack.length >= 300)
269
+ return [];
270
+ let nextStates = this.p.parser.nextStates(this.state);
271
+ if (nextStates.length > 4 << 1 || this.stack.length >= 120) {
272
+ let best = [];
273
+ for (let i = 0, s; i < nextStates.length; i += 2) {
274
+ if ((s = nextStates[i + 1]) != this.state && this.p.parser.hasAction(s, next))
275
+ best.push(nextStates[i], s);
276
+ }
277
+ if (this.stack.length < 120)
278
+ for (let i = 0; best.length < 4 << 1 && i < nextStates.length; i += 2) {
279
+ let s = nextStates[i + 1];
280
+ if (!best.some((v, i2) => i2 & 1 && v == s))
281
+ best.push(nextStates[i], s);
282
+ }
283
+ nextStates = best;
284
+ }
285
+ let result = [];
286
+ for (let i = 0; i < nextStates.length && result.length < 4; i += 2) {
287
+ let s = nextStates[i + 1];
288
+ if (s == this.state)
289
+ continue;
290
+ let stack = this.split();
291
+ stack.pushState(s, this.pos);
292
+ stack.storeNode(0, stack.pos, stack.pos, 4, true);
293
+ stack.shiftContext(nextStates[i], this.pos);
294
+ stack.reducePos = this.pos;
295
+ stack.score -= 200;
296
+ result.push(stack);
297
+ }
298
+ return result;
299
+ }
300
+ // Force a reduce, if possible. Return false if that can't
301
+ // be done.
302
+ /**
303
+ @internal
304
+ */
305
+ forceReduce() {
306
+ let { parser } = this.p;
307
+ let reduce = parser.stateSlot(
308
+ this.state,
309
+ 5
310
+ /* ParseState.ForcedReduce */
311
+ );
312
+ if ((reduce & 65536) == 0)
313
+ return false;
314
+ if (!parser.validAction(this.state, reduce)) {
315
+ let depth = reduce >> 19, term = reduce & 65535;
316
+ let target = this.stack.length - depth * 3;
317
+ if (target < 0 || parser.getGoto(this.stack[target], term, false) < 0) {
318
+ let backup = this.findForcedReduction();
319
+ if (backup == null)
320
+ return false;
321
+ reduce = backup;
322
+ }
323
+ this.storeNode(0, this.pos, this.pos, 4, true);
324
+ this.score -= 100;
325
+ }
326
+ this.reducePos = this.pos;
327
+ this.reduce(reduce);
328
+ return true;
329
+ }
330
+ /**
331
+ Try to scan through the automaton to find some kind of reduction
332
+ that can be applied. Used when the regular ForcedReduce field
333
+ isn't a valid action. @internal
334
+ */
335
+ findForcedReduction() {
336
+ let { parser } = this.p, seen = [];
337
+ let explore = (state, depth) => {
338
+ if (seen.includes(state))
339
+ return;
340
+ seen.push(state);
341
+ return parser.allActions(state, (action) => {
342
+ if (action & (262144 | 131072)) ;
343
+ else if (action & 65536) {
344
+ let rDepth = (action >> 19) - depth;
345
+ if (rDepth > 1) {
346
+ let term = action & 65535, target = this.stack.length - rDepth * 3;
347
+ if (target >= 0 && parser.getGoto(this.stack[target], term, false) >= 0)
348
+ return rDepth << 19 | 65536 | term;
349
+ }
350
+ } else {
351
+ let found = explore(action, depth + 1);
352
+ if (found != null)
353
+ return found;
354
+ }
355
+ });
356
+ };
357
+ return explore(this.state, 0);
358
+ }
359
+ /**
360
+ @internal
361
+ */
362
+ forceAll() {
363
+ while (!this.p.parser.stateFlag(
364
+ this.state,
365
+ 2
366
+ /* StateFlag.Accepting */
367
+ )) {
368
+ if (!this.forceReduce()) {
369
+ this.storeNode(0, this.pos, this.pos, 4, true);
370
+ break;
371
+ }
372
+ }
373
+ return this;
374
+ }
375
+ /**
376
+ Check whether this state has no further actions (assumed to be a direct descendant of the
377
+ top state, since any other states must be able to continue
378
+ somehow). @internal
379
+ */
380
+ get deadEnd() {
381
+ if (this.stack.length != 3)
382
+ return false;
383
+ let { parser } = this.p;
384
+ return parser.data[parser.stateSlot(
385
+ this.state,
386
+ 1
387
+ /* ParseState.Actions */
388
+ )] == 65535 && !parser.stateSlot(
389
+ this.state,
390
+ 4
391
+ /* ParseState.DefaultReduce */
392
+ );
393
+ }
394
+ /**
395
+ Restart the stack (put it back in its start state). Only safe
396
+ when this.stack.length == 3 (state is directly below the top
397
+ state). @internal
398
+ */
399
+ restart() {
400
+ this.storeNode(0, this.pos, this.pos, 4, true);
401
+ this.state = this.stack[0];
402
+ this.stack.length = 0;
403
+ }
404
+ /**
405
+ @internal
406
+ */
407
+ sameState(other) {
408
+ if (this.state != other.state || this.stack.length != other.stack.length)
409
+ return false;
410
+ for (let i = 0; i < this.stack.length; i += 3)
411
+ if (this.stack[i] != other.stack[i])
412
+ return false;
413
+ return true;
414
+ }
415
+ /**
416
+ Get the parser used by this stack.
417
+ */
418
+ get parser() {
419
+ return this.p.parser;
420
+ }
421
+ /**
422
+ Test whether a given dialect (by numeric ID, as exported from
423
+ the terms file) is enabled.
424
+ */
425
+ dialectEnabled(dialectID) {
426
+ return this.p.parser.dialect.flags[dialectID];
427
+ }
428
+ shiftContext(term, start) {
429
+ if (this.curContext)
430
+ this.updateContext(this.curContext.tracker.shift(this.curContext.context, term, this, this.p.stream.reset(start)));
431
+ }
432
+ reduceContext(term, start) {
433
+ if (this.curContext)
434
+ this.updateContext(this.curContext.tracker.reduce(this.curContext.context, term, this, this.p.stream.reset(start)));
435
+ }
436
+ /**
437
+ @internal
438
+ */
439
+ emitContext() {
440
+ let last = this.buffer.length - 1;
441
+ if (last < 0 || this.buffer[last] != -3)
442
+ this.buffer.push(this.curContext.hash, this.pos, this.pos, -3);
443
+ }
444
+ /**
445
+ @internal
446
+ */
447
+ emitLookAhead() {
448
+ let last = this.buffer.length - 1;
449
+ if (last < 0 || this.buffer[last] != -4)
450
+ this.buffer.push(this.lookAhead, this.pos, this.pos, -4);
451
+ }
452
+ updateContext(context) {
453
+ if (context != this.curContext.context) {
454
+ let newCx = new StackContext(this.curContext.tracker, context);
455
+ if (newCx.hash != this.curContext.hash)
456
+ this.emitContext();
457
+ this.curContext = newCx;
458
+ }
459
+ }
460
+ /**
461
+ @internal
462
+ */
463
+ setLookAhead(lookAhead) {
464
+ if (lookAhead <= this.lookAhead)
465
+ return false;
466
+ this.emitLookAhead();
467
+ this.lookAhead = lookAhead;
468
+ return true;
469
+ }
470
+ /**
471
+ @internal
472
+ */
473
+ close() {
474
+ if (this.curContext && this.curContext.tracker.strict)
475
+ this.emitContext();
476
+ if (this.lookAhead > 0)
477
+ this.emitLookAhead();
478
+ }
479
+ }
480
+ class StackContext {
481
+ constructor(tracker, context) {
482
+ this.tracker = tracker;
483
+ this.context = context;
484
+ this.hash = tracker.strict ? tracker.hash(context) : 0;
485
+ }
486
+ }
487
+ class SimulatedStack {
488
+ constructor(start) {
489
+ this.start = start;
490
+ this.state = start.state;
491
+ this.stack = start.stack;
492
+ this.base = this.stack.length;
493
+ }
494
+ reduce(action) {
495
+ let term = action & 65535, depth = action >> 19;
496
+ if (depth == 0) {
497
+ if (this.stack == this.start.stack)
498
+ this.stack = this.stack.slice();
499
+ this.stack.push(this.state, 0, 0);
500
+ this.base += 3;
501
+ } else {
502
+ this.base -= (depth - 1) * 3;
503
+ }
504
+ let goto = this.start.p.parser.getGoto(this.stack[this.base - 3], term, true);
505
+ this.state = goto;
506
+ }
507
+ }
508
+ class StackBufferCursor {
509
+ constructor(stack, pos, index) {
510
+ this.stack = stack;
511
+ this.pos = pos;
512
+ this.index = index;
513
+ this.buffer = stack.buffer;
514
+ if (this.index == 0)
515
+ this.maybeNext();
516
+ }
517
+ static create(stack, pos = stack.bufferBase + stack.buffer.length) {
518
+ return new StackBufferCursor(stack, pos, pos - stack.bufferBase);
519
+ }
520
+ maybeNext() {
521
+ let next = this.stack.parent;
522
+ if (next != null) {
523
+ this.index = this.stack.bufferBase - next.bufferBase;
524
+ this.stack = next;
525
+ this.buffer = next.buffer;
526
+ }
527
+ }
528
+ get id() {
529
+ return this.buffer[this.index - 4];
530
+ }
531
+ get start() {
532
+ return this.buffer[this.index - 3];
533
+ }
534
+ get end() {
535
+ return this.buffer[this.index - 2];
536
+ }
537
+ get size() {
538
+ return this.buffer[this.index - 1];
539
+ }
540
+ next() {
541
+ this.index -= 4;
542
+ this.pos -= 4;
543
+ if (this.index == 0)
544
+ this.maybeNext();
545
+ }
546
+ fork() {
547
+ return new StackBufferCursor(this.stack, this.pos, this.index);
548
+ }
549
+ }
550
+ function decodeArray(input, Type = Uint16Array) {
551
+ if (typeof input != "string")
552
+ return input;
553
+ let array = null;
554
+ for (let pos = 0, out = 0; pos < input.length; ) {
555
+ let value = 0;
556
+ for (; ; ) {
557
+ let next = input.charCodeAt(pos++), stop = false;
558
+ if (next == 126) {
559
+ value = 65535;
560
+ break;
561
+ }
562
+ if (next >= 92)
563
+ next--;
564
+ if (next >= 34)
565
+ next--;
566
+ let digit = next - 32;
567
+ if (digit >= 46) {
568
+ digit -= 46;
569
+ stop = true;
570
+ }
571
+ value += digit;
572
+ if (stop)
573
+ break;
574
+ value *= 46;
575
+ }
576
+ if (array)
577
+ array[out++] = value;
578
+ else
579
+ array = new Type(value);
580
+ }
581
+ return array;
582
+ }
583
+ class CachedToken {
584
+ constructor() {
585
+ this.start = -1;
586
+ this.value = -1;
587
+ this.end = -1;
588
+ this.extended = -1;
589
+ this.lookAhead = 0;
590
+ this.mask = 0;
591
+ this.context = 0;
592
+ }
593
+ }
594
+ const nullToken = new CachedToken();
595
+ class InputStream {
596
+ /**
597
+ @internal
598
+ */
599
+ constructor(input, ranges) {
600
+ this.input = input;
601
+ this.ranges = ranges;
602
+ this.chunk = "";
603
+ this.chunkOff = 0;
604
+ this.chunk2 = "";
605
+ this.chunk2Pos = 0;
606
+ this.next = -1;
607
+ this.token = nullToken;
608
+ this.rangeIndex = 0;
609
+ this.pos = this.chunkPos = ranges[0].from;
610
+ this.range = ranges[0];
611
+ this.end = ranges[ranges.length - 1].to;
612
+ this.readNext();
613
+ }
614
+ /**
615
+ @internal
616
+ */
617
+ resolveOffset(offset, assoc) {
618
+ let range = this.range, index = this.rangeIndex;
619
+ let pos = this.pos + offset;
620
+ while (pos < range.from) {
621
+ if (!index)
622
+ return null;
623
+ let next = this.ranges[--index];
624
+ pos -= range.from - next.to;
625
+ range = next;
626
+ }
627
+ while (assoc < 0 ? pos > range.to : pos >= range.to) {
628
+ if (index == this.ranges.length - 1)
629
+ return null;
630
+ let next = this.ranges[++index];
631
+ pos += next.from - range.to;
632
+ range = next;
633
+ }
634
+ return pos;
635
+ }
636
+ /**
637
+ @internal
638
+ */
639
+ clipPos(pos) {
640
+ if (pos >= this.range.from && pos < this.range.to)
641
+ return pos;
642
+ for (let range of this.ranges)
643
+ if (range.to > pos)
644
+ return Math.max(pos, range.from);
645
+ return this.end;
646
+ }
647
+ /**
648
+ Look at a code unit near the stream position. `.peek(0)` equals
649
+ `.next`, `.peek(-1)` gives you the previous character, and so
650
+ on.
651
+
652
+ Note that looking around during tokenizing creates dependencies
653
+ on potentially far-away content, which may reduce the
654
+ effectiveness incremental parsing—when looking forward—or even
655
+ cause invalid reparses when looking backward more than 25 code
656
+ units, since the library does not track lookbehind.
657
+ */
658
+ peek(offset) {
659
+ let idx = this.chunkOff + offset, pos, result;
660
+ if (idx >= 0 && idx < this.chunk.length) {
661
+ pos = this.pos + offset;
662
+ result = this.chunk.charCodeAt(idx);
663
+ } else {
664
+ let resolved = this.resolveOffset(offset, 1);
665
+ if (resolved == null)
666
+ return -1;
667
+ pos = resolved;
668
+ if (pos >= this.chunk2Pos && pos < this.chunk2Pos + this.chunk2.length) {
669
+ result = this.chunk2.charCodeAt(pos - this.chunk2Pos);
670
+ } else {
671
+ let i = this.rangeIndex, range = this.range;
672
+ while (range.to <= pos)
673
+ range = this.ranges[++i];
674
+ this.chunk2 = this.input.chunk(this.chunk2Pos = pos);
675
+ if (pos + this.chunk2.length > range.to)
676
+ this.chunk2 = this.chunk2.slice(0, range.to - pos);
677
+ result = this.chunk2.charCodeAt(0);
678
+ }
679
+ }
680
+ if (pos >= this.token.lookAhead)
681
+ this.token.lookAhead = pos + 1;
682
+ return result;
683
+ }
684
+ /**
685
+ Accept a token. By default, the end of the token is set to the
686
+ current stream position, but you can pass an offset (relative to
687
+ the stream position) to change that.
688
+ */
689
+ acceptToken(token, endOffset = 0) {
690
+ let end = endOffset ? this.resolveOffset(endOffset, -1) : this.pos;
691
+ if (end == null || end < this.token.start)
692
+ throw new RangeError("Token end out of bounds");
693
+ this.token.value = token;
694
+ this.token.end = end;
695
+ }
696
+ /**
697
+ Accept a token ending at a specific given position.
698
+ */
699
+ acceptTokenTo(token, endPos) {
700
+ this.token.value = token;
701
+ this.token.end = endPos;
702
+ }
703
+ getChunk() {
704
+ if (this.pos >= this.chunk2Pos && this.pos < this.chunk2Pos + this.chunk2.length) {
705
+ let { chunk, chunkPos } = this;
706
+ this.chunk = this.chunk2;
707
+ this.chunkPos = this.chunk2Pos;
708
+ this.chunk2 = chunk;
709
+ this.chunk2Pos = chunkPos;
710
+ this.chunkOff = this.pos - this.chunkPos;
711
+ } else {
712
+ this.chunk2 = this.chunk;
713
+ this.chunk2Pos = this.chunkPos;
714
+ let nextChunk = this.input.chunk(this.pos);
715
+ let end = this.pos + nextChunk.length;
716
+ this.chunk = end > this.range.to ? nextChunk.slice(0, this.range.to - this.pos) : nextChunk;
717
+ this.chunkPos = this.pos;
718
+ this.chunkOff = 0;
719
+ }
720
+ }
721
+ readNext() {
722
+ if (this.chunkOff >= this.chunk.length) {
723
+ this.getChunk();
724
+ if (this.chunkOff == this.chunk.length)
725
+ return this.next = -1;
726
+ }
727
+ return this.next = this.chunk.charCodeAt(this.chunkOff);
728
+ }
729
+ /**
730
+ Move the stream forward N (defaults to 1) code units. Returns
731
+ the new value of [`next`](#lr.InputStream.next).
732
+ */
733
+ advance(n = 1) {
734
+ this.chunkOff += n;
735
+ while (this.pos + n >= this.range.to) {
736
+ if (this.rangeIndex == this.ranges.length - 1)
737
+ return this.setDone();
738
+ n -= this.range.to - this.pos;
739
+ this.range = this.ranges[++this.rangeIndex];
740
+ this.pos = this.range.from;
741
+ }
742
+ this.pos += n;
743
+ if (this.pos >= this.token.lookAhead)
744
+ this.token.lookAhead = this.pos + 1;
745
+ return this.readNext();
746
+ }
747
+ setDone() {
748
+ this.pos = this.chunkPos = this.end;
749
+ this.range = this.ranges[this.rangeIndex = this.ranges.length - 1];
750
+ this.chunk = "";
751
+ return this.next = -1;
752
+ }
753
+ /**
754
+ @internal
755
+ */
756
+ reset(pos, token) {
757
+ if (token) {
758
+ this.token = token;
759
+ token.start = pos;
760
+ token.lookAhead = pos + 1;
761
+ token.value = token.extended = -1;
762
+ } else {
763
+ this.token = nullToken;
764
+ }
765
+ if (this.pos != pos) {
766
+ this.pos = pos;
767
+ if (pos == this.end) {
768
+ this.setDone();
769
+ return this;
770
+ }
771
+ while (pos < this.range.from)
772
+ this.range = this.ranges[--this.rangeIndex];
773
+ while (pos >= this.range.to)
774
+ this.range = this.ranges[++this.rangeIndex];
775
+ if (pos >= this.chunkPos && pos < this.chunkPos + this.chunk.length) {
776
+ this.chunkOff = pos - this.chunkPos;
777
+ } else {
778
+ this.chunk = "";
779
+ this.chunkOff = 0;
780
+ }
781
+ this.readNext();
782
+ }
783
+ return this;
784
+ }
785
+ /**
786
+ @internal
787
+ */
788
+ read(from, to) {
789
+ if (from >= this.chunkPos && to <= this.chunkPos + this.chunk.length)
790
+ return this.chunk.slice(from - this.chunkPos, to - this.chunkPos);
791
+ if (from >= this.chunk2Pos && to <= this.chunk2Pos + this.chunk2.length)
792
+ return this.chunk2.slice(from - this.chunk2Pos, to - this.chunk2Pos);
793
+ if (from >= this.range.from && to <= this.range.to)
794
+ return this.input.read(from, to);
795
+ let result = "";
796
+ for (let r of this.ranges) {
797
+ if (r.from >= to)
798
+ break;
799
+ if (r.to > from)
800
+ result += this.input.read(Math.max(r.from, from), Math.min(r.to, to));
801
+ }
802
+ return result;
803
+ }
804
+ }
805
+ class TokenGroup {
806
+ constructor(data, id2) {
807
+ this.data = data;
808
+ this.id = id2;
809
+ }
810
+ token(input, stack) {
811
+ let { parser } = stack.p;
812
+ readToken(this.data, input, stack, this.id, parser.data, parser.tokenPrecTable);
813
+ }
814
+ }
815
+ TokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
816
+ class LocalTokenGroup {
817
+ constructor(data, precTable, elseToken) {
818
+ this.precTable = precTable;
819
+ this.elseToken = elseToken;
820
+ this.data = typeof data == "string" ? decodeArray(data) : data;
821
+ }
822
+ token(input, stack) {
823
+ let start = input.pos, skipped = 0;
824
+ for (; ; ) {
825
+ let atEof = input.next < 0, nextPos = input.resolveOffset(1, 1);
826
+ readToken(this.data, input, stack, 0, this.data, this.precTable);
827
+ if (input.token.value > -1)
828
+ break;
829
+ if (this.elseToken == null)
830
+ return;
831
+ if (!atEof)
832
+ skipped++;
833
+ if (nextPos == null)
834
+ break;
835
+ input.reset(nextPos, input.token);
836
+ }
837
+ if (skipped) {
838
+ input.reset(start, input.token);
839
+ input.acceptToken(this.elseToken, skipped);
840
+ }
841
+ }
842
+ }
843
+ LocalTokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
844
+ class ExternalTokenizer {
845
+ /**
846
+ Create a tokenizer. The first argument is the function that,
847
+ given an input stream, scans for the types of tokens it
848
+ recognizes at the stream's position, and calls
849
+ [`acceptToken`](#lr.InputStream.acceptToken) when it finds
850
+ one.
851
+ */
852
+ constructor(token, options = {}) {
853
+ this.token = token;
854
+ this.contextual = !!options.contextual;
855
+ this.fallback = !!options.fallback;
856
+ this.extend = !!options.extend;
857
+ }
858
+ }
859
+ function readToken(data, input, stack, group, precTable, precOffset) {
860
+ let state = 0, groupMask = 1 << group, { dialect } = stack.p.parser;
861
+ scan: for (; ; ) {
862
+ if ((groupMask & data[state]) == 0)
863
+ break;
864
+ let accEnd = data[state + 1];
865
+ for (let i = state + 3; i < accEnd; i += 2)
866
+ if ((data[i + 1] & groupMask) > 0) {
867
+ let term = data[i];
868
+ if (dialect.allows(term) && (input.token.value == -1 || input.token.value == term || overrides(term, input.token.value, precTable, precOffset))) {
869
+ input.acceptToken(term);
870
+ break;
871
+ }
872
+ }
873
+ let next = input.next, low = 0, high = data[state + 2];
874
+ if (input.next < 0 && high > low && data[accEnd + high * 3 - 3] == 65535) {
875
+ state = data[accEnd + high * 3 - 1];
876
+ continue scan;
877
+ }
878
+ for (; low < high; ) {
879
+ let mid = low + high >> 1;
880
+ let index = accEnd + mid + (mid << 1);
881
+ let from = data[index], to = data[index + 1] || 65536;
882
+ if (next < from)
883
+ high = mid;
884
+ else if (next >= to)
885
+ low = mid + 1;
886
+ else {
887
+ state = data[index + 2];
888
+ input.advance();
889
+ continue scan;
890
+ }
891
+ }
892
+ break;
893
+ }
894
+ }
895
+ function findOffset(data, start, term) {
896
+ for (let i = start, next; (next = data[i]) != 65535; i++)
897
+ if (next == term)
898
+ return i - start;
899
+ return -1;
900
+ }
901
+ function overrides(token, prev, tableData, tableOffset) {
902
+ let iPrev = findOffset(tableData, tableOffset, prev);
903
+ return iPrev < 0 || findOffset(tableData, tableOffset, token) < iPrev;
904
+ }
905
+ const verbose = typeof process != "undefined" && process.env && /\bparse\b/.test(process.env.LOG);
906
+ let stackIDs = null;
907
+ function cutAt(tree, pos, side) {
908
+ let cursor = tree.cursor(IterMode.IncludeAnonymous);
909
+ cursor.moveTo(pos);
910
+ for (; ; ) {
911
+ if (!(side < 0 ? cursor.childBefore(pos) : cursor.childAfter(pos)))
912
+ for (; ; ) {
913
+ if ((side < 0 ? cursor.to < pos : cursor.from > pos) && !cursor.type.isError)
914
+ return side < 0 ? Math.max(0, Math.min(
915
+ cursor.to - 1,
916
+ pos - 25
917
+ /* Lookahead.Margin */
918
+ )) : Math.min(tree.length, Math.max(
919
+ cursor.from + 1,
920
+ pos + 25
921
+ /* Lookahead.Margin */
922
+ ));
923
+ if (side < 0 ? cursor.prevSibling() : cursor.nextSibling())
924
+ break;
925
+ if (!cursor.parent())
926
+ return side < 0 ? 0 : tree.length;
927
+ }
928
+ }
929
+ }
930
+ class FragmentCursor {
931
+ constructor(fragments, nodeSet) {
932
+ this.fragments = fragments;
933
+ this.nodeSet = nodeSet;
934
+ this.i = 0;
935
+ this.fragment = null;
936
+ this.safeFrom = -1;
937
+ this.safeTo = -1;
938
+ this.trees = [];
939
+ this.start = [];
940
+ this.index = [];
941
+ this.nextFragment();
942
+ }
943
+ nextFragment() {
944
+ let fr = this.fragment = this.i == this.fragments.length ? null : this.fragments[this.i++];
945
+ if (fr) {
946
+ this.safeFrom = fr.openStart ? cutAt(fr.tree, fr.from + fr.offset, 1) - fr.offset : fr.from;
947
+ this.safeTo = fr.openEnd ? cutAt(fr.tree, fr.to + fr.offset, -1) - fr.offset : fr.to;
948
+ while (this.trees.length) {
949
+ this.trees.pop();
950
+ this.start.pop();
951
+ this.index.pop();
952
+ }
953
+ this.trees.push(fr.tree);
954
+ this.start.push(-fr.offset);
955
+ this.index.push(0);
956
+ this.nextStart = this.safeFrom;
957
+ } else {
958
+ this.nextStart = 1e9;
959
+ }
960
+ }
961
+ // `pos` must be >= any previously given `pos` for this cursor
962
+ nodeAt(pos) {
963
+ if (pos < this.nextStart)
964
+ return null;
965
+ while (this.fragment && this.safeTo <= pos)
966
+ this.nextFragment();
967
+ if (!this.fragment)
968
+ return null;
969
+ for (; ; ) {
970
+ let last = this.trees.length - 1;
971
+ if (last < 0) {
972
+ this.nextFragment();
973
+ return null;
974
+ }
975
+ let top = this.trees[last], index = this.index[last];
976
+ if (index == top.children.length) {
977
+ this.trees.pop();
978
+ this.start.pop();
979
+ this.index.pop();
980
+ continue;
981
+ }
982
+ let next = top.children[index];
983
+ let start = this.start[last] + top.positions[index];
984
+ if (start > pos) {
985
+ this.nextStart = start;
986
+ return null;
987
+ }
988
+ if (next instanceof Tree) {
989
+ if (start == pos) {
990
+ if (start < this.safeFrom)
991
+ return null;
992
+ let end = start + next.length;
993
+ if (end <= this.safeTo) {
994
+ let lookAhead = next.prop(NodeProp.lookAhead);
995
+ if (!lookAhead || end + lookAhead < this.fragment.to)
996
+ return next;
997
+ }
998
+ }
999
+ this.index[last]++;
1000
+ if (start + next.length >= Math.max(this.safeFrom, pos)) {
1001
+ this.trees.push(next);
1002
+ this.start.push(start);
1003
+ this.index.push(0);
1004
+ }
1005
+ } else {
1006
+ this.index[last]++;
1007
+ this.nextStart = start + next.length;
1008
+ }
1009
+ }
1010
+ }
1011
+ }
1012
+ class TokenCache {
1013
+ constructor(parser, stream) {
1014
+ this.stream = stream;
1015
+ this.tokens = [];
1016
+ this.mainToken = null;
1017
+ this.actions = [];
1018
+ this.tokens = parser.tokenizers.map((_) => new CachedToken());
1019
+ }
1020
+ getActions(stack) {
1021
+ let actionIndex = 0;
1022
+ let main = null;
1023
+ let { parser } = stack.p, { tokenizers } = parser;
1024
+ let mask = parser.stateSlot(
1025
+ stack.state,
1026
+ 3
1027
+ /* ParseState.TokenizerMask */
1028
+ );
1029
+ let context = stack.curContext ? stack.curContext.hash : 0;
1030
+ let lookAhead = 0;
1031
+ for (let i = 0; i < tokenizers.length; i++) {
1032
+ if ((1 << i & mask) == 0)
1033
+ continue;
1034
+ let tokenizer = tokenizers[i], token = this.tokens[i];
1035
+ if (main && !tokenizer.fallback)
1036
+ continue;
1037
+ if (tokenizer.contextual || token.start != stack.pos || token.mask != mask || token.context != context) {
1038
+ this.updateCachedToken(token, tokenizer, stack);
1039
+ token.mask = mask;
1040
+ token.context = context;
1041
+ }
1042
+ if (token.lookAhead > token.end + 25)
1043
+ lookAhead = Math.max(token.lookAhead, lookAhead);
1044
+ if (token.value != 0) {
1045
+ let startIndex = actionIndex;
1046
+ if (token.extended > -1)
1047
+ actionIndex = this.addActions(stack, token.extended, token.end, actionIndex);
1048
+ actionIndex = this.addActions(stack, token.value, token.end, actionIndex);
1049
+ if (!tokenizer.extend) {
1050
+ main = token;
1051
+ if (actionIndex > startIndex)
1052
+ break;
1053
+ }
1054
+ }
1055
+ }
1056
+ while (this.actions.length > actionIndex)
1057
+ this.actions.pop();
1058
+ if (lookAhead)
1059
+ stack.setLookAhead(lookAhead);
1060
+ if (!main && stack.pos == this.stream.end) {
1061
+ main = new CachedToken();
1062
+ main.value = stack.p.parser.eofTerm;
1063
+ main.start = main.end = stack.pos;
1064
+ actionIndex = this.addActions(stack, main.value, main.end, actionIndex);
1065
+ }
1066
+ this.mainToken = main;
1067
+ return this.actions;
1068
+ }
1069
+ getMainToken(stack) {
1070
+ if (this.mainToken)
1071
+ return this.mainToken;
1072
+ let main = new CachedToken(), { pos, p } = stack;
1073
+ main.start = pos;
1074
+ main.end = Math.min(pos + 1, p.stream.end);
1075
+ main.value = pos == p.stream.end ? p.parser.eofTerm : 0;
1076
+ return main;
1077
+ }
1078
+ updateCachedToken(token, tokenizer, stack) {
1079
+ let start = this.stream.clipPos(stack.pos);
1080
+ tokenizer.token(this.stream.reset(start, token), stack);
1081
+ if (token.value > -1) {
1082
+ let { parser } = stack.p;
1083
+ for (let i = 0; i < parser.specialized.length; i++)
1084
+ if (parser.specialized[i] == token.value) {
1085
+ let result = parser.specializers[i](this.stream.read(token.start, token.end), stack);
1086
+ if (result >= 0 && stack.p.parser.dialect.allows(result >> 1)) {
1087
+ if ((result & 1) == 0)
1088
+ token.value = result >> 1;
1089
+ else
1090
+ token.extended = result >> 1;
1091
+ break;
1092
+ }
1093
+ }
1094
+ } else {
1095
+ token.value = 0;
1096
+ token.end = this.stream.clipPos(start + 1);
1097
+ }
1098
+ }
1099
+ putAction(action, token, end, index) {
1100
+ for (let i = 0; i < index; i += 3)
1101
+ if (this.actions[i] == action)
1102
+ return index;
1103
+ this.actions[index++] = action;
1104
+ this.actions[index++] = token;
1105
+ this.actions[index++] = end;
1106
+ return index;
1107
+ }
1108
+ addActions(stack, token, end, index) {
1109
+ let { state } = stack, { parser } = stack.p, { data } = parser;
1110
+ for (let set = 0; set < 2; set++) {
1111
+ for (let i = parser.stateSlot(
1112
+ state,
1113
+ set ? 2 : 1
1114
+ /* ParseState.Actions */
1115
+ ); ; i += 3) {
1116
+ if (data[i] == 65535) {
1117
+ if (data[i + 1] == 1) {
1118
+ i = pair(data, i + 2);
1119
+ } else {
1120
+ if (index == 0 && data[i + 1] == 2)
1121
+ index = this.putAction(pair(data, i + 2), token, end, index);
1122
+ break;
1123
+ }
1124
+ }
1125
+ if (data[i] == token)
1126
+ index = this.putAction(pair(data, i + 1), token, end, index);
1127
+ }
1128
+ }
1129
+ return index;
1130
+ }
1131
+ }
1132
+ class Parse {
1133
+ constructor(parser, input, fragments, ranges) {
1134
+ this.parser = parser;
1135
+ this.input = input;
1136
+ this.ranges = ranges;
1137
+ this.recovering = 0;
1138
+ this.nextStackID = 9812;
1139
+ this.minStackPos = 0;
1140
+ this.reused = [];
1141
+ this.stoppedAt = null;
1142
+ this.lastBigReductionStart = -1;
1143
+ this.lastBigReductionSize = 0;
1144
+ this.bigReductionCount = 0;
1145
+ this.stream = new InputStream(input, ranges);
1146
+ this.tokens = new TokenCache(parser, this.stream);
1147
+ this.topTerm = parser.top[1];
1148
+ let { from } = ranges[0];
1149
+ this.stacks = [Stack.start(this, parser.top[0], from)];
1150
+ this.fragments = fragments.length && this.stream.end - from > parser.bufferLength * 4 ? new FragmentCursor(fragments, parser.nodeSet) : null;
1151
+ }
1152
+ get parsedPos() {
1153
+ return this.minStackPos;
1154
+ }
1155
+ // Move the parser forward. This will process all parse stacks at
1156
+ // `this.pos` and try to advance them to a further position. If no
1157
+ // stack for such a position is found, it'll start error-recovery.
1158
+ //
1159
+ // When the parse is finished, this will return a syntax tree. When
1160
+ // not, it returns `null`.
1161
+ advance() {
1162
+ let stacks = this.stacks, pos = this.minStackPos;
1163
+ let newStacks = this.stacks = [];
1164
+ let stopped, stoppedTokens;
1165
+ if (this.bigReductionCount > 300 && stacks.length == 1) {
1166
+ let [s] = stacks;
1167
+ while (s.forceReduce() && s.stack.length && s.stack[s.stack.length - 2] >= this.lastBigReductionStart) {
1168
+ }
1169
+ this.bigReductionCount = this.lastBigReductionSize = 0;
1170
+ }
1171
+ for (let i = 0; i < stacks.length; i++) {
1172
+ let stack = stacks[i];
1173
+ for (; ; ) {
1174
+ this.tokens.mainToken = null;
1175
+ if (stack.pos > pos) {
1176
+ newStacks.push(stack);
1177
+ } else if (this.advanceStack(stack, newStacks, stacks)) {
1178
+ continue;
1179
+ } else {
1180
+ if (!stopped) {
1181
+ stopped = [];
1182
+ stoppedTokens = [];
1183
+ }
1184
+ stopped.push(stack);
1185
+ let tok = this.tokens.getMainToken(stack);
1186
+ stoppedTokens.push(tok.value, tok.end);
1187
+ }
1188
+ break;
1189
+ }
1190
+ }
1191
+ if (!newStacks.length) {
1192
+ let finished = stopped && findFinished(stopped);
1193
+ if (finished) {
1194
+ if (verbose)
1195
+ console.log("Finish with " + this.stackID(finished));
1196
+ return this.stackToTree(finished);
1197
+ }
1198
+ if (this.parser.strict) {
1199
+ if (verbose && stopped)
1200
+ console.log("Stuck with token " + (this.tokens.mainToken ? this.parser.getName(this.tokens.mainToken.value) : "none"));
1201
+ throw new SyntaxError("No parse at " + pos);
1202
+ }
1203
+ if (!this.recovering)
1204
+ this.recovering = 5;
1205
+ }
1206
+ if (this.recovering && stopped) {
1207
+ let finished = this.stoppedAt != null && stopped[0].pos > this.stoppedAt ? stopped[0] : this.runRecovery(stopped, stoppedTokens, newStacks);
1208
+ if (finished) {
1209
+ if (verbose)
1210
+ console.log("Force-finish " + this.stackID(finished));
1211
+ return this.stackToTree(finished.forceAll());
1212
+ }
1213
+ }
1214
+ if (this.recovering) {
1215
+ let maxRemaining = this.recovering == 1 ? 1 : this.recovering * 3;
1216
+ if (newStacks.length > maxRemaining) {
1217
+ newStacks.sort((a, b) => b.score - a.score);
1218
+ while (newStacks.length > maxRemaining)
1219
+ newStacks.pop();
1220
+ }
1221
+ if (newStacks.some((s) => s.reducePos > pos))
1222
+ this.recovering--;
1223
+ } else if (newStacks.length > 1) {
1224
+ outer: for (let i = 0; i < newStacks.length - 1; i++) {
1225
+ let stack = newStacks[i];
1226
+ for (let j = i + 1; j < newStacks.length; j++) {
1227
+ let other = newStacks[j];
1228
+ if (stack.sameState(other) || stack.buffer.length > 500 && other.buffer.length > 500) {
1229
+ if ((stack.score - other.score || stack.buffer.length - other.buffer.length) > 0) {
1230
+ newStacks.splice(j--, 1);
1231
+ } else {
1232
+ newStacks.splice(i--, 1);
1233
+ continue outer;
1234
+ }
1235
+ }
1236
+ }
1237
+ }
1238
+ if (newStacks.length > 12) {
1239
+ newStacks.sort((a, b) => b.score - a.score);
1240
+ newStacks.splice(
1241
+ 12,
1242
+ newStacks.length - 12
1243
+ /* Rec.MaxStackCount */
1244
+ );
1245
+ }
1246
+ }
1247
+ this.minStackPos = newStacks[0].pos;
1248
+ for (let i = 1; i < newStacks.length; i++)
1249
+ if (newStacks[i].pos < this.minStackPos)
1250
+ this.minStackPos = newStacks[i].pos;
1251
+ return null;
1252
+ }
1253
+ stopAt(pos) {
1254
+ if (this.stoppedAt != null && this.stoppedAt < pos)
1255
+ throw new RangeError("Can't move stoppedAt forward");
1256
+ this.stoppedAt = pos;
1257
+ }
1258
+ // Returns an updated version of the given stack, or null if the
1259
+ // stack can't advance normally. When `split` and `stacks` are
1260
+ // given, stacks split off by ambiguous operations will be pushed to
1261
+ // `split`, or added to `stacks` if they move `pos` forward.
1262
+ advanceStack(stack, stacks, split) {
1263
+ let start = stack.pos, { parser } = this;
1264
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1265
+ if (this.stoppedAt != null && start > this.stoppedAt)
1266
+ return stack.forceReduce() ? stack : null;
1267
+ if (this.fragments) {
1268
+ let strictCx = stack.curContext && stack.curContext.tracker.strict, cxHash = strictCx ? stack.curContext.hash : 0;
1269
+ for (let cached = this.fragments.nodeAt(start); cached; ) {
1270
+ let match = this.parser.nodeSet.types[cached.type.id] == cached.type ? parser.getGoto(stack.state, cached.type.id) : -1;
1271
+ if (match > -1 && cached.length && (!strictCx || (cached.prop(NodeProp.contextHash) || 0) == cxHash)) {
1272
+ stack.useNode(cached, match);
1273
+ if (verbose)
1274
+ console.log(base + this.stackID(stack) + ` (via reuse of ${parser.getName(cached.type.id)})`);
1275
+ return true;
1276
+ }
1277
+ if (!(cached instanceof Tree) || cached.children.length == 0 || cached.positions[0] > 0)
1278
+ break;
1279
+ let inner = cached.children[0];
1280
+ if (inner instanceof Tree && cached.positions[0] == 0)
1281
+ cached = inner;
1282
+ else
1283
+ break;
1284
+ }
1285
+ }
1286
+ let defaultReduce = parser.stateSlot(
1287
+ stack.state,
1288
+ 4
1289
+ /* ParseState.DefaultReduce */
1290
+ );
1291
+ if (defaultReduce > 0) {
1292
+ stack.reduce(defaultReduce);
1293
+ if (verbose)
1294
+ console.log(base + this.stackID(stack) + ` (via always-reduce ${parser.getName(
1295
+ defaultReduce & 65535
1296
+ /* Action.ValueMask */
1297
+ )})`);
1298
+ return true;
1299
+ }
1300
+ if (stack.stack.length >= 8400) {
1301
+ while (stack.stack.length > 6e3 && stack.forceReduce()) {
1302
+ }
1303
+ }
1304
+ let actions = this.tokens.getActions(stack);
1305
+ for (let i = 0; i < actions.length; ) {
1306
+ let action = actions[i++], term = actions[i++], end = actions[i++];
1307
+ let last = i == actions.length || !split;
1308
+ let localStack = last ? stack : stack.split();
1309
+ let main = this.tokens.mainToken;
1310
+ localStack.apply(action, term, main ? main.start : localStack.pos, end);
1311
+ if (verbose)
1312
+ console.log(base + this.stackID(localStack) + ` (via ${(action & 65536) == 0 ? "shift" : `reduce of ${parser.getName(
1313
+ action & 65535
1314
+ /* Action.ValueMask */
1315
+ )}`} for ${parser.getName(term)} @ ${start}${localStack == stack ? "" : ", split"})`);
1316
+ if (last)
1317
+ return true;
1318
+ else if (localStack.pos > start)
1319
+ stacks.push(localStack);
1320
+ else
1321
+ split.push(localStack);
1322
+ }
1323
+ return false;
1324
+ }
1325
+ // Advance a given stack forward as far as it will go. Returns the
1326
+ // (possibly updated) stack if it got stuck, or null if it moved
1327
+ // forward and was given to `pushStackDedup`.
1328
+ advanceFully(stack, newStacks) {
1329
+ let pos = stack.pos;
1330
+ for (; ; ) {
1331
+ if (!this.advanceStack(stack, null, null))
1332
+ return false;
1333
+ if (stack.pos > pos) {
1334
+ pushStackDedup(stack, newStacks);
1335
+ return true;
1336
+ }
1337
+ }
1338
+ }
1339
+ runRecovery(stacks, tokens, newStacks) {
1340
+ let finished = null, restarted = false;
1341
+ for (let i = 0; i < stacks.length; i++) {
1342
+ let stack = stacks[i], token = tokens[i << 1], tokenEnd = tokens[(i << 1) + 1];
1343
+ let base = verbose ? this.stackID(stack) + " -> " : "";
1344
+ if (stack.deadEnd) {
1345
+ if (restarted)
1346
+ continue;
1347
+ restarted = true;
1348
+ stack.restart();
1349
+ if (verbose)
1350
+ console.log(base + this.stackID(stack) + " (restarted)");
1351
+ let done = this.advanceFully(stack, newStacks);
1352
+ if (done)
1353
+ continue;
1354
+ }
1355
+ let force = stack.split(), forceBase = base;
1356
+ for (let j = 0; j < 10 && force.forceReduce(); j++) {
1357
+ if (verbose)
1358
+ console.log(forceBase + this.stackID(force) + " (via force-reduce)");
1359
+ let done = this.advanceFully(force, newStacks);
1360
+ if (done)
1361
+ break;
1362
+ if (verbose)
1363
+ forceBase = this.stackID(force) + " -> ";
1364
+ }
1365
+ for (let insert of stack.recoverByInsert(token)) {
1366
+ if (verbose)
1367
+ console.log(base + this.stackID(insert) + " (via recover-insert)");
1368
+ this.advanceFully(insert, newStacks);
1369
+ }
1370
+ if (this.stream.end > stack.pos) {
1371
+ if (tokenEnd == stack.pos) {
1372
+ tokenEnd++;
1373
+ token = 0;
1374
+ }
1375
+ stack.recoverByDelete(token, tokenEnd);
1376
+ if (verbose)
1377
+ console.log(base + this.stackID(stack) + ` (via recover-delete ${this.parser.getName(token)})`);
1378
+ pushStackDedup(stack, newStacks);
1379
+ } else if (!finished || finished.score < force.score) {
1380
+ finished = force;
1381
+ }
1382
+ }
1383
+ return finished;
1384
+ }
1385
+ // Convert the stack's buffer to a syntax tree.
1386
+ stackToTree(stack) {
1387
+ stack.close();
1388
+ return Tree.build({
1389
+ buffer: StackBufferCursor.create(stack),
1390
+ nodeSet: this.parser.nodeSet,
1391
+ topID: this.topTerm,
1392
+ maxBufferLength: this.parser.bufferLength,
1393
+ reused: this.reused,
1394
+ start: this.ranges[0].from,
1395
+ length: stack.pos - this.ranges[0].from,
1396
+ minRepeatType: this.parser.minRepeatTerm
1397
+ });
1398
+ }
1399
+ stackID(stack) {
1400
+ let id2 = (stackIDs || (stackIDs = /* @__PURE__ */ new WeakMap())).get(stack);
1401
+ if (!id2)
1402
+ stackIDs.set(stack, id2 = String.fromCodePoint(this.nextStackID++));
1403
+ return id2 + stack;
1404
+ }
1405
+ }
1406
+ function pushStackDedup(stack, newStacks) {
1407
+ for (let i = 0; i < newStacks.length; i++) {
1408
+ let other = newStacks[i];
1409
+ if (other.pos == stack.pos && other.sameState(stack)) {
1410
+ if (newStacks[i].score < stack.score)
1411
+ newStacks[i] = stack;
1412
+ return;
1413
+ }
1414
+ }
1415
+ newStacks.push(stack);
1416
+ }
1417
+ class Dialect {
1418
+ constructor(source, flags, disabled) {
1419
+ this.source = source;
1420
+ this.flags = flags;
1421
+ this.disabled = disabled;
1422
+ }
1423
+ allows(term) {
1424
+ return !this.disabled || this.disabled[term] == 0;
1425
+ }
1426
+ }
1427
+ const id = (x) => x;
1428
+ class ContextTracker {
1429
+ /**
1430
+ Define a context tracker.
1431
+ */
1432
+ constructor(spec) {
1433
+ this.start = spec.start;
1434
+ this.shift = spec.shift || id;
1435
+ this.reduce = spec.reduce || id;
1436
+ this.reuse = spec.reuse || id;
1437
+ this.hash = spec.hash || (() => 0);
1438
+ this.strict = spec.strict !== false;
1439
+ }
1440
+ }
1441
+ class LRParser extends Parser {
1442
+ /**
1443
+ @internal
1444
+ */
1445
+ constructor(spec) {
1446
+ super();
1447
+ this.wrappers = [];
1448
+ if (spec.version != 14)
1449
+ throw new RangeError(`Parser version (${spec.version}) doesn't match runtime version (${14})`);
1450
+ let nodeNames = spec.nodeNames.split(" ");
1451
+ this.minRepeatTerm = nodeNames.length;
1452
+ for (let i = 0; i < spec.repeatNodeCount; i++)
1453
+ nodeNames.push("");
1454
+ let topTerms = Object.keys(spec.topRules).map((r) => spec.topRules[r][1]);
1455
+ let nodeProps = [];
1456
+ for (let i = 0; i < nodeNames.length; i++)
1457
+ nodeProps.push([]);
1458
+ function setProp(nodeID, prop, value) {
1459
+ nodeProps[nodeID].push([prop, prop.deserialize(String(value))]);
1460
+ }
1461
+ if (spec.nodeProps)
1462
+ for (let propSpec of spec.nodeProps) {
1463
+ let prop = propSpec[0];
1464
+ if (typeof prop == "string")
1465
+ prop = NodeProp[prop];
1466
+ for (let i = 1; i < propSpec.length; ) {
1467
+ let next = propSpec[i++];
1468
+ if (next >= 0) {
1469
+ setProp(next, prop, propSpec[i++]);
1470
+ } else {
1471
+ let value = propSpec[i + -next];
1472
+ for (let j = -next; j > 0; j--)
1473
+ setProp(propSpec[i++], prop, value);
1474
+ i++;
1475
+ }
1476
+ }
1477
+ }
1478
+ this.nodeSet = new NodeSet(nodeNames.map((name, i) => NodeType.define({
1479
+ name: i >= this.minRepeatTerm ? void 0 : name,
1480
+ id: i,
1481
+ props: nodeProps[i],
1482
+ top: topTerms.indexOf(i) > -1,
1483
+ error: i == 0,
1484
+ skipped: spec.skippedNodes && spec.skippedNodes.indexOf(i) > -1
1485
+ })));
1486
+ if (spec.propSources)
1487
+ this.nodeSet = this.nodeSet.extend(...spec.propSources);
1488
+ this.strict = false;
1489
+ this.bufferLength = DefaultBufferLength;
1490
+ let tokenArray = decodeArray(spec.tokenData);
1491
+ this.context = spec.context;
1492
+ this.specializerSpecs = spec.specialized || [];
1493
+ this.specialized = new Uint16Array(this.specializerSpecs.length);
1494
+ for (let i = 0; i < this.specializerSpecs.length; i++)
1495
+ this.specialized[i] = this.specializerSpecs[i].term;
1496
+ this.specializers = this.specializerSpecs.map(getSpecializer);
1497
+ this.states = decodeArray(spec.states, Uint32Array);
1498
+ this.data = decodeArray(spec.stateData);
1499
+ this.goto = decodeArray(spec.goto);
1500
+ this.maxTerm = spec.maxTerm;
1501
+ this.tokenizers = spec.tokenizers.map((value) => typeof value == "number" ? new TokenGroup(tokenArray, value) : value);
1502
+ this.topRules = spec.topRules;
1503
+ this.dialects = spec.dialects || {};
1504
+ this.dynamicPrecedences = spec.dynamicPrecedences || null;
1505
+ this.tokenPrecTable = spec.tokenPrec;
1506
+ this.termNames = spec.termNames || null;
1507
+ this.maxNode = this.nodeSet.types.length - 1;
1508
+ this.dialect = this.parseDialect();
1509
+ this.top = this.topRules[Object.keys(this.topRules)[0]];
1510
+ }
1511
+ createParse(input, fragments, ranges) {
1512
+ let parse = new Parse(this, input, fragments, ranges);
1513
+ for (let w of this.wrappers)
1514
+ parse = w(parse, input, fragments, ranges);
1515
+ return parse;
1516
+ }
1517
+ /**
1518
+ Get a goto table entry @internal
1519
+ */
1520
+ getGoto(state, term, loose = false) {
1521
+ let table = this.goto;
1522
+ if (term >= table[0])
1523
+ return -1;
1524
+ for (let pos = table[term + 1]; ; ) {
1525
+ let groupTag = table[pos++], last = groupTag & 1;
1526
+ let target = table[pos++];
1527
+ if (last && loose)
1528
+ return target;
1529
+ for (let end = pos + (groupTag >> 1); pos < end; pos++)
1530
+ if (table[pos] == state)
1531
+ return target;
1532
+ if (last)
1533
+ return -1;
1534
+ }
1535
+ }
1536
+ /**
1537
+ Check if this state has an action for a given terminal @internal
1538
+ */
1539
+ hasAction(state, terminal) {
1540
+ let data = this.data;
1541
+ for (let set = 0; set < 2; set++) {
1542
+ for (let i = this.stateSlot(
1543
+ state,
1544
+ set ? 2 : 1
1545
+ /* ParseState.Actions */
1546
+ ), next; ; i += 3) {
1547
+ if ((next = data[i]) == 65535) {
1548
+ if (data[i + 1] == 1)
1549
+ next = data[i = pair(data, i + 2)];
1550
+ else if (data[i + 1] == 2)
1551
+ return pair(data, i + 2);
1552
+ else
1553
+ break;
1554
+ }
1555
+ if (next == terminal || next == 0)
1556
+ return pair(data, i + 1);
1557
+ }
1558
+ }
1559
+ return 0;
1560
+ }
1561
+ /**
1562
+ @internal
1563
+ */
1564
+ stateSlot(state, slot) {
1565
+ return this.states[state * 6 + slot];
1566
+ }
1567
+ /**
1568
+ @internal
1569
+ */
1570
+ stateFlag(state, flag) {
1571
+ return (this.stateSlot(
1572
+ state,
1573
+ 0
1574
+ /* ParseState.Flags */
1575
+ ) & flag) > 0;
1576
+ }
1577
+ /**
1578
+ @internal
1579
+ */
1580
+ validAction(state, action) {
1581
+ return !!this.allActions(state, (a) => a == action ? true : null);
1582
+ }
1583
+ /**
1584
+ @internal
1585
+ */
1586
+ allActions(state, action) {
1587
+ let deflt = this.stateSlot(
1588
+ state,
1589
+ 4
1590
+ /* ParseState.DefaultReduce */
1591
+ );
1592
+ let result = deflt ? action(deflt) : void 0;
1593
+ for (let i = this.stateSlot(
1594
+ state,
1595
+ 1
1596
+ /* ParseState.Actions */
1597
+ ); result == null; i += 3) {
1598
+ if (this.data[i] == 65535) {
1599
+ if (this.data[i + 1] == 1)
1600
+ i = pair(this.data, i + 2);
1601
+ else
1602
+ break;
1603
+ }
1604
+ result = action(pair(this.data, i + 1));
1605
+ }
1606
+ return result;
1607
+ }
1608
+ /**
1609
+ Get the states that can follow this one through shift actions or
1610
+ goto jumps. @internal
1611
+ */
1612
+ nextStates(state) {
1613
+ let result = [];
1614
+ for (let i = this.stateSlot(
1615
+ state,
1616
+ 1
1617
+ /* ParseState.Actions */
1618
+ ); ; i += 3) {
1619
+ if (this.data[i] == 65535) {
1620
+ if (this.data[i + 1] == 1)
1621
+ i = pair(this.data, i + 2);
1622
+ else
1623
+ break;
1624
+ }
1625
+ if ((this.data[i + 2] & 65536 >> 16) == 0) {
1626
+ let value = this.data[i + 1];
1627
+ if (!result.some((v, i2) => i2 & 1 && v == value))
1628
+ result.push(this.data[i], value);
1629
+ }
1630
+ }
1631
+ return result;
1632
+ }
1633
+ /**
1634
+ Configure the parser. Returns a new parser instance that has the
1635
+ given settings modified. Settings not provided in `config` are
1636
+ kept from the original parser.
1637
+ */
1638
+ configure(config) {
1639
+ let copy = Object.assign(Object.create(LRParser.prototype), this);
1640
+ if (config.props)
1641
+ copy.nodeSet = this.nodeSet.extend(...config.props);
1642
+ if (config.top) {
1643
+ let info = this.topRules[config.top];
1644
+ if (!info)
1645
+ throw new RangeError(`Invalid top rule name ${config.top}`);
1646
+ copy.top = info;
1647
+ }
1648
+ if (config.tokenizers)
1649
+ copy.tokenizers = this.tokenizers.map((t) => {
1650
+ let found = config.tokenizers.find((r) => r.from == t);
1651
+ return found ? found.to : t;
1652
+ });
1653
+ if (config.specializers) {
1654
+ copy.specializers = this.specializers.slice();
1655
+ copy.specializerSpecs = this.specializerSpecs.map((s, i) => {
1656
+ let found = config.specializers.find((r) => r.from == s.external);
1657
+ if (!found)
1658
+ return s;
1659
+ let spec = Object.assign(Object.assign({}, s), { external: found.to });
1660
+ copy.specializers[i] = getSpecializer(spec);
1661
+ return spec;
1662
+ });
1663
+ }
1664
+ if (config.contextTracker)
1665
+ copy.context = config.contextTracker;
1666
+ if (config.dialect)
1667
+ copy.dialect = this.parseDialect(config.dialect);
1668
+ if (config.strict != null)
1669
+ copy.strict = config.strict;
1670
+ if (config.wrap)
1671
+ copy.wrappers = copy.wrappers.concat(config.wrap);
1672
+ if (config.bufferLength != null)
1673
+ copy.bufferLength = config.bufferLength;
1674
+ return copy;
1675
+ }
1676
+ /**
1677
+ Tells you whether any [parse wrappers](#lr.ParserConfig.wrap)
1678
+ are registered for this parser.
1679
+ */
1680
+ hasWrappers() {
1681
+ return this.wrappers.length > 0;
1682
+ }
1683
+ /**
1684
+ Returns the name associated with a given term. This will only
1685
+ work for all terms when the parser was generated with the
1686
+ `--names` option. By default, only the names of tagged terms are
1687
+ stored.
1688
+ */
1689
+ getName(term) {
1690
+ return this.termNames ? this.termNames[term] : String(term <= this.maxNode && this.nodeSet.types[term].name || term);
1691
+ }
1692
+ /**
1693
+ The eof term id is always allocated directly after the node
1694
+ types. @internal
1695
+ */
1696
+ get eofTerm() {
1697
+ return this.maxNode + 1;
1698
+ }
1699
+ /**
1700
+ The type of top node produced by the parser.
1701
+ */
1702
+ get topNode() {
1703
+ return this.nodeSet.types[this.top[1]];
1704
+ }
1705
+ /**
1706
+ @internal
1707
+ */
1708
+ dynamicPrecedence(term) {
1709
+ let prec = this.dynamicPrecedences;
1710
+ return prec == null ? 0 : prec[term] || 0;
1711
+ }
1712
+ /**
1713
+ @internal
1714
+ */
1715
+ parseDialect(dialect) {
1716
+ let values = Object.keys(this.dialects), flags = values.map(() => false);
1717
+ if (dialect)
1718
+ for (let part of dialect.split(" ")) {
1719
+ let id2 = values.indexOf(part);
1720
+ if (id2 >= 0)
1721
+ flags[id2] = true;
1722
+ }
1723
+ let disabled = null;
1724
+ for (let i = 0; i < values.length; i++)
1725
+ if (!flags[i]) {
1726
+ for (let j = this.dialects[values[i]], id2; (id2 = this.data[j++]) != 65535; )
1727
+ (disabled || (disabled = new Uint8Array(this.maxTerm + 1)))[id2] = 1;
1728
+ }
1729
+ return new Dialect(dialect, flags, disabled);
1730
+ }
1731
+ /**
1732
+ Used by the output of the parser generator. Not available to
1733
+ user code. @hide
1734
+ */
1735
+ static deserialize(spec) {
1736
+ return new LRParser(spec);
1737
+ }
1738
+ }
1739
+ function pair(data, off) {
1740
+ return data[off] | data[off + 1] << 16;
1741
+ }
1742
+ function findFinished(stacks) {
1743
+ let best = null;
1744
+ for (let stack of stacks) {
1745
+ let stopped = stack.p.stoppedAt;
1746
+ if ((stack.pos == stack.p.stream.end || stopped != null && stack.pos > stopped) && stack.p.parser.stateFlag(
1747
+ stack.state,
1748
+ 2
1749
+ /* StateFlag.Accepting */
1750
+ ) && (!best || best.score < stack.score))
1751
+ best = stack;
1752
+ }
1753
+ return best;
1754
+ }
1755
+ function getSpecializer(spec) {
1756
+ if (spec.external) {
1757
+ let mask = spec.extend ? 1 : 0;
1758
+ return (value, stack) => spec.external(value, stack) << 1 | mask;
1759
+ }
1760
+ return spec.get;
1761
+ }
1762
+ export {
1763
+ ContextTracker as C,
1764
+ ExternalTokenizer as E,
1765
+ LRParser as L,
1766
+ LocalTokenGroup as a
1767
+ };