tla-ts 0.0.9 → 0.0.10-dev.b4a856304

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.
Files changed (390) hide show
  1. package/README.md +29 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +28 -28
  5. package/dist/deciders.js +43 -43
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +72 -10
  8. package/dist/index.d.ts +72 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +128 -103
  21. package/dist/transfer.js +128 -103
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +576 -300
  30. package/src/cli/options.ts +11 -6
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlapm.ts +0 -12
  35. package/src/cli/tlc-arguments.ts +646 -0
  36. package/src/cli/tlc-usage.ts +362 -0
  37. package/src/cli/tlc.ts +538 -220
  38. package/src/cli/typescript.ts +23 -0
  39. package/src/cli/xml-exporter.ts +4 -16
  40. package/src/cli.ts +0 -1
  41. package/src/codegen.ts +191 -64
  42. package/src/config.ts +140 -109
  43. package/src/coverage.ts +837 -0
  44. package/src/dependencies.ts +6 -40
  45. package/src/dfid.ts +618 -0
  46. package/src/environment.ts +240 -118
  47. package/src/errors.ts +70 -55
  48. package/src/evaluate.ts +630 -297
  49. package/src/exit-codes.ts +37 -3
  50. package/src/fingerprints.ts +10 -10
  51. package/src/index.ts +3 -0
  52. package/src/instances.ts +79 -11
  53. package/src/java-overrides.ts +2 -12
  54. package/src/json-reader.ts +456 -0
  55. package/src/json.ts +262 -139
  56. package/src/levels.ts +2 -3
  57. package/src/lexer.ts +17 -26
  58. package/src/liveness-checker.ts +10 -18
  59. package/src/liveness.ts +121 -49
  60. package/src/obligations/action.ts +4 -28
  61. package/src/obligations/anonymize.ts +84 -80
  62. package/src/obligations/equality.ts +1 -1
  63. package/src/obligations/expression.ts +2 -19
  64. package/src/obligations/format.ts +1 -9
  65. package/src/obligations/generate.ts +1 -11
  66. package/src/obligations/index.ts +0 -11
  67. package/src/obligations/levels.ts +4 -8
  68. package/src/obligations/module.ts +1 -15
  69. package/src/obligations/normalize.ts +5 -24
  70. package/src/obligations/operators.ts +1 -2
  71. package/src/obligations/pragma.ts +1 -10
  72. package/src/obligations/printer.ts +47 -46
  73. package/src/obligations/proof.ts +1 -5
  74. package/src/obligations/regroup.ts +1 -4
  75. package/src/obligations/simplify.ts +3 -14
  76. package/src/obligations/source.ts +1 -21
  77. package/src/obligations/subexpression.ts +5 -11
  78. package/src/obligations/substitution.ts +1 -3
  79. package/src/obligations/temporal.ts +8 -14
  80. package/src/obligations/tuples.ts +3 -9
  81. package/src/parallel-worker.ts +29 -39
  82. package/src/parallel.ts +29 -13
  83. package/src/parser.ts +436 -90
  84. package/src/pcal/ast.ts +7 -28
  85. package/src/pcal/parse.ts +11 -39
  86. package/src/pcal/tokenize.ts +4 -20
  87. package/src/pcal/translate.ts +10 -42
  88. package/src/pcal/validate.ts +3 -21
  89. package/src/pretty-print.ts +1 -2
  90. package/src/prover/decide-pool.ts +5 -10
  91. package/src/prover/deciders.ts +0 -4
  92. package/src/prover/first-order.ts +0 -11
  93. package/src/prover/isabelle/decide.ts +0 -6
  94. package/src/prover/isabelle/encode.ts +11 -23
  95. package/src/prover/isabelle/isar/inner-syntax.ts +108 -114
  96. package/src/prover/isabelle/isar/method.ts +25 -38
  97. package/src/prover/isabelle/isar/proof.ts +1 -10
  98. package/src/prover/isabelle/isar/statement.ts +1 -6
  99. package/src/prover/isabelle/isar/theory.ts +15 -26
  100. package/src/prover/isabelle/isar/type-check.ts +11 -25
  101. package/src/prover/isabelle/provers/blast/blast.ts +10 -41
  102. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  103. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  104. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  105. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  106. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  107. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  108. package/src/prover/isabelle/provers/classical.ts +14 -42
  109. package/src/prover/isabelle/provers/hypsubst.ts +11 -25
  110. package/src/prover/isabelle/pure/bicompose.ts +16 -13
  111. package/src/prover/isabelle/pure/consts.ts +1 -7
  112. package/src/prover/isabelle/pure/conv.ts +1 -9
  113. package/src/prover/isabelle/pure/drule.ts +24 -40
  114. package/src/prover/isabelle/pure/envir.ts +3 -52
  115. package/src/prover/isabelle/pure/goal.ts +1 -7
  116. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  117. package/src/prover/isabelle/pure/list.ts +0 -3
  118. package/src/prover/isabelle/pure/logic.ts +1 -14
  119. package/src/prover/isabelle/pure/name.ts +2 -9
  120. package/src/prover/isabelle/pure/net.ts +3 -59
  121. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  122. package/src/prover/isabelle/pure/pattern.ts +2 -19
  123. package/src/prover/isabelle/pure/raw-simplifier.ts +13 -49
  124. package/src/prover/isabelle/pure/rewriter.ts +26 -58
  125. package/src/prover/isabelle/pure/search.ts +3 -10
  126. package/src/prover/isabelle/pure/seq.ts +1 -10
  127. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  128. package/src/prover/isabelle/pure/table.ts +1 -31
  129. package/src/prover/isabelle/pure/tactic.ts +12 -22
  130. package/src/prover/isabelle/pure/tactical.ts +6 -12
  131. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  132. package/src/prover/isabelle/pure/term.ts +2 -43
  133. package/src/prover/isabelle/pure/thm.ts +13 -36
  134. package/src/prover/isabelle/pure/type.ts +2 -15
  135. package/src/prover/isabelle/pure/unify.ts +20 -30
  136. package/src/prover/isabelle/pure/variable.ts +3 -7
  137. package/src/prover/isabelle/tla/classical-setup.ts +9 -19
  138. package/src/prover/isabelle/tla/context.ts +8 -16
  139. package/src/prover/isabelle/tla/logic.ts +4 -11
  140. package/src/prover/isabelle/tla/simpdata.ts +46 -34
  141. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  142. package/src/prover/ocaml.ts +0 -6
  143. package/src/prover/prep/action.ts +4 -36
  144. package/src/prover/prep/enabled-axioms.ts +12 -19
  145. package/src/prover/prep/fingerprint.ts +0 -16
  146. package/src/prover/prep/level-comparison.ts +8 -12
  147. package/src/prover/prep/md5.ts +0 -2
  148. package/src/prover/prep/methods.ts +9 -36
  149. package/src/prover/prep/normalize.ts +4 -28
  150. package/src/prover/prove-worker.ts +0 -2
  151. package/src/prover/prove.ts +0 -36
  152. package/src/prover/ptl/decide.ts +7 -12
  153. package/src/prover/ptl/encode.ts +14 -36
  154. package/src/prover/ptl/sat.ts +24 -32
  155. package/src/prover/ptl/tableau.ts +12 -26
  156. package/src/prover/smt/axioms.ts +0 -7
  157. package/src/prover/smt/context.ts +19 -26
  158. package/src/prover/smt/data.ts +51 -54
  159. package/src/prover/smt/encode.ts +14 -35
  160. package/src/prover/smt/extensionality.ts +0 -10
  161. package/src/prover/smt/flatten.ts +3 -22
  162. package/src/prover/smt/format.ts +5 -10
  163. package/src/prover/smt/instances.ts +0 -10
  164. package/src/prover/smt/rewrite.ts +66 -101
  165. package/src/prover/smt/solver/act-cache.ts +0 -15
  166. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  167. package/src/prover/smt/solver/array.ts +1 -3
  168. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  169. package/src/prover/smt/solver/ast-lt.ts +1 -11
  170. package/src/prover/smt/solver/ast-util.ts +1 -6
  171. package/src/prover/smt/solver/ast.ts +1 -143
  172. package/src/prover/smt/solver/auf-solver.ts +6 -39
  173. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  174. package/src/prover/smt/solver/cg-table.ts +1 -18
  175. package/src/prover/smt/solver/clause.ts +1 -27
  176. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  177. package/src/prover/smt/solver/context.ts +4 -61
  178. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  179. package/src/prover/smt/solver/der.ts +2 -21
  180. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  181. package/src/prover/smt/solver/e-matching.ts +3 -34
  182. package/src/prover/smt/solver/enode.ts +1 -21
  183. package/src/prover/smt/solver/expr-ref.ts +1 -5
  184. package/src/prover/smt/solver/expr-safe-replace.ts +0 -5
  185. package/src/prover/smt/solver/fingerprints.ts +1 -12
  186. package/src/prover/smt/solver/flat-literals.ts +1 -9
  187. package/src/prover/smt/solver/goal.ts +2 -14
  188. package/src/prover/smt/solver/hash.ts +0 -8
  189. package/src/prover/smt/solver/hashtable.ts +1 -12
  190. package/src/prover/smt/solver/internalizer.ts +6 -98
  191. package/src/prover/smt/solver/lbool.ts +1 -2
  192. package/src/prover/smt/solver/literal.ts +0 -3
  193. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  194. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  195. package/src/prover/smt/solver/lp/containers.ts +12 -30
  196. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  197. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  198. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  199. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  200. package/src/prover/smt/solver/lp/dioph-eq.ts +12 -98
  201. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  202. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  203. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  204. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  205. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  206. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  207. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  208. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  209. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  210. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  211. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  212. package/src/prover/smt/solver/lp/nex.ts +1 -6
  213. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  214. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  215. package/src/prover/smt/solver/lp/nla-core.ts +35 -130
  216. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  217. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  218. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  220. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  221. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  222. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  223. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  224. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  225. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  226. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  227. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  228. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  229. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  230. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  231. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  232. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  233. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  234. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  235. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  236. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  237. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  238. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  239. package/src/prover/smt/solver/mam-compiler.ts +14 -56
  240. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  241. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  242. package/src/prover/smt/solver/model-checker.ts +2 -36
  243. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  244. package/src/prover/smt/solver/model-finder.ts +3 -15
  245. package/src/prover/smt/solver/model-generator.ts +1 -12
  246. package/src/prover/smt/solver/model-macro-solver.ts +12 -69
  247. package/src/prover/smt/solver/nlsat/algebraic.ts +55 -151
  248. package/src/prover/smt/solver/nlsat/atoms.ts +5 -19
  249. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  250. package/src/prover/smt/solver/nlsat/explain.ts +3 -47
  251. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  252. package/src/prover/smt/solver/nlsat/levelwise.ts +48 -129
  253. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  254. package/src/prover/smt/solver/nlsat/polynomial.ts +17 -156
  255. package/src/prover/smt/solver/nlsat/solver.ts +7 -67
  256. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  257. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -44
  258. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  259. package/src/prover/smt/solver/nnf.ts +3 -38
  260. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  261. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  262. package/src/prover/smt/solver/propagate-values.ts +1 -10
  263. package/src/prover/smt/solver/proto-model.ts +3 -48
  264. package/src/prover/smt/solver/pull-quant.ts +1 -12
  265. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  266. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  267. package/src/prover/smt/solver/qe-lite.ts +3 -33
  268. package/src/prover/smt/solver/qi-queue.ts +1 -16
  269. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  270. package/src/prover/smt/solver/quantifier-analyzer.ts +17 -37
  271. package/src/prover/smt/solver/quantifier-manager.ts +1 -8
  272. package/src/prover/smt/solver/random-gen.ts +1 -2
  273. package/src/prover/smt/solver/rational.ts +1 -8
  274. package/src/prover/smt/solver/ref-vector.ts +1 -6
  275. package/src/prover/smt/solver/relevancy.ts +2 -15
  276. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  277. package/src/prover/smt/solver/rewriter.ts +3 -85
  278. package/src/prover/smt/solver/rlimit.ts +2 -20
  279. package/src/prover/smt/solver/smtlib-parser.ts +2 -37
  280. package/src/prover/smt/solver/solver.ts +23 -34
  281. package/src/prover/smt/solver/sort.ts +1 -5
  282. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  283. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  284. package/src/prover/smt/solver/theory-lra-bounds.ts +0 -5
  285. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  286. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -27
  287. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  288. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  289. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  290. package/src/prover/smt/solver/theory-lra.ts +1 -8
  291. package/src/prover/smt/solver/var-subst.ts +2 -26
  292. package/src/prover/smt/table.ts +0 -8
  293. package/src/prover/tlapm/cache.ts +20 -25
  294. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  295. package/src/prover/tlapm/tlapm.ts +12 -17
  296. package/src/prover/verdict.ts +0 -2
  297. package/src/prover/zenon/check/check.ts +0 -9
  298. package/src/prover/zenon/check/definitions.ts +5 -15
  299. package/src/prover/zenon/check/equality.ts +0 -8
  300. package/src/prover/zenon/check/functions.ts +0 -4
  301. package/src/prover/zenon/check/keys.ts +0 -7
  302. package/src/prover/zenon/check/propositional.ts +0 -3
  303. package/src/prover/zenon/check/records.ts +0 -5
  304. package/src/prover/zenon/check/relations.ts +0 -13
  305. package/src/prover/zenon/check/rewrites.ts +0 -6
  306. package/src/prover/zenon/check/sets.ts +0 -3
  307. package/src/prover/zenon/check/terms.ts +0 -17
  308. package/src/prover/zenon/check/tla.ts +0 -6
  309. package/src/prover/zenon/decide.ts +0 -12
  310. package/src/prover/zenon/encode.ts +1 -8
  311. package/src/prover/zenon/eqrel.ts +0 -13
  312. package/src/prover/zenon/equality-rules.ts +0 -4
  313. package/src/prover/zenon/expr.ts +1 -26
  314. package/src/prover/zenon/index-table.ts +17 -38
  315. package/src/prover/zenon/isar-case.ts +9 -19
  316. package/src/prover/zenon/isar.ts +1 -29
  317. package/src/prover/zenon/limits.ts +0 -4
  318. package/src/prover/zenon/llproof.ts +0 -9
  319. package/src/prover/zenon/mltoll.ts +54 -76
  320. package/src/prover/zenon/node.ts +1 -11
  321. package/src/prover/zenon/ocaml.ts +1 -17
  322. package/src/prover/zenon/parse.ts +2 -7
  323. package/src/prover/zenon/phrase.ts +1 -11
  324. package/src/prover/zenon/proof.ts +0 -9
  325. package/src/prover/zenon/propositional-rules.ts +0 -5
  326. package/src/prover/zenon/rules.ts +2 -6
  327. package/src/prover/zenon/search.ts +1 -17
  328. package/src/prover/zenon/tla/functions.ts +57 -29
  329. package/src/prover/zenon/tla/llproof.ts +63 -70
  330. package/src/prover/zenon/tla/records.ts +56 -27
  331. package/src/prover/zenon/tla/rewrites.ts +12 -19
  332. package/src/prover/zenon/tla/sets.ts +68 -39
  333. package/src/prover/zenon/tla/terms.ts +0 -6
  334. package/src/prover/zenon/tla/tla.ts +59 -58
  335. package/src/randomization.ts +84 -35
  336. package/src/resolve.ts +18 -60
  337. package/src/runtime.ts +11 -4
  338. package/src/sany/builtins.ts +3 -4
  339. package/src/sany/generator.ts +119 -169
  340. package/src/sany/levels.ts +6 -30
  341. package/src/sany/lexer.ts +7 -23
  342. package/src/sany/linter.ts +1 -8
  343. package/src/sany/modules.ts +1 -5
  344. package/src/sany/nodes.ts +0 -31
  345. package/src/sany/parse-error.ts +39 -117
  346. package/src/sany/report.ts +29 -63
  347. package/src/sany/selectors.ts +15 -39
  348. package/src/sany/source.ts +1 -17
  349. package/src/sany/xml.ts +13 -63
  350. package/src/search.ts +494 -154
  351. package/src/simulate.ts +694 -0
  352. package/src/spec-description.ts +166 -48
  353. package/src/standard-definitions.ts +2 -3
  354. package/src/standard-modules.ts +264 -68
  355. package/src/state-graph.ts +51 -18
  356. package/src/subexpressions.ts +31 -5
  357. package/src/substitution-levels.ts +2 -3
  358. package/src/successors.ts +267 -77
  359. package/src/symmetry.ts +31 -8
  360. package/src/tableau.ts +27 -24
  361. package/src/tlc/fingerprint.ts +183 -61
  362. package/src/tlc/jar-data.ts +113 -6
  363. package/src/tlc/messages.ts +122 -37
  364. package/src/tlc/order.ts +33 -38
  365. package/src/tlc/random.ts +16 -16
  366. package/src/tlc/replay.ts +563 -211
  367. package/src/tlc/summary.ts +2 -4
  368. package/src/tlc/tlc-operators.ts +81 -36
  369. package/src/tlc/tlcext-operators.ts +138 -117
  370. package/src/tlc/trace-spec.ts +273 -0
  371. package/src/tlc/value-stream.ts +143 -0
  372. package/src/tlc/version.ts +2 -4
  373. package/src/transfer.ts +22 -25
  374. package/src/typescript/actions.ts +481 -0
  375. package/src/typescript/collections.ts +600 -0
  376. package/src/typescript/compile.ts +65 -0
  377. package/src/typescript/context.ts +94 -0
  378. package/src/typescript/emit.ts +121 -0
  379. package/src/typescript/expressions.ts +496 -0
  380. package/src/typescript/formula.ts +152 -0
  381. package/src/typescript/functions.ts +284 -0
  382. package/src/typescript/model.ts +163 -0
  383. package/src/typescript/names.ts +114 -0
  384. package/src/typescript/program.ts +48 -0
  385. package/src/typescript/properties.ts +139 -0
  386. package/src/typescript/scope.ts +112 -0
  387. package/src/typescript/steps.ts +165 -0
  388. package/src/typescript/types.ts +171 -0
  389. package/src/values.ts +826 -276
  390. package/src/worker.ts +7 -8
