arktype 2.0.0-dev.10 → 2.0.0-dev.12

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 (146) hide show
  1. package/out/api.d.ts +6 -0
  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 +7 -6
  9. package/out/module.js +2 -3
  10. package/out/parser/definition.d.ts +11 -9
  11. package/out/parser/definition.js +2 -2
  12. package/out/parser/objectLiteral.d.ts +8 -13
  13. package/out/parser/objectLiteral.js +20 -23
  14. package/out/parser/semantic/bounds.d.ts +3 -3
  15. package/out/parser/semantic/divisor.d.ts +2 -2
  16. package/out/parser/semantic/infer.d.ts +1 -1
  17. package/out/parser/semantic/validate.d.ts +6 -4
  18. package/out/parser/string/reduce/dynamic.d.ts +6 -6
  19. package/out/parser/string/reduce/shared.d.ts +16 -16
  20. package/out/parser/string/reduce/shared.js +4 -2
  21. package/out/parser/string/shift/operand/date.d.ts +2 -2
  22. package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
  23. package/out/parser/string/shift/operand/enclosed.js +2 -1
  24. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  25. package/out/parser/string/shift/operand/unenclosed.d.ts +9 -7
  26. package/out/parser/string/shift/operand/unenclosed.js +2 -2
  27. package/out/parser/string/shift/operator/bounds.d.ts +5 -5
  28. package/out/parser/string/shift/operator/bounds.js +3 -3
  29. package/out/parser/string/shift/operator/divisor.d.ts +1 -1
  30. package/out/parser/string/shift/operator/operator.d.ts +1 -1
  31. package/out/parser/string/string.d.ts +2 -2
  32. package/out/parser/tuple.d.ts +11 -11
  33. package/out/parser/tuple.js +3 -3
  34. package/out/scope.d.ts +15 -13
  35. package/out/scope.js +7 -7
  36. package/out/type.d.ts +4 -4
  37. package/out/type.js +3 -3
  38. package/package.json +8 -10
  39. package/__tests__/array.test.ts +0 -391
  40. package/__tests__/badDefinitionType.test.ts +0 -58
  41. package/__tests__/basis.test.ts +0 -50
  42. package/__tests__/bounds.test.ts +0 -318
  43. package/__tests__/cast.test.ts +0 -37
  44. package/__tests__/completions.test.ts +0 -51
  45. package/__tests__/config.test.ts +0 -96
  46. package/__tests__/dateLiteral.test.ts +0 -36
  47. package/__tests__/declared.test.ts +0 -124
  48. package/__tests__/discrimination.test.ts +0 -74
  49. package/__tests__/divisor.test.ts +0 -114
  50. package/__tests__/enclosed.test.ts +0 -107
  51. package/__tests__/examples.test.ts +0 -56
  52. package/__tests__/expressions.test.ts +0 -210
  53. package/__tests__/generateBenchData.ts +0 -89
  54. package/__tests__/generated/cyclic.ts +0 -3058
  55. package/__tests__/generic.test.ts +0 -313
  56. package/__tests__/group.test.ts +0 -85
  57. package/__tests__/hkt.test.ts +0 -22
  58. package/__tests__/imports.test.ts +0 -126
  59. package/__tests__/instanceof.test.ts +0 -104
  60. package/__tests__/intersection.test.ts +0 -108
  61. package/__tests__/keyTraversal.test.ts +0 -77
  62. package/__tests__/keyof.test.ts +0 -150
  63. package/__tests__/keywords.test.ts +0 -214
  64. package/__tests__/literal.test.ts +0 -90
  65. package/__tests__/match.bench.ts +0 -31
  66. package/__tests__/match.test.ts +0 -234
  67. package/__tests__/methods.bench.ts +0 -22
  68. package/__tests__/narrow.test.ts +0 -100
  69. package/__tests__/object.bench.ts +0 -28
  70. package/__tests__/objectLiteral.test.ts +0 -442
  71. package/__tests__/operand.bench.ts +0 -45
  72. package/__tests__/operator.bench.ts +0 -130
  73. package/__tests__/pipe.test.ts +0 -501
  74. package/__tests__/realWorld.test.ts +0 -63
  75. package/__tests__/regex.test.ts +0 -68
  76. package/__tests__/scope.bench.ts +0 -39
  77. package/__tests__/scope.test.ts +0 -374
  78. package/__tests__/serialization.test.ts +0 -19
  79. package/__tests__/string.test.ts +0 -52
  80. package/__tests__/submodule.test.ts +0 -86
  81. package/__tests__/this.test.ts +0 -83
  82. package/__tests__/thunk.test.ts +0 -86
  83. package/__tests__/traverse.test.ts +0 -141
  84. package/__tests__/tsGenerics.test.ts +0 -12
  85. package/__tests__/type.test.ts +0 -36
  86. package/__tests__/typeReference.test.ts +0 -39
  87. package/__tests__/unenclosed.test.ts +0 -136
  88. package/__tests__/union.test.ts +0 -214
  89. package/ark.ts +0 -47
  90. package/generic.ts +0 -69
  91. package/main.ts +0 -6
  92. package/match.ts +0 -218
  93. package/module.ts +0 -15
  94. package/out/ark.js.map +0 -1
  95. package/out/generic.js.map +0 -1
  96. package/out/main.js.map +0 -1
  97. package/out/match.js.map +0 -1
  98. package/out/module.js.map +0 -1
  99. package/out/parser/definition.js.map +0 -1
  100. package/out/parser/generic.js.map +0 -1
  101. package/out/parser/objectLiteral.js.map +0 -1
  102. package/out/parser/semantic/bounds.js.map +0 -1
  103. package/out/parser/semantic/divisor.js.map +0 -1
  104. package/out/parser/semantic/infer.js.map +0 -1
  105. package/out/parser/semantic/utils.js.map +0 -1
  106. package/out/parser/semantic/validate.js.map +0 -1
  107. package/out/parser/string/reduce/dynamic.js.map +0 -1
  108. package/out/parser/string/reduce/shared.js.map +0 -1
  109. package/out/parser/string/reduce/static.js.map +0 -1
  110. package/out/parser/string/shift/operand/date.js.map +0 -1
  111. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  112. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  113. package/out/parser/string/shift/operand/operand.js.map +0 -1
  114. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  115. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  116. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  117. package/out/parser/string/shift/operator/operator.js.map +0 -1
  118. package/out/parser/string/shift/scanner.js.map +0 -1
  119. package/out/parser/string/string.js.map +0 -1
  120. package/out/parser/tuple.js.map +0 -1
  121. package/out/scope.js.map +0 -1
  122. package/out/type.js.map +0 -1
  123. package/parser/definition.ts +0 -143
  124. package/parser/generic.ts +0 -66
  125. package/parser/objectLiteral.ts +0 -233
  126. package/parser/semantic/bounds.ts +0 -62
  127. package/parser/semantic/divisor.ts +0 -11
  128. package/parser/semantic/infer.ts +0 -141
  129. package/parser/semantic/utils.ts +0 -15
  130. package/parser/semantic/validate.ts +0 -117
  131. package/parser/string/reduce/dynamic.ts +0 -208
  132. package/parser/string/reduce/shared.ts +0 -99
  133. package/parser/string/reduce/static.ts +0 -296
  134. package/parser/string/shift/operand/date.ts +0 -55
  135. package/parser/string/shift/operand/enclosed.ts +0 -123
  136. package/parser/string/shift/operand/genericArgs.ts +0 -128
  137. package/parser/string/shift/operand/operand.ts +0 -42
  138. package/parser/string/shift/operand/unenclosed.ts +0 -217
  139. package/parser/string/shift/operator/bounds.ts +0 -270
  140. package/parser/string/shift/operator/divisor.ts +0 -33
  141. package/parser/string/shift/operator/operator.ts +0 -71
  142. package/parser/string/shift/scanner.ts +0 -210
  143. package/parser/string/string.ts +0 -85
  144. package/parser/tuple.ts +0 -542
  145. package/scope.ts +0 -291
  146. package/type.ts +0 -266
