arktype 2.0.0-dev.2 → 2.0.0-dev.20

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.
Files changed (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
@@ -1,268 +0,0 @@
1
- import { BaseType, schema, type TypeNode } from "@arktype/schema"
2
- import {
3
- printable,
4
- throwParseError,
5
- tryParseNumber,
6
- tryParseWellFormedBigint,
7
- type BigintLiteral,
8
- type Completion,
9
- type ErrorMessage,
10
- type join,
11
- type NumberLiteral
12
- } from "@arktype/util"
13
- import type { Module } from "../../../../scope.js"
14
- import type { Generic, GenericProps } from "../../../../type.js"
15
- import { hasArkKind } from "../../../../util.js"
16
- import type { GenericInstantiationAst } from "../../../semantic/semantic.js"
17
- import type { DynamicState } from "../../reduce/dynamic.js"
18
- import type { state, StaticState } from "../../reduce/static.js"
19
- import type { BaseCompletions } from "../../string.js"
20
- import type { Scanner } from "../scanner.js"
21
- import {
22
- parseGenericArgs,
23
- writeInvalidGenericArgsMessage,
24
- type ParsedArgs
25
- } from "./genericArgs.js"
26
-
27
- export const parseUnenclosed = (s: DynamicState) => {
28
- const token = s.scanner.shiftUntilNextTerminator()
29
- if (token === "keyof") {
30
- s.addPrefix("keyof")
31
- } else {
32
- s.root = unenclosedToNode(s, token)
33
- }
34
- }
35
-
36
- export type parseUnenclosed<
37
- s extends StaticState,
38
- $,
39
- args
40
- > = Scanner.shiftUntilNextTerminator<
41
- s["unscanned"]
42
- > extends Scanner.shiftResult<infer token, infer unscanned>
43
- ? token extends "keyof"
44
- ? state.addPrefix<s, "keyof", unscanned>
45
- : tryResolve<s, token, $, args> extends infer result
46
- ? result extends ErrorMessage<infer message>
47
- ? state.error<message>
48
- : result extends keyof $
49
- ? $[result] extends GenericProps
50
- ? parseGenericInstantiation<
51
- token,
52
- $[result],
53
- state.scanTo<s, unscanned>,
54
- $,
55
- args
56
- >
57
- : state.setRoot<s, result, unscanned>
58
- : state.setRoot<s, result, unscanned>
59
- : never
60
- : never
61
-
62
- export const parseGenericInstantiation = (
63
- name: string,
64
- g: Generic,
65
- s: DynamicState
66
- ): TypeNode => {
67
- s.scanner.shiftUntilNonWhitespace()
68
- const lookahead = s.scanner.shift()
69
- if (lookahead !== "<") {
70
- return s.error(writeInvalidGenericArgsMessage(name, g.parameters, []))
71
- }
72
- const parsedArgs = parseGenericArgs(name, g.parameters, s)
73
- const remainingChars = parsedArgs.unscanned.length
74
- // set the scanner position to where the args scanner left off
75
- s.scanner.jumpToIndex(
76
- remainingChars === 0 ? s.scanner.length : -remainingChars
77
- )
78
- return g(...parsedArgs.result).root as never
79
- }
80
-
81
- export type parseGenericInstantiation<
82
- name extends string,
83
- g extends GenericProps,
84
- s extends StaticState,
85
- $,
86
- args
87
- // have to skip whitespace here since TS allows instantiations like `Partial <T>`
88
- > = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}`
89
- ? parseGenericArgs<
90
- name,
91
- g["parameters"],
92
- unscanned,
93
- $,
94
- args
95
- > extends infer result
96
- ? result extends ParsedArgs<infer argAsts, infer nextUnscanned>
97
- ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned>
98
- : // propagate error
99
- result
100
- : never
101
- : state.error<writeInvalidGenericArgsMessage<name, g["parameters"], []>>
102
-
103
- const unenclosedToNode = (s: DynamicState, token: string): TypeNode =>
104
- maybeParseReference(s, token) ??
105
- maybeParseUnenclosedLiteral(s, token) ??
106
- s.error(
107
- token === ""
108
- ? writeMissingOperandMessage(s)
109
- : writeUnresolvableMessage(token)
110
- )
111
-
112
- const maybeParseReference = (
113
- s: DynamicState,
114
- token: string
115
- ): TypeNode | undefined => {
116
- if (s.ctx.args?.[token]) {
117
- return s.ctx.args[token]
118
- }
119
- const resolution = s.ctx.scope.maybeResolve(token)
120
- if (resolution instanceof BaseType) {
121
- return resolution
122
- }
123
- if (resolution === undefined) {
124
- return
125
- }
126
- if (hasArkKind(resolution, "generic")) {
127
- return parseGenericInstantiation(token, resolution, s)
128
- }
129
- return throwParseError(`Unexpected resolution ${printable(resolution)}`)
130
- }
131
-
132
- const maybeParseUnenclosedLiteral = (
133
- s: DynamicState,
134
- token: string
135
- ): TypeNode | undefined => {
136
- const maybeNumber = tryParseNumber(token, { strict: true })
137
- if (maybeNumber !== undefined) {
138
- return schema({ unit: maybeNumber })
139
- }
140
- const maybeBigint = tryParseWellFormedBigint(token)
141
- if (maybeBigint !== undefined) {
142
- return schema({ unit: maybeBigint })
143
- }
144
- }
145
-
146
- type tryResolve<
147
- s extends StaticState,
148
- token extends string,
149
- $,
150
- args
151
- > = token extends keyof $
152
- ? token
153
- : token extends keyof args
154
- ? token
155
- : token extends NumberLiteral
156
- ? token
157
- : token extends BigintLiteral
158
- ? token
159
- : token extends `${infer submodule extends keyof $ &
160
- string}.${infer reference}`
161
- ? $[submodule] extends Module<infer r>
162
- ? reference extends keyof r["exports"]
163
- ? token
164
- : unknown extends r["exports"]
165
- ? // not sure why I need the additional check here, but for now TS seems to
166
- // hit this branch for a non-scope dot access rather than failing
167
- // initially when we try to infer r. if this can be removed without breaking
168
- // any submodule test cases, do it!
169
- ErrorMessage<writeNonSubmoduleDotMessage<submodule>>
170
- : unresolvableError<s, reference, $[submodule], args, [submodule]>
171
- : ErrorMessage<writeNonSubmoduleDotMessage<submodule>>
172
- : unresolvableError<s, token, $, args, []>
173
-
174
- export const writeNonSubmoduleDotMessage = <name extends string>(
175
- name: name
176
- ): writeNonSubmoduleDotMessage<name> =>
177
- `'${name}' must reference a module to be accessed using dot syntax`
178
-
179
- type writeNonSubmoduleDotMessage<name extends string> =
180
- `'${name}' must reference a module to be accessed using dot syntax`
181
-
182
- export const writeMissingSubmoduleAccessMessage = <name extends string>(
183
- name: name
184
- ): writeMissingSubmoduleAccessMessage<name> =>
185
- `Reference to submodule '${name}' must specify an alias`
186
-
187
- export type writeMissingSubmoduleAccessMessage<name extends string> =
188
- `Reference to submodule '${name}' must specify an alias`
189
-
190
- /** Provide valid completions for the current token, or fallback to an
191
- * unresolvable error if there are none */
192
- export type unresolvableError<
193
- s extends StaticState,
194
- token extends string,
195
- $,
196
- args,
197
- submodulePath extends string[]
198
- > = validReferenceFromToken<token, $, args, submodulePath> extends never
199
- ? ErrorMessage<
200
- writeUnresolvableMessage<qualifiedReference<token, submodulePath>>
201
- >
202
- : Completion<`${s["scanned"]}${qualifiedReference<
203
- validReferenceFromToken<token, $, args, submodulePath>,
204
- submodulePath
205
- >}`>
206
-
207
- type qualifiedReference<
208
- reference extends string,
209
- submodulePath extends string[]
210
- > = join<[...submodulePath, reference], ".">
211
-
212
- type validReferenceFromToken<
213
- token extends string,
214
- $,
215
- args,
216
- submodulePath extends string[]
217
- > = Extract<
218
- submodulePath extends [] ? BaseCompletions<$, args> : keyof $,
219
- `${token}${string}`
220
- >
221
-
222
- export const writeUnresolvableMessage = <token extends string>(
223
- token: token
224
- ): writeUnresolvableMessage<token> => `'${token}' is unresolvable`
225
-
226
- type writeUnresolvableMessage<token extends string> =
227
- `'${token}' is unresolvable`
228
-
229
- export const writeMissingOperandMessage = <s extends DynamicState>(s: s) => {
230
- const operator = s.previousOperator()
231
- return operator
232
- ? writeMissingRightOperandMessage(operator, s.scanner.unscanned)
233
- : writeExpressionExpectedMessage(s.scanner.unscanned)
234
- }
235
-
236
- export type writeMissingOperandMessage<
237
- s extends StaticState,
238
- operator extends string | undefined = state.previousOperator<s>
239
- > = operator extends string
240
- ? writeMissingRightOperandMessage<operator, s["unscanned"]>
241
- : writeExpressionExpectedMessage<s["unscanned"]>
242
-
243
- export type writeMissingRightOperandMessage<
244
- token extends string,
245
- unscanned extends string = ""
246
- > = `Token '${token}' requires a right operand${unscanned extends ""
247
- ? ""
248
- : ` before '${unscanned}'`}`
249
-
250
- export const writeMissingRightOperandMessage = <
251
- token extends string,
252
- unscanned extends string
253
- >(
254
- token: token,
255
- unscanned = "" as unscanned
256
- ): writeMissingRightOperandMessage<token, unscanned> =>
257
- `Token '${token}' requires a right operand${
258
- unscanned ? (` before '${unscanned}'` as any) : ""
259
- }`
260
-
261
- export const writeExpressionExpectedMessage = <unscanned extends string>(
262
- unscanned: unscanned
263
- ) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`
264
-
265
- export type writeExpressionExpectedMessage<unscanned extends string> =
266
- `Expected an expression${unscanned extends ""
267
- ? ""
268
- : ` before '${unscanned}'`}`
@@ -1,264 +0,0 @@
1
- import {
2
- keywords,
3
- type BoundKind,
4
- type LimitSchemaValue,
5
- type Schema,
6
- type TypeNode
7
- } from "@arktype/schema"
8
- import {
9
- isKeyOf,
10
- throwParseError,
11
- tryParseNumber,
12
- type keySet
13
- } from "@arktype/util"
14
- import type { astToString } from "../../../semantic/utils.js"
15
- import type {
16
- DynamicState,
17
- DynamicStateWithRoot
18
- } from "../../reduce/dynamic.js"
19
- import {
20
- maxComparators,
21
- writeUnpairableComparatorMessage,
22
- type Comparator,
23
- type LimitLiteral,
24
- type MaxComparator,
25
- type OpenLeftBound
26
- } from "../../reduce/shared.js"
27
- import type { StaticState, state } from "../../reduce/static.js"
28
- import { extractDateLiteralSource, isDateLiteral } from "../operand/date.js"
29
- import type { parseOperand } from "../operand/operand.js"
30
- import type { Scanner } from "../scanner.js"
31
-
32
- export const parseBound = (
33
- s: DynamicStateWithRoot,
34
- start: ComparatorStartChar
35
- ) => {
36
- const comparator = shiftComparator(s, start)
37
- if (s.root.kind === "unit") {
38
- if (typeof s.root.unit === "number") {
39
- s.unsetRoot()
40
- return s.reduceLeftBound(s.root.unit, comparator)
41
- }
42
- if (s.root.unit instanceof Date) {
43
- s.unsetRoot()
44
- const literal = `d'${
45
- s.root.description ?? s.root.unit.toISOString()
46
- }'` as const
47
- return s.reduceLeftBound(literal, comparator)
48
- }
49
- }
50
- return parseRightBound(s, comparator)
51
- }
52
-
53
- export type parseBound<
54
- s extends StaticState,
55
- start extends ComparatorStartChar,
56
- unscanned extends string,
57
- $,
58
- args
59
- > = shiftComparator<start, unscanned> extends infer shiftResultOrError
60
- ? shiftResultOrError extends Scanner.shiftResult<
61
- infer comparator extends Comparator,
62
- infer nextUnscanned
63
- >
64
- ? s["root"] extends `${infer limit extends LimitLiteral}`
65
- ? state.reduceLeftBound<s, limit, comparator, nextUnscanned>
66
- : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args>
67
- : shiftResultOrError
68
- : never
69
-
70
- const oneCharComparators = {
71
- "<": true,
72
- ">": true
73
- } as const
74
-
75
- type OneCharComparator = keyof typeof oneCharComparators
76
-
77
- export type ComparatorStartChar = Comparator extends `${infer char}${string}`
78
- ? char
79
- : never
80
-
81
- export const comparatorStartChars: keySet<ComparatorStartChar> = {
82
- "<": 1,
83
- ">": 1,
84
- "=": 1
85
- }
86
-
87
- const shiftComparator = (
88
- s: DynamicState,
89
- start: ComparatorStartChar
90
- ): Comparator =>
91
- s.scanner.lookaheadIs("=")
92
- ? `${start}${s.scanner.shift()}`
93
- : isKeyOf(start, oneCharComparators)
94
- ? start
95
- : s.error(singleEqualsMessage)
96
-
97
- type shiftComparator<
98
- start extends ComparatorStartChar,
99
- unscanned extends string
100
- > = unscanned extends `=${infer nextUnscanned}`
101
- ? [`${start}=`, nextUnscanned]
102
- : start extends OneCharComparator
103
- ? [start, unscanned]
104
- : state.error<singleEqualsMessage>
105
-
106
- export const writeUnboundableMessage = <root extends string>(
107
- root: root
108
- ): writeUnboundableMessage<root> =>
109
- `Bounded expression ${root} must be a number, string, Array, or Date`
110
-
111
- export type writeUnboundableMessage<root extends string> =
112
- `Bounded expression ${root} must be a number, string, Array, or Date`
113
-
114
- export const writeIncompatibleRangeMessage = (l: BoundKind, r: BoundKind) =>
115
- `Bound kinds ${l} and ${r} are incompatible`
116
-
117
- export const writeLimitMismatchMessage = (
118
- root: string,
119
- limitValue: LimitSchemaValue
120
- ) => `Limit '${limitValue}' cannot bound ${root}`
121
-
122
- export const getBoundKinds = (
123
- comparator: Comparator,
124
- limit: LimitSchemaValue,
125
- root: TypeNode
126
- ): BoundKind[] => {
127
- if (root.extends(keywords.number)) {
128
- if (typeof limit !== "number") {
129
- return throwParseError(writeLimitMismatchMessage(root.toString(), limit))
130
- }
131
- return comparator === "=="
132
- ? ["min", "max"]
133
- : comparator[0] === ">"
134
- ? ["min"]
135
- : ["max"]
136
- }
137
- if (root.extends(keywords.string) || root.extends(keywords.Array)) {
138
- if (typeof limit !== "number") {
139
- return throwParseError(writeLimitMismatchMessage(root.toString(), limit))
140
- }
141
- return comparator === "=="
142
- ? ["minLength", "maxLength"]
143
- : comparator[0] === ">"
144
- ? ["minLength"]
145
- : ["maxLength"]
146
- }
147
- if (root.extends(keywords.Date)) {
148
- // allow either numeric or date limits
149
- return comparator === "=="
150
- ? ["after", "before"]
151
- : comparator[0] === ">"
152
- ? ["after"]
153
- : ["before"]
154
- }
155
- return throwParseError(writeUnboundableMessage(root.toString()))
156
- }
157
-
158
- export const singleEqualsMessage = `= is not a valid comparator. Use == to check for equality`
159
- type singleEqualsMessage = typeof singleEqualsMessage
160
-
161
- const openLeftBoundToSchema = (
162
- leftBound: OpenLeftBound
163
- ): Schema<BoundKind> => ({
164
- limit: isDateLiteral(leftBound.limit)
165
- ? extractDateLiteralSource(leftBound.limit)
166
- : leftBound.limit,
167
- exclusive: leftBound.comparator.length === 1
168
- })
169
-
170
- export const parseRightBound = (
171
- s: DynamicStateWithRoot,
172
- comparator: Comparator
173
- ) => {
174
- // store the node that will be bounded
175
- const previousRoot = s.unsetRoot()
176
- const previousScannerIndex = s.scanner.location
177
- s.parseOperand()
178
- // after parsing the next operand, use the locations to get the
179
- // token from which it was parsed
180
- const limitToken = s.scanner.sliceChars(
181
- previousScannerIndex,
182
- s.scanner.location
183
- )
184
- s.setRoot(previousRoot)
185
- const limit =
186
- tryParseNumber(limitToken) ??
187
- (isDateLiteral(limitToken)
188
- ? extractDateLiteralSource(limitToken)
189
- : s.error(writeInvalidLimitMessage(comparator, limitToken, "right")))
190
- // apply the newly-parsed right bound
191
- const exclusive = comparator.length === 1
192
- // if the comparator is ==, both the min and max of that pair will be applied
193
- for (const kind of getBoundKinds(comparator, limit, previousRoot)) {
194
- s.constrainRoot(kind, { limit, exclusive })
195
- }
196
- if (!s.branches.leftBound) {
197
- return
198
- }
199
- // if there's an open left bound, perform additional validation and apply it
200
- if (!isKeyOf(comparator, maxComparators)) {
201
- return s.error(writeUnpairableComparatorMessage(comparator))
202
- }
203
- const lowerBoundKind = getBoundKinds(
204
- s.branches.leftBound.comparator,
205
- s.branches.leftBound.limit,
206
- previousRoot
207
- )
208
- s.constrainRoot(
209
- lowerBoundKind[0],
210
- openLeftBoundToSchema(s.branches.leftBound)
211
- )
212
- delete s.branches.leftBound
213
- }
214
-
215
- export type parseRightBound<
216
- s extends StaticState,
217
- comparator extends Comparator,
218
- $,
219
- args
220
- > = parseOperand<s, $, args> extends infer nextState extends StaticState
221
- ? nextState["root"] extends `${infer limit extends LimitLiteral}`
222
- ? s["branches"]["leftBound"] extends {}
223
- ? comparator extends MaxComparator
224
- ? state.reduceRange<
225
- s,
226
- s["branches"]["leftBound"]["limit"],
227
- s["branches"]["leftBound"]["comparator"],
228
- comparator,
229
- limit,
230
- nextState["unscanned"]
231
- >
232
- : state.error<writeUnpairableComparatorMessage<comparator>>
233
- : state.reduceSingleBound<s, comparator, limit, nextState["unscanned"]>
234
- : state.error<
235
- writeInvalidLimitMessage<
236
- comparator,
237
- astToString<nextState["root"]>,
238
- "right"
239
- >
240
- >
241
- : never
242
-
243
- export const writeInvalidLimitMessage = <
244
- comparator extends Comparator,
245
- limit extends string | number,
246
- boundKind extends BoundExpressionKind
247
- >(
248
- comparator: comparator,
249
- limit: limit,
250
- boundKind: boundKind
251
- ): writeInvalidLimitMessage<comparator, limit, boundKind> =>
252
- `Comparator ${comparator} must be ${
253
- boundKind === "left" ? "preceded" : ("followed" as any)
254
- } by a corresponding literal (was '${limit}')`
255
-
256
- export type writeInvalidLimitMessage<
257
- comparator extends Comparator,
258
- limit extends string | number,
259
- boundKind extends BoundExpressionKind
260
- > = `Comparator ${comparator} must be ${boundKind extends "left"
261
- ? "preceded"
262
- : "followed"} by a corresponding literal (was '${limit}')`
263
-
264
- export type BoundExpressionKind = "left" | "right"
@@ -1,36 +0,0 @@
1
- import { tryParseInteger, type NumberLiteral } from "@arktype/util"
2
- import type { DynamicStateWithRoot } from "../../reduce/dynamic.js"
3
- import type { StaticState, state } from "../../reduce/static.js"
4
- import type { Scanner } from "../scanner.js"
5
-
6
- export const parseDivisor = (s: DynamicStateWithRoot) => {
7
- const divisorToken = s.scanner.shiftUntilNextTerminator()
8
- const divisor = tryParseInteger(divisorToken, {
9
- errorOnFail: writeInvalidDivisorMessage(divisorToken)
10
- })
11
- if (divisor === 0) {
12
- s.error(writeInvalidDivisorMessage(0))
13
- }
14
- s.root = s.root.constrain("divisor", divisor)
15
- }
16
-
17
- export type parseDivisor<
18
- s extends StaticState,
19
- unscanned extends string
20
- > = Scanner.shiftUntilNextTerminator<
21
- Scanner.skipWhitespace<unscanned>
22
- > extends Scanner.shiftResult<infer scanned, infer nextUnscanned>
23
- ? scanned extends NumberLiteral<infer divisor>
24
- ? divisor extends 0
25
- ? state.error<writeInvalidDivisorMessage<0>>
26
- : state.setRoot<s, [s["root"], "%", scanned], nextUnscanned>
27
- : state.error<writeInvalidDivisorMessage<scanned>>
28
- : never
29
-
30
- export const writeInvalidDivisorMessage = <divisor extends string | number>(
31
- divisor: divisor
32
- ): writeInvalidDivisorMessage<divisor> =>
33
- `% operator must be followed by a non-zero integer literal (was ${divisor})`
34
-
35
- export type writeInvalidDivisorMessage<divisor extends string | number> =
36
- `% operator must be followed by a non-zero integer literal (was ${divisor})`
@@ -1,82 +0,0 @@
1
- import { isKeyOf } from "@arktype/util"
2
- import type { DynamicStateWithRoot } from "../../reduce/dynamic.js"
3
- import type { state, StaticState } from "../../reduce/static.js"
4
- import { Scanner } from "../scanner.js"
5
- import {
6
- comparatorStartChars,
7
- parseBound,
8
- type ComparatorStartChar
9
- } from "./bounds.js"
10
- import { parseDivisor } from "./divisor.js"
11
-
12
- export const parseOperator = (s: DynamicStateWithRoot): void => {
13
- const lookahead = s.scanner.shift()
14
- return lookahead === ""
15
- ? s.finalize("")
16
- : lookahead === "["
17
- ? s.scanner.shift() === "]"
18
- ? s.setRoot(s.root.array())
19
- : s.error(incompleteArrayTokenMessage)
20
- : lookahead === "|" || lookahead === "&"
21
- ? s.pushRootToBranch(lookahead)
22
- : lookahead === ")"
23
- ? s.finalizeGroup()
24
- : Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned)
25
- ? s.finalize(lookahead)
26
- : isKeyOf(lookahead, comparatorStartChars)
27
- ? parseBound(s, lookahead)
28
- : lookahead === "%"
29
- ? parseDivisor(s)
30
- : lookahead === " "
31
- ? parseOperator(s)
32
- : s.error(writeUnexpectedCharacterMessage(lookahead))
33
- }
34
-
35
- export type parseOperator<
36
- s extends StaticState,
37
- $,
38
- args
39
- > = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
40
- ? lookahead extends "["
41
- ? unscanned extends Scanner.shift<"]", infer nextUnscanned>
42
- ? state.setRoot<s, [s["root"], "[]"], nextUnscanned>
43
- : state.error<incompleteArrayTokenMessage>
44
- : lookahead extends "|" | "&"
45
- ? state.reduceBranch<s, lookahead, unscanned>
46
- : lookahead extends ")"
47
- ? state.finalizeGroup<s, unscanned>
48
- : Scanner.lookaheadIsFinalizing<lookahead, unscanned> extends true
49
- ? state.finalize<
50
- state.scanTo<s, unscanned>,
51
- lookahead & Scanner.FinalizingLookahead
52
- >
53
- : lookahead extends ComparatorStartChar
54
- ? parseBound<s, lookahead, unscanned, $, args>
55
- : lookahead extends "%"
56
- ? parseDivisor<s, unscanned>
57
- : lookahead extends Scanner.WhiteSpaceToken
58
- ? parseOperator<state.scanTo<s, unscanned>, $, args>
59
- : state.error<writeUnexpectedCharacterMessage<lookahead>>
60
- : state.finalize<s, "">
61
-
62
- export const writeUnexpectedCharacterMessage = <
63
- char extends string,
64
- shouldBe extends string
65
- >(
66
- char: char,
67
- shouldBe: shouldBe = "" as shouldBe
68
- ): writeUnexpectedCharacterMessage<char, shouldBe> =>
69
- `'${char}' is not allowed here${
70
- shouldBe && (` (should be ${shouldBe})` as any)
71
- }`
72
-
73
- export type writeUnexpectedCharacterMessage<
74
- char extends string,
75
- shouldBe extends string = ""
76
- > = `'${char}' is not allowed here${shouldBe extends ""
77
- ? ""
78
- : ` (should be ${shouldBe})`}`
79
-
80
- export const incompleteArrayTokenMessage = `Missing expected ']'`
81
-
82
- type incompleteArrayTokenMessage = typeof incompleteArrayTokenMessage