arktype 2.0.0-rc.1 → 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 +47 -48
- package/out/generic.js +6 -8
- package/out/index.d.ts +4 -4
- package/out/index.js +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/constructors/Array.d.ts +2 -2
- package/out/keywords/constructors/Array.js +7 -7
- package/out/keywords/constructors/Date.d.ts +32 -0
- package/out/keywords/constructors/FormData.d.ts +2 -2
- package/out/keywords/constructors/FormData.js +8 -8
- package/out/keywords/constructors/TypedArray.js +2 -2
- package/out/keywords/constructors/constructors.d.ts +0 -1
- package/out/keywords/constructors/constructors.js +2 -2
- package/out/keywords/inference.d.ts +227 -0
- package/out/keywords/keywords.d.ts +61 -0
- package/out/keywords/{ark.js → keywords.js} +10 -8
- 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 +32 -6
- 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 +2 -2
- 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 +4 -8
- 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 +2 -2
- package/out/keywords/string/integer.js +7 -7
- package/out/keywords/string/ip.d.ts +2 -2
- package/out/keywords/string/ip.js +5 -5
- package/out/keywords/string/json.d.ts +3 -2
- package/out/keywords/string/json.js +24 -16
- package/out/keywords/string/lower.d.ts +2 -2
- package/out/keywords/string/lower.js +4 -4
- package/out/keywords/string/normalize.d.ts +23 -27
- package/out/keywords/string/normalize.js +14 -14
- package/out/keywords/string/numeric.d.ts +2 -2
- package/out/keywords/string/numeric.js +7 -7
- package/out/keywords/string/semver.d.ts +1 -1
- package/out/keywords/string/string.d.ts +39 -42
- package/out/keywords/string/string.js +3 -3
- package/out/keywords/string/trim.d.ts +2 -2
- package/out/keywords/string/trim.js +4 -4
- package/out/keywords/string/upper.d.ts +2 -2
- package/out/keywords/string/upper.js +4 -4
- package/out/keywords/string/url.d.ts +2 -2
- package/out/keywords/string/url.js +8 -8
- package/out/keywords/string/uuid.d.ts +11 -11
- package/out/keywords/string/uuid.js +3 -3
- package/out/keywords/ts.d.ts +11 -0
- package/out/keywords/ts.js +7 -3
- 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 +3 -3
- package/out/parser/ast/default.d.ts +8 -0
- package/out/parser/{semantic → ast}/infer.d.ts +11 -11
- package/out/parser/{semantic → ast}/utils.d.ts +1 -1
- package/out/parser/{semantic → ast}/validate.d.ts +6 -6
- package/out/parser/definition.d.ts +8 -9
- package/out/parser/objectLiteral.d.ts +11 -37
- 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 +3 -2
- package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +3 -3
- package/out/parser/{string/shift → shift}/operand/unenclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operator/bounds.d.ts +3 -3
- 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}/scanner.d.ts +3 -1
- package/out/parser/{string/shift → shift}/scanner.js +5 -3
- package/out/parser/{string/string.d.ts → string.d.ts} +6 -7
- 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 +32 -39
- package/out/scope.js +43 -35
- package/out/type.d.ts +24 -21
- package/out/type.js +14 -10
- 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 -39
- package/out/keywords/ast.d.ts +0 -193
- package/out/parser/semantic/default.d.ts +0 -9
- package/out/parser/semantic/default.js +0 -1
- package/out/subtypes/base.d.ts +0 -85
- 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
- /package/out/parser/{string/shift → shift}/operator/operator.js +0 -0
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { BaseRoot } from "@ark/schema";
|
|
1
|
+
import type { BaseParseContext, BaseRoot } from "@ark/schema";
|
|
2
2
|
import { type requireKeys } from "@ark/util";
|
|
3
|
-
import type { LimitLiteral } from "
|
|
4
|
-
import type {
|
|
5
|
-
import type { InfixOperator } from "../../semantic/infer.ts";
|
|
3
|
+
import type { LimitLiteral } from "../../keywords/inference.ts";
|
|
4
|
+
import type { InfixOperator } from "../ast/infer.ts";
|
|
6
5
|
import type { Scanner } from "../shift/scanner.ts";
|
|
7
6
|
import { type Comparator, type MinComparator, type OpenLeftBound, type StringifiablePrefixOperator } from "./shared.ts";
|
|
8
7
|
type BranchState = {
|
|
@@ -18,9 +17,8 @@ export declare class DynamicState {
|
|
|
18
17
|
finalizer: Scanner.FinalizingLookahead | undefined;
|
|
19
18
|
groups: BranchState[];
|
|
20
19
|
scanner: Scanner;
|
|
21
|
-
ctx:
|
|
22
|
-
|
|
23
|
-
constructor(scanner: Scanner, ctx: ParseContext, defaultable: boolean);
|
|
20
|
+
ctx: BaseParseContext;
|
|
21
|
+
constructor(scanner: Scanner, ctx: BaseParseContext);
|
|
24
22
|
error(message: string): never;
|
|
25
23
|
hasRoot(): this is DynamicStateWithRoot;
|
|
26
24
|
setRoot(root: BaseRoot): void;
|
|
@@ -16,11 +16,9 @@ export class DynamicState {
|
|
|
16
16
|
groups = [];
|
|
17
17
|
scanner;
|
|
18
18
|
ctx;
|
|
19
|
-
|
|
20
|
-
constructor(scanner, ctx, defaultable) {
|
|
19
|
+
constructor(scanner, ctx) {
|
|
21
20
|
this.scanner = scanner;
|
|
22
21
|
this.ctx = ctx;
|
|
23
|
-
this.defaultable = defaultable;
|
|
24
22
|
}
|
|
25
23
|
error(message) {
|
|
26
24
|
return throwParseError(message);
|
|
@@ -93,17 +91,18 @@ export class DynamicState {
|
|
|
93
91
|
this.assertRangeUnset();
|
|
94
92
|
this.applyPrefixes();
|
|
95
93
|
const root = this.root;
|
|
96
|
-
this.branches.intersection =
|
|
94
|
+
this.branches.intersection =
|
|
95
|
+
this.branches.intersection?.rawAnd(root) ?? root;
|
|
97
96
|
if (token === "|") {
|
|
98
97
|
this.branches.union =
|
|
99
|
-
this.branches.union?.
|
|
98
|
+
this.branches.union?.rawOr(this.branches.intersection) ??
|
|
100
99
|
this.branches.intersection;
|
|
101
100
|
this.branches.intersection = null;
|
|
102
101
|
}
|
|
103
102
|
this.root = undefined;
|
|
104
103
|
}
|
|
105
104
|
parseUntilFinalizer() {
|
|
106
|
-
return parseUntilFinalizer(new DynamicState(this.scanner, this.ctx
|
|
105
|
+
return parseUntilFinalizer(new DynamicState(this.scanner, this.ctx));
|
|
107
106
|
}
|
|
108
107
|
parseOperator() {
|
|
109
108
|
return parseOperator(this);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Completion, ErrorMessage, defined } from "@ark/util";
|
|
2
|
-
import type { LimitLiteral } from "
|
|
2
|
+
import type { LimitLiteral } from "../../keywords/inference.ts";
|
|
3
3
|
import type { Scanner } from "../shift/scanner.ts";
|
|
4
4
|
import type { Comparator, InvertedComparators, MaxComparator, MinComparator, OpenLeftBound, StringifiablePrefixOperator, writeMultipleLeftBoundsMessage, writeOpenRangeMessage, writeUnclosedGroupMessage, writeUnmatchedGroupCloseMessage, writeUnpairableComparatorMessage } from "./shared.ts";
|
|
5
5
|
export type StaticState = {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { DateLiteral } from "
|
|
1
|
+
import type { DateLiteral } from "../../../keywords/inference.ts";
|
|
2
2
|
export declare const isDateLiteral: (value: unknown) => value is DateLiteral;
|
|
3
3
|
export declare const isValidDate: (d: Date) => boolean;
|
|
4
4
|
export declare const extractDateLiteralSource: <literal extends DateLiteral>(literal: literal) => extractDateLiteralSource<literal>;
|
|
5
|
+
type extractDateLiteralSource<literal extends DateLiteral> = literal extends DateLiteral<infer source> ? source : never;
|
|
5
6
|
export declare const writeInvalidDateMessage: <source extends string>(source: source) => writeInvalidDateMessage<source>;
|
|
6
7
|
export type writeInvalidDateMessage<source extends string> = `'${source}' could not be parsed by the Date constructor`;
|
|
7
8
|
export type DateInput = ConstructorParameters<typeof Date>[0];
|
|
8
9
|
export type DateParseResult<errorOnFail extends boolean | string = boolean | string> = Date | (errorOnFail extends true | string ? never : undefined);
|
|
9
10
|
export declare const tryParseDate: <errorOnFail extends boolean | string>(source: string, errorOnFail?: errorOnFail) => DateParseResult<errorOnFail>;
|
|
11
|
+
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { Date
|
|
2
|
-
import type {
|
|
1
|
+
import type { Date } from "../../../keywords/constructors/Date.ts";
|
|
2
|
+
import type { string } from "../../../keywords/inference.ts";
|
|
3
|
+
import type { InferredAst } from "../../ast/infer.ts";
|
|
3
4
|
import type { DynamicState } from "../../reduce/dynamic.ts";
|
|
4
5
|
import type { StaticState, state } from "../../reduce/static.ts";
|
|
5
6
|
import type { Scanner } from "../scanner.ts";
|
|
@@ -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,8 +1,8 @@
|
|
|
1
1
|
import { writeUnresolvableMessage, type BaseRoot, type GenericAst, type GenericRoot, type arkKind, type genericParamNames, type resolvableReferenceIn, type writeNonSubmoduleDotMessage } from "@ark/schema";
|
|
2
2
|
import { type BigintLiteral, type NumberLiteral, type join, type lastOf } from "@ark/util";
|
|
3
|
-
import type { ArkAmbient } from "
|
|
4
|
-
import type { resolutionToAst } from "
|
|
5
|
-
import type { GenericInstantiationAst, InferredAst } from "
|
|
3
|
+
import type { ArkAmbient } from "../../../config.ts";
|
|
4
|
+
import type { resolutionToAst } from "../../../scope.ts";
|
|
5
|
+
import type { GenericInstantiationAst, InferredAst } from "../../ast/infer.ts";
|
|
6
6
|
import type { DynamicState } from "../../reduce/dynamic.ts";
|
|
7
7
|
import type { StaticState, state } from "../../reduce/static.ts";
|
|
8
8
|
import type { BaseCompletions } from "../../string.ts";
|
|
@@ -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,8 +1,8 @@
|
|
|
1
1
|
import { type BaseRoot, type BoundKind } from "@ark/schema";
|
|
2
2
|
import { type keySet } from "@ark/util";
|
|
3
|
-
import type { DateLiteral } from "
|
|
4
|
-
import type { InferredAst } from "
|
|
5
|
-
import type { astToString } from "
|
|
3
|
+
import type { DateLiteral } from "../../../keywords/inference.ts";
|
|
4
|
+
import type { InferredAst } from "../../ast/infer.ts";
|
|
5
|
+
import type { astToString } from "../../ast/utils.ts";
|
|
6
6
|
import type { DynamicState, DynamicStateWithRoot } from "../../reduce/dynamic.ts";
|
|
7
7
|
import { writeUnpairableComparatorMessage, type Comparator, type InvertedComparators, type MaxComparator } from "../../reduce/shared.ts";
|
|
8
8
|
import type { state, StaticState } from "../../reduce/static.ts";
|
|
@@ -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' }`;
|
|
@@ -34,19 +34,21 @@ export declare class Scanner<lookahead extends string = string> {
|
|
|
34
34
|
readonly "%": true;
|
|
35
35
|
readonly ",": true;
|
|
36
36
|
readonly ":": true;
|
|
37
|
+
readonly "?": true;
|
|
37
38
|
};
|
|
38
39
|
static finalizingLookaheads: {
|
|
39
40
|
readonly ">": true;
|
|
40
41
|
readonly ",": true;
|
|
41
42
|
readonly "": true;
|
|
42
43
|
readonly "=": true;
|
|
44
|
+
readonly "?": true;
|
|
43
45
|
};
|
|
44
46
|
static lookaheadIsTerminator: Scanner.UntilCondition;
|
|
45
47
|
static lookaheadIsNotWhitespace: Scanner.UntilCondition;
|
|
46
48
|
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=";
|
|
47
49
|
}
|
|
48
50
|
export declare namespace Scanner {
|
|
49
|
-
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;
|
|
50
52
|
type UntilCondition = (scanner: Scanner, shifted: string) => boolean;
|
|
51
53
|
type OnInputEndFn = (scanner: Scanner, shifted: string) => string;
|
|
52
54
|
type ShiftUntilOptions = {
|
|
@@ -74,13 +74,15 @@ export class Scanner {
|
|
|
74
74
|
"%": true,
|
|
75
75
|
",": true,
|
|
76
76
|
":": true,
|
|
77
|
+
"?": true,
|
|
77
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,13 +1,13 @@
|
|
|
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 { resolutionToAst } from "
|
|
5
|
-
import type { inferAstRoot } from "
|
|
3
|
+
import type { ArkAmbient } from "../config.ts";
|
|
4
|
+
import type { resolutionToAst } from "../scope.ts";
|
|
5
|
+
import type { inferAstRoot } from "./ast/infer.ts";
|
|
6
6
|
import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.ts";
|
|
7
7
|
import type { StringifiablePrefixOperator } from "./reduce/shared.ts";
|
|
8
8
|
import type { state, StaticState } from "./reduce/static.ts";
|
|
9
9
|
import type { parseOperand } from "./shift/operand/operand.ts";
|
|
10
|
-
import { parseDefault
|
|
10
|
+
import { parseDefault } from "./shift/operator/default.ts";
|
|
11
11
|
import { writeUnexpectedCharacterMessage, type parseOperator } from "./shift/operator/operator.ts";
|
|
12
12
|
/**
|
|
13
13
|
* Try to parse the definition from right to left using the most common syntax.
|
|
@@ -19,12 +19,11 @@ export type parseString<def extends string, $, args> = def extends keyof $ ? res
|
|
|
19
19
|
] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
|
|
20
20
|
export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
|
|
21
21
|
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<ArkAmbient.$> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
|
|
22
|
-
export
|
|
23
|
-
export declare const fullStringParse: (s: DynamicState) => StringParseResult;
|
|
22
|
+
export declare const fullStringParse: (s: DynamicState) => BaseRoot;
|
|
24
23
|
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
|
|
25
24
|
export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithRoot;
|
|
26
25
|
export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
|
|
27
26
|
declare const next: (s: DynamicState) => void;
|
|
28
27
|
type next<s extends StaticState, $, args> = s["root"] extends undefined ? parseOperand<s, $, args> : parseOperator<s, $, args>;
|
|
29
|
-
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"]}`>>;
|
|
30
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,
|