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/cjs/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/cjs/Type.js
CHANGED
|
@@ -7,91 +7,131 @@ const ObjectAtom_js_1 = require("./atom/ObjectAtom.js");
|
|
|
7
7
|
const FunctionAtom_js_1 = require("./atom/FunctionAtom.js");
|
|
8
8
|
class Type {
|
|
9
9
|
_atoms;
|
|
10
|
-
constructor(
|
|
11
|
-
this._atoms =
|
|
10
|
+
constructor(atoms) {
|
|
11
|
+
this._atoms = atoms;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
return this._atoms
|
|
13
|
+
atom(ix) {
|
|
14
|
+
return (this._atoms ?? [])[ix];
|
|
15
15
|
}
|
|
16
|
-
get
|
|
17
|
-
return this._atoms
|
|
16
|
+
get isNever() {
|
|
17
|
+
return !this._atoms;
|
|
18
18
|
}
|
|
19
|
-
get
|
|
20
|
-
return this.
|
|
19
|
+
get isAny() {
|
|
20
|
+
return this._atoms?.length === 0;
|
|
21
|
+
}
|
|
22
|
+
get isAtom() {
|
|
23
|
+
return this._atoms?.length === 1;
|
|
21
24
|
}
|
|
22
|
-
get
|
|
23
|
-
return this.
|
|
25
|
+
get isNull() {
|
|
26
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Null.atom(0));
|
|
24
27
|
}
|
|
25
28
|
get isBoolean() {
|
|
26
|
-
return this.
|
|
29
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Boolean.atom(0));
|
|
27
30
|
}
|
|
28
31
|
get isTimestamp() {
|
|
29
|
-
return this.
|
|
32
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Timestamp.atom(0));
|
|
30
33
|
}
|
|
31
34
|
get isFloat() {
|
|
32
|
-
return this.
|
|
35
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Float.atom(0));
|
|
33
36
|
}
|
|
34
37
|
get isInteger() {
|
|
35
|
-
return this.
|
|
38
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Integer.atom(0));
|
|
36
39
|
}
|
|
37
40
|
get isBuffer() {
|
|
38
|
-
return this.
|
|
41
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.Buffer.atom(0));
|
|
39
42
|
}
|
|
40
43
|
get isString() {
|
|
41
|
-
return this.
|
|
44
|
+
return this._atoms?.length === 1 && this._atoms[0].equals(Type.String.atom(0));
|
|
42
45
|
}
|
|
43
46
|
get isArray() {
|
|
44
|
-
return this._atoms
|
|
47
|
+
return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom));
|
|
45
48
|
}
|
|
46
49
|
get isObject() {
|
|
47
|
-
return this._atoms
|
|
50
|
+
return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom));
|
|
48
51
|
}
|
|
49
52
|
get isFunction() {
|
|
50
|
-
return this._atoms
|
|
53
|
+
return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom));
|
|
51
54
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
get isOptional() {
|
|
56
|
+
return Boolean(this._atoms?.length === 0 || this._atoms?.some((a) => a.equals(Type.Null.atom(0))));
|
|
57
|
+
}
|
|
58
|
+
get isUniformArray() {
|
|
59
|
+
return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom && a.isUniform));
|
|
60
|
+
}
|
|
61
|
+
get isUniformObject() {
|
|
62
|
+
return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom && a.isUniform));
|
|
63
|
+
}
|
|
64
|
+
get isVariadicFunction() {
|
|
65
|
+
return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom && a.isVariadic));
|
|
66
|
+
}
|
|
67
|
+
get isVolatileFunction() {
|
|
68
|
+
return Boolean(this._atoms?.some((a) => a instanceof FunctionAtom_js_1.FunctionAtom && a.isVolatile));
|
|
69
|
+
}
|
|
70
|
+
equals(type) {
|
|
71
|
+
if (this !== type) {
|
|
72
|
+
if (!type || this.isNever !== type.isNever || this._atoms?.length !== type._atoms?.length) {
|
|
60
73
|
return false;
|
|
61
74
|
}
|
|
62
|
-
unmatched.
|
|
75
|
+
const unmatched = [...type._atoms ?? []];
|
|
76
|
+
for (const atom of this._atoms ?? []) {
|
|
77
|
+
const index = unmatched.findIndex((a) => atom.equals(a));
|
|
78
|
+
if (index < 0) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
unmatched.splice(index, 1);
|
|
82
|
+
}
|
|
63
83
|
}
|
|
64
84
|
return true;
|
|
65
85
|
}
|
|
66
|
-
|
|
86
|
+
intersects(type) {
|
|
67
87
|
if (type instanceof Type) {
|
|
68
|
-
if (this.
|
|
88
|
+
if (this.isNever || type.isNever) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
return Boolean(this.isAny || type.isAny || this._atoms?.some((a) => type._atoms?.some((atom) => a.intersects(atom))));
|
|
92
|
+
}
|
|
93
|
+
return Boolean(this.isAny || this._atoms?.some((atom) => atom.intersects(type)));
|
|
94
|
+
}
|
|
95
|
+
accepts(type) {
|
|
96
|
+
if (type instanceof Type) {
|
|
97
|
+
if (this.isNever || type.isNever) {
|
|
98
|
+
return type.isNever;
|
|
99
|
+
}
|
|
100
|
+
if (this.isAny || type.isAny) {
|
|
69
101
|
return true;
|
|
70
102
|
}
|
|
71
|
-
return type._atoms
|
|
103
|
+
return Boolean(type._atoms?.every((a) => this._atoms?.some((atom) => atom.accepts(a))));
|
|
72
104
|
}
|
|
73
|
-
return this._atoms
|
|
105
|
+
return Boolean(this._atoms?.some((a) => a.accepts(type)));
|
|
74
106
|
}
|
|
75
|
-
|
|
76
|
-
if (this.
|
|
107
|
+
acceptsValue(value) {
|
|
108
|
+
if (this.isAny) {
|
|
77
109
|
return true;
|
|
78
110
|
}
|
|
79
|
-
const valueAtom = Type.of(value)._atoms[0];
|
|
80
|
-
return this._atoms
|
|
81
|
-
|
|
111
|
+
const valueAtom = Type.of(value)._atoms?.[0];
|
|
112
|
+
return Boolean(valueAtom && this._atoms?.some((atom) => {
|
|
113
|
+
const matches = atom instanceof ArrayAtom_js_1.ArrayAtom
|
|
114
|
+
? valueAtom instanceof ArrayAtom_js_1.ArrayAtom
|
|
115
|
+
: atom instanceof ObjectAtom_js_1.ObjectAtom
|
|
116
|
+
? valueAtom instanceof ObjectAtom_js_1.ObjectAtom
|
|
117
|
+
: atom.accepts(valueAtom);
|
|
118
|
+
if (!matches) {
|
|
82
119
|
return false;
|
|
83
120
|
}
|
|
121
|
+
if ((atom instanceof ArrayAtom_js_1.ArrayAtom || atom instanceof ObjectAtom_js_1.ObjectAtom) && atom.valType(undefined)?.isAny) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
84
124
|
if (atom instanceof ArrayAtom_js_1.ArrayAtom) {
|
|
85
125
|
const values = value;
|
|
86
126
|
const types = atom.valTypes();
|
|
87
127
|
if (!types.length) {
|
|
88
128
|
return true;
|
|
89
129
|
}
|
|
90
|
-
if (atom.
|
|
91
|
-
return types.every((type, ix) => type.
|
|
130
|
+
if (!atom.isUniform) {
|
|
131
|
+
return types.every((type, ix) => type.acceptsValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
|
|
92
132
|
}
|
|
93
133
|
for (let i = 0; i < values.length; ++i) {
|
|
94
|
-
if (!types[0].
|
|
134
|
+
if (!types[0].acceptsValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
|
|
95
135
|
return false;
|
|
96
136
|
}
|
|
97
137
|
}
|
|
@@ -99,93 +139,143 @@ class Type {
|
|
|
99
139
|
}
|
|
100
140
|
if (atom instanceof ObjectAtom_js_1.ObjectAtom) {
|
|
101
141
|
const values = value;
|
|
102
|
-
return atom.valTypes().every(([key, type]) =>
|
|
142
|
+
return atom.valTypes().every(([key, type]) => key != null
|
|
143
|
+
? type.acceptsValue(Object.hasOwn(values, key) ? values[key] : undefined)
|
|
144
|
+
: Object.values(values).every((v) => type.acceptsValue(v)));
|
|
103
145
|
}
|
|
104
146
|
return true;
|
|
105
|
-
});
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
hasFunction() {
|
|
150
|
+
return Boolean(this._atoms?.some((atom) => atom instanceof FunctionAtom_js_1.FunctionAtom ||
|
|
151
|
+
atom instanceof ArrayAtom_js_1.ArrayAtom && atom.valTypes().some((type) => type.hasFunction()) ||
|
|
152
|
+
atom instanceof ObjectAtom_js_1.ObjectAtom && atom.valTypes().some(([, type]) => type.hasFunction())));
|
|
106
153
|
}
|
|
107
|
-
reduce(
|
|
108
|
-
if (
|
|
154
|
+
reduce(type) {
|
|
155
|
+
if (this.isNever || type.isNever) {
|
|
156
|
+
return Type.Never;
|
|
157
|
+
}
|
|
158
|
+
if (type.isAny || type.isNull) {
|
|
109
159
|
return this;
|
|
110
160
|
}
|
|
111
|
-
if (this.
|
|
112
|
-
return
|
|
161
|
+
if (this.isAny) {
|
|
162
|
+
return type;
|
|
113
163
|
}
|
|
114
|
-
const list = this._atoms
|
|
115
|
-
return list
|
|
164
|
+
const list = this._atoms?.filter((a) => type.accepts(a));
|
|
165
|
+
return list?.length === 0
|
|
116
166
|
? undefined
|
|
117
|
-
: list
|
|
167
|
+
: list?.length === this._atoms?.length
|
|
118
168
|
? this
|
|
119
169
|
: new Type(list);
|
|
120
170
|
}
|
|
121
|
-
intersect(type) {
|
|
122
|
-
if (this.isUnknown || type.isUnknown) {
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
|
|
126
|
-
}
|
|
127
171
|
weight() {
|
|
128
|
-
return this._atoms
|
|
172
|
+
return this._atoms?.reduce((acc, a) => acc + a.weight(), 0) ?? 0;
|
|
129
173
|
}
|
|
130
174
|
arrayAtomValType(index) {
|
|
131
|
-
const atoms = this.
|
|
175
|
+
const atoms = this.arrayAtoms();
|
|
132
176
|
if (!atoms.length) {
|
|
133
|
-
return Type.
|
|
177
|
+
return Type.Any;
|
|
134
178
|
}
|
|
135
179
|
const valTypes = [];
|
|
136
180
|
for (const atom of atoms) {
|
|
137
|
-
const valType = atom.
|
|
138
|
-
? atom.valType(index)
|
|
139
|
-
: atom.valType(0);
|
|
181
|
+
const valType = atom.valType(index);
|
|
140
182
|
if (!valType) {
|
|
141
|
-
return Type.
|
|
183
|
+
return Type.Any;
|
|
142
184
|
}
|
|
143
185
|
valTypes.push(valType);
|
|
144
186
|
}
|
|
145
187
|
return Type.union(...valTypes);
|
|
146
188
|
}
|
|
147
|
-
|
|
148
|
-
const atoms = this.
|
|
149
|
-
|
|
189
|
+
objectAtomValType(key) {
|
|
190
|
+
const atoms = this.objectAtoms();
|
|
191
|
+
if (!atoms.length) {
|
|
192
|
+
return Type.Any;
|
|
193
|
+
}
|
|
194
|
+
const valTypes = [];
|
|
195
|
+
for (const atom of atoms) {
|
|
196
|
+
const valType = atom.valType(key);
|
|
197
|
+
if (!valType) {
|
|
198
|
+
return Type.Any;
|
|
199
|
+
}
|
|
200
|
+
valTypes.push(valType);
|
|
201
|
+
}
|
|
202
|
+
return Type.union(...valTypes);
|
|
150
203
|
}
|
|
151
|
-
|
|
152
|
-
|
|
204
|
+
functionAtomRetType() {
|
|
205
|
+
const types = this.functionAtoms().map((atom) => atom.retType());
|
|
206
|
+
return types.length ? Type.union(...types) : undefined;
|
|
207
|
+
}
|
|
208
|
+
functionAtomArgType(index) {
|
|
209
|
+
const types = this.functionAtoms().map((atom) => atom.argType(index)).filter((type) => type !== undefined);
|
|
210
|
+
return types.length ? Type.union(...types) : undefined;
|
|
211
|
+
}
|
|
212
|
+
arrayAtoms() {
|
|
213
|
+
if (this.isAny) {
|
|
214
|
+
return Type.Array._atoms;
|
|
215
|
+
}
|
|
216
|
+
return this._atoms?.filter((a) => a instanceof ArrayAtom_js_1.ArrayAtom) ?? [];
|
|
217
|
+
}
|
|
218
|
+
objectAtoms() {
|
|
219
|
+
if (this.isAny) {
|
|
220
|
+
return Type.Object._atoms;
|
|
221
|
+
}
|
|
222
|
+
return this._atoms?.filter((a) => a instanceof ObjectAtom_js_1.ObjectAtom) ?? [];
|
|
223
|
+
}
|
|
224
|
+
functionAtoms() {
|
|
225
|
+
if (this.isAny || this.isNever) {
|
|
153
226
|
return Type.Function._atoms;
|
|
154
227
|
}
|
|
155
|
-
return this._atoms
|
|
228
|
+
return this._atoms?.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom) ?? [];
|
|
229
|
+
}
|
|
230
|
+
functionSignatures() {
|
|
231
|
+
return this.functionAtoms().flatMap((i) => i.signatures()).map((i) => [i.retType, i.argTypes, i.isVariadic]);
|
|
232
|
+
}
|
|
233
|
+
optionalFirstParameter() {
|
|
234
|
+
return this.isNever
|
|
235
|
+
? this
|
|
236
|
+
: Type.union(...this.functionAtoms().map((atom) => new Type([atom.firstArgOptional()])));
|
|
156
237
|
}
|
|
157
238
|
toOptional() {
|
|
158
|
-
return
|
|
239
|
+
return Type.union(Type.Null, this);
|
|
159
240
|
}
|
|
160
241
|
toMandatory() {
|
|
161
|
-
if (!this.isOptional || this.
|
|
242
|
+
if (!this._atoms?.length || !this.isOptional || this.isNull) {
|
|
162
243
|
return this;
|
|
163
244
|
}
|
|
164
|
-
return new Type(this._atoms.filter((a) => !a.
|
|
245
|
+
return new Type(this._atoms.filter((a) => !a.equals(Type.Null.atom(0))));
|
|
165
246
|
}
|
|
166
247
|
toString() {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
248
|
+
if (this._atoms == null) {
|
|
249
|
+
return "never";
|
|
250
|
+
}
|
|
251
|
+
if (this._atoms.length === 0) {
|
|
252
|
+
return "any";
|
|
253
|
+
}
|
|
254
|
+
return [...this._atoms]
|
|
255
|
+
.sort((a, b) => a.weight() - b.weight())
|
|
256
|
+
.map((a) => a.toString())
|
|
257
|
+
.join("|");
|
|
173
258
|
}
|
|
174
259
|
static union(...types) {
|
|
175
|
-
if (types.some((t) => t.
|
|
176
|
-
return Type.
|
|
260
|
+
if (types.some((t) => t.isAny)) {
|
|
261
|
+
return Type.Any;
|
|
262
|
+
}
|
|
263
|
+
if (types.length && types.every((t) => t.isNever)) {
|
|
264
|
+
return Type.Never;
|
|
177
265
|
}
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
266
|
+
const atoms = [];
|
|
267
|
+
for (const type of types) {
|
|
268
|
+
for (const atom of type._atoms ?? []) {
|
|
269
|
+
if (!atoms.some((a) => a.equals(atom))) {
|
|
270
|
+
atoms.push(atom);
|
|
271
|
+
}
|
|
182
272
|
}
|
|
183
|
-
}
|
|
184
|
-
return
|
|
273
|
+
}
|
|
274
|
+
return new Type(atoms);
|
|
185
275
|
}
|
|
186
276
|
static of(value) {
|
|
187
277
|
return value == null
|
|
188
|
-
? Type.
|
|
278
|
+
? Type.Null
|
|
189
279
|
: typeof value === "boolean"
|
|
190
280
|
? Type.Boolean
|
|
191
281
|
: value instanceof Date
|
|
@@ -205,53 +295,59 @@ class Type {
|
|
|
205
295
|
: Type.Function;
|
|
206
296
|
}
|
|
207
297
|
static isPrimitive(value) {
|
|
208
|
-
return value == null
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
298
|
+
return value == null ||
|
|
299
|
+
typeof value === "boolean" ||
|
|
300
|
+
value instanceof Date ||
|
|
301
|
+
typeof value === "number" ||
|
|
302
|
+
typeof value === "bigint" ||
|
|
303
|
+
value instanceof ArrayBuffer ||
|
|
304
|
+
typeof value === "string";
|
|
305
|
+
}
|
|
306
|
+
static isFunction(value) {
|
|
307
|
+
return typeof value === "function";
|
|
215
308
|
}
|
|
216
309
|
static primitiveType(primitive) {
|
|
217
310
|
return new Type([new PrimitiveAtom_js_1.PrimitiveAtom(primitive)]);
|
|
218
311
|
}
|
|
219
|
-
static arrayType(
|
|
220
|
-
return new Type([new ArrayAtom_js_1.ArrayAtom(
|
|
312
|
+
static arrayType(types = Type.Any) {
|
|
313
|
+
return new Type([new ArrayAtom_js_1.ArrayAtom(types)]);
|
|
221
314
|
}
|
|
222
|
-
static objectType(
|
|
223
|
-
return new Type([new ObjectAtom_js_1.ObjectAtom(
|
|
315
|
+
static objectType(types = Type.Any) {
|
|
316
|
+
return new Type([new ObjectAtom_js_1.ObjectAtom(types)]);
|
|
224
317
|
}
|
|
225
|
-
static functionType(
|
|
226
|
-
return new Type([new FunctionAtom_js_1.FunctionAtom(
|
|
318
|
+
static functionType(signatures = [[Type.Any, [], true]], volatile) {
|
|
319
|
+
return new Type([new FunctionAtom_js_1.FunctionAtom(signatures, volatile)]);
|
|
227
320
|
}
|
|
228
|
-
static
|
|
229
|
-
static
|
|
321
|
+
static Never = new Type();
|
|
322
|
+
static Any = new Type([]);
|
|
323
|
+
static Null = Type.primitiveType("null");
|
|
230
324
|
static Boolean = Type.primitiveType("boolean");
|
|
231
|
-
static OptionalBoolean = Type.union(Type.
|
|
325
|
+
static OptionalBoolean = Type.union(Type.Null, Type.Boolean);
|
|
232
326
|
static Timestamp = Type.primitiveType("timestamp");
|
|
233
|
-
static OptionalTimestamp = Type.union(Type.
|
|
327
|
+
static OptionalTimestamp = Type.union(Type.Null, Type.Timestamp);
|
|
234
328
|
static Float = Type.primitiveType("float");
|
|
235
|
-
static OptionalFloat = Type.union(Type.
|
|
329
|
+
static OptionalFloat = Type.union(Type.Null, Type.Float);
|
|
236
330
|
static Integer = Type.primitiveType("integer");
|
|
237
|
-
static OptionalInteger = Type.union(Type.
|
|
331
|
+
static OptionalInteger = Type.union(Type.Null, Type.Integer);
|
|
238
332
|
static Buffer = Type.primitiveType("buffer");
|
|
239
|
-
static OptionalBuffer = Type.union(Type.
|
|
333
|
+
static OptionalBuffer = Type.union(Type.Null, Type.Buffer);
|
|
240
334
|
static String = Type.primitiveType("string");
|
|
241
|
-
static OptionalString = Type.union(Type.
|
|
335
|
+
static OptionalString = Type.union(Type.Null, Type.String);
|
|
242
336
|
static Array = Type.arrayType();
|
|
243
|
-
static OptionalArray = Type.union(Type.
|
|
337
|
+
static OptionalArray = Type.union(Type.Null, Type.Array);
|
|
244
338
|
static Object = Type.objectType();
|
|
245
|
-
static OptionalObject = Type.union(Type.
|
|
246
|
-
static Function = Type.functionType(
|
|
247
|
-
static OptionalFunction = Type.union(Type.
|
|
339
|
+
static OptionalObject = Type.union(Type.Null, Type.Object);
|
|
340
|
+
static Function = Type.functionType();
|
|
341
|
+
static OptionalFunction = Type.union(Type.Null, Type.Function);
|
|
248
342
|
static Reference = Type.union(Type.Array, Type.Object, Type.Function);
|
|
249
|
-
static OptionalReference = Type.union(Type.
|
|
343
|
+
static OptionalReference = Type.union(Type.Null, Type.Array, Type.Object, Type.Function);
|
|
250
344
|
static Number = Type.union(Type.Float, Type.Integer);
|
|
251
|
-
static OptionalNumber = Type.union(Type.
|
|
345
|
+
static OptionalNumber = Type.union(Type.Null, Type.Float, Type.Integer);
|
|
346
|
+
static Composite = Type.union(Type.Array, Type.Object);
|
|
347
|
+
static OptionalComposite = Type.union(Type.Null, Type.Array, Type.Object);
|
|
252
348
|
static Enumerable = Type.union(Type.Buffer, Type.String, Type.Array);
|
|
253
|
-
static OptionalEnumerable = Type.union(Type.
|
|
349
|
+
static OptionalEnumerable = Type.union(Type.Null, Type.Buffer, Type.String, Type.Array);
|
|
254
350
|
static Iterable = Type.union(Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
255
|
-
static OptionalIterable = Type.union(Type.
|
|
351
|
+
static OptionalIterable = Type.union(Type.Null, Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
256
352
|
}
|
|
257
353
|
exports.Type = Type;
|
|
@@ -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
|
+
}
|