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.
@@ -1 +1 @@
1
- {"version":3,"file":"lex-program.d.ts","sourceRoot":"","sources":["../../src/table/lex-program.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjE;;0EAE0E;AAC1E,MAAM,MAAM,gBAAgB,GAAG;IAC7B,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,MAAM,CAAA;IACnF,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,eAAO,MAAM,iBAAiB,IAAI,CAAA;AAClC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,YAAY,IAAI,CAAA;AAC7B,eAAO,MAAM,iBAAiB,IAAI,CAAA;AAElC,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAOjE;AAsQD;2EAC2E;AAC3E,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,cAAc,EACpB,QAAQ,EAAE,SAAS,iBAAiB,EAAE,EACtC,OAAO,EAAE,SAAS,aAAa,EAAE,EACjC,QAAQ,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,GACvC,gBAAgB,CAyHlB"}
1
+ {"version":3,"file":"lex-program.d.ts","sourceRoot":"","sources":["../../src/table/lex-program.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjE;;0EAE0E;AAC1E,MAAM,MAAM,gBAAgB,GAAG;IAC7B,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,MAAM,CAAA;IACnF,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,eAAO,MAAM,iBAAiB,IAAI,CAAA;AAClC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,kBAAkB,IAAI,CAAA;AACnC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,gBAAgB,IAAI,CAAA;AACjC,eAAO,MAAM,YAAY,IAAI,CAAA;AAC7B,eAAO,MAAM,iBAAiB,IAAI,CAAA;AAElC,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAOjE;AA+QD;2EAC2E;AAC3E,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,cAAc,EACpB,QAAQ,EAAE,SAAS,iBAAiB,EAAE,EACtC,OAAO,EAAE,SAAS,aAAa,EAAE,EACjC,QAAQ,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,GACvC,gBAAgB,CAyHlB"}
@@ -29,10 +29,10 @@ export declare const OP_SEQV = 4;
29
29
  * `CHOICE d n fx c1 … cn` — `d` indexes a dispatch table (or −1 for ordered
30
30
  * try); `fx` indexes the choice's OWN expected set.
31
31
  *
32
- * Both shipped engines report the UNION of the arms' expectations when a choice
33
- * fails. The driver reported whatever the last attempted arm left behind — or,
34
- * on a dispatch miss with no arm claiming the lead character, NOTHING, so a user
35
- * got an error naming nothing at all.
32
+ * Ordered failure keeps the expected sets at the deepest arm offset, merging
33
+ * exact ties in source order. Exclusive dispatch propagates its sole selected
34
+ * arm. A pure dispatch miss uses `fx`, the static union of every arm opener, so
35
+ * the result never names nothing merely because no arm claimed the lead.
36
36
  */
37
37
  export declare const OP_CHOICE = 5;
38
38
  /**
package/dist/version.d.ts CHANGED
@@ -13,5 +13,5 @@
13
13
  * Kept in sync with `package.json` by `test/unit/version-sync.test.ts`, which fails
14
14
  * the build if this constant drifts from the published version.
15
15
  */
16
- export declare const PARSEMAN_VERSION = "0.48.0";
16
+ export declare const PARSEMAN_VERSION = "0.48.1";
17
17
  //# sourceMappingURL=version.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "parseman",
3
- "version": "0.48.0",
3
+ "version": "0.48.1",
4
4
  "packageManager": "pnpm@10.15.1",
5
5
  "engines": {
6
6
  "node": "^20.19.0 || >=22.13.0"
@@ -85,6 +85,7 @@ export function choice<T extends [Combinator<unknown> | GatedArm<unknown>, ...(C
85
85
  },
86
86
  parse(input: string, pos: number, ctx: ParseContext): ParseResult<UnionArms<T>> {
87
87
  const expected: string[] = []
88
+ let expectedAt = pos
88
89
 
89
90
  // ── Disjoint: O(1) first-char dispatch (arms may be gated) ────────────
90
91
  //
@@ -164,7 +165,9 @@ export function choice<T extends [Combinator<unknown> | GatedArm<unknown>, ...(C
164
165
  const result = parsers[i]!.parse(input, pos, ctx)
165
166
  if (!result.ok) {
166
167
  rollbackTrivia(ctx, mark)
167
- expected.push(...result.expected)
168
+ const at = result.span.start
169
+ if (at > expectedAt) { expectedAt = at; expected.length = 0 }
170
+ if (at === expectedAt) expected.push(...result.expected)
168
171
  if (result.committed) return { ok: false, expected, span: result.span, committed: true }
169
172
  continue
170
173
  }
@@ -175,6 +178,12 @@ export function choice<T extends [Combinator<unknown> | GatedArm<unknown>, ...(C
175
178
  }
176
179
  return result as ParseResult<UnionArms<T>>
177
180
  }
181
+ // A nested gated choice can decline every arm after an enclosing arm has
182
+ // already advanced. Do not let that empty dynamic set erase both the
183
+ // shallower arms and the choice's own static opener contract.
184
+ if (expected.length === 0) {
185
+ for (const parser of parsers) expected.push(...deriveExpected(parser))
186
+ }
178
187
  return { ok: false, expected, span: { start: pos, end: pos } }
179
188
  },
180
189
  }
@@ -224,13 +224,13 @@ type MaskedChoiceBlock = (
224
224
  input: string, pos: number, ctx: ParseContext, bits: number,
225
225
  need: boolean, mRaw: number, mTl: number, mLv: number, mFl: number,
226
226
  mEr: number, mLog: number, mRoot: number,
227
- acc: string[] | undefined, prev: number,
227
+ acc: string[] | undefined, prev: number, best: number,
228
228
  ) => unknown
229
229
  type GeneralChoiceBlock = (
230
230
  input: string, pos: number, ctx: ParseContext, c: number,
231
231
  need: boolean, mRaw: number, mTl: number, mLv: number, mFl: number,
232
232
  mEr: number, mLog: number, mRoot: number,
233
- acc: string[] | undefined, prev: number,
233
+ acc: string[] | undefined, prev: number, best: number,
234
234
  ) => unknown
235
235
  type ExclusiveChoiceBlock = (arm: number, input: string, pos: number, ctx: ParseContext) => unknown
236
236
  type DispatchMatcher = (key: string) => boolean
@@ -1008,19 +1008,7 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1008
1008
  return c._fc === true
1009
1009
  }
1010
1010
 
1011
- /**
1012
- * Accumulate one failed choice arm's expected set, in arm order.
1013
- *
1014
- * `choice.ts:167` is `expected.push(...result.expected)` per failed arm, and the
1015
- * accumulation — not the static union of the arms' OPENERS — is what a failing
1016
- * ordered choice reports. A left-factored or shared-prefix choice whose arms all
1017
- * failed PAST their common opener reported that opener alone: `['/::?/']` where
1018
- * the interpreter says `['"before"', '"hover"']`.
1019
- *
1020
- * Returns the accumulator so the common one-failing-arm case borrows the arm's
1021
- * own array (`slice` once) instead of building one per choice execution the way
1022
- * the interpreter's eager `const expected: string[] = []` does.
1023
- */
1011
+ /** Append an exact-depth failed arm set in source order. Callers own depth ranking. */
1024
1012
  function accSet(ax: readonly string[] | undefined, acc: string[] | undefined): string[] | undefined {
1025
1013
  if (ax === undefined || ax.length === 0) return acc
1026
1014
  if (acc === undefined) return ax.slice()
@@ -1062,13 +1050,16 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1062
1050
  total: number,
1063
1051
  choiceFx: string[],
1064
1052
  ): MaskedChoiceBlock {
1065
- return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
1053
+ return (input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
1066
1054
  if ((bits & (1 << start)) !== 0) {
1067
1055
  ctx._fc = false
1068
1056
  const v = a0(input, pos, ctx)
1069
1057
  if (v !== FAIL) return v
1070
- acc = through(start, prev, acc); prev = start + 1
1071
- acc = accSet(ctx._fx, acc)
1058
+ if (best === pos) acc = through(start, prev, acc)
1059
+ prev = start + 1
1060
+ const at = ctx._fe ?? pos
1061
+ if (at > best) { best = at; acc = undefined }
1062
+ if (at === best) acc = accSet(ctx._fx, acc)
1072
1063
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1073
1064
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1074
1065
  }
@@ -1076,8 +1067,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1076
1067
  ctx._fc = false
1077
1068
  const v = a1(input, pos, ctx)
1078
1069
  if (v !== FAIL) return v
1079
- acc = through(start + 1, prev, acc); prev = start + 2
1080
- acc = accSet(ctx._fx, acc)
1070
+ if (best === pos) acc = through(start + 1, prev, acc)
1071
+ prev = start + 2
1072
+ const at = ctx._fe ?? pos
1073
+ if (at > best) { best = at; acc = undefined }
1074
+ if (at === best) acc = accSet(ctx._fx, acc)
1081
1075
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1082
1076
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1083
1077
  }
@@ -1085,8 +1079,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1085
1079
  ctx._fc = false
1086
1080
  const v = a2(input, pos, ctx)
1087
1081
  if (v !== FAIL) return v
1088
- acc = through(start + 2, prev, acc); prev = start + 3
1089
- acc = accSet(ctx._fx, acc)
1082
+ if (best === pos) acc = through(start + 2, prev, acc)
1083
+ prev = start + 3
1084
+ const at = ctx._fe ?? pos
1085
+ if (at > best) { best = at; acc = undefined }
1086
+ if (at === best) acc = accSet(ctx._fx, acc)
1090
1087
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1091
1088
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1092
1089
  }
@@ -1094,15 +1091,18 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1094
1091
  ctx._fc = false
1095
1092
  const v = a3(input, pos, ctx)
1096
1093
  if (v !== FAIL) return v
1097
- acc = through(start + 3, prev, acc); prev = start + 4
1098
- acc = accSet(ctx._fx, acc)
1094
+ if (best === pos) acc = through(start + 3, prev, acc)
1095
+ prev = start + 4
1096
+ const at = ctx._fe ?? pos
1097
+ if (at > best) { best = at; acc = undefined }
1098
+ if (at === best) acc = accSet(ctx._fx, acc)
1099
1099
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1100
1100
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1101
1101
  }
1102
1102
  if (next !== undefined) {
1103
- return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev)
1103
+ return next(input, pos, ctx, bits, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best)
1104
1104
  }
1105
- acc = through(total, prev, acc)
1105
+ if (best === pos) acc = through(total, prev, acc)
1106
1106
  ctx._fe = pos; ctx._fx = acc ?? choiceFx
1107
1107
  return FAIL
1108
1108
  }
@@ -1119,13 +1119,16 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1119
1119
  total: number,
1120
1120
  choiceFx: string[],
1121
1121
  ): GeneralChoiceBlock {
1122
- return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev) => {
1122
+ return (input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best) => {
1123
1123
  if (g0 === null || classHas(g0, c)) {
1124
1124
  ctx._fc = false
1125
1125
  const v = a0(input, pos, ctx)
1126
1126
  if (v !== FAIL) return v
1127
- acc = through(start, prev, acc); prev = start + 1
1128
- acc = accSet(ctx._fx, acc)
1127
+ if (best === pos) acc = through(start, prev, acc)
1128
+ prev = start + 1
1129
+ const at = ctx._fe ?? pos
1130
+ if (at > best) { best = at; acc = undefined }
1131
+ if (at === best) acc = accSet(ctx._fx, acc)
1129
1132
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1130
1133
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1131
1134
  }
@@ -1133,8 +1136,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1133
1136
  ctx._fc = false
1134
1137
  const v = a1(input, pos, ctx)
1135
1138
  if (v !== FAIL) return v
1136
- acc = through(start + 1, prev, acc); prev = start + 2
1137
- acc = accSet(ctx._fx, acc)
1139
+ if (best === pos) acc = through(start + 1, prev, acc)
1140
+ prev = start + 2
1141
+ const at = ctx._fe ?? pos
1142
+ if (at > best) { best = at; acc = undefined }
1143
+ if (at === best) acc = accSet(ctx._fx, acc)
1138
1144
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1139
1145
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1140
1146
  }
@@ -1142,8 +1148,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1142
1148
  ctx._fc = false
1143
1149
  const v = a2(input, pos, ctx)
1144
1150
  if (v !== FAIL) return v
1145
- acc = through(start + 2, prev, acc); prev = start + 3
1146
- acc = accSet(ctx._fx, acc)
1151
+ if (best === pos) acc = through(start + 2, prev, acc)
1152
+ prev = start + 3
1153
+ const at = ctx._fe ?? pos
1154
+ if (at > best) { best = at; acc = undefined }
1155
+ if (at === best) acc = accSet(ctx._fx, acc)
1147
1156
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1148
1157
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1149
1158
  }
