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.
package/dist/index.js CHANGED
@@ -5547,7 +5547,7 @@ function emitDispatch(p, ctx, pos) {
5547
5547
  ctx.indent = 1;
5548
5548
  ctx.failLabel = "_pfail";
5549
5549
  ctx.recordFail = true;
5550
- const innerR = emit(innerParser, ctx, "_pos");
5550
+ const innerR = emitDispatch(innerParser, ctx, "_pos");
5551
5551
  ctx.indent = savedIndent;
5552
5552
  ctx.failLabel = savedFailLabel;
5553
5553
  ctx.recordFail = savedRecord;
@@ -6179,6 +6179,23 @@ function escapeClassChar(ch) {
6179
6179
  return ch.replace(/[\\\]^-]/g, "\\$&");
6180
6180
  }
6181
6181
 
6182
+ // src/combinators/withCtx.ts
6183
+ function withCtx(extra, combinator) {
6184
+ const meta = {
6185
+ firstSet: combinator._meta.firstSet,
6186
+ canMatchNewline: combinator._meta.canMatchNewline,
6187
+ isTrivia: false
6188
+ };
6189
+ return {
6190
+ _tag: "withCtx",
6191
+ _meta: meta,
6192
+ _def: { tag: "withCtx", extra, parser: combinator },
6193
+ parse(input, pos, ctx) {
6194
+ return combinator.parse(input, pos, { ...ctx, state: extra });
6195
+ }
6196
+ };
6197
+ }
6198
+
6182
6199
  // src/compiler/ir-serialize.ts
