parseman 0.26.1 → 0.26.2

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.
package/dist/index.js CHANGED
@@ -6257,6 +6257,34 @@ function evalRuleMapIR(ir) {
6257
6257
  n._def.buildSrc = src;
6258
6258
  return n;
6259
6259
  };
6260
+ const _gch = (items) => {
6261
+ const gateSrcs = [];
6262
+ const arms = items.map((it) => {
6263
+ if (Array.isArray(it)) {
6264
+ const [src, comb] = it;
6265
+ gateSrcs.push(src);
6266
+ let gate;
6267
+ try {
6268
+ gate = (0, eval)(`(${src})`);
6269
+ } catch {
6270
+ gate = () => {
6271
+ throw new Error("IR gate fn not materialized");
6272
+ };
6273
+ }
6274
+ return { gate, combinator: comb };
6275
+ }
6276
+ gateSrcs.push(null);
6277
+ return it;
6278
+ });
6279
+ const c = choice(...arms);
6280
+ const gates = c._def.gates;
6281
+ if (gates && gates.length !== gateSrcs.length) {
6282
+ throw new Error(`_gch: gateSrcs length ${gateSrcs.length} != choice gates length ${gates.length}`);
6283
+ }
6284
+ ;
6285
+ c._def.gateSrcs = gateSrcs;
6286
+ return c;
6287
+ };
6260
6288
  const fn = new Function(
6261
6289
  "rules",
6262
6290
  "ref",
@@ -6282,6 +6310,7 @@ function evalRuleMapIR(ir) {
6282
6310
  "expect",
6283
6311
  "_tf",
6284
6312
  "_nd",
6313
+ "_gch",
6285
6314
  `return (${ir})`
6286
6315
  );
6287
6316
  const map = fn(
@@ -6308,7 +6337,8 @@ function evalRuleMapIR(ir) {
6308
6337
  field,
6309
6338
  expect,
6310
6339
  _tf,
6311
- _nd
6340
+ _nd,
6341
+ _gch
6312
6342
  );
6313
6343
  return Object.entries(map);
6314
6344
  }
@@ -6420,8 +6450,16 @@ ${body}
6420
6450
  case "sequence":
6421
6451
  return `sequence(${def.parsers.map(kid).join(", ")})`;
6422
6452
  case "choice": {
6423
- if (def.gates.some((g) => g !== null)) throw new Unserializable("choice with a gate() has no source");
6424
- return `choice(${def.parsers.map(kid).join(", ")})`;
6453
+ const gates = def.gates;
6454
+ if (gates.every((g) => g === null)) return `choice(${def.parsers.map(kid).join(", ")})`;
6455
+ const gateSrcs = def.gateSrcs;
6456
+ const items = def.parsers.map((p, i) => {
6457
+ if (gates[i] === null) return kid(p);
6458
+ const src = gateSrcs?.[i];
6459
+ if (src == null) throw new Unserializable("choice gate() has no captured source");
6460
+ return `[${JSON.stringify(src)}, ${kid(p)}]`;
6461
+ });
6462
+ return `_gch([${items.join(", ")}])`;
6425
6463
  }
6426
6464
  case "many":
6427
6465
  return `many(${kid(def.parser)})`;