tla-ts 0.0.3 → 0.0.4-dev.66582d4

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.
Files changed (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +160 -8
  6. package/dist/index.d.ts +160 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +77 -37
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +796 -246
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +200 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +219 -107
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +731 -184
package/src/levels.ts CHANGED
@@ -1,15 +1,23 @@
1
- import type {
2
- AssumeProve,
3
- Definition,
4
- Expression,
5
- Module,
6
- OperatorParam,
7
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type AssumeProve,
4
+ type Definition,
5
+ type Expression,
6
+ type Module,
7
+ type OperatorParam,
8
+ type Proof,
9
+ type ProofStep,
10
+ type SourceLocation,
8
11
  } from "./ast";
9
- import { TlaSyntaxError } from "./errors";
10
- import { getChildren } from "./evaluate";
12
+ import {
13
+ createSanyError,
14
+ createSemanticError,
15
+ formatRange,
16
+ TlaSyntaxError,
17
+ type SanyError,
18
+ } from "./errors";
11
19
  import { mapChildren } from "./instances";
12
- import { getBoundNames, getPatternNames } from "./resolve";
20
+ import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
13
21
  import { STANDARD_LEVELS } from "./standard-levels";
14
22
 
15
23
  interface LevelData {
@@ -34,6 +42,7 @@ interface LevelContext {
34
42
  constants: Set<string>;
35
43
  variables: Set<string>;
36
44
  getDefinition: (name: string) => Definition | undefined;
45
+ getQualifiedName?: (name: string) => string | undefined;
37
46
  }
38
47
 
39
48
  type ScopeEntry =
@@ -172,12 +181,19 @@ const bindFormals = (scope: Scope, formals: OperatorParam[]) =>
172
181
 
173
182
  const toFormals = (names: string[]) => names.map((name) => ({ name, arity: 0 }));
174
183
 
184
+ // The levels of tla2sany.semantic.LevelConstants.
185
+ export const CONSTANT_LEVEL = 0;
186
+ export const VARIABLE_LEVEL = 1;
175
187
  export const ACTION_LEVEL = 2;
176
-
177
188
  export const TEMPORAL_LEVEL = 3;
178
189
 
179
- const createLevelError = (message: string, location: SourceLocation) =>
180
- new TlaSyntaxError(`${message} at line ${location.line}, column ${location.column}`);
190
+ // SANY reports a level error at the range of the expression it rejects.
191
+ const createLevelError = (message: string, expression: Expression) =>
192
+ expression.location.range
193
+ ? createSemanticError(`${message}.`, expression.location.range)
194
+ : new TlaSyntaxError(
195
+ `${message} at line ${expression.location.line}, column ${expression.location.column}`,
196
+ );
181
197
 
182
198
  const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
183
199
 
@@ -201,7 +217,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
201
217
  if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
202
218
  const definition = context.getDefinition(name);
203
219
  if (definition) {
204
- return { kind: "Defined", levels: analyzeDefinition(definition, new Map()) };
220
+ const levels = analyzeDefinition(definition, new Map());
221
+ // SANY names a definition of an instance by its qualified name, as in I!Foo.
222
+ const qualifiedName = context.getQualifiedName?.(name);
223
+ return {
224
+ kind: "Defined",
225
+ levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
226
+ };
205
227
  }
206
228
  const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
207
229
  if (signature) return { kind: "Defined", levels: signature };
@@ -237,13 +259,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
237
259
  if (operand.operator || operand.declared) return;
238
260
  if (operand.data.level > getMaxLevel(operator, index)) {
239
261
  throw new TlaSyntaxError(
240
- `Level error in applying operator ${operator.name}: The level of argument ${index + 1} exceeds the maximum level allowed by the operator.`,
262
+ `Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
241
263
  );
242
264
  }
243
265
  });
244
266
  if (ranges.some((range) => range.level > ACTION_LEVEL)) {
245
267
  throw new TlaSyntaxError(
246
- `Level error in applying operator ${operator.name}: The level of the range for a bounded variable exceeds the maximum level allowed by the operator.`,
268
+ `Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
247
269
  );
248
270
  }
249
271
  const getOperand = (formal: string) => {
@@ -444,10 +466,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
444
466
  ? applyDeclared(resolved.name, resolved.level, operands)
445
467
  : applyDefined(resolved.levels, operands);
446
468
 
447
- const checkJunction = (operands: Operand[], location: SourceLocation) => {
469
+ const checkJunction = (operands: Operand[], expression: Expression) => {
448
470
  const levels = operands.map((operand) => operand.data.level);
449
471
  if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
450
- throw createLevelError("Formula has both temporal formula and action as arguments", location);
472
+ throw createLevelError(
473
+ "Formula has both temporal formula and action as arguments",
474
+ expression,
475
+ );
451
476
  }
452
477
  };
453
478
 
@@ -456,10 +481,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
456
481
  if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
457
482
  throw createLevelError(
458
483
  "Action used where only temporal formula or state predicate allowed",
459
- expression.location,
484
+ expression,
460
485
  );
461
486
  }
462
- if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression.location);
487
+ if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
463
488
  };
