arktype 2.0.0-dev.12 → 2.0.0-dev.12-cjs
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/out/api.js +19 -5
- package/out/ark.js +13 -10
- package/out/config.js +17 -1
- package/out/generic.js +8 -4
- package/out/match.js +5 -1
- package/out/module.js +5 -2
- package/out/package.json +3 -0
- package/out/parser/definition.js +19 -14
- package/out/parser/generic.js +11 -7
- package/out/parser/objectLiteral.js +23 -17
- package/out/parser/semantic/bounds.js +5 -1
- package/out/parser/semantic/divisor.js +2 -1
- package/out/parser/semantic/infer.js +2 -1
- package/out/parser/semantic/utils.js +2 -1
- package/out/parser/semantic/validate.js +7 -2
- package/out/parser/string/reduce/dynamic.js +24 -20
- package/out/parser/string/reduce/shared.js +17 -9
- package/out/parser/string/reduce/static.js +2 -1
- package/out/parser/string/shift/operand/date.js +18 -10
- package/out/parser/string/shift/operand/enclosed.js +17 -12
- package/out/parser/string/shift/operand/genericArgs.js +10 -5
- package/out/parser/string/shift/operand/operand.js +15 -11
- package/out/parser/string/shift/operand/unenclosed.js +30 -22
- package/out/parser/string/shift/operator/bounds.js +35 -27
- package/out/parser/string/shift/operator/divisor.js +11 -6
- package/out/parser/string/shift/operator/operator.js +18 -13
- package/out/parser/string/shift/scanner.js +8 -4
- package/out/parser/string/string.js +12 -7
- package/out/parser/tuple.js +46 -35
- package/out/scope.d.ts +3 -3
- package/out/scope.js +30 -24
- package/out/type.d.ts +1 -1
- package/out/type.js +13 -9
- package/package.json +6 -14
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseOperand = void 0;
|
|
4
|
+
const scanner_js_1 = require("../scanner.js");
|
|
5
|
+
const enclosed_js_1 = require("./enclosed.js");
|
|
6
|
+
const unenclosed_js_1 = require("./unenclosed.js");
|
|
7
|
+
const parseOperand = (s) => s.scanner.lookahead === "" ? s.error((0, unenclosed_js_1.writeMissingOperandMessage)(s))
|
|
5
8
|
: s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
|
|
6
|
-
: s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
|
|
7
|
-
: s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens) ?
|
|
8
|
-
parseOperand(s.shiftedByOne())
|
|
9
|
+
: s.scanner.lookaheadIsIn(enclosed_js_1.enclosingChar) ? (0, enclosed_js_1.parseEnclosed)(s, s.scanner.shift())
|
|
10
|
+
: s.scanner.lookaheadIsIn(scanner_js_1.Scanner.whiteSpaceTokens) ?
|
|
11
|
+
(0, exports.parseOperand)(s.shiftedByOne())
|
|
9
12
|
: s.scanner.lookahead === "d" ?
|
|
10
|
-
s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar) ?
|
|
11
|
-
parseEnclosed(s, `d${s.scanner.shift()}`)
|
|
12
|
-
: parseUnenclosed(s)
|
|
13
|
-
: parseUnenclosed(s);
|
|
13
|
+
s.shiftedByOne().scanner.lookaheadIsIn(enclosed_js_1.enclosingChar) ?
|
|
14
|
+
(0, enclosed_js_1.parseEnclosed)(s, `d${s.scanner.shift()}`)
|
|
15
|
+
: (0, unenclosed_js_1.parseUnenclosed)(s)
|
|
16
|
+
: (0, unenclosed_js_1.parseUnenclosed)(s);
|
|
17
|
+
exports.parseOperand = parseOperand;
|
|
@@ -1,56 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeExpressionExpectedMessage = exports.writeMissingRightOperandMessage = exports.writeMissingOperandMessage = exports.parseGenericInstantiation = exports.parseUnenclosed = void 0;
|
|
4
|
+
const schema_1 = require("@arktype/schema");
|
|
5
|
+
const util_1 = require("@arktype/util");
|
|
6
|
+
const validate_js_1 = require("../../../semantic/validate.js");
|
|
7
|
+
const genericArgs_js_1 = require("./genericArgs.js");
|
|
8
|
+
const parseUnenclosed = (s) => {
|
|
6
9
|
const token = s.scanner.shiftUntilNextTerminator();
|
|
7
10
|
if (token === "keyof")
|
|
8
11
|
s.addPrefix("keyof");
|
|
9
12
|
else
|
|
10
13
|
s.root = unenclosedToNode(s, token);
|
|
11
14
|
};
|
|
12
|
-
|
|
15
|
+
exports.parseUnenclosed = parseUnenclosed;
|
|
16
|
+
const parseGenericInstantiation = (name, g, s) => {
|
|
13
17
|
s.scanner.shiftUntilNonWhitespace();
|
|
14
18
|
const lookahead = s.scanner.shift();
|
|
15
19
|
if (lookahead !== "<")
|
|
16
|
-
return s.error(writeInvalidGenericArgsMessage(name, g.params, []));
|
|
17
|
-
const parsedArgs = parseGenericArgs(name, g.params, s);
|
|
20
|
+
return s.error((0, genericArgs_js_1.writeInvalidGenericArgsMessage)(name, g.params, []));
|
|
21
|
+
const parsedArgs = (0, genericArgs_js_1.parseGenericArgs)(name, g.params, s);
|
|
18
22
|
const remainingChars = parsedArgs.unscanned.length;
|
|
19
23
|
// set the scanner position to where the args scanner left off
|
|
20
24
|
s.scanner.jumpToIndex(remainingChars === 0 ? s.scanner.length : -remainingChars);
|
|
21
25
|
return g(...parsedArgs.result);
|
|
22
26
|
};
|
|
27
|
+
exports.parseGenericInstantiation = parseGenericInstantiation;
|
|
23
28
|
const unenclosedToNode = (s, token) => maybeParseReference(s, token) ??
|
|
24
29
|
maybeParseUnenclosedLiteral(s, token) ??
|
|
25
|
-
s.error(token === "" ? writeMissingOperandMessage(s)
|
|
30
|
+
s.error(token === "" ? (0, exports.writeMissingOperandMessage)(s)
|
|
26
31
|
: token[0] === "#" ?
|
|
27
|
-
writePrefixedPrivateReferenceMessage(token)
|
|
28
|
-
: writeUnresolvableMessage(token));
|
|
32
|
+
(0, validate_js_1.writePrefixedPrivateReferenceMessage)(token)
|
|
33
|
+
: (0, schema_1.writeUnresolvableMessage)(token));
|
|
29
34
|
const maybeParseReference = (s, token) => {
|
|
30
35
|
if (s.ctx.args?.[token])
|
|
31
36
|
return s.ctx.args[token].raw;
|
|
32
37
|
const resolution = s.ctx.$.maybeResolve(token);
|
|
33
|
-
if (resolution instanceof BaseRoot)
|
|
38
|
+
if (resolution instanceof schema_1.BaseRoot)
|
|
34
39
|
return resolution;
|
|
35
40
|
if (resolution === undefined)
|
|
36
41
|
return;
|
|
37
|
-
if (hasArkKind(resolution, "generic"))
|
|
38
|
-
return parseGenericInstantiation(token, resolution, s);
|
|
39
|
-
return throwParseError(`Unexpected resolution ${printable(resolution)}`);
|
|
42
|
+
if ((0, schema_1.hasArkKind)(resolution, "generic"))
|
|
43
|
+
return (0, exports.parseGenericInstantiation)(token, resolution, s);
|
|
44
|
+
return (0, util_1.throwParseError)(`Unexpected resolution ${(0, util_1.printable)(resolution)}`);
|
|
40
45
|
};
|
|
41
46
|
const maybeParseUnenclosedLiteral = (s, token) => {
|
|
42
|
-
const maybeNumber = tryParseNumber(token, { strict: true });
|
|
47
|
+
const maybeNumber = (0, util_1.tryParseNumber)(token, { strict: true });
|
|
43
48
|
if (maybeNumber !== undefined)
|
|
44
49
|
return s.ctx.$.node("unit", { unit: maybeNumber });
|
|
45
|
-
const maybeBigint = tryParseWellFormedBigint(token);
|
|
50
|
+
const maybeBigint = (0, util_1.tryParseWellFormedBigint)(token);
|
|
46
51
|
if (maybeBigint !== undefined)
|
|
47
52
|
return s.ctx.$.node("unit", { unit: maybeBigint });
|
|
48
53
|
};
|
|
49
|
-
|
|
54
|
+
const writeMissingOperandMessage = (s) => {
|
|
50
55
|
const operator = s.previousOperator();
|
|
51
56
|
return operator ?
|
|
52
|
-
writeMissingRightOperandMessage(operator, s.scanner.unscanned)
|
|
53
|
-
: writeExpressionExpectedMessage(s.scanner.unscanned);
|
|
57
|
+
(0, exports.writeMissingRightOperandMessage)(operator, s.scanner.unscanned)
|
|
58
|
+
: (0, exports.writeExpressionExpectedMessage)(s.scanner.unscanned);
|
|
54
59
|
};
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
exports.writeMissingOperandMessage = writeMissingOperandMessage;
|
|
61
|
+
const writeMissingRightOperandMessage = (token, unscanned = "") => `Token '${token}' requires a right operand${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
62
|
+
exports.writeMissingRightOperandMessage = writeMissingRightOperandMessage;
|
|
63
|
+
const writeExpressionExpectedMessage = (unscanned) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
64
|
+
exports.writeExpressionExpectedMessage = writeExpressionExpectedMessage;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeInvalidLimitMessage = exports.parseRightBound = exports.singleEqualsMessage = exports.getBoundKinds = exports.writeIncompatibleRangeMessage = exports.comparatorStartChars = exports.parseBound = void 0;
|
|
4
|
+
const schema_1 = require("@arktype/schema");
|
|
5
|
+
const util_1 = require("@arktype/util");
|
|
6
|
+
const shared_js_1 = require("../../reduce/shared.js");
|
|
7
|
+
const date_js_1 = require("../operand/date.js");
|
|
8
|
+
const parseBound = (s, start) => {
|
|
6
9
|
const comparator = shiftComparator(s, start);
|
|
7
10
|
if (s.root.hasKind("unit")) {
|
|
8
11
|
if (typeof s.root.unit === "number") {
|
|
@@ -17,54 +20,57 @@ export const parseBound = (s, start) => {
|
|
|
17
20
|
return;
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
|
-
return parseRightBound(s, comparator);
|
|
23
|
+
return (0, exports.parseRightBound)(s, comparator);
|
|
21
24
|
};
|
|
25
|
+
exports.parseBound = parseBound;
|
|
22
26
|
const oneCharComparators = {
|
|
23
27
|
"<": true,
|
|
24
28
|
">": true
|
|
25
29
|
};
|
|
26
|
-
|
|
30
|
+
exports.comparatorStartChars = {
|
|
27
31
|
"<": 1,
|
|
28
32
|
">": 1,
|
|
29
33
|
"=": 1
|
|
30
34
|
};
|
|
31
35
|
const shiftComparator = (s, start) => s.scanner.lookaheadIs("=") ? `${start}${s.scanner.shift()}`
|
|
32
|
-
: isKeyOf(start, oneCharComparators) ? start
|
|
33
|
-
: s.error(singleEqualsMessage);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
: (0, util_1.isKeyOf)(start, oneCharComparators) ? start
|
|
37
|
+
: s.error(exports.singleEqualsMessage);
|
|
38
|
+
const writeIncompatibleRangeMessage = (l, r) => `Bound kinds ${l} and ${r} are incompatible`;
|
|
39
|
+
exports.writeIncompatibleRangeMessage = writeIncompatibleRangeMessage;
|
|
40
|
+
const getBoundKinds = (comparator, limit, root, boundKind) => {
|
|
41
|
+
if (root.extends(schema_1.tsKeywords.number)) {
|
|
37
42
|
if (typeof limit !== "number") {
|
|
38
|
-
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
43
|
+
return (0, util_1.throwParseError)((0, exports.writeInvalidLimitMessage)(comparator, limit, boundKind));
|
|
39
44
|
}
|
|
40
45
|
return (comparator === "==" ? ["min", "max"]
|
|
41
46
|
: comparator[0] === ">" ? ["min"]
|
|
42
47
|
: ["max"]);
|
|
43
48
|
}
|
|
44
|
-
if (root.extends(internalKeywords.lengthBoundable)) {
|
|
49
|
+
if (root.extends(schema_1.internalKeywords.lengthBoundable)) {
|
|
45
50
|
if (typeof limit !== "number") {
|
|
46
|
-
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
51
|
+
return (0, util_1.throwParseError)((0, exports.writeInvalidLimitMessage)(comparator, limit, boundKind));
|
|
47
52
|
}
|
|
48
53
|
return (comparator === "==" ? ["minLength", "maxLength"]
|
|
49
54
|
: comparator[0] === ">" ? ["minLength"]
|
|
50
55
|
: ["maxLength"]);
|
|
51
56
|
}
|
|
52
|
-
if (root.extends(jsObjects.Date)) {
|
|
57
|
+
if (root.extends(schema_1.jsObjects.Date)) {
|
|
53
58
|
// allow either numeric or date limits
|
|
54
59
|
return (comparator === "==" ? ["after", "before"]
|
|
55
60
|
: comparator[0] === ">" ? ["after"]
|
|
56
61
|
: ["before"]);
|
|
57
62
|
}
|
|
58
|
-
return throwParseError(writeUnboundableMessage(root.expression));
|
|
63
|
+
return (0, util_1.throwParseError)((0, schema_1.writeUnboundableMessage)(root.expression));
|
|
59
64
|
};
|
|
60
|
-
|
|
65
|
+
exports.getBoundKinds = getBoundKinds;
|
|
66
|
+
exports.singleEqualsMessage = "= is not a valid comparator. Use == to check for equality";
|
|
61
67
|
const openLeftBoundToRoot = (leftBound) => ({
|
|
62
|
-
rule: isDateLiteral(leftBound.limit) ?
|
|
63
|
-
extractDateLiteralSource(leftBound.limit)
|
|
68
|
+
rule: (0, date_js_1.isDateLiteral)(leftBound.limit) ?
|
|
69
|
+
(0, date_js_1.extractDateLiteralSource)(leftBound.limit)
|
|
64
70
|
: leftBound.limit,
|
|
65
71
|
exclusive: leftBound.comparator.length === 1
|
|
66
72
|
});
|
|
67
|
-
|
|
73
|
+
const parseRightBound = (s, comparator) => {
|
|
68
74
|
// store the node that will be bounded
|
|
69
75
|
const previousRoot = s.unsetRoot();
|
|
70
76
|
const previousScannerIndex = s.scanner.location;
|
|
@@ -76,20 +82,22 @@ export const parseRightBound = (s, comparator) => {
|
|
|
76
82
|
s.root = previousRoot;
|
|
77
83
|
if (!limitNode.hasKind("unit") ||
|
|
78
84
|
(typeof limitNode.unit !== "number" && !(limitNode.unit instanceof Date)))
|
|
79
|
-
return s.error(writeInvalidLimitMessage(comparator, limitToken, "right"));
|
|
85
|
+
return s.error((0, exports.writeInvalidLimitMessage)(comparator, limitToken, "right"));
|
|
80
86
|
const limit = limitNode.unit;
|
|
81
87
|
// apply the newly-parsed right bound
|
|
82
88
|
const exclusive = comparator.length === 1;
|
|
83
89
|
// if the comparator is ==, both the min and max of that pair will be applied
|
|
84
|
-
for (const kind of getBoundKinds(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right"))
|
|
90
|
+
for (const kind of (0, exports.getBoundKinds)(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right"))
|
|
85
91
|
s.constrainRoot(kind, { rule: limit, exclusive });
|
|
86
92
|
if (!s.branches.leftBound)
|
|
87
93
|
return;
|
|
88
94
|
// if there's an open left bound, perform additional validation and apply it
|
|
89
|
-
if (!isKeyOf(comparator, maxComparators))
|
|
90
|
-
return s.error(writeUnpairableComparatorMessage(comparator));
|
|
91
|
-
const lowerBoundKind = getBoundKinds(s.branches.leftBound.comparator, s.branches.leftBound.limit, previousRoot, "left");
|
|
95
|
+
if (!(0, util_1.isKeyOf)(comparator, shared_js_1.maxComparators))
|
|
96
|
+
return s.error((0, shared_js_1.writeUnpairableComparatorMessage)(comparator));
|
|
97
|
+
const lowerBoundKind = (0, exports.getBoundKinds)(s.branches.leftBound.comparator, s.branches.leftBound.limit, previousRoot, "left");
|
|
92
98
|
s.constrainRoot(lowerBoundKind[0], openLeftBoundToRoot(s.branches.leftBound));
|
|
93
99
|
s.branches.leftBound = null;
|
|
94
100
|
};
|
|
95
|
-
|
|
101
|
+
exports.parseRightBound = parseRightBound;
|
|
102
|
+
const writeInvalidLimitMessage = (comparator, limit, boundKind) => `Comparator ${boundKind === "left" ? shared_js_1.invertedComparators[comparator] : comparator} must be ${boundKind === "left" ? "preceded" : "followed"} by a corresponding literal (was ${limit})`;
|
|
103
|
+
exports.writeInvalidLimitMessage = writeInvalidLimitMessage;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeInvalidDivisorMessage = exports.parseDivisor = void 0;
|
|
4
|
+
const util_1 = require("@arktype/util");
|
|
5
|
+
const parseDivisor = (s) => {
|
|
3
6
|
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
4
|
-
const divisor = tryParseInteger(divisorToken, {
|
|
5
|
-
errorOnFail: writeInvalidDivisorMessage(divisorToken)
|
|
7
|
+
const divisor = (0, util_1.tryParseInteger)(divisorToken, {
|
|
8
|
+
errorOnFail: (0, exports.writeInvalidDivisorMessage)(divisorToken)
|
|
6
9
|
});
|
|
7
10
|
if (divisor === 0)
|
|
8
|
-
s.error(writeInvalidDivisorMessage(0));
|
|
11
|
+
s.error((0, exports.writeInvalidDivisorMessage)(0));
|
|
9
12
|
s.root = s.root.constrain("divisor", divisor);
|
|
10
13
|
};
|
|
11
|
-
|
|
14
|
+
exports.parseDivisor = parseDivisor;
|
|
15
|
+
const writeInvalidDivisorMessage = (divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
16
|
+
exports.writeInvalidDivisorMessage = writeInvalidDivisorMessage;
|
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.incompleteArrayTokenMessage = exports.writeUnexpectedCharacterMessage = exports.parseOperator = void 0;
|
|
4
|
+
const util_1 = require("@arktype/util");
|
|
5
|
+
const scanner_js_1 = require("../scanner.js");
|
|
6
|
+
const bounds_js_1 = require("./bounds.js");
|
|
7
|
+
const divisor_js_1 = require("./divisor.js");
|
|
8
|
+
const parseOperator = (s) => {
|
|
6
9
|
const lookahead = s.scanner.shift();
|
|
7
10
|
return (lookahead === "" ? s.finalize("")
|
|
8
11
|
: lookahead === "[" ?
|
|
9
12
|
s.scanner.shift() === "]" ?
|
|
10
13
|
s.setRoot(s.root.array())
|
|
11
|
-
: s.error(incompleteArrayTokenMessage)
|
|
14
|
+
: s.error(exports.incompleteArrayTokenMessage)
|
|
12
15
|
: lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead)
|
|
13
16
|
: lookahead === ")" ? s.finalizeGroup()
|
|
14
|
-
: Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
|
|
17
|
+
: scanner_js_1.Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
|
|
15
18
|
s.finalize(lookahead)
|
|
16
|
-
: isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
|
|
17
|
-
: lookahead === "%" ? parseDivisor(s)
|
|
18
|
-
: lookahead === " " ? parseOperator(s)
|
|
19
|
-
: s.error(writeUnexpectedCharacterMessage(lookahead)));
|
|
19
|
+
: (0, util_1.isKeyOf)(lookahead, bounds_js_1.comparatorStartChars) ? (0, bounds_js_1.parseBound)(s, lookahead)
|
|
20
|
+
: lookahead === "%" ? (0, divisor_js_1.parseDivisor)(s)
|
|
21
|
+
: lookahead === " " ? (0, exports.parseOperator)(s)
|
|
22
|
+
: s.error((0, exports.writeUnexpectedCharacterMessage)(lookahead)));
|
|
20
23
|
};
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
exports.parseOperator = parseOperator;
|
|
25
|
+
const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
|
|
26
|
+
exports.writeUnexpectedCharacterMessage = writeUnexpectedCharacterMessage;
|
|
27
|
+
exports.incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Scanner = void 0;
|
|
4
|
+
const util_1 = require("@arktype/util");
|
|
5
|
+
class Scanner {
|
|
3
6
|
chars;
|
|
4
7
|
i;
|
|
5
8
|
constructor(def) {
|
|
@@ -58,6 +61,7 @@ export class Scanner {
|
|
|
58
61
|
return this.lookahead in tokens;
|
|
59
62
|
}
|
|
60
63
|
}
|
|
64
|
+
exports.Scanner = Scanner;
|
|
61
65
|
(function (Scanner) {
|
|
62
66
|
Scanner.lookaheadIsTerminator = (scanner) => scanner.lookahead in Scanner.terminatingChars;
|
|
63
67
|
Scanner.lookaheadIsNotWhitespace = (scanner) => !(scanner.lookahead in Scanner.whiteSpaceTokens);
|
|
@@ -91,7 +95,7 @@ export class Scanner {
|
|
|
91
95
|
unscanned[1] === "="
|
|
92
96
|
// if > is the end of a generic instantiation, the next token will be an operator or the end of the string
|
|
93
97
|
: unscanned.trimStart() === "" ||
|
|
94
|
-
isKeyOf(unscanned.trimStart()[0], Scanner.terminatingChars)
|
|
98
|
+
(0, util_1.isKeyOf)(unscanned.trimStart()[0], Scanner.terminatingChars)
|
|
95
99
|
// if the lookahead is a finalizing token but not >, it's unambiguously a finalizer (currently just ",")
|
|
96
100
|
: lookahead === ",";
|
|
97
|
-
})(Scanner || (Scanner = {}));
|
|
101
|
+
})(Scanner || (exports.Scanner = Scanner = {}));
|
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseUntilFinalizer = exports.fullStringParse = void 0;
|
|
4
|
+
const util_1 = require("@arktype/util");
|
|
5
|
+
const operator_js_1 = require("./shift/operator/operator.js");
|
|
6
|
+
const fullStringParse = (s) => {
|
|
4
7
|
s.parseOperand();
|
|
5
|
-
const result = parseUntilFinalizer(s).root;
|
|
8
|
+
const result = (0, exports.parseUntilFinalizer)(s).root;
|
|
6
9
|
if (!result) {
|
|
7
|
-
return throwInternalError(`Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`);
|
|
10
|
+
return (0, util_1.throwInternalError)(`Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`);
|
|
8
11
|
}
|
|
9
12
|
s.scanner.shiftUntilNonWhitespace();
|
|
10
13
|
if (s.scanner.lookahead) {
|
|
11
14
|
// throw a parse error if non-whitespace characters made it here without being parsed
|
|
12
|
-
throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead));
|
|
15
|
+
(0, util_1.throwParseError)((0, operator_js_1.writeUnexpectedCharacterMessage)(s.scanner.lookahead));
|
|
13
16
|
}
|
|
14
17
|
return result;
|
|
15
18
|
};
|
|
16
|
-
|
|
19
|
+
exports.fullStringParse = fullStringParse;
|
|
20
|
+
const parseUntilFinalizer = (s) => {
|
|
17
21
|
while (s.finalizer === undefined)
|
|
18
22
|
next(s);
|
|
19
23
|
return s;
|
|
20
24
|
};
|
|
25
|
+
exports.parseUntilFinalizer = parseUntilFinalizer;
|
|
21
26
|
const next = (s) => s.hasRoot() ? s.parseOperator() : s.parseOperand();
|
package/out/parser/tuple.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeInvalidConstructorMessage = exports.parseNarrowTuple = exports.writeMalformedFunctionalExpressionMessage = exports.parseMorphTuple = exports.parseKeyOfTuple = exports.spreadOptionalMessage = exports.optionalPostVariadicMessage = exports.requiredPostOptionalMessage = exports.multipleVariadicMesage = exports.writeNonArraySpreadMessage = exports.parseTupleLiteral = exports.parseTuple = void 0;
|
|
4
|
+
const schema_1 = require("@arktype/schema");
|
|
5
|
+
const util_1 = require("@arktype/util");
|
|
6
|
+
const unenclosed_js_1 = require("./string/shift/operand/unenclosed.js");
|
|
7
|
+
const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? (0, exports.parseTupleLiteral)(def, ctx);
|
|
8
|
+
exports.parseTuple = parseTuple;
|
|
9
|
+
const parseTupleLiteral = (def, ctx) => {
|
|
6
10
|
let sequences = [{}];
|
|
7
11
|
let i = 0;
|
|
8
12
|
while (i < def.length) {
|
|
@@ -16,19 +20,19 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
16
20
|
i++;
|
|
17
21
|
if (def[i] === "?") {
|
|
18
22
|
if (spread)
|
|
19
|
-
return throwParseError(spreadOptionalMessage);
|
|
23
|
+
return (0, util_1.throwParseError)(exports.spreadOptionalMessage);
|
|
20
24
|
optional = true;
|
|
21
25
|
i++;
|
|
22
26
|
}
|
|
23
27
|
if (spread) {
|
|
24
|
-
if (!element.extends(jsObjects.Array))
|
|
25
|
-
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
28
|
+
if (!element.extends(schema_1.jsObjects.Array))
|
|
29
|
+
return (0, util_1.throwParseError)((0, exports.writeNonArraySpreadMessage)(element.expression));
|
|
26
30
|
// a spread must be distributed over branches e.g.:
|
|
27
31
|
// def: [string, ...(number[] | [true, false])]
|
|
28
32
|
// nodes: [string, ...number[]] | [string, true, false]
|
|
29
33
|
sequences = sequences.flatMap(base =>
|
|
30
34
|
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
31
|
-
element.branches.map(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
35
|
+
element.branches.map(branch => appendSpreadBranch((0, schema_1.makeRootAndArrayPropertiesMutable)(base), branch)));
|
|
32
36
|
}
|
|
33
37
|
else {
|
|
34
38
|
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
@@ -39,36 +43,37 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
39
43
|
sequence
|
|
40
44
|
})));
|
|
41
45
|
};
|
|
46
|
+
exports.parseTupleLiteral = parseTupleLiteral;
|
|
42
47
|
const appendElement = (base, kind, element) => {
|
|
43
48
|
switch (kind) {
|
|
44
49
|
case "required":
|
|
45
50
|
if (base.optionals)
|
|
46
51
|
// e.g. [string?, number]
|
|
47
|
-
return throwParseError(requiredPostOptionalMessage);
|
|
52
|
+
return (0, util_1.throwParseError)(exports.requiredPostOptionalMessage);
|
|
48
53
|
if (base.variadic) {
|
|
49
54
|
// e.g. [...string[], number]
|
|
50
|
-
base.postfix = append(base.postfix, element);
|
|
55
|
+
base.postfix = (0, util_1.append)(base.postfix, element);
|
|
51
56
|
}
|
|
52
57
|
else {
|
|
53
58
|
// e.g. [string, number]
|
|
54
|
-
base.prefix = append(base.prefix, element);
|
|
59
|
+
base.prefix = (0, util_1.append)(base.prefix, element);
|
|
55
60
|
}
|
|
56
61
|
return base;
|
|
57
62
|
case "optional":
|
|
58
63
|
if (base.variadic)
|
|
59
64
|
// e.g. [...string[], number?]
|
|
60
|
-
return throwParseError(optionalPostVariadicMessage);
|
|
65
|
+
return (0, util_1.throwParseError)(exports.optionalPostVariadicMessage);
|
|
61
66
|
// e.g. [string, number?]
|
|
62
|
-
base.optionals = append(base.optionals, element);
|
|
67
|
+
base.optionals = (0, util_1.append)(base.optionals, element);
|
|
63
68
|
return base;
|
|
64
69
|
case "variadic":
|
|
65
70
|
// e.g. [...string[], number, ...string[]]
|
|
66
71
|
if (base.postfix)
|
|
67
|
-
throwParseError(multipleVariadicMesage);
|
|
72
|
+
(0, util_1.throwParseError)(exports.multipleVariadicMesage);
|
|
68
73
|
if (base.variadic) {
|
|
69
74
|
if (!base.variadic.equals(element)) {
|
|
70
75
|
// e.g. [...string[], ...number[]]
|
|
71
|
-
throwParseError(multipleVariadicMesage);
|
|
76
|
+
(0, util_1.throwParseError)(exports.multipleVariadicMesage);
|
|
72
77
|
}
|
|
73
78
|
// e.g. [...string[], ...string[]]
|
|
74
79
|
// do nothing, second spread doesn't change the type
|
|
@@ -84,7 +89,7 @@ const appendSpreadBranch = (base, branch) => {
|
|
|
84
89
|
const spread = branch.firstReferenceOfKind("sequence");
|
|
85
90
|
if (!spread) {
|
|
86
91
|
// the only array with no sequence reference is unknown[]
|
|
87
|
-
return appendElement(base, "variadic", tsKeywords.unknown);
|
|
92
|
+
return appendElement(base, "variadic", schema_1.tsKeywords.unknown);
|
|
88
93
|
}
|
|
89
94
|
spread.prefix.forEach(node => appendElement(base, "required", node));
|
|
90
95
|
spread.optionals.forEach(node => appendElement(base, "optional", node));
|
|
@@ -110,55 +115,60 @@ const maybeParseTupleExpression = (def, ctx) => {
|
|
|
110
115
|
// )
|
|
111
116
|
// : tupleExpressionResult
|
|
112
117
|
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
119
|
+
exports.writeNonArraySpreadMessage = writeNonArraySpreadMessage;
|
|
120
|
+
exports.multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
121
|
+
exports.requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
122
|
+
exports.optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
123
|
+
exports.spreadOptionalMessage = "A spread element cannot be optional";
|
|
124
|
+
const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
|
|
125
|
+
exports.parseKeyOfTuple = parseKeyOfTuple;
|
|
119
126
|
const parseBranchTuple = (def, ctx) => {
|
|
120
127
|
if (def[2] === undefined)
|
|
121
|
-
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
128
|
+
return (0, util_1.throwParseError)((0, unenclosed_js_1.writeMissingRightOperandMessage)(def[1], ""));
|
|
122
129
|
const l = ctx.$.parse(def[0], ctx);
|
|
123
130
|
const r = ctx.$.parse(def[2], ctx);
|
|
124
131
|
return def[1] === "&" ? l.and(r) : l.or(r);
|
|
125
132
|
};
|
|
126
133
|
const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
|
|
127
134
|
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
128
|
-
|
|
135
|
+
const parseMorphTuple = (def, ctx) => {
|
|
129
136
|
if (typeof def[2] !== "function") {
|
|
130
|
-
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
137
|
+
return (0, util_1.throwParseError)((0, exports.writeMalformedFunctionalExpressionMessage)("=>", def[2]));
|
|
131
138
|
}
|
|
132
139
|
// TODO: nested morphs?
|
|
133
140
|
return ctx.$.parse(def[0], ctx).pipe(def[2]);
|
|
134
141
|
};
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
exports.parseMorphTuple = parseMorphTuple;
|
|
143
|
+
const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
144
|
+
exports.writeMalformedFunctionalExpressionMessage = writeMalformedFunctionalExpressionMessage;
|
|
145
|
+
const parseNarrowTuple = (def, ctx) => {
|
|
137
146
|
if (typeof def[2] !== "function") {
|
|
138
|
-
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
147
|
+
return (0, util_1.throwParseError)((0, exports.writeMalformedFunctionalExpressionMessage)(":", def[2]));
|
|
139
148
|
}
|
|
140
149
|
return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
|
|
141
150
|
};
|
|
151
|
+
exports.parseNarrowTuple = parseNarrowTuple;
|
|
142
152
|
const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
|
|
143
153
|
const indexOneParsers = {
|
|
144
154
|
"|": parseBranchTuple,
|
|
145
155
|
"&": parseBranchTuple,
|
|
146
156
|
"[]": parseArrayTuple,
|
|
147
|
-
":": parseNarrowTuple,
|
|
148
|
-
"=>": parseMorphTuple,
|
|
157
|
+
":": exports.parseNarrowTuple,
|
|
158
|
+
"=>": exports.parseMorphTuple,
|
|
149
159
|
"@": parseAttributeTuple
|
|
150
160
|
};
|
|
151
161
|
const prefixParsers = {
|
|
152
|
-
keyof: parseKeyOfTuple,
|
|
162
|
+
keyof: exports.parseKeyOfTuple,
|
|
153
163
|
instanceof: (def, ctx) => {
|
|
154
164
|
if (typeof def[1] !== "function") {
|
|
155
|
-
return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
|
|
165
|
+
return (0, util_1.throwParseError)((0, exports.writeInvalidConstructorMessage)((0, util_1.objectKindOrDomainOf)(def[1])));
|
|
156
166
|
}
|
|
157
167
|
const branches = def
|
|
158
168
|
.slice(1)
|
|
159
169
|
.map(ctor => typeof ctor === "function" ?
|
|
160
170
|
ctx.$.node("proto", { proto: ctor })
|
|
161
|
-
: throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
|
|
171
|
+
: (0, util_1.throwParseError)((0, exports.writeInvalidConstructorMessage)((0, util_1.objectKindOrDomainOf)(ctor))));
|
|
162
172
|
return branches.length === 1 ?
|
|
163
173
|
branches[0]
|
|
164
174
|
: ctx.$.node("union", { branches });
|
|
@@ -166,4 +176,5 @@ const prefixParsers = {
|
|
|
166
176
|
"===": (def, ctx) => ctx.$.units(def.slice(1))
|
|
167
177
|
};
|
|
168
178
|
const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
|
|
169
|
-
|
|
179
|
+
const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
|
|
180
|
+
exports.writeInvalidConstructorMessage = writeInvalidConstructorMessage;
|
package/out/scope.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { type MatchParser } from "./match.js";
|
|
|
6
6
|
import type { Module } from "./module.js";
|
|
7
7
|
import { type inferDefinition, type validateDefinition } from "./parser/definition.js";
|
|
8
8
|
import { parseGenericParams, type GenericDeclaration, type GenericParamsParseError } from "./parser/generic.js";
|
|
9
|
-
import {
|
|
9
|
+
import { RawTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.js";
|
|
10
10
|
export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkConfig) => Scope<inferBootstrapped<bootstrapAliases<def>>>;
|
|
11
11
|
type validateScope<def> = {
|
|
12
12
|
[k in keyof def]: k extends symbol ? unknown : parseScopeKey<k>["params"] extends [] ? def[k] extends Type | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? keyError<writeDuplicateAliasError<name>> : validateDefinition<def[k], ambient & bootstrapAliases<def>, {}> : parseScopeKey<k>["params"] extends GenericParamsParseError ? parseScopeKey<k>["params"][0] : validateDefinition<def[k], ambient & bootstrapAliases<def>, {
|
|
@@ -57,10 +57,10 @@ export interface Scope<$ = any> extends RootScope<$> {
|
|
|
57
57
|
export declare class RawScope<$ extends RawRootResolutions = RawRootResolutions> extends RawRootScope<$> {
|
|
58
58
|
private parseCache;
|
|
59
59
|
constructor(def: Record<string, unknown>, config?: ArkConfig);
|
|
60
|
-
type:
|
|
60
|
+
type: RawTypeParser;
|
|
61
61
|
match: MatchParser<$>;
|
|
62
62
|
declare: () => {
|
|
63
|
-
type:
|
|
63
|
+
type: RawTypeParser;
|
|
64
64
|
};
|
|
65
65
|
define: (def: unknown) => unknown;
|
|
66
66
|
preparseRoot(def: unknown): unknown;
|