@ptolemy2002/rgx 13.5.0 → 13.7.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.
@@ -3,5 +3,6 @@ export type CreateRGXBoundsOptions = {
3
3
  flags?: string;
4
4
  anchorStart?: boolean;
5
5
  anchorEnd?: boolean;
6
+ inner?: boolean;
6
7
  };
7
8
  export declare function createRGXBounds(positive: RGXToken, negative: RGXToken, options?: CreateRGXBoundsOptions | string | boolean): [RGXConvertibleToken, RGXConvertibleToken];
@@ -27,10 +27,14 @@ function createRGXBounds(positive, negative, options = {}) {
27
27
  options = { flags: options };
28
28
  else if (typeof options === "boolean")
29
29
  options = { anchorStart: options, anchorEnd: options };
30
- const { flags = "", anchorStart = true, anchorEnd = true } = options;
30
+ const { flags = "", anchorStart = true, anchorEnd = true, inner = true } = options;
31
31
  const resolvedPositive = (0, resolve_1.resolveRGXToken)(convertNoGroupWrap(positive), { groupWrap: false, currentFlags: flags });
32
32
  const resolvedNegative = (0, resolve_1.resolveRGXToken)(convertNoGroupWrap(negative), { groupWrap: false, currentFlags: flags });
33
- const startBound = convertToBound((0, createRegex_1.createRegex)(`(?<=${resolvedNegative}${anchorStart ? "|^" : ""})(?=${resolvedPositive})`, flags, true));
34
- const endBound = convertToBound((0, createRegex_1.createRegex)(`(?<=${resolvedPositive})(?=${resolvedNegative}${anchorEnd ? "|$" : ""})`, flags, true));
33
+ const startLookbehind = `(?<=${resolvedNegative}${anchorStart ? "|^" : ""})`;
34
+ const startLookahead = inner ? `(?=${resolvedPositive})` : "";
35
+ const startBound = convertToBound((0, createRegex_1.createRegex)(`${startLookbehind}${startLookahead}`, flags, true));
36
+ const endLookbehind = inner ? `(?<=${resolvedPositive})` : "";
37
+ const endLookahead = `(?=${resolvedNegative}${anchorEnd ? "|$" : ""})`;
38
+ const endBound = convertToBound((0, createRegex_1.createRegex)(`${endLookbehind}${endLookahead}`, flags, true));
35
39
  return [startBound, endBound];
36
40
  }
