arktype 1.0.28-alpha → 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 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +8 -0
- 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/{mjs/scopes/validation → scopes/utils}/creditCard.js +14 -12
- package/dist/scopes/utils/creditCard.js.map +1 -0
- package/dist/{types/scopes/validation → scopes/utils}/date.d.ts +3 -2
- package/dist/scopes/utils/date.d.ts.map +1 -0
- package/dist/{mjs/scopes/validation → scopes/utils}/date.js +24 -19
- 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 +41 -108
- 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
- package/LICENSE +0 -7
- package/README.md +0 -351
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -163
- package/dist/cjs/nodes/discriminate.js +0 -265
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -167
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -16
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -108
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -390
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -141
- package/dist/mjs/nodes/discriminate.js +0 -250
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -149
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -2
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/expressions.js +0 -89
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -112
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -373
- package/dist/mjs/utils/chainableNoOpProxy.js +0 -15
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/paths.js +0 -18
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- package/dist/tsconfig.temp.tsbuildinfo +0 -1
- package/dist/types/main.d.ts +0 -15
- package/dist/types/nodes/branch.d.ts +0 -29
- package/dist/types/nodes/compose.d.ts +0 -98
- package/dist/types/nodes/discriminate.d.ts +0 -26
- package/dist/types/nodes/node.d.ts +0 -69
- package/dist/types/nodes/predicate.d.ts +0 -18
- package/dist/types/nodes/rules/class.d.ts +0 -4
- package/dist/types/nodes/rules/collapsibleSet.d.ts +0 -4
- package/dist/types/nodes/rules/divisor.d.ts +0 -3
- package/dist/types/nodes/rules/props.d.ts +0 -43
- package/dist/types/nodes/rules/range.d.ts +0 -34
- package/dist/types/nodes/rules/regex.d.ts +0 -5
- package/dist/types/nodes/rules/rules.d.ts +0 -33
- package/dist/types/parse/ast/ast.d.ts +0 -24
- package/dist/types/parse/ast/bound.d.ts +0 -27
- package/dist/types/parse/ast/config.d.ts +0 -15
- package/dist/types/parse/ast/distributableFunction.d.ts +0 -18
- package/dist/types/parse/ast/divisor.d.ts +0 -13
- package/dist/types/parse/ast/intersection.d.ts +0 -31
- package/dist/types/parse/ast/keyof.d.ts +0 -10
- package/dist/types/parse/ast/morph.d.ts +0 -18
- package/dist/types/parse/ast/narrow.d.ts +0 -21
- package/dist/types/parse/ast/node.d.ts +0 -35
- package/dist/types/parse/ast/tuple.d.ts +0 -41
- package/dist/types/parse/ast/union.d.ts +0 -17
- package/dist/types/parse/ast/utils.d.ts +0 -7
- package/dist/types/parse/definition.d.ts +0 -30
- package/dist/types/parse/record.d.ts +0 -26
- package/dist/types/parse/string/reduce/dynamic.d.ts +0 -34
- package/dist/types/parse/string/reduce/shared.d.ts +0 -25
- package/dist/types/parse/string/reduce/static.d.ts +0 -130
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +0 -28
- package/dist/types/parse/string/shift/operand/operand.d.ts +0 -9
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +0 -18
- package/dist/types/parse/string/shift/operator/bounds.d.ts +0 -21
- package/dist/types/parse/string/shift/operator/divisor.d.ts +0 -13
- package/dist/types/parse/string/shift/operator/operator.d.ts +0 -13
- package/dist/types/parse/string/shift/scanner.d.ts +0 -89
- package/dist/types/parse/string/string.d.ts +0 -25
- package/dist/types/scopes/ark.d.ts +0 -209
- package/dist/types/scopes/cache.d.ts +0 -14
- package/dist/types/scopes/expressions.d.ts +0 -136
- package/dist/types/scopes/jsObjects.d.ts +0 -26
- package/dist/types/scopes/scope.d.ts +0 -99
- package/dist/types/scopes/tsKeywords.d.ts +0 -50
- package/dist/types/scopes/type.d.ts +0 -67
- package/dist/types/scopes/validation/creditCard.d.ts +0 -2
- package/dist/types/scopes/validation/validation.d.ts +0 -49
- package/dist/types/traverse/problems.d.ts +0 -88
- package/dist/types/traverse/traverse.d.ts +0 -55
- package/dist/types/utils/chainableNoOpProxy.d.ts +0 -14
- package/dist/types/utils/data.d.ts +0 -13
- package/dist/types/utils/domains.d.ts +0 -32
- package/dist/types/utils/errors.d.ts +0 -6
- package/dist/types/utils/freeze.d.ts +0 -2
- package/dist/types/utils/generics.d.ts +0 -125
- package/dist/types/utils/numericLiterals.d.ts +0 -46
- package/dist/types/utils/objectKinds.d.ts +0 -84
- package/dist/types/utils/paths.d.ts +0 -9
- package/dist/types/utils/serialize.d.ts +0 -37
- package/dist/types/utils/unionToTuple.d.ts +0 -11
|
@@ -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
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { isKeyOf, type Dict } from "@arktype/util"
|
|
2
|
+
import type { Comparator } from "./operator/bounds.js"
|
|
3
|
+
|
|
4
|
+
export class Scanner<Lookahead extends string = string> {
|
|
5
|
+
private chars: string[]
|
|
6
|
+
private i: number
|
|
7
|
+
|
|
8
|
+
constructor(def: string) {
|
|
9
|
+
this.chars = [...def]
|
|
10
|
+
this.i = 0
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Get lookahead and advance scanner by one */
|
|
14
|
+
shift() {
|
|
15
|
+
return (this.chars[this.i++] ?? "") as Lookahead
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get lookahead() {
|
|
19
|
+
return (this.chars[this.i] ?? "") as Lookahead
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get length() {
|
|
23
|
+
return this.chars.length
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
shiftUntil(condition: Scanner.UntilCondition): string {
|
|
27
|
+
let shifted = ""
|
|
28
|
+
while (this.lookahead) {
|
|
29
|
+
if (condition(this, shifted)) {
|
|
30
|
+
if (shifted[shifted.length - 1] === Scanner.escapeToken) {
|
|
31
|
+
shifted = shifted.slice(0, -1)
|
|
32
|
+
} else {
|
|
33
|
+
break
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
shifted += this.shift()
|
|
37
|
+
}
|
|
38
|
+
return shifted
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
shiftUntilNextTerminator() {
|
|
42
|
+
this.shiftUntilNonWhitespace()
|
|
43
|
+
return this.shiftUntil(Scanner.lookaheadIsTerminator)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
shiftUntilNonWhitespace() {
|
|
47
|
+
return this.shiftUntil(Scanner.lookaheadIsNotWhitespace)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
jumpToIndex(i: number) {
|
|
51
|
+
this.i = i < 0 ? this.length + i : i
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get location() {
|
|
55
|
+
return this.i
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get unscanned() {
|
|
59
|
+
return this.chars.slice(this.i, this.length).join("")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get scanned() {
|
|
63
|
+
return this.chars.slice(0, this.i).join("")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
sliceChars(start: number, end?: number) {
|
|
67
|
+
return this.chars.slice(start, end).join("")
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
lookaheadIs<Char extends Lookahead>(char: Char): this is Scanner<Char> {
|
|
71
|
+
return this.lookahead === char
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
lookaheadIsIn<Tokens extends Dict>(
|
|
75
|
+
tokens: Tokens
|
|
76
|
+
): this is Scanner<Extract<keyof Tokens, string>> {
|
|
77
|
+
return this.lookahead in tokens
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export namespace Scanner {
|
|
82
|
+
export type UntilCondition = (scanner: Scanner, shifted: string) => boolean
|
|
83
|
+
|
|
84
|
+
export type OnInputEndFn = (scanner: Scanner, shifted: string) => string
|
|
85
|
+
|
|
86
|
+
export type ShiftUntilOptions = {
|
|
87
|
+
onInputEnd?: OnInputEndFn
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const lookaheadIsTerminator: UntilCondition = (scanner: Scanner) =>
|
|
91
|
+
scanner.lookahead in terminatingChars
|
|
92
|
+
|
|
93
|
+
export const lookaheadIsNotWhitespace: UntilCondition = (scanner: Scanner) =>
|
|
94
|
+
!(scanner.lookahead in whiteSpaceTokens)
|
|
95
|
+
|
|
96
|
+
export const terminatingChars = {
|
|
97
|
+
"<": true,
|
|
98
|
+
">": true,
|
|
99
|
+
"=": true,
|
|
100
|
+
"|": true,
|
|
101
|
+
"&": true,
|
|
102
|
+
")": true,
|
|
103
|
+
"[": true,
|
|
104
|
+
"%": true,
|
|
105
|
+
" ": true,
|
|
106
|
+
",": true
|
|
107
|
+
} as const
|
|
108
|
+
|
|
109
|
+
export type TerminatingChar = keyof typeof terminatingChars
|
|
110
|
+
|
|
111
|
+
export const finalizingLookaheads = {
|
|
112
|
+
">": true,
|
|
113
|
+
",": true,
|
|
114
|
+
"": true
|
|
115
|
+
} as const
|
|
116
|
+
|
|
117
|
+
export type FinalizingLookahead = keyof typeof finalizingLookaheads
|
|
118
|
+
|
|
119
|
+
export type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>"
|
|
120
|
+
|
|
121
|
+
export type PostfixToken = "[]"
|
|
122
|
+
|
|
123
|
+
export type OperatorToken = InfixToken | PostfixToken
|
|
124
|
+
|
|
125
|
+
export const escapeToken = "\\"
|
|
126
|
+
|
|
127
|
+
export type EscapeToken = typeof escapeToken
|
|
128
|
+
|
|
129
|
+
export const whiteSpaceTokens = {
|
|
130
|
+
" ": true,
|
|
131
|
+
"\n": true
|
|
132
|
+
} as const
|
|
133
|
+
|
|
134
|
+
export type WhiteSpaceToken = keyof typeof whiteSpaceTokens
|
|
135
|
+
|
|
136
|
+
export const lookaheadIsFinalizing = (
|
|
137
|
+
lookahead: string,
|
|
138
|
+
unscanned: string
|
|
139
|
+
): lookahead is ">" | "," =>
|
|
140
|
+
lookahead === ">"
|
|
141
|
+
? unscanned[0] === "="
|
|
142
|
+
? // >== would only occur in an expression like Array<number>==5
|
|
143
|
+
// otherwise, >= would only occur as part of a bound like number>=5
|
|
144
|
+
unscanned[1] === "="
|
|
145
|
+
: // if > is the end of a generic instantiation, the next token will be an operator or the end of the string
|
|
146
|
+
unscanned.trimStart() === "" ||
|
|
147
|
+
isKeyOf(unscanned.trimStart()[0], Scanner.terminatingChars)
|
|
148
|
+
: // if the lookahead is a finalizing token but not >, it's unambiguously a finalizer (currently just ",")
|
|
149
|
+
lookahead === ","
|
|
150
|
+
|
|
151
|
+
export type lookaheadIsFinalizing<
|
|
152
|
+
lookahead extends string,
|
|
153
|
+
unscanned extends string
|
|
154
|
+
> = lookahead extends ">"
|
|
155
|
+
? unscanned extends `=${infer nextUnscanned}`
|
|
156
|
+
? nextUnscanned extends `=${string}`
|
|
157
|
+
? true
|
|
158
|
+
: false
|
|
159
|
+
: Scanner.skipWhitespace<unscanned> extends
|
|
160
|
+
| ""
|
|
161
|
+
| `${TerminatingChar}${string}`
|
|
162
|
+
? true
|
|
163
|
+
: false
|
|
164
|
+
: lookahead extends ","
|
|
165
|
+
? true
|
|
166
|
+
: false
|
|
167
|
+
|
|
168
|
+
export type shift<
|
|
169
|
+
lookahead extends string,
|
|
170
|
+
unscanned extends string
|
|
171
|
+
> = `${lookahead}${unscanned}`
|
|
172
|
+
|
|
173
|
+
export type shiftUntil<
|
|
174
|
+
unscanned extends string,
|
|
175
|
+
terminator extends string,
|
|
176
|
+
scanned extends string = ""
|
|
177
|
+
> = unscanned extends shift<infer lookahead, infer nextUnscanned>
|
|
178
|
+
? lookahead extends terminator
|
|
179
|
+
? scanned extends `${infer base}${EscapeToken}`
|
|
180
|
+
? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`>
|
|
181
|
+
: [scanned, unscanned]
|
|
182
|
+
: shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`>
|
|
183
|
+
: [scanned, ""]
|
|
184
|
+
|
|
185
|
+
export type shiftUntilNot<
|
|
186
|
+
unscanned extends string,
|
|
187
|
+
nonTerminator extends string,
|
|
188
|
+
scanned extends string = ""
|
|
189
|
+
> = unscanned extends shift<infer lookahead, infer nextUnscanned>
|
|
190
|
+
? lookahead extends nonTerminator
|
|
191
|
+
? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`>
|
|
192
|
+
: [scanned, unscanned]
|
|
193
|
+
: [scanned, ""]
|
|
194
|
+
|
|
195
|
+
export type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<
|
|
196
|
+
unscanned,
|
|
197
|
+
TerminatingChar
|
|
198
|
+
>
|
|
199
|
+
|
|
200
|
+
export type skipWhitespace<unscanned extends string> = shiftUntilNot<
|
|
201
|
+
unscanned,
|
|
202
|
+
WhiteSpaceToken
|
|
203
|
+
>[1]
|
|
204
|
+
|
|
205
|
+
export type shiftResult<scanned extends string, unscanned extends string> = [
|
|
206
|
+
scanned,
|
|
207
|
+
unscanned
|
|
208
|
+
]
|
|
209
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Root } from "@arktype/schema"
|
|
2
|
+
import { throwParseError, type ErrorMessage } from "@arktype/util"
|
|
3
|
+
import type { ParseContext } from "../../scope.js"
|
|
4
|
+
import type { inferAst } from "../semantic/semantic.js"
|
|
5
|
+
import { writeUnsatisfiableExpressionError } from "../semantic/validate.js"
|
|
6
|
+
import { DynamicState, type DynamicStateWithRoot } from "./reduce/dynamic.js"
|
|
7
|
+
import type { StringifiablePrefixOperator } from "./reduce/shared.js"
|
|
8
|
+
import type { StaticState, state } from "./reduce/static.js"
|
|
9
|
+
import { parseOperand } from "./shift/operand/operand.js"
|
|
10
|
+
import {
|
|
11
|
+
parseOperator,
|
|
12
|
+
writeUnexpectedCharacterMessage
|
|
13
|
+
} from "./shift/operator/operator.js"
|
|
14
|
+
|
|
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
|
+
/**
|
|
22
|
+
* Try to parse the definition from right to left using the most common syntax.
|
|
23
|
+
* This can be much more efficient for simple definitions.
|
|
24
|
+
*/
|
|
25
|
+
export type parseString<def extends string, $, args> = def extends keyof $
|
|
26
|
+
? // def could also be a generic reference here, in which case it will
|
|
27
|
+
// fail semantic validation because it has no args
|
|
28
|
+
def
|
|
29
|
+
: def extends `${infer child}[]`
|
|
30
|
+
? child extends keyof $
|
|
31
|
+
? [child, "[]"]
|
|
32
|
+
: fullStringParse<def, $, args>
|
|
33
|
+
: fullStringParse<def, $, args>
|
|
34
|
+
|
|
35
|
+
export type inferString<def extends string, $, args> = inferAst<
|
|
36
|
+
parseString<def, $, args>,
|
|
37
|
+
$,
|
|
38
|
+
args
|
|
39
|
+
>
|
|
40
|
+
|
|
41
|
+
export type BaseCompletions<$, args, otherSuggestions extends string = never> =
|
|
42
|
+
| (keyof $ & string)
|
|
43
|
+
| (keyof args & string)
|
|
44
|
+
| StringifiablePrefixOperator
|
|
45
|
+
| otherSuggestions
|
|
46
|
+
|
|
47
|
+
export const fullStringParse = (def: string, ctx: ParseContext) => {
|
|
48
|
+
const s = new DynamicState(def, ctx)
|
|
49
|
+
parseOperand(s)
|
|
50
|
+
const result = parseUntilFinalizer(s).root
|
|
51
|
+
s.scanner.shiftUntilNonWhitespace()
|
|
52
|
+
if (s.scanner.lookahead) {
|
|
53
|
+
// throw a parse error if non-whitespace characters made it here without being parsed
|
|
54
|
+
throwParseError(writeUnexpectedCharacterMessage(s.scanner.lookahead))
|
|
55
|
+
}
|
|
56
|
+
// TODO: would this ever happen?
|
|
57
|
+
return result.isNever()
|
|
58
|
+
? throwParseError(writeUnsatisfiableExpressionError(def))
|
|
59
|
+
: result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type fullStringParse<def extends string, $, args> = extractFinalizedResult<
|
|
63
|
+
parseUntilFinalizer<state.initialize<def>, $, args>
|
|
64
|
+
>
|
|
65
|
+
|
|
66
|
+
export const parseUntilFinalizer = (s: DynamicState) => {
|
|
67
|
+
while (s.finalizer === undefined) {
|
|
68
|
+
next(s)
|
|
69
|
+
}
|
|
70
|
+
return s as DynamicStateWithRoot
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type parseUntilFinalizer<
|
|
74
|
+
s extends StaticState,
|
|
75
|
+
$,
|
|
76
|
+
args
|
|
77
|
+
> = s["finalizer"] extends undefined
|
|
78
|
+
? parseUntilFinalizer<next<s, $, args>, $, args>
|
|
79
|
+
: s
|
|
80
|
+
|
|
81
|
+
const next = (s: DynamicState) =>
|
|
82
|
+
s.hasRoot() ? parseOperator(s) : parseOperand(s)
|
|
83
|
+
|
|
84
|
+
type next<s extends StaticState, $, args> = s["root"] extends undefined
|
|
85
|
+
? parseOperand<s, $, args>
|
|
86
|
+
: parseOperator<s, $, args>
|
|
87
|
+
|
|
88
|
+
export type extractFinalizedResult<s extends StaticState> =
|
|
89
|
+
s["finalizer"] extends ErrorMessage
|
|
90
|
+
? s["finalizer"]
|
|
91
|
+
: s["finalizer"] extends ""
|
|
92
|
+
? s["root"]
|
|
93
|
+
: state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>
|