parseman 0.48.0 → 0.49.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 (45) hide show
  1. package/dist/analysis/diagnostics.js +158 -51
  2. package/dist/analysis/diagnostics.js.map +2 -2
  3. package/dist/cli/index.js +160 -52
  4. package/dist/cli/index.js.map +2 -2
  5. package/dist/combinators/choice.d.ts.map +1 -1
  6. package/dist/compiler/ir-serialize.d.ts.map +1 -1
  7. package/dist/compiler/linker.d.ts.map +1 -1
  8. package/dist/index.js +169 -55
  9. package/dist/index.js.map +2 -2
  10. package/dist/language-service/index.js.map +1 -1
  11. package/dist/plugin/direct-builder-static.d.ts +20 -0
  12. package/dist/plugin/direct-builder-static.d.ts.map +1 -1
  13. package/dist/plugin/evaluator.d.ts +13 -0
  14. package/dist/plugin/evaluator.d.ts.map +1 -1
  15. package/dist/plugin/index.d.ts.map +1 -1
  16. package/dist/plugin/index.js +365 -71
  17. package/dist/plugin/index.js.map +2 -2
  18. package/dist/spec/index.js +12 -2
  19. package/dist/spec/index.js.map +2 -2
  20. package/dist/table/assemble.d.ts.map +1 -1
  21. package/dist/table/emit-assembly.d.ts.map +1 -1
  22. package/dist/table/encode.d.ts.map +1 -1
  23. package/dist/table/exec.d.ts.map +1 -1
  24. package/dist/table/index.js +158 -51
  25. package/dist/table/index.js.map +1 -1
  26. package/dist/table/lex-program.d.ts.map +1 -1
  27. package/dist/table/ops.d.ts +4 -4
  28. package/dist/types.d.ts +6 -1
  29. package/dist/types.d.ts.map +1 -1
  30. package/dist/version.d.ts +1 -1
  31. package/package.json +1 -1
  32. package/src/combinators/choice.ts +10 -1
  33. package/src/compiler/ir-serialize.ts +15 -3
  34. package/src/compiler/linker.ts +14 -1
  35. package/src/plugin/direct-builder-static.ts +116 -12
  36. package/src/plugin/evaluator.ts +50 -2
  37. package/src/plugin/index.ts +140 -1
  38. package/src/table/assemble.ts +82 -59
  39. package/src/table/emit-assembly.ts +10 -5
  40. package/src/table/encode.ts +6 -10
  41. package/src/table/exec.ts +13 -18
  42. package/src/table/lex-program.ts +15 -6
  43. package/src/table/ops.ts +4 -4
  44. package/src/types.ts +1 -1
  45. package/src/version.ts +1 -1
@@ -3422,6 +3422,7 @@ function choice(...args) {
3422
3422
  },
3423
3423
  parse(input, pos, ctx) {
3424
3424
  const expected = [];
3425
+ let expectedAt = pos;
3425
3426
  if (disjoint) {
3426
3427
  const code = pos < input.length ? input.codePointAt(pos) : -1;
3427
3428
  let idx = code >= 0 && code < 128 ? asciiDispatch[code] : -1;
@@ -3479,7 +3480,12 @@ function choice(...args) {
3479
3480
  const result = parsers[i].parse(input, pos, ctx);
3480
3481
  if (!result.ok) {
3481
3482
  rollbackTrivia(ctx, mark);
3482
- expected.push(...result.expected);
3483
+ const at = result.span.start;
3484
+ if (at > expectedAt) {
3485
+ expectedAt = at;
3486
+ expected.length = 0;
3487
+ }
3488
+ if (at === expectedAt) expected.push(...result.expected);
3483
3489
  if (result.committed) return { ok: false, expected, span: result.span, committed: true };
3484
3490
  continue;
3485
3491
  }
@@ -3490,6 +3496,9 @@ function choice(...args) {
3490
3496
  }
3491
3497
  return result;
3492
3498
  }
3499
+ if (expected.length === 0) {
3500
+ for (const parser2 of parsers) expected.push(...deriveExpected(parser2));
3501
+ }
3493
3502
  return { ok: false, expected, span: { start: pos, end: pos } };
3494
3503
  }
3495
3504
  };
@@ -5181,24 +5190,43 @@ function choiceNode(arms, classes, armExpected, choiceExpected) {
5181
5190
  };
5182
5191
  for (let i = arms.length - 1; i >= 0; i--) {
5183
5192
  const arm = arms[i], cls = classes[i], staticExpected = armExpected[i], next = tail;
5184
- tail = (input, pos, ctx, lead2, acc) => {
5193
+ tail = (input, pos, ctx, lead2, acc, best) => {
5185
5194
  if (cls !== void 0 && !classHas(cls, lead2)) {
5186
- return next(input, pos, ctx, lead2, appendExpected(acc, staticExpected));
5195
+ return next(
5196
+ input,
5197
+ pos,
5198
+ ctx,
5199
+ lead2,
5200
+ best === pos ? appendExpected(acc, staticExpected) : acc,
5201
+ best
5202
+ );
5187
5203
  }
5188
5204
  ctx._fc = false;
5189
5205
  const end = arm.run(input, pos, ctx);
5190
5206
  if (end >= 0) return end;
5191
- const nextAcc = appendExpected(acc, ctx._fx);
5207
+ const at = ctx._fe ?? pos;
5208
+ if (at > best) {
5209
+ best = at;
5210
+ acc = void 0;
5211
+ }
5212
+ const nextAcc = at === best ? appendExpected(acc, ctx._fx) : acc;
5192
5213
  if (committed(ctx)) {
5193
5214
  if (nextAcc !== void 0) ctx._fx = nextAcc;
5194
5215
  return -1;
5195
5216
  }
5196
- return next(input, pos, ctx, lead2, nextAcc);
5217
+ return next(input, pos, ctx, lead2, nextAcc, best);
5197
5218
  };
5198
5219
  }
5199
5220
  return {
5200
5221
  match: matchTail,
5201
- run: (input, pos, ctx) => tail(input, pos, ctx, pos < input.length ? input.codePointAt(pos) : -1, void 0)
5222
+ run: (input, pos, ctx) => tail(
5223
+ input,
5224
+ pos,
5225
+ ctx,
5226
+ pos < input.length ? input.codePointAt(pos) : -1,
5227
+ void 0,
5228
+ pos
5229
+ )
5202
5230
  };
5203
5231
  }
