arktype 1.1.0-dev-2 → 2.0.0-beta.1
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/ark.d.ts +19 -0
- package/out/ark.js +9 -0
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +65 -0
- package/out/generic.js +39 -0
- package/out/index.d.ts +6 -0
- package/out/index.js +5 -0
- package/out/match.d.ts +71 -0
- package/out/match.js +50 -0
- package/out/module.d.ts +14 -0
- package/out/module.js +2 -0
- package/{dist → out}/parser/definition.d.ts +16 -16
- package/{dist → out}/parser/definition.js +12 -12
- package/out/parser/objectLiteral.d.ts +79 -0
- package/out/parser/objectLiteral.js +100 -0
- package/out/parser/semantic/bounds.d.ts +15 -0
- package/{dist → out}/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/divisor.d.ts +7 -0
- package/out/parser/semantic/divisor.js +1 -0
- package/out/parser/semantic/infer.d.ts +27 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/utils.d.ts +4 -0
- package/out/parser/semantic/utils.js +1 -0
- package/out/parser/semantic/validate.d.ts +30 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/string/reduce/dynamic.d.ts +43 -0
- package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
- package/out/parser/string/reduce/shared.d.ts +42 -0
- package/{dist → out}/parser/string/reduce/shared.js +22 -2
- package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
- package/out/parser/string/reduce/static.js +1 -0
- package/out/parser/string/shift/operand/date.d.ts +9 -0
- package/{dist → out}/parser/string/shift/operand/date.js +5 -8
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
- package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
- package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
- package/out/parser/string/shift/operand/genericArgs.js +16 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.js +12 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
- package/out/parser/string/shift/operand/unenclosed.js +53 -0
- package/out/parser/string/shift/operator/bounds.d.ts +27 -0
- package/out/parser/string/shift/operator/bounds.js +94 -0
- package/out/parser/string/shift/operator/default.d.ts +24 -0
- package/out/parser/string/shift/operator/default.js +23 -0
- package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
- package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
- package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
- package/out/parser/string/shift/operator/operator.js +22 -0
- package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
- package/{dist → out}/parser/string/shift/scanner.js +27 -24
- package/out/parser/string/string.d.ts +28 -0
- package/out/parser/string/string.js +24 -0
- package/out/parser/tuple.d.ts +130 -0
- package/out/parser/tuple.js +157 -0
- package/out/scope.d.ts +86 -0
- package/out/scope.js +132 -0
- package/out/type.d.ts +83 -0
- package/out/type.js +36 -0
- package/package.json +37 -41
- package/dist/main.d.ts +0 -11
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -8
- package/dist/main.js.map +0 -1
- package/dist/match.d.ts +0 -46
- package/dist/match.d.ts.map +0 -1
- package/dist/match.js +0 -23
- package/dist/match.js.map +0 -1
- package/dist/parser/definition.d.ts.map +0 -1
- package/dist/parser/definition.js.map +0 -1
- package/dist/parser/generic.d.ts +0 -44
- package/dist/parser/generic.d.ts.map +0 -1
- package/dist/parser/generic.js +0 -46
- package/dist/parser/generic.js.map +0 -1
- package/dist/parser/objectLiteral.d.ts +0 -24
- package/dist/parser/objectLiteral.d.ts.map +0 -1
- package/dist/parser/objectLiteral.js +0 -38
- package/dist/parser/objectLiteral.js.map +0 -1
- package/dist/parser/semantic/bounds.d.ts +0 -13
- package/dist/parser/semantic/bounds.d.ts.map +0 -1
- package/dist/parser/semantic/bounds.js.map +0 -1
- package/dist/parser/semantic/divisor.d.ts +0 -9
- package/dist/parser/semantic/divisor.d.ts.map +0 -1
- package/dist/parser/semantic/divisor.js +0 -2
- package/dist/parser/semantic/divisor.js.map +0 -1
- package/dist/parser/semantic/intersections.d.ts +0 -15
- package/dist/parser/semantic/intersections.d.ts.map +0 -1
- package/dist/parser/semantic/intersections.js +0 -2
- package/dist/parser/semantic/intersections.js.map +0 -1
- package/dist/parser/semantic/semantic.d.ts +0 -22
- package/dist/parser/semantic/semantic.d.ts.map +0 -1
- package/dist/parser/semantic/semantic.js +0 -2
- package/dist/parser/semantic/semantic.js.map +0 -1
- package/dist/parser/semantic/utils.d.ts +0 -8
- package/dist/parser/semantic/utils.d.ts.map +0 -1
- package/dist/parser/semantic/utils.js +0 -2
- package/dist/parser/semantic/utils.js.map +0 -1
- package/dist/parser/semantic/validate.d.ts +0 -24
- package/dist/parser/semantic/validate.d.ts.map +0 -1
- package/dist/parser/semantic/validate.js +0 -2
- package/dist/parser/semantic/validate.js.map +0 -1
- package/dist/parser/shared.d.ts +0 -48
- package/dist/parser/shared.d.ts.map +0 -1
- package/dist/parser/shared.js +0 -41
- package/dist/parser/shared.js.map +0 -1
- package/dist/parser/string/reduce/dynamic.d.ts +0 -40
- package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/dist/parser/string/reduce/dynamic.js.map +0 -1
- package/dist/parser/string/reduce/shared.d.ts +0 -29
- package/dist/parser/string/reduce/shared.d.ts.map +0 -1
- package/dist/parser/string/reduce/shared.js.map +0 -1
- package/dist/parser/string/reduce/static.d.ts.map +0 -1
- package/dist/parser/string/reduce/static.js +0 -2
- package/dist/parser/string/reduce/static.js.map +0 -1
- package/dist/parser/string/shift/operand/date.d.ts +0 -11
- package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/date.js.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
- package/dist/parser/string/shift/operand/operand.d.ts +0 -9
- package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/operand.js +0 -17
- package/dist/parser/string/shift/operand/operand.js.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.js +0 -68
- package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
- package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/bounds.js +0 -95
- package/dist/parser/string/shift/operator/bounds.js.map +0 -1
- package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/divisor.js.map +0 -1
- package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/operator.js +0 -29
- package/dist/parser/string/shift/operator/operator.js.map +0 -1
- package/dist/parser/string/shift/scanner.d.ts.map +0 -1
- package/dist/parser/string/shift/scanner.js.map +0 -1
- package/dist/parser/string/string.d.ts +0 -26
- package/dist/parser/string/string.d.ts.map +0 -1
- package/dist/parser/string/string.js +0 -31
- package/dist/parser/string/string.js.map +0 -1
- package/dist/parser/tuple.d.ts +0 -73
- package/dist/parser/tuple.d.ts.map +0 -1
- package/dist/parser/tuple.js +0 -134
- package/dist/parser/tuple.js.map +0 -1
- package/dist/scope.d.ts +0 -159
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js +0 -248
- package/dist/scope.js.map +0 -1
- package/dist/scopes/ark.d.ts +0 -38
- package/dist/scopes/ark.d.ts.map +0 -1
- package/dist/scopes/ark.js +0 -24
- package/dist/scopes/ark.js.map +0 -1
- package/dist/scopes/jsObjects.d.ts +0 -19
- package/dist/scopes/jsObjects.d.ts.map +0 -1
- package/dist/scopes/jsObjects.js +0 -15
- package/dist/scopes/jsObjects.js.map +0 -1
- package/dist/scopes/parsing.d.ts +0 -18
- package/dist/scopes/parsing.d.ts.map +0 -1
- package/dist/scopes/parsing.js +0 -61
- package/dist/scopes/parsing.js.map +0 -1
- package/dist/scopes/tsGenerics.d.ts +0 -17
- package/dist/scopes/tsGenerics.d.ts.map +0 -1
- package/dist/scopes/tsGenerics.js +0 -13
- package/dist/scopes/tsGenerics.js.map +0 -1
- package/dist/scopes/tsKeywords.d.ts +0 -24
- package/dist/scopes/tsKeywords.d.ts.map +0 -1
- package/dist/scopes/tsKeywords.js +0 -20
- package/dist/scopes/tsKeywords.js.map +0 -1
- package/dist/scopes/utils/creditCard.d.ts +0 -3
- package/dist/scopes/utils/creditCard.d.ts.map +0 -1
- package/dist/scopes/utils/creditCard.js +0 -36
- package/dist/scopes/utils/creditCard.js.map +0 -1
- package/dist/scopes/utils/date.d.ts +0 -24
- package/dist/scopes/utils/date.d.ts.map +0 -1
- package/dist/scopes/utils/date.js +0 -53
- package/dist/scopes/utils/date.js.map +0 -1
- package/dist/scopes/validation.d.ts +0 -20
- package/dist/scopes/validation.d.ts.map +0 -1
- package/dist/scopes/validation.js +0 -57
- package/dist/scopes/validation.js.map +0 -1
- package/dist/type.d.ts +0 -101
- package/dist/type.d.ts.map +0 -1
- package/dist/type.js +0 -138
- package/dist/type.js.map +0 -1
- package/main.ts +0 -20
- package/match.ts +0 -137
- package/parser/definition.ts +0 -156
- package/parser/generic.ts +0 -208
- package/parser/objectLiteral.ts +0 -120
- package/parser/semantic/bounds.ts +0 -63
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -47
- package/parser/semantic/semantic.ts +0 -107
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -105
- package/parser/shared.ts +0 -141
- package/parser/string/reduce/dynamic.ts +0 -181
- package/parser/string/reduce/shared.ts +0 -73
- package/parser/string/reduce/static.ts +0 -300
- package/parser/string/shift/operand/date.ts +0 -57
- package/parser/string/shift/operand/enclosed.ts +0 -119
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -282
- package/parser/string/shift/operator/bounds.ts +0 -229
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -93
- package/parser/tuple.ts +0 -475
- package/scope.ts +0 -573
- package/scopes/ark.ts +0 -64
- package/scopes/jsObjects.ts +0 -29
- package/scopes/parsing.ts +0 -81
- package/scopes/tsGenerics.ts +0 -28
- package/scopes/tsKeywords.ts +0 -39
- package/scopes/utils/creditCard.ts +0 -37
- package/scopes/utils/date.ts +0 -108
- package/scopes/validation.ts +0 -81
- package/type.ts +0 -403
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isKeyOf } from "@arktype/util";
|
|
1
|
+
import { isKeyOf } from "@ark/util";
|
|
3
2
|
import { tryParseDate, writeInvalidDateMessage } from "./date.js";
|
|
4
3
|
export const parseEnclosed = (s, enclosing) => {
|
|
5
4
|
const enclosed = s.scanner.shiftUntil(untilLookaheadIsClosing[enclosingTokens[enclosing]]);
|
|
6
|
-
if (s.scanner.lookahead === "")
|
|
5
|
+
if (s.scanner.lookahead === "")
|
|
7
6
|
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing));
|
|
8
|
-
}
|
|
9
7
|
// Shift the scanner one additional time for the second enclosing token
|
|
10
|
-
|
|
8
|
+
s.scanner.shift();
|
|
11
9
|
if (enclosing === "/") {
|
|
12
10
|
// fail parsing if the regex is invalid
|
|
13
11
|
new RegExp(enclosed);
|
|
14
|
-
s.root = node(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
s.root = s.ctx.$.node("intersection", {
|
|
13
|
+
domain: "string",
|
|
14
|
+
pattern: enclosed
|
|
15
|
+
}, { prereduced: true });
|
|
18
16
|
}
|
|
17
|
+
else if (isKeyOf(enclosing, enclosingQuote))
|
|
18
|
+
s.root = s.ctx.$.node("unit", { unit: enclosed });
|
|
19
19
|
else {
|
|
20
20
|
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed));
|
|
21
|
-
s.root = node({
|
|
21
|
+
s.root = s.ctx.$.node("unit", { unit: date, description: enclosed });
|
|
22
22
|
}
|
|
23
23
|
};
|
|
24
24
|
export const enclosingQuote = {
|
|
@@ -27,7 +27,8 @@ export const enclosingQuote = {
|
|
|
27
27
|
};
|
|
28
28
|
export const enclosingChar = {
|
|
29
29
|
"/": 1,
|
|
30
|
-
|
|
30
|
+
"'": 1,
|
|
31
|
+
'"': 1
|
|
31
32
|
};
|
|
32
33
|
export const enclosingTokens = {
|
|
33
34
|
"d'": "'",
|
|
@@ -37,9 +38,9 @@ export const enclosingTokens = {
|
|
|
37
38
|
"/": "/"
|
|
38
39
|
};
|
|
39
40
|
export const untilLookaheadIsClosing = {
|
|
40
|
-
"'":
|
|
41
|
-
'"':
|
|
42
|
-
"/":
|
|
41
|
+
"'": scanner => scanner.lookahead === `'`,
|
|
42
|
+
'"': scanner => scanner.lookahead === `"`,
|
|
43
|
+
"/": scanner => scanner.lookahead === `/`
|
|
43
44
|
};
|
|
44
45
|
const enclosingCharDescriptions = {
|
|
45
46
|
'"': "double-quote",
|
|
@@ -47,4 +48,3 @@ const enclosingCharDescriptions = {
|
|
|
47
48
|
"/": "forward slash"
|
|
48
49
|
};
|
|
49
50
|
export const writeUnterminatedEnclosedMessage = (fragment, enclosingStart) => `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[enclosingTokens[enclosingStart]]}`;
|
|
50
|
-
//# sourceMappingURL=enclosed.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BaseRoot, GenericProps, GenericRoot } from "@ark/schema";
|
|
2
|
+
import type { array, ErrorMessage, join } from "@ark/util";
|
|
3
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
4
|
+
import { writeUnclosedGroupMessage } from "../../reduce/shared.js";
|
|
5
|
+
import type { state, StaticState } from "../../reduce/static.js";
|
|
6
|
+
import type { parseUntilFinalizer } from "../../string.js";
|
|
7
|
+
export declare const parseGenericArgs: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot[];
|
|
8
|
+
export type parseGenericArgs<name extends string, g extends GenericProps, unscanned extends string, $, args> = _parseGenericArgs<name, g, unscanned, $, args, [], []>;
|
|
9
|
+
declare const _parseGenericArgs: (name: string, g: GenericRoot, s: DynamicState, argNodes: BaseRoot[]) => BaseRoot[];
|
|
10
|
+
export type ParsedArgs<result extends unknown[] = unknown[], unscanned extends string = string> = {
|
|
11
|
+
result: result;
|
|
12
|
+
unscanned: unscanned;
|
|
13
|
+
};
|
|
14
|
+
type _parseGenericArgs<name extends string, g extends GenericProps, unscanned extends string, $, args, argDefs extends string[], argAsts extends unknown[]> = parseUntilFinalizer<state.initialize<unscanned>, $, args> extends (infer finalArgState extends StaticState) ? {
|
|
15
|
+
defs: [
|
|
16
|
+
...argDefs,
|
|
17
|
+
finalArgState["scanned"] extends `${infer def}${"," | ">"}` ? def : finalArgState["scanned"]
|
|
18
|
+
];
|
|
19
|
+
asts: [...argAsts, finalArgState["root"]];
|
|
20
|
+
unscanned: finalArgState["unscanned"];
|
|
21
|
+
} extends ({
|
|
22
|
+
defs: infer nextDefs extends string[];
|
|
23
|
+
asts: infer nextAsts extends unknown[];
|
|
24
|
+
unscanned: infer nextUnscanned extends string;
|
|
25
|
+
}) ? finalArgState["finalizer"] extends ">" ? nextAsts["length"] extends g["params"]["length"] ? ParsedArgs<nextAsts, nextUnscanned> : state.error<writeInvalidGenericArgCountMessage<name, g["names"], nextDefs>> : finalArgState["finalizer"] extends "," ? _parseGenericArgs<name, g, nextUnscanned, $, args, nextDefs, nextAsts> : finalArgState["finalizer"] extends ErrorMessage ? finalArgState : state.error<writeUnclosedGroupMessage<">">> : never : never;
|
|
26
|
+
export declare const writeInvalidGenericArgCountMessage: <name extends string, params extends array<string>, argDefs extends array<string>>(name: name, params: params, argDefs: argDefs) => writeInvalidGenericArgCountMessage<name, params, argDefs>;
|
|
27
|
+
export type writeInvalidGenericArgCountMessage<name extends string, params extends array<string>, argDefs extends array<string>> = `${name}<${join<params, ", ">}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends (0) ? "" : `: ${join<argDefs, ",">}`})`;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { writeUnclosedGroupMessage } from "../../reduce/shared.js";
|
|
2
|
+
export const parseGenericArgs = (name, g, s) => _parseGenericArgs(name, g, s, []);
|
|
3
|
+
const _parseGenericArgs = (name, g, s, argNodes) => {
|
|
4
|
+
const argState = s.parseUntilFinalizer();
|
|
5
|
+
argNodes.push(argState.root);
|
|
6
|
+
if (argState.finalizer === ">") {
|
|
7
|
+
if (argNodes.length !== g.params.length) {
|
|
8
|
+
return s.error(writeInvalidGenericArgCountMessage(name, g.names, argNodes.map(arg => arg.expression)));
|
|
9
|
+
}
|
|
10
|
+
return argNodes;
|
|
11
|
+
}
|
|
12
|
+
if (argState.finalizer === ",")
|
|
13
|
+
return _parseGenericArgs(name, g, s, argNodes);
|
|
14
|
+
return argState.error(writeUnclosedGroupMessage(">"));
|
|
15
|
+
};
|
|
16
|
+
export const writeInvalidGenericArgCountMessage = (name, params, argDefs) => `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${argDefs.length}${argDefs.length === 0 ? "" : `: ${argDefs.join(", ")}`})`;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type WhiteSpaceToken } from "@ark/util";
|
|
2
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
3
|
+
import type { StaticState, state } from "../../reduce/static.js";
|
|
4
|
+
import type { BaseCompletions } from "../../string.js";
|
|
5
|
+
import type { Scanner } from "../scanner.js";
|
|
6
|
+
import { parseEnclosed, type EnclosingQuote, type EnclosingStartToken } from "./enclosed.js";
|
|
7
|
+
import { parseUnenclosed } from "./unenclosed.js";
|
|
8
|
+
export declare const parseOperand: (s: DynamicState) => void;
|
|
9
|
+
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 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>}`>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { whiteSpaceTokens } from "@ark/util";
|
|
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))
|
|
5
|
+
: s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
|
|
6
|
+
: s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
|
|
7
|
+
: s.scanner.lookaheadIsIn(whiteSpaceTokens) ? parseOperand(s.shiftedByOne())
|
|
8
|
+
: s.scanner.lookahead === "d" ?
|
|
9
|
+
s.scanner.nextLookahead in enclosingQuote ?
|
|
10
|
+
parseEnclosed(s, `${s.scanner.shift()}${s.scanner.shift()}`)
|
|
11
|
+
: parseUnenclosed(s)
|
|
12
|
+
: parseUnenclosed(s);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BaseRoot, writeUnresolvableMessage, type GenericProps, type GenericRoot, type arkKind, type resolvableReferenceIn, type resolveReference, type writeNonSubmoduleDotMessage } from "@ark/schema";
|
|
2
|
+
import { type BigintLiteral, type Completion, type ErrorMessage, type anyOrNever, type join } from "@ark/util";
|
|
3
|
+
import type { GenericInstantiationAst } from "../../../semantic/infer.js";
|
|
4
|
+
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
5
|
+
import type { StaticState, state } from "../../reduce/static.js";
|
|
6
|
+
import type { BaseCompletions } from "../../string.js";
|
|
7
|
+
import type { Scanner } from "../scanner.js";
|
|
8
|
+
import { parseGenericArgs, writeInvalidGenericArgCountMessage, type ParsedArgs } from "./genericArgs.js";
|
|
9
|
+
export declare const parseUnenclosed: (s: DynamicState) => void;
|
|
10
|
+
export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (Scanner.shiftResult<infer token, infer unscanned>) ? token extends "keyof" ? state.addPrefix<s, "keyof", unscanned> : tryResolve<s, token, $, args> extends infer result ? result extends ErrorMessage<infer message> ? state.error<message> : result extends resolvableReferenceIn<$> ? parseResolution<s, unscanned, result, resolveReference<result, $>, $, args> : result extends resolvableReferenceIn<ArkEnv.$> ? parseResolution<s, unscanned, result, resolveReference<result, ArkEnv.$>, $, args> : state.setRoot<s, result, unscanned> : never : never;
|
|
11
|
+
type parseResolution<s extends StaticState, unscanned extends string, alias extends string, resolution, $, args> = [
|
|
12
|
+
resolution
|
|
13
|
+
] extends [anyOrNever] ? state.setRoot<s, alias, unscanned> : resolution extends GenericProps ? parseGenericInstantiation<alias, resolution, state.scanTo<s, unscanned>, $, args> : state.setRoot<s, alias, unscanned>;
|
|
14
|
+
export declare const parseGenericInstantiation: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot;
|
|
15
|
+
export type parseGenericInstantiation<name extends string, g extends GenericProps, s extends StaticState, $, args> = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g, unscanned, $, args> extends infer result ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgCountMessage<name, g["names"], []>>;
|
|
16
|
+
type tryResolve<s extends StaticState, token extends string, $, args> = token extends keyof ArkEnv.$ ? token : token extends keyof $ ? token : `#${token}` extends keyof $ ? token : token extends keyof args ? token : token extends `${number}` ? token : token extends BigintLiteral ? token : token extends (`${infer submodule extends (keyof $ | keyof ArkEnv.$) & string}.${infer reference}`) ? tryResolveSubmodule<token, submodule, reference, s, $ & ArkEnv.$, args> : unresolvableError<s, token, $, args, []>;
|
|
17
|
+
type tryResolveSubmodule<token, submodule extends keyof $ & string, reference extends string, s extends StaticState, $, args> = $[submodule] extends {
|
|
18
|
+
[arkKind]: "module";
|
|
19
|
+
} ? reference extends keyof $[submodule] ? token : unresolvableError<s, reference, $[submodule], args, [submodule]> : ErrorMessage<writeNonSubmoduleDotMessage<submodule>>;
|
|
20
|
+
/** Provide valid completions for the current token, or fallback to an
|
|
21
|
+
* unresolvable error if there are none */
|
|
22
|
+
export type unresolvableError<s extends StaticState, token extends string, $, args, submodulePath extends string[]> = validReferenceFromToken<token, $, args, submodulePath> extends never ? ErrorMessage<writeUnresolvableMessage<qualifiedReference<token, submodulePath>>> : Completion<`${s["scanned"]}${qualifiedReference<validReferenceFromToken<token, $, args, submodulePath>, submodulePath>}`>;
|
|
23
|
+
type qualifiedReference<reference extends string, submodulePath extends string[]> = join<[...submodulePath, reference], ".">;
|
|
24
|
+
type validReferenceFromToken<token extends string, $, args, submodulePath extends string[]> = Extract<submodulePath extends [] ? BaseCompletions<$, args> : keyof $, `${token}${string}`>;
|
|
25
|
+
export declare const writeMissingOperandMessage: (s: DynamicState) => string;
|
|
26
|
+
export type writeMissingRightOperandMessage<token extends string, unscanned extends string = ""> = `Token '${token}' requires a right operand${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
27
|
+
export declare const writeMissingRightOperandMessage: <token extends string, unscanned extends string>(token: token, unscanned?: unscanned) => writeMissingRightOperandMessage<token, unscanned>;
|
|
28
|
+
export declare const writeExpressionExpectedMessage: <unscanned extends string>(unscanned: unscanned) => writeExpressionExpectedMessage<unscanned>;
|
|
29
|
+
export type writeExpressionExpectedMessage<unscanned extends string> = `Expected an expression${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BaseRoot, hasArkKind, writeUnresolvableMessage } from "@ark/schema";
|
|
2
|
+
import { printable, throwParseError, tryParseWellFormedBigint, tryParseWellFormedNumber } from "@ark/util";
|
|
3
|
+
import { writePrefixedPrivateReferenceMessage } from "../../../semantic/validate.js";
|
|
4
|
+
import { parseGenericArgs, writeInvalidGenericArgCountMessage } from "./genericArgs.js";
|
|
5
|
+
export const parseUnenclosed = (s) => {
|
|
6
|
+
const token = s.scanner.shiftUntilNextTerminator();
|
|
7
|
+
if (token === "keyof")
|
|
8
|
+
s.addPrefix("keyof");
|
|
9
|
+
else
|
|
10
|
+
s.root = unenclosedToNode(s, token);
|
|
11
|
+
};
|
|
12
|
+
export const parseGenericInstantiation = (name, g, s) => {
|
|
13
|
+
s.scanner.shiftUntilNonWhitespace();
|
|
14
|
+
const lookahead = s.scanner.shift();
|
|
15
|
+
if (lookahead !== "<")
|
|
16
|
+
return s.error(writeInvalidGenericArgCountMessage(name, g.names, []));
|
|
17
|
+
const parsedArgs = parseGenericArgs(name, g, s);
|
|
18
|
+
return g(...parsedArgs);
|
|
19
|
+
};
|
|
20
|
+
const unenclosedToNode = (s, token) => maybeParseReference(s, token) ??
|
|
21
|
+
maybeParseUnenclosedLiteral(s, token) ??
|
|
22
|
+
s.error(token === "" ? writeMissingOperandMessage(s)
|
|
23
|
+
: token[0] === "#" ?
|
|
24
|
+
writePrefixedPrivateReferenceMessage(token)
|
|
25
|
+
: writeUnresolvableMessage(token));
|
|
26
|
+
const maybeParseReference = (s, token) => {
|
|
27
|
+
if (s.ctx.args?.[token])
|
|
28
|
+
return s.ctx.args[token].internal;
|
|
29
|
+
const resolution = s.ctx.$.maybeResolve(token);
|
|
30
|
+
if (resolution instanceof BaseRoot)
|
|
31
|
+
return resolution;
|
|
32
|
+
if (resolution === undefined)
|
|
33
|
+
return;
|
|
34
|
+
if (hasArkKind(resolution, "generic"))
|
|
35
|
+
return parseGenericInstantiation(token, resolution, s);
|
|
36
|
+
return throwParseError(`Unexpected resolution ${printable(resolution)}`);
|
|
37
|
+
};
|
|
38
|
+
const maybeParseUnenclosedLiteral = (s, token) => {
|
|
39
|
+
const maybeNumber = tryParseWellFormedNumber(token);
|
|
40
|
+
if (maybeNumber !== undefined)
|
|
41
|
+
return s.ctx.$.node("unit", { unit: maybeNumber });
|
|
42
|
+
const maybeBigint = tryParseWellFormedBigint(token);
|
|
43
|
+
if (maybeBigint !== undefined)
|
|
44
|
+
return s.ctx.$.node("unit", { unit: maybeBigint });
|
|
45
|
+
};
|
|
46
|
+
export const writeMissingOperandMessage = (s) => {
|
|
47
|
+
const operator = s.previousOperator();
|
|
48
|
+
return operator ?
|
|
49
|
+
writeMissingRightOperandMessage(operator, s.scanner.unscanned)
|
|
50
|
+
: writeExpressionExpectedMessage(s.scanner.unscanned);
|
|
51
|
+
};
|
|
52
|
+
export const writeMissingRightOperandMessage = (token, unscanned = "") => `Token '${token}' requires a right operand${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
53
|
+
export const writeExpressionExpectedMessage = (unscanned) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type BaseRoot, type BoundKind, type LimitLiteral } from "@ark/schema";
|
|
2
|
+
import { type keySet } from "@ark/util";
|
|
3
|
+
import type { astToString } from "../../../semantic/utils.js";
|
|
4
|
+
import type { DynamicState, DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
5
|
+
import { writeUnpairableComparatorMessage, type Comparator, type InvertedComparators, type MaxComparator } from "../../reduce/shared.js";
|
|
6
|
+
import type { StaticState, state } from "../../reduce/static.js";
|
|
7
|
+
import type { parseOperand } from "../operand/operand.js";
|
|
8
|
+
import type { Scanner } from "../scanner.js";
|
|
9
|
+
export declare const parseBound: (s: DynamicStateWithRoot, start: ComparatorStartChar) => void;
|
|
10
|
+
export type parseBound<s extends StaticState, start extends ComparatorStartChar, unscanned extends string, $, args> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends (Scanner.shiftResult<infer comparator extends Comparator, infer nextUnscanned>) ? s["root"] extends `${infer limit extends LimitLiteral}` ? state.reduceLeftBound<s, limit, comparator, nextUnscanned> : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args> : shiftResultOrError : never;
|
|
11
|
+
declare const oneCharComparators: {
|
|
12
|
+
readonly "<": true;
|
|
13
|
+
readonly ">": true;
|
|
14
|
+
};
|
|
15
|
+
type OneCharComparator = keyof typeof oneCharComparators;
|
|
16
|
+
export type ComparatorStartChar = Comparator extends `${infer char}${string}` ? char : never;
|
|
17
|
+
export declare const comparatorStartChars: keySet<ComparatorStartChar>;
|
|
18
|
+
declare const shiftComparator: (s: DynamicState, start: ComparatorStartChar) => Comparator;
|
|
19
|
+
type shiftComparator<start extends ComparatorStartChar, unscanned extends string> = unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned] : [start & OneCharComparator, unscanned];
|
|
20
|
+
export declare const writeIncompatibleRangeMessage: (l: BoundKind, r: BoundKind) => string;
|
|
21
|
+
export declare const getBoundKinds: (comparator: Comparator, limit: LimitLiteral, root: BaseRoot, boundKind: BoundExpressionKind) => BoundKind[];
|
|
22
|
+
export declare const parseRightBound: (s: DynamicStateWithRoot, comparator: Comparator) => void;
|
|
23
|
+
export type parseRightBound<s extends StaticState, comparator extends Comparator, $, args> = parseOperand<s, $, args> extends infer nextState extends StaticState ? nextState["root"] extends `${infer limit extends LimitLiteral}` ? s["branches"]["leftBound"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["leftBound"]["limit"], s["branches"]["leftBound"]["comparator"], comparator, limit, nextState["unscanned"]> : state.error<writeUnpairableComparatorMessage<comparator>> : state.reduceSingleBound<s, comparator, limit, nextState["unscanned"]> : state.error<writeInvalidLimitMessage<comparator, astToString<nextState["root"]>, "right">> : never;
|
|
24
|
+
export declare const writeInvalidLimitMessage: <comparator extends Comparator, limit extends string | number, boundKind extends BoundExpressionKind>(comparator: comparator, limit: limit, boundKind: boundKind) => writeInvalidLimitMessage<comparator, limit, boundKind>;
|
|
25
|
+
export type writeInvalidLimitMessage<comparator extends Comparator, limit extends string | number, boundKind extends BoundExpressionKind> = `Comparator ${boundKind extends "left" ? InvertedComparators[comparator] : comparator} must be ${boundKind extends "left" ? "preceded" : "followed"} by a corresponding literal (was ${limit})`;
|
|
26
|
+
export type BoundExpressionKind = "left" | "right";
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { internal, jsObjects, tsKeywords, writeUnboundableMessage } from "@ark/schema";
|
|
2
|
+
import { isKeyOf, throwParseError } from "@ark/util";
|
|
3
|
+
import { invertedComparators, maxComparators, writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
4
|
+
import { extractDateLiteralSource, isDateLiteral } from "../operand/date.js";
|
|
5
|
+
export const parseBound = (s, start) => {
|
|
6
|
+
const comparator = shiftComparator(s, start);
|
|
7
|
+
if (s.root.hasKind("unit")) {
|
|
8
|
+
if (typeof s.root.unit === "number") {
|
|
9
|
+
s.reduceLeftBound(s.root.unit, comparator);
|
|
10
|
+
s.unsetRoot();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (s.root.unit instanceof Date) {
|
|
14
|
+
const literal = `d'${s.root.description ?? s.root.unit.toISOString()}'`;
|
|
15
|
+
s.unsetRoot();
|
|
16
|
+
s.reduceLeftBound(literal, comparator);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return parseRightBound(s, comparator);
|
|
21
|
+
};
|
|
22
|
+
const oneCharComparators = {
|
|
23
|
+
"<": true,
|
|
24
|
+
">": true
|
|
25
|
+
};
|
|
26
|
+
export const comparatorStartChars = {
|
|
27
|
+
"<": 1,
|
|
28
|
+
">": 1,
|
|
29
|
+
"=": 1
|
|
30
|
+
};
|
|
31
|
+
const shiftComparator = (s, start) => s.scanner.lookaheadIs("=") ?
|
|
32
|
+
`${start}${s.scanner.shift()}`
|
|
33
|
+
: start;
|
|
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)) {
|
|
37
|
+
if (typeof limit !== "number") {
|
|
38
|
+
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
39
|
+
}
|
|
40
|
+
return (comparator === "==" ? ["min", "max"]
|
|
41
|
+
: comparator[0] === ">" ? ["min"]
|
|
42
|
+
: ["max"]);
|
|
43
|
+
}
|
|
44
|
+
if (root.extends(internal.lengthBoundable)) {
|
|
45
|
+
if (typeof limit !== "number") {
|
|
46
|
+
return throwParseError(writeInvalidLimitMessage(comparator, limit, boundKind));
|
|
47
|
+
}
|
|
48
|
+
return (comparator === "==" ? ["minLength", "maxLength"]
|
|
49
|
+
: comparator[0] === ">" ? ["minLength"]
|
|
50
|
+
: ["maxLength"]);
|
|
51
|
+
}
|
|
52
|
+
if (root.extends(jsObjects.Date)) {
|
|
53
|
+
// allow either numeric or date limits
|
|
54
|
+
return (comparator === "==" ? ["after", "before"]
|
|
55
|
+
: comparator[0] === ">" ? ["after"]
|
|
56
|
+
: ["before"]);
|
|
57
|
+
}
|
|
58
|
+
return throwParseError(writeUnboundableMessage(root.expression));
|
|
59
|
+
};
|
|
60
|
+
const openLeftBoundToRoot = (leftBound) => ({
|
|
61
|
+
rule: isDateLiteral(leftBound.limit) ?
|
|
62
|
+
extractDateLiteralSource(leftBound.limit)
|
|
63
|
+
: leftBound.limit,
|
|
64
|
+
exclusive: leftBound.comparator.length === 1
|
|
65
|
+
});
|
|
66
|
+
export const parseRightBound = (s, comparator) => {
|
|
67
|
+
// store the node that will be bounded
|
|
68
|
+
const previousRoot = s.unsetRoot();
|
|
69
|
+
const previousScannerIndex = s.scanner.location;
|
|
70
|
+
s.parseOperand();
|
|
71
|
+
const limitNode = s.unsetRoot();
|
|
72
|
+
// after parsing the next operand, use the locations to get the
|
|
73
|
+
// token from which it was parsed
|
|
74
|
+
const limitToken = s.scanner.sliceChars(previousScannerIndex, s.scanner.location);
|
|
75
|
+
s.root = previousRoot;
|
|
76
|
+
if (!limitNode.hasKind("unit") ||
|
|
77
|
+
(typeof limitNode.unit !== "number" && !(limitNode.unit instanceof Date)))
|
|
78
|
+
return s.error(writeInvalidLimitMessage(comparator, limitToken, "right"));
|
|
79
|
+
const limit = limitNode.unit;
|
|
80
|
+
// apply the newly-parsed right bound
|
|
81
|
+
const exclusive = comparator.length === 1;
|
|
82
|
+
// if the comparator is ==, both the min and max of that pair will be applied
|
|
83
|
+
for (const kind of getBoundKinds(comparator, typeof limit === "number" ? limit : limitToken, previousRoot, "right"))
|
|
84
|
+
s.constrainRoot(kind, { rule: limit, exclusive });
|
|
85
|
+
if (!s.branches.leftBound)
|
|
86
|
+
return;
|
|
87
|
+
// if there's an open left bound, perform additional validation and apply it
|
|
88
|
+
if (!isKeyOf(comparator, maxComparators))
|
|
89
|
+
return s.error(writeUnpairableComparatorMessage(comparator));
|
|
90
|
+
const lowerBoundKind = getBoundKinds(s.branches.leftBound.comparator, s.branches.leftBound.limit, previousRoot, "left");
|
|
91
|
+
s.constrainRoot(lowerBoundKind[0], openLeftBoundToRoot(s.branches.leftBound));
|
|
92
|
+
s.branches.leftBound = null;
|
|
93
|
+
};
|
|
94
|
+
export const writeInvalidLimitMessage = (comparator, limit, boundKind) => `Comparator ${boundKind === "left" ? invertedComparators[comparator] : comparator} must be ${boundKind === "left" ? "preceded" : "followed"} by a corresponding literal (was ${limit})`;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BaseRoot, DateLiteral } from "@ark/schema";
|
|
2
|
+
import { type BigintLiteral, type ErrorMessage, type NumberLiteral, type trim } from "@ark/util";
|
|
3
|
+
import type { DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
4
|
+
import type { StringLiteral } from "../operand/enclosed.js";
|
|
5
|
+
declare const unitLiteralKeywords: {
|
|
6
|
+
readonly null: null;
|
|
7
|
+
readonly undefined: undefined;
|
|
8
|
+
readonly true: true;
|
|
9
|
+
readonly false: false;
|
|
10
|
+
};
|
|
11
|
+
type UnitLiteralKeyword = keyof typeof unitLiteralKeywords;
|
|
12
|
+
export type UnitLiteral = StringLiteral | BigintLiteral | NumberLiteral | DateLiteral | UnitLiteralKeyword;
|
|
13
|
+
export type ParsedDefault = [BaseRoot, "=", unknown];
|
|
14
|
+
export declare const parseDefault: (s: DynamicStateWithRoot) => ParsedDefault;
|
|
15
|
+
export type parseDefault<root, unscanned extends string> = trim<unscanned> extends infer defaultValue extends UnitLiteral ? [
|
|
16
|
+
root,
|
|
17
|
+
"=",
|
|
18
|
+
defaultValue
|
|
19
|
+
] : ErrorMessage<writeNonLiteralDefaultMessage<trim<unscanned>>>;
|
|
20
|
+
export declare const writeNonLiteralDefaultMessage: <defaultDef extends string>(defaultDef: defaultDef) => writeNonLiteralDefaultMessage<defaultDef>;
|
|
21
|
+
export type writeNonLiteralDefaultMessage<defaultDef extends string> = `Default value '${defaultDef}' must a literal value`;
|
|
22
|
+
export declare const shallowDefaultMessage = "Default values must be specified on objects like { isAdmin: 'boolean = false' }";
|
|
23
|
+
export type shallowDefaultMessage = typeof shallowDefaultMessage;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { throwParseError } from "@ark/util";
|
|
2
|
+
const unitLiteralKeywords = {
|
|
3
|
+
null: null,
|
|
4
|
+
undefined,
|
|
5
|
+
true: true,
|
|
6
|
+
false: false
|
|
7
|
+
};
|
|
8
|
+
export const parseDefault = (s) => {
|
|
9
|
+
if (!s.defaultable)
|
|
10
|
+
return throwParseError(shallowDefaultMessage);
|
|
11
|
+
// store the node that will be bounded
|
|
12
|
+
const baseNode = s.unsetRoot();
|
|
13
|
+
s.parseOperand();
|
|
14
|
+
const defaultNode = s.unsetRoot();
|
|
15
|
+
// after parsing the next operand, use the locations to get the
|
|
16
|
+
// token from which it was parsed
|
|
17
|
+
if (!defaultNode.hasKind("unit"))
|
|
18
|
+
return s.error(writeNonLiteralDefaultMessage(defaultNode.expression));
|
|
19
|
+
// assignability is checked in parseEntries
|
|
20
|
+
return [baseNode, "=", defaultNode.unit];
|
|
21
|
+
};
|
|
22
|
+
export const writeNonLiteralDefaultMessage = (defaultDef) => `Default value '${defaultDef}' must a literal value`;
|
|
23
|
+
export const shallowDefaultMessage = `Default values must be specified on objects like { isAdmin: 'boolean = false' }`;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { type NumberLiteral } from "@arktype/util";
|
|
2
1
|
import type { DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
3
2
|
import type { StaticState, state } from "../../reduce/static.js";
|
|
4
3
|
import type { Scanner } from "../scanner.js";
|
|
5
4
|
export declare const parseDivisor: (s: DynamicStateWithRoot) => void;
|
|
6
|
-
export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<Scanner.skipWhitespace<unscanned>> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? scanned extends
|
|
7
|
-
export declare const writeInvalidDivisorMessage: <divisor extends string | number>(divisor: divisor) =>
|
|
5
|
+
export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<Scanner.skipWhitespace<unscanned>> extends (Scanner.shiftResult<infer scanned, infer nextUnscanned>) ? scanned extends `${infer divisor extends number}` ? divisor extends 0 ? state.error<writeInvalidDivisorMessage<0>> : state.setRoot<s, [s["root"], "%", divisor], nextUnscanned> : state.error<writeInvalidDivisorMessage<scanned>> : never;
|
|
6
|
+
export declare const writeInvalidDivisorMessage: <divisor extends string | number>(divisor: divisor) => writeInvalidDivisorMessage<divisor>;
|
|
8
7
|
export type writeInvalidDivisorMessage<divisor extends string | number> = `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
9
|
-
//# sourceMappingURL=divisor.d.ts.map
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { tryParseInteger } from "@
|
|
1
|
+
import { tryParseInteger } from "@ark/util";
|
|
2
2
|
export const parseDivisor = (s) => {
|
|
3
3
|
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
4
4
|
const divisor = tryParseInteger(divisorToken, {
|
|
5
5
|
errorOnFail: writeInvalidDivisorMessage(divisorToken)
|
|
6
6
|
});
|
|
7
|
-
if (divisor === 0)
|
|
7
|
+
if (divisor === 0)
|
|
8
8
|
s.error(writeInvalidDivisorMessage(0));
|
|
9
|
-
}
|
|
10
9
|
s.root = s.root.constrain("divisor", divisor);
|
|
11
10
|
};
|
|
12
11
|
export const writeInvalidDivisorMessage = (divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
13
|
-
//# sourceMappingURL=divisor.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
import { type WhiteSpaceToken } from "@ark/util";
|
|
1
2
|
import type { DynamicStateWithRoot } from "../../reduce/dynamic.js";
|
|
2
|
-
import type {
|
|
3
|
+
import type { StaticState, state } from "../../reduce/static.js";
|
|
3
4
|
import { Scanner } from "../scanner.js";
|
|
4
5
|
import { parseBound, type ComparatorStartChar } from "./bounds.js";
|
|
5
6
|
import { parseDivisor } from "./divisor.js";
|
|
6
7
|
export declare const parseOperator: (s: DynamicStateWithRoot) => void;
|
|
7
|
-
export type parseOperator<s extends StaticState, $, args> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "[" ? unscanned extends Scanner.shift<"]", infer nextUnscanned> ? state.setRoot<s, [s["root"], "[]"], nextUnscanned> : state.error<incompleteArrayTokenMessage> : lookahead extends "|" | "&" ? state.reduceBranch<s, lookahead, unscanned> : lookahead extends ")" ? state.finalizeGroup<s, unscanned> : Scanner.lookaheadIsFinalizing<lookahead, unscanned> extends true ? state.finalize<state.scanTo<s, unscanned>, lookahead & Scanner.FinalizingLookahead> : lookahead extends ComparatorStartChar ? parseBound<s, lookahead, unscanned, $, args> : lookahead extends "%" ? parseDivisor<s, unscanned> : lookahead extends
|
|
8
|
-
export declare const writeUnexpectedCharacterMessage: <char extends string, shouldBe extends string>(char: char, shouldBe?: shouldBe) =>
|
|
8
|
+
export type parseOperator<s extends StaticState, $, args> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "[" ? unscanned extends Scanner.shift<"]", infer nextUnscanned> ? state.setRoot<s, [s["root"], "[]"], nextUnscanned> : state.error<incompleteArrayTokenMessage> : lookahead extends "|" | "&" ? state.reduceBranch<s, lookahead, unscanned> : lookahead extends ")" ? state.finalizeGroup<s, unscanned> : Scanner.lookaheadIsFinalizing<lookahead, unscanned> extends true ? state.finalize<state.scanTo<s, unscanned>, lookahead & Scanner.FinalizingLookahead> : lookahead extends ComparatorStartChar ? parseBound<s, lookahead, unscanned, $, args> : lookahead extends "%" ? parseDivisor<s, unscanned> : lookahead extends WhiteSpaceToken ? parseOperator<state.scanTo<s, unscanned>, $, args> : state.error<writeUnexpectedCharacterMessage<lookahead>> : state.finalize<s, "">;
|
|
9
|
+
export declare const writeUnexpectedCharacterMessage: <char extends string, shouldBe extends string>(char: char, shouldBe?: shouldBe) => writeUnexpectedCharacterMessage<char, shouldBe>;
|
|
9
10
|
export type writeUnexpectedCharacterMessage<char extends string, shouldBe extends string = ""> = `'${char}' is not allowed here${shouldBe extends "" ? "" : ` (should be ${shouldBe})`}`;
|
|
10
11
|
export declare const incompleteArrayTokenMessage = "Missing expected ']'";
|
|
11
12
|
type incompleteArrayTokenMessage = typeof incompleteArrayTokenMessage;
|
|
12
13
|
export {};
|
|
13
|
-
//# sourceMappingURL=operator.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { isKeyOf } from "@ark/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) => {
|
|
6
|
+
const lookahead = s.scanner.shift();
|
|
7
|
+
return (lookahead === "" ? s.finalize("")
|
|
8
|
+
: lookahead === "[" ?
|
|
9
|
+
s.scanner.shift() === "]" ?
|
|
10
|
+
s.setRoot(s.root.array())
|
|
11
|
+
: s.error(incompleteArrayTokenMessage)
|
|
12
|
+
: lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead)
|
|
13
|
+
: lookahead === ")" ? s.finalizeGroup()
|
|
14
|
+
: Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
|
|
15
|
+
s.finalize(lookahead)
|
|
16
|
+
: isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
|
|
17
|
+
: lookahead === "%" ? parseDivisor(s)
|
|
18
|
+
: lookahead === " " ? parseOperator(s)
|
|
19
|
+
: s.error(writeUnexpectedCharacterMessage(lookahead)));
|
|
20
|
+
};
|
|
21
|
+
export const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
|
|
22
|
+
export const incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import { type Dict } from "@
|
|
2
|
-
import type { Comparator } from "
|
|
3
|
-
export declare class Scanner<
|
|
1
|
+
import { type Dict, type EscapeToken, type WhiteSpaceToken } from "@ark/util";
|
|
2
|
+
import type { Comparator } from "../reduce/shared.js";
|
|
3
|
+
export declare class Scanner<lookahead extends string = string> {
|
|
4
4
|
private chars;
|
|
5
5
|
private i;
|
|
6
6
|
constructor(def: string);
|
|
7
7
|
/** Get lookahead and advance scanner by one */
|
|
8
|
-
shift():
|
|
9
|
-
get lookahead():
|
|
8
|
+
shift(): lookahead;
|
|
9
|
+
get lookahead(): lookahead;
|
|
10
|
+
get nextLookahead(): string;
|
|
10
11
|
get length(): number;
|
|
11
12
|
shiftUntil(condition: Scanner.UntilCondition): string;
|
|
12
13
|
shiftUntilNextTerminator(): string;
|
|
13
14
|
shiftUntilNonWhitespace(): string;
|
|
14
15
|
jumpToIndex(i: number): void;
|
|
16
|
+
jumpForward(count: number): void;
|
|
15
17
|
get location(): number;
|
|
16
18
|
get unscanned(): string;
|
|
17
19
|
get scanned(): string;
|
|
18
20
|
sliceChars(start: number, end?: number): string;
|
|
19
|
-
lookaheadIs<
|
|
20
|
-
lookaheadIsIn<
|
|
21
|
+
lookaheadIs<char extends lookahead>(char: char): this is Scanner<char>;
|
|
22
|
+
lookaheadIsIn<tokens extends Dict>(tokens: tokens): this is Scanner<Extract<keyof tokens, string>>;
|
|
21
23
|
}
|
|
22
24
|
export declare namespace Scanner {
|
|
23
25
|
type UntilCondition = (scanner: Scanner, shifted: string) => boolean;
|
|
@@ -38,26 +40,21 @@ export declare namespace Scanner {
|
|
|
38
40
|
readonly "%": true;
|
|
39
41
|
readonly " ": true;
|
|
40
42
|
readonly ",": true;
|
|
43
|
+
readonly ":": true;
|
|
41
44
|
};
|
|
42
45
|
type TerminatingChar = keyof typeof terminatingChars;
|
|
43
46
|
const finalizingLookaheads: {
|
|
44
47
|
readonly ">": true;
|
|
45
48
|
readonly ",": true;
|
|
46
49
|
readonly "": true;
|
|
50
|
+
readonly "=": true;
|
|
47
51
|
};
|
|
48
52
|
type FinalizingLookahead = keyof typeof finalizingLookaheads;
|
|
49
53
|
type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>";
|
|
50
54
|
type PostfixToken = "[]";
|
|
51
55
|
type OperatorToken = InfixToken | PostfixToken;
|
|
52
|
-
const
|
|
53
|
-
type
|
|
54
|
-
const whiteSpaceTokens: {
|
|
55
|
-
readonly " ": true;
|
|
56
|
-
readonly "\n": true;
|
|
57
|
-
};
|
|
58
|
-
type WhiteSpaceToken = keyof typeof whiteSpaceTokens;
|
|
59
|
-
const lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | ",";
|
|
60
|
-
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : Scanner.skipWhitespace<unscanned> extends "" | `${TerminatingChar}${string}` ? true : false : lookahead extends "," ? true : false;
|
|
56
|
+
const lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=";
|
|
57
|
+
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : Scanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," ? true : false;
|
|
61
58
|
type shift<lookahead extends string, unscanned extends string> = `${lookahead}${unscanned}`;
|
|
62
59
|
type shiftUntil<unscanned extends string, terminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends terminator ? scanned extends `${infer base}${EscapeToken}` ? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`> : [scanned, unscanned] : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`> : [scanned, ""];
|
|
63
60
|
type shiftUntilNot<unscanned extends string, nonTerminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends nonTerminator ? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`> : [scanned, unscanned] : [scanned, ""];
|
|
@@ -68,4 +65,3 @@ export declare namespace Scanner {
|
|
|
68
65
|
unscanned
|
|
69
66
|
];
|
|
70
67
|
}
|
|
71
|
-
//# sourceMappingURL=scanner.d.ts.map
|