parseman 0.26.3 → 0.27.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.
@@ -2252,6 +2252,55 @@ function parserHasOwnFields(p, seen = /* @__PURE__ */ new Set()) {
2252
2252
  return false;
2253
2253
  }
2254
2254
  }
2255
+ function parserHasTriviaSite(p, seen = /* @__PURE__ */ new Set()) {
2256
+ if (seen.has(p)) return true;
2257
+ seen.add(p);
2258
+ const d = p._def;
2259
+ switch (d.tag) {
2260
+ // Trivia is skipped between/around elements or iterations → a site.
2261
+ case "sequence":
2262
+ case "many":
2263
+ case "oneOrMore":
2264
+ case "sepBy":
2265
+ case "scanTo":
2266
+ case "recover":
2267
+ case "skip":
2268
+ return true;
2269
+ // Nested node manages its own trivia frame; none logs here.
2270
+ case "node":
2271
+ return false;
2272
+ // Pure terminals + trivia-suppressing token: no site.
2273
+ case "regex":
2274
+ case "literal":
2275
+ case "keywords":
2276
+ case "guard":
2277
+ case "token":
2278
+ return false;
2279
+ // Transparent single-child wrappers: recurse.
2280
+ case "optional":
2281
+ case "transform":
2282
+ case "trivia":
2283
+ case "label":
2284
+ case "expect":
2285
+ case "withCtx":
2286
+ case "not":
2287
+ case "field":
2288
+ return parserHasTriviaSite(d.parser, seen);
2289
+ case "choice":
2290
+ return d.parsers.some((x) => parserHasTriviaSite(x, seen));
2291
+ case "grammar":
2292
+ return parserHasTriviaSite(d.parser, seen);
2293
+ case "lazy": {
2294
+ try {
2295
+ return parserHasTriviaSite(d.thunk(), seen);
2296
+ } catch {
2297
+ return true;
2298
+ }
2299
+ }
2300
+ default:
2301
+ return true;
2302
+ }
2303
+ }
2255
2304
  function buildFieldMap(captures) {
2256
2305
  if (!captures?.length) return void 0;
2257
2306
  const out = {};
@@ -5226,32 +5275,49 @@ function emitNode(def, ctx, pos) {
5226
5275
  const smk = structural ? v(ctx, "_smk") : null;
5227
5276
  if (structural) ctx.needsHostReads = true;
5228
5277
  const hostTriviaGate = `_ctx.build !== undefined && (_ctx.build._parsemanCaptureTrivia !== undefined ? _ctx.build._parsemanCaptureTrivia(${JSON.stringify(def.type)}) : (_ctx._pmCapTL ??= _hostReads(_ctx.build, 5)))`;
5229
- 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)` : ""}
5230
- ${i}const ${chV} = [], ${rawV} = [], ${tlV} = ${capTLv} ? [] : _EMPTY_TL` : capturesTrivia ? `${i}const ${chV} = [], ${rawV} = [], ${tlV} = []` : `${i}const ${chV} = [], ${rawV} = []`;
5278
+ const pmV = v(ctx, "_pm"), recV = v(ctx, "_rec"), capV = v(ctx, "_cap");
5279
+ const profHoist = `${i}const ${pmV} = _ctx._pmProfile, ${recV} = ${pmV}?.phase === 'recognizer', ${capV} = ${pmV}?.phase === 'capture'`;
5280
+ const profileRecognizer = recV;
5281
+ const profileCapture = capV;
5282
+ const allocStmt = structural ? `${i}const ${capTLv} = !(${profileRecognizer}) && (${profileCapture} || ${hostTriviaGate}), ${capSTv} = !(${profileRecognizer} || ${profileCapture}) && (_ctx._pmCapST ??= (_ctx.build === undefined || _hostReads(_ctx.build, 6)))${capFv ? `, ${capFv} = !(${profileRecognizer}) && (${profileCapture} || (_ctx.build !== undefined && _hostReads(_ctx.build, 2)))` : ""}
5283
+ ${i}const ${chV} = ${profileRecognizer} ? undefined : [], ${rawV} = ${profileRecognizer} ? undefined : [], ${tlV} = ${capTLv} ? [] : _EMPTY_TL` : capturesTrivia ? `${i}const ${chV} = ${profileRecognizer} ? undefined : [], ${rawV} = ${profileRecognizer} ? undefined : [], ${tlV} = ${profileRecognizer} ? undefined : []` : `${i}const ${chV} = ${profileRecognizer} ? undefined : [], ${rawV} = ${profileRecognizer} ? undefined : []`;
5231
5284
  const innerTl = structural ? `${capTLv} ? ${tlV} : undefined` : capturesTrivia ? tlV : "undefined";
5232
- const fieldsOn = structural ? capFv ?? "false" : capturesFields ? "true" : "false";
5285
+ const fieldsOn = structural ? capFv ?? "false" : `${profileRecognizer} ? false : ${capturesFields ? "true" : "false"}`;
5233
5286
  const sf = hasFields ? v(ctx, "_sf") : null;
5234
5287
  const fArr = hasFields ? v(ctx, "_fa") : null;
5235
5288
  const fObj = hasFields ? v(ctx, "_fields") : "undefined";
5289
+ const needsTriviaFrame = parserHasTriviaSite(def.parser);
5290
+ const saveTrivia = needsTriviaFrame ? `, ${st} = _ctx.captureTrivia, ${stl} = _ctx._cstTriviaLog${smk ? `, ${smk} = _ctx._triviaCaptureMask` : ""}` : "";
5291
+ const installTrivia = needsTriviaFrame ? `; _ctx.captureTrivia = true; _ctx._cstTriviaLog = ${innerTl}${smk ? `; _ctx._triviaCaptureMask = ${capTLv} && _ctx.build._parsemanTriviaKinds !== undefined ? _ctx.build._parsemanTriviaKinds(${JSON.stringify(def.type)}) : ${smk}` : ""}` : "";
5292
+ const restoreTrivia = needsTriviaFrame ? `; _ctx.captureTrivia = ${st}; _ctx._cstTriviaLog = ${stl}${smk ? `; _ctx._triviaCaptureMask = ${smk}` : ""}` : "";
5236
5293
  const stmts = [
5294
+ profHoist,
5237
5295
  allocStmt,
5238
- `${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` : ""}`,
5239
- `${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` : ""}`
5296
+ `${i}const ${sc} = _ctx._cstChildren, ${sl} = _ctx._cstLeaves, ${sr} = _ctx._cstRawChildren${saveTrivia}${sf ? `, ${sf} = _ctx._fields` : ""}`,
5297
+ `${i}_ctx._cstChildren = ${chV}; _ctx._cstLeaves = ${chV}; _ctx._cstRawChildren = ${rawV}${installTrivia}${sf ? `; _ctx._fields = ${fieldsOn} ? [] : undefined` : ""}`
5240
5298
  ];
5241
5299
  const { stmts: innerStmts, okVar, endVar } = emitFallible(def.parser, ctx, pos);
5242
5300
  stmts.push(...innerStmts);
5243
5301
  if (sf && fArr) {
5244
5302
  stmts.push(`${i}const ${fArr} = _ctx._fields`);
5245
5303
  }
5246
- 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}` : ""}`);
5304
+ stmts.push(`${i}_ctx._cstChildren = ${sc}; _ctx._cstLeaves = ${sl}; _ctx._cstRawChildren = ${sr}${restoreTrivia}${sf ? `; _ctx._fields = ${sf}` : ""}`);
5247
5305
  stmts.push(...emitIfFail(ctx, `!${okVar}`, propagateFailBody(ctx)));
