tla-ts 0.0.3-dev.3504663 → 0.0.3-dev.3539317

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);
@@ -336,7 +337,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
336
337
  tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
337
338
  tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
338
339
  const range = {
339
- module: openModules[openModules.length - 1].name,
340
+ module: openModules[0].name,
340
341
  start: tokenStarts[startIndex],
341
342
  end: tokenEnds[endIndex],
342
343
  };
@@ -471,6 +472,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
471
472
  return token.text;
472
473
  };
473
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
+
474
485
  const withoutOffside = <Result>(parse: () => Result) => {
475
486
  cursor.offsideColumns.push(NO_OFFSIDE);
476
487
  const result = parse();
@@ -513,10 +524,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
513
524
  };
514
525
 
515
526
  const parsePattern = (): BoundPattern => {
516
- if (!accept("<<")) return { kind: "Name", name: expectIdentifier() };
517
- 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));
518
532
  expect(">>");
519
- return { kind: "Tuple", names };
533
+ return { kind: "Tuple", names: names.names, ranges: names.ranges };
520
534
  };
521
535
 
522
536
  const parseBounds = () =>
@@ -622,10 +636,19 @@ const parse = (source: string, isTlapm: boolean): Module => {
622
636
  if (isAt("LAMBDA")) {
623
637
  const start = cursor.index;
624
638
  advance();
625
- const params = parseSeparated(expectIdentifier);
639
+ const params = getNames(parseSeparated(expectName));
626
640
  expect(":");
627
641
  const body = parseExpression();
628
- 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
+ );
629
652
  }
630
653
  const argument = parseExpression();
631
654
  // SANY reads an argument that only names an operator of a parameterized instance, such as
@@ -775,12 +798,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
775
798
  expect(":");
776
799
  return { kind, bounds, body: parseExpression(), location };
777
800
  }
778
- const names = parseSeparated(expectIdentifier);
801
+ const names = getNames(parseSeparated(expectName));
779
802
  expect(":");
