parseman 0.48.0 → 0.48.1

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.
@@ -2859,6 +2859,7 @@ function choice(...args) {
2859
2859
  },
2860
2860
  parse(input, pos, ctx) {
2861
2861
  const expected = [];
2862
+ let expectedAt = pos;
2862
2863
  if (disjoint) {
2863
2864
  const code = pos < input.length ? input.codePointAt(pos) : -1;
2864
2865
  let idx = code >= 0 && code < 128 ? asciiDispatch[code] : -1;
@@ -2916,7 +2917,12 @@ function choice(...args) {
2916
2917
  const result = parsers[i].parse(input, pos, ctx);
2917
2918
  if (!result.ok) {
2918
2919
  rollbackTrivia(ctx, mark);
2919
- expected.push(...result.expected);
2920
+ const at = result.span.start;
2921
+ if (at > expectedAt) {
2922
+ expectedAt = at;
2923
+ expected.length = 0;
2924
+ }
2925
+ if (at === expectedAt) expected.push(...result.expected);
2920
2926
  if (result.committed) return { ok: false, expected, span: result.span, committed: true };
2921
2927
  continue;
2922
2928
  }
@@ -2927,6 +2933,9 @@ function choice(...args) {
2927
2933
  }
2928
2934
  return result;
2929
2935
  }
2936
+ if (expected.length === 0) {
2937
+ for (const parser2 of parsers) expected.push(...deriveExpected(parser2));
2938
+ }
2930
2939
  return { ok: false, expected, span: { start: pos, end: pos } };
2931
2940
  }
2932
2941
  };
@@ -3950,24 +3959,43 @@ function choiceNode(arms, classes, armExpected, choiceExpected) {
3950
3959
  };
3951
3960
  for (let i = arms.length - 1; i >= 0; i--) {
3952
3961
  const arm = arms[i], cls = classes[i], staticExpected = armExpected[i], next = tail;
3953
- tail = (input, pos, ctx, lead2, acc) => {
3962
+ tail = (input, pos, ctx, lead2, acc, best) => {
3954
3963
  if (cls !== void 0 && !classHas(cls, lead2)) {
3955
- return next(input, pos, ctx, lead2, appendExpected(acc, staticExpected));
3964
+ return next(
3965
+ input,
3966
+ pos,
3967
+ ctx,
3968
+ lead2,
3969
+ best === pos ? appendExpected(acc, staticExpected) : acc,
3970
+ best
3971
+ );
3956
3972
  }
3957
3973
  ctx._fc = false;
3958
3974
  const end = arm.run(input, pos, ctx);
3959
3975
  if (end >= 0) return end;
3960
- const nextAcc = appendExpected(acc, ctx._fx);
3976
+ const at = ctx._fe ?? pos;
3977
+ if (at > best) {
3978
+ best = at;
3979
+ acc = void 0;
3980
+ }
3981
+ const nextAcc = at === best ? appendExpected(acc, ctx._fx) : acc;
3961
3982
  if (committed(ctx)) {
3962
3983
  if (nextAcc !== void 0) ctx._fx = nextAcc;
3963
3984
  return -1;
3964
3985
  }
3965
- return next(input, pos, ctx, lead2, nextAcc);
3986
+ return next(input, pos, ctx, lead2, nextAcc, best);
3966
3987
  };
3967
3988
  }
3968
3989
  return {
3969
3990
  match: matchTail,
3970
- run: (input, pos, ctx) => tail(input, pos, ctx, pos < input.length ? input.codePointAt(pos) : -1, void 0)
3991
+ run: (input, pos, ctx) => tail(
3992
+ input,
3993
+ pos,
3994
+ ctx,
3995
+ pos < input.length ? input.codePointAt(pos) : -1,
3996
+ void 0,
3997
+ pos
3998
+ )
3971
3999
  };
3972
4000
  }
