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
@@ -1,47 +0,0 @@
1
- import type { Out } from "@arktype/schema"
2
- import {
3
- Hkt,
4
- type List,
5
- type conform,
6
- type evaluate,
7
- type intersectArrays,
8
- type isAny
9
- } from "@arktype/util"
10
- import type { MorphAst } from "../tuple.js"
11
-
12
- export type inferIntersection<l, r> = [l] extends [never]
13
- ? never
14
- : [r] extends [never]
15
- ? never
16
- : [l & r] extends [never]
17
- ? never
18
- : isAny<l | r> extends true
19
- ? any
20
- : l extends MorphAst<infer lIn, infer lOut>
21
- ? r extends MorphAst
22
- ? never
23
- : (In: evaluate<lIn & r>) => Out<lOut>
24
- : r extends MorphAst<infer rIn, infer rOut>
25
- ? (In: evaluate<rIn & l>) => Out<rOut>
26
- : [l, r] extends [object, object]
27
- ? intersectObjects<l, r> extends infer result
28
- ? result
29
- : never
30
- : l & r
31
-
32
- declare class MorphableIntersection extends Hkt.Kind {
33
- f: (
34
- In: conform<this[Hkt.key], [l: unknown, r: unknown]>
35
- ) => inferIntersection<(typeof In)[0], (typeof In)[1]>
36
- }
37
-
38
- type intersectObjects<l, r> = [l, r] extends [
39
- infer lList extends List,
40
- infer rList extends List
41
- ]
42
- ? intersectArrays<lList, rList, MorphableIntersection>
43
- : evaluate<
44
- {
45
- [k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
46
- } & Omit<r, keyof l>
47
- >
@@ -1,107 +0,0 @@
1
- import type { BigintLiteral, List, NumberLiteral } from "@arktype/util"
2
- import type {
3
- UnparsedScope,
4
- resolve,
5
- tryInferSubmoduleReference
6
- } from "../../scope.js"
7
- import type { GenericProps } from "../../type.js"
8
- import type { inferDefinition } from "../definition.js"
9
- import type { DateLiteral } from "../string/shift/operand/date.js"
10
- import type { StringLiteral } from "../string/shift/operand/enclosed.js"
11
- import type {
12
- Comparator,
13
- LimitLiteral
14
- } from "../string/shift/operator/bounds.js"
15
- import type { inferIntersection } from "./intersections.js"
16
-
17
- export type inferAst<ast, $, args> = ast extends List
18
- ? inferExpression<ast, $, args>
19
- : inferTerminal<ast, $, args>
20
-
21
- export type GenericInstantiationAst<
22
- g extends GenericProps = GenericProps,
23
- argAsts extends unknown[] = unknown[]
24
- > = [g, "<>", argAsts]
25
-
26
- export type inferExpression<
27
- ast extends List,
28
- $,
29
- args
30
- > = ast extends GenericInstantiationAst
31
- ? inferDefinition<
32
- ast[0]["definition"],
33
- ast[0]["$"] extends UnparsedScope
34
- ? // If the generic was defined in the current scope, its definition can be
35
- // resolved using the same scope as that of the input args.
36
- $
37
- : // Otherwise, use the scope that was explicitly associated with it.
38
- ast[0]["$"],
39
- {
40
- // Using keyof g["parameters"] & number here results in the element types
41
- // being mixed- another reason TS should not have separate `${number}` and number keys!
42
- [i in keyof ast[0]["parameters"] &
43
- `${number}` as ast[0]["parameters"][i]]: inferAst<
44
- ast[2][i & keyof ast[2]],
45
- $,
46
- args
47
- >
48
- }
49
- >
50
- : ast[1] extends "[]"
51
- ? inferAst<ast[0], $, args>[]
52
- : ast[1] extends "|"
53
- ? inferAst<ast[0], $, args> | inferAst<ast[2], $, args>
54
- : ast[1] extends "&"
55
- ? inferIntersection<
56
- inferAst<ast[0], $, args>,
57
- inferAst<ast[2], $, args>
58
- >
59
- : ast[1] extends Comparator
60
- ? ast[0] extends LimitLiteral
61
- ? inferAst<ast[2], $, args>
62
- : inferAst<ast[0], $, args>
63
- : ast[1] extends "%"
64
- ? inferAst<ast[0], $, args>
65
- : ast[0] extends "keyof"
66
- ? keyof inferAst<ast[1], $, args>
67
- : never
68
-
69
- export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
70
-
71
- export type PrefixExpression<
72
- operator extends PrefixOperator = PrefixOperator,
73
- operand = unknown
74
- > = [operator, operand]
75
-
76
- export type PostfixOperator = "[]"
77
-
78
- export type PostfixExpression<
79
- operator extends PostfixOperator = PostfixOperator,
80
- operand = unknown
81
- > = readonly [operand, operator]
82
-
83
- export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
84
-
85
- export type InfixExpression<
86
- operator extends InfixOperator = InfixOperator,
87
- l = unknown,
88
- r = unknown
89
- > = [l, operator, r]
90
-
91
- export type RegexLiteral<source extends string = string> = `/${source}/`
92
-
93
- export type inferTerminal<token, $, args> = token extends keyof args | keyof $
94
- ? resolve<token, $, args>
95
- : token extends StringLiteral<infer Text>
96
- ? Text
97
- : token extends RegexLiteral
98
- ? string
99
- : token extends DateLiteral
100
- ? Date
101
- : token extends NumberLiteral<infer value>
102
- ? value
103
- : token extends BigintLiteral<infer value>
104
- ? value
105
- : // doing this last allows us to infer never if it isn't valid rather than check
106
- // if it's a valid submodule reference ahead of time
107
- tryInferSubmoduleReference<$, token>
@@ -1,26 +0,0 @@
1
- import type { List, Stringifiable } from "@arktype/util"
2
- import type { Comparator } from "../string/shift/operator/bounds.js"
3
- import type { InfixExpression, PostfixExpression } from "./semantic.js"
4
-
5
- export type astToString<ast> = `'${astToStringRecurse<ast>}'`
6
-
7
- type astToStringRecurse<ast> = ast extends PostfixExpression<
8
- infer operator,
9
- infer operand
10
- >
11
- ? operator extends "[]"
12
- ? `${groupAst<operand>}[]`
13
- : never
14
- : ast extends InfixExpression<infer operator, infer l, infer r>
15
- ? operator extends "&" | "|" | "%" | Comparator
16
- ? `${groupAst<l>}${operator}${groupAst<r>}`
17
- : never
18
- : ast extends Stringifiable
19
- ? `${ast extends bigint ? `${ast}n` : ast}`
20
- : "..."
21
-
22
- type groupAst<ast> = ast extends List
23
- ? ast[1] extends "[]"
24
- ? astToStringRecurse<ast>
25
- : `(${astToStringRecurse<ast>})`
26
- : astToStringRecurse<ast>
@@ -1,105 +0,0 @@
1
- import type {
2
- BigintLiteral,
3
- Completion,
4
- ErrorMessage,
5
- NumberLiteral,
6
- writeMalformedNumericLiteralMessage
7
- } from "@arktype/util"
8
- import type { Module } from "../../scope.js"
9
- import type { GenericProps } from "../../type.js"
10
- import type { writeInvalidGenericArgsMessage } from "../generic.js"
11
- import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
12
- import type { Comparator } from "../string/shift/operator/bounds.js"
13
- import type { parseString } from "../string/string.js"
14
- import type { validateRange } from "./bounds.js"
15
- import type { validateDivisor } from "./divisor.js"
16
- import type {
17
- GenericInstantiationAst,
18
- InfixExpression,
19
- PostfixExpression
20
- } from "./semantic.js"
21
- import type { astToString } from "./utils.js"
22
-
23
- export type validateAst<ast, $, args> = ast extends string
24
- ? validateStringAst<ast, $>
25
- : ast extends PostfixExpression<infer operator, infer operand>
26
- ? operator extends "[]"
27
- ? validateAst<operand, $, args>
28
- : never
29
- : ast extends InfixExpression<infer operator, infer l, infer r>
30
- ? operator extends "&" | "|"
31
- ? validateInfix<ast, $, args>
32
- : operator extends Comparator
33
- ? validateRange<l, operator, r, $, args>
34
- : operator extends "%"
35
- ? validateDivisor<l, $, args>
36
- : undefined
37
- : ast extends readonly ["keyof", infer operand]
38
- ? validateAst<operand, $, args>
39
- : ast extends GenericInstantiationAst
40
- ? validateGenericArgs<ast["2"], $, args>
41
- : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
42
-
43
- type writeUnexpectedExpressionMessage<expression extends string> =
44
- `Unexpectedly failed to parse the expression resulting from ${expression}`
45
-
46
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
47
- infer head,
48
- ...infer tail
49
- ]
50
- ? validateAst<head, $, args> extends ErrorMessage<infer message>
51
- ? ErrorMessage<message>
52
- : validateGenericArgs<tail, $, args>
53
- : undefined
54
-
55
- export const writeUnsatisfiableExpressionError = <expression extends string>(
56
- expression: expression
57
- ): writeUnsatisfiableExpressionError<expression> =>
58
- `${expression} results in an unsatisfiable type`
59
-
60
- export type writeUnsatisfiableExpressionError<expression extends string> =
61
- `${expression} results in an unsatisfiable type`
62
-
63
- type validateStringAst<def extends string, $> = def extends NumberLiteral<
64
- infer value
65
- >
66
- ? number extends value
67
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
68
- : undefined
69
- : def extends BigintLiteral<infer value>
70
- ? bigint extends value
71
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
72
- : undefined
73
- : def extends keyof $
74
- ? // these problems would've been caught during a fullStringParse, but it's most
75
- // efficient to check for them here in case the string was naively parsed
76
- $[def] extends GenericProps
77
- ? ErrorMessage<
78
- writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
79
- >
80
- : $[def] extends Module
81
- ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
82
- : undefined
83
- : def extends ErrorMessage
84
- ? def
85
- : undefined
86
-
87
- export type validateString<def extends string, $, args> = validateAst<
88
- parseString<def, $, args>,
89
- $,
90
- args
91
- > extends infer result extends ErrorMessage
92
- ? result extends Completion<infer text>
93
- ? text
94
- : result
95
- : def
96
-
97
- type validateInfix<ast extends InfixExpression, $, args> = validateAst<
98
- ast[0],
99
- $,
100
- args
101
- > extends ErrorMessage<infer message>
102
- ? ErrorMessage<message>
103
- : validateAst<ast[2], $, args> extends ErrorMessage<infer message>
104
- ? ErrorMessage<message>
105
- : undefined
package/parser/shared.ts DELETED
@@ -1,141 +0,0 @@
1
- import type { extend } from "@arktype/util"
2
- import type { validateDefinition } from "./definition.js"
3
- import { Scanner } from "./string/shift/scanner.js"
4
-
5
- type parsedKey<result extends KeyParseResult> = result
6
-
7
- type OptionalTuple<value> = readonly [value, "?"]
8
-
9
- type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
10
- kind: kind
11
- innerKey: string | symbol
12
- }
13
-
14
- type ValueParseResult<kind extends ParsedValueKind = ParsedValueKind> = {
15
- kind: kind
16
- innerValue: unknown
17
- }
18
-
19
- export type EntryParseResult<kind extends ParsedKeyKind = ParsedKeyKind> =
20
- extend<
21
- KeyParseResult<kind>,
22
- {
23
- innerValue: unknown
24
- }
25
- >
26
-
27
- type ParsedValueKind = "required" | "optional"
28
-
29
- type ParsedKeyKind = "required" | "optional" | "indexed"
30
-
31
- type parsedEntry<result extends EntryParseResult> = result
32
-
33
- export type OptionalValue<value> =
34
- | OptionalStringDefinition<value & string>
35
- | OptionalTuple<value>
36
-
37
- export type OptionalStringDefinition<name extends string = string> = `${name}?`
38
-
39
- export type IndexedKey<def extends string = string> = `[${def}]`
40
-
41
- export type validateObjectValue<def, $, args> = def extends OptionalTuple<
42
- infer value
43
- >
44
- ? readonly [validateObjectValueString<value, $, args>, "?"]
45
- : validateObjectValueString<def, $, args>
46
-
47
- type validateObjectValueString<def, $, args> =
48
- def extends OptionalStringDefinition<infer innerValue>
49
- ? `${validateDefinition<innerValue, $, args>}?`
50
- : validateDefinition<def, $, args>
51
-
52
- type DefinitionEntry = readonly [string | symbol, unknown]
53
-
54
- const getInnerValue = (value: unknown): ValueParseResult => {
55
- if (typeof value === "string") {
56
- if (value[value.length - 1] === "?") {
57
- return {
58
- kind: "optional",
59
- innerValue: value.slice(0, -1)
60
- }
61
- }
62
- } else if (Array.isArray(value)) {
63
- if (value.length === 2 && value[1] === "?") {
64
- return {
65
- kind: "optional",
66
- innerValue: getInnerValue(value[0]).innerValue
67
- }
68
- }
69
- }
70
- return {
71
- kind: "required",
72
- innerValue: value
73
- }
74
- }
75
-
76
- export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
77
- const keyParseResult: KeyParseResult =
78
- typeof key === "string" && key.at(-1) === "?"
79
- ? key.at(-2) === Scanner.escapeToken
80
- ? { innerKey: `${key.slice(0, -2)}?`, kind: "required" }
81
- : { innerKey: key.slice(0, -1), kind: "optional" }
82
- : { innerKey: key, kind: "required" }
83
- const valueParseResult = getInnerValue(value)
84
- return {
85
- innerKey: keyParseResult.innerKey,
86
- innerValue: valueParseResult.innerValue,
87
- kind:
88
- keyParseResult.kind === "indexed"
89
- ? "indexed"
90
- : valueParseResult.kind === "optional"
91
- ? "optional"
92
- : keyParseResult.kind
93
- }
94
- }
95
-
96
- export type parseEntry<
97
- keyDef extends PropertyKey,
98
- valueDef
99
- > = parseKey<keyDef> extends infer keyParseResult extends KeyParseResult
100
- ? valueDef extends OptionalValue<infer innerValue>
101
- ? parsedEntry<{
102
- kind: "optional"
103
- innerKey: keyParseResult["innerKey"]
104
- innerValue: innerValue extends OptionalStringDefinition<
105
- infer innerStringValue
106
- >
107
- ? innerStringValue
108
- : innerValue
109
- }>
110
- : parsedEntry<{
111
- kind: keyParseResult["kind"]
112
- innerKey: keyParseResult["innerKey"]
113
- innerValue: valueDef
114
- }>
115
- : never
116
-
117
- type parseKey<k> = k extends OptionalStringDefinition<infer inner>
118
- ? inner extends `${infer baseName}${Scanner.EscapeToken}`
119
- ? parsedKey<{
120
- kind: "required"
121
- innerKey: OptionalStringDefinition<baseName>
122
- }>
123
- : parsedKey<{
124
- kind: "optional"
125
- innerKey: inner
126
- }>
127
- : k extends IndexedKey<infer def>
128
- ? parsedKey<{
129
- kind: "indexed"
130
- innerKey: def
131
- }>
132
- : k extends `${Scanner.EscapeToken}${infer escapedIndexKey extends
133
- IndexedKey}`
134
- ? parsedKey<{
135
- kind: "required"
136
- innerKey: escapedIndexKey
137
- }>
138
- : parsedKey<{
139
- kind: "required"
140
- innerKey: k & (string | symbol)
141
- }>
@@ -1,181 +0,0 @@
1
- import type { Root } from "@arktype/schema"
2
- import {
3
- isKeyOf,
4
- throwInternalError,
5
- throwParseError,
6
- type requireKeys
7
- } from "@arktype/util"
8
- import type { ParseContext } from "../../../scope.js"
9
- import {
10
- invertedComparators,
11
- minComparators,
12
- type Comparator,
13
- type LimitLiteral
14
- } from "../shift/operator/bounds.js"
15
- import { Scanner } from "../shift/scanner.js"
16
- import {
17
- writeMultipleLeftBoundsMessage,
18
- writeOpenRangeMessage,
19
- writeUnclosedGroupMessage,
20
- writeUnmatchedGroupCloseMessage,
21
- writeUnpairableComparatorMessage,
22
- type OpenLeftBound,
23
- type StringifiablePrefixOperator
24
- } from "./shared.js"
25
-
26
- type BranchState = {
27
- prefixes: StringifiablePrefixOperator[]
28
- leftBound?: OpenLeftBound
29
- "&"?: Root
30
- "|"?: Root
31
- }
32
-
33
- export type DynamicStateWithRoot = requireKeys<DynamicState, "root">
34
-
35
- export class DynamicState {
36
- readonly scanner: Scanner
37
- root: Root | undefined
38
- branches: BranchState = {
39
- prefixes: []
40
- }
41
- finalizer: Scanner.FinalizingLookahead | undefined
42
- groups: BranchState[] = []
43
-
44
- constructor(
45
- def: string,
46
- public readonly ctx: ParseContext
47
- ) {
48
- this.scanner = new Scanner(def)
49
- }
50
-
51
- error(message: string) {
52
- return throwParseError(message)
53
- }
54
-
55
- hasRoot(): this is DynamicStateWithRoot {
56
- return this.root !== undefined
57
- }
58
-
59
- unsetRoot(): this["root"] {
60
- const value = this.root
61
- this.root = undefined
62
- return value
63
- }
64
-
65
- constrainRoot(...args: Parameters<Root["constrain"]>) {
66
- this.root = this.root!.constrain(...args)
67
- }
68
-
69
- setRoot(root: Root) {
70
- this.root = root
71
- }
72
-
73
- finalize(finalizer: Scanner.FinalizingLookahead) {
74
- if (this.groups.length) {
75
- return this.error(writeUnclosedGroupMessage(")"))
76
- }
77
- this.finalizeBranches()
78
- this.finalizer = finalizer
79
- }
80
-
81
- reduceLeftBound(limit: LimitLiteral, comparator: Comparator) {
82
- const invertedComparator = invertedComparators[comparator]
83
- if (!isKeyOf(invertedComparator, minComparators)) {
84
- return this.error(writeUnpairableComparatorMessage(comparator))
85
- }
86
- if (this.branches.leftBound) {
87
- return this.error(
88
- writeMultipleLeftBoundsMessage(
89
- this.branches.leftBound.limit,
90
- this.branches.leftBound.comparator,
91
- limit,
92
- invertedComparator
93
- )
94
- )
95
- }
96
- this.branches.leftBound = {
97
- comparator: invertedComparator,
98
- limit
99
- }
100
- }
101
-
102
- finalizeBranches() {
103
- this.assertRangeUnset()
104
- if (this.branches["|"]) {
105
- this.pushRootToBranch("|")
106
- this.root = this.branches["|"]
107
- } else if (this.branches["&"]) {
108
- this.pushRootToBranch("&")
109
- this.root = this.branches["&"]
110
- } else {
111
- this.applyPrefixes()
112
- }
113
- }
114
-
115
- finalizeGroup() {
116
- this.finalizeBranches()
117
- const topBranchState = this.groups.pop()
118
- if (!topBranchState) {
119
- return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
120
- }
121
- this.branches = topBranchState
122
- }
123
-
124
- addPrefix(prefix: StringifiablePrefixOperator) {
125
- this.branches.prefixes.push(prefix)
126
- }
127
-
128
- applyPrefixes() {
129
- while (this.branches.prefixes.length) {
130
- const lastPrefix = this.branches.prefixes.pop()!
131
- this.root =
132
- lastPrefix === "keyof"
133
- ? this.root!.keyof()
134
- : throwInternalError(`Unexpected prefix '${lastPrefix}'`)
135
- }
136
- }
137
-
138
- pushRootToBranch(token: "|" | "&") {
139
- this.assertRangeUnset()
140
- this.applyPrefixes()
141
- const root = this.root!
142
- this.branches["&"] = this.branches["&"]?.and(root) ?? root
143
- if (token === "|") {
144
- this.branches["|"] =
145
- this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"]
146
- delete this.branches["&"]
147
- }
148
- this.root = undefined
149
- }
150
-
151
- private assertRangeUnset() {
152
- if (this.branches.leftBound) {
153
- return this.error(
154
- writeOpenRangeMessage(
155
- this.branches.leftBound.limit,
156
- this.branches.leftBound.comparator
157
- )
158
- )
159
- }
160
- }
161
-
162
- reduceGroupOpen() {
163
- this.groups.push(this.branches)
164
- this.branches = {
165
- prefixes: []
166
- }
167
- }
168
-
169
- previousOperator() {
170
- return (
171
- this.branches.leftBound?.comparator ??
172
- this.branches.prefixes.at(-1) ??
173
- (this.branches["&"] ? "&" : this.branches["|"] ? "|" : undefined)
174
- )
175
- }
176
-
177
- shiftedByOne() {
178
- this.scanner.shift()
179
- return this
180
- }
181
- }
@@ -1,73 +0,0 @@
1
- import {
2
- invertedComparators,
3
- type Comparator,
4
- type InvertedComparators,
5
- type LimitLiteral,
6
- type MinComparator
7
- } from "../shift/operator/bounds.js"
8
-
9
- export type StringifiablePrefixOperator = "keyof"
10
-
11
- export type OpenLeftBound = { limit: LimitLiteral; comparator: MinComparator }
12
-
13
- export const writeUnmatchedGroupCloseMessage = <unscanned extends string>(
14
- unscanned: unscanned
15
- ): writeUnmatchedGroupCloseMessage<unscanned> =>
16
- `Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`) as any}`
17
-
18
- export type writeUnmatchedGroupCloseMessage<unscanned extends string> =
19
- `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`
20
-
21
- export const writeUnclosedGroupMessage = <missingChar extends string>(
22
- missingChar: missingChar
23
- ): writeUnclosedGroupMessage<missingChar> => `Missing ${missingChar}`
24
-
25
- export type writeUnclosedGroupMessage<missingChar extends string> =
26
- `Missing ${missingChar}`
27
-
28
- export const writeOpenRangeMessage = <
29
- min extends LimitLiteral,
30
- comparator extends MinComparator
31
- >(
32
- min: min,
33
- comparator: comparator
34
- ): writeOpenRangeMessage<min, comparator> =>
35
- `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
36
-
37
- export type writeOpenRangeMessage<
38
- min extends LimitLiteral,
39
- comparator extends MinComparator
40
- > = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
41
-
42
- export type writeUnpairableComparatorMessage<comparator extends Comparator> =
43
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
44
-
45
- export const writeUnpairableComparatorMessage = <comparator extends Comparator>(
46
- comparator: comparator
47
- ): writeUnpairableComparatorMessage<comparator> =>
48
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
49
-
50
- export const writeMultipleLeftBoundsMessage = <
51
- openLimit extends LimitLiteral,
52
- openComparator extends MinComparator,
53
- limit extends LimitLiteral,
54
- comparator extends MinComparator
55
- >(
56
- openLimit: openLimit,
57
- openComparator: openComparator,
58
- limit: limit,
59
- comparator: comparator
60
- ): writeMultipleLeftBoundsMessage<
61
- openLimit,
62
- openComparator,
63
- limit,
64
- comparator
65
- > =>
66
- `An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`
67
-
68
- export type writeMultipleLeftBoundsMessage<
69
- openLimit extends LimitLiteral,
70
- openComparator extends MinComparator,
71
- limit extends LimitLiteral,
72
- comparator extends MinComparator
73
- > = `An expression may have at most one left bound (parsed ${openLimit}${InvertedComparators[openComparator]}, ${limit}${InvertedComparators[comparator]})`