parseman 0.54.0 → 0.55.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.
@@ -1,13 +1,28 @@
1
1
  import type { Combinator } from '../types.ts';
2
2
  /**
3
- * Runs `combinator` with `ctx.state` set to `extra` for the duration of the parse.
4
- * The outer user context is restored on exit (lexical scoping).
3
+ * Runs `combinator` under a scoped `ctx.state`, restored on exit (lexical scoping).
5
4
  *
6
- * const functionBody = withCtx({ inFn: true },
7
- * sequence(literal('{'), many(statement), literal('}'))
8
- * )
5
+ * Two forms, told apart by whether the first argument is a function:
9
6
  *
10
- * Read back with guard() or from within a transform's span argument.
7
+ * - **Replace** — `withCtx(extra, c)`: the subtree's state IS `extra`.
8
+ *
9
+ * const functionBody = withCtx({ inFn: true },
10
+ * sequence(literal('{'), many(statement), literal('}'))
11
+ * )
12
+ *
13
+ * - **Derive** — `withCtx(state => next, c)`: the subtree's state is computed
14
+ * FROM the state in force at entry, once per entry. Use it to add or override
15
+ * a field while every outer field (options, document-wide sinks) stays visible
16
+ * to the gates and reducers inside:
17
+ *
18
+ * const argument = withCtx(s => ({ ...s, stop: 'comparison' }), value)
19
+ *
20
+ * Because a function argument always selects the derive form, a state that is
21
+ * itself a function cannot be installed with the replace form; wrap it
22
+ * (`withCtx(() => fn, c)`).
23
+ *
24
+ * Read back with gate() or from within a transform's span argument.
11
25
  */
12
- export declare function withCtx<U, T>(extra: U, combinator: Combinator<T>): Combinator<T>;
26
+ export declare function withCtx<S = Readonly<Record<string, unknown>> | undefined, U = unknown, T = unknown>(derive: (state: S) => U, combinator: Combinator<T>): Combinator<T>;
27
+ export declare function withCtx<U, T>(extra: U extends (...args: never[]) => unknown ? never : U, combinator: Combinator<T>): Combinator<T>;
13
28
  //# sourceMappingURL=withCtx.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withCtx.d.ts","sourceRoot":"","sources":["../../src/combinators/withCtx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAA;AAEpF;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAgChF"}
1
+ {"version":3,"file":"withCtx.d.ts","sourceRoot":"","sources":["../../src/combinators/withCtx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAA;AAEpF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAIH,wBAAgB,OAAO,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;AAKvK,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,OAAO,GAAG,KAAK,GAAG,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA"}
package/dist/index.js CHANGED
@@ -8277,6 +8277,9 @@ var Encoder = class {
8277
8277
  this.expected([adjacencyExpected(d)])
8278
8278
  );
8279
8279
  case "withCtx": {
8280
+ if (typeof d.extra === "function") {
8281
+ return this.emit(OP_WITHCTX, ~this.fn(d.extra, d.extraSrc ?? null), this.node(d.parser).ip);
8282
+ }
8280
8283
  if (!emittableConst(d.extra)) {
8281
8284
  this.runtimeOnly.add("withCtx(extra) \u2014 the state object is not serialisable");
8282
8285
  }
@@ -13233,8 +13236,21 @@ function assemble(t, prog, cfg) {
13233
13236
  };
13234
13237
  }
13235
13238
  case OP_WITHCTX: {
13236
- const extra = k[code[ip + 1]];
13239
+ const ki = code[ip + 1];
13237
13240
  const child = link(code[ip + 2]);
13241
+ if (ki < 0) {
13242
+ const derive2 = fns[~ki];
13243
+ return (input, pos, ctx) => {
13244
+ const saved = ctx.state;
13245
+ ctx.state = derive2(saved);
13246
+ try {
13247
+ return child(input, pos, ctx);
13248
+ } finally {
13249
+ ctx.state = saved;
13250
+ }
13251
+ };
13252
+ }
13253
+ const extra = k[ki];
13238
13254
  return (input, pos, ctx) => {
13239
13255
  const saved = ctx.state;
13240
13256
  ctx.state = extra;
@@ -17725,7 +17741,7 @@ function fieldOptionsSrc(def) {
17725
17741
  }
17726
17742
 
17727
17743
  // src/version.ts
17728
- var PARSEMAN_VERSION = "0.54.0";
17744
+ var PARSEMAN_VERSION = "0.55.0";
17729
17745
 
17730
17746
  // src/compiler/compile-linkable-table.ts
17731
17747
  function isLocal(rule) {
@@ -19313,19 +19329,28 @@ function withCtx(extra, combinator) {
19313
19329
  canMatchNewline: combinator._meta.canMatchNewline,
19314
19330
  isTrivia: false
19315
19331
  };
19332
+ const parse2 = typeof extra === "function" ? (input, pos, ctx) => {
19333
+ const saved = ctx.state;
19334
+ ctx.state = extra(saved);
19335
+ try {
19336
+ return combinator.parse(input, pos, ctx);
19337
+ } finally {
19338
+ ctx.state = saved;
19339
+ }
19340
+ } : (input, pos, ctx) => {
19341
+ const saved = ctx.state;
19342
+ ctx.state = extra;
19343
+ try {
19344
+ return combinator.parse(input, pos, ctx);
19345
+ } finally {
19346
+ ctx.state = saved;
19347
+ }
19348
+ };
19316
19349
  return {
19317
19350
  _tag: "withCtx",
19318
19351
  _meta: meta,
19319
19352
  _def: { tag: "withCtx", extra, parser: combinator },
19320
- parse(input, pos, ctx) {
19321
- const saved = ctx.state;
19322
- ctx.state = extra;
19323
- try {
19324
- return combinator.parse(input, pos, ctx);
19325
- } finally {
19326
- ctx.state = saved;
19327
- }
19328
- }
19353
+ parse: parse2
19329
19354
  };
19330
19355
  }
19331
19356