parseman 0.26.1 → 0.26.3

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.
@@ -5530,7 +5530,7 @@ function emitDispatch(p, ctx, pos) {
5530
5530
  ctx.indent = 1;
5531
5531
  ctx.failLabel = "_pfail";
5532
5532
  ctx.recordFail = true;
5533
- const innerR = emit(innerParser, ctx, "_pos");
5533
+ const innerR = emitDispatch(innerParser, ctx, "_pos");
5534
5534
  ctx.indent = savedIndent;
5535
5535
  ctx.failLabel = savedFailLabel;
5536
5536
  ctx.recordFail = savedRecord;
@@ -6247,6 +6247,23 @@ function escapeClassChar(ch) {
6247
6247
  return ch.replace(/[\\\]^-]/g, "\\$&");
6248
6248
  }
6249
6249
 
6250
+ // src/combinators/withCtx.ts
6251
+ function withCtx(extra, combinator) {
6252
+ const meta = {
6253
+ firstSet: combinator._meta.firstSet,
6254
+ canMatchNewline: combinator._meta.canMatchNewline,
6255
+ isTrivia: false
6256
+ };
6257
+ return {
6258
+ _tag: "withCtx",
6259
+ _meta: meta,
6260
+ _def: { tag: "withCtx", extra, parser: combinator },
6261
+ parse(input, pos, ctx) {
6262
+ return combinator.parse(input, pos, { ...ctx, state: extra });
6263
+ }
6264
+ };
6265
+ }
6266
+
6250
6267
  // src/compiler/ir-serialize.ts
