arktype 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.cjs +9 -0
- package/dist/api.d.ts +10 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/nodes/branch.js +15 -6
- package/dist/cjs/nodes/compose.js +1 -1
- package/dist/cjs/nodes/discriminate.js +3 -3
- package/dist/cjs/nodes/node.js +11 -11
- package/dist/cjs/nodes/predicate.js +1 -1
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/nodes/rules/rules.js +4 -0
- package/dist/cjs/parse/ast/ast.js +4 -0
- package/dist/cjs/parse/ast/bound.js +9 -0
- package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
- package/dist/cjs/parse/ast/divisor.js +9 -0
- package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
- package/dist/cjs/parse/ast/keyof.js +109 -0
- package/dist/cjs/parse/{tuple → ast}/morph.js +0 -0
- package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
- package/dist/cjs/parse/{tuple → ast}/tuple.js +8 -8
- package/dist/cjs/parse/ast/union.js +9 -0
- package/dist/cjs/parse/ast/utils.js +4 -0
- package/dist/cjs/parse/definition.js +12 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
- package/dist/cjs/parse/string/shift/operator/bounds.js +3 -3
- package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/cjs/parse/string/string.js +1 -1
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/scope.js +56 -4
- package/dist/cjs/traverse/problems.js +59 -29
- package/dist/cjs/traverse/traverse.js +24 -27
- package/dist/cjs/utils/data.js +39 -0
- package/dist/cjs/utils/generics.js +20 -2
- package/dist/mjs/nodes/branch.js +14 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +3 -3
- package/dist/mjs/nodes/node.js +8 -8
- package/dist/mjs/nodes/predicate.js +2 -2
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/range.js +2 -2
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +4 -0
- package/dist/mjs/parse/ast/ast.js +1 -0
- package/dist/mjs/parse/ast/bound.js +1 -0
- package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
- package/dist/mjs/parse/ast/divisor.js +1 -0
- package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
- package/dist/mjs/parse/ast/keyof.js +101 -0
- package/dist/mjs/parse/{tuple → ast}/morph.js +0 -0
- package/dist/mjs/parse/ast/narrow.js +7 -0
- package/dist/mjs/parse/{tuple → ast}/tuple.js +9 -9
- package/dist/mjs/parse/ast/union.js +1 -0
- package/dist/mjs/parse/ast/utils.js +1 -0
- package/dist/mjs/parse/definition.js +12 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
- package/dist/mjs/parse/string/shift/operator/bounds.js +3 -3
- package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/scopes/expressions.js +9 -0
- package/dist/mjs/scopes/scope.js +55 -3
- package/dist/mjs/traverse/problems.js +59 -28
- package/dist/mjs/traverse/traverse.js +26 -30
- package/dist/mjs/utils/data.js +24 -0
- package/dist/mjs/utils/generics.js +18 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +3 -12
- package/dist/types/nodes/discriminate.d.ts +2 -2
- package/dist/types/nodes/infer.d.ts +5 -5
- package/dist/types/nodes/node.d.ts +4 -13
- package/dist/types/nodes/predicate.d.ts +2 -29
- package/dist/types/nodes/rules/rules.d.ts +1 -1
- package/dist/types/parse/ast/ast.d.ts +24 -0
- package/dist/types/parse/ast/bound.d.ts +10 -0
- package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
- package/dist/types/parse/ast/divisor.d.ts +8 -0
- package/dist/types/parse/ast/intersection.d.ts +17 -0
- package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
- package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
- package/dist/types/parse/ast/narrow.d.ts +21 -0
- package/dist/types/parse/ast/tuple.d.ts +40 -0
- package/dist/types/parse/ast/union.d.ts +12 -0
- package/dist/types/parse/ast/utils.d.ts +7 -0
- package/dist/types/parse/definition.d.ts +9 -7
- package/dist/types/parse/record.d.ts +2 -2
- package/dist/types/parse/string/reduce/shared.d.ts +5 -4
- package/dist/types/parse/string/reduce/static.d.ts +11 -6
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
- package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
- package/dist/types/parse/string/string.d.ts +1 -2
- package/dist/types/scopes/expressions.d.ts +48 -0
- package/dist/types/scopes/scope.d.ts +17 -7
- package/dist/types/scopes/type.d.ts +9 -5
- package/dist/types/traverse/problems.d.ts +12 -21
- package/dist/types/traverse/traverse.d.ts +66 -37
- package/dist/types/utils/data.d.ts +13 -0
- package/dist/types/utils/domains.d.ts +1 -1
- package/dist/types/utils/generics.d.ts +13 -9
- package/dist/types/utils/paths.d.ts +2 -0
- package/package.json +1 -1
- package/dist/cjs/parse/tuple/keyof.js +0 -64
- package/dist/cjs/utils/size.js +0 -16
- package/dist/mjs/parse/tuple/keyof.js +0 -56
- package/dist/mjs/parse/tuple/narrow.js +0 -7
- package/dist/mjs/utils/size.js +0 -2
- package/dist/types/parse/string/ast.d.ts +0 -46
- package/dist/types/parse/tuple/narrow.d.ts +0 -13
- package/dist/types/parse/tuple/tuple.d.ts +0 -31
- package/dist/types/utils/size.d.ts +0 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { error, isAny } from "../../utils/generics";
|
|
2
|
+
import type { inferAst, validateAst } from "./ast";
|
|
3
|
+
import type { astToString } from "./utils";
|
|
4
|
+
export type validateDivisor<l, $> = isDivisible<inferAst<l, $>> extends true ? validateAst<l, $> : error<writeIndivisibleMessage<astToString<l>>>;
|
|
5
|
+
type isDivisible<data> = isAny<data> extends true ? false : [data] extends [number] ? true : false;
|
|
6
|
+
export declare const writeIndivisibleMessage: <root extends string>(root: root) => `Divisibility operand ${root} must be a number`;
|
|
7
|
+
type writeIndivisibleMessage<root extends string> = `Divisibility operand ${root} must be a number`;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { DisjointsByPath } from "../../nodes/compose.js";
|
|
2
|
+
import type { MappedKeys } from "../../nodes/rules/props.js";
|
|
3
|
+
import type { asConst, Dict, error, evaluate, extractValues, isAny, List, stringKeyOf, tryCatch } from "../../utils/generics.js";
|
|
4
|
+
import type { Path, pathToString } from "../../utils/paths.js";
|
|
5
|
+
import type { Out, ParsedMorph } from "./morph.js";
|
|
6
|
+
export type inferIntersection<l, r> = inferIntersectionRecurse<l, r, []>;
|
|
7
|
+
type inferIntersectionRecurse<l, r, path extends string[]> = path["length"] extends 10 ? l & r : l extends never ? never : r extends never ? never : l & r extends never ? error<writeImplicitNeverMessage<path, "Intersection">> : isAny<l | r> extends true ? any : l extends ParsedMorph<infer lIn, infer lOut> ? r extends ParsedMorph ? error<writeImplicitNeverMessage<path, "Intersection", "of morphs">> : (In: evaluate<lIn & r>) => Out<lOut> : r extends ParsedMorph<infer rIn, infer rOut> ? (In: evaluate<rIn & l>) => Out<rOut> : [l, r] extends [Dict, Dict] ? bubblePropErrors<evaluate<{
|
|
8
|
+
[k in stringKeyOf<l>]: k extends keyof r ? inferIntersectionRecurse<l[k], r[k], [...path, k]> : l[k];
|
|
9
|
+
} & Omit<r, keyof l>>> : [l, r] extends [List<infer lItem>, List<infer rItem>] ? inferIntersectionRecurse<lItem, rItem, [
|
|
10
|
+
...path,
|
|
11
|
+
MappedKeys["index"]
|
|
12
|
+
]> extends infer result ? tryCatch<result, result[]> : never : l & r;
|
|
13
|
+
type bubblePropErrors<o> = extractValues<o, error> extends never ? o : extractValues<o, error>;
|
|
14
|
+
export declare const compileDisjointReasonsMessage: (disjoints: DisjointsByPath) => string;
|
|
15
|
+
export declare const writeImplicitNeverMessage: <path extends [] | Path, operator extends "keyof" | "Intersection", description extends string = "">(path: asConst<path>, operator: operator, description?: description | undefined) => `${path extends [] ? "" : `At ${pathToString<path, "/">}: `}${operator} ${description extends "" ? "" : `${description} `}results in an unsatisfiable type`;
|
|
16
|
+
export type writeImplicitNeverMessage<path extends string[], operator extends "Intersection" | "keyof", description extends string = ""> = `${path extends [] ? "" : `At ${pathToString<path>}: `}${operator} ${description extends "" ? "" : `${description} `}results in an unsatisfiable type`;
|
|
17
|
+
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { evaluate } from "../../utils/generics.js";
|
|
2
2
|
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
3
|
-
import { writeImplicitNeverMessage } from "
|
|
3
|
+
import { writeImplicitNeverMessage } from "./intersection.js";
|
|
4
4
|
import type { PrefixParser } from "./tuple.js";
|
|
5
5
|
export declare const parseKeyOfTuple: PrefixParser<"keyof">;
|
|
6
|
-
export type inferKeyOfExpression<operandDef, $> =
|
|
7
|
-
export type validateKeyOfExpression<operandDef, $> = [
|
|
6
|
+
export type inferKeyOfExpression<operandDef, $> = evaluate<keyof inferDefinition<operandDef, $>>;
|
|
7
|
+
export type validateKeyOfExpression<operandDef, $> = readonly [
|
|
8
8
|
"keyof",
|
|
9
9
|
inferKeyOfExpression<operandDef, $> extends never ? writeImplicitNeverMessage<[], "keyof"> : validateDefinition<operandDef, $>
|
|
10
10
|
];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { asOut } from "../../scopes/type.js";
|
|
1
|
+
import type { asIn, asOut } from "../../scopes/type.js";
|
|
2
2
|
import type { nominal } from "../../utils/generics.js";
|
|
3
3
|
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
4
4
|
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
@@ -7,9 +7,9 @@ export type Out<t = {}> = nominal<t, "out">;
|
|
|
7
7
|
export type validateMorphTuple<def extends TupleExpression, $> = readonly [
|
|
8
8
|
_: validateDefinition<def[0], $>,
|
|
9
9
|
_: "=>",
|
|
10
|
-
_: Morph<asOut<inferDefinition<def[0], $>>,
|
|
11
|
-
_?: validateDefinition<def[3], $>
|
|
10
|
+
_: Morph<asOut<inferDefinition<def[0], $>>, unknown>
|
|
12
11
|
];
|
|
13
12
|
export type Morph<i = any, o = unknown> = (In: i) => o;
|
|
14
13
|
export type ParsedMorph<i = any, o = unknown> = (In: i) => Out<o>;
|
|
14
|
+
export type inferMorph<inDef, morph, $> = morph extends Morph ? (In: asIn<inferDefinition<inDef, $>>) => Out<ReturnType<morph>> : never;
|
|
15
15
|
export declare const writeMalformedMorphExpressionMessage: (value: unknown) => string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { asIn } from "../../scopes/type.js";
|
|
2
|
+
import type { Problems } from "../../traverse/problems.js";
|
|
3
|
+
import type { Domain, inferDomain } from "../../utils/domains.js";
|
|
4
|
+
import type { inferDefinition, validateDefinition } from "../definition.js";
|
|
5
|
+
import type { distributable } from "./distributableFunction.js";
|
|
6
|
+
import type { PostfixParser, TupleExpression } from "./tuple.js";
|
|
7
|
+
export declare const parseNarrowTuple: PostfixParser<":">;
|
|
8
|
+
export type Narrow<data = any> = (data: data, problems: Problems) => boolean;
|
|
9
|
+
export type NarrowCast<data = any, to extends data = data> = (data: data, problems: Problems) => data is to;
|
|
10
|
+
export type validateNarrowTuple<def extends TupleExpression, $> = readonly [
|
|
11
|
+
_: validateDefinition<def[0], $>,
|
|
12
|
+
_: ":",
|
|
13
|
+
_: distributable<Narrow<asIn<inferDefinition<def[0], $>>>>
|
|
14
|
+
];
|
|
15
|
+
export type inferNarrow<inDef, narrow, $> = narrow extends Narrow ? inferNarrowFunction<inferDefinition<inDef, $>, narrow> : narrow extends {
|
|
16
|
+
[domain in Domain]?: Narrow;
|
|
17
|
+
} ? {
|
|
18
|
+
[domain in keyof narrow & Domain]: inferNarrowFunction<Extract<inferDefinition<inDef, $>, inferDomain<domain>>, narrow[domain]>;
|
|
19
|
+
}[keyof narrow & Domain] : never;
|
|
20
|
+
type inferNarrowFunction<input, narrow> = narrow extends NarrowCast<input, infer to> ? to : narrow extends Narrow<input> ? input : never;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { inferNode } from "../../nodes/infer.js";
|
|
2
|
+
import type { ResolvedNode, TypeNode } from "../../nodes/node.js";
|
|
3
|
+
import type { conform, constructor, Dict, error, evaluate, List } from "../../utils/generics.js";
|
|
4
|
+
import type { inferDefinition, ParseContext, validateDefinition } from "../definition.js";
|
|
5
|
+
import { writeMissingRightOperandMessage } from "../string/shift/operand/unenclosed.js";
|
|
6
|
+
import type { Scanner } from "../string/shift/scanner.js";
|
|
7
|
+
import type { inferIntersection } from "./intersection.js";
|
|
8
|
+
import type { inferKeyOfExpression, validateKeyOfExpression } from "./keyof.js";
|
|
9
|
+
import type { inferMorph, validateMorphTuple } from "./morph.js";
|
|
10
|
+
import type { inferNarrow, validateNarrowTuple } from "./narrow.js";
|
|
11
|
+
import type { inferUnion } from "./union.js";
|
|
12
|
+
export declare const parseTuple: (def: List, ctx: ParseContext) => TypeNode;
|
|
13
|
+
export type validateTupleExpression<def extends TupleExpression, $> = def[1] extends "[]" ? conform<def, readonly [validateDefinition<def[0], $>, "[]"]> : def[1] extends Scanner.BranchToken ? def[2] extends undefined ? [def[0], error<writeMissingRightOperandMessage<def[1], "">>] : conform<def, readonly [
|
|
14
|
+
validateDefinition<def[0], $>,
|
|
15
|
+
def[1],
|
|
16
|
+
validateDefinition<def[2], $>
|
|
17
|
+
]> : def[1] extends "=>" ? validateMorphTuple<def, $> : def[1] extends ":" ? validateNarrowTuple<def, $> : def[0] extends "===" ? conform<def, readonly ["===", unknown]> : def[0] extends "instanceof" ? conform<def, readonly ["instanceof", constructor]> : def[0] extends "node" ? conform<def, readonly ["node", ResolvedNode<$>]> : def[0] extends "keyof" ? conform<def, validateKeyOfExpression<def[1], $>> : never;
|
|
18
|
+
export type UnparsedTupleExpressionInput<$> = {
|
|
19
|
+
instanceof: constructor;
|
|
20
|
+
node: ResolvedNode<$>;
|
|
21
|
+
"===": unknown;
|
|
22
|
+
};
|
|
23
|
+
export type UnparsedTupleOperator = evaluate<keyof UnparsedTupleExpressionInput<Dict>>;
|
|
24
|
+
export type inferTuple<def extends List, $> = def extends TupleExpression ? inferTupleExpression<def, $> : {
|
|
25
|
+
[i in keyof def]: inferDefinition<def[i], $>;
|
|
26
|
+
};
|
|
27
|
+
export type inferTupleExpression<def extends TupleExpression, $> = def[1] extends "[]" ? inferDefinition<def[0], $>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $>, inferDefinition<def[2], $>> : def[1] extends "|" ? inferUnion<inferDefinition<def[0], $>, inferDefinition<def[2], $>> : def[1] extends ":" ? inferNarrow<def[0], def[2], $> : def[1] extends "=>" ? inferMorph<def[0], def[2], $> : def[0] extends "===" ? def[1] : def[0] extends "instanceof" ? def[1] extends constructor<infer t> ? t : never : def[0] extends "node" ? def[1] extends ResolvedNode<$> ? inferNode<def[1], $> : never : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $> : never;
|
|
28
|
+
export type PostfixParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: ParseContext) => TypeNode;
|
|
29
|
+
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: ParseContext) => TypeNode;
|
|
30
|
+
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
31
|
+
export declare const writeMalformedFunctionalExpressionMessage: (operator: "=>" | ":", rightDef: unknown) => string;
|
|
32
|
+
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
33
|
+
type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
34
|
+
export type TuplePostfixOperator = "[]";
|
|
35
|
+
export type TupleInfixOperator = "&" | "|" | ":" | "=>";
|
|
36
|
+
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
37
|
+
export type FunctionalTupleOperator = ":" | "=>";
|
|
38
|
+
export type IndexZeroOperator = "keyof" | "instanceof" | "===" | "node";
|
|
39
|
+
export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { asIn } from "../../scopes/type.js";
|
|
2
|
+
import type { domainOf } from "../../utils/domains.js";
|
|
3
|
+
import type { equals, error, extractValues, isAny, requiredKeyOf } from "../../utils/generics.js";
|
|
4
|
+
import type { objectKindOf } from "../../utils/objectKinds.js";
|
|
5
|
+
export type inferUnion<l, r> = isAny<l | r> extends true ? any : [l] extends [never] ? r : [r] extends [never] ? l : [asIn<l>, asIn<r>] extends [infer lIn, infer rIn] ? [equals<l, lIn>, equals<r, rIn>] extends [true, true] ? l | r : discriminatable<lIn, rIn> extends true ? l | r : error<undiscriminatableMorphUnionMessage> : never;
|
|
6
|
+
type discriminatable<l, r> = discriminatableRecurse<l, r, []> extends never ? false : true;
|
|
7
|
+
type discriminatableRecurse<l, r, path extends string[]> = path["length"] extends 10 ? never : l & r extends never ? path : domainOf<l> & domainOf<r> extends never ? path : objectKindOf<l> & objectKindOf<r> extends never ? path : [objectKindOf<l>, objectKindOf<r>] extends ["Object", "Object"] ? extractValues<{
|
|
8
|
+
[k in requiredKeyOf<l>]: k extends requiredKeyOf<r> ? discriminatableRecurse<l[k], r[k], [...path, k & string]> : never;
|
|
9
|
+
}, string[]> : never;
|
|
10
|
+
export declare const writeUndiscriminatableMorphUnionMessage: <path extends string>(path: path) => string;
|
|
11
|
+
type undiscriminatableMorphUnionMessage = `A union including one or more morphs must be discriminatable`;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Literalable } from "../../utils/generics.js";
|
|
2
|
+
export type astToString<ast> = `'${astToStringRecurse<ast, "">}'`;
|
|
3
|
+
type astToStringRecurse<ast, result extends string> = ast extends [
|
|
4
|
+
infer head,
|
|
5
|
+
...infer tail
|
|
6
|
+
] ? astToStringRecurse<tail, `${result}${astToStringRecurse<head, "">}`> : ast extends Literalable ? `${result}${ast extends bigint ? `${ast}n` : ast}` : "...";
|
|
7
|
+
export {};
|
|
@@ -2,19 +2,20 @@ import type { TypeNode } from "../nodes/node.js";
|
|
|
2
2
|
import type { Type } from "../scopes/type.js";
|
|
3
3
|
import type { Primitive } from "../utils/domains.js";
|
|
4
4
|
import { domainOf } from "../utils/domains.js";
|
|
5
|
-
import type { Dict,
|
|
5
|
+
import type { Dict, evaluate, isAny, isUnknown, List } from "../utils/generics.js";
|
|
6
6
|
import { objectKindOf } from "../utils/objectKinds.js";
|
|
7
7
|
import type { Path } from "../utils/paths.js";
|
|
8
|
+
import type { validateString } from "./ast/ast.js";
|
|
9
|
+
import type { inferTuple, TupleExpression, validateTupleExpression } from "./ast/tuple.js";
|
|
8
10
|
import type { inferRecord } from "./record.js";
|
|
9
|
-
import type { inferString
|
|
10
|
-
import type { inferTuple, TupleExpression, validateTupleExpression } from "./tuple/tuple.js";
|
|
11
|
+
import type { inferString } from "./string/string.js";
|
|
11
12
|
export type ParseContext = {
|
|
12
13
|
type: Type;
|
|
13
14
|
path: Path;
|
|
14
15
|
};
|
|
15
16
|
export declare const parseDefinition: (def: unknown, ctx: ParseContext) => TypeNode;
|
|
16
|
-
export type inferDefinition<def, $> = isAny<def> extends true ? never : def extends inferred<infer t> ? t : def extends string ? inferString<def, $> : def extends List ? inferTuple<def, $> : def extends RegExp ? string : def extends Dict ? inferRecord<def, $> : never;
|
|
17
|
-
export type validateDefinition<def, $> = def extends Terminal ? def : def extends string ? validateString<def, $> : def extends TupleExpression ? validateTupleExpression<def, $> : def extends BadDefinitionType ? writeBadDefinitionTypeMessage<objectKindOf<def> extends string ? objectKindOf<def> : domainOf<def>> : isUnknown<def> extends true ? unknownDefinitionMessage :
|
|
17
|
+
export type inferDefinition<def, $> = isAny<def> extends true ? never : def extends inferred<infer t> | InferredThunk<infer t> ? t : def extends string ? inferString<def, $> : def extends List ? inferTuple<def, $> : def extends RegExp ? string : def extends Dict ? inferRecord<def, $> : never;
|
|
18
|
+
export type validateDefinition<def, $> = def extends (...args: any[]) => any ? def : def extends Terminal ? def : def extends string ? validateString<def, $> : def extends TupleExpression ? validateTupleExpression<def, $> : def extends BadDefinitionType ? writeBadDefinitionTypeMessage<objectKindOf<def> extends string ? objectKindOf<def> : domainOf<def>> : isUnknown<def> extends true ? unknownDefinitionMessage : evaluate<{
|
|
18
19
|
[k in keyof def]: validateDefinition<def[k], $>;
|
|
19
20
|
}>;
|
|
20
21
|
export declare const as: unique symbol;
|
|
@@ -23,8 +24,9 @@ export type inferred<t> = {
|
|
|
23
24
|
};
|
|
24
25
|
export declare const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
25
26
|
export type unknownDefinitionMessage = typeof unknownDefinitionMessage;
|
|
26
|
-
type
|
|
27
|
-
type
|
|
27
|
+
type InferredThunk<t = unknown> = () => inferred<t>;
|
|
28
|
+
type Terminal = RegExp | inferred<unknown> | InferredThunk;
|
|
29
|
+
type BadDefinitionType = Exclude<Primitive, string>;
|
|
28
30
|
export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => `Type definitions must be strings or objects (was ${actual})`;
|
|
29
31
|
type writeBadDefinitionTypeMessage<actual extends string> = `Type definitions must be strings or objects (was ${actual})`;
|
|
30
32
|
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { ResolvedNode } from "../nodes/node.js";
|
|
2
|
-
import type { Dict,
|
|
2
|
+
import type { Dict, evaluate } from "../utils/generics.js";
|
|
3
3
|
import type { inferDefinition, ParseContext } from "./definition.js";
|
|
4
4
|
import { Scanner } from "./string/shift/scanner.js";
|
|
5
5
|
export declare const parseRecord: (def: Dict, ctx: ParseContext) => ResolvedNode;
|
|
6
6
|
type withPossiblePreviousEscapeCharacter<k> = k extends `${infer name}?` ? `${name}${Scanner.EscapeToken}?` : k;
|
|
7
|
-
export type inferRecord<def extends Dict, $> =
|
|
7
|
+
export type inferRecord<def extends Dict, $> = evaluate<{
|
|
8
8
|
[requiredKeyName in requiredKeyOf<def>]: inferDefinition<def[withPossiblePreviousEscapeCharacter<requiredKeyName>], $>;
|
|
9
9
|
} & {
|
|
10
10
|
[optionalKeyName in optionalKeyOf<def>]?: inferDefinition<def[`${optionalKeyName}?`], $>;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { MinComparator } from "../../../nodes/rules/range.js";
|
|
2
|
+
import type { NumberLiteral } from "../../../utils/numericLiterals.js";
|
|
2
3
|
import { Scanner } from "../shift/scanner.js";
|
|
3
4
|
export declare const writeUnmatchedGroupCloseMessage: <unscanned extends string>(unscanned: unscanned) => `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`;
|
|
4
5
|
export type writeUnmatchedGroupCloseMessage<unscanned extends string> = `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`;
|
|
5
6
|
export declare const unclosedGroupMessage = "Missing )";
|
|
6
7
|
export type unclosedGroupMessage = typeof unclosedGroupMessage;
|
|
7
|
-
export declare const writeOpenRangeMessage: <min extends number
|
|
8
|
-
export type writeOpenRangeMessage<min extends
|
|
8
|
+
export declare const writeOpenRangeMessage: <min extends `${number}`, comparator extends ">" | ">=">(min: min, comparator: comparator) => `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
9
|
+
export type writeOpenRangeMessage<min extends NumberLiteral, comparator extends MinComparator> = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
9
10
|
export type writeUnpairableComparatorMessage<comparator extends Scanner.Comparator> = `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
10
11
|
export declare const writeUnpairableComparatorMessage: <comparator extends ">" | "<" | ">=" | "<=" | "==">(comparator: comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
11
|
-
export declare const writeMultipleLeftBoundsMessage: <openLimit extends number
|
|
12
|
+
export declare const writeMultipleLeftBoundsMessage: <openLimit extends `${number}`, openComparator extends ">" | ">=", limit extends `${number}`, comparator extends ">" | ">=">(openLimit: openLimit, openComparator: openComparator, limit: limit, comparator: comparator) => `An expression may have at most one left bound (parsed ${openLimit}${{
|
|
12
13
|
readonly "<": ">";
|
|
13
14
|
readonly ">": "<";
|
|
14
15
|
readonly "<=": ">=";
|
|
@@ -21,4 +22,4 @@ export declare const writeMultipleLeftBoundsMessage: <openLimit extends number,
|
|
|
21
22
|
readonly ">=": "<=";
|
|
22
23
|
readonly "==": "==";
|
|
23
24
|
}[comparator]})`;
|
|
24
|
-
export type writeMultipleLeftBoundsMessage<openLimit extends
|
|
25
|
+
export type writeMultipleLeftBoundsMessage<openLimit extends NumberLiteral, openComparator extends MinComparator, limit extends NumberLiteral, comparator extends MinComparator> = `An expression may have at most one left bound (parsed ${openLimit}${Scanner.InvertedComparators[openComparator]}, ${limit}${Scanner.InvertedComparators[comparator]})`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MaxComparator, MinComparator } from "../../../nodes/rules/range.js";
|
|
2
2
|
import type { defined, error } from "../../../utils/generics.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { NumberLiteral } from "../../../utils/numericLiterals.js";
|
|
4
|
+
import type { astToString } from "../../ast/utils.js";
|
|
4
5
|
import type { Scanner } from "../shift/scanner.js";
|
|
5
6
|
import type { unclosedGroupMessage, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.js";
|
|
6
7
|
export type StaticState = {
|
|
@@ -9,8 +10,12 @@ export type StaticState = {
|
|
|
9
10
|
groups: BranchState[];
|
|
10
11
|
unscanned: string;
|
|
11
12
|
};
|
|
13
|
+
type StaticOpenLeftBound = {
|
|
14
|
+
limit: NumberLiteral;
|
|
15
|
+
comparator: MinComparator;
|
|
16
|
+
};
|
|
12
17
|
type BranchState = {
|
|
13
|
-
range:
|
|
18
|
+
range: StaticOpenLeftBound | undefined;
|
|
14
19
|
"&": unknown;
|
|
15
20
|
"|": unknown;
|
|
16
21
|
};
|
|
@@ -42,7 +47,7 @@ export declare namespace state {
|
|
|
42
47
|
groups: s["groups"];
|
|
43
48
|
unscanned: unscanned;
|
|
44
49
|
}>;
|
|
45
|
-
export type reduceLeftBound<s extends StaticState, limit extends
|
|
50
|
+
export type reduceLeftBound<s extends StaticState, limit extends NumberLiteral, comparator extends Scanner.Comparator, unscanned extends string> = comparator extends "<" | "<=" ? s["branches"]["range"] extends {} ? error<writeMultipleLeftBoundsMessage<s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], limit, Scanner.InvertedComparators[comparator]>> : from<{
|
|
46
51
|
root: undefined;
|
|
47
52
|
branches: {
|
|
48
53
|
range: {
|
|
@@ -55,7 +60,7 @@ export declare namespace state {
|
|
|
55
60
|
groups: s["groups"];
|
|
56
61
|
unscanned: unscanned;
|
|
57
62
|
}> : error<writeUnpairableComparatorMessage<comparator>>;
|
|
58
|
-
export type reduceRange<s extends StaticState, minLimit extends
|
|
63
|
+
export type reduceRange<s extends StaticState, minLimit extends NumberLiteral, minComparator extends MinComparator, maxComparator extends MaxComparator, maxLimit extends NumberLiteral, unscanned extends string> = state.from<{
|
|
59
64
|
root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]];
|
|
60
65
|
branches: {
|
|
61
66
|
range: undefined;
|
|
@@ -65,7 +70,7 @@ export declare namespace state {
|
|
|
65
70
|
groups: s["groups"];
|
|
66
71
|
unscanned: unscanned;
|
|
67
72
|
}>;
|
|
68
|
-
export type reduceSingleBound<s extends StaticState, comparator extends Scanner.Comparator, limit extends
|
|
73
|
+
export type reduceSingleBound<s extends StaticState, comparator extends Scanner.Comparator, limit extends NumberLiteral, unscanned extends string> = state.from<{
|
|
69
74
|
root: [s["root"], comparator, limit];
|
|
70
75
|
branches: {
|
|
71
76
|
range: undefined;
|
|
@@ -6,7 +6,7 @@ import type { Scanner } from "../scanner.js";
|
|
|
6
6
|
export declare const parseUnenclosed: (s: DynamicState) => void;
|
|
7
7
|
export type parseUnenclosed<s extends StaticState, $> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryResolve<s, scanned, $> extends infer result ? result extends error<infer message> ? error<message> : state.setRoot<s, result, nextUnscanned> : never : never;
|
|
8
8
|
export type isResolvableIdentifier<token, $> = token extends stringKeyOf<$> ? true : false;
|
|
9
|
-
type tryResolve<s extends StaticState, token extends string, $> = isResolvableIdentifier<token, $> extends true ? token : token extends NumberLiteral<infer value> ? number extends value ? error<writeMalformedNumericLiteralMessage<token, "number">> :
|
|
9
|
+
type tryResolve<s extends StaticState, token extends string, $> = isResolvableIdentifier<token, $> extends true ? token : token extends NumberLiteral<infer value> ? number extends value ? error<writeMalformedNumericLiteralMessage<token, "number">> : token : token extends BigintLiteral<infer value> ? bigint extends value ? error<writeMalformedNumericLiteralMessage<token, "bigint">> : token : error<token extends "" ? writeMissingOperandMessage<s> : writeUnresolvableMessage<token>>;
|
|
10
10
|
export declare const writeUnresolvableMessage: <token extends string>(token: token) => `'${token}' is unresolvable`;
|
|
11
11
|
type writeUnresolvableMessage<token extends string> = `'${token}' is unresolvable`;
|
|
12
12
|
export declare const writeMissingOperandMessage: <s extends DynamicState>(s: s) => `Token '|' requires a right operand before '${string}'` | `Token '&' requires a right operand before '${string}'` | `Expected an expression before '${string}'`;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import type { MaxComparator, Range } from "../../../../nodes/rules/range.js";
|
|
2
2
|
import type { error } from "../../../../utils/generics.js";
|
|
3
|
+
import type { NumberLiteral } from "../../../../utils/numericLiterals.js";
|
|
3
4
|
import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
4
5
|
import type { DynamicState } from "../../reduce/dynamic.js";
|
|
5
6
|
import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
6
7
|
import type { state, StaticState } from "../../reduce/static.js";
|
|
7
8
|
import { Scanner } from "../scanner.js";
|
|
8
9
|
export declare const parseBound: (s: DynamicState, start: Scanner.ComparatorStartChar) => void;
|
|
9
|
-
export type parseBound<s extends StaticState, start extends Scanner.ComparatorStartChar, unscanned extends string> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends Scanner.shiftResult<infer comparator extends Scanner.Comparator, infer nextUnscanned> ? s["root"] extends
|
|
10
|
+
export type parseBound<s extends StaticState, start extends Scanner.ComparatorStartChar, unscanned extends string> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends Scanner.shiftResult<infer comparator extends Scanner.Comparator, infer nextUnscanned> ? s["root"] extends NumberLiteral ? state.reduceLeftBound<s, s["root"], comparator, nextUnscanned> : parseRightBound<s, comparator, nextUnscanned> : shiftResultOrError : never;
|
|
10
11
|
declare const shiftComparator: (s: DynamicState, start: Scanner.ComparatorStartChar) => Scanner.Comparator;
|
|
11
12
|
type shiftComparator<start extends Scanner.ComparatorStartChar, unscanned extends string> = unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned] : start extends Scanner.OneCharComparator ? [start, unscanned] : error<singleEqualsMessage>;
|
|
12
13
|
export declare const singleEqualsMessage = "= is not a valid comparator. Use == to check for equality";
|
|
13
14
|
type singleEqualsMessage = typeof singleEqualsMessage;
|
|
14
15
|
export declare const parseRightBound: (s: DynamicState, comparator: Scanner.Comparator) => void;
|
|
15
|
-
export type parseRightBound<s extends StaticState, comparator extends Scanner.Comparator, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedNumber<scanned, writeInvalidLimitMessage<comparator, scanned>> extends infer limit ? limit extends number ? s["branches"]["range"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], comparator, limit
|
|
16
|
+
export type parseRightBound<s extends StaticState, comparator extends Scanner.Comparator, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedNumber<scanned, writeInvalidLimitMessage<comparator, scanned>> extends infer limit ? limit extends number ? s["branches"]["range"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["range"]["limit"], s["branches"]["range"]["comparator"], comparator, `${limit}`, nextUnscanned> : error<writeUnpairableComparatorMessage<comparator>> : state.reduceSingleBound<s, comparator, `${limit}`, nextUnscanned> : error<limit & string> : never : never;
|
|
16
17
|
export declare const writeInvalidLimitMessage: <comparator extends ">" | "<" | ">=" | "<=" | "==", limit extends string>(comparator: comparator, limit: limit) => `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
17
18
|
export type writeInvalidLimitMessage<comparator extends Scanner.Comparator, limit extends string> = `Comparator ${comparator} must be followed by a number literal (was '${limit}')`;
|
|
18
19
|
export declare const writeEmptyRangeMessage: (range: Range) => string;
|
|
@@ -4,6 +4,10 @@ import type { DynamicState } from "../../reduce/dynamic.js";
|
|
|
4
4
|
import type { state, StaticState } from "../../reduce/static.js";
|
|
5
5
|
import type { Scanner } from "../scanner.js";
|
|
6
6
|
export declare const parseDivisor: (s: DynamicState) => void;
|
|
7
|
-
export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedInteger<scanned, writeInvalidDivisorMessage<scanned>> extends infer
|
|
7
|
+
export type parseDivisor<s extends StaticState, unscanned extends string> = Scanner.shiftUntilNextTerminator<unscanned> extends Scanner.shiftResult<infer scanned, infer nextUnscanned> ? tryParseWellFormedInteger<scanned, writeInvalidDivisorMessage<scanned>> extends infer divisor ? divisor extends number ? divisor extends 0 ? error<writeInvalidDivisorMessage<0>> : state.setRoot<s, [
|
|
8
|
+
s["root"],
|
|
9
|
+
"%",
|
|
10
|
+
`${divisor}`
|
|
11
|
+
], nextUnscanned> : error<divisor & string> : never : never;
|
|
8
12
|
export declare const writeInvalidDivisorMessage: <divisor extends string | number>(divisor: divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
9
13
|
export type writeInvalidDivisorMessage<divisor extends string | number> = `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { error } from "../../utils/generics.js";
|
|
2
|
+
import type { inferAst } from "../ast/ast.js";
|
|
2
3
|
import type { ParseContext } from "../definition.js";
|
|
3
|
-
import type { inferAst, validateAstSemantics } from "./ast.js";
|
|
4
4
|
import { DynamicState } from "./reduce/dynamic.js";
|
|
5
5
|
import type { state, StaticState } from "./reduce/static.js";
|
|
6
6
|
import { parseOperand } from "./shift/operand/operand.js";
|
|
@@ -10,7 +10,6 @@ import type { Scanner } from "./shift/scanner.js";
|
|
|
10
10
|
export declare const parseString: (def: string, ctx: ParseContext) => import("../../nodes/node.js").TypeNode;
|
|
11
11
|
export type parseString<def extends string, $> = maybeNaiveParse<def, $>;
|
|
12
12
|
export type inferString<def extends string, $> = inferAst<parseString<def, $>, $>;
|
|
13
|
-
export type validateString<def extends string, $> = parseString<def, $> extends infer ast ? ast extends error<infer message> ? message : validateAstSemantics<ast, $> extends error<infer message> ? message : def : never;
|
|
14
13
|
/**
|
|
15
14
|
* Try to parse the definition from right to left using the most common syntax.
|
|
16
15
|
* This can be much more efficient for simple definitions.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { distributable } from "../parse/ast/distributableFunction.js";
|
|
2
|
+
import type { Morph } from "../parse/ast/morph.js";
|
|
3
|
+
import type { Narrow } from "../parse/ast/narrow.js";
|
|
4
|
+
import type { FunctionalTupleOperator, inferTupleExpression, TupleExpression, TuplePostfixOperator, UnparsedTupleExpressionInput, UnparsedTupleOperator } from "../parse/ast/tuple.js";
|
|
5
|
+
import type { inferDefinition, validateDefinition } from "../parse/definition.js";
|
|
6
|
+
import type { conform } from "../utils/generics.js";
|
|
7
|
+
import type { PrecompiledDefaults } from "./standard.js";
|
|
8
|
+
import type { asIn, asOut, Type, TypeOptions } from "./type.js";
|
|
9
|
+
export type Expressions<$> = {
|
|
10
|
+
intersection: BinaryExpressionParser<$, "&">;
|
|
11
|
+
union: BinaryExpressionParser<$, "|">;
|
|
12
|
+
arrayOf: UnaryExpressionParser<$, "[]">;
|
|
13
|
+
keyOf: UnaryExpressionParser<$, "keyof">;
|
|
14
|
+
fromNode: UnvalidatedExpressionParser<$, "node">;
|
|
15
|
+
instanceOf: UnvalidatedExpressionParser<$, "instanceof">;
|
|
16
|
+
valueOf: UnvalidatedExpressionParser<$, "===">;
|
|
17
|
+
narrow: FunctionalExpressionParser<$, ":">;
|
|
18
|
+
morph: FunctionalExpressionParser<$, "=>">;
|
|
19
|
+
};
|
|
20
|
+
type Standard = Expressions<PrecompiledDefaults>;
|
|
21
|
+
export declare const intersection: Standard["intersection"];
|
|
22
|
+
export declare const union: Standard["union"];
|
|
23
|
+
export declare const arrayOf: Standard["arrayOf"];
|
|
24
|
+
export declare const keyOf: Standard["keyOf"];
|
|
25
|
+
export declare const instanceOf: Standard["instanceOf"];
|
|
26
|
+
export declare const valueOf: Standard["valueOf"];
|
|
27
|
+
export declare const narrow: Standard["narrow"];
|
|
28
|
+
export declare const morph: Standard["morph"];
|
|
29
|
+
export type BinaryExpressionParser<$, operator extends "&" | "|"> = {
|
|
30
|
+
<l, r>(l: validateDefinition<l, $>, r: validateDefinition<r, $>): parseTupleExpression<[l, operator, r], $>;
|
|
31
|
+
<l, r>(l: validateDefinition<l, $>, r: validateDefinition<r, $>, opts: TypeOptions): parseTupleExpression<[l, operator, r], $>;
|
|
32
|
+
};
|
|
33
|
+
export type UnaryExpressionParser<$, operator extends "keyof" | "[]"> = {
|
|
34
|
+
<def>(def: validateDefinition<def, $>): parseTupleExpression<unaryToTupleExpression<def, operator>, $>;
|
|
35
|
+
<def>(def: validateDefinition<def, $>, opts: TypeOptions): parseTupleExpression<unaryToTupleExpression<def, operator>, $>;
|
|
36
|
+
};
|
|
37
|
+
export type UnvalidatedExpressionParser<$, operator extends UnparsedTupleOperator> = {
|
|
38
|
+
<def>(def: conform<def, UnparsedTupleExpressionInput<$>[operator]>): parseTupleExpression<[operator, def], $>;
|
|
39
|
+
<def>(def: conform<def, UnparsedTupleExpressionInput<$>[operator]>, opts: TypeOptions): parseTupleExpression<[operator, def], $>;
|
|
40
|
+
};
|
|
41
|
+
export type FunctionalExpressionParser<$, operator extends FunctionalTupleOperator> = {
|
|
42
|
+
<inDef, fn extends FunctionWithInferredInput<$, operator, inDef>>(inDef: validateDefinition<inDef, $>, fn: fn): parseTupleExpression<[inDef, operator, fn], $>;
|
|
43
|
+
<inDef, fn extends FunctionWithInferredInput<$, operator, inDef>>(inDef: validateDefinition<inDef, $>, fn: fn, opts: TypeOptions): parseTupleExpression<[inDef, operator, fn], $>;
|
|
44
|
+
};
|
|
45
|
+
export type FunctionWithInferredInput<$, operator extends FunctionalTupleOperator, inDef> = operator extends ":" ? distributable<Narrow<asIn<inferDefinition<inDef, $>>>> : Morph<asOut<inferDefinition<inDef, $>>>;
|
|
46
|
+
type unaryToTupleExpression<def, operator extends "keyof" | "[]"> = operator extends TuplePostfixOperator ? [def, "[]"] : [operator, def];
|
|
47
|
+
type parseTupleExpression<expression extends TupleExpression, $> = inferTupleExpression<expression, $> extends infer result ? [result] extends [never] ? never : Type<result> : never;
|
|
48
|
+
export {};
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import type { ResolvedNode, TypeNode } from "../nodes/node.js";
|
|
2
2
|
import type { inferDefinition, ParseContext, validateDefinition } from "../parse/definition.js";
|
|
3
|
-
import type { Dict, error,
|
|
3
|
+
import type { Dict, error, evaluate, isAny, List, nominal } from "../utils/generics.js";
|
|
4
4
|
import type { stringifyUnion } from "../utils/unionToTuple.js";
|
|
5
5
|
import { FreezingCache } from "./cache.js";
|
|
6
|
+
import type { Expressions } from "./expressions.js";
|
|
6
7
|
import type { PrecompiledDefaults } from "./standard.js";
|
|
7
8
|
import type { Type, TypeConfig, TypeParser } from "./type.js";
|
|
8
9
|
type ScopeParser = {
|
|
9
10
|
<aliases>(aliases: validateAliases<aliases, {}>): Scope<parseScope<aliases, {}>>;
|
|
10
11
|
<aliases, opts extends ScopeOptions>(aliases: validateAliases<aliases, opts>, opts: validateOptions<opts>): Scope<parseScope<aliases, opts>>;
|
|
11
12
|
};
|
|
13
|
+
type validateAliases<aliases, opts extends ScopeOptions> = {
|
|
14
|
+
[name in keyof aliases]: name extends keyof preresolved<opts> ? writeDuplicateAliasesMessage<name & string> : validateDefinition<aliases[name], bootstrapScope<aliases, opts>>;
|
|
15
|
+
};
|
|
12
16
|
export type ScopeOptions = {
|
|
13
17
|
imports?: Space[] | [];
|
|
14
18
|
includes?: Space[] | [];
|
|
@@ -37,15 +41,12 @@ type mergeSpaces<scopes, base extends Dict = {}> = scopes extends readonly [
|
|
|
37
41
|
Space<infer head>,
|
|
38
42
|
...infer tail
|
|
39
43
|
] ? keyof base & keyof head extends never ? mergeSpaces<tail, base & head> : error<writeDuplicateAliasesMessage<stringifyUnion<keyof base & keyof head & string>>> : base;
|
|
40
|
-
type validateAliases<aliases, opts extends ScopeOptions> = {
|
|
41
|
-
[name in keyof aliases]: name extends keyof preresolved<opts> ? writeDuplicateAliasesMessage<name & string> : validateDefinition<aliases[name], bootstrapScope<aliases, opts>>;
|
|
42
|
-
};
|
|
43
44
|
type preresolved<opts extends ScopeOptions> = includesOf<opts> & importsOf<opts> & (opts["standard"] extends false ? {} : PrecompiledDefaults);
|
|
44
45
|
type alias<def = {}> = nominal<def, "alias">;
|
|
45
46
|
type bootstrapScope<aliases, opts extends ScopeOptions> = {
|
|
46
47
|
[k in keyof aliases]: alias<aliases[k]>;
|
|
47
48
|
} & preresolved<opts>;
|
|
48
|
-
type inferExports<aliases, opts extends ScopeOptions> =
|
|
49
|
+
type inferExports<aliases, opts extends ScopeOptions> = evaluate<{
|
|
49
50
|
[k in keyof aliases]: inferDefinition<aliases[k], bootstrapScope<aliases, opts>>;
|
|
50
51
|
} & includesOf<opts>>;
|
|
51
52
|
export type Space<exports = Dict> = {
|
|
@@ -61,16 +62,25 @@ export declare class Scope<context extends ScopeContext = any> {
|
|
|
61
62
|
config: TypeConfig | undefined;
|
|
62
63
|
parseCache: FreezingCache<TypeNode>;
|
|
63
64
|
constructor(aliases: Dict, opts: ScopeOptions);
|
|
64
|
-
type: TypeParser<resolutions<context>>;
|
|
65
65
|
createAnonymousTypeSuffix(): number;
|
|
66
66
|
get infer(): exportsOf<context>;
|
|
67
67
|
compile(): Space<exportsOf<context>>;
|
|
68
68
|
addReferenceIfResolvable(name: name<context>, ctx: ParseContext): boolean;
|
|
69
69
|
resolve(name: name<context>): Type;
|
|
70
70
|
resolveNode(node: TypeNode): ResolvedNode;
|
|
71
|
+
expressions: Expressions<resolutions<context>>;
|
|
72
|
+
intersection: import("./expressions.js").BinaryExpressionParser<resolutions<context>, "&">;
|
|
73
|
+
union: import("./expressions.js").BinaryExpressionParser<resolutions<context>, "|">;
|
|
74
|
+
arrayOf: import("./expressions.js").UnaryExpressionParser<resolutions<context>, "[]">;
|
|
75
|
+
keyOf: import("./expressions.js").UnaryExpressionParser<resolutions<context>, "keyof">;
|
|
76
|
+
valueOf: import("./expressions.js").UnvalidatedExpressionParser<resolutions<context>, "===">;
|
|
77
|
+
instanceOf: import("./expressions.js").UnvalidatedExpressionParser<resolutions<context>, "instanceof">;
|
|
78
|
+
narrow: import("./expressions.js").FunctionalExpressionParser<resolutions<context>, ":">;
|
|
79
|
+
morph: import("./expressions.js").FunctionalExpressionParser<resolutions<context>, "=>">;
|
|
80
|
+
type: TypeParser<resolutions<context>>;
|
|
71
81
|
}
|
|
72
|
-
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
73
82
|
export declare const scope: ScopeParser;
|
|
83
|
+
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
74
84
|
export declare const writeDuplicateAliasesMessage: <name_1 extends string>(name: name_1) => `Alias '${name_1}' is already defined`;
|
|
75
85
|
type writeDuplicateAliasesMessage<name extends string> = `Alias '${name}' is already defined`;
|
|
76
86
|
export {};
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import type { TraversalNode, TypeNode } from "../nodes/node.js";
|
|
2
|
+
import type { ParsedMorph } from "../parse/ast/morph.js";
|
|
2
3
|
import type { as, inferDefinition, validateDefinition } from "../parse/definition.js";
|
|
3
|
-
import type { ParsedMorph } from "../parse/tuple/morph.js";
|
|
4
4
|
import type { Problems, ProblemsConfig } from "../traverse/problems.js";
|
|
5
|
-
import type { defer,
|
|
5
|
+
import type { defer, evaluate, xor } from "../utils/generics.js";
|
|
6
6
|
import type { BuiltinClass } from "../utils/objectKinds.js";
|
|
7
|
+
import type { Expressions } from "./expressions.js";
|
|
7
8
|
import type { Scope } from "./scope.js";
|
|
8
9
|
export type TypeParser<$> = {
|
|
9
10
|
<def>(def: validateDefinition<def, $>): parseType<def, $>;
|
|
10
11
|
<def>(def: validateDefinition<def, $>, opts: TypeOptions): parseType<def, $>;
|
|
12
|
+
} & TypeParserProps<$>;
|
|
13
|
+
export type TypeParserProps<$> = {
|
|
14
|
+
from: Expressions<$>["fromNode"];
|
|
11
15
|
};
|
|
12
|
-
export type parseType<def, $> = def extends validateDefinition<def, $> ? Type<inferDefinition<def, $>> : never;
|
|
16
|
+
export type parseType<def, $> = [def] extends [validateDefinition<def, $>] ? Type<inferDefinition<def, $>> : never;
|
|
13
17
|
type TypeRoot<t = unknown> = {
|
|
14
18
|
[as]: t;
|
|
15
19
|
infer: asOut<t>;
|
|
@@ -17,7 +21,7 @@ type TypeRoot<t = unknown> = {
|
|
|
17
21
|
flat: TraversalNode;
|
|
18
22
|
meta: TypeMeta;
|
|
19
23
|
};
|
|
20
|
-
export type TypeOptions =
|
|
24
|
+
export type TypeOptions = evaluate<{
|
|
21
25
|
name?: string;
|
|
22
26
|
} & ProblemsConfig>;
|
|
23
27
|
export type TypeConfig = ProblemsConfig;
|
|
@@ -41,7 +45,7 @@ export type Type<t = unknown> = defer<Checker<t> & TypeRoot<t>>;
|
|
|
41
45
|
export type QualifiedTypeName = `${string}.${string}`;
|
|
42
46
|
export type asIn<t> = asIo<t, "in">;
|
|
43
47
|
export type asOut<t> = asIo<t, "out">;
|
|
44
|
-
type asIo<t, io extends "in" | "out"> = t extends ParsedMorph<infer i, infer o> ? io extends "in" ? i : o : t extends object ? t extends
|
|
48
|
+
type asIo<t, io extends "in" | "out"> = t extends ParsedMorph<infer i, infer o> ? io extends "in" ? i : o : t extends object ? t extends BuiltinClass | ((...args: any[]) => any) ? t : {
|
|
45
49
|
[k in keyof t]: asIo<t[k], io>;
|
|
46
50
|
} : t;
|
|
47
51
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FlatBound } from "../nodes/rules/range.js";
|
|
2
|
+
import { DataWrapper } from "../utils/data.js";
|
|
2
3
|
import type { Domain } from "../utils/domains.js";
|
|
3
|
-
import {
|
|
4
|
-
import type { constructor, evaluate, evaluateObject, instanceOf, RegexLiteral } from "../utils/generics.js";
|
|
4
|
+
import type { arraySubclassToReadonly, constructor, evaluate, instanceOf, RegexLiteral } from "../utils/generics.js";
|
|
5
5
|
import type { DefaultObjectKind } from "../utils/objectKinds.js";
|
|
6
6
|
import { Path } from "../utils/paths.js";
|
|
7
7
|
import type { ConstrainedRuleTraversalData, TraversalState } from "./traverse.js";
|
|
@@ -12,9 +12,9 @@ export declare class ArkTypeError extends TypeError {
|
|
|
12
12
|
export declare class Problem<code extends ProblemCode = any> {
|
|
13
13
|
code: code;
|
|
14
14
|
path: Path;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
private data;
|
|
16
|
+
private source;
|
|
17
|
+
private writers;
|
|
18
18
|
parts?: Problem[];
|
|
19
19
|
constructor(code: code, path: Path, data: ProblemData<code>, source: ProblemSource<code>, writers: ProblemWriters<code>);
|
|
20
20
|
toString(): string;
|
|
@@ -22,21 +22,21 @@ export declare class Problem<code extends ProblemCode = any> {
|
|
|
22
22
|
get reason(): string;
|
|
23
23
|
get mustBe(): string;
|
|
24
24
|
}
|
|
25
|
-
export type AddProblemOptions = {
|
|
25
|
+
export type AddProblemOptions<code extends ProblemCode = ProblemCode> = {
|
|
26
|
+
data?: ProblemData<code>;
|
|
26
27
|
path?: Path;
|
|
27
28
|
};
|
|
28
29
|
declare class ProblemArray extends Array<Problem> {
|
|
29
|
-
private
|
|
30
|
+
#private;
|
|
30
31
|
byPath: Record<string, Problem>;
|
|
31
32
|
constructor(state: TraversalState);
|
|
32
|
-
|
|
33
|
+
create<code extends ProblemCode>(code: code, source: ProblemSource<code>, opts?: AddProblemOptions<code>): false;
|
|
34
|
+
add(problem: Problem): false;
|
|
33
35
|
get summary(): string;
|
|
34
36
|
toString(): string;
|
|
35
37
|
throw(): never;
|
|
36
38
|
}
|
|
37
|
-
export declare const Problems: new (state: TraversalState) =>
|
|
38
|
-
[k in Exclude<keyof ProblemArray, keyof unknown[]>]: ProblemArray[k];
|
|
39
|
-
};
|
|
39
|
+
export declare const Problems: new (state: TraversalState) => arraySubclassToReadonly<ProblemArray>;
|
|
40
40
|
export type Problems = instanceOf<typeof Problems>;
|
|
41
41
|
export declare const describeDomains: (domains: Domain[]) => string;
|
|
42
42
|
export declare const describeBranches: (descriptions: string[]) => string;
|
|
@@ -62,22 +62,13 @@ export type ProblemData<code extends ProblemCode = ProblemCode> = ProblemDataByC
|
|
|
62
62
|
export type MustBeWriter<code extends ProblemCode> = (source: ProblemSources[code]) => string;
|
|
63
63
|
export type ReasonWriter<code extends ProblemCode = ProblemCode> = (mustBe: string, data: DataWrapper<code extends keyof ConstrainedRuleTraversalData ? ConstrainedRuleTraversalData[code] : unknown>) => string;
|
|
64
64
|
export type ContextWriter = (reason: string, path: Path) => string;
|
|
65
|
-
export declare class DataWrapper<value = unknown> {
|
|
66
|
-
value: value;
|
|
67
|
-
constructor(value: value);
|
|
68
|
-
toString(): string;
|
|
69
|
-
get domain(): domainOf<value>;
|
|
70
|
-
get size(): number;
|
|
71
|
-
get units(): "" | "characters" | "items";
|
|
72
|
-
get className(): any;
|
|
73
|
-
}
|
|
74
65
|
export declare const defaultProblemWriters: DefaultProblemsWriters;
|
|
75
66
|
export type ProblemOptions<code extends ProblemCode = ProblemCode> = {
|
|
76
67
|
mustBe?: MustBeWriter<code>;
|
|
77
68
|
writeReason?: ReasonWriter<code>;
|
|
78
69
|
addContext?: ContextWriter;
|
|
79
70
|
};
|
|
80
|
-
export type ProblemsConfig =
|
|
71
|
+
export type ProblemsConfig = evaluate<{
|
|
81
72
|
defaults?: ProblemOptions<ProblemCode>;
|
|
82
73
|
} & {
|
|
83
74
|
[code in ProblemCode]?: ProblemOptions<code>;
|