tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.a91da46

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 -27
  5. package/dist/index.d.ts +167 -27
  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 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +83 -28
  14. package/dist/transfer.js +83 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +52 -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 +1332 -262
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +431 -120
  26. package/src/errors.ts +195 -7
  27. package/src/evaluate.ts +718 -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 +436 -128
  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 +1364 -405
  48. package/src/run-cli.ts +1167 -155
  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 +26 -7
  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
@@ -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,
@@ -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
  }
@@ -623,19 +715,38 @@ const parse = (source: string, isTlapm: boolean): Module => {
623
715
  if (isAt("LAMBDA")) {
624
716
  const start = cursor.index;
625
717
  advance();
626
- const params = parseSeparated(expectIdentifier);
718
+ const params = getNames(parseSeparated(expectName));
627
719
  expect(":");
628
720
  const body = parseExpression();
629
- return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
721
+ return withRange(
722
+ {
723
+ kind: "Lambda",
724
+ params: params.names,
725
+ paramRanges: params.ranges,
726
+ body,
727
+ location: getLocation(token),
728
+ },
729
+ start,
730
+ );
630
731
  }
631
- return parseExpression();
732
+ const argument = parseExpression();
733
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
734
+ // I!Op, as that operator, which takes the arguments of the instance too.
735
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
736
+ return argument;
632
737
  };
633
738
 
739
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
740
+
741
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
742
+
634
743
  const parseArguments = () =>
