@nova-energy/openamx 0.16.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 +8 -4
- package/dist/editor/completion.js +3 -1
- package/dist/editor/highlighting.js +37 -8
- package/dist/editor/moduleAnalysis.js +20 -11
- package/dist/editor/symbols.js +5 -3
- package/dist/formatter/formatAmx.js +29 -5
- package/dist/parser/parseIf.js +7 -1
- package/dist/parser/parseStatements.js +157 -34
- 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 +3 -3
- package/dist/runtime/moduleLoader.js +40 -9
- package/dist/typechecker/checkDocument.js +107 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,9 @@ 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.
|
|
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
|
|
25
27
|
functions while preserving expression-bodied functions. V0.14 adds runtime
|
|
26
28
|
type predicates, mathematical functions, text helpers, and bounded numeric
|
|
27
29
|
formatting with `en-US`, `de-DE`, and `fr-FR` locales. Earlier additions
|
|
@@ -143,7 +145,7 @@ unified HTML/PDF/DOCX export workflow.
|
|
|
143
145
|
[function bodies](examples/15_Function_Bodies.amx) are standalone references.
|
|
144
146
|
- V0.12 features: [records and enums](examples/04_Typed_Records_Enums_and_Modules.amx)
|
|
145
147
|
and [expressions and decisions](examples/02_Expressions_and_Decisions.amx).
|
|
146
|
-
- 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)
|
|
147
149
|
and the [battery-resistor reference](examples/14_Battery_Resistor_Circuit.amx).
|
|
148
150
|
- V0.14 examples: [runtime type checks](examples/09_Runtime_Type_Checks.amx),
|
|
149
151
|
[math and Measurements](examples/10_Mathematics_and_Measurement_Operations.amx),
|
|
@@ -154,8 +156,10 @@ Read the [V0.9 language specification](docs/language-spec-v0.9.md) and
|
|
|
154
156
|
[V0.12 language specification](docs/language-spec-v0.12.md),
|
|
155
157
|
[V0.13 diagram specification](docs/language-spec-v0.13.md), and
|
|
156
158
|
[V0.14 language specification](docs/language-spec-v0.14.md) and
|
|
157
|
-
[V0.16 language specification](docs/language-spec-v0.16.md)
|
|
158
|
-
|
|
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
|
|
159
163
|
[desktop guide](desktop-app/README.md) and
|
|
160
164
|
[VS Code guide](vscode-extension/README.md) cover their respective tools.
|
|
161
165
|
|
|
@@ -166,7 +166,9 @@ function diagramAt(text, parsed, cursorOffset) {
|
|
|
166
166
|
const bounds = blockBounds(text, diagram.source);
|
|
167
167
|
if (!bounds || cursorOffset <= bounds.open || cursorOffset > bounds.close)
|
|
168
168
|
continue;
|
|
169
|
-
const element = diagram.
|
|
169
|
+
const element = diagram.content.find((candidate) => {
|
|
170
|
+
if (candidate.type !== "diagramElement")
|
|
171
|
+
return false;
|
|
170
172
|
const elementBounds = blockBounds(text, candidate.source);
|
|
171
173
|
return !!elementBounds && cursorOffset > elementBounds.open && cursorOffset <= elementBounds.close;
|
|
172
174
|
});
|
|
@@ -143,6 +143,42 @@ 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
|
+
};
|
|
146
182
|
const loopVariable = (node) => {
|
|
147
183
|
const start = sourceOffset(text, node.source);
|
|
148
184
|
if (start === undefined)
|
|
@@ -199,14 +235,7 @@ export function editorHighlightFacts(text, document) {
|
|
|
199
235
|
expression(time.to);
|
|
200
236
|
expression(time.duration);
|
|
201
237
|
}
|
|
202
|
-
|
|
203
|
-
add(element.source, element.primitive, "keyword");
|
|
204
|
-
add(element.nameSource, element.name, "declaration");
|
|
205
|
-
for (const property of element.properties) {
|
|
206
|
-
add(property.nameSource, property.name, "field");
|
|
207
|
-
expression(property.expression);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
238
|
+
diagramContent(node.content);
|
|
210
239
|
}
|
|
211
240
|
if (node.type === "typeDeclaration") {
|
|
212
241
|
const firstParent = node.parents?.[0];
|
|
@@ -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
|
@@ -283,7 +283,9 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
283
283
|
diagramScope.set(time.name, localTime);
|
|
284
284
|
}
|
|
285
285
|
const elementNames = new Set();
|
|
286
|
-
for (const element of statement.
|
|
286
|
+
for (const element of statement.content) {
|
|
287
|
+
if (element.type !== "diagramElement")
|
|
288
|
+
continue;
|
|
287
289
|
const range = sourceTokenRange(moduleText, element.nameSource, element.name);
|
|
288
290
|
if (range && diagramParent && !elementNames.has(element.name)) {
|
|
289
291
|
elementNames.add(element.name);
|
|
@@ -403,7 +405,7 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
403
405
|
parent: functionParent,
|
|
404
406
|
declaration: true,
|
|
405
407
|
kind: "binding",
|
|
406
|
-
detail: `
|
|
408
|
+
detail: `variable ${item.name}${item.annotation ? `: ${item.annotation.name ?? item.annotation.type}` : ""}`
|
|
407
409
|
};
|
|
408
410
|
result.push(localFact);
|
|
409
411
|
scope.set(item.name, localFact);
|
|
@@ -469,7 +471,7 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
469
471
|
if (statement.type === "variableDeclaration") {
|
|
470
472
|
const inferred = module?.checkResult.bindingTypes.get(statement.name) ?? (file === entryFile ? analysis?.bindingTypes?.get(statement.name) : undefined);
|
|
471
473
|
if (inferred)
|
|
472
|
-
declared.detail = `
|
|
474
|
+
declared.detail = `variable ${statement.name}: ${typeText(inferred)}`;
|
|
473
475
|
}
|
|
474
476
|
if (statement.type !== "diagramDeclaration")
|
|
475
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;
|
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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parseExpression } from './parseExpression';
|
|
2
2
|
import { parseForStatement } from './parseFor';
|
|
3
|
-
import { parseBracedIfStatement } from './parseIf';
|
|
3
|
+
import { parseBracedIfStatement, readIfBlocks } from './parseIf';
|
|
4
4
|
import { AmxError, staticError, syntaxError } from '../diagnostics/errors';
|
|
5
5
|
import { findStringLiteralEnd, withoutStringLiterals } from './stringScanner';
|
|
6
6
|
/** Parse executable AMX statements while preserving original-document locations. */
|
|
@@ -592,7 +592,7 @@ function parseDiagramDeclaration(lines, startIndex, source) {
|
|
|
592
592
|
nameSource: { line: source.line, column: headerLine.indexOf(name) + 1 },
|
|
593
593
|
properties: [],
|
|
594
594
|
times: [],
|
|
595
|
-
|
|
595
|
+
content: [],
|
|
596
596
|
source
|
|
597
597
|
};
|
|
598
598
|
let index = startIndex + 1;
|
|
@@ -649,46 +649,169 @@ function parseDiagramDeclaration(lines, startIndex, source) {
|
|
|
649
649
|
index++;
|
|
650
650
|
continue;
|
|
651
651
|
}
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
const element = {
|
|
657
|
-
type: 'diagramElement',
|
|
658
|
-
primitive,
|
|
659
|
-
name: elementName,
|
|
660
|
-
nameSource: { line: lineSource.line, column: line.indexOf(elementName) + 1 },
|
|
661
|
-
properties: [],
|
|
662
|
-
source: { line: lineSource.line, column: lineSource.column }
|
|
663
|
-
};
|
|
664
|
-
index++;
|
|
665
|
-
let elementClosed = false;
|
|
666
|
-
while (index < lines.length) {
|
|
667
|
-
const propertyLine = lines[index];
|
|
668
|
-
if (/^\s*}\s*$/.test(propertyLine)) {
|
|
669
|
-
elementClosed = true;
|
|
670
|
-
index++;
|
|
671
|
-
break;
|
|
672
|
-
}
|
|
673
|
-
if (!propertyLine.trim()) {
|
|
674
|
-
index++;
|
|
675
|
-
continue;
|
|
676
|
-
}
|
|
677
|
-
const property = parseDiagramProperty(lines, index, source.line + index - startIndex);
|
|
678
|
-
element.properties.push(property.node);
|
|
679
|
-
index += property.lineCount;
|
|
680
|
-
}
|
|
681
|
-
if (!elementClosed)
|
|
682
|
-
syntaxError('Unclosed diagram element', element.source);
|
|
683
|
-
statement.elements.push(element);
|
|
652
|
+
if (/^\s*(?:for|if)\b/.test(line) || /^\s*[A-Za-z][A-Za-z0-9_]*\s+[A-Za-z][A-Za-z0-9_]*\s*\{\s*$/.test(line)) {
|
|
653
|
+
const parsed = parseDiagramContentNodeAt(lines, index, lineSource, false);
|
|
654
|
+
statement.content.push(parsed.node);
|
|
655
|
+
index = parsed.endIndex + 1;
|
|
684
656
|
continue;
|
|
685
657
|
}
|
|
658
|
+
if (!/^\s*[A-Za-z][A-Za-z0-9_]*\s*:/.test(line)) {
|
|
659
|
+
syntaxError('Malformed for or if declaration', {
|
|
660
|
+
line: lineSource.line,
|
|
661
|
+
column: line.indexOf(line.trimStart()) + 1
|
|
662
|
+
});
|
|
663
|
+
}
|
|
686
664
|
const property = parseDiagramProperty(lines, index, source.line + index - startIndex);
|
|
687
665
|
statement.properties.push(property.node);
|
|
688
666
|
index += property.lineCount;
|
|
689
667
|
}
|
|
690
668
|
syntaxError('Unclosed diagram declaration', source);
|
|
691
669
|
}
|
|
670
|
+
function parseDiagramContentNodeAt(lines, index, source, insideFor) {
|
|
671
|
+
const line = lines[index];
|
|
672
|
+
if (/^\s*for\b/.test(line)) {
|
|
673
|
+
if (insideFor)
|
|
674
|
+
syntaxError('No nested loops are allowed inside a diagram', source);
|
|
675
|
+
const header = line.match(/^\s*for\s+([A-Za-z][A-Za-z0-9_]*)\s+in\s+(.+?)\s*\{\s*$/);
|
|
676
|
+
if (!header)
|
|
677
|
+
syntaxError('Malformed for or if declaration', source);
|
|
678
|
+
let endIndex;
|
|
679
|
+
try {
|
|
680
|
+
endIndex = findLoopEnd(lines, index, source);
|
|
681
|
+
}
|
|
682
|
+
catch (error) {
|
|
683
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
684
|
+
syntaxError('Malformed for or if declaration', {
|
|
685
|
+
line: diagnostic?.line ?? source.line,
|
|
686
|
+
column: diagnostic?.column ?? source.column
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
const iterableText = header[2].trim();
|
|
690
|
+
const iterableSource = { line: source.line, column: line.indexOf(iterableText) + 1 };
|
|
691
|
+
const iterable = parseDiagramControlExpression(iterableText, iterableSource);
|
|
692
|
+
const bodyText = lines.slice(index + 1, endIndex).join('\n');
|
|
693
|
+
const variableSource = { line: source.line, column: line.indexOf(header[1]) + 1 };
|
|
694
|
+
const node = {
|
|
695
|
+
type: 'diagramForBlock',
|
|
696
|
+
variable: header[1],
|
|
697
|
+
variableSource,
|
|
698
|
+
iterable,
|
|
699
|
+
iterableText,
|
|
700
|
+
iterableSource,
|
|
701
|
+
content: parseDiagramContentSequence(bodyText, { line: source.line + 1, column: 1 }, true),
|
|
702
|
+
openSource: { line: source.line, column: line.lastIndexOf('{') + 1 },
|
|
703
|
+
closeSource: { line: source.line + endIndex - index, column: lines[endIndex].lastIndexOf('}') + 1 },
|
|
704
|
+
source
|
|
705
|
+
};
|
|
706
|
+
return { node, endIndex };
|
|
707
|
+
}
|
|
708
|
+
if (/^\s*if\b/.test(line)) {
|
|
709
|
+
const indentation = line.length - line.trimStart().length;
|
|
710
|
+
const controlText = lines.slice(index).join('\n').slice(indentation);
|
|
711
|
+
const controlSource = { line: source.line, column: source.column };
|
|
712
|
+
let blocks;
|
|
713
|
+
try {
|
|
714
|
+
blocks = readIfBlocks(controlText, controlSource);
|
|
715
|
+
}
|
|
716
|
+
catch (error) {
|
|
717
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
718
|
+
syntaxError('Malformed for or if declaration', {
|
|
719
|
+
line: diagnostic?.line ?? source.line,
|
|
720
|
+
column: diagnostic?.column ?? source.column
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
const consumedLines = controlText.slice(0, blocks.endOffset).split('\n').length;
|
|
724
|
+
const endIndex = index + consumedLines - 1;
|
|
725
|
+
const testSource = locationAtOffset(controlText, controlText.indexOf(blocks.condition), controlSource);
|
|
726
|
+
const test = parseDiagramControlExpression(blocks.condition, testSource);
|
|
727
|
+
const node = {
|
|
728
|
+
type: 'diagramIfBlock',
|
|
729
|
+
test,
|
|
730
|
+
testText: blocks.condition,
|
|
731
|
+
testSource,
|
|
732
|
+
consequent: parseDiagramContentSequence(blocks.consequent, blocks.consequentSource, insideFor),
|
|
733
|
+
...(blocks.alternate === undefined ? {} : {
|
|
734
|
+
alternate: parseDiagramContentSequence(blocks.alternate, blocks.alternateSource, insideFor)
|
|
735
|
+
}),
|
|
736
|
+
consequentOpenSource: blocks.consequentOpenSource,
|
|
737
|
+
consequentCloseSource: blocks.consequentClosingSource,
|
|
738
|
+
...(blocks.alternate === undefined ? {} : {
|
|
739
|
+
elseSource: blocks.elseSource,
|
|
740
|
+
alternateOpenSource: blocks.alternateOpenSource,
|
|
741
|
+
alternateCloseSource: blocks.alternateClosingSource
|
|
742
|
+
}),
|
|
743
|
+
source
|
|
744
|
+
};
|
|
745
|
+
return { node, endIndex };
|
|
746
|
+
}
|
|
747
|
+
const elementHeader = line.match(/^\s*([A-Za-z][A-Za-z0-9_]*)\s+([A-Za-z][A-Za-z0-9_]*)\s*\{\s*$/);
|
|
748
|
+
if (!elementHeader)
|
|
749
|
+
syntaxError('Malformed for or if declaration', source);
|
|
750
|
+
const element = {
|
|
751
|
+
type: 'diagramElement',
|
|
752
|
+
primitive: elementHeader[1],
|
|
753
|
+
name: elementHeader[2],
|
|
754
|
+
nameSource: { line: source.line, column: line.indexOf(elementHeader[2]) + 1 },
|
|
755
|
+
properties: [],
|
|
756
|
+
source
|
|
757
|
+
};
|
|
758
|
+
let endIndex = index + 1;
|
|
759
|
+
let elementClosed = false;
|
|
760
|
+
while (endIndex < lines.length) {
|
|
761
|
+
const propertyLine = lines[endIndex];
|
|
762
|
+
if (/^\s*}\s*$/.test(propertyLine)) {
|
|
763
|
+
elementClosed = true;
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
if (!propertyLine.trim()) {
|
|
767
|
+
endIndex++;
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
if (/^\s*(?:for|if)\b/.test(propertyLine)
|
|
771
|
+
|| /^\s*[A-Za-z][A-Za-z0-9_]*\s+[A-Za-z][A-Za-z0-9_]*\s*\{\s*$/.test(propertyLine)) {
|
|
772
|
+
syntaxError('Malformed for or if declaration', {
|
|
773
|
+
line: source.line + endIndex - index,
|
|
774
|
+
column: propertyLine.indexOf(propertyLine.trimStart()) + 1
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
const property = parseDiagramProperty(lines, endIndex, source.line + endIndex - index);
|
|
778
|
+
element.properties.push(property.node);
|
|
779
|
+
endIndex += property.lineCount;
|
|
780
|
+
}
|
|
781
|
+
if (!elementClosed)
|
|
782
|
+
syntaxError('Unclosed diagram element', element.source);
|
|
783
|
+
return { node: element, endIndex };
|
|
784
|
+
}
|
|
785
|
+
function parseDiagramContentSequence(text, source, insideFor) {
|
|
786
|
+
const lines = text.split(/\r?\n/);
|
|
787
|
+
const content = [];
|
|
788
|
+
for (let index = 0; index < lines.length; index++) {
|
|
789
|
+
const line = lines[index];
|
|
790
|
+
if (!line.trim())
|
|
791
|
+
continue;
|
|
792
|
+
const indentation = line.length - line.trimStart().length;
|
|
793
|
+
const lineSource = {
|
|
794
|
+
line: source.line + index,
|
|
795
|
+
column: (index === 0 ? source.column : 1) + indentation
|
|
796
|
+
};
|
|
797
|
+
const parsed = parseDiagramContentNodeAt(lines, index, lineSource, insideFor);
|
|
798
|
+
content.push(parsed.node);
|
|
799
|
+
index = parsed.endIndex;
|
|
800
|
+
}
|
|
801
|
+
return content;
|
|
802
|
+
}
|
|
803
|
+
function parseDiagramControlExpression(text, source) {
|
|
804
|
+
try {
|
|
805
|
+
return parseExpression(text, source);
|
|
806
|
+
}
|
|
807
|
+
catch (error) {
|
|
808
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
809
|
+
syntaxError('Malformed for or if declaration', {
|
|
810
|
+
line: diagnostic?.line ?? source.line,
|
|
811
|
+
column: diagnostic?.column ?? source.column
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
}
|
|
692
815
|
function parseDiagramProperty(lines, index, lineNumber) {
|
|
693
816
|
const line = lines[index];
|
|
694
817
|
const match = line.match(/^\s*([A-Za-z][A-Za-z0-9_]*)\s*:\s*(.*)$/);
|
|
@@ -84,12 +84,13 @@ async function deserializePayload(payload) {
|
|
|
84
84
|
}
|
|
85
85
|
function syncControls(state) {
|
|
86
86
|
const title = state.model.title;
|
|
87
|
+
const displayTime = state.time.toFixed(3).toString();
|
|
87
88
|
state.playButton.textContent = state.running ? 'Pause' : 'Play';
|
|
88
89
|
state.playButton.setAttribute('aria-label', `${state.running ? 'Pause' : 'Play'} ${title}`);
|
|
89
90
|
state.scrubber.value = String(state.time);
|
|
90
|
-
state.scrubber.setAttribute('aria-valuetext',
|
|
91
|
-
state.timeOutput.value =
|
|
92
|
-
state.timeOutput.textContent =
|
|
91
|
+
state.scrubber.setAttribute('aria-valuetext', displayTime);
|
|
92
|
+
state.timeOutput.value = displayTime;
|
|
93
|
+
state.timeOutput.textContent = displayTime;
|
|
93
94
|
}
|
|
94
95
|
function stop(state) {
|
|
95
96
|
state.running = false;
|