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