tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.ab9c809
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/dist/index.cjs +1 -1
- package/dist/index.d.cts +26 -1
- package/dist/index.d.ts +26 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +14 -12
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +30 -17
- package/dist/transfer.js +30 -17
- package/package.json +1 -1
- package/src/ast.ts +6 -1
- package/src/builtins.ts +9 -3
- package/src/check.ts +234 -53
- package/src/config.ts +224 -170
- package/src/environment.ts +66 -27
- package/src/errors.ts +63 -1
- package/src/evaluate.ts +131 -13
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +173 -37
- package/src/liveness.ts +5 -1
- package/src/parser.ts +60 -17
- package/src/resolve.ts +108 -29
- package/src/run-cli.ts +59 -11
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +52 -38
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +87 -17
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
package/src/resolve.ts
CHANGED
|
@@ -13,9 +13,16 @@ import {
|
|
|
13
13
|
type Proof,
|
|
14
14
|
type ProofStep,
|
|
15
15
|
type SourceLocation,
|
|
16
|
+
type SourceRange,
|
|
16
17
|
type Theorem,
|
|
17
18
|
} from "./ast";
|
|
18
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
createSanyError,
|
|
21
|
+
createSemanticError,
|
|
22
|
+
formatRange,
|
|
23
|
+
TlaSyntaxError,
|
|
24
|
+
type SanyError,
|
|
25
|
+
} from "./errors";
|
|
19
26
|
|
|
20
27
|
export interface ModuleSymbol {
|
|
21
28
|
key: string;
|
|
@@ -103,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
|
103
110
|
: unit.imported.get(name);
|
|
104
111
|
};
|
|
105
112
|
|
|
113
|
+
// SANY reports a name at the range of its text.
|
|
114
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
115
|
+
module: location.range?.module ?? "",
|
|
116
|
+
start: location,
|
|
117
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
121
|
+
const getFixity = (expression: Expression) => {
|
|
122
|
+
if (expression.kind !== "Apply") return undefined;
|
|
123
|
+
if (expression.args.length === 2) return "infix";
|
|
124
|
+
if (expression.args.length !== 1) return undefined;
|
|
125
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
126
|
+
};
|
|
127
|
+
|
|
106
128
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
107
129
|
const isOperatorSymbol = (name: string) =>
|
|
108
130
|
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
@@ -135,7 +157,7 @@ const getSymbolArity = (
|
|
|
135
157
|
}
|
|
136
158
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
137
159
|
// arguments.
|
|
138
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
160
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
139
161
|
const isSelected =
|
|
140
162
|
prefix !== "" &&
|
|
141
163
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -144,11 +166,23 @@ const getSymbolArity = (
|
|
|
144
166
|
if (isSelected) {
|
|
145
167
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
146
168
|
}
|
|
147
|
-
const
|
|
148
|
-
throw
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
169
|
+
const range = getNameRange(location, name === "-." ? "-" : name);
|
|
170
|
+
if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
|
|
171
|
+
const missing = {
|
|
172
|
+
location: formatRange(range),
|
|
173
|
+
message: `Could not find declaration or definition of symbol '${name}'.`,
|
|
174
|
+
};
|
|
175
|
+
const fixity = getFixity(expression);
|
|
176
|
+
throw createSanyError(
|
|
177
|
+
fixity && location.range
|
|
178
|
+
? [
|
|
179
|
+
missing,
|
|
180
|
+
{
|
|
181
|
+
location: formatRange(location.range),
|
|
182
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
|
|
183
|
+
},
|
|
184
|
+
]
|
|
185
|
+
: [missing],
|
|
152
186
|
);
|
|
153
187
|
};
|
|
154
188
|
|
|
@@ -235,13 +269,13 @@ const resolveApply = (
|
|
|
235
269
|
scope: LocalName | undefined,
|
|
236
270
|
unit: Unit,
|
|
237
271
|
) => {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
272
|
+
const operator = expression.operator;
|
|
273
|
+
const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
|
|
274
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
242
275
|
if (arity !== expression.args.length) {
|
|
243
|
-
throw
|
|
244
|
-
`The operator ${
|
|
276
|
+
throw createSemanticError(
|
|
277
|
+
`The operator ${operator} requires ${arity} arguments.`,
|
|
278
|
+
expression.argumentsRange ?? getNameRange(expression.location, operator),
|
|
245
279
|
);
|
|
246
280
|
}
|
|
247
281
|
expression.args.forEach((arg, index) => {
|
|
@@ -275,10 +309,12 @@ const resolveExpression = (
|
|
|
275
309
|
case "ExceptAt":
|
|
276
310
|
return;
|
|
277
311
|
case "Identifier": {
|
|
278
|
-
const
|
|
312
|
+
const name = expression.name;
|
|
313
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
279
314
|
if (arity > 0) {
|
|
280
|
-
throw
|
|
281
|
-
`The operator ${
|
|
315
|
+
throw createSemanticError(
|
|
316
|
+
`The operator ${name} requires ${arity} arguments.`,
|
|
317
|
+
getNameRange(expression.location, name),
|
|
282
318
|
);
|
|
283
319
|
}
|
|
284
320
|
return;
|
|
@@ -347,14 +383,21 @@ const resolveExpression = (
|
|
|
347
383
|
resolveAll([expression.domain, expression.range]);
|
|
348
384
|
return;
|
|
349
385
|
case "Record":
|
|
350
|
-
case "RecordSet":
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
386
|
+
case "RecordSet": {
|
|
387
|
+
const repeated = expression.fields.filter((field, index) =>
|
|
388
|
+
expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
|
|
389
|
+
);
|
|
390
|
+
if (repeated.length > 0) {
|
|
391
|
+
throw createSanyError(
|
|
392
|
+
repeated.map((field) => ({
|
|
393
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
394
|
+
message: "Non-unique fields in constructor.",
|
|
395
|
+
})),
|
|
354
396
|
);
|
|
355
397
|
}
|
|
356
398
|
resolveAll(expression.fields.map((field) => field.value));
|
|
357
399
|
return;
|
|
400
|
+
}
|
|
358
401
|
case "Except":
|
|
359
402
|
resolveAll([
|
|
360
403
|
expression.function,
|
|
@@ -769,6 +812,31 @@ export const resolveSubstitutions = (
|
|
|
769
812
|
);
|
|
770
813
|
};
|
|
771
814
|
|
|
815
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module.
|
|
816
|
+
export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
|
|
817
|
+
const errors: SanyError[] = [];
|
|
818
|
+
units
|
|
819
|
+
.toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
820
|
+
.forEach((unit) => {
|
|
821
|
+
try {
|
|
822
|
+
unit.check();
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
|
|
825
|
+
error.sany.errors.forEach((item) => {
|
|
826
|
+
if (
|
|
827
|
+
errors.every(
|
|
828
|
+
(existing) =>
|
|
829
|
+
existing.location !== item.location || existing.message !== item.message,
|
|
830
|
+
)
|
|
831
|
+
) {
|
|
832
|
+
errors.push(item);
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
838
|
+
};
|
|
839
|
+
|
|
772
840
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
773
841
|
const symbols = createSymbols(module, unit.imported);
|
|
774
842
|
const facts = new Set(
|
|
@@ -782,13 +850,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
782
850
|
self,
|
|
783
851
|
facts,
|
|
784
852
|
});
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
853
|
+
collectSemanticErrors([
|
|
854
|
+
...module.definitions.map((definition) => ({
|
|
855
|
+
location: definition.location,
|
|
856
|
+
check: () =>
|
|
857
|
+
resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
|
|
858
|
+
})),
|
|
859
|
+
...module.assumptions.map((assumption) => ({
|
|
860
|
+
location: assumption.location,
|
|
861
|
+
check: () =>
|
|
862
|
+
resolveExpression(
|
|
863
|
+
assumption.expression,
|
|
864
|
+
undefined,
|
|
865
|
+
createUnit(assumption.location, undefined),
|
|
866
|
+
),
|
|
867
|
+
})),
|
|
868
|
+
...module.theorems.map((theorem) => ({
|
|
869
|
+
location: theorem.location,
|
|
870
|
+
check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
|
|
871
|
+
})),
|
|
872
|
+
]);
|
|
794
873
|
};
|
package/src/run-cli.ts
CHANGED
|
@@ -10,23 +10,25 @@ import {
|
|
|
10
10
|
import { spawnSync } from "node:child_process";
|
|
11
11
|
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
12
12
|
import { availableParallelism } from "node:os";
|
|
13
|
-
import type { CheckResult, CheckStatus, TraceStep } from "./check";
|
|
13
|
+
import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
|
|
14
14
|
import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
15
15
|
import { parseConfig } from "./config";
|
|
16
16
|
import {
|
|
17
|
+
createSanyError,
|
|
17
18
|
getErrorExitCode,
|
|
18
19
|
TlaConfigError,
|
|
19
20
|
TlaError,
|
|
20
21
|
TlaEvaluationError,
|
|
21
22
|
TlaProofError,
|
|
22
23
|
TlaSyntaxError,
|
|
24
|
+
type SanyReport,
|
|
23
25
|
} from "./errors";
|
|
24
26
|
import { EXIT_STATUS } from "./exit-codes";
|
|
25
27
|
import type { LineRange } from "./obligation-generate";
|
|
26
28
|
import { getAtPrefix, getLocusText } from "./obligation-expression";
|
|
27
29
|
import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
|
|
28
30
|
import { check } from "./parallel";
|
|
29
|
-
import {
|
|
31
|
+
import { parseModuleFile } from "./sany-parse-error";
|
|
30
32
|
import type { StateDump } from "./state-graph";
|
|
31
33
|
import {
|
|
32
34
|
formatProbability,
|
|
@@ -135,14 +137,42 @@ const getTemporalHeadline = (names: string[]) =>
|
|
|
135
137
|
? `Error: Temporal property ${names[0]} was violated.\n`
|
|
136
138
|
: `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
|
|
137
139
|
|
|
140
|
+
const getFrames = (callStack: string[]) =>
|
|
141
|
+
callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
|
|
142
|
+
|
|
143
|
+
const printCallStack = (frames: string) =>
|
|
144
|
+
console.log(
|
|
145
|
+
`Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const printReplay = (replay: Replay) => {
|
|
149
|
+
replay.output.forEach((line) => console.log(line));
|
|
150
|
+
const frames = getFrames(replay.callStack ?? []);
|
|
151
|
+
const fingerprint = replay.fingerprint;
|
|
152
|
+
if (replay.isBeforeError) {
|
|
153
|
+
if (frames) printCallStack(frames);
|
|
154
|
+
} else if (replay.callStack && fingerprint) {
|
|
155
|
+
const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
|
|
156
|
+
console.log(
|
|
157
|
+
`Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
|
|
158
|
+
);
|
|
159
|
+
} else if (replay.callStack) {
|
|
160
|
+
printCallStack(frames);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
138
164
|
const printResult = (result: CheckResult) => {
|
|
139
165
|
if (result.failure) {
|
|
166
|
+
const replay = result.failure.replay;
|
|
167
|
+
if (replay?.isBeforeError) printReplay(replay);
|
|
140
168
|
console.log(`Error: ${result.failure.message}`);
|
|
141
169
|
if (result.failure.initialState) {
|
|
142
170
|
console.log("While working on the initial state:");
|
|
143
171
|
printState(result.failure.initialState);
|
|
144
172
|
}
|
|
145
173
|
printTrace(result.failure.trace);
|
|
174
|
+
if (replay && !replay.isBeforeError) printReplay(replay);
|
|
175
|
+
if (result.failure.isWithoutStatistics) return;
|
|
146
176
|
} else if (result.violation) {
|
|
147
177
|
const violation = result.violation;
|
|
148
178
|
const name = violation.name;
|
|
@@ -217,10 +247,16 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
|
217
247
|
}
|
|
218
248
|
const source = readFileSync(specPath, "utf8");
|
|
219
249
|
const fileName = basename(specPath, ".tla");
|
|
220
|
-
const moduleName =
|
|
250
|
+
const moduleName = parseModuleFile(source, fileName).name;
|
|
221
251
|
if (moduleName !== fileName) {
|
|
222
|
-
throw
|
|
223
|
-
|
|
252
|
+
throw createSanyError(
|
|
253
|
+
[
|
|
254
|
+
{
|
|
255
|
+
location: "Unknown location",
|
|
256
|
+
message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
|
|
257
|
+
},
|
|
258
|
+
],
|
|
259
|
+
true,
|
|
224
260
|
);
|
|
225
261
|
}
|
|
226
262
|
const configPath =
|
|
@@ -286,21 +322,33 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
|
286
322
|
return result.failure?.exitCode ?? EXIT_CODES[result.status];
|
|
287
323
|
};
|
|
288
324
|
|
|
289
|
-
|
|
290
|
-
|
|
325
|
+
const formatSanyErrors = (report: SanyReport) =>
|
|
326
|
+
`*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
|
|
327
|
+
|
|
328
|
+
// TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
|
|
329
|
+
// others, such as a module it cannot parse.
|
|
330
|
+
const printError = (error: TlaError, path?: string) => {
|
|
291
331
|
if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
|
|
292
332
|
return console.log(`Error: ${error.message}`);
|
|
293
333
|
}
|
|
294
|
-
|
|
334
|
+
const report = error instanceof TlaSyntaxError ? error.sany : undefined;
|
|
335
|
+
if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
|
|
336
|
+
if (report.parseError !== undefined) console.log(report.parseError);
|
|
337
|
+
console.log(
|
|
338
|
+
report.isFatal
|
|
339
|
+
? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
|
|
340
|
+
: `Semantic errors:\n\n${formatSanyErrors(report)}`,
|
|
341
|
+
);
|
|
342
|
+
console.log("Error: Parsing or semantic analysis failed.");
|
|
295
343
|
};
|
|
296
344
|
|
|
297
345
|
// Prints a TlaError and returns its exit code.
|
|
298
|
-
const reportErrors = async (run: () => number | Promise<number
|
|
346
|
+
const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
|
|
299
347
|
try {
|
|
300
348
|
return await run();
|
|
301
349
|
} catch (error) {
|
|
302
350
|
if (!(error instanceof TlaError)) throw error;
|
|
303
|
-
printError(error);
|
|
351
|
+
printError(error, path);
|
|
304
352
|
return getErrorExitCode(error);
|
|
305
353
|
}
|
|
306
354
|
};
|
|
@@ -311,7 +359,7 @@ const runEach = async (paths: string[], runPath: (path: string) => number | Prom
|
|
|
311
359
|
const exitCodes: number[] = [];
|
|
312
360
|
for (const [index, path] of paths.entries()) {
|
|
313
361
|
if (index > 0) console.log("");
|
|
314
|
-
exitCodes.push(await reportErrors(() => runPath(path)));
|
|
362
|
+
exitCodes.push(await reportErrors(() => runPath(path), path));
|
|
315
363
|
}
|
|
316
364
|
return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
|
|
317
365
|
};
|