tla-ts 0.0.3-dev.07eab97 → 0.0.3-dev.0e500e5
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 +12 -0
- package/dist/index.d.ts +12 -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 +13 -13
- package/dist/run-cli.js +22 -22
- package/dist/transfer.cjs +24 -14
- package/dist/transfer.js +24 -14
- package/package.json +1 -1
- package/src/ast.ts +20 -3
- package/src/environment.ts +135 -45
- package/src/errors.ts +25 -3
- package/src/levels.ts +30 -26
- package/src/parser.ts +95 -34
- package/src/resolve.ts +844 -383
- package/src/run-cli.ts +5 -2
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +25 -13
|
@@ -0,0 +1,3143 @@
|
|
|
1
|
+
import { createSanyError, TlaParseError, TlaSyntaxError } from "./errors";
|
|
2
|
+
import { parseModule } from "./parser";
|
|
3
|
+
import {
|
|
4
|
+
_BM0,
|
|
5
|
+
_BM1,
|
|
6
|
+
_BM2,
|
|
7
|
+
ACTION,
|
|
8
|
+
AND,
|
|
9
|
+
ARAB,
|
|
10
|
+
ARROW,
|
|
11
|
+
ARSB,
|
|
12
|
+
ASSUME,
|
|
13
|
+
ASSUMPTION,
|
|
14
|
+
BANG,
|
|
15
|
+
BEGIN_PRAGMA,
|
|
16
|
+
BOXASSUME,
|
|
17
|
+
BOXPROVE,
|
|
18
|
+
BY,
|
|
19
|
+
CASE,
|
|
20
|
+
CASESEP,
|
|
21
|
+
CHOOSE,
|
|
22
|
+
COLON,
|
|
23
|
+
COLONCOLON,
|
|
24
|
+
COMMA,
|
|
25
|
+
CONSTANT,
|
|
26
|
+
createSanyLexer,
|
|
27
|
+
DEF,
|
|
28
|
+
DEFBREAK,
|
|
29
|
+
DEFINE,
|
|
30
|
+
DF,
|
|
31
|
+
DOT,
|
|
32
|
+
ELSE,
|
|
33
|
+
END_MODULE,
|
|
34
|
+
EOF,
|
|
35
|
+
EQUALS,
|
|
36
|
+
escapeImage,
|
|
37
|
+
EXCEPT,
|
|
38
|
+
EXISTS,
|
|
39
|
+
EXTENDS,
|
|
40
|
+
FORALL,
|
|
41
|
+
HAVE,
|
|
42
|
+
HIDE,
|
|
43
|
+
IDENTIFIER,
|
|
44
|
+
IF,
|
|
45
|
+
IN,
|
|
46
|
+
INSTANCE,
|
|
47
|
+
LAB,
|
|
48
|
+
LAMBDA,
|
|
49
|
+
LBC,
|
|
50
|
+
LBR,
|
|
51
|
+
LET,
|
|
52
|
+
LETIN,
|
|
53
|
+
LOCAL,
|
|
54
|
+
LSB,
|
|
55
|
+
MAPTO,
|
|
56
|
+
MINUS,
|
|
57
|
+
MODULE,
|
|
58
|
+
NEGATIVE,
|
|
59
|
+
NEW,
|
|
60
|
+
NUMBER,
|
|
61
|
+
NUMBER_LITERAL,
|
|
62
|
+
OBVIOUS,
|
|
63
|
+
OMITTED,
|
|
64
|
+
ONLY,
|
|
65
|
+
OR,
|
|
66
|
+
OTHER,
|
|
67
|
+
PICK,
|
|
68
|
+
PROOF,
|
|
69
|
+
PROOF_IMPLICIT_STEP,
|
|
70
|
+
PROOF_STEP,
|
|
71
|
+
PROPOSITION,
|
|
72
|
+
PROVE,
|
|
73
|
+
QED,
|
|
74
|
+
RAB,
|
|
75
|
+
RBC,
|
|
76
|
+
RBR,
|
|
77
|
+
RECURSIVE,
|
|
78
|
+
RSB,
|
|
79
|
+
SEPARATOR,
|
|
80
|
+
SF,
|
|
81
|
+
STATE,
|
|
82
|
+
STRING_LITERAL,
|
|
83
|
+
SUBSTITUTE,
|
|
84
|
+
SUFFICES,
|
|
85
|
+
T_EXISTS,
|
|
86
|
+
T_FORALL,
|
|
87
|
+
TAKE,
|
|
88
|
+
TEMPORAL,
|
|
89
|
+
THEN,
|
|
90
|
+
THEOREM,
|
|
91
|
+
US,
|
|
92
|
+
USE,
|
|
93
|
+
VARIABLE,
|
|
94
|
+
WF,
|
|
95
|
+
WITH,
|
|
96
|
+
WITNESS,
|
|
97
|
+
type SanyToken,
|
|
98
|
+
} from "./sany-lexer";
|
|
99
|
+
|
|
100
|
+
// A lookahead scan of SANY's grammar. It returns whether the tokens matched.
|
|
101
|
+
type Scan = () => boolean;
|
|
102
|
+
|
|
103
|
+
type ScanItem = number | Scan;
|
|
104
|
+
|
|
105
|
+
interface SyntaxNode {
|
|
106
|
+
kind: number | string;
|
|
107
|
+
image: string;
|
|
108
|
+
location: number[];
|
|
109
|
+
heirs: SyntaxNode[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface Operator {
|
|
113
|
+
name: string;
|
|
114
|
+
low: number;
|
|
115
|
+
high: number;
|
|
116
|
+
associativity: "none" | "left" | "right";
|
|
117
|
+
fix: "prefix" | "postfix" | "infix" | "nfix";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface StackElement {
|
|
121
|
+
node: SyntaxNode | undefined;
|
|
122
|
+
operator: Operator | undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface LookaheadCall {
|
|
126
|
+
generation: number;
|
|
127
|
+
first: SanyToken | undefined;
|
|
128
|
+
amount: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface Junction {
|
|
132
|
+
isConjunction: boolean;
|
|
133
|
+
column: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The state of SANY's parser: the last token it consumed, what its error message would say, and
|
|
137
|
+
// the context its productions keep.
|
|
138
|
+
interface ParserState {
|
|
139
|
+
token: SanyToken;
|
|
140
|
+
nextKind: number;
|
|
141
|
+
generation: number;
|
|
142
|
+
expectedGeneration: number;
|
|
143
|
+
moduleName: string | undefined;
|
|
144
|
+
expecting: string;
|
|
145
|
+
lastOperator: Operator | undefined;
|
|
146
|
+
fairnessHook: SyntaxNode | undefined;
|
|
147
|
+
proofDepth: number;
|
|
148
|
+
operatorStack: StackElement[] | undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The state of a lookahead scan, and of the rescan that finds the tokens an error message shows.
|
|
152
|
+
interface ScannerState {
|
|
153
|
+
scanPosition: SanyToken;
|
|
154
|
+
lastPosition: SanyToken;
|
|
155
|
+
remaining: number;
|
|
156
|
+
isSucceeded: boolean;
|
|
157
|
+
isLookingAhead: boolean;
|
|
158
|
+
isRescanning: boolean;
|
|
159
|
+
endPosition: number;
|
|
160
|
+
longestExpected: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
interface ProductionFrame {
|
|
164
|
+
name: string;
|
|
165
|
+
token: SanyToken;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const MAX_INT = 2147483647;
|
|
169
|
+
|
|
170
|
+
const MIN_INT = -2147483648;
|
|
171
|
+
|
|
172
|
+
const getRange = (from: number, to: number) =>
|
|
173
|
+
Array.from({ length: to - from + 1 }, (_, offset) => from + offset);
|
|
174
|
+
|
|
175
|
+
const PREFIX_OPERATORS = getRange(118, 127);
|
|
176
|
+
|
|
177
|
+
const NON_EXPRESSION_PREFIX_OPERATORS = getRange(117, 127);
|
|
178
|
+
|
|
179
|
+
const INFIX_OPERATORS = getRange(128, 288);
|
|
180
|
+
|
|
181
|
+
const POSTFIX_OPERATORS = getRange(113, 116);
|
|
182
|
+
|
|
183
|
+
const STEP_STARTS = getRange(290, 294);
|
|
184
|
+
|
|
185
|
+
const OPEN_STARTS = [CASE, CHOOSE, EXISTS, FORALL, IF, LET, T_EXISTS, T_FORALL];
|
|
186
|
+
|
|
187
|
+
const PRIMITIVE_STARTS = new Set(getRange(113, 291));
|
|
188
|
+
|
|
189
|
+
const BRACKETED_STARTS = new Set([SF, WF, LBR, LSB, LBC, LAB]);
|
|
190
|
+
|
|
191
|
+
const NEW_SYMBOL_STARTS = new Set([ACTION, CONSTANT, NEW, STATE, TEMPORAL, VARIABLE]);
|
|
192
|
+
|
|
193
|
+
const OPERATOR_DECLARATION_STARTS = new Set([US, ...NON_EXPRESSION_PREFIX_OPERATORS]);
|
|
194
|
+
|
|
195
|
+
const FAIRNESS_SUBSCRIPTS = new Set([
|
|
196
|
+
"Tuple",
|
|
197
|
+
"ParenExpr",
|
|
198
|
+
"SetEnumerate",
|
|
199
|
+
"SubsetOf",
|
|
200
|
+
"SetOfAll",
|
|
201
|
+
"SetOfFcns",
|
|
202
|
+
"RcdConstructor",
|
|
203
|
+
"SetOfRcds",
|
|
204
|
+
"Except",
|
|
205
|
+
"FcnConst",
|
|
206
|
+
"ActionExpr",
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
const OPERATOR_ROWS: [string, number, number, Operator["associativity"], Operator["fix"]][] = [
|
|
210
|
+
["[", 160, 160, "left", "postfix"],
|
|
211
|
+
[".", 170, 170, "left", "infix"],
|
|
212
|
+
["'", 150, 150, "none", "postfix"],
|
|
213
|
+
["^", 140, 140, "none", "infix"],
|
|
214
|
+
["/", 130, 130, "none", "infix"],
|
|
215
|
+
["*", 130, 130, "left", "infix"],
|
|
216
|
+
["-", 110, 110, "left", "infix"],
|
|
217
|
+
["-.", 120, 120, "none", "prefix"],
|
|
218
|
+
["+", 100, 100, "left", "infix"],
|
|
219
|
+
["=", 50, 50, "none", "infix"],
|
|
220
|
+
["\\lnot", 40, 40, "left", "prefix"],
|
|
221
|
+
["\\land", 30, 30, "left", "infix"],
|
|
222
|
+
["\\lor", 30, 30, "left", "infix"],
|
|
223
|
+
["~>", 20, 20, "none", "infix"],
|
|
224
|
+
["=>", 10, 10, "none", "infix"],
|
|
225
|
+
["[]", 40, 150, "none", "prefix"],
|
|
226
|
+
["<>", 40, 150, "none", "prefix"],
|
|
227
|
+
["ENABLED", 40, 150, "none", "prefix"],
|
|
228
|
+
["UNCHANGED", 40, 150, "none", "prefix"],
|
|
229
|
+
["SUBSET", 100, 130, "none", "prefix"],
|
|
230
|
+
["UNION", 100, 130, "none", "prefix"],
|
|
231
|
+
["DOMAIN", 100, 130, "none", "prefix"],
|
|
232
|
+
["^+", 150, 150, "none", "postfix"],
|
|
233
|
+
["^*", 150, 150, "none", "postfix"],
|
|
234
|
+
["^#", 150, 150, "none", "postfix"],
|
|
235
|
+
["\\cdot", 50, 140, "left", "infix"],
|
|
236
|
+
["\\equiv", 20, 20, "none", "infix"],
|
|
237
|
+
["-+->", 20, 20, "none", "infix"],
|
|
238
|
+
["/=", 50, 50, "none", "infix"],
|
|
239
|
+
["\\subseteq", 50, 50, "none", "infix"],
|
|
240
|
+
["\\in", 50, 50, "none", "infix"],
|
|
241
|
+
["\\notin", 50, 50, "none", "infix"],
|
|
242
|
+
["<", 50, 50, "none", "infix"],
|
|
243
|
+
["\\leq", 50, 50, "none", "infix"],
|
|
244
|
+
[">", 50, 50, "none", "infix"],
|
|
245
|
+
["\\geq", 50, 50, "none", "infix"],
|
|
246
|
+
["\\times", 100, 130, "left", "nfix"],
|
|
247
|
+
["\\", 80, 80, "none", "infix"],
|
|
248
|
+
["\\intersect", 80, 80, "left", "infix"],
|
|
249
|
+
["\\union", 80, 80, "left", "infix"],
|
|
250
|
+
["...", 90, 90, "none", "infix"],
|
|
251
|
+
["..", 90, 90, "none", "infix"],
|
|
252
|
+
["|", 100, 110, "left", "infix"],
|
|
253
|
+
["||", 100, 110, "left", "infix"],
|
|
254
|
+
["&&", 130, 130, "left", "infix"],
|
|
255
|
+
["&", 130, 130, "left", "infix"],
|
|
256
|
+
["$$", 90, 130, "left", "infix"],
|
|
257
|
+
["$", 90, 130, "left", "infix"],
|
|
258
|
+
["??", 90, 130, "left", "infix"],
|
|
259
|
+
["%%", 100, 110, "left", "infix"],
|
|
260
|
+
["%", 100, 110, "none", "infix"],
|
|
261
|
+
["##", 90, 130, "left", "infix"],
|
|
262
|
+
["++", 100, 100, "left", "infix"],
|
|
263
|
+
["--", 110, 110, "left", "infix"],
|
|
264
|
+
["**", 130, 130, "left", "infix"],
|
|
265
|
+
["//", 130, 130, "none", "infix"],
|
|
266
|
+
["^^", 140, 140, "none", "infix"],
|
|
267
|
+
["@@", 60, 60, "left", "infix"],
|
|
268
|
+
["!!", 90, 130, "none", "infix"],
|
|
269
|
+
["|-", 50, 50, "none", "infix"],
|
|
270
|
+
["|=", 50, 50, "none", "infix"],
|
|
271
|
+
["-|", 50, 50, "none", "infix"],
|
|
272
|
+
["=|", 50, 50, "none", "infix"],
|
|
273
|
+
["<:", 70, 70, "none", "infix"],
|
|
274
|
+
[":>", 70, 70, "none", "infix"],
|
|
275
|
+
[":=", 50, 50, "none", "infix"],
|
|
276
|
+
["::=", 50, 50, "none", "infix"],
|
|
277
|
+
["\\oplus", 100, 100, "left", "infix"],
|
|
278
|
+
["\\ominus", 110, 110, "left", "infix"],
|
|
279
|
+
["\\odot", 130, 130, "left", "infix"],
|
|
280
|
+
["\\oslash", 130, 130, "none", "infix"],
|
|
281
|
+
["\\otimes", 130, 130, "left", "infix"],
|
|
282
|
+
["\\uplus", 90, 130, "left", "infix"],
|
|
283
|
+
["\\sqcap", 90, 130, "left", "infix"],
|
|
284
|
+
["\\sqcup", 90, 130, "left", "infix"],
|
|
285
|
+
["\\div", 130, 130, "none", "infix"],
|
|
286
|
+
["\\wr", 90, 140, "none", "infix"],
|
|
287
|
+
["\\star", 130, 130, "left", "infix"],
|
|
288
|
+
["\\o", 130, 130, "left", "infix"],
|
|
289
|
+
["\\bigcirc", 130, 130, "left", "infix"],
|
|
290
|
+
["\\bullet", 130, 130, "left", "infix"],
|
|
291
|
+
["\\prec", 50, 50, "none", "infix"],
|
|
292
|
+
["\\succ", 50, 50, "none", "infix"],
|
|
293
|
+
["\\preceq", 50, 50, "none", "infix"],
|
|
294
|
+
["\\succeq", 50, 50, "none", "infix"],
|
|
295
|
+
["\\sim", 50, 50, "none", "infix"],
|
|
296
|
+
["\\simeq", 50, 50, "none", "infix"],
|
|
297
|
+
["\\ll", 50, 50, "none", "infix"],
|
|
298
|
+
["\\gg", 50, 50, "none", "infix"],
|
|
299
|
+
["\\asymp", 50, 50, "none", "infix"],
|
|
300
|
+
["\\subset", 50, 50, "none", "infix"],
|
|
301
|
+
["\\supset", 50, 50, "none", "infix"],
|
|
302
|
+
["\\supseteq", 50, 50, "none", "infix"],
|
|
303
|
+
["\\approx", 50, 50, "none", "infix"],
|
|
304
|
+
["\\cong", 50, 50, "none", "infix"],
|
|
305
|
+
["\\sqsubset", 50, 50, "none", "infix"],
|
|
306
|
+
["\\sqsubseteq", 50, 50, "none", "infix"],
|
|
307
|
+
["\\sqsupset", 50, 50, "none", "infix"],
|
|
308
|
+
["\\sqsupseteq", 50, 50, "none", "infix"],
|
|
309
|
+
["\\doteq", 50, 50, "none", "infix"],
|
|
310
|
+
["\\propto", 50, 50, "none", "infix"],
|
|
311
|
+
];
|
|
312
|
+
|
|
313
|
+
const OPERATOR_TABLE: Operator[] = OPERATOR_ROWS.map((entry) => ({
|
|
314
|
+
name: entry[0],
|
|
315
|
+
low: entry[1],
|
|
316
|
+
high: entry[2],
|
|
317
|
+
associativity: entry[3],
|
|
318
|
+
fix: entry[4],
|
|
319
|
+
}));
|
|
320
|
+
|
|
321
|
+
const OPERATOR_SYNONYMS = [
|
|
322
|
+
["\\lnot", "~", "\\neg", "¬"],
|
|
323
|
+
["[]", "□"],
|
|
324
|
+
["<>", "◇"],
|
|
325
|
+
["\\land", "/\\", "∧"],
|
|
326
|
+
["\\lor", "\\/", "∨"],
|
|
327
|
+
["\\equiv", "<=>", "≡", "⇔"],
|
|
328
|
+
["/=", "#", "≠"],
|
|
329
|
+
["\\leq", "<=", "=<", "≤"],
|
|
330
|
+
["\\geq", ">=", "≥"],
|
|
331
|
+
["\\times", "\\X", "×"],
|
|
332
|
+
["\\intersect", "\\cap", "∩"],
|
|
333
|
+
["\\union", "\\cup", "∪"],
|
|
334
|
+
["\\o", "\\circ", "∘"],
|
|
335
|
+
["\\oplus", "(+)", "⊕"],
|
|
336
|
+
["\\ominus", "(-)", "⊖"],
|
|
337
|
+
["\\odot", "(.)", "⊙"],
|
|
338
|
+
["\\oslash", "(/)", "⊘"],
|
|
339
|
+
["\\otimes", "(\\X)", "⊗"],
|
|
340
|
+
["\\approx", "≈"],
|
|
341
|
+
[":=", "≔"],
|
|
342
|
+
["\\asymp", "≍"],
|
|
343
|
+
["\\bigcirc", "◯"],
|
|
344
|
+
["::=", "⩴"],
|
|
345
|
+
["\\bullet", "●"],
|
|
346
|
+
["\\cdot", "⋅"],
|
|
347
|
+
["\\cong", "≅"],
|
|
348
|
+
["\\div", "÷"],
|
|
349
|
+
["\\doteq", "≐"],
|
|
350
|
+
["..", "‥"],
|
|
351
|
+
["...", "…"],
|
|
352
|
+
["!!", "‼"],
|
|
353
|
+
["\\gg", "≫"],
|
|
354
|
+
["=>", "⇒"],
|
|
355
|
+
["\\in", "∈"],
|
|
356
|
+
["=|", "⫤"],
|
|
357
|
+
["~>", "↝"],
|
|
358
|
+
["\\ll", "≪"],
|
|
359
|
+
["-|", "⊣"],
|
|
360
|
+
["\\notin", "∉"],
|
|
361
|
+
["-+->", "⇸"],
|
|
362
|
+
["\\prec", "≺"],
|
|
363
|
+
["\\preceq", "⪯"],
|
|
364
|
+
["\\propto", "∝"],
|
|
365
|
+
["??", "⁇"],
|
|
366
|
+
["|=", "⊨"],
|
|
367
|
+
["|-", "⊢"],
|
|
368
|
+
["\\sim", "∼"],
|
|
369
|
+
["\\simeq", "≃"],
|
|
370
|
+
["\\sqcap", "⊓"],
|
|
371
|
+
["\\sqcup", "⊔"],
|
|
372
|
+
["\\sqsubset", "⊏"],
|
|
373
|
+
["\\sqsubseteq", "⊑"],
|
|
374
|
+
["\\sqsupset", "⊐"],
|
|
375
|
+
["\\sqsupseteq", "⊒"],
|
|
376
|
+
["\\star", "⋆"],
|
|
377
|
+
["\\subset", "⊂"],
|
|
378
|
+
["\\subseteq", "⊆"],
|
|
379
|
+
["\\succ", "≻"],
|
|
380
|
+
["\\succeq", "⪰"],
|
|
381
|
+
["\\supset", "⊃"],
|
|
382
|
+
["\\supseteq", "⊇"],
|
|
383
|
+
["\\uplus", "⊎"],
|
|
384
|
+
["||", "‖"],
|
|
385
|
+
["\\wr", "≀"],
|
|
386
|
+
["^+", "⁺"],
|
|
387
|
+
];
|
|
388
|
+
|
|
389
|
+
const getOperators = () => {
|
|
390
|
+
const operators = new Map(OPERATOR_TABLE.map((operator) => [operator.name, operator]));
|
|
391
|
+
for (const synonyms of OPERATOR_SYNONYMS) {
|
|
392
|
+
const operator = operators.get(synonyms[0]);
|
|
393
|
+
if (operator) for (const synonym of synonyms.slice(1)) operators.set(synonym, operator);
|
|
394
|
+
}
|
|
395
|
+
return operators;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const OPERATORS = getOperators();
|
|
399
|
+
|
|
400
|
+
const FUNCTION_OPERATOR = OPERATORS.get("[");
|
|
401
|
+
|
|
402
|
+
const VOID_OPERATOR: Operator = {
|
|
403
|
+
name: "$$_void",
|
|
404
|
+
low: 0,
|
|
405
|
+
high: 0,
|
|
406
|
+
associativity: "none",
|
|
407
|
+
fix: "infix",
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const getMixfix = (operator: Operator) =>
|
|
411
|
+
operator.fix === "prefix" ? operator : OPERATORS.get(`${operator.name}.`);
|
|
412
|
+
|
|
413
|
+
const isInfix = (operator: Operator) => operator.fix === "infix" || operator.fix === "nfix";
|
|
414
|
+
|
|
415
|
+
const isSucceeding = (left: Operator, right: Operator) => left.low > right.high;
|
|
416
|
+
|
|
417
|
+
const isPreceding = (left: Operator, right: Operator) => left.high < right.low;
|
|
418
|
+
|
|
419
|
+
const isOperatorToken = (token: SanyToken | undefined) =>
|
|
420
|
+
token !== undefined && token.kind >= 113 && token.kind <= 288;
|
|
421
|
+
|
|
422
|
+
const isInfixToken = (token: SanyToken | undefined) =>
|
|
423
|
+
token !== undefined && token.kind >= 128 && token.kind <= 288;
|
|
424
|
+
|
|
425
|
+
// SANY's belchDEF: the index of the token that begins the left-hand side of a definition whose
|
|
426
|
+
// last token is at end, or 0 when it finds none.
|
|
427
|
+
const getDefinitionStart = (tokens: SanyToken[], end: number) => {
|
|
428
|
+
const last = tokens[end];
|
|
429
|
+
if (last.kind === RBR || last.kind === RSB) {
|
|
430
|
+
const bracket = { index: end, depth: 1 };
|
|
431
|
+
while (bracket.index > 0) {
|
|
432
|
+
bracket.index--;
|
|
433
|
+
const kind = tokens[bracket.index].kind;
|
|
434
|
+
const isOpening = kind === LBR || kind === LSB;
|
|
435
|
+
if (isOpening && bracket.depth === 1) break;
|
|
436
|
+
if (isOpening) bracket.depth--;
|
|
437
|
+
if (kind === RBR || kind === RSB) bracket.depth++;
|
|
438
|
+
}
|
|
439
|
+
return bracket.index > 0 && tokens[bracket.index - 1].kind === IDENTIFIER
|
|
440
|
+
? bracket.index - 1
|
|
441
|
+
: bracket.index;
|
|
442
|
+
}
|
|
443
|
+
if (last.kind === IDENTIFIER) {
|
|
444
|
+
const before = tokens[end - 1];
|
|
445
|
+
const isOperatorLeftSide =
|
|
446
|
+
isOperatorToken(before) &&
|
|
447
|
+
!(before.kind >= 113 && before.kind <= 116) &&
|
|
448
|
+
(!isInfixToken(before) || tokens[end - 2]?.kind === IDENTIFIER);
|
|
449
|
+
return !isOperatorLeftSide || tokens[end - 2]?.kind === SUBSTITUTE
|
|
450
|
+
? end
|
|
451
|
+
: isInfixToken(before)
|
|
452
|
+
? end - 2
|
|
453
|
+
: end - 1;
|
|
454
|
+
}
|
|
455
|
+
return isOperatorToken(last) ? end - 1 : end;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const isFieldNameToken = (token: SanyToken) =>
|
|
459
|
+
(token.kind >= ACTION && token.kind <= EXCEPT) ||
|
|
460
|
+
token.kind === EXTENDS ||
|
|
461
|
+
(token.kind >= IF && token.kind <= SF) ||
|
|
462
|
+
token.kind === STATE ||
|
|
463
|
+
(token.kind >= THEN && token.kind <= WITH) ||
|
|
464
|
+
token.kind === US ||
|
|
465
|
+
(token.kind >= 123 && token.kind <= 127);
|
|
466
|
+
|
|
467
|
+
const getLevelOfStep = (token: SanyToken) =>
|
|
468
|
+
token.image[1] === "*"
|
|
469
|
+
? -1
|
|
470
|
+
: token.image[1] === "+"
|
|
471
|
+
? -2
|
|
472
|
+
: Number.parseInt(token.image.slice(1, token.image.indexOf(">")));
|
|
473
|
+
|
|
474
|
+
// SANY's message for a module its parser rejects, or undefined when SANY parses it. This ports the
|
|
475
|
+
// control flow of SANY's JavaCC parser and token manager, since the message depends on the
|
|
476
|
+
// productions and lookaheads the parser was in when it failed.
|
|
477
|
+
export const getSanyParseError = (source: string): string | undefined => {
|
|
478
|
+
const lexer = createSanyLexer(source);
|
|
479
|
+
const errors: string[] = [];
|
|
480
|
+
const headToken: SanyToken = {
|
|
481
|
+
kind: EOF,
|
|
482
|
+
image: "",
|
|
483
|
+
beginLine: 0,
|
|
484
|
+
beginColumn: 0,
|
|
485
|
+
endLine: 0,
|
|
486
|
+
endColumn: 0,
|
|
487
|
+
next: undefined,
|
|
488
|
+
};
|
|
489
|
+
const parser: ParserState = {
|
|
490
|
+
token: headToken,
|
|
491
|
+
nextKind: -1,
|
|
492
|
+
generation: 0,
|
|
493
|
+
expectedGeneration: -1,
|
|
494
|
+
moduleName: undefined,
|
|
495
|
+
expecting: "",
|
|
496
|
+
lastOperator: undefined,
|
|
497
|
+
fairnessHook: undefined,
|
|
498
|
+
proofDepth: -1,
|
|
499
|
+
operatorStack: undefined,
|
|
500
|
+
};
|
|
501
|
+
const scanner: ScannerState = {
|
|
502
|
+
scanPosition: headToken,
|
|
503
|
+
lastPosition: headToken,
|
|
504
|
+
remaining: 0,
|
|
505
|
+
isSucceeded: false,
|
|
506
|
+
isLookingAhead: false,
|
|
507
|
+
isRescanning: false,
|
|
508
|
+
endPosition: 0,
|
|
509
|
+
longestExpected: 0,
|
|
510
|
+
};
|
|
511
|
+
const proofLevels: number[] = [];
|
|
512
|
+
const frames: ProductionFrame[] = [];
|
|
513
|
+
const heirStack: (SyntaxNode | undefined)[] = [];
|
|
514
|
+
const junctions: Junction[] = [];
|
|
515
|
+
const operatorStacks: StackElement[][] = [];
|
|
516
|
+
|
|
517
|
+
const getNext = (current: SanyToken) => (current.next ??= lexer.getNextToken());
|
|
518
|
+
|
|
519
|
+
const getToken = (index: number) =>
|
|
520
|
+
Array.from({ length: index }).reduce<SanyToken>(
|
|
521
|
+
(current) => getNext(current),
|
|
522
|
+
scanner.isLookingAhead ? scanner.scanPosition : parser.token,
|
|
523
|
+
);
|
|
524
|
+
|
|
525
|
+
const getNextKind = () =>
|
|
526
|
+
parser.nextKind === -1 ? (parser.nextKind = getNext(parser.token).kind) : parser.nextKind;
|
|
527
|
+
|
|
528
|
+
const markExpected = () => {
|
|
529
|
+
parser.expectedGeneration = parser.generation;
|
|
530
|
+
return false;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const isNextKind = (kind: number) => getNextKind() === kind || markExpected();
|
|
534
|
+
|
|
535
|
+
const formatLocation = (node: SyntaxNode) =>
|
|
536
|
+
`line ${node.location[0]}, col ${node.location[1]} to line ${node.location[2]}, col ${node.location[3]} of module ${parser.moduleName ?? "null"}`;
|
|
537
|
+
|
|
538
|
+
const getParseError = (message: string) =>
|
|
539
|
+
new TlaParseError(
|
|
540
|
+
`***Parse Error***\n${parser.expecting === "" ? "" : `Was expecting "${parser.expecting}"\n`}${message}\n\nResidual stack trace follows:\n${frames
|
|
541
|
+
.slice(-5)
|
|
542
|
+
.reverse()
|
|
543
|
+
.map(
|
|
544
|
+
(frame) =>
|
|
545
|
+
`${frame.name} starting at line ${frame.token.beginLine}, column ${frame.token.beginColumn}.\n`,
|
|
546
|
+
)
|
|
547
|
+
.join("")}`,
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
const addErrorToken = (position: number) => {
|
|
551
|
+
if (position >= 100) return;
|
|
552
|
+
if (position === scanner.endPosition + 1) {
|
|
553
|
+
scanner.endPosition++;
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (scanner.endPosition === 0) return;
|
|
557
|
+
scanner.longestExpected = Math.max(scanner.longestExpected, scanner.endPosition);
|
|
558
|
+
if (position !== 0) scanner.endPosition = position;
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
// How far the scan has reached past start, while that is within the 100 tokens JavaCC tracks.
|
|
562
|
+
const getScanOffset = (start: SanyToken | undefined, offset: number): number | undefined =>
|
|
563
|
+
start === undefined || offset >= 100
|
|
564
|
+
? undefined
|
|
565
|
+
: start === scanner.scanPosition
|
|
566
|
+
? offset
|
|
567
|
+
: getScanOffset(start.next, offset + 1);
|
|
568
|
+
|
|
569
|
+
const scanToken = (kinds: ReadonlySet<number>) => {
|
|
570
|
+
if (scanner.scanPosition === scanner.lastPosition) {
|
|
571
|
+
scanner.remaining--;
|
|
572
|
+
scanner.lastPosition = getNext(scanner.scanPosition);
|
|
573
|
+
}
|
|
574
|
+
scanner.scanPosition = getNext(scanner.scanPosition);
|
|
575
|
+
const offset = scanner.isRescanning ? getScanOffset(parser.token, 0) : undefined;
|
|
576
|
+
if (offset !== undefined) addErrorToken(offset);
|
|
577
|
+
if (!kinds.has(scanner.scanPosition.kind)) return false;
|
|
578
|
+
if (scanner.remaining === 0 && scanner.scanPosition === scanner.lastPosition)
|
|
579
|
+
scanner.isSucceeded = true;
|
|
580
|
+
return true;
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const toScan = (item: ScanItem): Scan => {
|
|
584
|
+
if (typeof item !== "number") return item;
|
|
585
|
+
const kinds = new Set([item]);
|
|
586
|
+
return () => scanToken(kinds);
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const oneOf = (...kinds: number[]): Scan => {
|
|
590
|
+
const kindSet = new Set(kinds);
|
|
591
|
+
return () => scanToken(kindSet);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
const sequence = (...items: ScanItem[]): Scan => {
|
|
595
|
+
const scans = items.map(toScan);
|
|
596
|
+
return () => {
|
|
597
|
+
for (const scan of scans) {
|
|
598
|
+
if (!scan()) return false;
|
|
599
|
+
if (scanner.isSucceeded) return true;
|
|
600
|
+
}
|
|
601
|
+
return true;
|
|
602
|
+
};
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
const choice = (...items: ScanItem[]): Scan => {
|
|
606
|
+
const scans = items.map(toScan);
|
|
607
|
+
return () => {
|
|
608
|
+
const start = scanner.scanPosition;
|
|
609
|
+
for (const scan of scans) {
|
|
610
|
+
scanner.scanPosition = start;
|
|
611
|
+
if (scan()) return true;
|
|
612
|
+
}
|
|
613
|
+
return false;
|
|
614
|
+
};
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
const optional = (item: ScanItem): Scan => {
|
|
618
|
+
const scan = toScan(item);
|
|
619
|
+
return () => {
|
|
620
|
+
const start = scanner.scanPosition;
|
|
621
|
+
if (!scan()) scanner.scanPosition = start;
|
|
622
|
+
return true;
|
|
623
|
+
};
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
const repeat = (item: ScanItem): Scan => {
|
|
627
|
+
const scan = toScan(item);
|
|
628
|
+
return () => {
|
|
629
|
+
for (;;) {
|
|
630
|
+
const start = scanner.scanPosition;
|
|
631
|
+
if (!scan()) {
|
|
632
|
+
scanner.scanPosition = start;
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
if (scanner.isSucceeded) return true;
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
const guard = (isAllowed: () => boolean, item: ScanItem): Scan => {
|
|
641
|
+
const scan = toScan(item);
|
|
642
|
+
return () => {
|
|
643
|
+
scanner.isLookingAhead = true;
|
|
644
|
+
const isAllowedHere = isAllowed();
|
|
645
|
+
scanner.isLookingAhead = false;
|
|
646
|
+
return isAllowedHere && scan();
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
const isAboveJunction = (column: number) =>
|
|
651
|
+
junctions.length === 0 || junctions[junctions.length - 1].column < column;
|
|
652
|
+
|
|
653
|
+
const isNextAboveJunction = () => isAboveJunction(getToken(1).beginColumn);
|
|
654
|
+
|
|
655
|
+
const isNewBullet = () => {
|
|
656
|
+
const next = getToken(1);
|
|
657
|
+
const junction = junctions.at(-1);
|
|
658
|
+
return (
|
|
659
|
+
junction !== undefined &&
|
|
660
|
+
(next.kind === AND || next.kind === OR) &&
|
|
661
|
+
junction.column === next.beginColumn &&
|
|
662
|
+
junction.isConjunction === (next.kind === AND)
|
|
663
|
+
);
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
const isNextAssume = () => getToken(1).kind === ASSUME || getToken(1).kind === BOXASSUME;
|
|
667
|
+
|
|
668
|
+
const isNestedAssume = () =>
|
|
669
|
+
isNextAssume() ||
|
|
670
|
+
(getToken(2).kind === COLONCOLON &&
|
|
671
|
+
(getToken(3).kind === ASSUME || getToken(3).kind === BOXASSUME));
|
|
672
|
+
|
|
673
|
+
const isOperandExpected = () => {
|
|
674
|
+
const operator = parser.operatorStack?.at(-1)?.operator;
|
|
675
|
+
return (
|
|
676
|
+
parser.operatorStack === undefined ||
|
|
677
|
+
parser.operatorStack.length === 0 ||
|
|
678
|
+
(operator !== undefined && operator.fix !== "postfix")
|
|
679
|
+
);
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// SANY's matchFcnConst: whether the tokens ahead begin "<< ... >> \in" or "x, y \in".
|
|
683
|
+
const isFunctionBound = () => {
|
|
684
|
+
const first = getNext(scanner.isLookingAhead ? scanner.scanPosition : parser.token);
|
|
685
|
+
if (first.kind === LAB) {
|
|
686
|
+
const tuple = { token: first, depth: 1 };
|
|
687
|
+
while (tuple.depth !== 0) {
|
|
688
|
+
tuple.token = getNext(tuple.token);
|
|
689
|
+
if (tuple.token.kind === LAB) tuple.depth++;
|
|
690
|
+
if (tuple.token.kind === RAB) tuple.depth--;
|
|
691
|
+
if (tuple.token.kind === EOF) return false;
|
|
692
|
+
}
|
|
693
|
+
return getNext(tuple.token).kind === IN;
|
|
694
|
+
}
|
|
695
|
+
if (first.kind !== IDENTIFIER) return false;
|
|
696
|
+
const list = { token: getNext(first) };
|
|
697
|
+
while (list.token.kind === COMMA) {
|
|
698
|
+
const identifier = getNext(list.token);
|
|
699
|
+
if (identifier.kind !== IDENTIFIER) return false;
|
|
700
|
+
list.token = getNext(identifier);
|
|
701
|
+
}
|
|
702
|
+
return list.token.kind === IN;
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
const expression: Scan = () => expressionScan();
|
|
706
|
+
const assumeProve: Scan = () => assumeProveScan();
|
|
707
|
+
const identifierTuple = sequence(
|
|
708
|
+
LAB,
|
|
709
|
+
optional(sequence(IDENTIFIER, repeat(sequence(COMMA, IDENTIFIER)))),
|
|
710
|
+
RAB,
|
|
711
|
+
);
|
|
712
|
+
const boundStart = choice(identifierTuple, IDENTIFIER);
|
|
713
|
+
const decimalPart = sequence(DOT, NUMBER_LITERAL);
|
|
714
|
+
const number = sequence(NUMBER_LITERAL, optional(decimalPart));
|
|
715
|
+
const parenthesized = sequence(LBR, expression);
|
|
716
|
+
const setContents = choice(
|
|
717
|
+
guard(isFunctionBound, boundStart),
|
|
718
|
+
expression,
|
|
719
|
+
expression,
|
|
720
|
+
expression,
|
|
721
|
+
);
|
|
722
|
+
const braceCases = sequence(LBC, optional(setContents), RBC);
|
|
723
|
+
const bracketCases = sequence(
|
|
724
|
+
LSB,
|
|
725
|
+
choice(
|
|
726
|
+
guard(isFunctionBound, boundStart),
|
|
727
|
+
IDENTIFIER,
|
|
728
|
+
guard(() => getToken(2).kind === MAPTO && isFieldNameToken(getToken(1)), IDENTIFIER),
|
|
729
|
+
IDENTIFIER,
|
|
730
|
+
expression,
|
|
731
|
+
),
|
|
732
|
+
);
|
|
733
|
+
const tupleOrAction = sequence(LAB, optional(expression), oneOf(RAB, ARAB));
|
|
734
|
+
const junctionItem = sequence(oneOf(OR, AND), expression);
|
|
735
|
+
const definition = sequence(
|
|
736
|
+
optional(LOCAL),
|
|
737
|
+
DEFBREAK,
|
|
738
|
+
oneOf(IDENTIFIER, ...NON_EXPRESSION_PREFIX_OPERATORS),
|
|
739
|
+
);
|
|
740
|
+
const letDefinition = choice(definition, RECURSIVE);
|
|
741
|
+
const identifierDeclaration = sequence(IDENTIFIER, optional(LBR));
|
|
742
|
+
const operatorDeclaration = oneOf(US, ...NON_EXPRESSION_PREFIX_OPERATORS);
|
|
743
|
+
const operatorArguments = sequence(
|
|
744
|
+
LBR,
|
|
745
|
+
choice(
|
|
746
|
+
guard(
|
|
747
|
+
isNextAboveJunction,
|
|
748
|
+
oneOf(...NON_EXPRESSION_PREFIX_OPERATORS, ...INFIX_OPERATORS, ...POSTFIX_OPERATORS),
|
|
749
|
+
),
|
|
750
|
+
guard(isNextAboveJunction, LAMBDA),
|
|
751
|
+
expression,
|
|
752
|
+
),
|
|
753
|
+
);
|
|
754
|
+
const newConstant = sequence(NEW, CONSTANT);
|
|
755
|
+
const constantDeclaration = sequence(identifierDeclaration, optional(IN));
|
|
756
|
+
const constantSymbol = sequence(
|
|
757
|
+
choice(newConstant, NEW, CONSTANT),
|
|
758
|
+
choice(constantDeclaration, operatorDeclaration),
|
|
759
|
+
);
|
|
760
|
+
const variableSymbol = sequence(optional(NEW), VARIABLE, IDENTIFIER);
|
|
761
|
+
const levelSymbol = sequence(
|
|
762
|
+
optional(NEW),
|
|
763
|
+
oneOf(STATE, ACTION, TEMPORAL),
|
|
764
|
+
choice(identifierDeclaration, operatorDeclaration),
|
|
765
|
+
);
|
|
766
|
+
const assumeProveScan = sequence(
|
|
767
|
+
optional(sequence(IDENTIFIER, COLONCOLON, assumeProve)),
|
|
768
|
+
oneOf(ASSUME, BOXASSUME),
|
|
769
|
+
choice(
|
|
770
|
+
guard(isNestedAssume, assumeProve),
|
|
771
|
+
choice(constantSymbol, variableSymbol, levelSymbol),
|
|
772
|
+
expression,
|
|
773
|
+
),
|
|
774
|
+
repeat(COMMA),
|
|
775
|
+
oneOf(PROVE, BOXPROVE),
|
|
776
|
+
);
|
|
777
|
+
const assumptionName = sequence(optional(DEFBREAK), IDENTIFIER, DEF);
|
|
778
|
+
const theoremName = sequence(IDENTIFIER, DEF);
|
|
779
|
+
const proofKeyword = toScan(PROOF);
|
|
780
|
+
const assertStep = sequence(
|
|
781
|
+
optional(SUFFICES),
|
|
782
|
+
choice(expression, guard(isNextAssume, assumeProve)),
|
|
783
|
+
);
|
|
784
|
+
const primitiveExpression = choice(
|
|
785
|
+
guard(isNextAboveJunction, STRING_LITERAL),
|
|
786
|
+
guard(isNextAboveJunction, number),
|
|
787
|
+
sequence(
|
|
788
|
+
oneOf(
|
|
789
|
+
IDENTIFIER,
|
|
790
|
+
PROOF_STEP,
|
|
791
|
+
PROOF_IMPLICIT_STEP,
|
|
792
|
+
...INFIX_OPERATORS,
|
|
793
|
+
...POSTFIX_OPERATORS,
|
|
794
|
+
...NON_EXPRESSION_PREFIX_OPERATORS,
|
|
795
|
+
),
|
|
796
|
+
optional(operatorArguments),
|
|
797
|
+
repeat(BANG),
|
|
798
|
+
),
|
|
799
|
+
);
|
|
800
|
+
const expressionSuffix = choice(
|
|
801
|
+
guard(isNextAboveJunction, oneOf(...POSTFIX_OPERATORS)),
|
|
802
|
+
guard(isNextAboveJunction, DOT),
|
|
803
|
+
guard(isNextAboveJunction, bracketCases),
|
|
804
|
+
);
|
|
805
|
+
const expressionContinuation = choice(
|
|
806
|
+
guard(isNextAboveJunction, oneOf(...INFIX_OPERATORS)),
|
|
807
|
+
guard(isNextAboveJunction, COLONCOLON),
|
|
808
|
+
);
|
|
809
|
+
const expressionScan = sequence(
|
|
810
|
+
repeat(oneOf(...PREFIX_OPERATORS, ...INFIX_OPERATORS)),
|
|
811
|
+
choice(
|
|
812
|
+
guard(
|
|
813
|
+
isNextAboveJunction,
|
|
814
|
+
choice(
|
|
815
|
+
sequence(oneOf(EXISTS, FORALL), choice(IDENTIFIER, boundStart)),
|
|
816
|
+
sequence(oneOf(T_EXISTS, T_FORALL), IDENTIFIER),
|
|
817
|
+
sequence(IF, expression),
|
|
818
|
+
sequence(CASE, expression),
|
|
819
|
+
sequence(LET, letDefinition, repeat(letDefinition)),
|
|
820
|
+
sequence(CHOOSE, choice(IDENTIFIER, identifierTuple)),
|
|
821
|
+
),
|
|
822
|
+
),
|
|
823
|
+
guard(
|
|
824
|
+
isNextAboveJunction,
|
|
825
|
+
sequence(
|
|
826
|
+
choice(
|
|
827
|
+
guard(isOperandExpected, choice(junctionItem, junctionItem)),
|
|
828
|
+
choice(
|
|
829
|
+
parenthesized,
|
|
830
|
+
braceCases,
|
|
831
|
+
bracketCases,
|
|
832
|
+
tupleOrAction,
|
|
833
|
+
sequence(
|
|
834
|
+
oneOf(WF, SF),
|
|
835
|
+
choice(IDENTIFIER, parenthesized, braceCases, bracketCases, tupleOrAction),
|
|
836
|
+
),
|
|
837
|
+
),
|
|
838
|
+
primitiveExpression,
|
|
839
|
+
),
|
|
840
|
+
repeat(expressionSuffix),
|
|
841
|
+
optional(expressionContinuation),
|
|
842
|
+
),
|
|
843
|
+
),
|
|
844
|
+
),
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
// Each lookahead of SANY's grammar with the number of tokens it may scan, in the order JavaCC
|
|
848
|
+
// numbers them, which is the order it rescans them to find the tokens an error message shows.
|
|
849
|
+
const lookaheads = {
|
|
850
|
+
moduleUnit: [
|
|
851
|
+
choice(
|
|
852
|
+
SEPARATOR,
|
|
853
|
+
VARIABLE,
|
|
854
|
+
CONSTANT,
|
|
855
|
+
definition,
|
|
856
|
+
RECURSIVE,
|
|
857
|
+
sequence(optional(LOCAL), INSTANCE, IDENTIFIER),
|
|
858
|
+
sequence(oneOf(ASSUMPTION, ASSUME), optional(assumptionName), expression),
|
|
859
|
+
sequence(
|
|
860
|
+
oneOf(THEOREM, PROPOSITION),
|
|
861
|
+
optional(theoremName),
|
|
862
|
+
choice(guard(isNextAssume, assumeProve), expression),
|
|
863
|
+
),
|
|
864
|
+
oneOf(_BM0, _BM1, _BM2),
|
|
865
|
+
guard(
|
|
866
|
+
() =>
|
|
867
|
+
(getToken(1).kind === USE && getToken(2).kind !== ONLY) || getToken(1).kind === HIDE,
|
|
868
|
+
choice(sequence(optional(PROOF), BY), USE, HIDE),
|
|
869
|
+
),
|
|
870
|
+
),
|
|
871
|
+
1,
|
|
872
|
+
],
|
|
873
|
+
unitDefinition: [definition, 2],
|
|
874
|
+
unitInstance: [sequence(optional(LOCAL), INSTANCE, IDENTIFIER), 2],
|
|
875
|
+
unitAssumption: [sequence(oneOf(ASSUMPTION, ASSUME), optional(assumptionName), expression), 2],
|
|
876
|
+
unitTheorem: [
|
|
877
|
+
sequence(
|
|
878
|
+
oneOf(THEOREM, PROPOSITION),
|
|
879
|
+
optional(theoremName),
|
|
880
|
+
choice(guard(isNextAssume, assumeProve), expression),
|
|
881
|
+
),
|
|
882
|
+
2,
|
|
883
|
+
],
|
|
884
|
+
constantArguments: [sequence(LBR, US), 2],
|
|
885
|
+
definitionBody: [expression, 1],
|
|
886
|
+
functionDefinition: [sequence(IDENTIFIER, LSB), MAX_INT],
|
|
887
|
+
postfixDefinition: [sequence(IDENTIFIER, oneOf(...POSTFIX_OPERATORS)), MAX_INT],
|
|
888
|
+
infixDefinition: [sequence(IDENTIFIER, oneOf(...INFIX_OPERATORS)), MAX_INT],
|
|
889
|
+
identifierDefinition: [sequence(IDENTIFIER, oneOf(LBR, DEF)), MAX_INT],
|
|
890
|
+
nextSubstitution: [
|
|
891
|
+
sequence(
|
|
892
|
+
COMMA,
|
|
893
|
+
oneOf(
|
|
894
|
+
IDENTIFIER,
|
|
895
|
+
...NON_EXPRESSION_PREFIX_OPERATORS,
|
|
896
|
+
...INFIX_OPERATORS,
|
|
897
|
+
...POSTFIX_OPERATORS,
|
|
898
|
+
),
|
|
899
|
+
SUBSTITUTE,
|
|
900
|
+
),
|
|
901
|
+
3,
|
|
902
|
+
],
|
|
903
|
+
assumptionName: [assumptionName, 2],
|
|
904
|
+
assumeExpression: [expression, 1],
|
|
905
|
+
nextAssumeExpression: [expression, 1],
|
|
906
|
+
newConstant: [newConstant, 2],
|
|
907
|
+
constantDeclaration: [constantDeclaration, 2],
|
|
908
|
+
constantSymbol: [constantSymbol, 2],
|
|
909
|
+
variableSymbol: [variableSymbol, 2],
|
|
910
|
+
levelSymbol: [levelSymbol, 2],
|
|
911
|
+
theoremName: [theoremName, 2],
|
|
912
|
+
theoremAssumeProve: [guard(isNextAssume, assumeProve), 3],
|
|
913
|
+
theoremExpression: [expression, 1],
|
|
914
|
+
proofKeyword: [proofKeyword, 2],
|
|
915
|
+
terminalProof: [sequence(optional(PROOF), oneOf(OBVIOUS, OMITTED)), 2],
|
|
916
|
+
stepProof: [
|
|
917
|
+
sequence(optional(proofKeyword), repeat(oneOf(...STEP_STARTS)), oneOf(...STEP_STARTS)),
|
|
918
|
+
1,
|
|
919
|
+
],
|
|
920
|
+
usedExpression: [expression, 1],
|
|
921
|
+
nextUsedExpression: [expression, 1],
|
|
922
|
+
usedFacts: [choice(MODULE, expression), 1],
|
|
923
|
+
definedExpression: [expression, 1],
|
|
924
|
+
nextDefinedExpression: [expression, 1],
|
|
925
|
+
assertStep: [assertStep, 1],
|
|
926
|
+
nextDefinition: [definition, 2],
|
|
927
|
+
takeBounds: [
|
|
928
|
+
choice(sequence(IDENTIFIER, repeat(sequence(COMMA, IDENTIFIER)), IN), LAB),
|
|
929
|
+
MAX_INT,
|
|
930
|
+
],
|
|
931
|
+
pickIdentifiers: [sequence(IDENTIFIER, repeat(sequence(COMMA, IDENTIFIER)), COLON), MAX_INT],
|
|
932
|
+
assertedExpression: [expression, 1],
|
|
933
|
+
decimalPart: [decimalPart, 2],
|
|
934
|
+
operatorArgument: [
|
|
935
|
+
sequence(
|
|
936
|
+
oneOf(...NON_EXPRESSION_PREFIX_OPERATORS, ...INFIX_OPERATORS, ...POSTFIX_OPERATORS),
|
|
937
|
+
oneOf(
|
|
938
|
+
COMMA,
|
|
939
|
+
RBR,
|
|
940
|
+
DEFBREAK,
|
|
941
|
+
LOCAL,
|
|
942
|
+
INSTANCE,
|
|
943
|
+
THEOREM,
|
|
944
|
+
ASSUME,
|
|
945
|
+
END_MODULE,
|
|
946
|
+
SEPARATOR,
|
|
947
|
+
_BM0,
|
|
948
|
+
ASSUMPTION,
|
|
949
|
+
CONSTANT,
|
|
950
|
+
VARIABLE,
|
|
951
|
+
RECURSIVE,
|
|
952
|
+
),
|
|
953
|
+
),
|
|
954
|
+
2,
|
|
955
|
+
],
|
|
956
|
+
lambdaArgument: [toScan(LAMBDA), MAX_INT],
|
|
957
|
+
expressionArgument: [expression, 1],
|
|
958
|
+
unboundQuantifier: [sequence(IDENTIFIER, repeat(sequence(COMMA, IDENTIFIER)), COLON), MAX_INT],
|
|
959
|
+
setEnumeration: [sequence(boundStart, COMMA), MAX_INT],
|
|
960
|
+
setOfAll: [sequence(boundStart, COLON), MAX_INT],
|
|
961
|
+
setExpression: [expression, 1],
|
|
962
|
+
setContents: [setContents, 1],
|
|
963
|
+
recordFields: [sequence(IDENTIFIER, MAPTO), MAX_INT],
|
|
964
|
+
recordSets: [sequence(IDENTIFIER, COLON), MAX_INT],
|
|
965
|
+
bracketExpression: [expression, 1],
|
|
966
|
+
tupleContents: [expression, 1],
|
|
967
|
+
identifierArguments: [operatorArguments, 2],
|
|
968
|
+
fairnessArguments: [sequence(LBR, expression), 2],
|
|
969
|
+
disjunctionList: [toScan(OR), MAX_INT],
|
|
970
|
+
conjunctionList: [toScan(AND), MAX_INT],
|
|
971
|
+
leadingOperator: [oneOf(...PREFIX_OPERATORS, MINUS), MAX_INT],
|
|
972
|
+
openExpression: [oneOf(...OPEN_STARTS), MAX_INT],
|
|
973
|
+
junctionList: [oneOf(AND, OR), MAX_INT],
|
|
974
|
+
primitiveExpression: [primitiveExpression, 1],
|
|
975
|
+
expressionSuffix: [expressionSuffix, 1],
|
|
976
|
+
postfixSuffix: [oneOf(...POSTFIX_OPERATORS), MAX_INT],
|
|
977
|
+
recordSuffix: [toScan(DOT), MAX_INT],
|
|
978
|
+
applicationSuffix: [toScan(LSB), MAX_INT],
|
|
979
|
+
nextLeadingOperator: [oneOf(...PREFIX_OPERATORS, MINUS), MAX_INT],
|
|
980
|
+
nextOpenExpression: [oneOf(...OPEN_STARTS), MAX_INT],
|
|
981
|
+
infixContinuation: [oneOf(...INFIX_OPERATORS), MAX_INT],
|
|
982
|
+
labelContinuation: [toScan(COLONCOLON), MAX_INT],
|
|
983
|
+
expressionContinuation: [expressionContinuation, 1],
|
|
984
|
+
negativeOperator: [toScan(NEGATIVE), MAX_INT],
|
|
985
|
+
primitiveArguments: [operatorArguments, 2],
|
|
986
|
+
stringLiteral: [toScan(STRING_LITERAL), MAX_INT],
|
|
987
|
+
numberLiteral: [toScan(NUMBER_LITERAL), MAX_INT],
|
|
988
|
+
bangIdentifier: [toScan(IDENTIFIER), MAX_INT],
|
|
989
|
+
bangArguments: [operatorArguments, 2],
|
|
990
|
+
bangOperand: [
|
|
991
|
+
choice(
|
|
992
|
+
guard(() => getToken(1).image !== "@", IDENTIFIER),
|
|
993
|
+
oneOf(...NON_EXPRESSION_PREFIX_OPERATORS),
|
|
994
|
+
oneOf(...INFIX_OPERATORS),
|
|
995
|
+
oneOf(...POSTFIX_OPERATORS),
|
|
996
|
+
),
|
|
997
|
+
1,
|
|
998
|
+
],
|
|
999
|
+
structureOperator: [
|
|
1000
|
+
choice(
|
|
1001
|
+
LAB,
|
|
1002
|
+
RAB,
|
|
1003
|
+
COLON,
|
|
1004
|
+
number,
|
|
1005
|
+
guard(() => getToken(1).image === "@", IDENTIFIER),
|
|
1006
|
+
),
|
|
1007
|
+
1,
|
|
1008
|
+
],
|
|
1009
|
+
} satisfies Record<string, [Scan, number]>;
|
|
1010
|
+
|
|
1011
|
+
const lookaheadNames = Object.keys(lookaheads);
|
|
1012
|
+
const lookaheadCalls: LookaheadCall[][] = lookaheadNames.map(() => [
|
|
1013
|
+
{ generation: 0, first: undefined, amount: 0 },
|
|
1014
|
+
]);
|
|
1015
|
+
|
|
1016
|
+
const runLookahead = (scan: Scan, start: SanyToken, amount: number) => {
|
|
1017
|
+
scanner.remaining = amount;
|
|
1018
|
+
scanner.scanPosition = start;
|
|
1019
|
+
scanner.lastPosition = start;
|
|
1020
|
+
scanner.isSucceeded = false;
|
|
1021
|
+
return scan();
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
// JavaCC's jj_2 call: a lookahead whose scan it records so a later error message can rescan it.
|
|
1025
|
+
const isLookahead = (name: keyof typeof lookaheads) => {
|
|
1026
|
+
const amount = lookaheads[name][1];
|
|
1027
|
+
const isMatched = runLookahead(lookaheads[name][0], parser.token, amount);
|
|
1028
|
+
const calls = lookaheadCalls[lookaheadNames.indexOf(name)];
|
|
1029
|
+
const call =
|
|
1030
|
+
calls.find((candidate) => candidate.generation <= parser.generation) ??
|
|
1031
|
+
calls[calls.push({ generation: 0, first: undefined, amount: 0 }) - 1];
|
|
1032
|
+
call.generation = parser.generation + amount - scanner.remaining;
|
|
1033
|
+
call.first = parser.token;
|
|
1034
|
+
call.amount = amount;
|
|
1035
|
+
return isMatched;
|
|
1036
|
+
};
|
|
1037
|
+
|
|
1038
|
+
// The images of count tokens from start, as a parse error shows them, stopping at <EOF>.
|
|
1039
|
+
const getImages = (start: SanyToken, count: number): string[] =>
|
|
1040
|
+
count === 0
|
|
1041
|
+
? []
|
|
1042
|
+
: start.kind === EOF
|
|
1043
|
+
? ["<EOF>"]
|
|
1044
|
+
: [start.image, ...getImages(getNext(start), count - 1)];
|
|
1045
|
+
|
|
1046
|
+
// JavaCC's generateParseException: the error shows as many tokens as the longest sequence the
|
|
1047
|
+
// lookaheads still pending at this token scanned.
|
|
1048
|
+
const getUnexpectedError = (kind = -1) => {
|
|
1049
|
+
scanner.longestExpected = kind >= 0 || parser.expectedGeneration === parser.generation ? 1 : 0;
|
|
1050
|
+
scanner.endPosition = 0;
|
|
1051
|
+
scanner.isRescanning = true;
|
|
1052
|
+
Object.values(lookaheads).forEach((lookahead, index) => {
|
|
1053
|
+
for (const call of lookaheadCalls[index]) {
|
|
1054
|
+
if (call.generation <= parser.generation || call.first === undefined) continue;
|
|
1055
|
+
runLookahead(lookahead[0], call.first, call.amount);
|
|
1056
|
+
if (scanner.isSucceeded) break;
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
scanner.isRescanning = false;
|
|
1060
|
+
addErrorToken(0);
|
|
1061
|
+
const next = getNext(parser.token);
|
|
1062
|
+
return getParseError(
|
|
1063
|
+
`Encountered "${getImages(next, scanner.longestExpected).join(" ")}" at line ${next.beginLine}, column ${next.beginColumn} and token "${escapeImage(parser.token.image)}" `,
|
|
1064
|
+
);
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
const getNextUnexpectedError = () => {
|
|
1068
|
+
getNext(parser.token);
|
|
1069
|
+
parser.nextKind = -1;
|
|
1070
|
+
return getUnexpectedError();
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
const consume = (kind: number) => {
|
|
1074
|
+
const previous = parser.token;
|
|
1075
|
+
parser.token = getNext(parser.token);
|
|
1076
|
+
parser.nextKind = -1;
|
|
1077
|
+
if (parser.token.kind === kind) {
|
|
1078
|
+
parser.generation++;
|
|
1079
|
+
return parser.token;
|
|
1080
|
+
}
|
|
1081
|
+
parser.token = previous;
|
|
1082
|
+
throw getUnexpectedError(kind);
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
const consumeOneOf = (kinds: readonly number[]) => {
|
|
1086
|
+
if (kinds.includes(getNextKind())) return consume(getNextKind());
|
|
1087
|
+
markExpected();
|
|
1088
|
+
throw getNextUnexpectedError();
|
|
1089
|
+
};
|
|
1090
|
+
|
|
1091
|
+
const getTokenNode = (current: SanyToken, kind: number | string = current.kind): SyntaxNode => ({
|
|
1092
|
+
kind,
|
|
1093
|
+
image: current.image,
|
|
1094
|
+
location: [current.beginLine, current.beginColumn, current.endLine, current.endColumn],
|
|
1095
|
+
heirs: [],
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
const getNode = (
|
|
1099
|
+
kind: string,
|
|
1100
|
+
...groups: (SyntaxNode | SyntaxNode[] | undefined)[]
|
|
1101
|
+
): SyntaxNode => {
|
|
1102
|
+
const heirs = groups.flatMap((group) => group ?? []);
|
|
1103
|
+
const location = [MAX_INT, MAX_INT, MIN_INT, MIN_INT];
|
|
1104
|
+
for (const heir of heirs) {
|
|
1105
|
+
if (
|
|
1106
|
+
heir.location[0] < location[0] ||
|
|
1107
|
+
(heir.location[0] === location[0] && heir.location[1] < location[1])
|
|
1108
|
+
) {
|
|
1109
|
+
location[0] = heir.location[0];
|
|
1110
|
+
location[1] = heir.location[1];
|
|
1111
|
+
}
|
|
1112
|
+
if (
|
|
1113
|
+
heir.location[2] > location[2] ||
|
|
1114
|
+
(heir.location[2] === location[2] && heir.location[3] > location[3])
|
|
1115
|
+
) {
|
|
1116
|
+
location[2] = heir.location[2];
|
|
1117
|
+
location[3] = heir.location[3];
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
return { kind, image: kind, location, heirs };
|
|
1121
|
+
};
|
|
1122
|
+
|
|
1123
|
+
const getIdPrefix = () => getNode("IdPrefix");
|
|
1124
|
+
|
|
1125
|
+
const addHeir = (node: SyntaxNode) => heirStack.push(node);
|
|
1126
|
+
|
|
1127
|
+
const getLastHeirs = () => {
|
|
1128
|
+
const start = heirStack.lastIndexOf(undefined) + 1;
|
|
1129
|
+
if (start === heirStack.length) return undefined;
|
|
1130
|
+
return heirStack.splice(start).filter((heir) => heir !== undefined);
|
|
1131
|
+
};
|
|
1132
|
+
|
|
1133
|
+
const beginProduction = (name: string) => {
|
|
1134
|
+
heirStack.push(undefined);
|
|
1135
|
+
frames.push({ name, token: getToken(1) });
|
|
1136
|
+
parser.expecting = "";
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
const endProduction = () => {
|
|
1140
|
+
frames.pop();
|
|
1141
|
+
heirStack.pop();
|
|
1142
|
+
parser.expecting = "";
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
const inProduction = <Result>(name: string, parse: () => Result) => {
|
|
1146
|
+
beginProduction(name);
|
|
1147
|
+
const result = parse();
|
|
1148
|
+
endProduction();
|
|
1149
|
+
return result;
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
// Runs a production that adds its heirs, and makes the node of the kind it returns.
|
|
1153
|
+
const parseProduction = (name: string, parseHeirs: () => string) =>
|
|
1154
|
+
inProduction(name, () => {
|
|
1155
|
+
const kind = parseHeirs();
|
|
1156
|
+
return getNode(kind, getLastHeirs());
|
|
1157
|
+
});
|
|
1158
|
+
|
|
1159
|
+
const convertFieldName = (current: SanyToken) => {
|
|
1160
|
+
if (isFieldNameToken(current)) current.kind = IDENTIFIER;
|
|
1161
|
+
};
|
|
1162
|
+
|
|
1163
|
+
// SANY's belchDEF: inserts a DEFBREAK token before the left-hand side of the next definition.
|
|
1164
|
+
const insertDefinitionBreak = () => {
|
|
1165
|
+
const examined = [parser.token, getNext(parser.token)];
|
|
1166
|
+
for (;;) {
|
|
1167
|
+
const current = examined[examined.length - 1];
|
|
1168
|
+
const nextToken = getNext(current);
|
|
1169
|
+
if (
|
|
1170
|
+
[EOF, THEOREM, PROPOSITION, ASSUMPTION, END_MODULE].includes(nextToken.kind) ||
|
|
1171
|
+
current.kind === THEOREM ||
|
|
1172
|
+
current.kind === PROPOSITION
|
|
1173
|
+
) {
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
if (current.kind === DEF) break;
|
|
1177
|
+
examined.push(nextToken);
|
|
1178
|
+
}
|
|
1179
|
+
const start = getDefinitionStart(examined, examined.length - 2);
|
|
1180
|
+
if (start <= 0 || examined[start - 1].kind === DEFBREAK) return;
|
|
1181
|
+
const target = examined[start];
|
|
1182
|
+
examined[start - 1].next = {
|
|
1183
|
+
kind: DEFBREAK,
|
|
1184
|
+
image: "Beginning of definition",
|
|
1185
|
+
beginLine: target.beginLine,
|
|
1186
|
+
beginColumn: target.beginColumn,
|
|
1187
|
+
endLine: target.endLine,
|
|
1188
|
+
endColumn: target.endColumn,
|
|
1189
|
+
next: target,
|
|
1190
|
+
};
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
const pushOperand = (node: SyntaxNode | undefined, operator?: Operator) =>
|
|
1194
|
+
parser.operatorStack?.push({ node, operator });
|
|
1195
|
+
|
|
1196
|
+
const getStack = () => parser.operatorStack ?? [];
|
|
1197
|
+
|
|
1198
|
+
const getElementNode = (index: number) => getStack()[index].node ?? getNode("Missing");
|
|
1199
|
+
|
|
1200
|
+
const reduceInfix = (operator: Operator) => {
|
|
1201
|
+
const stack = getStack();
|
|
1202
|
+
const last = stack.length - 1;
|
|
1203
|
+
if (last < 3) return;
|
|
1204
|
+
const operatorNode = getElementNode(last - 2);
|
|
1205
|
+
const left = getElementNode(last - 3);
|
|
1206
|
+
const right = getElementNode(last - 1);
|
|
1207
|
+
stack.splice(last - 2, 2);
|
|
1208
|
+
const node =
|
|
1209
|
+
operator.fix === "nfix" && left.kind === "Times"
|
|
1210
|
+
? getNode("Times", left.heirs, [operatorNode, right])
|
|
1211
|
+
: getNode(operator.fix === "nfix" ? "Times" : "InfixExpr", [left, operatorNode, right]);
|
|
1212
|
+
stack[last - 3] = { node, operator: undefined };
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
const reducePrefix = () => {
|
|
1216
|
+
const stack = getStack();
|
|
1217
|
+
const last = stack.length - 1;
|
|
1218
|
+
if (last < 2) return;
|
|
1219
|
+
const node = getNode("PrefixExpr", [getElementNode(last - 2), getElementNode(last - 1)]);
|
|
1220
|
+
stack.splice(last - 1, 1);
|
|
1221
|
+
stack[last - 2] = { node, operator: undefined };
|
|
1222
|
+
};
|
|
1223
|
+
|
|
1224
|
+
const reducePostfix = () => {
|
|
1225
|
+
const stack = getStack();
|
|
1226
|
+
const last = stack.length - 1;
|
|
1227
|
+
if (last < 2) return;
|
|
1228
|
+
const operatorNode = getElementNode(last - 1);
|
|
1229
|
+
const node =
|
|
1230
|
+
stack[last - 1].operator === FUNCTION_OPERATOR
|
|
1231
|
+
? getNode("FcnAppl", [getElementNode(last - 2)], operatorNode.heirs)
|
|
1232
|
+
: getNode("PostfixExpr", [getElementNode(last - 2), operatorNode]);
|
|
1233
|
+
stack.splice(last - 1, 1);
|
|
1234
|
+
stack[last - 2] = { node, operator: undefined };
|
|
1235
|
+
};
|
|
1236
|
+
|
|
1237
|
+
// SANY's OperatorStack.reduceStack: reduces the operators on top of the stack as precedence
|
|
1238
|
+
// allows, and rejects combinations whose precedence ranges conflict.
|
|
1239
|
+
const reduceStack = () => {
|
|
1240
|
+
const stack = getStack();
|
|
1241
|
+
for (;;) {
|
|
1242
|
+
const last = stack.length - 1;
|
|
1243
|
+
const right = stack[last];
|
|
1244
|
+
const rightOperator = right.operator;
|
|
1245
|
+
if (rightOperator === undefined) return;
|
|
1246
|
+
const rightLocation = formatLocation(right.node ?? getNode("Missing"));
|
|
1247
|
+
if (rightOperator.fix === "postfix") {
|
|
1248
|
+
if (last === 0) {
|
|
1249
|
+
throw getParseError(
|
|
1250
|
+
`\n Encountered postfix op ${rightOperator.name} in block ${rightLocation} on empty stack`,
|
|
1251
|
+
);
|
|
1252
|
+
}
|
|
1253
|
+
const below = stack[last - 1];
|
|
1254
|
+
if (below.operator !== undefined) {
|
|
1255
|
+
if (isInfix(below.operator) || below.operator.fix === "prefix") {
|
|
1256
|
+
throw getParseError(
|
|
1257
|
+
`\n Encountered postfix op ${rightOperator.name} in block ${rightLocation} following prefix or infix op ${below.operator.name}.`,
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
reducePostfix();
|
|
1261
|
+
} else {
|
|
1262
|
+
if (last <= 1) return;
|
|
1263
|
+
const leftOperator = stack[last - 2].operator;
|
|
1264
|
+
if (leftOperator === undefined) {
|
|
1265
|
+
throw getParseError(
|
|
1266
|
+
`Expression at location ${formatLocation(getElementNode(last - 2))} and expression at location ${formatLocation(getElementNode(last - 1))} follow each other without any intervening operator.`,
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
if (isSucceeding(leftOperator, rightOperator)) {
|
|
1270
|
+
if (isInfix(leftOperator)) reduceInfix(leftOperator);
|
|
1271
|
+
else reducePrefix();
|
|
1272
|
+
} else if (!isPreceding(leftOperator, rightOperator)) {
|
|
1273
|
+
throw getParseError(
|
|
1274
|
+
`Precedence conflict between ops ${leftOperator.name} in block ${rightLocation} and ${rightOperator.name}.`,
|
|
1275
|
+
);
|
|
1276
|
+
} else return;
|
|
1277
|
+
}
|
|
1278
|
+
} else if (rightOperator.fix === "prefix") {
|
|
1279
|
+
if (last === 0) return;
|
|
1280
|
+
const below = stack[last - 1];
|
|
1281
|
+
if (below.operator === undefined) {
|
|
1282
|
+
throw getParseError(
|
|
1283
|
+
`\n Encountered prefix op ${rightOperator.name} in block ${rightLocation} following an expression.`,
|
|
1284
|
+
);
|
|
1285
|
+
}
|
|
1286
|
+
if (below.operator.fix === "postfix") {
|
|
1287
|
+
throw getParseError(
|
|
1288
|
+
`\n Encountered prefix op ${rightOperator.name} in block ${rightLocation} following postfix op ${below.operator.name}.`,
|
|
1289
|
+
);
|
|
1290
|
+
}
|
|
1291
|
+
return;
|
|
1292
|
+
} else if (last === 0) {
|
|
1293
|
+
if (getMixfix(rightOperator) === undefined) {
|
|
1294
|
+
throw getParseError(
|
|
1295
|
+
`\n Encountered infix op ${rightOperator.name} in block ${rightLocation} on empty stack.`,
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1298
|
+
return;
|
|
1299
|
+
} else {
|
|
1300
|
+
const below = stack[last - 1];
|
|
1301
|
+
if (below.operator !== undefined) {
|
|
1302
|
+
const belowOperator = below.operator;
|
|
1303
|
+
if (isInfix(belowOperator) || belowOperator.fix === "prefix") {
|
|
1304
|
+
const mixfix = getMixfix(rightOperator);
|
|
1305
|
+
const belowLocation = formatLocation(getElementNode(last - 1));
|
|
1306
|
+
if (mixfix === undefined) {
|
|
1307
|
+
throw getParseError(
|
|
1308
|
+
rightOperator === VOID_OPERATOR
|
|
1309
|
+
? `\n Missing expression in block ${belowLocation} following prefix or infix op ${belowOperator.name}.`
|
|
1310
|
+
: `\n Encountered infix op ${rightOperator.name} in block ${belowLocation} following prefix or infix op ${belowOperator.name}.`,
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
if (
|
|
1314
|
+
isSucceeding(belowOperator, mixfix) ||
|
|
1315
|
+
(belowOperator === mixfix && belowOperator.associativity === "left")
|
|
1316
|
+
) {
|
|
1317
|
+
throw getParseError(
|
|
1318
|
+
`\n Precedence conflict between ops ${belowOperator.name} in block ${belowLocation} and ${mixfix.name}.`,
|
|
1319
|
+
);
|
|
1320
|
+
}
|
|
1321
|
+
} else reducePostfix();
|
|
1322
|
+
} else {
|
|
1323
|
+
if (last <= 1) return;
|
|
1324
|
+
const left = stack[last - 2];
|
|
1325
|
+
if (left.operator === undefined) {
|
|
1326
|
+
throw getParseError(
|
|
1327
|
+
`Expression at location ${formatLocation(getElementNode(last - 2))} and expression at location ${formatLocation(getElementNode(last - 1))} follow each other without any intervening operator.`,
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
const mixfix = getMixfix(left.operator);
|
|
1331
|
+
const leftOperator =
|
|
1332
|
+
mixfix !== undefined && (last === 2 || stack[last - 3].operator !== undefined)
|
|
1333
|
+
? mixfix
|
|
1334
|
+
: left.operator;
|
|
1335
|
+
const leftLocation = formatLocation(getElementNode(last - 2));
|
|
1336
|
+
if (
|
|
1337
|
+
isSucceeding(leftOperator, rightOperator) ||
|
|
1338
|
+
(leftOperator === rightOperator && leftOperator.associativity === "left")
|
|
1339
|
+
) {
|
|
1340
|
+
if (isInfix(leftOperator)) reduceInfix(leftOperator);
|
|
1341
|
+
else if (leftOperator.fix === "prefix") reducePrefix();
|
|
1342
|
+
else if (
|
|
1343
|
+
getElementNode(last - 2).location[0] < getElementNode(last - 1).location[0] &&
|
|
1344
|
+
rightOperator.name === "="
|
|
1345
|
+
) {
|
|
1346
|
+
throw getParseError(
|
|
1347
|
+
`\n *** Hint *** You may have mistyped ==\n Illegal combination of operators ${leftOperator.name} in block ${leftLocation} and ${rightOperator.name}.`,
|
|
1348
|
+
);
|
|
1349
|
+
} else if (rightOperator === VOID_OPERATOR) {
|
|
1350
|
+
throw getParseError(
|
|
1351
|
+
`\n Error following expression at ${leftLocation}, missing operator or separator.`,
|
|
1352
|
+
);
|
|
1353
|
+
} else {
|
|
1354
|
+
throw getParseError(
|
|
1355
|
+
`\n Illegal combination of operators ${leftOperator.name} in block ${leftLocation} and ${rightOperator.name}.`,
|
|
1356
|
+
);
|
|
1357
|
+
}
|
|
1358
|
+
} else if (
|
|
1359
|
+
!(
|
|
1360
|
+
isPreceding(leftOperator, rightOperator) ||
|
|
1361
|
+
(leftOperator === rightOperator && leftOperator.associativity === "right")
|
|
1362
|
+
)
|
|
1363
|
+
) {
|
|
1364
|
+
throw getParseError(
|
|
1365
|
+
`\n Precedence conflict between ops ${leftOperator.name} in block ${leftLocation} and ${rightOperator.name}.`,
|
|
1366
|
+
);
|
|
1367
|
+
} else return;
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
if (last === stack.length - 1) return;
|
|
1371
|
+
}
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
const finalReduce = () => {
|
|
1375
|
+
pushOperand(undefined, VOID_OPERATOR);
|
|
1376
|
+
reduceStack();
|
|
1377
|
+
const stack = getStack();
|
|
1378
|
+
if (stack.length === 2) return stack[0].node;
|
|
1379
|
+
errors.push(
|
|
1380
|
+
`Couldn't properly parse expression${stack
|
|
1381
|
+
.slice(0, Math.max(stack.length - 1, 1))
|
|
1382
|
+
.map(
|
|
1383
|
+
(_, index) => `-- incomplete expression at ${formatLocation(getElementNode(index))}.\n`,
|
|
1384
|
+
)
|
|
1385
|
+
.join("")}`,
|
|
1386
|
+
);
|
|
1387
|
+
return undefined;
|
|
1388
|
+
};
|
|
1389
|
+
|
|
1390
|
+
const reduceRecord = (middle: SyntaxNode, right: SyntaxNode) => {
|
|
1391
|
+
const stack = getStack();
|
|
1392
|
+
const element = stack.at(-1);
|
|
1393
|
+
if (element === undefined)
|
|
1394
|
+
throw getParseError(`\n \`\`.'' has no left hand side at ${formatLocation(middle)}.`);
|
|
1395
|
+
if (element.operator !== undefined) {
|
|
1396
|
+
if (element.operator.fix !== "postfix" || stack[stack.length - 2].operator !== undefined) {
|
|
1397
|
+
throw getParseError(
|
|
1398
|
+
`\n \`\`.'' follows operator ${formatLocation(element.node ?? getNode("Missing"))}.`,
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
stack.push({ node: undefined, operator: undefined });
|
|
1402
|
+
reducePostfix();
|
|
1403
|
+
stack.pop();
|
|
1404
|
+
}
|
|
1405
|
+
const index = stack.length - 1;
|
|
1406
|
+
stack[index] = {
|
|
1407
|
+
node: getNode("RecordComponent", [getElementNode(index), middle, right]),
|
|
1408
|
+
operator: undefined,
|
|
1409
|
+
};
|
|
1410
|
+
};
|
|
1411
|
+
|
|
1412
|
+
// A call, so TypeScript does not narrow the hook to undefined across the parse that sets it.
|
|
1413
|
+
const setFairnessHook = (node: SyntaxNode | undefined) => {
|
|
1414
|
+
parser.fairnessHook = node;
|
|
1415
|
+
};
|
|
1416
|
+
|
|
1417
|
+
const isLabel = (node: SyntaxNode | undefined) => {
|
|
1418
|
+
if (node === undefined) return false;
|
|
1419
|
+
if (node.kind === "GeneralId") return node.heirs[0].heirs.length === 0;
|
|
1420
|
+
if (node.kind !== "OpApplication") return false;
|
|
1421
|
+
return node.heirs[1].heirs
|
|
1422
|
+
.filter((_, index) => index % 2 === 1)
|
|
1423
|
+
.every((argument) => argument.kind === "GeneralId" && argument.heirs[0].heirs.length === 0);
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
const checkIndentation = (node: SyntaxNode, junction: SyntaxNode) => {
|
|
1427
|
+
for (const child of node.heirs) {
|
|
1428
|
+
if (child.kind === "ConjList" || child.kind === "DisjList") continue;
|
|
1429
|
+
if (!isAboveJunction(child.location[1])) {
|
|
1430
|
+
throw getParseError(
|
|
1431
|
+
`Item at ${formatLocation(child)} is not properly indented inside conjunction or disjunction list item at ${formatLocation(junction)}`,
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
checkIndentation(child, junction);
|
|
1435
|
+
}
|
|
1436
|
+
};
|
|
1437
|
+
|
|
1438
|
+
const getProofLevel = () =>
|
|
1439
|
+
parser.proofDepth < 0 ? parser.proofDepth : proofLevels[parser.proofDepth];
|
|
1440
|
+
|
|
1441
|
+
const isProofStart = (current: SanyToken) => {
|
|
1442
|
+
if (current.image.length < 2) return false;
|
|
1443
|
+
if (current.image[1] === "*") return parser.proofDepth < 0;
|
|
1444
|
+
if (current.image[1] === "+") return true;
|
|
1445
|
+
if ([PROOF_STEP, 293, 294, 292].includes(current.kind)) {
|
|
1446
|
+
if (parser.proofDepth < 0) return true;
|
|
1447
|
+
return (
|
|
1448
|
+
proofLevels[parser.proofDepth] >= 0 &&
|
|
1449
|
+
getLevelOfStep(current) > proofLevels[parser.proofDepth]
|
|
1450
|
+
);
|
|
1451
|
+
}
|
|
1452
|
+
return [BY, PROOF, OBVIOUS, OMITTED].includes(current.kind);
|
|
1453
|
+
};
|
|
1454
|
+
|
|
1455
|
+
const isCorrectLevel = (current: SanyToken) => {
|
|
1456
|
+
const level = getLevelOfStep(current);
|
|
1457
|
+
const lastLevel = parser.proofDepth > 0 ? proofLevels[parser.proofDepth - 1] : -1;
|
|
1458
|
+
if (level === -1) {
|
|
1459
|
+
if (proofLevels[parser.proofDepth] < 0) proofLevels[parser.proofDepth] = lastLevel + 1;
|
|
1460
|
+
return true;
|
|
1461
|
+
}
|
|
1462
|
+
if (proofLevels[parser.proofDepth] >= 0)
|
|
1463
|
+
return level === -2 ? false : proofLevels[parser.proofDepth] === level;
|
|
1464
|
+
proofLevels[parser.proofDepth] = level === -2 ? lastLevel + 1 : level;
|
|
1465
|
+
return level === -2 || level > lastLevel;
|
|
1466
|
+
};
|
|
1467
|
+
|
|
1468
|
+
const parseIdentifier = () => getTokenNode(consume(IDENTIFIER));
|
|
1469
|
+
|
|
1470
|
+
const addToken = (kind: number) => addHeir(getTokenNode(consume(kind)));
|
|
1471
|
+
|
|
1472
|
+
const parsePrefixOperator = (kinds: readonly number[], kind: string) => {
|
|
1473
|
+
const current = consumeOneOf(kinds);
|
|
1474
|
+
parser.lastOperator = OPERATORS.get(current.image);
|
|
1475
|
+
return getTokenNode(current, kind);
|
|
1476
|
+
};
|
|
1477
|
+
|
|
1478
|
+
const parseInfixOperator = () => {
|
|
1479
|
+
beginProduction("Infix Op");
|
|
1480
|
+
const current = consumeOneOf(INFIX_OPERATORS);
|
|
1481
|
+
parser.lastOperator = OPERATORS.get(current.image);
|
|
1482
|
+
endProduction();
|
|
1483
|
+
return getTokenNode(current, "InfixOp");
|
|
1484
|
+
};
|
|
1485
|
+
|
|
1486
|
+
const parsePostfixOperator = () => {
|
|
1487
|
+
const current = consumeOneOf(POSTFIX_OPERATORS);
|
|
1488
|
+
parser.lastOperator = OPERATORS.get(current.image);
|
|
1489
|
+
return getTokenNode(current, "PostfixOp");
|
|
1490
|
+
};
|
|
1491
|
+
|
|
1492
|
+
const parseNonExpressionPrefixOperator = () =>
|
|
1493
|
+
parsePrefixOperator(NON_EXPRESSION_PREFIX_OPERATORS, "NonExpPrefixOp");
|
|
1494
|
+
|
|
1495
|
+
const parseModuleDefinition = (): SyntaxNode => {
|
|
1496
|
+
beginProduction("Module definition");
|
|
1497
|
+
parser.expecting = "---- MODULE";
|
|
1498
|
+
const begin = parseBeginModule();
|
|
1499
|
+
parser.expecting = "EXTENDS clause or module body";
|
|
1500
|
+
const extension = parseExtends();
|
|
1501
|
+
parser.expecting = "Module body";
|
|
1502
|
+
const body = parseBody();
|
|
1503
|
+
parser.expecting = "==== or more Module body";
|
|
1504
|
+
const end = getNode("EndModule", [getTokenNode(consume(END_MODULE))]);
|
|
1505
|
+
endProduction();
|
|
1506
|
+
return getNode("Module", [begin, extension, body, end]);
|
|
1507
|
+
};
|
|
1508
|
+
|
|
1509
|
+
const parseBeginModule = () => {
|
|
1510
|
+
beginProduction("Begin module");
|
|
1511
|
+
parser.expecting = "---- MODULE (beginning of module)";
|
|
1512
|
+
const begin = getTokenNode(consumeOneOf([_BM0, _BM1, _BM2]));
|
|
1513
|
+
parser.expecting = "Identifier";
|
|
1514
|
+
convertFieldName(getToken(1));
|
|
1515
|
+
const name = parseIdentifier();
|
|
1516
|
+
parser.moduleName ??= name.image;
|
|
1517
|
+
parser.expecting = "----";
|
|
1518
|
+
const separator = getTokenNode(consume(SEPARATOR));
|
|
1519
|
+
endProduction();
|
|
1520
|
+
return getNode("BeginModule", [begin, name, separator]);
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1523
|
+
const parseExtends = () => {
|
|
1524
|
+
beginProduction("Extends");
|
|
1525
|
+
if (isNextKind(EXTENDS)) {
|
|
1526
|
+
addToken(EXTENDS);
|
|
1527
|
+
parser.expecting = "Identifier";
|
|
1528
|
+
convertFieldName(getToken(1));
|
|
1529
|
+
addHeir(parseIdentifier());
|
|
1530
|
+
parser.expecting = "comma or module body";
|
|
1531
|
+
while (isNextKind(COMMA)) {
|
|
1532
|
+
addToken(COMMA);
|
|
1533
|
+
parser.expecting = "Identifier";
|
|
1534
|
+
convertFieldName(getToken(1));
|
|
1535
|
+
addHeir(parseIdentifier());
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
const heirs = getLastHeirs();
|
|
1539
|
+
endProduction();
|
|
1540
|
+
return getNode("Extends", heirs);
|
|
1541
|
+
};
|
|
1542
|
+
|
|
1543
|
+
const parseBodyUnit = (): SyntaxNode => {
|
|
1544
|
+
const kind = getNextKind();
|
|
1545
|
+
if (kind === SEPARATOR) return getTokenNode(consume(SEPARATOR));
|
|
1546
|
+
if (kind === VARIABLE) return parseVariableDeclaration();
|
|
1547
|
+
if (kind === CONSTANT) return parseParameterDeclaration();
|
|
1548
|
+
markExpected();
|
|
1549
|
+
if (isLookahead("unitDefinition")) return parseDefinition();
|
|
1550
|
+
if (getNextKind() === RECURSIVE) return parseRecursive();
|
|
1551
|
+
markExpected();
|
|
1552
|
+
if (isLookahead("unitInstance")) return parseInstance();
|
|
1553
|
+
if (isLookahead("unitAssumption")) return parseAssumption();
|
|
1554
|
+
if (isLookahead("unitTheorem")) return parseTheorem();
|
|
1555
|
+
if ([_BM0, _BM1, _BM2].includes(getNextKind())) {
|
|
1556
|
+
const module = parseModuleDefinition();
|
|
1557
|
+
insertDefinitionBreak();
|
|
1558
|
+
return module;
|
|
1559
|
+
}
|
|
1560
|
+
markExpected();
|
|
1561
|
+
if ((getToken(1).kind === USE && getToken(2).kind !== ONLY) || getToken(1).kind === HIDE) {
|
|
1562
|
+
return parseUseOrHideOrBy();
|
|
1563
|
+
}
|
|
1564
|
+
throw getNextUnexpectedError();
|
|
1565
|
+
};
|
|
1566
|
+
|
|
1567
|
+
const parseBody = () => {
|
|
1568
|
+
beginProduction("Module body");
|
|
1569
|
+
parser.expecting =
|
|
1570
|
+
"LOCAL, INSTANCE, PROOF, ASSUMPTION, THEOREM, RECURSIVE, declaration, or definition";
|
|
1571
|
+
while (isLookahead("moduleUnit")) addHeir(parseBodyUnit());
|
|
1572
|
+
const heirs = getLastHeirs();
|
|
1573
|
+
endProduction();
|
|
1574
|
+
return getNode("Body", heirs);
|
|
1575
|
+
};
|
|
1576
|
+
|
|
1577
|
+
const parseVariableDeclaration = () => {
|
|
1578
|
+
beginProduction("variable declaration");
|
|
1579
|
+
const keyword = getTokenNode(consume(VARIABLE));
|
|
1580
|
+
parser.expecting = "Identifier";
|
|
1581
|
+
addHeir(parseIdentifier());
|
|
1582
|
+
parser.expecting = "comma or module body";
|
|
1583
|
+
while (isNextKind(COMMA)) {
|
|
1584
|
+
addToken(COMMA);
|
|
1585
|
+
parser.expecting = "Identifier";
|
|
1586
|
+
addHeir(parseIdentifier());
|
|
1587
|
+
}
|
|
1588
|
+
const heirs = getLastHeirs();
|
|
1589
|
+
endProduction();
|
|
1590
|
+
return getNode("VariableDeclaration", keyword, heirs);
|
|
1591
|
+
};
|
|
1592
|
+
|
|
1593
|
+
const parseParameterDeclaration = () => {
|
|
1594
|
+
beginProduction("Parameter declaration");
|
|
1595
|
+
parser.expecting = "CONSTANT";
|
|
1596
|
+
beginProduction("Parameter declaration item");
|
|
1597
|
+
const keyword = getNode("ConsDecl", [getTokenNode(consume(CONSTANT))]);
|
|
1598
|
+
endProduction();
|
|
1599
|
+
addHeir(keyword);
|
|
1600
|
+
parser.expecting = "Identifier, operator or _";
|
|
1601
|
+
addHeir(parseConstantDeclarationItems());
|
|
1602
|
+
parser.expecting = ",";
|
|
1603
|
+
while (isNextKind(COMMA)) {
|
|
1604
|
+
addToken(COMMA);
|
|
1605
|
+
parser.expecting = "Identifier, operator or _";
|
|
1606
|
+
addHeir(parseConstantDeclarationItems());
|
|
1607
|
+
}
|
|
1608
|
+
const heirs = getLastHeirs();
|
|
1609
|
+
endProduction();
|
|
1610
|
+
return getNode("ParamDeclaration", heirs);
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1613
|
+
const parseRecursive = () => {
|
|
1614
|
+
beginProduction("Recursive");
|
|
1615
|
+
parser.expecting = "RECURSIVE";
|
|
1616
|
+
addToken(RECURSIVE);
|
|
1617
|
+
parser.expecting = "Identifier, operator or _";
|
|
1618
|
+
addHeir(parseConstantDeclarationItems());
|
|
1619
|
+
parser.expecting = ",";
|
|
1620
|
+
while (isNextKind(COMMA)) {
|
|
1621
|
+
addToken(COMMA);
|
|
1622
|
+
parser.expecting = "Identifier, operator or _";
|
|
1623
|
+
addHeir(parseConstantDeclarationItems());
|
|
1624
|
+
parser.expecting = "`,' or `)'";
|
|
1625
|
+
}
|
|
1626
|
+
const heirs = getLastHeirs();
|
|
1627
|
+
endProduction();
|
|
1628
|
+
return getNode("Recursive", heirs);
|
|
1629
|
+
};
|
|
1630
|
+
|
|
1631
|
+
const parseUnderscores = () => {
|
|
1632
|
+
addToken(LBR);
|
|
1633
|
+
parser.expecting = "_";
|
|
1634
|
+
addToken(US);
|
|
1635
|
+
parser.expecting = "comma or )";
|
|
1636
|
+
while (isNextKind(COMMA)) {
|
|
1637
|
+
addToken(COMMA);
|
|
1638
|
+
parser.expecting = "_";
|
|
1639
|
+
addToken(US);
|
|
1640
|
+
parser.expecting = "comma or )";
|
|
1641
|
+
}
|
|
1642
|
+
addToken(RBR);
|
|
1643
|
+
};
|
|
1644
|
+
|
|
1645
|
+
const parseConstantDeclarationItems = () =>
|
|
1646
|
+
parseProduction("Constant declaration items", () => {
|
|
1647
|
+
parser.expecting = "Identifier, _ or prefix op";
|
|
1648
|
+
const kind = getNextKind();
|
|
1649
|
+
if (kind === IDENTIFIER) {
|
|
1650
|
+
addHeir(parseIdentifier());
|
|
1651
|
+
parser.expecting = "(, comma, or Module Body";
|
|
1652
|
+
if (isLookahead("constantArguments")) parseUnderscores();
|
|
1653
|
+
return "IdentDecl";
|
|
1654
|
+
}
|
|
1655
|
+
if (NON_EXPRESSION_PREFIX_OPERATORS.includes(kind)) {
|
|
1656
|
+
const operator = parseNonExpressionPrefixOperator();
|
|
1657
|
+
parser.expecting = "_";
|
|
1658
|
+
addHeir(operator);
|
|
1659
|
+
addToken(US);
|
|
1660
|
+
return "PrefixDecl";
|
|
1661
|
+
}
|
|
1662
|
+
if (kind !== US) {
|
|
1663
|
+
markExpected();
|
|
1664
|
+
throw getNextUnexpectedError();
|
|
1665
|
+
}
|
|
1666
|
+
const underscore = getTokenNode(consume(US));
|
|
1667
|
+
parser.expecting = "prefix or postfix operator";
|
|
1668
|
+
const operatorKind = getNextKind();
|
|
1669
|
+
if (INFIX_OPERATORS.includes(operatorKind)) {
|
|
1670
|
+
const operator = parseInfixOperator();
|
|
1671
|
+
parser.expecting = "_";
|
|
1672
|
+
addHeir(underscore);
|
|
1673
|
+
addHeir(operator);
|
|
1674
|
+
addToken(US);
|
|
1675
|
+
return "InfixDecl";
|
|
1676
|
+
}
|
|
1677
|
+
if (POSTFIX_OPERATORS.includes(operatorKind)) {
|
|
1678
|
+
const operator = parsePostfixOperator();
|
|
1679
|
+
addHeir(underscore);
|
|
1680
|
+
addHeir(operator);
|
|
1681
|
+
return "PostfixDecl";
|
|
1682
|
+
}
|
|
1683
|
+
markExpected();
|
|
1684
|
+
throw getNextUnexpectedError();
|
|
1685
|
+
});
|
|
1686
|
+
|
|
1687
|
+
const parseDefinitionBody = (kind: string) => {
|
|
1688
|
+
const definition = getTokenNode(consume(DEF));
|
|
1689
|
+
insertDefinitionBreak();
|
|
1690
|
+
addHeir(definition);
|
|
1691
|
+
parser.expecting = kind;
|
|
1692
|
+
};
|
|
1693
|
+
|
|
1694
|
+
const parseDefinition = () =>
|
|
1695
|
+
parseProduction("Definition", () => {
|
|
1696
|
+
parser.expecting = "LOCAL, Identifier or Operator Symbol";
|
|
1697
|
+
if (isNextKind(LOCAL)) addToken(LOCAL);
|
|
1698
|
+
consume(DEFBREAK);
|
|
1699
|
+
parser.expecting = "LOCAL, Identifier or Operator Symbol";
|
|
1700
|
+
if (isLookahead("functionDefinition")) {
|
|
1701
|
+
addHeir(parseIdentifier());
|
|
1702
|
+
parser.expecting = "[";
|
|
1703
|
+
addToken(LSB);
|
|
1704
|
+
parser.expecting = "Identifier";
|
|
1705
|
+
addHeir(parseQuantifierBound());
|
|
1706
|
+
parser.expecting = "COMMA or ]";
|
|
1707
|
+
while (isNextKind(COMMA)) {
|
|
1708
|
+
addToken(COMMA);
|
|
1709
|
+
parser.expecting = "Identifier";
|
|
1710
|
+
addHeir(parseQuantifierBound());
|
|
1711
|
+
}
|
|
1712
|
+
const bracket = getTokenNode(consume(RSB));
|
|
1713
|
+
parser.expecting = "==";
|
|
1714
|
+
addHeir(bracket);
|
|
1715
|
+
parseDefinitionBody("Expression");
|
|
1716
|
+
addHeir(parseExpression());
|
|
1717
|
+
return "FunctionDefinition";
|
|
1718
|
+
}
|
|
1719
|
+
if (isLookahead("postfixDefinition")) {
|
|
1720
|
+
beginProduction("Postfix LHS");
|
|
1721
|
+
const name = parseIdentifier();
|
|
1722
|
+
const operator = getTokenNode(consumeOneOf(POSTFIX_OPERATORS));
|
|
1723
|
+
endProduction();
|
|
1724
|
+
addHeir(getNode("PostfixLHS", [name, operator]));
|
|
1725
|
+
parser.expecting = "==";
|
|
1726
|
+
parseDefinitionBody("Expression");
|
|
1727
|
+
addHeir(parseExpression());
|
|
1728
|
+
return "OperatorDefinition";
|
|
1729
|
+
}
|
|
1730
|
+
if (isLookahead("infixDefinition")) {
|
|
1731
|
+
beginProduction("Infix LHS");
|
|
1732
|
+
const left = parseIdentifier();
|
|
1733
|
+
const operator = getTokenNode(consumeOneOf(INFIX_OPERATORS));
|
|
1734
|
+
const right = parseIdentifier();
|
|
1735
|
+
endProduction();
|
|
1736
|
+
addHeir(getNode("InfixLHS", [left, operator, right]));
|
|
1737
|
+
parser.expecting = "==";
|
|
1738
|
+
parseDefinitionBody("Expression");
|
|
1739
|
+
addHeir(parseExpression());
|
|
1740
|
+
return "OperatorDefinition";
|
|
1741
|
+
}
|
|
1742
|
+
if (isLookahead("identifierDefinition")) {
|
|
1743
|
+
addHeir(parseIdentifierLeftSide());
|
|
1744
|
+
parser.expecting = "==";
|
|
1745
|
+
parseDefinitionBody("Expression or Instance");
|
|
1746
|
+
if (isLookahead("definitionBody")) {
|
|
1747
|
+
addHeir(parseExpression());
|
|
1748
|
+
return "OperatorDefinition";
|
|
1749
|
+
}
|
|
1750
|
+
if (getNextKind() === INSTANCE) {
|
|
1751
|
+
addHeir(parseInstantiation());
|
|
1752
|
+
return "ModuleDefinition";
|
|
1753
|
+
}
|
|
1754
|
+
markExpected();
|
|
1755
|
+
throw getNextUnexpectedError();
|
|
1756
|
+
}
|
|
1757
|
+
if (!NON_EXPRESSION_PREFIX_OPERATORS.includes(getNextKind())) {
|
|
1758
|
+
markExpected();
|
|
1759
|
+
throw getNextUnexpectedError();
|
|
1760
|
+
}
|
|
1761
|
+
beginProduction("Prefix LHS");
|
|
1762
|
+
const operator = getTokenNode(consumeOneOf(NON_EXPRESSION_PREFIX_OPERATORS));
|
|
1763
|
+
parser.expecting = "Identifier";
|
|
1764
|
+
const name = parseIdentifier();
|
|
1765
|
+
endProduction();
|
|
1766
|
+
addHeir(getNode("PrefixLHS", [operator, name]));
|
|
1767
|
+
parser.expecting = "==";
|
|
1768
|
+
parseDefinitionBody("Expression");
|
|
1769
|
+
addHeir(parseExpression());
|
|
1770
|
+
return "OperatorDefinition";
|
|
1771
|
+
});
|
|
1772
|
+
|
|
1773
|
+
const parseIdentifierTuple = () => {
|
|
1774
|
+
beginProduction("Identifier tuple");
|
|
1775
|
+
addToken(LAB);
|
|
1776
|
+
parser.expecting = "Identifier or >>";
|
|
1777
|
+
if (isNextKind(IDENTIFIER)) {
|
|
1778
|
+
addHeir(parseIdentifier());
|
|
1779
|
+
parser.expecting = "COMMA or >>";
|
|
1780
|
+
while (isNextKind(COMMA)) {
|
|
1781
|
+
addToken(COMMA);
|
|
1782
|
+
parser.expecting = "COMMA or >>";
|
|
1783
|
+
addHeir(parseIdentifier());
|
|
1784
|
+
parser.expecting = "COMMA or >>";
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
addToken(RAB);
|
|
1788
|
+
const heirs = getLastHeirs();
|
|
1789
|
+
endProduction();
|
|
1790
|
+
return getNode("IdentifierTuple", heirs);
|
|
1791
|
+
};
|
|
1792
|
+
|
|
1793
|
+
const parseDeclaration = () => {
|
|
1794
|
+
const kind = getNextKind();
|
|
1795
|
+
if (kind === IDENTIFIER) return parseIdentifierDeclaration();
|
|
1796
|
+
if (OPERATOR_DECLARATION_STARTS.has(kind)) return parseOperatorDeclaration();
|
|
1797
|
+
markExpected();
|
|
1798
|
+
throw getNextUnexpectedError();
|
|
1799
|
+
};
|
|
1800
|
+
|
|
1801
|
+
const parseIdentifierLeftSide = () => {
|
|
1802
|
+
beginProduction("Identifier LHS");
|
|
1803
|
+
addHeir(parseIdentifier());
|
|
1804
|
+
parser.expecting = "( or ==";
|
|
1805
|
+
if (isNextKind(LBR)) {
|
|
1806
|
+
addToken(LBR);
|
|
1807
|
+
parser.expecting = "Identifier Declaration, prefix op, _ or )";
|
|
1808
|
+
addHeir(parseDeclaration());
|
|
1809
|
+
parser.expecting = "COMMA or )";
|
|
1810
|
+
while (isNextKind(COMMA)) {
|
|
1811
|
+
addToken(COMMA);
|
|
1812
|
+
parser.expecting = "Identifier Declaration, prefix op or _";
|
|
1813
|
+
addHeir(parseDeclaration());
|
|
1814
|
+
parser.expecting = "COMMA or )";
|
|
1815
|
+
}
|
|
1816
|
+
addToken(RBR);
|
|
1817
|
+
}
|
|
1818
|
+
const heirs = getLastHeirs();
|
|
1819
|
+
endProduction();
|
|
1820
|
+
return getNode("IdentLHS", heirs);
|
|
1821
|
+
};
|
|
1822
|
+
|
|
1823
|
+
const parseIdentifierDeclaration = () => {
|
|
1824
|
+
beginProduction("Identifier Declation");
|
|
1825
|
+
addHeir(parseIdentifier());
|
|
1826
|
+
parser.expecting = "( or ...";
|
|
1827
|
+
if (isNextKind(LBR)) {
|
|
1828
|
+
addToken(LBR);
|
|
1829
|
+
parser.expecting = "_";
|
|
1830
|
+
addToken(US);
|
|
1831
|
+
parser.expecting = "COMMA or )";
|
|
1832
|
+
while (isNextKind(COMMA)) {
|
|
1833
|
+
addToken(COMMA);
|
|
1834
|
+
parser.expecting = "_";
|
|
1835
|
+
addToken(US);
|
|
1836
|
+
parser.expecting = "COMMA or )";
|
|
1837
|
+
}
|
|
1838
|
+
addToken(RBR);
|
|
1839
|
+
}
|
|
1840
|
+
const heirs = getLastHeirs();
|
|
1841
|
+
endProduction();
|
|
1842
|
+
return getNode("IdentDecl", heirs);
|
|
1843
|
+
};
|
|
1844
|
+
|
|
1845
|
+
const parseOperatorDeclaration = () =>
|
|
1846
|
+
parseProduction("Op. Symbol Declaration", () => {
|
|
1847
|
+
const kind = getNextKind();
|
|
1848
|
+
if (NON_EXPRESSION_PREFIX_OPERATORS.includes(kind)) {
|
|
1849
|
+
addHeir(parseNonExpressionPrefixOperator());
|
|
1850
|
+
parser.expecting = "_";
|
|
1851
|
+
addToken(US);
|
|
1852
|
+
return "PrefixDecl";
|
|
1853
|
+
}
|
|
1854
|
+
if (kind !== US) {
|
|
1855
|
+
markExpected();
|
|
1856
|
+
throw getNextUnexpectedError();
|
|
1857
|
+
}
|
|
1858
|
+
addToken(US);
|
|
1859
|
+
parser.expecting = "infix or postfix operator";
|
|
1860
|
+
const operatorKind = getNextKind();
|
|
1861
|
+
if (INFIX_OPERATORS.includes(operatorKind)) {
|
|
1862
|
+
addHeir(parseInfixOperator());
|
|
1863
|
+
parser.expecting = "_";
|
|
1864
|
+
addToken(US);
|
|
1865
|
+
return "InfixDecl";
|
|
1866
|
+
}
|
|
1867
|
+
if (POSTFIX_OPERATORS.includes(operatorKind)) {
|
|
1868
|
+
addHeir(parsePostfixOperator());
|
|
1869
|
+
return "PostfixDecl";
|
|
1870
|
+
}
|
|
1871
|
+
markExpected();
|
|
1872
|
+
throw getNextUnexpectedError();
|
|
1873
|
+
});
|
|
1874
|
+
|
|
1875
|
+
const parseInstance = () => {
|
|
1876
|
+
beginProduction("Instance");
|
|
1877
|
+
parser.expecting = "LOCAL or instance";
|
|
1878
|
+
const local = isNextKind(LOCAL) ? getTokenNode(consume(LOCAL)) : undefined;
|
|
1879
|
+
addHeir(parseInstantiation());
|
|
1880
|
+
parser.expecting = "COMMA or Module Body";
|
|
1881
|
+
const heirs = getLastHeirs();
|
|
1882
|
+
endProduction();
|
|
1883
|
+
return getNode("Instance", local, heirs);
|
|
1884
|
+
};
|
|
1885
|
+
|
|
1886
|
+
const parseInstantiation = () => {
|
|
1887
|
+
beginProduction("NonLocalInstance");
|
|
1888
|
+
addToken(INSTANCE);
|
|
1889
|
+
parser.expecting = "Module identifier";
|
|
1890
|
+
convertFieldName(getToken(1));
|
|
1891
|
+
addHeir(parseIdentifier());
|
|
1892
|
+
parser.expecting = "WITH or another definition.";
|
|
1893
|
+
if (isNextKind(WITH)) {
|
|
1894
|
+
addToken(WITH);
|
|
1895
|
+
parser.expecting = "";
|
|
1896
|
+
addHeir(parseSubstitution());
|
|
1897
|
+
parser.expecting = "";
|
|
1898
|
+
while (isLookahead("nextSubstitution")) {
|
|
1899
|
+
addToken(COMMA);
|
|
1900
|
+
parser.expecting = "";
|
|
1901
|
+
addHeir(parseSubstitution());
|
|
1902
|
+
parser.expecting = "";
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
const heirs = getLastHeirs();
|
|
1906
|
+
endProduction();
|
|
1907
|
+
return getNode("NonLocalInstance", heirs);
|
|
1908
|
+
};
|
|
1909
|
+
|
|
1910
|
+
const parseSubstitution = () => {
|
|
1911
|
+
beginProduction("Substitution");
|
|
1912
|
+
const kind = getNextKind();
|
|
1913
|
+
const target =
|
|
1914
|
+
kind === IDENTIFIER
|
|
1915
|
+
? parseIdentifier()
|
|
1916
|
+
: NON_EXPRESSION_PREFIX_OPERATORS.includes(kind)
|
|
1917
|
+
? parseNonExpressionPrefixOperator()
|
|
1918
|
+
: INFIX_OPERATORS.includes(kind)
|
|
1919
|
+
? parseInfixOperator()
|
|
1920
|
+
: POSTFIX_OPERATORS.includes(kind)
|
|
1921
|
+
? parsePostfixOperator()
|
|
1922
|
+
: undefined;
|
|
1923
|
+
if (target === undefined) {
|
|
1924
|
+
markExpected();
|
|
1925
|
+
throw getNextUnexpectedError();
|
|
1926
|
+
}
|
|
1927
|
+
parser.expecting = "<-";
|
|
1928
|
+
const arrow = getTokenNode(consume(SUBSTITUTE));
|
|
1929
|
+
parser.expecting = "Expression or Op. Symbol";
|
|
1930
|
+
const value = parseOperatorOrExpression();
|
|
1931
|
+
endProduction();
|
|
1932
|
+
return getNode("Substitution", [target, arrow, value]);
|
|
1933
|
+
};
|
|
1934
|
+
|
|
1935
|
+
const parseAssumption = () => {
|
|
1936
|
+
beginProduction("Assumption");
|
|
1937
|
+
parser.expecting = "ASSUM...";
|
|
1938
|
+
addHeir(getTokenNode(consumeOneOf([ASSUMPTION, ASSUME])));
|
|
1939
|
+
if (isLookahead("assumptionName")) {
|
|
1940
|
+
if (isNextKind(DEFBREAK)) consume(DEFBREAK);
|
|
1941
|
+
addHeir(parseIdentifier());
|
|
1942
|
+
parser.expecting = "==";
|
|
1943
|
+
addToken(DEF);
|
|
1944
|
+
}
|
|
1945
|
+
insertDefinitionBreak();
|
|
1946
|
+
parser.expecting = "Expression";
|
|
1947
|
+
addHeir(parseExpression());
|
|
1948
|
+
const heirs = getLastHeirs();
|
|
1949
|
+
endProduction();
|
|
1950
|
+
return getNode("Assumption", heirs);
|
|
1951
|
+
};
|
|
1952
|
+
|
|
1953
|
+
const parseAssumeItem = (lookahead: "assumeExpression" | "nextAssumeExpression") => {
|
|
1954
|
+
if (isNestedAssume()) return parseAssumeProve();
|
|
1955
|
+
if (NEW_SYMBOL_STARTS.has(getNextKind())) return parseNewSymbol();
|
|
1956
|
+
markExpected();
|
|
1957
|
+
if (isLookahead(lookahead)) return parseExpression();
|
|
1958
|
+
throw getNextUnexpectedError();
|
|
1959
|
+
};
|
|
1960
|
+
|
|
1961
|
+
const parseAssumeProve = (): SyntaxNode => {
|
|
1962
|
+
beginProduction("Assume-Prove");
|
|
1963
|
+
if (isNextKind(IDENTIFIER)) {
|
|
1964
|
+
addHeir(getNode("GeneralId", [getIdPrefix(), parseIdentifier()]));
|
|
1965
|
+
addToken(COLONCOLON);
|
|
1966
|
+
addHeir(parseAssumeProve());
|
|
1967
|
+
const heirs = getLastHeirs();
|
|
1968
|
+
endProduction();
|
|
1969
|
+
return getNode("Label", heirs);
|
|
1970
|
+
}
|
|
1971
|
+
addHeir(getTokenNode(consumeOneOf([ASSUME, BOXASSUME])));
|
|
1972
|
+
parser.expecting = "Expression, Declaration, or AssumeProve";
|
|
1973
|
+
addHeir(parseAssumeItem("assumeExpression"));
|
|
1974
|
+
parser.expecting = "PROVE or `,'";
|
|
1975
|
+
while (isNextKind(COMMA)) {
|
|
1976
|
+
addToken(COMMA);
|
|
1977
|
+
parser.expecting = "Expression, Declaration, or AssumeProve";
|
|
1978
|
+
addHeir(parseAssumeItem("nextAssumeExpression"));
|
|
1979
|
+
parser.expecting = "PROVE or `,'";
|
|
1980
|
+
}
|
|
1981
|
+
addHeir(getTokenNode(consumeOneOf([PROVE, BOXPROVE])));
|
|
1982
|
+
parser.expecting = "Expression";
|
|
1983
|
+
addHeir(parseExpression());
|
|
1984
|
+
const heirs = getLastHeirs();
|
|
1985
|
+
endProduction();
|
|
1986
|
+
return getNode("AssumeProve", heirs);
|
|
1987
|
+
};
|
|
1988
|
+
|
|
1989
|
+
const parseNewSymbol = () => {
|
|
1990
|
+
beginProduction("NEW symbol declaration");
|
|
1991
|
+
parser.expecting = "NEW, CONSTANT, VARIABLE, STATE, ACTION, or TEMPORAL";
|
|
1992
|
+
if (isLookahead("constantSymbol")) {
|
|
1993
|
+
if (isLookahead("newConstant")) {
|
|
1994
|
+
addToken(NEW);
|
|
1995
|
+
addToken(CONSTANT);
|
|
1996
|
+
} else addHeir(getTokenNode(consumeOneOf([NEW, CONSTANT])));
|
|
1997
|
+
parser.expecting = "Constant declaration";
|
|
1998
|
+
if (isLookahead("constantDeclaration")) {
|
|
1999
|
+
const declaration = parseIdentifierDeclaration();
|
|
2000
|
+
addHeir(declaration);
|
|
2001
|
+
if (isNextKind(IN)) {
|
|
2002
|
+
const membership = consume(IN);
|
|
2003
|
+
if (declaration.heirs.length > 1) {
|
|
2004
|
+
throw getParseError(
|
|
2005
|
+
`declared symbol with arguments before \\in at ${formatLocation(declaration)}`,
|
|
2006
|
+
);
|
|
2007
|
+
}
|
|
2008
|
+
addHeir(getTokenNode(membership));
|
|
2009
|
+
parser.expecting = "Expression";
|
|
2010
|
+
addHeir(parseExpression());
|
|
2011
|
+
}
|
|
2012
|
+
} else if (OPERATOR_DECLARATION_STARTS.has(getNextKind()))
|
|
2013
|
+
addHeir(parseOperatorDeclaration());
|
|
2014
|
+
else {
|
|
2015
|
+
markExpected();
|
|
2016
|
+
throw getNextUnexpectedError();
|
|
2017
|
+
}
|
|
2018
|
+
} else if (isLookahead("variableSymbol")) {
|
|
2019
|
+
if (isNextKind(NEW)) addToken(NEW);
|
|
2020
|
+
addToken(VARIABLE);
|
|
2021
|
+
parser.expecting = "Identifier";
|
|
2022
|
+
addHeir(getNode("IdentDecl", [parseIdentifier()]));
|
|
2023
|
+
} else if (isLookahead("levelSymbol")) {
|
|
2024
|
+
if (isNextKind(NEW)) addToken(NEW);
|
|
2025
|
+
addHeir(getTokenNode(consumeOneOf([STATE, ACTION, TEMPORAL])));
|
|
2026
|
+
parser.expecting = "Declaration";
|
|
2027
|
+
addHeir(parseDeclaration());
|
|
2028
|
+
} else throw getNextUnexpectedError();
|
|
2029
|
+
const heirs = getLastHeirs();
|
|
2030
|
+
endProduction();
|
|
2031
|
+
return getNode("NewSymb", heirs);
|
|
2032
|
+
};
|
|
2033
|
+
|
|
2034
|
+
const parseTheorem = () => {
|
|
2035
|
+
beginProduction("Theorem");
|
|
2036
|
+
parser.expecting = "THEOREM, PROPOSITION";
|
|
2037
|
+
addHeir(getTokenNode(consumeOneOf([THEOREM, PROPOSITION])));
|
|
2038
|
+
parser.expecting = "Identifier, Assume-Prove or Expression";
|
|
2039
|
+
if (isLookahead("theoremName")) {
|
|
2040
|
+
addHeir(parseIdentifier());
|
|
2041
|
+
parser.expecting = "==";
|
|
2042
|
+
addToken(DEF);
|
|
2043
|
+
}
|
|
2044
|
+
insertDefinitionBreak();
|
|
2045
|
+
if (isLookahead("theoremAssumeProve")) {
|
|
2046
|
+
if (!isNextAssume()) throw getNextUnexpectedError();
|
|
2047
|
+
addHeir(parseAssumeProve());
|
|
2048
|
+
} else if (isLookahead("theoremExpression")) addHeir(parseExpression());
|
|
2049
|
+
else throw getNextUnexpectedError();
|
|
2050
|
+
if (isProofStart(getToken(1))) addHeir(parseProof());
|
|
2051
|
+
const heirs = getLastHeirs();
|
|
2052
|
+
endProduction();
|
|
2053
|
+
return getNode("Theorem", heirs);
|
|
2054
|
+
};
|
|
2055
|
+
|
|
2056
|
+
const parseProof = (): SyntaxNode => {
|
|
2057
|
+
parser.proofDepth++;
|
|
2058
|
+
if (parser.proofDepth >= 100) throw getParseError("Proofs nested more than 100deep.");
|
|
2059
|
+
proofLevels[parser.proofDepth] = -1;
|
|
2060
|
+
const proof = inProduction("Proof", () => {
|
|
2061
|
+
if (getToken(1).kind === BY || getToken(2).kind === BY) return parseUseOrHideOrBy();
|
|
2062
|
+
if (isLookahead("terminalProof")) {
|
|
2063
|
+
const keyword = isNextKind(PROOF) ? getTokenNode(consume(PROOF)) : undefined;
|
|
2064
|
+
return getNode("TerminalProof", keyword, [getTokenNode(consumeOneOf([OBVIOUS, OMITTED]))]);
|
|
2065
|
+
}
|
|
2066
|
+
if (!isLookahead("stepProof")) throw getNextUnexpectedError();
|
|
2067
|
+
if (isLookahead("proofKeyword")) addToken(PROOF);
|
|
2068
|
+
while (getToken(2).kind !== QED) {
|
|
2069
|
+
addHeir(parseStep());
|
|
2070
|
+
parser.expecting = "a proof step";
|
|
2071
|
+
}
|
|
2072
|
+
addHeir(parseQedStep());
|
|
2073
|
+
return getNode("Proof", getLastHeirs());
|
|
2074
|
+
});
|
|
2075
|
+
if (parser.proofDepth < 0) throw getParseError("Parser bug: an extra QED step found.");
|
|
2076
|
+
parser.proofDepth--;
|
|
2077
|
+
return proof;
|
|
2078
|
+
};
|
|
2079
|
+
|
|
2080
|
+
const parseFact = (lookahead: keyof typeof lookaheads) => {
|
|
2081
|
+
if (getNextKind() === MODULE) {
|
|
2082
|
+
addToken(MODULE);
|
|
2083
|
+
parser.expecting = "identifier";
|
|
2084
|
+
addHeir(parseIdentifier());
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
markExpected();
|
|
2088
|
+
if (!isLookahead(lookahead)) throw getNextUnexpectedError();
|
|
2089
|
+
addHeir(parseExpression());
|
|
2090
|
+
};
|
|
2091
|
+
|
|
2092
|
+
const parseUseOrHideOrBy = () => {
|
|
2093
|
+
beginProduction("UseOrHideOrBy");
|
|
2094
|
+
const keyword = getNextKind();
|
|
2095
|
+
const kind = keyword === BY || keyword === PROOF ? "TerminalProof" : "UseOrHide";
|
|
2096
|
+
if (kind === "TerminalProof") {
|
|
2097
|
+
if (isNextKind(PROOF)) addToken(PROOF);
|
|
2098
|
+
addToken(BY);
|
|
2099
|
+
if (isNextKind(ONLY)) addToken(ONLY);
|
|
2100
|
+
} else if (keyword === USE) {
|
|
2101
|
+
addToken(USE);
|
|
2102
|
+
if (isNextKind(ONLY)) addToken(ONLY);
|
|
2103
|
+
} else if (keyword === HIDE) addToken(HIDE);
|
|
2104
|
+
else {
|
|
2105
|
+
markExpected();
|
|
2106
|
+
throw getNextUnexpectedError();
|
|
2107
|
+
}
|
|
2108
|
+
parser.expecting = "an expression, `MODULE' or `DEF'";
|
|
2109
|
+
if (isLookahead("usedFacts")) {
|
|
2110
|
+
parseFact("usedExpression");
|
|
2111
|
+
while (isNextKind(COMMA)) {
|
|
2112
|
+
addToken(COMMA);
|
|
2113
|
+
parser.expecting = "MODULE or expression";
|
|
2114
|
+
parseFact("nextUsedExpression");
|
|
2115
|
+
parser.expecting =
|
|
2116
|
+
kind === "TerminalProof" ? "comma, DEF, or [.]" : "comma, DEF, or proof step";
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
if (isNextKind(DF)) {
|
|
2120
|
+
addToken(DF);
|
|
2121
|
+
parser.expecting = "MODULE or expression";
|
|
2122
|
+
parseFact("definedExpression");
|
|
2123
|
+
while (isNextKind(COMMA)) {
|
|
2124
|
+
addToken(COMMA);
|
|
2125
|
+
parser.expecting = "MODULE or expression";
|
|
2126
|
+
parseFact("nextDefinedExpression");
|
|
2127
|
+
parser.expecting = kind === "TerminalProof" ? "comma or [.]" : "comma or proof step";
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
const heirs = getLastHeirs();
|
|
2131
|
+
endProduction();
|
|
2132
|
+
return getNode(kind, heirs);
|
|
2133
|
+
};
|
|
2134
|
+
|
|
2135
|
+
const parseStepNumber = (message: string) => {
|
|
2136
|
+
parser.expecting = "Step number";
|
|
2137
|
+
const number = getTokenNode(consumeOneOf(STEP_STARTS));
|
|
2138
|
+
if (!isCorrectLevel(parser.token)) throw getParseError(`${formatLocation(number)}: ${message}`);
|
|
2139
|
+
addHeir(number);
|
|
2140
|
+
};
|
|
2141
|
+
|
|
2142
|
+
const parseQedStep = () => {
|
|
2143
|
+
beginProduction("QED step");
|
|
2144
|
+
parseStepNumber("QED step's number has bad level.");
|
|
2145
|
+
parser.expecting = "QED";
|
|
2146
|
+
addHeir(getNode("QEDStep", [getTokenNode(consume(QED))]));
|
|
2147
|
+
if (isProofStart(getToken(1))) addHeir(parseProof());
|
|
2148
|
+
const heirs = getLastHeirs();
|
|
2149
|
+
endProduction();
|
|
2150
|
+
return getNode("ProofStep", heirs);
|
|
2151
|
+
};
|
|
2152
|
+
|
|
2153
|
+
const parseStepBody = (): [SyntaxNode, boolean] => {
|
|
2154
|
+
const next = getToken(1).kind;
|
|
2155
|
+
if (next === USE || next === HIDE) return [parseUseOrHideOrBy(), false];
|
|
2156
|
+
if (next === INSTANCE) return [parseInstantiation(), false];
|
|
2157
|
+
if (next === DEFBREAK || next === DEFINE) return [parseDefinitionStep(), false];
|
|
2158
|
+
const kind = getNextKind();
|
|
2159
|
+
if (kind === HAVE) return [parseKeywordStep("HaveStep", HAVE), false];
|
|
2160
|
+
if (kind === TAKE) return [parseTakeStep(), false];
|
|
2161
|
+
if (kind === WITNESS) return [parseWitnessStep(), false];
|
|
2162
|
+
if (kind === PICK) return [parsePickStep(), true];
|
|
2163
|
+
if (kind === CASE) return [parseKeywordStep("CaseStep", CASE), true];
|
|
2164
|
+
markExpected();
|
|
2165
|
+
if (isLookahead("assertStep")) return [parseAssertStep(), true];
|
|
2166
|
+
throw getNextUnexpectedError();
|
|
2167
|
+
};
|
|
2168
|
+
|
|
2169
|
+
const parseStep = () => {
|
|
2170
|
+
beginProduction("Step");
|
|
2171
|
+
parseStepNumber("step's number has bad level.");
|
|
2172
|
+
parser.expecting = "proof step";
|
|
2173
|
+
const step = parseStepBody();
|
|
2174
|
+
addHeir(step[0]);
|
|
2175
|
+
if (isProofStart(getToken(1))) {
|
|
2176
|
+
if (!step[1])
|
|
2177
|
+
throw getParseError(
|
|
2178
|
+
`${formatLocation(step[0])}: proof of step that does not take a proof.`,
|
|
2179
|
+
);
|
|
2180
|
+
addHeir(parseProof());
|
|
2181
|
+
}
|
|
2182
|
+
const heirs = getLastHeirs();
|
|
2183
|
+
endProduction();
|
|
2184
|
+
return getNode("ProofStep", heirs);
|
|
2185
|
+
};
|
|
2186
|
+
|
|
2187
|
+
const parseDefinitionStep = () => {
|
|
2188
|
+
beginProduction("DefStep");
|
|
2189
|
+
if (isNextKind(DEFINE)) addToken(DEFINE);
|
|
2190
|
+
do addHeir(parseDefinition());
|
|
2191
|
+
while (isLookahead("nextDefinition"));
|
|
2192
|
+
const heirs = getLastHeirs();
|
|
2193
|
+
endProduction();
|
|
2194
|
+
return getNode("DefStep", heirs);
|
|
2195
|
+
};
|
|
2196
|
+
|
|
2197
|
+
const parseKeywordStep = (name: string, keyword: number) => {
|
|
2198
|
+
beginProduction(name);
|
|
2199
|
+
addToken(keyword);
|
|
2200
|
+
parser.expecting = "expression";
|
|
2201
|
+
addHeir(parseExpression());
|
|
2202
|
+
const heirs = getLastHeirs();
|
|
2203
|
+
endProduction();
|
|
2204
|
+
return getNode(name, heirs);
|
|
2205
|
+
};
|
|
2206
|
+
|
|
2207
|
+
const parseCommaList = (
|
|
2208
|
+
parseItem: () => SyntaxNode,
|
|
2209
|
+
first: string,
|
|
2210
|
+
item: string,
|
|
2211
|
+
after: string,
|
|
2212
|
+
) => {
|
|
2213
|
+
addHeir(parseItem());
|
|
2214
|
+
parser.expecting = first;
|
|
2215
|
+
while (isNextKind(COMMA)) {
|
|
2216
|
+
addToken(COMMA);
|
|
2217
|
+
parser.expecting = item;
|
|
2218
|
+
addHeir(parseItem());
|
|
2219
|
+
parser.expecting = after;
|
|
2220
|
+
}
|
|
2221
|
+
};
|
|
2222
|
+
|
|
2223
|
+
const parseTakeStep = () => {
|
|
2224
|
+
beginProduction("TakeStep");
|
|
2225
|
+
addToken(TAKE);
|
|
2226
|
+
parser.expecting = "identifier";
|
|
2227
|
+
if (isLookahead("takeBounds")) {
|
|
2228
|
+
parseCommaList(
|
|
2229
|
+
parseQuantifierBound,
|
|
2230
|
+
"comma or step",
|
|
2231
|
+
"identifier or tuple of identifiers",
|
|
2232
|
+
"comma or proof step",
|
|
2233
|
+
);
|
|
2234
|
+
} else if (isNextKind(IDENTIFIER)) {
|
|
2235
|
+
parseCommaList(parseIdentifier, "comma or proof step", "identifier", "comma or proof step");
|
|
2236
|
+
} else throw getNextUnexpectedError();
|
|
2237
|
+
const heirs = getLastHeirs();
|
|
2238
|
+
endProduction();
|
|
2239
|
+
return getNode("TakeStep", heirs);
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
const parseWitnessStep = () => {
|
|
2243
|
+
beginProduction("WitnessStep");
|
|
2244
|
+
addToken(WITNESS);
|
|
2245
|
+
parser.expecting = "expression";
|
|
2246
|
+
addHeir(parseExpression());
|
|
2247
|
+
while (isNextKind(COMMA)) {
|
|
2248
|
+
addToken(COMMA);
|
|
2249
|
+
parser.expecting = "expression";
|
|
2250
|
+
addHeir(parseExpression());
|
|
2251
|
+
parser.expecting = "comma or colon";
|
|
2252
|
+
}
|
|
2253
|
+
const heirs = getLastHeirs();
|
|
2254
|
+
endProduction();
|
|
2255
|
+
return getNode("WitnessStep", heirs);
|
|
2256
|
+
};
|
|
2257
|
+
|
|
2258
|
+
const parsePickStep = () => {
|
|
2259
|
+
beginProduction("PickStep");
|
|
2260
|
+
addToken(PICK);
|
|
2261
|
+
parser.expecting = "identifier";
|
|
2262
|
+
if (isLookahead("pickIdentifiers")) {
|
|
2263
|
+
parseCommaList(parseIdentifier, "comma, or colon", "identifier", "comma or colon");
|
|
2264
|
+
} else if (getNextKind() === LAB || getNextKind() === IDENTIFIER) {
|
|
2265
|
+
parseCommaList(
|
|
2266
|
+
parseQuantifierBound,
|
|
2267
|
+
"comma or colon",
|
|
2268
|
+
"identifier or tuple of identifiers",
|
|
2269
|
+
"comma or colon",
|
|
2270
|
+
);
|
|
2271
|
+
} else {
|
|
2272
|
+
markExpected();
|
|
2273
|
+
throw getNextUnexpectedError();
|
|
2274
|
+
}
|
|
2275
|
+
addToken(COLON);
|
|
2276
|
+
parser.expecting = "expression";
|
|
2277
|
+
addHeir(parseExpression());
|
|
2278
|
+
const heirs = getLastHeirs();
|
|
2279
|
+
endProduction();
|
|
2280
|
+
return getNode("PickStep", heirs);
|
|
2281
|
+
};
|
|
2282
|
+
|
|
2283
|
+
const parseAssertStep = () => {
|
|
2284
|
+
beginProduction("AssertStep");
|
|
2285
|
+
if (isNextKind(SUFFICES)) {
|
|
2286
|
+
addToken(SUFFICES);
|
|
2287
|
+
parser.expecting = "expression or ASSUME/PROVE";
|
|
2288
|
+
}
|
|
2289
|
+
if (isLookahead("assertedExpression")) addHeir(parseExpression());
|
|
2290
|
+
else if (isNextAssume()) addHeir(parseAssumeProve());
|
|
2291
|
+
else throw getNextUnexpectedError();
|
|
2292
|
+
const heirs = getLastHeirs();
|
|
2293
|
+
endProduction();
|
|
2294
|
+
return getNode("AssertStep", heirs);
|
|
2295
|
+
};
|
|
2296
|
+
|
|
2297
|
+
const parseBracketed = () => {
|
|
2298
|
+
const kind = getNextKind();
|
|
2299
|
+
if (kind === LBR) return parseParenthesized();
|
|
2300
|
+
if (kind === LBC) return parseBraceCases();
|
|
2301
|
+
if (kind === LSB) return parseBracketCases();
|
|
2302
|
+
if (kind === LAB) return parseTupleOrAction();
|
|
2303
|
+
if (kind === SF || kind === WF) return parseFairness();
|
|
2304
|
+
markExpected();
|
|
2305
|
+
throw getNextUnexpectedError();
|
|
2306
|
+
};
|
|
2307
|
+
|
|
2308
|
+
const parseOpenExpression = () => {
|
|
2309
|
+
const kind = getNextKind();
|
|
2310
|
+
if (kind === EXISTS || kind === FORALL) return parseQuantifier();
|
|
2311
|
+
if (kind === T_EXISTS || kind === T_FORALL) return parseTemporalQuantifier();
|
|
2312
|
+
if (kind === IF) return parseIfThenElse();
|
|
2313
|
+
if (kind === CASE) return parseCase();
|
|
2314
|
+
if (kind === LET) return parseLetIn();
|
|
2315
|
+
if (kind === CHOOSE) return parseChoose();
|
|
2316
|
+
markExpected();
|
|
2317
|
+
throw getNextUnexpectedError();
|
|
2318
|
+
};
|
|
2319
|
+
|
|
2320
|
+
const parseNumber = () => {
|
|
2321
|
+
const whole = getTokenNode(consume(NUMBER_LITERAL));
|
|
2322
|
+
if (!isLookahead("decimalPart")) return getNode("Number", [whole]);
|
|
2323
|
+
const dot = getTokenNode(consume(DOT));
|
|
2324
|
+
return getNode("Real", [whole, dot, getTokenNode(consume(NUMBER_LITERAL))]);
|
|
2325
|
+
};
|
|
2326
|
+
|
|
2327
|
+
const parseOperatorArguments = () => {
|
|
2328
|
+
beginProduction("Optional Arguments");
|
|
2329
|
+
addToken(LBR);
|
|
2330
|
+
addHeir(parseOperatorOrExpression());
|
|
2331
|
+
while (isNextKind(COMMA)) {
|
|
2332
|
+
addToken(COMMA);
|
|
2333
|
+
addHeir(parseOperatorOrExpression());
|
|
2334
|
+
}
|
|
2335
|
+
addToken(RBR);
|
|
2336
|
+
const heirs = getLastHeirs();
|
|
2337
|
+
endProduction();
|
|
2338
|
+
return getNode("OpArgs", heirs);
|
|
2339
|
+
};
|
|
2340
|
+
|
|
2341
|
+
const parseOperatorOrExpression = (): SyntaxNode => {
|
|
2342
|
+
if (isLookahead("operatorArgument") && isNextAboveJunction()) {
|
|
2343
|
+
const kind = getNextKind();
|
|
2344
|
+
if (NON_EXPRESSION_PREFIX_OPERATORS.includes(kind)) {
|
|
2345
|
+
return getNode("GenNonExpPrefixOp", [getIdPrefix(), parseNonExpressionPrefixOperator()]);
|
|
2346
|
+
}
|
|
2347
|
+
if (INFIX_OPERATORS.includes(kind)) {
|
|
2348
|
+
const operator = parseInfixOperator();
|
|
2349
|
+
if (operator.image === "\\X" || operator.image === "\\times") {
|
|
2350
|
+
throw getParseError(
|
|
2351
|
+
`${formatLocation(operator)}: \\X may not be used as an infix operator.`,
|
|
2352
|
+
);
|
|
2353
|
+
}
|
|
2354
|
+
return getNode("GenInfixOp", [getIdPrefix(), operator]);
|
|
2355
|
+
}
|
|
2356
|
+
if (POSTFIX_OPERATORS.includes(kind))
|
|
2357
|
+
return getNode("GenPostfixOp", [getIdPrefix(), parsePostfixOperator()]);
|
|
2358
|
+
markExpected();
|
|
2359
|
+
throw getNextUnexpectedError();
|
|
2360
|
+
}
|
|
2361
|
+
if (isLookahead("lambdaArgument") && isNextAboveJunction()) return parseLambda();
|
|
2362
|
+
if (isLookahead("expressionArgument")) return parseExpression();
|
|
2363
|
+
throw getNextUnexpectedError();
|
|
2364
|
+
};
|
|
2365
|
+
|
|
2366
|
+
const parseParenthesized = () =>
|
|
2367
|
+
getNode("ParenExpr", [
|
|
2368
|
+
getTokenNode(consume(LBR)),
|
|
2369
|
+
parseExpression(),
|
|
2370
|
+
getTokenNode(consume(RBR)),
|
|
2371
|
+
]);
|
|
2372
|
+
|
|
2373
|
+
const parseQuantifier = () => {
|
|
2374
|
+
beginProduction("Quantified form");
|
|
2375
|
+
addHeir(getTokenNode(consumeOneOf([EXISTS, FORALL])));
|
|
2376
|
+
const isUnbound = isLookahead("unboundQuantifier");
|
|
2377
|
+
if (isUnbound) {
|
|
2378
|
+
addHeir(parseIdentifier());
|
|
2379
|
+
while (isNextKind(COMMA)) {
|
|
2380
|
+
addToken(COMMA);
|
|
2381
|
+
addHeir(parseIdentifier());
|
|
2382
|
+
}
|
|
2383
|
+
} else if (getNextKind() === LAB || getNextKind() === IDENTIFIER) {
|
|
2384
|
+
addHeir(parseQuantifierBound());
|
|
2385
|
+
while (isNextKind(COMMA)) {
|
|
2386
|
+
addToken(COMMA);
|
|
2387
|
+
addHeir(parseQuantifierBound());
|
|
2388
|
+
}
|
|
2389
|
+
} else {
|
|
2390
|
+
markExpected();
|
|
2391
|
+
throw getNextUnexpectedError();
|
|
2392
|
+
}
|
|
2393
|
+
addToken(COLON);
|
|
2394
|
+
addHeir(parseExpression());
|
|
2395
|
+
const heirs = getLastHeirs();
|
|
2396
|
+
endProduction();
|
|
2397
|
+
return getNode(isUnbound ? "UnboundQuant" : "BoundQuant", heirs);
|
|
2398
|
+
};
|
|
2399
|
+
|
|
2400
|
+
const parseTemporalQuantifier = () => {
|
|
2401
|
+
beginProduction("Bound Quantified Expression");
|
|
2402
|
+
addHeir(getTokenNode(consumeOneOf([T_EXISTS, T_FORALL])));
|
|
2403
|
+
addHeir(parseIdentifier());
|
|
2404
|
+
while (isNextKind(COMMA)) {
|
|
2405
|
+
addToken(COMMA);
|
|
2406
|
+
addHeir(parseIdentifier());
|
|
2407
|
+
}
|
|
2408
|
+
addToken(COLON);
|
|
2409
|
+
addHeir(parseExpression());
|
|
2410
|
+
const heirs = getLastHeirs();
|
|
2411
|
+
endProduction();
|
|
2412
|
+
return getNode("UnboundQuant", heirs);
|
|
2413
|
+
};
|
|
2414
|
+
|
|
2415
|
+
const parseQuantifierBound = () => {
|
|
2416
|
+
beginProduction("Quant Bound");
|
|
2417
|
+
const kind = getNextKind();
|
|
2418
|
+
if (kind === LAB) addHeir(parseIdentifierTuple());
|
|
2419
|
+
else if (kind === IDENTIFIER) {
|
|
2420
|
+
addHeir(parseIdentifier());
|
|
2421
|
+
while (isNextKind(COMMA)) {
|
|
2422
|
+
addToken(COMMA);
|
|
2423
|
+
addHeir(parseIdentifier());
|
|
2424
|
+
parser.expecting = ", or \\in";
|
|
2425
|
+
}
|
|
2426
|
+
} else {
|
|
2427
|
+
markExpected();
|
|
2428
|
+
throw getNextUnexpectedError();
|
|
2429
|
+
}
|
|
2430
|
+
addToken(IN);
|
|
2431
|
+
addHeir(parseExpression());
|
|
2432
|
+
const heirs = getLastHeirs();
|
|
2433
|
+
endProduction();
|
|
2434
|
+
return getNode("QuantBound", heirs);
|
|
2435
|
+
};
|
|
2436
|
+
|
|
2437
|
+
const parseBraceCases = () =>
|
|
2438
|
+
parseProduction("Some { } form", () => {
|
|
2439
|
+
addToken(LBC);
|
|
2440
|
+
const kind = isLookahead("setContents") ? parseSetContents() : "SetEnumerate";
|
|
2441
|
+
addToken(RBC);
|
|
2442
|
+
return kind;
|
|
2443
|
+
});
|
|
2444
|
+
|
|
2445
|
+
// The heirs of a set in braces, before its closing brace, and the kind of set they make.
|
|
2446
|
+
const parseSetContents = () => {
|
|
2447
|
+
if (isFunctionBound()) {
|
|
2448
|
+
const nextKind = getNextKind();
|
|
2449
|
+
const bound =
|
|
2450
|
+
nextKind === LAB
|
|
2451
|
+
? parseIdentifierTuple()
|
|
2452
|
+
: nextKind === IDENTIFIER
|
|
2453
|
+
? parseIdentifier()
|
|
2454
|
+
: undefined;
|
|
2455
|
+
if (bound === undefined) {
|
|
2456
|
+
markExpected();
|
|
2457
|
+
throw getNextUnexpectedError();
|
|
2458
|
+
}
|
|
2459
|
+
parser.expecting = "\\in";
|
|
2460
|
+
const operator = getTokenNode(consume(IN));
|
|
2461
|
+
const set = parseExpression();
|
|
2462
|
+
parser.expecting = "':', ',' or '}'";
|
|
2463
|
+
if (getNextKind() === COLON) {
|
|
2464
|
+
const colon = getTokenNode(consume(COLON));
|
|
2465
|
+
addHeir(bound);
|
|
2466
|
+
addHeir(operator);
|
|
2467
|
+
addHeir(set);
|
|
2468
|
+
addHeir(colon);
|
|
2469
|
+
addHeir(parseExpression());
|
|
2470
|
+
return "SubsetOf";
|
|
2471
|
+
}
|
|
2472
|
+
addHeir(
|
|
2473
|
+
getNode("InfixExpr", [
|
|
2474
|
+
getNode("GeneralId", [getIdPrefix(), bound]),
|
|
2475
|
+
getNode("GenInfixOp", [getIdPrefix(), operator]),
|
|
2476
|
+
set,
|
|
2477
|
+
]),
|
|
2478
|
+
);
|
|
2479
|
+
if (getNextKind() !== COMMA) {
|
|
2480
|
+
markExpected();
|
|
2481
|
+
return "SetEnumerate";
|
|
2482
|
+
}
|
|
2483
|
+
do {
|
|
2484
|
+
addToken(COMMA);
|
|
2485
|
+
addHeir(parseExpression());
|
|
2486
|
+
} while (isNextKind(COMMA));
|
|
2487
|
+
return "SetEnumerate";
|
|
2488
|
+
}
|
|
2489
|
+
if (isLookahead("setEnumeration")) {
|
|
2490
|
+
addHeir(parseExpression());
|
|
2491
|
+
while (isNextKind(COMMA)) {
|
|
2492
|
+
addToken(COMMA);
|
|
2493
|
+
addHeir(parseExpression());
|
|
2494
|
+
}
|
|
2495
|
+
return "SetEnumerate";
|
|
2496
|
+
}
|
|
2497
|
+
if (isLookahead("setOfAll")) {
|
|
2498
|
+
addHeir(parseExpression());
|
|
2499
|
+
addToken(COLON);
|
|
2500
|
+
addHeir(parseQuantifierBound());
|
|
2501
|
+
while (isNextKind(COMMA)) {
|
|
2502
|
+
addToken(COMMA);
|
|
2503
|
+
addHeir(parseQuantifierBound());
|
|
2504
|
+
}
|
|
2505
|
+
return "SetOfAll";
|
|
2506
|
+
}
|
|
2507
|
+
if (!isLookahead("setExpression")) throw getNextUnexpectedError();
|
|
2508
|
+
const element = parseExpression();
|
|
2509
|
+
addHeir(element);
|
|
2510
|
+
const separator = getNextKind();
|
|
2511
|
+
if (separator === COLON) {
|
|
2512
|
+
const colon = consume(COLON);
|
|
2513
|
+
if (
|
|
2514
|
+
element.heirs.length > 1 &&
|
|
2515
|
+
element.kind === "InfixExpr" &&
|
|
2516
|
+
element.heirs[1].heirs[1].image === "\\in"
|
|
2517
|
+
) {
|
|
2518
|
+
throw getParseError(
|
|
2519
|
+
`Form {a \\in b : c \\in d }, at line ${colon.beginLine}, is not allowed`,
|
|
2520
|
+
);
|
|
2521
|
+
}
|
|
2522
|
+
addHeir(getTokenNode(colon));
|
|
2523
|
+
addHeir(parseQuantifierBound());
|
|
2524
|
+
while (isNextKind(COMMA)) {
|
|
2525
|
+
addToken(COMMA);
|
|
2526
|
+
addHeir(parseQuantifierBound());
|
|
2527
|
+
}
|
|
2528
|
+
return "SetOfAll";
|
|
2529
|
+
}
|
|
2530
|
+
if (separator !== COMMA) {
|
|
2531
|
+
markExpected();
|
|
2532
|
+
return "SetEnumerate";
|
|
2533
|
+
}
|
|
2534
|
+
do {
|
|
2535
|
+
addToken(COMMA);
|
|
2536
|
+
addHeir(parseExpression());
|
|
2537
|
+
} while (isNextKind(COMMA));
|
|
2538
|
+
return "SetEnumerate";
|
|
2539
|
+
};
|
|
2540
|
+
|
|
2541
|
+
const parseFields = (parseField: () => SyntaxNode) => {
|
|
2542
|
+
addHeir(parseField());
|
|
2543
|
+
while (isNextKind(COMMA)) {
|
|
2544
|
+
addToken(COMMA);
|
|
2545
|
+
addHeir(parseField());
|
|
2546
|
+
}
|
|
2547
|
+
addToken(RSB);
|
|
2548
|
+
};
|
|
2549
|
+
|
|
2550
|
+
const parseField = (name: string, kind: string, separator: number) => () => {
|
|
2551
|
+
beginProduction(name);
|
|
2552
|
+
const field = parseIdentifier();
|
|
2553
|
+
const operator = getTokenNode(consume(separator));
|
|
2554
|
+
const value = parseExpression();
|
|
2555
|
+
endProduction();
|
|
2556
|
+
return getNode(kind, [field, operator, value]);
|
|
2557
|
+
};
|
|
2558
|
+
|
|
2559
|
+
const parseBracketCases = () =>
|
|
2560
|
+
parseProduction("Some [] Form", () => {
|
|
2561
|
+
addToken(LSB);
|
|
2562
|
+
if (isFunctionBound()) {
|
|
2563
|
+
addHeir(parseQuantifierBound());
|
|
2564
|
+
while (isNextKind(COMMA)) {
|
|
2565
|
+
addToken(COMMA);
|
|
2566
|
+
addHeir(parseQuantifierBound());
|
|
2567
|
+
}
|
|
2568
|
+
addToken(MAPTO);
|
|
2569
|
+
addHeir(parseExpression());
|
|
2570
|
+
addToken(RSB);
|
|
2571
|
+
return "FcnConst";
|
|
2572
|
+
}
|
|
2573
|
+
if (isLookahead("recordFields")) {
|
|
2574
|
+
parseFields(parseField("Field Value", "FieldVal", MAPTO));
|
|
2575
|
+
return "RcdConstructor";
|
|
2576
|
+
}
|
|
2577
|
+
if (getToken(2).kind === MAPTO && isFieldNameToken(getToken(1))) {
|
|
2578
|
+
getToken(1).kind = IDENTIFIER;
|
|
2579
|
+
parseFields(parseField("Field Value", "FieldVal", MAPTO));
|
|
2580
|
+
return "RcdConstructor";
|
|
2581
|
+
}
|
|
2582
|
+
if (isLookahead("recordSets")) {
|
|
2583
|
+
parseFields(parseField("Field Set", "FieldSet", COLON));
|
|
2584
|
+
return "SetOfRcds";
|
|
2585
|
+
}
|
|
2586
|
+
if (!isLookahead("bracketExpression")) throw getNextUnexpectedError();
|
|
2587
|
+
addHeir(parseExpression());
|
|
2588
|
+
const next = getNextKind();
|
|
2589
|
+
if (next === COMMA || next === RSB) {
|
|
2590
|
+
while (isNextKind(COMMA)) {
|
|
2591
|
+
addToken(COMMA);
|
|
2592
|
+
addHeir(parseExpression());
|
|
2593
|
+
}
|
|
2594
|
+
addToken(RSB);
|
|
2595
|
+
parser.lastOperator = FUNCTION_OPERATOR;
|
|
2596
|
+
return "FcnAppl";
|
|
2597
|
+
}
|
|
2598
|
+
if (next === ARROW) {
|
|
2599
|
+
addToken(ARROW);
|
|
2600
|
+
addHeir(parseExpression());
|
|
2601
|
+
addToken(RSB);
|
|
2602
|
+
return "SetOfFcns";
|
|
2603
|
+
}
|
|
2604
|
+
if (next === EXCEPT) {
|
|
2605
|
+
addToken(EXCEPT);
|
|
2606
|
+
addHeir(parseExceptSpecification());
|
|
2607
|
+
while (isNextKind(COMMA)) {
|
|
2608
|
+
addToken(COMMA);
|
|
2609
|
+
addHeir(parseExceptSpecification());
|
|
2610
|
+
}
|
|
2611
|
+
addToken(RSB);
|
|
2612
|
+
return "Except";
|
|
2613
|
+
}
|
|
2614
|
+
if (next === ARSB) {
|
|
2615
|
+
addToken(ARSB);
|
|
2616
|
+
addHeir(parseReducedExpression());
|
|
2617
|
+
return "ActionExpr";
|
|
2618
|
+
}
|
|
2619
|
+
markExpected();
|
|
2620
|
+
throw getNextUnexpectedError();
|
|
2621
|
+
});
|
|
2622
|
+
|
|
2623
|
+
const parseExceptSpecification = () => {
|
|
2624
|
+
beginProduction("Except Spec");
|
|
2625
|
+
addToken(BANG);
|
|
2626
|
+
do {
|
|
2627
|
+
addHeir(parseExceptComponent());
|
|
2628
|
+
parser.expecting = "= or ,";
|
|
2629
|
+
} while (getNextKind() === DOT || getNextKind() === LSB || markExpected());
|
|
2630
|
+
addToken(EQUALS);
|
|
2631
|
+
addHeir(parseExpression());
|
|
2632
|
+
const heirs = getLastHeirs();
|
|
2633
|
+
endProduction();
|
|
2634
|
+
return getNode("ExceptSpec", heirs);
|
|
2635
|
+
};
|
|
2636
|
+
|
|
2637
|
+
const parseExceptComponent = () => {
|
|
2638
|
+
beginProduction("Except Component");
|
|
2639
|
+
const kind = getNextKind();
|
|
2640
|
+
if (kind === DOT) {
|
|
2641
|
+
addToken(DOT);
|
|
2642
|
+
convertFieldName(getToken(1));
|
|
2643
|
+
const field = parseIdentifier();
|
|
2644
|
+
if (field.image === "@") errors.push("@ used in !.@");
|
|
2645
|
+
addHeir(field);
|
|
2646
|
+
} else if (kind === LSB) {
|
|
2647
|
+
addToken(LSB);
|
|
2648
|
+
addHeir(parseExpression());
|
|
2649
|
+
while (isNextKind(COMMA)) {
|
|
2650
|
+
addToken(COMMA);
|
|
2651
|
+
addHeir(parseExpression());
|
|
2652
|
+
}
|
|
2653
|
+
addToken(RSB);
|
|
2654
|
+
} else {
|
|
2655
|
+
markExpected();
|
|
2656
|
+
throw getNextUnexpectedError();
|
|
2657
|
+
}
|
|
2658
|
+
const heirs = getLastHeirs();
|
|
2659
|
+
endProduction();
|
|
2660
|
+
return getNode("ExceptComponent", heirs);
|
|
2661
|
+
};
|
|
2662
|
+
|
|
2663
|
+
const parseTupleOrAction = () =>
|
|
2664
|
+
parseProduction("Some << -- >> or >>_ Form", () => {
|
|
2665
|
+
addToken(LAB);
|
|
2666
|
+
if (isLookahead("tupleContents")) {
|
|
2667
|
+
addHeir(parseExpression());
|
|
2668
|
+
while (isNextKind(COMMA)) {
|
|
2669
|
+
addToken(COMMA);
|
|
2670
|
+
addHeir(parseExpression());
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2673
|
+
const kind = getNextKind();
|
|
2674
|
+
if (kind === RAB) {
|
|
2675
|
+
addToken(RAB);
|
|
2676
|
+
return "Tuple";
|
|
2677
|
+
}
|
|
2678
|
+
if (kind === ARAB) {
|
|
2679
|
+
addToken(ARAB);
|
|
2680
|
+
addHeir(parseReducedExpression());
|
|
2681
|
+
return "ActionExpr";
|
|
2682
|
+
}
|
|
2683
|
+
markExpected();
|
|
2684
|
+
throw getNextUnexpectedError();
|
|
2685
|
+
});
|
|
2686
|
+
|
|
2687
|
+
const parseNoOperatorExtension = (): SyntaxNode => {
|
|
2688
|
+
const name = parseIdentifier();
|
|
2689
|
+
const operatorArguments = isLookahead("identifierArguments")
|
|
2690
|
+
? parseOperatorArguments()
|
|
2691
|
+
: undefined;
|
|
2692
|
+
if (isNextKind(BANG)) {
|
|
2693
|
+
const bang = getTokenNode(consume(BANG));
|
|
2694
|
+
addHeir(getNode("IdPrefixElement", [name], operatorArguments, [bang]));
|
|
2695
|
+
return parseNoOperatorExtension();
|
|
2696
|
+
}
|
|
2697
|
+
setFairnessHook(operatorArguments);
|
|
2698
|
+
return getNode("GeneralId", [getNode("IdPrefix", getLastHeirs()), name]);
|
|
2699
|
+
};
|
|
2700
|
+
|
|
2701
|
+
const parseReducedExpression = () => {
|
|
2702
|
+
beginProduction("restricted form of expression");
|
|
2703
|
+
const kind = getNextKind();
|
|
2704
|
+
const expression =
|
|
2705
|
+
kind === IDENTIFIER
|
|
2706
|
+
? parseNoOperatorExtension()
|
|
2707
|
+
: kind === LBR
|
|
2708
|
+
? parseParenthesized()
|
|
2709
|
+
: kind === LBC
|
|
2710
|
+
? parseBraceCases()
|
|
2711
|
+
: kind === LSB
|
|
2712
|
+
? parseBracketCases()
|
|
2713
|
+
: kind === LAB
|
|
2714
|
+
? parseTupleOrAction()
|
|
2715
|
+
: undefined;
|
|
2716
|
+
if (expression === undefined) {
|
|
2717
|
+
markExpected();
|
|
2718
|
+
throw getNextUnexpectedError();
|
|
2719
|
+
}
|
|
2720
|
+
endProduction();
|
|
2721
|
+
return expression;
|
|
2722
|
+
};
|
|
2723
|
+
|
|
2724
|
+
const parseFairness = () => {
|
|
2725
|
+
setFairnessHook(undefined);
|
|
2726
|
+
beginProduction("Fairness Expression");
|
|
2727
|
+
const keyword = getTokenNode(consumeOneOf([WF, SF]));
|
|
2728
|
+
const expression = parseReducedExpression();
|
|
2729
|
+
const position = `${keyword.location[0]}`;
|
|
2730
|
+
if (isLookahead("fairnessArguments")) {
|
|
2731
|
+
const open = getTokenNode(consume(LBR));
|
|
2732
|
+
const argument = parseExpression();
|
|
2733
|
+
const close = getTokenNode(consume(RBR));
|
|
2734
|
+
endProduction();
|
|
2735
|
+
if (expression.kind === "GeneralId" && parser.fairnessHook !== undefined) {
|
|
2736
|
+
return getNode("FairnessExpr", [
|
|
2737
|
+
keyword,
|
|
2738
|
+
getNode("OpApplication", [expression, parser.fairnessHook]),
|
|
2739
|
+
open,
|
|
2740
|
+
argument,
|
|
2741
|
+
close,
|
|
2742
|
+
]);
|
|
2743
|
+
}
|
|
2744
|
+
if (!FAIRNESS_SUBSCRIPTS.has(String(expression.kind))) {
|
|
2745
|
+
errors.push(
|
|
2746
|
+
`Error in fairness expression at ${position}: ${keyword.location[1]}: could not link arguments\n`,
|
|
2747
|
+
);
|
|
2748
|
+
return expression;
|
|
2749
|
+
}
|
|
2750
|
+
return getNode("FairnessExpr", [keyword, expression, open, argument, close]);
|
|
2751
|
+
}
|
|
2752
|
+
endProduction();
|
|
2753
|
+
if (parser.fairnessHook === undefined) {
|
|
2754
|
+
throw getParseError(
|
|
2755
|
+
`Ill-structured fairness expression at line ${position}, column ${keyword.location[1]}`,
|
|
2756
|
+
);
|
|
2757
|
+
}
|
|
2758
|
+
const parameters = parser.fairnessHook.heirs;
|
|
2759
|
+
if (parameters.length !== 3) {
|
|
2760
|
+
errors.push(`Error in fairness expression at ${position}: ${keyword.location[1]}\n`);
|
|
2761
|
+
return expression;
|
|
2762
|
+
}
|
|
2763
|
+
return getNode("FairnessExpr", [keyword, expression, ...parameters]);
|
|
2764
|
+
};
|
|
2765
|
+
|
|
2766
|
+
const parseIfThenElse = () => {
|
|
2767
|
+
beginProduction("IF THEN ELSE");
|
|
2768
|
+
const heirs = [
|
|
2769
|
+
getTokenNode(consume(IF)),
|
|
2770
|
+
parseExpression(),
|
|
2771
|
+
getTokenNode(consume(THEN)),
|
|
2772
|
+
parseExpression(),
|
|
2773
|
+
getTokenNode(consume(ELSE)),
|
|
2774
|
+
parseExpression(),
|
|
2775
|
+
];
|
|
2776
|
+
endProduction();
|
|
2777
|
+
return getNode("IfThenElse", heirs);
|
|
2778
|
+
};
|
|
2779
|
+
|
|
2780
|
+
const isCaseSeparator = () => {
|
|
2781
|
+
const next = getToken(1);
|
|
2782
|
+
return next.kind === CASESEP && isAboveJunction(next.beginColumn);
|
|
2783
|
+
};
|
|
2784
|
+
|
|
2785
|
+
const parseCaseArm = (name: string, kind: string, parseCondition: () => SyntaxNode) => {
|
|
2786
|
+
beginProduction(name);
|
|
2787
|
+
const heirs = [parseCondition(), getTokenNode(consume(ARROW)), parseExpression()];
|
|
2788
|
+
endProduction();
|
|
2789
|
+
return getNode(kind, heirs);
|
|
2790
|
+
};
|
|
2791
|
+
|
|
2792
|
+
const parseCase = () => {
|
|
2793
|
+
beginProduction("CASE Expression");
|
|
2794
|
+
addToken(CASE);
|
|
2795
|
+
addHeir(parseCaseArm("Case Arm", "CaseArm", parseExpression));
|
|
2796
|
+
while (isCaseSeparator() && getToken(2).kind !== OTHER) {
|
|
2797
|
+
addToken(CASESEP);
|
|
2798
|
+
addHeir(parseCaseArm("Case Arm", "CaseArm", parseExpression));
|
|
2799
|
+
}
|
|
2800
|
+
if (isCaseSeparator()) {
|
|
2801
|
+
addToken(CASESEP);
|
|
2802
|
+
addHeir(parseCaseArm("Case Other Arm", "OtherArm", () => getTokenNode(consume(OTHER))));
|
|
2803
|
+
}
|
|
2804
|
+
const heirs = getLastHeirs();
|
|
2805
|
+
endProduction();
|
|
2806
|
+
return getNode("Case", heirs);
|
|
2807
|
+
};
|
|
2808
|
+
|
|
2809
|
+
const parseLetIn = () => {
|
|
2810
|
+
beginProduction("Case Other Arm");
|
|
2811
|
+
const heirs = [
|
|
2812
|
+
getTokenNode(consume(LET)),
|
|
2813
|
+
parseLetDefinitions(),
|
|
2814
|
+
getTokenNode(consume(LETIN)),
|
|
2815
|
+
parseExpression(),
|
|
2816
|
+
];
|
|
2817
|
+
endProduction();
|
|
2818
|
+
return getNode("LetIn", heirs);
|
|
2819
|
+
};
|
|
2820
|
+
|
|
2821
|
+
const parseLetDefinitions = () => {
|
|
2822
|
+
beginProduction("Let Definitions");
|
|
2823
|
+
do {
|
|
2824
|
+
const kind = getNextKind();
|
|
2825
|
+
if (kind === LOCAL || kind === DEFBREAK) addHeir(parseDefinition());
|
|
2826
|
+
else if (kind === RECURSIVE) addHeir(parseRecursive());
|
|
2827
|
+
else {
|
|
2828
|
+
markExpected();
|
|
2829
|
+
throw getNextUnexpectedError();
|
|
2830
|
+
}
|
|
2831
|
+
} while ([LOCAL, RECURSIVE, DEFBREAK].includes(getNextKind()) || markExpected());
|
|
2832
|
+
const heirs = getLastHeirs();
|
|
2833
|
+
endProduction();
|
|
2834
|
+
return getNode("LetDefinitions", heirs);
|
|
2835
|
+
};
|
|
2836
|
+
|
|
2837
|
+
const parseJunctionItem = (kind: string) => {
|
|
2838
|
+
beginProduction("Junction Item");
|
|
2839
|
+
const bullet = getTokenNode(consumeOneOf([OR, AND]));
|
|
2840
|
+
const item = parseExpression();
|
|
2841
|
+
endProduction();
|
|
2842
|
+
const node = getNode(kind, [bullet, item]);
|
|
2843
|
+
checkIndentation(item, node);
|
|
2844
|
+
return node;
|
|
2845
|
+
};
|
|
2846
|
+
|
|
2847
|
+
const parseJunctions = () => {
|
|
2848
|
+
const first = getToken(1);
|
|
2849
|
+
junctions.push({ isConjunction: first.kind === AND, column: first.beginColumn });
|
|
2850
|
+
return parseProduction("AND-OR Junction", () => {
|
|
2851
|
+
const kind = isLookahead("disjunctionList")
|
|
2852
|
+
? "DisjList"
|
|
2853
|
+
: isLookahead("conjunctionList")
|
|
2854
|
+
? "ConjList"
|
|
2855
|
+
: undefined;
|
|
2856
|
+
if (kind === undefined) throw getNextUnexpectedError();
|
|
2857
|
+
const itemKind = kind === "DisjList" ? "DisjItem" : "ConjItem";
|
|
2858
|
+
addHeir(parseJunctionItem(itemKind));
|
|
2859
|
+
while (isNewBullet()) addHeir(parseJunctionItem(itemKind));
|
|
2860
|
+
junctions.pop();
|
|
2861
|
+
return kind;
|
|
2862
|
+
});
|
|
2863
|
+
};
|
|
2864
|
+
|
|
2865
|
+
const parseChoose = () => {
|
|
2866
|
+
beginProduction("(Un)Bounded Choose");
|
|
2867
|
+
const keyword = getTokenNode(consume(CHOOSE));
|
|
2868
|
+
const kind = getNextKind();
|
|
2869
|
+
const bound =
|
|
2870
|
+
kind === IDENTIFIER ? parseIdentifier() : kind === LAB ? parseIdentifierTuple() : undefined;
|
|
2871
|
+
if (bound === undefined) {
|
|
2872
|
+
markExpected();
|
|
2873
|
+
throw getNextUnexpectedError();
|
|
2874
|
+
}
|
|
2875
|
+
beginProduction("Domain binding");
|
|
2876
|
+
if (isNextKind(IN)) {
|
|
2877
|
+
addToken(IN);
|
|
2878
|
+
parser.expecting = "Expression";
|
|
2879
|
+
addHeir(parseExpression());
|
|
2880
|
+
}
|
|
2881
|
+
const domain = getNode("MaybeBound", getLastHeirs());
|
|
2882
|
+
endProduction();
|
|
2883
|
+
const heirs = [keyword, bound, domain, getTokenNode(consume(COLON)), parseExpression()];
|
|
2884
|
+
endProduction();
|
|
2885
|
+
return getNode("UnboundOrBoundChoose", heirs);
|
|
2886
|
+
};
|
|
2887
|
+
|
|
2888
|
+
const parseLambda = () => {
|
|
2889
|
+
beginProduction("Lambda");
|
|
2890
|
+
addToken(LAMBDA);
|
|
2891
|
+
parser.expecting = "Identifier";
|
|
2892
|
+
parseCommaList(parseIdentifier, "`,' or `:'", "Identifier", "`,' or `:'");
|
|
2893
|
+
addToken(COLON);
|
|
2894
|
+
parser.expecting = "Expression";
|
|
2895
|
+
addHeir(parseExpression());
|
|
2896
|
+
const heirs = getLastHeirs();
|
|
2897
|
+
endProduction();
|
|
2898
|
+
return getNode("Lambda", heirs);
|
|
2899
|
+
};
|
|
2900
|
+
|
|
2901
|
+
const pushOperator = (kind: string, operator: SyntaxNode) => {
|
|
2902
|
+
pushOperand(getNode(kind, [getIdPrefix(), operator]), parser.lastOperator);
|
|
2903
|
+
if (getStack().length !== 1) reduceStack();
|
|
2904
|
+
};
|
|
2905
|
+
|
|
2906
|
+
const parseLeadingOperator = () => {
|
|
2907
|
+
const kind = getNextKind();
|
|
2908
|
+
if (PREFIX_OPERATORS.includes(kind)) {
|
|
2909
|
+
return pushOperator("GenPrefixOp", parsePrefixOperator(PREFIX_OPERATORS, "PrefixOp"));
|
|
2910
|
+
}
|
|
2911
|
+
if (INFIX_OPERATORS.includes(kind)) return pushOperator("GenInfixOp", parseInfixOperator());
|
|
2912
|
+
markExpected();
|
|
2913
|
+
throw getNextUnexpectedError();
|
|
2914
|
+
};
|
|
2915
|
+
|
|
2916
|
+
const parseExpression = (): SyntaxNode => {
|
|
2917
|
+
beginProduction("Expression");
|
|
2918
|
+
parser.operatorStack = [];
|
|
2919
|
+
operatorStacks.push(parser.operatorStack);
|
|
2920
|
+
while (isLookahead("leadingOperator") && isNextAboveJunction()) parseLeadingOperator();
|
|
2921
|
+
if (isLookahead("openExpression") && isNextAboveJunction()) pushOperand(parseOpenExpression());
|
|
2922
|
+
else if (isNextAboveJunction()) parseExtendableExpression();
|
|
2923
|
+
else throw getNextUnexpectedError();
|
|
2924
|
+
endProduction();
|
|
2925
|
+
const expression = finalReduce();
|
|
2926
|
+
if (expression === undefined) throw getParseError(" Couldn't reduce expression stack.");
|
|
2927
|
+
operatorStacks.pop();
|
|
2928
|
+
parser.operatorStack = operatorStacks.at(-1);
|
|
2929
|
+
return expression;
|
|
2930
|
+
};
|
|
2931
|
+
|
|
2932
|
+
const parseExtendableExpression = () => {
|
|
2933
|
+
beginProduction("ExtendableExpr");
|
|
2934
|
+
if (isLookahead("junctionList") && isOperandExpected()) pushOperand(parseJunctions());
|
|
2935
|
+
else if (BRACKETED_STARTS.has(getNextKind())) pushOperand(parseBracketed());
|
|
2936
|
+
else {
|
|
2937
|
+
markExpected();
|
|
2938
|
+
if (!isLookahead("primitiveExpression")) throw getNextUnexpectedError();
|
|
2939
|
+
pushOperand(parsePrimitiveExpression());
|
|
2940
|
+
}
|
|
2941
|
+
while (isLookahead("expressionSuffix")) {
|
|
2942
|
+
if (isLookahead("postfixSuffix") && isNextAboveJunction())
|
|
2943
|
+
pushOperator("GenPostfixOp", parsePostfixOperator());
|
|
2944
|
+
else if (isLookahead("recordSuffix") && isNextAboveJunction()) {
|
|
2945
|
+
const dot = getTokenNode(consume(DOT));
|
|
2946
|
+
convertFieldName(getToken(1));
|
|
2947
|
+
if (!isNextAboveJunction()) throw getNextUnexpectedError();
|
|
2948
|
+
reduceRecord(dot, parseIdentifier());
|
|
2949
|
+
} else if (isLookahead("applicationSuffix") && isNextAboveJunction()) {
|
|
2950
|
+
parser.expecting = "function argument";
|
|
2951
|
+
const application = parseBracketCases();
|
|
2952
|
+
if (
|
|
2953
|
+
["GenPrefixOp", "GenNonExpPrefixOp", "GenInfixOp", "GenPostfixOp", "FcnAppl"].includes(
|
|
2954
|
+
String(application.kind),
|
|
2955
|
+
)
|
|
2956
|
+
) {
|
|
2957
|
+
pushOperand(application, parser.lastOperator);
|
|
2958
|
+
if (getStack().length !== 1) reduceStack();
|
|
2959
|
+
} else pushOperand(application);
|
|
2960
|
+
} else throw getNextUnexpectedError();
|
|
2961
|
+
}
|
|
2962
|
+
if (isLookahead("expressionContinuation")) {
|
|
2963
|
+
if (isLookahead("infixContinuation") && isNextAboveJunction()) {
|
|
2964
|
+
pushOperator("GenInfixOp", parseInfixOperator());
|
|
2965
|
+
while (isLookahead("nextLeadingOperator") && isNextAboveJunction()) parseLeadingOperator();
|
|
2966
|
+
if (isLookahead("nextOpenExpression") && isNextAboveJunction())
|
|
2967
|
+
pushOperand(parseOpenExpression());
|
|
2968
|
+
else if (isNextAboveJunction()) parseExtendableExpression();
|
|
2969
|
+
else throw getNextUnexpectedError();
|
|
2970
|
+
} else if (isLookahead("labelContinuation") && isNextAboveJunction()) {
|
|
2971
|
+
const colons = getTokenNode(consume(COLONCOLON));
|
|
2972
|
+
const label = getStack().at(-1)?.node;
|
|
2973
|
+
if (!isLabel(label))
|
|
2974
|
+
throw getParseError(`\`::' at ${formatLocation(colons)} does not follow a label.`);
|
|
2975
|
+
getStack().pop();
|
|
2976
|
+
const labeled = parseExpression();
|
|
2977
|
+
const stackOperator = getStack().at(-1)?.operator;
|
|
2978
|
+
if (
|
|
2979
|
+
(labeled.kind === "InfixExpr" || labeled.kind === "PostfixExpr") &&
|
|
2980
|
+
getStack().length > 0 &&
|
|
2981
|
+
stackOperator !== undefined &&
|
|
2982
|
+
parser.lastOperator !== undefined &&
|
|
2983
|
+
!isPreceding(stackOperator, parser.lastOperator)
|
|
2984
|
+
) {
|
|
2985
|
+
throw getParseError(
|
|
2986
|
+
`Removing label at ${formatLocation(label ?? getNode("Missing"))} would change expression parsing.`,
|
|
2987
|
+
);
|
|
2988
|
+
}
|
|
2989
|
+
pushOperand(getNode("Label", [label ?? getNode("Missing"), colons, labeled]));
|
|
2990
|
+
} else throw getNextUnexpectedError();
|
|
2991
|
+
}
|
|
2992
|
+
endProduction();
|
|
2993
|
+
};
|
|
2994
|
+
|
|
2995
|
+
const parseStepExpression = () => {
|
|
2996
|
+
const step = consumeOneOf([PROOF_STEP, PROOF_IMPLICIT_STEP]);
|
|
2997
|
+
const node = getTokenNode(step);
|
|
2998
|
+
if (getProofLevel() < 0 && parser.proofDepth <= 0)
|
|
2999
|
+
throw getParseError(`${formatLocation(node)}: Step number used outside proof.`);
|
|
3000
|
+
if (step.image[1] === "+")
|
|
3001
|
+
throw getParseError(`${formatLocation(node)}: <+> step number used in an expression.`);
|
|
3002
|
+
return node;
|
|
3003
|
+
};
|
|
3004
|
+
|
|
3005
|
+
const parsePrimitiveName = () => {
|
|
3006
|
+
const kind = getNextKind();
|
|
3007
|
+
if (kind === IDENTIFIER) return parseIdentifier();
|
|
3008
|
+
if (kind === PROOF_STEP || kind === PROOF_IMPLICIT_STEP) return parseStepExpression();
|
|
3009
|
+
if (INFIX_OPERATORS.includes(kind)) return parseInfixOperator();
|
|
3010
|
+
if (POSTFIX_OPERATORS.includes(kind)) return parsePostfixOperator();
|
|
3011
|
+
markExpected();
|
|
3012
|
+
if (!isLookahead("negativeOperator")) throw getNextUnexpectedError();
|
|
3013
|
+
return parseNonExpressionPrefixOperator();
|
|
3014
|
+
};
|
|
3015
|
+
|
|
3016
|
+
const parsePrimitiveExpression = () =>
|
|
3017
|
+
inProduction("Primitive expression", () => {
|
|
3018
|
+
if (isLookahead("stringLiteral") && isNextAboveJunction()) {
|
|
3019
|
+
return inProduction("String", () => getTokenNode(consume(STRING_LITERAL), "String"));
|
|
3020
|
+
}
|
|
3021
|
+
if (isLookahead("numberLiteral") && isNextAboveJunction()) return parseNumber();
|
|
3022
|
+
if (!PRIMITIVE_STARTS.has(getNextKind())) {
|
|
3023
|
+
markExpected();
|
|
3024
|
+
throw getNextUnexpectedError();
|
|
3025
|
+
}
|
|
3026
|
+
const name = parsePrimitiveName();
|
|
3027
|
+
const operatorArguments = isLookahead("primitiveArguments")
|
|
3028
|
+
? parseOperatorArguments()
|
|
3029
|
+
: undefined;
|
|
3030
|
+
while (isNextKind(BANG)) addHeir(parseBangExtension());
|
|
3031
|
+
const bangs = getLastHeirs();
|
|
3032
|
+
if (bangs === undefined) {
|
|
3033
|
+
const identifier = getNode("GeneralId", [getNode("IdPrefix", []), name]);
|
|
3034
|
+
return operatorArguments
|
|
3035
|
+
? getNode("OpApplication", [identifier, operatorArguments])
|
|
3036
|
+
: identifier;
|
|
3037
|
+
}
|
|
3038
|
+
const elements = [
|
|
3039
|
+
getNode("IdPrefixElement", [name], operatorArguments, [bangs[0].heirs[0]]),
|
|
3040
|
+
...bangs
|
|
3041
|
+
.slice(0, -1)
|
|
3042
|
+
.map((bang, index) =>
|
|
3043
|
+
getNode("IdPrefixElement", bang.heirs.slice(1), [bangs[index + 1].heirs[0]]),
|
|
3044
|
+
),
|
|
3045
|
+
];
|
|
3046
|
+
const lastBang = bangs[bangs.length - 1].heirs;
|
|
3047
|
+
const identifier = getNode("GeneralId", [getNode("IdPrefix", elements), lastBang[1]]);
|
|
3048
|
+
return lastBang.length === 2
|
|
3049
|
+
? identifier
|
|
3050
|
+
: getNode("OpApplication", [identifier, lastBang[2]]);
|
|
3051
|
+
});
|
|
3052
|
+
|
|
3053
|
+
const parseBangExtension = () => {
|
|
3054
|
+
beginProduction("Bang Extension");
|
|
3055
|
+
addToken(BANG);
|
|
3056
|
+
if (isLookahead("bangOperand")) {
|
|
3057
|
+
if (isLookahead("bangIdentifier") && getToken(1).image !== "@") addHeir(parseIdentifier());
|
|
3058
|
+
else {
|
|
3059
|
+
const kind = getNextKind();
|
|
3060
|
+
if (NON_EXPRESSION_PREFIX_OPERATORS.includes(kind))
|
|
3061
|
+
addHeir(parseNonExpressionPrefixOperator());
|
|
3062
|
+
else if (INFIX_OPERATORS.includes(kind)) addHeir(parseInfixOperator());
|
|
3063
|
+
else if (POSTFIX_OPERATORS.includes(kind)) addHeir(parsePostfixOperator());
|
|
3064
|
+
else {
|
|
3065
|
+
markExpected();
|
|
3066
|
+
throw getNextUnexpectedError();
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
if (isLookahead("bangArguments")) addHeir(parseOperatorArguments());
|
|
3070
|
+
} else if (getNextKind() === LBR) addHeir(parseOperatorArguments());
|
|
3071
|
+
else {
|
|
3072
|
+
markExpected();
|
|
3073
|
+
if (!isLookahead("structureOperator")) throw getNextUnexpectedError();
|
|
3074
|
+
addHeir(parseStructureOperator());
|
|
3075
|
+
}
|
|
3076
|
+
const node = getNode("IdPrefixElement", getLastHeirs());
|
|
3077
|
+
endProduction();
|
|
3078
|
+
return node;
|
|
3079
|
+
};
|
|
3080
|
+
|
|
3081
|
+
const parseStructureOperator = () => {
|
|
3082
|
+
const operator = inProduction("StructOp", () => {
|
|
3083
|
+
parser.expecting = "`<<' , `>>' , `:' , `@' , or number";
|
|
3084
|
+
const kind = getNextKind();
|
|
3085
|
+
if (kind === LAB || kind === RAB || kind === COLON) {
|
|
3086
|
+
return getTokenNode(consume(kind), "StructOp");
|
|
3087
|
+
}
|
|
3088
|
+
if (kind === NUMBER_LITERAL) return parseNumber();
|
|
3089
|
+
markExpected();
|
|
3090
|
+
if (getToken(1).image !== "@") throw getNextUnexpectedError();
|
|
3091
|
+
return getTokenNode(consume(IDENTIFIER), "StructOp");
|
|
3092
|
+
});
|
|
3093
|
+
if (operator.kind === "Real")
|
|
3094
|
+
throw getParseError(`Illegal structural term at ${formatLocation(operator)}`);
|
|
3095
|
+
return operator.kind === "Number" ? getNode("StructOp", [operator]) : operator;
|
|
3096
|
+
};
|
|
3097
|
+
|
|
3098
|
+
const parseCompilationUnit = () => {
|
|
3099
|
+
insertDefinitionBreak();
|
|
3100
|
+
if (isNextKind(BEGIN_PRAGMA)) {
|
|
3101
|
+
consume(BEGIN_PRAGMA);
|
|
3102
|
+
for (;;) {
|
|
3103
|
+
const kind = getNextKind();
|
|
3104
|
+
if (kind !== NUMBER && kind !== IDENTIFIER) {
|
|
3105
|
+
markExpected();
|
|
3106
|
+
break;
|
|
3107
|
+
}
|
|
3108
|
+
consume(kind);
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
parseModuleDefinition();
|
|
3112
|
+
};
|
|
3113
|
+
|
|
3114
|
+
try {
|
|
3115
|
+
parseCompilationUnit();
|
|
3116
|
+
} catch (error) {
|
|
3117
|
+
if (!(error instanceof TlaParseError)) throw error;
|
|
3118
|
+
errors.push(error.message);
|
|
3119
|
+
}
|
|
3120
|
+
return errors.length === 0 ? undefined : errors.join("\n");
|
|
3121
|
+
};
|
|
3122
|
+
|
|
3123
|
+
// Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
|
|
3124
|
+
// does in TLC: SANY's message, then a fatal error that names the module.
|
|
3125
|
+
export const parseModuleFile = (source: string, name: string) => {
|
|
3126
|
+
try {
|
|
3127
|
+
return parseModule(source);
|
|
3128
|
+
} catch (error) {
|
|
3129
|
+
if (!(error instanceof TlaSyntaxError)) throw error;
|
|
3130
|
+
const parseError = getSanyParseError(source);
|
|
3131
|
+
if (parseError === undefined) throw error;
|
|
3132
|
+
throw createSanyError(
|
|
3133
|
+
[
|
|
3134
|
+
{
|
|
3135
|
+
location: `In module ${name}`,
|
|
3136
|
+
message: `Could not parse module ${name} from file ${name}.tla`,
|
|
3137
|
+
},
|
|
3138
|
+
],
|
|
3139
|
+
true,
|
|
3140
|
+
parseError,
|
|
3141
|
+
);
|
|
3142
|
+
}
|
|
3143
|
+
};
|