parseman 0.20.0 → 0.21.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.
@@ -1219,13 +1219,25 @@ function tryFastLabeledScan(input, cur, trivia2) {
1219
1219
  }
1220
1220
  function recordTriviaChunks(ctx, chunks) {
1221
1221
  const kinds = ctx.triviaKindLabels;
1222
+ const mask = ctx._triviaCaptureMask;
1222
1223
  for (const ch of chunks) {
1223
1224
  pushTriviaLogEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1224
1225
  if (ctx.captureTrivia && (ctx._cstBuf !== void 0 || ctx._cstTriviaLog !== void 0)) {
1225
- pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1226
+ if (mask === void 0 || kinds === void 0 || (mask & 1 << ch.kindIndex) !== 0) {
1227
+ pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1228
+ }
1226
1229
  }
1227
1230
  }
1228
1231
  }
1232
+ function triviaKindMask(labels, keep) {
1233
+ if (!labels) return void 0;
1234
+ let mask = 0;
1235
+ for (const name of keep) {
1236
+ const idx = labels.indexOf(name);
1237
+ if (idx >= 0) mask |= 1 << idx;
1238
+ }
1239
+ return mask;
1240
+ }
1229
1241
 
1230
1242
  // src/combinators/trivia-skip.ts
1231
1243
  var NOOP_COMMIT = () => {
@@ -1877,10 +1889,15 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
1877
1889
  const saved = beginCstNodeCapture(ctx);
1878
1890
  const savedFields = ctx._fields;
1879
1891
  ctx._fields = capturesFields ? [] : void 0;
1892
+ const savedMask = ctx._triviaCaptureMask;
1893
+ if (build === void 0 && ctx.build?._parsemanTriviaKinds !== void 0 && def.type !== void 0) {
1894
+ ctx._triviaCaptureMask = ctx.build._parsemanTriviaKinds(def.type);
1895
+ }
1880
1896
  if (!capturesTrivia) ctx.captureTrivia = false;
1881
1897
  const r = combinator.parse(input, pos, ctx);
1882
1898
  const fields = capturesFields ? buildFieldMap(ctx._fields) : void 0;
1883
1899
  ctx._fields = savedFields;
1900
+ ctx._triviaCaptureMask = savedMask;
1884
1901
  const { children, rawChildren, triviaLog } = endCstNodeCapture(ctx, saved);
1885
1902
  if (!r.ok) return r;
1886
1903
  const st = clonesState && ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
@@ -2018,7 +2035,13 @@ function parser(opts, root) {
2018
2035
  trivia: opts.trivia,
2019
2036
  ...opts.trivia._meta.triviaKindLabels ? { triviaKindLabels: opts.trivia._meta.triviaKindLabels } : {}
2020
2037
  } : {},
2021
- ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {}
2038
+ ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {},
2039
+ // Kind-filter for per-node capture. Resolve against this scope's trivia
2040
+ // labels — this parser's own trivia if it declares one, else the INHERITED
2041
+ // labels (`_ctx.triviaKindLabels`), so captureTriviaKinds still applies when
2042
+ // trivia is inherited rather than re-declared here. No labels → undefined
2043
+ // (capture all).
2044
+ ...opts.captureTriviaKinds && !clearTrivia ? { _triviaCaptureMask: triviaKindMask(opts.trivia?._meta.triviaKindLabels ?? _ctx?.triviaKindLabels, opts.captureTriviaKinds) } : {}
2022
2045
  };
2023
2046
  const result = root.parse(input, pos ?? 0, ctx);
