arktype 2.0.0-dev.10 → 2.0.0-dev.11

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 (144) hide show
  1. package/{main.ts → out/api.d.ts} +6 -6
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +1 -1
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +3 -3
  7. package/out/match.js +1 -1
  8. package/out/module.d.ts +4 -2
  9. package/out/parser/definition.d.ts +10 -8
  10. package/out/parser/definition.js +2 -2
  11. package/out/parser/objectLiteral.d.ts +8 -13
  12. package/out/parser/objectLiteral.js +20 -23
  13. package/out/parser/semantic/bounds.d.ts +3 -3
  14. package/out/parser/semantic/divisor.d.ts +2 -2
  15. package/out/parser/semantic/infer.d.ts +1 -1
  16. package/out/parser/semantic/validate.d.ts +6 -4
  17. package/out/parser/string/reduce/dynamic.d.ts +6 -6
  18. package/out/parser/string/reduce/shared.d.ts +16 -16
  19. package/out/parser/string/reduce/shared.js +4 -2
  20. package/out/parser/string/shift/operand/date.d.ts +2 -2
  21. package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
  22. package/out/parser/string/shift/operand/enclosed.js +2 -1
  23. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  24. package/out/parser/string/shift/operand/unenclosed.d.ts +8 -6
  25. package/out/parser/string/shift/operand/unenclosed.js +2 -2
  26. package/out/parser/string/shift/operator/bounds.d.ts +5 -5
  27. package/out/parser/string/shift/operator/bounds.js +3 -3
  28. package/out/parser/string/shift/operator/divisor.d.ts +1 -1
  29. package/out/parser/string/shift/operator/operator.d.ts +1 -1
  30. package/out/parser/string/string.d.ts +2 -2
  31. package/out/parser/tuple.d.ts +11 -11
  32. package/out/parser/tuple.js +3 -3
  33. package/out/scope.d.ts +13 -11
  34. package/out/scope.js +5 -5
  35. package/out/type.d.ts +4 -4
  36. package/out/type.js +2 -2
  37. package/package.json +8 -10
  38. package/__tests__/array.test.ts +0 -391
  39. package/__tests__/badDefinitionType.test.ts +0 -58
  40. package/__tests__/basis.test.ts +0 -50
  41. package/__tests__/bounds.test.ts +0 -318
  42. package/__tests__/cast.test.ts +0 -37
  43. package/__tests__/completions.test.ts +0 -51
  44. package/__tests__/config.test.ts +0 -96
  45. package/__tests__/dateLiteral.test.ts +0 -36
  46. package/__tests__/declared.test.ts +0 -124
  47. package/__tests__/discrimination.test.ts +0 -74
  48. package/__tests__/divisor.test.ts +0 -114
  49. package/__tests__/enclosed.test.ts +0 -107
  50. package/__tests__/examples.test.ts +0 -56
  51. package/__tests__/expressions.test.ts +0 -210
  52. package/__tests__/generateBenchData.ts +0 -89
  53. package/__tests__/generated/cyclic.ts +0 -3058
  54. package/__tests__/generic.test.ts +0 -313
  55. package/__tests__/group.test.ts +0 -85
  56. package/__tests__/hkt.test.ts +0 -22
  57. package/__tests__/imports.test.ts +0 -126
  58. package/__tests__/instanceof.test.ts +0 -104
  59. package/__tests__/intersection.test.ts +0 -108
  60. package/__tests__/keyTraversal.test.ts +0 -77
  61. package/__tests__/keyof.test.ts +0 -150
  62. package/__tests__/keywords.test.ts +0 -214
  63. package/__tests__/literal.test.ts +0 -90
  64. package/__tests__/match.bench.ts +0 -31
  65. package/__tests__/match.test.ts +0 -234
  66. package/__tests__/methods.bench.ts +0 -22
  67. package/__tests__/narrow.test.ts +0 -100
  68. package/__tests__/object.bench.ts +0 -28
  69. package/__tests__/objectLiteral.test.ts +0 -442
  70. package/__tests__/operand.bench.ts +0 -45
  71. package/__tests__/operator.bench.ts +0 -130
  72. package/__tests__/pipe.test.ts +0 -501
  73. package/__tests__/realWorld.test.ts +0 -63
  74. package/__tests__/regex.test.ts +0 -68
  75. package/__tests__/scope.bench.ts +0 -39
  76. package/__tests__/scope.test.ts +0 -374
  77. package/__tests__/serialization.test.ts +0 -19
  78. package/__tests__/string.test.ts +0 -52
  79. package/__tests__/submodule.test.ts +0 -86
  80. package/__tests__/this.test.ts +0 -83
  81. package/__tests__/thunk.test.ts +0 -86
  82. package/__tests__/traverse.test.ts +0 -141
  83. package/__tests__/tsGenerics.test.ts +0 -12
  84. package/__tests__/type.test.ts +0 -36
  85. package/__tests__/typeReference.test.ts +0 -39
  86. package/__tests__/unenclosed.test.ts +0 -136
  87. package/__tests__/union.test.ts +0 -214
  88. package/ark.ts +0 -47
  89. package/generic.ts +0 -69
  90. package/match.ts +0 -218
  91. package/module.ts +0 -15
  92. package/out/ark.js.map +0 -1
  93. package/out/generic.js.map +0 -1
  94. package/out/main.js.map +0 -1
  95. package/out/match.js.map +0 -1
  96. package/out/module.js.map +0 -1
  97. package/out/parser/definition.js.map +0 -1
  98. package/out/parser/generic.js.map +0 -1
  99. package/out/parser/objectLiteral.js.map +0 -1
  100. package/out/parser/semantic/bounds.js.map +0 -1
  101. package/out/parser/semantic/divisor.js.map +0 -1
  102. package/out/parser/semantic/infer.js.map +0 -1
  103. package/out/parser/semantic/utils.js.map +0 -1
  104. package/out/parser/semantic/validate.js.map +0 -1
  105. package/out/parser/string/reduce/dynamic.js.map +0 -1
  106. package/out/parser/string/reduce/shared.js.map +0 -1
  107. package/out/parser/string/reduce/static.js.map +0 -1
  108. package/out/parser/string/shift/operand/date.js.map +0 -1
  109. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  110. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  111. package/out/parser/string/shift/operand/operand.js.map +0 -1
  112. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  113. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  114. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  115. package/out/parser/string/shift/operator/operator.js.map +0 -1
  116. package/out/parser/string/shift/scanner.js.map +0 -1
  117. package/out/parser/string/string.js.map +0 -1
  118. package/out/parser/tuple.js.map +0 -1
  119. package/out/scope.js.map +0 -1
  120. package/out/type.js.map +0 -1
  121. package/parser/definition.ts +0 -143
  122. package/parser/generic.ts +0 -66
  123. package/parser/objectLiteral.ts +0 -233
  124. package/parser/semantic/bounds.ts +0 -62
  125. package/parser/semantic/divisor.ts +0 -11
  126. package/parser/semantic/infer.ts +0 -141
  127. package/parser/semantic/utils.ts +0 -15
  128. package/parser/semantic/validate.ts +0 -117
  129. package/parser/string/reduce/dynamic.ts +0 -208
  130. package/parser/string/reduce/shared.ts +0 -99
  131. package/parser/string/reduce/static.ts +0 -296
  132. package/parser/string/shift/operand/date.ts +0 -55
  133. package/parser/string/shift/operand/enclosed.ts +0 -123
  134. package/parser/string/shift/operand/genericArgs.ts +0 -128
  135. package/parser/string/shift/operand/operand.ts +0 -42
  136. package/parser/string/shift/operand/unenclosed.ts +0 -217
  137. package/parser/string/shift/operator/bounds.ts +0 -270
  138. package/parser/string/shift/operator/divisor.ts +0 -33
  139. package/parser/string/shift/operator/operator.ts +0 -71
  140. package/parser/string/shift/scanner.ts +0 -210
  141. package/parser/string/string.ts +0 -85
  142. package/parser/tuple.ts +0 -542
  143. package/scope.ts +0 -291
  144. package/type.ts +0 -266
