tla-ts 0.0.3-dev.cf3b82c → 0.0.3-dev.cfd469d

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.
package/src/liveness.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isLocallyResolved, type Definition, type Expression } from "./ast";
2
- import { STACK_OVERFLOW, 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)}`,
@@ -193,12 +210,16 @@ export const expandTemporal = (
193
210
  if (expanding.has(definition)) {
194
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)}`,
@@ -354,9 +375,45 @@ export const toLiveExpression = (
354
375
  return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
355
376
  };
356
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
+
357
411
  const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
358
412
  const level = getLevel(expression, scope, environment);
359
- 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
+ }
360
417
  switch (expression.kind) {
361
418
  case "Label":
362
419
  return convert(expression.body, scope);
@@ -399,7 +456,10 @@ export const toLiveExpression = (
399
456
  return convertQuantifier(expression, scope);
400
457
  case "If": {
401
458
  if (getLevel(expression.condition, scope, environment) === 0) {
402
- const condition = evaluate(expression.condition, getConstantContext(environment, scope));
459
+ const condition = evaluateConstant(
460
+ expression.condition,
461
+ getConstantContext(environment, scope),
462
+ );
403
463
  if (typeof condition === "boolean") {
404
464
  return convert(condition ? expression.consequent : expression.alternative, scope);
405
465
  }
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("[")) {
@@ -1181,9 +1259,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1181
1259
  const module = expectIdentifier();
1182
1260
  const substitutions = accept("WITH")
1183
1261
  ? parseSeparated(() => {
1262
+ const targetStart = cursor.index;
1184
1263
  const target = advance().text;
1264
+ const nameRange = getRange(targetStart);
1185
1265
  expect("<-");
1186
- return { name: target, expression: parseArgument() };
1266
+ return { name: target, nameRange, expression: parseArgument() };
1187
1267
  })
1188
1268
  : [];
1189
1269
  return {
@@ -1199,6 +1279,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1199
1279
  };
1200
1280
 
1201
1281
  const parseDeclaration = (): Declaration => {
1282
+ const start = cursor.index;
1283
+ return withRange(parseDeclarationUnit(), start);
1284
+ };
1285
+
1286
+ const parseDeclarationUnit = (): Declaration => {
1202
1287
  const location = getLocation(peek());
1203
1288
  if (isAt("_") && peek(1).kind === "Operator") {
1204
1289
  advance();
@@ -1423,11 +1508,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1508
  location: getLocation(keyword),
1424
1509
  };
1425
1510
  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,
1511
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1512
+ if (invalid.length > 0) {
1513
+ throw createSanyError(
1514
+ invalid.map((fact) => ({
1515
+ location: formatRange(fact.location.range ?? getRange(start)),
1516
+ message:
1517
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1518
+ })),
1431
1519
  );
1432
1520
  }
1433
1521
  return withRange(use, start);
@@ -1443,10 +1531,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1443
1531
  }
1444
1532
  if (level !== undefined) advance();
1445
1533
  if (level === "Variable") {
1446
- const name = expectIdentifier();
1447
- declareProofSymbol(name);
1534
+ const named = expectName();
1535
+ declareProofSymbol(named.name);
1448
1536
  return withRange(
1449
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1537
+ {
1538
+ kind: "NewSymbol",
1539
+ name: named.name,
1540
+ arity: 0,
1541
+ level,
1542
+ set: undefined,
1543
+ nameRange: named.range,
1544
+ location,
1545
+ },
1450
1546
  start,
1451
1547
  );
1452
1548
  }
@@ -1464,6 +1560,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1464
1560
  arity: declaration.arity,
1465
1561
  level: level ?? "Constant",
1466
1562
  set: isBounded ? parseExpression() : undefined,
1563
+ nameRange: declaration.location.range,
1467
1564
  location,
1468
1565
  },
1469
1566
  start,
@@ -1519,21 +1616,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1519
1616
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1520
1617
  if (accept("TAKE")) {
1521
1618
  const isBounded = isBoundAhead();
1522
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1619
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1523
1620
  const bounds = isBounded ? parseBounds() : [];
1524
- declareProofSymbols(names, bounds);
1525
- return { kind: "Take", names, bounds };
1621
+ declareProofSymbols(names.names, bounds);
1622
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1526
1623
  }
1527
1624
  if (accept("WITNESS")) {
1528
1625
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1529
1626
  }
1530
1627
  if (accept("PICK")) {
1531
1628
  const isNames = isNameListAhead(":");
1532
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1629
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1533
1630
  const bounds = isNames ? [] : parseBounds();
1534
- declareProofSymbols(names, bounds);
1631
+ declareProofSymbols(names.names, bounds);
1535
1632
  expect(":");
1536
- return { kind: "Pick", names, bounds, body: parseExpression() };
1633
+ return {
1634
+ kind: "Pick",
1635
+ names: names.names,
1636
+ nameRanges: names.ranges,
1637
+ bounds,
1638
+ body: parseExpression(),
1639
+ };
1537
1640
  }
1538
1641
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1539
1642
  return {
@@ -1563,10 +1666,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1563
1666
  const parseStep = (): ProofStep => {
1564
1667
  const start = cursor.index;
1565
1668
  const step = parseStepStart(isAt("QED", 1));
1669
+ const statementStart = cursor.index;
1566
1670
  const scope = new Set<string>();
1567
1671
  proofScopes.push(scope);
1568
1672
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
1673
  const head = getRange(start);
1674
+ const statement = getRange(statementStart);
1570
1675
  const hasProof = isProofStart(peek());
1571
1676
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
1677
  throw createError("Proof of a step that does not take a proof");
@@ -1574,7 +1679,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1679
  const proof = hasProof ? parseProof() : undefined;
1575
1680
  proofScopes.pop();
1576
1681
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
- return withRange({ ...step, body, head, proof }, start);
1682
+ return withRange({ ...step, body, head, statement, proof }, start);
1578
1683
  };
1579
1684
 
1580
1685
  const parseProofBody = (): Proof => {
@@ -1700,11 +1805,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1700
1805
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1701
1806
  if (token.kind === "ModuleEnd") {
1702
1807
  location.range = {
1703
- module: name,
1808
+ module: openModules[0].name,
1704
1809
  start: { line: location.line, column: location.column },
1705
1810
  end: { line: token.line, column: token.endColumn },
1706
1811
  };
1707
1812
  }
1813
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1708
1814
  openModules.pop();
1709
1815
  recursiveScopes.pop();
1710
1816
  return module;
@@ -1786,7 +1892,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1786
1892
  );
1787
1893
  continue;
1788
1894
  }
1789
- addDefinitionUnit(module, parseDefinition(true));
1895
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1790
1896
  continue;
1791
1897
  }
1792
1898
  }