react-dsl-editor 0.2.3 → 0.3.0

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