package/parser/tuple.ts DELETED
@@ -1,542 +0,0 @@
1
- import {
2
- type BaseMeta,
3
- type Morph,
4
- type MutableInner,
5
- type Node,
6
- type Out,
7
- type Predicate,
8
- type RawSchema,
9
- type UnionChildKind,
10
- type UnknownSchema,
11
- type distillConstrainableIn,
12
- type distillConstrainableOut,
13
- type inferIntersection,
14
- type inferMorphOut,
15
- type inferNarrow,
16
- jsObjects,
17
- makeRootAndArrayPropertiesMutable,
18
- tsKeywords
19
- } from "@arktype/schema"
20
- import {
21
- type BuiltinObjectKind,
22
- type Constructor,
23
- type Domain,
24
- type ErrorMessage,
25
- append,
26
- type array,
27
- type conform,
28
- objectKindOrDomainOf,
29
- type show,
30
- throwParseError
31
- } from "@arktype/util"
32
- import type { ParseContext } from "../scope.js"
33
- import type { inferDefinition, validateDefinition } from "./definition.js"
34
- import type { InfixOperator, PostfixExpression } from "./semantic/infer.js"
35
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
36
- import type { BaseCompletions } from "./string/string.js"
37
-
38
- export const parseTuple = (def: array, ctx: ParseContext): RawSchema =>
39
- maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
40
-
41
- export const parseTupleLiteral = (def: array, ctx: ParseContext): RawSchema => {
42
- let sequences: MutableInner<"sequence">[] = [{}]
43
- let i = 0
44
- while (i < def.length) {
45
- let spread = false
46
- let optional = false
47
- if (def[i] === "..." && i < def.length - 1) {
48
- spread = true
49
- i++
50
- }
51
-
52
- const element = ctx.$.parse(def[i], ctx)
53
-
54
- i++
55
- if (def[i] === "?") {
56
- if (spread) return throwParseError(spreadOptionalMessage)
57
-
58
- optional = true
59
- i++
60
- }
61
- if (spread) {
62
- if (!element.extends(jsObjects.Array))
63
- return throwParseError(writeNonArraySpreadMessage(element.expression))
64
-
65
- // a spread must be distributed over branches e.g.:
66
- // def: [string, ...(number[] | [true, false])]
67
- // nodes: [string, ...number[]] | [string, true, false]
68
- sequences = sequences.flatMap(base =>
69
- // since appendElement mutates base, we have to shallow-ish clone it for each branch
70
- element.branches.map(branch =>
71
- appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)
72
- )
73
- )
74
- } else {
75
- sequences = sequences.map(base =>
76
- appendElement(base, optional ? "optional" : "required", element)
77
- )
78
- }
79
- }
80
- return ctx.$.raw.schema(
81
- sequences.map(
82
- sequence =>
83
- ({
84
- proto: Array,
85
- sequence
86
- }) as const
87
- )
88
- )
89
- }
90
-
91
- type ElementKind = "optional" | "required" | "variadic"
92
-
93
- const appendElement = (
94
- base: MutableInner<"sequence">,
95
- kind: ElementKind,
96
- element: UnknownSchema
97
- ): MutableInner<"sequence"> => {
98
- switch (kind) {
99
- case "required":
100
- if (base.optional)
101
- // e.g. [string?, number]
102
- return throwParseError(requiredPostOptionalMessage)
103
- if (base.variadic) {
104
- // e.g. [...string[], number]
105
- base.postfix = append(base.postfix, element)
106
- } else {
107
- // e.g. [string, number]
108
- base.prefix = append(base.prefix, element)
109
- }
110
- return base
111
- case "optional":
112
- if (base.variadic)
113
- // e.g. [...string[], number?]
114
- return throwParseError(optionalPostVariadicMessage)
115
- // e.g. [string, number?]
116
- base.optional = append(base.optional, element)
117
- return base
118
- case "variadic":
119
- // e.g. [...string[], number, ...string[]]
120
- if (base.postfix) throwParseError(multipleVariadicMesage)
121
- if (base.variadic) {
122
- if (!base.variadic.equals(element)) {
123
- // e.g. [...string[], ...number[]]
124
- throwParseError(multipleVariadicMesage)
125
- }
126
- // e.g. [...string[], ...string[]]
127
- // do nothing, second spread doesn't change the type
128
- } else {
129
- // e.g. [string, ...number[]]
130
- base.variadic = element.raw
131
- }
132
- return base
133
- }
134
- }
135
-
136
- const appendSpreadBranch = (
137
- base: MutableInner<"sequence">,
138
- branch: Node<UnionChildKind>
139
- ): MutableInner<"sequence"> => {
140
- const spread = branch.firstReferenceOfKind("sequence")
141
- if (!spread) {
142
- // the only array with no sequence reference is unknown[]
143
- return appendElement(base, "variadic", tsKeywords.unknown)
144
- }
145
- spread.prefix.forEach(node => appendElement(base, "required", node))
146
- spread.optional.forEach(node => appendElement(base, "optional", node))
147
- spread.variadic && appendElement(base, "variadic", spread.variadic)
148
- spread.postfix.forEach(node => appendElement(base, "required", node))
149
- return base
150
- }
151
-
152
- const maybeParseTupleExpression = (
153
- def: array,
154
- ctx: ParseContext
155
- ): RawSchema | undefined => {
156
- const tupleExpressionResult =
157
- isIndexZeroExpression(def) ? prefixParsers[def[0]](def as never, ctx)
158
- : isIndexOneExpression(def) ? indexOneParsers[def[1]](def as never, ctx)
159
- : undefined
160
- return tupleExpressionResult
161
-
162
- // TODO: remove
163
- // return tupleExpressionResult.isNever()
164
- // ? throwParseError(
165
- // writeUnsatisfiableExpressionError(
166
- // def
167
- // .map((def) =>
168
- // typeof def === "string" ? def : printable(def)
169
- // )
170
- // .join(" ")
171
- // )
172
- // )
173
- // : tupleExpressionResult
174
- }
175
-
176
- // It is *extremely* important we use readonly any time we check a tuple against
177
- // something like this. Not doing so will always cause the check to fail, since
178
- // def is declared as a const parameter.
179
- type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]]
180
-
181
- export type validateTuple<def extends array, $, args> =
182
- def extends IndexZeroExpression ? validatePrefixExpression<def, $, args>
183
- : def extends PostfixExpression ? validatePostfixExpression<def, $, args>
184
- : def extends InfixExpression ? validateInfixExpression<def, $, args>
185
- : def extends (
186
- readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]
187
- ) ?
188
- readonly [
189
- def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "...">
190
- : def[0],
191
- def[1] extends "" ?
192
- BaseCompletions<$, args, IndexOneOperator | "..." | "?">
193
- : def[1]
194
- ]
195
- : validateTupleLiteral<def, $, args>
196
-
197
- export type validateTupleLiteral<def extends array, $, args> = Readonly<
198
- parseSequence<def, $, args>["validated"]
199
- >
200
-
201
- type inferTupleLiteral<def extends array, $, args> = parseSequence<
202
- def,
203
- $,
204
- args
205
- >["inferred"]
206
-
207
- type SequenceParseState = {
208
- unscanned: array
209
- inferred: array
210
- validated: array
211
- }
212
-
213
- type parseSequence<def extends array, $, args> = parseNextElement<
214
- {
215
- unscanned: def
216
- inferred: []
217
- validated: []
218
- },
219
- $,
220
- args
221
- >
222
-
223
- type PreparsedElement = {
224
- head: unknown
225
- tail: array
226
- optional: boolean
227
- spread: boolean
228
- }
229
-
230
- namespace PreparsedElement {
231
- export type from<result extends PreparsedElement> = result
232
- }
233
-
234
- type preparseNextElement<s extends SequenceParseState> =
235
- s["unscanned"] extends readonly ["...", infer head, ...infer tail] ?
236
- tail extends readonly ["?", ...infer postOptionalTail] ?
237
- PreparsedElement.from<{
238
- head: head
239
- tail: postOptionalTail
240
- // this will be an error we have to handle
241
- optional: true
242
- spread: true
243
- }>
244
- : PreparsedElement.from<{
245
- head: head
246
- tail: tail
247
- optional: false
248
- spread: true
249
- }>
250
- : s["unscanned"] extends readonly [infer head, "?", ...infer tail] ?
251
- PreparsedElement.from<{
252
- head: head
253
- tail: tail
254
- optional: true
255
- spread: false
256
- }>
257
- : s["unscanned"] extends readonly [infer head, ...infer tail] ?
258
- PreparsedElement.from<{
259
- head: head
260
- tail: tail
261
- optional: false
262
- spread: false
263
- }>
264
- : null
265
-
266
- type parseNextElement<s extends SequenceParseState, $, args> =
267
- preparseNextElement<s> extends infer next extends PreparsedElement ?
268
- parseNextElement<
269
- {
270
- unscanned: next["tail"]
271
- inferred: next["spread"] extends true ?
272
- [
273
- ...s["inferred"],
274
- ...conform<inferDefinition<next["head"], $, args>, array>
275
- ]
276
- : next["optional"] extends true ?
277
- [...s["inferred"], inferDefinition<next["head"], $, args>?]
278
- : [...s["inferred"], inferDefinition<next["head"], $, args>]
279
- validated: [
280
- ...s["validated"],
281
- ...(next["spread"] extends true ?
282
- [
283
- inferDefinition<next["head"], $, args> extends (
284
- infer spreadOperand extends array
285
- ) ?
286
- [number, number] extends (
287
- [s["inferred"]["length"], spreadOperand["length"]]
288
- ) ?
289
- ErrorMessage<multipleVariadicMessage>
290
- : "..."
291
- : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
292
- ]
293
- : []),
294
- [next["optional"] | next["spread"], "?"] extends (
295
- [false, s["validated"][number]]
296
- ) ?
297
- ErrorMessage<requiredPostOptionalMessage>
298
- : validateDefinition<next["head"], $, args>,
299
- ...(next["optional"] extends true ?
300
- [
301
- next["spread"] extends true ? ErrorMessage<spreadOptionalMessage>
302
- : number extends s["inferred"]["length"] ?
303
- ErrorMessage<optionalPostVariadicMessage>
304
- : "?"
305
- ]
306
- : [])
307
- ]
308
- },
309
- $,
310
- args
311
- >
312
- : s
313
-
314
- export const writeNonArraySpreadMessage = <operand extends string>(
315
- operand: operand
316
- ): writeNonArraySpreadMessage<operand> =>
317
- `Spread element must be an array (was ${operand})` as never
318
-
319
- type writeNonArraySpreadMessage<operand> =
320
- `Spread element must be an array${operand extends string ? `(was ${operand})`
321
- : ""}`
322
-
323
- export const multipleVariadicMesage =
324
- "A tuple may have at most one variadic element"
325
-
326
- type multipleVariadicMessage = typeof multipleVariadicMesage
327
-
328
- export const requiredPostOptionalMessage =
329
- "A required element may not follow an optional element"
330
-
331
- type requiredPostOptionalMessage = typeof requiredPostOptionalMessage
332
-
333
- export const optionalPostVariadicMessage =
334
- "An optional element may not follow a variadic element"
335
-
336
- type optionalPostVariadicMessage = typeof optionalPostVariadicMessage
337
-
338
- export const spreadOptionalMessage = "A spread element cannot be optional"
339
-
340
- type spreadOptionalMessage = typeof optionalPostVariadicMessage
341
-
342
- export type inferTuple<def extends array, $, args> =
343
- def extends TupleExpression ? inferTupleExpression<def, $, args>
344
- : inferTupleLiteral<def, $, args>
345
-
346
- export type inferTupleExpression<def extends TupleExpression, $, args> =
347
- def[1] extends "[]" ? inferDefinition<def[0], $, args>[]
348
- : def[1] extends "&" ?
349
- inferIntersection<
350
- inferDefinition<def[0], $, args>,
351
- inferDefinition<def[2], $, args>
352
- >
353
- : def[1] extends "|" ?
354
- inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
355
- : def[1] extends ":" ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
356
- : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args>
357
- : def[1] extends "@" ? inferDefinition<def[0], $, args>
358
- : def extends readonly ["===", ...infer values] ? values[number]
359
- : def extends (
360
- readonly ["instanceof", ...infer constructors extends Constructor[]]
361
- ) ?
362
- InstanceType<constructors[number]>
363
- : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args>
364
- : never
365
-
366
- export type validatePrefixExpression<def extends IndexZeroExpression, $, args> =
367
- def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>]
368
- : def[0] extends "keyof" ?
369
- readonly [def[0], validateDefinition<def[1], $, args>]
370
- : def[0] extends "===" ? readonly [def[0], ...unknown[]]
371
- : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]]
372
- : never
373
-
374
- export type validatePostfixExpression<
375
- def extends PostfixExpression,
376
- $,
377
- args
378
- // conform here is needed to preserve completions for shallow tuple
379
- // expressions at index 1 after TS 5.1
380
- > = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>
381
-
382
- export type validateInfixExpression<def extends InfixExpression, $, args> =
383
- def["length"] extends 2 ?
384
- readonly [def[0], writeMissingRightOperandMessage<def[1]>]
385
- : readonly [
386
- validateDefinition<def[0], $, args>,
387
- def[1],
388
- def[1] extends "|" ? validateDefinition<def[2], $, args>
389
- : def[1] extends "&" ? validateDefinition<def[2], $, args>
390
- : def[1] extends ":" ?
391
- Predicate<distillConstrainableIn<inferDefinition<def[0], $, args>>>
392
- : def[1] extends "=>" ?
393
- Morph<
394
- distillConstrainableOut<inferDefinition<def[0], $, args>>,
395
- unknown
396
- >
397
- : def[1] extends "@" ? BaseMeta | string
398
- : validateDefinition<def[2], $, args>
399
- ]
400
-
401
- export type UnparsedTupleExpressionInput = {
402
- instanceof: Constructor
403
- "===": unknown
404
- }
405
-
406
- export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>
407
-
408
- export const parseKeyOfTuple: PrefixParser<"keyof"> = (def, ctx) =>
409
- ctx.$.parse(def[1], ctx).keyof()
410
-
411
- export type inferKeyOfExpression<operandDef, $, args> = show<
412
- keyof inferDefinition<operandDef, $, args>
413
- >
414
-
415
- const parseBranchTuple: PostfixParser<"|" | "&"> = (def, ctx) => {
416
- if (def[2] === undefined)
417
- return throwParseError(writeMissingRightOperandMessage(def[1], ""))
418
-
419
- const l = ctx.$.parse(def[0], ctx)
420
- const r = ctx.$.parse(def[2], ctx)
421
- return def[1] === "&" ? l.and(r) : l.or(r)
422
- }
423
-
424
- const parseArrayTuple: PostfixParser<"[]"> = (def, ctx) =>
425
- ctx.$.parse(def[0], ctx).array()
426
-
427
- export type PostfixParser<token extends IndexOneOperator> = (
428
- def: IndexOneExpression<token>,
429
- ctx: ParseContext
430
- ) => RawSchema
431
-
432
- export type PrefixParser<token extends IndexZeroOperator> = (
433
- def: IndexZeroExpression<token>,
434
- ctx: ParseContext
435
- ) => RawSchema
436
-
437
- export type TupleExpression = IndexZeroExpression | IndexOneExpression
438
-
439
- export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator
440
-
441
- export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator
442
-
443
- export type TuplePostfixOperator = "[]"
444
-
445
- export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@"
446
-
447
- export type IndexOneExpression<
448
- token extends IndexOneOperator = IndexOneOperator
449
- > = readonly [unknown, token, ...unknown[]]
450
-
451
- const isIndexOneExpression = (def: array): def is IndexOneExpression =>
452
- indexOneParsers[def[1] as IndexOneOperator] !== undefined
453
-
454
- export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
455
- if (typeof def[2] !== "function") {
456
- return throwParseError(
457
- writeMalformedFunctionalExpressionMessage("=>", def[2])
458
- )
459
- }
460
- // TODO: nested morphs?
461
- return ctx.$.parse(def[0], ctx).pipe(def[2] as Morph)
462
- }
463
-
464
- export const writeMalformedFunctionalExpressionMessage = (
465
- operator: ":" | "=>",
466
- value: unknown
467
- ) =>
468
- `${
469
- operator === ":" ? "Narrow" : "Morph"
470
- } expression requires a function following '${operator}' (was ${typeof value})` as const
471
-
472
- export type parseMorph<inDef, morph, $, args> =
473
- morph extends Morph ?
474
- (
475
- In: distillConstrainableIn<inferDefinition<inDef, $, args>>
476
- ) => Out<inferMorphOut<morph>>
477
- : never
478
-
479
- export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
480
- if (typeof def[2] !== "function") {
481
- return throwParseError(
482
- writeMalformedFunctionalExpressionMessage(":", def[2])
483
- )
484
- }
485
- return ctx.$.parse(def[0], ctx).constrain("predicate", def[2] as Predicate)
486
- }
487
-
488
- const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) =>
489
- ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2] as never)
490
-
491
- const indexOneParsers: {
492
- [token in IndexOneOperator]: PostfixParser<token>
493
- } = {
494
- "|": parseBranchTuple,
495
- "&": parseBranchTuple,
496
- "[]": parseArrayTuple,
497
- ":": parseNarrowTuple,
498
- "=>": parseMorphTuple,
499
- "@": parseAttributeTuple
500
- }
501
-
502
- export type IndexZeroOperator = "keyof" | "instanceof" | "==="
503
-
504
- export type IndexZeroExpression<
505
- token extends IndexZeroOperator = IndexZeroOperator
506
- > = readonly [token, ...unknown[]]
507
-
508
- const prefixParsers: {
509
- [token in IndexZeroOperator]: PrefixParser<token>
510
- } = {
511
- keyof: parseKeyOfTuple,
512
- instanceof: (def, ctx) => {
513
- if (typeof def[1] !== "function") {
514
- return throwParseError(
515
- writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
516
- )
517
- }
518
- const branches = def
519
- .slice(1)
520
- .map(ctor =>
521
- typeof ctor === "function" ?
522
- ctx.$.node("proto", { proto: ctor as Constructor })
523
- : throwParseError(
524
- writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
525
- )
526
- )
527
- return branches.length === 1 ?
528
- branches[0]
529
- : ctx.$.node("union", { branches })
530
- },
531
- "===": (def, ctx) => ctx.$.units(def.slice(1))
532
- }
533
-
534
- const isIndexZeroExpression = (def: array): def is IndexZeroExpression =>
535
- prefixParsers[def[0] as IndexZeroOperator] !== undefined
536
-
537
- export const writeInvalidConstructorMessage = <
538
- actual extends Domain | BuiltinObjectKind
539
- >(
540
- actual: actual
541
- ) =>
542
- `Expected a constructor following 'instanceof' operator (was ${actual})` as const