parseman 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +34 -19
  2. package/dist/analysis/duplication.d.ts +421 -0
  3. package/dist/analysis/duplication.d.ts.map +1 -0
  4. package/dist/analysis/gating.d.ts +25 -0
  5. package/dist/analysis/gating.d.ts.map +1 -1
  6. package/dist/analysis/grammar.d.ts +12 -0
  7. package/dist/analysis/grammar.d.ts.map +1 -0
  8. package/dist/combinators/expect.d.ts +12 -0
  9. package/dist/combinators/expect.d.ts.map +1 -1
  10. package/dist/combinators/parser.d.ts +26 -0
  11. package/dist/combinators/parser.d.ts.map +1 -1
  12. package/dist/compiler/build-arity.d.ts +12 -0
  13. package/dist/compiler/build-arity.d.ts.map +1 -1
  14. package/dist/compiler/codegen.d.ts +43 -1
  15. package/dist/compiler/codegen.d.ts.map +1 -1
  16. package/dist/compiler/linker.d.ts +64 -7
  17. package/dist/compiler/linker.d.ts.map +1 -1
  18. package/dist/cst/host-mode.d.ts +43 -0
  19. package/dist/cst/host-mode.d.ts.map +1 -0
  20. package/dist/functional/doc.d.ts.map +1 -1
  21. package/dist/functional/run.d.ts.map +1 -1
  22. package/dist/index.cjs +1621 -55
  23. package/dist/index.cjs.map +4 -4
  24. package/dist/index.d.ts +6 -2
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1621 -55
  27. package/dist/index.js.map +4 -4
  28. package/dist/language-service/index.cjs +37 -1
  29. package/dist/language-service/index.cjs.map +3 -3
  30. package/dist/language-service/index.js +37 -1
  31. package/dist/language-service/index.js.map +3 -3
  32. package/dist/oracle/corpus.d.ts +32 -0
  33. package/dist/oracle/corpus.d.ts.map +1 -0
  34. package/dist/oracle/digest.d.ts +21 -0
  35. package/dist/oracle/digest.d.ts.map +1 -0
  36. package/dist/oracle/identity.d.ts +117 -0
  37. package/dist/oracle/identity.d.ts.map +1 -0
  38. package/dist/oracle/index.cjs +423 -0
  39. package/dist/oracle/index.cjs.map +7 -0
  40. package/dist/oracle/index.d.ts +37 -0
  41. package/dist/oracle/index.d.ts.map +1 -0
  42. package/dist/oracle/index.js +400 -0
  43. package/dist/oracle/index.js.map +7 -0
  44. package/dist/plugin/index.cjs +1628 -62
  45. package/dist/plugin/index.cjs.map +4 -4
  46. package/dist/plugin/index.d.ts.map +1 -1
  47. package/dist/plugin/index.js +1628 -62
  48. package/dist/plugin/index.js.map +4 -4
  49. package/dist/run/index.cjs +23 -0
  50. package/dist/run/index.cjs.map +3 -3
  51. package/dist/run/index.js +23 -0
  52. package/dist/run/index.js.map +3 -3
  53. package/dist/spec/index.cjs +2977 -56
  54. package/dist/spec/index.cjs.map +4 -4
  55. package/dist/spec/index.js +2977 -56
  56. package/dist/spec/index.js.map +4 -4
  57. package/dist/spec/model.d.ts.map +1 -1
  58. package/dist/types.d.ts +7 -0
  59. package/dist/types.d.ts.map +1 -1
  60. package/dist/version.d.ts +1 -1
  61. package/package.json +10 -3
package/dist/index.js CHANGED
@@ -1049,7 +1049,7 @@ var REC = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSet
1049
1049
 
1050
1050
  // src/combinators/expect.ts
1051
1051
  function deriveExpected(c) {
1052
- return derive(c, /* @__PURE__ */ new Set());
1052
+ return [...new Set(derive(c, /* @__PURE__ */ new Set()))];
1053
1053
  }
1054
1054
  function derive(c, seen) {
1055
1055
  const deriveExpected2 = (p) => derive(p, seen);
@@ -2399,6 +2399,12 @@ function rules(a, b) {
2399
2399
  if (rule && !rule._meta.isTrivia) rule._meta.grammarScanSkip = options.scanSkip;
2400
2400
  }
2401
2401
  }
2402
+ if (options?.hostMode === "cst") {
2403
+ for (const key of Object.keys(definitions)) {
2404
+ const rule = cache[key];
2405
+ if (rule && !rule._meta.isTrivia) rule._meta.grammarHostMode = "cst";
2406
+ }
2407
+ }
2402
2408
  for (const key of Object.keys(definitions)) {
2403
2409
  markUnusedValues(cache[key]);
2404
2410
  }
@@ -2457,6 +2463,9 @@ function peek(combinator) {
2457
2463
  }
2458
2464
 
2459
2465
  // src/compiler/build-arity.ts
2466
+ function cstOutputHost(build) {
2467
+ return build?._parsemanCstOutput === true;
2468
+ }
2460
2469
  var PARAM_LIST_RE = (
2461
2470
  // (params) => ... | function name?(params) ... | single-ident arrow `x => ...`
2462
2471
  /^(?:function\b[^(]*\(([^)]*)\)|\(([^)]*)\)\s*=>|([A-Za-z_$][\w$]*)\s*=>)/
@@ -2706,7 +2715,8 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2706
2715
  const def = unwrap || collapse || captureTrivia || trailingTrivia ? { ...baseDef, ...unwrap ? { unwrap: true } : {}, ...collapse ? { collapse: true } : {}, ...captureTrivia ? { captureTrivia: true } : {}, ...trailingTrivia ? { trailingTrivia: true } : {} } : baseDef;
2707
2716
  const capturesTrivia = captureTrivia || trailingTrivia || (build ? buildReadsTrivia(def) : true);
2708
2717
  const clonesState = build ? buildReadsState(def) : true;
2709
- const capturesFields = parserHasOwnFields(combinator) && (build ? buildReadsFields(def) : true);
2718
+ const hasOwnFields = parserHasOwnFields(combinator);
2719
+ const capturesFields = hasOwnFields && (build ? buildReadsFields(def) : true);
2710
2720
  const guardable = combinator._meta.firstSet.kind !== "any" && !matchesEmpty(combinator);
2711
2721
  let failExpected;
2712
2722
  return {
@@ -2721,28 +2731,32 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2721
2731
  }
2722
2732
  return { ok: false, expected: failExpected, span: { start: pos, end: pos } };
2723
2733
  }
2734
+ const hostCst = build !== void 0 && cstOutputHost(ctx.build);
2735
+ const effTrivia = capturesTrivia || hostCst;
2736
+ const effFields = capturesFields || hostCst && hasOwnFields;
2724
2737
  const saved = beginCstNodeCapture(ctx);
2725
2738
  const savedFields = ctx._fields;
2726
- ctx._fields = capturesFields ? [] : void 0;
2739
+ ctx._fields = effFields ? [] : void 0;
2727
2740
  const savedMask = ctx._triviaCaptureMask;
2728
2741
  if (build === void 0 && ctx.build?._parsemanTriviaKinds !== void 0 && def.type !== void 0) {
2729
2742
  ctx._triviaCaptureMask = ctx.build._parsemanTriviaKinds(def.type);
2730
2743
  }
2731
- if (!capturesTrivia) ctx.captureTrivia = false;
2744
+ if (!effTrivia) ctx.captureTrivia = false;
2732
2745
  let r = combinator.parse(input, pos, ctx);
2733
2746
  if (r.ok && trailingTrivia && ctx.trivia) {
2734
2747
  const end = consumeTrivia(input, r.span.end, ctx);
2735
2748
  r = { ...r, span: { start: r.span.start, end } };
2736
2749
  }
2737
- const fields = capturesFields ? buildFieldMap(ctx._fields) : void 0;
2750
+ const fields = effFields ? buildFieldMap(ctx._fields) : void 0;
2738
2751
  ctx._fields = savedFields;
2739
2752
  ctx._triviaCaptureMask = savedMask;
2740
2753
  const { children: children2, rawChildren, triviaLog } = endCstNodeCapture(ctx, saved);
2741
2754
  if (!r.ok) return r;
2742
2755
  const st = clonesState && ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
2743
2756
  const nodeType = def.type ?? missingInferredType();
2744
- const cstOutput = ctx.build?._parsemanCstOutput === true;
2745
- const built = unwrap && children2.length === 1 ? unwrapChild(children2[0]) : collapse && children2.length === 1 ? children2[0] : !build && ctx.build?._parsemanCstCollapse && children2.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children2[0], children2, rawChildren) ? children2[0] : build ? cstOutput && ctx.build ? ctx.build(nodeType, children2, fields, r.span, rawChildren, triviaLog, st) : build(children2, fields, r.span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children2, fields, r.span, rawChildren, triviaLog, st) : { _tag: "node", type: nodeType, span: { start: r.span.start, end: r.span.end }, state: st ?? null, children: children2 };
2757
+ const cstOutput = cstOutputHost(ctx.build);
2758
+ const hostState = clonesState ? st : ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
2759
+ const built = unwrap && children2.length === 1 ? unwrapChild(children2[0]) : collapse && children2.length === 1 ? children2[0] : !build && ctx.build?._parsemanCstCollapse && children2.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children2[0], children2, rawChildren) ? children2[0] : build ? cstOutput && ctx.build ? ctx.build(nodeType, children2, fields, r.span, rawChildren, triviaLog, hostState) : build(children2, fields, r.span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children2, fields, r.span, rawChildren, triviaLog, st) : { _tag: "node", type: nodeType, span: { start: r.span.start, end: r.span.end }, state: st ?? null, children: children2 };
2746
2760
  const rawEntry = isCstChild(built) ? built : { _tag: "leaf", value: typeof built === "string" ? built : typeof built === "object" && built !== null ? input.slice(r.span.start, r.span.end) : "", span: r.span };