5306
+ stmts.push(
5307
+ `${i}if (_ctx._pmProfile) {`,
5308
+ `${i} _ctx._pmProfile.nodes++`,
5309
+ `${i} if (${profileCapture}) {`,
5310
+ `${i} _ctx._pmProfile.childSlots += ${chV}.length; _ctx._pmProfile.rawSlots += ${rawV}.length; _ctx._pmProfile.triviaSlots += ${tlV}.length${fArr ? `; _ctx._pmProfile.fieldSlots += ${fArr} ? ${fArr}.length : 0` : ""}`,
5311
+ `${i} }`,
5312
+ `${i}}`
5313
+ );
5248
5314
  let stV = "undefined";
5249
5315
  if (structural) {
5250
5316
  stV = v(ctx, "_nst");
5251
5317
  stmts.push(`${i}const ${stV} = ${capSTv} && _ctx.state !== undefined ? Object.assign({}, _ctx.state) : undefined`);
5252
5318
  } else if (clonesState) {
5253
5319
  stV = v(ctx, "_nst");
5254
- stmts.push(`${i}const ${stV} = _ctx.state !== undefined ? Object.assign({}, _ctx.state) : undefined`);
5320
+ stmts.push(`${i}const ${stV} = !(${profileRecognizer} || ${profileCapture}) && _ctx.state !== undefined ? Object.assign({}, _ctx.state) : undefined`);
5255
5321
  }