@@ -1151,15 +1160,18 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
1151
1160
  ctx._fc = false
1152
1161
  const v = a3(input, pos, ctx)
1153
1162
  if (v !== FAIL) return v
1154
- acc = through(start + 3, prev, acc); prev = start + 4
1155
- acc = accSet(ctx._fx, acc)
1163
+ if (best === pos) acc = through(start + 3, prev, acc)
1164
+ prev = start + 4
1165
+ const at = ctx._fe ?? pos
1166
+ if (at > best) { best = at; acc = undefined }
1167
+ if (at === best) acc = accSet(ctx._fx, acc)
1156
1168
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
1157
1169
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
1158
1170
  }
1159
1171
  if (next !== undefined) {
1160
- return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev)
1172
+ return next(input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, acc, prev, best)
1161
1173
  }
1162
- acc = through(total, prev, acc)
1174
+ if (best === pos) acc = through(total, prev, acc)
1163
1175
  ctx._fe = pos; ctx._fx = acc ?? choiceFx
1164
1176
  return FAIL
1165
1177
  }
@@ -2690,10 +2702,8 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2690
2702
  // THE CUT — a committed failure fails the whole choice.
2691
2703
  if (committed(ctx)) return FAIL
2692
2704
  // THE DISPATCHED ARM'S OWN SET IS THE ANSWER, at the choice's own