2024
2047
  if (trackLines) {
@@ -3168,7 +3191,7 @@ function scanBranchLabeled(shape, kindIndex, mint) {
3168
3191
  ` if (${m.end} > _e) {`,
3169
3192
  ` if (_cap) {`,
3170
3193
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, ${m.end}, ${kindIndex})`,
3171
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,
3194
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << kindIndex}))) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,
3172
3195
  ` }`,
3173
3196
  ` _e = ${m.end}`,
3174
3197
  ` continue`,
@@ -3284,7 +3307,7 @@ function buildLabeledRegexTriviaFnDecl(fnName, spec, reNames) {
3284
3307
  ` const _ce = _e + _m${i}[0].length`,
3285
3308
  ` if (_cap) {`,
3286
3309
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,
3287
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3310
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3288
3311
  ` }`,
3289
3312
  ` _e = _ce`,
3290
3313
  ` _matched = true`,
@@ -3322,7 +3345,7 @@ function buildLabeledRuntimeTriviaFnDecl(fnName, spec, rpStartIndex) {
3322
3345
  ` const _ce = _r${i}.span.end`,
3323
3346
  ` if (_cap) {`,
3324
3347
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,
3325
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3348
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3326
3349
  ` }`,
3327
3350
  ` _e = _ce`,
3328
3351
  ` _matched = true`,
@@ -4720,6 +4743,7 @@ function emitNode(def, ctx, pos) {
4720
4743
  const tlV = capturesTrivia || structural ? v(ctx, "_tl") : "_EMPTY_TL";
4721
4744
  if (!capturesTrivia) ctx.needsEmptyTl = true;
4722
4745
  const sc = v(ctx, "_sc"), sl = v(ctx, "_sl"), sr = v(ctx, "_sr"), st = v(ctx, "_st"), stl = v(ctx, "_stl");
4746
+ const smk = structural ? v(ctx, "_smk") : null;
4723
4747
  if (structural) ctx.needsHostReads = true;
4724
4748
  const hostTriviaGate = `_ctx.build !== undefined && (_ctx.build._parsemanCaptureTrivia !== undefined ? _ctx.build._parsemanCaptureTrivia(${JSON.stringify(def.type)}) : (_ctx._pmCapTL ??= _hostReads(_ctx.build, 5)))`;
4725
4749
  const allocStmt = structural ? `${i}const ${capTLv} = ${hostTriviaGate}, ${capSTv} = _ctx._pmCapST ??= (_ctx.build === undefined || _hostReads(_ctx.build, 6))${capFv ? `, ${capFv} = _ctx.build !== undefined && _hostReads(_ctx.build, 2)` : ""}
@@ -4731,15 +4755,15 @@ ${i}const ${chV} = [], ${rawV} = [], ${tlV} = ${capTLv} ? [] : _EMPTY_TL` : capt
4731
4755
  const fObj = hasFields ? v(ctx, "_fields") : "undefined";
4732
4756
  const stmts = [
4733
4757
  allocStmt,
4734
- `${i}const ${sc} = _ctx._cstChildren, ${sl} = _ctx._cstLeaves, ${sr} = _ctx._cstRawChildren, ${st} = _ctx.captureTrivia, ${stl} = _ctx._cstTriviaLog${sf ? `, ${sf} = _ctx._fields` : ""}`,
4735
- `${i}_ctx._cstChildren = ${chV}; _ctx._cstLeaves = ${chV}; _ctx._cstRawChildren = ${rawV}; _ctx.captureTrivia = true; _ctx._cstTriviaLog = ${innerTl}${sf ? `; _ctx._fields = ${fieldsOn} ? [] : undefined` : ""}`
4758
+ `${i}const ${sc} = _ctx._cstChildren, ${sl} = _ctx._cstLeaves, ${sr} = _ctx._cstRawChildren, ${st} = _ctx.captureTrivia, ${stl} = _ctx._cstTriviaLog${smk ? `, ${smk} = _ctx._triviaCaptureMask` : ""}${sf ? `, ${sf} = _ctx._fields` : ""}`,
4759
+ `${i}_ctx._cstChildren = ${chV}; _ctx._cstLeaves = ${chV}; _ctx._cstRawChildren = ${rawV}; _ctx.captureTrivia = true; _ctx._cstTriviaLog = ${innerTl}${smk ? `; _ctx._triviaCaptureMask = ${capTLv} && _ctx.build._parsemanTriviaKinds !== undefined ? _ctx.build._parsemanTriviaKinds(${JSON.stringify(def.type)}) : ${smk}` : ""}${sf ? `; _ctx._fields = ${fieldsOn} ? [] : undefined` : ""}`
4736
4760
  ];
4737
4761
  const { stmts: innerStmts, okVar, endVar } = emitFallible(def.parser, ctx, pos);
4738
4762
  stmts.push(...innerStmts);
4739
4763
  if (sf && fArr) {
4740
4764
  stmts.push(`${i}const ${fArr} = _ctx._fields`);
4741
4765
  }
4742
- stmts.push(`${i}_ctx._cstChildren = ${sc}; _ctx._cstLeaves = ${sl}; _ctx._cstRawChildren = ${sr}; _ctx.captureTrivia = ${st}; _ctx._cstTriviaLog = ${stl}${sf ? `; _ctx._fields = ${sf}` : ""}`);
4766
+ stmts.push(`${i}_ctx._cstChildren = ${sc}; _ctx._cstLeaves = ${sl}; _ctx._cstRawChildren = ${sr}; _ctx.captureTrivia = ${st}; _ctx._cstTriviaLog = ${stl}${smk ? `; _ctx._triviaCaptureMask = ${smk}` : ""}${sf ? `; _ctx._fields = ${sf}` : ""}`);
4743
4767
  stmts.push(...emitIfFail(ctx, `!${okVar}`, propagateFailBody(ctx)));
4744
4768
  let stV = "undefined";
4745
4769
  if (structural) {