arktype 1.1.0-dev-2 → 2.0.0-beta.1

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 (225) hide show
  1. package/LICENSE +7 -0
  2. package/out/ark.d.ts +19 -0
  3. package/out/ark.js +9 -0
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +65 -0
  7. package/out/generic.js +39 -0
  8. package/out/index.d.ts +6 -0
  9. package/out/index.js +5 -0
  10. package/out/match.d.ts +71 -0
  11. package/out/match.js +50 -0
  12. package/out/module.d.ts +14 -0
  13. package/out/module.js +2 -0
  14. package/{dist → out}/parser/definition.d.ts +16 -16
  15. package/{dist → out}/parser/definition.js +12 -12
  16. package/out/parser/objectLiteral.d.ts +79 -0
  17. package/out/parser/objectLiteral.js +100 -0
  18. package/out/parser/semantic/bounds.d.ts +15 -0
  19. package/{dist → out}/parser/semantic/bounds.js +0 -1
  20. package/out/parser/semantic/default.d.ts +8 -0
  21. package/out/parser/semantic/default.js +1 -0
  22. package/out/parser/semantic/divisor.d.ts +7 -0
  23. package/out/parser/semantic/divisor.js +1 -0
  24. package/out/parser/semantic/infer.d.ts +27 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +4 -0
  27. package/out/parser/semantic/utils.js +1 -0
  28. package/out/parser/semantic/validate.d.ts +30 -0
  29. package/out/parser/semantic/validate.js +2 -0
  30. package/out/parser/string/reduce/dynamic.d.ts +43 -0
  31. package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
  32. package/out/parser/string/reduce/shared.d.ts +42 -0
  33. package/{dist → out}/parser/string/reduce/shared.js +22 -2
  34. package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
  35. package/out/parser/string/reduce/static.js +1 -0
  36. package/out/parser/string/shift/operand/date.d.ts +9 -0
  37. package/{dist → out}/parser/string/shift/operand/date.js +5 -8
  38. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
  39. package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
  41. package/out/parser/string/shift/operand/genericArgs.js +16 -0
  42. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  43. package/out/parser/string/shift/operand/operand.js +12 -0
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
  45. package/out/parser/string/shift/operand/unenclosed.js +53 -0
  46. package/out/parser/string/shift/operator/bounds.d.ts +27 -0
  47. package/out/parser/string/shift/operator/bounds.js +94 -0
  48. package/out/parser/string/shift/operator/default.d.ts +24 -0
  49. package/out/parser/string/shift/operator/default.js +23 -0
  50. package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
  51. package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
  52. package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
  53. package/out/parser/string/shift/operator/operator.js +22 -0
  54. package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
  55. package/{dist → out}/parser/string/shift/scanner.js +27 -24
  56. package/out/parser/string/string.d.ts +28 -0
  57. package/out/parser/string/string.js +24 -0
  58. package/out/parser/tuple.d.ts +130 -0
  59. package/out/parser/tuple.js +157 -0
  60. package/out/scope.d.ts +86 -0
  61. package/out/scope.js +132 -0
  62. package/out/type.d.ts +83 -0
  63. package/out/type.js +36 -0
  64. package/package.json +37 -41
  65. package/dist/main.d.ts +0 -11
  66. package/dist/main.d.ts.map +0 -1
  67. package/dist/main.js +0 -8
  68. package/dist/main.js.map +0 -1
  69. package/dist/match.d.ts +0 -46
  70. package/dist/match.d.ts.map +0 -1
  71. package/dist/match.js +0 -23
  72. package/dist/match.js.map +0 -1
  73. package/dist/parser/definition.d.ts.map +0 -1
  74. package/dist/parser/definition.js.map +0 -1
  75. package/dist/parser/generic.d.ts +0 -44
  76. package/dist/parser/generic.d.ts.map +0 -1
  77. package/dist/parser/generic.js +0 -46
  78. package/dist/parser/generic.js.map +0 -1
  79. package/dist/parser/objectLiteral.d.ts +0 -24
  80. package/dist/parser/objectLiteral.d.ts.map +0 -1
  81. package/dist/parser/objectLiteral.js +0 -38
  82. package/dist/parser/objectLiteral.js.map +0 -1
  83. package/dist/parser/semantic/bounds.d.ts +0 -13
  84. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  85. package/dist/parser/semantic/bounds.js.map +0 -1
  86. package/dist/parser/semantic/divisor.d.ts +0 -9
  87. package/dist/parser/semantic/divisor.d.ts.map +0 -1
  88. package/dist/parser/semantic/divisor.js +0 -2
  89. package/dist/parser/semantic/divisor.js.map +0 -1
  90. package/dist/parser/semantic/intersections.d.ts +0 -15
  91. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  92. package/dist/parser/semantic/intersections.js +0 -2
  93. package/dist/parser/semantic/intersections.js.map +0 -1
  94. package/dist/parser/semantic/semantic.d.ts +0 -22
  95. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  96. package/dist/parser/semantic/semantic.js +0 -2
  97. package/dist/parser/semantic/semantic.js.map +0 -1
  98. package/dist/parser/semantic/utils.d.ts +0 -8
  99. package/dist/parser/semantic/utils.d.ts.map +0 -1
  100. package/dist/parser/semantic/utils.js +0 -2
  101. package/dist/parser/semantic/utils.js.map +0 -1
  102. package/dist/parser/semantic/validate.d.ts +0 -24
  103. package/dist/parser/semantic/validate.d.ts.map +0 -1
  104. package/dist/parser/semantic/validate.js +0 -2
  105. package/dist/parser/semantic/validate.js.map +0 -1
  106. package/dist/parser/shared.d.ts +0 -48
  107. package/dist/parser/shared.d.ts.map +0 -1
  108. package/dist/parser/shared.js +0 -41
  109. package/dist/parser/shared.js.map +0 -1
  110. package/dist/parser/string/reduce/dynamic.d.ts +0 -40
  111. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  112. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  113. package/dist/parser/string/reduce/shared.d.ts +0 -29
  114. package/dist/parser/string/reduce/shared.d.ts.map +0 -1
  115. package/dist/parser/string/reduce/shared.js.map +0 -1
  116. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  117. package/dist/parser/string/reduce/static.js +0 -2
  118. package/dist/parser/string/reduce/static.js.map +0 -1
  119. package/dist/parser/string/shift/operand/date.d.ts +0 -11
  120. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  121. package/dist/parser/string/shift/operand/date.js.map +0 -1
  122. package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  123. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  124. package/dist/parser/string/shift/operand/operand.d.ts +0 -9
  125. package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
  126. package/dist/parser/string/shift/operand/operand.js +0 -17
  127. package/dist/parser/string/shift/operand/operand.js.map +0 -1
  128. package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
  129. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  130. package/dist/parser/string/shift/operand/unenclosed.js +0 -68
  131. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  132. package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
  133. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  134. package/dist/parser/string/shift/operator/bounds.js +0 -95
  135. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  136. package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
  137. package/dist/parser/string/shift/operator/divisor.js.map +0 -1
  138. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  139. package/dist/parser/string/shift/operator/operator.js +0 -29
  140. package/dist/parser/string/shift/operator/operator.js.map +0 -1
  141. package/dist/parser/string/shift/scanner.d.ts.map +0 -1
  142. package/dist/parser/string/shift/scanner.js.map +0 -1
  143. package/dist/parser/string/string.d.ts +0 -26
  144. package/dist/parser/string/string.d.ts.map +0 -1
  145. package/dist/parser/string/string.js +0 -31
  146. package/dist/parser/string/string.js.map +0 -1
  147. package/dist/parser/tuple.d.ts +0 -73
  148. package/dist/parser/tuple.d.ts.map +0 -1
  149. package/dist/parser/tuple.js +0 -134
  150. package/dist/parser/tuple.js.map +0 -1
  151. package/dist/scope.d.ts +0 -159
  152. package/dist/scope.d.ts.map +0 -1
  153. package/dist/scope.js +0 -248
  154. package/dist/scope.js.map +0 -1
  155. package/dist/scopes/ark.d.ts +0 -38
  156. package/dist/scopes/ark.d.ts.map +0 -1
  157. package/dist/scopes/ark.js +0 -24
  158. package/dist/scopes/ark.js.map +0 -1
  159. package/dist/scopes/jsObjects.d.ts +0 -19
  160. package/dist/scopes/jsObjects.d.ts.map +0 -1
  161. package/dist/scopes/jsObjects.js +0 -15
  162. package/dist/scopes/jsObjects.js.map +0 -1
  163. package/dist/scopes/parsing.d.ts +0 -18
  164. package/dist/scopes/parsing.d.ts.map +0 -1
  165. package/dist/scopes/parsing.js +0 -61
  166. package/dist/scopes/parsing.js.map +0 -1
  167. package/dist/scopes/tsGenerics.d.ts +0 -17
  168. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  169. package/dist/scopes/tsGenerics.js +0 -13
  170. package/dist/scopes/tsGenerics.js.map +0 -1
  171. package/dist/scopes/tsKeywords.d.ts +0 -24
  172. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  173. package/dist/scopes/tsKeywords.js +0 -20
  174. package/dist/scopes/tsKeywords.js.map +0 -1
  175. package/dist/scopes/utils/creditCard.d.ts +0 -3
  176. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  177. package/dist/scopes/utils/creditCard.js +0 -36
  178. package/dist/scopes/utils/creditCard.js.map +0 -1
  179. package/dist/scopes/utils/date.d.ts +0 -24
  180. package/dist/scopes/utils/date.d.ts.map +0 -1
  181. package/dist/scopes/utils/date.js +0 -53
  182. package/dist/scopes/utils/date.js.map +0 -1
  183. package/dist/scopes/validation.d.ts +0 -20
  184. package/dist/scopes/validation.d.ts.map +0 -1
  185. package/dist/scopes/validation.js +0 -57
  186. package/dist/scopes/validation.js.map +0 -1
  187. package/dist/type.d.ts +0 -101
  188. package/dist/type.d.ts.map +0 -1
  189. package/dist/type.js +0 -138
  190. package/dist/type.js.map +0 -1
  191. package/main.ts +0 -20
  192. package/match.ts +0 -137
  193. package/parser/definition.ts +0 -156
  194. package/parser/generic.ts +0 -208
  195. package/parser/objectLiteral.ts +0 -120
  196. package/parser/semantic/bounds.ts +0 -63
  197. package/parser/semantic/divisor.ts +0 -13
  198. package/parser/semantic/intersections.ts +0 -47
  199. package/parser/semantic/semantic.ts +0 -107
  200. package/parser/semantic/utils.ts +0 -26
  201. package/parser/semantic/validate.ts +0 -105
  202. package/parser/shared.ts +0 -141
  203. package/parser/string/reduce/dynamic.ts +0 -181
  204. package/parser/string/reduce/shared.ts +0 -73
  205. package/parser/string/reduce/static.ts +0 -300
  206. package/parser/string/shift/operand/date.ts +0 -57
  207. package/parser/string/shift/operand/enclosed.ts +0 -119
  208. package/parser/string/shift/operand/operand.ts +0 -48
  209. package/parser/string/shift/operand/unenclosed.ts +0 -282
  210. package/parser/string/shift/operator/bounds.ts +0 -229
  211. package/parser/string/shift/operator/divisor.ts +0 -36
  212. package/parser/string/shift/operator/operator.ts +0 -82
  213. package/parser/string/shift/scanner.ts +0 -209
  214. package/parser/string/string.ts +0 -93
  215. package/parser/tuple.ts +0 -475
  216. package/scope.ts +0 -573
  217. package/scopes/ark.ts +0 -64
  218. package/scopes/jsObjects.ts +0 -29
  219. package/scopes/parsing.ts +0 -81
  220. package/scopes/tsGenerics.ts +0 -28
  221. package/scopes/tsKeywords.ts +0 -39
  222. package/scopes/utils/creditCard.ts +0 -37
  223. package/scopes/utils/date.ts +0 -108
  224. package/scopes/validation.ts +0 -81
  225. package/type.ts +0 -403