2693
- // position — the rule both shipped engines apply (choice.ts:105,
2694
- // codegen.ts:1985). `_fx` already holds it; reporting the arm is
2695
- // declining to overwrite it. See `exec.ts` for why this is NOT
2696
- // furthest-failure merging and what the residual JSON gap is.
2705
+ // position. `_fx` already holds it; reporting the sole selected arm
2706
+ // is declining to overwrite it.
2697
2707
  const armFx = ctx._fx
2698
2708
  if (armFx !== undefined && armFx.length > 0) { ctx._fe = pos; return FAIL }
2699
2709
  }
@@ -2739,6 +2749,7 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2739
2749
  const mLog = need ? ctx._triviaLog?.length ?? 0 : 0
2740
2750
  const mRoot = need ? ctx._rootTriviaLog?.length ?? 0 : 0
2741
2751
  let acc: string[] | undefined
2752
+ let best = pos
2742
2753
  if (c < 128) {
2743
2754
  const bits = mask[c < 0 ? 128 : c]!
2744
2755
  let prev = 0
@@ -2747,7 +2758,9 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2747
2758
  const v = a0(input, pos, ctx)
2748
2759
  if (v !== FAIL) return v
2749
2760
  prev = 1
2750
- acc = accSet(ctx._fx, acc)
2761
+ const at = ctx._fe ?? pos
2762
+ if (at > best) { best = at; acc = undefined }
2763
+ if (at === best) acc = accSet(ctx._fx, acc)
2751
2764
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2752
2765
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2753
2766
  }
