tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.cfd469d

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,
@@ -114,16 +125,25 @@ interface LinkedModule {
114
125
  extended: LinkedModule[];
115
126
  }
116
127
 
128
+ // errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
129
+ // the modules in its file, which SANY runs only when it reports no error.
117
130
  interface ModuleScope {
118
131
  id: string;
119
132
  symbols: Symbols;
120
133
  modules: Map<string, LinkedModule>;
121
134
  nested: Module[];
135
+ errors: SanyError[];
136
+ warnings: SanyError[];
137
+ levelChecks: (() => void)[];
122
138
  }
123
139
 
124
- const getDeclarations = (linked: LinkedModule) => [
140
+ const getDeclarations = (linked: LinkedModule): Declaration[] => [
125
141
  ...linked.constants,
126
- ...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
142
+ ...linked.variables.map((variable, index) => ({
143
+ name: variable,
144
+ arity: 0,
145
+ location: linked.variableLocations[index],
146
+ })),
127
147
  ];
128
148
 
129
149
  const createModuleRedefinedError = (module: Module) =>
@@ -131,6 +151,22 @@ const createModuleRedefinedError = (module: Module) =>
131
151
  `Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
132
152
  );
133
153
 
154
+ const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
155
+ module.location.range &&
156
+ existing.location.range && {
157
+ location: formatRange(module.location.range),
158
+ message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
159
+ };
160
+
161
+ // Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
162
+ // declaration.
163
+ const getNodeWord = (kind: SymbolKind) =>
164
+ ["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
165
+
166
+ // The Java class of the node SANY binds a symbol to.
167
+ const getNodeClass = (kind: SymbolKind) =>
168
+ kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
169
+
134
170
  const createLocalFunction = (definition: Definition, key: string): Definition => ({
135
171
  kind: "Operator",
136
172
  name: key,
@@ -229,6 +265,8 @@ export const createEnvironment = (
229
265
  const runtime = createRuntime(config.checkDeadlock, options.seed);
230
266
  const linkedModules = new Map<string, LinkedModule>();
231
267
  const nestedModules = new Map<string, LinkedModule>();
268
+ // Each module by name, from the time its level checks run.
269
+ const checkedModules = new Map<string, LinkedModule>();
232
270
  const linkedList: LinkedModule[] = [];
233
271
  const dependencies = new Map<string, Set<string>>();
234
272
  const loading: Set<string>[] = [];
@@ -243,7 +281,25 @@ export const createEnvironment = (
243
281
  const copyCount = { value: 0 };
244
282
  const createName = (name: string) => `${name}#${copyCount.value++}`;
245
283
  const instanceChecks: InstanceCheck[] = [];
284
+ // The warnings SANY reports for the spec.
285
+ const warnings: SanyError[] = [];
286
+ const checkInstance = createInstanceChecker((name) => {
287
+ const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
288
+ return {
289
+ constants: linked.constants,
290
+ variables: linked.variables,
291
+ definitions: [
292
+ ...new Set(
293
+ [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
294
+ ),
295
+ ],
296
+ assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
297
+ theorems: linked.module.theorems,
298
+ getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
299
+ };
300
+ });
246
301
  const instanceActions = new Map<string, InstanceAction>();
302
+ const qualifiedNames = new Map<string, string>();
247
303
 
248
304
  const addStandardModule = (name: string): Symbols | undefined => {
249
305
  const cached = standardExports.get(name);
@@ -258,7 +314,13 @@ export const createEnvironment = (
258
314
  );
259
315
  standardModule.operators.forEach((operator) => {
260
316
  builtins.set(operator.name, operator);
261
- exported.set(operator.name, { key: operator.name, arity: operator.arity });
317
+ exported.set(operator.name, {
318
+ key: operator.name,
319
+ arity: operator.arity,
320
+ params: STANDARD_LEVELS.get(operator.name)?.formals.map((formal) => formal.arity),
321
+ kind: "Definition",
322
+ range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
323
+ });
262
324
  });
263
325
  standardExports.set(name, exported);
264
326
  return exported;
@@ -266,7 +328,11 @@ export const createEnvironment = (
266
328
 
267
329
  const getStandardSymbols = (name: string) => {
268
330
  const symbols = addStandardModule(name);
269
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
331
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
332
+ if (symbols) useModule(name, dependencies);
333
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
334
+ // overrides the arithmetic of Naturals.
335
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
270
336
  return symbols;
271
337
  };
272
338
 
@@ -304,7 +370,7 @@ export const createEnvironment = (
304
370
  ],
305
371
  true,
306
372
  );
307
- const module = parseModule(source);
373
+ const module = parseModuleFile(source, name);
308
374
  if (module.name !== name) {
309
375
  throw createSanyError(
310
376
  [
@@ -329,6 +395,8 @@ export const createEnvironment = (
329
395
  }
330
396
  const closure = new Set<string>();
331
397
  const nested: Module[] = [];
398
+ const errors: SanyError[] = [];
399
+ const levelChecks: (() => void)[] = [];
332
400
  linking.push(parsed.name);
333
401
  loading.push(closure);
334
402
  const linked = linkModule(parsed, {
@@ -336,11 +404,19 @@ export const createEnvironment = (
336
404
  symbols: new Map(),
337
405
  modules: new Map(),
338
406
  nested,
407
+ errors,
408
+ warnings,
409
+ levelChecks,
339
410
  });
340
411
  loading.pop();
341
412
  linking.pop();
413
+ if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
342
414
  const duplicate = nested.find((module) => closure.has(module.name));
415
+ const loaded = duplicate && linkedModules.get(duplicate.name);
416
+ const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
417
+ if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
343
418
  if (duplicate) throw createModuleRedefinedError(duplicate);
419
+ levelChecks.forEach((check) => check());
344
420
  return { linked, closure };
345
421
  };
346
422
 
@@ -360,12 +436,7 @@ export const createEnvironment = (
360
436
  const instantiate = (
361
437
  instance: Instance,
362
438
  module: Module,
363
- unit: {
364
- imported: Symbols;
365
- importedAt: Map<string, SourceLocation>;
366
- definitions: Map<string, Definition>;
367
- getKey: (definition: Definition) => string;
368
- },
439
+ unit: ModuleUnit,
369
440
  isParameterFree: boolean,
370
441
  nested: LinkedModule | undefined,
371
442
  ): Symbols => {
@@ -373,7 +444,10 @@ export const createEnvironment = (
373
444
  if (standardSymbols) {
374
445
  if (instance.name === undefined) return standardSymbols;
375
446
  return new Map(
376
- [...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
447
+ [...standardSymbols].map(([name, symbol]) => [
448
+ `${instance.name}!${name}`,
449
+ { key: symbol.key, arity: symbol.arity, params: symbol.params, kind: symbol.kind },
450
+ ]),
377
451
  );
378
452
  }
379
453
  const linked = nested ?? getLinkedModule(instance.module, module.name);
@@ -418,16 +492,26 @@ export const createEnvironment = (
418
492
  const key = instantiation.getKey(symbol.key);
419
493
  if (declarations.length > 0)
420
494
  instanceActions.set(key, { name, location: instance.location });
421
- symbols.set(name, { key, arity: symbol.arity });
495
+ // SANY binds a copy of a definition that depends on the parameters at the INSTANCE.
496
+ symbols.set(name, {
497
+ key,
498
+ arity: symbol.arity,
499
+ params: symbol.params,
500
+ kind: symbol.kind,
501
+ range: parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
502
+ });
422
503
  return;
423
504
  }
424
505
  const key = instantiation.getQualifiedKey(symbol.key);
425
506
  if (declarations.length > 0) {
426
507
  instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
427
508
  }
509
+ qualifiedNames.set(key, `${instance.name}!${name}`);
428
510
  symbols.set(`${instance.name}!${name}`, {
429
511
  key,
430
512
  arity: symbol.arity + instance.params.length,
513
+ params: symbol.params && [...instance.params.map((param) => param.arity), ...symbol.params],
514
+ kind: symbol.kind,
431
515
  });
432
516
  });
433
517
  return symbols;
@@ -605,29 +689,58 @@ export const createEnvironment = (
605
689
 
606
690
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
607
691
  const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
692
+ const firstCheck = instanceChecks.length;
608
693
  const visible: Symbols = new Map([
609
694
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
610
695
  operator.name,
611
- { key: operator.name, arity: operator.arity },
696
+ { key: operator.name, arity: operator.arity, kind: "Builtin" },
612
697
  ]),
613
698
  ...scope.symbols,
614
699
  ]);
700
+ const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
701
+ const reports = [extendsReport];
702
+ // Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
703
+ // and warns of two definitions or two declarations but rejects a definition and a declaration.
704
+ const mergeExtended = (symbols: Symbols) =>
705
+ symbols.forEach((symbol, name) => {
706
+ const current = exported.get(name);
707
+ if (!current) {
708
+ visible.set(name, symbol);
709
+ exported.set(name, symbol);
710
+ return;
711
+ }
712
+ if (
713
+ !current.range ||
714
+ !symbol.range ||
715
+ formatRange(current.range) === formatRange(symbol.range)
716
+ ) {
717
+ return;
718
+ }
719
+ const location = formatRange(symbol.range);
720
+ const existing = formatRange(current.range);
721
+ if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
722
+ extendsReport.warnings.push({
723
+ location,
724
+ message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
725
+ });
726
+ return;
727
+ }
728
+ extendsReport.errors.push({
729
+ location,
730
+ message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
731
+ });
732
+ });
615
733
  const exported: Symbols = new Map();
616
734
  const modules = new Map<string, LinkedModule>();
617
735
  const extended: LinkedModule[] = [];
618
736
  const constants = [...parsed.constants];
619
737
  const variables = parsed.variables.map((variable) => variable.name);
620
738
  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
739
  parsed.extends.forEach((name) => {
627
740
  const nested = scope.modules.get(name);
628
741
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
629
742
  if (standardSymbols) {
630
- addSymbols(standardSymbols, true);
743
+ mergeExtended(standardSymbols);
631
744
  return;
632
745
  }
633
746
  const linked = nested ?? getLinkedModule(name, parsed.name);
@@ -641,61 +754,85 @@ export const createEnvironment = (
641
754
  variables.push(variable);
642
755
  variableLocations.push(linked.variableLocations[index]);
643
756
  });
644
- addSymbols(linked.exported, true);
757
+ mergeExtended(linked.exported);
645
758
  linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
646
759
  extended.push(linked);
647
760
  });
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" }),
761
+ const bound = new Map(visible);
762
+ const declarations = [
763
+ ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
764
+ ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
765
+ ];
766
+ const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
767
+ key: declaration.name,
768
+ arity: declaration.arity,
769
+ kind: declaration.kind,
770
+ range: declaration.location.range,
771
+ });
772
+ declarations.forEach((declaration) =>
773
+ exported.set(declaration.name, getDeclarationSymbol(declaration)),
657
774
  );
775
+ const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
776
+ key: getKey(definition),
777
+ arity: getArity(definition),
778
+ params: getParamArities(definition),
779
+ kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
780
+ range: definition.location.range,
781
+ });
658
782
  const isParameterFree = constants.length === 0 && variables.length === 0;
659
783
  const getKey = (definition: Definition) =>
660
784
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
661
- const importedAt = new Map<string, SourceLocation>();
662
- const getUnit = (module: Module) => ({
785
+ const instanced = new Map<Instance, Symbols>();
786
+ const getUnit = (module: Module): ModuleUnit => ({
663
787
  imported: visible,
664
- importedAt,
665
- definitions: new Map(module.definitions.map((definition) => [definition.name, definition])),
788
+ bound,
789
+ // SANY keeps the first of two definitions of a name.
790
+ definitions: new Map(
791
+ module.definitions.toReversed().map((definition) => [definition.name, definition]),
792
+ ),
666
793
  getKey,
794
+ instanced,
795
+ reports,
796
+ warnings: scope.warnings,
667
797
  });
668
798
  const getVisibleModules = () => new Map([...scope.modules, ...modules]);
669
799
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
670
800
  new Map([
671
801
  ...visible,
672
- ...[
673
- ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
674
- ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
675
- ]
802
+ ...declarations
676
803
  .filter((declaration) => isBefore(declaration.location, location))
677
804
  .map((declaration): [string, ModuleSymbol] => [
678
805
  declaration.name,
679
- { key: declaration.name, arity: declaration.arity, declaration: declaration.kind },
806
+ getDeclarationSymbol(declaration),
680
807
  ]),
681
808
  ...parsed.definitions
682
809
  .filter((definition) => isBefore(definition.location, location))
683
810
  .map((definition): [string, ModuleSymbol] => [
684
811
  definition.name,
685
- { key: getKey(definition), arity: getArity(definition) },
812
+ getDefinitionSymbol(definition),
686
813
  ]),
687
814
  ]);
815
+ // Generator.generateModule reports the errors of a nested module and goes on, and then
816
+ // SymbolTable.addModule reports a module already defined.
688
817
  const linkNested = (nested: Module) => {
689
818
  const visibleModules = getVisibleModules();
690
- if (visibleModules.has(nested.name)) throw createModuleRedefinedError(nested);
819
+ const existing = visibleModules.get(nested.name);
820
+ const redefinition = existing && getModuleRedefinition(nested, existing.module);
821
+ if (existing && !redefinition) throw createModuleRedefinedError(nested);
691
822
  scope.nested.push(nested);
823
+ const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
824
+ reports.push(report);
692
825
  const linked = linkModule(nested, {
693
826
  id: `${scope.id}/${nested.name}`,
694
827
  symbols: getSymbolsBefore(nested.location),
695
828
  modules: visibleModules,
696
829
  nested: scope.nested,
830
+ errors: report.errors,
831
+ warnings: report.warnings,
832
+ levelChecks: scope.levelChecks,
697
833
  });
698
- modules.set(nested.name, linked);
834
+ if (redefinition) report.errors.push(redefinition);
835
+ if (!existing) modules.set(nested.name, linked);
699
836
  if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
700
837
  };
701
838
  [
@@ -709,10 +846,12 @@ export const createEnvironment = (
709
846
  isParameterFree,
710
847
  getVisibleModules().get(instance.module),
711
848
  );
712
- symbols.forEach((_, name) => {
713
- if (!visible.has(name)) importedAt.set(name, instance.location);
849
+ instanced.set(instance, symbols);
850
+ symbols.forEach((symbol, name) => {
851
+ if (visible.has(name)) return;
852
+ visible.set(name, symbol);
853
+ if (!instance.isLocal) exported.set(name, symbol);
714
854
  });
715
- addSymbols(symbols, !instance.isLocal);
716
855
  },
717
856
  })),
718
857
  ...parsed.modules.map((nested) => ({
@@ -722,7 +861,6 @@ export const createEnvironment = (
722
861
  ]
723
862
  .sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
724
863
  .forEach((unit) => unit.link());
725
- checkRedefinitions(parsed, getUnit(parsed));
726
864
  const isInstancedAfterDefinition = (nested: Module) => {
727
865
  const first = parsed.instances.find((instance) => instance.module === nested.name);
728
866
  return first !== undefined && isBefore(nested.location, first.location);
@@ -738,20 +876,15 @@ export const createEnvironment = (
738
876
  ),
739
877
  getAvailableDefinitions(visible),
740
878
  );
741
- resolveModule(module, getUnit(module));
879
+ scope.errors.push(...resolveModule(module, getUnit(module)));
742
880
  module.definitions.forEach((definition) => {
743
881
  const key = getKey(definition);
744
882
  definitions.set(key, definition);
745
883
  parameterized.set(key, !isParameterFree);
746
- const symbol = { key, arity: getArity(definition) };
884
+ const symbol = getDefinitionSymbol(definition);
747
885
  visible.set(definition.name, symbol);
748
886
  if (!definition.isLocal) exported.set(definition.name, symbol);
749
887
  });
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
888
  const linked = {
756
889
  module,
757
890
  constants,
@@ -762,11 +895,31 @@ export const createEnvironment = (
762
895
  modules,
763
896
  extended,
764
897
  };
898
+ if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
899
+ // SANY checks the instances of a module with its definitions and theorems, so it reports their
900
+ // errors together.
901
+ const checks = instanceChecks
902
+ .splice(firstCheck)
903
+ .map((check) => ({ location: check.location, check: () => checkInstance(check) }));
904
+ scope.levelChecks.push(() =>
905
+ checkModuleLevels(
906
+ module,
907
+ {
908
+ constants: new Set(constants.map((constant) => constant.name)),
909
+ variables: new Set(variables),
910
+ getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
911
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
912
+ },
913
+ checks,
914
+ warnings,
915
+ ),
916
+ );
765
917
  linkedList.push(linked);
766
918
  return linked;
767
919
  };
768
920
 
769
921
  const linkedRoot = linkTopLevel(root).linked;
922
+ if (warnings.length > 0) options.print(formatSanyWarnings(warnings));
770
923
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
771
924
  linkedRoot.visible.forEach((symbol, name) => {
772
925
  const definition = definitions.get(symbol.key);
@@ -956,20 +1109,6 @@ export const createEnvironment = (
956
1109
  constantLevels: new Map(),
957
1110
  evaluators: new WeakMap(),
958
1111
  };
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
1112
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
974
1113
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
975
1114
  // 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;