tla-ts 0.0.3-dev.07eab97 → 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.
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,10 +636,19 @@ 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
653
  const argument = parseExpression();
632
654
  // SANY reads an argument that only names an operator of a parameterized instance, such as
@@ -776,12 +798,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
776
798
  expect(":");
777
799
  return { kind, bounds, body: parseExpression(), location };
778
800
  }
779
- const names = parseSeparated(expectIdentifier);
801
+ const names = getNames(parseSeparated(expectName));
780
802
  expect(":");
781
803
  return {
782
804
  kind: "UnboundedQuantifier",
783
805
  quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
784
- names,
806
+ names: names.names,
807
+ nameRanges: names.ranges,
785
808
  body: parseExpression(),
786
809
  isTemporal,
787
810
  location,
@@ -888,8 +911,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
888
911
  );
889
912
  }
890
913
  const selectors: Selector[] = [{ kind: "Name", name, args }];
914
+ const selectorRanges = [getRange(start, start)];
891
915
  while (isSelectorAhead()) {
892
916
  advance();
917
+ selectorRanges.push(getRange(cursor.index, cursor.index));
893
918
  selectors.push(parseSelector());
894
919
  }
895
920
  const names = selectors.flatMap((selector) =>
@@ -905,9 +930,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
905
930
  const argumentsRange = lastArguments.range;
906
931
  const reference: Expression =
907
932
  names.length < selectors.length
908
- ? { kind: "Subexpression", selectors, location }
933
+ ? { kind: "Subexpression", selectors, selectorRanges, location }
909
934
  : qualifiedArgs.length === 0
910
- ? { kind: "Identifier", name: operator, location }
935
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
911
936
  : argumentCounts.slice(0, -1).some((count) => count > 0)
912
937
  ? {
913
938
  kind: "Apply",
@@ -915,9 +940,17 @@ const parse = (source: string, isTlapm: boolean): Module => {
915
940
  args: qualifiedArgs,
916
941
  argumentCounts,
917
942
  argumentsRange,
943
+ selectorRanges,
918
944
  location,
919
945
  }
920
- : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
946
+ : {
947
+ kind: "Apply",
948
+ operator,
949
+ args: qualifiedArgs,
950
+ argumentsRange,
951
+ selectorRanges,
952
+ location,
953
+ };
921
954
  if (isMissingArguments) {
922
955
  missingArguments.set(reference, {
923
956
  location: formatRange(getRange(start)),
@@ -1136,7 +1169,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1136
1169
  const body = parseExpression();
1137
1170
  const last = tokens[cursor.index - 1];
1138
1171
  const end = { line: last.line, column: last.endColumn };
1139
- return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1172
+ return { body, bodyRange: { module: openModules[0].name, start, end } };
1140
1173
  };
1141
1174
 
1142
1175
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
@@ -1144,6 +1177,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
1144
1177
  return withRange(parseDefinitionUnit(isLocal), start);
1145
1178
  };
1146
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
+
1147
1190
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1148
1191
  const token = peek();
1149
1192
  const location = getLocation(token);
@@ -1163,24 +1206,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
1163
1206
  peek(2).kind === "Identifier" &&
1164
1207
  isAt("==", 3)
1165
1208
  ) {
1166
- const left = expectIdentifier();
1209
+ const left = expectName();
1167
1210
  const operator = advance().text;
1168
- const right = expectIdentifier();
1211
+ const right = expectName();
1169
1212
  expect("==");
1170
- return parseOperator(operator, [
1171
- { name: left, arity: 0 },
1172
- { name: right, arity: 0 },
1173
- ]);
1213
+ return parseOperator(operator, [toParam(left), toParam(right)]);
1174
1214
  }
1175
1215
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
1176
1216
  const isPostfixDefinition =
1177
1217
  token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1178
1218
  if (isPrefixDefinition || isPostfixDefinition) {
1179
1219
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1180
- cursor.index += isPrefixDefinition ? 1 : 2;
1181
- const param = isPrefixDefinition ? expectIdentifier() : token.text;
1220
+ if (isPrefixDefinition) advance();
1221
+ const param = expectName();
1222
+ if (isPostfixDefinition) advance();
1182
1223
  expect("==");
1183
- return parseOperator(operator, [{ name: param, arity: 0 }]);
1224
+ return parseOperator(operator, [toParam(param)]);
1184
1225
  }
1185
1226
  const name = expectIdentifier();
1186
1227
  if (accept("[")) {
@@ -1236,6 +1277,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
1236
1277
  };
1237
1278
 
1238
1279
  const parseDeclaration = (): Declaration => {
1280
+ const start = cursor.index;
1281
+ return withRange(parseDeclarationUnit(), start);
1282
+ };
1283
+
1284
+ const parseDeclarationUnit = (): Declaration => {
1239
1285
  const location = getLocation(peek());
1240
1286
  if (isAt("_") && peek(1).kind === "Operator") {
1241
1287
  advance();
@@ -1483,10 +1529,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
1483
1529
  }
1484
1530
  if (level !== undefined) advance();
1485
1531
  if (level === "Variable") {
1486
- const name = expectIdentifier();
1487
- declareProofSymbol(name);
1532
+ const named = expectName();
1533
+ declareProofSymbol(named.name);
1488
1534
  return withRange(
1489
- { 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
+ },
1490
1544
  start,
1491
1545
  );
1492
1546
  }
@@ -1504,6 +1558,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1504
1558
  arity: declaration.arity,
1505
1559
  level: level ?? "Constant",
1506
1560
  set: isBounded ? parseExpression() : undefined,
1561
+ nameRange: declaration.location.range,
1507
1562
  location,
1508
1563
  },
1509
1564
  start,
@@ -1559,21 +1614,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
1559
1614
  if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1560
1615
  if (accept("TAKE")) {
1561
1616
  const isBounded = isBoundAhead();
1562
- const names = isBounded ? [] : parseSeparated(expectIdentifier);
1617
+ const names = getNames(isBounded ? [] : parseSeparated(expectName));
1563
1618
  const bounds = isBounded ? parseBounds() : [];
1564
- declareProofSymbols(names, bounds);
1565
- return { kind: "Take", names, bounds };
1619
+ declareProofSymbols(names.names, bounds);
1620
+ return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1566
1621
  }
1567
1622
  if (accept("WITNESS")) {
1568
1623
  return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1569
1624
  }
1570
1625
  if (accept("PICK")) {
1571
1626
  const isNames = isNameListAhead(":");
1572
- const names = isNames ? parseSeparated(expectIdentifier) : [];
1627
+ const names = getNames(isNames ? parseSeparated(expectName) : []);
1573
1628
  const bounds = isNames ? [] : parseBounds();
1574
- declareProofSymbols(names, bounds);
1629
+ declareProofSymbols(names.names, bounds);
1575
1630
  expect(":");
1576
- 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
+ };
1577
1638
  }
1578
1639
  if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
1579
1640
  return {
@@ -1742,7 +1803,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1742
1803
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1743
1804
  if (token.kind === "ModuleEnd") {
1744
1805
  location.range = {
1745
- module: name,
1806
+ module: openModules[0].name,
1746
1807
  start: { line: location.line, column: location.column },
1747
1808
  end: { line: token.line, column: token.endColumn },
1748
1809
  };
@@ -1829,7 +1890,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
1829
1890
  );
1830
1891
  continue;
1831
1892
  }
1832
- addDefinitionUnit(module, parseDefinition(true));
1893
+ addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
1833
1894
  continue;
1834
1895
  }
1835
1896
  }