arktype 2.0.0-dev.12-cjs → 2.0.0-dev.14
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/LICENSE +7 -0
- package/out/api.d.ts +1 -1
- package/out/api.js +5 -19
- package/out/ark.js +10 -13
- package/out/config.js +1 -17
- package/out/generic.d.ts +1 -1
- package/out/generic.js +4 -8
- package/out/match.js +50 -56
- package/out/module.js +2 -5
- package/out/parser/definition.d.ts +1 -1
- package/out/parser/definition.js +14 -19
- package/out/parser/generic.js +7 -11
- package/out/parser/objectLiteral.d.ts +31 -18
- package/out/parser/objectLiteral.js +72 -43
- package/out/parser/semantic/bounds.js +1 -5
- package/out/parser/semantic/divisor.js +1 -2
- package/out/parser/semantic/infer.js +1 -2
- package/out/parser/semantic/utils.js +1 -2
- package/out/parser/semantic/validate.js +2 -7
- package/out/parser/string/reduce/dynamic.js +20 -24
- package/out/parser/string/reduce/shared.js +9 -17
- package/out/parser/string/reduce/static.js +1 -2
- package/out/parser/string/shift/operand/date.js +10 -18
- package/out/parser/string/shift/operand/enclosed.js +12 -17
- package/out/parser/string/shift/operand/genericArgs.js +5 -10
- package/out/parser/string/shift/operand/operand.d.ts +1 -1
- package/out/parser/string/shift/operand/operand.js +11 -15
- package/out/parser/string/shift/operand/unenclosed.js +22 -30
- package/out/parser/string/shift/operator/bounds.js +27 -35
- package/out/parser/string/shift/operator/divisor.js +6 -11
- package/out/parser/string/shift/operator/operator.js +13 -18
- package/out/parser/string/shift/scanner.d.ts +6 -5
- package/out/parser/string/shift/scanner.js +7 -8
- package/out/parser/string/string.js +7 -12
- package/out/parser/tuple.js +35 -58
- package/out/scope.d.ts +1 -2
- package/out/scope.js +24 -30
- package/out/type.js +9 -13
- package/package.json +33 -33
- package/out/main.d.ts +0 -6
- package/out/main.js +0 -5
- package/out/package.json +0 -3
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const string_js_1 = require("../string.js");
|
|
9
|
-
const shared_js_1 = require("./shared.js");
|
|
10
|
-
class DynamicState {
|
|
1
|
+
import { isKeyOf, throwInternalError, throwParseError } from "@arktype/util";
|
|
2
|
+
import { parseOperand } from "../shift/operand/operand.js";
|
|
3
|
+
import { parseOperator } from "../shift/operator/operator.js";
|
|
4
|
+
import { Scanner } from "../shift/scanner.js";
|
|
5
|
+
import { parseUntilFinalizer } from "../string.js";
|
|
6
|
+
import { invertedComparators, minComparators, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnclosedGroupMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
7
|
+
export class DynamicState {
|
|
11
8
|
ctx;
|
|
12
9
|
scanner;
|
|
13
10
|
// set root type to `any` so that all constraints can be applied
|
|
@@ -22,10 +19,10 @@ class DynamicState {
|
|
|
22
19
|
groups = [];
|
|
23
20
|
constructor(def, ctx) {
|
|
24
21
|
this.ctx = ctx;
|
|
25
|
-
this.scanner = new
|
|
22
|
+
this.scanner = new Scanner(def);
|
|
26
23
|
}
|
|
27
24
|
error(message) {
|
|
28
|
-
return
|
|
25
|
+
return throwParseError(message);
|
|
29
26
|
}
|
|
30
27
|
hasRoot() {
|
|
31
28
|
return this.root !== undefined;
|
|
@@ -43,16 +40,16 @@ class DynamicState {
|
|
|
43
40
|
}
|
|
44
41
|
finalize(finalizer) {
|
|
45
42
|
if (this.groups.length)
|
|
46
|
-
return this.error(
|
|
43
|
+
return this.error(writeUnclosedGroupMessage(")"));
|
|
47
44
|
this.finalizeBranches();
|
|
48
45
|
this.finalizer = finalizer;
|
|
49
46
|
}
|
|
50
47
|
reduceLeftBound(limit, comparator) {
|
|
51
|
-
const invertedComparator =
|
|
52
|
-
if (!
|
|
53
|
-
return this.error(
|
|
48
|
+
const invertedComparator = invertedComparators[comparator];
|
|
49
|
+
if (!isKeyOf(invertedComparator, minComparators))
|
|
50
|
+
return this.error(writeUnpairableComparatorMessage(comparator));
|
|
54
51
|
if (this.branches.leftBound) {
|
|
55
|
-
return this.error(
|
|
52
|
+
return this.error(writeMultipleLeftBoundsMessage(this.branches.leftBound.limit, this.branches.leftBound.comparator, limit, invertedComparator));
|
|
56
53
|
}
|
|
57
54
|
this.branches.leftBound = {
|
|
58
55
|
comparator: invertedComparator,
|
|
@@ -76,7 +73,7 @@ class DynamicState {
|
|
|
76
73
|
this.finalizeBranches();
|
|
77
74
|
const topBranchState = this.groups.pop();
|
|
78
75
|
if (!topBranchState)
|
|
79
|
-
return this.error(
|
|
76
|
+
return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned));
|
|
80
77
|
this.branches = topBranchState;
|
|
81
78
|
}
|
|
82
79
|
addPrefix(prefix) {
|
|
@@ -88,7 +85,7 @@ class DynamicState {
|
|
|
88
85
|
this.root =
|
|
89
86
|
lastPrefix === "keyof" ?
|
|
90
87
|
this.root.keyof()
|
|
91
|
-
:
|
|
88
|
+
: throwInternalError(`Unexpected prefix '${lastPrefix}'`);
|
|
92
89
|
}
|
|
93
90
|
}
|
|
94
91
|
pushRootToBranch(token) {
|
|
@@ -105,17 +102,17 @@ class DynamicState {
|
|
|
105
102
|
this.root = undefined;
|
|
106
103
|
}
|
|
107
104
|
parseUntilFinalizer() {
|
|
108
|
-
return
|
|
105
|
+
return parseUntilFinalizer(new DynamicState(this.scanner.unscanned, this.ctx));
|
|
109
106
|
}
|
|
110
107
|
parseOperator() {
|
|
111
|
-
return
|
|
108
|
+
return parseOperator(this);
|
|
112
109
|
}
|
|
113
110
|
parseOperand() {
|
|
114
|
-
return
|
|
111
|
+
return parseOperand(this);
|
|
115
112
|
}
|
|
116
113
|
assertRangeUnset() {
|
|
117
114
|
if (this.branches.leftBound) {
|
|
118
|
-
return this.error(
|
|
115
|
+
return this.error(writeOpenRangeMessage(this.branches.leftBound.limit, this.branches.leftBound.comparator));
|
|
119
116
|
}
|
|
120
117
|
}
|
|
121
118
|
reduceGroupOpen() {
|
|
@@ -139,4 +136,3 @@ class DynamicState {
|
|
|
139
136
|
return this;
|
|
140
137
|
}
|
|
141
138
|
}
|
|
142
|
-
exports.DynamicState = DynamicState;
|
|
@@ -1,35 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.writeMultipleLeftBoundsMessage = exports.writeUnpairableComparatorMessage = exports.writeOpenRangeMessage = exports.writeUnclosedGroupMessage = exports.writeUnmatchedGroupCloseMessage = exports.invertedComparators = exports.comparators = exports.maxComparators = exports.minComparators = void 0;
|
|
4
|
-
exports.minComparators = {
|
|
1
|
+
export const minComparators = {
|
|
5
2
|
">": true,
|
|
6
3
|
">=": true
|
|
7
4
|
};
|
|
8
|
-
|
|
5
|
+
export const maxComparators = {
|
|
9
6
|
"<": true,
|
|
10
7
|
"<=": true
|
|
11
8
|
};
|
|
12
|
-
|
|
9
|
+
export const comparators = {
|
|
13
10
|
">": true,
|
|
14
11
|
">=": true,
|
|
15
12
|
"<": true,
|
|
16
13
|
"<=": true,
|
|
17
14
|
"==": true
|
|
18
15
|
};
|
|
19
|
-
|
|
16
|
+
export const invertedComparators = {
|
|
20
17
|
"<": ">",
|
|
21
18
|
">": "<",
|
|
22
19
|
"<=": ">=",
|
|
23
20
|
">=": "<=",
|
|
24
21
|
"==": "=="
|
|
25
22
|
};
|
|
26
|
-
const writeUnmatchedGroupCloseMessage = (unscanned) => `Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`)}`;
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
exports.writeOpenRangeMessage = writeOpenRangeMessage;
|
|
32
|
-
const writeUnpairableComparatorMessage = (comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
33
|
-
exports.writeUnpairableComparatorMessage = writeUnpairableComparatorMessage;
|
|
34
|
-
const writeMultipleLeftBoundsMessage = (openLimit, openComparator, limit, comparator) => `An expression may have at most one left bound (parsed ${openLimit}${exports.invertedComparators[openComparator]}, ${limit}${exports.invertedComparators[comparator]})`;
|
|
35
|
-
exports.writeMultipleLeftBoundsMessage = writeMultipleLeftBoundsMessage;
|
|
23
|
+
export const writeUnmatchedGroupCloseMessage = (unscanned) => `Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`)}`;
|
|
24
|
+
export const writeUnclosedGroupMessage = (missingChar) => `Missing ${missingChar}`;
|
|
25
|
+
export const writeOpenRangeMessage = (min, comparator) => `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
26
|
+
export const writeUnpairableComparatorMessage = (comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
27
|
+
export const writeMultipleLeftBoundsMessage = (openLimit, openComparator, limit, comparator) => `An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.tryParseDate = exports.writeInvalidDateMessage = exports.extractDateLiteralSource = exports.isValidDate = exports.isDateLiteral = void 0;
|
|
4
|
-
const util_1 = require("@arktype/util");
|
|
5
|
-
const isDateLiteral = (value) => typeof value === "string" &&
|
|
1
|
+
import { throwParseError, tryParseNumber } from "@arktype/util";
|
|
2
|
+
export const isDateLiteral = (value) => typeof value === "string" &&
|
|
6
3
|
value[0] === "d" &&
|
|
7
4
|
(value[1] === "'" || value[1] === '"') &&
|
|
8
5
|
value.at(-1) === value[1];
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
exports.extractDateLiteralSource = extractDateLiteralSource;
|
|
14
|
-
const writeInvalidDateMessage = (source) => `'${source}' could not be parsed by the Date constructor`;
|
|
15
|
-
exports.writeInvalidDateMessage = writeInvalidDateMessage;
|
|
16
|
-
const tryParseDate = (source, errorOnFail) => maybeParseDate(source, errorOnFail);
|
|
17
|
-
exports.tryParseDate = tryParseDate;
|
|
6
|
+
export const isValidDate = (d) => d.toString() !== "Invalid Date";
|
|
7
|
+
export const extractDateLiteralSource = (literal) => literal.slice(2, -1);
|
|
8
|
+
export const writeInvalidDateMessage = (source) => `'${source}' could not be parsed by the Date constructor`;
|
|
9
|
+
export const tryParseDate = (source, errorOnFail) => maybeParseDate(source, errorOnFail);
|
|
18
10
|
const maybeParseDate = (source, errorOnFail) => {
|
|
19
11
|
const stringParsedDate = new Date(source);
|
|
20
|
-
if (
|
|
12
|
+
if (isValidDate(stringParsedDate))
|
|
21
13
|
return stringParsedDate;
|
|
22
|
-
const epochMillis =
|
|
14
|
+
const epochMillis = tryParseNumber(source);
|
|
23
15
|
if (epochMillis !== undefined) {
|
|
24
16
|
const numberParsedDate = new Date(epochMillis);
|
|
25
|
-
if (
|
|
17
|
+
if (isValidDate(numberParsedDate))
|
|
26
18
|
return numberParsedDate;
|
|
27
19
|
}
|
|
28
20
|
return errorOnFail ?
|
|
29
|
-
|
|
21
|
+
throwParseError(errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail)
|
|
30
22
|
: undefined;
|
|
31
23
|
};
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const date_js_1 = require("./date.js");
|
|
6
|
-
const parseEnclosed = (s, enclosing) => {
|
|
7
|
-
const enclosed = s.scanner.shiftUntil(exports.untilLookaheadIsClosing[exports.enclosingTokens[enclosing]]);
|
|
1
|
+
import { isKeyOf } from "@arktype/util";
|
|
2
|
+
import { tryParseDate, writeInvalidDateMessage } from "./date.js";
|
|
3
|
+
export const parseEnclosed = (s, enclosing) => {
|
|
4
|
+
const enclosed = s.scanner.shiftUntil(untilLookaheadIsClosing[enclosingTokens[enclosing]]);
|
|
8
5
|
if (s.scanner.lookahead === "")
|
|
9
|
-
return s.error(
|
|
6
|
+
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing));
|
|
10
7
|
// Shift the scanner one additional time for the second enclosing token
|
|
11
8
|
s.scanner.shift();
|
|
12
9
|
if (enclosing === "/") {
|
|
@@ -17,31 +14,30 @@ const parseEnclosed = (s, enclosing) => {
|
|
|
17
14
|
regex: enclosed
|
|
18
15
|
}, { prereduced: true });
|
|
19
16
|
}
|
|
20
|
-
else if (
|
|
17
|
+
else if (isKeyOf(enclosing, enclosingQuote))
|
|
21
18
|
s.root = s.ctx.$.node("unit", { unit: enclosed });
|
|
22
19
|
else {
|
|
23
|
-
const date =
|
|
20
|
+
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed));
|
|
24
21
|
s.root = s.ctx.$.node("unit", { unit: date, description: enclosed });
|
|
25
22
|
}
|
|
26
23
|
};
|
|
27
|
-
|
|
28
|
-
exports.enclosingQuote = {
|
|
24
|
+
export const enclosingQuote = {
|
|
29
25
|
"'": 1,
|
|
30
26
|
'"': 1
|
|
31
27
|
};
|
|
32
|
-
|
|
28
|
+
export const enclosingChar = {
|
|
33
29
|
"/": 1,
|
|
34
30
|
"'": 1,
|
|
35
31
|
'"': 1
|
|
36
32
|
};
|
|
37
|
-
|
|
33
|
+
export const enclosingTokens = {
|
|
38
34
|
"d'": "'",
|
|
39
35
|
'd"': '"',
|
|
40
36
|
"'": "'",
|
|
41
37
|
'"': '"',
|
|
42
38
|
"/": "/"
|
|
43
39
|
};
|
|
44
|
-
|
|
40
|
+
export const untilLookaheadIsClosing = {
|
|
45
41
|
"'": scanner => scanner.lookahead === `'`,
|
|
46
42
|
'"': scanner => scanner.lookahead === `"`,
|
|
47
43
|
"/": scanner => scanner.lookahead === `/`
|
|
@@ -51,5 +47,4 @@ const enclosingCharDescriptions = {
|
|
|
51
47
|
"'": "single-quote",
|
|
52
48
|
"/": "forward slash"
|
|
53
49
|
};
|
|
54
|
-
const writeUnterminatedEnclosedMessage = (fragment, enclosingStart) => `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[
|
|
55
|
-
exports.writeUnterminatedEnclosedMessage = writeUnterminatedEnclosedMessage;
|
|
50
|
+
export const writeUnterminatedEnclosedMessage = (fragment, enclosingStart) => `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[enclosingTokens[enclosingStart]]}`;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.writeInvalidGenericArgsMessage = exports.parseGenericArgs = void 0;
|
|
4
|
-
const shared_js_1 = require("../../reduce/shared.js");
|
|
5
|
-
const parseGenericArgs = (name, params, s) => _parseGenericArgs(name, params, s, [], []);
|
|
6
|
-
exports.parseGenericArgs = parseGenericArgs;
|
|
1
|
+
import { writeUnclosedGroupMessage } from "../../reduce/shared.js";
|
|
2
|
+
export const parseGenericArgs = (name, params, s) => _parseGenericArgs(name, params, s, [], []);
|
|
7
3
|
const _parseGenericArgs = (name, params, s, argDefs, argNodes) => {
|
|
8
4
|
const argState = s.parseUntilFinalizer();
|
|
9
5
|
// remove the finalizing token from the argDef
|
|
@@ -16,11 +12,10 @@ const _parseGenericArgs = (name, params, s, argDefs, argNodes) => {
|
|
|
16
12
|
unscanned: argState.scanner.unscanned
|
|
17
13
|
};
|
|
18
14
|
}
|
|
19
|
-
return argState.error(
|
|
15
|
+
return argState.error(writeInvalidGenericArgsMessage(name, params, argDefs));
|
|
20
16
|
}
|
|
21
17
|
if (argState.finalizer === ",")
|
|
22
18
|
return _parseGenericArgs(name, params, s, argDefs, argNodes);
|
|
23
|
-
return argState.error(
|
|
19
|
+
return argState.error(writeUnclosedGroupMessage(">"));
|
|
24
20
|
};
|
|
25
|
-
const writeInvalidGenericArgsMessage = (name, params, argDefs) => `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${argDefs.length}${argDefs.length === 0 ? "" : `: ${argDefs.join(", ")}`})`;
|
|
26
|
-
exports.writeInvalidGenericArgsMessage = writeInvalidGenericArgsMessage;
|
|
21
|
+
export const writeInvalidGenericArgsMessage = (name, params, argDefs) => `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${argDefs.length}${argDefs.length === 0 ? "" : `: ${argDefs.join(", ")}`})`;
|
|
@@ -2,7 +2,7 @@ import type { DynamicState } from "../../reduce/dynamic.js";
|
|
|
2
2
|
import type { StaticState, state } from "../../reduce/static.js";
|
|
3
3
|
import type { BaseCompletions } from "../../string.js";
|
|
4
4
|
import { Scanner } from "../scanner.js";
|
|
5
|
-
import { type EnclosingQuote, type EnclosingStartToken
|
|
5
|
+
import { parseEnclosed, type EnclosingQuote, type EnclosingStartToken } from "./enclosed.js";
|
|
6
6
|
import { parseUnenclosed } from "./unenclosed.js";
|
|
7
7
|
export declare const parseOperand: (s: DynamicState) => void;
|
|
8
8
|
export type parseOperand<s extends StaticState, $, args> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "(" ? state.reduceGroupOpen<s, unscanned> : lookahead extends EnclosingStartToken ? parseEnclosed<s, lookahead, unscanned> : lookahead extends Scanner.WhiteSpaceToken ? parseOperand<state.scanTo<s, unscanned>, $, args> : lookahead extends "d" ? unscanned extends (Scanner.shift<infer enclosing extends EnclosingQuote, infer nextUnscanned>) ? parseEnclosed<s, `d${enclosing}`, nextUnscanned> : parseUnenclosed<s, $, args> : parseUnenclosed<s, $, args> : state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>;
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
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))
|
|
1
|
+
import { Scanner } from "../scanner.js";
|
|
2
|
+
import { enclosingChar, enclosingQuote, parseEnclosed } from "./enclosed.js";
|
|
3
|
+
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
4
|
+
export const parseOperand = (s) => s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s))
|
|
8
5
|
: s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
|
|
9
|
-
: s.scanner.lookaheadIsIn(
|
|
10
|
-
: s.scanner.lookaheadIsIn(
|
|
11
|
-
|
|
6
|
+
: s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
|
|
7
|
+
: s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens) ?
|
|
8
|
+
parseOperand(s.shiftedByOne())
|
|
12
9
|
: s.scanner.lookahead === "d" ?
|
|
13
|
-
s.
|
|
14
|
-
(
|
|
15
|
-
:
|
|
16
|
-
:
|
|
17
|
-
exports.parseOperand = parseOperand;
|
|
10
|
+
s.scanner.nextLookahead in enclosingQuote ?
|
|
11
|
+
parseEnclosed(s, `${s.scanner.shift()}${s.scanner.shift()}`)
|
|
12
|
+
: parseUnenclosed(s)
|
|
13
|
+
: parseUnenclosed(s);
|
|
@@ -1,64 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const validate_js_1 = require("../../../semantic/validate.js");
|
|
7
|
-
const genericArgs_js_1 = require("./genericArgs.js");
|
|
8
|
-
const parseUnenclosed = (s) => {
|
|
1
|
+
import { BaseRoot, hasArkKind, writeUnresolvableMessage } from "@arktype/schema";
|
|
2
|
+
import { printable, throwParseError, tryParseNumber, tryParseWellFormedBigint } from "@arktype/util";
|
|
3
|
+
import { writePrefixedPrivateReferenceMessage } from "../../../semantic/validate.js";
|
|
4
|
+
import { parseGenericArgs, writeInvalidGenericArgsMessage } from "./genericArgs.js";
|
|
5
|
+
export const parseUnenclosed = (s) => {
|
|
9
6
|
const token = s.scanner.shiftUntilNextTerminator();
|
|
10
7
|
if (token === "keyof")
|
|
11
8
|
s.addPrefix("keyof");
|
|
12
9
|
else
|
|
13
10
|
s.root = unenclosedToNode(s, token);
|
|
14
11
|
};
|
|
15
|
-
|
|
16
|
-
const parseGenericInstantiation = (name, g, s) => {
|
|
12
|
+
export const parseGenericInstantiation = (name, g, s) => {
|
|
17
13
|
s.scanner.shiftUntilNonWhitespace();
|
|
18
14
|
const lookahead = s.scanner.shift();
|
|
19
15
|
if (lookahead !== "<")
|
|
20
|
-
return s.error(
|
|
21
|
-
const parsedArgs =
|
|
16
|
+
return s.error(writeInvalidGenericArgsMessage(name, g.params, []));
|
|
17
|
+
const parsedArgs = parseGenericArgs(name, g.params, s);
|
|
22
18
|
const remainingChars = parsedArgs.unscanned.length;
|
|
23
19
|
// set the scanner position to where the args scanner left off
|
|
24
20
|
s.scanner.jumpToIndex(remainingChars === 0 ? s.scanner.length : -remainingChars);
|
|
25
21
|
return g(...parsedArgs.result);
|
|
26
22
|
};
|
|
27
|
-
exports.parseGenericInstantiation = parseGenericInstantiation;
|
|
28
23
|
const unenclosedToNode = (s, token) => maybeParseReference(s, token) ??
|
|
29
24
|
maybeParseUnenclosedLiteral(s, token) ??
|
|
30
|
-
s.error(token === "" ?
|
|
25
|
+
s.error(token === "" ? writeMissingOperandMessage(s)
|
|
31
26
|
: token[0] === "#" ?
|
|
32
|
-
|
|
33
|
-
:
|
|
27
|
+
writePrefixedPrivateReferenceMessage(token)
|
|
28
|
+
: writeUnresolvableMessage(token));
|
|
34
29
|
const maybeParseReference = (s, token) => {
|
|
35
30
|
if (s.ctx.args?.[token])
|
|
36
31
|
return s.ctx.args[token].raw;
|
|
37
32
|
const resolution = s.ctx.$.maybeResolve(token);
|
|
38
|
-
if (resolution instanceof
|
|
33
|
+
if (resolution instanceof BaseRoot)
|
|
39
34
|
return resolution;
|
|
40
35
|
if (resolution === undefined)
|
|
41
36
|
return;
|
|
42
|
-
if (
|
|
43
|
-
return
|
|
44
|
-
return
|
|
37
|
+
if (hasArkKind(resolution, "generic"))
|
|
38
|
+
return parseGenericInstantiation(token, resolution, s);
|
|
39
|
+
return throwParseError(`Unexpected resolution ${printable(resolution)}`);
|
|
45
40
|
};
|
|
46
41
|
const maybeParseUnenclosedLiteral = (s, token) => {
|
|
47
|
-
const maybeNumber =
|
|
42
|
+
const maybeNumber = tryParseNumber(token, { strict: true });
|
|
48
43
|
if (maybeNumber !== undefined)
|
|
49
44
|
return s.ctx.$.node("unit", { unit: maybeNumber });
|
|
50
|
-
const maybeBigint =
|
|
45
|
+
const maybeBigint = tryParseWellFormedBigint(token);
|
|
51
46
|
if (maybeBigint !== undefined)
|
|
52
47
|
return s.ctx.$.node("unit", { unit: maybeBigint });
|
|
53
48
|
};
|
|
54
|
-
const writeMissingOperandMessage = (s) => {
|
|
49
|
+
export const writeMissingOperandMessage = (s) => {
|
|
55
50
|
const operator = s.previousOperator();
|
|
56
51
|
return operator ?
|
|
57
|
-
|
|
58
|
-
:
|
|
52
|
+
writeMissingRightOperandMessage(operator, s.scanner.unscanned)
|
|
53
|
+
: writeExpressionExpectedMessage(s.scanner.unscanned);
|
|
59
54
|
};
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
exports.writeMissingRightOperandMessage = writeMissingRightOperandMessage;
|
|
63
|
-
const writeExpressionExpectedMessage = (unscanned) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
64
|
-
exports.writeExpressionExpectedMessage = writeExpressionExpectedMessage;
|
|
55
|
+
export const writeMissingRightOperandMessage = (token, unscanned = "") => `Token '${token}' requires a right operand${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
56
|
+
export const writeExpressionExpectedMessage = (unscanned) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const shared_js_1 = require("../../reduce/shared.js");
|
|
7
|
-
const date_js_1 = require("../operand/date.js");
|
|
8
|
-
const parseBound = (s, start) => {
|
|
1
|
+
import { internalKeywords, jsObjects, tsKeywords, writeUnboundableMessage } from "@arktype/schema";
|
|
2
|
+
import { isKeyOf, throwParseError } from "@arktype/util";
|
|
3
|
+
import { invertedComparators, maxComparators, writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
4
|
+
import { extractDateLiteralSource, isDateLiteral } from "../operand/date.js";
|
|
5
|
+
export const parseBound = (s, start) => {
|
|
9
6
|
const comparator = shiftComparator(s, start);
|
|
10
7
|
if (s.root.hasKind("unit")) {
|
|
11
8
|
if (typeof s.root.unit === "number") {
|
|
@@ -20,57 +17,54 @@ const parseBound = (s, start) => {
|
|
|
20
17
|
return;
|
|
21
18
|
}
|
|
22
19
|
}
|
|
23
|
-
return
|
|
20
|
+
return parseRightBound(s, comparator);
|
|
24
21
|
};
|
|
25
|
-
exports.parseBound = parseBound;
|
|
26
22
|
const oneCharComparators = {
|
|
27
23
|
"<": true,
|
|
28
24
|
">": true
|
|
29
25
|
};
|
|
30
|
-
|
|
26
|
+
export const comparatorStartChars = {
|
|
31
27
|
"<": 1,
|
|
32
28
|
">": 1,
|
|
33
29
|
"=": 1
|
|
34
30
|
};
|
|
35
31
|
const shiftComparator = (s, start) => s.scanner.lookaheadIs("=") ? `${start}${s.scanner.shift()}`
|
|
36
|
-
:
|
|
37
|
-
: s.error(
|
|
38
|
-
const writeIncompatibleRangeMessage = (l, r) => `Bound kinds ${l} and ${r} are incompatible`;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (root.extends(schema_1.tsKeywords.number)) {
|
|
32
|
+
: isKeyOf(start, oneCharComparators) ? start
|
|
33
|
+
: s.error(singleEqualsMessage);
|
|
34
|
+
export const writeIncompatibleRangeMessage = (l, r) => `Bound kinds ${l} and ${r} are incompatible`;
|
|
35
|
+
export const getBoundKinds = (comparator, limit, root, boundKind) => {
|
|
36
|
+
if (root.extends(tsKeywords.number)) {
|
|
42
37
|
if (typeof limit !== "number") {
|
|
43
|
-
return
|
|
38
|
+
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
44
39
|
}
|
|
45
40
|
return (comparator === "==" ? ["min", "max"]
|
|
46
41
|
: comparator[0] === ">" ? ["min"]
|
|
47
42
|
: ["max"]);
|
|
48
43
|
}
|
|
49
|
-
if (root.extends(
|
|
44
|
+
if (root.extends(internalKeywords.lengthBoundable)) {
|
|
50
45
|
if (typeof limit !== "number") {
|
|
51
|
-
return
|
|
46
|
+
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
52
47
|
}
|
|
53
48
|
return (comparator === "==" ? ["minLength", "maxLength"]
|
|
54
49
|
: comparator[0] === ">" ? ["minLength"]
|
|
55
50
|
: ["maxLength"]);
|
|
56
51
|
}
|
|
57
|
-
if (root.extends(
|
|
52
|
+
if (root.extends(jsObjects.Date)) {
|
|
58
53
|
// allow either numeric or date limits
|
|
59
54
|
return (comparator === "==" ? ["after", "before"]
|
|
60
55
|
: comparator[0] === ">" ? ["after"]
|
|
61
56
|
: ["before"]);
|
|
62
57
|
}
|
|
63
|
-
return
|
|
58
|
+
return throwParseError(writeUnboundableMessage(root.expression));
|
|
64
59
|
};
|
|
65
|
-
|
|
66
|
-
exports.singleEqualsMessage = "= is not a valid comparator. Use == to check for equality";
|
|
60
|
+
export const singleEqualsMessage = "= is not a valid comparator. Use == to check for equality";
|
|
67
61
|
const openLeftBoundToRoot = (leftBound) => ({
|
|
68
|
-
rule:
|
|
69
|
-
|
|
62
|
+
rule: isDateLiteral(leftBound.limit) ?
|
|
63
|
+
extractDateLiteralSource(leftBound.limit)
|
|
70
64
|
: leftBound.limit,
|
|
71
65
|
exclusive: leftBound.comparator.length === 1
|
|
72
66
|
});
|
|
73
|
-
const parseRightBound = (s, comparator) => {
|
|
67
|
+
export const parseRightBound = (s, comparator) => {
|
|
74
68
|
// store the node that will be bounded
|
|
75
69
|
const previousRoot = s.unsetRoot();
|
|
76
70
|
const previousScannerIndex = s.scanner.location;
|
|
@@ -82,22 +76,20 @@ const parseRightBound = (s, comparator) => {
|
|
|
82
76
|
s.root = previousRoot;
|
|
83
77
|
if (!limitNode.hasKind("unit") ||
|
|
84
78
|
(typeof limitNode.unit !== "number" && !(limitNode.unit instanceof Date)))
|
|
85
|
-
return s.error(
|
|
79
|
+
return s.error(writeInvalidLimitMessage(comparator, limitToken, "right"));
|
|
86
80
|
const limit = limitNode.unit;
|
|
87
81
|
// apply the newly-parsed right bound
|
|
88
82
|
const exclusive = comparator.length === 1;
|
|
89
83
|
// if the comparator is ==, both the min and max of that pair will be applied
|
|
90
|
-
for (const kind of
|
|
84
|
+
for (const kind of getBoundKinds(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right"))
|
|
91
85
|
s.constrainRoot(kind, { rule: limit, exclusive });
|
|
92
86
|
if (!s.branches.leftBound)
|
|
93
87
|
return;
|
|
94
88
|
// if there's an open left bound, perform additional validation and apply it
|
|
95
|
-
if (!
|
|
96
|
-
return s.error(
|
|
97
|
-
const lowerBoundKind =
|
|
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");
|
|
98
92
|
s.constrainRoot(lowerBoundKind[0], openLeftBoundToRoot(s.branches.leftBound));
|
|
99
93
|
s.branches.leftBound = null;
|
|
100
94
|
};
|
|
101
|
-
|
|
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;
|
|
95
|
+
export const writeInvalidLimitMessage = (comparator, limit, boundKind) => `Comparator ${boundKind === "left" ? invertedComparators[comparator] : comparator} must be ${boundKind === "left" ? "preceded" : "followed"} by a corresponding literal (was ${limit})`;
|
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.writeInvalidDivisorMessage = exports.parseDivisor = void 0;
|
|
4
|
-
const util_1 = require("@arktype/util");
|
|
5
|
-
const parseDivisor = (s) => {
|
|
1
|
+
import { tryParseInteger } from "@arktype/util";
|
|
2
|
+
export const parseDivisor = (s) => {
|
|
6
3
|
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
7
|
-
const divisor =
|
|
8
|
-
errorOnFail:
|
|
4
|
+
const divisor = tryParseInteger(divisorToken, {
|
|
5
|
+
errorOnFail: writeInvalidDivisorMessage(divisorToken)
|
|
9
6
|
});
|
|
10
7
|
if (divisor === 0)
|
|
11
|
-
s.error(
|
|
8
|
+
s.error(writeInvalidDivisorMessage(0));
|
|
12
9
|
s.root = s.root.constrain("divisor", divisor);
|
|
13
10
|
};
|
|
14
|
-
|
|
15
|
-
const writeInvalidDivisorMessage = (divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
16
|
-
exports.writeInvalidDivisorMessage = writeInvalidDivisorMessage;
|
|
11
|
+
export const writeInvalidDivisorMessage = (divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const bounds_js_1 = require("./bounds.js");
|
|
7
|
-
const divisor_js_1 = require("./divisor.js");
|
|
8
|
-
const parseOperator = (s) => {
|
|
1
|
+
import { isKeyOf } from "@arktype/util";
|
|
2
|
+
import { Scanner } from "../scanner.js";
|
|
3
|
+
import { comparatorStartChars, parseBound } from "./bounds.js";
|
|
4
|
+
import { parseDivisor } from "./divisor.js";
|
|
5
|
+
export const parseOperator = (s) => {
|
|
9
6
|
const lookahead = s.scanner.shift();
|
|
10
7
|
return (lookahead === "" ? s.finalize("")
|
|
11
8
|
: lookahead === "[" ?
|
|
12
9
|
s.scanner.shift() === "]" ?
|
|
13
10
|
s.setRoot(s.root.array())
|
|
14
|
-
: s.error(
|
|
11
|
+
: s.error(incompleteArrayTokenMessage)
|
|
15
12
|
: lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead)
|
|
16
13
|
: lookahead === ")" ? s.finalizeGroup()
|
|
17
|
-
:
|
|
14
|
+
: Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
|
|
18
15
|
s.finalize(lookahead)
|
|
19
|
-
:
|
|
20
|
-
: lookahead === "%" ?
|
|
21
|
-
: lookahead === " " ?
|
|
22
|
-
: s.error(
|
|
16
|
+
: isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
|
|
17
|
+
: lookahead === "%" ? parseDivisor(s)
|
|
18
|
+
: lookahead === " " ? parseOperator(s)
|
|
19
|
+
: s.error(writeUnexpectedCharacterMessage(lookahead)));
|
|
23
20
|
};
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
exports.writeUnexpectedCharacterMessage = writeUnexpectedCharacterMessage;
|
|
27
|
-
exports.incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
21
|
+
export const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
|
|
22
|
+
export const incompleteArrayTokenMessage = `Missing expected ']'`;
|