@traqula/core 0.0.15 → 0.0.17

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.
package/lib/index.cjs CHANGED
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // lib/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- CoreFactory: () => CoreFactory,
23
+ AstCoreFactory: () => AstCoreFactory,
24
24
  GeneratorBuilder: () => GeneratorBuilder,
25
25
  IndirBuilder: () => IndirBuilder,
26
26
  LexerBuilder: () => LexerBuilder,
@@ -29,12 +29,13 @@ __export(index_exports, {
29
29
  TransformerType: () => TransformerType,
30
30
  createToken: () => createToken2,
31
31
  listToIndirectionMap: () => listToIndirectionMap,
32
+ traqulaIndentation: () => traqulaIndentation,
32
33
  unCapitalize: () => unCapitalize
33
34
  });
34
35
  module.exports = __toCommonJS(index_exports);
35
36
 
36
- // lib/CoreFactory.js
37
- var CoreFactory = class {
37
+ // lib/AstCoreFactory.js
38
+ var AstCoreFactory = class {
38
39
  wrap(val, loc) {
39
40
  return { val, loc };
40
41
  }
@@ -160,313 +161,6 @@ var CoreFactory = class {
160
161
  }
161
162
  };
162
163
 
163
- // lib/generator-builder/dynamicGenerator.js
164
- var DynamicGenerator = class {
165
- rules;
166
- factory = new CoreFactory();
167
- __context = void 0;
168
- origSource = "";
169
- generatedUntil = 0;
170
- expectsSpace;
171
- stringBuilder = [];
172
- constructor(rules) {
173
- this.rules = rules;
174
- for (const rule of Object.values(rules)) {
175
- this[rule.name] = ((input, context, args) => {
176
- this.expectsSpace = false;
177
- this.stringBuilder.length = 0;
178
- this.origSource = context.origSource;
179
- this.generatedUntil = context?.offset ?? 0;
180
- this.setContext(context);
181
- this.subrule(rule, input, args);
182
- this.catchup(this.origSource.length);
183
- return this.stringBuilder.join("");
184
- });
185
- }
186
- }
187
- setContext(context) {
188
- this.__context = context;
189
- }
190
- getSafeContext() {
191
- return this.__context;
192
- }
193
- subrule = (cstDef, ast, ...arg) => {
194
- const def = this.rules[cstDef.name];
195
- if (!def) {
196
- throw new Error(`Rule ${cstDef.name} not found`);
197
- }
198
- const generate = () => def.gImpl({
199
- SUBRULE: this.subrule,
200
- PRINT: this.print,
201
- PRINT_SPACE_LEFT: this.printSpaceLeft,
202
- PRINT_WORD: this.printWord,
203
- PRINT_WORDS: this.printWords,
204
- CATCHUP: this.catchup,
205
- HANDLE_LOC: this.handleLoc
206
- })(ast, this.getSafeContext(), ...arg);
207
- if (this.factory.isLocalized(ast)) {
208
- this.handleLoc(ast, generate);
209
- } else {
210
- generate();
211
- }
212
- };
213
- handleLoc = (localized, handle) => {
214
- if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
215
- return;
216
- }
217
- if (this.factory.isSourceLocationStringReplace(localized.loc)) {
218
- this.catchup(localized.loc.start);
219
- this.print(localized.loc.newSource);
220
- this.generatedUntil = localized.loc.end;
221
- return;
222
- }
223
- if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
224
- this.catchup(localized.loc.start);
225
- this.generatedUntil = localized.loc.end;
226
- }
227
- if (this.factory.isSourceLocationSource(localized.loc)) {
228
- this.catchup(localized.loc.start);
229
- }
230
- const ret = handle();
231
- if (this.factory.isSourceLocationSource(localized.loc)) {
232
- this.catchup(localized.loc.end);
233
- }
234
- return ret;
235
- };
236
- catchup = (until) => {
237
- const start = this.generatedUntil;
238
- if (start < until) {
239
- this.print(this.origSource.slice(start, until));
240
- }
241
- this.generatedUntil = Math.max(this.generatedUntil, until);
242
- };
243
- print = (...args) => {
244
- const pureArgs = args.filter((x) => x.length > 0);
245
- if (pureArgs.length > 0) {
246
- const [head2, ...tail] = pureArgs;
247
- if (this.expectsSpace) {
248
- this.expectsSpace = false;
249
- const blanks = /* @__PURE__ */ new Set(["\n", " ", " "]);
250
- if (this.stringBuilder.length > 0 && !(blanks.has(head2[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
251
- this.stringBuilder.push(" ");
252
- }
253
- }
254
- this.stringBuilder.push(head2);
255
- this.stringBuilder.push(...tail);
256
- }
257
- };
258
- printWord = (...args) => {
259
- this.expectsSpace = true;
260
- this.print(...args);
261
- this.expectsSpace = true;
262
- };
263
- printSpaceLeft = (...args) => {
264
- this.expectsSpace = true;
265
- this.print(...args);
266
- };
267
- printWords = (...args) => {
268
- for (const arg of args) {
269
- this.printWord(arg);
270
- }
271
- };
272
- };
273
-
274
- // lib/generator-builder/generatorBuilder.js
275
- function listToRuleDefMap(rules) {
276
- const newRules = {};
277
- for (const rule of rules) {
278
- newRules[rule.name] = rule;
279
- }
280
- return newRules;
281
- }
282
- var GeneratorBuilder = class _GeneratorBuilder {
283
- static create(start) {
284
- if (start instanceof _GeneratorBuilder) {
285
- return new _GeneratorBuilder({ ...start.rules });
286
- }
287
- return new _GeneratorBuilder(listToRuleDefMap(start));
288
- }
289
- rules;
290
- constructor(startRules) {
291
- this.rules = startRules;
292
- }
293
- widenContext() {
294
- return this;
295
- }
296
- typePatch() {
297
- return this;
298
- }
299
- /**
300
- * Change the implementation of an existing generator rule.
301
- */
302
- patchRule(patch) {
303
- const self2 = this;
304
- self2.rules[patch.name] = patch;
305
- return self2;
306
- }
307
- /**
308
- * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
309
- */
310
- addRuleRedundant(rule) {
311
- const self2 = this;
312
- const rules = self2.rules;
313
- if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
314
- throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);
315
- }
316
- rules[rule.name] = rule;
317
- return self2;
318
- }
319
- /**
320
- * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
321
- */
322
- addRule(rule) {
323
- return this.addRuleRedundant(rule);
324
- }
325
- addMany(...rules) {
326
- this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
327
- return this;
328
- }
329
- /**
330
- * Delete a grammar rule by its name.
331
- */
332
- deleteRule(ruleName) {
333
- delete this.rules[ruleName];
334
- return this;
335
- }
336
- /**
337
- * Merge this grammar GeneratorBuilder with another.
338
- * It is best to merge the bigger grammar with the smaller one.
339
- * If the two builders both have a grammar rule with the same name,
340
- * no error will be thrown case they map to the same ruledef object.
341
- * If they map to a different object, an error will be thrown.
342
- * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
343
- * this rule implementation will be used.
344
- */
345
- merge(GeneratorBuilder2, overridingRules) {
346
- const otherRules = { ...GeneratorBuilder2.rules };
347
- const myRules = this.rules;
348
- for (const rule of Object.values(myRules)) {
349
- if (otherRules[rule.name] === void 0) {
350
- otherRules[rule.name] = rule;
351
- } else {
352
- const existingRule = otherRules[rule.name];
353
- if (existingRule !== rule) {
354
- const override = overridingRules.find((x) => x.name === rule.name);
355
- if (override) {
356
- otherRules[rule.name] = override;
357
- } else {
358
- throw new Error(`Rule with name "${rule.name}" already exists in the GeneratorBuilder, specify an override to resolve conflict`);
359
- }
360
- }
361
- }
362
- }
363
- this.rules = otherRules;
364
- return this;
365
- }
366
- build() {
367
- return new DynamicGenerator(this.rules);
368
- }
369
- };
370
-
371
- // lib/indirection-builder/helpers.js
372
- function listToIndirectionMap(rules) {
373
- const newRules = {};
374
- for (const rule of rules) {
375
- newRules[rule.name] = rule;
376
- }
377
- return newRules;
378
- }
379
-
380
- // lib/indirection-builder/dynamicIndirected.js
381
- var DynamicIndirect = class {
382
- rules;
383
- __context = void 0;
384
- constructor(rules) {
385
- this.rules = rules;
386
- for (const rule of Object.values(rules)) {
387
- this[rule.name] = ((context, ...args) => {
388
- this.setContext(context);
389
- return this.subrule(rule, ...args);
390
- });
391
- }
392
- }
393
- setContext(context) {
394
- this.__context = context;
395
- }
396
- getSafeContext() {
397
- return this.__context;
398
- }
399
- subrule = (cstDef, ...args) => {
400
- const def = this.rules[cstDef.name];
401
- if (!def) {
402
- throw new Error(`Rule ${cstDef.name} not found`);
403
- }
404
- return def.fun({
405
- SUBRULE: this.subrule
406
- })(this.getSafeContext(), ...args);
407
- };
408
- };
409
-
410
- // lib/indirection-builder/IndirBuilder.js
411
- var IndirBuilder = class _IndirBuilder {
412
- static create(start) {
413
- if (start instanceof _IndirBuilder) {
414
- return new _IndirBuilder({ ...start.rules });
415
- }
416
- return new _IndirBuilder(listToIndirectionMap(start));
417
- }
418
- rules;
419
- constructor(startRules) {
420
- this.rules = startRules;
421
- }
422
- widenContext() {
423
- return this;
424
- }
425
- typePatch() {
426
- return this;
427
- }
428
- /**
429
- * Change the implementation of an existing indirection.
430
- */
431
- patchRule(patch) {
432
- const self2 = this;
433
- self2.rules[patch.name] = patch;
434
- return self2;
435
- }
436
- /**
437
- * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
438
- */
439
- addRuleRedundant(rule) {
440
- const self2 = this;
441
- const rules = self2.rules;
442
- if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
443
- throw new Error(`Function ${rule.name} already exists in the builder`);
444
- }
445
- rules[rule.name] = rule;
446
- return self2;
447
- }
448
- /**
449
- * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
450
- */
451
- addRule(rule) {
452
- return this.addRuleRedundant(rule);
453
- }
454
- addMany(...rules) {
455
- this.rules = { ...this.rules, ...listToIndirectionMap(rules) };
456
- return this;
457
- }
458
- /**
459
- * Delete a grammar rule by its name.
460
- */
461
- deleteRule(ruleName) {
462
- delete this.rules[ruleName];
463
- return this;
464
- }
465
- build() {
466
- return new DynamicIndirect(this.rules);
467
- }
468
- };
469
-
470
164
  // ../../node_modules/lodash-es/_freeGlobal.js
