everystate 0.0.0-stage → 0.1.0
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/CONFORMANCE.md +64 -0
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/dist/bundled.d.ts +1 -0
- package/dist/bundled.js +2 -0
- package/dist/cfg.d.ts +24 -0
- package/dist/cfg.js +252 -0
- package/dist/checker.d.ts +96 -0
- package/dist/checker.js +520 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +4 -0
- package/dist/community.d.ts +17 -0
- package/dist/community.js +1381 -0
- package/dist/entry.d.ts +2 -0
- package/dist/entry.js +39 -0
- package/dist/eval.d.ts +119 -0
- package/dist/eval.js +1533 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +24 -0
- package/dist/java.d.ts +2 -0
- package/dist/java.js +6 -0
- package/dist/lexer.d.ts +17 -0
- package/dist/lexer.js +255 -0
- package/dist/liveness.d.ts +80 -0
- package/dist/liveness.js +855 -0
- package/dist/main.d.ts +40 -0
- package/dist/main.js +494 -0
- package/dist/model.d.ts +56 -0
- package/dist/model.js +582 -0
- package/dist/modules.d.ts +91 -0
- package/dist/modules.js +649 -0
- package/dist/node-host.d.ts +2 -0
- package/dist/node-host.js +25 -0
- package/dist/parser.d.ts +30 -0
- package/dist/parser.js +1114 -0
- package/dist/pprint.d.ts +7 -0
- package/dist/pprint.js +647 -0
- package/dist/stdlib.d.ts +14 -0
- package/dist/stdlib.js +588 -0
- package/dist/values.d.ts +150 -0
- package/dist/values.js +822 -0
- package/package.json +51 -4
package/dist/parser.js
ADDED
|
@@ -0,0 +1,1114 @@
|
|
|
1
|
+
// TLA+ parser: tokens -> AST. Hand-written recursive descent with precedence climbing and
|
|
2
|
+
// column-sensitive junction lists.
|
|
3
|
+
import { lex, TlaError } from "./lexer.js";
|
|
4
|
+
import { strId } from "./values.js";
|
|
5
|
+
// [lo, hi, leftAssoc]
|
|
6
|
+
const INFIX = {
|
|
7
|
+
'=>': [1, 1, false], '-+->': [2, 2, false], '<=>': [2, 2, false], '~>': [2, 2, false],
|
|
8
|
+
'\\/': [3, 3, true], '/\\': [3, 3, true],
|
|
9
|
+
'\\cdot': [5, 14, true], '@@': [6, 6, true], ':>': [7, 7, false], '<:': [7, 7, false],
|
|
10
|
+
'\\': [8, 8, false], '\\cap': [8, 8, true], '\\cup': [8, 8, true],
|
|
11
|
+
'..': [9, 9, false], '...': [9, 9, false],
|
|
12
|
+
'!!': [9, 13, false], '##': [9, 13, true], '$': [9, 13, true], '$$': [9, 13, true], '??': [9, 13, true],
|
|
13
|
+
'\\sqcap': [9, 13, true], '\\sqcup': [9, 13, true], '\\uplus': [9, 13, true], '\\wr': [9, 14, false],
|
|
14
|
+
'\\oplus': [10, 10, true], '+': [10, 10, true], '++': [10, 10, true],
|
|
15
|
+
'%': [10, 11, false], '%%': [10, 11, true], '|': [10, 11, true], '||': [10, 11, true],
|
|
16
|
+
'\\X': [10, 13, false],
|
|
17
|
+
'-': [11, 11, true], '--': [11, 11, true], '\\ominus': [11, 11, true],
|
|
18
|
+
'&': [13, 13, true], '&&': [13, 13, true], '*': [13, 13, true], '**': [13, 13, true], '/': [13, 13, false],
|
|
19
|
+
'//': [13, 13, false], '\\div': [13, 13, false], '\\odot': [13, 13, true], '\\oslash': [13, 13, false],
|
|
20
|
+
'\\otimes': [13, 13, true], '\\bigcirc': [13, 13, true], '\\bullet': [13, 13, true], '\\o': [13, 13, true],
|
|
21
|
+
'\\star': [13, 13, true], '^': [14, 14, false], '^^': [14, 14, false],
|
|
22
|
+
};
|
|
23
|
+
for (const r of ['=', '/=', '<', '>', '<=', '>=', '\\in', '\\notin', '\\subseteq', '\\subset', '\\supseteq',
|
|
24
|
+
'\\supset', '\\prec', '\\preceq', '\\succ', '\\succeq', '\\ll', '\\gg', '\\sim', '\\simeq', '\\approx',
|
|
25
|
+
'\\cong', '\\doteq', '\\asymp', '\\propto', '\\sqsubset', '\\sqsubseteq', '\\sqsupset', '\\sqsupseteq',
|
|
26
|
+
'::=', ':=', '|-', '|=', '-|', '=|'])
|
|
27
|
+
INFIX[r] = [5, 5, false];
|
|
28
|
+
const PREFIX = {
|
|
29
|
+
'~': [4, 4], '[]': [4, 15], '<>': [4, 15], 'ENABLED': [4, 15], 'UNCHANGED': [4, 15],
|
|
30
|
+
'SUBSET': [8, 8], 'UNION': [8, 8], 'DOMAIN': [8, 8], '-': [12, 12],
|
|
31
|
+
};
|
|
32
|
+
const POSTFIX = new Set(["'", '^+', '^*', '^#']);
|
|
33
|
+
// Operators with fixed (non user-definable) meaning get their own node kinds.
|
|
34
|
+
const FIXED = {
|
|
35
|
+
'/\\': 'and', '\\/': 'or', '~': 'not', '=>': 'imp', '<=>': 'eqv', '=': 'eq', '/=': 'ne', '\\in': 'in',
|
|
36
|
+
'\\notin': 'notin', '\\subseteq': 'subseteq', '\\cup': 'cup', '\\cap': 'cap', '\\': 'diff',
|
|
37
|
+
'SUBSET': 'powerset', 'UNION': 'bigunion', 'DOMAIN': 'domain', "'": 'prime', 'ENABLED': 'enabled',
|
|
38
|
+
'UNCHANGED': 'unchanged', '[]': 'always', '<>': 'eventually', '~>': 'leadsto', '-+->': 'guarantee',
|
|
39
|
+
'\\cdot': 'cdot',
|
|
40
|
+
};
|
|
41
|
+
const THEOREM_KW = new Set(['THEOREM', 'LEMMA', 'PROPOSITION', 'COROLLARY']);
|
|
42
|
+
const UNIT_START_KW = new Set(['VARIABLE', 'VARIABLES', 'CONSTANT', 'CONSTANTS', 'RECURSIVE', 'LOCAL', 'INSTANCE',
|
|
43
|
+
'ASSUME', 'ASSUMPTION', 'AXIOM', 'THEOREM', 'LEMMA', 'PROPOSITION', 'COROLLARY', 'USE', 'HIDE']);
|
|
44
|
+
class Parser {
|
|
45
|
+
toks;
|
|
46
|
+
p = 0;
|
|
47
|
+
js = []; // junction column stack; -1 = unconstrained
|
|
48
|
+
last;
|
|
49
|
+
mod;
|
|
50
|
+
noArgs = false;
|
|
51
|
+
constructor(toks, mod) {
|
|
52
|
+
this.toks = toks;
|
|
53
|
+
this.last = toks[0];
|
|
54
|
+
this.mod = mod;
|
|
55
|
+
}
|
|
56
|
+
static STOP = { t: 'eof', v: '<end of junction item>', line: 0, col: 0, ec: 0 };
|
|
57
|
+
raw() { return this.toks[this.p]; }
|
|
58
|
+
peek() {
|
|
59
|
+
const t = this.toks[this.p];
|
|
60
|
+
const n = this.js.length;
|
|
61
|
+
if (n > 0 && t.col <= this.js[n - 1] && t.t !== 'eof')
|
|
62
|
+
return Parser.STOP;
|
|
63
|
+
return t;
|
|
64
|
+
}
|
|
65
|
+
peekAt(k) { return this.toks[Math.min(this.p + k, this.toks.length - 1)]; }
|
|
66
|
+
next() {
|
|
67
|
+
const t = this.peek();
|
|
68
|
+
if (t === Parser.STOP)
|
|
69
|
+
this.fail('Unexpected end of junction list item');
|
|
70
|
+
this.last = t;
|
|
71
|
+
this.p++;
|
|
72
|
+
return t;
|
|
73
|
+
}
|
|
74
|
+
isOp(v) { const t = this.peek(); return t.t === 'op' && t.v === v; }
|
|
75
|
+
isKw(v) { const t = this.peek(); return t.t === 'kw' && t.v === v; }
|
|
76
|
+
eatOp(v) { if (this.isOp(v)) {
|
|
77
|
+
this.next();
|
|
78
|
+
return true;
|
|
79
|
+
} return false; }
|
|
80
|
+
eatKw(v) { if (this.isKw(v)) {
|
|
81
|
+
this.next();
|
|
82
|
+
return true;
|
|
83
|
+
} return false; }
|
|
84
|
+
expectOp(v) { if (!this.isOp(v))
|
|
85
|
+
this.fail(`Expected '${v}'`); return this.next(); }
|
|
86
|
+
expectKw(v) { if (!this.isKw(v))
|
|
87
|
+
this.fail(`Expected ${v}`); return this.next(); }
|
|
88
|
+
expectId() { const t = this.peek(); if (t.t !== 'id')
|
|
89
|
+
this.fail('Expected identifier'); return this.next(); }
|
|
90
|
+
fail(msg) {
|
|
91
|
+
const t = this.raw();
|
|
92
|
+
throw new TlaError(`${msg} but found '${t.v}' at line ${t.line}, col ${t.col} of module ${this.mod}`, [t.line, t.col, t.line, t.ec], this.mod, 'parse');
|
|
93
|
+
}
|
|
94
|
+
node(k, start, props = {}) {
|
|
95
|
+
const bl = 'loc' in start ? start.loc[0] : start.line;
|
|
96
|
+
const bc = 'loc' in start ? start.loc[1] : start.col;
|
|
97
|
+
return { k, loc: [bl, bc, this.last.line, this.last.ec], ...props };
|
|
98
|
+
}
|
|
99
|
+
// ---------- module level ----------
|
|
100
|
+
parseModule() {
|
|
101
|
+
const start = this.raw();
|
|
102
|
+
if (start.t !== 'sep')
|
|
103
|
+
this.fail('Expected module header');
|
|
104
|
+
this.next();
|
|
105
|
+
this.expectKw('MODULE');
|
|
106
|
+
const name = this.expectId().v;
|
|
107
|
+
this.mod = name;
|
|
108
|
+
if (this.raw().t !== 'sep')
|
|
109
|
+
this.fail('Expected ---- after module name');
|
|
110
|
+
this.next();
|
|
111
|
+
const ext = [];
|
|
112
|
+
if (this.eatKw('EXTENDS')) {
|
|
113
|
+
do
|
|
114
|
+
ext.push(this.expectId().v);
|
|
115
|
+
while (this.eatOp(','));
|
|
116
|
+
}
|
|
117
|
+
const units = [];
|
|
118
|
+
for (;;) {
|
|
119
|
+
const t = this.raw();
|
|
120
|
+
if (t.t === 'end') {
|
|
121
|
+
this.next();
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
if (t.t === 'eof')
|
|
125
|
+
this.fail('Unexpected end of file; module not terminated by ====');
|
|
126
|
+
if (t.t === 'sep') {
|
|
127
|
+
if (this.peekAt(1).t === 'kw' && this.peekAt(1).v === 'MODULE') {
|
|
128
|
+
const inner = this.parseModule();
|
|
129
|
+
this.mod = name;
|
|
130
|
+
units.push(inner);
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
this.next();
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const u = this.parseUnit();
|
|
137
|
+
if (u)
|
|
138
|
+
units.push(u);
|
|
139
|
+
}
|
|
140
|
+
return this.node('module', start, { name, extends: ext, units });
|
|
141
|
+
}
|
|
142
|
+
parseUnit() {
|
|
143
|
+
const t = this.peek();
|
|
144
|
+
if (t.t === 'kw') {
|
|
145
|
+
switch (t.v) {
|
|
146
|
+
case 'VARIABLE':
|
|
147
|
+
case 'VARIABLES': {
|
|
148
|
+
this.next();
|
|
149
|
+
const names = [];
|
|
150
|
+
do
|
|
151
|
+
names.push(this.expectId().v);
|
|
152
|
+
while (this.eatOp(','));
|
|
153
|
+
return this.node('variables', t, { names });
|
|
154
|
+
}
|
|
155
|
+
case 'CONSTANT':
|
|
156
|
+
case 'CONSTANTS': {
|
|
157
|
+
this.next();
|
|
158
|
+
const decls = [];
|
|
159
|
+
do
|
|
160
|
+
decls.push(this.parseOpDecl());
|
|
161
|
+
while (this.eatOp(','));
|
|
162
|
+
return this.node('constants', t, { decls });
|
|
163
|
+
}
|
|
164
|
+
case 'RECURSIVE': {
|
|
165
|
+
this.next();
|
|
166
|
+
const decls = [];
|
|
167
|
+
do
|
|
168
|
+
decls.push(this.parseOpDecl());
|
|
169
|
+
while (this.eatOp(','));
|
|
170
|
+
return this.node('recursive', t, { decls });
|
|
171
|
+
}
|
|
172
|
+
case 'LOCAL': {
|
|
173
|
+
this.next();
|
|
174
|
+
if (this.isKw('INSTANCE')) {
|
|
175
|
+
const i = this.parseInstance();
|
|
176
|
+
i.local = true;
|
|
177
|
+
return i;
|
|
178
|
+
}
|
|
179
|
+
const d = this.parseDef();
|
|
180
|
+
d.local = true;
|
|
181
|
+
return d;
|
|
182
|
+
}
|
|
183
|
+
case 'INSTANCE': return this.parseInstance();
|
|
184
|
+
case 'ASSUME':
|
|
185
|
+
case 'ASSUMPTION':
|
|
186
|
+
case 'AXIOM': {
|
|
187
|
+
this.next();
|
|
188
|
+
let name = null;
|
|
189
|
+
if (this.peek().t === 'id' && this.peekAt(1).t === 'op' && this.peekAt(1).v === '==') {
|
|
190
|
+
name = this.next().v;
|
|
191
|
+
this.next();
|
|
192
|
+
}
|
|
193
|
+
const e = this.parseExpr();
|
|
194
|
+
return this.node('assume', t, { name, e });
|
|
195
|
+
}
|
|
196
|
+
case 'USE':
|
|
197
|
+
case 'HIDE':
|
|
198
|
+
this.next();
|
|
199
|
+
this.parseUseBody();
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
if (THEOREM_KW.has(t.v)) {
|
|
203
|
+
this.next();
|
|
204
|
+
let name = null;
|
|
205
|
+
if (this.peek().t === 'id' && this.peekAt(1).t === 'op' && this.peekAt(1).v === '==') {
|
|
206
|
+
name = this.next().v;
|
|
207
|
+
this.next();
|
|
208
|
+
}
|
|
209
|
+
const e = this.isKw('ASSUME') ? this.parseAssumeProve() : this.parseExpr();
|
|
210
|
+
const th = this.node('theorem', t, { name, e });
|
|
211
|
+
this.parseProofOpt();
|
|
212
|
+
return th;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return this.parseDef();
|
|
216
|
+
}
|
|
217
|
+
parseOpDecl() {
|
|
218
|
+
const t = this.peek();
|
|
219
|
+
if (t.t === 'id') {
|
|
220
|
+
this.next();
|
|
221
|
+
let arity = 0;
|
|
222
|
+
if (this.eatOp('(')) {
|
|
223
|
+
do {
|
|
224
|
+
this.expectOp('_');
|
|
225
|
+
arity++;
|
|
226
|
+
} while (this.eatOp(','));
|
|
227
|
+
this.expectOp(')');
|
|
228
|
+
}
|
|
229
|
+
return { name: t.v, arity };
|
|
230
|
+
}
|
|
231
|
+
if (t.t === 'op' && t.v === '_') {
|
|
232
|
+
this.next();
|
|
233
|
+
const op = this.next();
|
|
234
|
+
if (this.eatOp('_'))
|
|
235
|
+
return { name: op.v, arity: 2 };
|
|
236
|
+
return { name: op.v, arity: 1 }; // postfix
|
|
237
|
+
}
|
|
238
|
+
if (t.t === 'op' || t.t === 'kw') {
|
|
239
|
+
this.next();
|
|
240
|
+
this.expectOp('_');
|
|
241
|
+
return { name: t.v === '-' ? '-.' : t.v, arity: 1 };
|
|
242
|
+
}
|
|
243
|
+
this.fail('Expected operator declaration');
|
|
244
|
+
}
|
|
245
|
+
parseInstance() {
|
|
246
|
+
const t = this.expectKw('INSTANCE');
|
|
247
|
+
const module = this.expectId().v;
|
|
248
|
+
const substs = [];
|
|
249
|
+
if (this.eatKw('WITH')) {
|
|
250
|
+
do {
|
|
251
|
+
const nt = this.next();
|
|
252
|
+
if (nt.t !== 'id' && nt.t !== 'op')
|
|
253
|
+
this.fail('Expected substitution target');
|
|
254
|
+
this.expectOp('<-');
|
|
255
|
+
substs.push({ name: nt.v, e: this.parseOpArg() });
|
|
256
|
+
} while (this.eatOp(','));
|
|
257
|
+
}
|
|
258
|
+
return this.node('instance', t, { module, substs, local: false, name: null, params: [] });
|
|
259
|
+
}
|
|
260
|
+
// Definition: operator, function, or module (instance) definition.
|
|
261
|
+
parseDef() {
|
|
262
|
+
const t = this.peek();
|
|
263
|
+
let name;
|
|
264
|
+
let params = [];
|
|
265
|
+
let fnBounds = null;
|
|
266
|
+
const nx = this.peekAt(1);
|
|
267
|
+
if (t.t === 'op' && (PREFIX[t.v] || t.v === '-.') && nx.t === 'id') {
|
|
268
|
+
// prefix operator definition
|
|
269
|
+
this.next();
|
|
270
|
+
name = t.v === '-' ? '-.' : t.v;
|
|
271
|
+
params = [{ name: this.expectId().v, arity: 0 }];
|
|
272
|
+
}
|
|
273
|
+
else if (t.t === 'kw' && PREFIX[t.v] && nx.t === 'id') {
|
|
274
|
+
this.fail(`Cannot redefine ${t.v}`);
|
|
275
|
+
}
|
|
276
|
+
else if (t.t === 'id') {
|
|
277
|
+
this.next();
|
|
278
|
+
name = t.v;
|
|
279
|
+
const n1 = this.peek();
|
|
280
|
+
if (n1.t === 'op' && n1.v === '(') {
|
|
281
|
+
this.next();
|
|
282
|
+
do
|
|
283
|
+
params.push(this.parseOpDecl());
|
|
284
|
+
while (this.eatOp(','));
|
|
285
|
+
this.expectOp(')');
|
|
286
|
+
}
|
|
287
|
+
else if (n1.t === 'op' && n1.v === '[') {
|
|
288
|
+
this.next();
|
|
289
|
+
this.js.push(-1);
|
|
290
|
+
fnBounds = this.parseBounds();
|
|
291
|
+
this.js.pop();
|
|
292
|
+
this.expectOp(']');
|
|
293
|
+
}
|
|
294
|
+
else if (n1.t === 'op' && INFIX[n1.v] && this.peekAt(1).t === 'id') {
|
|
295
|
+
this.next();
|
|
296
|
+
params = [{ name: t.v, arity: 0 }, { name: this.expectId().v, arity: 0 }];
|
|
297
|
+
name = n1.v;
|
|
298
|
+
}
|
|
299
|
+
else if (n1.t === 'op' && POSTFIX.has(n1.v)) {
|
|
300
|
+
this.next();
|
|
301
|
+
params = [{ name: t.v, arity: 0 }];
|
|
302
|
+
name = n1.v;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else
|
|
306
|
+
this.fail('Expected a definition or declaration');
|
|
307
|
+
const defTok = this.expectOp('==');
|
|
308
|
+
if (this.isKw('INSTANCE')) {
|
|
309
|
+
const inst = this.parseInstance();
|
|
310
|
+
inst.name = name;
|
|
311
|
+
inst.params = params;
|
|
312
|
+
inst.loc = [t.line, t.col, this.last.line, this.last.ec];
|
|
313
|
+
return inst;
|
|
314
|
+
}
|
|
315
|
+
void defTok;
|
|
316
|
+
let body = this.parseExpr();
|
|
317
|
+
if (fnBounds)
|
|
318
|
+
body = { k: 'fndef', loc: body.loc, bounds: fnBounds, body, recname: name };
|
|
319
|
+
return this.node('def', t, { name, params, body, local: false });
|
|
320
|
+
}
|
|
321
|
+
// ---------- proofs (parsed and discarded) ----------
|
|
322
|
+
parseAssumeProve() {
|
|
323
|
+
const t = this.expectKw('ASSUME');
|
|
324
|
+
do {
|
|
325
|
+
if (this.isKw('ASSUME')) {
|
|
326
|
+
this.parseAssumeProve();
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (this.isKw('NEW') || this.isKw('CONSTANT') || this.isKw('VARIABLE') || this.isKw('STATE') ||
|
|
330
|
+
this.isKw('ACTION') || this.isKw('TEMPORAL')) {
|
|
331
|
+
this.eatKw('NEW');
|
|
332
|
+
const lvl = this.peek();
|
|
333
|
+
if (lvl.t === 'kw' && ['CONSTANT', 'VARIABLE', 'STATE', 'ACTION', 'TEMPORAL'].includes(lvl.v))
|
|
334
|
+
this.next();
|
|
335
|
+
this.parseOpDecl();
|
|
336
|
+
if (this.eatOp('\\in'))
|
|
337
|
+
this.parseExpr();
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
this.parseExpr();
|
|
341
|
+
} while (this.eatOp(','));
|
|
342
|
+
this.expectKw('PROVE');
|
|
343
|
+
const e = this.parseExpr();
|
|
344
|
+
return this.node('assumeprove', t, { e });
|
|
345
|
+
}
|
|
346
|
+
parseUseBody() {
|
|
347
|
+
// [ONLY] facts [DEF(S) names]
|
|
348
|
+
this.eatKw('ONLY');
|
|
349
|
+
const isDef = () => this.isKw('DEF') || this.isKw('DEFS');
|
|
350
|
+
if (!isDef()) {
|
|
351
|
+
do {
|
|
352
|
+
if (this.eatKw('MODULE')) {
|
|
353
|
+
this.expectId();
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
this.parseExpr();
|
|
357
|
+
} while (this.eatOp(','));
|
|
358
|
+
}
|
|
359
|
+
if (isDef()) {
|
|
360
|
+
this.next();
|
|
361
|
+
do {
|
|
362
|
+
if (this.eatKw('MODULE')) {
|
|
363
|
+
this.expectId();
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (this.peek().t === 'op' && this.peek().v !== '(') {
|
|
367
|
+
this.next();
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
this.parseOpArg();
|
|
371
|
+
} while (this.eatOp(','));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
parseProofOpt() {
|
|
375
|
+
const t = this.peek();
|
|
376
|
+
if (t.t === 'kw') {
|
|
377
|
+
if (t.v === 'PROOF') {
|
|
378
|
+
this.next();
|
|
379
|
+
this.parseProofOpt();
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (t.v === 'OBVIOUS' || t.v === 'OMITTED') {
|
|
383
|
+
this.next();
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (t.v === 'BY') {
|
|
387
|
+
this.next();
|
|
388
|
+
this.parseUseBody();
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (t.t === 'step')
|
|
393
|
+
this.parseSteps();
|
|
394
|
+
}
|
|
395
|
+
stepLevel(t) { return /^<(\d+|\*|\+)>/.exec(t.v)[1]; }
|
|
396
|
+
parseSteps() {
|
|
397
|
+
// All steps at this level until QED.
|
|
398
|
+
for (;;) {
|
|
399
|
+
const t = this.peek();
|
|
400
|
+
if (t.t !== 'step')
|
|
401
|
+
return;
|
|
402
|
+
this.next();
|
|
403
|
+
const k = this.peek();
|
|
404
|
+
if (k.t === 'kw') {
|
|
405
|
+
switch (k.v) {
|
|
406
|
+
case 'QED':
|
|
407
|
+
this.next();
|
|
408
|
+
this.parseProofOpt();
|
|
409
|
+
return;
|
|
410
|
+
case 'USE':
|
|
411
|
+
case 'HIDE':
|
|
412
|
+
this.next();
|
|
413
|
+
this.parseUseBody();
|
|
414
|
+
continue;
|
|
415
|
+
case 'DEFINE':
|
|
416
|
+
this.next();
|
|
417
|
+
this.parseStepDefs();
|
|
418
|
+
continue;
|
|
419
|
+
case 'HAVE':
|
|
420
|
+
this.next();
|
|
421
|
+
this.parseExpr();
|
|
422
|
+
continue;
|
|
423
|
+
case 'WITNESS':
|
|
424
|
+
this.next();
|
|
425
|
+
do
|
|
426
|
+
this.parseExpr();
|
|
427
|
+
while (this.eatOp(','));
|
|
428
|
+
continue;
|
|
429
|
+
case 'TAKE':
|
|
430
|
+
this.next();
|
|
431
|
+
this.parseTakeBounds();
|
|
432
|
+
continue;
|
|
433
|
+
case 'PICK':
|
|
434
|
+
this.next();
|
|
435
|
+
this.parseTakeBounds();
|
|
436
|
+
this.expectOp(':');
|
|
437
|
+
this.parseExpr();
|
|
438
|
+
this.parseProofOpt();
|
|
439
|
+
continue;
|
|
440
|
+
case 'CASE':
|
|
441
|
+
this.next();
|
|
442
|
+
this.parseExpr();
|
|
443
|
+
this.parseProofOpt();
|
|
444
|
+
continue;
|
|
445
|
+
case 'SUFFICES':
|
|
446
|
+
this.next();
|
|
447
|
+
if (this.isKw('ASSUME'))
|
|
448
|
+
this.parseAssumeProve();
|
|
449
|
+
else
|
|
450
|
+
this.parseExpr();
|
|
451
|
+
this.parseProofOpt();
|
|
452
|
+
continue;
|
|
453
|
+
case 'ASSUME':
|
|
454
|
+
this.parseAssumeProve();
|
|
455
|
+
this.parseProofOpt();
|
|
456
|
+
continue;
|
|
457
|
+
case 'INSTANCE':
|
|
458
|
+
this.parseInstance();
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// definition step without DEFINE, or an assertion
|
|
463
|
+
if (this.looksLikeDef()) {
|
|
464
|
+
this.parseStepDefs();
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
this.parseExpr();
|
|
468
|
+
this.parseProofOpt();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
looksLikeDef() {
|
|
472
|
+
const save = this.p, saveLast = this.last, js = this.js.slice();
|
|
473
|
+
try {
|
|
474
|
+
const t = this.peek();
|
|
475
|
+
if (t.t !== 'id' && !(t.t === 'op' && PREFIX[t.v]))
|
|
476
|
+
return false;
|
|
477
|
+
this.parseDef();
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
catch {
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
finally {
|
|
484
|
+
this.p = save;
|
|
485
|
+
this.last = saveLast;
|
|
486
|
+
this.js = js;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
parseStepDefs() {
|
|
490
|
+
do
|
|
491
|
+
this.parseDef();
|
|
492
|
+
while (this.peek().t === 'id' && this.looksLikeDef());
|
|
493
|
+
}
|
|
494
|
+
parseTakeBounds() {
|
|
495
|
+
// x, y | x \in S, y \in T
|
|
496
|
+
const save = this.p;
|
|
497
|
+
try {
|
|
498
|
+
const b = this.parseBounds();
|
|
499
|
+
if (b.length)
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
catch { /* fall through */ }
|
|
503
|
+
this.p = save;
|
|
504
|
+
do
|
|
505
|
+
this.expectId();
|
|
506
|
+
while (this.eatOp(','));
|
|
507
|
+
}
|
|
508
|
+
// ---------- expressions ----------
|
|
509
|
+
parseExpr() { return this.parseBin(0); }
|
|
510
|
+
parseBin(minPrec) {
|
|
511
|
+
let left = this.parsePrefix(minPrec);
|
|
512
|
+
for (;;) {
|
|
513
|
+
const t = this.peek();
|
|
514
|
+
if (t.t !== 'op')
|
|
515
|
+
break;
|
|
516
|
+
const info = INFIX[t.v];
|
|
517
|
+
if (!info)
|
|
518
|
+
break;
|
|
519
|
+
if (info[0] < minPrec)
|
|
520
|
+
break;
|
|
521
|
+
this.next();
|
|
522
|
+
const op = t.v;
|
|
523
|
+
if (op === '\\X') {
|
|
524
|
+
const items = [left, this.parseBin(info[1] + 1)];
|
|
525
|
+
while (this.isOp('\\X')) {
|
|
526
|
+
this.next();
|
|
527
|
+
items.push(this.parseBin(info[1] + 1));
|
|
528
|
+
}
|
|
529
|
+
left = this.node('cross', left, { a: items });
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
const right = this.parseBin(info[1] + 1);
|
|
533
|
+
const fixed = FIXED[op];
|
|
534
|
+
if (fixed === 'and' || fixed === 'or') {
|
|
535
|
+
if (left.k === fixed && !left.junction && !left.paren) {
|
|
536
|
+
left.a.push(right);
|
|
537
|
+
left.loc = [left.loc[0], left.loc[1], this.last.line, this.last.ec];
|
|
538
|
+
}
|
|
539
|
+
else
|
|
540
|
+
left = this.node(fixed, left, { a: [left, right] });
|
|
541
|
+
}
|
|
542
|
+
else if (fixed)
|
|
543
|
+
left = this.node(fixed, left, { a: [left, right] });
|
|
544
|
+
else
|
|
545
|
+
left = this.node('app', left, { name: op, args: [left, right] });
|
|
546
|
+
}
|
|
547
|
+
return left;
|
|
548
|
+
}
|
|
549
|
+
parsePrefix(minPrec) {
|
|
550
|
+
const t = this.peek();
|
|
551
|
+
if ((t.t === 'op' || t.t === 'kw') && PREFIX[t.v]) {
|
|
552
|
+
// "-" followed by nothing useful, or operator used as argument, are handled by parseOpArg.
|
|
553
|
+
const info = PREFIX[t.v];
|
|
554
|
+
this.next();
|
|
555
|
+
const arg = this.parseBin(info[1] + 1);
|
|
556
|
+
let e;
|
|
557
|
+
if (t.v === '-')
|
|
558
|
+
e = this.node('app', t, { name: '-.', args: [arg] });
|
|
559
|
+
else
|
|
560
|
+
e = this.node(FIXED[t.v], t, { a: [arg] });
|
|
561
|
+
return this.parsePostfixOps(e, minPrec);
|
|
562
|
+
}
|
|
563
|
+
return this.parsePostfixOps(this.parsePrimary(), minPrec);
|
|
564
|
+
}
|
|
565
|
+
parsePostfixOps(e, minPrec) {
|
|
566
|
+
for (;;) {
|
|
567
|
+
const t = this.peek();
|
|
568
|
+
if (t.t !== 'op')
|
|
569
|
+
break;
|
|
570
|
+
if (t.v === '[') {
|
|
571
|
+
this.next();
|
|
572
|
+
this.js.push(-1);
|
|
573
|
+
const args = [this.parseExpr()];
|
|
574
|
+
while (this.eatOp(','))
|
|
575
|
+
args.push(this.parseExpr());
|
|
576
|
+
this.js.pop();
|
|
577
|
+
this.expectOp(']');
|
|
578
|
+
e = this.node('fnapp', e, { f: e, args });
|
|
579
|
+
}
|
|
580
|
+
else if (t.v === '.') {
|
|
581
|
+
this.next();
|
|
582
|
+
const f = this.next();
|
|
583
|
+
if (f.t !== 'id' && f.t !== 'kw')
|
|
584
|
+
this.fail('Expected field name');
|
|
585
|
+
e = this.node('dot', e, { e, field: f.v });
|
|
586
|
+
}
|
|
587
|
+
else if (t.v === "'") {
|
|
588
|
+
this.next();
|
|
589
|
+
e = this.node('prime', e, { a: [e] });
|
|
590
|
+
}
|
|
591
|
+
else if (POSTFIX.has(t.v)) {
|
|
592
|
+
this.next();
|
|
593
|
+
e = this.node('app', e, { name: t.v, args: [e] });
|
|
594
|
+
}
|
|
595
|
+
else
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
void minPrec;
|
|
599
|
+
return e;
|
|
600
|
+
}
|
|
601
|
+
// An operator argument: expression, or a bare operator symbol (e.g. F(+, 1)), or LAMBDA.
|
|
602
|
+
parseOpArg() {
|
|
603
|
+
const t = this.peek();
|
|
604
|
+
if ((t.t === 'op' && (INFIX[t.v] || POSTFIX.has(t.v) || t.v === '-.' || PREFIX[t.v])) || (t.t === 'kw' && PREFIX[t.v])) {
|
|
605
|
+
const n = this.peekAt(1);
|
|
606
|
+
if (n.t === 'op' && (n.v === ',' || n.v === ')') || n.t === 'eof' || n.t === 'end' || n.t === 'sep' ||
|
|
607
|
+
(n.t === 'kw' && UNIT_START_KW.has(n.v))) {
|
|
608
|
+
this.next();
|
|
609
|
+
return this.opRef(t);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
return this.parseExpr();
|
|
613
|
+
}
|
|
614
|
+
// A bare operator symbol used as an operator argument. Built-in (non-definable) operators become LAMBDAs.
|
|
615
|
+
opRef(t) {
|
|
616
|
+
const fixed = FIXED[t.v];
|
|
617
|
+
if (!fixed)
|
|
618
|
+
return this.node('app', t, { name: t.v, args: [] });
|
|
619
|
+
const loc = [t.line, t.col, t.line, t.ec];
|
|
620
|
+
const arg = (name) => ({ k: 'app', loc, name, args: [] });
|
|
621
|
+
if (PREFIX[t.v] || t.v === "'")
|
|
622
|
+
return { k: 'lambda', loc, params: [{ name: '_a', arity: 0 }], body: { k: fixed, loc, a: [arg('_a')] } };
|
|
623
|
+
return { k: 'lambda', loc, params: [{ name: '_a', arity: 0 }, { name: '_b', arity: 0 }], body: { k: fixed, loc, a: [arg('_a'), arg('_b')] } };
|
|
624
|
+
}
|
|
625
|
+
// Non-fix application of an infix/postfix operator symbol: +(a, b), \in(x, S), ^+(a), -.(a)
|
|
626
|
+
prefixApply(t) {
|
|
627
|
+
this.next();
|
|
628
|
+
this.next();
|
|
629
|
+
const args = this.parseArgs();
|
|
630
|
+
const fixed = FIXED[t.v];
|
|
631
|
+
const want = POSTFIX.has(t.v) || t.v === '-.' ? 1 : 2;
|
|
632
|
+
if (args.length !== want)
|
|
633
|
+
this.fail(`The operator ${t.v} requires ${want} arguments`);
|
|
634
|
+
if (fixed)
|
|
635
|
+
return this.node(fixed, t, { a: args });
|
|
636
|
+
return this.node('app', t, { name: t.v, args });
|
|
637
|
+
}
|
|
638
|
+
parseArgs() {
|
|
639
|
+
// after '('
|
|
640
|
+
this.js.push(-1);
|
|
641
|
+
const args = [];
|
|
642
|
+
do
|
|
643
|
+
args.push(this.parseOpArg());
|
|
644
|
+
while (this.eatOp(','));
|
|
645
|
+
this.js.pop();
|
|
646
|
+
this.expectOp(')');
|
|
647
|
+
return args;
|
|
648
|
+
}
|
|
649
|
+
parseJunction(bullet) {
|
|
650
|
+
const first = this.peek();
|
|
651
|
+
const col = first.col;
|
|
652
|
+
const items = [];
|
|
653
|
+
for (;;) {
|
|
654
|
+
const r = this.raw();
|
|
655
|
+
if (!(r.t === 'op' && r.v === bullet && r.col === col))
|
|
656
|
+
break;
|
|
657
|
+
// the bullet must itself be visible to the enclosing junction context
|
|
658
|
+
if (this.peek() === Parser.STOP)
|
|
659
|
+
break;
|
|
660
|
+
this.next();
|
|
661
|
+
this.js.push(col);
|
|
662
|
+
items.push(this.parseExpr());
|
|
663
|
+
this.js.pop();
|
|
664
|
+
}
|
|
665
|
+
const kind = bullet === '/\\' ? 'and' : 'or';
|
|
666
|
+
if (items.length === 1) {
|
|
667
|
+
const it = items[0];
|
|
668
|
+
return it.k === 'and' || it.k === 'or' ? { ...it, junction: true } : it;
|
|
669
|
+
}
|
|
670
|
+
return this.node(kind, first, { a: items, junction: true });
|
|
671
|
+
}
|
|
672
|
+
// names (\in set)? groups separated by commas. Returns [] if no bound parsed.
|
|
673
|
+
parseBounds() {
|
|
674
|
+
const out = [];
|
|
675
|
+
for (;;) {
|
|
676
|
+
const t = this.peek();
|
|
677
|
+
if (t.t === 'op' && t.v === '<<') {
|
|
678
|
+
this.next();
|
|
679
|
+
const tuple = [];
|
|
680
|
+
if (!this.isOp('>>'))
|
|
681
|
+
do
|
|
682
|
+
tuple.push(this.expectId().v);
|
|
683
|
+
while (this.eatOp(','));
|
|
684
|
+
this.expectOp('>>');
|
|
685
|
+
this.expectOp('\\in');
|
|
686
|
+
out.push({ names: null, tuple, set: this.parseExpr() });
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
const names = [this.expectId().v];
|
|
690
|
+
while (this.eatOp(','))
|
|
691
|
+
names.push(this.expectId().v);
|
|
692
|
+
if (this.eatOp('\\in'))
|
|
693
|
+
out.push({ names, tuple: null, set: this.parseExpr() });
|
|
694
|
+
else
|
|
695
|
+
out.push({ names, tuple: null, set: null });
|
|
696
|
+
}
|
|
697
|
+
if (!this.eatOp(','))
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
return out;
|
|
701
|
+
}
|
|
702
|
+
attempt(f) {
|
|
703
|
+
const save = this.p, saveLast = this.last, js = this.js.slice();
|
|
704
|
+
try {
|
|
705
|
+
return f();
|
|
706
|
+
}
|
|
707
|
+
catch (e) {
|
|
708
|
+
if (!(e instanceof TlaError))
|
|
709
|
+
throw e;
|
|
710
|
+
this.p = save;
|
|
711
|
+
this.last = saveLast;
|
|
712
|
+
this.js = js;
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
parsePrimary() {
|
|
717
|
+
const t = this.peek();
|
|
718
|
+
switch (t.t) {
|
|
719
|
+
case 'num':
|
|
720
|
+
this.next();
|
|
721
|
+
return this.node('num', t, { v: t.n });
|
|
722
|
+
case 'str':
|
|
723
|
+
this.next();
|
|
724
|
+
return this.node('str', t, { v: t.v });
|
|
725
|
+
case 'step':
|
|
726
|
+
this.next();
|
|
727
|
+
while (this.isOp('!')) {
|
|
728
|
+
this.next();
|
|
729
|
+
this.next();
|
|
730
|
+
}
|
|
731
|
+
return this.node('stepref', t, { v: t.v });
|
|
732
|
+
case 'id': return this.parseIdent();
|
|
733
|
+
case 'kw': return this.parseKeywordExpr(t);
|
|
734
|
+
case 'op': break;
|
|
735
|
+
default: this.fail('Expected expression');
|
|
736
|
+
}
|
|
737
|
+
switch (t.v) {
|
|
738
|
+
case '(': {
|
|
739
|
+
this.next();
|
|
740
|
+
this.js.push(-1);
|
|
741
|
+
const e = this.parseExpr();
|
|
742
|
+
this.js.pop();
|
|
743
|
+
this.expectOp(')');
|
|
744
|
+
// mark parenthesised junctions so they do not merge with neighbours
|
|
745
|
+
return e.k === 'and' || e.k === 'or' ? { ...e, paren: true } : e;
|
|
746
|
+
}
|
|
747
|
+
case '/\\':
|
|
748
|
+
case '\\/': return this.parseJunction(t.v);
|
|
749
|
+
case '<<': {
|
|
750
|
+
this.next();
|
|
751
|
+
this.js.push(-1);
|
|
752
|
+
const items = [];
|
|
753
|
+
if (!this.isOp('>>') && !this.isOp('>>_'))
|
|
754
|
+
do
|
|
755
|
+
items.push(this.parseExpr());
|
|
756
|
+
while (this.eatOp(','));
|
|
757
|
+
this.js.pop();
|
|
758
|
+
if (this.eatOp('>>_')) {
|
|
759
|
+
const v = this.parseSubscript();
|
|
760
|
+
if (items.length !== 1)
|
|
761
|
+
this.fail('<<A>>_v requires exactly one action');
|
|
762
|
+
return this.node('angleact', t, { a: [items[0], v] });
|
|
763
|
+
}
|
|
764
|
+
this.expectOp('>>');
|
|
765
|
+
return this.node('tuple', t, { a: items });
|
|
766
|
+
}
|
|
767
|
+
case '{': return this.parseSet(t);
|
|
768
|
+
case '[': return this.parseBracket(t);
|
|
769
|
+
case '\\A':
|
|
770
|
+
case '\\E': {
|
|
771
|
+
this.next();
|
|
772
|
+
const bounds = this.parseBounds();
|
|
773
|
+
this.expectOp(':');
|
|
774
|
+
const body = this.parseExpr();
|
|
775
|
+
return this.node('quant', t, { q: t.v === '\\A' ? 'A' : 'E', bounds, body });
|
|
776
|
+
}
|
|
777
|
+
case '\\AA':
|
|
778
|
+
case '\\EE': {
|
|
779
|
+
this.next();
|
|
780
|
+
const names = [this.expectId().v];
|
|
781
|
+
while (this.eatOp(','))
|
|
782
|
+
names.push(this.expectId().v);
|
|
783
|
+
this.expectOp(':');
|
|
784
|
+
const body = this.parseExpr();
|
|
785
|
+
return this.node('tquant', t, { q: t.v, names, body });
|
|
786
|
+
}
|
|
787
|
+
case '@':
|
|
788
|
+
this.next();
|
|
789
|
+
return this.node('at', t);
|
|
790
|
+
}
|
|
791
|
+
{
|
|
792
|
+
const n1 = this.peekAt(1);
|
|
793
|
+
if ((INFIX[t.v] || POSTFIX.has(t.v) || t.v === '-.') && t.v !== '-' && n1.t === 'op' && n1.v === '(')
|
|
794
|
+
return this.prefixApply(t);
|
|
795
|
+
}
|
|
796
|
+
this.fail('Expected expression');
|
|
797
|
+
}
|
|
798
|
+
parseSubscript() {
|
|
799
|
+
// subscript of [A]_v, <<A>>_v, WF_v, SF_v: an identifier-ish expression or parenthesised/tuple expr
|
|
800
|
+
let e;
|
|
801
|
+
if (this.peek().t === 'id') {
|
|
802
|
+
const t = this.next();
|
|
803
|
+
let name = t.v;
|
|
804
|
+
while (this.isOp('!') && this.peekAt(1).t === 'id') {
|
|
805
|
+
this.next();
|
|
806
|
+
name += '!' + this.next().v;
|
|
807
|
+
}
|
|
808
|
+
e = this.node('app', t, { name, args: [] });
|
|
809
|
+
}
|
|
810
|
+
else
|
|
811
|
+
e = this.parsePrimary();
|
|
812
|
+
while (this.isOp('.') || this.isOp('[')) {
|
|
813
|
+
if (this.eatOp('.')) {
|
|
814
|
+
const f = this.next();
|
|
815
|
+
e = this.node('dot', e, { e, field: f.v });
|
|
816
|
+
}
|
|
817
|
+
else {
|
|
818
|
+
this.next();
|
|
819
|
+
this.js.push(-1);
|
|
820
|
+
const args = [this.parseExpr()];
|
|
821
|
+
while (this.eatOp(','))
|
|
822
|
+
args.push(this.parseExpr());
|
|
823
|
+
this.js.pop();
|
|
824
|
+
this.expectOp(']');
|
|
825
|
+
e = this.node('fnapp', e, { f: e, args });
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return e;
|
|
829
|
+
}
|
|
830
|
+
parseIdent() {
|
|
831
|
+
const t = this.next();
|
|
832
|
+
let name = t.v;
|
|
833
|
+
let args = [];
|
|
834
|
+
if (this.isOp('(')) {
|
|
835
|
+
this.next();
|
|
836
|
+
args = this.parseArgs();
|
|
837
|
+
}
|
|
838
|
+
// label: lab:: expr
|
|
839
|
+
if (this.isOp('::')) {
|
|
840
|
+
this.next();
|
|
841
|
+
return this.parseExpr();
|
|
842
|
+
}
|
|
843
|
+
// instance path: A!B(x)!C, and subexpression selectors Op!2, Op!<<, Op!(x)
|
|
844
|
+
let sel = null;
|
|
845
|
+
while (this.isOp('!')) {
|
|
846
|
+
const n1 = this.peekAt(1);
|
|
847
|
+
const n2 = this.peekAt(2);
|
|
848
|
+
if (!sel && (n1.t === 'id' || (n1.t === 'op' && INFIX[n1.v] !== undefined && ((n1.v !== '<' && n1.v !== ':>') || (n2.t === 'op' && n2.v === '('))) ||
|
|
849
|
+
(n1.t === 'op' && (n1.v === '-.' || (POSTFIX.has(n1.v) && n1.v !== "'")) && n2.t === 'op' && n2.v === '(')) && !(n1.t === 'op' && n1.v === '<<')) {
|
|
850
|
+
this.next();
|
|
851
|
+
const part = this.next();
|
|
852
|
+
name += '!' + part.v;
|
|
853
|
+
if (this.isOp('(')) {
|
|
854
|
+
this.next();
|
|
855
|
+
args = args.concat(this.parseArgs());
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
else if (n1.t === 'num' || (n1.t === 'op' && (n1.v === '<<' || n1.v === '>>' || n1.v === ':' || n1.v === '@' || n1.v === '('))) {
|
|
859
|
+
this.next();
|
|
860
|
+
sel = sel ?? [];
|
|
861
|
+
const s1 = this.next();
|
|
862
|
+
if (s1.v === '(')
|
|
863
|
+
sel.push(this.parseArgs());
|
|
864
|
+
else
|
|
865
|
+
sel.push(s1.t === 'num' ? s1.n : s1.v);
|
|
866
|
+
}
|
|
867
|
+
else
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
if (sel)
|
|
871
|
+
return this.node('app', t, { name, args, sel });
|
|
872
|
+
return this.node('app', t, { name, args });
|
|
873
|
+
}
|
|
874
|
+
parseKeywordExpr(t) {
|
|
875
|
+
switch (t.v) {
|
|
876
|
+
case 'TRUE':
|
|
877
|
+
this.next();
|
|
878
|
+
return this.node('bool', t, { v: true });
|
|
879
|
+
case 'FALSE':
|
|
880
|
+
this.next();
|
|
881
|
+
return this.node('bool', t, { v: false });
|
|
882
|
+
case 'IF': {
|
|
883
|
+
this.next();
|
|
884
|
+
const c = this.parseExpr();
|
|
885
|
+
this.expectKw('THEN');
|
|
886
|
+
const a = this.parseExpr();
|
|
887
|
+
this.expectKw('ELSE');
|
|
888
|
+
const b = this.parseExpr();
|
|
889
|
+
return this.node('if', t, { a: [c, a, b] });
|
|
890
|
+
}
|
|
891
|
+
case 'CASE': {
|
|
892
|
+
this.next();
|
|
893
|
+
const arms = [];
|
|
894
|
+
let other = null;
|
|
895
|
+
for (;;) {
|
|
896
|
+
if (this.eatKw('OTHER')) {
|
|
897
|
+
this.expectOp('->');
|
|
898
|
+
other = this.parseExpr();
|
|
899
|
+
break; // OTHER is always the last arm; a following [] belongs to an enclosing CASE
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
const c = this.parseExpr();
|
|
903
|
+
this.expectOp('->');
|
|
904
|
+
arms.push({ c, e: this.parseExpr() });
|
|
905
|
+
}
|
|
906
|
+
if (!this.eatOp('[]'))
|
|
907
|
+
break;
|
|
908
|
+
}
|
|
909
|
+
return this.node('case', t, { arms, other });
|
|
910
|
+
}
|
|
911
|
+
case 'LET': {
|
|
912
|
+
this.next();
|
|
913
|
+
const defs = [];
|
|
914
|
+
this.js.push(-1);
|
|
915
|
+
while (!this.isKw('IN')) {
|
|
916
|
+
if (this.isKw('RECURSIVE')) {
|
|
917
|
+
this.next();
|
|
918
|
+
do
|
|
919
|
+
this.parseOpDecl();
|
|
920
|
+
while (this.eatOp(','));
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
defs.push(this.parseDef());
|
|
924
|
+
}
|
|
925
|
+
this.js.pop();
|
|
926
|
+
this.expectKw('IN');
|
|
927
|
+
const body = this.parseExpr();
|
|
928
|
+
return this.node('let', t, { defs, body });
|
|
929
|
+
}
|
|
930
|
+
case 'CHOOSE': {
|
|
931
|
+
this.next();
|
|
932
|
+
const b = this.parseBounds();
|
|
933
|
+
if (b.length !== 1 || (b[0].names && b[0].names.length !== 1))
|
|
934
|
+
this.fail('CHOOSE takes one bound variable');
|
|
935
|
+
this.expectOp(':');
|
|
936
|
+
const body = this.parseExpr();
|
|
937
|
+
return this.node('choose', t, { bound: b[0], body });
|
|
938
|
+
}
|
|
939
|
+
case 'LAMBDA': {
|
|
940
|
+
this.next();
|
|
941
|
+
const params = [];
|
|
942
|
+
do
|
|
943
|
+
params.push({ name: this.expectId().v, arity: 0 });
|
|
944
|
+
while (this.eatOp(','));
|
|
945
|
+
this.expectOp(':');
|
|
946
|
+
const body = this.parseExpr();
|
|
947
|
+
return this.node('lambda', t, { params, body });
|
|
948
|
+
}
|
|
949
|
+
case 'WF_':
|
|
950
|
+
case 'SF_': {
|
|
951
|
+
this.next();
|
|
952
|
+
const v = this.parseSubscript();
|
|
953
|
+
this.expectOp('(');
|
|
954
|
+
this.js.push(-1);
|
|
955
|
+
const a = this.parseExpr();
|
|
956
|
+
this.js.pop();
|
|
957
|
+
this.expectOp(')');
|
|
958
|
+
return this.node(t.v === 'WF_' ? 'wf' : 'sf', t, { a: [v, a] });
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
this.fail('Expected expression');
|
|
962
|
+
}
|
|
963
|
+
parseSet(t) {
|
|
964
|
+
this.next();
|
|
965
|
+
this.js.push(-1);
|
|
966
|
+
try {
|
|
967
|
+
if (this.eatOp('}'))
|
|
968
|
+
return this.node('setenum', t, { a: [] });
|
|
969
|
+
// {x \in S : P}
|
|
970
|
+
const p0 = this.peek();
|
|
971
|
+
const p1 = this.peekAt(1);
|
|
972
|
+
if ((p0.t === 'id' && p1.t === 'op' && p1.v === '\\in') || (p0.t === 'op' && p0.v === '<<')) {
|
|
973
|
+
const r = this.attempt(() => {
|
|
974
|
+
let bound;
|
|
975
|
+
if (p0.t === 'id') {
|
|
976
|
+
this.next();
|
|
977
|
+
this.next();
|
|
978
|
+
bound = { names: [p0.v], tuple: null, set: this.parseExpr() };
|
|
979
|
+
}
|
|
980
|
+
else {
|
|
981
|
+
this.next();
|
|
982
|
+
const tuple = [];
|
|
983
|
+
do
|
|
984
|
+
tuple.push(this.expectId().v);
|
|
985
|
+
while (this.eatOp(','));
|
|
986
|
+
this.expectOp('>>');
|
|
987
|
+
this.expectOp('\\in');
|
|
988
|
+
bound = { names: null, tuple, set: this.parseExpr() };
|
|
989
|
+
}
|
|
990
|
+
this.expectOp(':');
|
|
991
|
+
const pred = this.parseExpr();
|
|
992
|
+
this.expectOp('}');
|
|
993
|
+
return this.node('setfilter', t, { bound, pred });
|
|
994
|
+
});
|
|
995
|
+
if (r)
|
|
996
|
+
return r;
|
|
997
|
+
}
|
|
998
|
+
const first = this.parseExpr();
|
|
999
|
+
if (this.eatOp(':')) {
|
|
1000
|
+
const bounds = this.parseBounds();
|
|
1001
|
+
this.expectOp('}');
|
|
1002
|
+
return this.node('setmap', t, { e: first, bounds });
|
|
1003
|
+
}
|
|
1004
|
+
const items = [first];
|
|
1005
|
+
while (this.eatOp(','))
|
|
1006
|
+
items.push(this.parseExpr());
|
|
1007
|
+
this.expectOp('}');
|
|
1008
|
+
return this.node('setenum', t, { a: items });
|
|
1009
|
+
}
|
|
1010
|
+
finally {
|
|
1011
|
+
this.js.pop();
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
parseBracket(t) {
|
|
1015
|
+
this.next();
|
|
1016
|
+
this.js.push(-1);
|
|
1017
|
+
try {
|
|
1018
|
+
const p0 = this.peek();
|
|
1019
|
+
const p1 = this.peekAt(1);
|
|
1020
|
+
if ((p0.t === 'id' || p0.t === 'kw') && p1.t === 'op' && (p1.v === '|->' || p1.v === ':')) {
|
|
1021
|
+
const isRec = p1.v === '|->';
|
|
1022
|
+
const fields = [];
|
|
1023
|
+
do {
|
|
1024
|
+
const f = this.next();
|
|
1025
|
+
this.expectOp(isRec ? '|->' : ':');
|
|
1026
|
+
fields.push({ name: f.v, e: this.parseExpr() });
|
|
1027
|
+
} while (this.eatOp(','));
|
|
1028
|
+
this.expectOp(']');
|
|
1029
|
+
const seen = new Set();
|
|
1030
|
+
for (const f of fields) {
|
|
1031
|
+
if (seen.has(f.name))
|
|
1032
|
+
throw new TlaError(`line ${f.e.loc[0]}, col ${f.e.loc[1]} to line ${f.e.loc[2]}, col ${f.e.loc[3]} of module ${this.mod}\n\nNon-unique fields in constructor.`, f.e.loc, this.mod, 'semantic');
|
|
1033
|
+
seen.add(f.name);
|
|
1034
|
+
}
|
|
1035
|
+
return this.node(isRec ? 'rec' : 'recset', t, { fields });
|
|
1036
|
+
}
|
|
1037
|
+
// [x \in S |-> e]
|
|
1038
|
+
if (p0.t === 'id' || (p0.t === 'op' && p0.v === '<<')) {
|
|
1039
|
+
const r = this.attempt(() => {
|
|
1040
|
+
const bounds = this.parseBounds();
|
|
1041
|
+
if (bounds.some(b => !b.set))
|
|
1042
|
+
this.fail('Expected bound');
|
|
1043
|
+
this.expectOp('|->');
|
|
1044
|
+
const body = this.parseExpr();
|
|
1045
|
+
this.expectOp(']');
|
|
1046
|
+
return this.node('fndef', t, { bounds, body });
|
|
1047
|
+
});
|
|
1048
|
+
if (r)
|
|
1049
|
+
return r;
|
|
1050
|
+
}
|
|
1051
|
+
const e = this.parseExpr();
|
|
1052
|
+
if (this.eatOp('->')) {
|
|
1053
|
+
const rng = this.parseExpr();
|
|
1054
|
+
this.expectOp(']');
|
|
1055
|
+
return this.node('fnset', t, { a: [e, rng] });
|
|
1056
|
+
}
|
|
1057
|
+
if (this.eatKw('EXCEPT')) {
|
|
1058
|
+
const updates = [];
|
|
1059
|
+
do {
|
|
1060
|
+
this.expectOp('!');
|
|
1061
|
+
const path = [];
|
|
1062
|
+
for (;;) {
|
|
1063
|
+
if (this.eatOp('.')) {
|
|
1064
|
+
const f = this.next();
|
|
1065
|
+
path.push({ k: 'str', loc: [f.line, f.col, f.line, f.ec], v: f.v });
|
|
1066
|
+
}
|
|
1067
|
+
else if (this.isOp('[')) {
|
|
1068
|
+
this.next();
|
|
1069
|
+
const idx = [this.parseExpr()];
|
|
1070
|
+
while (this.eatOp(','))
|
|
1071
|
+
idx.push(this.parseExpr());
|
|
1072
|
+
this.expectOp(']');
|
|
1073
|
+
path.push(idx.length === 1 ? idx[0] : { k: 'tuple', loc: idx[0].loc, a: idx });
|
|
1074
|
+
}
|
|
1075
|
+
else
|
|
1076
|
+
break;
|
|
1077
|
+
}
|
|
1078
|
+
if (!path.length)
|
|
1079
|
+
this.fail('Expected EXCEPT path');
|
|
1080
|
+
this.expectOp('=');
|
|
1081
|
+
updates.push({ path, val: this.parseExpr() });
|
|
1082
|
+
} while (this.eatOp(','));
|
|
1083
|
+
this.expectOp(']');
|
|
1084
|
+
return this.node('except', t, { base: e, updates });
|
|
1085
|
+
}
|
|
1086
|
+
if (this.eatOp(']_')) {
|
|
1087
|
+
this.js.pop();
|
|
1088
|
+
this.js.push(this.js.length ? this.js[this.js.length - 1] : -1);
|
|
1089
|
+
const v = this.parseSubscript();
|
|
1090
|
+
return this.node('boxact', t, { a: [e, v] });
|
|
1091
|
+
}
|
|
1092
|
+
this.fail("Expected ']', '->', 'EXCEPT' or ']_'");
|
|
1093
|
+
}
|
|
1094
|
+
finally {
|
|
1095
|
+
this.js.pop();
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
export function parseModule(src, nameHint = '?') {
|
|
1100
|
+
const toks = lex(src, nameHint, strId);
|
|
1101
|
+
const p = new Parser(toks, nameHint);
|
|
1102
|
+
const m = p.parseModule();
|
|
1103
|
+
m.strings = toks.filter(t => t.t === 'str').map(t => t.v);
|
|
1104
|
+
return m;
|
|
1105
|
+
}
|
|
1106
|
+
// Parse a standalone expression (used for config values and tests).
|
|
1107
|
+
export function parseExpression(src) {
|
|
1108
|
+
const toks = lex(src, '<expr>');
|
|
1109
|
+
const p = new Parser(toks, '<expr>');
|
|
1110
|
+
const e = p.parseExpr();
|
|
1111
|
+
if (p.raw().t !== 'eof')
|
|
1112
|
+
p.fail('Unexpected trailing input');
|
|
1113
|
+
return e;
|
|
1114
|
+
}
|