tla-ts 0.0.3 → 0.0.4-dev.2be6058

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 +797 -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/parser.ts CHANGED
@@ -22,15 +22,30 @@ import type {
22
22
  StepBody,
23
23
  UseOrHide,
24
24
  } from "./ast";
25
- import { TlaSyntaxError } from "./errors";
26
- import { getNumeralValue, tokenize, type Token } from "./lexer";
25
+ import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
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,
@@ -135,10 +150,25 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
135
150
  ["^^", createInfix(14, 14, false)],
136
151
  ]);
137
152
 
153
+ const NEGATION_PRECEDENCE = 12;
154
+
138
155
  const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
139
156
 
140
157
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
141
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
+
142
172
  const EXPRESSION_PREFIXES = new Set([
143
173
  "-",
144
174
  "~",
@@ -167,6 +197,16 @@ const OPERATOR_ARGUMENTS = new Set([
167
197
  ...POSTFIX_OPERATORS,
168
198
  ]);
169
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
+
170
210
  const isOperatorName = (text: string) =>
171
211
  INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
172
212
 
@@ -260,6 +300,7 @@ const FACT_KEYWORDS = new Set([
260
300
  "IF",
261
301
  "LAMBDA",
262
302
  "LET",
303
+ "MODULE",
263
304
  "SF_",
264
305
  "SUBSET",
265
306
  "UNCHANGED",
@@ -289,30 +330,33 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
289
330
 
290
331
  const NO_OFFSIDE = -1;
291
332
 
292
- const getModuleHeader = (name: string) => new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
293
-
294
- // The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
295
- // 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.
296
335
  export const extractModule = (source: string, name: string) => {
336
+ const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
297
337
  const lines = source.split(/\r?\n/);
298
- const start = lines.findIndex((line) => getModuleHeader(name).test(line));
338
+ const start = lines.findIndex((line) => header.test(line));
299
339
  if (start === -1) return undefined;
300
340
  const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
301
- return lines.slice(start, end === -1 ? undefined : end + 1).join("\n");
341
+ return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
302
342
  };
303
343
 
304
- // isTlapm parses as tlapm does for proof obligations, which accepts and rejects some modules that
305
- // SANY does not.
306
- const parse = (source: string, isTlapm: boolean): Module => {
307
- 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);
308
352
  const cursor = { index: 0, offsideColumns: [] as number[] };
353
+ const operators: OperatorContext = {};
309
354
  // A RECURSIVE declaration covers the definitions of its own LET or module.
310
355
  const recursiveScopes: Set<string>[] = [];
311
356
  const factNames = new Set<string>();
312
357
  // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
313
358
  const proofScopes: Set<string>[] = [];
314
- // A fact may cite an operator of a parameterized instance without the instance's arguments.
315
- const factStart = { index: -1 };
359
+ // SANY reports a location in a nested module as one in the module of its file.
316
360
  const openModules: Module[] = [];
317
361
 
318
362
  const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
@@ -327,15 +371,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
327
371
  const tokenEnds: SourceLocation[] = [];
328
372
  const tokenRanges: SourceRange[] = [];
329
373
 
330
- const getRange = (startIndex: number) => {
331
- const endIndex = cursor.index - 1;
374
+ const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
332
375
  const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
333
376
  if (cached) return cached;
334
377
  const last = tokens[endIndex];
335
378
  tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
336
379
  tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
337
380
  const range = {
338
- module: openModules[openModules.length - 1].name,
381
+ module: openModules[0].name,
339
382
  start: tokenStarts[startIndex],
340
383
  end: tokenEnds[endIndex],
341
384
  };
@@ -433,8 +476,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
433
476
  const createUnexpected = (message: string, token: Token = peekRaw()) =>
434
477
  createError(message, token, token);
435
478
 
479
+ // TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
436
480
  const isFieldName = (token: Token) =>
437
- (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);
438
485
 
439
486
  const isAt = (text: string, offset = 0) => {
440
487
  const token = peek(offset);
@@ -470,7 +517,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
470
517
  return token.text;
471
518
  };
472
519
 
473
- const withoutOffside = <Result>(parse: () => Result) => {
520
+ const expectName = () => {
521
+ const start = cursor.index;
522
+ return { name: expectIdentifier(), range: getRange(start) };
523
+ };
524
+
525
+ const getNames = (names: { name: string; range: SourceRange }[]) => ({
526
+ names: names.map((item) => item.name),
527
+ ranges: names.map((item) => item.range),
528
+ });
529
+
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();
474
556
  cursor.offsideColumns.push(NO_OFFSIDE);
475
557
  const result = parse();
476
558
  cursor.offsideColumns.pop();
@@ -512,10 +594,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
512
594
  };
513
595
 
514
596
  const parsePattern = (): BoundPattern => {
515
- if (!accept("<<")) return { kind: "Name", name: expectIdentifier() };
516
- const names = parseSeparated(expectIdentifier);
597
+ if (!accept("<<")) {
598
+ const named = expectName();
599
+ return { kind: "Name", name: named.name, ranges: [named.range] };
600
+ }
601
+ const names = getNames(parseSeparated(expectName));
517
602
  expect(">>");
518
- return { kind: "Tuple", names };
603
+ return { kind: "Tuple", names: names.names, ranges: names.ranges };
519
604
  };
520
605
 
521
606
  const parseBounds = () =>
@@ -558,8 +643,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
558
643
  );
559
644
  };
560
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.
561
648
  const isStandalone = (operator: Token) => {
562
- if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
563
649
  const token = peek(1);
564
650
  return (
565
651
  token.kind === "End" ||
@@ -598,7 +684,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
598
684
  if (isOperatorAlone) {
599
685
  cursor.index++;
600
686
  return withRange(
601
- { 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
+ },
602
693
  cursor.index - 1,
603
694
  );
604
695
  }
@@ -608,32 +699,52 @@ const parse = (source: string, isTlapm: boolean): Module => {
608
699
  cursor.index++;
609
700
  const location = getLocation(token);
610
701
  if (POSTFIX_OPERATORS.has(token.text)) {
611
- 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
+ );
612
706
  }
613
707
  const argument: Expression = { kind: "Identifier", name: "argument", location };
614
- return {
615
- kind: "Lambda",
616
- params: [argument.name],
617
- body: getOperatorApplication(token.text, argument, location),
618
- location,
619
- };
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
+ );
620
718
  }
621
719
  if (isAt("LAMBDA")) {
622
720
  const start = cursor.index;
623
721
  advance();
624
- const params = parseSeparated(expectIdentifier);
722
+ const params = getNames(parseSeparated(expectName));
625
723
  expect(":");
626
724
  const body = parseExpression();
627
- return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
725
+ return withRange(
726
+ {
727
+ kind: "Lambda",
728
+ params: params.names,
729
+ paramRanges: params.ranges,
730
+ body,
731
+ location: getLocation(token),
732
+ },
733
+ start,
734
+ );
628
735
  }
629
736
  return parseExpression();
630
737
  };
631
738
 
739
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
740
+
632
741
  const parseArguments = () =>
633
- withoutOffside(() => {
742
+ withBracketLayout(() => {
743
+ const start = cursor.index;
634
744
  expect("(");
635
745
  const args = parseSeparated(parseArgument);
636
746
  expect(")");
747
+ lastArguments.range = getRange(start);
637
748
  return args;
638
749
  });
639
750
 
@@ -655,15 +766,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
655
766
  };
656
767
 
657
768
  const parseBracket = (start: Token): Expression =>
658
- withoutOffside(() => {
769
+ withBracketLayout(() => {
659
770
  const location = getLocation(start);
660
- if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
661
- const kind = isAt("|->", 1) ? "Record" : "RecordSet";
662
- const fields = parseSeparated(() => {
663
- const name = expectIdentifier();
664
- expect(kind === "Record" ? "|->" : ":");
665
- return { name, value: parseExpression() };
666
- });
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) => {
777
+ const fieldStart = cursor.index;
778
+ const fieldLocation = getLocation(peek());
779
+ const name = readName();
780
+ fieldLocation.range = getRange(fieldStart);
781
+ expect(isRecord ? "|->" : ":");
782
+ return { name, value: parseExpression(), location: fieldLocation };
783
+ };
784
+ const fields = [
785
+ parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
786
+ ...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
787
+ ];
667
788
  expect("]");
668
789
  return { kind, fields, location };
669
790
  }
@@ -701,17 +822,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
701
822
  expect("]");
702
823
  return { kind: "Except", function: expression, updates, location };
703
824
  }
825
+ if (!isTlapm && !isAt("]_")) {
826
+ const args = [expression];
827
+ while (accept(",")) args.push(parseExpression());
828
+ expect("]");
829
+ return { kind: "Bracket", args, location };
830
+ }
704
831
  expect("]_");
705
- return {
706
- kind: "BoxAction",
707
- action: expression,
708
- subscript: parseSubscript(),
709
- location,
710
- };
832
+ return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
711
833
  });
712
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
+
713
846
  const parseBrace = (start: Token): Expression =>
714
- withoutOffside(() => {
847
+ withBracketLayout(() => {
715
848
  const location = getLocation(start);
716
849
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
717
850
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
@@ -726,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
726
859
  }
727
860
  cursor.index = checkpoint;
728
861
  }
729
- const first = parseExpression();
862
+ const first =
863
+ peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
730
864
  if (accept(":")) {
731
865
  const bounds = parseBounds();
732
866
  expect("}");
@@ -740,7 +874,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
740
874
 
741
875
  const parseTuple = (start: Token): Expression => {
742
876
  const location = getLocation(start);
743
- const elements = withoutOffside(() =>
877
+ const elements = withBracketLayout(() =>
744
878
  isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
745
879
  );
746
880
  if (accept(">>_")) {
@@ -761,12 +895,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
761
895
  expect(":");
762
896
  return { kind, bounds, body: parseExpression(), location };
763
897
  }
764
- const names = parseSeparated(expectIdentifier);
898
+ const names = getNames(parseSeparated(expectName));
765
899
  expect(":");
766
900
  return {
767
901
  kind: "UnboundedQuantifier",
768
902
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
769
- names,
903
+ names: names.names,
904
+ nameRanges: names.ranges,
770
905
  body: parseExpression(),
771
906
  isTemporal,
772
907
  location,
@@ -820,8 +955,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
820
955
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
821
956
  };
822
957
 
823
- const parsePrimary = (): Expression => {
824
- const isFactStart = cursor.index === factStart.index;
958
+ const parsePrimary = (preceding?: Precedence): Expression => {
959
+ const start = cursor.index;
825
960
  const token = advance();
826
961
  const location = getLocation(token);
827
962
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -845,53 +980,100 @@ const parse = (source: string, isTlapm: boolean): Module => {
845
980
  if (token.kind === "Identifier" || isStep) {
846
981
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
847
982
  if (isLabelAhead()) {
848
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
849
- if (params.length > 0) expect(")");
983
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
984
+ if (params.names.length > 0) expect(")");
850
985
  expect("::");
851
- return { kind: "Label", name, params, body: parseExpression(), location };
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
+ }
997
+ return {
998
+ kind: "Label",
999
+ name,
1000
+ params: params.names,
1001
+ paramRanges: params.ranges,
1002
+ body,
1003
+ location,
1004
+ };
852
1005
  }
853
1006
  const hasArguments = isAt("(");
1007
+ if (name === "@" && !hasArguments && !isSelectorAhead())
1008
+ return { kind: "ExceptAt", location };
854
1009
  const args = hasArguments ? parseArguments() : [];
1010
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
855
1011
  if (!isSelectorAhead()) {
856
1012
  return hasArguments
857
- ? { kind: "Apply", operator: name, args, location }
1013
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
858
1014
  : { kind: "Identifier", name, location };
859
1015
  }
860
- const instance = openModules
861
- .flatMap((module) => module.instances)
862
- .find((candidate) => candidate.name === name);
863
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
864
- throw createError(
865
- `The operator ${name} requires ${instance.params.length} arguments`,
866
- token,
867
- );
868
- }
869
1016
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1017
+ const selectorRanges = [getRange(start, start)];
870
1018
  while (isSelectorAhead()) {
871
1019
  advance();
1020
+ selectorRanges.push(getRange(cursor.index, cursor.index));
1021
+ lastArguments.range = undefined;
872
1022
  selectors.push(parseSelector());
1023
+ argumentRanges.push(lastArguments.range);
873
1024
  }
874
1025
  const names = selectors.flatMap((selector) =>
875
1026
  selector.kind === "Name" ? [selector.name] : [],
876
1027
  );
877
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
878
1028
  const operator = names.join("!");
879
1029
  const qualifiedArgs = selectors.flatMap((selector) =>
880
1030
  "args" in selector ? selector.args : [],
881
1031
  );
882
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
883
1032
  const argumentCounts = selectors.map((selector) =>
884
1033
  "args" in selector ? selector.args.length : 0,
885
1034
  );
886
- return argumentCounts.slice(0, -1).some((count) => count > 0)
887
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
888
- : { kind: "Apply", operator, args: qualifiedArgs, location };
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 };
889
1073
  }
890
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
891
- return { kind: "Apply", operator: token.text, args: parseArguments(), location };
892
1074
  switch (token.text) {
893
1075
  case "(": {
894
- const expression = withoutOffside(() => parseExpression());
1076
+ const expression = withBracketLayout(() => parseExpression());
895
1077
  expect(")");
896
1078
  return expression;
897
1079
  }
@@ -947,7 +1129,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
947
1129
  case "WF_":
948
1130
  case "SF_": {
949
1131
  const subscript = parseSubscript();
950
- const action = withoutOffside(() => {
1132
+ const action = withBracketLayout(() => {
951
1133
  expect("(");
952
1134
  const expression = parseExpression();
953
1135
  expect(")");
@@ -964,7 +1146,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
964
1146
  throw createUnexpected(`Unexpected ${describe(token)}`, token);
965
1147
  };
966
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
+
967
1165
  const parsePrefix = (): Expression => {
1166
+ const preceding = operators.preceding;
1167
+ operators.preceding = undefined;
968
1168
  const token = peek();
969
1169
  const location = getLocation(token);
970
1170
  if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
@@ -974,30 +1174,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
974
1174
  switch (token.text) {
975
1175
  case "~":
976
1176
  advance();
977
- return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
1177
+ return {
1178
+ kind: "Apply",
1179
+ operator: "~",
1180
+ args: [parsePrefixOperand(token, 4, 4)],
1181
+ location,
1182
+ };
978
1183
  case "-":
979
1184
  advance();
980
- return { kind: "Apply", operator: "-.", args: [parseExpression(13)], location };
1185
+ return {
1186
+ kind: "Apply",
1187
+ operator: "-.",
1188
+ args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
1189
+ location,
1190
+ };
981
1191
  case "[]":
982
1192
  advance();
983
- return { kind: "Always", expression: parseExpression(16), location };
1193
+ return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
984
1194
  case "<>":
985
1195
  advance();
986
- return { kind: "Eventually", expression: parseExpression(16), location };
1196
+ return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
987
1197
  case "ENABLED":
988
1198
  advance();
989
- return { kind: "Enabled", expression: parseExpression(16), location };
1199
+ return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
990
1200
  case "UNCHANGED":
991
1201
  advance();
992
- return { kind: "Unchanged", expression: parseExpression(16), location };
1202
+ return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
993
1203
  case "SUBSET":
994
1204
  case "UNION":
995
1205
  case "DOMAIN":
996
1206
  advance();
997
- 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
+ };
998
1214
  }
999
1215
  }
1000
- return parsePrimary();
1216
+ return parsePrimary(preceding);
1001
1217
  };
1002
1218
 
1003
1219
  const parseExpression = (minimumPrecedence = 0): Expression => {
@@ -1013,21 +1229,30 @@ const parse = (source: string, isTlapm: boolean): Module => {
1013
1229
  const location = getLocation(token);
1014
1230
  if (token.text === "'" && minimumPrecedence <= 15) {
1015
1231
  advance();
1232
+ operators.last = POSTFIX_PRECEDENCE;
1016
1233
  state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1017
1234
  continue;
1018
1235
  }
1019
1236
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1020
1237
  advance();
1238
+ operators.last = POSTFIX_PRECEDENCE;
1021
1239
  state.left = withRange(
1022
- { 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
+ },
1023
1247
  start,
1024
1248
  );
1025
1249
  continue;
1026
1250
  }
1027
1251
  if (token.text === "[") {
1028
1252
  advance();
1029
- const args = withoutOffside(() => parseSeparated(() => parseExpression()));
1030
- withoutOffside(() => expect("]"));
1253
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1254
+ withBracketLayout(() => expect("]"));
1255
+ operators.last = FUNCTION_PRECEDENCE;
1031
1256
  state.left = withRange(
1032
1257
  { kind: "FunctionApplication", function: state.left, args, location },
1033
1258
  start,
@@ -1054,21 +1279,37 @@ const parse = (source: string, isTlapm: boolean): Module => {
1054
1279
  }
1055
1280
  }
1056
1281
  advance();
1282
+ // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1283
+ if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1284
+ throw createError(`Precedence conflict between ${token.text} and -`, peek());
1285
+ }
1286
+ operators.last = operators.preceding = infix;
1057
1287
  const right = parseExpression(infix.high + 1);
1058
1288
  const left = state.left;
1059
1289
  if (token.text === "/\\" || token.text === "\\/") {
1060
1290
  const kind = token.text === "/\\" ? "And" : "Or";
1291
+ const prefixEnds = [getRange(start).end];
1061
1292
  state.left =
1062
1293
  left.kind === kind && previous?.text === token.text
1063
- ? { ...left, operands: [...left.operands, right] }
1064
- : { 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 };
1065
1300
  } else if (token.text === "\\X") {
1066
1301
  state.left =
1067
1302
  left.kind === "Product" && previous?.text === "\\X"
1068
1303
  ? { ...left, factors: [...left.factors, right] }
1069
1304
  : { kind: "Product", factors: [left, right], location };
1070
1305
  } else {
1071
- 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
+ };
1072
1313
  }
1073
1314
  state.left = withRange(state.left, start);
1074
1315
  state.previous = { ...infix, text: token.text };
@@ -1088,7 +1329,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1088
1329
  const body = parseExpression();
1089
1330
  const last = tokens[cursor.index - 1];
1090
1331
  const end = { line: last.line, column: last.endColumn };
1091
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1332
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1092
1333
  };
1093
1334
 
1094
1335
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1096,6 +1337,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1096
1337
  return withRange(parseDefinitionUnit(isLocal), start);
1097
1338
  };
1098
1339
 
1340
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1341
+ name: named.name,
1342
+ arity: 0,
1343
+ location: {
1344
+ line: named.range.start.line,
1345
+ column: named.range.start.column,
1346
+ range: named.range,
1347
+ },
1348
+ });
1349
+
1099
1350
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1100
1351
  const token = peek();
1101
1352
  const location = getLocation(token);
@@ -1115,28 +1366,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1115
1366
  peek(2).kind === "Identifier" &&
1116
1367
  isAt("==", 3)
1117
1368
  ) {
1118
- const left = expectIdentifier();
1369
+ const left = expectName();
1119
1370
  const operator = advance().text;
1120
- const right = expectIdentifier();
1371
+ const right = expectName();
1121
1372
  expect("==");
1122
- return parseOperator(operator, [
1123
- { name: left, arity: 0 },
1124
- { name: right, arity: 0 },
1125
- ]);
1373
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1126
1374
  }
1127
- const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1375
+ const isPrefixDefinition =
1376
+ token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1128
1377
  const isPostfixDefinition =
1129
1378
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1130
1379
  if (isPrefixDefinition || isPostfixDefinition) {
1131
1380
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1132
- cursor.index += isPrefixDefinition ? 1 : 2;
1133
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1381
+ if (isPrefixDefinition) advance();
1382
+ const param = expectName();
1383
+ if (isPostfixDefinition) advance();
1134
1384
  expect("==");
1135
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1385
+ return parseOperator(operator, [toParam(param)]);
1136
1386
  }
1137
1387
  const name = expectIdentifier();
1138
1388
  if (accept("[")) {
1139
- const bounds = withoutOffside(() => parseBounds());
1389
+ const bounds = withBracketLayout(() => parseBounds());
1140
1390
  expect("]");
1141
1391
  expect("==");
1142
1392
  return {
@@ -1167,12 +1417,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1167
1417
  ): Instance => {
1168
1418
  const start = cursor.index;
1169
1419
  expect("INSTANCE");
1170
- const module = expectIdentifier();
1420
+ const module = expectModuleName();
1171
1421
  const substitutions = accept("WITH")
1172
1422
  ? parseSeparated(() => {
1173
- const target = advance().text;
1423
+ const targetStart = cursor.index;
1424
+ const target = expectSubstitutionTarget();
1425
+ const nameRange = getRange(targetStart);
1174
1426
  expect("<-");
1175
- return { name: target, expression: parseArgument() };
1427
+ return { name: target, nameRange, expression: parseArgument() };
1176
1428
  })
1177
1429
  : [];
1178
1430
  return {
@@ -1188,6 +1440,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1188
1440
  };
1189
1441
 
1190
1442
  const parseDeclaration = (): Declaration => {
1443
+ const start = cursor.index;
1444
+ return withRange(parseDeclarationUnit(), start);
1445
+ };
1446
+
1447
+ const parseDeclarationUnit = (): Declaration => {
1191
1448
  const location = getLocation(peek());
1192
1449
  if (isAt("_") && peek(1).kind === "Operator") {
1193
1450
  advance();
@@ -1233,7 +1490,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1233
1490
  const token = peek();
1234
1491
  const next = peek(1);
1235
1492
  if (token.kind !== "Identifier") {
1236
- 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
+ );
1237
1499
  }
1238
1500
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1239
1501
  if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
@@ -1325,7 +1587,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
1325
1587
  // start of an expression.
1326
1588
  const parseFact = (): Fact => {
1327
1589
  const start = cursor.index;
1328
- factStart.index = start;
1329
1590
  const token = peek();
1330
1591
  if (
1331
1592
  token.kind === "Operator" &&
@@ -1334,7 +1595,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1334
1595
  ) {
1335
1596
  cursor.index++;
1336
1597
  return withRange(
1337
- { 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
+ },
1338
1604
  start,
1339
1605
  );
1340
1606
  }
@@ -1387,7 +1653,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1387
1653
  const parseDefs = () => {
1388
1654
  if (isTlapm) return parseSeparated(parseDefinable);
1389
1655
  const defs = parseSeparated(parseFact);
1390
- 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;
1391
1660
  if (invalid) {
1392
1661
  throw createError("DEF clause entry should describe a defined operator", invalid.location);
1393
1662
  }
@@ -1412,11 +1681,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1412
1681
  location: getLocation(keyword),
1413
1682
  };
1414
1683
  const invalid =
1415
- use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1416
- if (invalid) {
1417
- throw createError(
1418
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1419
- invalid.location,
1684
+ use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1685
+ if (invalid.length > 0) {
1686
+ throw createSanyError(
1687
+ invalid.map((fact) => ({
1688
+ location: formatRange(fact.location.range ?? getRange(start)),
1689
+ message:
1690
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1691
+ })),
1420
1692
  );
1421
1693
  }
1422
1694
  return withRange(use, start);
@@ -1432,10 +1704,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1432
1704
  }
1433
1705
  if (level !== undefined) advance();
1434
1706
  if (level === "Variable") {
1435
- const name = expectIdentifier();
1436
- declareProofSymbol(name);
1707
+ const named = expectName();
1708
+ declareProofSymbol(named.name);
1437
1709
  return withRange(
1438
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1710
+ {
1711
+ kind: "NewSymbol",
1712
+ name: named.name,
1713
+ arity: 0,
1714
+ level,
1715
+ set: undefined,
1716
+ nameRange: named.range,
1717
+ location,
1718
+ },
1439
1719
  start,
1440
1720
  );
1441
1721
  }
@@ -1453,6 +1733,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1453
1733
  arity: declaration.arity,
1454
1734
  level: level ?? "Constant",
1455
1735
  set: isBounded ? parseExpression() : undefined,
1736
+ nameRange: declaration.location.range,
1456
1737
  location,
1457
1738
  },
1458
1739
  start,
@@ -1495,7 +1776,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1495
1776
  const token = peek();
1496
1777
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1497
1778
  if (isAt("INSTANCE")) {
1498
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1779
+ const start = cursor.index;
1780
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1499
1781
  }
1500
1782
  if (isAt("DEFINE") || isDefinitionStart()) {
1501
1783
  const start = cursor.index;
@@ -1508,21 +1790,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1508
1790
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1509
1791
  if (accept("TAKE")) {
1510
1792
  const isBounded = isBoundAhead();
1511
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1793
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1512
1794
  const bounds = isBounded ? parseBounds() : [];
1513
- declareProofSymbols(names, bounds);
1514
- return { kind: "Take", names, bounds };
1795
+ declareProofSymbols(names.names, bounds);
1796
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1515
1797
  }
1516
1798
  if (accept("WITNESS")) {
1517
1799
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1518
1800
  }
1519
1801
  if (accept("PICK")) {
1520
1802
  const isNames = isNameListAhead(":");
1521
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1803
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1522
1804
  const bounds = isNames ? [] : parseBounds();
1523
- declareProofSymbols(names, bounds);
1805
+ declareProofSymbols(names.names, bounds);
1524
1806
  expect(":");
1525
- return { kind: "Pick", names, bounds, body: parseExpression() };
1807
+ return {
1808
+ kind: "Pick",
1809
+ names: names.names,
1810
+ nameRanges: names.ranges,
1811
+ bounds,
1812
+ body: parseExpression(),
1813
+ };
1526
1814
  }
1527
1815
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1528
1816
  return {
@@ -1552,10 +1840,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1552
1840
  const parseStep = (): ProofStep => {
1553
1841
  const start = cursor.index;
1554
1842
  const step = parseStepStart(isAt("QED", 1));
1843
+ const statementStart = cursor.index;
1555
1844
  const scope = new Set<string>();
1556
1845
  proofScopes.push(scope);
1557
1846
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1558
1847
  const head = getRange(start);
1848
+ const statement = getRange(statementStart);
1559
1849
  const hasProof = isProofStart(peek());
1560
1850
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1561
1851
  throw createError("Proof of a step that does not take a proof");
@@ -1563,7 +1853,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1853
  const proof = hasProof ? parseProof() : undefined;
1564
1854
  proofScopes.pop();
1565
1855
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1566
- return withRange({ ...step, body, head, proof }, start);
1856
+ return withRange({ ...step, body, head, statement, proof }, start);
1567
1857
  };
1568
1858
 
1569
1859
  const parseProofBody = (): Proof => {
@@ -1598,6 +1888,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1598
1888
  return withRange(proof, start);
1599
1889
  };
1600
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
+
1601
1898
  const parseTheorem = (module: Module) => {
1602
1899
  const start = cursor.index;
1603
1900
  const keyword = advance();
@@ -1619,7 +1916,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1619
1916
  module.theorems.push(theorem);
1620
1917
  module.units.push({ kind: "Theorem", theorem });
1621
1918
  if (name === undefined || statement.kind === "AssumeProve") return;
1622
- module.definitions.push(
1919
+ addFactDefinition(
1920
+ module,
1623
1921
  withRange(
1624
1922
  {
1625
1923
  kind: "Operator",
@@ -1656,7 +1954,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1656
1954
  const parseModuleBody = (): Module => {
1657
1955
  const location = getLocation(expect("----"));
1658
1956
  expect("MODULE");
1659
- const name = expectIdentifier();
1957
+ const name = expectModuleName();
1660
1958
  expect("----");
1661
1959
  const module: Module = {
1662
1960
  name,
@@ -1678,7 +1976,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1678
1976
  if (accept("EXTENDS")) {
1679
1977
  parseSeparated(() => {
1680
1978
  const start = cursor.index;
1681
- module.extends.push(expectIdentifier());
1979
+ module.extends.push(expectModuleName());
1682
1980
  module.extendsRanges.push(getRange(start));
1683
1981
  });
1684
1982
  }
@@ -1686,14 +1984,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1686
1984
  const tokenIndex = cursor.index;
1687
1985
  unitStart.index = tokenIndex;
1688
1986
  const token = peek();
1689
- if (token.kind === "ModuleEnd" || token.kind === "End") {
1690
- if (token.kind === "ModuleEnd") {
1691
- location.range = {
1692
- module: name,
1693
- start: { line: location.line, column: location.column },
1694
- end: { line: token.line, column: token.endColumn },
1695
- };
1696
- }
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
+ };
1697
1993
  openModules.pop();
1698
1994
  recursiveScopes.pop();
1699
1995
  return module;
@@ -1712,22 +2008,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
1712
2008
  switch (token.text) {
1713
2009
  case "CONSTANT":
1714
2010
  case "CONSTANTS": {
1715
- advance();
2011
+ const unitLocation = getLocation(advance());
1716
2012
  const declarations = parseDeclarations();
1717
2013
  module.constants.push(...declarations);
1718
- module.units.push({ kind: "Constants", declarations });
2014
+ unitLocation.range = getRange(tokenIndex);
2015
+ module.units.push({ kind: "Constants", declarations, location: unitLocation });
1719
2016
  continue;
1720
2017
  }
1721
2018
  case "VARIABLE":
1722
2019
  case "VARIABLES": {
1723
- advance();
2020
+ const unitLocation = getLocation(advance());
1724
2021
  const declarations = parseSeparated(() => {
1725
2022
  const start = cursor.index;
1726
2023
  const location = getLocation(peek());
1727
2024
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1728
2025
  });
1729
2026
  module.variables.push(...declarations);
1730
- module.units.push({ kind: "Variables", declarations });
2027
+ unitLocation.range = getRange(tokenIndex);
2028
+ module.units.push({ kind: "Variables", declarations, location: unitLocation });
1731
2029
  continue;
1732
2030
  }
1733
2031
  case "ASSUME":
@@ -1737,7 +2035,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1737
2035
  module.assumptions.push(assumption);
1738
2036
  module.units.push({ kind: "Assumption", assumption });
1739
2037
  if (assumption.name !== undefined) {
1740
- module.definitions.push({
2038
+ addFactDefinition(module, {
1741
2039
  kind: "Operator",
1742
2040
  name: assumption.name,
1743
2041
  params: [],
@@ -1751,6 +2049,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1751
2049
  }
1752
2050
  case "USE":
1753
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
+ }
1754
2056
  const use = parseUseOrHide();
1755
2057
  module.uses.push(use);
1756
2058
  module.units.push({ kind: "Use", use });
@@ -1775,7 +2077,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1775
2077
  );
1776
2078
  continue;
1777
2079
  }
1778
- addDefinitionUnit(module, parseDefinition(true));
2080
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1779
2081
  continue;
1780
2082
  }
1781
2083
  }
@@ -1797,9 +2099,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1797
2099
  module.units.push({ kind: "Definition", definition: unit });
1798
2100
  };
1799
2101
 
1800
- 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;
1801
2108
  };
1802
2109
 
1803
- 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");
1804
2113
 
1805
- export const parseTlapmModule = (source: string) => parse(source, true);
2114
+ export const parseSanyModule = (source: string) => parse(source, "sany");