471
165
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
472
166
  var freeGlobal_default = freeGlobal;
@@ -9630,58 +9324,413 @@ var Parser = class _Parser {
9630
9324
  this.resyncFollows = allFollows;
9631
9325
  });
9632
9326
  }
9633
- this.TRACE_INIT("ComputeLookaheadFunctions", () => {
9634
- var _a, _b;
9635
- (_b = (_a = this.lookaheadStrategy).initialize) === null || _b === void 0 ? void 0 : _b.call(_a, {
9636
- rules: values_default(this.gastProductionsCache)
9637
- });
9638
- this.preComputeLookaheadFunctions(values_default(this.gastProductionsCache));
9639
- });
9327
+ this.TRACE_INIT("ComputeLookaheadFunctions", () => {
9328
+ var _a, _b;
9329
+ (_b = (_a = this.lookaheadStrategy).initialize) === null || _b === void 0 ? void 0 : _b.call(_a, {
9330
+ rules: values_default(this.gastProductionsCache)
9331
+ });
9332
+ this.preComputeLookaheadFunctions(values_default(this.gastProductionsCache));
9333
+ });
9334
+ }
9335
+ if (!_Parser.DEFER_DEFINITION_ERRORS_HANDLING && !isEmpty_default(this.definitionErrors)) {
9336
+ defErrorsMsgs = map_default(this.definitionErrors, (defError) => defError.message);
9337
+ throw new Error(`Parser Definition Errors detected:
9338
+ ${defErrorsMsgs.join("\n-------------------------------\n")}`);
9339
+ }
9340
+ });
9341
+ }
9342
+ constructor(tokenVocabulary, config) {
9343
+ this.definitionErrors = [];
9344
+ this.selfAnalysisDone = false;
9345
+ const that = this;
9346
+ that.initErrorHandler(config);
9347
+ that.initLexerAdapter();
9348
+ that.initLooksAhead(config);
9349
+ that.initRecognizerEngine(tokenVocabulary, config);
9350
+ that.initRecoverable(config);
9351
+ that.initTreeBuilder(config);
9352
+ that.initContentAssist();
9353
+ that.initGastRecorder(config);
9354
+ that.initPerformanceTracer(config);
9355
+ if (has_default(config, "ignoredIssues")) {
9356
+ throw new Error("The <ignoredIssues> IParserConfig property has been deprecated.\n Please use the <IGNORE_AMBIGUITIES> flag on the relevant DSL method instead.\n See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#IGNORING_AMBIGUITIES\n For further details.");
9357
+ }
9358
+ this.skipValidations = has_default(config, "skipValidations") ? config.skipValidations : DEFAULT_PARSER_CONFIG.skipValidations;
9359
+ }
9360
+ };
9361
+ Parser.DEFER_DEFINITION_ERRORS_HANDLING = false;
9362
+ applyMixins(Parser, [
9363
+ Recoverable,
9364
+ LooksAhead,
9365
+ TreeBuilder,
9366
+ LexerAdapter,
9367
+ RecognizerEngine,
9368
+ RecognizerApi,
9369
+ ErrorHandler,
9370
+ ContentAssist,
9371
+ GastRecorder,
9372
+ PerformanceTracer
9373
+ ]);
9374
+ var EmbeddedActionsParser = class extends Parser {
9375
+ constructor(tokenVocabulary, config = DEFAULT_PARSER_CONFIG) {
9376
+ const configClone = clone_default(config);
9377
+ configClone.outputCst = false;
9378
+ super(tokenVocabulary, configClone);
9379
+ }
9380
+ };
9381
+
9382
+ // lib/utils.js
9383
+ function unCapitalize(str) {
9384
+ return str.charAt(0).toLowerCase() + str.slice(1);
9385
+ }
9386
+ function createToken2(config) {
9387
+ return createToken(config);
9388
+ }
9389
+ var traqulaIndentation = "When you use this string, you expect traqula to handle indentation after every newline";
9390
+
9391
+ // lib/generator-builder/dynamicGenerator.js
9392
+ var DynamicGenerator = class {
9393
+ rules;
9394
+ factory = new AstCoreFactory();
9395
+ __context = void 0;
9396
+ origSource = "";
9397
+ generatedUntil = 0;
9398
+ expectsSpace;
9399
+ stringBuilder = [];
9400
+ constructor(rules) {
9401
+ this.rules = rules;
9402
+ for (const rule of Object.values(rules)) {
9403
+ this[rule.name] = ((input, context, args) => {
9404
+ this.expectsSpace = false;
9405
+ this.stringBuilder.length = 0;
9406
+ this.origSource = context.origSource;
9407
+ this.generatedUntil = context?.offset ?? 0;
9408
+ this.setContext(context);
9409
+ this.subrule(rule, input, args);
9410
+ this.catchup(this.origSource.length);
9411
+ return this.stringBuilder.join("");
9412
+ });
9413
+ }
9414
+ }
9415
+ setContext(context) {
9416
+ this.__context = context;
9417
+ }
9418
+ getSafeContext() {
9419
+ return this.__context;
9420
+ }
9421
+ subrule = (cstDef, ast, ...arg) => {
9422
+ const def = this.rules[cstDef.name];
9423
+ if (!def) {
9424
+ throw new Error(`Rule ${cstDef.name} not found`);
9425
+ }
9426
+ const generate = () => def.gImpl({
9427
+ SUBRULE: this.subrule,
9428
+ PRINT: this.print,
9429
+ PRINT_SPACE_LEFT: this.printSpaceLeft,
9430
+ PRINT_WORD: this.printWord,
9431
+ PRINT_WORDS: this.printWords,
9432
+ PRINT_ON_EMPTY: this.printOnEmpty,
9433
+ CATCHUP: this.catchup,
9434
+ HANDLE_LOC: this.handleLoc
9435
+ })(ast, this.getSafeContext(), ...arg);
9436
+ if (this.factory.isLocalized(ast)) {
9437
+ this.handleLoc(ast, generate);
9438
+ } else {
9439
+ generate();
9440
+ }
9441
+ };
9442
+ handleLoc = (localized, handle) => {
9443
+ if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
9444
+ return;
9445
+ }
9446
+ if (this.factory.isSourceLocationStringReplace(localized.loc)) {
9447
+ this.catchup(localized.loc.start);
9448
+ this.print(localized.loc.newSource);
9449
+ this.generatedUntil = localized.loc.end;
9450
+ return;
9451
+ }
9452
+ if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
9453
+ this.catchup(localized.loc.start);
9454
+ this.generatedUntil = localized.loc.end;
9455
+ }
9456
+ if (this.factory.isSourceLocationSource(localized.loc)) {
9457
+ this.catchup(localized.loc.start);
9458
+ }
9459
+ const ret = handle();
9460
+ if (this.factory.isSourceLocationSource(localized.loc)) {
9461
+ this.catchup(localized.loc.end);
9462
+ }
9463
+ return ret;
9464
+ };
9465
+ catchup = (until) => {
9466
+ const start = this.generatedUntil;
9467
+ if (start < until) {
9468
+ this.print(this.origSource.slice(start, until));
9469
+ }
9470
+ this.generatedUntil = Math.max(this.generatedUntil, until);
9471
+ };
9472
+ print = (...args) => {
9473
+ const pureArgs = args.filter((x) => x.length > 0);
9474
+ if (pureArgs.length > 0) {
9475
+ const head2 = pureArgs[0];
9476
+ if (this.expectsSpace) {
9477
+ this.expectsSpace = false;
9478
+ const blanks = /* @__PURE__ */ new Set(["\n", " ", " "]);
9479
+ if (this.stringBuilder.length > 0 && !(blanks.has(head2[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
9480
+ this.stringBuilder.push(" ");
9481
+ }
9482
+ }
9483
+ const context = this.getSafeContext();
9484
+ if (context[traqulaIndentation] && typeof context[traqulaIndentation] === "number") {
9485
+ const indent = context[traqulaIndentation];
9486
+ for (const str of pureArgs) {
9487
+ const [noNl, ...postNl] = str.split("\n");
9488
+ this.stringBuilder.push(noNl);
9489
+ for (const subStr of postNl.map((line) => line.trimStart())) {
9490
+ this.stringBuilder.push("\n", " ".repeat(indent));
9491
+ if (subStr.length > 0) {
9492
+ this.stringBuilder.push(subStr);
9493
+ }
9494
+ }
9495
+ }
9496
+ } else {
9497
+ this.stringBuilder.push(...pureArgs);
9498
+ }
9499
+ }
9500
+ };
9501
+ printWord = (...args) => {
9502
+ this.expectsSpace = true;
9503
+ this.print(...args);
9504
+ this.expectsSpace = true;
9505
+ };
9506
+ printSpaceLeft = (...args) => {
9507
+ this.expectsSpace = true;
9508
+ this.print(...args);
9509
+ };
9510
+ printWords = (...args) => {
9511
+ for (const arg of args) {
9512
+ this.printWord(arg);
9513
+ }
9514
+ };
9515
+ printOnEmpty = (...args) => {
9516
+ let counter = this.stringBuilder.length - 1;
9517
+ let onEmpty = true;
9518
+ const isEmptyTest = /^[ \t\n]*$/u;
9519
+ while (counter >= 0 && onEmpty) {
9520
+ const cur = this.stringBuilder[counter];
9521
+ const indexOfNl = cur.lastIndexOf("\n");
9522
+ if (indexOfNl >= 0) {
9523
+ onEmpty = isEmptyTest.test(cur.slice(indexOfNl));
9524
+ if (onEmpty) {
9525
+ const newVal = cur.slice(0, indexOfNl);
9526
+ if (newVal) {
9527
+ this.stringBuilder[counter] = cur.slice(0, indexOfNl);
9528
+ } else {
9529
+ this.stringBuilder.splice(counter, 1);
9530
+ }
9531
+ }
9532
+ break;
9533
+ }
9534
+ onEmpty = isEmptyTest.test(cur);
9535
+ counter--;
9536
+ }
9537
+ this.print("\n", ...args);
9538
+ };
9539
+ };
9540
+
9541
+ // lib/generator-builder/generatorBuilder.js
9542
+ function listToRuleDefMap(rules) {
9543
+ const newRules = {};
9544
+ for (const rule of rules) {
9545
+ newRules[rule.name] = rule;
9546
+ }
9547
+ return newRules;
9548
+ }
9549
+ var GeneratorBuilder = class _GeneratorBuilder {
9550
+ static create(start) {
9551
+ if (start instanceof _GeneratorBuilder) {
9552
+ return new _GeneratorBuilder({ ...start.rules });
9553
+ }
9554
+ return new _GeneratorBuilder(listToRuleDefMap(start));
9555
+ }
9556
+ rules;
9557
+ constructor(startRules) {
9558
+ this.rules = startRules;
9559
+ }
9560
+ widenContext() {
9561
+ return this;
9562
+ }
9563
+ typePatch() {
9564
+ return this;
9565
+ }
9566
+ /**
9567
+ * Change the implementation of an existing generator rule.
9568
+ */
9569
+ patchRule(patch) {
9570
+ const self2 = this;
9571
+ self2.rules[patch.name] = patch;
9572
+ return self2;
9573
+ }
9574
+ /**
9575
+ * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
9576
+ */
9577
+ addRuleRedundant(rule) {
9578
+ const self2 = this;
9579
+ const rules = self2.rules;
9580
+ if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
9581
+ throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);
9582
+ }
9583
+ rules[rule.name] = rule;
9584
+ return self2;
9585
+ }
9586
+ /**
9587
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
9588
+ */
9589
+ addRule(rule) {
9590
+ return this.addRuleRedundant(rule);
9591
+ }
9592
+ addMany(...rules) {
9593
+ this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
9594
+ return this;
9595
+ }
9596
+ /**
9597
+ * Delete a grammar rule by its name.
9598
+ */
9599
+ deleteRule(ruleName) {
9600
+ delete this.rules[ruleName];
9601
+ return this;
9602
+ }
9603
+ /**
9604
+ * Merge this grammar GeneratorBuilder with another.
9605
+ * It is best to merge the bigger grammar with the smaller one.
9606
+ * If the two builders both have a grammar rule with the same name,
9607
+ * no error will be thrown case they map to the same ruledef object.
9608
+ * If they map to a different object, an error will be thrown.
9609
+ * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
9610
+ * this rule implementation will be used.
9611
+ */
9612
+ merge(GeneratorBuilder2, overridingRules) {
9613
+ const otherRules = { ...GeneratorBuilder2.rules };
9614
+ const myRules = this.rules;
9615
+ for (const rule of Object.values(myRules)) {
9616
+ if (otherRules[rule.name] === void 0) {
9617
+ otherRules[rule.name] = rule;
9618
+ } else {
9619
+ const existingRule = otherRules[rule.name];
9620
+ if (existingRule !== rule) {
9621
+ const override = overridingRules.find((x) => x.name === rule.name);
9622
+ if (override) {
9623
+ otherRules[rule.name] = override;
9624
+ } else {
9625
+ throw new Error(`Rule with name "${rule.name}" already exists in the GeneratorBuilder, specify an override to resolve conflict`);
9626
+ }
9627
+ }
9640
9628
  }
9641
- if (!_Parser.DEFER_DEFINITION_ERRORS_HANDLING && !isEmpty_default(this.definitionErrors)) {
9642
- defErrorsMsgs = map_default(this.definitionErrors, (defError) => defError.message);
9643
- throw new Error(`Parser Definition Errors detected:
9644
- ${defErrorsMsgs.join("\n-------------------------------\n")}`);
9645
- }
9646
- });
9629
+ }
9630
+ this.rules = otherRules;
9631
+ return this;
9647
9632
  }
