tla-ts 0.0.3-dev.c949d82 → 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/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 = () =>
@@ -666,9 +694,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
666
694
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
667
695
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
668
696
  const fields = parseSeparated(() => {
697
+ const fieldStart = cursor.index;
698
+ const fieldLocation = getLocation(peek());
669
699
  const name = expectIdentifier();
700
+ fieldLocation.range = getRange(fieldStart);
670
701
  expect(kind === "Record" ? "|->" : ":");
671
- return { name, value: parseExpression() };
702
+ return { name, value: parseExpression(), location: fieldLocation };
672
703
  });
673
704
  expect("]");
674
705
  return { kind, fields, location };
@@ -767,12 +798,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
767
798
  expect(":");
768
799
  return { kind, bounds, body: parseExpression(), location };
769
800
  }
770
- const names = parseSeparated(expectIdentifier);
801
+ const names = getNames(parseSeparated(expectName));
771
802
  expect(":");
772
803
  return {
773
804
  kind: "UnboundedQuantifier",
774
805
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
775
- names,
806
+ names: names.names,
807
+ nameRanges: names.ranges,
776
808
  body: parseExpression(),
777
809
  isTemporal,
778
810
  location,
@@ -827,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
827
859
  };
828
860
 
829
861
  const parsePrimary = (): Expression => {
830
- const isFactStart = cursor.index === factStart.index;
862
+ const start = cursor.index;
863
+ const isFactStart = start === factStart.index;
831
864
  const token = advance();
832
865
  const location = getLocation(token);
833
866
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -866,33 +899,65 @@ const parse = (source: string, isTlapm: boolean): Module => {
866
899
  const instance = openModules
867
900
  .flatMap((module) => module.instances)
868
901
  .find((candidate) => candidate.name === name);
869
- 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) {
870
908
  throw createError(
871
909
  `The operator ${name} requires ${instance.params.length} arguments`,
872
910
  token,
873
911
  );
874
912
  }
875
913
  const selectors: Selector[] = [{ kind: "Name", name, args }];
914
+ const selectorRanges = [getRange(start, start)];
876
915
  while (isSelectorAhead()) {
877
916
  advance();
917
+ selectorRanges.push(getRange(cursor.index, cursor.index));
878
918
  selectors.push(parseSelector());
879
919
  }
880
920
  const names = selectors.flatMap((selector) =>
881
921
  selector.kind === "Name" ? [selector.name] : [],
882
922
  );
883
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
884
923
  const operator = names.join("!");
885
924
  const qualifiedArgs = selectors.flatMap((selector) =>
886
925
  "args" in selector ? selector.args : [],
887
926
  );
888
- if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
889
927
  const argumentCounts = selectors.map((selector) =>
890
928
  "args" in selector ? selector.args.length : 0,
891
929
  );
892
930
  const argumentsRange = lastArguments.range;
893
- return argumentCounts.slice(0, -1).some((count) => count > 0)
894
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
895
- : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
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;
896
961
  }
897
962
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
898
963
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -1104,7 +1169,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1104
1169
  const body = parseExpression();
1105
1170
  const last = tokens[cursor.index - 1];
1106
1171
  const end = { line: last.line, column: last.endColumn };
1107
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1172
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1108
1173
  };
1109
1174
 
1110
1175
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1112,6 +1177,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1112
1177
  return withRange(parseDefinitionUnit(isLocal), start);
1113
1178
  };
1114
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
+
1115
1190
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1116
1191
  const token = peek();
1117
1192
  const location = getLocation(token);
@@ -1131,24 +1206,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
1131
1206
  peek(2).kind === "Identifier" &&
1132
1207
  isAt("==", 3)
1133
1208
  ) {
1134
- const left = expectIdentifier();
1209
+ const left = expectName();
1135
1210
  const operator = advance().text;
1136
- const right = expectIdentifier();
1211
+ const right = expectName();
1137
1212
  expect("==");
1138
- return parseOperator(operator, [
1139
- { name: left, arity: 0 },
1140
- { name: right, arity: 0 },
1141
- ]);
1213
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1142
1214
  }
1143
1215
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1144
1216
  const isPostfixDefinition =
1145
1217
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1146
1218
  if (isPrefixDefinition || isPostfixDefinition) {
1147
1219
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1148
- cursor.index += isPrefixDefinition ? 1 : 2;
1149
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1220
+ if (isPrefixDefinition) advance();
1221
+ const param = expectName();
1222
+ if (isPostfixDefinition) advance();
1150
1223
  expect("==");
1151
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1224
+ return parseOperator(operator, [toParam(param)]);
1152
1225
  }
