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.
@@ -1254,13 +1254,25 @@ function tryFastLabeledScan(input, cur, trivia2) {
1254
1254
  }
1255
1255
  function recordTriviaChunks(ctx, chunks) {
1256
1256
  const kinds = ctx.triviaKindLabels;
1257
+ const mask = ctx._triviaCaptureMask;
1257
1258
  for (const ch of chunks) {
1258
1259
  pushTriviaLogEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1259
1260
  if (ctx.captureTrivia && (ctx._cstBuf !== void 0 || ctx._cstTriviaLog !== void 0)) {
1260
- pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1261
+ if (mask === void 0 || kinds === void 0 || (mask & 1 << ch.kindIndex) !== 0) {
1262
+ pushCstTriviaEntry(ctx, ch.start, ch.end, kinds ? ch.kindIndex : void 0);
1263
+ }
1261
1264
  }
1262
1265
  }
1263
1266
  }
1267
+ function triviaKindMask(labels, keep) {
1268
+ if (!labels) return void 0;
1269
+ let mask = 0;
1270
+ for (const name of keep) {
1271
+ const idx = labels.indexOf(name);
1272
+ if (idx >= 0) mask |= 1 << idx;
1273
+ }
1274
+ return mask;
1275
+ }
1264
1276
 
1265
1277
  // src/combinators/trivia-skip.ts
1266
1278
  var NOOP_COMMIT = () => {
@@ -1912,10 +1924,15 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
1912
1924
  const saved = beginCstNodeCapture(ctx);
1913
1925
  const savedFields = ctx._fields;
1914
1926
  ctx._fields = capturesFields ? [] : void 0;
1927
+ const savedMask = ctx._triviaCaptureMask;
1928
+ if (build === void 0 && ctx.build?._parsemanTriviaKinds !== void 0 && def.type !== void 0) {
1929
+ ctx._triviaCaptureMask = ctx.build._parsemanTriviaKinds(def.type);
1930
+ }
1915
1931
  if (!capturesTrivia) ctx.captureTrivia = false;
1916
1932
  const r = combinator.parse(input, pos, ctx);
1917
1933
  const fields = capturesFields ? buildFieldMap(ctx._fields) : void 0;
1918
1934
  ctx._fields = savedFields;
1935
+ ctx._triviaCaptureMask = savedMask;
1919
1936
  const { children, rawChildren, triviaLog } = endCstNodeCapture(ctx, saved);
1920
1937
  if (!r.ok) return r;
1921
1938
  const st = clonesState && ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
@@ -2053,7 +2070,13 @@ function parser(opts, root) {
2053
2070
  trivia: opts.trivia,
2054
2071
  ...opts.trivia._meta.triviaKindLabels ? { triviaKindLabels: opts.trivia._meta.triviaKindLabels } : {}
2055
2072
  } : {},
2056
- ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {}
2073
+ ...opts.captureTrivia || _ctx?.captureTrivia ? { captureTrivia: true } : {},
2074
+ // Kind-filter for per-node capture. Resolve against this scope's trivia
2075
+ // labels — this parser's own trivia if it declares one, else the INHERITED
2076
+ // labels (`_ctx.triviaKindLabels`), so captureTriviaKinds still applies when
2077
+ // trivia is inherited rather than re-declared here. No labels → undefined
2078
+ // (capture all).
2079
+ ...opts.captureTriviaKinds && !clearTrivia ? { _triviaCaptureMask: triviaKindMask(opts.trivia?._meta.triviaKindLabels ?? _ctx?.triviaKindLabels, opts.captureTriviaKinds) } : {}
2057
2080
  };
2058
2081
  const result = root.parse(input, pos ?? 0, ctx);
