@nova-energy/openamx 0.16.0 → 0.19.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 +22 -12
- package/dist/editor/completion.js +3 -1
- package/dist/editor/highlighting.js +48 -9
- 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 +197 -34
- package/dist/renderer/chartModel.js +1 -0
- package/dist/renderer/diagramBrowserEntry.js +4 -3
- package/dist/renderer/diagramBrowserRuntime.js +1 -1
- package/dist/renderer/diagramHtml.js +2 -2
- package/dist/renderer/renderHtml.js +24 -7
- package/dist/renderer/reportDocx.js +13 -4
- package/dist/renderer/reportPdf.js +2 -0
- package/dist/runtime/diagramModel.js +267 -11
- package/dist/runtime/evaluateExpression.js +67 -35
- package/dist/runtime/externalUnits.js +21 -6
- package/dist/runtime/inputData.js +7 -2
- package/dist/runtime/measurement.js +28 -1
- package/dist/runtime/moduleLoader.js +40 -9
- package/dist/runtime/outputData.js +5 -3
- package/dist/typechecker/checkDocument.js +259 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,15 +21,16 @@ 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
|
-
|
|
28
|
-
|
|
24
|
+
- **Use the latest language additions.** V0.19 lets charts omit their data table
|
|
25
|
+
with `showTable false` (the omitted default) and adds affine offset units for
|
|
26
|
+
Celsius/Fahrenheit-style conversions. V0.18 adds diagram-local loops and
|
|
27
|
+
conditionals, V0.16 adds statement-bodied functions, and V0.14 adds runtime
|
|
28
|
+
type predicates, mathematical functions, text helpers, and numeric formatting.
|
|
29
|
+
Earlier additions include record inheritance, enums, and braced conditionals.
|
|
29
30
|
- **Publish reports in familiar formats.** Render standalone, offline HTML or
|
|
30
|
-
export PDF and DOCX. Charts are interactive in HTML
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
export PDF and DOCX. Charts are interactive in HTML, while `showTable true`
|
|
32
|
+
or a true Boolean expression retains their data table across HTML, PDF, and
|
|
33
|
+
DOCX. Omitted or false chart tables are genuinely absent from those outputs.
|
|
33
34
|
- **Choose your authoring environment.** Use the command-line interface (CLI),
|
|
34
35
|
the offline-first desktop workbench, or the VS Code extension.
|
|
35
36
|
|
|
@@ -135,15 +136,19 @@ unified HTML/PDF/DOCX export workflow.
|
|
|
135
136
|
numbered path from the first computable document to a complete report.
|
|
136
137
|
- [First computable document](examples/01_First_Computable_Document.amx) is
|
|
137
138
|
the guide's small, input-free first lesson.
|
|
139
|
+
- [Measurements and units](examples/05_Measurements_and_Units.amx) covers affine
|
|
140
|
+
temperature conversions, PI/E declarations, subtraction, and physical aggregates.
|
|
138
141
|
- [Mapped data and validation](examples/06_Mapped_Data_and_Validation.amx)
|
|
139
142
|
introduces explicit JSON/CSV input mappings.
|
|
143
|
+
- [Tables, charts, and named outputs](examples/07_Tables_Charts_and_Named_Outputs.amx)
|
|
144
|
+
demonstrates omitted, false, true, and expression-controlled chart tables.
|
|
140
145
|
- [Complete asset screening report](examples/12_Complete_Asset_Screening_Report.amx)
|
|
141
146
|
is the guided destination; [route measurements](examples/13_Route_Measurement_Report.amx),
|
|
142
147
|
the [battery-resistor circuit](examples/14_Battery_Resistor_Circuit.amx), and
|
|
143
148
|
[function bodies](examples/15_Function_Bodies.amx) are standalone references.
|
|
144
149
|
- V0.12 features: [records and enums](examples/04_Typed_Records_Enums_and_Modules.amx)
|
|
145
150
|
and [expressions and decisions](examples/02_Expressions_and_Decisions.amx).
|
|
146
|
-
- V0.13 diagrams: [formula-driven diagrams](examples/08_Formula_Driven_Diagrams.amx)
|
|
151
|
+
- V0.13/V0.18 diagrams: [formula-driven diagrams](examples/08_Formula_Driven_Diagrams.amx)
|
|
147
152
|
and the [battery-resistor reference](examples/14_Battery_Resistor_Circuit.amx).
|
|
148
153
|
- V0.14 examples: [runtime type checks](examples/09_Runtime_Type_Checks.amx),
|
|
149
154
|
[math and Measurements](examples/10_Mathematics_and_Measurement_Operations.amx),
|
|
@@ -153,9 +158,14 @@ Read the [V0.9 language specification](docs/language-spec-v0.9.md) and
|
|
|
153
158
|
[V0.9 migration guide](docs/migrating-to-v0.9.md), then the additive
|
|
154
159
|
[V0.12 language specification](docs/language-spec-v0.12.md),
|
|
155
160
|
[V0.13 diagram specification](docs/language-spec-v0.13.md), and
|
|
156
|
-
[V0.14 language specification](docs/language-spec-v0.14.md)
|
|
157
|
-
[V0.16 language specification](docs/language-spec-v0.16.md)
|
|
158
|
-
|
|
161
|
+
[V0.14 language specification](docs/language-spec-v0.14.md), the
|
|
162
|
+
[V0.16 language specification](docs/language-spec-v0.16.md), the
|
|
163
|
+
[V0.18 diagram generation specification](docs/language-spec-v0.18.md), and the
|
|
164
|
+
additive [V0.19 rendering and affine-unit specification](docs/language-spec-v0.19.md)
|
|
165
|
+
for the current language contract. V0.19 omits chart data tables by default;
|
|
166
|
+
authors that intentionally retain one use `showTable true` or another Boolean
|
|
167
|
+
expression that resolves true. Browse the [examples](examples/) and
|
|
168
|
+
[documentation](docs/). The
|
|
159
169
|
[desktop guide](desktop-app/README.md) and
|
|
160
170
|
[VS Code guide](vscode-extension/README.md) cover their respective tools.
|
|
161
171
|
|
|
@@ -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
|
});
|
|
@@ -93,6 +93,7 @@ export function editorHighlightFacts(text, document) {
|
|
|
93
93
|
break;
|
|
94
94
|
case "binaryExpression":
|
|
95
95
|
expression(node.left);
|
|
96
|
+
add(node.operatorSource, node.operator, "keyword");
|
|
96
97
|
expression(node.right);
|
|
97
98
|
break;
|
|
98
99
|
case "typeTestExpression":
|
|
@@ -143,6 +144,42 @@ export function editorHighlightFacts(text, document) {
|
|
|
143
144
|
if (node.element)
|
|
144
145
|
typeReference(node.element);
|
|
145
146
|
};
|
|
147
|
+
const diagramContent = (content) => {
|
|
148
|
+
for (const item of content) {
|
|
149
|
+
if (item.type === "diagramElement") {
|
|
150
|
+
add(item.source, item.primitive, "keyword");
|
|
151
|
+
add(item.nameSource, item.name, "declaration");
|
|
152
|
+
for (const property of item.properties) {
|
|
153
|
+
add(property.nameSource, property.name, "field");
|
|
154
|
+
expression(property.expression);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else if (item.type === "diagramForBlock") {
|
|
158
|
+
add(item.source, "for", "keyword");
|
|
159
|
+
add(item.variableSource, item.variable, "declaration");
|
|
160
|
+
const variableOffset = sourceOffset(text, item.variableSource);
|
|
161
|
+
if (variableOffset !== undefined) {
|
|
162
|
+
const afterVariable = variableOffset + item.variable.length;
|
|
163
|
+
const inToken = text.slice(afterVariable).match(/^\s+in\b/)?.[0];
|
|
164
|
+
if (inToken) {
|
|
165
|
+
const from = afterVariable + inToken.length - 2;
|
|
166
|
+
facts.push({ from, to: from + 2, kind: "keyword" });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
expression(item.iterable);
|
|
170
|
+
diagramContent(item.content);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
add(item.source, "if", "keyword");
|
|
174
|
+
expression(item.test);
|
|
175
|
+
diagramContent(item.consequent);
|
|
176
|
+
if (item.elseSource)
|
|
177
|
+
add(item.elseSource, "else", "keyword");
|
|
178
|
+
if (item.alternate)
|
|
179
|
+
diagramContent(item.alternate);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
146
183
|
const loopVariable = (node) => {
|
|
147
184
|
const start = sourceOffset(text, node.source);
|
|
148
185
|
if (start === undefined)
|
|
@@ -199,14 +236,7 @@ export function editorHighlightFacts(text, document) {
|
|
|
199
236
|
expression(time.to);
|
|
200
237
|
expression(time.duration);
|
|
201
238
|
}
|
|
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
|
-
}
|
|
239
|
+
diagramContent(node.content);
|
|
210
240
|
}
|
|
211
241
|
if (node.type === "typeDeclaration") {
|
|
212
242
|
const firstParent = node.parents?.[0];
|
|
@@ -246,8 +276,17 @@ export function editorHighlightFacts(text, document) {
|
|
|
246
276
|
else
|
|
247
277
|
node.body.statements.forEach(statement);
|
|
248
278
|
}
|
|
249
|
-
else if (node.type === "tableDeclaration" || node.type === "chartDeclaration")
|
|
279
|
+
else if (node.type === "tableDeclaration" || node.type === "chartDeclaration") {
|
|
250
280
|
add(node.bindingSource, node.binding, "reference");
|
|
281
|
+
if (node.type === "chartDeclaration") {
|
|
282
|
+
for (const option of node.options) {
|
|
283
|
+
if (option.type === "chartShowTableOption") {
|
|
284
|
+
add(option.source, "showTable", "keyword");
|
|
285
|
+
expression(option.expression);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
251
290
|
else if (node.type === "showStatement")
|
|
252
291
|
add(node.nameSource, node.name, "reference");
|
|
253
292
|
else if (node.type === "assignmentStatement" || node.type === "compoundAssignmentStatement") {
|
|
@@ -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. */
|
|
@@ -533,6 +533,46 @@ function parseVisualization(lines, startIndex, source, exported) {
|
|
|
533
533
|
options.push({ type: 'viewTitleOption', value: match[1].slice(1, -1), source: optionSource });
|
|
534
534
|
continue;
|
|
535
535
|
}
|
|
536
|
+
if (/^\s*showTable\b/.test(line)) {
|
|
537
|
+
match = line.match(/^(\s*)showTable[ \t]+(.*?)[ \t]*$/);
|
|
538
|
+
if (!match || !match[2].trim())
|
|
539
|
+
return syntaxError('Invalid chart option', optionSource);
|
|
540
|
+
const expressionText = match[2].trimEnd();
|
|
541
|
+
const expressionOffset = line.indexOf(expressionText, match[1].length + 'showTable'.length);
|
|
542
|
+
const expressionSource = { line: optionSource.line, column: expressionOffset + 1 };
|
|
543
|
+
let expression;
|
|
544
|
+
try {
|
|
545
|
+
expression = parseExpression(expressionText, expressionSource);
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
if (error instanceof AmxError)
|
|
549
|
+
throw error;
|
|
550
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
551
|
+
let errorOffset = 0;
|
|
552
|
+
if (message.startsWith('Unexpected input after expression')) {
|
|
553
|
+
for (let index = 1; index < expressionText.length; index++) {
|
|
554
|
+
if (!/\s/.test(expressionText[index]))
|
|
555
|
+
continue;
|
|
556
|
+
const prefix = expressionText.slice(0, index).trimEnd();
|
|
557
|
+
try {
|
|
558
|
+
parseExpression(prefix);
|
|
559
|
+
errorOffset = index + expressionText.slice(index).search(/\S/);
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return syntaxError(message, {
|
|
567
|
+
line: expressionSource.line,
|
|
568
|
+
column: expressionSource.column + errorOffset
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
options.push({
|
|
572
|
+
type: 'chartShowTableOption', expression, source: optionSource
|
|
573
|
+
});
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
536
576
|
if (header[1] === 'chart' && (match = line.match(new RegExp(`^\\s*description\\s*:\\s*${quoted}\\s*$`)))) {
|
|
537
577
|
options.push({ type: 'viewDescriptionOption', value: match[1].slice(1, -1), source: optionSource });
|
|
538
578
|
continue;
|
|
@@ -592,7 +632,7 @@ function parseDiagramDeclaration(lines, startIndex, source) {
|
|
|
592
632
|
nameSource: { line: source.line, column: headerLine.indexOf(name) + 1 },
|
|
593
633
|
properties: [],
|
|
594
634
|
times: [],
|
|
595
|
-
|
|
635
|
+
content: [],
|
|
596
636
|
source
|
|
597
637
|
};
|
|
598
638
|
let index = startIndex + 1;
|
|
@@ -649,46 +689,169 @@ function parseDiagramDeclaration(lines, startIndex, source) {
|
|
|
649
689
|
index++;
|
|
650
690
|
continue;
|
|
651
691
|
}
|
|
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);
|
|
692
|
+
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)) {
|
|
693
|
+
const parsed = parseDiagramContentNodeAt(lines, index, lineSource, false);
|
|
694
|
+
statement.content.push(parsed.node);
|
|
695
|
+
index = parsed.endIndex + 1;
|
|
684
696
|
continue;
|
|
685
697
|
}
|
|
698
|
+
if (!/^\s*[A-Za-z][A-Za-z0-9_]*\s*:/.test(line)) {
|
|
699
|
+
syntaxError('Malformed for or if declaration', {
|
|
700
|
+
line: lineSource.line,
|
|
701
|
+
column: line.indexOf(line.trimStart()) + 1
|
|
702
|
+
});
|
|
703
|
+
}
|
|
686
704
|
const property = parseDiagramProperty(lines, index, source.line + index - startIndex);
|
|
687
705
|
statement.properties.push(property.node);
|
|
688
706
|
index += property.lineCount;
|
|
689
707
|
}
|
|
690
708
|
syntaxError('Unclosed diagram declaration', source);
|
|
691
709
|
}
|
|
710
|
+
function parseDiagramContentNodeAt(lines, index, source, insideFor) {
|
|
711
|
+
const line = lines[index];
|
|
712
|
+
if (/^\s*for\b/.test(line)) {
|
|
713
|
+
if (insideFor)
|
|
714
|
+
syntaxError('No nested loops are allowed inside a diagram', source);
|
|
715
|
+
const header = line.match(/^\s*for\s+([A-Za-z][A-Za-z0-9_]*)\s+in\s+(.+?)\s*\{\s*$/);
|
|
716
|
+
if (!header)
|
|
717
|
+
syntaxError('Malformed for or if declaration', source);
|
|
718
|
+
let endIndex;
|
|
719
|
+
try {
|
|
720
|
+
endIndex = findLoopEnd(lines, index, source);
|
|
721
|
+
}
|
|
722
|
+
catch (error) {
|
|
723
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
724
|
+
syntaxError('Malformed for or if declaration', {
|
|
725
|
+
line: diagnostic?.line ?? source.line,
|
|
726
|
+
column: diagnostic?.column ?? source.column
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
const iterableText = header[2].trim();
|
|
730
|
+
const iterableSource = { line: source.line, column: line.indexOf(iterableText) + 1 };
|
|
731
|
+
const iterable = parseDiagramControlExpression(iterableText, iterableSource);
|
|
732
|
+
const bodyText = lines.slice(index + 1, endIndex).join('\n');
|
|
733
|
+
const variableSource = { line: source.line, column: line.indexOf(header[1]) + 1 };
|
|
734
|
+
const node = {
|
|
735
|
+
type: 'diagramForBlock',
|
|
736
|
+
variable: header[1],
|
|
737
|
+
variableSource,
|
|
738
|
+
iterable,
|
|
739
|
+
iterableText,
|
|
740
|
+
iterableSource,
|
|
741
|
+
content: parseDiagramContentSequence(bodyText, { line: source.line + 1, column: 1 }, true),
|
|
742
|
+
openSource: { line: source.line, column: line.lastIndexOf('{') + 1 },
|
|
743
|
+
closeSource: { line: source.line + endIndex - index, column: lines[endIndex].lastIndexOf('}') + 1 },
|
|
744
|
+
source
|
|
745
|
+
};
|
|
746
|
+
return { node, endIndex };
|
|
747
|
+
}
|
|
748
|
+
if (/^\s*if\b/.test(line)) {
|
|
749
|
+
const indentation = line.length - line.trimStart().length;
|
|
750
|
+
const controlText = lines.slice(index).join('\n').slice(indentation);
|
|
751
|
+
const controlSource = { line: source.line, column: source.column };
|
|
752
|
+
let blocks;
|
|
753
|
+
try {
|
|
754
|
+
blocks = readIfBlocks(controlText, controlSource);
|
|
755
|
+
}
|
|
756
|
+
catch (error) {
|
|
757
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
758
|
+
syntaxError('Malformed for or if declaration', {
|
|
759
|
+
line: diagnostic?.line ?? source.line,
|
|
760
|
+
column: diagnostic?.column ?? source.column
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
const consumedLines = controlText.slice(0, blocks.endOffset).split('\n').length;
|
|
764
|
+
const endIndex = index + consumedLines - 1;
|
|
765
|
+
const testSource = locationAtOffset(controlText, controlText.indexOf(blocks.condition), controlSource);
|
|
766
|
+
const test = parseDiagramControlExpression(blocks.condition, testSource);
|
|
767
|
+
const node = {
|
|
768
|
+
type: 'diagramIfBlock',
|
|
769
|
+
test,
|
|
770
|
+
testText: blocks.condition,
|
|
771
|
+
testSource,
|
|
772
|
+
consequent: parseDiagramContentSequence(blocks.consequent, blocks.consequentSource, insideFor),
|
|
773
|
+
...(blocks.alternate === undefined ? {} : {
|
|
774
|
+
alternate: parseDiagramContentSequence(blocks.alternate, blocks.alternateSource, insideFor)
|
|
775
|
+
}),
|
|
776
|
+
consequentOpenSource: blocks.consequentOpenSource,
|
|
777
|
+
consequentCloseSource: blocks.consequentClosingSource,
|
|
778
|
+
...(blocks.alternate === undefined ? {} : {
|
|
779
|
+
elseSource: blocks.elseSource,
|
|
780
|
+
alternateOpenSource: blocks.alternateOpenSource,
|
|
781
|
+
alternateCloseSource: blocks.alternateClosingSource
|
|
782
|
+
}),
|
|
783
|
+
source
|
|
784
|
+
};
|
|
785
|
+
return { node, endIndex };
|
|
786
|
+
}
|
|
787
|
+
const elementHeader = line.match(/^\s*([A-Za-z][A-Za-z0-9_]*)\s+([A-Za-z][A-Za-z0-9_]*)\s*\{\s*$/);
|
|
788
|
+
if (!elementHeader)
|
|
789
|
+
syntaxError('Malformed for or if declaration', source);
|
|
790
|
+
const element = {
|
|
791
|
+
type: 'diagramElement',
|
|
792
|
+
primitive: elementHeader[1],
|
|
793
|
+
name: elementHeader[2],
|
|
794
|
+
nameSource: { line: source.line, column: line.indexOf(elementHeader[2]) + 1 },
|
|
795
|
+
properties: [],
|
|
796
|
+
source
|
|
797
|
+
};
|
|
798
|
+
let endIndex = index + 1;
|
|
799
|
+
let elementClosed = false;
|
|
800
|
+
while (endIndex < lines.length) {
|
|
801
|
+
const propertyLine = lines[endIndex];
|
|
802
|
+
if (/^\s*}\s*$/.test(propertyLine)) {
|
|
803
|
+
elementClosed = true;
|
|
804
|
+
break;
|
|
805
|
+
}
|
|
806
|
+
if (!propertyLine.trim()) {
|
|
807
|
+
endIndex++;
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
if (/^\s*(?:for|if)\b/.test(propertyLine)
|
|
811
|
+
|| /^\s*[A-Za-z][A-Za-z0-9_]*\s+[A-Za-z][A-Za-z0-9_]*\s*\{\s*$/.test(propertyLine)) {
|
|
812
|
+
syntaxError('Malformed for or if declaration', {
|
|
813
|
+
line: source.line + endIndex - index,
|
|
814
|
+
column: propertyLine.indexOf(propertyLine.trimStart()) + 1
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
const property = parseDiagramProperty(lines, endIndex, source.line + endIndex - index);
|
|
818
|
+
element.properties.push(property.node);
|
|
819
|
+
endIndex += property.lineCount;
|
|
820
|
+
}
|
|
821
|
+
if (!elementClosed)
|
|
822
|
+
syntaxError('Unclosed diagram element', element.source);
|
|
823
|
+
return { node: element, endIndex };
|
|
824
|
+
}
|
|
825
|
+
function parseDiagramContentSequence(text, source, insideFor) {
|
|
826
|
+
const lines = text.split(/\r?\n/);
|
|
827
|
+
const content = [];
|
|
828
|
+
for (let index = 0; index < lines.length; index++) {
|
|
829
|
+
const line = lines[index];
|
|
830
|
+
if (!line.trim())
|
|
831
|
+
continue;
|
|
832
|
+
const indentation = line.length - line.trimStart().length;
|
|
833
|
+
const lineSource = {
|
|
834
|
+
line: source.line + index,
|
|
835
|
+
column: (index === 0 ? source.column : 1) + indentation
|
|
836
|
+
};
|
|
837
|
+
const parsed = parseDiagramContentNodeAt(lines, index, lineSource, insideFor);
|
|
838
|
+
content.push(parsed.node);
|
|
839
|
+
index = parsed.endIndex;
|
|
840
|
+
}
|
|
841
|
+
return content;
|
|
842
|
+
}
|
|
843
|
+
function parseDiagramControlExpression(text, source) {
|
|
844
|
+
try {
|
|
845
|
+
return parseExpression(text, source);
|
|
846
|
+
}
|
|
847
|
+
catch (error) {
|
|
848
|
+
const diagnostic = error instanceof AmxError ? error : undefined;
|
|
849
|
+
syntaxError('Malformed for or if declaration', {
|
|
850
|
+
line: diagnostic?.line ?? source.line,
|
|
851
|
+
column: diagnostic?.column ?? source.column
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
}
|
|
692
855
|
function parseDiagramProperty(lines, index, lineNumber) {
|
|
693
856
|
const line = lines[index];
|
|
694
857
|
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;
|