tla-ts 0.0.3-dev.a50fc89 → 0.0.3-dev.b0f84e8

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/tlc-module.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  getValue,
16
16
  } from "./builtins";
17
17
  import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "./runtime";
18
- import { createActionRecord } from "./tlc-ext";
18
+ import { createActionRecord, setTlcValue } from "./tlc-ext";
19
19
  import { getRandomElement } from "./tlc-random";
20
20
  import {
21
21
  ANY_SET,
@@ -39,11 +39,17 @@ const formatCalendarVersion = (date: Date) =>
39
39
  const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
40
40
  createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
41
41
 
42
+ // TLC creates its model checker after it processes the spec, so these are undefined before then.
43
+ const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
44
+
42
45
  const getRuntimeValue = (
43
46
  runtime: CheckerRuntime,
44
47
  key: string,
45
48
  state: (Value | undefined)[] | undefined,
46
49
  ) => {
50
+ if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
51
+ throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
52
+ }
47
53
  const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
48
54
  switch (key) {
49
55
  case "level":
@@ -203,33 +209,8 @@ export const createTlcOperators = (
203
209
  }),
204
210
  defineStateOperator("TLCSet", 2, (args) => {
205
211
  const key = getValue(args, 0, "TLCSet");
206
- const value = getValue(args, 1, "TLCSet");
207
- if (typeof key === "number" && key >= 0) {
208
- runtime.registers.set(key, value);
209
- return true;
210
- }
211
- if (key === "exit") {
212
- if (value === true) runtime.isStopped = true;
213
- return true;
214
- }
215
- if (key === "pause") {
216
- if (value === true) {
217
- print("Press enter to resume model checking.");
218
- runtime.pause();
219
- }
220
- return true;
221
- }
222
- if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
223
- runtime.namedRegisters.set(key, value);
224
- return true;
225
- }
226
- if (typeof key === "string" && key.startsWith("-D")) {
227
- runtime.properties.set(key.slice(2), formatValue(value));
228
- return true;
229
- }
230
- throw new TlaModuleError(
231
- `The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
232
- );
212
+ setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
213
+ return true;
233
214
  }),
234
215
  defineConstant("Any", ANY_SET),
235
216
  defineOperator("RandomElement", 1, (args) =>
package/src/tlc-order.ts CHANGED
@@ -184,9 +184,18 @@ const isAdjacent = (left: Token, right: Token | undefined) =>
184
184
  right.line === left.line &&
185
185
  right.column === left.column + left.text.length;
186
186
 
187
+ const STRING_ESCAPES: Record<string, string> = {
188
+ "\\": "\\\\",
189
+ '"': '\\"',
190
+ "\n": "\\n",
191
+ "\t": "\\t",
192
+ "\r": "\\r",
193
+ "\f": "\\f",
194
+ };
195
+
187
196
  // The texts SANY interns while parsing, in order. SANY reads a proof step label such as <1>2 and
188
- // a module header such as ---- MODULE as one token, and interns a string literal's contents only
189
- // during semantic analysis.
197
+ // a module header such as ---- MODULE as one token. It interns a string literal as written, quotes
198
+ // included, and interns the literal's contents only during semantic analysis.
190
199
  const getParsedTexts = (tokens: Token[]) => {
191
200
  const texts: string[] = [];
192
201
  for (let index = 0; index < tokens.length; index++) {
@@ -202,7 +211,12 @@ const getParsedTexts = (tokens: Token[]) => {
202
211
  continue;
203
212
  }
204
213
  const isHeader = token.text === "MODULE" && tokens[index - 1]?.kind === "Separator";
205
- if (token.kind !== "String" && !isHeader) texts.push(token.text);
214
+ if (isHeader) continue;
215
+ texts.push(
216
+ token.kind === "String"
217
+ ? `"${token.text.replace(/[\\"\n\t\r\f]/g, (character) => STRING_ESCAPES[character])}"`
218
+ : token.text,
219
+ );
206
220
  }
207
221
  return texts;
208
222
  };
package/src/values.ts CHANGED
@@ -5,6 +5,7 @@ export interface ModelValue {
5
5
  kind: "ModelValue";
6
6
  name: string;
7
7
  type: string | undefined;
8
+ rank: number;
8
9
  }
9
10
 
10
11
  export interface SetValue {
@@ -264,6 +265,7 @@ const stringOrder = { ranks: Object.create(null) as Record<string, number>, size
264
265
  export const setStringOrder = (texts: Iterable<string>) => {
265
266
  stringOrder.ranks = Object.create(null);
266
267
  stringOrder.size = 0;
268
+ modelValues.clear();
267
269
  for (const text of texts) rankString(text);
268
270
  };
269
271
 
@@ -353,7 +355,7 @@ export const compareValues = (left: Value, right: Value): number => {
353
355
  if (left.type !== undefined && right.type !== undefined) {
354
356
  checkModelValueTypes(left, right, false);
355
357
  }
356
- return compareStrings(left.name, right.name);
358
+ return left.rank - right.rank;
357
359
  }
358
360
  if (leftKind === "ModelValue" || rightKind === "ModelValue") {
359
361
  checkModelValueTypes(left, right, false);
@@ -385,14 +387,15 @@ export const compareValues = (left: Value, right: Value): number => {
385
387
 
386
388
  const modelValues = new Map<string, ModelValue>();
387
389
 
390
+ // A check ranks every model value's name when it creates the value, and creates its own values.
388
391
  export const createModelValue = (name: string): ModelValue => {
389
- rankString(name);
390
392
  const known = modelValues.get(name);
391
393
  if (known) return known;
392
394
  const value: ModelValue = {
393
395
  kind: "ModelValue",
394
396
  name,
395
397
  type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
398
+ rank: getStringRank(name),
396
399
  };
397
400
  modelValues.set(name, value);
398
401
  return value;
@@ -562,7 +565,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
562
565
 
563
566
  const compareModelValue = (left: ModelValue, right: Value) => {
564
567
  const type = getModelValueType(left);
565
- if (type === undefined) return isModelValue(right) ? compareStrings(left.name, right.name) : -1;
568
+ if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
566
569
  if (!isModelValue(right)) {
567
570
  throw new TlaEvaluationError(
568
571
  `Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
@@ -574,7 +577,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
574
577
  `Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
575
578
  );
576
579
  }
577
- return compareStrings(left.name, right.name);
580
+ return left.rank - right.rank;
578
581
  };
579
582
 
580
583
  const equalsModelValue = (left: ModelValue, right: Value) => {
@@ -2113,3 +2116,14 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
2113
2116
  }
2114
2117
  }
2115
2118
  };
2119
+
2120
+ // TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
2121
+ export const formatState = (state: Record<string, Value>) => {
2122
+ const entries = Object.entries(state);
2123
+ return entries
2124
+ .map(
2125
+ ([name, value]) =>
2126
+ `${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
2127
+ )
2128
+ .join("");
2129
+ };