tla-ts 0.0.3-dev.d9ff6f2 → 0.0.3-dev.e5b05b9

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.
@@ -14,10 +14,13 @@ import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
15
  import {
16
16
  createSanyError,
17
+ formatRange,
18
+ formatSanyWarnings,
17
19
  TlaConfigError,
18
20
  TlaEvaluationError,
19
21
  TlaModuleError,
20
22
  TlaSyntaxError,
23
+ type SanyError,
21
24
  } from "./errors";
22
25
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
23
26
  import {
@@ -38,21 +41,29 @@ import {
38
41
  import { overrideDefinitions } from "./java-overrides";
39
42
  import { extractModule, parseModule } from "./parser";
40
43
  import {
41
- checkRedefinitions,
42
44
  getArity,
45
+ getParamArities,
43
46
  isBefore,
47
+ isTheoremDefinition,
44
48
  KEY_SEPARATOR,
45
49
  LOCAL_RESOLUTION,
50
+ MODULE_START,
46
51
  resolveModule,
47
52
  resolveSubstitutions,
53
+ type ModuleReport,
48
54
  type ModuleSymbol,
55
+ type ModuleUnit,
56
+ type SymbolKind,
49
57
  type Symbols,
50
58
  } from "./resolve";
51
59
  import { createRuntime } from "./runtime";
60
+ import { parseModuleFile } from "./sany-parse-error";
52
61
  import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
62
+ import { STANDARD_LEVELS } from "./standard-levels";
63
+ import { getStandardRange } from "./standard-ranges";
53
64
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
54
65
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
55
- import { checkInstances, type InstanceCheck } from "./substitution-levels";
66
+ import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
56
67
  import { getTlcDeclarationOrder } from "./tlc-order";
57
68
  import {
58
69
  checkInteger,
@@ -112,18 +123,28 @@ interface LinkedModule {
112
123
  exported: Symbols;
113
124
  modules: Map<string, LinkedModule>;
114
125
  extended: LinkedModule[];
126
+ facts: Set<string>;
115
127
  }
116
128
 
129
+ // errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
130
+ // the modules in its file, which SANY runs only when it reports no error.
117
131
  interface ModuleScope {
118
132
  id: string;
119
133
  symbols: Symbols;
120
134
  modules: Map<string, LinkedModule>;
121
135
  nested: Module[];
136
+ errors: SanyError[];
137
+ warnings: SanyError[];
138
+ levelChecks: (() => void)[];
122
139
  }
123
140
 
124
- const getDeclarations = (linked: LinkedModule) => [
141
+ const getDeclarations = (linked: LinkedModule): Declaration[] => [
125
142
  ...linked.constants,
126
- ...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
143
+ ...linked.variables.map((variable, index) => ({
144
+ name: variable,
145
+ arity: 0,
146
+ location: linked.variableLocations[index],
147
+ })),
127
148
  ];
128
149
 
129
150
  const createModuleRedefinedError = (module: Module) =>
@@ -131,6 +152,22 @@ const createModuleRedefinedError = (module: Module) =>
131
152
  `Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
132
153
  );
133
154
 
155
+ const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
156
+ module.location.range &&
157
+ existing.location.range && {
158
+ location: formatRange(module.location.range),
159
+ message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
160
+ };
161
+
162
+ // Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
163
+ // declaration.
164
+ const getNodeWord = (kind: SymbolKind) =>
165
+ ["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
166
+
167
+ // The Java class of the node SANY binds a symbol to.
168
+ const getNodeClass = (kind: SymbolKind) =>
169
+ kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
170
+
134
171
  const createLocalFunction = (definition: Definition, key: string): Definition => ({
135
172
  kind: "Operator",
136
173
  name: key,
@@ -229,6 +266,8 @@ export const createEnvironment = (
229
266
  const runtime = createRuntime(config.checkDeadlock, options.seed);
230
267
  const linkedModules = new Map<string, LinkedModule>();
231
268
  const nestedModules = new Map<string, LinkedModule>();
269
+ // Each module by name, from the time its level checks run.
270
+ const checkedModules = new Map<string, LinkedModule>();
232
271
  const linkedList: LinkedModule[] = [];
233
272
  const dependencies = new Map<string, Set<string>>();
234
273
  const loading: Set<string>[] = [];
@@ -243,7 +282,25 @@ export const createEnvironment = (
243
282
  const copyCount = { value: 0 };
244
283
  const createName = (name: string) => `${name}#${copyCount.value++}`;
245
284
  const instanceChecks: InstanceCheck[] = [];
285
+ // The warnings SANY reports for the spec.
286
+ const warnings: SanyError[] = [];
287
+ const checkInstance = createInstanceChecker((name) => {
288
+ const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
289
+ return {
290
+ constants: linked.constants,
291
+ variables: linked.variables,
292
+ definitions: [
293
+ ...new Set(
294
+ [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
295
+ ),
296
+ ],
297
+ assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
298
+ theorems: linked.module.theorems,
299
+ getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
300
+ };
301
+ });
246
302
  const instanceActions = new Map<string, InstanceAction>();
303
+ const qualifiedNames = new Map<string, string>();
247
304
 
248
305
  const addStandardModule = (name: string): Symbols | undefined => {
249
306
  const cached = standardExports.get(name);
@@ -258,7 +315,13 @@ export const createEnvironment = (
258
315
  );
259
316
  standardModule.operators.forEach((operator) => {
260
317
  builtins.set(operator.name, operator);
261
- exported.set(operator.name, { key: operator.name, arity: operator.arity });
318
+ exported.set(operator.name, {
319
+ key: operator.name,
320
+ arity: operator.arity,
321
+ params: STANDARD_LEVELS.get(operator.name)?.formals.map((formal) => formal.arity),
322
+ kind: "Definition",
323
+ range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
324
+ });
262
325
  });
263
326
  standardExports.set(name, exported);
264
327
  return exported;
@@ -266,7 +329,11 @@ export const createEnvironment = (
266
329
 
267
330
  const getStandardSymbols = (name: string) => {
268
331
  const symbols = addStandardModule(name);
269
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
332
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
333
+ if (symbols) useModule(name, dependencies);
334
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
335
+ // overrides the arithmetic of Naturals.
336
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
270
337
  return symbols;
271
338
  };
272
339
 
@@ -304,7 +371,7 @@ export const createEnvironment = (
304
371
  ],
305
372
  true,
306
373
  );
307
- const module = parseModule(source);
374
+ const module = parseModuleFile(source, name);
308
375
  if (module.name !== name) {
309
376
  throw createSanyError(
310
377
  [
@@ -329,6 +396,8 @@ export const createEnvironment = (
329
396
  }
330
397
  const closure = new Set<string>();
331
398
  const nested: Module[] = [];
399
+ const errors: SanyError[] = [];
400
+ const levelChecks: (() => void)[] = [];
332
401
  linking.push(parsed.name);
333
402
  loading.push(closure);
334
403
  const linked = linkModule(parsed, {
@@ -336,11 +405,19 @@ export const createEnvironment = (
336
405
  symbols: new Map(),
337
406
  modules: new Map(),
338
407
  nested,
408
+ errors,
409
+ warnings,
410
+ levelChecks,
339
411
  });
340
412
  loading.pop();
341
413
  linking.pop();
414
+ if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
342
415
  const duplicate = nested.find((module) => closure.has(module.name));
416
+ const loaded = duplicate && linkedModules.get(duplicate.name);
417
+ const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
418
+ if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
343
419
  if (duplicate) throw createModuleRedefinedError(duplicate);
420
+ levelChecks.forEach((check) => check());
344
421
  return { linked, closure };
345
422
  };
346
423
 
@@ -360,12 +437,7 @@ export const createEnvironment = (
360
437
  const instantiate = (
361
438
  instance: Instance,
362
439
  module: Module,
363
- unit: {
364
- imported: Symbols;
365
- importedAt: Map<string, SourceLocation>;
366
- definitions: Map<string, Definition>;
367
- getKey: (definition: Definition) => string;
368
- },
440
+ unit: ModuleUnit,
369
441
  isParameterFree: boolean,
370
442
  nested: LinkedModule | undefined,
371
443
  ): Symbols => {
@@ -373,10 +445,16 @@ export const createEnvironment = (
373
445
  if (standardSymbols) {
374
446
  if (instance.name === undefined) return standardSymbols;
375
447
  return new Map(
376
- [...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
448
+ [...standardSymbols].map(([name, symbol]) => [
449
+ `${instance.name}!${name}`,
450
+ { key: symbol.key, arity: symbol.arity, params: symbol.params, kind: symbol.kind },
451
+ ]),
377
452
  );
378
453
  }
379
454
  const linked = nested ?? getLinkedModule(instance.module, module.name);
455
+ linked.facts.forEach((name) =>
456
+ unit.facts.add(instance.name === undefined ? name : `${instance.name}!${name}`),
457
+ );
380
458
  const declarations = getDeclarations(linked);
381
459
  const suffix = `#${copyCount.value++}`;
382
460
  const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
@@ -418,16 +496,26 @@ export const createEnvironment = (
418
496
  const key = instantiation.getKey(symbol.key);
419
497
  if (declarations.length > 0)
420
498
  instanceActions.set(key, { name, location: instance.location });
421
- symbols.set(name, { key, arity: symbol.arity });
499
+ // SANY binds a copy of a definition that depends on the parameters at the INSTANCE.
500
+ symbols.set(name, {
501
+ key,
502
+ arity: symbol.arity,
503
+ params: symbol.params,
504
+ kind: symbol.kind,
505
+ range: parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
506
+ });
422
507
  return;
423
508
  }
424
509
  const key = instantiation.getQualifiedKey(symbol.key);
425
510
  if (declarations.length > 0) {
426
511
  instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
427
512
  }
513
+ qualifiedNames.set(key, `${instance.name}!${name}`);
428
514
  symbols.set(`${instance.name}!${name}`, {
429
515
  key,
430
516
  arity: symbol.arity + instance.params.length,
517
+ params: symbol.params && [...instance.params.map((param) => param.arity), ...symbol.params],
518
+ kind: symbol.kind,
431
519
  });
432
520
  });
433
521
  return symbols;
@@ -605,29 +693,59 @@ export const createEnvironment = (
605
693
 
606
694
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
607
695
  const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
696
+ const firstCheck = instanceChecks.length;
608
697
  const visible: Symbols = new Map([
609
698
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
610
699
  operator.name,
611
- { key: operator.name, arity: operator.arity },
700
+ { key: operator.name, arity: operator.arity, kind: "Builtin" },
612
701
  ]),
613
702
  ...scope.symbols,
614
703
  ]);
704
+ const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
705
+ const reports = [extendsReport];
706
+ // Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
707
+ // and warns of two definitions or two declarations but rejects a definition and a declaration.
708
+ const mergeExtended = (symbols: Symbols) =>
709
+ symbols.forEach((symbol, name) => {
710
+ const current = exported.get(name);
711
+ if (!current) {
712
+ visible.set(name, symbol);
713
+ exported.set(name, symbol);
714
+ return;
715
+ }
716
+ if (
717
+ !current.range ||
718
+ !symbol.range ||
719
+ formatRange(current.range) === formatRange(symbol.range)
720
+ ) {
721
+ return;
722
+ }
723
+ const location = formatRange(symbol.range);
724
+ const existing = formatRange(current.range);
725
+ if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
726
+ extendsReport.warnings.push({
727
+ location,
728
+ message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
729
+ });
730
+ return;
731
+ }
732
+ extendsReport.errors.push({
733
+ location,
734
+ message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
735
+ });
736
+ });
615
737
  const exported: Symbols = new Map();
738
+ const facts = new Set<string>();
616
739
  const modules = new Map<string, LinkedModule>();
617
740
  const extended: LinkedModule[] = [];
618
741
  const constants = [...parsed.constants];
619
742
  const variables = parsed.variables.map((variable) => variable.name);
620
743
  const variableLocations = parsed.variables.map((variable) => variable.location);
621
- const addSymbols = (symbols: Symbols, isExported: boolean) =>
622
- symbols.forEach((symbol, name) => {
623
- visible.set(name, symbol);
624
- if (isExported) exported.set(name, symbol);
625
- });
626
744
  parsed.extends.forEach((name) => {
627
745
  const nested = scope.modules.get(name);
628
746
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
629
747
  if (standardSymbols) {
630
- addSymbols(standardSymbols, true);
748
+ mergeExtended(standardSymbols);
631
749
  return;
632
750
  }
633
751
  const linked = nested ?? getLinkedModule(name, parsed.name);
@@ -641,61 +759,87 @@ export const createEnvironment = (
641
759
  variables.push(variable);
642
760
  variableLocations.push(linked.variableLocations[index]);
643
761
  });
644
- addSymbols(linked.exported, true);
762
+ mergeExtended(linked.exported);
763
+ linked.facts.forEach((name) => facts.add(name));
645
764
  linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
646
765
  extended.push(linked);
647
766
  });
648
- parsed.constants.forEach((constant) =>
649
- exported.set(constant.name, {
650
- key: constant.name,
651
- arity: constant.arity,
652
- declaration: "Constant",
653
- }),
654
- );
655
- parsed.variables.forEach((variable) =>
656
- exported.set(variable.name, { key: variable.name, arity: 0, declaration: "Variable" }),
767
+ const bound = new Map(visible);
768
+ const declarations = [
769
+ ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
770
+ ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
771
+ ];
772
+ const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
773
+ key: declaration.name,
774
+ arity: declaration.arity,
775
+ kind: declaration.kind,
776
+ range: declaration.location.range,
777
+ });
778
+ declarations.forEach((declaration) =>
779
+ exported.set(declaration.name, getDeclarationSymbol(declaration)),
657
780
  );
781
+ const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
782
+ key: getKey(definition),
783
+ arity: getArity(definition),
784
+ params: getParamArities(definition),
785
+ kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
786
+ range: definition.location.range,
787
+ });
658
788
  const isParameterFree = constants.length === 0 && variables.length === 0;
659
789
  const getKey = (definition: Definition) =>
660
790
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
661
- const importedAt = new Map<string, SourceLocation>();
662
- const getUnit = (module: Module) => ({
791
+ const instanced = new Map<Instance, Symbols>();
792
+ const getUnit = (module: Module): ModuleUnit => ({
663
793
  imported: visible,
664
- importedAt,
665
- definitions: new Map(module.definitions.map((definition) => [definition.name, definition])),
794
+ bound,
795
+ // SANY keeps the first of two definitions of a name.
796
+ definitions: new Map(
797
+ module.definitions.toReversed().map((definition) => [definition.name, definition]),
798
+ ),
666
799
  getKey,
800
+ instanced,
801
+ facts,
802
+ reports,
803
+ warnings: scope.warnings,
667
804
  });
668
805
  const getVisibleModules = () => new Map([...scope.modules, ...modules]);
669
806
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
670
807
  new Map([
671
808
  ...visible,
672
- ...[
673
- ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
674
- ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
675
- ]
809
+ ...declarations
676
810
  .filter((declaration) => isBefore(declaration.location, location))
677
811
  .map((declaration): [string, ModuleSymbol] => [
678
812
  declaration.name,
679
- { key: declaration.name, arity: declaration.arity, declaration: declaration.kind },
813
+ getDeclarationSymbol(declaration),
680
814
  ]),
681
815
  ...parsed.definitions
682
816
  .filter((definition) => isBefore(definition.location, location))
683
817
  .map((definition): [string, ModuleSymbol] => [
684
818
  definition.name,
685
- { key: getKey(definition), arity: getArity(definition) },
819
+ getDefinitionSymbol(definition),
686
820
  ]),
687
821
  ]);
822
+ // Generator.generateModule reports the errors of a nested module and goes on, and then
823
+ // SymbolTable.addModule reports a module already defined.
688
824
  const linkNested = (nested: Module) => {
689
825
  const visibleModules = getVisibleModules();
690
- if (visibleModules.has(nested.name)) throw createModuleRedefinedError(nested);
826
+ const existing = visibleModules.get(nested.name);
827
+ const redefinition = existing && getModuleRedefinition(nested, existing.module);
828
+ if (existing && !redefinition) throw createModuleRedefinedError(nested);
691
829
  scope.nested.push(nested);
830
+ const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
831
+ reports.push(report);
692
832
  const linked = linkModule(nested, {
693
833
  id: `${scope.id}/${nested.name}`,
694
834
  symbols: getSymbolsBefore(nested.location),
695
835
  modules: visibleModules,
696
836
  nested: scope.nested,
837
+ errors: report.errors,
838
+ warnings: report.warnings,
839
+ levelChecks: scope.levelChecks,
697
840
  });
698
- modules.set(nested.name, linked);
841
+ if (redefinition) report.errors.push(redefinition);
842
+ if (!existing) modules.set(nested.name, linked);
699
843
  if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
700
844
  };
701
845
  [
@@ -709,10 +853,12 @@ export const createEnvironment = (
709
853
  isParameterFree,
710
854
  getVisibleModules().get(instance.module),
711
855
  );
712
- symbols.forEach((_, name) => {
713
- if (!visible.has(name)) importedAt.set(name, instance.location);
856
+ instanced.set(instance, symbols);
857
+ symbols.forEach((symbol, name) => {
858
+ if (visible.has(name)) return;
859
+ visible.set(name, symbol);
860
+ if (!instance.isLocal) exported.set(name, symbol);
714
861
  });
715
- addSymbols(symbols, !instance.isLocal);
716
862
  },
717
863
  })),
718
864
  ...parsed.modules.map((nested) => ({
@@ -722,7 +868,6 @@ export const createEnvironment = (
722
868
  ]
723
869
  .sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
724
870
  .forEach((unit) => unit.link());
725
- checkRedefinitions(parsed, getUnit(parsed));
726
871
  const isInstancedAfterDefinition = (nested: Module) => {
727
872
  const first = parsed.instances.find((instance) => instance.module === nested.name);
728
873
  return first !== undefined && isBefore(nested.location, first.location);
@@ -738,20 +883,15 @@ export const createEnvironment = (
738
883
  ),
739
884
  getAvailableDefinitions(visible),
740
885
  );
741
- resolveModule(module, getUnit(module));
886
+ scope.errors.push(...resolveModule(module, getUnit(module)));
742
887
  module.definitions.forEach((definition) => {
743
888
  const key = getKey(definition);
744
889
  definitions.set(key, definition);
745
890
  parameterized.set(key, !isParameterFree);
746
- const symbol = { key, arity: getArity(definition) };
891
+ const symbol = getDefinitionSymbol(definition);
747
892
  visible.set(definition.name, symbol);
748
893
  if (!definition.isLocal) exported.set(definition.name, symbol);
749
894
  });
750
- checkModuleLevels(module, {
751
- constants: new Set(constants.map((constant) => constant.name)),
752
- variables: new Set(variables),
753
- getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
754
- });
755
895
  const linked = {
756
896
  module,
757
897
  constants,
@@ -761,12 +901,36 @@ export const createEnvironment = (
761
901
  exported,
762
902
  modules,
763
903
  extended,
904
+ facts: new Set([
905
+ ...facts,
906
+ ...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
907
+ ]),
764
908
  };
909
+ if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
910
+ // SANY checks the instances of a module with its definitions and theorems, so it reports their
911
+ // errors together.
912
+ const checks = instanceChecks
913
+ .splice(firstCheck)
914
+ .map((check) => ({ location: check.location, check: () => checkInstance(check) }));
915
+ scope.levelChecks.push(() =>
916
+ checkModuleLevels(
917
+ module,
918
+ {
919
+ constants: new Set(constants.map((constant) => constant.name)),
920
+ variables: new Set(variables),
921
+ getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
922
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
923
+ },
924
+ checks,
925
+ warnings,
926
+ ),
927
+ );
765
928
  linkedList.push(linked);
766
929
  return linked;
767
930
  };
768
931
 
769
932
  const linkedRoot = linkTopLevel(root).linked;
933
+ if (warnings.length > 0) options.print(formatSanyWarnings(warnings));
770
934
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
771
935
  linkedRoot.visible.forEach((symbol, name) => {
772
936
  const definition = definitions.get(symbol.key);
@@ -956,20 +1120,6 @@ export const createEnvironment = (
956
1120
  constantLevels: new Map(),
957
1121
  evaluators: new WeakMap(),
958
1122
  };
959
- checkInstances(instanceChecks, (name) => {
960
- const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
961
- return {
962
- constants: linked.constants,
963
- variables: linked.variables,
964
- definitions: [
965
- ...new Set(
966
- [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
967
- ),
968
- ],
969
- assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
970
- getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
971
- };
972
- });
973
1123
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
974
1124
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
975
1125
  // a minimum level as state-level, so the analyzer takes them for variables.
package/src/errors.ts CHANGED
@@ -34,10 +34,14 @@ export interface SanyError {
34
34
  message: string;
35
35
  }
36
36
 
37
- // The errors SANY reports for a spec. A fatal error stops it before semantic analysis.
37
+ // The errors SANY reports for a spec, and the warnings it prints before them. A fatal error stops it
38
+ // before semantic analysis. SANY's parser prints its message for a module it rejects before the
39
+ // report.
38
40
  export interface SanyReport {
39
41
  errors: SanyError[];
42
+ warnings: SanyError[];
40
43
  isFatal: boolean;
44
+ parseError?: string;
41
45
  }
42
46
 
43
47
  export class TlaSyntaxError extends TlaError {
@@ -52,14 +56,32 @@ export class TlaSyntaxError extends TlaError {
52
56
  }
53
57
  }
54
58
 
55
- export const createSanyError = (errors: SanyError[], isFatal = false) =>
59
+ export const createSanyError = (
60
+ errors: SanyError[],
61
+ isFatal = false,
62
+ parseError?: string,
63
+ warnings: SanyError[] = [],
64
+ ) =>
56
65
  new TlaSyntaxError(
57
66
  errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
58
67
  undefined,
59
68
  undefined,
60
- { errors, isFatal },
69
+ { errors, warnings, isFatal, parseError },
61
70
  );
62
71
 
72
+ // How SANY prints the warnings it reports, which do not stop TLC.
73
+ export const formatSanyWarnings = (warnings: SanyError[]) =>
74
+ `*** Warnings: ${warnings.length}\n\n${warnings.map((warning) => `${warning.location}\n\n${warning.message}\n\n\n`).join("\n")}`;
75
+
76
+ // SANY's message for a module its parser rejects, such as a "***Parse Error***" with the
77
+ // productions it was in.
78
+ export class TlaParseError extends TlaError {
79
+ constructor(message: string) {
80
+ super(message);
81
+ this.name = "TlaParseError";
82
+ }
83
+ }
84
+
63
85
  export const formatRange = (range: SourceRange) =>
64
86
  `line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
65
87
 
@@ -92,6 +114,17 @@ export class TlaModuleError extends TlaEvaluationError {
92
114
  }
93
115
  }
94
116
 
117
+ // A variable that a partial state leaves without a value.
118
+ export class TlaUnassignedError extends TlaEvaluationError {
119
+ variable: string;
120
+
121
+ constructor(message: string, variable: string) {
122
+ super(message);
123
+ this.name = "TlaEvaluationError";
124
+ this.variable = variable;
125
+ }
126
+ }
127
+
95
128
  // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
96
129
  // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
97
130
  export class TlaIncompleteStateError extends TlaEvaluationError {
@@ -110,6 +143,17 @@ export class TlaIncompleteStateError extends TlaEvaluationError {
110
143
  }
111
144
  }
112
145
 
146
+ // TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
147
+ export class TlaIllegalStateError extends TlaEvaluationError {
148
+ state: (Value | undefined)[];
149
+
150
+ constructor(state: (Value | undefined)[]) {
151
+ super("current state is not a legal state");
152
+ this.name = "TlaEvaluationError";
153
+ this.state = state;
154
+ }
155
+ }
156
+
113
157
  export interface TlapmReport {
114
158
  failure?: string;
115
159
  exception?: string;