2747
2761
  if (saved.buf !== void 0 || saved.ch !== void 0) {
2748
2762
  pushCstChild(ctx, built, rawEntry);
@@ -3020,7 +3034,24 @@ function leaf(root, fn) {
3020
3034
  }
3021
3035
 
3022
3036
  // src/version.ts
3023
- var PARSEMAN_VERSION = "0.35.0";
3037
+ var PARSEMAN_VERSION = "0.37.0";
3038
+
3039
+ // src/cst/host-mode.ts
3040
+ var FUSED_HOST_MODE = /* @__PURE__ */ Symbol.for("parseman.fusedHostMode");
3041
+ var FUSED_HOST_ELIDED = /* @__PURE__ */ Symbol.for("parseman.fusedHostElided");
3042
+ function assertHostModeCompatible(mode, build, hostBranchElided = true) {
3043
+ const isCstHost = build?._parsemanCstOutput === true;
3044
+ if (mode === "ast" && isCstHost && hostBranchElided) {
3045
+ throw new Error(
3046
+ "parseman: this parser was compiled for host mode \"ast\" (the default), so its nodes build through their own `build` callbacks and no positioned-CST branch was emitted. It cannot be driven with a positioned-CST host (cstBuildHost / a language-service host). Compile a second artifact from the SAME grammar with `compile(grammar, { hostMode: 'cst' })` \u2014 or, under the macro, a second `rules({ hostMode: 'cst' }, factory)` call site \u2014 and drive that one instead."
3047
+ );
3048
+ }
3049
+ if (mode === "cst" && !isCstHost) {
3050
+ throw new Error(
3051
+ 'parseman: this parser was compiled for host mode "cst", so every node builds through a positioned-CST `ctx.build` host. Pass one (e.g. `{ build: cstBuildHost }`), or use an artifact compiled with the default `hostMode: "ast"` to get the grammar\'s own AST.'
3052
+ );
3053
+ }
3054
+ }
3024
3055
 
3025
3056
  // src/analysis/gating.ts
3026
3057
  var isAny = (fs) => fs.kind === "any";
@@ -3220,11 +3251,34 @@ function analyzeGating(entry, opts) {
3220
3251
  function analyzeGatingRules(ruleMap, opts) {
3221
3252
  const raw = [];
3222
3253
  const antiPatterns = [];
3254
+ const unanalysable = [];
3255
+ const seenUnanalysable = /* @__PURE__ */ new Set();
3223
3256
  const visited = /* @__PURE__ */ new Set();
3257
+ const noteUnanalysable = (u) => {
3258
+ const key = `${u.rule}\0${u.kind}`;
3259
+ if (seenUnanalysable.has(key)) return;
3260
+ seenUnanalysable.add(key);
3261
+ unanalysable.push(u);
3262
+ };
3224
3263
  const visit = (p, enclosingRule) => {
3225
3264
  if (visited.has(p)) return;
3226
3265
  visited.add(p);
3227
- const d = p._def;
3266
+ const def = p?._def;
3267
+ if (def === null || typeof def !== "object") {
3268
+ noteUnanalysable(
3269
+ typeof p === "function" ? {
3270
+ rule: enclosingRule,
3271
+ kind: "fused-rule",
3272
+ reason: "fused rule function \u2014 a compose()/fuse result has no combinator graph. Analyze the composed grammar itself (analyzeGrammarGating) so the carried IR is re-lowered first."
3273
+ } : {
3274
+ rule: enclosingRule,
3275
+ kind: "not-a-combinator",
3276
+ reason: `value of type ${p === null ? "null" : typeof p} is not a combinator and carries no _def descriptor.`
3277
+ }
3278
+ );
3279
+ return;
3280
+ }
3281
+ const d = def;
3228
3282
  const rule = ruleNameOf(p) ?? enclosingRule;
3229
3283
  if (d.tag === "choice") {
3230
3284
  raw.push({ g: analyzeChoice(p, d, rule, opts?.resolveRef), rule });
@@ -3264,7 +3318,18 @@ function analyzeGatingRules(ruleMap, opts) {
3264
3318
  const accepted = choices.filter((c) => c.gates === "no" && c.accepted);
3265
3319
  const deferred = choices.filter((c) => c.gates === "no" && !c.accepted && c.deferred);
3266
3320
  const acceptedUnused = [...accept].filter((id) => !usedAccept.has(id));
3267
- return { totalChoices: choices.length, gated, recoverable, ungated, accepted, deferred, acceptedUnused, choices, antiPatterns };
3321
+ return {
3322
+ totalChoices: choices.length,
3323
+ gated,
3324
+ recoverable,
3325
+ unanalysable,
3326
+ ungated,
3327
+ accepted,
3328
+ deferred,
3329
+ acceptedUnused,
3330
+ choices,
3331
+ antiPatterns
3332
+ };
3268
3333
  }
3269
3334
  function analyzeChoice(p, d, rule, resolve) {
3270
3335
  const arms = d.parsers;
@@ -3301,6 +3366,13 @@ function analyzeChoice(p, d, rule, resolve) {
3301
3366
  }
3302
3367
  function formatGatingWarnings(report) {
3303
3368
  const lines = [];
3369
+ if (report.unanalysable.length > 0) {
3370
+ lines.push(
3371
+ `parseman gating: ${report.unanalysable.length} rule(s) UNANALYSABLE \u2014 this report is PARTIAL (${report.totalChoices} choice(s) examined). An empty finding list below does NOT mean the grammar is clean.`
3372
+ );
3373
+ for (const u of report.unanalysable)
3374
+ lines.push(` \xB7 ${u.rule} [${u.kind}]: ${u.reason}`);
3375
+ }
3304
3376
  for (const c of report.ungated) {
3305
3377
  lines.push(`parseman gating: choice @ ${c.id} is UNGATED [${c.strategy}] \u2014 no first-char dispatch; every position speculatively enters doomed arms.`);
3306
3378
  for (const a of c.anyArms)
@@ -3316,6 +3388,1284 @@ function formatGatingWarnings(report) {
3316
3388
  return lines;
3317
3389
  }
3318
3390
 
3391
+ // src/analysis/duplication.ts
3392
+ var siteToString = (s) => s.path === "" ? s.rule : `${s.rule} \u203A ${s.path}`;
3393
+ function childrenOf(d) {
3394
+ switch (d.tag) {
3395
+ case "sequence":
3396
+ case "choice":
3397
+ return d.parsers;
3398
+ case "skip":
3399
+ return [d.main, d.skipped];
3400
+ case "sepBy":
3401
+ return [d.parser, d.separator];
3402
+ case "recover":
3403
+ return [d.parser, d.sentinel];
3404
+ case "scanTo":
3405
+ return [d.sentinel, ...d.skip];
3406
+ case "grammar":
3407
+ return d.triviaParser ? [d.parser, d.triviaParser] : [d.parser];
3408
+ // A `lazy` is a REFERENCE, not a subtree: hashing through it would make every
3409
+ // rule's shape include the whole reachable grammar. Treated as a leaf, keyed
3410
+ // by the rule name it refers to — which is also the right semantics, since two
3411
+ // productions referencing `g.Ident` really do fill that slot the same way.
3412
+ case "lazy":
3413
+ case "literal":
3414
+ case "regex":
3415
+ case "keywords":
3416
+ case "guard":
3417
+ case "unknown":
3418
+ return [];
3419
+ default: {
3420
+ const rec = d;
3421
+ return rec.parser ? [rec.parser] : [];
3422
+ }
3423
+ }
3424
+ }
3425
+ var ruleNameOf2 = (p) => p._ruleName;
3426
+ var fnKey = (src, fn) => src ?? (typeof fn === "function" ? String(fn) : "");
3427
+ function payloadKey(p, d) {
3428
+ switch (d.tag) {
3429
+ case "literal":
3430
+ return `literal\0${d.value}\0${d.caseInsensitive}`;
3431
+ case "regex":
3432
+ return `regex\0${d.source}\0${d.flags}`;
3433
+ case "keywords":
3434
+ return `keywords\0${[...d.words].join("")}\0${d.caseInsensitive}\0${d.boundary ?? ""}`;
3435
+ case "lazy": {
3436
+ const n = ruleNameOf2(p);
3437
+ return n === void 0 ? `ref\0#${anonId(p)}` : `ref\0${n}`;
3438
+ }
3439
+ case "label":
3440
+ return `label\0${d.label}`;
3441
+ case "field":
3442
+ return `field\0${d.name}`;
3443
+ case "node":
3444
+ return `node\0${d.type ?? ""}\0${d.unwrap === true}\0${d.collapse === true}\0${fnKey(d.buildSrc, d.build)}`;
3445
+ case "transform":
3446
+ return `transform\0${fnKey(d.fnSrc, d.fn)}\0${d.recognitionOnly === true}`;
3447
+ case "leaf":
3448
+ return `leaf\0${fnKey(d.fnSrc, d.fn)}`;
3449
+ case "many":
3450
+ case "oneOrMore":
3451
+ return `${d.tag}\0${d.min}\0${d.max ?? ""}`;
3452
+ case "sepBy":
3453
+ return `sepBy\0${d.min}\0${d.max ?? ""}\0${d.trailing ?? ""}`;
3454
+ case "expect":
3455
+ return `expect\0${d.label ?? ""}\0${d.expected.join("")}`;
3456
+ case "scanTo":
3457
+ return `scanTo\0${d.raw}\0${d.orEOF}`;
3458
+ case "guard":
3459
+ return `guard\0${fnKey(d.predSrc, d.predicate)}`;
3460
+ case "withCtx":
3461
+ return `withCtx\0${d.extraSrc ?? safeJson(d.extra)}`;
3462
+ case "grammar":
3463
+ return `grammar\0${d.clearTrivia === true}\0${d.captureTrivia === true}\0${d.trackLines}`;
3464
+ default:
3465
+ return d.tag;
3466
+ }
3467
+ }
3468
+ function safeJson(v2) {
3469
+ try {
3470
+ return JSON.stringify(v2) ?? String(v2);
3471
+ } catch {
3472
+ return "?";
3473
+ }
3474
+ }
3475
+ var anonCounter = 0;
3476
+ var anonIds = /* @__PURE__ */ new WeakMap();
3477
+ function anonId(p) {
3478
+ let id = anonIds.get(p);
3479
+ if (id === void 0) {
3480
+ id = ++anonCounter;
3481
+ anonIds.set(p, id);
3482
+ }
3483
+ return id;
3484
+ }
3485
+ var Interner = class {
3486
+ ids = /* @__PURE__ */ new Map();
3487
+ hole;
3488
+ constructor() {
3489
+ this.hole = this.of("\0HOLE\0");
3490
+ }
3491
+ of(key) {
3492
+ let id = this.ids.get(key);
3493
+ if (id === void 0) {
3494
+ id = this.ids.size;
3495
+ this.ids.set(key, id);
3496
+ }
3497
+ return id;
3498
+ }
3499
+ };
3500
+ var MAX_RENDER = 160;
3501
+ function render(p, depth = 3) {
3502
+ const d = p._def;
3503
+ const name = ruleNameOf2(p);
3504
+ switch (d.tag) {
3505
+ case "literal":
3506
+ return `literal('${d.value}')`;
3507
+ case "regex":
3508
+ return `regex(/${d.source}/${d.flags})`;
3509
+ case "keywords":
3510
+ return `keywords([${[...d.words].slice(0, 4).map((w) => `'${w}'`).join(", ")}${d.words.length > 4 ? ", \u2026" : ""}])`;
3511
+ case "lazy":
3512
+ return `g.${name ?? "?"}`;
3513
+ case "guard":
3514
+ return "gate(\u2026)";
3515
+ default:
3516
+ break;
3517
+ }
3518
+ if (depth <= 0) return `${d.tag}(\u2026)`;
3519
+ const kids = childrenOf(d).map((k) => render(k, depth - 1));
3520
+ switch (d.tag) {
3521
+ case "sequence":
3522
+ return `sequence(${kids.join(", ")})`;
3523
+ case "choice":
3524
+ return `choice(${kids.join(", ")})`;
3525
+ case "node":
3526
+ return `node(${d.type ? `'${d.type}', ` : ""}${kids[0] ?? ""})`;
3527
+ case "label":
3528
+ return `label('${d.label}', ${kids[0] ?? ""})`;
3529
+ case "field":
3530
+ return `field('${d.name}', ${kids[0] ?? ""})`;
3531
+ case "many":
3532
+ return `many(${kids[0] ?? ""}${d.min === 0 && d.max === void 0 ? "" : `, { min: ${d.min}${d.max === void 0 ? "" : `, max: ${d.max}`} }`})`;
3533
+ case "oneOrMore":
3534
+ return `oneOrMore(${kids[0] ?? ""}${d.min === 1 && d.max === void 0 ? "" : `, { min: ${d.min}${d.max === void 0 ? "" : `, max: ${d.max}`} }`})`;
3535
+ case "sepBy":
3536
+ return `sepBy(${kids[0] ?? ""}, ${kids[1] ?? ""})`;
3537
+ default:
3538
+ return `${d.tag}(${kids.join(", ")})`;
3539
+ }
3540
+ }
3541
+ var clamp = (s) => s.length <= MAX_RENDER ? s : `${s.slice(0, MAX_RENDER - 1)}\u2026`;
3542
+ function segment(d, index) {
3543
+ switch (d.tag) {
3544
+ case "sequence":
3545
+ return `seq[${index}]`;
3546
+ case "choice":
3547
+ return `choice[${index}]`;
3548
+ case "node":
3549
+ return `node(${d.type ?? ""})`;
3550
+ case "label":
3551
+ return `label(${d.label})`;
3552
+ case "field":
3553
+ return `field(${d.name})`;
3554
+ case "sepBy":
3555
+ return index === 0 ? "sepBy.item" : "sepBy.sep";
3556
+ case "skip":
3557
+ return index === 0 ? "skip.main" : "skip.skipped";
3558
+ case "recover":
3559
+ return index === 0 ? "recover.body" : "recover.sentinel";
3560
+ case "scanTo":
3561
+ return index === 0 ? "scanTo.sentinel" : `scanTo.skip[${index - 1}]`;
3562
+ default:
3563
+ return d.tag;
3564
+ }
3565
+ }
3566
+ function alternationGroups(src) {
3567
+ const out = [];
3568
+ const stack = [{ branches: [], last: 0 }];
3569
+ let i = 0;
3570
+ while (i < src.length) {
3571
+ const c = src[i];
3572
+ if (c === "\\") {
3573
+ i += 2;
3574
+ continue;
3575
+ }
3576
+ if (c === "[") {
3577
+ let j = i + 1;
3578
+ if (src[j] === "^") j++;
3579
+ if (src[j] === "]") j++;
3580
+ while (j < src.length && src[j] !== "]") {
3581
+ if (src[j] === "\\") j++;
3582
+ j++;
3583
+ }
3584
+ i = j + 1;
3585
+ continue;
3586
+ }
3587
+ if (c === "(") {
3588
+ let open = i + 1;
3589
+ if (src[open] === "?") {
3590
+ const rest = src.slice(open);
3591
+ const m = /^\?(?::|=|!|<=|<!|<[^>]*>)/.exec(rest);
3592
+ open += m ? m[0].length : 1;
3593
+ }
3594
+ stack.push({ branches: [], last: open });
3595
+ i = open;
3596
+ continue;
3597
+ }
3598
+ if (c === ")") {
3599
+ const top = stack.pop();
3600
+ if (top !== void 0) {
3601
+ top.branches.push(src.slice(top.last, i));
3602
+ if (top.branches.length >= 2) out.push(top.branches);
3603
+ }
3604
+ i++;
3605
+ continue;
3606
+ }
3607
+ if (c === "|") {
3608
+ const top = stack[stack.length - 1];
3609
+ top.branches.push(src.slice(top.last, i));
3610
+ top.last = i + 1;
3611
+ i++;
3612
+ continue;
3613
+ }
3614
+ i++;
3615
+ }
3616
+ const root = stack[0];
3617
+ root.branches.push(src.slice(root.last));
3618
+ if (root.branches.length >= 2) out.push(root.branches);
3619
+ return out;
3620
+ }
3621
+ var WORD_RE_SRC = "-?[A-Za-z][A-Za-z0-9_-]*";
3622
+ var WORDS_RE = new RegExp(`^${WORD_RE_SRC}(?:\\|${WORD_RE_SRC})*$`);
3623
+ var VOCABULARY_MIN_WORDS = 3;
3624
+ function keywordRegexShape(source) {
3625
+ let s = source;
3626
+ if (s.startsWith("^")) s = s.slice(1);
3627
+ s = s.replace(/^\(\?<!\[[^\]]+\]\)/, "");
3628
+ let boundary = null;
3629
+ const tail = /(?:\(\?!\[([^\]]+)\]\)|\\b)$/.exec(s);
3630
+ if (tail !== null) {
3631
+ const cls = tail[1];
3632
+ if (cls !== void 0 && cls.startsWith("^")) return null;
3633
+ boundary = cls ?? "_0-9A-Za-z";
3634
+ s = s.slice(0, tail.index);
3635
+ }
3636
+ const grouped = /^\(\?:([^()]*)\)$/.exec(s);
3637
+ if (grouped !== null) s = grouped[1];
3638
+ if (s.endsWith("$")) s = s.slice(0, -1);
3639
+ if (!WORDS_RE.test(s)) return null;
3640
+ const words = s.split("|");
3641
+ if (boundary === null && words.length < VOCABULARY_MIN_WORDS) return null;
3642
+ return { words, boundary };
3643
+ }
3644
+ function boundaryMatches(boundary, ch2) {
3645
+ const cp = ch2.codePointAt(0);
3646
+ for (const m of charClassMembers(boundary)) {
3647
+ if (m === "\\w") {
3648
+ if (/[A-Za-z0-9_]/.test(ch2)) return true;
3649
+ continue;
3650
+ }
3651
+ if (m === "\\d") {
3652
+ if (/[0-9]/.test(ch2)) return true;
3653
+ continue;
3654
+ }
3655
+ if (m === "\\s") {
3656
+ if (/\s/.test(ch2)) return true;
3657
+ continue;
3658
+ }
3659
+ const range = /^(.|\\u[0-9a-f]{4})-(.|\\u[0-9a-f]{4})$/.exec(m);
3660
+ if (range !== null) {
3661
+ const dec = (t) => t.startsWith("\\u") ? parseInt(t.slice(2), 16) : t.codePointAt(0);
3662
+ if (cp >= dec(range[1]) && cp <= dec(range[2])) return true;
3663
+ continue;
3664
+ }
3665
+ if (m === ch2) return true;
3666
+ }
3667
+ return false;
3668
+ }
3669
+ function keywordAlternationHazards(words, boundary) {
3670
+ const out = [];
3671
+ for (let i = 0; i < words.length; i++) {
3672
+ for (let j = i + 1; j < words.length; j++) {
3673
+ const a = words[i], b = words[j];
3674
+ if (a.length >= b.length || !b.startsWith(a)) continue;
3675
+ const at = b[a.length];
3676
+ out.push({ shorter: a, longer: b, at, rescuedByBoundary: boundary !== null && boundaryMatches(boundary, at) });
3677
+ }
3678
+ }
3679
+ return out;
3680
+ }
3681
+ function analyzeDuplication(entry, opts) {
3682
+ return analyzeDuplicationRules([[ruleNameOf2(entry) ?? opts?.entryName ?? "<entry>", entry]], opts);
3683
+ }
3684
+ function assertAnalyzable(ruleMap) {
3685
+ for (const [name, rule] of ruleMap) {
3686
+ const def = rule?._def;
3687
+ if (def !== null && typeof def === "object" && typeof def.tag === "string") continue;
3688
+ throw new TypeError(
3689
+ `analyzeDuplicationRules: rule '${name}' is not a combinator (no _def). This analysis walks the COMBINATOR TREE; the value returned by compose()/composeLeaf() is a fused, already-compiled artifact whose entries are parse functions. Pass the rules() map itself (or the map given to compileRuleMap/compileLinkable) instead \u2014 for a composed grammar, analyze each piece's own rules() map.`
3690
+ );
3691
+ }
3692
+ }
3693
+ function analyzeDuplicationRules(ruleMap, opts) {
3694
+ assertAnalyzable(ruleMap);
3695
+ const minSize = opts?.minSize ?? 3;
3696
+ const maxFindings = opts?.maxFindings ?? 25;
3697
+ const interner = new Interner();
3698
+ const shapeCache = /* @__PURE__ */ new Map();
3699
+ const sizeCache = /* @__PURE__ */ new Map();
3700
+ const shapeOf = (p) => {
3701
+ const hit = shapeCache.get(p);
3702
+ if (hit !== void 0) return hit;
3703
+ const d = p._def;
3704
+ shapeCache.set(p, interner.hole);
3705
+ const kids = childrenOf(d);
3706
+ const id = interner.of(`${payloadKey(p, d)}|${kids.map(shapeOf).join(",")}`);
3707
+ shapeCache.set(p, id);
3708
+ return id;
3709
+ };
3710
+ const sizeOf = (p) => {
3711
+ const hit = sizeCache.get(p);
3712
+ if (hit !== void 0) return hit;
3713
+ sizeCache.set(p, 1);
3714
+ let n = 1;
3715
+ for (const k of childrenOf(p._def)) n += sizeOf(k);
3716
+ sizeCache.set(p, n);
3717
+ return n;
3718
+ };
3719
+ const visited = /* @__PURE__ */ new Map();
3720
+ const visit = (p, rule, path, enclosingNode, enclosingReducer) => {
3721
+ if (visited.has(p)) return;
3722
+ const d = p._def;
3723
+ const own = ruleNameOf2(p);
3724
+ const here = own !== void 0 && own !== rule ? { rule: own, path: "" } : { rule, path };
3725
+ visited.set(p, { p, d, site: here, shape: shapeOf(p), size: sizeOf(p), enclosingNode, enclosingReducer });
3726
+ const inner = d.tag === "node" ? d.type ?? "(anonymous)" : enclosingNode;
3727
+ const innerReducer = enclosingReducer || d.tag === "node" && (d.build !== void 0 || d.buildSrc !== void 0) || d.tag === "transform" || d.tag === "leaf";
3728
+ const kids = childrenOf(d);
3729
+ kids.forEach((k, i) => {
3730
+ const seg = segment(d, i);
3731
+ visit(k, here.rule, here.path === "" ? seg : `${here.path} \u203A ${seg}`, inner, innerReducer);
3732
+ });
3733
+ if (d.tag === "lazy") {
3734
+ try {
3735
+ const target = d.thunk();
3736
+ visit(target, ruleNameOf2(target) ?? ruleNameOf2(p) ?? here.rule, "", null, false);
3737
+ } catch {
3738
+ }
3739
+ }
3740
+ };
3741
+ for (const [name, root] of ruleMap) visit(root, ruleNameOf2(root) ?? name, "", null, false);
3742
+ const accept = new Set(opts?.accept ?? []);
3743
+ const used = /* @__PURE__ */ new Set();
3744
+ const keep = (f) => {
3745
+ if (!accept.has(f.id)) return true;
3746
+ used.add(f.id);
3747
+ return false;
3748
+ };
3749
+ const duplicates = findExactDuplicates(visited, minSize);
3750
+ const nearDuplicates = findNearDuplicates(visited, interner, shapeOf, minSize);
3751
+ const regexFragments = findRegexFragments(visited);
3752
+ const regexClasses = findRegexClasses(visited);
3753
+ const overlaps = findOverlaps(visited, shapeOf, opts?.resolveRef);
3754
+ const rewrites = findRewrites(visited, shapeOf);
3755
+ const divergentNodes = findDivergentNodes(visited, shapeOf);
3756
+ const structureLoss = findStructureLoss(visited, opts?.resolveRef);
3757
+ const keywordRegexes = findKeywordRegexes(visited);
3758
+ const subsume = (fs) => {
3759
+ const covered = /* @__PURE__ */ new Set();
3760
+ const surviving = /* @__PURE__ */ new Set();
3761
+ for (const f of [...fs].sort((a, b) => b.savings - a.savings)) {
3762
+ if (f._members.every((m) => covered.has(m))) continue;
3763
+ surviving.add(f.id);
3764
+ for (const m of f._members) markSubtree(m, covered);
3765
+ }
3766
+ return surviving;
3767
+ };
3768
+ const survivingExact = subsume(duplicates);
3769
+ const survivingNear = subsume(nearDuplicates);
3770
+ const report = {
3771
+ duplicates: duplicates.filter((f) => survivingExact.has(f.id)).filter(keep).slice(0, maxFindings).map(stripMembers),
3772
+ nearDuplicates: nearDuplicates.filter((f) => survivingNear.has(f.id)).filter(keep).slice(0, maxFindings).map(stripMembers),
3773
+ regexFragments: regexFragments.filter(keep).slice(0, maxFindings),
3774
+ regexClasses: regexClasses.filter(keep).slice(0, maxFindings),
3775
+ overlaps: overlaps.filter(keep).slice(0, maxFindings),
3776
+ rewrites: rewrites.filter(keep).slice(0, maxFindings),
3777
+ divergentNodes: divergentNodes.filter(keep).slice(0, maxFindings),
3778
+ structureLoss: structureLoss.filter(keep).slice(0, maxFindings),
3779
+ keywordRegexes: keywordRegexes.filter(keep).slice(0, maxFindings),
3780
+ acceptedUnused: [...accept].filter((id) => !used.has(id)),
3781
+ stats: { rules: ruleMap.length, nodes: visited.size, shapes: new Set([...visited.values()].map((v2) => v2.shape)).size }
3782
+ };
3783
+ return report;
3784
+ }
3785
+ function markSubtree(p, into) {
3786
+ if (into.has(p)) return;
3787
+ into.add(p);
3788
+ for (const k of childrenOf(p._def)) markSubtree(k, into);
3789
+ }
3790
+ var stripMembers = (f) => {
3791
+ const { _members, ...rest } = f;
3792
+ void _members;
3793
+ return rest;
3794
+ };
3795
+ function findExactDuplicates(visited, minSize) {
3796
+ const byShape = /* @__PURE__ */ new Map();
3797
+ for (const v2 of visited.values()) {
3798
+ if (v2.size < minSize) continue;
3799
+ const list = byShape.get(v2.shape);
3800
+ if (list === void 0) byShape.set(v2.shape, [v2]);
3801
+ else list.push(v2);
3802
+ }
3803
+ const out = [];
3804
+ for (const group of byShape.values()) {
3805
+ if (group.length < 2) continue;
3806
+ const size = group[0].size;
3807
+ out.push({
3808
+ kind: "exact-duplicate",
3809
+ id: `exact-duplicate:${siteToString(group[0].site)}`,
3810
+ size,
3811
+ count: group.length,
3812
+ savings: (group.length - 1) * size,
3813
+ shape: clamp(render(group[0].p)),
3814
+ sites: group.map((g) => g.site),
3815
+ suggestion: `${group.length} DISTINCT instances build this identical ${size}-node shape. Bind it to one const and reference that \u2014 hoisting removes ${(group.length - 1) * size} nodes and makes a future edit land in one place instead of ${group.length}.`,
3816
+ _members: group.map((g) => g.p)
3817
+ });
3818
+ }
3819
+ return out.sort((a, b) => b.savings - a.savings);
3820
+ }
3821
+ function findNearDuplicates(visited, interner, shapeOf, minSize) {
3822
+ const holeCache = /* @__PURE__ */ new Map();
3823
+ const holesOf = (p) => {
3824
+ const hit = holeCache.get(p);
3825
+ if (hit !== void 0) return hit;
3826
+ holeCache.set(p, []);
3827
+ const d = p._def;
3828
+ const kids = childrenOf(d);
3829
+ const kidShapes = kids.map(shapeOf);
3830
+ const payload = payloadKey(p, d);
3831
+ const out2 = [];
3832
+ kids.forEach((k, i) => {
3833
+ for (const inner of [interner.hole, ...holesOf(k)]) {
3834
+ const arr = kidShapes.slice();
3835
+ arr[i] = inner;
3836
+ out2.push(interner.of(`${payload}|${arr.join(",")}`));
3837
+ }
3838
+ });
3839
+ holeCache.set(p, out2);
3840
+ return out2;
3841
+ };
3842
+ const byHole = /* @__PURE__ */ new Map();
3843
+ for (const v2 of visited.values()) {
3844
+ if (v2.size < minSize) continue;
3845
+ for (const h of holesOf(v2.p)) {
3846
+ const list = byHole.get(h);
3847
+ if (list === void 0) byHole.set(h, [v2]);
3848
+ else list.push(v2);
3849
+ }
3850
+ }
3851
+ const out = [];
3852
+ const seenGroups = /* @__PURE__ */ new Set();
3853
+ for (const group of byHole.values()) {
3854
+ if (group.length < 2) continue;
3855
+ const shapes = new Set(group.map((g) => g.shape));
3856
+ if (shapes.size < 2) continue;
3857
+ const perShape = /* @__PURE__ */ new Map();
3858
+ for (const g of group) if (!perShape.has(g.shape)) perShape.set(g.shape, g);
3859
+ const members = [...perShape.values()];
3860
+ const key = members.map((m) => m.shape).sort((a, b) => a - b).join(",");
3861
+ if (seenGroups.has(key)) continue;
3862
+ seenGroups.add(key);
3863
+ const slot = findDivergentSlot(members.map((m) => m.p), shapeOf);
3864
+ if (slot === null) continue;
3865
+ const sharedSize = Math.min(...members.map((m) => m.size)) - slot.slotSize;
3866
+ if (sharedSize < minSize) continue;
3867
+ out.push({
3868
+ kind: "near-duplicate",
3869
+ id: `near-duplicate:${siteToString(members[0].site)}`,
3870
+ sharedSize,
3871
+ count: members.length,
3872
+ savings: (members.length - 1) * sharedSize,
3873
+ shape: clamp(renderWithHole(members[0].p, slot.path)),
3874
+ slotPath: slot.path.map((i, k) => segment(slot.defs[k], i)).join(" \u203A "),
3875
+ variants: slot.variants.map((v2) => clamp(render(v2))),
3876
+ sites: members.map((m) => m.site),
3877
+ suggestion: `${members.length} productions share this ${sharedSize}-node scaffolding and differ ONLY at \`${slot.path.map((i, k) => segment(slot.defs[k], i)).join(" \u203A ")}\`. That is one production whose varying slot is a \`choice(${slot.variants.map((v2) => clamp(render(v2, 1))).join(", ")})\` \u2014 not ${members.length} copies of the scaffolding. Re-check the AST: a single production emits one node type where the clones may emit several.`,
3878
+ _members: members.map((m) => m.p)
3879
+ });
3880
+ }
3881
+ return out.sort((a, b) => b.savings - a.savings);
3882
+ }
3883
+ function findDivergentSlot(members, shapeOf) {
3884
+ const path = [];
3885
+ const defs = [];
3886
+ let cur = [...members];
3887
+ for (let guard2 = 0; guard2 < 64; guard2++) {
3888
+ const d0 = cur[0]._def;
3889
+ const kidLists = cur.map((c) => childrenOf(c._def));
3890
+ const arity = kidLists[0].length;
3891
+ if (kidLists.some((k) => k.length !== arity) || arity === 0) break;
3892
+ const diff = [];
3893
+ for (let i2 = 0; i2 < arity; i2++) {
3894
+ const s = shapeOf(kidLists[0][i2]);
3895
+ if (kidLists.some((k) => shapeOf(k[i2]) !== s)) diff.push(i2);
3896
+ }
3897
+ if (diff.length !== 1) break;
3898
+ const i = diff[0];
3899
+ path.push(i);
3900
+ defs.push(d0);
3901
+ cur = kidLists.map((k) => k[i]);
3902
+ const nextArity = childrenOf(cur[0]._def).length;
3903
+ const samePayload = cur.every((c) => payloadKey(c, c._def) === payloadKey(cur[0], cur[0]._def));
3904
+ if (!samePayload || nextArity === 0) break;
3905
+ }
3906
+ if (path.length === 0) return null;
3907
+ let slotSize = 0;
3908
+ const sizeOf = (p) => {
3909
+ let n = 1;
3910
+ for (const k of childrenOf(p._def)) n += sizeOf(k);
3911
+ return n;
3912
+ };
3913
+ for (const c of cur) slotSize = Math.max(slotSize, sizeOf(c));
3914
+ return { path, defs, variants: cur, slotSize };
3915
+ }
3916
+ function renderWithHole(p, path, depth = 0) {
3917
+ if (depth === path.length) return "\u2039slot\u203A";
3918
+ const d = p._def;
3919
+ const kids = childrenOf(d);
3920
+ const idx = path[depth];
3921
+ const parts = kids.map((k, i) => i === idx ? renderWithHole(k, path, depth + 1) : render(k, 2));
3922
+ switch (d.tag) {
3923
+ case "sequence":
3924
+ return `sequence(${parts.join(", ")})`;
3925
+ case "choice":
3926
+ return `choice(${parts.join(", ")})`;
3927
+ case "node":
3928
+ return `node(${d.type ? `'${d.type}', ` : ""}${parts[0] ?? ""})`;
3929
+ default:
3930
+ return `${d.tag}(${parts.join(", ")})`;
3931
+ }
3932
+ }
3933
+ var MAX_RUN = 32;
3934
+ function findRegexFragments(visited) {
3935
+ const byRun = /* @__PURE__ */ new Map();
3936
+ for (const v2 of visited.values()) {
3937
+ const d = v2.d;
3938
+ if (d.tag !== "regex") continue;
3939
+ const runs = /* @__PURE__ */ new Set();
3940
+ for (const branches of alternationGroups(d.source)) {
3941
+ if (branches.length >= 2) runs.add(branches.join("|"));
3942
+ for (let i = 0; i < branches.length; i++) {
3943
+ for (let n = 2; n <= Math.min(MAX_RUN, branches.length - i); n++) {
3944
+ const run2 = branches.slice(i, i + n).join("|");
3945
+ if (run2.length < 4) continue;
3946
+ runs.add(run2);
3947
+ }
3948
+ }
3949
+ }
3950
+ for (const run2 of runs) {
3951
+ let e = byRun.get(run2);
3952
+ if (e === void 0) {
3953
+ e = { occ: [], nodes: /* @__PURE__ */ new Set(), branches: run2.split("|").length };
3954
+ byRun.set(run2, e);
3955
+ }
3956
+ if (e.nodes.has(v2.p)) continue;
3957
+ e.nodes.add(v2.p);
3958
+ e.occ.push({ site: v2.site, source: d.source });
3959
+ }
3960
+ }
3961
+ const candidates = [...byRun.entries()].filter(([, e]) => e.occ.length >= 2).map(([run2, e]) => ({ run: run2, e, savings: (e.occ.length - 1) * run2.length })).sort((a, b) => b.e.occ.length - a.e.occ.length || b.savings - a.savings || b.run.length - a.run.length);
3962
+ const kept = [];
3963
+ for (const c of candidates) {
3964
+ if (kept.some((k) => k.e.occ.length >= c.e.occ.length && `|${k.run}|`.includes(`|${c.run}|`))) continue;
3965
+ kept.push(c);
3966
+ }
3967
+ return kept.map(({ run: run2, e }) => ({
3968
+ kind: "regex-fragment",
3969
+ id: `regex-fragment:${run2}`,
3970
+ fragment: run2,
3971
+ branches: e.branches,
3972
+ count: e.occ.length,
3973
+ savings: (e.occ.length - 1) * run2.length,
3974
+ sources: e.occ.map((o) => `/${o.source}/`),
3975
+ sites: e.occ.map((o) => o.site),
3976
+ suggestion: `this ${e.branches}-branch alternation is re-spelled in ${e.occ.length} DISTINCT regex() terminals. Hoist it to one shared source string and interpolate it (\`new RegExp(\\\`\u2026\\\${OP}\u2026\\\`)\`) or lift the shared part into its own terminal \u2014 an edit to the operator set currently has to land in ${e.occ.length} places, and the analysis cannot tell you when one of them drifts.`
3977
+ }));
3978
+ }
3979
+ function extractCharClasses(src) {
3980
+ const out = [];
3981
+ let i = 0;
3982
+ while (i < src.length) {
3983
+ const c = src[i];
3984
+ if (c === "\\") {
3985
+ i += 2;
3986
+ continue;
3987
+ }
3988
+ if (c !== "[") {
3989
+ i++;
3990
+ continue;
3991
+ }
3992
+ let j = i + 1;
3993
+ if (src[j] === "^") j++;
3994
+ if (src[j] === "]") j++;
3995
+ while (j < src.length && src[j] !== "]") {
3996
+ if (src[j] === "\\") j++;
3997
+ j++;
3998
+ }
3999
+ out.push(src.slice(i + 1, j));
4000
+ i = j + 1;
4001
+ }
4002
+ return out;
4003
+ }
4004
+ function charClassMembers(body) {
4005
+ const esc = (ch2) => {
4006
+ const cp = ch2.codePointAt(0);
4007
+ return cp >= 128 ? `\\u${cp.toString(16).padStart(4, "0")}` : ch2;
4008
+ };
4009
+ const atoms = [];
4010
+ let i = 0;
4011
+ while (i < body.length) {
4012
+ if (body[i] === "\\") {
4013
+ const m = /^\\(?:u\{([0-9a-fA-F]+)\}|u([0-9a-fA-F]{4})|x([0-9a-fA-F]{2}))/.exec(body.slice(i));
4014
+ if (m !== null) {
4015
+ const cp = parseInt(m[1] ?? m[2] ?? m[3], 16);
4016
+ atoms.push(cp >= 128 ? `\\u${cp.toString(16).padStart(4, "0")}` : String.fromCodePoint(cp));
4017
+ i += m[0].length;
4018
+ continue;
4019
+ }
4020
+ atoms.push(body.slice(i, i + 2));
4021
+ i += 2;
4022
+ continue;
4023
+ }
4024
+ atoms.push(esc(body[i]));
4025
+ i++;
4026
+ }
4027
+ const members = [];
4028
+ for (let k = 0; k < atoms.length; k++) {
4029
+ if (k + 2 < atoms.length && atoms[k + 1] === "-" && atoms[k + 2] !== void 0) {
4030
+ members.push(`${atoms[k]}-${atoms[k + 2]}`);
4031
+ k += 2;
4032
+ continue;
4033
+ }
4034
+ members.push(atoms[k]);
4035
+ }
4036
+ return members;
4037
+ }
4038
+ var CLASS_MIN_MEMBERS = 2;
4039
+ function findRegexClasses(visited) {
4040
+ const spellings = /* @__PURE__ */ new Map();
4041
+ for (const v2 of visited.values()) {
4042
+ if (v2.d.tag !== "regex") continue;
4043
+ for (const body of new Set(extractCharClasses(v2.d.source))) {
4044
+ const members = charClassMembers(body);
4045
+ if (members.length < CLASS_MIN_MEMBERS) continue;
4046
+ const key = [...new Set(members)].sort().join("");
4047
+ let s = spellings.get(key);
4048
+ if (s === void 0) {
4049
+ s = { source: body, members, key, nodes: /* @__PURE__ */ new Set(), sites: [] };
4050
+ spellings.set(key, s);
4051
+ }
4052
+ if (s.nodes.has(v2.p)) continue;
4053
+ s.nodes.add(v2.p);
4054
+ s.sites.push(v2.site);
4055
+ }
4056
+ }
4057
+ const MAX_DELTA = 3;
4058
+ const MIN_JACCARD = 0.6;
4059
+ const all = [...spellings.values()];
4060
+ const parent = all.map((_, i) => i);
4061
+ const find = (i) => {
4062
+ while (parent[i] !== i) {
4063
+ parent[i] = parent[parent[i]];
4064
+ i = parent[i];
4065
+ }
4066
+ return i;
4067
+ };
4068
+ const union2 = (a, b) => {
4069
+ const ra = find(a), rb = find(b);
4070
+ if (ra !== rb) parent[ra] = rb;
4071
+ };
4072
+ for (let i = 0; i < all.length; i++) for (let j = i + 1; j < all.length; j++) {
4073
+ if (all[i].members.includes("^") !== all[j].members.includes("^")) continue;
4074
+ const A = new Set(all[i].members), B = new Set(all[j].members);
4075
+ let shared = 0;
4076
+ for (const m of A) if (B.has(m)) shared++;
4077
+ const delta = A.size - shared + (B.size - shared);
4078
+ const jaccard = shared / (shared + delta);
4079
+ if (shared >= 2 && delta > 0 && delta <= MAX_DELTA && jaccard >= MIN_JACCARD) union2(i, j);
4080
+ }
4081
+ const clusters = /* @__PURE__ */ new Map();
4082
+ all.forEach((_, i) => {
4083
+ const r = find(i);
4084
+ const list = clusters.get(r);
4085
+ if (list === void 0) clusters.set(r, [i]);
4086
+ else list.push(i);
4087
+ });
4088
+ const out = [];
4089
+ for (const idxs of clusters.values()) {
4090
+ const members = idxs.map((i) => all[i]).sort((a, b) => b.nodes.size - a.nodes.size);
4091
+ const total = members.reduce((n, m) => n + m.nodes.size, 0);
4092
+ const drifted = members.length > 1;
4093
+ if (!drifted && total < 2) continue;
4094
+ const base = new Set(members[0].members);
4095
+ const variants = members.map((m) => {
4096
+ const mine = new Set(m.members);
4097
+ const plus = [...mine].filter((x) => !base.has(x));
4098
+ const minus = [...base].filter((x) => !mine.has(x));
4099
+ return {
4100
+ source: m.source,
4101
+ delta: m === members[0] ? "(reference)" : `${plus.map((p) => `+\`${p}\``).join(" ")}${plus.length > 0 && minus.length > 0 ? " " : ""}${minus.map((p) => `-\`${p}\``).join(" ")}` || "(same members, different spelling)",
4102
+ count: m.nodes.size,
4103
+ sites: m.sites
4104
+ };
4105
+ });
4106
+ const bmpCeiling = members.some((m) => m.members.some((x) => x.endsWith("-\\uffff")));
4107
+ out.push({
4108
+ kind: "regex-class",
4109
+ id: `regex-class:${members[0].key}`,
4110
+ canonical: members[0].source,
4111
+ variants,
4112
+ count: total,
4113
+ drifted,
4114
+ bmpCeiling,
4115
+ suggestion: [
4116
+ drifted ? `${members.length} NEARLY-identical spellings of one class, across ${total} regex() terminals. They differ by ${variants.slice(1).map((v2) => v2.delta).join("; ")} \u2014 which is drift, not intent, unless every difference is deliberate. Two classes this close cannot be told apart by reading, so one of them is probably wrong and the tests cannot say which.` : `this class is re-spelled in ${total} DISTINCT regex() terminals. Extract it once \u2014 a named sub-leaf, or an interpolated source constant \u2014 so a range fix lands in one place. Re-spelling is how the near-identical variants elsewhere in this report came to exist.`,
4117
+ `Prefer the shared recognition surface where one already covers this class rather than a local extraction.`,
4118
+ bmpCeiling ? `The class tops out at U+FFFF, so astral-plane characters (emoji, many CJK extensions) fall OUTSIDE it. Widening the ceiling is exactly the kind of edit that has to reach every spelling.` : "",
4119
+ `Single-regex hygiene (duplicate class members, obscure ranges, a missing \`u\` flag) is \`eslint-plugin-regexp\`'s job and is not duplicated here \u2014 it sees one regex at a time, which is why it cannot see this.`
4120
+ ].filter((s) => s !== "").join(" ")
4121
+ });
4122
+ }
4123
+ return out.sort((a, b) => Number(b.drifted) - Number(a.drifted) || b.count - a.count);
4124
+ }
4125
+ function intersects3(a, b) {
4126
+ if (a.kind === "any" || b.kind === "any") return true;
4127
+ if (a.kind === "empty" || b.kind === "empty") return false;
4128
+ for (const ra of a.ranges) for (const rb of b.ranges) if (ra.lo <= rb.hi && rb.lo <= ra.hi) return true;
4129
+ return false;
4130
+ }
4131
+ function intersection2(a, b) {
4132
+ if (a.kind === "any") return b;
4133
+ if (b.kind === "any") return a;
4134
+ if (a.kind === "empty" || b.kind === "empty") return { kind: "empty" };
4135
+ const ranges = [];
4136
+ for (const ra of a.ranges) for (const rb of b.ranges) {
4137
+ const lo = Math.max(ra.lo, rb.lo);
4138
+ const hi = Math.min(ra.hi, rb.hi);
4139
+ if (lo <= hi) ranges.push({ lo, hi });
4140
+ }
4141
+ return ranges.length === 0 ? { kind: "empty" } : { kind: "ranges", ranges };
4142
+ }
4143
+ function termsOf(arm) {
4144
+ let cur = arm;
4145
+ for (; ; ) {
4146
+ const d2 = cur._def;
4147
+ if (d2.tag === "node" || d2.tag === "transform" || d2.tag === "label" || d2.tag === "attempt" || d2.tag === "leaf") {
4148
+ cur = d2.parser;
4149
+ continue;
4150
+ }
4151
+ break;
4152
+ }
4153
+ const d = cur._def;
4154
+ return d.tag === "sequence" ? d.parsers : [cur];
4155
+ }
4156
+ function leadingRegex(arm) {
4157
+ const t = termsOf(arm)[0];
4158
+ return t !== void 0 && t._def.tag === "regex";
4159
+ }
4160
+ function findOverlaps(visited, shapeOf, resolve) {
4161
+ const out = [];
4162
+ for (const v2 of visited.values()) {
4163
+ const d = v2.d;
4164
+ if (d.tag !== "choice") continue;
4165
+ const arms = d.parsers;
4166
+ const fs = arms.map((a) => firstSetOf(a, /* @__PURE__ */ new Set(), resolve));
4167
+ const strategy = d.strategy;
4168
+ for (let i = 0; i < arms.length; i++) {
4169
+ for (let j = i + 1; j < arms.length; j++) {
4170
+ const A = fs[i], B = fs[j];
4171
+ if (A.kind === "any" || B.kind === "any") continue;
4172
+ if (!intersects3(A, B)) continue;
4173
+ const ta = termsOf(arms[i]), tb = termsOf(arms[j]);
4174
+ let shared = 0;
4175
+ while (shared < ta.length && shared < tb.length && shapeOf(ta[shared]) === shapeOf(tb[shared])) shared++;
4176
+ const handled = strategy?.tag === "sharedPrefix" && Array.isArray(strategy.members) && strategy.members.includes(i) && strategy.members.includes(j);
4177
+ const regexPair = leadingRegex(arms[i]) && leadingRegex(arms[j]);
4178
+ out.push({
4179
+ kind: "arm-overlap",
4180
+ id: `arm-overlap:${siteToString(v2.site)}:${i}-${j}`,
4181
+ site: v2.site,
4182
+ a: i,
4183
+ b: j,
4184
+ on: intersection2(A, B),
4185
+ sharedLeadingTerms: shared,
4186
+ sharedPrefix: shared > 0 ? clamp(ta.slice(0, shared).map((t) => render(t, 2)).join(", ")) : null,
4187
+ handledByStrategy: handled,
4188
+ regexPair,
4189
+ suggestion: overlapSuggestion(shared, handled, regexPair)
4190
+ });
4191
+ }
4192
+ }
4193
+ }
4194
+ return out.sort((a, b) => b.sharedLeadingTerms - a.sharedLeadingTerms);
4195
+ }
4196
+ function overlapSuggestion(shared, handled, regexPair) {
4197
+ if (shared > 0 && handled)
4198
+ return `the arms spell ${shared} identical leading term(s). parseman's \`sharedPrefix\` strategy already recognizes that prefix ONCE at runtime, so this costs nothing at parse time \u2014 it is a READABILITY finding: left-factoring into \`sequence(prefix, choice(\u2026))\` says in the grammar what the compiler had to infer.`;
4199
+ if (shared > 0)
4200
+ return `the arms spell ${shared} identical leading term(s) but do NOT qualify for the \`sharedPrefix\` strategy (it needs every arm to be a bare \`sequence\` starting with the same concrete terminal). Left-factor into \`sequence(prefix, choice(\u2026))\` \u2014 that both removes the re-spelling and lets the inner choice dispatch on its own first char.`;
4201
+ if (regexPair)
4202
+ return `two regex() arms whose character classes intersect. Nothing dispatches between them: the first is tried, and on failure the second re-scans the same position. Two regexes that can start on the same char are usually one terminal \u2014 merge them, or make the distinction explicit with a leading literal.`;
4203
+ return `the arms can start on the same character, so the choice cannot dispatch between them: on those chars the first arm is entered speculatively and rolled back before the second is tried. Give the arms disjoint leading terminals, or left-factor the common part out.`;
4204
+ }
4205
+ var PERF_ARM = "removes a speculative arm: on every input position reaching this choice, the deleted arm no longer costs a ctx save, a child-array push and a rollback before the surviving arm is tried. The saving scales with how often the choice is reached and how much the doomed arm consumes before failing.";
4206
+ var PERF_NONE = "";
4207
+ function findRewrites(visited, shapeOf) {
4208
+ const out = [];
4209
+ const push = (f, at) => {
4210
+ const site = siteToString(f.site);
4211
+ out.push({ kind: "rewrite", id: `rewrite:${f.rewrite}:${site}${at === void 0 ? "" : `:${at}`}`, ...f });
4212
+ };
4213
+ for (const v2 of visited.values()) {
4214
+ const d = v2.d;
4215
+ const site = v2.site;
4216
+ if (d.tag === "optional" || d.tag === "many" || d.tag === "oneOrMore") {
4217
+ const inner = d.parser;
4218
+ const id = inner._def;
4219
+ const collapsed = collapseRepeat(d, id);
4220
+ if (collapsed !== null) {
4221
+ push({
4222
+ rewrite: "idempotent-nesting",
4223
+ site,
4224
+ from: clamp(render(v2.p, 2)),
4225
+ to: collapsed,
4226
+ astNeutral: false,
4227
+ perf: "the outer wrapper still runs its own bookkeeping (and, for `many(optional(X))`, guards a zero-width iteration on every pass) for a nullability the inner combinator already has.",
4228
+ bug: id.tag === "optional" && d.tag === "many",
4229
+ suggestion: `\`${clamp(render(v2.p, 2))}\` is exactly \`${collapsed}\`. ${id.tag === "optional" && d.tag === "many" ? "The nested form is also a zero-width-iteration hazard: the inner `optional` succeeds consuming nothing, which the repeat has to detect and break on every pass." : ""} Candidate \u2014 verify AST identity: the collapsed form produces ONE level of array/undefined where the nested form produces two.`
4230
+ });
4231
+ }
4232
+ }
4233
+ if ((d.tag === "choice" || d.tag === "sequence") && d.parsers.length === 1) {
4234
+ push({
4235
+ rewrite: "single-element",
4236
+ site,
4237
+ from: clamp(render(v2.p, 2)),
4238
+ to: d.tag === "choice" ? clamp(render(d.parsers[0], 2)) : `${clamp(render(d.parsers[0], 2))} (note: sequence() of one still wraps its value in a 1-tuple)`,
4239
+ astNeutral: d.tag === "choice",
4240
+ perf: d.tag === "choice" ? "a one-arm choice still emits its dispatch scaffolding." : PERF_NONE,
4241
+ bug: false,
4242
+ suggestion: d.tag === "choice" ? "a `choice` with one arm IS that arm \u2014 the wrapper only adds dispatch scaffolding. Safe to unwrap (choice forwards its arm value unchanged)." : "a `sequence` with one element only exists to build a 1-tuple. If nothing reads that tuple, drop the wrapper \u2014 CANDIDATE, verify AST identity first."
4243
+ });
4244
+ }
4245
+ if (d.tag !== "choice") continue;
4246
+ const arms = d.parsers;
4247
+ const armShapes = arms.map(shapeOf);
4248
+ const strategy = d.strategy;
4249
+ for (let i = 0; i < arms.length; i++) for (let j = i + 1; j < arms.length; j++) {
4250
+ if (armShapes[i] !== armShapes[j]) continue;
4251
+ push({
4252
+ rewrite: "duplicate-arm",
4253
+ site,
4254
+ from: `choice(\u2026 arm[${i}] \u2026, arm[${j}] \u2026)`,
4255
+ to: `delete arm[${j}]`,
4256
+ astNeutral: true,
4257
+ perf: PERF_ARM,
4258
+ bug: true,
4259
+ suggestion: `arm[${i}] and arm[${j}] are the SAME shape (\`${clamp(render(arms[i], 2))}\`). Under ordered choice arm[${i}] always wins, so arm[${j}] is unreachable \u2014 delete it. This is AST-neutral by construction: an arm that can never be selected contributes nothing to any parse.`
4260
+ }, `${i}-${j}`);
4261
+ }
4262
+ for (let i = 0; i < arms.length; i++) for (let j = 0; j < arms.length; j++) {
4263
+ if (i >= j) continue;
4264
+ if (armShapes[i] === armShapes[j]) continue;
4265
+ const ta = termsOf(arms[i]), tb = termsOf(arms[j]);
4266
+ if (ta.length === 0 || ta.length >= tb.length) continue;
4267
+ let k = 0;
4268
+ while (k < ta.length && shapeOf(ta[k]) === shapeOf(tb[k])) k++;
4269
+ if (k !== ta.length) continue;
4270
+ push({
4271
+ rewrite: "shadowed-arm",
4272
+ site,
4273
+ from: `choice(\u2026 arm[${i}]=${clamp(render(arms[i], 2))}, \u2026 arm[${j}]=${clamp(render(arms[j], 2))} \u2026)`,
4274
+ to: `arm[${j}] is unreachable`,
4275
+ astNeutral: true,
4276
+ perf: PERF_ARM,
4277
+ bug: true,
4278
+ suggestion: `arm[${i}] is a strict prefix of arm[${j}] and comes FIRST. Whenever arm[${j}] would match, arm[${i}] also matches, and ordered choice commits to it \u2014 so arm[${j}] can never be selected. Either swap them (longest first) or fold arm[${j}]'s extra terms into arm[${i}] as \`optional(\u2026)\`. Verify by hand where a \`not()\`/\`gate()\` in arm[${i}] makes its success input-dependent in a way this static check cannot see.`
4279
+ }, `${i}-${j}`);
4280
+ }
4281
+ for (let i = 0; i + 1 < arms.length; i++) {
4282
+ const j = i + 1;
4283
+ const longer = termsOf(arms[i]), shorter = termsOf(arms[j]);
4284
+ const ordered = detectOptionalTerm(longer, shorter, shapeOf);
4285
+ const reversed = ordered === null ? detectOptionalTerm(shorter, longer, shapeOf) : null;
4286
+ const hit = ordered ?? reversed;
4287
+ if (hit === null) continue;
4288
+ const nodeWrapped = v2.enclosingNode ?? wrappingNodeType(arms[i]) ?? wrappingNodeType(arms[j]);
4289
+ const rest = hit.rest.map((t) => render(t, 2)).join(", ");
4290
+ const to = hit.where === "prefix" ? `sequence(optional(${render(hit.term, 2)}), ${rest})` : `sequence(${rest}, optional(${render(hit.term, 2)}))`;
4291
+ push({
4292
+ rewrite: hit.where === "prefix" ? "optional-prefix" : "optional-suffix",
4293
+ site,
4294
+ from: clamp(render(v2.p, 2)),
4295
+ to: clamp(to),
4296
+ astNeutral: false,
4297
+ perf: PERF_ARM,
4298
+ bug: false,
4299
+ suggestion: `these two arms differ only by a ${hit.where === "prefix" ? "leading" : "trailing"} \`${clamp(render(hit.term, 2))}\`, so the choice is exactly \`${clamp(to)}\` \u2014 same language, one arm instead of two.${reversed !== null ? ` NOTE THE ORDER: the shorter arm is FIRST, so under ordered choice the longer arm is only reachable when the shorter one fails \u2014 check that is what you meant.` : ""} CANDIDATE \u2014 verify AST identity before applying:${nodeWrapped === null ? "" : ` this sits under \`node('${nodeWrapped}')\`, and`} the two forms produce different child arrays (two children vs. one child plus an absent optional). If a build fn or a downstream consumer reads children positionally, the rewrite MOVES the tree.`
4300
+ }, `${i}-${j}`);
4301
+ }
4302
+ for (let i = 0; i + 1 < arms.length; i++) {
4303
+ const j = i + 1;
4304
+ const ta = termsOf(arms[i]), tb = termsOf(arms[j]);
4305
+ if (ta.length < 2 || tb.length < 2) continue;
4306
+ let k = 0;
4307
+ while (k < ta.length - 1 && k < tb.length - 1 && shapeOf(ta[k]) === shapeOf(tb[k])) k++;
4308
+ if (k === 0) continue;
4309
+ const handled = strategy?.tag === "sharedPrefix" && Array.isArray(strategy.members) && strategy.members.includes(i) && strategy.members.includes(j);
4310
+ push({
4311
+ rewrite: "left-factor",
4312
+ site,
4313
+ from: `choice(sequence(${ta.map((t) => render(t, 1)).join(", ")}), sequence(${tb.map((t) => render(t, 1)).join(", ")}))`,
4314
+ to: clamp(`sequence(${ta.slice(0, k).map((t) => render(t, 2)).join(", ")}, choice(sequence(${ta.slice(k).map((t) => render(t, 1)).join(", ")}), sequence(${tb.slice(k).map((t) => render(t, 1)).join(", ")})))`),
4315
+ astNeutral: false,
4316
+ perf: handled ? "NONE \u2014 parseman's `sharedPrefix` choice strategy already recognizes this prefix once and replays it, so the runtime cost is identical. This is a readability finding." : PERF_ARM,
4317
+ bug: false,
4318
+ suggestion: `arm[${i}] and arm[${j}] spell the same ${k} leading term(s). ${handled ? "The `sharedPrefix` strategy already collapses this AT RUNTIME, so left-factoring buys readability, not speed \u2014 but writing it out means the next reader does not have to trust the optimizer." : "Left-factor to `sequence(prefix, choice(tailA, tailB))`: the prefix is then recognized once and the inner choice dispatches on its own first char."} CANDIDATE \u2014 verify AST identity: factoring nests the tails one level deeper in the value.`
4319
+ }, `${i}-${j}`);
4320
+ }
4321
+ }
4322
+ for (const v2 of visited.values()) {
4323
+ if (v2.d.tag !== "sequence") continue;
4324
+ const hit = detectHandRolledSepBy(v2.d.parsers, shapeOf);
4325
+ if (hit === null) continue;
4326
+ const captured = v2.d.parsers.some((t) => capturesPositionally(t));
4327
+ const verdict = captured ? "blocked-by-capture" : v2.enclosingReducer ? "reducer-stride-review" : "convertible";
4328
+ const to = `sepBy(${clamp(render(hit.item, 2))}, ${clamp(render(hit.sep, 2))}${hit.trailing ? `, { trailing: 'allow' }` : ""}, { min: ${hit.min} })`;
4329
+ out.push({
4330
+ kind: "rewrite",
4331
+ id: `rewrite:hand-rolled-sepby:${siteToString(v2.site)}`,
4332
+ rewrite: "hand-rolled-sepby",
4333
+ site: v2.site,
4334
+ sepByVerdict: verdict,
4335
+ from: clamp(render(v2.p, 3)),
4336
+ to: verdict === "convertible" ? to : `${to} \u2014 NOT APPLICABLE HERE (${verdict})`,
4337
+ astNeutral: false,
4338
+ perf: verdict === "convertible" ? "the hand-rolled form allocates a tuple per repetition (`sequence(sep, item)`) and a wrapper array around them; `sepBy` builds the item list directly." : PERF_NONE,
4339
+ bug: false,
4340
+ suggestion: `${SEPBY_SPELLED_OUT(hit.trailing)} ${SEPBY_VERDICT_TEXT[verdict](hit.trailing)}`
4341
+ });
4342
+ }
4343
+ return out;
4344
+ }
4345
+ function collapseRepeat(outer, inner) {
4346
+ const o = outer.tag, i = inner.tag;
4347
+ if (o === "optional" && i === "optional") return "optional(X)";
4348
+ if (o === "optional" && i === "many") return "many(X)";
4349
+ if (o === "many" && i === "many") return "many(X)";
4350
+ if (o === "many" && i === "optional") return "many(X)";
4351
+ if (o === "optional" && i === "oneOrMore" && inner.min === 1 && inner.max === void 0) return "many(X)";
4352
+ if (o === "many" && i === "oneOrMore" && inner.min === 1 && inner.max === void 0) return "many(X)";
4353
+ return null;
4354
+ }
4355
+ function wrappingNodeType(arm) {
4356
+ const d = arm._def;
4357
+ return d.tag === "node" ? d.type ?? "(anonymous)" : null;
4358
+ }
4359
+ function detectOptionalTerm(longer, shorter, shapeOf) {
4360
+ if (longer.length !== shorter.length + 1 || shorter.length === 0) return null;
4361
+ const same = (a, b) => a.length === b.length && a.every((x, i) => shapeOf(x) === shapeOf(b[i]));
4362
+ if (same(longer.slice(1), shorter)) return { where: "prefix", term: longer[0], rest: shorter };
4363
+ if (same(longer.slice(0, -1), shorter)) return { where: "suffix", term: longer[longer.length - 1], rest: shorter };
4364
+ return null;
4365
+ }
4366
+ var SEPBY_SPELLED_OUT = (trailing) => `this is \`sepBy(item, sep${trailing ? ", { trailing: 'allow' }" : ""})\` spelled out.`;
4367
+ var SEPBY_VERDICT_TEXT = {
4368
+ "blocked-by-capture": () => "BLOCKED: the repetition CAPTURES its separator (`field`/`label`), which `sepBy` cannot express \u2014 it yields items and discards separators, so byte-faithful layout replay is lost. Leave this site alone; read it as a parseman gap (a separator-capturing repeat), not as work.",
4369
+ "reducer-stride-review": () => "NEEDS REVIEW, not conversion: an enclosing `node()`/`transform()` reducer reads this repetition's children, and a left-associating reducer typically STRIDES BY TWO over `[item, sep, item, sep, \u2026]` and re-emits the operator text. `sepBy` yields a FLAT ITEM list with the separators gone, so the reducer has to be rewritten in the same change or the tree moves. Convert only together with the reducer.",
4370
+ convertible: (trailing) => `No separator capture, and no enclosing reducer reads these children \u2014 this site is convertible. Beyond the ${trailing ? "four" : "three"} combinators it replaces, \`sepBy\` carries the right NULLABILITY: \`min: 0\` (the default) matches empty and does NOT gate as a choice arm, \`min: 1\` does \u2014 a distinction the hand-rolled shape leaves implicit and gets wrong silently. Still check the consumer: \`sepBy\` yields a flat item list where the hand-rolled form yields \`[first, [[sep, item], \u2026]]\`.`
4371
+ };
4372
+ function capturesPositionally(p, seen = /* @__PURE__ */ new Set()) {
4373
+ if (seen.has(p)) return false;
4374
+ seen.add(p);
4375
+ const d = p._def;
4376
+ if (d.tag === "field" || d.tag === "label") return true;
4377
+ for (const k of childrenOf(d)) if (capturesPositionally(k, seen)) return true;
4378
+ return false;
4379
+ }
4380
+ function detectHandRolledSepBy(terms, shapeOf) {
4381
+ if (terms.length < 2 || terms.length > 3) return null;
4382
+ const item = terms[0];
4383
+ const rep = terms[1]._def;
4384
+ if (rep.tag !== "many" && rep.tag !== "oneOrMore") return null;
4385
+ if (rep.max !== void 0) return null;
4386
+ const innerDef = rep.parser._def;
4387
+ if (innerDef.tag !== "sequence" || innerDef.parsers.length !== 2) return null;
4388
+ const [sep, repeated] = innerDef.parsers;
4389
+ if (shapeOf(repeated) !== shapeOf(item)) return null;
4390
+ let trailing = false;
4391
+ if (terms.length === 3) {
4392
+ const t = terms[2]._def;
4393
+ if (t.tag !== "optional" || shapeOf(t.parser) !== shapeOf(sep)) return null;
4394
+ trailing = true;
4395
+ }
4396
+ return { item, sep, trailing, min: 1 + rep.min };
4397
+ }
4398
+ function bodyTermsOf(p) {
4399
+ let cur = p;
4400
+ for (; ; ) {
4401
+ const d2 = cur._def;
4402
+ if (d2.tag === "node" || d2.tag === "grammar" || d2.tag === "transform" || d2.tag === "label" || d2.tag === "attempt" || d2.tag === "leaf" || d2.tag === "token" || d2.tag === "expect" || d2.tag === "field") {
4403
+ cur = d2.parser;
4404
+ continue;
4405
+ }
4406
+ break;
4407
+ }
4408
+ const d = cur._def;
4409
+ return d.tag === "sequence" ? d.parsers : [cur];
4410
+ }
4411
+ var MIN_SHARED_TERMS = 2;
4412
+ function findDivergentNodes(visited, shapeOf) {
4413
+ const byType = /* @__PURE__ */ new Map();
4414
+ for (const v2 of visited.values()) {
4415
+ if (v2.d.tag !== "node" || v2.d.type === void 0) continue;
4416
+ let m = byType.get(v2.d.type);
4417
+ if (m === void 0) {
4418
+ m = /* @__PURE__ */ new Map();
4419
+ byType.set(v2.d.type, m);
4420
+ }
4421
+ if (!m.has(v2.shape)) m.set(v2.shape, v2);
4422
+ }
4423
+ const out = [];
4424
+ for (const [type, m] of byType) {
4425
+ if (m.size < 2) continue;
4426
+ const members = [...m.values()];
4427
+ const termSets = members.map((v2) => bodyTermsOf(v2.p).map((t) => ({ id: shapeOf(t), text: clamp(render(t, 2)) })));
4428
+ const shared = termSets[0].filter((t) => termSets.every((set) => set.some((x) => x.id === t.id)));
4429
+ const uniqueShared = [...new Map(shared.map((t) => [t.id, t])).values()];
4430
+ if (uniqueShared.length < MIN_SHARED_TERMS) continue;
4431
+ const sharedIds = new Set(uniqueShared.map((t) => t.id));
4432
+ out.push({
4433
+ kind: "divergent-node",
4434
+ id: `divergent-node:${type}`,
4435
+ nodeType: type,
4436
+ count: members.length,
4437
+ sharedTerms: uniqueShared.map((t) => t.text),
4438
+ productions: members.map((v2, i) => ({
4439
+ shape: clamp(render(v2.p, 3)),
4440
+ site: v2.site,
4441
+ distinctTerms: termSets[i].filter((t) => !sharedIds.has(t.id)).map((t) => t.text)
4442
+ })),
4443
+ suggestion: `\`${type}\` is built by ${members.length} structurally different productions that nevertheless spell ${uniqueShared.length} identical term(s) (${uniqueShared.map((t) => `\`${t.text}\``).join(", ")}) \u2014 they are variants of ONE shape, not ${members.length} constructs. Nothing keeps them in sync: an edit to the ${type} shape has to land in all ${members.length}, and no test fails when it lands in one. Fold them into a single production whose varying part is a \`choice\`, or, if the variants exist for a parse-order reason (a fast path tried first), say so at the definition \u2014 the cost of the split is that a reader cannot tell which it is.`
4444
+ });
4445
+ }
4446
+ return out.sort((a, b) => b.sharedTerms.length * b.count - a.sharedTerms.length * a.count);
4447
+ }
4448
+ function resolveArm(p) {
4449
+ let cur = p;
4450
+ const seen = /* @__PURE__ */ new Set();
4451
+ for (; ; ) {
4452
+ if (seen.has(cur)) return cur;
4453
+ seen.add(cur);
4454
+ const d = cur._def;
4455
+ if (d.tag === "label" || d.tag === "attempt") {
4456
+ cur = d.parser;
4457
+ continue;
4458
+ }
4459
+ if (d.tag === "lazy") {
4460
+ try {
4461
+ cur = d.thunk();
4462
+ continue;
4463
+ } catch {
4464
+ return cur;
4465
+ }
4466
+ }
4467
+ return cur;
4468
+ }
4469
+ }
4470
+ function nestedNodeTypes(root, out, seen, isRoot) {
4471
+ if (seen.has(root)) return;
4472
+ seen.add(root);
4473
+ const d = root._def;
4474
+ if (d.tag === "leaf" || d.tag === "token") return;
4475
+ if (d.tag === "node" && !isRoot) out.add(d.type ?? "(anonymous)");
4476
+ if (d.tag === "lazy") {
4477
+ try {
4478
+ nestedNodeTypes(d.thunk(), out, seen, false);
4479
+ } catch {
4480
+ }
4481
+ return;
4482
+ }
4483
+ for (const k of childrenOf(d)) nestedNodeTypes(k, out, seen, false);
4484
+ }
4485
+ var MAX_LOST_TYPES = 8;
4486
+ function findStructureLoss(visited, resolve) {
4487
+ const nestedCache = /* @__PURE__ */ new Map();
4488
+ const nested = (p) => {
4489
+ const hit = nestedCache.get(p);
4490
+ if (hit !== void 0) return hit;
4491
+ const out = /* @__PURE__ */ new Set();
4492
+ nestedNodeTypes(p, out, /* @__PURE__ */ new Set(), true);
4493
+ nestedCache.set(p, out);
4494
+ return out;
4495
+ };
4496
+ const findings = [];
4497
+ for (const v2 of visited.values()) {
4498
+ const d = v2.d;
4499
+ if (d.tag !== "choice") continue;
4500
+ const arms = d.parsers;
4501
+ const resolved = arms.map(resolveArm);
4502
+ const nodeTypes = resolved.map(wrappingNodeType);
4503
+ const fs = arms.map((a) => firstSetOf(a, /* @__PURE__ */ new Set(), resolve));
4504
+ for (let i = 0; i < arms.length; i++) {
4505
+ if (d.gates[i] != null) continue;
4506
+ const type = nodeTypes[i] ?? null;
4507
+ if (type === null) continue;
4508
+ if (nested(resolved[i]).size !== 0) continue;
4509
+ const A = fs[i];
4510
+ if (A.kind === "any") continue;
4511
+ for (let j = i + 1; j < arms.length; j++) {
4512
+ if (nodeTypes[j] !== type) continue;
4513
+ const B = fs[j];
4514
+ if (B.kind === "any" || !intersects3(A, B)) continue;
4515
+ const lost = nested(resolved[j]);
4516
+ if (lost.size === 0) continue;
4517
+ const lostNodeTypes = [...lost].sort();
4518
+ findings.push({
4519
+ kind: "structure-loss",
4520
+ id: `structure-loss:${siteToString(v2.site)}:${i}-${j}`,
4521
+ site: v2.site,
4522
+ nodeType: type,
4523
+ earlier: i,
4524
+ later: j,
4525
+ on: intersection2(A, B),
4526
+ lostNodeTypes: lostNodeTypes.slice(0, MAX_LOST_TYPES),
4527
+ earlierShape: clamp(render(resolved[i], 3)),
4528
+ laterShape: clamp(render(resolved[j], 3)),
4529
+ suggestion: structureLossSuggestion(type, i, j, lostNodeTypes)
4530
+ });
4531
+ }
4532
+ }
4533
+ }
4534
+ return findings.sort((a, b) => b.lostNodeTypes.length - a.lostNodeTypes.length);
4535
+ }
4536
+ function structureLossSuggestion(type, i, j, lost) {
4537
+ const names = lost.slice(0, MAX_LOST_TYPES).map((t) => `\`${t}\``).join(", ");
4538
+ const more = lost.length > MAX_LOST_TYPES ? ` (and ${lost.length - MAX_LOST_TYPES} more)` : "";
4539
+ return `arm[${i}] and arm[${j}] both build \`${type}\` and can start on the same character, so on every input BOTH accept, arm[${i}] wins \u2014 and arm[${i}] contains no \`node()\`, so it yields a \`${type}\` over bare leaves. arm[${j}] would have built ${names}${more} there. The parse still succeeds and the span is still right; only the tree shape changes, and only on the inputs arm[${i}] happens to accept \u2014 so a suite that asserts "it parses" and an output diff both stay green while every consumer of the tree (language service, formatter, refactor) silently loses those nodes on the commonest inputs. Decide which tree is the contract: if the structured one is, DELETE arm[${i}] or move it after arm[${j}]; if the flat one is, it must be flat for ALL inputs of this shape, not just the ones arm[${i}] matches \u2014 a fast path that is not tree-neutral is not a fast path.`;
4540
+ }
4541
+ function findKeywordRegexes(visited) {
4542
+ const armIndexOf = /* @__PURE__ */ new Map();
4543
+ for (const v2 of visited.values()) {
4544
+ if (v2.d.tag !== "choice") continue;
4545
+ v2.d.parsers.forEach((a, i) => {
4546
+ let cur = a;
4547
+ for (; ; ) {
4548
+ const cd = cur._def;
4549
+ if (cd.tag === "node" || cd.tag === "label" || cd.tag === "transform" || cd.tag === "leaf" || cd.tag === "attempt") {
4550
+ cur = cd.parser;
4551
+ continue;
4552
+ }
4553
+ break;
4554
+ }
4555
+ armIndexOf.set(cur, { owner: v2.p, index: i });
4556
+ });
4557
+ }
4558
+ const keywordArms = /* @__PURE__ */ new Map();
4559
+ for (const [arm, { owner }] of armIndexOf) {
4560
+ const d = arm._def;
4561
+ if (d.tag !== "regex" || keywordRegexShape(d.source) === null) continue;
4562
+ const list = keywordArms.get(owner) ?? [];
4563
+ list.push(armIndexOf.get(arm).index);
4564
+ keywordArms.set(owner, list);
4565
+ }
4566
+ const out = [];
4567
+ for (const v2 of visited.values()) {
4568
+ const d = v2.d;
4569
+ if (d.tag !== "regex") continue;
4570
+ const shape = keywordRegexShape(d.source);
4571
+ if (shape === null) continue;
4572
+ const ci = d.flags.includes("i");
4573
+ const unicode = d.flags.includes("u") || d.flags.includes("v");
4574
+ const own = armIndexOf.get(v2.p);
4575
+ const siblings = own === void 0 ? [] : (keywordArms.get(own.owner) ?? []).filter((i) => i !== own.index);
4576
+ const bq = (s) => `'${s.replace(/\\/g, "\\\\")}'`;
4577
+ const vocabulary = shape.words.length >= VOCABULARY_MIN_WORDS;
4578
+ const hazards = keywordAlternationHazards(shape.words, shape.boundary);
4579
+ const unrescued = hazards.filter((h) => !h.rescuedByBoundary);
4580
+ const longestFirst = shape.words.every((w, i) => i === 0 || shape.words[i - 1].length >= w.length);
4581
+ const shown = shape.words.length > 8 ? [...shape.words.slice(0, 6), "/* \u2026 */", shape.words[shape.words.length - 1]] : shape.words;
4582
+ const call = shape.words.length === 1 ? `word(${bq(shape.words[0])}, ${bq(shape.boundary ?? "_0-9A-Za-z")}${ci ? ", { caseInsensitive: true }" : ""})` : `keywords([${shown.map((w) => w === "/* \u2026 */" ? w : bq(w)).join(", ")}], { boundary: ${bq(shape.boundary ?? "_0-9A-Za-z")}${ci ? ", caseInsensitive: true" : ""} })`;
4583
+ out.push({
4584
+ kind: "keyword-regex",
4585
+ id: `keyword-regex:${siteToString(v2.site)}`,
4586
+ site: v2.site,
4587
+ source: d.source,
4588
+ flags: d.flags,
4589
+ words: shape.words,
4590
+ boundary: shape.boundary,
4591
+ caseFoldRisk: ci && !unicode,
4592
+ siblingArms: siblings,
4593
+ vocabulary,
4594
+ longestFirst,
4595
+ hazards,
4596
+ bug: unrescued.length > 0,
4597
+ suggestion: [
4598
+ vocabulary ? `this regex enumerates a FIXED VOCABULARY of ${shape.words.length} literal words${shape.words.length > 20 ? " \u2014 the alternation is long enough that nobody re-reads it" : ""}. A regex enumerating a fixed vocabulary is a keyword set written the hard way. Use \`${call}\` \u2014 it exposes an EXACT first-set that \`choice\` can dispatch on (the alternation exposes none), it owns the boundary so it is not re-spelled per word, it sorts LONGEST-FIRST by construction, and it lowers to a single charCodeAt scan.` : `\`regex(/${d.source}/${d.flags})\` hand-rolls a keyword and its word boundary. Use \`${call}\` \u2014 it owns the boundary (so \`${shape.boundary ?? ""}\` is not re-spelled per keyword), exposes an EXACT first-set that \`choice\` can dispatch on, and lowers to the same charCodeAt scan.`,
4599
+ unrescued.length > 0 ? `LIVE BUG \u2014 the alternation is ordered so ${unrescued.length} later alternative(s) can NEVER match: ${unrescued.slice(0, 3).map((h) => `\`${h.shorter}\` precedes \`${h.longer}\` and nothing rejects the following \`${h.at}\``).join("; ")}${unrescued.length > 3 ? `, and ${unrescued.length - 3} more` : ""}. Regex alternation is FIRST-match, not longest-match, and with no boundary guard to force a backtrack the shorter branch wins outright. \`keywords()\` cannot express this bug: it sorts longest-first.` : hazards.length > 0 ? `ORDERING HAZARD (currently harmless): ${hazards.length} earlier alternative(s) are strict prefixes of later ones \u2014 e.g. \`${hazards[0].shorter}\` before \`${hazards[0].longer}\`. Regex alternation is first-match, so these work ONLY because the trailing boundary guard rejects the next character and the engine backtracks into the longer branch. That correctness rests on a hand-maintained order plus a guard that happens to cover the right characters; \`keywords()\` sorts longest-first by construction and does not depend on either.` : !longestFirst && vocabulary ? `The alternation is not in longest-first order. It is correct today${shape.boundary === null ? "" : " (the boundary guard forces a backtrack where it matters)"}, but the ordering is hand-maintained and nothing checks it \u2014 adding a word that extends an existing one is a silent bug. \`keywords()\` sorts longest-first by construction.` : "",
4600
+ ci && !unicode ? `CORRECTNESS, not style: \`/i\` without \`/u\` does NOT fold case the way \`{c, toUpperCase(c), toLowerCase(c)}\` suggests \u2014 67 BMP code points fold in ways those three miss (\u03C2/\u03C3, \xB5/\u03BC, the \u01C4\u01C5\u01C6 digraphs, combining iota subscript). parseman fixed exactly this INSIDE \`keywords()\` (see combinators/case-fold.ts); a hand-rolled copy never received the fix, so its first-set is unsound for those inputs.` : "",
4601
+ siblings.length > 0 ? `arm[${own?.index}] here sits alongside arm(s) [${siblings.join(", ")}] in the same \`choice\`, which are also hand-rolled keywords. A single \`keywords([\u2026])\` replaces the whole group and gives longest-match dispatch across it \u2014 which is what that combinator exists for.` : ""
4602
+ ].filter((s) => s !== "").join(" ")
4603
+ });
4604
+ }
4605
+ return out.sort((a, b) => Number(b.bug) - Number(a.bug) || b.hazards.length - a.hazards.length || b.words.length - a.words.length || b.siblingArms.length - a.siblingArms.length);
4606
+ }
4607
+ var sitesLine = (sites) => sites.slice(0, 6).map(siteToString).join("\n ") + (sites.length > 6 ? `
4608
+ \u2026 and ${sites.length - 6} more` : "");
4609
+ function formatDuplicationFindings(report) {
4610
+ const lines = [];
4611
+ for (const f of report.structureLoss) {
4612
+ lines.push(`parseman BUG [structure-loss] @ ${siteToString(f.site)}: arm[${f.earlier}] flattens \`${f.nodeType}\` where arm[${f.later}] structures it, on ${firstSetToString(f.on)}`);
4613
+ lines.push(` flat: ${f.earlierShape}`);
4614
+ lines.push(` rich: ${f.laterShape}`);
4615
+ lines.push(` lost: ${f.lostNodeTypes.map((t) => `\`${t}\``).join(", ")}`);
4616
+ lines.push(` fix: ${f.suggestion}`);
4617
+ }
4618
+ for (const f of report.rewrites) {
4619
+ lines.push(`parseman ${f.bug ? "BUG" : "rewrite"} [${f.rewrite}${f.sepByVerdict === void 0 ? "" : `/${f.sepByVerdict}`}] @ ${siteToString(f.site)}${f.astNeutral ? "" : " (candidate \u2014 verify AST identity)"}`);
4620
+ lines.push(` from: ${f.from}`);
4621
+ lines.push(` to: ${f.to}`);
4622
+ lines.push(` fix: ${f.suggestion}`);
4623
+ if (f.perf !== "") lines.push(` perf: ${f.perf}`);
4624
+ }
4625
+ for (const f of report.divergentNodes) {
4626
+ lines.push(`parseman divergent-node: \`${f.nodeType}\` is built by ${f.count} different productions sharing ${f.sharedTerms.length} term(s): ${f.sharedTerms.map((t) => `\`${t}\``).join(", ")}`);
4627
+ for (const p of f.productions) lines.push(` ${siteToString(p.site)}
4628
+ ${p.shape}
4629
+ unique to it: ${p.distinctTerms.length === 0 ? "(none)" : p.distinctTerms.map((t) => `\`${t}\``).join(", ")}`);
4630
+ lines.push(` fix: ${f.suggestion}`);
4631
+ }
4632
+ for (const f of report.keywordRegexes) {
4633
+ const src = f.source.length > 90 ? `${f.source.slice(0, 88)}\u2026` : f.source;
4634
+ lines.push(`parseman ${f.bug ? "BUG " : ""}keyword-regex @ ${siteToString(f.site)}: /${src}/${f.flags}${f.vocabulary ? ` [${f.words.length} literal words${f.longestFirst ? "" : ", NOT longest-first"}]` : ""}${f.hazards.length > 0 ? ` [${f.hazards.length} prefix hazard(s)${f.bug ? ", UNRESCUED" : ""}]` : ""}${f.caseFoldRisk ? " [CASE-FOLD RISK]" : ""}`);
4635
+ lines.push(` fix: ${f.suggestion}`);
4636
+ }
4637
+ for (const f of report.duplicates) {
4638
+ lines.push(`parseman duplication: ${f.count}\xD7 identical ${f.size}-node shape (saves ${f.savings} nodes) \u2014 ${f.shape}`);
4639
+ lines.push(` at: ${sitesLine(f.sites)}`);
4640
+ lines.push(` fix: ${f.suggestion}`);
4641
+ }
4642
+ for (const f of report.nearDuplicates) {
4643
+ lines.push(`parseman near-duplication: ${f.count} clones of a ${f.sharedSize}-node shape differing at ONE slot (saves ${f.savings} nodes) \u2014 ${f.shape}`);
4644
+ lines.push(` slot: ${f.slotPath} \u2208 { ${f.variants.join(" | ")} }`);
4645
+ lines.push(` at: ${sitesLine(f.sites)}`);
4646
+ lines.push(` fix: ${f.suggestion}`);
4647
+ }
4648
+ for (const f of report.regexFragments) {
4649
+ lines.push(`parseman regex-fragment: \`${f.fragment}\` (${f.branches} branches) re-spelled in ${f.count} regex() terminals`);
4650
+ lines.push(` at: ${sitesLine(f.sites)}`);
4651
+ lines.push(` fix: ${f.suggestion}`);
4652
+ }
4653
+ for (const f of report.regexClasses) {
4654
+ lines.push(`parseman regex-class${f.drifted ? " DRIFT" : ""}: [${f.canonical}] across ${f.count} regex() terminals${f.bmpCeiling ? " [BMP ceiling]" : ""}`);
4655
+ for (const v2 of f.variants) lines.push(` [${v2.source}] \xD7${v2.count} ${v2.delta}
4656
+ ${sitesLine(v2.sites)}`);
4657
+ lines.push(` fix: ${f.suggestion}`);
4658
+ }
4659
+ for (const f of report.overlaps) {
4660
+ lines.push(`parseman overlap @ ${siteToString(f.site)}: arm[${f.a}] \u2229 arm[${f.b}] on ${firstSetToString(f.on)}${f.sharedPrefix === null ? "" : ` \u2014 shared prefix: ${f.sharedPrefix}`}${f.handledByStrategy ? " [sharedPrefix strategy already handles this]" : ""}`);
4661
+ lines.push(` fix: ${f.suggestion}`);
4662
+ }
4663
+ return lines;
4664
+ }
4665
+ function duplicationFindingCount(report) {
4666
+ return report.duplicates.length + report.nearDuplicates.length + report.regexFragments.length + report.regexClasses.length + report.overlaps.length + report.rewrites.length + report.divergentNodes.length + report.structureLoss.length + report.keywordRegexes.length;
4667
+ }
4668
+
3319
4669
  // src/combinators/scanTo.ts