5204
5232
  function notNode(body, failure) {
@@ -9289,9 +9317,11 @@ ${catchCases}
9289
9317
  ctx._fc=false
9290
9318
  {const v=${arm}(input,pos,ctx)
9291
9319
  if(v!==FAIL)return v}
9292
- acc=${catchName}(${i},prev,acc)
9320
+ if(best===pos)acc=${catchName}(${i},prev,acc)
9293
9321
  prev=${i + 1}
9294
- acc=_accSet(ctx._fx,acc)
9322
+ {const at=ctx._fe??pos
9323
+ if(at>best){best=at;acc=undefined}
9324
+ if(at===best)acc=_accSet(ctx._fx,acc)}
9295
9325
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
9296
9326
  ${emitRollback(p, L.buf, sinks)}
9297
9327
  }`;
@@ -9300,19 +9330,22 @@ ${emitRollback(p, L.buf, sinks)}
9300
9330
  ctx._fc=false
9301
9331
  {const v=${arm}(input,pos,ctx)
9302
9332
  if(v!==FAIL)return v}
9303
- acc=_accSet(ctx._fx,acc)
9333
+ {const at=ctx._fe??pos
9334
+ if(at>best){best=at;acc=undefined}
9335
+ if(at===best)acc=_accSet(ctx._fx,acc)}
9304
9336
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
9305
9337
  ${emitRollback(p, L.buf, sinks)}
9306
- }else acc=_accSet(${expected[i]},acc)`).join("\n");
9338
+ }else if(best===pos)acc=_accSet(${expected[i]},acc)`).join("\n");
9307
9339
  return `${head}
9308
9340
  const c=lead(input,pos)
9309
9341
  ${emitMark(p, L.buf, sinks)}
9310
9342
  let acc
