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.
@@ -6325,6 +6325,34 @@ function evalRuleMapIR(ir) {
6325
6325
  n._def.buildSrc = src;
6326
6326
  return n;
6327
6327
  };
6328
+ const _gch = (items) => {
6329
+ const gateSrcs = [];
6330
+ const arms = items.map((it) => {
6331
+ if (Array.isArray(it)) {
6332
+ const [src, comb] = it;
6333
+ gateSrcs.push(src);
6334
+ let gate;
6335
+ try {
6336
+ gate = (0, eval)(`(${src})`);
6337
+ } catch {
6338
+ gate = () => {
6339
+ throw new Error("IR gate fn not materialized");
6340
+ };
6341
+ }
6342
+ return { gate, combinator: comb };
6343
+ }
6344
+ gateSrcs.push(null);
6345
+ return it;
6346
+ });
6347
+ const c = choice(...arms);
6348
+ const gates = c._def.gates;
6349
+ if (gates && gates.length !== gateSrcs.length) {
6350
+ throw new Error(`_gch: gateSrcs length ${gateSrcs.length} != choice gates length ${gates.length}`);
6351
+ }
6352
+ ;
6353
+ c._def.gateSrcs = gateSrcs;
6354
+ return c;
6355
+ };
6328
6356
  const fn = new Function(
6329
6357
  "rules",
6330
6358
  "ref",
@@ -6350,6 +6378,7 @@ function evalRuleMapIR(ir) {
6350
6378
  "expect",
6351
6379
  "_tf",
6352
6380
  "_nd",
6381
+ "_gch",
6353
6382
  `return (${ir})`
6354
6383
  );
6355
6384
  const map = fn(
@@ -6376,7 +6405,8 @@ function evalRuleMapIR(ir) {
6376
6405
  field,
6377
6406
  expect,
6378
6407
  _tf,
6379
- _nd
6408
+ _nd,
6409
+ _gch
6380
6410
  );
6381
6411
  return Object.entries(map);
6382
6412
  }
@@ -6488,8 +6518,16 @@ ${body}
6488
6518
  case "sequence":
6489
6519
  return `sequence(${def.parsers.map(kid).join(", ")})`;
6490
6520
  case "choice": {
6491
- if (def.gates.some((g) => g !== null)) throw new Unserializable("choice with a gate() has no source");
6492
- return `choice(${def.parsers.map(kid).join(", ")})`;
6521
+ const gates = def.gates;
6522
+ if (gates.every((g) => g === null)) return `choice(${def.parsers.map(kid).join(", ")})`;
6523
+ const gateSrcs = def.gateSrcs;
6524
+ const items = def.parsers.map((p, i) => {
6525
+ if (gates[i] === null) return kid(p);
6526
+ const src = gateSrcs?.[i];
6527
+ if (src == null) throw new Unserializable("choice gate() has no captured source");
6528
+ return `[${JSON.stringify(src)}, ${kid(p)}]`;
6529
+ });
6530
+ return `_gch([${items.join(", ")}])`;
6493
6531
  }
6494
6532
  case "many":
6495
6533
  return `many(${kid(def.parser)})`;
@@ -6670,6 +6708,48 @@ var { isFinite } = Number;
6670
6708
  registerRegexAnalyzer(firstSetFromRegex);
6671
6709
 
6672
6710
  // src/plugin/evaluator.ts
6711
+ function stripTsFromSource(node2, code) {
6712
+ const cuts = [];
6713
+ const walk2 = (n) => {
6714
+ if (!n || typeof n !== "object") return;
6715
+ const rec = n;
6716
+ if (typeof rec.type === "string" && rec.type.startsWith("TS") && typeof rec.start === "number" && typeof rec.end === "number") {
6717
+ const ex = rec.expression;
6718
+ if ((rec.type === "TSAsExpression" || rec.type === "TSSatisfiesExpression" || rec.type === "TSNonNullExpression" || rec.type === "TSInstantiationExpression") && ex && typeof ex.end === "number") {
6719
+ cuts.push([ex.end, rec.end]);
6720
+ walk2(rec.expression);
6721
+ return;
6722
+ }
6723
+ if (rec.type === "TSTypeAssertion" && ex && typeof ex.start === "number") {
6724
+ cuts.push([rec.start, ex.start]);
6725
+ walk2(rec.expression);
6726
+ return;
6727
+ }
6728
+ cuts.push([rec.start, rec.end]);
6729
+ return;
6730
+ }
6731
+ for (const key of Object.keys(rec)) {
6732
+ if (key === "type" || key === "start" || key === "end") continue;
6733
+ const v2 = rec[key];
6734
+ if (Array.isArray(v2)) {
6735
+ for (const item of v2) walk2(item);
6736
+ } else if (v2 && typeof v2 === "object") walk2(v2);
6737
+ }
6738
+ };
6739
+ walk2(node2);
6740
+ const start = node2.start;
6741
+ const end = node2.end;
6742
+ if (cuts.length === 0) return code.slice(start, end);
6743
+ cuts.sort((a, b) => a[0] - b[0]);
6744
+ let out = "";
6745
+ let cur = start;
6746
+ for (const [s, e] of cuts) {
6747
+ if (s < cur) continue;
6748
+ out += code.slice(cur, s);
6749
+ cur = e;
6750
+ }
6751
+ return out + code.slice(cur, end);
6752
+ }
6673
6753
  function isWordFactory(v2) {
6674
6754
  return !!v2 && typeof v2 === "object" && v2.tag === "wordFactory";
6675
6755
  }
@@ -6945,7 +7025,7 @@ function exprToCombi(node2, scope, code, mfs) {
6945
7025
  if (isGatedArmExpr(argNode)) {
6946
7026
  const parts = gatedArmParts(argNode);
6947
7027
  if (!parts) return null;
6948
- const gateSrc = code.slice(parts.gate.start, parts.gate.end);
7028
+ const gateSrc = stripTsFromSource(parts.gate, code);
6949
7029
  mfs.push(gateSrc);
6950
7030
  const combi = anyValue(parts.combinator, scope, code, mfs);
6951
7031
  if (!isCombinator(combi)) return null;