@ptolemy2002/rgx 13.7.0 → 13.9.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 +13 -0
- package/dist/class/exclusion.d.ts +4 -1
- package/dist/class/exclusion.js +27 -0
- package/dist/class/group.d.ts +4 -0
- package/dist/class/group.js +30 -0
- package/dist/class/init.d.ts +3 -0
- package/dist/class/init.js +24 -0
- package/dist/class/lookahead.d.ts +4 -1
- package/dist/class/lookahead.js +14 -2
- package/dist/class/lookaround.d.ts +2 -0
- package/dist/class/lookaround.js +13 -0
- package/dist/class/lookbehind.d.ts +4 -1
- package/dist/class/lookbehind.js +14 -2
- package/dist/class/repeat.d.ts +4 -1
- package/dist/class/repeat.js +23 -0
- package/dist/class/subpattern.d.ts +4 -0
- package/dist/class/subpattern.js +15 -0
- package/dist/class/toRGXClassToken.js +4 -0
- package/dist/class/union.d.ts +4 -0
- package/dist/class/union.js +29 -1
- package/dist/class/wrapper.d.ts +4 -1
- package/dist/class/wrapper.js +19 -2
- package/dist/errors/base.d.ts +1 -1
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.js +5 -0
- package/dist/errors/invalidJSONClassKey.d.ts +6 -0
- package/dist/errors/invalidJSONClassKey.js +15 -0
- package/dist/errors/invalidJSONToken.d.ts +4 -0
- package/dist/errors/invalidJSONToken.js +12 -0
- package/dist/errors/invalidToken.js +15 -2
- package/dist/errors/jsonClassArgsValidationFailed.d.ts +8 -0
- package/dist/errors/jsonClassArgsValidationFailed.js +20 -0
- package/dist/errors/jsonClassConflict.d.ts +6 -0
- package/dist/errors/jsonClassConflict.js +15 -0
- package/dist/errors/notJSONSerializable.d.ts +7 -0
- package/dist/errors/notJSONSerializable.js +26 -0
- package/dist/flag-transformer/accentInsensitive.js +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/internal/assureAcceptance.js +4 -1
- package/dist/json.d.ts +35 -0
- package/dist/json.js +208 -0
- package/dist/resolve.js +4 -0
- package/dist/typeGuards.d.ts +27 -10
- package/dist/typeGuards.js +208 -36
- package/dist/types.d.ts +26 -5
- package/dist/types.js +3 -1
- package/dist/walker/base.d.ts +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXInvalidJSONClassKeyError = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
class RGXInvalidJSONClassKeyError extends base_1.RGXError {
|
|
6
|
+
constructor(message, got) {
|
|
7
|
+
super(message, 'INVALID_JSON_CLASS_KEY');
|
|
8
|
+
this.name = 'RGXInvalidJSONClassKeyError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
}
|
|
11
|
+
calcMessage(message) {
|
|
12
|
+
return `${message}; Got: ${JSON.stringify(this.got)}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RGXInvalidJSONClassKeyError = RGXInvalidJSONClassKeyError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXInvalidJSONTokenError = void 0;
|
|
4
|
+
const invalidToken_1 = require("./invalidToken");
|
|
5
|
+
class RGXInvalidJSONTokenError extends invalidToken_1.RGXInvalidTokenError {
|
|
6
|
+
constructor(message, expected = { type: "tokenType", values: ["json"] }, got = undefined) {
|
|
7
|
+
super(message, expected, got);
|
|
8
|
+
this.name = 'RGXInvalidJSONTokenError';
|
|
9
|
+
this.code = 'INVALID_RGX_JSON_TOKEN';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.RGXInvalidJSONTokenError = RGXInvalidJSONTokenError;
|
|
@@ -10,7 +10,8 @@ const tokenExpectationMap = {
|
|
|
10
10
|
'native': ['string', 'number', 'boolean', 'null', 'undefined'],
|
|
11
11
|
'convertible': ['object with a toRgx method that returns a valid native/literal token or an array of valid native/literal tokens'],
|
|
12
12
|
'array': ['array of native/literal/convertible tokens'],
|
|
13
|
-
'class': ['instance of RGXClassToken']
|
|
13
|
+
'class': ['instance of RGXClassToken'],
|
|
14
|
+
'json': ['JSON-serializable native token', 'object with $rgx set to true and either a regex source (with optional flags) or a registered class name (with optional args)', 'array of JSON tokens']
|
|
14
15
|
};
|
|
15
16
|
class RGXInvalidTokenError extends errors_1.RGXError {
|
|
16
17
|
setExpected(expected) {
|
|
@@ -46,7 +47,19 @@ class RGXInvalidTokenError extends errors_1.RGXError {
|
|
|
46
47
|
this.setExpected(expected);
|
|
47
48
|
}
|
|
48
49
|
calcMessage(message) {
|
|
49
|
-
|
|
50
|
+
let gotString;
|
|
51
|
+
if (class_1.RGXClassToken.check(this.got)) {
|
|
52
|
+
gotString = `instance of ${this.got.constructor.name}`;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
try {
|
|
56
|
+
gotString = JSON.stringify(this.got) ?? String(this.got);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// Values like BigInts and circular structures cannot be stringified as JSON.
|
|
60
|
+
gotString = String(this.got);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
50
63
|
return `${message}; Expected: ${this.expected}; Got: [${gotString}]`;
|
|
51
64
|
}
|
|
52
65
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RGXError } from "./base";
|
|
2
|
+
export declare class RGXJSONClassArgsValidationFailedError extends RGXError {
|
|
3
|
+
className: string;
|
|
4
|
+
args: unknown[];
|
|
5
|
+
reason: string | null;
|
|
6
|
+
constructor(className: string, args: unknown[], reason?: string | null);
|
|
7
|
+
calcMessage(message: string): string;
|
|
8
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXJSONClassArgsValidationFailedError = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
class RGXJSONClassArgsValidationFailedError extends base_1.RGXError {
|
|
6
|
+
constructor(className, args, reason = null) {
|
|
7
|
+
super("JSON class argument validation failed", 'JSON_CLASS_ARGS_VALIDATION_FAILED');
|
|
8
|
+
this.name = 'RGXJSONClassArgsValidationFailedError';
|
|
9
|
+
this.className = className;
|
|
10
|
+
this.args = args;
|
|
11
|
+
this.reason = reason;
|
|
12
|
+
}
|
|
13
|
+
calcMessage(message) {
|
|
14
|
+
let result = `${message}; Class: ${JSON.stringify(this.className)}; Args: ${JSON.stringify(this.args)}`;
|
|
15
|
+
if (this.reason !== null)
|
|
16
|
+
result += `; Reason: ${this.reason}`;
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.RGXJSONClassArgsValidationFailedError = RGXJSONClassArgsValidationFailedError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXJSONClassConflictError = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
class RGXJSONClassConflictError extends base_1.RGXError {
|
|
6
|
+
constructor(message, got) {
|
|
7
|
+
super(message, 'JSON_CLASS_CONFLICT');
|
|
8
|
+
this.name = 'RGXJSONClassConflictError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
}
|
|
11
|
+
calcMessage(message) {
|
|
12
|
+
return `${message}; Got: ${JSON.stringify(this.got)}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RGXJSONClassConflictError = RGXJSONClassConflictError;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXNotJSONSerializableError = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
class RGXNotJSONSerializableError extends base_1.RGXError {
|
|
6
|
+
constructor(message, got, reason = null) {
|
|
7
|
+
super(message, 'NOT_JSON_SERIALIZABLE');
|
|
8
|
+
this.name = 'RGXNotJSONSerializableError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
this.reason = reason;
|
|
11
|
+
}
|
|
12
|
+
calcMessage(message) {
|
|
13
|
+
let gotString;
|
|
14
|
+
try {
|
|
15
|
+
gotString = JSON.stringify(this.got) ?? String(this.got);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
gotString = String(this.got);
|
|
19
|
+
}
|
|
20
|
+
let result = `${message}; Got: ${gotString}`;
|
|
21
|
+
if (this.reason !== null)
|
|
22
|
+
result += `; Reason: ${this.reason}`;
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.RGXNotJSONSerializableError = RGXNotJSONSerializableError;
|
|
@@ -5,11 +5,11 @@ const constants_1 = require("../constants");
|
|
|
5
5
|
const resolve_1 = require("../resolve");
|
|
6
6
|
const utils_1 = require("../utils");
|
|
7
7
|
const accentPatterns = [
|
|
8
|
-
"(a|á|à|ä|â|ã)", "(A|Á|À|Ä|Â|Ã)",
|
|
9
|
-
"(e|é|è|ë|ê)", "(E|É|È|Ë|Ê)",
|
|
10
|
-
"(i|í|ì|ï|î)", "(I|Í|Ì|Ï|Î)",
|
|
11
|
-
"(o|ó|ò|ö|ô|õ)", "(O|Ó|Ò|Ö|Ô|Õ)",
|
|
12
|
-
"(u|ú|ù|ü|û)", "(U|Ú|Ù|Ü|Û)"
|
|
8
|
+
"(?:a|á|à|ä|â|ã)", "(?:A|Á|À|Ä|Â|Ã)",
|
|
9
|
+
"(?:e|é|è|ë|ê)", "(?:E|É|È|Ë|Ê)",
|
|
10
|
+
"(?:i|í|ì|ï|î)", "(?:I|Í|Ì|Ï|Î)",
|
|
11
|
+
"(?:o|ó|ò|ö|ô|õ)", "(?:O|Ó|Ò|Ö|Ô|Õ)",
|
|
12
|
+
"(?:u|ú|ù|ü|û)", "(?:U|Ú|Ù|Ü|Û)"
|
|
13
13
|
];
|
|
14
14
|
const nonEscapeBound = (0, resolve_1.resolveRGXToken)((0, constants_1.rgxConstant)("non-escape-bound"));
|
|
15
15
|
const nonLocalizedFlagBound = (0, resolve_1.resolveRGXToken)({
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -58,6 +58,7 @@ __exportStar(require("./typeGuards"), exports);
|
|
|
58
58
|
__exportStar(require("./collection"), exports);
|
|
59
59
|
__exportStar(require("./class"), exports);
|
|
60
60
|
__exportStar(require("./resolve"), exports);
|
|
61
|
+
__exportStar(require("./json"), exports);
|
|
61
62
|
__exportStar(require("./concat"), exports);
|
|
62
63
|
__exportStar(require("./utils"), exports);
|
|
63
64
|
__exportStar(require("./ExtRegExp"), exports);
|
|
@@ -4,9 +4,12 @@ exports.assureAcceptance = assureAcceptance;
|
|
|
4
4
|
const class_1 = require("../class");
|
|
5
5
|
const errors_1 = require("../errors");
|
|
6
6
|
const typeGuards_1 = require("../typeGuards");
|
|
7
|
+
const json_1 = require("../json");
|
|
7
8
|
function assureAcceptance(tokens, flags) {
|
|
8
9
|
for (let i = 0; i < tokens.length; i++) {
|
|
9
|
-
|
|
10
|
+
// JSON class tokens become class tokens, which may have insertion preferences.
|
|
11
|
+
const rawToken = tokens[i];
|
|
12
|
+
const token = (0, typeGuards_1.isRGXJSONClassToken)(rawToken, false) ? (0, json_1.rgxTokenFromJSON)(rawToken) : rawToken;
|
|
10
13
|
if ((0, typeGuards_1.isRGXConvertibleToken)(token) && token.rgxAcceptInsertion) {
|
|
11
14
|
const messageOrAccepted = token.rgxAcceptInsertion(tokens, flags);
|
|
12
15
|
if (messageOrAccepted === true)
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as t from "./types";
|
|
2
|
+
import { RGXClassToken } from "./class";
|
|
3
|
+
export type RGXJSONClassArgsValidator = (args: t.RGXJSONValue[]) => boolean | string;
|
|
4
|
+
export type RGXJSONClassConstructor = (args: t.RGXJSONValue[]) => RGXClassToken;
|
|
5
|
+
export type RGXJSONClassRegistryEntry = {
|
|
6
|
+
validateArgs: RGXJSONClassArgsValidator;
|
|
7
|
+
construct: RGXJSONClassConstructor;
|
|
8
|
+
};
|
|
9
|
+
export type RGXJSONClassTokenConstructor = {
|
|
10
|
+
validateJSONArgs: RGXJSONClassArgsValidator;
|
|
11
|
+
fromJSON: (json: t.RGXJSONClassToken) => RGXClassToken;
|
|
12
|
+
};
|
|
13
|
+
export type RGXJSONBuiltinClassName = "RGXClassWrapperToken" | "RGXClassUnionToken" | "RGXGroupToken" | "RGXRepeatToken" | "RGXLookaheadToken" | "RGXLookbehindToken" | "RGXExclusionToken" | "RGXSubpatternToken";
|
|
14
|
+
export type RGXJSONClassName = RGXJSONBuiltinClassName | (string & {});
|
|
15
|
+
export type RGXTokenToJSONOptions = {
|
|
16
|
+
resolveConvertible?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare function listRGXJSONClasses(): string[];
|
|
19
|
+
export declare function hasRGXJSONClass(name: RGXJSONClassName): boolean;
|
|
20
|
+
export declare function assertHasRGXJSONClass(name: RGXJSONClassName): void;
|
|
21
|
+
export declare function assertNotHasRGXJSONClass(name: RGXJSONClassName): void;
|
|
22
|
+
export declare function registerRGXJSONClass(name: RGXJSONClassName, entry: RGXJSONClassRegistryEntry): RGXJSONClassRegistryEntry;
|
|
23
|
+
export declare function registerRGXJSONClassToken(name: RGXJSONClassName, constructor: RGXJSONClassTokenConstructor): RGXJSONClassRegistryEntry;
|
|
24
|
+
export declare function getRGXJSONClass(name: RGXJSONClassName): RGXJSONClassRegistryEntry;
|
|
25
|
+
export declare function unregisterRGXJSONClass(name: RGXJSONClassName): void;
|
|
26
|
+
export declare function validateRGXJSONClassArgs(name: RGXJSONClassName, args: t.RGXJSONValue[]): true | string;
|
|
27
|
+
export declare function assertValidRGXJSONClassArgs(name: RGXJSONClassName, args: t.RGXJSONValue[]): void;
|
|
28
|
+
export declare function createRGXJSONClassToken(name: RGXJSONClassName, args?: t.RGXJSONValue[]): t.RGXJSONClassToken;
|
|
29
|
+
export declare function createRGXJSONLiteralToken(source: string, flags?: string): t.RGXJSONLiteralToken;
|
|
30
|
+
export declare function assertRGXJSONClassTokenOf(json: unknown, name: RGXJSONClassName): asserts json is t.RGXJSONClassToken;
|
|
31
|
+
export declare function rgxJSONClassArgs(json: unknown, name: RGXJSONClassName, validate: RGXJSONClassArgsValidator): t.RGXJSONValue[];
|
|
32
|
+
export declare function rgxTokenToJSON(token: t.RGXToken, { resolveConvertible }?: RGXTokenToJSONOptions): t.RGXJSONToken;
|
|
33
|
+
export declare function rgxTokenFromJSON(json: t.RGXJSONToken): t.RGXNonJSONToken;
|
|
34
|
+
export declare function rgxTokenToJSONString(token: t.RGXToken, options?: RGXTokenToJSONOptions, space?: string | number): string;
|
|
35
|
+
export declare function rgxTokenFromJSONString(json: string): t.RGXNonJSONToken;
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.listRGXJSONClasses = listRGXJSONClasses;
|
|
37
|
+
exports.hasRGXJSONClass = hasRGXJSONClass;
|
|
38
|
+
exports.assertHasRGXJSONClass = assertHasRGXJSONClass;
|
|
39
|
+
exports.assertNotHasRGXJSONClass = assertNotHasRGXJSONClass;
|
|
40
|
+
exports.registerRGXJSONClass = registerRGXJSONClass;
|
|
41
|
+
exports.registerRGXJSONClassToken = registerRGXJSONClassToken;
|
|
42
|
+
exports.getRGXJSONClass = getRGXJSONClass;
|
|
43
|
+
exports.unregisterRGXJSONClass = unregisterRGXJSONClass;
|
|
44
|
+
exports.validateRGXJSONClassArgs = validateRGXJSONClassArgs;
|
|
45
|
+
exports.assertValidRGXJSONClassArgs = assertValidRGXJSONClassArgs;
|
|
46
|
+
exports.createRGXJSONClassToken = createRGXJSONClassToken;
|
|
47
|
+
exports.createRGXJSONLiteralToken = createRGXJSONLiteralToken;
|
|
48
|
+
exports.assertRGXJSONClassTokenOf = assertRGXJSONClassTokenOf;
|
|
49
|
+
exports.rgxJSONClassArgs = rgxJSONClassArgs;
|
|
50
|
+
exports.rgxTokenToJSON = rgxTokenToJSON;
|
|
51
|
+
exports.rgxTokenFromJSON = rgxTokenFromJSON;
|
|
52
|
+
exports.rgxTokenToJSONString = rgxTokenToJSONString;
|
|
53
|
+
exports.rgxTokenFromJSONString = rgxTokenFromJSONString;
|
|
54
|
+
const t = __importStar(require("./types"));
|
|
55
|
+
const e = __importStar(require("./errors"));
|
|
56
|
+
const tg = __importStar(require("./typeGuards"));
|
|
57
|
+
const class_1 = require("./class");
|
|
58
|
+
const collection_1 = require("./collection");
|
|
59
|
+
const ExtRegExp_1 = require("./ExtRegExp");
|
|
60
|
+
const resolve_1 = require("./resolve");
|
|
61
|
+
const rgxJSONClasses = {};
|
|
62
|
+
// ------------------ Registry ------------------
|
|
63
|
+
function listRGXJSONClasses() {
|
|
64
|
+
return Object.keys(rgxJSONClasses);
|
|
65
|
+
}
|
|
66
|
+
function hasRGXJSONClass(name) {
|
|
67
|
+
return name in rgxJSONClasses;
|
|
68
|
+
}
|
|
69
|
+
function assertHasRGXJSONClass(name) {
|
|
70
|
+
if (!hasRGXJSONClass(name)) {
|
|
71
|
+
throw new e.RGXInvalidJSONClassKeyError("JSON class with name not registered.", name);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function assertNotHasRGXJSONClass(name) {
|
|
75
|
+
if (hasRGXJSONClass(name)) {
|
|
76
|
+
throw new e.RGXJSONClassConflictError("JSON class with name already registered.", name);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function registerRGXJSONClass(name, entry) {
|
|
80
|
+
assertNotHasRGXJSONClass(name);
|
|
81
|
+
rgxJSONClasses[name] = entry;
|
|
82
|
+
return entry;
|
|
83
|
+
}
|
|
84
|
+
function registerRGXJSONClassToken(name, constructor) {
|
|
85
|
+
return registerRGXJSONClass(name, {
|
|
86
|
+
validateArgs: args => constructor.validateJSONArgs(args),
|
|
87
|
+
construct: args => constructor.fromJSON(createRGXJSONClassToken(name, args))
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function getRGXJSONClass(name) {
|
|
91
|
+
assertHasRGXJSONClass(name);
|
|
92
|
+
return rgxJSONClasses[name];
|
|
93
|
+
}
|
|
94
|
+
function unregisterRGXJSONClass(name) {
|
|
95
|
+
assertHasRGXJSONClass(name);
|
|
96
|
+
delete rgxJSONClasses[name];
|
|
97
|
+
}
|
|
98
|
+
function validateRGXJSONClassArgs(name, args) {
|
|
99
|
+
const result = getRGXJSONClass(name).validateArgs(args);
|
|
100
|
+
if (result === true)
|
|
101
|
+
return true;
|
|
102
|
+
if (result === false)
|
|
103
|
+
return "Argument validation failed.";
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
function assertValidRGXJSONClassArgs(name, args) {
|
|
107
|
+
const result = validateRGXJSONClassArgs(name, args);
|
|
108
|
+
if (result !== true)
|
|
109
|
+
throw new e.RGXJSONClassArgsValidationFailedError(name, args, result);
|
|
110
|
+
}
|
|
111
|
+
// ------------------ Helpers for class token implementations ------------------
|
|
112
|
+
function createRGXJSONClassToken(name, args = []) {
|
|
113
|
+
return { [t.RGX_JSON_FLAG]: true, class: name, args };
|
|
114
|
+
}
|
|
115
|
+
function createRGXJSONLiteralToken(source, flags = '') {
|
|
116
|
+
tg.assertValidRegexString(source);
|
|
117
|
+
if (flags === '')
|
|
118
|
+
return { [t.RGX_JSON_FLAG]: true, source };
|
|
119
|
+
return { [t.RGX_JSON_FLAG]: true, source, flags };
|
|
120
|
+
}
|
|
121
|
+
function assertRGXJSONClassTokenOf(json, name) {
|
|
122
|
+
tg.assertRGXJSONClassToken(json, false);
|
|
123
|
+
if (json.class !== name) {
|
|
124
|
+
throw new e.RGXInvalidJSONTokenError("JSON class token is for a different class", { type: "custom", values: [`JSON class token with class ${JSON.stringify(name)}`] }, json);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Extracts the args from a JSON class token after checking that it targets the given class and that
|
|
128
|
+
// its args pass the given validator. Throws RGXJSONClassArgsValidationFailedError if validation fails.
|
|
129
|
+
function rgxJSONClassArgs(json, name, validate) {
|
|
130
|
+
assertRGXJSONClassTokenOf(json, name);
|
|
131
|
+
const args = json.args ?? [];
|
|
132
|
+
const result = validate(args);
|
|
133
|
+
if (result === true)
|
|
134
|
+
return args;
|
|
135
|
+
throw new e.RGXJSONClassArgsValidationFailedError(name, args, result === false ? "Argument validation failed." : result);
|
|
136
|
+
}
|
|
137
|
+
// ------------------ Conversion ------------------
|
|
138
|
+
function rgxTokenToJSON(token, { resolveConvertible = true } = {}) {
|
|
139
|
+
if (tg.isRGXNoOpToken(token))
|
|
140
|
+
return null;
|
|
141
|
+
if (tg.isRGXNativeToken(token)) {
|
|
142
|
+
if (typeof token === "number" && !Number.isFinite(token)) {
|
|
143
|
+
throw new e.RGXNotJSONSerializableError("Cannot convert token to JSON", token, "Non-finite numbers are not JSON-serializable.");
|
|
144
|
+
}
|
|
145
|
+
return token;
|
|
146
|
+
}
|
|
147
|
+
if (tg.isRGXLiteralToken(token)) {
|
|
148
|
+
// The source of an ExtRegExp already has any custom flag transformations applied,
|
|
149
|
+
// so only the vanilla flags are kept to avoid applying the transformations twice.
|
|
150
|
+
return createRGXJSONLiteralToken(token.source, (0, ExtRegExp_1.extractVanillaRegexFlags)(token.flags));
|
|
151
|
+
}
|
|
152
|
+
if (tg.isRGXJSONObjectToken(token, false)) {
|
|
153
|
+
tg.assertRGXJSONObjectToken(token);
|
|
154
|
+
return token;
|
|
155
|
+
}
|
|
156
|
+
if (class_1.RGXClassToken.check(token))
|
|
157
|
+
return token.toJSON();
|
|
158
|
+
if (collection_1.RGXTokenCollection.check(token))
|
|
159
|
+
return (0, class_1.toRGXClassToken)(token).toJSON();
|
|
160
|
+
if (tg.isRGXConvertibleToken(token, false)) {
|
|
161
|
+
if (!resolveConvertible) {
|
|
162
|
+
throw new e.RGXNotJSONSerializableError("Cannot convert token to JSON", token, "Convertible tokens that are not class tokens can only be converted by resolving them, but resolveConvertible is false.");
|
|
163
|
+
}
|
|
164
|
+
return createRGXJSONLiteralToken((0, resolve_1.resolveRGXToken)(token, { groupWrap: false }));
|
|
165
|
+
}
|
|
166
|
+
if (tg.isRGXArrayToken(token, false)) {
|
|
167
|
+
return token.map(item => rgxTokenToJSON(item, { resolveConvertible }));
|
|
168
|
+
}
|
|
169
|
+
// Ignoring this line since it should be impossible to reach if the types are correct.
|
|
170
|
+
/* istanbul ignore next */
|
|
171
|
+
throw new e.RGXInvalidTokenError("Invalid RGX token", null, token);
|
|
172
|
+
}
|
|
173
|
+
function rgxTokenFromJSON(json) {
|
|
174
|
+
if (tg.isRGXJSONNativeToken(json))
|
|
175
|
+
return json;
|
|
176
|
+
if (tg.isRGXJSONLiteralToken(json, false)) {
|
|
177
|
+
tg.assertRGXJSONLiteralToken(json);
|
|
178
|
+
return new ExtRegExp_1.ExtRegExp(json.source, json.flags ?? '');
|
|
179
|
+
}
|
|
180
|
+
if (tg.isRGXJSONClassToken(json, false)) {
|
|
181
|
+
const args = json.args ?? [];
|
|
182
|
+
assertValidRGXJSONClassArgs(json.class, args);
|
|
183
|
+
return getRGXJSONClass(json.class).construct(args);
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(json))
|
|
186
|
+
return json.map(item => rgxTokenFromJSON(item));
|
|
187
|
+
throw new e.RGXInvalidJSONTokenError("Invalid RGX JSON token", { type: "tokenType", values: ["json"] }, json);
|
|
188
|
+
}
|
|
189
|
+
function rgxTokenToJSONString(token, options = {}, space) {
|
|
190
|
+
return JSON.stringify(rgxTokenToJSON(token, options), null, space);
|
|
191
|
+
}
|
|
192
|
+
function rgxTokenFromJSONString(json) {
|
|
193
|
+
let parsed;
|
|
194
|
+
try {
|
|
195
|
+
parsed = JSON.parse(json);
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
if (err instanceof SyntaxError) {
|
|
199
|
+
throw new e.RGXInvalidJSONTokenError(`Invalid JSON string: ${err.message}`, { type: "tokenType", values: ["json"] }, json);
|
|
200
|
+
}
|
|
201
|
+
// This is ignored because I don't know what kind of
|
|
202
|
+
// unexpected errors might happen.
|
|
203
|
+
/* istanbul ignore next */
|
|
204
|
+
throw err;
|
|
205
|
+
}
|
|
206
|
+
tg.assertRGXJSONToken(parsed, false);
|
|
207
|
+
return rgxTokenFromJSON(parsed);
|
|
208
|
+
}
|
package/dist/resolve.js
CHANGED
|
@@ -40,6 +40,7 @@ const e = __importStar(require("./errors"));
|
|
|
40
40
|
const ExtRegExp_1 = require("./ExtRegExp");
|
|
41
41
|
const tg = __importStar(require("./typeGuards"));
|
|
42
42
|
const internal_1 = require("./internal");
|
|
43
|
+
const json_1 = require("./json");
|
|
43
44
|
function escapeRegex(value) {
|
|
44
45
|
const result = value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
|
|
45
46
|
tg.assertValidRegexString(result);
|
|
@@ -77,6 +78,9 @@ function hasParenErrors(pattern) {
|
|
|
77
78
|
}
|
|
78
79
|
function resolveRGXToken(token, { groupWrap = true, topLevel = true, currentFlags = '' } = {}) {
|
|
79
80
|
(0, ExtRegExp_1.assertValidRegexFlags)(currentFlags);
|
|
81
|
+
// JSON tokens are converted to their runtime equivalents (literal or class tokens) before resolving.
|
|
82
|
+
if (tg.isRGXJSONObjectToken(token, false))
|
|
83
|
+
token = (0, json_1.rgxTokenFromJSON)(token);
|
|
80
84
|
let acceptParenErrors = false;
|
|
81
85
|
const innerResolve = () => {
|
|
82
86
|
if (tg.isRGXNoOpToken(token))
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -5,26 +5,43 @@ export declare function isRGXLiteralToken(value: unknown): value is t.RGXLiteral
|
|
|
5
5
|
export declare function assertRGXLiteralToken(value: unknown): asserts value is t.RGXLiteralToken;
|
|
6
6
|
export declare function isRGXNativeToken(value: unknown): value is t.RGXNativeToken;
|
|
7
7
|
export declare function assertRGXNativeToken(value: unknown): asserts value is t.RGXNativeToken;
|
|
8
|
-
export declare function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is t.RGXConvertibleToken;
|
|
9
|
-
export declare function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is t.RGXConvertibleToken;
|
|
10
|
-
export declare function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is t.RGXToken[];
|
|
11
|
-
export declare function assertRGXArrayToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXToken[];
|
|
12
|
-
export declare function
|
|
13
|
-
export declare function
|
|
8
|
+
export declare function isRGXConvertibleToken(value: unknown, returnCheck?: boolean, acceptJSON?: boolean): value is t.RGXConvertibleToken;
|
|
9
|
+
export declare function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean, acceptJSON?: boolean): asserts value is t.RGXConvertibleToken;
|
|
10
|
+
export declare function isRGXArrayToken(value: unknown, contentCheck?: boolean, acceptJSON?: boolean): value is t.RGXToken[];
|
|
11
|
+
export declare function assertRGXArrayToken(value: unknown, contentCheck?: boolean, acceptJSON?: boolean): asserts value is t.RGXToken[];
|
|
12
|
+
export declare function isRGXJSONValue(value: unknown): value is t.RGXJSONValue;
|
|
13
|
+
export declare function assertRGXJSONValue(value: unknown): asserts value is t.RGXJSONValue;
|
|
14
|
+
export declare function isRGXJSONNativeToken(value: unknown): value is t.RGXJSONNativeToken;
|
|
15
|
+
export declare function assertRGXJSONNativeToken(value: unknown): asserts value is t.RGXJSONNativeToken;
|
|
16
|
+
export declare function isRGXJSONLiteralToken(value: unknown, contentCheck?: boolean): value is t.RGXJSONLiteralToken;
|
|
17
|
+
export declare function assertRGXJSONLiteralToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXJSONLiteralToken;
|
|
18
|
+
export declare function isRGXJSONClassToken(value: unknown, registryCheck?: boolean): value is t.RGXJSONClassToken;
|
|
19
|
+
export declare function assertRGXJSONClassToken(value: unknown, registryCheck?: boolean): asserts value is t.RGXJSONClassToken;
|
|
20
|
+
export declare function isRGXJSONObjectToken(value: unknown, contentCheck?: boolean): value is t.RGXJSONObjectToken;
|
|
21
|
+
export declare function assertRGXJSONObjectToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXJSONObjectToken;
|
|
22
|
+
export declare function isRGXJSONToken(value: unknown, contentCheck?: boolean): value is t.RGXJSONToken;
|
|
23
|
+
export declare function assertRGXJSONToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXJSONToken;
|
|
24
|
+
export declare function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean, acceptJSON?: boolean): t.RGXTokenTypeFlat;
|
|
25
|
+
export declare function rgxTokenType(value: unknown, recognizeClass?: boolean, acceptJSON?: boolean): t.RGXTokenType;
|
|
14
26
|
export declare function rgxTokenFromType<T extends t.RGXTokenTypeGuardInput>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
15
27
|
export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
|
|
16
28
|
export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
|
|
17
|
-
export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is t.RGXTokenFromType<T>;
|
|
18
|
-
export declare function assertRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is t.RGXTokenFromType<T>;
|
|
19
|
-
export declare function isRGXGroupedToken(value: unknown, contentCheck?: boolean): value is t.RGXGroupedToken;
|
|
20
|
-
export declare function assertRGXGroupedToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXGroupedToken;
|
|
29
|
+
export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null, J extends boolean = true>(value: unknown, type?: T, matchLength?: boolean, acceptJSON?: J): value is t.RGXTokenFromType<T, J>;
|
|
30
|
+
export declare function assertRGXToken<T extends t.RGXTokenTypeGuardInput = null, J extends boolean = true>(value: unknown, type?: T, matchLength?: boolean, acceptJSON?: J): asserts value is t.RGXTokenFromType<T, J>;
|
|
31
|
+
export declare function isRGXGroupedToken(value: unknown, contentCheck?: boolean, acceptJSON?: boolean): value is t.RGXGroupedToken;
|
|
32
|
+
export declare function assertRGXGroupedToken(value: unknown, contentCheck?: boolean, acceptJSON?: boolean): asserts value is t.RGXGroupedToken;
|
|
21
33
|
export declare function isValidRegexString(value: string): value is t.ValidRegexString;
|
|
22
34
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
35
|
+
export declare function castValidRegexString(value: string): t.ValidRegexString;
|
|
23
36
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
|
24
37
|
export declare function assertValidVanillaRegexFlags(value: string): asserts value is t.ValidVanillaRegexFlags;
|
|
38
|
+
export declare function castValidVanillaRegexFlags(value: string): t.ValidVanillaRegexFlags;
|
|
25
39
|
export declare function isValidIdentifier(value: string): value is t.ValidIdentifier;
|
|
26
40
|
export declare function assertValidIdentifier(value: string): asserts value is t.ValidIdentifier;
|
|
41
|
+
export declare function castValidIdentifier(value: string): t.ValidIdentifier;
|
|
27
42
|
export declare function isValidRegexLocalizableFlagDiff(value: string): value is t.ValidRegexLocalizableFlagDiff;
|
|
28
43
|
export declare function assertValidRegexLocalizableFlagDiff(value: string): asserts value is t.ValidRegexLocalizableFlagDiff;
|
|
44
|
+
export declare function castValidRegexLocalizableFlagDiff(value: string): t.ValidRegexLocalizableFlagDiff;
|
|
29
45
|
export declare function isValidRegexLocalizableFlags(value: string): value is t.ValidRegexLocalizableFlags;
|
|
30
46
|
export declare function assertValidRegexLocalizableFlags(value: string): asserts value is t.ValidRegexLocalizableFlags;
|
|
47
|
+
export declare function castValidRegexLocalizableFlags(value: string): t.ValidRegexLocalizableFlags;
|