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