tla-ts 0.0.3-dev.bfd303f → 0.0.3-dev.c2b256f
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/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +169 -27
- package/dist/index.d.ts +169 -27
- 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 -20
- package/dist/run-cli.js +1351 -20
- package/dist/transfer.cjs +83 -28
- package/dist/transfer.js +83 -28
- package/package.json +5 -1
- package/src/ast.ts +57 -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 +1332 -262
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +252 -179
- package/src/environment.ts +437 -123
- package/src/errors.ts +195 -7
- package/src/evaluate.ts +718 -146
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +112 -29
- package/src/live-check.ts +204 -146
- package/src/liveness.ts +190 -117
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +445 -147
- 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 +1439 -411
- package/src/run-cli.ts +1167 -155
- package/src/runtime.ts +16 -0
- 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 +26 -7
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +158 -120
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +683 -256
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import { PcalError, PcalBugError } from "./errors";
|
|
2
|
+
import { createNormalizedPcalExpr, createPcalToken, PCAL_TOKEN, type PcalToken } from "./pcal-ast";
|
|
3
|
+
|
|
4
|
+
// PcalCharReader: reads the algorithm's characters from the lines between a start and an end
|
|
5
|
+
// position, returning "\n" at the end of each line and "\t" at the end of the input. Columns count
|
|
6
|
+
// code points, and getColumnNumber counts tabs as reaching the next multiple of 8.
|
|
7
|
+
export interface PcalCharReader {
|
|
8
|
+
lines: string[];
|
|
9
|
+
currentLine: string[] | undefined;
|
|
10
|
+
line: number;
|
|
11
|
+
column: number;
|
|
12
|
+
vcolumn: number;
|
|
13
|
+
lastLine: number;
|
|
14
|
+
lastColumn: number;
|
|
15
|
+
isTabToSpaces: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const createPcalCharReader = (
|
|
19
|
+
lines: string[],
|
|
20
|
+
firstLine: number,
|
|
21
|
+
firstColumn: number,
|
|
22
|
+
lastLine: number,
|
|
23
|
+
lastColumn: number,
|
|
24
|
+
): PcalCharReader => ({
|
|
25
|
+
lines,
|
|
26
|
+
currentLine: firstLine < lines.length ? Array.from(lines[firstLine]) : undefined,
|
|
27
|
+
line: firstLine,
|
|
28
|
+
column: firstColumn,
|
|
29
|
+
vcolumn:
|
|
30
|
+
firstLine < lines.length
|
|
31
|
+
? Array.from(lines[firstLine].slice(0, firstColumn)).reduce(
|
|
32
|
+
(vcolumn, char) => (char === "\t" ? (Math.floor(vcolumn / 8) + 1) * 8 : vcolumn + 1),
|
|
33
|
+
0,
|
|
34
|
+
)
|
|
35
|
+
: 0,
|
|
36
|
+
lastLine,
|
|
37
|
+
lastColumn,
|
|
38
|
+
isTabToSpaces: false,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const getNextPcalChar = (reader: PcalCharReader) => {
|
|
42
|
+
if (reader.currentLine === undefined) return "\t";
|
|
43
|
+
if (reader.isTabToSpaces) {
|
|
44
|
+
reader.vcolumn += 1;
|
|
45
|
+
if (reader.vcolumn % 8 === 0) reader.isTabToSpaces = false;
|
|
46
|
+
return " ";
|
|
47
|
+
}
|
|
48
|
+
if (
|
|
49
|
+
reader.line > reader.lastLine ||
|
|
50
|
+
(reader.line === reader.lastLine && reader.column >= reader.lastColumn)
|
|
51
|
+
) {
|
|
52
|
+
return "\t";
|
|
53
|
+
}
|
|
54
|
+
if (reader.currentLine.length === reader.column) {
|
|
55
|
+
reader.line += 1;
|
|
56
|
+
reader.column = 0;
|
|
57
|
+
reader.vcolumn = 0;
|
|
58
|
+
reader.currentLine =
|
|
59
|
+
reader.line >= reader.lines.length ? undefined : Array.from(reader.lines[reader.line]);
|
|
60
|
+
return "\n";
|
|
61
|
+
}
|
|
62
|
+
const char = reader.currentLine[reader.column];
|
|
63
|
+
reader.column += 1;
|
|
64
|
+
reader.vcolumn += 1;
|
|
65
|
+
if (char !== "\t") return char;
|
|
66
|
+
if (reader.vcolumn % 8 !== 0) reader.isTabToSpaces = true;
|
|
67
|
+
return " ";
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const backspacePcalReader = (reader: PcalCharReader) => {
|
|
71
|
+
if (reader.column > 0) {
|
|
72
|
+
reader.column -= 1;
|
|
73
|
+
reader.vcolumn -= 1;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (reader.line === 0) {
|
|
77
|
+
throw new PcalBugError("PcalCharReader.backspace trying to move past beginning of reader");
|
|
78
|
+
}
|
|
79
|
+
reader.line -= 1;
|
|
80
|
+
const currentLine = Array.from(reader.lines[reader.line]);
|
|
81
|
+
reader.currentLine = currentLine;
|
|
82
|
+
reader.column = 0;
|
|
83
|
+
reader.vcolumn = 0;
|
|
84
|
+
while (reader.column < currentLine.length - 1) getNextPcalChar(reader);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// PcalCharReader.peek: the rest of the line from the next character that is not white space, with
|
|
88
|
+
// its "\n", or "\t" at the end of the input.
|
|
89
|
+
export const peekPcalReader = (reader: PcalCharReader) => {
|
|
90
|
+
for (;;) {
|
|
91
|
+
const next = getNextPcalChar(reader);
|
|
92
|
+
if (next === "\t") return "\t";
|
|
93
|
+
if (next !== " " && next !== "\n") break;
|
|
94
|
+
}
|
|
95
|
+
backspacePcalReader(reader);
|
|
96
|
+
return `${(reader.currentLine ?? []).slice(reader.column).join("")}\n`;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Symbol.LEFT_PAREN and RIGHT_PAREN of PcalBuiltInSymbols' table.
|
|
100
|
+
const LEFT_PARENS = new Set(["(", "[", "{", "<<", "⟨"]);
|
|
101
|
+
const RIGHT_PARENS = new Set([">>_", "⟩_", "]_", ")", "}", "]", ">>", "⟩"]);
|
|
102
|
+
|
|
103
|
+
// PcalBuiltInSymbols.buildHashTable.
|
|
104
|
+
const BUILT_IN_SYMBOLS = new Set([
|
|
105
|
+
...LEFT_PARENS,
|
|
106
|
+
...RIGHT_PARENS,
|
|
107
|
+
..."_ ASSUME ASSUMPTION AXIOM BOOLEAN CASE CONSTANT CONSTANTS EXCEPT EXTENDS FALSE IF INSTANCE LOCAL MODULE OTHER STRING THEOREM TRUE VARIABLE VARIABLES WITH WF_ SF_".split(
|
|
108
|
+
" ",
|
|
109
|
+
),
|
|
110
|
+
..."\\A ∀ \\forall \\E ∃ \\exists \\AA \\EE ~ ¬ \\lnot \\neg <> ◇ CHOOSE ENABLED UNCHANGED SUBSET UNION DOMAIN ' ^+ ⁺ ^* ^#".split(
|
|
111
|
+
" ",
|
|
112
|
+
),
|
|
113
|
+
..."=> ⇒ \\cdot ⋅ <=> ⇔ \\equiv ≡ ~> ↝ -+-> ⇸ \\subseteq ⊆ \\subset ⊂ \\supset ⊃ \\supseteq ⊇ \\ll ≪ \\gg ≫ \\ \\cap \\intersect ∩ \\cup \\union ∪ /\\ ∧ \\/ ∨ \\land \\lor \\X × - + * / ^ | || ‖ & && ++ -- ** // ^^ |- ⊢ |= ⊨ -| ⊣ =| ⫤ <: :> := ≔ ::= ⩴".split(
|
|
114
|
+
" ",
|
|
115
|
+
),
|
|
116
|
+
..."(+) ⊕ (-) ⊖ \\oplus \\ominus (.) ⊙ \\odot (/) ⊘ \\oslash (\\X) ⊗ \\otimes \\uplus ⊎ \\sqcap ⊓ \\sqcup ⊔ \\div ÷ \\star ⋆ \\o ∘ \\circ \\bigcirc ◯ \\bullet ●".split(
|
|
117
|
+
" ",
|
|
118
|
+
),
|
|
119
|
+
..."\\in ∈ \\notin ∉ = # /= ≠ < > =< <= ≤ >= ≥ \\prec ≺ \\succ ≻ \\preceq ⪯ \\succeq ⪰ \\sim ∼ \\simeq ≃ \\approx ≈ \\doteq ≐ \\asymp ≍ \\sqsubset ⊏ \\sqsupset ⊐ \\sqsubseteq ⊑ \\sqsupseteq ⊒ \\propto ∝".split(
|
|
120
|
+
" ",
|
|
121
|
+
),
|
|
122
|
+
...": -> → |-> ↦ <- ← == ≜ ELSE THEN LET IN [] □ .. ‥ ... … $ $$ ? ?? ⁇ % %% ## @@ !! ‼ \\times \\leq \\geq \\mod \\wr ≀ ≅ ! ; , . -. @".split(
|
|
123
|
+
" ",
|
|
124
|
+
),
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
// tla2tex.Misc.IsLetter, IsDigit and IsSpace, which only accept ASCII.
|
|
128
|
+
const isLetter = (char: string) => /^[A-Za-z_]$/.test(char);
|
|
129
|
+
const isDigit = (char: string) => /^[0-9]$/.test(char);
|
|
130
|
+
const isSpace = (char: string) => char === " " || char === "\f" || char === "\r";
|
|
131
|
+
|
|
132
|
+
// PcalBuiltInSymbols.buildPrefixHashTable: the prefixes of the symbols that are not words.
|
|
133
|
+
const BUILT_IN_PREFIXES = new Set(
|
|
134
|
+
[...BUILT_IN_SYMBOLS]
|
|
135
|
+
.filter((symbol) => !isLetter(symbol[0]) && !(symbol[0] === "\\" && isLetter(symbol[1] ?? "")))
|
|
136
|
+
.flatMap((symbol) => Array.from(symbol, (_, index) => symbol.slice(0, symbol.length - index))),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
// PcalBuiltInSymbols.buildStringCharTable.
|
|
140
|
+
const STRING_CHARS = new Set(
|
|
141
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ~!@#$%^&*()_-+={}[]|:;<,>.?/`'0123456789",
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// Tokenize.IsDelimiter: the words and symbols that end an expression of the algorithm.
|
|
145
|
+
const DELIMITERS = new Set(
|
|
146
|
+
"; do then := ≔ begin variable variables || ‖ end else elsif if either or while with call return goto print assert skip procedure define process fair".split(
|
|
147
|
+
" ",
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// tla2tex.Token's types, of which an expression allows the ones TLAToken shares.
|
|
152
|
+
const TOKEN_TYPE = { ...PCAL_TOKEN, dashes: 6, endModule: 7 };
|
|
153
|
+
|
|
154
|
+
// Tokenize's static Delimiter fields, the delimiter its last call ended at, and its comment depth,
|
|
155
|
+
// which an error in a comment leaves set for the next call.
|
|
156
|
+
export interface PcalTokenizerState {
|
|
157
|
+
delimiter: string;
|
|
158
|
+
delimiterLine: number;
|
|
159
|
+
delimiterCol: number;
|
|
160
|
+
commentDepth: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const createPcalTokenizerState = (): PcalTokenizerState => ({
|
|
164
|
+
delimiter: "",
|
|
165
|
+
delimiterLine: 0,
|
|
166
|
+
delimiterCol: 0,
|
|
167
|
+
commentDepth: 0,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
interface PcalScan {
|
|
171
|
+
lines: PcalToken[][];
|
|
172
|
+
line: PcalToken[];
|
|
173
|
+
token: string;
|
|
174
|
+
state: string;
|
|
175
|
+
column: number;
|
|
176
|
+
nextColumn: number;
|
|
177
|
+
nextChar: string;
|
|
178
|
+
parenDepth: number;
|
|
179
|
+
isInQuantifier: boolean;
|
|
180
|
+
isExprEnd: boolean;
|
|
181
|
+
previousToken: string;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Tokenize.InnerTokenize in TLA mode: reads an expression up to the delimiter that ends it when
|
|
185
|
+
// isExpr, and otherwise a single token of the algorithm, which becomes state.delimiter. A define
|
|
186
|
+
// block's commas at the top level don't end it.
|
|
187
|
+
const tokenize = (
|
|
188
|
+
state: PcalTokenizerState,
|
|
189
|
+
reader: PcalCharReader,
|
|
190
|
+
isExpr: boolean,
|
|
191
|
+
isInDefineBlock: boolean,
|
|
192
|
+
) => {
|
|
193
|
+
const scan: PcalScan = {
|
|
194
|
+
lines: [],
|
|
195
|
+
line: [],
|
|
196
|
+
token: "",
|
|
197
|
+
state: "start",
|
|
198
|
+
column: reader.vcolumn,
|
|
199
|
+
nextColumn: reader.vcolumn,
|
|
200
|
+
nextChar: getNextPcalChar(reader),
|
|
201
|
+
parenDepth: 0,
|
|
202
|
+
isInQuantifier: false,
|
|
203
|
+
isExprEnd: false,
|
|
204
|
+
previousToken: " ",
|
|
205
|
+
};
|
|
206
|
+
const skipNextChar = () => {
|
|
207
|
+
scan.nextColumn = reader.vcolumn;
|
|
208
|
+
scan.nextChar = getNextPcalChar(reader);
|
|
209
|
+
};
|
|
210
|
+
const addNextChar = () => {
|
|
211
|
+
scan.token += scan.nextChar;
|
|
212
|
+
skipNextChar();
|
|
213
|
+
};
|
|
214
|
+
const gotoStart = () => {
|
|
215
|
+
scan.state = "start";
|
|
216
|
+
scan.column = scan.nextColumn;
|
|
217
|
+
};
|
|
218
|
+
const startNewLine = () => {
|
|
219
|
+
scan.lines.push(scan.line);
|
|
220
|
+
scan.line = [];
|
|
221
|
+
scan.column = 0;
|
|
222
|
+
};
|
|
223
|
+
const throwTokenizingError = (message: string): never => {
|
|
224
|
+
throw new PcalError(
|
|
225
|
+
`${message} \`${scan.token}' found at\n line ${reader.line + 1}, column ${scan.column + 1}`,
|
|
226
|
+
);
|
|
227
|
+
};
|
|
228
|
+
const endExpression = () => {
|
|
229
|
+
scan.isExprEnd = true;
|
|
230
|
+
state.delimiter = scan.token;
|
|
231
|
+
state.delimiterCol = scan.column;
|
|
232
|
+
state.delimiterLine = reader.line - (scan.nextChar === "\n" ? 1 : 0);
|
|
233
|
+
};
|
|
234
|
+
// Tokenize.TokenOut.
|
|
235
|
+
const tokenOut = (type: number) => {
|
|
236
|
+
if (!isExpr) {
|
|
237
|
+
endExpression();
|
|
238
|
+
scan.token = "";
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (type > TOKEN_TYPE.ident) throwTokenizingError("Illegal token in an expression");
|
|
242
|
+
const token = scan.token;
|
|
243
|
+
const isDelimiter =
|
|
244
|
+
type !== TOKEN_TYPE.string &&
|
|
245
|
+
((DELIMITERS.has(token) &&
|
|
246
|
+
!(type === TOKEN_TYPE.ident && [".", "[", ","].includes(scan.previousToken))) ||
|
|
247
|
+
(((token === "," && !scan.isInQuantifier && !isInDefineBlock) ||
|
|
248
|
+
token === ")" ||
|
|
249
|
+
token === "}") &&
|
|
250
|
+
scan.parenDepth === 0));
|
|
251
|
+
if (isDelimiter) {
|
|
252
|
+
if (scan.parenDepth !== 0) {
|
|
253
|
+
throwTokenizingError("Expression with an unmatched (, [, {, or << followed by");
|
|
254
|
+
}
|
|
255
|
+
if (scan.isInQuantifier) {
|
|
256
|
+
throwTokenizingError("Expression with \\A or \\E but no following `:` followed by");
|
|
257
|
+
}
|
|
258
|
+
endExpression();
|
|
259
|
+
if (scan.line.length > 0) startNewLine();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (type === TOKEN_TYPE.builtin && LEFT_PARENS.has(token)) scan.parenDepth += 1;
|
|
263
|
+
if (type === TOKEN_TYPE.builtin && RIGHT_PARENS.has(token)) {
|
|
264
|
+
scan.parenDepth -= 1;
|
|
265
|
+
if (scan.parenDepth < 0) throwTokenizingError("Extra (unmatching)");
|
|
266
|
+
}
|
|
267
|
+
scan.previousToken = type === TOKEN_TYPE.string ? " " : token;
|
|
268
|
+
if (["\\A", "∀", "\\E", "∃"].includes(token) && scan.parenDepth === 0) {
|
|
269
|
+
scan.isInQuantifier = true;
|
|
270
|
+
}
|
|
271
|
+
if (scan.isInQuantifier && token === ":" && scan.parenDepth === 0) {
|
|
272
|
+
scan.isInQuantifier = false;
|
|
273
|
+
}
|
|
274
|
+
if (token !== "" || type === TOKEN_TYPE.string) {
|
|
275
|
+
scan.line.push(createPcalToken(token, scan.column, type));
|
|
276
|
+
}
|
|
277
|
+
scan.token = "";
|
|
278
|
+
};
|
|
279
|
+
const outputToken = (type: number) => {
|
|
280
|
+
tokenOut(type);
|
|
281
|
+
gotoStart();
|
|
282
|
+
};
|
|
283
|
+
const addAndGoto = (nextState: string) => {
|
|
284
|
+
addNextChar();
|
|
285
|
+
scan.state = nextState;
|
|
286
|
+
};
|
|
287
|
+
const skipAndGoto = (nextState: string) => {
|
|
288
|
+
skipNextChar();
|
|
289
|
+
scan.state = nextState;
|
|
290
|
+
};
|
|
291
|
+
// One transition of the state machine on the next character.
|
|
292
|
+
const step = (next: string) => {
|
|
293
|
+
switch (scan.state) {
|
|
294
|
+
case "start":
|
|
295
|
+
if (isSpace(next)) {
|
|
296
|
+
skipNextChar();
|
|
297
|
+
return gotoStart();
|
|
298
|
+
}
|
|
299
|
+
if (isLetter(next)) return addAndGoto("id");
|
|
300
|
+
if (next === "ℕ" || next === "ℤ" || next === "ℝ") {
|
|
301
|
+
addNextChar();
|
|
302
|
+
return outputToken(TOKEN_TYPE.ident);
|
|
303
|
+
}
|
|
304
|
+
if (isDigit(next)) return addAndGoto("numOrId");
|
|
305
|
+
if (next === "\\") return addAndGoto("bs");
|
|
306
|
+
if (next === "-") return addAndGoto("dash1");
|
|
307
|
+
if (next === "=") return addAndGoto("eq1");
|
|
308
|
+
if (next === "(") return skipAndGoto("leftParen");
|
|
309
|
+
if (next === '"') return skipAndGoto("string");
|
|
310
|
+
if (next === "\n") {
|
|
311
|
+
skipNextChar();
|
|
312
|
+
startNewLine();
|
|
313
|
+
return gotoStart();
|
|
314
|
+
}
|
|
315
|
+
if (BUILT_IN_PREFIXES.has(next)) return addAndGoto("builtIn");
|
|
316
|
+
if (next === "\t") {
|
|
317
|
+
scan.state = "done";
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
addNextChar();
|
|
321
|
+
return throwTokenizingError("Illegal lexeme");
|
|
322
|
+
case "id":
|
|
323
|
+
if (scan.token === "WF_" || scan.token === "SF_") return outputToken(TOKEN_TYPE.builtin);
|
|
324
|
+
if (isLetter(next) || isDigit(next)) return addNextChar();
|
|
325
|
+
return outputToken(
|
|
326
|
+
BUILT_IN_SYMBOLS.has(scan.token) ? TOKEN_TYPE.builtin : TOKEN_TYPE.ident,
|
|
327
|
+
);
|
|
328
|
+
case "numOrId":
|
|
329
|
+
if (isDigit(next)) return addNextChar();
|
|
330
|
+
if (isLetter(next)) return addAndGoto("id");
|
|
331
|
+
return outputToken(TOKEN_TYPE.number);
|
|
332
|
+
case "bs":
|
|
333
|
+
if (["b", "B", "o", "O", "h", "H"].includes(next)) return addAndGoto("numOrBi");
|
|
334
|
+
if (isLetter(next)) {
|
|
335
|
+
scan.state = "bsBuiltIn";
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (next === "*") {
|
|
339
|
+
skipNextChar();
|
|
340
|
+
scan.token = "";
|
|
341
|
+
scan.state = "lineComment";
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
scan.state = "builtIn";
|
|
345
|
+
return;
|
|
346
|
+
case "numOrBi":
|
|
347
|
+
scan.state = isDigit(next) ? "num" : "bsBuiltIn";
|
|
348
|
+
return;
|
|
349
|
+
case "num":
|
|
350
|
+
if (isDigit(next)) return addNextChar();
|
|
351
|
+
if (!isLetter(next)) return outputToken(TOKEN_TYPE.number);
|
|
352
|
+
addNextChar();
|
|
353
|
+
if (scan.token.startsWith("\\")) throwTokenizingError("Illegal lexeme");
|
|
354
|
+
scan.state = "id";
|
|
355
|
+
return;
|
|
356
|
+
case "bsBuiltIn":
|
|
357
|
+
if (isLetter(next) && next !== "_") return addNextChar();
|
|
358
|
+
if (BUILT_IN_SYMBOLS.has(scan.token)) return outputToken(TOKEN_TYPE.builtin);
|
|
359
|
+
return throwTokenizingError("Illegal lexeme ");
|
|
360
|
+
case "builtIn":
|
|
361
|
+
if (BUILT_IN_PREFIXES.has(scan.token + next)) return addNextChar();
|
|
362
|
+
// A prefix that is no symbol backs up to its longest symbol.
|
|
363
|
+
if (!BUILT_IN_SYMBOLS.has(scan.token)) {
|
|
364
|
+
backspacePcalReader(reader);
|
|
365
|
+
while (!BUILT_IN_SYMBOLS.has(scan.token)) {
|
|
366
|
+
backspacePcalReader(reader);
|
|
367
|
+
if (scan.token.length === 0) throwTokenizingError("Illegal lexeme");
|
|
368
|
+
scan.token = Array.from(scan.token).slice(0, -1).join("");
|
|
369
|
+
}
|
|
370
|
+
skipNextChar();
|
|
371
|
+
}
|
|
372
|
+
return outputToken(TOKEN_TYPE.builtin);
|
|
373
|
+
case "dash1":
|
|
374
|
+
case "dash2":
|
|
375
|
+
if (next === "-") return addAndGoto(scan.state === "dash1" ? "dash2" : "dash3");
|
|
376
|
+
scan.state = "builtIn";
|
|
377
|
+
return;
|
|
378
|
+
case "dash3":
|
|
379
|
+
if (next !== "-") throwTokenizingError("Illegal lexeme");
|
|
380
|
+
return addAndGoto("dashes");
|
|
381
|
+
case "dashes":
|
|
382
|
+
return next === "-" ? addNextChar() : outputToken(TOKEN_TYPE.dashes);
|
|
383
|
+
case "eq1":
|
|
384
|
+
case "eq2":
|
|
385
|
+
if (next === "=") return addAndGoto(scan.state === "eq1" ? "eq2" : "eq3");
|
|
386
|
+
scan.state = "builtIn";
|
|
387
|
+
return;
|
|
388
|
+
case "eq3":
|
|
389
|
+
if (next !== "=") throwTokenizingError("Illegal lexeme");
|
|
390
|
+
return addAndGoto("eqs");
|
|
391
|
+
case "eqs":
|
|
392
|
+
return next === "=" ? addNextChar() : outputToken(TOKEN_TYPE.endModule);
|
|
393
|
+
case "leftParen":
|
|
394
|
+
if (next === "*") {
|
|
395
|
+
state.commentDepth = 1;
|
|
396
|
+
return skipAndGoto("comment");
|
|
397
|
+
}
|
|
398
|
+
scan.token = "(";
|
|
399
|
+
scan.state = "builtIn";
|
|
400
|
+
return;
|
|
401
|
+
case "string":
|
|
402
|
+
if (next === "\\") return addAndGoto("escString");
|
|
403
|
+
if (next === '"') {
|
|
404
|
+
skipNextChar();
|
|
405
|
+
return outputToken(TOKEN_TYPE.string);
|
|
406
|
+
}
|
|
407
|
+
addNextChar();
|
|
408
|
+
if (!STRING_CHARS.has(next)) throwTokenizingError("Illegal character in string");
|
|
409
|
+
return;
|
|
410
|
+
case "escString":
|
|
411
|
+
addNextChar();
|
|
412
|
+
if (!['"', "\\", "t", "n", "f", "r"].includes(next)) {
|
|
413
|
+
throwTokenizingError("Illegal character following \\ in string");
|
|
414
|
+
}
|
|
415
|
+
scan.state = "string";
|
|
416
|
+
return;
|
|
417
|
+
case "lineComment":
|
|
418
|
+
if (next === "(") return skipAndGoto("lineComParen");
|
|
419
|
+
if (next === "*" && state.commentDepth > 0) return skipAndGoto("lineComStar");
|
|
420
|
+
if (next === "\n" || next === "\t") {
|
|
421
|
+
scan.token = "";
|
|
422
|
+
state.commentDepth = 0;
|
|
423
|
+
return gotoStart();
|
|
424
|
+
}
|
|
425
|
+
return state.commentDepth === 0 ? addNextChar() : skipNextChar();
|
|
426
|
+
case "lineComParen":
|
|
427
|
+
if (next === "*") {
|
|
428
|
+
state.commentDepth += 1;
|
|
429
|
+
return skipAndGoto("lineComment");
|
|
430
|
+
}
|
|
431
|
+
if (state.commentDepth === 0) scan.token += "(";
|
|
432
|
+
scan.state = "lineComment";
|
|
433
|
+
return;
|
|
434
|
+
case "lineComStar":
|
|
435
|
+
if (next === ")") {
|
|
436
|
+
state.commentDepth -= 1;
|
|
437
|
+
if (state.commentDepth < 0) {
|
|
438
|
+
throw new PcalBugError("Failure of assertion: case LINE_COM_STAR");
|
|
439
|
+
}
|
|
440
|
+
return skipAndGoto("lineComment");
|
|
441
|
+
}
|
|
442
|
+
if (state.commentDepth === 0) scan.token += "*";
|
|
443
|
+
scan.state = "lineComment";
|
|
444
|
+
return;
|
|
445
|
+
// A comment that continues past its first line is an "or comment".
|
|
446
|
+
case "comment":
|
|
447
|
+
case "orComment": {
|
|
448
|
+
const isFirstLine = scan.state === "comment";
|
|
449
|
+
if (next === "*") return skipAndGoto(isFirstLine ? "commentStar" : "orCommentStar");
|
|
450
|
+
if (next === "(") return skipAndGoto(isFirstLine ? "commentParen" : "orCommentParen");
|
|
451
|
+
if (next === "\n") {
|
|
452
|
+
scan.token = "";
|
|
453
|
+
skipNextChar();
|
|
454
|
+
startNewLine();
|
|
455
|
+
scan.state = "orComment";
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (next === "\t") {
|
|
459
|
+
throw new PcalError(
|
|
460
|
+
isFirstLine
|
|
461
|
+
? "Input ended in the middle of a comment"
|
|
462
|
+
: "Input ended in the middle of a multi-line comment",
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
return state.commentDepth === 1 ? addNextChar() : skipNextChar();
|
|
466
|
+
}
|
|
467
|
+
case "commentStar":
|
|
468
|
+
case "orCommentStar": {
|
|
469
|
+
const commentState = scan.state === "commentStar" ? "comment" : "orComment";
|
|
470
|
+
if (next !== ")") {
|
|
471
|
+
if (state.commentDepth === 1) scan.token += "*";
|
|
472
|
+
scan.state = commentState;
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
skipNextChar();
|
|
476
|
+
state.commentDepth -= 1;
|
|
477
|
+
if (commentState === "orComment" && state.commentDepth < 0) {
|
|
478
|
+
throw new PcalBugError("Assertion failure");
|
|
479
|
+
}
|
|
480
|
+
if (state.commentDepth !== 0) {
|
|
481
|
+
scan.state = commentState;
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
scan.token = "";
|
|
485
|
+
return gotoStart();
|
|
486
|
+
}
|
|
487
|
+
case "commentParen":
|
|
488
|
+
case "orCommentParen": {
|
|
489
|
+
const commentState = scan.state === "commentParen" ? "comment" : "orComment";
|
|
490
|
+
if (next === "*") {
|
|
491
|
+
state.commentDepth += 1;
|
|
492
|
+
return skipAndGoto(commentState);
|
|
493
|
+
}
|
|
494
|
+
if (state.commentDepth === 1) scan.token += "(";
|
|
495
|
+
scan.state = commentState;
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
while (scan.state !== "done" && !scan.isExprEnd) step(scan.nextChar);
|
|
501
|
+
if (scan.nextChar !== "\n") backspacePcalReader(reader);
|
|
502
|
+
return createNormalizedPcalExpr(scan.lines);
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// Tokenize.TokenizeExpr and TokenizeDefineBlock.
|
|
506
|
+
export const tokenizePcalExpr = (
|
|
507
|
+
state: PcalTokenizerState,
|
|
508
|
+
reader: PcalCharReader,
|
|
509
|
+
isInDefineBlock: boolean,
|
|
510
|
+
) => tokenize(state, reader, true, isInDefineBlock);
|
|
511
|
+
|
|
512
|
+
// Tokenize.GetAlgorithmToken.
|
|
513
|
+
export const getPcalAlgorithmToken = (state: PcalTokenizerState, reader: PcalCharReader) => {
|
|
514
|
+
tokenize(state, reader, false, false);
|
|
515
|
+
return state.delimiter;
|
|
516
|
+
};
|