@@ -2755,9 +2768,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2755
2768
  ctx._fc = false
2756
2769
  const v = a1(input, pos, ctx)
2757
2770
  if (v !== FAIL) return v
2758
- if (prev < 1) acc = accSet(e0, acc)
2771
+ if (best === pos && prev < 1) acc = accSet(e0, acc)
2759
2772
  prev = 2
2760
- acc = accSet(ctx._fx, acc)
2773
+ const at = ctx._fe ?? pos
2774
+ if (at > best) { best = at; acc = undefined }
2775
+ if (at === best) acc = accSet(ctx._fx, acc)
2761
2776
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2762
2777
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2763
2778
  }
@@ -2765,16 +2780,18 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2765
2780
  ctx._fc = false
2766
2781
  const v = a2!(input, pos, ctx)
2767
2782
  if (v !== FAIL) return v
2768
- if (prev < 1) acc = accSet(e0, acc)
2769
- if (prev < 2) acc = accSet(e1, acc)
2783
+ if (best === pos && prev < 1) acc = accSet(e0, acc)
2784
+ if (best === pos && prev < 2) acc = accSet(e1, acc)
2770
2785
  prev = 3
2771
- acc = accSet(ctx._fx, acc)
2786
+ const at = ctx._fe ?? pos
2787
+ if (at > best) { best = at; acc = undefined }
2788
+ if (at === best) acc = accSet(ctx._fx, acc)
2772
2789
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2773
2790
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2774
2791
  }