464
489
 
465
490
  const checkTemporal = (
@@ -473,7 +498,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
473
498
  expression.kind === "Always"
474
499
  ? "[] followed by action not of form [A]_v"
475
500
  : "<> followed by action not of form <<A>>_v",
476
- expression.location,
501
+ expression,
477
502
  );
478
503
  };
479
504
 
@@ -501,7 +526,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
501
526
  return data;
502
527
  };
503
528
 
529
+ // SANY reports a level error of an operator's arguments at the application.
504
530
  const analyze = (expression: Expression, scope: Scope): LevelData => {
531
+ try {
532
+ return analyzeNode(expression, scope);
533
+ } catch (error) {
534
+ if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
535
+ throw createLevelError(error.message, expression);
536
+ }
537
+ };
538
+
539
+ const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
505
540
  switch (expression.kind) {
506
541
  case "Number":
507
542
  case "Decimal":
@@ -521,7 +556,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
521
556
  case "And":
522
557
  case "Or": {
523
558
  const operands = toOperands(expression.operands, scope);
524
- checkJunction(operands, expression.location);
559
+ checkJunction(operands, expression);
525
560
  return applyDefined(JUNCTION_LEVELS, operands);
526
561
  }
527
562
  case "Prime":
@@ -561,13 +596,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
561
596
  expression.bounds.map((bound) => bound.set),
562
597
  getBoundNames(expression.bounds),
563
598
  );