@@ -1,270 +0,0 @@
1
- import {
2
- type BoundKind,
3
- type DateLiteral,
4
- type LimitLiteral,
5
- type NodeDef,
6
- type RawSchema,
7
- internalKeywords,
8
- jsObjects,
9
- tsKeywords,
10
- writeUnboundableMessage
11
- } from "@arktype/schema"
12
- import { isKeyOf, type keySet, throwParseError } from "@arktype/util"
13
- import type { astToString } from "../../../semantic/utils.js"
14
- import type {
15
- DynamicState,
16
- DynamicStateWithRoot
17
- } from "../../reduce/dynamic.js"
18
- import {
19
- type Comparator,
20
- type InvertedComparators,
21
- type MaxComparator,
22
- type OpenLeftBound,
23
- invertedComparators,
24
- maxComparators,
25
- writeUnpairableComparatorMessage
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
- ): void => {
36
- const comparator = shiftComparator(s, start)
37
- if (s.root.hasKind("unit")) {
38
- if (typeof s.root.unit === "number") {
39
- s.reduceLeftBound(s.root.unit, comparator)
40
- s.unsetRoot()
41
- return
42
- }
43
- if (s.root.unit instanceof Date) {
44
- const literal = `d'${
45
- s.root.description ?? s.root.unit.toISOString()
46
- }'` as const
47
- s.unsetRoot()
48
- s.reduceLeftBound(literal, comparator)
49
- return
50
- }
51
- }
52
- return parseRightBound(s, comparator)
53
- }
54
-
55
- export type parseBound<
56
- s extends StaticState,
57
- start extends ComparatorStartChar,
58
- unscanned extends string,
59
- $,
60
- args
61
- > =
62
- shiftComparator<start, unscanned> extends infer shiftResultOrError ?
63
- shiftResultOrError extends (
64
- Scanner.shiftResult<
65
- infer comparator extends Comparator,
66
- infer nextUnscanned
67
- >
68
- ) ?
69
- s["root"] extends `${infer limit extends LimitLiteral}` ?
70
- state.reduceLeftBound<s, limit, comparator, nextUnscanned>
71
- : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args>
72
- : shiftResultOrError
73
- : never
74
-
75
- const oneCharComparators = {
76
- "<": true,
77
- ">": true
78
- } as const
79
-
80
- type OneCharComparator = keyof typeof oneCharComparators
81
-
82
- export type ComparatorStartChar =
83
- Comparator extends `${infer char}${string}` ? char : never
84
-
85
- export const comparatorStartChars: keySet<ComparatorStartChar> = {
86
- "<": 1,
87
- ">": 1,
88
- "=": 1
89
- }
90
-
91
- const shiftComparator = (
92
- s: DynamicState,
93
- start: ComparatorStartChar
94
- ): Comparator =>
95
- s.scanner.lookaheadIs("=") ? `${start}${s.scanner.shift()}`
96
- : isKeyOf(start, oneCharComparators) ? start
97
- : s.error(singleEqualsMessage)
98
-
99
- type shiftComparator<
100
- start extends ComparatorStartChar,
101
- unscanned extends string
102
- > =
103
- unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned]
104
- : start extends OneCharComparator ? [start, unscanned]
105
- : state.error<singleEqualsMessage>
106
-
107
- export const writeIncompatibleRangeMessage = (l: BoundKind, r: BoundKind) =>
108
- `Bound kinds ${l} and ${r} are incompatible` as const
109
-
110
- export const getBoundKinds = (
111
- comparator: Comparator,
112
- limit: LimitLiteral,
113
- root: RawSchema,
114
- boundKind: BoundExpressionKind
115
- ): BoundKind[] => {
116
- if (root.extends(tsKeywords.number)) {
117
- if (typeof limit !== "number") {
118
- return throwParseError(
119
- writeInvalidLimitMessage(comparator, limit, boundKind)
120
- )
121
- }
122
- return (
123
- comparator === "==" ? ["min", "max"]
124
- : comparator[0] === ">" ? ["min"]
125
- : ["max"]
126
- )
127
- }
128
- if (root.extends(internalKeywords.lengthBoundable)) {
129
- if (typeof limit !== "number") {
130
- return throwParseError(
131
- writeInvalidLimitMessage(comparator, limit, boundKind)
132
- )
133
- }
134
- return (
135
- comparator === "==" ? ["minLength", "maxLength"]
136
- : comparator[0] === ">" ? ["minLength"]
137
- : ["maxLength"]
138
- )
139
- }
140
- if (root.extends(jsObjects.Date)) {
141
- // allow either numeric or date limits
142
- return (
143
- comparator === "==" ? ["after", "before"]
144
- : comparator[0] === ">" ? ["after"]
145
- : ["before"]
146
- )
147
- }
148
- return throwParseError(writeUnboundableMessage(root.expression))
149
- }
150
-
151
- export const singleEqualsMessage =
152
- "= is not a valid comparator. Use == to check for equality"
153
- type singleEqualsMessage = typeof singleEqualsMessage
154
-
155
- const openLeftBoundToSchema = (
156
- leftBound: OpenLeftBound
157
- ): NodeDef<BoundKind> => ({
158
- rule:
159
- isDateLiteral(leftBound.limit) ?
160
- extractDateLiteralSource(leftBound.limit)
161
- : leftBound.limit,
162
- exclusive: leftBound.comparator.length === 1
163
- })
164
-
165
- export const parseRightBound = (
166
- s: DynamicStateWithRoot,
167
- comparator: Comparator
168
- ): void => {
169
- // store the node that will be bounded
170
- const previousRoot = s.unsetRoot()
171
- const previousScannerIndex = s.scanner.location
172
- s.parseOperand()
173
- const limitNode = s.unsetRoot()
174
- // after parsing the next operand, use the locations to get the
175
- // token from which it was parsed
176
- const limitToken = s.scanner.sliceChars(
177
- previousScannerIndex,
178
- s.scanner.location
179
- )
180
- s.root = previousRoot
181
- if (
182
- !limitNode.hasKind("unit") ||
183
- (typeof limitNode.unit !== "number" && !(limitNode.unit instanceof Date))
184
- )
185
- return s.error(writeInvalidLimitMessage(comparator, limitToken, "right"))
186
-
187
- const limit = limitNode.unit
188
- // apply the newly-parsed right bound
189
- const exclusive = comparator.length === 1
190
- // if the comparator is ==, both the min and max of that pair will be applied
191
- for (const kind of getBoundKinds(
192
- comparator,
193
- typeof limit === "number" ? limit : (limitToken as DateLiteral),
194
- previousRoot,
195
- "right"
196
- ))
197
- s.constrainRoot(kind, { rule: limit, exclusive })
198
-
199
- if (!s.branches.leftBound) return
200
-
201
- // if there's an open left bound, perform additional validation and apply it
202
- if (!isKeyOf(comparator, maxComparators))
203
- return s.error(writeUnpairableComparatorMessage(comparator))
204
-
205
- const lowerBoundKind = getBoundKinds(
206
- s.branches.leftBound.comparator,
207
- s.branches.leftBound.limit,
208
- previousRoot,
209
- "left"
210
- )
211
- s.constrainRoot(
212
- lowerBoundKind[0],
213
- openLeftBoundToSchema(s.branches.leftBound)
214
- )
215
- s.branches.leftBound = null
216
- }
217
-
218
- export type parseRightBound<
219
- s extends StaticState,
220
- comparator extends Comparator,
221
- $,
222
- args
223
- > =
224
- parseOperand<s, $, args> extends infer nextState extends StaticState ?
225
- nextState["root"] extends `${infer limit extends LimitLiteral}` ?
226
- s["branches"]["leftBound"] extends {} ?
227
- comparator extends MaxComparator ?
228
- state.reduceRange<
229
- s,
230
- s["branches"]["leftBound"]["limit"],
231
- s["branches"]["leftBound"]["comparator"],
232
- comparator,
233
- limit,
234
- nextState["unscanned"]
235
- >
236
- : state.error<writeUnpairableComparatorMessage<comparator>>
237
- : state.reduceSingleBound<s, comparator, limit, nextState["unscanned"]>
238
- : state.error<
239
- writeInvalidLimitMessage<
240
- comparator,
241
- astToString<nextState["root"]>,
242
- "right"
243
- >
244
- >
245
- : never
246
-
247
- export const writeInvalidLimitMessage = <
248
- comparator extends Comparator,
249
- limit extends string | number,
250
- boundKind extends BoundExpressionKind
251
- >(
252
- comparator: comparator,
253
- limit: limit,
254
- boundKind: boundKind
255
- ): writeInvalidLimitMessage<comparator, limit, boundKind> =>
256
- `Comparator ${
257
- boundKind === "left" ? invertedComparators[comparator] : (comparator as any)
258
- } must be ${
259
- boundKind === "left" ? "preceded" : ("followed" as any)
260
- } by a corresponding literal (was '${limit}')`
261
-
262
- export type writeInvalidLimitMessage<
263
- comparator extends Comparator,
264
- limit extends string | number,
265
- boundKind extends BoundExpressionKind
266
- > = `Comparator ${boundKind extends "left" ? InvertedComparators[comparator]
267
- : comparator} must be ${boundKind extends "left" ? "preceded"
268
- : "followed"} by a corresponding literal (was '${limit}')`
269
-
270
- export type BoundExpressionKind = "left" | "right"
@@ -1,33 +0,0 @@
1
- import { tryParseInteger } 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): void => {
7
- const divisorToken = s.scanner.shiftUntilNextTerminator()
8
- const divisor = tryParseInteger(divisorToken, {
9
- errorOnFail: writeInvalidDivisorMessage(divisorToken)
10
- })
11
- if (divisor === 0) s.error(writeInvalidDivisorMessage(0))
12
-
13
- s.root = s.root.constrain("divisor", divisor)
14
- }
15
-
16
- export type parseDivisor<s extends StaticState, unscanned extends string> =
17
- Scanner.shiftUntilNextTerminator<Scanner.skipWhitespace<unscanned>> extends (
18
- Scanner.shiftResult<infer scanned, infer nextUnscanned>
19
- ) ?
20
- scanned extends `${infer divisor extends number}` ?
21
- divisor extends 0 ?
22
- state.error<writeInvalidDivisorMessage<0>>
23
- : state.setRoot<s, [s["root"], "%", divisor], nextUnscanned>
24
- : state.error<writeInvalidDivisorMessage<scanned>>
25
- : never
26
-
27
- export const writeInvalidDivisorMessage = <divisor extends string | number>(
28
- divisor: divisor
29
- ): writeInvalidDivisorMessage<divisor> =>
30
- `% operator must be followed by a non-zero integer literal (was ${divisor})`
31
-
32
- export type writeInvalidDivisorMessage<divisor extends string | number> =
33
- `% operator must be followed by a non-zero integer literal (was ${divisor})`
@@ -1,71 +0,0 @@
1
- import { isKeyOf } from "@arktype/util"
2
- import type { DynamicStateWithRoot } from "../../reduce/dynamic.js"
3
- import type { StaticState, state } 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 (
15
- lookahead === "" ? s.finalize("")
16
- : lookahead === "[" ?
17
- s.scanner.shift() === "]" ?
18
- s.setRoot(s.root.array())
19
- : s.error(incompleteArrayTokenMessage)
20
- : lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead)
21
- : lookahead === ")" ? s.finalizeGroup()
22
- : Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ?
23
- s.finalize(lookahead)
24
- : isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
25
- : lookahead === "%" ? parseDivisor(s)
26
- : lookahead === " " ? parseOperator(s)
27
- : s.error(writeUnexpectedCharacterMessage(lookahead))
28
- )
29
- }
30
-
31
- export type parseOperator<s extends StaticState, $, args> =
32
- s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ?
33
- lookahead extends "[" ?
34
- unscanned extends Scanner.shift<"]", infer nextUnscanned> ?
35
- state.setRoot<s, [s["root"], "[]"], nextUnscanned>
36
- : state.error<incompleteArrayTokenMessage>
37
- : lookahead extends "|" | "&" ? state.reduceBranch<s, lookahead, unscanned>
38
- : lookahead extends ")" ? state.finalizeGroup<s, unscanned>
39
- : Scanner.lookaheadIsFinalizing<lookahead, unscanned> extends true ?
40
- state.finalize<
41
- state.scanTo<s, unscanned>,
42
- lookahead & Scanner.FinalizingLookahead
43
- >
44
- : lookahead extends ComparatorStartChar ?
45
- parseBound<s, lookahead, unscanned, $, args>
46
- : lookahead extends "%" ? parseDivisor<s, unscanned>
47
- : lookahead extends Scanner.WhiteSpaceToken ?
48
- parseOperator<state.scanTo<s, unscanned>, $, args>
49
- : state.error<writeUnexpectedCharacterMessage<lookahead>>
50
- : state.finalize<s, "">
51
-
52
- export const writeUnexpectedCharacterMessage = <
53
- char extends string,
54
- shouldBe extends string
55
- >(
56
- char: char,
57
- shouldBe: shouldBe = "" as shouldBe
58
- ): writeUnexpectedCharacterMessage<char, shouldBe> =>
59
- `'${char}' is not allowed here${
60
- shouldBe && (` (should be ${shouldBe})` as any)
61
- }`
62
-
63
- export type writeUnexpectedCharacterMessage<
64
- char extends string,
65
- shouldBe extends string = ""
66
- > = `'${char}' is not allowed here${shouldBe extends "" ? ""
67
- : ` (should be ${shouldBe})`}`
68
-
69
- export const incompleteArrayTokenMessage = `Missing expected ']'`
70
-
71
- type incompleteArrayTokenMessage = typeof incompleteArrayTokenMessage
@@ -1,210 +0,0 @@
1
- import { type Dict, isKeyOf } from "@arktype/util"
2
- import type { Comparator } from "../reduce/shared.js"
3
-
4
- export class Scanner<Lookahead extends string = string> {
5
- private chars: string[]
6
- private i: number
7
-
8
- constructor(def: string) {
9
- this.chars = [...def]
10
- this.i = 0
11
- }
12
-
13
- /** Get lookahead and advance scanner by one */
14
- shift(): Lookahead {
15
- return (this.chars[this.i++] ?? "") as Lookahead
16
- }
17
-
18
- get lookahead(): Lookahead {
19
- return (this.chars[this.i] ?? "") as Lookahead
20
- }
21
-
22
- get length(): number {
23
- return this.chars.length
24
- }
25
-
26
- shiftUntil(condition: Scanner.UntilCondition): string {
27
- let shifted = ""
28
- while (this.lookahead) {
29
- if (condition(this, shifted)) {
30
- if (shifted[shifted.length - 1] === Scanner.escapeToken)
31
- shifted = shifted.slice(0, -1)
32
- else break
33
- }
34
- shifted += this.shift()
35
- }
36
- return shifted
37
- }
38
-
39
- shiftUntilNextTerminator(): string {
40
- this.shiftUntilNonWhitespace()
41
- return this.shiftUntil(Scanner.lookaheadIsTerminator)
42
- }
43
-
44
- shiftUntilNonWhitespace(): string {
45
- return this.shiftUntil(Scanner.lookaheadIsNotWhitespace)
46
- }
47
-
48
- jumpToIndex(i: number): void {
49
- this.i = i < 0 ? this.length + i : i
50
- }
51
-
52
- get location(): number {
53
- return this.i
54
- }
55
-
56
- get unscanned(): string {
57
- return this.chars.slice(this.i, this.length).join("")
58
- }
59
-
60
- get scanned(): string {
61
- return this.chars.slice(0, this.i).join("")
62
- }
63
-
64
- sliceChars(start: number, end?: number): string {
65
- return this.chars.slice(start, end).join("")
66
- }
67
-
68
- lookaheadIs<Char extends Lookahead>(char: Char): this is Scanner<Char> {
69
- return this.lookahead === char
70
- }
71
-
72
- lookaheadIsIn<Tokens extends Dict>(
73
- tokens: Tokens
74
- ): this is Scanner<Extract<keyof Tokens, string>> {
75
- return this.lookahead in tokens
76
- }
77
- }
78
-
79
- export namespace Scanner {
80
- export type UntilCondition = (scanner: Scanner, shifted: string) => boolean
81
-
82
- export type OnInputEndFn = (scanner: Scanner, shifted: string) => string
83
-
84
- export type ShiftUntilOptions = {
85
- onInputEnd?: OnInputEndFn
86
- }
87
-
88
- export const lookaheadIsTerminator: UntilCondition = (scanner: Scanner) =>
89
- scanner.lookahead in terminatingChars
90
-
91
- export const lookaheadIsNotWhitespace: UntilCondition = (scanner: Scanner) =>
92
- !(scanner.lookahead in whiteSpaceTokens)
93
-
94
- export const terminatingChars = {
95
- "<": true,
96
- ">": true,
97
- "=": true,
98
- "|": true,
99
- "&": true,
100
- ")": true,
101
- "[": true,
102
- "%": true,
103
- " ": true,
104
- ",": true
105
- } as const
106
-
107
- export type TerminatingChar = keyof typeof terminatingChars
108
-
109
- export const finalizingLookaheads = {
110
- ">": true,
111
- ",": true,
112
- "": true
113
- } as const
114
-
115
- export type FinalizingLookahead = keyof typeof finalizingLookaheads
116
-
117
- export type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>"
118
-
119
- export type PostfixToken = "[]"
120
-
121
- export type OperatorToken = InfixToken | PostfixToken
122
-
123
- export const escapeToken = "\\"
124
-
125
- export type EscapeToken = typeof escapeToken
126
-
127
- export const whiteSpaceTokens = {
128
- " ": true,
129
- "\n": true,
130
- "\t": true
131
- } as const
132
-
133
- export type WhiteSpaceToken = keyof typeof whiteSpaceTokens
134
-
135
- export const lookaheadIsFinalizing = (
136
- lookahead: string,
137
- unscanned: string
138
- ): lookahead is ">" | "," =>
139
- lookahead === ">" ?
140
- unscanned[0] === "=" ?
141
- // >== would only occur in an expression like Array<number>==5
142
- // otherwise, >= would only occur as part of a bound like number>=5
143
- unscanned[1] === "="
144
- // if > is the end of a generic instantiation, the next token will be an operator or the end of the string
145
- : unscanned.trimStart() === "" ||
146
- isKeyOf(unscanned.trimStart()[0], Scanner.terminatingChars)
147
- // if the lookahead is a finalizing token but not >, it's unambiguously a finalizer (currently just ",")
148
- : lookahead === ","
149
-
150
- export type lookaheadIsFinalizing<
151
- lookahead extends string,
152
- unscanned extends string
153
- > =
154
- lookahead extends ">" ?
155
- unscanned extends `=${infer nextUnscanned}` ?
156
- nextUnscanned extends `=${string}` ?
157
- true
158
- : false
159
- : Scanner.skipWhitespace<unscanned> extends (
160
- "" | `${TerminatingChar}${string}`
161
- ) ?
162
- true
163
- : false
164
- : lookahead extends "," ? true
165
- : false
166
-
167
- export type shift<
168
- lookahead extends string,
169
- unscanned extends string
170
- > = `${lookahead}${unscanned}`
171
-
172
- export type shiftUntil<
173
- unscanned extends string,
174
- terminator extends string,
175
- scanned extends string = ""
176
- > =
177
- unscanned extends shift<infer lookahead, infer nextUnscanned> ?
178
- lookahead extends terminator ?
179
- scanned extends `${infer base}${EscapeToken}` ?
180
- shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`>
181
- : [scanned, unscanned]
182
- : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`>
183
- : [scanned, ""]
184
-
185
- export type shiftUntilNot<
186
- unscanned extends string,
187
- nonTerminator extends string,
188
- scanned extends string = ""
189
- > =
190
- unscanned extends shift<infer lookahead, infer nextUnscanned> ?
191
- lookahead extends nonTerminator ?
192
- shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`>
193
- : [scanned, unscanned]
194
- : [scanned, ""]
195
-
196
- export type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<
197
- unscanned,
198
- TerminatingChar
199
- >
200
-
201
- export type skipWhitespace<unscanned extends string> = shiftUntilNot<
202
- unscanned,
203
- WhiteSpaceToken
204
- >[1]
205
-
206
- export type shiftResult<scanned extends string, unscanned extends string> = [
207
- scanned,
208
- unscanned
209
- ]
210
- }
@@ -1,85 +0,0 @@
1
- import type { RawSchema } from "@arktype/schema"
2
- import {
3
- type ErrorMessage,
4
- throwInternalError,
5
- throwParseError
6
- } from "@arktype/util"
7
- import type { inferAstRoot } from "../semantic/infer.js"
8
- import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.js"
9
- import type { StringifiablePrefixOperator } from "./reduce/shared.js"
10
- import type { StaticState, state } from "./reduce/static.js"
11
- import type { parseOperand } from "./shift/operand/operand.js"
12
- import {
13
- type parseOperator,
14
- writeUnexpectedCharacterMessage
15
- } from "./shift/operator/operator.js"
16
-
17
- /**
18
- * Try to parse the definition from right to left using the most common syntax.
19
- * This can be much more efficient for simple definitions.
20
- */
21
- export type parseString<def extends string, $, args> =
22
- def extends keyof $ ?
23
- // def could also be a generic reference here, in which case it will
24
- // fail semantic validation because it has no args
25
- def
26
- : def extends `${infer child}[]` ?
27
- child extends keyof $ ?
28
- [child, "[]"]
29
- : fullStringParse<state.initialize<def>, $, args>
30
- : fullStringParse<state.initialize<def>, $, args>
31
-
32
- export type inferString<def extends string, $, args> = inferAstRoot<
33
- parseString<def, $, args>,
34
- $,
35
- args
36
- >
37
-
38
- export type BaseCompletions<$, args, otherSuggestions extends string = never> =
39
- | (keyof $ & string)
40
- | (keyof args & string)
41
- | StringifiablePrefixOperator
42
- | otherSuggestions
43
-
44
- export const fullStringParse = (s: DynamicState): RawSchema => {
45
- s.parseOperand()
46
- const result = parseUntilFinalizer(s).root
47
- if (!result) {
48
- return throwInternalError(
49
- `Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`
50
- )
51
- }
52
- s.scanner.shiftUntilNonWhitespace()
53
- if (s.scanner.lookahead) {
54
- // throw a parse error if non-whitespace characters made it here without being parsed
55
- throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead))
56
- }
57
- return result
58
- }
59
-
60
- type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<
61
- parseUntilFinalizer<s, $, args>
62
- >
63
-
64
- export const parseUntilFinalizer = (s: DynamicState): DynamicStateWithRoot => {
65
- while (s.finalizer === undefined) next(s)
66
-
67
- return s as DynamicStateWithRoot
68
- }
69
-
70
- export type parseUntilFinalizer<s extends StaticState, $, args> =
71
- s["finalizer"] extends undefined ?
72
- parseUntilFinalizer<next<s, $, args>, $, args>
73
- : s
74
-
75
- const next = (s: DynamicState) =>
76
- s.hasRoot() ? s.parseOperator() : s.parseOperand()
77
-
78
- type next<s extends StaticState, $, args> =
79
- s["root"] extends undefined ? parseOperand<s, $, args>
80
- : parseOperator<s, $, args>
81
-
82
- export type extractFinalizedResult<s extends StaticState> =
83
- s["finalizer"] extends ErrorMessage ? s["finalizer"]
84
- : s["finalizer"] extends "" ? s["root"]
85
- : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>