affinirum 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +301 -0
- package/dst/Affinirum.d.ts +73 -0
- package/dst/Affinirum.js +536 -0
- package/dst/Constant.d.ts +15 -0
- package/dst/Constant.js +26 -0
- package/dst/Constants.d.ts +2 -0
- package/dst/Constants.js +22 -0
- package/dst/Functions.d.ts +2 -0
- package/dst/Functions.js +85 -0
- package/dst/Keywords.d.ts +1 -0
- package/dst/Keywords.js +17 -0
- package/dst/Node.d.ts +13 -0
- package/dst/Node.js +22 -0
- package/dst/ParserFrame.d.ts +13 -0
- package/dst/ParserFrame.js +80 -0
- package/dst/ParserState.d.ts +54 -0
- package/dst/ParserState.js +562 -0
- package/dst/StaticScope.d.ts +13 -0
- package/dst/StaticScope.js +44 -0
- package/dst/Type.d.ts +72 -0
- package/dst/Type.js +196 -0
- package/dst/Value.d.ts +3 -0
- package/dst/Value.js +1 -0
- package/dst/Variable.d.ts +13 -0
- package/dst/Variable.js +25 -0
- package/dst/atom/ArrayAtom.d.ts +11 -0
- package/dst/atom/ArrayAtom.js +38 -0
- package/dst/atom/FunctionAtom.d.ts +17 -0
- package/dst/atom/FunctionAtom.js +54 -0
- package/dst/atom/ObjectAtom.d.ts +11 -0
- package/dst/atom/ObjectAtom.js +44 -0
- package/dst/atom/PrimitiveAtom.d.ts +8 -0
- package/dst/atom/PrimitiveAtom.js +26 -0
- package/dst/cjs/Affinirum.js +540 -0
- package/dst/cjs/Constant.js +30 -0
- package/dst/cjs/Constants.js +25 -0
- package/dst/cjs/Functions.js +88 -0
- package/dst/cjs/Keywords.js +20 -0
- package/dst/cjs/Node.js +26 -0
- package/dst/cjs/ParserFrame.js +84 -0
- package/dst/cjs/ParserState.js +566 -0
- package/dst/cjs/StaticScope.js +48 -0
- package/dst/cjs/Type.js +200 -0
- package/dst/cjs/Value.js +2 -0
- package/dst/cjs/Variable.js +29 -0
- package/dst/cjs/atom/ArrayAtom.js +42 -0
- package/dst/cjs/atom/FunctionAtom.js +58 -0
- package/dst/cjs/atom/ObjectAtom.js +48 -0
- package/dst/cjs/atom/PrimitiveAtom.js +30 -0
- package/dst/cjs/constant/Array.js +93 -0
- package/dst/cjs/constant/Boolean.js +25 -0
- package/dst/cjs/constant/Buffer.js +68 -0
- package/dst/cjs/constant/Enumerable.js +34 -0
- package/dst/cjs/constant/Float.js +85 -0
- package/dst/cjs/constant/Integer.js +104 -0
- package/dst/cjs/constant/Iterable.js +24 -0
- package/dst/cjs/constant/Number.js +59 -0
- package/dst/cjs/constant/Object.js +13 -0
- package/dst/cjs/constant/String.js +197 -0
- package/dst/cjs/constant/Timestamp.js +56 -0
- package/dst/cjs/constant/Unknown.js +101 -0
- package/dst/cjs/constant/notation/AN.js +46 -0
- package/dst/cjs/constant/notation/JSON.js +14 -0
- package/dst/cjs/index.js +19 -0
- package/dst/cjs/node/ArrayNode.js +34 -0
- package/dst/cjs/node/BlockNode.js +33 -0
- package/dst/cjs/node/CallNode.js +54 -0
- package/dst/cjs/node/ConstantNode.js +33 -0
- package/dst/cjs/node/LoopNode.js +34 -0
- package/dst/cjs/node/ObjectNode.js +42 -0
- package/dst/cjs/node/SwitchNode.js +45 -0
- package/dst/cjs/node/VariableNode.js +30 -0
- package/dst/cjs/package.json +3 -0
- package/dst/constant/Array.d.ts +22 -0
- package/dst/constant/Array.js +90 -0
- package/dst/constant/Boolean.d.ts +11 -0
- package/dst/constant/Boolean.js +22 -0
- package/dst/constant/Buffer.d.ts +10 -0
- package/dst/constant/Buffer.js +61 -0
- package/dst/constant/Enumerable.d.ts +5 -0
- package/dst/constant/Enumerable.js +31 -0
- package/dst/constant/Float.d.ts +22 -0
- package/dst/constant/Float.js +80 -0
- package/dst/constant/Integer.d.ts +10 -0
- package/dst/constant/Integer.js +100 -0
- package/dst/constant/Iterable.d.ts +3 -0
- package/dst/constant/Iterable.js +21 -0
- package/dst/constant/Number.d.ts +14 -0
- package/dst/constant/Number.js +56 -0
- package/dst/constant/Object.d.ts +7 -0
- package/dst/constant/Object.js +10 -0
- package/dst/constant/String.d.ts +34 -0
- package/dst/constant/String.js +179 -0
- package/dst/constant/Timestamp.d.ts +20 -0
- package/dst/constant/Timestamp.js +50 -0
- package/dst/constant/Unknown.d.ts +10 -0
- package/dst/constant/Unknown.js +95 -0
- package/dst/constant/notation/AN.d.ts +6 -0
- package/dst/constant/notation/AN.js +42 -0
- package/dst/constant/notation/JSON.d.ts +7 -0
- package/dst/constant/notation/JSON.js +10 -0
- package/dst/index.d.ts +3 -0
- package/dst/index.js +3 -0
- package/dst/node/ArrayNode.d.ts +12 -0
- package/dst/node/ArrayNode.js +30 -0
- package/dst/node/BlockNode.d.ts +12 -0
- package/dst/node/BlockNode.js +29 -0
- package/dst/node/CallNode.d.ts +14 -0
- package/dst/node/CallNode.js +50 -0
- package/dst/node/ConstantNode.d.ts +15 -0
- package/dst/node/ConstantNode.js +29 -0
- package/dst/node/LoopNode.d.ts +13 -0
- package/dst/node/LoopNode.js +30 -0
- package/dst/node/ObjectNode.d.ts +13 -0
- package/dst/node/ObjectNode.js +38 -0
- package/dst/node/SwitchNode.d.ts +14 -0
- package/dst/node/SwitchNode.js +41 -0
- package/dst/node/VariableNode.d.ts +14 -0
- package/dst/node/VariableNode.js +26 -0
- package/package.json +67 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Constant } from '../Constant.js';
|
|
2
|
+
export declare const encodeTimestamp: (value: Date, encoding?: "int64" | "int64le") => ArrayBuffer;
|
|
3
|
+
export declare const decodeTimestamp: (value: ArrayBuffer, encoding?: "int64" | "int64le", byteOffset?: bigint) => Date;
|
|
4
|
+
export declare const formatTimestamp: (value: Date, radix?: number) => string;
|
|
5
|
+
export declare const funcYear: Constant;
|
|
6
|
+
export declare const funcMonth: Constant;
|
|
7
|
+
export declare const funcMonthIndex: Constant;
|
|
8
|
+
export declare const funcWeekdayIndex: Constant;
|
|
9
|
+
export declare const funcDay: Constant;
|
|
10
|
+
export declare const funcHour: Constant;
|
|
11
|
+
export declare const funcMinute: Constant;
|
|
12
|
+
export declare const funcSecond: Constant;
|
|
13
|
+
export declare const funcMillisecond: Constant;
|
|
14
|
+
export declare const funcEpochTime: Constant;
|
|
15
|
+
export declare const constTimestamp: {
|
|
16
|
+
Now: Constant;
|
|
17
|
+
Epoch: Constant;
|
|
18
|
+
Decode: Constant;
|
|
19
|
+
Parse: Constant;
|
|
20
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Constant } from '../Constant.js';
|
|
2
|
+
import { Type } from '../Type.js';
|
|
3
|
+
const typeTimestampPart = Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalBoolean]);
|
|
4
|
+
export const encodeTimestamp = (value, encoding = 'int64') => {
|
|
5
|
+
const buf = new ArrayBuffer(8);
|
|
6
|
+
const dv = new DataView(buf);
|
|
7
|
+
dv.setBigInt64(0, BigInt(value.getTime()), encoding === 'int64le');
|
|
8
|
+
return buf;
|
|
9
|
+
};
|
|
10
|
+
export const decodeTimestamp = (value, encoding = 'int64', byteOffset) => new Date(Number(new DataView(value).getBigInt64(byteOffset == null ? 0 : Number(byteOffset), encoding === 'int64le')));
|
|
11
|
+
export const formatTimestamp = (value, radix) => {
|
|
12
|
+
const str = value.toISOString();
|
|
13
|
+
switch (radix) {
|
|
14
|
+
case 1: return str.slice(0, 4);
|
|
15
|
+
case 2: return str.slice(5, 7);
|
|
16
|
+
case 3: return str.slice(8, 10);
|
|
17
|
+
case 4: return str.slice(11, 13);
|
|
18
|
+
case 5: return str.slice(14, 16);
|
|
19
|
+
case 6: return str.slice(17, 19);
|
|
20
|
+
case 7: return str.slice(20, 23);
|
|
21
|
+
default: return str;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const parseTimestamp = (value) => {
|
|
25
|
+
if (value == null) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const date = new Date(value);
|
|
29
|
+
return isNaN(date.getTime()) ? undefined : date;
|
|
30
|
+
};
|
|
31
|
+
const funcNow = new Constant(() => new Date(), Type.functionType(Type.Timestamp, []), false);
|
|
32
|
+
export const funcYear = new Constant((value, utc = false) => BigInt(utc ? value.getUTCFullYear() : value.getFullYear()), typeTimestampPart);
|
|
33
|
+
export const funcMonth = new Constant((value, utc = false) => BigInt(1 + (utc ? value.getUTCMonth() : value.getMonth())), typeTimestampPart);
|
|
34
|
+
export const funcMonthIndex = new Constant((value, utc = false) => BigInt(utc ? value.getUTCMonth() : value.getMonth()), typeTimestampPart);
|
|
35
|
+
export const funcWeekdayIndex = new Constant((value, utc = false) => BigInt(utc ? value.getUTCDay() : value.getDay()), typeTimestampPart);
|
|
36
|
+
export const funcDay = new Constant((value, utc = false) => BigInt(utc ? value.getUTCDate() : value.getDate()), typeTimestampPart);
|
|
37
|
+
export const funcHour = new Constant((value, utc = false) => BigInt(utc ? value.getUTCHours() : value.getHours()), typeTimestampPart);
|
|
38
|
+
export const funcMinute = new Constant((value, utc = false) => BigInt(utc ? value.getUTCMinutes() : value.getMinutes()), typeTimestampPart);
|
|
39
|
+
export const funcSecond = new Constant((value, utc = false) => BigInt(utc ? value.getUTCSeconds() : value.getSeconds()), typeTimestampPart);
|
|
40
|
+
export const funcMillisecond = new Constant((value, utc = false) => BigInt(utc ? value.getUTCMilliseconds() : value.getMilliseconds()), typeTimestampPart);
|
|
41
|
+
export const funcEpochTime = new Constant((value, epoch = new Date(0)) => BigInt.asIntN(64, BigInt(value.getTime() - epoch.getTime())), Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalTimestamp]));
|
|
42
|
+
const funcEpochTimestamp = new Constant((value, epoch = new Date(0)) => new Date(Number(value) + epoch.getTime()), Type.functionType(Type.Timestamp, [Type.Number, Type.OptionalTimestamp]));
|
|
43
|
+
const funcDecodeTimestamp = new Constant((value, encoding = 'int64', byteOffset) => decodeTimestamp(value, encoding, byteOffset), Type.functionType(Type.OptionalTimestamp, [Type.Buffer, Type.OptionalString, Type.OptionalInteger]));
|
|
44
|
+
const funcParseTimestamp = new Constant((value) => parseTimestamp(value), Type.functionType(Type.OptionalTimestamp, [Type.String]));
|
|
45
|
+
export const constTimestamp = {
|
|
46
|
+
Now: funcNow,
|
|
47
|
+
Epoch: funcEpochTimestamp,
|
|
48
|
+
Decode: funcDecodeTimestamp,
|
|
49
|
+
Parse: funcParseTimestamp,
|
|
50
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Constant } from '../Constant.js';
|
|
2
|
+
import { Value } from '../Value.js';
|
|
3
|
+
export declare const equate: (value1: Value, value2: Value) => boolean;
|
|
4
|
+
export declare const encode: (value: Value, encoding?: "float32" | "float32le" | "float64" | "float64le" | "int8" | "int16" | "int16le" | "int32" | "int32le" | "int64" | "int64le" | "uint8" | "uint16" | "uint16le" | "uint32" | "uint32le" | "uint64" | "uint64le" | "utf8" | "ucs2" | "ucs2le") => ArrayBuffer;
|
|
5
|
+
export declare const format: (value: Value, radix?: number, separator?: string) => string;
|
|
6
|
+
export declare const funcCoalesce: Constant;
|
|
7
|
+
export declare const funcEqual: Constant;
|
|
8
|
+
export declare const funcNotEqual: Constant;
|
|
9
|
+
export declare const funcEncode: Constant;
|
|
10
|
+
export declare const funcFormat: Constant;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Constant } from '../Constant.js';
|
|
2
|
+
import { Type } from '../Type.js';
|
|
3
|
+
import { encodeFloat, formatFloat } from './Float.js';
|
|
4
|
+
import { encodeInteger } from './Integer.js';
|
|
5
|
+
import { concatBuffers, equateBuffers, formatBuffer } from './Buffer.js';
|
|
6
|
+
import { encodeString } from './String.js';
|
|
7
|
+
import { encodeTimestamp, formatTimestamp } from './Timestamp.js';
|
|
8
|
+
export const equate = (value1, value2) => {
|
|
9
|
+
if (value1 == null || value2 == null) {
|
|
10
|
+
return value1 == value2;
|
|
11
|
+
}
|
|
12
|
+
if (typeof value1 !== typeof value2) {
|
|
13
|
+
if (typeof value1 === 'number' && typeof value2 === 'bigint') {
|
|
14
|
+
return value1 === Number(value2);
|
|
15
|
+
}
|
|
16
|
+
if (typeof value1 === 'bigint' && typeof value2 === 'number') {
|
|
17
|
+
return Number.isInteger(value2) && value1 === BigInt(value2);
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value1 === 'number') {
|
|
22
|
+
return isNaN(value1) && isNaN(value2) ? true : value1 === value2;
|
|
23
|
+
}
|
|
24
|
+
if (typeof value1 === 'boolean' || typeof value1 === 'bigint' || typeof value1 === 'string' || typeof value1 === 'function') {
|
|
25
|
+
return value1 === value2;
|
|
26
|
+
}
|
|
27
|
+
if (value1 instanceof Date && value2 instanceof Date) {
|
|
28
|
+
return value1.getTime() === value2.getTime();
|
|
29
|
+
}
|
|
30
|
+
if (value1 instanceof ArrayBuffer && value2 instanceof ArrayBuffer) {
|
|
31
|
+
return equateBuffers(value1, value2);
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(value1) && Array.isArray(value2)) {
|
|
34
|
+
if (value1.length === value2.length) {
|
|
35
|
+
for (let i = 0; i < value1.length; ++i) {
|
|
36
|
+
if (!equate(value1[i], value2[i])) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const props = new Set([...Object.getOwnPropertyNames(value1), ...Object.getOwnPropertyNames(value2)]);
|
|
45
|
+
for (const prop of props) {
|
|
46
|
+
if (!equate(value1[prop], value2[prop])) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
export const encode = (value, encoding) => value == null
|
|
53
|
+
? new Uint8Array(0).buffer
|
|
54
|
+
: typeof value === 'boolean'
|
|
55
|
+
? new Uint8Array([value ? 255 : 0]).buffer
|
|
56
|
+
: value instanceof Date
|
|
57
|
+
? encodeTimestamp(value, encoding ?? 'int64')
|
|
58
|
+
: typeof value === 'number'
|
|
59
|
+
? encodeFloat(value, encoding ?? 'float64')
|
|
60
|
+
: typeof value === 'bigint'
|
|
61
|
+
? encodeInteger(value, encoding ?? 'int64')
|
|
62
|
+
: value instanceof ArrayBuffer
|
|
63
|
+
? value
|
|
64
|
+
: typeof value === 'string'
|
|
65
|
+
? encodeString(value, encoding ?? 'utf8')
|
|
66
|
+
: Array.isArray(value)
|
|
67
|
+
? value.map((i) => encode(i, encoding)).reduce((acc, val) => concatBuffers(acc, val))
|
|
68
|
+
: typeof value === 'object'
|
|
69
|
+
? Object.entries(value).map(([k, v]) => concatBuffers(encode(k, encoding), encode(v, encoding))).reduce((acc, val) => concatBuffers(acc, val))
|
|
70
|
+
: new Uint8Array(0).buffer;
|
|
71
|
+
export const format = (value, radix, separator = '') => value == null
|
|
72
|
+
? 'null'
|
|
73
|
+
: typeof value === 'boolean'
|
|
74
|
+
? value.toString()
|
|
75
|
+
: value instanceof Date
|
|
76
|
+
? formatTimestamp(value, radix ? Number(radix) : undefined)
|
|
77
|
+
: typeof value === 'number'
|
|
78
|
+
? formatFloat(value, radix ? Number(radix) : undefined)
|
|
79
|
+
: typeof value === 'bigint'
|
|
80
|
+
? value.toString(radix ? Number(radix) : undefined)
|
|
81
|
+
: value instanceof ArrayBuffer
|
|
82
|
+
? formatBuffer(value)
|
|
83
|
+
: typeof value === 'string'
|
|
84
|
+
? value
|
|
85
|
+
: Array.isArray(value)
|
|
86
|
+
? value.map((i) => format(i, radix)).join(separator)
|
|
87
|
+
: typeof value === 'object'
|
|
88
|
+
? Object.entries(value).map(([k, v]) => `${format(k)}${radix}${format(v)}`).join(separator)
|
|
89
|
+
: 'function';
|
|
90
|
+
const typeEquator = Type.functionType(Type.Boolean, [Type.Unknown, Type.Unknown]);
|
|
91
|
+
export const funcCoalesce = new Constant((value, valueOtherwise) => value ?? valueOtherwise, Type.union(Type.functionType(Type.Unknown, [Type.Unknown, Type.Unknown]), Type.functionType(Type.OptionalFloat, [Type.OptionalFloat, Type.OptionalFloat]), Type.functionType(Type.OptionalBoolean, [Type.OptionalBoolean, Type.OptionalBoolean]), Type.functionType(Type.OptionalTimestamp, [Type.OptionalTimestamp, Type.OptionalTimestamp]), Type.functionType(Type.OptionalInteger, [Type.OptionalInteger, Type.OptionalInteger]), Type.functionType(Type.OptionalBuffer, [Type.OptionalBuffer, Type.OptionalBuffer]), Type.functionType(Type.OptionalString, [Type.OptionalString, Type.OptionalString]), Type.functionType(Type.OptionalArray, [Type.OptionalArray, Type.OptionalArray]), Type.functionType(Type.OptionalObject, [Type.OptionalObject, Type.OptionalObject]), Type.functionType(Type.OptionalFunction, [Type.OptionalFunction, Type.OptionalFunction])));
|
|
92
|
+
export const funcEqual = new Constant((value1, value2) => equate(value1, value2), typeEquator);
|
|
93
|
+
export const funcNotEqual = new Constant((value1, value2) => !equate(value1, value2), typeEquator);
|
|
94
|
+
export const funcEncode = new Constant((value, encoding) => encode(value, encoding), Type.union(Type.functionType(Type.Buffer, [Type.Boolean]), Type.functionType(Type.Buffer, [Type.Timestamp, Type.OptionalString]), Type.functionType(Type.Buffer, [Type.Float, Type.OptionalString]), Type.functionType(Type.Buffer, [Type.Integer, Type.OptionalString]), Type.functionType(Type.Buffer, [Type.String, Type.OptionalString]), Type.functionType(Type.Buffer, [Type.Array, Type.OptionalString]), Type.functionType(Type.Buffer, [Type.Object, Type.OptionalString])));
|
|
95
|
+
export const funcFormat = new Constant((value, radix, separator = '') => format(value, radix == null ? undefined : Number(radix), separator), Type.union(Type.functionType(Type.String, [Type.Boolean]), Type.functionType(Type.String, [Type.Timestamp, Type.OptionalInteger]), Type.functionType(Type.String, [Type.Float, Type.OptionalInteger]), Type.functionType(Type.String, [Type.Integer, Type.OptionalInteger]), Type.functionType(Type.String, [Type.Buffer]), Type.functionType(Type.String, [Type.Array, Type.OptionalInteger, Type.OptionalString]), Type.functionType(Type.String, [Type.Object, Type.OptionalInteger, Type.OptionalString])));
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Constant } from '../../Constant.js';
|
|
2
|
+
import { Type } from '../../Type.js';
|
|
3
|
+
import { formatFloat } from '../Float.js';
|
|
4
|
+
import { formatBuffer } from '../Buffer.js';
|
|
5
|
+
export const formatAN = (value, whitespace) => {
|
|
6
|
+
if (value == null) {
|
|
7
|
+
return 'null';
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === 'boolean') {
|
|
10
|
+
return value.toString();
|
|
11
|
+
}
|
|
12
|
+
if (value instanceof Date) {
|
|
13
|
+
return `@${value.toISOString()}`;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === 'number') {
|
|
16
|
+
return formatFloat(value);
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'bigint') {
|
|
19
|
+
return value.toString();
|
|
20
|
+
}
|
|
21
|
+
if (value instanceof ArrayBuffer) {
|
|
22
|
+
return `#${formatBuffer(value)}`;
|
|
23
|
+
}
|
|
24
|
+
if (typeof value === 'string') {
|
|
25
|
+
return `"${value}"`;
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
const [prefix, suffix] = whitespace ? ['\n' + whitespace, '\n'] : ['', ''];
|
|
29
|
+
const lines = value.map((i) => `${prefix}${formatAN(i, whitespace).split('\n').join(prefix)}`);
|
|
30
|
+
return `[${lines.join(',')}${suffix}]`;
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === 'object') {
|
|
33
|
+
const [prefix, suffix] = whitespace ? ['\n' + whitespace, '\n'] : ['', ''];
|
|
34
|
+
const lines = Object.entries(value).map(([k, v]) => `${prefix}"${k}":${formatAN(v, whitespace).split('\n').join(prefix)}`);
|
|
35
|
+
return `[${lines.join(',')}${suffix}]`;
|
|
36
|
+
}
|
|
37
|
+
return 'function';
|
|
38
|
+
};
|
|
39
|
+
const funcFormatAN = new Constant((value, whitespace) => formatAN(value ?? null, whitespace), Type.functionType(Type.String, [Type.Unknown, Type.OptionalString]));
|
|
40
|
+
export const constAN = {
|
|
41
|
+
Format: funcFormatAN,
|
|
42
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Constant } from '../../Constant.js';
|
|
2
|
+
import { Type } from '../../Type.js';
|
|
3
|
+
const typeJson = Type.union(Type.Void, Type.Boolean, Type.Timestamp, Type.Float, Type.Integer, Type.String, Type.Array, Type.Object);
|
|
4
|
+
export const formatJSON = (value, whitespace) => JSON.stringify(value, (_key, value) => typeof value === 'bigint' ? value.toString() : value, whitespace);
|
|
5
|
+
const funcFormatJSON = new Constant((value, whitespace) => formatJSON(value ?? null, whitespace), Type.functionType(Type.String, [typeJson, Type.OptionalString]));
|
|
6
|
+
const funcParseJSON = new Constant((value) => JSON.parse(value), Type.functionType(typeJson, [Type.String]));
|
|
7
|
+
export const constJSON = {
|
|
8
|
+
Format: funcFormatJSON,
|
|
9
|
+
Parse: funcParseJSON,
|
|
10
|
+
};
|
package/dst/index.d.ts
ADDED
package/dst/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class ArrayNode extends Node {
|
|
6
|
+
protected _subnodes: Node[];
|
|
7
|
+
constructor(frame: ParserFrame, _subnodes: Node[]);
|
|
8
|
+
get type(): Type;
|
|
9
|
+
compile(type: Type): Node;
|
|
10
|
+
evaluate(): Value;
|
|
11
|
+
toString(ident?: number): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Constant } from '../Constant.js';
|
|
3
|
+
import { ConstantNode } from './ConstantNode.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export class ArrayNode extends Node {
|
|
6
|
+
_subnodes;
|
|
7
|
+
constructor(frame, _subnodes) {
|
|
8
|
+
super(frame);
|
|
9
|
+
this._subnodes = _subnodes;
|
|
10
|
+
}
|
|
11
|
+
get type() {
|
|
12
|
+
return Type.Array;
|
|
13
|
+
}
|
|
14
|
+
compile(type) {
|
|
15
|
+
this.reduceType(type);
|
|
16
|
+
let constant = true;
|
|
17
|
+
for (let i = 0; i < this._subnodes.length; ++i) {
|
|
18
|
+
this._subnodes[i] = this._subnodes[i].compile(Type.Unknown);
|
|
19
|
+
constant &&= this._subnodes[i].constant;
|
|
20
|
+
}
|
|
21
|
+
return constant ? new ConstantNode(this, new Constant(this.evaluate(), this.type)) : this;
|
|
22
|
+
}
|
|
23
|
+
evaluate() {
|
|
24
|
+
return this._subnodes.map((s) => s.evaluate());
|
|
25
|
+
}
|
|
26
|
+
toString(ident = 0) {
|
|
27
|
+
const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
|
|
28
|
+
return `${super.toString(ident)} array node subnodes:\n${subnodes}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class BlockNode extends Node {
|
|
6
|
+
protected _subnodes: Node[];
|
|
7
|
+
constructor(frame: ParserFrame, _subnodes: Node[]);
|
|
8
|
+
get type(): Type;
|
|
9
|
+
compile(type: Type): Node;
|
|
10
|
+
evaluate(): Value;
|
|
11
|
+
toString(ident?: number): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Constant } from '../Constant.js';
|
|
3
|
+
import { ConstantNode } from './ConstantNode.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export class BlockNode extends Node {
|
|
6
|
+
_subnodes;
|
|
7
|
+
constructor(frame, _subnodes) {
|
|
8
|
+
super(frame);
|
|
9
|
+
this._subnodes = _subnodes;
|
|
10
|
+
}
|
|
11
|
+
get type() {
|
|
12
|
+
return this._subnodes[this._subnodes.length - 1].type;
|
|
13
|
+
}
|
|
14
|
+
compile(type) {
|
|
15
|
+
let constant = true;
|
|
16
|
+
for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) {
|
|
17
|
+
this._subnodes[i] = this._subnodes[i].compile(i < last ? Type.Unknown : type);
|
|
18
|
+
constant &&= this._subnodes[i].constant;
|
|
19
|
+
}
|
|
20
|
+
return constant ? new ConstantNode(this, new Constant(this.evaluate(), this.type)) : this;
|
|
21
|
+
}
|
|
22
|
+
evaluate() {
|
|
23
|
+
return this._subnodes.map((s) => s.evaluate())[this._subnodes.length - 1];
|
|
24
|
+
}
|
|
25
|
+
toString(ident = 0) {
|
|
26
|
+
const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
|
|
27
|
+
return `${super.toString(ident)} block node subnodes:\n${subnodes}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class CallNode extends Node {
|
|
6
|
+
protected _fnode: Node;
|
|
7
|
+
protected _subnodes: Node[];
|
|
8
|
+
protected _type: Type;
|
|
9
|
+
constructor(frame: ParserFrame, _fnode: Node, _subnodes: Node[]);
|
|
10
|
+
get type(): Type;
|
|
11
|
+
compile(type: Type): Node;
|
|
12
|
+
evaluate(): Value;
|
|
13
|
+
toString(ident?: number): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Constant } from '../Constant.js';
|
|
3
|
+
import { ConstantNode } from './ConstantNode.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export class CallNode extends Node {
|
|
6
|
+
_fnode;
|
|
7
|
+
_subnodes;
|
|
8
|
+
_type;
|
|
9
|
+
constructor(frame, _fnode, _subnodes) {
|
|
10
|
+
super(frame);
|
|
11
|
+
this._fnode = _fnode;
|
|
12
|
+
this._subnodes = _subnodes;
|
|
13
|
+
this._type = this._fnode.type.mergeFunctionAtom(Type.Unknown, _subnodes.length)?.retType ?? Type.Unknown;
|
|
14
|
+
}
|
|
15
|
+
get type() {
|
|
16
|
+
return this._type;
|
|
17
|
+
}
|
|
18
|
+
compile(type) {
|
|
19
|
+
this._fnode = this._fnode.compile(this._fnode.type);
|
|
20
|
+
const mergedFunctionAtom = this._fnode.type.mergeFunctionAtom(type, this._subnodes.length);
|
|
21
|
+
if (!mergedFunctionAtom) {
|
|
22
|
+
this.throwError(`function type ${this._fnode.type} mismatch with expected return type ${type} and ${this._subnodes.length} arguments`);
|
|
23
|
+
}
|
|
24
|
+
this._type = mergedFunctionAtom.retType;
|
|
25
|
+
if (this._subnodes.length < mergedFunctionAtom.minArity) {
|
|
26
|
+
this.throwError(`function requires ${mergedFunctionAtom.minArity} arguments not ${this._subnodes.length}`);
|
|
27
|
+
}
|
|
28
|
+
if (this._subnodes.length > mergedFunctionAtom.maxArity) {
|
|
29
|
+
this.throwError(`function requires ${mergedFunctionAtom.maxArity} arguments not ${this._subnodes.length}`);
|
|
30
|
+
}
|
|
31
|
+
let constant = this._fnode.constant;
|
|
32
|
+
for (let i = 0; i < this._subnodes.length; ++i) {
|
|
33
|
+
this._subnodes[i] = this._subnodes[i].compile(mergedFunctionAtom.argType(i));
|
|
34
|
+
constant &&= this._subnodes[i].constant;
|
|
35
|
+
}
|
|
36
|
+
return constant ? new ConstantNode(this, new Constant(this.evaluate(), this.type)) : this;
|
|
37
|
+
}
|
|
38
|
+
evaluate() {
|
|
39
|
+
const func = this._fnode.evaluate();
|
|
40
|
+
if (typeof func !== 'function') {
|
|
41
|
+
this.throwError(`function expected not ${Type.of(func)}`);
|
|
42
|
+
}
|
|
43
|
+
return func(...this._subnodes.map((node) => node.evaluate()));
|
|
44
|
+
}
|
|
45
|
+
toString(ident = 0) {
|
|
46
|
+
const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
|
|
47
|
+
return `${super.toString(ident)} invocation node fnode:\n${this._fnode.toString(ident + 1)}\n`
|
|
48
|
+
+ `${super.toString(ident)} invocation node subnodes:\n${subnodes}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Constant } from '../Constant.js';
|
|
4
|
+
import { Value } from '../Value.js';
|
|
5
|
+
import { Type } from '../Type.js';
|
|
6
|
+
export declare class ConstantNode extends Node {
|
|
7
|
+
protected readonly _constant: Constant;
|
|
8
|
+
protected _subnode?: Node | undefined;
|
|
9
|
+
constructor(frame: ParserFrame, _constant: Constant, _subnode?: Node | undefined);
|
|
10
|
+
get type(): Type;
|
|
11
|
+
compile(type: Type): Node;
|
|
12
|
+
evaluate(): Value;
|
|
13
|
+
get constant(): boolean;
|
|
14
|
+
toString(ident?: number): string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
export class ConstantNode extends Node {
|
|
3
|
+
_constant;
|
|
4
|
+
_subnode;
|
|
5
|
+
constructor(frame, _constant, _subnode) {
|
|
6
|
+
super(frame);
|
|
7
|
+
this._constant = _constant;
|
|
8
|
+
this._subnode = _subnode;
|
|
9
|
+
}
|
|
10
|
+
get type() {
|
|
11
|
+
return this._constant.type;
|
|
12
|
+
}
|
|
13
|
+
compile(type) {
|
|
14
|
+
this._constant.type = this.reduceType(type);
|
|
15
|
+
this._subnode = this._subnode?.compile(this._constant.type.mergeFunctionAtomRetType(type));
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
evaluate() {
|
|
19
|
+
return this._constant.value;
|
|
20
|
+
}
|
|
21
|
+
get constant() {
|
|
22
|
+
return !this._subnode && this._constant.deterministic;
|
|
23
|
+
}
|
|
24
|
+
toString(ident = 0) {
|
|
25
|
+
return this._subnode
|
|
26
|
+
? `${super.toString(ident)} constant node subnode:\n${this._subnode.toString(ident + 1)}`
|
|
27
|
+
: `${super.toString(ident)} constant node`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class LoopNode extends Node {
|
|
6
|
+
protected _cnode: Node;
|
|
7
|
+
protected _subnode: Node;
|
|
8
|
+
constructor(frame: ParserFrame, _cnode: Node, _subnode: Node);
|
|
9
|
+
get type(): Type;
|
|
10
|
+
compile(type: Type): Node;
|
|
11
|
+
evaluate(): Value;
|
|
12
|
+
toString(ident?: number): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Type } from '../Type.js';
|
|
3
|
+
export class LoopNode extends Node {
|
|
4
|
+
_cnode;
|
|
5
|
+
_subnode;
|
|
6
|
+
constructor(frame, _cnode, _subnode) {
|
|
7
|
+
super(frame);
|
|
8
|
+
this._cnode = _cnode;
|
|
9
|
+
this._subnode = _subnode;
|
|
10
|
+
}
|
|
11
|
+
get type() {
|
|
12
|
+
return this._subnode.type;
|
|
13
|
+
}
|
|
14
|
+
compile(type) {
|
|
15
|
+
this._cnode = this._cnode.compile(Type.Boolean);
|
|
16
|
+
this._subnode = this._subnode.compile(type);
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
evaluate() {
|
|
20
|
+
let value;
|
|
21
|
+
while (this._cnode.evaluate()) {
|
|
22
|
+
value = this._subnode.evaluate();
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
toString(ident = 0) {
|
|
27
|
+
return `${super.toString(ident)} loop node cnode:\n${this._cnode.toString(ident + 1)}\n`
|
|
28
|
+
+ `${super.toString(ident)} subnode:\n${this._subnode.toString(ident + 1)}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class ObjectNode extends Node {
|
|
6
|
+
protected _subnodes: [Node, Node][];
|
|
7
|
+
protected _type: Type;
|
|
8
|
+
constructor(frame: ParserFrame, _subnodes: [Node, Node][]);
|
|
9
|
+
get type(): Type;
|
|
10
|
+
compile(type: Type): Node;
|
|
11
|
+
evaluate(): Value;
|
|
12
|
+
toString(ident?: number): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Constant } from '../Constant.js';
|
|
3
|
+
import { ConstantNode } from './ConstantNode.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export class ObjectNode extends Node {
|
|
6
|
+
_subnodes;
|
|
7
|
+
_type;
|
|
8
|
+
constructor(frame, _subnodes) {
|
|
9
|
+
super(frame);
|
|
10
|
+
this._subnodes = _subnodes;
|
|
11
|
+
this._type = Type.Object;
|
|
12
|
+
}
|
|
13
|
+
get type() {
|
|
14
|
+
return this._type;
|
|
15
|
+
}
|
|
16
|
+
compile(type) {
|
|
17
|
+
this._type = this.reduceType(type);
|
|
18
|
+
let constant = true;
|
|
19
|
+
for (let i = 0; i < this._subnodes.length; ++i) {
|
|
20
|
+
this._subnodes[i][0] = this._subnodes[i][0].compile(Type.String);
|
|
21
|
+
constant &&= this._subnodes[i][0].constant;
|
|
22
|
+
this._subnodes[i][1] = this._subnodes[i][1].compile(Type.Unknown);
|
|
23
|
+
constant &&= this._subnodes[i][1].constant;
|
|
24
|
+
}
|
|
25
|
+
return constant ? new ConstantNode(this, new Constant(this.evaluate(), this.type)) : this;
|
|
26
|
+
}
|
|
27
|
+
evaluate() {
|
|
28
|
+
const obj = {};
|
|
29
|
+
for (const [key, value] of this._subnodes) {
|
|
30
|
+
obj[key.evaluate()] = value.evaluate();
|
|
31
|
+
}
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
toString(ident = 0) {
|
|
35
|
+
const subnodes = this._subnodes.map(([k, v]) => `${k.toString(ident + 1)}:\n${v.toString(ident + 1)}`).join('\n');
|
|
36
|
+
return `${super.toString(ident)} object node subnodes:\n${subnodes}`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Value } from '../Value.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export declare class SwitchNode extends Node {
|
|
6
|
+
protected _cnode: Node;
|
|
7
|
+
protected _subnodes: Node[];
|
|
8
|
+
protected _type: Type;
|
|
9
|
+
constructor(frame: ParserFrame, _cnode: Node, _subnodes: Node[]);
|
|
10
|
+
get type(): Type;
|
|
11
|
+
compile(type: Type): Node;
|
|
12
|
+
evaluate(): Value;
|
|
13
|
+
toString(ident?: number): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { Constant } from '../Constant.js';
|
|
3
|
+
import { ConstantNode } from './ConstantNode.js';
|
|
4
|
+
import { Type } from '../Type.js';
|
|
5
|
+
export class SwitchNode extends Node {
|
|
6
|
+
_cnode;
|
|
7
|
+
_subnodes;
|
|
8
|
+
_type;
|
|
9
|
+
constructor(frame, _cnode, _subnodes) {
|
|
10
|
+
super(frame);
|
|
11
|
+
this._cnode = _cnode;
|
|
12
|
+
this._subnodes = _subnodes;
|
|
13
|
+
if (_subnodes.length !== 2) {
|
|
14
|
+
this.throwError(`switch requires 2 subnodes not ${_subnodes.length}`);
|
|
15
|
+
}
|
|
16
|
+
this._type = Type.union(...this._subnodes.map((i) => i.type));
|
|
17
|
+
}
|
|
18
|
+
get type() {
|
|
19
|
+
return Type.union(...this._subnodes.map((i) => i.type));
|
|
20
|
+
}
|
|
21
|
+
compile(type) {
|
|
22
|
+
this._cnode = this._cnode.compile(Type.Boolean);
|
|
23
|
+
this._type = type;
|
|
24
|
+
let constant = this._cnode.constant;
|
|
25
|
+
for (let i = 0; i < this._subnodes.length; ++i) {
|
|
26
|
+
this._subnodes[i] = this._subnodes[i].compile(type);
|
|
27
|
+
constant &&= this._subnodes[i].constant;
|
|
28
|
+
}
|
|
29
|
+
return constant
|
|
30
|
+
? new ConstantNode(this, new Constant(this.evaluate(), this.type))
|
|
31
|
+
: this;
|
|
32
|
+
}
|
|
33
|
+
evaluate() {
|
|
34
|
+
return this._cnode.evaluate() ? this._subnodes[0].evaluate() : this._subnodes[1].evaluate();
|
|
35
|
+
}
|
|
36
|
+
toString(ident = 0) {
|
|
37
|
+
const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
|
|
38
|
+
return `${super.toString(ident)} switch node cnode:\n${this._cnode.toString(ident + 1)}\n`
|
|
39
|
+
+ `${super.toString(ident)} switch node subnodes:\n${subnodes}`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Node } from '../Node.js';
|
|
2
|
+
import { ParserFrame } from '../ParserFrame.js';
|
|
3
|
+
import { Variable } from '../Variable.js';
|
|
4
|
+
import { Value } from '../Value.js';
|
|
5
|
+
import { Type } from '../Type.js';
|
|
6
|
+
export declare class VariableNode extends Node {
|
|
7
|
+
protected readonly _variable: Variable;
|
|
8
|
+
protected _subnode?: Node | undefined;
|
|
9
|
+
constructor(frame: ParserFrame, _variable: Variable, _subnode?: Node | undefined);
|
|
10
|
+
get type(): Type;
|
|
11
|
+
compile(type: Type): Node;
|
|
12
|
+
evaluate(): Value;
|
|
13
|
+
toString(ident?: number): string;
|
|
14
|
+
}
|