3320
4670
  function resolveScanSkip(explicitSkip, raw, ctx) {
3321
4671
  if (raw) return [];
@@ -6536,12 +7886,14 @@ function emitNode(def, ctx, pos) {
6536
7886
  ctx.buildSrcs.push(def.buildSrc ?? null);
6537
7887
  }
6538
7888
  const i = ind(ctx);
6539
- const capturesTrivia = mkType !== null || def.captureTrivia === true || def.trailingTrivia === true || !structural && buildReadsTrivia(def);
6540
- const clonesState = !structural && buildReadsState(def);
6541
- const capturesChildren = !structural && (mkType !== null || def.unwrap || def.collapse || buildReadsChildren(def));
6542
- const capturesRaw = !structural && (mkType !== null || buildReadsRaw(def));
7889
+ const cstOut = ctx.hostMode === "cst" && !structural;
7890
+ if (!structural && !cstOut) ctx.hostBranchElided = true;
7891
+ const capturesTrivia = cstOut || mkType !== null || def.captureTrivia === true || def.trailingTrivia === true || !structural && buildReadsTrivia(def);
7892
+ const clonesState = !structural && (cstOut || buildReadsState(def));
7893
+ const capturesChildren = !structural && (cstOut || mkType !== null || def.unwrap || def.collapse || buildReadsChildren(def));
7894
+ const capturesRaw = !structural && (cstOut || mkType !== null || buildReadsRaw(def));
6543
7895
  const hasFields = parserHasOwnFields(def.parser);
6544
- const capturesFields = hasFields && !structural && buildReadsFields(def);
7896
+ const capturesFields = hasFields && !structural && (cstOut || buildReadsFields(def));
6545
7897
  const innerEnablesTriviaCapture = parserEnablesTriviaCapture(def.parser);
6546
7898
  const chV = v(ctx, "_ch");
6547
7899
  const rawV = v(ctx, "_raw");
@@ -6558,8 +7910,9 @@ function emitNode(def, ctx, pos) {
6558
7910
  const profHoist = `${i}const ${pmV} = _ctx._pmProfile, ${recV} = ${pmV}?.phase === 'recognizer', ${capV} = ${pmV}?.phase === 'capture'`;
6559
7911
  const profileRecognizer = recV;
6560
7912
  const profileCapture = capV;
6561
- const directCstV = !structural && (!capturesChildren || !capturesRaw) ? v(ctx, "_dcst") : null;
6562
- const directCstGate = directCstV === null ? "false" : `${profileCapture} || _ctx.build?._parsemanCstOutput === true`;
7913
+ const cstMode = ctx.hostMode === "cst";
7914
+ const directCstV = !structural && !cstMode && (!capturesChildren || !capturesRaw) ? v(ctx, "_dcst") : null;
7915
+ const directCstGate = directCstV === null ? "false" : profileCapture;
6563
7916
  const structuralCapturesTrivia = structural && (def.captureTrivia === true || def.trailingTrivia === true);
6564
7917
  const chNeededExpr = def.unwrap || def.collapse ? "true" : `(_ctx._pmReadsCh ??= (_ctx.build === undefined || _ctx.build._parsemanReadsChildren !== false || _ctx.build._parsemanCstCollapse !== undefined))`;
6565
7918
  const chAlloc = def.unwrap || def.collapse ? `${profileRecognizer} ? undefined : []` : `${profileRecognizer} ? undefined : ((${profileCapture} || ${chNeededExpr}) ? [] : undefined)`;
@@ -6640,7 +7993,7 @@ ${i}const ${chV} = ${chAlloc}, ${rawV} = ${profileRecognizer} ? undefined : [],
6640
7993
  const ndV = v(ctx, "_nd");
6641
7994
  const buildExpr = structural ? `{ _tag: 'node', type: ${JSON.stringify(def.type)}, span: { start: ${pos}, end: ${endVar} }, state: ${stV} ?? null, children: ${chV} }` : mkType ? emitInlineMkNodeExpr(mkType, chV, rawV, pos, endVar, tlV) : `${buildRef(ctx)}[${buildIdx}](${chV}, ${fObj}, { start: ${pos}, end: ${endVar} }, ${rawV}, ${tlV}, ${stV})`;
6642
7995
  const hostBuildExpr = `_ctx._pmProfile?.phase === 'host' && _ctx._pmProfile.hostCalls++, _ctx.build(${JSON.stringify(def.type)}, ${chV}, ${fObj}, { start: ${pos}, end: ${endVar} }, ${rawV}, ${tlV}, ${stV})`;
6643
- const ndExpr = structural ? `_ctx.build !== undefined ? (${hostBuildExpr}) : (${buildExpr})` : `_ctx.build?._parsemanCstOutput === true ? (${hostBuildExpr}) : (${buildExpr})`;
7996
+ const ndExpr = structural ? `_ctx.build !== undefined ? (${hostBuildExpr}) : (${buildExpr})` : cstOut ? hostBuildExpr : buildExpr;
6644
7997
  const hostCollapseExpr = structural ? `_ctx.build !== undefined && _ctx.build._parsemanCstCollapse !== undefined && ${chV}.length === 1 && ${rawV}.length === 1 && _ctx.build._parsemanCstCollapse(${JSON.stringify(def.type)}, ${chV}[0], ${chV}, ${rawV}) ? ${chV}[0] : (${ndExpr})` : ndExpr;
6645
7998
  const unwrapExpr = `${chV}.length === 1 ? (${chV}[0] !== null && typeof ${chV}[0] === 'object' && ${chV}[0]._tag === 'leaf' ? ${chV}[0].value : ${chV}[0]) : (${ndExpr})`;
6646
7999
  const collapseExpr = `${chV}.length === 1 ? ${chV}[0] : (${ndExpr})`;
@@ -7089,9 +8442,9 @@ function hasDirectBuildDef(p, seen = /* @__PURE__ */ new Set()) {
7089
8442
  return false;
7090
8443
  }
7091
8444
  }
7092
- return childrenOf(d).some((child) => hasDirectBuildDef(child, seen));
8445
+ return childrenOf2(d).some((child) => hasDirectBuildDef(child, seen));
7093
8446
  }