1153
1226
  const name = expectIdentifier();
1154
1227
  if (accept("[")) {
@@ -1186,9 +1259,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1186
1259
  const module = expectIdentifier();
1187
1260
  const substitutions = accept("WITH")
1188
1261
  ? parseSeparated(() => {
1262
+ const targetStart = cursor.index;
1189
1263
  const target = advance().text;
1264
+ const nameRange = getRange(targetStart);
1190
1265
  expect("<-");
1191
- return { name: target, expression: parseArgument() };
1266
+ return { name: target, nameRange, expression: parseArgument() };
1192
1267
  })
1193
1268
  : [];
1194
1269
  return {
@@ -1204,6 +1279,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1204
1279
  };
1205
1280
 
1206
1281
  const parseDeclaration = (): Declaration => {
1282
+ const start = cursor.index;
1283
+ return withRange(parseDeclarationUnit(), start);
1284
+ };
1285
+
1286
+ const parseDeclarationUnit = (): Declaration => {
1207
1287
  const location = getLocation(peek());
1208
1288
  if (isAt("_") && peek(1).kind === "Operator") {
1209
1289
  advance();
@@ -1451,10 +1531,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1451
1531
  }
1452
1532
  if (level !== undefined) advance();
1453
1533
  if (level === "Variable") {
1454
- const name = expectIdentifier();
1455
- declareProofSymbol(name);
1534
+ const named = expectName();
1535
+ declareProofSymbol(named.name);
1456
1536
  return withRange(
1457
- { 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
+ },
1458
1546
  start,
1459
1547
  );
1460
1548
  }
@@ -1472,6 +1560,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1472
1560
  arity: declaration.arity,
1473
1561
  level: level ?? "Constant",
1474
1562
  set: isBounded ? parseExpression() : undefined,
1563
+ nameRange: declaration.location.range,
1475
1564
  location,
1476
1565
  },
1477
1566
  start,
@@ -1527,21 +1616,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1527
1616
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1528
1617
  if (accept("TAKE")) {
1529
1618
  const isBounded = isBoundAhead();
1530
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1619
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1531
1620
  const bounds = isBounded ? parseBounds() : [];
1532
- declareProofSymbols(names, bounds);
1533
- return { kind: "Take", names, bounds };
1621
+ declareProofSymbols(names.names, bounds);
1622
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1534
1623
  }
1535
1624
  if (accept("WITNESS")) {
1536
1625
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1537
1626
  }
1538
1627
  if (accept("PICK")) {
1539
1628
  const isNames = isNameListAhead(":");
1540
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1629
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1541
1630
  const bounds = isNames ? [] : parseBounds();
1542
- declareProofSymbols(names, bounds);
1631
+ declareProofSymbols(names.names, bounds);
1543
1632
  expect(":");
1544
- 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
+ };
1545
1640
  }
1546
1641
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1547
1642
  return {
@@ -1571,10 +1666,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1571
1666
  const parseStep = (): ProofStep => {
1572
1667
  const start = cursor.index;
1573
1668
  const step = parseStepStart(isAt("QED", 1));
1669
+ const statementStart = cursor.index;
1574
1670
  const scope = new Set<string>();
1575
1671
  proofScopes.push(scope);
1576
1672
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1577
1673
  const head = getRange(start);
1674
+ const statement = getRange(statementStart);
1578
1675
  const hasProof = isProofStart(peek());
1579
1676
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1580
1677
  throw createError("Proof of a step that does not take a proof");
@@ -1582,7 +1679,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1582
1679
  const proof = hasProof ? parseProof() : undefined;
1583
1680
  proofScopes.pop();
1584
1681
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1585
- return withRange({ ...step, body, head, proof }, start);
1682
+ return withRange({ ...step, body, head, statement, proof }, start);
1586
1683
  };
1587
1684
 
1588
1685
  const parseProofBody = (): Proof => {
@@ -1708,11 +1805,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
1708
1805
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1709
1806
  if (token.kind === "ModuleEnd") {
1710
1807
  location.range = {
1711
- module: name,
1808
+ module: openModules[0].name,
1712
1809
  start: { line: location.line, column: location.column },
1713
1810
  end: { line: token.line, column: token.endColumn },
1714
1811
  };
1715
1812
  }
1813
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1716
1814
  openModules.pop();
1717
1815
  recursiveScopes.pop();
1718
1816
  return module;
@@ -1794,7 +1892,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1794
1892
  );
1795
1893
  continue;
1796
1894
  }
1797
- addDefinitionUnit(module, parseDefinition(true));
1895
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1798
1896
  continue;
1799
1897
  }
1800
1898
  }