arktype 1.1.0-dev-1 → 1.1.0-dev-2
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/main.d.ts +11 -1018
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +8 -1450
- package/dist/main.js.map +1 -0
- package/dist/match.d.ts +46 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/match.js +23 -0
- package/dist/match.js.map +1 -0
- package/dist/parser/definition.d.ts +32 -0
- package/dist/parser/definition.d.ts.map +1 -0
- package/dist/parser/definition.js +29 -0
- package/dist/parser/definition.js.map +1 -0
- package/dist/parser/generic.d.ts +44 -0
- package/dist/parser/generic.d.ts.map +1 -0
- package/dist/parser/generic.js +46 -0
- package/dist/parser/generic.js.map +1 -0
- package/dist/parser/objectLiteral.d.ts +24 -0
- package/dist/parser/objectLiteral.d.ts.map +1 -0
- package/dist/parser/objectLiteral.js +38 -0
- package/dist/parser/objectLiteral.js.map +1 -0
- package/dist/parser/semantic/bounds.d.ts +13 -0
- package/dist/parser/semantic/bounds.d.ts.map +1 -0
- package/dist/parser/semantic/bounds.js +2 -0
- package/dist/parser/semantic/bounds.js.map +1 -0
- package/dist/parser/semantic/divisor.d.ts +9 -0
- package/dist/parser/semantic/divisor.d.ts.map +1 -0
- package/dist/parser/semantic/divisor.js +2 -0
- package/dist/parser/semantic/divisor.js.map +1 -0
- package/dist/parser/semantic/intersections.d.ts +15 -0
- package/dist/parser/semantic/intersections.d.ts.map +1 -0
- package/dist/parser/semantic/intersections.js +2 -0
- package/dist/parser/semantic/intersections.js.map +1 -0
- package/dist/parser/semantic/semantic.d.ts +22 -0
- package/dist/parser/semantic/semantic.d.ts.map +1 -0
- package/dist/parser/semantic/semantic.js +2 -0
- package/dist/parser/semantic/semantic.js.map +1 -0
- package/dist/parser/semantic/utils.d.ts +8 -0
- package/dist/parser/semantic/utils.d.ts.map +1 -0
- package/dist/parser/semantic/utils.js +2 -0
- package/dist/parser/semantic/utils.js.map +1 -0
- package/dist/parser/semantic/validate.d.ts +24 -0
- package/dist/parser/semantic/validate.d.ts.map +1 -0
- package/dist/parser/semantic/validate.js +2 -0
- package/dist/parser/semantic/validate.js.map +1 -0
- package/dist/parser/shared.d.ts +48 -0
- package/dist/parser/shared.d.ts.map +1 -0
- package/dist/parser/shared.js +41 -0
- package/dist/parser/shared.js.map +1 -0
- package/dist/parser/string/reduce/dynamic.d.ts +40 -0
- package/dist/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/dist/parser/string/reduce/dynamic.js +122 -0
- package/dist/parser/string/reduce/dynamic.js.map +1 -0
- package/dist/parser/string/reduce/shared.d.ts +29 -0
- package/dist/parser/string/reduce/shared.d.ts.map +1 -0
- package/dist/parser/string/reduce/shared.js +7 -0
- package/dist/parser/string/reduce/shared.js.map +1 -0
- package/dist/parser/string/reduce/static.d.ts +177 -0
- package/dist/parser/string/reduce/static.d.ts.map +1 -0
- package/dist/parser/string/reduce/static.js +2 -0
- package/dist/parser/string/reduce/static.js.map +1 -0
- package/dist/parser/string/shift/operand/date.d.ts +11 -0
- package/dist/parser/string/shift/operand/date.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/date.js +26 -0
- package/dist/parser/string/shift/operand/date.js.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.js +50 -0
- package/dist/parser/string/shift/operand/enclosed.js.map +1 -0
- package/dist/parser/string/shift/operand/operand.d.ts +9 -0
- package/dist/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/operand.js +17 -0
- package/dist/parser/string/shift/operand/operand.js.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts +36 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.js +68 -0
- package/dist/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/dist/parser/string/shift/operator/bounds.d.ts +55 -0
- package/dist/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/bounds.js +95 -0
- package/dist/parser/string/shift/operator/bounds.js.map +1 -0
- package/dist/parser/string/shift/operator/divisor.d.ts +9 -0
- package/dist/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/divisor.js +13 -0
- package/dist/parser/string/shift/operator/divisor.js.map +1 -0
- package/dist/parser/string/shift/operator/operator.d.ts +13 -0
- package/dist/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/operator.js +29 -0
- package/dist/parser/string/shift/operator/operator.js.map +1 -0
- package/dist/parser/string/shift/scanner.d.ts +71 -0
- package/dist/parser/string/shift/scanner.d.ts.map +1 -0
- package/dist/parser/string/shift/scanner.js +99 -0
- package/dist/parser/string/shift/scanner.js.map +1 -0
- package/dist/parser/string/string.d.ts +26 -0
- package/dist/parser/string/string.d.ts.map +1 -0
- package/dist/parser/string/string.js +31 -0
- package/dist/parser/string/string.js.map +1 -0
- package/dist/parser/tuple.d.ts +73 -0
- package/dist/parser/tuple.d.ts.map +1 -0
- package/dist/parser/tuple.js +134 -0
- package/dist/parser/tuple.js.map +1 -0
- package/dist/scope.d.ts +159 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +248 -0
- package/dist/scope.js.map +1 -0
- package/dist/scopes/ark.d.ts +38 -0
- package/dist/scopes/ark.d.ts.map +1 -0
- package/dist/scopes/ark.js +24 -0
- package/dist/scopes/ark.js.map +1 -0
- package/dist/scopes/jsObjects.d.ts +19 -0
- package/dist/scopes/jsObjects.d.ts.map +1 -0
- package/dist/scopes/jsObjects.js +15 -0
- package/dist/scopes/jsObjects.js.map +1 -0
- package/dist/scopes/parsing.d.ts +18 -0
- package/dist/scopes/parsing.d.ts.map +1 -0
- package/dist/scopes/parsing.js +61 -0
- package/dist/scopes/parsing.js.map +1 -0
- package/dist/scopes/tsGenerics.d.ts +17 -0
- package/dist/scopes/tsGenerics.d.ts.map +1 -0
- package/dist/scopes/tsGenerics.js +13 -0
- package/dist/scopes/tsGenerics.js.map +1 -0
- package/dist/scopes/tsKeywords.d.ts +24 -0
- package/dist/scopes/tsKeywords.d.ts.map +1 -0
- package/dist/scopes/tsKeywords.js +20 -0
- package/dist/scopes/tsKeywords.js.map +1 -0
- package/dist/scopes/utils/creditCard.d.ts +3 -0
- package/dist/scopes/utils/creditCard.d.ts.map +1 -0
- package/dist/scopes/utils/creditCard.js +36 -0
- package/dist/scopes/utils/creditCard.js.map +1 -0
- package/dist/scopes/utils/date.d.ts +24 -0
- package/dist/scopes/utils/date.d.ts.map +1 -0
- package/dist/scopes/utils/date.js +53 -0
- package/dist/scopes/utils/date.js.map +1 -0
- package/dist/scopes/validation.d.ts +20 -0
- package/dist/scopes/validation.d.ts.map +1 -0
- package/dist/scopes/validation.js +57 -0
- package/dist/scopes/validation.js.map +1 -0
- package/dist/type.d.ts +101 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +138 -0
- package/dist/type.js.map +1 -0
- package/main.ts +20 -0
- package/match.ts +137 -0
- package/package.json +12 -7
- package/parser/definition.ts +156 -0
- package/parser/generic.ts +208 -0
- package/parser/objectLiteral.ts +120 -0
- package/parser/semantic/bounds.ts +63 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +47 -0
- package/parser/semantic/semantic.ts +107 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +105 -0
- package/parser/shared.ts +141 -0
- package/parser/string/reduce/dynamic.ts +181 -0
- package/parser/string/reduce/shared.ts +73 -0
- package/parser/string/reduce/static.ts +300 -0
- package/parser/string/shift/operand/date.ts +57 -0
- package/parser/string/shift/operand/enclosed.ts +119 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +282 -0
- package/parser/string/shift/operator/bounds.ts +229 -0
- package/parser/string/shift/operator/divisor.ts +36 -0
- package/parser/string/shift/operator/operator.ts +82 -0
- package/parser/string/shift/scanner.ts +209 -0
- package/parser/string/string.ts +93 -0
- package/parser/tuple.ts +475 -0
- package/scope.ts +573 -0
- package/scopes/ark.ts +64 -0
- package/scopes/jsObjects.ts +29 -0
- package/scopes/parsing.ts +81 -0
- package/scopes/tsGenerics.ts +28 -0
- package/scopes/tsKeywords.ts +39 -0
- package/scopes/utils/creditCard.ts +37 -0
- package/scopes/utils/date.ts +108 -0
- package/scopes/validation.ts +81 -0
- package/type.ts +403 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NumericallyBoundable,
|
|
3
|
+
writeUnboundableMessage
|
|
4
|
+
} from "@arktype/schema"
|
|
5
|
+
import type { ErrorMessage } from "@arktype/util"
|
|
6
|
+
import type { DateLiteral } from "../string/shift/operand/date.js"
|
|
7
|
+
import type {
|
|
8
|
+
BoundKind,
|
|
9
|
+
Comparator,
|
|
10
|
+
InvertedComparators,
|
|
11
|
+
LimitLiteral,
|
|
12
|
+
writeInvalidLimitMessage
|
|
13
|
+
} from "../string/shift/operator/bounds.js"
|
|
14
|
+
import type { inferAst } from "./semantic.js"
|
|
15
|
+
import type { astToString } from "./utils.js"
|
|
16
|
+
import type { validateAst } from "./validate.js"
|
|
17
|
+
|
|
18
|
+
export type validateRange<
|
|
19
|
+
l,
|
|
20
|
+
comparator extends Comparator,
|
|
21
|
+
r,
|
|
22
|
+
$,
|
|
23
|
+
args
|
|
24
|
+
> = l extends LimitLiteral
|
|
25
|
+
? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
|
|
26
|
+
: l extends [infer leftAst, Comparator, unknown]
|
|
27
|
+
? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
|
|
28
|
+
: validateBound<l, comparator, r & LimitLiteral, "right", $, args>
|
|
29
|
+
|
|
30
|
+
export type validateBound<
|
|
31
|
+
boundedAst,
|
|
32
|
+
comparator extends Comparator,
|
|
33
|
+
limit extends LimitLiteral,
|
|
34
|
+
boundKind extends BoundKind,
|
|
35
|
+
$,
|
|
36
|
+
args
|
|
37
|
+
> = inferAst<boundedAst, $, args> extends infer bounded
|
|
38
|
+
? [bounded] extends [NumericallyBoundable]
|
|
39
|
+
? limit extends number
|
|
40
|
+
? validateAst<boundedAst, $, args>
|
|
41
|
+
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
42
|
+
: bounded extends Date
|
|
43
|
+
? limit extends DateLiteral
|
|
44
|
+
? validateAst<boundedAst, $, args>
|
|
45
|
+
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
46
|
+
: ErrorMessage<
|
|
47
|
+
writeUnboundableMessage<
|
|
48
|
+
astToString<
|
|
49
|
+
boundKind extends "left"
|
|
50
|
+
? boundedAst[0 & keyof boundedAst]
|
|
51
|
+
: boundedAst
|
|
52
|
+
>
|
|
53
|
+
>
|
|
54
|
+
>
|
|
55
|
+
: never
|
|
56
|
+
|
|
57
|
+
export const writeDoubleRightBoundMessage = <root extends string>(
|
|
58
|
+
root: root
|
|
59
|
+
): writeDoubleRightBoundMessage<root> =>
|
|
60
|
+
`Expression ${root} must have at most one right bound`
|
|
61
|
+
|
|
62
|
+
type writeDoubleRightBoundMessage<root extends string> =
|
|
63
|
+
`Expression ${root} must have at most one right bound`
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { writeIndivisibleMessage } from "@arktype/schema"
|
|
2
|
+
import type { ErrorMessage } from "@arktype/util"
|
|
3
|
+
import type { inferAst } from "./semantic.js"
|
|
4
|
+
import type { astToString } from "./utils.js"
|
|
5
|
+
import type { validateAst } from "./validate.js"
|
|
6
|
+
|
|
7
|
+
export type validateDivisor<l, $, args> = isDivisible<
|
|
8
|
+
inferAst<l, $, args>
|
|
9
|
+
> extends true
|
|
10
|
+
? validateAst<l, $, args>
|
|
11
|
+
: ErrorMessage<writeIndivisibleMessage<astToString<l>>>
|
|
12
|
+
|
|
13
|
+
type isDivisible<data> = [data] extends [number] ? true : false
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Out } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
Hkt,
|
|
4
|
+
type List,
|
|
5
|
+
type conform,
|
|
6
|
+
type evaluate,
|
|
7
|
+
type intersectArrays,
|
|
8
|
+
type isAny
|
|
9
|
+
} from "@arktype/util"
|
|
10
|
+
import type { MorphAst } from "../tuple.js"
|
|
11
|
+
|
|
12
|
+
export type inferIntersection<l, r> = [l] extends [never]
|
|
13
|
+
? never
|
|
14
|
+
: [r] extends [never]
|
|
15
|
+
? never
|
|
16
|
+
: [l & r] extends [never]
|
|
17
|
+
? never
|
|
18
|
+
: isAny<l | r> extends true
|
|
19
|
+
? any
|
|
20
|
+
: l extends MorphAst<infer lIn, infer lOut>
|
|
21
|
+
? r extends MorphAst
|
|
22
|
+
? never
|
|
23
|
+
: (In: evaluate<lIn & r>) => Out<lOut>
|
|
24
|
+
: r extends MorphAst<infer rIn, infer rOut>
|
|
25
|
+
? (In: evaluate<rIn & l>) => Out<rOut>
|
|
26
|
+
: [l, r] extends [object, object]
|
|
27
|
+
? intersectObjects<l, r> extends infer result
|
|
28
|
+
? result
|
|
29
|
+
: never
|
|
30
|
+
: l & r
|
|
31
|
+
|
|
32
|
+
declare class MorphableIntersection extends Hkt.Kind {
|
|
33
|
+
f: (
|
|
34
|
+
In: conform<this[Hkt.key], [l: unknown, r: unknown]>
|
|
35
|
+
) => inferIntersection<(typeof In)[0], (typeof In)[1]>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type intersectObjects<l, r> = [l, r] extends [
|
|
39
|
+
infer lList extends List,
|
|
40
|
+
infer rList extends List
|
|
41
|
+
]
|
|
42
|
+
? intersectArrays<lList, rList, MorphableIntersection>
|
|
43
|
+
: evaluate<
|
|
44
|
+
{
|
|
45
|
+
[k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
|
|
46
|
+
} & Omit<r, keyof l>
|
|
47
|
+
>
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { BigintLiteral, List, NumberLiteral } from "@arktype/util"
|
|
2
|
+
import type {
|
|
3
|
+
UnparsedScope,
|
|
4
|
+
resolve,
|
|
5
|
+
tryInferSubmoduleReference
|
|
6
|
+
} from "../../scope.js"
|
|
7
|
+
import type { GenericProps } from "../../type.js"
|
|
8
|
+
import type { inferDefinition } from "../definition.js"
|
|
9
|
+
import type { DateLiteral } from "../string/shift/operand/date.js"
|
|
10
|
+
import type { StringLiteral } from "../string/shift/operand/enclosed.js"
|
|
11
|
+
import type {
|
|
12
|
+
Comparator,
|
|
13
|
+
LimitLiteral
|
|
14
|
+
} from "../string/shift/operator/bounds.js"
|
|
15
|
+
import type { inferIntersection } from "./intersections.js"
|
|
16
|
+
|
|
17
|
+
export type inferAst<ast, $, args> = ast extends List
|
|
18
|
+
? inferExpression<ast, $, args>
|
|
19
|
+
: inferTerminal<ast, $, args>
|
|
20
|
+
|
|
21
|
+
export type GenericInstantiationAst<
|
|
22
|
+
g extends GenericProps = GenericProps,
|
|
23
|
+
argAsts extends unknown[] = unknown[]
|
|
24
|
+
> = [g, "<>", argAsts]
|
|
25
|
+
|
|
26
|
+
export type inferExpression<
|
|
27
|
+
ast extends List,
|
|
28
|
+
$,
|
|
29
|
+
args
|
|
30
|
+
> = ast extends GenericInstantiationAst
|
|
31
|
+
? inferDefinition<
|
|
32
|
+
ast[0]["definition"],
|
|
33
|
+
ast[0]["$"] extends UnparsedScope
|
|
34
|
+
? // If the generic was defined in the current scope, its definition can be
|
|
35
|
+
// resolved using the same scope as that of the input args.
|
|
36
|
+
$
|
|
37
|
+
: // Otherwise, use the scope that was explicitly associated with it.
|
|
38
|
+
ast[0]["$"],
|
|
39
|
+
{
|
|
40
|
+
// Using keyof g["parameters"] & number here results in the element types
|
|
41
|
+
// being mixed- another reason TS should not have separate `${number}` and number keys!
|
|
42
|
+
[i in keyof ast[0]["parameters"] &
|
|
43
|
+
`${number}` as ast[0]["parameters"][i]]: inferAst<
|
|
44
|
+
ast[2][i & keyof ast[2]],
|
|
45
|
+
$,
|
|
46
|
+
args
|
|
47
|
+
>
|
|
48
|
+
}
|
|
49
|
+
>
|
|
50
|
+
: ast[1] extends "[]"
|
|
51
|
+
? inferAst<ast[0], $, args>[]
|
|
52
|
+
: ast[1] extends "|"
|
|
53
|
+
? inferAst<ast[0], $, args> | inferAst<ast[2], $, args>
|
|
54
|
+
: ast[1] extends "&"
|
|
55
|
+
? inferIntersection<
|
|
56
|
+
inferAst<ast[0], $, args>,
|
|
57
|
+
inferAst<ast[2], $, args>
|
|
58
|
+
>
|
|
59
|
+
: ast[1] extends Comparator
|
|
60
|
+
? ast[0] extends LimitLiteral
|
|
61
|
+
? inferAst<ast[2], $, args>
|
|
62
|
+
: inferAst<ast[0], $, args>
|
|
63
|
+
: ast[1] extends "%"
|
|
64
|
+
? inferAst<ast[0], $, args>
|
|
65
|
+
: ast[0] extends "keyof"
|
|
66
|
+
? keyof inferAst<ast[1], $, args>
|
|
67
|
+
: never
|
|
68
|
+
|
|
69
|
+
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
|
|
70
|
+
|
|
71
|
+
export type PrefixExpression<
|
|
72
|
+
operator extends PrefixOperator = PrefixOperator,
|
|
73
|
+
operand = unknown
|
|
74
|
+
> = [operator, operand]
|
|
75
|
+
|
|
76
|
+
export type PostfixOperator = "[]"
|
|
77
|
+
|
|
78
|
+
export type PostfixExpression<
|
|
79
|
+
operator extends PostfixOperator = PostfixOperator,
|
|
80
|
+
operand = unknown
|
|
81
|
+
> = readonly [operand, operator]
|
|
82
|
+
|
|
83
|
+
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
|
|
84
|
+
|
|
85
|
+
export type InfixExpression<
|
|
86
|
+
operator extends InfixOperator = InfixOperator,
|
|
87
|
+
l = unknown,
|
|
88
|
+
r = unknown
|
|
89
|
+
> = [l, operator, r]
|
|
90
|
+
|
|
91
|
+
export type RegexLiteral<source extends string = string> = `/${source}/`
|
|
92
|
+
|
|
93
|
+
export type inferTerminal<token, $, args> = token extends keyof args | keyof $
|
|
94
|
+
? resolve<token, $, args>
|
|
95
|
+
: token extends StringLiteral<infer Text>
|
|
96
|
+
? Text
|
|
97
|
+
: token extends RegexLiteral
|
|
98
|
+
? string
|
|
99
|
+
: token extends DateLiteral
|
|
100
|
+
? Date
|
|
101
|
+
: token extends NumberLiteral<infer value>
|
|
102
|
+
? value
|
|
103
|
+
: token extends BigintLiteral<infer value>
|
|
104
|
+
? value
|
|
105
|
+
: // doing this last allows us to infer never if it isn't valid rather than check
|
|
106
|
+
// if it's a valid submodule reference ahead of time
|
|
107
|
+
tryInferSubmoduleReference<$, token>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { List, Stringifiable } from "@arktype/util"
|
|
2
|
+
import type { Comparator } from "../string/shift/operator/bounds.js"
|
|
3
|
+
import type { InfixExpression, PostfixExpression } from "./semantic.js"
|
|
4
|
+
|
|
5
|
+
export type astToString<ast> = `'${astToStringRecurse<ast>}'`
|
|
6
|
+
|
|
7
|
+
type astToStringRecurse<ast> = ast extends PostfixExpression<
|
|
8
|
+
infer operator,
|
|
9
|
+
infer operand
|
|
10
|
+
>
|
|
11
|
+
? operator extends "[]"
|
|
12
|
+
? `${groupAst<operand>}[]`
|
|
13
|
+
: never
|
|
14
|
+
: ast extends InfixExpression<infer operator, infer l, infer r>
|
|
15
|
+
? operator extends "&" | "|" | "%" | Comparator
|
|
16
|
+
? `${groupAst<l>}${operator}${groupAst<r>}`
|
|
17
|
+
: never
|
|
18
|
+
: ast extends Stringifiable
|
|
19
|
+
? `${ast extends bigint ? `${ast}n` : ast}`
|
|
20
|
+
: "..."
|
|
21
|
+
|
|
22
|
+
type groupAst<ast> = ast extends List
|
|
23
|
+
? ast[1] extends "[]"
|
|
24
|
+
? astToStringRecurse<ast>
|
|
25
|
+
: `(${astToStringRecurse<ast>})`
|
|
26
|
+
: astToStringRecurse<ast>
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BigintLiteral,
|
|
3
|
+
Completion,
|
|
4
|
+
ErrorMessage,
|
|
5
|
+
NumberLiteral,
|
|
6
|
+
writeMalformedNumericLiteralMessage
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { Module } from "../../scope.js"
|
|
9
|
+
import type { GenericProps } from "../../type.js"
|
|
10
|
+
import type { writeInvalidGenericArgsMessage } from "../generic.js"
|
|
11
|
+
import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
|
|
12
|
+
import type { Comparator } from "../string/shift/operator/bounds.js"
|
|
13
|
+
import type { parseString } from "../string/string.js"
|
|
14
|
+
import type { validateRange } from "./bounds.js"
|
|
15
|
+
import type { validateDivisor } from "./divisor.js"
|
|
16
|
+
import type {
|
|
17
|
+
GenericInstantiationAst,
|
|
18
|
+
InfixExpression,
|
|
19
|
+
PostfixExpression
|
|
20
|
+
} from "./semantic.js"
|
|
21
|
+
import type { astToString } from "./utils.js"
|
|
22
|
+
|
|
23
|
+
export type validateAst<ast, $, args> = ast extends string
|
|
24
|
+
? validateStringAst<ast, $>
|
|
25
|
+
: ast extends PostfixExpression<infer operator, infer operand>
|
|
26
|
+
? operator extends "[]"
|
|
27
|
+
? validateAst<operand, $, args>
|
|
28
|
+
: never
|
|
29
|
+
: ast extends InfixExpression<infer operator, infer l, infer r>
|
|
30
|
+
? operator extends "&" | "|"
|
|
31
|
+
? validateInfix<ast, $, args>
|
|
32
|
+
: operator extends Comparator
|
|
33
|
+
? validateRange<l, operator, r, $, args>
|
|
34
|
+
: operator extends "%"
|
|
35
|
+
? validateDivisor<l, $, args>
|
|
36
|
+
: undefined
|
|
37
|
+
: ast extends readonly ["keyof", infer operand]
|
|
38
|
+
? validateAst<operand, $, args>
|
|
39
|
+
: ast extends GenericInstantiationAst
|
|
40
|
+
? validateGenericArgs<ast["2"], $, args>
|
|
41
|
+
: ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
|
|
42
|
+
|
|
43
|
+
type writeUnexpectedExpressionMessage<expression extends string> =
|
|
44
|
+
`Unexpectedly failed to parse the expression resulting from ${expression}`
|
|
45
|
+
|
|
46
|
+
type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
|
|
47
|
+
infer head,
|
|
48
|
+
...infer tail
|
|
49
|
+
]
|
|
50
|
+
? validateAst<head, $, args> extends ErrorMessage<infer message>
|
|
51
|
+
? ErrorMessage<message>
|
|
52
|
+
: validateGenericArgs<tail, $, args>
|
|
53
|
+
: undefined
|
|
54
|
+
|
|
55
|
+
export const writeUnsatisfiableExpressionError = <expression extends string>(
|
|
56
|
+
expression: expression
|
|
57
|
+
): writeUnsatisfiableExpressionError<expression> =>
|
|
58
|
+
`${expression} results in an unsatisfiable type`
|
|
59
|
+
|
|
60
|
+
export type writeUnsatisfiableExpressionError<expression extends string> =
|
|
61
|
+
`${expression} results in an unsatisfiable type`
|
|
62
|
+
|
|
63
|
+
type validateStringAst<def extends string, $> = def extends NumberLiteral<
|
|
64
|
+
infer value
|
|
65
|
+
>
|
|
66
|
+
? number extends value
|
|
67
|
+
? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
|
|
68
|
+
: undefined
|
|
69
|
+
: def extends BigintLiteral<infer value>
|
|
70
|
+
? bigint extends value
|
|
71
|
+
? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
|
|
72
|
+
: undefined
|
|
73
|
+
: def extends keyof $
|
|
74
|
+
? // these problems would've been caught during a fullStringParse, but it's most
|
|
75
|
+
// efficient to check for them here in case the string was naively parsed
|
|
76
|
+
$[def] extends GenericProps
|
|
77
|
+
? ErrorMessage<
|
|
78
|
+
writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
|
|
79
|
+
>
|
|
80
|
+
: $[def] extends Module
|
|
81
|
+
? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
|
|
82
|
+
: undefined
|
|
83
|
+
: def extends ErrorMessage
|
|
84
|
+
? def
|
|
85
|
+
: undefined
|
|
86
|
+
|
|
87
|
+
export type validateString<def extends string, $, args> = validateAst<
|
|
88
|
+
parseString<def, $, args>,
|
|
89
|
+
$,
|
|
90
|
+
args
|
|
91
|
+
> extends infer result extends ErrorMessage
|
|
92
|
+
? result extends Completion<infer text>
|
|
93
|
+
? text
|
|
94
|
+
: result
|
|
95
|
+
: def
|
|
96
|
+
|
|
97
|
+
type validateInfix<ast extends InfixExpression, $, args> = validateAst<
|
|
98
|
+
ast[0],
|
|
99
|
+
$,
|
|
100
|
+
args
|
|
101
|
+
> extends ErrorMessage<infer message>
|
|
102
|
+
? ErrorMessage<message>
|
|
103
|
+
: validateAst<ast[2], $, args> extends ErrorMessage<infer message>
|
|
104
|
+
? ErrorMessage<message>
|
|
105
|
+
: undefined
|
package/parser/shared.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { extend } from "@arktype/util"
|
|
2
|
+
import type { validateDefinition } from "./definition.js"
|
|
3
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
4
|
+
|
|
5
|
+
type parsedKey<result extends KeyParseResult> = result
|
|
6
|
+
|
|
7
|
+
type OptionalTuple<value> = readonly [value, "?"]
|
|
8
|
+
|
|
9
|
+
type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
|
|
10
|
+
kind: kind
|
|
11
|
+
innerKey: string | symbol
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type ValueParseResult<kind extends ParsedValueKind = ParsedValueKind> = {
|
|
15
|
+
kind: kind
|
|
16
|
+
innerValue: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type EntryParseResult<kind extends ParsedKeyKind = ParsedKeyKind> =
|
|
20
|
+
extend<
|
|
21
|
+
KeyParseResult<kind>,
|
|
22
|
+
{
|
|
23
|
+
innerValue: unknown
|
|
24
|
+
}
|
|
25
|
+
>
|
|
26
|
+
|
|
27
|
+
type ParsedValueKind = "required" | "optional"
|
|
28
|
+
|
|
29
|
+
type ParsedKeyKind = "required" | "optional" | "indexed"
|
|
30
|
+
|
|
31
|
+
type parsedEntry<result extends EntryParseResult> = result
|
|
32
|
+
|
|
33
|
+
export type OptionalValue<value> =
|
|
34
|
+
| OptionalStringDefinition<value & string>
|
|
35
|
+
| OptionalTuple<value>
|
|
36
|
+
|
|
37
|
+
export type OptionalStringDefinition<name extends string = string> = `${name}?`
|
|
38
|
+
|
|
39
|
+
export type IndexedKey<def extends string = string> = `[${def}]`
|
|
40
|
+
|
|
41
|
+
export type validateObjectValue<def, $, args> = def extends OptionalTuple<
|
|
42
|
+
infer value
|
|
43
|
+
>
|
|
44
|
+
? readonly [validateObjectValueString<value, $, args>, "?"]
|
|
45
|
+
: validateObjectValueString<def, $, args>
|
|
46
|
+
|
|
47
|
+
type validateObjectValueString<def, $, args> =
|
|
48
|
+
def extends OptionalStringDefinition<infer innerValue>
|
|
49
|
+
? `${validateDefinition<innerValue, $, args>}?`
|
|
50
|
+
: validateDefinition<def, $, args>
|
|
51
|
+
|
|
52
|
+
type DefinitionEntry = readonly [string | symbol, unknown]
|
|
53
|
+
|
|
54
|
+
const getInnerValue = (value: unknown): ValueParseResult => {
|
|
55
|
+
if (typeof value === "string") {
|
|
56
|
+
if (value[value.length - 1] === "?") {
|
|
57
|
+
return {
|
|
58
|
+
kind: "optional",
|
|
59
|
+
innerValue: value.slice(0, -1)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} else if (Array.isArray(value)) {
|
|
63
|
+
if (value.length === 2 && value[1] === "?") {
|
|
64
|
+
return {
|
|
65
|
+
kind: "optional",
|
|
66
|
+
innerValue: getInnerValue(value[0]).innerValue
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
kind: "required",
|
|
72
|
+
innerValue: value
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
|
|
77
|
+
const keyParseResult: KeyParseResult =
|
|
78
|
+
typeof key === "string" && key.at(-1) === "?"
|
|
79
|
+
? key.at(-2) === Scanner.escapeToken
|
|
80
|
+
? { innerKey: `${key.slice(0, -2)}?`, kind: "required" }
|
|
81
|
+
: { innerKey: key.slice(0, -1), kind: "optional" }
|
|
82
|
+
: { innerKey: key, kind: "required" }
|
|
83
|
+
const valueParseResult = getInnerValue(value)
|
|
84
|
+
return {
|
|
85
|
+
innerKey: keyParseResult.innerKey,
|
|
86
|
+
innerValue: valueParseResult.innerValue,
|
|
87
|
+
kind:
|
|
88
|
+
keyParseResult.kind === "indexed"
|
|
89
|
+
? "indexed"
|
|
90
|
+
: valueParseResult.kind === "optional"
|
|
91
|
+
? "optional"
|
|
92
|
+
: keyParseResult.kind
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type parseEntry<
|
|
97
|
+
keyDef extends PropertyKey,
|
|
98
|
+
valueDef
|
|
99
|
+
> = parseKey<keyDef> extends infer keyParseResult extends KeyParseResult
|
|
100
|
+
? valueDef extends OptionalValue<infer innerValue>
|
|
101
|
+
? parsedEntry<{
|
|
102
|
+
kind: "optional"
|
|
103
|
+
innerKey: keyParseResult["innerKey"]
|
|
104
|
+
innerValue: innerValue extends OptionalStringDefinition<
|
|
105
|
+
infer innerStringValue
|
|
106
|
+
>
|
|
107
|
+
? innerStringValue
|
|
108
|
+
: innerValue
|
|
109
|
+
}>
|
|
110
|
+
: parsedEntry<{
|
|
111
|
+
kind: keyParseResult["kind"]
|
|
112
|
+
innerKey: keyParseResult["innerKey"]
|
|
113
|
+
innerValue: valueDef
|
|
114
|
+
}>
|
|
115
|
+
: never
|
|
116
|
+
|
|
117
|
+
type parseKey<k> = k extends OptionalStringDefinition<infer inner>
|
|
118
|
+
? inner extends `${infer baseName}${Scanner.EscapeToken}`
|
|
119
|
+
? parsedKey<{
|
|
120
|
+
kind: "required"
|
|
121
|
+
innerKey: OptionalStringDefinition<baseName>
|
|
122
|
+
}>
|
|
123
|
+
: parsedKey<{
|
|
124
|
+
kind: "optional"
|
|
125
|
+
innerKey: inner
|
|
126
|
+
}>
|
|
127
|
+
: k extends IndexedKey<infer def>
|
|
128
|
+
? parsedKey<{
|
|
129
|
+
kind: "indexed"
|
|
130
|
+
innerKey: def
|
|
131
|
+
}>
|
|
132
|
+
: k extends `${Scanner.EscapeToken}${infer escapedIndexKey extends
|
|
133
|
+
IndexedKey}`
|
|
134
|
+
? parsedKey<{
|
|
135
|
+
kind: "required"
|
|
136
|
+
innerKey: escapedIndexKey
|
|
137
|
+
}>
|
|
138
|
+
: parsedKey<{
|
|
139
|
+
kind: "required"
|
|
140
|
+
innerKey: k & (string | symbol)
|
|
141
|
+
}>
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
isKeyOf,
|
|
4
|
+
throwInternalError,
|
|
5
|
+
throwParseError,
|
|
6
|
+
type requireKeys
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { ParseContext } from "../../../scope.js"
|
|
9
|
+
import {
|
|
10
|
+
invertedComparators,
|
|
11
|
+
minComparators,
|
|
12
|
+
type Comparator,
|
|
13
|
+
type LimitLiteral
|
|
14
|
+
} from "../shift/operator/bounds.js"
|
|
15
|
+
import { Scanner } from "../shift/scanner.js"
|
|
16
|
+
import {
|
|
17
|
+
writeMultipleLeftBoundsMessage,
|
|
18
|
+
writeOpenRangeMessage,
|
|
19
|
+
writeUnclosedGroupMessage,
|
|
20
|
+
writeUnmatchedGroupCloseMessage,
|
|
21
|
+
writeUnpairableComparatorMessage,
|
|
22
|
+
type OpenLeftBound,
|
|
23
|
+
type StringifiablePrefixOperator
|
|
24
|
+
} from "./shared.js"
|
|
25
|
+
|
|
26
|
+
type BranchState = {
|
|
27
|
+
prefixes: StringifiablePrefixOperator[]
|
|
28
|
+
leftBound?: OpenLeftBound
|
|
29
|
+
"&"?: Root
|
|
30
|
+
"|"?: Root
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type DynamicStateWithRoot = requireKeys<DynamicState, "root">
|
|
34
|
+
|
|
35
|
+
export class DynamicState {
|
|
36
|
+
readonly scanner: Scanner
|
|
37
|
+
root: Root | undefined
|
|
38
|
+
branches: BranchState = {
|
|
39
|
+
prefixes: []
|
|
40
|
+
}
|
|
41
|
+
finalizer: Scanner.FinalizingLookahead | undefined
|
|
42
|
+
groups: BranchState[] = []
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
def: string,
|
|
46
|
+
public readonly ctx: ParseContext
|
|
47
|
+
) {
|
|
48
|
+
this.scanner = new Scanner(def)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
error(message: string) {
|
|
52
|
+
return throwParseError(message)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
hasRoot(): this is DynamicStateWithRoot {
|
|
56
|
+
return this.root !== undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
unsetRoot(): this["root"] {
|
|
60
|
+
const value = this.root
|
|
61
|
+
this.root = undefined
|
|
62
|
+
return value
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
constrainRoot(...args: Parameters<Root["constrain"]>) {
|
|
66
|
+
this.root = this.root!.constrain(...args)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setRoot(root: Root) {
|
|
70
|
+
this.root = root
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
finalize(finalizer: Scanner.FinalizingLookahead) {
|
|
74
|
+
if (this.groups.length) {
|
|
75
|
+
return this.error(writeUnclosedGroupMessage(")"))
|
|
76
|
+
}
|
|
77
|
+
this.finalizeBranches()
|
|
78
|
+
this.finalizer = finalizer
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
reduceLeftBound(limit: LimitLiteral, comparator: Comparator) {
|
|
82
|
+
const invertedComparator = invertedComparators[comparator]
|
|
83
|
+
if (!isKeyOf(invertedComparator, minComparators)) {
|
|
84
|
+
return this.error(writeUnpairableComparatorMessage(comparator))
|
|
85
|
+
}
|
|
86
|
+
if (this.branches.leftBound) {
|
|
87
|
+
return this.error(
|
|
88
|
+
writeMultipleLeftBoundsMessage(
|
|
89
|
+
this.branches.leftBound.limit,
|
|
90
|
+
this.branches.leftBound.comparator,
|
|
91
|
+
limit,
|
|
92
|
+
invertedComparator
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
this.branches.leftBound = {
|
|
97
|
+
comparator: invertedComparator,
|
|
98
|
+
limit
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
finalizeBranches() {
|
|
103
|
+
this.assertRangeUnset()
|
|
104
|
+
if (this.branches["|"]) {
|
|
105
|
+
this.pushRootToBranch("|")
|
|
106
|
+
this.root = this.branches["|"]
|
|
107
|
+
} else if (this.branches["&"]) {
|
|
108
|
+
this.pushRootToBranch("&")
|
|
109
|
+
this.root = this.branches["&"]
|
|
110
|
+
} else {
|
|
111
|
+
this.applyPrefixes()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
finalizeGroup() {
|
|
116
|
+
this.finalizeBranches()
|
|
117
|
+
const topBranchState = this.groups.pop()
|
|
118
|
+
if (!topBranchState) {
|
|
119
|
+
return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
|
|
120
|
+
}
|
|
121
|
+
this.branches = topBranchState
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
addPrefix(prefix: StringifiablePrefixOperator) {
|
|
125
|
+
this.branches.prefixes.push(prefix)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
applyPrefixes() {
|
|
129
|
+
while (this.branches.prefixes.length) {
|
|
130
|
+
const lastPrefix = this.branches.prefixes.pop()!
|
|
131
|
+
this.root =
|
|
132
|
+
lastPrefix === "keyof"
|
|
133
|
+
? this.root!.keyof()
|
|
134
|
+
: throwInternalError(`Unexpected prefix '${lastPrefix}'`)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
pushRootToBranch(token: "|" | "&") {
|
|
139
|
+
this.assertRangeUnset()
|
|
140
|
+
this.applyPrefixes()
|
|
141
|
+
const root = this.root!
|
|
142
|
+
this.branches["&"] = this.branches["&"]?.and(root) ?? root
|
|
143
|
+
if (token === "|") {
|
|
144
|
+
this.branches["|"] =
|
|
145
|
+
this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"]
|
|
146
|
+
delete this.branches["&"]
|
|
147
|
+
}
|
|
148
|
+
this.root = undefined
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private assertRangeUnset() {
|
|
152
|
+
if (this.branches.leftBound) {
|
|
153
|
+
return this.error(
|
|
154
|
+
writeOpenRangeMessage(
|
|
155
|
+
this.branches.leftBound.limit,
|
|
156
|
+
this.branches.leftBound.comparator
|
|
157
|
+
)
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
reduceGroupOpen() {
|
|
163
|
+
this.groups.push(this.branches)
|
|
164
|
+
this.branches = {
|
|
165
|
+
prefixes: []
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
previousOperator() {
|
|
170
|
+
return (
|
|
171
|
+
this.branches.leftBound?.comparator ??
|
|
172
|
+
this.branches.prefixes.at(-1) ??
|
|
173
|
+
(this.branches["&"] ? "&" : this.branches["|"] ? "|" : undefined)
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
shiftedByOne() {
|
|
178
|
+
this.scanner.shift()
|
|
179
|
+
return this
|
|
180
|
+
}
|
|
181
|
+
}
|