564
- if (
565
- data.level === TEMPORAL_LEVEL &&
566
- expression.bounds.some((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
567
- ) {
599
+ const actionBound =
600
+ data.level === TEMPORAL_LEVEL
601
+ ? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
602
+ : undefined;
603
+ if (actionBound) {
568
604
  throw createLevelError(
569
605
  "Action-level bound of quantified temporal formula",
570
- expression.location,
606
+ actionBound.set,
571
607
  );
572
608
  }
573
609
  return data;
@@ -656,6 +692,9 @@ export const createLevelAnalyzer = (context: LevelContext) => {
656
692
  ? toOperand(expression.args[0], scope)
657
693
  : { data: applyBuiltin(SET_LEVELS, expression.args, scope) },
658
694
  ]);
695
+ // SANY's semantic checker rejects the expression without computing its level.
696
+ case "Bracket":
697
+ return createData();
659
698
  case "FieldAccess":
660
699
  return applyDefined(SELECT_LEVELS, [
661
700
  toOperand(expression.record, scope),
@@ -817,34 +856,141 @@ export const createLevelAnalyzer = (context: LevelContext) => {
817
856
  return { analyze, analyzeDefinition, toOperand };
818
857
  };
819
858
 
820
- const analyzeAssumeProve = (
859
+ export const analyzeAssumeProve = (
821
860
  assumeProve: AssumeProve,
822
861
  scope: Scope,
823
862
  analyzer: ReturnType<typeof createLevelAnalyzer>,
824
- ) => {
863
+ ): LevelData => {
825
864
  const inner = new Map(scope);
826
- assumeProve.assumptions.forEach((assumption) => {
865
+ const parts = assumeProve.assumptions.flatMap((assumption) => {
827
866
  if (assumption.kind === "NewSymbol") {
828
- if (assumption.set) analyzer.analyze(assumption.set, inner);
867
+ const set = assumption.set && analyzer.analyze(assumption.set, inner);
829
868
  inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
830
- return;
869
+ return set ? [set] : [];
831
870
  }
832
- if (assumption.kind === "AssumeProve") analyzeAssumeProve(assumption, inner, analyzer);
833
- else analyzer.analyze(assumption, inner);
871
+ return [
872
+ assumption.kind === "AssumeProve"
873
+ ? analyzeAssumeProve(assumption, inner, analyzer)
874
+ : analyzer.analyze(assumption, inner),
875
+ ];
834
876
  });
835
- analyzer.analyze(assumeProve.conclusion, inner);
877
+ parts.push(analyzer.analyze(assumeProve.conclusion, inner));
878
+ const data = createData(Math.max(...parts.map((part) => part.level)));
879
+ parts.forEach((part) => {
880
+ part.params.forEach((param) => data.params.add(param));
881
+ part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
882
+ part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
883
+ part.argParams.forEach((key) => data.argParams.add(key));
884
+ });
885
+ return data;
836
886
  };
837
887
 
838
- // SANY checks the level of every definition, assumption, and theorem of a module.
839
- export const checkModuleLevels = (module: Module, context: LevelContext) => {
888
+ // SANY checks the level of every definition, assumption, theorem, and instance of a module.
889
+ export const checkModuleLevels = (
890
+ module: Module,
891
+ context: LevelContext,
892
+ instances: { location: SourceLocation; check: () => void }[],
893
+ warnings: SanyError[],
894
+ ) => {
840
895
  const analyzer = createLevelAnalyzer(context);
841
- module.definitions
842
- .filter((definition) => !definition.name.includes("!"))
843
- .forEach((definition) => analyzer.analyzeDefinition(definition, new Map()));
844
- module.assumptions.forEach((assumption) => analyzer.analyze(assumption.expression, new Map()));
845
- module.theorems.forEach((theorem) =>
846
- theorem.statement.kind === "AssumeProve"
847
- ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
848
- : analyzer.analyze(theorem.statement, new Map()),
896
+ const getLevel = (expressions: Expression[]) =>
897
+ Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
898
+ const getStepLevel = (step: ProofStep) => {
899
+ const body = step.body;
900
+ if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
901
+ if (body.kind === "Witness") return getLevel(body.expressions);
902
+ if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
903
+ if (body.kind === "Pick") {
904
+ return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
905
+ }
906
+ if (body.kind !== "Assert") return 0;
907
+ return body.statement.kind === "AssumeProve"
908
+ ? analyzeAssumeProve(body.statement, new Map(), analyzer).level
909
+ : getLevel([body.statement]);
910
+ };
911
+ // SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
912
+ const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
913
+ proof?.kind !== "Steps"
914
+ ? []
915
+ : proof.steps.flatMap((step) => {
916
+ const location = formatRange(step.statement);
917
+ const isConstant = () => getStepLevel(step) === 0;
918
+ if (
919
+ step.body.kind === "Have" ||
920
+ step.body.kind === "Take" ||
921
+ step.body.kind === "Witness"
922
+ ) {
923
+ return isConstant()
924
+ ? []
925
+ : [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
926
+ }
927
+ if (step.body.kind === "Case") {
928
+ return [
929
+ ...(isConstant()
930
+ ? []
931
+ : [{ location, message: "Non-constant CASE for temporal goal." }]),
932
+ ...checkTemporalGoal(step.proof),
933
+ ];
934
+ }
935
+ return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
936
+ });
937
+ // SANY checks each step as a theorem of its own after the steps of its proof.
938
+ const checkProof = (proof: Proof | undefined): SanyError[] =>
939
+ proof?.kind !== "Steps"
940
+ ? []
941
+ : proof.steps.flatMap((step) => {
942
+ const body = step.body;
943
+ const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
944
+ const pickErrors =
945
+ body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
946
+ ? body.bounds
947
+ .filter((bound) => getLevel([bound.set]) !== 0)
948
+ .flatMap((bound) =>
949
+ bound.set.location.range
950
+ ? [
951
+ {
952
+ location: formatRange(bound.set.location.range),
953
+ message: "Non-constant bound of temporal PICK.",
954
+ },
955
+ ]
956
+ : [],
957
+ )
958
+ : [];
959
+ return [
960
+ ...checkProof(step.proof),
961
+ ...pickErrors,
962
+ ...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
963
+ ];
964
+ });
965
+ const errors = collectSemanticErrors(
966
+ [
967
+ ...module.definitions
968
+ .filter((definition) => !definition.name.includes("!"))
969
+ .map((definition) => ({
970
+ location: definition.location,
971
+ check: () => analyzer.analyzeDefinition(definition, new Map()),
972
+ })),
973
+ ...module.assumptions.map((assumption) => ({
974
+ location: assumption.location,
975
+ check: () => analyzer.analyze(assumption.expression, new Map()),
976
+ })),
977
+ ...module.theorems.map((theorem) => ({
978
+ location: theorem.location,
979
+ check: () => {
980
+ const data =
981
+ theorem.statement.kind === "AssumeProve"
982
+ ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
983
+ : analyzer.analyze(theorem.statement, new Map());
984
+ const errors = [
985
+ ...checkProof(theorem.proof),
986
+ ...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
987
+ ];
988
+ if (errors.length > 0) throw createSanyError(errors);
989
+ },
990
+ })),
991
+ ...instances,
992
+ ],
993
+ warnings,
849
994
  );
995
+ if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
850
996
  };
package/src/lexer.ts CHANGED
@@ -12,9 +12,11 @@ type TokenKind =
12
12
  | "ModuleEnd"
13
13
  | "End";
14
14
 
15
+ // spelling holds the text of an operator written as a synonym, such as (+) for \oplus.
15
16
  export interface Token {
16
17
  kind: TokenKind;
17
18
  text: string;
19
+ spelling?: string;
18
20
  line: number;
19
21
  column: number;
20
22
  endColumn: number;
@@ -232,38 +234,84 @@ const SYMBOL_OPERATORS = [
232
234
  "?",
233
235
  ];
234
236
 
237
+ // The Unicode forms of the operators SANY's grammar, javacc/tla+.jj, reads, as their ASCII forms.
235
238
  const UNICODE_OPERATORS = new Map([
239
+ ["∃", "\\E"],
240
+ ["∀", "\\A"],
241
+ ["∷", "::"],
242
+ ["≜", "=="],
243
+ ["⟨", "<<"],
244
+ ["⟩_", ">>_"],
245
+ ["⟩", ">>"],
246
+ ["→", "->"],
247
+ ["←", "<-"],
248
+ ["↦", "|->"],
249
+ ["⁺", "^+"],
250
+ ["¬", "~"],
251
+ ["□", "[]"],
252
+ ["◇", "<>"],
236
253
  ["∧", "/\\"],
237
254
  ["∨", "\\/"],
238
- ["¬", "~"],
239
- ["⇒", "=>"],
240
- ["⟹", "=>"],
255
+ ["∈", "\\in"],
256
+ ["≈", "\\approx"],
257
+ ["≔", ":="],
258
+ ["≍", "\\asymp"],
259
+ ["◯", "\\bigcirc"],
260
+ ["⩴", "::="],
261
+ ["●", "\\bullet"],
262
+ ["∩", "\\cap"],
263
+ ["⋅", "\\cdot"],
264
+ ["∘", "\\o"],
265
+ ["≅", "\\cong"],
266
+ ["∪", "\\cup"],
267
+ ["÷", "\\div"],
268
+ ["≐", "\\doteq"],
269
+ ["‥", ".."],
270
+ ["…", "..."],
241
271
  ["≡", "<=>"],
272
+ ["‼", "!!"],
273
+ ["≥", ">="],
274
+ ["≫", "\\gg"],
242
275
  ["⇔", "<=>"],
243
- ["∈", "\\in"],
276
+ ["⇒", "=>"],
277
+ ["⫤", "=|"],
278
+ ["↝", "~>"],
279
+ ["≤", "<="],
280
+ ["≪", "\\ll"],
281
+ ["⊣", "-|"],
282
+ ["≠", "#"],
244
283
  ["∉", "\\notin"],
245
- ["⊆", "\\subseteq"],
284
+ ["⊙", "\\odot"],
285
+ ["⊖", "\\ominus"],
286
+ ["⊕", "\\oplus"],
287
+ ["⊘", "\\oslash"],
288
+ ["⊗", "\\otimes"],
289
+ ["⇸", "-+->"],
290
+ ["≺", "\\prec"],
291
+ ["⪯", "\\preceq"],
292
+ ["∝", "\\propto"],
293
+ ["⁇", "??"],
294
+ ["⊨", "|="],
295
+ ["⊢", "|-"],
296
+ ["∼", "\\sim"],
297
+ ["≃", "\\simeq"],
298
+ ["⊓", "\\sqcap"],
299
+ ["⊔", "\\sqcup"],
300
+ ["⊏", "\\sqsubset"],
301
+ ["⊑", "\\sqsubseteq"],
302
+ ["⊐", "\\sqsupset"],
303
+ ["⊒", "\\sqsupseteq"],
304
+ ["⋆", "\\star"],
246
305
  ["⊂", "\\subset"],
247
- ["⊇", "\\supseteq"],
306
+ ["⊆", "\\subseteq"],
307
+ ["≻", "\\succ"],
308
+ ["⪰", "\\succeq"],
248
309
  ["⊃", "\\supset"],
249
- ["∪", "\\cup"],
250
- ["∩", "\\cap"],
310
+ ["⊇", "\\supseteq"],
251
311
  ["×", "\\X"],
252
- ["∀", "\\A"],
253
- ["∃", "\\E"],
254
- ["≠", "#"],
255
- ["≤", "<="],
256
- ["≥", ">="],
257
- ["↦", "|->"],
258
- ["→", "->"],
259
- ["⟨", "<<"],
260
- ["⟩", ">>"],
261
- ["□", "[]"],
262
- ["◇", "<>"],
263
- ["≜", "=="],
264
- ["′", "'"],
265
- ["∘", "\\o"],
266
- ["⋅", "\\cdot"],
312
+ ["⊎", "\\uplus"],
313
+ ["‖", "||"],
314
+ ["≀", "\\wr"],
267
315
  ]);
268
316
 
269
317
  const BASED_NUMBER = /^\\([bB][01]+|[oO][0-7]+|[hH][0-9a-fA-F]+)/;
@@ -301,13 +349,27 @@ export const matchOperator = (source: string, index: number): OperatorMatch | un
301
349
  const operator = BACKSLASH_OPERATORS.get(word.slice(0, length.value));
302
350
  return { text: operator ?? "\\", length: operator === undefined ? 1 : length.value + 1 };
303
351
  }
304
- const unicodeOperator = UNICODE_OPERATORS.get(character);
305
- if (unicodeOperator) return { text: unicodeOperator, length: character.length };
352
+ const unicodeSymbol = [source.slice(index, index + 2), character].find((candidate) =>
353
+ UNICODE_OPERATORS.has(candidate),
354
+ );
355
+ if (unicodeSymbol !== undefined) {
356
+ return { text: UNICODE_OPERATORS.get(unicodeSymbol) ?? "", length: unicodeSymbol.length };
357
+ }
306
358
  const symbol = SYMBOL_OPERATORS.find((candidate) => source.startsWith(candidate, index));
307
359
  if (symbol) return { text: SYMBOL_ALIASES.get(symbol) ?? symbol, length: symbol.length };
308
360
  return character === "\\" ? { text: "\\", length: 1 } : undefined;
309
361
  };
310
362
 
363
+ // TLC's configuration parser reads its tokens with SANY's lexer, so it reads a Unicode operator, or
364
+ // ℕ, ℤ, or ℝ, as one token, which it compares as written with the words it expects.
365
+ const UNICODE_NAMES = new Set(["ℕ", "ℤ", "ℝ"]);
366
+ export const matchUnicodeToken = (source: string, index: number): OperatorMatch | undefined => {
367
+ const token = [source.slice(index, index + 2), source[index]].find(
368
+ (candidate) => UNICODE_OPERATORS.has(candidate) || UNICODE_NAMES.has(candidate),
369
+ );
370
+ return token === undefined ? undefined : { text: token, length: token.length };
371
+ };
372
+
311
373
  const isDigit = (character: string | undefined) =>
312
374
  character !== undefined && character >= "0" && character <= "9";
313
375
 
@@ -335,6 +397,10 @@ const matchStep = (source: string, index: number) => {
335
397
  );
336
398
  };
337
399
 
400
+ // SANY's lexer reads @ as an identifier, which stands for the value an EXCEPT changes only alone.
401
+ export const readAtAsIdentifier = (token: Token): Token =>
402
+ token.kind === "Operator" && token.text === "@" ? { ...token, kind: "Identifier" } : token;
403
+
338
404
  const TAB_SIZE = 8;
339
405
 
340
406
  const getWidthAfter = (width: number, character: string | undefined) =>
@@ -345,6 +411,8 @@ export const tokenize = (source: string): Token[] => {
345
411
  if (!headerMatch)
346
412
  throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
347
413
  const tokens: Token[] = [];
414
+ // Each name is one string object, so looking a name up in a scope compares pointers.
415
+ const names = new Map<string, string>();
348
416
  const prefixLines = source.slice(0, headerMatch.index).split("\n");
349
417
  const cursor = {
350
418
  index: headerMatch.index,
@@ -365,9 +433,16 @@ export const tokenize = (source: string): Token[] => {
365
433
  }
366
434
  return getWidthAfter(scan.width, source[cursor.index]);
367
435
  };
368
- const pushToken = (kind: TokenKind, text: string, length: number) => {
436
+ const pushToken = (kind: TokenKind, text: string, length: number, spelling?: string) => {
369
437
  const column = getColumn();
370
- tokens.push({ kind, text, line: cursor.line, column, endColumn: column + length - 1 });
438
+ tokens.push({
439
+ kind,
440
+ text,
441
+ spelling,
442
+ line: cursor.line,
443
+ column,
444
+ endColumn: column + length - 1,
445
+ });
371
446
  cursor.index += length;
372
447
  };
373
448
  const advanceOver = (length: number) => {
@@ -440,11 +515,14 @@ export const tokenize = (source: string): Token[] => {
440
515
  const characters: string[] = [];
441
516
  advanceOver(1);
442
517
  while (source[cursor.index] !== '"') {
443
- if (cursor.index >= source.length || source[cursor.index] === "\n") {
518
+ if (cursor.index >= source.length || "\n\r".includes(source[cursor.index])) {
444
519
  throw createError("Unterminated string");
445
520
  }
446
521
  if (source[cursor.index] === "\\") {
447
522
  const escaped = source[cursor.index + 1];
523
+ if (escaped === undefined || !'ntrf\\"'.includes(escaped)) {
524
+ throw createError("Invalid escape in string");
525
+ }
448
526
  characters.push({ n: "\n", t: "\t", r: "\r", f: "\f" }[escaped] ?? escaped);
449
527
  advanceOver(2);
450
528
  continue;
@@ -477,11 +555,24 @@ export const tokenize = (source: string): Token[] => {
477
555
  pushToken(number.includes(".") ? "Decimal" : "Number", number, number.length);
478
556
  continue;
479
557
  }
558
+ // A word needs a letter to be a name, so SANY reads one without as underscores and numbers.
559
+ const part = /^(_|\d+)/.exec(word)?.[0];
560
+ if (part !== undefined && !/[A-Za-z]/.test(word)) {
561
+ pushToken(part === "_" ? "Operator" : "Number", part, part.length);
562
+ continue;
563
+ }
480
564
  if (word.startsWith("WF_") || word.startsWith("SF_")) {
481
565
  pushToken("Keyword", word.slice(0, 3), 3);
482
566
  continue;
483
567
  }
484
- pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", word, word.length);
568
+ const name = names.get(word) ?? word;
569
+ names.set(name, name);
570
+ pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
571
+ continue;
572
+ }
573
+ if ("ℕℤℝ".includes(character)) {
574
+ pushToken("Identifier", names.get(character) ?? character, 1);
575
+ names.set(character, character);
485
576
  continue;
486
577
  }
487
578
  const stepLength = matchStep(source, cursor.index);
@@ -491,7 +582,13 @@ export const tokenize = (source: string): Token[] => {
491
582
  }
492
583
  const operator = matchOperator(source, cursor.index);
493
584
  if (operator) {
494
- pushToken("Operator", operator.text, operator.length);
585
+ const spelling = source.slice(cursor.index, cursor.index + operator.length);
586
+ pushToken(
587
+ "Operator",
588
+ operator.text,
589
+ operator.length,
590
+ spelling === operator.text ? undefined : spelling,
591
+ );
495
592
  continue;
496
593
  }
497
594
  throw createError(`Unexpected character ${JSON.stringify(character)}`);