tla-ts 0.0.2 → 0.0.3-dev.14337b3

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 (65) hide show
  1. package/README.md +78 -12
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +85 -15
  4. package/dist/index.d.ts +85 -15
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +22 -2
  11. package/dist/run-cli.js +22 -2
  12. package/dist/transfer.cjs +37 -16
  13. package/dist/transfer.js +37 -16
  14. package/package.json +3 -1
  15. package/src/ast.ts +137 -4
  16. package/src/check.ts +378 -100
  17. package/src/cli-options.ts +64 -0
  18. package/src/codegen.ts +15 -22
  19. package/src/config.ts +224 -170
  20. package/src/dependencies.ts +2 -2
  21. package/src/environment.ts +116 -28
  22. package/src/errors.ts +101 -3
  23. package/src/evaluate.ts +129 -153
  24. package/src/exit-codes.ts +39 -42
  25. package/src/fingerprints.ts +46 -11
  26. package/src/instances.ts +16 -15
  27. package/src/levels.ts +62 -34
  28. package/src/lexer.ts +11 -2
  29. package/src/live-check.ts +772 -0
  30. package/src/liveness.ts +116 -543
  31. package/src/obligation-action.ts +384 -0
  32. package/src/obligation-anon.ts +575 -0
  33. package/src/obligation-deref.ts +637 -0
  34. package/src/obligation-expression.ts +1060 -0
  35. package/src/obligation-format.ts +406 -0
  36. package/src/obligation-generate.ts +1074 -0
  37. package/src/obligation-levels.ts +329 -0
  38. package/src/obligation-module.ts +1042 -0
  39. package/src/obligation-printer.ts +889 -0
  40. package/src/obligation-proof.ts +76 -0
  41. package/src/obligation-source.ts +1084 -0
  42. package/src/obligation-tuples.ts +184 -0
  43. package/src/obligations.ts +316 -0
  44. package/src/parallel-worker.ts +168 -212
  45. package/src/parallel.ts +78 -37
  46. package/src/parser.ts +315 -60
  47. package/src/resolve.ts +122 -44
  48. package/src/run-cli.ts +376 -44
  49. package/src/runtime.ts +3 -1
  50. package/src/spec-description.ts +19 -8
  51. package/src/standard-levels.ts +3 -3
  52. package/src/standard-modules.ts +2 -7
  53. package/src/state-graph.ts +157 -0
  54. package/src/subexpressions.ts +10 -10
  55. package/src/successors.ts +99 -25
  56. package/src/symmetry.ts +30 -8
  57. package/src/tableau.ts +51 -24
  58. package/src/tlc-ext.ts +29 -12
  59. package/src/tlc-module.ts +14 -33
  60. package/src/tlc-modules.ts +4 -0
  61. package/src/tlc-order.ts +25 -7
  62. package/src/tlc-random.ts +9 -6
  63. package/src/tlc-summary.ts +20 -5
  64. package/src/transfer.ts +22 -1
  65. package/src/values.ts +179 -42
package/src/parser.ts CHANGED
@@ -22,7 +22,7 @@ import type {
22
22
  StepBody,
23
23
  UseOrHide,
24
24
  } from "./ast";
25
- import { TlaSyntaxError } from "./errors";
25
+ import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
26
26
  import { getNumeralValue, tokenize, type Token } from "./lexer";
27
27
 
28
28
  interface InfixOperator {
@@ -135,6 +135,8 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
135
135
  ["^^", createInfix(14, 14, false)],
136
136
  ]);
137
137
 
138
+ const NEGATION_PRECEDENCE = 12;
139
+
138
140
  const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
139
141
 
140
142
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
@@ -153,6 +155,8 @@ const EXPRESSION_PREFIXES = new Set([
153
155
  "<>",
154
156
  ]);
155
157
 