@@ -60,12 +60,19 @@ export declare class RGXWalker<R, S = unknown> {
60
60
  private validateCapture;
61
61
  private handleAfterCapture;
62
62
  step(): RGXCapture | null;
63
- stepToToken(predicate: (token: RGXTokenOrPart<R>) => boolean): this;
63
+ private _stepToTokenIter;
64
+ stepToToken(predicate?: (token: RGXTokenOrPart<R>) => boolean): this;
65
+ stepToTokenAsync(predicate?: (token: RGXTokenOrPart<R, S, unknown>) => boolean, yieldFn?: () => Promise<void>): Promise<this>;
66
+ private _stepPastCurrentPartIfNeeded;
64
67
  stepToPart(predicate?: (part: RGXPart<R, S, unknown>) => boolean): this;
68
+ stepToPartAsync(predicate?: (part: RGXPart<R, S, unknown>) => boolean, yieldFn?: () => Promise<void>): Promise<this>;
65
69
  walk(): R;
70
+ walkAsync(yieldFn?: () => Promise<void>): Promise<R>;
66
71
  snapshot(name: string, ...keys: Array<RGXWalkerSnapshotKey | false>): this;
67
72
  restore(name: string): this;
73
+ private _walkerSnapshot;
68
74
  tryWalk({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions): boolean;
75
+ tryWalkAsync({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions, yieldFn?: () => Promise<void>): Promise<boolean>;
69
76
  clone(depth?: CloneDepth): RGXWalker<R, S>;
70
77
  }
71
78
  export declare function rgxWalker<R, S = unknown>(...args: ConstructorParameters<typeof RGXWalker<R, S>>): RGXWalker<R, S>;
@@ -315,7 +315,7 @@ class RGXWalker {
315
315
  this.advanceToken();
316
316
  return captureResult;
317
317
  }
318
- stepToToken(predicate) {
318
+ *_stepToTokenIter(predicate) {
319
319
  while (this.hasNextToken()) {
320
320
  this._stopped = false;
321
321
  if (predicate(this.currentToken()))
@@ -323,25 +323,51 @@ class RGXWalker {
323
323
  this.step();
324
324
  if (this._stopped)
325
325
  break;
326
+ yield;
326
327
  }
328
+ }
329
+ stepToToken(predicate = () => true) {
330
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
331
+ for (const _ of this._stepToTokenIter(predicate)) { /* sync — no yield needed */ }
327
332
  return this;
328
333
  }
329
- stepToPart(predicate = () => true) {
330
- // If currently at a Part, step past it first so repeated
331
- // calls advance to the next Part rather than getting stuck.
334
+ async stepToTokenAsync(predicate = () => true, yieldFn = () => new Promise(resolve => setImmediate(resolve))) {
335
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
336
+ for (const _ of this._stepToTokenIter(predicate))
337
+ await yieldFn();
338
+ return this;
339
+ }
340
+ // If currently at a Part, steps past it (so repeated stepToPart calls advance
341
+ // rather than getting stuck). Returns true when the walker stopped mid-step.
342
+ _stepPastCurrentPartIfNeeded() {
332
343
  if (this.hasNextToken() && this.currentToken() instanceof part_1.RGXPart) {
333
344
  this._stopped = false;
334
345
  this.step();
335
346
  if (this._stopped)
336
- return this;
347
+ return true;
337
348
  }
349
+ return false;
350
+ }
351
+ stepToPart(predicate = () => true) {
352
+ if (this._stepPastCurrentPartIfNeeded())
353
+ return this;
338
354
  this.stepToToken(token => token instanceof part_1.RGXPart && predicate(token));
339
355
  return this;
340
356
  }
357
+ async stepToPartAsync(predicate = () => true, yieldFn = () => new Promise(resolve => setImmediate(resolve))) {
358
+ if (this._stepPastCurrentPartIfNeeded())
359
+ return this;
360
+ await this.stepToTokenAsync(token => token instanceof part_1.RGXPart && predicate(token), yieldFn);
361
+ return this;
362
+ }
341
363
  walk() {
342
364
  this.stepToToken(() => false);
343
365
  return this.reduced;
344
366
  }
367
+ async walkAsync(yieldFn) {
368
+ await this.stepToTokenAsync(() => false, yieldFn);
369
+ return this.reduced;
370
+ }
345
371
  snapshot(name, ...keys) {
346
372
  const filteredKeys = keys.filter((k) => k !== false);
347
373
  const snap = {};
@@ -374,8 +400,11 @@ class RGXWalker {
374
400
  this.namedCaptures = snap.namedCaptures;
375
401
  return this;
376
402
  }
403
+ _walkerSnapshot(name, { revertReduced, revertShare, revertCaptures }) {
404
+ return this.snapshot(name, "sourcePosition", "tokenPosition", revertReduced && "reduced", revertShare && "share", revertCaptures && "captures", revertCaptures && "namedCaptures");
405
+ }
377
406
  tryWalk({ revertReduced = false, revertShare = false, revertCaptures = false } = {}) {
378
- this.snapshot("tryWalk", "sourcePosition", "tokenPosition", revertReduced && "reduced", revertShare && "share", revertCaptures && "captures", revertCaptures && "namedCaptures");
407
+ this._walkerSnapshot("tryWalk", { revertReduced, revertShare, revertCaptures });
379
408
  try {
380
409
  this.walk();
381
410
  return true;
@@ -387,6 +416,19 @@ class RGXWalker {
387
416
  throw e;
388
417
  }
389
418
  }
419
+ async tryWalkAsync({ revertReduced = false, revertShare = false, revertCaptures = false } = {}, yieldFn) {
420
+ this._walkerSnapshot("tryWalk", { revertReduced, revertShare, revertCaptures });
421
+ try {
422
+ await this.walkAsync(yieldFn);
423
+ return true;
424
+ }
425
+ catch (e) {
426
+ this.restore("tryWalk");
427
+ if (isMatchError(e))
428
+ return false;
429
+ throw e;
430
+ }
431
+ }
390
432
  // Clone method
391
433
  clone(depth = "max") {
392
434
  if (depth === 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "13.5.0",
3
+ "version": "13.7.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",