@ptolemy2002/rgx 7.7.4 → 8.0.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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as t from "./types";
2
2
  import { ExtRegExp } from "./ExtRegExp";
3
- import { RGXWalker } from "./walker";
3
+ import { RGXTokenOrPart, RGXWalker } from "./walker";
4
4
  export * from "./errors";
5
5
  export * from "./types";
6
6
  export * from "./typeGuards";
@@ -16,5 +16,5 @@ export * from "./constants";
16
16
  export * from "./walker";
17
17
  export declare function rgxa(tokens: t.RGXToken[], flags?: string): ExtRegExp;
18
18
  export default function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => ExtRegExp;
19
- export declare function rgxwa<R = unknown>(source: string, tokens: t.RGXToken[], options?: Omit<t.RGXWOptions<R>, "multiline">): RGXWalker<R>;
20
- export declare function rgxw<R = unknown>(source: string, { multiline, ...options }?: t.RGXWOptions<R>): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => RGXWalker<R>;
19
+ export declare function rgxwa<R = unknown, T = unknown>(source: string, tokens: RGXTokenOrPart<R, T>[], options?: Omit<t.RGXWOptions<R>, "multiline">): RGXWalker<R>;
20
+ export declare function rgxw<R = unknown, T = unknown>(source: string, { multiline, ...options }?: t.RGXWOptions<R>): (strings: TemplateStringsArray, ...tokens: RGXTokenOrPart<R, T>[]) => RGXWalker<R>;
package/dist/index.js CHANGED
@@ -54,7 +54,7 @@ function rgx(flags = '', multiline = true) {
54
54
  };
55
55
  }
56
56
  function rgxwa(source, tokens, options = {}) {
57
- (0, internal_1.assureAcceptance)(tokens, '');
57
+ (0, internal_1.assureAcceptance)(tokens.map(t => walker_1.RGXPart.check(t) ? t.token : t), '');
58
58
  return new walker_1.RGXWalker(source, tokens, options);
59
59
  }
