tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.cfd469d
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/dist/index.cjs +1 -1
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +14 -13
- package/dist/run-cli.js +22 -21
- package/dist/transfer.cjs +27 -18
- package/dist/transfer.js +27 -18
- package/package.json +1 -1
- package/src/ast.ts +25 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +210 -40
- package/src/environment.ts +208 -69
- package/src/errors.ts +47 -3
- package/src/evaluate.ts +131 -9
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +177 -36
- package/src/liveness.ts +5 -1
- package/src/parser.ts +140 -42
- package/src/resolve.ts +962 -391
- package/src/run-cli.ts +34 -3
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/standard-modules.ts +52 -38
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +26 -14
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +87 -17
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
package/src/run-cli.ts
CHANGED
|
@@ -10,11 +10,12 @@ import {
|
|
|
10
10
|
import { spawnSync } from "node:child_process";
|
|
11
11
|
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
12
12
|
import { availableParallelism } from "node:os";
|
|
13
|
-
import type { CheckResult, CheckStatus, TraceStep } from "./check";
|
|
13
|
+
import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
|
|
14
14
|
import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
15
15
|
import { parseConfig } from "./config";
|
|
16
16
|
import {
|
|
17
17
|
createSanyError,
|
|
18
|
+
formatSanyWarnings,
|
|
18
19
|
getErrorExitCode,
|
|
19
20
|
TlaConfigError,
|
|
20
21
|
TlaError,
|
|
@@ -28,7 +29,7 @@ import type { LineRange } from "./obligation-generate";
|
|
|
28
29
|
import { getAtPrefix, getLocusText } from "./obligation-expression";
|
|
29
30
|
import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
|
|
30
31
|
import { check } from "./parallel";
|
|
31
|
-
import {
|
|
32
|
+
import { parseModuleFile } from "./sany-parse-error";
|
|
32
33
|
import type { StateDump } from "./state-graph";
|
|
33
34
|
import {
|
|
34
35
|
formatProbability,
|
|
@@ -137,14 +138,42 @@ const getTemporalHeadline = (names: string[]) =>
|
|
|
137
138
|
? `Error: Temporal property ${names[0]} was violated.\n`
|
|
138
139
|
: `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
|
|
139
140
|
|
|
141
|
+
const getFrames = (callStack: string[]) =>
|
|
142
|
+
callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
|
|
143
|
+
|
|
144
|
+
const printCallStack = (frames: string) =>
|
|
145
|
+
console.log(
|
|
146
|
+
`Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const printReplay = (replay: Replay) => {
|
|
150
|
+
replay.output.forEach((line) => console.log(line));
|
|
151
|
+
const frames = getFrames(replay.callStack ?? []);
|
|
152
|
+
const fingerprint = replay.fingerprint;
|
|
153
|
+
if (replay.isBeforeError) {
|
|
154
|
+
if (frames) printCallStack(frames);
|
|
155
|
+
} else if (replay.callStack && fingerprint) {
|
|
156
|
+
const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
|
|
157
|
+
console.log(
|
|
158
|
+
`Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
|
|
159
|
+
);
|
|
160
|
+
} else if (replay.callStack) {
|
|
161
|
+
printCallStack(frames);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
140
165
|
const printResult = (result: CheckResult) => {
|
|
141
166
|
if (result.failure) {
|
|
167
|
+
const replay = result.failure.replay;
|
|
168
|
+
if (replay?.isBeforeError) printReplay(replay);
|
|
142
169
|
console.log(`Error: ${result.failure.message}`);
|
|
143
170
|
if (result.failure.initialState) {
|
|
144
171
|
console.log("While working on the initial state:");
|
|
145
172
|
printState(result.failure.initialState);
|
|
146
173
|
}
|
|
147
174
|
printTrace(result.failure.trace);
|
|
175
|
+
if (replay && !replay.isBeforeError) printReplay(replay);
|
|
176
|
+
if (result.failure.isWithoutStatistics) return;
|
|
148
177
|
} else if (result.violation) {
|
|
149
178
|
const violation = result.violation;
|
|
150
179
|
const name = violation.name;
|
|
@@ -219,7 +248,7 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
|
219
248
|
}
|
|
220
249
|
const source = readFileSync(specPath, "utf8");
|
|
221
250
|
const fileName = basename(specPath, ".tla");
|
|
222
|
-
const moduleName =
|
|
251
|
+
const moduleName = parseModuleFile(source, fileName).name;
|
|
223
252
|
if (moduleName !== fileName) {
|
|
224
253
|
throw createSanyError(
|
|
225
254
|
[
|
|
@@ -305,6 +334,8 @@ const printError = (error: TlaError, path?: string) => {
|
|
|
305
334
|
}
|
|
306
335
|
const report = error instanceof TlaSyntaxError ? error.sany : undefined;
|
|
307
336
|
if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
|
|
337
|
+
if (report.parseError !== undefined) console.log(report.parseError);
|
|
338
|
+
if (report.warnings.length > 0) console.log(formatSanyWarnings(report.warnings));
|
|
308
339
|
console.log(
|
|
309
340
|
report.isFatal
|
|
310
341
|
? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
|
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
import { TlaParseError } from "./errors";
|
|
2
|
+
|
|
3
|
+
// The token kinds of SANY's TLA+ grammar, numbered as in its TLAplusParserConstants.
|
|
4
|
+
export const EOF = 0;
|
|
5
|
+
export const BEGIN_PRAGMA = 2;
|
|
6
|
+
export const _BM1 = 3;
|
|
7
|
+
export const NUMBER = 20;
|
|
8
|
+
export const _BM2 = 21;
|
|
9
|
+
export const _BM0 = 35;
|
|
10
|
+
export const SEPARATOR = 36;
|
|
11
|
+
export const END_MODULE = 37;
|
|
12
|
+
export const ACTION = 38;
|
|
13
|
+
export const ASSUME = 39;
|
|
14
|
+
export const BOXASSUME = 40;
|
|
15
|
+
export const ASSUMPTION = 41;
|
|
16
|
+
export const CASE = 42;
|
|
17
|
+
export const CHOOSE = 43;
|
|
18
|
+
export const CONSTANT = 44;
|
|
19
|
+
export const ELSE = 45;
|
|
20
|
+
export const EXCEPT = 46;
|
|
21
|
+
export const EXISTS = 47;
|
|
22
|
+
export const EXTENDS = 48;
|
|
23
|
+
export const FORALL = 49;
|
|
24
|
+
export const IF = 50;
|
|
25
|
+
export const INSTANCE = 51;
|
|
26
|
+
export const LET = 52;
|
|
27
|
+
export const LETIN = 53;
|
|
28
|
+
export const LOCAL = 54;
|
|
29
|
+
export const MODULE = 55;
|
|
30
|
+
export const NEW = 56;
|
|
31
|
+
export const OTHER = 57;
|
|
32
|
+
export const PROPOSITION = 58;
|
|
33
|
+
export const SF = 59;
|
|
34
|
+
export const T_EXISTS = 60;
|
|
35
|
+
export const T_FORALL = 61;
|
|
36
|
+
export const THEN = 62;
|
|
37
|
+
export const BY = 63;
|
|
38
|
+
export const ONLY = 64;
|
|
39
|
+
export const DEFINE = 65;
|
|
40
|
+
export const DF = 66;
|
|
41
|
+
export const THEOREM = 67;
|
|
42
|
+
export const USE = 68;
|
|
43
|
+
export const HIDE = 69;
|
|
44
|
+
export const HAVE = 70;
|
|
45
|
+
export const OBVIOUS = 71;
|
|
46
|
+
export const OMITTED = 72;
|
|
47
|
+
export const LAMBDA = 73;
|
|
48
|
+
export const TAKE = 74;
|
|
49
|
+
export const PROOF = 75;
|
|
50
|
+
export const PROVE = 76;
|
|
51
|
+
export const BOXPROVE = 77;
|
|
52
|
+
export const QED = 78;
|
|
53
|
+
export const RECURSIVE = 79;
|
|
54
|
+
export const STATE = 80;
|
|
55
|
+
export const TEMPORAL = 81;
|
|
56
|
+
export const PICK = 82;
|
|
57
|
+
export const WITNESS = 83;
|
|
58
|
+
export const SUFFICES = 84;
|
|
59
|
+
export const VARIABLE = 85;
|
|
60
|
+
export const WF = 86;
|
|
61
|
+
export const WITH = 87;
|
|
62
|
+
export const COMMA = 88;
|
|
63
|
+
export const COLON = 89;
|
|
64
|
+
export const COLONCOLON = 90;
|
|
65
|
+
export const DOT = 91;
|
|
66
|
+
export const US = 92;
|
|
67
|
+
export const DEF = 93;
|
|
68
|
+
export const LBR = 94;
|
|
69
|
+
export const RBR = 95;
|
|
70
|
+
export const DEFBREAK = 96;
|
|
71
|
+
export const LSB = 97;
|
|
72
|
+
export const ARSB = 98;
|
|
73
|
+
export const RSB = 99;
|
|
74
|
+
export const LBC = 100;
|
|
75
|
+
export const RBC = 101;
|
|
76
|
+
export const LAB = 102;
|
|
77
|
+
export const ARAB = 103;
|
|
78
|
+
export const RAB = 104;
|
|
79
|
+
export const BANG = 105;
|
|
80
|
+
export const ARROW = 106;
|
|
81
|
+
export const SUBSTITUTE = 107;
|
|
82
|
+
export const MAPTO = 108;
|
|
83
|
+
export const NUMBER_LITERAL = 109;
|
|
84
|
+
export const STRING_LITERAL = 110;
|
|
85
|
+
const BAND = 111;
|
|
86
|
+
const BOR = 112;
|
|
87
|
+
export const PRIME = 113;
|
|
88
|
+
export const NEGATIVE = 117;
|
|
89
|
+
export const CASESEP = 121;
|
|
90
|
+
export const AND = 129;
|
|
91
|
+
export const OR = 132;
|
|
92
|
+
export const IN = 147;
|
|
93
|
+
export const EQUALS = 185;
|
|
94
|
+
export const MINUS = 193;
|
|
95
|
+
export const IDENTIFIER = 289;
|
|
96
|
+
export const PROOF_STEP = 290;
|
|
97
|
+
export const PROOF_IMPLICIT_STEP = 291;
|
|
98
|
+
export const PROOF_STEP_DOT = 292;
|
|
99
|
+
export const BARE_LEVEL = 293;
|
|
100
|
+
export const UNNUMBERED_STEP = 294;
|
|
101
|
+
|
|
102
|
+
export interface SanyToken {
|
|
103
|
+
kind: number;
|
|
104
|
+
image: string;
|
|
105
|
+
beginLine: number;
|
|
106
|
+
beginColumn: number;
|
|
107
|
+
endLine: number;
|
|
108
|
+
endColumn: number;
|
|
109
|
+
next: SanyToken | undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface SanyLexer {
|
|
113
|
+
getNextToken: () => SanyToken;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const enum LexicalState {
|
|
117
|
+
Default,
|
|
118
|
+
Pragma,
|
|
119
|
+
Spec,
|
|
120
|
+
InComment,
|
|
121
|
+
Embedded,
|
|
122
|
+
InEndOfLineComment,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const UNICODE_INFIX_OPERATORS = "≈≔≍◯⩴●∩⋅∘≅∪÷≐‥…≡‼≥≫⇔⇒⫤↝≤≪⊣≠∉⊙⊖⊕⊘⊗⇸≺⪯∝⁇⊨⊢∼≃⊓⊔⊏⊑⊐⊒⋆⊂⊆≻⪰⊃⊇×⊎‖≀";
|
|
126
|
+
|
|
127
|
+
const LITERALS: [number, string[]][] = [
|
|
128
|
+
[ACTION, ["ACTION", "ACTIONS"]],
|
|
129
|
+
[ASSUME, ["ASSUME"]],
|
|
130
|
+
[BOXASSUME, ["[]ASSUME"]],
|
|
131
|
+
[ASSUMPTION, ["ASSUMPTION", "AXIOM"]],
|
|
132
|
+
[CASE, ["CASE"]],
|
|
133
|
+
[CHOOSE, ["CHOOSE"]],
|
|
134
|
+
[CONSTANT, ["CONSTANT", "CONSTANTS"]],
|
|
135
|
+
[ELSE, ["ELSE"]],
|
|
136
|
+
[EXCEPT, ["EXCEPT"]],
|
|
137
|
+
[EXISTS, ["\\E", "\\exists", "∃"]],
|
|
138
|
+
[EXTENDS, ["EXTENDS"]],
|
|
139
|
+
[FORALL, ["\\A", "\\forall", "∀"]],
|
|
140
|
+
[IF, ["IF"]],
|
|
141
|
+
[INSTANCE, ["INSTANCE"]],
|
|
142
|
+
[LET, ["LET"]],
|
|
143
|
+
[LETIN, ["IN"]],
|
|
144
|
+
[LOCAL, ["LOCAL"]],
|
|
145
|
+
[MODULE, ["MODULE"]],
|
|
146
|
+
[NEW, ["NEW"]],
|
|
147
|
+
[OTHER, ["OTHER"]],
|
|
148
|
+
[PROPOSITION, ["PROPOSITION", "LEMMA", "COROLLARY"]],
|
|
149
|
+
[SF, ["SF_"]],
|
|
150
|
+
[T_EXISTS, ["\\EE"]],
|
|
151
|
+
[T_FORALL, ["\\AA"]],
|
|
152
|
+
[THEN, ["THEN"]],
|
|
153
|
+
[BY, ["BY"]],
|
|
154
|
+
[ONLY, ["ONLY"]],
|
|
155
|
+
[DEFINE, ["DEFINE"]],
|
|
156
|
+
[DF, ["DEF", "DEFS"]],
|
|
157
|
+
[THEOREM, ["THEOREM"]],
|
|
158
|
+
[USE, ["USE"]],
|
|
159
|
+
[HIDE, ["HIDE"]],
|
|
160
|
+
[HAVE, ["HAVE"]],
|
|
161
|
+
[OBVIOUS, ["OBVIOUS"]],
|
|
162
|
+
[OMITTED, ["OMITTED"]],
|
|
163
|
+
[LAMBDA, ["LAMBDA"]],
|
|
164
|
+
[TAKE, ["TAKE"]],
|
|
165
|
+
[PROOF, ["PROOF"]],
|
|
166
|
+
[PROVE, ["PROVE"]],
|
|
167
|
+
[BOXPROVE, ["[]PROVE"]],
|
|
168
|
+
[QED, ["QED"]],
|
|
169
|
+
[RECURSIVE, ["RECURSIVE"]],
|
|
170
|
+
[STATE, ["STATE"]],
|
|
171
|
+
[TEMPORAL, ["TEMPORAL", "TEMPORALS"]],
|
|
172
|
+
[PICK, ["PICK"]],
|
|
173
|
+
[WITNESS, ["WITNESS"]],
|
|
174
|
+
[SUFFICES, ["SUFFICES"]],
|
|
175
|
+
[VARIABLE, ["VARIABLE", "VARIABLES"]],
|
|
176
|
+
[WF, ["WF_"]],
|
|
177
|
+
[WITH, ["WITH"]],
|
|
178
|
+
[COMMA, [","]],
|
|
179
|
+
[COLON, [":"]],
|
|
180
|
+
[COLONCOLON, ["::", "∷"]],
|
|
181
|
+
[DOT, ["."]],
|
|
182
|
+
[US, ["_"]],
|
|
183
|
+
[DEF, ["==", "≜"]],
|
|
184
|
+
[LBR, ["("]],
|
|
185
|
+
[RBR, [")"]],
|
|
186
|
+
[DEFBREAK, ["-|-"]],
|
|
187
|
+
[LSB, ["["]],
|
|
188
|
+
[ARSB, ["]_"]],
|
|
189
|
+
[RSB, ["]"]],
|
|
190
|
+
[LBC, ["{"]],
|
|
191
|
+
[RBC, ["}"]],
|
|
192
|
+
[LAB, ["<<", "⟨"]],
|
|
193
|
+
[ARAB, [">>_", "⟩_"]],
|
|
194
|
+
[RAB, [">>", "⟩"]],
|
|
195
|
+
[BANG, ["!"]],
|
|
196
|
+
[ARROW, ["->", "→"]],
|
|
197
|
+
[SUBSTITUTE, ["<-", "←"]],
|
|
198
|
+
[MAPTO, ["|->", "↦"]],
|
|
199
|
+
[PRIME, ["'"]],
|
|
200
|
+
[114, ["^+", "⁺"]],
|
|
201
|
+
[115, ["^*"]],
|
|
202
|
+
[116, ["^#"]],
|
|
203
|
+
[NEGATIVE, ["-."]],
|
|
204
|
+
[118, ["\\lnot", "¬"]],
|
|
205
|
+
[119, ["\\neg"]],
|
|
206
|
+
[120, ["~"]],
|
|
207
|
+
[CASESEP, ["[]", "□"]],
|
|
208
|
+
[122, ["<>", "◇"]],
|
|
209
|
+
[123, ["ENABLED"]],
|
|
210
|
+
[124, ["UNCHANGED"]],
|
|
211
|
+
[125, ["SUBSET"]],
|
|
212
|
+
[126, ["UNION"]],
|
|
213
|
+
[127, ["DOMAIN"]],
|
|
214
|
+
...[
|
|
215
|
+
"//",
|
|
216
|
+
"/\\",
|
|
217
|
+
"/=",
|
|
218
|
+
"/",
|
|
219
|
+
"\\/",
|
|
220
|
+
"\\approx",
|
|
221
|
+
"\\asymp",
|
|
222
|
+
"\\bigcirc",
|
|
223
|
+
"\\bullet",
|
|
224
|
+
"\\cap",
|
|
225
|
+
"\\cdot",
|
|
226
|
+
"\\circ",
|
|
227
|
+
"\\cong",
|
|
228
|
+
"\\cup",
|
|
229
|
+
"\\div",
|
|
230
|
+
"\\doteq",
|
|
231
|
+
"\\equiv",
|
|
232
|
+
"\\geq",
|
|
233
|
+
"\\gg",
|
|
234
|
+
"\\in",
|
|
235
|
+
"\\intersect",
|
|
236
|
+
"\\union",
|
|
237
|
+
"\\land",
|
|
238
|
+
"\\leq",
|
|
239
|
+
"\\ll",
|
|
240
|
+
"\\lor",
|
|
241
|
+
"\\o",
|
|
242
|
+
"\\odot",
|
|
243
|
+
"\\ominus",
|
|
244
|
+
"\\oplus",
|
|
245
|
+
"\\oslash",
|
|
246
|
+
"\\otimes",
|
|
247
|
+
"\\prec",
|
|
248
|
+
"\\preceq",
|
|
249
|
+
"\\propto",
|
|
250
|
+
"\\sim",
|
|
251
|
+
"\\simeq",
|
|
252
|
+
"\\sqcap",
|
|
253
|
+
"\\sqcup",
|
|
254
|
+
"\\sqsubset",
|
|
255
|
+
"\\sqsupset",
|
|
256
|
+
"\\sqsubseteq",
|
|
257
|
+
"\\sqsupseteq",
|
|
258
|
+
"\\star",
|
|
259
|
+
"\\subset",
|
|
260
|
+
"\\subseteq",
|
|
261
|
+
"\\succ",
|
|
262
|
+
"\\succeq",
|
|
263
|
+
"\\supset",
|
|
264
|
+
"\\supseteq",
|
|
265
|
+
"\\uplus",
|
|
266
|
+
"\\wr",
|
|
267
|
+
"\\",
|
|
268
|
+
"~>",
|
|
269
|
+
"=>",
|
|
270
|
+
"=<",
|
|
271
|
+
"=|",
|
|
272
|
+
"=",
|
|
273
|
+
"##",
|
|
274
|
+
"#",
|
|
275
|
+
"^^",
|
|
276
|
+
"^",
|
|
277
|
+
"--",
|
|
278
|
+
"-|",
|
|
279
|
+
"-+->",
|
|
280
|
+
"-",
|
|
281
|
+
"**",
|
|
282
|
+
"*",
|
|
283
|
+
"++",
|
|
284
|
+
"+",
|
|
285
|
+
"<=>",
|
|
286
|
+
"<:",
|
|
287
|
+
"<=",
|
|
288
|
+
"<",
|
|
289
|
+
">=",
|
|
290
|
+
">",
|
|
291
|
+
"...",
|
|
292
|
+
"..",
|
|
293
|
+
"||",
|
|
294
|
+
"|",
|
|
295
|
+
"|-",
|
|
296
|
+
"|=",
|
|
297
|
+
"&&",
|
|
298
|
+
"&",
|
|
299
|
+
"$$",
|
|
300
|
+
"$",
|
|
301
|
+
"??",
|
|
302
|
+
"%%",
|
|
303
|
+
"%",
|
|
304
|
+
"@@",
|
|
305
|
+
"!!",
|
|
306
|
+
":>",
|
|
307
|
+
":=",
|
|
308
|
+
"::=",
|
|
309
|
+
"(+)",
|
|
310
|
+
"(-)",
|
|
311
|
+
"(.)",
|
|
312
|
+
"(/)",
|
|
313
|
+
"(\\X)",
|
|
314
|
+
"\\notin",
|
|
315
|
+
...UNICODE_INFIX_OPERATORS,
|
|
316
|
+
"\\times",
|
|
317
|
+
"\\X",
|
|
318
|
+
].map((operator, index): [number, string[]] => [128 + index, [operator]]),
|
|
319
|
+
[AND, ["∧"]],
|
|
320
|
+
[OR, ["∨"]],
|
|
321
|
+
[IN, ["∈"]],
|
|
322
|
+
];
|
|
323
|
+
|
|
324
|
+
const getLiteralsByFirstCharacter = () => {
|
|
325
|
+
const literals = new Map<string, [number, string][]>();
|
|
326
|
+
for (const entry of LITERALS) {
|
|
327
|
+
for (const image of entry[1])
|
|
328
|
+
literals.set(image[0], [...(literals.get(image[0]) ?? []), [entry[0], image]]);
|
|
329
|
+
}
|
|
330
|
+
return literals;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const LITERALS_BY_FIRST_CHARACTER = getLiteralsByFirstCharacter();
|
|
334
|
+
|
|
335
|
+
const BEGIN_MODULE = /-{4,} *MODULE/y;
|
|
336
|
+
|
|
337
|
+
const BAND_PATTERNS = [
|
|
338
|
+
/[0-9][A-Za-z0-9]*\.\/\\/y,
|
|
339
|
+
/[WS][a-zA-EG-Z0-9][A-Za-z0-9]*\.\/\\/y,
|
|
340
|
+
/[a-zA-RT-VX-Z][A-Za-z0-9]*\.\/\\/y,
|
|
341
|
+
];
|
|
342
|
+
|
|
343
|
+
const BOR_PATTERNS = [
|
|
344
|
+
/[0-9][A-Za-z0-9]*\.\\\//y,
|
|
345
|
+
/[WS][a-zA-EG-Z0-9][A-Za-z0-9]*\.\\\//y,
|
|
346
|
+
/[a-zA-RT-VX-Z][A-Za-z0-9]*\.\\\//y,
|
|
347
|
+
];
|
|
348
|
+
|
|
349
|
+
const IDENTIFIER_PATTERNS = [
|
|
350
|
+
/_[A-Za-z0-9_]*[A-Za-z][A-Za-z0-9_]*/y,
|
|
351
|
+
/[0-9][A-Za-z0-9_]*[A-Za-z][A-Za-z0-9_]*/y,
|
|
352
|
+
/[WS](?:[a-zA-EG-Z_0-9][A-Za-z0-9_]*)?/y,
|
|
353
|
+
/[WS]F(?:[A-Za-z0-9][A-Za-z0-9_]*)?/y,
|
|
354
|
+
/[a-zA-RT-VX-Z][A-Za-z0-9_]*/y,
|
|
355
|
+
/[@ℕℤℝ]/y,
|
|
356
|
+
];
|
|
357
|
+
|
|
358
|
+
const SPEC_PATTERNS: [number, RegExp[]][] = [
|
|
359
|
+
[_BM0, [BEGIN_MODULE]],
|
|
360
|
+
[SEPARATOR, [/-{4,}/y]],
|
|
361
|
+
[END_MODULE, [/={4,}/y]],
|
|
362
|
+
[NUMBER_LITERAL, [/[0-9]+|\\[oO][0-7]+|\\[bB][01]+|\\[hH][0-9a-fA-F]+/y]],
|
|
363
|
+
[STRING_LITERAL, [/"(?:[^"\n\r\\]|\\[ntrf\\"])*"/y]],
|
|
364
|
+
[BAND, BAND_PATTERNS],
|
|
365
|
+
[BOR, BOR_PATTERNS],
|
|
366
|
+
[IDENTIFIER, IDENTIFIER_PATTERNS],
|
|
367
|
+
[PROOF_STEP, [/<[0-9]+>[A-Za-z0-9_]+/y]],
|
|
368
|
+
[PROOF_IMPLICIT_STEP, [/<[+*]>[A-Za-z0-9]+/y]],
|
|
369
|
+
[PROOF_STEP_DOT, [/<(?:[0-9]+|\+|\*)>[A-Za-z0-9_]+\.+/y]],
|
|
370
|
+
[BARE_LEVEL, [/<(?:[0-9]+|\+|\*)>/y]],
|
|
371
|
+
[UNNUMBERED_STEP, [/<(?:[0-9]+|\+|\*)>(?:\*\.*|-\.*|\.+)/y]],
|
|
372
|
+
];
|
|
373
|
+
|
|
374
|
+
const PRAGMA_PATTERNS: [number, RegExp[]][] = [
|
|
375
|
+
[NUMBER, [/[0-9]+/y]],
|
|
376
|
+
[_BM2, [BEGIN_MODULE]],
|
|
377
|
+
[BAND, BAND_PATTERNS],
|
|
378
|
+
[BOR, BOR_PATTERNS],
|
|
379
|
+
[IDENTIFIER, IDENTIFIER_PATTERNS],
|
|
380
|
+
];
|
|
381
|
+
|
|
382
|
+
const getPatternLength = (patterns: RegExp[], source: string, index: number) =>
|
|
383
|
+
Math.max(
|
|
384
|
+
0,
|
|
385
|
+
...patterns.map((pattern) => {
|
|
386
|
+
pattern.lastIndex = index;
|
|
387
|
+
return pattern.exec(source)?.[0].length ?? 0;
|
|
388
|
+
}),
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
// The longest token at index, the lowest kind winning a tie, as JavaCC's token manager picks it.
|
|
392
|
+
const matchToken = (
|
|
393
|
+
source: string,
|
|
394
|
+
index: number,
|
|
395
|
+
patterns: [number, RegExp[]][],
|
|
396
|
+
isSpec: boolean,
|
|
397
|
+
) => {
|
|
398
|
+
const candidates: [number, number][] = patterns.map(([kind, kindPatterns]) => [
|
|
399
|
+
kind,
|
|
400
|
+
getPatternLength(kindPatterns, source, index),
|
|
401
|
+
]);
|
|
402
|
+
if (isSpec) {
|
|
403
|
+
for (const [kind, image] of LITERALS_BY_FIRST_CHARACTER.get(source[index]) ?? []) {
|
|
404
|
+
if (source.startsWith(image, index)) candidates.push([kind, image.length]);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return candidates.reduce<[number, number]>(
|
|
408
|
+
(best, candidate) =>
|
|
409
|
+
candidate[1] > best[1] ||
|
|
410
|
+
(candidate[1] === best[1] && candidate[1] > 0 && candidate[0] < best[0])
|
|
411
|
+
? candidate
|
|
412
|
+
: best,
|
|
413
|
+
[EOF, 0],
|
|
414
|
+
);
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
// The length of input at index that some token could still begin with, when no token matches it.
|
|
418
|
+
const getViablePrefixLength = (source: string, index: number) => {
|
|
419
|
+
if (source[index] === "?") return 1;
|
|
420
|
+
if (source[index] !== '"') return 0;
|
|
421
|
+
const stringPrefix = /"(?:[^"\n\r\\]|\\[ntrf\\"])*\\?/y;
|
|
422
|
+
stringPrefix.lastIndex = index;
|
|
423
|
+
return stringPrefix.exec(source)?.[0].length ?? 0;
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const ESCAPES = new Map([
|
|
427
|
+
["\b", "\\b"],
|
|
428
|
+
["\t", "\\t"],
|
|
429
|
+
["\n", "\\n"],
|
|
430
|
+
["\f", "\\f"],
|
|
431
|
+
["\r", "\\r"],
|
|
432
|
+
['"', '\\"'],
|
|
433
|
+
["'", "\\'"],
|
|
434
|
+
["\\", "\\\\"],
|
|
435
|
+
["\0", ""],
|
|
436
|
+
]);
|
|
437
|
+
|
|
438
|
+
// Escapes text as JavaCC's error messages print it.
|
|
439
|
+
export const escapeImage = (text: string) =>
|
|
440
|
+
text
|
|
441
|
+
.split("")
|
|
442
|
+
.map((unit) => {
|
|
443
|
+
const code = unit.charCodeAt(0);
|
|
444
|
+
return (
|
|
445
|
+
ESCAPES.get(unit) ??
|
|
446
|
+
(code < 0x20 || code > 0x7e ? `\\u${code.toString(16).padStart(4, "0")}` : unit)
|
|
447
|
+
);
|
|
448
|
+
})
|
|
449
|
+
.join("");
|
|
450
|
+
|
|
451
|
+
// Reads SANY tokens one at a time, so a lexical error surfaces only when the parser reaches it.
|
|
452
|
+
export const createSanyLexer = (source: string): SanyLexer => {
|
|
453
|
+
const lines: number[] = [];
|
|
454
|
+
const columns: number[] = [];
|
|
455
|
+
const position = { line: 1, column: 0, isAfterCarriageReturn: false, isAfterLineFeed: false };
|
|
456
|
+
for (const character of source.split("")) {
|
|
457
|
+
position.column++;
|
|
458
|
+
if (position.isAfterLineFeed) {
|
|
459
|
+
position.isAfterLineFeed = false;
|
|
460
|
+
position.line++;
|
|
461
|
+
position.column = 1;
|
|
462
|
+
} else if (position.isAfterCarriageReturn) {
|
|
463
|
+
position.isAfterCarriageReturn = false;
|
|
464
|
+
if (character === "\n") position.isAfterLineFeed = true;
|
|
465
|
+
else {
|
|
466
|
+
position.line++;
|
|
467
|
+
position.column = 1;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (character === "\r") position.isAfterCarriageReturn = true;
|
|
471
|
+
if (character === "\n") position.isAfterLineFeed = true;
|
|
472
|
+
if (character === "\t")
|
|
473
|
+
position.column = position.column - 1 + (8 - ((position.column - 1) % 8));
|
|
474
|
+
lines.push(position.line);
|
|
475
|
+
columns.push(position.column);
|
|
476
|
+
}
|
|
477
|
+
const cursor = { state: LexicalState.Default, index: 0, commentDepth: 0 };
|
|
478
|
+
const createToken = (kind: number, begin: number, end: number): SanyToken => ({
|
|
479
|
+
kind,
|
|
480
|
+
image: source.slice(begin, end),
|
|
481
|
+
beginLine: lines[begin],
|
|
482
|
+
beginColumn: columns[begin],
|
|
483
|
+
endLine: lines[end - 1],
|
|
484
|
+
endColumn: columns[end - 1],
|
|
485
|
+
next: undefined,
|
|
486
|
+
});
|
|
487
|
+
const throwLexicalError = (begin: number, after: string, killer: number | undefined) => {
|
|
488
|
+
if (killer !== undefined && killer + 1 < source.length) {
|
|
489
|
+
throw new TlaParseError(
|
|
490
|
+
`Lexical error at line ${lines[killer]}, column ${columns[killer]}. Encountered: "${escapeImage(source[killer])}" (${source.charCodeAt(killer)}), after : "${escapeImage(after)}"`,
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
const last = source.length - 1;
|
|
494
|
+
const isLineEnd = source[last] === "\n" || source[last] === "\r";
|
|
495
|
+
const message = `Lexical error at line ${isLineEnd ? lines[last] + 1 : lines[last]}, column ${isLineEnd ? 0 : columns[last] + 1}. Encountered: <EOF> after : "${escapeImage(after)}"`;
|
|
496
|
+
throw new TlaParseError(
|
|
497
|
+
lines[begin] === lines[last]
|
|
498
|
+
? message
|
|
499
|
+
: `Lexical {error: EOF reached, possibly open comment starting around line ${lines[begin]}`,
|
|
500
|
+
);
|
|
501
|
+
};
|
|
502
|
+
const readComment = (begin: number): SanyToken | undefined => {
|
|
503
|
+
for (;;) {
|
|
504
|
+
if (cursor.index >= source.length) return throwLexicalError(begin, "", undefined);
|
|
505
|
+
const opening = source.startsWith("(*.", cursor.index)
|
|
506
|
+
? 3
|
|
507
|
+
: source.startsWith("(*", cursor.index)
|
|
508
|
+
? 2
|
|
509
|
+
: 0;
|
|
510
|
+
if (cursor.state === LexicalState.InEndOfLineComment) {
|
|
511
|
+
const lineEnd = source.startsWith("\r\n", cursor.index)
|
|
512
|
+
? 2
|
|
513
|
+
: source[cursor.index] === "\n" || source[cursor.index] === "\r"
|
|
514
|
+
? 1
|
|
515
|
+
: 0;
|
|
516
|
+
if (lineEnd > 0) {
|
|
517
|
+
cursor.index += lineEnd;
|
|
518
|
+
cursor.state = LexicalState.Spec;
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
} else if (opening > 0) {
|
|
522
|
+
cursor.index += opening;
|
|
523
|
+
cursor.commentDepth++;
|
|
524
|
+
cursor.state = LexicalState.Embedded;
|
|
525
|
+
return undefined;
|
|
526
|
+
} else if (source.startsWith("*)", cursor.index)) {
|
|
527
|
+
cursor.index += 2;
|
|
528
|
+
if (cursor.state === LexicalState.InComment) cursor.state = LexicalState.Spec;
|
|
529
|
+
else {
|
|
530
|
+
cursor.commentDepth--;
|
|
531
|
+
if (cursor.commentDepth === 0) cursor.state = LexicalState.InComment;
|
|
532
|
+
}
|
|
533
|
+
return undefined;
|
|
534
|
+
}
|
|
535
|
+
cursor.index++;
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
const getNextToken = (): SanyToken => {
|
|
539
|
+
for (;;) {
|
|
540
|
+
if (cursor.index >= source.length) {
|
|
541
|
+
const last = source.length - 1;
|
|
542
|
+
return {
|
|
543
|
+
kind: EOF,
|
|
544
|
+
image: "",
|
|
545
|
+
beginLine: lines[last] ?? 0,
|
|
546
|
+
beginColumn: columns[last] ?? 0,
|
|
547
|
+
endLine: lines[last] ?? 0,
|
|
548
|
+
endColumn: columns[last] ?? 0,
|
|
549
|
+
next: undefined,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
const begin = cursor.index;
|
|
553
|
+
if (cursor.state === LexicalState.Default) {
|
|
554
|
+
const pragma = source.startsWith("--->", cursor.index) ? 4 : 0;
|
|
555
|
+
BEGIN_MODULE.lastIndex = cursor.index;
|
|
556
|
+
const module = BEGIN_MODULE.exec(source)?.[0].length ?? 0;
|
|
557
|
+
if (module > 0 && module >= pragma) {
|
|
558
|
+
cursor.index += module;
|
|
559
|
+
cursor.state = LexicalState.Spec;
|
|
560
|
+
return createToken(_BM1, begin, cursor.index);
|
|
561
|
+
}
|
|
562
|
+
if (pragma > 0) {
|
|
563
|
+
cursor.index += pragma;
|
|
564
|
+
cursor.state = LexicalState.Pragma;
|
|
565
|
+
return createToken(BEGIN_PRAGMA, begin, cursor.index);
|
|
566
|
+
}
|
|
567
|
+
cursor.index++;
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
if (cursor.state === LexicalState.Pragma) {
|
|
571
|
+
const [kind, length] = matchToken(source, cursor.index, PRAGMA_PATTERNS, false);
|
|
572
|
+
if (length === 0) {
|
|
573
|
+
cursor.index++;
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
cursor.index += length;
|
|
577
|
+
if (kind === _BM2) cursor.state = LexicalState.Spec;
|
|
578
|
+
return createToken(kind, begin, cursor.index);
|
|
579
|
+
}
|
|
580
|
+
if (cursor.state !== LexicalState.Spec) {
|
|
581
|
+
readComment(begin);
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (" \t\n\r".includes(source[cursor.index])) {
|
|
585
|
+
cursor.index++;
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
if (source.startsWith("(*", cursor.index) || source.startsWith("\\*", cursor.index)) {
|
|
589
|
+
cursor.state =
|
|
590
|
+
source[cursor.index] === "(" ? LexicalState.InComment : LexicalState.InEndOfLineComment;
|
|
591
|
+
cursor.index += source.startsWith("(*.", cursor.index) ? 3 : 2;
|
|
592
|
+
readComment(begin);
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
const [kind, length] = matchToken(source, cursor.index, SPEC_PATTERNS, true);
|
|
596
|
+
if (length === 0) {
|
|
597
|
+
const viable = getViablePrefixLength(source, cursor.index);
|
|
598
|
+
const killer = cursor.index + viable;
|
|
599
|
+
if (killer < source.length) {
|
|
600
|
+
return throwLexicalError(
|
|
601
|
+
begin,
|
|
602
|
+
killer + 1 < source.length || viable === 0
|
|
603
|
+
? source.slice(begin, killer)
|
|
604
|
+
: source.slice(begin, killer + 1),
|
|
605
|
+
killer,
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
return throwLexicalError(begin, viable <= 1 ? "" : source.slice(begin), undefined);
|
|
609
|
+
}
|
|
610
|
+
cursor.index += length;
|
|
611
|
+
return createToken(kind, begin, cursor.index);
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
return { getNextToken };
|
|
615
|
+
};
|