tla-ts 0.0.3-dev.4cf9a33 → 0.0.3-dev.4fdd845

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 +164 -40
  5. package/dist/index.d.ts +164 -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 +49 -6
  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 +225 -206
  25. package/src/environment.ts +417 -124
  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 +32 -4
  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 +419 -122
  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 +1334 -452
  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 +3112 -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 +354 -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 +683 -256
@@ -0,0 +1,545 @@
1
+ import { TlaError } from "./errors";
2
+
3
+ // An emulation of the token manager that JavaCC generates for SANY's grammar, javacc/tla+.jj: the
4
+ // tokens it produces, their kinds in TLAplusParserConstants order, and their positions.
5
+
6
+ export interface SanyToken {
7
+ kind: number;
8
+ image: string;
9
+ beginLine: number;
10
+ beginColumn: number;
11
+ next: SanyToken | undefined;
12
+ }
13
+
14
+ // The token kinds, in the order of TLAplusParserConstants, each with the literal images that
15
+ // produce it. JavaCC breaks a tie between matches of the same length by this order.
16
+ const KINDS: [string, ...string[]][] = [
17
+ ["EOF"],
18
+ ["BEGIN_PRAGMA", "--->"],
19
+ ["_BM1"],
20
+ ["NUMBER"],
21
+ ["_BM2"],
22
+ ["_BM0"],
23
+ ["SEPARATOR"],
24
+ ["END_MODULE"],
25
+ ["ACTION", "ACTION", "ACTIONS"],
26
+ ["ASSUME", "ASSUME"],
27
+ ["BOXASSUME", "[]ASSUME"],
28
+ ["ASSUMPTION", "ASSUMPTION", "AXIOM"],
29
+ ["CASE", "CASE"],
30
+ ["CHOOSE", "CHOOSE"],
31
+ ["CONSTANT", "CONSTANT", "CONSTANTS"],
32
+ ["ELSE", "ELSE"],
33
+ ["EXCEPT", "EXCEPT"],
34
+ ["EXISTS", "\\E", "\\exists", "∃"],
35
+ ["EXTENDS", "EXTENDS"],
36
+ ["FORALL", "\\A", "\\forall", "∀"],
37
+ ["IF", "IF"],
38
+ ["INSTANCE", "INSTANCE"],
39
+ ["LET", "LET"],
40
+ ["LETIN", "IN"],
41
+ ["LOCAL", "LOCAL"],
42
+ ["MODULE", "MODULE"],
43
+ ["NEW", "NEW"],
44
+ ["OTHER", "OTHER"],
45
+ ["PROPOSITION", "PROPOSITION", "LEMMA", "COROLLARY"],
46
+ ["SF", "SF_"],
47
+ ["T_EXISTS", "\\EE"],
48
+ ["T_FORALL", "\\AA"],
49
+ ["THEN", "THEN"],
50
+ ["BY", "BY"],
51
+ ["ONLY", "ONLY"],
52
+ ["DEFINE", "DEFINE"],
53
+ ["DF", "DEF", "DEFS"],
54
+ ["THEOREM", "THEOREM"],
55
+ ["USE", "USE"],
56
+ ["HIDE", "HIDE"],
57
+ ["HAVE", "HAVE"],
58
+ ["OBVIOUS", "OBVIOUS"],
59
+ ["OMITTED", "OMITTED"],
60
+ ["LAMBDA", "LAMBDA"],
61
+ ["TAKE", "TAKE"],
62
+ ["PROOF", "PROOF"],
63
+ ["PROVE", "PROVE"],
64
+ ["BOXPROVE", "[]PROVE"],
65
+ ["QED", "QED"],
66
+ ["RECURSIVE", "RECURSIVE"],
67
+ ["STATE", "STATE"],
68
+ ["TEMPORAL", "TEMPORAL", "TEMPORALS"],
69
+ ["PICK", "PICK"],
70
+ ["WITNESS", "WITNESS"],
71
+ ["SUFFICES", "SUFFICES"],
72
+ ["VARIABLE", "VARIABLE", "VARIABLES"],
73
+ ["WF", "WF_"],
74
+ ["WITH", "WITH"],
75
+ ["COMMA", ","],
76
+ ["COLON", ":"],
77
+ ["COLONCOLON", "::", "∷"],
78
+ ["DOT", "."],
79
+ ["US", "_"],
80
+ ["DEF", "==", "≜"],
81
+ ["LBR", "("],
82
+ ["RBR", ")"],
83
+ ["DEFBREAK", "-|-"],
84
+ ["LSB", "["],
85
+ ["ARSB", "]_"],
86
+ ["RSB", "]"],
87
+ ["LBC", "{"],
88
+ ["RBC", "}"],
89
+ ["LAB", "<<", "⟨"],
90
+ ["ARAB", ">>_", "⟩_"],
91
+ ["RAB", ">>", "⟩"],
92
+ ["BANG", "!"],
93
+ ["ARROW", "->", "→"],
94
+ ["SUBSTITUTE", "<-", "←"],
95
+ ["MAPTO", "|->", "↦"],
96
+ ["NUMBER_LITERAL"],
97
+ ["STRING_LITERAL"],
98
+ ["BAND"],
99
+ ["BOR"],
100
+ ["op_57", "'"],
101
+ ["op_68", "^+", "⁺"],
102
+ ["op_69", "^*"],
103
+ ["op_70", "^#"],
104
+ ["op_76", "-."],
105
+ ["op_26", "\\lnot", "¬"],
106
+ ["op_29", "\\neg"],
107
+ ["op_58", "~"],
108
+ ["CASESEP", "[]", "□"],
109
+ ["op_61", "<>", "◇"],
110
+ ["op_112", "ENABLED"],
111
+ ["op_113", "UNCHANGED"],
112
+ ["op_114", "SUBSET"],
113
+ ["op_115", "UNION"],
114
+ ["op_116", "DOMAIN"],
115
+ ["op_1", "//"],
116
+ ["AND", "/\\", "∧"],
117
+ ["op_3", "/="],
118
+ ["op_4", "/"],
119
+ ["OR", "\\/", "∨"],
120
+ ...[
121
+ "\\approx",
122
+ "\\asymp",
123
+ "\\bigcirc",
124
+ "\\bullet",
125
+ "\\cap",
126
+ "\\cdot",
127
+ "\\circ",
128
+ "\\cong",
129
+ "\\cup",
130
+ "\\div",
131
+ "\\doteq",
132
+ "\\equiv",
133
+ "\\geq",
134
+ "\\gg",
135
+ ].map((image): [string, string] => ["infix", image]),
136
+ ["IN", "\\in", "∈"],
137
+ ...[
138
+ "\\intersect",
139
+ "\\union",
140
+ "\\land",
141
+ "\\leq",
142
+ "\\ll",
143
+ "\\lor",
144
+ "\\o",
145
+ "\\odot",
146
+ "\\ominus",
147
+ "\\oplus",
148
+ "\\oslash",
149
+ "\\otimes",
150
+ "\\prec",
151
+ "\\preceq",
152
+ "\\propto",
153
+ "\\sim",
154
+ "\\simeq",
155
+ "\\sqcap",
156
+ "\\sqcup",
157
+ "\\sqsubset",
158
+ "\\sqsupset",
159
+ "\\sqsubseteq",
160
+ "\\sqsupseteq",
161
+ "\\star",
162
+ "\\subset",
163
+ "\\subseteq",
164
+ "\\succ",
165
+ "\\succeq",
166
+ "\\supset",
167
+ "\\supseteq",
168
+ "\\uplus",
169
+ "\\wr",
170
+ "\\",
171
+ "~>",
172
+ "=>",
173
+ "=<",
174
+ "=|",
175
+ ].map((image): [string, string] => ["infix", image]),
176
+ ["EQUALS", "="],
177
+ ...["##", "#", "^^", "^", "--", "-|", "-+->"].map((image): [string, string] => ["infix", image]),
178
+ ["op_77", "-"],
179
+ ...[
180
+ "**",
181
+ "*",
182
+ "++",
183
+ "+",
184
+ "<=>",
185
+ "<:",
186
+ "<=",
187
+ "<",
188
+ ">=",
189
+ ">",
190
+ "...",
191
+ "..",
192
+ "||",
193
+ "|",
194
+ "|-",
195
+ "|=",
196
+ "&&",
197
+ "&",
198
+ "$$",
199
+ "$",
200
+ "??",
201
+ "%%",
202
+ "%",
203
+ "@@",
204
+ "!!",
205
+ ":>",
206
+ ":=",
207
+ "::=",
208
+ "(+)",
209
+ "(-)",
210
+ "(.)",
211
+ "(/)",
212
+ "(\\X)",
213
+ "\\notin",
214
+ ..."≈≔≍◯⩴●∩⋅∘≅∪÷≐‥…≡‼≥≫⇔⇒⫤↝≤≪⊣≠∉⊙⊖⊕⊘⊗⇸≺⪯∝⁇⊨⊢∼≃⊓⊔⊏⊑⊐⊒⋆⊂⊆≻⪰⊃⊇×⊎‖≀",
215
+ "\\times",
216
+ ].map((image): [string, string] => ["infix", image]),
217
+ ["op_119", "\\X"],
218
+ ["IDENTIFIER"],
219
+ ["ProofStepLexeme"],
220
+ ["ProofImplicitStepLexeme"],
221
+ ["ProofStepDotLexeme"],
222
+ ["BareLevelLexeme"],
223
+ ["UnnumberedStepLexeme"],
224
+ ];
225
+
226
+ const KIND_NAMES = KINDS.map((entry) => entry[0]);
227
+ const KIND_INDEXES = new Map(
228
+ KIND_NAMES.map((name, kind): [string, number] => [name, kind]).reverse(),
229
+ );
230
+
231
+ // The kind named name. The anonymous infix operators share the name "infix", which no caller
232
+ // looks up.
233
+ export const getKind = (name: string) => {
234
+ const kind = KIND_INDEXES.get(name);
235
+ if (kind === undefined) throw new TlaError(`Unknown SANY token kind ${name}`);
236
+ return kind;
237
+ };
238
+
239
+ const LITERALS = new Map(
240
+ KINDS.flatMap((entry, kind) => entry.slice(1).map((image): [string, number] => [image, kind])),
241
+ );
242
+ const LONGEST_LITERAL = Math.max(...[...LITERALS.keys()].map((image) => image.length));
243
+ const LITERAL_LENGTHS = Array.from(
244
+ { length: LONGEST_LITERAL },
245
+ (_, index) => LONGEST_LITERAL - index,
246
+ );
247
+
248
+ const BEGIN_MODULE = /----+ *MODULE/y;
249
+ const IDENTIFIER_PATTERNS = [
250
+ /_\w*[A-Za-z]\w*/y,
251
+ /[0-9]\w*[A-Za-z]\w*/y,
252
+ /[WS](?:[a-zA-EG-Z_0-9]\w*)?/y,
253
+ /[WS]F(?:[A-Za-z0-9]\w*)?/y,
254
+ /[a-zA-RT-VX-Z]\w*/y,
255
+ /@|ℕ|ℤ|ℝ/y,
256
+ ];
257
+ const getJunctionPatterns = (junction: string) =>
258
+ [/[0-9][A-Za-z0-9]*/, /[WS][a-zA-EG-Z0-9][A-Za-z0-9]*/, /[a-zA-RT-VX-Z][A-Za-z0-9]*/].map(
259
+ (prefix) => new RegExp(`${prefix.source}${junction}`, "y"),
260
+ );
261
+ const BAND_PATTERNS = getJunctionPatterns(String.raw`\.\/\\`);
262
+ const BOR_PATTERNS = getJunctionPatterns(String.raw`\.\\\/`);
263
+ const STEP = String.raw`<(?:[0-9]+|\+|\*)>`;
264
+
265
+ // The tokens of the SPEC lexical state that are not literals, with their kinds.
266
+ const SPEC_PATTERNS: [string, RegExp[]][] = [
267
+ ["_BM0", [BEGIN_MODULE]],
268
+ ["SEPARATOR", [/----+/y]],
269
+ ["END_MODULE", [/====+/y]],
270
+ ["NUMBER_LITERAL", [/[0-9]+|\\[oO][0-7]+|\\[bB][01]+|\\[hH][0-9a-fA-F]+/y]],
271
+ ["STRING_LITERAL", [/"(?:[^"\n\r\\]|\\[ntrf\\"])*"/y]],
272
+ ["BAND", BAND_PATTERNS],
273
+ ["BOR", BOR_PATTERNS],
274
+ ["IDENTIFIER", IDENTIFIER_PATTERNS],
275
+ ["ProofStepLexeme", [/<[0-9]+>\w+/y]],
276
+ ["ProofImplicitStepLexeme", [/<[+*]>[A-Za-z0-9]+/y]],
277
+ ["ProofStepDotLexeme", [new RegExp(String.raw`${STEP}\w+\.+`, "y")]],
278
+ ["BareLevelLexeme", [new RegExp(STEP, "y")]],
279
+ ["UnnumberedStepLexeme", [new RegExp(String.raw`${STEP}(?:\*\.*|-\.*|\.+)`, "y")]],
280
+ ];
281
+
282
+ // The tokens of the DEFAULT and PRAGMA lexical states, which skip every other character.
283
+ const DEFAULT_PATTERNS: [string, RegExp[]][] = [
284
+ ["BEGIN_PRAGMA", [/--->/y]],
285
+ ["_BM1", [BEGIN_MODULE]],
286
+ ];
287
+ const PRAGMA_PATTERNS: [string, RegExp[]][] = [
288
+ ["NUMBER", [/[0-9]+/y]],
289
+ ["_BM2", [BEGIN_MODULE]],
290
+ ["BAND", BAND_PATTERNS],
291
+ ["BOR", BOR_PATTERNS],
292
+ ["IDENTIFIER", IDENTIFIER_PATTERNS],
293
+ ];
294
+
295
+ const getMatchLength = (pattern: RegExp, source: string, offset: number) => {
296
+ pattern.lastIndex = offset;
297
+ return pattern.exec(source)?.[0].length ?? 0;
298
+ };
299
+
300
+ // The longest match at offset, the earliest kind among matches of that length.
301
+ const matchPatterns = (patterns: [string, RegExp[]][], source: string, offset: number) =>
302
+ patterns
303
+ .map(([name, regexes]) => ({
304
+ kind: getKind(name),
305
+ length: Math.max(...regexes.map((regex) => getMatchLength(regex, source, offset))),
306
+ }))
307
+ .reduce(
308
+ (best, match) =>
309
+ match.length > best.length || (match.length === best.length && match.kind < best.kind)
310
+ ? match
311
+ : best,
312
+ { kind: -1, length: 0 },
313
+ );
314
+
315
+ const matchSpecToken = (source: string, offset: number) => {
316
+ const literalLength = LITERAL_LENGTHS.find((length) =>
317
+ LITERALS.has(source.slice(offset, offset + length)),
318
+ );
319
+ const pattern = matchPatterns(SPEC_PATTERNS, source, offset);
320
+ if (literalLength === undefined) return pattern;
321
+ const literal = {
322
+ kind: LITERALS.get(source.slice(offset, offset + literalLength)) ?? -1,
323
+ length: literalLength,
324
+ };
325
+ return literal.length > pattern.length ||
326
+ (literal.length === pattern.length && literal.kind < pattern.kind)
327
+ ? literal
328
+ : pattern;
329
+ };
330
+
331
+ export const getTabStop = (column: number) => column + 8 - (column % 8);
332
+
333
+ // SimpleCharStream.UpdateLineColumn: the line and column of each character, with tabs stopping
334
+ // at multiples of 8.
335
+ const getPositions = (source: string) => {
336
+ const lines: number[] = [];
337
+ const columns: number[] = [];
338
+ const state = { line: 1, column: 0, isAfterCr: false, isAfterLf: false };
339
+ for (const character of source) {
340
+ for (let unit = 0; unit < character.length; unit++) {
341
+ state.column++;
342
+ if (state.isAfterLf) {
343
+ state.isAfterLf = false;
344
+ state.line++;
345
+ state.column = 1;
346
+ } else if (state.isAfterCr) {
347
+ state.isAfterCr = false;
348
+ if (character === "\n") state.isAfterLf = true;
349
+ else {
350
+ state.line++;
351
+ state.column = 1;
352
+ }
353
+ }
354
+ if (character === "\r") state.isAfterCr = true;
355
+ else if (character === "\n") state.isAfterLf = true;
356
+ else if (character === "\t") state.column = getTabStop(state.column - 1);
357
+ lines.push(state.line);
358
+ columns.push(state.column);
359
+ }
360
+ }
361
+ return { lines, columns };
362
+ };
363
+
364
+ // The characters of a string literal that a string literal could still continue.
365
+ const STRING_PREFIX = /"(?:[^"\n\r\\]|\\[ntrf\\"])*\\?/y;
366
+ const LITERAL_PREFIXES = new Set(
367
+ [...LITERALS.keys()].flatMap((image) =>
368
+ Array.from({ length: image.length - 1 }, (_, index) => image.slice(0, index + 1)),
369
+ ),
370
+ );
371
+
372
+ // The characters JavaCC's token manager reads at offset while some token could still continue.
373
+ const getTokenPrefix = (source: string, offset: number) => {
374
+ let length = 0;
375
+ while (LITERAL_PREFIXES.has(source.slice(offset, offset + length + 1))) length++;
376
+ return source.slice(
377
+ offset,
378
+ offset + Math.max(length, getMatchLength(STRING_PREFIX, source, offset)),
379
+ );
380
+ };
381
+
382
+ // A lazy token stream over source, which returns SANY's message for the TokenMgrError JavaCC's
383
+ // token manager throws at a character no token continues with, or at the end of the file in a
384
+ // comment.
385
+ export const createSanyLexer = (source: string) => {
386
+ const positions = getPositions(source);
387
+ const state = { offset: 0, mode: "DEFAULT" };
388
+ const createToken = (kind: number, start: number, length: number): SanyToken => ({
389
+ kind,
390
+ image: source.slice(start, start + length),
391
+ beginLine: positions.lines[start] ?? 0,
392
+ beginColumn: positions.columns[start] ?? 0,
393
+ next: undefined,
394
+ });
395
+ // JavaCC places the end of the file at the file's last character.
396
+ const getEndOfFile = () => createToken(getKind("EOF"), source.length - 1, 0);
397
+ // The lexer read the characters from start to end, and TLAplusParser.parse reports the end of
398
+ // the file in a token that starts on an earlier line as an open comment.
399
+ const getLexicalError = (start: number, end: number, after: string) => {
400
+ const index = end - 1;
401
+ const line = positions.lines[index];
402
+ const column = positions.columns[index];
403
+ if (end < source.length) return formatLexicalError(line, column, source[index], after);
404
+ if (positions.lines[start] !== line) {
405
+ return `Lexical {error: EOF reached, possibly open comment starting around line ${positions.lines[start]}`;
406
+ }
407
+ return source[index] === "\n" || source[index] === "\r"
408
+ ? formatLexicalError(line + 1, 0, undefined, after)
409
+ : formatLexicalError(line, column + 1, undefined, after);
410
+ };
411
+ // A comment is a chain of MORE matches, which restarts after each nested comment bracket, and the
412
+ // end of the file right after a bracket ends the token stream.
413
+ const skipComment = () => {
414
+ const start = state.offset;
415
+ if (source.startsWith("\\*", start)) {
416
+ const end = source.slice(start).search(/[\n\r]/);
417
+ if (end === -1) return getLexicalError(start, source.length, "");
418
+ state.offset += end + 1;
419
+ return undefined;
420
+ }
421
+ let depth = 0;
422
+ let chainStart = start;
423
+ let offset = start;
424
+ while (offset < source.length) {
425
+ if (source.startsWith("(*", offset)) {
426
+ depth++;
427
+ offset += 2;
428
+ if (depth > 1) chainStart = offset;
429
+ } else if (source.startsWith("*)", offset)) {
430
+ depth--;
431
+ offset += 2;
432
+ chainStart = offset;
433
+ if (depth === 0) {
434
+ state.offset = offset;
435
+ return undefined;
436
+ }
437
+ } else offset++;
438
+ }
439
+ state.offset = offset;
440
+ return chainStart === source.length ? undefined : getLexicalError(chainStart, offset, "");
441
+ };
442
+ const getSkippingToken = (patterns: [string, RegExp[]][]): SanyToken => {
443
+ while (state.offset < source.length) {
444
+ const match = matchPatterns(patterns, source, state.offset);
445
+ if (match.length > 0) {
446
+ const token = createToken(match.kind, state.offset, match.length);
447
+ state.offset += match.length;
448
+ if (KIND_NAMES[match.kind].startsWith("_BM")) state.mode = "SPEC";
449
+ if (KIND_NAMES[match.kind] === "BEGIN_PRAGMA") state.mode = "PRAGMA";
450
+ return token;
451
+ }
452
+ state.offset++;
453
+ }
454
+ return getEndOfFile();
455
+ };
456
+ const getNextToken = (): SanyToken | string => {
457
+ if (state.mode === "DEFAULT") return getSkippingToken(DEFAULT_PATTERNS);
458
+ if (state.mode === "PRAGMA") return getSkippingToken(PRAGMA_PATTERNS);
459
+ while (state.offset < source.length) {
460
+ const offset = state.offset;
461
+ if (" \t\n\r".includes(source[offset])) state.offset++;
462
+ else if (source.startsWith("(*", offset) || source.startsWith("\\*", offset)) {
463
+ const error = skipComment();
464
+ if (error !== undefined) return error;
465
+ } else {
466
+ const match = matchSpecToken(source, offset);
467
+ if (match.length === 0) {
468
+ // JavaCC reads on while a token could continue, and one more character.
469
+ const prefix = getTokenPrefix(source, offset);
470
+ const end = Math.min(offset + prefix.length + 1, source.length);
471
+ const read = source.slice(offset, end);
472
+ const after = end < source.length ? prefix : read.length > 1 ? read : "";
473
+ return getLexicalError(offset, end, after);
474
+ }
475
+ const token = createToken(match.kind, offset, match.length);
476
+ state.offset += match.length;
477
+ return token;
478
+ }
479
+ }
480
+ return getEndOfFile();
481
+ };
482
+ return getNextToken;
483
+ };
484
+
485
+ // JavaCC's TokenMgrError.addEscapes, which escapes the characters of a lexical error's message.
486
+ const LEXEME_ESCAPES = new Map([
487
+ ["\b", "\\b"],
488
+ ["\t", "\\t"],
489
+ ["\n", "\\n"],
490
+ ["\f", "\\f"],
491
+ ["\r", "\\r"],
492
+ ['"', '\\"'],
493
+ ["'", "\\'"],
494
+ ["\\", "\\\\"],
495
+ ]);
496
+
497
+ export const escapeLexeme = (text: string) =>
498
+ text.replace(
499
+ // eslint-disable-next-line no-control-regex
500
+ /[\0-\x1f"'\\\x7f-\uffff]/g,
501
+ (character) =>
502
+ LEXEME_ESCAPES.get(character) ??
503
+ (character === "\0" ? "" : `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`),
504
+ );
505
+
506
+ // TokenMgrError.LexicalError: the character the lexer stopped at, or the end of the file, and the
507
+ // characters of the token it had read.
508
+ export const formatLexicalError = (
509
+ line: number,
510
+ column: number,
511
+ character: string | undefined,
512
+ after: string,
513
+ ) =>
514
+ `Lexical error at line ${line}, column ${column}. Encountered: ${character === undefined ? "<EOF> " : `"${escapeLexeme(character)}" (${character.charCodeAt(0)}), `}after : "${escapeLexeme(after)}"`;
515
+
516
+ // TLC 1.7.4's lexer, which JavaCC generated without UNICODE_INPUT, reads a character past U+00FF
517
+ // outside a comment by its low byte, and stops at one whose low byte is below 0x80.
518
+ export const getSany174LexicalError = (source: string) => {
519
+ const readToken = createSanyLexer(source);
520
+ for (
521
+ let token = readToken();
522
+ typeof token !== "string" && token.kind !== getKind("EOF");
523
+ token = readToken()
524
+ ) {
525
+ const index = token.image.split("").findIndex((character) => {
526
+ const code = character.charCodeAt(0);
527
+ return code > 0xff && (code & 0xff) < 0x80;
528
+ });
529
+ if (index === -1) continue;
530
+ const column = token.image
531
+ .slice(1, index + 1)
532
+ .split("")
533
+ .reduce(
534
+ (previous, character) => (character === "\t" ? getTabStop(previous) : previous + 1),
535
+ token.beginColumn,
536
+ );
537
+ return formatLexicalError(
538
+ token.beginLine,
539
+ column,
540
+ token.image[index],
541
+ token.image.slice(0, index),
542
+ );
543
+ }
544
+ return undefined;
545
+ };
@@ -0,0 +1,122 @@
1
+ import {
2
+ formatSanyLocation,
3
+ getContextSymbol,
4
+ type ModuleNode,
5
+ type OpApplNode,
6
+ type SanyMessage,
7
+ type SemanticNode,
8
+ } from "./sany-nodes";
9
+
10
+ // A port of SANY's tla2sany.linter.Linter, which warns about a record field named like a
11
+ // definition or declaration of the module.
12
+
13
+ // SemanticNode.getChildren.
14
+ const getChildren = (node: SemanticNode): (SemanticNode | undefined)[] => {
15
+ switch (node.kind) {
16
+ case "OpAppl":
17
+ return [...node.ranges, ...node.operands];
18
+ case "OpDef":
19
+ case "ThmOrAssumpDef":
20
+ case "Label":
21
+ return [node.body];
22
+ case "LetIn":
23
+ return [...node.opDefs, ...node.instances, node.body];
24
+ case "SubstIn":
25
+ case "APSubstIn":
26
+ return [node.body, ...node.substs.map((subst) => subst.expr)];
27
+ case "Instance":
28
+ return node.substs.map((subst) => subst.expr);
29
+ case "AssumeProve":
30
+ return [...node.assumptions, node.goal];
31
+ case "NewSymb":
32
+ return [node.set];
33
+ default:
34
+ return [];
35
+ }
36
+ };
37
+
38
+ // Operators whose values are sets of values that a record field may range over.
39
+ const SET_OPERATORS = new Map([
40
+ ["Integers", "Int"],
41
+ ["Naturals", "Nat"],
42
+ // SANY compares the name of the definition with the module's, so no definition of Reals counts.
43
+ ["Reals", "Reals"],
44
+ ]);
45
+
46
+ // Linter.isBuiltFromDeclarations: whether an expression applies a declared or bound symbol, BOOLEAN
47
+ // or a standard set of numbers, directly or through the definitions it applies.
48
+ const isBuiltFromDeclarations = (node: SemanticNode | undefined) => {
49
+ const visited = new Set<SemanticNode>();
50
+ const visit = (current: SemanticNode | undefined): boolean => {
51
+ if (current === undefined || visited.has(current)) return false;
52
+ visited.add(current);
53
+ if (current.kind === "OpAppl") {
54
+ const operator = current.operator;
55
+ if (operator.kind === "OpDecl" || operator.kind === "FormalParam") return true;
56
+ if (operator.kind === "OpDef") {
57
+ if (operator.name === "BOOLEAN") return true;
58
+ const moduleName = operator.module?.name;
59
+ if (moduleName !== undefined && SET_OPERATORS.get(moduleName) === operator.name)
60
+ return true;
61
+ if (visit(operator)) return true;
62
+ }
63
+ }
64
+ return getChildren(current).some(visit);
65
+ };
66
+ return visit(node);
67
+ };
68
+
69
+ const getFieldPairs = (record: OpApplNode) =>
70
+ record.operands.filter((operand): operand is OpApplNode => operand?.kind === "OpAppl");
71
+
72
+ const getFieldName = (pair: OpApplNode) => {
73
+ const name = pair.operands[0];
74
+ return name?.kind === "String" ? name : undefined;
75
+ };
76
+
77
+ // Records.sameDomain.
78
+ const isSameDomain = (record: OpApplNode, other: OpApplNode) => {
79
+ if (record === other) return true;
80
+ if (record.operands.length !== other.operands.length) return false;
81
+ const names = new Set(getFieldPairs(other).map((pair) => getFieldName(pair)?.value));
82
+ return getFieldPairs(record).every((pair) => names.has(getFieldName(pair)?.value));
83
+ };
84
+
85
+ // ModuleNode.getExtendedModuleSet(true).
86
+ const getExtendedModules = (module: ModuleNode, found = new Set<ModuleNode>()) => {
87
+ module.extendees.forEach((extendee) => {
88
+ if (found.has(extendee)) return;
89
+ found.add(extendee);
90
+ getExtendedModules(extendee, found);
91
+ });
92
+ return found;
93
+ };
94
+
95
+ // Linter.lintRecords: the warnings for the records of a module.
96
+ export const lintSanyModule = (module: ModuleNode): SanyMessage[] => {
97
+ const allRecords = [
98
+ ...[...getExtendedModules(module)].flatMap((extended) => extended.records),
99
+ ...module.records,
100
+ ];
101
+ return module.records.flatMap((record) => {
102
+ // Linter checks the field's value, the record's fields and the fields of every record with the
103
+ // same domain, which includes the record itself.
104
+ const isExempt = allRecords
105
+ .filter((other) => isSameDomain(other, record))
106
+ .flatMap(getFieldPairs)
107
+ .some(isBuiltFromDeclarations);
108
+ if (isExempt) return [];
109
+ return getFieldPairs(record).flatMap((pair) => {
110
+ const name = getFieldName(pair);
111
+ const symbol = name && getContextSymbol(module.context, name.value);
112
+ if (name === undefined || symbol === undefined) return [];
113
+ return [
114
+ {
115
+ location: name.location,
116
+ text: `The field name "${name.value}" in the record constructor is identical to the existing definition or declaration\nnamed ${name.value}, located at ${formatSanyLocation(symbol.location)}.\nThe field in the record will not take the value of the ${name.value} definition or declaration.\nIn TLA+, field names in records are strings, regardless of any similarly named declarations or definitions.\nTherefore, DOMAIN [${name.value} |-> ...] = {"${name.value}"} holds true.`,
117
+ isWarning: true,
118
+ },
119
+ ];
120
+ });
121
+ });
122
+ };