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.
Files changed (243) hide show
  1. package/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
  2. package/.cache/function-syntax-tests.log +20 -0
  3. package/README.md +925 -355
  4. package/dst/Affinirum.d.ts +29 -19
  5. package/dst/Affinirum.js +399 -334
  6. package/dst/CallFrame.d.ts +12 -3
  7. package/dst/CallFrame.js +49 -10
  8. package/dst/Constant.d.ts +1 -3
  9. package/dst/Constant.js +3 -8
  10. package/dst/LexicalScope.d.ts +11 -9
  11. package/dst/LexicalScope.js +10 -6
  12. package/dst/ParserSpan.d.ts +11 -0
  13. package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
  14. package/dst/ParserState.d.ts +28 -49
  15. package/dst/ParserState.js +256 -489
  16. package/dst/Type.d.ts +34 -17
  17. package/dst/Type.js +211 -115
  18. package/dst/TypeParser.d.ts +9 -0
  19. package/dst/TypeParser.js +127 -0
  20. package/dst/Value.d.ts +1 -1
  21. package/dst/Value.js +21 -10
  22. package/dst/Variable.d.ts +16 -5
  23. package/dst/Variable.js +32 -7
  24. package/dst/atom/ArrayAtom.d.ts +10 -10
  25. package/dst/atom/ArrayAtom.js +62 -55
  26. package/dst/atom/Atom.d.ts +7 -0
  27. package/dst/atom/FunctionAtom.d.ts +13 -15
  28. package/dst/atom/FunctionAtom.js +53 -104
  29. package/dst/atom/FunctionSignature.d.ts +23 -0
  30. package/dst/atom/FunctionSignature.js +124 -0
  31. package/dst/atom/ObjectAtom.d.ts +11 -9
  32. package/dst/atom/ObjectAtom.js +73 -33
  33. package/dst/atom/PrimitiveAtom.d.ts +6 -7
  34. package/dst/atom/PrimitiveAtom.js +7 -10
  35. package/dst/cjs/Affinirum.d.ts +29 -19
  36. package/dst/cjs/Affinirum.js +398 -333
  37. package/dst/cjs/CallFrame.d.ts +12 -3
  38. package/dst/cjs/CallFrame.js +49 -10
  39. package/dst/cjs/Constant.d.ts +1 -3
  40. package/dst/cjs/Constant.js +3 -8
  41. package/dst/cjs/LexicalScope.d.ts +11 -9
  42. package/dst/cjs/LexicalScope.js +10 -6
  43. package/dst/cjs/ParserSpan.d.ts +11 -0
  44. package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
  45. package/dst/cjs/ParserState.d.ts +28 -49
  46. package/dst/cjs/ParserState.js +255 -488
  47. package/dst/cjs/Type.d.ts +34 -17
  48. package/dst/cjs/Type.js +211 -115
  49. package/dst/cjs/TypeParser.d.ts +9 -0
  50. package/dst/cjs/TypeParser.js +131 -0
  51. package/dst/cjs/Value.d.ts +1 -1
  52. package/dst/cjs/Value.js +21 -10
  53. package/dst/cjs/Variable.d.ts +16 -5
  54. package/dst/cjs/Variable.js +36 -9
  55. package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
  56. package/dst/cjs/atom/ArrayAtom.js +62 -55
  57. package/dst/cjs/atom/Atom.d.ts +7 -0
  58. package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
  59. package/dst/cjs/atom/FunctionAtom.js +54 -105
  60. package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
  61. package/dst/cjs/atom/FunctionSignature.js +128 -0
  62. package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
  63. package/dst/cjs/atom/ObjectAtom.js +73 -33
  64. package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
  65. package/dst/cjs/atom/PrimitiveAtom.js +7 -10
  66. package/dst/cjs/dictionary/Constants.d.ts +2 -0
  67. package/dst/cjs/dictionary/Constants.js +27 -0
  68. package/dst/cjs/dictionary/Functions.d.ts +2 -0
  69. package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
  70. package/dst/cjs/native/Aggregable.js +10 -2
  71. package/dst/cjs/native/Array.d.ts +2 -3
  72. package/dst/cjs/native/Array.js +38 -39
  73. package/dst/cjs/native/Boolean.d.ts +1 -1
  74. package/dst/cjs/native/Boolean.js +13 -10
  75. package/dst/cjs/native/Buffer.d.ts +5 -5
  76. package/dst/cjs/native/Buffer.js +70 -46
  77. package/dst/cjs/native/Composite.d.ts +5 -0
  78. package/dst/cjs/native/Composite.js +35 -0
  79. package/dst/cjs/native/Enumerable.js +20 -8
  80. package/dst/cjs/native/Float.d.ts +3 -1
  81. package/dst/cjs/native/Float.js +51 -12
  82. package/dst/cjs/native/Integer.d.ts +4 -2
  83. package/dst/cjs/native/Integer.js +60 -25
  84. package/dst/cjs/native/Iterable.d.ts +0 -2
  85. package/dst/cjs/native/Iterable.js +9 -28
  86. package/dst/cjs/native/Number.d.ts +0 -2
  87. package/dst/cjs/native/Number.js +34 -48
  88. package/dst/cjs/native/Object.d.ts +1 -1
  89. package/dst/cjs/native/Object.js +5 -6
  90. package/dst/cjs/native/String.d.ts +7 -6
  91. package/dst/cjs/native/String.js +28 -27
  92. package/dst/cjs/native/Timestamp.d.ts +3 -3
  93. package/dst/cjs/native/Timestamp.js +34 -36
  94. package/dst/cjs/native/Unknown.d.ts +3 -3
  95. package/dst/cjs/native/Unknown.js +48 -17
  96. package/dst/cjs/native/notation/AN.d.ts +1 -1
  97. package/dst/cjs/native/notation/AN.js +37 -35
  98. package/dst/cjs/native/notation/IP.d.ts +1 -1
  99. package/dst/cjs/native/notation/IP.js +11 -12
  100. package/dst/cjs/native/notation/JSON.d.ts +1 -1
  101. package/dst/cjs/native/notation/JSON.js +7 -6
  102. package/dst/cjs/node/AccessNode.d.ts +24 -0
  103. package/dst/cjs/node/AccessNode.js +193 -0
  104. package/dst/cjs/node/ArrayNode.d.ts +7 -4
  105. package/dst/cjs/node/ArrayNode.js +26 -15
  106. package/dst/cjs/node/AssignableNode.d.ts +16 -0
  107. package/dst/cjs/node/AssignableNode.js +18 -0
  108. package/dst/cjs/node/AssignmentNode.d.ts +20 -0
  109. package/dst/cjs/node/AssignmentNode.js +66 -0
  110. package/dst/cjs/node/BlockNode.d.ts +5 -4
  111. package/dst/cjs/node/BlockNode.js +18 -11
  112. package/dst/cjs/node/CallNode.d.ts +7 -5
  113. package/dst/cjs/node/CallNode.js +40 -41
  114. package/dst/cjs/node/CastNode.d.ts +5 -6
  115. package/dst/cjs/node/CastNode.js +16 -17
  116. package/dst/cjs/node/ConstantNode.d.ts +4 -6
  117. package/dst/cjs/node/ConstantNode.js +9 -17
  118. package/dst/cjs/node/FinalNode.d.ts +19 -0
  119. package/dst/cjs/node/FinalNode.js +41 -0
  120. package/dst/cjs/node/FunctionNode.d.ts +17 -0
  121. package/dst/cjs/node/FunctionNode.js +51 -0
  122. package/dst/cjs/node/InvocationNode.d.ts +5 -0
  123. package/dst/cjs/node/InvocationNode.js +34 -0
  124. package/dst/cjs/node/JumpNode.d.ts +7 -9
  125. package/dst/cjs/node/JumpNode.js +12 -19
  126. package/dst/cjs/node/LoopNode.d.ts +7 -5
  127. package/dst/cjs/node/LoopNode.js +33 -22
  128. package/dst/cjs/node/MatchNode.d.ts +17 -0
  129. package/dst/cjs/node/MatchNode.js +65 -0
  130. package/dst/cjs/node/Node.d.ts +14 -0
  131. package/dst/cjs/{Node.js → node/Node.js} +17 -11
  132. package/dst/cjs/node/ObjectNode.d.ts +7 -4
  133. package/dst/cjs/node/ObjectNode.js +42 -11
  134. package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  135. package/dst/cjs/node/TernaryNode.js +45 -0
  136. package/dst/cjs/node/VariableNode.d.ts +10 -6
  137. package/dst/cjs/node/VariableNode.js +50 -16
  138. package/dst/dictionary/Constants.d.ts +2 -0
  139. package/dst/dictionary/Constants.js +24 -0
  140. package/dst/dictionary/Functions.d.ts +2 -0
  141. package/dst/{Functions.js → dictionary/Functions.js} +26 -24
  142. package/dst/native/Aggregable.js +11 -3
  143. package/dst/native/Array.d.ts +2 -3
  144. package/dst/native/Array.js +37 -38
  145. package/dst/native/Boolean.d.ts +1 -1
  146. package/dst/native/Boolean.js +12 -9
  147. package/dst/native/Buffer.d.ts +5 -5
  148. package/dst/native/Buffer.js +66 -42
  149. package/dst/native/Composite.d.ts +5 -0
  150. package/dst/native/Composite.js +32 -0
  151. package/dst/native/Enumerable.js +20 -8
  152. package/dst/native/Float.d.ts +3 -1
  153. package/dst/native/Float.js +48 -11
  154. package/dst/native/Integer.d.ts +4 -2
  155. package/dst/native/Integer.js +56 -23
  156. package/dst/native/Iterable.d.ts +0 -2
  157. package/dst/native/Iterable.js +8 -27
  158. package/dst/native/Number.d.ts +0 -2
  159. package/dst/native/Number.js +34 -46
  160. package/dst/native/Object.d.ts +1 -1
  161. package/dst/native/Object.js +4 -5
  162. package/dst/native/String.d.ts +7 -6
  163. package/dst/native/String.js +24 -24
  164. package/dst/native/Timestamp.d.ts +3 -3
  165. package/dst/native/Timestamp.js +31 -33
  166. package/dst/native/Unknown.d.ts +3 -3
  167. package/dst/native/Unknown.js +49 -18
  168. package/dst/native/notation/AN.d.ts +1 -1
  169. package/dst/native/notation/AN.js +37 -35
  170. package/dst/native/notation/IP.d.ts +1 -1
  171. package/dst/native/notation/IP.js +10 -11
  172. package/dst/native/notation/JSON.d.ts +1 -1
  173. package/dst/native/notation/JSON.js +6 -5
  174. package/dst/node/AccessNode.d.ts +24 -0
  175. package/dst/node/AccessNode.js +189 -0
  176. package/dst/node/ArrayNode.d.ts +7 -4
  177. package/dst/node/ArrayNode.js +26 -15
  178. package/dst/node/AssignableNode.d.ts +16 -0
  179. package/dst/node/AssignableNode.js +14 -0
  180. package/dst/node/AssignmentNode.d.ts +20 -0
  181. package/dst/node/AssignmentNode.js +62 -0
  182. package/dst/node/BlockNode.d.ts +5 -4
  183. package/dst/node/BlockNode.js +18 -11
  184. package/dst/node/CallNode.d.ts +7 -5
  185. package/dst/node/CallNode.js +40 -41
  186. package/dst/node/CastNode.d.ts +5 -6
  187. package/dst/node/CastNode.js +16 -17
  188. package/dst/node/ConstantNode.d.ts +4 -6
  189. package/dst/node/ConstantNode.js +9 -17
  190. package/dst/node/FinalNode.d.ts +19 -0
  191. package/dst/node/FinalNode.js +37 -0
  192. package/dst/node/FunctionNode.d.ts +17 -0
  193. package/dst/node/FunctionNode.js +47 -0
  194. package/dst/node/InvocationNode.d.ts +5 -0
  195. package/dst/node/InvocationNode.js +30 -0
  196. package/dst/node/JumpNode.d.ts +7 -9
  197. package/dst/node/JumpNode.js +12 -19
  198. package/dst/node/LoopNode.d.ts +7 -5
  199. package/dst/node/LoopNode.js +33 -22
  200. package/dst/node/MatchNode.d.ts +17 -0
  201. package/dst/node/MatchNode.js +61 -0
  202. package/dst/node/Node.d.ts +14 -0
  203. package/dst/node/Node.js +28 -0
  204. package/dst/node/ObjectNode.d.ts +7 -4
  205. package/dst/node/ObjectNode.js +42 -11
  206. package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  207. package/dst/node/TernaryNode.js +41 -0
  208. package/dst/node/VariableNode.d.ts +10 -6
  209. package/dst/node/VariableNode.js +50 -16
  210. package/package.json +4 -4
  211. package/dst/Atom.d.ts +0 -7
  212. package/dst/Constants.d.ts +0 -2
  213. package/dst/Constants.js +0 -24
  214. package/dst/Functions.d.ts +0 -2
  215. package/dst/Jump.d.ts +0 -1
  216. package/dst/Jump.js +0 -1
  217. package/dst/JumpException.d.ts +0 -9
  218. package/dst/JumpException.js +0 -14
  219. package/dst/Keywords.d.ts +0 -1
  220. package/dst/Keywords.js +0 -17
  221. package/dst/Node.d.ts +0 -14
  222. package/dst/Node.js +0 -22
  223. package/dst/ScriptSpan.d.ts +0 -10
  224. package/dst/cjs/Atom.d.ts +0 -7
  225. package/dst/cjs/Constants.d.ts +0 -2
  226. package/dst/cjs/Constants.js +0 -27
  227. package/dst/cjs/Functions.d.ts +0 -2
  228. package/dst/cjs/Jump.d.ts +0 -1
  229. package/dst/cjs/Jump.js +0 -2
  230. package/dst/cjs/JumpException.d.ts +0 -9
  231. package/dst/cjs/JumpException.js +0 -18
  232. package/dst/cjs/Keywords.d.ts +0 -1
  233. package/dst/cjs/Keywords.js +0 -20
  234. package/dst/cjs/Node.d.ts +0 -14
  235. package/dst/cjs/ScriptSpan.d.ts +0 -10
  236. package/dst/cjs/node/SwitchNode.js +0 -49
  237. package/dst/cjs/node/VariadicNode.d.ts +0 -4
  238. package/dst/cjs/node/VariadicNode.js +0 -10
  239. package/dst/node/SwitchNode.js +0 -45
  240. package/dst/node/VariadicNode.d.ts +0 -4
  241. package/dst/node/VariadicNode.js +0 -6
  242. /package/dst/{Atom.js → atom/Atom.js} +0 -0
  243. /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
