tla-ts 0.0.3-dev.d9ff6f2 → 0.0.3-dev.e5b05b9

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/parser.ts CHANGED
@@ -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,14 +636,29 @@ 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
+
634
662
  const lastArguments: { range: SourceRange | undefined } = { range: undefined };
635
663
 
636
664
  const parseArguments = () =>
@@ -770,12 +798,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
770
798
  expect(":");
771
799
  return { kind, bounds, body: parseExpression(), location };
772
800
  }
773
- const names = parseSeparated(expectIdentifier);
801
+ const names = getNames(parseSeparated(expectName));
774
802
  expect(":");
775
803
  return {
776
804
  kind: "UnboundedQuantifier",
777
805
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
778
- names,
806
+ names: names.names,
807
+ nameRanges: names.ranges,
779
808
  body: parseExpression(),
780
809
  isTemporal,
781
810
  location,
@@ -830,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
830
859
  };
831
860
 
832
861
  const parsePrimary = (): Expression => {
833
- const isFactStart = cursor.index === factStart.index;
862
+ const start = cursor.index;
863
+ const isFactStart = start === factStart.index;
834
864
  const token = advance();
835
865
  const location = getLocation(token);
836
866
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -854,13 +884,21 @@ const parse = (source: string, isTlapm: boolean): Module => {
854
884
  if (token.kind === "Identifier" || isStep) {
855
885
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
856
886
  if (isLabelAhead()) {
857
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
858
- if (params.length > 0) expect(")");
887
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
888
+ if (params.names.length > 0) expect(")");
859
889
  expect("::");
860
- return { kind: "Label", name, params, body: parseExpression(), location };
890
+ return {
891
+ kind: "Label",
892
+ name,
893
+ params: params.names,
894
+ paramRanges: params.ranges,
895
+ body: parseExpression(),
896
+ location,
897
+ };
861
898
  }
862
899
  const hasArguments = isAt("(");
863
900
  const args = hasArguments ? parseArguments() : [];
901
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
864
902
  if (!isSelectorAhead()) {
865
903
  return hasArguments
866
904
  ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
@@ -869,33 +907,69 @@ const parse = (source: string, isTlapm: boolean): Module => {
869
907
  const instance = openModules
870
908
  .flatMap((module) => module.instances)
871
909
  .find((candidate) => candidate.name === name);
872
- if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
910
+ const isMissingArguments =
911
+ instance !== undefined &&
912
+ !isFactStart &&
913
+ !isTlapm &&
914
+ instance.params.length !== args.length;
915
+ if (isMissingArguments && args.length > 0) {
873
916
  throw createError(
874
917
  `The operator ${name} requires ${instance.params.length} arguments`,
875
918
  token,
876
919
  );
877
920
  }
878
921
  const selectors: Selector[] = [{ kind: "Name", name, args }];
922
+ const selectorRanges = [getRange(start, start)];
879
923
  while (isSelectorAhead()) {
880
924
  advance();
925
+ selectorRanges.push(getRange(cursor.index, cursor.index));
926
+ lastArguments.range = undefined;
881
927
  selectors.push(parseSelector());
928
+ argumentRanges.push(lastArguments.range);
882
929
  }
883
930
  const names = selectors.flatMap((selector) =>
884
931
  selector.kind === "Name" ? [selector.name] : [],
885
932
  );
886
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
887
933
  const operator = names.join("!");
888
934
  const qualifiedArgs = selectors.flatMap((selector) =>
889
935
  "args" in selector ? selector.args : [],
890
936
  );
891
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
892
937
  const argumentCounts = selectors.map((selector) =>
893
938
  "args" in selector ? selector.args.length : 0,
894
939
  );
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 };
940
+ const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
941
+ const reference: Expression =
942
+ names.length < selectors.length
943
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
944
+ : qualifiedArgs.length === 0
945
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
946
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
947
+ ? {
948
+ kind: "Apply",
949
+ operator,
950
+ args: qualifiedArgs,
951
+ argumentCounts,
952
+ argumentsRange,
953
+ selectorRanges,
954
+ argumentRanges,
955
+ location,
956
+ }
957
+ : {
958
+ kind: "Apply",
959
+ operator,
960
+ args: qualifiedArgs,
961
+ argumentsRange,
962
+ selectorRanges,
963
+ argumentRanges,
964
+ location,
965
+ };
966
+ if (isMissingArguments) {
967
+ missingArguments.set(reference, {
968
+ location: formatRange(getRange(start)),
969
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
970
+ });
971
+ }
972
+ return reference;
899
973
  }
900
974
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
901
975
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -1107,7 +1181,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1107
1181
  const body = parseExpression();
1108
1182
  const last = tokens[cursor.index - 1];
1109
1183
  const end = { line: last.line, column: last.endColumn };
1110
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1184
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1111
1185
  };
1112
1186
 
1113
1187
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1115,6 +1189,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1115
1189
  return withRange(parseDefinitionUnit(isLocal), start);
1116
1190
  };
1117
1191
 
1192
+ const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1193
+ name: named.name,
1194
+ arity: 0,
1195
+ location: {
1196
+ line: named.range.start.line,
1197
+ column: named.range.start.column,
1198
+ range: named.range,
1199
+ },
1200
+ });
1201
+
1118
1202
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1119
1203
  const token = peek();
1120
1204
  const location = getLocation(token);
@@ -1134,24 +1218,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
1134
1218
  peek(2).kind === "Identifier" &&
1135
1219
  isAt("==", 3)
1136
1220
  ) {
1137
- const left = expectIdentifier();
1221
+ const left = expectName();
1138
1222
  const operator = advance().text;
1139
- const right = expectIdentifier();
1223
+ const right = expectName();
1140
1224
  expect("==");
1141
- return parseOperator(operator, [
1142
- { name: left, arity: 0 },
1143
- { name: right, arity: 0 },
1144
- ]);
1225
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1145
1226
  }
