tla-ts 0.0.8 → 0.0.9-dev.0dd11f695
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 +30 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +40 -40
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +98 -19
- package/dist/index.d.ts +98 -19
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +116 -36
- package/dist/prove.js +116 -36
- package/dist/run.cjs +143 -31
- package/dist/run.js +143 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +128 -106
- package/dist/transfer.js +128 -106
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +3 -1
- package/src/ast.ts +7 -21
- package/src/bags.ts +99 -99
- package/src/builtins.ts +44 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check.ts +605 -311
- package/src/cli/options.ts +12 -3
- package/src/cli/run.ts +2 -2
- package/src/cli/runtime-invariants.ts +135 -0
- package/src/cli/shared.ts +20 -9
- package/src/cli/tlc-arguments.ts +646 -0
- package/src/cli/tlc-usage.ts +362 -0
- package/src/cli/tlc.ts +571 -185
- package/src/cli/typescript.ts +26 -0
- package/src/cli/xml-exporter.ts +4 -10
- package/src/codegen.ts +190 -51
- package/src/config.ts +101 -68
- package/src/coverage.ts +855 -0
- package/src/dependencies.ts +340 -24
- package/src/dfid.ts +636 -0
- package/src/environment.ts +289 -109
- package/src/errors.ts +142 -45
- package/src/evaluate.ts +734 -980
- package/src/exit-codes.ts +37 -1
- package/src/index.ts +3 -0
- package/src/instances.ts +85 -9
- package/src/java-overrides.ts +2 -12
- package/src/json-reader.ts +456 -0
- package/src/json.ts +262 -139
- package/src/levels.ts +2 -3
- package/src/lexer.ts +150 -70
- package/src/liveness-checker.ts +10 -18
- package/src/liveness.ts +122 -64
- package/src/parallel-worker.ts +14 -10
- package/src/parallel.ts +41 -17
- package/src/parser.ts +436 -90
- package/src/pcal/ast.ts +7 -28
- package/src/pcal/parse.ts +11 -39
- package/src/pcal/tokenize.ts +4 -20
- package/src/pcal/translate.ts +10 -42
- package/src/pcal/validate.ts +3 -21
- package/src/pretty-print.ts +1 -2
- package/src/prover/smt/solver/solver.ts +31 -27
- package/src/randomization.ts +84 -35
- package/src/resolve.ts +18 -60
- package/src/runtime.ts +11 -4
- package/src/sany/builtins.ts +3 -4
- package/src/sany/generator.ts +128 -176
- package/src/sany/levels.ts +6 -30
- package/src/sany/lexer.ts +7 -23
- package/src/sany/linter.ts +31 -29
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +4 -28
- package/src/sany/parse-error.ts +44 -100
- package/src/sany/report.ts +25 -40
- package/src/sany/selectors.ts +8 -22
- package/src/sany/source.ts +8 -15
- package/src/sany/xml.ts +11 -42
- package/src/search.ts +550 -150
- package/src/simulate.ts +694 -0
- package/src/spec-description.ts +167 -49
- package/src/standard-definitions.ts +2 -3
- package/src/standard-modules.ts +264 -68
- package/src/state-graph.ts +51 -18
- package/src/subexpressions.ts +31 -5
- package/src/successors.ts +269 -85
- package/src/symmetry.ts +59 -147
- package/src/tableau.ts +1 -2
- package/src/tlc/fingerprint.ts +226 -151
- package/src/tlc/jar-data.ts +113 -6
- package/src/tlc/messages.ts +122 -37
- package/src/tlc/order.ts +56 -82
- package/src/tlc/random.ts +16 -16
- package/src/tlc/replay.ts +564 -212
- package/src/tlc/summary.ts +2 -4
- package/src/tlc/tlc-operators.ts +81 -36
- package/src/tlc/tlcext-operators.ts +138 -117
- package/src/tlc/trace-spec.ts +273 -0
- package/src/tlc/value-stream.ts +143 -0
- package/src/tlc/version.ts +2 -4
- package/src/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +148 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +910 -318
package/src/pcal/ast.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { PcalBugError, PcalError, PcalRuntimeError } from "../errors";
|
|
2
2
|
import type { TlcVersion } from "../tlc/version";
|
|
3
3
|
|
|
4
|
-
// TLAToken's types.
|
|
5
4
|
export const PCAL_TOKEN = { builtin: 1, number: 2, string: 3, ident: 4, added: 5 } as const;
|
|
6
5
|
|
|
7
6
|
export interface PcalToken {
|
|
@@ -10,22 +9,21 @@ export interface PcalToken {
|
|
|
10
9
|
type: number;
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
//
|
|
14
|
-
//
|
|
12
|
+
// For each line, the token of an earlier line it is aligned under, with that token's column when
|
|
13
|
+
// the expression was normalized.
|
|
15
14
|
export interface PcalExpr {
|
|
16
15
|
tokens: PcalToken[][];
|
|
17
16
|
anchorTokens: (PcalToken | undefined)[] | undefined;
|
|
18
17
|
anchorTokCol: number[] | undefined;
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
// IntPair, as the "Java coordinates" of a token in an expression.
|
|
22
20
|
interface PcalCoord {
|
|
23
21
|
line: number;
|
|
24
22
|
item: number;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
//
|
|
28
|
-
//
|
|
25
|
+
// The Java keeps these in static fields that SANY never resets, so options one module sets carry
|
|
26
|
+
// over to the modules SANY validates after it.
|
|
29
27
|
export interface PcalParams {
|
|
30
28
|
labelFlag: boolean;
|
|
31
29
|
labelRoot: string;
|
|
@@ -36,7 +34,6 @@ export interface PcalParams {
|
|
|
36
34
|
inputVersionNumber: number;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
// PcalParams.versionWeight.
|
|
40
37
|
export const PCAL_VERSION_WEIGHT = 1903;
|
|
41
38
|
|
|
42
39
|
export const createPcalParams = (): PcalParams => ({
|
|
@@ -49,7 +46,7 @@ export const createPcalParams = (): PcalParams => ({
|
|
|
49
46
|
inputVersionNumber: PCAL_VERSION_WEIGHT,
|
|
50
47
|
});
|
|
51
48
|
|
|
52
|
-
//
|
|
49
|
+
// "1.5" is 1500, and -1 means a malformed version.
|
|
53
50
|
export const getPcalVersionNumber = (text: string) => {
|
|
54
51
|
const state = { result: 0, digitsAfterDecimal: 0, isAfterDecimal: false };
|
|
55
52
|
for (const char of text) {
|
|
@@ -104,7 +101,6 @@ export interface PcalProcedure extends PcalBase {
|
|
|
104
101
|
body: PcalStmt[];
|
|
105
102
|
}
|
|
106
103
|
|
|
107
|
-
// AST.UNFAIR_PROC, WF_PROC and SF_PROC index FairnessString.
|
|
108
104
|
const PCAL_FAIRNESS = ["unfair", "wf", "sf"];
|
|
109
105
|
|
|
110
106
|
export interface PcalProcess extends PcalBase {
|
|
@@ -309,13 +305,11 @@ export const createPcalBase = (): PcalBase => ({
|
|
|
309
305
|
lbl: "",
|
|
310
306
|
});
|
|
311
307
|
|
|
312
|
-
// AST.location.
|
|
313
308
|
export const getPcalLocation = (node: PcalBase) =>
|
|
314
309
|
node.macroLine < 0
|
|
315
310
|
? `line ${node.line}, column ${node.col}`
|
|
316
311
|
: `line ${node.line}, column ${node.col} of macro called at line ${node.macroLine}, column ${node.macroCol}`;
|
|
317
312
|
|
|
318
|
-
// A Java cast of a statement to AST.LabeledStmt.
|
|
319
313
|
export const toLabeledStmt = (node: PcalStmt) => {
|
|
320
314
|
if (node.kind !== "LabeledStmt") throw new PcalRuntimeError(`ClassCastException: ${node.kind}`);
|
|
321
315
|
return node;
|
|
@@ -335,7 +329,7 @@ export const createPcalToken = (string: string, column: number, type: number): P
|
|
|
335
329
|
type,
|
|
336
330
|
});
|
|
337
331
|
|
|
338
|
-
//
|
|
332
|
+
// A string token's width counts its quotes.
|
|
339
333
|
export const getPcalTokenWidth = (token: PcalToken) =>
|
|
340
334
|
token.type === PCAL_TOKEN.string ? token.string.length + 2 : token.string.length;
|
|
341
335
|
|
|
@@ -345,8 +339,6 @@ export const createPcalExpr = (tokens: PcalToken[][] = []): PcalExpr => ({
|
|
|
345
339
|
anchorTokCol: undefined,
|
|
346
340
|
});
|
|
347
341
|
|
|
348
|
-
// TLAExpr.normalize: drops empty first and last lines, shifts the tokens left as far as they go,
|
|
349
|
-
// and records the token each line is aligned under.
|
|
350
342
|
export const normalizePcalExpr = (expr: PcalExpr) => {
|
|
351
343
|
while (expr.tokens.length > 0 && expr.tokens[0].length === 0) expr.tokens.shift();
|
|
352
344
|
while (expr.tokens.length > 0 && expr.tokens[expr.tokens.length - 1].length === 0) {
|
|
@@ -387,7 +379,6 @@ export const createNormalizedPcalExpr = (tokens: PcalToken[][]) => {
|
|
|
387
379
|
return expr;
|
|
388
380
|
};
|
|
389
381
|
|
|
390
|
-
// TLAExpr.renormalize: shifts each line right by as much as its anchor token has moved.
|
|
391
382
|
const renormalizePcalExpr = (expr: PcalExpr) => {
|
|
392
383
|
const anchorTokens = expr.anchorTokens;
|
|
393
384
|
const anchorTokCol = expr.anchorTokCol;
|
|
@@ -409,7 +400,6 @@ const renormalizePcalExpr = (expr: PcalExpr) => {
|
|
|
409
400
|
export const clonePcalExpr = (expr: PcalExpr) =>
|
|
410
401
|
createNormalizedPcalExpr(expr.tokens.map((line) => line.map((token) => ({ ...token }))));
|
|
411
402
|
|
|
412
|
-
// TLAExpr.prepend: puts prefix in front of the expression, spaces columns before its first token.
|
|
413
403
|
export const prependPcalExpr = (expr: PcalExpr, prefix: PcalExpr, spaces: number) => {
|
|
414
404
|
const prefixLines = prefix.tokens.length;
|
|
415
405
|
expr.tokens.splice(0, 0, ...prefix.tokens.slice(0, prefixLines - 1));
|
|
@@ -438,7 +428,6 @@ export const isOneTokenPcalExpr = (expr: PcalExpr) =>
|
|
|
438
428
|
export const getPcalTokenAt = (expr: PcalExpr, coord: PcalCoord) =>
|
|
439
429
|
getElement(getElement(expr.tokens, coord.line), coord.item);
|
|
440
430
|
|
|
441
|
-
// TLAExpr.stepCoord: the coordinates steps tokens after coord, or undefined past the end.
|
|
442
431
|
export const stepPcalCoord = (expr: PcalExpr, coord: PcalCoord, steps: number) => {
|
|
443
432
|
if (expr.tokens.length <= coord.line) {
|
|
444
433
|
throw new PcalBugError("TLAExpr.StepCoord called with line too big");
|
|
@@ -461,8 +450,7 @@ export const stepPcalCoord = (expr: PcalExpr, coord: PcalCoord, steps: number) =
|
|
|
461
450
|
return result;
|
|
462
451
|
};
|
|
463
452
|
|
|
464
|
-
//
|
|
465
|
-
// name, such as the a in r.a, [a |-> 1] or [a : S].
|
|
453
|
+
// Skips a record field name, such as the a in r.a, [a |-> 1] or [a : S].
|
|
466
454
|
const findNextInstance = (expr: PcalExpr, id: string, start: PcalCoord) => {
|
|
467
455
|
for (let coord = expr.tokens.length === 0 ? undefined : { ...start }; coord !== undefined;) {
|
|
468
456
|
const token = getPcalTokenAt(expr, coord);
|
|
@@ -484,9 +472,6 @@ const findNextInstance = (expr: PcalExpr, id: string, start: PcalCoord) => {
|
|
|
484
472
|
return undefined;
|
|
485
473
|
};
|
|
486
474
|
|
|
487
|
-
// TLAExpr.substituteAt: replaces the token at coord with a copy of replacement, parenthesized when
|
|
488
|
-
// isParenthesized and replacement has more than one token, and returns the coordinates of the
|
|
489
|
-
// token after it.
|
|
490
475
|
const substituteAt = (
|
|
491
476
|
expr: PcalExpr,
|
|
492
477
|
replacement: PcalExpr,
|
|
@@ -507,7 +492,6 @@ const substituteAt = (
|
|
|
507
492
|
? tokenLine[coord.item + 1].column - (token.column + getPcalTokenWidth(token))
|
|
508
493
|
: 0;
|
|
509
494
|
const restOfLine = tokenLine.splice(coord.item + 1);
|
|
510
|
-
// The line the replacement's tokens go on, and how far right they move.
|
|
511
495
|
const cursor = {
|
|
512
496
|
lineIndex: coord.line,
|
|
513
497
|
indent: (isParenthesized ? 1 : 0) + token.column,
|
|
@@ -578,8 +562,6 @@ const substituteAt = (
|
|
|
578
562
|
return stepPcalCoord(expr, result, 1);
|
|
579
563
|
};
|
|
580
564
|
|
|
581
|
-
// TLAExpr.substituteForAll: replaces each instance of names[i] with exprs[i], in the order the
|
|
582
|
-
// instances appear.
|
|
583
565
|
export const substitutePcalExpr = (
|
|
584
566
|
expr: PcalExpr,
|
|
585
567
|
exprs: PcalExpr[],
|
|
@@ -625,7 +607,6 @@ export const getSubstitutedPcalExpr = (expr: PcalExpr, args: PcalExpr[], params:
|
|
|
625
607
|
return result;
|
|
626
608
|
};
|
|
627
609
|
|
|
628
|
-
// TLAExpr.SeqSubstituteForAll.
|
|
629
610
|
export const substitutePcalExprs = (exprs: PcalExpr[], args: PcalExpr[], params: string[]) =>
|
|
630
611
|
exprs.map((expr) => getSubstitutedPcalExpr(expr, args, params));
|
|
631
612
|
|
|
@@ -651,8 +632,6 @@ const formatPcalExpr = (expr: PcalExpr, version?: TlcVersion) => {
|
|
|
651
632
|
return `<< ${text} >>`;
|
|
652
633
|
};
|
|
653
634
|
|
|
654
|
-
// AST.toString: the TLA+ record that PlusCal.tla uses for the node. Indent, EndIndent and NewLine
|
|
655
|
-
// keep a stack of indentation columns as the Java concatenates the record's text left to right.
|
|
656
635
|
export const formatPcalNode = (
|
|
657
636
|
node: PcalNode,
|
|
658
637
|
inputVersionNumber: number,
|
package/src/pcal/parse.ts
CHANGED
|
@@ -44,16 +44,14 @@ import {
|
|
|
44
44
|
type PcalTokenizerState,
|
|
45
45
|
} from "./tokenize";
|
|
46
46
|
|
|
47
|
-
// The
|
|
48
|
-
//
|
|
49
|
-
// lookahead buffer and last token, and whether a macro's parsing was cut short by an error.
|
|
47
|
+
// The Java keeps this state in static fields, which SANY never resets between the modules it
|
|
48
|
+
// validates.
|
|
50
49
|
export interface PcalSession {
|
|
51
50
|
params: PcalParams;
|
|
52
51
|
tokenizer: PcalTokenizerState;
|
|
53
52
|
lookahead: { tokens: string[]; columns: number[]; lines: number[] };
|
|
54
53
|
lastToken: { column: number; line: number; text: string };
|
|
55
54
|
isInMacro: boolean;
|
|
56
|
-
// ToolIO.out.println, which PcalDebug's reports go to.
|
|
57
55
|
log: (line: string) => void;
|
|
58
56
|
}
|
|
59
57
|
|
|
@@ -73,7 +71,6 @@ export const createPcalSession = (
|
|
|
73
71
|
log,
|
|
74
72
|
});
|
|
75
73
|
|
|
76
|
-
// ParseAlgorithm's static fields that Init resets for each algorithm.
|
|
77
74
|
interface PcalParserState {
|
|
78
75
|
lookaheadSize: number;
|
|
79
76
|
addedLabels: string[];
|
|
@@ -99,27 +96,22 @@ interface PcalParseResult {
|
|
|
99
96
|
hasDefaultInitialization: boolean;
|
|
100
97
|
}
|
|
101
98
|
|
|
102
|
-
// PcalParams.DefaultVarInit: the value of a variable declared without one.
|
|
103
99
|
const createDefaultVarInit = () =>
|
|
104
100
|
createNormalizedPcalExpr([[createPcalToken("defaultInitValue", 0, 0)]]);
|
|
105
101
|
|
|
106
|
-
// ParseAlgorithm.Union.
|
|
107
102
|
const getUnion = (first: string[], second: string[]) => [
|
|
108
103
|
...first,
|
|
109
104
|
...second.filter((name, index) => !first.includes(name) && second.indexOf(name) === index),
|
|
110
105
|
];
|
|
111
106
|
|
|
112
|
-
// ParseAlgorithm.HasEmptyIntersection.
|
|
113
107
|
const isDisjoint = (first: string[], second: string[]) =>
|
|
114
108
|
!second.some((name) => first.includes(name));
|
|
115
109
|
|
|
116
|
-
//
|
|
110
|
+
// Replaces the contents of target in place, since callers share it.
|
|
117
111
|
const copyInto = (source: string[], target: string[]) => {
|
|
118
112
|
target.splice(0, target.length, ...source);
|
|
119
113
|
};
|
|
120
114
|
|
|
121
|
-
// ParseAlgorithm.getAlgorithm: parses the algorithm the reader is at, just after "--algorithm" or
|
|
122
|
-
// "--fair", adds the labels it needs, and expands its macros.
|
|
123
115
|
export const parsePcalAlgorithm = (
|
|
124
116
|
session: PcalSession,
|
|
125
117
|
reader: PcalCharReader,
|
|
@@ -128,7 +120,6 @@ export const parsePcalAlgorithm = (
|
|
|
128
120
|
const params = session.params;
|
|
129
121
|
const lookahead = session.lookahead;
|
|
130
122
|
const lastToken = session.lastToken;
|
|
131
|
-
// ParseAlgorithm.Init.
|
|
132
123
|
const state: PcalParserState = {
|
|
133
124
|
lookaheadSize: 0,
|
|
134
125
|
addedLabels: [],
|
|
@@ -148,7 +139,6 @@ export const parsePcalAlgorithm = (
|
|
|
148
139
|
omitPC: params.inputVersionNumber >= 1500,
|
|
149
140
|
};
|
|
150
141
|
|
|
151
|
-
// ParseAlgorithm.ParsingError.
|
|
152
142
|
const throwParsingError = (message: string): never => {
|
|
153
143
|
throw new PcalError(`${message}\n line ${lastToken.line}, column ${lastToken.column}`);
|
|
154
144
|
};
|
|
@@ -190,7 +180,6 @@ export const parsePcalAlgorithm = (
|
|
|
190
180
|
return lookahead.tokens[tokenNumber - 1];
|
|
191
181
|
};
|
|
192
182
|
|
|
193
|
-
// ParseAlgorithm.PeekPastAlgToken: the rest of the line after the next tokenNumber tokens.
|
|
194
183
|
const peekPastAlgToken = (tokenNumber: number) => {
|
|
195
184
|
if (tokenNumber < state.lookaheadSize) {
|
|
196
185
|
throw new PcalBugError(
|
|
@@ -204,7 +193,7 @@ export const parsePcalAlgorithm = (
|
|
|
204
193
|
return peekPcalReader(reader);
|
|
205
194
|
};
|
|
206
195
|
|
|
207
|
-
//
|
|
196
|
+
// An expression's delimiter becomes the next token.
|
|
208
197
|
const getExpr = (isDefineBlock = false) => {
|
|
209
198
|
if (state.lookaheadSize !== 0) {
|
|
210
199
|
throw new PcalBugError("ParseAlgorithm: GetExpr called after lookahead");
|
|
@@ -217,7 +206,7 @@ export const parsePcalAlgorithm = (
|
|
|
217
206
|
return result;
|
|
218
207
|
};
|
|
219
208
|
|
|
220
|
-
//
|
|
209
|
+
// A missing ";" is fine before a token that can't start a statement.
|
|
221
210
|
const gobbleThis = (expected: string, alternate?: string) => {
|
|
222
211
|
if (expected === ";") {
|
|
223
212
|
const next = peekAtAlgToken(1);
|
|
@@ -328,7 +317,6 @@ export const parsePcalAlgorithm = (
|
|
|
328
317
|
return result;
|
|
329
318
|
};
|
|
330
319
|
|
|
331
|
-
// ParseAlgorithm.IsLabelNext: an identifier followed by ":" but not ":=".
|
|
332
320
|
const isLabelNext = () => {
|
|
333
321
|
const keywords =
|
|
334
322
|
"while if assert print else elsif either or end with when await skip call return goto";
|
|
@@ -546,7 +534,6 @@ export const parsePcalAlgorithm = (
|
|
|
546
534
|
return result;
|
|
547
535
|
};
|
|
548
536
|
|
|
549
|
-
// ParseAlgorithm.InnerGetWith: "with x = e, y \in f" nests a with for each variable.
|
|
550
537
|
const getWith = (depth: number): PcalWith => {
|
|
551
538
|
if (depth === 0) {
|
|
552
539
|
gobbleThis("with");
|
|
@@ -620,7 +607,6 @@ export const parsePcalAlgorithm = (
|
|
|
620
607
|
return result;
|
|
621
608
|
};
|
|
622
609
|
|
|
623
|
-
// The arguments of a call or macro call, through its ")" and ";".
|
|
624
610
|
const getCallArgs = () => {
|
|
625
611
|
const args: PcalExpr[] = [];
|
|
626
612
|
if (peekPastAlgToken(0)[0] !== ")") {
|
|
@@ -801,7 +787,6 @@ export const parsePcalAlgorithm = (
|
|
|
801
787
|
};
|
|
802
788
|
};
|
|
803
789
|
|
|
804
|
-
// ParseAlgorithm.NeedsLabel: the label root and the next number no label of the algorithm has.
|
|
805
790
|
const getUnusedLabel = (): string => {
|
|
806
791
|
const label = `${params.labelRoot}${state.nextLabelNum}`;
|
|
807
792
|
state.nextLabelNum += 1;
|
|
@@ -815,9 +800,7 @@ export const parsePcalAlgorithm = (
|
|
|
815
800
|
state.addedLabelsLocs.push(getPcalLocation(stmt));
|
|
816
801
|
};
|
|
817
802
|
|
|
818
|
-
//
|
|
819
|
-
// variables assigned since the last label in outAssigned. True when the statement after the
|
|
820
|
-
// sequence needs a label.
|
|
803
|
+
// True when the statement after the sequence needs a label.
|
|
821
804
|
const addLabels = (
|
|
822
805
|
stmts: PcalStmt[],
|
|
823
806
|
isFirstLabeled: boolean,
|
|
@@ -936,8 +919,6 @@ export const parsePcalAlgorithm = (
|
|
|
936
919
|
return progress.hasLabel || progress.isNextLabeled;
|
|
937
920
|
};
|
|
938
921
|
|
|
939
|
-
// ParseAlgorithm.FixStmtSeq: splits each compound statement's sequences into the statements
|
|
940
|
-
// before their first label and the labeled statements from it on.
|
|
941
922
|
const fixStmtSeq = (stmts: PcalStmt[]) => {
|
|
942
923
|
const split = (sequence: PcalStmt[]) => {
|
|
943
924
|
const index = sequence.findIndex((stmt) => stmt.lbl !== "");
|
|
@@ -972,7 +953,6 @@ export const parsePcalAlgorithm = (
|
|
|
972
953
|
});
|
|
973
954
|
};
|
|
974
955
|
|
|
975
|
-
// ParseAlgorithm.InnerMakeLabeledStmtSeq: groups the statements into labeled statements.
|
|
976
956
|
const makeLabeledStmts = (stmts: PcalStmt[]) => {
|
|
977
957
|
const result: PcalLabeledStmt[] = [];
|
|
978
958
|
stmts.forEach((stmt) => {
|
|
@@ -998,9 +978,8 @@ export const parsePcalAlgorithm = (
|
|
|
998
978
|
return result;
|
|
999
979
|
};
|
|
1000
980
|
|
|
1001
|
-
//
|
|
1002
|
-
//
|
|
1003
|
-
// statement inside. An if or either without labels inside becomes an If or Either.
|
|
981
|
+
// 0 for a statement that may have another after it in the step, 1 for a call, return or goto,
|
|
982
|
+
// which ends a step, and 2 for one with a labeled statement inside.
|
|
1004
983
|
const classifyStmt = (stmts: PcalStmt[], index: number): number => {
|
|
1005
984
|
const node = stmts[index];
|
|
1006
985
|
switch (node.kind) {
|
|
@@ -1077,7 +1056,6 @@ export const parsePcalAlgorithm = (
|
|
|
1077
1056
|
}
|
|
1078
1057
|
};
|
|
1079
1058
|
|
|
1080
|
-
// ParseAlgorithm.ClassifyStmtSeq: a statement that ends a step can only end its sequence.
|
|
1081
1059
|
const classifyStmtSeq = (stmts: PcalStmt[]): number => {
|
|
1082
1060
|
for (let index = 0; index < stmts.length; index++) {
|
|
1083
1061
|
const stmtClass = classifyStmt(stmts, index);
|
|
@@ -1090,7 +1068,6 @@ export const parsePcalAlgorithm = (
|
|
|
1090
1068
|
return 0;
|
|
1091
1069
|
};
|
|
1092
1070
|
|
|
1093
|
-
// ParseAlgorithm.AddLabelsToStmtSeq followed by MakeLabeledStmtSeq.
|
|
1094
1071
|
const makeLabeledStmtSeq = (stmts: PcalStmt[]) => {
|
|
1095
1072
|
addLabels(stmts, true, false, [], []);
|
|
1096
1073
|
const result = makeLabeledStmts(stmts);
|
|
@@ -1098,8 +1075,6 @@ export const parsePcalAlgorithm = (
|
|
|
1098
1075
|
return result;
|
|
1099
1076
|
};
|
|
1100
1077
|
|
|
1101
|
-
// ParseAlgorithm.SubstituteInStmt: a copy of a macro's statement with the macro's arguments
|
|
1102
|
-
// for its parameters, which records the position of the macro call.
|
|
1103
1078
|
const substituteInStmt = (
|
|
1104
1079
|
stmt: PcalStmt,
|
|
1105
1080
|
args: PcalExpr[],
|
|
@@ -1228,8 +1203,8 @@ export const parsePcalAlgorithm = (
|
|
|
1228
1203
|
}
|
|
1229
1204
|
};
|
|
1230
1205
|
|
|
1231
|
-
//
|
|
1232
|
-
// argument
|
|
1206
|
+
// A parameter assigned to becomes the variable its argument starts with, and the rest of the
|
|
1207
|
+
// argument goes before the subscript.
|
|
1233
1208
|
const substituteInSingleAssign = (
|
|
1234
1209
|
assign: PcalSingleAssign,
|
|
1235
1210
|
args: PcalExpr[],
|
|
@@ -1276,7 +1251,6 @@ export const parsePcalAlgorithm = (
|
|
|
1276
1251
|
return result;
|
|
1277
1252
|
};
|
|
1278
1253
|
|
|
1279
|
-
// ParseAlgorithm.ExpandMacrosInStmtSeq: replaces each macro call with the macro's body.
|
|
1280
1254
|
const expandMacrosInStmtSeq = (stmts: PcalStmt[], macros: PcalMacro[]) => {
|
|
1281
1255
|
const expanded = stmts.flatMap((node) => {
|
|
1282
1256
|
switch (node.kind) {
|
|
@@ -1328,8 +1302,7 @@ export const parsePcalAlgorithm = (
|
|
|
1328
1302
|
});
|
|
1329
1303
|
};
|
|
1330
1304
|
|
|
1331
|
-
//
|
|
1332
|
-
// lets the translation omit pc.
|
|
1305
|
+
// A body that is a single labeled "while TRUE" without labels inside lets the translation omit pc.
|
|
1333
1306
|
const checkBody = (body: PcalStmt[]) => {
|
|
1334
1307
|
if (body.length === 0) return;
|
|
1335
1308
|
const keepPC = () => {
|
|
@@ -1350,7 +1323,6 @@ export const parsePcalAlgorithm = (
|
|
|
1350
1323
|
if (hasLabeled) state.omitPC = false;
|
|
1351
1324
|
};
|
|
1352
1325
|
|
|
1353
|
-
// ParseAlgorithm.AddedMessagesError.
|
|
1354
1326
|
const throwAddedLabels = (): never => {
|
|
1355
1327
|
const header =
|
|
1356
1328
|
state.addedLabels.length > 1
|
package/src/pcal/tokenize.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { PcalError, PcalBugError } from "../errors";
|
|
2
2
|
import { createNormalizedPcalExpr, createPcalToken, PCAL_TOKEN, type PcalToken } from "./ast";
|
|
3
3
|
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// points. The lines come without tabs, as trans.removeTabs replaced them.
|
|
4
|
+
// Returns "\n" at the end of each line and "\t" at the end of the input. Columns count code
|
|
5
|
+
// points, and the lines come without tabs, as trans.removeTabs replaced them.
|
|
7
6
|
export interface PcalCharReader {
|
|
8
7
|
lines: string[];
|
|
9
8
|
currentLine: string[] | undefined;
|
|
@@ -55,8 +54,6 @@ const backspacePcalReader = (reader: PcalCharReader) => {
|
|
|
55
54
|
reader.vcolumn = reader.column;
|
|
56
55
|
};
|
|
57
56
|
|
|
58
|
-
// PcalCharReader.peek: the rest of the line from the next character that is not white space, with
|
|
59
|
-
// its "\n", or "\t" at the end of the input.
|
|
60
57
|
export const peekPcalReader = (reader: PcalCharReader) => {
|
|
61
58
|
for (;;) {
|
|
62
59
|
const next = getNextPcalChar(reader);
|
|
@@ -67,11 +64,9 @@ export const peekPcalReader = (reader: PcalCharReader) => {
|
|
|
67
64
|
return `${(reader.currentLine ?? []).slice(reader.column).join("")}\n`;
|
|
68
65
|
};
|
|
69
66
|
|
|
70
|
-
// Symbol.LEFT_PAREN and RIGHT_PAREN of PcalBuiltInSymbols' table.
|
|
71
67
|
const LEFT_PARENS = new Set(["(", "[", "{", "<<", "⟨"]);
|
|
72
68
|
const RIGHT_PARENS = new Set([">>_", "⟩_", "]_", ")", "}", "]", ">>", "⟩"]);
|
|
73
69
|
|
|
74
|
-
// PcalBuiltInSymbols.buildHashTable.
|
|
75
70
|
const BUILT_IN_SYMBOLS = new Set([
|
|
76
71
|
...LEFT_PARENS,
|
|
77
72
|
...RIGHT_PARENS,
|
|
@@ -100,30 +95,25 @@ const isLetter = (char: string) => /^[A-Za-z_]$/.test(char);
|
|
|
100
95
|
const isDigit = (char: string) => /^[0-9]$/.test(char);
|
|
101
96
|
const isSpace = (char: string) => char === " " || char === "\f" || char === "\r";
|
|
102
97
|
|
|
103
|
-
// PcalBuiltInSymbols.buildPrefixHashTable: the prefixes of the symbols that are not words.
|
|
104
98
|
const BUILT_IN_PREFIXES = new Set(
|
|
105
99
|
[...BUILT_IN_SYMBOLS]
|
|
106
100
|
.filter((symbol) => !isLetter(symbol[0]) && !(symbol[0] === "\\" && isLetter(symbol[1] ?? "")))
|
|
107
101
|
.flatMap((symbol) => Array.from(symbol, (_, index) => symbol.slice(0, symbol.length - index))),
|
|
108
102
|
);
|
|
109
103
|
|
|
110
|
-
// PcalBuiltInSymbols.buildStringCharTable.
|
|
111
104
|
const STRING_CHARS = new Set(
|
|
112
105
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ~!@#$%^&*()_-+={}[]|:;<,>.?/`'0123456789",
|
|
113
106
|
);
|
|
114
107
|
|
|
115
|
-
// Tokenize.IsDelimiter: the words and symbols that end an expression of the algorithm.
|
|
116
108
|
const DELIMITERS = new Set(
|
|
117
109
|
"; do then := ≔ begin variable variables || ‖ end else elsif if either or while with call return goto print assert skip procedure define process fair".split(
|
|
118
110
|
" ",
|
|
119
111
|
),
|
|
120
112
|
);
|
|
121
113
|
|
|
122
|
-
// tla2tex.Token's types, of which an expression allows the ones TLAToken shares.
|
|
123
114
|
const TOKEN_TYPE = { ...PCAL_TOKEN, dashes: 6, endModule: 7 };
|
|
124
115
|
|
|
125
|
-
//
|
|
126
|
-
// which an error in a comment leaves set for the next call.
|
|
116
|
+
// An error in a comment leaves the comment depth set for the next call.
|
|
127
117
|
export interface PcalTokenizerState {
|
|
128
118
|
delimiter: string;
|
|
129
119
|
delimiterLine: number;
|
|
@@ -152,9 +142,7 @@ interface PcalScan {
|
|
|
152
142
|
previousToken: string;
|
|
153
143
|
}
|
|
154
144
|
|
|
155
|
-
//
|
|
156
|
-
// isExpr, and otherwise a single token of the algorithm, which becomes state.delimiter. A define
|
|
157
|
-
// block's commas at the top level don't end it.
|
|
145
|
+
// A define block's commas at the top level don't end its expression.
|
|
158
146
|
const tokenize = (
|
|
159
147
|
state: PcalTokenizerState,
|
|
160
148
|
reader: PcalCharReader,
|
|
@@ -202,7 +190,6 @@ const tokenize = (
|
|
|
202
190
|
state.delimiterCol = scan.column;
|
|
203
191
|
state.delimiterLine = reader.line - (scan.nextChar === "\n" ? 1 : 0);
|
|
204
192
|
};
|
|
205
|
-
// Tokenize.TokenOut.
|
|
206
193
|
const tokenOut = (type: number) => {
|
|
207
194
|
if (!isExpr) {
|
|
208
195
|
endExpression();
|
|
@@ -259,7 +246,6 @@ const tokenize = (
|
|
|
259
246
|
skipNextChar();
|
|
260
247
|
scan.state = nextState;
|
|
261
248
|
};
|
|
262
|
-
// One transition of the state machine on the next character.
|
|
263
249
|
const step = (next: string) => {
|
|
264
250
|
switch (scan.state) {
|
|
265
251
|
case "start":
|
|
@@ -473,14 +459,12 @@ const tokenize = (
|
|
|
473
459
|
return createNormalizedPcalExpr(scan.lines);
|
|
474
460
|
};
|
|
475
461
|
|
|
476
|
-
// Tokenize.TokenizeExpr and TokenizeDefineBlock.
|
|
477
462
|
export const tokenizePcalExpr = (
|
|
478
463
|
state: PcalTokenizerState,
|
|
479
464
|
reader: PcalCharReader,
|
|
480
465
|
isInDefineBlock: boolean,
|
|
481
466
|
) => tokenize(state, reader, true, isInDefineBlock);
|
|
482
467
|
|
|
483
|
-
// Tokenize.GetAlgorithmToken.
|
|
484
468
|
export const getPcalAlgorithmToken = (state: PcalTokenizerState, reader: PcalCharReader) => {
|
|
485
469
|
tokenize(state, reader, false, false);
|
|
486
470
|
return state.delimiter;
|