arktype 2.0.0-dev.10 → 2.0.0-dev.11
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/{main.ts → out/api.d.ts} +6 -6
- package/out/api.js +5 -0
- package/out/ark.d.ts +1 -1
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +3 -3
- package/out/match.js +1 -1
- package/out/module.d.ts +4 -2
- package/out/parser/definition.d.ts +10 -8
- package/out/parser/definition.js +2 -2
- package/out/parser/objectLiteral.d.ts +8 -13
- package/out/parser/objectLiteral.js +20 -23
- package/out/parser/semantic/bounds.d.ts +3 -3
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +1 -1
- package/out/parser/semantic/validate.d.ts +6 -4
- package/out/parser/string/reduce/dynamic.d.ts +6 -6
- package/out/parser/string/reduce/shared.d.ts +16 -16
- package/out/parser/string/reduce/shared.js +4 -2
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
- package/out/parser/string/shift/operand/enclosed.js +2 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +8 -6
- package/out/parser/string/shift/operand/unenclosed.js +2 -2
- package/out/parser/string/shift/operator/bounds.d.ts +5 -5
- package/out/parser/string/shift/operator/bounds.js +3 -3
- package/out/parser/string/shift/operator/divisor.d.ts +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +1 -1
- package/out/parser/string/string.d.ts +2 -2
- package/out/parser/tuple.d.ts +11 -11
- package/out/parser/tuple.js +3 -3
- package/out/scope.d.ts +13 -11
- package/out/scope.js +5 -5
- package/out/type.d.ts +4 -4
- package/out/type.js +2 -2
- package/package.json +8 -10
- package/__tests__/array.test.ts +0 -391
- package/__tests__/badDefinitionType.test.ts +0 -58
- package/__tests__/basis.test.ts +0 -50
- package/__tests__/bounds.test.ts +0 -318
- package/__tests__/cast.test.ts +0 -37
- package/__tests__/completions.test.ts +0 -51
- package/__tests__/config.test.ts +0 -96
- package/__tests__/dateLiteral.test.ts +0 -36
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -114
- package/__tests__/enclosed.test.ts +0 -107
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -210
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -313
- package/__tests__/group.test.ts +0 -85
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -126
- package/__tests__/instanceof.test.ts +0 -104
- package/__tests__/intersection.test.ts +0 -108
- package/__tests__/keyTraversal.test.ts +0 -77
- package/__tests__/keyof.test.ts +0 -150
- package/__tests__/keywords.test.ts +0 -214
- package/__tests__/literal.test.ts +0 -90
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -234
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/narrow.test.ts +0 -100
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -442
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/pipe.test.ts +0 -501
- package/__tests__/realWorld.test.ts +0 -63
- package/__tests__/regex.test.ts +0 -68
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -374
- package/__tests__/serialization.test.ts +0 -19
- package/__tests__/string.test.ts +0 -52
- package/__tests__/submodule.test.ts +0 -86
- package/__tests__/this.test.ts +0 -83
- package/__tests__/thunk.test.ts +0 -86
- package/__tests__/traverse.test.ts +0 -141
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/type.test.ts +0 -36
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -136
- package/__tests__/union.test.ts +0 -214
- package/ark.ts +0 -47
- package/generic.ts +0 -69
- package/match.ts +0 -218
- package/module.ts +0 -15
- package/out/ark.js.map +0 -1
- package/out/generic.js.map +0 -1
- package/out/main.js.map +0 -1
- package/out/match.js.map +0 -1
- package/out/module.js.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.js.map +0 -1
- package/out/parser/semantic/infer.js.map +0 -1
- package/out/parser/semantic/utils.js.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.js.map +0 -1
- package/out/type.js.map +0 -1
- package/parser/definition.ts +0 -143
- package/parser/generic.ts +0 -66
- package/parser/objectLiteral.ts +0 -233
- package/parser/semantic/bounds.ts +0 -62
- package/parser/semantic/divisor.ts +0 -11
- package/parser/semantic/infer.ts +0 -141
- package/parser/semantic/utils.ts +0 -15
- package/parser/semantic/validate.ts +0 -117
- package/parser/string/reduce/dynamic.ts +0 -208
- package/parser/string/reduce/shared.ts +0 -99
- package/parser/string/reduce/static.ts +0 -296
- package/parser/string/shift/operand/date.ts +0 -55
- package/parser/string/shift/operand/enclosed.ts +0 -123
- package/parser/string/shift/operand/genericArgs.ts +0 -128
- package/parser/string/shift/operand/operand.ts +0 -42
- package/parser/string/shift/operand/unenclosed.ts +0 -217
- package/parser/string/shift/operator/bounds.ts +0 -270
- package/parser/string/shift/operator/divisor.ts +0 -33
- package/parser/string/shift/operator/operator.ts +0 -71
- package/parser/string/shift/scanner.ts +0 -210
- package/parser/string/string.ts +0 -85
- package/parser/tuple.ts +0 -542
- package/scope.ts +0 -291
- package/type.ts +0 -266
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { isKeyOf } from "@arktype/util"
|
|
2
|
-
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
3
|
-
import type { StaticState, state } from "../../reduce/static.js"
|
|
4
|
-
import type { Scanner } from "../scanner.js"
|
|
5
|
-
import { tryParseDate, writeInvalidDateMessage } from "./date.js"
|
|
6
|
-
|
|
7
|
-
export type StringLiteral<Text extends string = string> =
|
|
8
|
-
| DoubleQuotedStringLiteral<Text>
|
|
9
|
-
| SingleQuotedStringLiteral<Text>
|
|
10
|
-
|
|
11
|
-
export type DoubleQuotedStringLiteral<Text extends string = string> =
|
|
12
|
-
`"${Text}"`
|
|
13
|
-
|
|
14
|
-
export type SingleQuotedStringLiteral<Text extends string = string> =
|
|
15
|
-
`'${Text}'`
|
|
16
|
-
|
|
17
|
-
export const parseEnclosed = (
|
|
18
|
-
s: DynamicState,
|
|
19
|
-
enclosing: EnclosingStartToken
|
|
20
|
-
): void => {
|
|
21
|
-
const enclosed = s.scanner.shiftUntil(
|
|
22
|
-
untilLookaheadIsClosing[enclosingTokens[enclosing]]
|
|
23
|
-
)
|
|
24
|
-
if (s.scanner.lookahead === "")
|
|
25
|
-
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
|
|
26
|
-
|
|
27
|
-
// Shift the scanner one additional time for the second enclosing token
|
|
28
|
-
s.scanner.shift()
|
|
29
|
-
if (enclosing === "/") {
|
|
30
|
-
// fail parsing if the regex is invalid
|
|
31
|
-
new RegExp(enclosed)
|
|
32
|
-
s.root = s.ctx.$.node(
|
|
33
|
-
"intersection",
|
|
34
|
-
{
|
|
35
|
-
domain: "string",
|
|
36
|
-
regex: enclosed
|
|
37
|
-
},
|
|
38
|
-
{ prereduced: true }
|
|
39
|
-
)
|
|
40
|
-
} else if (isKeyOf(enclosing, enclosingQuote))
|
|
41
|
-
s.root = s.ctx.$.node("unit", { unit: enclosed })
|
|
42
|
-
else {
|
|
43
|
-
const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
|
|
44
|
-
s.root = s.ctx.$.node("unit", { unit: date, description: enclosed })
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type parseEnclosed<
|
|
49
|
-
s extends StaticState,
|
|
50
|
-
enclosingStart extends EnclosingStartToken,
|
|
51
|
-
unscanned extends string
|
|
52
|
-
> =
|
|
53
|
-
Scanner.shiftUntil<unscanned, EnclosingTokens[enclosingStart]> extends (
|
|
54
|
-
Scanner.shiftResult<infer scanned, infer nextUnscanned>
|
|
55
|
-
) ?
|
|
56
|
-
nextUnscanned extends "" ?
|
|
57
|
-
state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
|
|
58
|
-
: state.setRoot<
|
|
59
|
-
s,
|
|
60
|
-
`${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`,
|
|
61
|
-
nextUnscanned extends Scanner.shift<string, infer unscanned> ? unscanned
|
|
62
|
-
: ""
|
|
63
|
-
>
|
|
64
|
-
: never
|
|
65
|
-
|
|
66
|
-
export const enclosingQuote = {
|
|
67
|
-
"'": 1,
|
|
68
|
-
'"': 1
|
|
69
|
-
} as const
|
|
70
|
-
|
|
71
|
-
export type EnclosingQuote = keyof typeof enclosingQuote
|
|
72
|
-
|
|
73
|
-
export const enclosingChar = {
|
|
74
|
-
"/": 1,
|
|
75
|
-
...enclosingQuote
|
|
76
|
-
} as const
|
|
77
|
-
|
|
78
|
-
export const enclosingTokens = {
|
|
79
|
-
"d'": "'",
|
|
80
|
-
'd"': '"',
|
|
81
|
-
"'": "'",
|
|
82
|
-
'"': '"',
|
|
83
|
-
"/": "/"
|
|
84
|
-
} as const
|
|
85
|
-
|
|
86
|
-
export type EnclosingTokens = typeof enclosingTokens
|
|
87
|
-
|
|
88
|
-
export type EnclosingStartToken = keyof EnclosingTokens
|
|
89
|
-
|
|
90
|
-
export type EnclosingEndToken = EnclosingTokens[keyof EnclosingTokens]
|
|
91
|
-
|
|
92
|
-
export const untilLookaheadIsClosing: Record<
|
|
93
|
-
EnclosingEndToken,
|
|
94
|
-
Scanner.UntilCondition
|
|
95
|
-
> = {
|
|
96
|
-
"'": scanner => scanner.lookahead === `'`,
|
|
97
|
-
'"': scanner => scanner.lookahead === `"`,
|
|
98
|
-
"/": scanner => scanner.lookahead === `/`
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const enclosingCharDescriptions = {
|
|
102
|
-
'"': "double-quote",
|
|
103
|
-
"'": "single-quote",
|
|
104
|
-
"/": "forward slash"
|
|
105
|
-
} as const
|
|
106
|
-
|
|
107
|
-
type enclosingCharDescriptions = typeof enclosingCharDescriptions
|
|
108
|
-
|
|
109
|
-
export const writeUnterminatedEnclosedMessage = <
|
|
110
|
-
fragment extends string,
|
|
111
|
-
enclosingStart extends EnclosingStartToken
|
|
112
|
-
>(
|
|
113
|
-
fragment: fragment,
|
|
114
|
-
enclosingStart: enclosingStart
|
|
115
|
-
): writeUnterminatedEnclosedMessage<fragment, enclosingStart> =>
|
|
116
|
-
`${enclosingStart}${fragment} requires a closing ${
|
|
117
|
-
enclosingCharDescriptions[enclosingTokens[enclosingStart]]
|
|
118
|
-
}`
|
|
119
|
-
|
|
120
|
-
export type writeUnterminatedEnclosedMessage<
|
|
121
|
-
fragment extends string,
|
|
122
|
-
enclosingStart extends EnclosingStartToken
|
|
123
|
-
> = `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[EnclosingTokens[enclosingStart]]}`
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import type { RawSchema } from "@arktype/schema"
|
|
2
|
-
import type { ErrorMessage, join } from "@arktype/util"
|
|
3
|
-
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
4
|
-
import { writeUnclosedGroupMessage } from "../../reduce/shared.js"
|
|
5
|
-
import type { StaticState, state } from "../../reduce/static.js"
|
|
6
|
-
import type { parseUntilFinalizer } from "../../string.js"
|
|
7
|
-
|
|
8
|
-
export type ParsedArgs<
|
|
9
|
-
result extends unknown[] = unknown[],
|
|
10
|
-
unscanned extends string = string
|
|
11
|
-
> = {
|
|
12
|
-
result: result
|
|
13
|
-
unscanned: unscanned
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const parseGenericArgs = (
|
|
17
|
-
name: string,
|
|
18
|
-
params: string[],
|
|
19
|
-
s: DynamicState
|
|
20
|
-
): ParsedArgs<RawSchema[]> => _parseGenericArgs(name, params, s, [], [])
|
|
21
|
-
|
|
22
|
-
export type parseGenericArgs<
|
|
23
|
-
name extends string,
|
|
24
|
-
params extends string[],
|
|
25
|
-
unscanned extends string,
|
|
26
|
-
$,
|
|
27
|
-
args
|
|
28
|
-
> = _parseGenericArgs<name, params, unscanned, $, args, [], []>
|
|
29
|
-
|
|
30
|
-
const _parseGenericArgs = (
|
|
31
|
-
name: string,
|
|
32
|
-
params: string[],
|
|
33
|
-
s: DynamicState,
|
|
34
|
-
argDefs: string[],
|
|
35
|
-
argNodes: RawSchema[]
|
|
36
|
-
): ParsedArgs<RawSchema[]> => {
|
|
37
|
-
const argState = s.parseUntilFinalizer()
|
|
38
|
-
// remove the finalizing token from the argDef
|
|
39
|
-
argDefs.push(argState.scanner.scanned.slice(0, -1))
|
|
40
|
-
argNodes.push(argState.root)
|
|
41
|
-
if (argState.finalizer === ">") {
|
|
42
|
-
if (argNodes.length === params.length) {
|
|
43
|
-
return {
|
|
44
|
-
result: argNodes,
|
|
45
|
-
unscanned: argState.scanner.unscanned
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return argState.error(writeInvalidGenericArgsMessage(name, params, argDefs))
|
|
49
|
-
}
|
|
50
|
-
if (argState.finalizer === ",")
|
|
51
|
-
return _parseGenericArgs(name, params, s, argDefs, argNodes)
|
|
52
|
-
|
|
53
|
-
return argState.error(writeUnclosedGroupMessage(">"))
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
type _parseGenericArgs<
|
|
57
|
-
name extends string,
|
|
58
|
-
params extends string[],
|
|
59
|
-
unscanned extends string,
|
|
60
|
-
$,
|
|
61
|
-
args,
|
|
62
|
-
argDefs extends string[],
|
|
63
|
-
argAsts extends unknown[]
|
|
64
|
-
> =
|
|
65
|
-
parseUntilFinalizer<state.initialize<unscanned>, $, args> extends (
|
|
66
|
-
infer finalArgState extends StaticState
|
|
67
|
-
) ?
|
|
68
|
-
{
|
|
69
|
-
defs: [
|
|
70
|
-
...argDefs,
|
|
71
|
-
finalArgState["scanned"] extends `${infer def}${"," | ">"}` ? def
|
|
72
|
-
: finalArgState["scanned"]
|
|
73
|
-
]
|
|
74
|
-
asts: [...argAsts, finalArgState["root"]]
|
|
75
|
-
unscanned: finalArgState["unscanned"]
|
|
76
|
-
} extends (
|
|
77
|
-
{
|
|
78
|
-
defs: infer nextDefs extends string[]
|
|
79
|
-
asts: infer nextAsts extends unknown[]
|
|
80
|
-
unscanned: infer nextUnscanned extends string
|
|
81
|
-
}
|
|
82
|
-
) ?
|
|
83
|
-
finalArgState["finalizer"] extends ">" ?
|
|
84
|
-
nextAsts["length"] extends params["length"] ?
|
|
85
|
-
ParsedArgs<nextAsts, nextUnscanned>
|
|
86
|
-
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
87
|
-
: finalArgState["finalizer"] extends "," ?
|
|
88
|
-
_parseGenericArgs<
|
|
89
|
-
name,
|
|
90
|
-
params,
|
|
91
|
-
nextUnscanned,
|
|
92
|
-
$,
|
|
93
|
-
args,
|
|
94
|
-
nextDefs,
|
|
95
|
-
nextAsts
|
|
96
|
-
>
|
|
97
|
-
: finalArgState["finalizer"] extends ErrorMessage ? finalArgState
|
|
98
|
-
: state.error<writeUnclosedGroupMessage<">">>
|
|
99
|
-
: never
|
|
100
|
-
: never
|
|
101
|
-
|
|
102
|
-
export const writeInvalidGenericArgsMessage = <
|
|
103
|
-
name extends string,
|
|
104
|
-
params extends string[],
|
|
105
|
-
argDefs extends string[]
|
|
106
|
-
>(
|
|
107
|
-
name: name,
|
|
108
|
-
params: params,
|
|
109
|
-
argDefs: argDefs
|
|
110
|
-
): writeInvalidGenericArgsMessage<name, params, argDefs> =>
|
|
111
|
-
`${name}<${params.join(", ")}> requires exactly ${
|
|
112
|
-
params.length
|
|
113
|
-
} args (got ${argDefs.length}${
|
|
114
|
-
argDefs.length === 0 ? "" : `: ${argDefs.join(", ")}`
|
|
115
|
-
})` as never
|
|
116
|
-
|
|
117
|
-
export type writeInvalidGenericArgsMessage<
|
|
118
|
-
name extends string,
|
|
119
|
-
params extends string[],
|
|
120
|
-
argDefs extends string[]
|
|
121
|
-
> = `${name}<${join<
|
|
122
|
-
params,
|
|
123
|
-
", "
|
|
124
|
-
>}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends (
|
|
125
|
-
0
|
|
126
|
-
) ?
|
|
127
|
-
""
|
|
128
|
-
: `: ${join<argDefs, ",">}`})`
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
2
|
-
import type { StaticState, state } from "../../reduce/static.js"
|
|
3
|
-
import type { BaseCompletions } from "../../string.js"
|
|
4
|
-
import { Scanner } from "../scanner.js"
|
|
5
|
-
import {
|
|
6
|
-
type EnclosingQuote,
|
|
7
|
-
type EnclosingStartToken,
|
|
8
|
-
enclosingChar,
|
|
9
|
-
parseEnclosed
|
|
10
|
-
} from "./enclosed.js"
|
|
11
|
-
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js"
|
|
12
|
-
|
|
13
|
-
export const parseOperand = (s: DynamicState): void =>
|
|
14
|
-
s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s))
|
|
15
|
-
: s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
|
|
16
|
-
: s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
|
|
17
|
-
: s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens) ?
|
|
18
|
-
parseOperand(s.shiftedByOne())
|
|
19
|
-
: s.scanner.lookahead === "d" ?
|
|
20
|
-
s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar) ?
|
|
21
|
-
parseEnclosed(s, `d${s.scanner.shift()}` as EnclosingStartToken)
|
|
22
|
-
: parseUnenclosed(s)
|
|
23
|
-
: parseUnenclosed(s)
|
|
24
|
-
|
|
25
|
-
export type parseOperand<s extends StaticState, $, args> =
|
|
26
|
-
s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ?
|
|
27
|
-
lookahead extends "(" ? state.reduceGroupOpen<s, unscanned>
|
|
28
|
-
: lookahead extends EnclosingStartToken ?
|
|
29
|
-
parseEnclosed<s, lookahead, unscanned>
|
|
30
|
-
: lookahead extends Scanner.WhiteSpaceToken ?
|
|
31
|
-
parseOperand<state.scanTo<s, unscanned>, $, args>
|
|
32
|
-
: lookahead extends "d" ?
|
|
33
|
-
unscanned extends (
|
|
34
|
-
Scanner.shift<
|
|
35
|
-
infer enclosing extends EnclosingQuote,
|
|
36
|
-
infer nextUnscanned
|
|
37
|
-
>
|
|
38
|
-
) ?
|
|
39
|
-
parseEnclosed<s, `d${enclosing}`, nextUnscanned>
|
|
40
|
-
: parseUnenclosed<s, $, args>
|
|
41
|
-
: parseUnenclosed<s, $, args>
|
|
42
|
-
: state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>
|
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type GenericProps,
|
|
3
|
-
type PrivateDeclaration,
|
|
4
|
-
RawSchema,
|
|
5
|
-
type arkKind,
|
|
6
|
-
hasArkKind,
|
|
7
|
-
type writeNonSubmoduleDotMessage,
|
|
8
|
-
writeUnresolvableMessage
|
|
9
|
-
} from "@arktype/schema"
|
|
10
|
-
import {
|
|
11
|
-
type BigintLiteral,
|
|
12
|
-
type Completion,
|
|
13
|
-
type ErrorMessage,
|
|
14
|
-
type isAnyOrNever,
|
|
15
|
-
type join,
|
|
16
|
-
printable,
|
|
17
|
-
throwParseError,
|
|
18
|
-
tryParseNumber,
|
|
19
|
-
tryParseWellFormedBigint
|
|
20
|
-
} from "@arktype/util"
|
|
21
|
-
import type { Generic } from "../../../../generic.js"
|
|
22
|
-
import type { GenericInstantiationAst } from "../../../semantic/infer.js"
|
|
23
|
-
import { writePrefixedPrivateReferenceMessage } from "../../../semantic/validate.js"
|
|
24
|
-
import type { DynamicState } from "../../reduce/dynamic.js"
|
|
25
|
-
import type { StaticState, state } from "../../reduce/static.js"
|
|
26
|
-
import type { BaseCompletions } from "../../string.js"
|
|
27
|
-
import type { Scanner } from "../scanner.js"
|
|
28
|
-
import {
|
|
29
|
-
type ParsedArgs,
|
|
30
|
-
parseGenericArgs,
|
|
31
|
-
writeInvalidGenericArgsMessage
|
|
32
|
-
} from "./genericArgs.js"
|
|
33
|
-
|
|
34
|
-
export const parseUnenclosed = (s: DynamicState): void => {
|
|
35
|
-
const token = s.scanner.shiftUntilNextTerminator()
|
|
36
|
-
if (token === "keyof") s.addPrefix("keyof")
|
|
37
|
-
else s.root = unenclosedToNode(s, token)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export type parseUnenclosed<s extends StaticState, $, args> =
|
|
41
|
-
Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (
|
|
42
|
-
Scanner.shiftResult<infer token, infer unscanned>
|
|
43
|
-
) ?
|
|
44
|
-
token extends "keyof" ? state.addPrefix<s, "keyof", unscanned>
|
|
45
|
-
: tryResolve<s, token, $, args> extends infer result ?
|
|
46
|
-
result extends ErrorMessage<infer message> ? state.error<message>
|
|
47
|
-
: result extends keyof $ ?
|
|
48
|
-
isAnyOrNever<$[result]> extends true ?
|
|
49
|
-
state.setRoot<s, result, unscanned>
|
|
50
|
-
: $[result] extends GenericProps ?
|
|
51
|
-
parseGenericInstantiation<
|
|
52
|
-
token,
|
|
53
|
-
$[result],
|
|
54
|
-
state.scanTo<s, unscanned>,
|
|
55
|
-
$,
|
|
56
|
-
args
|
|
57
|
-
>
|
|
58
|
-
: state.setRoot<s, result, unscanned>
|
|
59
|
-
: state.setRoot<s, result, unscanned>
|
|
60
|
-
: never
|
|
61
|
-
: never
|
|
62
|
-
|
|
63
|
-
export const parseGenericInstantiation = (
|
|
64
|
-
name: string,
|
|
65
|
-
g: Generic,
|
|
66
|
-
s: DynamicState
|
|
67
|
-
): RawSchema => {
|
|
68
|
-
s.scanner.shiftUntilNonWhitespace()
|
|
69
|
-
const lookahead = s.scanner.shift()
|
|
70
|
-
if (lookahead !== "<")
|
|
71
|
-
return s.error(writeInvalidGenericArgsMessage(name, g.params, []))
|
|
72
|
-
|
|
73
|
-
const parsedArgs = parseGenericArgs(name, g.params, s)
|
|
74
|
-
const remainingChars = parsedArgs.unscanned.length
|
|
75
|
-
// set the scanner position to where the args scanner left off
|
|
76
|
-
s.scanner.jumpToIndex(
|
|
77
|
-
remainingChars === 0 ? s.scanner.length : -remainingChars
|
|
78
|
-
)
|
|
79
|
-
return g(...parsedArgs.result) as never
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export type parseGenericInstantiation<
|
|
83
|
-
name extends string,
|
|
84
|
-
g extends GenericProps,
|
|
85
|
-
s extends StaticState,
|
|
86
|
-
$,
|
|
87
|
-
args
|
|
88
|
-
// have to skip whitespace here since TS allows instantiations like `Partial <T>`
|
|
89
|
-
> =
|
|
90
|
-
Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ?
|
|
91
|
-
parseGenericArgs<name, g["params"], unscanned, $, args> extends (
|
|
92
|
-
infer result
|
|
93
|
-
) ?
|
|
94
|
-
result extends ParsedArgs<infer argAsts, infer nextUnscanned> ?
|
|
95
|
-
state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned>
|
|
96
|
-
: // propagate error
|
|
97
|
-
result
|
|
98
|
-
: never
|
|
99
|
-
: state.error<writeInvalidGenericArgsMessage<name, g["params"], []>>
|
|
100
|
-
|
|
101
|
-
const unenclosedToNode = (s: DynamicState, token: string): RawSchema =>
|
|
102
|
-
maybeParseReference(s, token) ??
|
|
103
|
-
maybeParseUnenclosedLiteral(s, token) ??
|
|
104
|
-
s.error(
|
|
105
|
-
token === "" ? writeMissingOperandMessage(s)
|
|
106
|
-
: token[0] === "#" ?
|
|
107
|
-
writePrefixedPrivateReferenceMessage(token as PrivateDeclaration)
|
|
108
|
-
: writeUnresolvableMessage(token)
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
const maybeParseReference = (
|
|
112
|
-
s: DynamicState,
|
|
113
|
-
token: string
|
|
114
|
-
): RawSchema | undefined => {
|
|
115
|
-
if (s.ctx.args?.[token]) return s.ctx.args[token].raw
|
|
116
|
-
const resolution = s.ctx.$.maybeResolve(token)
|
|
117
|
-
if (resolution instanceof RawSchema) return resolution
|
|
118
|
-
if (resolution === undefined) return
|
|
119
|
-
if (hasArkKind(resolution, "generic"))
|
|
120
|
-
return parseGenericInstantiation(token, resolution as Generic, s)
|
|
121
|
-
return throwParseError(`Unexpected resolution ${printable(resolution)}`)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const maybeParseUnenclosedLiteral = (
|
|
125
|
-
s: DynamicState,
|
|
126
|
-
token: string
|
|
127
|
-
): RawSchema | undefined => {
|
|
128
|
-
const maybeNumber = tryParseNumber(token, { strict: true })
|
|
129
|
-
if (maybeNumber !== undefined)
|
|
130
|
-
return s.ctx.$.node("unit", { unit: maybeNumber })
|
|
131
|
-
|
|
132
|
-
const maybeBigint = tryParseWellFormedBigint(token)
|
|
133
|
-
if (maybeBigint !== undefined)
|
|
134
|
-
return s.ctx.$.node("unit", { unit: maybeBigint })
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
type tryResolve<s extends StaticState, token extends string, $, args> =
|
|
138
|
-
token extends keyof $ ? token
|
|
139
|
-
: `#${token}` extends keyof $ ? token
|
|
140
|
-
: token extends keyof args ? token
|
|
141
|
-
: token extends `${number}` ? token
|
|
142
|
-
: token extends BigintLiteral ? token
|
|
143
|
-
: token extends (
|
|
144
|
-
`${infer submodule extends keyof $ & string}.${infer reference}`
|
|
145
|
-
) ?
|
|
146
|
-
$[submodule] extends { [arkKind]: "module" } ?
|
|
147
|
-
reference extends keyof $[submodule] ?
|
|
148
|
-
token
|
|
149
|
-
: unresolvableError<s, reference, $[submodule], args, [submodule]>
|
|
150
|
-
: ErrorMessage<writeNonSubmoduleDotMessage<submodule>>
|
|
151
|
-
: unresolvableError<s, token, $, args, []>
|
|
152
|
-
|
|
153
|
-
/** Provide valid completions for the current token, or fallback to an
|
|
154
|
-
* unresolvable error if there are none */
|
|
155
|
-
export type unresolvableError<
|
|
156
|
-
s extends StaticState,
|
|
157
|
-
token extends string,
|
|
158
|
-
$,
|
|
159
|
-
args,
|
|
160
|
-
submodulePath extends string[]
|
|
161
|
-
> =
|
|
162
|
-
validReferenceFromToken<token, $, args, submodulePath> extends never ?
|
|
163
|
-
ErrorMessage<
|
|
164
|
-
writeUnresolvableMessage<qualifiedReference<token, submodulePath>>
|
|
165
|
-
>
|
|
166
|
-
: Completion<`${s["scanned"]}${qualifiedReference<
|
|
167
|
-
validReferenceFromToken<token, $, args, submodulePath>,
|
|
168
|
-
submodulePath
|
|
169
|
-
>}`>
|
|
170
|
-
|
|
171
|
-
type qualifiedReference<
|
|
172
|
-
reference extends string,
|
|
173
|
-
submodulePath extends string[]
|
|
174
|
-
> = join<[...submodulePath, reference], ".">
|
|
175
|
-
|
|
176
|
-
type validReferenceFromToken<
|
|
177
|
-
token extends string,
|
|
178
|
-
$,
|
|
179
|
-
args,
|
|
180
|
-
submodulePath extends string[]
|
|
181
|
-
> = Extract<
|
|
182
|
-
submodulePath extends [] ? BaseCompletions<$, args> : keyof $,
|
|
183
|
-
`${token}${string}`
|
|
184
|
-
>
|
|
185
|
-
|
|
186
|
-
export const writeMissingOperandMessage = (s: DynamicState): string => {
|
|
187
|
-
const operator = s.previousOperator()
|
|
188
|
-
return operator ?
|
|
189
|
-
writeMissingRightOperandMessage(operator, s.scanner.unscanned)
|
|
190
|
-
: writeExpressionExpectedMessage(s.scanner.unscanned)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export type writeMissingRightOperandMessage<
|
|
194
|
-
token extends string,
|
|
195
|
-
unscanned extends string = ""
|
|
196
|
-
> = `Token '${token}' requires a right operand${unscanned extends "" ? ""
|
|
197
|
-
: ` before '${unscanned}'`}`
|
|
198
|
-
|
|
199
|
-
export const writeMissingRightOperandMessage = <
|
|
200
|
-
token extends string,
|
|
201
|
-
unscanned extends string
|
|
202
|
-
>(
|
|
203
|
-
token: token,
|
|
204
|
-
unscanned = "" as unscanned
|
|
205
|
-
): writeMissingRightOperandMessage<token, unscanned> =>
|
|
206
|
-
`Token '${token}' requires a right operand${
|
|
207
|
-
unscanned ? (` before '${unscanned}'` as any) : ""
|
|
208
|
-
}`
|
|
209
|
-
|
|
210
|
-
export const writeExpressionExpectedMessage = <unscanned extends string>(
|
|
211
|
-
unscanned: unscanned
|
|
212
|
-
): writeExpressionExpectedMessage<unscanned> =>
|
|
213
|
-
`Expected an expression${unscanned ? ` before '${unscanned}'` : ""}` as never
|
|
214
|
-
|
|
215
|
-
export type writeExpressionExpectedMessage<unscanned extends string> =
|
|
216
|
-
`Expected an expression${unscanned extends "" ? ""
|
|
217
|
-
: ` before '${unscanned}'`}`
|