9648
- constructor(tokenVocabulary, config) {
9649
- this.definitionErrors = [];
9650
- this.selfAnalysisDone = false;
9651
- const that = this;
9652
- that.initErrorHandler(config);
9653
- that.initLexerAdapter();
9654
- that.initLooksAhead(config);
9655
- that.initRecognizerEngine(tokenVocabulary, config);
9656
- that.initRecoverable(config);
9657
- that.initTreeBuilder(config);
9658
- that.initContentAssist();
9659
- that.initGastRecorder(config);
9660
- that.initPerformanceTracer(config);
9661
- if (has_default(config, "ignoredIssues")) {
9662
- throw new Error("The <ignoredIssues> IParserConfig property has been deprecated.\n Please use the <IGNORE_AMBIGUITIES> flag on the relevant DSL method instead.\n See: https://chevrotain.io/docs/guide/resolving_grammar_errors.html#IGNORING_AMBIGUITIES\n For further details.");
9633
+ build() {
9634
+ return new DynamicGenerator(this.rules);
9635
+ }
9636
+ };
9637
+
9638
+ // lib/indirection-builder/helpers.js
9639
+ function listToIndirectionMap(rules) {
9640
+ const newRules = {};
9641
+ for (const rule of rules) {
9642
+ newRules[rule.name] = rule;
9643
+ }
9644
+ return newRules;
9645
+ }
9646
+
9647
+ // lib/indirection-builder/dynamicIndirected.js
9648
+ var DynamicIndirect = class {
9649
+ rules;
9650
+ __context = void 0;
9651
+ constructor(rules) {
9652
+ this.rules = rules;
9653
+ for (const rule of Object.values(rules)) {
9654
+ this[rule.name] = ((context, ...args) => {
9655
+ this.setContext(context);
9656
+ return this.subrule(rule, ...args);
9657
+ });
9663
9658
  }
9664
- this.skipValidations = has_default(config, "skipValidations") ? config.skipValidations : DEFAULT_PARSER_CONFIG.skipValidations;
9665
9659
  }
9660
+ setContext(context) {
9661
+ this.__context = context;
9662
+ }
9663
+ getSafeContext() {
9664
+ return this.__context;
9665
+ }
9666
+ subrule = (cstDef, ...args) => {
9667
+ const def = this.rules[cstDef.name];
9668
+ if (!def) {
9669
+ throw new Error(`Rule ${cstDef.name} not found`);
9670
+ }
9671
+ return def.fun({
9672
+ SUBRULE: this.subrule
9673
+ })(this.getSafeContext(), ...args);
9674
+ };
9666
9675
  };
9667
- Parser.DEFER_DEFINITION_ERRORS_HANDLING = false;
9668
- applyMixins(Parser, [
9669
- Recoverable,
9670
- LooksAhead,
9671
- TreeBuilder,
9672
- LexerAdapter,
9673
- RecognizerEngine,
9674
- RecognizerApi,
9675
- ErrorHandler,
9676
- ContentAssist,
9677
- GastRecorder,
9678
- PerformanceTracer
9679
- ]);
9680
- var EmbeddedActionsParser = class extends Parser {
9681
- constructor(tokenVocabulary, config = DEFAULT_PARSER_CONFIG) {
9682
- const configClone = clone_default(config);
9683
- configClone.outputCst = false;
9684
- super(tokenVocabulary, configClone);
9676
+
9677
+ // lib/indirection-builder/IndirBuilder.js
9678
+ var IndirBuilder = class _IndirBuilder {
9679
+ static create(start) {
9680
+ if (start instanceof _IndirBuilder) {
9681
+ return new _IndirBuilder({ ...start.rules });
9682
+ }
9683
+ return new _IndirBuilder(listToIndirectionMap(start));
9684
+ }
9685
+ rules;
9686
+ constructor(startRules) {
9687
+ this.rules = startRules;
9688
+ }
9689
+ widenContext() {
9690
+ return this;
9691
+ }
9692
+ typePatch() {
9693
+ return this;
9694
+ }
9695
+ /**
9696
+ * Change the implementation of an existing indirection.
9697
+ */
9698
+ patchRule(patch) {
9699
+ const self2 = this;
9700
+ self2.rules[patch.name] = patch;
9701
+ return self2;
9702
+ }
9703
+ /**
9704
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
9705
+ */
9706
+ addRuleRedundant(rule) {
9707
+ const self2 = this;
9708
+ const rules = self2.rules;
9709
+ if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
9710
+ throw new Error(`Function ${rule.name} already exists in the builder`);
9711
+ }
9712
+ rules[rule.name] = rule;
9713
+ return self2;
9714
+ }
9715
+ /**
9716
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
9717
+ */
9718
+ addRule(rule) {
9719
+ return this.addRuleRedundant(rule);
9720
+ }
9721
+ addMany(...rules) {
9722
+ this.rules = { ...this.rules, ...listToIndirectionMap(rules) };
9723
+ return this;
9724
+ }
9725
+ /**
9726
+ * Delete a grammar rule by its name.
9727
+ */
9728
+ deleteRule(ruleName) {
9729
+ delete this.rules[ruleName];
9730
+ return this;
9731
+ }
9732
+ build() {
9733
+ return new DynamicIndirect(this.rules);
9685
9734
  }
9686
9735
  };
9687
9736
 
@@ -10047,16 +10096,13 @@ ${firstError.message}`);
10047
10096
  }
10048
10097
  };
10049
10098
 
10050
- // lib/utils.js
10051
- function unCapitalize(str) {
10052
- return str.charAt(0).toLowerCase() + str.slice(1);
10053
- }
10054
- function createToken2(config) {
10055
- return createToken(config);
10056
- }
10057
-
10058
10099
  // lib/Transformers.js
10059
10100
  var TransformerType = class {
10101
+ clone(obj) {
10102
+ const newObj = Object.create(Object.getPrototypeOf(obj));
10103
+ Object.defineProperties(newObj, Object.getOwnPropertyDescriptors(obj));
10104
+ return newObj;
10105
+ }
10060
10106
  safeObjectVisit(value, mapper) {
10061
10107
  if (value && typeof value === "object") {
10062
10108
  if (Array.isArray(value)) {
@@ -10066,104 +10112,213 @@ var TransformerType = class {
10066
10112
  }
10067
10113
  return value;
10068
10114
  }
10069
- transformNode(curObject, nodeCallBacks) {
10070
- let continueCheck;
10071
- let transformation;
10072
- const casted = curObject;
10073
- if (casted.type) {
10074
- continueCheck = nodeCallBacks[casted.type]?.continue;
10075
- transformation = nodeCallBacks[casted.type]?.transform;
10076
- }
10077
- let shouldContinue = true;
10078
- if (continueCheck) {
10079
- shouldContinue = continueCheck(curObject);
10080
- }
10081
- if (!shouldContinue) {
10082
- return curObject;
10083
- }
10084
- const copy = { ...curObject };
10085
- for (const [key, value] of Object.entries(copy)) {
10086
- copy[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, nodeCallBacks));
10087
- }
10088
- if (transformation) {
10089
- return transformation(copy);
10090
- }
10091
- return copy;
10115
+ /**
10116
+ * Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
10117
+ * @param startObject object to start iterating from
10118
+ * @param mapper mapper to transform the various objects - argument is a copy of the original
10119
+ * @param preVisitor callback that is evaluated before iterating deeper.
10120
+ * If continues is false, we do not iterate deeper, current object is still mapped. - default: true
10121
+ * If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
10122
+ * - Default false
10123
+ */
10124
+ transformObject(startObject, mapper, preVisitor = () => ({})) {
10125
+ let didShortCut = false;
10126
+ const recurse = (curObject) => {
10127
+ const copy = this.clone(curObject);
10128
+ const context = preVisitor(copy);
10129
+ didShortCut = context.shortcut ?? false;
10130
+ const continues = context.continue ?? true;
10131
+ if (continues && !didShortCut) {
10132
+ for (const [key, value] of Object.entries(copy)) {
10133
+ if (didShortCut) {
10134
+ return copy;
10135
+ }
10136
+ copy[key] = this.safeObjectVisit(value, (obj) => recurse(obj));
10137
+ }
10138
+ }
10139
+ return mapper(copy);
10140
+ };
10141
+ return recurse(startObject);
10092
10142
  }
10093
- visitNode(curObject, nodeCallBacks) {
10094
- let callback;
10095
- const casted = curObject;
10096
- if (casted.type) {
10097
- callback = nodeCallBacks[casted.type];
10098
- }
10099
- let shouldIterate = true;
10100
- if (callback) {
10101
- shouldIterate = callback(curObject);
10102
- }
10103
- if (shouldIterate) {
10104
- for (const value of Object.values(curObject)) {
10105
- this.safeObjectVisit(value, (obj) => this.visitNode(obj, nodeCallBacks));
10143
+ /**
10144
+ * Visitor that visits all objects. Visits deeper objects first.
10145
+ */
10146
+ visitObject(startObject, visitor, preVisitor = () => ({})) {
10147
+ let didShortCut = false;
10148
+ const recurse = (curObject) => {
10149
+ const context = preVisitor(curObject);
10150
+ didShortCut = context.shortcut ?? false;
10151
+ const continues = context.continue ?? true;
10152
+ if (continues && !didShortCut) {
10153
+ for (const value of Object.values(curObject)) {
10154
+ if (didShortCut) {
10155
+ return;
10156
+ }
10157
+ this.safeObjectVisit(value, (obj) => recurse(obj));
10158
+ }
10106
10159
  }
10107
- }
10160
+ visitor(curObject);
10161
+ };
10162
+ recurse(startObject);
10163
+ }
10164
+ transformNode(startObject, nodeCallBacks) {
10165
+ const transformWrapper = (curObject) => {
10166
+ const casted = curObject;
10167
+ if (casted.type) {
10168
+ const ogFunc = nodeCallBacks[casted.type]?.transform;
10169
+ if (ogFunc) {
10170
+ return ogFunc(casted);
10171
+ }
10172
+ }
10173
+ return curObject;
10174
+ };
10175
+ const preTransformWrapper = (curObject) => {
10176
+ const casted = curObject;
10177
+ if (casted.type) {
10178
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
10179
+ if (ogFunc) {
10180
+ return ogFunc(casted);
10181
+ }
10182
+ }
10183
+ return {};
10184
+ };
10185
+ return this.transformObject(startObject, transformWrapper, preTransformWrapper);
10186
+ }
10187
+ visitNode(startObject, nodeCallBacks) {
10188
+ const visitWrapper = (curObject) => {
10189
+ const casted = curObject;
10190
+ if (casted.type) {
10191
+ const ogFunc = nodeCallBacks[casted.type]?.visitor;
10192
+ if (ogFunc) {
10193
+ ogFunc(casted);
10194
+ }
10195
+ }
10196
+ };
10197
+ const preVisitWrapper = (curObject) => {
10198
+ const casted = curObject;
10199
+ if (casted.type) {
10200
+ const ogFunc = nodeCallBacks[casted.type]?.preVisitor;
10201
+ if (ogFunc) {
10202
+ return ogFunc(casted);
10203
+ }
10204
+ }
10205
+ return {};
10206
+ };
10207
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
10208
+ }
10209
+ traverseNodes(currentNode, traverse) {
10210
+ let didShortCut = false;
10211
+ const recurse = (curNode) => {
10212
+ const traverser = traverse[curNode.type];
10213
+ if (traverser) {
10214
+ const { next, shortcut } = traverser(curNode);
10215
+ didShortCut = shortcut ?? false;
10216
+ if (!didShortCut) {
10217
+ for (const node of next ?? []) {
10218
+ if (didShortCut) {
10219
+ return;
10220
+ }
10221
+ recurse(node);
10222
+ }
10223
+ }
10224
+ }
10225
+ };
10226
+ recurse(currentNode);
10108
10227
  }
10109
10228
  };
10110
10229
  var TransformerSubType = class extends TransformerType {
10111
- // Public visitObjects(curObject: object, visitor: (current: object) => void): void {
10112
- // for (const value of Object.values(curObject)) {
10113
- // this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));
10114
- // }
10115
- // }
10116
- transformNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
10117
- let continueCheck;
10118
- let transformation;
10119
- const casted = curObject;
10120
- if (casted.type && casted.subType) {
10121
- const specific = nodeSpecificCallBacks[casted.type];
10122
- if (specific) {
10123
- continueCheck = specific[casted.subType]?.continue;
10124
- transformation = specific[casted.subType]?.transform;
10125
- }
10126
- }
10127
- let shouldContinue = true;
10128
- if (continueCheck) {
10129
- shouldContinue = continueCheck(curObject);
10130
- }
10131
- if (!shouldContinue) {
10132
- return curObject;
10133
- }
10134
- const copy = { ...curObject };
10135
- for (const [key, value] of Object.entries(copy)) {
10136
- copy[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
10137
- }
10138
- if (transformation) {
10139
- return transformation(copy);
10140
- }
10141
- return copy;
10230
+ transformNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
10231
+ const transformWrapper = (curObject) => {
10232
+ let ogTransform;
10233
+ const casted = curObject;
10234
+ if (casted.type && casted.subType) {
10235
+ const specific = nodeSpecificCallBacks[casted.type];
10236
+ if (specific) {
10237
+ ogTransform = specific[casted.subType]?.transform;
10238
+ }
10239
+ if (!ogTransform) {
10240
+ ogTransform = nodeCallBacks[casted.type]?.transform;
10241
+ }
10242
+ }
10243
+ return ogTransform ? ogTransform(casted) : curObject;
10244
+ };
10245
+ const preVisitWrapper = (curObject) => {
10246
+ let ogPreVisit;
10247
+ const casted = curObject;
10248
+ if (casted.type && casted.subType) {
10249
+ const specific = nodeSpecificCallBacks[casted.type];
10250
+ if (specific) {
10251
+ ogPreVisit = specific[casted.subType]?.preVisitor;
10252
+ }
10253
+ if (!ogPreVisit) {
10254
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
10255
+ }
10256
+ }
10257
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
10258
+ };
10259
+ return this.transformObject(startObject, transformWrapper, preVisitWrapper);
10142
10260
  }
10143
10261
  /**
10144
10262
  * When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
10145
10263
  */
10146
- visitNodeSpecific(curObject, nodeCallBacks, nodeSpecificCallBacks = {}) {
10147
- let callback;
10148
- const casted = curObject;
10149
- if (casted.type && casted.subType) {
10150
- const specific = nodeSpecificCallBacks[casted.type];
10151
- if (specific) {
10152
- callback = specific[casted.subType];
10264
+ visitNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
10265
+ const visitWrapper = (curObject) => {
10266
+ let ogTransform;
10267
+ const casted = curObject;
10268
+ if (casted.type && casted.subType) {
10269
+ const specific = nodeSpecificCallBacks[casted.type];
10270
+ if (specific) {
10271
+ ogTransform = specific[casted.subType]?.visitor;
10272
+ }
10273
+ if (!ogTransform) {
10274
+ ogTransform = nodeCallBacks[casted.type]?.visitor;
10275
+ }
10153
10276
  }
10154
- }
10155
- if (!callback && casted.type) {
10156
- callback = nodeCallBacks[curObject.type];
10157
- }
10158
- let shouldIterate = true;
10159
- if (callback) {
10160
- shouldIterate = callback(curObject) ?? true;
10161
- }
10162
- if (shouldIterate) {
10163
- for (const value of Object.values(curObject)) {
10164
- this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, nodeCallBacks, nodeSpecificCallBacks));
10277
+ if (ogTransform) {
10278
+ ogTransform(casted);
10165
10279
  }
10166
- }
10280
+ };
10281
+ const preVisitWrapper = (curObject) => {
10282
+ let ogPreVisit;
10283
+ const casted = curObject;
10284
+ if (casted.type && casted.subType) {
10285
+ const specific = nodeSpecificCallBacks[casted.type];
10286
+ if (specific) {
10287
+ ogPreVisit = specific[casted.subType]?.preVisitor;
10288
+ }
10289
+ if (!ogPreVisit) {
10290
+ ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;
10291
+ }
10292
+ }
10293
+ return ogPreVisit ? ogPreVisit(casted) : curObject;
10294
+ };
10295
+ this.visitObject(startObject, visitWrapper, preVisitWrapper);
10296
+ }
10297
+ traverseSubNodes(currentNode, traverseNode, traverseSubNode) {
10298
+ let didShortCut = false;
10299
+ const recurse = (curNode) => {
10300
+ let traverser;
10301
+ const subObj = traverseSubNode[curNode.type];
10302
+ if (subObj) {
10303
+ traverser = subObj[curNode.subType];
10304
+ }
10305
+ if (!traverser) {
10306
+ traverser = traverseNode[curNode.type];
10307
+ }
10308
+ if (traverser) {
10309
+ const { next, shortcut } = traverser(curNode);
10310
+ didShortCut = shortcut ?? false;
10311
+ if (!didShortCut) {
10312
+ for (const node of next ?? []) {
10313
+ if (didShortCut) {
10314
+ return;
10315
+ }
10316
+ recurse(node);
10317
+ }
10318
+ }
10319
+ }
10320
+ };
10321
+ recurse(currentNode);
10167
10322
  }
10168
10323
  };
10169
10324
  /*! Bundled license information: