parseman 0.39.1 → 0.40.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.
@@ -2764,12 +2764,30 @@ function isCstChild(value) {
2764
2764
  function missingInferredType() {
2765
2765
  throw new Error('node(): inferred node type requires a rules() key; pass node("Type", parser) outside rules()');
2766
2766
  }
2767
+ function normalizeProject(project) {
2768
+ if (project === void 0) return void 0;
2769
+ if (!Number.isInteger(project) || project < 0) {
2770
+ throw new Error("node() project child must be a non-negative integer");
2771
+ }
2772
+ return project;
2773
+ }
2774
+ function projectChild(children, project, type) {
2775
+ if (!(project in children)) {
2776
+ throw new Error(`node(${JSON.stringify(type)}) project child ${project} was not captured`);
2777
+ }
2778
+ return unwrapChild(children[project]);
2779
+ }
2767
2780
  function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2768
2781
  const hasExplicitType = typeof typeOrCombinator === "string";
2769
2782
  const type = hasExplicitType ? typeOrCombinator : void 0;
2770
2783
  const combinator = hasExplicitType ? combinatorOrBuild : typeOrCombinator;
2771
- const build = hasExplicitType ? buildOrOpts : combinatorOrBuild;
2772
- const opts = hasExplicitType ? maybeOpts : buildOrOpts;
2784
+ const buildArg = hasExplicitType ? buildOrOpts : combinatorOrBuild;
2785
+ const build = typeof buildArg === "function" ? buildArg : void 0;
2786
+ const opts = typeof buildArg === "function" ? maybeOpts : buildArg ?? maybeOpts;
2787
+ const project = normalizeProject(opts?.project);
2788
+ if (project !== void 0 && build !== void 0) {
2789
+ throw new Error("node() options cannot combine project with a build callback");
2790
+ }
2773
2791
  const baseDef = { tag: "node", parser: combinator, ...type === void 0 ? {} : { type }, ...build === void 0 ? {} : { build } };
2774
2792
  const meta = {
2775
2793
  firstSet: combinator._meta.firstSet,
@@ -2783,11 +2801,14 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2783
2801
  if (unwrap && collapse) {
2784
2802
  throw new Error("node() options cannot set both unwrap and collapse");
2785
2803
  }
2786
- const def = unwrap || collapse || captureTrivia || trailingTrivia ? { ...baseDef, ...unwrap ? { unwrap: true } : {}, ...collapse ? { collapse: true } : {}, ...captureTrivia ? { captureTrivia: true } : {}, ...trailingTrivia ? { trailingTrivia: true } : {} } : baseDef;
2787
- const capturesTrivia = captureTrivia || trailingTrivia || (build ? buildReadsTrivia(def) : true);
2788
- const clonesState = build ? buildReadsState(def) : true;
2804
+ if (project !== void 0 && (unwrap || collapse)) {
2805
+ throw new Error("node() options cannot combine project with unwrap or collapse");
2806
+ }
2807
+ const def = unwrap || collapse || project !== void 0 || captureTrivia || trailingTrivia ? { ...baseDef, ...unwrap ? { unwrap: true } : {}, ...collapse ? { collapse: true } : {}, ...project !== void 0 ? { project } : {}, ...captureTrivia ? { captureTrivia: true } : {}, ...trailingTrivia ? { trailingTrivia: true } : {} } : baseDef;
2808
+ const capturesTrivia = captureTrivia || trailingTrivia || (build ? buildReadsTrivia(def) : project === void 0);
2809
+ const clonesState = build ? buildReadsState(def) : project === void 0;
2789
2810
  const hasOwnFields = parserHasOwnFields(combinator);
2790
- const capturesFields = hasOwnFields && (build ? buildReadsFields(def) : true);
2811
+ const capturesFields = hasOwnFields && (build ? buildReadsFields(def) : project === void 0);
2791
2812
  const guardable = combinator._meta.firstSet.kind !== "any" && !matchesEmpty(combinator);
2792
2813
  let failExpected;
2793
2814
  return {
@@ -2802,14 +2823,15 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2802
2823
  }
2803
2824
  return { ok: false, expected: failExpected, span: { start: pos, end: pos } };
2804
2825
  }
2805
- const hostCst = build !== void 0 && cstOutputHost(ctx.build);
2826
+ const hasDirectValue = build !== void 0 || project !== void 0;
2827
+ const hostCst = hasDirectValue && cstOutputHost(ctx.build);
2806
2828
  const effTrivia = capturesTrivia || hostCst;
2807
2829
  const effFields = capturesFields || hostCst && hasOwnFields;
2808
2830
  const saved = beginCstNodeCapture(ctx);
2809
2831
  const savedFields = ctx._fields;
2810
2832
  ctx._fields = effFields ? [] : void 0;
2811
2833
  const savedMask = ctx._triviaCaptureMask;
2812
- if (build === void 0 && ctx.build?._parsemanTriviaKinds !== void 0 && def.type !== void 0) {
2834
+ if (build === void 0 && project === void 0 && ctx.build?._parsemanTriviaKinds !== void 0 && def.type !== void 0) {
2813
2835
  ctx._triviaCaptureMask = ctx.build._parsemanTriviaKinds(def.type);
2814
2836
  }
2815
2837
  if (!effTrivia) ctx.captureTrivia = false;
@@ -2827,7 +2849,7 @@ function node(typeOrCombinator, combinatorOrBuild, buildOrOpts, maybeOpts) {
2827
2849
  const nodeType = def.type ?? missingInferredType();
2828
2850
  const cstOutput = cstOutputHost(ctx.build);
2829
2851
  const hostState = clonesState ? st : ctx.state !== void 0 ? Object.assign({}, ctx.state) : void 0;
2830
- const built = unwrap && children.length === 1 ? unwrapChild(children[0]) : collapse && children.length === 1 ? children[0] : !build && ctx.build?._parsemanCstCollapse && children.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children[0], children, rawChildren) ? children[0] : build ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, r.span, rawChildren, triviaLog, hostState) : build(children, fields, r.span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children, fields, r.span, rawChildren, triviaLog, st) : { _tag: "node", type: nodeType, span: { start: r.span.start, end: r.span.end }, state: st ?? null, children };
2852
+ const built = unwrap && children.length === 1 ? unwrapChild(children[0]) : collapse && children.length === 1 ? children[0] : !build && project === void 0 && ctx.build?._parsemanCstCollapse && children.length === 1 && rawChildren.length === 1 && ctx.build._parsemanCstCollapse(nodeType, children[0], children, rawChildren) ? children[0] : project !== void 0 ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, r.span, rawChildren, triviaLog, hostState) : projectChild(children, project, nodeType) : build ? cstOutput && ctx.build ? ctx.build(nodeType, children, fields, r.span, rawChildren, triviaLog, hostState) : build(children, fields, r.span, rawChildren, triviaLog, st) : ctx.build ? ctx.build(nodeType, children, fields, r.span, rawChildren, triviaLog, st) : { _tag: "node", type: nodeType, span: { start: r.span.start, end: r.span.end }, state: st ?? null, children };
2831
2853
  const rawEntry = isCstChild(built) ? built : { _tag: "leaf", value: typeof built === "string" ? built : typeof built === "object" && built !== null ? input.slice(r.span.start, r.span.end) : "", span: r.span };
2832
2854
  if (saved.buf !== void 0 || saved.ch !== void 0) {
2833
2855
  pushCstChild(ctx, built, rawEntry);