2775
- if (prev < 1) acc = accSet(e0, acc)
2776
- if (prev < 2) acc = accSet(e1, acc)
2777
- if (n === 3 && prev < 3) acc = accSet(e2, acc)
2792
+ if (best === pos && prev < 1) acc = accSet(e0, acc)
2793
+ if (best === pos && prev < 2) acc = accSet(e1, acc)
2794
+ if (best === pos && n === 3 && prev < 3) acc = accSet(e2, acc)
2778
2795
  ctx._fe = pos; ctx._fx = acc ?? choiceFx
2779
2796
  return FAIL
2780
2797
  }
@@ -2783,27 +2800,33 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2783
2800
  ctx._fc = false
2784
2801
  const v = a0(input, pos, ctx)
2785
2802
  if (v !== FAIL) return v
2786
- acc = accSet(ctx._fx, acc)
2803
+ const at = ctx._fe ?? pos
2804
+ if (at > best) { best = at; acc = undefined }
2805
+ if (at === best) acc = accSet(ctx._fx, acc)
2787
2806
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2788
2807
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2789
- } else acc = accSet(e0, acc)
2808
+ } else if (best === pos) acc = accSet(e0, acc)
2790
2809
  if (g1 === null || classHas(g1, c)) {
2791
2810
  ctx._fc = false
2792
2811
  const v = a1(input, pos, ctx)
2793
2812
  if (v !== FAIL) return v
2794
- acc = accSet(ctx._fx, acc)
2813
+ const at = ctx._fe ?? pos
2814
+ if (at > best) { best = at; acc = undefined }
2815
+ if (at === best) acc = accSet(ctx._fx, acc)
2795
2816
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2796
2817
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2797
- } else acc = accSet(e1, acc)
2818
+ } else if (best === pos) acc = accSet(e1, acc)
2798
2819
  if (n === 3) {
2799
2820
  if (g2 === null || classHas(g2, c)) {
2800
2821
  ctx._fc = false
2801
2822
  const v = a2!(input, pos, ctx)
2802
2823
  if (v !== FAIL) return v
2803
- acc = accSet(ctx._fx, acc)
2824
+ const at = ctx._fe ?? pos
2825
+ if (at > best) { best = at; acc = undefined }
2826
+ if (at === best) acc = accSet(ctx._fx, acc)
2804
2827
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
2805
2828
  if (need) rollbackTriviaAt(ctx, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot)
2806
- } else acc = accSet(e2, acc)
2829
+ } else if (best === pos) acc = accSet(e2, acc)
2807
2830
  }
2808
2831
  ctx._fe = pos; ctx._fx = acc ?? choiceFx
2809
2832
  return FAIL
@@ -2869,11 +2892,11 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2869
2892
  if (c < 128) {
2870
2893
  return masked(
2871
2894
  input, pos, ctx, mask[c < 0 ? 128 : c]!,
2872
- need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0,
2895
+ need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0, pos,
2873
2896
  )
2874
2897
  }
2875
2898
  return general(
2876
- input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0,
2899
+ input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0, pos,
2877
2900
  )
2878
2901
  }
2879
2902
  }
@@ -2889,7 +2912,7 @@ export function assemble(t: ResolvedTable, prog: TableProgram, cfg: RunCfg): Ass
2889
2912
  const mLog = need ? ctx._triviaLog?.length ?? 0 : 0
2890
2913
  const mRoot = need ? ctx._rootTriviaLog?.length ?? 0 : 0
2891
2914
  return general(
2892
- input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0,
2915
+ input, pos, ctx, c, need, mRaw, mTl, mLv, mFl, mEr, mLog, mRoot, undefined, 0, pos,
2893
2916
  )
2894
2917
  }
2895
2918
  }
@@ -1463,9 +1463,11 @@ return FAIL
1463
1463
  ctx._fc=false
1464
1464
  {const v=${arm}(input,pos,ctx)
1465
1465
  if(v!==FAIL)return v}
