react-dsl-editor 0.2.3 → 0.3.2

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
- import { jsx, jsxs } from "react/jsx-runtime";
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
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,
11
+ enumerable: !0
12
+ }) : g, o));
3
13
  const textStyle = {
4
14
  position: "absolute",
5
15
  inset: 0,
@@ -8,360 +18,331 @@ const textStyle = {
8
18
  fontSize: "1em",
9
19
  lineHeight: "1.3em",
10
20
  fontFamily: "monospace"
11
- }, ReadOnlyTextarea = forwardRef(function({ wrap: o, children: _, style: v = {} }, y) {
21
+ }, ReadOnlyTextarea = forwardRef(function({ wrap: o, children: h, style: g = {} }, _) {
12
22
  return /* @__PURE__ */ jsx("pre", {
13
- ref: y,
23
+ ref: _,
14
24
  style: {
15
25
  ...textStyle,
16
26
  pointerEvents: "none",
17
27
  margin: 0,
18
28
  overflow: "auto",
19
29
  whiteSpace: o ? "pre-wrap" : "pre",
20
- ...v
30
+ ...g
21
31
  },
22
- children: _
32
+ children: h
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_${_}`))
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
34
40
  });
35
41
  }
36
- function e$3(o, _, v) {
37
- let y = (v) => o(v, ..._);
38
- return v === void 0 ? y : Object.assign(y, {
39
- lazy: v,
40
- lazyArgs: _
41
- });
42
- }
43
- function t$2(o, _, v) {
44
- let y = o.length - _.length;
45
- if (y === 0) return o(..._);
46
- if (y === 1) return e$3(o, _, v);
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);
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, h) => o ?? h, 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;
55
- for (; b < _.length;) {
56
- if (y[b] === void 0 || !i$1(v)) {
57
- let o = _[b];
58
- v = o(v), b += 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;
59
64
  continue;
60
65
  }
61
66
  let o = [];
62
- for (let v = b; v < _.length; v++) {
63
- let _ = y[v];
64
- if (_ === void 0 || (o.push(_), _.isSingle)) break;
65
- }
66
- let x = [];
67
- for (let _ of v) if (n$3(_, x, o)) break;
68
- let { isSingle: S } = o.at(-1);
69
- v = S ? x[0] : x, b += o.length;
67
+ for (let g = v; g < h.length; g++) {
68
+ let h = _[g];
69
+ if (h === void 0 || (o.push(h), h.isSingle)) break;
70
+ }
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;
70
75
  }
71
- return v;
72
- }
73
- function n$3(o, _, v) {
74
- if (v.length === 0) return _.push(o), !1;
75
- let y = o, b = t$5, x = !1;
76
- for (let [o, S] of v.entries()) {
77
- let { index: C, items: w } = S;
78
- if (w.push(y), b = S(y, C, w), S.index += 1, b.hasNext) {
79
- if (b.hasMany ?? !1) {
80
- for (let y of b.next) if (n$3(y, _, v.slice(o + 1))) return !0;
81
- return x;
76
+ return g;
77
+ }
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;
82
87
  }
83
- y = b.next;
88
+ _ = v.next;
84
89
  }
85
- if (!b.hasNext) break;
86
- b.done && (x = !0);
90
+ if (!v.hasNext) break;
91
+ v.done && (y = !0);
87
92
  }
88
- return b.hasNext && _.push(y), x;
93
+ return v.hasNext && h.push(_), y;
89
94
  }
90
- function r$2(o) {
91
- let { lazy: _, lazyArgs: v } = o, y = _(...v);
92
- return Object.assign(y, {
93
- isSingle: _.single ?? !1,
95
+ function r$8(o) {
96
+ let { lazy: h, lazyArgs: g } = o, _ = h(...g);
97
+ return Object.assign(_, {
98
+ isSingle: h.single ?? !1,
94
99
  index: 0,
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 = {
102
- asc: (o, _) => o > _,
103
- desc: (o, _) => o < _
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: _
113
+ });
114
+ }
115
+ if (g === 0) {
116
+ let g = {
117
+ lazy: o,
118
+ lazyArgs: h
119
+ };
120
+ return Object.assign((o) => t(o, g), g);
121
+ }
122
+ throw Error("Wrong number of arguments");
123
+ }
124
+ var e$2 = {
125
+ asc: (o, h) => o > h,
126
+ desc: (o, h) => o < h
104
127
  };
105
- function t(o, _) {
106
- let [v, ...y] = _;
107
- if (!i(v)) {
108
- let _ = r$1(...y);
109
- return o(v, _);
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);
110
133
  }
111
- let b = r$1(v, ...y);
112
- return (_) => o(_, b);
113
- }
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);
116
- return (o, _) => {
117
- let v = y(o), b = y(_);
118
- return x(v, b) ? 1 : x(b, v) ? -1 : S?.(o, _) ?? 0;
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;
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
- let [_, v, ...y] = o;
125
- return a(_) && typeof v == "string" && v in e$1 && y.length === 0;
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);
126
153
  }
127
- var a = (o) => typeof o == "function" && o.length === 1;
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));
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, h) => o.flatMap(h), r$4 = (o) => (h, g, _) => {
171
+ let v = o(h, g, _);
172
+ return Array.isArray(v) ? {
173
+ done: !1,
174
+ hasNext: !0,
175
+ hasMany: !0,
176
+ next: v
177
+ } : {
178
+ done: !1,
179
+ hasNext: !0,
180
+ next: v
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, h) => o.map(h), r$3 = (o) => (h, g, _) => ({
139
194
  done: !1,
140
195
  hasNext: !0,
141
- next: o(_, v, y)
196
+ next: o(h, g, _)
142
197
  });
143
- function t$1(...o) {
144
- return t(n, o);
198
+ function t$6(...o) {
199
+ return t$10(n$8, o);
145
200
  }
146
- var n = (o, _) => [...o].sort(_);
147
- function isParserError(o) {
148
- return !e(o.errors);
201
+ var n$8 = (o) => o.length === 1 ? o[0] : void 0;
202
+ function t$4(...o) {
203
+ return t$10(n$7, o);
149
204
  }
150
- function isParserSuccess(o) {
151
- return !e(o.text);
205
+ function n$7(o, h) {
206
+ let g = [];
207
+ for (let _ = o; _ < h; _++) g.push(_);
208
+ return g;
152
209
  }
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
- };
167
- }
168
- function appendOffset(o, _) {
169
- return {
170
- ...o,
171
- offset: o.offset + _
172
- };
210
+ function t$1(...o) {
211
+ return t$11(n$6, o);
173
212
  }
174
- function appendOffsets(o, _) {
175
- return o.map((o) => appendOffset(o, _));
213
+ var n$6 = (o, h) => [...o].sort(h);
214
+ function n$3(...o) {
215
+ return t$10(r$2, o, i);
176
216
  }
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
- };
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
+ });
186
226
  }
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
- };
227
+ function n$2(...o) {
228
+ return t$9(r$1, o);
205
229
  }
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
- } : _;
230
+ function r$1() {
231
+ let o = /* @__PURE__ */ new Set();
232
+ return (h) => o.has(h) ? t$8 : (o.add(h), {
233
+ done: !1,
234
+ hasNext: !0,
235
+ next: h
236
+ });
214
237
  }
215
- function trimEmptyNode(o) {
216
- return filter((o) => isParserError(o) || o.text !== "" || !!o.children, o);
238
+ function n$4(...o) {
239
+ return t$9(r, o);
217
240
  }
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: _
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), {
246
+ done: !1,
247
+ hasNext: !0,
248
+ next: o
249
+ });
227
250
  };
228
251
  }
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
- }
252
+ var squiggly = {
253
+ textDecorationLine: "underline",
254
+ textDecorationStyle: "wavy",
255
+ textDecorationColor: "red"
251
256
  };
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
- };
282
- };
257
+ function SyntaxHighlighter({ cstRoot: o, ref: h, wrap: g, syntaxColors: _ }) {
258
+ return /* @__PURE__ */ jsx(ReadOnlyTextarea, {
259
+ ref: h,
260
+ wrap: g,
261
+ children: /* @__PURE__ */ jsx(StyledNode, {
262
+ node: o,
263
+ styleFor: _
264
+ })
265
+ });
283
266
  }
284
- function seq(o = "seq", ..._) {
285
- return sequence(o, optionalWhitespace, ..._.flatMap((o) => [o, optionalWhitespace]));
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,
272
+ "data-id": "ErrorHighlighter",
273
+ 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, {});
286
284
  }
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
- };
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))
293
+ });
303
294
  }
304
- const ws = repeat("ws", term(" ", "space"));
305
- function getSuggestions(o, _, v) {
306
- return console.assert(!0, o, _, v), {
307
- suggestions: [],
308
- prefix: ""
309
- };
295
+ function cstPathAt(o, h) {
296
+ return _cstPathAt(o, h, []);
310
297
  }
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: _
317
- });
318
- return v;
319
- }
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];
328
- });
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) : [];
329
300
  }
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);
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));
341
322
  }
342
- const CursorPosition = forwardRef(function(o, _) {
343
- let v = useRef(null), b = useRef(null);
344
- return useImperativeHandle(_, () => ({ getCursorPosition() {
345
- if (!v.current || !b.current) return {
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 {
346
327
  top: 0,
347
328
  left: 0
348
329
  };
349
- let { offsetTop: o, offsetLeft: _ } = v.current, { scrollTop: y, scrollLeft: x } = b.current, S = window.getComputedStyle(b.current), C = parseInt(S.lineHeight);
330
+ let { offsetTop: o, offsetLeft: h } = g.current, { scrollTop: _, scrollLeft: y } = v.current, b = window.getComputedStyle(v.current), x = parseInt(b.lineHeight);
350
331
  return {
351
- top: o - y + C,
352
- left: _ - x
332
+ top: o - _ + x,
333
+ left: h - y
353
334
  };
354
335
  } }), []), /* @__PURE__ */ jsxs(ReadOnlyTextarea, {
355
- ref: b,
336
+ ref: v,
356
337
  wrap: o.wrap,
357
338
  style: { visibility: "hidden" },
358
339
  children: [o.text, /* @__PURE__ */ jsx("span", {
359
- ref: v,
340
+ ref: g,
360
341
  children: "\xA0"
361
342
  })]
362
343
  });
363
344
  });
364
- function SuggestionsView({ suggestions: o, onSelect: _ }) {
345
+ function SuggestionsView({ suggestions: o, onSelect: h }) {
365
346
  return /* @__PURE__ */ jsx("div", {
366
347
  style: {
367
348
  display: "flex",
@@ -369,33 +350,203 @@ function SuggestionsView({ suggestions: o, onSelect: _ }) {
369
350
  padding: "4px 0",
370
351
  overflowY: "auto"
371
352
  },
372
- children: o.map((o, v) => /* @__PURE__ */ jsxs("button", {
373
- onClick: () => _(o),
353
+ children: o.map((o, g) => /* @__PURE__ */ jsxs("button", {
354
+ onClick: () => h(o),
374
355
  children: [
375
356
  "\xA0",
376
357
  o,
377
358
  "\xA0"
378
359
  ]
379
- }, v))
360
+ }, g))
380
361
  });
381
362
  }
382
363
  function shortcutName(o) {
383
- let _ = "";
384
- o.ctrlKey && (_ = "Ctrl");
385
- let { key: v } = o;
386
- return v === " " && (v = "Space"), `${_}${v}`;
387
- }
388
- function useSyncScroll(o) {
389
- return useCallback((_) => {
390
- let v = _.currentTarget;
391
- o.current && v && (o.current.scrollTop = v.scrollTop, o.current.scrollLeft = v.scrollLeft);
364
+ let h = "";
365
+ o.ctrlKey && (h = "Ctrl");
366
+ let { key: g } = o;
367
+ return g === " " && (g = "Space"), `${h}${g}`;
368
+ }
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
+ });
392
375
  }, [o]);
393
376
  }
394
- function SuggestionsMenu({ suggestions: o, onSelect: _, style: v, selectedIndex: y, onHover: b }) {
377
+ var ParsingError = class extends Error {
378
+ constructor(o) {
379
+ super(o);
380
+ }
381
+ };
382
+ function success(o) {
383
+ return o;
384
+ }
385
+ function error(o) {
386
+ return {
387
+ ...o,
388
+ offset: 0
389
+ };
390
+ }
391
+ function isParserError(o) {
392
+ return "expected" in o;
393
+ }
394
+ function isParserSuccess(o) {
395
+ return !isParserError(o);
396
+ }
397
+ function asException(o) {
398
+ return new ParsingError(`[${o.grammar.type}]: Expected ${o.expected.map((o) => `'${o}'`).join(" | ")}, but got '${o.got}'`);
399
+ }
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
+ };
420
+ }
421
+ v += S.text.length, y.push({
422
+ ...S,
423
+ recoverableError: b
424
+ }), b = !1;
425
+ }
426
+ return success({
427
+ grammar: h,
428
+ text: g.substring(0, v),
429
+ children: y
430
+ });
431
+ }
432
+ };
433
+ return h;
434
+ }
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
463
+ };
464
+ }
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]);
544
+ }
545
+ function SuggestionsMenu({ suggestions: o, onSelect: h, style: g, selectedIndex: _, onHover: v }) {
395
546
  return /* @__PURE__ */ jsx("div", {
396
547
  style: {
397
548
  position: "absolute",
398
- ...v,
549
+ ...g,
399
550
  background: "#252526",
400
551
  border: "1px solid #454545",
401
552
  boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
@@ -405,101 +556,89 @@ function SuggestionsMenu({ suggestions: o, onSelect: _, style: v, selectedIndex:
405
556
  minWidth: 200,
406
557
  zIndex: 100
407
558
  },
408
- children: o.map((o, v) => /* @__PURE__ */ jsx("div", {
409
- onClick: () => _(o),
410
- onMouseOver: () => b(v),
559
+ children: o.map((o, g) => /* @__PURE__ */ jsx("div", {
560
+ onClick: () => h(o),
561
+ onMouseOver: () => v(g),
411
562
  style: {
412
563
  cursor: "pointer",
413
564
  padding: "4px 8px",
414
- backgroundColor: y === v ? "#094771" : "transparent"
565
+ backgroundColor: _ === g ? "#094771" : "transparent"
415
566
  },
416
567
  children: o
417
- }, v))
568
+ }, g))
418
569
  });
419
570
  }
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({
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({
425
573
  top: 0,
426
574
  left: 0,
427
575
  visible: !1
428
- }), [R, z] = useState(0), [B, V] = useState(""), H = useRef(null), U = useRef(null), W = useRef(null), G = useRef(null), K = useCallback((o) => {
429
- let _ = U.current?.selectionStart ?? 0, v = getSuggestions(o, _, O);
430
- N(v);
431
- }, [O]), q = useCallback(() => {
432
- K(P), V(U.current?.value?.substring(0, U.current?.selectionStart ?? 0) ?? "");
433
- }, [P, K]);
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]);
434
582
  useEffect(() => {
435
- H.current = new Parser(E);
436
- }, [E]), useEffect(() => {
437
- if (!H.current) return;
438
- 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));
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));
442
588
  }, [
443
589
  o,
444
- T,
590
+ x,
445
591
  K
446
592
  ]);
447
- let J = useCallback(() => W.current?.getCursorPosition?.() ?? {
593
+ let J = useCallback(() => U.current?.getCursorPosition?.() ?? {
448
594
  top: 0,
449
595
  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
- }));
596
+ }, []), Y = useCallback((o) => {
597
+ H.current && console.log(o);
459
598
  }, [
460
599
  o,
461
- y,
462
- M
600
+ _,
601
+ A
463
602
  ]), X = useMemo(() => ({
464
603
  ArrowDown() {
465
- z((o) => (o + 1) % M.suggestions.length);
604
+ R((o) => (o + 1) % A.length);
466
605
  },
467
606
  ArrowUp() {
468
- z((o) => (o - 1 + M.suggestions.length) % M.suggestions.length);
607
+ R((o) => (o - 1 + A.length) % A.length);
469
608
  },
470
609
  Enter() {
471
- M.suggestions[R] && Y(M.suggestions[R]);
610
+ A[L] && Y(A[L].suggestion);
472
611
  },
473
612
  Escape() {
474
- L((o) => ({
613
+ I((o) => ({
475
614
  ...o,
476
615
  visible: !1
477
616
  }));
478
617
  }
479
618
  }), [
480
619
  Y,
481
- R,
482
- M
620
+ L,
621
+ A
483
622
  ]), Z = useMemo(() => ({ CtrlSpace() {
484
- let { top: o, left: _ } = J();
485
- z(0), L({
623
+ let { top: o, left: h } = J();
624
+ R(0), I({
486
625
  visible: !0,
487
626
  top: o,
488
- left: _
627
+ left: h
489
628
  });
490
629
  } }), [J]), Q = useCallback((o) => {
491
- let _ = (I.visible ? X : Z)[shortcutName(o)];
492
- _ && (o.preventDefault(), _());
630
+ let h = (F.visible ? X : Z)[shortcutName(o)];
631
+ h && (o.preventDefault(), h());
493
632
  }, [
494
- I.visible,
633
+ F.visible,
495
634
  X,
496
635
  Z
497
636
  ]), $ = useCallback((o) => {
498
- y(o.target.value), L((o) => ({
637
+ _(o.target.value), I((o) => ({
499
638
  ...o,
500
639
  visible: !1
501
- })), V(o.target.value.substring(0, o.target.selectionStart));
502
- }, [y]);
640
+ })), B(o.target.value.substring(0, o.target.selectionStart));
641
+ }, [_]);
503
642
  return /* @__PURE__ */ jsxs("div", {
504
643
  style: {
505
644
  display: "grid",
@@ -508,59 +647,732 @@ function DslEditor({ code: o, onChange: y, onParsed: T, grammar: E, wrap: D = !1
508
647
  width: "100%",
509
648
  height: "100%"
510
649
  },
511
- 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
- })]
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)) ?? []);
717
+ }
718
+ var require_types = /* @__PURE__ */ __commonJSMin(((o, h) => {
719
+ h.exports = {
720
+ ROOT: 0,
721
+ GROUP: 1,
722
+ POSITION: 2,
723
+ SET: 3,
724
+ RANGE: 4,
725
+ REPETITION: 5,
726
+ REFERENCE: 6,
727
+ CHAR: 7
728
+ };
729
+ })), require_sets = /* @__PURE__ */ __commonJSMin(((o) => {
730
+ var h = require_types(), g = () => [{
731
+ type: h.RANGE,
732
+ from: 48,
733
+ to: 57
734
+ }], _ = () => [
735
+ {
736
+ type: h.CHAR,
737
+ value: 95
738
+ },
739
+ {
740
+ type: h.RANGE,
741
+ from: 97,
742
+ to: 122
743
+ },
744
+ {
745
+ type: h.RANGE,
746
+ from: 65,
747
+ to: 90
748
+ }
749
+ ].concat(g()), v = () => [
750
+ {
751
+ type: h.CHAR,
752
+ value: 9
753
+ },
754
+ {
755
+ type: h.CHAR,
756
+ value: 10
757
+ },
758
+ {
759
+ type: h.CHAR,
760
+ value: 11
761
+ },
762
+ {
763
+ type: h.CHAR,
764
+ value: 12
765
+ },
766
+ {
767
+ type: h.CHAR,
768
+ value: 13
769
+ },
770
+ {
771
+ type: h.CHAR,
772
+ value: 32
773
+ },
774
+ {
775
+ type: h.CHAR,
776
+ value: 160
777
+ },
778
+ {
779
+ type: h.CHAR,
780
+ value: 5760
781
+ },
782
+ {
783
+ type: h.RANGE,
784
+ from: 8192,
785
+ to: 8202
786
+ },
787
+ {
788
+ type: h.CHAR,
789
+ value: 8232
790
+ },
791
+ {
792
+ type: h.CHAR,
793
+ value: 8233
794
+ },
795
+ {
796
+ type: h.CHAR,
797
+ value: 8239
798
+ },
799
+ {
800
+ type: h.CHAR,
801
+ value: 8287
802
+ },
803
+ {
804
+ type: h.CHAR,
805
+ value: 12288
806
+ },
807
+ {
808
+ type: h.CHAR,
809
+ value: 65279
810
+ }
811
+ ], y = () => [
812
+ {
813
+ type: h.CHAR,
814
+ value: 10
815
+ },
816
+ {
817
+ type: h.CHAR,
818
+ value: 13
819
+ },
820
+ {
821
+ type: h.CHAR,
822
+ value: 8232
823
+ },
824
+ {
825
+ type: h.CHAR,
826
+ value: 8233
827
+ }
828
+ ];
829
+ o.words = () => ({
830
+ type: h.SET,
831
+ set: _(),
832
+ not: !1
833
+ }), o.notWords = () => ({
834
+ type: h.SET,
835
+ set: _(),
836
+ not: !0
837
+ }), o.ints = () => ({
838
+ type: h.SET,
839
+ set: g(),
840
+ not: !1
841
+ }), o.notInts = () => ({
842
+ type: h.SET,
843
+ set: g(),
844
+ not: !0
845
+ }), o.whitespace = () => ({
846
+ type: h.SET,
847
+ set: v(),
848
+ not: !1
849
+ }), o.notWhitespace = () => ({
850
+ type: h.SET,
851
+ set: v(),
852
+ not: !0
853
+ }), o.anyChar = () => ({
854
+ type: h.SET,
855
+ set: y(),
856
+ not: !0
564
857
  });
858
+ })), require_util = /* @__PURE__ */ __commonJSMin(((o) => {
859
+ var h = require_types(), g = require_sets(), _ = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ ?", v = {
860
+ 0: 0,
861
+ t: 9,
862
+ n: 10,
863
+ v: 11,
864
+ f: 12,
865
+ r: 13
866
+ };
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)
884
+ });
885
+ else if (S = x[12]) y.push({
886
+ type: h.CHAR,
887
+ value: S.charCodeAt(0)
888
+ });
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);
893
+ };
894
+ })), require_positions = /* @__PURE__ */ __commonJSMin(((o) => {
895
+ var h = require_types();
896
+ o.wordBoundary = () => ({
897
+ type: h.POSITION,
898
+ value: "b"
899
+ }), o.nonWordBoundary = () => ({
900
+ type: h.POSITION,
901
+ value: "B"
902
+ }), o.begin = () => ({
903
+ type: h.POSITION,
904
+ value: "^"
905
+ }), o.end = () => ({
906
+ type: h.POSITION,
907
+ value: "$"
908
+ });
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,
914
+ 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) {
919
+ case "\\":
920
+ switch (x = D[h++], x) {
921
+ case "b":
922
+ w.push(y.wordBoundary());
923
+ break;
924
+ case "B":
925
+ w.push(y.nonWordBoundary());
926
+ break;
927
+ case "w":
928
+ w.push(v.words());
929
+ break;
930
+ case "W":
931
+ w.push(v.notWords());
932
+ break;
933
+ case "d":
934
+ w.push(v.ints());
935
+ break;
936
+ case "D":
937
+ w.push(v.notInts());
938
+ break;
939
+ case "s":
940
+ w.push(v.whitespace());
941
+ break;
942
+ case "S":
943
+ w.push(v.notWhitespace());
944
+ 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)
951
+ });
952
+ }
953
+ break;
954
+ case "^":
955
+ w.push(y.begin());
956
+ break;
957
+ case "$":
958
+ w.push(y.end());
959
+ break;
960
+ 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
968
+ });
969
+ break;
970
+ case ".":
971
+ w.push(v.anyChar());
972
+ break;
973
+ case "(":
974
+ var A = {
975
+ type: _.GROUP,
976
+ stack: [],
977
+ remember: !0
978
+ };
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;
980
+ break;
981
+ 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;
983
+ break;
984
+ case "|":
985
+ C.options || (C.options = [C.stack], delete C.stack);
986
+ var j = [];
987
+ C.options.push(j), w = j;
988
+ break;
989
+ case "{":
990
+ var M = /^(\d+)(,(\d+)?)?\}/.exec(D.slice(h)), N, P;
991
+ M === null ? w.push({
992
+ type: _.CHAR,
993
+ 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()
999
+ }));
1000
+ break;
1001
+ case "?":
1002
+ w.length === 0 && E(h), w.push({
1003
+ type: _.REPETITION,
1004
+ min: 0,
1005
+ max: 1,
1006
+ value: w.pop()
1007
+ });
1008
+ break;
1009
+ case "+":
1010
+ w.length === 0 && E(h), w.push({
1011
+ type: _.REPETITION,
1012
+ min: 1,
1013
+ max: Infinity,
1014
+ value: w.pop()
1015
+ });
1016
+ break;
1017
+ case "*":
1018
+ w.length === 0 && E(h), w.push({
1019
+ type: _.REPETITION,
1020
+ min: 0,
1021
+ max: Infinity,
1022
+ value: w.pop()
1023
+ });
1024
+ break;
1025
+ default: w.push({
1026
+ type: _.CHAR,
1027
+ value: x.charCodeAt(0)
1028
+ });
1029
+ }
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;
1036
+ }
1037
+ overlaps(o) {
1038
+ return !(this.high < o.low || this.low > o.high);
1039
+ }
1040
+ touches(o) {
1041
+ return !(this.high + 1 < o.low || this.low - 1 > o.high);
1042
+ }
1043
+ add(h) {
1044
+ return new o(Math.min(this.low, h.low), Math.max(this.high, h.high));
1045
+ }
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)];
1048
+ }
1049
+ toString() {
1050
+ return this.low == this.high ? this.low.toString() : this.low + "-" + this.high;
1051
+ }
1052
+ };
1053
+ h.exports = class o {
1054
+ constructor(o, h) {
1055
+ this.ranges = [], this.length = 0, o != null && this.add(o, h);
1056
+ }
1057
+ _update_length() {
1058
+ this.length = this.ranges.reduce((o, h) => o + h.length, 0);
1059
+ }
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();
1065
+ };
1066
+ return h instanceof o ? h.ranges.forEach(v) : (_ ??= h, v(new g(h, _))), this;
1067
+ }
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();
1073
+ };
1074
+ return h instanceof o ? h.ranges.forEach(v) : (_ ??= h, v(new g(h, _))), this;
1075
+ }
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++;
1082
+ }
1083
+ };
1084
+ return h instanceof o ? h.ranges.forEach(y) : (_ ??= h, y(new g(h, _))), this.ranges = v, this._update_length(), this;
1085
+ }
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;
1089
+ }
1090
+ toString() {
1091
+ return "[ " + this.ranges.join(", ") + " ]";
1092
+ }
1093
+ clone() {
1094
+ return new o(this);
1095
+ }
1096
+ numbers() {
1097
+ return this.ranges.reduce((o, h) => {
1098
+ for (var g = h.low; g <= h.high;) o.push(g), g++;
1099
+ return o;
1100
+ }, []);
1101
+ }
1102
+ subranges() {
1103
+ return this.ranges.map((o) => ({
1104
+ low: o.low,
1105
+ high: o.high,
1106
+ length: 1 + o.high - o.low
1107
+ }));
1108
+ }
1109
+ };
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;
1116
+ else throw Error("Expected a regexp or string");
1117
+ this.tokens = g(o);
1118
+ }
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);
1121
+ }
1122
+ gen() {
1123
+ return this._gen(this.tokens, []);
1124
+ }
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);
1144
+ }
1145
+ }
1146
+ _toOtherCase(o) {
1147
+ return o + (97 <= o && o <= 122 ? -32 : 65 <= o && o <= 90 ? 32 : 0);
1148
+ }
1149
+ _randBool() {
1150
+ return !this.randInt(0, 1);
1151
+ }
1152
+ _randSelect(o) {
1153
+ return o instanceof _ ? o.index(this.randInt(0, o.length - 1)) : o[this.randInt(0, o.length - 1)];
1154
+ }
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);
1158
+ {
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);
1165
+ }
1166
+ }
1167
+ return o.not ? this.defaultRange.clone().subtract(h) : this.defaultRange.clone().intersect(h);
1168
+ }
1169
+ }
1170
+ randInt(o, h) {
1171
+ return o + Math.floor(Math.random() * (1 + h - o));
1172
+ }
1173
+ get defaultRange() {
1174
+ return this._range = this._range || new _(32, 126);
1175
+ }
1176
+ set defaultRange(o) {
1177
+ this._range = o;
1178
+ }
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();
1182
+ }
1183
+ static sugar() {
1184
+ RegExp.prototype.gen = function() {
1185
+ return o.randexp(this);
1186
+ };
1187
+ }
1188
+ };
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
+ }
1209
+ }
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 _ = {
1219
+ type: "pattern",
1220
+ children: [],
1221
+ meta: { regex: o },
1222
+ suggestions: g,
1223
+ parse(h, v) {
1224
+ let y = new RegExp(o, "yu");
1225
+ y.lastIndex = 0;
1226
+ let b = y.exec(h);
1227
+ if (b) return success({
1228
+ 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,
1261
+ children: []
1262
+ }) : v;
1263
+ }
1264
+ };
1265
+ return h;
1266
+ }
1267
+ const ws = optional(pattern(/\s+/));
1268
+ function seq(...o) {
1269
+ return sequence(ws, ...o.flatMap((o) => [o, ws]));
1270
+ }
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;
1304
+ }
1305
+ return o;
1306
+ }
1307
+ return error({
1308
+ got: g,
1309
+ expected: v.flatMap((o) => o.expected),
1310
+ grammar: h
1311
+ });
1312
+ },
1313
+ suggestions() {
1314
+ return o.flatMap((o) => o.suggestions());
1315
+ }
1316
+ };
1317
+ return h;
1318
+ }
1319
+ function named(o, h) {
1320
+ let g = {
1321
+ meta: { name: o },
1322
+ type: "named",
1323
+ suggestions() {
1324
+ return h.suggestions();
1325
+ },
1326
+ children: [h],
1327
+ parse(o, _) {
1328
+ let v = h.parse(o, _);
1329
+ return isParserError(v) ? v : success({
1330
+ text: v.text,
1331
+ grammar: g,
1332
+ children: [v]
1333
+ });
1334
+ }
1335
+ };
1336
+ return g;
1337
+ }
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
1367
+ });
1368
+ }
1369
+ return success({
1370
+ grammar: _,
1371
+ children: S,
1372
+ text: v.substring(0, b)
1373
+ });
1374
+ }
1375
+ };
1376
+ return _;
565
1377
  }
566
- export { DslEditor, Parser, ParsingError, alternative, appendOffset, appendOffsets, isParserError, isParserSuccess, optional, optionalWhitespace, pattern, rational, repeat, seq, sequence, term, ws };
1378
+ export { DSLParser, DslEditor, ParsingError, alternative, asException, eof, error, isParserError, isParserSuccess, named, optional, pattern, rational, repeat, seq, sequence, success, term, visit, ws };