package/parser/tuple.ts DELETED
@@ -1,475 +0,0 @@
1
- import {
2
- builtins,
3
- node,
4
- type BaseAttributes,
5
- type Morph,
6
- type Out,
7
- type Predicate,
8
- type Root,
9
- type inferMorphOut,
10
- type inferNarrow
11
- } from "@arktype/schema"
12
- import {
13
- isArray,
14
- objectKindOrDomainOf,
15
- stringify,
16
- throwParseError,
17
- type BuiltinObjectKind,
18
- type Constructor,
19
- type Domain,
20
- type List,
21
- type conform,
22
- type evaluate,
23
- type isAny
24
- } from "@arktype/util"
25
- import type { ParseContext } from "../scope.js"
26
- import type { extractIn, extractOut } from "../type.js"
27
- import type { inferDefinition, validateDefinition } from "./definition.js"
28
- import type { inferIntersection } from "./semantic/intersections.js"
29
- import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
30
- import { writeUnsatisfiableExpressionError } from "./semantic/validate.js"
31
- import {
32
- parseEntry,
33
- type EntryParseResult,
34
- type validateObjectValue
35
- } from "./shared.js"
36
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
37
- import type { BaseCompletions } from "./string/string.js"
38
-
39
- export const parseTuple = (def: List, ctx: ParseContext) =>
40
- maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
41
-
42
- export const parseTupleLiteral = (def: List, ctx: ParseContext): Root => {
43
- const props: unknown[] = []
44
- let isVariadic = false
45
- for (let i = 0; i < def.length; i++) {
46
- let elementDef = def[i]
47
- ctx.path.push(`${i}`)
48
- if (typeof elementDef === "string" && elementDef.startsWith("...")) {
49
- elementDef = elementDef.slice(3)
50
- isVariadic = true
51
- } else if (
52
- isArray(elementDef) &&
53
- elementDef.length === 2 &&
54
- elementDef[0] === "..."
55
- ) {
56
- elementDef = elementDef[1]
57
- isVariadic = true
58
- }
59
- const parsedEntry = parseEntry([`${i}`, elementDef])
60
- const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
61
- if (isVariadic) {
62
- if (!value.extends(builtins.array)) {
63
- return throwParseError(writeNonArrayRestMessage(elementDef))
64
- }
65
- if (i !== def.length - 1) {
66
- return throwParseError(prematureRestMessage)
67
- }
68
- // TODO: Fix builtins.arrayIndexTypeNode()
69
- const elementType = value.getPath()
70
- // TODO: first variadic i
71
- props.push({ key: builtins.number, value: elementType })
72
- } else {
73
- props.push({
74
- key: {
75
- name: `${i}`,
76
- prerequisite: false,
77
- optional: parsedEntry.kind === "optional"
78
- },
79
- value
80
- })
81
- }
82
- ctx.path.pop()
83
- }
84
- if (!isVariadic) {
85
- props.push({
86
- key: {
87
- name: "length",
88
- prerequisite: true,
89
- optional: false
90
- },
91
- // , ctx
92
- value: node({ is: def.length })
93
- })
94
- }
95
- // props , ctx
96
- return node(Array)
97
- }
98
-
99
- export const maybeParseTupleExpression = (
100
- def: List,
101
- ctx: ParseContext
102
- ): Root | undefined => {
103
- const tupleExpressionResult = isIndexOneExpression(def)
104
- ? indexOneParsers[def[1]](def as never, ctx)
105
- : isIndexZeroExpression(def)
106
- ? prefixParsers[def[0]](def as never, ctx)
107
- : undefined
108
- if (tupleExpressionResult) {
109
- return tupleExpressionResult.isNever()
110
- ? throwParseError(
111
- writeUnsatisfiableExpressionError(
112
- def
113
- .map((def) => (typeof def === "string" ? def : stringify(def)))
114
- .join(" ")
115
- )
116
- )
117
- : tupleExpressionResult
118
- }
119
- }
120
-
121
- // It is *extremely* important we use readonly any time we check a tuple against
122
- // something like this. Not doing so will always cause the check to fail, since
123
- // def is declared as a const parameter.
124
- type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]]
125
-
126
- export type validateTuple<
127
- def extends List,
128
- $,
129
- args
130
- > = def extends IndexZeroExpression
131
- ? validatePrefixExpression<def, $, args>
132
- : def extends PostfixExpression
133
- ? validatePostfixExpression<def, $, args>
134
- : def extends InfixExpression
135
- ? validateInfixExpression<def, $, args>
136
- : def extends
137
- | readonly ["", ...unknown[]]
138
- | readonly [unknown, "", ...unknown[]]
139
- ? readonly [
140
- def[0] extends ""
141
- ? BaseCompletions<$, args, IndexZeroOperator>
142
- : def[0],
143
- def[1] extends ""
144
- ? BaseCompletions<$, args, IndexOneOperator>
145
- : def[1]
146
- ]
147
- : validateTupleLiteral<def, $, args>
148
-
149
- export type validateTupleLiteral<
150
- def extends List,
151
- $,
152
- args,
153
- result extends unknown[] = []
154
- > = def extends readonly [infer head, ...infer tail]
155
- ? validateTupleLiteral<
156
- tail,
157
- $,
158
- args,
159
- [...result, validateTupleElement<head, tail, $, args>]
160
- >
161
- : result
162
-
163
- type validateTupleElement<head, tail, $, args> =
164
- head extends variadicExpression<infer operand>
165
- ? validateDefinition<operand, $, args> extends infer syntacticResult
166
- ? syntacticResult extends operand
167
- ? semanticallyValidateRestElement<
168
- operand,
169
- $,
170
- args
171
- > extends infer semanticResult
172
- ? semanticResult extends operand
173
- ? tail extends []
174
- ? head
175
- : prematureRestMessage
176
- : semanticResult
177
- : never
178
- : syntacticResult
179
- : never
180
- : validateObjectValue<head, $, args>
181
-
182
- type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
183
- operand,
184
- $,
185
- args
186
- > extends infer result
187
- ? result extends never
188
- ? writeNonArrayRestMessage<operand>
189
- : isAny<result> extends true
190
- ? writeNonArrayRestMessage<operand>
191
- : result extends readonly unknown[]
192
- ? operand
193
- : writeNonArrayRestMessage<operand>
194
- : never
195
-
196
- export const writeNonArrayRestMessage = <operand>(operand: operand) =>
197
- `Rest element ${
198
- typeof operand === "string" ? `'${operand}'` : ""
199
- } must be an array` as writeNonArrayRestMessage<operand>
200
-
201
- type writeNonArrayRestMessage<operand> = `Rest element ${operand extends string
202
- ? `'${operand}'`
203
- : ""} must be an array`
204
-
205
- export const prematureRestMessage = `Rest elements are only allowed at the end of a tuple`
206
-
207
- type prematureRestMessage = typeof prematureRestMessage
208
-
209
- type inferTupleLiteral<
210
- def extends List,
211
- $,
212
- args,
213
- result extends unknown[] = []
214
- > = def extends readonly [infer head, ...infer tail]
215
- ? parseEntry<
216
- result["length"],
217
- head extends variadicExpression<infer operand> ? operand : head
218
- > extends infer entryParseResult extends EntryParseResult
219
- ? inferDefinition<
220
- entryParseResult["innerValue"],
221
- $,
222
- args
223
- > extends infer element
224
- ? head extends variadicExpression
225
- ? element extends readonly unknown[]
226
- ? inferTupleLiteral<tail, $, args, [...result, ...element]>
227
- : never
228
- : inferTupleLiteral<
229
- tail,
230
- $,
231
- args,
232
- entryParseResult["kind"] extends "optional"
233
- ? [...result, element?]
234
- : [...result, element]
235
- >
236
- : never
237
- : never
238
- : result
239
-
240
- type variadicExpression<operandDef = unknown> =
241
- | variadicStringExpression<operandDef & string>
242
- | variadicTupleExpression<operandDef>
243
-
244
- type variadicStringExpression<operandDef extends string = string> =
245
- `...${operandDef}`
246
-
247
- type variadicTupleExpression<operandDef = unknown> = readonly [
248
- "...",
249
- operandDef
250
- ]
251
-
252
- export type inferTuple<def extends List, $, args> = def extends TupleExpression
253
- ? inferTupleExpression<def, $, args>
254
- : inferTupleLiteral<def, $, args>
255
-
256
- export type inferTupleExpression<
257
- def extends TupleExpression,
258
- $,
259
- args
260
- > = def[1] extends "[]"
261
- ? inferDefinition<def[0], $, args>[]
262
- : def[1] extends "&"
263
- ? inferIntersection<
264
- inferDefinition<def[0], $, args>,
265
- inferDefinition<def[2], $, args>
266
- >
267
- : def[1] extends "|"
268
- ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
269
- : def[1] extends ":"
270
- ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
271
- : def[1] extends "=>"
272
- ? parseMorph<def[0], def[2], $, args>
273
- : def[1] extends "@"
274
- ? inferDefinition<def[0], $, args>
275
- : def extends readonly ["===", ...infer values]
276
- ? values[number]
277
- : def extends readonly [
278
- "instanceof",
279
- ...infer constructors extends Constructor[]
280
- ]
281
- ? InstanceType<constructors[number]>
282
- : def[0] extends "keyof"
283
- ? inferKeyOfExpression<def[1], $, args>
284
- : never
285
-
286
- export type validatePrefixExpression<
287
- def extends IndexZeroExpression,
288
- $,
289
- args
290
- > = def["length"] extends 1
291
- ? readonly [writeMissingRightOperandMessage<def[0]>]
292
- : def[0] extends "keyof"
293
- ? readonly [def[0], validateDefinition<def[1], $, args>]
294
- : def[0] extends "==="
295
- ? readonly [def[0], ...unknown[]]
296
- : def[0] extends "instanceof"
297
- ? readonly [def[0], ...Constructor[]]
298
- : never
299
-
300
- export type validatePostfixExpression<
301
- def extends PostfixExpression,
302
- $,
303
- args
304
- // conform here is needed to preserve completions for shallow tuple
305
- // expressions at index 1 after TS 5.1
306
- > = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>
307
-
308
- export type validateInfixExpression<
309
- def extends InfixExpression,
310
- $,
311
- args
312
- > = def["length"] extends 2
313
- ? readonly [def[0], writeMissingRightOperandMessage<def[1]>]
314
- : readonly [
315
- validateDefinition<def[0], $, args>,
316
- def[1],
317
- def[1] extends "|"
318
- ? validateDefinition<def[2], $, args>
319
- : def[1] extends "&"
320
- ? validateDefinition<def[2], $, args>
321
- : def[1] extends ":"
322
- ? Predicate<extractIn<inferDefinition<def[0], $, args>>>
323
- : def[1] extends "=>"
324
- ? Morph<extractOut<inferDefinition<def[0], $, args>>, unknown>
325
- : def[1] extends "@"
326
- ? BaseAttributes | string
327
- : validateDefinition<def[2], $, args>
328
- ]
329
-
330
- export type UnparsedTupleExpressionInput = {
331
- instanceof: Constructor
332
- "===": unknown
333
- }
334
-
335
- export type UnparsedTupleOperator = evaluate<keyof UnparsedTupleExpressionInput>
336
-
337
- export const parseKeyOfTuple: PrefixParser<"keyof"> = (def, ctx) =>
338
- ctx.scope.parse(def[1], ctx).keyof()
339
-
340
- export type inferKeyOfExpression<operandDef, $, args> = evaluate<
341
- keyof inferDefinition<operandDef, $, args>
342
- >
343
-
344
- const parseBranchTuple: PostfixParser<"|" | "&"> = (def, ctx) => {
345
- if (def[2] === undefined) {
346
- return throwParseError(writeMissingRightOperandMessage(def[1], ""))
347
- }
348
- const l = ctx.scope.parse(def[0], ctx)
349
- const r = ctx.scope.parse(def[2], ctx)
350
- return def[1] === "&" ? l.and(r) : l.or(r)
351
- }
352
-
353
- const parseArrayTuple: PostfixParser<"[]"> = (def, ctx) =>
354
- ctx.scope.parse(def[0], ctx).array()
355
-
356
- export type PostfixParser<token extends IndexOneOperator> = (
357
- def: IndexOneExpression<token>,
358
- ctx: ParseContext
359
- ) => Root
360
-
361
- export type PrefixParser<token extends IndexZeroOperator> = (
362
- def: IndexZeroExpression<token>,
363
- ctx: ParseContext
364
- ) => Root
365
-
366
- export type TupleExpression = IndexZeroExpression | IndexOneExpression
367
-
368
- export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator
369
-
370
- export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator
371
-
372
- export type TuplePostfixOperator = "[]"
373
-
374
- export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@"
375
-
376
- export type IndexOneExpression<
377
- token extends IndexOneOperator = IndexOneOperator
378
- > = readonly [unknown, token, ...unknown[]]
379
-
380
- const isIndexOneExpression = (def: List): def is IndexOneExpression =>
381
- indexOneParsers[def[1] as IndexOneOperator] !== undefined
382
-
383
- export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
384
- if (typeof def[2] !== "function") {
385
- return throwParseError(
386
- writeMalformedFunctionalExpressionMessage("=>", def[2])
387
- )
388
- }
389
- // TODO: fix
390
- return ctx.scope.parse(def[0], ctx) //.constrain("morph", def[2] as Morph)
391
- }
392
-
393
- export type parseMorph<inDef, morph, $, args> = morph extends Morph
394
- ? (
395
- // TODO: should this be extractOut
396
- In: extractIn<inferDefinition<inDef, $, args>>
397
- ) => Out<inferMorphOut<ReturnType<morph>>>
398
- : never
399
-
400
- export type MorphAst<i = any, o = any> = (In: i) => Out<o>
401
-
402
- export const writeMalformedFunctionalExpressionMessage = (
403
- operator: FunctionalTupleOperator,
404
- value: unknown
405
- ) =>
406
- `${
407
- operator === ":" ? "Narrow" : "Morph"
408
- } expression requires a function following '${operator}' (was ${typeof value})`
409
-
410
- export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
411
- if (typeof def[2] !== "function") {
412
- return throwParseError(
413
- writeMalformedFunctionalExpressionMessage(":", def[2])
414
- )
415
- }
416
- return ctx.scope
417
- .parse(def[0], ctx)
418
- .constrain("predicate", def[2] as Predicate)
419
- }
420
-
421
- const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) => {
422
- return ctx.scope.parse(def[0], ctx)
423
- }
424
-
425
- const indexOneParsers: {
426
- [token in IndexOneOperator]: PostfixParser<token>
427
- } = {
428
- "|": parseBranchTuple,
429
- "&": parseBranchTuple,
430
- "[]": parseArrayTuple,
431
- ":": parseNarrowTuple,
432
- "=>": parseMorphTuple,
433
- "@": parseAttributeTuple
434
- }
435
-
436
- export type FunctionalTupleOperator = ":" | "=>"
437
-
438
- export type IndexZeroOperator = "keyof" | "instanceof" | "==="
439
-
440
- export type IndexZeroExpression<
441
- token extends IndexZeroOperator = IndexZeroOperator
442
- > = readonly [token, ...unknown[]]
443
-
444
- const prefixParsers: {
445
- [token in IndexZeroOperator]: PrefixParser<token>
446
- } = {
447
- keyof: parseKeyOfTuple,
448
- instanceof: (def, ctx) => {
449
- if (typeof def[1] !== "function") {
450
- return throwParseError(
451
- writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
452
- )
453
- }
454
- const branches = def
455
- .slice(1)
456
- .map((ctor) =>
457
- typeof ctor === "function"
458
- ? { basis: ctor as Constructor }
459
- : throwParseError(
460
- writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
461
- )
462
- )
463
- return node(...branches)
464
- },
465
- "===": (def) => node({ is: def.slice(1) })
466
- }
467
-
468
- const isIndexZeroExpression = (def: List): def is IndexZeroExpression =>
469
- prefixParsers[def[0] as IndexZeroOperator] !== undefined
470
-
471
- export const writeInvalidConstructorMessage = <
472
- actual extends Domain | BuiltinObjectKind
473
- >(
474
- actual: actual
475
- ) => `Expected a constructor following 'instanceof' operator (was ${actual}).`