tla-ts 0.0.3-dev.e5b05b9 → 0.0.3-dev.e8bef80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +150 -48
- package/dist/index.d.ts +150 -48
- 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 +1352 -23
- package/dist/transfer.cjs +83 -43
- package/dist/transfer.js +83 -43
- package/package.json +5 -1
- package/src/ast.ts +28 -4
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -17
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1317 -428
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +226 -207
- package/src/environment.ts +221 -75
- package/src/errors.ts +130 -15
- package/src/evaluate.ts +713 -259
- package/src/exit-codes.ts +6 -24
- package/src/fingerprints.ts +1 -2
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +6 -1
- package/src/lexer.ts +122 -29
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +327 -147
- 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 +252 -87
- package/src/run-cli.ts +1162 -200
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +410 -480
- 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 +2033 -3015
- package/src/sany-report.ts +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +172 -129
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +121 -26
- package/src/successors.ts +141 -173
- 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/values.ts +664 -317
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,32 @@ 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 instance's arguments.
|
|
317
|
-
const factStart = { index: -1 };
|
|
318
359
|
// SANY reports a location in a nested module as one in the module of its file.
|
|
319
360
|
const openModules: Module[] = [];
|
|
320
361
|
|
|
@@ -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);
|
|
@@ -482,7 +527,32 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
482
527
|
ranges: names.map((item) => item.range),
|
|
483
528
|
});
|
|
484
529
|
|
|
485
|
-
|
|
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();
|
|
486
556
|
cursor.offsideColumns.push(NO_OFFSIDE);
|
|
487
557
|
const result = parse();
|
|
488
558
|
cursor.offsideColumns.pop();
|
|
@@ -573,8 +643,9 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
573
643
|
);
|
|
574
644
|
};
|
|
575
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.
|
|
576
648
|
const isStandalone = (operator: Token) => {
|
|
577
|
-
if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
|
|
578
649
|
const token = peek(1);
|
|
579
650
|
return (
|
|
580
651
|
token.kind === "End" ||
|
|
@@ -613,7 +684,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
613
684
|
if (isOperatorAlone) {
|
|
614
685
|
cursor.index++;
|
|
615
686
|
return withRange(
|
|
616
|
-
{
|
|
687
|
+
{
|
|
688
|
+
kind: "OperatorReference",
|
|
689
|
+
operator: token.text,
|
|
690
|
+
spelling: token.spelling,
|
|
691
|
+
location: getLocation(token),
|
|
692
|
+
},
|
|
617
693
|
cursor.index - 1,
|
|
618
694
|
);
|
|
619
695
|
}
|
|
@@ -623,15 +699,22 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
623
699
|
cursor.index++;
|
|
624
700
|
const location = getLocation(token);
|
|
625
701
|
if (POSTFIX_OPERATORS.has(token.text)) {
|
|
626
|
-
return
|
|
702
|
+
return withRange(
|
|
703
|
+
{ kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
|
|
704
|
+
cursor.index - 1,
|
|
705
|
+
);
|
|
627
706
|
}
|
|
628
707
|
const argument: Expression = { kind: "Identifier", name: "argument", location };
|
|
629
|
-
return
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
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
|
+
);
|
|
635
718
|
}
|
|
636
719
|
if (isAt("LAMBDA")) {
|
|
637
720
|
const start = cursor.index;
|
|
@@ -650,19 +733,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
650
733
|
start,
|
|
651
734
|
);
|
|
652
735
|
}
|
|
653
|
-
|
|
654
|
-
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
655
|
-
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
656
|
-
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
657
|
-
return argument;
|
|
736
|
+
return parseExpression();
|
|
658
737
|
};
|
|
659
738
|
|
|
660
|
-
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
661
|
-
|
|
662
739
|
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
663
740
|
|
|
664
741
|
const parseArguments = () =>
|
|
665
|
-
|
|
742
|
+
withBracketLayout(() => {
|
|
666
743
|
const start = cursor.index;
|
|
667
744
|
expect("(");
|
|
668
745
|
const args = parseSeparated(parseArgument);
|
|
@@ -689,18 +766,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
689
766
|
};
|
|
690
767
|
|
|
691
768
|
const parseBracket = (start: Token): Expression =>
|
|
692
|
-
|
|
769
|
+
withBracketLayout(() => {
|
|
693
770
|
const location = getLocation(start);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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) => {
|
|
697
777
|
const fieldStart = cursor.index;
|
|
698
778
|
const fieldLocation = getLocation(peek());
|
|
699
|
-
const name =
|
|
779
|
+
const name = readName();
|
|
700
780
|
fieldLocation.range = getRange(fieldStart);
|
|
701
|
-
expect(
|
|
781
|
+
expect(isRecord ? "|->" : ":");
|
|
702
782
|
return { name, value: parseExpression(), location: fieldLocation };
|
|
703
|
-
}
|
|
783
|
+
};
|
|
784
|
+
const fields = [
|
|
785
|
+
parseField(() => (isIdentifier ? expectIdentifier() : advance().text)),
|
|
786
|
+
...(accept(",") ? parseSeparated(() => parseField(expectIdentifier)) : []),
|
|
787
|
+
];
|
|
704
788
|
expect("]");
|
|
705
789
|
return { kind, fields, location };
|
|
706
790
|
}
|
|
@@ -738,17 +822,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
738
822
|
expect("]");
|
|
739
823
|
return { kind: "Except", function: expression, updates, location };
|
|
740
824
|
}
|
|
825
|
+
if (!isTlapm && !isAt("]_")) {
|
|
826
|
+
const args = [expression];
|
|
827
|
+
while (accept(",")) args.push(parseExpression());
|
|
828
|
+
expect("]");
|
|
829
|
+
return { kind: "Bracket", args, location };
|
|
830
|
+
}
|
|
741
831
|
expect("]_");
|
|
742
|
-
return {
|
|
743
|
-
kind: "BoxAction",
|
|
744
|
-
action: expression,
|
|
745
|
-
subscript: parseSubscript(),
|
|
746
|
-
location,
|
|
747
|
-
};
|
|
832
|
+
return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
|
|
748
833
|
});
|
|
749
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
|
+
|
|
750
846
|
const parseBrace = (start: Token): Expression =>
|
|
751
|
-
|
|
847
|
+
withBracketLayout(() => {
|
|
752
848
|
const location = getLocation(start);
|
|
753
849
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
754
850
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
@@ -763,7 +859,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
763
859
|
}
|
|
764
860
|
cursor.index = checkpoint;
|
|
765
861
|
}
|
|
766
|
-
const first =
|
|
862
|
+
const first =
|
|
863
|
+
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
767
864
|
if (accept(":")) {
|
|
768
865
|
const bounds = parseBounds();
|
|
769
866
|
expect("}");
|
|
@@ -777,7 +874,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
777
874
|
|
|
778
875
|
const parseTuple = (start: Token): Expression => {
|
|
779
876
|
const location = getLocation(start);
|
|
780
|
-
const elements =
|
|
877
|
+
const elements = withBracketLayout(() =>
|
|
781
878
|
isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
|
|
782
879
|
);
|
|
783
880
|
if (accept(">>_")) {
|
|
@@ -858,9 +955,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
858
955
|
return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
|
|
859
956
|
};
|
|
860
957
|
|
|
861
|
-
const parsePrimary = (): Expression => {
|
|
958
|
+
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
862
959
|
const start = cursor.index;
|
|
863
|
-
const isFactStart = start === factStart.index;
|
|
864
960
|
const token = advance();
|
|
865
961
|
const location = getLocation(token);
|
|
866
962
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -887,16 +983,29 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
887
983
|
const params = getNames(accept("(") ? parseSeparated(expectName) : []);
|
|
888
984
|
if (params.names.length > 0) expect(")");
|
|
889
985
|
expect("::");
|
|
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
|
+
}
|
|
890
997
|
return {
|
|
891
998
|
kind: "Label",
|
|
892
999
|
name,
|
|
893
1000
|
params: params.names,
|
|
894
1001
|
paramRanges: params.ranges,
|
|
895
|
-
body
|
|
1002
|
+
body,
|
|
896
1003
|
location,
|
|
897
1004
|
};
|
|
898
1005
|
}
|
|
899
1006
|
const hasArguments = isAt("(");
|
|
1007
|
+
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1008
|
+
return { kind: "ExceptAt", location };
|
|
900
1009
|
const args = hasArguments ? parseArguments() : [];
|
|
901
1010
|
const argumentRanges = [hasArguments ? lastArguments.range : undefined];
|
|
902
1011
|
if (!isSelectorAhead()) {
|
|
@@ -904,20 +1013,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
904
1013
|
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
905
1014
|
: { kind: "Identifier", name, location };
|
|
906
1015
|
}
|
|
907
|
-
const instance = openModules
|
|
908
|
-
.flatMap((module) => module.instances)
|
|
909
|
-
.find((candidate) => candidate.name === name);
|
|
910
|
-
const isMissingArguments =
|
|
911
|
-
instance !== undefined &&
|
|
912
|
-
!isFactStart &&
|
|
913
|
-
!isTlapm &&
|
|
914
|
-
instance.params.length !== args.length;
|
|
915
|
-
if (isMissingArguments && args.length > 0) {
|
|
916
|
-
throw createError(
|
|
917
|
-
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
918
|
-
token,
|
|
919
|
-
);
|
|
920
|
-
}
|
|
921
1016
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
922
1017
|
const selectorRanges = [getRange(start, start)];
|
|
923
1018
|
while (isSelectorAhead()) {
|
|
@@ -937,45 +1032,48 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
937
1032
|
const argumentCounts = selectors.map((selector) =>
|
|
938
1033
|
"args" in selector ? selector.args.length : 0,
|
|
939
1034
|
);
|
|
940
|
-
const argumentsRange = argumentRanges.
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
:
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
1035
|
+
const argumentsRange = argumentRanges.at(-1) ?? getRange(start);
|
|
1036
|
+
return names.length < selectors.length
|
|
1037
|
+
? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
|
|
1038
|
+
: qualifiedArgs.length === 0
|
|
1039
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
1040
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
1041
|
+
? {
|
|
1042
|
+
kind: "Apply",
|
|
1043
|
+
operator,
|
|
1044
|
+
args: qualifiedArgs,
|
|
1045
|
+
argumentCounts,
|
|
1046
|
+
argumentsRange,
|
|
1047
|
+
selectorRanges,
|
|
1048
|
+
argumentRanges,
|
|
1049
|
+
location,
|
|
1050
|
+
}
|
|
1051
|
+
: {
|
|
1052
|
+
kind: "Apply",
|
|
1053
|
+
operator,
|
|
1054
|
+
args: qualifiedArgs,
|
|
1055
|
+
argumentsRange,
|
|
1056
|
+
selectorRanges,
|
|
1057
|
+
argumentRanges,
|
|
1058
|
+
location,
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1062
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
1063
|
+
return {
|
|
1064
|
+
kind: "Apply",
|
|
1065
|
+
operator: token.text,
|
|
1066
|
+
spelling: token.spelling,
|
|
1067
|
+
args: parseArguments(),
|
|
1068
|
+
location,
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
|
|
1072
|
+
return { kind: "Identifier", name: token.text, spelling: token.spelling, location };
|
|
973
1073
|
}
|
|
974
|
-
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
975
|
-
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
976
1074
|
switch (token.text) {
|
|
977
1075
|
case "(": {
|
|
978
|
-
const expression =
|
|
1076
|
+
const expression = withBracketLayout(() => parseExpression());
|
|
979
1077
|
expect(")");
|
|
980
1078
|
return expression;
|
|
981
1079
|
}
|
|
@@ -1031,7 +1129,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1031
1129
|
case "WF_":
|
|
1032
1130
|
case "SF_": {
|
|
1033
1131
|
const subscript = parseSubscript();
|
|
1034
|
-
const action =
|
|
1132
|
+
const action = withBracketLayout(() => {
|
|
1035
1133
|
expect("(");
|
|
1036
1134
|
const expression = parseExpression();
|
|
1037
1135
|
expect(")");
|
|
@@ -1048,7 +1146,25 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1048
1146
|
throw createUnexpected(`Unexpected ${describe(token)}`, token);
|
|
1049
1147
|
};
|
|
1050
1148
|
|
|
1149
|
+
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1150
|
+
// prefix operator.
|
|
1151
|
+
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1152
|
+
operators.last = operators.preceding = { low, high };
|
|
1153
|
+
const operand = parseExpression(high + 1);
|
|
1154
|
+
const next = peek();
|
|
1155
|
+
const nextHigh =
|
|
1156
|
+
next.text === "'" || POSTFIX_OPERATORS.has(next.text)
|
|
1157
|
+
? POSTFIX_PRECEDENCE.high
|
|
1158
|
+
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1159
|
+
if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1160
|
+
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1161
|
+
}
|
|
1162
|
+
return operand;
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1051
1165
|
const parsePrefix = (): Expression => {
|
|
1166
|
+
const preceding = operators.preceding;
|
|
1167
|
+
operators.preceding = undefined;
|
|
1052
1168
|
const token = peek();
|
|
1053
1169
|
const location = getLocation(token);
|
|
1054
1170
|
if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
|
|
@@ -1058,35 +1174,46 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1058
1174
|
switch (token.text) {
|
|
1059
1175
|
case "~":
|
|
1060
1176
|
advance();
|
|
1061
|
-
return {
|
|
1177
|
+
return {
|
|
1178
|
+
kind: "Apply",
|
|
1179
|
+
operator: "~",
|
|
1180
|
+
args: [parsePrefixOperand(token, 4, 4)],
|
|
1181
|
+
location,
|
|
1182
|
+
};
|
|
1062
1183
|
case "-":
|
|
1063
1184
|
advance();
|
|
1064
1185
|
return {
|
|
1065
1186
|
kind: "Apply",
|
|
1066
1187
|
operator: "-.",
|
|
1067
|
-
args: [
|
|
1188
|
+
args: [parsePrefixOperand(token, NEGATION_PRECEDENCE, NEGATION_PRECEDENCE)],
|
|
1068
1189
|
location,
|
|
1069
1190
|
};
|
|
1070
1191
|
case "[]":
|
|
1071
1192
|
advance();
|
|
1072
|
-
return { kind: "Always", expression:
|
|
1193
|
+
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1073
1194
|
case "<>":
|
|
1074
1195
|
advance();
|
|
1075
|
-
return { kind: "Eventually", expression:
|
|
1196
|
+
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1076
1197
|
case "ENABLED":
|
|
1077
1198
|
advance();
|
|
1078
|
-
return { kind: "Enabled", expression:
|
|
1199
|
+
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1079
1200
|
case "UNCHANGED":
|
|
1080
1201
|
advance();
|
|
1081
|
-
return { kind: "Unchanged", expression:
|
|
1202
|
+
return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1082
1203
|
case "SUBSET":
|
|
1083
1204
|
case "UNION":
|
|
1084
1205
|
case "DOMAIN":
|
|
1085
1206
|
advance();
|
|
1086
|
-
return {
|
|
1207
|
+
return {
|
|
1208
|
+
kind: "Apply",
|
|
1209
|
+
operator: token.text,
|
|
1210
|
+
// tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
|
|
1211
|
+
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1212
|
+
location,
|
|
1213
|
+
};
|
|
1087
1214
|
}
|
|
1088
1215
|
}
|
|
1089
|
-
return parsePrimary();
|
|
1216
|
+
return parsePrimary(preceding);
|
|
1090
1217
|
};
|
|
1091
1218
|
|
|
1092
1219
|
const parseExpression = (minimumPrecedence = 0): Expression => {
|
|
@@ -1102,21 +1229,30 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1102
1229
|
const location = getLocation(token);
|
|
1103
1230
|
if (token.text === "'" && minimumPrecedence <= 15) {
|
|
1104
1231
|
advance();
|
|
1232
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1105
1233
|
state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
|
|
1106
1234
|
continue;
|
|
1107
1235
|
}
|
|
1108
1236
|
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1109
1237
|
advance();
|
|
1238
|
+
operators.last = POSTFIX_PRECEDENCE;
|
|
1110
1239
|
state.left = withRange(
|
|
1111
|
-
{
|
|
1240
|
+
{
|
|
1241
|
+
kind: "Apply",
|
|
1242
|
+
operator: token.text,
|
|
1243
|
+
spelling: token.spelling,
|
|
1244
|
+
args: [state.left],
|
|
1245
|
+
location,
|
|
1246
|
+
},
|
|
1112
1247
|
start,
|
|
1113
1248
|
);
|
|
1114
1249
|
continue;
|
|
1115
1250
|
}
|
|
1116
1251
|
if (token.text === "[") {
|
|
1117
1252
|
advance();
|
|
1118
|
-
const args =
|
|
1119
|
-
|
|
1253
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1254
|
+
withBracketLayout(() => expect("]"));
|
|
1255
|
+
operators.last = FUNCTION_PRECEDENCE;
|
|
1120
1256
|
state.left = withRange(
|
|
1121
1257
|
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1122
1258
|
start,
|
|
@@ -1147,21 +1283,33 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1147
1283
|
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1148
1284
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1149
1285
|
}
|
|
1286
|
+
operators.last = operators.preceding = infix;
|
|
1150
1287
|
const right = parseExpression(infix.high + 1);
|
|
1151
1288
|
const left = state.left;
|
|
1152
1289
|
if (token.text === "/\\" || token.text === "\\/") {
|
|
1153
1290
|
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1291
|
+
const prefixEnds = [getRange(start).end];
|
|
1154
1292
|
state.left =
|
|
1155
1293
|
left.kind === kind && previous?.text === token.text
|
|
1156
|
-
? {
|
|
1157
|
-
|
|
1294
|
+
? {
|
|
1295
|
+
...left,
|
|
1296
|
+
operands: [...left.operands, right],
|
|
1297
|
+
prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
|
|
1298
|
+
}
|
|
1299
|
+
: { kind, operands: [left, right], prefixEnds, location };
|
|
1158
1300
|
} else if (token.text === "\\X") {
|
|
1159
1301
|
state.left =
|
|
1160
1302
|
left.kind === "Product" && previous?.text === "\\X"
|
|
1161
1303
|
? { ...left, factors: [...left.factors, right] }
|
|
1162
1304
|
: { kind: "Product", factors: [left, right], location };
|
|
1163
1305
|
} else {
|
|
1164
|
-
state.left = {
|
|
1306
|
+
state.left = {
|
|
1307
|
+
kind: "Apply",
|
|
1308
|
+
operator: token.text,
|
|
1309
|
+
spelling: token.spelling,
|
|
1310
|
+
args: [left, right],
|
|
1311
|
+
location,
|
|
1312
|
+
};
|
|
1165
1313
|
}
|
|
1166
1314
|
state.left = withRange(state.left, start);
|
|
1167
1315
|
state.previous = { ...infix, text: token.text };
|
|
@@ -1224,7 +1372,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1224
1372
|
expect("==");
|
|
1225
1373
|
return parseOperator(operator, [toParam(left), toParam(right)]);
|
|
1226
1374
|
}
|
|
1227
|
-
const isPrefixDefinition =
|
|
1375
|
+
const isPrefixDefinition =
|
|
1376
|
+
token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
|
|
1228
1377
|
const isPostfixDefinition =
|
|
1229
1378
|
token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
|
|
1230
1379
|
if (isPrefixDefinition || isPostfixDefinition) {
|
|
@@ -1237,7 +1386,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1237
1386
|
}
|
|
1238
1387
|
const name = expectIdentifier();
|
|
1239
1388
|
if (accept("[")) {
|
|
1240
|
-
const bounds =
|
|
1389
|
+
const bounds = withBracketLayout(() => parseBounds());
|
|
1241
1390
|
expect("]");
|
|
1242
1391
|
expect("==");
|
|
1243
1392
|
return {
|
|
@@ -1268,11 +1417,11 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1268
1417
|
): Instance => {
|
|
1269
1418
|
const start = cursor.index;
|
|
1270
1419
|
expect("INSTANCE");
|
|
1271
|
-
const module =
|
|
1420
|
+
const module = expectModuleName();
|
|
1272
1421
|
const substitutions = accept("WITH")
|
|
1273
1422
|
? parseSeparated(() => {
|
|
1274
1423
|
const targetStart = cursor.index;
|
|
1275
|
-
const target =
|
|
1424
|
+
const target = expectSubstitutionTarget();
|
|
1276
1425
|
const nameRange = getRange(targetStart);
|
|
1277
1426
|
expect("<-");
|
|
1278
1427
|
return { name: target, nameRange, expression: parseArgument() };
|
|
@@ -1341,7 +1490,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1341
1490
|
const token = peek();
|
|
1342
1491
|
const next = peek(1);
|
|
1343
1492
|
if (token.kind !== "Identifier") {
|
|
1344
|
-
return
|
|
1493
|
+
return (
|
|
1494
|
+
token.kind !== "String" &&
|
|
1495
|
+
DEFINABLE_PREFIX_OPERATORS.has(token.text) &&
|
|
1496
|
+
next.kind === "Identifier" &&
|
|
1497
|
+
isAt("==", 2)
|
|
1498
|
+
);
|
|
1345
1499
|
}
|
|
1346
1500
|
if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
|
|
1347
1501
|
if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
|
|
@@ -1433,7 +1587,6 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1433
1587
|
// start of an expression.
|
|
1434
1588
|
const parseFact = (): Fact => {
|
|
1435
1589
|
const start = cursor.index;
|
|
1436
|
-
factStart.index = start;
|
|
1437
1590
|
const token = peek();
|
|
1438
1591
|
if (
|
|
1439
1592
|
token.kind === "Operator" &&
|
|
@@ -1442,7 +1595,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1442
1595
|
) {
|
|
1443
1596
|
cursor.index++;
|
|
1444
1597
|
return withRange(
|
|
1445
|
-
{
|
|
1598
|
+
{
|
|
1599
|
+
kind: "OperatorReference",
|
|
1600
|
+
operator: token.text,
|
|
1601
|
+
spelling: token.spelling,
|
|
1602
|
+
location: getLocation(token),
|
|
1603
|
+
},
|
|
1446
1604
|
start,
|
|
1447
1605
|
);
|
|
1448
1606
|
}
|
|
@@ -1495,7 +1653,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1495
1653
|
const parseDefs = () => {
|
|
1496
1654
|
if (isTlapm) return parseSeparated(parseDefinable);
|
|
1497
1655
|
const defs = parseSeparated(parseFact);
|
|
1498
|
-
const invalid =
|
|
1656
|
+
const invalid =
|
|
1657
|
+
mode === "tlc"
|
|
1658
|
+
? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
|
|
1659
|
+
: undefined;
|
|
1499
1660
|
if (invalid) {
|
|
1500
1661
|
throw createError("DEF clause entry should describe a defined operator", invalid.location);
|
|
1501
1662
|
}
|
|
@@ -1520,7 +1681,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1520
1681
|
location: getLocation(keyword),
|
|
1521
1682
|
};
|
|
1522
1683
|
const invalid =
|
|
1523
|
-
use.kind === "Hide" &&
|
|
1684
|
+
use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1524
1685
|
if (invalid.length > 0) {
|
|
1525
1686
|
throw createSanyError(
|
|
1526
1687
|
invalid.map((fact) => ({
|
|
@@ -1615,7 +1776,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1615
1776
|
const token = peek();
|
|
1616
1777
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1617
1778
|
if (isAt("INSTANCE")) {
|
|
1618
|
-
|
|
1779
|
+
const start = cursor.index;
|
|
1780
|
+
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1619
1781
|
}
|
|
1620
1782
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1621
1783
|
const start = cursor.index;
|
|
@@ -1726,6 +1888,13 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1726
1888
|
return withRange(proof, start);
|
|
1727
1889
|
};
|
|
1728
1890
|
|
|
1891
|
+
// SANY binds the name of a theorem or assumption that a module already defines to the first
|
|
1892
|
+
// definition, warning of the second.
|
|
1893
|
+
const addFactDefinition = (module: Module, definition: Definition) => {
|
|
1894
|
+
if (module.definitions.some((other) => other.name === definition.name)) return;
|
|
1895
|
+
module.definitions.push(definition);
|
|
1896
|
+
};
|
|
1897
|
+
|
|
1729
1898
|
const parseTheorem = (module: Module) => {
|
|
1730
1899
|
const start = cursor.index;
|
|
1731
1900
|
const keyword = advance();
|
|
@@ -1747,7 +1916,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1747
1916
|
module.theorems.push(theorem);
|
|
1748
1917
|
module.units.push({ kind: "Theorem", theorem });
|
|
1749
1918
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
1750
|
-
|
|
1919
|
+
addFactDefinition(
|
|
1920
|
+
module,
|
|
1751
1921
|
withRange(
|
|
1752
1922
|
{
|
|
1753
1923
|
kind: "Operator",
|
|
@@ -1784,7 +1954,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1784
1954
|
const parseModuleBody = (): Module => {
|
|
1785
1955
|
const location = getLocation(expect("----"));
|
|
1786
1956
|
expect("MODULE");
|
|
1787
|
-
const name =
|
|
1957
|
+
const name = expectModuleName();
|
|
1788
1958
|
expect("----");
|
|
1789
1959
|
const module: Module = {
|
|
1790
1960
|
name,
|
|
@@ -1806,7 +1976,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1806
1976
|
if (accept("EXTENDS")) {
|
|
1807
1977
|
parseSeparated(() => {
|
|
1808
1978
|
const start = cursor.index;
|
|
1809
|
-
module.extends.push(
|
|
1979
|
+
module.extends.push(expectModuleName());
|
|
1810
1980
|
module.extendsRanges.push(getRange(start));
|
|
1811
1981
|
});
|
|
1812
1982
|
}
|
|
@@ -1814,15 +1984,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1814
1984
|
const tokenIndex = cursor.index;
|
|
1815
1985
|
unitStart.index = tokenIndex;
|
|
1816
1986
|
const token = peek();
|
|
1817
|
-
if (token.kind === "ModuleEnd"
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
};
|
|
1824
|
-
}
|
|
1825
|
-
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1987
|
+
if (token.kind === "ModuleEnd") {
|
|
1988
|
+
location.range = {
|
|
1989
|
+
module: openModules[0].name,
|
|
1990
|
+
start: { line: location.line, column: location.column },
|
|
1991
|
+
end: { line: token.line, column: token.endColumn },
|
|
1992
|
+
};
|
|
1826
1993
|
openModules.pop();
|
|
1827
1994
|
recursiveScopes.pop();
|
|
1828
1995
|
return module;
|
|
@@ -1841,22 +2008,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1841
2008
|
switch (token.text) {
|
|
1842
2009
|
case "CONSTANT":
|
|
1843
2010
|
case "CONSTANTS": {
|
|
1844
|
-
advance();
|
|
2011
|
+
const unitLocation = getLocation(advance());
|
|
1845
2012
|
const declarations = parseDeclarations();
|
|
1846
2013
|
module.constants.push(...declarations);
|
|
1847
|
-
|
|
2014
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2015
|
+
module.units.push({ kind: "Constants", declarations, location: unitLocation });
|
|
1848
2016
|
continue;
|
|
1849
2017
|
}
|
|
1850
2018
|
case "VARIABLE":
|
|
1851
2019
|
case "VARIABLES": {
|
|
1852
|
-
advance();
|
|
2020
|
+
const unitLocation = getLocation(advance());
|
|
1853
2021
|
const declarations = parseSeparated(() => {
|
|
1854
2022
|
const start = cursor.index;
|
|
1855
2023
|
const location = getLocation(peek());
|
|
1856
2024
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
1857
2025
|
});
|
|
1858
2026
|
module.variables.push(...declarations);
|
|
1859
|
-
|
|
2027
|
+
unitLocation.range = getRange(tokenIndex);
|
|
2028
|
+
module.units.push({ kind: "Variables", declarations, location: unitLocation });
|
|
1860
2029
|
continue;
|
|
1861
2030
|
}
|
|
1862
2031
|
case "ASSUME":
|
|
@@ -1866,7 +2035,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1866
2035
|
module.assumptions.push(assumption);
|
|
1867
2036
|
module.units.push({ kind: "Assumption", assumption });
|
|
1868
2037
|
if (assumption.name !== undefined) {
|
|
1869
|
-
module
|
|
2038
|
+
addFactDefinition(module, {
|
|
1870
2039
|
kind: "Operator",
|
|
1871
2040
|
name: assumption.name,
|
|
1872
2041
|
params: [],
|
|
@@ -1880,6 +2049,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1880
2049
|
}
|
|
1881
2050
|
case "USE":
|
|
1882
2051
|
case "HIDE": {
|
|
2052
|
+
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2053
|
+
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2054
|
+
throw createUnexpected(`Expected a module unit but found ${describe(token)}`);
|
|
2055
|
+
}
|
|
1883
2056
|
const use = parseUseOrHide();
|
|
1884
2057
|
module.uses.push(use);
|
|
1885
2058
|
module.units.push({ kind: "Use", use });
|
|
@@ -1926,9 +2099,16 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1926
2099
|
module.units.push({ kind: "Definition", definition: unit });
|
|
1927
2100
|
};
|
|
1928
2101
|
|
|
1929
|
-
|
|
2102
|
+
const module = parseModuleBody();
|
|
2103
|
+
// SANY's lexer marks where each definition begins before its parser runs, which can split a
|
|
2104
|
+
// module's name from a keyword that the parser would take for one.
|
|
2105
|
+
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2106
|
+
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2107
|
+
return module;
|
|
1930
2108
|
};
|
|
1931
2109
|
|
|
1932
|
-
export const parseModule = (source: string) => parse(source,
|
|
2110
|
+
export const parseModule = (source: string) => parse(source, "tlc");
|
|
2111
|
+
|
|
2112
|
+
export const parseTlapmModule = (source: string) => parse(source, "tlapm");
|
|
1933
2113
|
|
|
1934
|
-
export const
|
|
2114
|
+
export const parseSanyModule = (source: string) => parse(source, "sany");
|