2059
2082
  if (trackLines) {
@@ -3203,7 +3226,7 @@ function scanBranchLabeled(shape, kindIndex, mint) {
3203
3226
  ` if (${m.end} > _e) {`,
3204
3227
  ` if (_cap) {`,
3205
3228
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, ${m.end}, ${kindIndex})`,
3206
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,
3229
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << kindIndex}))) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,
3207
3230
  ` }`,
3208
3231
  ` _e = ${m.end}`,
3209
3232
  ` continue`,
@@ -3319,7 +3342,7 @@ function buildLabeledRegexTriviaFnDecl(fnName, spec, reNames) {
3319
3342
  ` const _ce = _e + _m${i}[0].length`,
3320
3343
  ` if (_cap) {`,
3321
3344
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,
3322
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3345
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3323
3346
  ` }`,
3324
3347
  ` _e = _ce`,
3325
3348
  ` _matched = true`,
@@ -3357,7 +3380,7 @@ function buildLabeledRuntimeTriviaFnDecl(fnName, spec, rpStartIndex) {
3357
3380
  ` const _ce = _r${i}.span.end`,
3358
3381
  ` if (_cap) {`,
3359
3382
  ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,
3360
- ` if (_ctx._cstTriviaLog !== undefined) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3383
+ ` if (_ctx._cstTriviaLog !== undefined && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,
3361
3384
  ` }`,
3362
3385
  ` _e = _ce`,
3363
3386
  ` _matched = true`,
@@ -4755,6 +4778,7 @@ function emitNode(def, ctx, pos) {
4755
4778
  const tlV = capturesTrivia || structural ? v(ctx, "_tl") : "_EMPTY_TL";
4756
4779
  if (!capturesTrivia) ctx.needsEmptyTl = true;
4757
4780
  const sc = v(ctx, "_sc"), sl = v(ctx, "_sl"), sr = v(ctx, "_sr"), st = v(ctx, "_st"), stl = v(ctx, "_stl");
4781
+ const smk = structural ? v(ctx, "_smk") : null;
4758
4782
  if (structural) ctx.needsHostReads = true;
4759
4783
  const hostTriviaGate = `_ctx.build !== undefined && (_ctx.build._parsemanCaptureTrivia !== undefined ? _ctx.build._parsemanCaptureTrivia(${JSON.stringify(def.type)}) : (_ctx._pmCapTL ??= _hostReads(_ctx.build, 5)))`;
4760
4784
  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)` : ""}
@@ -4766,15 +4790,15 @@ ${i}const ${chV} = [], ${rawV} = [], ${tlV} = ${capTLv} ? [] : _EMPTY_TL` : capt
4766
4790
  const fObj = hasFields ? v(ctx, "_fields") : "undefined";
4767
4791
  const stmts = [
4768
4792
  allocStmt,
4769
- `${i}const ${sc} = _ctx._cstChildren, ${sl} = _ctx._cstLeaves, ${sr} = _ctx._cstRawChildren, ${st} = _ctx.captureTrivia, ${stl} = _ctx._cstTriviaLog${sf ? `, ${sf} = _ctx._fields` : ""}`,
4770
- `${i}_ctx._cstChildren = ${chV}; _ctx._cstLeaves = ${chV}; _ctx._cstRawChildren = ${rawV}; _ctx.captureTrivia = true; _ctx._cstTriviaLog = ${innerTl}${sf ? `; _ctx._fields = ${fieldsOn} ? [] : undefined` : ""}`
4793
+ `${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` : ""}`,
4794
+ `${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` : ""}`
4771
4795
  ];
4772
4796
  const { stmts: innerStmts, okVar, endVar } = emitFallible(def.parser, ctx, pos);
4773
4797
  stmts.push(...innerStmts);
4774
4798
  if (sf && fArr) {
4775
4799
  stmts.push(`${i}const ${fArr} = _ctx._fields`);
4776
4800
  }
4777
- stmts.push(`${i}_ctx._cstChildren = ${sc}; _ctx._cstLeaves = ${sl}; _ctx._cstRawChildren = ${sr}; _ctx.captureTrivia = ${st}; _ctx._cstTriviaLog = ${stl}${sf ? `; _ctx._fields = ${sf}` : ""}`);
4801
+ 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}` : ""}`);
4778
4802
  stmts.push(...emitIfFail(ctx, `!${okVar}`, propagateFailBody(ctx)));
4779
4803
  let stV = "undefined";
4780
4804
  if (structural) {