affinirum 1.4.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +226 -154
- package/dst/Affinirum.js +24 -15
- package/dst/Atom.d.ts +3 -1
- package/dst/Constant.js +1 -1
- package/dst/Functions.js +7 -4
- package/dst/Keywords.js +4 -4
- package/dst/ParserState.d.ts +2 -1
- package/dst/ParserState.js +43 -82
- package/dst/Type.d.ts +11 -7
- package/dst/Type.js +113 -38
- package/dst/Value.d.ts +1 -0
- package/dst/Value.js +19 -1
- package/dst/atom/ArrayAtom.d.ts +7 -3
- package/dst/atom/ArrayAtom.js +53 -8
- package/dst/atom/FunctionAtom.d.ts +4 -1
- package/dst/atom/FunctionAtom.js +65 -15
- package/dst/atom/ObjectAtom.d.ts +7 -5
- package/dst/atom/ObjectAtom.js +35 -13
- package/dst/atom/PrimitiveAtom.d.ts +3 -1
- package/dst/atom/PrimitiveAtom.js +10 -1
- package/dst/cjs/Affinirum.js +24 -15
- package/dst/cjs/Constant.js +1 -1
- package/dst/cjs/Functions.js +6 -3
- package/dst/cjs/Keywords.js +4 -4
- package/dst/cjs/ParserState.js +42 -81
- package/dst/cjs/Type.js +113 -38
- package/dst/cjs/Value.js +21 -0
- package/dst/cjs/atom/ArrayAtom.js +53 -8
- package/dst/cjs/atom/FunctionAtom.js +65 -15
- package/dst/cjs/atom/ObjectAtom.js +35 -13
- package/dst/cjs/atom/PrimitiveAtom.js +10 -1
- package/dst/cjs/native/Aggregable.js +3 -2
- package/dst/cjs/native/Array.js +10 -8
- package/dst/cjs/native/Boolean.js +8 -5
- package/dst/cjs/native/Buffer.js +32 -3
- package/dst/cjs/native/Enumerable.js +7 -4
- package/dst/cjs/native/Float.js +26 -13
- package/dst/cjs/native/Integer.js +64 -28
- package/dst/cjs/native/Iterable.js +8 -5
- package/dst/cjs/native/Number.js +62 -33
- package/dst/cjs/native/Object.js +2 -1
- package/dst/cjs/native/String.js +71 -24
- package/dst/cjs/native/Timestamp.js +76 -18
- package/dst/cjs/native/Unknown.js +1 -1
- package/dst/cjs/native/notation/AN.js +152 -11
- package/dst/cjs/native/notation/IP.js +4 -3
- package/dst/cjs/native/notation/JSON.js +1 -1
- package/dst/cjs/node/ArrayNode.js +6 -6
- package/dst/cjs/node/BlockNode.js +5 -4
- package/dst/cjs/node/CallNode.js +28 -22
- package/dst/cjs/node/CastNode.js +42 -0
- package/dst/cjs/node/ObjectNode.js +2 -4
- package/dst/cjs/node/SwitchNode.js +10 -10
- package/dst/cjs/node/VariableNode.js +3 -1
- package/dst/native/Aggregable.js +3 -2
- package/dst/native/Array.js +10 -8
- package/dst/native/Boolean.d.ts +1 -0
- package/dst/native/Boolean.js +7 -4
- package/dst/native/Buffer.d.ts +2 -0
- package/dst/native/Buffer.js +29 -2
- package/dst/native/Enumerable.js +7 -4
- package/dst/native/Float.d.ts +2 -0
- package/dst/native/Float.js +25 -12
- package/dst/native/Integer.d.ts +5 -1
- package/dst/native/Integer.js +61 -27
- package/dst/native/Iterable.js +8 -5
- package/dst/native/Number.d.ts +4 -3
- package/dst/native/Number.js +57 -32
- package/dst/native/Object.js +2 -1
- package/dst/native/String.d.ts +2 -7
- package/dst/native/String.js +68 -16
- package/dst/native/Timestamp.d.ts +4 -2
- package/dst/native/Timestamp.js +73 -17
- package/dst/native/Unknown.js +1 -1
- package/dst/native/notation/AN.d.ts +1 -0
- package/dst/native/notation/AN.js +152 -12
- package/dst/native/notation/IP.js +4 -3
- package/dst/native/notation/JSON.js +1 -1
- package/dst/node/ArrayNode.d.ts +1 -1
- package/dst/node/ArrayNode.js +6 -6
- package/dst/node/BlockNode.js +5 -4
- package/dst/node/CallNode.js +28 -22
- package/dst/node/CastNode.d.ts +15 -0
- package/dst/node/CastNode.js +38 -0
- package/dst/node/ObjectNode.d.ts +1 -1
- package/dst/node/ObjectNode.js +2 -4
- package/dst/node/SwitchNode.js +10 -10
- package/dst/node/VariableNode.js +3 -1
- package/package.json +5 -4
package/dst/Affinirum.js
CHANGED
|
@@ -6,6 +6,7 @@ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, fun
|
|
|
6
6
|
import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
|
|
7
7
|
import { Constant } from "./Constant.js";
|
|
8
8
|
import { Variable } from "./Variable.js";
|
|
9
|
+
import { normalize } from "./Value.js";
|
|
9
10
|
import { Type } from "./Type.js";
|
|
10
11
|
import { Keywords } from "./Keywords.js";
|
|
11
12
|
import { Constants } from "./Constants.js";
|
|
@@ -13,6 +14,7 @@ import { Functions } from "./Functions.js";
|
|
|
13
14
|
import { ArrayNode } from "./node/ArrayNode.js";
|
|
14
15
|
import { BlockNode } from "./node/BlockNode.js";
|
|
15
16
|
import { CallNode } from "./node/CallNode.js";
|
|
17
|
+
import { CastNode } from "./node/CastNode.js";
|
|
16
18
|
import { ConstantNode } from "./node/ConstantNode.js";
|
|
17
19
|
import { JumpNode } from "./node/JumpNode.js";
|
|
18
20
|
import { LoopNode } from "./node/LoopNode.js";
|
|
@@ -93,23 +95,23 @@ export class Affinirum {
|
|
|
93
95
|
evaluate(values) {
|
|
94
96
|
const variables = this._scope.variables();
|
|
95
97
|
for (const name in variables) {
|
|
96
|
-
if (!Object.
|
|
98
|
+
if (!values || !Object.hasOwn(values, name)) {
|
|
97
99
|
this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
98
100
|
}
|
|
99
101
|
const variable = variables[name];
|
|
100
|
-
const value = values?.[name] ?? undefined;
|
|
101
|
-
if (!variable.type.
|
|
102
|
+
const value = normalize(values?.[name] ?? undefined);
|
|
103
|
+
if (!variable.type.acceptValue(value)) {
|
|
102
104
|
this._varframes.get(name)?.throwError(`unexpected type ${Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
103
105
|
}
|
|
104
106
|
variable.value = value;
|
|
105
107
|
}
|
|
106
108
|
try {
|
|
107
|
-
return this._root.evaluate();
|
|
109
|
+
return normalize(this._root.evaluate() ?? undefined);
|
|
108
110
|
}
|
|
109
111
|
catch (e) {
|
|
110
112
|
if (e instanceof JumpException) {
|
|
111
113
|
if (e.jump === "exit") {
|
|
112
|
-
return e.value;
|
|
114
|
+
return normalize(e.value ?? undefined);
|
|
113
115
|
}
|
|
114
116
|
else {
|
|
115
117
|
throw this._root.throwError(`unexpected ${e.jump} jump`);
|
|
@@ -235,9 +237,9 @@ export class Affinirum {
|
|
|
235
237
|
return node;
|
|
236
238
|
}
|
|
237
239
|
_accessor(state, scope) {
|
|
240
|
+
const frame = state.starts();
|
|
238
241
|
let node = this._term(state, scope);
|
|
239
|
-
while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen) {
|
|
240
|
-
const frame = state.starts();
|
|
242
|
+
while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
|
|
241
243
|
if (state.isDot || state.isQuestion) { // property access or property existance operator
|
|
242
244
|
const operator = state.isDot ? funcAt : funcHas;
|
|
243
245
|
if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
|
|
@@ -283,13 +285,20 @@ export class Affinirum {
|
|
|
283
285
|
break;
|
|
284
286
|
}
|
|
285
287
|
}
|
|
288
|
+
state.closeParentheses();
|
|
286
289
|
node = new CallNode(frame.ends(state), node, subnodes);
|
|
287
|
-
state.
|
|
290
|
+
state.next();
|
|
288
291
|
}
|
|
289
292
|
else if (state.isBracketsOpen) { // index access operator
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
+
const subnodes = [node, this._unit(state.next(), scope)];
|
|
294
|
+
state.closeBrackets();
|
|
295
|
+
const fnode = new ConstantNode(frame.ends(state), funcAt);
|
|
296
|
+
node = new CallNode(frame, fnode, subnodes);
|
|
297
|
+
state.next();
|
|
298
|
+
}
|
|
299
|
+
else if (state.isCast) { // type cast operator
|
|
300
|
+
const type = this._type(state.next(), scope);
|
|
301
|
+
node = new CastNode(frame.ends(state), node, type);
|
|
293
302
|
}
|
|
294
303
|
}
|
|
295
304
|
return node;
|
|
@@ -462,8 +471,8 @@ export class Affinirum {
|
|
|
462
471
|
state.throwError("unexpected expression token");
|
|
463
472
|
}
|
|
464
473
|
_function(state, scope) {
|
|
465
|
-
state.next().openParentheses();
|
|
466
474
|
const frame = state.starts();
|
|
475
|
+
state.next().openParentheses();
|
|
467
476
|
let variadic = false;
|
|
468
477
|
const variables = new Map();
|
|
469
478
|
while (!state.next().isParenthesesClose) {
|
|
@@ -501,8 +510,8 @@ export class Affinirum {
|
|
|
501
510
|
const args = Array.from(variables.values());
|
|
502
511
|
const subnode = this._unit(state, scope.subscope(variables));
|
|
503
512
|
const func = (...values) => {
|
|
504
|
-
args.forEach((arg, ix) => arg.value = values[ix]);
|
|
505
|
-
return subnode.evaluate();
|
|
513
|
+
args.forEach((arg, ix) => arg.value = normalize(values[ix]));
|
|
514
|
+
return normalize(subnode.evaluate());
|
|
506
515
|
};
|
|
507
516
|
const constant = new Constant(func, Type.functionType(retType, args.map((v) => v.type), variadic));
|
|
508
517
|
return new ConstantNode(frame, constant, subnode);
|
|
@@ -554,7 +563,7 @@ export class Affinirum {
|
|
|
554
563
|
}
|
|
555
564
|
state.closeBrackets().next();
|
|
556
565
|
return colon
|
|
557
|
-
? Type.objectType(
|
|
566
|
+
? Type.objectType(itemKeyTypes.map(([key, type]) => [key, type]))
|
|
558
567
|
: Type.arrayType(itemKeyTypes.map(([, v]) => v));
|
|
559
568
|
}
|
|
560
569
|
else if (state.isTilda) { // function type
|
package/dst/Atom.d.ts
CHANGED
package/dst/Constant.js
CHANGED
|
@@ -12,7 +12,7 @@ export class Constant {
|
|
|
12
12
|
return this._value;
|
|
13
13
|
}
|
|
14
14
|
get type() {
|
|
15
|
-
return Type.
|
|
15
|
+
return Type.isPrimitive(this._value) ? Type.of(this._value) : this._type;
|
|
16
16
|
}
|
|
17
17
|
set type(type) {
|
|
18
18
|
this._type = type;
|
package/dst/Functions.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { funcAdd } from "./native/Aggregable.js";
|
|
2
2
|
import { funcFirst, funcLast, funcFirstIndex, funcLastIndex, funcEvery, funcAny, funcFlatten, funcReverse, funcDerive, funcFilter, funcReduce, funcCompose, funcPrepend, funcAppend } from "./native/Array.js";
|
|
3
|
+
import { funcBoolean } from "./native/Boolean.js";
|
|
3
4
|
import { funcByte } from "./native/Buffer.js";
|
|
4
5
|
import { funcSlice, funcSplice, funcInject } from "./native/Enumerable.js";
|
|
6
|
+
import { funcFloat } from "./native/Float.js";
|
|
7
|
+
import { funcInteger } from "./native/Integer.js";
|
|
5
8
|
import { funcLength, funcContains, funcAt, funcHas } from "./native/Iterable.js";
|
|
6
|
-
import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate
|
|
9
|
+
import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate } from "./native/Number.js";
|
|
7
10
|
import { funcEntries, funcKeys, funcValues } from "./native/Object.js";
|
|
8
11
|
import { funcLike, funcUnlike, funcStartsWith, funcEndsWith, funcChar, funcCharCode, funcTrim, funcTrimStart, funcTrimEnd, funcLowerCase, funcUpperCase, funcSplit, funcReplaceWith } from "./native/String.js";
|
|
9
12
|
import { funcYear, funcMonth, funcMonthIndex, funcWeekdayIndex, funcDay, funcHour, funcMinute, funcSecond, funcMillisecond, funcEpochTime, funcDaysSince, funcHoursSince, funcMinutesSince, funcSecondsSince } from "./native/Timestamp.js";
|
|
@@ -50,9 +53,9 @@ export const Functions = [
|
|
|
50
53
|
["Power", funcPower],
|
|
51
54
|
["Root", funcRoot],
|
|
52
55
|
["Negate", funcNegate],
|
|
53
|
-
["
|
|
54
|
-
["
|
|
55
|
-
["
|
|
56
|
+
["Boolean", funcBoolean],
|
|
57
|
+
["Float", funcFloat],
|
|
58
|
+
["Integer", funcInteger],
|
|
56
59
|
// Object
|
|
57
60
|
["Entries", funcEntries],
|
|
58
61
|
["Keys", funcKeys],
|
package/dst/Keywords.js
CHANGED
package/dst/ParserState.d.ts
CHANGED
|
@@ -32,10 +32,11 @@ export declare class ParserState extends ParserFrame {
|
|
|
32
32
|
get isSemicolon(): boolean;
|
|
33
33
|
get isColon(): boolean;
|
|
34
34
|
get isComma(): boolean;
|
|
35
|
+
get isTilda(): boolean;
|
|
35
36
|
get isDot(): boolean;
|
|
36
37
|
get isQuestion(): boolean;
|
|
37
38
|
get isEllipsis(): boolean;
|
|
38
|
-
get
|
|
39
|
+
get isCast(): boolean;
|
|
39
40
|
get isVar(): boolean;
|
|
40
41
|
get isVal(): boolean;
|
|
41
42
|
get isIf(): boolean;
|
package/dst/ParserState.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { funcOr, funcAnd, funcNot } from "./native/Boolean.js";
|
|
2
|
-
import {
|
|
2
|
+
import { parseLiteralBuffer } from "./native/Buffer.js";
|
|
3
3
|
import { funcAdd } from "./native/Aggregable.js";
|
|
4
|
-
import {
|
|
5
|
-
import { Constant } from "./Constant.js";
|
|
6
|
-
import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower } from "./native/Number.js";
|
|
4
|
+
import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower, isNumericSymbol, parseLiteralNumber } from "./native/Number.js";
|
|
7
5
|
import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
|
|
6
|
+
import { isTokenStartSymbol, isTokenSymbol, parseLiteralString } from "./native/String.js";
|
|
7
|
+
import { parseLiteralTimestamp } from "./native/Timestamp.js";
|
|
8
|
+
import { Constant } from "./Constant.js";
|
|
8
9
|
import { Type } from "./Type.js";
|
|
9
10
|
import { ParserFrame } from "./ParserFrame.js";
|
|
10
11
|
class Literal {
|
|
@@ -39,10 +40,11 @@ const idBracesClose = Symbol();
|
|
|
39
40
|
const idSemicolon = Symbol();
|
|
40
41
|
const idColon = Symbol();
|
|
41
42
|
const idComma = Symbol();
|
|
43
|
+
const idTilda = Symbol();
|
|
42
44
|
const idDot = Symbol();
|
|
43
45
|
const idQuestion = Symbol();
|
|
44
46
|
const idEllipsis = Symbol();
|
|
45
|
-
const
|
|
47
|
+
const idCast = Symbol();
|
|
46
48
|
const idVar = Symbol();
|
|
47
49
|
const idVal = Symbol();
|
|
48
50
|
const idIf = Symbol();
|
|
@@ -114,6 +116,9 @@ export class ParserState extends ParserFrame {
|
|
|
114
116
|
get isComma() {
|
|
115
117
|
return this._fragment === idComma;
|
|
116
118
|
}
|
|
119
|
+
get isTilda() {
|
|
120
|
+
return this._fragment === idTilda;
|
|
121
|
+
}
|
|
117
122
|
get isDot() {
|
|
118
123
|
return this._fragment === idDot;
|
|
119
124
|
}
|
|
@@ -123,8 +128,8 @@ export class ParserState extends ParserFrame {
|
|
|
123
128
|
get isEllipsis() {
|
|
124
129
|
return this._fragment === idEllipsis;
|
|
125
130
|
}
|
|
126
|
-
get
|
|
127
|
-
return this._fragment ===
|
|
131
|
+
get isCast() {
|
|
132
|
+
return this._fragment === idCast;
|
|
128
133
|
}
|
|
129
134
|
get isVar() {
|
|
130
135
|
return this._fragment === idVar;
|
|
@@ -240,7 +245,15 @@ export class ParserState extends ParserFrame {
|
|
|
240
245
|
this._fragment = idSemicolon;
|
|
241
246
|
break;
|
|
242
247
|
case ":":
|
|
243
|
-
this.
|
|
248
|
+
switch (this._expr.charAt(this._end)) {
|
|
249
|
+
case ":":
|
|
250
|
+
++this._end;
|
|
251
|
+
this._fragment = idCast;
|
|
252
|
+
break;
|
|
253
|
+
default:
|
|
254
|
+
this._fragment = idColon;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
244
257
|
break;
|
|
245
258
|
case ",":
|
|
246
259
|
this._fragment = idComma;
|
|
@@ -437,53 +450,25 @@ export class ParserState extends ParserFrame {
|
|
|
437
450
|
break;
|
|
438
451
|
}
|
|
439
452
|
break;
|
|
440
|
-
case "@":
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
if (isDateTimeSeparatorSymbol(this._expr.charAt(this._end))) {
|
|
445
|
-
++this._end;
|
|
446
|
-
while (isTimeSymbol(this._expr.charAt(this._end))) {
|
|
447
|
-
++this._end;
|
|
448
|
-
}
|
|
449
|
-
if (this._expr.charAt(this._end) === "."
|
|
450
|
-
&& isNumericSymbol(this._expr.charAt(this._end + 1))) {
|
|
451
|
-
++this._end;
|
|
452
|
-
while (isNumericSymbol(this._expr.charAt(this._end))) {
|
|
453
|
-
++this._end;
|
|
454
|
-
}
|
|
455
|
-
if (this._expr.charAt(this._end) === "Z") {
|
|
456
|
-
++this._end;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
this._fragment = new Literal(new Date(this._expr.substring(this._start + 1, this._end)));
|
|
453
|
+
case "@": {
|
|
454
|
+
const [ts, tsEnd] = parseLiteralTimestamp(this._expr, this._start);
|
|
455
|
+
this._fragment = new Literal(ts);
|
|
456
|
+
this._end = tsEnd;
|
|
461
457
|
break;
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
if (this._end >= this._expr.length) {
|
|
468
|
-
this._start = this._expr.length;
|
|
469
|
-
throw new Error(`missing closing buffer quotation mark ${c}`);
|
|
470
|
-
}
|
|
471
|
-
this._fragment = new Literal(parseBuffer(replaceWith(this._expr.substring(this._start + 1, this._end), "", " ", "\t", "\n")));
|
|
472
|
-
++this._end;
|
|
458
|
+
}
|
|
459
|
+
case "`": {
|
|
460
|
+
const [buf, bufEnd] = parseLiteralBuffer(this._expr, this._start);
|
|
461
|
+
this._fragment = new Literal(buf);
|
|
462
|
+
this._end = bufEnd;
|
|
473
463
|
break;
|
|
464
|
+
}
|
|
474
465
|
case "\"":
|
|
475
|
-
case "'":
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
if (this._end >= this._expr.length) {
|
|
481
|
-
this._start = this._expr.length;
|
|
482
|
-
throw new Error(`missing closing string quotation mark ${c}`);
|
|
483
|
-
}
|
|
484
|
-
this._fragment = new Literal(replaceWith(this._expr.substring(this._start + 1, this._end), "", "\\\n"));
|
|
485
|
-
++this._end;
|
|
466
|
+
case "'": {
|
|
467
|
+
const [str, strEnd] = parseLiteralString(this._expr, this._start);
|
|
468
|
+
this._fragment = new Literal(str);
|
|
469
|
+
this._end = strEnd;
|
|
486
470
|
break;
|
|
471
|
+
}
|
|
487
472
|
default:
|
|
488
473
|
if (isTokenStartSymbol(c)) {
|
|
489
474
|
while (isTokenSymbol(this._expr.charAt(this._end))) {
|
|
@@ -503,15 +488,19 @@ export class ParserState extends ParserFrame {
|
|
|
503
488
|
case "void":
|
|
504
489
|
this._fragment = Type.Void;
|
|
505
490
|
break;
|
|
491
|
+
case "bool":
|
|
506
492
|
case "boolean":
|
|
507
493
|
this._fragment = Type.Boolean;
|
|
508
494
|
break;
|
|
495
|
+
case "time":
|
|
509
496
|
case "timestamp":
|
|
510
497
|
this._fragment = Type.Timestamp;
|
|
511
498
|
break;
|
|
499
|
+
case "f64":
|
|
512
500
|
case "float":
|
|
513
501
|
this._fragment = Type.Float;
|
|
514
502
|
break;
|
|
503
|
+
case "i64":
|
|
515
504
|
case "integer":
|
|
516
505
|
this._fragment = Type.Integer;
|
|
517
506
|
break;
|
|
@@ -557,37 +546,9 @@ export class ParserState extends ParserFrame {
|
|
|
557
546
|
}
|
|
558
547
|
}
|
|
559
548
|
else if (isNumericSymbol(c)) {
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
if (this._expr.charAt(this._end) === ".") {
|
|
565
|
-
++this._end;
|
|
566
|
-
if (isNumericSymbol(this._expr.charAt(this._end))) {
|
|
567
|
-
++this._end;
|
|
568
|
-
while (isNumericSymbol(this._expr.charAt(this._end))) {
|
|
569
|
-
++this._end;
|
|
570
|
-
}
|
|
571
|
-
integer = false;
|
|
572
|
-
}
|
|
573
|
-
else {
|
|
574
|
-
--this._end;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
if (this._expr.charAt(this._end) === "e") {
|
|
578
|
-
++this._end;
|
|
579
|
-
if (isNumericSymbol(this._expr.charAt(this._end))
|
|
580
|
-
|| isSignSymbol(this._expr.charAt(this._end))) {
|
|
581
|
-
++this._end;
|
|
582
|
-
while (isNumericSymbol(this._expr.charAt(this._end))) {
|
|
583
|
-
++this._end;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
integer = false;
|
|
587
|
-
}
|
|
588
|
-
this._fragment = integer
|
|
589
|
-
? new Literal(BigInt(this._expr.substring(this._start, this._end)))
|
|
590
|
-
: new Literal(parseFloat(this._expr.substring(this._start, this._end)));
|
|
549
|
+
const [num, numEnd] = parseLiteralNumber(this._expr, this._start);
|
|
550
|
+
this._fragment = new Literal(num);
|
|
551
|
+
this._end = numEnd;
|
|
591
552
|
}
|
|
592
553
|
else {
|
|
593
554
|
throw new Error(`unknown symbol ${c}`);
|
package/dst/Type.d.ts
CHANGED
|
@@ -20,20 +20,24 @@ export declare class Type {
|
|
|
20
20
|
get isArray(): boolean;
|
|
21
21
|
get isObject(): boolean;
|
|
22
22
|
get isFunction(): boolean;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
functionAtoms(type: Type, argc: number): FunctionAtom[];
|
|
23
|
+
equate(type: Type | undefined): boolean;
|
|
24
|
+
accept(type: Type | Atom): boolean;
|
|
25
|
+
acceptValue(value: Value): boolean;
|
|
27
26
|
reduce(mask: Type): Type | undefined;
|
|
28
|
-
|
|
27
|
+
intersect(type: Type): boolean;
|
|
29
28
|
weight(): number;
|
|
29
|
+
arrayAtomValType(index: number): Type;
|
|
30
|
+
functionAtomRetType(type: Type): Type;
|
|
31
|
+
functionAtoms(type: Type, argc: number): FunctionAtom[];
|
|
32
|
+
toOptional(): Type;
|
|
33
|
+
toMandatory(): Type;
|
|
30
34
|
toString(): string;
|
|
31
35
|
static union(...types: Type[]): Type;
|
|
32
36
|
static of(value: Value): Type;
|
|
33
|
-
static
|
|
37
|
+
static isPrimitive(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
|
|
34
38
|
static primitiveType(primitive: Primitive): Type;
|
|
35
39
|
static arrayType(itemTypes?: Type[]): Type;
|
|
36
|
-
static objectType(propTypes?:
|
|
40
|
+
static objectType(propTypes?: [string, Type][]): Type;
|
|
37
41
|
static functionType(retType?: Type, argTypes?: Type[], isVariadic?: boolean): Type;
|
|
38
42
|
static readonly Unknown: Type;
|
|
39
43
|
static readonly Void: Type;
|
package/dst/Type.js
CHANGED
|
@@ -14,54 +14,92 @@ export class Type {
|
|
|
14
14
|
return this._atoms.length === 1;
|
|
15
15
|
}
|
|
16
16
|
get isOptional() {
|
|
17
|
-
return this.isUnknown || this._atoms.length > 1 && this._atoms.some((
|
|
17
|
+
return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
|
|
18
18
|
}
|
|
19
19
|
get isVoid() {
|
|
20
|
-
return this.isAtom && this._atoms[0].
|
|
20
|
+
return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
|
|
21
21
|
}
|
|
22
22
|
get isBoolean() {
|
|
23
|
-
return this.isAtom && this._atoms[0].
|
|
23
|
+
return this.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
|
|
24
24
|
}
|
|
25
25
|
get isTimestamp() {
|
|
26
|
-
return this.isAtom && this._atoms[0].
|
|
26
|
+
return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
|
|
27
27
|
}
|
|
28
28
|
get isFloat() {
|
|
29
|
-
return this.isAtom && this._atoms[0].
|
|
29
|
+
return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
|
|
30
30
|
}
|
|
31
31
|
get isInteger() {
|
|
32
|
-
return this.isAtom && this._atoms[0].
|
|
32
|
+
return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
|
|
33
33
|
}
|
|
34
34
|
get isBuffer() {
|
|
35
|
-
return this.isAtom && this._atoms[0].
|
|
35
|
+
return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
|
|
36
36
|
}
|
|
37
37
|
get isString() {
|
|
38
|
-
return this.isAtom && this._atoms[0].
|
|
38
|
+
return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
|
|
39
39
|
}
|
|
40
40
|
get isArray() {
|
|
41
|
-
return this._atoms.every((
|
|
41
|
+
return this._atoms.every((a) => a instanceof ArrayAtom);
|
|
42
42
|
}
|
|
43
43
|
get isObject() {
|
|
44
|
-
return this._atoms.every((
|
|
44
|
+
return this._atoms.every((a) => a instanceof ObjectAtom);
|
|
45
45
|
}
|
|
46
46
|
get isFunction() {
|
|
47
|
-
return this._atoms.every((
|
|
47
|
+
return this._atoms.every((a) => a instanceof FunctionAtom);
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
unmatched.splice(index, 1);
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
55
62
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
accept(type) {
|
|
64
|
+
if (type instanceof Type) {
|
|
65
|
+
if (this.isUnknown || type.isUnknown) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return type._atoms.every((a) => this._atoms.some((atom) => atom.accept(a)));
|
|
69
|
+
}
|
|
70
|
+
return this._atoms.some((a) => a.accept(type));
|
|
59
71
|
}
|
|
60
|
-
|
|
72
|
+
acceptValue(value) {
|
|
61
73
|
if (this.isUnknown) {
|
|
62
|
-
return
|
|
74
|
+
return true;
|
|
63
75
|
}
|
|
64
|
-
|
|
76
|
+
const valueAtom = Type.of(value)._atoms[0];
|
|
77
|
+
return this._atoms.some((atom) => {
|
|
78
|
+
if (!atom.accept(valueAtom)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (atom instanceof ArrayAtom) {
|
|
82
|
+
const values = value;
|
|
83
|
+
const types = atom.valTypes();
|
|
84
|
+
if (!types.length) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (atom.isTuple) {
|
|
88
|
+
return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
|
|
89
|
+
}
|
|
90
|
+
for (let i = 0; i < values.length; ++i) {
|
|
91
|
+
if (!types[0].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (atom instanceof ObjectAtom) {
|
|
98
|
+
const values = value;
|
|
99
|
+
return atom.valTypes().every(([key, type]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
});
|
|
65
103
|
}
|
|
66
104
|
reduce(mask) {
|
|
67
105
|
if (mask.isUnknown || mask.isVoid) {
|
|
@@ -70,37 +108,74 @@ export class Type {
|
|
|
70
108
|
if (this.isUnknown) {
|
|
71
109
|
return mask;
|
|
72
110
|
}
|
|
73
|
-
const list = this._atoms.filter((
|
|
111
|
+
const list = this._atoms.filter((a) => mask.accept(a));
|
|
74
112
|
return list.length === 0
|
|
75
113
|
? undefined
|
|
76
114
|
: list.length === this._atoms.length
|
|
77
115
|
? this
|
|
78
116
|
: new Type(list);
|
|
79
117
|
}
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
return this._atoms.some((i) => i.match(type));
|
|
118
|
+
intersect(type) {
|
|
119
|
+
if (this.isUnknown || type.isUnknown) {
|
|
120
|
+
return true;
|
|
86
121
|
}
|
|
122
|
+
return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
|
|
87
123
|
}
|
|
88
124
|
weight() {
|
|
89
|
-
return this._atoms.reduce((acc,
|
|
125
|
+
return this._atoms.reduce((acc, a) => acc + a.weight(), 0);
|
|
126
|
+
}
|
|
127
|
+
arrayAtomValType(index) {
|
|
128
|
+
const atoms = this._atoms.filter((a) => a instanceof ArrayAtom);
|
|
129
|
+
if (!atoms.length) {
|
|
130
|
+
return Type.Unknown;
|
|
131
|
+
}
|
|
132
|
+
const valTypes = [];
|
|
133
|
+
for (const atom of atoms) {
|
|
134
|
+
const valType = atom.isTuple
|
|
135
|
+
? atom.valType(index)
|
|
136
|
+
: atom.valType(0);
|
|
137
|
+
if (!valType) {
|
|
138
|
+
return Type.Unknown;
|
|
139
|
+
}
|
|
140
|
+
valTypes.push(valType);
|
|
141
|
+
}
|
|
142
|
+
return Type.union(...valTypes);
|
|
143
|
+
}
|
|
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));
|
|
147
|
+
}
|
|
148
|
+
functionAtoms(type, argc) {
|
|
149
|
+
if (this.isUnknown) {
|
|
150
|
+
return Type.Function._atoms;
|
|
151
|
+
}
|
|
152
|
+
return this._atoms.filter((a) => a instanceof FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
|
|
153
|
+
}
|
|
154
|
+
toOptional() {
|
|
155
|
+
return this._atoms.length ? Type.union(Type.Void, this) : this;
|
|
156
|
+
}
|
|
157
|
+
toMandatory() {
|
|
158
|
+
if (!this.isOptional || this.isUnknown) {
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
|
|
90
162
|
}
|
|
91
163
|
toString() {
|
|
92
164
|
return this._atoms.length
|
|
93
|
-
? this._atoms
|
|
165
|
+
? [...this._atoms]
|
|
166
|
+
.sort((a, b) => a.weight() - b.weight())
|
|
167
|
+
.map((a) => a.toString())
|
|
168
|
+
.join("|")
|
|
94
169
|
: "??";
|
|
95
170
|
}
|
|
96
171
|
static union(...types) {
|
|
97
|
-
if (types.some((
|
|
172
|
+
if (types.some((t) => t.isUnknown)) {
|
|
98
173
|
return Type.Unknown;
|
|
99
174
|
}
|
|
100
175
|
const type = new Type([]);
|
|
101
|
-
types.
|
|
102
|
-
if (!type.
|
|
103
|
-
type._atoms.push(
|
|
176
|
+
types.flatMap((t) => t._atoms).forEach((a) => {
|
|
177
|
+
if (!type._atoms.some((atom) => atom.equate(a))) {
|
|
178
|
+
type._atoms.push(a);
|
|
104
179
|
}
|
|
105
180
|
});
|
|
106
181
|
return type;
|
|
@@ -126,7 +201,7 @@ export class Type {
|
|
|
126
201
|
? Type.Object
|
|
127
202
|
: Type.Function;
|
|
128
203
|
}
|
|
129
|
-
static
|
|
204
|
+
static isPrimitive(value) {
|
|
130
205
|
return value == null
|
|
131
206
|
|| typeof value === "boolean"
|
|
132
207
|
|| value instanceof Date
|
|
@@ -141,7 +216,7 @@ export class Type {
|
|
|
141
216
|
static arrayType(itemTypes = []) {
|
|
142
217
|
return new Type([new ArrayAtom(itemTypes)]);
|
|
143
218
|
}
|
|
144
|
-
static objectType(propTypes =
|
|
219
|
+
static objectType(propTypes = []) {
|
|
145
220
|
return new Type([new ObjectAtom(propTypes)]);
|
|
146
221
|
}
|
|
147
222
|
static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
|
package/dst/Value.d.ts
CHANGED