tla-ts 0.0.3-dev.fd18eec → 0.0.3-dev.fe239f8

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 (81) 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 +167 -36
  5. package/dist/index.d.ts +167 -36
  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 -21
  12. package/dist/run-cli.js +1351 -21
  13. package/dist/transfer.cjs +83 -34
  14. package/dist/transfer.js +83 -34
  15. package/package.json +5 -1
  16. package/src/ast.ts +53 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1344 -285
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +225 -206
  25. package/src/environment.ts +417 -124
  26. package/src/errors.ts +168 -9
  27. package/src/evaluate.ts +722 -146
  28. package/src/exit-codes.ts +6 -24
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +204 -148
  35. package/src/liveness.ts +190 -117
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +437 -130
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1347 -414
  48. package/src/run-cli.ts +1167 -174
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +25 -5
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +158 -120
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +683 -256
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,36 @@ 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.
359
+ // A fact or an operator argument may cite an operator of a parameterized instance without the
360
+ // instance's arguments, as with Op <- I!Op in a substitution.
317
361
  const factStart = { index: -1 };
362
+ // SANY reports a location in a nested module as one in the module of its file.
318
363
  const openModules: Module[] = [];
319
364
 
320
365
  const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
@@ -329,15 +374,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
329
374
  const tokenEnds: SourceLocation[] = [];
330
375
  const tokenRanges: SourceRange[] = [];
331
376
 
332
- const getRange = (startIndex: number) => {
333
- const endIndex = cursor.index - 1;
377
+ const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
334
378
  const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
335
379
  if (cached) return cached;
336
380
  const last = tokens[endIndex];
337
381
  tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
338
382
  tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
339
383
  const range = {
340
- module: openModules[openModules.length - 1].name,
384
+ module: openModules[0].name,
341
385
  start: tokenStarts[startIndex],
342
386
  end: tokenEnds[endIndex],
343
387
  };
@@ -435,8 +479,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
435
479
  const createUnexpected = (message: string, token: Token = peekRaw()) =>
436
480
  createError(message, token, token);
437
481
 
482
+ // TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
438
483
  const isFieldName = (token: Token) =>
439
- (token.kind === "Identifier" || token.kind === "Keyword") && !isTlapmKeyword(token);
484
+ (token.kind === "Identifier" ||
485
+ token.kind === "Keyword" ||
486
+ (token.kind === "Operator" && token.text === "_")) &&
487
+ !isTlapmKeyword(token);
440
488
 
441
489
  const isAt = (text: string, offset = 0) => {
442
490
  const token = peek(offset);
@@ -472,7 +520,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
472
520
  return token.text;
473
521
  };
474
522
 
475
- const withoutOffside = <Result>(parse: () => Result) => {
523
+ const expectName = () => {
524
+ const start = cursor.index;
525
+ return { name: expectIdentifier(), range: getRange(start) };
526
+ };
527
+
528
+ const getNames = (names: { name: string; range: SourceRange }[]) => ({
529
+ names: names.map((item) => item.name),
530
+ ranges: names.map((item) => item.range),
531
+ });
532
+
533
+ // TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
534
+ const expectSubstitutionTarget = () => {
535
+ const token = peek();
536
+ const isOperator =
537
+ token.kind !== "String" &&
538
+ (DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
539
+ INFIX_OPERATORS.has(token.text) ||
540
+ POSTFIX_OPERATORS.has(token.text) ||
541
+ token.text === "'");
542
+ if (!isOperator) return expectIdentifier();
543
+ cursor.index++;
544
+ return token.text;
545
+ };
546
+
547
+ // SANY takes a keyword for the name of a module, as it does for a field.
548
+ const keywordModuleName = { isUsed: false };
549
+ const expectModuleName = () => {
550
+ if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
551
+ keywordModuleName.isUsed = true;
552
+ return advance().text;
553
+ };
554
+
555
+ // tlapm reads a bracketed expression apart from the junction list around it, whose alignment
556
+ // SANY keeps applying.
557
+ const withBracketLayout = <Result>(parse: () => Result) => {
558
+ if (!isTlapm) return parse();
476
559
  cursor.offsideColumns.push(NO_OFFSIDE);
477
560
  const result = parse();
478
561
  cursor.offsideColumns.pop();
@@ -514,10 +597,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
514
597
  };
515
598
 
516
599
  const parsePattern = (): BoundPattern => {
517
- if (!accept("<<")) return { kind: "Name", name: expectIdentifier() };
518
- const names = parseSeparated(expectIdentifier);
600
+ if (!accept("<<")) {
601
+ const named = expectName();
602
+ return { kind: "Name", name: named.name, ranges: [named.range] };
603
+ }
604
+ const names = getNames(parseSeparated(expectName));
519
605
  expect(">>");
520
- return { kind: "Tuple", names };
606
+ return { kind: "Tuple", names: names.names, ranges: names.ranges };
521
607
  };
522
608
 
523
609
  const parseBounds = () =>
@@ -560,8 +646,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
560
646
  );
561
647
  };
