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

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.
Files changed (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +166 -40
  5. package/dist/index.d.ts +166 -40
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -22
  12. package/dist/run-cli.js +1351 -22
  13. package/dist/transfer.cjs +83 -34
  14. package/dist/transfer.js +83 -34
  15. package/package.json +5 -1
  16. package/src/ast.ts +55 -7
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1331 -367
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +226 -207
  25. package/src/environment.ts +423 -127
  26. package/src/errors.ts +168 -9
  27. package/src/evaluate.ts +720 -177
  28. package/src/exit-codes.ts +6 -24
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +3 -1
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +149 -31
  33. package/src/lexer.ts +122 -29
  34. package/src/live-check.ts +204 -148
  35. package/src/liveness.ts +191 -122
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +428 -141
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1436 -459
  48. package/src/run-cli.ts +1166 -181
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3128 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +356 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +25 -5
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +161 -167
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +680 -255
package/src/config.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { TlaConfigError } from "./errors";
2
- import { matchOperator } from "./lexer";
2
+ import { matchOperator, matchUnicodeToken } from "./lexer";
3
+ import { formatLexicalError } from "./sany-lexer";
4
+ import type { TlcVersion } from "./tlc-version";
3
5
 
4
6
  export type ConfigValue =
5
7
  | { kind: "Number"; value: number }
@@ -42,12 +44,12 @@ export interface ModelConfig {
42
44
  }
43
45
 
44
46
  interface ConfigToken {
45
- kind: "Word" | "Number" | "String" | "Symbol" | "End";
47
+ kind: "Word" | "Number" | "String" | "Symbol" | "End" | "Error";
46
48
  text: string;
47
49
  line: number;
48
50
  }
49
51
 
50
- const KEYWORDS = new Set([
52
+ const SHARED_KEYWORDS = [
51
53
  "CONSTANT",
52
54
  "CONSTANTS",
53
55
  "INIT",
@@ -64,6 +66,10 @@ const KEYWORDS = new Set([
64
66
  "SYMMETRY",
65
67
  "VIEW",
66
68
  "CHECK_DEADLOCK",
69
+ ];
70
+
71
+ const KEYWORDS = new Set([
72
+ ...SHARED_KEYWORDS,
67
73
  "ALIAS",
68
74
  "POSTCONDITION",
69
75
  "POSTCONDITIONS",
@@ -72,147 +78,149 @@ const KEYWORDS = new Set([
72
78
  "_RL_REWARD",
73
79
  ]);
74
80
 
75
- const SYMBOLS = ["<-", "<<", ">>", "=", "{", "}", ",", "[", "]", "(", ")"];
76
-
77
- const SINGLE_KEYWORDS = {
78
- INIT: "init",
79
- NEXT: "next",
80
- SPECIFICATION: "specification",
81
- VIEW: "view",
82
- SYMMETRY: "symmetry",
83
- ALIAS: "alias",
84
- _PERIODIC: "periodic",
85
- _RL_REWARD: "rlReward",
86
- } as const;
87
-
88
- const LIST_KEYWORDS = {
89
- INVARIANT: "invariants",
90
- INVARIANTS: "invariants",
91
- PROPERTY: "properties",
92
- PROPERTIES: "properties",
93
- CONSTRAINT: "constraints",
94
- CONSTRAINTS: "constraints",
95
- ACTION_CONSTRAINT: "actionConstraints",
96
- ACTION_CONSTRAINTS: "actionConstraints",
97
- POSTCONDITION: "postconditions",
98
- POSTCONDITIONS: "postconditions",
99
- _POSSIBLE: "possible",
100
- } as const;
101
-
102
- const isSingleKeyword = (text: string): text is keyof typeof SINGLE_KEYWORDS =>
103
- Object.hasOwn(SINGLE_KEYWORDS, text);
81
+ // TLC 1.7.4 reads a TYPE and a TYPE_CONSTRAINT, which it doesn't check, and none of the keywords
82
+ // that came after it.
83
+ const TYPE_KEYWORDS = ["TYPE", "TYPE_CONSTRAINT"];
84
+ const KEYWORDS_174 = new Set([...SHARED_KEYWORDS, ...TYPE_KEYWORDS]);
104
85
 
105
- const isListKeyword = (text: string): text is keyof typeof LIST_KEYWORDS =>
106
- Object.hasOwn(LIST_KEYWORDS, text);
107
-
108
- // JavaCC's TokenMgrError.addEscapes, which TLC's lexical errors use.
109
- const escapeLexeme = (text: string) =>
110
- [...text]
111
- .map((character) => {
112
- const escaped = { "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r" }[
113
- character
114
- ];
115
- if (escaped) return escaped;
116
- if (character === '"' || character === "'" || character === "\\") return `\\${character}`;
117
- const code = character.codePointAt(0) ?? 0;
118
- return code < 0x20 || code > 0x7e ? `\\u${code.toString(16).padStart(4, "0")}` : character;
119
- })
120
- .join("");
121
-
122
- // TLC reads a configuration with SANY's lexer, which reports a character it cannot read as a
123
- // JavaCC lexical error.
124
- const createLexicalError = (line: number, column: number, found: string | undefined, after = "") =>
125
- new TlaConfigError(
126
- `TLC found an error in the configuration file:\nLexical error at line ${line}, column ${column}. Encountered: ${found === undefined ? "<EOF> " : `"${escapeLexeme(found)}" (${found.codePointAt(0)}), `}after : "${escapeLexeme(after)}"`,
127
- );
128
-
129
- const createConfigFileError = (line: number, detail: string) =>
130
- new TlaConfigError(`TLC found an error in the configuration file at line ${line}\n${detail}`);
86
+ const SYMBOLS = ["<-", "<<", ">>", "=", "{", "}", ",", "[", "]", "(", ")", "!"];
131
87
 
88
+ // TLC reads a configuration file with the TLA+ lexer. A lexical error ends the tokens; TLC reports
89
+ // it only when it reads that far.
132
90
  const tokenizeConfig = (source: string) => {
133
- // TLC appends a newline so that a trailing \* comment ends.
134
- const text = `${source}\n`;
135
91
  const tokens: ConfigToken[] = [];
136
92
  const cursor = { index: 0, line: 1, lineStart: 0 };
137
- // Moves past text, keeping the line and where it starts.
138
- const skip = (length: number) => {
139
- const skipped = text.slice(cursor.index, cursor.index + length);
140
- const lastBreak = skipped.lastIndexOf("\n");
141
- if (lastBreak !== -1) {
142
- cursor.line += skipped.split("\n").length - 1;
143
- cursor.lineStart = cursor.index + lastBreak + 1;
93
+ const addLexicalError = (index: number, after: string) => {
94
+ tokens.push({
95
+ kind: "Error",
96
+ text: formatLexicalError(cursor.line, index - cursor.lineStart + 1, source[index], after),
97
+ line: cursor.line,
98
+ });
99
+ };
100
+ // TLC appends a newline to the file. JavaCC reports a token that the end of the input cuts off,
101
+ // that newline included, at the start of the line after it.
102
+ const addEndOfInputError = (after: string) => {
103
+ tokens.push({
104
+ kind: "Error",
105
+ text: formatLexicalError(cursor.line + 1, 0, undefined, after),
106
+ line: cursor.line,
107
+ });
108
+ };
109
+ const skip = (text: string) => {
110
+ const lines = text.split("\n");
111
+ if (lines.length > 1) {
112
+ cursor.line += lines.length - 1;
113
+ cursor.lineStart = cursor.index + text.length - lines[lines.length - 1].length;
144
114
  }
145
- cursor.index += length;
115
+ cursor.index += text.length;
146
116
  };
147
- const getEndOfInputError = (after: string) =>
148
- createLexicalError(cursor.line + 1, 0, undefined, after);
149
- while (cursor.index < text.length) {
150
- const rest = text.slice(cursor.index);
117
+ while (cursor.index < source.length) {
118
+ const rest = source.slice(cursor.index);
151
119
  const whitespace = /^\s+/.exec(rest)?.[0];
152
120
  if (whitespace) {
153
- skip(whitespace.length);
121
+ skip(whitespace);
154
122
  continue;
155
123
  }
156
124
  if (rest.startsWith("\\*")) {
157
- cursor.index += rest.indexOf("\n");
125
+ const lineEnd = rest.indexOf("\n");
126
+ skip(lineEnd === -1 ? rest : rest.slice(0, lineEnd));
158
127
  continue;
159
128
  }
160
129
  if (rest.startsWith("(*")) {
161
130
  const end = rest.indexOf("*)");
162
131
  if (end === -1) {
163
- cursor.line += rest.split("\n").length - 2;
164
- throw getEndOfInputError("");
132
+ skip(rest);
133
+ addEndOfInputError("");
134
+ return tokens;
165
135
  }
166
- skip(end + 2);
136
+ skip(rest.slice(0, end + 2));
167
137
  continue;
168
138
  }
139
+ const line = cursor.line;
169
140
  const number = /^\d+(?![A-Za-z_])/.exec(rest)?.[0];
170
141
  if (number) {
171
- tokens.push({ kind: "Number", text: number, line: cursor.line });
172
- cursor.index += number.length;
142
+ tokens.push({ kind: "Number", text: number, line });
143
+ skip(number);
173
144
  continue;
174
145
  }
175
- const word = /^[A-Za-z0-9_!]+/.exec(rest)?.[0];
146
+ const word = /^[A-Za-z0-9_]+/.exec(rest)?.[0];
176
147
  if (word) {
177
- tokens.push({ kind: "Word", text: word, line: cursor.line });
178
- cursor.index += word.length;
148
+ tokens.push({ kind: "Word", text: word, line });
149
+ skip(word);
179
150
  continue;
180
151
  }
181
152
  if (rest.startsWith('"')) {
182
- const string = /^"((?:[^"\\\n]|\\.)*)"/.exec(rest);
183
- if (!string) {
184
- const lineEnd = rest.indexOf("\n");
185
- if (lineEnd === rest.length - 1) throw getEndOfInputError(rest);
186
- throw createLexicalError(
187
- cursor.line,
188
- cursor.index + lineEnd - cursor.lineStart + 1,
189
- "\n",
190
- rest.slice(0, lineEnd),
191
- );
153
+ const string = /^"((?:[^"\\\n]|\\[^\n])*)("?)/.exec(rest);
154
+ const text = string?.[0] ?? '"';
155
+ if (string?.[2] !== '"') {
156
+ skip(text);
157
+ if (cursor.index === source.length) addEndOfInputError(`${text}\n`);
158
+ else addLexicalError(cursor.index, text);
159
+ return tokens;
192
160
  }
193
- tokens.push({ kind: "String", text: string[1], line: cursor.line });
194
- cursor.index += string[0].length;
161
+ tokens.push({ kind: "String", text: string[1], line });
162
+ skip(text);
195
163
  continue;
196
164
  }
197
165
  const symbol = SYMBOLS.find((candidate) => rest.startsWith(candidate));
198
166
  if (symbol) {
199
- tokens.push({ kind: "Symbol", text: symbol, line: cursor.line });
200
- cursor.index += symbol.length;
167
+ tokens.push({ kind: "Symbol", text: symbol, line });
168
+ skip(symbol);
201
169
  continue;
202
170
  }
203
- const operator = matchOperator(text, cursor.index);
171
+ const operator = matchUnicodeToken(source, cursor.index) ?? matchOperator(source, cursor.index);
204
172
  if (!operator) {
205
- throw createLexicalError(cursor.line, cursor.index - cursor.lineStart + 1, rest[0]);
173
+ addLexicalError(cursor.index, "");
174
+ return tokens;
206
175
  }
207
- tokens.push({ kind: "Word", text: operator.text, line: cursor.line });
208
- cursor.index += operator.length;
176
+ tokens.push({ kind: "Word", text: operator.text, line });
177
+ skip(source.slice(cursor.index, cursor.index + operator.length));
209
178
  }
210
- tokens.push({ kind: "End", text: "", line: cursor.line - 1 });
179
+ tokens.push({ kind: "End", text: "", line: cursor.line });
211
180
  return tokens;
212
181
  };
213
182
 
214
- // TLC's ModelConfig.parse, which takes any token for a name and stops a list at the next keyword.
215
- export const parseConfig = (source: string): ModelConfig => {
183
+ // TLC's messages for a configuration file it can't read, from ModelConfig.parse.
184
+ const createSyntaxError = (line: number, problem: string) =>
185
+ new TlaConfigError(`TLC found an error in the configuration file at line ${line}\n${problem}`);
186
+
187
+ const createDuplicateKeywordError = (line: number, keyword: string) =>
188
+ createSyntaxError(line, `The keyword ${keyword} appeared twice.`);
189
+
190
+ const SINGLE_KEYWORDS = new Map<
191
+ string,
192
+ "init" | "next" | "specification" | "view" | "symmetry" | "alias" | "periodic" | "rlReward"
193
+ >([
194
+ ["INIT", "init"],
195
+ ["NEXT", "next"],
196
+ ["SPECIFICATION", "specification"],
197
+ ["VIEW", "view"],
198
+ ["SYMMETRY", "symmetry"],
199
+ ["ALIAS", "alias"],
200
+ ["_PERIODIC", "periodic"],
201
+ ["_RL_REWARD", "rlReward"],
202
+ ]);
203
+
204
+ const LIST_KEYWORDS = new Map<
205
+ string,
206
+ "invariants" | "properties" | "constraints" | "actionConstraints" | "postconditions" | "possible"
207
+ >([
208
+ ["INVARIANT", "invariants"],
209
+ ["INVARIANTS", "invariants"],
210
+ ["PROPERTY", "properties"],
211
+ ["PROPERTIES", "properties"],
212
+ ["CONSTRAINT", "constraints"],
213
+ ["CONSTRAINTS", "constraints"],
214
+ ["ACTION_CONSTRAINT", "actionConstraints"],
215
+ ["ACTION_CONSTRAINTS", "actionConstraints"],
216
+ ["POSTCONDITION", "postconditions"],
217
+ ["POSTCONDITIONS", "postconditions"],
218
+ ["_POSSIBLE", "possible"],
219
+ ]);
220
+
221
+ // A port of TLC's ModelConfig.parse, which takes any token where it expects a name or a value.
222
+ export const parseConfig = (source: string, version: TlcVersion = "master"): ModelConfig => {
223
+ const keywords = version === "1.7.4" ? KEYWORDS_174 : KEYWORDS;
216
224
  const tokens = tokenizeConfig(source);
217
225
  const cursor = { index: 0 };
218
226
  const config: ModelConfig = {
@@ -235,157 +243,168 @@ export const parseConfig = (source: string): ModelConfig => {
235
243
  rlReward: undefined,
236
244
  };
237
245
  const deadlock = { isSet: false };
238
- const advance = () => tokens[Math.min(cursor.index++, tokens.length - 1)];
239
- const isKeyword = (token: ConfigToken) => token.kind === "Word" && KEYWORDS.has(token.text);
240
-
241
- const parseValue = (first: ConfigToken): ConfigValue => {
242
- if (first.kind === "Number") return { kind: "Number", value: Number(first.text) };
243
- if (first.kind === "String") return { kind: "String", value: first.text };
244
- if (first.text === "TRUE" || first.text === "FALSE") {
245
- return { kind: "Boolean", value: first.text === "TRUE" };
246
+ const types = new Set<string>();
247
+ const advance = () => {
248
+ const token = tokens[Math.min(cursor.index++, tokens.length - 1)];
249
+ if (token.kind === "Error") {
250
+ throw new TlaConfigError(`TLC found an error in the configuration file:\n${token.text}`);
246
251
  }
247
- if (first.text === "{") {
252
+ return token;
253
+ };
254
+
255
+ const parseValue = (token: ConfigToken): ConfigValue => {
256
+ if (token.kind === "Number") return { kind: "Number", value: Number(token.text) };
257
+ if (token.kind === "String") return { kind: "String", value: token.text };
258
+ if (token.text === "TRUE" || token.text === "FALSE")
259
+ return { kind: "Boolean", value: token.text === "TRUE" };
260
+ if (token.text === "{") {
248
261
  const elements: ConfigValue[] = [];
249
- let token = advance();
250
- if (token.text !== "}") {
251
- for (;;) {
252
- elements.push(parseValue(token));
253
- token = advance();
254
- if (token.text !== ",") break;
255
- token = advance();
262
+ const current = { token: advance() };
263
+ if (current.token.text !== "}") {
264
+ while (true) {
265
+ elements.push(parseValue(current.token));
266
+ current.token = advance();
267
+ if (current.token.text !== ",") break;
268
+ current.token = advance();
256
269
  }
257
270
  }
258
- if (token.text !== "}")
259
- throw createConfigFileError(token.line, "It was expecting }, but did not find it.");
271
+ if (current.token.text !== "}") {
272
+ throw createSyntaxError(current.token.line, "It was expecting }, but did not find it.");
273
+ }
260
274
  return { kind: "Set", elements };
261
275
  }
262
- if (first.kind !== "End") return { kind: "ModelValue", name: first.text };
263
- throw createConfigFileError(first.line, "It was expecting a value, but did not find it.");
276
+ if (token.kind !== "End") return { kind: "ModelValue", name: token.text };
277
+ throw createSyntaxError(token.line, "It was expecting a value, but did not find it.");
264
278
  };
265
279
 
280
+ // A module name in brackets after = or <-, which applies the assignment in that module.
266
281
  const parseModuleScope = (operator: string) => {
267
282
  const name = advance();
268
283
  if (name.kind === "End") {
269
- throw createConfigFileError(name.line, `Expected an identifier after ${operator}[.`);
284
+ throw createSyntaxError(name.line, `Expected an identifier after ${operator}[.`);
270
285
  }
271
286
  const close = advance();
272
287
  if (close.text !== "]") {
273
- throw createConfigFileError(close.line, "It was expecting ], but did not find it.");
288
+ throw createSyntaxError(close.line, "It was expecting ], but did not find it.");
274
289
  }
275
290
  return name.text;
276
291
  };
277
292
 
278
- // Returns the token after the constants, which starts the next section.
279
- const parseConstants = (line: number) => {
280
- for (;;) {
293
+ // TLC reports a malformed argument list at the line of the CONSTANT keyword.
294
+ const parseConstants = (keywordLine: number) => {
295
+ while (true) {
281
296
  const first = advance();
282
- if (first.kind === "End" || isKeyword(first)) return first;
283
- const name = { text: first.text };
284
- let token = advance();
285
- while (token.text === "!") {
286
- name.text = `${name.text}!${advance().text}`;
287
- token = advance();
297
+ if (first.kind === "End" || keywords.has(first.text)) return first;
298
+ const current = { token: advance(), name: first.text };
299
+ while (current.token.text === "!") {
300
+ current.name = `${current.name}!${advance().text}`;
301
+ current.token = advance();
288
302
  }
289
- if (token.text === "<-") {
290
- token = advance();
291
- if (token.text === "[") {
303
+ const name = current.name;
304
+ if (current.token.text === "<-") {
305
+ current.token = advance();
306
+ if (current.token.text === "[") {
292
307
  const module = parseModuleScope("<-");
293
308
  const target = advance();
294
309
  if (target.kind === "End") {
295
- throw createConfigFileError(target.line, "Expected an identifier after <-[mod].");
310
+ throw createSyntaxError(target.line, "Expected an identifier after <-[mod].");
296
311
  }
297
- config.substitutions.push({ name: name.text, target: target.text, module });
312
+ config.substitutions.push({ name, target: target.text, module });
298
313
  continue;
299
314
  }
300
- if (token.kind === "End") {
301
- throw createConfigFileError(token.line, "Expected an identifier after <-.");
315
+ if (current.token.kind === "End") {
316
+ throw createSyntaxError(current.token.line, "Expected an identifier after <-.");
302
317
  }
303
- config.substitutions.push({ name: name.text, target: token.text });
318
+ config.substitutions.push({ name, target: current.token.text });
304
319
  continue;
305
320
  }
306
321
  const args: ConfigValue[] = [];
307
- if (token.text === "(") {
308
- for (;;) {
322
+ const hasArgs = current.token.text === "(";
323
+ if (hasArgs) {
324
+ while (true) {
309
325
  args.push(parseValue(advance()));
310
- token = advance();
311
- if (token.text !== ",") break;
326
+ current.token = advance();
327
+ if (current.token.text !== ",") break;
328
+ }
329
+ if (current.token.text !== ")") {
330
+ throw createSyntaxError(keywordLine, "");
312
331
  }
313
- if (token.text !== ")") throw createConfigFileError(line, "");
314
- token = advance();
332
+ current.token = advance();
315
333
  }
316
- if (token.text !== "=") {
317
- throw createConfigFileError(token.line, "It was expecting = or <-, but did not find it.");
334
+ if (current.token.text !== "=") {
335
+ throw createSyntaxError(
336
+ current.token.line,
337
+ "It was expecting = or <-, but did not find it.",
338
+ );
318
339
  }
319
- token = advance();
320
- const module = token.text === "[" ? parseModuleScope("=") : undefined;
321
- const value = parseValue(module === undefined ? token : advance());
340
+ current.token = advance();
341
+ const module = current.token.text === "[" ? parseModuleScope("=") : undefined;
342
+ const value = parseValue(module === undefined ? current.token : advance());
322
343
  config.constants.push({
323
- name: name.text,
344
+ name,
324
345
  value,
325
- ...(args.length > 0 && { args }),
326
- ...(module && { module }),
346
+ ...(hasArgs && { args }),
347
+ ...(module !== undefined && { module }),
327
348
  });
328
349
  }
329
350
  };
330
351
 
331
- let token = advance();
332
- while (token.kind !== "End") {
333
- const keyword = token.text;
334
- const line = token.line;
335
- if (isSingleKeyword(keyword)) {
336
- const name = advance();
337
- if (name.kind === "End") {
338
- throw createConfigFileError(
339
- line,
340
- `The keyword ${keyword} was not followed by an identifier.`,
341
- );
342
- }
343
- const key = SINGLE_KEYWORDS[keyword];
344
- if (config[key] !== undefined) {
345
- throw createConfigFileError(line, `The keyword ${keyword} appeared twice.`);
346
- }
347
- config[key] = name.text;
348
- token = advance();
349
- } else if (isListKeyword(keyword)) {
350
- const names = config[LIST_KEYWORDS[keyword]];
351
- token = advance();
352
- while (token.kind !== "End" && !isKeyword(token)) {
353
- names.push(token.text);
354
- token = advance();
352
+ const parseNames = (names: string[]) => {
353
+ while (true) {
354
+ const token = advance();
355
+ if (token.kind === "End" || keywords.has(token.text)) return token;
356
+ names.push(token.text);
357
+ }
358
+ };
359
+
360
+ const current = { token: advance() };
361
+ while (current.token.kind !== "End") {
362
+ const keyword = current.token.text;
363
+ const line = current.token.line;
364
+ const singleKey = keywords.has(keyword) ? SINGLE_KEYWORDS.get(keyword) : undefined;
365
+ const listKey = keywords.has(keyword) ? LIST_KEYWORDS.get(keyword) : undefined;
366
+ const advanceToArgument = () => {
367
+ const argument = advance();
368
+ if (argument.kind === "End") {
369
+ throw createSyntaxError(line, `The keyword ${keyword} was not followed by an identifier.`);
355
370
  }
356
- } else if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
357
- token = parseConstants(line);
358
- } else if (keyword === "CHECK_DEADLOCK") {
359
- const value = advance();
360
- if (value.kind === "End") {
361
- throw createConfigFileError(
362
- line,
363
- "The keyword CHECK_DEADLOCK was not followed by an identifier.",
364
- );
371
+ return argument;
372
+ };
373
+ if (singleKey !== undefined) {
374
+ const name = advanceToArgument();
375
+ if (config[singleKey] !== undefined) {
376
+ throw createDuplicateKeywordError(line, keyword);
365
377
  }
378
+ config[singleKey] = name.text;
379
+ current.token = advance();
380
+ continue;
381
+ }
382
+ if (listKey !== undefined) {
383
+ current.token = parseNames(config[listKey]);
384
+ continue;
385
+ }
386
+ if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
387
+ current.token = parseConstants(line);
388
+ continue;
389
+ }
390
+ if (keyword === "CHECK_DEADLOCK") {
391
+ const value = advanceToArgument();
366
392
  if (value.text !== "TRUE" && value.text !== "FALSE") {
367
- throw createConfigFileError(
368
- value.line,
369
- "It was expecting TRUE or FALSE, but did not find it.",
370
- );
371
- }
372
- if (deadlock.isSet) {
373
- throw createConfigFileError(line, "The keyword CHECK_DEADLOCK appeared twice.");
393
+ throw createSyntaxError(value.line, "It was expecting TRUE or FALSE, but did not find it.");
374
394
  }
395
+ if (deadlock.isSet) throw createDuplicateKeywordError(line, keyword);
375
396
  deadlock.isSet = true;
376
397
  config.checkDeadlock = value.text === "TRUE";
377
- token = advance();
378
- } else {
379
- throw createConfigFileError(line, "It was expecting a keyword, but did not find it.");
398
+ current.token = advance();
399
+ continue;
380
400
  }
381
- }
382
- if (
383
- config.specification !== undefined &&
384
- (config.init !== undefined || config.next !== undefined)
385
- ) {
386
- throw new TlaConfigError(
387
- "The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.",
388
- );
401
+ if (!keywords.has(keyword) || !TYPE_KEYWORDS.includes(keyword)) {
402
+ throw createSyntaxError(line, "It was expecting a keyword, but did not find it.");
403
+ }
404
+ advanceToArgument();
405
+ if (types.has(keyword)) throw createDuplicateKeywordError(line, keyword);
406
+ types.add(keyword);
407
+ current.token = advance();
389
408
  }
390
409
  return config;
391
410
  };