1466
- acc=${catchName}(${i},prev,acc)
1466
+ if(best===pos)acc=${catchName}(${i},prev,acc)
1467
1467
  prev=${i + 1}
1468
- acc=_accSet(ctx._fx,acc)
1468
+ {const at=ctx._fe??pos
1469
+ if(at>best){best=at;acc=undefined}
1470
+ if(at===best)acc=_accSet(ctx._fx,acc)}
1469
1471
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
1470
1472
  ${emitRollback(p, L.buf, sinks)}
1471
1473
  }`
@@ -1474,20 +1476,23 @@ ${emitRollback(p, L.buf, sinks)}
1474
1476
  ctx._fc=false
1475
1477
  {const v=${arm}(input,pos,ctx)
1476
1478
  if(v!==FAIL)return v}
1477
- acc=_accSet(ctx._fx,acc)
1479
+ {const at=ctx._fe??pos
1480
+ if(at>best){best=at;acc=undefined}
1481
+ if(at===best)acc=_accSet(ctx._fx,acc)}
1478
1482
  if(ctx._fc===true){if(acc!==undefined)ctx._fx=acc;return FAIL}
1479
1483
  ${emitRollback(p, L.buf, sinks)}
1480
- }else acc=_accSet(${expected[i]},acc)`).join('\n')
1484
+ }else if(best===pos)acc=_accSet(${expected[i]},acc)`).join('\n')
1481
1485
 
1482
1486
  return `${head}
1483
1487
  const c=lead(input,pos)
1484
1488
  ${emitMark(p, L.buf, sinks)}
1485
1489
  let acc
