tla-ts 0.0.3-dev.4cf9a33 → 0.0.3-dev.4fdd845
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +164 -40
- package/dist/index.d.ts +164 -40
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -22
- package/dist/run-cli.js +1351 -22
- package/dist/transfer.cjs +83 -34
- package/dist/transfer.js +83 -34
- package/package.json +5 -1
- package/src/ast.ts +49 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1331 -367
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +417 -124
- package/src/errors.ts +168 -9
- package/src/evaluate.ts +720 -177
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +149 -31
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +419 -122
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1334 -452
- package/src/run-cli.ts +1166 -181
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +161 -167
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +683 -256
|
@@ -0,0 +1,2161 @@
|
|
|
1
|
+
import { createSanyLexer, escapeLexeme, getKind, type SanyToken } from "./sany-lexer";
|
|
2
|
+
import { SANY_OPERATOR_SYNONYMS } from "./tlc-modules";
|
|
3
|
+
|
|
4
|
+
// The report SANY's parser, TLAplusParser, gives for a module it fails to parse. This interprets
|
|
5
|
+
// the grammar of javacc/tla+.jj the way the code JavaCC generates from it runs: the productions,
|
|
6
|
+
// their lookahead specifications, and the semantic actions that steer parsing or set the
|
|
7
|
+
// `expecting` message. TLAplusParser.msgStackToString prints that message, the token the parser
|
|
8
|
+
// failed at, and the productions it was in, so the interpreter must fail at the same token, in the
|
|
9
|
+
// same productions, and must look as far ahead as JavaCC's jj_rescan_token does.
|
|
10
|
+
|
|
11
|
+
type Predicate = (state: ParserState) => boolean;
|
|
12
|
+
|
|
13
|
+
interface TokensUnit {
|
|
14
|
+
type: "tokens";
|
|
15
|
+
kinds: Set<number>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface CallUnit {
|
|
19
|
+
type: "call";
|
|
20
|
+
name: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface SequenceUnit {
|
|
24
|
+
type: "sequence";
|
|
25
|
+
units: Unit[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ChoiceUnit {
|
|
29
|
+
type: "choice";
|
|
30
|
+
alternatives: SequenceUnit[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface RepeatUnit {
|
|
34
|
+
type: "zeroOrMore" | "oneOrMore" | "optional";
|
|
35
|
+
body: SequenceUnit;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Java code in a production, which may throw a ParseException with a message.
|
|
39
|
+
interface ActionUnit {
|
|
40
|
+
type: "action";
|
|
41
|
+
run: (state: ParserState) => string | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// A LOOKAHEAD specification. First in a choice's alternative or in a loop's body, it decides
|
|
45
|
+
// whether to take that expansion. Elsewhere, JavaCC checks only its semantic part.
|
|
46
|
+
interface LookaheadUnit {
|
|
47
|
+
type: "lookahead";
|
|
48
|
+
amount: number;
|
|
49
|
+
expansion: SequenceUnit | undefined;
|
|
50
|
+
semantic: Predicate | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Unit =
|
|
54
|
+
| TokensUnit
|
|
55
|
+
| CallUnit
|
|
56
|
+
| SequenceUnit
|
|
57
|
+
| ChoiceUnit
|
|
58
|
+
| RepeatUnit
|
|
59
|
+
| ActionUnit
|
|
60
|
+
| LookaheadUnit;
|
|
61
|
+
|
|
62
|
+
// A token kind in angle brackets, a production's name, or a unit.
|
|
63
|
+
type Part = Unit | string;
|
|
64
|
+
|
|
65
|
+
// A SyntaxTreeNode: a token, or what a production matched, which spans from its first token to
|
|
66
|
+
// its last.
|
|
67
|
+
interface SyntaxNode {
|
|
68
|
+
kind: string;
|
|
69
|
+
heirs: SyntaxNode[];
|
|
70
|
+
first: SanyToken;
|
|
71
|
+
last: SanyToken;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The production being parsed: the nodes of what it has matched, and the node it returns if that
|
|
75
|
+
// is not a node of its own kind over them.
|
|
76
|
+
interface Frame {
|
|
77
|
+
heirs: SyntaxNode[];
|
|
78
|
+
node: SyntaxNode | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface Production {
|
|
82
|
+
// The name TLAplusParser.bpa pushes on the message stack, if the production calls it.
|
|
83
|
+
message: string | undefined;
|
|
84
|
+
body: SequenceUnit;
|
|
85
|
+
// Java code that runs after the production's epa.
|
|
86
|
+
exit: ((frame: Frame, state: ParserState) => string | void) | undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// An Operator of Operators.CanonicalOperators. None of them is right-associative.
|
|
90
|
+
interface Operator {
|
|
91
|
+
name: string;
|
|
92
|
+
low: number;
|
|
93
|
+
high: number;
|
|
94
|
+
isLeftAssociative: boolean;
|
|
95
|
+
fix: "prefix" | "postfix" | "infix" | "nfix";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// An OSelement of OperatorStack: an operand, or an operator with its node.
|
|
99
|
+
interface StackElement {
|
|
100
|
+
node: SyntaxNode;
|
|
101
|
+
operator: Operator | undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface Analysis {
|
|
105
|
+
first: Set<number>;
|
|
106
|
+
isNullable: boolean;
|
|
107
|
+
hasSemanticStart: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// A jj_2 call, which jj_rescan_token scans again if it looked past the current token: JJCalls.
|
|
111
|
+
interface LookaheadCall {
|
|
112
|
+
generation: number;
|
|
113
|
+
first: SanyToken;
|
|
114
|
+
amount: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface StackMessage {
|
|
118
|
+
name: string;
|
|
119
|
+
token: SanyToken;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface Junction {
|
|
123
|
+
kind: number;
|
|
124
|
+
column: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface LookaheadOptions {
|
|
128
|
+
amount?: number;
|
|
129
|
+
expansion?: Part[];
|
|
130
|
+
semantic?: Predicate;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
interface ParserState {
|
|
134
|
+
readToken: () => SanyToken;
|
|
135
|
+
lexicalError: string | undefined;
|
|
136
|
+
token: SanyToken;
|
|
137
|
+
// jj_gen, which counts consumed tokens, and its value at the last failed jj_ntk switch.
|
|
138
|
+
generation: number;
|
|
139
|
+
failedSwitchGeneration: number;
|
|
140
|
+
scanPosition: SanyToken;
|
|
141
|
+
lastPosition: SanyToken;
|
|
142
|
+
remaining: number;
|
|
143
|
+
isLookingAhead: boolean;
|
|
144
|
+
isRescanning: boolean;
|
|
145
|
+
farthestRescan: number;
|
|
146
|
+
calls: Map<SequenceUnit, LookaheadCall[]>;
|
|
147
|
+
expecting: string;
|
|
148
|
+
messages: StackMessage[];
|
|
149
|
+
moduleName: string | undefined;
|
|
150
|
+
junctions: Junction[];
|
|
151
|
+
frames: Frame[];
|
|
152
|
+
// OperatorStack's stack of stacks, one for each Expression being parsed.
|
|
153
|
+
operatorStacks: StackElement[][];
|
|
154
|
+
lastOperator: Operator | undefined;
|
|
155
|
+
proofLevels: number[];
|
|
156
|
+
isTerminalProof: boolean;
|
|
157
|
+
// Whether FairnessHook is set, which it is if the last NoOpExtension ended with arguments.
|
|
158
|
+
isFairnessHooked: boolean;
|
|
159
|
+
isFairnessParenthesized: boolean[];
|
|
160
|
+
report: string | undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type ScanResult = "matched" | "failed" | "done";
|
|
164
|
+
|
|
165
|
+
// JavaCC's amount for a LOOKAHEAD that gives an expansion but no amount.
|
|
166
|
+
const UNBOUNDED_AMOUNT = 2147483647;
|
|
167
|
+
const EOF = getKind("EOF");
|
|
168
|
+
const IDENTIFIER = getKind("IDENTIFIER");
|
|
169
|
+
const DEFBREAK = getKind("DEFBREAK");
|
|
170
|
+
|
|
171
|
+
const getKindRange = (first: string, last: string) =>
|
|
172
|
+
Array.from(
|
|
173
|
+
{ length: getKind(last) - getKind(first) + 1 },
|
|
174
|
+
(_, offset) => getKind(first) + offset,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// TLAplusParser.isOp, isInfixOp, isPostfixOp, and isFieldNameToken.
|
|
178
|
+
const OPERATOR_KINDS = new Set(getKindRange("op_57", "op_119"));
|
|
179
|
+
const INFIX_KINDS = new Set(getKindRange("op_1", "op_119"));
|
|
180
|
+
const POSTFIX_KINDS = new Set(getKindRange("op_57", "op_70"));
|
|
181
|
+
const FIELD_NAME_KINDS = new Set([
|
|
182
|
+
...getKindRange("ACTION", "EXCEPT"),
|
|
183
|
+
getKind("EXTENDS"),
|
|
184
|
+
...getKindRange("IF", "SF"),
|
|
185
|
+
getKind("STATE"),
|
|
186
|
+
...getKindRange("THEN", "WITH"),
|
|
187
|
+
getKind("US"),
|
|
188
|
+
...getKindRange("op_112", "op_116"),
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
const createOperators = (
|
|
192
|
+
fix: Operator["fix"],
|
|
193
|
+
low: number,
|
|
194
|
+
high: number,
|
|
195
|
+
isLeftAssociative: boolean,
|
|
196
|
+
...names: string[]
|
|
197
|
+
) => names.map((name): [string, Operator] => [name, { name, low, high, isLeftAssociative, fix }]);
|
|
198
|
+
|
|
199
|
+
// Operators.CanonicalOperators, but for ".", which only OperatorStack.reduceRecord handles.
|
|
200
|
+
const CANONICAL_OPERATORS = new Map([
|
|
201
|
+
...createOperators("postfix", 160, 160, true, "["),
|
|
202
|
+
...createOperators("postfix", 150, 150, false, "'", "^+", "^*", "^#"),
|
|
203
|
+
...createOperators("prefix", 120, 120, false, "-."),
|
|
204
|
+
...createOperators("prefix", 100, 130, false, "SUBSET", "UNION", "DOMAIN"),
|
|
205
|
+
...createOperators("prefix", 40, 150, false, "[]", "<>", "ENABLED", "UNCHANGED"),
|
|
206
|
+
...createOperators("prefix", 40, 40, true, "\\lnot"),
|
|
207
|
+
...createOperators("nfix", 100, 130, true, "\\times"),
|
|
208
|
+
...createOperators("infix", 140, 140, false, "^", "^^"),
|
|
209
|
+
...createOperators("infix", 130, 130, false, "/", "//", "\\oslash", "\\div"),
|
|
210
|
+
...createOperators(
|
|
211
|
+
"infix",
|
|
212
|
+
130,
|
|
213
|
+
130,
|
|
214
|
+
true,
|
|
215
|
+
"*",
|
|
216
|
+
"&&",
|
|
217
|
+
"&",
|
|
218
|
+
"**",
|
|
219
|
+
"\\odot",
|
|
220
|
+
"\\otimes",
|
|
221
|
+
"\\star",
|
|
222
|
+
"\\o",
|
|
223
|
+
"\\bigcirc",
|
|
224
|
+
"\\bullet",
|
|
225
|
+
),
|
|
226
|
+
...createOperators("infix", 110, 110, true, "-", "--", "\\ominus"),
|
|
227
|
+
...createOperators("infix", 100, 110, true, "|", "||", "%%"),
|
|
228
|
+
...createOperators("infix", 100, 110, false, "%"),
|
|
229
|
+
...createOperators("infix", 100, 100, true, "+", "++", "\\oplus"),
|
|
230
|
+
...createOperators("infix", 90, 140, false, "\\wr"),
|
|
231
|
+
...createOperators(
|
|
232
|
+
"infix",
|
|
233
|
+
90,
|
|
234
|
+
130,
|
|
235
|
+
true,
|
|
236
|
+
"$$",
|
|
237
|
+
"$",
|
|
238
|
+
"??",
|
|
239
|
+
"##",
|
|
240
|
+
"\\uplus",
|
|
241
|
+
"\\sqcap",
|
|
242
|
+
"\\sqcup",
|
|
243
|
+
),
|
|
244
|
+
...createOperators("infix", 90, 130, false, "!!"),
|
|
245
|
+
...createOperators("infix", 90, 90, false, "...", ".."),
|
|
246
|
+
...createOperators("infix", 80, 80, true, "\\intersect", "\\union"),
|
|
247
|
+
...createOperators("infix", 80, 80, false, "\\"),
|
|
248
|
+
...createOperators("infix", 70, 70, false, "<:", ":>"),
|
|
249
|
+
...createOperators("infix", 60, 60, true, "@@"),
|
|
250
|
+
...createOperators("infix", 50, 140, true, "\\cdot"),
|
|
251
|
+
...createOperators(
|
|
252
|
+
"infix",
|
|
253
|
+
50,
|
|
254
|
+
50,
|
|
255
|
+
false,
|
|
256
|
+
"=",
|
|
257
|
+
"/=",
|
|
258
|
+
"<",
|
|
259
|
+
">",
|
|
260
|
+
"\\in",
|
|
261
|
+
"\\notin",
|
|
262
|
+
"\\leq",
|
|
263
|
+
"\\geq",
|
|
264
|
+
"\\subseteq",
|
|
265
|
+
"\\subset",
|
|
266
|
+
"\\supseteq",
|
|
267
|
+
"\\supset",
|
|
268
|
+
"\\sqsubseteq",
|
|
269
|
+
"\\sqsubset",
|
|
270
|
+
"\\sqsupseteq",
|
|
271
|
+
"\\sqsupset",
|
|
272
|
+
"\\prec",
|
|
273
|
+
"\\preceq",
|
|
274
|
+
"\\succ",
|
|
275
|
+
"\\succeq",
|
|
276
|
+
"\\sim",
|
|
277
|
+
"\\simeq",
|
|
278
|
+
"\\approx",
|
|
279
|
+
"\\cong",
|
|
280
|
+
"\\asymp",
|
|
281
|
+
"\\doteq",
|
|
282
|
+
"\\propto",
|
|
283
|
+
"\\ll",
|
|
284
|
+
"\\gg",
|
|
285
|
+
"|-",
|
|
286
|
+
"|=",
|
|
287
|
+
"-|",
|
|
288
|
+
"=|",
|
|
289
|
+
":=",
|
|
290
|
+
"::=",
|
|
291
|
+
),
|
|
292
|
+
...createOperators("infix", 30, 30, true, "\\land", "\\lor"),
|
|
293
|
+
...createOperators("infix", 20, 20, false, "~>", "\\equiv", "-+->"),
|
|
294
|
+
...createOperators("infix", 10, 10, false, "=>"),
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
// Operators.getOperator, which knows an operator by its name or a synonym.
|
|
298
|
+
const OPERATORS = new Map([
|
|
299
|
+
...CANONICAL_OPERATORS,
|
|
300
|
+
...SANY_OPERATOR_SYNONYMS.flatMap((synonyms) =>
|
|
301
|
+
synonyms.slice(1).map((synonym) => [synonym, CANONICAL_OPERATORS.get(synonyms[0])] as const),
|
|
302
|
+
),
|
|
303
|
+
]);
|
|
304
|
+
// OperatorStack's fcnOp, which a function application's "[" pushes, and Operator.VoidOperator,
|
|
305
|
+
// which OperatorStack.finalReduce pushes to reduce the whole stack.
|
|
306
|
+
const FCN_OPERATOR = OPERATORS.get("[");
|
|
307
|
+
const VOID_OPERATOR: Operator = {
|
|
308
|
+
name: "$$_void",
|
|
309
|
+
low: 0,
|
|
310
|
+
high: 0,
|
|
311
|
+
isLeftAssociative: false,
|
|
312
|
+
fix: "infix",
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const tokens = (kinds: Iterable<number>): TokensUnit => ({ type: "tokens", kinds: new Set(kinds) });
|
|
316
|
+
const toUnit = (part: Part): Unit => {
|
|
317
|
+
if (typeof part !== "string") return part;
|
|
318
|
+
return part.startsWith("<") ? tokens([getKind(part.slice(1, -1))]) : { type: "call", name: part };
|
|
319
|
+
};
|
|
320
|
+
const sequence = (...parts: Part[]): SequenceUnit => ({
|
|
321
|
+
type: "sequence",
|
|
322
|
+
units: parts.map(toUnit),
|
|
323
|
+
});
|
|
324
|
+
const choice = (...alternatives: Part[]): ChoiceUnit => ({
|
|
325
|
+
type: "choice",
|
|
326
|
+
alternatives: alternatives.map((alternative) =>
|
|
327
|
+
typeof alternative !== "string" && alternative.type === "sequence"
|
|
328
|
+
? alternative
|
|
329
|
+
: sequence(alternative),
|
|
330
|
+
),
|
|
331
|
+
});
|
|
332
|
+
const repeat =
|
|
333
|
+
(type: RepeatUnit["type"]) =>
|
|
334
|
+
(...parts: Part[]): RepeatUnit => ({ type, body: sequence(...parts) });
|
|
335
|
+
const zeroOrMore = repeat("zeroOrMore");
|
|
336
|
+
const oneOrMore = repeat("oneOrMore");
|
|
337
|
+
const optional = repeat("optional");
|
|
338
|
+
const action = (run: (state: ParserState) => string | undefined | void): ActionUnit => ({
|
|
339
|
+
type: "action",
|
|
340
|
+
run: (state) => run(state) ?? undefined,
|
|
341
|
+
});
|
|
342
|
+
const expect = (expecting: string) =>
|
|
343
|
+
action((state) => {
|
|
344
|
+
state.expecting = expecting;
|
|
345
|
+
});
|
|
346
|
+
const lookahead = (options: LookaheadOptions): LookaheadUnit => ({
|
|
347
|
+
type: "lookahead",
|
|
348
|
+
amount: options.amount ?? (options.expansion ? UNBOUNDED_AMOUNT : 0),
|
|
349
|
+
expansion: options.expansion && sequence(...options.expansion),
|
|
350
|
+
semantic: options.semantic,
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const getNext = (state: ParserState, token: SanyToken) => {
|
|
354
|
+
if (token.next === undefined) token.next = state.readToken();
|
|
355
|
+
return token.next;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
// TLAplusParser.getToken, which counts from the scan position while a semantic lookahead runs
|
|
359
|
+
// during a scan.
|
|
360
|
+
const getToken = (state: ParserState, index: number) => {
|
|
361
|
+
let token = state.isLookingAhead ? state.scanPosition : state.token;
|
|
362
|
+
for (let count = 0; count < index; count++) token = getNext(state, token);
|
|
363
|
+
return token;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const isNextKind = (...names: string[]) => {
|
|
367
|
+
const kinds = names.map(getKind);
|
|
368
|
+
return (state: ParserState) => kinds.includes(getToken(state, 1).kind);
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// JunctionListContext.isAboveCurrent, for the next token by default, and isNewBullet.
|
|
372
|
+
const isAboveCurrent = (state: ParserState, column = getToken(state, 1).beginColumn) => {
|
|
373
|
+
const junction = state.junctions.at(-1);
|
|
374
|
+
return junction === undefined || junction.column < column;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const isNewBullet = (state: ParserState) => {
|
|
378
|
+
const token = getToken(state, 1);
|
|
379
|
+
const junction = state.junctions.at(-1);
|
|
380
|
+
return junction?.column === token.beginColumn && junction.kind === token.kind;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// TLAplusParser.caseSep.
|
|
384
|
+
const isCaseSeparator = (state: ParserState) =>
|
|
385
|
+
isNextKind("CASESEP")(state) && isAboveCurrent(state);
|
|
386
|
+
|
|
387
|
+
// The Java code that lets a keyword name a module or a record field.
|
|
388
|
+
const setFieldName = (state: ParserState) => {
|
|
389
|
+
const token = getToken(state, 1);
|
|
390
|
+
if (FIELD_NAME_KINDS.has(token.kind)) token.kind = IDENTIFIER;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// SyntaxTreeNode.getLocation for the tokens first to last, as Location.toString prints it.
|
|
394
|
+
const formatLocation = (state: ParserState, first: SanyToken, last = first) =>
|
|
395
|
+
`line ${first.beginLine}, col ${first.beginColumn} to line ${last.beginLine}, col ${last.beginColumn + last.image.length - 1} of module ${state.moduleName}`;
|
|
396
|
+
|
|
397
|
+
const createNode = (kind: string, heirs: SyntaxNode[]) => ({
|
|
398
|
+
kind,
|
|
399
|
+
heirs,
|
|
400
|
+
first: heirs[0].first,
|
|
401
|
+
last: heirs[heirs.length - 1].last,
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// The node offset nodes back among those of what the production being parsed has matched.
|
|
405
|
+
const getHeir = (state: ParserState, offset: number) => {
|
|
406
|
+
const heirs = state.frames[state.frames.length - 1].heirs;
|
|
407
|
+
return heirs[heirs.length - offset];
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const getOperatorStack = (state: ParserState) =>
|
|
411
|
+
state.operatorStacks[state.operatorStacks.length - 1];
|
|
412
|
+
|
|
413
|
+
// Operator.succ and Operator.prec: whether left binds tighter than right, or looser.
|
|
414
|
+
const isTighter = (left: Operator, right: Operator) => left.low > right.high;
|
|
415
|
+
const isLooser = (left: Operator, right: Operator) => left.high < right.low;
|
|
416
|
+
|
|
417
|
+
// Operators.getMixfix, which turns "-" into the prefix "-.".
|
|
418
|
+
const getMixfix = (operator: Operator) =>
|
|
419
|
+
operator.fix === "prefix" ? operator : OPERATORS.get(`${operator.name}.`);
|
|
420
|
+
|
|
421
|
+
// OperatorStack.reducePostfix: replaces the operand and postfix operator below index top with
|
|
422
|
+
// their expression.
|
|
423
|
+
const reducePostfix = (stack: StackElement[], top: number) => {
|
|
424
|
+
const operand = stack[top - 2].node;
|
|
425
|
+
const postfix = stack[top - 1];
|
|
426
|
+
stack.splice(top - 2, 2, {
|
|
427
|
+
node:
|
|
428
|
+
postfix.operator === FCN_OPERATOR
|
|
429
|
+
? createNode("FcnAppl", [operand, ...postfix.node.heirs])
|
|
430
|
+
: createNode("PostfixExpr", [operand, postfix.node]),
|
|
431
|
+
operator: undefined,
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// OperatorStack.reducePrefix and reduceInfix: replace the operator below the top of the stack and
|
|
436
|
+
// its operands with their expression.
|
|
437
|
+
const reduceOperator = (stack: StackElement[], operator: Operator) => {
|
|
438
|
+
const top = stack.length - 1;
|
|
439
|
+
if (operator.fix !== "infix" && operator.fix !== "nfix") {
|
|
440
|
+
stack.splice(top - 2, 2, {
|
|
441
|
+
node: createNode("PrefixExpr", [stack[top - 2].node, stack[top - 1].node]),
|
|
442
|
+
operator: undefined,
|
|
443
|
+
});
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const left = stack[top - 3].node;
|
|
447
|
+
const isTimes = operator.fix === "nfix";
|
|
448
|
+
stack.splice(top - 3, 3, {
|
|
449
|
+
node: createNode(isTimes ? "Times" : "InfixExpr", [
|
|
450
|
+
...(isTimes && left.kind === "Times" ? left.heirs : [left]),
|
|
451
|
+
stack[top - 2].node,
|
|
452
|
+
stack[top - 1].node,
|
|
453
|
+
]),
|
|
454
|
+
operator: undefined,
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// OperatorStack.reduceStack, run when an operator is pushed: it reduces the operators below it
|
|
459
|
+
// that bind tighter, and gives the message it throws where precedence can't order two operators,
|
|
460
|
+
// or two operands follow each other. The grammar pushes an infix or postfix operator only after an
|
|
461
|
+
// operand, except for a "-" that Expression takes for a prefix one, so the checks that reject
|
|
462
|
+
// other orders never fail and are left out.
|
|
463
|
+
const reduceStack = (state: ParserState): string | undefined => {
|
|
464
|
+
const stack = getOperatorStack(state);
|
|
465
|
+
const top = stack.length - 1;
|
|
466
|
+
const right = stack[top];
|
|
467
|
+
const middle = stack[top - 1];
|
|
468
|
+
const left = stack[top - 2];
|
|
469
|
+
if (right.operator === undefined || right.operator.fix === "prefix") return undefined;
|
|
470
|
+
if (middle.operator?.fix === "postfix") {
|
|
471
|
+
reducePostfix(stack, top);
|
|
472
|
+
return reduceStack(state);
|
|
473
|
+
}
|
|
474
|
+
if (middle.operator !== undefined) {
|
|
475
|
+
const mixfix = getMixfix(right.operator);
|
|
476
|
+
return mixfix !== undefined && isTighter(middle.operator, mixfix)
|
|
477
|
+
? `\n Precedence conflict between ops ${middle.operator.name} in block ${formatLocation(state, middle.node.first, middle.node.last)} and ${mixfix.name}.`
|
|
478
|
+
: undefined;
|
|
479
|
+
}
|
|
480
|
+
if (left === undefined) return undefined;
|
|
481
|
+
if (left.operator === undefined) {
|
|
482
|
+
return `Expression at location ${formatLocation(state, left.node.first, left.node.last)} and expression at location ${formatLocation(state, middle.node.first, middle.node.last)} follow each other without any intervening operator.`;
|
|
483
|
+
}
|
|
484
|
+
if (right.operator.fix === "postfix") {
|
|
485
|
+
if (isLooser(left.operator, right.operator)) return undefined;
|
|
486
|
+
if (!isTighter(left.operator, right.operator)) {
|
|
487
|
+
return `Precedence conflict between ops ${left.operator.name} in block ${formatLocation(state, right.node.first, right.node.last)} and ${right.operator.name}.`;
|
|
488
|
+
}
|
|
489
|
+
reduceOperator(stack, left.operator);
|
|
490
|
+
return reduceStack(state);
|
|
491
|
+
}
|
|
492
|
+
const mixfix = getMixfix(left.operator);
|
|
493
|
+
const operator =
|
|
494
|
+
mixfix !== undefined && (top === 2 || stack[top - 3].operator !== undefined)
|
|
495
|
+
? mixfix
|
|
496
|
+
: left.operator;
|
|
497
|
+
if (isLooser(operator, right.operator)) return undefined;
|
|
498
|
+
if (
|
|
499
|
+
!isTighter(operator, right.operator) &&
|
|
500
|
+
!(operator === right.operator && operator.isLeftAssociative)
|
|
501
|
+
) {
|
|
502
|
+
return `\n Precedence conflict between ops ${operator.name} in block ${formatLocation(state, left.node.first, left.node.last)} and ${right.operator.name}.`;
|
|
503
|
+
}
|
|
504
|
+
if (operator.fix !== "postfix") {
|
|
505
|
+
reduceOperator(stack, operator);
|
|
506
|
+
return reduceStack(state);
|
|
507
|
+
}
|
|
508
|
+
const location = formatLocation(state, left.node.first, left.node.last);
|
|
509
|
+
if (left.node.first.beginLine < middle.node.first.beginLine && right.operator.name === "=") {
|
|
510
|
+
return `\n *** Hint *** You may have mistyped ==\n Illegal combination of operators ${operator.name} in block ${location} and =.`;
|
|
511
|
+
}
|
|
512
|
+
return right.operator === VOID_OPERATOR
|
|
513
|
+
? `\n Error following expression at ${location}, missing operator or separator.`
|
|
514
|
+
: `\n Illegal combination of operators ${operator.name} in block ${location} and ${right.operator.name}.`;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// OperatorStack.pushOnStack for the node of the production just parsed: an operand, or, if
|
|
518
|
+
// isOperator holds of it, an operator, which reduceStack reduces with what is below it.
|
|
519
|
+
const pushOnStack = (isOperator: (node: SyntaxNode) => boolean) =>
|
|
520
|
+
action((state) => {
|
|
521
|
+
const stack = getOperatorStack(state);
|
|
522
|
+
const node = getHeir(state, 1);
|
|
523
|
+
if (!isOperator(node)) {
|
|
524
|
+
stack.push({ node, operator: undefined });
|
|
525
|
+
return undefined;
|
|
526
|
+
}
|
|
527
|
+
stack.push({ node, operator: state.lastOperator });
|
|
528
|
+
return stack.length === 1 ? undefined : reduceStack(state);
|
|
529
|
+
});
|
|
530
|
+
const PUSH_OPERAND = pushOnStack(() => false);
|
|
531
|
+
const PUSH_OPERATOR = pushOnStack(() => true);
|
|
532
|
+
|
|
533
|
+
// TLAplusParser's lastOp, which the productions of operators set.
|
|
534
|
+
const SET_LAST_OPERATOR = action((state) => {
|
|
535
|
+
state.lastOperator = OPERATORS.get(state.token.image);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
// The OpArgs node that a PrimitiveExp's heirs end with, directly or in their last "!" extension.
|
|
539
|
+
const getArguments = (heirs: SyntaxNode[]) => {
|
|
540
|
+
const last = heirs[heirs.length - 1];
|
|
541
|
+
const node = last.kind === "BangExt" ? last.heirs[last.heirs.length - 1] : last;
|
|
542
|
+
return node.kind === "OpArgs" ? node : undefined;
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
// TLAplusParser.isLabel: whether a node is an identifier, or an operator applied to identifiers.
|
|
546
|
+
const isLabel = (node: SyntaxNode) => {
|
|
547
|
+
if (node.kind === "GeneralId") return true;
|
|
548
|
+
const opArgs = node.kind === "OpApplication" ? getArguments(node.heirs) : undefined;
|
|
549
|
+
return (
|
|
550
|
+
opArgs !== undefined &&
|
|
551
|
+
opArgs.heirs.every((argument, index) => index % 2 === 0 || argument.kind === "GeneralId")
|
|
552
|
+
);
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// The first node below node, stopping at junction lists, that does not start to the right of the
|
|
556
|
+
// current junction list's bullet: what TLAplusParser.checkIndentation reports.
|
|
557
|
+
const getMisindentedNode = (state: ParserState, node: SyntaxNode): SyntaxNode | undefined => {
|
|
558
|
+
for (const child of node.heirs) {
|
|
559
|
+
if (child.kind === "Junctions") continue;
|
|
560
|
+
if (!isAboveCurrent(state, child.first.beginColumn)) return child;
|
|
561
|
+
const misindented = getMisindentedNode(state, child);
|
|
562
|
+
if (misindented !== undefined) return misindented;
|
|
563
|
+
}
|
|
564
|
+
return undefined;
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
// TLAplusParser.belchDEF, which inserts a DEFBREAK token before the left-hand side of the next
|
|
568
|
+
// definition. It reverses the links of the tokens it walks over, so that `next` leads back toward
|
|
569
|
+
// the definition's start, and restores them when done.
|
|
570
|
+
const belchDef = (state: ParserState) => {
|
|
571
|
+
const stops = ["EOF", "THEOREM", "PROPOSITION", "ASSUMPTION", "END_MODULE"].map(getKind);
|
|
572
|
+
const isKind = (token: SanyToken, ...names: string[]) =>
|
|
573
|
+
names.some((name) => token.kind === getKind(name));
|
|
574
|
+
const isOperator = (token: SanyToken | undefined): token is SanyToken =>
|
|
575
|
+
token !== undefined && OPERATOR_KINDS.has(token.kind);
|
|
576
|
+
let previous: SanyToken | undefined = state.token;
|
|
577
|
+
let current = getNext(state, previous);
|
|
578
|
+
previous.next = undefined;
|
|
579
|
+
let next = getNext(state, current);
|
|
580
|
+
current.next = previous;
|
|
581
|
+
while (!stops.includes(next.kind) && !isKind(current, "THEOREM", "PROPOSITION")) {
|
|
582
|
+
if (isKind(current, "DEF")) {
|
|
583
|
+
let head: SanyToken | undefined = previous;
|
|
584
|
+
if (isKind(head, "RBR", "RSB")) {
|
|
585
|
+
let depth = 1;
|
|
586
|
+
while (head.next !== undefined) {
|
|
587
|
+
head = head.next;
|
|
588
|
+
if (isKind(head, "LBR", "LSB") && depth === 1) break;
|
|
589
|
+
if (isKind(head, "LBR", "LSB") && depth > 1) depth--;
|
|
590
|
+
if (isKind(head, "RBR", "RSB")) depth++;
|
|
591
|
+
}
|
|
592
|
+
if (head.next === undefined) head = undefined;
|
|
593
|
+
else if (head.next.kind === IDENTIFIER) head = head.next;
|
|
594
|
+
} else if (head.kind === IDENTIFIER) {
|
|
595
|
+
const operator = head.next;
|
|
596
|
+
if (
|
|
597
|
+
isOperator(operator) &&
|
|
598
|
+
!POSTFIX_KINDS.has(operator.kind) &&
|
|
599
|
+
(!INFIX_KINDS.has(operator.kind) || operator.next?.kind === IDENTIFIER)
|
|
600
|
+
) {
|
|
601
|
+
if (operator.next?.kind === getKind("SUBSTITUTE")) head = previous;
|
|
602
|
+
else head = INFIX_KINDS.has(operator.kind) ? operator.next : operator;
|
|
603
|
+
}
|
|
604
|
+
} else if (isOperator(head)) head = head.next;
|
|
605
|
+
if (head !== undefined && head.next?.kind !== DEFBREAK) {
|
|
606
|
+
head.next = {
|
|
607
|
+
kind: DEFBREAK,
|
|
608
|
+
image: "Beginning of definition",
|
|
609
|
+
beginLine: head.beginLine,
|
|
610
|
+
beginColumn: head.beginColumn,
|
|
611
|
+
next: head.next,
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
previous = current;
|
|
617
|
+
current = next;
|
|
618
|
+
next = getNext(state, current);
|
|
619
|
+
current.next = previous;
|
|
620
|
+
}
|
|
621
|
+
while (previous !== undefined) {
|
|
622
|
+
current.next = next;
|
|
623
|
+
next = current;
|
|
624
|
+
current = previous;
|
|
625
|
+
previous = current.next;
|
|
626
|
+
}
|
|
627
|
+
current.next = next;
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// TLAplusParser.matchFcnConst: whether the next tokens are "<< ... >> \in" or a list of
|
|
631
|
+
// identifiers followed by \in.
|
|
632
|
+
const matchFcnConst = (state: ParserState) => {
|
|
633
|
+
let token = getToken(state, 1);
|
|
634
|
+
if (token.kind === getKind("LAB")) {
|
|
635
|
+
let depth = 1;
|
|
636
|
+
while (depth !== 0) {
|
|
637
|
+
token = getNext(state, token);
|
|
638
|
+
if (token.kind === getKind("LAB")) depth++;
|
|
639
|
+
if (token.kind === getKind("RAB")) depth--;
|
|
640
|
+
if (token.kind === EOF) return false;
|
|
641
|
+
}
|
|
642
|
+
return getNext(state, token).kind === getKind("IN");
|
|
643
|
+
}
|
|
644
|
+
if (token.kind !== IDENTIFIER) return false;
|
|
645
|
+
token = getNext(state, token);
|
|
646
|
+
while (token.kind === getKind("COMMA")) {
|
|
647
|
+
token = getNext(state, token);
|
|
648
|
+
if (token.kind !== IDENTIFIER) return false;
|
|
649
|
+
token = getNext(state, token);
|
|
650
|
+
}
|
|
651
|
+
return token.kind === getKind("IN");
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
// TLAplusParser.levelOfProofStepLexeme: -1 for <*> and -2 for <+>.
|
|
655
|
+
const getStepLevel = (token: SanyToken) => {
|
|
656
|
+
if (token.image[1] === "*") return -1;
|
|
657
|
+
return token.image[1] === "+" ? -2 : Number.parseInt(token.image.slice(1));
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
// TLAplusParser.getProofLevel. TLAplusParser's proofDepth is the length of proofLevels less 1.
|
|
661
|
+
const getProofLevel = (state: ParserState) => state.proofLevels.at(-1) ?? -1;
|
|
662
|
+
|
|
663
|
+
const beginsProof = (state: ParserState) => {
|
|
664
|
+
const token = getToken(state, 1);
|
|
665
|
+
if (token.image.length < 2) return false;
|
|
666
|
+
if (token.image[1] === "*") return state.proofLevels.length === 0;
|
|
667
|
+
if (token.image[1] === "+") return true;
|
|
668
|
+
if (
|
|
669
|
+
isNextKind(
|
|
670
|
+
"ProofStepLexeme",
|
|
671
|
+
"BareLevelLexeme",
|
|
672
|
+
"UnnumberedStepLexeme",
|
|
673
|
+
"ProofStepDotLexeme",
|
|
674
|
+
)(state)
|
|
675
|
+
) {
|
|
676
|
+
if (state.proofLevels.length === 0) return true;
|
|
677
|
+
return getProofLevel(state) >= 0 && getStepLevel(token) > getProofLevel(state);
|
|
678
|
+
}
|
|
679
|
+
return isNextKind("BY", "PROOF", "OBVIOUS", "OMITTED")(state);
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// TLAplusParser.correctLevel, which also sets the level of a proof from its first step.
|
|
683
|
+
const isCorrectLevel = (state: ParserState, token: SanyToken) => {
|
|
684
|
+
const level = getStepLevel(token);
|
|
685
|
+
const depth = state.proofLevels.length - 1;
|
|
686
|
+
const outerLevel = state.proofLevels[depth - 1] ?? -1;
|
|
687
|
+
const isFirstStep = state.proofLevels[depth] < 0;
|
|
688
|
+
if (isFirstStep) state.proofLevels[depth] = level < 0 ? outerLevel + 1 : level;
|
|
689
|
+
if (level === -1) return true;
|
|
690
|
+
if (level === -2) return isFirstStep;
|
|
691
|
+
return isFirstStep ? level > outerLevel : state.proofLevels[depth] === level;
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
const checkStepLevel = (problem: string) =>
|
|
695
|
+
action((state) =>
|
|
696
|
+
isCorrectLevel(state, state.token)
|
|
697
|
+
? undefined
|
|
698
|
+
: `${formatLocation(state, state.token)}: ${problem}`,
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const PREFIX_OPERATOR = tokens(
|
|
702
|
+
[
|
|
703
|
+
"op_26",
|
|
704
|
+
"op_29",
|
|
705
|
+
"op_58",
|
|
706
|
+
"CASESEP",
|
|
707
|
+
"op_61",
|
|
708
|
+
"op_112",
|
|
709
|
+
"op_113",
|
|
710
|
+
"op_114",
|
|
711
|
+
"op_115",
|
|
712
|
+
"op_116",
|
|
713
|
+
].map(getKind),
|
|
714
|
+
);
|
|
715
|
+
const NONEXP_PREFIX_OPERATOR = tokens([...PREFIX_OPERATOR.kinds, getKind("op_76")]);
|
|
716
|
+
const INFIX_OPERATOR = tokens(INFIX_KINDS);
|
|
717
|
+
const POSTFIX_OPERATOR = tokens(POSTFIX_KINDS);
|
|
718
|
+
const STEP_START = tokens(
|
|
719
|
+
[
|
|
720
|
+
"ProofStepLexeme",
|
|
721
|
+
"ProofImplicitStepLexeme",
|
|
722
|
+
"ProofStepDotLexeme",
|
|
723
|
+
"BareLevelLexeme",
|
|
724
|
+
"UnnumberedStepLexeme",
|
|
725
|
+
].map(getKind),
|
|
726
|
+
);
|
|
727
|
+
|
|
728
|
+
// The prefix operators and the operand that Expression and ExtendableExpr share. Each use is a
|
|
729
|
+
// separate lookahead site, as in the generated parser.
|
|
730
|
+
const getPrefixOperators = () =>
|
|
731
|
+
zeroOrMore(
|
|
732
|
+
lookahead({ expansion: [choice(PREFIX_OPERATOR, "<op_77>")], semantic: isAboveCurrent }),
|
|
733
|
+
choice("PrefixOp", "InfixOp"),
|
|
734
|
+
PUSH_OPERATOR,
|
|
735
|
+
);
|
|
736
|
+
const getOperand = () =>
|
|
737
|
+
choice(
|
|
738
|
+
sequence(
|
|
739
|
+
lookahead({ expansion: ["OpenStart"], semantic: isAboveCurrent }),
|
|
740
|
+
"OpenExpression",
|
|
741
|
+
PUSH_OPERAND,
|
|
742
|
+
),
|
|
743
|
+
sequence(lookahead({ semantic: isAboveCurrent }), "ExtendableExpr"),
|
|
744
|
+
);
|
|
745
|
+
|
|
746
|
+
const getUseItem = () =>
|
|
747
|
+
choice(sequence("<MODULE>", expect("identifier"), "Identifier"), "Expression");
|
|
748
|
+
|
|
749
|
+
const getAssumeProveItem = () =>
|
|
750
|
+
choice(
|
|
751
|
+
sequence(
|
|
752
|
+
lookahead({
|
|
753
|
+
semantic: (state) =>
|
|
754
|
+
isNextKind("ASSUME", "BOXASSUME")(state) ||
|
|
755
|
+
(getToken(state, 2).kind === getKind("COLONCOLON") &&
|
|
756
|
+
[getKind("ASSUME"), getKind("BOXASSUME")].includes(getToken(state, 3).kind)),
|
|
757
|
+
}),
|
|
758
|
+
"AssumeProve",
|
|
759
|
+
),
|
|
760
|
+
"NewSymb",
|
|
761
|
+
"Expression",
|
|
762
|
+
);
|
|
763
|
+
|
|
764
|
+
const getJunctionList = () => [
|
|
765
|
+
"JuncItem",
|
|
766
|
+
zeroOrMore(lookahead({ semantic: isNewBullet }), "JuncItem"),
|
|
767
|
+
];
|
|
768
|
+
|
|
769
|
+
const setFieldNameExpecting = (expecting: string) =>
|
|
770
|
+
action((state) => {
|
|
771
|
+
state.expecting = expecting;
|
|
772
|
+
setFieldName(state);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
const setTerminalProof = (isTerminalProof: boolean) =>
|
|
776
|
+
action((state) => {
|
|
777
|
+
state.isTerminalProof = isTerminalProof;
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
// An alternative of OperatorOrFunctionDefinition: a left-hand side, then "==", then a body.
|
|
781
|
+
const definition = (expecting: string, ...parts: Part[]) =>
|
|
782
|
+
sequence(
|
|
783
|
+
...parts.slice(0, -1),
|
|
784
|
+
expect("=="),
|
|
785
|
+
"<DEF>",
|
|
786
|
+
action((state) => {
|
|
787
|
+
belchDef(state);
|
|
788
|
+
state.expecting = expecting;
|
|
789
|
+
}),
|
|
790
|
+
parts[parts.length - 1],
|
|
791
|
+
);
|
|
792
|
+
|
|
793
|
+
const production = (
|
|
794
|
+
message: string | undefined,
|
|
795
|
+
parts: Part[],
|
|
796
|
+
exit?: Production["exit"],
|
|
797
|
+
): Production => ({
|
|
798
|
+
message,
|
|
799
|
+
body: sequence(...parts),
|
|
800
|
+
exit,
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
const PRODUCTIONS: Record<string, Production> = {
|
|
804
|
+
CompilationUnit: production(undefined, [action(belchDef), optional("Prelude"), "Module"]),
|
|
805
|
+
Prelude: production(undefined, [
|
|
806
|
+
"<BEGIN_PRAGMA>",
|
|
807
|
+
zeroOrMore(choice("<NUMBER>", "<IDENTIFIER>")),
|
|
808
|
+
]),
|
|
809
|
+
Module: production("Module definition", [
|
|
810
|
+
expect("---- MODULE"),
|
|
811
|
+
"BeginModule",
|
|
812
|
+
expect("EXTENDS clause or module body"),
|
|
813
|
+
"Extends",
|
|
814
|
+
expect("Module body"),
|
|
815
|
+
"Body",
|
|
816
|
+
expect("==== or more Module body"),
|
|
817
|
+
"EndModule",
|
|
818
|
+
]),
|
|
819
|
+
BeginModule: production("Begin module", [
|
|
820
|
+
expect("---- MODULE (beginning of module)"),
|
|
821
|
+
choice("<_BM0>", "<_BM1>", "<_BM2>"),
|
|
822
|
+
setFieldNameExpecting("Identifier"),
|
|
823
|
+
"Identifier",
|
|
824
|
+
action((state) => {
|
|
825
|
+
state.moduleName ??= state.token.image;
|
|
826
|
+
state.expecting = "----";
|
|
827
|
+
}),
|
|
828
|
+
"<SEPARATOR>",
|
|
829
|
+
]),
|
|
830
|
+
EndModule: production(undefined, ["<END_MODULE>"]),
|
|
831
|
+
Extends: production("Extends", [
|
|
832
|
+
optional(
|
|
833
|
+
"<EXTENDS>",
|
|
834
|
+
setFieldNameExpecting("Identifier"),
|
|
835
|
+
"Identifier",
|
|
836
|
+
expect("comma or module body"),
|
|
837
|
+
zeroOrMore("<COMMA>", setFieldNameExpecting("Identifier"), "Identifier"),
|
|
838
|
+
),
|
|
839
|
+
]),
|
|
840
|
+
Body: production("Module body", [
|
|
841
|
+
expect("LOCAL, INSTANCE, PROOF, ASSUMPTION, THEOREM, RECURSIVE, declaration, or definition"),
|
|
842
|
+
zeroOrMore(
|
|
843
|
+
choice(
|
|
844
|
+
"<SEPARATOR>",
|
|
845
|
+
"VariableDeclaration",
|
|
846
|
+
"ParamDeclaration",
|
|
847
|
+
sequence(lookahead({ amount: 2 }), "OperatorOrFunctionDefinition"),
|
|
848
|
+
"Recursive",
|
|
849
|
+
sequence(lookahead({ amount: 2 }), "Instance"),
|
|
850
|
+
sequence(lookahead({ amount: 2 }), "Assumption"),
|
|
851
|
+
sequence(lookahead({ amount: 2 }), "Theorem"),
|
|
852
|
+
sequence(lookahead({ amount: 1 }), "Module", action(belchDef)),
|
|
853
|
+
sequence(
|
|
854
|
+
lookahead({
|
|
855
|
+
semantic: (state) =>
|
|
856
|
+
(isNextKind("USE")(state) && getToken(state, 2).kind !== getKind("ONLY")) ||
|
|
857
|
+
isNextKind("HIDE")(state),
|
|
858
|
+
}),
|
|
859
|
+
"UseOrHideOrBy",
|
|
860
|
+
),
|
|
861
|
+
),
|
|
862
|
+
),
|
|
863
|
+
]),
|
|
864
|
+
VariableDeclaration: production("variable declaration", [
|
|
865
|
+
"<VARIABLE>",
|
|
866
|
+
expect("Identifier"),
|
|
867
|
+
"Identifier",
|
|
868
|
+
expect("comma or module body"),
|
|
869
|
+
zeroOrMore("<COMMA>", expect("Identifier"), "Identifier"),
|
|
870
|
+
]),
|
|
871
|
+
ParamDeclaration: production("Parameter declaration", [
|
|
872
|
+
expect("CONSTANT"),
|
|
873
|
+
"ParamSubDecl",
|
|
874
|
+
expect("Identifier, operator or _"),
|
|
875
|
+
"ConstantDeclarationItems",
|
|
876
|
+
expect(","),
|
|
877
|
+
zeroOrMore("<COMMA>", expect("Identifier, operator or _"), "ConstantDeclarationItems"),
|
|
878
|
+
]),
|
|
879
|
+
ParamSubDecl: production("Parameter declaration item", ["<CONSTANT>"]),
|
|
880
|
+
Recursive: production("Recursive", [
|
|
881
|
+
expect("RECURSIVE"),
|
|
882
|
+
"<RECURSIVE>",
|
|
883
|
+
expect("Identifier, operator or _"),
|
|
884
|
+
"ConstantDeclarationItems",
|
|
885
|
+
expect(","),
|
|
886
|
+
zeroOrMore(
|
|
887
|
+
"<COMMA>",
|
|
888
|
+
expect("Identifier, operator or _"),
|
|
889
|
+
"ConstantDeclarationItems",
|
|
890
|
+
expect("`,' or `)'"),
|
|
891
|
+
),
|
|
892
|
+
]),
|
|
893
|
+
ConstantDeclarationItems: production("Constant declaration items", [
|
|
894
|
+
expect("Identifier, _ or prefix op"),
|
|
895
|
+
choice(
|
|
896
|
+
sequence(
|
|
897
|
+
"Identifier",
|
|
898
|
+
expect("(, comma, or Module Body"),
|
|
899
|
+
optional(
|
|
900
|
+
lookahead({ amount: 2 }),
|
|
901
|
+
"<LBR>",
|
|
902
|
+
expect("_"),
|
|
903
|
+
"<US>",
|
|
904
|
+
expect("comma or )"),
|
|
905
|
+
zeroOrMore("<COMMA>", expect("_"), "<US>", expect("comma or )")),
|
|
906
|
+
"<RBR>",
|
|
907
|
+
),
|
|
908
|
+
),
|
|
909
|
+
sequence("NonExpPrefixOp", expect("_"), "<US>"),
|
|
910
|
+
sequence(
|
|
911
|
+
"<US>",
|
|
912
|
+
expect("prefix or postfix operator"),
|
|
913
|
+
choice(sequence("InfixOp", expect("_"), "<US>"), "PostfixOp"),
|
|
914
|
+
),
|
|
915
|
+
),
|
|
916
|
+
]),
|
|
917
|
+
OperatorOrFunctionDefinition: production("Definition", [
|
|
918
|
+
expect("LOCAL, Identifier or Operator Symbol"),
|
|
919
|
+
optional("<LOCAL>"),
|
|
920
|
+
"<DEFBREAK>",
|
|
921
|
+
expect("LOCAL, Identifier or Operator Symbol"),
|
|
922
|
+
choice(
|
|
923
|
+
definition(
|
|
924
|
+
"Expression",
|
|
925
|
+
lookahead({ expansion: ["<IDENTIFIER>", "<LSB>"] }),
|
|
926
|
+
"Identifier",
|
|
927
|
+
expect("["),
|
|
928
|
+
"<LSB>",
|
|
929
|
+
expect("Identifier"),
|
|
930
|
+
"QuantBound",
|
|
931
|
+
expect("COMMA or ]"),
|
|
932
|
+
zeroOrMore("<COMMA>", expect("Identifier"), "QuantBound"),
|
|
933
|
+
"<RSB>",
|
|
934
|
+
"Expression",
|
|
935
|
+
),
|
|
936
|
+
definition(
|
|
937
|
+
"Expression",
|
|
938
|
+
lookahead({ expansion: ["<IDENTIFIER>", POSTFIX_OPERATOR] }),
|
|
939
|
+
"PostfixLHS",
|
|
940
|
+
"Expression",
|
|
941
|
+
),
|
|
942
|
+
definition(
|
|
943
|
+
"Expression",
|
|
944
|
+
lookahead({ expansion: ["<IDENTIFIER>", INFIX_OPERATOR] }),
|
|
945
|
+
"InfixLHS",
|
|
946
|
+
"Expression",
|
|
947
|
+
),
|
|
948
|
+
definition(
|
|
949
|
+
"Expression or Instance",
|
|
950
|
+
lookahead({ expansion: ["<IDENTIFIER>", choice("<LBR>", "<DEF>")] }),
|
|
951
|
+
"IdentLHS",
|
|
952
|
+
choice("Expression", "Instantiation"),
|
|
953
|
+
),
|
|
954
|
+
definition("Expression", "PrefixLHS", "Expression"),
|
|
955
|
+
),
|
|
956
|
+
]),
|
|
957
|
+
IdentifierTuple: production("Identifier tuple", [
|
|
958
|
+
"<LAB>",
|
|
959
|
+
expect("Identifier or >>"),
|
|
960
|
+
optional(
|
|
961
|
+
"Identifier",
|
|
962
|
+
expect("COMMA or >>"),
|
|
963
|
+
zeroOrMore("<COMMA>", expect("COMMA or >>"), "Identifier", expect("COMMA or >>")),
|
|
964
|
+
),
|
|
965
|
+
"<RAB>",
|
|
966
|
+
]),
|
|
967
|
+
IdentLHS: production("Identifier LHS", [
|
|
968
|
+
"Identifier",
|
|
969
|
+
expect("( or =="),
|
|
970
|
+
optional(
|
|
971
|
+
"<LBR>",
|
|
972
|
+
expect("Identifier Declaration, prefix op, _ or )"),
|
|
973
|
+
choice("IdentDecl", "SomeFixDecl"),
|
|
974
|
+
expect("COMMA or )"),
|
|
975
|
+
zeroOrMore(
|
|
976
|
+
"<COMMA>",
|
|
977
|
+
expect("Identifier Declaration, prefix op or _"),
|
|
978
|
+
choice("IdentDecl", "SomeFixDecl"),
|
|
979
|
+
expect("COMMA or )"),
|
|
980
|
+
),
|
|
981
|
+
"<RBR>",
|
|
982
|
+
),
|
|
983
|
+
]),
|
|
984
|
+
PrefixLHS: production("Prefix LHS", [NONEXP_PREFIX_OPERATOR, expect("Identifier"), "Identifier"]),
|
|
985
|
+
InfixLHS: production("Infix LHS", ["Identifier", INFIX_OPERATOR, "Identifier"]),
|
|
986
|
+
PostfixLHS: production("Postfix LHS", ["Identifier", POSTFIX_OPERATOR]),
|
|
987
|
+
IdentDecl: production("Identifier Declation", [
|
|
988
|
+
"Identifier",
|
|
989
|
+
expect("( or ..."),
|
|
990
|
+
optional(
|
|
991
|
+
"<LBR>",
|
|
992
|
+
expect("_"),
|
|
993
|
+
"<US>",
|
|
994
|
+
expect("COMMA or )"),
|
|
995
|
+
zeroOrMore("<COMMA>", expect("_"), "<US>", expect("COMMA or )")),
|
|
996
|
+
"<RBR>",
|
|
997
|
+
),
|
|
998
|
+
]),
|
|
999
|
+
SomeFixDecl: production("Op. Symbol Declaration", [
|
|
1000
|
+
choice(
|
|
1001
|
+
sequence("NonExpPrefixOp", expect("_"), "<US>"),
|
|
1002
|
+
sequence(
|
|
1003
|
+
"<US>",
|
|
1004
|
+
expect("infix or postfix operator"),
|
|
1005
|
+
choice(sequence("InfixOp", expect("_"), "<US>"), "PostfixOp"),
|
|
1006
|
+
),
|
|
1007
|
+
),
|
|
1008
|
+
]),
|
|
1009
|
+
Instance: production("Instance", [
|
|
1010
|
+
expect("LOCAL or instance"),
|
|
1011
|
+
optional("<LOCAL>"),
|
|
1012
|
+
"Instantiation",
|
|
1013
|
+
expect("COMMA or Module Body"),
|
|
1014
|
+
]),
|
|
1015
|
+
Instantiation: production("NonLocalInstance", [
|
|
1016
|
+
"<INSTANCE>",
|
|
1017
|
+
setFieldNameExpecting("Module identifier"),
|
|
1018
|
+
"Identifier",
|
|
1019
|
+
expect("WITH or another definition."),
|
|
1020
|
+
optional(
|
|
1021
|
+
"<WITH>",
|
|
1022
|
+
expect(""),
|
|
1023
|
+
"Substitution",
|
|
1024
|
+
expect(""),
|
|
1025
|
+
zeroOrMore(lookahead({ amount: 3 }), "<COMMA>", expect(""), "Substitution", expect("")),
|
|
1026
|
+
),
|
|
1027
|
+
]),
|
|
1028
|
+
Substitution: production("Substitution", [
|
|
1029
|
+
choice("Identifier", "NonExpPrefixOp", "InfixOp", "PostfixOp"),
|
|
1030
|
+
expect("<-"),
|
|
1031
|
+
"<SUBSTITUTE>",
|
|
1032
|
+
expect("Expression or Op. Symbol"),
|
|
1033
|
+
"OpOrExpr",
|
|
1034
|
+
]),
|
|
1035
|
+
PrefixOp: production(undefined, [PREFIX_OPERATOR, SET_LAST_OPERATOR]),
|
|
1036
|
+
NonExpPrefixOp: production(undefined, [NONEXP_PREFIX_OPERATOR, SET_LAST_OPERATOR]),
|
|
1037
|
+
InfixOp: production("Infix Op", [INFIX_OPERATOR, SET_LAST_OPERATOR]),
|
|
1038
|
+
PostfixOp: production(undefined, [POSTFIX_OPERATOR, SET_LAST_OPERATOR]),
|
|
1039
|
+
Identifier: production(undefined, ["<IDENTIFIER>"]),
|
|
1040
|
+
Assumption: production("Assumption", [
|
|
1041
|
+
expect("ASSUM..."),
|
|
1042
|
+
choice("<ASSUMPTION>", "<ASSUME>"),
|
|
1043
|
+
optional(lookahead({ amount: 2 }), optional("<DEFBREAK>"), "Identifier", expect("=="), "<DEF>"),
|
|
1044
|
+
action((state) => {
|
|
1045
|
+
belchDef(state);
|
|
1046
|
+
state.expecting = "Expression";
|
|
1047
|
+
}),
|
|
1048
|
+
"Expression",
|
|
1049
|
+
]),
|
|
1050
|
+
// A label returns from AssumeProve after the labeled AssumeProve, which a choice expresses.
|
|
1051
|
+
AssumeProve: production("Assume-Prove", [
|
|
1052
|
+
choice(
|
|
1053
|
+
sequence("Identifier", "<COLONCOLON>", "AssumeProve"),
|
|
1054
|
+
sequence(
|
|
1055
|
+
choice("<ASSUME>", "<BOXASSUME>"),
|
|
1056
|
+
expect("Expression, Declaration, or AssumeProve"),
|
|
1057
|
+
getAssumeProveItem(),
|
|
1058
|
+
expect("PROVE or `,'"),
|
|
1059
|
+
zeroOrMore(
|
|
1060
|
+
"<COMMA>",
|
|
1061
|
+
expect("Expression, Declaration, or AssumeProve"),
|
|
1062
|
+
getAssumeProveItem(),
|
|
1063
|
+
expect("PROVE or `,'"),
|
|
1064
|
+
),
|
|
1065
|
+
choice("<PROVE>", "<BOXPROVE>"),
|
|
1066
|
+
expect("Expression"),
|
|
1067
|
+
"Expression",
|
|
1068
|
+
),
|
|
1069
|
+
),
|
|
1070
|
+
]),
|
|
1071
|
+
NewSymb: production("NEW symbol declaration", [
|
|
1072
|
+
expect("NEW, CONSTANT, VARIABLE, STATE, ACTION, or TEMPORAL"),
|
|
1073
|
+
choice(
|
|
1074
|
+
sequence(
|
|
1075
|
+
lookahead({ amount: 2 }),
|
|
1076
|
+
choice(sequence(lookahead({ amount: 2 }), "<NEW>", "<CONSTANT>"), "<NEW>", "<CONSTANT>"),
|
|
1077
|
+
expect("Constant declaration"),
|
|
1078
|
+
choice(
|
|
1079
|
+
sequence(
|
|
1080
|
+
lookahead({ amount: 2 }),
|
|
1081
|
+
"IdentDecl",
|
|
1082
|
+
optional(
|
|
1083
|
+
"<IN>",
|
|
1084
|
+
action((state) => {
|
|
1085
|
+
const declaration = getHeir(state, 2);
|
|
1086
|
+
return declaration.heirs.length > 1
|
|
1087
|
+
? `declared symbol with arguments before \\in at ${formatLocation(state, declaration.first, declaration.last)}`
|
|
1088
|
+
: undefined;
|
|
1089
|
+
}),
|
|
1090
|
+
expect("Expression"),
|
|
1091
|
+
"Expression",
|
|
1092
|
+
),
|
|
1093
|
+
),
|
|
1094
|
+
"SomeFixDecl",
|
|
1095
|
+
),
|
|
1096
|
+
),
|
|
1097
|
+
sequence(
|
|
1098
|
+
lookahead({ amount: 2 }),
|
|
1099
|
+
optional("<NEW>"),
|
|
1100
|
+
"<VARIABLE>",
|
|
1101
|
+
expect("Identifier"),
|
|
1102
|
+
"Identifier",
|
|
1103
|
+
),
|
|
1104
|
+
sequence(
|
|
1105
|
+
lookahead({ amount: 2 }),
|
|
1106
|
+
optional("<NEW>"),
|
|
1107
|
+
choice("<STATE>", "<ACTION>", "<TEMPORAL>"),
|
|
1108
|
+
expect("Declaration"),
|
|
1109
|
+
choice("IdentDecl", "SomeFixDecl"),
|
|
1110
|
+
),
|
|
1111
|
+
),
|
|
1112
|
+
]),
|
|
1113
|
+
MaybeBound: production("Domain binding", [optional("<IN>", expect("Expression"), "Expression")]),
|
|
1114
|
+
Theorem: production("Theorem", [
|
|
1115
|
+
expect("THEOREM, PROPOSITION"),
|
|
1116
|
+
choice("<THEOREM>", "<PROPOSITION>"),
|
|
1117
|
+
expect("Identifier, Assume-Prove or Expression"),
|
|
1118
|
+
optional(lookahead({ amount: 2 }), "Identifier", expect("=="), "<DEF>"),
|
|
1119
|
+
action(belchDef),
|
|
1120
|
+
choice(
|
|
1121
|
+
sequence(
|
|
1122
|
+
lookahead({ amount: 3 }),
|
|
1123
|
+
lookahead({ semantic: isNextKind("ASSUME", "BOXASSUME") }),
|
|
1124
|
+
"AssumeProve",
|
|
1125
|
+
),
|
|
1126
|
+
"Expression",
|
|
1127
|
+
),
|
|
1128
|
+
optional(lookahead({ semantic: beginsProof }), "Proof"),
|
|
1129
|
+
]),
|
|
1130
|
+
Proof: production("Proof", [
|
|
1131
|
+
action((state) => {
|
|
1132
|
+
state.proofLevels.push(-1);
|
|
1133
|
+
}),
|
|
1134
|
+
choice(
|
|
1135
|
+
sequence(
|
|
1136
|
+
lookahead({
|
|
1137
|
+
semantic: (state) => isNextKind("BY")(state) || getToken(state, 2).kind === getKind("BY"),
|
|
1138
|
+
}),
|
|
1139
|
+
"UseOrHideOrBy",
|
|
1140
|
+
),
|
|
1141
|
+
sequence(lookahead({ amount: 2 }), optional("<PROOF>"), choice("<OBVIOUS>", "<OMITTED>")),
|
|
1142
|
+
sequence(
|
|
1143
|
+
optional(lookahead({ amount: 2 }), "<PROOF>"),
|
|
1144
|
+
zeroOrMore(
|
|
1145
|
+
lookahead({ semantic: (state) => getToken(state, 2).kind !== getKind("QED") }),
|
|
1146
|
+
"Step",
|
|
1147
|
+
expect("a proof step"),
|
|
1148
|
+
),
|
|
1149
|
+
"QEDStep",
|
|
1150
|
+
),
|
|
1151
|
+
),
|
|
1152
|
+
action((state) => {
|
|
1153
|
+
state.proofLevels.pop();
|
|
1154
|
+
}),
|
|
1155
|
+
]),
|
|
1156
|
+
UseOrHideOrBy: production("UseOrHideOrBy", [
|
|
1157
|
+
choice(
|
|
1158
|
+
sequence(optional("<PROOF>"), "<BY>", setTerminalProof(true), optional("<ONLY>")),
|
|
1159
|
+
sequence("<USE>", setTerminalProof(false), optional("<ONLY>")),
|
|
1160
|
+
sequence("<HIDE>", setTerminalProof(false)),
|
|
1161
|
+
),
|
|
1162
|
+
expect("an expression, `MODULE' or `DEF'"),
|
|
1163
|
+
optional(
|
|
1164
|
+
getUseItem(),
|
|
1165
|
+
zeroOrMore(
|
|
1166
|
+
"<COMMA>",
|
|
1167
|
+
expect("MODULE or expression"),
|
|
1168
|
+
getUseItem(),
|
|
1169
|
+
action((state) => {
|
|
1170
|
+
state.expecting = state.isTerminalProof
|
|
1171
|
+
? "comma, DEF, or [.]"
|
|
1172
|
+
: "comma, DEF, or proof step";
|
|
1173
|
+
}),
|
|
1174
|
+
),
|
|
1175
|
+
),
|
|
1176
|
+
optional(
|
|
1177
|
+
"<DF>",
|
|
1178
|
+
expect("MODULE or expression"),
|
|
1179
|
+
getUseItem(),
|
|
1180
|
+
zeroOrMore(
|
|
1181
|
+
"<COMMA>",
|
|
1182
|
+
expect("MODULE or expression"),
|
|
1183
|
+
getUseItem(),
|
|
1184
|
+
action((state) => {
|
|
1185
|
+
state.expecting = state.isTerminalProof ? "comma or [.]" : "comma or proof step";
|
|
1186
|
+
}),
|
|
1187
|
+
),
|
|
1188
|
+
),
|
|
1189
|
+
action((state) => {
|
|
1190
|
+
if (state.isTerminalProof) state.expecting = "[.]";
|
|
1191
|
+
}),
|
|
1192
|
+
]),
|
|
1193
|
+
QEDStep: production("QED step", [
|
|
1194
|
+
expect("Step number"),
|
|
1195
|
+
STEP_START,
|
|
1196
|
+
checkStepLevel("QED step's number has bad level."),
|
|
1197
|
+
expect("QED"),
|
|
1198
|
+
"<QED>",
|
|
1199
|
+
optional(lookahead({ semantic: beginsProof }), "Proof"),
|
|
1200
|
+
]),
|
|
1201
|
+
Step: production("Step", [
|
|
1202
|
+
expect("Step number"),
|
|
1203
|
+
STEP_START,
|
|
1204
|
+
checkStepLevel("step's number has bad level."),
|
|
1205
|
+
expect("proof step"),
|
|
1206
|
+
choice(
|
|
1207
|
+
sequence(lookahead({ semantic: isNextKind("USE", "HIDE") }), "UseOrHideOrBy"),
|
|
1208
|
+
sequence(lookahead({ semantic: isNextKind("INSTANCE") }), "Instantiation"),
|
|
1209
|
+
sequence(lookahead({ semantic: isNextKind("DEFBREAK", "DEFINE") }), "DefStep"),
|
|
1210
|
+
"HaveStep",
|
|
1211
|
+
"TakeStep",
|
|
1212
|
+
"WitnessStep",
|
|
1213
|
+
"PickStep",
|
|
1214
|
+
"CaseStep",
|
|
1215
|
+
"AssertStep",
|
|
1216
|
+
),
|
|
1217
|
+
optional(
|
|
1218
|
+
lookahead({ semantic: beginsProof }),
|
|
1219
|
+
action((state) => {
|
|
1220
|
+
const step = getHeir(state, 1);
|
|
1221
|
+
return ["PickStep", "CaseStep", "AssertStep"].includes(step.kind)
|
|
1222
|
+
? undefined
|
|
1223
|
+
: `${formatLocation(state, step.first, step.last)}: proof of step that does not take a proof.`;
|
|
1224
|
+
}),
|
|
1225
|
+
"Proof",
|
|
1226
|
+
),
|
|
1227
|
+
]),
|
|
1228
|
+
DefStep: production("DefStep", [
|
|
1229
|
+
optional("<DEFINE>"),
|
|
1230
|
+
oneOrMore(lookahead({ amount: 2 }), "OperatorOrFunctionDefinition"),
|
|
1231
|
+
]),
|
|
1232
|
+
HaveStep: production("HaveStep", ["<HAVE>", expect("expression"), "Expression"]),
|
|
1233
|
+
TakeStep: production("TakeStep", [
|
|
1234
|
+
"<TAKE>",
|
|
1235
|
+
expect("identifier"),
|
|
1236
|
+
choice(
|
|
1237
|
+
sequence(
|
|
1238
|
+
lookahead({
|
|
1239
|
+
expansion: [
|
|
1240
|
+
choice(
|
|
1241
|
+
sequence("<IDENTIFIER>", zeroOrMore("<COMMA>", "<IDENTIFIER>"), "<IN>"),
|
|
1242
|
+
"<LAB>",
|
|
1243
|
+
),
|
|
1244
|
+
],
|
|
1245
|
+
}),
|
|
1246
|
+
"QuantBound",
|
|
1247
|
+
expect("comma or step"),
|
|
1248
|
+
zeroOrMore(
|
|
1249
|
+
"<COMMA>",
|
|
1250
|
+
expect("identifier or tuple of identifiers"),
|
|
1251
|
+
"QuantBound",
|
|
1252
|
+
expect("comma or proof step"),
|
|
1253
|
+
),
|
|
1254
|
+
),
|
|
1255
|
+
sequence(
|
|
1256
|
+
"Identifier",
|
|
1257
|
+
expect("comma or proof step"),
|
|
1258
|
+
zeroOrMore("<COMMA>", expect("identifier"), "Identifier", expect("comma or proof step")),
|
|
1259
|
+
),
|
|
1260
|
+
),
|
|
1261
|
+
]),
|
|
1262
|
+
WitnessStep: production("WitnessStep", [
|
|
1263
|
+
"<WITNESS>",
|
|
1264
|
+
expect("expression"),
|
|
1265
|
+
"Expression",
|
|
1266
|
+
zeroOrMore("<COMMA>", expect("expression"), "Expression", expect("comma or colon")),
|
|
1267
|
+
]),
|
|
1268
|
+
PickStep: production("PickStep", [
|
|
1269
|
+
"<PICK>",
|
|
1270
|
+
expect("identifier"),
|
|
1271
|
+
choice(
|
|
1272
|
+
sequence(
|
|
1273
|
+
lookahead({
|
|
1274
|
+
expansion: ["<IDENTIFIER>", zeroOrMore("<COMMA>", "<IDENTIFIER>"), "<COLON>"],
|
|
1275
|
+
}),
|
|
1276
|
+
"Identifier",
|
|
1277
|
+
expect("comma, or colon"),
|
|
1278
|
+
zeroOrMore("<COMMA>", expect("identifier"), "Identifier", expect("comma or colon")),
|
|
1279
|
+
),
|
|
1280
|
+
sequence(
|
|
1281
|
+
"QuantBound",
|
|
1282
|
+
expect("comma or colon"),
|
|
1283
|
+
zeroOrMore(
|
|
1284
|
+
"<COMMA>",
|
|
1285
|
+
expect("identifier or tuple of identifiers"),
|
|
1286
|
+
"QuantBound",
|
|
1287
|
+
expect("comma or colon"),
|
|
1288
|
+
),
|
|
1289
|
+
),
|
|
1290
|
+
),
|
|
1291
|
+
"<COLON>",
|
|
1292
|
+
expect("expression"),
|
|
1293
|
+
"Expression",
|
|
1294
|
+
]),
|
|
1295
|
+
CaseStep: production("CaseStep", ["<CASE>", expect("expression"), "Expression"]),
|
|
1296
|
+
AssertStep: production("AssertStep", [
|
|
1297
|
+
optional("<SUFFICES>", expect("expression or ASSUME/PROVE")),
|
|
1298
|
+
choice(
|
|
1299
|
+
"Expression",
|
|
1300
|
+
sequence(lookahead({ semantic: isNextKind("ASSUME", "BOXASSUME") }), "AssumeProve"),
|
|
1301
|
+
),
|
|
1302
|
+
]),
|
|
1303
|
+
ParenthesesExpression: production(undefined, [
|
|
1304
|
+
choice("ParenExpr", "BraceCases", "SBracketCases", "TupleOrAction", "FairnessExpr"),
|
|
1305
|
+
]),
|
|
1306
|
+
OpenExpression: production(undefined, [
|
|
1307
|
+
choice("SomeQuant", "SomeTQuant", "IfThenElse", "Case", "LetIn", "UnboundOrBoundChoose"),
|
|
1308
|
+
]),
|
|
1309
|
+
String: production("String", ["<STRING_LITERAL>"]),
|
|
1310
|
+
Number: production(undefined, [
|
|
1311
|
+
"<NUMBER_LITERAL>",
|
|
1312
|
+
optional(lookahead({ amount: 2 }), "<DOT>", "<NUMBER_LITERAL>"),
|
|
1313
|
+
]),
|
|
1314
|
+
OpArgs: production("Optional Arguments", [
|
|
1315
|
+
"<LBR>",
|
|
1316
|
+
"OpOrExpr",
|
|
1317
|
+
zeroOrMore("<COMMA>", "OpOrExpr"),
|
|
1318
|
+
"<RBR>",
|
|
1319
|
+
]),
|
|
1320
|
+
OpOrExpr: production(
|
|
1321
|
+
undefined,
|
|
1322
|
+
[
|
|
1323
|
+
choice(
|
|
1324
|
+
sequence(
|
|
1325
|
+
lookahead({
|
|
1326
|
+
amount: 2,
|
|
1327
|
+
expansion: [
|
|
1328
|
+
choice(NONEXP_PREFIX_OPERATOR, INFIX_OPERATOR, POSTFIX_OPERATOR),
|
|
1329
|
+
tokens(
|
|
1330
|
+
[
|
|
1331
|
+
"COMMA",
|
|
1332
|
+
"RBR",
|
|
1333
|
+
"DEFBREAK",
|
|
1334
|
+
"LOCAL",
|
|
1335
|
+
"INSTANCE",
|
|
1336
|
+
"THEOREM",
|
|
1337
|
+
"ASSUME",
|
|
1338
|
+
"END_MODULE",
|
|
1339
|
+
"SEPARATOR",
|
|
1340
|
+
"_BM0",
|
|
1341
|
+
"ASSUMPTION",
|
|
1342
|
+
"CONSTANT",
|
|
1343
|
+
"VARIABLE",
|
|
1344
|
+
"RECURSIVE",
|
|
1345
|
+
].map(getKind),
|
|
1346
|
+
),
|
|
1347
|
+
],
|
|
1348
|
+
semantic: isAboveCurrent,
|
|
1349
|
+
}),
|
|
1350
|
+
choice(
|
|
1351
|
+
"NonExpPrefixOp",
|
|
1352
|
+
sequence(
|
|
1353
|
+
"InfixOp",
|
|
1354
|
+
action((state) =>
|
|
1355
|
+
state.token.image === "\\X" || state.token.image === "\\times"
|
|
1356
|
+
? `${formatLocation(state, state.token)}: \\X may not be used as an infix operator.`
|
|
1357
|
+
: undefined,
|
|
1358
|
+
),
|
|
1359
|
+
),
|
|
1360
|
+
"PostfixOp",
|
|
1361
|
+
),
|
|
1362
|
+
),
|
|
1363
|
+
sequence(lookahead({ expansion: ["<LAMBDA>"], semantic: isAboveCurrent }), "Lambda"),
|
|
1364
|
+
"Expression",
|
|
1365
|
+
),
|
|
1366
|
+
],
|
|
1367
|
+
(frame) => {
|
|
1368
|
+
frame.node = frame.heirs[0];
|
|
1369
|
+
},
|
|
1370
|
+
),
|
|
1371
|
+
ParenExpr: production(undefined, ["<LBR>", "Expression", "<RBR>"]),
|
|
1372
|
+
SomeQuant: production("Quantified form", [
|
|
1373
|
+
choice("<EXISTS>", "<FORALL>"),
|
|
1374
|
+
choice(
|
|
1375
|
+
sequence(
|
|
1376
|
+
lookahead({
|
|
1377
|
+
expansion: ["<IDENTIFIER>", zeroOrMore("<COMMA>", "<IDENTIFIER>"), "<COLON>"],
|
|
1378
|
+
}),
|
|
1379
|
+
"Identifier",
|
|
1380
|
+
zeroOrMore("<COMMA>", "Identifier"),
|
|
1381
|
+
),
|
|
1382
|
+
sequence("QuantBound", zeroOrMore("<COMMA>", "QuantBound")),
|
|
1383
|
+
),
|
|
1384
|
+
"<COLON>",
|
|
1385
|
+
"Expression",
|
|
1386
|
+
]),
|
|
1387
|
+
SomeTQuant: production("Bound Quantified Expression", [
|
|
1388
|
+
choice("<T_EXISTS>", "<T_FORALL>"),
|
|
1389
|
+
"Identifier",
|
|
1390
|
+
zeroOrMore("<COMMA>", "Identifier"),
|
|
1391
|
+
"<COLON>",
|
|
1392
|
+
"Expression",
|
|
1393
|
+
]),
|
|
1394
|
+
QuantBound: production("Quant Bound", [
|
|
1395
|
+
choice(
|
|
1396
|
+
"IdentifierTuple",
|
|
1397
|
+
sequence("Identifier", zeroOrMore("<COMMA>", "Identifier", expect(", or \\in"))),
|
|
1398
|
+
),
|
|
1399
|
+
"<IN>",
|
|
1400
|
+
"Expression",
|
|
1401
|
+
]),
|
|
1402
|
+
BraceCases: production("Some { } form", [
|
|
1403
|
+
"<LBC>",
|
|
1404
|
+
optional(
|
|
1405
|
+
choice(
|
|
1406
|
+
sequence(
|
|
1407
|
+
lookahead({ semantic: matchFcnConst }),
|
|
1408
|
+
choice("IdentifierTuple", "Identifier"),
|
|
1409
|
+
expect("\\in"),
|
|
1410
|
+
"<IN>",
|
|
1411
|
+
"Expression",
|
|
1412
|
+
expect("':', ',' or '}'"),
|
|
1413
|
+
optional(choice(sequence("<COLON>", "Expression"), oneOrMore("<COMMA>", "Expression"))),
|
|
1414
|
+
),
|
|
1415
|
+
sequence(
|
|
1416
|
+
lookahead({ expansion: [choice("IdentifierTuple", "<IDENTIFIER>"), "<COMMA>"] }),
|
|
1417
|
+
"Expression",
|
|
1418
|
+
zeroOrMore("<COMMA>", "Expression"),
|
|
1419
|
+
),
|
|
1420
|
+
sequence(
|
|
1421
|
+
lookahead({ expansion: [choice("IdentifierTuple", "<IDENTIFIER>"), "<COLON>"] }),
|
|
1422
|
+
"Expression",
|
|
1423
|
+
"<COLON>",
|
|
1424
|
+
"QuantBound",
|
|
1425
|
+
zeroOrMore("<COMMA>", "QuantBound"),
|
|
1426
|
+
),
|
|
1427
|
+
sequence(
|
|
1428
|
+
"Expression",
|
|
1429
|
+
optional(
|
|
1430
|
+
choice(
|
|
1431
|
+
sequence(
|
|
1432
|
+
"<COLON>",
|
|
1433
|
+
action((state) => {
|
|
1434
|
+
const expression = getHeir(state, 2);
|
|
1435
|
+
return expression.kind === "InfixExpr" &&
|
|
1436
|
+
expression.heirs[1].first.image === "\\in"
|
|
1437
|
+
? `Form {a \\in b : c \\in d }, at line ${state.token.beginLine}, is not allowed`
|
|
1438
|
+
: undefined;
|
|
1439
|
+
}),
|
|
1440
|
+
"QuantBound",
|
|
1441
|
+
zeroOrMore("<COMMA>", "QuantBound"),
|
|
1442
|
+
),
|
|
1443
|
+
oneOrMore("<COMMA>", "Expression"),
|
|
1444
|
+
),
|
|
1445
|
+
),
|
|
1446
|
+
),
|
|
1447
|
+
),
|
|
1448
|
+
),
|
|
1449
|
+
"<RBC>",
|
|
1450
|
+
]),
|
|
1451
|
+
SBracketCases: production("Some [] Form", [
|
|
1452
|
+
"<LSB>",
|
|
1453
|
+
choice(
|
|
1454
|
+
sequence(
|
|
1455
|
+
lookahead({ semantic: matchFcnConst }),
|
|
1456
|
+
"QuantBound",
|
|
1457
|
+
zeroOrMore("<COMMA>", "QuantBound"),
|
|
1458
|
+
"<MAPTO>",
|
|
1459
|
+
"Expression",
|
|
1460
|
+
"<RSB>",
|
|
1461
|
+
),
|
|
1462
|
+
sequence(
|
|
1463
|
+
lookahead({ expansion: ["<IDENTIFIER>", "<MAPTO>"] }),
|
|
1464
|
+
"FieldVal",
|
|
1465
|
+
zeroOrMore("<COMMA>", "FieldVal"),
|
|
1466
|
+
"<RSB>",
|
|
1467
|
+
),
|
|
1468
|
+
sequence(
|
|
1469
|
+
lookahead({
|
|
1470
|
+
semantic: (state) =>
|
|
1471
|
+
getToken(state, 2).kind === getKind("MAPTO") &&
|
|
1472
|
+
FIELD_NAME_KINDS.has(getToken(state, 1).kind),
|
|
1473
|
+
}),
|
|
1474
|
+
action((state) => {
|
|
1475
|
+
getToken(state, 1).kind = IDENTIFIER;
|
|
1476
|
+
}),
|
|
1477
|
+
"FieldVal",
|
|
1478
|
+
zeroOrMore("<COMMA>", "FieldVal"),
|
|
1479
|
+
"<RSB>",
|
|
1480
|
+
),
|
|
1481
|
+
sequence(
|
|
1482
|
+
lookahead({ expansion: ["<IDENTIFIER>", "<COLON>"] }),
|
|
1483
|
+
"FieldSet",
|
|
1484
|
+
zeroOrMore("<COMMA>", "FieldSet"),
|
|
1485
|
+
"<RSB>",
|
|
1486
|
+
),
|
|
1487
|
+
sequence(
|
|
1488
|
+
"Expression",
|
|
1489
|
+
choice(
|
|
1490
|
+
sequence(
|
|
1491
|
+
zeroOrMore("<COMMA>", "Expression"),
|
|
1492
|
+
"<RSB>",
|
|
1493
|
+
action((state) => {
|
|
1494
|
+
const frame = state.frames[state.frames.length - 1];
|
|
1495
|
+
frame.node = createNode("FcnAppl", frame.heirs);
|
|
1496
|
+
state.lastOperator = FCN_OPERATOR;
|
|
1497
|
+
}),
|
|
1498
|
+
),
|
|
1499
|
+
sequence("<ARROW>", "Expression", "<RSB>"),
|
|
1500
|
+
sequence("<EXCEPT>", "ExceptSpec", zeroOrMore("<COMMA>", "ExceptSpec"), "<RSB>"),
|
|
1501
|
+
sequence("<ARSB>", "ReducedExpression"),
|
|
1502
|
+
),
|
|
1503
|
+
),
|
|
1504
|
+
),
|
|
1505
|
+
]),
|
|
1506
|
+
FieldVal: production("Field Value", ["Identifier", "<MAPTO>", "Expression"]),
|
|
1507
|
+
FieldSet: production("Field Set", ["Identifier", "<COLON>", "Expression"]),
|
|
1508
|
+
ExceptSpec: production("Except Spec", [
|
|
1509
|
+
"<BANG>",
|
|
1510
|
+
oneOrMore("ExceptComponent", expect("= or ,")),
|
|
1511
|
+
"<EQUALS>",
|
|
1512
|
+
"Expression",
|
|
1513
|
+
]),
|
|
1514
|
+
ExceptComponent: production("Except Component", [
|
|
1515
|
+
choice(
|
|
1516
|
+
sequence("<DOT>", action(setFieldName), "Identifier"),
|
|
1517
|
+
sequence("<LSB>", "Expression", zeroOrMore("<COMMA>", "Expression"), "<RSB>"),
|
|
1518
|
+
),
|
|
1519
|
+
]),
|
|
1520
|
+
TupleOrAction: production("Some << -- >> or >>_ Form", [
|
|
1521
|
+
"<LAB>",
|
|
1522
|
+
optional("Expression", zeroOrMore("<COMMA>", "Expression")),
|
|
1523
|
+
choice("<RAB>", sequence("<ARAB>", "ReducedExpression")),
|
|
1524
|
+
]),
|
|
1525
|
+
// The innermost NoOpExtension sets FairnessHook to its arguments, which end with ")".
|
|
1526
|
+
NoOpExtension: production(undefined, [
|
|
1527
|
+
"Identifier",
|
|
1528
|
+
optional(lookahead({ amount: 2 }), "OpArgs"),
|
|
1529
|
+
choice(
|
|
1530
|
+
sequence("<BANG>", "NoOpExtension"),
|
|
1531
|
+
action((state) => {
|
|
1532
|
+
state.isFairnessHooked = state.token.kind === getKind("RBR");
|
|
1533
|
+
}),
|
|
1534
|
+
),
|
|
1535
|
+
]),
|
|
1536
|
+
ReducedExpression: production("restricted form of expression", [
|
|
1537
|
+
choice("NoOpExtension", "ParenExpr", "BraceCases", "SBracketCases", "TupleOrAction"),
|
|
1538
|
+
]),
|
|
1539
|
+
FairnessExpr: production(
|
|
1540
|
+
"Fairness Expression",
|
|
1541
|
+
[
|
|
1542
|
+
action((state) => {
|
|
1543
|
+
state.isFairnessHooked = false;
|
|
1544
|
+
state.isFairnessParenthesized.push(false);
|
|
1545
|
+
}),
|
|
1546
|
+
choice("<WF>", "<SF>"),
|
|
1547
|
+
"ReducedExpression",
|
|
1548
|
+
optional(
|
|
1549
|
+
lookahead({ amount: 2 }),
|
|
1550
|
+
"<LBR>",
|
|
1551
|
+
"Expression",
|
|
1552
|
+
"<RBR>",
|
|
1553
|
+
action((state) => {
|
|
1554
|
+
state.isFairnessParenthesized[state.isFairnessParenthesized.length - 1] = true;
|
|
1555
|
+
}),
|
|
1556
|
+
),
|
|
1557
|
+
],
|
|
1558
|
+
(frame, state) =>
|
|
1559
|
+
state.isFairnessParenthesized.pop() || state.isFairnessHooked
|
|
1560
|
+
? undefined
|
|
1561
|
+
: `Ill-structured fairness expression at line ${frame.heirs[0].first.beginLine}, column ${frame.heirs[0].first.beginColumn}`,
|
|
1562
|
+
),
|
|
1563
|
+
IfThenElse: production("IF THEN ELSE", [
|
|
1564
|
+
"<IF>",
|
|
1565
|
+
"Expression",
|
|
1566
|
+
"<THEN>",
|
|
1567
|
+
"Expression",
|
|
1568
|
+
"<ELSE>",
|
|
1569
|
+
"Expression",
|
|
1570
|
+
]),
|
|
1571
|
+
Case: production("CASE Expression", [
|
|
1572
|
+
"<CASE>",
|
|
1573
|
+
"CaseArm",
|
|
1574
|
+
zeroOrMore(
|
|
1575
|
+
lookahead({
|
|
1576
|
+
semantic: (state) => isCaseSeparator(state) && getToken(state, 2).kind !== getKind("OTHER"),
|
|
1577
|
+
}),
|
|
1578
|
+
"<CASESEP>",
|
|
1579
|
+
"CaseArm",
|
|
1580
|
+
),
|
|
1581
|
+
optional(lookahead({ semantic: isCaseSeparator }), "<CASESEP>", "OtherArm"),
|
|
1582
|
+
]),
|
|
1583
|
+
CaseArm: production("Case Arm", ["Expression", "<ARROW>", "Expression"]),
|
|
1584
|
+
OtherArm: production("Case Other Arm", ["<OTHER>", "<ARROW>", "Expression"]),
|
|
1585
|
+
LetIn: production("Case Other Arm", ["<LET>", "LetDefinitions", "<LETIN>", "Expression"]),
|
|
1586
|
+
LetDefinitions: production("Let Definitions", [
|
|
1587
|
+
oneOrMore(choice("OperatorOrFunctionDefinition", "Recursive")),
|
|
1588
|
+
]),
|
|
1589
|
+
Junctions: production("AND-OR Junction", [
|
|
1590
|
+
action((state) => {
|
|
1591
|
+
const token = getToken(state, 1);
|
|
1592
|
+
state.junctions.push({ kind: token.kind, column: token.beginColumn });
|
|
1593
|
+
}),
|
|
1594
|
+
choice(
|
|
1595
|
+
sequence(lookahead({ expansion: ["<OR>"] }), ...getJunctionList()),
|
|
1596
|
+
sequence(lookahead({ expansion: ["<AND>"] }), ...getJunctionList()),
|
|
1597
|
+
),
|
|
1598
|
+
action((state) => {
|
|
1599
|
+
state.junctions.pop();
|
|
1600
|
+
}),
|
|
1601
|
+
]),
|
|
1602
|
+
JuncItem: production(
|
|
1603
|
+
"Junction Item",
|
|
1604
|
+
[choice("<OR>", "<AND>"), "Expression"],
|
|
1605
|
+
// TLAplusParser.checkIndentation.
|
|
1606
|
+
(frame, state) => {
|
|
1607
|
+
const item = getMisindentedNode(state, frame.heirs[1]);
|
|
1608
|
+
return (
|
|
1609
|
+
item &&
|
|
1610
|
+
`Item at ${formatLocation(state, item.first, item.last)} is not properly indented inside conjunction or disjunction list item at ${formatLocation(state, frame.heirs[0].first, frame.heirs[1].last)}`
|
|
1611
|
+
);
|
|
1612
|
+
},
|
|
1613
|
+
),
|
|
1614
|
+
UnboundOrBoundChoose: production("(Un)Bounded Choose", [
|
|
1615
|
+
"<CHOOSE>",
|
|
1616
|
+
choice("Identifier", "IdentifierTuple"),
|
|
1617
|
+
"MaybeBound",
|
|
1618
|
+
"<COLON>",
|
|
1619
|
+
"Expression",
|
|
1620
|
+
]),
|
|
1621
|
+
Lambda: production("Lambda", [
|
|
1622
|
+
"<LAMBDA>",
|
|
1623
|
+
expect("Identifier"),
|
|
1624
|
+
"Identifier",
|
|
1625
|
+
expect("`,' or `:'"),
|
|
1626
|
+
zeroOrMore("<COMMA>", expect("Identifier"), "Identifier", expect("`,' or `:'")),
|
|
1627
|
+
"<COLON>",
|
|
1628
|
+
expect("Expression"),
|
|
1629
|
+
"Expression",
|
|
1630
|
+
]),
|
|
1631
|
+
Expression: production(
|
|
1632
|
+
"Expression",
|
|
1633
|
+
[
|
|
1634
|
+
action((state) => {
|
|
1635
|
+
state.operatorStacks.push([]);
|
|
1636
|
+
}),
|
|
1637
|
+
getPrefixOperators(),
|
|
1638
|
+
getOperand(),
|
|
1639
|
+
],
|
|
1640
|
+
// OperatorStack.finalReduce, which pushes an operator that binds looser than any other, whose
|
|
1641
|
+
// node, null in Java, is never printed.
|
|
1642
|
+
(frame, state) => {
|
|
1643
|
+
const stack = getOperatorStack(state);
|
|
1644
|
+
stack.push({ node: frame.heirs[0], operator: VOID_OPERATOR });
|
|
1645
|
+
const message = reduceStack(state);
|
|
1646
|
+
frame.node = stack[0].node;
|
|
1647
|
+
state.operatorStacks.pop();
|
|
1648
|
+
return message;
|
|
1649
|
+
},
|
|
1650
|
+
),
|
|
1651
|
+
ExtendableExpr: production("ExtendableExpr", [
|
|
1652
|
+
choice(
|
|
1653
|
+
sequence(
|
|
1654
|
+
lookahead({
|
|
1655
|
+
expansion: [choice("<AND>", "<OR>")],
|
|
1656
|
+
// OperatorStack.preInEmptyTop.
|
|
1657
|
+
semantic: (state) => {
|
|
1658
|
+
const top = state.operatorStacks.at(-1)?.at(-1);
|
|
1659
|
+
return (
|
|
1660
|
+
top === undefined || (top.operator !== undefined && top.operator.fix !== "postfix")
|
|
1661
|
+
);
|
|
1662
|
+
},
|
|
1663
|
+
}),
|
|
1664
|
+
"Junctions",
|
|
1665
|
+
),
|
|
1666
|
+
"ParenthesesExpression",
|
|
1667
|
+
"PrimitiveExp",
|
|
1668
|
+
),
|
|
1669
|
+
PUSH_OPERAND,
|
|
1670
|
+
zeroOrMore(
|
|
1671
|
+
choice(
|
|
1672
|
+
sequence(
|
|
1673
|
+
lookahead({ expansion: ["PostfixOp"], semantic: isAboveCurrent }),
|
|
1674
|
+
"PostfixOp",
|
|
1675
|
+
PUSH_OPERATOR,
|
|
1676
|
+
),
|
|
1677
|
+
sequence(
|
|
1678
|
+
lookahead({ expansion: ["<DOT>"], semantic: isAboveCurrent }),
|
|
1679
|
+
"<DOT>",
|
|
1680
|
+
action(setFieldName),
|
|
1681
|
+
lookahead({ semantic: isAboveCurrent }),
|
|
1682
|
+
"Identifier",
|
|
1683
|
+
// OperatorStack.reduceRecord, which first applies a postfix operator on top.
|
|
1684
|
+
action((state) => {
|
|
1685
|
+
const stack = getOperatorStack(state);
|
|
1686
|
+
if (stack[stack.length - 1].operator !== undefined) reducePostfix(stack, stack.length);
|
|
1687
|
+
stack[stack.length - 1] = {
|
|
1688
|
+
node: createNode("RecordComponent", [
|
|
1689
|
+
stack[stack.length - 1].node,
|
|
1690
|
+
getHeir(state, 2),
|
|
1691
|
+
getHeir(state, 1),
|
|
1692
|
+
]),
|
|
1693
|
+
operator: undefined,
|
|
1694
|
+
};
|
|
1695
|
+
}),
|
|
1696
|
+
),
|
|
1697
|
+
sequence(
|
|
1698
|
+
lookahead({ expansion: ["<LSB>"], semantic: isAboveCurrent }),
|
|
1699
|
+
expect("function argument"),
|
|
1700
|
+
"SBracketCases",
|
|
1701
|
+
// TLAplusParser.isGenOp, which holds of a function application.
|
|
1702
|
+
pushOnStack((node) => node.kind === "FcnAppl"),
|
|
1703
|
+
),
|
|
1704
|
+
),
|
|
1705
|
+
),
|
|
1706
|
+
optional(
|
|
1707
|
+
choice(
|
|
1708
|
+
sequence(
|
|
1709
|
+
lookahead({ expansion: ["InfixOp"], semantic: isAboveCurrent }),
|
|
1710
|
+
"InfixOp",
|
|
1711
|
+
PUSH_OPERATOR,
|
|
1712
|
+
getPrefixOperators(),
|
|
1713
|
+
getOperand(),
|
|
1714
|
+
),
|
|
1715
|
+
sequence(
|
|
1716
|
+
lookahead({ expansion: ["<COLONCOLON>"], semantic: isAboveCurrent }),
|
|
1717
|
+
"<COLONCOLON>",
|
|
1718
|
+
action((state) => {
|
|
1719
|
+
const stack = getOperatorStack(state);
|
|
1720
|
+
if (!isLabel(stack[stack.length - 1].node)) {
|
|
1721
|
+
return `\`::' at ${formatLocation(state, state.token)} does not follow a label.`;
|
|
1722
|
+
}
|
|
1723
|
+
stack.pop();
|
|
1724
|
+
return undefined;
|
|
1725
|
+
}),
|
|
1726
|
+
"Expression",
|
|
1727
|
+
// TLAplusParser.labelDoesNotChangeParse, given the operator parsed last.
|
|
1728
|
+
action((state) => {
|
|
1729
|
+
const stack = getOperatorStack(state);
|
|
1730
|
+
const labeled = getHeir(state, 1);
|
|
1731
|
+
const operator = stack.at(-1)?.operator;
|
|
1732
|
+
if (
|
|
1733
|
+
(labeled.kind === "InfixExpr" || labeled.kind === "PostfixExpr") &&
|
|
1734
|
+
operator !== undefined &&
|
|
1735
|
+
state.lastOperator !== undefined &&
|
|
1736
|
+
!isLooser(operator, state.lastOperator)
|
|
1737
|
+
) {
|
|
1738
|
+
const label = getHeir(state, 3);
|
|
1739
|
+
return `Removing label at ${formatLocation(state, label.first, label.last)} would change expression parsing.`;
|
|
1740
|
+
}
|
|
1741
|
+
stack.push({
|
|
1742
|
+
node: createNode("Label", [getHeir(state, 3), getHeir(state, 2), labeled]),
|
|
1743
|
+
operator: undefined,
|
|
1744
|
+
});
|
|
1745
|
+
return undefined;
|
|
1746
|
+
}),
|
|
1747
|
+
),
|
|
1748
|
+
),
|
|
1749
|
+
),
|
|
1750
|
+
]),
|
|
1751
|
+
PrimitiveExp: production(
|
|
1752
|
+
"Primitive expression",
|
|
1753
|
+
[
|
|
1754
|
+
choice(
|
|
1755
|
+
sequence(lookahead({ expansion: ["String"], semantic: isAboveCurrent }), "String"),
|
|
1756
|
+
sequence(
|
|
1757
|
+
lookahead({ expansion: ["<NUMBER_LITERAL>"], semantic: isAboveCurrent }),
|
|
1758
|
+
"Number",
|
|
1759
|
+
),
|
|
1760
|
+
sequence(
|
|
1761
|
+
choice(
|
|
1762
|
+
"Identifier",
|
|
1763
|
+
sequence(
|
|
1764
|
+
choice("<ProofStepLexeme>", "<ProofImplicitStepLexeme>"),
|
|
1765
|
+
action((state) => {
|
|
1766
|
+
if (getProofLevel(state) < 0 && state.proofLevels.length <= 1)
|
|
1767
|
+
return `${formatLocation(state, state.token)}: Step number used outside proof.`;
|
|
1768
|
+
return state.token.image[1] === "+"
|
|
1769
|
+
? `${formatLocation(state, state.token)}: <+> step number used in an expression.`
|
|
1770
|
+
: undefined;
|
|
1771
|
+
}),
|
|
1772
|
+
),
|
|
1773
|
+
"InfixOp",
|
|
1774
|
+
"PostfixOp",
|
|
1775
|
+
sequence(lookahead({ expansion: ["<op_76>"] }), "NonExpPrefixOp"),
|
|
1776
|
+
),
|
|
1777
|
+
optional(lookahead({ amount: 2 }), "OpArgs"),
|
|
1778
|
+
zeroOrMore("BangExt"),
|
|
1779
|
+
),
|
|
1780
|
+
),
|
|
1781
|
+
],
|
|
1782
|
+
(frame) => {
|
|
1783
|
+
const head = frame.heirs[0];
|
|
1784
|
+
frame.node =
|
|
1785
|
+
head.kind === "String" || head.kind === "Number"
|
|
1786
|
+
? head
|
|
1787
|
+
: getArguments(frame.heirs) !== undefined
|
|
1788
|
+
? createNode("OpApplication", frame.heirs)
|
|
1789
|
+
: frame.heirs.length === 1
|
|
1790
|
+
? createNode("GeneralId", frame.heirs)
|
|
1791
|
+
: undefined;
|
|
1792
|
+
},
|
|
1793
|
+
),
|
|
1794
|
+
BangExt: production("Bang Extension", [
|
|
1795
|
+
"<BANG>",
|
|
1796
|
+
choice(
|
|
1797
|
+
sequence(
|
|
1798
|
+
choice(
|
|
1799
|
+
sequence(
|
|
1800
|
+
lookahead({
|
|
1801
|
+
expansion: ["Identifier"],
|
|
1802
|
+
semantic: (state) => getToken(state, 1).image !== "@",
|
|
1803
|
+
}),
|
|
1804
|
+
"Identifier",
|
|
1805
|
+
),
|
|
1806
|
+
"NonExpPrefixOp",
|
|
1807
|
+
"InfixOp",
|
|
1808
|
+
"PostfixOp",
|
|
1809
|
+
),
|
|
1810
|
+
optional(lookahead({ amount: 2 }), "OpArgs"),
|
|
1811
|
+
),
|
|
1812
|
+
"OpArgs",
|
|
1813
|
+
"StructOp",
|
|
1814
|
+
),
|
|
1815
|
+
]),
|
|
1816
|
+
StructOp: production(
|
|
1817
|
+
"StructOp",
|
|
1818
|
+
[
|
|
1819
|
+
expect("`<<' , `>>' , `:' , `@' , or number"),
|
|
1820
|
+
choice(
|
|
1821
|
+
"<LAB>",
|
|
1822
|
+
"<RAB>",
|
|
1823
|
+
"<COLON>",
|
|
1824
|
+
"Number",
|
|
1825
|
+
sequence(
|
|
1826
|
+
lookahead({ semantic: (state) => getToken(state, 1).image === "@" }),
|
|
1827
|
+
"<IDENTIFIER>",
|
|
1828
|
+
),
|
|
1829
|
+
),
|
|
1830
|
+
],
|
|
1831
|
+
(frame, state) => {
|
|
1832
|
+
const number = frame.heirs[0];
|
|
1833
|
+
return number.kind === "Number" && number.heirs.length > 1
|
|
1834
|
+
? `Illegal structural term at ${formatLocation(state, number.first, number.last)}`
|
|
1835
|
+
: undefined;
|
|
1836
|
+
},
|
|
1837
|
+
),
|
|
1838
|
+
OpenStart: production(undefined, [
|
|
1839
|
+
choice(
|
|
1840
|
+
"<CASE>",
|
|
1841
|
+
"<CHOOSE>",
|
|
1842
|
+
"<EXISTS>",
|
|
1843
|
+
"<FORALL>",
|
|
1844
|
+
"<IF>",
|
|
1845
|
+
"<LET>",
|
|
1846
|
+
"<T_EXISTS>",
|
|
1847
|
+
"<T_FORALL>",
|
|
1848
|
+
),
|
|
1849
|
+
]),
|
|
1850
|
+
};
|
|
1851
|
+
|
|
1852
|
+
const analyses = new Map<Unit, Analysis>();
|
|
1853
|
+
|
|
1854
|
+
// What JavaCC's ParseEngine.genFirstSet and Semanticize.emptyExpansionExists compute: the tokens
|
|
1855
|
+
// an expansion can start with, whether it can match nothing, and whether its start passes a
|
|
1856
|
+
// semantic lookahead, which makes JavaCC decide by a jj_2 call instead of a switch (jj2LA).
|
|
1857
|
+
const analyze = (unit: Unit): Analysis => {
|
|
1858
|
+
const cached = analyses.get(unit);
|
|
1859
|
+
if (cached) return cached;
|
|
1860
|
+
const analysis = { first: new Set<number>(), isNullable: true, hasSemanticStart: false };
|
|
1861
|
+
const add = (inner: Unit) => {
|
|
1862
|
+
const innerAnalysis = analyze(inner);
|
|
1863
|
+
for (const kind of innerAnalysis.first) analysis.first.add(kind);
|
|
1864
|
+
analysis.hasSemanticStart ||= innerAnalysis.hasSemanticStart;
|
|
1865
|
+
return innerAnalysis.isNullable;
|
|
1866
|
+
};
|
|
1867
|
+
if (unit.type === "tokens") {
|
|
1868
|
+
analysis.first = unit.kinds;
|
|
1869
|
+
analysis.isNullable = false;
|
|
1870
|
+
} else if (unit.type === "call") {
|
|
1871
|
+
Object.assign(analysis, analyze(PRODUCTIONS[unit.name].body));
|
|
1872
|
+
} else if (unit.type === "lookahead") {
|
|
1873
|
+
analysis.hasSemanticStart = unit.semantic !== undefined;
|
|
1874
|
+
} else if (unit.type === "sequence") {
|
|
1875
|
+
const head = unit.units[0];
|
|
1876
|
+
analysis.hasSemanticStart = head?.type === "lookahead" && head.semantic !== undefined;
|
|
1877
|
+
analysis.isNullable = unit.units.every(
|
|
1878
|
+
(inner, index) => (index === 0 && inner.type === "lookahead") || add(inner),
|
|
1879
|
+
);
|
|
1880
|
+
} else if (unit.type === "choice") {
|
|
1881
|
+
analysis.isNullable = unit.alternatives.map(add).some(Boolean);
|
|
1882
|
+
} else if (unit.type !== "action") {
|
|
1883
|
+
analysis.isNullable = add(unit.body) || unit.type !== "oneOrMore";
|
|
1884
|
+
}
|
|
1885
|
+
analyses.set(unit, analysis);
|
|
1886
|
+
return analysis;
|
|
1887
|
+
};
|
|
1888
|
+
|
|
1889
|
+
const evaluateSemantic = (state: ParserState, semantic: Predicate) => {
|
|
1890
|
+
state.isLookingAhead = true;
|
|
1891
|
+
const isPassed = semantic(state);
|
|
1892
|
+
state.isLookingAhead = false;
|
|
1893
|
+
return isPassed;
|
|
1894
|
+
};
|
|
1895
|
+
|
|
1896
|
+
// jj_scan_token, which also records how far past the current token jj_rescan_token scans, the
|
|
1897
|
+
// length of the longest token sequence the ParseException expects.
|
|
1898
|
+
const scanToken = (state: ParserState, kinds: Set<number>): ScanResult => {
|
|
1899
|
+
if (state.scanPosition === state.lastPosition) {
|
|
1900
|
+
state.remaining--;
|
|
1901
|
+
state.lastPosition = getNext(state, state.scanPosition);
|
|
1902
|
+
}
|
|
1903
|
+
state.scanPosition = getNext(state, state.scanPosition);
|
|
1904
|
+
if (state.isRescanning) {
|
|
1905
|
+
let position = 0;
|
|
1906
|
+
let token: SanyToken | undefined = state.token;
|
|
1907
|
+
while (token !== undefined && token !== state.scanPosition) {
|
|
1908
|
+
position++;
|
|
1909
|
+
token = token.next;
|
|
1910
|
+
}
|
|
1911
|
+
if (token !== undefined && position < 100)
|
|
1912
|
+
state.farthestRescan = Math.max(state.farthestRescan, position);
|
|
1913
|
+
}
|
|
1914
|
+
if (!kinds.has(state.scanPosition.kind)) return "failed";
|
|
1915
|
+
return state.remaining === 0 && state.scanPosition === state.lastPosition ? "done" : "matched";
|
|
1916
|
+
};
|
|
1917
|
+
|
|
1918
|
+
// The jj_3 routines, which match an expansion without running its actions. A loop or option skips
|
|
1919
|
+
// its body's lookahead, but a choice checks the semantic part of each alternative's.
|
|
1920
|
+
const scan = (state: ParserState, unit: Unit): ScanResult => {
|
|
1921
|
+
if (unit.type === "tokens") return scanToken(state, unit.kinds);
|
|
1922
|
+
if (unit.type === "call") return scan(state, PRODUCTIONS[unit.name].body);
|
|
1923
|
+
if (unit.type === "action") return "matched";
|
|
1924
|
+
if (unit.type === "lookahead") {
|
|
1925
|
+
return unit.semantic === undefined || evaluateSemantic(state, unit.semantic)
|
|
1926
|
+
? "matched"
|
|
1927
|
+
: "failed";
|
|
1928
|
+
}
|
|
1929
|
+
if (unit.type === "sequence") {
|
|
1930
|
+
for (const [index, inner] of unit.units.entries()) {
|
|
1931
|
+
if (index === 0 && inner.type === "lookahead") continue;
|
|
1932
|
+
const result = scan(state, inner);
|
|
1933
|
+
if (result !== "matched") return result;
|
|
1934
|
+
}
|
|
1935
|
+
return "matched";
|
|
1936
|
+
}
|
|
1937
|
+
const start = state.scanPosition;
|
|
1938
|
+
if (unit.type === "choice") {
|
|
1939
|
+
for (const alternative of unit.alternatives) {
|
|
1940
|
+
const head = alternative.units[0];
|
|
1941
|
+
const isPassed =
|
|
1942
|
+
head?.type !== "lookahead" ||
|
|
1943
|
+
head.semantic === undefined ||
|
|
1944
|
+
evaluateSemantic(state, head.semantic);
|
|
1945
|
+
const result = isPassed ? scan(state, alternative) : "failed";
|
|
1946
|
+
if (result !== "failed") return result;
|
|
1947
|
+
state.scanPosition = start;
|
|
1948
|
+
}
|
|
1949
|
+
return "failed";
|
|
1950
|
+
}
|
|
1951
|
+
const first = scan(state, unit.body);
|
|
1952
|
+
if (first === "failed") {
|
|
1953
|
+
state.scanPosition = start;
|
|
1954
|
+
return unit.type === "oneOrMore" ? "failed" : "matched";
|
|
1955
|
+
}
|
|
1956
|
+
if (first === "done" || unit.type === "optional") return first;
|
|
1957
|
+
for (;;) {
|
|
1958
|
+
const iterationStart = state.scanPosition;
|
|
1959
|
+
const result = scan(state, unit.body);
|
|
1960
|
+
if (result === "done") return result;
|
|
1961
|
+
if (result === "failed") {
|
|
1962
|
+
state.scanPosition = iterationStart;
|
|
1963
|
+
return "matched";
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
};
|
|
1967
|
+
|
|
1968
|
+
// A jj_2 call, which scans expansion with a budget of amount new tokens. It saves the call for
|
|
1969
|
+
// jj_rescan_token as jj_save does, over a call of the same site that can't be rescanned.
|
|
1970
|
+
const scanAhead = (state: ParserState, amount: number, expansion: SequenceUnit) => {
|
|
1971
|
+
state.remaining = amount;
|
|
1972
|
+
state.scanPosition = state.token;
|
|
1973
|
+
state.lastPosition = state.token;
|
|
1974
|
+
const isMatched = scan(state, expansion) !== "failed";
|
|
1975
|
+
const call = {
|
|
1976
|
+
generation: state.generation + amount - state.remaining,
|
|
1977
|
+
first: state.token,
|
|
1978
|
+
amount,
|
|
1979
|
+
};
|
|
1980
|
+
const chain = state.calls.get(expansion) ?? [];
|
|
1981
|
+
const staleIndex = chain.findIndex((saved) => saved.generation <= state.generation);
|
|
1982
|
+
if (staleIndex === -1) chain.push(call);
|
|
1983
|
+
else chain[staleIndex] = call;
|
|
1984
|
+
state.calls.set(expansion, chain);
|
|
1985
|
+
return isMatched;
|
|
1986
|
+
};
|
|
1987
|
+
|
|
1988
|
+
// The index of the alternative that the generated code's lookahead checks choose, or -1. As
|
|
1989
|
+
// ParseEngine.buildLookaheadChecker builds them, a check is the semantic lookahead alone where no
|
|
1990
|
+
// tokens are needed, a jj_ntk switch where one token decides, and a jj_2 call otherwise.
|
|
1991
|
+
const decide = (state: ParserState, alternatives: SequenceUnit[]) =>
|
|
1992
|
+
alternatives.findIndex((alternative) => {
|
|
1993
|
+
const head = alternative.units[0];
|
|
1994
|
+
const explicit = head?.type === "lookahead" ? head : undefined;
|
|
1995
|
+
const amount = explicit?.amount ?? 1;
|
|
1996
|
+
const expansion = explicit?.expansion ?? alternative;
|
|
1997
|
+
const semantic = explicit?.semantic;
|
|
1998
|
+
if (amount === 0 || analyze(expansion).isNullable)
|
|
1999
|
+
return semantic === undefined || semantic(state);
|
|
2000
|
+
if (amount === 1 && semantic === undefined && !analyze(expansion).hasSemanticStart) {
|
|
2001
|
+
if (analyze(expansion).first.has(getToken(state, 1).kind)) return true;
|
|
2002
|
+
state.failedSwitchGeneration = state.generation;
|
|
2003
|
+
return false;
|
|
2004
|
+
}
|
|
2005
|
+
return scanAhead(state, amount, expansion) && (semantic === undefined || semantic(state));
|
|
2006
|
+
});
|
|
2007
|
+
|
|
2008
|
+
// ParseExceptionExtended.getShortMessage for the ParseException that generateParseException
|
|
2009
|
+
// builds, which shows as many tokens as the longest token sequence it expects. A rescan that
|
|
2010
|
+
// finishes its lookahead ends the rescans of its site.
|
|
2011
|
+
const getEncountered = (state: ParserState) => {
|
|
2012
|
+
state.isRescanning = true;
|
|
2013
|
+
for (const [expansion, chain] of state.calls) {
|
|
2014
|
+
for (const call of chain.filter((saved) => saved.generation > state.generation)) {
|
|
2015
|
+
state.remaining = call.amount;
|
|
2016
|
+
state.scanPosition = call.first;
|
|
2017
|
+
state.lastPosition = call.first;
|
|
2018
|
+
if (scan(state, expansion) === "done") break;
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
state.isRescanning = false;
|
|
2022
|
+
const length = Math.max(
|
|
2023
|
+
state.failedSwitchGeneration === state.generation ? 1 : 0,
|
|
2024
|
+
state.farthestRescan,
|
|
2025
|
+
);
|
|
2026
|
+
const images: string[] = [];
|
|
2027
|
+
const next = getNext(state, state.token);
|
|
2028
|
+
let token = next;
|
|
2029
|
+
for (let index = 0; index < length; index++) {
|
|
2030
|
+
if (token.kind === EOF) {
|
|
2031
|
+
images.push("<EOF>");
|
|
2032
|
+
break;
|
|
2033
|
+
}
|
|
2034
|
+
images.push(token.image);
|
|
2035
|
+
// SANY's message follows the tokens the parser has read, without reading another.
|
|
2036
|
+
if (index < length - 1) token = getNext(state, token);
|
|
2037
|
+
}
|
|
2038
|
+
return `Encountered "${images.join(" ")}" at line ${next.beginLine}, column ${next.beginColumn} and token "${escapeLexeme(state.token.image)}" `;
|
|
2039
|
+
};
|
|
2040
|
+
|
|
2041
|
+
// Records TLAplusParser.msgStackToString's report, without its "***Parse Error***" heading.
|
|
2042
|
+
const fail = (state: ParserState, message?: string) => {
|
|
2043
|
+
const expecting = state.expecting === "" ? "" : `Was expecting "${state.expecting}"\n`;
|
|
2044
|
+
const stack = state.messages
|
|
2045
|
+
.slice(-5)
|
|
2046
|
+
.reverse()
|
|
2047
|
+
.map(
|
|
2048
|
+
(entry) =>
|
|
2049
|
+
`${entry.name} starting at line ${entry.token.beginLine}, column ${entry.token.beginColumn}.\n`,
|
|
2050
|
+
)
|
|
2051
|
+
.join("");
|
|
2052
|
+
state.report = `${expecting}${message ?? getEncountered(state)}\n\nResidual stack trace follows:\n${stack}`;
|
|
2053
|
+
return false;
|
|
2054
|
+
};
|
|
2055
|
+
|
|
2056
|
+
const consume = (state: ParserState, kinds: Set<number>) => {
|
|
2057
|
+
const next = getNext(state, state.token);
|
|
2058
|
+
if (!kinds.has(next.kind)) {
|
|
2059
|
+
state.failedSwitchGeneration = state.generation;
|
|
2060
|
+
return fail(state);
|
|
2061
|
+
}
|
|
2062
|
+
state.token = next;
|
|
2063
|
+
state.generation++;
|
|
2064
|
+
state.frames[state.frames.length - 1].heirs.push({
|
|
2065
|
+
kind: "token",
|
|
2066
|
+
heirs: [],
|
|
2067
|
+
first: next,
|
|
2068
|
+
last: next,
|
|
2069
|
+
});
|
|
2070
|
+
return true;
|
|
2071
|
+
};
|
|
2072
|
+
|
|
2073
|
+
// Parses a production, and adds the node it returns to those of the production that called it.
|
|
2074
|
+
const parseProduction = (state: ParserState, name: string) => {
|
|
2075
|
+
const production = PRODUCTIONS[name];
|
|
2076
|
+
if (production.message !== undefined) {
|
|
2077
|
+
state.messages.push({ name: production.message, token: getToken(state, 1) });
|
|
2078
|
+
state.expecting = "";
|
|
2079
|
+
}
|
|
2080
|
+
const frame: Frame = { heirs: [], node: undefined };
|
|
2081
|
+
state.frames.push(frame);
|
|
2082
|
+
if (!parse(state, production.body)) return false;
|
|
2083
|
+
state.frames.pop();
|
|
2084
|
+
if (production.message !== undefined) {
|
|
2085
|
+
state.messages.pop();
|
|
2086
|
+
state.expecting = "";
|
|
2087
|
+
}
|
|
2088
|
+
const message = production.exit?.(frame, state);
|
|
2089
|
+
if (typeof message === "string") return fail(state, message);
|
|
2090
|
+
const node = frame.node ?? (frame.heirs.length === 0 ? undefined : createNode(name, frame.heirs));
|
|
2091
|
+
if (node !== undefined) state.frames.at(-1)?.heirs.push(node);
|
|
2092
|
+
return true;
|
|
2093
|
+
};
|
|
2094
|
+
|
|
2095
|
+
// Runs the parse-mode code JavaCC generates for unit. It returns false once a ParseException's
|
|
2096
|
+
// report is recorded.
|
|
2097
|
+
const parse = (state: ParserState, unit: Unit): boolean => {
|
|
2098
|
+
if (unit.type === "tokens") return consume(state, unit.kinds);
|
|
2099
|
+
if (unit.type === "call") return parseProduction(state, unit.name);
|
|
2100
|
+
if (unit.type === "action") {
|
|
2101
|
+
const message = unit.run(state);
|
|
2102
|
+
return message === undefined || fail(state, message);
|
|
2103
|
+
}
|
|
2104
|
+
if (unit.type === "lookahead")
|
|
2105
|
+
return unit.semantic === undefined || unit.semantic(state) || fail(state);
|
|
2106
|
+
if (unit.type === "sequence") {
|
|
2107
|
+
return unit.units.every(
|
|
2108
|
+
(inner, index) => (index === 0 && inner.type === "lookahead") || parse(state, inner),
|
|
2109
|
+
);
|
|
2110
|
+
}
|
|
2111
|
+
if (unit.type === "choice") {
|
|
2112
|
+
const index = decide(state, unit.alternatives);
|
|
2113
|
+
return index === -1 ? fail(state) : parse(state, unit.alternatives[index]);
|
|
2114
|
+
}
|
|
2115
|
+
if (unit.type === "oneOrMore" && !parse(state, unit.body)) return false;
|
|
2116
|
+
if (unit.type === "optional") return decide(state, [unit.body]) === -1 || parse(state, unit.body);
|
|
2117
|
+
while (decide(state, [unit.body]) === 0) {
|
|
2118
|
+
if (!parse(state, unit.body)) return false;
|
|
2119
|
+
}
|
|
2120
|
+
return true;
|
|
2121
|
+
};
|
|
2122
|
+
|
|
2123
|
+
// What SANY reports for a module its lexer or parser rejects, or undefined if it accepts the module.
|
|
2124
|
+
export const getSanyParseError = (source: string) => {
|
|
2125
|
+
const readToken = createSanyLexer(source);
|
|
2126
|
+
const start: SanyToken = { kind: EOF, image: "", beginLine: 0, beginColumn: 0, next: undefined };
|
|
2127
|
+
const state: ParserState = {
|
|
2128
|
+
readToken: () => {
|
|
2129
|
+
const token = readToken();
|
|
2130
|
+
if (typeof token !== "string") return token;
|
|
2131
|
+
state.lexicalError ??= token;
|
|
2132
|
+
return { ...start, next: undefined };
|
|
2133
|
+
},
|
|
2134
|
+
lexicalError: undefined,
|
|
2135
|
+
token: start,
|
|
2136
|
+
generation: 0,
|
|
2137
|
+
failedSwitchGeneration: -1,
|
|
2138
|
+
scanPosition: start,
|
|
2139
|
+
lastPosition: start,
|
|
2140
|
+
remaining: 0,
|
|
2141
|
+
isLookingAhead: false,
|
|
2142
|
+
isRescanning: false,
|
|
2143
|
+
farthestRescan: 0,
|
|
2144
|
+
calls: new Map(),
|
|
2145
|
+
expecting: "nothing",
|
|
2146
|
+
messages: [],
|
|
2147
|
+
moduleName: undefined,
|
|
2148
|
+
junctions: [],
|
|
2149
|
+
frames: [],
|
|
2150
|
+
operatorStacks: [],
|
|
2151
|
+
lastOperator: undefined,
|
|
2152
|
+
proofLevels: [],
|
|
2153
|
+
isTerminalProof: false,
|
|
2154
|
+
isFairnessHooked: false,
|
|
2155
|
+
isFairnessParenthesized: [],
|
|
2156
|
+
report: undefined,
|
|
2157
|
+
};
|
|
2158
|
+
const isParsed = parse(state, { type: "call", name: "CompilationUnit" });
|
|
2159
|
+
if (state.lexicalError !== undefined) return state.lexicalError;
|
|
2160
|
+
return isParsed || state.report === undefined ? undefined : `***Parse Error***\n${state.report}`;
|
|
2161
|
+
};
|