tla-ts 0.0.3-dev.4cf9a33 → 0.0.3-dev.4fdd845
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +164 -40
- package/dist/index.d.ts +164 -40
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -22
- package/dist/run-cli.js +1351 -22
- package/dist/transfer.cjs +83 -34
- package/dist/transfer.js +83 -34
- package/package.json +5 -1
- package/src/ast.ts +49 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1331 -367
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +417 -124
- package/src/errors.ts +168 -9
- package/src/evaluate.ts +720 -177
- package/src/exit-codes.ts +6 -24
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +149 -31
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +419 -122
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1334 -452
- package/src/run-cli.ts +1166 -181
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +161 -167
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +683 -256
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,36 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
|
|
|
291
330
|
|
|
292
331
|
const NO_OFFSIDE = -1;
|
|
293
332
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
// The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
|
|
297
|
-
// modules that follow the root module.
|
|
333
|
+
// MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
|
|
334
|
+
// root module's file, from its ---- MODULE line to the next ==== line.
|
|
298
335
|
export const extractModule = (source: string, name: string) => {
|
|
336
|
+
const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
|
|
299
337
|
const lines = source.split(/\r?\n/);
|
|
300
|
-
const start = lines.findIndex((line) =>
|
|
338
|
+
const start = lines.findIndex((line) => header.test(line));
|
|
301
339
|
if (start === -1) return undefined;
|
|
302
340
|
const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
|
|
303
|
-
return lines.slice(start, end === -1 ? undefined : end + 1).join("\n")
|
|
341
|
+
return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
|
|
304
342
|
};
|
|
305
343
|
|
|
306
|
-
//
|
|
307
|
-
// SANY does not.
|
|
308
|
-
|
|
309
|
-
|
|
344
|
+
// The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some modules
|
|
345
|
+
// that SANY does not. The sany mode leaves the checks that SANY makes after parsing to the SANY
|
|
346
|
+
// generator.
|
|
347
|
+
type ParseMode = "tlc" | "tlapm" | "sany";
|
|
348
|
+
|
|
349
|
+
const parse = (source: string, mode: ParseMode): Module => {
|
|
350
|
+
const isTlapm = mode === "tlapm";
|
|
351
|
+
const tokens = isTlapm ? tokenize(source) : tokenize(source).map(readAtAsIdentifier);
|
|
310
352
|
const cursor = { index: 0, offsideColumns: [] as number[] };
|
|
353
|
+
const operators: OperatorContext = {};
|
|
311
354
|
// A RECURSIVE declaration covers the definitions of its own LET or module.
|
|
312
355
|
const recursiveScopes: Set<string>[] = [];
|
|
313
356
|
const factNames = new Set<string>();
|
|
314
357
|
// The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
|
|
315
358
|
const proofScopes: Set<string>[] = [];
|
|
316
|
-
// A fact may cite an operator of a parameterized instance without the
|
|
359
|
+
// A fact or an operator argument may cite an operator of a parameterized instance without the
|
|
360
|
+
// instance's arguments, as with Op <- I!Op in a substitution.
|
|
317
361
|
const factStart = { index: -1 };
|
|
362
|
+
// SANY reports a location in a nested module as one in the module of its file.
|
|
318
363
|
const openModules: Module[] = [];
|
|
319
364
|
|
|
320
365
|
const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
|
|
@@ -329,15 +374,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
329
374
|
const tokenEnds: SourceLocation[] = [];
|
|
330
375
|
const tokenRanges: SourceRange[] = [];
|
|
331
376
|
|
|
332
|
-
const getRange = (startIndex: number) => {
|
|
333
|
-
const endIndex = cursor.index - 1;
|
|
377
|
+
const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
|
|
334
378
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
335
379
|
if (cached) return cached;
|
|
336
380
|
const last = tokens[endIndex];
|
|
337
381
|
tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
|
|
338
382
|
tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
|
|
339
383
|
const range = {
|
|
340
|
-
module: openModules[
|
|
384
|
+
module: openModules[0].name,
|
|
341
385
|
start: tokenStarts[startIndex],
|
|
342
386
|
end: tokenEnds[endIndex],
|
|
343
387
|
};
|
|
@@ -435,8 +479,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
435
479
|
const createUnexpected = (message: string, token: Token = peekRaw()) =>
|
|
436
480
|
createError(message, token, token);
|
|
437
481
|
|
|
482
|
+
// TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
|
|
438
483
|
const isFieldName = (token: Token) =>
|
|
439
|
-
(token.kind === "Identifier" ||
|
|
484
|
+
(token.kind === "Identifier" ||
|
|
485
|
+
token.kind === "Keyword" ||
|
|
486
|
+
(token.kind === "Operator" && token.text === "_")) &&
|
|
487
|
+
!isTlapmKeyword(token);
|
|
440
488
|
|
|
441
489
|
const isAt = (text: string, offset = 0) => {
|
|
442
490
|
const token = peek(offset);
|
|
@@ -472,7 +520,42 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
472
520
|
return token.text;
|
|
473
521
|
};
|
|
474
522
|
|
|
475
|
-
const
|
|
523
|
+
const expectName = () => {
|
|
524
|
+
const start = cursor.index;
|
|
525
|
+
return { name: expectIdentifier(), range: getRange(start) };
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const getNames = (names: { name: string; range: SourceRange }[]) => ({
|
|
529
|
+
names: names.map((item) => item.name),
|
|
530
|
+
ranges: names.map((item) => item.range),
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
// TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
|
|
534
|
+
const expectSubstitutionTarget = () => {
|
|
535
|
+
const token = peek();
|
|
536
|
+
const isOperator =
|
|
537
|
+
token.kind !== "String" &&
|
|
538
|
+
(DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
|
|
539
|
+
INFIX_OPERATORS.has(token.text) ||
|
|
540
|
+
POSTFIX_OPERATORS.has(token.text) ||
|
|
541
|
+
token.text === "'");
|
|
542
|
+
if (!isOperator) return expectIdentifier();
|
|
543
|
+
cursor.index++;
|
|
544
|
+
return token.text;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// SANY takes a keyword for the name of a module, as it does for a field.
|
|
548
|
+
const keywordModuleName = { isUsed: false };
|
|
549
|
+
const expectModuleName = () => {
|
|
550
|
+
if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
|
|
551
|
+
keywordModuleName.isUsed = true;
|
|
552
|
+
return advance().text;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// tlapm reads a bracketed expression apart from the junction list around it, whose alignment
|
|
556
|
+
// SANY keeps applying.
|
|
557
|
+
const withBracketLayout = <Result>(parse: () => Result) => {
|
|
558
|
+
if (!isTlapm) return parse();
|
|
476
559
|
cursor.offsideColumns.push(NO_OFFSIDE);
|
|
477
560
|
const result = parse();
|
|
478
561
|
cursor.offsideColumns.pop();
|
|
@@ -514,10 +597,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
514
597
|
};
|
|
515
598
|
|
|
516
599
|
const parsePattern = (): BoundPattern => {
|
|
517
|
-
if (!accept("<<"))
|
|
518
|
-
|
|
600
|
+
if (!accept("<<")) {
|
|
601
|
+
const named = expectName();
|
|
602
|
+
return { kind: "Name", name: named.name, ranges: [named.range] };
|
|
603
|
+
}
|
|
604
|
+
const names = getNames(parseSeparated(expectName));
|
|
519
605
|
expect(">>");
|
|
520
|
-
return { kind: "Tuple", names };
|
|
606
|
+
return { kind: "Tuple", names: names.names, ranges: names.ranges };
|
|
521
607
|
};
|
|
522
608
|
|
|
523
609
|
const parseBounds = () =>
|
|
@@ -560,8 +646,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
560
646
|
);
|
|
561
647
|
};
|
|
562
648
|
|
|
649
|
+
// SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
|
|
650
|
+
// the start of an expression, such as -.(1), otherwise.
|
|
563
651
|
const isStandalone = (operator: Token) => {
|
|
564
|
-
if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
|
|
565
652
|
const token = peek(1);
|
|
566
653
|
return (
|
|
567
654
|
token.kind === "End" ||
|
|
@@ -600,7 +687,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
600
687
|
if (isOperatorAlone) {
|
|
601
688
|
cursor.index++;
|
|
602
689
|
return withRange(
|
|
603
|
-
{
|
|
690
|
+
{
|
|
691
|
+
kind: "OperatorReference",
|
|
692
|
+
operator: token.text,
|
|
693
|
+
spelling: token.spelling,
|
|
694
|
+
location: getLocation(token),
|
|
695
|
+
},
|
|
604
696
|
cursor.index - 1,
|
|
605
697
|
);
|
|
606
698
|
}
|
|
@@ -623,18 +715,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
623
715
|
if (isAt("LAMBDA")) {
|
|
624
716
|
const start = cursor.index;
|
|
625
717
|
advance();
|
|
626
|
-
const params = parseSeparated(
|
|
718
|
+
const params = getNames(parseSeparated(expectName));
|
|
627
719
|
expect(":");
|
|
628
720
|
const body = parseExpression();
|
|
629
|
-
return withRange(
|
|
721
|
+
return withRange(
|
|
722
|
+
{
|
|
723
|
+
kind: "Lambda",
|
|
724
|
+
params: params.names,
|
|
725
|
+
paramRanges: params.ranges,
|
|
726
|
+
body,
|
|
727
|
+
location: getLocation(token),
|
|
728
|
+
},
|
|
729
|
+
start,
|
|
730
|
+
);
|
|
630
731
|
}
|
|
631
|
-
|
|
732
|
+
const argument = parseExpression();
|
|
733
|
+
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
734
|
+
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
735
|
+
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
736
|
+
return argument;
|
|
632
737
|
};
|
|
633
738
|
|
|
739
|
+
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
740
|
+
|
|
634
741
|
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
635
742
|
|
|
636
743
|
const parseArguments = () =>
|
|
637
|
-
|
|
744
|
+
withBracketLayout(() => {
|
|
638
745
|
const start = cursor.index;
|
|
639
746
|
expect("(");
|
|
640
747
|
const args = parseSeparated(parseArgument);
|
|
@@ -661,18 +768,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
661
768
|
};
|
|
662
769
|
|
|
663
770
|
const parseBracket = (start: Token): Expression =>
|
|
664
|
-
|
|
771
|
+
withBracketLayout(() => {
|
|
665
772
|
const location = getLocation(start);
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
773
|
+
// SANY takes a keyword for the first field of a record, as it does for a selected field.
|
|
774
|
+
const isIdentifier = peek().kind === "Identifier";
|
|
775
|
+
const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
|
|
776
|
+
if (isRecord || (isIdentifier && isAt(":", 1))) {
|
|
777
|
+
const kind = isRecord ? "Record" : "RecordSet";
|
|
778
|
+
const parseField = (readName: () => string) => {
|
|
669
779
|
const fieldStart = cursor.index;
|
|
670
780
|
const fieldLocation = getLocation(peek());
|
|
671
|
-
const name =
|
|
781
|
+
const name = readName();
|
|
672
782
|
fieldLocation.range = getRange(fieldStart);
|
|
673
|
-
expect(
|
|
783
|
+
expect(isRecord ? "|->" : ":");
|
|
674
784
|
return { name, value: parseExpression(), location: fieldLocation };
|
|
675
|
-
}
|
|
785
|
+
};
|
|
786
|
+
const fields = [
|
|
787
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
788
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
789
|
+
];
|
|
676
790
|
expect("]");
|
|
677
791
|
return { kind, fields, location };
|
|
678
792
|
}
|
|
@@ -710,17 +824,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
710
824
|
expect("]");
|
|
711
825
|
return { kind: "Except", function: expression, updates, location };
|
|
712
826
|
}
|
|
827
|
+
if (!isTlapm && !isAt("]_")) {
|
|
828
|
+
const args = [expression];
|
|
829
|
+
while (accept(",")) args.push(parseExpression());
|
|
830
|
+
expect("]");
|
|
831
|
+
return { kind: "Bracket", args, location };
|
|
832
|
+
}
|
|
713
833
|
expect("]_");
|
|
714
|
-
return {
|
|
715
|
-
kind: "BoxAction",
|
|
716
|
-
action: expression,
|
|
717
|
-
subscript: parseSubscript(),
|
|
718
|
-
location,
|
|
719
|
-
};
|
|
834
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
720
835
|
});
|
|
721
836
|
|
|
837
|
+
// SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
|
|
838
|
+
const parseSetMembership = (): Expression => {
|
|
839
|
+
const start = cursor.index;
|
|
840
|
+
const element = withRange(parsePrefix(), start);
|
|
841
|
+
const location = getLocation(advance());
|
|
842
|
+
return withRange(
|
|
843
|
+
{ kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
|
|
844
|
+
start,
|
|
845
|
+
);
|
|
846
|
+
};
|
|
847
|
+
|
|
722
848
|
const parseBrace = (start: Token): Expression =>
|
|
723
|
-
|
|
849
|
+
withBracketLayout(() => {
|
|
724
850
|
const location = getLocation(start);
|
|
725
851
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
726
852
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -735,7 +861,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
735
861
|
}
|
|
736
862
|
cursor.index = checkpoint;
|
|
737
863
|
}
|
|
738
|
-
const first =
|
|
864
|
+
const first =
|
|
865
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
739
866
|
if (accept(":")) {
|
|
740
867
|
const bounds = parseBounds();
|
|
741
868
|
expect("}");
|
|
@@ -749,7 +876,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
749
876
|
|
|
750
877
|
const parseTuple = (start: Token): Expression => {
|
|
751
878
|
const location = getLocation(start);
|
|
752
|
-
const elements =
|
|
879
|
+
const elements = withBracketLayout(() =>
|
|
753
880
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
754
881
|
);
|
|
755
882
|
if (accept(">>_")) {
|
|
@@ -770,12 +897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
770
897
|
expect(":");
|
|
771
898
|
return { kind, bounds, body: parseExpression(), location };
|
|
772
899
|
}
|
|
773
|
-
const names = parseSeparated(
|
|
900
|
+
const names = getNames(parseSeparated(expectName));
|
|
774
901
|
expect(":");
|
|
775
902
|
return {
|
|
776
903
|
kind: "UnboundedQuantifier",
|
|
777
904
|
quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
|
|
778
|
-
names,
|
|
905
|
+
names: names.names,
|
|
906
|
+
nameRanges: names.ranges,
|
|
779
907
|
body: parseExpression(),
|
|
780
908
|
isTemporal,
|
|
781
909
|
location,
|
|
@@ -829,8 +957,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
829
957
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
830
958
|
};
|
|
831
959
|
|
|
832
|
-
const parsePrimary = (): Expression => {
|
|
833
|
-
const
|
|
960
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
961
|
+
const start = cursor.index;
|
|
962
|
+
const isFactStart = start === factStart.index;
|
|
834
963
|
const token = advance();
|
|
835
964
|
const location = getLocation(token);
|
|
836
965
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -854,13 +983,34 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
854
983
|
if (token.kind === "Identifier" || isStep) {
|
|
855
984
|
const name = isStep ? (getStepName(token) ?? token.text) : token.text;
|
|
856
985
|
if (isLabelAhead()) {
|
|
857
|
-
const params = accept("(") ? parseSeparated(
|
|
858
|
-
if (params.length > 0) expect(")");
|
|
986
|
+
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
987
|
+
if (params.names.length > 0) expect(")");
|
|
859
988
|
expect("::");
|
|
860
|
-
|
|
989
|
+
const body = parseExpression();
|
|
990
|
+
// TLAplusParser.labelDoesNotChangeParse.
|
|
991
|
+
if (
|
|
992
|
+
!isTlapm &&
|
|
993
|
+
preceding !== undefined &&
|
|
994
|
+
operators.last !== undefined &&
|
|
995
|
+
isOperatorExpression(body) &&
|
|
996
|
+
preceding.high >= operators.last.low
|
|
997
|
+
) {
|
|
998
|
+
throw createError("Removing the label would change expression parsing", token);
|
|
999
|
+
}
|
|
1000
|
+
return {
|
|
1001
|
+
kind: "Label",
|
|
1002
|
+
name,
|
|
1003
|
+
params: params.names,
|
|
1004
|
+
paramRanges: params.ranges,
|
|
1005
|
+
body,
|
|
1006
|
+
location,
|
|
1007
|
+
};
|
|
861
1008
|
}
|
|
862
1009
|
const hasArguments = isAt("(");
|
|
1010
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1011
|
+
return { kind: "ExceptAt", location };
|
|
863
1012
|
const args = hasArguments ? parseArguments() : [];
|
|
1013
|
+
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
864
1014
|
if (!isSelectorAhead()) {
|
|
865
1015
|
return hasArguments
|
|
866
1016
|
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
@@ -869,39 +1019,80 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
869
1019
|
const instance = openModules
|
|
870
1020
|
.flatMap((module) => module.instances)
|
|
871
1021
|
.find((candidate) => candidate.name === name);
|
|
872
|
-
|
|
1022
|
+
const isMissingArguments =
|
|
1023
|
+
instance !== undefined &&
|
|
1024
|
+
mode === "tlc" &&
|
|
1025
|
+
!isFactStart &&
|
|
1026
|
+
instance.params.length !== args.length;
|
|
1027
|
+
if (isMissingArguments && args.length > 0) {
|
|
873
1028
|
throw createError(
|
|
874
1029
|
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
875
1030
|
token,
|
|
876
1031
|
);
|
|
877
1032
|
}
|
|
878
1033
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1034
|
+
const selectorRanges = [getRange(start, start)];
|
|
879
1035
|
while (isSelectorAhead()) {
|
|
880
1036
|
advance();
|
|
1037
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
1038
|
+
lastArguments.range = undefined;
|
|
881
1039
|
selectors.push(parseSelector());
|
|
1040
|
+
argumentRanges.push(lastArguments.range);
|
|
882
1041
|
}
|
|
883
1042
|
const names = selectors.flatMap((selector) =>
|
|
884
1043
|
selector.kind === "Name" ? [selector.name] : [],
|
|
885
1044
|
);
|
|
886
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
887
1045
|
const operator = names.join("!");
|
|
888
1046
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
889
1047
|
"args" in selector ? selector.args : [],
|
|
890
1048
|
);
|
|
891
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
892
1049
|
const argumentCounts = selectors.map((selector) =>
|
|
893
1050
|
"args" in selector ? selector.args.length : 0,
|
|
894
1051
|
);
|
|
895
|
-
const argumentsRange =
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
1052
|
+
const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
|
|
1053
|
+
const reference: Expression =
|
|
1054
|
+
names.length < selectors.length
|
|
1055
|
+
? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
|
|
1056
|
+
: qualifiedArgs.length === 0
|
|
1057
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
1058
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
1059
|
+
? {
|
|
1060
|
+
kind: "Apply",
|
|
1061
|
+
operator,
|
|
1062
|
+
args: qualifiedArgs,
|
|
1063
|
+
argumentCounts,
|
|
1064
|
+
argumentsRange,
|
|
1065
|
+
selectorRanges,
|
|
1066
|
+
argumentRanges,
|
|
1067
|
+
location,
|
|
1068
|
+
}
|
|
1069
|
+
: {
|
|
1070
|
+
kind: "Apply",
|
|
1071
|
+
operator,
|
|
1072
|
+
args: qualifiedArgs,
|
|
1073
|
+
argumentsRange,
|
|
1074
|
+
selectorRanges,
|
|
1075
|
+
argumentRanges,
|
|
1076
|
+
location,
|
|
1077
|
+
};
|
|
1078
|
+
if (isMissingArguments) {
|
|
1079
|
+
missingArguments.set(reference, {
|
|
1080
|
+
location: formatRange(getRange(start)),
|
|
1081
|
+
message: `The operator ${name} requires ${instance.params.length} arguments.`,
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
return reference;
|
|
899
1085
|
}
|
|
900
|
-
|
|
1086
|
+
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1087
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
901
1088
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
1089
|
+
}
|
|
1090
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1091
|
+
return { kind: "Identifier", name: token.text, location };
|
|
1092
|
+
}
|
|
902
1093
|
switch (token.text) {
|
|
903
1094
|
case "(": {
|
|
904
|
-
const expression =
|
|
1095
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
905
1096
|
expect(")");
|
|
906
1097
|
return expression;
|
|
907
1098
|
}
|
|
@@ -957,7 +1148,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
957
1148
|
case "WF_":
|
|
958
1149
|
case "SF_": {
|
|
959
1150
|
const subscript = parseSubscript();
|
|
960
|
-
const action =
|
|
1151
|
+
const action = withBracketLayout(() => {
|
|
961
1152
|
expect("(");
|
|
962
1153
|
const expression = parseExpression();
|
|
963
1154
|
expect(")");
|
|
@@ -974,7 +1165,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
974
1165
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
975
1166
|
};
|
|
976
1167
|
|
|
1168
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1169
|
+
// prefix operator.
|
|
1170
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1171
|
+
operators.last = operators.preceding = { low, high };
|
|
1172
|
+
const operand = parseExpression(high + 1);
|
|
1173
|
+
const next = peek();
|
|
1174
|
+
const nextHigh =
|
|
1175
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1176
|
+
? POSTFIX_PRECEDENCE.high
|
|
1177
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1178
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1179
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1180
|
+
}
|
|
1181
|
+
return operand;
|
|
1182
|
+
};
|
|
1183
|
+
|
|
977
1184
|
const parsePrefix = (): Expression => {
|
|
1185
|
+
const preceding = operators.preceding;
|
|
1186
|
+
operators.preceding = undefined;
|
|
978
1187
|
const token = peek();
|
|
979
1188
|
const location = getLocation(token);
|
|
980
1189
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -984,35 +1193,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
984
1193
|
switch (token.text) {
|
|
985
1194
|
case "~":
|
|
986
1195
|
advance();
|
|
987
|
-
return {
|
|
1196
|
+
return {
|
|
1197
|
+
kind: "Apply",
|
|
1198
|
+
operator: "~",
|
|
1199
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1200
|
+
location,
|
|
1201
|
+
};
|
|
988
1202
|
case "-":
|
|
989
1203
|
advance();
|
|
990
1204
|
return {
|
|
991
1205
|
kind: "Apply",
|
|
992
1206
|
operator: "-.",
|
|
993
|
-
args: [
|
|
1207
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
994
1208
|
location,
|
|
995
1209
|
};
|
|
996
1210
|
case "[]":
|
|
997
1211
|
advance();
|
|
998
|
-
return { kind: "Always", expression:
|
|
1212
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
999
1213
|
case "<>":
|
|
1000
1214
|
advance();
|
|
1001
|
-
return { kind: "Eventually", expression:
|
|
1215
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1002
1216
|
case "ENABLED":
|
|
1003
1217
|
advance();
|
|
1004
|
-
return { kind: "Enabled", expression:
|
|
1218
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1005
1219
|
case "UNCHANGED":
|
|
1006
1220
|
advance();
|
|
1007
|
-
return { kind: "Unchanged", expression:
|
|
1221
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1008
1222
|
case "SUBSET":
|
|
1009
1223
|
case "UNION":
|
|
1010
1224
|
case "DOMAIN":
|
|
1011
1225
|
advance();
|
|
1012
|
-
return {
|
|
1226
|
+
return {
|
|
1227
|
+
kind: "Apply",
|
|
1228
|
+
operator: token.text,
|
|
1229
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1230
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1231
|
+
location,
|
|
1232
|
+
};
|
|
1013
1233
|
}
|
|
1014
1234
|
}
|
|
1015
|
-
return parsePrimary();
|
|
1235
|
+
return parsePrimary(preceding);
|
|
1016
1236
|
};
|
|
1017
1237
|
|
|
1018
1238
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1028,11 +1248,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1028
1248
|
const location = getLocation(token);
|
|
1029
1249
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1030
1250
|
advance();
|
|
1251
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1031
1252
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1032
1253
|
continue;
|
|
1033
1254
|
}
|
|
1034
1255
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1035
1256
|
advance();
|
|
1257
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1036
1258
|
state.left = withRange(
|
|
1037
1259
|
{ kind: "Apply", operator: token.text, args: [state.left], location },
|
|
1038
1260
|
start,
|
|
@@ -1041,8 +1263,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1041
1263
|
}
|
|
1042
1264
|
if (token.text === "[") {
|
|
1043
1265
|
advance();
|
|
1044
|
-
const args =
|
|
1045
|
-
|
|
1266
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1267
|
+
withBracketLayout(() => expect("]"));
|
|
1268
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1046
1269
|
state.left = withRange(
|
|
1047
1270
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1048
1271
|
start,
|
|
@@ -1073,21 +1296,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1073
1296
|
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1074
1297
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1075
1298
|
}
|
|
1299
|
+
operators.last = operators.preceding = infix;
|
|
1076
1300
|
const right = parseExpression(infix.high + 1);
|
|
1077
1301
|
const left = state.left;
|
|
1078
1302
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1079
1303
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1304
|
+
const prefixEnds = [getRange(start).end];
|
|
1080
1305
|
state.left =
|
|
1081
1306
|
left.kind === kind && previous?.text === token.text
|
|
1082
|
-
? {
|
|
1083
|
-
|
|
1307
|
+
? {
|
|
1308
|
+
...left,
|
|
1309
|
+
operands: [...left.operands, right],
|
|
1310
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1311
|
+
}
|
|
1312
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1084
1313
|
} else if (token.text === "\\X") {
|
|
1085
1314
|
state.left =
|
|
1086
1315
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1087
1316
|
? { ...left, factors: [...left.factors, right] }
|
|
1088
1317
|
: { kind: "Product", factors: [left, right], location };
|
|
1089
1318
|
} else {
|
|
1090
|
-
state.left = {
|
|
1319
|
+
state.left = {
|
|
1320
|
+
kind: "Apply",
|
|
1321
|
+
operator: token.text,
|
|
1322
|
+
spelling: token.spelling,
|
|
1323
|
+
args: [left, right],
|
|
1324
|
+
location,
|
|
1325
|
+
};
|
|
1091
1326
|
}
|
|
1092
1327
|
state.left = withRange(state.left, start);
|
|
1093
1328
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1107,7 +1342,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1107
1342
|
const body = parseExpression();
|
|
1108
1343
|
const last = tokens[cursor.index - 1];
|
|
1109
1344
|
const end = { line: last.line, column: last.endColumn };
|
|
1110
|
-
return { body, bodyRange: { module: openModules[
|
|
1345
|
+
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1111
1346
|
};
|
|
1112
1347
|
|
|
1113
1348
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
@@ -1115,6 +1350,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1115
1350
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1116
1351
|
};
|
|
1117
1352
|
|
|
1353
|
+
const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
|
|
1354
|
+
name: named.name,
|
|
1355
|
+
arity: 0,
|
|
1356
|
+
location: {
|
|
1357
|
+
line: named.range.start.line,
|
|
1358
|
+
column: named.range.start.column,
|
|
1359
|
+
range: named.range,
|
|
1360
|
+
},
|
|
1361
|
+
});
|
|
1362
|
+
|
|
1118
1363
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1119
1364
|
const token = peek();
|
|
1120
1365
|
const location = getLocation(token);
|
|
@@ -1134,28 +1379,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1134
1379
|
peek(2).kind === "Identifier" &&
|
|
1135
1380
|
isAt("==", 3)
|
|
1136
1381
|
) {
|
|
1137
|
-
const left =
|
|
1382
|
+
const left = expectName();
|
|
1138
1383
|
const operator = advance().text;
|
|
1139
|
-
const right =
|
|
1384
|
+
const right = expectName();
|
|
1140
1385
|
expect("==");
|
|
1141
|
-
return parseOperator(operator, [
|
|
1142
|
-
{ name: left, arity: 0 },
|
|
1143
|
-
{ name: right, arity: 0 },
|
|
1144
|
-
]);
|
|
1386
|
+
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1145
1387
|
}
|
|
1146
|
-
const isPrefixDefinition =
|
|
1388
|
+
const isPrefixDefinition =
|
|
1389
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1147
1390
|
const isPostfixDefinition =
|
|
1148
1391
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1149
1392
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
1150
1393
|
const operator = isPrefixDefinition ? token.text : peek(1).text;
|
|
1151
|
-
|
|
1152
|
-
const param =
|
|
1394
|
+
if (isPrefixDefinition) advance();
|
|
1395
|
+
const param = expectName();
|
|
1396
|
+
if (isPostfixDefinition) advance();
|
|
1153
1397
|
expect("==");
|
|
1154
|
-
return parseOperator(operator, [
|
|
1398
|
+
return parseOperator(operator, [toParam(param)]);
|
|
1155
1399
|
}
|
|
1156
1400
|
const name = expectIdentifier();
|
|
1157
1401
|
if (accept("[")) {
|
|
1158
|
-
const bounds =
|
|
1402
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1159
1403
|
expect("]");
|
|
1160
1404
|
expect("==");
|
|
1161
1405
|
return {
|
|
@@ -1186,12 +1430,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1186
1430
|
): Instance => {
|
|
1187
1431
|
const start = cursor.index;
|
|
1188
1432
|
expect("INSTANCE");
|
|
1189
|
-
const module =
|
|
1433
|
+
const module = expectModuleName();
|
|
1190
1434
|
const substitutions = accept("WITH")
|
|
1191
1435
|
? parseSeparated(() => {
|
|
1192
|
-
const
|
|
1436
|
+
const targetStart = cursor.index;
|
|
1437
|
+
const target = expectSubstitutionTarget();
|
|
1438
|
+
const nameRange = getRange(targetStart);
|
|
1193
1439
|
expect("<-");
|
|
1194
|
-
return { name: target, expression: parseArgument() };
|
|
1440
|
+
return { name: target, nameRange, expression: parseArgument() };
|
|
1195
1441
|
})
|
|
1196
1442
|
: [];
|
|
1197
1443
|
return {
|
|
@@ -1207,6 +1453,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1207
1453
|
};
|
|
1208
1454
|
|
|
1209
1455
|
const parseDeclaration = (): Declaration => {
|
|
1456
|
+
const start = cursor.index;
|
|
1457
|
+
return withRange(parseDeclarationUnit(), start);
|
|
1458
|
+
};
|
|
1459
|
+
|
|
1460
|
+
const parseDeclarationUnit = (): Declaration => {
|
|
1210
1461
|
const location = getLocation(peek());
|
|
1211
1462
|
if (isAt("_") && peek(1).kind === "Operator") {
|
|
1212
1463
|
advance();
|
|
@@ -1252,7 +1503,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1252
1503
|
const token = peek();
|
|
1253
1504
|
const next = peek(1);
|
|
1254
1505
|
if (token.kind !== "Identifier") {
|
|
1255
|
-
return
|
|
1506
|
+
return (
|
|
1507
|
+
token.kind !== "String" &&
|
|
1508
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1509
|
+
next.kind === "Identifier" &&
|
|
1510
|
+
isAt("==", 2)
|
|
1511
|
+
);
|
|
1256
1512
|
}
|
|
1257
1513
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1258
1514
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1406,7 +1662,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1406
1662
|
const parseDefs = () => {
|
|
1407
1663
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1408
1664
|
const defs = parseSeparated(parseFact);
|
|
1409
|
-
const invalid =
|
|
1665
|
+
const invalid =
|
|
1666
|
+
mode === "tlc"
|
|
1667
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1668
|
+
: undefined;
|
|
1410
1669
|
if (invalid) {
|
|
1411
1670
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1412
1671
|
}
|
|
@@ -1431,7 +1690,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1431
1690
|
location: getLocation(keyword),
|
|
1432
1691
|
};
|
|
1433
1692
|
const invalid =
|
|
1434
|
-
use.kind === "Hide" &&
|
|
1693
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1435
1694
|
if (invalid.length > 0) {
|
|
1436
1695
|
throw createSanyError(
|
|
1437
1696
|
invalid.map((fact) => ({
|
|
@@ -1454,10 +1713,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1454
1713
|
}
|
|
1455
1714
|
if (level !== undefined) advance();
|
|
1456
1715
|
if (level === "Variable") {
|
|
1457
|
-
const
|
|
1458
|
-
declareProofSymbol(name);
|
|
1716
|
+
const named = expectName();
|
|
1717
|
+
declareProofSymbol(named.name);
|
|
1459
1718
|
return withRange(
|
|
1460
|
-
{
|
|
1719
|
+
{
|
|
1720
|
+
kind: "NewSymbol",
|
|
1721
|
+
name: named.name,
|
|
1722
|
+
arity: 0,
|
|
1723
|
+
level,
|
|
1724
|
+
set: undefined,
|
|
1725
|
+
nameRange: named.range,
|
|
1726
|
+
location,
|
|
1727
|
+
},
|
|
1461
1728
|
start,
|
|
1462
1729
|
);
|
|
1463
1730
|
}
|
|
@@ -1475,6 +1742,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1475
1742
|
arity: declaration.arity,
|
|
1476
1743
|
level: level ?? "Constant",
|
|
1477
1744
|
set: isBounded ? parseExpression() : undefined,
|
|
1745
|
+
nameRange: declaration.location.range,
|
|
1478
1746
|
location,
|
|
1479
1747
|
},
|
|
1480
1748
|
start,
|
|
@@ -1517,7 +1785,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1517
1785
|
const token = peek();
|
|
1518
1786
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1519
1787
|
if (isAt("INSTANCE")) {
|
|
1520
|
-
|
|
1788
|
+
const start = cursor.index;
|
|
1789
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1521
1790
|
}
|
|
1522
1791
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1523
1792
|
const start = cursor.index;
|
|
@@ -1530,21 +1799,27 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1530
1799
|
if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
|
|
1531
1800
|
if (accept("TAKE")) {
|
|
1532
1801
|
const isBounded = isBoundAhead();
|
|
1533
|
-
const names = isBounded ? [] : parseSeparated(
|
|
1802
|
+
const names = getNames(isBounded ? [] : parseSeparated(expectName));
|
|
1534
1803
|
const bounds = isBounded ? parseBounds() : [];
|
|
1535
|
-
declareProofSymbols(names, bounds);
|
|
1536
|
-
return { kind: "Take", names, bounds };
|
|
1804
|
+
declareProofSymbols(names.names, bounds);
|
|
1805
|
+
return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
|
|
1537
1806
|
}
|
|
1538
1807
|
if (accept("WITNESS")) {
|
|
1539
1808
|
return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
|
|
1540
1809
|
}
|
|
1541
1810
|
if (accept("PICK")) {
|
|
1542
1811
|
const isNames = isNameListAhead(":");
|
|
1543
|
-
const names = isNames ? parseSeparated(
|
|
1812
|
+
const names = getNames(isNames ? parseSeparated(expectName) : []);
|
|
1544
1813
|
const bounds = isNames ? [] : parseBounds();
|
|
1545
|
-
declareProofSymbols(names, bounds);
|
|
1814
|
+
declareProofSymbols(names.names, bounds);
|
|
1546
1815
|
expect(":");
|
|
1547
|
-
return {
|
|
1816
|
+
return {
|
|
1817
|
+
kind: "Pick",
|
|
1818
|
+
names: names.names,
|
|
1819
|
+
nameRanges: names.ranges,
|
|
1820
|
+
bounds,
|
|
1821
|
+
body: parseExpression(),
|
|
1822
|
+
};
|
|
1548
1823
|
}
|
|
1549
1824
|
if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
|
|
1550
1825
|
return {
|
|
@@ -1574,10 +1849,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1574
1849
|
const parseStep = (): ProofStep => {
|
|
1575
1850
|
const start = cursor.index;
|
|
1576
1851
|
const step = parseStepStart(isAt("QED", 1));
|
|
1852
|
+
const statementStart = cursor.index;
|
|
1577
1853
|
const scope = new Set<string>();
|
|
1578
1854
|
proofScopes.push(scope);
|
|
1579
1855
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1580
1856
|
const head = getRange(start);
|
|
1857
|
+
const statement = getRange(statementStart);
|
|
1581
1858
|
const hasProof = isProofStart(peek());
|
|
1582
1859
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1583
1860
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1585,7 +1862,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1585
1862
|
const proof = hasProof ? parseProof() : undefined;
|
|
1586
1863
|
proofScopes.pop();
|
|
1587
1864
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1588
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1865
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1589
1866
|
};
|
|
1590
1867
|
|
|
1591
1868
|
const parseProofBody = (): Proof => {
|
|
@@ -1620,6 +1897,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1620
1897
|
return withRange(proof, start);
|
|
1621
1898
|
};
|
|
1622
1899
|
|
|
1900
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1901
|
+
// definition, warning of the second.
|
|
1902
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1903
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1904
|
+
module.definitions.push(definition);
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1623
1907
|
const parseTheorem = (module: Module) => {
|
|
1624
1908
|
const start = cursor.index;
|
|
1625
1909
|
const keyword = advance();
|
|
@@ -1641,7 +1925,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1641
1925
|
module.theorems.push(theorem);
|
|
1642
1926
|
module.units.push({ kind: "Theorem", theorem });
|
|
1643
1927
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1644
|
-
|
|
1928
|
+
addFactDefinition(
|
|
1929
|
+
module,
|
|
1645
1930
|
withRange(
|
|
1646
1931
|
{
|
|
1647
1932
|
kind: "Operator",
|
|
@@ -1678,7 +1963,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1678
1963
|
const parseModuleBody = (): Module => {
|
|
1679
1964
|
const location = getLocation(expect("----"));
|
|
1680
1965
|
expect("MODULE");
|
|
1681
|
-
const name =
|
|
1966
|
+
const name = expectModuleName();
|
|
1682
1967
|
expect("----");
|
|
1683
1968
|
const module: Module = {
|
|
1684
1969
|
name,
|
|
@@ -1700,7 +1985,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1700
1985
|
if (accept("EXTENDS")) {
|
|
1701
1986
|
parseSeparated(() => {
|
|
1702
1987
|
const start = cursor.index;
|
|
1703
|
-
module.extends.push(
|
|
1988
|
+
module.extends.push(expectModuleName());
|
|
1704
1989
|
module.extendsRanges.push(getRange(start));
|
|
1705
1990
|
});
|
|
1706
1991
|
}
|
|
@@ -1708,14 +1993,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1708
1993
|
const tokenIndex = cursor.index;
|
|
1709
1994
|
unitStart.index = tokenIndex;
|
|
1710
1995
|
const token = peek();
|
|
1711
|
-
if (token.kind === "ModuleEnd"
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
}
|
|
1996
|
+
if (token.kind === "ModuleEnd") {
|
|
1997
|
+
location.range = {
|
|
1998
|
+
module: openModules[0].name,
|
|
1999
|
+
start: { line: location.line, column: location.column },
|
|
2000
|
+
end: { line: token.line, column: token.endColumn },
|
|
2001
|
+
};
|
|
2002
|
+
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1719
2003
|
openModules.pop();
|
|
1720
2004
|
recursiveScopes.pop();
|
|
1721
2005
|
return module;
|
|
@@ -1734,22 +2018,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1734
2018
|
switch (token.text) {
|
|
1735
2019
|
case "CONSTANT":
|
|
1736
2020
|
case "CONSTANTS": {
|
|
1737
|
-
advance();
|
|
2021
|
+
const unitLocation = getLocation(advance());
|
|
1738
2022
|
const declarations = parseDeclarations();
|
|
1739
2023
|
module.constants.push(...declarations);
|
|
1740
|
-
|
|
2024
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2025
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1741
2026
|
continue;
|
|
1742
2027
|
}
|
|
1743
2028
|
case "VARIABLE":
|
|
1744
2029
|
case "VARIABLES": {
|
|
1745
|
-
advance();
|
|
2030
|
+
const unitLocation = getLocation(advance());
|
|
1746
2031
|
const declarations = parseSeparated(() => {
|
|
1747
2032
|
const start = cursor.index;
|
|
1748
2033
|
const location = getLocation(peek());
|
|
1749
2034
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1750
2035
|
});
|
|
1751
2036
|
module.variables.push(...declarations);
|
|
1752
|
-
|
|
2037
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2038
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1753
2039
|
continue;
|
|
1754
2040
|
}
|
|
1755
2041
|
case "ASSUME":
|
|
@@ -1759,7 +2045,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1759
2045
|
module.assumptions.push(assumption);
|
|
1760
2046
|
module.units.push({ kind: "Assumption", assumption });
|
|
1761
2047
|
if (assumption.name !== undefined) {
|
|
1762
|
-
module
|
|
2048
|
+
addFactDefinition(module, {
|
|
1763
2049
|
kind: "Operator",
|
|
1764
2050
|
name: assumption.name,
|
|
1765
2051
|
params: [],
|
|
@@ -1773,6 +2059,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1773
2059
|
}
|
|
1774
2060
|
case "USE":
|
|
1775
2061
|
case "HIDE": {
|
|
2062
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2063
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2064
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2065
|
+
}
|
|
1776
2066
|
const use = parseUseOrHide();
|
|
1777
2067
|
module.uses.push(use);
|
|
1778
2068
|
module.units.push({ kind: "Use", use });
|
|
@@ -1797,7 +2087,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1797
2087
|
);
|
|
1798
2088
|
continue;
|
|
1799
2089
|
}
|
|
1800
|
-
addDefinitionUnit(module,
|
|
2090
|
+
addDefinitionUnit(module, withRange(parseDefinitionUnit(true), tokenIndex));
|
|
1801
2091
|
continue;
|
|
1802
2092
|
}
|
|
1803
2093
|
}
|
|
@@ -1819,9 +2109,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1819
2109
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1820
2110
|
};
|
|
1821
2111
|
|
|
1822
|
-
|
|
2112
|
+
const module = parseModuleBody();
|
|
2113
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2114
|
+
// module's name from a keyword that the parser would take for one.
|
|
2115
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2116
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2117
|
+
return module;
|
|
1823
2118
|
};
|
|
1824
2119
|
|
|
1825
|
-
export const parseModule = (source: string) => parse(source,
|
|
2120
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2121
|
+
|
|
2122
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1826
2123
|
|
|
1827
|
-
export const
|
|
2124
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|