react-dsl-editor 0.3.2 → 0.4.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.
@@ -1,15 +1,15 @@
1
- import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
2
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
- var __create = Object.create, __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __getOwnPropNames = Object.getOwnPropertyNames, __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty, __commonJSMin = (o, h) => () => (h || o((h = { exports: {} }).exports, h), h.exports), __copyProps = (o, h, g, _) => {
4
- if (h && typeof h == "object" || typeof h == "function") for (var v = __getOwnPropNames(h), y = 0, b = v.length, x; y < b; y++) x = v[y], !__hasOwnProp.call(o, x) && x !== g && __defProp(o, x, {
5
- get: ((o) => h[o]).bind(null, x),
6
- enumerable: !(_ = __getOwnPropDesc(h, x)) || _.enumerable
1
+ import { Fragment, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
2
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
3
+ var __create = Object.create, __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __getOwnPropNames = Object.getOwnPropertyNames, __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty, __commonJSMin = (a, g) => () => (g || a((g = { exports: {} }).exports, g), g.exports), __copyProps = (a, g, _, v) => {
4
+ if (g && typeof g == "object" || typeof g == "function") for (var y = __getOwnPropNames(g), b = 0, x = y.length, S; b < x; b++) S = y[b], !__hasOwnProp.call(a, S) && S !== _ && __defProp(a, S, {
5
+ get: ((a) => g[a]).bind(null, S),
6
+ enumerable: !(v = __getOwnPropDesc(g, S)) || v.enumerable
7
7
  });
8
- return o;
9
- }, __toESM = (o, h, g) => (g = o == null ? {} : __create(__getProtoOf(o)), __copyProps(h || !o || !o.__esModule ? __defProp(g, "default", {
10
- value: o,
8
+ return a;
9
+ }, __toESM = (a, g, _) => (_ = a == null ? {} : __create(__getProtoOf(a)), __copyProps(g || !a || !a.__esModule ? __defProp(_, "default", {
10
+ value: a,
11
11
  enumerable: !0
12
- }) : g, o));
12
+ }) : _, a));
13
13
  const textStyle = {
14
14
  position: "absolute",
15
15
  inset: 0,
@@ -18,705 +18,323 @@ const textStyle = {
18
18
  fontSize: "1em",
19
19
  lineHeight: "1.3em",
20
20
  fontFamily: "monospace"
21
- }, ReadOnlyTextarea = forwardRef(function({ wrap: o, children: h, style: g = {} }, _) {
21
+ }, ReadOnlyTextarea = forwardRef(function({ wrap: a, children: g, style: _ = {} }, v) {
22
22
  return /* @__PURE__ */ jsx("pre", {
23
- ref: _,
23
+ ref: v,
24
24
  style: {
25
25
  ...textStyle,
26
26
  pointerEvents: "none",
27
27
  margin: 0,
28
28
  overflow: "auto",
29
- whiteSpace: o ? "pre-wrap" : "pre",
30
- ...g
29
+ whiteSpace: a ? "pre-wrap" : "pre",
30
+ ..._
31
31
  },
32
- children: h
32
+ children: g
33
33
  });
34
34
  });
35
- function e$4(o, h, g) {
36
- let _ = (g) => o(g, ...h);
37
- return g === void 0 ? _ : Object.assign(_, {
38
- lazy: g,
39
- lazyArgs: h
35
+ function e$2(a, g, _) {
36
+ let v = (_) => a(_, ...g);
37
+ return _ === void 0 ? v : Object.assign(v, {
38
+ lazy: _,
39
+ lazyArgs: g
40
40
  });
41
41
  }
42
- function t$10(o, h, g) {
43
- let _ = o.length - h.length;
44
- if (_ === 0) return o(...h);
45
- if (_ === 1) return e$4(o, h, g);
42
+ function t$6(a, g, _) {
43
+ let v = a.length - g.length;
44
+ if (v === 0) return a(...g);
45
+ if (v === 1) return e$2(a, g, _);
46
46
  throw Error("Wrong number of arguments");
47
47
  }
48
- function t$5(...o) {
49
- return t$10(n$13, o);
50
- }
51
- var n$13 = (o, h) => o ?? h, e$3 = {
52
- done: !0,
53
- hasNext: !1
54
- }, t$8 = {
48
+ var t$4 = {
55
49
  done: !1,
56
50
  hasNext: !1
57
- }, n$5 = () => e$3;
58
- function t(o, ...h) {
59
- let g = o, _ = h.map((o) => "lazy" in o ? r$8(o) : void 0), v = 0;
60
- for (; v < h.length;) {
61
- if (_[v] === void 0 || !i$4(g)) {
62
- let o = h[v];
63
- g = o(g), v += 1;
51
+ };
52
+ function t$1(a, ...g) {
53
+ let _ = a, v = g.map((a) => "lazy" in a ? r$4(a) : void 0), y = 0;
54
+ for (; y < g.length;) {
55
+ if (v[y] === void 0 || !i(_)) {
56
+ let a = g[y];
57
+ _ = a(_), y += 1;
64
58
  continue;
65
59
  }
66
- let o = [];
67
- for (let g = v; g < h.length; g++) {
68
- let h = _[g];
69
- if (h === void 0 || (o.push(h), h.isSingle)) break;
60
+ let a = [];
61
+ for (let _ = y; _ < g.length; _++) {
62
+ let g = v[_];
63
+ if (g === void 0 || (a.push(g), g.isSingle)) break;
70
64
  }
71
- let y = [];
72
- for (let h of g) if (n$12(h, y, o)) break;
73
- let { isSingle: b } = o.at(-1);
74
- g = b ? y[0] : y, v += o.length;
65
+ let b = [];
66
+ for (let g of _) if (n$5(g, b, a)) break;
67
+ let { isSingle: x } = a.at(-1);
68
+ _ = x ? b[0] : b, y += a.length;
75
69
  }
76
- return g;
70
+ return _;
77
71
  }
78
- function n$12(o, h, g) {
79
- if (g.length === 0) return h.push(o), !1;
80
- let _ = o, v = t$8, y = !1;
81
- for (let [o, b] of g.entries()) {
82
- let { index: x, items: S } = b;
83
- if (S.push(_), v = b(_, x, S), b.index += 1, v.hasNext) {
84
- if (v.hasMany ?? !1) {
85
- for (let _ of v.next) if (n$12(_, h, g.slice(o + 1))) return !0;
86
- return y;
72
+ function n$5(a, g, _) {
73
+ if (_.length === 0) return g.push(a), !1;
74
+ let v = a, y = t$4, b = !1;
75
+ for (let [a, x] of _.entries()) {
76
+ let { index: S, items: C } = x;
77
+ if (C.push(v), y = x(v, S, C), x.index += 1, y.hasNext) {
78
+ if (y.hasMany ?? !1) {
79
+ for (let v of y.next) if (n$5(v, g, _.slice(a + 1))) return !0;
80
+ return b;
87
81
  }
88
- _ = v.next;
82
+ v = y.next;
89
83
  }
90
- if (!v.hasNext) break;
91
- v.done && (y = !0);
84
+ if (!y.hasNext) break;
85
+ y.done && (b = !0);
92
86
  }
93
- return v.hasNext && h.push(_), y;
87
+ return y.hasNext && g.push(v), b;
94
88
  }
95
- function r$8(o) {
96
- let { lazy: h, lazyArgs: g } = o, _ = h(...g);
97
- return Object.assign(_, {
98
- isSingle: h.single ?? !1,
89
+ function r$4(a) {
90
+ let { lazy: g, lazyArgs: _ } = a, v = g(..._);
91
+ return Object.assign(v, {
92
+ isSingle: g.single ?? !1,
99
93
  index: 0,
100
94
  items: []
101
95
  });
102
96
  }
103
- function i$4(o) {
104
- return typeof o == "string" || typeof o == "object" && !!o && Symbol.iterator in o;
97
+ function i(a) {
98
+ return typeof a == "string" || typeof a == "object" && !!a && Symbol.iterator in a;
105
99
  }
106
- function t$9(o, h) {
107
- let g = h.length - o.length;
108
- if (g === 1) {
109
- let [g, ..._] = h;
110
- return t(g, {
111
- lazy: o,
112
- lazyArgs: _
100
+ function t$5(a, g) {
101
+ let _ = g.length - a.length;
102
+ if (_ === 1) {
103
+ let [_, ...v] = g;
104
+ return t$1(_, {
105
+ lazy: a,
106
+ lazyArgs: v
113
107
  });
114
108
  }
115
- if (g === 0) {
116
- let g = {
117
- lazy: o,
118
- lazyArgs: h
109
+ if (_ === 0) {
110
+ let _ = {
111
+ lazy: a,
112
+ lazyArgs: g
119
113
  };
120
- return Object.assign((o) => t(o, g), g);
114
+ return Object.assign((a) => t$1(a, _), _);
121
115
  }
122
116
  throw Error("Wrong number of arguments");
123
117
  }
124
- var e$2 = {
125
- asc: (o, h) => o > h,
126
- desc: (o, h) => o < h
127
- };
128
- function t$11(o, h) {
129
- let [g, ..._] = h;
130
- if (!i$3(g)) {
131
- let h = r$7(..._);
132
- return o(g, h);
133
- }
134
- let v = r$7(g, ..._);
135
- return (h) => o(h, v);
136
- }
137
- function r$7(o, h, ...g) {
138
- let _ = typeof o == "function" ? o : o[0], v = typeof o == "function" ? "asc" : o[1], { [v]: y } = e$2, b = h === void 0 ? void 0 : r$7(h, ...g);
139
- return (o, h) => {
140
- let g = _(o), v = _(h);
141
- return y(g, v) ? 1 : y(v, g) ? -1 : b?.(o, h) ?? 0;
142
- };
143
- }
144
- function i$3(o) {
145
- if (a$1(o)) return !0;
146
- if (typeof o != "object" || !Array.isArray(o)) return !1;
147
- let [h, g, ..._] = o;
148
- return a$1(h) && typeof g == "string" && g in e$2 && _.length === 0;
149
- }
150
- var a$1 = (o) => typeof o == "function" && o.length === 1;
151
- function n(...o) {
152
- return t$10(r$6, o, i$2);
153
- }
154
- var r$6 = (o, h) => o.filter(h), i$2 = (o) => (h, g, _) => o(h, g, _) ? {
155
- done: !1,
156
- hasNext: !0,
157
- next: h
158
- } : t$8, e$1 = (o) => Object.assign(o, { single: !0 });
159
- function n$1(...o) {
160
- return t$10(r$5, o, e$1(i$1));
118
+ function t(...a) {
119
+ return t$6(n$4, a, r$3);
161
120
  }
162
- var r$5 = ([o]) => o, i$1 = () => a, a = (o) => ({
163
- hasNext: !0,
164
- next: o,
165
- done: !0
166
- });
167
- function t$7(...o) {
168
- return t$10(n$11, o, r$4);
169
- }
170
- var n$11 = (o, h) => o.flatMap(h), r$4 = (o) => (h, g, _) => {
171
- let v = o(h, g, _);
172
- return Array.isArray(v) ? {
121
+ var n$4 = (a, g) => a.flatMap(g), r$3 = (a) => (g, _, v) => {
122
+ let y = a(g, _, v);
123
+ return Array.isArray(y) ? {
173
124
  done: !1,
174
125
  hasNext: !0,
175
126
  hasMany: !0,
176
- next: v
127
+ next: y
177
128
  } : {
178
129
  done: !1,
179
130
  hasNext: !0,
180
- next: v
131
+ next: y
181
132
  };
182
133
  };
183
- function e(o) {
184
- return o === "" || o === void 0 ? !0 : Array.isArray(o) ? o.length === 0 : Object.keys(o).length === 0;
185
- }
186
- function t$2(...o) {
187
- return t$10(n$10, o);
134
+ function e(a) {
135
+ return a === "" || a === void 0 ? !0 : Array.isArray(a) ? a.length === 0 : Object.keys(a).length === 0;
188
136
  }
189
- var n$10 = (o) => o.at(-1);
190
- function t$3(...o) {
191
- return t$10(n$9, o, r$3);
137
+ function t$2(...a) {
138
+ return t$6(n$3, a, r$2);
192
139
  }
193
- var n$9 = (o, h) => o.map(h), r$3 = (o) => (h, g, _) => ({
140
+ var n$3 = (a, g) => a.map(g), r$2 = (a) => (g, _, v) => ({
194
141
  done: !1,
195
142
  hasNext: !0,
196
- next: o(h, g, _)
143
+ next: a(g, _, v)
197
144
  });
198
- function t$6(...o) {
199
- return t$10(n$8, o);
200
- }
201
- var n$8 = (o) => o.length === 1 ? o[0] : void 0;
202
- function t$4(...o) {
203
- return t$10(n$7, o);
145
+ function t$3(...a) {
146
+ return t$6(n$2, a);
204
147
  }
205
- function n$7(o, h) {
206
- let g = [];
207
- for (let _ = o; _ < h; _++) g.push(_);
208
- return g;
209
- }
210
- function t$1(...o) {
211
- return t$11(n$6, o);
212
- }
213
- var n$6 = (o, h) => [...o].sort(h);
214
- function n$3(...o) {
215
- return t$10(r$2, o, i);
216
- }
217
- var r$2 = (o, h) => h < 0 ? [] : o.slice(0, h);
218
- function i(o) {
219
- if (o <= 0) return n$5;
220
- let h = o;
221
- return (o) => (--h, {
222
- done: h <= 0,
223
- hasNext: !0,
224
- next: o
225
- });
148
+ function n$2(a, g) {
149
+ let _ = [];
150
+ for (let v = a; v < g; v++) _.push(v);
151
+ return _;
226
152
  }
227
- function n$2(...o) {
228
- return t$9(r$1, o);
153
+ function n$1(...a) {
154
+ return t$5(r$1, a);
229
155
  }
230
156
  function r$1() {
231
- let o = /* @__PURE__ */ new Set();
232
- return (h) => o.has(h) ? t$8 : (o.add(h), {
157
+ let a = /* @__PURE__ */ new Set();
158
+ return (g) => a.has(g) ? t$4 : (a.add(g), {
233
159
  done: !1,
234
160
  hasNext: !0,
235
- next: h
161
+ next: g
236
162
  });
237
163
  }
238
- function n$4(...o) {
239
- return t$9(r, o);
164
+ function n(...a) {
165
+ return t$5(r, a);
240
166
  }
241
- function r(o) {
242
- let h = o, g = /* @__PURE__ */ new Set();
243
- return (o, _, v) => {
244
- let y = h(o, _, v);
245
- return g.has(y) ? t$8 : (g.add(y), {
167
+ function r(a) {
168
+ let g = a, _ = /* @__PURE__ */ new Set();
169
+ return (a, v, y) => {
170
+ let b = g(a, v, y);
171
+ return _.has(b) ? t$4 : (_.add(b), {
246
172
  done: !1,
247
173
  hasNext: !0,
248
- next: o
174
+ next: a
249
175
  });
250
176
  };
251
177
  }
178
+ function disjointIntervals(a) {
179
+ if (a.length === 0) return [];
180
+ let g = [...a].sort((a, g) => a.start - g.start), _ = [g[0]];
181
+ for (let a = 1; a < g.length; a++) {
182
+ let v = g[a], y = _[_.length - 1];
183
+ v.start >= y.end ? _.push(v) : v.end > y.end && (_[_.length - 1] = {
184
+ ...y,
185
+ end: v.end
186
+ });
187
+ }
188
+ return _;
189
+ }
190
+ function decorateIntervals(g, _, v) {
191
+ let y = [], b = 0;
192
+ return g.forEach((g, x) => {
193
+ g.start > b && y.push(/* @__PURE__ */ jsx(Fragment, { children: _.substring(b, g.start) }, `text-${x}`)), y.push(v(_.substring(g.start, g.end), g, x)), b = g.end;
194
+ }), b < _.length && y.push(/* @__PURE__ */ jsx(Fragment, { children: _.substring(b) }, "text-end")), y;
195
+ }
252
196
  var squiggly = {
253
197
  textDecorationLine: "underline",
254
198
  textDecorationStyle: "wavy",
255
199
  textDecorationColor: "red"
256
200
  };
257
- function SyntaxHighlighter({ cstRoot: o, ref: h, wrap: g, syntaxColors: _ }) {
201
+ function SyntaxHighlighter({ cstRoot: a, ref: g, wrap: _, syntaxColors: v }) {
258
202
  return /* @__PURE__ */ jsx(ReadOnlyTextarea, {
259
- ref: h,
260
- wrap: g,
203
+ ref: g,
204
+ wrap: _,
261
205
  children: /* @__PURE__ */ jsx(StyledNode, {
262
- node: o,
263
- styleFor: _
206
+ node: a,
207
+ styleFor: v
264
208
  })
265
209
  });
266
210
  }
267
- function ErrorHighlighter({ ref: o, wrap: h, errors: g, children: _ }) {
268
- let v = `${_} `, [y] = g;
269
- return y ? /* @__PURE__ */ jsxs(ReadOnlyTextarea, {
270
- ref: o,
271
- wrap: h,
211
+ function ErrorHighlighter({ ref: a, wrap: g, errors: _, children: v }) {
212
+ let y = `${v} `, b = disjointIntervals(_);
213
+ return b.length === 0 ? /* @__PURE__ */ jsx(Fragment$1, {}) : /* @__PURE__ */ jsx(ReadOnlyTextarea, {
214
+ ref: a,
215
+ wrap: g,
272
216
  "data-id": "ErrorHighlighter",
273
217
  style: { color: "transparent" },
274
- children: [
275
- v.substring(0, y.start),
276
- /* @__PURE__ */ jsx("span", {
277
- style: squiggly,
278
- title: y.message,
279
- children: v.substring(y.start, y.end)
280
- }),
281
- v.substring(y.end)
282
- ]
283
- }) : /* @__PURE__ */ jsx(Fragment, {});
284
- }
285
- function StyledNode(o) {
286
- let h = o.styleFor(o.node);
287
- return /* @__PURE__ */ jsx("span", {
288
- style: h,
289
- children: e(o.node.children ?? []) ? o.node.text : o.node.children.map((h, g) => /* @__PURE__ */ jsx(StyledNode, {
290
- node: h,
291
- styleFor: o.styleFor
292
- }, g))
218
+ children: decorateIntervals(b, y, (a, g, _) => /* @__PURE__ */ jsx("span", {
219
+ style: squiggly,
220
+ title: g.message,
221
+ children: a
222
+ }, `error-${_}`))
293
223
  });
294
224
  }
295
- function cstPathAt(o, h) {
296
- return _cstPathAt(o, h, []);
297
- }
298
- function _cstPathAt(o, h, g = []) {
299
- return o.offset <= h && h <= o.end ? (g.push(o), o.children?.forEach((o) => _cstPathAt(o, h, g)), g) : [];
300
- }
301
- function getSuggestions(o, h, g = () => void 0) {
302
- let _ = cstPathAt(o, h), v = t(_, t$7((o) => {
303
- if (o.grammar.meta?.name !== void 0) {
304
- let h = g(o);
305
- if (h) return h.map((h) => ({
306
- node: o,
307
- suggestion: h
308
- }));
309
- }
310
- return o.grammar.suggestions().map((h) => ({
311
- node: o,
312
- suggestion: h
313
- }));
314
- }));
315
- return t(v, t$7((o) => {
316
- let g = o.node.text.substring(0, h - o.node.offset);
317
- return o.suggestion.startsWith(g) ? [{
318
- suggestion: o.suggestion,
319
- prefix: g
320
- }] : [];
321
- }), n$4((o) => o.suggestion));
322
- }
323
- const CursorPosition = forwardRef(function(o, h) {
324
- let g = useRef(null), v = useRef(null);
325
- return useImperativeHandle(h, () => ({ getCursorPosition() {
326
- if (!g.current || !v.current) return {
327
- top: 0,
328
- left: 0
329
- };
330
- let { offsetTop: o, offsetLeft: h } = g.current, { scrollTop: _, scrollLeft: y } = v.current, b = window.getComputedStyle(v.current), x = parseInt(b.lineHeight);
331
- return {
332
- top: o - _ + x,
333
- left: h - y
334
- };
335
- } }), []), /* @__PURE__ */ jsxs(ReadOnlyTextarea, {
336
- ref: v,
337
- wrap: o.wrap,
338
- style: { visibility: "hidden" },
339
- children: [o.text, /* @__PURE__ */ jsx("span", {
340
- ref: g,
341
- children: "\xA0"
342
- })]
343
- });
344
- });
345
- function SuggestionsView({ suggestions: o, onSelect: h }) {
346
- return /* @__PURE__ */ jsx("div", {
347
- style: {
348
- display: "flex",
349
- gap: 4,
350
- padding: "4px 0",
351
- overflowY: "auto"
352
- },
353
- children: o.map((o, g) => /* @__PURE__ */ jsxs("button", {
354
- onClick: () => h(o),
355
- children: [
356
- "\xA0",
357
- o,
358
- "\xA0"
359
- ]
360
- }, g))
225
+ function StyledNode(a) {
226
+ let g = a.styleFor(a.node);
227
+ return /* @__PURE__ */ jsx("span", {
228
+ style: g,
229
+ "data-node": a.node.grammar.type,
230
+ "data-node-error": a.node.recoverableError,
231
+ "data-node-meta": a.node.grammar.meta ? JSON.stringify(a.node.grammar.meta) : void 0,
232
+ children: e(a.node.children ?? []) ? a.node.text : a.node.children.map((g, _) => /* @__PURE__ */ jsx(StyledNode, {
233
+ node: g,
234
+ styleFor: a.styleFor
235
+ }, _))
361
236
  });
362
237
  }
363
- function shortcutName(o) {
364
- let h = "";
365
- o.ctrlKey && (h = "Ctrl");
366
- let { key: g } = o;
367
- return g === " " && (g = "Space"), `${h}${g}`;
238
+ function cstPathAt(a, g) {
239
+ return _cstPathAt(a, g, []);
368
240
  }
369
- function useSyncScroll(...o) {
370
- return useCallback((h) => {
371
- let g = h.currentTarget;
372
- o.forEach((o) => {
373
- o.current && g && (o.current.scrollTop = g.scrollTop, o.current.scrollLeft = g.scrollLeft);
374
- });
375
- }, [o]);
241
+ function _cstPathAt(a, g, _ = []) {
242
+ return a.offset <= g && g <= a.end ? (_.push(a), a.children?.forEach((a) => _cstPathAt(a, g, _)), _) : [];
376
243
  }
377
244
  var ParsingError = class extends Error {
378
- constructor(o) {
379
- super(o);
245
+ constructor(a) {
246
+ super(a);
380
247
  }
381
248
  };
382
- function success(o) {
383
- return o;
249
+ function success(a) {
250
+ return a;
384
251
  }
385
- function error(o) {
386
- return {
387
- ...o,
388
- offset: 0
389
- };
252
+ function error(a) {
253
+ return a;
390
254
  }
391
- function isParserError(o) {
392
- return "expected" in o;
255
+ function isParserError(a) {
256
+ return "expected" in a;
393
257
  }
394
- function isParserSuccess(o) {
395
- return !isParserError(o);
258
+ function isParserSuccess(a) {
259
+ return !isParserError(a);
396
260
  }
397
- function asException(o) {
398
- return new ParsingError(`[${o.grammar.type}]: Expected ${o.expected.map((o) => `'${o}'`).join(" | ")}, but got '${o.got}'`);
261
+ function asException(a) {
262
+ return new ParsingError(`[${a.grammar.type}]: Expected ${a.expected.map((a) => `'${a}'`).join(" | ")}, but got '${a.got}'`);
399
263
  }
400
- function sequence(...o) {
401
- let h = {
402
- children: o,
403
- type: "sequence",
404
- suggestions() {
405
- return t(o, t$3((o) => o.suggestions()), n$3(1), t$6(), t$5([]));
406
- },
407
- parse(g, _) {
408
- let v = 0, y = [], b = !1;
409
- for (let x of o) {
410
- let o = g.substring(v), S = _.faultCorrection(x.parse(o, _), h);
411
- if (isParserError(S)) {
412
- if (_.faultTolerant) {
413
- b = !0;
414
- continue;
415
- }
416
- return {
417
- ...S,
418
- offset: v
419
- };
264
+ function alternative(...a) {
265
+ let g = {
266
+ type: "alternative",
267
+ children: a,
268
+ parse(_, v) {
269
+ let y = {
270
+ ...v,
271
+ path: [...v.path, g]
272
+ }, b = [];
273
+ for (let g of a) {
274
+ let a = g.parse(_, y);
275
+ if (isParserError(a)) {
276
+ b.push(a);
277
+ continue;
420
278
  }
421
- v += S.text.length, y.push({
422
- ...S,
423
- recoverableError: b
424
- }), b = !1;
279
+ return a;
425
280
  }
426
- return success({
427
- grammar: h,
428
- text: g.substring(0, v),
429
- children: y
281
+ return error({
282
+ offset: 0,
283
+ path: y.path,
284
+ grammar: g,
285
+ got: _,
286
+ expected: b.flatMap((a) => a.expected)
430
287
  });
288
+ },
289
+ suggestions() {
290
+ return a.flatMap((a) => a.suggestions());
431
291
  }
432
292
  };
433
- return h;
293
+ return g;
434
294
  }
435
- const eof = {
436
- type: "eof",
437
- suggestions: () => [],
438
- children: [],
439
- parse(o) {
440
- return o.length === 0 ? success({
441
- text: o,
442
- grammar: eof,
443
- children: []
444
- }) : error({
445
- got: o,
446
- expected: [""],
447
- grammar: eof
448
- });
449
- }
450
- };
451
- function withOffset(o, h = 0) {
452
- let g = h;
453
- return {
454
- text: o.text,
455
- offset: h,
456
- end: h + o.text.length,
457
- children: o.children?.map((o, h, _) => {
458
- let { text: v } = _[h - 1] ?? { text: "" };
459
- return g += v.length, withOffset(o, g);
460
- }),
461
- grammar: o.grammar,
462
- recoverableError: o.recoverableError ?? !1
295
+ function named(a, g) {
296
+ let _ = {
297
+ meta: { name: a },
298
+ type: "named",
299
+ suggestions() {
300
+ return g.suggestions();
301
+ },
302
+ children: [g],
303
+ parse(a, v) {
304
+ let y = g.parse(a, v);
305
+ return isParserError(y) ? y : success({
306
+ text: y.text,
307
+ grammar: _,
308
+ children: [y]
309
+ });
310
+ }
463
311
  };
312
+ return _;
464
313
  }
465
- function _flatCST(o) {
466
- return o.children && !e(o.children) ? o.children.flatMap((o) => flatCST(o)) : [o];
467
- }
468
- function flatCST(o) {
469
- return _flatCST(o).filter((o) => o.text !== "");
470
- }
471
- function getErrors(o) {
472
- let h = o.children?.flatMap((o) => getErrors(o)) ?? [];
473
- return o.recoverableError ? [{
474
- start: o.offset,
475
- end: Math.max(o.end, o.offset + 1),
476
- message: o.grammar.type
477
- }, ...h] : h;
478
- }
479
- var DSLParser = class {
480
- grammar;
481
- constructor(o) {
482
- this.grammar = sequence(o, eof);
483
- }
484
- parse(o) {
485
- let h = this.grammar.parse(o, {
486
- faultTolerant: !1,
487
- faultCorrection: (o) => o
488
- }), g = isParserError(h) ? this.grammar.parse(o, {
489
- faultTolerant: !0,
490
- faultCorrection: (o) => o
491
- }) : h;
492
- if (isParserError(g)) throw asException(g);
493
- let _ = withOffset(g), v = flatCST(_);
494
- return {
495
- cst: _,
496
- terminals: v,
497
- result: g,
498
- strictResult: isParserSuccess(h) ? h : void 0,
499
- errors: getErrors(_)
500
- };
501
- }
502
- }, themes = {
503
- light: [
504
- "#FF0000",
505
- "#0000FF",
506
- "#FF00FF",
507
- "#800000",
508
- "#008000",
509
- "#000080",
510
- "#808000",
511
- "#800080",
512
- "#008080",
513
- "#808080",
514
- "#993366",
515
- "#336699"
516
- ],
517
- dark: [
518
- "#FF5555",
519
- "#5555FF",
520
- "#FF55FF",
521
- "#FF5555",
522
- "#55FF55",
523
- "#5555FF",
524
- "#FFFF55",
525
- "#FF55FF",
526
- "#55FFFF",
527
- "#AAAAAA",
528
- "#CC99FF",
529
- "#99CCFF"
530
- ]
531
- }, colorAssignments = /* @__PURE__ */ new Map();
532
- function getColorAssigment(o, h) {
533
- let g = String(o), _ = colorAssignments.get(g);
534
- if (_) return _;
535
- let v = h[colorAssignments.size % h.length];
536
- return colorAssignments.set(g, v), console.log(colorAssignments), v;
537
- }
538
- function defaultStyleFor(o, h) {
539
- let { name: g, regex: _ } = o.grammar.meta ?? {}, v = g ?? _;
540
- if (v) return { color: getColorAssigment(v, h) };
541
- }
542
- function defaultSyntaxColors(o) {
543
- return (h) => defaultStyleFor(h, themes[o]);
314
+ function nodeName(a) {
315
+ return a.grammar.meta?.name;
544
316
  }
545
- function SuggestionsMenu({ suggestions: o, onSelect: h, style: g, selectedIndex: _, onHover: v }) {
546
- return /* @__PURE__ */ jsx("div", {
547
- style: {
548
- position: "absolute",
549
- ...g,
550
- background: "#252526",
551
- border: "1px solid #454545",
552
- boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
553
- color: "#cccccc",
554
- fontFamily: "monospace",
555
- fontSize: "0.9em",
556
- minWidth: 200,
557
- zIndex: 100
558
- },
559
- children: o.map((o, g) => /* @__PURE__ */ jsx("div", {
560
- onClick: () => h(o),
561
- onMouseOver: () => v(g),
562
- style: {
563
- cursor: "pointer",
564
- padding: "4px 8px",
565
- backgroundColor: _ === g ? "#094771" : "transparent"
566
- },
567
- children: o
568
- }, g))
569
- });
570
- }
571
- function DslEditor({ code: o, onChange: _, onParsed: x, grammar: w, wrap: T = !1, suggestions: E, className: D = DslEditor.name, syntaxColors: O = defaultSyntaxColors("light"),...k }) {
572
- let [A, j] = useState([]), [M, P] = useState(), [F, I] = useState({
573
- top: 0,
574
- left: 0,
575
- visible: !1
576
- }), [L, R] = useState(0), [z, B] = useState(""), V = useRef(null), H = useRef(null), U = useRef(null), W = useRef(null), G = useRef(null), K = useCallback((o) => {
577
- let h = H.current?.selectionStart ?? 0, g = getSuggestions(o, h, E);
578
- j(g);
579
- }, [E]), q = useCallback(() => {
580
- M && (K(M.cst), B(H.current?.value?.substring(0, H.current?.selectionStart ?? 0) ?? ""));
581
- }, [M, K]);
582
- useEffect(() => {
583
- V.current = new DSLParser(w);
584
- }, [w]), useEffect(() => {
585
- if (!V.current) return;
586
- let h = V.current.parse(o);
587
- P(h), x?.(h), K(h.cst), B(o.substring(0, H.current?.selectionStart ?? 0));
588
- }, [
589
- o,
590
- x,
591
- K
592
- ]);
593
- let J = useCallback(() => U.current?.getCursorPosition?.() ?? {
594
- top: 0,
595
- left: 0
596
- }, []), Y = useCallback((o) => {
597
- H.current && console.log(o);
598
- }, [
599
- o,
600
- _,
601
- A
602
- ]), X = useMemo(() => ({
603
- ArrowDown() {
604
- R((o) => (o + 1) % A.length);
605
- },
606
- ArrowUp() {
607
- R((o) => (o - 1 + A.length) % A.length);
608
- },
609
- Enter() {
610
- A[L] && Y(A[L].suggestion);
611
- },
612
- Escape() {
613
- I((o) => ({
614
- ...o,
615
- visible: !1
616
- }));
317
+ function optional(a) {
318
+ let g = {
319
+ type: "optional",
320
+ suggestions: () => ["", ...a.suggestions()],
321
+ children: [a],
322
+ parse(_, v) {
323
+ let y = {
324
+ ...v,
325
+ path: [...v.path, g]
326
+ }, b = a.parse(_, y);
327
+ return isParserError(b) ? success({
328
+ text: "",
329
+ grammar: g,
330
+ children: []
331
+ }) : b;
617
332
  }
618
- }), [
619
- Y,
620
- L,
621
- A
622
- ]), Z = useMemo(() => ({ CtrlSpace() {
623
- let { top: o, left: h } = J();
624
- R(0), I({
625
- visible: !0,
626
- top: o,
627
- left: h
628
- });
629
- } }), [J]), Q = useCallback((o) => {
630
- let h = (F.visible ? X : Z)[shortcutName(o)];
631
- h && (o.preventDefault(), h());
632
- }, [
633
- F.visible,
634
- X,
635
- Z
636
- ]), $ = useCallback((o) => {
637
- _(o.target.value), I((o) => ({
638
- ...o,
639
- visible: !1
640
- })), B(o.target.value.substring(0, o.target.selectionStart));
641
- }, [_]);
642
- return /* @__PURE__ */ jsxs("div", {
643
- style: {
644
- display: "grid",
645
- gridTemplateRows: "1fr auto",
646
- flex: 1,
647
- width: "100%",
648
- height: "100%"
649
- },
650
- className: D,
651
- children: [
652
- /* @__PURE__ */ jsxs("div", {
653
- style: {
654
- position: "relative",
655
- border: "1px solid black",
656
- overflow: "hidden"
657
- },
658
- children: [
659
- /* @__PURE__ */ jsx("textarea", {
660
- ref: H,
661
- spellCheck: !1,
662
- wrap: T ? "soft" : "off",
663
- style: {
664
- ...textStyle,
665
- color: "transparent",
666
- background: "transparent",
667
- caretColor: "black",
668
- border: "none",
669
- resize: "none"
670
- },
671
- value: o,
672
- onSelect: q,
673
- onChange: $,
674
- onScroll: useSyncScroll(W, G),
675
- onKeyDown: Q,
676
- ...k
677
- }),
678
- M && /* @__PURE__ */ jsx(SyntaxHighlighter, {
679
- cstRoot: M.cst,
680
- ref: W,
681
- wrap: T,
682
- syntaxColors: O
683
- }),
684
- !e(M?.errors ?? []) && /* @__PURE__ */ jsx(ErrorHighlighter, {
685
- ref: G,
686
- errors: M?.errors ?? [],
687
- wrap: T,
688
- children: o
689
- }),
690
- /* @__PURE__ */ jsx(CursorPosition, {
691
- ref: U,
692
- text: z,
693
- wrap: T
694
- }),
695
- F.visible && A.length > 0 && /* @__PURE__ */ jsx(SuggestionsMenu, {
696
- suggestions: A.map((o) => o.suggestion),
697
- onSelect: Y,
698
- style: {
699
- top: F.top,
700
- left: F.left
701
- },
702
- selectedIndex: L,
703
- onHover: R
704
- })
705
- ]
706
- }),
707
- JSON.stringify(M?.errors ?? []),
708
- /* @__PURE__ */ jsx(SuggestionsView, {
709
- suggestions: A.map((o) => o.suggestion),
710
- onSelect: Y
711
- })
712
- ]
713
- });
714
- }
715
- function visit(o, h, g = (o) => o.text) {
716
- return isParserError(o) ? [] : (((o) => o.grammar.meta?.name !== void 0 && h.includes(o.grammar.meta?.name))(o) ? [g(o)] : []).concat(...o.children?.flatMap((o) => visit(o, h, g)) ?? []);
333
+ };
334
+ return g;
717
335
  }
718
- var require_types = /* @__PURE__ */ __commonJSMin(((o, h) => {
719
- h.exports = {
336
+ var require_types = /* @__PURE__ */ __commonJSMin(((a, g) => {
337
+ g.exports = {
720
338
  ROOT: 0,
721
339
  GROUP: 1,
722
340
  POSITION: 2,
@@ -726,137 +344,137 @@ var require_types = /* @__PURE__ */ __commonJSMin(((o, h) => {
726
344
  REFERENCE: 6,
727
345
  CHAR: 7
728
346
  };
729
- })), require_sets = /* @__PURE__ */ __commonJSMin(((o) => {
730
- var h = require_types(), g = () => [{
731
- type: h.RANGE,
347
+ })), require_sets = /* @__PURE__ */ __commonJSMin(((a) => {
348
+ var g = require_types(), _ = () => [{
349
+ type: g.RANGE,
732
350
  from: 48,
733
351
  to: 57
734
- }], _ = () => [
352
+ }], v = () => [
735
353
  {
736
- type: h.CHAR,
354
+ type: g.CHAR,
737
355
  value: 95
738
356
  },
739
357
  {
740
- type: h.RANGE,
358
+ type: g.RANGE,
741
359
  from: 97,
742
360
  to: 122
743
361
  },
744
362
  {
745
- type: h.RANGE,
363
+ type: g.RANGE,
746
364
  from: 65,
747
365
  to: 90
748
366
  }
749
- ].concat(g()), v = () => [
367
+ ].concat(_()), y = () => [
750
368
  {
751
- type: h.CHAR,
369
+ type: g.CHAR,
752
370
  value: 9
753
371
  },
754
372
  {
755
- type: h.CHAR,
373
+ type: g.CHAR,
756
374
  value: 10
757
375
  },
758
376
  {
759
- type: h.CHAR,
377
+ type: g.CHAR,
760
378
  value: 11
761
379
  },
762
380
  {
763
- type: h.CHAR,
381
+ type: g.CHAR,
764
382
  value: 12
765
383
  },
766
384
  {
767
- type: h.CHAR,
385
+ type: g.CHAR,
768
386
  value: 13
769
387
  },
770
388
  {
771
- type: h.CHAR,
389
+ type: g.CHAR,
772
390
  value: 32
773
391
  },
774
392
  {
775
- type: h.CHAR,
393
+ type: g.CHAR,
776
394
  value: 160
777
395
  },
778
396
  {
779
- type: h.CHAR,
397
+ type: g.CHAR,
780
398
  value: 5760
781
399
  },
782
400
  {
783
- type: h.RANGE,
401
+ type: g.RANGE,
784
402
  from: 8192,
785
403
  to: 8202
786
404
  },
787
405
  {
788
- type: h.CHAR,
406
+ type: g.CHAR,
789
407
  value: 8232
790
408
  },
791
409
  {
792
- type: h.CHAR,
410
+ type: g.CHAR,
793
411
  value: 8233
794
412
  },
795
413
  {
796
- type: h.CHAR,
414
+ type: g.CHAR,
797
415
  value: 8239
798
416
  },
799
417
  {
800
- type: h.CHAR,
418
+ type: g.CHAR,
801
419
  value: 8287
802
420
  },
803
421
  {
804
- type: h.CHAR,
422
+ type: g.CHAR,
805
423
  value: 12288
806
424
  },
807
425
  {
808
- type: h.CHAR,
426
+ type: g.CHAR,
809
427
  value: 65279
810
428
  }
811
- ], y = () => [
429
+ ], b = () => [
812
430
  {
813
- type: h.CHAR,
431
+ type: g.CHAR,
814
432
  value: 10
815
433
  },
816
434
  {
817
- type: h.CHAR,
435
+ type: g.CHAR,
818
436
  value: 13
819
437
  },
820
438
  {
821
- type: h.CHAR,
439
+ type: g.CHAR,
822
440
  value: 8232
823
441
  },
824
442
  {
825
- type: h.CHAR,
443
+ type: g.CHAR,
826
444
  value: 8233
827
445
  }
828
446
  ];
829
- o.words = () => ({
830
- type: h.SET,
831
- set: _(),
447
+ a.words = () => ({
448
+ type: g.SET,
449
+ set: v(),
832
450
  not: !1
833
- }), o.notWords = () => ({
834
- type: h.SET,
835
- set: _(),
451
+ }), a.notWords = () => ({
452
+ type: g.SET,
453
+ set: v(),
836
454
  not: !0
837
- }), o.ints = () => ({
838
- type: h.SET,
839
- set: g(),
455
+ }), a.ints = () => ({
456
+ type: g.SET,
457
+ set: _(),
840
458
  not: !1
841
- }), o.notInts = () => ({
842
- type: h.SET,
843
- set: g(),
459
+ }), a.notInts = () => ({
460
+ type: g.SET,
461
+ set: _(),
844
462
  not: !0
845
- }), o.whitespace = () => ({
846
- type: h.SET,
847
- set: v(),
463
+ }), a.whitespace = () => ({
464
+ type: g.SET,
465
+ set: y(),
848
466
  not: !1
849
- }), o.notWhitespace = () => ({
850
- type: h.SET,
851
- set: v(),
852
- not: !0
853
- }), o.anyChar = () => ({
854
- type: h.SET,
467
+ }), a.notWhitespace = () => ({
468
+ type: g.SET,
855
469
  set: y(),
856
470
  not: !0
471
+ }), a.anyChar = () => ({
472
+ type: g.SET,
473
+ set: b(),
474
+ not: !0
857
475
  });
858
- })), require_util = /* @__PURE__ */ __commonJSMin(((o) => {
859
- var h = require_types(), g = require_sets(), _ = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ ?", v = {
476
+ })), require_util = /* @__PURE__ */ __commonJSMin(((a) => {
477
+ var g = require_types(), _ = require_sets(), v = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ ?", y = {
860
478
  0: 0,
861
479
  t: 9,
862
480
  n: 10,
@@ -864,515 +482,828 @@ var require_types = /* @__PURE__ */ __commonJSMin(((o, h) => {
864
482
  f: 12,
865
483
  r: 13
866
484
  };
867
- o.strToChars = function(o) {
868
- return o = o.replace(/(\[\\b\])|(\\)?\\(?:u([A-F0-9]{4})|x([A-F0-9]{2})|(0?[0-7]{2})|c([@A-Z[\\\]^?])|([0tnvfr]))/g, function(o, h, g, y, b, x, S, C) {
869
- if (g) return o;
870
- var w = h ? 8 : y ? parseInt(y, 16) : b ? parseInt(b, 16) : x ? parseInt(x, 8) : S ? _.indexOf(S) : v[C], T = String.fromCharCode(w);
871
- return /[[\]{}^$.|?*+()]/.test(T) && (T = "\\" + T), T;
872
- }), o;
873
- }, o.tokenizeClass = (_, v) => {
874
- for (var y = [], b = /\\(?:(w)|(d)|(s)|(W)|(D)|(S))|((?:(?:\\)(.)|([^\]\\]))-(?:\\)?([^\]]))|(\])|(?:\\)?([^])/g, x, S; (x = b.exec(_)) != null;) if (x[1]) y.push(g.words());
875
- else if (x[2]) y.push(g.ints());
876
- else if (x[3]) y.push(g.whitespace());
877
- else if (x[4]) y.push(g.notWords());
878
- else if (x[5]) y.push(g.notInts());
879
- else if (x[6]) y.push(g.notWhitespace());
880
- else if (x[7]) y.push({
881
- type: h.RANGE,
882
- from: (x[8] || x[9]).charCodeAt(0),
883
- to: x[10].charCodeAt(0)
485
+ a.strToChars = function(a) {
486
+ return a = a.replace(/(\[\\b\])|(\\)?\\(?:u([A-F0-9]{4})|x([A-F0-9]{2})|(0?[0-7]{2})|c([@A-Z[\\\]^?])|([0tnvfr]))/g, function(a, g, _, b, x, S, C, w) {
487
+ if (_) return a;
488
+ var T = g ? 8 : b ? parseInt(b, 16) : x ? parseInt(x, 16) : S ? parseInt(S, 8) : C ? v.indexOf(C) : y[w], E = String.fromCharCode(T);
489
+ return /[[\]{}^$.|?*+()]/.test(E) && (E = "\\" + E), E;
490
+ }), a;
491
+ }, a.tokenizeClass = (v, y) => {
492
+ for (var b = [], x = /\\(?:(w)|(d)|(s)|(W)|(D)|(S))|((?:(?:\\)(.)|([^\]\\]))-(?:\\)?([^\]]))|(\])|(?:\\)?([^])/g, S, C; (S = x.exec(v)) != null;) if (S[1]) b.push(_.words());
493
+ else if (S[2]) b.push(_.ints());
494
+ else if (S[3]) b.push(_.whitespace());
495
+ else if (S[4]) b.push(_.notWords());
496
+ else if (S[5]) b.push(_.notInts());
497
+ else if (S[6]) b.push(_.notWhitespace());
498
+ else if (S[7]) b.push({
499
+ type: g.RANGE,
500
+ from: (S[8] || S[9]).charCodeAt(0),
501
+ to: S[10].charCodeAt(0)
884
502
  });
885
- else if (S = x[12]) y.push({
886
- type: h.CHAR,
887
- value: S.charCodeAt(0)
503
+ else if (C = S[12]) b.push({
504
+ type: g.CHAR,
505
+ value: C.charCodeAt(0)
888
506
  });
889
- else return [y, b.lastIndex];
890
- o.error(v, "Unterminated character class");
891
- }, o.error = (o, h) => {
892
- throw SyntaxError("Invalid regular expression: /" + o + "/: " + h);
507
+ else return [b, x.lastIndex];
508
+ a.error(y, "Unterminated character class");
509
+ }, a.error = (a, g) => {
510
+ throw SyntaxError("Invalid regular expression: /" + a + "/: " + g);
893
511
  };
894
- })), require_positions = /* @__PURE__ */ __commonJSMin(((o) => {
895
- var h = require_types();
896
- o.wordBoundary = () => ({
897
- type: h.POSITION,
512
+ })), require_positions = /* @__PURE__ */ __commonJSMin(((a) => {
513
+ var g = require_types();
514
+ a.wordBoundary = () => ({
515
+ type: g.POSITION,
898
516
  value: "b"
899
- }), o.nonWordBoundary = () => ({
900
- type: h.POSITION,
517
+ }), a.nonWordBoundary = () => ({
518
+ type: g.POSITION,
901
519
  value: "B"
902
- }), o.begin = () => ({
903
- type: h.POSITION,
520
+ }), a.begin = () => ({
521
+ type: g.POSITION,
904
522
  value: "^"
905
- }), o.end = () => ({
906
- type: h.POSITION,
523
+ }), a.end = () => ({
524
+ type: g.POSITION,
907
525
  value: "$"
908
526
  });
909
- })), require_lib$1 = /* @__PURE__ */ __commonJSMin(((o, h) => {
910
- var g = require_util(), _ = require_types(), v = require_sets(), y = require_positions();
911
- h.exports = (o) => {
912
- var h = 0, b, x, S = {
913
- type: _.ROOT,
527
+ })), require_lib$1 = /* @__PURE__ */ __commonJSMin(((a, g) => {
528
+ var _ = require_util(), v = require_types(), y = require_sets(), b = require_positions();
529
+ g.exports = (a) => {
530
+ var g = 0, x, S, C = {
531
+ type: v.ROOT,
914
532
  stack: []
915
- }, C = S, w = S.stack, T = [], E = (h) => {
916
- g.error(o, `Nothing to repeat at column ${h - 1}`);
917
- }, D = g.strToChars(o);
918
- for (b = D.length; h < b;) switch (x = D[h++], x) {
533
+ }, w = C, T = C.stack, E = [], D = (g) => {
534
+ _.error(a, `Nothing to repeat at column ${g - 1}`);
535
+ }, O = _.strToChars(a);
536
+ for (x = O.length; g < x;) switch (S = O[g++], S) {
919
537
  case "\\":
920
- switch (x = D[h++], x) {
538
+ switch (S = O[g++], S) {
921
539
  case "b":
922
- w.push(y.wordBoundary());
540
+ T.push(b.wordBoundary());
923
541
  break;
924
542
  case "B":
925
- w.push(y.nonWordBoundary());
543
+ T.push(b.nonWordBoundary());
926
544
  break;
927
545
  case "w":
928
- w.push(v.words());
546
+ T.push(y.words());
929
547
  break;
930
548
  case "W":
931
- w.push(v.notWords());
549
+ T.push(y.notWords());
932
550
  break;
933
551
  case "d":
934
- w.push(v.ints());
552
+ T.push(y.ints());
935
553
  break;
936
554
  case "D":
937
- w.push(v.notInts());
555
+ T.push(y.notInts());
938
556
  break;
939
557
  case "s":
940
- w.push(v.whitespace());
558
+ T.push(y.whitespace());
941
559
  break;
942
560
  case "S":
943
- w.push(v.notWhitespace());
561
+ T.push(y.notWhitespace());
944
562
  break;
945
- default: /\d/.test(x) ? w.push({
946
- type: _.REFERENCE,
947
- value: parseInt(x, 10)
948
- }) : w.push({
949
- type: _.CHAR,
950
- value: x.charCodeAt(0)
563
+ default: /\d/.test(S) ? T.push({
564
+ type: v.REFERENCE,
565
+ value: parseInt(S, 10)
566
+ }) : T.push({
567
+ type: v.CHAR,
568
+ value: S.charCodeAt(0)
951
569
  });
952
570
  }
953
571
  break;
954
572
  case "^":
955
- w.push(y.begin());
573
+ T.push(b.begin());
956
574
  break;
957
575
  case "$":
958
- w.push(y.end());
576
+ T.push(b.end());
959
577
  break;
960
578
  case "[":
961
- var O;
962
- D[h] === "^" ? (O = !0, h++) : O = !1;
963
- var k = g.tokenizeClass(D.slice(h), o);
964
- h += k[1], w.push({
965
- type: _.SET,
966
- set: k[0],
967
- not: O
579
+ var k;
580
+ O[g] === "^" ? (k = !0, g++) : k = !1;
581
+ var A = _.tokenizeClass(O.slice(g), a);
582
+ g += A[1], T.push({
583
+ type: v.SET,
584
+ set: A[0],
585
+ not: k
968
586
  });
969
587
  break;
970
588
  case ".":
971
- w.push(v.anyChar());
589
+ T.push(y.anyChar());
972
590
  break;
973
591
  case "(":
974
- var A = {
975
- type: _.GROUP,
592
+ var j = {
593
+ type: v.GROUP,
976
594
  stack: [],
977
595
  remember: !0
978
596
  };
979
- x = D[h], x === "?" && (x = D[h + 1], h += 2, x === "=" ? A.followedBy = !0 : x === "!" ? A.notFollowedBy = !0 : x !== ":" && g.error(o, `Invalid group, character '${x}' after '?' at column ${h - 1}`), A.remember = !1), w.push(A), T.push(C), C = A, w = A.stack;
597
+ S = O[g], S === "?" && (S = O[g + 1], g += 2, S === "=" ? j.followedBy = !0 : S === "!" ? j.notFollowedBy = !0 : S !== ":" && _.error(a, `Invalid group, character '${S}' after '?' at column ${g - 1}`), j.remember = !1), T.push(j), E.push(w), w = j, T = j.stack;
980
598
  break;
981
599
  case ")":
982
- T.length === 0 && g.error(o, `Unmatched ) at column ${h - 1}`), C = T.pop(), w = C.options ? C.options[C.options.length - 1] : C.stack;
600
+ E.length === 0 && _.error(a, `Unmatched ) at column ${g - 1}`), w = E.pop(), T = w.options ? w.options[w.options.length - 1] : w.stack;
983
601
  break;
984
602
  case "|":
985
- C.options || (C.options = [C.stack], delete C.stack);
986
- var j = [];
987
- C.options.push(j), w = j;
603
+ w.options || (w.options = [w.stack], delete w.stack);
604
+ var M = [];
605
+ w.options.push(M), T = M;
988
606
  break;
989
607
  case "{":
990
- var M = /^(\d+)(,(\d+)?)?\}/.exec(D.slice(h)), N, P;
991
- M === null ? w.push({
992
- type: _.CHAR,
608
+ var N = /^(\d+)(,(\d+)?)?\}/.exec(O.slice(g)), P, F;
609
+ N === null ? T.push({
610
+ type: v.CHAR,
993
611
  value: 123
994
- }) : (w.length === 0 && E(h), N = parseInt(M[1], 10), P = M[2] ? M[3] ? parseInt(M[3], 10) : Infinity : N, h += M[0].length, w.push({
995
- type: _.REPETITION,
996
- min: N,
997
- max: P,
998
- value: w.pop()
612
+ }) : (T.length === 0 && D(g), P = parseInt(N[1], 10), F = N[2] ? N[3] ? parseInt(N[3], 10) : Infinity : P, g += N[0].length, T.push({
613
+ type: v.REPETITION,
614
+ min: P,
615
+ max: F,
616
+ value: T.pop()
999
617
  }));
1000
618
  break;
1001
619
  case "?":
1002
- w.length === 0 && E(h), w.push({
1003
- type: _.REPETITION,
620
+ T.length === 0 && D(g), T.push({
621
+ type: v.REPETITION,
1004
622
  min: 0,
1005
623
  max: 1,
1006
- value: w.pop()
624
+ value: T.pop()
1007
625
  });
1008
626
  break;
1009
627
  case "+":
1010
- w.length === 0 && E(h), w.push({
1011
- type: _.REPETITION,
628
+ T.length === 0 && D(g), T.push({
629
+ type: v.REPETITION,
1012
630
  min: 1,
1013
631
  max: Infinity,
1014
- value: w.pop()
632
+ value: T.pop()
1015
633
  });
1016
634
  break;
1017
635
  case "*":
1018
- w.length === 0 && E(h), w.push({
1019
- type: _.REPETITION,
636
+ T.length === 0 && D(g), T.push({
637
+ type: v.REPETITION,
1020
638
  min: 0,
1021
639
  max: Infinity,
1022
- value: w.pop()
640
+ value: T.pop()
1023
641
  });
1024
642
  break;
1025
- default: w.push({
1026
- type: _.CHAR,
1027
- value: x.charCodeAt(0)
643
+ default: T.push({
644
+ type: v.CHAR,
645
+ value: S.charCodeAt(0)
1028
646
  });
1029
647
  }
1030
- return T.length !== 0 && g.error(o, "Unterminated group"), S;
1031
- }, h.exports.types = _;
1032
- })), require_lib = /* @__PURE__ */ __commonJSMin(((o, h) => {
1033
- var g = class o {
1034
- constructor(o, h) {
1035
- this.low = o, this.high = h, this.length = 1 + h - o;
648
+ return E.length !== 0 && _.error(a, "Unterminated group"), C;
649
+ }, g.exports.types = v;
650
+ })), require_lib = /* @__PURE__ */ __commonJSMin(((a, g) => {
651
+ var _ = class a {
652
+ constructor(a, g) {
653
+ this.low = a, this.high = g, this.length = 1 + g - a;
1036
654
  }
1037
- overlaps(o) {
1038
- return !(this.high < o.low || this.low > o.high);
655
+ overlaps(a) {
656
+ return !(this.high < a.low || this.low > a.high);
1039
657
  }
1040
- touches(o) {
1041
- return !(this.high + 1 < o.low || this.low - 1 > o.high);
658
+ touches(a) {
659
+ return !(this.high + 1 < a.low || this.low - 1 > a.high);
1042
660
  }
1043
- add(h) {
1044
- return new o(Math.min(this.low, h.low), Math.max(this.high, h.high));
661
+ add(g) {
662
+ return new a(Math.min(this.low, g.low), Math.max(this.high, g.high));
1045
663
  }
1046
- subtract(h) {
1047
- return h.low <= this.low && h.high >= this.high ? [] : h.low > this.low && h.high < this.high ? [new o(this.low, h.low - 1), new o(h.high + 1, this.high)] : h.low <= this.low ? [new o(h.high + 1, this.high)] : [new o(this.low, h.low - 1)];
664
+ subtract(g) {
665
+ return g.low <= this.low && g.high >= this.high ? [] : g.low > this.low && g.high < this.high ? [new a(this.low, g.low - 1), new a(g.high + 1, this.high)] : g.low <= this.low ? [new a(g.high + 1, this.high)] : [new a(this.low, g.low - 1)];
1048
666
  }
1049
667
  toString() {
1050
668
  return this.low == this.high ? this.low.toString() : this.low + "-" + this.high;
1051
669
  }
1052
670
  };
1053
- h.exports = class o {
1054
- constructor(o, h) {
1055
- this.ranges = [], this.length = 0, o != null && this.add(o, h);
671
+ g.exports = class a {
672
+ constructor(a, g) {
673
+ this.ranges = [], this.length = 0, a != null && this.add(a, g);
1056
674
  }
1057
675
  _update_length() {
1058
- this.length = this.ranges.reduce((o, h) => o + h.length, 0);
676
+ this.length = this.ranges.reduce((a, g) => a + g.length, 0);
1059
677
  }
1060
- add(h, _) {
1061
- var v = (o) => {
1062
- for (var h = 0; h < this.ranges.length && !o.touches(this.ranges[h]);) h++;
1063
- for (var g = this.ranges.slice(0, h); h < this.ranges.length && o.touches(this.ranges[h]);) o = o.add(this.ranges[h]), h++;
1064
- g.push(o), this.ranges = g.concat(this.ranges.slice(h)), this._update_length();
678
+ add(g, v) {
679
+ var y = (a) => {
680
+ for (var g = 0; g < this.ranges.length && !a.touches(this.ranges[g]);) g++;
681
+ for (var _ = this.ranges.slice(0, g); g < this.ranges.length && a.touches(this.ranges[g]);) a = a.add(this.ranges[g]), g++;
682
+ _.push(a), this.ranges = _.concat(this.ranges.slice(g)), this._update_length();
1065
683
  };
1066
- return h instanceof o ? h.ranges.forEach(v) : (_ ??= h, v(new g(h, _))), this;
684
+ return g instanceof a ? g.ranges.forEach(y) : (v ??= g, y(new _(g, v))), this;
1067
685
  }
1068
- subtract(h, _) {
1069
- var v = (o) => {
1070
- for (var h = 0; h < this.ranges.length && !o.overlaps(this.ranges[h]);) h++;
1071
- for (var g = this.ranges.slice(0, h); h < this.ranges.length && o.overlaps(this.ranges[h]);) g = g.concat(this.ranges[h].subtract(o)), h++;
1072
- this.ranges = g.concat(this.ranges.slice(h)), this._update_length();
686
+ subtract(g, v) {
687
+ var y = (a) => {
688
+ for (var g = 0; g < this.ranges.length && !a.overlaps(this.ranges[g]);) g++;
689
+ for (var _ = this.ranges.slice(0, g); g < this.ranges.length && a.overlaps(this.ranges[g]);) _ = _.concat(this.ranges[g].subtract(a)), g++;
690
+ this.ranges = _.concat(this.ranges.slice(g)), this._update_length();
1073
691
  };
1074
- return h instanceof o ? h.ranges.forEach(v) : (_ ??= h, v(new g(h, _))), this;
692
+ return g instanceof a ? g.ranges.forEach(y) : (v ??= g, y(new _(g, v))), this;
1075
693
  }
1076
- intersect(h, _) {
1077
- var v = [], y = (o) => {
1078
- for (var h = 0; h < this.ranges.length && !o.overlaps(this.ranges[h]);) h++;
1079
- for (; h < this.ranges.length && o.overlaps(this.ranges[h]);) {
1080
- var _ = Math.max(this.ranges[h].low, o.low), y = Math.min(this.ranges[h].high, o.high);
1081
- v.push(new g(_, y)), h++;
694
+ intersect(g, v) {
695
+ var y = [], b = (a) => {
696
+ for (var g = 0; g < this.ranges.length && !a.overlaps(this.ranges[g]);) g++;
697
+ for (; g < this.ranges.length && a.overlaps(this.ranges[g]);) {
698
+ var v = Math.max(this.ranges[g].low, a.low), b = Math.min(this.ranges[g].high, a.high);
699
+ y.push(new _(v, b)), g++;
1082
700
  }
1083
701
  };
1084
- return h instanceof o ? h.ranges.forEach(y) : (_ ??= h, y(new g(h, _))), this.ranges = v, this._update_length(), this;
702
+ return g instanceof a ? g.ranges.forEach(b) : (v ??= g, b(new _(g, v))), this.ranges = y, this._update_length(), this;
1085
703
  }
1086
- index(o) {
1087
- for (var h = 0; h < this.ranges.length && this.ranges[h].length <= o;) o -= this.ranges[h].length, h++;
1088
- return this.ranges[h].low + o;
704
+ index(a) {
705
+ for (var g = 0; g < this.ranges.length && this.ranges[g].length <= a;) a -= this.ranges[g].length, g++;
706
+ return this.ranges[g].low + a;
1089
707
  }
1090
708
  toString() {
1091
709
  return "[ " + this.ranges.join(", ") + " ]";
1092
710
  }
1093
711
  clone() {
1094
- return new o(this);
712
+ return new a(this);
1095
713
  }
1096
714
  numbers() {
1097
- return this.ranges.reduce((o, h) => {
1098
- for (var g = h.low; g <= h.high;) o.push(g), g++;
1099
- return o;
715
+ return this.ranges.reduce((a, g) => {
716
+ for (var _ = g.low; _ <= g.high;) a.push(_), _++;
717
+ return a;
1100
718
  }, []);
1101
719
  }
1102
720
  subranges() {
1103
- return this.ranges.map((o) => ({
1104
- low: o.low,
1105
- high: o.high,
1106
- length: 1 + o.high - o.low
721
+ return this.ranges.map((a) => ({
722
+ low: a.low,
723
+ high: a.high,
724
+ length: 1 + a.high - a.low
1107
725
  }));
1108
726
  }
1109
727
  };
1110
- })), import_randexp = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((o, h) => {
1111
- var g = require_lib$1(), _ = require_lib(), v = g.types;
1112
- h.exports = class o {
1113
- constructor(o, h) {
1114
- if (this._setDefaults(o), o instanceof RegExp) this.ignoreCase = o.ignoreCase, this.multiline = o.multiline, o = o.source;
1115
- else if (typeof o == "string") this.ignoreCase = h && h.indexOf("i") !== -1, this.multiline = h && h.indexOf("m") !== -1;
728
+ })), import_randexp = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((a, g) => {
729
+ var _ = require_lib$1(), v = require_lib(), y = _.types;
730
+ g.exports = class a {
731
+ constructor(a, g) {
732
+ if (this._setDefaults(a), a instanceof RegExp) this.ignoreCase = a.ignoreCase, this.multiline = a.multiline, a = a.source;
733
+ else if (typeof a == "string") this.ignoreCase = g && g.indexOf("i") !== -1, this.multiline = g && g.indexOf("m") !== -1;
1116
734
  else throw Error("Expected a regexp or string");
1117
- this.tokens = g(o);
735
+ this.tokens = _(a);
1118
736
  }
1119
- _setDefaults(h) {
1120
- this.max = h.max == null ? o.prototype.max == null ? 100 : o.prototype.max : h.max, this.defaultRange = h.defaultRange ? h.defaultRange : this.defaultRange.clone(), h.randInt && (this.randInt = h.randInt);
737
+ _setDefaults(g) {
738
+ this.max = g.max == null ? a.prototype.max == null ? 100 : a.prototype.max : g.max, this.defaultRange = g.defaultRange ? g.defaultRange : this.defaultRange.clone(), g.randInt && (this.randInt = g.randInt);
1121
739
  }
1122
740
  gen() {
1123
741
  return this._gen(this.tokens, []);
1124
742
  }
1125
- _gen(o, h) {
1126
- var g, _, y, b, x;
1127
- switch (o.type) {
1128
- case v.ROOT:
1129
- case v.GROUP:
1130
- if (o.followedBy || o.notFollowedBy) return "";
1131
- for (o.remember && o.groupNumber === void 0 && (o.groupNumber = h.push(null) - 1), g = o.options ? this._randSelect(o.options) : o.stack, _ = "", b = 0, x = g.length; b < x; b++) _ += this._gen(g[b], h);
1132
- return o.remember && (h[o.groupNumber] = _), _;
1133
- case v.POSITION: return "";
1134
- case v.SET:
1135
- var S = this._expand(o);
1136
- return S.length ? String.fromCharCode(this._randSelect(S)) : "";
1137
- case v.REPETITION:
1138
- for (y = this.randInt(o.min, o.max === Infinity ? o.min + this.max : o.max), _ = "", b = 0; b < y; b++) _ += this._gen(o.value, h);
1139
- return _;
1140
- case v.REFERENCE: return h[o.value - 1] || "";
1141
- case v.CHAR:
1142
- var C = this.ignoreCase && this._randBool() ? this._toOtherCase(o.value) : o.value;
1143
- return String.fromCharCode(C);
743
+ _gen(a, g) {
744
+ var _, v, b, x, S;
745
+ switch (a.type) {
746
+ case y.ROOT:
747
+ case y.GROUP:
748
+ if (a.followedBy || a.notFollowedBy) return "";
749
+ for (a.remember && a.groupNumber === void 0 && (a.groupNumber = g.push(null) - 1), _ = a.options ? this._randSelect(a.options) : a.stack, v = "", x = 0, S = _.length; x < S; x++) v += this._gen(_[x], g);
750
+ return a.remember && (g[a.groupNumber] = v), v;
751
+ case y.POSITION: return "";
752
+ case y.SET:
753
+ var C = this._expand(a);
754
+ return C.length ? String.fromCharCode(this._randSelect(C)) : "";
755
+ case y.REPETITION:
756
+ for (b = this.randInt(a.min, a.max === Infinity ? a.min + this.max : a.max), v = "", x = 0; x < b; x++) v += this._gen(a.value, g);
757
+ return v;
758
+ case y.REFERENCE: return g[a.value - 1] || "";
759
+ case y.CHAR:
760
+ var w = this.ignoreCase && this._randBool() ? this._toOtherCase(a.value) : a.value;
761
+ return String.fromCharCode(w);
1144
762
  }
1145
763
  }
1146
- _toOtherCase(o) {
1147
- return o + (97 <= o && o <= 122 ? -32 : 65 <= o && o <= 90 ? 32 : 0);
764
+ _toOtherCase(a) {
765
+ return a + (97 <= a && a <= 122 ? -32 : 65 <= a && a <= 90 ? 32 : 0);
1148
766
  }
1149
767
  _randBool() {
1150
768
  return !this.randInt(0, 1);
1151
769
  }
1152
- _randSelect(o) {
1153
- return o instanceof _ ? o.index(this.randInt(0, o.length - 1)) : o[this.randInt(0, o.length - 1)];
770
+ _randSelect(a) {
771
+ return a instanceof v ? a.index(this.randInt(0, a.length - 1)) : a[this.randInt(0, a.length - 1)];
1154
772
  }
1155
- _expand(o) {
1156
- if (o.type === g.types.CHAR) return new _(o.value);
1157
- if (o.type === g.types.RANGE) return new _(o.from, o.to);
773
+ _expand(a) {
774
+ if (a.type === _.types.CHAR) return new v(a.value);
775
+ if (a.type === _.types.RANGE) return new v(a.from, a.to);
1158
776
  {
1159
- let h = new _();
1160
- for (let g = 0; g < o.set.length; g++) {
1161
- let _ = this._expand(o.set[g]);
1162
- if (h.add(_), this.ignoreCase) for (let o = 0; o < _.length; o++) {
1163
- let g = _.index(o), v = this._toOtherCase(g);
1164
- g !== v && h.add(v);
777
+ let g = new v();
778
+ for (let _ = 0; _ < a.set.length; _++) {
779
+ let v = this._expand(a.set[_]);
780
+ if (g.add(v), this.ignoreCase) for (let a = 0; a < v.length; a++) {
781
+ let _ = v.index(a), y = this._toOtherCase(_);
782
+ _ !== y && g.add(y);
1165
783
  }
1166
784
  }
1167
- return o.not ? this.defaultRange.clone().subtract(h) : this.defaultRange.clone().intersect(h);
785
+ return a.not ? this.defaultRange.clone().subtract(g) : this.defaultRange.clone().intersect(g);
1168
786
  }
1169
787
  }
1170
- randInt(o, h) {
1171
- return o + Math.floor(Math.random() * (1 + h - o));
788
+ randInt(a, g) {
789
+ return a + Math.floor(Math.random() * (1 + g - a));
1172
790
  }
1173
791
  get defaultRange() {
1174
- return this._range = this._range || new _(32, 126);
792
+ return this._range = this._range || new v(32, 126);
1175
793
  }
1176
- set defaultRange(o) {
1177
- this._range = o;
794
+ set defaultRange(a) {
795
+ this._range = a;
1178
796
  }
1179
- static randexp(h, g) {
1180
- var _;
1181
- return typeof h == "string" && (h = new RegExp(h, g)), h._randexp === void 0 ? (_ = new o(h, g), h._randexp = _) : (_ = h._randexp, _._setDefaults(h)), _.gen();
797
+ static randexp(g, _) {
798
+ var v;
799
+ return typeof g == "string" && (g = new RegExp(g, _)), g._randexp === void 0 ? (v = new a(g, _), g._randexp = v) : (v = g._randexp, v._setDefaults(g)), v.gen();
1182
800
  }
1183
801
  static sugar() {
1184
802
  RegExp.prototype.gen = function() {
1185
- return o.randexp(this);
803
+ return a.randexp(this);
1186
804
  };
1187
805
  }
1188
806
  };
1189
- })))()), array = [], characterCodeCache = [];
1190
- function leven(o, h, g) {
1191
- if (o === h) return 0;
1192
- let _ = g?.maxDistance, v = o;
1193
- o.length > h.length && (o = h, h = v);
1194
- let y = o.length, b = h.length;
1195
- for (; y > 0 && o.charCodeAt(~-y) === h.charCodeAt(~-b);) y--, b--;
1196
- let x = 0;
1197
- for (; x < y && o.charCodeAt(x) === h.charCodeAt(x);) x++;
1198
- if (y -= x, b -= x, _ !== void 0 && b - y > _) return _;
1199
- if (y === 0) return _ !== void 0 && b > _ ? _ : b;
1200
- let S, C, w, T, E = 0, D = 0;
1201
- for (; E < y;) characterCodeCache[E] = o.charCodeAt(x + E), array[E] = ++E;
1202
- for (; D < b;) {
1203
- for (S = h.charCodeAt(x + D), w = D++, C = D, E = 0; E < y; E++) T = S === characterCodeCache[E] ? w : w + 1, w = array[E], C = array[E] = w > C ? T > C ? C + 1 : T : T > w ? w + 1 : T;
1204
- if (_ !== void 0) {
1205
- let o = C;
1206
- for (E = 0; E < y; E++) array[E] < o && (o = array[E]);
1207
- if (o > _) return _;
1208
- }
807
+ })))());
808
+ function pattern(a) {
809
+ let g = new import_randexp.default(a);
810
+ g.randInt = (a) => a;
811
+ function _() {
812
+ return t$1(t$3(0, 10), t$2(() => g.gen()), n$1());
1209
813
  }
1210
- return array.length = y, characterCodeCache.length = y, _ !== void 0 && C > _ ? _ : C;
1211
- }
1212
- function pattern(o) {
1213
- let h = new import_randexp.default(o);
1214
- h.randInt = () => 0, h.defaultRange.subtract(-Infinity, Infinity);
1215
- function g() {
1216
- return t(t$4(0, 10), t$3(() => h.gen()), n$2());
1217
- }
1218
- let _ = {
814
+ let v = {
1219
815
  type: "pattern",
1220
816
  children: [],
1221
- meta: { regex: o },
1222
- suggestions: g,
1223
- parse(h, v) {
1224
- let y = new RegExp(o, "yu");
817
+ meta: { regex: a },
818
+ suggestions: _,
819
+ parse(g, _) {
820
+ let y = new RegExp(a, "yu");
1225
821
  y.lastIndex = 0;
1226
- let b = y.exec(h);
1227
- if (b) return success({
822
+ let b = y.exec(g);
823
+ return b ? success({
1228
824
  text: b[0],
1229
- grammar: _,
1230
- children: []
1231
- });
1232
- if (v.faultTolerant) {
1233
- let o = t(g(), n((o) => leven(o, h) <= 2), n$1());
1234
- if (o !== void 0) return success({
1235
- text: h.substring(0, o.length),
1236
- grammar: _,
1237
- children: [],
1238
- recoverableError: !0
1239
- });
1240
- }
1241
- return error({
1242
- grammar: _,
1243
- got: h,
1244
- expected: _.suggestions()
1245
- });
1246
- }
1247
- };
1248
- return _;
1249
- }
1250
- const rational = pattern(/\d+(:?.\d+)?/);
1251
- function optional(o) {
1252
- let h = {
1253
- type: "optional",
1254
- suggestions: () => o.suggestions(),
1255
- children: [o],
1256
- parse(g, _) {
1257
- let v = o.parse(g, _);
1258
- return isParserError(v) ? success({
1259
- text: "",
1260
- grammar: h,
825
+ grammar: v,
1261
826
  children: []
1262
- }) : v;
827
+ }) : _.handleTerminalError(g, _, v);
1263
828
  }
1264
829
  };
1265
- return h;
1266
- }
1267
- const ws = optional(pattern(/\s+/));
1268
- function seq(...o) {
1269
- return sequence(ws, ...o.flatMap((o) => [o, ws]));
830
+ return v;
1270
831
  }
1271
- function term(o) {
1272
- return pattern(new RegExp(regexEscape(o)));
1273
- }
1274
- function regexEscape(o) {
1275
- return o.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1276
- }
1277
- function alternative(...o) {
1278
- let h = {
1279
- type: "alternative",
1280
- children: o,
1281
- parse(g, _) {
1282
- let v = [];
1283
- if (_.faultTolerant) {
1284
- for (let h of o) {
1285
- let o = h.parse(g, {
1286
- ..._,
1287
- faultTolerant: !1
1288
- });
1289
- if (isParserSuccess(o)) return o;
1290
- }
1291
- let h = o.map((o) => o.parse(g, {
1292
- ..._,
1293
- faultTolerant: !0
1294
- })), v = t(h, n(isParserSuccess), t$1((o) => o.text.length), t$2());
1295
- if (v) return {
1296
- ...v,
1297
- recoverableError: !0
1298
- };
1299
- } else for (let h of o) {
1300
- let o = h.parse(g, _);
1301
- if (isParserError(o)) {
1302
- v.push(o);
1303
- continue;
832
+ function repeat(a, g = 1, _ = 1e3) {
833
+ let v = {
834
+ type: "repeat",
835
+ children: [a],
836
+ suggestions: () => a.suggestions(),
837
+ parse(y, b) {
838
+ let x = {
839
+ ...b,
840
+ path: [...b.path, v]
841
+ }, S = 0, C = [], w = 0;
842
+ for (; w < _ && S < y.length; w++) {
843
+ let _ = y.substring(S), b = a.parse(_, x);
844
+ if (isParserError(b)) {
845
+ let a = {
846
+ ...b,
847
+ offset: S
848
+ };
849
+ return w >= g ? success({
850
+ grammar: v,
851
+ children: C,
852
+ text: y.substring(0, S)
853
+ }) : a;
1304
854
  }
1305
- return o;
855
+ S += b.text.length, C.push(b);
1306
856
  }
1307
- return error({
1308
- got: g,
1309
- expected: v.flatMap((o) => o.expected),
1310
- grammar: h
857
+ return success({
858
+ grammar: v,
859
+ children: C,
860
+ text: y.substring(0, S)
1311
861
  });
1312
- },
1313
- suggestions() {
1314
- return o.flatMap((o) => o.suggestions());
1315
862
  }
1316
863
  };
1317
- return h;
864
+ return v;
1318
865
  }
1319
- function named(o, h) {
866
+ function sequence(...a) {
1320
867
  let g = {
1321
- meta: { name: o },
1322
- type: "named",
868
+ children: a,
869
+ type: "sequence",
1323
870
  suggestions() {
1324
- return h.suggestions();
871
+ let g = [];
872
+ for (let _ of a) {
873
+ let a = _.suggestions();
874
+ if (g.push(...a.filter((a) => a !== "")), !a.includes("")) break;
875
+ }
876
+ return g;
1325
877
  },
1326
- children: [h],
1327
- parse(o, _) {
1328
- let v = h.parse(o, _);
1329
- return isParserError(v) ? v : success({
1330
- text: v.text,
878
+ parse(_, v) {
879
+ let y = {
880
+ ...v,
881
+ path: [...v.path, g]
882
+ }, b = 0, x = [];
883
+ for (let g = 0; g < a.length; g++) {
884
+ let v = a[g], S = _.substring(b), C = v.parse(S, y);
885
+ if (isParserError(C)) return error({
886
+ ...C,
887
+ offset: C.offset + b
888
+ });
889
+ b += C.text.length, x.push(C);
890
+ }
891
+ return success({
1331
892
  grammar: g,
1332
- children: [v]
893
+ text: _.substring(0, b),
894
+ children: x
1333
895
  });
1334
896
  }
1335
897
  };
1336
898
  return g;
1337
899
  }
1338
- function repeat(o, h = 1, g = 1e3) {
1339
- let _ = {
1340
- type: "repeat",
1341
- children: [o],
1342
- suggestions: () => o.suggestions(),
1343
- parse(v, y) {
1344
- let b = 0, x = !1, S = [];
1345
- for (let _ = 0; _ < g && b < v.length; _++) {
1346
- let g = v.substring(b), C = o.parse(g, {
1347
- ...y,
1348
- faultTolerant: y.faultTolerant && !x
1349
- });
1350
- if (isParserError(C)) {
1351
- let o = {
1352
- ...C,
1353
- offset: b
1354
- };
1355
- if (_ >= h) break;
1356
- if (y.faultTolerant) {
1357
- if (!x) {
1358
- x = !0;
1359
- continue;
1360
- }
1361
- return o;
1362
- } else return o;
1363
- }
1364
- b += C.text.length, S.push({
1365
- ...C,
1366
- recoverableError: x
900
+ function getErrors(a, g = 1) {
901
+ let _ = a.children?.flatMap((a) => getErrors(a, g + 1)) ?? [];
902
+ return a.recoverableError ? [{
903
+ start: a.offset,
904
+ end: Math.max(a.end, a.offset + 1),
905
+ message: `${a.grammar.type} ${a.text}`,
906
+ depth: g
907
+ }, ..._] : _;
908
+ }
909
+ function withOffset(a, g = 0) {
910
+ let _ = g;
911
+ return {
912
+ text: a.text,
913
+ offset: g,
914
+ end: g + a.text.length,
915
+ children: a.children?.map((a, g, v) => {
916
+ let { text: y } = v[g - 1] ?? { text: "" };
917
+ return _ += y.length, withOffset(a, _);
918
+ }),
919
+ grammar: a.grammar,
920
+ recoverableError: a.recoverableError ?? !1
921
+ };
922
+ }
923
+ const eof = pattern(/$/);
924
+ function _flatCST(a) {
925
+ return a.children && !e(a.children) ? a.children.flatMap((a) => flatCST(a)) : [a];
926
+ }
927
+ function flatCST(a) {
928
+ return _flatCST(a).filter((a) => a.text !== "");
929
+ }
930
+ function topError(a) {
931
+ return isParserSuccess(a) ? [] : [{
932
+ message: a.expected.join(" or ") + " expected",
933
+ expected: a.expected,
934
+ start: a.offset,
935
+ end: a.offset + Math.max(...a.expected.map((a) => a.length)),
936
+ depth: 1
937
+ }];
938
+ }
939
+ var DSLParser = class {
940
+ grammar;
941
+ constructor(a) {
942
+ this.grammar = sequence(a, eof, trailingInput);
943
+ }
944
+ _parseStrict(a) {
945
+ let g = [], _ = this.grammar.parse(a, {
946
+ path: [],
947
+ handleTerminalError(a, _, v) {
948
+ let y = [..._.path, v];
949
+ return g.length < y.length && (g = y), error({
950
+ grammar: v,
951
+ offset: 0,
952
+ expected: v.suggestions(),
953
+ got: a,
954
+ path: y
1367
955
  });
1368
956
  }
1369
- return success({
1370
- grammar: _,
1371
- children: S,
1372
- text: v.substring(0, b)
1373
- });
957
+ });
958
+ return {
959
+ deepestErrorPath: g,
960
+ parserResult: _
961
+ };
962
+ }
963
+ parseStrict(a) {
964
+ let { parserResult: g } = this._parseStrict(a);
965
+ if (!isParserError(g)) return g;
966
+ }
967
+ parse(a) {
968
+ let { parserResult: g, deepestErrorPath: _ } = this._parseStrict(a), v = g, y = [pathToString(_)], b = 20;
969
+ for (; b-- > 0 && isParserError(v);) {
970
+ let g = [];
971
+ v = this.grammar.parse(a, {
972
+ path: [],
973
+ handleTerminalError(a, _, v) {
974
+ let b = pathToString([..._.path, v]);
975
+ if (y.includes(b)) return success({
976
+ text: a[0] ?? "",
977
+ grammar: v,
978
+ children: [],
979
+ recoverableError: !0
980
+ });
981
+ {
982
+ let y = [..._.path, v];
983
+ return g.length < y.length && (g = y), error({
984
+ grammar: v,
985
+ offset: 0,
986
+ expected: v.suggestions(),
987
+ got: a,
988
+ path: _.path
989
+ });
990
+ }
991
+ }
992
+ }), y.push(pathToString(g));
1374
993
  }
1375
- };
1376
- return _;
994
+ if (isParserError(v)) throw asException(g);
995
+ let x = withOffset(v), S = flatCST(x);
996
+ return {
997
+ cst: x,
998
+ terminals: S,
999
+ result: v,
1000
+ strictResult: isParserSuccess(g) ? g : void 0,
1001
+ errors: [...topError(g), ...getErrors(x)]
1002
+ };
1003
+ }
1004
+ };
1005
+ function pathToString(a) {
1006
+ return a.map((a) => `${a.type}${a.meta?.regex?.toString() ?? ""}`).join("/");
1007
+ }
1008
+ var trailingInput = {
1009
+ children: [],
1010
+ parse: (a) => success({
1011
+ children: [],
1012
+ grammar: trailingInput,
1013
+ text: a
1014
+ }),
1015
+ type: "trailing-input",
1016
+ suggestions: () => []
1017
+ };
1018
+ function visit(a, g, _ = (a) => a.text) {
1019
+ return isParserError(a) ? [] : (((a) => {
1020
+ let _ = nodeName(a);
1021
+ return _ !== void 0 && g.includes(_);
1022
+ })(a) ? [_(a)] : []).concat(...a.children?.flatMap((a) => visit(a, g, _)) ?? []);
1023
+ }
1024
+ const rational = pattern(/\d+(:?.\d+)?/), ws = optional(pattern(/\s+/));
1025
+ function seq(...a) {
1026
+ return sequence(ws, ...a.flatMap((a) => [a, ws]));
1027
+ }
1028
+ function term(a) {
1029
+ return pattern(new RegExp(regexEscape(a)));
1030
+ }
1031
+ function regexEscape(a) {
1032
+ return a.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1033
+ }
1034
+ function getSuggestions(a, g, _ = () => void 0) {
1035
+ let v = cstPathAt(a, g), y = t$1(v, t((a) => {
1036
+ if (nodeName(a) !== void 0) {
1037
+ let g = _(a);
1038
+ if (g) return g.map((g) => ({
1039
+ node: a,
1040
+ suggestion: g
1041
+ }));
1042
+ }
1043
+ return a.grammar.suggestions().map((g) => ({
1044
+ node: a,
1045
+ suggestion: g
1046
+ }));
1047
+ }));
1048
+ return t$1(y, t((a) => {
1049
+ let _ = a.node.text.substring(0, g - a.node.offset);
1050
+ return a.suggestion.startsWith(_) && a.suggestion.trim().length > 0 ? [{
1051
+ suggestion: a.suggestion,
1052
+ prefix: _
1053
+ }] : [];
1054
+ }), n((a) => a.suggestion));
1055
+ }
1056
+ const CursorPosition = forwardRef(function(a, g) {
1057
+ let _ = useRef(null), v = useRef(null);
1058
+ return useImperativeHandle(g, () => ({ getCursorPosition() {
1059
+ if (!_.current || !v.current) return {
1060
+ top: 0,
1061
+ left: 0
1062
+ };
1063
+ let { offsetTop: a, offsetLeft: g } = _.current, { scrollTop: y, scrollLeft: b } = v.current, x = window.getComputedStyle(v.current), S = parseInt(x.lineHeight);
1064
+ return {
1065
+ top: a - y + S,
1066
+ left: g - b
1067
+ };
1068
+ } }), []), /* @__PURE__ */ jsxs(ReadOnlyTextarea, {
1069
+ ref: v,
1070
+ wrap: a.wrap,
1071
+ style: { visibility: "hidden" },
1072
+ children: [a.text, /* @__PURE__ */ jsx("span", {
1073
+ ref: _,
1074
+ children: "\xA0"
1075
+ })]
1076
+ });
1077
+ });
1078
+ function shortcutName(a) {
1079
+ let g = "";
1080
+ a.ctrlKey && (g = "Ctrl");
1081
+ let { key: _ } = a;
1082
+ return _ === " " && (_ = "Space"), `${g}${_}`;
1083
+ }
1084
+ function useSyncScroll(...a) {
1085
+ return useCallback((g) => {
1086
+ let _ = g.currentTarget;
1087
+ a.forEach((a) => {
1088
+ a.current && _ && (a.current.scrollTop = _.scrollTop, a.current.scrollLeft = _.scrollLeft);
1089
+ });
1090
+ }, [a]);
1091
+ }
1092
+ var themes = {
1093
+ light: [
1094
+ "#FF0000",
1095
+ "#0000FF",
1096
+ "#FF00FF",
1097
+ "#800000",
1098
+ "#008000",
1099
+ "#000080",
1100
+ "#808000",
1101
+ "#800080",
1102
+ "#008080",
1103
+ "#808080",
1104
+ "#993366",
1105
+ "#336699"
1106
+ ],
1107
+ dark: [
1108
+ "#FF5555",
1109
+ "#5555FF",
1110
+ "#FF55FF",
1111
+ "#FF5555",
1112
+ "#55FF55",
1113
+ "#5555FF",
1114
+ "#FFFF55",
1115
+ "#FF55FF",
1116
+ "#55FFFF",
1117
+ "#AAAAAA",
1118
+ "#CC99FF",
1119
+ "#99CCFF"
1120
+ ]
1121
+ };
1122
+ function cyrb53(a, g = 0) {
1123
+ let _ = 3735928559 ^ g, v = 1103547991 ^ g;
1124
+ for (let g = 0, y; g < a.length; g++) y = a.charCodeAt(g), _ = Math.imul(_ ^ y, 2654435761), v = Math.imul(v ^ y, 1597334677);
1125
+ return _ = Math.imul(_ ^ _ >>> 16, 2246822507), _ ^= Math.imul(v ^ v >>> 13, 3266489909), v = Math.imul(v ^ v >>> 16, 2246822507), v ^= Math.imul(_ ^ _ >>> 13, 3266489909), 4294967296 * (2097151 & v) + (_ >>> 0);
1126
+ }
1127
+ function getColorAssigment(a, g) {
1128
+ let _ = cyrb53(String(a)) % g.length;
1129
+ return g[_];
1130
+ }
1131
+ function defaultStyleFor(a, g) {
1132
+ let { name: _, regex: v } = a.grammar.meta ?? {}, y = _ ?? v;
1133
+ if (y) return { color: getColorAssigment(y, g) };
1134
+ }
1135
+ function defaultSyntaxColors(a) {
1136
+ return (g) => defaultStyleFor(g, themes[a]);
1137
+ }
1138
+ function SuggestionsMenu({ suggestions: a, onSelect: g, style: _, selectedIndex: v, onHover: y }) {
1139
+ return /* @__PURE__ */ jsx("div", {
1140
+ style: {
1141
+ position: "absolute",
1142
+ ..._,
1143
+ background: "#252526",
1144
+ border: "1px solid #454545",
1145
+ boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
1146
+ color: "#cccccc",
1147
+ fontFamily: "monospace",
1148
+ fontSize: "0.9em",
1149
+ minWidth: 200,
1150
+ zIndex: 100
1151
+ },
1152
+ children: a.map((a, _) => /* @__PURE__ */ jsx("div", {
1153
+ onClick: () => g(a),
1154
+ onMouseOver: () => y(_),
1155
+ style: {
1156
+ cursor: "pointer",
1157
+ padding: "4px 8px",
1158
+ backgroundColor: v === _ ? "#094771" : "transparent"
1159
+ },
1160
+ children: a.suggestion
1161
+ }, _))
1162
+ });
1163
+ }
1164
+ function DslEditor({ code: a, onChange: g, onParsed: y, grammar: C, wrap: E = !1, suggestions: D, className: O = DslEditor.name, syntaxColors: k = defaultSyntaxColors("light"),...A }) {
1165
+ let [j, M] = useState([]), [N, P] = useState(), [I, L] = useState({
1166
+ top: 0,
1167
+ left: 0,
1168
+ visible: !1
1169
+ }), [R, z] = useState(0), [B, V] = useState(""), H = useRef(null), U = useRef(null), W = useRef(null), G = useRef(null), K = useCallback((a) => {
1170
+ let g = H.current?.selectionStart ?? 0, _ = getSuggestions(a, g, D);
1171
+ return M(_), _;
1172
+ }, [D]), q = useCallback(() => {
1173
+ N && V(H.current?.value?.substring(0, H.current?.selectionStart ?? 0) ?? "");
1174
+ }, [N]);
1175
+ useEffect(() => {
1176
+ let g = new DSLParser(C).parse(a);
1177
+ P(g), y?.(g), K(g.cst), V(a.substring(0, H.current?.selectionStart ?? 0));
1178
+ }, [
1179
+ a,
1180
+ y,
1181
+ C,
1182
+ K
1183
+ ]);
1184
+ let J = useCallback(() => U.current?.getCursorPosition?.() ?? {
1185
+ top: 0,
1186
+ left: 0
1187
+ }, []), Y = useCallback((_) => {
1188
+ if (!H.current) return;
1189
+ let { selectionStart: v, selectionEnd: y } = H.current, { prefix: b } = _, x = a.substring(0, v - b.length) + _.suggestion + a.substring(y);
1190
+ g(x), setTimeout(() => {
1191
+ H.current && (H.current.focus(), H.current.selectionStart = H.current.selectionEnd = v - b.length + _.suggestion.length);
1192
+ }, 0), L((a) => ({
1193
+ ...a,
1194
+ visible: !1
1195
+ }));
1196
+ }, [a, g]), X = useMemo(() => ({
1197
+ ArrowDown() {
1198
+ z((a) => (a + 1) % j.length);
1199
+ },
1200
+ ArrowUp() {
1201
+ z((a) => (a - 1 + j.length) % j.length);
1202
+ },
1203
+ Enter() {
1204
+ j[R] && Y(j[R]);
1205
+ },
1206
+ Escape() {
1207
+ L((a) => ({
1208
+ ...a,
1209
+ visible: !1
1210
+ }));
1211
+ }
1212
+ }), [
1213
+ Y,
1214
+ R,
1215
+ j
1216
+ ]), Z = useMemo(() => ({ CtrlSpace() {
1217
+ if (!N?.cst) return;
1218
+ let a = K(N?.cst);
1219
+ if (e(a)) return;
1220
+ let { top: g, left: _ } = J();
1221
+ z(0), L({
1222
+ visible: !0,
1223
+ top: g,
1224
+ left: _
1225
+ });
1226
+ } }), [
1227
+ J,
1228
+ N?.cst,
1229
+ K
1230
+ ]), Q = useCallback((a) => {
1231
+ let g = (I.visible ? X : Z)[shortcutName(a)];
1232
+ g && (a.preventDefault(), g());
1233
+ }, [
1234
+ I.visible,
1235
+ X,
1236
+ Z
1237
+ ]), $ = useCallback((a) => {
1238
+ g(a.target.value), L((a) => ({
1239
+ ...a,
1240
+ visible: !1
1241
+ })), V(a.target.value.substring(0, a.target.selectionStart));
1242
+ }, [g]);
1243
+ return /* @__PURE__ */ jsx("div", {
1244
+ style: {
1245
+ display: "grid",
1246
+ gridTemplateRows: "1fr auto",
1247
+ flex: 1,
1248
+ width: "100%",
1249
+ height: "100%"
1250
+ },
1251
+ className: O,
1252
+ children: /* @__PURE__ */ jsxs("div", {
1253
+ style: {
1254
+ position: "relative",
1255
+ border: "1px solid black",
1256
+ overflow: "hidden"
1257
+ },
1258
+ children: [
1259
+ /* @__PURE__ */ jsx("textarea", {
1260
+ ref: H,
1261
+ spellCheck: !1,
1262
+ wrap: E ? "soft" : "off",
1263
+ style: {
1264
+ ...textStyle,
1265
+ color: "transparent",
1266
+ background: "transparent",
1267
+ caretColor: "black",
1268
+ border: "none",
1269
+ resize: "none"
1270
+ },
1271
+ value: a,
1272
+ onSelect: q,
1273
+ onChange: $,
1274
+ onScroll: useSyncScroll(W, G),
1275
+ onKeyDown: Q,
1276
+ ...A
1277
+ }),
1278
+ N && /* @__PURE__ */ jsx(SyntaxHighlighter, {
1279
+ cstRoot: N.cst,
1280
+ ref: W,
1281
+ wrap: E,
1282
+ syntaxColors: k
1283
+ }),
1284
+ !e(N?.errors ?? []) && /* @__PURE__ */ jsx(ErrorHighlighter, {
1285
+ ref: G,
1286
+ errors: N?.errors ?? [],
1287
+ wrap: E,
1288
+ children: a
1289
+ }),
1290
+ /* @__PURE__ */ jsx(CursorPosition, {
1291
+ ref: U,
1292
+ text: B,
1293
+ wrap: E
1294
+ }),
1295
+ I.visible && j.length > 0 && /* @__PURE__ */ jsx(SuggestionsMenu, {
1296
+ suggestions: j,
1297
+ onSelect: Y,
1298
+ style: {
1299
+ top: I.top,
1300
+ left: I.left
1301
+ },
1302
+ selectedIndex: R,
1303
+ onHover: z
1304
+ })
1305
+ ]
1306
+ })
1307
+ });
1377
1308
  }
1378
- export { DSLParser, DslEditor, ParsingError, alternative, asException, eof, error, isParserError, isParserSuccess, named, optional, pattern, rational, repeat, seq, sequence, success, term, visit, ws };
1309
+ export { DSLParser, DslEditor, ParsingError, alternative, asException, defaultSyntaxColors, eof, error, isParserError, isParserSuccess, named, nodeName, optional, pattern, rational, repeat, seq, sequence, success, term, visit, ws };