tla-ts 0.0.3-dev.fca2ca8 → 0.0.3-dev.fe239f8
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 +168 -27
- package/dist/index.d.ts +168 -27
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -20
- package/dist/run-cli.js +1351 -20
- package/dist/transfer.cjs +83 -28
- package/dist/transfer.js +83 -28
- package/package.json +5 -1
- package/src/ast.ts +55 -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 +1343 -284
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +252 -179
- package/src/environment.ts +431 -120
- package/src/errors.ts +195 -7
- package/src/evaluate.ts +718 -146
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +190 -117
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +450 -135
- 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 +1367 -404
- package/src/run-cli.ts +1167 -155
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +26 -7
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +158 -120
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +683 -256
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,
|
|
@@ -141,6 +156,19 @@ const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
|
|
|
141
156
|
|
|
142
157
|
const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
|
|
143
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
|
+
|
|
144
172
|
const EXPRESSION_PREFIXES = new Set([
|
|
145
173
|
"-",
|
|
146
174
|
"~",
|
|
@@ -169,6 +197,16 @@ const OPERATOR_ARGUMENTS = new Set([
|
|
|
169
197
|
...POSTFIX_OPERATORS,
|
|
170
198
|
]);
|
|
171
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
|
+
|
|
172
210
|
const isOperatorName = (text: string) =>
|
|
173
211
|
INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
|
|
174
212
|
|
|
@@ -262,6 +300,7 @@ const FACT_KEYWORDS = new Set([
|
|
|
262
300
|
"IF",
|
|
263
301
|
"LAMBDA",
|
|
264
302
|
"LET",
|
|
303
|
+
"MODULE",
|
|
265
304
|
"SF_",
|
|
266
305
|
"SUBSET",
|
|
267
306
|
"UNCHANGED",
|
|
@@ -291,30 +330,36 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
|
|
|
291
330
|
|
|
292
331
|
const NO_OFFSIDE = -1;
|
|
293
332
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
// The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
|
|
297
|
-
// 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.
|
|
298
335
|
export const extractModule = (source: string, name: string) => {
|
|
336
|
+
const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
|
|
299
337
|
const lines = source.split(/\r?\n/);
|
|
300
|
-
const start = lines.findIndex((line) =>
|
|
338
|
+
const start = lines.findIndex((line) => header.test(line));
|
|
301
339
|
if (start === -1) return undefined;
|
|
302
340
|
const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
|
|
303
|
-
return lines.slice(start, end === -1 ? undefined : end + 1).join("\n")
|
|
341
|
+
return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
|
|
304
342
|
};
|
|
305
343
|
|
|
306
|
-
//
|
|
307
|
-
// SANY does not.
|
|
308
|
-
|
|
309
|
-
|
|
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);
|
|
310
352
|
const cursor = { index: 0, offsideColumns: [] as number[] };
|
|
353
|
+
const operators: OperatorContext = {};
|
|
311
354
|
// A RECURSIVE declaration covers the definitions of its own LET or module.
|
|
312
355
|
const recursiveScopes: Set<string>[] = [];
|
|
313
356
|
const factNames = new Set<string>();
|
|
314
357
|
// The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
|
|
315
358
|
const proofScopes: Set<string>[] = [];
|
|
316
|
-
// 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.
|
|
317
361
|
const factStart = { index: -1 };
|
|
362
|
+
// SANY reports a location in a nested module as one in the module of its file.
|
|
318
363
|
const openModules: Module[] = [];
|
|
319
364
|
|
|
320
365
|
const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
|
|
@@ -329,15 +374,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
329
374
|
const tokenEnds: SourceLocation[] = [];
|
|
330
375
|
const tokenRanges: SourceRange[] = [];
|
|
331
376
|
|
|
332
|
-
const getRange = (startIndex: number) => {
|
|
333
|
-
const endIndex = cursor.index - 1;
|
|
377
|
+
const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
|
|
334
378
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
335
379
|
if (cached) return cached;
|
|
336
380
|
const last = tokens[endIndex];
|
|
337
381
|
tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
|
|
338
382
|
tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
|
|
339
383
|
const range = {
|
|
340
|
-
module: openModules[
|
|
384
|
+
module: openModules[0].name,
|
|
341
385
|
start: tokenStarts[startIndex],
|
|
342
386
|
end: tokenEnds[endIndex],
|
|
343
387
|
};
|
|
@@ -435,8 +479,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
435
479
|
const createUnexpected = (message: string, token: Token = peekRaw()) =>
|
|
436
480
|
createError(message, token, token);
|
|
437
481
|
|
|
482
|
+
// TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
|
|
438
483
|
const isFieldName = (token: Token) =>
|
|
439
|
-
(token.kind === "Identifier" ||
|
|
484
|
+
(token.kind === "Identifier" ||
|
|
485
|
+
token.kind === "Keyword" ||
|
|
486
|
+
(token.kind === "Operator" && token.text === "_")) &&
|
|
487
|
+
!isTlapmKeyword(token);
|
|
440
488
|
|
|
441
489
|
const isAt = (text: string, offset = 0) => {
|
|
442
490
|
const token = peek(offset);
|
|
@@ -472,7 +520,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
472
520
|
return token.text;
|
|
473
521
|
};
|
|
474
522
|
|
|
475
|
-
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();
|
|
476
559
|
cursor.offsideColumns.push(NO_OFFSIDE);
|
|
477
560
|
const result = parse();
|
|
478
561
|
cursor.offsideColumns.pop();
|
|
@@ -514,10 +597,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
514
597
|
};
|
|
515
598
|
|
|
516
599
|
const parsePattern = (): BoundPattern => {
|
|
517
|
-
if (!accept("<<"))
|
|
518
|
-
|
|
600
|
+
if (!accept("<<")) {
|
|
601
|
+
const named = expectName();
|
|
602
|
+
return { kind: "Name", name: named.name, ranges: [named.range] };
|
|
603
|
+
}
|
|
604
|
+
const names = getNames(parseSeparated(expectName));
|
|
519
605
|
expect(">>");
|
|
520
|
-
return { kind: "Tuple", names };
|
|
606
|
+
return { kind: "Tuple", names: names.names, ranges: names.ranges };
|
|
521
607
|
};
|
|
522
608
|
|
|
523
609
|
const parseBounds = () =>
|
|
@@ -560,8 +646,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
560
646
|
);
|
|
561
647
|
};
|
|
562
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.
|
|
563
651
|
const isStandalone = (operator: Token) => {
|
|
564
|
-
if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
|
|
565
652
|
const token = peek(1);
|
|
566
653
|
return (
|
|
567
654
|
token.kind === "End" ||
|
|
@@ -600,7 +687,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
600
687
|
if (isOperatorAlone) {
|
|
601
688
|
cursor.index++;
|
|
602
689
|
return withRange(
|
|
603
|
-
{
|
|
690
|
+
{
|
|
691
|
+
kind: "OperatorReference",
|
|
692
|
+
operator: token.text,
|
|
693
|
+
spelling: token.spelling,
|
|
694
|
+
location: getLocation(token),
|
|
695
|
+
},
|
|
604
696
|
cursor.index - 1,
|
|
605
697
|
);
|
|
606
698
|
}
|
|
@@ -610,32 +702,58 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
610
702
|
cursor.index++;
|
|
611
703
|
const location = getLocation(token);
|
|
612
704
|
if (POSTFIX_OPERATORS.has(token.text)) {
|
|
613
|
-
return
|
|
705
|
+
return withRange(
|
|
706
|
+
{ kind: "OperatorReference", operator: token.text, location },
|
|
707
|
+
cursor.index - 1,
|
|
708
|
+
);
|
|
614
709
|
}
|
|
615
710
|
const argument: Expression = { kind: "Identifier", name: "argument", location };
|
|
616
|
-
return
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
711
|
+
return withRange(
|
|
712
|
+
{
|
|
713
|
+
kind: "Lambda",
|
|
714
|
+
params: [argument.name],
|
|
715
|
+
body: getOperatorApplication(token.text, argument, location),
|
|
716
|
+
operator: token.text,
|
|
717
|
+
location: getLocation(token),
|
|
718
|
+
},
|
|
719
|
+
cursor.index - 1,
|
|
720
|
+
);
|
|
622
721
|
}
|
|
623
722
|
if (isAt("LAMBDA")) {
|
|
624
723
|
const start = cursor.index;
|
|
625
724
|
advance();
|
|
626
|
-
const params = parseSeparated(
|
|
725
|
+
const params = getNames(parseSeparated(expectName));
|
|
627
726
|
expect(":");
|
|
628
727
|
const body = parseExpression();
|
|
629
|
-
return withRange(
|
|
728
|
+
return withRange(
|
|
729
|
+
{
|
|
730
|
+
kind: "Lambda",
|
|
731
|
+
params: params.names,
|
|
732
|
+
paramRanges: params.ranges,
|
|
733
|
+
body,
|
|
734
|
+
location: getLocation(token),
|
|
735
|
+
},
|
|
736
|
+
start,
|
|
737
|
+
);
|
|
630
738
|
}
|
|
631
|
-
|
|
739
|
+
const argument = parseExpression();
|
|
740
|
+
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
741
|
+
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
742
|
+
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
743
|
+
return argument;
|
|
632
744
|
};
|
|
633
745
|
|
|
746
|
+
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
747
|
+
|
|
748
|
+
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
749
|
+
|
|
634
750
|
const parseArguments = () =>
|
|
635
|
-
|
|
751
|
+
withBracketLayout(() => {
|
|
752
|
+
const start = cursor.index;
|
|
636
753
|
expect("(");
|
|
637
754
|
const args = parseSeparated(parseArgument);
|
|
638
755
|
expect(")");
|
|
756
|
+
lastArguments.range = getRange(start);
|
|
639
757
|
return args;
|
|
640
758
|
});
|
|
641
759
|
|
|
@@ -657,15 +775,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
657
775
|
};
|
|
658
776
|
|
|
659
777
|
const parseBracket = (start: Token): Expression =>
|
|
660
|
-
|
|
778
|
+
withBracketLayout(() => {
|
|
661
779
|
const location = getLocation(start);
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
780
|
+
// SANY takes a keyword for the first field of a record, as it does for a selected field.
|
|
781
|
+
const isIdentifier = peek().kind === "Identifier";
|
|
782
|
+
const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
|
|
783
|
+
if (isRecord || (isIdentifier && isAt(":", 1))) {
|
|
784
|
+
const kind = isRecord ? "Record" : "RecordSet";
|
|
785
|
+
const parseField = (readName: () => string) => {
|
|
786
|
+
const fieldStart = cursor.index;
|
|
787
|
+
const fieldLocation = getLocation(peek());
|
|
788
|
+
const name = readName();
|
|
789
|
+
fieldLocation.range = getRange(fieldStart);
|
|
790
|
+
expect(isRecord ? "|->" : ":");
|
|
791
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
792
|
+
};
|
|
793
|
+
const fields = [
|
|
794
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
795
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
796
|
+
];
|
|
669
797
|
expect("]");
|
|
670
798
|
return { kind, fields, location };
|
|
671
799
|
}
|
|
@@ -703,17 +831,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
703
831
|
expect("]");
|
|
704
832
|
return { kind: "Except", function: expression, updates, location };
|
|
705
833
|
}
|
|
834
|
+
if (!isTlapm && !isAt("]_")) {
|
|
835
|
+
const args = [expression];
|
|
836
|
+
while (accept(",")) args.push(parseExpression());
|
|
837
|
+
expect("]");
|
|
838
|
+
return { kind: "Bracket", args, location };
|
|
839
|
+
}
|
|
706
840
|
expect("]_");
|
|
707
|
-
return {
|
|
708
|
-
kind: "BoxAction",
|
|
709
|
-
action: expression,
|
|
710
|
-
subscript: parseSubscript(),
|
|
711
|
-
location,
|
|
712
|
-
};
|
|
841
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
713
842
|
});
|
|
714
843
|
|
|
844
|
+
// SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
|
|
845
|
+
const parseSetMembership = (): Expression => {
|
|
846
|
+
const start = cursor.index;
|
|
847
|
+
const element = withRange(parsePrefix(), start);
|
|
848
|
+
const location = getLocation(advance());
|
|
849
|
+
return withRange(
|
|
850
|
+
{ kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
|
|
851
|
+
start,
|
|
852
|
+
);
|
|
853
|
+
};
|
|
854
|
+
|
|
715
855
|
const parseBrace = (start: Token): Expression =>
|
|
716
|
-
|
|
856
|
+
withBracketLayout(() => {
|
|
717
857
|
const location = getLocation(start);
|
|
718
858
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
719
859
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -728,7 +868,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
728
868
|
}
|
|
729
869
|
cursor.index = checkpoint;
|
|
730
870
|
}
|
|
731
|
-
const first =
|
|
871
|
+
const first =
|
|
872
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
732
873
|
if (accept(":")) {
|
|
733
874
|
const bounds = parseBounds();
|
|
734
875
|
expect("}");
|
|
@@ -742,7 +883,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
742
883
|
|
|
743
884
|
const parseTuple = (start: Token): Expression => {
|
|
744
885
|
const location = getLocation(start);
|
|
745
|
-
const elements =
|
|
886
|
+
const elements = withBracketLayout(() =>
|
|
746
887
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
747
888
|
);
|
|
748
889
|
if (accept(">>_")) {
|
|
@@ -763,12 +904,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
763
904
|
expect(":");
|
|
764
905
|
return { kind, bounds, body: parseExpression(), location };
|
|
765
906
|
}
|
|
766
|
-
const names = parseSeparated(
|
|
907
|
+
const names = getNames(parseSeparated(expectName));
|
|
767
908
|
expect(":");
|
|
768
909
|
return {
|
|
769
910
|
kind: "UnboundedQuantifier",
|
|
770
911
|
quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
|
|
771
|
-
names,
|
|
912
|
+
names: names.names,
|
|
913
|
+
nameRanges: names.ranges,
|
|
772
914
|
body: parseExpression(),
|
|
773
915
|
isTemporal,
|
|
774
916
|
location,
|
|
@@ -822,8 +964,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
822
964
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
823
965
|
};
|
|
824
966
|
|
|
825
|
-
const parsePrimary = (): Expression => {
|
|
826
|
-
const
|
|
967
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
968
|
+
const start = cursor.index;
|
|
969
|
+
const isFactStart = start === factStart.index;
|
|
827
970
|
const token = advance();
|
|
828
971
|
const location = getLocation(token);
|
|
829
972
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -847,53 +990,116 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
847
990
|
if (token.kind === "Identifier" || isStep) {
|
|
848
991
|
const name = isStep ? (getStepName(token) ?? token.text) : token.text;
|
|
849
992
|
if (isLabelAhead()) {
|
|
850
|
-
const params = accept("(") ? parseSeparated(
|
|
851
|
-
if (params.length > 0) expect(")");
|
|
993
|
+
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
994
|
+
if (params.names.length > 0) expect(")");
|
|
852
995
|
expect("::");
|
|
853
|
-
|
|
996
|
+
const body = parseExpression();
|
|
997
|
+
// TLAplusParser.labelDoesNotChangeParse.
|
|
998
|
+
if (
|
|
999
|
+
!isTlapm &&
|
|
1000
|
+
preceding !== undefined &&
|
|
1001
|
+
operators.last !== undefined &&
|
|
1002
|
+
isOperatorExpression(body) &&
|
|
1003
|
+
preceding.high >= operators.last.low
|
|
1004
|
+
) {
|
|
1005
|
+
throw createError("Removing the label would change expression parsing", token);
|
|
1006
|
+
}
|
|
1007
|
+
return {
|
|
1008
|
+
kind: "Label",
|
|
1009
|
+
name,
|
|
1010
|
+
params: params.names,
|
|
1011
|
+
paramRanges: params.ranges,
|
|
1012
|
+
body,
|
|
1013
|
+
location,
|
|
1014
|
+
};
|
|
854
1015
|
}
|
|
855
1016
|
const hasArguments = isAt("(");
|
|
1017
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1018
|
+
return { kind: "ExceptAt", location };
|
|
856
1019
|
const args = hasArguments ? parseArguments() : [];
|
|
1020
|
+
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
857
1021
|
if (!isSelectorAhead()) {
|
|
858
1022
|
return hasArguments
|
|
859
|
-
? { kind: "Apply", operator: name, args, location }
|
|
1023
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
860
1024
|
: { kind: "Identifier", name, location };
|
|
861
1025
|
}
|
|
862
1026
|
const instance = openModules
|
|
863
1027
|
.flatMap((module) => module.instances)
|
|
864
1028
|
.find((candidate) => candidate.name === name);
|
|
865
|
-
|
|
1029
|
+
const isMissingArguments =
|
|
1030
|
+
instance !== undefined &&
|
|
1031
|
+
mode === "tlc" &&
|
|
1032
|
+
!isFactStart &&
|
|
1033
|
+
instance.params.length !== args.length;
|
|
1034
|
+
if (isMissingArguments && args.length > 0) {
|
|
866
1035
|
throw createError(
|
|
867
1036
|
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
868
1037
|
token,
|
|
869
1038
|
);
|
|
870
1039
|
}
|
|
871
1040
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1041
|
+
const selectorRanges = [getRange(start, start)];
|
|
872
1042
|
while (isSelectorAhead()) {
|
|
873
1043
|
advance();
|
|
1044
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
1045
|
+
lastArguments.range = undefined;
|
|
874
1046
|
selectors.push(parseSelector());
|
|
1047
|
+
argumentRanges.push(lastArguments.range);
|
|
875
1048
|
}
|
|
876
1049
|
const names = selectors.flatMap((selector) =>
|
|
877
1050
|
selector.kind === "Name" ? [selector.name] : [],
|
|
878
1051
|
);
|
|
879
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
880
1052
|
const operator = names.join("!");
|
|
881
1053
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
882
1054
|
"args" in selector ? selector.args : [],
|
|
883
1055
|
);
|
|
884
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
885
1056
|
const argumentCounts = selectors.map((selector) =>
|
|
886
1057
|
"args" in selector ? selector.args.length : 0,
|
|
887
1058
|
);
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
1059
|
+
const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
|
|
1060
|
+
const reference: Expression =
|
|
1061
|
+
names.length < selectors.length
|
|
1062
|
+
? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
|
|
1063
|
+
: qualifiedArgs.length === 0
|
|
1064
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
1065
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
1066
|
+
? {
|
|
1067
|
+
kind: "Apply",
|
|
1068
|
+
operator,
|
|
1069
|
+
args: qualifiedArgs,
|
|
1070
|
+
argumentCounts,
|
|
1071
|
+
argumentsRange,
|
|
1072
|
+
selectorRanges,
|
|
1073
|
+
argumentRanges,
|
|
1074
|
+
location,
|
|
1075
|
+
}
|
|
1076
|
+
: {
|
|
1077
|
+
kind: "Apply",
|
|
1078
|
+
operator,
|
|
1079
|
+
args: qualifiedArgs,
|
|
1080
|
+
argumentsRange,
|
|
1081
|
+
selectorRanges,
|
|
1082
|
+
argumentRanges,
|
|
1083
|
+
location,
|
|
1084
|
+
};
|
|
1085
|
+
if (isMissingArguments) {
|
|
1086
|
+
missingArguments.set(reference, {
|
|
1087
|
+
location: formatRange(getRange(start)),
|
|
1088
|
+
message: `The operator ${name} requires ${instance.params.length} arguments.`,
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
return reference;
|
|
891
1092
|
}
|
|
892
|
-
|
|
1093
|
+
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1094
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
893
1095
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
1096
|
+
}
|
|
1097
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1098
|
+
return { kind: "Identifier", name: token.text, location };
|
|
1099
|
+
}
|
|
894
1100
|
switch (token.text) {
|
|
895
1101
|
case "(": {
|
|
896
|
-
const expression =
|
|
1102
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
897
1103
|
expect(")");
|
|
898
1104
|
return expression;
|
|
899
1105
|
}
|
|
@@ -949,7 +1155,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
949
1155
|
case "WF_":
|
|
950
1156
|
case "SF_": {
|
|
951
1157
|
const subscript = parseSubscript();
|
|
952
|
-
const action =
|
|
1158
|
+
const action = withBracketLayout(() => {
|
|
953
1159
|
expect("(");
|
|
954
1160
|
const expression = parseExpression();
|
|
955
1161
|
expect(")");
|
|
@@ -966,7 +1172,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
966
1172
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
967
1173
|
};
|
|
968
1174
|
|
|
1175
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1176
|
+
// prefix operator.
|
|
1177
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1178
|
+
operators.last = operators.preceding = { low, high };
|
|
1179
|
+
const operand = parseExpression(high + 1);
|
|
1180
|
+
const next = peek();
|
|
1181
|
+
const nextHigh =
|
|
1182
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1183
|
+
? POSTFIX_PRECEDENCE.high
|
|
1184
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1185
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1186
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1187
|
+
}
|
|
1188
|
+
return operand;
|
|
1189
|
+
};
|
|
1190
|
+
|
|
969
1191
|
const parsePrefix = (): Expression => {
|
|
1192
|
+
const preceding = operators.preceding;
|
|
1193
|
+
operators.preceding = undefined;
|
|
970
1194
|
const token = peek();
|
|
971
1195
|
const location = getLocation(token);
|
|
972
1196
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -976,35 +1200,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
976
1200
|
switch (token.text) {
|
|
977
1201
|
case "~":
|
|
978
1202
|
advance();
|
|
979
|
-
return {
|
|
1203
|
+
return {
|
|
1204
|
+
kind: "Apply",
|
|
1205
|
+
operator: "~",
|
|
1206
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1207
|
+
location,
|
|
1208
|
+
};
|
|
980
1209
|
case "-":
|
|
981
1210
|
advance();
|
|
982
1211
|
return {
|
|
983
1212
|
kind: "Apply",
|
|
984
1213
|
operator: "-.",
|
|
985
|
-
args: [
|
|
1214
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
986
1215
|
location,
|
|
987
1216
|
};
|
|
988
1217
|
case "[]":
|
|
989
1218
|
advance();
|
|
990
|
-
return { kind: "Always", expression:
|
|
1219
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
991
1220
|
case "<>":
|
|
992
1221
|
advance();
|
|
993
|
-
return { kind: "Eventually", expression:
|
|
1222
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
994
1223
|
case "ENABLED":
|
|
995
1224
|
advance();
|
|
996
|
-
return { kind: "Enabled", expression:
|
|
1225
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
997
1226
|
case "UNCHANGED":
|
|
998
1227
|
advance();
|
|
999
|
-
return { kind: "Unchanged", expression:
|
|
1228
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1000
1229
|
case "SUBSET":
|
|
1001
1230
|
case "UNION":
|
|
1002
1231
|
case "DOMAIN":
|
|
1003
1232
|
advance();
|
|
1004
|
-
return {
|
|
1233
|
+
return {
|
|
1234
|
+
kind: "Apply",
|
|
1235
|
+
operator: token.text,
|
|
1236
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1237
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1238
|
+
location,
|
|
1239
|
+
};
|
|
1005
1240
|
}
|
|
1006
1241
|
}
|
|
1007
|
-
return parsePrimary();
|
|
1242
|
+
return parsePrimary(preceding);
|
|
1008
1243
|
};
|
|
1009
1244
|
|
|
1010
1245
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1020,11 +1255,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1020
1255
|
const location = getLocation(token);
|
|
1021
1256
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1022
1257
|
advance();
|
|
1258
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1023
1259
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1024
1260
|
continue;
|
|
1025
1261
|
}
|
|
1026
1262
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1027
1263
|
advance();
|
|
1264
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1028
1265
|
state.left = withRange(
|
|
1029
1266
|
{ kind: "Apply", operator: token.text, args: [state.left], location },
|
|
1030
1267
|
start,
|
|
@@ -1033,8 +1270,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1033
1270
|
}
|
|
1034
1271
|
if (token.text === "[") {
|
|
1035
1272
|
advance();
|
|
1036
|
-
const args =
|
|
1037
|
-
|
|
1273
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1274
|
+
withBracketLayout(() => expect("]"));
|
|
1275
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1038
1276
|
state.left = withRange(
|
|
1039
1277
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1040
1278
|
start,
|
|
@@ -1065,21 +1303,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1065
1303
|
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1066
1304
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1067
1305
|
}
|
|
1306
|
+
operators.last = operators.preceding = infix;
|
|
1068
1307
|
const right = parseExpression(infix.high + 1);
|
|
1069
1308
|
const left = state.left;
|
|
1070
1309
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1071
1310
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1311
|
+
const prefixEnds = [getRange(start).end];
|
|
1072
1312
|
state.left =
|
|
1073
1313
|
left.kind === kind && previous?.text === token.text
|
|
1074
|
-
? {
|
|
1075
|
-
|
|
1314
|
+
? {
|
|
1315
|
+
...left,
|
|
1316
|
+
operands: [...left.operands, right],
|
|
1317
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1318
|
+
}
|
|
1319
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1076
1320
|
} else if (token.text === "\\X") {
|
|
1077
1321
|
state.left =
|
|
1078
1322
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1079
1323
|
? { ...left, factors: [...left.factors, right] }
|
|
1080
1324
|
: { kind: "Product", factors: [left, right], location };
|
|
1081
1325
|
} else {
|
|
1082
|
-
state.left = {
|
|
1326
|
+
state.left = {
|
|
1327
|
+
kind: "Apply",
|
|
1328
|
+
operator: token.text,
|
|
1329
|
+
spelling: token.spelling,
|
|
1330
|
+
args: [left, right],
|
|
1331
|
+
location,
|
|
1332
|
+
};
|
|
1083
1333
|
}
|
|
1084
1334
|
state.left = withRange(state.left, start);
|
|
1085
1335
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1099,7 +1349,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1099
1349
|
const body = parseExpression();
|
|
1100
1350
|
const last = tokens[cursor.index - 1];
|
|
1101
1351
|
const end = { line: last.line, column: last.endColumn };
|
|
1102
|
-
return { body, bodyRange: { module: openModules[
|
|
1352
|
+
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1103
1353
|
};
|
|
1104
1354
|
|
|
1105
1355
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
@@ -1107,6 +1357,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1107
1357
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1108
1358
|
};
|
|
1109
1359
|
|
|
1360
|
+
const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
|
|
1361
|
+
name: named.name,
|
|
1362
|
+
arity: 0,
|
|
1363
|
+
location: {
|
|
1364
|
+
line: named.range.start.line,
|
|
1365
|
+
column: named.range.start.column,
|
|
1366
|
+
range: named.range,
|
|
1367
|
+
},
|
|
1368
|
+
});
|
|
1369
|
+
|
|
1110
1370
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1111
1371
|
const token = peek();
|
|
1112
1372
|
const location = getLocation(token);
|
|
@@ -1126,28 +1386,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1126
1386
|
peek(2).kind === "Identifier" &&
|
|
1127
1387
|
isAt("==", 3)
|
|
1128
1388
|
) {
|
|
1129
|
-
const left =
|
|
1389
|
+
const left = expectName();
|
|
1130
1390
|
const operator = advance().text;
|
|
1131
|
-
const right =
|
|
1391
|
+
const right = expectName();
|
|
1132
1392
|
expect("==");
|
|
1133
|
-
return parseOperator(operator, [
|
|
1134
|
-
{ name: left, arity: 0 },
|
|
1135
|
-
{ name: right, arity: 0 },
|
|
1136
|
-
]);
|
|
1393
|
+
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1137
1394
|
}
|
|
1138
|
-
const isPrefixDefinition =
|
|
1395
|
+
const isPrefixDefinition =
|
|
1396
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1139
1397
|
const isPostfixDefinition =
|
|
1140
1398
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1141
1399
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
1142
1400
|
const operator = isPrefixDefinition ? token.text : peek(1).text;
|
|
1143
|
-
|
|
1144
|
-
const param =
|
|
1401
|
+
if (isPrefixDefinition) advance();
|
|
1402
|
+
const param = expectName();
|
|
1403
|
+
if (isPostfixDefinition) advance();
|
|
1145
1404
|
expect("==");
|
|
1146
|
-
return parseOperator(operator, [
|
|
1405
|
+
return parseOperator(operator, [toParam(param)]);
|
|
1147
1406
|
}
|
|
1148
1407
|
const name = expectIdentifier();
|
|
1149
1408
|
if (accept("[")) {
|
|
1150
|
-
const bounds =
|
|
1409
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1151
1410
|
expect("]");
|
|
1152
1411
|
expect("==");
|
|
1153
1412
|
return {
|
|
@@ -1178,12 +1437,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1178
1437
|
): Instance => {
|
|
1179
1438
|
const start = cursor.index;
|
|
1180
1439
|
expect("INSTANCE");
|
|
1181
|
-
const module =
|
|
1440
|
+
const module = expectModuleName();
|
|
1182
1441
|
const substitutions = accept("WITH")
|
|
1183
1442
|
? parseSeparated(() => {
|
|
1184
|
-
const
|
|
1443
|
+
const targetStart = cursor.index;
|
|
1444
|
+
const target = expectSubstitutionTarget();
|
|
1445
|
+
const nameRange = getRange(targetStart);
|
|
1185
1446
|
expect("<-");
|
|
1186
|
-
return { name: target, expression: parseArgument() };
|
|
1447
|
+
return { name: target, nameRange, expression: parseArgument() };
|
|
1187
1448
|
})
|
|
1188
1449
|
: [];
|
|
1189
1450
|
return {
|
|
@@ -1199,6 +1460,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1199
1460
|
};
|
|
1200
1461
|
|
|
1201
1462
|
const parseDeclaration = (): Declaration => {
|
|
1463
|
+
const start = cursor.index;
|
|
1464
|
+
return withRange(parseDeclarationUnit(), start);
|
|
1465
|
+
};
|
|
1466
|
+
|
|
1467
|
+
const parseDeclarationUnit = (): Declaration => {
|
|
1202
1468
|
const location = getLocation(peek());
|
|
1203
1469
|
if (isAt("_") && peek(1).kind === "Operator") {
|
|
1204
1470
|
advance();
|
|
@@ -1244,7 +1510,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1244
1510
|
const token = peek();
|
|
1245
1511
|
const next = peek(1);
|
|
1246
1512
|
if (token.kind !== "Identifier") {
|
|
1247
|
-
return
|
|
1513
|
+
return (
|
|
1514
|
+
token.kind !== "String" &&
|
|
1515
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1516
|
+
next.kind === "Identifier" &&
|
|
1517
|
+
isAt("==", 2)
|
|
1518
|
+
);
|
|
1248
1519
|
}
|
|
1249
1520
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1250
1521
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1398,7 +1669,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1398
1669
|
const parseDefs = () => {
|
|
1399
1670
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1400
1671
|
const defs = parseSeparated(parseFact);
|
|
1401
|
-
const invalid =
|
|
1672
|
+
const invalid =
|
|
1673
|
+
mode === "tlc"
|
|
1674
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1675
|
+
: undefined;
|
|
1402
1676
|
if (invalid) {
|
|
1403
1677
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1404
1678
|
}
|
|
@@ -1423,11 +1697,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1423
1697
|
location: getLocation(keyword),
|
|
1424
1698
|
};
|
|
1425
1699
|
const invalid =
|
|
1426
|
-
use.kind === "Hide" &&
|
|
1427
|
-
if (invalid) {
|
|
1428
|
-
throw
|
|
1429
|
-
|
|
1430
|
-
|
|
1700
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1701
|
+
if (invalid.length > 0) {
|
|
1702
|
+
throw createSanyError(
|
|
1703
|
+
invalid.map((fact) => ({
|
|
1704
|
+
location: formatRange(fact.location.range ?? getRange(start)),
|
|
1705
|
+
message:
|
|
1706
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1707
|
+
})),
|
|
1431
1708
|
);
|
|
1432
1709
|
}
|
|
1433
1710
|
return withRange(use, start);
|
|
@@ -1443,10 +1720,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1443
1720
|
}
|
|
1444
1721
|
if (level !== undefined) advance();
|
|
1445
1722
|
if (level === "Variable") {
|
|
1446
|
-
const
|
|
1447
|
-
declareProofSymbol(name);
|
|
1723
|
+
const named = expectName();
|
|
1724
|
+
declareProofSymbol(named.name);
|
|
1448
1725
|
return withRange(
|
|
1449
|
-
{
|
|
1726
|
+
{
|
|
1727
|
+
kind: "NewSymbol",
|
|
1728
|
+
name: named.name,
|
|
1729
|
+
arity: 0,
|
|
1730
|
+
level,
|
|
1731
|
+
set: undefined,
|
|
1732
|
+
nameRange: named.range,
|
|
1733
|
+
location,
|
|
1734
|
+
},
|
|
1450
1735
|
start,
|
|
1451
1736
|
);
|
|
1452
1737
|
}
|
|
@@ -1464,6 +1749,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1464
1749
|
arity: declaration.arity,
|
|
1465
1750
|
level: level ?? "Constant",
|
|
1466
1751
|
set: isBounded ? parseExpression() : undefined,
|
|
1752
|
+
nameRange: declaration.location.range,
|
|
1467
1753
|
location,
|
|
1468
1754
|
},
|
|
1469
1755
|
start,
|
|
@@ -1506,7 +1792,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1506
1792
|
const token = peek();
|
|
1507
1793
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1508
1794
|
if (isAt("INSTANCE")) {
|
|
1509
|
-
|
|
1795
|
+
const start = cursor.index;
|
|
1796
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1510
1797
|
}
|
|
1511
1798
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1512
1799
|
const start = cursor.index;
|
|
@@ -1519,21 +1806,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1519
1806
|
if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
|
|
1520
1807
|
if (accept("TAKE")) {
|
|
1521
1808
|
const isBounded = isBoundAhead();
|
|
1522
|
-
const names = isBounded ? [] : parseSeparated(
|
|
1809
|
+
const names = getNames(isBounded ? [] : parseSeparated(expectName));
|
|
1523
1810
|
const bounds = isBounded ? parseBounds() : [];
|
|
1524
|
-
declareProofSymbols(names, bounds);
|
|
1525
|
-
return { kind: "Take", names, bounds };
|
|
1811
|
+
declareProofSymbols(names.names, bounds);
|
|
1812
|
+
return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
|
|
1526
1813
|
}
|
|
1527
1814
|
if (accept("WITNESS")) {
|
|
1528
1815
|
return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
|
|
1529
1816
|
}
|
|
1530
1817
|
if (accept("PICK")) {
|
|
1531
1818
|
const isNames = isNameListAhead(":");
|
|
1532
|
-
const names = isNames ? parseSeparated(
|
|
1819
|
+
const names = getNames(isNames ? parseSeparated(expectName) : []);
|
|
1533
1820
|
const bounds = isNames ? [] : parseBounds();
|
|
1534
|
-
declareProofSymbols(names, bounds);
|
|
1821
|
+
declareProofSymbols(names.names, bounds);
|
|
1535
1822
|
expect(":");
|
|
1536
|
-
return {
|
|
1823
|
+
return {
|
|
1824
|
+
kind: "Pick",
|
|
1825
|
+
names: names.names,
|
|
1826
|
+
nameRanges: names.ranges,
|
|
1827
|
+
bounds,
|
|
1828
|
+
body: parseExpression(),
|
|
1829
|
+
};
|
|
1537
1830
|
}
|
|
1538
1831
|
if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
|
|
1539
1832
|
return {
|
|
@@ -1563,10 +1856,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1563
1856
|
const parseStep = (): ProofStep => {
|
|
1564
1857
|
const start = cursor.index;
|
|
1565
1858
|
const step = parseStepStart(isAt("QED", 1));
|
|
1859
|
+
const statementStart = cursor.index;
|
|
1566
1860
|
const scope = new Set<string>();
|
|
1567
1861
|
proofScopes.push(scope);
|
|
1568
1862
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1569
1863
|
const head = getRange(start);
|
|
1864
|
+
const statement = getRange(statementStart);
|
|
1570
1865
|
const hasProof = isProofStart(peek());
|
|
1571
1866
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1572
1867
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1574,7 +1869,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1574
1869
|
const proof = hasProof ? parseProof() : undefined;
|
|
1575
1870
|
proofScopes.pop();
|
|
1576
1871
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1577
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1872
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1578
1873
|
};
|
|
1579
1874
|
|
|
1580
1875
|
const parseProofBody = (): Proof => {
|
|
@@ -1609,6 +1904,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1609
1904
|
return withRange(proof, start);
|
|
1610
1905
|
};
|
|
1611
1906
|
|
|
1907
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1908
|
+
// definition, warning of the second.
|
|
1909
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1910
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1911
|
+
module.definitions.push(definition);
|
|
1912
|
+
};
|
|
1913
|
+
|
|
1612
1914
|
const parseTheorem = (module: Module) => {
|
|
1613
1915
|
const start = cursor.index;
|
|
1614
1916
|
const keyword = advance();
|
|
@@ -1630,7 +1932,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1630
1932
|
module.theorems.push(theorem);
|
|
1631
1933
|
module.units.push({ kind: "Theorem", theorem });
|
|
1632
1934
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1633
|
-
|
|
1935
|
+
addFactDefinition(
|
|
1936
|
+
module,
|
|
1634
1937
|
withRange(
|
|
1635
1938
|
{
|
|
1636
1939
|
kind: "Operator",
|
|
@@ -1667,7 +1970,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1667
1970
|
const parseModuleBody = (): Module => {
|
|
1668
1971
|
const location = getLocation(expect("----"));
|
|
1669
1972
|
expect("MODULE");
|
|
1670
|
-
const name =
|
|
1973
|
+
const name = expectModuleName();
|
|
1671
1974
|
expect("----");
|
|
1672
1975
|
const module: Module = {
|
|
1673
1976
|
name,
|
|
@@ -1689,7 +1992,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1689
1992
|
if (accept("EXTENDS")) {
|
|
1690
1993
|
parseSeparated(() => {
|
|
1691
1994
|
const start = cursor.index;
|
|
1692
|
-
module.extends.push(
|
|
1995
|
+
module.extends.push(expectModuleName());
|
|
1693
1996
|
module.extendsRanges.push(getRange(start));
|
|
1694
1997
|
});
|
|
1695
1998
|
}
|
|
@@ -1697,14 +2000,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1697
2000
|
const tokenIndex = cursor.index;
|
|
1698
2001
|
unitStart.index = tokenIndex;
|
|
1699
2002
|
const token = peek();
|
|
1700
|
-
if (token.kind === "ModuleEnd"
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
}
|
|
2003
|
+
if (token.kind === "ModuleEnd") {
|
|
2004
|
+
location.range = {
|
|
2005
|
+
module: openModules[0].name,
|
|
2006
|
+
start: { line: location.line, column: location.column },
|
|
2007
|
+
end: { line: token.line, column: token.endColumn },
|
|
2008
|
+
};
|
|
2009
|
+
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1708
2010
|
openModules.pop();
|
|
1709
2011
|
recursiveScopes.pop();
|
|
1710
2012
|
return module;
|
|
@@ -1723,22 +2025,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1723
2025
|
switch (token.text) {
|
|
1724
2026
|
case "CONSTANT":
|
|
1725
2027
|
case "CONSTANTS": {
|
|
1726
|
-
advance();
|
|
2028
|
+
const unitLocation = getLocation(advance());
|
|
1727
2029
|
const declarations = parseDeclarations();
|
|
1728
2030
|
module.constants.push(...declarations);
|
|
1729
|
-
|
|
2031
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2032
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1730
2033
|
continue;
|
|
1731
2034
|
}
|
|
1732
2035
|
case "VARIABLE":
|
|
1733
2036
|
case "VARIABLES": {
|
|
1734
|
-
advance();
|
|
2037
|
+
const unitLocation = getLocation(advance());
|
|
1735
2038
|
const declarations = parseSeparated(() => {
|
|
1736
2039
|
const start = cursor.index;
|
|
1737
2040
|
const location = getLocation(peek());
|
|
1738
2041
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1739
2042
|
});
|
|
1740
2043
|
module.variables.push(...declarations);
|
|
1741
|
-
|
|
2044
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2045
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1742
2046
|
continue;
|
|
1743
2047
|
}
|
|
1744
2048
|
case "ASSUME":
|
|
@@ -1748,7 +2052,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1748
2052
|
module.assumptions.push(assumption);
|
|
1749
2053
|
module.units.push({ kind: "Assumption", assumption });
|
|
1750
2054
|
if (assumption.name !== undefined) {
|
|
1751
|
-
module
|
|
2055
|
+
addFactDefinition(module, {
|
|
1752
2056
|
kind: "Operator",
|
|
1753
2057
|
name: assumption.name,
|
|
1754
2058
|
params: [],
|
|
@@ -1762,6 +2066,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1762
2066
|
}
|
|
1763
2067
|
case "USE":
|
|
1764
2068
|
case "HIDE": {
|
|
2069
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2070
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2071
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2072
|
+
}
|
|
1765
2073
|
const use = parseUseOrHide();
|
|
1766
2074
|
module.uses.push(use);
|
|
1767
2075
|
module.units.push({ kind: "Use", use });
|
|
@@ -1786,7 +2094,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1786
2094
|
);
|
|
1787
2095
|
continue;
|
|
1788
2096
|
}
|
|
1789
|
-
addDefinitionUnit(module,
|
|
2097
|
+
addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
|
|
1790
2098
|
continue;
|
|
1791
2099
|
}
|
|
1792
2100
|
}
|
|
@@ -1808,9 +2116,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1808
2116
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1809
2117
|
};
|
|
1810
2118
|
|
|
1811
|
-
|
|
2119
|
+
const module = parseModuleBody();
|
|
2120
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2121
|
+
// module's name from a keyword that the parser would take for one.
|
|
2122
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2123
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2124
|
+
return module;
|
|
1812
2125
|
};
|
|
1813
2126
|
|
|
1814
|
-
export const parseModule = (source: string) => parse(source,
|
|
2127
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2128
|
+
|
|
2129
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1815
2130
|
|
|
1816
|
-
export const
|
|
2131
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|