@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
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { syntaxError } from '../diagnostics/errors';
|
|
2
|
+
import { parseExpression } from './parseExpression';
|
|
3
|
+
import { parseStatements } from './parseStatements';
|
|
4
|
+
export function parseBracedIfExpression(text, source) {
|
|
5
|
+
const blocks = readIfBlocks(text, source);
|
|
6
|
+
if (!blocks.alternate)
|
|
7
|
+
syntaxError("Expression-form 'if' requires an 'else' block", source);
|
|
8
|
+
return {
|
|
9
|
+
type: 'bracedIfExpression',
|
|
10
|
+
test: parseExpression(blocks.condition, offsetLocation(text, text.indexOf(blocks.condition), source)),
|
|
11
|
+
consequent: parseStatements(blocks.consequent, blocks.consequentSource, { allowReturn: true }),
|
|
12
|
+
alternate: parseStatements(blocks.alternate, blocks.alternateSource, { allowReturn: true }),
|
|
13
|
+
consequentSource: blocks.consequentClosingSource,
|
|
14
|
+
alternateSource: blocks.alternateClosingSource,
|
|
15
|
+
source
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function parseBracedIfStatement(text, source, context = {}) {
|
|
19
|
+
const blocks = readIfBlocks(text, source);
|
|
20
|
+
const statementText = text.slice(0, blocks.endOffset);
|
|
21
|
+
return {
|
|
22
|
+
statement: {
|
|
23
|
+
type: 'bracedIfStatement',
|
|
24
|
+
test: parseExpression(blocks.condition, offsetLocation(text, text.indexOf(blocks.condition), source)),
|
|
25
|
+
consequent: parseStatements(blocks.consequent, blocks.consequentSource, context),
|
|
26
|
+
...(blocks.alternate === undefined ? {} : { alternate: parseStatements(blocks.alternate, blocks.alternateSource, context) }),
|
|
27
|
+
consequentSource: blocks.consequentClosingSource,
|
|
28
|
+
...(blocks.alternateClosingSource ? { alternateSource: blocks.alternateClosingSource } : {}),
|
|
29
|
+
source
|
|
30
|
+
},
|
|
31
|
+
lineCount: statementText.split(/\r?\n/).length
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function findBracedIfExpressionEnd(text, start) {
|
|
35
|
+
if (!/^if\b/.test(text.slice(start)))
|
|
36
|
+
return undefined;
|
|
37
|
+
const firstOpen = findFirstBlockOpen(text, start + 2);
|
|
38
|
+
if (firstOpen === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
if (containsTopLevelWord(text, start + 2, firstOpen, 'then'))
|
|
41
|
+
return undefined;
|
|
42
|
+
const firstClose = matchingBrace(text, firstOpen);
|
|
43
|
+
if (firstClose === undefined)
|
|
44
|
+
return undefined;
|
|
45
|
+
let cursor = skipWhitespace(text, firstClose + 1);
|
|
46
|
+
if (!text.startsWith('else', cursor) || /[A-Za-z0-9_]/.test(text[cursor + 4] ?? ''))
|
|
47
|
+
return firstClose;
|
|
48
|
+
cursor = skipWhitespace(text, cursor + 4);
|
|
49
|
+
if (text[cursor] !== '{')
|
|
50
|
+
return firstClose;
|
|
51
|
+
return matchingBrace(text, cursor);
|
|
52
|
+
}
|
|
53
|
+
function containsTopLevelWord(text, start, end, word) {
|
|
54
|
+
let depth = 0;
|
|
55
|
+
for (let index = start; index < end; index++) {
|
|
56
|
+
const character = text[index];
|
|
57
|
+
if (character === '"' || character === "'") {
|
|
58
|
+
const close = quotedEnd(text, index);
|
|
59
|
+
if (close === undefined)
|
|
60
|
+
return false;
|
|
61
|
+
index = close;
|
|
62
|
+
}
|
|
63
|
+
else if (character === '(' || character === '[') {
|
|
64
|
+
depth++;
|
|
65
|
+
}
|
|
66
|
+
else if (character === ')' || character === ']') {
|
|
67
|
+
depth--;
|
|
68
|
+
}
|
|
69
|
+
else if (depth === 0 && text.startsWith(word, index)
|
|
70
|
+
&& !/[A-Za-z0-9_]/.test(text[index - 1] ?? '')
|
|
71
|
+
&& !/[A-Za-z0-9_]/.test(text[index + word.length] ?? '')) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function readIfBlocks(text, source) {
|
|
78
|
+
const firstOpen = findFirstBlockOpen(text, 2);
|
|
79
|
+
if (firstOpen === undefined)
|
|
80
|
+
syntaxError("Expected '{' after braced 'if' condition", source);
|
|
81
|
+
const firstClose = matchingBrace(text, firstOpen);
|
|
82
|
+
if (firstClose === undefined)
|
|
83
|
+
syntaxError("Unclosed 'if' block", source);
|
|
84
|
+
const condition = text.slice(2, firstOpen).trim();
|
|
85
|
+
if (!condition)
|
|
86
|
+
syntaxError("Expected condition after 'if'", source);
|
|
87
|
+
const consequentBody = text.slice(firstOpen + 1, firstClose);
|
|
88
|
+
const consequentSource = offsetLocation(text, firstOpen + 1, source);
|
|
89
|
+
const consequentClosingSource = offsetLocation(text, firstClose, source);
|
|
90
|
+
let cursor = skipWhitespace(text, firstClose + 1);
|
|
91
|
+
if (!text.startsWith('else', cursor) || /[A-Za-z0-9_]/.test(text[cursor + 4] ?? '')) {
|
|
92
|
+
return {
|
|
93
|
+
condition,
|
|
94
|
+
consequent: consequentBody,
|
|
95
|
+
consequentSource,
|
|
96
|
+
consequentClosingSource,
|
|
97
|
+
endOffset: firstClose + 1
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
cursor = skipWhitespace(text, cursor + 4);
|
|
101
|
+
if (text[cursor] !== '{')
|
|
102
|
+
syntaxError("Expected '{' after 'else'", offsetLocation(text, cursor, source));
|
|
103
|
+
const alternateClose = matchingBrace(text, cursor);
|
|
104
|
+
if (alternateClose === undefined)
|
|
105
|
+
syntaxError("Unclosed 'else' block", offsetLocation(text, cursor, source));
|
|
106
|
+
return {
|
|
107
|
+
condition,
|
|
108
|
+
consequent: consequentBody,
|
|
109
|
+
alternate: text.slice(cursor + 1, alternateClose),
|
|
110
|
+
consequentSource,
|
|
111
|
+
alternateSource: offsetLocation(text, cursor + 1, source),
|
|
112
|
+
consequentClosingSource,
|
|
113
|
+
alternateClosingSource: offsetLocation(text, alternateClose, source),
|
|
114
|
+
endOffset: alternateClose + 1
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function findFirstBlockOpen(text, start) {
|
|
118
|
+
let depth = 0;
|
|
119
|
+
for (let index = start; index < text.length; index++) {
|
|
120
|
+
const character = text[index];
|
|
121
|
+
if (character === '"' || character === "'") {
|
|
122
|
+
const close = quotedEnd(text, index);
|
|
123
|
+
if (close === undefined)
|
|
124
|
+
return undefined;
|
|
125
|
+
index = close;
|
|
126
|
+
}
|
|
127
|
+
else if (character === '(' || character === '[') {
|
|
128
|
+
depth++;
|
|
129
|
+
}
|
|
130
|
+
else if (character === ')' || character === ']') {
|
|
131
|
+
depth--;
|
|
132
|
+
}
|
|
133
|
+
else if (character === '{' && depth === 0) {
|
|
134
|
+
return index;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
function matchingBrace(text, open) {
|
|
140
|
+
let depth = 0;
|
|
141
|
+
for (let index = open; index < text.length; index++) {
|
|
142
|
+
const character = text[index];
|
|
143
|
+
if (character === '"' || character === "'") {
|
|
144
|
+
const close = quotedEnd(text, index);
|
|
145
|
+
if (close === undefined)
|
|
146
|
+
return undefined;
|
|
147
|
+
index = close;
|
|
148
|
+
}
|
|
149
|
+
else if (character === '{') {
|
|
150
|
+
depth++;
|
|
151
|
+
}
|
|
152
|
+
else if (character === '}' && --depth === 0) {
|
|
153
|
+
return index;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
function quotedEnd(text, start) {
|
|
159
|
+
const quote = text[start];
|
|
160
|
+
for (let index = start + 1; index < text.length; index++) {
|
|
161
|
+
if (text[index] === '\\')
|
|
162
|
+
index++;
|
|
163
|
+
else if (text[index] === quote)
|
|
164
|
+
return index;
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
function skipWhitespace(text, start) {
|
|
169
|
+
let index = start;
|
|
170
|
+
while (index < text.length && /\s/.test(text[index]))
|
|
171
|
+
index++;
|
|
172
|
+
return index;
|
|
173
|
+
}
|
|
174
|
+
function offsetLocation(text, offset, source) {
|
|
175
|
+
const before = text.slice(0, offset).split(/\r?\n/);
|
|
176
|
+
return {
|
|
177
|
+
line: (source?.line ?? 1) + before.length - 1,
|
|
178
|
+
column: before.length === 1 ? (source?.column ?? 1) + before[0].length : before.at(-1).length + 1
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parseExpression } from './parseExpression';
|
|
2
2
|
import { parseForStatement } from './parseFor';
|
|
3
|
+
import { parseBracedIfStatement } from './parseIf';
|
|
3
4
|
import { AmxError, staticError, syntaxError } from '../diagnostics/errors';
|
|
4
5
|
import { findStringLiteralEnd, withoutStringLiterals } from './stringScanner';
|
|
5
6
|
/** Parse executable AMX statements while preserving original-document locations. */
|
|
@@ -33,7 +34,7 @@ export function parseStatements(body, start = { line: 1, column: 1 }, context =
|
|
|
33
34
|
continue;
|
|
34
35
|
}
|
|
35
36
|
let exported = false;
|
|
36
|
-
const exportMatch = rawLine.match(/^(\s*)export(\s+)(type|fn|let|table|chart|dimension|unit)\b/);
|
|
37
|
+
const exportMatch = rawLine.match(/^(\s*)export(\s+)(type|enum|fn|let|table|chart|dimension|unit)\b/);
|
|
37
38
|
if (exportMatch) {
|
|
38
39
|
if (context.allowFor === false && exportMatch[3] !== 'table' && exportMatch[3] !== 'chart'
|
|
39
40
|
&& exportMatch[3] !== 'dimension' && exportMatch[3] !== 'unit')
|
|
@@ -104,6 +105,54 @@ export function parseStatements(body, start = { line: 1, column: 1 }, context =
|
|
|
104
105
|
statements.push(input);
|
|
105
106
|
continue;
|
|
106
107
|
}
|
|
108
|
+
if (/^\s*enum\b/.test(rawLine)) {
|
|
109
|
+
if (context.allowFor === false)
|
|
110
|
+
throw new Error(`Enum declarations cannot occur in loops at ${source.line}:${source.column}`);
|
|
111
|
+
const header = rawLine.match(/^\s*enum\s+([A-Za-z][A-Za-z0-9_]*)\s*=\s*\{\s*$/);
|
|
112
|
+
if (!header)
|
|
113
|
+
syntaxError('Invalid enum declaration', source);
|
|
114
|
+
const members = [];
|
|
115
|
+
let closed = false;
|
|
116
|
+
let closingSource;
|
|
117
|
+
while (++i < lines.length) {
|
|
118
|
+
const memberLine = lines[i];
|
|
119
|
+
if (/^\s*}\s*$/.test(memberLine)) {
|
|
120
|
+
closed = true;
|
|
121
|
+
closingSource = { line: start.line + i, column: memberLine.indexOf('}') + 1 };
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
if (!memberLine.trim())
|
|
125
|
+
continue;
|
|
126
|
+
const match = memberLine.match(/^\s*([A-Za-z][A-Za-z0-9_]*)(?:\s*=\s*(.+?))?\s*,?\s*$/);
|
|
127
|
+
const memberColumn = match
|
|
128
|
+
? memberLine.indexOf(match[1]) + 1
|
|
129
|
+
: memberLine.length - memberLine.trimStart().length + 1;
|
|
130
|
+
const memberSource = { line: start.line + i, column: memberColumn };
|
|
131
|
+
if (!match)
|
|
132
|
+
syntaxError('Invalid enum member', memberSource);
|
|
133
|
+
let value;
|
|
134
|
+
let valueSource;
|
|
135
|
+
if (match[2]) {
|
|
136
|
+
const valueText = match[2].replace(/,\s*$/, '').trim();
|
|
137
|
+
const valueColumn = memberLine.indexOf(valueText, memberColumn - 1) + 1;
|
|
138
|
+
valueSource = { line: memberSource.line, column: valueColumn };
|
|
139
|
+
value = parseExpression(valueText, valueSource);
|
|
140
|
+
}
|
|
141
|
+
members.push({ name: match[1], nameSource: memberSource, ...(value ? { value, valueSource } : {}) });
|
|
142
|
+
}
|
|
143
|
+
if (!closed)
|
|
144
|
+
syntaxError('Unclosed enum declaration', source);
|
|
145
|
+
statements.push({
|
|
146
|
+
type: 'enumDeclaration',
|
|
147
|
+
name: header[1],
|
|
148
|
+
nameSource: { line: source.line, column: rawLine.indexOf(header[1]) + 1 },
|
|
149
|
+
members,
|
|
150
|
+
closingSource,
|
|
151
|
+
...(exported ? { exported: true } : {}),
|
|
152
|
+
source
|
|
153
|
+
});
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
107
156
|
function parseDeclarationExpression(rawLine, expressionText, source) {
|
|
108
157
|
const offset = rawLine.indexOf(expressionText);
|
|
109
158
|
try {
|
|
@@ -148,9 +197,16 @@ export function parseStatements(body, start = { line: 1, column: 1 }, context =
|
|
|
148
197
|
if (/^\s*type\b/.test(rawLine)) {
|
|
149
198
|
if (context.allowFor === false)
|
|
150
199
|
throw new Error(`Type declarations cannot occur in loops at ${source.line}:${source.column}`);
|
|
151
|
-
const header = rawLine.match(/^\s*type\s+([A-Za-z][A-Za-z0-9_]*)\s*\{\s*$/);
|
|
200
|
+
const header = rawLine.match(/^\s*type\s+([A-Za-z][A-Za-z0-9_]*)(?:\s+extends\s+([A-Za-z][A-Za-z0-9_]*(?:\s*,\s*[A-Za-z][A-Za-z0-9_]*)*))?\s*\{\s*$/);
|
|
152
201
|
if (!header)
|
|
153
202
|
throw new Error(`Invalid type declaration at ${source.line}:${source.column}`);
|
|
203
|
+
let parentSearchFrom = rawLine.indexOf('extends') + 'extends'.length;
|
|
204
|
+
const parents = header[2] ? header[2].split(',').map(rawName => {
|
|
205
|
+
const name = rawName.trim();
|
|
206
|
+
const parentIndex = rawLine.indexOf(name, parentSearchFrom);
|
|
207
|
+
parentSearchFrom = parentIndex + name.length;
|
|
208
|
+
return { name, source: { line: source.line, column: parentIndex + 1 } };
|
|
209
|
+
}) : undefined;
|
|
154
210
|
const fields = [];
|
|
155
211
|
let closed = false;
|
|
156
212
|
while (++i < lines.length) {
|
|
@@ -161,21 +217,43 @@ export function parseStatements(body, start = { line: 1, column: 1 }, context =
|
|
|
161
217
|
}
|
|
162
218
|
if (!fieldLine.trim())
|
|
163
219
|
continue;
|
|
164
|
-
const match = fieldLine.match(/^\s*([A-Za-z][A-Za-z0-9_]*)(\?)?\s*:\s*([A-Za-z][A-Za-z0-9_]*(?:(?:\[\])|\?)*)\s*(?:=\s*(.+))?\s*$/);
|
|
165
|
-
const
|
|
220
|
+
const match = fieldLine.match(/^\s*(?:(override)\s+)?([A-Za-z][A-Za-z0-9_]*)(\?)?\s*:\s*([A-Za-z][A-Za-z0-9_]*(?:(?:\[\])|\?)*)\s*(?:=\s*(.+))?\s*$/);
|
|
221
|
+
const fieldColumn = match
|
|
222
|
+
? fieldLine.indexOf(match[2], match[1] ? fieldLine.indexOf(match[1]) + match[1].length : 0) + 1
|
|
223
|
+
: fieldLine.length - fieldLine.trimStart().length + 1;
|
|
224
|
+
const fieldSource = { line: start.line + i, column: fieldColumn };
|
|
166
225
|
if (!match)
|
|
167
226
|
throw new Error(`Invalid record field at ${fieldSource.line}:${fieldSource.column}`);
|
|
168
|
-
|
|
169
|
-
|
|
227
|
+
const overrideSource = match[1]
|
|
228
|
+
? { line: fieldSource.line, column: fieldLine.indexOf(match[1]) + 1 }
|
|
229
|
+
: undefined;
|
|
230
|
+
fields.push({ name: match[2], optional: !!match[3], annotation: parseTypeReference(match[4], fieldSource),
|
|
231
|
+
...(match[1] ? { override: true, overrideSource } : {}),
|
|
232
|
+
...(match[5] ? { defaultExpression: parseExpression(match[5], { line: fieldSource.line, column: fieldLine.indexOf(match[5]) + 1 }) } : {}), source: fieldSource });
|
|
170
233
|
}
|
|
171
234
|
if (!closed)
|
|
172
235
|
throw new Error(`Unclosed type declaration at ${source.line}:${source.column}`);
|
|
173
|
-
statements.push({
|
|
236
|
+
statements.push({
|
|
237
|
+
type: 'typeDeclaration',
|
|
238
|
+
name: header[1],
|
|
239
|
+
nameSource: { line: source.line, column: rawLine.indexOf(header[1]) + 1 },
|
|
240
|
+
fields,
|
|
241
|
+
...(parents ? { parents } : {}),
|
|
242
|
+
...(exported ? { exported } : {}),
|
|
243
|
+
source
|
|
244
|
+
});
|
|
174
245
|
continue;
|
|
175
246
|
}
|
|
176
247
|
if (context.allowFor === false && containsForExpression(rawLine)) {
|
|
177
248
|
throw new Error(`Nested loops are unsupported at ${source.line}:${source.column}`);
|
|
178
249
|
}
|
|
250
|
+
if (/^\s*if\b/.test(rawLine)) {
|
|
251
|
+
const collected = collectDelimitedExpression(lines, i, rawLine.trimStart());
|
|
252
|
+
const parsed = parseBracedIfStatement(collected.text, source, context);
|
|
253
|
+
statements.push(parsed.statement);
|
|
254
|
+
i += parsed.lineCount - 1;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
179
257
|
if (/^\s*for\b/.test(rawLine)) {
|
|
180
258
|
if (context.allowFor === false) {
|
|
181
259
|
throw new Error(`Nested loops are unsupported at ${source.line}:${source.column}`);
|