3973
4001
  function notNode(body, failure) {
@@ -8760,9 +8788,11 @@ ${catchCases}
8760
8788
  ctx._fc=false
8761
8789
  {const v=${arm}(input,pos,ctx)
8762
8790
  if(v!==FAIL)return v}
8763
- acc=${catchName}(${i},prev,acc)
8791
+ if(best===pos)acc=${catchName}(${i},prev,acc)
8764
8792
  prev=${i + 1}
8765
- acc=_accSet(ctx._fx,acc)
8793
+ {const at=ctx._fe??pos
8794
+ if(at>best){best=at;acc=undefined}
8795
+ if(at===best)acc=_accSet(ctx._fx,acc)}
8766
8796
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
8767
8797
  ${emitRollback(p, L.buf, sinks)}
8768
8798
  }`;
@@ -8771,19 +8801,22 @@ ${emitRollback(p, L.buf, sinks)}
8771
8801
  ctx._fc=false
8772
8802
  {const v=${arm}(input,pos,ctx)
8773
8803
  if(v!==FAIL)return v}
8774
- acc=_accSet(ctx._fx,acc)
8804
+ {const at=ctx._fe??pos
8805
+ if(at>best){best=at;acc=undefined}
8806
+ if(at===best)acc=_accSet(ctx._fx,acc)}
8775
8807
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
8776
8808
  ${emitRollback(p, L.buf, sinks)}
8777
- }else acc=_accSet(${expected[i]},acc)`).join("\n");
8809
+ }else if(best===pos)acc=_accSet(${expected[i]},acc)`).join("\n");
8778
8810
  return `${head}
8779
8811
  const c=lead(input,pos)
8780
8812
  ${emitMark(p, L.buf, sinks)}
8781
8813
  let acc
