@traqula/core 0.0.1-alpha.148 → 0.0.1-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +44 -22
  2. package/lib/CoreFactory.d.ts +39 -0
  3. package/lib/CoreFactory.js +135 -0
  4. package/lib/CoreFactory.js.map +1 -0
  5. package/lib/RangeArithmetic.d.ts +14 -0
  6. package/lib/RangeArithmetic.js +71 -0
  7. package/lib/RangeArithmetic.js.map +1 -0
  8. package/lib/Transformer.d.ts +26 -0
  9. package/lib/Transformer.js +57 -0
  10. package/lib/Transformer.js.map +1 -0
  11. package/lib/generator-builder/builderTypes.d.ts +23 -0
  12. package/lib/generator-builder/builderTypes.js.map +1 -0
  13. package/lib/generator-builder/dynamicGenerator.d.ts +22 -0
  14. package/lib/generator-builder/dynamicGenerator.js +115 -0
  15. package/lib/generator-builder/dynamicGenerator.js.map +1 -0
  16. package/lib/generator-builder/generatorBuilder.d.ts +59 -0
  17. package/lib/generator-builder/generatorBuilder.js +104 -0
  18. package/lib/generator-builder/generatorBuilder.js.map +1 -0
  19. package/lib/generator-builder/generatorTypes.d.ts +36 -0
  20. package/lib/generator-builder/generatorTypes.js +2 -0
  21. package/lib/generator-builder/generatorTypes.js.map +1 -0
  22. package/lib/index.cjs +706 -297
  23. package/lib/index.d.ts +11 -6
  24. package/lib/index.js +15 -6
  25. package/lib/index.js.map +1 -1
  26. package/lib/lexer-builder/LexerBuilder.d.ts +11 -9
  27. package/lib/lexer-builder/LexerBuilder.js +16 -4
  28. package/lib/lexer-builder/LexerBuilder.js.map +1 -1
  29. package/lib/nodeTypings.d.ts +53 -0
  30. package/lib/nodeTypings.js +2 -0
  31. package/lib/nodeTypings.js.map +1 -0
  32. package/lib/parser-builder/builderTypes.d.ts +24 -0
  33. package/lib/parser-builder/builderTypes.js +2 -0
  34. package/lib/parser-builder/builderTypes.js.map +1 -0
  35. package/lib/parser-builder/dynamicParser.d.ts +9 -0
  36. package/lib/parser-builder/dynamicParser.js +113 -0
  37. package/lib/parser-builder/dynamicParser.js.map +1 -0
  38. package/lib/parser-builder/parserBuilder.d.ts +74 -0
  39. package/lib/parser-builder/parserBuilder.js +171 -0
  40. package/lib/parser-builder/parserBuilder.js.map +1 -0
  41. package/lib/{grammar-builder → parser-builder}/ruleDefTypes.d.ts +5 -5
  42. package/lib/parser-builder/ruleDefTypes.js.map +1 -0
  43. package/lib/utils.d.ts +17 -0
  44. package/lib/utils.js +8 -0
  45. package/lib/utils.js.map +1 -0
  46. package/package.json +6 -6
  47. package/lib/Wildcard.d.ts +0 -9
  48. package/lib/Wildcard.js +0 -19
  49. package/lib/Wildcard.js.map +0 -1
  50. package/lib/grammar-builder/builderTypes.d.ts +0 -28
  51. package/lib/grammar-builder/builderTypes.js.map +0 -1
  52. package/lib/grammar-builder/parserBuilder.d.ts +0 -68
  53. package/lib/grammar-builder/parserBuilder.js +0 -283
  54. package/lib/grammar-builder/parserBuilder.js.map +0 -1
  55. package/lib/grammar-builder/ruleDefTypes.js.map +0 -1
  56. package/lib/grammar-helpers/utils.d.ts +0 -16
  57. package/lib/grammar-helpers/utils.js +0 -53
  58. package/lib/grammar-helpers/utils.js.map +0 -1
  59. package/lib/lexer-helper/utils.d.ts +0 -6
  60. package/lib/lexer-helper/utils.js +0 -5
  61. package/lib/lexer-helper/utils.js.map +0 -1
  62. /package/lib/{grammar-builder → generator-builder}/builderTypes.js +0 -0
  63. /package/lib/{grammar-builder → parser-builder}/ruleDefTypes.js +0 -0
package/lib/index.cjs CHANGED
@@ -20,16 +20,349 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // lib/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- Builder: () => Builder,
24
- CommonIRIs: () => CommonIRIs,
23
+ CoreFactory: () => CoreFactory,
24
+ GeneratorBuilder: () => GeneratorBuilder,
25
25
  LexerBuilder: () => LexerBuilder,
26
- Wildcard: () => Wildcard,
26
+ ParserBuilder: () => ParserBuilder,
27
+ RangeArithmetic: () => RangeArithmetic,
28
+ Transformer: () => Transformer,
27
29
  createToken: () => createToken2,
28
- resolveIRI: () => resolveIRI,
29
30
  unCapitalize: () => unCapitalize
30
31
  });
31
32
  module.exports = __toCommonJS(index_exports);
32
33
 
