@nova-energy/openamx 0.10.1 → 0.12.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/COMMERCIAL-LICENSE.md +73 -73
- package/LICENSE.md +659 -659
- package/README.md +538 -513
- package/dist/cli.js +14 -3
- package/dist/editor/completion.js +96 -5
- package/dist/editor/highlighting.js +36 -3
- package/dist/editor/moduleAnalysis.js +8 -2
- package/dist/editor/refactoring.js +1 -1
- package/dist/editor/sourceRanges.js +1 -0
- package/dist/editor/symbols.js +109 -3
- package/dist/parser/parseExpression.js +14 -0
- package/dist/parser/parseIf.js +180 -0
- package/dist/parser/parseStatements.js +85 -7
- package/dist/renderer/narrativeModel.js +561 -0
- package/dist/renderer/renderHtml.js +199 -61
- package/dist/renderer/reportDocx.js +653 -91
- package/dist/renderer/reportPaths.js +31 -0
- package/dist/renderer/reportPdf.js +137 -25
- package/dist/renderer/reportPreparation.js +25 -37
- package/dist/runtime/environment.js +2 -0
- package/dist/runtime/evaluateExpression.js +116 -8
- package/dist/runtime/inputData.js +20 -18
- package/dist/runtime/moduleLoader.js +16 -2
- package/dist/runtime/outputData.js +18 -12
- package/dist/typechecker/checkDocument.js +254 -13
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -91,7 +91,10 @@ cli
|
|
|
91
91
|
validation: options.validation
|
|
92
92
|
});
|
|
93
93
|
const report = await prepareReport(doc, env, { file: input, projectRoot: options.projectRoot });
|
|
94
|
-
const bytes = await serializeDocxReport(prepareDocxReport(report
|
|
94
|
+
const bytes = await serializeDocxReport(prepareDocxReport(report, {
|
|
95
|
+
sourceDocumentPath: input,
|
|
96
|
+
destinationPath: destination.path
|
|
97
|
+
}));
|
|
95
98
|
await writeDocxAtomically(destination, bytes);
|
|
96
99
|
console.log(`Exported DOCX to ${destination.path}`);
|
|
97
100
|
return;
|
|
@@ -104,7 +107,10 @@ cli
|
|
|
104
107
|
validation: options.validation
|
|
105
108
|
});
|
|
106
109
|
const report = await prepareReport(doc, env, { file: input, projectRoot: options.projectRoot });
|
|
107
|
-
const bytes = await serializePdfReport(preparePdfReport(report
|
|
110
|
+
const bytes = await serializePdfReport(preparePdfReport(report, {
|
|
111
|
+
sourceDocumentPath: input,
|
|
112
|
+
destinationPath: destination.path
|
|
113
|
+
}));
|
|
108
114
|
await writePdfAtomically(destination, bytes);
|
|
109
115
|
console.log(`Exported PDF to ${destination.path}`);
|
|
110
116
|
}
|
|
@@ -137,7 +143,12 @@ cli
|
|
|
137
143
|
reservedOutputPath: outputMappings?.length ? outPath : undefined
|
|
138
144
|
});
|
|
139
145
|
const report = await prepareReport(doc, env, { file: input, projectRoot: options.projectRoot });
|
|
140
|
-
const html = renderPreparedHtml(report
|
|
146
|
+
const html = renderPreparedHtml(report, {
|
|
147
|
+
mode: "standalone",
|
|
148
|
+
sourceDocumentPath: input,
|
|
149
|
+
projectRoot: options.projectRoot,
|
|
150
|
+
outputPath: resolve(outPath)
|
|
151
|
+
});
|
|
141
152
|
const serialized = serializeOutputs(outputs, env);
|
|
142
153
|
await writeOutputs([{ path: outPath, contents: html }, ...serialized]);
|
|
143
154
|
console.log(`Rendered to ${outPath}`);
|
|
@@ -8,6 +8,7 @@ const v03Keywords = ["null", "type", "fn", "import", "from", "input", "export"];
|
|
|
8
8
|
const v04Keywords = ["table", "chart", "show", "title", "description", "column", "category", "x", "y", "group", "labels", "series", "as"];
|
|
9
9
|
const v09ListKeywords = ["add", "remove", "at"];
|
|
10
10
|
const v09UnitKeywords = ["dimension", "unit"];
|
|
11
|
+
const v12Keywords = ["enum", "extends", "override"];
|
|
11
12
|
const standardFunctions = ["sum", "min", "max", "mean", "round", "abs", "sqrt", "pow"];
|
|
12
13
|
const primitiveTypes = ["Number", "String", "Boolean", "DateTime"];
|
|
13
14
|
function cursorInsideOpenAmxFence(text, cursorOffset) {
|
|
@@ -129,19 +130,82 @@ function loopBounds(text, loop) {
|
|
|
129
130
|
if (end === undefined)
|
|
130
131
|
return undefined;
|
|
131
132
|
offset = end;
|
|
132
|
-
continue;
|
|
133
133
|
}
|
|
134
|
-
if (character === "{")
|
|
134
|
+
else if (character === "{")
|
|
135
135
|
depth++;
|
|
136
136
|
else if (character === "}" && --depth === 0)
|
|
137
137
|
return { open, close: offset };
|
|
138
138
|
}
|
|
139
139
|
return undefined;
|
|
140
140
|
}
|
|
141
|
+
function ifBranchBounds(text, node, alternate) {
|
|
142
|
+
const start = sourceOffset(text, node.source);
|
|
143
|
+
if (start === undefined)
|
|
144
|
+
return undefined;
|
|
145
|
+
const findOpen = (from) => {
|
|
146
|
+
let brackets = 0;
|
|
147
|
+
for (let offset = from; offset < text.length; offset++) {
|
|
148
|
+
const character = text[offset];
|
|
149
|
+
if (character === '"' || character === "'") {
|
|
150
|
+
const end = findStringLiteralEnd(text, offset);
|
|
151
|
+
if (end === undefined)
|
|
152
|
+
return undefined;
|
|
153
|
+
offset = end;
|
|
154
|
+
}
|
|
155
|
+
else if (character === "(" || character === "[")
|
|
156
|
+
brackets++;
|
|
157
|
+
else if (character === ")" || character === "]")
|
|
158
|
+
brackets--;
|
|
159
|
+
else if (character === "{" && brackets === 0)
|
|
160
|
+
return offset;
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
};
|
|
164
|
+
const findClose = (open) => {
|
|
165
|
+
let depth = 0;
|
|
166
|
+
for (let offset = open; offset < text.length; offset++) {
|
|
167
|
+
const character = text[offset];
|
|
168
|
+
if (character === '"' || character === "'") {
|
|
169
|
+
const end = findStringLiteralEnd(text, offset);
|
|
170
|
+
if (end === undefined)
|
|
171
|
+
return undefined;
|
|
172
|
+
offset = end;
|
|
173
|
+
}
|
|
174
|
+
else if (character === "{")
|
|
175
|
+
depth++;
|
|
176
|
+
else if (character === "}" && --depth === 0)
|
|
177
|
+
return offset;
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
};
|
|
181
|
+
const firstOpen = findOpen(start + 2);
|
|
182
|
+
if (firstOpen === undefined)
|
|
183
|
+
return undefined;
|
|
184
|
+
const firstClose = findClose(firstOpen);
|
|
185
|
+
if (firstClose === undefined)
|
|
186
|
+
return undefined;
|
|
187
|
+
if (!alternate)
|
|
188
|
+
return { open: firstOpen, close: firstClose };
|
|
189
|
+
let cursor = firstClose + 1;
|
|
190
|
+
while (/\s/.test(text[cursor] ?? ""))
|
|
191
|
+
cursor++;
|
|
192
|
+
if (!text.startsWith("else", cursor))
|
|
193
|
+
return undefined;
|
|
194
|
+
const secondOpen = findOpen(cursor + 4);
|
|
195
|
+
if (secondOpen === undefined)
|
|
196
|
+
return undefined;
|
|
197
|
+
const secondClose = findClose(secondOpen);
|
|
198
|
+
return secondClose === undefined ? undefined : { open: secondOpen, close: secondClose };
|
|
199
|
+
}
|
|
200
|
+
function insideIfBranch(text, node, cursorOffset, alternate) {
|
|
201
|
+
const bounds = ifBranchBounds(text, node, alternate);
|
|
202
|
+
return !!bounds && cursorOffset > bounds.open && cursorOffset < bounds.close;
|
|
203
|
+
}
|
|
141
204
|
function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
142
205
|
const variables = new Set();
|
|
143
206
|
const functions = new Set();
|
|
144
207
|
const types = new Set(primitiveTypes);
|
|
208
|
+
const enums = new Map();
|
|
145
209
|
const views = new Set();
|
|
146
210
|
const recordTypes = new Map();
|
|
147
211
|
const localTypeDeclarations = new Map();
|
|
@@ -175,6 +239,12 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
175
239
|
localTypeDeclarations.set(statement.name, statement);
|
|
176
240
|
}
|
|
177
241
|
}
|
|
242
|
+
else if (statement.type === "enumDeclaration") {
|
|
243
|
+
if (statementEndOffset(text, statement) <= cursorOffset) {
|
|
244
|
+
types.add(statement.name);
|
|
245
|
+
enums.set(statement.name, statement);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
178
248
|
else if (statement.type === "functionDeclaration") {
|
|
179
249
|
if (statementEndOffset(text, statement) <= cursorOffset)
|
|
180
250
|
functions.add(statement.name);
|
|
@@ -188,6 +258,10 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
188
258
|
for (const item of statement.names) {
|
|
189
259
|
if (analysis?.importedTypes.has(item.name))
|
|
190
260
|
types.add(item.name);
|
|
261
|
+
if (analysis?.importedEnums.has(item.name)) {
|
|
262
|
+
types.add(item.name);
|
|
263
|
+
enums.set(item.name, analysis.importedEnums.get(item.name));
|
|
264
|
+
}
|
|
191
265
|
if (analysis?.importedFunctions.has(item.name))
|
|
192
266
|
functions.add(item.name);
|
|
193
267
|
if (analysis?.importedBindings.has(item.name)) {
|
|
@@ -201,6 +275,13 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
201
275
|
}
|
|
202
276
|
else if (statement.type === "forStatement")
|
|
203
277
|
collectLoop(statement);
|
|
278
|
+
else if (statement.type === "bracedIfStatement") {
|
|
279
|
+
collectExpression(statement.test);
|
|
280
|
+
if (insideIfBranch(text, statement, cursorOffset, false))
|
|
281
|
+
collectStatements(statement.consequent);
|
|
282
|
+
if (statement.alternate && insideIfBranch(text, statement, cursorOffset, true))
|
|
283
|
+
collectStatements(statement.alternate);
|
|
284
|
+
}
|
|
204
285
|
else if (statement.type === "assignmentStatement" || statement.type === "compoundAssignmentStatement")
|
|
205
286
|
collectExpression(statement.expression);
|
|
206
287
|
else if ("expression" in statement && statement.expression)
|
|
@@ -239,6 +320,13 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
239
320
|
collectExpression(expression.consequent);
|
|
240
321
|
collectExpression(expression.alternate);
|
|
241
322
|
break;
|
|
323
|
+
case "bracedIfExpression":
|
|
324
|
+
collectExpression(expression.test);
|
|
325
|
+
if (insideIfBranch(text, expression, cursorOffset, false))
|
|
326
|
+
collectStatements(expression.consequent);
|
|
327
|
+
if (insideIfBranch(text, expression, cursorOffset, true))
|
|
328
|
+
collectStatements(expression.alternate);
|
|
329
|
+
break;
|
|
242
330
|
case "matchExpression":
|
|
243
331
|
collectMatch(expression);
|
|
244
332
|
break;
|
|
@@ -280,7 +368,7 @@ function visibleSymbols(text, parsed, cursorOffset, analysis) {
|
|
|
280
368
|
for (const [name, declaration] of analysis.importedTypes)
|
|
281
369
|
if (types.has(name))
|
|
282
370
|
localTypeDeclarations.set(name, declaration);
|
|
283
|
-
return { variables, functions, types, views, recordTypes };
|
|
371
|
+
return { variables, functions, types, enums, views, recordTypes };
|
|
284
372
|
}
|
|
285
373
|
/** Return only candidates proven visible before the cursor in the parsed source order. */
|
|
286
374
|
export function editorCompletionFacts(text, cursorOffset, parsed, analysis, contextText = text) {
|
|
@@ -290,9 +378,12 @@ export function editorCompletionFacts(text, cursorOffset, parsed, analysis, cont
|
|
|
290
378
|
const lineStart = contextText.lastIndexOf("\n", Math.max(0, contextCursor - 1)) + 1;
|
|
291
379
|
const prefix = contextText.slice(lineStart, contextCursor);
|
|
292
380
|
const fieldReceiver = prefix.match(/\b([A-Za-z][A-Za-z0-9_]*)\.\w*$/)?.[1];
|
|
293
|
-
const fields = fieldReceiver
|
|
381
|
+
const fields = fieldReceiver
|
|
382
|
+
? visible.enums.get(fieldReceiver)?.members.map(member => member.name)
|
|
383
|
+
?? visible.recordTypes.get(fieldReceiver)?.fields.map(field => field.name) ?? []
|
|
384
|
+
: [];
|
|
294
385
|
const facts = [
|
|
295
|
-
...[...v02Keywords, ...v03Keywords, ...v04Keywords, ...v09ListKeywords, ...v09UnitKeywords].map(label => ({ label, kind: "keyword" })),
|
|
386
|
+
...[...v02Keywords, ...v03Keywords, ...v04Keywords, ...v09ListKeywords, ...v09UnitKeywords, ...v12Keywords].map(label => ({ label, kind: "keyword" })),
|
|
296
387
|
...standardFunctions.map(label => ({ label, kind: "function" })),
|
|
297
388
|
...[...visible.functions].map(label => ({ label, kind: "function" })),
|
|
298
389
|
...[...visible.types].map(label => ({ label, kind: "class" })),
|
|
@@ -3,6 +3,10 @@ import { findStringLiteralEnd } from "../parser/stringScanner";
|
|
|
3
3
|
function statementsOf(document) {
|
|
4
4
|
return document.nodes.flatMap(node => node.type === "executableCodeBlock" ? node.statements : []);
|
|
5
5
|
}
|
|
6
|
+
function sourceLocationAtOffset(text, offset) {
|
|
7
|
+
const before = text.slice(0, offset).split(/\r?\n/);
|
|
8
|
+
return { line: before.length, column: before.at(-1).length + 1 };
|
|
9
|
+
}
|
|
6
10
|
/** Return parser-proven tokens only; narrative and ordinary fences never enter the AST statement walk. */
|
|
7
11
|
export function editorHighlightFacts(text, document) {
|
|
8
12
|
const facts = [];
|
|
@@ -99,6 +103,12 @@ export function editorHighlightFacts(text, document) {
|
|
|
99
103
|
expression(node.consequent);
|
|
100
104
|
expression(node.alternate);
|
|
101
105
|
break;
|
|
106
|
+
case "bracedIfExpression":
|
|
107
|
+
add(node.source, "if", "keyword");
|
|
108
|
+
expression(node.test);
|
|
109
|
+
node.consequent.forEach(statement);
|
|
110
|
+
node.alternate.forEach(statement);
|
|
111
|
+
break;
|
|
102
112
|
case "forExpression":
|
|
103
113
|
expression(node.iterable);
|
|
104
114
|
node.body.forEach(statement);
|
|
@@ -130,11 +140,11 @@ export function editorHighlightFacts(text, document) {
|
|
|
130
140
|
};
|
|
131
141
|
const statement = (node) => {
|
|
132
142
|
const keyword = {
|
|
133
|
-
variableDeclaration: "let", typeDeclaration: "type", functionDeclaration: "fn",
|
|
143
|
+
variableDeclaration: "let", typeDeclaration: "type", enumDeclaration: "enum", functionDeclaration: "fn",
|
|
134
144
|
dimensionDeclaration: "dimension", unitDeclaration: "unit",
|
|
135
145
|
inputDeclaration: "input", importDeclaration: "import", tableDeclaration: "table",
|
|
136
146
|
chartDeclaration: "chart", showStatement: "show", forStatement: "for",
|
|
137
|
-
assignmentStatement: "", compoundAssignmentStatement: "",
|
|
147
|
+
assignmentStatement: "", compoundAssignmentStatement: "", bracedIfStatement: "if", returnStatement: "return",
|
|
138
148
|
addStatement: "add", removeStatement: "remove"
|
|
139
149
|
};
|
|
140
150
|
const word = keyword[node.type];
|
|
@@ -160,11 +170,27 @@ export function editorHighlightFacts(text, document) {
|
|
|
160
170
|
add(item.source, item.name, "reference");
|
|
161
171
|
if (node.type === "inputDeclaration")
|
|
162
172
|
typeReference(node.annotation);
|
|
163
|
-
if (node.type === "typeDeclaration")
|
|
173
|
+
if (node.type === "typeDeclaration") {
|
|
174
|
+
const firstParent = node.parents?.[0];
|
|
175
|
+
const typeOffset = sourceOffset(text, node.source);
|
|
176
|
+
const parentOffset = sourceOffset(text, firstParent?.source);
|
|
177
|
+
if (typeOffset !== undefined && parentOffset !== undefined) {
|
|
178
|
+
const extendsOffset = text.slice(typeOffset, parentOffset).indexOf("extends");
|
|
179
|
+
if (extendsOffset >= 0)
|
|
180
|
+
add(sourceLocationAtOffset(text, typeOffset + extendsOffset), "extends", "keyword");
|
|
181
|
+
}
|
|
182
|
+
for (const parent of node.parents ?? [])
|
|
183
|
+
add(parent.source, parent.name, "type");
|
|
164
184
|
for (const field of node.fields) {
|
|
185
|
+
if (field.override)
|
|
186
|
+
add(field.overrideSource, "override", "keyword");
|
|
165
187
|
add(field.source, field.name, "field");
|
|
166
188
|
typeReference(field.annotation);
|
|
167
189
|
}
|
|
190
|
+
}
|
|
191
|
+
if (node.type === "enumDeclaration")
|
|
192
|
+
for (const member of node.members)
|
|
193
|
+
add(member.nameSource, member.name, "declaration");
|
|
168
194
|
if (node.type === "variableDeclaration") {
|
|
169
195
|
typeReference(node.annotation);
|
|
170
196
|
expression(node.expression);
|
|
@@ -196,6 +222,13 @@ export function editorHighlightFacts(text, document) {
|
|
|
196
222
|
expression(node.iterable);
|
|
197
223
|
node.body.forEach(statement);
|
|
198
224
|
}
|
|
225
|
+
else if (node.type === "bracedIfStatement") {
|
|
226
|
+
expression(node.test);
|
|
227
|
+
node.consequent.forEach(statement);
|
|
228
|
+
node.alternate?.forEach(statement);
|
|
229
|
+
}
|
|
230
|
+
else if (node.type === "returnStatement")
|
|
231
|
+
expression(node.expression);
|
|
199
232
|
else if ("expression" in node && node.expression)
|
|
200
233
|
expression(node.expression);
|
|
201
234
|
};
|
|
@@ -57,6 +57,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
57
57
|
return {
|
|
58
58
|
document: entryDocument,
|
|
59
59
|
importedTypes: new Map(),
|
|
60
|
+
importedEnums: new Map(),
|
|
60
61
|
importedFunctions: new Map(),
|
|
61
62
|
importedBindings: new Map(),
|
|
62
63
|
importedDimensions: new Map(),
|
|
@@ -96,12 +97,13 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
96
97
|
moduleIssue("AMX3005", "Inputs may only be declared in the entry module", file, input.source);
|
|
97
98
|
}
|
|
98
99
|
const imports = statements.filter((statement) => statement.type === "importDeclaration");
|
|
99
|
-
const localNames = new Set(statements.flatMap(statement => statement.type === "typeDeclaration" || statement.type === "functionDeclaration"
|
|
100
|
+
const localNames = new Set(statements.flatMap(statement => statement.type === "typeDeclaration" || statement.type === "enumDeclaration" || statement.type === "functionDeclaration"
|
|
100
101
|
|| statement.type === "variableDeclaration" || statement.type === "inputDeclaration"
|
|
101
102
|
|| statement.type === "tableDeclaration" || statement.type === "chartDeclaration"
|
|
102
103
|
|| statement.type === "dimensionDeclaration" || statement.type === "unitDeclaration" ? [statement.name] : []));
|
|
103
104
|
const localDimensionUnitNames = new Set(statements.flatMap(statement => statement.type === "dimensionDeclaration" || statement.type === "unitDeclaration" ? [statement.name] : []));
|
|
104
105
|
const importedTypes = new Map();
|
|
106
|
+
const importedEnums = new Map();
|
|
105
107
|
const importedFunctions = new Map();
|
|
106
108
|
const importedBindings = new Map();
|
|
107
109
|
const importedDimensions = new Map();
|
|
@@ -154,6 +156,8 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
154
156
|
}
|
|
155
157
|
if (dependency.checkResult.exportedTypes.has(importedName.name))
|
|
156
158
|
importedTypes.set(importedName.name, dependency.checkResult.exportedTypes.get(importedName.name));
|
|
159
|
+
else if (dependency.checkResult.exportedEnums.has(importedName.name))
|
|
160
|
+
importedEnums.set(importedName.name, dependency.checkResult.exportedEnums.get(importedName.name));
|
|
157
161
|
else if (dependency.checkResult.exportedFunctions.has(importedName.name))
|
|
158
162
|
importedFunctions.set(importedName.name, dependency.checkResult.exportedFunctions.get(importedName.name));
|
|
159
163
|
else if (dependency.checkResult.exportedBindings.has(importedName.name))
|
|
@@ -175,6 +179,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
175
179
|
try {
|
|
176
180
|
checkResult = checkDocument(document, file, {
|
|
177
181
|
types: importedTypes,
|
|
182
|
+
enums: importedEnums,
|
|
178
183
|
functions: importedFunctions,
|
|
179
184
|
bindings: importedBindings,
|
|
180
185
|
dimensions: importedDimensions,
|
|
@@ -188,7 +193,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
188
193
|
throw locatedError(error, file);
|
|
189
194
|
}
|
|
190
195
|
visiting.pop();
|
|
191
|
-
const record = { file, document, checkResult, importedTypes, importedFunctions, importedBindings, importedDimensions, importedUnits, importTargets };
|
|
196
|
+
const record = { file, document, checkResult, importedTypes, importedEnums, importedFunctions, importedBindings, importedDimensions, importedUnits, importTargets };
|
|
192
197
|
records.set(file, record);
|
|
193
198
|
return record;
|
|
194
199
|
};
|
|
@@ -196,6 +201,7 @@ export function analyzeEditorModules(text, entryFile, resolveModule) {
|
|
|
196
201
|
return {
|
|
197
202
|
document: result.document,
|
|
198
203
|
importedTypes: result.importedTypes,
|
|
204
|
+
importedEnums: result.importedEnums,
|
|
199
205
|
importedFunctions: result.importedFunctions,
|
|
200
206
|
importedBindings: result.importedBindings,
|
|
201
207
|
importedDimensions: result.importedDimensions,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { sourceTokenRange } from "./sourceRanges";
|
|
2
2
|
const reservedNames = new Set([
|
|
3
|
-
"let", "for", "in", "to", "return", "match", "case", "default", "if", "then", "else", "and", "or", "not", "dimension", "unit",
|
|
3
|
+
"let", "for", "in", "to", "return", "match", "case", "default", "if", "then", "else", "and", "or", "not", "dimension", "unit", "extends", "override", "enum",
|
|
4
4
|
"true", "false", "null", "type", "fn", "import", "from", "input", "export", "table", "chart", "show",
|
|
5
5
|
"title", "description", "column", "category", "x", "y", "group", "labels", "series", "as",
|
|
6
6
|
"Number", "String", "Boolean", "DateTime"
|
package/dist/editor/symbols.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { declarationNameRange, sourceTokenRange } from "./sourceRanges";
|
|
1
|
+
import { declarationNameRange, sourceOffset, sourceTokenRange } from "./sourceRanges";
|
|
2
2
|
function statementsOf(document) {
|
|
3
3
|
return document.nodes.flatMap(node => node.type === "executableCodeBlock" ? node.statements : []);
|
|
4
4
|
}
|
|
@@ -28,6 +28,13 @@ function declaration(text, file, statement) {
|
|
|
28
28
|
function moduleRecord(analysis, file) {
|
|
29
29
|
return analysis?.modules?.get(file);
|
|
30
30
|
}
|
|
31
|
+
function enumMemberSource(text, source) {
|
|
32
|
+
const dot = source && sourceOffset(text, source);
|
|
33
|
+
if (dot === undefined || text[dot] !== ".")
|
|
34
|
+
return undefined;
|
|
35
|
+
const whitespace = text.slice(dot + 1).match(/^\s*/)?.[0].length ?? 0;
|
|
36
|
+
return { line: source.line, column: source.column + 1 + whitespace };
|
|
37
|
+
}
|
|
31
38
|
/** Build identity-bearing symbol occurrences; uncertain parser spans and duplicate declarations have no target. */
|
|
32
39
|
export function editorSymbolFacts(text, entryFile, entryDocument, analysis, moduleSources = new Map()) {
|
|
33
40
|
const documents = new Map();
|
|
@@ -36,6 +43,35 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
36
43
|
documents.set(file, module.document);
|
|
37
44
|
documents.set(entryFile, entryDocument);
|
|
38
45
|
const result = [];
|
|
46
|
+
const enumMemberTargets = new Map();
|
|
47
|
+
for (const [file, document] of documents) {
|
|
48
|
+
const moduleText = file === entryFile ? text : moduleSources.get(file);
|
|
49
|
+
if (moduleText === undefined)
|
|
50
|
+
continue;
|
|
51
|
+
const statements = statementsOf(document);
|
|
52
|
+
const enumDeclarations = statements.filter((statement) => statement.type === "enumDeclaration");
|
|
53
|
+
for (const declarationNode of enumDeclarations) {
|
|
54
|
+
const nameCount = enumDeclarations.filter(item => item.name === declarationNode.name).length;
|
|
55
|
+
const memberCounts = new Map();
|
|
56
|
+
declarationNode.members.forEach(member => memberCounts.set(member.name, (memberCounts.get(member.name) ?? 0) + 1));
|
|
57
|
+
if (nameCount !== 1 || [...memberCounts.values()].some(count => count > 1))
|
|
58
|
+
continue;
|
|
59
|
+
for (const member of declarationNode.members) {
|
|
60
|
+
const range = sourceTokenRange(moduleText, member.nameSource, member.name);
|
|
61
|
+
if (!range)
|
|
62
|
+
continue;
|
|
63
|
+
enumMemberTargets.set(`${file}\0${declarationNode.name}\0${member.name}`, {
|
|
64
|
+
...range,
|
|
65
|
+
file,
|
|
66
|
+
name: member.name,
|
|
67
|
+
target: { file, ...range },
|
|
68
|
+
declaration: true,
|
|
69
|
+
kind: "enumMember",
|
|
70
|
+
detail: `enum member ${declarationNode.name}.${member.name}`
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
39
75
|
for (const [file, document] of documents) {
|
|
40
76
|
const moduleText = file === entryFile ? text : moduleSources.get(file);
|
|
41
77
|
if (moduleText === undefined)
|
|
@@ -45,6 +81,7 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
45
81
|
const declarations = statements.filter(statement => "name" in statement && declarationNameRange(moduleText, statement));
|
|
46
82
|
const duplicates = new Set(declarations.filter((candidate, index) => declarations.some((other, otherIndex) => otherIndex !== index && "name" in other && "name" in candidate && other.name === candidate.name)).map(statement => "name" in statement ? statement.name : ""));
|
|
47
83
|
const visible = new Map();
|
|
84
|
+
const visibleEnums = new Map();
|
|
48
85
|
const add = (source, name, target) => {
|
|
49
86
|
if (!target)
|
|
50
87
|
return;
|
|
@@ -100,19 +137,58 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
100
137
|
node.cases.forEach(arm => expression(arm.expression));
|
|
101
138
|
expression(node.defaultExpression);
|
|
102
139
|
break;
|
|
103
|
-
case "fieldAccess":
|
|
140
|
+
case "fieldAccess": {
|
|
104
141
|
expression(node.receiver);
|
|
142
|
+
if (node.receiver.type === "identifier") {
|
|
143
|
+
const visibleEnum = visibleEnums.get(node.receiver.name);
|
|
144
|
+
const member = visibleEnum && enumMemberTargets.get(`${visibleEnum.file}\0${visibleEnum.declaration.name}\0${node.field}`);
|
|
145
|
+
add(enumMemberSource(moduleText, node.source), node.field, member);
|
|
146
|
+
}
|
|
105
147
|
break;
|
|
148
|
+
}
|
|
106
149
|
case "listAccess":
|
|
107
150
|
expression(node.receiver);
|
|
108
151
|
expression(node.index);
|
|
109
152
|
break;
|
|
110
153
|
case "forExpression":
|
|
111
154
|
expression(node.iterable);
|
|
155
|
+
node.body.forEach(nestedStatement);
|
|
156
|
+
break;
|
|
157
|
+
case "bracedIfExpression":
|
|
158
|
+
expression(node.test);
|
|
159
|
+
node.consequent.forEach(nestedStatement);
|
|
160
|
+
node.alternate.forEach(nestedStatement);
|
|
112
161
|
break;
|
|
113
162
|
default: break;
|
|
114
163
|
}
|
|
115
164
|
};
|
|
165
|
+
const nestedStatement = (statement) => {
|
|
166
|
+
if (statement.type === "variableDeclaration")
|
|
167
|
+
expression(statement.expression);
|
|
168
|
+
else if (statement.type === "typeDeclaration") {
|
|
169
|
+
for (const parent of statement.parents ?? [])
|
|
170
|
+
add(parent.source, parent.name, visible.get(parent.name));
|
|
171
|
+
for (const field of statement.fields)
|
|
172
|
+
if (field.defaultExpression)
|
|
173
|
+
expression(field.defaultExpression);
|
|
174
|
+
}
|
|
175
|
+
else if (statement.type === "enumDeclaration") {
|
|
176
|
+
for (const member of statement.members)
|
|
177
|
+
if (member.value)
|
|
178
|
+
expression(member.value);
|
|
179
|
+
}
|
|
180
|
+
else if (statement.type === "forStatement") {
|
|
181
|
+
expression(statement.iterable);
|
|
182
|
+
statement.body.forEach(nestedStatement);
|
|
183
|
+
}
|
|
184
|
+
else if (statement.type === "bracedIfStatement") {
|
|
185
|
+
expression(statement.test);
|
|
186
|
+
statement.consequent.forEach(nestedStatement);
|
|
187
|
+
statement.alternate?.forEach(nestedStatement);
|
|
188
|
+
}
|
|
189
|
+
else if ("expression" in statement && statement.expression)
|
|
190
|
+
expression(statement.expression);
|
|
191
|
+
};
|
|
116
192
|
for (const statement of statements) {
|
|
117
193
|
if (statement.type === "importDeclaration") {
|
|
118
194
|
for (const imported of statement.names) {
|
|
@@ -134,6 +210,9 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
134
210
|
if (importedTarget) {
|
|
135
211
|
const importedOccurrence = { ...importedTarget, origin: declarationFile };
|
|
136
212
|
visible.set(imported.name, importedOccurrence);
|
|
213
|
+
if (targetStatement?.type === "enumDeclaration" && declarationFile) {
|
|
214
|
+
visibleEnums.set(imported.name, { declaration: targetStatement, file: declarationFile });
|
|
215
|
+
}
|
|
137
216
|
add(imported.source, imported.name, importedOccurrence);
|
|
138
217
|
}
|
|
139
218
|
}
|
|
@@ -153,6 +232,24 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
153
232
|
}
|
|
154
233
|
if (statement.type === "variableDeclaration")
|
|
155
234
|
expression(statement.expression);
|
|
235
|
+
if (statement.type === "typeDeclaration") {
|
|
236
|
+
for (const parent of statement.parents ?? [])
|
|
237
|
+
add(parent.source, parent.name, visible.get(parent.name));
|
|
238
|
+
for (const field of statement.fields) {
|
|
239
|
+
if (field.annotation.type === "namedType" && field.annotation.name)
|
|
240
|
+
add(field.annotation.source, field.annotation.name, visible.get(field.annotation.name));
|
|
241
|
+
if (field.defaultExpression)
|
|
242
|
+
expression(field.defaultExpression);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (statement.type === "enumDeclaration") {
|
|
246
|
+
const memberFacts = statement.members.map(member => enumMemberTargets.get(`${file}\0${statement.name}\0${member.name}`)).filter((fact) => !!fact);
|
|
247
|
+
result.push(...memberFacts);
|
|
248
|
+
visibleEnums.set(statement.name, { declaration: statement, file });
|
|
249
|
+
for (const member of statement.members)
|
|
250
|
+
if (member.value)
|
|
251
|
+
expression(member.value);
|
|
252
|
+
}
|
|
156
253
|
if (statement.type === "variableDeclaration" || statement.type === "inputDeclaration") {
|
|
157
254
|
const annotation = statement.annotation;
|
|
158
255
|
if (annotation?.type === "namedType" && annotation.name)
|
|
@@ -178,8 +275,17 @@ export function editorSymbolFacts(text, entryFile, entryDocument, analysis, modu
|
|
|
178
275
|
if (statement.index)
|
|
179
276
|
expression(statement.index);
|
|
180
277
|
}
|
|
181
|
-
if (statement.type === "forStatement")
|
|
278
|
+
if (statement.type === "forStatement") {
|
|
182
279
|
expression(statement.iterable);
|
|
280
|
+
statement.body.forEach(nestedStatement);
|
|
281
|
+
}
|
|
282
|
+
if (statement.type === "bracedIfStatement") {
|
|
283
|
+
expression(statement.test);
|
|
284
|
+
statement.consequent.forEach(nestedStatement);
|
|
285
|
+
statement.alternate?.forEach(nestedStatement);
|
|
286
|
+
}
|
|
287
|
+
if (statement.type === "functionDeclaration")
|
|
288
|
+
expression(statement.body);
|
|
183
289
|
const declared = declaration(moduleText, file, statement);
|
|
184
290
|
if (declared && "name" in statement && !duplicates.has(statement.name)) {
|
|
185
291
|
if (statement.type === "variableDeclaration") {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parseForExpression } from './parseFor';
|
|
2
|
+
import { findBracedIfExpressionEnd, parseBracedIfExpression } from './parseIf';
|
|
2
3
|
import { AmxError, syntaxError } from '../diagnostics/errors';
|
|
3
4
|
import { findInterpolationEnd as findInterpolationClose, findStringLiteralEnd } from './stringScanner';
|
|
4
5
|
function tokenize(text, source) {
|
|
@@ -14,6 +15,15 @@ function tokenize(text, source) {
|
|
|
14
15
|
if (i >= len)
|
|
15
16
|
break;
|
|
16
17
|
const ch = text[i];
|
|
18
|
+
if (text.startsWith('if', i) && !/[A-Za-z0-9_]/.test(text[i + 2] ?? '')) {
|
|
19
|
+
const end = findBracedIfExpressionEnd(text, i);
|
|
20
|
+
if (end !== undefined) {
|
|
21
|
+
const ifText = text.slice(i, end + 1);
|
|
22
|
+
tokens.push({ type: 'bracedIfExpression', value: ifText, text: ifText, offset: i });
|
|
23
|
+
i = end + 1;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
17
27
|
if (text.startsWith('for', i) && !/[A-Za-z0-9_]/.test(text[i + 3] ?? '')) {
|
|
18
28
|
const end = findForExpressionEnd(text, i, source);
|
|
19
29
|
const loopText = text.slice(i, end + 1);
|
|
@@ -487,6 +497,10 @@ export function parseExpression(text, source) {
|
|
|
487
497
|
advance();
|
|
488
498
|
return parseForExpression(t.value, source);
|
|
489
499
|
}
|
|
500
|
+
if (t.type === 'bracedIfExpression') {
|
|
501
|
+
advance();
|
|
502
|
+
return parseBracedIfExpression(t.value, locationAt(text, t.offset, source));
|
|
503
|
+
}
|
|
490
504
|
if (t.type === 'number') {
|
|
491
505
|
advance();
|
|
492
506
|
return attach({ type: 'numberLiteral', value: t.value, source: locationAt(text, t.offset, source) });
|