@@ -1,13 +1,6 @@
1
1
  import { createSanyLexer, escapeLexeme, getKind, type SanyToken } from "./lexer";
2
2
  import { SANY_OPERATOR_SYNONYMS } from "../tlc/jar-data";
3
3
 
4
- // The report SANY's parser, TLAplusParser, gives for a module it fails to parse. This interprets
5
- // the grammar of javacc/tla+.jj the way the code JavaCC generates from it runs: the productions,
6
- // their lookahead specifications, and the semantic actions that steer parsing or set the
7
- // `expecting` message. TLAplusParser.msgStackToString prints that message, the token the parser
8
- // failed at, and the productions it was in, so the interpreter must fail at the same token, in the
9
- // same productions, and must look as far ahead as JavaCC's jj_rescan_token does.
10
-
11
4
  type Predicate = (state: ParserState) => boolean;
12
5
 
13
6
  interface TokensUnit {
@@ -35,14 +28,11 @@ interface RepeatUnit {
35
28
  body: SequenceUnit;
36
29
  }
37
30
 
38
- // Java code in a production, which may throw a ParseException with a message.
39
31
  interface ActionUnit {
40
32
  type: "action";
41
33
  run: (state: ParserState) => string | undefined;
42
34
  }
43
35
 
44
- // A LOOKAHEAD specification. First in a choice's alternative or in a loop's body, it decides
45
- // whether to take that expansion. Elsewhere, JavaCC checks only its semantic part.
46
36
  interface LookaheadUnit {
47
37
  type: "lookahead";
48
38
  amount: number;
@@ -59,11 +49,8 @@ type Unit =
59
49
  | ActionUnit
60
50
  | LookaheadUnit;
61
51
 
62
- // A token kind in angle brackets, a production's name, or a unit.
63
52
  type Part = Unit | string;
64
53
 
65
- // A SyntaxTreeNode: a token, or what a production matched, which spans from its first token to
66
- // its last.
67
54
  interface SyntaxNode {
68
55
  kind: string;
69
56
  heirs: SyntaxNode[];
@@ -71,22 +58,17 @@ interface SyntaxNode {
71
58
  last: SanyToken;
72
59
  }
73
60
 
74
- // The production being parsed: the nodes of what it has matched, and the node it returns if that
75
- // is not a node of its own kind over them.
76
61
  interface Frame {
77
62
  heirs: SyntaxNode[];
78
63
  node: SyntaxNode | undefined;
79
64
  }
80
65
 
81
66
  interface Production {
82
- // The name TLAplusParser.bpa pushes on the message stack, if the production calls it.
83
67
  message: string | undefined;
84
68
  body: SequenceUnit;
85
- // Java code that runs after the production's epa.
86
69
  exit: ((frame: Frame, state: ParserState) => string | void) | undefined;
87
70
  }
88
71
 
89
- // An Operator of Operators.CanonicalOperators. None of them is right-associative.
90
72
  interface Operator {
91
73
  name: string;
92
74
  low: number;
@@ -95,7 +77,6 @@ interface Operator {
95
77
  fix: "prefix" | "postfix" | "infix" | "nfix";
96
78
  }
97
79
 
98
- // An OSelement of OperatorStack: an operand, or an operator with its node.
99
80
  interface StackElement {
100
81
  node: SyntaxNode;
101
82
  operator: Operator | undefined;
@@ -107,7 +88,6 @@ interface Analysis {
107
88
  hasSemanticStart: boolean;
108
89
  }
109
90
 
110
- // A jj_2 call, which jj_rescan_token scans again if it looked past the current token: JJCalls.
111
91
  interface LookaheadCall {
112
92
  generation: number;
113
93
  first: SanyToken;
@@ -134,7 +114,6 @@ interface ParserState {
134
114
  readToken: () => SanyToken;
135
115
  lexicalError: string | undefined;
136
116
  token: SanyToken;
137
- // jj_gen, which counts consumed tokens, and its value at the last failed jj_ntk switch.
138
117
  generation: number;
139
118
  failedSwitchGeneration: number;
140
119
  scanPosition: SanyToken;
@@ -146,15 +125,14 @@ interface ParserState {
146
125
  calls: Map<SequenceUnit, LookaheadCall[]>;
147
126
  expecting: string;
148
127
  messages: StackMessage[];
128
+ pushedErrors: string[];
149
129
  moduleName: string | undefined;
150
130
  junctions: Junction[];
151
131
  frames: Frame[];
152
- // OperatorStack's stack of stacks, one for each Expression being parsed.
153
132
  operatorStacks: StackElement[][];
154
133
  lastOperator: Operator | undefined;
155
134
  proofLevels: number[];
156
135
  isTerminalProof: boolean;
157
- // Whether FairnessHook is set, which it is if the last NoOpExtension ended with arguments.
158
136
  isFairnessHooked: boolean;
159
137
  isFairnessParenthesized: boolean[];
160
138
  report: string | undefined;
@@ -162,7 +140,6 @@ interface ParserState {
162
140
 
163
141
  type ScanResult = "matched" | "failed" | "done";
164
142
 
165
- // JavaCC's amount for a LOOKAHEAD that gives an expansion but no amount.
166
143
  const UNBOUNDED_AMOUNT = 2147483647;
167
144
  const EOF = getKind("EOF");
168
145
  const IDENTIFIER = getKind("IDENTIFIER");
@@ -171,10 +148,9 @@ const DEFBREAK = getKind("DEFBREAK");
171
148
  const getKindRange = (first: string, last: string) =>
172
149
  Array.from(
173
150
  { length: getKind(last) - getKind(first) + 1 },
174
- (_, offset) => getKind(first) + offset,
151
+ (_element, offset) => getKind(first) + offset,
175
152
  );
176
153
 
177
- // TLAplusParser.isOp, isInfixOp, isPostfixOp, and isFieldNameToken.
178
154
  const OPERATOR_KINDS = new Set(getKindRange("op_57", "op_119"));
179
155
  const INFIX_KINDS = new Set(getKindRange("op_1", "op_119"));
180
156
  const POSTFIX_KINDS = new Set(getKindRange("op_57", "op_70"));
@@ -196,7 +172,6 @@ const createOperators = (
196
172
  ...names: string[]
197
173
  ) => names.map((name): [string, Operator] => [name, { name, low, high, isLeftAssociative, fix }]);
198
174
 
199
- // Operators.CanonicalOperators, but for ".", which only OperatorStack.reduceRecord handles.
200
175
  const CANONICAL_OPERATORS = new Map([
201
176
  ...createOperators("postfix", 160, 160, true, "["),
202
177
  ...createOperators("postfix", 150, 150, false, "'", "^+", "^*", "^#"),
@@ -294,15 +269,17 @@ const CANONICAL_OPERATORS = new Map([
294
269
  ...createOperators("infix", 10, 10, false, "=>"),
295
270
  ]);
296
271
 
297
- // Operators.getOperator, which knows an operator by its name or a synonym.
298
272
  const OPERATORS = new Map([
299
273
  ...CANONICAL_OPERATORS,
300
274
  ...SANY_OPERATOR_SYNONYMS.flatMap((synonyms) =>
301
- synonyms.slice(1).map((synonym) => [synonym, CANONICAL_OPERATORS.get(synonyms[0])] as const),
275
+ synonyms
276
+ .slice(1)
277
+ .map((synonym): [string, Operator | undefined] => [
278
+ synonym,
279
+ CANONICAL_OPERATORS.get(synonyms[0]),
280
+ ]),
302
281
  ),
303
282
  ]);
304
- // OperatorStack's fcnOp, which a function application's "[" pushes, and Operator.VoidOperator,
305
- // which OperatorStack.finalReduce pushes to reduce the whole stack.
306
283
  const FCN_OPERATOR = OPERATORS.get("[");
307
284
  const VOID_OPERATOR: Operator = {
308
285
  name: "$$_void",
@@ -355,8 +332,6 @@ const getNext = (state: ParserState, token: SanyToken) => {
355
332
  return token.next;
356
333
  };
357
334
 
358
- // TLAplusParser.getToken, which counts from the scan position while a semantic lookahead runs
359
- // during a scan.
360
335
  const getToken = (state: ParserState, index: number) => {
361
336
  let token = state.isLookingAhead ? state.scanPosition : state.token;
362
337
  for (let count = 0; count < index; count++) token = getNext(state, token);
@@ -368,7 +343,6 @@ const isNextKind = (...names: string[]) => {
368
343
  return (state: ParserState) => kinds.includes(getToken(state, 1).kind);
369
344
  };
370
345
 
371
- // JunctionListContext.isAboveCurrent, for the next token by default, and isNewBullet.
372
346
  const isAboveCurrent = (state: ParserState, column = getToken(state, 1).beginColumn) => {
373
347
  const junction = state.junctions.at(-1);
374
348
  return junction === undefined || junction.column < column;
@@ -380,17 +354,14 @@ const isNewBullet = (state: ParserState) => {
380
354
  return junction?.column === token.beginColumn && junction.kind === token.kind;
381
355
  };
382
356
 
383
- // TLAplusParser.caseSep.
384
357
  const isCaseSeparator = (state: ParserState) =>
385
358
  isNextKind("CASESEP")(state) && isAboveCurrent(state);
386
359
 
387
- // The Java code that lets a keyword name a module or a record field.
388
360
  const setFieldName = (state: ParserState) => {
389
361
  const token = getToken(state, 1);
390
362
  if (FIELD_NAME_KINDS.has(token.kind)) token.kind = IDENTIFIER;
391
363
  };
392
364
 
393
- // SyntaxTreeNode.getLocation for the tokens first to last, as Location.toString prints it.
394
365
  const formatLocation = (state: ParserState, first: SanyToken, last = first) =>
395
366
  `line ${first.beginLine}, col ${first.beginColumn} to line ${last.beginLine}, col ${last.beginColumn + last.image.length - 1} of module ${state.moduleName}`;
396
367
 
@@ -401,7 +372,6 @@ const createNode = (kind: string, heirs: SyntaxNode[]) => ({
401
372
  last: heirs[heirs.length - 1].last,
402
373
  });
403
374
 
404
- // The node offset nodes back among those of what the production being parsed has matched.
405
375
  const getHeir = (state: ParserState, offset: number) => {
406
376
  const heirs = state.frames[state.frames.length - 1].heirs;
407
377
  return heirs[heirs.length - offset];
@@ -410,16 +380,12 @@ const getHeir = (state: ParserState, offset: number) => {
410
380
  const getOperatorStack = (state: ParserState) =>
411
381
  state.operatorStacks[state.operatorStacks.length - 1];
412
382
 
413
- // Operator.succ and Operator.prec: whether left binds tighter than right, or looser.
414
383
  const isTighter = (left: Operator, right: Operator) => left.low > right.high;
415
384
  const isLooser = (left: Operator, right: Operator) => left.high < right.low;
416
385
 
417
- // Operators.getMixfix, which turns "-" into the prefix "-.".
418
386
  const getMixfix = (operator: Operator) =>
419
387
  operator.fix === "prefix" ? operator : OPERATORS.get(`${operator.name}.`);
420
388
 
421
- // OperatorStack.reducePostfix: replaces the operand and postfix operator below index top with
422
- // their expression.
423
389
  const reducePostfix = (stack: StackElement[], top: number) => {
424
390
  const operand = stack[top - 2].node;
425
391
  const postfix = stack[top - 1];
@@ -432,8 +398,6 @@ const reducePostfix = (stack: StackElement[], top: number) => {
432
398
  });
433
399
  };
434
400
 
435
- // OperatorStack.reducePrefix and reduceInfix: replace the operator below the top of the stack and
436
- // its operands with their expression.
437
401
  const reduceOperator = (stack: StackElement[], operator: Operator) => {
438
402
  const top = stack.length - 1;
439
403
  if (operator.fix !== "infix" && operator.fix !== "nfix") {
@@ -455,11 +419,6 @@ const reduceOperator = (stack: StackElement[], operator: Operator) => {
455
419
  });
456
420
  };
457
421
 
458
- // OperatorStack.reduceStack, run when an operator is pushed: it reduces the operators below it
459
- // that bind tighter, and gives the message it throws where precedence can't order two operators,
460
- // or two operands follow each other. The grammar pushes an infix or postfix operator only after an
461
- // operand, except for a "-" that Expression takes for a prefix one, so the checks that reject
462
- // other orders never fail and are left out.
463
422
  const reduceStack = (state: ParserState): string | undefined => {
464
423
  const stack = getOperatorStack(state);
465
424
  const top = stack.length - 1;
@@ -514,8 +473,6 @@ const reduceStack = (state: ParserState): string | undefined => {
514
473
  : `\n Illegal combination of operators ${operator.name} in block ${location} and ${right.operator.name}.`;
515
474
  };
516
475
 
517
- // OperatorStack.pushOnStack for the node of the production just parsed: an operand, or, if
518
- // isOperator holds of it, an operator, which reduceStack reduces with what is below it.
519
476
  const pushOnStack = (isOperator: (node: SyntaxNode) => boolean) =>
520
477
  action((state) => {
521
478
  const stack = getOperatorStack(state);
@@ -530,30 +487,27 @@ const pushOnStack = (isOperator: (node: SyntaxNode) => boolean) =>
530
487
  const PUSH_OPERAND = pushOnStack(() => false);
531
488
  const PUSH_OPERATOR = pushOnStack(() => true);
532
489
 
533
- // TLAplusParser's lastOp, which the productions of operators set.
534
490
  const SET_LAST_OPERATOR = action((state) => {
535
491
  state.lastOperator = OPERATORS.get(state.token.image);
536
492
  });
537
493
 
538
- // The OpArgs node that a PrimitiveExp's heirs end with, directly or in their last "!" extension.
539
494
  const getArguments = (heirs: SyntaxNode[]) => {
540
495
  const last = heirs[heirs.length - 1];
541
496
  const node = last.kind === "BangExt" ? last.heirs[last.heirs.length - 1] : last;
542
497
  return node.kind === "OpArgs" ? node : undefined;
543
498
  };
544
499
 
545
- // TLAplusParser.isLabel: whether a node is an identifier, or an operator applied to identifiers.
500
+ // SANY also takes A!B!lbl(x) for a label, its bug tlaplus/tlaplus#885.
546
501
  const isLabel = (node: SyntaxNode) => {
547
502
  if (node.kind === "GeneralId") return true;
548
- const opArgs = node.kind === "OpApplication" ? getArguments(node.heirs) : undefined;
503
+ const opArgs =
504
+ node.kind === "OpApplication" && node.heirs.length === 2 ? getArguments(node.heirs) : undefined;
549
505
  return (
550
506
  opArgs !== undefined &&
551
507
  opArgs.heirs.every((argument, index) => index % 2 === 0 || argument.kind === "GeneralId")
552
508
  );
553
509
  };
554
510
 
555
- // The first node below node, stopping at junction lists, that does not start to the right of the
556
- // current junction list's bullet: what TLAplusParser.checkIndentation reports.
557
511
  const getMisindentedNode = (state: ParserState, node: SyntaxNode): SyntaxNode | undefined => {
558
512
  for (const child of node.heirs) {
559
513
  if (child.kind === "Junctions") continue;
@@ -564,9 +518,6 @@ const getMisindentedNode = (state: ParserState, node: SyntaxNode): SyntaxNode |
564
518
  return undefined;
565
519
  };
566
520
 
567
- // TLAplusParser.belchDEF, which inserts a DEFBREAK token before the left-hand side of the next
568
- // definition. It reverses the links of the tokens it walks over, so that `next` leads back toward
569
- // the definition's start, and restores them when done.
570
521
  const belchDef = (state: ParserState) => {
571
522
  const stops = ["EOF", "THEOREM", "PROPOSITION", "ASSUMPTION", "END_MODULE"].map(getKind);
572
523
  const isKind = (token: SanyToken, ...names: string[]) =>
@@ -627,8 +578,6 @@ const belchDef = (state: ParserState) => {
627
578
  current.next = next;
628
579
  };
629
580
 
630
- // TLAplusParser.matchFcnConst: whether the next tokens are "<< ... >> \in" or a list of
631
- // identifiers followed by \in.
632
581
  const matchFcnConst = (state: ParserState) => {
633
582
  let token = getToken(state, 1);
634
583
  if (token.kind === getKind("LAB")) {
@@ -651,13 +600,11 @@ const matchFcnConst = (state: ParserState) => {
651
600
  return token.kind === getKind("IN");
652
601
  };
653
602
 
654
- // TLAplusParser.levelOfProofStepLexeme: -1 for <*> and -2 for <+>.
655
603
  const getStepLevel = (token: SanyToken) => {
656
604
  if (token.image[1] === "*") return -1;
657
605
  return token.image[1] === "+" ? -2 : Number.parseInt(token.image.slice(1));
658
606
  };
659
607
 
660
- // TLAplusParser.getProofLevel. TLAplusParser's proofDepth is the length of proofLevels less 1.
661
608
  const getProofLevel = (state: ParserState) => state.proofLevels.at(-1) ?? -1;
662
609
 
663
610
  const beginsProof = (state: ParserState) => {
@@ -679,7 +626,6 @@ const beginsProof = (state: ParserState) => {
679
626
  return isNextKind("BY", "PROOF", "OBVIOUS", "OMITTED")(state);
680
627
  };
681
628
 
682
- // TLAplusParser.correctLevel, which also sets the level of a proof from its first step.
683
629
  const isCorrectLevel = (state: ParserState, token: SanyToken) => {
684
630
  const level = getStepLevel(token);
685
631
  const depth = state.proofLevels.length - 1;
@@ -725,8 +671,7 @@ const STEP_START = tokens(
725
671
  ].map(getKind),
726
672
  );
727
673
 
728
- // The prefix operators and the operand that Expression and ExtendableExpr share. Each use is a
729
- // separate lookahead site, as in the generated parser.
674
+ // Each use is a separate lookahead site, as in the generated parser.
730
675
  const getPrefixOperators = () =>
731
676
  zeroOrMore(
732
677
  lookahead({ expansion: [choice(PREFIX_OPERATOR, "<op_77>")], semantic: isAboveCurrent }),
@@ -777,7 +722,6 @@ const setTerminalProof = (isTerminalProof: boolean) =>
777
722
  state.isTerminalProof = isTerminalProof;
778
723
  });
779
724
 
780
- // An alternative of OperatorOrFunctionDefinition: a left-hand side, then "==", then a body.
781
725
  const definition = (expecting: string, ...parts: Part[]) =>
782
726
  sequence(
783
727
  ...parts.slice(0, -1),
@@ -1047,7 +991,6 @@ const PRODUCTIONS: Record<string, Production> = {
1047
991
  }),
1048
992
  "Expression",
1049
993
  ]),
1050
- // A label returns from AssumeProve after the labeled AssumeProve, which a choice expresses.
1051
994
  AssumeProve: production("Assume-Prove", [
1052
995
  choice(
1053
996
  sequence("Identifier", "<COLONCOLON>", "AssumeProve"),
@@ -1513,7 +1456,14 @@ const PRODUCTIONS: Record<string, Production> = {
1513
1456
  ]),
1514
1457
  ExceptComponent: production("Except Component", [
1515
1458
  choice(
1516
- sequence("<DOT>", action(setFieldName), "Identifier"),
1459
+ sequence(
1460
+ "<DOT>",
1461
+ action(setFieldName),
1462
+ "Identifier",
1463
+ action((state) => {
1464
+ if (state.token.image === "@") state.pushedErrors.push("@ used in !.@");
1465
+ }),
1466
+ ),
1517
1467
  sequence("<LSB>", "Expression", zeroOrMore("<COMMA>", "Expression"), "<RSB>"),
1518
1468
  ),
1519
1469
  ]),
@@ -1522,7 +1472,6 @@ const PRODUCTIONS: Record<string, Production> = {
1522
1472
  optional("Expression", zeroOrMore("<COMMA>", "Expression")),
1523
1473
  choice("<RAB>", sequence("<ARAB>", "ReducedExpression")),
1524
1474
  ]),
1525
- // The innermost NoOpExtension sets FairnessHook to its arguments, which end with ")".
1526
1475
  NoOpExtension: production(undefined, [
1527
1476
  "Identifier",
1528
1477
  optional(lookahead({ amount: 2 }), "OpArgs"),
@@ -1599,18 +1548,13 @@ const PRODUCTIONS: Record<string, Production> = {
1599
1548
  state.junctions.pop();
1600
1549
  }),
1601
1550
  ]),
1602
- JuncItem: production(
1603
- "Junction Item",
1604
- [choice("<OR>", "<AND>"), "Expression"],
1605
- // TLAplusParser.checkIndentation.
1606
- (frame, state) => {
1607
- const item = getMisindentedNode(state, frame.heirs[1]);
1608
- return (
1609
- item &&
1610
- `Item at ${formatLocation(state, item.first, item.last)} is not properly indented inside conjunction or disjunction list item at ${formatLocation(state, frame.heirs[0].first, frame.heirs[1].last)}`
1611
- );
1612
- },
1613
- ),
1551
+ JuncItem: production("Junction Item", [choice("<OR>", "<AND>"), "Expression"], (frame, state) => {
1552
+ const item = getMisindentedNode(state, frame.heirs[1]);
1553
+ return (
1554
+ item &&
1555
+ `Item at ${formatLocation(state, item.first, item.last)} is not properly indented inside conjunction or disjunction list item at ${formatLocation(state, frame.heirs[0].first, frame.heirs[1].last)}`
1556
+ );
1557
+ }),
1614
1558
  UnboundOrBoundChoose: production("(Un)Bounded Choose", [
1615
1559
  "<CHOOSE>",
1616
1560
  choice("Identifier", "IdentifierTuple"),
@@ -1637,8 +1581,6 @@ const PRODUCTIONS: Record<string, Production> = {
1637
1581
  getPrefixOperators(),
1638
1582
  getOperand(),
1639
1583
  ],
1640
- // OperatorStack.finalReduce, which pushes an operator that binds looser than any other, whose
1641
- // node, null in Java, is never printed.
1642
1584
  (frame, state) => {
1643
1585
  const stack = getOperatorStack(state);
1644
1586
  stack.push({ node: frame.heirs[0], operator: VOID_OPERATOR });
@@ -1653,7 +1595,6 @@ const PRODUCTIONS: Record<string, Production> = {
1653
1595
  sequence(
1654
1596
  lookahead({
1655
1597
  expansion: [choice("<AND>", "<OR>")],
1656
- // OperatorStack.preInEmptyTop.
1657
1598
  semantic: (state) => {
1658
1599
  const top = state.operatorStacks.at(-1)?.at(-1);
1659
1600
  return (
@@ -1680,7 +1621,6 @@ const PRODUCTIONS: Record<string, Production> = {
1680
1621
  action(setFieldName),
1681
1622
  lookahead({ semantic: isAboveCurrent }),
1682
1623
  "Identifier",
1683
- // OperatorStack.reduceRecord, which first applies a postfix operator on top.
1684
1624
  action((state) => {
1685
1625
  const stack = getOperatorStack(state);
1686
1626
  if (stack[stack.length - 1].operator !== undefined) reducePostfix(stack, stack.length);
@@ -1698,7 +1638,6 @@ const PRODUCTIONS: Record<string, Production> = {
1698
1638
  lookahead({ expansion: ["<LSB>"], semantic: isAboveCurrent }),
1699
1639
  expect("function argument"),
1700
1640
  "SBracketCases",
1701
- // TLAplusParser.isGenOp, which holds of a function application.
1702
1641
  pushOnStack((node) => node.kind === "FcnAppl"),
1703
1642
  ),
1704
1643
  ),
@@ -1724,7 +1663,6 @@ const PRODUCTIONS: Record<string, Production> = {
1724
1663
  return undefined;
1725
1664
  }),
1726
1665
  "Expression",
1727
- // TLAplusParser.labelDoesNotChangeParse, given the operator parsed last.
1728
1666
  action((state) => {
1729
1667
  const stack = getOperatorStack(state);
1730
1668
  const labeled = getHeir(state, 1);
@@ -1851,9 +1789,6 @@ const PRODUCTIONS: Record<string, Production> = {
1851
1789
 
1852
1790
  const analyses = new Map<Unit, Analysis>();
1853
1791
 
1854
- // What JavaCC's ParseEngine.genFirstSet and Semanticize.emptyExpansionExists compute: the tokens
1855
- // an expansion can start with, whether it can match nothing, and whether its start passes a
1856
- // semantic lookahead, which makes JavaCC decide by a jj_2 call instead of a switch (jj2LA).
1857
1792
  const analyze = (unit: Unit): Analysis => {
1858
1793
  const cached = analyses.get(unit);
1859
1794
  if (cached) return cached;
@@ -1893,8 +1828,6 @@ const evaluateSemantic = (state: ParserState, semantic: Predicate) => {
1893
1828
  return isPassed;
1894
1829
  };
1895
1830
 
1896
- // jj_scan_token, which also records how far past the current token jj_rescan_token scans, the
1897
- // length of the longest token sequence the ParseException expects.
1898
1831
  const scanToken = (state: ParserState, kinds: Set<number>): ScanResult => {
1899
1832
  if (state.scanPosition === state.lastPosition) {
1900
1833
  state.remaining--;
@@ -1915,8 +1848,6 @@ const scanToken = (state: ParserState, kinds: Set<number>): ScanResult => {
1915
1848
  return state.remaining === 0 && state.scanPosition === state.lastPosition ? "done" : "matched";
1916
1849
  };
1917
1850
 
1918
- // The jj_3 routines, which match an expansion without running its actions. A loop or option skips
1919
- // its body's lookahead, but a choice checks the semantic part of each alternative's.
1920
1851
  const scan = (state: ParserState, unit: Unit): ScanResult => {
1921
1852
  if (unit.type === "tokens") return scanToken(state, unit.kinds);
1922
1853
  if (unit.type === "call") return scan(state, PRODUCTIONS[unit.name].body);
@@ -1965,8 +1896,6 @@ const scan = (state: ParserState, unit: Unit): ScanResult => {
1965
1896
  }
1966
1897
  };
1967
1898
 
1968
- // A jj_2 call, which scans expansion with a budget of amount new tokens. It saves the call for
1969
- // jj_rescan_token as jj_save does, over a call of the same site that can't be rescanned.
1970
1899
  const scanAhead = (state: ParserState, amount: number, expansion: SequenceUnit) => {
1971
1900
  state.remaining = amount;
1972
1901
  state.scanPosition = state.token;
@@ -1985,9 +1914,6 @@ const scanAhead = (state: ParserState, amount: number, expansion: SequenceUnit)
1985
1914
  return isMatched;
1986
1915
  };
1987
1916
 
1988
- // The index of the alternative that the generated code's lookahead checks choose, or -1. As
1989
- // ParseEngine.buildLookaheadChecker builds them, a check is the semantic lookahead alone where no
1990
- // tokens are needed, a jj_ntk switch where one token decides, and a jj_2 call otherwise.
1991
1917
  const decide = (state: ParserState, alternatives: SequenceUnit[]) =>
1992
1918
  alternatives.findIndex((alternative) => {
1993
1919
  const head = alternative.units[0];
@@ -2005,9 +1931,6 @@ const decide = (state: ParserState, alternatives: SequenceUnit[]) =>
2005
1931
  return scanAhead(state, amount, expansion) && (semantic === undefined || semantic(state));
2006
1932
  });
2007
1933
 
2008
- // ParseExceptionExtended.getShortMessage for the ParseException that generateParseException
2009
- // builds, which shows as many tokens as the longest token sequence it expects. A rescan that
2010
- // finishes its lookahead ends the rescans of its site.
2011
1934
  const getEncountered = (state: ParserState) => {
2012
1935
  state.isRescanning = true;
2013
1936
  for (const [expansion, chain] of state.calls) {
@@ -2032,14 +1955,12 @@ const getEncountered = (state: ParserState) => {
2032
1955
  break;
2033
1956
  }
2034
1957
  images.push(token.image);
2035
- // SANY's message follows the tokens the parser has read, without reading another.
2036
1958
  if (index < length - 1) token = getNext(state, token);
2037
1959
  }
2038
1960
  return `Encountered "${images.join(" ")}" at line ${next.beginLine}, column ${next.beginColumn} and token "${escapeLexeme(state.token.image)}" `;
2039
1961
  };
2040
1962
 
2041
- // Records TLAplusParser.msgStackToString's report, without its "***Parse Error***" heading.
2042
- const fail = (state: ParserState, message?: string) => {
1963
+ const recordParseException = (state: ParserState, message?: string) => {
2043
1964
  const expecting = state.expecting === "" ? "" : `Was expecting "${state.expecting}"\n`;
2044
1965
  const stack = state.messages
2045
1966
  .slice(-5)
@@ -2057,7 +1978,7 @@ const consume = (state: ParserState, kinds: Set<number>) => {
2057
1978
  const next = getNext(state, state.token);
2058
1979
  if (!kinds.has(next.kind)) {
2059
1980
  state.failedSwitchGeneration = state.generation;
2060
- return fail(state);
1981
+ return recordParseException(state);
2061
1982
  }
2062
1983
  state.token = next;
2063
1984
  state.generation++;
@@ -2070,7 +1991,6 @@ const consume = (state: ParserState, kinds: Set<number>) => {
2070
1991
  return true;
2071
1992
  };
2072
1993
 
2073
- // Parses a production, and adds the node it returns to those of the production that called it.
2074
1994
  const parseProduction = (state: ParserState, name: string) => {
2075
1995
  const production = PRODUCTIONS[name];
2076
1996
  if (production.message !== undefined) {
@@ -2086,23 +2006,21 @@ const parseProduction = (state: ParserState, name: string) => {
2086
2006
  state.expecting = "";
2087
2007
  }
2088
2008
  const message = production.exit?.(frame, state);
2089
- if (typeof message === "string") return fail(state, message);
2009
+ if (typeof message === "string") return recordParseException(state, message);
2090
2010
  const node = frame.node ?? (frame.heirs.length === 0 ? undefined : createNode(name, frame.heirs));
2091
2011
  if (node !== undefined) state.frames.at(-1)?.heirs.push(node);
2092
2012
  return true;
2093
2013
  };
2094
2014
 
2095
- // Runs the parse-mode code JavaCC generates for unit. It returns false once a ParseException's
2096
- // report is recorded.
2097
2015
  const parse = (state: ParserState, unit: Unit): boolean => {
2098
2016
  if (unit.type === "tokens") return consume(state, unit.kinds);
2099
2017
  if (unit.type === "call") return parseProduction(state, unit.name);
2100
2018
  if (unit.type === "action") {
2101
2019
  const message = unit.run(state);
2102
- return message === undefined || fail(state, message);
2020
+ return message === undefined || recordParseException(state, message);
2103
2021
  }
2104
2022
  if (unit.type === "lookahead")
2105
- return unit.semantic === undefined || unit.semantic(state) || fail(state);
2023
+ return unit.semantic === undefined || unit.semantic(state) || recordParseException(state);
2106
2024
  if (unit.type === "sequence") {
2107
2025
  return unit.units.every(
2108
2026
  (inner, index) => (index === 0 && inner.type === "lookahead") || parse(state, inner),
@@ -2110,7 +2028,7 @@ const parse = (state: ParserState, unit: Unit): boolean => {
2110
2028
  }
2111
2029
  if (unit.type === "choice") {
2112
2030
  const index = decide(state, unit.alternatives);
2113
- return index === -1 ? fail(state) : parse(state, unit.alternatives[index]);
2031
+ return index === -1 ? recordParseException(state) : parse(state, unit.alternatives[index]);
2114
2032
  }
2115
2033
  if (unit.type === "oneOrMore" && !parse(state, unit.body)) return false;
2116
2034
  if (unit.type === "optional") return decide(state, [unit.body]) === -1 || parse(state, unit.body);
@@ -2120,7 +2038,6 @@ const parse = (state: ParserState, unit: Unit): boolean => {
2120
2038
  return true;
2121
2039
  };
2122
2040
 
2123
- // What SANY reports for a module its lexer or parser rejects, or undefined if it accepts the module.
2124
2041
  export const getSanyParseError = (source: string) => {
2125
2042
  const readToken = createSanyLexer(source);
2126
2043
  const start: SanyToken = { kind: EOF, image: "", beginLine: 0, beginColumn: 0, next: undefined };
@@ -2144,6 +2061,7 @@ export const getSanyParseError = (source: string) => {
2144
2061
  calls: new Map(),
2145
2062
  expecting: "nothing",
2146
2063
  messages: [],
2064
+ pushedErrors: [],
2147
2065
  moduleName: undefined,
2148
2066
  junctions: [],
2149
2067
  frames: [],
@@ -2157,5 +2075,9 @@ export const getSanyParseError = (source: string) => {
2157
2075
  };
2158
2076
  const isParsed = parse(state, { type: "call", name: "CompilationUnit" });
2159
2077
  if (state.lexicalError !== undefined) return state.lexicalError;
2160
- return isParsed || state.report === undefined ? undefined : `***Parse Error***\n${state.report}`;
2078
+ const errors = [
2079
+ ...state.pushedErrors,
2080
+ ...(isParsed || state.report === undefined ? [] : [`***Parse Error***\n${state.report}`]),
2081
+ ];
2082
+ return errors.length === 0 ? undefined : errors.join("\n");
2161
2083
  };