arktype 2.0.0-rc.9 → 2.0.1
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/attributes.d.ts +94 -0
- package/out/attributes.js +3 -0
- package/out/generic.d.ts +13 -13
- package/out/index.d.ts +3 -1
- package/out/index.js +2 -1
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
- package/out/keywords/{constructors/Array.js → Array.js} +2 -2
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
- package/out/keywords/{constructors/FormData.js → FormData.js} +2 -2
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
- package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
- package/out/keywords/keywords.d.ts +14 -13
- package/out/keywords/keywords.js +9 -5
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +22 -1
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/keywords/ts.d.ts +31 -4
- package/out/keywords/ts.js +21 -8
- package/out/methods/array.d.ts +6 -7
- package/out/methods/base.d.ts +461 -35
- package/out/methods/date.d.ts +5 -6
- package/out/methods/instantiate.d.ts +4 -4
- package/out/methods/morph.d.ts +6 -6
- package/out/methods/number.d.ts +8 -9
- package/out/methods/object.d.ts +39 -15
- package/out/methods/string.d.ts +9 -10
- package/out/parser/ast/bounds.d.ts +1 -1
- package/out/parser/ast/default.d.ts +2 -2
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +8 -22
- package/out/parser/ast/validate.d.ts +13 -9
- package/out/parser/ast/validate.js +3 -1
- package/out/parser/definition.d.ts +17 -8
- package/out/parser/definition.js +38 -3
- package/out/parser/objectLiteral.d.ts +39 -39
- package/out/parser/objectLiteral.js +83 -67
- package/out/parser/property.d.ts +36 -0
- package/out/parser/property.js +16 -0
- package/out/parser/reduce/dynamic.d.ts +8 -9
- package/out/parser/reduce/shared.d.ts +1 -1
- package/out/parser/reduce/static.d.ts +4 -4
- package/out/parser/shift/operand/date.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/shift/operand/operand.d.ts +3 -3
- package/out/parser/shift/operand/operand.js +2 -2
- package/out/parser/shift/operand/unenclosed.d.ts +13 -5
- package/out/parser/shift/operand/unenclosed.js +5 -4
- package/out/parser/shift/operator/bounds.d.ts +5 -5
- package/out/parser/shift/operator/bounds.js +5 -4
- package/out/parser/shift/operator/brand.d.ts +6 -0
- package/out/parser/shift/operator/brand.js +5 -0
- package/out/parser/shift/operator/default.d.ts +3 -2
- package/out/parser/shift/operator/default.js +4 -1
- package/out/parser/shift/operator/divisor.d.ts +2 -2
- package/out/parser/shift/operator/operator.d.ts +4 -3
- package/out/parser/shift/operator/operator.js +7 -5
- package/out/parser/shift/scanner.d.ts +31 -55
- package/out/parser/shift/scanner.js +24 -83
- package/out/parser/string.d.ts +5 -4
- package/out/parser/string.js +18 -1
- package/out/parser/tupleExpressions.d.ts +48 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +95 -0
- package/out/parser/tupleLiteral.js +122 -0
- package/out/scope.d.ts +17 -12
- package/out/scope.js +25 -41
- package/out/type.d.ts +63 -5
- package/out/type.js +3 -2
- package/package.json +8 -8
- package/out/keywords/constructors/Date.d.ts +0 -31
- package/out/keywords/inference.d.ts +0 -220
- package/out/keywords/inference.js +0 -2
- package/out/keywords/number/epoch.d.ts +0 -9
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -53
- package/out/keywords/number/number.js +0 -21
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -55
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -16
- package/out/keywords/string/json.js +0 -53
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -88
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -108
- package/out/keywords/string/string.js +0 -41
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/keywords/utils.d.ts +0 -1
- package/out/keywords/utils.js +0 -2
- package/out/methods/validator.d.ts +0 -11
- package/out/methods/validator.js +0 -1
- package/out/parser/tuple.d.ts +0 -143
- package/out/parser/tuple.js +0 -164
- /package/out/{keywords/constructors/Date.js → parser/ast/generic.js} +0 -0
|
@@ -1,69 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Scanner, type EscapeChar, type WhitespaceChar } from "@ark/util";
|
|
2
2
|
import type { Comparator } from "../reduce/shared.ts";
|
|
3
|
-
export declare class
|
|
4
|
-
private chars;
|
|
5
|
-
private i;
|
|
6
|
-
constructor(def: string);
|
|
7
|
-
/** Get lookahead and advance scanner by one */
|
|
8
|
-
shift(): lookahead;
|
|
9
|
-
get lookahead(): lookahead;
|
|
10
|
-
get nextLookahead(): string;
|
|
11
|
-
get length(): number;
|
|
12
|
-
shiftUntil(condition: Scanner.UntilCondition): string;
|
|
3
|
+
export declare class ArkTypeScanner<lookahead extends string = string> extends Scanner<lookahead> {
|
|
13
4
|
shiftUntilNextTerminator(): string;
|
|
14
|
-
shiftUntilNonWhitespace(): string;
|
|
15
|
-
jumpToIndex(i: number): void;
|
|
16
|
-
jumpForward(count: number): void;
|
|
17
|
-
get location(): number;
|
|
18
|
-
get unscanned(): string;
|
|
19
|
-
get scanned(): string;
|
|
20
|
-
sliceChars(start: number, end?: number): string;
|
|
21
|
-
lookaheadIs<char extends lookahead>(char: char): this is Scanner<char>;
|
|
22
|
-
lookaheadIsIn<tokens extends Dict>(tokens: tokens): this is Scanner<Extract<keyof tokens, string>>;
|
|
23
5
|
static terminatingChars: {
|
|
24
|
-
readonly " ":
|
|
25
|
-
readonly "\n":
|
|
26
|
-
readonly "\t":
|
|
27
|
-
readonly "<":
|
|
28
|
-
readonly ">":
|
|
29
|
-
readonly "=":
|
|
30
|
-
readonly "|":
|
|
31
|
-
readonly "&":
|
|
32
|
-
readonly ")":
|
|
33
|
-
readonly "[":
|
|
34
|
-
readonly "%":
|
|
35
|
-
readonly ",":
|
|
36
|
-
readonly ":":
|
|
37
|
-
readonly "?":
|
|
6
|
+
readonly " ": 1;
|
|
7
|
+
readonly "\n": 1;
|
|
8
|
+
readonly "\t": 1;
|
|
9
|
+
readonly "<": 1;
|
|
10
|
+
readonly ">": 1;
|
|
11
|
+
readonly "=": 1;
|
|
12
|
+
readonly "|": 1;
|
|
13
|
+
readonly "&": 1;
|
|
14
|
+
readonly ")": 1;
|
|
15
|
+
readonly "[": 1;
|
|
16
|
+
readonly "%": 1;
|
|
17
|
+
readonly ",": 1;
|
|
18
|
+
readonly ":": 1;
|
|
19
|
+
readonly "?": 1;
|
|
20
|
+
readonly "#": 1;
|
|
38
21
|
};
|
|
39
22
|
static finalizingLookaheads: {
|
|
40
|
-
readonly ">":
|
|
41
|
-
readonly ",":
|
|
42
|
-
readonly "":
|
|
43
|
-
readonly "=":
|
|
44
|
-
readonly "?":
|
|
23
|
+
readonly ">": 1;
|
|
24
|
+
readonly ",": 1;
|
|
25
|
+
readonly "": 1;
|
|
26
|
+
readonly "=": 1;
|
|
27
|
+
readonly "?": 1;
|
|
45
28
|
};
|
|
46
|
-
static
|
|
47
|
-
static lookaheadIsNotWhitespace: Scanner.UntilCondition;
|
|
48
|
-
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=";
|
|
29
|
+
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=" | "?";
|
|
49
30
|
}
|
|
50
|
-
export declare namespace
|
|
51
|
-
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false :
|
|
52
|
-
type
|
|
53
|
-
type
|
|
54
|
-
type
|
|
55
|
-
|
|
56
|
-
};
|
|
57
|
-
type TerminatingChar = keyof typeof Scanner.terminatingChars;
|
|
58
|
-
type FinalizingLookahead = keyof typeof Scanner.finalizingLookaheads;
|
|
59
|
-
type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>";
|
|
60
|
-
type PostfixToken = "[]";
|
|
31
|
+
export declare namespace ArkTypeScanner {
|
|
32
|
+
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : ArkTypeScanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," | "?" ? true : false;
|
|
33
|
+
type TerminatingChar = keyof typeof ArkTypeScanner.terminatingChars;
|
|
34
|
+
type FinalizingLookahead = keyof typeof ArkTypeScanner.finalizingLookaheads;
|
|
35
|
+
type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>" | "#" | "@" | "=";
|
|
36
|
+
type PostfixToken = "[]" | "?";
|
|
61
37
|
type OperatorToken = InfixToken | PostfixToken;
|
|
62
38
|
type shift<lookahead extends string, unscanned extends string> = `${lookahead}${unscanned}`;
|
|
63
|
-
type shiftUntil<unscanned extends string, terminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends terminator ? scanned extends `${infer base}${
|
|
39
|
+
type shiftUntil<unscanned extends string, terminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends terminator ? scanned extends `${infer base}${EscapeChar}` ? shiftUntil<nextUnscanned, terminator, `${base}${lookahead}`> : [scanned, unscanned] : shiftUntil<nextUnscanned, terminator, `${scanned}${lookahead}`> : [scanned, ""];
|
|
64
40
|
type shiftUntilNot<unscanned extends string, nonTerminator extends string, scanned extends string = ""> = unscanned extends shift<infer lookahead, infer nextUnscanned> ? lookahead extends nonTerminator ? shiftUntilNot<nextUnscanned, nonTerminator, `${scanned}${lookahead}`> : [scanned, unscanned] : [scanned, ""];
|
|
65
41
|
type shiftUntilNextTerminator<unscanned extends string> = shiftUntil<unscanned, TerminatingChar>;
|
|
66
|
-
type skipWhitespace<unscanned extends string> = shiftUntilNot<unscanned,
|
|
42
|
+
type skipWhitespace<unscanned extends string> = shiftUntilNot<unscanned, WhitespaceChar>[1];
|
|
67
43
|
type shiftResult<scanned extends string, unscanned extends string> = [
|
|
68
44
|
scanned,
|
|
69
45
|
unscanned
|
|
@@ -1,99 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export class Scanner {
|
|
3
|
-
chars;
|
|
4
|
-
i;
|
|
5
|
-
constructor(def) {
|
|
6
|
-
this.chars = [...def];
|
|
7
|
-
this.i = 0;
|
|
8
|
-
}
|
|
9
|
-
/** Get lookahead and advance scanner by one */
|
|
10
|
-
shift() {
|
|
11
|
-
return (this.chars[this.i++] ?? "");
|
|
12
|
-
}
|
|
13
|
-
get lookahead() {
|
|
14
|
-
return (this.chars[this.i] ?? "");
|
|
15
|
-
}
|
|
16
|
-
get nextLookahead() {
|
|
17
|
-
return this.chars[this.i + 1] ?? "";
|
|
18
|
-
}
|
|
19
|
-
get length() {
|
|
20
|
-
return this.chars.length;
|
|
21
|
-
}
|
|
22
|
-
shiftUntil(condition) {
|
|
23
|
-
let shifted = "";
|
|
24
|
-
while (this.lookahead) {
|
|
25
|
-
if (condition(this, shifted)) {
|
|
26
|
-
if (shifted[shifted.length - 1] === escapeToken)
|
|
27
|
-
shifted = shifted.slice(0, -1);
|
|
28
|
-
else
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
shifted += this.shift();
|
|
32
|
-
}
|
|
33
|
-
return shifted;
|
|
34
|
-
}
|
|
1
|
+
import { isKeyOf, Scanner, whitespaceChars } from "@ark/util";
|
|
2
|
+
export class ArkTypeScanner extends Scanner {
|
|
35
3
|
shiftUntilNextTerminator() {
|
|
36
4
|
this.shiftUntilNonWhitespace();
|
|
37
|
-
return this.shiftUntil(
|
|
38
|
-
}
|
|
39
|
-
shiftUntilNonWhitespace() {
|
|
40
|
-
return this.shiftUntil(Scanner.lookaheadIsNotWhitespace);
|
|
41
|
-
}
|
|
42
|
-
jumpToIndex(i) {
|
|
43
|
-
this.i = i < 0 ? this.length + i : i;
|
|
44
|
-
}
|
|
45
|
-
jumpForward(count) {
|
|
46
|
-
this.i += count;
|
|
47
|
-
}
|
|
48
|
-
get location() {
|
|
49
|
-
return this.i;
|
|
50
|
-
}
|
|
51
|
-
get unscanned() {
|
|
52
|
-
return this.chars.slice(this.i, this.length).join("");
|
|
53
|
-
}
|
|
54
|
-
get scanned() {
|
|
55
|
-
return this.chars.slice(0, this.i).join("");
|
|
56
|
-
}
|
|
57
|
-
sliceChars(start, end) {
|
|
58
|
-
return this.chars.slice(start, end).join("");
|
|
59
|
-
}
|
|
60
|
-
lookaheadIs(char) {
|
|
61
|
-
return this.lookahead === char;
|
|
62
|
-
}
|
|
63
|
-
lookaheadIsIn(tokens) {
|
|
64
|
-
return this.lookahead in tokens;
|
|
5
|
+
return this.shiftUntil(() => this.lookahead in ArkTypeScanner.terminatingChars);
|
|
65
6
|
}
|
|
66
7
|
static terminatingChars = {
|
|
67
|
-
"<":
|
|
68
|
-
">":
|
|
69
|
-
"=":
|
|
70
|
-
"|":
|
|
71
|
-
"&":
|
|
72
|
-
")":
|
|
73
|
-
"[":
|
|
74
|
-
"%":
|
|
75
|
-
",":
|
|
76
|
-
":":
|
|
77
|
-
"?":
|
|
78
|
-
|
|
8
|
+
"<": 1,
|
|
9
|
+
">": 1,
|
|
10
|
+
"=": 1,
|
|
11
|
+
"|": 1,
|
|
12
|
+
"&": 1,
|
|
13
|
+
")": 1,
|
|
14
|
+
"[": 1,
|
|
15
|
+
"%": 1,
|
|
16
|
+
",": 1,
|
|
17
|
+
":": 1,
|
|
18
|
+
"?": 1,
|
|
19
|
+
"#": 1,
|
|
20
|
+
...whitespaceChars
|
|
79
21
|
};
|
|
80
22
|
static finalizingLookaheads = {
|
|
81
|
-
">":
|
|
82
|
-
",":
|
|
83
|
-
"":
|
|
84
|
-
"=":
|
|
85
|
-
"?":
|
|
23
|
+
">": 1,
|
|
24
|
+
",": 1,
|
|
25
|
+
"": 1,
|
|
26
|
+
"=": 1,
|
|
27
|
+
"?": 1
|
|
86
28
|
};
|
|
87
|
-
static lookaheadIsTerminator = (scanner) => scanner.lookahead in this.terminatingChars;
|
|
88
|
-
static lookaheadIsNotWhitespace = (scanner) => !(scanner.lookahead in whiteSpaceTokens);
|
|
89
29
|
static lookaheadIsFinalizing = (lookahead, unscanned) => lookahead === ">" ?
|
|
90
30
|
unscanned[0] === "=" ?
|
|
91
31
|
// >== would only occur in an expression like Array<number>==5
|
|
92
32
|
// otherwise, >= would only occur as part of a bound like number>=5
|
|
93
33
|
unscanned[1] === "="
|
|
94
|
-
// if > is the end of a generic instantiation, the next token will be
|
|
34
|
+
// if > is the end of a generic instantiation, the next token will be
|
|
35
|
+
// an operator or the end of the string
|
|
95
36
|
: unscanned.trimStart() === "" ||
|
|
96
|
-
isKeyOf(unscanned.trimStart()[0],
|
|
37
|
+
isKeyOf(unscanned.trimStart()[0], ArkTypeScanner.terminatingChars)
|
|
97
38
|
// "=" is a finalizer on its own (representing a default value),
|
|
98
39
|
// but not with a second "=" (an equality comparator)
|
|
99
40
|
: lookahead === "=" ? unscanned[0] !== "="
|
package/out/parser/string.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseParseContext, resolvableReferenceIn } from "@ark/schema";
|
|
2
2
|
import { type ErrorMessage } from "@ark/util";
|
|
3
3
|
import type { ArkAmbient } from "../config.ts";
|
|
4
|
-
import type { resolutionToAst } from "../scope.ts";
|
|
4
|
+
import type { InnerParseResult, resolutionToAst } from "../scope.ts";
|
|
5
5
|
import type { inferAstRoot } from "./ast/infer.ts";
|
|
6
|
-
import
|
|
6
|
+
import { DynamicState, type 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
10
|
import { parseDefault } from "./shift/operator/default.ts";
|
|
11
11
|
import { writeUnexpectedCharacterMessage, type parseOperator } from "./shift/operator/operator.ts";
|
|
12
|
+
export declare const parseString: (def: string, ctx: BaseParseContext) => InnerParseResult;
|
|
12
13
|
/**
|
|
13
14
|
* Try to parse the definition from right to left using the most common syntax.
|
|
14
15
|
* This can be much more efficient for simple definitions.
|
|
@@ -19,7 +20,7 @@ export type parseString<def extends string, $, args> = def extends keyof $ ? res
|
|
|
19
20
|
] : fullStringParse<state.initialize<def>, $, args> : fullStringParse<state.initialize<def>, $, args>;
|
|
20
21
|
export type inferString<def extends string, $, args> = inferAstRoot<parseString<def, $, args>, $, args>;
|
|
21
22
|
export type BaseCompletions<$, args, otherSuggestions extends string = never> = resolvableReferenceIn<$> | resolvableReferenceIn<ArkAmbient.$> | (keyof args & string) | StringifiablePrefixOperator | otherSuggestions;
|
|
22
|
-
export declare const fullStringParse: (s: DynamicState) =>
|
|
23
|
+
export declare const fullStringParse: (s: DynamicState) => InnerParseResult;
|
|
23
24
|
type fullStringParse<s extends StaticState, $, args> = extractFinalizedResult<parseUntilFinalizer<s, $, args>>;
|
|
24
25
|
export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithRoot;
|
|
25
26
|
export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
|
package/out/parser/string.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { throwInternalError, throwParseError } from "@ark/util";
|
|
2
|
+
import { DynamicState } from "./reduce/dynamic.js";
|
|
2
3
|
import { parseDefault } from "./shift/operator/default.js";
|
|
3
4
|
import { writeUnexpectedCharacterMessage } from "./shift/operator/operator.js";
|
|
5
|
+
import { ArkTypeScanner } from "./shift/scanner.js";
|
|
6
|
+
export const parseString = (def, ctx) => {
|
|
7
|
+
const aliasResolution = ctx.$.maybeResolveRoot(def);
|
|
8
|
+
if (aliasResolution)
|
|
9
|
+
return aliasResolution;
|
|
10
|
+
if (def.endsWith("[]")) {
|
|
11
|
+
const possibleElementResolution = ctx.$.maybeResolveRoot(def.slice(0, -2));
|
|
12
|
+
if (possibleElementResolution)
|
|
13
|
+
return possibleElementResolution.array();
|
|
14
|
+
}
|
|
15
|
+
const s = new DynamicState(new ArkTypeScanner(def), ctx);
|
|
16
|
+
const node = fullStringParse(s);
|
|
17
|
+
if (s.finalizer === ">")
|
|
18
|
+
throwParseError(writeUnexpectedCharacterMessage(">"));
|
|
19
|
+
return node;
|
|
20
|
+
};
|
|
4
21
|
export const fullStringParse = (s) => {
|
|
5
22
|
s.parseOperand();
|
|
6
23
|
let result = parseUntilFinalizer(s).root;
|
|
@@ -10,7 +27,7 @@ export const fullStringParse = (s) => {
|
|
|
10
27
|
if (s.finalizer === "=")
|
|
11
28
|
result = parseDefault(s);
|
|
12
29
|
else if (s.finalizer === "?")
|
|
13
|
-
result = result
|
|
30
|
+
result = [result, "?"];
|
|
14
31
|
s.scanner.shiftUntilNonWhitespace();
|
|
15
32
|
if (s.scanner.lookahead) {
|
|
16
33
|
// throw a parse error if non-whitespace characters made it here without being parsed
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type BaseParseContext, type BaseRoot, type MetaSchema, type Morph, type Predicate, type unwrapDefault } from "@ark/schema";
|
|
2
|
+
import { type array, type BuiltinObjectKind, type Constructor, type Domain, type show } from "@ark/util";
|
|
3
|
+
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPredicate, Out, withDefault } from "../attributes.ts";
|
|
4
|
+
import type { type } from "../keywords/keywords.ts";
|
|
5
|
+
import type { inferDefinition, validateDefinition } from "./definition.ts";
|
|
6
|
+
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
|
|
7
|
+
import type { ArkTypeScanner } from "./shift/scanner.ts";
|
|
8
|
+
import type { BaseCompletions } from "./string.ts";
|
|
9
|
+
export declare const maybeParseTupleExpression: (def: array, ctx: BaseParseContext) => BaseRoot | null;
|
|
10
|
+
export type maybeValidateTupleExpression<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends IndexOneExpression ? validateIndexOneExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
|
|
11
|
+
def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
|
|
12
|
+
def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
|
|
13
|
+
] : null;
|
|
14
|
+
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, 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 "=" ? withDefault<inferDefinition<def[0], $, args>, unwrapDefault<def[2]>> : 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;
|
|
15
|
+
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;
|
|
16
|
+
export type validateIndexOneExpression<def extends IndexOneExpression, $, args> = def[1] extends TuplePostfixOperator ? readonly [validateDefinition<def[0], $, args>, def[1]] : readonly [
|
|
17
|
+
validateDefinition<def[0], $, args>,
|
|
18
|
+
def["length"] extends 2 ? writeMissingRightOperandMessage<def[1]> : def[1],
|
|
19
|
+
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 "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ? MetaSchema : validateDefinition<def[2], $, args>
|
|
20
|
+
];
|
|
21
|
+
export type UnparsedTupleExpressionInput = {
|
|
22
|
+
instanceof: Constructor;
|
|
23
|
+
"===": unknown;
|
|
24
|
+
};
|
|
25
|
+
export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
|
|
26
|
+
export declare const parseKeyOfTuple: PrefixParser<"keyof">;
|
|
27
|
+
export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
|
|
28
|
+
export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
29
|
+
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
30
|
+
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
31
|
+
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
32
|
+
export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
33
|
+
export type ArgTwoOperator = Exclude<IndexOneOperator, "?" | "=">;
|
|
34
|
+
export type TuplePostfixOperator = "[]" | "?";
|
|
35
|
+
export type TupleInfixOperator = "&" | "|" | "=>" | "=" | ":" | "@";
|
|
36
|
+
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
37
|
+
export declare const parseMorphTuple: IndexOneParser<"=>">;
|
|
38
|
+
export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
|
|
39
|
+
export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
|
|
40
|
+
export declare const parseNarrowTuple: IndexOneParser<":">;
|
|
41
|
+
export type IndexZeroOperator = "keyof" | "instanceof" | "===";
|
|
42
|
+
export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
|
|
43
|
+
export type InfixExpression = readonly [
|
|
44
|
+
unknown,
|
|
45
|
+
ArkTypeScanner.InfixToken,
|
|
46
|
+
...unknown[]
|
|
47
|
+
];
|
|
48
|
+
export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Disjoint, intersectNodesRoot } from "@ark/schema";
|
|
2
|
+
import { objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
|
+
import { shallowDefaultableMessage, shallowOptionalMessage } from "./ast/validate.js";
|
|
4
|
+
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
|
|
5
|
+
export const maybeParseTupleExpression = (def, ctx) => isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
|
|
6
|
+
: isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
|
|
7
|
+
: null;
|
|
8
|
+
export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
|
|
9
|
+
const parseBranchTuple = (def, ctx) => {
|
|
10
|
+
if (def[2] === undefined)
|
|
11
|
+
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
12
|
+
const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
|
|
13
|
+
const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
|
|
14
|
+
if (def[1] === "|")
|
|
15
|
+
return ctx.$.node("union", { branches: [l, r] });
|
|
16
|
+
const result = intersectNodesRoot(l, r, ctx.$);
|
|
17
|
+
if (result instanceof Disjoint)
|
|
18
|
+
return result.throw();
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
|
|
22
|
+
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
23
|
+
export const parseMorphTuple = (def, ctx) => {
|
|
24
|
+
if (typeof def[2] !== "function") {
|
|
25
|
+
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
26
|
+
}
|
|
27
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
|
|
28
|
+
};
|
|
29
|
+
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
30
|
+
export const parseNarrowTuple = (def, ctx) => {
|
|
31
|
+
if (typeof def[2] !== "function") {
|
|
32
|
+
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
33
|
+
}
|
|
34
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
|
|
35
|
+
};
|
|
36
|
+
const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
|
|
37
|
+
const indexOneParsers = {
|
|
38
|
+
"[]": parseArrayTuple,
|
|
39
|
+
"|": parseBranchTuple,
|
|
40
|
+
"&": parseBranchTuple,
|
|
41
|
+
":": parseNarrowTuple,
|
|
42
|
+
"=>": parseMorphTuple,
|
|
43
|
+
"@": parseAttributeTuple,
|
|
44
|
+
// since object and tuple literals parse there via `parseProperty`,
|
|
45
|
+
// they must be shallow if parsed directly as a tuple expression
|
|
46
|
+
"=": () => throwParseError(shallowDefaultableMessage),
|
|
47
|
+
"?": () => throwParseError(shallowOptionalMessage)
|
|
48
|
+
};
|
|
49
|
+
const prefixParsers = {
|
|
50
|
+
keyof: parseKeyOfTuple,
|
|
51
|
+
instanceof: (def, ctx) => {
|
|
52
|
+
if (typeof def[1] !== "function") {
|
|
53
|
+
return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
|
|
54
|
+
}
|
|
55
|
+
const branches = def
|
|
56
|
+
.slice(1)
|
|
57
|
+
.map(ctor => typeof ctor === "function" ?
|
|
58
|
+
ctx.$.node("proto", { proto: ctor })
|
|
59
|
+
: throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
|
|
60
|
+
return branches.length === 1 ?
|
|
61
|
+
branches[0]
|
|
62
|
+
: ctx.$.node("union", { branches });
|
|
63
|
+
},
|
|
64
|
+
"===": (def, ctx) => ctx.$.units(def.slice(1))
|
|
65
|
+
};
|
|
66
|
+
const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
|
|
67
|
+
export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { postfixAfterOptionalOrDefaultableMessage, type BaseParseContext, type BaseRoot, type Sequence } from "@ark/schema";
|
|
2
|
+
import { type array, type conform, type ErrorMessage, type satisfy } from "@ark/util";
|
|
3
|
+
import type { inferDefinition, validateInnerDefinition } from "./definition.ts";
|
|
4
|
+
import { type DefaultablePropertyTuple, type isDefaultable, type OptionalPropertyDefinition } from "./property.ts";
|
|
5
|
+
export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
6
|
+
export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
|
|
7
|
+
export type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
|
|
8
|
+
type SequencePhase = satisfy<keyof Sequence.Inner, SequencePhase.prefix | SequencePhase.optionals | SequencePhase.defaultables | SequencePhase.postfix>;
|
|
9
|
+
declare namespace SequencePhase {
|
|
10
|
+
type prefix = "prefix";
|
|
11
|
+
type optionals = "optionals";
|
|
12
|
+
type defaultables = "defaultables";
|
|
13
|
+
type postfix = "postfix";
|
|
14
|
+
}
|
|
15
|
+
type SequenceParseState = {
|
|
16
|
+
unscanned: array;
|
|
17
|
+
inferred: array;
|
|
18
|
+
validated: array;
|
|
19
|
+
phase: SequencePhase;
|
|
20
|
+
};
|
|
21
|
+
type parseSequence<def extends array, $, args> = parseNextElement<{
|
|
22
|
+
unscanned: def;
|
|
23
|
+
inferred: [];
|
|
24
|
+
validated: [];
|
|
25
|
+
phase: SequencePhase.prefix;
|
|
26
|
+
}, $, args>;
|
|
27
|
+
type PreparsedElementKind = "required" | SequencePhase.optionals | SequencePhase.defaultables;
|
|
28
|
+
type PreparsedElement = {
|
|
29
|
+
head: unknown;
|
|
30
|
+
tail: array;
|
|
31
|
+
inferred: unknown;
|
|
32
|
+
validated: unknown;
|
|
33
|
+
kind: PreparsedElementKind;
|
|
34
|
+
spread: boolean;
|
|
35
|
+
};
|
|
36
|
+
declare namespace PreparsedElement {
|
|
37
|
+
type from<result extends PreparsedElement> = result;
|
|
38
|
+
type required = "required";
|
|
39
|
+
type optionals = "optionals";
|
|
40
|
+
type defaultables = "defaultables";
|
|
41
|
+
}
|
|
42
|
+
type preparseNextState<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? preparseNextElement<head, tail, true, $, args> : s["unscanned"] extends readonly [infer head, ...infer tail] ? preparseNextElement<head, tail, false, $, args> : null;
|
|
43
|
+
type preparseNextElement<head, tail extends array, spread extends boolean, $, args> = PreparsedElement.from<{
|
|
44
|
+
head: head;
|
|
45
|
+
tail: tail;
|
|
46
|
+
inferred: inferDefinition<head, $, args>;
|
|
47
|
+
validated: validateInnerDefinition<head, $, args>;
|
|
48
|
+
kind: head extends OptionalPropertyDefinition ? PreparsedElement.optionals : head extends DefaultablePropertyTuple ? PreparsedElement.defaultables : isDefaultable<head, $, args> extends true ? PreparsedElement.defaultables : PreparsedElement.required;
|
|
49
|
+
spread: spread;
|
|
50
|
+
}>;
|
|
51
|
+
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextState<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
52
|
+
unscanned: next["tail"];
|
|
53
|
+
inferred: nextInferred<s, next>;
|
|
54
|
+
validated: nextValidated<s, next>;
|
|
55
|
+
phase: next["kind"] extends (SequencePhase.optionals | SequencePhase.defaultables) ? next["kind"] : number extends nextInferred<s, next>["length"] ? s["phase"] : SequencePhase.prefix;
|
|
56
|
+
}, $, args> : s;
|
|
57
|
+
type nextInferred<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
58
|
+
...s["inferred"],
|
|
59
|
+
...conform<next["inferred"], array>
|
|
60
|
+
] : next["kind"] extends SequencePhase.optionals ? [
|
|
61
|
+
...s["inferred"],
|
|
62
|
+
next["inferred"]?
|
|
63
|
+
] : [...s["inferred"], next["inferred"]];
|
|
64
|
+
type nextValidated<s extends SequenceParseState, next extends PreparsedElement> = [
|
|
65
|
+
...s["validated"],
|
|
66
|
+
...nextValidatedSpreadOperatorIfPresent<s, next>,
|
|
67
|
+
nextValidatedElement<s, next>
|
|
68
|
+
];
|
|
69
|
+
type nextValidatedSpreadOperatorIfPresent<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
70
|
+
next["inferred"] extends infer spreadOperand extends array ? [
|
|
71
|
+
number,
|
|
72
|
+
number
|
|
73
|
+
] extends ([
|
|
74
|
+
s["inferred"]["length"],
|
|
75
|
+
spreadOperand["length"]
|
|
76
|
+
]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
|
|
77
|
+
] : [];
|
|
78
|
+
type nextValidatedElement<s extends SequenceParseState, next extends PreparsedElement> = next["kind"] extends SequencePhase.optionals ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : next["kind"] extends SequencePhase.defaultables ? next["spread"] extends true ? ErrorMessage<spreadDefaultableMessage> : s["phase"] extends SequencePhase.optionals ? ErrorMessage<defaultablePostOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : [s["phase"], next["spread"]] extends ([
|
|
79
|
+
SequencePhase.optionals | SequencePhase.defaultables,
|
|
80
|
+
false
|
|
81
|
+
]) ? ErrorMessage<postfixAfterOptionalOrDefaultableMessage> : next["validated"];
|
|
82
|
+
export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
|
|
83
|
+
type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
|
|
84
|
+
export declare const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
85
|
+
type multipleVariadicMessage = typeof multipleVariadicMesage;
|
|
86
|
+
export declare const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
87
|
+
export declare const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
88
|
+
type optionalOrDefaultableAfterVariadicMessage = typeof optionalOrDefaultableAfterVariadicMessage;
|
|
89
|
+
export declare const spreadOptionalMessage = "A spread element cannot be optional";
|
|
90
|
+
type spreadOptionalMessage = typeof spreadOptionalMessage;
|
|
91
|
+
export declare const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
92
|
+
type spreadDefaultableMessage = typeof spreadDefaultableMessage;
|
|
93
|
+
export declare const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|
|
94
|
+
type defaultablePostOptionalMessage = typeof defaultablePostOptionalMessage;
|
|
95
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { $ark, makeRootAndArrayPropertiesMutable, postfixAfterOptionalOrDefaultableMessage } from "@ark/schema";
|
|
2
|
+
import { append, isArray, isEmptyObject, throwParseError } from "@ark/util";
|
|
3
|
+
import { parseProperty } from "./property.js";
|
|
4
|
+
export const parseTupleLiteral = (def, ctx) => {
|
|
5
|
+
let sequences = [{}];
|
|
6
|
+
let i = 0;
|
|
7
|
+
while (i < def.length) {
|
|
8
|
+
let spread = false;
|
|
9
|
+
if (def[i] === "..." && i < def.length - 1) {
|
|
10
|
+
spread = true;
|
|
11
|
+
i++;
|
|
12
|
+
}
|
|
13
|
+
const parsedProperty = parseProperty(def[i], ctx);
|
|
14
|
+
const [valueNode, operator, possibleDefaultValue] = !isArray(parsedProperty) ? [parsedProperty] : parsedProperty;
|
|
15
|
+
i++;
|
|
16
|
+
if (spread) {
|
|
17
|
+
if (!valueNode.extends($ark.intrinsic.Array))
|
|
18
|
+
return throwParseError(writeNonArraySpreadMessage(valueNode.expression));
|
|
19
|
+
// a spread must be distributed over branches e.g.:
|
|
20
|
+
// def: [string, ...(number[] | [true, false])]
|
|
21
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
22
|
+
sequences = sequences.flatMap(base =>
|
|
23
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
24
|
+
valueNode.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
sequences = sequences.map(base => {
|
|
28
|
+
if (operator === "?")
|
|
29
|
+
return appendOptionalElement(base, valueNode);
|
|
30
|
+
if (operator === "=")
|
|
31
|
+
return appendDefaultableElement(base, valueNode, possibleDefaultValue);
|
|
32
|
+
return appendRequiredElement(base, valueNode);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
|
|
37
|
+
{
|
|
38
|
+
proto: Array,
|
|
39
|
+
exactLength: 0
|
|
40
|
+
}
|
|
41
|
+
: {
|
|
42
|
+
proto: Array,
|
|
43
|
+
sequence
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
const appendRequiredElement = (base, element) => {
|
|
47
|
+
if (base.defaultables || base.optionals) {
|
|
48
|
+
return throwParseError(base.variadic ?
|
|
49
|
+
// e.g. [boolean = true, ...string[], number]
|
|
50
|
+
postfixAfterOptionalOrDefaultableMessage
|
|
51
|
+
// e.g. [string?, number]
|
|
52
|
+
: requiredPostOptionalMessage);
|
|
53
|
+
}
|
|
54
|
+
if (base.variadic) {
|
|
55
|
+
// e.g. [...string[], number]
|
|
56
|
+
base.postfix = append(base.postfix, element);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// e.g. [string, number]
|
|
60
|
+
base.prefix = append(base.prefix, element);
|
|
61
|
+
}
|
|
62
|
+
return base;
|
|
63
|
+
};
|
|
64
|
+
const appendOptionalElement = (base, element) => {
|
|
65
|
+
if (base.variadic)
|
|
66
|
+
// e.g. [...string[], number?]
|
|
67
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
68
|
+
// e.g. [string, number?]
|
|
69
|
+
base.optionals = append(base.optionals, element);
|
|
70
|
+
return base;
|
|
71
|
+
};
|
|
72
|
+
const appendDefaultableElement = (base, element, value) => {
|
|
73
|
+
if (base.variadic)
|
|
74
|
+
// e.g. [...string[], number = 0]
|
|
75
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
76
|
+
if (base.optionals)
|
|
77
|
+
// e.g. [string?, number = 0]
|
|
78
|
+
return throwParseError(defaultablePostOptionalMessage);
|
|
79
|
+
// value's assignability to element will be checked when the
|
|
80
|
+
// sequence is instantiated by @ark/schema
|
|
81
|
+
// e.g. [string, number = 0]
|
|
82
|
+
base.defaultables = append(base.defaultables, [[element, value]]);
|
|
83
|
+
return base;
|
|
84
|
+
};
|
|
85
|
+
const appendVariadicElement = (base, element) => {
|
|
86
|
+
// e.g. [...string[], number, ...string[]]
|
|
87
|
+
if (base.postfix)
|
|
88
|
+
throwParseError(multipleVariadicMesage);
|
|
89
|
+
if (base.variadic) {
|
|
90
|
+
if (!base.variadic.equals(element)) {
|
|
91
|
+
// e.g. [...string[], ...number[]]
|
|
92
|
+
throwParseError(multipleVariadicMesage);
|
|
93
|
+
}
|
|
94
|
+
// e.g. [...string[], ...string[]]
|
|
95
|
+
// do nothing, second spread doesn't change the type
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// e.g. [string, ...number[]]
|
|
99
|
+
base.variadic = element.internal;
|
|
100
|
+
}
|
|
101
|
+
return base;
|
|
102
|
+
};
|
|
103
|
+
const appendSpreadBranch = (base, branch) => {
|
|
104
|
+
const spread = branch.firstReferenceOfKind("sequence");
|
|
105
|
+
if (!spread) {
|
|
106
|
+
// the only array with no sequence reference is unknown[]
|
|
107
|
+
return appendVariadicElement(base, $ark.intrinsic.unknown);
|
|
108
|
+
}
|
|
109
|
+
spread.prefix?.forEach(node => appendRequiredElement(base, node));
|
|
110
|
+
spread.optionals?.forEach(node => appendOptionalElement(base, node));
|
|
111
|
+
if (spread.variadic)
|
|
112
|
+
appendVariadicElement(base, spread.variadic);
|
|
113
|
+
spread.postfix?.forEach(node => appendRequiredElement(base, node));
|
|
114
|
+
return base;
|
|
115
|
+
};
|
|
116
|
+
export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
117
|
+
export const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
118
|
+
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
119
|
+
export const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
120
|
+
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
121
|
+
export const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
122
|
+
export const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|