780
803
  return {
781
804
  kind: "UnboundedQuantifier",
782
805
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
783
- names,
806
+ names: names.names,
807
+ nameRanges: names.ranges,
784
808
  body: parseExpression(),
785
809
  isTemporal,
786
810
  location,
@@ -860,13 +884,21 @@ const parse = (source: string, isTlapm: boolean): Module => {
860
884
  if (token.kind === "Identifier" || isStep) {
861
885
  const name = isStep ? (getStepName(token) ?? token.text) : token.text;
862
886
  if (isLabelAhead()) {
863
- const params = accept("(") ? parseSeparated(expectIdentifier) : [];
864
- if (params.length > 0) expect(")");
887
+ const params = getNames(accept("(") ? parseSeparated(expectName) : []);
888
+ if (params.names.length > 0) expect(")");
865
889
  expect("::");
866
- 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
+ };
867
898
  }
868
899
  const hasArguments = isAt("(");
869
900
  const args = hasArguments ? parseArguments() : [];
901
+ const argumentRanges = [hasArguments ? lastArguments.range : undefined];
870
902
  if (!isSelectorAhead()) {
871
903
  return hasArguments
872
904
  ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
@@ -891,7 +923,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
891
923
  while (isSelectorAhead()) {
892
924
  advance();
893
925
  selectorRanges.push(getRange(cursor.index, cursor.index));
926
+ lastArguments.range = undefined;
894
927
  selectors.push(parseSelector());
928
+ argumentRanges.push(lastArguments.range);
895
929
  }
896
930
  const names = selectors.flatMap((selector) =>
897
931
  selector.kind === "Name" ? [selector.name] : [],
@@ -903,10 +937,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
903
937
  const argumentCounts = selectors.map((selector) =>
904
938
  "args" in selector ? selector.args.length : 0,
905
939
  );
906
- const argumentsRange = lastArguments.range;
940
+ const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
907
941
  const reference: Expression =
908
942
  names.length < selectors.length
909
- ? { kind: "Subexpression", selectors, selectorRanges, location }
943
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
910
944
  : qualifiedArgs.length === 0
911
945
  ? { kind: "Identifier", name: operator, selectorRanges, location }
912
946
  : argumentCounts.slice(0, -1).some((count) => count > 0)
@@ -917,6 +951,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
917
951
  argumentCounts,
918
952
  argumentsRange,
919
953
  selectorRanges,
954
+ argumentRanges,
920
955
  location,
921
956
  }
922
957
  : {
@@ -925,6 +960,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
925
960
  args: qualifiedArgs,
926
961
  argumentsRange,
927
962
  selectorRanges,
963
+ argumentRanges,
928
964
  location,
929
965
  };
930
966
  if (isMissingArguments) {
@@ -1145,7 +1181,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1145
1181
  const body = parseExpression();
1146
1182
  const last = tokens[cursor.index - 1];
1147
1183
  const end = { line: last.line, column: last.endColumn };
1148
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1184
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1149
1185
  };
1150
1186
 
1151
1187
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1153,6 +1189,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1153
1189
  return withRange(parseDefinitionUnit(isLocal), start);
1154
1190
  };
1155
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
+
1156
1202
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1157
1203
  const token = peek();
1158
1204
  const location = getLocation(token);
@@ -1172,24 +1218,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
1172
1218
  peek(2).kind === "Identifier" &&
1173
1219
  isAt("==", 3)
1174
1220
  ) {
1175
- const left = expectIdentifier();
1221
+ const left = expectName();
1176
1222
  const operator = advance().text;
1177
- const right = expectIdentifier();
1223
+ const right = expectName();
1178
1224
  expect("==");
1179
- return parseOperator(operator, [
1180
- { name: left, arity: 0 },
1181
- { name: right, arity: 0 },
1182
- ]);
1225
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1183
1226
  }
1184
1227
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1185
1228
  const isPostfixDefinition =
1186
1229
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1187
1230
  if (isPrefixDefinition || isPostfixDefinition) {
1188
1231
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1189
- cursor.index += isPrefixDefinition ? 1 : 2;
1190
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1232
+ if (isPrefixDefinition) advance();
1233
+ const param = expectName();
1234
+ if (isPostfixDefinition) advance();
1191
1235
  expect("==");
1192
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1236
+ return parseOperator(operator, [toParam(param)]);
1193
1237
  }
1194
1238
  const name = expectIdentifier();
1195
1239
  if (accept("[")) {
@@ -1227,9 +1271,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1227
1271
  const module = expectIdentifier();
1228
1272
  const substitutions = accept("WITH")
1229
1273
  ? parseSeparated(() => {
1274
+ const targetStart = cursor.index;
1230
1275
  const target = advance().text;
1276
+ const nameRange = getRange(targetStart);
1231
1277
  expect("<-");
1232
- return { name: target, expression: parseArgument() };
1278
+ return { name: target, nameRange, expression: parseArgument() };
1233
1279
  })
1234
1280
  : [];
1235
1281
  return {
@@ -1245,6 +1291,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1245
1291
  };
1246
1292
 
1247
1293
  const parseDeclaration = (): Declaration => {
1294
+ const start = cursor.index;
1295
+ return withRange(parseDeclarationUnit(), start);
1296
+ };
1297
+
1298
+ const parseDeclarationUnit = (): Declaration => {
1248
1299
  const location = getLocation(peek());
1249
1300
  if (isAt("_") && peek(1).kind === "Operator") {
1250
1301
  advance();
@@ -1492,10 +1543,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1492
1543
  }
1493
1544
  if (level !== undefined) advance();
1494
1545
  if (level === "Variable") {
1495
- const name = expectIdentifier();
1496
- declareProofSymbol(name);
1546
+ const named = expectName();
1547
+ declareProofSymbol(named.name);
1497
1548
  return withRange(
1498
- { 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
+ },
1499
1558
  start,
1500
1559
  );
1501
1560
  }
@@ -1513,6 +1572,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1513
1572
  arity: declaration.arity,
1514
1573
  level: level ?? "Constant",
1515
1574
  set: isBounded ? parseExpression() : undefined,
1575
+ nameRange: declaration.location.range,
1516
1576
  location,
1517
1577
  },
1518
1578
  start,
@@ -1555,7 +1615,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
1555
1615
  const token = peek();
1556
1616
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1557
1617
  if (isAt("INSTANCE")) {
1558
- return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
1618
+ const start = cursor.index;
1619
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1559
1620
  }
1560
1621
  if (isAt("DEFINE") || isDefinitionStart()) {
1561
1622
  const start = cursor.index;
@@ -1568,21 +1629,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1568
1629
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1569
1630
  if (accept("TAKE")) {
1570
1631
  const isBounded = isBoundAhead();
1571
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1632
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1572
1633
  const bounds = isBounded ? parseBounds() : [];
1573
- declareProofSymbols(names, bounds);
1574
- return { kind: "Take", names, bounds };
1634
+ declareProofSymbols(names.names, bounds);
1635
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1575
1636
  }
1576
1637
  if (accept("WITNESS")) {
1577
1638
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1578
1639
  }
1579
1640
  if (accept("PICK")) {
1580
1641
  const isNames = isNameListAhead(":");
1581
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1642
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1582
1643
  const bounds = isNames ? [] : parseBounds();
1583
- declareProofSymbols(names, bounds);
1644
+ declareProofSymbols(names.names, bounds);
1584
1645
  expect(":");
1585
- return { kind: "Pick", names, bounds, body: parseExpression() };
1646
+ return {
1647
+ kind: "Pick",
1648
+ names: names.names,
1649
+ nameRanges: names.ranges,
1650
+ bounds,
1651
+ body: parseExpression(),
1652
+ };
1586
1653
  }
1587
1654
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1588
1655
  return {
@@ -1751,7 +1818,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1751
1818
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1752
1819
  if (token.kind === "ModuleEnd") {
1753
1820
  location.range = {
1754
- module: name,
1821
+ module: openModules[0].name,
1755
1822
  start: { line: location.line, column: location.column },
1756
1823
  end: { line: token.line, column: token.endColumn },
1757
1824
  };
@@ -1838,7 +1905,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1838
1905
  );
1839
1906
  continue;
1840
1907
  }
1841
- addDefinitionUnit(module, parseDefinition(true));
1908
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1842
1909
  continue;
1843
1910
  }
1844
1911
  }