@nova-energy/openamx 0.15.0 → 0.18.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 +16 -10
- package/dist/diagnostics/errors.js +9 -0
- package/dist/editor/completion.js +103 -6
- package/dist/editor/highlighting.js +63 -11
- package/dist/editor/moduleAnalysis.js +20 -11
- package/dist/editor/symbols.js +126 -4
- package/dist/formatter/formatAmx.js +31 -7
- package/dist/parser/parseFor.js +2 -5
- package/dist/parser/parseIf.js +7 -1
- package/dist/parser/parseStatements.js +382 -100
- package/dist/renderer/diagramBrowserEntry.js +4 -3
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/renderer/diagramHtml.js +2 -2
- package/dist/runtime/diagramModel.js +267 -11
- package/dist/runtime/evaluateExpression.js +72 -11
- package/dist/runtime/moduleLoader.js +40 -9
- package/dist/typechecker/checkDocument.js +175 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -21,10 +21,13 @@ not certified risk models or industry standards.
|
|
|
21
21
|
results as JSON or CSV.
|
|
22
22
|
- **Build visual explanations.** Create tables, charts, and formula-driven 2D
|
|
23
23
|
diagrams alongside the narrative.
|
|
24
|
-
- **Use the latest language additions.** V0.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
- **Use the latest language additions.** V0.18 adds diagram-local loops and
|
|
25
|
+
conditionals that generate primitives once per model while allowing
|
|
26
|
+
properties to animate with diagram time. V0.16 adds statement-bodied
|
|
27
|
+
functions while preserving expression-bodied functions. V0.14 adds runtime
|
|
28
|
+
type predicates, mathematical functions, text helpers, and bounded numeric
|
|
29
|
+
formatting with `en-US`, `de-DE`, and `fr-FR` locales. Earlier additions
|
|
30
|
+
include record inheritance, enums, and braced conditionals.
|
|
28
31
|
- **Publish reports in familiar formats.** Render standalone, offline HTML or
|
|
29
32
|
export PDF and DOCX. Charts are interactive in HTML; PDF charts are static
|
|
30
33
|
with their data retained in tables. DOCX charts are editable native charts
|
|
@@ -137,12 +140,12 @@ unified HTML/PDF/DOCX export workflow.
|
|
|
137
140
|
- [Mapped data and validation](examples/06_Mapped_Data_and_Validation.amx)
|
|
138
141
|
introduces explicit JSON/CSV input mappings.
|
|
139
142
|
- [Complete asset screening report](examples/12_Complete_Asset_Screening_Report.amx)
|
|
140
|
-
is the guided destination; [route measurements](examples/13_Route_Measurement_Report.amx)
|
|
141
|
-
|
|
142
|
-
are standalone references.
|
|
143
|
+
is the guided destination; [route measurements](examples/13_Route_Measurement_Report.amx),
|
|
144
|
+
the [battery-resistor circuit](examples/14_Battery_Resistor_Circuit.amx), and
|
|
145
|
+
[function bodies](examples/15_Function_Bodies.amx) are standalone references.
|
|
143
146
|
- V0.12 features: [records and enums](examples/04_Typed_Records_Enums_and_Modules.amx)
|
|
144
147
|
and [expressions and decisions](examples/02_Expressions_and_Decisions.amx).
|
|
145
|
-
- V0.13 diagrams: [formula-driven diagrams](examples/08_Formula_Driven_Diagrams.amx)
|
|
148
|
+
- V0.13/V0.18 diagrams: [formula-driven diagrams](examples/08_Formula_Driven_Diagrams.amx)
|
|
146
149
|
and the [battery-resistor reference](examples/14_Battery_Resistor_Circuit.amx).
|
|
147
150
|
- V0.14 examples: [runtime type checks](examples/09_Runtime_Type_Checks.amx),
|
|
148
151
|
[math and Measurements](examples/10_Mathematics_and_Measurement_Operations.amx),
|
|
@@ -152,8 +155,11 @@ Read the [V0.9 language specification](docs/language-spec-v0.9.md) and
|
|
|
152
155
|
[V0.9 migration guide](docs/migrating-to-v0.9.md), then the additive
|
|
153
156
|
[V0.12 language specification](docs/language-spec-v0.12.md),
|
|
154
157
|
[V0.13 diagram specification](docs/language-spec-v0.13.md), and
|
|
155
|
-
[V0.14 language specification](docs/language-spec-v0.14.md)
|
|
156
|
-
|
|
158
|
+
[V0.14 language specification](docs/language-spec-v0.14.md) and
|
|
159
|
+
[V0.16 language specification](docs/language-spec-v0.16.md), and the additive
|
|
160
|
+
[V0.18 diagram generation specification](docs/language-spec-v0.18.md) for the
|
|
161
|
+
current language contract. Browse the [examples](examples/) and
|
|
162
|
+
[documentation](docs/). The
|
|
157
163
|
[desktop guide](desktop-app/README.md) and
|
|
158
164
|
[VS Code guide](vscode-extension/README.md) cover their respective tools.
|
|
159
165
|
|
|
@@ -89,6 +89,15 @@ export function throwInvalidReturnContext(source, file) {
|
|
|
89
89
|
column: source?.column
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
|
+
export function throwFunctionFallthrough(source, file) {
|
|
93
|
+
throw new AmxError({
|
|
94
|
+
code: 'AMXF4002',
|
|
95
|
+
message: 'Function finished without returning a valid expression',
|
|
96
|
+
file,
|
|
97
|
+
line: source?.line,
|
|
98
|
+
column: source?.column
|
|
99
|
+
});
|
|
100
|
+
}
|
|
92
101
|
export function throwListOperationError(message, source, file) {
|
|
93
102
|
throw new AmxError({ code: 'AMX1008', message, file, line: source?.line, column: source?.column });
|
|
94
103
|
}
|
|
@@ -127,6 +127,17 @@ function statementEndOffset(text, statement) {
|
|
|
127
127
|
}
|
|
128
128
|
return text.length;
|
|
129
129
|
}
|
|
130
|
+
function expressionFunctionBodyBounds(text, fn) {
|
|
131
|
+
if (fn.body.kind !== "expression")
|
|
132
|
+
return undefined;
|
|
133
|
+
const start = sourceOffset(text, fn.source);
|
|
134
|
+
if (start === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
const signature = text.slice(start).match(/^fn\s+[A-Za-z][A-Za-z0-9_]*\s*\([^)]*\)\s*:\s*[A-Za-z][A-Za-z0-9_]*(?:(?:\[\])|\?)*\s*=/);
|
|
137
|
+
if (!signature)
|
|
138
|
+
return undefined;
|
|
139
|
+
return { from: start + signature[0].length, to: statementEndOffset(text, fn) };
|
|
140
|
+
}
|
|
130
141
|
function blockBounds(text, source) {
|
|
131
142
|
const start = sourceOffset(text, source);
|
|
132
143
|
if (start === undefined)
|
|
@@ -155,7 +166,9 @@ function diagramAt(text, parsed, cursorOffset) {
|
|
|
155
166
|
const bounds = blockBounds(text, diagram.source);
|
|
156
167
|
if (!bounds || cursorOffset <= bounds.open || cursorOffset > bounds.close)
|
|
157
168
|
continue;
|
|
158
|
-
const element = diagram.
|
|
169
|
+
const element = diagram.content.find((candidate) => {
|
|
170
|
+
if (candidate.type !== "diagramElement")
|
|
171
|
+
return false;
|
|
159
172
|
const elementBounds = blockBounds(text, candidate.source);
|
|
160
173
|
return !!elementBounds && cursorOffset > elementBounds.open && cursorOffset <= elementBounds.close;
|
|
161
174
|
});
|
|
@@ -450,13 +463,97 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
450
463
|
collectExpression(expression.defaultExpression);
|
|
451
464
|
};
|
|
452
465
|
const collectRange = (expression) => { collectExpression(expression.start); collectExpression(expression.end); };
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
466
|
+
const functionDeclarations = statementsOf(parsed).filter((statement) => statement.type === "functionDeclaration");
|
|
467
|
+
const activeFunction = functionDeclarations.find(statement => {
|
|
468
|
+
if (statement.body.kind === "expression") {
|
|
469
|
+
const bounds = expressionFunctionBodyBounds(text, statement);
|
|
470
|
+
return !!bounds && cursorOffset >= bounds.from && cursorOffset <= bounds.to;
|
|
471
|
+
}
|
|
472
|
+
const opening = sourceOffset(text, statement.body.openingSource);
|
|
473
|
+
const closing = sourceOffset(text, statement.body.closingSource);
|
|
474
|
+
return opening !== undefined && closing !== undefined && cursorOffset > opening && cursorOffset <= closing;
|
|
475
|
+
});
|
|
476
|
+
if (activeFunction) {
|
|
477
|
+
const functionStart = sourceOffset(text, activeFunction.source) ?? Number.MAX_SAFE_INTEGER;
|
|
478
|
+
const moduleDeclarations = statementsOf(parsed).filter(statement => {
|
|
479
|
+
const start = sourceOffset(text, statement.source);
|
|
480
|
+
return start !== undefined && start < functionStart && [
|
|
481
|
+
"functionDeclaration", "typeDeclaration", "enumDeclaration", "dimensionDeclaration", "unitDeclaration"
|
|
482
|
+
].includes(statement.type);
|
|
483
|
+
});
|
|
484
|
+
collectStatements(moduleDeclarations);
|
|
485
|
+
for (const imported of statementsOf(parsed).filter((statement) => statement.type === "importDeclaration" && (sourceOffset(text, statement.source) ?? Number.MAX_SAFE_INTEGER) < functionStart)) {
|
|
486
|
+
for (const item of imported.names) {
|
|
487
|
+
if (analysis?.importedTypes.has(item.name))
|
|
488
|
+
types.add(item.name);
|
|
489
|
+
if (analysis?.importedEnums.has(item.name)) {
|
|
490
|
+
types.add(item.name);
|
|
491
|
+
enums.set(item.name, analysis.importedEnums.get(item.name));
|
|
492
|
+
}
|
|
493
|
+
if (analysis?.importedFunctions.has(item.name))
|
|
494
|
+
functions.add(item.name);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
const namedType = (type) => {
|
|
498
|
+
if (!type)
|
|
499
|
+
return undefined;
|
|
500
|
+
if (type.type === "namedType")
|
|
501
|
+
return type.name;
|
|
502
|
+
return namedType(type.element);
|
|
503
|
+
};
|
|
504
|
+
let functionScope = new Map(activeFunction.parameters.map(parameter => [parameter.name, namedType(parameter.annotation)]));
|
|
505
|
+
const collectFunctionScope = (statements, scope) => {
|
|
506
|
+
for (const statement of statements) {
|
|
507
|
+
const start = sourceOffset(text, statement.source);
|
|
508
|
+
if (start === undefined || start >= cursorOffset)
|
|
509
|
+
continue;
|
|
510
|
+
if (statement.type === "variableDeclaration") {
|
|
511
|
+
if (statementEndOffset(text, statement) <= cursorOffset) {
|
|
512
|
+
const inferred = namedType(statement.annotation)
|
|
513
|
+
?? (statement.expression.type === "recordConstructor" ? statement.expression.name : undefined);
|
|
514
|
+
scope.set(statement.name, inferred);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
else if (statement.type === "bracedIfStatement") {
|
|
518
|
+
if (insideIfBranch(text, statement, cursorOffset, false)) {
|
|
519
|
+
return collectFunctionScope(statement.consequent, new Map(scope));
|
|
520
|
+
}
|
|
521
|
+
if (statement.alternate && insideIfBranch(text, statement, cursorOffset, true)) {
|
|
522
|
+
return collectFunctionScope(statement.alternate, new Map(scope));
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
else if (statement.type === "forStatement") {
|
|
526
|
+
const bounds = loopBounds(text, statement);
|
|
527
|
+
if (bounds && cursorOffset > bounds.open && cursorOffset <= bounds.close) {
|
|
528
|
+
const loopScope = new Map(scope);
|
|
529
|
+
loopScope.set(statement.variable, undefined);
|
|
530
|
+
return collectFunctionScope(statement.body, loopScope);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return scope;
|
|
535
|
+
};
|
|
536
|
+
if (activeFunction.body.kind === "statements") {
|
|
537
|
+
functionScope = collectFunctionScope(activeFunction.body.statements, functionScope);
|
|
538
|
+
}
|
|
539
|
+
variables.clear();
|
|
540
|
+
localBindingTypes.clear();
|
|
541
|
+
for (const [name, typeName] of functionScope) {
|
|
542
|
+
variables.add(name);
|
|
543
|
+
if (typeName)
|
|
544
|
+
localBindingTypes.set(name, typeName);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
for (const node of parsed.nodes) {
|
|
549
|
+
if (node.type === "executableCodeBlock" && node.source && (sourceOffset(text, node.source) ?? Number.MAX_SAFE_INTEGER) < cursorOffset) {
|
|
550
|
+
collectStatements(node.statements);
|
|
551
|
+
}
|
|
456
552
|
}
|
|
457
553
|
}
|
|
458
|
-
|
|
459
|
-
|
|
554
|
+
if (!activeFunction)
|
|
555
|
+
for (const iterator of activeIterators)
|
|
556
|
+
variables.add(iterator);
|
|
460
557
|
for (const [name, typeName] of localBindingTypes) {
|
|
461
558
|
const declaration = localTypeDeclarations.get(typeName) ?? analysis?.importedTypes.get(typeName);
|
|
462
559
|
if (declaration)
|
|
@@ -143,6 +143,54 @@ export function editorHighlightFacts(text, document) {
|
|
|
143
143
|
if (node.element)
|
|
144
144
|
typeReference(node.element);
|
|
145
145
|
};
|
|
146
|
+
const diagramContent = (content) => {
|
|
147
|
+
for (const item of content) {
|
|
148
|
+
if (item.type === "diagramElement") {
|
|
149
|
+
add(item.source, item.primitive, "keyword");
|
|
150
|
+
add(item.nameSource, item.name, "declaration");
|
|
151
|
+
for (const property of item.properties) {
|
|
152
|
+
add(property.nameSource, property.name, "field");
|
|
153
|
+
expression(property.expression);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (item.type === "diagramForBlock") {
|
|
157
|
+
add(item.source, "for", "keyword");
|
|
158
|
+
add(item.variableSource, item.variable, "declaration");
|
|
159
|
+
const variableOffset = sourceOffset(text, item.variableSource);
|
|
160
|
+
if (variableOffset !== undefined) {
|
|
161
|
+
const afterVariable = variableOffset + item.variable.length;
|
|
162
|
+
const inToken = text.slice(afterVariable).match(/^\s+in\b/)?.[0];
|
|
163
|
+
if (inToken) {
|
|
164
|
+
const from = afterVariable + inToken.length - 2;
|
|
165
|
+
facts.push({ from, to: from + 2, kind: "keyword" });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
expression(item.iterable);
|
|
169
|
+
diagramContent(item.content);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
add(item.source, "if", "keyword");
|
|
173
|
+
expression(item.test);
|
|
174
|
+
diagramContent(item.consequent);
|
|
175
|
+
if (item.elseSource)
|
|
176
|
+
add(item.elseSource, "else", "keyword");
|
|
177
|
+
if (item.alternate)
|
|
178
|
+
diagramContent(item.alternate);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const loopVariable = (node) => {
|
|
183
|
+
const start = sourceOffset(text, node.source);
|
|
184
|
+
if (start === undefined)
|
|
185
|
+
return;
|
|
186
|
+
const lineEnd = text.indexOf("\n", start);
|
|
187
|
+
const header = text.slice(start, lineEnd < 0 ? text.length : lineEnd).replace(/\r$/, "");
|
|
188
|
+
const match = header.match(/^for\s+([A-Za-z][A-Za-z0-9_]*)\s+in\b/);
|
|
189
|
+
if (!match || match[1] !== node.variable)
|
|
190
|
+
return;
|
|
191
|
+
const from = start + match[0].indexOf(node.variable);
|
|
192
|
+
facts.push({ from, to: from + node.variable.length, kind: "declaration" });
|
|
193
|
+
};
|
|
146
194
|
const statement = (node) => {
|
|
147
195
|
const keyword = {
|
|
148
196
|
variableDeclaration: "let", typeDeclaration: "type", enumDeclaration: "enum", functionDeclaration: "fn",
|
|
@@ -187,14 +235,7 @@ export function editorHighlightFacts(text, document) {
|
|
|
187
235
|
expression(time.to);
|
|
188
236
|
expression(time.duration);
|
|
189
237
|
}
|
|
190
|
-
|
|
191
|
-
add(element.source, element.primitive, "keyword");
|
|
192
|
-
add(element.nameSource, element.name, "declaration");
|
|
193
|
-
for (const property of element.properties) {
|
|
194
|
-
add(property.nameSource, property.name, "field");
|
|
195
|
-
expression(property.expression);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
238
|
+
diagramContent(node.content);
|
|
198
239
|
}
|
|
199
240
|
if (node.type === "typeDeclaration") {
|
|
200
241
|
const firstParent = node.parents?.[0];
|
|
@@ -222,16 +263,26 @@ export function editorHighlightFacts(text, document) {
|
|
|
222
263
|
expression(node.expression);
|
|
223
264
|
}
|
|
224
265
|
else if (node.type === "functionDeclaration") {
|
|
225
|
-
node.parameters.forEach(param =>
|
|
266
|
+
node.parameters.forEach(param => {
|
|
267
|
+
const range = sourceTokenRange(text, param.source, param.name);
|
|
268
|
+
if (range)
|
|
269
|
+
facts.push({ ...range, kind: "declaration" });
|
|
270
|
+
typeReference(param.annotation);
|
|
271
|
+
});
|
|
226
272
|
typeReference(node.returnType);
|
|
227
|
-
|
|
273
|
+
if (node.body.kind === "expression")
|
|
274
|
+
expression(node.body.expression);
|
|
275
|
+
else
|
|
276
|
+
node.body.statements.forEach(statement);
|
|
228
277
|
}
|
|
229
278
|
else if (node.type === "tableDeclaration" || node.type === "chartDeclaration")
|
|
230
279
|
add(node.bindingSource, node.binding, "reference");
|
|
231
280
|
else if (node.type === "showStatement")
|
|
232
281
|
add(node.nameSource, node.name, "reference");
|
|
233
|
-
else if (node.type === "assignmentStatement" || node.type === "compoundAssignmentStatement")
|
|
282
|
+
else if (node.type === "assignmentStatement" || node.type === "compoundAssignmentStatement") {
|
|
283
|
+
add(node.source, node.name, "reference");
|
|
234
284
|
expression(node.expression);
|
|
285
|
+
}
|
|
235
286
|
else if (node.type === "addStatement") {
|
|
236
287
|
add(node.targetSource, node.name, "reference");
|
|
237
288
|
expression(node.value);
|
|
@@ -245,6 +296,7 @@ export function editorHighlightFacts(text, document) {
|
|
|
245
296
|
expression(node.index);
|
|
246
297
|
}
|
|
247
298
|
else if (node.type === "forStatement") {
|
|
299
|
+
loopVariable(node);
|
|
248
300
|
expression(node.iterable);
|
|
249
301
|
node.body.forEach(statement);
|
|
250
302
|
}
|
|
@@ -28,21 +28,27 @@ function locatedError(error, file) {
|
|
|
28
28
|
const location = message.match(/\bat (\d+):(\d+)/);
|
|
29
29
|
return new AmxError({ code: "AMX3001", message, file, line: location ? Number(location[1]) : undefined, column: location ? Number(location[2]) : undefined });
|
|
30
30
|
}
|
|
31
|
-
function
|
|
31
|
+
function isWithinOrEqual(root, candidate) {
|
|
32
|
+
const relative = path.relative(root, candidate);
|
|
33
|
+
return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
|
|
34
|
+
}
|
|
35
|
+
function isWithinDirectory(root, candidate) {
|
|
36
|
+
return root !== candidate && isWithinOrEqual(root, candidate);
|
|
37
|
+
}
|
|
38
|
+
function validateImportPath(importNode, file, importRoot, rootDescription) {
|
|
32
39
|
const importPath = importNode.path;
|
|
33
40
|
if (!(importPath.startsWith("./") || importPath.startsWith("../"))
|
|
34
41
|
|| importPath.includes("\\") || !importPath.endsWith(".amx") || path.isAbsolute(importPath)) {
|
|
35
42
|
moduleIssue("AMX5001", `Invalid local module path '${importPath}'`, file, importNode.pathSource);
|
|
36
43
|
}
|
|
37
44
|
const target = path.resolve(path.dirname(file), importPath);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
moduleIssue("AMX5001", `Module '${importPath}' resolves outside the entry directory tree`, file, importNode.pathSource);
|
|
45
|
+
if (!isWithinDirectory(importRoot, target)) {
|
|
46
|
+
moduleIssue("AMX5001", `Module '${importPath}' resolves outside the ${rootDescription}`, file, importNode.pathSource);
|
|
41
47
|
}
|
|
42
48
|
return target;
|
|
43
49
|
}
|
|
44
50
|
/** Parse and statically check a bounded reachable module graph without evaluating AMX or loading data. */
|
|
45
|
-
export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
51
|
+
export function analyzeEditorModules(text, entryFile, resolveModule, importRoot) {
|
|
46
52
|
let entryDocument;
|
|
47
53
|
try {
|
|
48
54
|
entryDocument = parseDocumentText(text);
|
|
@@ -72,7 +78,11 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
72
78
|
moduleIssue("AMX5001", "Local imports are unavailable to this editor analysis", entryFile, entryImports[0].pathSource);
|
|
73
79
|
}
|
|
74
80
|
const entryPath = entryFile ? canonicalFile(entryFile) : "";
|
|
75
|
-
const
|
|
81
|
+
const moduleRoot = importRoot ? canonicalFile(importRoot) : entryPath ? path.dirname(entryPath) : "";
|
|
82
|
+
const rootDescription = importRoot ? "import root directory tree" : "entry directory tree";
|
|
83
|
+
if (!isWithinOrEqual(moduleRoot, entryPath)) {
|
|
84
|
+
moduleIssue("AMX5001", `Entry module '${entryFile}' resolves outside the ${rootDescription}`, entryFile, entryImports[0].pathSource);
|
|
85
|
+
}
|
|
76
86
|
const records = new Map();
|
|
77
87
|
const visiting = [];
|
|
78
88
|
const baseUnits = new Map();
|
|
@@ -83,7 +93,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
83
93
|
return existing;
|
|
84
94
|
const cycleIndex = visiting.indexOf(file);
|
|
85
95
|
if (cycleIndex >= 0) {
|
|
86
|
-
const cycle = [...visiting.slice(cycleIndex), file].map(item => path.relative(
|
|
96
|
+
const cycle = [...visiting.slice(cycleIndex), file].map(item => path.relative(moduleRoot, item) || path.basename(item)).join(" -> ");
|
|
87
97
|
moduleIssue("AMX5003", `Import cycle detected: ${cycle}`, viaImport?.file ?? file, viaImport?.source);
|
|
88
98
|
}
|
|
89
99
|
if (records.size + visiting.length >= MAX_EDITOR_MODULES) {
|
|
@@ -112,7 +122,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
112
122
|
const importedDimensionUnitNames = new Set();
|
|
113
123
|
const importTargets = new Map();
|
|
114
124
|
for (const importNode of imports) {
|
|
115
|
-
const targetPath = validateImportPath(importNode, file,
|
|
125
|
+
const targetPath = validateImportPath(importNode, file, moduleRoot, rootDescription);
|
|
116
126
|
let resolved;
|
|
117
127
|
try {
|
|
118
128
|
resolved = resolveModule(file, targetPath, importNode);
|
|
@@ -122,9 +132,8 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
122
132
|
throw error;
|
|
123
133
|
moduleIssue("AMX5001", `Module '${importNode.path}' could not be found`, file, importNode.pathSource);
|
|
124
134
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
moduleIssue("AMX5001", `Module '${importNode.path}' resolves outside the entry directory tree`, file, importNode.pathSource);
|
|
135
|
+
if (!isWithinDirectory(moduleRoot, canonicalFile(resolved.file))) {
|
|
136
|
+
moduleIssue("AMX5001", `Module '${importNode.path}' resolves outside the ${rootDescription}`, file, importNode.pathSource);
|
|
128
137
|
}
|
|
129
138
|
let dependencyDocument;
|
|
130
139
|
try {
|
package/dist/editor/symbols.js
CHANGED
|
@@ -35,6 +35,18 @@ function enumMemberSource(text, source) {
|
|
|
35
35
|
const whitespace = text.slice(dot + 1).match(/^\s*/)?.[0].length ?? 0;
|
|
36
36
|
return { line: source.line, column: source.column + 1 + whitespace };
|
|
37
37
|
}
|
|
38
|
+
function loopVariableRange(text, loop) {
|
|
39
|
+
const start = sourceOffset(text, loop.source);
|
|
40
|
+
if (start === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
const lineEnd = text.indexOf("\n", start);
|
|
43
|
+
const header = text.slice(start, lineEnd < 0 ? text.length : lineEnd).replace(/\r$/, "");
|
|
44
|
+
const match = header.match(/^for\s+([A-Za-z][A-Za-z0-9_]*)\s+in\b/);
|
|
45
|
+
if (!match || match[1] !== loop.variable)
|
|
46
|
+
return undefined;
|
|
47
|
+
const from = start + match[0].indexOf(loop.variable);
|
|
48
|
+
return { from, to: from + loop.variable.length };
|
|
49
|
+
}
|
|
38
50
|
/** Build identity-bearing symbol occurrences; uncertain parser spans and duplicate declarations have no target. */
|
|
39
51
|
export function editorSymbolFacts(text, entryFile, entryDocument, analysis, moduleSources = new Map()) {
|
|
40
52
|
const documents = new Map();
|
|
@@ -271,7 +283,9 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
271
283
|
diagramScope.set(time.name, localTime);
|
|
272
284
|
}
|
|
273
285
|
const elementNames = new Set();
|
|
274
|
-
for (const element of statement.
|
|
286
|
+
for (const element of statement.content) {
|
|
287
|
+
if (element.type !== "diagramElement")
|
|
288
|
+
continue;
|
|
275
289
|
const range = sourceTokenRange(moduleText, element.nameSource, element.name);
|
|
276
290
|
if (range && diagramParent && !elementNames.has(element.name)) {
|
|
277
291
|
elementNames.add(element.name);
|
|
@@ -342,14 +356,122 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
342
356
|
statement.consequent.forEach(item => nestedStatement(item));
|
|
343
357
|
statement.alternate?.forEach(item => nestedStatement(item));
|
|
344
358
|
}
|
|
345
|
-
if (statement.type === "functionDeclaration")
|
|
346
|
-
|
|
359
|
+
if (statement.type === "functionDeclaration") {
|
|
360
|
+
const functionDeclaration = declaration(moduleText, file, statement);
|
|
361
|
+
const functionParent = functionDeclaration && !duplicates.has(statement.name) ? functionDeclaration.target : undefined;
|
|
362
|
+
const functionScope = new Map([...visible].filter(([, fact]) => !["binding", "input", "table", "chart", "diagram"].includes(fact.kind)));
|
|
363
|
+
const declarationsInScope = new Set();
|
|
364
|
+
for (const parameter of statement.parameters) {
|
|
365
|
+
typeReference(parameter.annotation, visible);
|
|
366
|
+
const range = sourceTokenRange(moduleText, parameter.source, parameter.name);
|
|
367
|
+
if (!range || !functionParent)
|
|
368
|
+
continue;
|
|
369
|
+
const parameterFact = {
|
|
370
|
+
...range,
|
|
371
|
+
file,
|
|
372
|
+
name: parameter.name,
|
|
373
|
+
target: { file, ...range },
|
|
374
|
+
parent: functionParent,
|
|
375
|
+
declaration: true,
|
|
376
|
+
kind: "parameter",
|
|
377
|
+
detail: `parameter ${parameter.name}: ${parameter.annotation.name ?? parameter.annotation.type}`
|
|
378
|
+
};
|
|
379
|
+
result.push(parameterFact);
|
|
380
|
+
functionScope.set(parameter.name, parameterFact);
|
|
381
|
+
declarationsInScope.add(parameter.name);
|
|
382
|
+
}
|
|
383
|
+
typeReference(statement.returnType, visible);
|
|
384
|
+
const walkFunctionStatements = (items, scope, declared) => {
|
|
385
|
+
for (const item of items) {
|
|
386
|
+
if (item.type === "variableDeclaration") {
|
|
387
|
+
expression(item.expression, scope);
|
|
388
|
+
if (item.annotation)
|
|
389
|
+
typeReference(item.annotation, visible);
|
|
390
|
+
const range = declarationNameRange(moduleText, item);
|
|
391
|
+
if (!range)
|
|
392
|
+
continue;
|
|
393
|
+
const existing = declared.has(item.name) ? scope.get(item.name) : undefined;
|
|
394
|
+
if (existing) {
|
|
395
|
+
result.push({ ...existing, ...range, file, declaration: false });
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (!functionParent)
|
|
399
|
+
continue;
|
|
400
|
+
const localFact = {
|
|
401
|
+
...range,
|
|
402
|
+
file,
|
|
403
|
+
name: item.name,
|
|
404
|
+
target: { file, ...range },
|
|
405
|
+
parent: functionParent,
|
|
406
|
+
declaration: true,
|
|
407
|
+
kind: "binding",
|
|
408
|
+
detail: `variable ${item.name}${item.annotation ? `: ${item.annotation.name ?? item.annotation.type}` : ""}`
|
|
409
|
+
};
|
|
410
|
+
result.push(localFact);
|
|
411
|
+
scope.set(item.name, localFact);
|
|
412
|
+
declared.add(item.name);
|
|
413
|
+
}
|
|
414
|
+
else if (item.type === "assignmentStatement" || item.type === "compoundAssignmentStatement") {
|
|
415
|
+
add(item.source, item.name, scope.get(item.name));
|
|
416
|
+
expression(item.expression, scope);
|
|
417
|
+
}
|
|
418
|
+
else if (item.type === "addStatement") {
|
|
419
|
+
add(item.targetSource, item.name, scope.get(item.name));
|
|
420
|
+
expression(item.value, scope);
|
|
421
|
+
if (item.index)
|
|
422
|
+
expression(item.index, scope);
|
|
423
|
+
}
|
|
424
|
+
else if (item.type === "removeStatement") {
|
|
425
|
+
add(item.targetSource, item.name, scope.get(item.name));
|
|
426
|
+
expression(item.count, scope);
|
|
427
|
+
if (item.index)
|
|
428
|
+
expression(item.index, scope);
|
|
429
|
+
}
|
|
430
|
+
else if (item.type === "forStatement") {
|
|
431
|
+
expression(item.iterable, scope);
|
|
432
|
+
const loopScope = new Map(scope);
|
|
433
|
+
const loopDeclared = new Set();
|
|
434
|
+
const range = loopVariableRange(moduleText, item);
|
|
435
|
+
if (range && functionParent) {
|
|
436
|
+
const iteratorFact = {
|
|
437
|
+
...range,
|
|
438
|
+
file,
|
|
439
|
+
name: item.variable,
|
|
440
|
+
target: { file, ...range },
|
|
441
|
+
parent: functionParent,
|
|
442
|
+
declaration: true,
|
|
443
|
+
kind: "iterator",
|
|
444
|
+
detail: `loop variable ${item.variable}`
|
|
445
|
+
};
|
|
446
|
+
result.push(iteratorFact);
|
|
447
|
+
loopScope.set(item.variable, iteratorFact);
|
|
448
|
+
loopDeclared.add(item.variable);
|
|
449
|
+
}
|
|
450
|
+
walkFunctionStatements(item.body, loopScope, loopDeclared);
|
|
451
|
+
}
|
|
452
|
+
else if (item.type === "bracedIfStatement") {
|
|
453
|
+
expression(item.test, scope);
|
|
454
|
+
walkFunctionStatements(item.consequent, new Map(scope), new Set());
|
|
455
|
+
if (item.alternate)
|
|
456
|
+
walkFunctionStatements(item.alternate, new Map(scope), new Set());
|
|
457
|
+
}
|
|
458
|
+
else if (item.type === "returnStatement")
|
|
459
|
+
expression(item.expression, scope);
|
|
460
|
+
else
|
|
461
|
+
nestedStatement(item, scope);
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
if (statement.body.kind === "expression")
|
|
465
|
+
expression(statement.body.expression, functionScope);
|
|
466
|
+
else
|
|
467
|
+
walkFunctionStatements(statement.body.statements, functionScope, declarationsInScope);
|
|
468
|
+
}
|
|
347
469
|
const declared = declaration(moduleText, file, statement);
|
|
348
470
|
if (declared && "name" in statement && !duplicates.has(statement.name)) {
|
|
349
471
|
if (statement.type === "variableDeclaration") {
|
|
350
472
|
const inferred = module?.checkResult.bindingTypes.get(statement.name) ?? (file === entryFile ? analysis?.bindingTypes?.get(statement.name) : undefined);
|
|
351
473
|
if (inferred)
|
|
352
|
-
declared.detail = `
|
|
474
|
+
declared.detail = `variable ${statement.name}: ${typeText(inferred)}`;
|
|
353
475
|
}
|
|
354
476
|
if (statement.type !== "diagramDeclaration")
|
|
355
477
|
visible.set(statement.name, declared);
|
|
@@ -115,11 +115,35 @@ function formatDiagram(diagram, level) {
|
|
|
115
115
|
for (const time of diagram.times) {
|
|
116
116
|
output.push(`${' '.repeat(level + 1)}time ${time.name} from ${time.fromText} to ${time.toText} duration ${time.durationText}`);
|
|
117
117
|
}
|
|
118
|
-
for (const
|
|
119
|
-
output.push(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
for (const node of diagram.content)
|
|
119
|
+
output.push(...formatDiagramContent(node, level + 1));
|
|
120
|
+
output.push(`${indentation}}`);
|
|
121
|
+
return output;
|
|
122
|
+
}
|
|
123
|
+
function formatDiagramContent(node, level) {
|
|
124
|
+
const indentation = ' '.repeat(level);
|
|
125
|
+
if (node.type === 'diagramElement') {
|
|
126
|
+
return [
|
|
127
|
+
`${indentation}${node.primitive} ${node.name} {`,
|
|
128
|
+
...orderedProperties(node.properties, elementPropertyOrder(node))
|
|
129
|
+
.flatMap(property => formatProperty(property, level + 1)),
|
|
130
|
+
`${indentation}}`
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
if (node.type === 'diagramForBlock') {
|
|
134
|
+
return [
|
|
135
|
+
`${indentation}for ${node.variable} in ${node.iterableText} {`,
|
|
136
|
+
...node.content.flatMap(child => formatDiagramContent(child, level + 1)),
|
|
137
|
+
`${indentation}}`
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
const output = [
|
|
141
|
+
`${indentation}if ${node.testText} {`,
|
|
142
|
+
...node.consequent.flatMap(child => formatDiagramContent(child, level + 1))
|
|
143
|
+
];
|
|
144
|
+
if (node.alternate) {
|
|
145
|
+
output.push(`${indentation}} else {`);
|
|
146
|
+
output.push(...node.alternate.flatMap(child => formatDiagramContent(child, level + 1)));
|
|
123
147
|
}
|
|
124
148
|
output.push(`${indentation}}`);
|
|
125
149
|
return output;
|
|
@@ -137,11 +161,11 @@ function scanBraces(line) {
|
|
|
137
161
|
if (end !== undefined)
|
|
138
162
|
index = end;
|
|
139
163
|
}
|
|
140
|
-
else if (character === '{') {
|
|
164
|
+
else if (character === '{' || character === '[') {
|
|
141
165
|
leading = false;
|
|
142
166
|
opens++;
|
|
143
167
|
}
|
|
144
|
-
else if (character === '}') {
|
|
168
|
+
else if (character === '}' || character === ']') {
|
|
145
169
|
if (leading)
|
|
146
170
|
leadingClosures++;
|
|
147
171
|
closes++;
|
package/dist/parser/parseFor.js
CHANGED
|
@@ -19,12 +19,9 @@ function parseLoop(text, source) {
|
|
|
19
19
|
bodySource: { line: (source?.line ?? 1) + 1, column: 1 }
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
export function parseForStatement(text, source) {
|
|
22
|
+
export function parseForStatement(text, source, context = {}) {
|
|
23
23
|
const loop = parseLoop(text, source);
|
|
24
|
-
|
|
25
|
-
throw new Error(`Statement-form for loops cannot contain return at ${source?.line ?? 1}:${source?.column ?? 1}`);
|
|
26
|
-
}
|
|
27
|
-
const body = parseStatements(loop.bodyText, loop.bodySource, { allowFor: false });
|
|
24
|
+
const body = parseStatements(loop.bodyText, loop.bodySource, { ...context, allowFor: false });
|
|
28
25
|
return {
|
|
29
26
|
type: 'forStatement',
|
|
30
27
|
variable: loop.variable,
|
package/dist/parser/parseIf.js
CHANGED
|
@@ -74,7 +74,7 @@ function containsTopLevelWord(text, start, end, word) {
|
|
|
74
74
|
}
|
|
75
75
|
return false;
|
|
76
76
|
}
|
|
77
|
-
function readIfBlocks(text, source) {
|
|
77
|
+
export function readIfBlocks(text, source) {
|
|
78
78
|
const firstOpen = findFirstBlockOpen(text, 2);
|
|
79
79
|
if (firstOpen === undefined)
|
|
80
80
|
syntaxError("Expected '{' after braced 'if' condition", source);
|
|
@@ -86,6 +86,7 @@ function readIfBlocks(text, source) {
|
|
|
86
86
|
syntaxError("Expected condition after 'if'", source);
|
|
87
87
|
const consequentBody = text.slice(firstOpen + 1, firstClose);
|
|
88
88
|
const consequentSource = offsetLocation(text, firstOpen + 1, source);
|
|
89
|
+
const consequentOpenSource = offsetLocation(text, firstOpen, source);
|
|
89
90
|
const consequentClosingSource = offsetLocation(text, firstClose, source);
|
|
90
91
|
let cursor = skipWhitespace(text, firstClose + 1);
|
|
91
92
|
if (!text.startsWith('else', cursor) || /[A-Za-z0-9_]/.test(text[cursor + 4] ?? '')) {
|
|
@@ -93,10 +94,12 @@ function readIfBlocks(text, source) {
|
|
|
93
94
|
condition,
|
|
94
95
|
consequent: consequentBody,
|
|
95
96
|
consequentSource,
|
|
97
|
+
consequentOpenSource,
|
|
96
98
|
consequentClosingSource,
|
|
97
99
|
endOffset: firstClose + 1
|
|
98
100
|
};
|
|
99
101
|
}
|
|
102
|
+
const elseStart = cursor;
|
|
100
103
|
cursor = skipWhitespace(text, cursor + 4);
|
|
101
104
|
if (text[cursor] !== '{')
|
|
102
105
|
syntaxError("Expected '{' after 'else'", offsetLocation(text, cursor, source));
|
|
@@ -109,8 +112,11 @@ function readIfBlocks(text, source) {
|
|
|
109
112
|
alternate: text.slice(cursor + 1, alternateClose),
|
|
110
113
|
consequentSource,
|
|
111
114
|
alternateSource: offsetLocation(text, cursor + 1, source),
|
|
115
|
+
consequentOpenSource,
|
|
112
116
|
consequentClosingSource,
|
|
117
|
+
alternateOpenSource: offsetLocation(text, cursor, source),
|
|
113
118
|
alternateClosingSource: offsetLocation(text, alternateClose, source),
|
|
119
|
+
elseSource: offsetLocation(text, elseStart, source),
|
|
114
120
|
endOffset: alternateClose + 1
|
|
115
121
|
};
|
|
116
122
|
}
|