arktype 1.1.0-dev-2 → 2.0.0-dev.0
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/__tests__/array.test.ts +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +7 -9
- package/match.ts +47 -68
- package/out/ark.d.ts +52 -0
- package/out/ark.d.ts.map +1 -0
- package/out/ark.js +19 -0
- package/out/ark.js.map +1 -0
- package/out/arkConfig.d.ts +2 -0
- package/out/arkConfig.d.ts.map +1 -0
- package/out/arkConfig.js +10 -0
- package/out/arkConfig.js.map +1 -0
- package/out/main.d.ts +7 -0
- package/out/main.d.ts.map +1 -0
- package/out/main.js +4 -0
- package/out/main.js.map +1 -0
- package/out/match.d.ts +48 -0
- package/out/match.d.ts.map +1 -0
- package/{dist → out}/match.js +0 -5
- package/out/match.js.map +1 -0
- package/{dist → out}/parser/definition.d.ts +3 -3
- package/out/parser/definition.d.ts.map +1 -0
- package/{dist → out}/parser/definition.js +8 -5
- package/out/parser/definition.js.map +1 -0
- package/out/parser/generic.d.ts +18 -0
- package/out/parser/generic.d.ts.map +1 -0
- package/out/parser/generic.js +19 -0
- package/out/parser/generic.js.map +1 -0
- package/{dist → out}/parser/objectLiteral.d.ts +14 -5
- package/out/parser/objectLiteral.d.ts.map +1 -0
- package/out/parser/objectLiteral.js +66 -0
- package/out/parser/objectLiteral.js.map +1 -0
- package/out/parser/semantic/bounds.d.ts +13 -0
- package/out/parser/semantic/bounds.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/bounds.js.map +1 -1
- package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
- package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/utils.d.ts +1 -1
- package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/validate.d.ts +3 -3
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/validate.js.map +1 -1
- package/{dist → out}/parser/shared.d.ts +7 -1
- package/out/parser/shared.d.ts.map +1 -0
- package/{dist → out}/parser/shared.js +7 -1
- package/out/parser/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
- package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
- package/{dist → out}/parser/string/reduce/shared.js +20 -1
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
- package/out/parser/string/shift/operand/enclosed.js.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.js +25 -0
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
- package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/out/parser/string/shift/operator/bounds.js +106 -0
- package/out/parser/string/shift/operator/bounds.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/{dist → out}/parser/string/string.d.ts +9 -11
- package/out/parser/string/string.d.ts.map +1 -0
- package/out/parser/string/string.js +23 -0
- package/out/parser/string/string.js.map +1 -0
- package/{dist → out}/parser/tuple.d.ts +9 -11
- package/out/parser/tuple.d.ts.map +1 -0
- package/{dist → out}/parser/tuple.js +17 -14
- package/out/parser/tuple.js.map +1 -0
- package/{dist → out}/scope.d.ts +16 -16
- package/out/scope.d.ts.map +1 -0
- package/{dist → out}/scope.js +27 -22
- package/out/scope.js.map +1 -0
- package/out/test.d.ts +2 -0
- package/out/test.d.ts.map +1 -0
- package/out/test.js +7 -0
- package/out/test.js.map +1 -0
- package/out/tsconfig.build.tsbuildinfo +1 -0
- package/{dist → out}/type.d.ts +23 -47
- package/out/type.d.ts.map +1 -0
- package/{dist → out}/type.js +27 -31
- package/out/type.js.map +1 -0
- package/out/util.d.ts +19 -0
- package/out/util.d.ts.map +1 -0
- package/out/util.js +7 -0
- package/out/util.js.map +1 -0
- package/package.json +13 -15
- package/parser/definition.ts +58 -55
- package/parser/generic.ts +17 -157
- package/parser/objectLiteral.ts +107 -30
- package/parser/semantic/bounds.ts +22 -24
- package/parser/semantic/divisor.ts +2 -2
- package/parser/semantic/intersections.ts +17 -18
- package/parser/semantic/semantic.ts +80 -43
- package/parser/semantic/utils.ts +7 -7
- package/parser/semantic/validate.ts +40 -36
- package/parser/shared.ts +28 -17
- package/parser/string/reduce/dynamic.ts +26 -11
- package/parser/string/reduce/shared.ts +35 -7
- package/parser/string/reduce/static.ts +20 -22
- package/parser/string/shift/operand/date.ts +1 -4
- package/parser/string/shift/operand/enclosed.ts +5 -6
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +21 -21
- package/parser/string/shift/operand/unenclosed.ts +54 -68
- package/parser/string/shift/operator/bounds.ts +99 -64
- package/parser/string/shift/operator/operator.ts +31 -31
- package/parser/string/shift/scanner.ts +7 -7
- package/parser/string/string.ts +29 -32
- package/parser/tuple.ts +87 -88
- package/scope.ts +81 -80
- package/type.ts +76 -132
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -11
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -8
- package/dist/main.js.map +0 -1
- package/dist/match.d.ts +0 -46
- package/dist/match.d.ts.map +0 -1
- package/dist/match.js.map +0 -1
- package/dist/parser/definition.d.ts.map +0 -1
- package/dist/parser/definition.js.map +0 -1
- package/dist/parser/generic.d.ts +0 -44
- package/dist/parser/generic.d.ts.map +0 -1
- package/dist/parser/generic.js +0 -46
- package/dist/parser/generic.js.map +0 -1
- package/dist/parser/objectLiteral.d.ts.map +0 -1
- package/dist/parser/objectLiteral.js +0 -38
- package/dist/parser/objectLiteral.js.map +0 -1
- package/dist/parser/semantic/bounds.d.ts +0 -13
- package/dist/parser/semantic/bounds.d.ts.map +0 -1
- package/dist/parser/semantic/intersections.d.ts.map +0 -1
- package/dist/parser/semantic/semantic.d.ts +0 -22
- package/dist/parser/semantic/semantic.d.ts.map +0 -1
- package/dist/parser/semantic/validate.d.ts.map +0 -1
- package/dist/parser/shared.d.ts.map +0 -1
- package/dist/parser/shared.js.map +0 -1
- package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/dist/parser/string/reduce/dynamic.js.map +0 -1
- package/dist/parser/string/reduce/shared.js.map +0 -1
- package/dist/parser/string/reduce/static.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/bounds.js +0 -95
- package/dist/parser/string/shift/operator/bounds.js.map +0 -1
- package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/dist/parser/string/shift/scanner.js.map +0 -1
- package/dist/parser/string/string.d.ts.map +0 -1
- package/dist/parser/string/string.js +0 -31
- package/dist/parser/string/string.js.map +0 -1
- package/dist/parser/tuple.d.ts.map +0 -1
- package/dist/parser/tuple.js.map +0 -1
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js.map +0 -1
- package/dist/scopes/ark.d.ts +0 -38
- package/dist/scopes/ark.d.ts.map +0 -1
- package/dist/scopes/ark.js +0 -24
- package/dist/scopes/ark.js.map +0 -1
- package/dist/scopes/jsObjects.d.ts +0 -19
- package/dist/scopes/jsObjects.d.ts.map +0 -1
- package/dist/scopes/jsObjects.js +0 -15
- package/dist/scopes/jsObjects.js.map +0 -1
- package/dist/scopes/parsing.d.ts +0 -18
- package/dist/scopes/parsing.d.ts.map +0 -1
- package/dist/scopes/parsing.js +0 -61
- package/dist/scopes/parsing.js.map +0 -1
- package/dist/scopes/tsGenerics.d.ts +0 -17
- package/dist/scopes/tsGenerics.d.ts.map +0 -1
- package/dist/scopes/tsGenerics.js +0 -13
- package/dist/scopes/tsGenerics.js.map +0 -1
- package/dist/scopes/tsKeywords.d.ts +0 -24
- package/dist/scopes/tsKeywords.d.ts.map +0 -1
- package/dist/scopes/tsKeywords.js +0 -20
- package/dist/scopes/tsKeywords.js.map +0 -1
- package/dist/scopes/utils/creditCard.d.ts +0 -3
- package/dist/scopes/utils/creditCard.d.ts.map +0 -1
- package/dist/scopes/utils/creditCard.js +0 -36
- package/dist/scopes/utils/creditCard.js.map +0 -1
- package/dist/scopes/utils/date.d.ts +0 -24
- package/dist/scopes/utils/date.d.ts.map +0 -1
- package/dist/scopes/utils/date.js +0 -53
- package/dist/scopes/utils/date.js.map +0 -1
- package/dist/scopes/validation.d.ts +0 -20
- package/dist/scopes/validation.d.ts.map +0 -1
- package/dist/scopes/validation.js +0 -57
- package/dist/scopes/validation.js.map +0 -1
- package/dist/type.d.ts.map +0 -1
- package/dist/type.js.map +0 -1
- package/scopes/ark.ts +0 -64
- package/scopes/jsObjects.ts +0 -29
- package/scopes/parsing.ts +0 -81
- package/scopes/tsGenerics.ts +0 -28
- package/scopes/tsKeywords.ts +0 -39
- package/scopes/utils/creditCard.ts +0 -37
- package/scopes/utils/date.ts +0 -108
- package/scopes/validation.ts +0 -81
- /package/{dist → out}/parser/semantic/bounds.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
- /package/{dist → out}/parser/semantic/intersections.js +0 -0
- /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
- /package/{dist → out}/parser/semantic/semantic.js +0 -0
- /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
- /package/{dist → out}/parser/semantic/utils.js +0 -0
- /package/{dist → out}/parser/semantic/utils.js.map +0 -0
- /package/{dist → out}/parser/semantic/validate.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
- /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
- /package/{dist → out}/parser/string/shift/scanner.js +0 -0
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
keywords,
|
|
3
|
+
type BoundKind,
|
|
4
|
+
type LimitSchemaValue,
|
|
5
|
+
type Schema,
|
|
6
|
+
type TypeNode
|
|
7
|
+
} from "@arktype/schema"
|
|
8
|
+
import {
|
|
9
|
+
isKeyOf,
|
|
10
|
+
throwParseError,
|
|
11
|
+
tryParseNumber,
|
|
12
|
+
type keySet
|
|
13
|
+
} from "@arktype/util"
|
|
3
14
|
import type { astToString } from "../../../semantic/utils.js"
|
|
4
15
|
import type {
|
|
5
16
|
DynamicState,
|
|
6
17
|
DynamicStateWithRoot
|
|
7
18
|
} from "../../reduce/dynamic.js"
|
|
8
19
|
import {
|
|
20
|
+
maxComparators,
|
|
9
21
|
writeUnpairableComparatorMessage,
|
|
22
|
+
type Comparator,
|
|
23
|
+
type LimitLiteral,
|
|
24
|
+
type MaxComparator,
|
|
10
25
|
type OpenLeftBound
|
|
11
26
|
} from "../../reduce/shared.js"
|
|
12
27
|
import type { StaticState, state } from "../../reduce/static.js"
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
isDateLiteral,
|
|
16
|
-
type DateLiteral
|
|
17
|
-
} from "../operand/date.js"
|
|
18
|
-
import { parseOperand } from "../operand/operand.js"
|
|
28
|
+
import { extractDateLiteralSource, isDateLiteral } from "../operand/date.js"
|
|
29
|
+
import type { parseOperand } from "../operand/operand.js"
|
|
19
30
|
import type { Scanner } from "../scanner.js"
|
|
20
31
|
|
|
21
32
|
export const parseBound = (
|
|
@@ -24,14 +35,14 @@ export const parseBound = (
|
|
|
24
35
|
) => {
|
|
25
36
|
const comparator = shiftComparator(s, start)
|
|
26
37
|
if (s.root.kind === "unit") {
|
|
27
|
-
if (typeof s.root.
|
|
38
|
+
if (typeof s.root.unit === "number") {
|
|
28
39
|
s.unsetRoot()
|
|
29
|
-
return s.reduceLeftBound(s.root.
|
|
40
|
+
return s.reduceLeftBound(s.root.unit, comparator)
|
|
30
41
|
}
|
|
31
|
-
if (s.root.
|
|
42
|
+
if (s.root.unit instanceof Date) {
|
|
32
43
|
s.unsetRoot()
|
|
33
44
|
const literal = `d'${
|
|
34
|
-
s.root.description ?? s.root.
|
|
45
|
+
s.root.description ?? s.root.unit.toISOString()
|
|
35
46
|
}'` as const
|
|
36
47
|
return s.reduceLeftBound(literal, comparator)
|
|
37
48
|
}
|
|
@@ -56,30 +67,6 @@ export type parseBound<
|
|
|
56
67
|
: shiftResultOrError
|
|
57
68
|
: never
|
|
58
69
|
|
|
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
70
|
const oneCharComparators = {
|
|
84
71
|
"<": true,
|
|
85
72
|
">": true
|
|
@@ -104,8 +91,8 @@ const shiftComparator = (
|
|
|
104
91
|
s.scanner.lookaheadIs("=")
|
|
105
92
|
? `${start}${s.scanner.shift()}`
|
|
106
93
|
: isKeyOf(start, oneCharComparators)
|
|
107
|
-
|
|
108
|
-
|
|
94
|
+
? start
|
|
95
|
+
: s.error(singleEqualsMessage)
|
|
109
96
|
|
|
110
97
|
type shiftComparator<
|
|
111
98
|
start extends ComparatorStartChar,
|
|
@@ -113,20 +100,73 @@ type shiftComparator<
|
|
|
113
100
|
> = unscanned extends `=${infer nextUnscanned}`
|
|
114
101
|
? [`${start}=`, nextUnscanned]
|
|
115
102
|
: start extends OneCharComparator
|
|
116
|
-
|
|
117
|
-
|
|
103
|
+
? [start, unscanned]
|
|
104
|
+
: state.error<singleEqualsMessage>
|
|
105
|
+
|
|
106
|
+
export const writeUnboundableMessage = <root extends string>(
|
|
107
|
+
root: root
|
|
108
|
+
): writeUnboundableMessage<root> =>
|
|
109
|
+
`Bounded expression ${root} must be a number, string, Array, or Date`
|
|
110
|
+
|
|
111
|
+
export type writeUnboundableMessage<root extends string> =
|
|
112
|
+
`Bounded expression ${root} must be a number, string, Array, or Date`
|
|
113
|
+
|
|
114
|
+
export const writeIncompatibleRangeMessage = (l: BoundKind, r: BoundKind) =>
|
|
115
|
+
`Bound kinds ${l} and ${r} are incompatible`
|
|
116
|
+
|
|
117
|
+
export const writeLimitMismatchMessage = (
|
|
118
|
+
root: string,
|
|
119
|
+
limitValue: LimitSchemaValue
|
|
120
|
+
) => `Limit '${limitValue}' cannot bound ${root}`
|
|
121
|
+
|
|
122
|
+
export const getBoundKinds = (
|
|
123
|
+
comparator: Comparator,
|
|
124
|
+
limit: LimitSchemaValue,
|
|
125
|
+
root: TypeNode
|
|
126
|
+
): BoundKind[] => {
|
|
127
|
+
if (root.extends(keywords.number)) {
|
|
128
|
+
if (typeof limit !== "number") {
|
|
129
|
+
return throwParseError(writeLimitMismatchMessage(root.toString(), limit))
|
|
130
|
+
}
|
|
131
|
+
return comparator === "=="
|
|
132
|
+
? ["min", "max"]
|
|
133
|
+
: comparator[0] === ">"
|
|
134
|
+
? ["min"]
|
|
135
|
+
: ["max"]
|
|
136
|
+
}
|
|
137
|
+
if (root.extends(keywords.string) || root.extends(keywords.Array)) {
|
|
138
|
+
if (typeof limit !== "number") {
|
|
139
|
+
return throwParseError(writeLimitMismatchMessage(root.toString(), limit))
|
|
140
|
+
}
|
|
141
|
+
return comparator === "=="
|
|
142
|
+
? ["minLength", "maxLength"]
|
|
143
|
+
: comparator[0] === ">"
|
|
144
|
+
? ["minLength"]
|
|
145
|
+
: ["maxLength"]
|
|
146
|
+
}
|
|
147
|
+
if (root.extends(keywords.Date)) {
|
|
148
|
+
// allow either numeric or date limits
|
|
149
|
+
return comparator === "=="
|
|
150
|
+
? ["after", "before"]
|
|
151
|
+
: comparator[0] === ">"
|
|
152
|
+
? ["after"]
|
|
153
|
+
: ["before"]
|
|
154
|
+
}
|
|
155
|
+
return throwParseError(writeUnboundableMessage(root.toString()))
|
|
156
|
+
}
|
|
118
157
|
|
|
119
158
|
export const singleEqualsMessage = `= is not a valid comparator. Use == to check for equality`
|
|
120
159
|
type singleEqualsMessage = typeof singleEqualsMessage
|
|
121
160
|
|
|
122
|
-
const openLeftBoundToSchema = (
|
|
123
|
-
|
|
161
|
+
const openLeftBoundToSchema = (
|
|
162
|
+
leftBound: OpenLeftBound
|
|
163
|
+
): Schema<BoundKind> => ({
|
|
164
|
+
limit: isDateLiteral(leftBound.limit)
|
|
124
165
|
? extractDateLiteralSource(leftBound.limit)
|
|
125
166
|
: leftBound.limit,
|
|
126
167
|
exclusive: leftBound.comparator.length === 1
|
|
127
168
|
})
|
|
128
169
|
|
|
129
|
-
// TODO: allow numeric limits for Dates?
|
|
130
170
|
export const parseRightBound = (
|
|
131
171
|
s: DynamicStateWithRoot,
|
|
132
172
|
comparator: Comparator
|
|
@@ -134,7 +174,7 @@ export const parseRightBound = (
|
|
|
134
174
|
// store the node that will be bounded
|
|
135
175
|
const previousRoot = s.unsetRoot()
|
|
136
176
|
const previousScannerIndex = s.scanner.location
|
|
137
|
-
parseOperand(
|
|
177
|
+
s.parseOperand()
|
|
138
178
|
// after parsing the next operand, use the locations to get the
|
|
139
179
|
// token from which it was parsed
|
|
140
180
|
const limitToken = s.scanner.sliceChars(
|
|
@@ -149,12 +189,9 @@ export const parseRightBound = (
|
|
|
149
189
|
: s.error(writeInvalidLimitMessage(comparator, limitToken, "right")))
|
|
150
190
|
// apply the newly-parsed right bound
|
|
151
191
|
const exclusive = comparator.length === 1
|
|
152
|
-
// if the comparator is ==, both
|
|
153
|
-
|
|
154
|
-
s.constrainRoot(
|
|
155
|
-
}
|
|
156
|
-
if (comparator[0] !== "<") {
|
|
157
|
-
s.constrainRoot("min", { min: limit, exclusive })
|
|
192
|
+
// if the comparator is ==, both the min and max of that pair will be applied
|
|
193
|
+
for (const kind of getBoundKinds(comparator, limit, previousRoot)) {
|
|
194
|
+
s.constrainRoot(kind, { limit, exclusive })
|
|
158
195
|
}
|
|
159
196
|
if (!s.branches.leftBound) {
|
|
160
197
|
return
|
|
@@ -163,7 +200,15 @@ export const parseRightBound = (
|
|
|
163
200
|
if (!isKeyOf(comparator, maxComparators)) {
|
|
164
201
|
return s.error(writeUnpairableComparatorMessage(comparator))
|
|
165
202
|
}
|
|
166
|
-
|
|
203
|
+
const lowerBoundKind = getBoundKinds(
|
|
204
|
+
s.branches.leftBound.comparator,
|
|
205
|
+
s.branches.leftBound.limit,
|
|
206
|
+
previousRoot
|
|
207
|
+
)
|
|
208
|
+
s.constrainRoot(
|
|
209
|
+
lowerBoundKind[0],
|
|
210
|
+
openLeftBoundToSchema(s.branches.leftBound)
|
|
211
|
+
)
|
|
167
212
|
delete s.branches.leftBound
|
|
168
213
|
}
|
|
169
214
|
|
|
@@ -198,7 +243,7 @@ export type parseRightBound<
|
|
|
198
243
|
export const writeInvalidLimitMessage = <
|
|
199
244
|
comparator extends Comparator,
|
|
200
245
|
limit extends string | number,
|
|
201
|
-
boundKind extends
|
|
246
|
+
boundKind extends BoundExpressionKind
|
|
202
247
|
>(
|
|
203
248
|
comparator: comparator,
|
|
204
249
|
limit: limit,
|
|
@@ -211,19 +256,9 @@ export const writeInvalidLimitMessage = <
|
|
|
211
256
|
export type writeInvalidLimitMessage<
|
|
212
257
|
comparator extends Comparator,
|
|
213
258
|
limit extends string | number,
|
|
214
|
-
boundKind extends
|
|
259
|
+
boundKind extends BoundExpressionKind
|
|
215
260
|
> = `Comparator ${comparator} must be ${boundKind extends "left"
|
|
216
261
|
? "preceded"
|
|
217
262
|
: "followed"} by a corresponding literal (was '${limit}')`
|
|
218
263
|
|
|
219
|
-
export type
|
|
220
|
-
|
|
221
|
-
export const invertedComparators = {
|
|
222
|
-
"<": ">",
|
|
223
|
-
">": "<",
|
|
224
|
-
"<=": ">=",
|
|
225
|
-
">=": "<=",
|
|
226
|
-
"==": "=="
|
|
227
|
-
} as const satisfies Record<Comparator, Comparator>
|
|
228
|
-
|
|
229
|
-
export type InvertedComparators = typeof invertedComparators
|
|
264
|
+
export type BoundExpressionKind = "left" | "right"
|
|
@@ -14,22 +14,22 @@ export const parseOperator = (s: DynamicStateWithRoot): void => {
|
|
|
14
14
|
return lookahead === ""
|
|
15
15
|
? s.finalize("")
|
|
16
16
|
: lookahead === "["
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
33
|
}
|
|
34
34
|
|
|
35
35
|
export type parseOperator<
|
|
@@ -42,21 +42,21 @@ export type parseOperator<
|
|
|
42
42
|
? state.setRoot<s, [s["root"], "[]"], nextUnscanned>
|
|
43
43
|
: state.error<incompleteArrayTokenMessage>
|
|
44
44
|
: lookahead extends "|" | "&"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
60
|
: state.finalize<s, "">
|
|
61
61
|
|
|
62
62
|
export const writeUnexpectedCharacterMessage = <
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isKeyOf, type Dict } from "@arktype/util"
|
|
2
|
-
import type { Comparator } from "
|
|
2
|
+
import type { Comparator } from "../reduce/shared.js"
|
|
3
3
|
|
|
4
4
|
export class Scanner<Lookahead extends string = string> {
|
|
5
5
|
private chars: string[]
|
|
@@ -157,13 +157,13 @@ export namespace Scanner {
|
|
|
157
157
|
? true
|
|
158
158
|
: false
|
|
159
159
|
: Scanner.skipWhitespace<unscanned> extends
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
| ""
|
|
161
|
+
| `${TerminatingChar}${string}`
|
|
162
|
+
? true
|
|
163
|
+
: false
|
|
164
164
|
: lookahead extends ","
|
|
165
|
-
|
|
166
|
-
|
|
165
|
+
? true
|
|
166
|
+
: false
|
|
167
167
|
|
|
168
168
|
export type shift<
|
|
169
169
|
lookahead extends string,
|
package/parser/string/string.ts
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { TypeNode } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
throwInternalError,
|
|
4
|
+
throwParseError,
|
|
5
|
+
type ErrorMessage
|
|
6
|
+
} from "@arktype/util"
|
|
7
|
+
import type { inferAstRoot } from "../semantic/semantic.js"
|
|
8
|
+
import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.js"
|
|
7
9
|
import type { StringifiablePrefixOperator } from "./reduce/shared.js"
|
|
8
10
|
import type { StaticState, state } from "./reduce/static.js"
|
|
9
|
-
import { parseOperand } from "./shift/operand/operand.js"
|
|
11
|
+
import type { parseOperand } from "./shift/operand/operand.js"
|
|
10
12
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
writeUnexpectedCharacterMessage,
|
|
14
|
+
type parseOperator
|
|
13
15
|
} from "./shift/operator/operator.js"
|
|
14
16
|
|
|
15
|
-
export const parseString = (def: string, ctx: ParseContext): Root =>
|
|
16
|
-
ctx.scope.maybeResolveNode(def) ??
|
|
17
|
-
((def.endsWith("[]") &&
|
|
18
|
-
ctx.scope.maybeResolveNode(def.slice(0, -2))?.array()) ||
|
|
19
|
-
fullStringParse(def, ctx))
|
|
20
|
-
|
|
21
17
|
/**
|
|
22
18
|
* Try to parse the definition from right to left using the most common syntax.
|
|
23
19
|
* This can be much more efficient for simple definitions.
|
|
@@ -27,12 +23,12 @@ export type parseString<def extends string, $, args> = def extends keyof $
|
|
|
27
23
|
// fail semantic validation because it has no args
|
|
28
24
|
def
|
|
29
25
|
: def extends `${infer child}[]`
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
? child extends keyof $
|
|
27
|
+
? [child, "[]"]
|
|
28
|
+
: fullStringParse<state.initialize<def>, $, args>
|
|
29
|
+
: fullStringParse<state.initialize<def>, $, args>
|
|
34
30
|
|
|
35
|
-
export type inferString<def extends string, $, args> =
|
|
31
|
+
export type inferString<def extends string, $, args> = inferAstRoot<
|
|
36
32
|
parseString<def, $, args>,
|
|
37
33
|
$,
|
|
38
34
|
args
|
|
@@ -44,23 +40,24 @@ export type BaseCompletions<$, args, otherSuggestions extends string = never> =
|
|
|
44
40
|
| StringifiablePrefixOperator
|
|
45
41
|
| otherSuggestions
|
|
46
42
|
|
|
47
|
-
export const fullStringParse = (
|
|
48
|
-
|
|
49
|
-
parseOperand(s)
|
|
43
|
+
export const fullStringParse = (s: DynamicState): TypeNode => {
|
|
44
|
+
s.parseOperand()
|
|
50
45
|
const result = parseUntilFinalizer(s).root
|
|
46
|
+
if (!result) {
|
|
47
|
+
return throwInternalError(
|
|
48
|
+
`Root was unexpectedly unset after parsing string '${s.scanner.scanned}'`
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
51
|
s.scanner.shiftUntilNonWhitespace()
|
|
52
52
|
if (s.scanner.lookahead) {
|
|
53
53
|
// throw a parse error if non-whitespace characters made it here without being parsed
|
|
54
54
|
throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead))
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
return result.isNever()
|
|
58
|
-
? throwParseError(writeUnsatisfiableExpressionError(def))
|
|
59
|
-
: result
|
|
56
|
+
return result
|
|
60
57
|
}
|
|
61
58
|
|
|
62
|
-
type fullStringParse<
|
|
63
|
-
parseUntilFinalizer<
|
|
59
|
+
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<
|
|
60
|
+
parseUntilFinalizer<s, $, args>
|
|
64
61
|
>
|
|
65
62
|
|
|
66
63
|
export const parseUntilFinalizer = (s: DynamicState) => {
|
|
@@ -79,7 +76,7 @@ export type parseUntilFinalizer<
|
|
|
79
76
|
: s
|
|
80
77
|
|
|
81
78
|
const next = (s: DynamicState) =>
|
|
82
|
-
s.hasRoot() ? parseOperator(
|
|
79
|
+
s.hasRoot() ? s.parseOperator() : s.parseOperand()
|
|
83
80
|
|
|
84
81
|
type next<s extends StaticState, $, args> = s["root"] extends undefined
|
|
85
82
|
? parseOperand<s, $, args>
|
|
@@ -89,5 +86,5 @@ export type extractFinalizedResult<s extends StaticState> =
|
|
|
89
86
|
s["finalizer"] extends ErrorMessage
|
|
90
87
|
? s["finalizer"]
|
|
91
88
|
: s["finalizer"] extends ""
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
? s["root"]
|
|
90
|
+
: state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>
|