60
60
  function rgxw(source, { multiline = true, ...options } = {}) {
@@ -1,6 +1,5 @@
1
1
  import { CloneDepth } from "@ptolemy2002/immutability-utils";
2
- import { RGXTokenCollection, RGXTokenCollectionInput } from "../collection";
3
- import { RGXConvertibleToken, RGXToken } from "../types";
2
+ import { RGXToken } from "../types";
4
3
  import { RGXPart, RGXCapture } from "./part";
5
4
  export type RGXWalkerOptions<R> = {
6
5
  startingSourcePosition?: number;
@@ -8,10 +7,11 @@ export type RGXWalkerOptions<R> = {
8
7
  infinite?: boolean;
9
8
  looping?: boolean;
10
9
  };
11
- export declare class RGXWalker<R> implements RGXConvertibleToken {
10
+ export type RGXTokenOrPart<R, T = unknown> = RGXToken | RGXPart<R, T>;
11
+ export declare class RGXWalker<R> {
12
12
  readonly source: string;
13
13
  _sourcePosition: number;
14
- readonly tokens: RGXTokenCollection;
14
+ readonly tokens: RGXTokenOrPart<R>[];
15
15
  _tokenPosition: number;
16
16
  reduced: R;
17
17
  captures: RGXCapture[];
@@ -26,22 +26,21 @@ export declare class RGXWalker<R> implements RGXConvertibleToken {
26
26
  get tokenPosition(): number;
27
27
  set tokenPosition(value: number);
28
28
  get stopped(): boolean;
29
- constructor(source: string, tokens: RGXTokenCollectionInput, options?: RGXWalkerOptions<R>);
29
+ constructor(source: string, tokens: RGXTokenOrPart<R>[], options?: RGXWalkerOptions<R>);
30
30
  stop(): this;
31
31
  atTokenEnd(): boolean;
32
- hasNextToken(predicate?: (token: RGXToken) => boolean): boolean;
32
+ hasNextToken(predicate?: (token: RGXToken | RGXPart<R>) => boolean): boolean;
33
33
  atSourceEnd(): boolean;
34
34
  hasNextSource(predicate?: (rest: string) => boolean): boolean;
35
35
  lastCapture(): RGXCapture | null;
36
- currentToken(): RGXToken;
36
+ currentToken(): RGXTokenOrPart<R, unknown>;
37
37
  remainingSource(): string | null;
38
- capture(token: RGXToken, includeMatch: true): RegExpExecArray;
39
- capture(token: RGXToken, includeMatch?: false): string;
38
+ capture(token: RGXTokenOrPart<R>, includeMatch: true): RegExpExecArray;
39
+ capture(token: RGXTokenOrPart<R>, includeMatch?: false): string;
40
40
  step(): RGXCapture | null;
41
- stepToToken(predicate: (token: RGXToken) => boolean): this;
42
- stepToPart(predicate?: (part: RGXPart<R>) => boolean): this;
41
+ stepToToken(predicate: (token: RGXTokenOrPart<R>) => boolean): this;
42
+ stepToPart(predicate?: (part: RGXPart<R, unknown>) => boolean): this;
43
43
  walk(): this;
44
- toRgx(): RGXTokenCollection;
45
44
  clone(depth?: CloneDepth): RGXWalker<R>;
46
45
  }
47
46
  export declare function rgxWalker<R>(...args: ConstructorParameters<typeof RGXWalker<R>>): RGXWalker<R>;
@@ -46,7 +46,7 @@ class RGXWalker {
46
46
  this._stopped = false;
47
47
  this.source = source;
48
48
  this.sourcePosition = options.startingSourcePosition ?? 0;
49
- this.tokens = new collection_1.RGXTokenCollection(tokens, "concat");
49
+ this.tokens = tokens;
50
50
  this.tokenPosition = 0;
51
51
  this.reduced = options.reduced ?? null;
52
52
  this.infinite = options.infinite ?? false;
@@ -76,7 +76,7 @@ class RGXWalker {
76
76
  currentToken() {
77
77
  if (this.atTokenEnd())
78
78
  return null;
79
- return this.tokens.at(this.tokenPosition);
79
+ return this.tokens[this.tokenPosition];
80
80
  }
81
81
  remainingSource() {
82
82
  if (this.atSourceEnd())
@@ -84,7 +84,7 @@ class RGXWalker {
84
84
  return this.source.slice(this.sourcePosition);
85
85
  }
86
86
  capture(token, includeMatch = false) {
87
- const regex = (0, index_1.rgxa)([token]);
87
+ const regex = (0, index_1.rgxa)([part_1.RGXPart.check(token) ? token.token : token]);
88
88
  const match = (0, index_1.assertRegexMatchesAtPosition)(regex, this.source, this.sourcePosition, 10, true);
89
89
  this.sourcePosition += match[0].length;
90
90
  return includeMatch ? match : match[0];
@@ -119,29 +119,29 @@ class RGXWalker {
119
119
  }
120
120
  // Capture the match
121
121
  const start = this.sourcePosition;
122
- let innerToken = token;
122
+ let branchedToken;
123
123
  if (isPart)
124
- innerToken = createBranchGroups(token.token);
125
- const capture = this.capture(innerToken, true);
124
+ branchedToken = createBranchGroups(token.token);
125
+ else
126
+ branchedToken = createBranchGroups(token);
127
+ const capture = this.capture(branchedToken, true);
126
128
  const raw = isPart ? token.rawTransform(capture[0]) : capture[0];
127
129
  const end = this.sourcePosition;
128
130
  const value = isPart ? token.transform(raw) : raw;
129
131
  let branch = 0;
130
- if (isPart) {
131
- // Determine branch index for captureResult by finding the first index
132
- // with non-undefined match group.
133
- for (let i = 0; i < capture.length; i++) {
134
- const branchKey = `rgx_branch_${i}`;
135
- if (capture.groups && capture.groups[branchKey] !== undefined) {
136
- branch = i;
137
- break;
138
- }
132
+ // Determine branch index for captureResult by finding the first index
133
+ // with non-undefined match group.
134
+ for (let i = 0; i < capture.length; i++) {
135
+ const branchKey = `rgx_branch_${i}`;
136
+ if (capture.groups && capture.groups[branchKey] !== undefined) {
137
+ branch = i;
138
+ break;
139
139
  }
140
140
  }
141
141
  const captureResult = {
142
142
  raw, value, start, end, branch,
143
143
  ownerId: isPart && token.hasId() ? token.id : null,
144
- groups: isPart ? capture.groups ?? null : null
144
+ groups: capture.groups ?? null
145
145
  };
146
146
  // Validate the part. If validation fails, it will throw an error, so nothing below will run.
147
147
  if (isPart) {
@@ -194,15 +194,11 @@ class RGXWalker {
194
194
  walk() {
195
195
  return this.stepToToken(() => false);
196
196
  }
197
- // When used as a convertible token, treat the walker as its collection.
198
- toRgx() {
199
- return this.tokens;
200
- }
201
197
  // Clone method
202
198
  clone(depth = "max") {
203
199
  if (depth === 0)
204
200
  return this;
205
- const clone = new RGXWalker(this.source, this.tokens.clone((0, immutability_utils_1.depthDecrement)(1)), {
201
+ const clone = new RGXWalker(this.source, (0, immutability_utils_1.extClone)(this.tokens, (0, immutability_utils_1.depthDecrement)(1)), {
206
202
  startingSourcePosition: this.sourcePosition,
207
203
  reduced: (0, immutability_utils_1.extClone)(this.reduced, (0, immutability_utils_1.depthDecrement)(1)),
208
204
  infinite: this.infinite,
@@ -220,3 +216,4 @@ RGXWalker.assert = (0, internal_1.createAssertClassGuardFunction)(RGXWalker);
220
216
  function rgxWalker(...args) {
221
217
  return new RGXWalker(...args);
222
218
  }
219
+ ;
@@ -1,4 +1,4 @@
1
- import { RGXConvertibleToken, RGXToken } from "../types";
1
+ import { RGXToken } from "../types";
2
2
  import type { RGXWalker } from "./base";
3
3
  import { CloneDepth } from "@ptolemy2002/immutability-utils";
4
4
  export type RGXPartControl = "skip" | "stop" | "silent" | void;
@@ -19,7 +19,7 @@ export type RGXPartOptions<R, T = string> = {
19
19
  beforeCapture: ((part: RGXPart<R, T>, walker: RGXWalker<R>) => RGXPartControl) | null;
20
20
  afterCapture: ((capture: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => void) | null;
21
21
  };
22
- export declare class RGXPart<R, T = string> implements RGXConvertibleToken {
22
+ export declare class RGXPart<R, T = string> {
23
23
  id: string | null;
24
24
  token: RGXToken;
25
25
  readonly rawTransform: RGXPartOptions<R, T>["rawTransform"];
@@ -34,9 +34,6 @@ export declare class RGXPart<R, T = string> implements RGXConvertibleToken {
34
34
  id: string;
35
35
  };
36
36
  validate(capture: RGXCapture<T>, walker: RGXWalker<R>): void;
37
- get rgxIsGroup(): boolean;
38
- get rgxIsRepeatable(): boolean;
39
- toRgx(): RGXToken;
40
37
  clone(depth?: CloneDepth): RGXPart<R, T>;
41
38
  }
42
39
  export declare function rgxPart<R, T = string>(...args: ConstructorParameters<typeof RGXPart<R, T>>): RGXPart<R, T>;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RGXPart = void 0;
4
4
  exports.rgxPart = rgxPart;
5
- const typeGuards_1 = require("../typeGuards");
6
5
  const internal_1 = require("../internal");
7
6
  const clone_1 = require("../clone");
8
7
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
@@ -27,19 +26,6 @@ class RGXPart {
27
26
  const message = typeof result === "string" ? result : "Part Validation Failed";
28
27
  throw new errors_1.RGXPartValidationFailedError(this.id, message, capture.raw, capture.value);
29
28
  }
30
- // Properties used for conversion to an RGXToken
31
- get rgxIsGroup() {
32
- return (0, typeGuards_1.isRGXGroupedToken)(this.token);
33
- }
34
- get rgxIsRepeatable() {
35
- if ((0, typeGuards_1.isRGXToken)(this.token, 'convertible'))
36
- return this.token.rgxIsRepeatable ?? true;
37
- // Assume any other token is repeatable, since we don't know its implementation.
38
- return true;
39
- }
40
- toRgx() {
41
- return this.token;
42
- }
43
29
  // Clone method
44
30
  clone(depth = "max") {
45
31
  if (depth === 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "7.7.4",
3
+ "version": "8.0.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",