arktype 1.1.0-dev-1 → 2.0.0-dev.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/__tests__/array.test.ts +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +18 -0
- package/match.ts +116 -0
- package/out/ark.d.ts +52 -0
- package/out/ark.d.ts.map +1 -0
- package/out/ark.js +19 -0
- package/out/ark.js.map +1 -0
- package/out/arkConfig.d.ts +2 -0
- package/out/arkConfig.d.ts.map +1 -0
- package/out/arkConfig.js +10 -0
- package/out/arkConfig.js.map +1 -0
- package/out/main.d.ts +7 -0
- package/out/main.d.ts.map +1 -0
- package/out/main.js +4 -0
- package/out/main.js.map +1 -0
- package/out/match.d.ts +48 -0
- package/out/match.d.ts.map +1 -0
- package/out/match.js +18 -0
- package/out/match.js.map +1 -0
- package/out/parser/definition.d.ts +32 -0
- package/out/parser/definition.d.ts.map +1 -0
- package/out/parser/definition.js +32 -0
- package/out/parser/definition.js.map +1 -0
- package/out/parser/generic.d.ts +18 -0
- package/out/parser/generic.d.ts.map +1 -0
- package/out/parser/generic.js +19 -0
- package/out/parser/generic.js.map +1 -0
- package/out/parser/objectLiteral.d.ts +33 -0
- package/out/parser/objectLiteral.d.ts.map +1 -0
- package/out/parser/objectLiteral.js +66 -0
- package/out/parser/objectLiteral.js.map +1 -0
- package/out/parser/semantic/bounds.d.ts +13 -0
- package/out/parser/semantic/bounds.d.ts.map +1 -0
- package/out/parser/semantic/bounds.js +2 -0
- package/out/parser/semantic/bounds.js.map +1 -0
- package/out/parser/semantic/divisor.d.ts +9 -0
- package/out/parser/semantic/divisor.d.ts.map +1 -0
- package/out/parser/semantic/divisor.js +2 -0
- package/out/parser/semantic/divisor.js.map +1 -0
- package/out/parser/semantic/intersections.d.ts +14 -0
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/intersections.js +2 -0
- package/out/parser/semantic/intersections.js.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/out/parser/semantic/semantic.js +2 -0
- package/out/parser/semantic/semantic.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +8 -0
- package/out/parser/semantic/utils.d.ts.map +1 -0
- package/out/parser/semantic/utils.js +2 -0
- package/out/parser/semantic/utils.js.map +1 -0
- package/out/parser/semantic/validate.d.ts +24 -0
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/semantic/validate.js.map +1 -0
- package/out/parser/shared.d.ts +54 -0
- package/out/parser/shared.d.ts.map +1 -0
- package/out/parser/shared.js +47 -0
- package/out/parser/shared.js.map +1 -0
- package/out/parser/string/reduce/dynamic.d.ts +42 -0
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/out/parser/string/reduce/dynamic.js +133 -0
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/out/parser/string/reduce/shared.d.ts +56 -0
- package/out/parser/string/reduce/shared.d.ts.map +1 -0
- package/out/parser/string/reduce/shared.js +26 -0
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/out/parser/string/reduce/static.d.ts +176 -0
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/out/parser/string/reduce/static.js +2 -0
- package/out/parser/string/reduce/static.js.map +1 -0
- package/out/parser/string/shift/operand/date.d.ts +11 -0
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/out/parser/string/shift/operand/date.js +26 -0
- package/out/parser/string/shift/operand/date.js.map +1 -0
- package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/enclosed.js +50 -0
- package/out/parser/string/shift/operand/enclosed.js.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.js +25 -0
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/out/parser/string/shift/operand/operand.js +17 -0
- package/out/parser/string/shift/operand/operand.js.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.js +68 -0
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/out/parser/string/shift/operator/bounds.d.ts +33 -0
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/out/parser/string/shift/operator/bounds.js +106 -0
- package/out/parser/string/shift/operator/bounds.js.map +1 -0
- package/out/parser/string/shift/operator/divisor.d.ts +9 -0
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/out/parser/string/shift/operator/divisor.js +13 -0
- package/out/parser/string/shift/operator/divisor.js.map +1 -0
- package/out/parser/string/shift/operator/operator.d.ts +13 -0
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/out/parser/string/shift/operator/operator.js +29 -0
- package/out/parser/string/shift/operator/operator.js.map +1 -0
- package/out/parser/string/shift/scanner.d.ts +71 -0
- package/out/parser/string/shift/scanner.d.ts.map +1 -0
- package/out/parser/string/shift/scanner.js +99 -0
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/out/parser/string/string.d.ts +24 -0
- package/out/parser/string/string.d.ts.map +1 -0
- package/out/parser/string/string.js +23 -0
- package/out/parser/string/string.js.map +1 -0
- package/out/parser/tuple.d.ts +71 -0
- package/out/parser/tuple.d.ts.map +1 -0
- package/out/parser/tuple.js +137 -0
- package/out/parser/tuple.js.map +1 -0
- package/out/scope.d.ts +159 -0
- package/out/scope.d.ts.map +1 -0
- package/out/scope.js +253 -0
- package/out/scope.js.map +1 -0
- package/out/test.d.ts +2 -0
- package/out/test.d.ts.map +1 -0
- package/out/test.js +7 -0
- package/out/test.js.map +1 -0
- package/out/tsconfig.build.tsbuildinfo +1 -0
- package/out/type.d.ts +77 -0
- package/out/type.d.ts.map +1 -0
- package/out/type.js +134 -0
- package/out/type.js.map +1 -0
- package/out/util.d.ts +19 -0
- package/out/util.d.ts.map +1 -0
- package/out/util.js +7 -0
- package/out/util.js.map +1 -0
- package/package.json +13 -10
- package/parser/definition.ts +159 -0
- package/parser/generic.ts +68 -0
- package/parser/objectLiteral.ts +197 -0
- package/parser/semantic/bounds.ts +61 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +46 -0
- package/parser/semantic/semantic.ts +144 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +109 -0
- package/parser/shared.ts +152 -0
- package/parser/string/reduce/dynamic.ts +196 -0
- package/parser/string/reduce/shared.ts +101 -0
- package/parser/string/reduce/static.ts +298 -0
- package/parser/string/shift/operand/date.ts +54 -0
- package/parser/string/shift/operand/enclosed.ts +118 -0
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +268 -0
- package/parser/string/shift/operator/bounds.ts +264 -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 +90 -0
- package/parser/tuple.ts +474 -0
- package/scope.ts +574 -0
- package/type.ts +347 -0
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -1018
- package/dist/main.js +0 -1450
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import type { Completion, defined, ErrorMessage } from "@arktype/util"
|
|
2
|
+
import type { Scanner } from "../shift/scanner.js"
|
|
3
|
+
import type {
|
|
4
|
+
Comparator,
|
|
5
|
+
InvertedComparators,
|
|
6
|
+
LimitLiteral,
|
|
7
|
+
MaxComparator,
|
|
8
|
+
MinComparator,
|
|
9
|
+
OpenLeftBound,
|
|
10
|
+
StringifiablePrefixOperator,
|
|
11
|
+
writeMultipleLeftBoundsMessage,
|
|
12
|
+
writeOpenRangeMessage,
|
|
13
|
+
writeUnclosedGroupMessage,
|
|
14
|
+
writeUnmatchedGroupCloseMessage,
|
|
15
|
+
writeUnpairableComparatorMessage
|
|
16
|
+
} from "./shared.js"
|
|
17
|
+
|
|
18
|
+
export type StaticState = {
|
|
19
|
+
root: unknown
|
|
20
|
+
branches: BranchState
|
|
21
|
+
groups: BranchState[]
|
|
22
|
+
finalizer: Scanner.FinalizingLookahead | ErrorMessage | undefined
|
|
23
|
+
scanned: string
|
|
24
|
+
unscanned: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type AutocompletePrefix = `${StringifiablePrefixOperator} `
|
|
28
|
+
|
|
29
|
+
type BranchState = {
|
|
30
|
+
prefixes: StringifiablePrefixOperator[]
|
|
31
|
+
leftBound: OpenLeftBound | undefined
|
|
32
|
+
"&": unknown
|
|
33
|
+
"|": unknown
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export namespace state {
|
|
37
|
+
export type initialize<def extends string> = from<{
|
|
38
|
+
root: undefined
|
|
39
|
+
branches: initialBranches
|
|
40
|
+
groups: []
|
|
41
|
+
finalizer: undefined
|
|
42
|
+
scanned: ""
|
|
43
|
+
unscanned: def
|
|
44
|
+
}>
|
|
45
|
+
|
|
46
|
+
export type error<message extends string> = from<{
|
|
47
|
+
root: undefined
|
|
48
|
+
branches: initialBranches
|
|
49
|
+
groups: []
|
|
50
|
+
finalizer: ErrorMessage<message>
|
|
51
|
+
scanned: ""
|
|
52
|
+
unscanned: ""
|
|
53
|
+
}>
|
|
54
|
+
|
|
55
|
+
export type completion<text extends string> = from<{
|
|
56
|
+
root: undefined
|
|
57
|
+
branches: initialBranches
|
|
58
|
+
groups: []
|
|
59
|
+
finalizer: Completion<text>
|
|
60
|
+
scanned: ""
|
|
61
|
+
unscanned: ""
|
|
62
|
+
}>
|
|
63
|
+
|
|
64
|
+
type initialBranches = branchesFrom<{
|
|
65
|
+
prefixes: []
|
|
66
|
+
leftBound: undefined
|
|
67
|
+
"&": undefined
|
|
68
|
+
"|": undefined
|
|
69
|
+
}>
|
|
70
|
+
|
|
71
|
+
type updateScanned<
|
|
72
|
+
previousScanned extends string,
|
|
73
|
+
previousUnscanned extends string,
|
|
74
|
+
updatedUnscanned extends string
|
|
75
|
+
> = previousUnscanned extends `${infer justScanned}${updatedUnscanned}`
|
|
76
|
+
? `${previousScanned}${justScanned}`
|
|
77
|
+
: previousScanned
|
|
78
|
+
|
|
79
|
+
export type setRoot<
|
|
80
|
+
s extends StaticState,
|
|
81
|
+
root,
|
|
82
|
+
unscanned extends string
|
|
83
|
+
> = from<{
|
|
84
|
+
root: root
|
|
85
|
+
branches: s["branches"]
|
|
86
|
+
groups: s["groups"]
|
|
87
|
+
finalizer: s["finalizer"]
|
|
88
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
89
|
+
unscanned: unscanned
|
|
90
|
+
}>
|
|
91
|
+
|
|
92
|
+
export type addPrefix<
|
|
93
|
+
s extends StaticState,
|
|
94
|
+
prefix extends StringifiablePrefixOperator,
|
|
95
|
+
unscanned extends string
|
|
96
|
+
> = from<{
|
|
97
|
+
root: s["root"]
|
|
98
|
+
branches: {
|
|
99
|
+
prefixes: [...s["branches"]["prefixes"], prefix]
|
|
100
|
+
leftBound: s["branches"]["leftBound"]
|
|
101
|
+
"&": s["branches"]["&"]
|
|
102
|
+
"|": s["branches"]["|"]
|
|
103
|
+
}
|
|
104
|
+
groups: s["groups"]
|
|
105
|
+
finalizer: s["finalizer"]
|
|
106
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
107
|
+
unscanned: unscanned
|
|
108
|
+
}>
|
|
109
|
+
|
|
110
|
+
export type reduceBranch<
|
|
111
|
+
s extends StaticState,
|
|
112
|
+
token extends "|" | "&",
|
|
113
|
+
unscanned extends string
|
|
114
|
+
> = s["branches"]["leftBound"] extends {}
|
|
115
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
116
|
+
: from<{
|
|
117
|
+
root: undefined
|
|
118
|
+
branches: {
|
|
119
|
+
prefixes: []
|
|
120
|
+
leftBound: undefined
|
|
121
|
+
"&": token extends "&" ? mergeToIntersection<s> : undefined
|
|
122
|
+
"|": token extends "|" ? mergeToUnion<s> : s["branches"]["|"]
|
|
123
|
+
}
|
|
124
|
+
groups: s["groups"]
|
|
125
|
+
finalizer: s["finalizer"]
|
|
126
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
127
|
+
unscanned: unscanned
|
|
128
|
+
}>
|
|
129
|
+
|
|
130
|
+
export type reduceLeftBound<
|
|
131
|
+
s extends StaticState,
|
|
132
|
+
limit extends LimitLiteral,
|
|
133
|
+
comparator extends Comparator,
|
|
134
|
+
unscanned extends string
|
|
135
|
+
> = comparator extends "<" | "<="
|
|
136
|
+
? s["branches"]["leftBound"] extends {}
|
|
137
|
+
? state.error<
|
|
138
|
+
writeMultipleLeftBoundsMessage<
|
|
139
|
+
s["branches"]["leftBound"]["limit"],
|
|
140
|
+
s["branches"]["leftBound"]["comparator"],
|
|
141
|
+
limit,
|
|
142
|
+
InvertedComparators[comparator]
|
|
143
|
+
>
|
|
144
|
+
>
|
|
145
|
+
: from<{
|
|
146
|
+
root: undefined
|
|
147
|
+
branches: {
|
|
148
|
+
prefixes: s["branches"]["prefixes"]
|
|
149
|
+
leftBound: {
|
|
150
|
+
limit: limit
|
|
151
|
+
comparator: InvertedComparators[comparator]
|
|
152
|
+
}
|
|
153
|
+
"&": s["branches"]["&"]
|
|
154
|
+
"|": s["branches"]["|"]
|
|
155
|
+
}
|
|
156
|
+
groups: s["groups"]
|
|
157
|
+
finalizer: s["finalizer"]
|
|
158
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
159
|
+
unscanned: unscanned
|
|
160
|
+
}>
|
|
161
|
+
: state.error<writeUnpairableComparatorMessage<comparator>>
|
|
162
|
+
|
|
163
|
+
export type reduceRange<
|
|
164
|
+
s extends StaticState,
|
|
165
|
+
minLimit extends LimitLiteral,
|
|
166
|
+
minComparator extends MinComparator,
|
|
167
|
+
maxComparator extends MaxComparator,
|
|
168
|
+
maxLimit extends LimitLiteral,
|
|
169
|
+
unscanned extends string
|
|
170
|
+
> = state.from<{
|
|
171
|
+
root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]]
|
|
172
|
+
branches: {
|
|
173
|
+
prefixes: s["branches"]["prefixes"]
|
|
174
|
+
leftBound: undefined
|
|
175
|
+
"&": s["branches"]["&"]
|
|
176
|
+
"|": s["branches"]["|"]
|
|
177
|
+
}
|
|
178
|
+
groups: s["groups"]
|
|
179
|
+
finalizer: s["finalizer"]
|
|
180
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
181
|
+
unscanned: unscanned
|
|
182
|
+
}>
|
|
183
|
+
|
|
184
|
+
export type reduceSingleBound<
|
|
185
|
+
s extends StaticState,
|
|
186
|
+
comparator extends Comparator,
|
|
187
|
+
limit extends LimitLiteral,
|
|
188
|
+
unscanned extends string
|
|
189
|
+
> = state.from<{
|
|
190
|
+
root: [s["root"], comparator, limit]
|
|
191
|
+
branches: {
|
|
192
|
+
prefixes: s["branches"]["prefixes"]
|
|
193
|
+
leftBound: undefined
|
|
194
|
+
"&": s["branches"]["&"]
|
|
195
|
+
"|": s["branches"]["|"]
|
|
196
|
+
}
|
|
197
|
+
groups: s["groups"]
|
|
198
|
+
finalizer: s["finalizer"]
|
|
199
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
200
|
+
unscanned: unscanned
|
|
201
|
+
}>
|
|
202
|
+
|
|
203
|
+
type mergeToUnion<s extends StaticState> =
|
|
204
|
+
s["branches"]["|"] extends undefined
|
|
205
|
+
? mergeToIntersection<s>
|
|
206
|
+
: [s["branches"]["|"], "|", mergeToIntersection<s>]
|
|
207
|
+
|
|
208
|
+
type mergeToIntersection<s extends StaticState> =
|
|
209
|
+
s["branches"]["&"] extends undefined
|
|
210
|
+
? mergePrefixes<s>
|
|
211
|
+
: [s["branches"]["&"], "&", mergePrefixes<s>]
|
|
212
|
+
|
|
213
|
+
type mergePrefixes<
|
|
214
|
+
s extends StaticState,
|
|
215
|
+
remaining extends unknown[] = s["branches"]["prefixes"]
|
|
216
|
+
> = remaining extends [infer head, ...infer tail]
|
|
217
|
+
? [head, mergePrefixes<s, tail>]
|
|
218
|
+
: s["root"]
|
|
219
|
+
|
|
220
|
+
type popGroup<stack extends BranchState[], top extends BranchState> = [
|
|
221
|
+
...stack,
|
|
222
|
+
top
|
|
223
|
+
]
|
|
224
|
+
|
|
225
|
+
export type finalizeGroup<
|
|
226
|
+
s extends StaticState,
|
|
227
|
+
unscanned extends string
|
|
228
|
+
> = s["branches"]["leftBound"] extends {}
|
|
229
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
230
|
+
: s["groups"] extends popGroup<infer stack, infer top>
|
|
231
|
+
? from<{
|
|
232
|
+
groups: stack
|
|
233
|
+
branches: top
|
|
234
|
+
root: mergeToUnion<s>
|
|
235
|
+
finalizer: s["finalizer"]
|
|
236
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
237
|
+
unscanned: unscanned
|
|
238
|
+
}>
|
|
239
|
+
: state.error<writeUnmatchedGroupCloseMessage<unscanned>>
|
|
240
|
+
|
|
241
|
+
export type reduceGroupOpen<
|
|
242
|
+
s extends StaticState,
|
|
243
|
+
unscanned extends string
|
|
244
|
+
> = from<{
|
|
245
|
+
groups: [...s["groups"], s["branches"]]
|
|
246
|
+
branches: initialBranches
|
|
247
|
+
root: undefined
|
|
248
|
+
finalizer: s["finalizer"]
|
|
249
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
250
|
+
unscanned: unscanned
|
|
251
|
+
}>
|
|
252
|
+
|
|
253
|
+
export type finalize<
|
|
254
|
+
s extends StaticState,
|
|
255
|
+
finalizer extends Scanner.FinalizingLookahead
|
|
256
|
+
> = s["groups"] extends []
|
|
257
|
+
? s["branches"]["leftBound"] extends {}
|
|
258
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
259
|
+
: from<{
|
|
260
|
+
root: mergeToUnion<s>
|
|
261
|
+
groups: s["groups"]
|
|
262
|
+
branches: initialBranches
|
|
263
|
+
finalizer: finalizer
|
|
264
|
+
scanned: s["scanned"]
|
|
265
|
+
unscanned: s["unscanned"]
|
|
266
|
+
}>
|
|
267
|
+
: state.error<writeUnclosedGroupMessage<")">>
|
|
268
|
+
|
|
269
|
+
type openRangeError<range extends defined<BranchState["leftBound"]>> =
|
|
270
|
+
state.error<writeOpenRangeMessage<range["limit"], range["comparator"]>>
|
|
271
|
+
|
|
272
|
+
export type previousOperator<s extends StaticState> =
|
|
273
|
+
s["branches"]["leftBound"] extends {}
|
|
274
|
+
? s["branches"]["leftBound"]["comparator"]
|
|
275
|
+
: s["branches"]["prefixes"] extends [
|
|
276
|
+
...unknown[],
|
|
277
|
+
infer tail extends string
|
|
278
|
+
]
|
|
279
|
+
? tail
|
|
280
|
+
: s["branches"]["&"] extends {}
|
|
281
|
+
? "&"
|
|
282
|
+
: s["branches"]["|"] extends {}
|
|
283
|
+
? "|"
|
|
284
|
+
: undefined
|
|
285
|
+
|
|
286
|
+
export type scanTo<s extends StaticState, unscanned extends string> = from<{
|
|
287
|
+
root: s["root"]
|
|
288
|
+
branches: s["branches"]
|
|
289
|
+
groups: s["groups"]
|
|
290
|
+
finalizer: s["finalizer"]
|
|
291
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
292
|
+
unscanned: unscanned
|
|
293
|
+
}>
|
|
294
|
+
|
|
295
|
+
export type from<s extends StaticState> = s
|
|
296
|
+
|
|
297
|
+
export type branchesFrom<b extends BranchState> = b
|
|
298
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { DateLiteral } from "@arktype/schema"
|
|
2
|
+
import { throwParseError, tryParseNumber } from "@arktype/util"
|
|
3
|
+
|
|
4
|
+
export const isDateLiteral = (value: unknown): value is DateLiteral =>
|
|
5
|
+
typeof value === "string" &&
|
|
6
|
+
value[0] === "d" &&
|
|
7
|
+
(value[1] === "'" || value[1] === '"') &&
|
|
8
|
+
value.at(-1) === value[1]
|
|
9
|
+
|
|
10
|
+
export const isValidDate = (d: Date) => d.toString() !== "Invalid Date"
|
|
11
|
+
|
|
12
|
+
export const extractDateLiteralSource = <literal extends DateLiteral>(
|
|
13
|
+
literal: literal
|
|
14
|
+
) => literal.slice(2, -1) as extractDateLiteralSource<literal>
|
|
15
|
+
|
|
16
|
+
type extractDateLiteralSource<literal extends DateLiteral> =
|
|
17
|
+
literal extends DateLiteral<infer source> ? source : never
|
|
18
|
+
|
|
19
|
+
export const writeInvalidDateMessage = <source extends string>(
|
|
20
|
+
source: source
|
|
21
|
+
): writeInvalidDateMessage<source> =>
|
|
22
|
+
`'${source}' could not be parsed by the Date constructor`
|
|
23
|
+
|
|
24
|
+
export type writeInvalidDateMessage<source extends string> =
|
|
25
|
+
`'${source}' could not be parsed by the Date constructor`
|
|
26
|
+
|
|
27
|
+
export type DateInput = ConstructorParameters<typeof Date>[0]
|
|
28
|
+
|
|
29
|
+
export const tryParseDate = <errorOnFail extends boolean | string>(
|
|
30
|
+
source: string,
|
|
31
|
+
errorOnFail?: errorOnFail
|
|
32
|
+
) => maybeParseDate(source, errorOnFail)
|
|
33
|
+
|
|
34
|
+
const maybeParseDate = <errorOnFail extends boolean | string>(
|
|
35
|
+
source: string,
|
|
36
|
+
errorOnFail?: errorOnFail
|
|
37
|
+
): Date | (errorOnFail extends true | string ? never : undefined) => {
|
|
38
|
+
const stringParsedDate = new Date(source)
|
|
39
|
+
if (isValidDate(stringParsedDate)) {
|
|
40
|
+
return stringParsedDate
|
|
41
|
+
}
|
|
42
|
+
const epochMillis = tryParseNumber(source)
|
|
43
|
+
if (epochMillis !== undefined) {
|
|
44
|
+
const numberParsedDate = new Date(epochMillis)
|
|
45
|
+
if (isValidDate(numberParsedDate)) {
|
|
46
|
+
return numberParsedDate
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return errorOnFail
|
|
50
|
+
? throwParseError(
|
|
51
|
+
errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
|
|
52
|
+
)
|
|
53
|
+
: (undefined as never)
|
|
54
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { schema, type RegexLiteral } from "@arktype/schema"
|
|
2
|
+
import { isKeyOf } from "@arktype/util"
|
|
3
|
+
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
4
|
+
import type { StaticState, state } from "../../reduce/static.js"
|
|
5
|
+
import type { Scanner } from "../scanner.js"
|
|
6
|
+
import { tryParseDate, writeInvalidDateMessage } from "./date.js"
|
|
7
|
+
|
|
8
|
+
export type StringLiteral<Text extends string = string> =
|
|
9
|
+
| DoubleQuotedStringLiteral<Text>
|
|
10
|
+
| SingleQuotedStringLiteral<Text>
|
|
11
|
+
|
|
12
|
+
export type DoubleQuotedStringLiteral<Text extends string = string> =
|
|
13
|
+
`"${Text}"`
|
|
14
|
+
|
|
15
|
+
export type SingleQuotedStringLiteral<Text extends string = string> =
|
|
16
|
+
`'${Text}'`
|
|
17
|
+
|
|
18
|
+
export const parseEnclosed = (
|
|
19
|
+
s: DynamicState,
|
|
20
|
+
enclosing: EnclosingStartToken
|
|
21
|
+
) => {
|
|
22
|
+
const enclosed = s.scanner.shiftUntil(
|
|
23
|
+
untilLookaheadIsClosing[enclosingTokens[enclosing]]
|
|
24
|
+
)
|
|
25
|
+
if (s.scanner.lookahead === "") {
|
|
26
|
+
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
|
|
27
|
+
}
|
|
28
|
+
// Shift the scanner one additional time for the second enclosing token
|
|
29
|
+
const token = `${enclosing}${enclosed}${s.scanner.shift()}`
|
|
30
|
+
if (enclosing === "/") {
|
|
31
|
+
// fail parsing if the regex is invalid
|
|
32
|
+
new RegExp(enclosed)
|
|
33
|
+
s.root = schema({ basis: "string", pattern: token as RegexLiteral })
|
|
34
|
+
} else if (isKeyOf(enclosing, enclosingQuote)) {
|
|
35
|
+
s.root = schema({ unit: enclosed })
|
|
36
|
+
} else {
|
|
37
|
+
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
|
|
38
|
+
s.root = schema({ unit: date, description: token })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type parseEnclosed<
|
|
43
|
+
s extends StaticState,
|
|
44
|
+
enclosingStart extends EnclosingStartToken,
|
|
45
|
+
unscanned extends string
|
|
46
|
+
> = Scanner.shiftUntil<
|
|
47
|
+
unscanned,
|
|
48
|
+
EnclosingTokens[enclosingStart]
|
|
49
|
+
> extends Scanner.shiftResult<infer scanned, infer nextUnscanned>
|
|
50
|
+
? nextUnscanned extends ""
|
|
51
|
+
? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
|
|
52
|
+
: state.setRoot<
|
|
53
|
+
s,
|
|
54
|
+
`${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`,
|
|
55
|
+
nextUnscanned extends Scanner.shift<string, infer unscanned>
|
|
56
|
+
? unscanned
|
|
57
|
+
: ""
|
|
58
|
+
>
|
|
59
|
+
: never
|
|
60
|
+
|
|
61
|
+
export const enclosingQuote = {
|
|
62
|
+
"'": 1,
|
|
63
|
+
'"': 1
|
|
64
|
+
} as const
|
|
65
|
+
|
|
66
|
+
export type EnclosingQuote = keyof typeof enclosingQuote
|
|
67
|
+
|
|
68
|
+
export const enclosingChar = {
|
|
69
|
+
"/": 1,
|
|
70
|
+
...enclosingQuote
|
|
71
|
+
} as const
|
|
72
|
+
|
|
73
|
+
export const enclosingTokens = {
|
|
74
|
+
"d'": "'",
|
|
75
|
+
'd"': '"',
|
|
76
|
+
"'": "'",
|
|
77
|
+
'"': '"',
|
|
78
|
+
"/": "/"
|
|
79
|
+
} as const
|
|
80
|
+
|
|
81
|
+
export type EnclosingTokens = typeof enclosingTokens
|
|
82
|
+
|
|
83
|
+
export type EnclosingStartToken = keyof EnclosingTokens
|
|
84
|
+
|
|
85
|
+
export type EnclosingEndToken = EnclosingTokens[keyof EnclosingTokens]
|
|
86
|
+
|
|
87
|
+
export const untilLookaheadIsClosing: Record<
|
|
88
|
+
EnclosingEndToken,
|
|
89
|
+
Scanner.UntilCondition
|
|
90
|
+
> = {
|
|
91
|
+
"'": (scanner) => scanner.lookahead === `'`,
|
|
92
|
+
'"': (scanner) => scanner.lookahead === `"`,
|
|
93
|
+
"/": (scanner) => scanner.lookahead === `/`
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const enclosingCharDescriptions = {
|
|
97
|
+
'"': "double-quote",
|
|
98
|
+
"'": "single-quote",
|
|
99
|
+
"/": "forward slash"
|
|
100
|
+
} as const
|
|
101
|
+
|
|
102
|
+
type enclosingCharDescriptions = typeof enclosingCharDescriptions
|
|
103
|
+
|
|
104
|
+
export const writeUnterminatedEnclosedMessage = <
|
|
105
|
+
fragment extends string,
|
|
106
|
+
enclosingStart extends EnclosingStartToken
|
|
107
|
+
>(
|
|
108
|
+
fragment: fragment,
|
|
109
|
+
enclosingStart: enclosingStart
|
|
110
|
+
): writeUnterminatedEnclosedMessage<fragment, enclosingStart> =>
|
|
111
|
+
`${enclosingStart}${fragment} requires a closing ${
|
|
112
|
+
enclosingCharDescriptions[enclosingTokens[enclosingStart]]
|
|
113
|
+
}`
|
|
114
|
+
|
|
115
|
+
export type writeUnterminatedEnclosedMessage<
|
|
116
|
+
fragment extends string,
|
|
117
|
+
enclosingStart extends EnclosingStartToken
|
|
118
|
+
> = `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[EnclosingTokens[enclosingStart]]}`
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { TypeNode } from "@arktype/schema"
|
|
2
|
+
import type { ErrorMessage, join } from "@arktype/util"
|
|
3
|
+
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
4
|
+
import { writeUnclosedGroupMessage } from "../../reduce/shared.js"
|
|
5
|
+
import type { StaticState, state } from "../../reduce/static.js"
|
|
6
|
+
import type { parseUntilFinalizer } from "../../string.js"
|
|
7
|
+
|
|
8
|
+
export type ParsedArgs<
|
|
9
|
+
result extends unknown[] = unknown[],
|
|
10
|
+
unscanned extends string = string
|
|
11
|
+
> = {
|
|
12
|
+
result: result
|
|
13
|
+
unscanned: unscanned
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const parseGenericArgs = (
|
|
17
|
+
name: string,
|
|
18
|
+
params: string[],
|
|
19
|
+
s: DynamicState
|
|
20
|
+
) => parseGenericArgsRecurse(name, params, s, [], [])
|
|
21
|
+
|
|
22
|
+
export type parseGenericArgs<
|
|
23
|
+
name extends string,
|
|
24
|
+
params extends string[],
|
|
25
|
+
unscanned extends string,
|
|
26
|
+
$,
|
|
27
|
+
args
|
|
28
|
+
> = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
|
|
29
|
+
|
|
30
|
+
const parseGenericArgsRecurse = (
|
|
31
|
+
name: string,
|
|
32
|
+
params: string[],
|
|
33
|
+
s: DynamicState,
|
|
34
|
+
argDefs: string[],
|
|
35
|
+
argNodes: TypeNode[]
|
|
36
|
+
): ParsedArgs<TypeNode[]> => {
|
|
37
|
+
const argState = s.parseUntilFinalizer()
|
|
38
|
+
// remove the finalizing token from the argDef
|
|
39
|
+
argDefs.push(argState.scanner.scanned.slice(0, -1))
|
|
40
|
+
argNodes.push(argState.root)
|
|
41
|
+
if (argState.finalizer === ">") {
|
|
42
|
+
if (argNodes.length === params.length) {
|
|
43
|
+
return {
|
|
44
|
+
result: argNodes,
|
|
45
|
+
unscanned: argState.scanner.unscanned
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
return argState.error(
|
|
49
|
+
writeInvalidGenericArgsMessage(name, params, argDefs)
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
} else if (argState.finalizer === ",") {
|
|
53
|
+
return parseGenericArgsRecurse(name, params, s, argDefs, argNodes)
|
|
54
|
+
}
|
|
55
|
+
return argState.error(writeUnclosedGroupMessage(">"))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type parseGenericArgsRecurse<
|
|
59
|
+
name extends string,
|
|
60
|
+
params extends string[],
|
|
61
|
+
unscanned extends string,
|
|
62
|
+
$,
|
|
63
|
+
args,
|
|
64
|
+
argDefs extends string[],
|
|
65
|
+
argAsts extends unknown[]
|
|
66
|
+
> = parseUntilFinalizer<
|
|
67
|
+
state.initialize<unscanned>,
|
|
68
|
+
$,
|
|
69
|
+
args
|
|
70
|
+
> extends infer finalArgState extends StaticState
|
|
71
|
+
? {
|
|
72
|
+
defs: [
|
|
73
|
+
...argDefs,
|
|
74
|
+
finalArgState["scanned"] extends `${infer def}${"," | ">"}`
|
|
75
|
+
? def
|
|
76
|
+
: finalArgState["scanned"]
|
|
77
|
+
]
|
|
78
|
+
asts: [...argAsts, finalArgState["root"]]
|
|
79
|
+
unscanned: finalArgState["unscanned"]
|
|
80
|
+
} extends {
|
|
81
|
+
defs: infer nextDefs extends string[]
|
|
82
|
+
asts: infer nextAsts extends unknown[]
|
|
83
|
+
unscanned: infer nextUnscanned extends string
|
|
84
|
+
}
|
|
85
|
+
? finalArgState["finalizer"] extends ">"
|
|
86
|
+
? nextAsts["length"] extends params["length"]
|
|
87
|
+
? ParsedArgs<nextAsts, nextUnscanned>
|
|
88
|
+
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
89
|
+
: finalArgState["finalizer"] extends ","
|
|
90
|
+
? parseGenericArgsRecurse<
|
|
91
|
+
name,
|
|
92
|
+
params,
|
|
93
|
+
nextUnscanned,
|
|
94
|
+
$,
|
|
95
|
+
args,
|
|
96
|
+
nextDefs,
|
|
97
|
+
nextAsts
|
|
98
|
+
>
|
|
99
|
+
: finalArgState["finalizer"] extends ErrorMessage
|
|
100
|
+
? finalArgState
|
|
101
|
+
: state.error<writeUnclosedGroupMessage<">">>
|
|
102
|
+
: never
|
|
103
|
+
: never
|
|
104
|
+
|
|
105
|
+
export const writeInvalidGenericArgsMessage = <
|
|
106
|
+
name extends string,
|
|
107
|
+
params extends string[],
|
|
108
|
+
argDefs extends string[]
|
|
109
|
+
>(
|
|
110
|
+
name: name,
|
|
111
|
+
params: params,
|
|
112
|
+
argDefs: argDefs
|
|
113
|
+
) =>
|
|
114
|
+
`${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
|
|
115
|
+
argDefs.length
|
|
116
|
+
}${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
|
|
117
|
+
|
|
118
|
+
export type writeInvalidGenericArgsMessage<
|
|
119
|
+
name extends string,
|
|
120
|
+
params extends string[],
|
|
121
|
+
argDefs extends string[]
|
|
122
|
+
> = `${name}<${join<
|
|
123
|
+
params,
|
|
124
|
+
", "
|
|
125
|
+
>}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
|
|
126
|
+
? ""
|
|
127
|
+
: `: ${join<argDefs, ",">}`})`
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
2
|
+
import type { state, StaticState } from "../../reduce/static.js"
|
|
3
|
+
import type { BaseCompletions } from "../../string.js"
|
|
4
|
+
import { Scanner } from "../scanner.js"
|
|
5
|
+
import {
|
|
6
|
+
enclosingChar,
|
|
7
|
+
parseEnclosed,
|
|
8
|
+
type EnclosingEndToken,
|
|
9
|
+
type EnclosingQuote,
|
|
10
|
+
type EnclosingStartToken
|
|
11
|
+
} from "./enclosed.js"
|
|
12
|
+
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js"
|
|
13
|
+
|
|
14
|
+
export const parseOperand = (s: DynamicState): void =>
|
|
15
|
+
s.scanner.lookahead === ""
|
|
16
|
+
? s.error(writeMissingOperandMessage(s))
|
|
17
|
+
: s.scanner.lookahead === "("
|
|
18
|
+
? s.shiftedByOne().reduceGroupOpen()
|
|
19
|
+
: s.scanner.lookaheadIsIn(enclosingChar)
|
|
20
|
+
? parseEnclosed(s, s.scanner.shift())
|
|
21
|
+
: s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens)
|
|
22
|
+
? parseOperand(s.shiftedByOne())
|
|
23
|
+
: s.scanner.lookahead === "d"
|
|
24
|
+
? s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar)
|
|
25
|
+
? parseEnclosed(s, `d${s.scanner.shift()}` as EnclosingStartToken)
|
|
26
|
+
: parseUnenclosed(s)
|
|
27
|
+
: parseUnenclosed(s)
|
|
28
|
+
|
|
29
|
+
export type parseOperand<
|
|
30
|
+
s extends StaticState,
|
|
31
|
+
$,
|
|
32
|
+
args
|
|
33
|
+
> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
|
|
34
|
+
? lookahead extends "("
|
|
35
|
+
? state.reduceGroupOpen<s, unscanned>
|
|
36
|
+
: lookahead extends EnclosingEndToken
|
|
37
|
+
? parseEnclosed<s, lookahead, unscanned>
|
|
38
|
+
: lookahead extends Scanner.WhiteSpaceToken
|
|
39
|
+
? parseOperand<state.scanTo<s, unscanned>, $, args>
|
|
40
|
+
: lookahead extends "d"
|
|
41
|
+
? unscanned extends Scanner.shift<
|
|
42
|
+
infer enclosing extends EnclosingQuote,
|
|
43
|
+
infer nextUnscanned
|
|
44
|
+
>
|
|
45
|
+
? parseEnclosed<s, `d${enclosing}`, nextUnscanned>
|
|
46
|
+
: parseUnenclosed<s, $, args>
|
|
47
|
+
: parseUnenclosed<s, $, args>
|
|
48
|
+
: state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>
|