5256
5322
  if (sf && fArr) {
5257
5323
  const fe = v(ctx, "_fe");
@@ -5271,15 +5337,17 @@ ${i}const ${chV} = [], ${rawV} = [], ${tlV} = ${capTLv} ? [] : _EMPTY_TL` : capt
5271
5337
  }
5272
5338
  const ndV = v(ctx, "_nd");
5273
5339
  const buildExpr = structural ? `{ _tag: 'node', type: ${JSON.stringify(def.type)}, span: { start: ${pos}, end: ${endVar} }, state: ${stV} ?? null, children: ${chV} }` : mkType ? emitInlineMkNodeExpr(mkType, chV, rawV, pos, endVar, tlV) : `${buildRef(ctx)}[${buildIdx}](${chV}, ${fObj}, { start: ${pos}, end: ${endVar} }, ${rawV}, ${tlV}, ${stV})`;
5274
- const ndExpr = ctx.ns || structural ? `_ctx.build !== undefined ? _ctx.build(${JSON.stringify(def.type)}, ${chV}, ${fObj}, { start: ${pos}, end: ${endVar} }, ${rawV}, ${tlV}, ${stV}) : (${buildExpr})` : buildExpr;
5340
+ const ndExpr = ctx.ns || structural ? `_ctx.build !== undefined ? (_ctx._pmProfile?.phase === 'host' && _ctx._pmProfile.hostCalls++, _ctx.build(${JSON.stringify(def.type)}, ${chV}, ${fObj}, { start: ${pos}, end: ${endVar} }, ${rawV}, ${tlV}, ${stV})) : (${buildExpr})` : buildExpr;
5275
5341
  const hostCollapseExpr = ctx.ns || structural ? `_ctx.build !== undefined && _ctx.build._parsemanCstCollapse !== undefined && ${chV}.length === 1 && ${rawV}.length === 1 && _ctx.build._parsemanCstCollapse(${JSON.stringify(def.type)}, ${chV}[0], ${chV}, ${rawV}) ? ${chV}[0] : (${ndExpr})` : ndExpr;
5276
5342
  const unwrapExpr = `${chV}.length === 1 ? (${chV}[0] !== null && typeof ${chV}[0] === 'object' && ${chV}[0]._tag === 'leaf' ? ${chV}[0].value : ${chV}[0]) : (${ndExpr})`;
5277
5343
  const collapseExpr = `${chV}.length === 1 ? ${chV}[0] : (${ndExpr})`;
5278
5344
  const finalExpr = def.unwrap ? unwrapExpr : def.collapse ? collapseExpr : hostCollapseExpr;
5279
5345
  stmts.push(
5280
- `${i}const ${ndV} = ${finalExpr}`,
5281
- `${i}if (${sc}) ${sc}.push(${ndV})`,
5282
- `${i}if (${sr}) ${sr}.push((typeof ${ndV} === 'object' && ${ndV} !== null && (${ndV}._tag === 'node' || ${ndV}._tag === 'leaf' || ${ndV}._tag === 'parseError')) ? ${ndV} : { _tag: 'leaf', value: typeof ${ndV} === 'string' ? ${ndV} : '', span: { start: ${pos}, end: ${endVar} } })`
5346
+ `${i}const ${ndV} = (${profileRecognizer} || ${profileCapture}) ? undefined : (${finalExpr})`,
5347
+ `${i}if (!(${profileRecognizer})) {`,
5348
+ `${i} if (${sc}) ${sc}.push(${ndV})`,
5349
+ `${i} if (${sr}) ${sr}.push((typeof ${ndV} === 'object' && ${ndV} !== null && (${ndV}._tag === 'node' || ${ndV}._tag === 'leaf' || ${ndV}._tag === 'parseError')) ? ${ndV} : { _tag: 'leaf', value: typeof ${ndV} === 'string' ? ${ndV} : '', span: { start: ${pos}, end: ${endVar} } })`,
5350
+ `${i}}`
5283
5351
  );
5284
5352
  return { stmts, valueVar: ndV, endVar };
5285
5353
  }