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,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
invertedComparators,
|
|
3
|
+
type Comparator,
|
|
4
|
+
type InvertedComparators,
|
|
5
|
+
type LimitLiteral,
|
|
6
|
+
type MinComparator
|
|
7
|
+
} from "../shift/operator/bounds.js"
|
|
8
|
+
|
|
9
|
+
export type StringifiablePrefixOperator = "keyof"
|
|
10
|
+
|
|
11
|
+
export type OpenLeftBound = { limit: LimitLiteral; comparator: MinComparator }
|
|
12
|
+
|
|
13
|
+
export const writeUnmatchedGroupCloseMessage = <unscanned extends string>(
|
|
14
|
+
unscanned: unscanned
|
|
15
|
+
): writeUnmatchedGroupCloseMessage<unscanned> =>
|
|
16
|
+
`Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`) as any}`
|
|
17
|
+
|
|
18
|
+
export type writeUnmatchedGroupCloseMessage<unscanned extends string> =
|
|
19
|
+
`Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`
|
|
20
|
+
|
|
21
|
+
export const writeUnclosedGroupMessage = <missingChar extends string>(
|
|
22
|
+
missingChar: missingChar
|
|
23
|
+
): writeUnclosedGroupMessage<missingChar> => `Missing ${missingChar}`
|
|
24
|
+
|
|
25
|
+
export type writeUnclosedGroupMessage<missingChar extends string> =
|
|
26
|
+
`Missing ${missingChar}`
|
|
27
|
+
|
|
28
|
+
export const writeOpenRangeMessage = <
|
|
29
|
+
min extends LimitLiteral,
|
|
30
|
+
comparator extends MinComparator
|
|
31
|
+
>(
|
|
32
|
+
min: min,
|
|
33
|
+
comparator: comparator
|
|
34
|
+
): writeOpenRangeMessage<min, comparator> =>
|
|
35
|
+
`Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
|
|
36
|
+
|
|
37
|
+
export type writeOpenRangeMessage<
|
|
38
|
+
min extends LimitLiteral,
|
|
39
|
+
comparator extends MinComparator
|
|
40
|
+
> = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
|
|
41
|
+
|
|
42
|
+
export type writeUnpairableComparatorMessage<comparator extends Comparator> =
|
|
43
|
+
`Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
|
|
44
|
+
|
|
45
|
+
export const writeUnpairableComparatorMessage = <comparator extends Comparator>(
|
|
46
|
+
comparator: comparator
|
|
47
|
+
): writeUnpairableComparatorMessage<comparator> =>
|
|
48
|
+
`Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
|
|
49
|
+
|
|
50
|
+
export const writeMultipleLeftBoundsMessage = <
|
|
51
|
+
openLimit extends LimitLiteral,
|
|
52
|
+
openComparator extends MinComparator,
|
|
53
|
+
limit extends LimitLiteral,
|
|
54
|
+
comparator extends MinComparator
|
|
55
|
+
>(
|
|
56
|
+
openLimit: openLimit,
|
|
57
|
+
openComparator: openComparator,
|
|
58
|
+
limit: limit,
|
|
59
|
+
comparator: comparator
|
|
60
|
+
): writeMultipleLeftBoundsMessage<
|
|
61
|
+
openLimit,
|
|
62
|
+
openComparator,
|
|
63
|
+
limit,
|
|
64
|
+
comparator
|
|
65
|
+
> =>
|
|
66
|
+
`An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`
|
|
67
|
+
|
|
68
|
+
export type writeMultipleLeftBoundsMessage<
|
|
69
|
+
openLimit extends LimitLiteral,
|
|
70
|
+
openComparator extends MinComparator,
|
|
71
|
+
limit extends LimitLiteral,
|
|
72
|
+
comparator extends MinComparator
|
|
73
|
+
> = `An expression may have at most one left bound (parsed ${openLimit}${InvertedComparators[openComparator]}, ${limit}${InvertedComparators[comparator]})`
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type { Completion, defined, ErrorMessage } from "@arktype/util"
|
|
2
|
+
import type {
|
|
3
|
+
Comparator,
|
|
4
|
+
InvertedComparators,
|
|
5
|
+
LimitLiteral,
|
|
6
|
+
MaxComparator,
|
|
7
|
+
MinComparator
|
|
8
|
+
} from "../shift/operator/bounds.js"
|
|
9
|
+
import type { Scanner } from "../shift/scanner.js"
|
|
10
|
+
import type {
|
|
11
|
+
OpenLeftBound,
|
|
12
|
+
StringifiablePrefixOperator,
|
|
13
|
+
writeMultipleLeftBoundsMessage,
|
|
14
|
+
writeOpenRangeMessage,
|
|
15
|
+
writeUnclosedGroupMessage,
|
|
16
|
+
writeUnmatchedGroupCloseMessage,
|
|
17
|
+
writeUnpairableComparatorMessage
|
|
18
|
+
} from "./shared.js"
|
|
19
|
+
|
|
20
|
+
export type StaticState = {
|
|
21
|
+
root: unknown
|
|
22
|
+
branches: BranchState
|
|
23
|
+
groups: BranchState[]
|
|
24
|
+
finalizer: Scanner.FinalizingLookahead | ErrorMessage | undefined
|
|
25
|
+
scanned: string
|
|
26
|
+
unscanned: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type AutocompletePrefix = `${StringifiablePrefixOperator} `
|
|
30
|
+
|
|
31
|
+
type BranchState = {
|
|
32
|
+
prefixes: StringifiablePrefixOperator[]
|
|
33
|
+
leftBound: OpenLeftBound | undefined
|
|
34
|
+
"&": unknown
|
|
35
|
+
"|": unknown
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export namespace state {
|
|
39
|
+
export type initialize<def extends string> = from<{
|
|
40
|
+
root: undefined
|
|
41
|
+
branches: initialBranches
|
|
42
|
+
groups: []
|
|
43
|
+
finalizer: undefined
|
|
44
|
+
scanned: ""
|
|
45
|
+
unscanned: def
|
|
46
|
+
}>
|
|
47
|
+
|
|
48
|
+
export type error<message extends string> = from<{
|
|
49
|
+
root: undefined
|
|
50
|
+
branches: initialBranches
|
|
51
|
+
groups: []
|
|
52
|
+
finalizer: ErrorMessage<message>
|
|
53
|
+
scanned: ""
|
|
54
|
+
unscanned: ""
|
|
55
|
+
}>
|
|
56
|
+
|
|
57
|
+
export type completion<text extends string> = from<{
|
|
58
|
+
root: undefined
|
|
59
|
+
branches: initialBranches
|
|
60
|
+
groups: []
|
|
61
|
+
finalizer: Completion<text>
|
|
62
|
+
scanned: ""
|
|
63
|
+
unscanned: ""
|
|
64
|
+
}>
|
|
65
|
+
|
|
66
|
+
type initialBranches = branchesFrom<{
|
|
67
|
+
prefixes: []
|
|
68
|
+
leftBound: undefined
|
|
69
|
+
"&": undefined
|
|
70
|
+
"|": undefined
|
|
71
|
+
}>
|
|
72
|
+
|
|
73
|
+
type updateScanned<
|
|
74
|
+
previousScanned extends string,
|
|
75
|
+
previousUnscanned extends string,
|
|
76
|
+
updatedUnscanned extends string
|
|
77
|
+
> = previousUnscanned extends `${infer justScanned}${updatedUnscanned}`
|
|
78
|
+
? `${previousScanned}${justScanned}`
|
|
79
|
+
: previousScanned
|
|
80
|
+
|
|
81
|
+
export type setRoot<
|
|
82
|
+
s extends StaticState,
|
|
83
|
+
root,
|
|
84
|
+
unscanned extends string
|
|
85
|
+
> = from<{
|
|
86
|
+
root: root
|
|
87
|
+
branches: s["branches"]
|
|
88
|
+
groups: s["groups"]
|
|
89
|
+
finalizer: s["finalizer"]
|
|
90
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
91
|
+
unscanned: unscanned
|
|
92
|
+
}>
|
|
93
|
+
|
|
94
|
+
export type addPrefix<
|
|
95
|
+
s extends StaticState,
|
|
96
|
+
prefix extends StringifiablePrefixOperator,
|
|
97
|
+
unscanned extends string
|
|
98
|
+
> = from<{
|
|
99
|
+
root: s["root"]
|
|
100
|
+
branches: {
|
|
101
|
+
prefixes: [...s["branches"]["prefixes"], prefix]
|
|
102
|
+
leftBound: s["branches"]["leftBound"]
|
|
103
|
+
"&": s["branches"]["&"]
|
|
104
|
+
"|": s["branches"]["|"]
|
|
105
|
+
}
|
|
106
|
+
groups: s["groups"]
|
|
107
|
+
finalizer: s["finalizer"]
|
|
108
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
109
|
+
unscanned: unscanned
|
|
110
|
+
}>
|
|
111
|
+
|
|
112
|
+
export type reduceBranch<
|
|
113
|
+
s extends StaticState,
|
|
114
|
+
token extends "|" | "&",
|
|
115
|
+
unscanned extends string
|
|
116
|
+
> = s["branches"]["leftBound"] extends {}
|
|
117
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
118
|
+
: from<{
|
|
119
|
+
root: undefined
|
|
120
|
+
branches: {
|
|
121
|
+
prefixes: []
|
|
122
|
+
leftBound: undefined
|
|
123
|
+
"&": token extends "&" ? mergeToIntersection<s> : undefined
|
|
124
|
+
"|": token extends "|" ? mergeToUnion<s> : s["branches"]["|"]
|
|
125
|
+
}
|
|
126
|
+
groups: s["groups"]
|
|
127
|
+
finalizer: s["finalizer"]
|
|
128
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
129
|
+
unscanned: unscanned
|
|
130
|
+
}>
|
|
131
|
+
|
|
132
|
+
export type reduceLeftBound<
|
|
133
|
+
s extends StaticState,
|
|
134
|
+
limit extends LimitLiteral,
|
|
135
|
+
comparator extends Comparator,
|
|
136
|
+
unscanned extends string
|
|
137
|
+
> = comparator extends "<" | "<="
|
|
138
|
+
? s["branches"]["leftBound"] extends {}
|
|
139
|
+
? state.error<
|
|
140
|
+
writeMultipleLeftBoundsMessage<
|
|
141
|
+
s["branches"]["leftBound"]["limit"],
|
|
142
|
+
s["branches"]["leftBound"]["comparator"],
|
|
143
|
+
limit,
|
|
144
|
+
InvertedComparators[comparator]
|
|
145
|
+
>
|
|
146
|
+
>
|
|
147
|
+
: from<{
|
|
148
|
+
root: undefined
|
|
149
|
+
branches: {
|
|
150
|
+
prefixes: s["branches"]["prefixes"]
|
|
151
|
+
leftBound: {
|
|
152
|
+
limit: limit
|
|
153
|
+
comparator: InvertedComparators[comparator]
|
|
154
|
+
}
|
|
155
|
+
"&": s["branches"]["&"]
|
|
156
|
+
"|": s["branches"]["|"]
|
|
157
|
+
}
|
|
158
|
+
groups: s["groups"]
|
|
159
|
+
finalizer: s["finalizer"]
|
|
160
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
161
|
+
unscanned: unscanned
|
|
162
|
+
}>
|
|
163
|
+
: state.error<writeUnpairableComparatorMessage<comparator>>
|
|
164
|
+
|
|
165
|
+
export type reduceRange<
|
|
166
|
+
s extends StaticState,
|
|
167
|
+
minLimit extends LimitLiteral,
|
|
168
|
+
minComparator extends MinComparator,
|
|
169
|
+
maxComparator extends MaxComparator,
|
|
170
|
+
maxLimit extends LimitLiteral,
|
|
171
|
+
unscanned extends string
|
|
172
|
+
> = state.from<{
|
|
173
|
+
root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]]
|
|
174
|
+
branches: {
|
|
175
|
+
prefixes: s["branches"]["prefixes"]
|
|
176
|
+
leftBound: undefined
|
|
177
|
+
"&": s["branches"]["&"]
|
|
178
|
+
"|": s["branches"]["|"]
|
|
179
|
+
}
|
|
180
|
+
groups: s["groups"]
|
|
181
|
+
finalizer: s["finalizer"]
|
|
182
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
183
|
+
unscanned: unscanned
|
|
184
|
+
}>
|
|
185
|
+
|
|
186
|
+
export type reduceSingleBound<
|
|
187
|
+
s extends StaticState,
|
|
188
|
+
comparator extends Comparator,
|
|
189
|
+
limit extends LimitLiteral,
|
|
190
|
+
unscanned extends string
|
|
191
|
+
> = state.from<{
|
|
192
|
+
root: [s["root"], comparator, limit]
|
|
193
|
+
branches: {
|
|
194
|
+
prefixes: s["branches"]["prefixes"]
|
|
195
|
+
leftBound: undefined
|
|
196
|
+
"&": s["branches"]["&"]
|
|
197
|
+
"|": s["branches"]["|"]
|
|
198
|
+
}
|
|
199
|
+
groups: s["groups"]
|
|
200
|
+
finalizer: s["finalizer"]
|
|
201
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
202
|
+
unscanned: unscanned
|
|
203
|
+
}>
|
|
204
|
+
|
|
205
|
+
type mergeToUnion<s extends StaticState> =
|
|
206
|
+
s["branches"]["|"] extends undefined
|
|
207
|
+
? mergeToIntersection<s>
|
|
208
|
+
: [s["branches"]["|"], "|", mergeToIntersection<s>]
|
|
209
|
+
|
|
210
|
+
type mergeToIntersection<s extends StaticState> =
|
|
211
|
+
s["branches"]["&"] extends undefined
|
|
212
|
+
? mergePrefixes<s>
|
|
213
|
+
: [s["branches"]["&"], "&", mergePrefixes<s>]
|
|
214
|
+
|
|
215
|
+
type mergePrefixes<
|
|
216
|
+
s extends StaticState,
|
|
217
|
+
remaining extends unknown[] = s["branches"]["prefixes"]
|
|
218
|
+
> = remaining extends [infer head, ...infer tail]
|
|
219
|
+
? [head, mergePrefixes<s, tail>]
|
|
220
|
+
: s["root"]
|
|
221
|
+
|
|
222
|
+
type popGroup<stack extends BranchState[], top extends BranchState> = [
|
|
223
|
+
...stack,
|
|
224
|
+
top
|
|
225
|
+
]
|
|
226
|
+
|
|
227
|
+
export type finalizeGroup<
|
|
228
|
+
s extends StaticState,
|
|
229
|
+
unscanned extends string
|
|
230
|
+
> = s["branches"]["leftBound"] extends {}
|
|
231
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
232
|
+
: s["groups"] extends popGroup<infer stack, infer top>
|
|
233
|
+
? from<{
|
|
234
|
+
groups: stack
|
|
235
|
+
branches: top
|
|
236
|
+
root: mergeToUnion<s>
|
|
237
|
+
finalizer: s["finalizer"]
|
|
238
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
239
|
+
unscanned: unscanned
|
|
240
|
+
}>
|
|
241
|
+
: state.error<writeUnmatchedGroupCloseMessage<unscanned>>
|
|
242
|
+
|
|
243
|
+
export type reduceGroupOpen<
|
|
244
|
+
s extends StaticState,
|
|
245
|
+
unscanned extends string
|
|
246
|
+
> = from<{
|
|
247
|
+
groups: [...s["groups"], s["branches"]]
|
|
248
|
+
branches: initialBranches
|
|
249
|
+
root: undefined
|
|
250
|
+
finalizer: s["finalizer"]
|
|
251
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
252
|
+
unscanned: unscanned
|
|
253
|
+
}>
|
|
254
|
+
|
|
255
|
+
export type finalize<
|
|
256
|
+
s extends StaticState,
|
|
257
|
+
finalizer extends Scanner.FinalizingLookahead
|
|
258
|
+
> = s["groups"] extends []
|
|
259
|
+
? s["branches"]["leftBound"] extends {}
|
|
260
|
+
? openRangeError<s["branches"]["leftBound"]>
|
|
261
|
+
: from<{
|
|
262
|
+
root: mergeToUnion<s>
|
|
263
|
+
groups: s["groups"]
|
|
264
|
+
branches: initialBranches
|
|
265
|
+
finalizer: finalizer
|
|
266
|
+
scanned: s["scanned"]
|
|
267
|
+
unscanned: s["unscanned"]
|
|
268
|
+
}>
|
|
269
|
+
: state.error<writeUnclosedGroupMessage<")">>
|
|
270
|
+
|
|
271
|
+
type openRangeError<range extends defined<BranchState["leftBound"]>> =
|
|
272
|
+
state.error<writeOpenRangeMessage<range["limit"], range["comparator"]>>
|
|
273
|
+
|
|
274
|
+
export type previousOperator<s extends StaticState> =
|
|
275
|
+
s["branches"]["leftBound"] extends {}
|
|
276
|
+
? s["branches"]["leftBound"]["comparator"]
|
|
277
|
+
: s["branches"]["prefixes"] extends [
|
|
278
|
+
...unknown[],
|
|
279
|
+
infer tail extends string
|
|
280
|
+
]
|
|
281
|
+
? tail
|
|
282
|
+
: s["branches"]["&"] extends {}
|
|
283
|
+
? "&"
|
|
284
|
+
: s["branches"]["|"] extends {}
|
|
285
|
+
? "|"
|
|
286
|
+
: undefined
|
|
287
|
+
|
|
288
|
+
export type scanTo<s extends StaticState, unscanned extends string> = from<{
|
|
289
|
+
root: s["root"]
|
|
290
|
+
branches: s["branches"]
|
|
291
|
+
groups: s["groups"]
|
|
292
|
+
finalizer: s["finalizer"]
|
|
293
|
+
scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
|
|
294
|
+
unscanned: unscanned
|
|
295
|
+
}>
|
|
296
|
+
|
|
297
|
+
export type from<s extends StaticState> = s
|
|
298
|
+
|
|
299
|
+
export type branchesFrom<b extends BranchState> = b
|
|
300
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { throwParseError, tryParseNumber } from "@arktype/util"
|
|
2
|
+
|
|
3
|
+
export type DateLiteral<source extends string = string> =
|
|
4
|
+
| `d"${source}"`
|
|
5
|
+
| `d'${source}'`
|
|
6
|
+
|
|
7
|
+
export const isDateLiteral = (value: unknown): value is DateLiteral =>
|
|
8
|
+
typeof value === "string" &&
|
|
9
|
+
value[0] === "d" &&
|
|
10
|
+
(value[1] === "'" || value[1] === '"') &&
|
|
11
|
+
value.at(-1) === value[1]
|
|
12
|
+
|
|
13
|
+
export const isValidDate = (d: Date) => d.toString() !== "Invalid Date"
|
|
14
|
+
|
|
15
|
+
export const extractDateLiteralSource = <literal extends DateLiteral>(
|
|
16
|
+
literal: literal
|
|
17
|
+
) => literal.slice(2, -1) as extractDateLiteralSource<literal>
|
|
18
|
+
|
|
19
|
+
type extractDateLiteralSource<literal extends DateLiteral> =
|
|
20
|
+
literal extends DateLiteral<infer source> ? source : never
|
|
21
|
+
|
|
22
|
+
export const writeInvalidDateMessage = <source extends string>(
|
|
23
|
+
source: source
|
|
24
|
+
): writeInvalidDateMessage<source> =>
|
|
25
|
+
`'${source}' could not be parsed by the Date constructor`
|
|
26
|
+
|
|
27
|
+
export type writeInvalidDateMessage<source extends string> =
|
|
28
|
+
`'${source}' could not be parsed by the Date constructor`
|
|
29
|
+
|
|
30
|
+
export type DateInput = ConstructorParameters<typeof Date>[0]
|
|
31
|
+
|
|
32
|
+
export const tryParseDate = <errorOnFail extends boolean | string>(
|
|
33
|
+
source: string,
|
|
34
|
+
errorOnFail?: errorOnFail
|
|
35
|
+
) => maybeParseDate(source, errorOnFail)
|
|
36
|
+
|
|
37
|
+
const maybeParseDate = <errorOnFail extends boolean | string>(
|
|
38
|
+
source: string,
|
|
39
|
+
errorOnFail?: errorOnFail
|
|
40
|
+
): Date | (errorOnFail extends true | string ? never : undefined) => {
|
|
41
|
+
const stringParsedDate = new Date(source)
|
|
42
|
+
if (isValidDate(stringParsedDate)) {
|
|
43
|
+
return stringParsedDate
|
|
44
|
+
}
|
|
45
|
+
const epochMillis = tryParseNumber(source)
|
|
46
|
+
if (epochMillis !== undefined) {
|
|
47
|
+
const numberParsedDate = new Date(epochMillis)
|
|
48
|
+
if (isValidDate(numberParsedDate)) {
|
|
49
|
+
return numberParsedDate
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return errorOnFail
|
|
53
|
+
? throwParseError(
|
|
54
|
+
errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
|
|
55
|
+
)
|
|
56
|
+
: (undefined as never)
|
|
57
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { node } from "@arktype/schema"
|
|
2
|
+
import { isKeyOf } from "@arktype/util"
|
|
3
|
+
import type { RegexLiteral } from "../../../semantic/semantic.js"
|
|
4
|
+
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
5
|
+
import type { state, StaticState } from "../../reduce/static.js"
|
|
6
|
+
import type { Scanner } from "../scanner.js"
|
|
7
|
+
import { tryParseDate, writeInvalidDateMessage } from "./date.js"
|
|
8
|
+
|
|
9
|
+
export type StringLiteral<Text extends string = string> =
|
|
10
|
+
| DoubleQuotedStringLiteral<Text>
|
|
11
|
+
| SingleQuotedStringLiteral<Text>
|
|
12
|
+
|
|
13
|
+
export type DoubleQuotedStringLiteral<Text extends string = string> =
|
|
14
|
+
`"${Text}"`
|
|
15
|
+
|
|
16
|
+
export type SingleQuotedStringLiteral<Text extends string = string> =
|
|
17
|
+
`'${Text}'`
|
|
18
|
+
|
|
19
|
+
export const parseEnclosed = (
|
|
20
|
+
s: DynamicState,
|
|
21
|
+
enclosing: EnclosingStartToken
|
|
22
|
+
) => {
|
|
23
|
+
const enclosed = s.scanner.shiftUntil(
|
|
24
|
+
untilLookaheadIsClosing[enclosingTokens[enclosing]]
|
|
25
|
+
)
|
|
26
|
+
if (s.scanner.lookahead === "") {
|
|
27
|
+
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
|
|
28
|
+
}
|
|
29
|
+
// Shift the scanner one additional time for the second enclosing token
|
|
30
|
+
const token = `${enclosing}${enclosed}${s.scanner.shift()}`
|
|
31
|
+
if (enclosing === "/") {
|
|
32
|
+
// fail parsing if the regex is invalid
|
|
33
|
+
new RegExp(enclosed)
|
|
34
|
+
s.root = node({ basis: "string", pattern: token as RegexLiteral })
|
|
35
|
+
} else if (isKeyOf(enclosing, enclosingQuote)) {
|
|
36
|
+
s.root = node({ is: enclosed })
|
|
37
|
+
} else {
|
|
38
|
+
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
|
|
39
|
+
s.root = node({ is: date, description: token })
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type parseEnclosed<
|
|
44
|
+
s extends StaticState,
|
|
45
|
+
enclosingStart extends EnclosingStartToken,
|
|
46
|
+
unscanned extends string
|
|
47
|
+
> = Scanner.shiftUntil<
|
|
48
|
+
unscanned,
|
|
49
|
+
EnclosingTokens[enclosingStart]
|
|
50
|
+
> extends Scanner.shiftResult<infer scanned, infer nextUnscanned>
|
|
51
|
+
? nextUnscanned extends ""
|
|
52
|
+
? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
|
|
53
|
+
: state.setRoot<
|
|
54
|
+
s,
|
|
55
|
+
`${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`,
|
|
56
|
+
nextUnscanned extends Scanner.shift<string, infer unscanned>
|
|
57
|
+
? unscanned
|
|
58
|
+
: ""
|
|
59
|
+
>
|
|
60
|
+
: never
|
|
61
|
+
|
|
62
|
+
export const enclosingQuote = {
|
|
63
|
+
"'": 1,
|
|
64
|
+
'"': 1
|
|
65
|
+
} as const
|
|
66
|
+
|
|
67
|
+
export type EnclosingQuote = keyof typeof enclosingQuote
|
|
68
|
+
|
|
69
|
+
export const enclosingChar = {
|
|
70
|
+
"/": 1,
|
|
71
|
+
...enclosingQuote
|
|
72
|
+
} as const
|
|
73
|
+
|
|
74
|
+
export const enclosingTokens = {
|
|
75
|
+
"d'": "'",
|
|
76
|
+
'd"': '"',
|
|
77
|
+
"'": "'",
|
|
78
|
+
'"': '"',
|
|
79
|
+
"/": "/"
|
|
80
|
+
} as const
|
|
81
|
+
|
|
82
|
+
export type EnclosingTokens = typeof enclosingTokens
|
|
83
|
+
|
|
84
|
+
export type EnclosingStartToken = keyof EnclosingTokens
|
|
85
|
+
|
|
86
|
+
export type EnclosingEndToken = EnclosingTokens[keyof EnclosingTokens]
|
|
87
|
+
|
|
88
|
+
export const untilLookaheadIsClosing: Record<
|
|
89
|
+
EnclosingEndToken,
|
|
90
|
+
Scanner.UntilCondition
|
|
91
|
+
> = {
|
|
92
|
+
"'": (scanner) => scanner.lookahead === `'`,
|
|
93
|
+
'"': (scanner) => scanner.lookahead === `"`,
|
|
94
|
+
"/": (scanner) => scanner.lookahead === `/`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const enclosingCharDescriptions = {
|
|
98
|
+
'"': "double-quote",
|
|
99
|
+
"'": "single-quote",
|
|
100
|
+
"/": "forward slash"
|
|
101
|
+
} as const
|
|
102
|
+
|
|
103
|
+
type enclosingCharDescriptions = typeof enclosingCharDescriptions
|
|
104
|
+
|
|
105
|
+
export const writeUnterminatedEnclosedMessage = <
|
|
106
|
+
fragment extends string,
|
|
107
|
+
enclosingStart extends EnclosingStartToken
|
|
108
|
+
>(
|
|
109
|
+
fragment: fragment,
|
|
110
|
+
enclosingStart: enclosingStart
|
|
111
|
+
): writeUnterminatedEnclosedMessage<fragment, enclosingStart> =>
|
|
112
|
+
`${enclosingStart}${fragment} requires a closing ${
|
|
113
|
+
enclosingCharDescriptions[enclosingTokens[enclosingStart]]
|
|
114
|
+
}`
|
|
115
|
+
|
|
116
|
+
export type writeUnterminatedEnclosedMessage<
|
|
117
|
+
fragment extends string,
|
|
118
|
+
enclosingStart extends EnclosingStartToken
|
|
119
|
+
> = `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[EnclosingTokens[enclosingStart]]}`
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
2
|
+
import type { state, StaticState } from "../../reduce/static.js"
|
|
3
|
+
import type { BaseCompletions } from "../../string.js"
|
|
4
|
+
import { Scanner } from "../scanner.js"
|
|
5
|
+
import {
|
|
6
|
+
enclosingChar,
|
|
7
|
+
parseEnclosed,
|
|
8
|
+
type EnclosingEndToken,
|
|
9
|
+
type EnclosingQuote,
|
|
10
|
+
type EnclosingStartToken
|
|
11
|
+
} from "./enclosed.js"
|
|
12
|
+
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js"
|
|
13
|
+
|
|
14
|
+
export const parseOperand = (s: DynamicState): void =>
|
|
15
|
+
s.scanner.lookahead === ""
|
|
16
|
+
? s.error(writeMissingOperandMessage(s))
|
|
17
|
+
: s.scanner.lookahead === "("
|
|
18
|
+
? s.shiftedByOne().reduceGroupOpen()
|
|
19
|
+
: s.scanner.lookaheadIsIn(enclosingChar)
|
|
20
|
+
? parseEnclosed(s, s.scanner.shift())
|
|
21
|
+
: s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens)
|
|
22
|
+
? parseOperand(s.shiftedByOne())
|
|
23
|
+
: s.scanner.lookahead === "d"
|
|
24
|
+
? s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar)
|
|
25
|
+
? parseEnclosed(s, `d${s.scanner.shift()}` as EnclosingStartToken)
|
|
26
|
+
: parseUnenclosed(s)
|
|
27
|
+
: parseUnenclosed(s)
|
|
28
|
+
|
|
29
|
+
export type parseOperand<
|
|
30
|
+
s extends StaticState,
|
|
31
|
+
$,
|
|
32
|
+
args
|
|
33
|
+
> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
|
|
34
|
+
? lookahead extends "("
|
|
35
|
+
? state.reduceGroupOpen<s, unscanned>
|
|
36
|
+
: lookahead extends EnclosingEndToken
|
|
37
|
+
? parseEnclosed<s, lookahead, unscanned>
|
|
38
|
+
: lookahead extends Scanner.WhiteSpaceToken
|
|
39
|
+
? parseOperand<state.scanTo<s, unscanned>, $, args>
|
|
40
|
+
: lookahead extends "d"
|
|
41
|
+
? unscanned extends Scanner.shift<
|
|
42
|
+
infer enclosing extends EnclosingQuote,
|
|
43
|
+
infer nextUnscanned
|
|
44
|
+
>
|
|
45
|
+
? parseEnclosed<s, `d${enclosing}`, nextUnscanned>
|
|
46
|
+
: parseUnenclosed<s, $, args>
|
|
47
|
+
: parseUnenclosed<s, $, args>
|
|
48
|
+
: state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>
|