6183
6200
  function childrenOf2(def) {
6184
6201
  switch (def.tag) {
@@ -6195,6 +6212,7 @@ function childrenOf2(def) {
6195
6212
  case "field":
6196
6213
  case "not":
6197
6214
  case "node":
6215
+ case "withCtx":
6198
6216
  case "expect":
6199
6217
  return [def.parser];
6200
6218
  case "grammar":
@@ -6210,7 +6228,6 @@ function childrenOf2(def) {
6210
6228
  case "regex":
6211
6229
  case "keywords":
6212
6230
  case "guard":
6213
- case "withCtx":
6214
6231
  case "recover":
6215
6232
  case "unknown":
6216
6233
  return [];
@@ -6257,6 +6274,45 @@ function evalRuleMapIR(ir) {
6257
6274
  n._def.buildSrc = src;
6258
6275
  return n;
6259
6276
  };
6277
+ const _gch = (items) => {
6278
+ const gateSrcs = [];
6279
+ const arms = items.map((it) => {
6280
+ if (Array.isArray(it)) {
6281
+ const [src, comb] = it;
6282
+ gateSrcs.push(src);
6283
+ let gate;
6284
+ try {
6285
+ gate = (0, eval)(`(${src})`);
6286
+ } catch {
6287
+ gate = () => {
6288
+ throw new Error("IR gate fn not materialized");
6289
+ };
6290
+ }
6291
+ return { gate, combinator: comb };
6292
+ }
6293
+ gateSrcs.push(null);
6294
+ return it;
6295
+ });
6296
+ const c = choice(...arms);
6297
+ const gates = c._def.gates;
6298
+ if (gates && gates.length !== gateSrcs.length) {
6299
+ throw new Error(`_gch: gateSrcs length ${gateSrcs.length} != choice gates length ${gates.length}`);
6300
+ }
6301
+ ;
6302
+ c._def.gateSrcs = gateSrcs;
6303
+ return c;
6304
+ };
6305
+ const _wc = (src, inner) => {
6306
+ let extra;
6307
+ try {
6308
+ extra = (0, eval)(`(${src})`);
6309
+ } catch {
6310
+ extra = void 0;
6311
+ }
6312
+ const w = withCtx(extra, inner);
6313
+ w._def.extraSrc = src;
6314
+ return w;
6315
+ };
6260
6316
  const fn = new Function(
6261
6317
  "rules",
6262
6318
  "ref",
@@ -6282,6 +6338,8 @@ function evalRuleMapIR(ir) {
6282
6338
  "expect",
6283
6339
  "_tf",
6284
6340
  "_nd",
6341
+ "_gch",
6342
+ "_wc",
6285
6343
  `return (${ir})`
6286
6344
  );
6287
6345
  const map = fn(
@@ -6308,7 +6366,9 @@ function evalRuleMapIR(ir) {
6308
6366
  field,
6309
6367
  expect,
6310
6368
  _tf,
6311
- _nd
6369
+ _nd,
6370
+ _gch,
6371
+ _wc
6312
6372
  );
6313
6373
  return Object.entries(map);
6314
6374
  }
@@ -6364,7 +6424,7 @@ ${body}
6364
6424
  this.analyze(target, active);
6365
6425
  return;
6366
6426
  }
6367
- if (def.tag === "guard" || def.tag === "withCtx" || def.tag === "recover" || def.tag === "unknown") {
6427
+ if (def.tag === "guard" || def.tag === "recover" || def.tag === "unknown") {
6368
6428
  throw new Unserializable(`unsupported tag ${def.tag}`);
6369
6429
  }
6370
6430
  this.counts.set(c, (this.counts.get(c) ?? 0) + 1);
@@ -6420,8 +6480,16 @@ ${body}
6420
6480
  case "sequence":
6421
6481
  return `sequence(${def.parsers.map(kid).join(", ")})`;
6422
6482
  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(", ")})`;
6483
+ const gates = def.gates;
6484
+ if (gates.every((g) => g === null)) return `choice(${def.parsers.map(kid).join(", ")})`;
6485
+ const gateSrcs = def.gateSrcs;
6486
+ const items = def.parsers.map((p, i) => {
6487
+ if (gates[i] === null) return kid(p);
6488
+ const src = gateSrcs?.[i];
6489
+ if (src == null) throw new Unserializable("choice gate() has no captured source");
6490
+ return `[${JSON.stringify(src)}, ${kid(p)}]`;
6491
+ });
6492
+ return `_gch([${items.join(", ")}])`;
6425
6493
  }
6426
6494
  case "many":
6427
6495
  return `many(${kid(def.parser)})`;
@@ -6465,8 +6533,11 @@ ${body}
6465
6533
  const trivia2 = def.clearTrivia ? "null" : def.triviaParser ? kid(def.triviaParser) : "undefined";
6466
6534
  return `parser({ trivia: ${trivia2}${def.trackLines ? ", trackLines: true" : ""} }, ${kid(def.parser)})`;
6467
6535
  }
6536
+ case "withCtx": {
6537
+ if (def.extraSrc === void 0) throw new Unserializable("withCtx without extraSrc");
6538
+ return `_wc(${JSON.stringify(def.extraSrc)}, ${kid(def.parser)})`;
6539
+ }
6468
6540
  case "guard":
6469
- case "withCtx":
6470
6541
  case "recover":
6471
6542
  case "unknown":
6472
6543
  throw new Unserializable(`unsupported tag ${def.tag}`);
@@ -6640,23 +6711,6 @@ function guard(predicate) {
6640
6711
  };
6641
6712
  }
6642
6713
 
6643
- // src/combinators/withCtx.ts
6644
- function withCtx(extra, combinator) {
6645
- const meta = {
6646
- firstSet: combinator._meta.firstSet,
6647
- canMatchNewline: combinator._meta.canMatchNewline,
6648
- isTrivia: false
6649
- };
6650
- return {
6651
- _tag: "withCtx",
6652
- _meta: meta,
6653
- _def: { tag: "withCtx", extra, parser: combinator },
6654
- parse(input, pos, ctx) {
6655
- return combinator.parse(input, pos, { ...ctx, state: extra });
6656
- }
6657
- };
6658
- }
6659
-
6660
6714
  // src/combinators/completions.ts
6661
6715
  function completionsAt(target, input, offset, options = {}) {
6662
6716
  const probe = { offset, best: null };