1146
1227
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1147
1228
  const isPostfixDefinition =
1148
1229
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1149
1230
  if (isPrefixDefinition || isPostfixDefinition) {
1150
1231
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1151
- cursor.index += isPrefixDefinition ? 1 : 2;
1152
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1232
+ if (isPrefixDefinition) advance();
1233
+ const param = expectName();
1234
+ if (isPostfixDefinition) advance();
1153
1235
  expect("==");
1154
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1236
+ return parseOperator(operator, [toParam(param)]);
1155
1237
  }
1156
1238
  const name = expectIdentifier();
1157
1239
  if (accept("[")) {
@@ -1189,9 +1271,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1189
1271
  const module = expectIdentifier();
1190
1272
  const substitutions = accept("WITH")
1191
1273
  ? parseSeparated(() => {
1274
+ const targetStart = cursor.index;
1192
1275
  const target = advance().text;
1276
+ const nameRange = getRange(targetStart);
1193
1277
  expect("<-");
1194
- return { name: target, expression: parseArgument() };
1278
+ return { name: target, nameRange, expression: parseArgument() };
1195
1279
  })
1196
1280
  : [];
1197
1281
  return {
@@ -1207,6 +1291,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1207
1291
  };
1208
1292
 
1209
1293
  const parseDeclaration = (): Declaration => {
1294
+ const start = cursor.index;
1295
+ return withRange(parseDeclarationUnit(), start);
1296
+ };
1297
+
1298
+ const parseDeclarationUnit = (): Declaration => {
1210
1299
  const location = getLocation(peek());
1211
1300
  if (isAt("_") && peek(1).kind === "Operator") {
1212
1301
  advance();
@@ -1454,10 +1543,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1454
1543
  }
1455
1544
  if (level !== undefined) advance();
1456
1545
  if (level === "Variable") {
1457
- const name = expectIdentifier();
1458
- declareProofSymbol(name);
1546
+ const named = expectName();
1547
+ declareProofSymbol(named.name);
1459
1548
  return withRange(
1460
- { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1549
+ {
1550
+ kind: "NewSymbol",
1551
+ name: named.name,
1552
+ arity: 0,
1553
+ level,
1554
+ set: undefined,
1555
+ nameRange: named.range,
1556
+ location,
1557
+ },
1461
1558
  start,
1462
1559
  );
1463
1560
  }
@@ -1475,6 +1572,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1475
1572
  arity: declaration.arity,
1476
1573
  level: level ?? "Constant",
1477
1574
  set: isBounded ? parseExpression() : undefined,
1575
+ nameRange: declaration.location.range,
1478
1576
  location,
1479
1577
  },
1480
1578
  start,
@@ -1530,21 +1628,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1530
1628
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1531
1629
  if (accept("TAKE")) {
1532
1630
  const isBounded = isBoundAhead();
1533
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1631
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1534
1632
  const bounds = isBounded ? parseBounds() : [];
1535
- declareProofSymbols(names, bounds);
1536
- return { kind: "Take", names, bounds };
1633
+ declareProofSymbols(names.names, bounds);
1634
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1537
1635
  }
1538
1636
  if (accept("WITNESS")) {
1539
1637
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1540
1638
  }
1541
1639
  if (accept("PICK")) {
1542
1640
  const isNames = isNameListAhead(":");
1543
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1641
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1544
1642
  const bounds = isNames ? [] : parseBounds();
1545
- declareProofSymbols(names, bounds);
1643
+ declareProofSymbols(names.names, bounds);
1546
1644
  expect(":");
1547
- return { kind: "Pick", names, bounds, body: parseExpression() };
1645
+ return {
1646
+ kind: "Pick",
1647
+ names: names.names,
1648
+ nameRanges: names.ranges,
1649
+ bounds,
1650
+ body: parseExpression(),
1651
+ };
1548
1652
  }
1549
1653
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1550
1654
  return {
@@ -1574,10 +1678,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1574
1678
  const parseStep = (): ProofStep => {
1575
1679
  const start = cursor.index;
1576
1680
  const step = parseStepStart(isAt("QED", 1));
1681
+ const statementStart = cursor.index;
1577
1682
  const scope = new Set<string>();
1578
1683
  proofScopes.push(scope);
1579
1684
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1580
1685
  const head = getRange(start);
1686
+ const statement = getRange(statementStart);
1581
1687
  const hasProof = isProofStart(peek());
1582
1688
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1583
1689
  throw createError("Proof of a step that does not take a proof");
@@ -1585,7 +1691,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1585
1691
  const proof = hasProof ? parseProof() : undefined;
1586
1692
  proofScopes.pop();
1587
1693
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1588
- return withRange({ ...step, body, head, proof }, start);
1694
+ return withRange({ ...step, body, head, statement, proof }, start);
1589
1695
  };
1590
1696
 
1591
1697
  const parseProofBody = (): Proof => {
@@ -1711,11 +1817,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1711
1817
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1712
1818
  if (token.kind === "ModuleEnd") {
1713
1819
  location.range = {
1714
- module: name,
1820
+ module: openModules[0].name,
1715
1821
  start: { line: location.line, column: location.column },
1716
1822
  end: { line: token.line, column: token.endColumn },
1717
1823
  };
1718
1824
  }
1825
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1719
1826
  openModules.pop();
1720
1827
  recursiveScopes.pop();
1721
1828
  return module;
@@ -1797,7 +1904,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1797
1904
  );
1798
1905
  continue;
1799
1906
  }
1800
- addDefinitionUnit(module, parseDefinition(true));
1907
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1801
1908
  continue;
1802
1909
  }
1803
1910
  }