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/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/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(_atoms = []) {
8
- this._atoms = _atoms;
7
+ constructor(atoms) {
8
+ this._atoms = atoms;
9
9
  }
10
- get isUnknown() {
11
- return this._atoms.length === 0;
10
+ atom(ix) {
11
+ return (this._atoms ?? [])[ix];
12
12
  }
13
- get isAtom() {
14
- return this._atoms.length === 1;
13
+ get isNever() {
14
+ return !this._atoms;
15
15
  }
16
- get isOptional() {
17
- return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
16
+ get isAny() {
17
+ return this._atoms?.length === 0;
18
+ }
19
+ get isAtom() {
20
+ return this._atoms?.length === 1;
18
21
  }
19
- get isVoid() {
20
- return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
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.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
26
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Boolean.atom(0));
24
27
  }
25
28
  get isTimestamp() {
26
- return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
29
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Timestamp.atom(0));
27
30
  }
28
31
  get isFloat() {
29
- return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
32
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Float.atom(0));
30
33
  }
31
34
  get isInteger() {
32
- return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
35
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Integer.atom(0));
33
36
  }
34
37
  get isBuffer() {
35
- return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
38
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.Buffer.atom(0));
36
39
  }
37
40
  get isString() {
38
- return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
41
+ return this._atoms?.length === 1 && this._atoms[0].equals(Type.String.atom(0));
39
42
  }
40
43
  get isArray() {
41
- return this._atoms.every((a) => a instanceof ArrayAtom);
44
+ return Boolean(this._atoms?.every((a) => a instanceof ArrayAtom));
42
45
  }
43
46
  get isObject() {
44
- return this._atoms.every((a) => a instanceof ObjectAtom);
47
+ return Boolean(this._atoms?.every((a) => a instanceof ObjectAtom));
45
48
  }
46
49
  get isFunction() {
47
- return this._atoms.every((a) => a instanceof FunctionAtom);
50
+ return Boolean(this._atoms?.every((a) => a instanceof FunctionAtom));
48
51
  }
