@ptolemy2002/rgx 5.0.0 → 5.1.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/README.md +29 -5
- package/dist/class/base.d.ts +2 -2
- package/dist/class/base.js +4 -4
- package/dist/class/index.d.ts +1 -0
- package/dist/class/index.js +1 -0
- package/dist/class/init.js +2 -2
- package/dist/class/repeat.d.ts +3 -2
- package/dist/class/repeat.js +24 -16
- package/dist/class/subpattern.d.ts +13 -0
- package/dist/class/subpattern.js +43 -0
- package/dist/class/wrapper.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -286,8 +286,8 @@ An abstract base class for creating custom RGX token classes. Subclasses must im
|
|
|
286
286
|
#### Methods
|
|
287
287
|
- `or(...others: RGXTokenCollectionInput[]) => RGXClassUnionToken`: Creates an `RGXClassUnionToken` that represents a union (alternation) of this token with the provided others. If any of the `others` are `RGXClassUnionToken` instances, their tokens are flattened into the union rather than nested. If `this` is already an `RGXClassUnionToken`, its existing tokens are preserved and the others are appended.
|
|
288
288
|
- `group(args?: RGXGroupTokenArgs) => RGXGroupToken`: Wraps this token in an `RGXGroupToken` with the provided arguments. The `args` parameter defaults to `{}`, which creates a capturing group with no name. This is a convenience method that creates a new `RGXGroupToken` with `this` as the sole token.
|
|
289
|
-
- `repeat(min?: number, max?: number | null) => RGXRepeatToken`: Wraps this token in an `RGXRepeatToken` with the given repetition bounds. `min` defaults to `1`, `max` defaults to `min`. Pass `null` for `max` to allow unlimited repetitions. This is a convenience method that creates a new `RGXRepeatToken` with `this` as the token. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
290
|
-
- `optional() => RGXRepeatToken`: Shorthand for `repeat(0, 1)`. Wraps this token in an `RGXRepeatToken` that matches the token zero or one times. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
289
|
+
- `repeat(min?: number, max?: number | null, lazy?: boolean) => RGXRepeatToken`: Wraps this token in an `RGXRepeatToken` with the given repetition bounds. `min` defaults to `1`, `max` defaults to `min`, `lazy` defaults to `false`. Pass `null` for `max` to allow unlimited repetitions. When `lazy` is `true`, the resulting quantifier will be non-greedy. This is a convenience method that creates a new `RGXRepeatToken` with `this` as the token. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
290
|
+
- `optional(lazy?: boolean) => RGXRepeatToken`: Shorthand for `repeat(0, 1, lazy)`. Wraps this token in an `RGXRepeatToken` that matches the token zero or one times. `lazy` defaults to `false`. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
291
291
|
- `asLookahead(positive?: boolean) => RGXLookaheadToken`: Wraps this token in an `RGXLookaheadToken`. `positive` defaults to `true`. If this token is already an `RGXLookaheadToken`, it is returned as-is without re-wrapping.
|
|
292
292
|
- `asLookbehind(positive?: boolean) => RGXLookbehindToken`: Wraps this token in an `RGXLookbehindToken`. `positive` defaults to `true`. If this token is already an `RGXLookbehindToken`, it is returned as-is without re-wrapping.
|
|
293
293
|
- `resolve() => ValidRegexString`: A convenience method that resolves this token by calling `resolveRGXToken(this)`, returning the resolved regex string representation. Since this method is defined on `RGXClassToken`, it is available on all subclasses including `RGXClassUnionToken`, `RGXGroupToken`, `RGXRepeatToken`, and `RGXLookaroundToken`.
|
|
@@ -354,17 +354,19 @@ A function `rgxRepeat` is provided with the same parameters as this class' const
|
|
|
354
354
|
|
|
355
355
|
#### Constructor
|
|
356
356
|
```typescript
|
|
357
|
-
constructor(token: RGXToken, min?: number, max?: number | null)
|
|
357
|
+
constructor(token: RGXToken, min?: number, max?: number | null, lazy?: boolean)
|
|
358
358
|
```
|
|
359
359
|
- `token` (`RGXToken`): The token to repeat. If the token is not already a grouped token, it will be automatically wrapped in a non-capturing `RGXGroupToken`.
|
|
360
360
|
- `min` (`number`, optional): The minimum number of repetitions. Must be >= 0 and <= `max` (when `max` is not `null`). Non-integer values are floored. Defaults to `1`.
|
|
361
361
|
- `max` (`number | null`, optional): The maximum number of repetitions. Must be >= `min` when not `null`. Non-integer values are floored. Pass `null` for unlimited repetitions. Defaults to `min`.
|
|
362
|
+
- `lazy` (`boolean`, optional): Whether the quantifier should be non-greedy (lazy). Defaults to `false`.
|
|
362
363
|
|
|
363
364
|
#### Properties
|
|
364
365
|
- `token` (`RGXGroupedToken`): The token being repeated. Setting this will throw `RGXNotSupportedError` if the value is a convertible token with `rgxIsRepeatable` set to `false`, and will automatically wrap non-grouped tokens in a non-capturing `RGXGroupToken`.
|
|
365
366
|
- `min` (`number`): The minimum number of repetitions. Setting this validates that the value is >= 0 and <= `max` (when `max` is not `null`), and floors non-integer values. Throws `RGXOutOfBoundsError` if validation fails.
|
|
366
367
|
- `max` (`number | null`): The maximum number of repetitions. Setting this validates that the value is >= `min` when not `null`, and floors non-integer values. Pass `null` for unlimited. Throws `RGXOutOfBoundsError` if validation fails.
|
|
367
|
-
- `
|
|
368
|
+
- `lazy` (`boolean`): Whether the quantifier is non-greedy (lazy). When `true`, a `?` is appended to the `repeaterSuffix` (except when the suffix is `?` or empty, since those cases don't benefit from a lazy modifier). Defaults to `false`.
|
|
369
|
+
- `repeaterSuffix` (`string`): Returns the regex quantifier suffix based on the current `min`, `max`, and `lazy` values: `*` for `{0,}`, `+` for `{1,}`, `?` for `{0,1}`, `{n}` for exact repetitions, `{n,}` for minimum-only, `{n,m}` for a range, or an empty string for `{1,1}` (exactly once, no quantifier needed). When `lazy` is `true`, a `?` is appended to the suffix (e.g., `*?`, `+?`, `{2,5}?`), except when the suffix is already `?` or empty.
|
|
368
370
|
- `rgxGroupWrap` (`false`): Returns `false` as a constant, since the quantifier suffix binds tightly to the preceding group and does not need additional wrapping.
|
|
369
371
|
|
|
370
372
|
#### Methods
|
|
@@ -424,6 +426,28 @@ A function `rgxLookbehind` is provided with the same parameters as this class' c
|
|
|
424
426
|
- `reverse() => RGXLookaheadToken`: Returns a new `RGXLookaheadToken` with the same tokens and positivity.
|
|
425
427
|
- `toRgx() => RegExp`: Resolves the lookbehind to a `RegExp`. Positive lookbehinds produce `(?<=...)` and negative lookbehinds produce `(?<!...)`.
|
|
426
428
|
|
|
429
|
+
### RGXSubpatternToken extends RGXClassToken
|
|
430
|
+
A class representing a backreference to a previously captured group, either by name or by group number. Named backreferences produce `\k<name>` and numbered backreferences produce `\N` (where N is the group number). This is useful for matching the same text that was captured by a previous group.
|
|
431
|
+
|
|
432
|
+
A function `rgxSubpattern` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
433
|
+
|
|
434
|
+
#### Static Properties
|
|
435
|
+
- `check(value: unknown): value is RGXSubpatternToken`: A type guard that checks if the given value is an instance of `RGXSubpatternToken`.
|
|
436
|
+
- `assert(value: unknown): asserts value is RGXSubpatternToken`: An assertion that checks if the given value is an instance of `RGXSubpatternToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
437
|
+
|
|
438
|
+
#### Constructor
|
|
439
|
+
```typescript
|
|
440
|
+
constructor(pattern: string | number)
|
|
441
|
+
```
|
|
442
|
+
- `pattern` (`string | number`): The backreference pattern. If a string, it must be a valid identifier (validated via `assertValidIdentifier`) and produces a named backreference (`\k<name>`). If a number, it must be a positive integer (>= 1, as groups are 1-indexed) and produces a numbered backreference (`\N`). Non-integer numbers are floored.
|
|
443
|
+
|
|
444
|
+
#### Properties
|
|
445
|
+
- `pattern` (`string | number`): The backreference pattern. Setting this validates the value: strings must be valid identifiers, numbers must be positive integers (>= 1). Non-integer numbers are floored.
|
|
446
|
+
|
|
447
|
+
#### Methods
|
|
448
|
+
- `toRgx() => RegExp`: Resolves the backreference to a `RegExp`. Named patterns produce `/\k<name>/` and numbered patterns produce `/\N/`.
|
|
449
|
+
- `clone(depth: CloneDepth = "max") => RGXSubpatternToken`: Creates a clone of this token. When `depth` is `0`, returns `this`; otherwise, returns a new `RGXSubpatternToken` with the same pattern.
|
|
450
|
+
|
|
427
451
|
### RGXClassWrapperToken extends RGXClassToken
|
|
428
452
|
A class that wraps any `RGXToken` as an `RGXClassToken`, giving you access to the extended API class tokens provide. It delegates `rgxIsGroup` and `rgxIsRepeatable` to the wrapped token where possible.
|
|
429
453
|
|
|
@@ -442,7 +466,7 @@ constructor(token: RGXToken)
|
|
|
442
466
|
#### Properties
|
|
443
467
|
- `token` (`RGXToken`): The wrapped token.
|
|
444
468
|
- `rgxIsGroup` (`boolean`): Delegates to the wrapped token's group status via `isRGXGroupedToken`. Returns `true` if the wrapped token is a grouped token, otherwise `false`.
|
|
445
|
-
- `rgxIsRepeatable` (`boolean`): If the wrapped token is an `
|
|
469
|
+
- `rgxIsRepeatable` (`boolean`): If the wrapped token is an `RGXConvertibleToken`, delegates to its `rgxIsRepeatable` property (defaulting to `true` if not present). Otherwise, returns `true`.
|
|
446
470
|
|
|
447
471
|
#### Methods
|
|
448
472
|
- `unwrap() => RGXToken`: Returns the original wrapped token.
|
package/dist/class/base.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export declare abstract class RGXClassToken implements RGXConvertibleToken {
|
|
|
16
16
|
get rgxGroupWrap(): boolean;
|
|
17
17
|
or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
|
|
18
18
|
group(args?: RGXGroupTokenArgs): RGXGroupToken;
|
|
19
|
-
repeat(min?: number, max?: number | null): RGXRepeatToken;
|
|
20
|
-
optional(): RGXRepeatToken;
|
|
19
|
+
repeat(min?: number, max?: number | null, lazy?: boolean): RGXRepeatToken;
|
|
20
|
+
optional(lazy?: boolean): RGXRepeatToken;
|
|
21
21
|
asLookahead(positive?: boolean): RGXLookaheadToken;
|
|
22
22
|
asLookbehind(positive?: boolean): RGXLookbehindToken;
|
|
23
23
|
resolve(): ValidRegexString;
|
package/dist/class/base.js
CHANGED
|
@@ -19,11 +19,11 @@ class RGXClassToken {
|
|
|
19
19
|
group(args = {}) {
|
|
20
20
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.group(args)', 'call rgxClassInit() first.');
|
|
21
21
|
}
|
|
22
|
-
repeat(min = 1, max = min) {
|
|
23
|
-
throw new errors_1.RGXNotImplementedError('RGXClassToken.repeat(min, max)', 'call rgxClassInit() first.');
|
|
22
|
+
repeat(min = 1, max = min, lazy = false) {
|
|
23
|
+
throw new errors_1.RGXNotImplementedError('RGXClassToken.repeat(min, max, lazy)', 'call rgxClassInit() first.');
|
|
24
24
|
}
|
|
25
|
-
optional() {
|
|
26
|
-
return this.repeat(0, 1);
|
|
25
|
+
optional(lazy = false) {
|
|
26
|
+
return this.repeat(0, 1, lazy);
|
|
27
27
|
}
|
|
28
28
|
asLookahead(positive = true) {
|
|
29
29
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.asLookahead(positive)', 'call rgxClassInit() first.');
|
package/dist/class/index.d.ts
CHANGED
package/dist/class/index.js
CHANGED
|
@@ -23,4 +23,5 @@ __exportStar(require("./lookaround"), exports);
|
|
|
23
23
|
__exportStar(require("./lookahead"), exports);
|
|
24
24
|
__exportStar(require("./lookbehind"), exports);
|
|
25
25
|
__exportStar(require("./wrapper"), exports);
|
|
26
|
+
__exportStar(require("./subpattern"), exports);
|
|
26
27
|
__exportStar(require("./toRGXClassToken"), exports);
|
package/dist/class/init.js
CHANGED
|
@@ -28,10 +28,10 @@ function rgxClassInit() {
|
|
|
28
28
|
base_1.RGXClassToken.prototype.group = function (args = {}) {
|
|
29
29
|
return new group_1.RGXGroupToken(args, [this]);
|
|
30
30
|
};
|
|
31
|
-
base_1.RGXClassToken.prototype.repeat = function (min = 1, max = min) {
|
|
31
|
+
base_1.RGXClassToken.prototype.repeat = function (min = 1, max = min, lazy = false) {
|
|
32
32
|
if (lookaround_1.RGXLookaroundToken.check(this))
|
|
33
33
|
throw new errors_1.RGXNotSupportedError("RGXLookaroundToken.repeat()", "Lookaround tokens cannot be repeated or made optional.");
|
|
34
|
-
return new repeat_1.RGXRepeatToken(this, min, max);
|
|
34
|
+
return new repeat_1.RGXRepeatToken(this, min, max, lazy);
|
|
35
35
|
};
|
|
36
36
|
base_1.RGXClassToken.prototype.asLookahead = function (positive = true) {
|
|
37
37
|
if (lookahead_1.RGXLookaheadToken.check(this))
|
package/dist/class/repeat.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare class RGXRepeatToken extends RGXClassToken {
|
|
|
5
5
|
_token: RGXGroupedToken;
|
|
6
6
|
_min: number;
|
|
7
7
|
_max: number | null;
|
|
8
|
+
lazy: boolean;
|
|
8
9
|
static check: (value: unknown) => value is RGXRepeatToken;
|
|
9
10
|
static assert: (value: unknown) => asserts value is RGXRepeatToken;
|
|
10
11
|
get min(): number;
|
|
@@ -14,9 +15,9 @@ export declare class RGXRepeatToken extends RGXClassToken {
|
|
|
14
15
|
get token(): RGXGroupedToken;
|
|
15
16
|
set token(value: RGXToken);
|
|
16
17
|
get rgxGroupWrap(): false;
|
|
17
|
-
constructor(token: RGXToken, min?: number, max?: number | null);
|
|
18
|
+
constructor(token: RGXToken, min?: number, max?: number | null, lazy?: boolean);
|
|
18
19
|
get repeaterSuffix(): string;
|
|
19
20
|
toRgx(): RGXToken;
|
|
20
21
|
clone(depth?: CloneDepth): RGXRepeatToken;
|
|
21
22
|
}
|
|
22
|
-
export declare const rgxRepeat: (token: RGXToken, min?: number | undefined, max?: number | null | undefined) => RGXRepeatToken;
|
|
23
|
+
export declare const rgxRepeat: (token: RGXToken, min?: number | undefined, max?: number | null | undefined, lazy?: boolean | undefined) => RGXRepeatToken;
|
package/dist/class/repeat.js
CHANGED
|
@@ -52,29 +52,37 @@ class RGXRepeatToken extends base_1.RGXClassToken {
|
|
|
52
52
|
return false;
|
|
53
53
|
}
|
|
54
54
|
// By default, repeat a fixed number of times.
|
|
55
|
-
constructor(token, min = 1, max = min) {
|
|
55
|
+
constructor(token, min = 1, max = min, lazy = false) {
|
|
56
56
|
super();
|
|
57
57
|
this._max = null;
|
|
58
|
+
this.lazy = false;
|
|
58
59
|
this.token = token;
|
|
59
60
|
this.min = min;
|
|
60
61
|
this.max = max;
|
|
62
|
+
this.lazy = lazy;
|
|
61
63
|
}
|
|
62
64
|
get repeaterSuffix() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
65
|
+
const result = (() => {
|
|
66
|
+
if (this.min === 0 && this.max === null)
|
|
67
|
+
return '*';
|
|
68
|
+
if (this.min === 1 && this.max === null)
|
|
69
|
+
return '+';
|
|
70
|
+
if (this.min === 0 && this.max === 1)
|
|
71
|
+
return '?';
|
|
72
|
+
if (this.max === null)
|
|
73
|
+
return `{${this.min},}`;
|
|
74
|
+
if (this.min === this.max) {
|
|
75
|
+
// No need for a repeater suffix if we're repeating exactly once.
|
|
76
|
+
if (this.min === 1)
|
|
77
|
+
return '';
|
|
78
|
+
return `{${this.min}}`;
|
|
79
|
+
}
|
|
80
|
+
return `{${this.min},${this.max}}`;
|
|
81
|
+
})();
|
|
82
|
+
if (this.lazy && result.length > 0 && result !== "?")
|
|
83
|
+
return result + '?';
|
|
84
|
+
else
|
|
85
|
+
return result;
|
|
78
86
|
}
|
|
79
87
|
toRgx() {
|
|
80
88
|
// No-op if we're repeating zero times.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RGXClassToken } from "./base";
|
|
2
|
+
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
3
|
+
export declare class RGXSubpatternToken extends RGXClassToken {
|
|
4
|
+
_pattern: string | number;
|
|
5
|
+
get pattern(): string | number;
|
|
6
|
+
set pattern(value: string | number);
|
|
7
|
+
static check: (value: unknown) => value is RGXSubpatternToken;
|
|
8
|
+
static assert: (value: unknown) => asserts value is RGXSubpatternToken;
|
|
9
|
+
constructor(pattern: string | number);
|
|
10
|
+
toRgx(): RegExp;
|
|
11
|
+
clone(depth?: CloneDepth): RGXSubpatternToken;
|
|
12
|
+
}
|
|
13
|
+
export declare const rgxSubpattern: (pattern: string | number) => RGXSubpatternToken;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxSubpattern = exports.RGXSubpatternToken = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
const typeGuards_1 = require("../typeGuards");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const internal_1 = require("../internal");
|
|
8
|
+
class RGXSubpatternToken extends base_1.RGXClassToken {
|
|
9
|
+
get pattern() {
|
|
10
|
+
return this._pattern;
|
|
11
|
+
}
|
|
12
|
+
set pattern(value) {
|
|
13
|
+
if (typeof value === "string") {
|
|
14
|
+
(0, typeGuards_1.assertValidIdentifier)(value);
|
|
15
|
+
this._pattern = value;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
(0, errors_1.assertInRange)(value, { min: 1 }, "Subpattern group numbers must be positive integers (groups are 1-indexed).");
|
|
19
|
+
this._pattern = Math.floor(value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
constructor(pattern) {
|
|
23
|
+
super();
|
|
24
|
+
this.pattern = pattern;
|
|
25
|
+
}
|
|
26
|
+
toRgx() {
|
|
27
|
+
if (typeof this.pattern === "string") {
|
|
28
|
+
return new RegExp(`\\k<${this.pattern}>`);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return new RegExp(`\\${this.pattern}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
clone(depth = "max") {
|
|
35
|
+
if (depth === 0)
|
|
36
|
+
return this;
|
|
37
|
+
return new RGXSubpatternToken(this.pattern);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.RGXSubpatternToken = RGXSubpatternToken;
|
|
41
|
+
RGXSubpatternToken.check = (0, internal_1.createClassGuardFunction)(RGXSubpatternToken);
|
|
42
|
+
RGXSubpatternToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXSubpatternToken);
|
|
43
|
+
exports.rgxSubpattern = (0, internal_1.createConstructFunction)(RGXSubpatternToken);
|
package/dist/class/wrapper.js
CHANGED
|
@@ -15,8 +15,8 @@ class RGXClassWrapperToken extends base_1.RGXClassToken {
|
|
|
15
15
|
return (0, typeGuards_1.isRGXGroupedToken)(this.token);
|
|
16
16
|
}
|
|
17
17
|
get rgxIsRepeatable() {
|
|
18
|
-
if ((0, typeGuards_1.isRGXToken)(this.token, '
|
|
19
|
-
return this.token.rgxIsRepeatable;
|
|
18
|
+
if ((0, typeGuards_1.isRGXToken)(this.token, 'convertible'))
|
|
19
|
+
return this.token.rgxIsRepeatable ?? true;
|
|
20
20
|
// Assume any other token is repeatable, since we don't know its implementation.
|
|
21
21
|
return true;
|
|
22
22
|
}
|