562
648
 
649
+ // SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
650
+ // the start of an expression, such as -.(1), otherwise.
563
651
  const isStandalone = (operator: Token) => {
564
- if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
565
652
  const token = peek(1);
566
653
  return (
567
654
  token.kind === "End" ||
@@ -600,7 +687,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
600
687
  if (isOperatorAlone) {
601
688
  cursor.index++;
602
689
  return withRange(
603
- { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
690
+ {
691
+ kind: "OperatorReference",
692
+ operator: token.text,
693
+ spelling: token.spelling,
694
+ location: getLocation(token),
695
+ },
604
696
  cursor.index - 1,
605
697
  );
606
698
  }
@@ -610,31 +702,53 @@ const parse = (source: string, isTlapm: boolean): Module => {
610
702
  cursor.index++;
611
703
  const location = getLocation(token);
612
704
  if (POSTFIX_OPERATORS.has(token.text)) {
613
- return { kind: "OperatorReference", operator: token.text, location };
705
+ return withRange(
706
+ { kind: "OperatorReference", operator: token.text, location },
707
+ cursor.index - 1,
708
+ );
614
709
  }
615
710
  const argument: Expression = { kind: "Identifier", name: "argument", location };
616
- return {
617
- kind: "Lambda",
618
- params: [argument.name],
619
- body: getOperatorApplication(token.text, argument, location),
620
- location,
621
- };
711
+ return withRange(
712
+ {
713
+ kind: "Lambda",
714
+ params: [argument.name],
715
+ body: getOperatorApplication(token.text, argument, location),
716
+ operator: token.text,
717
+ location: getLocation(token),
718
+ },
719
+ cursor.index - 1,
720
+ );
622
721
  }
623
722
  if (isAt("LAMBDA")) {
624
723
  const start = cursor.index;
625
724
  advance();
626
- const params = parseSeparated(expectIdentifier);
725
+ const params = getNames(parseSeparated(expectName));
627
726
  expect(":");
628
727
  const body = parseExpression();
629
- return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
728
+ return withRange(
729
+ {
730
+ kind: "Lambda",
731
+ params: params.names,
732
+ paramRanges: params.ranges,
733
+ body,
734
+ location: getLocation(token),
735
+ },
736
+ start,
737
+ );
630
738
  }
631
- return parseExpression();
739
+ const argument = parseExpression();
740
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
741
+ // I!Op, as that operator, which takes the arguments of the instance too.
742
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
743
+ return argument;
632
744
  };
633
745
 
746
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
747
+
634
748
  const lastArguments: { range: SourceRange | undefined } = { range: undefined };
635
749
 
636
750
  const parseArguments = () =>
637
- withoutOffside(() => {
751
+ withBracketLayout(() => {
638
752
  const start = cursor.index;
639
753
  expect("(");
640
754
  const args = parseSeparated(parseArgument);
@@ -661,15 +775,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
661
775
  };
662
776
 
663
777
  const parseBracket = (start: Token): Expression =>
664
- withoutOffside(() => {
778
+ withBracketLayout(() => {
665
779
  const location = getLocation(start);
666
- if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
667
- const kind = isAt("|->", 1) ? "Record" : "RecordSet";
668
- const fields = parseSeparated(() => {
669
- const name = expectIdentifier();
670
- expect(kind === "Record" ? "|->" : ":");
671
- return { name, value: parseExpression() };
672
- });
780
+ // SANY takes a keyword for the first field of a record, as it does for a selected field.
781
+ const isIdentifier = peek().kind === "Identifier";
782
+ const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
783
+ if (isRecord || (isIdentifier && isAt(":", 1))) {
784
+ const kind = isRecord ? "Record" : "RecordSet";
785
+ const parseField = (readName: () => string) => {
786
+ const fieldStart = cursor.index;
787
+ const fieldLocation = getLocation(peek());
788
+ const name = readName();
789
+ fieldLocation.range = getRange(fieldStart);
790
+ expect(isRecord ? "|->" : ":");
791
+ return { name, value: parseExpression(), location: fieldLocation };
792
+ };
793
+ const fields = [
794
+ parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
795
+ ...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
796
+ ];
673
797
  expect("]");
674
798
  return { kind, fields, location };
675
799
  }
@@ -707,17 +831,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
707
831
  expect("]");
708
832
  return { kind: "Except", function: expression, updates, location };
709
833
  }
834
+ if (!isTlapm && !isAt("]_")) {
835
+ const args = [expression];
836
+ while (accept(",")) args.push(parseExpression());
837
+ expect("]");
838
+ return { kind: "Bracket", args, location };
839
+ }
710
840
  expect("]_");
711
- return {
712
- kind: "BoxAction",
713
- action: expression,
714
- subscript: parseSubscript(),
715
- location,
716
- };
841
+ return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
717
842
  });
718
843
 
844
+ // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
845
+ const parseSetMembership = (): Expression => {
846
+ const start = cursor.index;
847
+ const element = withRange(parsePrefix(), start);
848
+ const location = getLocation(advance());
849
+ return withRange(
850
+ { kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
851
+ start,
852
+ );
853
+ };
854
+
719
855
  const parseBrace = (start: Token): Expression =>
720
- withoutOffside(() => {
856
+ withBracketLayout(() => {
721
857
  const location = getLocation(start);
722
858
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
723
859
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
@@ -732,7 +868,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
732
868
  }
733
869
  cursor.index = checkpoint;
734
870
  }
735
- const first = parseExpression();
871
+ const first =
872
+ peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
736
873
  if (accept(":")) {
737
874
  const bounds = parseBounds();
738
875
  expect("}");
@@ -746,7 +883,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
746
883
 
747
884
  const parseTuple = (start: Token): Expression => {
748
885
  const location = getLocation(start);
749
- const elements = withoutOffside(() =>
886
+ const elements = withBracketLayout(() =>
750
887
  isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
751
888
  );
752
889
  if (accept(">>_")) {
@@ -767,12 +904,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
767
904
  expect(":");
768
905
  return { kind, bounds, body: parseExpression(), location };
769
906
  }
770
- const names = parseSeparated(expectIdentifier);
907
+ const names = getNames(parseSeparated(expectName));
771
908
  expect(":");
772
909
  return {
773
910
  kind: "UnboundedQuantifier",
774
911
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
775
- names,
912
+ names: names.names,
913
+ nameRanges: names.ranges,
776
914
  body: parseExpression(),
777
915
  isTemporal,
778
916
  location,
@@ -826,8 +964,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
826
964
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
827
965
  };
828
966
 
829
- const parsePrimary = (): Expression => {
830
- const isFactStart = cursor.index === factStart.index;
967
+ const parsePrimary = (preceding?: Precedence): Expression => {
968
+ const start = cursor.index;
969
+ const isFactStart = start === factStart.index;
831
970
  const token = advance();
832
971
  const location = getLocation(token);
833
972
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -851,13 +990,34 @@ const parse = (source: string, isTlapm: boolean): Module => {
851
990
  if (token.kind === "Identifier" || isStep) {
852
991
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
853
992
  if (isLabelAhead()) {
854
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
855
- if (params.length > 0) expect(")");
993
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
994
+ if (params.names.length > 0) expect(")");
856
995
  expect("::");
857
- return { kind: "Label", name, params, body: parseExpression(), location };
996
+ const body = parseExpression();
997
+ // TLAplusParser.labelDoesNotChangeParse.
998
+ if (
999
+ !isTlapm &&
1000
+ preceding !== undefined &&
1001
+ operators.last !== undefined &&
1002
+ isOperatorExpression(body) &&
1003
+ preceding.high >= operators.last.low
1004
+ ) {
1005
+ throw createError("Removing the label would change expression parsing", token);
1006
+ }
1007
+ return {
1008
+ kind: "Label",
1009
+ name,
1010
+ params: params.names,
1011
+ paramRanges: params.ranges,
1012
+ body,
1013
+ location,
1014
+ };
858
1015
  }
859
1016
  const hasArguments = isAt("(");
1017
+ if (name === "@" && !hasArguments && !isSelectorAhead())
1018
+ return { kind: "ExceptAt", location };
860
1019
  const args = hasArguments ? parseArguments() : [];
1020
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
861
1021
  if (!isSelectorAhead()) {
862
1022
  return hasArguments
863
1023
  ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
@@ -866,39 +1026,80 @@ const parse = (source: string, isTlapm: boolean): Module => {
866
1026
  const instance = openModules
867
1027
  .flatMap((module) => module.instances)
868
1028
  .find((candidate) => candidate.name === name);
869
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
1029
+ const isMissingArguments =
1030
+ instance !== undefined &&
1031
+ mode === "tlc" &&
1032
+ !isFactStart &&
1033
+ instance.params.length !== args.length;
1034
+ if (isMissingArguments && args.length > 0) {
870
1035
  throw createError(
871
1036
  `The operator ${name} requires ${instance.params.length} arguments`,
872
1037
  token,
873
1038
  );
874
1039
  }
875
1040
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1041
+ const selectorRanges = [getRange(start, start)];
876
1042
  while (isSelectorAhead()) {
877
1043
  advance();
1044
+ selectorRanges.push(getRange(cursor.index, cursor.index));
1045
+ lastArguments.range = undefined;
878
1046
  selectors.push(parseSelector());
1047
+ argumentRanges.push(lastArguments.range);
879
1048
  }
880
1049
  const names = selectors.flatMap((selector) =>
881
1050
  selector.kind === "Name" ? [selector.name] : [],
882
1051
  );
883
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
884
1052
  const operator = names.join("!");
885
1053
  const qualifiedArgs = selectors.flatMap((selector) =>
886
1054
  "args" in selector ? selector.args : [],
887
1055
  );
888
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
889
1056
  const argumentCounts = selectors.map((selector) =>
890
1057
  "args" in selector ? selector.args.length : 0,
891
1058
  );
892
- const argumentsRange = lastArguments.range;
893
- return argumentCounts.slice(0, -1).some((count) => count > 0)
894
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
895
- : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
1059
+ const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
1060
+ const reference: Expression =
1061
+ names.length < selectors.length
1062
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1063
+ : qualifiedArgs.length === 0
1064
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1065
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
1066
+ ? {
1067
+ kind: "Apply",
1068
+ operator,
1069
+ args: qualifiedArgs,
1070
+ argumentCounts,
1071
+ argumentsRange,
1072
+ selectorRanges,
1073
+ argumentRanges,
1074
+ location,
1075
+ }
1076
+ : {
1077
+ kind: "Apply",
1078
+ operator,
1079
+ args: qualifiedArgs,
1080
+ argumentsRange,
1081
+ selectorRanges,
1082
+ argumentRanges,
1083
+ location,
1084
+ };
1085
+ if (isMissingArguments) {
1086
+ missingArguments.set(reference, {
1087
+ location: formatRange(getRange(start)),
1088
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
1089
+ });
1090
+ }
1091
+ return reference;
896
1092
  }
897
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
1093
+ // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1094
+ if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
898
1095
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
1096
+ }
1097
+ if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1098
+ return { kind: "Identifier", name: token.text, location };
1099
+ }
899
1100
  switch (token.text) {
900
1101
  case "(": {
901
- const expression = withoutOffside(() => parseExpression());
1102
+ const expression = withBracketLayout(() => parseExpression());
902
1103
  expect(")");
903
1104
  return expression;
904
1105
  }
@@ -954,7 +1155,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
954
1155
  case "WF_":
955
1156
  case "SF_": {
956
1157
  const subscript = parseSubscript();
957
- const action = withoutOffside(() => {
1158
+ const action = withBracketLayout(() => {
958
1159
  expect("(");
959
1160
  const expression = parseExpression();
960
1161
  expect(")");
@@ -971,7 +1172,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
971
1172
  throw createUnexpected(`Unexpected ${describe(token)}`, token);
972
1173
  };
973
1174
 
1175
+ // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1176
+ // prefix operator.
1177
+ const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1178
+ operators.last = operators.preceding = { low, high };
1179
+ const operand = parseExpression(high + 1);
1180
+ const next = peek();
1181
+ const nextHigh =
1182
+ next.text === "'" || POSTFIX_OPERATORS.has(next.text)
1183
+ ? POSTFIX_PRECEDENCE.high
1184
+ : INFIX_OPERATORS.get(next.text)?.high;
1185
+ if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1186
+ throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
1187
+ }
1188
+ return operand;
1189
+ };
1190
+
974
1191
  const parsePrefix = (): Expression => {
1192
+ const preceding = operators.preceding;
1193
+ operators.preceding = undefined;
975
1194
  const token = peek();
976
1195
  const location = getLocation(token);
977
1196
  if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
@@ -981,35 +1200,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
981
1200
  switch (token.text) {
982
1201
  case "~":
983
1202
  advance();
984
- return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
1203
+ return {
1204
+ kind: "Apply",
1205
+ operator: "~",
1206
+ args: [parsePrefixOperand(token, 4, 4)],
1207
+ location,
1208
+ };
985
1209
  case "-":
986
1210
  advance();
987
1211
  return {
988
1212
  kind: "Apply",
989
1213
  operator: "-.",
990
- args: [parseExpression(NEGATION_PRECEDENCE + 1)],
1214
+ args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
991
1215
  location,
992
1216
  };
993
1217
  case "[]":
994
1218
  advance();
995
- return { kind: "Always", expression: parseExpression(16), location };
1219
+ return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
996
1220
  case "<>":
997
1221
  advance();
998
- return { kind: "Eventually", expression: parseExpression(16), location };
1222
+ return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
999
1223
  case "ENABLED":
1000
1224
  advance();
1001
- return { kind: "Enabled", expression: parseExpression(16), location };
1225
+ return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
1002
1226
  case "UNCHANGED":
1003
1227
  advance();
1004
- return { kind: "Unchanged", expression: parseExpression(16), location };
1228
+ return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
1005
1229
  case "SUBSET":
1006
1230
  case "UNION":
1007
1231
  case "DOMAIN":
1008
1232
  advance();
1009
- return { kind: "Apply", operator: token.text, args: [parseExpression(9)], location };
1233
+ return {
1234
+ kind: "Apply",
1235
+ operator: token.text,
1236
+ // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1237
+ args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1238
+ location,
1239
+ };
1010
1240
  }
1011
1241
  }
1012
- return parsePrimary();
1242
+ return parsePrimary(preceding);
1013
1243
  };
1014
1244
 
1015
1245
  const parseExpression = (minimumPrecedence = 0): Expression => {
@@ -1025,11 +1255,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1025
1255
  const location = getLocation(token);
1026
1256
  if (token.text === "'" && minimumPrecedence <= 15) {
1027
1257
  advance();
1258
+ operators.last = POSTFIX_PRECEDENCE;
1028
1259
  state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1029
1260
  continue;
1030
1261
  }
1031
1262
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1032
1263
  advance();
1264
+ operators.last = POSTFIX_PRECEDENCE;
1033
1265
  state.left = withRange(
1034
1266
  { kind: "Apply", operator: token.text, args: [state.left], location },
1035
1267
  start,
@@ -1038,8 +1270,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
1038
1270
  }
1039
1271
  if (token.text === "[") {
1040
1272
  advance();
1041
- const args = withoutOffside(() => parseSeparated(() => parseExpression()));
1042
- withoutOffside(() => expect("]"));
1273
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1274
+ withBracketLayout(() => expect("]"));
1275
+ operators.last = FUNCTION_PRECEDENCE;
1043
1276
  state.left = withRange(
1044
1277
  { kind: "FunctionApplication", function: state.left, args, location },
1045
1278
  start,
@@ -1070,21 +1303,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
1070
1303
  if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1071
1304
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1072
1305
  }
1306
+ operators.last = operators.preceding = infix;
1073
1307
  const right = parseExpression(infix.high + 1);
1074
1308
  const left = state.left;
1075
1309
  if (token.text === "/\\" || token.text === "\\/") {
1076
1310
  const kind = token.text === "/\\" ? "And" : "Or";
1311
+ const prefixEnds = [getRange(start).end];
1077
1312
  state.left =
1078
1313
  left.kind === kind && previous?.text === token.text
1079
- ? { ...left, operands: [...left.operands, right] }
1080
- : { kind, operands: [left, right], location };
1314
+ ? {
1315
+ ...left,
1316
+ operands: [...left.operands, right],
1317
+ prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
1318
+ }
1319
+ : { kind, operands: [left, right], prefixEnds, location };
1081
1320
  } else if (token.text === "\\X") {
1082
1321
  state.left =
1083
1322
  left.kind === "Product" && previous?.text === "\\X"
1084
1323
  ? { ...left, factors: [...left.factors, right] }
1085
1324
  : { kind: "Product", factors: [left, right], location };
1086
1325
  } else {
1087
- state.left = { kind: "Apply", operator: token.text, args: [left, right], location };
1326
+ state.left = {
1327
+ kind: "Apply",
1328
+ operator: token.text,
1329
+ spelling: token.spelling,
1330
+ args: [left, right],
1331
+ location,
1332
+ };
1088
1333
  }
1089
1334
  state.left = withRange(state.left, start);
1090
1335
  state.previous = { ...infix, text: token.text };
@@ -1104,7 +1349,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1104
1349
  const body = parseExpression();
1105
1350
  const last = tokens[cursor.index - 1];
1106
1351
  const end = { line: last.line, column: last.endColumn };
1107
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1352
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1108
1353
  };
1109
1354
 
1110
1355
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1112,6 +1357,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1112
1357
  return withRange(parseDefinitionUnit(isLocal), start);
1113
1358
  };
1114
1359
 
1360
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1361
+ name: named.name,
1362
+ arity: 0,
1363
+ location: {
1364
+ line: named.range.start.line,
1365
+ column: named.range.start.column,
1366
+ range: named.range,
1367
+ },
1368
+ });
1369
+
1115
1370
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1116
1371
  const token = peek();
1117
1372
  const location = getLocation(token);
@@ -1131,28 +1386,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1131
1386
  peek(2).kind === "Identifier" &&
1132
1387
  isAt("==", 3)
1133
1388
  ) {
1134
- const left = expectIdentifier();
1389
+ const left = expectName();
1135
1390
  const operator = advance().text;
1136
- const right = expectIdentifier();
1391
+ const right = expectName();
1137
1392
  expect("==");
1138
- return parseOperator(operator, [
1139
- { name: left, arity: 0 },
1140
- { name: right, arity: 0 },
1141
- ]);
1393
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1142
1394
  }
1143
- const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1395
+ const isPrefixDefinition =
1396
+ token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1144
1397
  const isPostfixDefinition =
1145
1398
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1146
1399
  if (isPrefixDefinition || isPostfixDefinition) {
1147
1400
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1148
- cursor.index += isPrefixDefinition ? 1 : 2;
1149
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1401
+ if (isPrefixDefinition) advance();
1402
+ const param = expectName();
1403
+ if (isPostfixDefinition) advance();
1150
1404
  expect("==");
1151
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1405
+ return parseOperator(operator, [toParam(param)]);
1152
1406
  }
1153
1407
  const name = expectIdentifier();
1154
1408
  if (accept("[")) {
1155
- const bounds = withoutOffside(() => parseBounds());
1409
+ const bounds = withBracketLayout(() => parseBounds());
1156
1410
  expect("]");
1157
1411
  expect("==");
1158
1412
  return {
@@ -1183,12 +1437,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1183
1437
  ): Instance => {
1184
1438
  const start = cursor.index;
1185
1439
  expect("INSTANCE");
1186
- const module = expectIdentifier();
1440
+ const module = expectModuleName();
1187
1441
  const substitutions = accept("WITH")
1188
1442
  ? parseSeparated(() => {
1189
- const target = advance().text;
1443
+ const targetStart = cursor.index;
1444
+ const target = expectSubstitutionTarget();
1445
+ const nameRange = getRange(targetStart);
1190
1446
  expect("<-");
1191
- return { name: target, expression: parseArgument() };
1447
+ return { name: target, nameRange, expression: parseArgument() };
1192
1448
  })
1193
1449
  : [];
1194
1450
  return {
@@ -1204,6 +1460,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1204
1460
  };
1205
1461
 
1206
1462
  const parseDeclaration = (): Declaration => {
1463
+ const start = cursor.index;
1464
+ return withRange(parseDeclarationUnit(), start);
1465
+ };
1466
+
1467
+ const parseDeclarationUnit = (): Declaration => {
1207
1468
  const location = getLocation(peek());
1208
1469
  if (isAt("_") && peek(1).kind === "Operator") {
1209
1470
  advance();
@@ -1249,7 +1510,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1249
1510
  const token = peek();
1250
1511
  const next = peek(1);
1251
1512
  if (token.kind !== "Identifier") {
1252
- return isAt("-.") && next.kind === "Identifier" && isAt("==", 2);
1513
+ return (
1514
+ token.kind !== "String" &&
1515
+ DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
1516
+ next.kind === "Identifier" &&
1517
+ isAt("==", 2)
1518
+ );
1253
1519
  }
1254
1520
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1255
1521
  if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
@@ -1403,7 +1669,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1403
1669
  const parseDefs = () => {
1404
1670
  if (isTlapm) return parseSeparated(parseDefinable);
1405
1671
  const defs = parseSeparated(parseFact);
1406
- const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1672
+ const invalid =
1673
+ mode === "tlc"
1674
+ ? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
1675
+ : undefined;
1407
1676
  if (invalid) {
1408
1677
  throw createError("DEF clause entry should describe a defined operator", invalid.location);
1409
1678
  }
@@ -1428,7 +1697,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1428
1697
  location: getLocation(keyword),
1429
1698
  };
1430
1699
  const invalid =
1431
- use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1700
+ use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1432
1701
  if (invalid.length > 0) {
1433
1702
  throw createSanyError(
1434
1703
  invalid.map((fact) => ({
@@ -1451,10 +1720,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1451
1720
  }
1452
1721
  if (level !== undefined) advance();
1453
1722
  if (level === "Variable") {
1454
- const name = expectIdentifier();
1455
- declareProofSymbol(name);
1723
+ const named = expectName();
1724
+ declareProofSymbol(named.name);
1456
1725
  return withRange(
1457
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1726
+ {
1727
+ kind: "NewSymbol",
1728
+ name: named.name,
1729
+ arity: 0,
1730
+ level,
1731
+ set: undefined,
1732
+ nameRange: named.range,
1733
+ location,
1734
+ },
1458
1735
  start,
1459
1736
  );
1460
1737
  }
@@ -1472,6 +1749,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1472
1749
  arity: declaration.arity,
1473
1750
  level: level ?? "Constant",
1474
1751
  set: isBounded ? parseExpression() : undefined,
1752
+ nameRange: declaration.location.range,
1475
1753
  location,
1476
1754
  },
1477
1755
  start,
@@ -1514,7 +1792,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1514
1792
  const token = peek();
1515
1793
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1516
1794
  if (isAt("INSTANCE")) {
1517
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1795
+ const start = cursor.index;
1796
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1518
1797
  }
1519
1798
  if (isAt("DEFINE") || isDefinitionStart()) {
1520
1799
  const start = cursor.index;
@@ -1527,21 +1806,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1527
1806
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1528
1807
  if (accept("TAKE")) {
1529
1808
  const isBounded = isBoundAhead();
1530
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1809
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1531
1810
  const bounds = isBounded ? parseBounds() : [];
1532
- declareProofSymbols(names, bounds);
1533
- return { kind: "Take", names, bounds };
1811
+ declareProofSymbols(names.names, bounds);
1812
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1534
1813
  }
1535
1814
  if (accept("WITNESS")) {
1536
1815
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1537
1816
  }
1538
1817
  if (accept("PICK")) {
1539
1818
  const isNames = isNameListAhead(":");
1540
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1819
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1541
1820
  const bounds = isNames ? [] : parseBounds();
1542
- declareProofSymbols(names, bounds);
1821
+ declareProofSymbols(names.names, bounds);
1543
1822
  expect(":");
1544
- return { kind: "Pick", names, bounds, body: parseExpression() };
1823
+ return {
1824
+ kind: "Pick",
1825
+ names: names.names,
1826
+ nameRanges: names.ranges,
1827
+ bounds,
1828
+ body: parseExpression(),
1829
+ };
1545
1830
  }
1546
1831
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1547
1832
  return {
@@ -1571,10 +1856,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1571
1856
  const parseStep = (): ProofStep => {
1572
1857
  const start = cursor.index;
1573
1858
  const step = parseStepStart(isAt("QED", 1));
1859
+ const statementStart = cursor.index;
1574
1860
  const scope = new Set<string>();
1575
1861
  proofScopes.push(scope);
1576
1862
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1577
1863
  const head = getRange(start);
1864
+ const statement = getRange(statementStart);
1578
1865
  const hasProof = isProofStart(peek());
1579
1866
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1580
1867
  throw createError("Proof of a step that does not take a proof");
@@ -1582,7 +1869,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1582
1869
  const proof = hasProof ? parseProof() : undefined;
1583
1870
  proofScopes.pop();
1584
1871
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1585
- return withRange({ ...step, body, head, proof }, start);
1872
+ return withRange({ ...step, body, head, statement, proof }, start);
1586
1873
  };
1587
1874
 
1588
1875
  const parseProofBody = (): Proof => {
@@ -1617,6 +1904,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1617
1904
  return withRange(proof, start);
1618
1905
  };
1619
1906
 
1907
+ // SANY binds the name of a theorem or assumption that a module already defines to the first
1908
+ // definition, warning of the second.
1909
+ const addFactDefinition = (module: Module, definition: Definition) => {
1910
+ if (module.definitions.some((other) => other.name === definition.name)) return;
1911
+ module.definitions.push(definition);
1912
+ };
1913
+
1620
1914
  const parseTheorem = (module: Module) => {
1621
1915
  const start = cursor.index;
1622
1916
  const keyword = advance();
@@ -1638,7 +1932,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1638
1932
  module.theorems.push(theorem);
1639
1933
  module.units.push({ kind: "Theorem", theorem });
1640
1934
  if (name === undefined || statement.kind === "AssumeProve") return;
1641
- module.definitions.push(
1935
+ addFactDefinition(
1936
+ module,
1642
1937
  withRange(
1643
1938
  {
1644
1939
  kind: "Operator",
@@ -1675,7 +1970,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1675
1970
  const parseModuleBody = (): Module => {
1676
1971
  const location = getLocation(expect("----"));
1677
1972
  expect("MODULE");
1678
- const name = expectIdentifier();
1973
+ const name = expectModuleName();
1679
1974
  expect("----");
1680
1975
  const module: Module = {
1681
1976
  name,
@@ -1697,7 +1992,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1697
1992
  if (accept("EXTENDS")) {
1698
1993
  parseSeparated(() => {
1699
1994
  const start = cursor.index;
1700
- module.extends.push(expectIdentifier());
1995
+ module.extends.push(expectModuleName());
1701
1996
  module.extendsRanges.push(getRange(start));
1702
1997
  });
1703
1998
  }
@@ -1705,14 +2000,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1705
2000
  const tokenIndex = cursor.index;
1706
2001
  unitStart.index = tokenIndex;
1707
2002
  const token = peek();
1708
- if (token.kind === "ModuleEnd" || token.kind === "End") {
1709
- if (token.kind === "ModuleEnd") {
1710
- location.range = {
1711
- module: name,
1712
- start: { line: location.line, column: location.column },
1713
- end: { line: token.line, column: token.endColumn },
1714
- };
1715
- }
2003
+ if (token.kind === "ModuleEnd") {
2004
+ location.range = {
2005
+ module: openModules[0].name,
2006
+ start: { line: location.line, column: location.column },
2007
+ end: { line: token.line, column: token.endColumn },
2008
+ };
2009
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1716
2010
  openModules.pop();
1717
2011
  recursiveScopes.pop();
1718
2012
  return module;
@@ -1731,22 +2025,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
1731
2025
  switch (token.text) {
1732
2026
  case "CONSTANT":
1733
2027
  case "CONSTANTS": {
1734
- advance();
2028
+ const unitLocation = getLocation(advance());
1735
2029
  const declarations = parseDeclarations();
1736
2030
  module.constants.push(...declarations);
1737
- module.units.push({ kind: "Constants", declarations });
2031
+ unitLocation.range = getRange(tokenIndex);
2032
+ module.units.push({ kind: "Constants", declarations, location: unitLocation });
1738
2033
  continue;
1739
2034
  }
1740
2035
  case "VARIABLE":
1741
2036
  case "VARIABLES": {
1742
- advance();
2037
+ const unitLocation = getLocation(advance());
1743
2038
  const declarations = parseSeparated(() => {
1744
2039
  const start = cursor.index;
1745
2040
  const location = getLocation(peek());
1746
2041
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1747
2042
  });
1748
2043
  module.variables.push(...declarations);
1749
- module.units.push({ kind: "Variables", declarations });
2044
+ unitLocation.range = getRange(tokenIndex);
2045
+ module.units.push({ kind: "Variables", declarations, location: unitLocation });
1750
2046
  continue;
1751
2047
  }
1752
2048
  case "ASSUME":
@@ -1756,7 +2052,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1756
2052
  module.assumptions.push(assumption);
1757
2053
  module.units.push({ kind: "Assumption", assumption });
1758
2054
  if (assumption.name !== undefined) {
1759
- module.definitions.push({
2055
+ addFactDefinition(module, {
1760
2056
  kind: "Operator",
1761
2057
  name: assumption.name,
1762
2058
  params: [],
@@ -1770,6 +2066,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1770
2066
  }
1771
2067
  case "USE":
1772
2068
  case "HIDE": {
2069
+ // SANY's module body takes a USE only when ONLY doesn't follow it.
2070
+ if (isAt("USE") && isAt("ONLY", 1)) {
2071
+ throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
2072
+ }
1773
2073
  const use = parseUseOrHide();
1774
2074
  module.uses.push(use);
1775
2075
  module.units.push({ kind: "Use", use });
@@ -1794,7 +2094,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1794
2094
  );
1795
2095
  continue;
1796
2096
  }
1797
- addDefinitionUnit(module, parseDefinition(true));
2097
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1798
2098
  continue;
1799
2099
  }
1800
2100
  }
@@ -1816,9 +2116,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1816
2116
  module.units.push({ kind: "Definition", definition: unit });
1817
2117
  };
1818
2118
 
1819
- return parseModuleBody();
2119
+ const module = parseModuleBody();
2120
+ // SANY's lexer marks where each definition begins before its parser runs, which can split a
2121
+ // module's name from a keyword that the parser would take for one.
2122
+ const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2123
+ if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2124
+ return module;
1820
2125
  };
1821
2126
 
1822
- export const parseModule = (source: string) => parse(source, false);
2127
+ export const parseModule = (source: string) => parse(source, "tlc");
2128
+
2129
+ export const parseTlapmModule = (source: string) => parse(source, "tlapm");
1823
2130
 
1824
- export const parseTlapmModule = (source: string) => parse(source, true);
2131
+ export const parseSanyModule = (source: string) => parse(source, "sany");