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

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 (79) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +150 -48
  5. package/dist/index.d.ts +150 -48
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -22
  12. package/dist/run-cli.js +1352 -23
  13. package/dist/transfer.cjs +83 -43
  14. package/dist/transfer.js +83 -43
  15. package/package.json +5 -1
  16. package/src/ast.ts +28 -4
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -17
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1317 -428
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +226 -207
  25. package/src/environment.ts +221 -75
  26. package/src/errors.ts +130 -15
  27. package/src/evaluate.ts +713 -259
  28. package/src/exit-codes.ts +6 -24
  29. package/src/fingerprints.ts +1 -2
  30. package/src/index.ts +1 -0
  31. package/src/instances.ts +3 -1
  32. package/src/java-overrides.ts +104 -25
  33. package/src/levels.ts +6 -1
  34. package/src/lexer.ts +122 -29
  35. package/src/live-check.ts +204 -148
  36. package/src/liveness.ts +191 -122
  37. package/src/obligation-expression.ts +2 -2
  38. package/src/obligation-source.ts +2 -0
  39. package/src/parallel-worker.ts +6 -1
  40. package/src/parallel.ts +57 -6
  41. package/src/parser.ts +327 -147
  42. package/src/pcal-ast.ts +770 -0
  43. package/src/pcal-parse.ts +1523 -0
  44. package/src/pcal-tokenize.ts +516 -0
  45. package/src/pcal-translate.ts +1230 -0
  46. package/src/pcal-validate.ts +454 -0
  47. package/src/randomization.ts +24 -7
  48. package/src/resolve.ts +252 -87
  49. package/src/run-cli.ts +1162 -200
  50. package/src/runtime.ts +16 -0
  51. package/src/sany-builtins.ts +147 -0
  52. package/src/sany-generator.ts +3128 -0
  53. package/src/sany-levels.ts +1208 -0
  54. package/src/sany-lexer.ts +410 -480
  55. package/src/sany-linter.ts +122 -0
  56. package/src/sany-modules.ts +1398 -0
  57. package/src/sany-nodes.ts +463 -0
  58. package/src/sany-parse-error.ts +2033 -3015
  59. package/src/sany-report.ts +356 -0
  60. package/src/sany-selectors.ts +738 -0
  61. package/src/sany-source.ts +248 -0
  62. package/src/sany-xml.ts +1017 -0
  63. package/src/spec-description.ts +25 -5
  64. package/src/standard-modules.ts +172 -129
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +121 -26
  67. package/src/successors.ts +141 -173
  68. package/src/symmetry.ts +168 -50
  69. package/src/tableau.ts +3 -1
  70. package/src/tlc-ext.ts +13 -17
  71. package/src/tlc-messages.ts +329 -0
  72. package/src/tlc-module.ts +49 -21
  73. package/src/tlc-modules.ts +0 -4
  74. package/src/tlc-order.ts +55 -9
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/values.ts +664 -317
package/src/parser.ts CHANGED
@@ -23,14 +23,29 @@ import type {
23
23
  UseOrHide,
24
24
  } from "./ast";
25
25
  import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
26
- import { getNumeralValue, tokenize, type Token } from "./lexer";
26
+ import { getNumeralValue, readAtAsIdentifier, tokenize, type Token } from "./lexer";
27
+ import { getSanyParseError } from "./sany-parse-error";
27
28
 