49
- equate(type) {
50
- if (this._atoms.length !== type?._atoms.length) {
51
- return false;
52
- }
53
- const unmatched = [...type._atoms];
54
- for (const atom of this._atoms) {
55
- const index = unmatched.findIndex((a) => atom.equate(a));
56
- if (index < 0) {
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.splice(index, 1);
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
- accept(type) {
83
+ intersects(type) {
64
84
  if (type instanceof Type) {
65
- if (this.isUnknown || type.isUnknown) {
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.every((a) => this._atoms.some((atom) => atom.accept(a)));
100
+ return Boolean(type._atoms?.every((a) => this._atoms?.some((atom) => atom.accepts(a))));
69
101
  }
70
- return this._atoms.some((a) => a.accept(type));
102
+ return Boolean(this._atoms?.some((a) => a.accepts(type)));
71
103
  }
72
- acceptValue(value) {
73
- if (this.isUnknown) {
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.some((atom) => {
78
- if (!atom.accept(valueAtom)) {
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.isTuple) {
88
- return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
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].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
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]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
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(mask) {
105
- if (mask.isUnknown || mask.isVoid) {
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.isUnknown) {
109
- return mask;
158
+ if (this.isAny) {
159
+ return type;
110
160
  }
111
- const list = this._atoms.filter((a) => mask.accept(a));
112
- return list.length === 0
161
+ const list = this._atoms?.filter((a) => type.accepts(a));
162
+ return list?.length === 0
113
163
  ? undefined
114
- : list.length === this._atoms.length
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.reduce((acc, a) => acc + a.weight(), 0);
169
+ return this._atoms?.reduce((acc, a) => acc + a.weight(), 0) ?? 0;
126
170
  }
127
171
  arrayAtomValType(index) {
128
- const atoms = this._atoms.filter((a) => a instanceof ArrayAtom);
172
+ const atoms = this.arrayAtoms();
129
173
  if (!atoms.length) {
130
- return Type.Unknown;
174
+ return Type.Any;
131
175
  }
132
176
  const valTypes = [];
133
177
  for (const atom of atoms) {
134
- const valType = atom.isTuple
135
- ? atom.valType(index)
136
- : atom.valType(0);
178
+ const valType = atom.valType(index);
137
179
  if (!valType) {
138
- return Type.Unknown;
180
+ return Type.Any;
139
181
  }
140
182
  valTypes.push(valType);
141
183
  }
142
184
  return Type.union(...valTypes);
143
185
  }
144
- functionAtomRetType(type) {
145
- const atoms = this._atoms.filter((a) => a instanceof FunctionAtom && type.accept(a.retType));
146
- return Type.union(...atoms.map((a) => a.retType));
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
- functionAtoms(type, argc) {
149
- if (this.isUnknown) {
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.filter((a) => a instanceof FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
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 this._atoms.length ? Type.union(Type.Void, this) : this;
236
+ return Type.union(Type.Null, this);
156
237
  }
157
238
  toMandatory() {
158
- if (!this.isOptional || this.isUnknown) {
239
+ if (!this._atoms?.length || !this.isOptional || this.isNull) {
159
240
  return this;
160
241
  }
161
- return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
242
+ return new Type(this._atoms.filter((a) => !a.equals(Type.Null.atom(0))));
162
243
  }
163
244
  toString() {
164
- return this._atoms.length
165
- ? [...this._atoms]
166
- .sort((a, b) => a.weight() - b.weight())
167
- .map((a) => a.toString())
168
- .join("|")
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.isUnknown)) {
173
- return Type.Unknown;
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 type = new Type([]);
176
- types.flatMap((t) => t._atoms).forEach((a) => {
177
- if (!type._atoms.some((atom) => atom.equate(a))) {
178
- type._atoms.push(a);
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 type;
270
+ }
271
+ return new Type(atoms);
182
272
  }
183
273
  static of(value) {
184
274
  return value == null
185
- ? Type.Void
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
- || typeof value === "boolean"
207
- || value instanceof Date
208
- || typeof value === "number"
209
- || typeof value === "bigint"
210
- || value instanceof ArrayBuffer
211
- || typeof value === "string";
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(itemTypes = []) {
217
- return new Type([new ArrayAtom(itemTypes)]);
309
+ static arrayType(types = Type.Any) {
310
+ return new Type([new ArrayAtom(types)]);
218
311
  }
219
- static objectType(propTypes = []) {
220
- return new Type([new ObjectAtom(propTypes)]);
312
+ static objectType(types = Type.Any) {
313
+ return new Type([new ObjectAtom(types)]);
221
314
  }
222
- static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
223
- return new Type([new FunctionAtom(retType, argTypes, isVariadic)]);
315
+ static functionType(signatures = [[Type.Any, [], true]], volatile) {
316
+ return new Type([new FunctionAtom(signatures, volatile)]);
224
317
  }
225
- static Unknown = new Type();
226
- static Void = Type.primitiveType("void");
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.Void, Type.Boolean);
322
+ static OptionalBoolean = Type.union(Type.Null, Type.Boolean);
229
323
  static Timestamp = Type.primitiveType("timestamp");
230
- static OptionalTimestamp = Type.union(Type.Void, Type.Timestamp);
324
+ static OptionalTimestamp = Type.union(Type.Null, Type.Timestamp);
231
325
  static Float = Type.primitiveType("float");
232
- static OptionalFloat = Type.union(Type.Void, Type.Float);
326
+ static OptionalFloat = Type.union(Type.Null, Type.Float);
233
327
  static Integer = Type.primitiveType("integer");
234
- static OptionalInteger = Type.union(Type.Void, Type.Integer);
328
+ static OptionalInteger = Type.union(Type.Null, Type.Integer);
235
329
  static Buffer = Type.primitiveType("buffer");
236
- static OptionalBuffer = Type.union(Type.Void, Type.Buffer);
330
+ static OptionalBuffer = Type.union(Type.Null, Type.Buffer);
237
331
  static String = Type.primitiveType("string");
238
- static OptionalString = Type.union(Type.Void, Type.String);
332
+ static OptionalString = Type.union(Type.Null, Type.String);
239
333
  static Array = Type.arrayType();
240
- static OptionalArray = Type.union(Type.Void, Type.Array);
334
+ static OptionalArray = Type.union(Type.Null, Type.Array);
241
335
  static Object = Type.objectType();
242
- static OptionalObject = Type.union(Type.Void, Type.Object);
243
- static Function = Type.functionType(undefined, undefined, true);
244
- static OptionalFunction = Type.union(Type.Void, Type.Function);
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.Void, Type.Array, Type.Object, Type.Function);
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.Void, Type.Float, Type.Integer);
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.Void, Type.Buffer, Type.String, Type.Array);
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.Void, Type.Buffer, Type.String, Type.Array, Type.Object);
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
+ }