tla-ts 0.0.3-dev.3cf2ad0 → 0.0.3-dev.3ef4193
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/README.md +16 -2
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +158 -8
- package/dist/index.d.ts +158 -8
- 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 +1351 -19
- package/dist/run-cli.js +1351 -19
- package/dist/transfer.cjs +88 -18
- package/dist/transfer.js +88 -18
- package/package.json +5 -1
- package/src/ast.ts +146 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1539 -235
- package/src/cli-options.ts +11 -5
- package/src/cli.ts +8 -1
- package/src/codegen.ts +77 -37
- package/src/config.ts +252 -179
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +524 -140
- package/src/errors.ts +224 -3
- package/src/evaluate.ts +796 -246
- package/src/exit-codes.ts +22 -43
- package/src/index.ts +1 -0
- package/src/instances.ts +19 -16
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +191 -45
- package/src/lexer.ts +37 -5
- package/src/live-check.ts +828 -0
- package/src/liveness.ts +218 -572
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +13 -4
- package/src/parallel.ts +73 -6
- package/src/parser.ts +436 -144
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1424 -415
- package/src/run-cli.ts +1200 -143
- package/src/runtime.ts +19 -1
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +42 -11
- package/src/standard-modules.ts +200 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +180 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +219 -107
- package/src/symmetry.ts +189 -53
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +42 -29
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +52 -43
- package/src/tlc-order.ts +73 -9
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +708 -180
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import type { Expression, SourceLocation, SourceRange } from "./ast";
|
|
2
|
+
import { TlaSyntaxError } from "./errors";
|
|
3
|
+
import { readAtAsIdentifier, tokenize, type Token } from "./lexer";
|
|
4
|
+
import { getTabStop } from "./sany-lexer";
|
|
5
|
+
import { getSanyParseError } from "./sany-parse-error";
|
|
6
|
+
import type { SanyLocation } from "./sany-nodes";
|
|
7
|
+
|
|
8
|
+
// The tokens of one file, to find the locations SANY gives to names, brackets and parentheses that
|
|
9
|
+
// the syntax tree of tla-ts leaves out, and the comments SANY attaches to definitions.
|
|
10
|
+
export interface SourceFile {
|
|
11
|
+
name: string;
|
|
12
|
+
source: string;
|
|
13
|
+
tokens: Token[];
|
|
14
|
+
lineOffsets: number[];
|
|
15
|
+
tokenIndexes: Map<string, number>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const OPENERS = new Map([
|
|
19
|
+
["(", ")"],
|
|
20
|
+
["[", "]"],
|
|
21
|
+
["{", "}"],
|
|
22
|
+
["<<", ">>"],
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const getPositionKey = (line: number, column: number) => `${line}:${column}`;
|
|
26
|
+
|
|
27
|
+
export const createSourceFile = (name: string, source: string): SourceFile => {
|
|
28
|
+
const tokens = tokenize(source)
|
|
29
|
+
.filter((token) => token.kind !== "End")
|
|
30
|
+
.map(readAtAsIdentifier);
|
|
31
|
+
const lineOffsets = [0, 0];
|
|
32
|
+
for (let index = 0; index < source.length; index++) {
|
|
33
|
+
if (source[index] === "\n") lineOffsets.push(index + 1);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
name,
|
|
37
|
+
source,
|
|
38
|
+
tokens,
|
|
39
|
+
lineOffsets,
|
|
40
|
+
tokenIndexes: new Map(
|
|
41
|
+
tokens.map((token, index) => [getPositionKey(token.line, token.column), index]),
|
|
42
|
+
),
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const isToken = (file: SourceFile, index: number, text: string) =>
|
|
47
|
+
file.tokens[index] !== undefined &&
|
|
48
|
+
file.tokens[index].text === text &&
|
|
49
|
+
file.tokens[index].kind !== "String";
|
|
50
|
+
|
|
51
|
+
// The number of tokens, in source order, before a position, or at or before it when isAtCounted.
|
|
52
|
+
const countTokensBefore = (file: SourceFile, position: SourceLocation, isAtCounted: boolean) => {
|
|
53
|
+
let low = 0;
|
|
54
|
+
let high = file.tokens.length;
|
|
55
|
+
while (low < high) {
|
|
56
|
+
const middle = (low + high) >>> 1;
|
|
57
|
+
const token = file.tokens[middle];
|
|
58
|
+
const isBefore =
|
|
59
|
+
token.line < position.line ||
|
|
60
|
+
(token.line === position.line &&
|
|
61
|
+
(isAtCounted ? token.column <= position.column : token.column < position.column));
|
|
62
|
+
if (isBefore) low = middle + 1;
|
|
63
|
+
else high = middle;
|
|
64
|
+
}
|
|
65
|
+
return low;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// The index of the first token at or after a position.
|
|
69
|
+
export const getTokenIndex = (file: SourceFile, position: SourceLocation) =>
|
|
70
|
+
file.tokenIndexes.get(getPositionKey(position.line, position.column)) ??
|
|
71
|
+
Math.min(countTokensBefore(file, position, false), file.tokens.length - 1);
|
|
72
|
+
|
|
73
|
+
// The index of the last token at or before a position.
|
|
74
|
+
const getEndTokenIndex = (file: SourceFile, position: SourceLocation) =>
|
|
75
|
+
Math.max(countTokensBefore(file, position, true) - 1, 0);
|
|
76
|
+
|
|
77
|
+
export const getMatchingIndex = (file: SourceFile, openIndex: number) => {
|
|
78
|
+
const opener = file.tokens[openIndex].text;
|
|
79
|
+
const closer = OPENERS.get(opener);
|
|
80
|
+
if (closer === undefined) return openIndex;
|
|
81
|
+
let depth = 0;
|
|
82
|
+
for (let index = openIndex; index < file.tokens.length; index++) {
|
|
83
|
+
if (isToken(file, index, opener)) depth++;
|
|
84
|
+
else if (isToken(file, index, closer)) {
|
|
85
|
+
depth--;
|
|
86
|
+
if (depth === 0) return index;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return openIndex;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export interface TokenSpan {
|
|
93
|
+
start: number;
|
|
94
|
+
end: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const getRangeSpan = (file: SourceFile, range: SourceRange): TokenSpan => ({
|
|
98
|
+
start: getTokenIndex(file, range.start),
|
|
99
|
+
end: getEndTokenIndex(file, range.end),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// The tokens of an expression. Some nodes, such as a tuple in a subscript, have no range, so their
|
|
103
|
+
// span runs from their first token to the bracket that closes it.
|
|
104
|
+
export const getExpressionSpan = (file: SourceFile, expression: Expression): TokenSpan => {
|
|
105
|
+
const range = expression.location.range;
|
|
106
|
+
if (range) return getRangeSpan(file, range);
|
|
107
|
+
const start = getTokenIndex(file, expression.location);
|
|
108
|
+
return { start, end: getMatchingIndex(file, start) };
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// The span with the parentheses around it, as SANY locates a parenthesized operand by its
|
|
112
|
+
// parentheses.
|
|
113
|
+
export const getParenthesizedSpan = (file: SourceFile, span: TokenSpan): TokenSpan => {
|
|
114
|
+
const start = span.start - 1;
|
|
115
|
+
const end = span.end + 1;
|
|
116
|
+
if (
|
|
117
|
+
isToken(file, start, "(") &&
|
|
118
|
+
isToken(file, end, ")") &&
|
|
119
|
+
getMatchingIndex(file, start) === end
|
|
120
|
+
) {
|
|
121
|
+
return getParenthesizedSpan(file, { start, end });
|
|
122
|
+
}
|
|
123
|
+
return span;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// SANY's parser keeps the parentheses around an argument of an operator, which the span of the
|
|
127
|
+
// argument's expression leaves out. Those of the operator's own arguments follow its name.
|
|
128
|
+
export const getArgumentSpan = (file: SourceFile, span: TokenSpan): TokenSpan => {
|
|
129
|
+
const start = span.start - 1;
|
|
130
|
+
const end = span.end + 1;
|
|
131
|
+
const isParenthesized =
|
|
132
|
+
isToken(file, start, "(") &&
|
|
133
|
+
getMatchingIndex(file, start) === end &&
|
|
134
|
+
(isToken(file, start - 1, "(") || isToken(file, start - 1, ","));
|
|
135
|
+
return isParenthesized ? getArgumentSpan(file, { start, end }) : span;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export const getSpanLocation = (file: SourceFile, span: TokenSpan): SanyLocation => ({
|
|
139
|
+
file: file.name,
|
|
140
|
+
beginLine: file.tokens[span.start].line,
|
|
141
|
+
beginColumn: file.tokens[span.start].column,
|
|
142
|
+
endLine: file.tokens[span.end].line,
|
|
143
|
+
endColumn: file.tokens[span.end].endColumn,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
export const getTokenLocation = (file: SourceFile, index: number) =>
|
|
147
|
+
getSpanLocation(file, { start: index, end: index });
|
|
148
|
+
|
|
149
|
+
const getOffset = (file: SourceFile, line: number, column: number) => {
|
|
150
|
+
let width = 0;
|
|
151
|
+
let offset = file.lineOffsets[line];
|
|
152
|
+
while (offset < file.source.length) {
|
|
153
|
+
width = file.source[offset] === "\t" ? getTabStop(width) : width + 1;
|
|
154
|
+
if (width >= column) return offset;
|
|
155
|
+
offset++;
|
|
156
|
+
}
|
|
157
|
+
return offset;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// The token as the source spells it, where the token's text is the name of the operator.
|
|
161
|
+
export const getTokenSource = (file: SourceFile, index: number) => {
|
|
162
|
+
const token = file.tokens[index];
|
|
163
|
+
const start = getOffset(file, token.line, token.column);
|
|
164
|
+
return file.source.slice(start, getOffset(file, token.line, token.endColumn) + 1);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// SANY lexes []ASSUME and []PROVE as single tokens.
|
|
168
|
+
export const isBoxToken = (file: SourceFile, index: number) => {
|
|
169
|
+
const box = file.tokens[index];
|
|
170
|
+
const next = file.tokens[index + 1];
|
|
171
|
+
return (
|
|
172
|
+
box?.kind === "Operator" &&
|
|
173
|
+
box.text === "[]" &&
|
|
174
|
+
next?.kind === "Keyword" &&
|
|
175
|
+
(next.text === "ASSUME" || next.text === "PROVE") &&
|
|
176
|
+
next.line === box.line &&
|
|
177
|
+
next.column === box.endColumn + 1
|
|
178
|
+
);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// HACK: the tla-ts parser has no []ASSUME or []PROVE, so it parses the source with each such []
|
|
182
|
+
// blanked out, which keeps every other token where it was. SANY's parser takes them only in an
|
|
183
|
+
// ASSUME/PROVE, which unboxing hides.
|
|
184
|
+
export const getUnboxedSource = (source: string) => {
|
|
185
|
+
const file = createSourceFile("", source);
|
|
186
|
+
const unboxed = file.tokens.reduce((text, token, index) => {
|
|
187
|
+
if (!isBoxToken(file, index)) return text;
|
|
188
|
+
const offset = getOffset(file, token.line, token.column);
|
|
189
|
+
return `${text.slice(0, offset)} ${text.slice(offset + 2)}`;
|
|
190
|
+
}, source);
|
|
191
|
+
if (unboxed !== source && getSanyParseError(source) !== undefined) {
|
|
192
|
+
throw new TlaSyntaxError("SANY rejects a []ASSUME or []PROVE outside an ASSUME/PROVE");
|
|
193
|
+
}
|
|
194
|
+
return unboxed;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// The comments between a token and the one before it, which SANY's parser attaches to the token.
|
|
198
|
+
export const getPreComments = (file: SourceFile, index: number) => {
|
|
199
|
+
const previous = file.tokens[index - 1];
|
|
200
|
+
const token = file.tokens[index];
|
|
201
|
+
const start = previous === undefined ? 0 : getOffset(file, previous.line, previous.endColumn) + 1;
|
|
202
|
+
const gap = file.source.slice(start, getOffset(file, token.line, token.column));
|
|
203
|
+
const comments: string[] = [];
|
|
204
|
+
let offset = 0;
|
|
205
|
+
while (offset < gap.length) {
|
|
206
|
+
if (gap.startsWith("\\*", offset)) {
|
|
207
|
+
const lineEnd = gap.indexOf("\n", offset);
|
|
208
|
+
const end = lineEnd === -1 ? gap.length : lineEnd + 1;
|
|
209
|
+
comments.push(gap.slice(offset, end));
|
|
210
|
+
offset = end;
|
|
211
|
+
} else if (gap.startsWith("(*", offset)) {
|
|
212
|
+
// SANY's lexer ends a comment token at each (* and *) nested in a block comment.
|
|
213
|
+
let depth = 1;
|
|
214
|
+
let segmentStart = offset;
|
|
215
|
+
let end = offset + 2;
|
|
216
|
+
while (depth > 0 && end < gap.length) {
|
|
217
|
+
const isOpen = gap.startsWith("(*", end);
|
|
218
|
+
const isClose = gap.startsWith("*)", end);
|
|
219
|
+
if (isOpen || isClose) {
|
|
220
|
+
depth += isOpen ? 1 : -1;
|
|
221
|
+
end += 2;
|
|
222
|
+
comments.push(gap.slice(segmentStart, end));
|
|
223
|
+
segmentStart = end;
|
|
224
|
+
} else end++;
|
|
225
|
+
}
|
|
226
|
+
if (segmentStart < end) comments.push(gap.slice(segmentStart, end));
|
|
227
|
+
offset = end;
|
|
228
|
+
} else offset++;
|
|
229
|
+
}
|
|
230
|
+
return comments;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// The identifier tokens with the given names, in order, between two token indexes.
|
|
234
|
+
export const findNameTokens = (file: SourceFile, start: number, end: number, names: string[]) => {
|
|
235
|
+
const found: number[] = [];
|
|
236
|
+
for (let index = start; index < end && found.length < names.length; index++) {
|
|
237
|
+
const token = file.tokens[index];
|
|
238
|
+
if (token.kind === "Identifier" && token.text === names[found.length]) found.push(index);
|
|
239
|
+
}
|
|
240
|
+
return found;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
export const findToken = (file: SourceFile, start: number, end: number, text: string) => {
|
|
244
|
+
for (let index = start; index <= end; index++) {
|
|
245
|
+
if (isToken(file, index, text)) return index;
|
|
246
|
+
}
|
|
247
|
+
return -1;
|
|
248
|
+
};
|