28
- interface InfixOperator {
29
+ interface Precedence {
29
30
  low: number;
30
31
  high: number;
32
+ }
33
+
34
+ interface InfixOperator extends Precedence {
31
35
  isAssociative: boolean;
32
36
  }
33
37
 
38
+ // TLAplusParser.lastOp, the operator parsed last, and the operator an operand being parsed follows
39
+ // in its expression.
40
+ interface OperatorContext {
41
+ last?: Precedence;
42
+ preceding?: Precedence;
43
+ }
44
+
45
+ // SANY's precedences of a postfix operator and of the "[" of a function application.
46
+ const POSTFIX_PRECEDENCE: Precedence = { low: 15, high: 15 };
47
+ const FUNCTION_PRECEDENCE: Precedence = { low: 16, high: 16 };
48
+
34
49
  const createInfix = (low: number, high: number, isAssociative: boolean) => ({
35
50
  low,
36
51
  high,
@@ -141,6 +156,19 @@ const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
141
156
 
142
157
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
143
158
 
159
+ // The prefix operators SANY reads in a definition's left-hand side, built-in ones included.
160
+ const DEFINABLE_PREFIX_OPERATORS = new Set([
161
+ "-.",
162
+ "~",
163
+ "[]",
164
+ "<>",
165
+ "ENABLED",
166
+ "UNCHANGED",
167
+ "SUBSET",
168
+ "UNION",
169
+ "DOMAIN",
170
+ ]);
171
+
144
172
  const EXPRESSION_PREFIXES = new Set([
145
173
  "-",
146
174
  "~",
@@ -169,6 +197,16 @@ const OPERATOR_ARGUMENTS = new Set([
169
197
  ...POSTFIX_OPERATORS,
170
198
  ]);
171
199
 
200
+ // A SANY InfixExpr or PostfixExpr.
201
+ const isOperatorExpression = (expression: Expression) =>
202
+ expression.kind === "Prime" ||
203
+ expression.kind === "Product" ||
204
+ ((expression.kind === "And" || expression.kind === "Or") && !expression.isBulleted) ||
205
+ (expression.kind === "Apply" &&
206
+ (expression.args.length === 2
207
+ ? INFIX_OPERATORS.has(expression.operator)
208
+ : POSTFIX_OPERATORS.has(expression.operator)));
209
+
172
210
  const isOperatorName = (text: string) =>
173
211
  INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
174
212
 
@@ -262,6 +300,7 @@ const FACT_KEYWORDS = new Set([
262
300
  "IF",
263
301
  "LAMBDA",
264
302
  "LET",
303
+ "MODULE",
265
304
  "SF_",
266
305
  "SUBSET",
267
306
  "UNCHANGED",
@@ -291,30 +330,32 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
291
330
 
292
331
  const NO_OFFSIDE = -1;
293
332
 
294
- const getModuleHeader = (name: string) => new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
295
-
296
- // The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
297
- // modules that follow the root module.
333
+ // MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
334
+ // root module's file, from its ---- MODULE line to the next ==== line.
298
335
  export const extractModule = (source: string, name: string) => {
336
+ const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
299
337
  const lines = source.split(/\r?\n/);
300
- const start = lines.findIndex((line) => getModuleHeader(name).test(line));
338
+ const start = lines.findIndex((line) => header.test(line));
301
339
  if (start === -1) return undefined;
302
340
  const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
303
- return lines.slice(start, end === -1 ? undefined : end + 1).join("\n");
341
+ return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
304
342
  };
305
343
 
306
- // isTlapm parses as tlapm does for proof obligations, which accepts and rejects some modules that
307
- // SANY does not.
308
- const parse = (source: string, isTlapm: boolean): Module => {
309
- const tokens = tokenize(source);
344
+ // The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some modules
345
+ // that SANY does not. The sany mode leaves the checks that SANY makes after parsing to the SANY
346
+ // generator.
347
+ type ParseMode = "tlc" | "tlapm" | "sany";
348
+
349
+ const parse = (source: string, mode: ParseMode): Module => {
350
+ const isTlapm = mode === "tlapm";
351
+ const tokens = isTlapm ? tokenize(source) : tokenize(source).map(readAtAsIdentifier);
310
352
  const cursor = { index: 0, offsideColumns: [] as number[] };
353
+ const operators: OperatorContext = {};
311
354
  // A RECURSIVE declaration covers the definitions of its own LET or module.
312
355
  const recursiveScopes: Set<string>[] = [];
313
356
  const factNames = new Set<string>();
314
357
  // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
315
358
  const proofScopes: Set<string>[] = [];
316
- // A fact may cite an operator of a parameterized instance without the instance's arguments.
317
- const factStart = { index: -1 };
318
359
  // SANY reports a location in a nested module as one in the module of its file.
319
360
  const openModules: Module[] = [];
320
361
 
@@ -435,8 +476,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
435
476
  const createUnexpected = (message: string, token: Token = peekRaw()) =>
436
477
  createError(message, token, token);
437
478
 
479
+ // TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
438
480
  const isFieldName = (token: Token) =>
439
- (token.kind === "Identifier" || token.kind === "Keyword") && !isTlapmKeyword(token);
481
+ (token.kind === "Identifier" ||
482
+ token.kind === "Keyword" ||
483
+ (token.kind === "Operator" && token.text === "_")) &&
484
+ !isTlapmKeyword(token);
440
485
 
441
486
  const isAt = (text: string, offset = 0) => {
442
487
  const token = peek(offset);
@@ -482,7 +527,32 @@ const parse = (source: string, isTlapm: boolean): Module => {
482
527
  ranges: names.map((item) => item.range),
483
528
  });
484
529
 
485
- const withoutOffside = <Result>(parse: () => Result) => {
530
+ // TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
531
+ const expectSubstitutionTarget = () => {
532
+ const token = peek();
533
+ const isOperator =
534
+ token.kind !== "String" &&
535
+ (DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
536
+ INFIX_OPERATORS.has(token.text) ||
537
+ POSTFIX_OPERATORS.has(token.text) ||
538
+ token.text === "'");
539
+ if (!isOperator) return expectIdentifier();
540
+ cursor.index++;
541
+ return token.text;
542
+ };
543
+
544
+ // SANY takes a keyword for the name of a module, as it does for a field.
545
+ const keywordModuleName = { isUsed: false };
546
+ const expectModuleName = () => {
547
+ if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
548
+ keywordModuleName.isUsed = true;
549
+ return advance().text;
550
+ };
551
+
552
+ // tlapm reads a bracketed expression apart from the junction list around it, whose alignment
553
+ // SANY keeps applying.
554
+ const withBracketLayout = <Result>(parse: () => Result) => {
555
+ if (!isTlapm) return parse();
486
556
  cursor.offsideColumns.push(NO_OFFSIDE);
487
557
  const result = parse();
488
558
  cursor.offsideColumns.pop();
@@ -573,8 +643,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
573
643
  );
574
644
  };
575
645
 
646
+ // SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
647
+ // the start of an expression, such as -.(1), otherwise.
576
648
  const isStandalone = (operator: Token) => {
577
- if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
578
649
  const token = peek(1);
579
650
  return (
580
651
  token.kind === "End" ||
@@ -613,7 +684,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
613
684
  if (isOperatorAlone) {
614
685
  cursor.index++;
615
686
  return withRange(
616
- { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
687
+ {
688
+ kind: "OperatorReference",
689
+ operator: token.text,
690
+ spelling: token.spelling,
691
+ location: getLocation(token),
692
+ },
617
693
  cursor.index - 1,
618
694
  );
619
695
  }
@@ -623,15 +699,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
623
699
  cursor.index++;
624
700
  const location = getLocation(token);
625
701
  if (POSTFIX_OPERATORS.has(token.text)) {
626
- return { kind: "OperatorReference", operator: token.text, location };
702
+ return withRange(
703
+ { kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
704
+ cursor.index - 1,
705
+ );
627
706
  }
628
707
  const argument: Expression = { kind: "Identifier", name: "argument", location };
629
- return {
630
- kind: "Lambda",
631
- params: [argument.name],
632
- body: getOperatorApplication(token.text, argument, location),
633
- location,
634
- };
708
+ return withRange(
709
+ {
710
+ kind: "Lambda",
711
+ params: [argument.name],
712
+ body: getOperatorApplication(token.text, argument, location),
713
+ operator: token.text,
714
+ location: getLocation(token),
715
+ },
716
+ cursor.index - 1,
717
+ );
635
718
  }
636
719
  if (isAt("LAMBDA")) {
637
720
  const start = cursor.index;
@@ -650,19 +733,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
650
733
  start,
651
734
  );
652
735
  }
653
- const argument = parseExpression();
654
- // SANY reads an argument that only names an operator of a parameterized instance, such as
655
- // I!Op, as that operator, which takes the arguments of the instance too.
656
- if (argument.kind === "Identifier") missingArguments.delete(argument);
657
- return argument;
736
+ return parseExpression();
658
737
  };
659
738
 
660
- const missingArguments = new Map<Expression, { location: string; message: string }>();
661
-
662
739
  const lastArguments: { range: SourceRange | undefined } = { range: undefined };
663
740
 
664
741
  const parseArguments = () =>
665
- withoutOffside(() => {
742
+ withBracketLayout(() => {
666
743
  const start = cursor.index;
667
744
  expect("(");
668
745
  const args = parseSeparated(parseArgument);
@@ -689,18 +766,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
689
766
  };
690
767
 
691
768
  const parseBracket = (start: Token): Expression =>
692
- withoutOffside(() => {
769
+ withBracketLayout(() => {
693
770
  const location = getLocation(start);
694
- if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
695
- const kind = isAt("|->", 1) ? "Record" : "RecordSet";
696
- const fields = parseSeparated(() => {
771
+ // SANY takes a keyword for the first field of a record, as it does for a selected field.
772
+ const isIdentifier = peek().kind === "Identifier";
773
+ const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
774
+ if (isRecord || (isIdentifier && isAt(":", 1))) {
775
+ const kind = isRecord ? "Record" : "RecordSet";
776
+ const parseField = (readName: () => string) => {
697
777
  const fieldStart = cursor.index;
698
778
  const fieldLocation = getLocation(peek());
699
- const name = expectIdentifier();
779
+ const name = readName();
700
780
  fieldLocation.range = getRange(fieldStart);
701
- expect(kind === "Record" ? "|->" : ":");
781
+ expect(isRecord ? "|->" : ":");
702
782
  return { name, value: parseExpression(), location: fieldLocation };
703
- });
783
+ };
784
+ const fields = [
785
+ parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
786
+ ...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
787
+ ];
704
788
  expect("]");
705
789
  return { kind, fields, location };
706
790
  }
@@ -738,17 +822,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
738
822
  expect("]");
739
823
  return { kind: "Except", function: expression, updates, location };
740
824
  }
825
+ if (!isTlapm && !isAt("]_")) {
826
+ const args = [expression];
827
+ while (accept(",")) args.push(parseExpression());
828
+ expect("]");
829
+ return { kind: "Bracket", args, location };
830
+ }
741
831
  expect("]_");
742
- return {
743
- kind: "BoxAction",
744
- action: expression,
745
- subscript: parseSubscript(),
746
- location,
747
- };
832
+ return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
748
833
  });
749
834
 
835
+ // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
836
+ const parseSetMembership = (): Expression => {
837
+ const start = cursor.index;
838
+ const element = withRange(parsePrefix(), start);
839
+ const location = getLocation(advance());
840
+ return withRange(
841
+ { kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
842
+ start,
843
+ );
844
+ };
845
+
750
846
  const parseBrace = (start: Token): Expression =>
751
- withoutOffside(() => {
847
+ withBracketLayout(() => {
752
848
  const location = getLocation(start);
753
849
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
754
850
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
@@ -763,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
763
859
  }
764
860
  cursor.index = checkpoint;
765
861
  }
766
- const first = parseExpression();
862
+ const first =
863
+ peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
767
864
  if (accept(":")) {
768
865
  const bounds = parseBounds();
769
866
  expect("}");
@@ -777,7 +874,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
777
874
 
778
875
  const parseTuple = (start: Token): Expression => {
779
876
  const location = getLocation(start);
780
- const elements = withoutOffside(() =>
877
+ const elements = withBracketLayout(() =>
781
878
  isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
782
879
  );
783
880
  if (accept(">>_")) {
@@ -858,9 +955,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
858
955
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
859
956
  };
860
957
 
861
- const parsePrimary = (): Expression => {
958
+ const parsePrimary = (preceding?: Precedence): Expression => {
862
959
  const start = cursor.index;
863
- const isFactStart = start === factStart.index;
864
960
  const token = advance();
865
961
  const location = getLocation(token);
866
962
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -887,16 +983,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
887
983
  const params = getNames(accept("(") ? parseSeparated(expectName) : []);
888
984
  if (params.names.length > 0) expect(")");
889
985
  expect("::");
986
+ const body = parseExpression();
987
+ // TLAplusParser.labelDoesNotChangeParse.
988
+ if (
989
+ !isTlapm &&
990
+ preceding !== undefined &&
991
+ operators.last !== undefined &&
992
+ isOperatorExpression(body) &&
993
+ preceding.high >= operators.last.low
994
+ ) {
995
+ throw createError("Removing the label would change expression parsing", token);
996
+ }
890
997
  return {
891
998
  kind: "Label",
892
999
  name,
893
1000
  params: params.names,
894
1001
  paramRanges: params.ranges,
895
- body: parseExpression(),
1002
+ body,
896
1003
  location,
897
1004
  };
898
1005
  }
899
1006
  const hasArguments = isAt("(");
1007
+ if (name === "@" && !hasArguments && !isSelectorAhead())
1008
+ return { kind: "ExceptAt", location };
900
1009
  const args = hasArguments ? parseArguments() : [];
901
1010
  const argumentRanges = [hasArguments ? lastArguments.range : undefined];
902
1011
  if (!isSelectorAhead()) {
@@ -904,20 +1013,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
904
1013
  ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
905
1014
  : { kind: "Identifier", name, location };
906
1015
  }
907
- const instance = openModules
908
- .flatMap((module) => module.instances)
909
- .find((candidate) => candidate.name === name);
910
- const isMissingArguments =
911
- instance !== undefined &&
912
- !isFactStart &&
913
- !isTlapm &&
914
- instance.params.length !== args.length;
915
- if (isMissingArguments && args.length > 0) {
916
- throw createError(
917
- `The operator ${name} requires ${instance.params.length} arguments`,
918
- token,
919
- );
920
- }
921
1016
  const selectors: Selector[] = [{ kind: "Name", name, args }];
922
1017
  const selectorRanges = [getRange(start, start)];
923
1018
  while (isSelectorAhead()) {
@@ -937,45 +1032,48 @@ const parse = (source: string, isTlapm: boolean): Module => {
937
1032
  const argumentCounts = selectors.map((selector) =>
938
1033
  "args" in selector ? selector.args.length : 0,
939
1034
  );
940
- const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
941
- const reference: Expression =
942
- names.length < selectors.length
943
- ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
944
- : qualifiedArgs.length === 0
945
- ? { kind: "Identifier", name: operator, selectorRanges, location }
946
- : argumentCounts.slice(0, -1).some((count) => count > 0)
947
- ? {
948
- kind: "Apply",
949
- operator,
950
- args: qualifiedArgs,
951
- argumentCounts,
952
- argumentsRange,
953
- selectorRanges,
954
- argumentRanges,
955
- location,
956
- }
957
- : {
958
- kind: "Apply",
959
- operator,
960
- args: qualifiedArgs,
961
- argumentsRange,
962
- selectorRanges,
963
- argumentRanges,
964
- location,
965
- };
966
- if (isMissingArguments) {
967
- missingArguments.set(reference, {
968
- location: formatRange(getRange(start)),
969
- message: `The operator ${name} requires ${instance.params.length} arguments.`,
970
- });
971
- }
972
- return reference;
1035
+ const argumentsRange = argumentRanges.at(-1) ?? getRange(start);
1036
+ return names.length < selectors.length
1037
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1038
+ : qualifiedArgs.length === 0
1039
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1040
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
1041
+ ? {
1042
+ kind: "Apply",
1043
+ operator,
1044
+ args: qualifiedArgs,
1045
+ argumentCounts,
1046
+ argumentsRange,
1047
+ selectorRanges,
1048
+ argumentRanges,
1049
+ location,
1050
+ }
1051
+ : {
1052
+ kind: "Apply",
1053
+ operator,
1054
+ args: qualifiedArgs,
1055
+ argumentsRange,
1056
+ selectorRanges,
1057
+ argumentRanges,
1058
+ location,
1059
+ };
1060
+ }
1061
+ // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1062
+ if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
1063
+ return {
1064
+ kind: "Apply",
1065
+ operator: token.text,
1066
+ spelling: token.spelling,
1067
+ args: parseArguments(),
1068
+ location,
1069
+ };
1070
+ }
1071
+ if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1072
+ return { kind: "Identifier", name: token.text, spelling: token.spelling, location };
973
1073
  }
974
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
975
- return { kind: "Apply", operator: token.text, args: parseArguments(), location };
976
1074
  switch (token.text) {
977
1075
  case "(": {
978
- const expression = withoutOffside(() => parseExpression());
1076
+ const expression = withBracketLayout(() => parseExpression());
979
1077
  expect(")");
980
1078
  return expression;
981
1079
  }
@@ -1031,7 +1129,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1031
1129
  case "WF_":
1032
1130
  case "SF_": {
1033
1131
  const subscript = parseSubscript();
1034
- const action = withoutOffside(() => {
1132
+ const action = withBracketLayout(() => {
1035
1133
  expect("(");
1036
1134
  const expression = parseExpression();
1037
1135
  expect(")");
@@ -1048,7 +1146,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
1048
1146
  throw createUnexpected(`Unexpected ${describe(token)}`, token);
1049
1147
  };
1050
1148
 
1149
+ // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1150
+ // prefix operator.
1151
+ const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1152
+ operators.last = operators.preceding = { low, high };
1153
+ const operand = parseExpression(high + 1);
1154
+ const next = peek();
1155
+ const nextHigh =
1156
+ next.text === "'" || POSTFIX_OPERATORS.has(next.text)
1157
+ ? POSTFIX_PRECEDENCE.high
1158
+ : INFIX_OPERATORS.get(next.text)?.high;
1159
+ if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1160
+ throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
1161
+ }
1162
+ return operand;
1163
+ };
1164
+
1051
1165
  const parsePrefix = (): Expression => {
1166
+ const preceding = operators.preceding;
1167
+ operators.preceding = undefined;
1052
1168
  const token = peek();
1053
1169
  const location = getLocation(token);
1054
1170
  if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
@@ -1058,35 +1174,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
1058
1174
  switch (token.text) {
1059
1175
  case "~":
1060
1176
  advance();
1061
- return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
1177
+ return {
1178
+ kind: "Apply",
1179
+ operator: "~",
1180
+ args: [parsePrefixOperand(token, 4, 4)],
1181
+ location,
1182
+ };
1062
1183
  case "-":
1063
1184
  advance();
1064
1185
  return {
1065
1186
  kind: "Apply",
1066
1187
  operator: "-.",
1067
- args: [parseExpression(NEGATION_PRECEDENCE + 1)],
1188
+ args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
1068
1189
  location,
1069
1190
  };
1070
1191
  case "[]":
1071
1192
  advance();
1072
- return { kind: "Always", expression: parseExpression(16), location };
1193
+ return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
1073
1194
  case "<>":
1074
1195
  advance();
1075
- return { kind: "Eventually", expression: parseExpression(16), location };
1196
+ return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
1076
1197
  case "ENABLED":
1077
1198
  advance();
1078
- return { kind: "Enabled", expression: parseExpression(16), location };
1199
+ return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
1079
1200
  case "UNCHANGED":
1080
1201
  advance();
1081
- return { kind: "Unchanged", expression: parseExpression(16), location };
1202
+ return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
1082
1203
  case "SUBSET":
1083
1204
  case "UNION":
1084
1205
  case "DOMAIN":
1085
1206
  advance();
1086
- return { kind: "Apply", operator: token.text, args: [parseExpression(9)], location };
1207
+ return {
1208
+ kind: "Apply",
1209
+ operator: token.text,
1210
+ // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1211
+ args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1212
+ location,
1213
+ };
1087
1214
  }
1088
1215
  }
1089
- return parsePrimary();
1216
+ return parsePrimary(preceding);
1090
1217
  };
1091
1218
 
1092
1219
  const parseExpression = (minimumPrecedence = 0): Expression => {
@@ -1102,21 +1229,30 @@ const parse = (source: string, isTlapm: boolean): Module => {
1102
1229
  const location = getLocation(token);
1103
1230
  if (token.text === "'" && minimumPrecedence <= 15) {
1104
1231
  advance();
1232
+ operators.last = POSTFIX_PRECEDENCE;
1105
1233
  state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1106
1234
  continue;
1107
1235
  }
1108
1236
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1109
1237
  advance();
1238
+ operators.last = POSTFIX_PRECEDENCE;
1110
1239
  state.left = withRange(
1111
- { kind: "Apply", operator: token.text, args: [state.left], location },
1240
+ {
1241
+ kind: "Apply",
1242
+ operator: token.text,
1243
+ spelling: token.spelling,
1244
+ args: [state.left],
1245
+ location,
1246
+ },
1112
1247
  start,
1113
1248
  );
1114
1249
  continue;
1115
1250
  }
1116
1251
  if (token.text === "[") {
1117
1252
  advance();
1118
- const args = withoutOffside(() => parseSeparated(() => parseExpression()));
1119
- withoutOffside(() => expect("]"));
1253
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1254
+ withBracketLayout(() => expect("]"));
1255
+ operators.last = FUNCTION_PRECEDENCE;
1120
1256
  state.left = withRange(
1121
1257
  { kind: "FunctionApplication", function: state.left, args, location },
1122
1258
  start,
@@ -1147,21 +1283,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
1147
1283
  if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1148
1284
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1149
1285
  }
1286
+ operators.last = operators.preceding = infix;
1150
1287
  const right = parseExpression(infix.high + 1);
1151
1288
  const left = state.left;
1152
1289
  if (token.text === "/\\" || token.text === "\\/") {
1153
1290
  const kind = token.text === "/\\" ? "And" : "Or";
1291
+ const prefixEnds = [getRange(start).end];
1154
1292
  state.left =
1155
1293
  left.kind === kind && previous?.text === token.text
1156
- ? { ...left, operands: [...left.operands, right] }
1157
- : { kind, operands: [left, right], location };
1294
+ ? {
1295
+ ...left,
1296
+ operands: [...left.operands, right],
1297
+ prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
1298
+ }
1299
+ : { kind, operands: [left, right], prefixEnds, location };
1158
1300
  } else if (token.text === "\\X") {
1159
1301
  state.left =
1160
1302
  left.kind === "Product" && previous?.text === "\\X"
1161
1303
  ? { ...left, factors: [...left.factors, right] }
1162
1304
  : { kind: "Product", factors: [left, right], location };
1163
1305
  } else {
1164
- state.left = { kind: "Apply", operator: token.text, args: [left, right], location };
1306
+ state.left = {
1307
+ kind: "Apply",
1308
+ operator: token.text,
1309
+ spelling: token.spelling,
1310
+ args: [left, right],
1311
+ location,
1312
+ };
1165
1313
  }
1166
1314
  state.left = withRange(state.left, start);
1167
1315
  state.previous = { ...infix, text: token.text };
@@ -1224,7 +1372,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1224
1372
  expect("==");
1225
1373
  return parseOperator(operator, [toParam(left), toParam(right)]);
1226
1374
  }
1227
- const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1375
+ const isPrefixDefinition =
1376
+ token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1228
1377
  const isPostfixDefinition =
1229
1378
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1230
1379
  if (isPrefixDefinition || isPostfixDefinition) {
@@ -1237,7 +1386,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1237
1386
  }
1238
1387
  const name = expectIdentifier();
1239
1388
  if (accept("[")) {
1240
- const bounds = withoutOffside(() => parseBounds());
1389
+ const bounds = withBracketLayout(() => parseBounds());
1241
1390
  expect("]");
1242
1391
  expect("==");
1243
1392
  return {
@@ -1268,11 +1417,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1268
1417
  ): Instance => {
1269
1418
  const start = cursor.index;
1270
1419
  expect("INSTANCE");
1271
- const module = expectIdentifier();
1420
+ const module = expectModuleName();
1272
1421
  const substitutions = accept("WITH")
1273
1422
  ? parseSeparated(() => {
1274
1423
  const targetStart = cursor.index;
1275
- const target = advance().text;
1424
+ const target = expectSubstitutionTarget();
1276
1425
  const nameRange = getRange(targetStart);
1277
1426
  expect("<-");
1278
1427
  return { name: target, nameRange, expression: parseArgument() };
@@ -1341,7 +1490,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1341
1490
  const token = peek();
1342
1491
  const next = peek(1);
1343
1492
  if (token.kind !== "Identifier") {
1344
- return isAt("-.") && next.kind === "Identifier" && isAt("==", 2);
1493
+ return (
1494
+ token.kind !== "String" &&
1495
+ DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
1496
+ next.kind === "Identifier" &&
1497
+ isAt("==", 2)
1498
+ );
1345
1499
  }
1346
1500
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1347
1501
  if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
@@ -1433,7 +1587,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
1433
1587
  // start of an expression.
1434
1588
  const parseFact = (): Fact => {
1435
1589
  const start = cursor.index;
1436
- factStart.index = start;
1437
1590
  const token = peek();
1438
1591
  if (
1439
1592
  token.kind === "Operator" &&
@@ -1442,7 +1595,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1442
1595
  ) {
1443
1596
  cursor.index++;
1444
1597
  return withRange(
1445
- { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
1598
+ {
1599
+ kind: "OperatorReference",
1600
+ operator: token.text,
1601
+ spelling: token.spelling,
1602
+ location: getLocation(token),
1603
+ },
1446
1604
  start,
1447
1605
  );
1448
1606
  }
@@ -1495,7 +1653,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1495
1653
  const parseDefs = () => {
1496
1654
  if (isTlapm) return parseSeparated(parseDefinable);
1497
1655
  const defs = parseSeparated(parseFact);
1498
- const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1656
+ const invalid =
1657
+ mode === "tlc"
1658
+ ? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
1659
+ : undefined;
1499
1660
  if (invalid) {
1500
1661
  throw createError("DEF clause entry should describe a defined operator", invalid.location);
1501
1662
  }
@@ -1520,7 +1681,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1520
1681
  location: getLocation(keyword),
1521
1682
  };
1522
1683
  const invalid =
1523
- use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1684
+ use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1524
1685
  if (invalid.length > 0) {
1525
1686
  throw createSanyError(
1526
1687
  invalid.map((fact) => ({
@@ -1615,7 +1776,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1615
1776
  const token = peek();
1616
1777
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1617
1778
  if (isAt("INSTANCE")) {
1618
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1779
+ const start = cursor.index;
1780
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1619
1781
  }
1620
1782
  if (isAt("DEFINE") || isDefinitionStart()) {
1621
1783
  const start = cursor.index;
@@ -1726,6 +1888,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1726
1888
  return withRange(proof, start);
1727
1889
  };
1728
1890
 
1891
+ // SANY binds the name of a theorem or assumption that a module already defines to the first
1892
+ // definition, warning of the second.
1893
+ const addFactDefinition = (module: Module, definition: Definition) => {
1894
+ if (module.definitions.some((other) => other.name === definition.name)) return;
1895
+ module.definitions.push(definition);
1896
+ };
1897
+
1729
1898
  const parseTheorem = (module: Module) => {
1730
1899
  const start = cursor.index;
1731
1900
  const keyword = advance();
@@ -1747,7 +1916,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1747
1916
  module.theorems.push(theorem);
1748
1917
  module.units.push({ kind: "Theorem", theorem });
1749
1918
  if (name === undefined || statement.kind === "AssumeProve") return;
1750
- module.definitions.push(
1919
+ addFactDefinition(
1920
+ module,
1751
1921
  withRange(
1752
1922
  {
1753
1923
  kind: "Operator",
@@ -1784,7 +1954,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1784
1954
  const parseModuleBody = (): Module => {
1785
1955
  const location = getLocation(expect("----"));
1786
1956
  expect("MODULE");
1787
- const name = expectIdentifier();
1957
+ const name = expectModuleName();
1788
1958
  expect("----");
1789
1959
  const module: Module = {
1790
1960
  name,
@@ -1806,7 +1976,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1806
1976
  if (accept("EXTENDS")) {
1807
1977
  parseSeparated(() => {
1808
1978
  const start = cursor.index;
1809
- module.extends.push(expectIdentifier());
1979
+ module.extends.push(expectModuleName());
1810
1980
  module.extendsRanges.push(getRange(start));
1811
1981
  });
1812
1982
  }
@@ -1814,15 +1984,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1814
1984
  const tokenIndex = cursor.index;
1815
1985
  unitStart.index = tokenIndex;
1816
1986
  const token = peek();
1817
- if (token.kind === "ModuleEnd" || token.kind === "End") {
1818
- if (token.kind === "ModuleEnd") {
1819
- location.range = {
1820
- module: openModules[0].name,
1821
- start: { line: location.line, column: location.column },
1822
- end: { line: token.line, column: token.endColumn },
1823
- };
1824
- }
1825
- if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1987
+ if (token.kind === "ModuleEnd") {
1988
+ location.range = {
1989
+ module: openModules[0].name,
1990
+ start: { line: location.line, column: location.column },
1991
+ end: { line: token.line, column: token.endColumn },
1992
+ };
1826
1993
  openModules.pop();
1827
1994
  recursiveScopes.pop();
1828
1995
  return module;
@@ -1841,22 +2008,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
1841
2008
  switch (token.text) {
1842
2009
  case "CONSTANT":
1843
2010
  case "CONSTANTS": {
1844
- advance();
2011
+ const unitLocation = getLocation(advance());
1845
2012
  const declarations = parseDeclarations();
1846
2013
  module.constants.push(...declarations);
1847
- module.units.push({ kind: "Constants", declarations });
2014
+ unitLocation.range = getRange(tokenIndex);
2015
+ module.units.push({ kind: "Constants", declarations, location: unitLocation });
1848
2016
  continue;
1849
2017
  }
1850
2018
  case "VARIABLE":
1851
2019
  case "VARIABLES": {
1852
- advance();
2020
+ const unitLocation = getLocation(advance());
1853
2021
  const declarations = parseSeparated(() => {
1854
2022
  const start = cursor.index;
1855
2023
  const location = getLocation(peek());
1856
2024
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1857
2025
  });
1858
2026
  module.variables.push(...declarations);
1859
- module.units.push({ kind: "Variables", declarations });
2027
+ unitLocation.range = getRange(tokenIndex);
2028
+ module.units.push({ kind: "Variables", declarations, location: unitLocation });
1860
2029
  continue;
1861
2030
  }
1862
2031
  case "ASSUME":
@@ -1866,7 +2035,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1866
2035
  module.assumptions.push(assumption);
1867
2036
  module.units.push({ kind: "Assumption", assumption });
1868
2037
  if (assumption.name !== undefined) {
1869
- module.definitions.push({
2038
+ addFactDefinition(module, {
1870
2039
  kind: "Operator",
1871
2040
  name: assumption.name,
1872
2041
  params: [],
@@ -1880,6 +2049,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1880
2049
  }
1881
2050
  case "USE":
1882
2051
  case "HIDE": {
2052
+ // SANY's module body takes a USE only when ONLY doesn't follow it.
2053
+ if (isAt("USE") && isAt("ONLY", 1)) {
2054
+ throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
2055
+ }
1883
2056
  const use = parseUseOrHide();
1884
2057
  module.uses.push(use);
1885
2058
  module.units.push({ kind: "Use", use });
@@ -1926,9 +2099,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1926
2099
  module.units.push({ kind: "Definition", definition: unit });
1927
2100
  };
1928
2101
 
1929
- return parseModuleBody();
2102
+ const module = parseModuleBody();
2103
+ // SANY's lexer marks where each definition begins before its parser runs, which can split a
2104
+ // module's name from a keyword that the parser would take for one.
2105
+ const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2106
+ if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2107
+ return module;
1930
2108
  };
1931
2109
 
1932
- export const parseModule = (source: string) => parse(source, false);
2110
+ export const parseModule = (source: string) => parse(source, "tlc");
2111
+
2112
+ export const parseTlapmModule = (source: string) => parse(source, "tlapm");
1933
2113
 
1934
- export const parseTlapmModule = (source: string) => parse(source, true);
2114
+ export const parseSanyModule = (source: string) => parse(source, "sany");