635
- withoutOffside(() => {
744
+ withBracketLayout(() => {
745
+ const start = cursor.index;
636
746
  expect("(");
637
747
  const args = parseSeparated(parseArgument);
638
748
  expect(")");
749
+ lastArguments.range = getRange(start);
639
750
  return args;
640
751
  });
641
752
 
@@ -657,15 +768,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
657
768
  };
658
769
 
659
770
  const parseBracket = (start: Token): Expression =>
660
- withoutOffside(() => {
771
+ withBracketLayout(() => {
661
772
  const location = getLocation(start);
662
- if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
663
- const kind = isAt("|->", 1) ? "Record" : "RecordSet";
664
- const fields = parseSeparated(() => {
665
- const name = expectIdentifier();
666
- expect(kind === "Record" ? "|->" : ":");
667
- return { name, value: parseExpression() };
668
- });
773
+ // SANY takes a keyword for the first field of a record, as it does for a selected field.
774
+ const isIdentifier = peek().kind === "Identifier";
775
+ const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
776
+ if (isRecord || (isIdentifier && isAt(":", 1))) {
777
+ const kind = isRecord ? "Record" : "RecordSet";
778
+ const parseField = (readName: () => string) => {
779
+ const fieldStart = cursor.index;
780
+ const fieldLocation = getLocation(peek());
781
+ const name = readName();
782
+ fieldLocation.range = getRange(fieldStart);
783
+ expect(isRecord ? "|->" : ":");
784
+ return { name, value: parseExpression(), location: fieldLocation };
785
+ };
786
+ const fields = [
787
+ parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
788
+ ...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
789
+ ];
669
790
  expect("]");
670
791
  return { kind, fields, location };
671
792
  }
@@ -703,17 +824,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
703
824
  expect("]");
704
825
  return { kind: "Except", function: expression, updates, location };
705
826
  }
827
+ if (!isTlapm && !isAt("]_")) {
828
+ const args = [expression];
829
+ while (accept(",")) args.push(parseExpression());
830
+ expect("]");
831
+ return { kind: "Bracket", args, location };
832
+ }
706
833
  expect("]_");
707
- return {
708
- kind: "BoxAction",
709
- action: expression,
710
- subscript: parseSubscript(),
711
- location,
712
- };
834
+ return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
713
835
  });
714
836
 
837
+ // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
838
+ const parseSetMembership = (): Expression => {
839
+ const start = cursor.index;
840
+ const element = withRange(parsePrefix(), start);
841
+ const location = getLocation(advance());
842
+ return withRange(
843
+ { kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
844
+ start,
845
+ );
846
+ };
847
+
715
848
  const parseBrace = (start: Token): Expression =>
716
- withoutOffside(() => {
849
+ withBracketLayout(() => {
717
850
  const location = getLocation(start);
718
851
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
719
852
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
@@ -728,7 +861,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
728
861
  }
729
862
  cursor.index = checkpoint;
730
863
  }
731
- const first = parseExpression();
864
+ const first =
865
+ peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
732
866
  if (accept(":")) {
733
867
  const bounds = parseBounds();
734
868
  expect("}");
@@ -742,7 +876,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
742
876
 
743
877
  const parseTuple = (start: Token): Expression => {
744
878
  const location = getLocation(start);
745
- const elements = withoutOffside(() =>
879
+ const elements = withBracketLayout(() =>
746
880
  isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
747
881
  );
748
882
  if (accept(">>_")) {
@@ -763,12 +897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
763
897
  expect(":");
764
898
  return { kind, bounds, body: parseExpression(), location };
765
899
  }
766
- const names = parseSeparated(expectIdentifier);
900
+ const names = getNames(parseSeparated(expectName));
767
901
  expect(":");
768
902
  return {
769
903
  kind: "UnboundedQuantifier",
770
904
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
771
- names,
905
+ names: names.names,
906
+ nameRanges: names.ranges,
772
907
  body: parseExpression(),
773
908
  isTemporal,
774
909
  location,
@@ -822,8 +957,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
822
957
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
823
958
  };
824
959
 
825
- const parsePrimary = (): Expression => {
826
- const isFactStart = cursor.index === factStart.index;
960
+ const parsePrimary = (preceding?: Precedence): Expression => {
961
+ const start = cursor.index;
962
+ const isFactStart = start === factStart.index;
827
963
  const token = advance();
828
964
  const location = getLocation(token);
829
965
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -847,53 +983,116 @@ const parse = (source: string, isTlapm: boolean): Module => {
847
983
  if (token.kind === "Identifier" || isStep) {
848
984
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
849
985
  if (isLabelAhead()) {
850
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
851
- if (params.length > 0) expect(")");
986
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
987
+ if (params.names.length > 0) expect(")");
852
988
  expect("::");
853
- return { kind: "Label", name, params, body: parseExpression(), location };
989
+ const body = parseExpression();
990
+ // TLAplusParser.labelDoesNotChangeParse.
991
+ if (
992
+ !isTlapm &&
993
+ preceding !== undefined &&
994
+ operators.last !== undefined &&
995
+ isOperatorExpression(body) &&
996
+ preceding.high >= operators.last.low
997
+ ) {
998
+ throw createError("Removing the label would change expression parsing", token);
999
+ }
1000
+ return {
1001
+ kind: "Label",
1002
+ name,
1003
+ params: params.names,
1004
+ paramRanges: params.ranges,
1005
+ body,
1006
+ location,
1007
+ };
854
1008
  }
855
1009
  const hasArguments = isAt("(");
1010
+ if (name === "@" && !hasArguments && !isSelectorAhead())
1011
+ return { kind: "ExceptAt", location };
856
1012
  const args = hasArguments ? parseArguments() : [];
1013
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
857
1014
  if (!isSelectorAhead()) {
858
1015
  return hasArguments
859
- ? { kind: "Apply", operator: name, args, location }
1016
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
860
1017
  : { kind: "Identifier", name, location };
861
1018
  }
862
1019
  const instance = openModules
863
1020
  .flatMap((module) => module.instances)
864
1021
  .find((candidate) => candidate.name === name);
865
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
1022
+ const isMissingArguments =
1023
+ instance !== undefined &&
1024
+ mode === "tlc" &&
1025
+ !isFactStart &&
1026
+ instance.params.length !== args.length;
1027
+ if (isMissingArguments && args.length > 0) {
866
1028
  throw createError(
867
1029
  `The operator ${name} requires ${instance.params.length} arguments`,
868
1030
  token,
869
1031
  );
870
1032
  }
871
1033
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1034
+ const selectorRanges = [getRange(start, start)];
872
1035
  while (isSelectorAhead()) {
873
1036
  advance();
1037
+ selectorRanges.push(getRange(cursor.index, cursor.index));
1038
+ lastArguments.range = undefined;
874
1039
  selectors.push(parseSelector());
1040
+ argumentRanges.push(lastArguments.range);
875
1041
  }
876
1042
  const names = selectors.flatMap((selector) =>
877
1043
  selector.kind === "Name" ? [selector.name] : [],
878
1044
  );
879
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
880
1045
  const operator = names.join("!");
881
1046
  const qualifiedArgs = selectors.flatMap((selector) =>
882
1047
  "args" in selector ? selector.args : [],
883
1048
  );
884
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
885
1049
  const argumentCounts = selectors.map((selector) =>
886
1050
  "args" in selector ? selector.args.length : 0,
887
1051
  );
888
- return argumentCounts.slice(0, -1).some((count) => count > 0)
889
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
890
- : { kind: "Apply", operator, args: qualifiedArgs, location };
1052
+ const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
1053
+ const reference: Expression =
1054
+ names.length < selectors.length
1055
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1056
+ : qualifiedArgs.length === 0
1057
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1058
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
1059
+ ? {
1060
+ kind: "Apply",
1061
+ operator,
1062
+ args: qualifiedArgs,
1063
+ argumentCounts,
1064
+ argumentsRange,
1065
+ selectorRanges,
1066
+ argumentRanges,
1067
+ location,
1068
+ }
1069
+ : {
1070
+ kind: "Apply",
1071
+ operator,
1072
+ args: qualifiedArgs,
1073
+ argumentsRange,
1074
+ selectorRanges,
1075
+ argumentRanges,
1076
+ location,
1077
+ };
1078
+ if (isMissingArguments) {
1079
+ missingArguments.set(reference, {
1080
+ location: formatRange(getRange(start)),
1081
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
1082
+ });
1083
+ }
1084
+ return reference;
891
1085
  }
892
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
1086
+ // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1087
+ if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
893
1088
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
1089
+ }
1090
+ if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1091
+ return { kind: "Identifier", name: token.text, location };
1092
+ }
894
1093
  switch (token.text) {
895
1094
  case "(": {
896
- const expression = withoutOffside(() => parseExpression());
1095
+ const expression = withBracketLayout(() => parseExpression());
897
1096
  expect(")");
898
1097
  return expression;
899
1098
  }
@@ -949,7 +1148,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
949
1148
  case "WF_":
950
1149
  case "SF_": {
951
1150
  const subscript = parseSubscript();
952
- const action = withoutOffside(() => {
1151
+ const action = withBracketLayout(() => {
953
1152
  expect("(");
954
1153
  const expression = parseExpression();
955
1154
  expect(")");
@@ -966,7 +1165,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
966
1165
  throw createUnexpected(`Unexpected ${describe(token)}`, token);
967
1166
  };
968
1167
 
1168
+ // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1169
+ // prefix operator.
1170
+ const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1171
+ operators.last = operators.preceding = { low, high };
1172
+ const operand = parseExpression(high + 1);
1173
+ const next = peek();
1174
+ const nextHigh =
1175
+ next.text === "'" || POSTFIX_OPERATORS.has(next.text)
1176
+ ? POSTFIX_PRECEDENCE.high
1177
+ : INFIX_OPERATORS.get(next.text)?.high;
1178
+ if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1179
+ throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
1180
+ }
1181
+ return operand;
1182
+ };
1183
+
969
1184
  const parsePrefix = (): Expression => {
1185
+ const preceding = operators.preceding;
1186
+ operators.preceding = undefined;
970
1187
  const token = peek();
971
1188
  const location = getLocation(token);
972
1189
  if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
@@ -976,35 +1193,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
976
1193
  switch (token.text) {
977
1194
  case "~":
978
1195
  advance();
979
- return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
1196
+ return {
1197
+ kind: "Apply",
1198
+ operator: "~",
1199
+ args: [parsePrefixOperand(token, 4, 4)],
1200
+ location,
1201
+ };
980
1202
  case "-":
981
1203
  advance();
982
1204
  return {
983
1205
  kind: "Apply",
984
1206
  operator: "-.",
985
- args: [parseExpression(NEGATION_PRECEDENCE + 1)],
1207
+ args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
986
1208
  location,
987
1209
  };
988
1210
  case "[]":
989
1211
  advance();
990
- return { kind: "Always", expression: parseExpression(16), location };
1212
+ return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
991
1213
  case "<>":
992
1214
  advance();
993
- return { kind: "Eventually", expression: parseExpression(16), location };
1215
+ return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
994
1216
  case "ENABLED":
995
1217
  advance();
996
- return { kind: "Enabled", expression: parseExpression(16), location };
1218
+ return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
997
1219
  case "UNCHANGED":
998
1220
  advance();
999
- return { kind: "Unchanged", expression: parseExpression(16), location };
1221
+ return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
1000
1222
  case "SUBSET":
1001
1223
  case "UNION":
1002
1224
  case "DOMAIN":
1003
1225
  advance();
1004
- return { kind: "Apply", operator: token.text, args: [parseExpression(9)], location };
1226
+ return {
1227
+ kind: "Apply",
1228
+ operator: token.text,
1229
+ // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1230
+ args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1231
+ location,
1232
+ };
1005
1233
  }
1006
1234
  }
1007
- return parsePrimary();
1235
+ return parsePrimary(preceding);
1008
1236
  };
1009
1237
 
1010
1238
  const parseExpression = (minimumPrecedence = 0): Expression => {
@@ -1020,11 +1248,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1020
1248
  const location = getLocation(token);
1021
1249
  if (token.text === "'" && minimumPrecedence <= 15) {
1022
1250
  advance();
1251
+ operators.last = POSTFIX_PRECEDENCE;
1023
1252
  state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1024
1253
  continue;
1025
1254
  }
1026
1255
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1027
1256
  advance();
1257
+ operators.last = POSTFIX_PRECEDENCE;
1028
1258
  state.left = withRange(
1029
1259
  { kind: "Apply", operator: token.text, args: [state.left], location },
1030
1260
  start,
@@ -1033,8 +1263,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
1033
1263
  }
1034
1264
  if (token.text === "[") {
1035
1265
  advance();
1036
- const args = withoutOffside(() => parseSeparated(() => parseExpression()));
1037
- withoutOffside(() => expect("]"));
1266
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1267
+ withBracketLayout(() => expect("]"));
1268
+ operators.last = FUNCTION_PRECEDENCE;
1038
1269
  state.left = withRange(
1039
1270
  { kind: "FunctionApplication", function: state.left, args, location },
1040
1271
  start,
@@ -1065,21 +1296,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
1065
1296
  if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1066
1297
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1067
1298
  }
1299
+ operators.last = operators.preceding = infix;
1068
1300
  const right = parseExpression(infix.high + 1);
1069
1301
  const left = state.left;
1070
1302
  if (token.text === "/\\" || token.text === "\\/") {
1071
1303
  const kind = token.text === "/\\" ? "And" : "Or";
1304
+ const prefixEnds = [getRange(start).end];
1072
1305
  state.left =
1073
1306
  left.kind === kind && previous?.text === token.text
1074
- ? { ...left, operands: [...left.operands, right] }
1075
- : { kind, operands: [left, right], location };
1307
+ ? {
1308
+ ...left,
1309
+ operands: [...left.operands, right],
1310
+ prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
1311
+ }
1312
+ : { kind, operands: [left, right], prefixEnds, location };
1076
1313
  } else if (token.text === "\\X") {
1077
1314
  state.left =
1078
1315
  left.kind === "Product" && previous?.text === "\\X"
1079
1316
  ? { ...left, factors: [...left.factors, right] }
1080
1317
  : { kind: "Product", factors: [left, right], location };
1081
1318
  } else {
1082
- state.left = { kind: "Apply", operator: token.text, args: [left, right], location };
1319
+ state.left = {
1320
+ kind: "Apply",
1321
+ operator: token.text,
1322
+ spelling: token.spelling,
1323
+ args: [left, right],
1324
+ location,
1325
+ };
1083
1326
  }
1084
1327
  state.left = withRange(state.left, start);
1085
1328
  state.previous = { ...infix, text: token.text };
@@ -1099,7 +1342,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1099
1342
  const body = parseExpression();
1100
1343
  const last = tokens[cursor.index - 1];
1101
1344
  const end = { line: last.line, column: last.endColumn };
1102
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1345
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1103
1346
  };
1104
1347
 
1105
1348
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1107,6 +1350,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1107
1350
  return withRange(parseDefinitionUnit(isLocal), start);
1108
1351
  };
1109
1352
 
1353
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1354
+ name: named.name,
1355
+ arity: 0,
1356
+ location: {
1357
+ line: named.range.start.line,
1358
+ column: named.range.start.column,
1359
+ range: named.range,
1360
+ },
1361
+ });
1362
+
1110
1363
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1111
1364
  const token = peek();
1112
1365
  const location = getLocation(token);
@@ -1126,28 +1379,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1126
1379
  peek(2).kind === "Identifier" &&
1127
1380
  isAt("==", 3)
1128
1381
  ) {
1129
- const left = expectIdentifier();
1382
+ const left = expectName();
1130
1383
  const operator = advance().text;
1131
- const right = expectIdentifier();
1384
+ const right = expectName();
1132
1385
  expect("==");
1133
- return parseOperator(operator, [
1134
- { name: left, arity: 0 },
1135
- { name: right, arity: 0 },
1136
- ]);
1386
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1137
1387
  }
1138
- const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1388
+ const isPrefixDefinition =
1389
+ token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1139
1390
  const isPostfixDefinition =
1140
1391
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1141
1392
  if (isPrefixDefinition || isPostfixDefinition) {
1142
1393
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1143
- cursor.index += isPrefixDefinition ? 1 : 2;
1144
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1394
+ if (isPrefixDefinition) advance();
1395
+ const param = expectName();
1396
+ if (isPostfixDefinition) advance();
1145
1397
  expect("==");
1146
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1398
+ return parseOperator(operator, [toParam(param)]);
1147
1399
  }
1148
1400
  const name = expectIdentifier();
1149
1401
  if (accept("[")) {
1150
- const bounds = withoutOffside(() => parseBounds());
1402
+ const bounds = withBracketLayout(() => parseBounds());
1151
1403
  expect("]");
1152
1404
  expect("==");
1153
1405
  return {
@@ -1178,12 +1430,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1178
1430
  ): Instance => {
1179
1431
  const start = cursor.index;
1180
1432
  expect("INSTANCE");
1181
- const module = expectIdentifier();
1433
+ const module = expectModuleName();
1182
1434
  const substitutions = accept("WITH")
1183
1435
  ? parseSeparated(() => {
1184
- const target = advance().text;
1436
+ const targetStart = cursor.index;
1437
+ const target = expectSubstitutionTarget();
1438
+ const nameRange = getRange(targetStart);
1185
1439
  expect("<-");
1186
- return { name: target, expression: parseArgument() };
1440
+ return { name: target, nameRange, expression: parseArgument() };
1187
1441
  })
1188
1442
  : [];
1189
1443
  return {
@@ -1199,6 +1453,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1199
1453
  };
1200
1454
 
1201
1455
  const parseDeclaration = (): Declaration => {
1456
+ const start = cursor.index;
1457
+ return withRange(parseDeclarationUnit(), start);
1458
+ };
1459
+
1460
+ const parseDeclarationUnit = (): Declaration => {
1202
1461
  const location = getLocation(peek());
1203
1462
  if (isAt("_") && peek(1).kind === "Operator") {
1204
1463
  advance();
@@ -1244,7 +1503,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1244
1503
  const token = peek();
1245
1504
  const next = peek(1);
1246
1505
  if (token.kind !== "Identifier") {
1247
- return isAt("-.") && next.kind === "Identifier" && isAt("==", 2);
1506
+ return (
1507
+ token.kind !== "String" &&
1508
+ DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
1509
+ next.kind === "Identifier" &&
1510
+ isAt("==", 2)
1511
+ );
1248
1512
  }
1249
1513
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1250
1514
  if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
@@ -1398,7 +1662,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1398
1662
  const parseDefs = () => {
1399
1663
  if (isTlapm) return parseSeparated(parseDefinable);
1400
1664
  const defs = parseSeparated(parseFact);
1401
- const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1665
+ const invalid =
1666
+ mode === "tlc"
1667
+ ? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
1668
+ : undefined;
1402
1669
  if (invalid) {
1403
1670
  throw createError("DEF clause entry should describe a defined operator", invalid.location);
1404
1671
  }
@@ -1423,11 +1690,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1690
  location: getLocation(keyword),
1424
1691
  };
1425
1692
  const invalid =
1426
- use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1427
- if (invalid) {
1428
- throw createError(
1429
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1430
- invalid.location,
1693
+ use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1694
+ if (invalid.length > 0) {
1695
+ throw createSanyError(
1696
+ invalid.map((fact) => ({
1697
+ location: formatRange(fact.location.range ?? getRange(start)),
1698
+ message:
1699
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1700
+ })),
1431
1701
  );
1432
1702
  }
1433
1703
  return withRange(use, start);
@@ -1443,10 +1713,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1443
1713
  }
1444
1714
  if (level !== undefined) advance();
1445
1715
  if (level === "Variable") {
1446
- const name = expectIdentifier();
1447
- declareProofSymbol(name);
1716
+ const named = expectName();
1717
+ declareProofSymbol(named.name);
1448
1718
  return withRange(
1449
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1719
+ {
1720
+ kind: "NewSymbol",
1721
+ name: named.name,
1722
+ arity: 0,
1723
+ level,
1724
+ set: undefined,
1725
+ nameRange: named.range,
1726
+ location,
1727
+ },
1450
1728
  start,
1451
1729
  );
1452
1730
  }
@@ -1464,6 +1742,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1464
1742
  arity: declaration.arity,
1465
1743
  level: level ?? "Constant",
1466
1744
  set: isBounded ? parseExpression() : undefined,
1745
+ nameRange: declaration.location.range,
1467
1746
  location,
1468
1747
  },
1469
1748
  start,
@@ -1506,7 +1785,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1506
1785
  const token = peek();
1507
1786
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1508
1787
  if (isAt("INSTANCE")) {
1509
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1788
+ const start = cursor.index;
1789
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1510
1790
  }
1511
1791
  if (isAt("DEFINE") || isDefinitionStart()) {
1512
1792
  const start = cursor.index;
@@ -1519,21 +1799,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1519
1799
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1520
1800
  if (accept("TAKE")) {
1521
1801
  const isBounded = isBoundAhead();
1522
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1802
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1523
1803
  const bounds = isBounded ? parseBounds() : [];
1524
- declareProofSymbols(names, bounds);
1525
- return { kind: "Take", names, bounds };
1804
+ declareProofSymbols(names.names, bounds);
1805
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1526
1806
  }
1527
1807
  if (accept("WITNESS")) {
1528
1808
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1529
1809
  }
1530
1810
  if (accept("PICK")) {
1531
1811
  const isNames = isNameListAhead(":");
1532
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1812
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1533
1813
  const bounds = isNames ? [] : parseBounds();
1534
- declareProofSymbols(names, bounds);
1814
+ declareProofSymbols(names.names, bounds);
1535
1815
  expect(":");
1536
- return { kind: "Pick", names, bounds, body: parseExpression() };
1816
+ return {
1817
+ kind: "Pick",
1818
+ names: names.names,
1819
+ nameRanges: names.ranges,
1820
+ bounds,
1821
+ body: parseExpression(),
1822
+ };
1537
1823
  }
1538
1824
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1539
1825
  return {
@@ -1563,10 +1849,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1849
  const parseStep = (): ProofStep => {
1564
1850
  const start = cursor.index;
1565
1851
  const step = parseStepStart(isAt("QED", 1));
1852
+ const statementStart = cursor.index;
1566
1853
  const scope = new Set<string>();
1567
1854
  proofScopes.push(scope);
1568
1855
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
1856
  const head = getRange(start);
1857
+ const statement = getRange(statementStart);
1570
1858
  const hasProof = isProofStart(peek());
1571
1859
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
1860
  throw createError("Proof of a step that does not take a proof");
@@ -1574,7 +1862,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1862
  const proof = hasProof ? parseProof() : undefined;
1575
1863
  proofScopes.pop();
1576
1864
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
- return withRange({ ...step, body, head, proof }, start);
1865
+ return withRange({ ...step, body, head, statement, proof }, start);
1578
1866
  };
1579
1867
 
1580
1868
  const parseProofBody = (): Proof => {
@@ -1609,6 +1897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1609
1897
  return withRange(proof, start);
1610
1898
  };
1611
1899
 
1900
+ // SANY binds the name of a theorem or assumption that a module already defines to the first
1901
+ // definition, warning of the second.
1902
+ const addFactDefinition = (module: Module, definition: Definition) => {
1903
+ if (module.definitions.some((other) => other.name === definition.name)) return;
1904
+ module.definitions.push(definition);
1905
+ };
1906
+
1612
1907
  const parseTheorem = (module: Module) => {
1613
1908
  const start = cursor.index;
1614
1909
  const keyword = advance();
@@ -1630,7 +1925,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1630
1925
  module.theorems.push(theorem);
1631
1926
  module.units.push({ kind: "Theorem", theorem });
1632
1927
  if (name === undefined || statement.kind === "AssumeProve") return;
1633
- module.definitions.push(
1928
+ addFactDefinition(
1929
+ module,
1634
1930
  withRange(
1635
1931
  {
1636
1932
  kind: "Operator",
@@ -1667,7 +1963,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1667
1963
  const parseModuleBody = (): Module => {
1668
1964
  const location = getLocation(expect("----"));
1669
1965
  expect("MODULE");
1670
- const name = expectIdentifier();
1966
+ const name = expectModuleName();
1671
1967
  expect("----");
1672
1968
  const module: Module = {
1673
1969
  name,
@@ -1689,7 +1985,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1689
1985
  if (accept("EXTENDS")) {
1690
1986
  parseSeparated(() => {
1691
1987
  const start = cursor.index;
1692
- module.extends.push(expectIdentifier());
1988
+ module.extends.push(expectModuleName());
1693
1989
  module.extendsRanges.push(getRange(start));
1694
1990
  });
1695
1991
  }
@@ -1697,14 +1993,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1697
1993
  const tokenIndex = cursor.index;
1698
1994
  unitStart.index = tokenIndex;
1699
1995
  const token = peek();
1700
- if (token.kind === "ModuleEnd" || token.kind === "End") {
1701
- if (token.kind === "ModuleEnd") {
1702
- location.range = {
1703
- module: name,
1704
- start: { line: location.line, column: location.column },
1705
- end: { line: token.line, column: token.endColumn },
1706
- };
1707
- }
1996
+ if (token.kind === "ModuleEnd") {
1997
+ location.range = {
1998
+ module: openModules[0].name,
1999
+ start: { line: location.line, column: location.column },
2000
+ end: { line: token.line, column: token.endColumn },
2001
+ };
2002
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1708
2003
  openModules.pop();
1709
2004
  recursiveScopes.pop();
1710
2005
  return module;
@@ -1723,22 +2018,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
1723
2018
  switch (token.text) {
1724
2019
  case "CONSTANT":
1725
2020
  case "CONSTANTS": {
1726
- advance();
2021
+ const unitLocation = getLocation(advance());
1727
2022
  const declarations = parseDeclarations();
1728
2023
  module.constants.push(...declarations);
1729
- module.units.push({ kind: "Constants", declarations });
2024
+ unitLocation.range = getRange(tokenIndex);
2025
+ module.units.push({ kind: "Constants", declarations, location: unitLocation });
1730
2026
  continue;
1731
2027
  }
1732
2028
  case "VARIABLE":
1733
2029
  case "VARIABLES": {
1734
- advance();
2030
+ const unitLocation = getLocation(advance());
1735
2031
  const declarations = parseSeparated(() => {
1736
2032
  const start = cursor.index;
1737
2033
  const location = getLocation(peek());
1738
2034
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1739
2035
  });
1740
2036
  module.variables.push(...declarations);
1741
- module.units.push({ kind: "Variables", declarations });
2037
+ unitLocation.range = getRange(tokenIndex);
2038
+ module.units.push({ kind: "Variables", declarations, location: unitLocation });
1742
2039
  continue;
1743
2040
  }
1744
2041
  case "ASSUME":
@@ -1748,7 +2045,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1748
2045
  module.assumptions.push(assumption);
1749
2046
  module.units.push({ kind: "Assumption", assumption });
1750
2047
  if (assumption.name !== undefined) {
1751
- module.definitions.push({
2048
+ addFactDefinition(module, {
1752
2049
  kind: "Operator",
1753
2050
  name: assumption.name,
1754
2051
  params: [],
@@ -1762,6 +2059,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1762
2059
  }
1763
2060
  case "USE":
1764
2061
  case "HIDE": {
2062
+ // SANY's module body takes a USE only when ONLY doesn't follow it.
2063
+ if (isAt("USE") && isAt("ONLY", 1)) {
2064
+ throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
2065
+ }
1765
2066
  const use = parseUseOrHide();
1766
2067
  module.uses.push(use);
1767
2068
  module.units.push({ kind: "Use", use });
@@ -1786,7 +2087,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1786
2087
  );
1787
2088
  continue;
1788
2089
  }
1789
- addDefinitionUnit(module, parseDefinition(true));
2090
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1790
2091
  continue;
1791
2092
  }
1792
2093
  }
@@ -1808,9 +2109,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1808
2109
  module.units.push({ kind: "Definition", definition: unit });
1809
2110
  };
1810
2111
 
1811
- return parseModuleBody();
2112
+ const module = parseModuleBody();
2113
+ // SANY's lexer marks where each definition begins before its parser runs, which can split a
2114
+ // module's name from a keyword that the parser would take for one.
2115
+ const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2116
+ if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2117
+ return module;
1812
2118
  };
1813
2119
 
1814
- export const parseModule = (source: string) => parse(source, false);
2120
+ export const parseModule = (source: string) => parse(source, "tlc");
2121
+
2122
+ export const parseTlapmModule = (source: string) => parse(source, "tlapm");
1815
2123
 
1816
- export const parseTlapmModule = (source: string) => parse(source, true);
2124
+ export const parseSanyModule = (source: string) => parse(source, "sany");