parseman 0.22.0 → 0.23.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.
@@ -112,5 +112,7 @@ export type LinkablePieces = {
112
112
  mfFns: ReadonlyArray<(...args: unknown[]) => unknown>;
113
113
  buildFns: ReadonlyArray<(...args: unknown[]) => unknown>;
114
114
  };
115
- export declare function compileLinkable(ruleMapArg: ReadonlyArray<readonly [string, Combinator<unknown>]>, ns: string): LinkablePieces | null;
115
+ export declare function compileLinkable(ruleMapArg: ReadonlyArray<readonly [string, Combinator<unknown>]>, ns: string, opts?: {
116
+ trivia?: Combinator<unknown>;
117
+ }): LinkablePieces | null;
116
118
  //# sourceMappingURL=codegen.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../../src/compiler/codegen.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,UAAU,EAAa,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAA4B,MAAM,aAAa,CAAA;AA0BnI;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,uTAC0R,CAAA;AActT,8EAA8E;AAC9E,wBAAgB,oBAAoB,IAAI,IAAI,CAE3C;AAED,+EAA+E;AAC/E,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAI7C;AA6bD,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,MAAM,CAQlE;AAcD,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,OAAO,GAAG,IAAI,CAA0B;AA01DjF,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC9B,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;IAClD,kGAAkG;IAClG,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,YAA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3B,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B;;;;OAIG;IACH,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IAChC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;IAC3B,YAAY,EAAE,OAAO,CAAA;IACrB,cAAc,EAAE,OAAO,CAAA;IACvB;;;;;OAKG;IACH,KAAK,EAAE,aAAa,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAA;IACrD,QAAQ,EAAE,aAAa,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAA;CACzD,CAAA;AAED,wBAAgB,eAAe,CAC7B,UAAU,EAAE,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,EACjE,EAAE,EAAE,MAAM,EACV,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;CAAE,GACtC,cAAc,GAAG,IAAI,CAqLvB"}
@@ -1,6 +1,6 @@
1
1
  import type { LinkablePieces } from './codegen.ts';
2
2
  import type { BuildHost, Combinator, CstCollapsePredicate } from '../types.ts';
