tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.f16792fed

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 (72) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +34 -0
  3. package/dist/index.d.ts +34 -0
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +48 -48
  8. package/dist/prove.js +29 -29
  9. package/dist/run.cjs +40 -40
  10. package/dist/run.js +31 -31
  11. package/dist/transfer.cjs +36 -36
  12. package/dist/transfer.js +40 -40
  13. package/package.json +1 -1
  14. package/src/ast.ts +0 -5
  15. package/src/bags.ts +2 -5
  16. package/src/builtins.ts +1 -8
  17. package/src/check.ts +128 -193
  18. package/src/cli/options.ts +1 -1
  19. package/src/cli/run.ts +0 -2
  20. package/src/cli/runtime-invariants.ts +8 -7
  21. package/src/cli/shared.ts +0 -4
  22. package/src/cli/tlc-arguments.ts +24 -30
  23. package/src/cli/tlc-usage.ts +2 -3
  24. package/src/coverage.ts +3 -8
  25. package/src/evaluate.ts +26 -102
  26. package/src/java-hash.ts +7 -0
  27. package/src/java-overrides.ts +4 -9
  28. package/src/json-reader.ts +1 -2
  29. package/src/json.ts +4 -11
  30. package/src/levels.ts +7 -15
  31. package/src/lexer.ts +72 -83
  32. package/src/liveness-checker.ts +77 -106
  33. package/src/liveness.ts +10 -34
  34. package/src/parallel.ts +26 -33
  35. package/src/parser.ts +163 -226
  36. package/src/pcal/ast.ts +53 -65
  37. package/src/pcal/parse.ts +144 -191
  38. package/src/pcal/tokenize.ts +91 -115
  39. package/src/pcal/translate.ts +83 -98
  40. package/src/pcal/validate.ts +48 -50
  41. package/src/pretty-print.ts +6 -6
  42. package/src/randomization.ts +1 -4
  43. package/src/resolve.ts +12 -70
  44. package/src/runtime.ts +11 -18
  45. package/src/sany/builtins.ts +1 -3
  46. package/src/sany/generator.ts +74 -127
  47. package/src/sany/levels.ts +5 -9
  48. package/src/sany/lexer.ts +40 -44
  49. package/src/sany/linter.ts +0 -1
  50. package/src/sany/nodes.ts +4 -10
  51. package/src/search.ts +1 -1
  52. package/src/simulate.ts +50 -59
  53. package/src/spec-description.ts +23 -34
  54. package/src/standard-definitions.ts +0 -3
  55. package/src/standard-modules.ts +1 -12
  56. package/src/state-graph.ts +3 -15
  57. package/src/subexpressions.ts +38 -47
  58. package/src/successors.ts +14 -35
  59. package/src/symmetry.ts +4 -19
  60. package/src/tlc/fingerprint.ts +15 -21
  61. package/src/tlc/jar-data.ts +0 -1
  62. package/src/tlc/messages.ts +2 -21
  63. package/src/tlc/order.ts +22 -47
  64. package/src/tlc/random.ts +18 -30
  65. package/src/tlc/replay.ts +13 -33
  66. package/src/tlc/summary.ts +36 -45
  67. package/src/tlc/tlc-operators.ts +4 -14
  68. package/src/tlc/tlcext-operators.ts +11 -15
  69. package/src/tlc/trace-spec.ts +4 -9
  70. package/src/tlc/value-stream.ts +9 -13
  71. package/src/tlc/version.ts +0 -1
  72. package/src/values.ts +74 -186
package/src/parser.ts CHANGED
@@ -35,14 +35,8 @@ interface InfixOperator extends Precedence {
35
35
  isAssociative: boolean;
36
36
  }
37
37
 
38
- interface OperatorContext {
39
- last?: Precedence;
40
- preceding?: Precedence;
41
- }
42
-
43
- // SANY's precedences of a postfix operator and of the "[" of a function application.
44
- const POSTFIX_PRECEDENCE: Precedence = { low: 15, high: 15 };
45
- const FUNCTION_PRECEDENCE: Precedence = { low: 16, high: 16 };
38
+ const POSTFIX_PRECEDENCE = { low: 15, high: 15 };
39
+ const FUNCTION_PRECEDENCE = { low: 16, high: 16 };
46
40
 
47
41
  const createInfix = (low: number, high: number, isAssociative: boolean) => ({
48
42
  low,
@@ -262,14 +256,16 @@ interface Point {
262
256
  column: number;
263
257
  }
264
258
 
265
- // A location as tlapm's Loc.locus has it, with stop just past the end.
266
259
  interface Span {
267
260
  start: Point;
268
261
  stop: Point;
269
262
  }
270
263
 
271
- // tlapm reads a proof as leaf proofs and steps without their proofs, then nests them. first is the
272
- // index of the first token, before any PROOF.
264
+ interface RangedName {
265
+ name: string;
266
+ range: SourceRange;
267
+ }
268
+
273
269
  type Preproof =
274
270
  | { kind: "Leaf"; proof: Proof; span: Span; first: number; end: number }
275
271
  | {
@@ -375,7 +371,6 @@ const isOperatorExpression = (expression: Expression) =>
375
371
  ? INFIX_OPERATORS.has(expression.operator)
376
372
  : POSTFIX_OPERATORS.has(expression.operator)));
377
373
 
378
- // SANY's \land and \lor are infix operators only, not bullets.
379
374
  const isBullet = (token: Token) =>
380
375
  (token.text === "/\\" || token.text === "\\/") &&
381
376
  token.spelling !== "\\land" &&
@@ -405,11 +400,8 @@ const getOperatorApplication = (
405
400
  }
406
401
  };
407
402
 
