affinirum 1.5.4 → 2.0.0
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/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
- package/.cache/function-syntax-tests.log +20 -0
- package/README.md +925 -355
- package/dst/Affinirum.d.ts +29 -19
- package/dst/Affinirum.js +399 -334
- package/dst/CallFrame.d.ts +12 -3
- package/dst/CallFrame.js +49 -10
- package/dst/Constant.d.ts +1 -3
- package/dst/Constant.js +3 -8
- package/dst/LexicalScope.d.ts +11 -9
- package/dst/LexicalScope.js +10 -6
- package/dst/ParserSpan.d.ts +11 -0
- package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
- package/dst/ParserState.d.ts +28 -49
- package/dst/ParserState.js +256 -489
- package/dst/Type.d.ts +34 -17
- package/dst/Type.js +211 -115
- package/dst/TypeParser.d.ts +9 -0
- package/dst/TypeParser.js +127 -0
- package/dst/Value.d.ts +1 -1
- package/dst/Value.js +21 -10
- package/dst/Variable.d.ts +16 -5
- package/dst/Variable.js +32 -7
- package/dst/atom/ArrayAtom.d.ts +10 -10
- package/dst/atom/ArrayAtom.js +62 -55
- package/dst/atom/Atom.d.ts +7 -0
- package/dst/atom/FunctionAtom.d.ts +13 -15
- package/dst/atom/FunctionAtom.js +53 -104
- package/dst/atom/FunctionSignature.d.ts +23 -0
- package/dst/atom/FunctionSignature.js +124 -0
- package/dst/atom/ObjectAtom.d.ts +11 -9
- package/dst/atom/ObjectAtom.js +73 -33
- package/dst/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/Affinirum.d.ts +29 -19
- package/dst/cjs/Affinirum.js +398 -333
- package/dst/cjs/CallFrame.d.ts +12 -3
- package/dst/cjs/CallFrame.js +49 -10
- package/dst/cjs/Constant.d.ts +1 -3
- package/dst/cjs/Constant.js +3 -8
- package/dst/cjs/LexicalScope.d.ts +11 -9
- package/dst/cjs/LexicalScope.js +10 -6
- package/dst/cjs/ParserSpan.d.ts +11 -0
- package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
- package/dst/cjs/ParserState.d.ts +28 -49
- package/dst/cjs/ParserState.js +255 -488
- package/dst/cjs/Type.d.ts +34 -17
- package/dst/cjs/Type.js +211 -115
- package/dst/cjs/TypeParser.d.ts +9 -0
- package/dst/cjs/TypeParser.js +131 -0
- package/dst/cjs/Value.d.ts +1 -1
- package/dst/cjs/Value.js +21 -10
- package/dst/cjs/Variable.d.ts +16 -5
- package/dst/cjs/Variable.js +36 -9
- package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
- package/dst/cjs/atom/ArrayAtom.js +62 -55
- package/dst/cjs/atom/Atom.d.ts +7 -0
- package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
- package/dst/cjs/atom/FunctionAtom.js +54 -105
- package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
- package/dst/cjs/atom/FunctionSignature.js +128 -0
- package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
- package/dst/cjs/atom/ObjectAtom.js +73 -33
- package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/cjs/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/dictionary/Constants.d.ts +2 -0
- package/dst/cjs/dictionary/Constants.js +27 -0
- package/dst/cjs/dictionary/Functions.d.ts +2 -0
- package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
- package/dst/cjs/native/Aggregable.js +10 -2
- package/dst/cjs/native/Array.d.ts +2 -3
- package/dst/cjs/native/Array.js +38 -39
- package/dst/cjs/native/Boolean.d.ts +1 -1
- package/dst/cjs/native/Boolean.js +13 -10
- package/dst/cjs/native/Buffer.d.ts +5 -5
- package/dst/cjs/native/Buffer.js +70 -46
- package/dst/cjs/native/Composite.d.ts +5 -0
- package/dst/cjs/native/Composite.js +35 -0
- package/dst/cjs/native/Enumerable.js +20 -8
- package/dst/cjs/native/Float.d.ts +3 -1
- package/dst/cjs/native/Float.js +51 -12
- package/dst/cjs/native/Integer.d.ts +4 -2
- package/dst/cjs/native/Integer.js +60 -25
- package/dst/cjs/native/Iterable.d.ts +0 -2
- package/dst/cjs/native/Iterable.js +9 -28
- package/dst/cjs/native/Number.d.ts +0 -2
- package/dst/cjs/native/Number.js +34 -48
- package/dst/cjs/native/Object.d.ts +1 -1
- package/dst/cjs/native/Object.js +5 -6
- package/dst/cjs/native/String.d.ts +7 -6
- package/dst/cjs/native/String.js +28 -27
- package/dst/cjs/native/Timestamp.d.ts +3 -3
- package/dst/cjs/native/Timestamp.js +34 -36
- package/dst/cjs/native/Unknown.d.ts +3 -3
- package/dst/cjs/native/Unknown.js +48 -17
- package/dst/cjs/native/notation/AN.d.ts +1 -1
- package/dst/cjs/native/notation/AN.js +37 -35
- package/dst/cjs/native/notation/IP.d.ts +1 -1
- package/dst/cjs/native/notation/IP.js +11 -12
- package/dst/cjs/native/notation/JSON.d.ts +1 -1
- package/dst/cjs/native/notation/JSON.js +7 -6
- package/dst/cjs/node/AccessNode.d.ts +24 -0
- package/dst/cjs/node/AccessNode.js +193 -0
- package/dst/cjs/node/ArrayNode.d.ts +7 -4
- package/dst/cjs/node/ArrayNode.js +26 -15
- package/dst/cjs/node/AssignableNode.d.ts +16 -0
- package/dst/cjs/node/AssignableNode.js +18 -0
- package/dst/cjs/node/AssignmentNode.d.ts +20 -0
- package/dst/cjs/node/AssignmentNode.js +66 -0
- package/dst/cjs/node/BlockNode.d.ts +5 -4
- package/dst/cjs/node/BlockNode.js +18 -11
- package/dst/cjs/node/CallNode.d.ts +7 -5
- package/dst/cjs/node/CallNode.js +40 -41
- package/dst/cjs/node/CastNode.d.ts +5 -6
- package/dst/cjs/node/CastNode.js +16 -17
- package/dst/cjs/node/ConstantNode.d.ts +4 -6
- package/dst/cjs/node/ConstantNode.js +9 -17
- package/dst/cjs/node/FinalNode.d.ts +19 -0
- package/dst/cjs/node/FinalNode.js +41 -0
- package/dst/cjs/node/FunctionNode.d.ts +17 -0
- package/dst/cjs/node/FunctionNode.js +51 -0
- package/dst/cjs/node/InvocationNode.d.ts +5 -0
- package/dst/cjs/node/InvocationNode.js +34 -0
- package/dst/cjs/node/JumpNode.d.ts +7 -9
- package/dst/cjs/node/JumpNode.js +12 -19
- package/dst/cjs/node/LoopNode.d.ts +7 -5
- package/dst/cjs/node/LoopNode.js +33 -22
- package/dst/cjs/node/MatchNode.d.ts +17 -0
- package/dst/cjs/node/MatchNode.js +65 -0
- package/dst/cjs/node/Node.d.ts +14 -0
- package/dst/cjs/{Node.js → node/Node.js} +17 -11
- package/dst/cjs/node/ObjectNode.d.ts +7 -4
- package/dst/cjs/node/ObjectNode.js +42 -11
- package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/cjs/node/TernaryNode.js +45 -0
- package/dst/cjs/node/VariableNode.d.ts +10 -6
- package/dst/cjs/node/VariableNode.js +50 -16
- package/dst/dictionary/Constants.d.ts +2 -0
- package/dst/dictionary/Constants.js +24 -0
- package/dst/dictionary/Functions.d.ts +2 -0
- package/dst/{Functions.js → dictionary/Functions.js} +26 -24
- package/dst/native/Aggregable.js +11 -3
- package/dst/native/Array.d.ts +2 -3
- package/dst/native/Array.js +37 -38
- package/dst/native/Boolean.d.ts +1 -1
- package/dst/native/Boolean.js +12 -9
- package/dst/native/Buffer.d.ts +5 -5
- package/dst/native/Buffer.js +66 -42
- package/dst/native/Composite.d.ts +5 -0
- package/dst/native/Composite.js +32 -0
- package/dst/native/Enumerable.js +20 -8
- package/dst/native/Float.d.ts +3 -1
- package/dst/native/Float.js +48 -11
- package/dst/native/Integer.d.ts +4 -2
- package/dst/native/Integer.js +56 -23
- package/dst/native/Iterable.d.ts +0 -2
- package/dst/native/Iterable.js +8 -27
- package/dst/native/Number.d.ts +0 -2
- package/dst/native/Number.js +34 -46
- package/dst/native/Object.d.ts +1 -1
- package/dst/native/Object.js +4 -5
- package/dst/native/String.d.ts +7 -6
- package/dst/native/String.js +24 -24
- package/dst/native/Timestamp.d.ts +3 -3
- package/dst/native/Timestamp.js +31 -33
- package/dst/native/Unknown.d.ts +3 -3
- package/dst/native/Unknown.js +49 -18
- package/dst/native/notation/AN.d.ts +1 -1
- package/dst/native/notation/AN.js +37 -35
- package/dst/native/notation/IP.d.ts +1 -1
- package/dst/native/notation/IP.js +10 -11
- package/dst/native/notation/JSON.d.ts +1 -1
- package/dst/native/notation/JSON.js +6 -5
- package/dst/node/AccessNode.d.ts +24 -0
- package/dst/node/AccessNode.js +189 -0
- package/dst/node/ArrayNode.d.ts +7 -4
- package/dst/node/ArrayNode.js +26 -15
- package/dst/node/AssignableNode.d.ts +16 -0
- package/dst/node/AssignableNode.js +14 -0
- package/dst/node/AssignmentNode.d.ts +20 -0
- package/dst/node/AssignmentNode.js +62 -0
- package/dst/node/BlockNode.d.ts +5 -4
- package/dst/node/BlockNode.js +18 -11
- package/dst/node/CallNode.d.ts +7 -5
- package/dst/node/CallNode.js +40 -41
- package/dst/node/CastNode.d.ts +5 -6
- package/dst/node/CastNode.js +16 -17
- package/dst/node/ConstantNode.d.ts +4 -6
- package/dst/node/ConstantNode.js +9 -17
- package/dst/node/FinalNode.d.ts +19 -0
- package/dst/node/FinalNode.js +37 -0
- package/dst/node/FunctionNode.d.ts +17 -0
- package/dst/node/FunctionNode.js +47 -0
- package/dst/node/InvocationNode.d.ts +5 -0
- package/dst/node/InvocationNode.js +30 -0
- package/dst/node/JumpNode.d.ts +7 -9
- package/dst/node/JumpNode.js +12 -19
- package/dst/node/LoopNode.d.ts +7 -5
- package/dst/node/LoopNode.js +33 -22
- package/dst/node/MatchNode.d.ts +17 -0
- package/dst/node/MatchNode.js +61 -0
- package/dst/node/Node.d.ts +14 -0
- package/dst/node/Node.js +28 -0
- package/dst/node/ObjectNode.d.ts +7 -4
- package/dst/node/ObjectNode.js +42 -11
- package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/node/TernaryNode.js +41 -0
- package/dst/node/VariableNode.d.ts +10 -6
- package/dst/node/VariableNode.js +50 -16
- package/package.json +4 -4
- package/dst/Atom.d.ts +0 -7
- package/dst/Constants.d.ts +0 -2
- package/dst/Constants.js +0 -24
- package/dst/Functions.d.ts +0 -2
- package/dst/Jump.d.ts +0 -1
- package/dst/Jump.js +0 -1
- package/dst/JumpException.d.ts +0 -9
- package/dst/JumpException.js +0 -14
- package/dst/Keywords.d.ts +0 -1
- package/dst/Keywords.js +0 -17
- package/dst/Node.d.ts +0 -14
- package/dst/Node.js +0 -22
- package/dst/ScriptSpan.d.ts +0 -10
- package/dst/cjs/Atom.d.ts +0 -7
- package/dst/cjs/Constants.d.ts +0 -2
- package/dst/cjs/Constants.js +0 -27
- package/dst/cjs/Functions.d.ts +0 -2
- package/dst/cjs/Jump.d.ts +0 -1
- package/dst/cjs/Jump.js +0 -2
- package/dst/cjs/JumpException.d.ts +0 -9
- package/dst/cjs/JumpException.js +0 -18
- package/dst/cjs/Keywords.d.ts +0 -1
- package/dst/cjs/Keywords.js +0 -20
- package/dst/cjs/Node.d.ts +0 -14
- package/dst/cjs/ScriptSpan.d.ts +0 -10
- package/dst/cjs/node/SwitchNode.js +0 -49
- package/dst/cjs/node/VariadicNode.d.ts +0 -4
- package/dst/cjs/node/VariadicNode.js +0 -10
- package/dst/node/SwitchNode.js +0 -45
- package/dst/node/VariadicNode.d.ts +0 -4
- package/dst/node/VariadicNode.js +0 -6
- /package/dst/{Atom.js → atom/Atom.js} +0 -0
- /package/dst/cjs/{Atom.js → atom/Atom.js} +0 -0
package/dst/Type.d.ts
CHANGED
|
@@ -5,12 +5,13 @@ import { ObjectAtom } from "./atom/ObjectAtom.js";
|
|
|
5
5
|
import { FunctionAtom } from "./atom/FunctionAtom.js";
|
|
6
6
|
type Atom = PrimitiveAtom | ArrayAtom | ObjectAtom | FunctionAtom;
|
|
7
7
|
export declare class Type {
|
|
8
|
-
protected readonly _atoms: Atom[];
|
|
8
|
+
protected readonly _atoms: Atom[] | undefined;
|
|
9
9
|
private constructor();
|
|
10
|
-
|
|
10
|
+
private atom;
|
|
11
|
+
get isNever(): boolean;
|
|
12
|
+
get isAny(): boolean;
|
|
11
13
|
get isAtom(): boolean;
|
|
12
|
-
get
|
|
13
|
-
get isVoid(): boolean;
|
|
14
|
+
get isNull(): boolean;
|
|
14
15
|
get isBoolean(): boolean;
|
|
15
16
|
get isTimestamp(): boolean;
|
|
16
17
|
get isFloat(): boolean;
|
|
@@ -20,27 +21,41 @@ export declare class Type {
|
|
|
20
21
|
get isArray(): boolean;
|
|
21
22
|
get isObject(): boolean;
|
|
22
23
|
get isFunction(): boolean;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
get isOptional(): boolean;
|
|
25
|
+
get isUniformArray(): boolean;
|
|
26
|
+
get isUniformObject(): boolean;
|
|
27
|
+
get isVariadicFunction(): boolean;
|
|
28
|
+
get isVolatileFunction(): boolean;
|
|
29
|
+
equals(type: Type | undefined): boolean;
|
|
30
|
+
intersects(type: Type | Atom): boolean;
|
|
31
|
+
accepts(type: Type | Atom): boolean;
|
|
32
|
+
acceptsValue(value: Value): boolean;
|
|
33
|
+
hasFunction(): boolean;
|
|
34
|
+
reduce(type: Type): Type | undefined;
|
|
28
35
|
weight(): number;
|
|
29
|
-
arrayAtomValType(index
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
arrayAtomValType(index?: number): Type;
|
|
37
|
+
objectAtomValType(key?: string): Type;
|
|
38
|
+
functionAtomRetType(): Type | undefined;
|
|
39
|
+
functionAtomArgType(index: number): Type | undefined;
|
|
40
|
+
arrayAtoms(): ArrayAtom[];
|
|
41
|
+
objectAtoms(): ObjectAtom[];
|
|
42
|
+
functionAtoms(): FunctionAtom[];
|
|
43
|
+
functionSignatures(): [retType: Type, argTypes: readonly Type[], variadic?: boolean][];
|
|
44
|
+
optionalFirstParameter(): Type;
|
|
32
45
|
toOptional(): Type;
|
|
33
46
|
toMandatory(): Type;
|
|
34
47
|
toString(): string;
|
|
35
48
|
static union(...types: Type[]): Type;
|
|
36
49
|
static of(value: Value): Type;
|
|
37
50
|
static isPrimitive(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
|
|
51
|
+
static isFunction(value: Value): value is (...args: any[]) => Value;
|
|
38
52
|
static primitiveType(primitive: Primitive): Type;
|
|
39
|
-
static arrayType(
|
|
40
|
-
static objectType(
|
|
41
|
-
static functionType(
|
|
42
|
-
static readonly
|
|
43
|
-
static readonly
|
|
53
|
+
static arrayType(types?: Type | readonly Type[]): Type;
|
|
54
|
+
static objectType(types?: Type | readonly (readonly [key: string, valType: Type])[]): Type;
|
|
55
|
+
static functionType(signatures?: [retType: Type, argTypes: readonly Type[], variadic?: boolean][], volatile?: boolean): Type;
|
|
56
|
+
static readonly Never: Type;
|
|
57
|
+
static readonly Any: Type;
|
|
58
|
+
static readonly Null: Type;
|
|
44
59
|
static readonly Boolean: Type;
|
|
45
60
|
static readonly OptionalBoolean: Type;
|
|
46
61
|
static readonly Timestamp: Type;
|
|
@@ -63,6 +78,8 @@ export declare class Type {
|
|
|
63
78
|
static readonly OptionalReference: Type;
|
|
64
79
|
static readonly Number: Type;
|
|
65
80
|
static readonly OptionalNumber: Type;
|
|
81
|
+
static readonly Composite: Type;
|
|
82
|
+
static readonly OptionalComposite: Type;
|
|
66
83
|
static readonly Enumerable: Type;
|
|
67
84
|
static readonly OptionalEnumerable: Type;
|
|
68
85
|
static readonly Iterable: Type;
|
package/dst/Type.js
CHANGED
|
@@ -4,91 +4,131 @@ import { ObjectAtom } from "./atom/ObjectAtom.js";
|
|
|
4
4
|
import { FunctionAtom } from "./atom/FunctionAtom.js";
|
|
5
5
|
export class Type {
|
|
6
6
|
_atoms;
|
|
7
|
-
constructor(
|
|
8
|
-
this._atoms =
|
|
7
|
+
constructor(atoms) {
|
|
8
|
+
this._atoms = atoms;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
return this._atoms
|
|
10
|
+
atom(ix) {
|
|
11
|
+
return (this._atoms ?? [])[ix];
|
|
12
12
|
}
|
|
13
|
-
get
|
|
14
|
-
return this._atoms
|
|
13
|
+
get isNever() {
|
|
14
|
+
return !this._atoms;
|
|
15
15
|
}
|
|
16
|
-
get
|
|
17
|
-
return this.
|
|
16
|
+
get isAny() {
|
|
17
|
+
return this._atoms?.length === 0;
|
|
18
|
+
}
|
|
19
|
+
get isAtom() {
|
|
20
|
+
return this._atoms?.length === 1;
|
|
18
21
|
}
|
|
19
|
-
get
|
|
20
|
-
return this.
|
|
22
|
+
get isNull() {
|
|
23
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Null.atom(0));
|
|
21
24
|
}
|
|
22
25
|
get isBoolean() {
|
|
23
|
-
return this.
|
|
26
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Boolean.atom(0));
|
|
24
27
|
}
|
|
25
28
|
get isTimestamp() {
|
|
26
|
-
return this.
|
|
29
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Timestamp.atom(0));
|
|
27
30
|
}
|
|
28
31
|
get isFloat() {
|
|
29
|
-
return this.
|
|
32
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Float.atom(0));
|
|
30
33
|
}
|
|
31
34
|
get isInteger() {
|
|
32
|
-
return this.
|
|
35
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Integer.atom(0));
|
|
33
36
|
}
|
|
34
37
|
get isBuffer() {
|
|
35
|
-
return this.
|
|
38
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Buffer.atom(0));
|
|
36
39
|
}
|
|
37
40
|
get isString() {
|
|
38
|
-
return this.
|
|
41
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.String.atom(0));
|
|
39
42
|
}
|
|
40
43
|
get isArray() {
|
|
41
|
-
return this._atoms
|
|
44
|
+
return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom));
|
|
42
45
|
}
|
|
43
46
|
get isObject() {
|
|
44
|
-
return this._atoms
|
|
47
|
+
return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom));
|
|
45
48
|
}
|
|
46
49
|
get isFunction() {
|
|
47
|
-
return this._atoms
|
|
50
|
+
return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom));
|
|
48
51
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
get isOptional() {
|
|
53
|
+
return Boolean(this._atoms?.length === 0 || this._atoms?.some((a) => a.equals(Type.Null.atom(0))));
|
|
54
|
+
}
|
|
55
|
+
get isUniformArray() {
|
|
56
|
+
return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom && a.isUniform));
|
|
57
|
+
}
|
|
58
|
+
get isUniformObject() {
|
|
59
|
+
return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom && a.isUniform));
|
|
60
|
+
}
|
|
61
|
+
get isVariadicFunction() {
|
|
62
|
+
return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom && a.isVariadic));
|
|
63
|
+
}
|
|
64
|
+
get isVolatileFunction() {
|
|
65
|
+
return Boolean(this._atoms?.some((a) => a instanceof FunctionAtom && a.isVolatile));
|
|
66
|
+
}
|
|
67
|
+
equals(type) {
|
|
68
|
+
if (this !== type) {
|
|
69
|
+
if (!type || this.isNever !== type.isNever || this._atoms?.length !== type._atoms?.length) {
|
|
57
70
|
return false;
|
|
58
71
|
}
|
|
59
|
-
unmatched.
|
|
72
|
+
const unmatched = [...type._atoms ?? []];
|
|
73
|
+
for (const atom of this._atoms ?? []) {
|
|
74
|
+
const index = unmatched.findIndex((a) => atom.equals(a));
|
|
75
|
+
if (index < 0) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
unmatched.splice(index, 1);
|
|
79
|
+
}
|
|
60
80
|
}
|
|
61
81
|
return true;
|
|
62
82
|
}
|
|
63
|
-
|
|
83
|
+
intersects(type) {
|
|
64
84
|
if (type instanceof Type) {
|
|
65
|
-
if (this.
|
|
85
|
+
if (this.isNever || type.isNever) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return Boolean(this.isAny || type.isAny || this._atoms?.some((a) => type._atoms?.some((atom) => a.intersects(atom))));
|
|
89
|
+
}
|
|
90
|
+
return Boolean(this.isAny || this._atoms?.some((atom) => atom.intersects(type)));
|
|
91
|
+
}
|
|
92
|
+
accepts(type) {
|
|
93
|
+
if (type instanceof Type) {
|
|
94
|
+
if (this.isNever || type.isNever) {
|
|
95
|
+
return type.isNever;
|
|
96
|
+
}
|
|
97
|
+
if (this.isAny || type.isAny) {
|
|
66
98
|
return true;
|
|
67
99
|
}
|
|
68
|
-
return type._atoms
|
|
100
|
+
return Boolean(type._atoms?.every((a) => this._atoms?.some((atom) => atom.accepts(a))));
|
|
69
101
|
}
|
|
70
|
-
return this._atoms
|
|
102
|
+
return Boolean(this._atoms?.some((a) => a.accepts(type)));
|
|
71
103
|
}
|
|
72
|
-
|
|
73
|
-
if (this.
|
|
104
|
+
acceptsValue(value) {
|
|
105
|
+
if (this.isAny) {
|
|
74
106
|
return true;
|
|
75
107
|
}
|
|
76
|
-
const valueAtom = Type.of(value)._atoms[0];
|
|
77
|
-
return this._atoms
|
|
78
|
-
|
|
108
|
+
const valueAtom = Type.of(value)._atoms?.[0];
|
|
109
|
+
return Boolean(valueAtom && this._atoms?.some((atom) => {
|
|
110
|
+
const matches = atom instanceof ArrayAtom
|
|
111
|
+
? valueAtom instanceof ArrayAtom
|
|
112
|
+
: atom instanceof ObjectAtom
|
|
113
|
+
? valueAtom instanceof ObjectAtom
|
|
114
|
+
: atom.accepts(valueAtom);
|
|
115
|
+
if (!matches) {
|
|
79
116
|
return false;
|
|
80
117
|
}
|
|
118
|
+
if ((atom instanceof ArrayAtom || atom instanceof ObjectAtom) && atom.valType(undefined)?.isAny) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
81
121
|
if (atom instanceof ArrayAtom) {
|
|
82
122
|
const values = value;
|
|
83
123
|
const types = atom.valTypes();
|
|
84
124
|
if (!types.length) {
|
|
85
125
|
return true;
|
|
86
126
|
}
|
|
87
|
-
if (atom.
|
|
88
|
-
return types.every((type, ix) => type.
|
|
127
|
+
if (!atom.isUniform) {
|
|
128
|
+
return types.every((type, ix) => type.acceptsValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
|
|
89
129
|
}
|
|
90
130
|
for (let i = 0; i < values.length; ++i) {
|
|
91
|
-
if (!types[0].
|
|
131
|
+
if (!types[0].acceptsValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
|
|
92
132
|
return false;
|
|
93
133
|
}
|
|
94
134
|
}
|
|
@@ -96,93 +136,143 @@ export class Type {
|
|
|
96
136
|
}
|
|
97
137
|
if (atom instanceof ObjectAtom) {
|
|
98
138
|
const values = value;
|
|
99
|
-
return atom.valTypes().every(([key, type]) =>
|
|
139
|
+
return atom.valTypes().every(([key, type]) => key != null
|
|
140
|
+
? type.acceptsValue(Object.hasOwn(values, key) ? values[key] : undefined)
|
|
141
|
+
: Object.values(values).every((v) => type.acceptsValue(v)));
|
|
100
142
|
}
|
|
101
143
|
return true;
|
|
102
|
-
});
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
hasFunction() {
|
|
147
|
+
return Boolean(this._atoms?.some((atom) => atom instanceof FunctionAtom ||
|
|
148
|
+
atom instanceof ArrayAtom && atom.valTypes().some((type) => type.hasFunction()) ||
|
|
149
|
+
atom instanceof ObjectAtom && atom.valTypes().some(([, type]) => type.hasFunction())));
|
|
103
150
|
}
|
|
104
|
-
reduce(
|
|
105
|
-
if (
|
|
151
|
+
reduce(type) {
|
|
152
|
+
if (this.isNever || type.isNever) {
|
|
153
|
+
return Type.Never;
|
|
154
|
+
}
|
|
155
|
+
if (type.isAny || type.isNull) {
|
|
106
156
|
return this;
|
|
107
157
|
}
|
|
108
|
-
if (this.
|
|
109
|
-
return
|
|
158
|
+
if (this.isAny) {
|
|
159
|
+
return type;
|
|
110
160
|
}
|
|
111
|
-
const list = this._atoms
|
|
112
|
-
return list
|
|
161
|
+
const list = this._atoms?.filter((a) => type.accepts(a));
|
|
162
|
+
return list?.length === 0
|
|
113
163
|
? undefined
|
|
114
|
-
: list
|
|
164
|
+
: list?.length === this._atoms?.length
|
|
115
165
|
? this
|
|
116
166
|
: new Type(list);
|
|
117
167
|
}
|
|
118
|
-
intersect(type) {
|
|
119
|
-
if (this.isUnknown || type.isUnknown) {
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
|
|
123
|
-
}
|
|
124
168
|
weight() {
|
|
125
|
-
return this._atoms
|
|
169
|
+
return this._atoms?.reduce((acc, a) => acc + a.weight(), 0) ?? 0;
|
|
126
170
|
}
|
|
127
171
|
arrayAtomValType(index) {
|
|
128
|
-
const atoms = this.
|
|
172
|
+
const atoms = this.arrayAtoms();
|
|
129
173
|
if (!atoms.length) {
|
|
130
|
-
return Type.
|
|
174
|
+
return Type.Any;
|
|
131
175
|
}
|
|
132
176
|
const valTypes = [];
|
|
133
177
|
for (const atom of atoms) {
|
|
134
|
-
const valType = atom.
|
|
135
|
-
? atom.valType(index)
|
|
136
|
-
: atom.valType(0);
|
|
178
|
+
const valType = atom.valType(index);
|
|
137
179
|
if (!valType) {
|
|
138
|
-
return Type.
|
|
180
|
+
return Type.Any;
|
|
139
181
|
}
|
|
140
182
|
valTypes.push(valType);
|
|
141
183
|
}
|
|
142
184
|
return Type.union(...valTypes);
|
|
143
185
|
}
|
|
144
|
-
|
|
145
|
-
const atoms = this.
|
|
146
|
-
|
|
186
|
+
objectAtomValType(key) {
|
|
187
|
+
const atoms = this.objectAtoms();
|
|
188
|
+
if (!atoms.length) {
|
|
189
|
+
return Type.Any;
|
|
190
|
+
}
|
|
191
|
+
const valTypes = [];
|
|
192
|
+
for (const atom of atoms) {
|
|
193
|
+
const valType = atom.valType(key);
|
|
194
|
+
if (!valType) {
|
|
195
|
+
return Type.Any;
|
|
196
|
+
}
|
|
197
|
+
valTypes.push(valType);
|
|
198
|
+
}
|
|
199
|
+
return Type.union(...valTypes);
|
|
147
200
|
}
|
|
148
|
-
|
|
149
|
-
|
|
201
|
+
functionAtomRetType() {
|
|
202
|
+
const types = this.functionAtoms().map((atom) => atom.retType());
|
|
203
|
+
return types.length ? Type.union(...types) : undefined;
|
|
204
|
+
}
|
|
205
|
+
functionAtomArgType(index) {
|
|
206
|
+
const types = this.functionAtoms().map((atom) => atom.argType(index)).filter((type) => type !== undefined);
|
|
207
|
+
return types.length ? Type.union(...types) : undefined;
|
|
208
|
+
}
|
|
209
|
+
arrayAtoms() {
|
|
210
|
+
if (this.isAny) {
|
|
211
|
+
return Type.Array._atoms;
|
|
212
|
+
}
|
|
213
|
+
return this._atoms?.filter((a) => a instanceof ArrayAtom) ?? [];
|
|
214
|
+
}
|
|
215
|
+
objectAtoms() {
|
|
216
|
+
if (this.isAny) {
|
|
217
|
+
return Type.Object._atoms;
|
|
218
|
+
}
|
|
219
|
+
return this._atoms?.filter((a) => a instanceof ObjectAtom) ?? [];
|
|
220
|
+
}
|
|
221
|
+
functionAtoms() {
|
|
222
|
+
if (this.isAny || this.isNever) {
|
|
150
223
|
return Type.Function._atoms;
|
|
151
224
|
}
|
|
152
|
-
return this._atoms
|
|
225
|
+
return this._atoms?.filter((a) => a instanceof FunctionAtom) ?? [];
|
|
226
|
+
}
|
|
227
|
+
functionSignatures() {
|
|
228
|
+
return this.functionAtoms().flatMap((i) => i.signatures()).map((i) => [i.retType, i.argTypes, i.isVariadic]);
|
|
229
|
+
}
|
|
230
|
+
optionalFirstParameter() {
|
|
231
|
+
return this.isNever
|
|
232
|
+
? this
|
|
233
|
+
: Type.union(...this.functionAtoms().map((atom) => new Type([atom.firstArgOptional()])));
|
|
153
234
|
}
|
|
154
235
|
toOptional() {
|
|
155
|
-
return
|
|
236
|
+
return Type.union(Type.Null, this);
|
|
156
237
|
}
|
|
157
238
|
toMandatory() {
|
|
158
|
-
if (!this.isOptional || this.
|
|
239
|
+
if (!this._atoms?.length || !this.isOptional || this.isNull) {
|
|
159
240
|
return this;
|
|
160
241
|
}
|
|
161
|
-
return new Type(this._atoms.filter((a) => !a.
|
|
242
|
+
return new Type(this._atoms.filter((a) => !a.equals(Type.Null.atom(0))));
|
|
162
243
|
}
|
|
163
244
|
toString() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
245
|
+
if (this._atoms == null) {
|
|
246
|
+
return "never";
|
|
247
|
+
}
|
|
248
|
+
if (this._atoms.length === 0) {
|
|
249
|
+
return "any";
|
|
250
|
+
}
|
|
251
|
+
return [...this._atoms]
|
|
252
|
+
.sort((a, b) => a.weight() - b.weight())
|
|
253
|
+
.map((a) => a.toString())
|
|
254
|
+
.join("|");
|
|
170
255
|
}
|
|
171
256
|
static union(...types) {
|
|
172
|
-
if (types.some((t) => t.
|
|
173
|
-
return Type.
|
|
257
|
+
if (types.some((t) => t.isAny)) {
|
|
258
|
+
return Type.Any;
|
|
259
|
+
}
|
|
260
|
+
if (types.length && types.every((t) => t.isNever)) {
|
|
261
|
+
return Type.Never;
|
|
174
262
|
}
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
263
|
+
const atoms = [];
|
|
264
|
+
for (const type of types) {
|
|
265
|
+
for (const atom of type._atoms ?? []) {
|
|
266
|
+
if (!atoms.some((a) => a.equals(atom))) {
|
|
267
|
+
atoms.push(atom);
|
|
268
|
+
}
|
|
179
269
|
}
|
|
180
|
-
}
|
|
181
|
-
return
|
|
270
|
+
}
|
|
271
|
+
return new Type(atoms);
|
|
182
272
|
}
|
|
183
273
|
static of(value) {
|
|
184
274
|
return value == null
|
|
185
|
-
? Type.
|
|
275
|
+
? Type.Null
|
|
186
276
|
: typeof value === "boolean"
|
|
187
277
|
? Type.Boolean
|
|
188
278
|
: value instanceof Date
|
|
@@ -202,52 +292,58 @@ export class Type {
|
|
|
202
292
|
: Type.Function;
|
|
203
293
|
}
|
|
204
294
|
static isPrimitive(value) {
|
|
205
|
-
return value == null
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
295
|
+
return value == null ||
|
|
296
|
+
typeof value === "boolean" ||
|
|
297
|
+
value instanceof Date ||
|
|
298
|
+
typeof value === "number" ||
|
|
299
|
+
typeof value === "bigint" ||
|
|
300
|
+
value instanceof ArrayBuffer ||
|
|
301
|
+
typeof value === "string";
|
|
302
|
+
}
|
|
303
|
+
static isFunction(value) {
|
|
304
|
+
return typeof value === "function";
|
|
212
305
|
}
|
|
213
306
|
static primitiveType(primitive) {
|
|
214
307
|
return new Type([new PrimitiveAtom(primitive)]);
|
|
215
308
|
}
|
|
216
|
-
static arrayType(
|
|
217
|
-
return new Type([new ArrayAtom(
|
|
309
|
+
static arrayType(types = Type.Any) {
|
|
310
|
+
return new Type([new ArrayAtom(types)]);
|
|
218
311
|
}
|
|
219
|
-
static objectType(
|
|
220
|
-
return new Type([new ObjectAtom(
|
|
312
|
+
static objectType(types = Type.Any) {
|
|
313
|
+
return new Type([new ObjectAtom(types)]);
|
|
221
314
|
}
|
|
222
|
-
static functionType(
|
|
223
|
-
return new Type([new FunctionAtom(
|
|
315
|
+
static functionType(signatures = [[Type.Any, [], true]], volatile) {
|
|
316
|
+
return new Type([new FunctionAtom(signatures, volatile)]);
|
|
224
317
|
}
|
|
225
|
-
static
|
|
226
|
-
static
|
|
318
|
+
static Never = new Type();
|
|
319
|
+
static Any = new Type([]);
|
|
320
|
+
static Null = Type.primitiveType("null");
|
|
227
321
|
static Boolean = Type.primitiveType("boolean");
|
|
228
|
-
static OptionalBoolean = Type.union(Type.
|
|
322
|
+
static OptionalBoolean = Type.union(Type.Null, Type.Boolean);
|
|
229
323
|
static Timestamp = Type.primitiveType("timestamp");
|
|
230
|
-
static OptionalTimestamp = Type.union(Type.
|
|
324
|
+
static OptionalTimestamp = Type.union(Type.Null, Type.Timestamp);
|
|
231
325
|
static Float = Type.primitiveType("float");
|
|
232
|
-
static OptionalFloat = Type.union(Type.
|
|
326
|
+
static OptionalFloat = Type.union(Type.Null, Type.Float);
|
|
233
327
|
static Integer = Type.primitiveType("integer");
|
|
234
|
-
static OptionalInteger = Type.union(Type.
|
|
328
|
+
static OptionalInteger = Type.union(Type.Null, Type.Integer);
|
|
235
329
|
static Buffer = Type.primitiveType("buffer");
|
|
236
|
-
static OptionalBuffer = Type.union(Type.
|
|
330
|
+
static OptionalBuffer = Type.union(Type.Null, Type.Buffer);
|
|
237
331
|
static String = Type.primitiveType("string");
|
|
238
|
-
static OptionalString = Type.union(Type.
|
|
332
|
+
static OptionalString = Type.union(Type.Null, Type.String);
|
|
239
333
|
static Array = Type.arrayType();
|
|
240
|
-
static OptionalArray = Type.union(Type.
|
|
334
|
+
static OptionalArray = Type.union(Type.Null, Type.Array);
|
|
241
335
|
static Object = Type.objectType();
|
|
242
|
-
static OptionalObject = Type.union(Type.
|
|
243
|
-
static Function = Type.functionType(
|
|
244
|
-
static OptionalFunction = Type.union(Type.
|
|
336
|
+
static OptionalObject = Type.union(Type.Null, Type.Object);
|
|
337
|
+
static Function = Type.functionType();
|
|
338
|
+
static OptionalFunction = Type.union(Type.Null, Type.Function);
|
|
245
339
|
static Reference = Type.union(Type.Array, Type.Object, Type.Function);
|
|
246
|
-
static OptionalReference = Type.union(Type.
|
|
340
|
+
static OptionalReference = Type.union(Type.Null, Type.Array, Type.Object, Type.Function);
|
|
247
341
|
static Number = Type.union(Type.Float, Type.Integer);
|
|
248
|
-
static OptionalNumber = Type.union(Type.
|
|
342
|
+
static OptionalNumber = Type.union(Type.Null, Type.Float, Type.Integer);
|
|
343
|
+
static Composite = Type.union(Type.Array, Type.Object);
|
|
344
|
+
static OptionalComposite = Type.union(Type.Null, Type.Array, Type.Object);
|
|
249
345
|
static Enumerable = Type.union(Type.Buffer, Type.String, Type.Array);
|
|
250
|
-
static OptionalEnumerable = Type.union(Type.
|
|
346
|
+
static OptionalEnumerable = Type.union(Type.Null, Type.Buffer, Type.String, Type.Array);
|
|
251
347
|
static Iterable = Type.union(Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
252
|
-
static OptionalIterable = Type.union(Type.
|
|
348
|
+
static OptionalIterable = Type.union(Type.Null, Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
253
349
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Type } from "./Type.js";
|
|
2
|
+
import { ParserState } from "./ParserState.js";
|
|
3
|
+
import { LexicalScope } from "./LexicalScope.js";
|
|
4
|
+
export declare class TypeParser {
|
|
5
|
+
parse(state: ParserState, scope: LexicalScope): Type;
|
|
6
|
+
protected _function(state: ParserState, scope: LexicalScope): Type;
|
|
7
|
+
protected _optional(state: ParserState, scope: LexicalScope): Type;
|
|
8
|
+
protected _element(state: ParserState, scope: LexicalScope): Type;
|
|
9
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Type } from "./Type.js";
|
|
2
|
+
export class TypeParser {
|
|
3
|
+
parse(state, scope) {
|
|
4
|
+
let type = this._function(state, scope);
|
|
5
|
+
while (state.equals("|")) { // type union
|
|
6
|
+
type = Type.union(type, this._function(state.next(), scope));
|
|
7
|
+
}
|
|
8
|
+
return type;
|
|
9
|
+
}
|
|
10
|
+
_function(state, scope) {
|
|
11
|
+
let type = this._optional(state, scope);
|
|
12
|
+
const signatures = type.functionSignatures();
|
|
13
|
+
while (type.isAtom && type.isFunction && state.equals("&")) {
|
|
14
|
+
const next = this._optional(state.next(), scope);
|
|
15
|
+
if (!next.isAtom || !next.isFunction) {
|
|
16
|
+
state.throwError("missing function types");
|
|
17
|
+
}
|
|
18
|
+
signatures.push(...next.functionSignatures());
|
|
19
|
+
type = Type.functionType(signatures);
|
|
20
|
+
}
|
|
21
|
+
return type;
|
|
22
|
+
}
|
|
23
|
+
_optional(state, scope) {
|
|
24
|
+
let type = this._element(state, scope);
|
|
25
|
+
if (state.equals("?")) {
|
|
26
|
+
if (type.isNull || type.isNever) {
|
|
27
|
+
state.throwError(`optional suffix is invalid on type ${type}`);
|
|
28
|
+
}
|
|
29
|
+
type = type.toOptional();
|
|
30
|
+
state.next();
|
|
31
|
+
}
|
|
32
|
+
return type;
|
|
33
|
+
}
|
|
34
|
+
_element(state, scope) {
|
|
35
|
+
if (state.isType) { // literal type
|
|
36
|
+
const type = state.type;
|
|
37
|
+
state.next();
|
|
38
|
+
return type;
|
|
39
|
+
}
|
|
40
|
+
if (state.equals("null")) {
|
|
41
|
+
state.next();
|
|
42
|
+
return Type.Null;
|
|
43
|
+
}
|
|
44
|
+
if (state.equals("never")) {
|
|
45
|
+
state.next();
|
|
46
|
+
return Type.Never;
|
|
47
|
+
}
|
|
48
|
+
else if (state.equals("[")) { // composite type
|
|
49
|
+
state.next();
|
|
50
|
+
if (state.equals("...")) {
|
|
51
|
+
const object = state.next().equals(":");
|
|
52
|
+
if (object) {
|
|
53
|
+
state.next();
|
|
54
|
+
}
|
|
55
|
+
const valType = this.parse(state, scope);
|
|
56
|
+
state.closeBrackets();
|
|
57
|
+
state.next();
|
|
58
|
+
return object ? Type.objectType(valType) : Type.arrayType(valType);
|
|
59
|
+
}
|
|
60
|
+
const valTypes = [];
|
|
61
|
+
let index = 0, colon = state.equals(":");
|
|
62
|
+
if (colon) {
|
|
63
|
+
state.next().closeBrackets();
|
|
64
|
+
}
|
|
65
|
+
while (!state.equals("]")) {
|
|
66
|
+
if (state.isLiteral && typeof state.literal.value === "string") {
|
|
67
|
+
const key = state.literal.value;
|
|
68
|
+
state.next().assertColon();
|
|
69
|
+
valTypes.push([key, this.parse(state.next(), scope)]);
|
|
70
|
+
colon = true;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
valTypes.push([String(index++), this.parse(state, scope)]);
|
|
74
|
+
}
|
|
75
|
+
if (!state.equals(",")) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
state.next();
|
|
79
|
+
}
|
|
80
|
+
state.closeBrackets();
|
|
81
|
+
state.next();
|
|
82
|
+
return colon
|
|
83
|
+
? Type.objectType(valTypes)
|
|
84
|
+
: Type.arrayType(valTypes.map(([, v]) => v));
|
|
85
|
+
}
|
|
86
|
+
else if (state.equals("{")) { // function type
|
|
87
|
+
if (state.next().equals(":")) {
|
|
88
|
+
state.next().closeBraces();
|
|
89
|
+
state.next();
|
|
90
|
+
return Type.Function;
|
|
91
|
+
}
|
|
92
|
+
state.openParentheses();
|
|
93
|
+
let variadic = false;
|
|
94
|
+
const argTypes = [];
|
|
95
|
+
while (!state.next().equals(")")) {
|
|
96
|
+
variadic = state.equals("...");
|
|
97
|
+
if (variadic) {
|
|
98
|
+
state.next();
|
|
99
|
+
}
|
|
100
|
+
const argType = this.parse(state, scope);
|
|
101
|
+
argTypes.push(variadic ? Type.arrayType(argType) : argType);
|
|
102
|
+
if (variadic) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
if (!state.equals(",")) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
state.closeParentheses();
|
|
110
|
+
state.next().assertColon();
|
|
111
|
+
const retType = this.parse(state.next(), scope);
|
|
112
|
+
state.closeBraces();
|
|
113
|
+
state.next();
|
|
114
|
+
return Type.functionType([[retType, argTypes, variadic]]);
|
|
115
|
+
}
|
|
116
|
+
else if (state.equals("(")) { // type grouping
|
|
117
|
+
state.next();
|
|
118
|
+
const type = this.parse(state, scope);
|
|
119
|
+
state.closeParentheses();
|
|
120
|
+
state.next();
|
|
121
|
+
return type;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
state.throwError("missing type name");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|