tla-ts 0.0.3-dev.949cce6 → 0.0.3-dev.9b15ea6

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 (80) 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 +146 -8
  5. package/dist/index.d.ts +146 -8
  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 +86 -28
  14. package/dist/transfer.js +86 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +48 -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 +1352 -215
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -18
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +451 -129
  26. package/src/errors.ts +217 -3
  27. package/src/evaluate.ts +724 -117
  28. package/src/exit-codes.ts +5 -4
  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 +14 -1
  34. package/src/live-check.ts +211 -89
  35. package/src/liveness.ts +192 -65
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +8 -3
  39. package/src/parallel.ts +56 -5
  40. package/src/parser.ts +423 -126
  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 +1363 -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 +162 -114
  69. package/src/symmetry.ts +180 -52
  70. package/src/tableau.ts +4 -2
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +46 -18
  74. package/src/tlc-order.ts +56 -6
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/value-sources.ts +29 -0
  80. package/src/values.ts +684 -204
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" ||
@@ -623,19 +710,38 @@ const parse = (source: string, isTlapm: boolean): Module => {
623
710
  if (isAt("LAMBDA")) {
624
711
  const start = cursor.index;
625
712
  advance();
626
- const params = parseSeparated(expectIdentifier);
713
+ const params = getNames(parseSeparated(expectName));
627
714
  expect(":");
628
715
  const body = parseExpression();
629
- return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
716
+ return withRange(
717
+ {
718
+ kind: "Lambda",
719
+ params: params.names,
720
+ paramRanges: params.ranges,
721
+ body,
722
+ location: getLocation(token),
723
+ },
724
+ start,
725
+ );
630
726
  }
631
- return parseExpression();
727
+ const argument = parseExpression();
728
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
729
+ // I!Op, as that operator, which takes the arguments of the instance too.
730
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
731
+ return argument;
632
732
  };
633
733
 
734
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
735
+
736
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
737
+
634
738
  const parseArguments = () =>
635
- withoutOffside(() => {
739
+ withBracketLayout(() => {
740
+ const start = cursor.index;
636
741
  expect("(");
637
742
  const args = parseSeparated(parseArgument);
638
743
  expect(")");
744
+ lastArguments.range = getRange(start);
639
745
  return args;
640
746
  });
641
747
 
@@ -657,15 +763,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
657
763
  };
658
764
 
659
765
  const parseBracket = (start: Token): Expression =>
660
- withoutOffside(() => {
766
+ withBracketLayout(() => {
661
767
  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
- });
768
+ // SANY takes a keyword for the first field of a record, as it does for a selected field.
769
+ const isIdentifier = peek().kind === "Identifier";
770
+ const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
771
+ if (isRecord || (isIdentifier && isAt(":", 1))) {
772
+ const kind = isRecord ? "Record" : "RecordSet";
773
+ const parseField = (readName: () => string) => {
774
+ const fieldStart = cursor.index;
775
+ const fieldLocation = getLocation(peek());
776
+ const name = readName();
777
+ fieldLocation.range = getRange(fieldStart);
778
+ expect(isRecord ? "|->" : ":");
779
+ return { name, value: parseExpression(), location: fieldLocation };
780
+ };
781
+ const fields = [
782
+ parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
783
+ ...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
784
+ ];
669
785
  expect("]");
670
786
  return { kind, fields, location };
671
787
  }
@@ -703,17 +819,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
703
819
  expect("]");
704
820
  return { kind: "Except", function: expression, updates, location };
705
821
  }
822
+ if (!isTlapm && !isAt("]_")) {
823
+ const args = [expression];
824
+ while (accept(",")) args.push(parseExpression());
825
+ expect("]");
826
+ return { kind: "Bracket", args, location };
827
+ }
706
828
  expect("]_");
707
- return {
708
- kind: "BoxAction",
709
- action: expression,
710
- subscript: parseSubscript(),
711
- location,
712
- };
829
+ return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
713
830
  });
714
831
 
