arktype 2.0.0-dev.24 → 2.0.0-dev.26
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/ark.d.ts +1 -1
- package/out/ark.js +1 -2
- package/out/parser/definition.d.ts +1 -1
- package/out/parser/generic.d.ts +2 -2
- package/out/parser/objectLiteral.d.ts +38 -25
- package/out/parser/objectLiteral.js +55 -62
- package/out/parser/semantic/bounds.d.ts +1 -1
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/infer.d.ts +4 -3
- package/out/parser/semantic/validate.d.ts +5 -3
- package/out/parser/string/reduce/dynamic.d.ts +2 -1
- package/out/parser/string/reduce/dynamic.js +4 -2
- package/out/parser/string/reduce/shared.d.ts +5 -5
- package/out/parser/string/shift/operand/date.d.ts +2 -4
- package/out/parser/string/shift/operand/enclosed.d.ts +1 -11
- package/out/parser/string/shift/operand/genericArgs.d.ts +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -2
- package/out/parser/string/shift/operand/operand.js +2 -3
- package/out/parser/string/shift/operand/unenclosed.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.js +6 -5
- package/out/parser/string/shift/operator/bounds.d.ts +3 -5
- package/out/parser/string/shift/operator/bounds.js +5 -6
- package/out/parser/string/shift/operator/default.d.ts +24 -0
- package/out/parser/string/shift/operator/default.js +23 -0
- package/out/parser/string/shift/operator/divisor.d.ts +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +3 -2
- package/out/parser/string/shift/scanner.d.ts +4 -11
- package/out/parser/string/shift/scanner.js +10 -12
- package/out/parser/string/string.d.ts +7 -5
- package/out/parser/string/string.js +4 -1
- package/out/parser/tuple.d.ts +4 -4
- package/out/parser/tuple.js +3 -3
- package/out/scope.d.ts +5 -4
- package/out/scope.js +104 -54
- package/out/type.d.ts +7 -5
- package/out/type.js +7 -4
- package/package.json +3 -3
package/out/ark.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ type TsGenericsExports<$ = Ark> = {
|
|
|
13
13
|
"[K]": "V";
|
|
14
14
|
}, $>;
|
|
15
15
|
};
|
|
16
|
-
export declare const tsGenerics: Module<TsGenericsExports
|
|
16
|
+
export declare const tsGenerics: Module<TsGenericsExports>;
|
|
17
17
|
export declare const ambient: Scope<Ark>;
|
|
18
18
|
export declare const ark: Module<Ark>;
|
|
19
19
|
export declare const type: TypeParser<{}>;
|
package/out/ark.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { keywordNodes } from "@arktype/schema";
|
|
2
|
-
import {
|
|
2
|
+
import { scope } from "./scope.js";
|
|
3
3
|
export const tsGenerics = {};
|
|
4
4
|
export const ambient = scope(keywordNodes);
|
|
5
|
-
RawScope.ambient = ambient.raw;
|
|
6
5
|
export const ark = ambient.export();
|
|
7
6
|
export const type = ambient.type;
|
|
8
7
|
export const match = ambient.match;
|
|
@@ -27,6 +27,6 @@ type declarationMismatch<def, declared, $, args> = {
|
|
|
27
27
|
type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown);
|
|
28
28
|
export type ThunkCast<t = unknown> = () => type.cast<t>;
|
|
29
29
|
type BadDefinitionType = Exclude<Primitive, string>;
|
|
30
|
-
export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) =>
|
|
30
|
+
export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => writeBadDefinitionTypeMessage<actual>;
|
|
31
31
|
type writeBadDefinitionTypeMessage<actual extends string> = `Type definitions must be strings or objects (was ${actual})`;
|
|
32
32
|
export {};
|
package/out/parser/generic.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type keyError } from "@arktype/util";
|
|
1
|
+
import { type keyError, type WhiteSpaceToken } from "@arktype/util";
|
|
2
2
|
import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js";
|
|
3
3
|
import { Scanner } from "./string/shift/scanner.js";
|
|
4
4
|
export type GenericDeclaration<name extends string = string, params extends string = string> = `${name}<${params}>`;
|
|
@@ -9,7 +9,7 @@ export declare const parseGenericParams: (def: string) => string[];
|
|
|
9
9
|
export type parseGenericParams<def extends string> = _parseParams<def, "", []> extends infer result extends string[] ? "" extends result[number] ? GenericParamsParseError<emptyGenericParameterMessage> : result : never;
|
|
10
10
|
export declare const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
11
11
|
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage;
|
|
12
|
-
type _parseParams<unscanned extends string, param extends string, result extends string[]> = unscanned extends `${infer lookahead}${infer nextUnscanned}` ? lookahead extends "," ? _parseParams<nextUnscanned, "", [...result, param]> : lookahead extends
|
|
12
|
+
type _parseParams<unscanned extends string, param extends string, result extends string[]> = unscanned extends `${infer lookahead}${infer nextUnscanned}` ? lookahead extends "," ? _parseParams<nextUnscanned, "", [...result, param]> : lookahead extends WhiteSpaceToken ? param extends "" ? _parseParams<Scanner.skipWhitespace<nextUnscanned>, "", result> : Scanner.skipWhitespace<nextUnscanned> extends (`${infer nextNonWhitespace}${infer rest}`) ? nextNonWhitespace extends "," ? _parseParams<rest, "", [...result, param]> : GenericParamsParseError<writeUnexpectedCharacterMessage<nextNonWhitespace, ",">> : [
|
|
13
13
|
...result,
|
|
14
14
|
param
|
|
15
15
|
] : _parseParams<nextUnscanned, `${param}${lookahead}`, result> : param extends "" ? result : [...result, param];
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { type BaseRoot, type Default, type of, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
|
|
2
|
-
import { type anyOrNever, type Dict, type ErrorMessage, type Key, type keyError, type merge, type show } from "@arktype/util";
|
|
1
|
+
import { type BaseRoot, type Default, type DefaultableAst, type IndexNode, type of, type OptionalNode, type RequiredNode, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
|
|
2
|
+
import { type anyOrNever, type Dict, type ErrorMessage, type EscapeToken, type Key, type keyError, type listable, type merge, type show } from "@arktype/util";
|
|
3
3
|
import type { ParseContext } from "../scope.js";
|
|
4
4
|
import type { inferDefinition, validateDefinition } from "./definition.js";
|
|
5
5
|
import type { astToString } from "./semantic/utils.js";
|
|
6
6
|
import type { validateString } from "./semantic/validate.js";
|
|
7
|
-
import { Scanner } from "./string/shift/scanner.js";
|
|
8
7
|
export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
|
|
9
8
|
export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
|
|
10
9
|
export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
@@ -13,23 +12,35 @@ export type inferObjectLiteral<def extends object, $, args> = show<"..." extends
|
|
|
13
12
|
* Infers the contents of an object literal, ignoring a spread definition
|
|
14
13
|
*/
|
|
15
14
|
type _inferObjectLiteral<def extends object, $, args> = {
|
|
16
|
-
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (
|
|
15
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (DefaultValueTuple<infer baseDef, infer defaultValue>) ? def[k] extends anyOrNever ? def[k] : (In?: inferDefinition<baseDef, $, args>) => Default<defaultValue> : inferDefinition<def[k], $, args>;
|
|
17
16
|
} & {
|
|
18
17
|
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
|
|
19
18
|
};
|
|
20
19
|
export type validateObjectLiteral<def, $, args> = {
|
|
21
|
-
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? keyError<message> : inferDefinition<indexDef, $, args> extends (PropertyKey | of<PropertyKey, {}>) ? validateDefinition<def[k], $, args> : keyError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : keyError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior :
|
|
20
|
+
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? keyError<message> : inferDefinition<indexDef, $, args> extends (PropertyKey | of<PropertyKey, {}>) ? validateDefinition<def[k], $, args> : keyError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : keyError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validateDefaultableValue<def, k, $, args>;
|
|
22
21
|
};
|
|
23
|
-
type
|
|
24
|
-
|
|
22
|
+
type validateDefaultableValue<def, k extends keyof def, $, args> = def[k] extends DefaultValueTuple ? validateDefaultValueTuple<def[k], k, $, args> : validateDefinition<def[k], $, args> extends def[k] ? [
|
|
23
|
+
inferDefinition<def[k], $, args>,
|
|
24
|
+
parseKey<k>["kind"]
|
|
25
|
+
] extends ([
|
|
26
|
+
DefaultableAst,
|
|
27
|
+
Exclude<ParsedKeyKind, "required">
|
|
28
|
+
]) ? ErrorMessage<invalidDefaultKeyKindMessage> : validateDefinition<def[k], $, args> : validateDefinition<def[k], $, args>;
|
|
29
|
+
type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
|
|
30
|
+
baseDef,
|
|
25
31
|
"=",
|
|
26
|
-
|
|
27
|
-
]
|
|
28
|
-
type
|
|
32
|
+
defaultValue
|
|
33
|
+
];
|
|
34
|
+
type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? readonly [
|
|
35
|
+
validateDefinition<def[0], $, args>,
|
|
36
|
+
"=",
|
|
37
|
+
inferDefinition<def[0], $, args>
|
|
38
|
+
] : ErrorMessage<invalidDefaultKeyKindMessage>;
|
|
39
|
+
type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t extends Key ? t : never : never;
|
|
29
40
|
type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
|
|
30
|
-
type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind,
|
|
41
|
+
type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
|
|
31
42
|
kind: kind;
|
|
32
|
-
key:
|
|
43
|
+
key: key;
|
|
33
44
|
};
|
|
34
45
|
declare namespace PreparsedKey {
|
|
35
46
|
type from<t extends PreparsedKey> = t;
|
|
@@ -37,18 +48,20 @@ declare namespace PreparsedKey {
|
|
|
37
48
|
type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
|
|
38
49
|
export type MetaKey = "..." | "+";
|
|
39
50
|
export type IndexKey<def extends string = string> = `[${def}]`;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
export type ParsedEntry = ParsedUndeclaredEntry | ParsedSpreadEntry | RequiredNode | OptionalNode | IndexNode;
|
|
52
|
+
export type ParsedUndeclaredEntry = {
|
|
53
|
+
kind: "undeclared";
|
|
54
|
+
behavior: UndeclaredKeyBehavior;
|
|
55
|
+
};
|
|
56
|
+
export type ParsedSpreadEntry = {
|
|
57
|
+
kind: "spread";
|
|
58
|
+
node: BaseRoot;
|
|
59
|
+
};
|
|
60
|
+
export declare const parseEntry: (key: Key, value: unknown, ctx: ParseContext) => listable<ParsedEntry>;
|
|
61
|
+
export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
|
|
49
62
|
export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
|
|
50
63
|
declare const parseKey: (key: Key) => PreparsedKey;
|
|
51
|
-
type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${
|
|
64
|
+
type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeToken}` ? PreparsedKey.from<{
|
|
52
65
|
kind: "required";
|
|
53
66
|
key: `${baseName}?`;
|
|
54
67
|
}> : PreparsedKey.from<{
|
|
@@ -57,7 +70,7 @@ type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName
|
|
|
57
70
|
}> : k extends MetaKey ? PreparsedKey.from<{
|
|
58
71
|
kind: k;
|
|
59
72
|
key: k;
|
|
60
|
-
}> : k extends `${
|
|
73
|
+
}> : k extends `${EscapeToken}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
|
|
61
74
|
kind: "required";
|
|
62
75
|
key: escapedMeta;
|
|
63
76
|
}> : k extends IndexKey<infer def> ? PreparsedKey.from<{
|
|
@@ -65,8 +78,8 @@ type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName
|
|
|
65
78
|
key: def;
|
|
66
79
|
}> : PreparsedKey.from<{
|
|
67
80
|
kind: "required";
|
|
68
|
-
key: k extends
|
|
81
|
+
key: k extends `${EscapeToken}${infer escapedIndexKey extends IndexKey}` ? escapedIndexKey : k extends Key ? k : `${k & number}`;
|
|
69
82
|
}>;
|
|
70
|
-
export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) =>
|
|
83
|
+
export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => writeInvalidSpreadTypeMessage<def>;
|
|
71
84
|
type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
|
|
72
85
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ArkErrors, normalizeIndex } from "@arktype/schema";
|
|
2
|
-
import { append, isArray, printable, stringAndSymbolicEntriesOf, throwParseError
|
|
3
|
-
import {
|
|
2
|
+
import { append, escapeToken, isArray, printable, stringAndSymbolicEntriesOf, throwParseError } from "@arktype/util";
|
|
3
|
+
import { writeUnassignableDefaultValueMessage } from "./semantic/default.js";
|
|
4
4
|
export const parseObjectLiteral = (def, ctx) => {
|
|
5
5
|
let spread;
|
|
6
6
|
const structure = {};
|
|
@@ -8,53 +8,25 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
8
8
|
// because to match JS behavior any keys before the spread are overwritten
|
|
9
9
|
// by the values in the target object, so there'd be no useful purpose in having it
|
|
10
10
|
// anywhere except for the beginning.
|
|
11
|
-
const parsedEntries = stringAndSymbolicEntriesOf(def).
|
|
12
|
-
if (parsedEntries[0]?.kind === "
|
|
11
|
+
const parsedEntries = stringAndSymbolicEntriesOf(def).flatMap(entry => parseEntry(entry[0], entry[1], ctx));
|
|
12
|
+
if (parsedEntries[0]?.kind === "spread") {
|
|
13
13
|
// remove the spread entry so we can iterate over the remaining entries
|
|
14
14
|
// expecting non-spread entries
|
|
15
15
|
const spreadEntry = parsedEntries.shift();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.
|
|
19
|
-
spreadEntry.value
|
|
20
|
-
: printable(spreadEntry.value)));
|
|
16
|
+
if (!spreadEntry.node.hasKind("intersection") ||
|
|
17
|
+
!spreadEntry.node.structure) {
|
|
18
|
+
return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.node.expression));
|
|
21
19
|
}
|
|
22
|
-
spread =
|
|
20
|
+
spread = spreadEntry.node.structure;
|
|
23
21
|
}
|
|
24
22
|
for (const entry of parsedEntries) {
|
|
25
|
-
if (entry.kind === "
|
|
23
|
+
if (entry.kind === "spread")
|
|
26
24
|
return throwParseError(nonLeadingSpreadError);
|
|
27
|
-
if (entry.kind === "
|
|
28
|
-
|
|
29
|
-
entry.value !== "delete" &&
|
|
30
|
-
entry.value !== "ignore")
|
|
31
|
-
throwParseError(writeInvalidUndeclaredBehaviorMessage(entry.value));
|
|
32
|
-
structure.undeclared = entry.value;
|
|
25
|
+
if (entry.kind === "undeclared") {
|
|
26
|
+
structure.undeclared = entry.behavior;
|
|
33
27
|
continue;
|
|
34
28
|
}
|
|
35
|
-
|
|
36
|
-
// handle key parsing first to match type behavior
|
|
37
|
-
const key = ctx.$.parse(entry.key, ctx);
|
|
38
|
-
const value = ctx.$.parse(entry.value, ctx);
|
|
39
|
-
const normalizedSignature = normalizeIndex(key, value, ctx.$);
|
|
40
|
-
if (normalizedSignature.required) {
|
|
41
|
-
structure.required = append(structure.required, normalizedSignature.required);
|
|
42
|
-
}
|
|
43
|
-
if (normalizedSignature.index)
|
|
44
|
-
structure.index = append(structure.index, normalizedSignature.index);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
const value = ctx.$.parse(entry.value, ctx);
|
|
48
|
-
const inner = { key: entry.key, value };
|
|
49
|
-
if (entry.default !== unset) {
|
|
50
|
-
const out = value(entry.default);
|
|
51
|
-
if (out instanceof ArkErrors)
|
|
52
|
-
throwParseError(`Default value at ${printable(entry.key)} ${out}`);
|
|
53
|
-
value.assert(entry.default);
|
|
54
|
-
inner.default = entry.default;
|
|
55
|
-
}
|
|
56
|
-
structure[entry.kind] = append(structure[entry.kind], inner);
|
|
57
|
-
}
|
|
29
|
+
structure[entry.kind] = append(structure[entry.kind], entry);
|
|
58
30
|
}
|
|
59
31
|
const structureNode = ctx.$.node("structure", structure);
|
|
60
32
|
return ctx.$.schema({
|
|
@@ -64,30 +36,50 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
64
36
|
};
|
|
65
37
|
export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
|
|
66
38
|
export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
67
|
-
export const parseEntry = (
|
|
39
|
+
export const parseEntry = (key, value, ctx) => {
|
|
68
40
|
const parsedKey = parseKey(key);
|
|
69
|
-
if (
|
|
41
|
+
if (parsedKey.kind === "+") {
|
|
42
|
+
if (value !== "reject" && value !== "delete" && value !== "ignore")
|
|
43
|
+
throwParseError(writeInvalidUndeclaredBehaviorMessage(value));
|
|
44
|
+
return { kind: "undeclared", behavior: value };
|
|
45
|
+
}
|
|
46
|
+
if (parsedKey.kind === "...")
|
|
47
|
+
return { kind: "spread", node: ctx.$.parse(value, ctx) };
|
|
48
|
+
const parsedValue = isArray(value) && value[1] === "=" ?
|
|
49
|
+
[ctx.$.parse(value[0], ctx), "=", value[2]]
|
|
50
|
+
: ctx.$.parse(value, ctx, true);
|
|
51
|
+
if (isArray(parsedValue)) {
|
|
70
52
|
if (parsedKey.kind !== "required")
|
|
71
53
|
throwParseError(invalidDefaultKeyKindMessage);
|
|
72
|
-
|
|
73
|
-
|
|
54
|
+
const out = parsedValue[0].traverse(parsedValue[2]);
|
|
55
|
+
if (out instanceof ArkErrors) {
|
|
56
|
+
throwParseError(writeUnassignableDefaultValueMessage(printable(parsedKey.key), out.message));
|
|
57
|
+
}
|
|
58
|
+
return ctx.$.node("optional", {
|
|
74
59
|
key: parsedKey.key,
|
|
75
|
-
value:
|
|
76
|
-
default:
|
|
77
|
-
};
|
|
60
|
+
value: parsedValue[0],
|
|
61
|
+
default: parsedValue[2]
|
|
62
|
+
});
|
|
78
63
|
}
|
|
79
|
-
|
|
80
|
-
|
|
64
|
+
if (parsedKey.kind === "index") {
|
|
65
|
+
const signature = ctx.$.parse(parsedKey.key, ctx);
|
|
66
|
+
const normalized = normalizeIndex(signature, parsedValue, ctx.$);
|
|
67
|
+
return (normalized.index ?
|
|
68
|
+
normalized.required ?
|
|
69
|
+
[normalized.index, ...normalized.required]
|
|
70
|
+
: normalized.index
|
|
71
|
+
: normalized.required ?? []);
|
|
72
|
+
}
|
|
73
|
+
return ctx.$.node(parsedKey.kind, {
|
|
81
74
|
key: parsedKey.key,
|
|
82
|
-
value
|
|
83
|
-
|
|
84
|
-
};
|
|
75
|
+
value: parsedValue
|
|
76
|
+
});
|
|
85
77
|
};
|
|
86
78
|
// single quote use here is better for TypeScript's inlined error to avoid escapes
|
|
87
|
-
export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. {
|
|
79
|
+
export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { value: 'number = 0' }`;
|
|
88
80
|
const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
|
|
89
81
|
: key.at(-1) === "?" ?
|
|
90
|
-
key.at(-2) ===
|
|
82
|
+
key.at(-2) === escapeToken ?
|
|
91
83
|
{ kind: "required", key: `${key.slice(0, -2)}?` }
|
|
92
84
|
: {
|
|
93
85
|
kind: "optional",
|
|
@@ -95,13 +87,14 @@ const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
|
|
|
95
87
|
}
|
|
96
88
|
: key[0] === "[" && key.at(-1) === "]" ?
|
|
97
89
|
{ kind: "index", key: key.slice(1, -1) }
|
|
98
|
-
: key[0] ===
|
|
90
|
+
: key[0] === escapeToken && key[1] === "[" && key.at(-1) === "]" ?
|
|
99
91
|
{ kind: "required", key: key.slice(1) }
|
|
100
|
-
: key === "..."
|
|
101
|
-
: {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
: key === "
|
|
105
|
-
: key
|
|
106
|
-
|
|
92
|
+
: key === "..." ? { kind: key, key }
|
|
93
|
+
: key === "+" ? { kind: key, key }
|
|
94
|
+
: {
|
|
95
|
+
kind: "required",
|
|
96
|
+
key: key === "\\..." ? "..."
|
|
97
|
+
: key === "\\+" ? "+"
|
|
98
|
+
: key
|
|
99
|
+
};
|
|
107
100
|
export const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
|
|
@@ -10,6 +10,6 @@ export type validateBound<boundedAst, comparator extends Comparator, limit exten
|
|
|
10
10
|
type isNumericallyBoundable<bounded> = [
|
|
11
11
|
bounded
|
|
12
12
|
] extends [number] ? true : [bounded] extends [string] ? true : [bounded] extends [array] ? true : false;
|
|
13
|
-
export declare const writeDoubleRightBoundMessage: <root extends string>(root: root) =>
|
|
13
|
+
export declare const writeDoubleRightBoundMessage: <root extends string>(root: root) => writeDoubleRightBoundMessage<root>;
|
|
14
14
|
type writeDoubleRightBoundMessage<root extends string> = `Expression ${root} must have at most one right bound`;
|
|
15
15
|
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ErrorMessage } from "@arktype/util";
|
|
2
|
+
import type { UnitLiteral } from "../string/shift/operator/default.js";
|
|
3
|
+
import type { inferAstOut, inferTerminal } from "./infer.js";
|
|
4
|
+
import type { astToString } from "./utils.js";
|
|
5
|
+
import type { validateAst } from "./validate.js";
|
|
6
|
+
export type validateDefault<baseAst, unitLiteral extends UnitLiteral, $, args> = validateAst<baseAst, $, args> extends infer e extends ErrorMessage ? e : inferTerminal<unitLiteral, $, args> extends inferAstOut<baseAst, $, args> ? undefined : ErrorMessage<writeUnassignableDefaultValueMessage<astToString<baseAst>, unitLiteral>>;
|
|
7
|
+
export declare const writeUnassignableDefaultValueMessage: <key extends string, message extends string>(key: key, message: message) => string;
|
|
8
|
+
export type writeUnassignableDefaultValueMessage<baseDef extends string, defaultValue extends string> = `Default value ${defaultValue} is not assignable to ${baseDef}`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const writeUnassignableDefaultValueMessage = (key, message) => `Default value at ${key} ${message}`;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Date, DateLiteral, GenericProps, LimitLiteral, RegexLiteral,
|
|
1
|
+
import type { Date, DateLiteral, Default, GenericProps, LimitLiteral, RegexLiteral, constrain, distillIn, distillOut, inferIntersection, normalizeLimit, string } from "@arktype/schema";
|
|
2
2
|
import type { BigintLiteral, array } from "@arktype/util";
|
|
3
3
|
import type { UnparsedScope, resolve, tryInferSubmoduleReference } from "../../scope.js";
|
|
4
4
|
import type { inferDefinition } from "../definition.js";
|
|
@@ -6,11 +6,12 @@ import type { Comparator, MinComparator } from "../string/reduce/shared.js";
|
|
|
6
6
|
import type { StringLiteral } from "../string/shift/operand/enclosed.js";
|
|
7
7
|
export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>;
|
|
8
8
|
export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>;
|
|
9
|
+
export type inferAstOut<ast, $, args> = distillOut<inferAstRoot<ast, $, args>>;
|
|
9
10
|
export type inferConstrainableAst<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : ast extends string ? inferTerminal<ast, $, args> : never;
|
|
10
11
|
export type GenericInstantiationAst<generic extends GenericProps = GenericProps, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
|
|
11
12
|
export type inferExpression<ast extends array, $, args> = ast extends GenericInstantiationAst<infer generic, infer argAsts> ? inferDefinition<generic["def"], generic["$"]["t"] extends UnparsedScope ? $ : generic["$"]["t"], {
|
|
12
13
|
[i in keyof generic["params"] & `${number}` as generic["params"][i]]: inferConstrainableAst<argAsts[i & keyof argAsts], $, args>;
|
|
13
|
-
}> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? constrain<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
|
|
14
|
+
}> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends "=" ? inferTerminal<ast[2] & string, $, args> extends infer defaultValue ? (In?: inferConstrainableAst<ast[0], $, args>) => Default<defaultValue> : never : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? constrain<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
|
|
14
15
|
export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : constrain<constrainableIn, "exactLength", limit & number> : constrain<constrainableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
|
|
15
16
|
rule: normalizeLimit<limit>;
|
|
16
17
|
exclusive: comparator extends ">" | "<" ? true : false;
|
|
@@ -21,4 +22,4 @@ export type PostfixOperator = "[]";
|
|
|
21
22
|
export type PostfixExpression<operator extends PostfixOperator = PostfixOperator, operand = unknown> = readonly [operand, operator];
|
|
22
23
|
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@";
|
|
23
24
|
export type InfixExpression<operator extends InfixOperator = InfixOperator, l = unknown, r = unknown> = [l, operator, r];
|
|
24
|
-
export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : token extends keyof
|
|
25
|
+
export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : token extends keyof ArkEnv.$ ? ArkEnv.$[token] : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args> : token extends StringLiteral<infer text> ? text : token extends `${infer n extends number}` ? n : token extends BigintLiteral<infer b> ? b : token extends RegexLiteral<infer source> ? string.matching<source> : token extends DateLiteral<infer source> ? Date.literal<source> : tryInferSubmoduleReference<$, token>;
|
|
@@ -2,19 +2,21 @@ import type { arkKind, GenericProps, PrivateDeclaration, writeMissingSubmoduleAc
|
|
|
2
2
|
import type { anyOrNever, BigintLiteral, charsAfterFirst, Completion, ErrorMessage, writeMalformedNumericLiteralMessage } from "@arktype/util";
|
|
3
3
|
import type { Comparator } from "../string/reduce/shared.js";
|
|
4
4
|
import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js";
|
|
5
|
+
import type { UnitLiteral } from "../string/shift/operator/default.js";
|
|
5
6
|
import type { parseString } from "../string/string.js";
|
|
6
7
|
import type { validateRange } from "./bounds.js";
|
|
8
|
+
import type { validateDefault } from "./default.js";
|
|
7
9
|
import type { validateDivisor } from "./divisor.js";
|
|
8
10
|
import type { GenericInstantiationAst, InfixExpression, PostfixExpression } from "./infer.js";
|
|
9
11
|
import type { astToString } from "./utils.js";
|
|
10
|
-
export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends readonly ["keyof", infer operand] ? validateAst<operand, $, args> : ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
12
|
+
export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends (readonly [infer baseAst, "=", infer unitLiteral extends UnitLiteral]) ? validateDefault<baseAst, unitLiteral, $, args> : ast extends readonly ["keyof", infer operand] ? validateAst<operand, $, args> : ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
11
13
|
ast: ast;
|
|
12
14
|
};
|
|
13
15
|
type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
|
|
14
16
|
type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [infer head, ...infer tail] ? validateAst<head, $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateGenericArgs<tail, $, args> : undefined;
|
|
15
|
-
export declare const writeUnsatisfiableExpressionError: <expression extends string>(expression: expression) =>
|
|
17
|
+
export declare const writeUnsatisfiableExpressionError: <expression extends string>(expression: expression) => writeUnsatisfiableExpressionError<expression>;
|
|
16
18
|
export type writeUnsatisfiableExpressionError<expression extends string> = `${expression} results in an unsatisfiable type`;
|
|
17
|
-
export declare const writePrefixedPrivateReferenceMessage: <def extends
|
|
19
|
+
export declare const writePrefixedPrivateReferenceMessage: <def extends PrivateDeclaration>(def: def) => writePrefixedPrivateReferenceMessage<def>;
|
|
18
20
|
export type writePrefixedPrivateReferenceMessage<def extends PrivateDeclaration> = `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
|
|
19
21
|
type validateStringAst<def extends string, $> = def extends `${infer n extends number}` ? number extends n ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer b> ? bigint extends b ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : maybeExtractAlias<def, $> extends infer alias extends keyof $ ? [
|
|
20
22
|
$[alias]
|
|
@@ -13,12 +13,13 @@ type BranchState = {
|
|
|
13
13
|
export type DynamicStateWithRoot = requireKeys<DynamicState, "root">;
|
|
14
14
|
export declare class DynamicState {
|
|
15
15
|
readonly ctx: ParseContext;
|
|
16
|
+
readonly defaultable: boolean;
|
|
16
17
|
readonly scanner: Scanner;
|
|
17
18
|
root: BaseRoot<any> | undefined;
|
|
18
19
|
branches: BranchState;
|
|
19
20
|
finalizer: Scanner.FinalizingLookahead | undefined;
|
|
20
21
|
groups: BranchState[];
|
|
21
|
-
constructor(def: string, ctx: ParseContext);
|
|
22
|
+
constructor(def: string, ctx: ParseContext, defaultable: boolean);
|
|
22
23
|
error(message: string): never;
|
|
23
24
|
hasRoot(): this is DynamicStateWithRoot;
|
|
24
25
|
setRoot(root: BaseRoot): void;
|
|
@@ -6,6 +6,7 @@ import { parseUntilFinalizer } from "../string.js";
|
|
|
6
6
|
import { invertedComparators, minComparators, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnclosedGroupMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
7
7
|
export class DynamicState {
|
|
8
8
|
ctx;
|
|
9
|
+
defaultable;
|
|
9
10
|
scanner;
|
|
10
11
|
// set root type to `any` so that all constraints can be applied
|
|
11
12
|
root;
|
|
@@ -17,8 +18,9 @@ export class DynamicState {
|
|
|
17
18
|
};
|
|
18
19
|
finalizer;
|
|
19
20
|
groups = [];
|
|
20
|
-
constructor(def, ctx) {
|
|
21
|
+
constructor(def, ctx, defaultable) {
|
|
21
22
|
this.ctx = ctx;
|
|
23
|
+
this.defaultable = defaultable;
|
|
22
24
|
this.scanner = new Scanner(def);
|
|
23
25
|
}
|
|
24
26
|
error(message) {
|
|
@@ -102,7 +104,7 @@ export class DynamicState {
|
|
|
102
104
|
this.root = undefined;
|
|
103
105
|
}
|
|
104
106
|
parseUntilFinalizer() {
|
|
105
|
-
return parseUntilFinalizer(new DynamicState(this.scanner.unscanned, this.ctx));
|
|
107
|
+
return parseUntilFinalizer(new DynamicState(this.scanner.unscanned, this.ctx, false));
|
|
106
108
|
}
|
|
107
109
|
parseOperator() {
|
|
108
110
|
return parseOperator(this);
|
|
@@ -30,13 +30,13 @@ export type OpenLeftBound = {
|
|
|
30
30
|
limit: LimitLiteral;
|
|
31
31
|
comparator: MinComparator;
|
|
32
32
|
};
|
|
33
|
-
export declare const writeUnmatchedGroupCloseMessage: <unscanned extends string>(unscanned: unscanned) =>
|
|
33
|
+
export declare const writeUnmatchedGroupCloseMessage: <unscanned extends string>(unscanned: unscanned) => writeUnmatchedGroupCloseMessage<unscanned>;
|
|
34
34
|
export type writeUnmatchedGroupCloseMessage<unscanned extends string> = `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`;
|
|
35
|
-
export declare const writeUnclosedGroupMessage: <missingChar extends string>(missingChar: missingChar) =>
|
|
35
|
+
export declare const writeUnclosedGroupMessage: <missingChar extends string>(missingChar: missingChar) => writeUnclosedGroupMessage<missingChar>;
|
|
36
36
|
export type writeUnclosedGroupMessage<missingChar extends string> = `Missing ${missingChar}`;
|
|
37
|
-
export declare const writeOpenRangeMessage: <min extends LimitLiteral, comparator extends
|
|
37
|
+
export declare const writeOpenRangeMessage: <min extends LimitLiteral, comparator extends MinComparator>(min: min, comparator: comparator) => writeOpenRangeMessage<min, comparator>;
|
|
38
38
|
export type writeOpenRangeMessage<min extends LimitLiteral, comparator extends MinComparator> = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
39
39
|
export type writeUnpairableComparatorMessage<comparator extends Comparator> = `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
40
|
-
export declare const writeUnpairableComparatorMessage: <comparator extends
|
|
41
|
-
export declare const writeMultipleLeftBoundsMessage: <openLimit extends LimitLiteral, openComparator extends
|
|
40
|
+
export declare const writeUnpairableComparatorMessage: <comparator extends Comparator>(comparator: comparator) => writeUnpairableComparatorMessage<comparator>;
|
|
41
|
+
export declare const writeMultipleLeftBoundsMessage: <openLimit extends LimitLiteral, openComparator extends MinComparator, limit extends LimitLiteral, comparator extends MinComparator>(openLimit: openLimit, openComparator: openComparator, limit: limit, comparator: comparator) => writeMultipleLeftBoundsMessage<openLimit, openComparator, limit, comparator>;
|
|
42
42
|
export type writeMultipleLeftBoundsMessage<openLimit extends LimitLiteral, openComparator extends MinComparator, limit extends LimitLiteral, comparator extends MinComparator> = `An expression may have at most one left bound (parsed ${openLimit}${InvertedComparators[openComparator]}, ${limit}${InvertedComparators[comparator]})`;
|
|
@@ -2,10 +2,8 @@ import type { DateLiteral } from "@arktype/schema";
|
|
|
2
2
|
export declare const isDateLiteral: (value: unknown) => value is DateLiteral;
|
|
3
3
|
export declare const isValidDate: (d: Date) => boolean;
|
|
4
4
|
export declare const extractDateLiteralSource: <literal extends DateLiteral>(literal: literal) => extractDateLiteralSource<literal>;
|
|
5
|
-
|
|
6
|
-
export declare const writeInvalidDateMessage: <source extends string>(source: source) => `'${source}' could not be parsed by the Date constructor`;
|
|
5
|
+
export declare const writeInvalidDateMessage: <source extends string>(source: source) => writeInvalidDateMessage<source>;
|
|
7
6
|
export type writeInvalidDateMessage<source extends string> = `'${source}' could not be parsed by the Date constructor`;
|
|
8
7
|
export type DateInput = ConstructorParameters<typeof Date>[0];
|
|
9
8
|
export type DateParseResult<errorOnFail extends boolean | string = boolean | string> = Date | (errorOnFail extends true | string ? never : undefined);
|
|
10
|
-
export declare const tryParseDate: <errorOnFail extends
|
|
11
|
-
export {};
|
|
9
|
+
export declare const tryParseDate: <errorOnFail extends boolean | string>(source: string, errorOnFail?: errorOnFail) => DateParseResult<errorOnFail>;
|
|
@@ -33,16 +33,6 @@ declare const enclosingCharDescriptions: {
|
|
|
33
33
|
readonly "/": "forward slash";
|
|
34
34
|
};
|
|
35
35
|
type enclosingCharDescriptions = typeof enclosingCharDescriptions;
|
|
36
|
-
export declare const writeUnterminatedEnclosedMessage: <fragment extends string, enclosingStart extends
|
|
37
|
-
readonly '"': "double-quote";
|
|
38
|
-
readonly "'": "single-quote";
|
|
39
|
-
readonly "/": "forward slash";
|
|
40
|
-
}[{
|
|
41
|
-
readonly "d'": "'";
|
|
42
|
-
readonly 'd"': "\"";
|
|
43
|
-
readonly "'": "'";
|
|
44
|
-
readonly '"': "\"";
|
|
45
|
-
readonly "/": "/";
|
|
46
|
-
}[enclosingStart]]}`;
|
|
36
|
+
export declare const writeUnterminatedEnclosedMessage: <fragment extends string, enclosingStart extends EnclosingStartToken>(fragment: fragment, enclosingStart: enclosingStart) => writeUnterminatedEnclosedMessage<fragment, enclosingStart>;
|
|
47
37
|
export type writeUnterminatedEnclosedMessage<fragment extends string, enclosingStart extends EnclosingStartToken> = `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[EnclosingTokens[enclosingStart]]}`;
|
|
48
38
|
export {};
|
|
@@ -23,6 +23,6 @@ type _parseGenericArgs<name extends string, params extends string[], unscanned e
|
|
|
23
23
|
asts: infer nextAsts extends unknown[];
|
|
24
24
|
unscanned: infer nextUnscanned extends string;
|
|
25
25
|
}) ? finalArgState["finalizer"] extends ">" ? nextAsts["length"] extends params["length"] ? ParsedArgs<nextAsts, nextUnscanned> : state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>> : finalArgState["finalizer"] extends "," ? _parseGenericArgs<name, params, nextUnscanned, $, args, nextDefs, nextAsts> : finalArgState["finalizer"] extends ErrorMessage ? finalArgState : state.error<writeUnclosedGroupMessage<">">> : never : never;
|
|
26
|
-
export declare const writeInvalidGenericArgsMessage: <name extends string, params extends string[], argDefs extends string[]>(name: name, params: params, argDefs: argDefs) =>
|
|
26
|
+
export declare const writeInvalidGenericArgsMessage: <name extends string, params extends string[], argDefs extends string[]>(name: name, params: params, argDefs: argDefs) => writeInvalidGenericArgsMessage<name, params, argDefs>;
|
|
27
27
|
export type writeInvalidGenericArgsMessage<name extends string, params extends string[], argDefs extends string[]> = `${name}<${join<params, ", ">}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends (0) ? "" : `: ${join<argDefs, ",">}`})`;
|
|
28
28
|
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { type WhiteSpaceToken } from "@arktype/util";
|
|
1
2
|
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
2
3
|
import type { StaticState, state } from "../../reduce/static.js";
|
|
3
4
|
import type { BaseCompletions } from "../../string.js";
|
|
4
|
-
import { Scanner } from "../scanner.js";
|
|
5
|
+
import type { Scanner } from "../scanner.js";
|
|
5
6
|
import { parseEnclosed, type EnclosingQuote, type EnclosingStartToken } from "./enclosed.js";
|
|
6
7
|
import { parseUnenclosed } from "./unenclosed.js";
|
|
7
8
|
export declare const parseOperand: (s: DynamicState) => void;
|
|
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
|
|
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>}`>;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { whiteSpaceTokens } from "@arktype/util";
|
|
2
2
|
import { enclosingChar, enclosingQuote, parseEnclosed } from "./enclosed.js";
|
|
3
3
|
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
4
4
|
export const parseOperand = (s) => s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s))
|
|
5
5
|
: s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
|
|
6
6
|
: s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
|
|
7
|
-
: s.scanner.lookaheadIsIn(
|
|
8
|
-
parseOperand(s.shiftedByOne())
|
|
7
|
+
: s.scanner.lookaheadIsIn(whiteSpaceTokens) ? parseOperand(s.shiftedByOne())
|
|
9
8
|
: s.scanner.lookahead === "d" ?
|
|
10
9
|
s.scanner.nextLookahead in enclosingQuote ?
|
|
11
10
|
parseEnclosed(s, `${s.scanner.shift()}${s.scanner.shift()}`)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseRoot, writeUnresolvableMessage, type GenericProps, type
|
|
1
|
+
import { BaseRoot, writeUnresolvableMessage, type GenericProps, type arkKind, type writeNonSubmoduleDotMessage } from "@arktype/schema";
|
|
2
2
|
import { type BigintLiteral, type Completion, type ErrorMessage, type anyOrNever, type join } from "@arktype/util";
|
|
3
3
|
import type { Generic } from "../../../../generic.js";
|
|
4
4
|
import type { GenericInstantiationAst } from "../../../semantic/infer.js";
|
|
@@ -13,7 +13,7 @@ export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntil
|
|
|
13
13
|
] extends [anyOrNever] ? state.setRoot<s, result, unscanned> : $[result] extends GenericProps ? parseGenericInstantiation<token, $[result], state.scanTo<s, unscanned>, $, args> : state.setRoot<s, result, unscanned> : state.setRoot<s, result, unscanned> : never : never;
|
|
14
14
|
export declare const parseGenericInstantiation: (name: string, g: Generic, s: DynamicState) => BaseRoot;
|
|
15
15
|
export type parseGenericInstantiation<name extends string, g extends GenericProps, s extends StaticState, $, args> = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g["params"], unscanned, $, args> extends (infer result) ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgsMessage<name, g["params"], []>>;
|
|
16
|
-
type tryResolve<s extends StaticState, token extends string, $, args> = token extends keyof
|
|
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
17
|
type tryResolveSubmodule<token, submodule extends keyof $ & string, reference extends string, s extends StaticState, $, args> = $[submodule] extends {
|
|
18
18
|
[arkKind]: "module";
|
|
19
19
|
} ? reference extends keyof $[submodule] ? token : unresolvableError<s, reference, $[submodule], args, [submodule]> : ErrorMessage<writeNonSubmoduleDotMessage<submodule>>;
|
|
@@ -24,7 +24,7 @@ type qualifiedReference<reference extends string, submodulePath extends string[]
|
|
|
24
24
|
type validReferenceFromToken<token extends string, $, args, submodulePath extends string[]> = Extract<submodulePath extends [] ? BaseCompletions<$, args> : keyof $, `${token}${string}`>;
|
|
25
25
|
export declare const writeMissingOperandMessage: (s: DynamicState) => string;
|
|
26
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) =>
|
|
28
|
-
export declare const writeExpressionExpectedMessage: <unscanned extends string>(unscanned: 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
29
|
export type writeExpressionExpectedMessage<unscanned extends string> = `Expected an expression${unscanned extends "" ? "" : ` before '${unscanned}'`}`;
|
|
30
30
|
export {};
|