@nova-energy/openamx 0.19.0 → 0.22.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/README.md +12 -3
- package/dist/cli.js +61 -0
- package/dist/index.js +1 -0
- package/dist/parser/parseDocument.js +26 -13
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/runtime/evaluateDocument.js +9 -14
- package/dist/runtime/evaluateExpression.js +247 -12
- package/dist/runtime/inputData.js +73 -20
- package/dist/runtime/moduleLoader.js +29 -14
- package/dist/runtime/typescriptBridge.js +171 -0
- package/dist/transpiler/emit.js +987 -0
- package/dist/transpiler/filesystemGraph.js +267 -0
- package/dist/transpiler/graph.js +201 -0
- package/dist/transpiler/index.js +52 -0
- package/dist/transpiler/outputWriter.js +378 -0
- package/dist/transpiler/source.js +16 -0
- package/dist/transpiler/types.js +1 -0
- package/dist/transpilerRuntime.js +684 -0
- package/dist/typechecker/checkDocument.js +65 -3
- package/dist/typechecker/typescriptAnalysis.js +372 -0
- package/package.json +3 -2
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import { isMeasurement, measurement, measurementDisplayValue, measurementPhysicalValue } from './runtime/measurement';
|
|
2
|
+
import { AmxError, staticError } from './diagnostics/errors';
|
|
3
|
+
import { evaluateBinaryValues, evaluateBuiltinValues, evaluateIterableValue, evaluateListAddition, evaluateListItem, evaluateListRemoval, evaluateRangeValues, evaluateUnaryValue } from './runtime/evaluateExpression';
|
|
4
|
+
export class AMXRuntimeError extends Error {
|
|
5
|
+
diagnostic;
|
|
6
|
+
code;
|
|
7
|
+
file;
|
|
8
|
+
line;
|
|
9
|
+
column;
|
|
10
|
+
inputPath;
|
|
11
|
+
originalCause;
|
|
12
|
+
constructor(diagnostic, cause) {
|
|
13
|
+
super(diagnostic.message);
|
|
14
|
+
this.name = 'AMXRuntimeError';
|
|
15
|
+
this.diagnostic = Object.freeze({ ...diagnostic });
|
|
16
|
+
this.code = diagnostic.code;
|
|
17
|
+
this.file = diagnostic.file;
|
|
18
|
+
this.line = diagnostic.line;
|
|
19
|
+
this.column = diagnostic.column;
|
|
20
|
+
this.inputPath = diagnostic.inputPath;
|
|
21
|
+
this.originalCause = cause;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class AMXInputError extends TypeError {
|
|
25
|
+
inputPath;
|
|
26
|
+
expected;
|
|
27
|
+
actual;
|
|
28
|
+
code = 'AMX4003';
|
|
29
|
+
diagnostic;
|
|
30
|
+
constructor(inputPath, expected, actual, sourceContext = {}) {
|
|
31
|
+
super(`Invalid input at '${inputPath}': expected ${expected}, got ${actual}`);
|
|
32
|
+
this.inputPath = inputPath;
|
|
33
|
+
this.expected = expected;
|
|
34
|
+
this.actual = actual;
|
|
35
|
+
this.name = 'AMXInputError';
|
|
36
|
+
this.diagnostic = Object.freeze({
|
|
37
|
+
code: this.code,
|
|
38
|
+
message: this.message,
|
|
39
|
+
...sourceContext,
|
|
40
|
+
inputPath
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function amxBinary(operator, left, right, context) {
|
|
45
|
+
return withRuntimeErrorContext(() => evaluateBinaryValues(operator, left, right, sourceLocation(context), context?.file), context);
|
|
46
|
+
}
|
|
47
|
+
export function amxUnary(operator, value, context) {
|
|
48
|
+
return withRuntimeErrorContext(() => evaluateUnaryValue(operator, value, sourceLocation(context), context?.file), context);
|
|
49
|
+
}
|
|
50
|
+
export function amxFormatValue(value, context) {
|
|
51
|
+
if (value === null)
|
|
52
|
+
return 'null';
|
|
53
|
+
if (typeof value === 'string')
|
|
54
|
+
return value;
|
|
55
|
+
if (typeof value === 'number')
|
|
56
|
+
return String(value);
|
|
57
|
+
if (typeof value === 'boolean')
|
|
58
|
+
return value ? 'true' : 'false';
|
|
59
|
+
if (isMeasurement(value))
|
|
60
|
+
return `${String(value.value)} ${value.unit.text}`;
|
|
61
|
+
return withRuntimeErrorContext(() => staticError('AMX3007', 'Only String, Number, Boolean, measurement, and null values can be interpolated', sourceLocation(context), context?.file), context);
|
|
62
|
+
}
|
|
63
|
+
export function amxBuiltin(name, args, context) {
|
|
64
|
+
return withRuntimeErrorContext(() => evaluateBuiltinValues(name, args, sourceLocation(context), context?.file, createDimensionRegistry(context?.unitDescriptors)), context);
|
|
65
|
+
}
|
|
66
|
+
export function amxIsType(value, descriptor) {
|
|
67
|
+
return matchesInputDescriptor(value, descriptor);
|
|
68
|
+
}
|
|
69
|
+
export function validateAMXValue(value, descriptor, context) {
|
|
70
|
+
const rootPath = context.inputPath ?? '';
|
|
71
|
+
return validateInputValue(value, descriptor, rootPath, context);
|
|
72
|
+
}
|
|
73
|
+
export function amxGet1Based(list, index, context) {
|
|
74
|
+
return withRuntimeErrorContext(() => evaluateListItem(list, index, sourceLocation(context), context?.file), context);
|
|
75
|
+
}
|
|
76
|
+
export function amxRange(start, end, context) {
|
|
77
|
+
return withRuntimeErrorContext(() => evaluateRangeValues(start, end, sourceLocation(context), context?.file), context);
|
|
78
|
+
}
|
|
79
|
+
export function amxAdd1Based(list, value, position, context) {
|
|
80
|
+
withRuntimeErrorContext(() => evaluateListAddition(list, value, position, context?.mutable !== false, context?.targetName ?? '__generated', sourceLocation(context), context?.file), context);
|
|
81
|
+
}
|
|
82
|
+
export function amxRemove1Based(list, count, position, context) {
|
|
83
|
+
withRuntimeErrorContext(() => evaluateListRemoval(list, count, position, context?.mutable !== false, context?.targetName ?? '__generated', sourceLocation(context), context?.file), context);
|
|
84
|
+
}
|
|
85
|
+
export function amxIterate(value, context) {
|
|
86
|
+
return withRuntimeErrorContext(() => evaluateIterableValue(value, sourceLocation(context), context?.file), context);
|
|
87
|
+
}
|
|
88
|
+
export function amxCopyListArgument(value) {
|
|
89
|
+
return copyArrayTree(value);
|
|
90
|
+
}
|
|
91
|
+
export function requireAMXNumber(value, inputPath) {
|
|
92
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
93
|
+
throw new AMXInputError(inputPath, 'finite Number', describeInput(value));
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
export function requireAMXString(value, inputPath) {
|
|
98
|
+
if (typeof value !== 'string') {
|
|
99
|
+
throw new AMXInputError(inputPath, 'String', describeInput(value));
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
export function requireAMXBoolean(value, inputPath) {
|
|
104
|
+
if (typeof value !== 'boolean') {
|
|
105
|
+
throw new AMXInputError(inputPath, 'Boolean', describeInput(value));
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
export function requireAMXEnum(value, enumName, members, inputPath) {
|
|
110
|
+
if ((typeof value !== 'string' && typeof value !== 'number') || !members.includes(value)) {
|
|
111
|
+
throw new AMXInputError(inputPath, `member of enum '${enumName}'`, describeInput(value));
|
|
112
|
+
}
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
export function requireAMXMeasurement(value, dimensionName, dimensionVector, units, inputPath) {
|
|
116
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
117
|
+
throw new AMXInputError(inputPath, `Measurement<${dimensionName}> as {value, unit}`, describeInput(value));
|
|
118
|
+
}
|
|
119
|
+
const record = value;
|
|
120
|
+
const keys = Object.keys(record);
|
|
121
|
+
if (keys.length !== 2 || !keys.includes('value') || !keys.includes('unit')) {
|
|
122
|
+
throw new AMXInputError(inputPath, `exact {value, unit} Measurement<${dimensionName}>`, describeInput(value));
|
|
123
|
+
}
|
|
124
|
+
if (typeof record.unit !== 'string') {
|
|
125
|
+
throw new AMXInputError(`${inputPath}/unit`, 'unit name', describeInput(record.unit));
|
|
126
|
+
}
|
|
127
|
+
const unit = units[record.unit];
|
|
128
|
+
if (!unit) {
|
|
129
|
+
throw new AMXInputError(`${inputPath}/unit`, `visible ${dimensionName} unit`, record.unit);
|
|
130
|
+
}
|
|
131
|
+
if (!sameVector(unit.vector, dimensionVector)) {
|
|
132
|
+
throw new AMXInputError(`${inputPath}/unit`, `unit of dimension ${dimensionName}`, record.unit);
|
|
133
|
+
}
|
|
134
|
+
const numericValue = requireAMXNumber(record.value, `${inputPath}/value`);
|
|
135
|
+
return amxAttach(numericValue, unit);
|
|
136
|
+
}
|
|
137
|
+
export function requireAMXList(value, inputPath) {
|
|
138
|
+
if (!Array.isArray(value)) {
|
|
139
|
+
throw new AMXInputError(inputPath, 'list', describeInput(value));
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
export function requireAMXRecord(value, inputPath, allowedFields) {
|
|
144
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
145
|
+
throw new AMXInputError(inputPath, 'record object', describeInput(value));
|
|
146
|
+
}
|
|
147
|
+
const record = value;
|
|
148
|
+
const allowed = new Set(allowedFields);
|
|
149
|
+
const unknownField = Object.keys(record).find(field => !allowed.has(field));
|
|
150
|
+
if (unknownField !== undefined) {
|
|
151
|
+
throw new AMXInputError(`${inputPath}/${escapePointer(unknownField)}`, 'declared record field', describeInput(record[unknownField]));
|
|
152
|
+
}
|
|
153
|
+
return record;
|
|
154
|
+
}
|
|
155
|
+
export function amxAttach(value, unit) {
|
|
156
|
+
validateUnit(unit);
|
|
157
|
+
const result = measurement(requireAMXNumber(value, 'measurement.value'), toMeasurementUnit(unit));
|
|
158
|
+
if (!Number.isFinite(measurementPhysicalValue(result))) {
|
|
159
|
+
throw new RangeError('Measurement physical value must be finite');
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
export function amxConvert(value, target) {
|
|
164
|
+
validateUnit(target);
|
|
165
|
+
if (!isMeasurement(value) || !sameVector(value.unit.vector, target.vector)) {
|
|
166
|
+
throw new TypeError(`Cannot convert measurement to unit '${target.name}'`);
|
|
167
|
+
}
|
|
168
|
+
const targetUnit = toMeasurementUnit(target);
|
|
169
|
+
const converted = measurementDisplayValue(measurementPhysicalValue(value), targetUnit);
|
|
170
|
+
if (!Number.isFinite(converted)) {
|
|
171
|
+
throw new RangeError('Converted measurement value must be finite');
|
|
172
|
+
}
|
|
173
|
+
return measurement(converted, targetUnit);
|
|
174
|
+
}
|
|
175
|
+
export function amxPhysicalValue(value) {
|
|
176
|
+
return measurementPhysicalValue(value);
|
|
177
|
+
}
|
|
178
|
+
export function amxEqual(left, right) {
|
|
179
|
+
return isMeasurement(left) && isMeasurement(right)
|
|
180
|
+
&& sameVector(left.unit.vector, right.unit.vector)
|
|
181
|
+
&& measurementPhysicalValue(left) === measurementPhysicalValue(right);
|
|
182
|
+
}
|
|
183
|
+
export function amxTableView(name, columns, rows, title = name, description = '') {
|
|
184
|
+
const copiedColumns = columns.map((column, index) => ({
|
|
185
|
+
field: requireViewString(column.field, `/columns/${index}/field`),
|
|
186
|
+
label: requireViewString(column.label, `/columns/${index}/label`)
|
|
187
|
+
}));
|
|
188
|
+
const copiedRows = rows.map((row, rowIndex) => row.map((value, columnIndex) => cloneViewData(value, `/rows/${rowIndex}/${columnIndex}`)));
|
|
189
|
+
return deepFreeze({
|
|
190
|
+
kind: 'table',
|
|
191
|
+
name,
|
|
192
|
+
title,
|
|
193
|
+
description,
|
|
194
|
+
columns: copiedColumns,
|
|
195
|
+
rows: copiedRows
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
export function amxChartView(view) {
|
|
199
|
+
if (!['bar', 'column', 'line', 'scatter'].includes(view.chartKind)) {
|
|
200
|
+
throwViewInputError('/chartKind', 'supported chart kind', String(view.chartKind));
|
|
201
|
+
}
|
|
202
|
+
if (typeof view.showTable !== 'boolean')
|
|
203
|
+
throwViewInputError('/showTable', 'Boolean', describeInput(view.showTable));
|
|
204
|
+
const copied = {
|
|
205
|
+
kind: 'chart',
|
|
206
|
+
name: requireViewString(view.name, '/name'),
|
|
207
|
+
chartKind: view.chartKind,
|
|
208
|
+
title: requireViewString(view.title, '/title'),
|
|
209
|
+
description: requireViewString(view.description, '/description'),
|
|
210
|
+
data: view.data.map((value, index) => cloneViewData(value, `/data/${index}`)),
|
|
211
|
+
...(view.labels === undefined ? {} : { labels: view.labels.map((label, index) => requireViewString(label, `/labels/${index}`)) }),
|
|
212
|
+
...(view.headings === undefined ? {} : { headings: view.headings.map((heading, index) => requireViewString(heading, `/headings/${index}`)) }),
|
|
213
|
+
...(view.measurementDescriptors === undefined ? {} : {
|
|
214
|
+
measurementDescriptors: view.measurementDescriptors.map((descriptor, index) => cloneChartMeasurementDescriptor(descriptor, index))
|
|
215
|
+
}),
|
|
216
|
+
showTable: view.showTable
|
|
217
|
+
};
|
|
218
|
+
return deepFreeze(copied);
|
|
219
|
+
}
|
|
220
|
+
export function amxDiagramView(view) {
|
|
221
|
+
if ('schema' in view && view.schema === 'structured-v2') {
|
|
222
|
+
const structured = view;
|
|
223
|
+
return deepFreeze({
|
|
224
|
+
kind: 'diagram',
|
|
225
|
+
schema: 'structured-v2',
|
|
226
|
+
name: requireViewString(structured.name, '/name'),
|
|
227
|
+
title: requireViewString(structured.title, '/title'),
|
|
228
|
+
description: requireViewString(structured.description, '/description'),
|
|
229
|
+
canvas: {
|
|
230
|
+
width: requireViewNumber(structured.canvas.width, '/canvas/width'),
|
|
231
|
+
height: requireViewNumber(structured.canvas.height, '/canvas/height'),
|
|
232
|
+
coordinates: structured.canvas.coordinates === 'bottom-left-y-up'
|
|
233
|
+
? 'bottom-left-y-up'
|
|
234
|
+
: throwViewInputError('/canvas/coordinates', 'bottom-left-y-up', String(structured.canvas.coordinates))
|
|
235
|
+
},
|
|
236
|
+
timeline: {
|
|
237
|
+
variable: requireViewString(structured.timeline.variable, '/timeline/variable'),
|
|
238
|
+
from: requireViewNumber(structured.timeline.from, '/timeline/from'),
|
|
239
|
+
to: requireViewNumber(structured.timeline.to, '/timeline/to'),
|
|
240
|
+
duration: requireViewNumber(structured.timeline.duration, '/timeline/duration')
|
|
241
|
+
},
|
|
242
|
+
initialFrame: {
|
|
243
|
+
time: requireViewNumber(structured.initialFrame.time, '/initialFrame/time'),
|
|
244
|
+
elements: structured.initialFrame.elements.map((element, index) => cloneDiagramPrimitive(element, index))
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const legacy = view;
|
|
249
|
+
return deepFreeze({
|
|
250
|
+
kind: 'diagram',
|
|
251
|
+
schema: 'legacy-v1',
|
|
252
|
+
name: requireViewString(legacy.name, '/name'),
|
|
253
|
+
title: requireViewString(legacy.title, '/title'),
|
|
254
|
+
description: requireViewString(legacy.description, '/description'),
|
|
255
|
+
canvas: {
|
|
256
|
+
width: requireViewNumber(legacy.canvas.width, '/canvas/width'),
|
|
257
|
+
height: requireViewNumber(legacy.canvas.height, '/canvas/height'),
|
|
258
|
+
coordinates: legacy.canvas.coordinates === 'bottom-left-y-up'
|
|
259
|
+
? 'bottom-left-y-up'
|
|
260
|
+
: throwViewInputError('/canvas/coordinates', 'bottom-left-y-up', String(legacy.canvas.coordinates))
|
|
261
|
+
},
|
|
262
|
+
timeline: {
|
|
263
|
+
variable: requireViewString(legacy.timeline.variable, '/timeline/variable'),
|
|
264
|
+
from: requireViewNumber(legacy.timeline.from, '/timeline/from'),
|
|
265
|
+
to: requireViewNumber(legacy.timeline.to, '/timeline/to'),
|
|
266
|
+
duration: requireViewNumber(legacy.timeline.duration, '/timeline/duration')
|
|
267
|
+
},
|
|
268
|
+
initialFrame: {
|
|
269
|
+
time: requireViewNumber(legacy.initialFrame.time, '/initialFrame/time'),
|
|
270
|
+
elements: legacy.initialFrame.elements.map((element, index) => ({
|
|
271
|
+
type: requireViewString(element.type, `/initialFrame/elements/${index}/type`),
|
|
272
|
+
name: requireViewString(element.name, `/initialFrame/elements/${index}/name`),
|
|
273
|
+
properties: cloneViewData(element.properties, `/initialFrame/elements/${index}/properties`)
|
|
274
|
+
}))
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
function requireViewString(value, path) {
|
|
279
|
+
if (typeof value !== 'string')
|
|
280
|
+
throwViewInputError(path, 'String', describeInput(value));
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
function requireViewNumber(value, path) {
|
|
284
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
285
|
+
throwViewInputError(path, 'finite Number', describeInput(value));
|
|
286
|
+
}
|
|
287
|
+
return value;
|
|
288
|
+
}
|
|
289
|
+
function throwViewInputError(path, expected, actual) {
|
|
290
|
+
throw new AMXInputError(path, expected, actual);
|
|
291
|
+
}
|
|
292
|
+
function cloneViewData(value, path, ancestors = new Set()) {
|
|
293
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
294
|
+
return value;
|
|
295
|
+
if (typeof value === 'number') {
|
|
296
|
+
if (!Number.isFinite(value))
|
|
297
|
+
throwViewInputError(path, 'finite JSON-like value', describeInput(value));
|
|
298
|
+
return value;
|
|
299
|
+
}
|
|
300
|
+
if (typeof value !== 'object' || isMeasurement(value)) {
|
|
301
|
+
throwViewInputError(path, 'AMX view data value', describeInput(value));
|
|
302
|
+
}
|
|
303
|
+
if (ancestors.has(value)) {
|
|
304
|
+
throw new AMXRuntimeError({
|
|
305
|
+
code: 'AMX3007',
|
|
306
|
+
message: 'Cyclic AMX view data is not supported'
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
ancestors.add(value);
|
|
310
|
+
try {
|
|
311
|
+
if (Array.isArray(value)) {
|
|
312
|
+
return value.map((item, index) => cloneViewData(item, `${path}/${index}`, ancestors));
|
|
313
|
+
}
|
|
314
|
+
const prototype = Object.getPrototypeOf(value);
|
|
315
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
316
|
+
throwViewInputError(path, 'plain AMX view data record', describeInput(value));
|
|
317
|
+
}
|
|
318
|
+
const copy = {};
|
|
319
|
+
for (const [field, item] of Object.entries(value)) {
|
|
320
|
+
copy[field] = cloneViewData(item, appendInputPath(path, field), ancestors);
|
|
321
|
+
}
|
|
322
|
+
return copy;
|
|
323
|
+
}
|
|
324
|
+
finally {
|
|
325
|
+
ancestors.delete(value);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function cloneChartMeasurementDescriptor(descriptor, index) {
|
|
329
|
+
const path = `/measurementDescriptors/${index}`;
|
|
330
|
+
if (!['series', 'x', 'y'].includes(descriptor.role)) {
|
|
331
|
+
throwViewInputError(`${path}/role`, 'series, x, or y', String(descriptor.role));
|
|
332
|
+
}
|
|
333
|
+
const unit = descriptor.unit;
|
|
334
|
+
if (!Number.isFinite(unit.scale) || unit.scale <= 0
|
|
335
|
+
|| Object.values(unit.vector).some(exponent => !Number.isFinite(exponent))) {
|
|
336
|
+
throwViewInputError(`${path}/unit`, 'valid display-unit descriptor', 'invalid descriptor');
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
role: descriptor.role,
|
|
340
|
+
...(descriptor.field === undefined ? {} : { field: requireViewString(descriptor.field, `${path}/field`) }),
|
|
341
|
+
label: requireViewString(descriptor.label, `${path}/label`),
|
|
342
|
+
unit: {
|
|
343
|
+
text: requireViewString(unit.text, `${path}/unit/text`),
|
|
344
|
+
scale: unit.scale,
|
|
345
|
+
vector: { ...unit.vector },
|
|
346
|
+
factors: unit.factors.map((factor, factorIndex) => ({
|
|
347
|
+
identity: requireViewString(factor.identity, `${path}/unit/factors/${factorIndex}/identity`),
|
|
348
|
+
name: requireViewString(factor.name, `${path}/unit/factors/${factorIndex}/name`),
|
|
349
|
+
exponent: requireViewNumber(factor.exponent, `${path}/unit/factors/${factorIndex}/exponent`)
|
|
350
|
+
}))
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function cloneDiagramPrimitive(element, index) {
|
|
355
|
+
const path = `/initialFrame/elements/${index}`;
|
|
356
|
+
switch (element.primitive) {
|
|
357
|
+
case 'line':
|
|
358
|
+
if (!['none', 'start', 'end', 'both'].includes(element.arrow)) {
|
|
359
|
+
throwViewInputError(`${path}/arrow`, 'none, start, end, or both', String(element.arrow));
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
primitive: 'line', name: requireViewString(element.name, `${path}/name`),
|
|
363
|
+
x1: requireViewNumber(element.x1, `${path}/x1`), y1: requireViewNumber(element.y1, `${path}/y1`),
|
|
364
|
+
x2: requireViewNumber(element.x2, `${path}/x2`), y2: requireViewNumber(element.y2, `${path}/y2`),
|
|
365
|
+
stroke: requireViewString(element.stroke, `${path}/stroke`),
|
|
366
|
+
lineWidth: requireViewNumber(element.lineWidth, `${path}/lineWidth`), arrow: element.arrow
|
|
367
|
+
};
|
|
368
|
+
case 'rectangle':
|
|
369
|
+
return {
|
|
370
|
+
primitive: 'rectangle', name: requireViewString(element.name, `${path}/name`),
|
|
371
|
+
x: requireViewNumber(element.x, `${path}/x`), y: requireViewNumber(element.y, `${path}/y`),
|
|
372
|
+
width: requireViewNumber(element.width, `${path}/width`), height: requireViewNumber(element.height, `${path}/height`),
|
|
373
|
+
stroke: requireViewString(element.stroke, `${path}/stroke`),
|
|
374
|
+
...(element.fill === undefined ? {} : { fill: requireViewString(element.fill, `${path}/fill`) }),
|
|
375
|
+
lineWidth: requireViewNumber(element.lineWidth, `${path}/lineWidth`)
|
|
376
|
+
};
|
|
377
|
+
case 'circle':
|
|
378
|
+
return {
|
|
379
|
+
primitive: 'circle', name: requireViewString(element.name, `${path}/name`),
|
|
380
|
+
cx: requireViewNumber(element.cx, `${path}/cx`), cy: requireViewNumber(element.cy, `${path}/cy`),
|
|
381
|
+
r: requireViewNumber(element.r, `${path}/r`), stroke: requireViewString(element.stroke, `${path}/stroke`),
|
|
382
|
+
...(element.fill === undefined ? {} : { fill: requireViewString(element.fill, `${path}/fill`) }),
|
|
383
|
+
lineWidth: requireViewNumber(element.lineWidth, `${path}/lineWidth`)
|
|
384
|
+
};
|
|
385
|
+
case 'text':
|
|
386
|
+
return {
|
|
387
|
+
primitive: 'text', name: requireViewString(element.name, `${path}/name`),
|
|
388
|
+
x: requireViewNumber(element.x, `${path}/x`), y: requireViewNumber(element.y, `${path}/y`),
|
|
389
|
+
value: requireViewString(element.value, `${path}/value`), fill: requireViewString(element.fill, `${path}/fill`)
|
|
390
|
+
};
|
|
391
|
+
default:
|
|
392
|
+
throwViewInputError(`${path}/primitive`, 'supported diagram primitive', String(element.primitive));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function deepFreeze(value) {
|
|
396
|
+
if (!value || typeof value !== 'object')
|
|
397
|
+
return value;
|
|
398
|
+
for (const child of Object.values(value))
|
|
399
|
+
deepFreeze(child);
|
|
400
|
+
return Object.freeze(value);
|
|
401
|
+
}
|
|
402
|
+
function createDimensionRegistry(descriptors) {
|
|
403
|
+
if (!descriptors)
|
|
404
|
+
return undefined;
|
|
405
|
+
const dimensions = new Map();
|
|
406
|
+
const units = new Map();
|
|
407
|
+
const baseUnits = new Map();
|
|
408
|
+
for (const descriptor of descriptors) {
|
|
409
|
+
validateUnit(descriptor);
|
|
410
|
+
const vector = new Map(Object.entries(descriptor.vector));
|
|
411
|
+
const unit = {
|
|
412
|
+
name: descriptor.name,
|
|
413
|
+
moduleIdentity: 'generated',
|
|
414
|
+
declarationName: descriptor.name,
|
|
415
|
+
identity: descriptor.identity,
|
|
416
|
+
vector,
|
|
417
|
+
scale: descriptor.scale,
|
|
418
|
+
offset: descriptor.offset
|
|
419
|
+
};
|
|
420
|
+
units.set(descriptor.name, unit);
|
|
421
|
+
for (const [identity, exponent] of vector) {
|
|
422
|
+
if (!dimensions.has(identity)) {
|
|
423
|
+
dimensions.set(identity, {
|
|
424
|
+
name: identity,
|
|
425
|
+
moduleIdentity: 'generated',
|
|
426
|
+
declarationName: identity,
|
|
427
|
+
baseIdentity: identity,
|
|
428
|
+
vector: new Map([[identity, 1]])
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
if (exponent === 1 && descriptor.scale === 1 && descriptor.offset === 0 && !baseUnits.has(identity)) {
|
|
432
|
+
baseUnits.set(identity, unit);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return { dimensions, units, baseUnits };
|
|
437
|
+
}
|
|
438
|
+
function matchesInputDescriptor(value, descriptor, ancestors = new Set()) {
|
|
439
|
+
switch (descriptor.kind) {
|
|
440
|
+
case 'number': return typeof value === 'number';
|
|
441
|
+
case 'string': return typeof value === 'string';
|
|
442
|
+
case 'boolean': return typeof value === 'boolean';
|
|
443
|
+
case 'dateTime': return typeof value === 'string' && validAMXDateTime(value);
|
|
444
|
+
case 'enum':
|
|
445
|
+
return (typeof value === 'string' || typeof value === 'number')
|
|
446
|
+
&& descriptor.members.some(member => typeof member === typeof value && member === value);
|
|
447
|
+
case 'measurement':
|
|
448
|
+
return isMeasurement(value)
|
|
449
|
+
&& sameVector(value.unit.vector, descriptor.dimensionVector);
|
|
450
|
+
case 'nullable':
|
|
451
|
+
return value === null || matchesInputDescriptor(value, descriptor.value, ancestors);
|
|
452
|
+
case 'list': {
|
|
453
|
+
if (!Array.isArray(value) || ancestors.has(value))
|
|
454
|
+
return false;
|
|
455
|
+
ancestors.add(value);
|
|
456
|
+
try {
|
|
457
|
+
return value.every(item => matchesInputDescriptor(item, descriptor.element, ancestors));
|
|
458
|
+
}
|
|
459
|
+
finally {
|
|
460
|
+
ancestors.delete(value);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
case 'record': {
|
|
464
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)
|
|
465
|
+
|| isMeasurement(value) || ancestors.has(value))
|
|
466
|
+
return false;
|
|
467
|
+
const record = value;
|
|
468
|
+
const fieldNames = new Set(descriptor.fields.map(field => field.name));
|
|
469
|
+
if (Object.keys(record).some(name => !fieldNames.has(name)))
|
|
470
|
+
return false;
|
|
471
|
+
ancestors.add(value);
|
|
472
|
+
try {
|
|
473
|
+
return descriptor.fields.every(field => {
|
|
474
|
+
if (!Object.prototype.hasOwnProperty.call(record, field.name)) {
|
|
475
|
+
return field.optional === true || field.defaultValue !== undefined;
|
|
476
|
+
}
|
|
477
|
+
return matchesInputDescriptor(record[field.name], field.descriptor, ancestors);
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
ancestors.delete(value);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
function validateInputValue(value, descriptor, inputPath, context, ancestors = new Set()) {
|
|
487
|
+
const withContext = (run) => {
|
|
488
|
+
try {
|
|
489
|
+
return run();
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
if (error instanceof AMXInputError) {
|
|
493
|
+
throw new AMXInputError(error.inputPath, error.expected, error.actual, context);
|
|
494
|
+
}
|
|
495
|
+
throw error;
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
switch (descriptor.kind) {
|
|
499
|
+
case 'number': return withContext(() => requireAMXNumber(value, inputPath));
|
|
500
|
+
case 'string': return withContext(() => requireAMXString(value, inputPath));
|
|
501
|
+
case 'boolean': return withContext(() => requireAMXBoolean(value, inputPath));
|
|
502
|
+
case 'dateTime':
|
|
503
|
+
if (typeof value !== 'string' || !validAMXDateTime(value)) {
|
|
504
|
+
throw new AMXInputError(inputPath, 'RFC 3339 DateTime', describeInput(value), context);
|
|
505
|
+
}
|
|
506
|
+
return value;
|
|
507
|
+
case 'enum':
|
|
508
|
+
return withContext(() => requireAMXEnum(value, descriptor.enumName, descriptor.members, inputPath));
|
|
509
|
+
case 'measurement': {
|
|
510
|
+
try {
|
|
511
|
+
validateMeasurementDescriptor(descriptor);
|
|
512
|
+
return requireAMXMeasurement(value, descriptor.dimensionName, descriptor.dimensionVector, descriptor.units, inputPath);
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
if (error instanceof AMXInputError) {
|
|
516
|
+
throw new AMXInputError(error.inputPath, error.expected, error.actual, context);
|
|
517
|
+
}
|
|
518
|
+
if (error instanceof RangeError) {
|
|
519
|
+
throw new AMXInputError(inputPath, 'finite physical Measurement', describeInput(value), context);
|
|
520
|
+
}
|
|
521
|
+
if (error instanceof TypeError) {
|
|
522
|
+
throw new AMXInputError(inputPath, 'valid Measurement descriptor', 'invalid descriptor', context);
|
|
523
|
+
}
|
|
524
|
+
throw error;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
case 'nullable':
|
|
528
|
+
return value === null ? null : validateInputValue(value, descriptor.value, inputPath, context, ancestors);
|
|
529
|
+
case 'list': {
|
|
530
|
+
const list = withContext(() => requireAMXList(value, inputPath));
|
|
531
|
+
if (ancestors.has(list)) {
|
|
532
|
+
throw new AMXInputError(inputPath, 'acyclic list', 'cyclic list', context);
|
|
533
|
+
}
|
|
534
|
+
ancestors.add(list);
|
|
535
|
+
try {
|
|
536
|
+
return list.map((item, index) => validateInputValue(item, descriptor.element, appendInputPath(inputPath, String(index)), context, ancestors));
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
ancestors.delete(list);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
case 'record': {
|
|
543
|
+
const fields = descriptor.fields;
|
|
544
|
+
if (new Set(fields.map(field => field.name)).size !== fields.length) {
|
|
545
|
+
throw new AMXInputError(inputPath, 'valid record descriptor', 'duplicate field name', context);
|
|
546
|
+
}
|
|
547
|
+
const record = withContext(() => requireAMXRecord(value, inputPath, fields.map(field => field.name)));
|
|
548
|
+
if (ancestors.has(record)) {
|
|
549
|
+
throw new AMXInputError(inputPath, 'acyclic record', 'cyclic record', context);
|
|
550
|
+
}
|
|
551
|
+
ancestors.add(record);
|
|
552
|
+
try {
|
|
553
|
+
const normalized = {};
|
|
554
|
+
for (const field of fields) {
|
|
555
|
+
const fieldPath = appendInputPath(inputPath, field.name);
|
|
556
|
+
if (Object.prototype.hasOwnProperty.call(record, field.name)) {
|
|
557
|
+
normalized[field.name] = validateInputValue(record[field.name], field.descriptor, fieldPath, context, ancestors);
|
|
558
|
+
}
|
|
559
|
+
else if (field.defaultValue) {
|
|
560
|
+
normalized[field.name] = validateInputValue(field.defaultValue(), field.descriptor, fieldPath, context, ancestors);
|
|
561
|
+
}
|
|
562
|
+
else if (field.optional) {
|
|
563
|
+
normalized[field.name] = null;
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
throw new AMXInputError(fieldPath, descriptorName(field.descriptor), 'missing', context);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return normalized;
|
|
570
|
+
}
|
|
571
|
+
finally {
|
|
572
|
+
ancestors.delete(record);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
function validateMeasurementDescriptor(descriptor) {
|
|
578
|
+
if (!descriptor.dimensionName || Object.values(descriptor.dimensionVector).some(value => !Number.isFinite(value))) {
|
|
579
|
+
throw new TypeError('Invalid measurement dimension descriptor');
|
|
580
|
+
}
|
|
581
|
+
for (const [name, unit] of Object.entries(descriptor.units)) {
|
|
582
|
+
validateUnit(unit);
|
|
583
|
+
if (name !== unit.name || !sameVector(unit.vector, descriptor.dimensionVector)) {
|
|
584
|
+
throw new TypeError('Invalid measurement unit descriptor');
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function descriptorName(descriptor) {
|
|
589
|
+
switch (descriptor.kind) {
|
|
590
|
+
case 'number': return 'finite Number';
|
|
591
|
+
case 'string': return 'String';
|
|
592
|
+
case 'boolean': return 'Boolean';
|
|
593
|
+
case 'dateTime': return 'RFC 3339 DateTime';
|
|
594
|
+
case 'enum': return `member of enum '${descriptor.enumName}'`;
|
|
595
|
+
case 'measurement': return `Measurement<${descriptor.dimensionName}>`;
|
|
596
|
+
case 'nullable': return `${descriptorName(descriptor.value)} or null`;
|
|
597
|
+
case 'list': return `list of ${descriptorName(descriptor.element)}`;
|
|
598
|
+
case 'record': return `record '${descriptor.recordName}'`;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function appendInputPath(inputPath, segment) {
|
|
602
|
+
return `${inputPath}/${escapePointer(segment)}`;
|
|
603
|
+
}
|
|
604
|
+
function validAMXDateTime(value) {
|
|
605
|
+
const parts = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/);
|
|
606
|
+
if (!parts)
|
|
607
|
+
return false;
|
|
608
|
+
const [, year, month, day, hour, minute, second, offset] = parts;
|
|
609
|
+
const date = new Date(0);
|
|
610
|
+
date.setUTCFullYear(+year, +month - 1, +day);
|
|
611
|
+
return date.getUTCFullYear() === +year && date.getUTCMonth() === +month - 1 && date.getUTCDate() === +day
|
|
612
|
+
&& +hour <= 23 && +minute <= 59 && +second <= 59
|
|
613
|
+
&& (offset === 'Z' || (+offset.slice(1, 3) <= 23 && +offset.slice(4) <= 59));
|
|
614
|
+
}
|
|
615
|
+
function copyArrayTree(value) {
|
|
616
|
+
return Array.isArray(value) ? value.map(copyArrayTree) : value;
|
|
617
|
+
}
|
|
618
|
+
function toMeasurementUnit(descriptor) {
|
|
619
|
+
return Object.freeze({
|
|
620
|
+
scale: descriptor.scale,
|
|
621
|
+
offset: descriptor.offset,
|
|
622
|
+
vector: Object.freeze({ ...descriptor.vector }),
|
|
623
|
+
factors: Object.freeze((descriptor.factors ?? [{
|
|
624
|
+
identity: descriptor.identity,
|
|
625
|
+
name: descriptor.name,
|
|
626
|
+
exponent: 1
|
|
627
|
+
}]).map(factor => Object.freeze({ ...factor }))),
|
|
628
|
+
text: descriptor.name
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
function sourceLocation(context) {
|
|
632
|
+
if (context?.line === undefined || context.column === undefined)
|
|
633
|
+
return undefined;
|
|
634
|
+
return { line: context.line, column: context.column };
|
|
635
|
+
}
|
|
636
|
+
function withRuntimeErrorContext(run, context) {
|
|
637
|
+
try {
|
|
638
|
+
return run();
|
|
639
|
+
}
|
|
640
|
+
catch (error) {
|
|
641
|
+
if (error instanceof AMXRuntimeError || error instanceof AMXInputError)
|
|
642
|
+
throw error;
|
|
643
|
+
if (error instanceof AmxError) {
|
|
644
|
+
throw new AMXRuntimeError({
|
|
645
|
+
code: error.code,
|
|
646
|
+
message: error.message,
|
|
647
|
+
file: error.file ?? context?.file,
|
|
648
|
+
line: error.line ?? context?.line,
|
|
649
|
+
column: error.column ?? context?.column,
|
|
650
|
+
inputPath: context?.inputPath
|
|
651
|
+
}, error);
|
|
652
|
+
}
|
|
653
|
+
throw error;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function validateUnit(unit) {
|
|
657
|
+
if (!unit.name || !unit.identity || !Number.isFinite(unit.scale) || unit.scale <= 0
|
|
658
|
+
|| !Number.isFinite(unit.offset)
|
|
659
|
+
|| Object.values(unit.vector).some(exponent => !Number.isFinite(exponent))) {
|
|
660
|
+
throw new TypeError('Invalid AMX unit descriptor');
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
function sameVector(left, right) {
|
|
664
|
+
const leftEntries = Object.entries(left);
|
|
665
|
+
return leftEntries.length === Object.keys(right).length
|
|
666
|
+
&& leftEntries.every(([identity, exponent]) => right[identity] === exponent);
|
|
667
|
+
}
|
|
668
|
+
function freezeData(value) {
|
|
669
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value))
|
|
670
|
+
return value;
|
|
671
|
+
for (const child of Object.values(value))
|
|
672
|
+
freezeData(child);
|
|
673
|
+
return Object.freeze(value);
|
|
674
|
+
}
|
|
675
|
+
function describeInput(value) {
|
|
676
|
+
if (value === null)
|
|
677
|
+
return 'null';
|
|
678
|
+
if (Array.isArray(value))
|
|
679
|
+
return 'list';
|
|
680
|
+
return typeof value;
|
|
681
|
+
}
|
|
682
|
+
function escapePointer(value) {
|
|
683
|
+
return value.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
684
|
+
}
|