832
+ // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
833
+ const parseSetMembership = (): Expression => {
834
+ const start = cursor.index;
835
+ const element = withRange(parsePrefix(), start);
836
+ const location = getLocation(advance());
837
+ return withRange(
838
+ { kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
839
+ start,
840
+ );
841
+ };
842
+
715
843
  const parseBrace = (start: Token): Expression =>
716
- withoutOffside(() => {
844
+ withBracketLayout(() => {
717
845
  const location = getLocation(start);
718
846
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
719
847
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
@@ -728,7 +856,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
728
856
  }
729
857
  cursor.index = checkpoint;
730
858
  }
731
- const first = parseExpression();
859
+ const first =
860
+ peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
732
861
  if (accept(":")) {
733
862
  const bounds = parseBounds();
734
863
  expect("}");
@@ -742,7 +871,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
742
871
 
743
872
  const parseTuple = (start: Token): Expression => {
744
873
  const location = getLocation(start);
745
- const elements = withoutOffside(() =>
874
+ const elements = withBracketLayout(() =>
746
875
  isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
747
876
  );
748
877
  if (accept(">>_")) {
@@ -763,12 +892,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
763
892
  expect(":");
764
893
  return { kind, bounds, body: parseExpression(), location };
765
894
  }
766
- const names = parseSeparated(expectIdentifier);
895
+ const names = getNames(parseSeparated(expectName));
767
896
  expect(":");
768
897
  return {
769
898
  kind: "UnboundedQuantifier",
770
899
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
771
- names,
900
+ names: names.names,
901
+ nameRanges: names.ranges,
772
902
  body: parseExpression(),
773
903
  isTemporal,
774
904
  location,
@@ -822,8 +952,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
822
952
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
823
953
  };
824
954
 
825
- const parsePrimary = (): Expression => {
826
- const isFactStart = cursor.index === factStart.index;
955
+ const parsePrimary = (preceding?: Precedence): Expression => {
956
+ const start = cursor.index;
957
+ const isFactStart = start === factStart.index;
827
958
  const token = advance();
828
959
  const location = getLocation(token);
829
960
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -847,53 +978,116 @@ const parse = (source: string, isTlapm: boolean): Module => {
847
978
  if (token.kind === "Identifier" || isStep) {
848
979
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
849
980
  if (isLabelAhead()) {
850
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
851
- if (params.length > 0) expect(")");
981
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
982
+ if (params.names.length > 0) expect(")");
852
983
  expect("::");
853
- return { kind: "Label", name, params, body: parseExpression(), location };
984
+ const body = parseExpression();
985
+ // TLAplusParser.labelDoesNotChangeParse.
986
+ if (
987
+ !isTlapm &&
988
+ preceding !== undefined &&
989
+ operators.last !== undefined &&
990
+ isOperatorExpression(body) &&
991
+ preceding.high >= operators.last.low
992
+ ) {
993
+ throw createError("Removing the label would change expression parsing", token);
994
+ }
995
+ return {
996
+ kind: "Label",
997
+ name,
998
+ params: params.names,
999
+ paramRanges: params.ranges,
1000
+ body,
1001
+ location,
1002
+ };
854
1003
  }
855
1004
  const hasArguments = isAt("(");
1005
+ if (name === "@" && !hasArguments && !isSelectorAhead())
1006
+ return { kind: "ExceptAt", location };
856
1007
  const args = hasArguments ? parseArguments() : [];
1008
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
857
1009
  if (!isSelectorAhead()) {
858
1010
  return hasArguments
859
- ? { kind: "Apply", operator: name, args, location }
1011
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
860
1012
  : { kind: "Identifier", name, location };
861
1013
  }
862
1014
  const instance = openModules
863
1015
  .flatMap((module) => module.instances)
864
1016
  .find((candidate) => candidate.name === name);
865
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
1017
+ const isMissingArguments =
1018
+ instance !== undefined &&
1019
+ mode === "tlc" &&
1020
+ !isFactStart &&
1021
+ instance.params.length !== args.length;
1022
+ if (isMissingArguments && args.length > 0) {
866
1023
  throw createError(
867
1024
  `The operator ${name} requires ${instance.params.length} arguments`,
868
1025
  token,
869
1026
  );
870
1027
  }
871
1028
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1029
+ const selectorRanges = [getRange(start, start)];
872
1030
  while (isSelectorAhead()) {
873
1031
  advance();
1032
+ selectorRanges.push(getRange(cursor.index, cursor.index));
1033
+ lastArguments.range = undefined;
874
1034
  selectors.push(parseSelector());
1035
+ argumentRanges.push(lastArguments.range);
875
1036
  }
876
1037
  const names = selectors.flatMap((selector) =>
877
1038
  selector.kind === "Name" ? [selector.name] : [],
878
1039
  );
879
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
880
1040
  const operator = names.join("!");
881
1041
  const qualifiedArgs = selectors.flatMap((selector) =>
882
1042
  "args" in selector ? selector.args : [],
883
1043
  );
884
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
885
1044
  const argumentCounts = selectors.map((selector) =>
886
1045
  "args" in selector ? selector.args.length : 0,
887
1046
  );
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 };
1047
+ const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
1048
+ const reference: Expression =
1049
+ names.length < selectors.length
1050
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1051
+ : qualifiedArgs.length === 0
1052
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1053
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
1054
+ ? {
1055
+ kind: "Apply",
1056
+ operator,
1057
+ args: qualifiedArgs,
1058
+ argumentCounts,
1059
+ argumentsRange,
1060
+ selectorRanges,
1061
+ argumentRanges,
1062
+ location,
1063
+ }
1064
+ : {
1065
+ kind: "Apply",
1066
+ operator,
1067
+ args: qualifiedArgs,
1068
+ argumentsRange,
1069
+ selectorRanges,
1070
+ argumentRanges,
1071
+ location,
1072
+ };
1073
+ if (isMissingArguments) {
1074
+ missingArguments.set(reference, {
1075
+ location: formatRange(getRange(start)),
1076
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
1077
+ });
1078
+ }
1079
+ return reference;
891
1080
  }
892
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
1081
+ // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1082
+ if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
893
1083
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
1084
+ }
1085
+ if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1086
+ return { kind: "Identifier", name: token.text, location };
1087
+ }
894
1088
  switch (token.text) {
895
1089
  case "(": {
896
- const expression = withoutOffside(() => parseExpression());
1090
+ const expression = withBracketLayout(() => parseExpression());
897
1091
  expect(")");
898
1092
  return expression;
899
1093
  }
@@ -949,7 +1143,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
949
1143
  case "WF_":
950
1144
  case "SF_": {
951
1145
  const subscript = parseSubscript();
952
- const action = withoutOffside(() => {
1146
+ const action = withBracketLayout(() => {
953
1147
  expect("(");
954
1148
  const expression = parseExpression();
955
1149
  expect(")");
@@ -966,7 +1160,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
966
1160
  throw createUnexpected(`Unexpected ${describe(token)}`, token);
967
1161
  };
968
1162
 
1163
+ // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1164
+ // prefix operator.
1165
+ const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1166
+ operators.last = operators.preceding = { low, high };
1167
+ const operand = parseExpression(high + 1);
1168
+ const next = peek();
1169
+ const nextHigh =
1170
+ next.text === "'" || POSTFIX_OPERATORS.has(next.text)
1171
+ ? POSTFIX_PRECEDENCE.high
1172
+ : INFIX_OPERATORS.get(next.text)?.high;
1173
+ if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1174
+ throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
1175
+ }
1176
+ return operand;
1177
+ };
1178
+
969
1179
  const parsePrefix = (): Expression => {
1180
+ const preceding = operators.preceding;
1181
+ operators.preceding = undefined;
970
1182
  const token = peek();
971
1183
  const location = getLocation(token);
972
1184
  if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
@@ -976,35 +1188,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
976
1188
  switch (token.text) {
977
1189
  case "~":
978
1190
  advance();
979
- return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
1191
+ return {
1192
+ kind: "Apply",
1193
+ operator: "~",
1194
+ args: [parsePrefixOperand(token, 4, 4)],
1195
+ location,
1196
+ };
980
1197
  case "-":
981
1198
  advance();
982
1199
  return {
983
1200
  kind: "Apply",
984
1201
  operator: "-.",
985
- args: [parseExpression(NEGATION_PRECEDENCE + 1)],
1202
+ args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
986
1203
  location,
987
1204
  };
988
1205
  case "[]":
989
1206
  advance();
990
- return { kind: "Always", expression: parseExpression(16), location };
1207
+ return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
991
1208
  case "<>":
992
1209
  advance();
993
- return { kind: "Eventually", expression: parseExpression(16), location };
1210
+ return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
994
1211
  case "ENABLED":
995
1212
  advance();
996
- return { kind: "Enabled", expression: parseExpression(16), location };
1213
+ return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
997
1214
  case "UNCHANGED":
998
1215
  advance();
999
- return { kind: "Unchanged", expression: parseExpression(16), location };
1216
+ return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
1000
1217
  case "SUBSET":
1001
1218
  case "UNION":
1002
1219
  case "DOMAIN":
1003
1220
  advance();
1004
- return { kind: "Apply", operator: token.text, args: [parseExpression(9)], location };
1221
+ return {
1222
+ kind: "Apply",
1223
+ operator: token.text,
1224
+ // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1225
+ args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1226
+ location,
1227
+ };
1005
1228
  }
1006
1229
  }
1007
- return parsePrimary();
1230
+ return parsePrimary(preceding);
1008
1231
  };
1009
1232
 
1010
1233
  const parseExpression = (minimumPrecedence = 0): Expression => {
@@ -1020,11 +1243,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1020
1243
  const location = getLocation(token);
1021
1244
  if (token.text === "'" && minimumPrecedence <= 15) {
1022
1245
  advance();
1246
+ operators.last = POSTFIX_PRECEDENCE;
1023
1247
  state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1024
1248
  continue;
1025
1249
  }
1026
1250
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1027
1251
  advance();
1252
+ operators.last = POSTFIX_PRECEDENCE;
1028
1253
  state.left = withRange(
1029
1254
  { kind: "Apply", operator: token.text, args: [state.left], location },
1030
1255
  start,
@@ -1033,8 +1258,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
1033
1258
  }
1034
1259
  if (token.text === "[") {
1035
1260
  advance();
1036
- const args = withoutOffside(() => parseSeparated(() => parseExpression()));
1037
- withoutOffside(() => expect("]"));
1261
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1262
+ withBracketLayout(() => expect("]"));
1263
+ operators.last = FUNCTION_PRECEDENCE;
1038
1264
  state.left = withRange(
1039
1265
  { kind: "FunctionApplication", function: state.left, args, location },
1040
1266
  start,
@@ -1065,14 +1291,20 @@ const parse = (source: string, isTlapm: boolean): Module => {
1065
1291
  if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1066
1292
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1067
1293
  }
1294
+ operators.last = operators.preceding = infix;
1068
1295
  const right = parseExpression(infix.high + 1);
1069
1296
  const left = state.left;
1070
1297
  if (token.text === "/\\" || token.text === "\\/") {
1071
1298
  const kind = token.text === "/\\" ? "And" : "Or";
1299
+ const prefixEnds = [getRange(start).end];
1072
1300
  state.left =
1073
1301
  left.kind === kind && previous?.text === token.text
1074
- ? { ...left, operands: [...left.operands, right] }
1075
- : { kind, operands: [left, right], location };
1302
+ ? {
1303
+ ...left,
1304
+ operands: [...left.operands, right],
1305
+ prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
1306
+ }
1307
+ : { kind, operands: [left, right], prefixEnds, location };
1076
1308
  } else if (token.text === "\\X") {
1077
1309
  state.left =
1078
1310
  left.kind === "Product" && previous?.text === "\\X"
@@ -1099,7 +1331,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1099
1331
  const body = parseExpression();
1100
1332
  const last = tokens[cursor.index - 1];
1101
1333
  const end = { line: last.line, column: last.endColumn };
1102
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1334
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1103
1335
  };
1104
1336
 
1105
1337
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1107,6 +1339,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1107
1339
  return withRange(parseDefinitionUnit(isLocal), start);
1108
1340
  };
1109
1341
 
1342
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1343
+ name: named.name,
1344
+ arity: 0,
1345
+ location: {
1346
+ line: named.range.start.line,
1347
+ column: named.range.start.column,
1348
+ range: named.range,
1349
+ },
1350
+ });
1351
+
1110
1352
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1111
1353
  const token = peek();
1112
1354
  const location = getLocation(token);
@@ -1126,28 +1368,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1126
1368
  peek(2).kind === "Identifier" &&
1127
1369
  isAt("==", 3)
1128
1370
  ) {
1129
- const left = expectIdentifier();
1371
+ const left = expectName();
1130
1372
  const operator = advance().text;
1131
- const right = expectIdentifier();
1373
+ const right = expectName();
1132
1374
  expect("==");
1133
- return parseOperator(operator, [
1134
- { name: left, arity: 0 },
1135
- { name: right, arity: 0 },
1136
- ]);
1375
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1137
1376
  }
1138
- const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1377
+ const isPrefixDefinition =
1378
+ token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1139
1379
  const isPostfixDefinition =
1140
1380
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1141
1381
  if (isPrefixDefinition || isPostfixDefinition) {
1142
1382
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1143
- cursor.index += isPrefixDefinition ? 1 : 2;
1144
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1383
+ if (isPrefixDefinition) advance();
1384
+ const param = expectName();
1385
+ if (isPostfixDefinition) advance();
1145
1386
  expect("==");
1146
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1387
+ return parseOperator(operator, [toParam(param)]);
1147
1388
  }
1148
1389
  const name = expectIdentifier();
1149
1390
  if (accept("[")) {
1150
- const bounds = withoutOffside(() => parseBounds());
1391
+ const bounds = withBracketLayout(() => parseBounds());
1151
1392
  expect("]");
1152
1393
  expect("==");
1153
1394
  return {
@@ -1178,12 +1419,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1178
1419
  ): Instance => {
1179
1420
  const start = cursor.index;
1180
1421
  expect("INSTANCE");
1181
- const module = expectIdentifier();
1422
+ const module = expectModuleName();
1182
1423
  const substitutions = accept("WITH")
1183
1424
  ? parseSeparated(() => {
1184
- const target = advance().text;
1425
+ const targetStart = cursor.index;
1426
+ const target = expectSubstitutionTarget();
1427
+ const nameRange = getRange(targetStart);
1185
1428
  expect("<-");
1186
- return { name: target, expression: parseArgument() };
1429
+ return { name: target, nameRange, expression: parseArgument() };
1187
1430
  })
1188
1431
  : [];
1189
1432
  return {
@@ -1199,6 +1442,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1199
1442
  };
1200
1443
 
1201
1444
  const parseDeclaration = (): Declaration => {
1445
+ const start = cursor.index;
1446
+ return withRange(parseDeclarationUnit(), start);
1447
+ };
1448
+
1449
+ const parseDeclarationUnit = (): Declaration => {
1202
1450
  const location = getLocation(peek());
1203
1451
  if (isAt("_") && peek(1).kind === "Operator") {
1204
1452
  advance();
@@ -1244,7 +1492,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1244
1492
  const token = peek();
1245
1493
  const next = peek(1);
1246
1494
  if (token.kind !== "Identifier") {
1247
- return isAt("-.") && next.kind === "Identifier" && isAt("==", 2);
1495
+ return (
1496
+ token.kind !== "String" &&
1497
+ DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
1498
+ next.kind === "Identifier" &&
1499
+ isAt("==", 2)
1500
+ );
1248
1501
  }
1249
1502
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1250
1503
  if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
@@ -1398,7 +1651,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1398
1651
  const parseDefs = () => {
1399
1652
  if (isTlapm) return parseSeparated(parseDefinable);
1400
1653
  const defs = parseSeparated(parseFact);
1401
- const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1654
+ const invalid =
1655
+ mode === "tlc"
1656
+ ? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
1657
+ : undefined;
1402
1658
  if (invalid) {
1403
1659
  throw createError("DEF clause entry should describe a defined operator", invalid.location);
1404
1660
  }
@@ -1423,11 +1679,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1679
  location: getLocation(keyword),
1424
1680
  };
1425
1681
  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,
1682
+ use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1683
+ if (invalid.length > 0) {
1684
+ throw createSanyError(
1685
+ invalid.map((fact) => ({
1686
+ location: formatRange(fact.location.range ?? getRange(start)),
1687
+ message:
1688
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1689
+ })),
1431
1690
  );
1432
1691
  }
1433
1692
  return withRange(use, start);
@@ -1443,10 +1702,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1443
1702
  }
1444
1703
  if (level !== undefined) advance();
1445
1704
  if (level === "Variable") {
1446
- const name = expectIdentifier();
1447
- declareProofSymbol(name);
1705
+ const named = expectName();
1706
+ declareProofSymbol(named.name);
1448
1707
  return withRange(
1449
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1708
+ {
1709
+ kind: "NewSymbol",
1710
+ name: named.name,
1711
+ arity: 0,
1712
+ level,
1713
+ set: undefined,
1714
+ nameRange: named.range,
1715
+ location,
1716
+ },
1450
1717
  start,
1451
1718
  );
1452
1719
  }
@@ -1464,6 +1731,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1464
1731
  arity: declaration.arity,
1465
1732
  level: level ?? "Constant",
1466
1733
  set: isBounded ? parseExpression() : undefined,
1734
+ nameRange: declaration.location.range,
1467
1735
  location,
1468
1736
  },
1469
1737
  start,
@@ -1506,7 +1774,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1506
1774
  const token = peek();
1507
1775
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1508
1776
  if (isAt("INSTANCE")) {
1509
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1777
+ const start = cursor.index;
1778
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1510
1779
  }
1511
1780
  if (isAt("DEFINE") || isDefinitionStart()) {
1512
1781
  const start = cursor.index;
@@ -1519,21 +1788,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1519
1788
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1520
1789
  if (accept("TAKE")) {
1521
1790
  const isBounded = isBoundAhead();
1522
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1791
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1523
1792
  const bounds = isBounded ? parseBounds() : [];
1524
- declareProofSymbols(names, bounds);
1525
- return { kind: "Take", names, bounds };
1793
+ declareProofSymbols(names.names, bounds);
1794
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1526
1795
  }
1527
1796
  if (accept("WITNESS")) {
1528
1797
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1529
1798
  }
1530
1799
  if (accept("PICK")) {
1531
1800
  const isNames = isNameListAhead(":");
1532
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1801
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1533
1802
  const bounds = isNames ? [] : parseBounds();
1534
- declareProofSymbols(names, bounds);
1803
+ declareProofSymbols(names.names, bounds);
1535
1804
  expect(":");
1536
- return { kind: "Pick", names, bounds, body: parseExpression() };
1805
+ return {
1806
+ kind: "Pick",
1807
+ names: names.names,
1808
+ nameRanges: names.ranges,
1809
+ bounds,
1810
+ body: parseExpression(),
1811
+ };
1537
1812
  }
1538
1813
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1539
1814
  return {
@@ -1563,10 +1838,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1838
  const parseStep = (): ProofStep => {
1564
1839
  const start = cursor.index;
1565
1840
  const step = parseStepStart(isAt("QED", 1));
1841
+ const statementStart = cursor.index;
1566
1842
  const scope = new Set<string>();
1567
1843
  proofScopes.push(scope);
1568
1844
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
1845
  const head = getRange(start);
1846
+ const statement = getRange(statementStart);
1570
1847
  const hasProof = isProofStart(peek());
1571
1848
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
1849
  throw createError("Proof of a step that does not take a proof");
@@ -1574,7 +1851,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1851
  const proof = hasProof ? parseProof() : undefined;
1575
1852
  proofScopes.pop();
1576
1853
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
- return withRange({ ...step, body, head, proof }, start);
1854
+ return withRange({ ...step, body, head, statement, proof }, start);
1578
1855
  };
1579
1856
 
1580
1857
  const parseProofBody = (): Proof => {
@@ -1609,6 +1886,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1609
1886
  return withRange(proof, start);
1610
1887
  };
1611
1888
 
1889
+ // SANY binds the name of a theorem or assumption that a module already defines to the first
1890
+ // definition, warning of the second.
1891
+ const addFactDefinition = (module: Module, definition: Definition) => {
1892
+ if (module.definitions.some((other) => other.name === definition.name)) return;
1893
+ module.definitions.push(definition);
1894
+ };
1895
+
1612
1896
  const parseTheorem = (module: Module) => {
1613
1897
  const start = cursor.index;
1614
1898
  const keyword = advance();
@@ -1630,7 +1914,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1630
1914
  module.theorems.push(theorem);
1631
1915
  module.units.push({ kind: "Theorem", theorem });
1632
1916
  if (name === undefined || statement.kind === "AssumeProve") return;
1633
- module.definitions.push(
1917
+ addFactDefinition(
1918
+ module,
1634
1919
  withRange(
1635
1920
  {
1636
1921
  kind: "Operator",
@@ -1667,7 +1952,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1667
1952
  const parseModuleBody = (): Module => {
1668
1953
  const location = getLocation(expect("----"));
1669
1954
  expect("MODULE");
1670
- const name = expectIdentifier();
1955
+ const name = expectModuleName();
1671
1956
  expect("----");
1672
1957
  const module: Module = {
1673
1958
  name,
@@ -1689,7 +1974,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1689
1974
  if (accept("EXTENDS")) {
1690
1975
  parseSeparated(() => {
1691
1976
  const start = cursor.index;
1692
- module.extends.push(expectIdentifier());
1977
+ module.extends.push(expectModuleName());
1693
1978
  module.extendsRanges.push(getRange(start));
1694
1979
  });
1695
1980
  }
@@ -1697,14 +1982,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
1697
1982
  const tokenIndex = cursor.index;
1698
1983
  unitStart.index = tokenIndex;
1699
1984
  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
- }
1985
+ if (token.kind === "ModuleEnd") {
1986
+ location.range = {
1987
+ module: openModules[0].name,
1988
+ start: { line: location.line, column: location.column },
1989
+ end: { line: token.line, column: token.endColumn },
1990
+ };
1991
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1708
1992
  openModules.pop();
1709
1993
  recursiveScopes.pop();
1710
1994
  return module;
@@ -1723,22 +2007,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
1723
2007
  switch (token.text) {
1724
2008
  case "CONSTANT":
1725
2009
  case "CONSTANTS": {
1726
- advance();
2010
+ const unitLocation = getLocation(advance());
1727
2011
  const declarations = parseDeclarations();
1728
2012
  module.constants.push(...declarations);
1729
- module.units.push({ kind: "Constants", declarations });
2013
+ unitLocation.range = getRange(tokenIndex);
2014
+ module.units.push({ kind: "Constants", declarations, location: unitLocation });
1730
2015
  continue;
1731
2016
  }
1732
2017
  case "VARIABLE":
1733
2018
  case "VARIABLES": {
1734
- advance();
2019
+ const unitLocation = getLocation(advance());
1735
2020
  const declarations = parseSeparated(() => {
1736
2021
  const start = cursor.index;
1737
2022
  const location = getLocation(peek());
1738
2023
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1739
2024
  });
1740
2025
  module.variables.push(...declarations);
1741
- module.units.push({ kind: "Variables", declarations });
2026
+ unitLocation.range = getRange(tokenIndex);
2027
+ module.units.push({ kind: "Variables", declarations, location: unitLocation });
1742
2028
  continue;
1743
2029
  }
1744
2030
  case "ASSUME":
@@ -1748,7 +2034,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1748
2034
  module.assumptions.push(assumption);
1749
2035
  module.units.push({ kind: "Assumption", assumption });
1750
2036
  if (assumption.name !== undefined) {
1751
- module.definitions.push({
2037
+ addFactDefinition(module, {
1752
2038
  kind: "Operator",
1753
2039
  name: assumption.name,
1754
2040
  params: [],
@@ -1762,6 +2048,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
1762
2048
  }
1763
2049
  case "USE":
1764
2050
  case "HIDE": {
2051
+ // SANY's module body takes a USE only when ONLY doesn't follow it.
2052
+ if (isAt("USE") && isAt("ONLY", 1)) {
2053
+ throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
2054
+ }
1765
2055
  const use = parseUseOrHide();
1766
2056
  module.uses.push(use);
1767
2057
  module.units.push({ kind: "Use", use });
@@ -1786,7 +2076,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1786
2076
  );
1787
2077
  continue;
1788
2078
  }
1789
- addDefinitionUnit(module, parseDefinition(true));
2079
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1790
2080
  continue;
1791
2081
  }
1792
2082
  }
@@ -1808,9 +2098,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1808
2098
  module.units.push({ kind: "Definition", definition: unit });
1809
2099
  };
1810
2100
 
1811
- return parseModuleBody();
2101
+ const module = parseModuleBody();
2102
+ // SANY's lexer marks where each definition begins before its parser runs, which can split a
2103
+ // module's name from a keyword that the parser would take for one.
2104
+ const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2105
+ if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2106
+ return module;
1812
2107
  };
1813
2108
 
1814
- export const parseModule = (source: string) => parse(source, false);
2109
+ export const parseModule = (source: string) => parse(source, "tlc");
2110
+
2111
+ export const parseTlapmModule = (source: string) => parse(source, "tlapm");
1815
2112
 
1816
- export const parseTlapmModule = (source: string) => parse(source, true);
2113
+ export const parseSanyModule = (source: string) => parse(source, "sany");