tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.ccbff8c
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 +167 -36
- package/dist/index.d.ts +167 -36
- 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 -21
- package/dist/run-cli.js +1351 -21
- 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 +1344 -285
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +423 -127
- package/src/errors.ts +168 -9
- package/src/evaluate.ts +722 -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 +412 -139
- 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 +1405 -416
- package/src/run-cli.ts +1167 -174
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +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
|
@@ -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, 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,15 +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
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
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) => {
|
|
777
|
+
const fieldStart = cursor.index;
|
|
778
|
+
const fieldLocation = getLocation(peek());
|
|
779
|
+
const name = readName();
|
|
780
|
+
fieldLocation.range = getRange(fieldStart);
|
|
781
|
+
expect(isRecord ? "|->" : ":");
|
|
782
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
783
|
+
};
|
|
784
|
+
const fields = [
|
|
785
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
786
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
787
|
+
];
|
|
673
788
|
expect("]");
|
|
674
789
|
return { kind, fields, location };
|
|
675
790
|
}
|
|
@@ -707,17 +822,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
707
822
|
expect("]");
|
|
708
823
|
return { kind: "Except", function: expression, updates, location };
|
|
709
824
|
}
|
|
825
|
+
if (!isTlapm && !isAt("]_")) {
|
|
826
|
+
const args = [expression];
|
|
827
|
+
while (accept(",")) args.push(parseExpression());
|
|
828
|
+
expect("]");
|
|
829
|
+
return { kind: "Bracket", args, location };
|
|
830
|
+
}
|
|
710
831
|
expect("]_");
|
|
711
|
-
return {
|
|
712
|
-
kind: "BoxAction",
|
|
713
|
-
action: expression,
|
|
714
|
-
subscript: parseSubscript(),
|
|
715
|
-
location,
|
|
716
|
-
};
|
|
832
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
717
833
|
});
|
|
718
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
|
+
|
|
719
846
|
const parseBrace = (start: Token): Expression =>
|
|
720
|
-
|
|
847
|
+
withBracketLayout(() => {
|
|
721
848
|
const location = getLocation(start);
|
|
722
849
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
723
850
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -732,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
732
859
|
}
|
|
733
860
|
cursor.index = checkpoint;
|
|
734
861
|
}
|
|
735
|
-
const first =
|
|
862
|
+
const first =
|
|
863
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
736
864
|
if (accept(":")) {
|
|
737
865
|
const bounds = parseBounds();
|
|
738
866
|
expect("}");
|
|
@@ -746,7 +874,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
746
874
|
|
|
747
875
|
const parseTuple = (start: Token): Expression => {
|
|
748
876
|
const location = getLocation(start);
|
|
749
|
-
const elements =
|
|
877
|
+
const elements = withBracketLayout(() =>
|
|
750
878
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
751
879
|
);
|
|
752
880
|
if (accept(">>_")) {
|
|
@@ -767,12 +895,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
767
895
|
expect(":");
|
|
768
896
|
return { kind, bounds, body: parseExpression(), location };
|
|
769
897
|
}
|
|
770
|
-
const names = parseSeparated(
|
|
898
|
+
const names = getNames(parseSeparated(expectName));
|
|
771
899
|
expect(":");
|
|
772
900
|
return {
|
|
773
901
|
kind: "UnboundedQuantifier",
|
|
774
902
|
quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
|
|
775
|
-
names,
|
|
903
|
+
names: names.names,
|
|
904
|
+
nameRanges: names.ranges,
|
|
776
905
|
body: parseExpression(),
|
|
777
906
|
isTemporal,
|
|
778
907
|
location,
|
|
@@ -826,8 +955,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
826
955
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
827
956
|
};
|
|
828
957
|
|
|
829
|
-
const parsePrimary = (): Expression => {
|
|
830
|
-
const
|
|
958
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
959
|
+
const start = cursor.index;
|
|
831
960
|
const token = advance();
|
|
832
961
|
const location = getLocation(token);
|
|
833
962
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -851,54 +980,94 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
851
980
|
if (token.kind === "Identifier" || isStep) {
|
|
852
981
|
const name = isStep ? (getStepName(token) ?? token.text) : token.text;
|
|
853
982
|
if (isLabelAhead()) {
|
|
854
|
-
const params = accept("(") ? parseSeparated(
|
|
855
|
-
if (params.length > 0) expect(")");
|
|
983
|
+
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
984
|
+
if (params.names.length > 0) expect(")");
|
|
856
985
|
expect("::");
|
|
857
|
-
|
|
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
|
+
};
|
|
858
1005
|
}
|
|
859
1006
|
const hasArguments = isAt("(");
|
|
1007
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1008
|
+
return { kind: "ExceptAt", location };
|
|
860
1009
|
const args = hasArguments ? parseArguments() : [];
|
|
1010
|
+
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
861
1011
|
if (!isSelectorAhead()) {
|
|
862
1012
|
return hasArguments
|
|
863
1013
|
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
864
1014
|
: { kind: "Identifier", name, location };
|
|
865
1015
|
}
|
|
866
|
-
const instance = openModules
|
|
867
|
-
.flatMap((module) => module.instances)
|
|
868
|
-
.find((candidate) => candidate.name === name);
|
|
869
|
-
if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
|
|
870
|
-
throw createError(
|
|
871
|
-
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
872
|
-
token,
|
|
873
|
-
);
|
|
874
|
-
}
|
|
875
1016
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1017
|
+
const selectorRanges = [getRange(start, start)];
|
|
876
1018
|
while (isSelectorAhead()) {
|
|
877
1019
|
advance();
|
|
1020
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
1021
|
+
lastArguments.range = undefined;
|
|
878
1022
|
selectors.push(parseSelector());
|
|
1023
|
+
argumentRanges.push(lastArguments.range);
|
|
879
1024
|
}
|
|
880
1025
|
const names = selectors.flatMap((selector) =>
|
|
881
1026
|
selector.kind === "Name" ? [selector.name] : [],
|
|
882
1027
|
);
|
|
883
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
884
1028
|
const operator = names.join("!");
|
|
885
1029
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
886
1030
|
"args" in selector ? selector.args : [],
|
|
887
1031
|
);
|
|
888
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
889
1032
|
const argumentCounts = selectors.map((selector) =>
|
|
890
1033
|
"args" in selector ? selector.args.length : 0,
|
|
891
1034
|
);
|
|
892
|
-
const argumentsRange =
|
|
893
|
-
return
|
|
894
|
-
? { kind: "
|
|
895
|
-
:
|
|
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
|
+
};
|
|
896
1060
|
}
|
|
897
|
-
|
|
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("(")) {
|
|
898
1063
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
1064
|
+
}
|
|
1065
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1066
|
+
return { kind: "Identifier", name: token.text, location };
|
|
1067
|
+
}
|
|
899
1068
|
switch (token.text) {
|
|
900
1069
|
case "(": {
|
|
901
|
-
const expression =
|
|
1070
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
902
1071
|
expect(")");
|
|
903
1072
|
return expression;
|
|
904
1073
|
}
|
|
@@ -954,7 +1123,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
954
1123
|
case "WF_":
|
|
955
1124
|
case "SF_": {
|
|
956
1125
|
const subscript = parseSubscript();
|
|
957
|
-
const action =
|
|
1126
|
+
const action = withBracketLayout(() => {
|
|
958
1127
|
expect("(");
|
|
959
1128
|
const expression = parseExpression();
|
|
960
1129
|
expect(")");
|
|
@@ -971,7 +1140,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
971
1140
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
972
1141
|
};
|
|
973
1142
|
|
|
1143
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1144
|
+
// prefix operator.
|
|
1145
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1146
|
+
operators.last = operators.preceding = { low, high };
|
|
1147
|
+
const operand = parseExpression(high + 1);
|
|
1148
|
+
const next = peek();
|
|
1149
|
+
const nextHigh =
|
|
1150
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1151
|
+
? POSTFIX_PRECEDENCE.high
|
|
1152
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1153
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1154
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1155
|
+
}
|
|
1156
|
+
return operand;
|
|
1157
|
+
};
|
|
1158
|
+
|
|
974
1159
|
const parsePrefix = (): Expression => {
|
|
1160
|
+
const preceding = operators.preceding;
|
|
1161
|
+
operators.preceding = undefined;
|
|
975
1162
|
const token = peek();
|
|
976
1163
|
const location = getLocation(token);
|
|
977
1164
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -981,35 +1168,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
981
1168
|
switch (token.text) {
|
|
982
1169
|
case "~":
|
|
983
1170
|
advance();
|
|
984
|
-
return {
|
|
1171
|
+
return {
|
|
1172
|
+
kind: "Apply",
|
|
1173
|
+
operator: "~",
|
|
1174
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1175
|
+
location,
|
|
1176
|
+
};
|
|
985
1177
|
case "-":
|
|
986
1178
|
advance();
|
|
987
1179
|
return {
|
|
988
1180
|
kind: "Apply",
|
|
989
1181
|
operator: "-.",
|
|
990
|
-
args: [
|
|
1182
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
991
1183
|
location,
|
|
992
1184
|
};
|
|
993
1185
|
case "[]":
|
|
994
1186
|
advance();
|
|
995
|
-
return { kind: "Always", expression:
|
|
1187
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
996
1188
|
case "<>":
|
|
997
1189
|
advance();
|
|
998
|
-
return { kind: "Eventually", expression:
|
|
1190
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
999
1191
|
case "ENABLED":
|
|
1000
1192
|
advance();
|
|
1001
|
-
return { kind: "Enabled", expression:
|
|
1193
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1002
1194
|
case "UNCHANGED":
|
|
1003
1195
|
advance();
|
|
1004
|
-
return { kind: "Unchanged", expression:
|
|
1196
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1005
1197
|
case "SUBSET":
|
|
1006
1198
|
case "UNION":
|
|
1007
1199
|
case "DOMAIN":
|
|
1008
1200
|
advance();
|
|
1009
|
-
return {
|
|
1201
|
+
return {
|
|
1202
|
+
kind: "Apply",
|
|
1203
|
+
operator: token.text,
|
|
1204
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1205
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1206
|
+
location,
|
|
1207
|
+
};
|
|
1010
1208
|
}
|
|
1011
1209
|
}
|
|
1012
|
-
return parsePrimary();
|
|
1210
|
+
return parsePrimary(preceding);
|
|
1013
1211
|
};
|
|
1014
1212
|
|
|
1015
1213
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1025,11 +1223,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1025
1223
|
const location = getLocation(token);
|
|
1026
1224
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1027
1225
|
advance();
|
|
1226
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1028
1227
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1029
1228
|
continue;
|
|
1030
1229
|
}
|
|
1031
1230
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1032
1231
|
advance();
|
|
1232
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1033
1233
|
state.left = withRange(
|
|
1034
1234
|
{ kind: "Apply", operator: token.text, args: [state.left], location },
|
|
1035
1235
|
start,
|
|
@@ -1038,8 +1238,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1038
1238
|
}
|
|
1039
1239
|
if (token.text === "[") {
|
|
1040
1240
|
advance();
|
|
1041
|
-
const args =
|
|
1042
|
-
|
|
1241
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1242
|
+
withBracketLayout(() => expect("]"));
|
|
1243
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1043
1244
|
state.left = withRange(
|
|
1044
1245
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1045
1246
|
start,
|
|
@@ -1070,21 +1271,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1070
1271
|
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1071
1272
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1072
1273
|
}
|
|
1274
|
+
operators.last = operators.preceding = infix;
|
|
1073
1275
|
const right = parseExpression(infix.high + 1);
|
|
1074
1276
|
const left = state.left;
|
|
1075
1277
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1076
1278
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1279
|
+
const prefixEnds = [getRange(start).end];
|
|
1077
1280
|
state.left =
|
|
1078
1281
|
left.kind === kind && previous?.text === token.text
|
|
1079
|
-
? {
|
|
1080
|
-
|
|
1282
|
+
? {
|
|
1283
|
+
...left,
|
|
1284
|
+
operands: [...left.operands, right],
|
|
1285
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1286
|
+
}
|
|
1287
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1081
1288
|
} else if (token.text === "\\X") {
|
|
1082
1289
|
state.left =
|
|
1083
1290
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1084
1291
|
? { ...left, factors: [...left.factors, right] }
|
|
1085
1292
|
: { kind: "Product", factors: [left, right], location };
|
|
1086
1293
|
} else {
|
|
1087
|
-
state.left = {
|
|
1294
|
+
state.left = {
|
|
1295
|
+
kind: "Apply",
|
|
1296
|
+
operator: token.text,
|
|
1297
|
+
spelling: token.spelling,
|
|
1298
|
+
args: [left, right],
|
|
1299
|
+
location,
|
|
1300
|
+
};
|
|
1088
1301
|
}
|
|
1089
1302
|
state.left = withRange(state.left, start);
|
|
1090
1303
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1104,7 +1317,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1104
1317
|
const body = parseExpression();
|
|
1105
1318
|
const last = tokens[cursor.index - 1];
|
|
1106
1319
|
const end = { line: last.line, column: last.endColumn };
|
|
1107
|
-
return { body, bodyRange: { module: openModules[
|
|
1320
|
+
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1108
1321
|
};
|
|
1109
1322
|
|
|
1110
1323
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
@@ -1112,6 +1325,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1112
1325
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1113
1326
|
};
|
|
1114
1327
|
|
|
1328
|
+
const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
|
|
1329
|
+
name: named.name,
|
|
1330
|
+
arity: 0,
|
|
1331
|
+
location: {
|
|
1332
|
+
line: named.range.start.line,
|
|
1333
|
+
column: named.range.start.column,
|
|
1334
|
+
range: named.range,
|
|
1335
|
+
},
|
|
1336
|
+
});
|
|
1337
|
+
|
|
1115
1338
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1116
1339
|
const token = peek();
|
|
1117
1340
|
const location = getLocation(token);
|
|
@@ -1131,28 +1354,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1131
1354
|
peek(2).kind === "Identifier" &&
|
|
1132
1355
|
isAt("==", 3)
|
|
1133
1356
|
) {
|
|
1134
|
-
const left =
|
|
1357
|
+
const left = expectName();
|
|
1135
1358
|
const operator = advance().text;
|
|
1136
|
-
const right =
|
|
1359
|
+
const right = expectName();
|
|
1137
1360
|
expect("==");
|
|
1138
|
-
return parseOperator(operator, [
|
|
1139
|
-
{ name: left, arity: 0 },
|
|
1140
|
-
{ name: right, arity: 0 },
|
|
1141
|
-
]);
|
|
1361
|
+
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1142
1362
|
}
|
|
1143
|
-
const isPrefixDefinition =
|
|
1363
|
+
const isPrefixDefinition =
|
|
1364
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1144
1365
|
const isPostfixDefinition =
|
|
1145
1366
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1146
1367
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
1147
1368
|
const operator = isPrefixDefinition ? token.text : peek(1).text;
|
|
1148
|
-
|
|
1149
|
-
const param =
|
|
1369
|
+
if (isPrefixDefinition) advance();
|
|
1370
|
+
const param = expectName();
|
|
1371
|
+
if (isPostfixDefinition) advance();
|
|
1150
1372
|
expect("==");
|
|
1151
|
-
return parseOperator(operator, [
|
|
1373
|
+
return parseOperator(operator, [toParam(param)]);
|
|
1152
1374
|
}
|
|
1153
1375
|
const name = expectIdentifier();
|
|
1154
1376
|
if (accept("[")) {
|
|
1155
|
-
const bounds =
|
|
1377
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1156
1378
|
expect("]");
|
|
1157
1379
|
expect("==");
|
|
1158
1380
|
return {
|
|
@@ -1183,12 +1405,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1183
1405
|
): Instance => {
|
|
1184
1406
|
const start = cursor.index;
|
|
1185
1407
|
expect("INSTANCE");
|
|
1186
|
-
const module =
|
|
1408
|
+
const module = expectModuleName();
|
|
1187
1409
|
const substitutions = accept("WITH")
|
|
1188
1410
|
? parseSeparated(() => {
|
|
1189
|
-
const
|
|
1411
|
+
const targetStart = cursor.index;
|
|
1412
|
+
const target = expectSubstitutionTarget();
|
|
1413
|
+
const nameRange = getRange(targetStart);
|
|
1190
1414
|
expect("<-");
|
|
1191
|
-
return { name: target, expression: parseArgument() };
|
|
1415
|
+
return { name: target, nameRange, expression: parseArgument() };
|
|
1192
1416
|
})
|
|
1193
1417
|
: [];
|
|
1194
1418
|
return {
|
|
@@ -1204,6 +1428,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1204
1428
|
};
|
|
1205
1429
|
|
|
1206
1430
|
const parseDeclaration = (): Declaration => {
|
|
1431
|
+
const start = cursor.index;
|
|
1432
|
+
return withRange(parseDeclarationUnit(), start);
|
|
1433
|
+
};
|
|
1434
|
+
|
|
1435
|
+
const parseDeclarationUnit = (): Declaration => {
|
|
1207
1436
|
const location = getLocation(peek());
|
|
1208
1437
|
if (isAt("_") && peek(1).kind === "Operator") {
|
|
1209
1438
|
advance();
|
|
@@ -1249,7 +1478,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1249
1478
|
const token = peek();
|
|
1250
1479
|
const next = peek(1);
|
|
1251
1480
|
if (token.kind !== "Identifier") {
|
|
1252
|
-
return
|
|
1481
|
+
return (
|
|
1482
|
+
token.kind !== "String" &&
|
|
1483
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1484
|
+
next.kind === "Identifier" &&
|
|
1485
|
+
isAt("==", 2)
|
|
1486
|
+
);
|
|
1253
1487
|
}
|
|
1254
1488
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1255
1489
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1341,7 +1575,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1341
1575
|
// start of an expression.
|
|
1342
1576
|
const parseFact = (): Fact => {
|
|
1343
1577
|
const start = cursor.index;
|
|
1344
|
-
factStart.index = start;
|
|
1345
1578
|
const token = peek();
|
|
1346
1579
|
if (
|
|
1347
1580
|
token.kind === "Operator" &&
|
|
@@ -1403,7 +1636,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1403
1636
|
const parseDefs = () => {
|
|
1404
1637
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1405
1638
|
const defs = parseSeparated(parseFact);
|
|
1406
|
-
const invalid =
|
|
1639
|
+
const invalid =
|
|
1640
|
+
mode === "tlc"
|
|
1641
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1642
|
+
: undefined;
|
|
1407
1643
|
if (invalid) {
|
|
1408
1644
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1409
1645
|
}
|
|
@@ -1428,7 +1664,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1428
1664
|
location: getLocation(keyword),
|
|
1429
1665
|
};
|
|
1430
1666
|
const invalid =
|
|
1431
|
-
use.kind === "Hide" &&
|
|
1667
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1432
1668
|
if (invalid.length > 0) {
|
|
1433
1669
|
throw createSanyError(
|
|
1434
1670
|
invalid.map((fact) => ({
|
|
@@ -1451,10 +1687,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1451
1687
|
}
|
|
1452
1688
|
if (level !== undefined) advance();
|
|
1453
1689
|
if (level === "Variable") {
|
|
1454
|
-
const
|
|
1455
|
-
declareProofSymbol(name);
|
|
1690
|
+
const named = expectName();
|
|
1691
|
+
declareProofSymbol(named.name);
|
|
1456
1692
|
return withRange(
|
|
1457
|
-
{
|
|
1693
|
+
{
|
|
1694
|
+
kind: "NewSymbol",
|
|
1695
|
+
name: named.name,
|
|
1696
|
+
arity: 0,
|
|
1697
|
+
level,
|
|
1698
|
+
set: undefined,
|
|
1699
|
+
nameRange: named.range,
|
|
1700
|
+
location,
|
|
1701
|
+
},
|
|
1458
1702
|
start,
|
|
1459
1703
|
);
|
|
1460
1704
|
}
|
|
@@ -1472,6 +1716,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1472
1716
|
arity: declaration.arity,
|
|
1473
1717
|
level: level ?? "Constant",
|
|
1474
1718
|
set: isBounded ? parseExpression() : undefined,
|
|
1719
|
+
nameRange: declaration.location.range,
|
|
1475
1720
|
location,
|
|
1476
1721
|
},
|
|
1477
1722
|
start,
|
|
@@ -1514,7 +1759,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1514
1759
|
const token = peek();
|
|
1515
1760
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1516
1761
|
if (isAt("INSTANCE")) {
|
|
1517
|
-
|
|
1762
|
+
const start = cursor.index;
|
|
1763
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1518
1764
|
}
|
|
1519
1765
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1520
1766
|
const start = cursor.index;
|
|
@@ -1527,21 +1773,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1527
1773
|
if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
|
|
1528
1774
|
if (accept("TAKE")) {
|
|
1529
1775
|
const isBounded = isBoundAhead();
|
|
1530
|
-
const names = isBounded ? [] : parseSeparated(
|
|
1776
|
+
const names = getNames(isBounded ? [] : parseSeparated(expectName));
|
|
1531
1777
|
const bounds = isBounded ? parseBounds() : [];
|
|
1532
|
-
declareProofSymbols(names, bounds);
|
|
1533
|
-
return { kind: "Take", names, bounds };
|
|
1778
|
+
declareProofSymbols(names.names, bounds);
|
|
1779
|
+
return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
|
|
1534
1780
|
}
|
|
1535
1781
|
if (accept("WITNESS")) {
|
|
1536
1782
|
return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
|
|
1537
1783
|
}
|
|
1538
1784
|
if (accept("PICK")) {
|
|
1539
1785
|
const isNames = isNameListAhead(":");
|
|
1540
|
-
const names = isNames ? parseSeparated(
|
|
1786
|
+
const names = getNames(isNames ? parseSeparated(expectName) : []);
|
|
1541
1787
|
const bounds = isNames ? [] : parseBounds();
|
|
1542
|
-
declareProofSymbols(names, bounds);
|
|
1788
|
+
declareProofSymbols(names.names, bounds);
|
|
1543
1789
|
expect(":");
|
|
1544
|
-
return {
|
|
1790
|
+
return {
|
|
1791
|
+
kind: "Pick",
|
|
1792
|
+
names: names.names,
|
|
1793
|
+
nameRanges: names.ranges,
|
|
1794
|
+
bounds,
|
|
1795
|
+
body: parseExpression(),
|
|
1796
|
+
};
|
|
1545
1797
|
}
|
|
1546
1798
|
if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
|
|
1547
1799
|
return {
|
|
@@ -1571,10 +1823,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1571
1823
|
const parseStep = (): ProofStep => {
|
|
1572
1824
|
const start = cursor.index;
|
|
1573
1825
|
const step = parseStepStart(isAt("QED", 1));
|
|
1826
|
+
const statementStart = cursor.index;
|
|
1574
1827
|
const scope = new Set<string>();
|
|
1575
1828
|
proofScopes.push(scope);
|
|
1576
1829
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1577
1830
|
const head = getRange(start);
|
|
1831
|
+
const statement = getRange(statementStart);
|
|
1578
1832
|
const hasProof = isProofStart(peek());
|
|
1579
1833
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1580
1834
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1582,7 +1836,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1582
1836
|
const proof = hasProof ? parseProof() : undefined;
|
|
1583
1837
|
proofScopes.pop();
|
|
1584
1838
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1585
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1839
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1586
1840
|
};
|
|
1587
1841
|
|
|
1588
1842
|
const parseProofBody = (): Proof => {
|
|
@@ -1617,6 +1871,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1617
1871
|
return withRange(proof, start);
|
|
1618
1872
|
};
|
|
1619
1873
|
|
|
1874
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1875
|
+
// definition, warning of the second.
|
|
1876
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1877
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1878
|
+
module.definitions.push(definition);
|
|
1879
|
+
};
|
|
1880
|
+
|
|
1620
1881
|
const parseTheorem = (module: Module) => {
|
|
1621
1882
|
const start = cursor.index;
|
|
1622
1883
|
const keyword = advance();
|
|
@@ -1638,7 +1899,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1638
1899
|
module.theorems.push(theorem);
|
|
1639
1900
|
module.units.push({ kind: "Theorem", theorem });
|
|
1640
1901
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1641
|
-
|
|
1902
|
+
addFactDefinition(
|
|
1903
|
+
module,
|
|
1642
1904
|
withRange(
|
|
1643
1905
|
{
|
|
1644
1906
|
kind: "Operator",
|
|
@@ -1675,7 +1937,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1675
1937
|
const parseModuleBody = (): Module => {
|
|
1676
1938
|
const location = getLocation(expect("----"));
|
|
1677
1939
|
expect("MODULE");
|
|
1678
|
-
const name =
|
|
1940
|
+
const name = expectModuleName();
|
|
1679
1941
|
expect("----");
|
|
1680
1942
|
const module: Module = {
|
|
1681
1943
|
name,
|
|
@@ -1697,7 +1959,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1697
1959
|
if (accept("EXTENDS")) {
|
|
1698
1960
|
parseSeparated(() => {
|
|
1699
1961
|
const start = cursor.index;
|
|
1700
|
-
module.extends.push(
|
|
1962
|
+
module.extends.push(expectModuleName());
|
|
1701
1963
|
module.extendsRanges.push(getRange(start));
|
|
1702
1964
|
});
|
|
1703
1965
|
}
|
|
@@ -1705,14 +1967,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1705
1967
|
const tokenIndex = cursor.index;
|
|
1706
1968
|
unitStart.index = tokenIndex;
|
|
1707
1969
|
const token = peek();
|
|
1708
|
-
if (token.kind === "ModuleEnd"
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
};
|
|
1715
|
-
}
|
|
1970
|
+
if (token.kind === "ModuleEnd") {
|
|
1971
|
+
location.range = {
|
|
1972
|
+
module: openModules[0].name,
|
|
1973
|
+
start: { line: location.line, column: location.column },
|
|
1974
|
+
end: { line: token.line, column: token.endColumn },
|
|
1975
|
+
};
|
|
1716
1976
|
openModules.pop();
|
|
1717
1977
|
recursiveScopes.pop();
|
|
1718
1978
|
return module;
|
|
@@ -1731,22 +1991,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1731
1991
|
switch (token.text) {
|
|
1732
1992
|
case "CONSTANT":
|
|
1733
1993
|
case "CONSTANTS": {
|
|
1734
|
-
advance();
|
|
1994
|
+
const unitLocation = getLocation(advance());
|
|
1735
1995
|
const declarations = parseDeclarations();
|
|
1736
1996
|
module.constants.push(...declarations);
|
|
1737
|
-
|
|
1997
|
+
unitLocation.range = getRange(tokenIndex);
|
|
1998
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1738
1999
|
continue;
|
|
1739
2000
|
}
|
|
1740
2001
|
case "VARIABLE":
|
|
1741
2002
|
case "VARIABLES": {
|
|
1742
|
-
advance();
|
|
2003
|
+
const unitLocation = getLocation(advance());
|
|
1743
2004
|
const declarations = parseSeparated(() => {
|
|
1744
2005
|
const start = cursor.index;
|
|
1745
2006
|
const location = getLocation(peek());
|
|
1746
2007
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1747
2008
|
});
|
|
1748
2009
|
module.variables.push(...declarations);
|
|
1749
|
-
|
|
2010
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2011
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1750
2012
|
continue;
|
|
1751
2013
|
}
|
|
1752
2014
|
case "ASSUME":
|
|
@@ -1756,7 +2018,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1756
2018
|
module.assumptions.push(assumption);
|
|
1757
2019
|
module.units.push({ kind: "Assumption", assumption });
|
|
1758
2020
|
if (assumption.name !== undefined) {
|
|
1759
|
-
module
|
|
2021
|
+
addFactDefinition(module, {
|
|
1760
2022
|
kind: "Operator",
|
|
1761
2023
|
name: assumption.name,
|
|
1762
2024
|
params: [],
|
|
@@ -1770,6 +2032,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1770
2032
|
}
|
|
1771
2033
|
case "USE":
|
|
1772
2034
|
case "HIDE": {
|
|
2035
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2036
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2037
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2038
|
+
}
|
|
1773
2039
|
const use = parseUseOrHide();
|
|
1774
2040
|
module.uses.push(use);
|
|
1775
2041
|
module.units.push({ kind: "Use", use });
|
|
@@ -1794,7 +2060,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1794
2060
|
);
|
|
1795
2061
|
continue;
|
|
1796
2062
|
}
|
|
1797
|
-
addDefinitionUnit(module,
|
|
2063
|
+
addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
|
|
1798
2064
|
continue;
|
|
1799
2065
|
}
|
|
1800
2066
|
}
|
|
@@ -1816,9 +2082,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1816
2082
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1817
2083
|
};
|
|
1818
2084
|
|
|
1819
|
-
|
|
2085
|
+
const module = parseModuleBody();
|
|
2086
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2087
|
+
// module's name from a keyword that the parser would take for one.
|
|
2088
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2089
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2090
|
+
return module;
|
|
1820
2091
|
};
|
|
1821
2092
|
|
|
1822
|
-
export const parseModule = (source: string) => parse(source,
|
|
2093
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2094
|
+
|
|
2095
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1823
2096
|
|
|
1824
|
-
export const
|
|
2097
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|