@ptolemy2002/rgx 12.7.3 → 12.8.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.
@@ -9,6 +9,11 @@ export type RGXWalkerOptions<R, S = unknown> = {
9
9
  looping?: boolean;
10
10
  contiguous?: boolean;
11
11
  };
12
+ export type RGXTryWalkOptions = {
13
+ revertReduced?: boolean;
14
+ revertShare?: boolean;
15
+ revertCaptures?: boolean;
16
+ };
12
17
  export type RGXTokenOrPart<R, S = unknown, T = unknown> = RGXToken | RGXPart<R, S, T>;
13
18
  export type RGXWalkerStepDirective = "stop" | "skip" | "silent";
14
19
  export declare class RGXWalker<R, S = unknown> {
@@ -56,7 +61,7 @@ export declare class RGXWalker<R, S = unknown> {
56
61
  stepToToken(predicate: (token: RGXTokenOrPart<R>) => boolean): this;
57
62
  stepToPart(predicate?: (part: RGXPart<R, S, unknown>) => boolean): this;
58
63
  walk(): R;
59
- tryWalk(): boolean;
64
+ tryWalk({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions): boolean;
60
65
  clone(depth?: CloneDepth): RGXWalker<R, S>;
61
66
  }
62
67
  export declare function rgxWalker<R, S = unknown>(...args: ConstructorParameters<typeof RGXWalker<R, S>>): RGXWalker<R, S>;
@@ -104,7 +104,7 @@ class RGXWalker {
104
104
  }
105
105
  capture(token, includeMatch = false) {
106
106
  const regex = (0, utils_1.createRegex)((0, resolve_1.resolveRGXToken)(part_1.RGXPart.check(token) ? token.token : token));
107
- const args = [regex, this.source, this.sourcePosition, 10, true];
107
+ const args = [regex, this.source, Math.min(this.sourcePosition, this.source.length - 1), 10, true];
108
108
  let match;
109
109
  let endPosition;
110
110
  if (this.contiguous) {
@@ -247,6 +247,9 @@ class RGXWalker {
247
247
  silent = dir === "silent";
248
248
  }
249
249
  const branchedToken = isPart ? createBranchGroups(token.token) : createBranchGroups(token);
250
+ // Still track the previous source position,
251
+ // because if we have to skip, we need to reset to it.
252
+ const prevSourcePosition = this.sourcePosition;
250
253
  let captureAttempt;
251
254
  try {
252
255
  captureAttempt = this.attemptCapture(branchedToken, isPart ? token : null);
@@ -284,7 +287,7 @@ class RGXWalker {
284
287
  groups: captureAttempt.groups ?? null
285
288
  };
286
289
  if (isPart) {
287
- const dir = this.validateCapture(token, captureResult, start);
290
+ const dir = this.validateCapture(token, captureResult, prevSourcePosition);
288
291
  if (dir === "stop") {
289
292
  this._stopped = true;
290
293
  return null;
@@ -297,7 +300,7 @@ class RGXWalker {
297
300
  if (!silent)
298
301
  this.registerCapture(captureResult, token);
299
302
  if (isPart) {
300
- const dir = this.handleAfterCapture(token, captureResult, silent, start);
303
+ const dir = this.handleAfterCapture(token, captureResult, silent, prevSourcePosition);
301
304
  if (dir === "stop") {
302
305
  this.advanceToken();
303
306
  this._stopped = true;
@@ -338,9 +341,13 @@ class RGXWalker {
338
341
  this.stepToToken(() => false);
339
342
  return this.reduced;
340
343
  }
341
- tryWalk() {
344
+ tryWalk({ revertReduced = false, revertShare = false, revertCaptures = false } = {}) {
342
345
  const prevSourcePosition = this.sourcePosition;
343
346
  const prevTokenPosition = this.tokenPosition;
347
+ const prevReduced = revertReduced ? (0, immutability_utils_1.extClone)(this.reduced, "max") : this.reduced;
348
+ const prevShare = revertShare ? (0, immutability_utils_1.extClone)(this.share, "max") : this.share;
349
+ const prevCaptures = revertCaptures ? (0, immutability_utils_1.extClone)(this.captures, "max") : this.captures;
350
+ const prevNamedCaptures = revertCaptures ? (0, immutability_utils_1.extClone)(this.namedCaptures, "max") : this.namedCaptures;
344
351
  try {
345
352
  this.walk();
346
353
  return true;
@@ -348,6 +355,14 @@ class RGXWalker {
348
355
  catch (e) {
349
356
  this.sourcePosition = prevSourcePosition;
350
357
  this.tokenPosition = prevTokenPosition;
358
+ if (revertReduced)
359
+ this.reduced = prevReduced;
360
+ if (revertShare)
361
+ this.share = prevShare;
362
+ if (revertCaptures)
363
+ this.captures = prevCaptures;
364
+ if (revertCaptures)
365
+ this.namedCaptures = prevNamedCaptures;
351
366
  if (isMatchError(e)) {
352
367
  return false;
353
368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "12.7.3",
3
+ "version": "12.8.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",