8814
+ let best=pos
8782
8815
  ${maskable ? `if(c<128){
8783
8816
  const bits=${maskName}[c<0?128:c]
8784
8817
  let prev=0
8785
8818
  ${maskArms}
8786
- acc=${catchName}(${n},prev,acc)
8819
+ if(best===pos)acc=${catchName}(${n},prev,acc)
8787
8820
  ctx._fe=pos;ctx._fx=acc??${choiceFx}
8788
8821
  return FAIL
8789
8822
  }
@@ -10307,14 +10340,19 @@ function assemble(t2, prog, cfg) {
10307
10340
  };
10308
10341
  }
10309
10342
  function maskedChoiceBlock(start, a0, a1, a2, a3, through, next, total, choiceFx) {
10310
- return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
10343
+ return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
10311
10344
  if ((bits & 1 << start) !== 0) {
10312
10345
  ctx._fc = false;
10313
10346
  const v = a0(input, pos, ctx);
10314
10347
  if (v !== FAIL) return v;
10315
- acc = through(start, prev, acc);
10348
+ if (best === pos) acc = through(start, prev, acc);
10316
10349
  prev = start + 1;
10317
- acc = accSet(ctx._fx, acc);
10350
+ const at = ctx._fe ?? pos;
10351
+ if (at > best) {
10352
+ best = at;
10353
+ acc = void 0;
10354
+ }
10355
+ if (at === best) acc = accSet(ctx._fx, acc);
10318
10356
  if (committed3(ctx)) {
10319
10357
  if (acc !== void 0) ctx._fx = acc;
10320
10358
  return FAIL;
@@ -10325,9 +10363,14 @@ function assemble(t2, prog, cfg) {
10325
10363
  ctx._fc = false;
10326
10364
  const v = a1(input, pos, ctx);
10327
10365
  if (v !== FAIL) return v;
10328
- acc = through(start + 1, prev, acc);
10366
+ if (best === pos) acc = through(start + 1, prev, acc);
10329
10367
  prev = start + 2;
10330
- acc = accSet(ctx._fx, acc);
10368
+ const at = ctx._fe ?? pos;
10369
+ if (at > best) {
10370
+ best = at;
10371
+ acc = void 0;
10372
+ }
10373
+ if (at === best) acc = accSet(ctx._fx, acc);
10331
10374
  if (committed3(ctx)) {
10332
10375
  if (acc !== void 0) ctx._fx = acc;
10333
10376
  return FAIL;
@@ -10338,9 +10381,14 @@ function assemble(t2, prog, cfg) {
10338
10381
  ctx._fc = false;
10339
10382
  const v = a2(input, pos, ctx);
10340
10383
  if (v !== FAIL) return v;
10341
- acc = through(start + 2, prev, acc);
10384
+ if (best === pos) acc = through(start + 2, prev, acc);
10342
10385
  prev = start + 3;
10343
- acc = accSet(ctx._fx, acc);
10386
+ const at = ctx._fe ?? pos;
10387
+ if (at > best) {
10388
+ best = at;
10389
+ acc = void 0;
10390
+ }
10391
+ if (at === best) acc = accSet(ctx._fx, acc);
10344
10392
  if (committed3(ctx)) {
10345
10393
  if (acc !== void 0) ctx._fx = acc;
10346
10394
  return FAIL;
@@ -10351,9 +10399,14 @@ function assemble(t2, prog, cfg) {
10351
10399
  ctx._fc = false;
10352
10400
  const v = a3(input, pos, ctx);
10353
10401
  if (v !== FAIL) return v;
10354
- acc = through(start + 3, prev, acc);
10402
+ if (best === pos) acc = through(start + 3, prev, acc);
10355
10403
  prev = start + 4;
10356
- acc = accSet(ctx._fx, acc);
10404
+ const at = ctx._fe ?? pos;
10405
+ if (at > best) {
10406
+ best = at;
10407
+ acc = void 0;
10408
+ }
10409
+ if (at === best) acc = accSet(ctx._fx, acc);
10357
10410
  if (committed3(ctx)) {
10358
10411
  if (acc !== void 0) ctx._fx = acc;
10359
10412
  return FAIL;
@@ -10361,23 +10414,28 @@ function assemble(t2, prog, cfg) {
10361
10414
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
10362
10415
  }
10363
10416
  if (next !== void 0) {
10364
- return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev);
10417
+ return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best);
10365
10418
  }
10366
- acc = through(total, prev, acc);
10419
+ if (best === pos) acc = through(total, prev, acc);
10367
10420
  ctx._fe = pos;
10368
10421
  ctx._fx = acc ?? choiceFx;
10369
10422
  return FAIL;
10370
10423
  };
10371
10424
  }
10372
10425
  function generalChoiceBlock(start, a0, a1, a2, a3, g0, g1, g2, g3, through, next, total, choiceFx) {
10373
- return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
10426
+ return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
10374
10427
  if (g0 === null || classHas2(g0, c)) {
10375
10428
  ctx._fc = false;
10376
10429
  const v = a0(input, pos, ctx);
10377
10430
  if (v !== FAIL) return v;
10378
- acc = through(start, prev, acc);
10431
+ if (best === pos) acc = through(start, prev, acc);
10379
10432
  prev = start + 1;
10380
- acc = accSet(ctx._fx, acc);
10433
+ const at = ctx._fe ?? pos;
10434
+ if (at > best) {
10435
+ best = at;
10436
+ acc = void 0;
10437
+ }
10438
+ if (at === best) acc = accSet(ctx._fx, acc);
10381
10439
  if (committed3(ctx)) {
10382
10440
  if (acc !== void 0) ctx._fx = acc;
10383
10441
  return FAIL;
@@ -10388,9 +10446,14 @@ function assemble(t2, prog, cfg) {
10388
10446
  ctx._fc = false;
10389
10447
  const v = a1(input, pos, ctx);
10390
10448
  if (v !== FAIL) return v;
10391
- acc = through(start + 1, prev, acc);
10449
+ if (best === pos) acc = through(start + 1, prev, acc);
10392
10450
  prev = start + 2;
10393
- acc = accSet(ctx._fx, acc);
10451
+ const at = ctx._fe ?? pos;
10452
+ if (at > best) {
10453
+ best = at;
10454
+ acc = void 0;
10455
+ }
10456
+ if (at === best) acc = accSet(ctx._fx, acc);
10394
10457
  if (committed3(ctx)) {
10395
10458
  if (acc !== void 0) ctx._fx = acc;
10396
10459
  return FAIL;
@@ -10401,9 +10464,14 @@ function assemble(t2, prog, cfg) {
10401
10464
  ctx._fc = false;
10402
10465
  const v = a2(input, pos, ctx);
10403
10466
  if (v !== FAIL) return v;
10404
- acc = through(start + 2, prev, acc);
10467
+ if (best === pos) acc = through(start + 2, prev, acc);
10405
10468
  prev = start + 3;
10406
- acc = accSet(ctx._fx, acc);
10469
+ const at = ctx._fe ?? pos;
10470
+ if (at > best) {
10471
+ best = at;
10472
+ acc = void 0;
10473
+ }
10474
+ if (at === best) acc = accSet(ctx._fx, acc);
10407
10475
  if (committed3(ctx)) {
10408
10476
  if (acc !== void 0) ctx._fx = acc;
10409
10477
  return FAIL;
@@ -10414,9 +10482,14 @@ function assemble(t2, prog, cfg) {
10414
10482
  ctx._fc = false;
10415
10483
  const v = a3(input, pos, ctx);
10416
10484
  if (v !== FAIL) return v;
10417
- acc = through(start + 3, prev, acc);
10485
+ if (best === pos) acc = through(start + 3, prev, acc);
10418
10486
  prev = start + 4;
10419
- acc = accSet(ctx._fx, acc);
10487
+ const at = ctx._fe ?? pos;
10488
+ if (at > best) {
10489
+ best = at;
10490
+ acc = void 0;
10491
+ }
10492
+ if (at === best) acc = accSet(ctx._fx, acc);
10420
10493
  if (committed3(ctx)) {
10421
10494
  if (acc !== void 0) ctx._fx = acc;
10422
10495
  return FAIL;
@@ -10424,9 +10497,9 @@ function assemble(t2, prog, cfg) {
10424
10497
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
10425
10498
  }
10426
10499
  if (next !== void 0) {
10427
- return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev);
10500
+ return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best);
10428
10501
  }
10429
- acc = through(total, prev, acc);
10502
+ if (best === pos) acc = through(total, prev, acc);
10430
10503
  ctx._fe = pos;
10431
10504
  ctx._fx = acc ?? choiceFx;
10432
10505
  return FAIL;
@@ -11782,6 +11855,7 @@ function assemble(t2, prog, cfg) {
11782
11855
  const mLog = need ? ctx._triviaLog?.length ?? 0 : 0;
11783
11856
  const mRoot = need ? ctx._rootTriviaLog?.length ?? 0 : 0;
11784
11857
  let acc;
11858
+ let best = pos;
11785
11859
  if (c < 128) {
11786
11860
  const bits = mask[c < 0 ? 128 : c];
11787
11861
  let prev = 0;
@@ -11790,7 +11864,12 @@ function assemble(t2, prog, cfg) {
11790
11864
  const v = a0(input, pos, ctx);
11791
11865
  if (v !== FAIL) return v;
11792
11866
  prev = 1;
11793
- acc = accSet(ctx._fx, acc);
11867
+ const at = ctx._fe ?? pos;
11868
+ if (at > best) {
11869
+ best = at;
11870
+ acc = void 0;
11871
+ }
11872
+ if (at === best) acc = accSet(ctx._fx, acc);
11794
11873
  if (committed3(ctx)) {
11795
11874
  if (acc !== void 0) ctx._fx = acc;
11796
11875
  return FAIL;
@@ -11801,9 +11880,14 @@ function assemble(t2, prog, cfg) {
11801
11880
  ctx._fc = false;
11802
11881
  const v = a1(input, pos, ctx);
11803
11882
  if (v !== FAIL) return v;
11804
- if (prev < 1) acc = accSet(e0, acc);
11883
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
11805
11884
  prev = 2;
11806
- acc = accSet(ctx._fx, acc);
11885
+ const at = ctx._fe ?? pos;
11886
+ if (at > best) {
11887
+ best = at;
11888
+ acc = void 0;
11889
+ }
11890
+ if (at === best) acc = accSet(ctx._fx, acc);
11807
11891
  if (committed3(ctx)) {
11808
11892
  if (acc !== void 0) ctx._fx = acc;
11809
11893
  return FAIL;
@@ -11814,19 +11898,24 @@ function assemble(t2, prog, cfg) {
11814
11898
  ctx._fc = false;
11815
11899
  const v = a2(input, pos, ctx);
11816
11900
  if (v !== FAIL) return v;
11817
- if (prev < 1) acc = accSet(e0, acc);
11818
- if (prev < 2) acc = accSet(e1, acc);
11901
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
11902
+ if (best === pos && prev < 2) acc = accSet(e1, acc);
11819
11903
  prev = 3;
11820
- acc = accSet(ctx._fx, acc);
11904
+ const at = ctx._fe ?? pos;
11905
+ if (at > best) {
11906
+ best = at;
11907
+ acc = void 0;
11908
+ }
11909
+ if (at === best) acc = accSet(ctx._fx, acc);
11821
11910
  if (committed3(ctx)) {
11822
11911
  if (acc !== void 0) ctx._fx = acc;
11823
11912
  return FAIL;
11824
11913
  }
11825
11914
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11826
11915
  }
11827
- if (prev < 1) acc = accSet(e0, acc);
11828
- if (prev < 2) acc = accSet(e1, acc);
11829
- if (n === 3 && prev < 3) acc = accSet(e2, acc);
11916
+ if (best === pos && prev < 1) acc = accSet(e0, acc);
11917
+ if (best === pos && prev < 2) acc = accSet(e1, acc);
11918
+ if (best === pos && n === 3 && prev < 3) acc = accSet(e2, acc);
11830
11919
  ctx._fe = pos;
11831
11920
  ctx._fx = acc ?? choiceFx;
11832
11921
  return FAIL;
@@ -11835,36 +11924,51 @@ function assemble(t2, prog, cfg) {
11835
11924
  ctx._fc = false;
11836
11925
  const v = a0(input, pos, ctx);
11837
11926
  if (v !== FAIL) return v;
11838
- acc = accSet(ctx._fx, acc);
11927
+ const at = ctx._fe ?? pos;
11928
+ if (at > best) {
11929
+ best = at;
11930
+ acc = void 0;
11931
+ }
11932
+ if (at === best) acc = accSet(ctx._fx, acc);
11839
11933
  if (committed3(ctx)) {
11840
11934
  if (acc !== void 0) ctx._fx = acc;
11841
11935
  return FAIL;
11842
11936
  }
11843
11937
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11844
- } else acc = accSet(e0, acc);
11938
+ } else if (best === pos) acc = accSet(e0, acc);
11845
11939
  if (g1 === null || classHas2(g1, c)) {
11846
11940
  ctx._fc = false;
11847
11941
  const v = a1(input, pos, ctx);
11848
11942
  if (v !== FAIL) return v;
11849
- acc = accSet(ctx._fx, acc);
11943
+ const at = ctx._fe ?? pos;
11944
+ if (at > best) {
11945
+ best = at;
11946
+ acc = void 0;
11947
+ }
11948
+ if (at === best) acc = accSet(ctx._fx, acc);
11850
11949
  if (committed3(ctx)) {
11851
11950
  if (acc !== void 0) ctx._fx = acc;
11852
11951
  return FAIL;
11853
11952
  }
11854
11953
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11855
- } else acc = accSet(e1, acc);
11954
+ } else if (best === pos) acc = accSet(e1, acc);
11856
11955
  if (n === 3) {
11857
11956
  if (g2 === null || classHas2(g2, c)) {
11858
11957
  ctx._fc = false;
11859
11958
  const v = a2(input, pos, ctx);
11860
11959
  if (v !== FAIL) return v;
11861
- acc = accSet(ctx._fx, acc);
11960
+ const at = ctx._fe ?? pos;
11961
+ if (at > best) {
11962
+ best = at;
11963
+ acc = void 0;
11964
+ }
11965
+ if (at === best) acc = accSet(ctx._fx, acc);
11862
11966
  if (committed3(ctx)) {
11863
11967
  if (acc !== void 0) ctx._fx = acc;
11864
11968
  return FAIL;
11865
11969
  }
11866
11970
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot);
11867
- } else acc = accSet(e2, acc);
11971
+ } else if (best === pos) acc = accSet(e2, acc);
11868
11972
  }
11869
11973
  ctx._fe = pos;
11870
11974
  ctx._fx = acc ?? choiceFx;
@@ -11962,7 +12066,8 @@ function assemble(t2, prog, cfg) {
11962
12066
  mLog,
11963
12067
  mRoot,
11964
12068
  void 0,
11965
- 0
12069
+ 0,
12070
+ pos
11966
12071
  );
11967
12072
  }
11968
12073
  return general(
@@ -11979,7 +12084,8 @@ function assemble(t2, prog, cfg) {
11979
12084
  mLog,
11980
12085
  mRoot,
11981
12086
  void 0,
11982
- 0
12087
+ 0,
12088
+ pos
11983
12089
  );
11984
12090
  };
11985
12091
  }
@@ -12007,7 +12113,8 @@ function assemble(t2, prog, cfg) {
12007
12113
  mLog,
12008
12114
  mRoot,
12009
12115
  void 0,
12010
- 0
12116
+ 0,
12117
+ pos
12011
12118
  );
12012
12119
  };
12013
12120
  }