- get isUnknown(): boolean;
10
+ private atom;
11
+ get isNever(): boolean;
12
+ get isAny(): boolean;
11
13
  get isAtom(): boolean;
12
- get isOptional(): boolean;
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
- equate(type: Type | undefined): boolean;
24
- accept(type: Type | Atom): boolean;
25
- acceptValue(value: Value): boolean;
26
- reduce(mask: Type): Type | undefined;
27
- intersect(type: Type): boolean;
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: number): Type;
30
- functionAtomRetType(type: Type): Type;
31
- functionAtoms(type: Type, argc: number): FunctionAtom[];
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(itemTypes?: Type[]): Type;
40
- static objectType(propTypes?: [string, Type][]): Type;
41
- static functionType(retType?: Type, argTypes?: Type[], isVariadic?: boolean): Type;
42
- static readonly Unknown: Type;
43
- static readonly Void: Type;
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(_atoms = []) {
11
- this._atoms = _atoms;
10
+ constructor(atoms) {
11
+ this._atoms = atoms;
12
12
  }
13
- get isUnknown() {
14
- return this._atoms.length === 0;
13
+ atom(ix) {
14
+ return (this._atoms ?? [])[ix];
15
15
  }
16
- get isAtom() {
17
- return this._atoms.length === 1;
16
+ get isNever() {
17
+ return !this._atoms;
18
18
  }
19
- get isOptional() {
20
- return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
19
+ get isAny() {
20
+ return this._atoms?.length === 0;
21
+ }
22
+ get isAtom() {
23
+ return this._atoms?.length === 1;
21
24
  }
22
- get isVoid() {
23
- return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
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.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
29
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Boolean.atom(0));
27
30
  }
28
31
  get isTimestamp() {
29
- return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
32
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Timestamp.atom(0));
30
33
  }
31
34
  get isFloat() {
32
- return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
35
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Float.atom(0));
33
36
  }
34
37
  get isInteger() {
35
- return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
38
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Integer.atom(0));
36
39
  }
