arktype 1.1.0-dev-1 → 2.0.0-beta.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/LICENSE +7 -0
- package/out/ark.d.ts +24 -0
- package/out/ark.js +8 -0
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +54 -0
- package/out/generic.js +39 -0
- package/out/index.d.ts +6 -0
- package/out/index.js +5 -0
- package/out/match.d.ts +71 -0
- package/out/match.js +50 -0
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/out/parser/definition.d.ts +32 -0
- package/out/parser/definition.js +29 -0
- package/out/parser/objectLiteral.d.ts +79 -0
- package/out/parser/objectLiteral.js +100 -0
- package/out/parser/semantic/bounds.d.ts +15 -0
- package/out/parser/semantic/bounds.js +1 -0
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/divisor.d.ts +7 -0
- package/out/parser/semantic/divisor.js +1 -0
- package/out/parser/semantic/infer.d.ts +25 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/utils.d.ts +4 -0
- package/out/parser/semantic/utils.js +1 -0
- package/out/parser/semantic/validate.d.ts +30 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/string/reduce/dynamic.d.ts +43 -0
- package/out/parser/string/reduce/dynamic.js +139 -0
- package/out/parser/string/reduce/shared.d.ts +42 -0
- package/out/parser/string/reduce/shared.js +27 -0
- package/out/parser/string/reduce/static.d.ts +179 -0
- package/out/parser/string/reduce/static.js +1 -0
- package/out/parser/string/shift/operand/date.d.ts +9 -0
- package/out/parser/string/shift/operand/date.js +23 -0
- package/out/parser/string/shift/operand/enclosed.d.ts +38 -0
- package/out/parser/string/shift/operand/enclosed.js +50 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
- package/out/parser/string/shift/operand/genericArgs.js +16 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.js +12 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
- package/out/parser/string/shift/operand/unenclosed.js +53 -0
- package/out/parser/string/shift/operator/bounds.d.ts +27 -0
- package/out/parser/string/shift/operator/bounds.js +94 -0
- package/out/parser/string/shift/operator/default.d.ts +24 -0
- package/out/parser/string/shift/operator/default.js +23 -0
- package/out/parser/string/shift/operator/divisor.d.ts +7 -0
- package/out/parser/string/shift/operator/divisor.js +11 -0
- package/out/parser/string/shift/operator/operator.d.ts +13 -0
- package/out/parser/string/shift/operator/operator.js +22 -0
- package/out/parser/string/shift/scanner.d.ts +67 -0
- package/out/parser/string/shift/scanner.js +102 -0
- package/out/parser/string/string.d.ts +28 -0
- package/out/parser/string/string.js +24 -0
- package/out/parser/tuple.d.ts +130 -0
- package/out/parser/tuple.js +157 -0
- package/out/scope.d.ts +84 -0
- package/out/scope.js +131 -0
- package/out/type.d.ts +79 -0
- package/out/type.js +37 -0
- package/package.json +37 -36
- package/dist/main.d.ts +0 -1018
- package/dist/main.js +0 -1450
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { type BaseMeta, type BaseRoot, type Morph, type Out, type Predicate, type distillConstrainableIn, type distillOut, type inferIntersection, type inferMorphOut, type inferPredicate } from "@ark/schema";
|
|
2
|
+
import { type BuiltinObjectKind, type Constructor, type Domain, type ErrorMessage, type array, type conform, type show } from "@ark/util";
|
|
3
|
+
import type { ParseContext } from "../scope.js";
|
|
4
|
+
import type { inferDefinition, validateDefinition } from "./definition.js";
|
|
5
|
+
import type { InfixOperator, PostfixExpression } from "./semantic/infer.js";
|
|
6
|
+
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
|
|
7
|
+
import type { BaseCompletions } from "./string/string.js";
|
|
8
|
+
export declare const parseTuple: (def: array, ctx: ParseContext) => BaseRoot;
|
|
9
|
+
export declare const parseTupleLiteral: (def: array, ctx: ParseContext) => BaseRoot;
|
|
10
|
+
type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]];
|
|
11
|
+
export type validateTuple<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends PostfixExpression ? validatePostfixExpression<def, $, args> : def extends InfixExpression ? validateInfixExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
|
|
12
|
+
def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
|
|
13
|
+
def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..." | "?"> : def[1]
|
|
14
|
+
] : validateTupleLiteral<def, $, args>;
|
|
15
|
+
export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
|
|
16
|
+
type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
|
|
17
|
+
type SequenceParseState = {
|
|
18
|
+
unscanned: array;
|
|
19
|
+
inferred: array;
|
|
20
|
+
validated: array;
|
|
21
|
+
};
|
|
22
|
+
type parseSequence<def extends array, $, args> = parseNextElement<{
|
|
23
|
+
unscanned: def;
|
|
24
|
+
inferred: [];
|
|
25
|
+
validated: [];
|
|
26
|
+
}, $, args>;
|
|
27
|
+
type PreparsedElement = {
|
|
28
|
+
head: unknown;
|
|
29
|
+
tail: array;
|
|
30
|
+
optional: boolean;
|
|
31
|
+
spread: boolean;
|
|
32
|
+
};
|
|
33
|
+
declare namespace PreparsedElement {
|
|
34
|
+
type from<result extends PreparsedElement> = result;
|
|
35
|
+
}
|
|
36
|
+
type preparseNextElement<s extends SequenceParseState> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? tail extends readonly ["?", ...infer postOptionalTail] ? PreparsedElement.from<{
|
|
37
|
+
head: head;
|
|
38
|
+
tail: postOptionalTail;
|
|
39
|
+
optional: true;
|
|
40
|
+
spread: true;
|
|
41
|
+
}> : PreparsedElement.from<{
|
|
42
|
+
head: head;
|
|
43
|
+
tail: tail;
|
|
44
|
+
optional: false;
|
|
45
|
+
spread: true;
|
|
46
|
+
}> : s["unscanned"] extends readonly [infer head, "?", ...infer tail] ? PreparsedElement.from<{
|
|
47
|
+
head: head;
|
|
48
|
+
tail: tail;
|
|
49
|
+
optional: true;
|
|
50
|
+
spread: false;
|
|
51
|
+
}> : s["unscanned"] extends readonly [infer head, ...infer tail] ? PreparsedElement.from<{
|
|
52
|
+
head: head;
|
|
53
|
+
tail: tail;
|
|
54
|
+
optional: false;
|
|
55
|
+
spread: false;
|
|
56
|
+
}> : null;
|
|
57
|
+
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
58
|
+
unscanned: next["tail"];
|
|
59
|
+
inferred: next["spread"] extends true ? [
|
|
60
|
+
...s["inferred"],
|
|
61
|
+
...conform<inferDefinition<next["head"], $, args>, array>
|
|
62
|
+
] : next["optional"] extends true ? [
|
|
63
|
+
...s["inferred"],
|
|
64
|
+
inferDefinition<next["head"], $, args>?
|
|
65
|
+
] : [...s["inferred"], inferDefinition<next["head"], $, args>];
|
|
66
|
+
validated: [
|
|
67
|
+
...s["validated"],
|
|
68
|
+
...(next["spread"] extends true ? [
|
|
69
|
+
inferDefinition<next["head"], $, args> extends (infer spreadOperand extends array) ? [
|
|
70
|
+
number,
|
|
71
|
+
number
|
|
72
|
+
] extends ([
|
|
73
|
+
s["inferred"]["length"],
|
|
74
|
+
spreadOperand["length"]
|
|
75
|
+
]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
|
|
76
|
+
] : []),
|
|
77
|
+
[
|
|
78
|
+
next["optional"] | next["spread"],
|
|
79
|
+
"?"
|
|
80
|
+
] extends ([
|
|
81
|
+
false,
|
|
82
|
+
s["validated"][number]
|
|
83
|
+
]) ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>,
|
|
84
|
+
...(next["optional"] extends true ? [
|
|
85
|
+
next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : "?"
|
|
86
|
+
] : [])
|
|
87
|
+
];
|
|
88
|
+
}, $, args> : s;
|
|
89
|
+
export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
|
|
90
|
+
type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
|
|
91
|
+
export declare const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
92
|
+
type multipleVariadicMessage = typeof multipleVariadicMesage;
|
|
93
|
+
export declare const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
94
|
+
type requiredPostOptionalMessage = typeof requiredPostOptionalMessage;
|
|
95
|
+
export declare const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
96
|
+
type optionalPostVariadicMessage = typeof optionalPostVariadicMessage;
|
|
97
|
+
export declare const spreadOptionalMessage = "A spread element cannot be optional";
|
|
98
|
+
type spreadOptionalMessage = typeof optionalPostVariadicMessage;
|
|
99
|
+
export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
|
|
100
|
+
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
|
|
101
|
+
export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
|
|
102
|
+
export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>;
|
|
103
|
+
export type validateInfixExpression<def extends InfixExpression, $, args> = def["length"] extends 2 ? readonly [def[0], writeMissingRightOperandMessage<def[1]>] : readonly [
|
|
104
|
+
validateDefinition<def[0], $, args>,
|
|
105
|
+
def[1],
|
|
106
|
+
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<distillOut<inferDefinition<def[0], $, args>>> : def[1] extends "=>" ? Morph<distillOut<inferDefinition<def[0], $, args>>, unknown> : def[1] extends "@" ? BaseMeta | string : validateDefinition<def[2], $, args>
|
|
107
|
+
];
|
|
108
|
+
export type UnparsedTupleExpressionInput = {
|
|
109
|
+
instanceof: Constructor;
|
|
110
|
+
"===": unknown;
|
|
111
|
+
};
|
|
112
|
+
export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
|
|
113
|
+
export declare const parseKeyOfTuple: PrefixParser<"keyof">;
|
|
114
|
+
export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
|
|
115
|
+
export type PostfixParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: ParseContext) => BaseRoot;
|
|
116
|
+
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: ParseContext) => BaseRoot;
|
|
117
|
+
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
118
|
+
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
119
|
+
export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
120
|
+
export type TuplePostfixOperator = "[]";
|
|
121
|
+
export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@";
|
|
122
|
+
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
123
|
+
export declare const parseMorphTuple: PostfixParser<"=>">;
|
|
124
|
+
export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
|
|
125
|
+
export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distillConstrainableIn<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
|
|
126
|
+
export declare const parseNarrowTuple: PostfixParser<":">;
|
|
127
|
+
export type IndexZeroOperator = "keyof" | "instanceof" | "===";
|
|
128
|
+
export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
|
|
129
|
+
export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
|
|
130
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { jsObjects, makeRootAndArrayPropertiesMutable, tsKeywords } from "@ark/schema";
|
|
2
|
+
import { append, objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
|
+
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
|
|
4
|
+
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
5
|
+
export const parseTupleLiteral = (def, ctx) => {
|
|
6
|
+
let sequences = [{}];
|
|
7
|
+
let i = 0;
|
|
8
|
+
while (i < def.length) {
|
|
9
|
+
let spread = false;
|
|
10
|
+
let optional = false;
|
|
11
|
+
if (def[i] === "..." && i < def.length - 1) {
|
|
12
|
+
spread = true;
|
|
13
|
+
i++;
|
|
14
|
+
}
|
|
15
|
+
const element = ctx.$.parse(def[i], ctx);
|
|
16
|
+
i++;
|
|
17
|
+
if (def[i] === "?") {
|
|
18
|
+
if (spread)
|
|
19
|
+
return throwParseError(spreadOptionalMessage);
|
|
20
|
+
optional = true;
|
|
21
|
+
i++;
|
|
22
|
+
}
|
|
23
|
+
if (spread) {
|
|
24
|
+
if (!element.extends(jsObjects.Array))
|
|
25
|
+
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
26
|
+
// a spread must be distributed over branches e.g.:
|
|
27
|
+
// def: [string, ...(number[] | [true, false])]
|
|
28
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
29
|
+
sequences = sequences.flatMap(base =>
|
|
30
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
31
|
+
element.branches.map(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return ctx.$.internal.schema(sequences.map(sequence => ({
|
|
38
|
+
proto: Array,
|
|
39
|
+
sequence
|
|
40
|
+
})));
|
|
41
|
+
};
|
|
42
|
+
const appendElement = (base, kind, element) => {
|
|
43
|
+
switch (kind) {
|
|
44
|
+
case "required":
|
|
45
|
+
if (base.optionals)
|
|
46
|
+
// e.g. [string?, number]
|
|
47
|
+
return throwParseError(requiredPostOptionalMessage);
|
|
48
|
+
if (base.variadic) {
|
|
49
|
+
// e.g. [...string[], number]
|
|
50
|
+
base.postfix = append(base.postfix, element);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
// e.g. [string, number]
|
|
54
|
+
base.prefix = append(base.prefix, element);
|
|
55
|
+
}
|
|
56
|
+
return base;
|
|
57
|
+
case "optional":
|
|
58
|
+
if (base.variadic)
|
|
59
|
+
// e.g. [...string[], number?]
|
|
60
|
+
return throwParseError(optionalPostVariadicMessage);
|
|
61
|
+
// e.g. [string, number?]
|
|
62
|
+
base.optionals = append(base.optionals, element);
|
|
63
|
+
return base;
|
|
64
|
+
case "variadic":
|
|
65
|
+
// e.g. [...string[], number, ...string[]]
|
|
66
|
+
if (base.postfix)
|
|
67
|
+
throwParseError(multipleVariadicMesage);
|
|
68
|
+
if (base.variadic) {
|
|
69
|
+
if (!base.variadic.equals(element)) {
|
|
70
|
+
// e.g. [...string[], ...number[]]
|
|
71
|
+
throwParseError(multipleVariadicMesage);
|
|
72
|
+
}
|
|
73
|
+
// e.g. [...string[], ...string[]]
|
|
74
|
+
// do nothing, second spread doesn't change the type
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// e.g. [string, ...number[]]
|
|
78
|
+
base.variadic = element.internal;
|
|
79
|
+
}
|
|
80
|
+
return base;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const appendSpreadBranch = (base, branch) => {
|
|
84
|
+
const spread = branch.firstReferenceOfKind("sequence");
|
|
85
|
+
if (!spread) {
|
|
86
|
+
// the only array with no sequence reference is unknown[]
|
|
87
|
+
return appendElement(base, "variadic", tsKeywords.unknown.internal);
|
|
88
|
+
}
|
|
89
|
+
spread.prefix.forEach(node => appendElement(base, "required", node));
|
|
90
|
+
spread.optionals.forEach(node => appendElement(base, "optional", node));
|
|
91
|
+
spread.variadic && appendElement(base, "variadic", spread.variadic);
|
|
92
|
+
spread.postfix.forEach(node => appendElement(base, "required", node));
|
|
93
|
+
return base;
|
|
94
|
+
};
|
|
95
|
+
const maybeParseTupleExpression = (def, ctx) => {
|
|
96
|
+
const tupleExpressionResult = isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
|
|
97
|
+
: isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
|
|
98
|
+
: undefined;
|
|
99
|
+
return tupleExpressionResult;
|
|
100
|
+
};
|
|
101
|
+
export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
102
|
+
export const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
103
|
+
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
104
|
+
export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
105
|
+
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
106
|
+
export const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
|
|
107
|
+
const parseBranchTuple = (def, ctx) => {
|
|
108
|
+
if (def[2] === undefined)
|
|
109
|
+
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
110
|
+
const l = ctx.$.parse(def[0], ctx);
|
|
111
|
+
const r = ctx.$.parse(def[2], ctx);
|
|
112
|
+
return def[1] === "&" ? l.and(r) : l.or(r);
|
|
113
|
+
};
|
|
114
|
+
const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
|
|
115
|
+
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
116
|
+
export const parseMorphTuple = (def, ctx) => {
|
|
117
|
+
if (typeof def[2] !== "function") {
|
|
118
|
+
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
119
|
+
}
|
|
120
|
+
// TODO: nested morphs?
|
|
121
|
+
return ctx.$.parse(def[0], ctx).pipe(def[2]);
|
|
122
|
+
};
|
|
123
|
+
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
124
|
+
export const parseNarrowTuple = (def, ctx) => {
|
|
125
|
+
if (typeof def[2] !== "function") {
|
|
126
|
+
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
127
|
+
}
|
|
128
|
+
return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
|
|
129
|
+
};
|
|
130
|
+
const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
|
|
131
|
+
const indexOneParsers = {
|
|
132
|
+
"|": parseBranchTuple,
|
|
133
|
+
"&": parseBranchTuple,
|
|
134
|
+
"[]": parseArrayTuple,
|
|
135
|
+
":": parseNarrowTuple,
|
|
136
|
+
"=>": parseMorphTuple,
|
|
137
|
+
"@": parseAttributeTuple
|
|
138
|
+
};
|
|
139
|
+
const prefixParsers = {
|
|
140
|
+
keyof: parseKeyOfTuple,
|
|
141
|
+
instanceof: (def, ctx) => {
|
|
142
|
+
if (typeof def[1] !== "function") {
|
|
143
|
+
return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
|
|
144
|
+
}
|
|
145
|
+
const branches = def
|
|
146
|
+
.slice(1)
|
|
147
|
+
.map(ctor => typeof ctor === "function" ?
|
|
148
|
+
ctx.$.node("proto", { proto: ctor })
|
|
149
|
+
: throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
|
|
150
|
+
return branches.length === 1 ?
|
|
151
|
+
branches[0]
|
|
152
|
+
: ctx.$.node("union", { branches });
|
|
153
|
+
},
|
|
154
|
+
"===": (def, ctx) => ctx.$.units(def.slice(1))
|
|
155
|
+
};
|
|
156
|
+
const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
|
|
157
|
+
export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
|
package/out/scope.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { RawRootScope, type AliasDefEntry, type ArkConfig, type BaseRoot, type GenericArgResolutions, type GenericParamAst, type GenericParamDef, type GenericProps, type PreparsedNodeResolution, type PrivateDeclaration, type RawRootResolutions, type RootScope, type arkKind, type destructuredExportContext, type destructuredImportContext, type exportedNameOf, type writeDuplicateAliasError } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type array, type keyError, type nominal, type show } from "@ark/util";
|
|
3
|
+
import { parseGenericParams, type Generic, type GenericDeclaration, type ParameterString, type baseGenericArgs, type parseValidGenericParams } from "./generic.js";
|
|
4
|
+
import { type MatchParser } from "./match.js";
|
|
5
|
+
import type { Module } from "./module.js";
|
|
6
|
+
import { type inferDefinition, type validateDefinition } from "./parser/definition.js";
|
|
7
|
+
import type { ParsedDefault } from "./parser/string/shift/operator/default.js";
|
|
8
|
+
import { RawTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.js";
|
|
9
|
+
export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkConfig) => Scope<inferScope<def>>;
|
|
10
|
+
export type validateScope<def> = {
|
|
11
|
+
[k in keyof def]: k extends symbol ? unknown : parseScopeKey<k, def>["params"] extends infer params ? params extends array<GenericParamAst> ? params["length"] extends 0 ? def[k] extends Type | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? keyError<writeDuplicateAliasError<name>> : validateDefinition<def[k], bootstrapAliases<def>, {}> : validateDefinition<def[k], bootstrapAliases<def>, baseGenericArgs<params>> : params : never;
|
|
12
|
+
};
|
|
13
|
+
export type inferScope<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
14
|
+
export type bindThis<def> = {
|
|
15
|
+
this: Def<def>;
|
|
16
|
+
};
|
|
17
|
+
/** nominal type for an unparsed definition used during scope bootstrapping */
|
|
18
|
+
type Def<def = {}> = nominal<def, "unparsed">;
|
|
19
|
+
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
20
|
+
export type UnparsedScope = "$";
|
|
21
|
+
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
22
|
+
type PreparsedResolution = PreparsedNodeResolution;
|
|
23
|
+
type bootstrapAliases<def> = {
|
|
24
|
+
[k in Exclude<keyof def, GenericDeclaration | symbol>]: def[k] extends PreparsedResolution ? def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn : Def<def[k]>;
|
|
25
|
+
} & {
|
|
26
|
+
[k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<parseValidGenericParams<extractGenericParameters<k>, bootstrapAliases<def>>, def[k], UnparsedScope>;
|
|
27
|
+
};
|
|
28
|
+
type inferBootstrapped<$> = show<{
|
|
29
|
+
[name in keyof $]: $[name] extends Def<infer def> ? inferDefinition<def, $, {}> : $[name] extends (Generic<infer params, infer def> | GenericProps<infer params, infer def>) ? Generic<params, def, $> : $[name];
|
|
30
|
+
}>;
|
|
31
|
+
type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
|
|
32
|
+
type extractGenericParameters<k> = k extends `${string}<${infer params}>` ? ParameterString<params> : never;
|
|
33
|
+
export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [
|
|
34
|
+
resolution
|
|
35
|
+
] extends [anyOrNever] ? resolution : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution : never;
|
|
36
|
+
export type moduleKeyOf<$> = {
|
|
37
|
+
[k in keyof $]: $[k] extends {
|
|
38
|
+
[arkKind]: "module";
|
|
39
|
+
} ? k & string : never;
|
|
40
|
+
}[keyof $];
|
|
41
|
+
export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? $[submodule][subalias] : never : token extends `${infer submodule}.${infer subalias}` ? submodule extends moduleKeyOf<ArkEnv.$> ? subalias extends keyof ArkEnv.$[submodule] ? ArkEnv.$[submodule][subalias] : never : never : never;
|
|
42
|
+
export interface ParseContext extends TypeParseOptions {
|
|
43
|
+
$: RawScope;
|
|
44
|
+
}
|
|
45
|
+
export interface TypeParseOptions {
|
|
46
|
+
args?: GenericArgResolutions;
|
|
47
|
+
}
|
|
48
|
+
export declare const scope: ScopeParser;
|
|
49
|
+
export interface Scope<$ = any> extends RootScope<$> {
|
|
50
|
+
type: TypeParser<$>;
|
|
51
|
+
match: MatchParser<$>;
|
|
52
|
+
declare: DeclarationParser<$>;
|
|
53
|
+
define: DefinitionParser<$>;
|
|
54
|
+
import<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredImportContext<$, names>>>;
|
|
55
|
+
export<names extends exportedNameOf<$>[]>(...names: names): Module<show<destructuredExportContext<$, names>>>;
|
|
56
|
+
}
|
|
57
|
+
export declare class RawScope<$ extends RawRootResolutions = RawRootResolutions> extends RawRootScope<$> {
|
|
58
|
+
private parseCache;
|
|
59
|
+
constructor(def: Record<string, unknown>, config?: ArkConfig);
|
|
60
|
+
type: RawTypeParser;
|
|
61
|
+
match: MatchParser<$>;
|
|
62
|
+
declare: () => {
|
|
63
|
+
type: RawTypeParser;
|
|
64
|
+
};
|
|
65
|
+
define: (def: unknown) => unknown;
|
|
66
|
+
preparseAlias(k: string, v: unknown): AliasDefEntry;
|
|
67
|
+
preparseRoot(def: unknown): unknown;
|
|
68
|
+
parseRoot(def: unknown, opts?: TypeParseOptions): BaseRoot;
|
|
69
|
+
parse<defaultable extends boolean = false>(def: unknown, ctx: ParseContext, defaultable?: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
|
|
70
|
+
parseString<defaultable extends boolean>(def: string, ctx: ParseContext, defaultable: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
|
|
71
|
+
}
|
|
72
|
+
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
73
|
+
export type ParsedScopeKey = {
|
|
74
|
+
name: string;
|
|
75
|
+
params: array<GenericParamDef>;
|
|
76
|
+
};
|
|
77
|
+
export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? {
|
|
78
|
+
name: name;
|
|
79
|
+
params: parseGenericParams<params, bootstrapAliases<def>>;
|
|
80
|
+
} : {
|
|
81
|
+
name: k;
|
|
82
|
+
params: [];
|
|
83
|
+
};
|
|
84
|
+
export {};
|
package/out/scope.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
+
var useValue = arguments.length > 2;
|
|
3
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
+
}
|
|
6
|
+
return useValue ? value : void 0;
|
|
7
|
+
};
|
|
8
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
+
var _, done = false;
|
|
14
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
+
var context = {};
|
|
16
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
+
if (kind === "accessor") {
|
|
21
|
+
if (result === void 0) continue;
|
|
22
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
+
}
|
|
27
|
+
else if (_ = accept(result)) {
|
|
28
|
+
if (kind === "field") initializers.unshift(_);
|
|
29
|
+
else descriptor[key] = _;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
+
done = true;
|
|
34
|
+
};
|
|
35
|
+
import { RawRootScope, hasArkKind, parseGeneric } from "@ark/schema";
|
|
36
|
+
import { bound, domainOf, hasDomain, isThunk, throwParseError } from "@ark/util";
|
|
37
|
+
import { parseGenericParams } from "./generic.js";
|
|
38
|
+
import { createMatchParser } from "./match.js";
|
|
39
|
+
import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
|
|
40
|
+
import { DynamicState } from "./parser/string/reduce/dynamic.js";
|
|
41
|
+
import { writeUnexpectedCharacterMessage } from "./parser/string/shift/operator/operator.js";
|
|
42
|
+
import { Scanner } from "./parser/string/shift/scanner.js";
|
|
43
|
+
import { fullStringParse } from "./parser/string/string.js";
|
|
44
|
+
import { RawTypeParser } from "./type.js";
|
|
45
|
+
export const scope = ((def, config = {}) => new RawScope(def, config));
|
|
46
|
+
let RawScope = (() => {
|
|
47
|
+
let _classSuper = RawRootScope;
|
|
48
|
+
let _instanceExtraInitializers = [];
|
|
49
|
+
let _parseRoot_decorators;
|
|
50
|
+
return class RawScope extends _classSuper {
|
|
51
|
+
static {
|
|
52
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
53
|
+
_parseRoot_decorators = [bound];
|
|
54
|
+
__esDecorate(this, null, _parseRoot_decorators, { kind: "method", name: "parseRoot", static: false, private: false, access: { has: obj => "parseRoot" in obj, get: obj => obj.parseRoot }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
55
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
56
|
+
}
|
|
57
|
+
parseCache = (__runInitializers(this, _instanceExtraInitializers), {});
|
|
58
|
+
constructor(def, config) {
|
|
59
|
+
super(def, config);
|
|
60
|
+
}
|
|
61
|
+
type = new RawTypeParser(this);
|
|
62
|
+
match = createMatchParser(this);
|
|
63
|
+
declare = (() => ({
|
|
64
|
+
type: this.type
|
|
65
|
+
})).bind(this);
|
|
66
|
+
define = ((def) => def).bind(this);
|
|
67
|
+
preparseAlias(k, v) {
|
|
68
|
+
const firstParamIndex = k.indexOf("<");
|
|
69
|
+
if (firstParamIndex === -1)
|
|
70
|
+
return [k, v];
|
|
71
|
+
if (k.at(-1) !== ">") {
|
|
72
|
+
throwParseError(`'>' must be the last character of a generic declaration in a scope`);
|
|
73
|
+
}
|
|
74
|
+
const name = k.slice(0, firstParamIndex);
|
|
75
|
+
const paramString = k.slice(firstParamIndex + 1, -1);
|
|
76
|
+
return [
|
|
77
|
+
name,
|
|
78
|
+
// use a thunk definition for the generic so that we can parse
|
|
79
|
+
// constraints within the current scope
|
|
80
|
+
() => {
|
|
81
|
+
const params = parseGenericParams(paramString, {
|
|
82
|
+
$: this,
|
|
83
|
+
args: {}
|
|
84
|
+
});
|
|
85
|
+
const generic = parseGeneric(params, v, this);
|
|
86
|
+
return generic;
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
preparseRoot(def) {
|
|
91
|
+
if (isThunk(def) && !hasArkKind(def, "generic"))
|
|
92
|
+
return def();
|
|
93
|
+
return def;
|
|
94
|
+
}
|
|
95
|
+
parseRoot(def, opts = {}) {
|
|
96
|
+
const node = this.parse(def, Object.assign(this.finalizeRootArgs(opts, () => node), { $: this })).bindScope(this);
|
|
97
|
+
return node;
|
|
98
|
+
}
|
|
99
|
+
parse(def, ctx, defaultable = false) {
|
|
100
|
+
if (typeof def === "string") {
|
|
101
|
+
if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
|
|
102
|
+
// we can only rely on the cache if there are no contextual
|
|
103
|
+
// resolutions like "this" or generic args
|
|
104
|
+
return this.parseString(def, ctx, defaultable);
|
|
105
|
+
}
|
|
106
|
+
const contextKey = `${def}${defaultable}`;
|
|
107
|
+
return (this.parseCache[contextKey] ??= this.parseString(def, ctx, defaultable));
|
|
108
|
+
}
|
|
109
|
+
return hasDomain(def, "object") ?
|
|
110
|
+
parseObject(def, ctx)
|
|
111
|
+
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
|
|
112
|
+
}
|
|
113
|
+
parseString(def, ctx, defaultable) {
|
|
114
|
+
const aliasResolution = this.maybeResolveRoot(def);
|
|
115
|
+
if (aliasResolution)
|
|
116
|
+
return aliasResolution;
|
|
117
|
+
const aliasArrayResolution = def.endsWith("[]") ?
|
|
118
|
+
this.maybeResolveRoot(def.slice(0, -2))?.array()
|
|
119
|
+
: undefined;
|
|
120
|
+
if (aliasArrayResolution)
|
|
121
|
+
return aliasArrayResolution;
|
|
122
|
+
const s = new DynamicState(new Scanner(def), ctx, defaultable);
|
|
123
|
+
const node = fullStringParse(s);
|
|
124
|
+
if (s.finalizer === ">")
|
|
125
|
+
throwParseError(writeUnexpectedCharacterMessage(">"));
|
|
126
|
+
return node;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
})();
|
|
130
|
+
export { RawScope };
|
|
131
|
+
export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
|
package/out/type.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ArkErrors, BaseRoot, type BaseMeta, type ConstraintKind, type Disjoint, type DivisorSchema, type ExactLengthSchema, type ExclusiveDateRangeSchema, type ExclusiveNumericRangeSchema, type InclusiveDateRangeSchema, type InclusiveNumericRangeSchema, type InnerRoot, type Morph, type MorphAst, type NodeSchema, type Out, type PatternSchema, type Predicate, type Prerequisite, type PrimitiveConstraintKind, type Root, type constrain, type constraintKindOf, type distillIn, type distillOut, type exclusivizeRangeSchema, type indexInto, type indexOf, type inferIntersection, type inferMorphOut, type inferPipes, type inferPredicate, type writeInvalidOperandMessage } from "@ark/schema";
|
|
2
|
+
import { Callable, type Constructor, type ErrorMessage, type array, type conform } from "@ark/util";
|
|
3
|
+
import type { type } from "./ark.js";
|
|
4
|
+
import { type Generic, type ParameterString, type baseGenericArgs, type parseValidGenericParams, type validateParameterString } from "./generic.js";
|
|
5
|
+
import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.js";
|
|
6
|
+
import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.js";
|
|
7
|
+
import type { RawScope, Scope, bindThis } from "./scope.js";
|
|
8
|
+
/** The convenience properties attached to `type` */
|
|
9
|
+
export type TypeParserAttachments = Omit<TypeParser, never>;
|
|
10
|
+
export interface TypeParser<$ = {}> {
|
|
11
|
+
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>;
|
|
12
|
+
<const zero, const one, const rest extends array>(_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>, _1: zero extends "keyof" ? validateTypeRoot<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distillIn<inferTypeRoot<zero, $>>>] : one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>] : one extends "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
|
|
13
|
+
<params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericArgs<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
14
|
+
raw(def: unknown): Type<any, $>;
|
|
15
|
+
errors: typeof ArkErrors;
|
|
16
|
+
}
|
|
17
|
+
export declare class RawTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
|
|
18
|
+
constructor($: RawScope);
|
|
19
|
+
}
|
|
20
|
+
export type DeclarationParser<$> = <preinferred>() => {
|
|
21
|
+
type: <def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
|
|
22
|
+
};
|
|
23
|
+
type validateChainedConstraint<kind extends ConstraintKind, t extends {
|
|
24
|
+
inferIn: unknown;
|
|
25
|
+
}> = t["inferIn"] extends Prerequisite<kind> ? t : ErrorMessage<writeInvalidOperandMessage<kind, Root<t["inferIn"]>>>;
|
|
26
|
+
declare class _Type<t = unknown, $ = any> extends InnerRoot<t, $> {
|
|
27
|
+
$: Scope<$>;
|
|
28
|
+
get in(): Type<this["tIn"], $>;
|
|
29
|
+
get out(): Type<this["tOut"], $>;
|
|
30
|
+
intersect<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint;
|
|
31
|
+
and<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>;
|
|
32
|
+
or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>;
|
|
33
|
+
array(): Type<t[], $>;
|
|
34
|
+
keyof(): Type<keyof this["inferIn"], $>;
|
|
35
|
+
pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>;
|
|
36
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(a: a, b: b): Type<inferPipes<t, [a, b]>, $>;
|
|
37
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>>(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>;
|
|
38
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>>(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>;
|
|
39
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>>(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>;
|
|
40
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>>(a: a, b: b, c: c, d: d, e: e, f: f): Type<inferPipes<t, [a, b, c, d, e, f]>, $>;
|
|
41
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>;
|
|
42
|
+
narrow<predicate extends Predicate<distillOut<t>>>(predicate: predicate): Type<t extends MorphAst ? inferPredicate<this["tOut"], predicate> extends infer narrowed ? (In: this["tIn"]) => Out<narrowed> : never : inferPredicate<t, predicate>, $>;
|
|
43
|
+
equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
44
|
+
extract<def>(r: validateTypeRoot<def, $>): Type<t, $>;
|
|
45
|
+
exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>;
|
|
46
|
+
extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
47
|
+
overlaps<def>(r: validateTypeRoot<def, $>): boolean;
|
|
48
|
+
get<k1 extends indexOf<t>>(k1: k1 | type.cast<k1>): Type<indexInto<t, k1>, $>;
|
|
49
|
+
get<k1 extends indexOf<t>, k2 extends indexOf<indexInto<t, k1>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): Type<indexInto<indexInto<t, k1>, k2>, $>;
|
|
50
|
+
get<k1 extends indexOf<t>, k2 extends indexOf<indexInto<t, k1>>, k3 extends indexOf<indexInto<indexInto<t, k1>, k2>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): Type<indexInto<indexInto<indexInto<t, k1>, k2>, k3>, $>;
|
|
51
|
+
constrain<kind extends PrimitiveConstraintKind, const def extends NodeSchema<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
|
|
52
|
+
satisfying<predicate extends Predicate<distillIn<t>>>(predicate: predicate): Type<t extends MorphAst ? (In: inferPredicate<this["tIn"], predicate>) => Out<this["tOut"]> : inferPredicate<t, predicate>, $>;
|
|
53
|
+
divisibleBy<const schema extends DivisorSchema>(this: validateChainedConstraint<"divisor", this>, schema: schema): Type<constrain<t, "divisor", schema>, $>;
|
|
54
|
+
matching<const schema extends PatternSchema>(this: validateChainedConstraint<"pattern", this>, schema: schema): Type<constrain<t, "pattern", schema>, $>;
|
|
55
|
+
atLeast<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", schema>, $>;
|
|
56
|
+
atMost<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", schema>, $>;
|
|
57
|
+
moreThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", exclusivizeRangeSchema<schema>>, $>;
|
|
58
|
+
lessThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", exclusivizeRangeSchema<schema>>, $>;
|
|
59
|
+
atLeastLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", schema>, $>;
|
|
60
|
+
atMostLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", schema>, $>;
|
|
61
|
+
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
|
|
62
|
+
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
|
|
63
|
+
exactlyLength<const schema extends ExactLengthSchema>(this: validateChainedConstraint<"exactLength", this>, schema: schema): Type<constrain<t, "exactLength", schema>, $>;
|
|
64
|
+
atOrAfter<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", schema>, $>;
|
|
65
|
+
atOrBefore<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", schema>, $>;
|
|
66
|
+
laterThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", exclusivizeRangeSchema<schema>>, $>;
|
|
67
|
+
earlierThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", exclusivizeRangeSchema<schema>>, $>;
|
|
68
|
+
}
|
|
69
|
+
export interface Type<
|
|
70
|
+
/** @ts-expect-error allow instantiation assignment to the base type */
|
|
71
|
+
out t = unknown, $ = {}> extends _Type<t, $> {
|
|
72
|
+
}
|
|
73
|
+
export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
|
|
74
|
+
export type AnyType<out t = unknown> = Type<t, any>;
|
|
75
|
+
export declare const Type: TypeConstructor;
|
|
76
|
+
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
|
|
77
|
+
export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
|
|
78
|
+
export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $, bindThis<def>>;
|
|
79
|
+
export {};
|
package/out/type.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ArkErrors, BaseRoot } from "@ark/schema";
|
|
2
|
+
import { Callable } from "@ark/util";
|
|
3
|
+
import { parseGenericParams } from "./generic.js";
|
|
4
|
+
export class RawTypeParser extends Callable {
|
|
5
|
+
constructor($) {
|
|
6
|
+
super((...args) => {
|
|
7
|
+
if (args.length === 1) {
|
|
8
|
+
// treat as a simple definition
|
|
9
|
+
return $.parseRoot(args[0]);
|
|
10
|
+
}
|
|
11
|
+
if (args.length === 2 &&
|
|
12
|
+
typeof args[0] === "string" &&
|
|
13
|
+
args[0][0] === "<" &&
|
|
14
|
+
args[0].at(-1) === ">") {
|
|
15
|
+
// if there are exactly two args, the first of which looks like <${string}>,
|
|
16
|
+
// treat as a generic
|
|
17
|
+
const params = parseGenericParams(args[0].slice(1, -1), {
|
|
18
|
+
$,
|
|
19
|
+
args: {}
|
|
20
|
+
});
|
|
21
|
+
const def = args[1];
|
|
22
|
+
return $.generic(params, def);
|
|
23
|
+
}
|
|
24
|
+
// otherwise, treat as a tuple expression. technically, this also allows
|
|
25
|
+
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
26
|
+
// part of the API as specified by the associated types
|
|
27
|
+
return $.parseRoot(args);
|
|
28
|
+
}, {
|
|
29
|
+
bind: $,
|
|
30
|
+
attach: {
|
|
31
|
+
errors: ArkErrors,
|
|
32
|
+
raw: $.parseRoot
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const Type = BaseRoot;
|