158
+ const CIRCLED_OPERATORS = new Set(["+", "-", "/", "\\X"]);
159
+
156
160
  const OPERATOR_ARGUMENTS = new Set([
157
161
  "SUBSET",
158
162
  "UNION",
@@ -213,6 +217,41 @@ const PROOF_STARTS = new Set(["BY", "PROOF", "OBVIOUS", "OMITTED"]);
213
217
 
214
218
  const PROVABLE_STEPS = new Set(["Qed", "Pick", "Case", "Assert"]);
215
219
 
220
+ const TLAPM_PUNCTUATION = new Set([
221
+ ",",
222
+ ".",
223
+ "_",
224
+ "(",
225
+ ")",
226
+ "[",
227
+ "]",
228
+ "{",
229
+ "}",
230
+ "<<",
231
+ ">>",
232
+ "]_",
233
+ ">>_",
234
+ "==",
235
+ "!",
236
+ "@",
237
+ ":",
238
+ "::",
239
+ ";",
240
+ "->",
241
+ "<-",
242
+ "|->",
243
+ "\\A",
244
+ "\\AA",
245
+ "\\E",
246
+ "\\EE",
247
+ "\\forall",
248
+ "\\exists",
249
+ "WF_",
250
+ "SF_",
251
+ "(*{",
252
+ "}*)",
253
+ ]);
254
+
216
255
  const UNNAMED_STEP_LABELS = new Set(["", "*", "-"]);
217
256
 
218
257
  const FACT_KEYWORDS = new Set([
@@ -264,7 +303,9 @@ export const extractModule = (source: string, name: string) => {
264
303
  return lines.slice(start, end === -1 ? undefined : end + 1).join("\n");
265
304
  };
266
305
 
267
- export const parseModule = (source: string): Module => {
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 => {
268
309
  const tokens = tokenize(source);
269
310
  const cursor = { index: 0, offsideColumns: [] as number[] };
270
311
  // A RECURSIVE declaration covers the definitions of its own LET or module.
@@ -280,7 +321,7 @@ export const parseModule = (source: string): Module => {
280
321
 
281
322
  const getLocation = (token: Token): SourceLocation => ({
282
323
  line: token.line,
283
- column: token.column,
324
+ column: isTlapm && token.kind === "String" ? token.endColumn : token.column,
284
325
  range: undefined,
285
326
  });
286
327
 
@@ -324,10 +365,78 @@ export const parseModule = (source: string): Module => {
324
365
  const describe = (token: Token) =>
325
366
  token.kind === "End" ? "end of input" : JSON.stringify(token.text);
326
367
 
327
- const createError = (message: string, token: { line: number; column: number } = peekRaw()) =>
328
- new TlaSyntaxError(`${message} at line ${token.line}, column ${token.column}`);
368
+ const unitStart = { index: 0 };
369
+
370
+ const getDefinitionEnd = (fallback: { line: number; column: number }) => {
371
+ const equals = tokens
372
+ .slice(unitStart.index, cursor.index)
373
+ .find((token) => token.text === "==" && token.kind !== "String");
374
+ return equals ? { line: equals.line, column: equals.endColumn + 1 } : fallback;
375
+ };
376
+
377
+ // tlapm reads TRUE, FALSE, BOOLEAN, STRING and the other keywords only as keywords.
378
+ const isTlapmKeyword = (token: Token) =>
379
+ isTlapm && (token.kind === "Keyword" || BUILTIN_CONSTANTS.has(token.text));
380
+
381
+ const getPreviousEnd = (position: { line: number; column: number }) => {
382
+ const previous = tokens.findLast(
383
+ (token) =>
384
+ token.kind !== "End" &&
385
+ (token.line < position.line ||
386
+ (token.line === position.line && token.column < position.column)),
387
+ );
388
+ return previous ? { line: previous.line, column: previous.endColumn + 1 } : position;
389
+ };
390
+
391
+ const getTlapmName = (token: Token) => {
392
+ if (/^-{4,}$/.test(token.text)) return "----";
393
+ if (/^={4,}$/.test(token.text)) return "====";
394
+ if (TLAPM_PUNCTUATION.has(token.text) || token.kind === "Number" || token.kind === "Decimal")
395
+ return token.text;
396
+ if (token.kind === "String") return JSON.stringify(token.text);
397
+ if (isTlapmKeyword(token)) return `keyword ${token.text}`;
398
+ return token.kind === "Identifier" ? `identifier ${token.text}` : `operator ${token.text}`;
399
+ };
400
+
401
+ const createError = (
402
+ message: string,
403
+ token: { line: number; column: number } = peekRaw(),
404
+ unexpected?: Token,
405
+ ) => {
406
+ const end = tokens[tokens.length - 1];
407
+ const isEnd = token.line === end.line && token.column === end.column;
408
+ return new TlaSyntaxError(
409
+ `${message} at line ${token.line}, column ${token.column}`,
410
+ undefined,
411
+ getFailure(message, token, isEnd, unexpected),
412
+ );
413
+ };
414
+
415
+ const getFailure = (
416
+ message: string,
417
+ token: { line: number; column: number },
418
+ isEnd: boolean,
419
+ unexpected: Token | undefined,
420
+ ) => {
421
+ if (isTlapm && isEnd) {
422
+ const end = tokens[tokens.length - 1];
423
+ return { reason: "Unexpected EOF", ...getDefinitionEnd(getPreviousEnd(end)), isEnd };
424
+ }
425
+ if (isTlapm && unexpected) {
426
+ return {
427
+ reason: `Unexpected ${getTlapmName(unexpected)}`,
428
+ ...getPreviousEnd(unexpected),
429
+ isEnd,
430
+ };
431
+ }
432
+ return { reason: message, line: token.line, column: token.column, isEnd };
433
+ };
434
+
435
+ const createUnexpected = (message: string, token: Token = peekRaw()) =>
436
+ createError(message, token, token);
329
437
 
330
- const isFieldName = (token: Token) => token.kind === "Identifier" || token.kind === "Keyword";
438
+ const isFieldName = (token: Token) =>
439
+ (token.kind === "Identifier" || token.kind === "Keyword") && !isTlapmKeyword(token);
331
440
 
332
441
  const isAt = (text: string, offset = 0) => {
333
442
  const token = peek(offset);
@@ -349,15 +458,16 @@ export const parseModule = (source: string): Module => {
349
458
 
350
459
  const expect = (text: string) => {
351
460
  if (!isAt(text))
352
- throw createError(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
461
+ throw createUnexpected(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
353
462
  return advance();
354
463
  };
355
464
 
356
465
  const expectIdentifier = () => {
357
466
  const token = peek();
358
467
  if (token.kind !== "Identifier") {
359
- throw createError(`Expected an identifier but found ${describe(token)}`);
468
+ throw createUnexpected(`Expected an identifier but found ${describe(token)}`);
360
469
  }
470
+ if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
361
471
  cursor.index++;
362
472
  return token.text;
363
473
  };
@@ -422,7 +532,7 @@ export const parseModule = (source: string): Module => {
422
532
  const token = peek();
423
533
  if (token.kind === "Identifier") {
424
534
  const start = cursor.index;
425
- const names = [expectIdentifier()];
535
+ const names = [advance().text];
426
536
  while (isAt("!") && peek(1).kind === "Identifier") {
427
537
  advance();
428
538
  names.push(expectIdentifier());
@@ -433,7 +543,7 @@ export const parseModule = (source: string): Module => {
433
543
  );
434
544
  }
435
545
  if (isAt("<<") || isAt("(")) return parsePrimary();
436
- throw createError(`Expected a subscript but found ${describe(token)}`);
546
+ throw createUnexpected(`Expected a subscript but found ${describe(token)}`);
437
547
  };
438
548
 
439
549
  const isDefinitionAhead = (operator: Token) => {
@@ -470,6 +580,23 @@ export const parseModule = (source: string): Module => {
470
580
  token.kind === "Operator" &&
471
581
  (INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
472
582
  isStandalone(token);
583
+ // tlapm reads a prefix operator as the start of an expression, and (+), (-), (/) and (\X) as
584
+ // the operators \oplus, \ominus, \oslash and \otimes.
585
+ const isTlapmIncomplete =
586
+ isTlapm &&
587
+ token.kind !== "String" &&
588
+ (PREFIX_OPERATOR_REFERENCES.has(token.text) ||
589
+ (OPERATOR_ARGUMENTS.has(token.text) &&
590
+ token.text !== "'" &&
591
+ !POSTFIX_OPERATORS.has(token.text)) ||
592
+ (CIRCLED_OPERATORS.has(token.text) &&
593
+ peek(-1).text === "(" &&
594
+ peek(-1).line === token.line &&
595
+ peek(-1).endColumn + 1 === token.column &&
596
+ peek(1).text === ")" &&
597
+ peek(1).line === token.line &&
598
+ peek(1).column === token.endColumn + 1));
599
+ if (isTlapmIncomplete && isStandalone(token)) throw createError("incomplete expression", token);
473
600
  if (isOperatorAlone) {
474
601
  cursor.index++;
475
602
  return withRange(
@@ -504,11 +631,15 @@ export const parseModule = (source: string): Module => {
504
631
  return parseExpression();
505
632
  };
506
633
 
634
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
635
+
507
636
  const parseArguments = () =>
508
637
  withoutOffside(() => {
638
+ const start = cursor.index;
509
639
  expect("(");
510
640
  const args = parseSeparated(parseArgument);
511
641
  expect(")");
642
+ lastArguments.range = getRange(start);
512
643
  return args;
513
644
  });
514
645
 
@@ -524,6 +655,7 @@ export const parseModule = (source: string): Module => {
524
655
  return {
525
656
  kind: bullet.text === "/\\" ? "And" : "Or",
526
657
  operands,
658
+ isBulleted: true,
527
659
  location: getLocation(bullet),
528
660
  };
529
661
  };
@@ -534,9 +666,12 @@ export const parseModule = (source: string): Module => {
534
666
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
535
667
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
536
668
  const fields = parseSeparated(() => {
669
+ const fieldStart = cursor.index;
670
+ const fieldLocation = getLocation(peek());
537
671
  const name = expectIdentifier();
672
+ fieldLocation.range = getRange(fieldStart);
538
673
  expect(kind === "Record" ? "|->" : ":");
539
- return { name, value: parseExpression() };
674
+ return { name, value: parseExpression(), location: fieldLocation };
540
675
  });
541
676
  expect("]");
542
677
  return { kind, fields, location };
@@ -561,7 +696,7 @@ export const parseModule = (source: string): Module => {
561
696
  while (isAt(".") || isAt("[")) {
562
697
  if (accept(".")) {
563
698
  if (!isFieldName(peek()))
564
- throw createError(`Expected a field name but found ${describe(peek())}`);
699
+ throw createUnexpected(`Expected a field name but found ${describe(peek())}`);
565
700
  path.push({ kind: "Field", name: advance().text });
566
701
  continue;
567
702
  }
@@ -637,7 +772,14 @@ export const parseModule = (source: string): Module => {
637
772
  }
638
773
  const names = parseSeparated(expectIdentifier);
639
774
  expect(":");
640
- return { kind: "UnboundedQuantifier", names, body: parseExpression(), isTemporal, location };
775
+ return {
776
+ kind: "UnboundedQuantifier",
777
+ quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
778
+ names,
779
+ body: parseExpression(),
780
+ isTemporal,
781
+ location,
782
+ };
641
783
  };
642
784
 
643
785
  const parseCase = (start: Token): Expression => {
@@ -691,7 +833,13 @@ export const parseModule = (source: string): Module => {
691
833
  const isFactStart = cursor.index === factStart.index;
692
834
  const token = advance();
693
835
  const location = getLocation(token);
836
+ if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
837
+ const params = parseSeparated(expectIdentifier);
838
+ expect(":");
839
+ return { kind: "Lambda", params, body: parseExpression(), location };
840
+ }
694
841
  if (token.kind === "Number") {
842
+ if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
695
843
  return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
696
844
  }
697
845
  if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
@@ -715,13 +863,13 @@ export const parseModule = (source: string): Module => {
715
863
  const args = hasArguments ? parseArguments() : [];
716
864
  if (!isSelectorAhead()) {
717
865
  return hasArguments
718
- ? { kind: "Apply", operator: name, args, location }
866
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
719
867
  : { kind: "Identifier", name, location };
720
868
  }
721
869
  const instance = openModules
722
870
  .flatMap((module) => module.instances)
723
871
  .find((candidate) => candidate.name === name);
724
- if (instance && !isFactStart && instance.params.length !== args.length) {
872
+ if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
725
873
  throw createError(
726
874
  `The operator ${name} requires ${instance.params.length} arguments`,
727
875
  token,
@@ -740,9 +888,14 @@ export const parseModule = (source: string): Module => {
740
888
  const qualifiedArgs = selectors.flatMap((selector) =>
741
889
  "args" in selector ? selector.args : [],
742
890
  );
743
- return qualifiedArgs.length > 0
744
- ? { kind: "Apply", operator, args: qualifiedArgs, location }
745
- : { kind: "Identifier", name: operator, location };
891
+ if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
892
+ const argumentCounts = selectors.map((selector) =>
893
+ "args" in selector ? selector.args.length : 0,
894
+ );
895
+ const argumentsRange = lastArguments.range;
896
+ return argumentCounts.slice(0, -1).some((count) => count > 0)
897
+ ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
898
+ : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
746
899
  }
747
900
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
748
901
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -779,6 +932,8 @@ export const parseModule = (source: string): Module => {
779
932
  const instances: Instance[] = [];
780
933
  recursiveScopes.push(new Set());
781
934
  while (!accept("IN")) {
935
+ if (isTlapm && isAt("RECURSIVE"))
936
+ throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
782
937
  if (accept("RECURSIVE")) {
783
938
  declareRecursive();
784
939
  continue;
@@ -816,7 +971,7 @@ export const parseModule = (source: string): Module => {
816
971
  };
817
972
  }
818
973
  }
819
- throw createError(`Unexpected ${describe(token)}`, token);
974
+ throw createUnexpected(`Unexpected ${describe(token)}`, token);
820
975
  };
821
976
 
822
977
  const parsePrefix = (): Expression => {
@@ -832,7 +987,12 @@ export const parseModule = (source: string): Module => {
832
987
  return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
833
988
  case "-":
834
989
  advance();
835
- return { kind: "Apply", operator: "-.", args: [parseExpression(13)], location };
990
+ return {
991
+ kind: "Apply",
992
+ operator: "-.",
993
+ args: [parseExpression(NEGATION_PRECEDENCE + 1)],
994
+ location,
995
+ };
836
996
  case "[]":
837
997
  advance();
838
998
  return { kind: "Always", expression: parseExpression(16), location };
@@ -909,6 +1069,10 @@ export const parseModule = (source: string): Module => {
909
1069
  }
910
1070
  }
911
1071
  advance();
1072
+ // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1073
+ if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1074
+ throw createError(`Precedence conflict between ${token.text} and -`, peek());
1075
+ }
912
1076
  const right = parseExpression(infix.high + 1);
913
1077
  const left = state.left;
914
1078
  if (token.text === "/\\" || token.text === "\\/") {
@@ -1004,9 +1168,13 @@ export const parseModule = (source: string): Module => {
1004
1168
  location,
1005
1169
  };
1006
1170
  }
1171
+ const definitionStart = cursor.index;
1007
1172
  const params = parseParams();
1008
1173
  expect("==");
1009
- if (isAt("INSTANCE")) return parseInstance(name, params, isLocal, location);
1174
+ if (isAt("INSTANCE")) {
1175
+ const instance = parseInstance(name, params, isLocal, location);
1176
+ return { ...instance, definitionRange: getRange(definitionStart) };
1177
+ }
1010
1178
  return parseOperator(name, params);
1011
1179
  };
1012
1180
 
@@ -1016,6 +1184,7 @@ export const parseModule = (source: string): Module => {
1016
1184
  isLocal: boolean,
1017
1185
  location: SourceLocation,
1018
1186
  ): Instance => {
1187
+ const start = cursor.index;
1019
1188
  expect("INSTANCE");
1020
1189
  const module = expectIdentifier();
1021
1190
  const substitutions = accept("WITH")
@@ -1025,7 +1194,16 @@ export const parseModule = (source: string): Module => {
1025
1194
  return { name: target, expression: parseArgument() };
1026
1195
  })
1027
1196
  : [];
1028
- return { kind: "Instance", name, params, module, substitutions, isLocal, location };
1197
+ return {
1198
+ kind: "Instance",
1199
+ name,
1200
+ params,
1201
+ module,
1202
+ substitutions,
1203
+ isLocal,
1204
+ location,
1205
+ instanceRange: getRange(start),
1206
+ };
1029
1207
  };
1030
1208
 
1031
1209
  const parseDeclaration = (): Declaration => {
@@ -1049,10 +1227,13 @@ export const parseModule = (source: string): Module => {
1049
1227
 
1050
1228
  const parseDeclarations = () => parseSeparated(parseDeclaration);
1051
1229
 
1052
- const declareRecursive = () =>
1053
- parseDeclarations().forEach((declaration) =>
1230
+ const declareRecursive = () => {
1231
+ const declarations = parseDeclarations();
1232
+ declarations.forEach((declaration) =>
1054
1233
  recursiveScopes[recursiveScopes.length - 1].add(declaration.name),
1055
1234
  );
1235
+ return declarations;
1236
+ };
1056
1237
 
1057
1238
  const getClosingOffset = (offset: number) => {
1058
1239
  const depth = { value: 0, offset };
@@ -1200,7 +1381,30 @@ export const parseModule = (source: string): Module => {
1200
1381
  module.variables.some((variable) => variable.name === name),
1201
1382
  );
1202
1383
 
1384
+ // tlapm's definable: names and operators joined by !.
1385
+ const parseDefinable = (): Fact => {
1386
+ const start = cursor.index;
1387
+ const token = peek();
1388
+ const parts = parseSeparated(() => {
1389
+ const part = peek();
1390
+ const isName = part.kind === "Identifier" && !isTlapmKeyword(part);
1391
+ if (!isName && part.kind !== "Operator") {
1392
+ throw createUnexpected(`Expected a definition name but found ${describe(part)}`, part);
1393
+ }
1394
+ cursor.index++;
1395
+ return part.text;
1396
+ }, "!");
1397
+ const location = getLocation(token);
1398
+ return withRange(
1399
+ parts.length === 1 && token.kind === "Operator"
1400
+ ? { kind: "OperatorReference", operator: token.text, location }
1401
+ : { kind: "Identifier", name: parts.join("!"), location },
1402
+ start,
1403
+ );
1404
+ };
1405
+
1203
1406
  const parseDefs = () => {
1407
+ if (isTlapm) return parseSeparated(parseDefinable);
1204
1408
  const defs = parseSeparated(parseFact);
1205
1409
  const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1206
1410
  if (invalid) {
@@ -1218,17 +1422,23 @@ export const parseModule = (source: string): Module => {
1218
1422
  const start = cursor.index;
1219
1423
  const keyword = advance();
1220
1424
  const isOnly = keyword.text === "USE" && accept("ONLY");
1425
+ const keywordRange = getRange(start);
1221
1426
  const use: UseOrHide = {
1222
1427
  kind: keyword.text === "USE" ? "Use" : "Hide",
1223
1428
  isOnly,
1224
1429
  ...parseFacts(),
1430
+ keyword: keywordRange,
1225
1431
  location: getLocation(keyword),
1226
1432
  };
1227
- const invalid = use.kind === "Hide" ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1228
- if (invalid) {
1229
- throw createError(
1230
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1231
- invalid.location,
1433
+ const invalid =
1434
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1435
+ if (invalid.length > 0) {
1436
+ throw createSanyError(
1437
+ invalid.map((fact) => ({
1438
+ location: formatRange(fact.location.range ?? getRange(start)),
1439
+ message:
1440
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1441
+ })),
1232
1442
  );
1233
1443
  }
1234
1444
  return withRange(use, start);
@@ -1240,7 +1450,7 @@ export const parseModule = (source: string): Module => {
1240
1450
  const isNew = accept("NEW");
1241
1451
  const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1242
1452
  if (!isNew && level === undefined) {
1243
- throw createError(`Expected a declaration but found ${describe(peek())}`);
1453
+ throw createUnexpected(`Expected a declaration but found ${describe(peek())}`);
1244
1454
  }
1245
1455
  if (level !== undefined) advance();
1246
1456
  if (level === "Variable") {
@@ -1347,7 +1557,7 @@ export const parseModule = (source: string): Module => {
1347
1557
  const parseStepStart = (isQed: boolean) => {
1348
1558
  const step = peek();
1349
1559
  if (step.kind !== "Step") {
1350
- throw createError(`Expected a step number but found ${describe(step)}`);
1560
+ throw createUnexpected(`Expected a step number but found ${describe(step)}`, step);
1351
1561
  }
1352
1562
  if (!isCorrectLevel(step)) {
1353
1563
  throw createError(`${isQed ? "QED step" : "step"}'s number has bad level`, step);
@@ -1356,7 +1566,7 @@ export const parseModule = (source: string): Module => {
1356
1566
  throw createError("<*> and <+> cannot be used for a named step", step);
1357
1567
  }
1358
1568
  cursor.index++;
1359
- return { name: getStepName(step), location: getLocation(step) };
1569
+ return { name: getStepName(step), token: step.text, location: getLocation(step) };
1360
1570
  };
1361
1571
 
1362
1572
  // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
@@ -1367,6 +1577,7 @@ export const parseModule = (source: string): Module => {
1367
1577
  const scope = new Set<string>();
1368
1578
  proofScopes.push(scope);
1369
1579
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1580
+ const head = getRange(start);
1370
1581
  const hasProof = isProofStart(peek());
1371
1582
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1372
1583
  throw createError("Proof of a step that does not take a proof");
@@ -1374,7 +1585,7 @@ export const parseModule = (source: string): Module => {
1374
1585
  const proof = hasProof ? parseProof() : undefined;
1375
1586
  proofScopes.pop();
1376
1587
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1377
- return withRange({ ...step, body, proof }, start);
1588
+ return withRange({ ...step, body, head, proof }, start);
1378
1589
  };
1379
1590
 
1380
1591
  const parseProofBody = (): Proof => {
@@ -1382,7 +1593,8 @@ export const parseModule = (source: string): Module => {
1382
1593
  if (isAt("BY") || (isAt("PROOF") && isAt("BY", 1))) {
1383
1594
  accept("PROOF");
1384
1595
  advance();
1385
- return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), location };
1596
+ const keyword = getRange(cursor.index - 1);
1597
+ return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
1386
1598
  }
1387
1599
  const terminal = peek(isAt("PROOF") ? 1 : 0);
1388
1600
  if (
@@ -1391,7 +1603,8 @@ export const parseModule = (source: string): Module => {
1391
1603
  ) {
1392
1604
  accept("PROOF");
1393
1605
  advance();
1394
- return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", location };
1606
+ const keyword = getRange(cursor.index - 1);
1607
+ return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
1395
1608
  }
1396
1609
  accept("PROOF");
1397
1610
  const steps = [parseStep()];
@@ -1421,9 +1634,12 @@ export const parseModule = (source: string): Module => {
1421
1634
  return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
1422
1635
  });
1423
1636
  const statement = parsed.statement;
1424
- module.theorems.push(
1425
- withRange({ name, statement, proof: parsed.proof, location: getLocation(keyword) }, start),
1637
+ const theorem = withRange(
1638
+ { name, statement, proof: parsed.proof, location: getLocation(keyword) },
1639
+ start,
1426
1640
  );
1641
+ module.theorems.push(theorem);
1642
+ module.units.push({ kind: "Theorem", theorem });
1427
1643
  if (name === undefined || statement.kind === "AssumeProve") return;
1428
1644
  module.definitions.push(
1429
1645
  withRange(
@@ -1467,7 +1683,8 @@ export const parseModule = (source: string): Module => {
1467
1683
  const module: Module = {
1468
1684
  name,
1469
1685
  modules: [],
1470
- extends: accept("EXTENDS") ? parseSeparated(expectIdentifier) : [],
1686
+ extends: [],
1687
+ extendsRanges: [],
1471
1688
  constants: [],
1472
1689
  variables: [],
1473
1690
  definitions: [],
@@ -1476,20 +1693,38 @@ export const parseModule = (source: string): Module => {
1476
1693
  location,
1477
1694
  theorems: [],
1478
1695
  uses: [],
1696
+ units: [],
1479
1697
  };
1480
1698
  openModules.push(module);
1481
1699
  recursiveScopes.push(new Set());
1700
+ if (accept("EXTENDS")) {
1701
+ parseSeparated(() => {
1702
+ const start = cursor.index;
1703
+ module.extends.push(expectIdentifier());
1704
+ module.extendsRanges.push(getRange(start));
1705
+ });
1706
+ }
1482
1707
  while (true) {
1483
1708
  const tokenIndex = cursor.index;
1709
+ unitStart.index = tokenIndex;
1484
1710
  const token = peek();
1485
1711
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1712
+ if (token.kind === "ModuleEnd") {
1713
+ location.range = {
1714
+ module: name,
1715
+ start: { line: location.line, column: location.column },
1716
+ end: { line: token.line, column: token.endColumn },
1717
+ };
1718
+ }
1486
1719
  openModules.pop();
1487
1720
  recursiveScopes.pop();
1488
1721
  return module;
1489
1722
  }
1490
1723
  if (token.kind === "Separator") {
1491
1724
  if (isAt("MODULE", 1)) {
1492
- module.modules.push(parseModuleBody());
1725
+ const submodule = parseModuleBody();
1726
+ module.modules.push(submodule);
1727
+ module.units.push({ kind: "Module", module: submodule });
1493
1728
  expect("====");
1494
1729
  continue;
1495
1730
  }
@@ -1498,26 +1733,31 @@ export const parseModule = (source: string): Module => {
1498
1733
  }
1499
1734
  switch (token.text) {
1500
1735
  case "CONSTANT":
1501
- case "CONSTANTS":
1736
+ case "CONSTANTS": {
1502
1737
  advance();
1503
- module.constants.push(...parseDeclarations());
1738
+ const declarations = parseDeclarations();
1739
+ module.constants.push(...declarations);
1740
+ module.units.push({ kind: "Constants", declarations });
1504
1741
  continue;
1742
+ }
1505
1743
  case "VARIABLE":
1506
- case "VARIABLES":
1744
+ case "VARIABLES": {
1507
1745
  advance();
1508
- module.variables.push(
1509
- ...parseSeparated(() => {
1510
- const start = cursor.index;
1511
- const location = getLocation(peek());
1512
- return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1513
- }),
1514
- );
1746
+ const declarations = parseSeparated(() => {
1747
+ const start = cursor.index;
1748
+ const location = getLocation(peek());
1749
+ return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1750
+ });
1751
+ module.variables.push(...declarations);
1752
+ module.units.push({ kind: "Variables", declarations });
1515
1753
  continue;
1754
+ }
1516
1755
  case "ASSUME":
1517
1756
  case "ASSUMPTION":
1518
1757
  case "AXIOM": {
1519
1758
  const assumption = parseAssumption();
1520
1759
  module.assumptions.push(assumption);
1760
+ module.units.push({ kind: "Assumption", assumption });
1521
1761
  if (assumption.name !== undefined) {
1522
1762
  module.definitions.push({
1523
1763
  kind: "Operator",
@@ -1532,29 +1772,32 @@ export const parseModule = (source: string): Module => {
1532
1772
  continue;
1533
1773
  }
1534
1774
  case "USE":
1535
- case "HIDE":
1536
- module.uses.push(parseUseOrHide());
1775
+ case "HIDE": {
1776
+ const use = parseUseOrHide();
1777
+ module.uses.push(use);
1778
+ module.units.push({ kind: "Use", use });
1537
1779
  continue;
1780
+ }
1538
1781
  case "RECURSIVE":
1539
1782
  advance();
1540
- declareRecursive();
1783
+ module.units.push({ kind: "Recursive", declarations: declareRecursive() });
1541
1784
  continue;
1542
1785
  case "INSTANCE":
1543
- module.instances.push(
1786
+ addDefinitionUnit(
1787
+ module,
1544
1788
  withRange(parseInstance(undefined, [], false, getLocation(token)), tokenIndex),
1545
1789
  );
1546
1790
  continue;
1547
1791
  case "LOCAL": {
1548
1792
  advance();
1549
1793
  if (isAt("INSTANCE")) {
1550
- module.instances.push(
1794
+ addDefinitionUnit(
1795
+ module,
1551
1796
  withRange(parseInstance(undefined, [], true, getLocation(token)), tokenIndex),
1552
1797
  );
1553
1798
  continue;
1554
1799
  }
1555
- const unit = parseDefinition(true);
1556
- if (unit.kind === "Instance") module.instances.push(unit);
1557
- else module.definitions.push(unit);
1800
+ addDefinitionUnit(module, parseDefinition(true));
1558
1801
  continue;
1559
1802
  }
1560
1803
  }
@@ -1562,11 +1805,23 @@ export const parseModule = (source: string): Module => {
1562
1805
  parseTheorem(module);
1563
1806
  continue;
1564
1807
  }
1565
- const unit = parseDefinition(false);
1566
- if (unit.kind === "Instance") module.instances.push(unit);
1567
- else module.definitions.push(unit);
1808
+ addDefinitionUnit(module, parseDefinition(false));
1809
+ }
1810
+ };
1811
+
1812
+ const addDefinitionUnit = (module: Module, unit: Definition | Instance) => {
1813
+ if (unit.kind === "Instance") {
1814
+ module.instances.push(unit);
1815
+ module.units.push({ kind: "Instance", instance: unit });
1816
+ return;
1568
1817
  }
1818
+ module.definitions.push(unit);
1819
+ module.units.push({ kind: "Definition", definition: unit });
1569
1820
  };
1570
1821
 
1571
1822
  return parseModuleBody();
1572
1823
  };
1824
+
1825
+ export const parseModule = (source: string) => parse(source, false);
1826
+
1827
+ export const parseTlapmModule = (source: string) => parse(source, true);