34
+ // lib/CoreFactory.ts
35
+ var CoreFactory = class {
36
+ wrap(val, loc) {
37
+ return { val, loc };
38
+ }
39
+ isLocalized(obj) {
40
+ return typeof obj === "object" && obj !== null && "loc" in obj && typeof obj.loc === "object" && obj.loc !== null && "sourceLocationType" in obj.loc;
41
+ }
42
+ sourceLocation(...elements) {
43
+ const pureElements = elements.filter((x) => x !== void 0);
44
+ if (pureElements.length === 0) {
45
+ return this.sourceLocationNoMaterialize();
46
+ }
47
+ const filtered = pureElements.filter((element) => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) || this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
48
+ if (filtered.length === 0) {
49
+ return this.gen();
50
+ }
51
+ const first2 = filtered[0];
52
+ const last2 = filtered.at(-1);
53
+ return {
54
+ sourceLocationType: "source",
55
+ start: this.isLocalized(first2) ? first2.loc.start : first2.startOffset,
56
+ end: this.isLocalized(last2) ? last2.loc.end : last2.endOffset + 1
57
+ };
58
+ }
59
+ sourceLocationNoMaterialize() {
60
+ return { sourceLocationType: "noMaterialize" };
61
+ }
62
+ /**
63
+ * Returns a copy of the argument that is not materialized
64
+ */
65
+ dematerialized(arg) {
66
+ return { ...arg, loc: this.sourceLocationNoMaterialize() };
67
+ }
68
+ safeObjectTransform(value, mapper) {
69
+ if (value && typeof value === "object") {
70
+ if (Array.isArray(value)) {
71
+ return value.map((x) => this.safeObjectTransform(x, mapper));
72
+ }
73
+ return mapper(value);
74
+ }
75
+ return value;
76
+ }
77
+ forcedAutoGenTree(obj) {
78
+ const copy = { ...obj };
79
+ for (const [key, value] of Object.entries(copy)) {
80
+ copy[key] = this.safeObjectTransform(value, (obj2) => this.forcedAutoGenTree(obj2));
81
+ }
82
+ if (this.isLocalized(copy)) {
83
+ copy.loc = this.gen();
84
+ }
85
+ return copy;
86
+ }
87
+ forceMaterialized(arg) {
88
+ if (this.isSourceLocationNoMaterialize(arg.loc)) {
89
+ return this.forcedAutoGenTree(arg);
90
+ }
91
+ return { ...arg };
92
+ }
93
+ isSourceLocation(loc) {
94
+ return "sourceLocationType" in loc;
95
+ }
96
+ sourceLocationSource(start, end) {
97
+ return {
98
+ sourceLocationType: "source",
99
+ start,
100
+ end
101
+ };
102
+ }
103
+ gen() {
104
+ return { sourceLocationType: "autoGenerate" };
105
+ }
106
+ isSourceLocationSource(loc) {
107
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "source";
108
+ }
109
+ isSourceLocationStringReplace(loc) {
110
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "stringReplace";
111
+ }
112
+ sourceLocationNodeReplaceUnsafe(loc) {
113
+ if (this.isSourceLocationSource(loc)) {
114
+ return this.sourceLocationNodeReplace(loc);
115
+ }
116
+ throw new Error("not possible");
117
+ }
118
+ sourceLocationNodeReplace(startOrLoc, end) {
119
+ let starting;
120
+ let ending;
121
+ if (typeof startOrLoc === "number") {
122
+ starting = startOrLoc;
123
+ ending = end;
124
+ } else {
125
+ starting = startOrLoc.start;
126
+ ending = startOrLoc.end;
127
+ }
128
+ return {
129
+ sourceLocationType: "nodeReplace",
130
+ start: starting,
131
+ end: ending
132
+ };
133
+ }
134
+ isSourceLocationNodeReplace(loc) {
135
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "nodeReplace";
136
+ }
137
+ isSourceLocationNodeAutoGenerate(loc) {
138
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "autoGenerate";
139
+ }
140
+ nodeShouldPrint(node) {
141
+ return this.isSourceLocationNodeReplace(node.loc) || this.isSourceLocationNodeAutoGenerate(node.loc);
142
+ }
143
+ printFilter(node, callback) {
144
+ if (this.nodeShouldPrint(node)) {
145
+ callback();
146
+ }
147
+ }
148
+ isSourceLocationNoMaterialize(loc) {
149
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "noMaterialize";
150
+ }
151
+ isOfType(obj, type) {
152
+ const casted = obj;
153
+ return casted.type === type;
154
+ }
155
+ isOfSubType(obj, type, subType) {
156
+ const temp = obj;
157
+ return temp.type === type && temp.subType === subType;
158
+ }
159
+ };
160
+
161
+ // lib/generator-builder/dynamicGenerator.ts
162
+ var DynamicGenerator = class {
163
+ constructor(rules) {
164
+ this.rules = rules;
165
+ this.factory = new CoreFactory();
166
+ this.__context = void 0;
167
+ this.origSource = "";
168
+ this.generatedUntil = 0;
169
+ this.stringBuilder = [];
170
+ this.subrule = (cstDef, ast, arg) => {
171
+ const def = this.rules[cstDef.name];
172
+ if (!def) {
173
+ throw new Error(`Rule ${cstDef.name} not found`);
174
+ }
175
+ const generate = () => def.gImpl({
176
+ SUBRULE: this.subrule,
177
+ PRINT: this.print,
178
+ PRINT_SPACE_LEFT: this.printSpaceLeft,
179
+ PRINT_WORD: this.printWord,
180
+ PRINT_WORDS: this.printWords,
181
+ CATCHUP: this.catchup,
182
+ HANDLE_LOC: this.handleLoc
183
+ })(ast, this.getSafeContext(), arg);
184
+ if (this.factory.isLocalized(ast)) {
185
+ this.handleLoc(ast, generate);
186
+ } else {
187
+ generate();
188
+ }
189
+ };
190
+ this.handleLoc = (localized, handle) => {
191
+ if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
192
+ return;
193
+ }
194
+ if (this.factory.isSourceLocationStringReplace(localized.loc)) {
195
+ this.catchup(localized.loc.start);
196
+ this.print(localized.loc.newSource);
197
+ this.generatedUntil = localized.loc.end;
198
+ return;
199
+ }
200
+ if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
201
+ this.catchup(localized.loc.start);
202
+ this.generatedUntil = localized.loc.end;
203
+ }
204
+ if (this.factory.isSourceLocationSource(localized.loc)) {
205
+ this.catchup(localized.loc.start);
206
+ }
207
+ const ret = handle();
208
+ if (this.factory.isSourceLocationSource(localized.loc)) {
209
+ this.catchup(localized.loc.end);
210
+ }
211
+ return ret;
212
+ };
213
+ this.catchup = (until) => {
214
+ const start = this.generatedUntil;
215
+ if (start < until) {
216
+ this.print(this.origSource.slice(start, until));
217
+ }
218
+ this.generatedUntil = Math.max(this.generatedUntil, until);
219
+ };
220
+ this.print = (...args) => {
221
+ const pureArgs = args.filter((x) => x.length > 0);
222
+ if (pureArgs.length > 0) {
223
+ const [head2, ...tail] = pureArgs;
224
+ if (this.expectsSpace) {
225
+ this.expectsSpace = false;
226
+ const blanks = /* @__PURE__ */ new Set(["\n", " ", " "]);
227
+ if (this.stringBuilder.length > 0 && !(blanks.has(head2[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
228
+ this.stringBuilder.push(" ");
229
+ }
230
+ }
231
+ this.stringBuilder.push(head2);
232
+ this.stringBuilder.push(...tail);
233
+ }
234
+ };
235
+ this.printWord = (...args) => {
236
+ this.expectsSpace = true;
237
+ this.print(...args);
238
+ this.expectsSpace = true;
239
+ };
240
+ this.printSpaceLeft = (...args) => {
241
+ this.expectsSpace = true;
242
+ this.print(...args);
243
+ };
244
+ this.printWords = (...args) => {
245
+ for (const arg of args) {
246
+ this.printWord(arg);
247
+ }
248
+ };
249
+ for (const rule of Object.values(rules)) {
250
+ this[rule.name] = (input, context, args) => {
251
+ this.expectsSpace = false;
252
+ this.stringBuilder.length = 0;
253
+ this.origSource = context.origSource;
254
+ this.generatedUntil = context?.offset ?? 0;
255
+ this.setContext(context);
256
+ this.subrule(rule, input, args);
257
+ this.catchup(this.origSource.length);
258
+ return this.stringBuilder.join("");
259
+ };
260
+ }
261
+ }
262
+ setContext(context) {
263
+ this.__context = context;
264
+ }
265
+ getSafeContext() {
266
+ return this.__context;
267
+ }
268
+ };
269
+
270
+ // lib/generator-builder/generatorBuilder.ts
271
+ function listToRuleDefMap(rules) {
272
+ const newRules = {};
273
+ for (const rule of rules) {
274
+ newRules[rule.name] = rule;
275
+ }
276
+ return newRules;
277
+ }
278
+ var GeneratorBuilder = class _GeneratorBuilder {
279
+ static create(start) {
280
+ if (start instanceof _GeneratorBuilder) {
281
+ return new _GeneratorBuilder({ ...start.rules });
282
+ }
283
+ return new _GeneratorBuilder(listToRuleDefMap(start));
284
+ }
285
+ constructor(startRules) {
286
+ this.rules = startRules;
287
+ }
288
+ widenContext() {
289
+ return this;
290
+ }
291
+ typePatch() {
292
+ return this;
293
+ }
294
+ /**
295
+ * Change the implementation of an existing generator rule.
296
+ */
297
+ patchRule(patch) {
298
+ const self2 = this;
299
+ self2.rules[patch.name] = patch;
300
+ return self2;
301
+ }
302
+ /**
303
+ * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
304
+ */
305
+ addRuleRedundant(rule) {
306
+ const self2 = this;
307
+ const rules = self2.rules;
308
+ if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
309
+ throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);
310
+ }
311
+ rules[rule.name] = rule;
312
+ return self2;
313
+ }
314
+ /**
315
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
316
+ */
317
+ addRule(rule) {
318
+ return this.addRuleRedundant(rule);
319
+ }
320
+ addMany(...rules) {
321
+ this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
322
+ return this;
323
+ }
324
+ /**
325
+ * Delete a grammar rule by its name.
326
+ */
327
+ deleteRule(ruleName) {
328
+ delete this.rules[ruleName];
329
+ return this;
330
+ }
331
+ /**
332
+ * Merge this grammar GeneratorBuilder with another.
333
+ * It is best to merge the bigger grammar with the smaller one.
334
+ * If the two builders both have a grammar rule with the same name,
335
+ * no error will be thrown case they map to the same ruledef object.
336
+ * If they map to a different object, an error will be thrown.
337
+ * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
338
+ * this rule implementation will be used.
339
+ */
340
+ merge(GeneratorBuilder2, overridingRules) {
341
+ const otherRules = { ...GeneratorBuilder2.rules };
342
+ const myRules = this.rules;
343
+ for (const rule of Object.values(myRules)) {
344
+ if (otherRules[rule.name] === void 0) {
345
+ otherRules[rule.name] = rule;
346
+ } else {
347
+ const existingRule = otherRules[rule.name];
348
+ if (existingRule !== rule) {
349
+ const override = overridingRules.find((x) => x.name === rule.name);
350
+ if (override) {
351
+ otherRules[rule.name] = override;
352
+ } else {
353
+ throw new Error(`Rule with name "${rule.name}" already exists in the GeneratorBuilder, specify an override to resolve conflict`);
354
+ }
355
+ }
356
+ }
357
+ }
358
+ this.rules = otherRules;
359
+ return this;
360
+ }
361
+ build() {
362
+ return new DynamicGenerator(this.rules);
363
+ }
364
+ };
365
+
33
366
  // ../../node_modules/lodash-es/_freeGlobal.js