37
40
  get isBuffer() {
38
- return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
41
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Buffer.atom(0));
39
42
  }
40
43
  get isString() {
41
- return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
44
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.String.atom(0));
42
45
  }
43
46
  get isArray() {
44
- return this._atoms.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
47
+ return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom));
45
48
  }
46
49
  get isObject() {
47
- return this._atoms.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom);
50
+ return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom));
48
51
  }
49
52
  get isFunction() {
50
- return this._atoms.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom);
53
+ return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom));
51
54
  }
52
- equate(type) {
53
- if (this._atoms.length !== type?._atoms.length) {
54
- return false;
55
- }
56
- const unmatched = [...type._atoms];
57
- for (const atom of this._atoms) {
58
- const index = unmatched.findIndex((a) => atom.equate(a));
59
- if (index < 0) {
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.splice(index, 1);
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
- accept(type) {
86
+ intersects(type) {
67
87
  if (type instanceof Type) {
68
- if (this.isUnknown || type.isUnknown) {
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.every((a) => this._atoms.some((atom) => atom.accept(a)));
103
+ return Boolean(type._atoms?.every((a) => this._atoms?.some((atom) => atom.accepts(a))));
72
104
  }
73
- return this._atoms.some((a) => a.accept(type));
105
+ return Boolean(this._atoms?.some((a) => a.accepts(type)));
74
106
  }
75
- acceptValue(value) {
76
- if (this.isUnknown) {
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.some((atom) => {
81
- if (!atom.accept(valueAtom)) {
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.isTuple) {
91
- return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
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].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
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]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
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(mask) {
108
- if (mask.isUnknown || mask.isVoid) {
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.isUnknown) {
112
- return mask;
161
+ if (this.isAny) {
162
+ return type;
113
163
  }
114
- const list = this._atoms.filter((a) => mask.accept(a));
115
- return list.length === 0
164
+ const list = this._atoms?.filter((a) => type.accepts(a));
165
+ return list?.length === 0
116
166
  ? undefined
117
- : list.length === this._atoms.length
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.reduce((acc, a) => acc + a.weight(), 0);
172
+ return this._atoms?.reduce((acc, a) => acc + a.weight(), 0) ?? 0;
129
173
  }
130
174
  arrayAtomValType(index) {
131
- const atoms = this._atoms.filter((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
175
+ const atoms = this.arrayAtoms();
132
176
  if (!atoms.length) {
133
- return Type.Unknown;
177
+ return Type.Any;
134
178
  }
135
179
  const valTypes = [];
136
180
  for (const atom of atoms) {
137
- const valType = atom.isTuple
138
- ? atom.valType(index)
139
- : atom.valType(0);
181
+ const valType = atom.valType(index);
140
182
  if (!valType) {
141
- return Type.Unknown;
183
+ return Type.Any;
142
184
  }
143
185
  valTypes.push(valType);
144
186
  }
145
187
  return Type.union(...valTypes);
146
188
  }
147
- functionAtomRetType(type) {
148
- const atoms = this._atoms.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && type.accept(a.retType));
149
- return Type.union(...atoms.map((a) => a.retType));
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
- functionAtoms(type, argc) {
152
- if (this.isUnknown) {
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.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
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 this._atoms.length ? Type.union(Type.Void, this) : this;
239
+ return Type.union(Type.Null, this);
159
240
  }
160
241
  toMandatory() {
161
- if (!this.isOptional || this.isUnknown) {
242
+ if (!this._atoms?.length || !this.isOptional || this.isNull) {
162
243
  return this;
163
244
  }
164
- return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
245
+ return new Type(this._atoms.filter((a) => !a.equals(Type.Null.atom(0))));
165
246
  }
166
247
  toString() {
167
- return this._atoms.length
168
- ? [...this._atoms]
169
- .sort((a, b) => a.weight() - b.weight())
170
- .map((a) => a.toString())
171
- .join("|")
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.isUnknown)) {
176
- return Type.Unknown;
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 type = new Type([]);
179
- types.flatMap((t) => t._atoms).forEach((a) => {
180
- if (!type._atoms.some((atom) => atom.equate(a))) {
181
- type._atoms.push(a);
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 type;
273
+ }
274
+ return new Type(atoms);
185
275
  }
186
276
  static of(value) {
187
277
  return value == null
188
- ? Type.Void
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
- || typeof value === "boolean"
210
- || value instanceof Date
211
- || typeof value === "number"
212
- || typeof value === "bigint"
213
- || value instanceof ArrayBuffer
214
- || typeof value === "string";
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(itemTypes = []) {
220
- return new Type([new ArrayAtom_js_1.ArrayAtom(itemTypes)]);
312
+ static arrayType(types = Type.Any) {
313
+ return new Type([new ArrayAtom_js_1.ArrayAtom(types)]);
221
314
  }
222
- static objectType(propTypes = []) {
223
- return new Type([new ObjectAtom_js_1.ObjectAtom(propTypes)]);
315
+ static objectType(types = Type.Any) {
316
+ return new Type([new ObjectAtom_js_1.ObjectAtom(types)]);
224
317
  }
225
- static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
226
- return new Type([new FunctionAtom_js_1.FunctionAtom(retType, argTypes, isVariadic)]);
318
+ static functionType(signatures = [[Type.Any, [], true]], volatile) {
319
+ return new Type([new FunctionAtom_js_1.FunctionAtom(signatures, volatile)]);
227
320
  }
228
- static Unknown = new Type();
229
- static Void = Type.primitiveType("void");
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.Void, Type.Boolean);
325
+ static OptionalBoolean = Type.union(Type.Null, Type.Boolean);
232
326
  static Timestamp = Type.primitiveType("timestamp");
233
- static OptionalTimestamp = Type.union(Type.Void, Type.Timestamp);
327
+ static OptionalTimestamp = Type.union(Type.Null, Type.Timestamp);
234
328
  static Float = Type.primitiveType("float");
235
- static OptionalFloat = Type.union(Type.Void, Type.Float);
329
+ static OptionalFloat = Type.union(Type.Null, Type.Float);
236
330
  static Integer = Type.primitiveType("integer");
237
- static OptionalInteger = Type.union(Type.Void, Type.Integer);
331
+ static OptionalInteger = Type.union(Type.Null, Type.Integer);
238
332
  static Buffer = Type.primitiveType("buffer");
239
- static OptionalBuffer = Type.union(Type.Void, Type.Buffer);
333
+ static OptionalBuffer = Type.union(Type.Null, Type.Buffer);
240
334
  static String = Type.primitiveType("string");
241
- static OptionalString = Type.union(Type.Void, Type.String);
335
+ static OptionalString = Type.union(Type.Null, Type.String);
242
336
  static Array = Type.arrayType();
243
- static OptionalArray = Type.union(Type.Void, Type.Array);
337
+ static OptionalArray = Type.union(Type.Null, Type.Array);
244
338
  static Object = Type.objectType();
245
- static OptionalObject = Type.union(Type.Void, Type.Object);
246
- static Function = Type.functionType(undefined, undefined, true);
247
- static OptionalFunction = Type.union(Type.Void, Type.Function);
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.Void, Type.Array, Type.Object, Type.Function);
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.Void, Type.Float, Type.Integer);
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.Void, Type.Buffer, Type.String, Type.Array);
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.Void, Type.Buffer, Type.String, Type.Array, Type.Object);
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
+ }