1490
+ let best=pos
1486
1491
  ${maskable ? `if(c<128){
1487
1492
  const bits=${maskName}[c<0?128:c]
1488
1493
  let prev=0
1489
1494
  ${maskArms}
1490
- acc=${catchName}(${n},prev,acc)
1495
+ if(best===pos)acc=${catchName}(${n},prev,acc)
1491
1496
  ctx._fe=pos;ctx._fx=acc??${choiceFx}
1492
1497
  return FAIL
1493
1498
  }
@@ -1078,20 +1078,16 @@ class Encoder {
1078
1078
  ? -1 : this.charClass(finalChoice.firstSets[i]!))
1079
1079
  const dispIdx = this.disp.length
1080
1080
  this.disp.push(classes)
1081
- // PER-ARM EXPECTED SETS RIDE ALONG, after the arm offsets. The ordered
1082
- // path reports the CONCATENATION of the arms' sets (`choice.ts:167`), and
1083
- // it must include the arms the driver's char gate declined to enter — the
1084
- // interpreter has no such gate, runs them, and gets exactly their static
1085
- // opener set back. Without these words a partially-gated site reported
1086
- // only the arms that happened to run.
1081
+ // PER-ARM EXPECTED SETS RIDE ALONG, after the arm offsets. While the best
1082
+ // failure remains at the choice position, an arm declined by the driver's
1083
+ // char gate contributes the static opener set the interpreter would get by
1084
+ // entering it. A deeper dynamic arm failure discards those shallower sets.
1087
1085
  const head = this.emitHead(OP_CHOICE, 3 + 2 * kids.length)
1088
1086
  this.code[head + 1] = dispIdx
1089
1087
  this.code[head + 2] = kids.length
1090
1088
  // THE CHOICE'S OWN SET IS THE ARMS' FLATMAP, NOT `deriveExpected(p)`.
1091
- // `deriveExpected` DEDUPES, and neither engine does: `choice.ts:110-118`
1092
- // is `parsers.flatMap(p => p.parse(...).expected)`, so a shared-prefix
1093
- // choice whose two arms both open with `/::?/` reports it TWICE. The
1094
- // deduped union silently collapsed that to one.
1089
+ // It is the pure-dispatch-miss and empty-dynamic fallback, where source
1090
+ // order and duplicates remain observable. `deriveExpected` would dedupe.
1095
1091
  this.code[head + 3] = this.expected(arms.flatMap(a => deriveExpected(a)))
1096
1092
  for (let i = 0; i < kids.length; i++) {
1097
1093
  this.code[head + 4 + i] = kids[i]!
package/src/table/exec.ts CHANGED
@@ -952,9 +952,8 @@ function makeDriver(
952
952
  const d = code[ip + 1]!
953
953
  const base = ip + 4
954
954
  const choiceFx = fx[code[ip + 3]!] as string[]
955
- // Report the union, at the choice's own position, on every exit that
956
- // fails — matching both engines and, more importantly, never handing a
957
- // user an empty expected set.
955
+ // A pure dispatch miss has no attempted arm to rank. Report the static
956
+ // opener union at the choice's own position so the result is meaningful.
958
957
  const failChoice = (): typeof FAIL => {
959
958
  ctx._fe = pos
960
959
  ctx._fx = choiceFx
@@ -1003,12 +1002,9 @@ function makeDriver(
1003
1002
  // union IS that flatMap's answer, without running seven parsers to
1004
1003
  // rediscover it.
1005
1004
  //
1006
- // NOT furthest-failure merging, which no engine on the `expected` path
1007
- // performs. `failAt` (combinators/probe.ts:13-24) and its compiled mirror
1008
- // `probeUpdate` (codegen.ts:692) are the only positional merges in the
1009
- // library, they are gated on `_ctx._probe` and they surface as
1010
- // `RunResult.furthestFail` under `{recover:true}` — a different field.
1011
- // Every `_fe`/`_fx` site in all three drivers is last-write-wins.
1005
+ // There is only one selected arm on this path, so ordered-arm depth
1006
+ // ranking does not apply. `furthestFail` under `{recover:true}` remains
1007
+ // a separate probe result.
1012
1008
  //
1013
1009
  // A residual JSON divergence survives this and is NOT a failure-reporting
1014
1010
  // bug: on `{"a":]` this driver reports `["}"]` (the Obj arm's own
@@ -1034,18 +1030,15 @@ function makeDriver(
1034
1030
  const mEr = need ? ctx._errors?.length ?? 0 : 0
1035
1031
  const mLog = need ? ctx._triviaLog?.length ?? 0 : 0
1036
1032
  const mRoot = need ? ctx._rootTriviaLog?.length ?? 0 : 0
1037
- // THE ORDERED PATH REPORTS THE ARMS THAT ACTUALLY RAN, accumulated in
1038
- // order — `choice.ts:167`'s `expected.push(...result.expected)` per failed
1039
- // arm, reported at the choice's own position. The static `choiceFx` union
1040
- // is a set of OPENERS, so a left-factored or shared-prefix choice whose
1041
- // arms all failed PAST their common opener reported that opener instead of
1042
- // the tokens the arms were actually waiting for: `['/::?/']` where the
1043
- // interpreter says `['"before"', '"hover"']`.
1033
+ // The ordered path keeps only arms at the deepest failure offset. Exact
1034
+ // ties merge in source order. The final failure is still anchored at the
1035
+ // choice's own position; the deeper offset ranks diagnostics only.
1044
1036
  //
1045
1037
  // `choiceFx` is still the answer when nothing ran or every arm reported
1046
1038
  // nothing — the char gate can skip arms the interpreter would have entered,
1047
1039
  // and the union is what those arms' start-failures would have contributed.
1048
1040
  let acc: string[] | undefined
1041
+ let best = pos
1049
1042
  const push = (ax: readonly string[] | undefined): void => {
1050
1043
  if (ax === undefined || ax.length === 0) return
1051
1044
  if (acc === undefined) acc = ax.slice()
@@ -1060,7 +1053,7 @@ function makeDriver(
1060
1053
  // enters the arm, the arm's opener fails, and its static set is what
1061
1054
  // lands in the accumulation. Declining to enter must not also decline
1062
1055
  // to say what the arm wanted.
1063
- push(fx[code[armFx + i]!] as string[])
1056
+ if (best === pos) push(fx[code[armFx + i]!] as string[])
1064
1057
  continue
1065
1058
  }
1066
1059
  ctx._fc = false
@@ -1069,7 +1062,9 @@ function makeDriver(
1069
1062
  const v = exec(code[base + i]!, input, pos, ctx)
1070
1063
  if (v !== FAIL) return v
1071
1064
  if (COUNT) { tableCounters.ungatedFails++; tableCounters.ungatedFailRows += tableCounters.rows - rows0 }
1072
- push(ctx._fx)
1065
+ const at = ctx._fe ?? pos
1066
+ if (at > best) { best = at; acc = undefined }
1067
+ if (at === best) push(ctx._fx)
1073
1068
  // A committed arm cuts, and the interpreter reports the accumulation at
1074
1069
  // the arm's OWN span (`choice.ts:170`), not at the choice's position.
1075
1070
  if (committed(ctx)) { if (acc !== undefined) ctx._fx = acc; return FAIL }
@@ -150,7 +150,8 @@ function sequence5(
150
150
  }
151
151
 
152
152
  type ChoiceTail = (
153
- input: string, pos: number, ctx: ParseContext, lead: number, acc: string[] | undefined,
153
+ input: string, pos: number, ctx: ParseContext, lead: number,
154
+ acc: string[] | undefined, best: number,
154
155
  ) => number
155
156
 
156
157
  function appendExpected(acc: string[] | undefined, values: readonly string[] | undefined): string[] | undefined {
@@ -181,24 +182,32 @@ function choiceNode(
181
182
  }
182
183
  for (let i = arms.length - 1; i >= 0; i--) {
183
184
  const arm = arms[i]!, cls = classes[i], staticExpected = armExpected[i]!, next = tail
184
- tail = (input, pos, ctx, lead, acc) => {
185
+ tail = (input, pos, ctx, lead, acc, best) => {
185
186
  if (cls !== undefined && !classHas(cls, lead)) {
186
- return next(input, pos, ctx, lead, appendExpected(acc, staticExpected))
187
+ return next(
188
+ input, pos, ctx, lead,
189
+ best === pos ? appendExpected(acc, staticExpected) : acc,
190
+ best,
191
+ )
187
192
  }
188
193
  ctx._fc = false
189
194
  const end = arm.run(input, pos, ctx)
190
195
  if (end >= 0) return end
191
- const nextAcc = appendExpected(acc, ctx._fx)
196
+ const at = ctx._fe ?? pos
197
+ if (at > best) { best = at; acc = undefined }
198
+ const nextAcc = at === best ? appendExpected(acc, ctx._fx) : acc
192
199
  if (committed(ctx)) {
193
200
  if (nextAcc !== undefined) ctx._fx = nextAcc
194
201
  return -1
195
202
  }
196
- return next(input, pos, ctx, lead, nextAcc)
203
+ return next(input, pos, ctx, lead, nextAcc, best)
197
204
  }
198
205
  }
199
206
  return {
200
207
  match: matchTail,
201
- run: (input, pos, ctx) => tail(input, pos, ctx, pos < input.length ? input.codePointAt(pos)! : -1, undefined),
208
+ run: (input, pos, ctx) => tail(
209
+ input, pos, ctx, pos < input.length ? input.codePointAt(pos)! : -1, undefined, pos,
210
+ ),
202
211
  }
203
212
  }
204
213
 
package/src/table/ops.ts CHANGED
@@ -30,10 +30,10 @@ export const OP_SEQV = 4
30
30
  * `CHOICE d n fx c1 … cn` — `d` indexes a dispatch table (or −1 for ordered
31
31
  * try); `fx` indexes the choice's OWN expected set.
32
32
  *
33
- * Both shipped engines report the UNION of the arms' expectations when a choice
34
- * fails. The driver reported whatever the last attempted arm left behind — or,
35
- * on a dispatch miss with no arm claiming the lead character, NOTHING, so a user
36
- * got an error naming nothing at all.
33
+ * Ordered failure keeps the expected sets at the deepest arm offset, merging
34
+ * exact ties in source order. Exclusive dispatch propagates its sole selected
35
+ * arm. A pure dispatch miss uses `fx`, the static union of every arm opener, so
36
+ * the result never names nothing merely because no arm claimed the lead.
37
37
  */
38
38
  export const OP_CHOICE = 5
39
39
  /**
package/src/version.ts CHANGED
@@ -13,4 +13,4 @@
13
13
  * Kept in sync with `package.json` by `test/unit/version-sync.test.ts`, which fails
14
14
  * the build if this constant drifts from the published version.
15
15
  */
16
- export const PARSEMAN_VERSION = '0.48.0'
16
+ export const PARSEMAN_VERSION = '0.48.1'