tla-ts 0.0.4-dev.c4929d7 → 0.0.4-dev.ca40dfe
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/README.md +31 -6
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +27 -27
- package/dist/deciders.js +34 -34
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +12 -14
- package/dist/index.d.ts +12 -14
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +37 -27
- package/dist/prove.js +39 -29
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +76 -86
- package/dist/transfer.js +73 -83
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +1 -1
- package/src/ast.ts +2 -2
- package/src/bags.ts +22 -23
- package/src/builtins.ts +16 -8
- package/src/check.ts +131 -1154
- package/src/{cli-options.ts → cli/options.ts} +54 -9
- package/src/cli/run.ts +63 -0
- package/src/cli/shared.ts +106 -0
- package/src/cli/tlapm.ts +374 -0
- package/src/cli/tlc.ts +941 -0
- package/src/cli/xml-exporter.ts +221 -0
- package/src/cli.ts +1 -1
- package/src/codegen.ts +17 -15
- package/src/config.ts +5 -5
- package/src/dependencies.ts +4 -6
- package/src/environment.ts +55 -60
- package/src/errors.ts +12 -36
- package/src/evaluate.ts +81 -101
- package/src/exit-codes.ts +1 -1
- package/src/fingerprints.ts +3 -3
- package/src/instances.ts +56 -46
- package/src/java-overrides.ts +79 -41
- package/src/json.ts +31 -36
- package/src/levels.ts +10 -13
- package/src/{live-check.ts → liveness-checker.ts} +169 -142
- package/src/liveness.ts +38 -34
- package/src/obligations/action.ts +9 -45
- package/src/obligations/{anon.ts → anonymize.ts} +13 -25
- package/src/obligations/equality.ts +260 -0
- package/src/obligations/expression.ts +42 -517
- package/src/obligations/generate.ts +88 -778
- package/src/obligations/index.ts +20 -22
- package/src/obligations/levels.ts +36 -41
- package/src/obligations/module.ts +88 -708
- package/src/obligations/normalize.ts +554 -0
- package/src/obligations/operators.ts +176 -0
- package/src/obligations/pragma.ts +21 -15
- package/src/obligations/printer.ts +81 -188
- package/src/obligations/proof.ts +0 -1
- package/src/obligations/regroup.ts +100 -0
- package/src/obligations/simplify.ts +668 -0
- package/src/obligations/source.ts +25 -131
- package/src/obligations/{deref.ts → subexpression.ts} +48 -56
- package/src/obligations/substitution.ts +261 -0
- package/src/obligations/temporal.ts +28 -26
- package/src/obligations/tuples.ts +37 -53
- package/src/parallel-worker.ts +37 -33
- package/src/parallel.ts +30 -24
- package/src/parser.ts +67 -74
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
- package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
- package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
- package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
- package/src/prove/decide-pool.ts +7 -12
- package/src/{obligations/pickle.ts → prove/first-order.ts} +79 -2
- package/src/prove/isabelle/decide.ts +3 -6
- package/src/prove/isabelle/encode.ts +35 -93
- package/src/prove/isabelle/isar/inner-syntax.ts +1 -1
- package/src/prove/isabelle/isar/method.ts +3 -3
- package/src/prove/isabelle/isar/type-check.ts +1 -1
- package/src/prove/isabelle/provers/blast/blast.ts +1 -1
- package/src/prove/isabelle/provers/blast/branch.ts +1 -1
- package/src/prove/isabelle/provers/blast/term.ts +3 -4
- package/src/prove/isabelle/provers/clasimp.ts +1 -1
- package/src/prove/isabelle/provers/classical.ts +3 -3
- package/src/prove/isabelle/provers/hypsubst.ts +1 -9
- package/src/prove/isabelle/pure/conv.ts +1 -1
- package/src/prove/isabelle/pure/drule.ts +2 -2
- package/src/prove/isabelle/pure/envir.ts +3 -35
- package/src/prove/isabelle/pure/logic.ts +4 -76
- package/src/prove/isabelle/pure/name.ts +1 -13
- package/src/prove/isabelle/pure/net.ts +2 -49
- package/src/prove/isabelle/pure/pattern.ts +3 -22
- package/src/prove/isabelle/pure/raw-simplifier.ts +3 -18
- package/src/prove/isabelle/pure/seq.ts +0 -23
- package/src/prove/isabelle/pure/simplifier.ts +2 -8
- package/src/prove/isabelle/pure/table.ts +1 -8
- package/src/prove/isabelle/pure/tactic.ts +5 -113
- package/src/prove/isabelle/pure/tactical.ts +1 -84
- package/src/prove/isabelle/pure/term-ord.ts +1 -55
- package/src/prove/isabelle/pure/term.ts +8 -128
- package/src/prove/isabelle/pure/thm.ts +4 -27
- package/src/prove/isabelle/pure/type.ts +0 -12
- package/src/prove/isabelle/pure/unify.ts +1 -1
- package/src/prove/isabelle/tla/classical-setup.ts +1 -1
- package/src/prove/ocaml.ts +1 -1
- package/src/prove/prep/action.ts +23 -36
- package/src/prove/prep/enabled-axioms.ts +2 -2
- package/src/prove/prep/fingerprint.ts +3 -3
- package/src/prove/prep/level-comparison.ts +9 -37
- package/src/prove/prep/md5.ts +2 -9
- package/src/prove/prep/normalize.ts +309 -0
- package/src/prove/prove-worker.ts +2 -5
- package/src/prove/prove.ts +98 -111
- package/src/prove/ptl/encode.ts +19 -31
- package/src/prove/smt/axioms.ts +45 -58
- package/src/prove/smt/encode.ts +124 -15
- package/src/prove/smt/instances.ts +2 -3
- package/src/prove/smt/rewrite.ts +24 -31
- package/src/prove/smt/solver/ast-util.ts +0 -56
- package/src/prove/smt/solver/case-split-queue.ts +1 -1
- package/src/prove/smt/solver/enode.ts +1 -1
- package/src/prove/smt/solver/lp/dep-intervals.ts +0 -14
- package/src/prove/smt/solver/model-macro-solver.ts +1 -1
- package/src/prove/smt/solver/nlsat/algebraic.ts +3 -3
- package/src/prove/smt/solver/nlsat/atoms.ts +1 -1
- package/src/prove/smt/solver/nlsat/explain.ts +1 -1
- package/src/prove/smt/solver/nlsat/interval-set.ts +1 -1
- package/src/prove/smt/solver/nlsat/levelwise.ts +4 -7
- package/src/prove/smt/solver/nlsat/polynomial.ts +4 -4
- package/src/prove/smt/solver/nlsat/upolynomial.ts +2 -2
- package/src/prove/smt/solver/pattern-inference.ts +1 -1
- package/src/prove/smt/solver/relevancy.ts +2 -2
- package/src/prove/smt/solver/rewriter.ts +1 -1
- package/src/prove/smt/solver/solver.ts +2 -2
- package/src/{tlapm-cache.ts → prove/tlapm/cache.ts} +11 -11
- package/src/{ocaml-marshal.ts → prove/tlapm/ocaml-marshal.ts} +1 -1
- package/src/prove/verdict.ts +0 -5
- package/src/prove/zenon/decide.ts +5 -11
- package/src/prove/zenon/encode.ts +21 -80
- package/src/prove/zenon/eqrel.ts +1 -1
- package/src/prove/zenon/equality-rules.ts +1 -1
- package/src/prove/zenon/expr.ts +2 -2
- package/src/prove/zenon/index-table.ts +1 -1
- package/src/prove/zenon/isar.ts +1 -1
- package/src/prove/zenon/llproof.ts +1 -1
- package/src/prove/zenon/mltoll.ts +1 -1
- package/src/prove/zenon/node.ts +1 -1
- package/src/prove/zenon/search.ts +1 -1
- package/src/prove/zenon/tla/terms.ts +1 -20
- package/src/prove/zenon/tla/tla.ts +1 -1
- package/src/randomization.ts +19 -24
- package/src/resolve.ts +15 -22
- package/src/runtime.ts +8 -8
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +38 -55
- package/src/{sany-levels.ts → sany/levels.ts} +45 -48
- package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +16 -17
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
- package/src/{sany-source.ts → sany/source.ts} +6 -7
- package/src/{sany-xml.ts → sany/xml.ts} +5 -6
- package/src/search.ts +969 -0
- package/src/spec-description.ts +26 -20
- package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
- package/src/standard-modules.ts +16 -20
- package/src/state-graph.ts +9 -8
- package/src/substitution-levels.ts +2 -2
- package/src/successors.ts +16 -19
- package/src/symmetry.ts +1 -1
- package/src/tableau.ts +6 -7
- package/src/tlc/fingerprint.ts +214 -0
- package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +3 -213
- package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
- package/src/transfer.ts +32 -32
- package/src/values.ts +72 -67
- package/src/worker.ts +1 -4
- package/dist/run-cli.cjs +0 -45
- package/dist/run-cli.js +0 -45
- package/src/canonical.ts +0 -40
- package/src/obligations/ctx.ts +0 -64
- package/src/prove/prep/defs.ts +0 -33
- package/src/prove/prep/elab.ts +0 -156
- package/src/prove/prep/enabled.ts +0 -43
- package/src/prove/prep/flatten.ts +0 -70
- package/src/prove/prep/trivial.ts +0 -22
- package/src/prove/smt/axiomatize.ts +0 -69
- package/src/prove/smt/simplify.ts +0 -57
- package/src/run-cli.ts +0 -1806
- package/src/standard-ranges.ts +0 -116
- package/src/value-sources.ts +0 -29
- /package/src/prove/prep/{pragma.ts → methods.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
- /package/src/{tlc-version.ts → tlc/version.ts} +0 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// tlapm's Optable: each operator's precedence range and fixity, by every spelling it has.
|
|
2
|
+
|
|
3
|
+
import type { Builtin } from "./expression";
|
|
4
|
+
|
|
5
|
+
export type Fixity = "Nonfix" | "Prefix" | "Postfix" | "InfixLeft" | "InfixNon" | "InfixRight";
|
|
6
|
+
|
|
7
|
+
export interface Operator {
|
|
8
|
+
name: string;
|
|
9
|
+
low: number;
|
|
10
|
+
high: number;
|
|
11
|
+
fixity: Fixity;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const OPERATORS: [string, number, number, Fixity, string[]][] = [
|
|
15
|
+
["=>", 1, 1, "InfixNon", []],
|
|
16
|
+
["<=>", 2, 2, "InfixNon", ["\\equiv"]],
|
|
17
|
+
["/\\", 3, 3, "InfixLeft", ["\\land"]],
|
|
18
|
+
["\\/", 3, 3, "InfixLeft", ["\\lor"]],
|
|
19
|
+
["~", 4, 4, "Prefix", ["\\neg", "\\lnot"]],
|
|
20
|
+
["=", 5, 5, "InfixNon", []],
|
|
21
|
+
["#", 5, 5, "InfixNon", ["/="]],
|
|
22
|
+
["SUBSET", 8, 8, "Prefix", []],
|
|
23
|
+
["UNION", 8, 8, "Prefix", []],
|
|
24
|
+
["DOMAIN", 9, 9, "Prefix", []],
|
|
25
|
+
["\\subseteq", 5, 5, "InfixNon", []],
|
|
26
|
+
["\\in", 5, 5, "InfixNon", []],
|
|
27
|
+
["\\notin", 5, 5, "InfixNon", []],
|
|
28
|
+
["\\", 8, 8, "InfixNon", []],
|
|
29
|
+
["\\cap", 8, 8, "InfixLeft", ["\\intersect"]],
|
|
30
|
+
["\\cup", 8, 8, "InfixLeft", ["\\union"]],
|
|
31
|
+
["\\X", 10, 13, "Prefix", ["\\times"]],
|
|
32
|
+
["'", 15, 15, "Postfix", []],
|
|
33
|
+
["~>", 2, 2, "InfixNon", ["\\leadsto"]],
|
|
34
|
+
["ENABLED", 4, 15, "Prefix", []],
|
|
35
|
+
["UNCHANGED", 4, 15, "Prefix", []],
|
|
36
|
+
["\\cdot", 5, 14, "InfixLeft", []],
|
|
37
|
+
["-+->", 2, 2, "InfixNon", []],
|
|
38
|
+
["[]", 4, 15, "Prefix", []],
|
|
39
|
+
["<>", 4, 15, "Prefix", []],
|
|
40
|
+
["^", 14, 14, "InfixNon", []],
|
|
41
|
+
["/", 13, 13, "InfixNon", []],
|
|
42
|
+
["*", 13, 13, "InfixLeft", []],
|
|
43
|
+
["-.", 12, 12, "Prefix", ["-"]],
|
|
44
|
+
["-", 11, 11, "InfixLeft", []],
|
|
45
|
+
["+", 10, 10, "InfixLeft", []],
|
|
46
|
+
["^+", 15, 15, "Postfix", []],
|
|
47
|
+
["^*", 15, 15, "Postfix", []],
|
|
48
|
+
["^#", 15, 15, "Postfix", []],
|
|
49
|
+
["<", 5, 5, "InfixNon", []],
|
|
50
|
+
["=<", 5, 5, "InfixNon", ["<=", "\\leq"]],
|
|
51
|
+
[">", 5, 5, "InfixNon", []],
|
|
52
|
+
[">=", 5, 5, "InfixNon", ["\\geq"]],
|
|
53
|
+
["...", 9, 9, "InfixNon", []],
|
|
54
|
+
["..", 9, 9, "InfixNon", []],
|
|
55
|
+
["|", 10, 11, "InfixLeft", []],
|
|
56
|
+
["||", 10, 11, "InfixLeft", []],
|
|
57
|
+
["&&", 13, 13, "InfixLeft", []],
|
|
58
|
+
["&", 13, 13, "InfixLeft", []],
|
|
59
|
+
["$$", 9, 13, "InfixLeft", []],
|
|
60
|
+
["$", 9, 13, "InfixLeft", []],
|
|
61
|
+
["??", 9, 13, "InfixLeft", []],
|
|
62
|
+
["%%", 10, 11, "InfixLeft", []],
|
|
63
|
+
["%", 10, 11, "InfixLeft", ["\\mod"]],
|
|
64
|
+
["##", 9, 13, "InfixLeft", []],
|
|
65
|
+
["++", 10, 10, "InfixLeft", []],
|
|
66
|
+
["--", 11, 11, "InfixLeft", []],
|
|
67
|
+
["**", 13, 13, "InfixLeft", []],
|
|
68
|
+
["//", 13, 13, "InfixNon", []],
|
|
69
|
+
["^^", 14, 14, "InfixNon", []],
|
|
70
|
+
["@@", 6, 6, "InfixLeft", []],
|
|
71
|
+
["!!", 9, 13, "InfixNon", []],
|
|
72
|
+
["|-", 5, 5, "InfixNon", []],
|
|
73
|
+
["|=", 5, 5, "InfixNon", []],
|
|
74
|
+
["-|", 5, 5, "InfixNon", []],
|
|
75
|
+
["=|", 5, 5, "InfixNon", []],
|
|
76
|
+
["<:", 7, 7, "InfixNon", []],
|
|
77
|
+
[":>", 7, 7, "InfixNon", []],
|
|
78
|
+
[":=", 5, 5, "InfixNon", []],
|
|
79
|
+
["::=", 5, 5, "InfixNon", []],
|
|
80
|
+
["(+)", 10, 10, "InfixLeft", ["\\oplus"]],
|
|
81
|
+
["(-)", 11, 11, "InfixLeft", ["\\ominus"]],
|
|
82
|
+
["(.)", 13, 13, "InfixLeft", ["\\odot"]],
|
|
83
|
+
["(/)", 13, 13, "InfixNon", ["\\oslash"]],
|
|
84
|
+
["(\\X)", 13, 13, "InfixLeft", ["\\otimes"]],
|
|
85
|
+
["\\uplus", 9, 13, "InfixLeft", []],
|
|
86
|
+
["\\sqcap", 9, 13, "InfixLeft", []],
|
|
87
|
+
["\\sqcup", 9, 13, "InfixLeft", []],
|
|
88
|
+
["\\div", 13, 13, "InfixNon", []],
|
|
89
|
+
["\\wr", 9, 14, "InfixNon", []],
|
|
90
|
+
["\\star", 13, 13, "InfixLeft", []],
|
|
91
|
+
["\\o", 13, 13, "InfixLeft", ["\\circ"]],
|
|
92
|
+
["\\bigcirc", 13, 13, "InfixLeft", []],
|
|
93
|
+
["\\bullet", 13, 13, "InfixLeft", []],
|
|
94
|
+
["\\prec", 5, 5, "InfixNon", []],
|
|
95
|
+
["\\succ", 5, 5, "InfixNon", []],
|
|
96
|
+
["\\preceq", 5, 5, "InfixNon", []],
|
|
97
|
+
["\\succeq", 5, 5, "InfixNon", []],
|
|
98
|
+
["\\sim", 5, 5, "InfixNon", []],
|
|
99
|
+
["\\simeq", 5, 5, "InfixNon", []],
|
|
100
|
+
["\\ll", 5, 5, "InfixNon", []],
|
|
101
|
+
["\\gg", 5, 5, "InfixNon", []],
|
|
102
|
+
["\\asymp", 5, 5, "InfixNon", []],
|
|
103
|
+
["\\subset", 5, 5, "InfixNon", []],
|
|
104
|
+
["\\supset", 5, 5, "InfixNon", []],
|
|
105
|
+
["\\supseteq", 5, 5, "InfixNon", []],
|
|
106
|
+
["\\approx", 5, 5, "InfixNon", []],
|
|
107
|
+
["\\cong", 5, 5, "InfixNon", []],
|
|
108
|
+
["\\sqsubset", 5, 5, "InfixNon", []],
|
|
109
|
+
["\\sqsubseteq", 5, 5, "InfixNon", []],
|
|
110
|
+
["\\sqsupset", 5, 5, "InfixNon", []],
|
|
111
|
+
["\\sqsupseteq", 5, 5, "InfixNon", []],
|
|
112
|
+
["\\doteq", 5, 5, "InfixNon", []],
|
|
113
|
+
["\\propto", 5, 5, "InfixNon", []],
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
// OCaml's Hashtbl.find returns the most recent binding, so later aliases win over earlier names.
|
|
117
|
+
const OPERATOR_TABLE = new Map<string, Operator>();
|
|
118
|
+
for (const [name, low, high, fixity, aliases] of OPERATORS) {
|
|
119
|
+
const operator = { name, low, high, fixity };
|
|
120
|
+
for (const spelling of [name, ...aliases]) OPERATOR_TABLE.set(spelling, operator);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const getNonfix = (name: string): Operator => ({ name, low: -1, high: -1, fixity: "Nonfix" });
|
|
124
|
+
|
|
125
|
+
export const lookupOperator = (name: string) => OPERATOR_TABLE.get(name) ?? getNonfix(name);
|
|
126
|
+
|
|
127
|
+
const STANDARD_NAMES: Partial<Record<Builtin, string>> = {
|
|
128
|
+
Implies: "=>",
|
|
129
|
+
Equiv: "<=>",
|
|
130
|
+
Conj: "/\\",
|
|
131
|
+
Disj: "\\/",
|
|
132
|
+
Neg: "~",
|
|
133
|
+
Eq: "=",
|
|
134
|
+
Neq: "#",
|
|
135
|
+
SUBSET: "SUBSET",
|
|
136
|
+
UNION: "UNION",
|
|
137
|
+
DOMAIN: "DOMAIN",
|
|
138
|
+
Subseteq: "\\subseteq",
|
|
139
|
+
Mem: "\\in",
|
|
140
|
+
Notmem: "\\notin",
|
|
141
|
+
Setminus: "\\",
|
|
142
|
+
Cap: "\\cap",
|
|
143
|
+
Cup: "\\cup",
|
|
144
|
+
Prime: "'",
|
|
145
|
+
StrongPrime: "'",
|
|
146
|
+
Leadsto: "~>",
|
|
147
|
+
ENABLED: "ENABLED",
|
|
148
|
+
UNCHANGED: "UNCHANGED",
|
|
149
|
+
Cdot: "\\cdot",
|
|
150
|
+
Actplus: "-+->",
|
|
151
|
+
BoxTrue: "[]",
|
|
152
|
+
BoxFalse: "[]",
|
|
153
|
+
Diamond: "<>",
|
|
154
|
+
Plus: "+",
|
|
155
|
+
Minus: "-",
|
|
156
|
+
Times: "*",
|
|
157
|
+
Ratio: "/",
|
|
158
|
+
Quotient: "\\div",
|
|
159
|
+
Remainder: "%",
|
|
160
|
+
Exp: "^",
|
|
161
|
+
Lteq: "=<",
|
|
162
|
+
Lt: "<",
|
|
163
|
+
Gteq: ">=",
|
|
164
|
+
Gt: ">",
|
|
165
|
+
Range: "..",
|
|
166
|
+
Cat: "\\o",
|
|
167
|
+
OneArg: ":>",
|
|
168
|
+
Extend: "@@",
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export const getStandardForm = (builtin: Builtin): Operator => {
|
|
172
|
+
if (builtin === "Divides") return { name: "?|", low: 10, high: 11, fixity: "InfixNon" };
|
|
173
|
+
if (builtin === "Uminus") return { ...lookupOperator("-."), name: "-" };
|
|
174
|
+
const name = STANDARD_NAMES[builtin];
|
|
175
|
+
return name ? lookupOperator(name) : getNonfix(builtin);
|
|
176
|
+
};
|
|
@@ -27,23 +27,29 @@ const PUNCTUATION = ["(", ")", ";", ":", "@", ","];
|
|
|
27
27
|
const isBlank = (character: string | undefined) =>
|
|
28
28
|
character === " " || character === "\r" || character === "\t";
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const skipBlanks = (lines: string[], position: Position) => {
|
|
31
|
+
while (position.line < lines.length) {
|
|
32
|
+
const line = lines[position.line];
|
|
33
|
+
while (isBlank(line[position.column])) position.column++;
|
|
34
|
+
if (position.column < line.length) return;
|
|
35
|
+
position.line++;
|
|
36
|
+
position.column = 0;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Where the pragma after start opens, past blanks and line ends.
|
|
41
|
+
export const findPragma = (lines: string[], start: Position) => {
|
|
32
42
|
const position = { ...start };
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
skipBlanks();
|
|
43
|
-
if (!(lines[position.line] ?? "").startsWith("(*{", position.column)) return undefined;
|
|
44
|
-
position.column += 3;
|
|
43
|
+
skipBlanks(lines, position);
|
|
44
|
+
return (lines[position.line] ?? "").startsWith("(*{", position.column) ? position : undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const tokenizePragma = (lines: string[], start: Position) => {
|
|
48
|
+
const open = findPragma(lines, start);
|
|
49
|
+
if (!open) return undefined;
|
|
50
|
+
const position = { line: open.line, column: open.column + 3 };
|
|
45
51
|
const tokens: Token[] = [];
|
|
46
|
-
for (skipBlanks(); position.line < lines.length; skipBlanks()) {
|
|
52
|
+
for (skipBlanks(lines, position); position.line < lines.length; skipBlanks(lines, position)) {
|
|
47
53
|
const rest = lines[position.line].slice(position.column);
|
|
48
54
|
if (rest.startsWith("}*)")) return tokens;
|
|
49
55
|
const match =
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
// A port of tlapm's obligation printer: E_fmt with Fmtutil's minimal parenthesization, the
|
|
2
|
-
//
|
|
1
|
+
// A port of tlapm's obligation printer: E_fmt with Fmtutil's minimal parenthesization, and the
|
|
2
|
+
// identifier renaming in Ctx.
|
|
3
3
|
|
|
4
4
|
import { TlaProofError } from "../errors";
|
|
5
5
|
import {
|
|
6
6
|
getHypName,
|
|
7
7
|
type Bound,
|
|
8
|
-
type Builtin,
|
|
9
8
|
type Defn,
|
|
10
9
|
type Expr,
|
|
11
10
|
type Hyp,
|
|
@@ -14,180 +13,71 @@ import {
|
|
|
14
13
|
type Sequent,
|
|
15
14
|
type Shape,
|
|
16
15
|
} from "./expression";
|
|
17
|
-
import { createContext, getBoundName, popNames, pushName, withNames } from "./ctx";
|
|
18
16
|
import { format, formatDelimited, renderDoc, text, type Doc } from "./format";
|
|
17
|
+
import { getStandardForm, lookupOperator } from "./operators";
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
high: number;
|
|
26
|
-
fixity: Fixity;
|
|
19
|
+
// Ctx, which names each binder apart from the names in scope by adding a salt, as a mutable stack:
|
|
20
|
+
// printers bind and unbind names in strict nesting order.
|
|
21
|
+
interface Ident {
|
|
22
|
+
rep: string;
|
|
23
|
+
salt: number;
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
["\\/", 3, 3, "InfixLeft", ["\\lor"]],
|
|
34
|
-
["~", 4, 4, "Prefix", ["\\neg", "\\lnot"]],
|
|
35
|
-
["=", 5, 5, "InfixNon", []],
|
|
36
|
-
["#", 5, 5, "InfixNon", ["/="]],
|
|
37
|
-
["SUBSET", 8, 8, "Prefix", []],
|
|
38
|
-
["UNION", 8, 8, "Prefix", []],
|
|
39
|
-
["DOMAIN", 9, 9, "Prefix", []],
|
|
40
|
-
["\\subseteq", 5, 5, "InfixNon", []],
|
|
41
|
-
["\\in", 5, 5, "InfixNon", []],
|
|
42
|
-
["\\notin", 5, 5, "InfixNon", []],
|
|
43
|
-
["\\", 8, 8, "InfixNon", []],
|
|
44
|
-
["\\cap", 8, 8, "InfixLeft", ["\\intersect"]],
|
|
45
|
-
["\\cup", 8, 8, "InfixLeft", ["\\union"]],
|
|
46
|
-
["\\X", 10, 13, "Prefix", ["\\times"]],
|
|
47
|
-
["'", 15, 15, "Postfix", []],
|
|
48
|
-
["~>", 2, 2, "InfixNon", ["\\leadsto"]],
|
|
49
|
-
["ENABLED", 4, 15, "Prefix", []],
|
|
50
|
-
["UNCHANGED", 4, 15, "Prefix", []],
|
|
51
|
-
["\\cdot", 5, 14, "InfixLeft", []],
|
|
52
|
-
["-+->", 2, 2, "InfixNon", []],
|
|
53
|
-
["[]", 4, 15, "Prefix", []],
|
|
54
|
-
["<>", 4, 15, "Prefix", []],
|
|
55
|
-
["^", 14, 14, "InfixNon", []],
|
|
56
|
-
["/", 13, 13, "InfixNon", []],
|
|
57
|
-
["*", 13, 13, "InfixLeft", []],
|
|
58
|
-
["-.", 12, 12, "Prefix", ["-"]],
|
|
59
|
-
["-", 11, 11, "InfixLeft", []],
|
|
60
|
-
["+", 10, 10, "InfixLeft", []],
|
|
61
|
-
["^+", 15, 15, "Postfix", []],
|
|
62
|
-
["^*", 15, 15, "Postfix", []],
|
|
63
|
-
["^#", 15, 15, "Postfix", []],
|
|
64
|
-
["<", 5, 5, "InfixNon", []],
|
|
65
|
-
["=<", 5, 5, "InfixNon", ["<=", "\\leq"]],
|
|
66
|
-
[">", 5, 5, "InfixNon", []],
|
|
67
|
-
[">=", 5, 5, "InfixNon", ["\\geq"]],
|
|
68
|
-
["...", 9, 9, "InfixNon", []],
|
|
69
|
-
["..", 9, 9, "InfixNon", []],
|
|
70
|
-
["|", 10, 11, "InfixLeft", []],
|
|
71
|
-
["||", 10, 11, "InfixLeft", []],
|
|
72
|
-
["&&", 13, 13, "InfixLeft", []],
|
|
73
|
-
["&", 13, 13, "InfixLeft", []],
|
|
74
|
-
["$$", 9, 13, "InfixLeft", []],
|
|
75
|
-
["$", 9, 13, "InfixLeft", []],
|
|
76
|
-
["??", 9, 13, "InfixLeft", []],
|
|
77
|
-
["%%", 10, 11, "InfixLeft", []],
|
|
78
|
-
["%", 10, 11, "InfixLeft", ["\\mod"]],
|
|
79
|
-
["##", 9, 13, "InfixLeft", []],
|
|
80
|
-
["++", 10, 10, "InfixLeft", []],
|
|
81
|
-
["--", 11, 11, "InfixLeft", []],
|
|
82
|
-
["**", 13, 13, "InfixLeft", []],
|
|
83
|
-
["//", 13, 13, "InfixNon", []],
|
|
84
|
-
["^^", 14, 14, "InfixNon", []],
|
|
85
|
-
["@@", 6, 6, "InfixLeft", []],
|
|
86
|
-
["!!", 9, 13, "InfixNon", []],
|
|
87
|
-
["|-", 5, 5, "InfixNon", []],
|
|
88
|
-
["|=", 5, 5, "InfixNon", []],
|
|
89
|
-
["-|", 5, 5, "InfixNon", []],
|
|
90
|
-
["=|", 5, 5, "InfixNon", []],
|
|
91
|
-
["<:", 7, 7, "InfixNon", []],
|
|
92
|
-
[":>", 7, 7, "InfixNon", []],
|
|
93
|
-
[":=", 5, 5, "InfixNon", []],
|
|
94
|
-
["::=", 5, 5, "InfixNon", []],
|
|
95
|
-
["(+)", 10, 10, "InfixLeft", ["\\oplus"]],
|
|
96
|
-
["(-)", 11, 11, "InfixLeft", ["\\ominus"]],
|
|
97
|
-
["(.)", 13, 13, "InfixLeft", ["\\odot"]],
|
|
98
|
-
["(/)", 13, 13, "InfixNon", ["\\oslash"]],
|
|
99
|
-
["(\\X)", 13, 13, "InfixLeft", ["\\otimes"]],
|
|
100
|
-
["\\uplus", 9, 13, "InfixLeft", []],
|
|
101
|
-
["\\sqcap", 9, 13, "InfixLeft", []],
|
|
102
|
-
["\\sqcup", 9, 13, "InfixLeft", []],
|
|
103
|
-
["\\div", 13, 13, "InfixNon", []],
|
|
104
|
-
["\\wr", 9, 14, "InfixNon", []],
|
|
105
|
-
["\\star", 13, 13, "InfixLeft", []],
|
|
106
|
-
["\\o", 13, 13, "InfixLeft", ["\\circ"]],
|
|
107
|
-
["\\bigcirc", 13, 13, "InfixLeft", []],
|
|
108
|
-
["\\bullet", 13, 13, "InfixLeft", []],
|
|
109
|
-
["\\prec", 5, 5, "InfixNon", []],
|
|
110
|
-
["\\succ", 5, 5, "InfixNon", []],
|
|
111
|
-
["\\preceq", 5, 5, "InfixNon", []],
|
|
112
|
-
["\\succeq", 5, 5, "InfixNon", []],
|
|
113
|
-
["\\sim", 5, 5, "InfixNon", []],
|
|
114
|
-
["\\simeq", 5, 5, "InfixNon", []],
|
|
115
|
-
["\\ll", 5, 5, "InfixNon", []],
|
|
116
|
-
["\\gg", 5, 5, "InfixNon", []],
|
|
117
|
-
["\\asymp", 5, 5, "InfixNon", []],
|
|
118
|
-
["\\subset", 5, 5, "InfixNon", []],
|
|
119
|
-
["\\supset", 5, 5, "InfixNon", []],
|
|
120
|
-
["\\supseteq", 5, 5, "InfixNon", []],
|
|
121
|
-
["\\approx", 5, 5, "InfixNon", []],
|
|
122
|
-
["\\cong", 5, 5, "InfixNon", []],
|
|
123
|
-
["\\sqsubset", 5, 5, "InfixNon", []],
|
|
124
|
-
["\\sqsubseteq", 5, 5, "InfixNon", []],
|
|
125
|
-
["\\sqsupset", 5, 5, "InfixNon", []],
|
|
126
|
-
["\\sqsupseteq", 5, 5, "InfixNon", []],
|
|
127
|
-
["\\doteq", 5, 5, "InfixNon", []],
|
|
128
|
-
["\\propto", 5, 5, "InfixNon", []],
|
|
129
|
-
];
|
|
130
|
-
|
|
131
|
-
// OCaml's Hashtbl.find returns the most recent binding, so later aliases win over earlier names.
|
|
132
|
-
const OPERATOR_TABLE = new Map<string, Operator>();
|
|
133
|
-
for (const [name, low, high, fixity, aliases] of OPERATORS) {
|
|
134
|
-
const operator = { name, low, high, fixity };
|
|
135
|
-
for (const spelling of [name, ...aliases]) OPERATOR_TABLE.set(spelling, operator);
|
|
26
|
+
export interface Context {
|
|
27
|
+
stack: Ident[];
|
|
28
|
+
idents: Map<string, Ident[]>;
|
|
29
|
+
defined: Map<string, number>;
|
|
136
30
|
}
|
|
137
31
|
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
UNION: "UNION",
|
|
152
|
-
DOMAIN: "DOMAIN",
|
|
153
|
-
Subseteq: "\\subseteq",
|
|
154
|
-
Mem: "\\in",
|
|
155
|
-
Notmem: "\\notin",
|
|
156
|
-
Setminus: "\\",
|
|
157
|
-
Cap: "\\cap",
|
|
158
|
-
Cup: "\\cup",
|
|
159
|
-
Prime: "'",
|
|
160
|
-
StrongPrime: "'",
|
|
161
|
-
Leadsto: "~>",
|
|
162
|
-
ENABLED: "ENABLED",
|
|
163
|
-
UNCHANGED: "UNCHANGED",
|
|
164
|
-
Cdot: "\\cdot",
|
|
165
|
-
Actplus: "-+->",
|
|
166
|
-
BoxTrue: "[]",
|
|
167
|
-
BoxFalse: "[]",
|
|
168
|
-
Diamond: "<>",
|
|
169
|
-
Plus: "+",
|
|
170
|
-
Minus: "-",
|
|
171
|
-
Times: "*",
|
|
172
|
-
Ratio: "/",
|
|
173
|
-
Quotient: "\\div",
|
|
174
|
-
Remainder: "%",
|
|
175
|
-
Exp: "^",
|
|
176
|
-
Lteq: "=<",
|
|
177
|
-
Lt: "<",
|
|
178
|
-
Gteq: ">=",
|
|
179
|
-
Gt: ">",
|
|
180
|
-
Range: "..",
|
|
181
|
-
Cat: "\\o",
|
|
182
|
-
OneArg: ":>",
|
|
183
|
-
Extend: "@@",
|
|
32
|
+
const getIdentName = (ident: Ident) =>
|
|
33
|
+
ident.salt === 0 ? ident.rep : `${ident.rep}_${ident.salt}`;
|
|
34
|
+
|
|
35
|
+
export const createContext = (): Context => ({
|
|
36
|
+
stack: [],
|
|
37
|
+
idents: new Map(),
|
|
38
|
+
defined: new Map(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// The name of the binder index places in, counting from 1 for the innermost.
|
|
42
|
+
export const getBoundName = (context: Context, index: number) => {
|
|
43
|
+
const ident = context.stack[context.stack.length - index];
|
|
44
|
+
return ident && getIdentName(ident);
|
|
184
45
|
};
|
|
185
46
|
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
47
|
+
export const pushName = (context: Context, rep: string) => {
|
|
48
|
+
const previous = context.idents.get(rep)?.at(-1);
|
|
49
|
+
let ident: Ident = previous ? { rep, salt: previous.salt + 1 } : { rep, salt: 0 };
|
|
50
|
+
while (context.defined.has(getIdentName(ident))) ident = { rep, salt: ident.salt + 1 };
|
|
51
|
+
context.stack.push(ident);
|
|
52
|
+
const idents = context.idents.get(rep);
|
|
53
|
+
if (idents) idents.push(ident);
|
|
54
|
+
else context.idents.set(rep, [ident]);
|
|
55
|
+
const key = getIdentName(ident);
|
|
56
|
+
context.defined.set(key, (context.defined.get(key) ?? 0) + 1);
|
|
57
|
+
return key;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const popNames = (context: Context, count: number) => {
|
|
61
|
+
for (let index = 0; index < count; index++) {
|
|
62
|
+
const ident = context.stack.pop();
|
|
63
|
+
if (!ident) return;
|
|
64
|
+
context.idents.get(ident.rep)?.pop();
|
|
65
|
+
const key = getIdentName(ident);
|
|
66
|
+
const remaining = (context.defined.get(key) ?? 1) - 1;
|
|
67
|
+
if (remaining === 0) context.defined.delete(key);
|
|
68
|
+
else context.defined.set(key, remaining);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const withNames = <Result>(
|
|
73
|
+
context: Context,
|
|
74
|
+
names: string[],
|
|
75
|
+
body: (idents: string[]) => Result,
|
|
76
|
+
) => {
|
|
77
|
+
const idents = names.map((name) => pushName(context, name));
|
|
78
|
+
const result = body(idents);
|
|
79
|
+
popNames(context, names.length);
|
|
80
|
+
return result;
|
|
191
81
|
};
|
|
192
82
|
|
|
193
83
|
type Assoc = "Left" | "Non" | "Right";
|
|
@@ -404,7 +294,7 @@ const createPrinter = (hyps: Hyp[] = []) => {
|
|
|
404
294
|
return atom(format("@[<h>@<1>%s%a@<1>%s@]", "<[", printExpr(args[0]), "]>"));
|
|
405
295
|
}
|
|
406
296
|
}
|
|
407
|
-
const
|
|
297
|
+
const form = (() => {
|
|
408
298
|
switch (operator.kind) {
|
|
409
299
|
case "Ix":
|
|
410
300
|
return lookupOperator(getIndexName(operator.index));
|
|
@@ -418,38 +308,41 @@ const createPrinter = (hyps: Hyp[] = []) => {
|
|
|
418
308
|
throw new TlaProofError("Cannot print this operator");
|
|
419
309
|
}
|
|
420
310
|
})();
|
|
421
|
-
const
|
|
311
|
+
const getOperation = (doc: Doc, skeleton: Skeleton): Minimal => ({
|
|
422
312
|
kind: "Op",
|
|
423
|
-
name:
|
|
313
|
+
name: form.name,
|
|
424
314
|
doc,
|
|
425
|
-
low:
|
|
426
|
-
high:
|
|
315
|
+
low: form.low,
|
|
316
|
+
high: form.high,
|
|
427
317
|
skeleton,
|
|
428
318
|
});
|
|
429
|
-
if (args.length === 0) return atom(text(
|
|
430
|
-
if (
|
|
431
|
-
return atom(format("@[<b2>%s@[<b1>(%a)@]@]",
|
|
319
|
+
if (args.length === 0) return atom(text(form.name));
|
|
320
|
+
if (form.fixity === "Nonfix") {
|
|
321
|
+
return atom(format("@[<b2>%s@[<b1>(%a)@]@]", form.name, formatDelimited(args, printExpr)));
|
|
432
322
|
}
|
|
433
|
-
if (
|
|
434
|
-
return
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
323
|
+
if (form.fixity === "Prefix" && args.length === 1) {
|
|
324
|
+
return getOperation(
|
|
325
|
+
/[A-Za-z]$/.test(form.name) ? format("%s@ ", form.name) : text(form.name),
|
|
326
|
+
{
|
|
327
|
+
kind: "Prefix",
|
|
328
|
+
operand: formatExpr(args[0]),
|
|
329
|
+
},
|
|
330
|
+
);
|
|
438
331
|
}
|
|
439
|
-
if (
|
|
440
|
-
return
|
|
332
|
+
if (form.fixity === "Postfix" && args.length === 1) {
|
|
333
|
+
return getOperation(text(form.name), { kind: "Postfix", operand: formatExpr(args[0]) });
|
|
441
334
|
}
|
|
442
|
-
if (
|
|
335
|
+
if (form.fixity.startsWith("Infix") && args.length === 2) {
|
|
443
336
|
const assoc: Assoc =
|
|
444
|
-
|
|
445
|
-
return
|
|
337
|
+
form.fixity === "InfixLeft" ? "Left" : form.fixity === "InfixRight" ? "Right" : "Non";
|
|
338
|
+
return getOperation(form.name === ".." ? text(form.name) : format("@ %s ", form.name), {
|
|
446
339
|
kind: "Infix",
|
|
447
340
|
assoc,
|
|
448
341
|
left: formatExpr(args[0]),
|
|
449
342
|
right: formatExpr(args[1]),
|
|
450
343
|
});
|
|
451
344
|
}
|
|
452
|
-
throw new TlaProofError(`Arity mismatch for operator ${
|
|
345
|
+
throw new TlaProofError(`Arity mismatch for operator ${form.name}`);
|
|
453
346
|
};
|
|
454
347
|
|
|
455
348
|
const formatExpr = (expression: Expr): Minimal => {
|
package/src/obligations/proof.ts
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Pco.resolve: tlapm's parser lets an operator take in every later infix operator that is not
|
|
2
|
+
// entirely below it, so SUBSET S \ T is SUBSET (S \ T) to tlapm though SANY reads (SUBSET S) \ T.
|
|
3
|
+
// This regroups an infix application the way tlapm does.
|
|
4
|
+
|
|
5
|
+
import type { Expression, SourceLocation } from "../ast";
|
|
6
|
+
import { lookupOperator, type Operator } from "./operators";
|
|
7
|
+
|
|
8
|
+
type Grouping =
|
|
9
|
+
| { kind: "Attached"; expression: Expression }
|
|
10
|
+
| { kind: "Reduced"; isFinal: boolean };
|
|
11
|
+
|
|
12
|
+
const PREFIX_KEYWORDS: Partial<Record<Expression["kind"], string>> = {
|
|
13
|
+
Always: "[]",
|
|
14
|
+
Eventually: "<>",
|
|
15
|
+
Enabled: "ENABLED",
|
|
16
|
+
Unchanged: "UNCHANGED",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const isSamePosition = (left: SourceLocation | undefined, right: SourceLocation | undefined) =>
|
|
20
|
+
left !== undefined &&
|
|
21
|
+
right !== undefined &&
|
|
22
|
+
left.line === right.line &&
|
|
23
|
+
left.column === right.column;
|
|
24
|
+
|
|
25
|
+
const getStart = (expression: Expression) => expression.location.range?.start;
|
|
26
|
+
|
|
27
|
+
const getEnd = (expression: Expression) => expression.location.range?.end;
|
|
28
|
+
|
|
29
|
+
const withEnd = (expression: Expression, end: SourceLocation | undefined): Expression => {
|
|
30
|
+
const range = expression.location.range;
|
|
31
|
+
return range && end
|
|
32
|
+
? { ...expression, location: { ...expression.location, range: { ...range, end } } }
|
|
33
|
+
: expression;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// The operator an expression leaves open on tlapm's parse stack, with the operand after it.
|
|
37
|
+
const getOpenOperator = (expression: Expression) => {
|
|
38
|
+
if (expression.kind === "Apply") {
|
|
39
|
+
const operator = lookupOperator(expression.operator);
|
|
40
|
+
const isInfix = expression.args.length === 2 && operator.fixity.startsWith("Infix");
|
|
41
|
+
const isPrefix = expression.args.length === 1 && operator.fixity === "Prefix";
|
|
42
|
+
if (!isInfix && !isPrefix) return undefined;
|
|
43
|
+
const last = expression.args.length - 1;
|
|
44
|
+
return {
|
|
45
|
+
operator,
|
|
46
|
+
isInfix,
|
|
47
|
+
operand: expression.args[last],
|
|
48
|
+
replace: (operand: Expression): Expression => ({
|
|
49
|
+
...expression,
|
|
50
|
+
args: [...expression.args.slice(0, last), operand],
|
|
51
|
+
}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const keyword = PREFIX_KEYWORDS[expression.kind];
|
|
55
|
+
if (keyword === undefined || !("expression" in expression)) return undefined;
|
|
56
|
+
return {
|
|
57
|
+
operator: lookupOperator(keyword),
|
|
58
|
+
isInfix: false,
|
|
59
|
+
operand: expression.expression,
|
|
60
|
+
replace: (operand: Expression): Expression => ({ ...expression, expression: operand }),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const isBelow = (left: Operator, right: Operator) => left.high < right.low;
|
|
65
|
+
|
|
66
|
+
export const regroup = (expression: Extract<Expression, { kind: "Apply" }>): Expression => {
|
|
67
|
+
const operator = lookupOperator(expression.operator);
|
|
68
|
+
const [left, right] = expression.args;
|
|
69
|
+
const end = getEnd(expression);
|
|
70
|
+
const getInfix = (operand: Expression): Expression => {
|
|
71
|
+
const range = expression.location.range;
|
|
72
|
+
const start = getStart(operand);
|
|
73
|
+
return {
|
|
74
|
+
...expression,
|
|
75
|
+
args: [operand, right],
|
|
76
|
+
location:
|
|
77
|
+
range && start
|
|
78
|
+
? { ...expression.location, range: { ...range, start } }
|
|
79
|
+
: expression.location,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const attach = (node: Expression, isOpen: boolean): Grouping => {
|
|
83
|
+
const open = isOpen ? getOpenOperator(node) : undefined;
|
|
84
|
+
if (!open) return { kind: "Reduced", isFinal: false };
|
|
85
|
+
const inner = attach(open.operand, isSamePosition(getEnd(open.operand), getEnd(node)));
|
|
86
|
+
if (inner.kind === "Attached")
|
|
87
|
+
return { kind: "Attached", expression: withEnd(open.replace(inner.expression), end) };
|
|
88
|
+
const isReduced =
|
|
89
|
+
!inner.isFinal &&
|
|
90
|
+
(isBelow(operator, open.operator) ||
|
|
91
|
+
(open.isInfix &&
|
|
92
|
+
operator.fixity === "InfixLeft" &&
|
|
93
|
+
open.operator.fixity === "InfixLeft" &&
|
|
94
|
+
!isBelow(open.operator, operator)));
|
|
95
|
+
if (isReduced) return { kind: "Reduced", isFinal: !isBelow(operator, open.operator) };
|
|
96
|
+
return { kind: "Attached", expression: withEnd(open.replace(getInfix(open.operand)), end) };
|
|
97
|
+
};
|
|
98
|
+
const grouping = attach(left, isSamePosition(getStart(left), getStart(expression)));
|
|
99
|
+
return grouping.kind === "Attached" ? grouping.expression : expression;
|
|
100
|
+
};
|