@ptolemy2002/rgx 13.0.2 → 13.2.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 +1 -0
- package/dist/class/base.d.ts +4 -1
- package/dist/class/base.js +5 -2
- package/dist/class/exclusion.d.ts +19 -0
- package/dist/class/exclusion.js +55 -0
- package/dist/class/index.d.ts +1 -0
- package/dist/class/index.js +1 -0
- package/dist/class/init.js +4 -0
- package/dist/collection.d.ts +2 -1
- package/dist/collection.js +2 -2
- package/dist/utils/createRGXBounds.d.ts +6 -1
- package/dist/utils/createRGXBounds.js +8 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,6 +33,7 @@ Because there is so much to document, it has been broken up into multiple files.
|
|
|
33
33
|
- [union](./docs/class/token/union.md) - The `RGXClassUnionToken` class, which represents a union of tokens that can match any one of the tokens in the union.
|
|
34
34
|
- [subpattern](./docs/class/token/subpattern.md) - The `RGXSubpatternToken` class, which re-matching the content of a previous capturing group.
|
|
35
35
|
- [wrapper](./docs/class/token/wrapper.md) - The `RGXClassWrapperToken` class, which represents a class token that can wrap any arbitrary token (even non-class), giving you the benefit of the class API for any token.
|
|
36
|
+
- [exclusion](./docs/class/token/exclusion.md) - The `RGXExclusionToken` class, which matches a token while excluding specific alternatives using lookahead-based subtraction.
|
|
36
37
|
- [to](./docs/class/token/to.md) - A utility for converting any token into a class token. Most tokens just get wrapped in a `RGXClassWrapperToken`, but some do not.
|
|
37
38
|
- `lookaround` - A directory containing documentation for lookahead and lookbehind tokens.
|
|
38
39
|
- [base](./docs/class/token/lookaround/base.md) - The `RGXLookaroundToken` class, which is the base class for lookahead and lookbehind tokens.
|
package/dist/class/base.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { RGXConvertibleToken, RGXToken, ValidRegexFlags, ValidRegexString } from "../types";
|
|
2
2
|
import { RGXTokenCollectionInput } from "../collection";
|
|
3
|
+
import { ResolveRGXTokenOptions } from "../resolve";
|
|
3
4
|
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
4
5
|
import type { RGXClassUnionToken } from "./union";
|
|
5
6
|
import type { RGXGroupToken, RGXGroupTokenArgs } from "./group";
|
|
6
7
|
import type { RGXRepeatToken } from "./repeat";
|
|
7
8
|
import type { RGXLookaheadToken } from "./lookahead";
|
|
8
9
|
import type { RGXLookbehindToken } from "./lookbehind";
|
|
10
|
+
import type { RGXExclusionToken } from "./exclusion";
|
|
9
11
|
export declare abstract class RGXClassToken implements RGXConvertibleToken {
|
|
10
12
|
abstract toRgx(): RGXToken;
|
|
11
13
|
abstract clone(depth?: CloneDepth): ThisType<this>;
|
|
@@ -22,5 +24,6 @@ export declare abstract class RGXClassToken implements RGXConvertibleToken {
|
|
|
22
24
|
optional(lazy?: boolean): RGXRepeatToken;
|
|
23
25
|
asLookahead(positive?: boolean): RGXLookaheadToken;
|
|
24
26
|
asLookbehind(positive?: boolean): RGXLookbehindToken;
|
|
25
|
-
|
|
27
|
+
subtract(exclusionId: string, exclusions?: RGXTokenCollectionInput, terminal?: RGXToken): RGXExclusionToken;
|
|
28
|
+
resolve(options?: ResolveRGXTokenOptions): ValidRegexString;
|
|
26
29
|
}
|
package/dist/class/base.js
CHANGED
|
@@ -37,8 +37,11 @@ class RGXClassToken {
|
|
|
37
37
|
asLookbehind(positive = true) {
|
|
38
38
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.asLookbehind(positive)', 'call rgxClassInit() first.');
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
subtract(exclusionId, exclusions = [], terminal = null) {
|
|
41
|
+
throw new errors_1.RGXNotImplementedError('RGXClassToken.subtract(exclusionId, exclusions, terminal)', 'call rgxClassInit() first.');
|
|
42
|
+
}
|
|
43
|
+
resolve(options = {}) {
|
|
44
|
+
return (0, resolve_1.resolveRGXToken)(this, options);
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
exports.RGXClassToken = RGXClassToken;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RGXToken } from "../types";
|
|
2
|
+
import { RGXClassToken } from "./base";
|
|
3
|
+
import { RGXClassUnionToken } from "./union";
|
|
4
|
+
import { RGXTokenCollectionInput } from "../collection";
|
|
5
|
+
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
6
|
+
export declare class RGXExclusionToken extends RGXClassToken {
|
|
7
|
+
_exclusionId: string;
|
|
8
|
+
token: RGXToken;
|
|
9
|
+
exclusions: RGXClassUnionToken;
|
|
10
|
+
terminal: RGXToken;
|
|
11
|
+
static check: (value: unknown) => value is RGXExclusionToken;
|
|
12
|
+
static assert: (value: unknown) => asserts value is RGXExclusionToken;
|
|
13
|
+
get exclusionId(): string;
|
|
14
|
+
set exclusionId(value: string);
|
|
15
|
+
constructor(exclusionId: string, token: RGXToken, exclusions?: RGXTokenCollectionInput, terminal?: RGXToken);
|
|
16
|
+
toRgx(): RGXToken;
|
|
17
|
+
clone(depth?: CloneDepth): RGXExclusionToken;
|
|
18
|
+
}
|
|
19
|
+
export declare const rgxExclusion: (exclusionId: string, token: RGXToken, exclusions?: RGXTokenCollectionInput, terminal?: RGXToken) => RGXExclusionToken;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxExclusion = exports.RGXExclusionToken = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
const union_1 = require("./union");
|
|
6
|
+
const resolve_1 = require("../resolve");
|
|
7
|
+
const typeGuards_1 = require("../typeGuards");
|
|
8
|
+
const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
|
|
9
|
+
const clone_1 = require("../clone");
|
|
10
|
+
const utils_1 = require("../utils");
|
|
11
|
+
const internal_1 = require("../internal");
|
|
12
|
+
class RGXExclusionToken extends base_1.RGXClassToken {
|
|
13
|
+
// exclusionId should be both a valid identifier and a unique group identifier across this branch of the entire pattern.
|
|
14
|
+
// The issue is that we can't verify that second condition. We should just tell that to the user in documentation.
|
|
15
|
+
get exclusionId() {
|
|
16
|
+
return this._exclusionId;
|
|
17
|
+
}
|
|
18
|
+
set exclusionId(value) {
|
|
19
|
+
(0, typeGuards_1.assertValidIdentifier)(value);
|
|
20
|
+
this._exclusionId = value;
|
|
21
|
+
}
|
|
22
|
+
constructor(exclusionId, token, exclusions = [], terminal = null) {
|
|
23
|
+
super();
|
|
24
|
+
this.terminal = null;
|
|
25
|
+
this.exclusionId = exclusionId;
|
|
26
|
+
this.token = token;
|
|
27
|
+
this.exclusions = new union_1.RGXClassUnionToken(exclusions);
|
|
28
|
+
this.terminal = terminal;
|
|
29
|
+
}
|
|
30
|
+
toRgx() {
|
|
31
|
+
const resolvedToken = (0, resolve_1.resolveRGXToken)(this.token);
|
|
32
|
+
const resolvedExclusions = this.exclusions.resolve();
|
|
33
|
+
// null and undefined are no-ops and will resolve to empty strings.
|
|
34
|
+
const resolvedTerminal = (0, resolve_1.resolveRGXToken)(this.terminal);
|
|
35
|
+
// Get a match to the pattern in a lookahead, use a negative lookahead to
|
|
36
|
+
// exclude the exclusion group from the match, then actually consume what we
|
|
37
|
+
// got from the lookahead.
|
|
38
|
+
// Note: the exclusions will prevent the pattern from matching if the matched text only
|
|
39
|
+
// begins with an exclusion pattern, not just if it is the entire exclusion pattern. The solution to this is to provide
|
|
40
|
+
// a terminal anchor, but that might not always be desirable or possible, so it's something to be aware of.
|
|
41
|
+
const source = `(?=(?<${this.exclusionId}>${resolvedToken}${resolvedTerminal}))(?!${resolvedExclusions}${resolvedTerminal})\\k<${this.exclusionId}>`;
|
|
42
|
+
// Because the terminal should be consuming no actual characters, it shuld be able to be repeated without issue, but we cannot actually validate
|
|
43
|
+
// that the terminal is not consuming characters, so we will just document that the user not consume characters in the terminal.
|
|
44
|
+
return new RegExp(source);
|
|
45
|
+
}
|
|
46
|
+
clone(depth = "max") {
|
|
47
|
+
if (depth === 0)
|
|
48
|
+
return this;
|
|
49
|
+
return new RGXExclusionToken(this.exclusionId, (0, clone_1.cloneRGXToken)(this.token, (0, immutability_utils_1.depthDecrement)(depth, 1)), this.exclusions.clone((0, immutability_utils_1.depthDecrement)(depth, 1)), (0, clone_1.cloneRGXToken)(this.terminal, (0, immutability_utils_1.depthDecrement)(depth, 1)));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.RGXExclusionToken = RGXExclusionToken;
|
|
53
|
+
RGXExclusionToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXExclusionToken);
|
|
54
|
+
RGXExclusionToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXExclusionToken);
|
|
55
|
+
exports.rgxExclusion = (0, internal_1.createConstructFunction)(RGXExclusionToken);
|
package/dist/class/index.d.ts
CHANGED
package/dist/class/index.js
CHANGED
package/dist/class/init.js
CHANGED
|
@@ -9,6 +9,7 @@ const lookaround_1 = require("./lookaround");
|
|
|
9
9
|
const errors_1 = require("../errors");
|
|
10
10
|
const lookahead_1 = require("./lookahead");
|
|
11
11
|
const lookbehind_1 = require("./lookbehind");
|
|
12
|
+
const exclusion_1 = require("./exclusion");
|
|
12
13
|
function rgxClassInit() {
|
|
13
14
|
// Patch RGXClassToken here, Since classes like RGXClassUnionToken are instances of RGXClassToken
|
|
14
15
|
// themselves. If we tried to import RGXClassUnionToken in base.ts, it would cause a circular dependency.
|
|
@@ -61,4 +62,7 @@ function rgxClassInit() {
|
|
|
61
62
|
return this.negate();
|
|
62
63
|
return new lookbehind_1.RGXLookbehindToken([this], positive);
|
|
63
64
|
};
|
|
65
|
+
base_1.RGXClassToken.prototype.subtract = function (exclusionId, exclusions = [], terminal = null) {
|
|
66
|
+
return new exclusion_1.RGXExclusionToken(exclusionId, this, exclusions, terminal);
|
|
67
|
+
};
|
|
64
68
|
}
|
package/dist/collection.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RGXConvertibleToken, RGXToken, ValidRegexString } from "./types";
|
|
2
|
+
import { ResolveRGXTokenOptions } from "./resolve";
|
|
2
3
|
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
3
4
|
import { Collection } from "@ptolemy2002/ts-utils";
|
|
4
5
|
export type RGXTokenCollectionMode = 'union' | 'concat';
|
|
@@ -9,7 +10,7 @@ export declare class RGXTokenCollection implements Collection<RGXToken>, RGXConv
|
|
|
9
10
|
static check: (value: unknown) => value is RGXTokenCollection;
|
|
10
11
|
static assert: (value: unknown) => asserts value is RGXTokenCollection;
|
|
11
12
|
constructor(tokens?: RGXTokenCollectionInput, mode?: RGXTokenCollectionMode);
|
|
12
|
-
resolve(): ValidRegexString;
|
|
13
|
+
resolve(options?: ResolveRGXTokenOptions): ValidRegexString;
|
|
13
14
|
toRgx(): RegExp;
|
|
14
15
|
getTokens(): RGXToken[];
|
|
15
16
|
clone(depth?: CloneDepth): RGXTokenCollection;
|
package/dist/collection.js
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import { RGXConvertibleToken, RGXToken } from "../types";
|
|
2
|
-
export
|
|
2
|
+
export type CreateRGXBoundsOptions = {
|
|
3
|
+
flags?: string;
|
|
4
|
+
anchorStart?: boolean;
|
|
5
|
+
anchorEnd?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare function createRGXBounds(positive: RGXToken, negative: RGXToken, options?: CreateRGXBoundsOptions | string | boolean): [RGXConvertibleToken, RGXConvertibleToken];
|
|
@@ -22,10 +22,15 @@ function convertToBound(token) {
|
|
|
22
22
|
toRgx: () => token
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
-
function createRGXBounds(positive, negative,
|
|
25
|
+
function createRGXBounds(positive, negative, options = {}) {
|
|
26
|
+
if (typeof options === "string")
|
|
27
|
+
options = { flags: options };
|
|
28
|
+
else if (typeof options === "boolean")
|
|
29
|
+
options = { anchorStart: options, anchorEnd: options };
|
|
30
|
+
const { flags = "", anchorStart = true, anchorEnd = true } = options;
|
|
26
31
|
const resolvedPositive = (0, resolve_1.resolveRGXToken)(convertNoGroupWrap(positive), { groupWrap: false, currentFlags: flags });
|
|
27
32
|
const resolvedNegative = (0, resolve_1.resolveRGXToken)(convertNoGroupWrap(negative), { groupWrap: false, currentFlags: flags });
|
|
28
|
-
const startBound = convertToBound((0, createRegex_1.createRegex)(`(?<=${resolvedNegative})(?=${resolvedPositive})`, flags, true));
|
|
29
|
-
const endBound = convertToBound((0, createRegex_1.createRegex)(`(?<=${resolvedPositive})(?=${resolvedNegative})`, flags, true));
|
|
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));
|
|
30
35
|
return [startBound, endBound];
|
|
31
36
|
}
|