7094
- function childrenOf(def) {
8447
+ function childrenOf2(def) {
7095
8448
  switch (def.tag) {
7096
8449
  case "sequence":
7097
8450
  case "choice":
@@ -7162,7 +8515,7 @@ function subtreeSizes(roots) {
7162
8515
  const def = p._def;
7163
8516
  let s = 1;
7164
8517
  if (def.tag !== "lazy") {
7165
- for (const child of childrenOf(def)) s += child._def.tag === "lazy" ? 1 : sz(child);
8518
+ for (const child of childrenOf2(def)) s += child._def.tag === "lazy" ? 1 : sz(child);
7166
8519
  }
7167
8520
  sizes.set(p, s);
7168
8521
  return s;
@@ -7183,7 +8536,7 @@ function subtreeSizes(roots) {
7183
8536
  return;
7184
8537
  }
7185
8538
  sz(p);
7186
- for (const child of childrenOf(def)) visit(child);
8539
+ for (const child of childrenOf2(def)) visit(child);
7187
8540
  };
7188
8541
  for (const root of roots) visit(root);
7189
8542
  return sizes;
@@ -7217,7 +8570,7 @@ function analyzeLazyUsageMulti(roots) {
7217
8570
  if (isHoistableTag(def.tag)) {
7218
8571
  counts.set(p, (counts.get(p) ?? 0) + 1);
7219
8572
  }
7220
- for (const child of childrenOf(def)) walk2(child);
8573
+ for (const child of childrenOf2(def)) walk2(child);
7221
8574
  }
7222
8575
  const rootsArr = [...roots];
7223
8576
  for (const root of rootsArr) walk2(root);
@@ -7249,7 +8602,7 @@ function ruleDependencies(ruleMap) {
7249
8602
  walk2(resolved, false);
7250
8603
  return;
7251
8604
  }
7252
- for (const child of childrenOf(def)) walk2(child, false);
8605
+ for (const child of childrenOf2(def)) walk2(child, false);
7253
8606
  };
7254
8607
  walk2(comb, true);
7255
8608
  deps.set(name, [...found]);
@@ -7269,10 +8622,30 @@ function runGatingDiagnostic(combinator, opt) {
7269
8622
  function runGatingDiagnosticRules(ruleMap, opt) {
7270
8623
  return reportGating(opt, (o) => analyzeGatingRules(ruleMap, o));
7271
8624
  }
7272
- function runFusedGatingDiagnostic(getRuleMaps, opt) {
8625
+ function runFusedGatingDiagnostic(getRuleMaps, opt, getOpaque) {
7273
8626
  if (resolveGatingLevel(opt) === "off") return void 0;
7274
8627
  const ruleMaps = getRuleMaps();
7275
- if (!ruleMaps.some((map) => hasExternalRuleRef(map))) return void 0;
8628
+ const opaque = getOpaque?.() ?? [];
8629
+ const opaqueFindings = opaque.map((o) => ({
8630
+ rule: o.ruleNames.length > 0 ? o.ruleNames.join(", ") : `<artifact ${o.ns}>`,
8631
+ kind: "opaque-artifact",
8632
+ reason: `precompiled artifact "${o.ns}" carries compiled rule functions, not re-lowerable IR \u2014 its choices were not examined at this fuse.`
8633
+ }));
8634
+ if (!ruleMaps.some((map) => hasExternalRuleRef(map))) {
8635
+ if (opaqueFindings.length === 0) return void 0;
8636
+ return reportGating(opt, () => ({
8637
+ totalChoices: 0,
8638
+ gated: 0,
8639
+ recoverable: 0,
8640
+ unanalysable: opaqueFindings,
8641
+ ungated: [],
8642
+ accepted: [],
8643
+ deferred: [],
8644
+ acceptedUnused: [],
8645
+ choices: [],
8646
+ antiPatterns: []
8647
+ }));
8648
+ }
7276
8649
  const winners = /* @__PURE__ */ new Map();
7277
8650
  for (const map of ruleMaps) for (const [name, rule] of map) {
7278
8651
  if (rule._def.tag === "lazy") {
@@ -7288,7 +8661,12 @@ function runFusedGatingDiagnostic(getRuleMaps, opt) {
7288
8661
  return reportGating(opt, (o) => {
7289
8662
  const deferredHere = new Set(analyzeGatingRules(entries, o).deferred.map((c) => c.id));
7290
8663
  const fused = analyzeGatingRules(entries, { ...o, resolveRef: (name) => winners.get(name) });
7291
- return { ...fused, ungated: fused.ungated.filter((c) => deferredHere.has(c.id)), antiPatterns: [] };
8664
+ return {
8665
+ ...fused,
8666
+ ungated: fused.ungated.filter((c) => deferredHere.has(c.id)),
8667
+ antiPatterns: [],
8668
+ unanalysable: [...fused.unanalysable, ...opaqueFindings]
8669
+ };
7292
8670
  });
7293
8671
  }
7294
8672
  function reportGating(opt, analyze) {
@@ -7299,22 +8677,82 @@ function reportGating(opt, analyze) {
7299
8677
  let report;
7300
8678
  try {
7301
8679
  report = analyze(analyzeOpts);
7302
- } catch {
7303
- return void 0;
8680
+ } catch (e) {
8681
+ const reason = e instanceof Error ? `${e.name}: ${e.message}` : String(e);
8682
+ const failed = {
8683
+ totalChoices: 0,
8684
+ gated: 0,
8685
+ recoverable: 0,
8686
+ unanalysable: [{ rule: "<whole grammar>", kind: "not-a-combinator", reason: `gating analysis threw \u2014 ${reason}` }],
8687
+ ungated: [],
8688
+ accepted: [],
8689
+ deferred: [],
8690
+ acceptedUnused: [],
8691
+ choices: [],
8692
+ antiPatterns: []
8693
+ };
8694
+ const failLines = formatGatingWarnings(failed);
8695
+ if (level === "error") throw new Error(`parseman: gating analysis FAILED (no verdict available)
8696
+ ${failLines.join("\n")}`);
8697
+ for (const l of failLines) console.warn(l);
8698
+ return failed;
7304
8699
  }
7305
8700
  const lines = formatGatingWarnings(report);
7306
8701
  if (lines.length > 0) {
7307
- if (level === "error") throw new Error(`parseman: ${report.ungated.length} ungated hot choice(s) / ${report.antiPatterns.length} anti-pattern(s)
8702
+ if (level === "error") throw new Error(`parseman: ${report.ungated.length} ungated hot choice(s) / ${report.antiPatterns.length} anti-pattern(s) / ${report.unanalysable.length} unanalysable rule(s)
8703
+ ${lines.join("\n")}`);
8704
+ for (const l of lines) console.warn(l);
8705
+ }
8706
+ return report;
8707
+ }
8708
+ function resolveDuplicationLevel(opt) {
8709
+ const explicit = typeof opt === "string" ? opt : opt?.level;
8710
+ if (explicit !== void 0) return explicit;
8711
+ const env = typeof process !== "undefined" ? process.env?.PARSEMAN_DUPLICATION : void 0;
8712
+ if (env === "off" || env === "warn" || env === "error") return env;
8713
+ return "off";
8714
+ }
8715
+ function reportDuplication(opt, analyze) {
8716
+ const level = resolveDuplicationLevel(opt);
8717
+ if (level === "off") return void 0;
8718
+ const obj = opt !== null && typeof opt === "object" ? opt : void 0;
8719
+ const analyzeOpts = obj === void 0 ? void 0 : {
8720
+ ...obj.accept !== void 0 ? { accept: obj.accept } : {},
8721
+ ...obj.minSize !== void 0 ? { minSize: obj.minSize } : {},
8722
+ ...obj.maxFindings !== void 0 ? { maxFindings: obj.maxFindings } : {},
8723
+ ...obj.entryName !== void 0 ? { entryName: obj.entryName } : {}
8724
+ };
8725
+ let report;
8726
+ try {
8727
+ report = analyze(analyzeOpts);
8728
+ } catch (err) {
8729
+ console.warn(
8730
+ `parseman: the duplication diagnostic could not run, so NOTHING was checked \u2014 this is not a clean result.
8731
+ ${err instanceof Error ? err.message : String(err)}`
8732
+ );
8733
+ return void 0;
8734
+ }
8735
+ const lines = formatDuplicationFindings(report);
8736
+ if (lines.length > 0) {
8737
+ if (level === "error") throw new Error(`parseman: ${duplicationFindingCount(report)} duplication/overlap finding(s)
7308
8738
  ${lines.join("\n")}`);
7309
8739
  for (const l of lines) console.warn(l);
7310
8740
  }
7311
8741
  return report;
7312
8742
  }
8743
+ function runDuplicationDiagnostic(combinator, opt) {
8744
+ return reportDuplication(opt, (o) => analyzeDuplication(combinator, o));
8745
+ }
8746
+ function runDuplicationDiagnosticRules(ruleMap, opt) {
8747
+ return reportDuplication(opt, (o) => analyzeDuplicationRules(ruleMap, o));
8748
+ }
7313
8749
  function compile(combinator, mapFnSources, opts) {
7314
8750
  const gatingReport = runGatingDiagnostic(combinator, opts?.gating);
8751
+ runDuplicationDiagnostic(combinator, opts?.duplication);
7315
8752
  markUnusedValues(combinator);
7316
8753
  const grammarTrivia = combinator._meta.grammarTrivia;
7317
8754
  const grammarScanSkip = combinator._meta.grammarScanSkip;
8755
+ const grammarHostMode = combinator._meta.grammarHostMode;
7318
8756
  const ctx = {
7319
8757
  vars: 0,
7320
8758
  indent: 1,
@@ -7334,6 +8772,7 @@ function compile(combinator, mapFnSources, opts) {
7334
8772
  namedFnDecls: [],
7335
8773
  capturing: hasNodeDef(combinator),
7336
8774
  recovery: opts?.recovery ?? false,
8775
+ hostMode: opts?.hostMode ?? grammarHostMode ?? "ast",
7337
8776
  ...opts?.coverage ? { coverage: { plan: buildGrammarPlan(combinator), entry: combinator } } : {},
7338
8777
  lazyUsage: analyzeLazyUsage(combinator),
7339
8778
  ...grammarTrivia ? { activeTrivia: grammarTrivia, triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {},
@@ -7406,6 +8845,7 @@ function compile(combinator, mapFnSources, opts) {
7406
8845
  return fn(input, pos, ctx.runtimeParsers, ctx.mapFns, ctx.buildFns, defaultCtx);
7407
8846
  },
7408
8847
  parseWithContext(input, parseCtx, pos = 0) {
8848
+ assertHostModeCompatible(ctx.hostMode ?? "ast", parseCtx.build, !!ctx.hostBranchElided);
7409
8849
  return fn(input, pos, ctx.runtimeParsers, ctx.mapFns, ctx.buildFns, parseCtx);
7410
8850
  },
7411
8851
  // Note: collects expect() errors via _errors. Unlike interpreter
@@ -7436,9 +8876,11 @@ function publicRuleWrapperSource(rule, fnSource) {
7436
8876
  function compileLinkable(ruleMapArg, ns, opts) {
7437
8877
  if (!ns) throw new Error("compileLinkable: ns must be a non-empty namespace");
7438
8878
  if (opts?.gating !== void 0) runGatingDiagnosticRules(ruleMapArg, opts.gating);
8879
+ if (opts?.duplication !== void 0) runDuplicationDiagnosticRules(ruleMapArg, opts.duplication);
7439
8880
  for (const [, rule] of ruleMapArg) markUnusedValues(rule);
7440
8881
  const grammarTrivia = opts?.trivia ?? ruleMapArg.map(([, r]) => r._meta.grammarTrivia).find(Boolean);
7441
8882
  const grammarScanSkip = opts?.scanSkip ?? ruleMapArg.map(([, r]) => r._meta.grammarScanSkip).find(Boolean);
8883
+ const grammarHostMode = opts?.hostMode ?? ruleMapArg.map(([, r]) => r._meta.grammarHostMode).find(Boolean);
7442
8884
  const ruleMap = ruleMapArg.filter(([, val]) => {
7443
8885
  const d = val._def;
7444
8886
  if (d.tag !== "lazy") return true;
@@ -7472,6 +8914,7 @@ function compileLinkable(ruleMapArg, ns, opts) {
7472
8914
  // merely returning their scalar parse values.
7473
8915
  capturing: opts?.captureTerminals === true || ruleMap.some(([, rule]) => hasNodeDef(rule)),
7474
8916
  recovery: opts?.recovery ?? false,
8917
+ hostMode: grammarHostMode ?? "ast",
7475
8918
  ...opts?.coverage ? { coverage: { plan: opts.coverage } } : {},
7476
8919
  lazyUsage: analyzeLazyUsageMulti(ruleMap.map(([, rule]) => rule)),
7477
8920
  ns,
@@ -7522,7 +8965,7 @@ function compileLinkable(ruleMapArg, ns, opts) {
7522
8965
  scanExternal(resolved);
7523
8966
  return;
7524
8967
  }
7525
- for (const child of childrenOf(def)) scanExternal(child);
8968
+ for (const child of childrenOf2(def)) scanExternal(child);
7526
8969
  };
7527
8970
  for (const [, rule] of ruleMap) scanExternal(rule);
7528
8971
  }
@@ -7627,6 +9070,8 @@ function compileLinkable(ruleMapArg, ns, opts) {
7627
9070
  deps: ruleDependencies(ruleMap),
7628
9071
  needsEmptyTl: !!ctx.needsEmptyTl,
7629
9072
  needsHostReads: !!ctx.needsHostReads,
9073
+ hostMode: ctx.hostMode ?? "ast",
9074
+ hostBranchElided: !!ctx.hostBranchElided,
7630
9075
  hasDirectBuilders: ruleMap.some(([, rule]) => hasDirectBuildDef(rule)),
7631
9076
  isRecognitionOnly: !hasSemanticReduction(ruleMap.map(([, rule]) => rule), externalRefs),
7632
9077
  mfFns: mfSrcs ? [] : ctx.mapFns,
@@ -7649,7 +9094,7 @@ function hasExternalRuleRef(ruleMap) {
7649
9094
  if (resolved === void 0) return p._ruleName !== void 0;
7650
9095
  return visit(resolved);
7651
9096
  }
7652
- return childrenOf(def).some(visit);
9097
+ return childrenOf2(def).some(visit);
7653
9098
  };
7654
9099
  return ruleMap.some(([, rule]) => visit(rule));
7655
9100
  }
@@ -7671,7 +9116,7 @@ function hasSemanticReduction(roots, externalRefs) {
7671
9116
  return true;
7672
9117
  }
7673
9118
  }
7674
- return childrenOf(def).some(visit);
9119
+ return childrenOf2(def).some(visit);
7675
9120
  };
7676
9121
  return roots.some(visit);
7677
9122
  }
@@ -7740,7 +9185,7 @@ function withCtx(extra, combinator) {
7740
9185
  }
7741
9186
 
7742
9187
  // src/compiler/ir-serialize.ts
7743
- function childrenOf2(def) {
9188
+ function childrenOf3(def) {
7744
9189
  switch (def.tag) {
7745
9190
  case "sequence":
7746
9191
  case "choice":
@@ -7779,7 +9224,7 @@ function childrenOf2(def) {
7779
9224
  return [];
7780
9225
  }
7781
9226
  }
7782
- var ruleNameOf2 = (c) => c._ruleName;
9227
+ var ruleNameOf3 = (c) => c._ruleName;
7783
9228
  function lazyTarget(c) {
7784
9229
  if (c._def.tag !== "lazy") return null;
7785
9230
  try {
@@ -7977,18 +9422,18 @@ var Serializer = class {
7977
9422
  if (seen.has(c)) return;
7978
9423
  seen.add(c);
7979
9424
  if (c._def.tag === "lazy") {
7980
- if (ruleNameOf2(c) !== void 0) throw new Unserializable("scanSkip unit references a grammar rule");
9425
+ if (ruleNameOf3(c) !== void 0) throw new Unserializable("scanSkip unit references a grammar rule");
7981
9426
  const target = lazyTarget(c);
7982
9427
  if (target) this.assertNoRuleRef(target, seen);
7983
9428
  return;
7984
9429
  }
7985
- for (const child of childrenOf2(c._def)) this.assertNoRuleRef(child, seen);
9430
+ for (const child of childrenOf3(c._def)) this.assertNoRuleRef(child, seen);
7986
9431
  }
7987
9432
  /** A `rules()` map wraps each rule value in a named `lazy` proxy; the real body is
7988
9433
  * its target (compileLinkable unwraps the same way). A ref INSIDE a body stays a
7989
9434
  * `g[name]` ref — only the top-level entry is unwrapped. */
7990
9435
  body(c) {
7991
- return c._def.tag === "lazy" && ruleNameOf2(c) !== void 0 ? lazyTarget(c) ?? c : c;
9436
+ return c._def.tag === "lazy" && ruleNameOf3(c) !== void 0 ? lazyTarget(c) ?? c : c;
7992
9437
  }
7993
9438
  run() {
7994
9439
  for (const [, c] of this.ruleMap) this.analyze(this.body(c), /* @__PURE__ */ new Set());
@@ -8030,7 +9475,7 @@ ${outerDecls.map((d) => " " + d).join("\n")}
8030
9475
  analyze(c, active) {
8031
9476
  const def = c._def;
8032
9477
  if (def.tag === "lazy") {
8033
- if (ruleNameOf2(c) !== void 0) return;
9478
+ if (ruleNameOf3(c) !== void 0) return;
8034
9479
  const target = lazyTarget(c);
8035
9480
  if (!target) throw new Unserializable("unresolved unnamed ref");
8036
9481
  if (active.has(target)) {
@@ -8046,7 +9491,7 @@ ${outerDecls.map((d) => " " + d).join("\n")}
8046
9491
  this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
8047
9492
  if ((this.counts.get(c) ?? 0) > 1) return;
8048
9493
  active.add(c);
8049
- for (const child of childrenOf2(def)) this.analyze(child, active);
9494
+ for (const child of childrenOf3(def)) this.analyze(child, active);
8050
9495
  active.delete(c);
8051
9496
  }
8052
9497
  /** Reference `c`: a const var if it has one (emitting its decl first), else inline.
@@ -8082,7 +9527,7 @@ ${outerDecls.map((d) => " " + d).join("\n")}
8082
9527
  const kid = (x) => this.ref(x, selfOf);
8083
9528
  switch (def.tag) {
8084
9529
  case "lazy": {
8085
- if (ruleNameOf2(c) !== void 0) return `g[${JSON.stringify(ruleNameOf2(c))}]`;
9530
+ if (ruleNameOf3(c) !== void 0) return `g[${JSON.stringify(ruleNameOf3(c))}]`;
8086
9531
  const target = lazyTarget(c);
8087
9532
  if (selfOf && target === selfOf.comb) return selfOf.var;
8088
9533
  return this.ref(target, selfOf);
@@ -8182,8 +9627,12 @@ ${outerDecls.map((d) => " " + d).join("\n")}
8182
9627
 
8183
9628
  // src/compiler/linker.ts
8184
9629
  var _nsCounter = 0;
8185
- function linkable(rulesMap, ns, trivia2) {
8186
- const pieces = compileLinkable(Object.entries(rulesMap), ns ?? `_lk${_nsCounter++}_`, trivia2 ? { trivia: trivia2 } : void 0);
9630
+ function linkable(rulesMap, ns, trivia2, hostMode) {
9631
+ const pieces = compileLinkable(
9632
+ Object.entries(rulesMap),
9633
+ ns ?? `_lk${_nsCounter++}_`,
9634
+ { ...trivia2 ? { trivia: trivia2 } : {}, ...hostMode ? { hostMode } : {} }
9635
+ );
8187
9636
  if (!pieces) throw new Error("linkable(): this grammar cannot be compiled to a linkable artifact (contains a runtime-only parser fallback)");
8188
9637
  return pieces;
8189
9638
  }
@@ -8245,7 +9694,23 @@ function fusedBody(pieces) {
8245
9694
  ...[...winner].map(([k, p]) => p.ruleFns.get(k))
8246
9695
  ];
8247
9696
  const wrapperEntries = [...winner].map(([k, p]) => `${JSON.stringify(k)}: ${p.wrappers.get(k)}`);
8248
- const rawBody = [...lines, "return {", wrapperEntries.join(",\n"), "}"].join("\n");
9697
+ const { mode, elided } = hostModeOfPieces(pieces);
9698
+ const rawBody = [
9699
+ ...lines,
9700
+ "const _map = {",
9701
+ wrapperEntries.join(",\n"),
9702
+ "}",
9703
+ `Object.defineProperty(_map, Symbol.for(${JSON.stringify(FUSED_HOST_MODE.description)}), { value: ${JSON.stringify(mode)}, enumerable: false })`,
9704
+ `Object.defineProperty(_map, Symbol.for(${JSON.stringify(FUSED_HOST_ELIDED.description)}), { value: ${JSON.stringify(elided)}, enumerable: false })`,
9705
+ // …and on each rule FUNCTION, because `run(map.Rule, …)` is handed the rule, not the
9706
+ // map, and would otherwise have nothing to check against. Done once at fuse time,
9707
+ // before any rule has been called, so it costs nothing per parse.
9708
+ "for (const _k of Object.keys(_map)) {",
9709
+ ` Object.defineProperty(_map[_k], Symbol.for(${JSON.stringify(FUSED_HOST_MODE.description)}), { value: ${JSON.stringify(mode)}, enumerable: false })`,
9710
+ ` Object.defineProperty(_map[_k], Symbol.for(${JSON.stringify(FUSED_HOST_ELIDED.description)}), { value: ${JSON.stringify(elided)}, enumerable: false })`,
9711
+ "}",
9712
+ "return _map"
9713
+ ].join("\n");
8249
9714
  const ANY2 = { kind: "any" };
8250
9715
  const EMPTY3 = { kind: "empty" };
8251
9716
  const recipes = /* @__PURE__ */ new Map();
@@ -8304,19 +9769,78 @@ function fusedBody(pieces) {
8304
9769
  }
8305
9770
  return { body, env };
8306
9771
  }
9772
+ function hostModeOfPieces(pieces) {
9773
+ const mode = pieces.find((p) => p.hostMode !== void 0 && p.hostMode !== "ast")?.hostMode ?? "ast";
9774
+ const elided = pieces.filter((p) => p.hostBranchElided === true);
9775
+ if (mode === "cst" && elided.length > 0) {
9776
+ throw new Error(
9777
+ `compose: cannot build a positioned-CST artifact from pieces that were compiled for host mode "ast" \u2014 ${elided.map((p) => `"${p.ns}"`).join(", ")} ${elided.length === 1 ? "dropped its" : "dropped their"} direct builders' CST branch, so ${elided.length === 1 ? "it" : "they"} would return AST objects inside the CST. An already-compiled piece keeps the mode it was built with; only pieces carried as IR are re-lowered by this compose. Re-compile ${elided.length === 1 ? "it" : "them"} with hostMode: 'cst', or pass the source grammar so it can be re-lowered.`
9778
+ );
9779
+ }
9780
+ return { mode, elided: elided.length > 0 };
9781
+ }
8307
9782
  function fuseRules(pieces) {
8308
9783
  const { body, env } = fusedBody(pieces);
8309
9784
  return new Function("_env", body)(env);
8310
9785
  }
9786
+ function fusedHostModeOf(registry) {
9787
+ const m = registry[FUSED_HOST_MODE];
9788
+ return m === "cst" ? "cst" : "ast";
9789
+ }
9790
+ function fusedHostElidedOf(registry) {
9791
+ return registry[FUSED_HOST_ELIDED] === true;
9792
+ }
8311
9793
  var COMPOSED_PIECES = /* @__PURE__ */ Symbol.for("parseman.composedPieces");
9794
+ function composedPiecesOf(grammar) {
9795
+ const pieces = grammar[COMPOSED_PIECES];
9796
+ return Array.isArray(pieces) ? pieces : void 0;
9797
+ }
8312
9798
  var LEAF_COMPOSED = /* @__PURE__ */ Symbol.for("parseman.leafComposed");
8313
9799
  var COMPOSED_TRIVIA = /* @__PURE__ */ Symbol.for("parseman.composedTrivia");
8314
9800
  var COMPOSED_COVERAGE_RULES = /* @__PURE__ */ Symbol.for("parseman.composedCoverageRules");
8315
9801
  function isIRPiece(p) {
8316
9802
  return !!p && typeof p === "object" && typeof p.ir === "string" && typeof p.ns === "string" && !("ruleFns" in p);
8317
9803
  }
8318
- function carriedRuleMaps(carried) {
8319
- return carried.filter(isIRPiece).map((p) => evalRuleMapIR(p.ir));
9804
+ function once(fn) {
9805
+ let done = false;
9806
+ let value;
9807
+ return () => {
9808
+ if (!done) {
9809
+ value = fn();
9810
+ done = true;
9811
+ }
9812
+ return value;
9813
+ };
9814
+ }
9815
+ function carriedRuleMapsDetailed(carried) {
9816
+ const maps = [];
9817
+ const opaque = [];
9818
+ for (const p of carried) {
9819
+ if (isIRPiece(p)) {
9820
+ maps.push(evalRuleMapIR(p.ir));
9821
+ continue;
9822
+ }
9823
+ const ruleFns = p.ruleFns;
9824
+ opaque.push({ ns: p.ns, ruleNames: ruleFns instanceof Map ? [...ruleFns.keys()] : [] });
9825
+ }
9826
+ return { maps, opaque };
9827
+ }
9828
+ function recoverComposedRules(grammar) {
9829
+ const carried = composedPiecesOf(grammar);
9830
+ if (carried === void 0) return void 0;
9831
+ const { maps, opaque } = carriedRuleMapsDetailed(carried);
9832
+ const rules2 = /* @__PURE__ */ new Map();
9833
+ for (const map of maps) for (const [name, rule] of map) {
9834
+ if (rule._def.tag === "lazy") {
9835
+ try {
9836
+ rule._def.thunk();
9837
+ } catch {
9838
+ continue;
9839
+ }
9840
+ }
9841
+ rules2.set(name, rule);
9842
+ }
9843
+ return { rules: rules2, opaque };
8320
9844
  }
8321
9845
  function coverageRulesOf(carried) {
8322
9846
  const winners = {};
@@ -8339,11 +9863,12 @@ function coverageRulesOf(carried) {
8339
9863
  function composedCoverageRules(grammar) {
8340
9864
  return grammar[COMPOSED_COVERAGE_RULES];
8341
9865
  }
8342
- function materializePiece(p, trivia2, captureTerminals = false) {
9866
+ function materializePiece(p, trivia2, captureTerminals = false, hostMode) {
8343
9867
  if (!isIRPiece(p)) return p;
8344
9868
  const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns, {
8345
9869
  ...trivia2 ? { trivia: trivia2 } : {},
8346
- ...captureTerminals ? { captureTerminals: true } : {}
9870
+ ...captureTerminals ? { captureTerminals: true } : {},
9871
+ ...hostMode ? { hostMode } : {}
8347
9872
  });
8348
9873
  if (!pieces) throw new Error(`compose: carried IR for ns "${p.ns}" could not be re-lowered`);
8349
9874
  return pieces;
@@ -8356,7 +9881,7 @@ function nextComposeNs(used) {
8356
9881
  used.add(ns);
8357
9882
  return ns;
8358
9883
  }
8359
- function itemCarried(item, used, trivia2) {
9884
+ function itemCarried(item, used, trivia2, hostMode) {
8360
9885
  const carried = item[COMPOSED_PIECES];
8361
9886
  if (Array.isArray(carried)) {
8362
9887
  const items = carried;
@@ -8382,7 +9907,7 @@ function itemCarried(item, used, trivia2) {
8382
9907
  });
8383
9908
  const scanSkip = entries.map(([, val]) => val._meta.grammarScanSkip).find(Boolean);
8384
9909
  const ir = serializeRuleMap(entries, scanSkip);
8385
- return ir ? [{ ns, ir }] : [linkable(map, ns, trivia2)];
9910
+ return ir ? [{ ns, ir }] : [linkable(map, ns, trivia2, hostMode)];
8386
9911
  }
8387
9912
  function composingTriviaOf(items) {
8388
9913
  for (let i = items.length - 1; i >= 0; i--) {
@@ -8395,15 +9920,20 @@ function composingTriviaOf(items) {
8395
9920
  }
8396
9921
  return void 0;
8397
9922
  }
8398
- function compose(items) {
9923
+ function compose(items, opts) {
8399
9924
  if (items.some((item) => item[LEAF_COMPOSED] === true)) {
8400
9925
  throw new Error("compose: a composeLeaf() result is terminal and cannot be composed again");
8401
9926
  }
8402
9927
  const used = /* @__PURE__ */ new Set();
8403
9928
  const trivia2 = composingTriviaOf(items);
8404
- const carried = items.flatMap((item) => itemCarried(item, used, trivia2));
8405
- runFusedGatingDiagnostic(() => carriedRuleMaps(carried));
8406
- const pieces = carried.map((p) => materializePiece(p, trivia2));
9929
+ const carried = items.flatMap((item) => itemCarried(item, used, trivia2, opts?.hostMode));
9930
+ const detailed = once(() => carriedRuleMapsDetailed(carried));
9931
+ runFusedGatingDiagnostic(
9932
+ () => detailed().maps,
9933
+ void 0,
9934
+ () => detailed().opaque
9935
+ );
9936
+ const pieces = carried.map((p) => materializePiece(p, trivia2, false, opts?.hostMode));
8407
9937
  const map = fuseRules(pieces);
8408
9938
  Object.defineProperty(map, COMPOSED_PIECES, { value: carried, enumerable: false });
8409
9939
  if (trivia2) Object.defineProperty(map, COMPOSED_TRIVIA, { value: trivia2, enumerable: false });
@@ -8415,6 +9945,23 @@ function composeLeaf(_items) {
8415
9945
  throw new Error("composeLeaf(): requires Parseman macro lowering; runtime composition is forbidden");
8416
9946
  }
8417
9947
 
9948
+ // src/analysis/grammar.ts
9949
+ function analyzeGrammarGating(grammar, opts) {
9950
+ const recovered = recoverComposedRules(grammar);
9951
+ if (recovered === void 0) {
9952
+ return analyzeGatingRules(Object.entries(grammar), opts);
9953
+ }
9954
+ const { rules: winners, opaque } = recovered;
9955
+ const entries = [...winners];
9956
+ const report = analyzeGatingRules(entries, { ...opts, resolveRef: (name) => winners.get(name) });
9957
+ const fromOpaque = opaque.map((o) => ({
9958
+ rule: o.ruleNames.length > 0 ? o.ruleNames.join(", ") : `<artifact ${o.ns}>`,
9959
+ kind: "opaque-artifact",
9960
+ reason: `precompiled artifact "${o.ns}" carries compiled rule functions, not re-lowerable IR \u2014 its choices cannot be examined. Recompile the contributing grammar so it carries IR (the macro emits IR by default) to bring it back into analysis.`
9961
+ }));
9962
+ return fromOpaque.length === 0 ? report : { ...report, unanalysable: [...report.unanalysable, ...fromOpaque] };
9963
+ }
9964
+
8418
9965
  // src/combinators/gate.ts
8419
9966
  function gate(predicate) {
8420
9967
  const meta = {
@@ -8547,7 +10094,8 @@ function editNode(node2, at, delta, parentBase, parentShift, stats) {
8547
10094
  }
8548
10095
 
8549
10096
  // src/functional/doc.ts
8550
- function mkCtx(state, build, tolerant) {
10097
+ function mkCtx(rawState, build, tolerant) {
10098
+ const state = rawState ?? void 0;
8551
10099
  return tolerant ? { trackLines: false, state, build, _tolerant: true, _rec: REC, _errors: [] } : { trackLines: false, state, build };
8552
10100
  }
8553
10101
  function asRuleFn(entry) {
@@ -8944,6 +10492,7 @@ function collectEmbeddedErrors(node2, base, out) {
8944
10492
  function parseDoc(registry, rootRule, input, opts = {}) {
8945
10493
  const entry = registry[rootRule];
8946
10494
  if (!entry) throw new Error(`No rule '${rootRule}' in registry`);
10495
+ assertHostModeCompatible(fusedHostModeOf(registry), opts.build, fusedHostElidedOf(registry));
8947
10496
  const ctx = mkCtx(opts.state, opts.build, !!opts.tolerant);
8948
10497
  const r = asRuleFn(entry)(input, 0, ctx);
8949
10498
  if (r.ok) {
@@ -8969,6 +10518,12 @@ function runOnce(entry, input, options, phase, profileState) {
8969
10518
  const skipGlobalSinks = phase === "recognizer" || phase === "capture";
8970
10519
  const grammarTrivia = typeof entry !== "function" ? entry._meta.grammarTrivia : void 0;
8971
10520
  const grammarScanSkip = typeof entry !== "function" ? entry._meta.grammarScanSkip : void 0;
10521
+ const stamped = entry;
10522
+ assertHostModeCompatible(
10523
+ typeof entry === "function" ? stamped[FUSED_HOST_MODE] ?? "ast" : entry._meta.grammarHostMode ?? "ast",
10524
+ options.build,
10525
+ typeof entry === "function" && stamped[FUSED_HOST_ELIDED] === true
10526
+ );
8972
10527
  const ctx = {
8973
10528
  trackLines: false,
8974
10529
  ...skipGlobalSinks ? { _pmProfile: profile } : { _triviaLog: triviaLog, _errors: errors, ...profile === void 0 ? {} : { _pmProfile: profile } },
@@ -9631,8 +11186,12 @@ export {
9631
11186
  absoluteSpanCST,
9632
11187
  absolutize,
9633
11188
  absolutizeCST,
11189
+ alternationGroups,
11190
+ analyzeDuplication,
11191
+ analyzeDuplicationRules,
9634
11192
  analyzeGating,
9635
11193
  analyzeGatingRules,
11194
+ analyzeGrammarGating,
9636
11195
  annotateSpan,
9637
11196
  applyEdit,
9638
11197
  attempt,
@@ -9641,6 +11200,7 @@ export {
9641
11200
  buildLineIndex,
9642
11201
  buildOffsetIndex,
9643
11202
  buildTriviaIndex,
11203
+ charClassMembers,
9644
11204
  choice,
9645
11205
  collectLeafSlots,
9646
11206
  commentsIn,
@@ -9655,9 +11215,12 @@ export {
9655
11215
  createGrammarTraceSink,
9656
11216
  createVisitor,
9657
11217
  cstBuildHost,
11218
+ duplicationFindingCount,
9658
11219
  expect,
11220
+ extractCharClasses,
9659
11221
  field,
9660
11222
  firstSetToString,
11223
+ formatDuplicationFindings,
9661
11224
  formatGatingWarnings,
9662
11225
  gapIsSignificant,
9663
11226
  gapText,
@@ -9667,6 +11230,8 @@ export {
9667
11230
  indentMixed,
9668
11231
  indentWidth,
9669
11232
  isParseError,
11233
+ keywordAlternationHazards,
11234
+ keywordRegexShape,
9670
11235
  keywords,
9671
11236
  label,
9672
11237
  leaf,
@@ -9697,6 +11262,7 @@ export {
9697
11262
  sepBy,
9698
11263
  sequence,
9699
11264
  shiftAbsolute,
11265
+ siteToString,
9700
11266
  skip,
9701
11267
  token,
9702
11268
  transform,