9343
+ let best=pos
9311
9344
  ${maskable ? `if(c<128){
9312
9345
  const bits=${maskName}[c<0?128:c]
9313
9346
  let prev=0
9314
9347
  ${maskArms}
9315
- acc=${catchName}(${n},prev,acc)
9348
+ if(best===pos)acc=${catchName}(${n},prev,acc)
9316
9349
  ctx._fe=pos;ctx._fx=acc??${choiceFx}
9317
9350
  return FAIL
9318
9351
  }
@@ -10431,14 +10464,19 @@ function assemble(t, prog, cfg) {
10431
10464
  };
10432
10465
  }
10433
10466
  function maskedChoiceBlock(start, a0, a1, a2, a3, through, next, total, choiceFx) {
10434
- return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
10467
+ return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
10435
10468
  if ((bits & 1 << start) !== 0) {
10436
10469
  ctx._fc = false;
10437
10470
  const v = a0(input, pos, ctx);
10438
10471
  if (v !== FAIL) return v;
10439
- acc = through(start, prev, acc);
10472
+ if (best === pos) acc = through(start, prev, acc);
10440
10473
  prev = start + 1;
10441
- acc = accSet(ctx._fx, acc);
10474
+ const at = ctx._fe ?? pos;
10475
+ if (at > best) {
10476
+ best = at;
10477
+ acc = void 0;
10478
+ }
10479
+ if (at === best) acc = accSet(ctx._fx, acc);
10442
10480
  if (committed3(ctx)) {
10443
10481
  if (acc !== void 0) ctx._fx = acc;
10444
10482
  return FAIL;
@@ -10449,9 +10487,14 @@ function assemble(t, prog, cfg) {
10449
10487
  ctx._fc = false;
10450
10488
  const v = a1(input, pos, ctx);
10451
10489
  if (v !== FAIL) return v;
10452
- acc = through(start + 1, prev, acc);
10490
+ if (best === pos) acc = through(start + 1, prev, acc);
10453
10491
  prev = start + 2;
10454
- acc = accSet(ctx._fx, acc);
10492
+ const at = ctx._fe ?? pos;
10493
+ if (at > best) {
10494
+ best = at;
10495
+ acc = void 0;
10496
+ }
10497
+ if (at === best) acc = accSet(ctx._fx, acc);
10455
10498
  if (committed3(ctx)) {
10456
10499
  if (acc !== void 0) ctx._fx = acc;
10457
10500
  return FAIL;
@@ -10462,9 +10505,14 @@ function assemble(t, prog, cfg) {
10462
10505
  ctx._fc = false;
10463
10506
  const v = a2(input, pos, ctx);
10464
10507
  if (v !== FAIL) return v;
10465
- acc = through(start + 2, prev, acc);
10508
+ if (best === pos) acc = through(start + 2, prev, acc);
10466
10509
  prev = start + 3;
10467
- acc = accSet(ctx._fx, acc);
10510
+ const at = ctx._fe ?? pos;
10511
+ if (at > best) {
10512
+ best = at;
10513
+ acc = void 0;
10514
+ }
10515
+ if (at === best) acc = accSet(ctx._fx, acc);
10468
10516
  if (committed3(ctx)) {
10469
10517
  if (acc !== void 0) ctx._fx = acc;
10470
10518
  return FAIL;
@@ -10475,9 +10523,14 @@ function assemble(t, prog, cfg) {
10475
10523
  ctx._fc = false;
10476
10524
  const v = a3(input, pos, ctx);
10477
10525
  if (v !== FAIL) return v;
10478
- acc = through(start + 3, prev, acc);
10526
+ if (best === pos) acc = through(start + 3, prev, acc);
10479
10527
  prev = start + 4;
10480
- acc = accSet(ctx._fx, acc);
10528
+ const at = ctx._fe ?? pos;
10529
+ if (at > best) {
10530
+ best = at;
10531
+ acc = void 0;
10532
+ }
10533
+ if (at === best) acc = accSet(ctx._fx, acc);
10481
10534
  if (committed3(ctx)) {
10482
10535
  if (acc !== void 0) ctx._fx = acc;
10483
10536
  return FAIL;
@@ -10485,23 +10538,28 @@ function assemble(t, prog, cfg) {
10485
10538
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
10486
10539
  }
10487
10540
  if (next !== void 0) {
10488
- return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev);
10541
+ return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best);
10489
10542
  }
10490
- acc = through(total, prev, acc);
10543
+ if (best === pos) acc = through(total, prev, acc);
10491
10544
  ctx._fe = pos;
10492
10545
  ctx._fx = acc ?? choiceFx;
10493
10546
  return FAIL;
10494
10547
  };
10495
10548
  }
10496
10549
  function generalChoiceBlock(start, a0, a1, a2, a3, g0, g1, g2, g3, through, next, total, choiceFx) {
10497
- return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
10550
+ return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
10498
10551
  if (g0 === null || classHas2(g0, c)) {
10499
10552
  ctx._fc = false;
10500
10553
  const v = a0(input, pos, ctx);
10501
10554
  if (v !== FAIL) return v;
10502
- acc = through(start, prev, acc);
10555
+ if (best === pos) acc = through(start, prev, acc);
10503
10556
  prev = start + 1;
10504
- acc = accSet(ctx._fx, acc);
10557
+ const at = ctx._fe ?? pos;
10558
+ if (at > best) {
10559
+ best = at;
10560
+ acc = void 0;
10561
+ }
10562
+ if (at === best) acc = accSet(ctx._fx, acc);
10505
10563
  if (committed3(ctx)) {
10506
10564
  if (acc !== void 0) ctx._fx = acc;
10507
10565
  return FAIL;
@@ -10512,9 +10570,14 @@ function assemble(t, prog, cfg) {
10512
10570
  ctx._fc = false;
10513
10571
  const v = a1(input, pos, ctx);
10514
10572
  if (v !== FAIL) return v;
10515
- acc = through(start + 1, prev, acc);
10573
+ if (best === pos) acc = through(start + 1, prev, acc);
10516
10574
  prev = start + 2;
10517
- acc = accSet(ctx._fx, acc);
10575
+ const at = ctx._fe ?? pos;
10576
+ if (at > best) {
10577
+ best = at;
10578
+ acc = void 0;
10579
+ }
10580
+ if (at === best) acc = accSet(ctx._fx, acc);
10518
10581
  if (committed3(ctx)) {
10519
10582
  if (acc !== void 0) ctx._fx = acc;
10520
10583
  return FAIL;
@@ -10525,9 +10588,14 @@ function assemble(t, prog, cfg) {
10525
10588
  ctx._fc = false;
10526
10589
  const v = a2(input, pos, ctx);
10527
10590
  if (v !== FAIL) return v;
10528
- acc = through(start + 2, prev, acc);
10591
+ if (best === pos) acc = through(start + 2, prev, acc);
10529
10592
  prev = start + 3;
10530
- acc = accSet(ctx._fx, acc);
10593
+ const at = ctx._fe ?? pos;
10594
+ if (at > best) {
10595
+ best = at;
10596
+ acc = void 0;
10597
+ }
10598
+ if (at === best) acc = accSet(ctx._fx, acc);
10531
10599
  if (committed3(ctx)) {
10532
10600
  if (acc !== void 0) ctx._fx = acc;
10533
10601
  return FAIL;
@@ -10538,9 +10606,14 @@ function assemble(t, prog, cfg) {
10538
10606
  ctx._fc = false;
10539
10607
  const v = a3(input, pos, ctx);
10540
10608
  if (v !== FAIL) return v;
10541
- acc = through(start + 3, prev, acc);
10609
+ if (best === pos) acc = through(start + 3, prev, acc);
10542
10610
  prev = start + 4;
10543
- acc = accSet(ctx._fx, acc);
10611
+ const at = ctx._fe ?? pos;
10612
+ if (at > best) {
10613
+ best = at;
10614
+ acc = void 0;
10615
+ }
10616
+ if (at === best) acc = accSet(ctx._fx, acc);
10544
10617
  if (committed3(ctx)) {
10545
10618
  if (acc !== void 0) ctx._fx = acc;
10546
10619
  return FAIL;
@@ -10548,9 +10621,9 @@ function assemble(t, prog, cfg) {
10548
10621
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
10549
10622
  }
10550
10623
  if (next !== void 0) {
10551
- return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev);
10624
+ return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best);
10552
10625
  }
10553
- acc = through(total, prev, acc);
10626
+ if (best === pos) acc = through(total, prev, acc);
10554
10627
  ctx._fe = pos;
10555
10628
  ctx._fx = acc ?? choiceFx;
10556
10629
  return FAIL;
@@ -11906,6 +11979,7 @@ function assemble(t, prog, cfg) {
11906
11979
  const mLog = need ? ctx._triviaLog?.length ?? 0 : 0;
11907
11980
  const mRoot = need ? ctx._rootTriviaLog?.length ?? 0 : 0;
11908
11981
  let acc;
11982
+ let best = pos;
11909
11983
  if (c < 128) {
11910
11984
  const bits = mask[c < 0 ? 128 : c];
11911
11985
  let prev = 0;
@@ -11914,7 +11988,12 @@ function assemble(t, prog, cfg) {
11914
11988
  const v = a0(input, pos, ctx);
11915
11989
  if (v !== FAIL) return v;
11916
11990
  prev = 1;
11917
- acc = accSet(ctx._fx, acc);
11991
+ const at = ctx._fe ?? pos;
11992
+ if (at > best) {
11993
+ best = at;
11994
+ acc = void 0;
11995
+ }
11996
+ if (at === best) acc = accSet(ctx._fx, acc);
11918
11997
  if (committed3(ctx)) {
11919
11998
  if (acc !== void 0) ctx._fx = acc;
11920
11999
  return FAIL;
@@ -11925,9 +12004,14 @@ function assemble(t, prog, cfg) {
11925
12004
  ctx._fc = false;
11926
12005
  const v = a1(input, pos, ctx);
11927
12006
  if (v !== FAIL) return v;
11928
- if (prev < 1) acc = accSet(e0, acc);
12007
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
11929
12008
  prev = 2;
11930
- acc = accSet(ctx._fx, acc);
12009
+ const at = ctx._fe ?? pos;
12010
+ if (at > best) {
12011
+ best = at;
12012
+ acc = void 0;
12013
+ }
12014
+ if (at === best) acc = accSet(ctx._fx, acc);
11931
12015
  if (committed3(ctx)) {
11932
12016
  if (acc !== void 0) ctx._fx = acc;
11933
12017
  return FAIL;
@@ -11938,19 +12022,24 @@ function assemble(t, prog, cfg) {
11938
12022
  ctx._fc = false;
11939
12023
  const v = a2(input, pos, ctx);
11940
12024
  if (v !== FAIL) return v;
11941
- if (prev < 1) acc = accSet(e0, acc);
11942
- if (prev < 2) acc = accSet(e1, acc);
12025
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
12026
+ if (best === pos && prev < 2) acc = accSet(e1, acc);
11943
12027
  prev = 3;
11944
- acc = accSet(ctx._fx, acc);
12028
+ const at = ctx._fe ?? pos;
12029
+ if (at > best) {
12030
+ best = at;
12031
+ acc = void 0;
12032
+ }
12033
+ if (at === best) acc = accSet(ctx._fx, acc);
11945
12034
  if (committed3(ctx)) {
11946
12035
  if (acc !== void 0) ctx._fx = acc;
11947
12036
  return FAIL;
11948
12037
  }
11949
12038
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11950
12039
  }
11951
- if (prev < 1) acc = accSet(e0, acc);
11952
- if (prev < 2) acc = accSet(e1, acc);
11953
- if (n === 3 && prev < 3) acc = accSet(e2, acc);
12040
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
12041
+ if (best === pos && prev < 2) acc = accSet(e1, acc);
12042
+ if (best === pos && n === 3 && prev < 3) acc = accSet(e2, acc);
11954
12043
  ctx._fe = pos;
11955
12044
  ctx._fx = acc ?? choiceFx;
11956
12045
  return FAIL;
@@ -11959,36 +12048,51 @@ function assemble(t, prog, cfg) {
11959
12048
  ctx._fc = false;
11960
12049
  const v = a0(input, pos, ctx);
11961
12050
  if (v !== FAIL) return v;
11962
- acc = accSet(ctx._fx, acc);
12051
+ const at = ctx._fe ?? pos;
12052
+ if (at > best) {
12053
+ best = at;
12054
+ acc = void 0;
12055
+ }
12056
+ if (at === best) acc = accSet(ctx._fx, acc);
11963
12057
  if (committed3(ctx)) {
11964
12058
  if (acc !== void 0) ctx._fx = acc;
11965
12059
  return FAIL;
11966
12060
  }
11967
12061
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11968
- } else acc = accSet(e0, acc);
12062
+ } else if (best === pos) acc = accSet(e0, acc);
11969
12063
  if (g1 === null || classHas2(g1, c)) {
11970
12064
  ctx._fc = false;
11971
12065
  const v = a1(input, pos, ctx);
11972
12066
  if (v !== FAIL) return v;
11973
- acc = accSet(ctx._fx, acc);
12067
+ const at = ctx._fe ?? pos;
12068
+ if (at > best) {
12069
+ best = at;
12070
+ acc = void 0;
12071
+ }
12072
+ if (at === best) acc = accSet(ctx._fx, acc);
11974
12073
  if (committed3(ctx)) {
11975
12074
  if (acc !== void 0) ctx._fx = acc;
11976
12075
  return FAIL;
11977
12076
  }
11978
12077
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11979
- } else acc = accSet(e1, acc);
12078
+ } else if (best === pos) acc = accSet(e1, acc);
11980
12079
  if (n === 3) {
11981
12080
  if (g2 === null || classHas2(g2, c)) {
11982
12081
  ctx._fc = false;
11983
12082
  const v = a2(input, pos, ctx);
11984
12083
  if (v !== FAIL) return v;
11985
- acc = accSet(ctx._fx, acc);
12084
+ const at = ctx._fe ?? pos;
12085
+ if (at > best) {
12086
+ best = at;
12087
+ acc = void 0;
12088
+ }
12089
+ if (at === best) acc = accSet(ctx._fx, acc);
11986
12090
  if (committed3(ctx)) {
11987
12091
  if (acc !== void 0) ctx._fx = acc;
11988
12092
  return FAIL;
11989
12093
  }
11990
12094
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11991
- } else acc = accSet(e2, acc);
12095
+ } else if (best === pos) acc = accSet(e2, acc);
11992
12096
  }
11993
12097
  ctx._fe = pos;
11994
12098
  ctx._fx = acc ?? choiceFx;
@@ -12086,7 +12190,8 @@ function assemble(t, prog, cfg) {
12086
12190
  mLog,
12087
12191
  mRoot,
12088
12192
  void 0,
12089
- 0
12193
+ 0,
12194
+ pos
12090
12195
  );
12091
12196
  }
12092
12197
  return general(
@@ -12103,7 +12208,8 @@ function assemble(t, prog, cfg) {
12103
12208
  mLog,
12104
12209
  mRoot,
12105
12210
  void 0,
12106
- 0
12211
+ 0,
12212
+ pos
12107
12213
  );
12108
12214
  };
12109
12215
  }
@@ -12131,7 +12237,8 @@ function assemble(t, prog, cfg) {
12131
12237
  mLog,
12132
12238
  mRoot,
12133
12239
  void 0,
12134
- 0
12240
+ 0,
12241
+ pos
12135
12242
  );
12136
12243
  };
12137
12244
  }
@@ -14916,7 +15023,7 @@ function evalRuleMapIR(ir) {
14916
15023
  l._def.fnSrc = src;
14917
15024
  return l;
14918
15025
  };
14919
- const _nd = (type, child, src, opts, staticError, sigSrc) => {
15026
+ const _nd = (type, child, src, opts, staticError, sigSrc, buildImports) => {
14920
15027
  if (staticError !== void 0 && staticError.length > 0) {
14921
15028
  throw new Error(`IR direct node builder for ${type} must be macro-static and self-contained; unsupported binding(s): ${staticError.join(", ")}`);
14922
15029
  }
@@ -14926,6 +15033,7 @@ function evalRuleMapIR(ir) {
14926
15033
  n._def.buildSrc = src;
14927
15034
  if (staticError !== void 0) n._def.buildStaticError = staticError;
14928
15035
  if (sigSrc !== void 0) n._def.buildSigSrc = sigSrc;
15036
+ if (buildImports !== void 0) n._def.buildImports = buildImports;
14929
15037
  return n;
14930
15038
  };
14931
15039
  const _gch = (items) => {
@@ -15317,11 +15425,12 @@ ${outerDecls.map((d) => " " + d).join("\n")}
15317
15425
  }
15318
15426
  if (def.buildSrc !== void 0) {
15319
15427
  const tail = [];
15320
- if (def.buildStaticError !== void 0 || def.buildSigSrc !== void 0) {
15428
+ if (def.buildStaticError !== void 0 || def.buildSigSrc !== void 0 || def.buildImports !== void 0) {
15321
15429
  if (!opts) tail.push("undefined");
15322
15430
  tail.push(def.buildStaticError === void 0 ? "undefined" : JSON.stringify(def.buildStaticError));
15323
15431
  }
15324
- if (def.buildSigSrc !== void 0) tail.push(JSON.stringify(def.buildSigSrc));
15432
+ if (def.buildSigSrc !== void 0 || def.buildImports !== void 0) tail.push(def.buildSigSrc === void 0 ? "undefined" : JSON.stringify(def.buildSigSrc));
15433
+ if (def.buildImports !== void 0) tail.push(JSON.stringify(def.buildImports));
15325
15434
  const trailing = tail.length > 0 ? `, ${tail.join(", ")}` : "";
15326
15435
  return `_nd(${JSON.stringify(def.type)}, ${kid(def.parser)}, ${JSON.stringify(def.buildSrc)}${opts}${trailing})`;
15327
15436
  }
@@ -15468,7 +15577,7 @@ function compileRuleMap(ruleMap, opts = {}) {
15468
15577
  }
15469
15578
 
15470
15579
  // src/version.ts
15471
- var PARSEMAN_VERSION = "0.48.0";
15580
+ var PARSEMAN_VERSION = "0.49.0";
15472
15581
 
15473
15582
  // src/compiler/compile-linkable-table.ts
15474
15583
  function isLocal(rule) {
@@ -15661,7 +15770,17 @@ var STATIC_BUILDER_GLOBALS = /* @__PURE__ */ new Set([
15661
15770
  "Infinity",
15662
15771
  "parseFloat",
15663
15772
  "parseInt",
15664
- "undefined"
15773
+ "undefined",
15774
+ // Frozen intrinsic error constructors. Reducers pervasively `throw new TypeError(…)`;
15775
+ // once the statement walker walks a `throw` argument these surface as free names, and
15776
+ // refusing them would re-refuse the very productions the block-body lift clears. They
15777
+ // carry no closure state, so admitting them cannot make an unanalyzable builder
15778
+ // analyzable by accident. A MODULE-SCOPE custom error stays a free name (rescued by
15779
+ // provenance or refused) — unchanged.
15780
+ "Error",
15781
+ "TypeError",
15782
+ "RangeError",
15783
+ "SyntaxError"
15665
15784
  ]);
15666
15785
  function astArray(value) {
15667
15786
  return Array.isArray(value) ? value : [];
@@ -15674,31 +15793,42 @@ function unwrapCallbackExpression(node2) {
15674
15793
  while (current?.type === "ParenthesizedExpression") current = astChild(current.expression) ?? void 0;
15675
15794
  return current;
15676
15795
  }
15677
- function directBuilderUnsupportedBindings(src) {
15796
+ function directBuilderBindings(src) {
15678
15797
  let init;
15679
15798
  try {
15680
15799
  const parsed = parseSync("parseman-direct-builder.ts", `const _direct = (${src})`);
15681
- if (parsed.errors.length > 0) return ["invalid callback source"];
15800
+ if (parsed.errors.length > 0) return { structural: ["invalid callback source"], free: [] };
15682
15801
  const program = parsed.program;
15683
- const statement = program.body?.[0];
15684
- const declaration = statement?.declarations?.[0];
15802
+ const statementNode = program.body?.[0];
15803
+ const declaration = statementNode?.declarations?.[0];
15685
15804
  init = unwrapCallbackExpression(declaration?.init);
15686
15805
  } catch {
15687
- return ["invalid callback source"];
15806
+ return { structural: ["invalid callback source"], free: [] };
15688
15807
  }
15689
- if (init?.type !== "ArrowFunctionExpression" || !Array.isArray(init.params)) return ["unsupported callback shape"];
15808
+ if (init === void 0) return { structural: ["unsupported callback shape"], free: [] };
15809
+ const isCallable = init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression" || init.type === "FunctionDeclaration";
15810
+ if (!isCallable || !Array.isArray(init.params)) return { structural: ["unsupported callback shape"], free: [] };
15811
+ if (init.async === true || init.generator === true) return { structural: ["unsupported callback shape"], free: [] };
15690
15812
  const allowed = new Set(STATIC_BUILDER_GLOBALS);
15691
15813
  for (const param of init.params) {
15692
- if (param.type !== "Identifier" || typeof param.name !== "string") return ["unsupported parameter pattern"];
15814
+ if (param.type !== "Identifier" || typeof param.name !== "string") return { structural: ["unsupported parameter pattern"], free: [] };
15693
15815
  allowed.add(param.name);
15694
15816
  }
15695
- const unsupported = /* @__PURE__ */ new Set();
15817
+ const selfName = astChild(init.id);
15818
+ if (selfName?.type === "Identifier" && typeof selfName.name === "string") allowed.add(selfName.name);
15819
+ const structural = /* @__PURE__ */ new Set();
15820
+ const free = /* @__PURE__ */ new Set();
15696
15821
  const reportUnsupportedNode = (ast) => {
15697
- unsupported.add(`unsupported ${ast.type}`);
15822
+ structural.add(`unsupported ${ast.type}`);
15698
15823
  };
15699
15824
  const read = (ast, allowedNames) => {
15700
15825
  if (typeof ast.name === "string" && allowedNames.has(ast.name)) return;
15701
- unsupported.add(typeof ast.name === "string" ? ast.name : "identifier");
15826
+ if (ast.name === "arguments") {
15827
+ structural.add("unsupported arguments");
15828
+ return;
15829
+ }
15830
+ if (typeof ast.name === "string") free.add(ast.name);
15831
+ else structural.add("unsupported identifier");
15702
15832
  };
15703
15833
  const expression = (node2, allowedNames) => {
15704
15834
  const ast = astChild(node2);
@@ -15775,8 +15905,9 @@ function directBuilderUnsupportedBindings(src) {
15775
15905
  }
15776
15906
  nested.add(param.name);
15777
15907
  }
15778
- if (astChild(ast.body)?.type === "BlockStatement") {
15779
- reportUnsupportedNode(astChild(ast.body));
15908
+ const nestedBody = astChild(ast.body);
15909
+ if (nestedBody?.type === "BlockStatement") {
15910
+ statement(nestedBody, nested);
15780
15911
  return;
15781
15912
  }
15782
15913
  expression(ast.body, nested);
@@ -15787,9 +15918,70 @@ function directBuilderUnsupportedBindings(src) {
15787
15918
  return;
15788
15919
  }
15789
15920
  };
15790
- if (astChild(init.body)?.type === "BlockStatement") unsupported.add("unsupported BlockStatement");
15921
+ const statement = (node2, names) => {
15922
+ const ast = astChild(node2);
15923
+ if (!ast) return;
15924
+ switch (ast.type) {
15925
+ case "VariableDeclaration":
15926
+ for (const d of astArray(ast.declarations)) {
15927
+ expression(d.init, names);
15928
+ const idAst = astChild(d.id);
15929
+ if (idAst?.type === "Identifier" && typeof idAst.name === "string") names.add(idAst.name);
15930
+ else reportUnsupportedNode(idAst ?? ast);
15931
+ }
15932
+ return;
15933
+ case "ReturnStatement":
15934
+ expression(ast.argument, names);
15935
+ return;
15936
+ case "ExpressionStatement":
15937
+ expression(ast.expression, names);
15938
+ return;
15939
+ case "ThrowStatement":
15940
+ expression(ast.argument, names);
15941
+ return;
15942
+ case "IfStatement":
15943
+ expression(ast.test, names);
15944
+ statement(ast.consequent, new Set(names));
15945
+ if (ast.alternate) statement(ast.alternate, new Set(names));
15946
+ return;
15947
+ case "ForStatement": {
15948
+ const inner = new Set(names);
15949
+ if (ast.init) {
15950
+ const initAst = astChild(ast.init);
15951
+ if (initAst?.type === "VariableDeclaration") statement(initAst, inner);
15952
+ else expression(initAst, inner);
15953
+ }
15954
+ if (ast.test) expression(ast.test, inner);
15955
+ if (ast.update) expression(ast.update, inner);
15956
+ statement(ast.body, inner);
15957
+ return;
15958
+ }
15959
+ case "ForOfStatement":
15960
+ case "ForInStatement": {
15961
+ const inner = new Set(names);
15962
+ statement(ast.left, inner);
15963
+ expression(ast.right, names);
15964
+ statement(ast.body, inner);
15965
+ return;
15966
+ }
15967
+ case "BlockStatement": {
15968
+ const inner = new Set(names);
15969
+ for (const s of astArray(ast.body)) statement(s, inner);
15970
+ return;
15971
+ }
15972
+ case "BreakStatement":
15973
+ case "ContinueStatement":
15974
+ case "EmptyStatement":
15975
+ return;
15976
+ default:
15977
+ reportUnsupportedNode(ast);
15978
+ return;
15979
+ }
15980
+ };
15981
+ const bodyAst = astChild(init.body);
15982
+ if (bodyAst?.type === "BlockStatement") statement(bodyAst, allowed);
15791
15983
  else expression(init.body, allowed);
15792
- return [...unsupported];
15984
+ return { structural: [...structural], free: [...free] };
15793
15985
  }
15794
15986
 
15795
15987
  // src/plugin/evaluator.ts
@@ -15844,6 +16036,10 @@ function setReducerResolver(r, entrySource = null) {
15844
16036
  _reducers = r;
15845
16037
  _entrySource = entrySource;
15846
16038
  }
16039
+ var _builderImports = null;
16040
+ function setBuilderImportResolver(r) {
16041
+ _builderImports = r;
16042
+ }
15847
16043
  function isWordFactory(v) {
15848
16044
  return !!v && typeof v === "object" && v.tag === "wordFactory";
15849
16045
  }
@@ -16230,8 +16426,21 @@ function exprToCombi(node2, scope, code, mfs) {
16230
16426
  if (combi._def.buildArity === void 0 && resolved.arity !== null) combi._def.buildArity = resolved.arity;
16231
16427
  if (resolved.reason !== void 0) combi._def.buildArityUnresolved = resolved.reason;
16232
16428
  }
16233
- const staticError = directBuilderUnsupportedBindings(buildSrc);
16429
+ const analysisSrc = combi._def.buildSigSrc ?? buildSrc;
16430
+ const report = directBuilderBindings(analysisSrc);
16431
+ const isInlineBuilder = combi._def.buildSigSrc === void 0;
16432
+ const carriedImports = [];
16433
+ const unresolved = [];
16434
+ if (isInlineBuilder) {
16435
+ for (const name of report.free) {
16436
+ const prov = _builderImports?.(name) ?? null;
16437
+ if (prov) carriedImports.push({ local: name, source: prov.source, imported: prov.imported });
16438
+ else unresolved.push(name);
16439
+ }
16440
+ }
16441
+ const staticError = [...report.structural, ...unresolved];
16234
16442
  if (staticError.length > 0) combi._def.buildStaticError = staticError;
16443
+ if (carriedImports.length > 0) combi._def.buildImports = carriedImports;
16235
16444
  }
16236
16445
  return combi;
16237
16446
  } catch {
@@ -17625,6 +17834,7 @@ function transformMacro(code, id, moduleAliases = /* @__PURE__ */ new Set([PARSE
17625
17834
  return transformMacroImpl(code, id, moduleAliases, warnUnloweredRegex, recovery, grammarCoverage);
17626
17835
  } finally {
17627
17836
  setReducerResolver(null);
17837
+ setBuilderImportResolver(null);
17628
17838
  for (const d of unwindDegradationCapture(depth)) console.warn(formatDegradation(d));
17629
17839
  }
17630
17840
  }
@@ -17690,6 +17900,7 @@ function transformMacroImpl(code, id, moduleAliases, warnUnloweredRegex, recover
17690
17900
  }
17691
17901
  const reducerResolver = createReducerResolver(id, body, code);
17692
17902
  setReducerResolver(reducerResolver, code);
17903
+ setBuilderImportResolver((name) => importBindings.get(name) ?? null);
17693
17904
  const topLevelFunction = (moduleBody, name) => {
17694
17905
  for (const st of moduleBody) {
17695
17906
  const decl = st.type === "ExportNamedDeclaration" ? st.declaration : st;
@@ -17793,6 +18004,48 @@ function transformMacroImpl(code, id, moduleAliases, warnUnloweredRegex, recover
17793
18004
  let anyUnresolved = false;
17794
18005
  let keepMacroImport = false;
17795
18006
  let usedTableRuntime = false;
18007
+ const pendingBuilderImports = /* @__PURE__ */ new Map();
18008
+ const collectBuilderImports = (ruleMap) => {
18009
+ const seen = /* @__PURE__ */ new Set();
18010
+ const walk = (v) => {
18011
+ if (v === null || typeof v !== "object" || seen.has(v)) return;
18012
+ seen.add(v);
18013
+ const def = v._def;
18014
+ for (const bi of def?.buildImports ?? []) {
18015
+ let m = pendingBuilderImports.get(bi.source);
18016
+ if (!m) {
18017
+ m = /* @__PURE__ */ new Map();
18018
+ pendingBuilderImports.set(bi.source, m);
18019
+ }
18020
+ m.set(bi.local, bi.imported);
18021
+ }
18022
+ if (def?.tag === "lazy" && v._ruleName === void 0 && typeof def.thunk === "function") {
18023
+ let target = null;
18024
+ try {
18025
+ target = def.thunk();
18026
+ } catch {
18027
+ }
18028
+ walk(target);
18029
+ }
18030
+ for (const child of Object.values(v)) {
18031
+ if (Array.isArray(child)) {
18032
+ for (const c of child) walk(c);
18033
+ } else walk(child);
18034
+ }
18035
+ };
18036
+ for (const [, rule] of ruleMap) {
18037
+ walk(rule);
18038
+ const def = rule._def;
18039
+ if (def?.tag === "lazy" && typeof def.thunk === "function") {
18040
+ let body2 = null;
18041
+ try {
18042
+ body2 = def.thunk();
18043
+ } catch {
18044
+ }
18045
+ walk(body2);
18046
+ }
18047
+ }
18048
+ };
17796
18049
  const exportedFactories = [];
17797
18050
  let runtimeComposeFallback = false;
17798
18051
  let ruleMapHolderCounter = 0;
@@ -18096,6 +18349,7 @@ function transformMacroImpl(code, id, moduleAliases, warnUnloweredRegex, recover
18096
18349
  const merged = mergedCarriedRules(carried);
18097
18350
  if (merged !== null) {
18098
18351
  if (composing) applyComposingTrivia(merged.rules, composing);
18352
+ collectBuilderImports(merged.rules);
18099
18353
  const refusals = [];
18100
18354
  const compiled = compileRuleMap(merged.rules, {
18101
18355
  ...composing ? { trivia: composing } : {},
@@ -18543,6 +18797,45 @@ ${which}
18543
18797
  ms.prepend(`import { tableRules } from ${JSON.stringify(TABLE_RUNTIME_SPECIFIER)}
18544
18798
  `);
18545
18799
  }
18800
+ if (applied.length > 0 && pendingBuilderImports.size > 0) {
18801
+ const boundLocally = /* @__PURE__ */ new Set();
18802
+ for (const stmt of body) {
18803
+ if (stmt.type === "ImportDeclaration") {
18804
+ for (const sp of stmt.specifiers) boundLocally.add(sp.local.name);
18805
+ continue;
18806
+ }
18807
+ const decl = stmt.type === "ExportNamedDeclaration" ? stmt.declaration : stmt;
18808
+ if (decl?.type === "FunctionDeclaration" || decl?.type === "ClassDeclaration") {
18809
+ const n = decl.id?.name;
18810
+ if (n) boundLocally.add(n);
18811
+ continue;
18812
+ }
18813
+ const vd = unwrapVd(stmt);
18814
+ for (const d of vd?.declarations ?? []) {
18815
+ const n = d.id.name;
18816
+ if (n) boundLocally.add(n);
18817
+ }
18818
+ }
18819
+ const emitted = /* @__PURE__ */ new Set();
18820
+ for (const [source, locals] of pendingBuilderImports) {
18821
+ const needed = [];
18822
+ for (const [local, imported] of locals) {
18823
+ const own = importBindings.get(local);
18824
+ if (own && own.source === source && own.imported === imported) continue;
18825
+ if (boundLocally.has(local) || emitted.has(local)) {
18826
+ throw new Error(
18827
+ `${id} \u2014 parseman will not emit this module: a fused builder needs \`${imported}\` from ${JSON.stringify(source)} bound as \`${local}\`, but this module already binds \`${local}\` to something else. The inlined builder source names \`${local}\` verbatim, so the import cannot be aliased; rename the conflicting local binding so the builder's import can be re-emitted.`
18828
+ );
18829
+ }
18830
+ emitted.add(local);
18831
+ needed.push([local, imported]);
18832
+ }
18833
+ if (needed.length === 0) continue;
18834
+ const specs = needed.map(([local, imported]) => imported === local ? local : `${imported} as ${local}`);
18835
+ ms.prepend(`import { ${specs.join(", ")} } from ${JSON.stringify(source)}
18836
+ `);
18837
+ }
18838
+ }
18546
18839
  if (applied.length > 0) {
18547
18840
  ms.prepend(
18548
18841
  `// Generated by parseman v${PARSEMAN_VERSION} \u2014 DO NOT EDIT.
@@ -18567,6 +18860,7 @@ ${free.map(where).join("\n")}${cause}`
18567
18860
  }
18568
18861
  }
18569
18862
  setReducerResolver(null);
18863
+ setBuilderImportResolver(null);
18570
18864
  const degradations = endDegradationCapture();
18571
18865
  if (degradations.length > 0) {
18572
18866
  const lines = formatDegradations(degradations);