34
367
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
35
368
  var freeGlobal_default = freeGlobal;
@@ -1395,8 +1728,8 @@ var Promise2 = getNative_default(root_default, "Promise");
1395
1728
  var Promise_default = Promise2;
1396
1729
 
1397
1730
  // ../../node_modules/lodash-es/_Set.js
1398
- var Set = getNative_default(root_default, "Set");
1399
- var Set_default = Set;
1731
+ var Set2 = getNative_default(root_default, "Set");
1732
+ var Set_default = Set2;
1400
1733
 
1401
1734
  // ../../node_modules/lodash-es/_getTag.js
1402
1735
  var mapTag2 = "[object Map]";
@@ -9248,28 +9581,242 @@ var EmbeddedActionsParser = class extends Parser {
9248
9581
  }
9249
9582
  };
9250
9583
 
9251
- // lib/grammar-builder/parserBuilder.ts
9252
- function listToRuleDefMap(rules) {
9584
+ // lib/lexer-builder/LexerBuilder.ts
9585
+ var LexerBuilder = class _LexerBuilder {
9586
+ static create(starter) {
9587
+ return new _LexerBuilder(starter);
9588
+ }
9589
+ constructor(starter) {
9590
+ this.tokens = starter?.tokens ? [...starter.tokens] : [];
9591
+ }
9592
+ merge(merge, overwrite = []) {
9593
+ const extraTokens = merge.tokens.filter((token) => {
9594
+ const overwriteToken = overwrite.find((t) => t.name === token.name);
9595
+ if (overwriteToken) {
9596
+ return false;
9597
+ }
9598
+ const match = this.tokens.find((t) => t.name === token.name);
9599
+ if (match) {
9600
+ if (match !== token) {
9601
+ throw new Error(`Token with name ${token.name} already exists. Implementation is different and no overwrite was provided.`);
9602
+ }
9603
+ return false;
9604
+ }
9605
+ return true;
9606
+ });
9607
+ this.tokens.push(...extraTokens);
9608
+ return this;
9609
+ }
9610
+ add(...token) {
9611
+ this.tokens.push(...token);
9612
+ return this;
9613
+ }
9614
+ addBefore(before, ...token) {
9615
+ const index = this.tokens.indexOf(before);
9616
+ if (index === -1) {
9617
+ throw new Error("Token not found");
9618
+ }
9619
+ this.tokens.splice(index, 0, ...token);
9620
+ return this;
9621
+ }
9622
+ moveBeforeOrAfter(beforeOrAfter, before, ...tokens) {
9623
+ const beforeIndex = this.tokens.indexOf(before) + (beforeOrAfter === "before" ? 0 : 1);
9624
+ if (beforeIndex === -1) {
9625
+ throw new Error("BeforeToken not found");
9626
+ }
9627
+ for (const token of tokens) {
9628
+ const tokenIndex = this.tokens.indexOf(token);
9629
+ if (tokenIndex === -1) {
9630
+ throw new Error("Token not found");
9631
+ }
9632
+ this.tokens.splice(tokenIndex, 1);
9633
+ this.tokens.splice(beforeIndex, 0, token);
9634
+ }
9635
+ return this;
9636
+ }
9637
+ /**
9638
+ * @param before token to move rest before
9639
+ * @param tokens tokens to move before the first token
9640
+ */
9641
+ moveBefore(before, ...tokens) {
9642
+ return this.moveBeforeOrAfter("before", before, ...tokens);
9643
+ }
9644
+ moveAfter(after, ...tokens) {
9645
+ return this.moveBeforeOrAfter("after", after, ...tokens);
9646
+ }
9647
+ addAfter(after, ...token) {
9648
+ const index = this.tokens.indexOf(after);
9649
+ if (index === -1) {
9650
+ throw new Error("Token not found");
9651
+ }
9652
+ this.tokens.splice(index + 1, 0, ...token);
9653
+ return this;
9654
+ }
9655
+ delete(...token) {
9656
+ for (const t of token) {
9657
+ const index = this.tokens.indexOf(t);
9658
+ if (index === -1) {
9659
+ throw new Error("Token not found");
9660
+ }
9661
+ this.tokens.splice(index, 1);
9662
+ }
9663
+ return this;
9664
+ }
9665
+ build(lexerConfig) {
9666
+ return new Lexer(this.tokens, {
9667
+ positionTracking: "onlyStart",
9668
+ recoveryEnabled: false,
9669
+ ensureOptimizations: true,
9670
+ // SafeMode: true,
9671
+ // SkipValidations: true,
9672
+ ...lexerConfig
9673
+ });
9674
+ }
9675
+ get tokenVocabulary() {
9676
+ return this.tokens;
9677
+ }
9678
+ };
9679
+
9680
+ // lib/parser-builder/dynamicParser.ts
9681
+ var DynamicParser = class extends EmbeddedActionsParser {
9682
+ setContext(context) {
9683
+ this.context = context;
9684
+ }
9685
+ constructor(rules, tokenVocabulary, config = {}) {
9686
+ super(tokenVocabulary, {
9687
+ // RecoveryEnabled: true,
9688
+ maxLookahead: 2,
9689
+ // SkipValidations: true,
9690
+ ...config
9691
+ });
9692
+ this.context = void 0;
9693
+ const selfRef = this.constructSelfRef();
9694
+ const implArgs = {
9695
+ ...selfRef,
9696
+ cache: /* @__PURE__ */ new WeakMap()
9697
+ };
9698
+ for (const rule of Object.values(rules)) {
9699
+ this[rule.name] = this.RULE(rule.name, rule.impl(implArgs));
9700
+ }
9701
+ this.performSelfAnalysis();
9702
+ }
9703
+ constructSelfRef() {
9704
+ const subRuleImpl = (chevrotainSubrule) => (cstDef, arg) => chevrotainSubrule(this[cstDef.name], { ARGS: [this.context, arg] });
9705
+ return {
9706
+ CONSUME: (tokenType, option) => this.CONSUME(tokenType, option),
9707
+ CONSUME1: (tokenType, option) => this.CONSUME1(tokenType, option),
9708
+ CONSUME2: (tokenType, option) => this.CONSUME2(tokenType, option),
9709
+ CONSUME3: (tokenType, option) => this.CONSUME3(tokenType, option),
9710
+ CONSUME4: (tokenType, option) => this.CONSUME4(tokenType, option),
9711
+ CONSUME5: (tokenType, option) => this.CONSUME5(tokenType, option),
9712
+ CONSUME6: (tokenType, option) => this.CONSUME6(tokenType, option),
9713
+ CONSUME7: (tokenType, option) => this.CONSUME7(tokenType, option),
9714
+ CONSUME8: (tokenType, option) => this.CONSUME8(tokenType, option),
9715
+ CONSUME9: (tokenType, option) => this.CONSUME9(tokenType, option),
9716
+ OPTION: (actionORMethodDef) => this.OPTION(actionORMethodDef),
9717
+ OPTION1: (actionORMethodDef) => this.OPTION1(actionORMethodDef),
9718
+ OPTION2: (actionORMethodDef) => this.OPTION2(actionORMethodDef),
9719
+ OPTION3: (actionORMethodDef) => this.OPTION3(actionORMethodDef),
9720
+ OPTION4: (actionORMethodDef) => this.OPTION4(actionORMethodDef),
9721
+ OPTION5: (actionORMethodDef) => this.OPTION5(actionORMethodDef),
9722
+ OPTION6: (actionORMethodDef) => this.OPTION6(actionORMethodDef),
9723
+ OPTION7: (actionORMethodDef) => this.OPTION7(actionORMethodDef),
9724
+ OPTION8: (actionORMethodDef) => this.OPTION8(actionORMethodDef),
9725
+ OPTION9: (actionORMethodDef) => this.OPTION9(actionORMethodDef),
9726
+ OR: (altsOrOpts) => this.OR(altsOrOpts),
9727
+ OR1: (altsOrOpts) => this.OR1(altsOrOpts),
9728
+ OR2: (altsOrOpts) => this.OR2(altsOrOpts),
9729
+ OR3: (altsOrOpts) => this.OR3(altsOrOpts),
9730
+ OR4: (altsOrOpts) => this.OR4(altsOrOpts),
9731
+ OR5: (altsOrOpts) => this.OR5(altsOrOpts),
9732
+ OR6: (altsOrOpts) => this.OR6(altsOrOpts),
9733
+ OR7: (altsOrOpts) => this.OR7(altsOrOpts),
9734
+ OR8: (altsOrOpts) => this.OR8(altsOrOpts),
9735
+ OR9: (altsOrOpts) => this.OR9(altsOrOpts),
9736
+ MANY: (actionORMethodDef) => this.MANY(actionORMethodDef),
9737
+ MANY1: (actionORMethodDef) => this.MANY1(actionORMethodDef),
9738
+ MANY2: (actionORMethodDef) => this.MANY2(actionORMethodDef),
9739
+ MANY3: (actionORMethodDef) => this.MANY3(actionORMethodDef),
9740
+ MANY4: (actionORMethodDef) => this.MANY4(actionORMethodDef),
9741
+ MANY5: (actionORMethodDef) => this.MANY5(actionORMethodDef),
9742
+ MANY6: (actionORMethodDef) => this.MANY6(actionORMethodDef),
9743
+ MANY7: (actionORMethodDef) => this.MANY7(actionORMethodDef),
9744
+ MANY8: (actionORMethodDef) => this.MANY8(actionORMethodDef),
9745
+ MANY9: (actionORMethodDef) => this.MANY9(actionORMethodDef),
9746
+ MANY_SEP: (options) => this.MANY_SEP(options),
9747
+ MANY_SEP1: (options) => this.MANY_SEP1(options),
9748
+ MANY_SEP2: (options) => this.MANY_SEP2(options),
9749
+ MANY_SEP3: (options) => this.MANY_SEP3(options),
9750
+ MANY_SEP4: (options) => this.MANY_SEP4(options),
9751
+ MANY_SEP5: (options) => this.MANY_SEP5(options),
9752
+ MANY_SEP6: (options) => this.MANY_SEP6(options),
9753
+ MANY_SEP7: (options) => this.MANY_SEP7(options),
9754
+ MANY_SEP8: (options) => this.MANY_SEP8(options),
9755
+ MANY_SEP9: (options) => this.MANY_SEP9(options),
9756
+ AT_LEAST_ONE: (actionORMethodDef) => this.AT_LEAST_ONE(actionORMethodDef),
9757
+ AT_LEAST_ONE1: (actionORMethodDef) => this.AT_LEAST_ONE1(actionORMethodDef),
9758
+ AT_LEAST_ONE2: (actionORMethodDef) => this.AT_LEAST_ONE2(actionORMethodDef),
9759
+ AT_LEAST_ONE3: (actionORMethodDef) => this.AT_LEAST_ONE3(actionORMethodDef),
9760
+ AT_LEAST_ONE4: (actionORMethodDef) => this.AT_LEAST_ONE4(actionORMethodDef),
9761
+ AT_LEAST_ONE5: (actionORMethodDef) => this.AT_LEAST_ONE5(actionORMethodDef),
9762
+ AT_LEAST_ONE6: (actionORMethodDef) => this.AT_LEAST_ONE6(actionORMethodDef),
9763
+ AT_LEAST_ONE7: (actionORMethodDef) => this.AT_LEAST_ONE7(actionORMethodDef),
9764
+ AT_LEAST_ONE8: (actionORMethodDef) => this.AT_LEAST_ONE8(actionORMethodDef),
9765
+ AT_LEAST_ONE9: (actionORMethodDef) => this.AT_LEAST_ONE9(actionORMethodDef),
9766
+ AT_LEAST_ONE_SEP: (options) => this.AT_LEAST_ONE_SEP(options),
9767
+ AT_LEAST_ONE_SEP1: (options) => this.AT_LEAST_ONE_SEP1(options),
9768
+ AT_LEAST_ONE_SEP2: (options) => this.AT_LEAST_ONE_SEP2(options),
9769
+ AT_LEAST_ONE_SEP3: (options) => this.AT_LEAST_ONE_SEP3(options),
9770
+ AT_LEAST_ONE_SEP4: (options) => this.AT_LEAST_ONE_SEP4(options),
9771
+ AT_LEAST_ONE_SEP5: (options) => this.AT_LEAST_ONE_SEP5(options),
9772
+ AT_LEAST_ONE_SEP6: (options) => this.AT_LEAST_ONE_SEP6(options),
9773
+ AT_LEAST_ONE_SEP7: (options) => this.AT_LEAST_ONE_SEP7(options),
9774
+ AT_LEAST_ONE_SEP8: (options) => this.AT_LEAST_ONE_SEP8(options),
9775
+ AT_LEAST_ONE_SEP9: (options) => this.AT_LEAST_ONE_SEP9(options),
9776
+ ACTION: (func) => this.ACTION(func),
9777
+ BACKTRACK: (cstDef, ...args) => this.BACKTRACK(this[cstDef.name], { ARGS: args }),
9778
+ SUBRULE: subRuleImpl((rule, args) => this.SUBRULE(rule, args)),
9779
+ SUBRULE1: subRuleImpl((rule, args) => this.SUBRULE1(rule, args)),
9780
+ SUBRULE2: subRuleImpl((rule, args) => this.SUBRULE2(rule, args)),
9781
+ SUBRULE3: subRuleImpl((rule, args) => this.SUBRULE3(rule, args)),
9782
+ SUBRULE4: subRuleImpl((rule, args) => this.SUBRULE4(rule, args)),
9783
+ SUBRULE5: subRuleImpl((rule, args) => this.SUBRULE5(rule, args)),
9784
+ SUBRULE6: subRuleImpl((rule, args) => this.SUBRULE6(rule, args)),
9785
+ SUBRULE7: subRuleImpl((rule, args) => this.SUBRULE7(rule, args)),
9786
+ SUBRULE8: subRuleImpl((rule, args) => this.SUBRULE8(rule, args)),
9787
+ SUBRULE9: subRuleImpl((rule, args) => this.SUBRULE9(rule, args))
9788
+ };
9789
+ }
9790
+ };
9791
+
9792
+ // lib/parser-builder/parserBuilder.ts
9793
+ function listToRuleDefMap2(rules) {
9253
9794
  const newRules = {};
9254
9795
  for (const rule of rules) {
9255
9796
  newRules[rule.name] = rule;
9256
9797
  }
9257
9798
  return newRules;
9258
9799
  }
9259
- var Builder = class _Builder {
9800
+ var ParserBuilder = class _ParserBuilder {
9260
9801
  /**
9261
9802
  * Create a builder from some initial grammar rules or an existing builder.
9262
9803
  * If a builder is provided, a new copy will be created.
9263
9804
  */
9264
- static createBuilder(start) {
9265
- if (start instanceof _Builder) {
9266
- return new _Builder({ ...start.rules });
9805
+ static create(start) {
9806
+ if (start instanceof _ParserBuilder) {
9807
+ return new _ParserBuilder({ ...start.rules });
9267
9808
  }
9268
- return new _Builder(listToRuleDefMap(start));
9809
+ return new _ParserBuilder(listToRuleDefMap2(start));
9269
9810
  }
9270
9811
  constructor(startRules) {
9271
9812
  this.rules = startRules;
9272
9813
  }
9814
+ widenContext() {
9815
+ return this;
9816
+ }
9817
+ typePatch() {
9818
+ return this;
9819
+ }
9273
9820
  /**
9274
9821
  * Change the implementation of an existing grammar rule.
9275
9822
  */
@@ -9297,7 +9844,7 @@ var Builder = class _Builder {
9297
9844
  return this.addRuleRedundant(rule);
9298
9845
  }
9299
9846
  addMany(...rules) {
9300
- this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
9847
+ this.rules = { ...this.rules, ...listToRuleDefMap2(rules) };
9301
9848
  return this;
9302
9849
  }
9303
9850
  /**
@@ -9310,9 +9857,11 @@ var Builder = class _Builder {
9310
9857
  /**
9311
9858
  * Merge this grammar builder with another.
9312
9859
  * It is best to merge the bigger grammar with the smaller one.
9313
- * If the two builders both have a grammar rule with the same name, no error will be thrown case they map to the same ruledef object.
9860
+ * If the two builders both have a grammar rule with the same name,
9861
+ * no error will be thrown case they map to the same ruledef object.
9314
9862
  * If they map to a different object, an error will be thrown.
9315
- * To fix this problem, the overridingRules array should contain a rule with the same conflicting name, this rule implementation will be used.
9863
+ * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
9864
+ * this rule implementation will be used.
9316
9865
  */
9317
9866
  merge(builder, overridingRules) {
9318
9867
  const otherRules = { ...builder.rules };
@@ -9335,44 +9884,57 @@ var Builder = class _Builder {
9335
9884
  this.rules = otherRules;
9336
9885
  return this;
9337
9886
  }
9338
- consumeToParser({ tokenVocabulary, parserConfig = {}, lexerConfig = {} }) {
9339
- const lexer = new Lexer(tokenVocabulary, {
9340
- positionTracking: "onlyStart",
9887
+ defaultErrorHandler(input, errors) {
9888
+ const firstError = errors[0];
9889
+ const messageBuilder = ["Parse error"];
9890
+ const lineIdx = firstError.token.startLine;
9891
+ if (lineIdx !== void 0) {
9892
+ const errorLine = input.split("\n")[lineIdx - 1];
9893
+ messageBuilder.push(` on line ${lineIdx}
9894
+ ${errorLine}`);
9895
+ const columnIdx = firstError.token.startColumn;
9896
+ if (columnIdx !== void 0) {
9897
+ messageBuilder.push(`
9898
+ ${"-".repeat(columnIdx - 1)}^`);
9899
+ }
9900
+ }
9901
+ messageBuilder.push(`
9902
+ ${firstError.message}`);
9903
+ throw new Error(messageBuilder.join(""));
9904
+ }
9905
+ build({
9906
+ tokenVocabulary,
9907
+ parserConfig = {},
9908
+ lexerConfig = {},
9909
+ queryPreProcessor = (s) => s,
9910
+ errorHandler
9911
+ }) {
9912
+ const lexer = LexerBuilder.create().add(...tokenVocabulary).build({
9913
+ positionTracking: "full",
9341
9914
  recoveryEnabled: false,
9342
9915
  // SafeMode: true,
9343
9916
  // SkipValidations: true,
9344
- ensureOptimizations: true,
9917
+ // ensureOptimizations: true,
9345
9918
  ...lexerConfig
9346
9919
  });
9347
- const parser = this.consume({ tokenVocabulary, config: parserConfig });
9920
+ const parser = this.consume({
9921
+ tokenVocabulary,
9922
+ config: parserConfig
9923
+ });
9348
9924
  const selfSufficientParser = {};
9349
9925
  for (const rule of Object.values(this.rules)) {
9350
9926
  selfSufficientParser[rule.name] = (input, context, arg) => {
9351
- input = input.replaceAll(
9352
- /\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8})/gu,
9353
- (_, unicode4, unicode8) => {
9354
- if (unicode4) {
9355
- const charCode2 = Number.parseInt(unicode4, 16);
9356
- return String.fromCodePoint(charCode2);
9357
- }
9358
- const charCode = Number.parseInt(unicode8, 16);
9359
- if (charCode < 65535) {
9360
- return String.fromCodePoint(charCode);
9361
- }
9362
- const substractedCharCode = charCode - 65536;
9363
- return String.fromCodePoint(55296 + (substractedCharCode >> 10), 56320 + (substractedCharCode & 1023));
9364
- }
9365
- );
9366
- if (/[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/u.test(input)) {
9367
- throw new Error(`Invalid unicode codepoint of surrogate pair without corresponding codepoint`);
9368
- }
9369
- const lexResult = lexer.tokenize(input);
9927
+ const processedInput = queryPreProcessor(input);
9928
+ const lexResult = lexer.tokenize(processedInput);
9370
9929
  parser.input = lexResult.tokens;
9371
9930
  parser.setContext(context);
9372
9931
  const result = parser[rule.name](context, arg);
9373
9932
  if (parser.errors.length > 0) {
9374
- throw new Error(`Parse error on line ${parser.errors.map((x) => x.token.startLine).join(", ")}
9375
- ${parser.errors.map((x) => `${x.token.startLine}: ${x.message}`).join("\n")}`);
9933
+ if (errorHandler) {
9934
+ errorHandler(parser.errors);
9935
+ } else {
9936
+ this.defaultErrorHandler(processedInput, parser.errors);
9937
+ }
9376
9938
  }
9377
9939
  return result;
9378
9940
  };
@@ -9380,292 +9942,139 @@ ${parser.errors.map((x) => `${x.token.startLine}: ${x.message}`).join("\n")}`);
9380
9942
  return selfSufficientParser;
9381
9943
  }
9382
9944
  consume({ tokenVocabulary, config = {} }) {
9383
- const rules = this.rules;
9384
- class MyParser extends EmbeddedActionsParser {
9385
- getSafeContext() {
9386
- if (this.context === void 0) {
9387
- throw new Error("context was not correctly set");
9388
- }
9389
- return this.context;
9390
- }
9391
- setContext(context) {
9392
- this.context = context;
9393
- }
9394
- constructor() {
9395
- super(tokenVocabulary, {
9396
- // RecoveryEnabled: true,
9397
- maxLookahead: 2,
9398
- // SkipValidations: true,
9399
- ...config
9400
- });
9401
- this.context = void 0;
9402
- const selfRef = this.getSelfRef();
9403
- const implArgs = {
9404
- ...selfRef,
9405
- cache: /* @__PURE__ */ new WeakMap()
9406
- };
9407
- for (const rule of Object.values(rules)) {
9408
- this[rule.name] = this.RULE(rule.name, rule.impl(implArgs));
9409
- }
9410
- this.performSelfAnalysis();
9411
- }
9412
- getSelfRef() {
9413
- const subRuleImpl = (chevrotainSubrule) => {
9414
- return (cstDef, arg) => {
9415
- return chevrotainSubrule(this[cstDef.name], { ARGS: [this.context, arg] });
9416
- };
9417
- };
9418
- return {
9419
- CONSUME: (tokenType, option) => this.CONSUME(tokenType, option),
9420
- CONSUME1: (tokenType, option) => this.CONSUME1(tokenType, option),
9421
- CONSUME2: (tokenType, option) => this.CONSUME2(tokenType, option),
9422
- CONSUME3: (tokenType, option) => this.CONSUME3(tokenType, option),
9423
- CONSUME4: (tokenType, option) => this.CONSUME4(tokenType, option),
9424
- CONSUME5: (tokenType, option) => this.CONSUME5(tokenType, option),
9425
- CONSUME6: (tokenType, option) => this.CONSUME6(tokenType, option),
9426
- CONSUME7: (tokenType, option) => this.CONSUME7(tokenType, option),
9427
- CONSUME8: (tokenType, option) => this.CONSUME8(tokenType, option),
9428
- CONSUME9: (tokenType, option) => this.CONSUME9(tokenType, option),
9429
- OPTION: (actionORMethodDef) => this.OPTION(actionORMethodDef),
9430
- OPTION1: (actionORMethodDef) => this.OPTION1(actionORMethodDef),
9431
- OPTION2: (actionORMethodDef) => this.OPTION2(actionORMethodDef),
9432
- OPTION3: (actionORMethodDef) => this.OPTION3(actionORMethodDef),
9433
- OPTION4: (actionORMethodDef) => this.OPTION4(actionORMethodDef),
9434
- OPTION5: (actionORMethodDef) => this.OPTION5(actionORMethodDef),
9435
- OPTION6: (actionORMethodDef) => this.OPTION6(actionORMethodDef),
9436
- OPTION7: (actionORMethodDef) => this.OPTION7(actionORMethodDef),
9437
- OPTION8: (actionORMethodDef) => this.OPTION8(actionORMethodDef),
9438
- OPTION9: (actionORMethodDef) => this.OPTION9(actionORMethodDef),
9439
- OR: (altsOrOpts) => this.OR(altsOrOpts),
9440
- OR1: (altsOrOpts) => this.OR1(altsOrOpts),
9441
- OR2: (altsOrOpts) => this.OR2(altsOrOpts),
9442
- OR3: (altsOrOpts) => this.OR3(altsOrOpts),
9443
- OR4: (altsOrOpts) => this.OR4(altsOrOpts),
9444
- OR5: (altsOrOpts) => this.OR5(altsOrOpts),
9445
- OR6: (altsOrOpts) => this.OR6(altsOrOpts),
9446
- OR7: (altsOrOpts) => this.OR7(altsOrOpts),
9447
- OR8: (altsOrOpts) => this.OR8(altsOrOpts),
9448
- OR9: (altsOrOpts) => this.OR9(altsOrOpts),
9449
- MANY: (actionORMethodDef) => this.MANY(actionORMethodDef),
9450
- MANY1: (actionORMethodDef) => this.MANY1(actionORMethodDef),
9451
- MANY2: (actionORMethodDef) => this.MANY2(actionORMethodDef),
9452
- MANY3: (actionORMethodDef) => this.MANY3(actionORMethodDef),
9453
- MANY4: (actionORMethodDef) => this.MANY4(actionORMethodDef),
9454
- MANY5: (actionORMethodDef) => this.MANY5(actionORMethodDef),
9455
- MANY6: (actionORMethodDef) => this.MANY6(actionORMethodDef),
9456
- MANY7: (actionORMethodDef) => this.MANY7(actionORMethodDef),
9457
- MANY8: (actionORMethodDef) => this.MANY8(actionORMethodDef),
9458
- MANY9: (actionORMethodDef) => this.MANY9(actionORMethodDef),
9459
- MANY_SEP: (options) => this.MANY_SEP(options),
9460
- MANY_SEP1: (options) => this.MANY_SEP1(options),
9461
- MANY_SEP2: (options) => this.MANY_SEP2(options),
9462
- MANY_SEP3: (options) => this.MANY_SEP3(options),
9463
- MANY_SEP4: (options) => this.MANY_SEP4(options),
9464
- MANY_SEP5: (options) => this.MANY_SEP5(options),
9465
- MANY_SEP6: (options) => this.MANY_SEP6(options),
9466
- MANY_SEP7: (options) => this.MANY_SEP7(options),
9467
- MANY_SEP8: (options) => this.MANY_SEP8(options),
9468
- MANY_SEP9: (options) => this.MANY_SEP9(options),
9469
- AT_LEAST_ONE: (actionORMethodDef) => this.AT_LEAST_ONE(actionORMethodDef),
9470
- AT_LEAST_ONE1: (actionORMethodDef) => this.AT_LEAST_ONE1(actionORMethodDef),
9471
- AT_LEAST_ONE2: (actionORMethodDef) => this.AT_LEAST_ONE2(actionORMethodDef),
9472
- AT_LEAST_ONE3: (actionORMethodDef) => this.AT_LEAST_ONE3(actionORMethodDef),
9473
- AT_LEAST_ONE4: (actionORMethodDef) => this.AT_LEAST_ONE4(actionORMethodDef),
9474
- AT_LEAST_ONE5: (actionORMethodDef) => this.AT_LEAST_ONE5(actionORMethodDef),
9475
- AT_LEAST_ONE6: (actionORMethodDef) => this.AT_LEAST_ONE6(actionORMethodDef),
9476
- AT_LEAST_ONE7: (actionORMethodDef) => this.AT_LEAST_ONE7(actionORMethodDef),
9477
- AT_LEAST_ONE8: (actionORMethodDef) => this.AT_LEAST_ONE8(actionORMethodDef),
9478
- AT_LEAST_ONE9: (actionORMethodDef) => this.AT_LEAST_ONE9(actionORMethodDef),
9479
- AT_LEAST_ONE_SEP: (options) => this.AT_LEAST_ONE_SEP(options),
9480
- AT_LEAST_ONE_SEP1: (options) => this.AT_LEAST_ONE_SEP1(options),
9481
- AT_LEAST_ONE_SEP2: (options) => this.AT_LEAST_ONE_SEP2(options),
9482
- AT_LEAST_ONE_SEP3: (options) => this.AT_LEAST_ONE_SEP3(options),
9483
- AT_LEAST_ONE_SEP4: (options) => this.AT_LEAST_ONE_SEP4(options),
9484
- AT_LEAST_ONE_SEP5: (options) => this.AT_LEAST_ONE_SEP5(options),
9485
- AT_LEAST_ONE_SEP6: (options) => this.AT_LEAST_ONE_SEP6(options),
9486
- AT_LEAST_ONE_SEP7: (options) => this.AT_LEAST_ONE_SEP7(options),
9487
- AT_LEAST_ONE_SEP8: (options) => this.AT_LEAST_ONE_SEP8(options),
9488
- AT_LEAST_ONE_SEP9: (options) => this.AT_LEAST_ONE_SEP9(options),
9489
- ACTION: (func) => this.ACTION(func),
9490
- BACKTRACK: (cstDef, ...args) => {
9491
- try {
9492
- return this.BACKTRACK(this[cstDef.name], { ARGS: args });
9493
- } catch (error) {
9494
- throw error;
9495
- }
9496
- },
9497
- SUBRULE: subRuleImpl((rule, args) => this.SUBRULE(rule, args)),
9498
- SUBRULE1: subRuleImpl((rule, args) => this.SUBRULE1(rule, args)),
9499
- SUBRULE2: subRuleImpl((rule, args) => this.SUBRULE2(rule, args)),
9500
- SUBRULE3: subRuleImpl((rule, args) => this.SUBRULE3(rule, args)),
9501
- SUBRULE4: subRuleImpl((rule, args) => this.SUBRULE4(rule, args)),
9502
- SUBRULE5: subRuleImpl((rule, args) => this.SUBRULE5(rule, args)),
9503
- SUBRULE6: subRuleImpl((rule, args) => this.SUBRULE6(rule, args)),
9504
- SUBRULE7: subRuleImpl((rule, args) => this.SUBRULE7(rule, args)),
9505
- SUBRULE8: subRuleImpl((rule, args) => this.SUBRULE8(rule, args)),
9506
- SUBRULE9: subRuleImpl((rule, args) => this.SUBRULE9(rule, args))
9507
- };
9508
- }
9509
- }
9510
- return new MyParser();
9945
+ return new DynamicParser(this.rules, tokenVocabulary, config);
9511
9946
  }
9512
9947
  };
9513
9948
 
9514
- // lib/grammar-helpers/utils.ts
9515
- var CommonIRIs = /* @__PURE__ */ ((CommonIRIs2) => {
9516
- CommonIRIs2["BOOLEAN"] = "http://www.w3.org/2001/XMLSchema#boolean";
9517
- CommonIRIs2["INTEGER"] = "http://www.w3.org/2001/XMLSchema#integer";
9518
- CommonIRIs2["DECIMAL"] = "http://www.w3.org/2001/XMLSchema#decimal";
9519
- CommonIRIs2["DOUBLE"] = "http://www.w3.org/2001/XMLSchema#double";
9520
- CommonIRIs2["FIRST"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first";
9521
- CommonIRIs2["REST"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";
9522
- CommonIRIs2["NIL"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";
9523
- CommonIRIs2["TYPE"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
9524
- CommonIRIs2["REIFIES"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies";
9525
- return CommonIRIs2;
9526
- })(CommonIRIs || {});
9949
+ // lib/utils.ts
9527
9950
  function unCapitalize(str) {
9528
9951
  return str.charAt(0).toLowerCase() + str.slice(1);
9529
9952
  }
9530
- function resolveIRI(iri, base) {
9531
- if (/^[a-z][\d+.a-z-]*:/iu.test(iri)) {
9532
- return iri;
9533
- }
9534
- if (!base) {
9535
- throw new Error(`Cannot resolve relative IRI ${iri} because no base IRI was set.`);
9536
- }
9537
- switch (iri[0]) {
9538
- // An empty relative IRI indicates the base IRI
9539
- case void 0:
9540
- return base;
9541
- // Resolve relative fragment IRIs against the base IRI
9542
- case "#":
9543
- return base + iri;
9544
- // Resolve relative query string IRIs by replacing the query string
9545
- case "?":
9546
- return base.replace(/(?:\?.*)?$/u, iri);
9547
- // Resolve root relative IRIs at the root of the base IRI
9548
- case "/":
9549
- const baseMatch = base.match(/^(?:[a-z]+:\/*)?[^\/]*/);
9550
- if (!baseMatch) {
9551
- throw new Error(`Could not determine relative IRI using base: ${base}`);
9552
- }
9553
- const baseRoot = baseMatch[0];
9554
- return baseRoot + iri;
9555
- // Resolve all other IRIs at the base IRI's path
9556
- default:
9557
- const basePath = base.replace(/[^\/:]*$/, "");
9558
- return basePath + iri;
9559
- }
9953
+ function createToken2(config) {
9954
+ return createToken(config);
9560
9955
  }
9561
9956
 
9562
- // lib/lexer-builder/LexerBuilder.ts
9563
- var LexerBuilder = class _LexerBuilder {
9564
- static create(starter) {
9565
- return new _LexerBuilder(starter);
9566
- }
9567
- constructor(starter) {
9568
- this.tokens = starter?.tokens ? [...starter.tokens] : [];
9957
+ // lib/RangeArithmetic.ts
9958
+ var RangeArithmetic = class _RangeArithmetic {
9959
+ constructor(start, end) {
9960
+ this.ranges = [];
9961
+ this.initRange = _RangeArithmetic.validate(start, end);
9962
+ this.ranges.push(this.initRange);
9569
9963
  }
9570
- get(index) {
9571
- return this.tokens[index];
9964
+ static validate(...range) {
9965
+ const [start, end] = range;
9966
+ if (start >= end) {
9967
+ throw new Error("Invalid range");
9968
+ }
9969
+ return range;
9572
9970
  }
9573
- merge(merge, overwrite = []) {
9574
- const extraTokens = merge.tokens.filter((token) => {
9575
- const overwriteToken = overwrite.find((t) => t.name === token.name);
9576
- if (overwriteToken) {
9577
- return false;
9971
+ static substractRange(included, ...range) {
9972
+ const [sMinus, eMinus] = _RangeArithmetic.validate(...range);
9973
+ return included.flatMap(([sCur, eCur]) => {
9974
+ if (sCur < sMinus && eMinus < eCur) {
9975
+ return [[sCur, sMinus], [eMinus, eCur]];
9578
9976
  }
9579
- const match = this.tokens.find((t) => t.name === token.name);
9580
- if (match) {
9581
- if (match !== token) {
9582
- throw new Error(`Token with name ${token.name} already exists. Implementation is different and no overwrite was provided.`);
9583
- }
9584
- return false;
9977
+ if (sMinus <= sCur && sCur < eMinus && eMinus < eCur) {
9978
+ return [[eMinus, eCur]];
9585
9979
  }
9586
- return true;
9980
+ if (sCur < sMinus && sMinus < eCur && eCur <= eMinus) {
9981
+ return [[sCur, sMinus]];
9982
+ }
9983
+ if (sMinus <= sCur && eCur <= eMinus) {
9984
+ return [];
9985
+ }
9986
+ return [[sCur, eCur]];
9587
9987
  });
9588
- this.tokens.push(...extraTokens);
9589
- return this;
9590
9988
  }
9591
- add(...token) {
9592
- this.tokens.push(...token);
9989
+ subtract(...range) {
9990
+ this.ranges = _RangeArithmetic.substractRange(this.ranges, ...range);
9593
9991
  return this;
9594
9992
  }
9595
- addBefore(before, ...token) {
9596
- const index = this.tokens.indexOf(before);
9597
- if (index === -1) {
9598
- throw new Error("Token not found");
9993
+ negate() {
9994
+ let iter = [this.initRange];
9995
+ for (const range of this.ranges) {
9996
+ iter = _RangeArithmetic.substractRange(iter, ...range);
9599
9997
  }
9600
- this.tokens.splice(index, 0, ...token);
9998
+ this.ranges = iter;
9601
9999
  return this;
9602
10000
  }
9603
- moveBeforeOrAfter(beforeOrAfter, before, ...tokens) {
9604
- const beforeIndex = this.tokens.indexOf(before) + (beforeOrAfter === "before" ? 0 : 1);
9605
- if (beforeIndex === -1) {
9606
- throw new Error("BeforeToken not found");
10001
+ projection(...range) {
10002
+ const [sProj, eProj] = range;
10003
+ if (sProj >= eProj) {
10004
+ return [];
9607
10005
  }
9608
- for (const token of tokens) {
9609
- const tokenIndex = this.tokens.indexOf(token);
9610
- if (tokenIndex === -1) {
9611
- throw new Error("Token not found");
10006
+ return this.ranges.flatMap(([sCur, eCur]) => {
10007
+ if (sCur < sProj && eProj < eCur) {
10008
+ return [[sProj, eProj]];
9612
10009
  }
9613
- this.tokens.splice(tokenIndex, 1);
9614
- this.tokens.splice(beforeIndex, 0, token);
9615
- }
9616
- return this;
9617
- }
9618
- moveBefore(before, ...tokens) {
9619
- return this.moveBeforeOrAfter("before", before, ...tokens);
9620
- }
9621
- moveAfter(after, ...tokens) {
9622
- return this.moveBeforeOrAfter("after", after, ...tokens);
10010
+ if (sProj <= sCur && eCur <= eProj) {
10011
+ return [[sCur, eCur]];
10012
+ }
10013
+ if (sProj <= sCur && sCur < eProj && eProj < eCur) {
10014
+ return [[sCur, eProj]];
10015
+ }
10016
+ if (sCur < sProj && sProj < eCur && eCur < eProj) {
10017
+ return [[sProj, eCur]];
10018
+ }
10019
+ return [];
10020
+ });
9623
10021
  }
9624
- addAfter(after, ...token) {
9625
- const index = this.tokens.indexOf(after);
9626
- if (index === -1) {
9627
- throw new Error("Token not found");
10022
+ };
10023
+
10024
+ // lib/Transformer.ts
10025
+ var Transformer = class {
10026
+ transformNode(curObject, searchType, patch) {
10027
+ const copy = { ...curObject };
10028
+ for (const [key, value] of Object.entries(copy)) {
10029
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, searchType, patch));
9628
10030
  }
9629
- this.tokens.splice(index + 1, 0, ...token);
9630
- return this;
10031
+ if (copy.type === searchType) {
10032
+ return patch(copy);
10033
+ }
10034
+ return copy;
9631
10035
  }
9632
- delete(...token) {
9633
- for (const t of token) {
9634
- const index = this.tokens.indexOf(t);
9635
- if (index === -1) {
9636
- throw new Error("Token not found");
9637
- }
9638
- this.tokens.splice(index, 1);
10036
+ transformNodeSpecific(curObject, searchType, searchSubType, patch) {
10037
+ const copy = { ...curObject };
10038
+ for (const [key, value] of Object.entries(copy)) {
10039
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, searchType, searchSubType, patch));
9639
10040
  }
9640
- return this;
10041
+ if (copy.type === searchType && copy.subType === searchSubType) {
10042
+ return patch(copy);
10043
+ }
10044
+ return copy;
9641
10045
  }
9642
- build() {
9643
- return this.tokens;
10046
+ visitObjects(curObject, visitor) {
10047
+ for (const value of Object.values(curObject)) {
10048
+ this.safeObjectVisit(value, (obj) => this.visitObjects(obj, visitor));
10049
+ }
9644
10050
  }
9645
- };
9646
-
9647
- // lib/lexer-helper/utils.ts
9648
- function createToken2(config) {
9649
- return createToken(config);
9650
- }
9651
-
9652
- // lib/Wildcard.ts
9653
- var Wildcard = class {
9654
- constructor() {
9655
- this.value = "*";
9656
- this.termType = "Wildcard";
9657
- return WILDCARD ?? this;
10051
+ visitNode(curObject, searchType, visitor) {
10052
+ for (const value of Object.values(curObject)) {
10053
+ this.safeObjectVisit(value, (obj) => this.visitNode(obj, searchType, visitor));
10054
+ }
10055
+ if (curObject.type === searchType) {
10056
+ visitor(curObject);
10057
+ }
9658
10058
  }
9659
- equals(other) {
9660
- return Boolean(other && this.termType === other.termType);
10059
+ visitNodeSpecific(curObject, searchType, searchSubType, visitor) {
10060
+ for (const value of Object.values(curObject)) {
10061
+ this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, searchType, searchSubType, visitor));
10062
+ }
10063
+ const cast = curObject;
10064
+ if (cast.type === searchType && cast.subType === searchSubType) {
10065
+ visitor(curObject);
10066
+ }
9661
10067
  }
9662
- toJSON() {
9663
- const { value, ...rest } = this;
9664
- return rest;
10068
+ safeObjectVisit(value, mapper) {
10069
+ if (value && typeof value === "object") {
10070
+ if (Array.isArray(value)) {
10071
+ return value.map((x) => this.safeObjectVisit(x, mapper));
10072
+ }
10073
+ return mapper(value);
10074
+ }
10075
+ return value;
9665
10076
  }
9666
10077
  };
9667
- var WILDCARD;
9668
- WILDCARD = new Wildcard();
9669
10078
  /*! Bundled license information:
9670
10079
 
9671
10080
  lodash-es/lodash.js: