tla-ts 0.0.3-dev.037ac3b → 0.0.3-dev.0e500e5

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 (47) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +58 -22
  3. package/dist/index.d.ts +58 -22
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +2 -2
  7. package/dist/parallel.cjs +1 -1
  8. package/dist/parallel.js +2 -2
  9. package/dist/run-cli.cjs +14 -12
  10. package/dist/run-cli.js +22 -20
  11. package/dist/transfer.cjs +39 -21
  12. package/dist/transfer.js +39 -21
  13. package/package.json +1 -1
  14. package/src/ast.ts +26 -4
  15. package/src/builtins.ts +9 -3
  16. package/src/check.ts +309 -61
  17. package/src/codegen.ts +6 -3
  18. package/src/config.ts +224 -170
  19. package/src/environment.ts +223 -84
  20. package/src/errors.ts +100 -1
  21. package/src/evaluate.ts +172 -19
  22. package/src/exit-codes.ts +22 -3
  23. package/src/fingerprints.ts +2 -1
  24. package/src/instances.ts +1 -1
  25. package/src/levels.ts +176 -36
  26. package/src/live-check.ts +129 -49
  27. package/src/liveness.ts +68 -10
  28. package/src/parallel-worker.ts +2 -2
  29. package/src/parallel.ts +2 -2
  30. package/src/parser.ts +152 -48
  31. package/src/resolve.ts +878 -338
  32. package/src/run-cli.ts +61 -11
  33. package/src/sany-lexer.ts +615 -0
  34. package/src/sany-parse-error.ts +3143 -0
  35. package/src/spec-description.ts +1 -2
  36. package/src/standard-modules.ts +52 -38
  37. package/src/standard-ranges.ts +116 -0
  38. package/src/subexpressions.ts +25 -13
  39. package/src/substitution-levels.ts +38 -17
  40. package/src/successors.ts +111 -31
  41. package/src/symmetry.ts +16 -6
  42. package/src/tableau.ts +1 -1
  43. package/src/tlc-module.ts +6 -6
  44. package/src/tlc-modules.ts +4 -0
  45. package/src/tlc-order.ts +8 -4
  46. package/src/value-sources.ts +29 -0
  47. package/src/values.ts +184 -51
package/src/liveness.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isLocallyResolved, type Definition, type Expression } from "./ast";
2
- import { TlaConfigError, TlaEvaluationError } from "./errors";
3
- import { EXIT_STATUS, setExitCode } from "./exit-codes";
2
+ import { STACK_OVERFLOW, TlaEvaluationError, TlaModuleError, UNEXPECTED_EXCEPTION } from "./errors";
3
+ import { EXIT_STATUS, getOwnExitCode, setExitCode } from "./exit-codes";
4
4
  import {
5
5
  describeLocation,
6
6
  describeRange,
@@ -61,6 +61,23 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
61
61
  next: undefined,
62
62
  });
63
63
 
64
+ // TLC evaluates the constant parts of a property while it processes the spec. Its top-level handler
65
+ // prints a failure without a TLC error code of its own as an unexpected exception.
66
+ const evaluateConstant = (expression: Expression, context: Context) => {
67
+ try {
68
+ return evaluate(expression, context);
69
+ } catch (error) {
70
+ if (!(error instanceof TlaEvaluationError) || error instanceof TlaModuleError) throw error;
71
+ const code = getOwnExitCode(error);
72
+ throw code === undefined
73
+ ? setExitCode(
74
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
75
+ EXIT_STATUS.error,
76
+ )
77
+ : setExitCode(error, code);
78
+ }
79
+ };
80
+
64
81
  const createCannotHandleError = (expression: Expression) =>
65
82
  new TlaEvaluationError(
66
83
  `TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
@@ -191,14 +208,18 @@ export const expandTemporal = (
191
208
  getDefinitionLevel(definition, environment) > 0
192
209
  ) {
193
210
  if (expanding.has(definition)) {
194
- throw new TlaConfigError(`The definition of ${expression.name} refers to itself`);
211
+ throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
195
212
  }
196
- return expand(
213
+ const expanded = expand(
197
214
  definition.body,
198
215
  undefined,
199
216
  expression.name,
200
217
  new Set([...expanding, definition]),
201
218
  );
219
+ // TLC takes the body of a state-level definition in a specification as one initial predicate.
220
+ return !isProperty && getDefinitionLevel(definition, environment) < ACTION_LEVEL
221
+ ? [{ expression: definition.body, scope: undefined, name: expression.name }]
222
+ : expanded;
202
223
  }
203
224
  }
204
225
  const context = getConstantContext(environment, scope);
@@ -278,7 +299,7 @@ export const toLiveExpression = (
278
299
  ): LiveExpression => {
279
300
  const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
280
301
  if (atomLevel === 0) {
281
- const value = evaluate(expression, getConstantContext(environment, scope));
302
+ const value = evaluateConstant(expression, getConstantContext(environment, scope));
282
303
  if (typeof value !== "boolean") {
283
304
  throw new TlaEvaluationError(
284
305
  `TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
@@ -299,9 +320,7 @@ export const toLiveExpression = (
299
320
  scope: Scope | undefined,
300
321
  ) => {
301
322
  if (expansion.depth >= MAXIMUM_EXPANSION_DEPTH) {
302
- throw new TlaEvaluationError(
303
- `TLC overflowed the stack while expanding the temporal formula at ${describeLocation(expression)}`,
304
- );
323
+ throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
305
324
  }
306
325
  expansion.depth++;
307
326
  const result = convert(body, bodyScope);
@@ -356,9 +375,45 @@ export const toLiveExpression = (
356
375
  return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
357
376
  };
358
377
 
378
+ // TLC translates the operands of a connective even when the whole formula is a state or action
379
+ // predicate, so it fails on a constant operand before it checks any state.
380
+ const convertOperands = (expression: Expression, scope: Scope | undefined) => {
381
+ switch (expression.kind) {
382
+ case "Label":
383
+ convert(expression.body, scope);
384
+ return;
385
+ case "Let":
386
+ convert(expression.body, bindLetDefinitions(expression, scope, environment));
387
+ return;
388
+ case "And":
389
+ case "Or":
390
+ for (const operand of expression.operands) convert(operand, scope);
391
+ return;
392
+ case "Apply":
393
+ if (
394
+ expression.operator === "=>" ||
395
+ (expression.operator === "~" && !isLocallyResolved(expression))
396
+ ) {
397
+ for (const operand of expression.args) convert(operand, scope);
398
+ }
399
+ return;
400
+ case "If": {
401
+ const condition = convert(expression.condition, scope);
402
+ const branches =
403
+ condition.kind === "Boolean"
404
+ ? [condition.value ? expression.consequent : expression.alternative]
405
+ : [expression.consequent, expression.alternative];
406
+ for (const branch of branches) convert(branch, scope);
407
+ }
408
+ }
409
+ };
410
+
359
411
  const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
360
412
  const level = getLevel(expression, scope, environment);
361
- if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
413
+ if (level <= ACTION_LEVEL) {
414
+ convertOperands(expression, scope);
415
+ return toAtom(expression, scope, level);
416
+ }
362
417
  switch (expression.kind) {
363
418
  case "Label":
364
419
  return convert(expression.body, scope);
@@ -401,7 +456,10 @@ export const toLiveExpression = (
401
456
  return convertQuantifier(expression, scope);
402
457
  case "If": {
403
458
  if (getLevel(expression.condition, scope, environment) === 0) {
404
- const condition = evaluate(expression.condition, getConstantContext(environment, scope));
459
+ const condition = evaluateConstant(
460
+ expression.condition,
461
+ getConstantContext(environment, scope),
462
+ );
405
463
  if (typeof condition === "boolean") {
406
464
  return convert(condition ? expression.consequent : expression.alternative, scope);
407
465
  }
@@ -15,7 +15,7 @@ import type {
15
15
  WorkerMessage,
16
16
  } from "./parallel";
17
17
  import { getSuccessors, type Successor } from "./successors";
18
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
18
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
19
19
  import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
20
20
  import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
21
21
  import { getRankedStringCount, setStringOrder, type Value } from "./values";
@@ -61,7 +61,7 @@ const run = (start: StartMessage, parent: Port) => {
61
61
  });
62
62
  const environment = specification.environment;
63
63
  const search = createSearch(start.config, environment, () =>
64
- getTlcVariableOrder(start.source, loadModule),
64
+ getTlcDeclarationOrder(start.source, loadModule, "variable"),
65
65
  );
66
66
  const nextAction = search?.spec.next;
67
67
  if (!search || nextAction === undefined)
package/src/parallel.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  type SharedFingerprints,
21
21
  } from "./fingerprints";
22
22
  import { getInitialStates } from "./successors";
23
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
23
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
24
24
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
25
25
  import { createStateEncoder, type EncodedStates } from "./transfer";
26
26
  import { getRankedStringCount, setStringOrder, type Value } from "./values";
@@ -150,7 +150,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
150
150
  });
151
151
  const environment = specification.environment;
152
152
  const search = createSearch(config, environment, () =>
153
- getTlcVariableOrder(options.source, loadModule),
153
+ getTlcDeclarationOrder(options.source, loadModule, "variable"),
154
154
  );
155
155
  if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
156
156
  if (
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 {
@@ -315,6 +315,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
315
315
  const proofScopes: Set<string>[] = [];
316
316
  // A fact may cite an operator of a parameterized instance without the instance's arguments.
317
317
  const factStart = { index: -1 };
318
+ // SANY reports a location in a nested module as one in the module of its file.
318
319
  const openModules: Module[] = [];
319
320
 
320
321
  const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
@@ -329,15 +330,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
329
330
  const tokenEnds: SourceLocation[] = [];
330
331
  const tokenRanges: SourceRange[] = [];
331
332
 
332
- const getRange = (startIndex: number) => {
333
- const endIndex = cursor.index - 1;
333
+ const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
334
334
  const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
335
335
  if (cached) return cached;
336
336
  const last = tokens[endIndex];
337
337
  tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
338
338
  tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
339
339
  const range = {
340
- module: openModules[openModules.length - 1].name,
340
+ module: openModules[0].name,
341
341
  start: tokenStarts[startIndex],
342
342
  end: tokenEnds[endIndex],
343
343
  };
@@ -472,6 +472,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
472
472
  return token.text;
473
473
  };
474
474
 
475
+ const expectName = () => {
476
+ const start = cursor.index;
477
+ return { name: expectIdentifier(), range: getRange(start) };
478
+ };
479
+
480
+ const getNames = (names: { name: string; range: SourceRange }[]) => ({
481
+ names: names.map((item) => item.name),
482
+ ranges: names.map((item) => item.range),
483
+ });
484
+
475
485
  const withoutOffside = <Result>(parse: () => Result) => {
476
486
  cursor.offsideColumns.push(NO_OFFSIDE);
477
487
  const result = parse();
@@ -514,10 +524,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
514
524
  };
515
525
 
516
526
  const parsePattern = (): BoundPattern => {
517
- if (!accept("<<")) return { kind: "Name", name: expectIdentifier() };
518
- const names = parseSeparated(expectIdentifier);
527
+ if (!accept("<<")) {
528
+ const named = expectName();
529
+ return { kind: "Name", name: named.name, ranges: [named.range] };
530
+ }
531
+ const names = getNames(parseSeparated(expectName));
519
532
  expect(">>");
520
- return { kind: "Tuple", names };
533
+ return { kind: "Tuple", names: names.names, ranges: names.ranges };
521
534
  };
522
535
 
523
536
  const parseBounds = () =>
@@ -623,19 +636,38 @@ const parse = (source: string, isTlapm: boolean): Module => {
623
636
  if (isAt("LAMBDA")) {
624
637
  const start = cursor.index;
625
638
  advance();
626
- const params = parseSeparated(expectIdentifier);
639
+ const params = getNames(parseSeparated(expectName));
627
640
  expect(":");
628
641
  const body = parseExpression();
629
- return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
642
+ return withRange(
643
+ {
644
+ kind: "Lambda",
645
+ params: params.names,
646
+ paramRanges: params.ranges,
647
+ body,
648
+ location: getLocation(token),
649
+ },
650
+ start,
651
+ );
630
652
  }
631
- return parseExpression();
653
+ const argument = parseExpression();
654
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
655
+ // I!Op, as that operator, which takes the arguments of the instance too.
656
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
657
+ return argument;
632
658
  };
633
659
 
660
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
661
+
662
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
663
+
634
664
  const parseArguments = () =>
635
665
  withoutOffside(() => {
666
+ const start = cursor.index;
636
667
  expect("(");
637
668
  const args = parseSeparated(parseArgument);
638
669
  expect(")");
670
+ lastArguments.range = getRange(start);
639
671
  return args;
640
672
  });
641
673
 
@@ -662,9 +694,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
662
694
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
663
695
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
664
696
  const fields = parseSeparated(() => {
697
+ const fieldStart = cursor.index;
698
+ const fieldLocation = getLocation(peek());
665
699
  const name = expectIdentifier();
700
+ fieldLocation.range = getRange(fieldStart);
666
701
  expect(kind === "Record" ? "|->" : ":");
667
- return { name, value: parseExpression() };
702
+ return { name, value: parseExpression(), location: fieldLocation };
668
703
  });
669
704
  expect("]");
670
705
  return { kind, fields, location };
@@ -763,12 +798,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
763
798
  expect(":");
764
799
  return { kind, bounds, body: parseExpression(), location };
765
800
  }
766
- const names = parseSeparated(expectIdentifier);
801
+ const names = getNames(parseSeparated(expectName));
767
802
  expect(":");
768
803
  return {
769
804
  kind: "UnboundedQuantifier",
770
805
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
771
- names,
806
+ names: names.names,
807
+ nameRanges: names.ranges,
772
808
  body: parseExpression(),
773
809
  isTemporal,
774
810
  location,
@@ -823,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
823
859
  };
824
860
 
825
861
  const parsePrimary = (): Expression => {
826
- const isFactStart = cursor.index === factStart.index;
862
+ const start = cursor.index;
863
+ const isFactStart = start === factStart.index;
827
864
  const token = advance();
828
865
  const location = getLocation(token);
829
866
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -856,38 +893,71 @@ const parse = (source: string, isTlapm: boolean): Module => {
856
893
  const args = hasArguments ? parseArguments() : [];
857
894
  if (!isSelectorAhead()) {
858
895
  return hasArguments
859
- ? { kind: "Apply", operator: name, args, location }
896
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
860
897
  : { kind: "Identifier", name, location };
861
898
  }
862
899
  const instance = openModules
863
900
  .flatMap((module) => module.instances)
864
901
  .find((candidate) => candidate.name === name);
865
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
902
+ const isMissingArguments =
903
+ instance !== undefined &&
904
+ !isFactStart &&
905
+ !isTlapm &&
906
+ instance.params.length !== args.length;
907
+ if (isMissingArguments && args.length > 0) {
866
908
  throw createError(
867
909
  `The operator ${name} requires ${instance.params.length} arguments`,
868
910
  token,
869
911
  );
870
912
  }
871
913
  const selectors: Selector[] = [{ kind: "Name", name, args }];
914
+ const selectorRanges = [getRange(start, start)];
872
915
  while (isSelectorAhead()) {
873
916
  advance();
917
+ selectorRanges.push(getRange(cursor.index, cursor.index));
874
918
  selectors.push(parseSelector());
875
919
  }
876
920
  const names = selectors.flatMap((selector) =>
877
921
  selector.kind === "Name" ? [selector.name] : [],
878
922
  );
879
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
880
923
  const operator = names.join("!");
881
924
  const qualifiedArgs = selectors.flatMap((selector) =>
882
925
  "args" in selector ? selector.args : [],
883
926
  );
884
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
885
927
  const argumentCounts = selectors.map((selector) =>
886
928
  "args" in selector ? selector.args.length : 0,
887
929
  );
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 };
930
+ const argumentsRange = lastArguments.range;
931
+ const reference: Expression =
932
+ names.length < selectors.length
933
+ ? { kind: "Subexpression", selectors, selectorRanges, location }
934
+ : qualifiedArgs.length === 0
935
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
936
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
937
+ ? {
938
+ kind: "Apply",
939
+ operator,
940
+ args: qualifiedArgs,
941
+ argumentCounts,
942
+ argumentsRange,
943
+ selectorRanges,
944
+ location,
945
+ }
946
+ : {
947
+ kind: "Apply",
948
+ operator,
949
+ args: qualifiedArgs,
950
+ argumentsRange,
951
+ selectorRanges,
952
+ location,
953
+ };
954
+ if (isMissingArguments) {
955
+ missingArguments.set(reference, {
956
+ location: formatRange(getRange(start)),
957
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
958
+ });
959
+ }
960
+ return reference;
891
961
  }
892
962
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
893
963
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -1099,7 +1169,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1099
1169
  const body = parseExpression();
1100
1170
  const last = tokens[cursor.index - 1];
1101
1171
  const end = { line: last.line, column: last.endColumn };
1102
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1172
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1103
1173
  };
1104
1174
 
1105
1175
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1107,6 +1177,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1107
1177
  return withRange(parseDefinitionUnit(isLocal), start);
1108
1178
  };
1109
1179
 
1180
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1181
+ name: named.name,
1182
+ arity: 0,
1183
+ location: {
1184
+ line: named.range.start.line,
1185
+ column: named.range.start.column,
1186
+ range: named.range,
1187
+ },
1188
+ });
1189
+
1110
1190
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1111
1191
  const token = peek();
1112
1192
  const location = getLocation(token);
@@ -1126,24 +1206,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
1126
1206
  peek(2).kind === "Identifier" &&
1127
1207
  isAt("==", 3)
1128
1208
  ) {
1129
- const left = expectIdentifier();
1209
+ const left = expectName();
1130
1210
  const operator = advance().text;
1131
- const right = expectIdentifier();
1211
+ const right = expectName();
1132
1212
  expect("==");
1133
- return parseOperator(operator, [
1134
- { name: left, arity: 0 },
1135
- { name: right, arity: 0 },
1136
- ]);
1213
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1137
1214
  }
1138
1215
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1139
1216
  const isPostfixDefinition =
1140
1217
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1141
1218
  if (isPrefixDefinition || isPostfixDefinition) {
1142
1219
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1143
- cursor.index += isPrefixDefinition ? 1 : 2;
1144
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1220
+ if (isPrefixDefinition) advance();
1221
+ const param = expectName();
1222
+ if (isPostfixDefinition) advance();
1145
1223
  expect("==");
1146
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1224
+ return parseOperator(operator, [toParam(param)]);
1147
1225
  }
1148
1226
  const name = expectIdentifier();
1149
1227
  if (accept("[")) {
@@ -1199,6 +1277,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1199
1277
  };
1200
1278
 
1201
1279
  const parseDeclaration = (): Declaration => {
1280
+ const start = cursor.index;
1281
+ return withRange(parseDeclarationUnit(), start);
1282
+ };
1283
+
1284
+ const parseDeclarationUnit = (): Declaration => {
1202
1285
  const location = getLocation(peek());
1203
1286
  if (isAt("_") && peek(1).kind === "Operator") {
1204
1287
  advance();
@@ -1423,11 +1506,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1506
  location: getLocation(keyword),
1424
1507
  };
1425
1508
  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,
1509
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1510
+ if (invalid.length > 0) {
1511
+ throw createSanyError(
1512
+ invalid.map((fact) => ({
1513
+ location: formatRange(fact.location.range ?? getRange(start)),
1514
+ message:
1515
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1516
+ })),
1431
1517
  );
1432
1518
  }
1433
1519
  return withRange(use, start);
@@ -1443,10 +1529,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1443
1529
  }
1444
1530
  if (level !== undefined) advance();
1445
1531
  if (level === "Variable") {
1446
- const name = expectIdentifier();
1447
- declareProofSymbol(name);
1532
+ const named = expectName();
1533
+ declareProofSymbol(named.name);
1448
1534
  return withRange(
1449
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1535
+ {
1536
+ kind: "NewSymbol",
1537
+ name: named.name,
1538
+ arity: 0,
1539
+ level,
1540
+ set: undefined,
1541
+ nameRange: named.range,
1542
+ location,
1543
+ },
1450
1544
  start,
1451
1545
  );
1452
1546
  }
@@ -1464,6 +1558,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1464
1558
  arity: declaration.arity,
1465
1559
  level: level ?? "Constant",
1466
1560
  set: isBounded ? parseExpression() : undefined,
1561
+ nameRange: declaration.location.range,
1467
1562
  location,
1468
1563
  },
1469
1564
  start,
@@ -1519,21 +1614,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1519
1614
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1520
1615
  if (accept("TAKE")) {
1521
1616
  const isBounded = isBoundAhead();
1522
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1617
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1523
1618
  const bounds = isBounded ? parseBounds() : [];
1524
- declareProofSymbols(names, bounds);
1525
- return { kind: "Take", names, bounds };
1619
+ declareProofSymbols(names.names, bounds);
1620
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1526
1621
  }
1527
1622
  if (accept("WITNESS")) {
1528
1623
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1529
1624
  }
1530
1625
  if (accept("PICK")) {
1531
1626
  const isNames = isNameListAhead(":");
1532
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1627
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1533
1628
  const bounds = isNames ? [] : parseBounds();
1534
- declareProofSymbols(names, bounds);
1629
+ declareProofSymbols(names.names, bounds);
1535
1630
  expect(":");
1536
- return { kind: "Pick", names, bounds, body: parseExpression() };
1631
+ return {
1632
+ kind: "Pick",
1633
+ names: names.names,
1634
+ nameRanges: names.ranges,
1635
+ bounds,
1636
+ body: parseExpression(),
1637
+ };
1537
1638
  }
1538
1639
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1539
1640
  return {
@@ -1563,10 +1664,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1664
  const parseStep = (): ProofStep => {
1564
1665
  const start = cursor.index;
1565
1666
  const step = parseStepStart(isAt("QED", 1));
1667
+ const statementStart = cursor.index;
1566
1668
  const scope = new Set<string>();
1567
1669
  proofScopes.push(scope);
1568
1670
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
1671
  const head = getRange(start);
1672
+ const statement = getRange(statementStart);
1570
1673
  const hasProof = isProofStart(peek());
1571
1674
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
1675
  throw createError("Proof of a step that does not take a proof");
@@ -1574,7 +1677,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1677
  const proof = hasProof ? parseProof() : undefined;
1575
1678
  proofScopes.pop();
1576
1679
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
- return withRange({ ...step, body, head, proof }, start);
1680
+ return withRange({ ...step, body, head, statement, proof }, start);
1578
1681
  };
1579
1682
 
1580
1683
  const parseProofBody = (): Proof => {
@@ -1700,11 +1803,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1700
1803
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1701
1804
  if (token.kind === "ModuleEnd") {
1702
1805
  location.range = {
1703
- module: name,
1806
+ module: openModules[0].name,
1704
1807
  start: { line: location.line, column: location.column },
1705
1808
  end: { line: token.line, column: token.endColumn },
1706
1809
  };
1707
1810
  }
1811
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1708
1812
  openModules.pop();
1709
1813
  recursiveScopes.pop();
1710
1814
  return module;
@@ -1786,7 +1890,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1786
1890
  );
1787
1891
  continue;
1788
1892
  }
1789
- addDefinitionUnit(module, parseDefinition(true));
1893
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1790
1894
  continue;
1791
1895
  }
1792
1896
  }