tla-ts 0.0.3-dev.4c8c06c → 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/README.md +16 -2
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +157 -8
- package/dist/index.d.ts +157 -8
- 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 -19
- package/dist/run-cli.js +1351 -19
- package/dist/transfer.cjs +86 -16
- package/dist/transfer.js +86 -16
- package/package.json +5 -1
- package/src/ast.ts +142 -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 +1539 -235
- package/src/cli-options.ts +11 -5
- package/src/cli.ts +8 -1
- package/src/codegen.ts +77 -37
- package/src/config.ts +252 -179
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +518 -137
- package/src/errors.ts +224 -3
- package/src/evaluate.ts +796 -246
- package/src/exit-codes.ts +22 -43
- package/src/index.ts +1 -0
- package/src/instances.ts +19 -16
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +191 -45
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +828 -0
- package/src/liveness.ts +218 -572
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +13 -4
- package/src/parallel.ts +73 -6
- package/src/parser.ts +447 -128
- 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 +1380 -420
- package/src/run-cli.ts +1200 -143
- package/src/runtime.ts +19 -1
- 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 +42 -11
- package/src/standard-modules.ts +200 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +180 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +219 -107
- package/src/symmetry.ts +180 -52
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +42 -29
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +52 -43
- package/src/tlc-order.ts +73 -9
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +708 -180
package/src/parser.ts
CHANGED
|
@@ -22,15 +22,30 @@ import type {
|
|
|
22
22
|
StepBody,
|
|
23
23
|
UseOrHide,
|
|
24
24
|
} from "./ast";
|
|
25
|
-
import { TlaSyntaxError } from "./errors";
|
|
26
|
-
import { getNumeralValue, tokenize, type Token } from "./lexer";
|
|
25
|
+
import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
|
|
26
|
+
import { getNumeralValue, readAtAsIdentifier, tokenize, type Token } from "./lexer";
|
|
27
|
+
import { getSanyParseError } from "./sany-parse-error";
|
|
27
28
|
|
|
28
|
-
interface
|
|
29
|
+
interface Precedence {
|
|
29
30
|
low: number;
|
|
30
31
|
high: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface InfixOperator extends Precedence {
|
|
31
35
|
isAssociative: boolean;
|
|
32
36
|
}
|
|
33
37
|
|
|
38
|
+
// TLAplusParser.lastOp, the operator parsed last, and the operator an operand being parsed follows
|
|
39
|
+
// in its expression.
|
|
40
|
+
interface OperatorContext {
|
|
41
|
+
last?: Precedence;
|
|
42
|
+
preceding?: Precedence;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// SANY's precedences of a postfix operator and of the "[" of a function application.
|
|
46
|
+
const POSTFIX_PRECEDENCE: Precedence = { low: 15, high: 15 };
|
|
47
|
+
const FUNCTION_PRECEDENCE: Precedence = { low: 16, high: 16 };
|
|
48
|
+
|
|
34
49
|
const createInfix = (low: number, high: number, isAssociative: boolean) => ({
|
|
35
50
|
low,
|
|
36
51
|
high,
|
|
@@ -135,10 +150,25 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
|
|
|
135
150
|
["^^", createInfix(14, 14, false)],
|
|
136
151
|
]);
|
|
137
152
|
|
|
153
|
+
const NEGATION_PRECEDENCE = 12;
|
|
154
|
+
|
|
138
155
|
const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
|
|
139
156
|
|
|
140
157
|
const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
|
|
141
158
|
|
|
159
|
+
// The prefix operators SANY reads in a definition's left-hand side, built-in ones included.
|
|
160
|
+
const DEFINABLE_PREFIX_OPERATORS = new Set([
|
|
161
|
+
"-.",
|
|
162
|
+
"~",
|
|
163
|
+
"[]",
|
|
164
|
+
"<>",
|
|
165
|
+
"ENABLED",
|
|
166
|
+
"UNCHANGED",
|
|
167
|
+
"SUBSET",
|
|
168
|
+
"UNION",
|
|
169
|
+
"DOMAIN",
|
|
170
|
+
]);
|
|
171
|
+
|
|
142
172
|
const EXPRESSION_PREFIXES = new Set([
|
|
143
173
|
"-",
|
|
144
174
|
"~",
|
|
@@ -167,6 +197,16 @@ const OPERATOR_ARGUMENTS = new Set([
|
|
|
167
197
|
...POSTFIX_OPERATORS,
|
|
168
198
|
]);
|
|
169
199
|
|
|
200
|
+
// A SANY InfixExpr or PostfixExpr.
|
|
201
|
+
const isOperatorExpression = (expression: Expression) =>
|
|
202
|
+
expression.kind === "Prime" ||
|
|
203
|
+
expression.kind === "Product" ||
|
|
204
|
+
((expression.kind === "And" || expression.kind === "Or") && !expression.isBulleted) ||
|
|
205
|
+
(expression.kind === "Apply" &&
|
|
206
|
+
(expression.args.length === 2
|
|
207
|
+
? INFIX_OPERATORS.has(expression.operator)
|
|
208
|
+
: POSTFIX_OPERATORS.has(expression.operator)));
|
|
209
|
+
|
|
170
210
|
const isOperatorName = (text: string) =>
|
|
171
211
|
INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
|
|
172
212
|
|
|
@@ -260,6 +300,7 @@ const FACT_KEYWORDS = new Set([
|
|
|
260
300
|
"IF",
|
|
261
301
|
"LAMBDA",
|
|
262
302
|
"LET",
|
|
303
|
+
"MODULE",
|
|
263
304
|
"SF_",
|
|
264
305
|
"SUBSET",
|
|
265
306
|
"UNCHANGED",
|
|
@@ -289,30 +330,36 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
|
|
|
289
330
|
|
|
290
331
|
const NO_OFFSIDE = -1;
|
|
291
332
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
// The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
|
|
295
|
-
// modules that follow the root module.
|
|
333
|
+
// MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
|
|
334
|
+
// root module's file, from its ---- MODULE line to the next ==== line.
|
|
296
335
|
export const extractModule = (source: string, name: string) => {
|
|
336
|
+
const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
|
|
297
337
|
const lines = source.split(/\r?\n/);
|
|
298
|
-
const start = lines.findIndex((line) =>
|
|
338
|
+
const start = lines.findIndex((line) => header.test(line));
|
|
299
339
|
if (start === -1) return undefined;
|
|
300
340
|
const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
|
|
301
|
-
return lines.slice(start, end === -1 ? undefined : end + 1).join("\n")
|
|
341
|
+
return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
|
|
302
342
|
};
|
|
303
343
|
|
|
304
|
-
//
|
|
305
|
-
// SANY does not.
|
|
306
|
-
|
|
307
|
-
|
|
344
|
+
// The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some modules
|
|
345
|
+
// that SANY does not. The sany mode leaves the checks that SANY makes after parsing to the SANY
|
|
346
|
+
// generator.
|
|
347
|
+
type ParseMode = "tlc" | "tlapm" | "sany";
|
|
348
|
+
|
|
349
|
+
const parse = (source: string, mode: ParseMode): Module => {
|
|
350
|
+
const isTlapm = mode === "tlapm";
|
|
351
|
+
const tokens = isTlapm ? tokenize(source) : tokenize(source).map(readAtAsIdentifier);
|
|
308
352
|
const cursor = { index: 0, offsideColumns: [] as number[] };
|
|
353
|
+
const operators: OperatorContext = {};
|
|
309
354
|
// A RECURSIVE declaration covers the definitions of its own LET or module.
|
|
310
355
|
const recursiveScopes: Set<string>[] = [];
|
|
311
356
|
const factNames = new Set<string>();
|
|
312
357
|
// The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
|
|
313
358
|
const proofScopes: Set<string>[] = [];
|
|
314
|
-
// A fact may cite an operator of a parameterized instance without the
|
|
359
|
+
// A fact or an operator argument may cite an operator of a parameterized instance without the
|
|
360
|
+
// instance's arguments, as with Op <- I!Op in a substitution.
|
|
315
361
|
const factStart = { index: -1 };
|
|
362
|
+
// SANY reports a location in a nested module as one in the module of its file.
|
|
316
363
|
const openModules: Module[] = [];
|
|
317
364
|
|
|
318
365
|
const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
|
|
@@ -327,15 +374,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
327
374
|
const tokenEnds: SourceLocation[] = [];
|
|
328
375
|
const tokenRanges: SourceRange[] = [];
|
|
329
376
|
|
|
330
|
-
const getRange = (startIndex: number) => {
|
|
331
|
-
const endIndex = cursor.index - 1;
|
|
377
|
+
const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
|
|
332
378
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
333
379
|
if (cached) return cached;
|
|
334
380
|
const last = tokens[endIndex];
|
|
335
381
|
tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
|
|
336
382
|
tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
|
|
337
383
|
const range = {
|
|
338
|
-
module: openModules[
|
|
384
|
+
module: openModules[0].name,
|
|
339
385
|
start: tokenStarts[startIndex],
|
|
340
386
|
end: tokenEnds[endIndex],
|
|
341
387
|
};
|
|
@@ -433,8 +479,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
433
479
|
const createUnexpected = (message: string, token: Token = peekRaw()) =>
|
|
434
480
|
createError(message, token, token);
|
|
435
481
|
|
|
482
|
+
// TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
|
|
436
483
|
const isFieldName = (token: Token) =>
|
|
437
|
-
(token.kind === "Identifier" ||
|
|
484
|
+
(token.kind === "Identifier" ||
|
|
485
|
+
token.kind === "Keyword" ||
|
|
486
|
+
(token.kind === "Operator" && token.text === "_")) &&
|
|
487
|
+
!isTlapmKeyword(token);
|
|
438
488
|
|
|
439
489
|
const isAt = (text: string, offset = 0) => {
|
|
440
490
|
const token = peek(offset);
|
|
@@ -470,7 +520,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
470
520
|
return token.text;
|
|
471
521
|
};
|
|
472
522
|
|
|
473
|
-
const
|
|
523
|
+
const expectName = () => {
|
|
524
|
+
const start = cursor.index;
|
|
525
|
+
return { name: expectIdentifier(), range: getRange(start) };
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const getNames = (names: { name: string; range: SourceRange }[]) => ({
|
|
529
|
+
names: names.map((item) => item.name),
|
|
530
|
+
ranges: names.map((item) => item.range),
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
// TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
|
|
534
|
+
const expectSubstitutionTarget = () => {
|
|
535
|
+
const token = peek();
|
|
536
|
+
const isOperator =
|
|
537
|
+
token.kind !== "String" &&
|
|
538
|
+
(DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
|
|
539
|
+
INFIX_OPERATORS.has(token.text) ||
|
|
540
|
+
POSTFIX_OPERATORS.has(token.text) ||
|
|
541
|
+
token.text === "'");
|
|
542
|
+
if (!isOperator) return expectIdentifier();
|
|
543
|
+
cursor.index++;
|
|
544
|
+
return token.text;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// SANY takes a keyword for the name of a module, as it does for a field.
|
|
548
|
+
const keywordModuleName = { isUsed: false };
|
|
549
|
+
const expectModuleName = () => {
|
|
550
|
+
if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
|
|
551
|
+
keywordModuleName.isUsed = true;
|
|
552
|
+
return advance().text;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// tlapm reads a bracketed expression apart from the junction list around it, whose alignment
|
|
556
|
+
// SANY keeps applying.
|
|
557
|
+
const withBracketLayout = <Result>(parse: () => Result) => {
|
|
558
|
+
if (!isTlapm) return parse();
|
|
474
559
|
cursor.offsideColumns.push(NO_OFFSIDE);
|
|
475
560
|
const result = parse();
|
|
476
561
|
cursor.offsideColumns.pop();
|
|
@@ -512,10 +597,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
512
597
|
};
|
|
513
598
|
|
|
514
599
|
const parsePattern = (): BoundPattern => {
|
|
515
|
-
if (!accept("<<"))
|
|
516
|
-
|
|
600
|
+
if (!accept("<<")) {
|
|
601
|
+
const named = expectName();
|
|
602
|
+
return { kind: "Name", name: named.name, ranges: [named.range] };
|
|
603
|
+
}
|
|
604
|
+
const names = getNames(parseSeparated(expectName));
|
|
517
605
|
expect(">>");
|
|
518
|
-
return { kind: "Tuple", names };
|
|
606
|
+
return { kind: "Tuple", names: names.names, ranges: names.ranges };
|
|
519
607
|
};
|
|
520
608
|
|
|
521
609
|
const parseBounds = () =>
|
|
@@ -558,8 +646,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
558
646
|
);
|
|
559
647
|
};
|
|
560
648
|
|
|
649
|
+
// SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
|
|
650
|
+
// the start of an expression, such as -.(1), otherwise.
|
|
561
651
|
const isStandalone = (operator: Token) => {
|
|
562
|
-
if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
|
|
563
652
|
const token = peek(1);
|
|
564
653
|
return (
|
|
565
654
|
token.kind === "End" ||
|
|
@@ -598,7 +687,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
598
687
|
if (isOperatorAlone) {
|
|
599
688
|
cursor.index++;
|
|
600
689
|
return withRange(
|
|
601
|
-
{
|
|
690
|
+
{
|
|
691
|
+
kind: "OperatorReference",
|
|
692
|
+
operator: token.text,
|
|
693
|
+
spelling: token.spelling,
|
|
694
|
+
location: getLocation(token),
|
|
695
|
+
},
|
|
602
696
|
cursor.index - 1,
|
|
603
697
|
);
|
|
604
698
|
}
|
|
@@ -621,19 +715,38 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
621
715
|
if (isAt("LAMBDA")) {
|
|
622
716
|
const start = cursor.index;
|
|
623
717
|
advance();
|
|
624
|
-
const params = parseSeparated(
|
|
718
|
+
const params = getNames(parseSeparated(expectName));
|
|
625
719
|
expect(":");
|
|
626
720
|
const body = parseExpression();
|
|
627
|
-
return withRange(
|
|
721
|
+
return withRange(
|
|
722
|
+
{
|
|
723
|
+
kind: "Lambda",
|
|
724
|
+
params: params.names,
|
|
725
|
+
paramRanges: params.ranges,
|
|
726
|
+
body,
|
|
727
|
+
location: getLocation(token),
|
|
728
|
+
},
|
|
729
|
+
start,
|
|
730
|
+
);
|
|
628
731
|
}
|
|
629
|
-
|
|
732
|
+
const argument = parseExpression();
|
|
733
|
+
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
734
|
+
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
735
|
+
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
736
|
+
return argument;
|
|
630
737
|
};
|
|
631
738
|
|
|
739
|
+
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
740
|
+
|
|
741
|
+
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
742
|
+
|
|
632
743
|
const parseArguments = () =>
|
|
633
|
-
|
|
744
|
+
withBracketLayout(() => {
|
|
745
|
+
const start = cursor.index;
|
|
634
746
|
expect("(");
|
|
635
747
|
const args = parseSeparated(parseArgument);
|
|
636
748
|
expect(")");
|
|
749
|
+
lastArguments.range = getRange(start);
|
|
637
750
|
return args;
|
|
638
751
|
});
|
|
639
752
|
|
|
@@ -655,15 +768,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
655
768
|
};
|
|
656
769
|
|
|
657
770
|
const parseBracket = (start: Token): Expression =>
|
|
658
|
-
|
|
771
|
+
withBracketLayout(() => {
|
|
659
772
|
const location = getLocation(start);
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
773
|
+
// SANY takes a keyword for the first field of a record, as it does for a selected field.
|
|
774
|
+
const isIdentifier = peek().kind === "Identifier";
|
|
775
|
+
const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
|
|
776
|
+
if (isRecord || (isIdentifier && isAt(":", 1))) {
|
|
777
|
+
const kind = isRecord ? "Record" : "RecordSet";
|
|
778
|
+
const parseField = (readName: () => string) => {
|
|
779
|
+
const fieldStart = cursor.index;
|
|
780
|
+
const fieldLocation = getLocation(peek());
|
|
781
|
+
const name = readName();
|
|
782
|
+
fieldLocation.range = getRange(fieldStart);
|
|
783
|
+
expect(isRecord ? "|->" : ":");
|
|
784
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
785
|
+
};
|
|
786
|
+
const fields = [
|
|
787
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
788
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
789
|
+
];
|
|
667
790
|
expect("]");
|
|
668
791
|
return { kind, fields, location };
|
|
669
792
|
}
|
|
@@ -701,17 +824,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
701
824
|
expect("]");
|
|
702
825
|
return { kind: "Except", function: expression, updates, location };
|
|
703
826
|
}
|
|
827
|
+
if (!isTlapm && !isAt("]_")) {
|
|
828
|
+
const args = [expression];
|
|
829
|
+
while (accept(",")) args.push(parseExpression());
|
|
830
|
+
expect("]");
|
|
831
|
+
return { kind: "Bracket", args, location };
|
|
832
|
+
}
|
|
704
833
|
expect("]_");
|
|
705
|
-
return {
|
|
706
|
-
kind: "BoxAction",
|
|
707
|
-
action: expression,
|
|
708
|
-
subscript: parseSubscript(),
|
|
709
|
-
location,
|
|
710
|
-
};
|
|
834
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
711
835
|
});
|
|
712
836
|
|
|
837
|
+
// SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
|
|
838
|
+
const parseSetMembership = (): Expression => {
|
|
839
|
+
const start = cursor.index;
|
|
840
|
+
const element = withRange(parsePrefix(), start);
|
|
841
|
+
const location = getLocation(advance());
|
|
842
|
+
return withRange(
|
|
843
|
+
{ kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
|
|
844
|
+
start,
|
|
845
|
+
);
|
|
846
|
+
};
|
|
847
|
+
|
|
713
848
|
const parseBrace = (start: Token): Expression =>
|
|
714
|
-
|
|
849
|
+
withBracketLayout(() => {
|
|
715
850
|
const location = getLocation(start);
|
|
716
851
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
717
852
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -726,7 +861,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
726
861
|
}
|
|
727
862
|
cursor.index = checkpoint;
|
|
728
863
|
}
|
|
729
|
-
const first =
|
|
864
|
+
const first =
|
|
865
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
730
866
|
if (accept(":")) {
|
|
731
867
|
const bounds = parseBounds();
|
|
732
868
|
expect("}");
|
|
@@ -740,7 +876,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
740
876
|
|
|
741
877
|
const parseTuple = (start: Token): Expression => {
|
|
742
878
|
const location = getLocation(start);
|
|
743
|
-
const elements =
|
|
879
|
+
const elements = withBracketLayout(() =>
|
|
744
880
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
745
881
|
);
|
|
746
882
|
if (accept(">>_")) {
|
|
@@ -761,12 +897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
761
897
|
expect(":");
|
|
762
898
|
return { kind, bounds, body: parseExpression(), location };
|
|
763
899
|
}
|
|
764
|
-
const names = parseSeparated(
|
|
900
|
+
const names = getNames(parseSeparated(expectName));
|
|
765
901
|
expect(":");
|
|
766
902
|
return {
|
|
767
903
|
kind: "UnboundedQuantifier",
|
|
768
904
|
quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
|
|
769
|
-
names,
|
|
905
|
+
names: names.names,
|
|
906
|
+
nameRanges: names.ranges,
|
|
770
907
|
body: parseExpression(),
|
|
771
908
|
isTemporal,
|
|
772
909
|
location,
|
|
@@ -820,8 +957,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
820
957
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
821
958
|
};
|
|
822
959
|
|
|
823
|
-
const parsePrimary = (): Expression => {
|
|
824
|
-
const
|
|
960
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
961
|
+
const start = cursor.index;
|
|
962
|
+
const isFactStart = start === factStart.index;
|
|
825
963
|
const token = advance();
|
|
826
964
|
const location = getLocation(token);
|
|
827
965
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -845,53 +983,116 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
845
983
|
if (token.kind === "Identifier" || isStep) {
|
|
846
984
|
const name = isStep ? (getStepName(token) ?? token.text) : token.text;
|
|
847
985
|
if (isLabelAhead()) {
|
|
848
|
-
const params = accept("(") ? parseSeparated(
|
|
849
|
-
if (params.length > 0) expect(")");
|
|
986
|
+
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
987
|
+
if (params.names.length > 0) expect(")");
|
|
850
988
|
expect("::");
|
|
851
|
-
|
|
989
|
+
const body = parseExpression();
|
|
990
|
+
// TLAplusParser.labelDoesNotChangeParse.
|
|
991
|
+
if (
|
|
992
|
+
!isTlapm &&
|
|
993
|
+
preceding !== undefined &&
|
|
994
|
+
operators.last !== undefined &&
|
|
995
|
+
isOperatorExpression(body) &&
|
|
996
|
+
preceding.high >= operators.last.low
|
|
997
|
+
) {
|
|
998
|
+
throw createError("Removing the label would change expression parsing", token);
|
|
999
|
+
}
|
|
1000
|
+
return {
|
|
1001
|
+
kind: "Label",
|
|
1002
|
+
name,
|
|
1003
|
+
params: params.names,
|
|
1004
|
+
paramRanges: params.ranges,
|
|
1005
|
+
body,
|
|
1006
|
+
location,
|
|
1007
|
+
};
|
|
852
1008
|
}
|
|
853
1009
|
const hasArguments = isAt("(");
|
|
1010
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1011
|
+
return { kind: "ExceptAt", location };
|
|
854
1012
|
const args = hasArguments ? parseArguments() : [];
|
|
1013
|
+
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
855
1014
|
if (!isSelectorAhead()) {
|
|
856
1015
|
return hasArguments
|
|
857
|
-
? { kind: "Apply", operator: name, args, location }
|
|
1016
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
858
1017
|
: { kind: "Identifier", name, location };
|
|
859
1018
|
}
|
|
860
1019
|
const instance = openModules
|
|
861
1020
|
.flatMap((module) => module.instances)
|
|
862
1021
|
.find((candidate) => candidate.name === name);
|
|
863
|
-
|
|
1022
|
+
const isMissingArguments =
|
|
1023
|
+
instance !== undefined &&
|
|
1024
|
+
mode === "tlc" &&
|
|
1025
|
+
!isFactStart &&
|
|
1026
|
+
instance.params.length !== args.length;
|
|
1027
|
+
if (isMissingArguments && args.length > 0) {
|
|
864
1028
|
throw createError(
|
|
865
1029
|
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
866
1030
|
token,
|
|
867
1031
|
);
|
|
868
1032
|
}
|
|
869
1033
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1034
|
+
const selectorRanges = [getRange(start, start)];
|
|
870
1035
|
while (isSelectorAhead()) {
|
|
871
1036
|
advance();
|
|
1037
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
1038
|
+
lastArguments.range = undefined;
|
|
872
1039
|
selectors.push(parseSelector());
|
|
1040
|
+
argumentRanges.push(lastArguments.range);
|
|
873
1041
|
}
|
|
874
1042
|
const names = selectors.flatMap((selector) =>
|
|
875
1043
|
selector.kind === "Name" ? [selector.name] : [],
|
|
876
1044
|
);
|
|
877
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
878
1045
|
const operator = names.join("!");
|
|
879
1046
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
880
1047
|
"args" in selector ? selector.args : [],
|
|
881
1048
|
);
|
|
882
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
883
1049
|
const argumentCounts = selectors.map((selector) =>
|
|
884
1050
|
"args" in selector ? selector.args.length : 0,
|
|
885
1051
|
);
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
1052
|
+
const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
|
|
1053
|
+
const reference: Expression =
|
|
1054
|
+
names.length < selectors.length
|
|
1055
|
+
? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
|
|
1056
|
+
: qualifiedArgs.length === 0
|
|
1057
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
1058
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
1059
|
+
? {
|
|
1060
|
+
kind: "Apply",
|
|
1061
|
+
operator,
|
|
1062
|
+
args: qualifiedArgs,
|
|
1063
|
+
argumentCounts,
|
|
1064
|
+
argumentsRange,
|
|
1065
|
+
selectorRanges,
|
|
1066
|
+
argumentRanges,
|
|
1067
|
+
location,
|
|
1068
|
+
}
|
|
1069
|
+
: {
|
|
1070
|
+
kind: "Apply",
|
|
1071
|
+
operator,
|
|
1072
|
+
args: qualifiedArgs,
|
|
1073
|
+
argumentsRange,
|
|
1074
|
+
selectorRanges,
|
|
1075
|
+
argumentRanges,
|
|
1076
|
+
location,
|
|
1077
|
+
};
|
|
1078
|
+
if (isMissingArguments) {
|
|
1079
|
+
missingArguments.set(reference, {
|
|
1080
|
+
location: formatRange(getRange(start)),
|
|
1081
|
+
message: `The operator ${name} requires ${instance.params.length} arguments.`,
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
return reference;
|
|
889
1085
|
}
|
|
890
|
-
|
|
1086
|
+
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1087
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
891
1088
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
1089
|
+
}
|
|
1090
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1091
|
+
return { kind: "Identifier", name: token.text, location };
|
|
1092
|
+
}
|
|
892
1093
|
switch (token.text) {
|
|
893
1094
|
case "(": {
|
|
894
|
-
const expression =
|
|
1095
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
895
1096
|
expect(")");
|
|
896
1097
|
return expression;
|
|
897
1098
|
}
|
|
@@ -947,7 +1148,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
947
1148
|
case "WF_":
|
|
948
1149
|
case "SF_": {
|
|
949
1150
|
const subscript = parseSubscript();
|
|
950
|
-
const action =
|
|
1151
|
+
const action = withBracketLayout(() => {
|
|
951
1152
|
expect("(");
|
|
952
1153
|
const expression = parseExpression();
|
|
953
1154
|
expect(")");
|
|
@@ -964,7 +1165,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
964
1165
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
965
1166
|
};
|
|
966
1167
|
|
|
1168
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1169
|
+
// prefix operator.
|
|
1170
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1171
|
+
operators.last = operators.preceding = { low, high };
|
|
1172
|
+
const operand = parseExpression(high + 1);
|
|
1173
|
+
const next = peek();
|
|
1174
|
+
const nextHigh =
|
|
1175
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1176
|
+
? POSTFIX_PRECEDENCE.high
|
|
1177
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1178
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1179
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1180
|
+
}
|
|
1181
|
+
return operand;
|
|
1182
|
+
};
|
|
1183
|
+
|
|
967
1184
|
const parsePrefix = (): Expression => {
|
|
1185
|
+
const preceding = operators.preceding;
|
|
1186
|
+
operators.preceding = undefined;
|
|
968
1187
|
const token = peek();
|
|
969
1188
|
const location = getLocation(token);
|
|
970
1189
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -974,30 +1193,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
974
1193
|
switch (token.text) {
|
|
975
1194
|
case "~":
|
|
976
1195
|
advance();
|
|
977
|
-
return {
|
|
1196
|
+
return {
|
|
1197
|
+
kind: "Apply",
|
|
1198
|
+
operator: "~",
|
|
1199
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1200
|
+
location,
|
|
1201
|
+
};
|
|
978
1202
|
case "-":
|
|
979
1203
|
advance();
|
|
980
|
-
return {
|
|
1204
|
+
return {
|
|
1205
|
+
kind: "Apply",
|
|
1206
|
+
operator: "-.",
|
|
1207
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
1208
|
+
location,
|
|
1209
|
+
};
|
|
981
1210
|
case "[]":
|
|
982
1211
|
advance();
|
|
983
|
-
return { kind: "Always", expression:
|
|
1212
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
984
1213
|
case "<>":
|
|
985
1214
|
advance();
|
|
986
|
-
return { kind: "Eventually", expression:
|
|
1215
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
987
1216
|
case "ENABLED":
|
|
988
1217
|
advance();
|
|
989
|
-
return { kind: "Enabled", expression:
|
|
1218
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
990
1219
|
case "UNCHANGED":
|
|
991
1220
|
advance();
|
|
992
|
-
return { kind: "Unchanged", expression:
|
|
1221
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
993
1222
|
case "SUBSET":
|
|
994
1223
|
case "UNION":
|
|
995
1224
|
case "DOMAIN":
|
|
996
1225
|
advance();
|
|
997
|
-
return {
|
|
1226
|
+
return {
|
|
1227
|
+
kind: "Apply",
|
|
1228
|
+
operator: token.text,
|
|
1229
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1230
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1231
|
+
location,
|
|
1232
|
+
};
|
|
998
1233
|
}
|
|
999
1234
|
}
|
|
1000
|
-
return parsePrimary();
|
|
1235
|
+
return parsePrimary(preceding);
|
|
1001
1236
|
};
|
|
1002
1237
|
|
|
1003
1238
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1013,11 +1248,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1013
1248
|
const location = getLocation(token);
|
|
1014
1249
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1015
1250
|
advance();
|
|
1251
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1016
1252
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1017
1253
|
continue;
|
|
1018
1254
|
}
|
|
1019
1255
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1020
1256
|
advance();
|
|
1257
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1021
1258
|
state.left = withRange(
|
|
1022
1259
|
{ kind: "Apply", operator: token.text, args: [state.left], location },
|
|
1023
1260
|
start,
|
|
@@ -1026,8 +1263,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1026
1263
|
}
|
|
1027
1264
|
if (token.text === "[") {
|
|
1028
1265
|
advance();
|
|
1029
|
-
const args =
|
|
1030
|
-
|
|
1266
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1267
|
+
withBracketLayout(() => expect("]"));
|
|
1268
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1031
1269
|
state.left = withRange(
|
|
1032
1270
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1033
1271
|
start,
|
|
@@ -1054,21 +1292,37 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1054
1292
|
}
|
|
1055
1293
|
}
|
|
1056
1294
|
advance();
|
|
1295
|
+
// SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
|
|
1296
|
+
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1297
|
+
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1298
|
+
}
|
|
1299
|
+
operators.last = operators.preceding = infix;
|
|
1057
1300
|
const right = parseExpression(infix.high + 1);
|
|
1058
1301
|
const left = state.left;
|
|
1059
1302
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1060
1303
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1304
|
+
const prefixEnds = [getRange(start).end];
|
|
1061
1305
|
state.left =
|
|
1062
1306
|
left.kind === kind && previous?.text === token.text
|
|
1063
|
-
? {
|
|
1064
|
-
|
|
1307
|
+
? {
|
|
1308
|
+
...left,
|
|
1309
|
+
operands: [...left.operands, right],
|
|
1310
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1311
|
+
}
|
|
1312
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1065
1313
|
} else if (token.text === "\\X") {
|
|
1066
1314
|
state.left =
|
|
1067
1315
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1068
1316
|
? { ...left, factors: [...left.factors, right] }
|
|
1069
1317
|
: { kind: "Product", factors: [left, right], location };
|
|
1070
1318
|
} else {
|
|
1071
|
-
state.left = {
|
|
1319
|
+
state.left = {
|
|
1320
|
+
kind: "Apply",
|
|
1321
|
+
operator: token.text,
|
|
1322
|
+
spelling: token.spelling,
|
|
1323
|
+
args: [left, right],
|
|
1324
|
+
location,
|
|
1325
|
+
};
|
|
1072
1326
|
}
|
|
1073
1327
|
state.left = withRange(state.left, start);
|
|
1074
1328
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1088,7 +1342,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1088
1342
|
const body = parseExpression();
|
|
1089
1343
|
const last = tokens[cursor.index - 1];
|
|
1090
1344
|
const end = { line: last.line, column: last.endColumn };
|
|
1091
|
-
return { body, bodyRange: { module: openModules[
|
|
1345
|
+
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1092
1346
|
};
|
|
1093
1347
|
|
|
1094
1348
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
@@ -1096,6 +1350,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1096
1350
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1097
1351
|
};
|
|
1098
1352
|
|
|
1353
|
+
const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
|
|
1354
|
+
name: named.name,
|
|
1355
|
+
arity: 0,
|
|
1356
|
+
location: {
|
|
1357
|
+
line: named.range.start.line,
|
|
1358
|
+
column: named.range.start.column,
|
|
1359
|
+
range: named.range,
|
|
1360
|
+
},
|
|
1361
|
+
});
|
|
1362
|
+
|
|
1099
1363
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1100
1364
|
const token = peek();
|
|
1101
1365
|
const location = getLocation(token);
|
|
@@ -1115,28 +1379,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1115
1379
|
peek(2).kind === "Identifier" &&
|
|
1116
1380
|
isAt("==", 3)
|
|
1117
1381
|
) {
|
|
1118
|
-
const left =
|
|
1382
|
+
const left = expectName();
|
|
1119
1383
|
const operator = advance().text;
|
|
1120
|
-
const right =
|
|
1384
|
+
const right = expectName();
|
|
1121
1385
|
expect("==");
|
|
1122
|
-
return parseOperator(operator, [
|
|
1123
|
-
{ name: left, arity: 0 },
|
|
1124
|
-
{ name: right, arity: 0 },
|
|
1125
|
-
]);
|
|
1386
|
+
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1126
1387
|
}
|
|
1127
|
-
const isPrefixDefinition =
|
|
1388
|
+
const isPrefixDefinition =
|
|
1389
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1128
1390
|
const isPostfixDefinition =
|
|
1129
1391
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1130
1392
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
1131
1393
|
const operator = isPrefixDefinition ? token.text : peek(1).text;
|
|
1132
|
-
|
|
1133
|
-
const param =
|
|
1394
|
+
if (isPrefixDefinition) advance();
|
|
1395
|
+
const param = expectName();
|
|
1396
|
+
if (isPostfixDefinition) advance();
|
|
1134
1397
|
expect("==");
|
|
1135
|
-
return parseOperator(operator, [
|
|
1398
|
+
return parseOperator(operator, [toParam(param)]);
|
|
1136
1399
|
}
|
|
1137
1400
|
const name = expectIdentifier();
|
|
1138
1401
|
if (accept("[")) {
|
|
1139
|
-
const bounds =
|
|
1402
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1140
1403
|
expect("]");
|
|
1141
1404
|
expect("==");
|
|
1142
1405
|
return {
|
|
@@ -1167,12 +1430,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1167
1430
|
): Instance => {
|
|
1168
1431
|
const start = cursor.index;
|
|
1169
1432
|
expect("INSTANCE");
|
|
1170
|
-
const module =
|
|
1433
|
+
const module = expectModuleName();
|
|
1171
1434
|
const substitutions = accept("WITH")
|
|
1172
1435
|
? parseSeparated(() => {
|
|
1173
|
-
const
|
|
1436
|
+
const targetStart = cursor.index;
|
|
1437
|
+
const target = expectSubstitutionTarget();
|
|
1438
|
+
const nameRange = getRange(targetStart);
|
|
1174
1439
|
expect("<-");
|
|
1175
|
-
return { name: target, expression: parseArgument() };
|
|
1440
|
+
return { name: target, nameRange, expression: parseArgument() };
|
|
1176
1441
|
})
|
|
1177
1442
|
: [];
|
|
1178
1443
|
return {
|
|
@@ -1188,6 +1453,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1188
1453
|
};
|
|
1189
1454
|
|
|
1190
1455
|
const parseDeclaration = (): Declaration => {
|
|
1456
|
+
const start = cursor.index;
|
|
1457
|
+
return withRange(parseDeclarationUnit(), start);
|
|
1458
|
+
};
|
|
1459
|
+
|
|
1460
|
+
const parseDeclarationUnit = (): Declaration => {
|
|
1191
1461
|
const location = getLocation(peek());
|
|
1192
1462
|
if (isAt("_") && peek(1).kind === "Operator") {
|
|
1193
1463
|
advance();
|
|
@@ -1233,7 +1503,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1233
1503
|
const token = peek();
|
|
1234
1504
|
const next = peek(1);
|
|
1235
1505
|
if (token.kind !== "Identifier") {
|
|
1236
|
-
return
|
|
1506
|
+
return (
|
|
1507
|
+
token.kind !== "String" &&
|
|
1508
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1509
|
+
next.kind === "Identifier" &&
|
|
1510
|
+
isAt("==", 2)
|
|
1511
|
+
);
|
|
1237
1512
|
}
|
|
1238
1513
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1239
1514
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1387,7 +1662,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1387
1662
|
const parseDefs = () => {
|
|
1388
1663
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1389
1664
|
const defs = parseSeparated(parseFact);
|
|
1390
|
-
const invalid =
|
|
1665
|
+
const invalid =
|
|
1666
|
+
mode === "tlc"
|
|
1667
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1668
|
+
: undefined;
|
|
1391
1669
|
if (invalid) {
|
|
1392
1670
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1393
1671
|
}
|
|
@@ -1412,11 +1690,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1412
1690
|
location: getLocation(keyword),
|
|
1413
1691
|
};
|
|
1414
1692
|
const invalid =
|
|
1415
|
-
use.kind === "Hide" &&
|
|
1416
|
-
if (invalid) {
|
|
1417
|
-
throw
|
|
1418
|
-
|
|
1419
|
-
|
|
1693
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1694
|
+
if (invalid.length > 0) {
|
|
1695
|
+
throw createSanyError(
|
|
1696
|
+
invalid.map((fact) => ({
|
|
1697
|
+
location: formatRange(fact.location.range ?? getRange(start)),
|
|
1698
|
+
message:
|
|
1699
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1700
|
+
})),
|
|
1420
1701
|
);
|
|
1421
1702
|
}
|
|
1422
1703
|
return withRange(use, start);
|
|
@@ -1432,10 +1713,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1432
1713
|
}
|
|
1433
1714
|
if (level !== undefined) advance();
|
|
1434
1715
|
if (level === "Variable") {
|
|
1435
|
-
const
|
|
1436
|
-
declareProofSymbol(name);
|
|
1716
|
+
const named = expectName();
|
|
1717
|
+
declareProofSymbol(named.name);
|
|
1437
1718
|
return withRange(
|
|
1438
|
-
{
|
|
1719
|
+
{
|
|
1720
|
+
kind: "NewSymbol",
|
|
1721
|
+
name: named.name,
|
|
1722
|
+
arity: 0,
|
|
1723
|
+
level,
|
|
1724
|
+
set: undefined,
|
|
1725
|
+
nameRange: named.range,
|
|
1726
|
+
location,
|
|
1727
|
+
},
|
|
1439
1728
|
start,
|
|
1440
1729
|
);
|
|
1441
1730
|
}
|
|
@@ -1453,6 +1742,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1453
1742
|
arity: declaration.arity,
|
|
1454
1743
|
level: level ?? "Constant",
|
|
1455
1744
|
set: isBounded ? parseExpression() : undefined,
|
|
1745
|
+
nameRange: declaration.location.range,
|
|
1456
1746
|
location,
|
|
1457
1747
|
},
|
|
1458
1748
|
start,
|
|
@@ -1495,7 +1785,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1495
1785
|
const token = peek();
|
|
1496
1786
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1497
1787
|
if (isAt("INSTANCE")) {
|
|
1498
|
-
|
|
1788
|
+
const start = cursor.index;
|
|
1789
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1499
1790
|
}
|
|
1500
1791
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1501
1792
|
const start = cursor.index;
|
|
@@ -1508,21 +1799,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1508
1799
|
if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
|
|
1509
1800
|
if (accept("TAKE")) {
|
|
1510
1801
|
const isBounded = isBoundAhead();
|
|
1511
|
-
const names = isBounded ? [] : parseSeparated(
|
|
1802
|
+
const names = getNames(isBounded ? [] : parseSeparated(expectName));
|
|
1512
1803
|
const bounds = isBounded ? parseBounds() : [];
|
|
1513
|
-
declareProofSymbols(names, bounds);
|
|
1514
|
-
return { kind: "Take", names, bounds };
|
|
1804
|
+
declareProofSymbols(names.names, bounds);
|
|
1805
|
+
return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
|
|
1515
1806
|
}
|
|
1516
1807
|
if (accept("WITNESS")) {
|
|
1517
1808
|
return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
|
|
1518
1809
|
}
|
|
1519
1810
|
if (accept("PICK")) {
|
|
1520
1811
|
const isNames = isNameListAhead(":");
|
|
1521
|
-
const names = isNames ? parseSeparated(
|
|
1812
|
+
const names = getNames(isNames ? parseSeparated(expectName) : []);
|
|
1522
1813
|
const bounds = isNames ? [] : parseBounds();
|
|
1523
|
-
declareProofSymbols(names, bounds);
|
|
1814
|
+
declareProofSymbols(names.names, bounds);
|
|
1524
1815
|
expect(":");
|
|
1525
|
-
return {
|
|
1816
|
+
return {
|
|
1817
|
+
kind: "Pick",
|
|
1818
|
+
names: names.names,
|
|
1819
|
+
nameRanges: names.ranges,
|
|
1820
|
+
bounds,
|
|
1821
|
+
body: parseExpression(),
|
|
1822
|
+
};
|
|
1526
1823
|
}
|
|
1527
1824
|
if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
|
|
1528
1825
|
return {
|
|
@@ -1552,10 +1849,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1552
1849
|
const parseStep = (): ProofStep => {
|
|
1553
1850
|
const start = cursor.index;
|
|
1554
1851
|
const step = parseStepStart(isAt("QED", 1));
|
|
1852
|
+
const statementStart = cursor.index;
|
|
1555
1853
|
const scope = new Set<string>();
|
|
1556
1854
|
proofScopes.push(scope);
|
|
1557
1855
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1558
1856
|
const head = getRange(start);
|
|
1857
|
+
const statement = getRange(statementStart);
|
|
1559
1858
|
const hasProof = isProofStart(peek());
|
|
1560
1859
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1561
1860
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1563,7 +1862,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1563
1862
|
const proof = hasProof ? parseProof() : undefined;
|
|
1564
1863
|
proofScopes.pop();
|
|
1565
1864
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1566
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1865
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1567
1866
|
};
|
|
1568
1867
|
|
|
1569
1868
|
const parseProofBody = (): Proof => {
|
|
@@ -1598,6 +1897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1598
1897
|
return withRange(proof, start);
|
|
1599
1898
|
};
|
|
1600
1899
|
|
|
1900
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1901
|
+
// definition, warning of the second.
|
|
1902
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1903
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1904
|
+
module.definitions.push(definition);
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1601
1907
|
const parseTheorem = (module: Module) => {
|
|
1602
1908
|
const start = cursor.index;
|
|
1603
1909
|
const keyword = advance();
|
|
@@ -1619,7 +1925,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1619
1925
|
module.theorems.push(theorem);
|
|
1620
1926
|
module.units.push({ kind: "Theorem", theorem });
|
|
1621
1927
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1622
|
-
|
|
1928
|
+
addFactDefinition(
|
|
1929
|
+
module,
|
|
1623
1930
|
withRange(
|
|
1624
1931
|
{
|
|
1625
1932
|
kind: "Operator",
|
|
@@ -1656,7 +1963,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1656
1963
|
const parseModuleBody = (): Module => {
|
|
1657
1964
|
const location = getLocation(expect("----"));
|
|
1658
1965
|
expect("MODULE");
|
|
1659
|
-
const name =
|
|
1966
|
+
const name = expectModuleName();
|
|
1660
1967
|
expect("----");
|
|
1661
1968
|
const module: Module = {
|
|
1662
1969
|
name,
|
|
@@ -1678,7 +1985,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1678
1985
|
if (accept("EXTENDS")) {
|
|
1679
1986
|
parseSeparated(() => {
|
|
1680
1987
|
const start = cursor.index;
|
|
1681
|
-
module.extends.push(
|
|
1988
|
+
module.extends.push(expectModuleName());
|
|
1682
1989
|
module.extendsRanges.push(getRange(start));
|
|
1683
1990
|
});
|
|
1684
1991
|
}
|
|
@@ -1686,14 +1993,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1686
1993
|
const tokenIndex = cursor.index;
|
|
1687
1994
|
unitStart.index = tokenIndex;
|
|
1688
1995
|
const token = peek();
|
|
1689
|
-
if (token.kind === "ModuleEnd"
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
}
|
|
1996
|
+
if (token.kind === "ModuleEnd") {
|
|
1997
|
+
location.range = {
|
|
1998
|
+
module: openModules[0].name,
|
|
1999
|
+
start: { line: location.line, column: location.column },
|
|
2000
|
+
end: { line: token.line, column: token.endColumn },
|
|
2001
|
+
};
|
|
2002
|
+
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1697
2003
|
openModules.pop();
|
|
1698
2004
|
recursiveScopes.pop();
|
|
1699
2005
|
return module;
|
|
@@ -1712,22 +2018,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1712
2018
|
switch (token.text) {
|
|
1713
2019
|
case "CONSTANT":
|
|
1714
2020
|
case "CONSTANTS": {
|
|
1715
|
-
advance();
|
|
2021
|
+
const unitLocation = getLocation(advance());
|
|
1716
2022
|
const declarations = parseDeclarations();
|
|
1717
2023
|
module.constants.push(...declarations);
|
|
1718
|
-
|
|
2024
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2025
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1719
2026
|
continue;
|
|
1720
2027
|
}
|
|
1721
2028
|
case "VARIABLE":
|
|
1722
2029
|
case "VARIABLES": {
|
|
1723
|
-
advance();
|
|
2030
|
+
const unitLocation = getLocation(advance());
|
|
1724
2031
|
const declarations = parseSeparated(() => {
|
|
1725
2032
|
const start = cursor.index;
|
|
1726
2033
|
const location = getLocation(peek());
|
|
1727
2034
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1728
2035
|
});
|
|
1729
2036
|
module.variables.push(...declarations);
|
|
1730
|
-
|
|
2037
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2038
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1731
2039
|
continue;
|
|
1732
2040
|
}
|
|
1733
2041
|
case "ASSUME":
|
|
@@ -1737,7 +2045,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1737
2045
|
module.assumptions.push(assumption);
|
|
1738
2046
|
module.units.push({ kind: "Assumption", assumption });
|
|
1739
2047
|
if (assumption.name !== undefined) {
|
|
1740
|
-
module
|
|
2048
|
+
addFactDefinition(module, {
|
|
1741
2049
|
kind: "Operator",
|
|
1742
2050
|
name: assumption.name,
|
|
1743
2051
|
params: [],
|
|
@@ -1751,6 +2059,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1751
2059
|
}
|
|
1752
2060
|
case "USE":
|
|
1753
2061
|
case "HIDE": {
|
|
2062
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2063
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2064
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2065
|
+
}
|
|
1754
2066
|
const use = parseUseOrHide();
|
|
1755
2067
|
module.uses.push(use);
|
|
1756
2068
|
module.units.push({ kind: "Use", use });
|
|
@@ -1775,7 +2087,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1775
2087
|
);
|
|
1776
2088
|
continue;
|
|
1777
2089
|
}
|
|
1778
|
-
addDefinitionUnit(module,
|
|
2090
|
+
addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
|
|
1779
2091
|
continue;
|
|
1780
2092
|
}
|
|
1781
2093
|
}
|
|
@@ -1797,9 +2109,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1797
2109
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1798
2110
|
};
|
|
1799
2111
|
|
|
1800
|
-
|
|
2112
|
+
const module = parseModuleBody();
|
|
2113
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2114
|
+
// module's name from a keyword that the parser would take for one.
|
|
2115
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2116
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2117
|
+
return module;
|
|
1801
2118
|
};
|
|
1802
2119
|
|
|
1803
|
-
export const parseModule = (source: string) => parse(source,
|
|
2120
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2121
|
+
|
|
2122
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1804
2123
|
|
|
1805
|
-
export const
|
|
2124
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|