6251
6268
  function childrenOf2(def) {
6252
6269
  switch (def.tag) {
@@ -6263,6 +6280,7 @@ function childrenOf2(def) {
6263
6280
  case "field":
6264
6281
  case "not":
6265
6282
  case "node":
6283
+ case "withCtx":
6266
6284
  case "expect":
6267
6285
  return [def.parser];
6268
6286
  case "grammar":
@@ -6278,7 +6296,6 @@ function childrenOf2(def) {
6278
6296
  case "regex":
6279
6297
  case "keywords":
6280
6298
  case "guard":
6281
- case "withCtx":
6282
6299
  case "recover":
6283
6300
  case "unknown":
6284
6301
  return [];
@@ -6325,6 +6342,45 @@ function evalRuleMapIR(ir) {
6325
6342
  n._def.buildSrc = src;
6326
6343
  return n;
6327
6344
  };
6345
+ const _gch = (items) => {
6346
+ const gateSrcs = [];
6347
+ const arms = items.map((it) => {
6348
+ if (Array.isArray(it)) {
6349
+ const [src, comb] = it;
6350
+ gateSrcs.push(src);
6351
+ let gate;
6352
+ try {
6353
+ gate = (0, eval)(`(${src})`);
6354
+ } catch {
6355
+ gate = () => {
6356
+ throw new Error("IR gate fn not materialized");
6357
+ };
6358
+ }
6359
+ return { gate, combinator: comb };
6360
+ }
6361
+ gateSrcs.push(null);
6362
+ return it;
6363
+ });
6364
+ const c = choice(...arms);
6365
+ const gates = c._def.gates;
6366
+ if (gates && gates.length !== gateSrcs.length) {
6367
+ throw new Error(`_gch: gateSrcs length ${gateSrcs.length} != choice gates length ${gates.length}`);
6368
+ }
6369
+ ;
6370
+ c._def.gateSrcs = gateSrcs;
6371
+ return c;
6372
+ };
6373
+ const _wc = (src, inner) => {
6374
+ let extra;
6375
+ try {
6376
+ extra = (0, eval)(`(${src})`);
6377
+ } catch {
6378
+ extra = void 0;
6379
+ }
6380
+ const w = withCtx(extra, inner);
6381
+ w._def.extraSrc = src;
6382
+ return w;
6383
+ };
6328
6384
  const fn = new Function(
6329
6385
  "rules",
6330
6386
  "ref",
@@ -6350,6 +6406,8 @@ function evalRuleMapIR(ir) {
6350
6406
  "expect",
6351
6407
  "_tf",
6352
6408
  "_nd",
6409
+ "_gch",
6410
+ "_wc",
6353
6411
  `return (${ir})`
6354
6412
  );
6355
6413
  const map = fn(
@@ -6376,7 +6434,9 @@ function evalRuleMapIR(ir) {
6376
6434
  field,
6377
6435
  expect,
6378
6436
  _tf,
6379
- _nd
6437
+ _nd,
6438
+ _gch,
6439
+ _wc
6380
6440
  );
6381
6441
  return Object.entries(map);
6382
6442
  }
@@ -6432,7 +6492,7 @@ ${body}
6432
6492
  this.analyze(target, active);
6433
6493
  return;
6434
6494
  }
6435
- if (def.tag === "guard" || def.tag === "withCtx" || def.tag === "recover" || def.tag === "unknown") {
6495
+ if (def.tag === "guard" || def.tag === "recover" || def.tag === "unknown") {
6436
6496
  throw new Unserializable(`unsupported tag ${def.tag}`);
6437
6497
  }
6438
6498
  this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
@@ -6488,8 +6548,16 @@ ${body}
6488
6548
  case "sequence":
6489
6549
  return `sequence(${def.parsers.map(kid).join(", ")})`;
6490
6550
  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(", ")})`;
6551
+ const gates = def.gates;
6552
+ if (gates.every((g) => g === null)) return `choice(${def.parsers.map(kid).join(", ")})`;
6553
+ const gateSrcs = def.gateSrcs;
6554
+ const items = def.parsers.map((p, i) => {
6555
+ if (gates[i] === null) return kid(p);
6556
+ const src = gateSrcs?.[i];
6557
+ if (src == null) throw new Unserializable("choice gate() has no captured source");
6558
+ return `[${JSON.stringify(src)}, ${kid(p)}]`;
6559
+ });
6560
+ return `_gch([${items.join(", ")}])`;
6493
6561
  }
6494
6562
  case "many":
6495
6563
  return `many(${kid(def.parser)})`;
@@ -6533,8 +6601,11 @@ ${body}
6533
6601
  const trivia2 = def.clearTrivia ? "null" : def.triviaParser ? kid(def.triviaParser) : "undefined";
6534
6602
  return `parser({ trivia: ${trivia2}${def.trackLines ? ", trackLines: true" : ""} }, ${kid(def.parser)})`;
6535
6603
  }
6604
+ case "withCtx": {
6605
+ if (def.extraSrc === void 0) throw new Unserializable("withCtx without extraSrc");
6606
+ return `_wc(${JSON.stringify(def.extraSrc)}, ${kid(def.parser)})`;
6607
+ }
6536
6608
  case "guard":
6537
- case "withCtx":
6538
6609
  case "recover":
6539
6610
  case "unknown":
6540
6611
  throw new Unserializable(`unsupported tag ${def.tag}`);
@@ -6646,23 +6717,6 @@ function guard(predicate) {
6646
6717
  };
6647
6718
  }
6648
6719
 
6649
- // src/combinators/withCtx.ts
6650
- function withCtx(extra, combinator) {
6651
- const meta = {
6652
- firstSet: combinator._meta.firstSet,
6653
- canMatchNewline: combinator._meta.canMatchNewline,
6654
- isTrivia: false
6655
- };
6656
- return {
6657
- _tag: "withCtx",
6658
- _meta: meta,
6659
- _def: { tag: "withCtx", extra, parser: combinator },
6660
- parse(input, pos, ctx) {
6661
- return combinator.parse(input, pos, { ...ctx, state: extra });
6662
- }
6663
- };
6664
- }
6665
-
6666
6720
  // src/cst/offset-model.ts
6667
6721
  var { isFinite } = Number;
6668
6722
 
@@ -6670,6 +6724,48 @@ var { isFinite } = Number;
6670
6724
  registerRegexAnalyzer(firstSetFromRegex);
6671
6725
 
6672
6726
  // src/plugin/evaluator.ts
6727
+ function stripTsFromSource(node2, code) {
6728
+ const cuts = [];
6729
+ const walk2 = (n) => {
6730
+ if (!n || typeof n !== "object") return;
6731
+ const rec = n;
6732
+ if (typeof rec.type === "string" && rec.type.startsWith("TS") && typeof rec.start === "number" && typeof rec.end === "number") {
6733
+ const ex = rec.expression;
6734
+ if ((rec.type === "TSAsExpression" || rec.type === "TSSatisfiesExpression" || rec.type === "TSNonNullExpression" || rec.type === "TSInstantiationExpression") && ex && typeof ex.end === "number") {
6735
+ cuts.push([ex.end, rec.end]);
6736
+ walk2(rec.expression);
6737
+ return;
6738
+ }
6739
+ if (rec.type === "TSTypeAssertion" && ex && typeof ex.start === "number") {
6740
+ cuts.push([rec.start, ex.start]);
6741
+ walk2(rec.expression);
6742
+ return;
6743
+ }
6744
+ cuts.push([rec.start, rec.end]);
6745
+ return;
6746
+ }
6747
+ for (const key of Object.keys(rec)) {
6748
+ if (key === "type" || key === "start" || key === "end") continue;
6749
+ const v2 = rec[key];
6750
+ if (Array.isArray(v2)) {
6751
+ for (const item of v2) walk2(item);
6752
+ } else if (v2 && typeof v2 === "object") walk2(v2);
6753
+ }
6754
+ };
6755
+ walk2(node2);
6756
+ const start = node2.start;
6757
+ const end = node2.end;
6758
+ if (cuts.length === 0) return code.slice(start, end);
6759
+ cuts.sort((a, b) => a[0] - b[0]);
6760
+ let out = "";
6761
+ let cur = start;
6762
+ for (const [s, e] of cuts) {
6763
+ if (s < cur) continue;
6764
+ out += code.slice(cur, s);
6765
+ cur = e;
6766
+ }
6767
+ return out + code.slice(cur, end);
6768
+ }
6673
6769
  function isWordFactory(v2) {
6674
6770
  return !!v2 && typeof v2 === "object" && v2.tag === "wordFactory";
6675
6771
  }
@@ -6945,7 +7041,7 @@ function exprToCombi(node2, scope, code, mfs) {
6945
7041
  if (isGatedArmExpr(argNode)) {
6946
7042
  const parts = gatedArmParts(argNode);
6947
7043
  if (!parts) return null;
6948
- const gateSrc = code.slice(parts.gate.start, parts.gate.end);
7044
+ const gateSrc = stripTsFromSource(parts.gate, code);
6949
7045
  mfs.push(gateSrc);
6950
7046
  const combi = anyValue(parts.combinator, scope, code, mfs);
6951
7047
  if (!isCombinator(combi)) return null;