arktype 2.0.0-rc.0 → 2.0.0-rc.10
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/out/config.d.ts +1 -1
- package/out/generic.d.ts +55 -53
- package/out/generic.js +9 -11
- package/out/index.d.ts +5 -5
- package/out/index.js +3 -2
- package/out/keywords/{builtin.d.ts → builtins.d.ts} +9 -5
- package/out/keywords/{builtin.js → builtins.js} +2 -2
- package/out/keywords/constructors/Array.d.ts +12 -8
- package/out/keywords/constructors/Array.js +7 -7
- package/out/keywords/constructors/Date.d.ts +32 -0
- package/out/keywords/constructors/FormData.d.ts +12 -6
- package/out/keywords/constructors/FormData.js +19 -6
- package/out/keywords/constructors/TypedArray.d.ts +18 -14
- package/out/keywords/constructors/TypedArray.js +2 -2
- package/out/keywords/constructors/constructors.d.ts +21 -32
- package/out/keywords/constructors/constructors.js +13 -28
- package/out/keywords/inference.d.ts +227 -0
- package/out/keywords/keywords.d.ts +61 -0
- package/out/keywords/keywords.js +26 -0
- package/out/keywords/number/epoch.d.ts +1 -2
- package/out/keywords/number/epoch.js +2 -2
- package/out/keywords/number/integer.js +2 -2
- package/out/keywords/number/number.d.ts +37 -9
- package/out/keywords/number/number.js +17 -5
- package/out/keywords/string/alpha.d.ts +1 -1
- package/out/keywords/string/alphanumeric.d.ts +1 -1
- package/out/keywords/string/capitalize.d.ts +11 -10
- package/out/keywords/string/capitalize.js +4 -4
- package/out/keywords/string/creditCard.d.ts +1 -1
- package/out/keywords/string/creditCard.js +2 -2
- package/out/keywords/string/date.d.ts +27 -26
- package/out/keywords/string/date.js +16 -16
- package/out/keywords/string/digits.d.ts +1 -1
- package/out/keywords/string/email.d.ts +1 -1
- package/out/keywords/string/integer.d.ts +11 -10
- package/out/keywords/string/integer.js +7 -7
- package/out/keywords/string/ip.d.ts +12 -12
- package/out/keywords/string/ip.js +5 -5
- package/out/keywords/string/json.d.ts +12 -10
- package/out/keywords/string/json.js +24 -16
- package/out/keywords/string/lower.d.ts +11 -10
- package/out/keywords/string/lower.js +4 -4
- package/out/keywords/string/normalize.d.ts +63 -47
- package/out/keywords/string/normalize.js +14 -14
- package/out/keywords/string/numeric.d.ts +11 -10
- package/out/keywords/string/numeric.js +7 -7
- package/out/keywords/string/semver.d.ts +1 -1
- package/out/keywords/string/string.d.ts +78 -26
- package/out/keywords/string/string.js +5 -5
- package/out/keywords/string/trim.d.ts +11 -10
- package/out/keywords/string/trim.js +4 -4
- package/out/keywords/string/upper.d.ts +11 -10
- package/out/keywords/string/upper.js +4 -4
- package/out/keywords/string/url.d.ts +11 -10
- package/out/keywords/string/url.js +8 -8
- package/out/keywords/string/uuid.d.ts +27 -23
- package/out/keywords/string/uuid.js +3 -3
- package/out/keywords/ts.d.ts +44 -22
- package/out/keywords/ts.js +20 -14
- package/out/keywords/utils.d.ts +1 -1
- package/out/keywords/utils.js +1 -1
- package/out/{subtypes → methods}/array.d.ts +6 -6
- package/out/methods/base.d.ts +96 -0
- package/out/{subtypes → methods}/date.d.ts +5 -5
- package/out/{subtypes → methods}/instantiate.d.ts +5 -4
- package/out/methods/morph.d.ts +12 -0
- package/out/{subtypes → methods}/number.d.ts +6 -6
- package/out/methods/object.d.ts +81 -0
- package/out/{subtypes → methods}/string.d.ts +7 -7
- package/out/{subtypes → methods}/validator.d.ts +2 -2
- package/out/module.d.ts +2 -2
- package/out/parser/{semantic → ast}/bounds.d.ts +6 -4
- package/out/parser/ast/default.d.ts +8 -0
- package/out/parser/ast/infer.d.ts +39 -0
- package/out/parser/ast/utils.d.ts +4 -0
- package/out/parser/ast/validate.d.ts +26 -0
- package/out/parser/definition.d.ts +9 -12
- package/out/parser/objectLiteral.d.ts +11 -25
- package/out/parser/objectLiteral.js +6 -24
- package/out/parser/{string/reduce → reduce}/dynamic.d.ts +5 -7
- package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
- package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
- package/out/parser/{string/reduce → reduce}/static.d.ts +1 -1
- package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
- package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -1
- package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +7 -8
- package/out/parser/{string/shift → shift}/operand/unenclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -6
- package/out/parser/{string/shift → shift}/operator/default.d.ts +3 -6
- package/out/parser/{string/shift → shift}/operator/default.js +1 -6
- package/out/parser/{string/shift → shift}/operator/operator.js +2 -2
- package/out/parser/{string/shift → shift}/scanner.d.ts +6 -2
- package/out/parser/{string/shift → shift}/scanner.js +7 -5
- package/out/parser/{string/string.d.ts → string.d.ts} +8 -8
- package/out/parser/{string/string.js → string.js} +2 -0
- package/out/parser/tuple.d.ts +54 -43
- package/out/parser/tuple.js +36 -30
- package/out/scope.d.ts +41 -34
- package/out/scope.js +48 -27
- package/out/type.d.ts +25 -20
- package/out/type.js +23 -14
- package/package.json +3 -3
- package/out/intersect.d.ts +0 -28
- package/out/keys.d.ts +0 -16
- package/out/keywords/ark.d.ts +0 -33
- package/out/keywords/ark.js +0 -18
- package/out/keywords/ast.d.ts +0 -193
- package/out/parser/semantic/default.d.ts +0 -8
- package/out/parser/semantic/default.js +0 -1
- package/out/parser/semantic/infer.d.ts +0 -33
- package/out/parser/semantic/utils.d.ts +0 -4
- package/out/parser/semantic/validate.d.ts +0 -29
- package/out/subtypes/base.d.ts +0 -84
- package/out/subtypes/morph.d.ts +0 -13
- package/out/subtypes/object.d.ts +0 -33
- /package/out/{intersect.js → keywords/constructors/Date.js} +0 -0
- /package/out/keywords/{ast.js → inference.js} +0 -0
- /package/out/{subtypes → methods}/array.js +0 -0
- /package/out/{subtypes → methods}/base.js +0 -0
- /package/out/{subtypes → methods}/date.js +0 -0
- /package/out/{subtypes → methods}/instantiate.js +0 -0
- /package/out/{subtypes → methods}/morph.js +0 -0
- /package/out/{subtypes → methods}/number.js +0 -0
- /package/out/{subtypes → methods}/object.js +0 -0
- /package/out/{subtypes → methods}/string.js +0 -0
- /package/out/{subtypes → methods}/validator.js +0 -0
- /package/out/parser/{semantic → ast}/bounds.js +0 -0
- /package/out/{keys.js → parser/ast/default.js} +0 -0
- /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
- /package/out/parser/{semantic → ast}/divisor.js +0 -0
- /package/out/parser/{semantic → ast}/infer.js +0 -0
- /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
- /package/out/parser/{semantic → ast}/keyof.js +0 -0
- /package/out/parser/{semantic → ast}/utils.js +0 -0
- /package/out/parser/{semantic → ast}/validate.js +0 -0
- /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
- /package/out/parser/{string/reduce → reduce}/static.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/operand.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/operand.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/bounds.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/operator.d.ts +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isKeyOf } from "@ark/util";
|
|
1
|
+
import { isKeyOf, throwParseError } from "@ark/util";
|
|
2
2
|
import { tryParseDate, writeInvalidDateMessage } from "./date.js";
|
|
3
3
|
export const parseEnclosed = (s, enclosing) => {
|
|
4
4
|
const enclosed = s.scanner.shiftUntil(untilLookaheadIsClosing[enclosingTokens[enclosing]]);
|
|
@@ -7,8 +7,12 @@ export const parseEnclosed = (s, enclosing) => {
|
|
|
7
7
|
// Shift the scanner one additional time for the second enclosing token
|
|
8
8
|
s.scanner.shift();
|
|
9
9
|
if (enclosing === "/") {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
try {
|
|
11
|
+
new RegExp(enclosed);
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
throwParseError(String(e));
|
|
15
|
+
}
|
|
12
16
|
s.root = s.ctx.$.node("intersection", {
|
|
13
17
|
domain: "string",
|
|
14
18
|
pattern: enclosed
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { writeUnresolvableMessage, type BaseRoot, type GenericAst, type GenericRoot, type arkKind, type genericParamNames, type resolvableReferenceIn, type writeNonSubmoduleDotMessage } from "@ark/schema";
|
|
2
|
-
import { type BigintLiteral, type
|
|
3
|
-
import type { ArkAmbient } from "
|
|
4
|
-
import type {
|
|
2
|
+
import { type BigintLiteral, type NumberLiteral, type join, type lastOf } from "@ark/util";
|
|
3
|
+
import type { ArkAmbient } from "../../../config.ts";
|
|
4
|
+
import type { resolutionToAst } from "../../../scope.ts";
|
|
5
|
+
import type { GenericInstantiationAst, InferredAst } from "../../ast/infer.ts";
|
|
5
6
|
import type { DynamicState } from "../../reduce/dynamic.ts";
|
|
6
7
|
import type { StaticState, state } from "../../reduce/static.ts";
|
|
7
8
|
import type { BaseCompletions } from "../../string.ts";
|
|
@@ -9,17 +10,15 @@ import type { Scanner } from "../scanner.ts";
|
|
|
9
10
|
import { parseGenericArgs, writeInvalidGenericArgCountMessage, type ParsedArgs } from "./genericArgs.ts";
|
|
10
11
|
export declare const parseUnenclosed: (s: DynamicState) => void;
|
|
11
12
|
export type parseUnenclosed<s extends StaticState, $, args> = Scanner.shiftUntilNextTerminator<s["unscanned"]> extends (Scanner.shiftResult<infer token, infer unscanned>) ? tryResolve<s, unscanned, token, $, args> extends state.from<infer s> ? s : never : never;
|
|
12
|
-
type parseResolution<s extends StaticState, unscanned extends string, alias extends string, resolution, $, args> =
|
|
13
|
-
resolution
|
|
14
|
-
] extends [anyOrNever] ? state.setRoot<s, alias, unscanned> : resolution extends GenericAst ? parseGenericInstantiation<alias, resolution, state.scanTo<s, unscanned>, $, args> : state.setRoot<s, alias, unscanned>;
|
|
13
|
+
type parseResolution<s extends StaticState, unscanned extends string, alias extends string, resolution, $, args> = resolutionToAst<alias, resolution> extends infer ast ? ast extends GenericAst ? parseGenericInstantiation<alias, ast, state.scanTo<s, unscanned>, $, args> : state.setRoot<s, ast, unscanned> : never;
|
|
15
14
|
export declare const parseGenericInstantiation: (name: string, g: GenericRoot, s: DynamicState) => BaseRoot;
|
|
16
15
|
export type parseGenericInstantiation<name extends string, g extends GenericAst, s extends StaticState, $, args> = Scanner.skipWhitespace<s["unscanned"]> extends `<${infer unscanned}` ? parseGenericArgs<name, g, unscanned, $, args> extends infer result ? result extends ParsedArgs<infer argAsts, infer nextUnscanned> ? state.setRoot<s, GenericInstantiationAst<g, argAsts>, nextUnscanned> : result : never : state.error<writeInvalidGenericArgCountMessage<name, genericParamNames<g["paramsAst"]>, [
|
|
17
16
|
]>>;
|
|
18
|
-
type tryResolve<s extends StaticState, unscanned extends string, token extends string, $, args> = token extends keyof args ? parseResolution<s, unscanned, token, args[token], $, args> : token extends keyof $ ? parseResolution<s, unscanned, token, $[token], $, args> : `#${token}` extends keyof $ ? parseResolution<s, unscanned, token, $[`#${token}`], $, args> : token extends keyof ArkAmbient.$ ? parseResolution<s, unscanned, token, ArkAmbient.$[token], $, args> : token extends
|
|
17
|
+
type tryResolve<s extends StaticState, unscanned extends string, token extends string, $, args> = token extends keyof args ? parseResolution<s, unscanned, token, args[token], $, args> : token extends keyof $ ? parseResolution<s, unscanned, token, $[token], $, args> : `#${token}` extends keyof $ ? parseResolution<s, unscanned, token, $[`#${token}`], $, args> : token extends keyof ArkAmbient.$ ? parseResolution<s, unscanned, token, ArkAmbient.$[token], $, args> : token extends NumberLiteral<infer n> ? state.setRoot<s, InferredAst<n, token>, unscanned> : token extends (`${infer submodule extends keyof $ & string}.${infer reference}`) ? tryResolveSubmodule<token, $[submodule], reference, s, unscanned, $, args, [
|
|
19
18
|
submodule
|
|
20
19
|
]> : token extends (`${infer submodule extends keyof ArkAmbient.$ & string}.${infer reference}`) ? tryResolveSubmodule<token, ArkAmbient.$[submodule], reference, s, unscanned, $, args, [
|
|
21
20
|
submodule
|
|
22
|
-
]> : token extends BigintLiteral ? state.setRoot<s,
|
|
21
|
+
]> : token extends BigintLiteral<infer b> ? state.setRoot<s, InferredAst<b, token>, unscanned> : token extends "keyof" ? state.addPrefix<s, "keyof", unscanned> : unresolvableState<s, token, $, args, []>;
|
|
23
22
|
type tryResolveSubmodule<token extends string, resolution, reference extends string, s extends StaticState, unscanned extends string, $, args, submodulePath extends string[]> = resolution extends {
|
|
24
23
|
[arkKind]: "module";
|
|
25
24
|
} ? reference extends keyof resolution ? parseResolution<s, unscanned, token, resolution[reference], $, args> : reference extends (`${infer nestedSubmodule extends keyof resolution & string}.${infer nestedReference}`) ? tryResolveSubmodule<token, resolution[nestedSubmodule], nestedReference, s, unscanned, $, args, [
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hasArkKind, writeUnresolvableMessage } from "@ark/schema";
|
|
2
2
|
import { printable, throwParseError, tryParseWellFormedBigint, tryParseWellFormedNumber } from "@ark/util";
|
|
3
|
-
import { writePrefixedPrivateReferenceMessage } from "
|
|
3
|
+
import { writePrefixedPrivateReferenceMessage } from "../../ast/validate.js";
|
|
4
4
|
import { parseGenericArgs, writeInvalidGenericArgCountMessage } from "./genericArgs.js";
|
|
5
5
|
export const parseUnenclosed = (s) => {
|
|
6
6
|
const token = s.scanner.shiftUntilNextTerminator();
|
|
@@ -24,8 +24,12 @@ const unenclosedToNode = (s, token) => maybeParseReference(s, token) ??
|
|
|
24
24
|
writePrefixedPrivateReferenceMessage(token)
|
|
25
25
|
: writeUnresolvableMessage(token));
|
|
26
26
|
const maybeParseReference = (s, token) => {
|
|
27
|
-
if (s.ctx.args?.[token])
|
|
28
|
-
|
|
27
|
+
if (s.ctx.args?.[token]) {
|
|
28
|
+
const arg = s.ctx.args[token];
|
|
29
|
+
if (typeof arg !== "string")
|
|
30
|
+
return arg;
|
|
31
|
+
return s.ctx.$.node("alias", { reference: arg }, { prereduced: true });
|
|
32
|
+
}
|
|
29
33
|
const resolution = s.ctx.$.maybeResolve(token);
|
|
30
34
|
if (hasArkKind(resolution, "root"))
|
|
31
35
|
return resolution;
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import { type BaseRoot, type BoundKind } from "@ark/schema";
|
|
2
2
|
import { type keySet } from "@ark/util";
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
3
|
+
import type { DateLiteral } from "../../../keywords/inference.ts";
|
|
4
|
+
import type { InferredAst } from "../../ast/infer.ts";
|
|
5
|
+
import type { astToString } from "../../ast/utils.ts";
|
|
5
6
|
import type { DynamicState, DynamicStateWithRoot } from "../../reduce/dynamic.ts";
|
|
6
7
|
import { writeUnpairableComparatorMessage, type Comparator, type InvertedComparators, type MaxComparator } from "../../reduce/shared.ts";
|
|
7
|
-
import type {
|
|
8
|
+
import type { state, StaticState } from "../../reduce/static.ts";
|
|
8
9
|
import type { parseOperand } from "../operand/operand.ts";
|
|
9
10
|
import type { Scanner } from "../scanner.ts";
|
|
10
11
|
export declare const parseBound: (s: DynamicStateWithRoot, start: ComparatorStartChar) => void;
|
|
11
|
-
export type parseBound<s extends StaticState, start extends ComparatorStartChar, unscanned extends string, $, args> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends (Scanner.shiftResult<infer comparator extends Comparator, infer nextUnscanned>) ? s["root"] extends `${infer limit extends
|
|
12
|
+
export type parseBound<s extends StaticState, start extends ComparatorStartChar, unscanned extends string, $, args> = shiftComparator<start, unscanned> extends infer shiftResultOrError ? shiftResultOrError extends (Scanner.shiftResult<infer comparator extends Comparator, infer nextUnscanned>) ? s["root"] extends (InferredAst<Date | number, `${infer limit extends number | DateLiteral}`>) ? state.reduceLeftBound<s, limit, comparator, nextUnscanned> : parseRightBound<state.scanTo<s, nextUnscanned>, comparator, $, args> : shiftResultOrError : never;
|
|
12
13
|
type OneCharComparator = ">" | "<";
|
|
13
14
|
export type ComparatorStartChar = Comparator extends `${infer char}${string}` ? char : never;
|
|
14
15
|
export declare const comparatorStartChars: keySet<ComparatorStartChar>;
|
|
15
16
|
declare const shiftComparator: (s: DynamicState, start: ComparatorStartChar) => Comparator;
|
|
16
17
|
type shiftComparator<start extends ComparatorStartChar, unscanned extends string> = unscanned extends `=${infer nextUnscanned}` ? [`${start}=`, nextUnscanned] : [start & OneCharComparator, unscanned];
|
|
17
18
|
export declare const writeIncompatibleRangeMessage: (l: BoundKind, r: BoundKind) => string;
|
|
18
|
-
export declare const getBoundKinds: (comparator: Comparator, limit:
|
|
19
|
+
export declare const getBoundKinds: (comparator: Comparator, limit: number | DateLiteral, root: BaseRoot, boundKind: BoundExpressionKind) => BoundKind[];
|
|
19
20
|
export declare const parseRightBound: (s: DynamicStateWithRoot, comparator: Comparator) => void;
|
|
20
|
-
export type parseRightBound<s extends StaticState, comparator extends Comparator, $, args> = parseOperand<s, $, args> extends infer nextState extends StaticState ? nextState["root"] extends `${infer limit extends
|
|
21
|
+
export type parseRightBound<s extends StaticState, comparator extends Comparator, $, args> = parseOperand<s, $, args> extends infer nextState extends StaticState ? nextState["root"] extends (InferredAst<unknown, `${infer limit extends number | DateLiteral}`>) ? s["branches"]["leftBound"] extends {} ? comparator extends MaxComparator ? state.reduceRange<s, s["branches"]["leftBound"]["limit"], s["branches"]["leftBound"]["comparator"], comparator, limit, nextState["unscanned"]> : state.error<writeUnpairableComparatorMessage<comparator>> : state.reduceSingleBound<s, comparator, limit, nextState["unscanned"]> : state.error<writeInvalidLimitMessage<comparator, astToString<nextState["root"]>, "right">> : never;
|
|
21
22
|
export declare const writeInvalidLimitMessage: <comparator extends Comparator, limit extends string | number, boundKind extends BoundExpressionKind>(comparator: comparator, limit: limit, boundKind: boundKind) => writeInvalidLimitMessage<comparator, limit, boundKind>;
|
|
22
23
|
export type writeInvalidLimitMessage<comparator extends Comparator, limit extends string | number, boundKind extends BoundExpressionKind> = `Comparator ${boundKind extends "left" ? InvertedComparators[comparator] : comparator} must be ${boundKind extends "left" ? "preceded" : "followed"} by a corresponding literal (was ${limit})`;
|
|
23
24
|
export type BoundExpressionKind = "left" | "right";
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { BaseRoot } from "@ark/schema";
|
|
2
|
-
import {
|
|
3
|
-
import type { DateLiteral } from "
|
|
2
|
+
import type { BigintLiteral, ErrorMessage, NumberLiteral, trim } from "@ark/util";
|
|
3
|
+
import type { DateLiteral } from "../../../keywords/inference.ts";
|
|
4
4
|
import type { DynamicStateWithRoot } from "../../reduce/dynamic.ts";
|
|
5
5
|
import type { StringLiteral } from "../operand/enclosed.ts";
|
|
6
6
|
type UnitLiteralKeyword = "null" | "undefined" | "true" | "false";
|
|
7
7
|
export type UnitLiteral = StringLiteral | BigintLiteral | NumberLiteral | DateLiteral | UnitLiteralKeyword;
|
|
8
|
-
export
|
|
9
|
-
export declare const parseDefault: (s: DynamicStateWithRoot) => ParsedDefault;
|
|
8
|
+
export declare const parseDefault: (s: DynamicStateWithRoot) => BaseRoot;
|
|
10
9
|
export type parseDefault<root, unscanned extends string> = trim<unscanned> extends infer defaultValue extends UnitLiteral ? [
|
|
11
10
|
root,
|
|
12
11
|
"=",
|
|
@@ -14,6 +13,4 @@ export type parseDefault<root, unscanned extends string> = trim<unscanned> exten
|
|
|
14
13
|
] : ErrorMessage<writeNonLiteralDefaultMessage<trim<unscanned>>>;
|
|
15
14
|
export declare const writeNonLiteralDefaultMessage: <defaultDef extends string>(defaultDef: defaultDef) => writeNonLiteralDefaultMessage<defaultDef>;
|
|
16
15
|
export type writeNonLiteralDefaultMessage<defaultDef extends string> = `Default value '${defaultDef}' must a literal value`;
|
|
17
|
-
export declare const shallowDefaultMessage = "Default values must be specified on objects like { isAdmin: 'boolean = false' }";
|
|
18
|
-
export type shallowDefaultMessage = typeof shallowDefaultMessage;
|
|
19
16
|
export {};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { throwParseError } from "@ark/util";
|
|
2
1
|
export const parseDefault = (s) => {
|
|
3
|
-
if (!s.defaultable)
|
|
4
|
-
return throwParseError(shallowDefaultMessage);
|
|
5
2
|
// store the node that will be bounded
|
|
6
3
|
const baseNode = s.unsetRoot();
|
|
7
4
|
s.parseOperand();
|
|
@@ -10,8 +7,6 @@ export const parseDefault = (s) => {
|
|
|
10
7
|
// token from which it was parsed
|
|
11
8
|
if (!defaultNode.hasKind("unit"))
|
|
12
9
|
return s.error(writeNonLiteralDefaultMessage(defaultNode.expression));
|
|
13
|
-
|
|
14
|
-
return [baseNode, "=", defaultNode.unit];
|
|
10
|
+
return baseNode.default(defaultNode.unit);
|
|
15
11
|
};
|
|
16
12
|
export const writeNonLiteralDefaultMessage = (defaultDef) => `Default value '${defaultDef}' must a literal value`;
|
|
17
|
-
export const shallowDefaultMessage = `Default values must be specified on objects like { isAdmin: 'boolean = false' }`;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isKeyOf } from "@ark/util";
|
|
1
|
+
import { isKeyOf, whiteSpaceTokens } from "@ark/util";
|
|
2
2
|
import { Scanner } from "../scanner.js";
|
|
3
3
|
import { comparatorStartChars, parseBound } from "./bounds.js";
|
|
4
4
|
import { parseDivisor } from "./divisor.js";
|
|
@@ -15,7 +15,7 @@ export const parseOperator = (s) => {
|
|
|
15
15
|
s.finalize(lookahead)
|
|
16
16
|
: isKeyOf(lookahead, comparatorStartChars) ? parseBound(s, lookahead)
|
|
17
17
|
: lookahead === "%" ? parseDivisor(s)
|
|
18
|
-
: lookahead
|
|
18
|
+
: lookahead in whiteSpaceTokens ? parseOperator(s)
|
|
19
19
|
: s.error(writeUnexpectedCharacterMessage(lookahead)));
|
|
20
20
|
};
|
|
21
21
|
export const writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
|
|
@@ -21,6 +21,9 @@ export declare class Scanner<lookahead extends string = string> {
|
|
|
21
21
|
lookaheadIs<char extends lookahead>(char: char): this is Scanner<char>;
|
|
22
22
|
lookaheadIsIn<tokens extends Dict>(tokens: tokens): this is Scanner<Extract<keyof tokens, string>>;
|
|
23
23
|
static terminatingChars: {
|
|
24
|
+
readonly " ": true;
|
|
25
|
+
readonly "\n": true;
|
|
26
|
+
readonly "\t": true;
|
|
24
27
|
readonly "<": true;
|
|
25
28
|
readonly ">": true;
|
|
26
29
|
readonly "=": true;
|
|
@@ -29,22 +32,23 @@ export declare class Scanner<lookahead extends string = string> {
|
|
|
29
32
|
readonly ")": true;
|
|
30
33
|
readonly "[": true;
|
|
31
34
|
readonly "%": true;
|
|
32
|
-
readonly " ": true;
|
|
33
35
|
readonly ",": true;
|
|
34
36
|
readonly ":": true;
|
|
37
|
+
readonly "?": true;
|
|
35
38
|
};
|
|
36
39
|
static finalizingLookaheads: {
|
|
37
40
|
readonly ">": true;
|
|
38
41
|
readonly ",": true;
|
|
39
42
|
readonly "": true;
|
|
40
43
|
readonly "=": true;
|
|
44
|
+
readonly "?": true;
|
|
41
45
|
};
|
|
42
46
|
static lookaheadIsTerminator: Scanner.UntilCondition;
|
|
43
47
|
static lookaheadIsNotWhitespace: Scanner.UntilCondition;
|
|
44
48
|
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=";
|
|
45
49
|
}
|
|
46
50
|
export declare namespace Scanner {
|
|
47
|
-
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : Scanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," ? true : false;
|
|
51
|
+
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : Scanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," | "?" ? true : false;
|
|
48
52
|
type UntilCondition = (scanner: Scanner, shifted: string) => boolean;
|
|
49
53
|
type OnInputEndFn = (scanner: Scanner, shifted: string) => string;
|
|
50
54
|
type ShiftUntilOptions = {
|
|
@@ -72,15 +72,17 @@ export class Scanner {
|
|
|
72
72
|
")": true,
|
|
73
73
|
"[": true,
|
|
74
74
|
"%": true,
|
|
75
|
-
" ": true,
|
|
76
75
|
",": true,
|
|
77
|
-
":": true
|
|
76
|
+
":": true,
|
|
77
|
+
"?": true,
|
|
78
|
+
...whiteSpaceTokens
|
|
78
79
|
};
|
|
79
80
|
static finalizingLookaheads = {
|
|
80
81
|
">": true,
|
|
81
82
|
",": true,
|
|
82
83
|
"": true,
|
|
83
|
-
"=": true
|
|
84
|
+
"=": true,
|
|
85
|
+
"?": true
|
|
84
86
|
};
|
|
85
87
|
static lookaheadIsTerminator = (scanner) => scanner.lookahead in this.terminatingChars;
|
|
86
88
|
static lookaheadIsNotWhitespace = (scanner) => !(scanner.lookahead in whiteSpaceTokens);
|
|
@@ -95,6 +97,6 @@ export class Scanner {
|
|
|
95
97
|
// "=" is a finalizer on its own (representing a default value),
|
|
96
98
|
// but not with a second "=" (an equality comparator)
|
|
97
99
|
: lookahead === "=" ? unscanned[0] !== "="
|
|
98
|
-
// ",""
|
|
99
|
-
: lookahead === ",";
|
|
100
|
+
// "," and "?" are unambiguously finalizers
|
|
101
|
+
: lookahead === "," || lookahead === "?";
|
|
100
102
|
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import type { BaseRoot, resolvableReferenceIn } from "@ark/schema";
|
|
2
2
|
import { type ErrorMessage } from "@ark/util";
|
|
3
|
-
import type { ArkAmbient } from "
|
|
4
|
-
import type {
|
|
3
|
+
import type { ArkAmbient } from "../config.ts";
|
|
4
|
+
import type { resolutionToAst } from "../scope.ts";
|
|
5
|
+
import type { inferAstRoot } from "./ast/infer.ts";
|
|
5
6
|
import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.ts";
|
|
6
7
|
import type { StringifiablePrefixOperator } from "./reduce/shared.ts";
|
|
7
8
|
import type { state, StaticState } from "./reduce/static.ts";
|
|
8
9
|
import type { parseOperand } from "./shift/operand/operand.ts";
|
|
9
|
-
import { parseDefault
|
|
10
|
+
import { parseDefault } from "./shift/operator/default.ts";
|
|
10
11
|
import { writeUnexpectedCharacterMessage, type parseOperator } from "./shift/operator/operator.ts";
|
|
11
12
|
/**
|
|
12
13
|
* Try to parse the definition from right to left using the most common syntax.
|
|
13
14
|
* This can be much more efficient for simple definitions.
|
|
14
15
|
*/
|
|
15
|
-
export type parseString<def extends string, $, args> = def extends keyof $ ? def : def extends `${infer child}[]` ? child extends keyof $ ? [
|
|
16
|
-
child,
|
|
16
|
+
export type parseString<def extends string, $, args> = def extends keyof $ ? resolutionToAst<def, $[def]> : def extends `${infer child}[]` ? child extends keyof $ ? [
|
|
17
|
+
resolutionToAst<child, $[child]>,
|
|
17
18
|
"[]"
|
|
18
19
|
] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
|
|
19
20
|
export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
|
|
20
21
|
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<ArkAmbient.$> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
|
|
21
|
-
export
|
|
22
|
-
export declare const fullStringParse: (s: DynamicState) => StringParseResult;
|
|
22
|
+
export declare const fullStringParse: (s: DynamicState) => BaseRoot;
|
|
23
23
|
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
|
|
24
24
|
export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithRoot;
|
|
25
25
|
export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
|
|
26
26
|
declare const next: (s: DynamicState) => void;
|
|
27
27
|
type next<s extends StaticState, $, args> = s["root"] extends undefined ? parseOperand<s, $, args> : parseOperator<s, $, args>;
|
|
28
|
-
export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
|
|
28
|
+
export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "?" ? [s["root"], "?"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
|
|
29
29
|
export {};
|
|
@@ -9,6 +9,8 @@ export const fullStringParse = (s) => {
|
|
|
9
9
|
}
|
|
10
10
|
if (s.finalizer === "=")
|
|
11
11
|
result = parseDefault(s);
|
|
12
|
+
else if (s.finalizer === "?")
|
|
13
|
+
result = result.optional();
|
|
12
14
|
s.scanner.shiftUntilNonWhitespace();
|
|
13
15
|
if (s.scanner.lookahead) {
|
|
14
16
|
// throw a parse error if non-whitespace characters made it here without being parsed
|
package/out/parser/tuple.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { type BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
|
|
2
|
-
import { type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
|
|
3
|
-
import type { inferIntersection } from "../
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
1
|
+
import { type BaseParseContext, type BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
|
|
3
|
+
import type { applyConstraint, Default, distill, inferIntersection, inferMorphOut, inferPredicate, InferredOptional, Optional, Out } from "../keywords/inference.ts";
|
|
4
|
+
import type { type } from "../keywords/keywords.ts";
|
|
5
|
+
import type { InfixOperator, PostfixExpression } from "./ast/infer.ts";
|
|
6
6
|
import type { inferDefinition, validateDefinition } from "./definition.ts";
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const parseTupleLiteral: (def: array, ctx: ParseContext) => BaseRoot;
|
|
7
|
+
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
|
|
8
|
+
import type { BaseCompletions } from "./string.ts";
|
|
9
|
+
export declare const parseTuple: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
10
|
+
export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
12
11
|
type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]];
|
|
13
12
|
export type validateTuple<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends PostfixExpression ? validatePostfixExpression<def, $, args> : def extends InfixExpression ? validateInfixExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
|
|
14
13
|
def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
|
|
15
|
-
def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."
|
|
14
|
+
def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
|
|
16
15
|
] : validateTupleLiteral<def, $, args>;
|
|
17
16
|
export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
|
|
18
17
|
type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
|
|
@@ -20,55 +19,75 @@ type SequenceParseState = {
|
|
|
20
19
|
unscanned: array;
|
|
21
20
|
inferred: array;
|
|
22
21
|
validated: array;
|
|
22
|
+
includesOptional: boolean;
|
|
23
23
|
};
|
|
24
24
|
type parseSequence<def extends array, $, args> = parseNextElement<{
|
|
25
25
|
unscanned: def;
|
|
26
26
|
inferred: [];
|
|
27
27
|
validated: [];
|
|
28
|
+
includesOptional: false;
|
|
28
29
|
}, $, args>;
|
|
29
30
|
type PreparsedElement = {
|
|
30
31
|
head: unknown;
|
|
31
32
|
tail: array;
|
|
33
|
+
inferred: unknown;
|
|
32
34
|
optional: boolean;
|
|
33
35
|
spread: boolean;
|
|
34
36
|
};
|
|
35
37
|
declare namespace PreparsedElement {
|
|
36
38
|
type from<result extends PreparsedElement> = result;
|
|
37
39
|
}
|
|
38
|
-
type preparseNextElement<s extends SequenceParseState> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ?
|
|
40
|
+
type preparseNextElement<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
|
|
41
|
+
t
|
|
42
|
+
] extends [anyOrNever] ? PreparsedElement.from<{
|
|
39
43
|
head: head;
|
|
40
|
-
tail:
|
|
44
|
+
tail: tail;
|
|
45
|
+
inferred: t;
|
|
46
|
+
optional: false;
|
|
47
|
+
spread: true;
|
|
48
|
+
}> : [t] extends [InferredOptional<infer base>] ? PreparsedElement.from<{
|
|
49
|
+
head: head;
|
|
50
|
+
tail: tail;
|
|
51
|
+
inferred: base;
|
|
41
52
|
optional: true;
|
|
42
53
|
spread: true;
|
|
43
54
|
}> : PreparsedElement.from<{
|
|
44
55
|
head: head;
|
|
45
56
|
tail: tail;
|
|
57
|
+
inferred: t;
|
|
46
58
|
optional: false;
|
|
47
59
|
spread: true;
|
|
48
|
-
}> : s["unscanned"] extends readonly [infer head,
|
|
60
|
+
}> : never : s["unscanned"] extends readonly [infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
|
|
61
|
+
t
|
|
62
|
+
] extends [anyOrNever] ? PreparsedElement.from<{
|
|
49
63
|
head: head;
|
|
50
64
|
tail: tail;
|
|
65
|
+
inferred: t;
|
|
66
|
+
optional: false;
|
|
67
|
+
spread: false;
|
|
68
|
+
}> : [t] extends [InferredOptional<infer base>] ? PreparsedElement.from<{
|
|
69
|
+
head: head;
|
|
70
|
+
tail: tail;
|
|
71
|
+
inferred: base;
|
|
51
72
|
optional: true;
|
|
52
73
|
spread: false;
|
|
53
|
-
}> :
|
|
74
|
+
}> : PreparsedElement.from<{
|
|
54
75
|
head: head;
|
|
55
76
|
tail: tail;
|
|
77
|
+
inferred: t;
|
|
56
78
|
optional: false;
|
|
57
79
|
spread: false;
|
|
58
|
-
}> : null;
|
|
59
|
-
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
80
|
+
}> : never : null;
|
|
81
|
+
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
60
82
|
unscanned: next["tail"];
|
|
61
83
|
inferred: next["spread"] extends true ? [
|
|
62
84
|
...s["inferred"],
|
|
63
|
-
...conform<
|
|
64
|
-
] : next["optional"] extends true ? [
|
|
65
|
-
...s["inferred"],
|
|
66
|
-
inferDefinition<next["head"], $, args>?
|
|
67
|
-
] : [...s["inferred"], inferDefinition<next["head"], $, args>];
|
|
85
|
+
...conform<next["inferred"], array>
|
|
86
|
+
] : next["optional"] extends true ? [...s["inferred"], next["inferred"]?] : [...s["inferred"], next["inferred"]];
|
|
68
87
|
validated: [
|
|
69
88
|
...s["validated"],
|
|
70
89
|
...(next["spread"] extends true ? [
|
|
71
|
-
|
|
90
|
+
next["inferred"] extends infer spreadOperand extends array ? [
|
|
72
91
|
number,
|
|
73
92
|
number
|
|
74
93
|
] extends ([
|
|
@@ -76,17 +95,9 @@ type parseNextElement<s extends SequenceParseState, $, args> = preparseNextEleme
|
|
|
76
95
|
spreadOperand["length"]
|
|
77
96
|
]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
|
|
78
97
|
] : []),
|
|
79
|
-
[
|
|
80
|
-
next["optional"] | next["spread"],
|
|
81
|
-
"?"
|
|
82
|
-
] extends ([
|
|
83
|
-
false,
|
|
84
|
-
s["validated"][number]
|
|
85
|
-
]) ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>,
|
|
86
|
-
...(next["optional"] extends true ? [
|
|
87
|
-
next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : "?"
|
|
88
|
-
] : [])
|
|
98
|
+
next["optional"] extends true ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : validateDefinition<next["head"], $, args> : [s["includesOptional"], next["spread"]] extends [true, false] ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>
|
|
89
99
|
];
|
|
100
|
+
includesOptional: s["includesOptional"] | next["optional"] extends (false) ? false : true;
|
|
90
101
|
}, $, args> : s;
|
|
91
102
|
export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
|
|
92
103
|
type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
|
|
@@ -99,13 +110,13 @@ type optionalPostVariadicMessage = typeof optionalPostVariadicMessage;
|
|
|
99
110
|
export declare const spreadOptionalMessage = "A spread element cannot be optional";
|
|
100
111
|
type spreadOptionalMessage = typeof optionalPostVariadicMessage;
|
|
101
112
|
export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
|
|
102
|
-
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
|
|
113
|
+
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def[1] extends "=" ? applyConstraint<inferDefinition<def[0], $, args>, Default<def[2]>> : def[1] extends "?" ? applyConstraint<inferDefinition<def[0], $, args>, Optional> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
|
|
103
114
|
export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
|
|
104
|
-
export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>;
|
|
115
|
+
export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]" | "?"]>;
|
|
105
116
|
export type validateInfixExpression<def extends InfixExpression, $, args> = def["length"] extends 2 ? readonly [def[0], writeMissingRightOperandMessage<def[1]>] : readonly [
|
|
106
117
|
validateDefinition<def[0], $, args>,
|
|
107
118
|
def[1],
|
|
108
|
-
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<
|
|
119
|
+
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "@" ? MetaSchema : def[1] extends "=" ? type.infer.Out<def[0], $, args> : validateDefinition<def[2], $, args>
|
|
109
120
|
];
|
|
110
121
|
export type UnparsedTupleExpressionInput = {
|
|
111
122
|
instanceof: Constructor;
|
|
@@ -114,18 +125,18 @@ export type UnparsedTupleExpressionInput = {
|
|
|
114
125
|
export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
|
|
115
126
|
export declare const parseKeyOfTuple: PrefixParser<"keyof">;
|
|
116
127
|
export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
|
|
117
|
-
export type
|
|
118
|
-
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx:
|
|
128
|
+
export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
129
|
+
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
119
130
|
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
120
131
|
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
121
132
|
export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
122
|
-
export type TuplePostfixOperator = "[]";
|
|
123
|
-
export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@";
|
|
133
|
+
export type TuplePostfixOperator = "[]" | "?";
|
|
134
|
+
export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@" | "=";
|
|
124
135
|
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
125
|
-
export declare const parseMorphTuple:
|
|
136
|
+
export declare const parseMorphTuple: IndexOneParser<"=>">;
|
|
126
137
|
export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
|
|
127
|
-
export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In:
|
|
128
|
-
export declare const parseNarrowTuple:
|
|
138
|
+
export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.brandable.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
|
|
139
|
+
export declare const parseNarrowTuple: IndexOneParser<":">;
|
|
129
140
|
export type IndexZeroOperator = "keyof" | "instanceof" | "===";
|
|
130
141
|
export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
|
|
131
142
|
export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
|
package/out/parser/tuple.js
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
import { $ark, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
2
|
-
import { append, objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
|
-
import { writeMissingRightOperandMessage } from "./
|
|
1
|
+
import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
2
|
+
import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
|
+
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
|
|
4
4
|
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
5
5
|
export const parseTupleLiteral = (def, ctx) => {
|
|
6
6
|
let sequences = [{}];
|
|
7
7
|
let i = 0;
|
|
8
8
|
while (i < def.length) {
|
|
9
9
|
let spread = false;
|
|
10
|
-
let optional = false;
|
|
11
10
|
if (def[i] === "..." && i < def.length - 1) {
|
|
12
11
|
spread = true;
|
|
13
12
|
i++;
|
|
14
13
|
}
|
|
15
|
-
const element = ctx.$.
|
|
14
|
+
const element = ctx.$.parseOwnDefinitionFormat(def[i], ctx);
|
|
16
15
|
i++;
|
|
17
|
-
if (def[i] === "?") {
|
|
18
|
-
if (spread)
|
|
19
|
-
return throwParseError(spreadOptionalMessage);
|
|
20
|
-
optional = true;
|
|
21
|
-
i++;
|
|
22
|
-
}
|
|
23
16
|
if (spread) {
|
|
24
17
|
if (!element.extends($ark.intrinsic.Array))
|
|
25
18
|
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
@@ -31,13 +24,18 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
31
24
|
element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
32
25
|
}
|
|
33
26
|
else {
|
|
34
|
-
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
27
|
+
sequences = sequences.map(base => appendElement(base, element.meta.optional ? "optional" : "required", element));
|
|
35
28
|
}
|
|
36
29
|
}
|
|
37
|
-
return ctx.$.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
|
|
31
|
+
{
|
|
32
|
+
proto: Array,
|
|
33
|
+
exactLength: 0
|
|
34
|
+
}
|
|
35
|
+
: {
|
|
36
|
+
proto: Array,
|
|
37
|
+
sequence
|
|
38
|
+
}));
|
|
41
39
|
};
|
|
42
40
|
const appendElement = (base, kind, element) => {
|
|
43
41
|
switch (kind) {
|
|
@@ -86,11 +84,11 @@ const appendSpreadBranch = (base, branch) => {
|
|
|
86
84
|
// the only array with no sequence reference is unknown[]
|
|
87
85
|
return appendElement(base, "variadic", $ark.intrinsic.unknown);
|
|
88
86
|
}
|
|
89
|
-
spread.prefix
|
|
90
|
-
spread.optionals
|
|
87
|
+
spread.prefix?.forEach(node => appendElement(base, "required", node));
|
|
88
|
+
spread.optionals?.forEach(node => appendElement(base, "optional", node));
|
|
91
89
|
if (spread.variadic)
|
|
92
90
|
appendElement(base, "variadic", spread.variadic);
|
|
93
|
-
spread.postfix
|
|
91
|
+
spread.postfix?.forEach(node => appendElement(base, "required", node));
|
|
94
92
|
return base;
|
|
95
93
|
};
|
|
96
94
|
const maybeParseTupleExpression = (def, ctx) => {
|
|
@@ -104,38 +102,46 @@ export const multipleVariadicMesage = "A tuple may have at most one variadic ele
|
|
|
104
102
|
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
105
103
|
export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
106
104
|
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
107
|
-
export const parseKeyOfTuple = (def, ctx) => ctx.$.
|
|
105
|
+
export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
|
|
108
106
|
const parseBranchTuple = (def, ctx) => {
|
|
109
107
|
if (def[2] === undefined)
|
|
110
108
|
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
111
|
-
const l = ctx.$.
|
|
112
|
-
const r = ctx.$.
|
|
113
|
-
|
|
109
|
+
const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
|
|
110
|
+
const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
|
|
111
|
+
if (def[1] === "|")
|
|
112
|
+
return ctx.$.node("union", { branches: [l, r] });
|
|
113
|
+
const result = intersectNodesRoot(l, r, ctx.$);
|
|
114
|
+
if (result instanceof Disjoint)
|
|
115
|
+
return result.throw();
|
|
116
|
+
return result;
|
|
114
117
|
};
|
|
115
|
-
const parseArrayTuple = (def, ctx) => ctx.$.
|
|
118
|
+
const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
|
|
116
119
|
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
117
120
|
export const parseMorphTuple = (def, ctx) => {
|
|
118
121
|
if (typeof def[2] !== "function") {
|
|
119
122
|
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
120
123
|
}
|
|
121
|
-
|
|
122
|
-
return ctx.$.parse(def[0], ctx).pipe(def[2]);
|
|
124
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
|
|
123
125
|
};
|
|
124
126
|
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
125
127
|
export const parseNarrowTuple = (def, ctx) => {
|
|
126
128
|
if (typeof def[2] !== "function") {
|
|
127
129
|
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
128
130
|
}
|
|
129
|
-
return ctx.$.
|
|
131
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
|
|
130
132
|
};
|
|
131
|
-
const parseAttributeTuple = (def, ctx) => ctx.$.
|
|
133
|
+
const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
|
|
134
|
+
const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
|
|
135
|
+
const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
|
|
132
136
|
const indexOneParsers = {
|
|
137
|
+
"[]": parseArrayTuple,
|
|
138
|
+
"?": parseOptionalTuple,
|
|
133
139
|
"|": parseBranchTuple,
|
|
134
140
|
"&": parseBranchTuple,
|
|
135
|
-
"[]": parseArrayTuple,
|
|
136
141
|
":": parseNarrowTuple,
|
|
137
142
|
"=>": parseMorphTuple,
|
|
138
|
-
"@": parseAttributeTuple
|
|
143
|
+
"@": parseAttributeTuple,
|
|
144
|
+
"=": parseDefaultTuple
|
|
139
145
|
};
|
|
140
146
|
const prefixParsers = {
|
|
141
147
|
keyof: parseKeyOfTuple,
|