3
- export declare function linkable(rulesMap: Record<string, Combinator<unknown>>, ns?: string): LinkablePieces;
3
+ export declare function linkable(rulesMap: Record<string, Combinator<unknown>>, ns?: string, trivia?: Combinator<unknown>): LinkablePieces;
4
4
  export type CstBuildHostOptions = {
5
5
  /**
6
6
  * Collapse transparent one-child CST wrapper nodes at build time.
@@ -43,6 +43,11 @@ export type FusedRule = (input: string, pos: number, ctx: Record<string, unknown
43
43
  * compiled artifact; returns an artifact for `compose()`.
44
44
  */
45
45
  export declare function pick(grammar: LinkablePieces | Record<string, Combinator<unknown>>, names: string[]): LinkablePieces;
46
+ /** Restrict a set of linkable pieces to `names` + their transitive dep closure,
47
+ * keeping each surviving rule in the piece that WINS it (later piece wins, matching
48
+ * compose override order). Shared by `pick()` (runtime) and the macro's build-time
49
+ * `pick(...)` handling, so à-la-carte selection is identical on both paths. */
50
+ export declare function pickPieces(pieces: LinkablePieces[], names: string[]): LinkablePieces[];
46
51
  /** Fuse at RUNTIME (via `new Function`) — used by `compose()` when not compiled
47
52
  * by the macro (like `compile()`). The macro path uses `emitFusedSource` instead. */
48
53
  export declare function fuseRules(pieces: LinkablePieces[]): Record<string, FusedRule>;
@@ -62,6 +67,6 @@ export type IRPiece = {
62
67
  };
63
68
  /** Materialize a carried item to full `LinkablePieces`: an IR piece is evaluated
64
69
  * back to a rule map and re-lowered; a full piece passes through. */
65
- export declare function materializePiece(p: LinkablePieces | IRPiece): LinkablePieces;
70
+ export declare function materializePiece(p: LinkablePieces | IRPiece, trivia?: Combinator<unknown>): LinkablePieces;
66
71
  export declare function compose(items: Array<LinkablePieces | Record<string, unknown>>): Record<string, FusedRule>;
67
72
  //# sourceMappingURL=linker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"linker.d.ts","sourceRoot":"","sources":["../../src/compiler/linker.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,oBAAoB,EAAY,MAAM,aAAa,CAAA;AAaxF,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,EAC7C,EAAE,CAAC,EAAE,MAAM,GACV,cAAc,CAIhB;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;;;OAMG;IACH,QAAQ,CAA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AAM,EAAE,SAAS,CAAC,CAe3B"}
package/dist/index.cjs CHANGED
@@ -63,7 +63,6 @@ __export(index_exports, {
63
63
  parse: () => parse,
64
64
  parseDoc: () => parseDoc,
65
65
  parser: () => parser,
66
- pick: () => pick,
67
66
  recover: () => recover,
68
67
  ref: () => ref,
69
68
  regex: () => regex,
@@ -5522,9 +5521,10 @@ function publicRuleWrapperSource(rule, fnSource) {
5522
5521
  if (labels === void 0) return fnSource;
5523
5522
  return `Object.assign(${fnSource}, { _meta: { triviaKindLabels: ${JSON.stringify(labels)} } })`;
5524
5523
  }
5525
- function compileLinkable(ruleMapArg, ns) {
5524
+ function compileLinkable(ruleMapArg, ns, opts) {
5526
5525
  if (!ns) throw new Error("compileLinkable: ns must be a non-empty namespace");
5527
5526
  for (const [, rule] of ruleMapArg) markUnusedValues(rule);
5527
+ const grammarTrivia = opts?.trivia ?? ruleMapArg.map(([, r]) => r._meta.grammarTrivia).find(Boolean);
5528
5528
  const ruleMap = ruleMapArg.filter(([, val]) => {
5529
5529
  const d = val._def;
5530
5530
  if (d.tag !== "lazy") return true;
@@ -5555,7 +5555,8 @@ function compileLinkable(ruleMapArg, ns) {
5555
5555
  capturing: ruleMap.some(([, rule]) => hasNodeDef(rule)),
5556
5556
  lazyUsage: analyzeLazyUsageMulti(ruleMap.map(([, rule]) => rule)),
5557
5557
  ns,
5558
- deferFirstSetRefs: true
5558
+ deferFirstSetRefs: true,
5559
+ ...grammarTrivia ? { activeTrivia: grammarTrivia, triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {}
5559
5560
  };
5560
5561
  const seen = /* @__PURE__ */ new Map();
5561
5562
  const ruleNames = /* @__PURE__ */ new Map();
@@ -5618,7 +5619,10 @@ function compileLinkable(ruleMapArg, ns) {
5618
5619
  ctx.indent = 1;
5619
5620
  ctx.failLabel = "_pfail";
5620
5621
  ctx.recordFail = true;
5622
+ const savedTrivia = ctx.activeTrivia;
5623
+ if (rule._meta.isTrivia) ctx.activeTrivia = void 0;
5621
5624
  const body = emit(resolved, ctx, "_pos");
5625
+ ctx.activeTrivia = savedTrivia;
5622
5626
  ctx.indent = savedIndent;
5623
5627
  ctx.failLabel = savedFail;
5624
5628
  ctx.recordFail = savedRec;
@@ -5817,6 +5821,64 @@ function escapeClassChar(ch) {
5817
5821
  }
5818
5822
 
5819
5823
  // src/compiler/ir-serialize.ts
5824
+ function childrenOf2(def) {
5825
+ switch (def.tag) {
5826
+ case "sequence":
5827
+ case "choice":
5828
+ return def.parsers;
5829
+ case "many":
5830
+ case "oneOrMore":
5831
+ case "optional":
5832
+ case "transform":
5833
+ case "trivia":
5834
+ case "token":
5835
+ case "label":
5836
+ case "field":
5837
+ case "not":
5838
+ case "node":
5839
+ case "expect":
5840
+ return [def.parser];
5841
+ case "grammar":
5842
+ return def.triviaParser ? [def.parser, def.triviaParser] : [def.parser];
5843
+ case "sepBy":
5844
+ return [def.parser, def.separator];
5845
+ case "skip":
5846
+ return [def.main, def.skipped];
5847
+ case "scanTo":
5848
+ return [def.sentinel, ...def.skip];
5849
+ case "lazy":
5850
+ case "literal":
5851
+ case "regex":
5852
+ case "keywords":
5853
+ case "guard":
5854
+ case "withCtx":
5855
+ case "recover":
5856
+ case "unknown":
5857
+ return [];
5858
+ }
5859
+ }
5860
+ var ruleNameOf = (c) => c._ruleName;
5861
+ function lazyTarget(c) {
5862
+ if (c._def.tag !== "lazy") return null;
5863
+ try {
5864
+ return c._def.thunk();
5865
+ } catch {
5866
+ return null;
5867
+ }
5868
+ }
5869
+ var Unserializable = class extends Error {
5870
+ };
5871
+ function serializeRuleMap(ruleMap) {
5872
+ try {
5873
+ return new Serializer(ruleMap).run();
5874
+ } catch (e) {
5875
+ if (e instanceof Unserializable) {
5876
+ if (process.env.PARSEMAN_IR_DEBUG) console.error(`[ir] fallback: ${e.message}`);
5877
+ return null;
5878
+ }
5879
+ throw e;
5880
+ }
5881
+ }
5820
5882
  function evalRuleMapIR(ir) {
5821
5883
  const _tf = (child, src) => {
5822
5884
  let fn2;
@@ -5891,11 +5953,172 @@ function evalRuleMapIR(ir) {
5891
5953
  );
5892
5954
  return Object.entries(map);
5893
5955
  }
5956
+ var Serializer = class {
5957
+ counts = /* @__PURE__ */ new Map();
5958
+ selfRef = /* @__PURE__ */ new Set();
5959
+ // combinators reached by an unnamed lazy pointing back into their own subtree
5960
+ constName = /* @__PURE__ */ new Map();
5961
+ emitted = /* @__PURE__ */ new Set();
5962
+ decls = [];
5963
+ ruleValues = /* @__PURE__ */ new Set();
5964
+ ruleMap;
5965
+ constructor(ruleMap) {
5966
+ this.ruleMap = ruleMap;
5967
+ for (const [, c] of ruleMap) this.ruleValues.add(this.body(c));
5968
+ }
5969
+ /** A `rules()` map wraps each rule value in a named `lazy` proxy; the real body is
5970
+ * its target (compileLinkable unwraps the same way). A ref INSIDE a body stays a
5971
+ * `g[name]` ref — only the top-level entry is unwrapped. */
5972
+ body(c) {
5973
+ return c._def.tag === "lazy" && ruleNameOf(c) !== void 0 ? lazyTarget(c) ?? c : c;
5974
+ }
5975
+ run() {
5976
+ for (const [, c] of this.ruleMap) this.analyze(this.body(c), /* @__PURE__ */ new Set());
5977
+ let n = 0;
5978
+ for (const [c, count] of this.counts) {
5979
+ if (this.ruleValues.has(c)) continue;
5980
+ if (count >= 2 || this.selfRef.has(c)) this.constName.set(c, `_s${n++}`);
5981
+ }
5982
+ const body = this.ruleMap.map(([name, c]) => ` ${JSON.stringify(name)}: ${this.wrap(this.body(c))}`).join(",\n");
5983
+ if (this.decls.length === 0) return `rules((g) => ({
5984
+ ${body}
5985
+ }))`;
5986
+ return `rules((g) => {
5987
+ ${this.decls.map((d) => " " + d).join("\n")}
5988
+ return ({
5989
+ ${body}
5990
+ })
5991
+ })`;
5992
+ }
5993
+ /** Count identity references and flag self-referential subtrees. `active` is the
5994
+ * set of combinators on the current DFS path (to detect a lazy pointing back). */
5995
+ analyze(c, active) {
5996
+ const def = c._def;
5997
+ if (def.tag === "lazy") {
5998
+ if (ruleNameOf(c) !== void 0) return;
5999
+ const target = lazyTarget(c);
6000
+ if (!target) throw new Unserializable("unresolved unnamed ref");
6001
+ if (active.has(target)) {
6002
+ this.selfRef.add(target);
6003
+ return;
6004
+ }
6005
+ this.analyze(target, active);
6006
+ return;
6007
+ }
6008
+ if (def.tag === "guard" || def.tag === "withCtx" || def.tag === "recover" || def.tag === "unknown") {
6009
+ throw new Unserializable(`unsupported tag ${def.tag}`);
6010
+ }
6011
+ this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
6012
+ if ((this.counts.get(c) ?? 0) > 1) return;
6013
+ active.add(c);
6014
+ for (const child of childrenOf2(def)) this.analyze(child, active);
6015
+ active.delete(c);
6016
+ }
6017
+ /** Reference `c`: a const var if it has one (emitting its decl first), else inline.
6018
+ * `selfOf` is the const currently being wrapped in a ref()/define closure — an
6019
+ * unnamed lazy back into it emits the local ref var instead of recursing. */
6020
+ ref(c, selfOf) {
6021
+ const cn = this.constName.get(c);
6022
+ if (cn) {
6023
+ this.emitDecl(c);
6024
+ return cn;
6025
+ }
6026
+ return this.expr(c, selfOf);
6027
+ }
6028
+ emitDecl(c) {
6029
+ const name = this.constName.get(c);
6030
+ if (this.emitted.has(name)) return;
6031
+ this.emitted.add(name);
6032
+ this.decls.push(`const ${name} = ${this.wrap(c)}`);
6033
+ }
6034
+ selfN = 0;
6035
+ /** Emit `c`'s own constructor call, wrapping it in a `ref()`+`.define()` closure
6036
+ * if it is self-referential (an unnamed lazy in its subtree points back to it) —
6037
+ * the shape `balanced()` and other recursive library combinators build. */
6038
+ wrap(c) {
6039
+ if (!this.selfRef.has(c)) return this.expr(c, null);
6040
+ const rv = `_rr${this.selfN++}`;
6041
+ const bodyExpr = this.expr(c, { comb: c, var: rv });
6042
+ return `(() => { const ${rv} = ref(); const _b = ${bodyExpr}; ${rv}.define(_b); return _b })()`;
6043
+ }
6044
+ /** Emit the constructor-call source for `c` (never a const shortcut for `c` itself). */
6045
+ expr(c, selfOf) {
6046
+ const def = c._def;
6047
+ const kid = (x) => this.ref(x, selfOf);
6048
+ switch (def.tag) {
6049
+ case "lazy": {
6050
+ if (ruleNameOf(c) !== void 0) return `g[${JSON.stringify(ruleNameOf(c))}]`;
6051
+ const target = lazyTarget(c);
6052
+ if (selfOf && target === selfOf.comb) return selfOf.var;
6053
+ return this.ref(target, selfOf);
6054
+ }
6055
+ case "literal":
6056
+ return `literal(${JSON.stringify(def.value)}${def.caseInsensitive ? ", { caseInsensitive: true }" : ""})`;
6057
+ case "regex":
6058
+ return `regex(${JSON.stringify(def.source)}, ${JSON.stringify(def.flags)})`;
6059
+ case "keywords":
6060
+ return `keywords(${JSON.stringify(def.words)}, { caseInsensitive: ${def.caseInsensitive}${def.boundary !== void 0 ? `, boundary: ${JSON.stringify(def.boundary)}` : ""} })`;
6061
+ case "sequence":
6062
+ return `sequence(${def.parsers.map(kid).join(", ")})`;
6063
+ case "choice": {
6064
+ if (def.gates.some((g) => g !== null)) throw new Unserializable("choice with a gate() has no source");
6065
+ return `choice(${def.parsers.map(kid).join(", ")})`;
6066
+ }
6067
+ case "many":
6068
+ return `many(${kid(def.parser)})`;
6069
+ case "oneOrMore":
6070
+ return `oneOrMore(${kid(def.parser)})`;
6071
+ case "optional":
6072
+ return `optional(${kid(def.parser)})`;
6073
+ case "sepBy":
6074
+ return `sepBy(${kid(def.parser)}, ${kid(def.separator)})`;
6075
+ case "not":
6076
+ return `not(${kid(def.parser)})`;
6077
+ case "trivia":
6078
+ return `trivia(${kid(def.parser)})`;
6079
+ case "token":
6080
+ return `token(${kid(def.parser)})`;
6081
+ case "label":
6082
+ return `label(${JSON.stringify(def.label)}, ${kid(def.parser)})`;
6083
+ case "field":
6084
+ return `field(${JSON.stringify(def.name)}, ${kid(def.parser)})`;
6085
+ case "expect":
6086
+ return `expect(${kid(def.parser)}${def.label !== void 0 ? `, ${JSON.stringify(def.label)}` : ""})`;
6087
+ case "skip":
6088
+ return `skip(${kid(def.main)}, ${kid(def.skipped)})`;
6089
+ case "scanTo":
6090
+ return `scanTo(${kid(def.sentinel)}, { skip: [${def.skip.map(kid).join(", ")}], orEOF: ${def.orEOF} })`;
6091
+ case "transform": {
6092
+ if (def.fnSrc === void 0) throw new Unserializable("transform without fnSrc");
6093
+ return `_tf(${kid(def.parser)}, ${JSON.stringify(def.fnSrc)})`;
6094
+ }
6095
+ case "node": {
6096
+ if (def.build !== void 0 && def.buildSrc === void 0) throw new Unserializable("node build without buildSrc");
6097
+ const opts = def.unwrap || def.collapse ? `, { ${def.unwrap ? "unwrap: true" : "collapse: true"} }` : "";
6098
+ if (def.type === void 0) {
6099
+ if (def.buildSrc !== void 0) throw new Unserializable("inferred node build without inferred type");
6100
+ return opts ? `node(${kid(def.parser)}, undefined${opts})` : `node(${kid(def.parser)})`;
6101
+ }
6102
+ if (def.buildSrc !== void 0) return `_nd(${JSON.stringify(def.type)}, ${kid(def.parser)}, ${JSON.stringify(def.buildSrc)}${opts})`;
6103
+ return opts ? `node(${JSON.stringify(def.type)}, ${kid(def.parser)}, undefined${opts})` : `node(${JSON.stringify(def.type)}, ${kid(def.parser)})`;
6104
+ }
6105
+ case "grammar": {
6106
+ const trivia2 = def.clearTrivia ? "null" : def.triviaParser ? kid(def.triviaParser) : "undefined";
6107
+ return `parser({ trivia: ${trivia2}${def.trackLines ? ", trackLines: true" : ""} }, ${kid(def.parser)})`;
6108
+ }
6109
+ case "guard":
6110
+ case "withCtx":
6111
+ case "recover":
6112
+ case "unknown":
6113
+ throw new Unserializable(`unsupported tag ${def.tag}`);
6114
+ }
6115
+ }
6116
+ };
5894
6117
 
5895
6118
  // src/compiler/linker.ts
5896
6119
  var _nsCounter = 0;
5897
- function linkable(rulesMap, ns) {
5898
- const pieces = compileLinkable([...Object.entries(rulesMap)], ns ?? `_lk${_nsCounter++}_`);
6120
+ function linkable(rulesMap, ns, trivia2) {
6121
+ const pieces = compileLinkable(Object.entries(rulesMap), ns ?? `_lk${_nsCounter++}_`, trivia2 ? { trivia: trivia2 } : void 0);
5899
6122
  if (!pieces) throw new Error("linkable(): this grammar cannot be compiled to a linkable artifact (contains a runtime-only parser fallback)");
5900
6123
  return pieces;
5901
6124
  }
@@ -5919,28 +6142,6 @@ function cstBuildHost(typeOrOptions, children, _fields, span, rawChildren, trivi
5919
6142
  if (collapse) host._parsemanCstCollapse = collapse;
5920
6143
  return host;
5921
6144
  }
5922
- function pick(grammar, names) {
5923
- const p = grammar.ruleFns instanceof Map ? grammar : linkable(grammar);
5924
- const keep = /* @__PURE__ */ new Set();
5925
- const has = new Set(p.keys);
5926
- for (const n of names) {
5927
- if (!has.has(n)) throw new Error(`pick: rule "${n}" is not in this grammar (available: ${p.keys.join(", ")})`);
5928
- }
5929
- const visit = (n) => {
5930
- if (keep.has(n) || !has.has(n)) return;
5931
- keep.add(n);
5932
- for (const d of p.deps.get(n) ?? []) visit(d);
5933
- };
5934
- for (const n of names) visit(n);
5935
- const filt = (m) => new Map([...m].filter(([k]) => keep.has(k)));
5936
- return {
5937
- ...p,
5938
- keys: [...keep],
5939
- ruleFns: filt(p.ruleFns),
5940
- wrappers: filt(p.wrappers),
5941
- deps: filt(p.deps)
5942
- };
5943
- }
5944
6145
  function fusedBody(pieces) {
5945
6146
  const winner = /* @__PURE__ */ new Map();
5946
6147
  for (const p of pieces) for (const k of p.keys) winner.set(k, p);
@@ -5960,7 +6161,7 @@ function fusedBody(pieces) {
5960
6161
  ...needsEmptyTl ? ["const _EMPTY_TL = Object.freeze([])"] : [],
5961
6162
  ...needsHostReads ? [HOST_READS_DECL] : [],
5962
6163
  // Each contributing artifact's namespaced private prelude (regexes, _pf, …).
5963
- ...[...new Set(contributingPieces.flatMap((p) => p.prelude))],
6164
+ ...new Set(contributingPieces.flatMap((p) => p.prelude)),
5964
6165
  // The winning `_r_<Name>` function for each rule (one per name → no redeclare).
5965
6166
  ...[...winner].map(([k, p]) => p.ruleFns.get(k))
5966
6167
  ];
@@ -5994,12 +6195,13 @@ function fuseRules(pieces) {
5994
6195
  return new Function("_env", body)(env);
5995
6196
  }
5996
6197
  var COMPOSED_PIECES = /* @__PURE__ */ Symbol.for("parseman.composedPieces");
6198
+ var COMPOSED_TRIVIA = /* @__PURE__ */ Symbol.for("parseman.composedTrivia");
5997
6199
  function isIRPiece(p) {
5998
6200
  return !!p && typeof p === "object" && typeof p.ir === "string" && typeof p.ns === "string" && !("ruleFns" in p);
5999
6201
  }
6000
- function materializePiece(p) {
6202
+ function materializePiece(p, trivia2) {
6001
6203
  if (!isIRPiece(p)) return p;
6002
- const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns);
6204
+ const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns, trivia2 ? { trivia: trivia2 } : void 0);
6003
6205
  if (!pieces) throw new Error(`compose: carried IR for ns "${p.ns}" could not be re-lowered`);
6004
6206
  return pieces;
6005
6207
  }
@@ -6011,17 +6213,52 @@ function nextComposeNs(used) {
6011
6213
  used.add(ns);
6012
6214
  return ns;
6013
6215
  }
6014
- function itemPieces(item, used) {
6216
+ function itemCarried(item, used, trivia2) {
6015
6217
  const carried = item[COMPOSED_PIECES];
6016
- const pieces = Array.isArray(carried) ? carried.map(materializePiece) : item.ruleFns instanceof Map ? [item] : [linkable(item, nextComposeNs(used))];
6017
- for (const p of pieces) used.add(p.ns);
6018
- return pieces;
6218
+ if (Array.isArray(carried)) {
6219
+ const items = carried;
6220
+ for (const p of items) used.add(p.ns);
6221
+ return items;
6222
+ }
6223
+ if (item.ruleFns instanceof Map) {
6224
+ const p = item;
6225
+ used.add(p.ns);
6226
+ return [p];
6227
+ }
6228
+ const map = item;
6229
+ const ns = nextComposeNs(used);
6230
+ const entries = Object.entries(map).filter(([, val]) => {
6231
+ const d = val._def;
6232
+ if (d.tag !== "lazy") return true;
6233
+ try {
6234
+ d.thunk();
6235
+ return true;
6236
+ } catch {
6237
+ return false;
6238
+ }
6239
+ });
6240
+ const ir = serializeRuleMap(entries);
6241
+ return ir ? [{ ns, ir }] : [linkable(map, ns, trivia2)];
6242
+ }
6243
+ function composingTriviaOf(items) {
6244
+ for (let i = items.length - 1; i >= 0; i--) {
6245
+ const item = items[i];
6246
+ if (!item || item.ruleFns instanceof Map || item[COMPOSED_PIECES]) continue;
6247
+ for (const v2 of Object.values(item)) {
6248
+ const t = v2?._meta?.grammarTrivia;
6249
+ if (t) return t;
6250
+ }
6251
+ }
6252
+ return void 0;
6019
6253
  }
6020
6254
  function compose(items) {
6021
6255
  const used = /* @__PURE__ */ new Set();
6022
- const pieces = items.flatMap((item) => itemPieces(item, used));
6256
+ const trivia2 = composingTriviaOf(items);
6257
+ const carried = items.flatMap((item) => itemCarried(item, used, trivia2));
6258
+ const pieces = carried.map((p) => materializePiece(p, trivia2));
6023
6259
  const map = fuseRules(pieces);
6024
- Object.defineProperty(map, COMPOSED_PIECES, { value: pieces, enumerable: false });
6260
+ Object.defineProperty(map, COMPOSED_PIECES, { value: carried, enumerable: false });
6261
+ if (trivia2) Object.defineProperty(map, COMPOSED_TRIVIA, { value: trivia2, enumerable: false });
6025
6262
  return map;
6026
6263
  }
6027
6264