408
- // MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
409
- // root module's file, from its ---- MODULE line to the next ==== line.
410
403
  export const extractModule = (source: string, name: string) => {
411
404
  const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
412
- // MonolithSpecExtractor.module writes each line it reads, with its own line terminator.
413
405
  const lines = source.replace(/\r?\n$/, "").split(/\r?\n/);
414
406
  const start = lines.findIndex((line) => header.test(line));
415
407
  if (start === -1) return undefined;
@@ -417,8 +409,6 @@ export const extractModule = (source: string, name: string) => {
417
409
  return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
418
410
  };
419
411
 
420
- // The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some
421
- // modules that SANY does not. The sany mode leaves SANY's post-parse checks to the SANY generator.
422
412
  type ParseMode = "tlc" | "tlapm" | "sany";
423
413
 
424
414
  const parse = (
@@ -428,13 +418,13 @@ const parse = (
428
418
  ): Module => {
429
419
  const isTlapm = mode === "tlapm";
430
420
  const tokens = isTlapm ? sourceTokens : sourceTokens.map(readAtAsIdentifier);
431
- const cursor = { index: 0, offsideColumns: [] as number[] };
432
- const operators: OperatorContext = {};
421
+ let cursor = 0;
422
+ const offsideColumns: number[] = [];
423
+ let lastOperator: Precedence | undefined;
424
+ let precedingOperator: Precedence | undefined;
433
425
  const recursiveScopes: Set<string>[] = [];
434
426
  const factNames = new Set<string>();
435
- // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
436
427
  const proofScopes: Set<string>[] = [];
437
- // SANY reports a location in a nested module as one in the module of its file.
438
428
  const openModules: Module[] = [];
439
429
 
440
430
  const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
@@ -449,7 +439,7 @@ const parse = (
449
439
  const tokenEnds: SourceLocation[] = [];
450
440
  const tokenRanges: SourceRange[] = [];
451
441
 
452
- const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
442
+ const getRange = (startIndex: number, endIndex = cursor - 1) => {
453
443
  const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
454
444
  if (cached) return cached;
455
445
  const last = tokens[endIndex];
@@ -464,17 +454,16 @@ const parse = (
464
454
  return range;
465
455
  };
466
456
 
467
- // Each node gets its own location object from getLocation, so the range can be set in place.
468
457
  const withRange = <Node extends { location: SourceLocation }>(node: Node, startIndex: number) => {
469
458
  node.location.range = getRange(startIndex);
470
459
  return node;
471
460
  };
472
461
 
473
- const peekRaw = (offset = 0) => tokens[Math.min(cursor.index + offset, tokens.length - 1)];
462
+ const peekRaw = (offset = 0) => tokens[Math.min(cursor + offset, tokens.length - 1)];
474
463
 
475
464
  const peek = (offset = 0) => {
476
465
  const token = peekRaw(offset);
477
- const offsideColumn = cursor.offsideColumns[cursor.offsideColumns.length - 1];
466
+ const offsideColumn = offsideColumns[offsideColumns.length - 1];
478
467
  if (offsideColumn === undefined || offsideColumn === NO_OFFSIDE || token.kind === "End") {
479
468
  return token;
480
469
  }
@@ -484,20 +473,19 @@ const parse = (
484
473
  const describeToken = (token: Token) =>
485
474
  token.kind === "End" ? "end of input" : JSON.stringify(token.text);
486
475
 
487
- const unitStart = { index: 0 };
476
+ let unitStart = 0;
488
477
 
489
- const getDefinitionEnd = (fallback: { line: number; column: number }) => {
478
+ const getDefinitionEnd = (fallback: Point) => {
490
479
  const equals = tokens
491
- .slice(unitStart.index, cursor.index)
480
+ .slice(unitStart, cursor)
492
481
  .find((token) => token.text === "==" && token.kind !== "String");
493
482
  return equals ? { line: equals.line, column: equals.endColumn + 1 } : fallback;
494
483
  };
495
484
 
496
- // tlapm reads TRUE, FALSE, BOOLEAN, STRING and the other keywords only as keywords.
497
485
  const isTlapmKeyword = (token: Token) =>
498
486
  isTlapm && (token.kind === "Keyword" || BUILTIN_CONSTANTS.has(token.text));
499
487
 
500
- const getPreviousEnd = (position: { line: number; column: number }) => {
488
+ const getPreviousEnd = (position: Point) => {
501
489
  const previous = tokens.findLast(
502
490
  (token) =>
503
491
  token.kind !== "End" &&
@@ -518,11 +506,7 @@ const parse = (
518
506
  return token.kind === "Identifier" ? `identifier ${token.text}` : `operator ${token.text}`;
519
507
  };
520
508
 
521
- const createError = (
522
- message: string,
523
- token: { line: number; column: number } = peekRaw(),
524
- unexpected?: Token,
525
- ) => {
509
+ const createError = (message: string, token: Point = peekRaw(), unexpected?: Token) => {
526
510
  const end = tokens[tokens.length - 1];
527
511
  const isEnd = token.line === end.line && token.column === end.column;
528
512
  return new TlaSyntaxError(
@@ -534,7 +518,7 @@ const parse = (
534
518
 
535
519
  const getFailure = (
536
520
  message: string,
537
- token: { line: number; column: number },
521
+ token: Point,
538
522
  isEnd: boolean,
539
523
  unexpected: Token | undefined,
540
524
  ) => {
@@ -555,7 +539,6 @@ const parse = (
555
539
  const createUnexpected = (message: string, token: Token = peekRaw()) =>
556
540
  createError(message, token, token);
557
541
 
558
- // TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
559
542
  const isFieldName = (token: Token) =>
560
543
  (token.kind === "Identifier" ||
561
544
  token.kind === "Keyword" ||
@@ -570,13 +553,13 @@ const parse = (
570
553
  const advance = () => {
571
554
  const token = peek();
572
555
  if (token.kind === "End") throw createError("Unexpected end of expression");
573
- cursor.index++;
556
+ cursor++;
574
557
  return token;
575
558
  };
576
559
 
577
560
  const accept = (text: string) => {
578
561
  if (!isAt(text)) return false;
579
- cursor.index++;
562
+ cursor++;
580
563
  return true;
581
564
  };
582
565
 
@@ -592,18 +575,18 @@ const parse = (
592
575
  throw createUnexpected(`Expected an identifier but found ${describeToken(token)}`);
593
576
  }
594
577
  if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
595
- cursor.index++;
578
+ cursor++;
596
579
  return token.text;
597
580
  };
598
581
 
599
582
  const expectName = () => {
600
- const start = cursor.index;
583
+ const start = cursor;
601
584
  return { name: expectIdentifier(), range: getRange(start) };
602
585
  };
603
586
 
604
- const getNames = (names: { name: string; range: SourceRange }[]) => ({
605
- names: names.map((item) => item.name),
606
- ranges: names.map((item) => item.range),
587
+ const getNames = (names: RangedName[]) => ({
588
+ names: names.map((named) => named.name),
589
+ ranges: names.map((named) => named.range),
607
590
  });
608
591
 
609
592
  const expectSubstitutionTarget = () => {
@@ -614,25 +597,22 @@ const parse = (
614
597
  INFIX_OPERATORS.has(token.text) ||
615
598
  DEFINABLE_POSTFIX_OPERATORS.has(token.text));
616
599
  if (!isOperator) return expectIdentifier();
617
- cursor.index++;
600
+ cursor++;
618
601
  return token.text;
619
602
  };
620
603
 
621
- // SANY takes a keyword for the name of a module, as it does for a field.
622
- const keywordModuleName = { isUsed: false };
604
+ let isKeywordModuleName = false;
623
605
  const expectModuleName = () => {
624
606
  if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
625
- keywordModuleName.isUsed = true;
607
+ isKeywordModuleName = true;
626
608
  return advance().text;
627
609
  };
628
610
 
629
- // tlapm reads a bracketed expression apart from the junction list around it, whose alignment
630
- // SANY keeps applying.
631
611
  const withBracketLayout = <Result>(parse: () => Result) => {
632
612
  if (!isTlapm) return parse();
633
- cursor.offsideColumns.push(NO_OFFSIDE);
613
+ offsideColumns.push(NO_OFFSIDE);
634
614
  const result = parse();
635
- cursor.offsideColumns.pop();
615
+ offsideColumns.pop();
636
616
  return result;
637
617
  };
638
618
 
@@ -644,15 +624,15 @@ const parse = (
644
624
 
645
625
  const isBoundAhead = () => {
646
626
  if (isAt("<<")) {
647
- const offset = { value: 1 };
648
- while (peek(offset.value).kind === "Identifier" || isAt(",", offset.value)) offset.value++;
649
- return isAt(">>", offset.value) && isAt("\\in", offset.value + 1);
627
+ let offset = 1;
628
+ while (peek(offset).kind === "Identifier" || isAt(",", offset)) offset++;
629
+ return isAt(">>", offset) && isAt("\\in", offset + 1);
650
630
  }
651
- const offset = { value: 0 };
652
- while (peek(offset.value).kind === "Identifier") {
653
- if (isAt("\\in", offset.value + 1)) return true;
654
- if (!isAt(",", offset.value + 1)) return false;
655
- offset.value += 2;
631
+ let offset = 0;
632
+ while (peek(offset).kind === "Identifier") {
633
+ if (isAt("\\in", offset + 1)) return true;
634
+ if (!isAt(",", offset + 1)) return false;
635
+ offset += 2;
656
636
  }
657
637
  return false;
658
638
  };
@@ -660,14 +640,9 @@ const parse = (
660
640
  const isLabelAhead = () => {
661
641
  if (isAt("::")) return true;
662
642
  if (!isAt("(")) return false;
663
- const offset = { value: 1 };
664
- while (peek(offset.value).kind === "Identifier" && isAt(",", offset.value + 1))
665
- offset.value += 2;
666
- return (
667
- peek(offset.value).kind === "Identifier" &&
668
- isAt(")", offset.value + 1) &&
669
- isAt("::", offset.value + 2)
670
- );
643
+ let offset = 1;
644
+ while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) offset += 2;
645
+ return peek(offset).kind === "Identifier" && isAt(")", offset + 1) && isAt("::", offset + 2);
671
646
  };
672
647
 
673
648
  const parsePattern = (): BoundPattern => {
@@ -688,12 +663,10 @@ const parse = (
688
663
  return patterns.map((pattern) => ({ pattern, set }));
689
664
  }).flat();
690
665
 
691
- // TLAplusParser.NoOpExtension, which leaves the arguments of the subscript's last name for a
692
- // fairness expression to take as its action.
693
666
  const parseSubscript = (): { subscript: Expression; lastArgs: Expression[] } => {
694
667
  const token = peek();
695
668
  if (isTlapm && BUILTIN_CONSTANTS.has(token.text)) {
696
- const start = cursor.index;
669
+ const start = cursor;
697
670
  advance();
698
671
  const constant: Expression = {
699
672
  kind: "Identifier",
@@ -705,11 +678,11 @@ const parse = (
705
678
  const isTlapmAtom =
706
679
  isTlapm && (token.kind === "Number" || token.kind === "String" || isAt("@"));
707
680
  if (token.kind === "Identifier") {
708
- const start = cursor.index;
681
+ const start = cursor;
709
682
  const names: string[] = [];
710
683
  const selectorRanges: SourceRange[] = [];
711
684
  const parseElement = (isApplicable: boolean) => {
712
- selectorRanges.push(getRange(cursor.index, cursor.index));
685
+ selectorRanges.push(getRange(cursor, cursor));
713
686
  names.push(expectIdentifier());
714
687
  return isApplicable && isAt("(") ? parseArguments() : [];
715
688
  };
@@ -736,7 +709,7 @@ const parse = (
736
709
  return { subscript, lastArgs: elementArgs[elementArgs.length - 1] };
737
710
  }
738
711
  if (isAt("<<") || isAt("(") || isAt("{") || isAt("[") || isTlapmAtom) {
739
- const start = cursor.index;
712
+ const start = cursor;
740
713
  const subscript = parsePrimary();
741
714
  return {
742
715
  subscript: subscript.location.range ? subscript : withRange(subscript, start),
@@ -761,8 +734,6 @@ const parse = (
761
734
  );
762
735
  };
763
736
 
764
- // SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
765
- // the start of an expression, such as -.(1), otherwise.
766
737
  const isStandalone = (operator: Token) => {
767
738
  const token = peek(1);
768
739
  return (
@@ -782,8 +753,6 @@ const parse = (
782
753
  token.kind === "Operator" &&
783
754
  (INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
784
755
  isStandalone(token);
785
- // tlapm reads a prefix operator as the start of an expression, and (+), (-), (/) and (\X) as
786
- // the operators \oplus, \ominus, \oslash and \otimes.
787
756
  const isTlapmIncomplete =
788
757
  isTlapm &&
789
758
  token.kind !== "String" &&
@@ -803,7 +772,7 @@ const parse = (
803
772
  throw createError("\\X may not be used as an infix operator", token);
804
773
  }
805
774
  if (isOperatorAlone) {
806
- cursor.index++;
775
+ cursor++;
807
776
  return withRange(
808
777
  {
809
778
  kind: "OperatorReference",
@@ -811,18 +780,18 @@ const parse = (
811
780
  spelling: token.spelling,
812
781
  location: getLocation(token),
813
782
  },
814
- cursor.index - 1,
783
+ cursor - 1,
815
784
  );
816
785
  }
817
786
  const isPrefixAlone =
818
787
  token.kind !== "String" && OPERATOR_ARGUMENTS.has(token.text) && isStandalone(token);
819
788
  if (isPrefixAlone) {
820
- cursor.index++;
789
+ cursor++;
821
790
  const location = getLocation(token);
822
791
  if (POSTFIX_OPERATORS.has(token.text)) {
823
792
  return withRange(
824
793
  { kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
825
- cursor.index - 1,
794
+ cursor - 1,
826
795
  );
827
796
  }
828
797
  const argument: Expression = { kind: "Identifier", name: "argument", location };
@@ -834,11 +803,11 @@ const parse = (
834
803
  operator: token.text,
835
804
  location: getLocation(token),
836
805
  },
837
- cursor.index - 1,
806
+ cursor - 1,
838
807
  );
839
808
  }
840
809
  if (isAt("LAMBDA")) {
841
- const start = cursor.index;
810
+ const start = cursor;
842
811
  advance();
843
812
  const params = getNames(parseSeparated(expectName));
844
813
  expect(":");
@@ -857,31 +826,31 @@ const parse = (
857
826
  return parseExpression();
858
827
  };
859
828
 
860
- const lastArguments: { range: SourceRange | undefined } = { range: undefined };
829
+ let lastArgumentsRange: SourceRange | undefined;
861
830
 
862
831
  const parseArguments = () =>
863
832
  withBracketLayout(() => {
864
- const start = cursor.index;
833
+ const start = cursor;
865
834
  expect("(");
866
835
  const args = parseSeparated(parseArgument);
867
836
  expect(")");
868
- lastArguments.range = getRange(start);
837
+ lastArgumentsRange = getRange(start);
869
838
  return args;
870
839
  });
871
840
 
872
841
  const parseJunctionList = (): Expression => {
873
842
  const bullet = advance();
874
- cursor.offsideColumns.push(bullet.column);
843
+ offsideColumns.push(bullet.column);
875
844
  const operands = [parseExpression()];
876
845
  while (
877
846
  isBullet(peekRaw()) &&
878
847
  peekRaw().text === bullet.text &&
879
848
  peekRaw().column === bullet.column
880
849
  ) {
881
- cursor.index++;
850
+ cursor++;
882
851
  operands.push(parseExpression());
883
852
  }
884
- cursor.offsideColumns.pop();
853
+ offsideColumns.pop();
885
854
  return {
886
855
  kind: bullet.text === "/\\" ? "And" : "Or",
887
856
  operands,
@@ -893,13 +862,12 @@ const parse = (
893
862
  const parseBracket = (start: Token): Expression =>
894
863
  withBracketLayout(() => {
895
864
  const location = getLocation(start);
896
- // SANY takes a keyword for the first field of a record, as it does for a selected field.
897
865
  const isIdentifier = peek().kind === "Identifier";
898
866
  const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
899
867
  if (isRecord || (isIdentifier && isAt(":", 1))) {
900
868
  const kind = isRecord ? "Record" : "RecordSet";
901
869
  const parseField = (readName: () => string) => {
902
- const fieldStart = cursor.index;
870
+ const fieldStart = cursor;
903
871
  const fieldLocation = getLocation(peek());
904
872
  const name = readName();
905
873
  fieldLocation.range = getRange(fieldStart);
@@ -914,7 +882,7 @@ const parse = (
914
882
  return { kind, fields, location };
915
883
  }
916
884
  if (isBoundAhead()) {
917
- const checkpoint = cursor.index;
885
+ const checkpoint = cursor;
918
886
  const bounds = parseBounds();
919
887
  if (!isTlapm || isAt("|->")) {
920
888
  expect("|->");
@@ -922,7 +890,7 @@ const parse = (
922
890
  expect("]");
923
891
  return { kind: "FunctionConstruction", bounds, body, location };
924
892
  }
925
- cursor.index = checkpoint;
893
+ cursor = checkpoint;
926
894
  }
927
895
  const expression = parseExpression();
928
896
  if (accept("->")) {
@@ -967,9 +935,8 @@ const parse = (
967
935
  };
968
936
  });
969
937
 
970
- // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
971
938
  const parseSetMembership = (): Expression => {
972
- const start = cursor.index;
939
+ const start = cursor;
973
940
  const element = withRange(parsePrefix(), start);
974
941
  const location = getLocation(advance());
975
942
  return withRange(
@@ -983,7 +950,7 @@ const parse = (
983
950
  const location = getLocation(start);
984
951
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
985
952
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
986
- const checkpoint = cursor.index;
953
+ const checkpoint = cursor;
987
954
  const pattern = parsePattern();
988
955
  expect("\\in");
989
956
  const set = parseExpression();
@@ -992,7 +959,7 @@ const parse = (
992
959
  expect("}");
993
960
  return { kind: "SetFilter", bound: { pattern, set }, predicate, location };
994
961
  }
995
- cursor.index = checkpoint;
962
+ cursor = checkpoint;
996
963
  }
997
964
  const first =
998
965
  peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
@@ -1051,7 +1018,7 @@ const parse = (
1051
1018
  const parseCase = (start: Token): Expression => {
1052
1019
  const arms: CaseArm[] = [];
1053
1020
  do {
1054
- const otherStart = cursor.index;
1021
+ const otherStart = cursor;
1055
1022
  if (accept("OTHER")) {
1056
1023
  expect("->");
1057
1024
  const other = parseExpression();
@@ -1096,7 +1063,7 @@ const parse = (
1096
1063
  case "@":
1097
1064
  return { kind: "At" };
1098
1065
  case "(": {
1099
- cursor.index--;
1066
+ cursor--;
1100
1067
  return { kind: "Bound", args: parseArguments() };
1101
1068
  }
1102
1069
  }
@@ -1104,7 +1071,7 @@ const parse = (
1104
1071
  };
1105
1072
 
1106
1073
  const parsePrimary = (preceding?: Precedence): Expression => {
1107
- const start = cursor.index;
1074
+ const start = cursor;
1108
1075
  const token = advance();
1109
1076
  const location = getLocation(token);
1110
1077
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -1136,9 +1103,9 @@ const parse = (
1136
1103
  if (
1137
1104
  !isTlapm &&
1138
1105
  preceding !== undefined &&
1139
- operators.last !== undefined &&
1106
+ lastOperator !== undefined &&
1140
1107
  isOperatorExpression(body) &&
1141
- preceding.high >= operators.last.low
1108
+ preceding.high >= lastOperator.low
1142
1109
  ) {
1143
1110
  throw createError("Removing the label would change expression parsing", token);
1144
1111
  }
@@ -1156,20 +1123,20 @@ const parse = (
1156
1123
  if (name === "@" && !hasArguments && !isSelectorAhead())
1157
1124
  return { kind: "ExceptAt", location };
1158
1125
  const args = hasArguments ? parseArguments() : [];
1159
- const argumentRanges = [hasArguments ? lastArguments.range : undefined];
1126
+ const argumentRanges = [hasArguments ? lastArgumentsRange : undefined];
1160
1127
  if (!isSelectorAhead()) {
1161
1128
  return hasArguments
1162
- ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
1129
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArgumentsRange, location }
1163
1130
  : { kind: "Identifier", name, location };
1164
1131
  }
1165
1132
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1166
1133
  const selectorRanges = [getRange(start, start)];
1167
1134
  while (isSelectorAhead()) {
1168
1135
  advance();
1169
- selectorRanges.push(getRange(cursor.index, cursor.index));
1170
- lastArguments.range = undefined;
1136
+ selectorRanges.push(getRange(cursor, cursor));
1137
+ lastArgumentsRange = undefined;
1171
1138
  selectors.push(parseSelector());
1172
- argumentRanges.push(lastArguments.range);
1139
+ argumentRanges.push(lastArgumentsRange);
1173
1140
  }
1174
1141
  const names = selectors.flatMap((selector) =>
1175
1142
  selector.kind === "Name" ? [selector.name] : [],
@@ -1197,10 +1164,9 @@ const parse = (
1197
1164
  location,
1198
1165
  };
1199
1166
  }
1200
- // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1201
1167
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
1202
1168
  const args = parseArguments();
1203
- // SANY's built-in \X has no definition for TLC to evaluate, a TLC bug.
1169
+ // Deviation from TLC: SANY's built-in \X has no definition for TLC to evaluate, a TLC bug.
1204
1170
  return mode === "tlc" && token.text === "\\X" && args.length === 2
1205
1171
  ? { kind: "Product", factors: args, location }
1206
1172
  : { kind: "Apply", operator: token.text, spelling: token.spelling, args, location };
@@ -1286,11 +1252,8 @@ const parse = (
1286
1252
  throw createUnexpected(`Unexpected ${describeToken(token)}`, token);
1287
1253
  };
1288
1254
 
1289
- // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1290
- // prefix operator.
1291
1255
  const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1292
- operators.last = operators.preceding = { low, high };
1293
- // tlapm applies a postfix operator to the operand, as no prefix operator binds above it.
1256
+ lastOperator = precedingOperator = { low, high };
1294
1257
  const operand = parseExpression(
1295
1258
  isTlapm ? Math.min(high + 1, POSTFIX_PRECEDENCE.low) : high + 1,
1296
1259
  );
@@ -1305,15 +1268,15 @@ const parse = (
1305
1268
  };
1306
1269
 
1307
1270
  const parsePrefix = (): Expression => {
1308
- const preceding = operators.preceding;
1309
- operators.preceding = undefined;
1271
+ const preceding = precedingOperator;
1272
+ precedingOperator = undefined;
1310
1273
  const token = peek();
1311
1274
  const location = getLocation(token);
1312
1275
  if (token.kind === "Operator" && isBullet(token)) {
1313
1276
  return parseJunctionList();
1314
1277
  }
1315
1278
  if (isTlapm && INFIX_OPERATORS.has(token.text) && isAt("(", 1)) {
1316
- const start = cursor.index;
1279
+ const start = cursor;
1317
1280
  const nonfix = withBracketLayout(() => {
1318
1281
  advance();
1319
1282
  advance();
@@ -1330,7 +1293,7 @@ const parse = (
1330
1293
  return withRange(apply, start);
1331
1294
  });
1332
1295
  if (nonfix !== undefined) return nonfix;
1333
- cursor.index = start;
1296
+ cursor = start;
1334
1297
  }
1335
1298
  if (token.kind === "Operator" || token.kind === "Keyword") {
1336
1299
  switch (token.text) {
@@ -1371,7 +1334,6 @@ const parse = (
1371
1334
  return {
1372
1335
  kind: "Apply",
1373
1336
  operator: token.text,
1374
- // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1375
1337
  args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1376
1338
  location,
1377
1339
  };
@@ -1381,31 +1343,29 @@ const parse = (
1381
1343
  };
1382
1344
 
1383
1345
  const parseExpression = (minimumPrecedence = 0): Expression => {
1384
- const start = cursor.index;
1346
+ const start = cursor;
1385
1347
  const prefix = parsePrefix();
1386
- const state = {
1387
- left: prefix.location.range ? prefix : withRange(prefix, start),
1388
- previous: undefined as (InfixOperator & { text: string }) | undefined,
1389
- };
1348
+ let left = prefix.location.range ? prefix : withRange(prefix, start);
1349
+ let previous: (InfixOperator & { text: string }) | undefined;
1390
1350
  while (true) {
1391
1351
  const token = peek();
1392
1352
  if (token.kind !== "Operator") break;
1393
1353
  const location = getLocation(token);
1394
1354
  if (token.text === "'" && minimumPrecedence <= 15) {
1395
1355
  advance();
1396
- operators.last = POSTFIX_PRECEDENCE;
1397
- state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1356
+ lastOperator = POSTFIX_PRECEDENCE;
1357
+ left = withRange({ kind: "Prime", expression: left, location }, start);
1398
1358
  continue;
1399
1359
  }
1400
1360
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1401
1361
  advance();
1402
- operators.last = POSTFIX_PRECEDENCE;
1403
- state.left = withRange(
1362
+ lastOperator = POSTFIX_PRECEDENCE;
1363
+ left = withRange(
1404
1364
  {
1405
1365
  kind: "Apply",
1406
1366
  operator: token.text,
1407
1367
  spelling: token.spelling,
1408
- args: [state.left],
1368
+ args: [left],
1409
1369
  location,
1410
1370
  },
1411
1371
  start,
@@ -1416,22 +1376,18 @@ const parse = (
1416
1376
  advance();
1417
1377
  const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1418
1378
  withBracketLayout(() => expect("]"));
1419
- operators.last = FUNCTION_PRECEDENCE;
1420
- state.left = withRange(
1421
- { kind: "FunctionApplication", function: state.left, args, location },
1422
- start,
1423
- );
1379
+ lastOperator = FUNCTION_PRECEDENCE;
1380
+ left = withRange({ kind: "FunctionApplication", function: left, args, location }, start);
1424
1381
  continue;
1425
1382
  }
1426
1383
  if (token.text === "." && isFieldName(peek(1))) {
1427
1384
  advance();
1428
1385
  const field = advance().text;
1429
- state.left = withRange({ kind: "FieldAccess", record: state.left, field, location }, start);
1386
+ left = withRange({ kind: "FieldAccess", record: left, field, location }, start);
1430
1387
  continue;
1431
1388
  }
1432
1389
  const infix = INFIX_OPERATORS.get(token.text);
1433
1390
  if (!infix || infix.low < minimumPrecedence) break;
1434
- const previous = state.previous;
1435
1391
  if (previous) {
1436
1392
  const isOverlapping = !(previous.low > infix.high || previous.high < infix.low);
1437
1393
  const isChain = previous.text === token.text && infix.isAssociative;
@@ -1443,17 +1399,15 @@ const parse = (
1443
1399
  }
1444
1400
  }
1445
1401
  advance();
1446
- // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1447
1402
  if (!isTlapm && infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1448
1403
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1449
1404
  }
1450
- operators.last = operators.preceding = infix;
1405
+ lastOperator = precedingOperator = infix;
1451
1406
  const right = parseExpression(infix.high + 1);
1452
- const left = state.left;
1453
1407
  if (token.text === "/\\" || token.text === "\\/") {
1454
1408
  const kind = token.text === "/\\" ? "And" : "Or";
1455
1409
  const prefixEnds = [getRange(start).end];
1456
- state.left =
1410
+ left =
1457
1411
  left.kind === kind && previous?.text === token.text
1458
1412
  ? {
1459
1413
  ...left,
@@ -1462,12 +1416,12 @@ const parse = (
1462
1416
  }
1463
1417
  : { kind, operands: [left, right], prefixEnds, location };
1464
1418
  } else if (token.text === "\\X") {
1465
- state.left =
1419
+ left =
1466
1420
  left.kind === "Product" && previous?.text === "\\X"
1467
1421
  ? { ...left, factors: [...left.factors, right] }
1468
1422
  : { kind: "Product", factors: [left, right], location };
1469
1423
  } else {
1470
- state.left = {
1424
+ left = {
1471
1425
  kind: "Apply",
1472
1426
  operator: token.text,
1473
1427
  spelling: token.spelling,
@@ -1475,10 +1429,10 @@ const parse = (
1475
1429
  location,
1476
1430
  };
1477
1431
  }
1478
- state.left = withRange(state.left, start);
1479
- state.previous = { ...infix, text: token.text };
1432
+ left = withRange(left, start);
1433
+ previous = { ...infix, text: token.text };
1480
1434
  }
1481
- return state.left;
1435
+ return left;
1482
1436
  };
1483
1437
 
1484
1438
  const parseParams = () => {
@@ -1491,17 +1445,17 @@ const parse = (
1491
1445
  const parseBody = () => {
1492
1446
  const start = getLocation(peek());
1493
1447
  const body = parseExpression();
1494
- const last = tokens[cursor.index - 1];
1448
+ const last = tokens[cursor - 1];
1495
1449
  const end = { line: last.line, column: last.endColumn };
1496
1450
  return { body, bodyRange: { module: openModules[0].name, start, end } };
1497
1451
  };
1498
1452
 
1499
1453
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
1500
- const start = cursor.index;
1454
+ const start = cursor;
1501
1455
  return withRange(parseDefinitionUnit(isLocal), start);
1502
1456
  };
1503
1457
 
1504
- const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1458
+ const toParam = (named: RangedName): OperatorParam => ({
1505
1459
  name: named.name,
1506
1460
  arity: 0,
1507
1461
  location: {
@@ -1513,13 +1467,13 @@ const parse = (
1513
1467
 
1514
1468
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1515
1469
  const token = peek();
1516
- const start = cursor.index;
1470
+ const start = cursor;
1517
1471
  const location = getLocation(token);
1518
1472
  const parseOperator = (name: string, params: OperatorParam[]): Definition => ({
1519
1473
  kind: "Operator",
1520
1474
  name,
1521
1475
  params,
1522
- head: getRange(start, cursor.index - 2),
1476
+ head: getRange(start, cursor - 2),
1523
1477
  ...parseBody(),
1524
1478
  isLocal,
1525
1479
  isRecursive: isRecursiveName(name),
@@ -1565,7 +1519,7 @@ const parse = (
1565
1519
  location,
1566
1520
  };
1567
1521
  }
1568
- const definitionStart = cursor.index;
1522
+ const definitionStart = cursor;
1569
1523
  const params = parseParams();
1570
1524
  expect("==");
1571
1525
  if (isAt("INSTANCE")) {
@@ -1581,12 +1535,12 @@ const parse = (
1581
1535
  isLocal: boolean,
1582
1536
  location: SourceLocation,
1583
1537
  ): Instance => {
1584
- const start = cursor.index;
1538
+ const start = cursor;
1585
1539
  expect("INSTANCE");
1586
1540
  const module = expectModuleName();
1587
1541
  const substitutions = accept("WITH")
1588
1542
  ? parseSeparated(() => {
1589
- const targetStart = cursor.index;
1543
+ const targetStart = cursor;
1590
1544
  const target = expectSubstitutionTarget();
1591
1545
  const nameRange = getRange(targetStart);
1592
1546
  expect("<-");
@@ -1606,7 +1560,7 @@ const parse = (
1606
1560
  };
1607
1561
 
1608
1562
  const parseDeclaration = (): Declaration => {
1609
- const start = cursor.index;
1563
+ const start = cursor;
1610
1564
  return withRange(parseDeclarationUnit(), start);
1611
1565
  };
1612
1566
 
@@ -1642,19 +1596,19 @@ const parse = (
1642
1596
  return declarations;
1643
1597
  };
1644
1598
 
1645
- const getClosingOffset = (offset: number) => {
1646
- const depth = { value: 0, offset };
1599
+ const getClosingOffset = (start: number) => {
1600
+ let depth = 0;
1601
+ let offset = start;
1647
1602
  do {
1648
- const token = peek(depth.offset);
1649
- if (token.kind === "End") return depth.offset;
1650
- if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth.value++;
1651
- if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth.value--;
1652
- depth.offset++;
1653
- } while (depth.value > 0);
1654
- return depth.offset;
1603
+ const token = peek(offset);
1604
+ if (token.kind === "End") return offset;
1605
+ if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth++;
1606
+ if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth--;
1607
+ offset++;
1608
+ } while (depth > 0);
1609
+ return offset;
1655
1610
  };
1656
1611
 
1657
- // SANY inserts a break before each definition, which ends the fact list or step before it.
1658
1612
  const isDefinitionStart = () => {
1659
1613
  const token = peek();
1660
1614
  const next = peek(1);
@@ -1675,7 +1629,6 @@ const parse = (
1675
1629
  return isAt("==", 1);
1676
1630
  };
1677
1631
 
1678
- // SANY's proof levels, one for each open proof, which stay -1 until a step sets them.
1679
1632
  const proofLevels: number[] = [];
1680
1633
 
1681
1634
  const getProofLevel = () => proofLevels[proofLevels.length - 1] ?? -1;
@@ -1688,9 +1641,9 @@ const parse = (
1688
1641
 
1689
1642
  const getStepLabel = (step: Token) => {
1690
1643
  const label = step.text.slice(step.text.indexOf(">") + 1);
1691
- const end = { value: label.length };
1692
- while (label[end.value - 1] === ".") end.value--;
1693
- return label.slice(0, end.value);
1644
+ let end = label.length;
1645
+ while (label[end - 1] === ".") end--;
1646
+ return label.slice(0, end);
1694
1647
  };
1695
1648
 
1696
1649
  const isStepReference = (step: Token) =>
@@ -1753,17 +1706,15 @@ const parse = (
1753
1706
  return !isDefinitionStart();
1754
1707
  };
1755
1708
 
1756
- // SANY reads an infix or postfix operator alone as its name, but a prefix operator such as - as the
1757
- // start of an expression.
1758
1709
  const parseFact = (): Fact => {
1759
- const start = cursor.index;
1710
+ const start = cursor;
1760
1711
  const token = peek();
1761
1712
  if (
1762
1713
  token.kind === "Operator" &&
1763
1714
  !EXPRESSION_PREFIXES.has(token.text) &&
1764
1715
  isOperatorName(token.text)
1765
1716
  ) {
1766
- cursor.index++;
1717
+ cursor++;
1767
1718
  return withRange(
1768
1719
  {
1769
1720
  kind: "OperatorReference",
@@ -1799,7 +1750,7 @@ const parse = (
1799
1750
  );
1800
1751
 
1801
1752
  const parseDefinable = (): Fact => {
1802
- const start = cursor.index;
1753
+ const start = cursor;
1803
1754
  const token = peek();
1804
1755
  const parts = parseSeparated(() => {
1805
1756
  const part = peek();
@@ -1807,7 +1758,7 @@ const parse = (
1807
1758
  if (!isName && part.kind !== "Operator") {
1808
1759
  throw createUnexpected(`Expected a definition name but found ${describeToken(part)}`, part);
1809
1760
  }
1810
- cursor.index++;
1761
+ cursor++;
1811
1762
  return part.text;
1812
1763
  }, "!");
1813
1764
  const location = getLocation(token);
@@ -1840,7 +1791,7 @@ const parse = (
1840
1791
  };
1841
1792
 
1842
1793
  const parseUseOrHide = (): UseOrHide => {
1843
- const start = cursor.index;
1794
+ const start = cursor;
1844
1795
  const keyword = advance();
1845
1796
  const isOnly = keyword.text === "USE" && accept("ONLY");
1846
1797
  const keywordRange = getRange(start);
@@ -1866,11 +1817,10 @@ const parse = (
1866
1817
  };
1867
1818
 
1868
1819
  const parseNewSymbol = (): NewSymbol => {
1869
- const newStart = cursor.index;
1820
+ const newStart = cursor;
1870
1821
  const newLocation = getLocation(peek());
1871
1822
  const isNew = accept("NEW");
1872
- // tlapm locates a declaration without its NEW.
1873
- const start = isTlapm ? cursor.index : newStart;
1823
+ const start = isTlapm ? cursor : newStart;
1874
1824
  const location = isTlapm ? getLocation(peek()) : newLocation;
1875
1825
  const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1876
1826
  if (!isNew && level === undefined) {
@@ -1915,7 +1865,7 @@ const parse = (
1915
1865
  };
1916
1866
 
1917
1867
  const parseAssumeProve = (): AssumeProve => {
1918
- const start = cursor.index;
1868
+ const start = cursor;
1919
1869
  const location = getLocation(expect("ASSUME"));
1920
1870
  const assumptions = parseSeparated(() => {
1921
1871
  if (peek().kind === "Identifier" && isAt("::", 1) && isAt("ASSUME", 2)) {
@@ -1934,11 +1884,11 @@ const parse = (
1934
1884
  };
1935
1885
 
1936
1886
  const isNameListAhead = (terminator: string) => {
1937
- const offset = { value: 0 };
1938
- while (peek(offset.value).kind === "Identifier" && isAt(",", offset.value + 1)) {
1939
- offset.value += 2;
1887
+ let offset = 0;
1888
+ while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) {
1889
+ offset += 2;
1940
1890
  }
1941
- return peek(offset.value).kind === "Identifier" && isAt(terminator, offset.value + 1);
1891
+ return peek(offset).kind === "Identifier" && isAt(terminator, offset + 1);
1942
1892
  };
1943
1893
 
1944
1894
  const declareProofSymbols = (names: string[], bounds: Bound[]) => {
@@ -1954,11 +1904,11 @@ const parse = (
1954
1904
  const token = peek();
1955
1905
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1956
1906
  if (isAt("INSTANCE")) {
1957
- const start = cursor.index;
1907
+ const start = cursor;
1958
1908
  return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1959
1909
  }
1960
1910
  if (isAt("DEFINE") || isDefinitionStart()) {
1961
- const start = cursor.index;
1911
+ const start = cursor;
1962
1912
  accept("DEFINE");
1963
1913
  const definitions = [parseDefinition(false)];
1964
1914
  while (isDefinitionStart()) definitions.push(parseDefinition(false));
@@ -2009,16 +1959,14 @@ const parse = (
2009
1959
  if (mode !== "sany" && getStepLevel(step) < 0 && getStepName(step) !== undefined) {
2010
1960
  throw createError("<*> and <+> cannot be used for a named step", step);
2011
1961
  }
2012
- cursor.index++;
1962
+ cursor++;
2013
1963
  return { name: getStepName(step), token: step.text, location: getLocation(step) };
2014
1964
  };
2015
1965
 
2016
- // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
2017
- // of the enclosing proof.
2018
1966
  const parseStep = (): ProofStep => {
2019
- const start = cursor.index;
1967
+ const start = cursor;
2020
1968
  const step = parseStepStart(isAt("QED", 1));
2021
- const statementStart = cursor.index;
1969
+ const statementStart = cursor;
2022
1970
  const scope = new Set<string>();
2023
1971
  proofScopes.push(scope);
2024
1972
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
@@ -2038,14 +1986,14 @@ const parse = (
2038
1986
  location: SourceLocation,
2039
1987
  ): Exclude<Proof, { kind: "Steps" }> | undefined => {
2040
1988
  if (accept("BY")) {
2041
- const keyword = getRange(cursor.index - 1);
1989
+ const keyword = getRange(cursor - 1);
2042
1990
  return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
2043
1991
  }
2044
1992
  const terminal = peek();
2045
1993
  if (terminal.kind !== "Keyword" || (terminal.text !== "OBVIOUS" && terminal.text !== "OMITTED"))
2046
1994
  return undefined;
2047
1995
  advance();
2048
- const keyword = getRange(cursor.index - 1);
1996
+ const keyword = getRange(cursor - 1);
2049
1997
  return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
2050
1998
  };
2051
1999
 
@@ -2060,7 +2008,7 @@ const parse = (
2060
2008
  };
2061
2009
 
2062
2010
  const parseProof = (): Proof => {
2063
- const start = cursor.index;
2011
+ const start = cursor;
2064
2012
  proofLevels.push(-1);
2065
2013
  const proof = withProofScope(parseProofBody);
2066
2014
  proofLevels.pop();
@@ -2070,7 +2018,6 @@ const parse = (
2070
2018
  const isPreproofStart = () =>
2071
2019
  peek().kind === "Step" || (peek().kind === "Keyword" && PROOF_STARTS.has(peek().text));
2072
2020
 
2073
- // tlapm locates a USE or HIDE step by its keywords and DEF clause.
2074
2021
  const getStepSpan = (head: SourceRange, body: StepBody) => {
2075
2022
  if (body.kind !== "Use" && body.kind !== "Hide") return getSpan(head);
2076
2023
  const lastDef = body.defs[body.defs.length - 1]?.location.range;
@@ -2078,21 +2025,21 @@ const parse = (
2078
2025
  };
2079
2026
 
2080
2027
  const parsePreproof = (): Preproof => {
2081
- const first = cursor.index;
2028
+ const first = cursor;
2082
2029
  const location = getLocation(peek());
2083
2030
  const isProof = accept("PROOF");
2084
2031
  const leaf = parseLeafProof(location);
2085
2032
  if (leaf) {
2086
2033
  const proof = withRange(leaf, first);
2087
- return { kind: "Leaf", proof, span: getSpan(proof.keyword), first, end: cursor.index - 1 };
2034
+ return { kind: "Leaf", proof, span: getSpan(proof.keyword), first, end: cursor - 1 };
2088
2035
  }
2089
2036
  const token = peek();
2090
2037
  if (token.kind !== "Step") {
2091
2038
  throw createUnexpected(`Expected a step number but found ${describeToken(token)}`);
2092
2039
  }
2093
- const start = cursor.index;
2040
+ const start = cursor;
2094
2041
  advance();
2095
- const statementStart = cursor.index;
2042
+ const statementStart = cursor;
2096
2043
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
2097
2044
  const head = getRange(start);
2098
2045
  return {
@@ -2105,11 +2052,10 @@ const parse = (
2105
2052
  span: getStepSpan(head, body),
2106
2053
  first,
2107
2054
  start,
2108
- end: cursor.index - 1,
2055
+ end: cursor - 1,
2109
2056
  };
2110
2057
  };
2111
2058
 
2112
- // A backtrack leaves the proof implicitly omitted.
2113
2059
  const nestProof = (
2114
2060
  items: Preproof[],
2115
2061
  index: number,
@@ -2202,8 +2148,6 @@ const parse = (
2202
2148
  );
2203
2149
  };
2204
2150
 
2205
- // tlapm reads every step and leaf proof that follows, then nests them by level, where the first
2206
- // step counts as having PROOF.
2207
2151
  const parseTlapmProof = () => {
2208
2152
  if (!isPreproofStart()) return undefined;
2209
2153
  const items: Preproof[] = [];
@@ -2222,22 +2166,23 @@ const parse = (
2222
2166
  return nested.proof;
2223
2167
  };
2224
2168
 
2225
- // SANY binds the name of a theorem or assumption that a module already defines to the first
2226
- // definition, warning of the second.
2227
2169
  const addFactDefinition = (module: Module, definition: Definition) => {
2228
2170
  if (module.definitions.some((other) => other.name === definition.name)) return;
2229
2171
  module.definitions.push(definition);
2230
2172
  };
2231
2173
 
2174
+ const parseFactName = () => {
2175
+ if (peek().kind !== "Identifier" || !isAt("==", 1)) return undefined;
2176
+ const name = expectIdentifier();
2177
+ factNames.add(name);
2178
+ advance();
2179
+ return name;
2180
+ };
2181
+
2232
2182
  const parseTheorem = (module: Module) => {
2233
- const start = cursor.index;
2183
+ const start = cursor;
2234
2184
  const keyword = advance();
2235
- const isNamed = peek().kind === "Identifier" && isAt("==", 1);
2236
- const name = isNamed ? expectIdentifier() : undefined;
2237
- if (name !== undefined) {
2238
- factNames.add(name);
2239
- advance();
2240
- }
2185
+ const name = parseFactName();
2241
2186
  const parsed = withProofScope(() => {
2242
2187
  const statement = isAt("ASSUME") ? parseAssumeProve() : parseExpression();
2243
2188
  if (isTlapm) return { statement, proof: parseTlapmProof() };
@@ -2269,14 +2214,9 @@ const parse = (
2269
2214
  };
2270
2215
 
2271
2216
  const parseAssumption = (): Assumption => {
2272
- const start = cursor.index;
2217
+ const start = cursor;
2273
2218
  const keyword = advance();
2274
- const isNamed = peek().kind === "Identifier" && isAt("==", 1);
2275
- const name = isNamed ? expectIdentifier() : undefined;
2276
- if (name !== undefined) {
2277
- factNames.add(name);
2278
- advance();
2279
- }
2219
+ const name = parseFactName();
2280
2220
  return withRange(
2281
2221
  {
2282
2222
  name,
@@ -2312,14 +2252,14 @@ const parse = (
2312
2252
  recursiveScopes.push(new Set());
2313
2253
  if (accept("EXTENDS")) {
2314
2254
  parseSeparated(() => {
2315
- const start = cursor.index;
2255
+ const start = cursor;
2316
2256
  module.extends.push(expectModuleName());
2317
2257
  module.extendsRanges.push(getRange(start));
2318
2258
  });
2319
2259
  }
2320
2260
  while (true) {
2321
- const tokenIndex = cursor.index;
2322
- unitStart.index = tokenIndex;
2261
+ const tokenIndex = cursor;
2262
+ unitStart = tokenIndex;
2323
2263
  const token = peek();
2324
2264
  if (token.kind === "ModuleEnd") {
2325
2265
  location.range = {
@@ -2356,7 +2296,7 @@ const parse = (
2356
2296
  case "VARIABLES": {
2357
2297
  const unitLocation = getLocation(advance());
2358
2298
  const declarations = parseSeparated(() => {
2359
- const start = cursor.index;
2299
+ const start = cursor;
2360
2300
  const location = getLocation(peek());
2361
2301
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
2362
2302
  });
@@ -2386,7 +2326,6 @@ const parse = (
2386
2326
  }
2387
2327
  case "USE":
2388
2328
  case "HIDE": {
2389
- // SANY's module body takes a USE only when ONLY doesn't follow it.
2390
2329
  if (isAt("USE") && isAt("ONLY", 1)) {
2391
2330
  throw createUnexpected(`Expected a module unit but found ${describeToken(token)}`);
2392
2331
  }
@@ -2440,9 +2379,7 @@ const parse = (
2440
2379
  };
2441
2380
 
2442
2381
  const module = parseModuleBody();
2443
- // SANY's lexer marks where each definition begins before its parser runs, which can split a
2444
- // module's name from a keyword that the parser would take for one.
2445
- const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2382
+ const sanyError = isKeywordModuleName ? getSanyParseError(source) : undefined;
2446
2383
  if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2447
2384
  return module;
2448
2385
  };