tla-ts 0.0.3-dev.d9ff6f2 → 0.0.3-dev.e8bef80
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 +166 -40
- package/dist/index.d.ts +166 -40
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -22
- package/dist/run-cli.js +1351 -22
- package/dist/transfer.cjs +83 -34
- package/dist/transfer.js +83 -34
- package/package.json +5 -1
- package/src/ast.ts +55 -7
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1331 -367
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +226 -207
- package/src/environment.ts +423 -127
- package/src/errors.ts +168 -9
- package/src/evaluate.ts +720 -177
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +149 -31
- package/src/lexer.ts +122 -29
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +428 -141
- 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 +1436 -459
- package/src/run-cli.ts +1166 -181
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -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 +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +161 -167
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +680 -255
package/src/parser.ts
CHANGED
|
@@ -23,14 +23,29 @@ import type {
|
|
|
23
23
|
UseOrHide,
|
|
24
24
|
} from "./ast";
|
|
25
25
|
import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
|
|
26
|
-
import { getNumeralValue, tokenize, type Token } from "./lexer";
|
|
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,33 @@ 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
|
-
//
|
|
317
|
-
const factStart = { index: -1 };
|
|
359
|
+
// SANY reports a location in a nested module as one in the module of its file.
|
|
318
360
|
const openModules: Module[] = [];
|
|
319
361
|
|
|
320
362
|
const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
|
|
@@ -329,15 +371,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
329
371
|
const tokenEnds: SourceLocation[] = [];
|
|
330
372
|
const tokenRanges: SourceRange[] = [];
|
|
331
373
|
|
|
332
|
-
const getRange = (startIndex: number) => {
|
|
333
|
-
const endIndex = cursor.index - 1;
|
|
374
|
+
const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
|
|
334
375
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
335
376
|
if (cached) return cached;
|
|
336
377
|
const last = tokens[endIndex];
|
|
337
378
|
tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
|
|
338
379
|
tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
|
|
339
380
|
const range = {
|
|
340
|
-
module: openModules[
|
|
381
|
+
module: openModules[0].name,
|
|
341
382
|
start: tokenStarts[startIndex],
|
|
342
383
|
end: tokenEnds[endIndex],
|
|
343
384
|
};
|
|
@@ -435,8 +476,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
435
476
|
const createUnexpected = (message: string, token: Token = peekRaw()) =>
|
|
436
477
|
createError(message, token, token);
|
|
437
478
|
|
|
479
|
+
// TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
|
|
438
480
|
const isFieldName = (token: Token) =>
|
|
439
|
-
(token.kind === "Identifier" ||
|
|
481
|
+
(token.kind === "Identifier" ||
|
|
482
|
+
token.kind === "Keyword" ||
|
|
483
|
+
(token.kind === "Operator" && token.text === "_")) &&
|
|
484
|
+
!isTlapmKeyword(token);
|
|
440
485
|
|
|
441
486
|
const isAt = (text: string, offset = 0) => {
|
|
442
487
|
const token = peek(offset);
|
|
@@ -472,7 +517,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
472
517
|
return token.text;
|
|
473
518
|
};
|
|
474
519
|
|
|
475
|
-
const
|
|
520
|
+
const expectName = () => {
|
|
521
|
+
const start = cursor.index;
|
|
522
|
+
return { name: expectIdentifier(), range: getRange(start) };
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const getNames = (names: { name: string; range: SourceRange }[]) => ({
|
|
526
|
+
names: names.map((item) => item.name),
|
|
527
|
+
ranges: names.map((item) => item.range),
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
// TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
|
|
531
|
+
const expectSubstitutionTarget = () => {
|
|
532
|
+
const token = peek();
|
|
533
|
+
const isOperator =
|
|
534
|
+
token.kind !== "String" &&
|
|
535
|
+
(DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
|
|
536
|
+
INFIX_OPERATORS.has(token.text) ||
|
|
537
|
+
POSTFIX_OPERATORS.has(token.text) ||
|
|
538
|
+
token.text === "'");
|
|
539
|
+
if (!isOperator) return expectIdentifier();
|
|
540
|
+
cursor.index++;
|
|
541
|
+
return token.text;
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// SANY takes a keyword for the name of a module, as it does for a field.
|
|
545
|
+
const keywordModuleName = { isUsed: false };
|
|
546
|
+
const expectModuleName = () => {
|
|
547
|
+
if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
|
|
548
|
+
keywordModuleName.isUsed = true;
|
|
549
|
+
return advance().text;
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
// tlapm reads a bracketed expression apart from the junction list around it, whose alignment
|
|
553
|
+
// SANY keeps applying.
|
|
554
|
+
const withBracketLayout = <Result>(parse: () => Result) => {
|
|
555
|
+
if (!isTlapm) return parse();
|
|
476
556
|
cursor.offsideColumns.push(NO_OFFSIDE);
|
|
477
557
|
const result = parse();
|
|
478
558
|
cursor.offsideColumns.pop();
|
|
@@ -514,10 +594,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
514
594
|
};
|
|
515
595
|
|
|
516
596
|
const parsePattern = (): BoundPattern => {
|
|
517
|
-
if (!accept("<<"))
|
|
518
|
-
|
|
597
|
+
if (!accept("<<")) {
|
|
598
|
+
const named = expectName();
|
|
599
|
+
return { kind: "Name", name: named.name, ranges: [named.range] };
|
|
600
|
+
}
|
|
601
|
+
const names = getNames(parseSeparated(expectName));
|
|
519
602
|
expect(">>");
|
|
520
|
-
return { kind: "Tuple", names };
|
|
603
|
+
return { kind: "Tuple", names: names.names, ranges: names.ranges };
|
|
521
604
|
};
|
|
522
605
|
|
|
523
606
|
const parseBounds = () =>
|
|
@@ -560,8 +643,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
560
643
|
);
|
|
561
644
|
};
|
|
562
645
|
|
|
646
|
+
// SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
|
|
647
|
+
// the start of an expression, such as -.(1), otherwise.
|
|
563
648
|
const isStandalone = (operator: Token) => {
|
|
564
|
-
if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
|
|
565
649
|
const token = peek(1);
|
|
566
650
|
return (
|
|
567
651
|
token.kind === "End" ||
|
|
@@ -600,7 +684,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
600
684
|
if (isOperatorAlone) {
|
|
601
685
|
cursor.index++;
|
|
602
686
|
return withRange(
|
|
603
|
-
{
|
|
687
|
+
{
|
|
688
|
+
kind: "OperatorReference",
|
|
689
|
+
operator: token.text,
|
|
690
|
+
spelling: token.spelling,
|
|
691
|
+
location: getLocation(token),
|
|
692
|
+
},
|
|
604
693
|
cursor.index - 1,
|
|
605
694
|
);
|
|
606
695
|
}
|
|
@@ -610,23 +699,39 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
610
699
|
cursor.index++;
|
|
611
700
|
const location = getLocation(token);
|
|
612
701
|
if (POSTFIX_OPERATORS.has(token.text)) {
|
|
613
|
-
return
|
|
702
|
+
return withRange(
|
|
703
|
+
{ kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
|
|
704
|
+
cursor.index - 1,
|
|
705
|
+
);
|
|
614
706
|
}
|
|
615
707
|
const argument: Expression = { kind: "Identifier", name: "argument", location };
|
|
616
|
-
return
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
708
|
+
return withRange(
|
|
709
|
+
{
|
|
710
|
+
kind: "Lambda",
|
|
711
|
+
params: [argument.name],
|
|
712
|
+
body: getOperatorApplication(token.text, argument, location),
|
|
713
|
+
operator: token.text,
|
|
714
|
+
location: getLocation(token),
|
|
715
|
+
},
|
|
716
|
+
cursor.index - 1,
|
|
717
|
+
);
|
|
622
718
|
}
|
|
623
719
|
if (isAt("LAMBDA")) {
|
|
624
720
|
const start = cursor.index;
|
|
625
721
|
advance();
|
|
626
|
-
const params = parseSeparated(
|
|
722
|
+
const params = getNames(parseSeparated(expectName));
|
|
627
723
|
expect(":");
|
|
628
724
|
const body = parseExpression();
|
|
629
|
-
return withRange(
|
|
725
|
+
return withRange(
|
|
726
|
+
{
|
|
727
|
+
kind: "Lambda",
|
|
728
|
+
params: params.names,
|
|
729
|
+
paramRanges: params.ranges,
|
|
730
|
+
body,
|
|
731
|
+
location: getLocation(token),
|
|
732
|
+
},
|
|
733
|
+
start,
|
|
734
|
+
);
|
|
630
735
|
}
|
|
631
736
|
return parseExpression();
|
|
632
737
|
};
|
|
@@ -634,7 +739,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
634
739
|
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
635
740
|
|
|
636
741
|
const parseArguments = () =>
|
|
637
|
-
|
|
742
|
+
withBracketLayout(() => {
|
|
638
743
|
const start = cursor.index;
|
|
639
744
|
expect("(");
|
|
640
745
|
const args = parseSeparated(parseArgument);
|
|
@@ -661,18 +766,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
661
766
|
};
|
|
662
767
|
|
|
663
768
|
const parseBracket = (start: Token): Expression =>
|
|
664
|
-
|
|
769
|
+
withBracketLayout(() => {
|
|
665
770
|
const location = getLocation(start);
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
771
|
+
// SANY takes a keyword for the first field of a record, as it does for a selected field.
|
|
772
|
+
const isIdentifier = peek().kind === "Identifier";
|
|
773
|
+
const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
|
|
774
|
+
if (isRecord || (isIdentifier && isAt(":", 1))) {
|
|
775
|
+
const kind = isRecord ? "Record" : "RecordSet";
|
|
776
|
+
const parseField = (readName: () => string) => {
|
|
669
777
|
const fieldStart = cursor.index;
|
|
670
778
|
const fieldLocation = getLocation(peek());
|
|
671
|
-
const name =
|
|
779
|
+
const name = readName();
|
|
672
780
|
fieldLocation.range = getRange(fieldStart);
|
|
673
|
-
expect(
|
|
781
|
+
expect(isRecord ? "|->" : ":");
|
|
674
782
|
return { name, value: parseExpression(), location: fieldLocation };
|
|
675
|
-
}
|
|
783
|
+
};
|
|
784
|
+
const fields = [
|
|
785
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
786
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
787
|
+
];
|
|
676
788
|
expect("]");
|
|
677
789
|
return { kind, fields, location };
|
|
678
790
|
}
|
|
@@ -710,17 +822,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
710
822
|
expect("]");
|
|
711
823
|
return { kind: "Except", function: expression, updates, location };
|
|
712
824
|
}
|
|
825
|
+
if (!isTlapm && !isAt("]_")) {
|
|
826
|
+
const args = [expression];
|
|
827
|
+
while (accept(",")) args.push(parseExpression());
|
|
828
|
+
expect("]");
|
|
829
|
+
return { kind: "Bracket", args, location };
|
|
830
|
+
}
|
|
713
831
|
expect("]_");
|
|
714
|
-
return {
|
|
715
|
-
kind: "BoxAction",
|
|
716
|
-
action: expression,
|
|
717
|
-
subscript: parseSubscript(),
|
|
718
|
-
location,
|
|
719
|
-
};
|
|
832
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
720
833
|
});
|
|
721
834
|
|
|
835
|
+
// SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
|
|
836
|
+
const parseSetMembership = (): Expression => {
|
|
837
|
+
const start = cursor.index;
|
|
838
|
+
const element = withRange(parsePrefix(), start);
|
|
839
|
+
const location = getLocation(advance());
|
|
840
|
+
return withRange(
|
|
841
|
+
{ kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
|
|
842
|
+
start,
|
|
843
|
+
);
|
|
844
|
+
};
|
|
845
|
+
|
|
722
846
|
const parseBrace = (start: Token): Expression =>
|
|
723
|
-
|
|
847
|
+
withBracketLayout(() => {
|
|
724
848
|
const location = getLocation(start);
|
|
725
849
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
726
850
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -735,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
735
859
|
}
|
|
736
860
|
cursor.index = checkpoint;
|
|
737
861
|
}
|
|
738
|
-
const first =
|
|
862
|
+
const first =
|
|
863
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
739
864
|
if (accept(":")) {
|
|
740
865
|
const bounds = parseBounds();
|
|
741
866
|
expect("}");
|
|
@@ -749,7 +874,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
749
874
|
|
|
750
875
|
const parseTuple = (start: Token): Expression => {
|
|
751
876
|
const location = getLocation(start);
|
|
752
|
-
const elements =
|
|
877
|
+
const elements = withBracketLayout(() =>
|
|
753
878
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
754
879
|
);
|
|
755
880
|
if (accept(">>_")) {
|
|
@@ -770,12 +895,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
770
895
|
expect(":");
|
|
771
896
|
return { kind, bounds, body: parseExpression(), location };
|
|
772
897
|
}
|
|
773
|
-
const names = parseSeparated(
|
|
898
|
+
const names = getNames(parseSeparated(expectName));
|
|
774
899
|
expect(":");
|
|
775
900
|
return {
|
|
776
901
|
kind: "UnboundedQuantifier",
|
|
777
902
|
quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
|
|
778
|
-
names,
|
|
903
|
+
names: names.names,
|
|
904
|
+
nameRanges: names.ranges,
|
|
779
905
|
body: parseExpression(),
|
|
780
906
|
isTemporal,
|
|
781
907
|
location,
|
|
@@ -829,8 +955,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
829
955
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
830
956
|
};
|
|
831
957
|
|
|
832
|
-
const parsePrimary = (): Expression => {
|
|
833
|
-
const
|
|
958
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
959
|
+
const start = cursor.index;
|
|
834
960
|
const token = advance();
|
|
835
961
|
const location = getLocation(token);
|
|
836
962
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -854,54 +980,100 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
854
980
|
if (token.kind === "Identifier" || isStep) {
|
|
855
981
|
const name = isStep ? (getStepName(token) ?? token.text) : token.text;
|
|
856
982
|
if (isLabelAhead()) {
|
|
857
|
-
const params = accept("(") ? parseSeparated(
|
|
858
|
-
if (params.length > 0) expect(")");
|
|
983
|
+
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
984
|
+
if (params.names.length > 0) expect(")");
|
|
859
985
|
expect("::");
|
|
860
|
-
|
|
986
|
+
const body = parseExpression();
|
|
987
|
+
// TLAplusParser.labelDoesNotChangeParse.
|
|
988
|
+
if (
|
|
989
|
+
!isTlapm &&
|
|
990
|
+
preceding !== undefined &&
|
|
991
|
+
operators.last !== undefined &&
|
|
992
|
+
isOperatorExpression(body) &&
|
|
993
|
+
preceding.high >= operators.last.low
|
|
994
|
+
) {
|
|
995
|
+
throw createError("Removing the label would change expression parsing", token);
|
|
996
|
+
}
|
|
997
|
+
return {
|
|
998
|
+
kind: "Label",
|
|
999
|
+
name,
|
|
1000
|
+
params: params.names,
|
|
1001
|
+
paramRanges: params.ranges,
|
|
1002
|
+
body,
|
|
1003
|
+
location,
|
|
1004
|
+
};
|
|
861
1005
|
}
|
|
862
1006
|
const hasArguments = isAt("(");
|
|
1007
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1008
|
+
return { kind: "ExceptAt", location };
|
|
863
1009
|
const args = hasArguments ? parseArguments() : [];
|
|
1010
|
+
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
864
1011
|
if (!isSelectorAhead()) {
|
|
865
1012
|
return hasArguments
|
|
866
1013
|
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
867
1014
|
: { kind: "Identifier", name, location };
|
|
868
1015
|
}
|
|
869
|
-
const instance = openModules
|
|
870
|
-
.flatMap((module) => module.instances)
|
|
871
|
-
.find((candidate) => candidate.name === name);
|
|
872
|
-
if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
|
|
873
|
-
throw createError(
|
|
874
|
-
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
875
|
-
token,
|
|
876
|
-
);
|
|
877
|
-
}
|
|
878
1016
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1017
|
+
const selectorRanges = [getRange(start, start)];
|
|
879
1018
|
while (isSelectorAhead()) {
|
|
880
1019
|
advance();
|
|
1020
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
1021
|
+
lastArguments.range = undefined;
|
|
881
1022
|
selectors.push(parseSelector());
|
|
1023
|
+
argumentRanges.push(lastArguments.range);
|
|
882
1024
|
}
|
|
883
1025
|
const names = selectors.flatMap((selector) =>
|
|
884
1026
|
selector.kind === "Name" ? [selector.name] : [],
|
|
885
1027
|
);
|
|
886
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
887
1028
|
const operator = names.join("!");
|
|
888
1029
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
889
1030
|
"args" in selector ? selector.args : [],
|
|
890
1031
|
);
|
|
891
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
892
1032
|
const argumentCounts = selectors.map((selector) =>
|
|
893
1033
|
"args" in selector ? selector.args.length : 0,
|
|
894
1034
|
);
|
|
895
|
-
const argumentsRange =
|
|
896
|
-
return
|
|
897
|
-
? { kind: "
|
|
898
|
-
:
|
|
1035
|
+
const argumentsRange = argumentRanges.at(-1) ?? getRange(start);
|
|
1036
|
+
return names.length < selectors.length
|
|
1037
|
+
? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
|
|
1038
|
+
: qualifiedArgs.length === 0
|
|
1039
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
1040
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
1041
|
+
? {
|
|
1042
|
+
kind: "Apply",
|
|
1043
|
+
operator,
|
|
1044
|
+
args: qualifiedArgs,
|
|
1045
|
+
argumentCounts,
|
|
1046
|
+
argumentsRange,
|
|
1047
|
+
selectorRanges,
|
|
1048
|
+
argumentRanges,
|
|
1049
|
+
location,
|
|
1050
|
+
}
|
|
1051
|
+
: {
|
|
1052
|
+
kind: "Apply",
|
|
1053
|
+
operator,
|
|
1054
|
+
args: qualifiedArgs,
|
|
1055
|
+
argumentsRange,
|
|
1056
|
+
selectorRanges,
|
|
1057
|
+
argumentRanges,
|
|
1058
|
+
location,
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1062
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
1063
|
+
return {
|
|
1064
|
+
kind: "Apply",
|
|
1065
|
+
operator: token.text,
|
|
1066
|
+
spelling: token.spelling,
|
|
1067
|
+
args: parseArguments(),
|
|
1068
|
+
location,
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1072
|
+
return { kind: "Identifier", name: token.text, spelling: token.spelling, location };
|
|
899
1073
|
}
|
|
900
|
-
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
901
|
-
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
902
1074
|
switch (token.text) {
|
|
903
1075
|
case "(": {
|
|
904
|
-
const expression =
|
|
1076
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
905
1077
|
expect(")");
|
|
906
1078
|
return expression;
|
|
907
1079
|
}
|
|
@@ -957,7 +1129,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
957
1129
|
case "WF_":
|
|
958
1130
|
case "SF_": {
|
|
959
1131
|
const subscript = parseSubscript();
|
|
960
|
-
const action =
|
|
1132
|
+
const action = withBracketLayout(() => {
|
|
961
1133
|
expect("(");
|
|
962
1134
|
const expression = parseExpression();
|
|
963
1135
|
expect(")");
|
|
@@ -974,7 +1146,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
974
1146
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
975
1147
|
};
|
|
976
1148
|
|
|
1149
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1150
|
+
// prefix operator.
|
|
1151
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1152
|
+
operators.last = operators.preceding = { low, high };
|
|
1153
|
+
const operand = parseExpression(high + 1);
|
|
1154
|
+
const next = peek();
|
|
1155
|
+
const nextHigh =
|
|
1156
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1157
|
+
? POSTFIX_PRECEDENCE.high
|
|
1158
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1159
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1160
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1161
|
+
}
|
|
1162
|
+
return operand;
|
|
1163
|
+
};
|
|
1164
|
+
|
|
977
1165
|
const parsePrefix = (): Expression => {
|
|
1166
|
+
const preceding = operators.preceding;
|
|
1167
|
+
operators.preceding = undefined;
|
|
978
1168
|
const token = peek();
|
|
979
1169
|
const location = getLocation(token);
|
|
980
1170
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -984,35 +1174,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
984
1174
|
switch (token.text) {
|
|
985
1175
|
case "~":
|
|
986
1176
|
advance();
|
|
987
|
-
return {
|
|
1177
|
+
return {
|
|
1178
|
+
kind: "Apply",
|
|
1179
|
+
operator: "~",
|
|
1180
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1181
|
+
location,
|
|
1182
|
+
};
|
|
988
1183
|
case "-":
|
|
989
1184
|
advance();
|
|
990
1185
|
return {
|
|
991
1186
|
kind: "Apply",
|
|
992
1187
|
operator: "-.",
|
|
993
|
-
args: [
|
|
1188
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
994
1189
|
location,
|
|
995
1190
|
};
|
|
996
1191
|
case "[]":
|
|
997
1192
|
advance();
|
|
998
|
-
return { kind: "Always", expression:
|
|
1193
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
999
1194
|
case "<>":
|
|
1000
1195
|
advance();
|
|
1001
|
-
return { kind: "Eventually", expression:
|
|
1196
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1002
1197
|
case "ENABLED":
|
|
1003
1198
|
advance();
|
|
1004
|
-
return { kind: "Enabled", expression:
|
|
1199
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1005
1200
|
case "UNCHANGED":
|
|
1006
1201
|
advance();
|
|
1007
|
-
return { kind: "Unchanged", expression:
|
|
1202
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1008
1203
|
case "SUBSET":
|
|
1009
1204
|
case "UNION":
|
|
1010
1205
|
case "DOMAIN":
|
|
1011
1206
|
advance();
|
|
1012
|
-
return {
|
|
1207
|
+
return {
|
|
1208
|
+
kind: "Apply",
|
|
1209
|
+
operator: token.text,
|
|
1210
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1211
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1212
|
+
location,
|
|
1213
|
+
};
|
|
1013
1214
|
}
|
|
1014
1215
|
}
|
|
1015
|
-
return parsePrimary();
|
|
1216
|
+
return parsePrimary(preceding);
|
|
1016
1217
|
};
|
|
1017
1218
|
|
|
1018
1219
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1028,21 +1229,30 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1028
1229
|
const location = getLocation(token);
|
|
1029
1230
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1030
1231
|
advance();
|
|
1232
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1031
1233
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1032
1234
|
continue;
|
|
1033
1235
|
}
|
|
1034
1236
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1035
1237
|
advance();
|
|
1238
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1036
1239
|
state.left = withRange(
|
|
1037
|
-
{
|
|
1240
|
+
{
|
|
1241
|
+
kind: "Apply",
|
|
1242
|
+
operator: token.text,
|
|
1243
|
+
spelling: token.spelling,
|
|
1244
|
+
args: [state.left],
|
|
1245
|
+
location,
|
|
1246
|
+
},
|
|
1038
1247
|
start,
|
|
1039
1248
|
);
|
|
1040
1249
|
continue;
|
|
1041
1250
|
}
|
|
1042
1251
|
if (token.text === "[") {
|
|
1043
1252
|
advance();
|
|
1044
|
-
const args =
|
|
1045
|
-
|
|
1253
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1254
|
+
withBracketLayout(() => expect("]"));
|
|
1255
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1046
1256
|
state.left = withRange(
|
|
1047
1257
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1048
1258
|
start,
|
|
@@ -1073,21 +1283,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1073
1283
|
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1074
1284
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1075
1285
|
}
|
|
1286
|
+
operators.last = operators.preceding = infix;
|
|
1076
1287
|
const right = parseExpression(infix.high + 1);
|
|
1077
1288
|
const left = state.left;
|
|
1078
1289
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1079
1290
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1291
|
+
const prefixEnds = [getRange(start).end];
|
|
1080
1292
|
state.left =
|
|
1081
1293
|
left.kind === kind && previous?.text === token.text
|
|
1082
|
-
? {
|
|
1083
|
-
|
|
1294
|
+
? {
|
|
1295
|
+
...left,
|
|
1296
|
+
operands: [...left.operands, right],
|
|
1297
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1298
|
+
}
|
|
1299
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1084
1300
|
} else if (token.text === "\\X") {
|
|
1085
1301
|
state.left =
|
|
1086
1302
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1087
1303
|
? { ...left, factors: [...left.factors, right] }
|
|
1088
1304
|
: { kind: "Product", factors: [left, right], location };
|
|
1089
1305
|
} else {
|
|
1090
|
-
state.left = {
|
|
1306
|
+
state.left = {
|
|
1307
|
+
kind: "Apply",
|
|
1308
|
+
operator: token.text,
|
|
1309
|
+
spelling: token.spelling,
|
|
1310
|
+
args: [left, right],
|
|
1311
|
+
location,
|
|
1312
|
+
};
|
|
1091
1313
|
}
|
|
1092
1314
|
state.left = withRange(state.left, start);
|
|
1093
1315
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1107,7 +1329,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1107
1329
|
const body = parseExpression();
|
|
1108
1330
|
const last = tokens[cursor.index - 1];
|
|
1109
1331
|
const end = { line: last.line, column: last.endColumn };
|
|
1110
|
-
return { body, bodyRange: { module: openModules[
|
|
1332
|
+
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1111
1333
|
};
|
|
1112
1334
|
|
|
1113
1335
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
@@ -1115,6 +1337,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1115
1337
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1116
1338
|
};
|
|
1117
1339
|
|
|
1340
|
+
const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
|
|
1341
|
+
name: named.name,
|
|
1342
|
+
arity: 0,
|
|
1343
|
+
location: {
|
|
1344
|
+
line: named.range.start.line,
|
|
1345
|
+
column: named.range.start.column,
|
|
1346
|
+
range: named.range,
|
|
1347
|
+
},
|
|
1348
|
+
});
|
|
1349
|
+
|
|
1118
1350
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1119
1351
|
const token = peek();
|
|
1120
1352
|
const location = getLocation(token);
|
|
@@ -1134,28 +1366,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1134
1366
|
peek(2).kind === "Identifier" &&
|
|
1135
1367
|
isAt("==", 3)
|
|
1136
1368
|
) {
|
|
1137
|
-
const left =
|
|
1369
|
+
const left = expectName();
|
|
1138
1370
|
const operator = advance().text;
|
|
1139
|
-
const right =
|
|
1371
|
+
const right = expectName();
|
|
1140
1372
|
expect("==");
|
|
1141
|
-
return parseOperator(operator, [
|
|
1142
|
-
{ name: left, arity: 0 },
|
|
1143
|
-
{ name: right, arity: 0 },
|
|
1144
|
-
]);
|
|
1373
|
+
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1145
1374
|
}
|
|
1146
|
-
const isPrefixDefinition =
|
|
1375
|
+
const isPrefixDefinition =
|
|
1376
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1147
1377
|
const isPostfixDefinition =
|
|
1148
1378
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1149
1379
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
1150
1380
|
const operator = isPrefixDefinition ? token.text : peek(1).text;
|
|
1151
|
-
|
|
1152
|
-
const param =
|
|
1381
|
+
if (isPrefixDefinition) advance();
|
|
1382
|
+
const param = expectName();
|
|
1383
|
+
if (isPostfixDefinition) advance();
|
|
1153
1384
|
expect("==");
|
|
1154
|
-
return parseOperator(operator, [
|
|
1385
|
+
return parseOperator(operator, [toParam(param)]);
|
|
1155
1386
|
}
|
|
1156
1387
|
const name = expectIdentifier();
|
|
1157
1388
|
if (accept("[")) {
|
|
1158
|
-
const bounds =
|
|
1389
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1159
1390
|
expect("]");
|
|
1160
1391
|
expect("==");
|
|
1161
1392
|
return {
|
|
@@ -1186,12 +1417,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1186
1417
|
): Instance => {
|
|
1187
1418
|
const start = cursor.index;
|
|
1188
1419
|
expect("INSTANCE");
|
|
1189
|
-
const module =
|
|
1420
|
+
const module = expectModuleName();
|
|
1190
1421
|
const substitutions = accept("WITH")
|
|
1191
1422
|
? parseSeparated(() => {
|
|
1192
|
-
const
|
|
1423
|
+
const targetStart = cursor.index;
|
|
1424
|
+
const target = expectSubstitutionTarget();
|
|
1425
|
+
const nameRange = getRange(targetStart);
|
|
1193
1426
|
expect("<-");
|
|
1194
|
-
return { name: target, expression: parseArgument() };
|
|
1427
|
+
return { name: target, nameRange, expression: parseArgument() };
|
|
1195
1428
|
})
|
|
1196
1429
|
: [];
|
|
1197
1430
|
return {
|
|
@@ -1207,6 +1440,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1207
1440
|
};
|
|
1208
1441
|
|
|
1209
1442
|
const parseDeclaration = (): Declaration => {
|
|
1443
|
+
const start = cursor.index;
|
|
1444
|
+
return withRange(parseDeclarationUnit(), start);
|
|
1445
|
+
};
|
|
1446
|
+
|
|
1447
|
+
const parseDeclarationUnit = (): Declaration => {
|
|
1210
1448
|
const location = getLocation(peek());
|
|
1211
1449
|
if (isAt("_") && peek(1).kind === "Operator") {
|
|
1212
1450
|
advance();
|
|
@@ -1252,7 +1490,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1252
1490
|
const token = peek();
|
|
1253
1491
|
const next = peek(1);
|
|
1254
1492
|
if (token.kind !== "Identifier") {
|
|
1255
|
-
return
|
|
1493
|
+
return (
|
|
1494
|
+
token.kind !== "String" &&
|
|
1495
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1496
|
+
next.kind === "Identifier" &&
|
|
1497
|
+
isAt("==", 2)
|
|
1498
|
+
);
|
|
1256
1499
|
}
|
|
1257
1500
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1258
1501
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1344,7 +1587,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1344
1587
|
// start of an expression.
|
|
1345
1588
|
const parseFact = (): Fact => {
|
|
1346
1589
|
const start = cursor.index;
|
|
1347
|
-
factStart.index = start;
|
|
1348
1590
|
const token = peek();
|
|
1349
1591
|
if (
|
|
1350
1592
|
token.kind === "Operator" &&
|
|
@@ -1353,7 +1595,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1353
1595
|
) {
|
|
1354
1596
|
cursor.index++;
|
|
1355
1597
|
return withRange(
|
|
1356
|
-
{
|
|
1598
|
+
{
|
|
1599
|
+
kind: "OperatorReference",
|
|
1600
|
+
operator: token.text,
|
|
1601
|
+
spelling: token.spelling,
|
|
1602
|
+
location: getLocation(token),
|
|
1603
|
+
},
|
|
1357
1604
|
start,
|
|
1358
1605
|
);
|
|
1359
1606
|
}
|
|
@@ -1406,7 +1653,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1406
1653
|
const parseDefs = () => {
|
|
1407
1654
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1408
1655
|
const defs = parseSeparated(parseFact);
|
|
1409
|
-
const invalid =
|
|
1656
|
+
const invalid =
|
|
1657
|
+
mode === "tlc"
|
|
1658
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1659
|
+
: undefined;
|
|
1410
1660
|
if (invalid) {
|
|
1411
1661
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1412
1662
|
}
|
|
@@ -1431,7 +1681,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1431
1681
|
location: getLocation(keyword),
|
|
1432
1682
|
};
|
|
1433
1683
|
const invalid =
|
|
1434
|
-
use.kind === "Hide" &&
|
|
1684
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1435
1685
|
if (invalid.length > 0) {
|
|
1436
1686
|
throw createSanyError(
|
|
1437
1687
|
invalid.map((fact) => ({
|
|
@@ -1454,10 +1704,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1454
1704
|
}
|
|
1455
1705
|
if (level !== undefined) advance();
|
|
1456
1706
|
if (level === "Variable") {
|
|
1457
|
-
const
|
|
1458
|
-
declareProofSymbol(name);
|
|
1707
|
+
const named = expectName();
|
|
1708
|
+
declareProofSymbol(named.name);
|
|
1459
1709
|
return withRange(
|
|
1460
|
-
{
|
|
1710
|
+
{
|
|
1711
|
+
kind: "NewSymbol",
|
|
1712
|
+
name: named.name,
|
|
1713
|
+
arity: 0,
|
|
1714
|
+
level,
|
|
1715
|
+
set: undefined,
|
|
1716
|
+
nameRange: named.range,
|
|
1717
|
+
location,
|
|
1718
|
+
},
|
|
1461
1719
|
start,
|
|
1462
1720
|
);
|
|
1463
1721
|
}
|
|
@@ -1475,6 +1733,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1475
1733
|
arity: declaration.arity,
|
|
1476
1734
|
level: level ?? "Constant",
|
|
1477
1735
|
set: isBounded ? parseExpression() : undefined,
|
|
1736
|
+
nameRange: declaration.location.range,
|
|
1478
1737
|
location,
|
|
1479
1738
|
},
|
|
1480
1739
|
start,
|
|
@@ -1517,7 +1776,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1517
1776
|
const token = peek();
|
|
1518
1777
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1519
1778
|
if (isAt("INSTANCE")) {
|
|
1520
|
-
|
|
1779
|
+
const start = cursor.index;
|
|
1780
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1521
1781
|
}
|
|
1522
1782
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1523
1783
|
const start = cursor.index;
|
|
@@ -1530,21 +1790,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1530
1790
|
if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
|
|
1531
1791
|
if (accept("TAKE")) {
|
|
1532
1792
|
const isBounded = isBoundAhead();
|
|
1533
|
-
const names = isBounded ? [] : parseSeparated(
|
|
1793
|
+
const names = getNames(isBounded ? [] : parseSeparated(expectName));
|
|
1534
1794
|
const bounds = isBounded ? parseBounds() : [];
|
|
1535
|
-
declareProofSymbols(names, bounds);
|
|
1536
|
-
return { kind: "Take", names, bounds };
|
|
1795
|
+
declareProofSymbols(names.names, bounds);
|
|
1796
|
+
return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
|
|
1537
1797
|
}
|
|
1538
1798
|
if (accept("WITNESS")) {
|
|
1539
1799
|
return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
|
|
1540
1800
|
}
|
|
1541
1801
|
if (accept("PICK")) {
|
|
1542
1802
|
const isNames = isNameListAhead(":");
|
|
1543
|
-
const names = isNames ? parseSeparated(
|
|
1803
|
+
const names = getNames(isNames ? parseSeparated(expectName) : []);
|
|
1544
1804
|
const bounds = isNames ? [] : parseBounds();
|
|
1545
|
-
declareProofSymbols(names, bounds);
|
|
1805
|
+
declareProofSymbols(names.names, bounds);
|
|
1546
1806
|
expect(":");
|
|
1547
|
-
return {
|
|
1807
|
+
return {
|
|
1808
|
+
kind: "Pick",
|
|
1809
|
+
names: names.names,
|
|
1810
|
+
nameRanges: names.ranges,
|
|
1811
|
+
bounds,
|
|
1812
|
+
body: parseExpression(),
|
|
1813
|
+
};
|
|
1548
1814
|
}
|
|
1549
1815
|
if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
|
|
1550
1816
|
return {
|
|
@@ -1574,10 +1840,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1574
1840
|
const parseStep = (): ProofStep => {
|
|
1575
1841
|
const start = cursor.index;
|
|
1576
1842
|
const step = parseStepStart(isAt("QED", 1));
|
|
1843
|
+
const statementStart = cursor.index;
|
|
1577
1844
|
const scope = new Set<string>();
|
|
1578
1845
|
proofScopes.push(scope);
|
|
1579
1846
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1580
1847
|
const head = getRange(start);
|
|
1848
|
+
const statement = getRange(statementStart);
|
|
1581
1849
|
const hasProof = isProofStart(peek());
|
|
1582
1850
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1583
1851
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1585,7 +1853,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1585
1853
|
const proof = hasProof ? parseProof() : undefined;
|
|
1586
1854
|
proofScopes.pop();
|
|
1587
1855
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1588
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1856
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1589
1857
|
};
|
|
1590
1858
|
|
|
1591
1859
|
const parseProofBody = (): Proof => {
|
|
@@ -1620,6 +1888,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1620
1888
|
return withRange(proof, start);
|
|
1621
1889
|
};
|
|
1622
1890
|
|
|
1891
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1892
|
+
// definition, warning of the second.
|
|
1893
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1894
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1895
|
+
module.definitions.push(definition);
|
|
1896
|
+
};
|
|
1897
|
+
|
|
1623
1898
|
const parseTheorem = (module: Module) => {
|
|
1624
1899
|
const start = cursor.index;
|
|
1625
1900
|
const keyword = advance();
|
|
@@ -1641,7 +1916,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1641
1916
|
module.theorems.push(theorem);
|
|
1642
1917
|
module.units.push({ kind: "Theorem", theorem });
|
|
1643
1918
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1644
|
-
|
|
1919
|
+
addFactDefinition(
|
|
1920
|
+
module,
|
|
1645
1921
|
withRange(
|
|
1646
1922
|
{
|
|
1647
1923
|
kind: "Operator",
|
|
@@ -1678,7 +1954,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1678
1954
|
const parseModuleBody = (): Module => {
|
|
1679
1955
|
const location = getLocation(expect("----"));
|
|
1680
1956
|
expect("MODULE");
|
|
1681
|
-
const name =
|
|
1957
|
+
const name = expectModuleName();
|
|
1682
1958
|
expect("----");
|
|
1683
1959
|
const module: Module = {
|
|
1684
1960
|
name,
|
|
@@ -1700,7 +1976,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1700
1976
|
if (accept("EXTENDS")) {
|
|
1701
1977
|
parseSeparated(() => {
|
|
1702
1978
|
const start = cursor.index;
|
|
1703
|
-
module.extends.push(
|
|
1979
|
+
module.extends.push(expectModuleName());
|
|
1704
1980
|
module.extendsRanges.push(getRange(start));
|
|
1705
1981
|
});
|
|
1706
1982
|
}
|
|
@@ -1708,14 +1984,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1708
1984
|
const tokenIndex = cursor.index;
|
|
1709
1985
|
unitStart.index = tokenIndex;
|
|
1710
1986
|
const token = peek();
|
|
1711
|
-
if (token.kind === "ModuleEnd"
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
};
|
|
1718
|
-
}
|
|
1987
|
+
if (token.kind === "ModuleEnd") {
|
|
1988
|
+
location.range = {
|
|
1989
|
+
module: openModules[0].name,
|
|
1990
|
+
start: { line: location.line, column: location.column },
|
|
1991
|
+
end: { line: token.line, column: token.endColumn },
|
|
1992
|
+
};
|
|
1719
1993
|
openModules.pop();
|
|
1720
1994
|
recursiveScopes.pop();
|
|
1721
1995
|
return module;
|
|
@@ -1734,22 +2008,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1734
2008
|
switch (token.text) {
|
|
1735
2009
|
case "CONSTANT":
|
|
1736
2010
|
case "CONSTANTS": {
|
|
1737
|
-
advance();
|
|
2011
|
+
const unitLocation = getLocation(advance());
|
|
1738
2012
|
const declarations = parseDeclarations();
|
|
1739
2013
|
module.constants.push(...declarations);
|
|
1740
|
-
|
|
2014
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2015
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1741
2016
|
continue;
|
|
1742
2017
|
}
|
|
1743
2018
|
case "VARIABLE":
|
|
1744
2019
|
case "VARIABLES": {
|
|
1745
|
-
advance();
|
|
2020
|
+
const unitLocation = getLocation(advance());
|
|
1746
2021
|
const declarations = parseSeparated(() => {
|
|
1747
2022
|
const start = cursor.index;
|
|
1748
2023
|
const location = getLocation(peek());
|
|
1749
2024
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1750
2025
|
});
|
|
1751
2026
|
module.variables.push(...declarations);
|
|
1752
|
-
|
|
2027
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2028
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1753
2029
|
continue;
|
|
1754
2030
|
}
|
|
1755
2031
|
case "ASSUME":
|
|
@@ -1759,7 +2035,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1759
2035
|
module.assumptions.push(assumption);
|
|
1760
2036
|
module.units.push({ kind: "Assumption", assumption });
|
|
1761
2037
|
if (assumption.name !== undefined) {
|
|
1762
|
-
module
|
|
2038
|
+
addFactDefinition(module, {
|
|
1763
2039
|
kind: "Operator",
|
|
1764
2040
|
name: assumption.name,
|
|
1765
2041
|
params: [],
|
|
@@ -1773,6 +2049,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1773
2049
|
}
|
|
1774
2050
|
case "USE":
|
|
1775
2051
|
case "HIDE": {
|
|
2052
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2053
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2054
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2055
|
+
}
|
|
1776
2056
|
const use = parseUseOrHide();
|
|
1777
2057
|
module.uses.push(use);
|
|
1778
2058
|
module.units.push({ kind: "Use", use });
|
|
@@ -1797,7 +2077,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1797
2077
|
);
|
|
1798
2078
|
continue;
|
|
1799
2079
|
}
|
|
1800
|
-
addDefinitionUnit(module,
|
|
2080
|
+
addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
|
|
1801
2081
|
continue;
|
|
1802
2082
|
}
|
|
1803
2083
|
}
|
|
@@ -1819,9 +2099,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1819
2099
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1820
2100
|
};
|
|
1821
2101
|
|
|
1822
|
-
|
|
2102
|
+
const module = parseModuleBody();
|
|
2103
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2104
|
+
// module's name from a keyword that the parser would take for one.
|
|
2105
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2106
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2107
|
+
return module;
|
|
1823
2108
|
};
|
|
1824
2109
|
|
|
1825
|
-
export const parseModule = (source: string) => parse(source,
|
|
2110
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2111
|
+
|
|
2112
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1826
2113
|
|
|
1827
|
-
export const
|
|
2114
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|