tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.d618a2d79
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 +2 -1
- package/dist/compile.cjs +6 -6
- package/dist/compile.js +10 -10
- package/dist/deciders.cjs +41 -37
- package/dist/deciders.js +57 -53
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +37 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +2683 -427
- package/dist/prove.js +2681 -425
- package/dist/run.cjs +47 -44
- package/dist/run.js +47 -44
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +44 -41
- package/dist/transfer.js +44 -41
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +1 -1
- package/src/ast.ts +41 -41
- package/src/bags.ts +2 -5
- package/src/builtins.ts +5 -21
- package/src/check-failure.ts +116 -0
- package/src/check-trace.ts +268 -0
- package/src/check.ts +474 -718
- package/src/cli/options.ts +6 -31
- package/src/cli/run.ts +0 -2
- package/src/cli/runtime-invariants.ts +8 -7
- package/src/cli/shared.ts +1 -5
- package/src/cli/tlapm.ts +433 -70
- package/src/cli/tlc-arguments.ts +244 -238
- package/src/cli/tlc-output.ts +687 -0
- package/src/cli/tlc-usage.ts +3 -5
- package/src/cli/tlc.ts +49 -702
- package/src/cli/xml-exporter.ts +23 -33
- package/src/codegen.ts +5 -10
- package/src/coverage.ts +3 -10
- package/src/dependencies.ts +30 -14
- package/src/dfid.ts +16 -16
- package/src/environment-config.ts +315 -0
- package/src/environment.ts +55 -338
- package/src/errors.ts +33 -5
- package/src/evaluate.ts +31 -106
- package/src/exit-codes.ts +2 -0
- package/src/fingerprints.ts +65 -1
- package/src/instances.ts +47 -10
- package/src/java-hash.ts +7 -0
- package/src/java-overrides.ts +4 -9
- package/src/json-reader.ts +1 -2
- package/src/json.ts +8 -18
- package/src/levels.ts +46 -57
- package/src/lexer.ts +76 -85
- package/src/liveness-checker.ts +616 -306
- package/src/liveness.ts +104 -120
- package/src/obligations/action.ts +14 -17
- package/src/obligations/anonymize.ts +37 -16
- package/src/obligations/enabled-axioms.ts +218 -0
- package/src/obligations/equality.ts +1 -3
- package/src/obligations/expression.ts +36 -8
- package/src/obligations/format.ts +0 -2
- package/src/obligations/generate.ts +66 -54
- package/src/obligations/index.ts +38 -8
- package/src/obligations/levels.ts +24 -11
- package/src/obligations/module.ts +28 -41
- package/src/obligations/normalize.ts +26 -14
- package/src/obligations/operators.ts +0 -2
- package/src/obligations/pragma.ts +53 -16
- package/src/obligations/printer.ts +43 -13
- package/src/obligations/proof.ts +3 -2
- package/src/obligations/regroup.ts +0 -2
- package/src/obligations/simplify.ts +55 -50
- package/src/obligations/source.ts +118 -74
- package/src/obligations/subexpression.ts +45 -18
- package/src/obligations/substitution.ts +1 -3
- package/src/obligations/temporal.ts +5 -7
- package/src/obligations/tuples.ts +0 -2
- package/src/parallel-worker.ts +10 -30
- package/src/parallel.ts +50 -80
- package/src/parser.ts +722 -385
- package/src/pcal/ast.ts +53 -65
- package/src/pcal/parse.ts +144 -191
- package/src/pcal/tokenize.ts +91 -115
- package/src/pcal/translate.ts +83 -98
- package/src/pcal/validate.ts +48 -50
- package/src/pretty-print.ts +7 -7
- package/src/prover/decide-pool.ts +3 -3
- package/src/prover/deciders.ts +12 -9
- package/src/prover/isabelle/decide.ts +3 -25
- package/src/prover/isabelle/encode.ts +72 -42
- package/src/prover/isabelle/isar/attrib.ts +250 -0
- package/src/prover/isabelle/isar/context.ts +335 -0
- package/src/prover/isabelle/isar/induct.ts +382 -0
- package/src/prover/isabelle/isar/inner-syntax.ts +158 -7
- package/src/prover/isabelle/isar/method.ts +509 -212
- package/src/prover/isabelle/isar/proof.ts +570 -14
- package/src/prover/isabelle/isar/rule-cases.ts +224 -0
- package/src/prover/isabelle/isar/theory.ts +487 -76
- package/src/prover/isabelle/isar/toplevel.ts +331 -0
- package/src/prover/isabelle/isar/type-check.ts +89 -14
- package/src/prover/isabelle/provers/blast/blast.ts +7 -65
- package/src/prover/isabelle/provers/blast/branch.ts +0 -2
- package/src/prover/isabelle/provers/blast/prove.ts +0 -2
- package/src/prover/isabelle/provers/blast/rules.ts +0 -2
- package/src/prover/isabelle/provers/blast/state.ts +0 -2
- package/src/prover/isabelle/provers/blast/term.ts +0 -2
- package/src/prover/isabelle/provers/clasimp.ts +0 -2
- package/src/prover/isabelle/provers/classical.ts +45 -20
- package/src/prover/isabelle/provers/hypsubst.ts +17 -28
- package/src/prover/isabelle/pure/bicompose.ts +9 -27
- package/src/prover/isabelle/pure/consts.ts +0 -2
- package/src/prover/isabelle/pure/conv.ts +2 -10
- package/src/prover/isabelle/pure/drule.ts +72 -6
- package/src/prover/isabelle/pure/envir.ts +0 -2
- package/src/prover/isabelle/pure/goal.ts +1 -3
- package/src/prover/isabelle/pure/logic.ts +1 -3
- package/src/prover/isabelle/pure/name.ts +0 -2
- package/src/prover/isabelle/pure/net.ts +0 -2
- package/src/prover/isabelle/pure/object-logic.ts +13 -7
- package/src/prover/isabelle/pure/pattern.ts +0 -2
- package/src/prover/isabelle/pure/raw-simplifier.ts +4 -15
- package/src/prover/isabelle/pure/rewriter.ts +6 -17
- package/src/prover/isabelle/pure/search.ts +0 -2
- package/src/prover/isabelle/pure/seq.ts +0 -2
- package/src/prover/isabelle/pure/simplifier.ts +26 -7
- package/src/prover/isabelle/pure/table.ts +2 -3
- package/src/prover/isabelle/pure/tactic.ts +0 -2
- package/src/prover/isabelle/pure/tactical.ts +42 -37
- package/src/prover/isabelle/pure/term-ord.ts +0 -2
- package/src/prover/isabelle/pure/term.ts +0 -2
- package/src/prover/isabelle/pure/thm.ts +14 -13
- package/src/prover/isabelle/pure/type.ts +0 -2
- package/src/prover/isabelle/pure/unify.ts +0 -2
- package/src/prover/isabelle/pure/variable.ts +0 -2
- package/src/prover/isabelle/tla/classical-setup.ts +18 -16
- package/src/prover/isabelle/tla/context.ts +58 -11
- package/src/prover/isabelle/tla/logic-data.ts +1 -1
- package/src/prover/isabelle/tla/logic.ts +49 -21
- package/src/prover/isabelle/tla/simpdata.ts +1 -12
- package/src/prover/isabelle/tools/intuitionistic.ts +28 -12
- package/src/prover/ocaml.ts +4 -1
- package/src/prover/prep/action.ts +30 -31
- package/src/prover/prep/enabled-axioms.ts +11 -2
- package/src/prover/prep/fingerprint.ts +2 -5
- package/src/prover/prep/methods.ts +181 -51
- package/src/prover/prep/normalize.ts +38 -9
- package/src/prover/prove.ts +395 -94
- package/src/prover/ptl/decide.ts +95 -50
- package/src/prover/ptl/encode.ts +2 -3
- package/src/prover/ptl/tableau.ts +28 -2
- package/src/prover/smt/arithmetic.ts +310 -0
- package/src/prover/smt/axioms.ts +12 -8
- package/src/prover/smt/context.ts +43 -8
- package/src/prover/smt/data.ts +498 -379
- package/src/prover/smt/encode.ts +148 -57
- package/src/prover/smt/extensionality.ts +5 -3
- package/src/prover/smt/flatten.ts +4 -6
- package/src/prover/smt/format.ts +28 -4
- package/src/prover/smt/instances.ts +7 -1
- package/src/prover/smt/layout.ts +91 -0
- package/src/prover/smt/rewrite.ts +1021 -618
- package/src/prover/smt/simplify.ts +441 -0
- package/src/prover/smt/solver/ackermannize-bv-tactic.ts +61 -0
- package/src/prover/smt/solver/ackr-helper.ts +179 -0
- package/src/prover/smt/solver/ackr-model-converter.ts +105 -0
- package/src/prover/smt/solver/act-cache.ts +0 -1
- package/src/prover/smt/solver/add-bounds-tactic.ts +59 -0
- package/src/prover/smt/solver/aig-tactic.ts +52 -0
- package/src/prover/smt/solver/aig.ts +889 -0
- package/src/prover/smt/solver/arith-rewriter.ts +613 -6
- package/src/prover/smt/solver/arith-terms.ts +121 -0
- package/src/prover/smt/solver/array-decl-plugin.ts +175 -0
- package/src/prover/smt/solver/array-rewriter.ts +309 -0
- package/src/prover/smt/solver/asserted-formulas.ts +87 -16
- package/src/prover/smt/solver/ast-lt.ts +166 -12
- package/src/prover/smt/solver/ast-util.ts +160 -2
- package/src/prover/smt/solver/ast.ts +1427 -41
- package/src/prover/smt/solver/auf-solver.ts +2 -6
- package/src/prover/smt/solver/bit-blast-tactic.ts +582 -0
- package/src/prover/smt/solver/bit-blaster.ts +871 -0
- package/src/prover/smt/solver/bound-manager.ts +196 -0
- package/src/prover/smt/solver/bound-simplifier.ts +600 -0
- package/src/prover/smt/solver/bv-bound-chk.ts +492 -0
- package/src/prover/smt/solver/bv-decl-plugin.ts +338 -0
- package/src/prover/smt/solver/bv-divrem-bounds.ts +95 -0
- package/src/prover/smt/solver/bv-rewriter.ts +1901 -0
- package/src/prover/smt/solver/bv1-blast.ts +214 -0
- package/src/prover/smt/solver/bv2fpa-converter.ts +174 -0
- package/src/prover/smt/solver/bv2int-rewriter.ts +544 -0
- package/src/prover/smt/solver/bv2real-rewriter.ts +533 -0
- package/src/prover/smt/solver/card2bv.ts +110 -0
- package/src/prover/smt/solver/case-split-queue.ts +0 -2
- package/src/prover/smt/solver/cg-table.ts +39 -25
- package/src/prover/smt/solver/check-logic.ts +330 -0
- package/src/prover/smt/solver/clause.ts +0 -1
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +278 -0
- package/src/prover/smt/solver/conflict-resolution.ts +78 -2
- package/src/prover/smt/solver/context.ts +516 -115
- package/src/prover/smt/solver/ctx-simplify.ts +20 -10
- package/src/prover/smt/solver/ctx-solver-simplify.ts +122 -0
- package/src/prover/smt/solver/datatype-decl-plugin.ts +572 -0
- package/src/prover/smt/solver/datatype-rewriter.ts +82 -0
- package/src/prover/smt/solver/demodulator.ts +786 -0
- package/src/prover/smt/solver/dependent-expr-state.ts +81 -0
- package/src/prover/smt/solver/der.ts +38 -10
- package/src/prover/smt/solver/diff-neq.ts +206 -0
- package/src/prover/smt/solver/distribute-forall.ts +57 -0
- package/src/prover/smt/solver/dom-simplify.ts +511 -0
- package/src/prover/smt/solver/dt2bv-tactic.ts +260 -0
- package/src/prover/smt/solver/dyn-ack.ts +0 -1
- package/src/prover/smt/solver/e-matching.ts +7 -8
- package/src/prover/smt/solver/elim-small-bv-tactic.ts +129 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +545 -0
- package/src/prover/smt/solver/enode.ts +100 -3
- package/src/prover/smt/solver/eq2bv-tactic.ts +218 -0
- package/src/prover/smt/solver/euf/arith-axioms.ts +403 -0
- package/src/prover/smt/solver/euf/arith-internalize.ts +653 -0
- package/src/prover/smt/solver/euf/arith-solver.ts +889 -0
- package/src/prover/smt/solver/euf/array-axioms.ts +527 -0
- package/src/prover/smt/solver/euf/array-internalize.ts +428 -0
- package/src/prover/smt/solver/euf/array-solver.ts +306 -0
- package/src/prover/smt/solver/euf/bv-ackerman.ts +162 -0
- package/src/prover/smt/solver/euf/bv-internalize.ts +748 -0
- package/src/prover/smt/solver/euf/bv-solver.ts +449 -0
- package/src/prover/smt/solver/euf/dt-solver.ts +618 -0
- package/src/prover/smt/solver/euf/egraph.ts +908 -0
- package/src/prover/smt/solver/euf/euf-solver.ts +1205 -0
- package/src/prover/smt/solver/euf/fpa-solver.ts +317 -0
- package/src/prover/smt/solver/euf/mam.ts +1050 -0
- package/src/prover/smt/solver/euf/q-ematch.ts +830 -0
- package/src/prover/smt/solver/euf/q-mbi.ts +587 -0
- package/src/prover/smt/solver/euf/q-model-fixer.ts +296 -0
- package/src/prover/smt/solver/euf/q-solver.ts +361 -0
- package/src/prover/smt/solver/euf/th-solver.ts +335 -0
- package/src/prover/smt/solver/euf/th-solvers.ts +30 -0
- package/src/prover/smt/solver/expr-ref.ts +0 -1
- package/src/prover/smt/solver/expr-safe-replace.ts +26 -2
- package/src/prover/smt/solver/expr2polynomial.ts +146 -0
- package/src/prover/smt/solver/factor.ts +141 -0
- package/src/prover/smt/solver/fd-solver.ts +265 -0
- package/src/prover/smt/solver/fingerprints.ts +15 -15
- package/src/prover/smt/solver/fix-dl-var.ts +148 -0
- package/src/prover/smt/solver/flat-literals.ts +0 -1
- package/src/prover/smt/solver/fm-tactic.ts +884 -0
- package/src/prover/smt/solver/fpa-decl-plugin.ts +618 -0
- package/src/prover/smt/solver/fpa-rewriter.ts +640 -0
- package/src/prover/smt/solver/fpa2bv-converter.ts +2249 -0
- package/src/prover/smt/solver/fpa2bv-rewriter.ts +348 -0
- package/src/prover/smt/solver/fpa2bv-tactic.ts +73 -0
- package/src/prover/smt/solver/goal.ts +175 -15
- package/src/prover/smt/solver/goal2nlsat.ts +189 -0
- package/src/prover/smt/solver/guard-set.ts +219 -0
- package/src/prover/smt/solver/hash.ts +0 -1
- package/src/prover/smt/solver/hashtable.ts +30 -16
- package/src/prover/smt/solver/inf-eps.ts +63 -0
- package/src/prover/smt/solver/inj-axiom.ts +101 -0
- package/src/prover/smt/solver/internalizer.ts +110 -38
- package/src/prover/smt/solver/lackr.ts +381 -0
- package/src/prover/smt/solver/lbool.ts +0 -1
- package/src/prover/smt/solver/lia2card.ts +268 -0
- package/src/prover/smt/solver/lia2pb.ts +154 -0
- package/src/prover/smt/solver/literal.ts +0 -1
- package/src/prover/smt/solver/logic-tactics.ts +345 -0
- package/src/prover/smt/solver/lp/bound-propagator.ts +0 -1
- package/src/prover/smt/solver/lp/constraints.ts +0 -1
- package/src/prover/smt/solver/lp/containers.ts +7 -2
- package/src/prover/smt/solver/lp/core-solver.ts +18 -3
- package/src/prover/smt/solver/lp/cross-nested.ts +1 -4
- package/src/prover/smt/solver/lp/dep-intervals.ts +0 -3
- package/src/prover/smt/solver/lp/dependency.ts +0 -2
- package/src/prover/smt/solver/lp/dioph-eq.ts +1 -10
- package/src/prover/smt/solver/lp/explanation.ts +0 -1
- package/src/prover/smt/solver/lp/gomory.ts +0 -1
- package/src/prover/smt/solver/lp/hnf-cutter.ts +1 -2
- package/src/prover/smt/solver/lp/hnf.ts +0 -3
- package/src/prover/smt/solver/lp/int-cube.ts +3 -4
- package/src/prover/smt/solver/lp/int-solver.ts +0 -2
- package/src/prover/smt/solver/lp/lar-solver.ts +40 -4
- package/src/prover/smt/solver/lp/lar-term.ts +0 -1
- package/src/prover/smt/solver/lp/lp-settings.ts +0 -1
- package/src/prover/smt/solver/lp/maximize-term.ts +59 -10
- package/src/prover/smt/solver/lp/nex-creator.ts +3 -3
- package/src/prover/smt/solver/lp/nex.ts +2 -2
- package/src/prover/smt/solver/lp/nla-backoff.ts +0 -2
- package/src/prover/smt/solver/lp/nla-basics.ts +46 -3
- package/src/prover/smt/solver/lp/nla-core.ts +67 -16
- package/src/prover/smt/solver/lp/nla-divisions.ts +0 -2
- package/src/prover/smt/solver/lp/nla-emonics.ts +0 -1
- package/src/prover/smt/solver/lp/nla-factorization.ts +0 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +9 -5
- package/src/prover/smt/solver/lp/nla-horner.ts +0 -2
- package/src/prover/smt/solver/lp/nla-intervals.ts +0 -1
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +0 -2
- package/src/prover/smt/solver/lp/nla-monotone.ts +0 -1
- package/src/prover/smt/solver/lp/nla-order.ts +0 -2
- package/src/prover/smt/solver/lp/nla-patcher.ts +0 -1
- package/src/prover/smt/solver/lp/nla-powers.ts +220 -0
- package/src/prover/smt/solver/lp/nla-tangents.ts +0 -2
- package/src/prover/smt/solver/lp/nla-throttle.ts +0 -1
- package/src/prover/smt/solver/lp/nla-transcendentals.ts +324 -0
- package/src/prover/smt/solver/lp/nla-types.ts +0 -2
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +0 -1
- package/src/prover/smt/solver/lp/nra-solver.ts +122 -64
- package/src/prover/smt/solver/lp/numeric-pair.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-interval.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-solver.ts +0 -2
- package/src/prover/smt/solver/lp/pdd.ts +2 -2
- package/src/prover/smt/solver/lp/random-updater.ts +0 -2
- package/src/prover/smt/solver/lp/static-matrix.ts +0 -1
- package/src/prover/smt/solver/lp/union-find.ts +7 -2
- package/src/prover/smt/solver/macro-finder.ts +530 -0
- package/src/prover/smt/solver/mam-compiler.ts +122 -90
- package/src/prover/smt/solver/mam-interpreter.ts +0 -1
- package/src/prover/smt/solver/max-bv-sharing-tactic.ts +17 -0
- package/src/prover/smt/solver/max-cliques.ts +105 -0
- package/src/prover/smt/solver/maxcore.ts +309 -0
- package/src/prover/smt/solver/maximize-ac-sharing.ts +8 -6
- package/src/prover/smt/solver/maxlex.ts +115 -0
- package/src/prover/smt/solver/maxsmt-base.ts +105 -0
- package/src/prover/smt/solver/maxsmt.ts +92 -0
- package/src/prover/smt/solver/mbp/mbp-arith.ts +467 -0
- package/src/prover/smt/solver/mbp/mbp-basic-tg.ts +101 -0
- package/src/prover/smt/solver/mbp/mbp-plugin.ts +287 -0
- package/src/prover/smt/solver/mbp/mbp-qel.ts +48 -0
- package/src/prover/smt/solver/mbp/mbp-solve-plugin.ts +271 -0
- package/src/prover/smt/solver/mbp/mbp-term-graph.ts +606 -0
- package/src/prover/smt/solver/model-based-opt.ts +844 -0
- package/src/prover/smt/solver/model-checker.ts +18 -27
- package/src/prover/smt/solver/model-evaluator.ts +188 -12
- package/src/prover/smt/solver/model-finder.ts +12 -18
- package/src/prover/smt/solver/model-generator.ts +56 -29
- package/src/prover/smt/solver/model-macro-solver.ts +49 -46
- package/src/prover/smt/solver/model-printer.ts +214 -0
- package/src/prover/smt/solver/mpf.ts +711 -0
- package/src/prover/smt/solver/mus.ts +51 -0
- package/src/prover/smt/solver/muz/dl-context.ts +404 -0
- package/src/prover/smt/solver/muz/dl-mk-bit-blast.ts +97 -0
- package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +167 -0
- package/src/prover/smt/solver/muz/dl-mk-elim-term-ite.ts +125 -0
- package/src/prover/smt/solver/muz/dl-mk-interp-tail-simplifier.ts +299 -0
- package/src/prover/smt/solver/muz/dl-mk-rule-inliner.ts +455 -0
- package/src/prover/smt/solver/muz/dl-mk-slice.ts +340 -0
- package/src/prover/smt/solver/muz/dl-mk-subsumption-checker.ts +163 -0
- package/src/prover/smt/solver/muz/dl-rule-set.ts +273 -0
- package/src/prover/smt/solver/muz/dl-rule-transformer.ts +123 -0
- package/src/prover/smt/solver/muz/dl-rule.ts +496 -0
- package/src/prover/smt/solver/muz/hnf.ts +201 -0
- package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +176 -0
- package/src/prover/smt/solver/muz/horn-tactic.ts +201 -0
- package/src/prover/smt/solver/muz/rule-properties.ts +337 -0
- package/src/prover/smt/solver/muz/spacer-context.ts +1406 -0
- package/src/prover/smt/solver/muz/spacer-dl-interface.ts +52 -0
- package/src/prover/smt/solver/muz/spacer-generalizers.ts +241 -0
- package/src/prover/smt/solver/muz/spacer-manager.ts +127 -0
- package/src/prover/smt/solver/muz/spacer-pdr.ts +218 -0
- package/src/prover/smt/solver/muz/spacer-prop-solver.ts +333 -0
- package/src/prover/smt/solver/muz/spacer-util.ts +282 -0
- package/src/prover/smt/solver/muz/substitution.ts +203 -0
- package/src/prover/smt/solver/nla2bv-tactic.ts +285 -0
- package/src/prover/smt/solver/nlqsat.ts +484 -0
- package/src/prover/smt/solver/nlsat/algebraic.ts +125 -12
- package/src/prover/smt/solver/nlsat/atoms.ts +20 -6
- package/src/prover/smt/solver/nlsat/evaluator.ts +0 -1
- package/src/prover/smt/solver/nlsat/explain.ts +63 -23
- package/src/prover/smt/solver/nlsat/interval-set.ts +1 -2
- package/src/prover/smt/solver/nlsat/levelwise.ts +3 -9
- package/src/prover/smt/solver/nlsat/mpbq.ts +45 -1
- package/src/prover/smt/solver/nlsat/polynomial.ts +13 -6
- package/src/prover/smt/solver/nlsat/solver.ts +236 -45
- package/src/prover/smt/solver/nlsat/transcendental-eval.ts +310 -0
- package/src/prover/smt/solver/nlsat/transcendentals.ts +367 -0
- package/src/prover/smt/solver/nlsat/types.ts +0 -2
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -4
- package/src/prover/smt/solver/nlsat/upolynomial.ts +0 -2
- package/src/prover/smt/solver/nlsat-tactic.ts +82 -0
- package/src/prover/smt/solver/nnf.ts +105 -23
- package/src/prover/smt/solver/normalize-bounds.ts +53 -0
- package/src/prover/smt/solver/occf.ts +97 -0
- package/src/prover/smt/solver/opt-context.ts +787 -0
- package/src/prover/smt/solver/opt-nlsat.ts +375 -0
- package/src/prover/smt/solver/opt-pareto.ts +282 -0
- package/src/prover/smt/solver/opt-preprocess.ts +101 -0
- package/src/prover/smt/solver/opt-solver.ts +308 -0
- package/src/prover/smt/solver/opt-value.ts +55 -0
- package/src/prover/smt/solver/optsmt.ts +692 -0
- package/src/prover/smt/solver/parallel-tactic.ts +299 -0
- package/src/prover/smt/solver/pattern-inference.ts +19 -11
- package/src/prover/smt/solver/pb-rewriter.ts +284 -0
- package/src/prover/smt/solver/pb2bv-tactic.ts +540 -0
- package/src/prover/smt/solver/poly-rewriter.ts +202 -57
- package/src/prover/smt/solver/probes.ts +512 -0
- package/src/prover/smt/solver/propagate-values.ts +37 -21
- package/src/prover/smt/solver/proto-model.ts +648 -19
- package/src/prover/smt/solver/pull-quant.ts +28 -12
- package/src/prover/smt/solver/purify-arith.ts +632 -0
- package/src/prover/smt/solver/qe-arith-plugin.ts +1098 -0
- package/src/prover/smt/solver/qe-lite-der.ts +2 -3
- package/src/prover/smt/solver/qe-lite-fm.ts +3 -4
- package/src/prover/smt/solver/qe-lite.ts +37 -17
- package/src/prover/smt/solver/qe-mbp.ts +270 -0
- package/src/prover/smt/solver/qe.ts +1498 -0
- package/src/prover/smt/solver/qel.ts +25 -0
- package/src/prover/smt/solver/qi-queue.ts +0 -1
- package/src/prover/smt/solver/qsat.ts +814 -0
- package/src/prover/smt/solver/quant-hoist.ts +196 -0
- package/src/prover/smt/solver/quant-tactics.ts +16 -56
- package/src/prover/smt/solver/quantifier-analyzer.ts +19 -20
- package/src/prover/smt/solver/quantifier-manager.ts +8 -16
- package/src/prover/smt/solver/quasi-macros.ts +243 -0
- package/src/prover/smt/solver/random-gen.ts +0 -1
- package/src/prover/smt/solver/rational.ts +45 -1
- package/src/prover/smt/solver/recfun-decl-plugin.ts +261 -0
- package/src/prover/smt/solver/recfun-rewriter.ts +107 -0
- package/src/prover/smt/solver/reduce-args.ts +225 -0
- package/src/prover/smt/solver/reduce-bv-size.ts +119 -0
- package/src/prover/smt/solver/ref-vector.ts +0 -1
- package/src/prover/smt/solver/relevancy.ts +1 -2
- package/src/prover/smt/solver/rewriter-tpl.ts +77 -10
- package/src/prover/smt/solver/rewriter.ts +475 -36
- package/src/prover/smt/solver/rlimit.ts +38 -2
- package/src/prover/smt/solver/sat/asymm-branch.ts +325 -0
- package/src/prover/smt/solver/sat/big.ts +177 -0
- package/src/prover/smt/solver/sat/clause-use-list.ts +71 -0
- package/src/prover/smt/solver/sat/clause.ts +203 -0
- package/src/prover/smt/solver/sat/cleaner.ts +94 -0
- package/src/prover/smt/solver/sat/config.ts +122 -0
- package/src/prover/smt/solver/sat/consequences.ts +203 -0
- package/src/prover/smt/solver/sat/elim-eqs.ts +142 -0
- package/src/prover/smt/solver/sat/goal2sat.ts +526 -0
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +378 -0
- package/src/prover/smt/solver/sat/model-converter.ts +136 -0
- package/src/prover/smt/solver/sat/pb-constraint.ts +261 -0
- package/src/prover/smt/solver/sat/pb-solver.ts +1577 -0
- package/src/prover/smt/solver/sat/probing.ts +170 -0
- package/src/prover/smt/solver/sat/sat-tactic.ts +43 -0
- package/src/prover/smt/solver/sat/sat2goal.ts +165 -0
- package/src/prover/smt/solver/sat/scc.ts +142 -0
- package/src/prover/smt/solver/sat/simplifier.ts +1618 -0
- package/src/prover/smt/solver/sat/solver.ts +2127 -0
- package/src/prover/smt/solver/sat/var-queue.ts +117 -0
- package/src/prover/smt/solver/sat/watch-list.ts +140 -0
- package/src/prover/smt/solver/search-tree.ts +284 -0
- package/src/prover/smt/solver/seq-axioms.ts +1010 -0
- package/src/prover/smt/solver/seq-decl-plugin.ts +819 -0
- package/src/prover/smt/solver/seq-derive.ts +1298 -0
- package/src/prover/smt/solver/seq-eq-approx.ts +236 -0
- package/src/prover/smt/solver/seq-eq-solver.ts +439 -0
- package/src/prover/smt/solver/seq-factory.ts +89 -0
- package/src/prover/smt/solver/seq-monadic.ts +728 -0
- package/src/prover/smt/solver/seq-range-predicate.ts +235 -0
- package/src/prover/smt/solver/seq-re-rewriter.ts +1170 -0
- package/src/prover/smt/solver/seq-re-util.ts +758 -0
- package/src/prover/smt/solver/seq-regex-bisim.ts +144 -0
- package/src/prover/smt/solver/seq-regex-live.ts +200 -0
- package/src/prover/smt/solver/seq-rewriter.ts +2402 -0
- package/src/prover/smt/solver/seq-skolem.ts +317 -0
- package/src/prover/smt/solver/seq-subset.ts +165 -0
- package/src/prover/smt/solver/seq-view-witness.ts +353 -0
- package/src/prover/smt/solver/smt-consequences.ts +297 -0
- package/src/prover/smt/solver/smt-kernel.ts +83 -0
- package/src/prover/smt/solver/smt-logics.ts +66 -0
- package/src/prover/smt/solver/smt-setup.ts +496 -0
- package/src/prover/smt/solver/smt2-printer.ts +765 -0
- package/src/prover/smt/solver/smtlib-parser.ts +3126 -316
- package/src/prover/smt/solver/smtlib-script.ts +1631 -0
- package/src/prover/smt/solver/solve-eqs.ts +929 -0
- package/src/prover/smt/solver/solver.ts +113 -22
- package/src/prover/smt/solver/static-features.ts +314 -0
- package/src/prover/smt/solver/sticky-generations.ts +0 -1
- package/src/prover/smt/solver/tactic.ts +1986 -0
- package/src/prover/smt/solver/term-enumeration.ts +205 -0
- package/src/prover/smt/solver/theory-array.ts +1240 -0
- package/src/prover/smt/solver/theory-bv.ts +1200 -0
- package/src/prover/smt/solver/theory-char.ts +355 -0
- package/src/prover/smt/solver/theory-datatype.ts +598 -0
- package/src/prover/smt/solver/theory-finite-set.ts +30 -0
- package/src/prover/smt/solver/theory-fpa.ts +389 -0
- package/src/prover/smt/solver/theory-lra-axioms.ts +53 -2
- package/src/prover/smt/solver/theory-lra-bounds.ts +31 -6
- package/src/prover/smt/solver/theory-lra-final-check.ts +56 -70
- package/src/prover/smt/solver/theory-lra-internalize.ts +94 -27
- package/src/prover/smt/solver/theory-lra-model.ts +20 -35
- package/src/prover/smt/solver/theory-lra-propagate.ts +24 -26
- package/src/prover/smt/solver/theory-lra-state.ts +6 -22
- package/src/prover/smt/solver/theory-lra.ts +188 -1
- package/src/prover/smt/solver/theory-pb.ts +934 -0
- package/src/prover/smt/solver/theory-polymorphism.ts +58 -0
- package/src/prover/smt/solver/theory-recfun.ts +307 -0
- package/src/prover/smt/solver/theory-seq-regex.ts +867 -0
- package/src/prover/smt/solver/theory-seq-solve.ts +1373 -0
- package/src/prover/smt/solver/theory-seq-state.ts +871 -0
- package/src/prover/smt/solver/theory-seq.ts +961 -0
- package/src/prover/smt/solver/theory.ts +189 -0
- package/src/prover/smt/solver/tseitin-cnf.ts +485 -0
- package/src/prover/smt/solver/var-subst.ts +36 -14
- package/src/prover/smt/table.ts +3 -2
- package/src/prover/smt/types.ts +146 -0
- package/src/prover/tlapm/cache.ts +50 -19
- package/src/prover/tlapm/tlapm.ts +15 -9
- package/src/prover/tlapm-library.ts +3208 -0
- package/src/prover/verdict.ts +9 -0
- package/src/prover/zenon/check/check.ts +1 -2
- package/src/prover/zenon/check/records.ts +1 -1
- package/src/prover/zenon/check/relations.ts +2 -1
- package/src/prover/zenon/check/rewrites.ts +2 -15
- package/src/prover/zenon/check/sets.ts +40 -58
- package/src/prover/zenon/check/terms.ts +1 -2
- package/src/prover/zenon/decide.ts +10 -7
- package/src/prover/zenon/encode.ts +0 -2
- package/src/prover/zenon/eqrel.ts +2 -2
- package/src/prover/zenon/equality-rules.ts +24 -2
- package/src/prover/zenon/expr.ts +28 -2
- package/src/prover/zenon/index-table.ts +11 -21
- package/src/prover/zenon/isar-case.ts +0 -2
- package/src/prover/zenon/isar.ts +35 -37
- package/src/prover/zenon/llproof.ts +2 -13
- package/src/prover/zenon/mltoll.ts +12 -30
- package/src/prover/zenon/node.ts +20 -5
- package/src/prover/zenon/parse.ts +9 -8
- package/src/prover/zenon/phrase.ts +0 -2
- package/src/prover/zenon/proof.ts +0 -2
- package/src/prover/zenon/propositional-rules.ts +0 -2
- package/src/prover/zenon/rules.ts +2 -3
- package/src/prover/zenon/search.ts +11 -19
- package/src/prover/zenon/tla/functions.ts +2 -4
- package/src/prover/zenon/tla/llproof.ts +9 -13
- package/src/prover/zenon/tla/records.ts +1 -8
- package/src/prover/zenon/tla/rewrites.ts +0 -2
- package/src/prover/zenon/tla/sets.ts +22 -22
- package/src/prover/zenon/tla/terms.ts +0 -2
- package/src/prover/zenon/tla/tla.ts +7 -8
- package/src/randomization.ts +1 -4
- package/src/resolve.ts +67 -143
- package/src/runtime.ts +15 -18
- package/src/sany/builtins.ts +1 -3
- package/src/sany/generation.ts +394 -0
- package/src/sany/generator-def-entries.ts +210 -0
- package/src/sany/generator-definitions.ts +686 -0
- package/src/sany/generator-expressions.ts +1100 -0
- package/src/sany/generator-proofs.ts +662 -0
- package/src/sany/generator.ts +160 -2683
- package/src/sany/levels.ts +5 -11
- package/src/sany/lexer.ts +57 -60
- package/src/sany/linter.ts +0 -3
- package/src/sany/nodes.ts +6 -13
- package/src/sany/report.ts +3 -6
- package/src/sany/selectors.ts +35 -38
- package/src/sany/xml.ts +23 -55
- package/src/search.ts +206 -44
- package/src/simulate.ts +202 -184
- package/src/spec-description.ts +56 -82
- package/src/standard-definitions.ts +4 -3
- package/src/standard-modules.ts +17 -44
- package/src/state-graph.ts +3 -17
- package/src/subexpressions.ts +38 -47
- package/src/substitution-levels.ts +5 -4
- package/src/successors.ts +20 -43
- package/src/symmetry.ts +6 -21
- package/src/tableau.ts +2 -2
- package/src/tlc/fingerprint.ts +96 -30
- package/src/tlc/jar-data.ts +0 -1
- package/src/tlc/messages.ts +22 -36
- package/src/tlc/order.ts +22 -47
- package/src/tlc/random.ts +22 -34
- package/src/tlc/replay.ts +21 -48
- package/src/tlc/summary.ts +64 -58
- package/src/tlc/tlc-operators.ts +6 -17
- package/src/tlc/tlcext-operators.ts +54 -26
- package/src/tlc/trace-spec.ts +26 -51
- package/src/tlc/value-stream.ts +9 -13
- package/src/tlc/version.ts +0 -1
- package/src/typed-tier.ts +6535 -0
- package/src/typescript/actions.ts +2 -4
- package/src/typescript/collections.ts +34 -20
- package/src/typescript/compile.ts +1 -3
- package/src/typescript/formula.ts +5 -3
- package/src/typescript/model.ts +4 -5
- package/src/typescript/steps.ts +20 -18
- package/src/values.ts +136 -338
- package/src/prover/isabelle/isar/statement.ts +0 -52
- package/src/prover/smt/solver/z3-license.txt +0 -10
- package/src/prover/tlaps-module.ts +0 -510
- package/src/prover/zenon/LICENSE +0 -32
package/src/parser.ts
CHANGED
|
@@ -35,14 +35,26 @@ interface InfixOperator extends Precedence {
|
|
|
35
35
|
isAssociative: boolean;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
interface
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
interface TlapmOperand {
|
|
39
|
+
start: number;
|
|
40
|
+
end: number;
|
|
41
|
+
expression: Expression;
|
|
42
|
+
joinedBy?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface TlapmOperator extends Precedence {
|
|
46
|
+
start: number;
|
|
47
|
+
end: number;
|
|
48
|
+
fixity: "Prefix" | "Infix" | "Postfix";
|
|
49
|
+
isLeft: boolean;
|
|
50
|
+
joins?: string;
|
|
51
|
+
apply: (operands: TlapmOperand[]) => Expression;
|
|
41
52
|
}
|
|
42
53
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
54
|
+
type TlapmItem = TlapmOperand | TlapmOperator;
|
|
55
|
+
|
|
56
|
+
const POSTFIX_PRECEDENCE = { low: 15, high: 15 };
|
|
57
|
+
const FUNCTION_PRECEDENCE = { low: 16, high: 16 };
|
|
46
58
|
|
|
47
59
|
const createInfix = (low: number, high: number, isAssociative: boolean) => ({
|
|
48
60
|
low,
|
|
@@ -150,6 +162,49 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
|
|
|
150
162
|
|
|
151
163
|
const NEGATION_PRECEDENCE = 12;
|
|
152
164
|
|
|
165
|
+
// tlapm's Optable has SANY's infix precedences, groups % and \X to the left with the operators
|
|
166
|
+
// SANY takes as associative, and has these prefix precedences. A function application and a field
|
|
167
|
+
// bind above every operator.
|
|
168
|
+
const isTlapmLeft = (text: string, infix: InfixOperator) =>
|
|
169
|
+
infix.isAssociative || text === "%" || text === "\\X";
|
|
170
|
+
|
|
171
|
+
const TLAPM_PREFIX_OPERATORS = new Map<string, Precedence>([
|
|
172
|
+
["~", { low: 4, high: 4 }],
|
|
173
|
+
["[]", { low: 4, high: 15 }],
|
|
174
|
+
["<>", { low: 4, high: 15 }],
|
|
175
|
+
["ENABLED", { low: 4, high: 15 }],
|
|
176
|
+
["UNCHANGED", { low: 4, high: 15 }],
|
|
177
|
+
["SUBSET", { low: 8, high: 8 }],
|
|
178
|
+
["UNION", { low: 8, high: 8 }],
|
|
179
|
+
["DOMAIN", { low: 9, high: 9 }],
|
|
180
|
+
["-", { low: 12, high: 12 }],
|
|
181
|
+
["-.", { low: 12, high: 12 }],
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
const TLAPM_SELECTION_PRECEDENCE = { low: 17, high: 17 };
|
|
185
|
+
|
|
186
|
+
const TLAPM_ATOM_STARTS = new Set([
|
|
187
|
+
"(",
|
|
188
|
+
"[",
|
|
189
|
+
"{",
|
|
190
|
+
"<<",
|
|
191
|
+
"@",
|
|
192
|
+
"\\A",
|
|
193
|
+
"\\E",
|
|
194
|
+
"\\AA",
|
|
195
|
+
"\\EE",
|
|
196
|
+
"IF",
|
|
197
|
+
"CASE",
|
|
198
|
+
"LET",
|
|
199
|
+
"CHOOSE",
|
|
200
|
+
"LAMBDA",
|
|
201
|
+
"WF_",
|
|
202
|
+
"SF_",
|
|
203
|
+
]);
|
|
204
|
+
|
|
205
|
+
// The brackets whose contents tlapm reads without committing, so a failure in them backtracks.
|
|
206
|
+
const TLAPM_OPEN_BRACKETS = new Set(["(", "{", "<<"]);
|
|
207
|
+
|
|
153
208
|
const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
|
|
154
209
|
|
|
155
210
|
const DEFINABLE_POSTFIX_OPERATORS = new Set([...POSTFIX_OPERATORS, "'"]);
|
|
@@ -262,14 +317,16 @@ interface Point {
|
|
|
262
317
|
column: number;
|
|
263
318
|
}
|
|
264
319
|
|
|
265
|
-
// A location as tlapm's Loc.locus has it, with stop just past the end.
|
|
266
320
|
interface Span {
|
|
267
321
|
start: Point;
|
|
268
322
|
stop: Point;
|
|
269
323
|
}
|
|
270
324
|
|
|
271
|
-
|
|
272
|
-
|
|
325
|
+
interface RangedName {
|
|
326
|
+
name: string;
|
|
327
|
+
range: SourceRange;
|
|
328
|
+
}
|
|
329
|
+
|
|
273
330
|
type Preproof =
|
|
274
331
|
| { kind: "Leaf"; proof: Proof; span: Span; first: number; end: number }
|
|
275
332
|
| {
|
|
@@ -319,13 +376,30 @@ const getSpan = (range: SourceRange): Span => ({
|
|
|
319
376
|
stop: { line: range.end.line, column: range.end.column + 1 },
|
|
320
377
|
});
|
|
321
378
|
|
|
322
|
-
|
|
379
|
+
interface ProofErrorEnds {
|
|
380
|
+
printed: string;
|
|
381
|
+
set: string;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const NO_NEWLINES: ProofErrorEnds = { printed: "", set: "" };
|
|
385
|
+
|
|
386
|
+
const PRINTED_NEWLINE: ProofErrorEnds = { printed: "\n", set: "" };
|
|
387
|
+
|
|
388
|
+
const NEWLINES: ProofErrorEnds = { printed: "\n", set: "\n" };
|
|
389
|
+
|
|
390
|
+
// p_parser.ml ends some of the messages it prints, and of those it sets, with a newline.
|
|
391
|
+
const createProofError = (
|
|
392
|
+
message: string,
|
|
393
|
+
span: Span,
|
|
394
|
+
ends: ProofErrorEnds,
|
|
395
|
+
exception = "Proof.Parser",
|
|
396
|
+
) =>
|
|
323
397
|
new TlaSyntaxError(message, undefined, {
|
|
324
|
-
reason: message
|
|
398
|
+
reason: `${message}${ends.set}`,
|
|
325
399
|
line: span.start.line,
|
|
326
400
|
column: span.start.column,
|
|
327
401
|
isEnd: false,
|
|
328
|
-
proof: { stop: span.stop, exception },
|
|
402
|
+
proof: { stop: span.stop, exception, printed: `${message}${ends.printed}` },
|
|
329
403
|
});
|
|
330
404
|
|
|
331
405
|
const FACT_KEYWORDS = new Set([
|
|
@@ -375,7 +449,6 @@ const isOperatorExpression = (expression: Expression) =>
|
|
|
375
449
|
? INFIX_OPERATORS.has(expression.operator)
|
|
376
450
|
: POSTFIX_OPERATORS.has(expression.operator)));
|
|
377
451
|
|
|
378
|
-
// SANY's \land and \lor are infix operators only, not bullets.
|
|
379
452
|
const isBullet = (token: Token) =>
|
|
380
453
|
(token.text === "/\\" || token.text === "\\/") &&
|
|
381
454
|
token.spelling !== "\\land" &&
|
|
@@ -405,11 +478,8 @@ const getOperatorApplication = (
|
|
|
405
478
|
}
|
|
406
479
|
};
|
|
407
480
|
|
|
408
|
-
// MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
|
|
409
|
-
// root module's file, from its ---- MODULE line to the next ==== line.
|
|
410
481
|
export const extractModule = (source: string, name: string) => {
|
|
411
482
|
const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
|
|
412
|
-
// MonolithSpecExtractor.module writes each line it reads, with its own line terminator.
|
|
413
483
|
const lines = source.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
414
484
|
const start = lines.findIndex((line) => header.test(line));
|
|
415
485
|
if (start === -1) return undefined;
|
|
@@ -417,8 +487,6 @@ export const extractModule = (source: string, name: string) => {
|
|
|
417
487
|
return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
|
|
418
488
|
};
|
|
419
489
|
|
|
420
|
-
// The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some
|
|
421
|
-
// modules that SANY does not. The sany mode leaves SANY's post-parse checks to the SANY generator.
|
|
422
490
|
type ParseMode = "tlc" | "tlapm" | "sany";
|
|
423
491
|
|
|
424
492
|
const parse = (
|
|
@@ -428,14 +496,19 @@ const parse = (
|
|
|
428
496
|
): Module => {
|
|
429
497
|
const isTlapm = mode === "tlapm";
|
|
430
498
|
const tokens = isTlapm ? sourceTokens : sourceTokens.map(readAtAsIdentifier);
|
|
431
|
-
|
|
432
|
-
const
|
|
499
|
+
let cursor = 0;
|
|
500
|
+
const offsideColumns: number[] = [];
|
|
501
|
+
let lastOperator: Precedence | undefined;
|
|
502
|
+
let precedingOperator: Precedence | undefined;
|
|
433
503
|
const recursiveScopes: Set<string>[] = [];
|
|
434
504
|
const factNames = new Set<string>();
|
|
435
|
-
// The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
|
|
436
505
|
const proofScopes: Set<string>[] = [];
|
|
437
|
-
// SANY reports a location in a nested module as one in the module of its file.
|
|
438
506
|
const openModules: Module[] = [];
|
|
507
|
+
// Pco's backtracking: a failure not past a commit rewinds to its attempt's frame.
|
|
508
|
+
let attemptFrame: number | undefined;
|
|
509
|
+
let attemptCount = 0;
|
|
510
|
+
const restorePoints: number[] = [];
|
|
511
|
+
const backtracks = new WeakMap<TlaSyntaxError, number>();
|
|
439
512
|
|
|
440
513
|
const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
|
|
441
514
|
|
|
@@ -449,7 +522,7 @@ const parse = (
|
|
|
449
522
|
const tokenEnds: SourceLocation[] = [];
|
|
450
523
|
const tokenRanges: SourceRange[] = [];
|
|
451
524
|
|
|
452
|
-
const getRange = (startIndex: number, endIndex = cursor
|
|
525
|
+
const getRange = (startIndex: number, endIndex = cursor - 1) => {
|
|
453
526
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
454
527
|
if (cached) return cached;
|
|
455
528
|
const last = tokens[endIndex];
|
|
@@ -464,17 +537,16 @@ const parse = (
|
|
|
464
537
|
return range;
|
|
465
538
|
};
|
|
466
539
|
|
|
467
|
-
// Each node gets its own location object from getLocation, so the range can be set in place.
|
|
468
540
|
const withRange = <Node extends { location: SourceLocation }>(node: Node, startIndex: number) => {
|
|
469
541
|
node.location.range = getRange(startIndex);
|
|
470
542
|
return node;
|
|
471
543
|
};
|
|
472
544
|
|
|
473
|
-
const peekRaw = (offset = 0) => tokens[Math.min(cursor
|
|
545
|
+
const peekRaw = (offset = 0) => tokens[Math.min(cursor + offset, tokens.length - 1)];
|
|
474
546
|
|
|
475
547
|
const peek = (offset = 0) => {
|
|
476
548
|
const token = peekRaw(offset);
|
|
477
|
-
const offsideColumn =
|
|
549
|
+
const offsideColumn = offsideColumns[offsideColumns.length - 1];
|
|
478
550
|
if (offsideColumn === undefined || offsideColumn === NO_OFFSIDE || token.kind === "End") {
|
|
479
551
|
return token;
|
|
480
552
|
}
|
|
@@ -484,20 +556,10 @@ const parse = (
|
|
|
484
556
|
const describeToken = (token: Token) =>
|
|
485
557
|
token.kind === "End" ? "end of input" : JSON.stringify(token.text);
|
|
486
558
|
|
|
487
|
-
const unitStart = { index: 0 };
|
|
488
|
-
|
|
489
|
-
const getDefinitionEnd = (fallback: { line: number; column: number }) => {
|
|
490
|
-
const equals = tokens
|
|
491
|
-
.slice(unitStart.index, cursor.index)
|
|
492
|
-
.find((token) => token.text === "==" && token.kind !== "String");
|
|
493
|
-
return equals ? { line: equals.line, column: equals.endColumn + 1 } : fallback;
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
// tlapm reads TRUE, FALSE, BOOLEAN, STRING and the other keywords only as keywords.
|
|
497
559
|
const isTlapmKeyword = (token: Token) =>
|
|
498
560
|
isTlapm && (token.kind === "Keyword" || BUILTIN_CONSTANTS.has(token.text));
|
|
499
561
|
|
|
500
|
-
const getPreviousEnd = (position:
|
|
562
|
+
const getPreviousEnd = (position: Point) => {
|
|
501
563
|
const previous = tokens.findLast(
|
|
502
564
|
(token) =>
|
|
503
565
|
token.kind !== "End" &&
|
|
@@ -508,6 +570,7 @@ const parse = (
|
|
|
508
570
|
};
|
|
509
571
|
|
|
510
572
|
const getTlapmName = (token: Token) => {
|
|
573
|
+
if (token.kind === "End") return "EOF";
|
|
511
574
|
if (/^-{4,}$/.test(token.text)) return "----";
|
|
512
575
|
if (/^={4,}$/.test(token.text)) return "====";
|
|
513
576
|
if (TLAPM_PUNCTUATION.has(token.text) || token.kind === "Number" || token.kind === "Decimal")
|
|
@@ -518,44 +581,78 @@ const parse = (
|
|
|
518
581
|
return token.kind === "Identifier" ? `identifier ${token.text}` : `operator ${token.text}`;
|
|
519
582
|
};
|
|
520
583
|
|
|
521
|
-
const createError = (
|
|
522
|
-
message: string,
|
|
523
|
-
token: { line: number; column: number } = peekRaw(),
|
|
524
|
-
unexpected?: Token,
|
|
525
|
-
) => {
|
|
584
|
+
const createError = (message: string, token: Point = peekRaw(), unexpected?: Token) => {
|
|
526
585
|
const end = tokens[tokens.length - 1];
|
|
527
586
|
const isEnd = token.line === end.line && token.column === end.column;
|
|
528
|
-
|
|
587
|
+
const error = new TlaSyntaxError(
|
|
529
588
|
`${message} at line ${token.line}, column ${token.column}`,
|
|
530
589
|
undefined,
|
|
531
590
|
getFailure(message, token, isEnd, unexpected),
|
|
532
591
|
);
|
|
592
|
+
if (attemptFrame !== undefined) backtracks.set(error, attemptFrame);
|
|
593
|
+
return error;
|
|
533
594
|
};
|
|
534
595
|
|
|
596
|
+
// tlapm reports a failure after the last token it read before the outermost attempt around it.
|
|
535
597
|
const getFailure = (
|
|
536
598
|
message: string,
|
|
537
|
-
token:
|
|
599
|
+
token: Point,
|
|
538
600
|
isEnd: boolean,
|
|
539
601
|
unexpected: Token | undefined,
|
|
540
602
|
) => {
|
|
541
|
-
if (isTlapm
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
reason: `Unexpected ${getTlapmName(unexpected)}`,
|
|
548
|
-
...getPreviousEnd(unexpected),
|
|
549
|
-
isEnd,
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
return { reason: message, line: token.line, column: token.column, isEnd };
|
|
603
|
+
if (!isTlapm) return { reason: message, line: token.line, column: token.column, isEnd };
|
|
604
|
+
const restored = tokens[restorePoints[0] - 1];
|
|
605
|
+
const reason = unexpected ? `Unexpected ${getTlapmName(unexpected)}` : message;
|
|
606
|
+
if (restored) return { reason, line: restored.line, column: restored.endColumn + 1, isEnd };
|
|
607
|
+
const position = unexpected ? getPreviousEnd(unexpected) : token;
|
|
608
|
+
return { reason, line: position.line, column: position.column, isEnd };
|
|
553
609
|
};
|
|
554
610
|
|
|
555
611
|
const createUnexpected = (message: string, token: Token = peekRaw()) =>
|
|
556
612
|
createError(message, token, token);
|
|
557
613
|
|
|
558
|
-
//
|
|
614
|
+
// Pco.attempt: a failure that backtracks to it leaves the input as it was, and gives undefined.
|
|
615
|
+
const attempt = <Result>(parse: () => Result): Result | undefined => {
|
|
616
|
+
const saved = {
|
|
617
|
+
cursor,
|
|
618
|
+
frame: attemptFrame,
|
|
619
|
+
restorePoints: restorePoints.length,
|
|
620
|
+
offsideColumns: offsideColumns.length,
|
|
621
|
+
recursiveScopes: recursiveScopes.length,
|
|
622
|
+
};
|
|
623
|
+
const frame = ++attemptCount;
|
|
624
|
+
attemptFrame = frame;
|
|
625
|
+
try {
|
|
626
|
+
const result = parse();
|
|
627
|
+
attemptFrame = saved.frame;
|
|
628
|
+
return result;
|
|
629
|
+
} catch (error) {
|
|
630
|
+
if (!(error instanceof TlaSyntaxError) || backtracks.get(error) !== frame) throw error;
|
|
631
|
+
cursor = saved.cursor;
|
|
632
|
+
attemptFrame = saved.frame;
|
|
633
|
+
restorePoints.length = saved.restorePoints;
|
|
634
|
+
offsideColumns.length = saved.offsideColumns;
|
|
635
|
+
recursiveScopes.length = saved.recursiveScopes;
|
|
636
|
+
return undefined;
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
// Pco.commit: a failure in parse no longer backtracks.
|
|
641
|
+
const commit = <Result>(parse: () => Result) => {
|
|
642
|
+
const frame = attemptFrame;
|
|
643
|
+
attemptFrame = undefined;
|
|
644
|
+
const result = parse();
|
|
645
|
+
attemptFrame = frame;
|
|
646
|
+
return result;
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
const withRestorePoint = <Result>(parse: () => Result) => {
|
|
650
|
+
restorePoints.push(cursor);
|
|
651
|
+
const result = parse();
|
|
652
|
+
restorePoints.pop();
|
|
653
|
+
return result;
|
|
654
|
+
};
|
|
655
|
+
|
|
559
656
|
const isFieldName = (token: Token) =>
|
|
560
657
|
(token.kind === "Identifier" ||
|
|
561
658
|
token.kind === "Keyword" ||
|
|
@@ -569,14 +666,14 @@ const parse = (
|
|
|
569
666
|
|
|
570
667
|
const advance = () => {
|
|
571
668
|
const token = peek();
|
|
572
|
-
if (token.kind === "End") throw
|
|
573
|
-
cursor
|
|
669
|
+
if (token.kind === "End") throw createUnexpected("Unexpected end of expression");
|
|
670
|
+
cursor++;
|
|
574
671
|
return token;
|
|
575
672
|
};
|
|
576
673
|
|
|
577
674
|
const accept = (text: string) => {
|
|
578
675
|
if (!isAt(text)) return false;
|
|
579
|
-
cursor
|
|
676
|
+
cursor++;
|
|
580
677
|
return true;
|
|
581
678
|
};
|
|
582
679
|
|
|
@@ -592,18 +689,18 @@ const parse = (
|
|
|
592
689
|
throw createUnexpected(`Expected an identifier but found ${describeToken(token)}`);
|
|
593
690
|
}
|
|
594
691
|
if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
|
|
595
|
-
cursor
|
|
692
|
+
cursor++;
|
|
596
693
|
return token.text;
|
|
597
694
|
};
|
|
598
695
|
|
|
599
696
|
const expectName = () => {
|
|
600
|
-
const start = cursor
|
|
697
|
+
const start = cursor;
|
|
601
698
|
return { name: expectIdentifier(), range: getRange(start) };
|
|
602
699
|
};
|
|
603
700
|
|
|
604
|
-
const getNames = (names:
|
|
605
|
-
names: names.map((
|
|
606
|
-
ranges: names.map((
|
|
701
|
+
const getNames = (names: RangedName[]) => ({
|
|
702
|
+
names: names.map((named) => named.name),
|
|
703
|
+
ranges: names.map((named) => named.range),
|
|
607
704
|
});
|
|
608
705
|
|
|
609
706
|
const expectSubstitutionTarget = () => {
|
|
@@ -614,25 +711,22 @@ const parse = (
|
|
|
614
711
|
INFIX_OPERATORS.has(token.text) ||
|
|
615
712
|
DEFINABLE_POSTFIX_OPERATORS.has(token.text));
|
|
616
713
|
if (!isOperator) return expectIdentifier();
|
|
617
|
-
cursor
|
|
714
|
+
cursor++;
|
|
618
715
|
return token.text;
|
|
619
716
|
};
|
|
620
717
|
|
|
621
|
-
|
|
622
|
-
const keywordModuleName = { isUsed: false };
|
|
718
|
+
let isKeywordModuleName = false;
|
|
623
719
|
const expectModuleName = () => {
|
|
624
720
|
if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
|
|
625
|
-
|
|
721
|
+
isKeywordModuleName = true;
|
|
626
722
|
return advance().text;
|
|
627
723
|
};
|
|
628
724
|
|
|
629
|
-
// tlapm reads a bracketed expression apart from the junction list around it, whose alignment
|
|
630
|
-
// SANY keeps applying.
|
|
631
725
|
const withBracketLayout = <Result>(parse: () => Result) => {
|
|
632
726
|
if (!isTlapm) return parse();
|
|
633
|
-
|
|
727
|
+
offsideColumns.push(NO_OFFSIDE);
|
|
634
728
|
const result = parse();
|
|
635
|
-
|
|
729
|
+
offsideColumns.pop();
|
|
636
730
|
return result;
|
|
637
731
|
};
|
|
638
732
|
|
|
@@ -642,17 +736,17 @@ const parse = (
|
|
|
642
736
|
return items;
|
|
643
737
|
};
|
|
644
738
|
|
|
645
|
-
const isBoundAhead = () => {
|
|
646
|
-
if (isAt("<<")) {
|
|
647
|
-
|
|
648
|
-
while (peek(offset
|
|
649
|
-
return isAt(">>", offset
|
|
739
|
+
const isBoundAhead = (start = 0) => {
|
|
740
|
+
if (isAt("<<", start)) {
|
|
741
|
+
let offset = start + 1;
|
|
742
|
+
while (peek(offset).kind === "Identifier" || isAt(",", offset)) offset++;
|
|
743
|
+
return isAt(">>", offset) && isAt("\\in", offset + 1);
|
|
650
744
|
}
|
|
651
|
-
|
|
652
|
-
while (peek(offset
|
|
653
|
-
if (isAt("\\in", offset
|
|
654
|
-
if (!isAt(",", offset
|
|
655
|
-
offset
|
|
745
|
+
let offset = start;
|
|
746
|
+
while (peek(offset).kind === "Identifier") {
|
|
747
|
+
if (isAt("\\in", offset + 1)) return true;
|
|
748
|
+
if (!isAt(",", offset + 1)) return false;
|
|
749
|
+
offset += 2;
|
|
656
750
|
}
|
|
657
751
|
return false;
|
|
658
752
|
};
|
|
@@ -660,14 +754,9 @@ const parse = (
|
|
|
660
754
|
const isLabelAhead = () => {
|
|
661
755
|
if (isAt("::")) return true;
|
|
662
756
|
if (!isAt("(")) return false;
|
|
663
|
-
|
|
664
|
-
while (peek(offset
|
|
665
|
-
|
|
666
|
-
return (
|
|
667
|
-
peek(offset.value).kind === "Identifier" &&
|
|
668
|
-
isAt(")", offset.value + 1) &&
|
|
669
|
-
isAt("::", offset.value + 2)
|
|
670
|
-
);
|
|
757
|
+
let offset = 1;
|
|
758
|
+
while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) offset += 2;
|
|
759
|
+
return peek(offset).kind === "Identifier" && isAt(")", offset + 1) && isAt("::", offset + 2);
|
|
671
760
|
};
|
|
672
761
|
|
|
673
762
|
const parsePattern = (): BoundPattern => {
|
|
@@ -680,20 +769,26 @@ const parse = (
|
|
|
680
769
|
return { kind: "Tuple", names: names.names, ranges: names.ranges };
|
|
681
770
|
};
|
|
682
771
|
|
|
683
|
-
|
|
684
|
-
|
|
772
|
+
// tlapm's func_boundeds reads another bound only where names and \in follow the comma.
|
|
773
|
+
const parseBounds = () => {
|
|
774
|
+
const parseBound = () => {
|
|
685
775
|
const patterns = parseSeparated(parsePattern);
|
|
686
776
|
expect("\\in");
|
|
687
|
-
const set = parseExpression
|
|
777
|
+
const set = commit(parseExpression);
|
|
688
778
|
return patterns.map((pattern) => ({ pattern, set }));
|
|
689
|
-
}
|
|
779
|
+
};
|
|
780
|
+
const bounds = [parseBound()];
|
|
781
|
+
while (isAt(",") && (!isTlapm || isBoundAhead(1))) {
|
|
782
|
+
advance();
|
|
783
|
+
bounds.push(parseBound());
|
|
784
|
+
}
|
|
785
|
+
return bounds.flat();
|
|
786
|
+
};
|
|
690
787
|
|
|
691
|
-
// TLAplusParser.NoOpExtension, which leaves the arguments of the subscript's last name for a
|
|
692
|
-
// fairness expression to take as its action.
|
|
693
788
|
const parseSubscript = (): { subscript: Expression; lastArgs: Expression[] } => {
|
|
694
789
|
const token = peek();
|
|
695
790
|
if (isTlapm && BUILTIN_CONSTANTS.has(token.text)) {
|
|
696
|
-
const start = cursor
|
|
791
|
+
const start = cursor;
|
|
697
792
|
advance();
|
|
698
793
|
const constant: Expression = {
|
|
699
794
|
kind: "Identifier",
|
|
@@ -705,11 +800,11 @@ const parse = (
|
|
|
705
800
|
const isTlapmAtom =
|
|
706
801
|
isTlapm && (token.kind === "Number" || token.kind === "String" || isAt("@"));
|
|
707
802
|
if (token.kind === "Identifier") {
|
|
708
|
-
const start = cursor
|
|
803
|
+
const start = cursor;
|
|
709
804
|
const names: string[] = [];
|
|
710
805
|
const selectorRanges: SourceRange[] = [];
|
|
711
806
|
const parseElement = (isApplicable: boolean) => {
|
|
712
|
-
selectorRanges.push(getRange(cursor
|
|
807
|
+
selectorRanges.push(getRange(cursor, cursor));
|
|
713
808
|
names.push(expectIdentifier());
|
|
714
809
|
return isApplicable && isAt("(") ? parseArguments() : [];
|
|
715
810
|
};
|
|
@@ -736,7 +831,7 @@ const parse = (
|
|
|
736
831
|
return { subscript, lastArgs: elementArgs[elementArgs.length - 1] };
|
|
737
832
|
}
|
|
738
833
|
if (isAt("<<") || isAt("(") || isAt("{") || isAt("[") || isTlapmAtom) {
|
|
739
|
-
const start = cursor
|
|
834
|
+
const start = cursor;
|
|
740
835
|
const subscript = parsePrimary();
|
|
741
836
|
return {
|
|
742
837
|
subscript: subscript.location.range ? subscript : withRange(subscript, start),
|
|
@@ -761,8 +856,6 @@ const parse = (
|
|
|
761
856
|
);
|
|
762
857
|
};
|
|
763
858
|
|
|
764
|
-
// SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
|
|
765
|
-
// the start of an expression, such as -.(1), otherwise.
|
|
766
859
|
const isStandalone = (operator: Token) => {
|
|
767
860
|
const token = peek(1);
|
|
768
861
|
return (
|
|
@@ -782,8 +875,6 @@ const parse = (
|
|
|
782
875
|
token.kind === "Operator" &&
|
|
783
876
|
(INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
|
|
784
877
|
isStandalone(token);
|
|
785
|
-
// tlapm reads a prefix operator as the start of an expression, and (+), (-), (/) and (\X) as
|
|
786
|
-
// the operators \oplus, \ominus, \oslash and \otimes.
|
|
787
878
|
const isTlapmIncomplete =
|
|
788
879
|
isTlapm &&
|
|
789
880
|
token.kind !== "String" &&
|
|
@@ -803,7 +894,7 @@ const parse = (
|
|
|
803
894
|
throw createError("\\X may not be used as an infix operator", token);
|
|
804
895
|
}
|
|
805
896
|
if (isOperatorAlone) {
|
|
806
|
-
cursor
|
|
897
|
+
cursor++;
|
|
807
898
|
return withRange(
|
|
808
899
|
{
|
|
809
900
|
kind: "OperatorReference",
|
|
@@ -811,18 +902,18 @@ const parse = (
|
|
|
811
902
|
spelling: token.spelling,
|
|
812
903
|
location: getLocation(token),
|
|
813
904
|
},
|
|
814
|
-
cursor
|
|
905
|
+
cursor - 1,
|
|
815
906
|
);
|
|
816
907
|
}
|
|
817
908
|
const isPrefixAlone =
|
|
818
909
|
token.kind !== "String" && OPERATOR_ARGUMENTS.has(token.text) && isStandalone(token);
|
|
819
910
|
if (isPrefixAlone) {
|
|
820
|
-
cursor
|
|
911
|
+
cursor++;
|
|
821
912
|
const location = getLocation(token);
|
|
822
913
|
if (POSTFIX_OPERATORS.has(token.text)) {
|
|
823
914
|
return withRange(
|
|
824
915
|
{ kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
|
|
825
|
-
cursor
|
|
916
|
+
cursor - 1,
|
|
826
917
|
);
|
|
827
918
|
}
|
|
828
919
|
const argument: Expression = { kind: "Identifier", name: "argument", location };
|
|
@@ -834,11 +925,11 @@ const parse = (
|
|
|
834
925
|
operator: token.text,
|
|
835
926
|
location: getLocation(token),
|
|
836
927
|
},
|
|
837
|
-
cursor
|
|
928
|
+
cursor - 1,
|
|
838
929
|
);
|
|
839
930
|
}
|
|
840
931
|
if (isAt("LAMBDA")) {
|
|
841
|
-
const start = cursor
|
|
932
|
+
const start = cursor;
|
|
842
933
|
advance();
|
|
843
934
|
const params = getNames(parseSeparated(expectName));
|
|
844
935
|
expect(":");
|
|
@@ -854,34 +945,43 @@ const parse = (
|
|
|
854
945
|
start,
|
|
855
946
|
);
|
|
856
947
|
}
|
|
857
|
-
return parseExpression();
|
|
948
|
+
if (!isTlapm) return parseExpression();
|
|
949
|
+
// tlapm's oparg reads an operator where an expression doesn't start, and fails otherwise.
|
|
950
|
+
const argument = attempt(parseExpression);
|
|
951
|
+
if (argument) return argument;
|
|
952
|
+
throw createUnexpected(`Unexpected ${describeToken(peek())}`);
|
|
858
953
|
};
|
|
859
954
|
|
|
860
|
-
|
|
955
|
+
let lastArgumentsRange: SourceRange | undefined;
|
|
861
956
|
|
|
862
957
|
const parseArguments = () =>
|
|
863
958
|
withBracketLayout(() => {
|
|
864
|
-
const start = cursor
|
|
959
|
+
const start = cursor;
|
|
865
960
|
expect("(");
|
|
866
961
|
const args = parseSeparated(parseArgument);
|
|
867
962
|
expect(")");
|
|
868
|
-
|
|
963
|
+
lastArgumentsRange = getRange(start);
|
|
869
964
|
return args;
|
|
870
965
|
});
|
|
871
966
|
|
|
872
967
|
const parseJunctionList = (): Expression => {
|
|
873
968
|
const bullet = advance();
|
|
874
|
-
|
|
969
|
+
offsideColumns.push(bullet.column);
|
|
875
970
|
const operands = [parseExpression()];
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
971
|
+
const parseItems = () => {
|
|
972
|
+
while (
|
|
973
|
+
isBullet(peekRaw()) &&
|
|
974
|
+
peekRaw().text === bullet.text &&
|
|
975
|
+
peekRaw().column === bullet.column
|
|
976
|
+
) {
|
|
977
|
+
cursor++;
|
|
978
|
+
operands.push(parseExpression());
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
// bulleted_list: a first item that fails leaves expr_or_op's other readings to fail after it.
|
|
982
|
+
if (isTlapm) commit(parseItems);
|
|
983
|
+
else parseItems();
|
|
984
|
+
offsideColumns.pop();
|
|
885
985
|
return {
|
|
886
986
|
kind: bullet.text === "/\\" ? "And" : "Or",
|
|
887
987
|
operands,
|
|
@@ -893,13 +993,12 @@ const parse = (
|
|
|
893
993
|
const parseBracket = (start: Token): Expression =>
|
|
894
994
|
withBracketLayout(() => {
|
|
895
995
|
const location = getLocation(start);
|
|
896
|
-
// SANY takes a keyword for the first field of a record, as it does for a selected field.
|
|
897
996
|
const isIdentifier = peek().kind === "Identifier";
|
|
898
997
|
const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
|
|
899
998
|
if (isRecord || (isIdentifier && isAt(":", 1))) {
|
|
900
999
|
const kind = isRecord ? "Record" : "RecordSet";
|
|
901
1000
|
const parseField = (readName: () => string) => {
|
|
902
|
-
const fieldStart = cursor
|
|
1001
|
+
const fieldStart = cursor;
|
|
903
1002
|
const fieldLocation = getLocation(peek());
|
|
904
1003
|
const name = readName();
|
|
905
1004
|
fieldLocation.range = getRange(fieldStart);
|
|
@@ -914,7 +1013,7 @@ const parse = (
|
|
|
914
1013
|
return { kind, fields, location };
|
|
915
1014
|
}
|
|
916
1015
|
if (isBoundAhead()) {
|
|
917
|
-
const checkpoint = cursor
|
|
1016
|
+
const checkpoint = cursor;
|
|
918
1017
|
const bounds = parseBounds();
|
|
919
1018
|
if (!isTlapm || isAt("|->")) {
|
|
920
1019
|
expect("|->");
|
|
@@ -922,7 +1021,7 @@ const parse = (
|
|
|
922
1021
|
expect("]");
|
|
923
1022
|
return { kind: "FunctionConstruction", bounds, body, location };
|
|
924
1023
|
}
|
|
925
|
-
cursor
|
|
1024
|
+
cursor = checkpoint;
|
|
926
1025
|
}
|
|
927
1026
|
const expression = parseExpression();
|
|
928
1027
|
if (accept("->")) {
|
|
@@ -967,13 +1066,12 @@ const parse = (
|
|
|
967
1066
|
};
|
|
968
1067
|
});
|
|
969
1068
|
|
|
970
|
-
// SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
|
|
971
1069
|
const parseSetMembership = (): Expression => {
|
|
972
|
-
const start = cursor
|
|
1070
|
+
const start = cursor;
|
|
973
1071
|
const element = withRange(parsePrefix(), start);
|
|
974
1072
|
const location = getLocation(advance());
|
|
975
1073
|
return withRange(
|
|
976
|
-
{ kind: "Apply", operator: "\\in", args: [element, parseExpression
|
|
1074
|
+
{ kind: "Apply", operator: "\\in", args: [element, commit(parseExpression)], location },
|
|
977
1075
|
start,
|
|
978
1076
|
);
|
|
979
1077
|
};
|
|
@@ -983,16 +1081,16 @@ const parse = (
|
|
|
983
1081
|
const location = getLocation(start);
|
|
984
1082
|
if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
|
|
985
1083
|
if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
|
|
986
|
-
const checkpoint = cursor
|
|
1084
|
+
const checkpoint = cursor;
|
|
987
1085
|
const pattern = parsePattern();
|
|
988
1086
|
expect("\\in");
|
|
989
|
-
const set = parseExpression
|
|
1087
|
+
const set = commit(parseExpression);
|
|
990
1088
|
if (accept(":")) {
|
|
991
|
-
const predicate = parseExpression
|
|
1089
|
+
const predicate = commit(parseExpression);
|
|
992
1090
|
expect("}");
|
|
993
1091
|
return { kind: "SetFilter", bound: { pattern, set }, predicate, location };
|
|
994
1092
|
}
|
|
995
|
-
cursor
|
|
1093
|
+
cursor = checkpoint;
|
|
996
1094
|
}
|
|
997
1095
|
const first =
|
|
998
1096
|
peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
|
|
@@ -1018,7 +1116,7 @@ const parse = (
|
|
|
1018
1116
|
return {
|
|
1019
1117
|
kind: "AngleAction",
|
|
1020
1118
|
action: elements[0],
|
|
1021
|
-
subscript: parseSubscript
|
|
1119
|
+
subscript: commit(parseSubscript).subscript,
|
|
1022
1120
|
location,
|
|
1023
1121
|
};
|
|
1024
1122
|
}
|
|
@@ -1051,7 +1149,7 @@ const parse = (
|
|
|
1051
1149
|
const parseCase = (start: Token): Expression => {
|
|
1052
1150
|
const arms: CaseArm[] = [];
|
|
1053
1151
|
do {
|
|
1054
|
-
const otherStart = cursor
|
|
1152
|
+
const otherStart = cursor;
|
|
1055
1153
|
if (accept("OTHER")) {
|
|
1056
1154
|
expect("->");
|
|
1057
1155
|
const other = parseExpression();
|
|
@@ -1096,7 +1194,7 @@ const parse = (
|
|
|
1096
1194
|
case "@":
|
|
1097
1195
|
return { kind: "At" };
|
|
1098
1196
|
case "(": {
|
|
1099
|
-
cursor
|
|
1197
|
+
cursor--;
|
|
1100
1198
|
return { kind: "Bound", args: parseArguments() };
|
|
1101
1199
|
}
|
|
1102
1200
|
}
|
|
@@ -1104,7 +1202,7 @@ const parse = (
|
|
|
1104
1202
|
};
|
|
1105
1203
|
|
|
1106
1204
|
const parsePrimary = (preceding?: Precedence): Expression => {
|
|
1107
|
-
const start = cursor
|
|
1205
|
+
const start = cursor;
|
|
1108
1206
|
const token = advance();
|
|
1109
1207
|
const location = getLocation(token);
|
|
1110
1208
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -1112,10 +1210,8 @@ const parse = (
|
|
|
1112
1210
|
expect(":");
|
|
1113
1211
|
return { kind: "Lambda", params, body: parseExpression(), location };
|
|
1114
1212
|
}
|
|
1115
|
-
if (token.kind === "Number")
|
|
1116
|
-
if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
|
|
1213
|
+
if (token.kind === "Number")
|
|
1117
1214
|
return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
|
|
1118
|
-
}
|
|
1119
1215
|
if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
|
|
1120
1216
|
if (token.kind === "String") return { kind: "String", value: token.text, location };
|
|
1121
1217
|
const isStep = token.kind === "Step" && isStepReference(token);
|
|
@@ -1136,9 +1232,9 @@ const parse = (
|
|
|
1136
1232
|
if (
|
|
1137
1233
|
!isTlapm &&
|
|
1138
1234
|
preceding !== undefined &&
|
|
1139
|
-
|
|
1235
|
+
lastOperator !== undefined &&
|
|
1140
1236
|
isOperatorExpression(body) &&
|
|
1141
|
-
preceding.high >=
|
|
1237
|
+
preceding.high >= lastOperator.low
|
|
1142
1238
|
) {
|
|
1143
1239
|
throw createError("Removing the label would change expression parsing", token);
|
|
1144
1240
|
}
|
|
@@ -1156,20 +1252,20 @@ const parse = (
|
|
|
1156
1252
|
if (name === "@" && !hasArguments && !isSelectorAhead())
|
|
1157
1253
|
return { kind: "ExceptAt", location };
|
|
1158
1254
|
const args = hasArguments ? parseArguments() : [];
|
|
1159
|
-
const argumentRanges = [hasArguments ?
|
|
1255
|
+
const argumentRanges = [hasArguments ? lastArgumentsRange : undefined];
|
|
1160
1256
|
if (!isSelectorAhead()) {
|
|
1161
1257
|
return hasArguments
|
|
1162
|
-
? { kind: "Apply", operator: name, args, argumentsRange:
|
|
1258
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArgumentsRange, location }
|
|
1163
1259
|
: { kind: "Identifier", name, location };
|
|
1164
1260
|
}
|
|
1165
1261
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
1166
1262
|
const selectorRanges = [getRange(start, start)];
|
|
1167
1263
|
while (isSelectorAhead()) {
|
|
1168
1264
|
advance();
|
|
1169
|
-
selectorRanges.push(getRange(cursor
|
|
1170
|
-
|
|
1265
|
+
selectorRanges.push(getRange(cursor, cursor));
|
|
1266
|
+
lastArgumentsRange = undefined;
|
|
1171
1267
|
selectors.push(parseSelector());
|
|
1172
|
-
argumentRanges.push(
|
|
1268
|
+
argumentRanges.push(lastArgumentsRange);
|
|
1173
1269
|
}
|
|
1174
1270
|
const names = selectors.flatMap((selector) =>
|
|
1175
1271
|
selector.kind === "Name" ? [selector.name] : [],
|
|
@@ -1197,10 +1293,9 @@ const parse = (
|
|
|
1197
1293
|
location,
|
|
1198
1294
|
};
|
|
1199
1295
|
}
|
|
1200
|
-
// SANY reads an infix or postfix operator as a primary expression, with or without arguments.
|
|
1201
1296
|
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
|
|
1202
1297
|
const args = parseArguments();
|
|
1203
|
-
// SANY's built-in \X has no definition for TLC to evaluate, a TLC bug.
|
|
1298
|
+
// Deviation from TLC: SANY's built-in \X has no definition for TLC to evaluate, a TLC bug.
|
|
1204
1299
|
return mode === "tlc" && token.text === "\\X" && args.length === 2
|
|
1205
1300
|
? { kind: "Product", factors: args, location }
|
|
1206
1301
|
: { kind: "Apply", operator: token.text, spelling: token.spelling, args, location };
|
|
@@ -1241,8 +1336,7 @@ const parse = (
|
|
|
1241
1336
|
const instances: Instance[] = [];
|
|
1242
1337
|
recursiveScopes.push(new Set());
|
|
1243
1338
|
while (!accept("IN")) {
|
|
1244
|
-
if (isTlapm && isAt("RECURSIVE"))
|
|
1245
|
-
throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
|
|
1339
|
+
if (isTlapm && isAt("RECURSIVE")) throw createUnexpected("Unexpected keyword RECURSIVE");
|
|
1246
1340
|
if (accept("RECURSIVE")) {
|
|
1247
1341
|
declareRecursive();
|
|
1248
1342
|
continue;
|
|
@@ -1286,52 +1380,27 @@ const parse = (
|
|
|
1286
1380
|
throw createUnexpected(`Unexpected ${describeToken(token)}`, token);
|
|
1287
1381
|
};
|
|
1288
1382
|
|
|
1289
|
-
// SANY rejects the operator after a prefix operator's operand unless it binds looser than the
|
|
1290
|
-
// prefix operator.
|
|
1291
1383
|
const parsePrefixOperand = (operator: Token, low: number, high: number) => {
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
const operand = parseExpression(
|
|
1295
|
-
isTlapm ? Math.min(high + 1, POSTFIX_PRECEDENCE.low) : high + 1,
|
|
1296
|
-
);
|
|
1384
|
+
lastOperator = precedingOperator = { low, high };
|
|
1385
|
+
const operand = parseExpression(high + 1);
|
|
1297
1386
|
const next = peek();
|
|
1298
1387
|
const nextHigh = DEFINABLE_POSTFIX_OPERATORS.has(next.text)
|
|
1299
1388
|
? POSTFIX_PRECEDENCE.high
|
|
1300
1389
|
: INFIX_OPERATORS.get(next.text)?.high;
|
|
1301
|
-
if (
|
|
1390
|
+
if (next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
|
|
1302
1391
|
throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
|
|
1303
1392
|
}
|
|
1304
1393
|
return operand;
|
|
1305
1394
|
};
|
|
1306
1395
|
|
|
1307
1396
|
const parsePrefix = (): Expression => {
|
|
1308
|
-
const preceding =
|
|
1309
|
-
|
|
1397
|
+
const preceding = precedingOperator;
|
|
1398
|
+
precedingOperator = undefined;
|
|
1310
1399
|
const token = peek();
|
|
1311
1400
|
const location = getLocation(token);
|
|
1312
1401
|
if (token.kind === "Operator" && isBullet(token)) {
|
|
1313
1402
|
return parseJunctionList();
|
|
1314
1403
|
}
|
|
1315
|
-
if (isTlapm && INFIX_OPERATORS.has(token.text) && isAt("(", 1)) {
|
|
1316
|
-
const start = cursor.index;
|
|
1317
|
-
const nonfix = withBracketLayout(() => {
|
|
1318
|
-
advance();
|
|
1319
|
-
advance();
|
|
1320
|
-
const left = parseExpression();
|
|
1321
|
-
if (!accept(",")) return undefined;
|
|
1322
|
-
const right = parseExpression();
|
|
1323
|
-
expect(")");
|
|
1324
|
-
const apply: Expression = {
|
|
1325
|
-
kind: "Apply",
|
|
1326
|
-
operator: token.text,
|
|
1327
|
-
args: [left, right],
|
|
1328
|
-
location,
|
|
1329
|
-
};
|
|
1330
|
-
return withRange(apply, start);
|
|
1331
|
-
});
|
|
1332
|
-
if (nonfix !== undefined) return nonfix;
|
|
1333
|
-
cursor.index = start;
|
|
1334
|
-
}
|
|
1335
1404
|
if (token.kind === "Operator" || token.kind === "Keyword") {
|
|
1336
1405
|
switch (token.text) {
|
|
1337
1406
|
case "~":
|
|
@@ -1343,8 +1412,6 @@ const parse = (
|
|
|
1343
1412
|
location,
|
|
1344
1413
|
};
|
|
1345
1414
|
case "-":
|
|
1346
|
-
case "-.":
|
|
1347
|
-
if (token.text === "-." && !isTlapm) break;
|
|
1348
1415
|
advance();
|
|
1349
1416
|
return {
|
|
1350
1417
|
kind: "Apply",
|
|
@@ -1353,17 +1420,11 @@ const parse = (
|
|
|
1353
1420
|
location,
|
|
1354
1421
|
};
|
|
1355
1422
|
case "[]":
|
|
1356
|
-
advance();
|
|
1357
|
-
return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1358
1423
|
case "<>":
|
|
1359
|
-
advance();
|
|
1360
|
-
return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1361
1424
|
case "ENABLED":
|
|
1362
|
-
advance();
|
|
1363
|
-
return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
|
|
1364
1425
|
case "UNCHANGED":
|
|
1365
1426
|
advance();
|
|
1366
|
-
return
|
|
1427
|
+
return getOperatorApplication(token.text, parsePrefixOperand(token, 4, 15), location);
|
|
1367
1428
|
case "SUBSET":
|
|
1368
1429
|
case "UNION":
|
|
1369
1430
|
case "DOMAIN":
|
|
@@ -1371,8 +1432,7 @@ const parse = (
|
|
|
1371
1432
|
return {
|
|
1372
1433
|
kind: "Apply",
|
|
1373
1434
|
operator: token.text,
|
|
1374
|
-
|
|
1375
|
-
args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
|
|
1435
|
+
args: [parsePrefixOperand(token, 10, 13)],
|
|
1376
1436
|
location,
|
|
1377
1437
|
};
|
|
1378
1438
|
}
|
|
@@ -1380,58 +1440,85 @@ const parse = (
|
|
|
1380
1440
|
return parsePrimary(preceding);
|
|
1381
1441
|
};
|
|
1382
1442
|
|
|
1383
|
-
const
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1443
|
+
const getInfixApplication = (
|
|
1444
|
+
token: Token,
|
|
1445
|
+
left: Expression,
|
|
1446
|
+
right: Expression,
|
|
1447
|
+
isChained: boolean,
|
|
1448
|
+
prefixEnd: Point,
|
|
1449
|
+
): Expression => {
|
|
1450
|
+
const location = getLocation(token);
|
|
1451
|
+
if (token.text === "/\\" || token.text === "\\/") {
|
|
1452
|
+
const kind = token.text === "/\\" ? "And" : "Or";
|
|
1453
|
+
return left.kind === kind && isChained
|
|
1454
|
+
? {
|
|
1455
|
+
...left,
|
|
1456
|
+
operands: [...left.operands, right],
|
|
1457
|
+
prefixEnds: [...(left.prefixEnds ?? []), prefixEnd],
|
|
1458
|
+
}
|
|
1459
|
+
: { kind, operands: [left, right], prefixEnds: [prefixEnd], location };
|
|
1460
|
+
}
|
|
1461
|
+
if (token.text === "\\X") {
|
|
1462
|
+
return left.kind === "Product" && isChained
|
|
1463
|
+
? { ...left, factors: [...left.factors, right] }
|
|
1464
|
+
: { kind: "Product", factors: [left, right], location };
|
|
1465
|
+
}
|
|
1466
|
+
return {
|
|
1467
|
+
kind: "Apply",
|
|
1468
|
+
operator: token.text,
|
|
1469
|
+
spelling: token.spelling,
|
|
1470
|
+
args: [left, right],
|
|
1471
|
+
location,
|
|
1389
1472
|
};
|
|
1473
|
+
};
|
|
1474
|
+
|
|
1475
|
+
const getPostfixApplication = (token: Token, operand: Expression): Expression =>
|
|
1476
|
+
token.text === "'"
|
|
1477
|
+
? { kind: "Prime", expression: operand, location: getLocation(token) }
|
|
1478
|
+
: {
|
|
1479
|
+
kind: "Apply",
|
|
1480
|
+
operator: token.text,
|
|
1481
|
+
spelling: token.spelling,
|
|
1482
|
+
args: [operand],
|
|
1483
|
+
location: getLocation(token),
|
|
1484
|
+
};
|
|
1485
|
+
|
|
1486
|
+
const parseFunctionArguments = () => {
|
|
1487
|
+
advance();
|
|
1488
|
+
const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
|
|
1489
|
+
withBracketLayout(() => expect("]"));
|
|
1490
|
+
return args;
|
|
1491
|
+
};
|
|
1492
|
+
|
|
1493
|
+
const parseSanyExpression = (minimumPrecedence: number): Expression => {
|
|
1494
|
+
const start = cursor;
|
|
1495
|
+
const prefix = parsePrefix();
|
|
1496
|
+
let left = prefix.location.range ? prefix : withRange(prefix, start);
|
|
1497
|
+
let previous: (InfixOperator & { text: string }) | undefined;
|
|
1390
1498
|
while (true) {
|
|
1391
1499
|
const token = peek();
|
|
1392
1500
|
if (token.kind !== "Operator") break;
|
|
1393
1501
|
const location = getLocation(token);
|
|
1394
|
-
if (token.text
|
|
1502
|
+
if (DEFINABLE_POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1395
1503
|
advance();
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
continue;
|
|
1399
|
-
}
|
|
1400
|
-
if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
|
|
1401
|
-
advance();
|
|
1402
|
-
operators.last = POSTFIX_PRECEDENCE;
|
|
1403
|
-
state.left = withRange(
|
|
1404
|
-
{
|
|
1405
|
-
kind: "Apply",
|
|
1406
|
-
operator: token.text,
|
|
1407
|
-
spelling: token.spelling,
|
|
1408
|
-
args: [state.left],
|
|
1409
|
-
location,
|
|
1410
|
-
},
|
|
1411
|
-
start,
|
|
1412
|
-
);
|
|
1504
|
+
lastOperator = POSTFIX_PRECEDENCE;
|
|
1505
|
+
left = withRange(getPostfixApplication(token, left), start);
|
|
1413
1506
|
continue;
|
|
1414
1507
|
}
|
|
1415
1508
|
if (token.text === "[") {
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
operators.last = FUNCTION_PRECEDENCE;
|
|
1420
|
-
state.left = withRange(
|
|
1421
|
-
{ kind: "FunctionApplication", function: state.left, args, location },
|
|
1422
|
-
start,
|
|
1423
|
-
);
|
|
1509
|
+
const args = parseFunctionArguments();
|
|
1510
|
+
lastOperator = FUNCTION_PRECEDENCE;
|
|
1511
|
+
left = withRange({ kind: "FunctionApplication", function: left, args, location }, start);
|
|
1424
1512
|
continue;
|
|
1425
1513
|
}
|
|
1426
1514
|
if (token.text === "." && isFieldName(peek(1))) {
|
|
1427
1515
|
advance();
|
|
1428
1516
|
const field = advance().text;
|
|
1429
|
-
|
|
1517
|
+
left = withRange({ kind: "FieldAccess", record: left, field, location }, start);
|
|
1430
1518
|
continue;
|
|
1431
1519
|
}
|
|
1432
1520
|
const infix = INFIX_OPERATORS.get(token.text);
|
|
1433
1521
|
if (!infix || infix.low < minimumPrecedence) break;
|
|
1434
|
-
const previous = state.previous;
|
|
1435
1522
|
if (previous) {
|
|
1436
1523
|
const isOverlapping = !(previous.low > infix.high || previous.high < infix.low);
|
|
1437
1524
|
const isChain = previous.text === token.text && infix.isAssociative;
|
|
@@ -1443,44 +1530,260 @@ const parse = (
|
|
|
1443
1530
|
}
|
|
1444
1531
|
}
|
|
1445
1532
|
advance();
|
|
1446
|
-
|
|
1447
|
-
if (!isTlapm && infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1533
|
+
if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
|
|
1448
1534
|
throw createError(`Precedence conflict between ${token.text} and -`, peek());
|
|
1449
1535
|
}
|
|
1450
|
-
|
|
1536
|
+
lastOperator = precedingOperator = infix;
|
|
1451
1537
|
const right = parseExpression(infix.high + 1);
|
|
1452
|
-
const
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1538
|
+
const isChained = previous?.text === token.text;
|
|
1539
|
+
left = withRange(
|
|
1540
|
+
getInfixApplication(token, left, right, isChained, getRange(start).end),
|
|
1541
|
+
start,
|
|
1542
|
+
);
|
|
1543
|
+
previous = { ...infix, text: token.text };
|
|
1544
|
+
}
|
|
1545
|
+
return left;
|
|
1546
|
+
};
|
|
1547
|
+
|
|
1548
|
+
const isTlapmAtomStart = (token: Token) =>
|
|
1549
|
+
token.kind === "Number" ||
|
|
1550
|
+
token.kind === "Decimal" ||
|
|
1551
|
+
token.kind === "String" ||
|
|
1552
|
+
token.kind === "Identifier" ||
|
|
1553
|
+
(token.kind === "Step" && isStepReference(token) && /^<\d/.test(token.text)) ||
|
|
1554
|
+
((token.kind === "Operator" || token.kind === "Keyword") && TLAPM_ATOM_STARTS.has(token.text));
|
|
1555
|
+
|
|
1556
|
+
const getTlapmOperand = (expression: Expression, start: number): TlapmOperand => ({
|
|
1557
|
+
start,
|
|
1558
|
+
end: cursor - 1,
|
|
1559
|
+
expression: expression.location.range ? expression : withRange(expression, start),
|
|
1560
|
+
});
|
|
1561
|
+
|
|
1562
|
+
const getTlapmOperator = (
|
|
1563
|
+
start: number,
|
|
1564
|
+
fixity: TlapmOperator["fixity"],
|
|
1565
|
+
precedence: Precedence,
|
|
1566
|
+
apply: TlapmOperator["apply"],
|
|
1567
|
+
): TlapmOperator => ({
|
|
1568
|
+
...precedence,
|
|
1569
|
+
start,
|
|
1570
|
+
end: cursor - 1,
|
|
1571
|
+
fixity,
|
|
1572
|
+
isLeft: false,
|
|
1573
|
+
apply,
|
|
1574
|
+
});
|
|
1575
|
+
|
|
1576
|
+
// expr_or_op's non_test: a postfix operator before an expression in parentheses is an atom.
|
|
1577
|
+
const parseTlapmNonfixPostfix = (token: Token) =>
|
|
1578
|
+
token.kind === "Operator" && DEFINABLE_POSTFIX_OPERATORS.has(token.text) && isAt("(", 1)
|
|
1579
|
+
? attempt(() =>
|
|
1580
|
+
withBracketLayout(() => {
|
|
1581
|
+
advance();
|
|
1582
|
+
advance();
|
|
1583
|
+
const operand = parseExpression();
|
|
1584
|
+
const end = cursor - 1;
|
|
1585
|
+
expect(")");
|
|
1586
|
+
return { end, expression: getPostfixApplication(token, operand) };
|
|
1587
|
+
}),
|
|
1588
|
+
)
|
|
1589
|
+
: undefined;
|
|
1590
|
+
|
|
1591
|
+
// A comma directly inside the parentheses after the operator, without which non_test's
|
|
1592
|
+
// attempt at `(e, f)` fails. A selector's << or >> isn't a bracket.
|
|
1593
|
+
const hasNonfixComma = () => {
|
|
1594
|
+
let depth = 0;
|
|
1595
|
+
for (let index = cursor + 1; index < tokens.length; index++) {
|
|
1596
|
+
const token = tokens[index];
|
|
1597
|
+
if (token.kind !== "Operator") continue;
|
|
1598
|
+
if (depth === 1 && token.text === ",") return true;
|
|
1599
|
+
if (tokens[index - 1].text === "!") continue;
|
|
1600
|
+
if (OPENING_BRACKETS.has(token.text)) depth++;
|
|
1601
|
+
if (CLOSING_BRACKETS.has(token.text) && --depth === 0) return false;
|
|
1602
|
+
}
|
|
1603
|
+
return false;
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
// expr_or_op's non_test: an infix operator before two expressions in parentheses is an atom.
|
|
1607
|
+
const parseTlapmNonfixInfix = (token: Token) =>
|
|
1608
|
+
isAt("(", 1) && hasNonfixComma()
|
|
1609
|
+
? attempt(() =>
|
|
1610
|
+
withBracketLayout((): Expression => {
|
|
1611
|
+
advance();
|
|
1612
|
+
advance();
|
|
1613
|
+
const left = parseExpression();
|
|
1614
|
+
expect(",");
|
|
1615
|
+
const right = parseExpression();
|
|
1616
|
+
expect(")");
|
|
1617
|
+
return {
|
|
1618
|
+
kind: "Apply",
|
|
1619
|
+
operator: token.text,
|
|
1620
|
+
args: [left, right],
|
|
1621
|
+
location: getLocation(token),
|
|
1622
|
+
};
|
|
1623
|
+
}),
|
|
1624
|
+
)
|
|
1625
|
+
: undefined;
|
|
1626
|
+
|
|
1627
|
+
const parseTlapmItem = (isStart: boolean): TlapmItem | undefined => {
|
|
1628
|
+
const start = cursor;
|
|
1629
|
+
const token = peek();
|
|
1630
|
+
const location = getLocation(token);
|
|
1631
|
+
const isOperator = token.kind === "Operator" || token.kind === "Keyword";
|
|
1632
|
+
const infix = isOperator ? INFIX_OPERATORS.get(token.text) : undefined;
|
|
1633
|
+
if (!isStart) {
|
|
1634
|
+
if (isAt("[")) {
|
|
1635
|
+
const args = parseFunctionArguments();
|
|
1636
|
+
return getTlapmOperator(start, "Postfix", TLAPM_SELECTION_PRECEDENCE, (operands) => ({
|
|
1637
|
+
kind: "FunctionApplication",
|
|
1638
|
+
function: operands[0].expression,
|
|
1639
|
+
args,
|
|
1475
1640
|
location,
|
|
1641
|
+
}));
|
|
1642
|
+
}
|
|
1643
|
+
if (isAt(".") && isFieldName(peek(1))) {
|
|
1644
|
+
advance();
|
|
1645
|
+
const field = advance().text;
|
|
1646
|
+
return getTlapmOperator(start, "Postfix", TLAPM_SELECTION_PRECEDENCE, (operands) => ({
|
|
1647
|
+
kind: "FieldAccess",
|
|
1648
|
+
record: operands[0].expression,
|
|
1649
|
+
field,
|
|
1650
|
+
location,
|
|
1651
|
+
}));
|
|
1652
|
+
}
|
|
1653
|
+
if (infix) {
|
|
1654
|
+
// Pco.resolve has no operator to put before that atom, so the expression ends here.
|
|
1655
|
+
if (parseTlapmNonfixInfix(token)) {
|
|
1656
|
+
cursor = start;
|
|
1657
|
+
return undefined;
|
|
1658
|
+
}
|
|
1659
|
+
advance();
|
|
1660
|
+
return {
|
|
1661
|
+
...getTlapmOperator(start, "Infix", infix, (operands) =>
|
|
1662
|
+
getInfixApplication(
|
|
1663
|
+
token,
|
|
1664
|
+
operands[0].expression,
|
|
1665
|
+
operands[1].expression,
|
|
1666
|
+
operands[0].joinedBy === token.text,
|
|
1667
|
+
getRange(operands[0].start, operands[1].end).end,
|
|
1668
|
+
),
|
|
1669
|
+
),
|
|
1670
|
+
isLeft: isTlapmLeft(token.text, infix),
|
|
1671
|
+
joins: token.text,
|
|
1476
1672
|
};
|
|
1477
1673
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1674
|
+
if (token.kind !== "Operator" || !DEFINABLE_POSTFIX_OPERATORS.has(token.text))
|
|
1675
|
+
return undefined;
|
|
1676
|
+
// Pco.resolve has no operator to put before that atom, so the expression ends here.
|
|
1677
|
+
if (parseTlapmNonfixPostfix(token)) {
|
|
1678
|
+
cursor = start;
|
|
1679
|
+
return undefined;
|
|
1680
|
+
}
|
|
1681
|
+
advance();
|
|
1682
|
+
return getTlapmOperator(start, "Postfix", POSTFIX_PRECEDENCE, (operands) =>
|
|
1683
|
+
getPostfixApplication(token, operands[0].expression),
|
|
1684
|
+
);
|
|
1685
|
+
}
|
|
1686
|
+
if (token.kind === "Operator" && isBullet(token))
|
|
1687
|
+
return getTlapmOperand(parseJunctionList(), start);
|
|
1688
|
+
const nonfix = infix ? parseTlapmNonfixInfix(token) : undefined;
|
|
1689
|
+
if (nonfix) return getTlapmOperand(nonfix, start);
|
|
1690
|
+
const nonfixPostfix = parseTlapmNonfixPostfix(token);
|
|
1691
|
+
if (nonfixPostfix) {
|
|
1692
|
+
nonfixPostfix.expression.location.range = getRange(start, nonfixPostfix.end);
|
|
1693
|
+
return { start, end: nonfixPostfix.end, expression: nonfixPostfix.expression };
|
|
1694
|
+
}
|
|
1695
|
+
const prefix = isOperator ? TLAPM_PREFIX_OPERATORS.get(token.text) : undefined;
|
|
1696
|
+
if (prefix) {
|
|
1697
|
+
advance();
|
|
1698
|
+
return getTlapmOperator(start, "Prefix", prefix, (operands) =>
|
|
1699
|
+
getOperatorApplication(
|
|
1700
|
+
token.text === "-" ? "-." : token.text,
|
|
1701
|
+
operands[0].expression,
|
|
1702
|
+
location,
|
|
1703
|
+
),
|
|
1704
|
+
);
|
|
1480
1705
|
}
|
|
1481
|
-
return
|
|
1706
|
+
if (!isTlapmAtomStart(token)) return undefined;
|
|
1707
|
+
const isTransparent = token.kind === "Operator" && TLAPM_OPEN_BRACKETS.has(token.text);
|
|
1708
|
+
return getTlapmOperand(isTransparent ? parsePrimary() : commit(parsePrimary), start);
|
|
1482
1709
|
};
|
|
1483
1710
|
|
|
1711
|
+
const isTlapmOperator = (item: TlapmItem | undefined): item is TlapmOperator =>
|
|
1712
|
+
item !== undefined && "fixity" in item;
|
|
1713
|
+
|
|
1714
|
+
const isTlapmOperand = (item: TlapmItem | undefined): item is TlapmOperand =>
|
|
1715
|
+
item !== undefined && !("fixity" in item);
|
|
1716
|
+
|
|
1717
|
+
const applyTlapmOperator = (operator: TlapmOperator, operands: TlapmOperand[]) => {
|
|
1718
|
+
const start = Math.min(operator.start, operands[0].start);
|
|
1719
|
+
const end = Math.max(operator.end, operands[operands.length - 1].end);
|
|
1720
|
+
const expression = operator.apply(operands);
|
|
1721
|
+
expression.location.range = getRange(start, end);
|
|
1722
|
+
return { start, end, expression, joinedBy: operator.joins };
|
|
1723
|
+
};
|
|
1724
|
+
|
|
1725
|
+
const reduceTlapmItems = (stack: TlapmItem[]) => {
|
|
1726
|
+
const right = stack.pop();
|
|
1727
|
+
const operator = stack.pop();
|
|
1728
|
+
if (!isTlapmOperand(right) || !isTlapmOperator(operator)) {
|
|
1729
|
+
throw createError("incomplete expression");
|
|
1730
|
+
}
|
|
1731
|
+
const left = operator.fixity === "Infix" ? stack.pop() : undefined;
|
|
1732
|
+
stack.push(applyTlapmOperator(operator, isTlapmOperand(left) ? [left, right] : [right]));
|
|
1733
|
+
};
|
|
1734
|
+
|
|
1735
|
+
const getPreviousOperator = (stack: TlapmItem[]) => {
|
|
1736
|
+
const previous = stack[stack.length - 2];
|
|
1737
|
+
return isTlapmOperator(previous) ? previous : undefined;
|
|
1738
|
+
};
|
|
1739
|
+
|
|
1740
|
+
const isBelow = (left: Precedence, right: Precedence | undefined) =>
|
|
1741
|
+
right !== undefined && left.high < right.low;
|
|
1742
|
+
|
|
1743
|
+
// Port of Pco.resolve. A failure after the first item backtracks no further than its attempt.
|
|
1744
|
+
const parseTlapmExpression = (): Expression =>
|
|
1745
|
+
withRestorePoint(() => {
|
|
1746
|
+
const first = attempt(() => parseTlapmItem(true));
|
|
1747
|
+
if (first === undefined) {
|
|
1748
|
+
const token = peekRaw();
|
|
1749
|
+
throw token.kind === "End"
|
|
1750
|
+
? createUnexpected("Unexpected end of expression", token)
|
|
1751
|
+
: createError(`required expressions(s) missing before '${getTlapmName(token)}'`, token);
|
|
1752
|
+
}
|
|
1753
|
+
return commit(() => {
|
|
1754
|
+
const stack = [first];
|
|
1755
|
+
while (true) {
|
|
1756
|
+
const item = attempt(() => parseTlapmItem(isTlapmOperator(stack[stack.length - 1])));
|
|
1757
|
+
if (item === undefined) break;
|
|
1758
|
+
if (isTlapmOperand(item) || item.fixity === "Prefix") {
|
|
1759
|
+
stack.push(item);
|
|
1760
|
+
continue;
|
|
1761
|
+
}
|
|
1762
|
+
while (isBelow(item, getPreviousOperator(stack))) reduceTlapmItems(stack);
|
|
1763
|
+
const operand = stack[stack.length - 1];
|
|
1764
|
+
if (item.fixity === "Postfix" && isTlapmOperand(operand)) {
|
|
1765
|
+
stack.splice(-1, 1, applyTlapmOperator(item, [operand]));
|
|
1766
|
+
continue;
|
|
1767
|
+
}
|
|
1768
|
+
const previous = getPreviousOperator(stack);
|
|
1769
|
+
const isLeftChain =
|
|
1770
|
+
previous?.fixity === "Infix" &&
|
|
1771
|
+
previous.isLeft &&
|
|
1772
|
+
item.isLeft &&
|
|
1773
|
+
!isBelow(previous, item);
|
|
1774
|
+
if (isLeftChain) reduceTlapmItems(stack);
|
|
1775
|
+
stack.push(item);
|
|
1776
|
+
}
|
|
1777
|
+
while (stack.length > 1) reduceTlapmItems(stack);
|
|
1778
|
+
const result = stack[0];
|
|
1779
|
+
if (!isTlapmOperand(result)) throw createError("incomplete expression");
|
|
1780
|
+
return result.expression;
|
|
1781
|
+
});
|
|
1782
|
+
});
|
|
1783
|
+
|
|
1784
|
+
const parseExpression = (minimumPrecedence = 0) =>
|
|
1785
|
+
isTlapm ? parseTlapmExpression() : parseSanyExpression(minimumPrecedence);
|
|
1786
|
+
|
|
1484
1787
|
const parseParams = () => {
|
|
1485
1788
|
if (!accept("(")) return [];
|
|
1486
1789
|
const params = parseDeclarations();
|
|
@@ -1491,17 +1794,17 @@ const parse = (
|
|
|
1491
1794
|
const parseBody = () => {
|
|
1492
1795
|
const start = getLocation(peek());
|
|
1493
1796
|
const body = parseExpression();
|
|
1494
|
-
const last = tokens[cursor
|
|
1797
|
+
const last = tokens[cursor - 1];
|
|
1495
1798
|
const end = { line: last.line, column: last.endColumn };
|
|
1496
1799
|
return { body, bodyRange: { module: openModules[0].name, start, end } };
|
|
1497
1800
|
};
|
|
1498
1801
|
|
|
1499
1802
|
const parseDefinition = (isLocal: boolean): Definition | Instance => {
|
|
1500
|
-
const start = cursor
|
|
1803
|
+
const start = cursor;
|
|
1501
1804
|
return withRange(parseDefinitionUnit(isLocal), start);
|
|
1502
1805
|
};
|
|
1503
1806
|
|
|
1504
|
-
const toParam = (named:
|
|
1807
|
+
const toParam = (named: RangedName): OperatorParam => ({
|
|
1505
1808
|
name: named.name,
|
|
1506
1809
|
arity: 0,
|
|
1507
1810
|
location: {
|
|
@@ -1513,13 +1816,13 @@ const parse = (
|
|
|
1513
1816
|
|
|
1514
1817
|
const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
|
|
1515
1818
|
const token = peek();
|
|
1516
|
-
const start = cursor
|
|
1819
|
+
const start = cursor;
|
|
1517
1820
|
const location = getLocation(token);
|
|
1518
1821
|
const parseOperator = (name: string, params: OperatorParam[]): Definition => ({
|
|
1519
1822
|
kind: "Operator",
|
|
1520
1823
|
name,
|
|
1521
1824
|
params,
|
|
1522
|
-
head: getRange(start, cursor
|
|
1825
|
+
head: getRange(start, cursor - 2),
|
|
1523
1826
|
...parseBody(),
|
|
1524
1827
|
isLocal,
|
|
1525
1828
|
isRecursive: isRecursiveName(name),
|
|
@@ -1565,7 +1868,7 @@ const parse = (
|
|
|
1565
1868
|
location,
|
|
1566
1869
|
};
|
|
1567
1870
|
}
|
|
1568
|
-
const definitionStart = cursor
|
|
1871
|
+
const definitionStart = cursor;
|
|
1569
1872
|
const params = parseParams();
|
|
1570
1873
|
expect("==");
|
|
1571
1874
|
if (isAt("INSTANCE")) {
|
|
@@ -1581,12 +1884,12 @@ const parse = (
|
|
|
1581
1884
|
isLocal: boolean,
|
|
1582
1885
|
location: SourceLocation,
|
|
1583
1886
|
): Instance => {
|
|
1584
|
-
const start = cursor
|
|
1887
|
+
const start = cursor;
|
|
1585
1888
|
expect("INSTANCE");
|
|
1586
1889
|
const module = expectModuleName();
|
|
1587
1890
|
const substitutions = accept("WITH")
|
|
1588
1891
|
? parseSeparated(() => {
|
|
1589
|
-
const targetStart = cursor
|
|
1892
|
+
const targetStart = cursor;
|
|
1590
1893
|
const target = expectSubstitutionTarget();
|
|
1591
1894
|
const nameRange = getRange(targetStart);
|
|
1592
1895
|
expect("<-");
|
|
@@ -1606,7 +1909,7 @@ const parse = (
|
|
|
1606
1909
|
};
|
|
1607
1910
|
|
|
1608
1911
|
const parseDeclaration = (): Declaration => {
|
|
1609
|
-
const start = cursor
|
|
1912
|
+
const start = cursor;
|
|
1610
1913
|
return withRange(parseDeclarationUnit(), start);
|
|
1611
1914
|
};
|
|
1612
1915
|
|
|
@@ -1642,19 +1945,19 @@ const parse = (
|
|
|
1642
1945
|
return declarations;
|
|
1643
1946
|
};
|
|
1644
1947
|
|
|
1645
|
-
const getClosingOffset = (
|
|
1646
|
-
|
|
1948
|
+
const getClosingOffset = (start: number) => {
|
|
1949
|
+
let depth = 0;
|
|
1950
|
+
let offset = start;
|
|
1647
1951
|
do {
|
|
1648
|
-
const token = peek(
|
|
1649
|
-
if (token.kind === "End") return
|
|
1650
|
-
if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth
|
|
1651
|
-
if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth
|
|
1652
|
-
|
|
1653
|
-
} while (depth
|
|
1654
|
-
return
|
|
1952
|
+
const token = peek(offset);
|
|
1953
|
+
if (token.kind === "End") return offset;
|
|
1954
|
+
if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth++;
|
|
1955
|
+
if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth--;
|
|
1956
|
+
offset++;
|
|
1957
|
+
} while (depth > 0);
|
|
1958
|
+
return offset;
|
|
1655
1959
|
};
|
|
1656
1960
|
|
|
1657
|
-
// SANY inserts a break before each definition, which ends the fact list or step before it.
|
|
1658
1961
|
const isDefinitionStart = () => {
|
|
1659
1962
|
const token = peek();
|
|
1660
1963
|
const next = peek(1);
|
|
@@ -1675,7 +1978,6 @@ const parse = (
|
|
|
1675
1978
|
return isAt("==", 1);
|
|
1676
1979
|
};
|
|
1677
1980
|
|
|
1678
|
-
// SANY's proof levels, one for each open proof, which stay -1 until a step sets them.
|
|
1679
1981
|
const proofLevels: number[] = [];
|
|
1680
1982
|
|
|
1681
1983
|
const getProofLevel = () => proofLevels[proofLevels.length - 1] ?? -1;
|
|
@@ -1688,9 +1990,9 @@ const parse = (
|
|
|
1688
1990
|
|
|
1689
1991
|
const getStepLabel = (step: Token) => {
|
|
1690
1992
|
const label = step.text.slice(step.text.indexOf(">") + 1);
|
|
1691
|
-
|
|
1692
|
-
while (label[end
|
|
1693
|
-
return label.slice(0, end
|
|
1993
|
+
let end = label.length;
|
|
1994
|
+
while (label[end - 1] === ".") end--;
|
|
1995
|
+
return label.slice(0, end);
|
|
1694
1996
|
};
|
|
1695
1997
|
|
|
1696
1998
|
const isStepReference = (step: Token) =>
|
|
@@ -1753,17 +2055,15 @@ const parse = (
|
|
|
1753
2055
|
return !isDefinitionStart();
|
|
1754
2056
|
};
|
|
1755
2057
|
|
|
1756
|
-
// SANY reads an infix or postfix operator alone as its name, but a prefix operator such as - as the
|
|
1757
|
-
// start of an expression.
|
|
1758
2058
|
const parseFact = (): Fact => {
|
|
1759
|
-
const start = cursor
|
|
2059
|
+
const start = cursor;
|
|
1760
2060
|
const token = peek();
|
|
1761
2061
|
if (
|
|
1762
2062
|
token.kind === "Operator" &&
|
|
1763
2063
|
!EXPRESSION_PREFIXES.has(token.text) &&
|
|
1764
2064
|
isOperatorName(token.text)
|
|
1765
2065
|
) {
|
|
1766
|
-
cursor
|
|
2066
|
+
cursor++;
|
|
1767
2067
|
return withRange(
|
|
1768
2068
|
{
|
|
1769
2069
|
kind: "OperatorReference",
|
|
@@ -1799,7 +2099,7 @@ const parse = (
|
|
|
1799
2099
|
);
|
|
1800
2100
|
|
|
1801
2101
|
const parseDefinable = (): Fact => {
|
|
1802
|
-
const start = cursor
|
|
2102
|
+
const start = cursor;
|
|
1803
2103
|
const token = peek();
|
|
1804
2104
|
const parts = parseSeparated(() => {
|
|
1805
2105
|
const part = peek();
|
|
@@ -1807,7 +2107,7 @@ const parse = (
|
|
|
1807
2107
|
if (!isName && part.kind !== "Operator") {
|
|
1808
2108
|
throw createUnexpected(`Expected a definition name but found ${describeToken(part)}`, part);
|
|
1809
2109
|
}
|
|
1810
|
-
cursor
|
|
2110
|
+
cursor++;
|
|
1811
2111
|
return part.text;
|
|
1812
2112
|
}, "!");
|
|
1813
2113
|
const location = getLocation(token);
|
|
@@ -1832,15 +2132,33 @@ const parse = (
|
|
|
1832
2132
|
return defs;
|
|
1833
2133
|
};
|
|
1834
2134
|
|
|
2135
|
+
// tlapm's usebody reads facts while an expression starts after the comma.
|
|
2136
|
+
const parseTlapmFacts = (): Fact[] => {
|
|
2137
|
+
const first = attempt(parseExpression);
|
|
2138
|
+
if (!first) return [];
|
|
2139
|
+
const facts = [first];
|
|
2140
|
+
withRestorePoint(() => {
|
|
2141
|
+
while (isAt(",")) {
|
|
2142
|
+
const fact = attempt(() => {
|
|
2143
|
+
advance();
|
|
2144
|
+
return parseExpression();
|
|
2145
|
+
});
|
|
2146
|
+
if (!fact) return;
|
|
2147
|
+
facts.push(fact);
|
|
2148
|
+
}
|
|
2149
|
+
});
|
|
2150
|
+
return facts;
|
|
2151
|
+
};
|
|
2152
|
+
|
|
1835
2153
|
const parseFacts = () => {
|
|
1836
|
-
const facts = isFactAhead() ? parseSeparated(parseFact) : [];
|
|
2154
|
+
const facts = isTlapm ? parseTlapmFacts() : isFactAhead() ? parseSeparated(parseFact) : [];
|
|
1837
2155
|
const isDef = accept("DEF") || accept("DEFS");
|
|
1838
2156
|
if (isTlapm && facts.length === 0 && !isDef) throw createUnexpected("Expected a fact");
|
|
1839
2157
|
return { facts, defs: isDef ? parseDefs() : [] };
|
|
1840
2158
|
};
|
|
1841
2159
|
|
|
1842
2160
|
const parseUseOrHide = (): UseOrHide => {
|
|
1843
|
-
const start = cursor
|
|
2161
|
+
const start = cursor;
|
|
1844
2162
|
const keyword = advance();
|
|
1845
2163
|
const isOnly = keyword.text === "USE" && accept("ONLY");
|
|
1846
2164
|
const keywordRange = getRange(start);
|
|
@@ -1866,11 +2184,10 @@ const parse = (
|
|
|
1866
2184
|
};
|
|
1867
2185
|
|
|
1868
2186
|
const parseNewSymbol = (): NewSymbol => {
|
|
1869
|
-
const newStart = cursor
|
|
2187
|
+
const newStart = cursor;
|
|
1870
2188
|
const newLocation = getLocation(peek());
|
|
1871
2189
|
const isNew = accept("NEW");
|
|
1872
|
-
|
|
1873
|
-
const start = isTlapm ? cursor.index : newStart;
|
|
2190
|
+
const start = isTlapm ? cursor : newStart;
|
|
1874
2191
|
const location = isTlapm ? getLocation(peek()) : newLocation;
|
|
1875
2192
|
const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
|
|
1876
2193
|
if (!isNew && level === undefined) {
|
|
@@ -1914,31 +2231,58 @@ const parse = (
|
|
|
1914
2231
|
);
|
|
1915
2232
|
};
|
|
1916
2233
|
|
|
1917
|
-
const
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
2234
|
+
const parseHypothesis = () => {
|
|
2235
|
+
if (peek().kind === "Identifier" && isAt("::", 1) && isAt("ASSUME", 2)) {
|
|
2236
|
+
advance();
|
|
2237
|
+
advance();
|
|
2238
|
+
}
|
|
2239
|
+
if (isAt("ASSUME")) return withProofScope(parseAssumeProve);
|
|
2240
|
+
if (peek().kind === "Keyword" && NEW_SYMBOL_STARTS.has(peek().text))
|
|
2241
|
+
return isTlapm ? commit(parseNewSymbol) : parseNewSymbol();
|
|
2242
|
+
if (!isTlapm) return parseExpression();
|
|
2243
|
+
// expr_or_sequent: where no expression starts, the sequent's ASSUME fails last.
|
|
2244
|
+
const expression = attempt(parseExpression);
|
|
2245
|
+
if (expression === undefined) throw createUnexpected("Expected a hypothesis");
|
|
2246
|
+
return expression;
|
|
2247
|
+
};
|
|
2248
|
+
|
|
2249
|
+
// comma1 (hyp b): Pco.star attempts the hypotheses after the first, each inside the one before,
|
|
2250
|
+
// so a failure that backtracks ends the list, and tlapm reports any other before the first comma.
|
|
2251
|
+
const parseTlapmHypotheses = () => {
|
|
2252
|
+
const assumptions = [parseHypothesis()];
|
|
2253
|
+
withRestorePoint(() => {
|
|
2254
|
+
while (true) {
|
|
2255
|
+
const assumption = attempt(() => {
|
|
2256
|
+
expect(",");
|
|
2257
|
+
return parseHypothesis();
|
|
2258
|
+
});
|
|
2259
|
+
if (assumption === undefined) break;
|
|
2260
|
+
assumptions.push(assumption);
|
|
1924
2261
|
}
|
|
1925
|
-
if (isAt("ASSUME")) return withProofScope(parseAssumeProve);
|
|
1926
|
-
if (peek().kind === "Keyword" && NEW_SYMBOL_STARTS.has(peek().text)) return parseNewSymbol();
|
|
1927
|
-
return parseExpression();
|
|
1928
2262
|
});
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
2263
|
+
return assumptions;
|
|
2264
|
+
};
|
|
2265
|
+
|
|
2266
|
+
const parseAssumeProve = (): AssumeProve => {
|
|
2267
|
+
const start = cursor;
|
|
2268
|
+
const location = getLocation(expect("ASSUME"));
|
|
2269
|
+
const parseSequent = (): AssumeProve => {
|
|
2270
|
+
const assumptions = isTlapm ? parseTlapmHypotheses() : parseSeparated(parseHypothesis);
|
|
2271
|
+
expect("PROVE");
|
|
2272
|
+
return withRange(
|
|
2273
|
+
{ kind: "AssumeProve", assumptions, conclusion: parseExpression(), location },
|
|
2274
|
+
start,
|
|
2275
|
+
);
|
|
2276
|
+
};
|
|
2277
|
+
return isTlapm ? commit(parseSequent) : parseSequent();
|
|
1934
2278
|
};
|
|
1935
2279
|
|
|
1936
2280
|
const isNameListAhead = (terminator: string) => {
|
|
1937
|
-
|
|
1938
|
-
while (peek(offset
|
|
1939
|
-
offset
|
|
2281
|
+
let offset = 0;
|
|
2282
|
+
while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) {
|
|
2283
|
+
offset += 2;
|
|
1940
2284
|
}
|
|
1941
|
-
return peek(offset
|
|
2285
|
+
return peek(offset).kind === "Identifier" && isAt(terminator, offset + 1);
|
|
1942
2286
|
};
|
|
1943
2287
|
|
|
1944
2288
|
const declareProofSymbols = (names: string[], bounds: Bound[]) => {
|
|
@@ -1953,12 +2297,12 @@ const parse = (
|
|
|
1953
2297
|
const parseStepBody = (): StepBody => {
|
|
1954
2298
|
const token = peek();
|
|
1955
2299
|
if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
|
|
1956
|
-
if (isAt("INSTANCE")) {
|
|
1957
|
-
const start = cursor
|
|
2300
|
+
if (!isTlapm && isAt("INSTANCE")) {
|
|
2301
|
+
const start = cursor;
|
|
1958
2302
|
return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
|
|
1959
2303
|
}
|
|
1960
2304
|
if (isAt("DEFINE") || isDefinitionStart()) {
|
|
1961
|
-
const start = cursor
|
|
2305
|
+
const start = cursor;
|
|
1962
2306
|
accept("DEFINE");
|
|
1963
2307
|
const definitions = [parseDefinition(false)];
|
|
1964
2308
|
while (isDefinitionStart()) definitions.push(parseDefinition(false));
|
|
@@ -2009,16 +2353,14 @@ const parse = (
|
|
|
2009
2353
|
if (mode !== "sany" && getStepLevel(step) < 0 && getStepName(step) !== undefined) {
|
|
2010
2354
|
throw createError("<*> and <+> cannot be used for a named step", step);
|
|
2011
2355
|
}
|
|
2012
|
-
cursor
|
|
2356
|
+
cursor++;
|
|
2013
2357
|
return { name: getStepName(step), token: step.text, location: getLocation(step) };
|
|
2014
2358
|
};
|
|
2015
2359
|
|
|
2016
|
-
// The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
|
|
2017
|
-
// of the enclosing proof.
|
|
2018
2360
|
const parseStep = (): ProofStep => {
|
|
2019
|
-
const start = cursor
|
|
2361
|
+
const start = cursor;
|
|
2020
2362
|
const step = parseStepStart(isAt("QED", 1));
|
|
2021
|
-
const statementStart = cursor
|
|
2363
|
+
const statementStart = cursor;
|
|
2022
2364
|
const scope = new Set<string>();
|
|
2023
2365
|
proofScopes.push(scope);
|
|
2024
2366
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
@@ -2038,14 +2380,14 @@ const parse = (
|
|
|
2038
2380
|
location: SourceLocation,
|
|
2039
2381
|
): Exclude<Proof, { kind: "Steps" }> | undefined => {
|
|
2040
2382
|
if (accept("BY")) {
|
|
2041
|
-
const keyword = getRange(cursor
|
|
2383
|
+
const keyword = getRange(cursor - 1);
|
|
2042
2384
|
return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
|
|
2043
2385
|
}
|
|
2044
2386
|
const terminal = peek();
|
|
2045
2387
|
if (terminal.kind !== "Keyword" || (terminal.text !== "OBVIOUS" && terminal.text !== "OMITTED"))
|
|
2046
2388
|
return undefined;
|
|
2047
2389
|
advance();
|
|
2048
|
-
const keyword = getRange(cursor
|
|
2390
|
+
const keyword = getRange(cursor - 1);
|
|
2049
2391
|
return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
|
|
2050
2392
|
};
|
|
2051
2393
|
|
|
@@ -2060,7 +2402,7 @@ const parse = (
|
|
|
2060
2402
|
};
|
|
2061
2403
|
|
|
2062
2404
|
const parseProof = (): Proof => {
|
|
2063
|
-
const start = cursor
|
|
2405
|
+
const start = cursor;
|
|
2064
2406
|
proofLevels.push(-1);
|
|
2065
2407
|
const proof = withProofScope(parseProofBody);
|
|
2066
2408
|
proofLevels.pop();
|
|
@@ -2070,7 +2412,6 @@ const parse = (
|
|
|
2070
2412
|
const isPreproofStart = () =>
|
|
2071
2413
|
peek().kind === "Step" || (peek().kind === "Keyword" && PROOF_STARTS.has(peek().text));
|
|
2072
2414
|
|
|
2073
|
-
// tlapm locates a USE or HIDE step by its keywords and DEF clause.
|
|
2074
2415
|
const getStepSpan = (head: SourceRange, body: StepBody) => {
|
|
2075
2416
|
if (body.kind !== "Use" && body.kind !== "Hide") return getSpan(head);
|
|
2076
2417
|
const lastDef = body.defs[body.defs.length - 1]?.location.range;
|
|
@@ -2078,21 +2419,21 @@ const parse = (
|
|
|
2078
2419
|
};
|
|
2079
2420
|
|
|
2080
2421
|
const parsePreproof = (): Preproof => {
|
|
2081
|
-
const first = cursor
|
|
2422
|
+
const first = cursor;
|
|
2082
2423
|
const location = getLocation(peek());
|
|
2083
2424
|
const isProof = accept("PROOF");
|
|
2084
2425
|
const leaf = parseLeafProof(location);
|
|
2085
2426
|
if (leaf) {
|
|
2086
2427
|
const proof = withRange(leaf, first);
|
|
2087
|
-
return { kind: "Leaf", proof, span: getSpan(proof.keyword), first, end: cursor
|
|
2428
|
+
return { kind: "Leaf", proof, span: getSpan(proof.keyword), first, end: cursor - 1 };
|
|
2088
2429
|
}
|
|
2089
2430
|
const token = peek();
|
|
2090
2431
|
if (token.kind !== "Step") {
|
|
2091
2432
|
throw createUnexpected(`Expected a step number but found ${describeToken(token)}`);
|
|
2092
2433
|
}
|
|
2093
|
-
const start = cursor
|
|
2434
|
+
const start = cursor;
|
|
2094
2435
|
advance();
|
|
2095
|
-
const statementStart = cursor
|
|
2436
|
+
const statementStart = cursor;
|
|
2096
2437
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
2097
2438
|
const head = getRange(start);
|
|
2098
2439
|
return {
|
|
@@ -2105,11 +2446,10 @@ const parse = (
|
|
|
2105
2446
|
span: getStepSpan(head, body),
|
|
2106
2447
|
first,
|
|
2107
2448
|
start,
|
|
2108
|
-
end: cursor
|
|
2449
|
+
end: cursor - 1,
|
|
2109
2450
|
};
|
|
2110
2451
|
};
|
|
2111
2452
|
|
|
2112
|
-
// A backtrack leaves the proof implicitly omitted.
|
|
2113
2453
|
const nestProof = (
|
|
2114
2454
|
items: Preproof[],
|
|
2115
2455
|
index: number,
|
|
@@ -2146,20 +2486,26 @@ const parse = (
|
|
|
2146
2486
|
throw createProofError(
|
|
2147
2487
|
`Unexpected end of (sub)proof at level ${level} before QED step`,
|
|
2148
2488
|
levelSpan,
|
|
2489
|
+
NEWLINES,
|
|
2149
2490
|
);
|
|
2150
2491
|
}
|
|
2151
2492
|
if (item.kind === "Leaf") {
|
|
2152
2493
|
throw createProofError(
|
|
2153
2494
|
`Expecting a proof step but found a${LEAF_NAMES[item.proof.kind]} leaf proof`,
|
|
2154
2495
|
item.span,
|
|
2496
|
+
NEWLINES,
|
|
2155
2497
|
);
|
|
2156
2498
|
}
|
|
2157
2499
|
if (!isFirst && item.isProof) {
|
|
2158
|
-
throw createProofError(
|
|
2500
|
+
throw createProofError(
|
|
2501
|
+
"PROOF keyword found in step that does not begin subproof",
|
|
2502
|
+
item.span,
|
|
2503
|
+
PRINTED_NEWLINE,
|
|
2504
|
+
);
|
|
2159
2505
|
}
|
|
2160
2506
|
const implicit = item.token.text[1];
|
|
2161
2507
|
if (implicit === "+" && !isFirst) {
|
|
2162
|
-
throw createProofError("<+> used but no subproof expected", item.span);
|
|
2508
|
+
throw createProofError("<+> used but no subproof expected", item.span, PRINTED_NEWLINE);
|
|
2163
2509
|
}
|
|
2164
2510
|
const stepLevel =
|
|
2165
2511
|
implicit === "*"
|
|
@@ -2202,8 +2548,6 @@ const parse = (
|
|
|
2202
2548
|
);
|
|
2203
2549
|
};
|
|
2204
2550
|
|
|
2205
|
-
// tlapm reads every step and leaf proof that follows, then nests them by level, where the first
|
|
2206
|
-
// step counts as having PROOF.
|
|
2207
2551
|
const parseTlapmProof = () => {
|
|
2208
2552
|
if (!isPreproofStart()) return undefined;
|
|
2209
2553
|
const items: Preproof[] = [];
|
|
@@ -2216,30 +2560,54 @@ const parse = (
|
|
|
2216
2560
|
throw createProofError(
|
|
2217
2561
|
"extra step(s) after finished proof",
|
|
2218
2562
|
extra.span,
|
|
2563
|
+
NO_NEWLINES,
|
|
2219
2564
|
"Proof.Parser.toplevel",
|
|
2220
2565
|
);
|
|
2221
2566
|
}
|
|
2222
2567
|
return nested.proof;
|
|
2223
2568
|
};
|
|
2224
2569
|
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2570
|
+
const addFactDefinition = (
|
|
2571
|
+
module: Module,
|
|
2572
|
+
name: string,
|
|
2573
|
+
body: Expression,
|
|
2574
|
+
location: SourceLocation,
|
|
2575
|
+
) => {
|
|
2576
|
+
if (module.definitions.some((other) => other.name === name)) return;
|
|
2577
|
+
module.definitions.push({
|
|
2578
|
+
kind: "Operator",
|
|
2579
|
+
name,
|
|
2580
|
+
params: [],
|
|
2581
|
+
body,
|
|
2582
|
+
isLocal: false,
|
|
2583
|
+
isRecursive: false,
|
|
2584
|
+
location,
|
|
2585
|
+
});
|
|
2586
|
+
};
|
|
2587
|
+
|
|
2588
|
+
const parseFactName = () => {
|
|
2589
|
+
if (peek().kind !== "Identifier" || !isAt("==", 1)) return undefined;
|
|
2590
|
+
const name = expectIdentifier();
|
|
2591
|
+
factNames.add(name);
|
|
2592
|
+
advance();
|
|
2593
|
+
return name;
|
|
2594
|
+
};
|
|
2595
|
+
|
|
2596
|
+
// A theorem whose statement fails to start backtracks in tlapm, whose module ends there.
|
|
2597
|
+
const parseTheoremExpression = () => {
|
|
2598
|
+
if (!isTlapm) return parseExpression();
|
|
2599
|
+
const token = peekRaw();
|
|
2600
|
+
const statement = attempt(parseExpression);
|
|
2601
|
+
if (statement) return statement;
|
|
2602
|
+
throw createUnexpected(`Expected "====" but found ${describeToken(token)}`, token);
|
|
2230
2603
|
};
|
|
2231
2604
|
|
|
2232
2605
|
const parseTheorem = (module: Module) => {
|
|
2233
|
-
const start = cursor
|
|
2606
|
+
const start = cursor;
|
|
2234
2607
|
const keyword = advance();
|
|
2235
|
-
const
|
|
2236
|
-
const name = isNamed ? expectIdentifier() : undefined;
|
|
2237
|
-
if (name !== undefined) {
|
|
2238
|
-
factNames.add(name);
|
|
2239
|
-
advance();
|
|
2240
|
-
}
|
|
2608
|
+
const name = parseFactName();
|
|
2241
2609
|
const parsed = withProofScope(() => {
|
|
2242
|
-
const statement = isAt("ASSUME") ? parseAssumeProve() :
|
|
2610
|
+
const statement = isAt("ASSUME") ? parseAssumeProve() : parseTheoremExpression();
|
|
2243
2611
|
if (isTlapm) return { statement, proof: parseTlapmProof() };
|
|
2244
2612
|
return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
|
|
2245
2613
|
});
|
|
@@ -2251,32 +2619,13 @@ const parse = (
|
|
|
2251
2619
|
module.theorems.push(theorem);
|
|
2252
2620
|
module.units.push({ kind: "Theorem", theorem });
|
|
2253
2621
|
if (name === undefined || statement.kind === "AssumeProve") return;
|
|
2254
|
-
addFactDefinition(
|
|
2255
|
-
module,
|
|
2256
|
-
withRange(
|
|
2257
|
-
{
|
|
2258
|
-
kind: "Operator",
|
|
2259
|
-
name,
|
|
2260
|
-
params: [],
|
|
2261
|
-
body: statement,
|
|
2262
|
-
isLocal: false,
|
|
2263
|
-
isRecursive: false,
|
|
2264
|
-
location: getLocation(keyword),
|
|
2265
|
-
},
|
|
2266
|
-
start,
|
|
2267
|
-
),
|
|
2268
|
-
);
|
|
2622
|
+
addFactDefinition(module, name, statement, theorem.location);
|
|
2269
2623
|
};
|
|
2270
2624
|
|
|
2271
2625
|
const parseAssumption = (): Assumption => {
|
|
2272
|
-
const start = cursor
|
|
2626
|
+
const start = cursor;
|
|
2273
2627
|
const keyword = advance();
|
|
2274
|
-
const
|
|
2275
|
-
const name = isNamed ? expectIdentifier() : undefined;
|
|
2276
|
-
if (name !== undefined) {
|
|
2277
|
-
factNames.add(name);
|
|
2278
|
-
advance();
|
|
2279
|
-
}
|
|
2628
|
+
const name = parseFactName();
|
|
2280
2629
|
return withRange(
|
|
2281
2630
|
{
|
|
2282
2631
|
name,
|
|
@@ -2312,14 +2661,13 @@ const parse = (
|
|
|
2312
2661
|
recursiveScopes.push(new Set());
|
|
2313
2662
|
if (accept("EXTENDS")) {
|
|
2314
2663
|
parseSeparated(() => {
|
|
2315
|
-
const start = cursor
|
|
2664
|
+
const start = cursor;
|
|
2316
2665
|
module.extends.push(expectModuleName());
|
|
2317
2666
|
module.extendsRanges.push(getRange(start));
|
|
2318
2667
|
});
|
|
2319
2668
|
}
|
|
2320
2669
|
while (true) {
|
|
2321
|
-
const tokenIndex = cursor
|
|
2322
|
-
unitStart.index = tokenIndex;
|
|
2670
|
+
const tokenIndex = cursor;
|
|
2323
2671
|
const token = peek();
|
|
2324
2672
|
if (token.kind === "ModuleEnd") {
|
|
2325
2673
|
location.range = {
|
|
@@ -2356,7 +2704,7 @@ const parse = (
|
|
|
2356
2704
|
case "VARIABLES": {
|
|
2357
2705
|
const unitLocation = getLocation(advance());
|
|
2358
2706
|
const declarations = parseSeparated(() => {
|
|
2359
|
-
const start = cursor
|
|
2707
|
+
const start = cursor;
|
|
2360
2708
|
const location = getLocation(peek());
|
|
2361
2709
|
return withRange({ name: expectIdentifier(), arity: 0, location }, start);
|
|
2362
2710
|
});
|
|
@@ -2372,21 +2720,12 @@ const parse = (
|
|
|
2372
2720
|
module.assumptions.push(assumption);
|
|
2373
2721
|
module.units.push({ kind: "Assumption", assumption });
|
|
2374
2722
|
if (assumption.name !== undefined) {
|
|
2375
|
-
addFactDefinition(module,
|
|
2376
|
-
kind: "Operator",
|
|
2377
|
-
name: assumption.name,
|
|
2378
|
-
params: [],
|
|
2379
|
-
body: assumption.expression,
|
|
2380
|
-
isLocal: false,
|
|
2381
|
-
isRecursive: false,
|
|
2382
|
-
location: assumption.location,
|
|
2383
|
-
});
|
|
2723
|
+
addFactDefinition(module, assumption.name, assumption.expression, assumption.location);
|
|
2384
2724
|
}
|
|
2385
2725
|
continue;
|
|
2386
2726
|
}
|
|
2387
2727
|
case "USE":
|
|
2388
2728
|
case "HIDE": {
|
|
2389
|
-
// SANY's module body takes a USE only when ONLY doesn't follow it.
|
|
2390
2729
|
if (isAt("USE") && isAt("ONLY", 1)) {
|
|
2391
2730
|
throw createUnexpected(`Expected a module unit but found ${describeToken(token)}`);
|
|
2392
2731
|
}
|
|
@@ -2440,9 +2779,7 @@ const parse = (
|
|
|
2440
2779
|
};
|
|
2441
2780
|
|
|
2442
2781
|
const module = parseModuleBody();
|
|
2443
|
-
|
|
2444
|
-
// module's name from a keyword that the parser would take for one.
|
|
2445
|
-
const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
|
|
2782
|
+
const sanyError = isKeywordModuleName ? getSanyParseError(source) : undefined;
|
|
2446
2783
|
if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
|
|
2447
2784
|
return module;
|
|
2448
2785
|
};
|