|
| 1 | +# -------------------------------------------------------------- |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import docutils as du |
| 5 | + |
| 6 | +import sphinx.application as sa |
| 7 | +import sphinx.errors as se |
| 8 | +import sphinx.util as su |
| 9 | + |
| 10 | +import bisect |
| 11 | +import json |
| 12 | +import os |
| 13 | +import re |
| 14 | +import subprocess as subp |
| 15 | +import tempfile |
| 16 | + |
| 17 | +# ====================================================================== |
| 18 | +ROOT = os.path.dirname(__file__) |
| 19 | + |
| 20 | +# ====================================================================== |
| 21 | +class ProofnavNode(du.nodes.General, du.nodes.Element): |
| 22 | + @staticmethod |
| 23 | + def visit_proofnav_node_html(self, node: ProofnavNode): |
| 24 | + pass |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def depart_proofnav_node_html(self, node: ProofnavNode): |
| 28 | + uid = node["uid"] |
| 29 | + json = node["json"] |
| 30 | + |
| 31 | + html = f""" |
| 32 | +<div class="proofnav-sphinx"> |
| 33 | + <div id="{uid}" class="proofnav-mount"></div> |
| 34 | + <script type="application/json" id="{uid}-data">{json}</script> |
| 35 | +</div> |
| 36 | +""" |
| 37 | + |
| 38 | + self.body.append(html) |
| 39 | + |
| 40 | +# ====================================================================== |
| 41 | +class EasyCryptProofDirective(su.docutils.SphinxDirective): |
| 42 | + has_content = True |
| 43 | + |
| 44 | + option_spec = { |
| 45 | + 'title': su.docutils.directives.unchanged, |
| 46 | + } |
| 47 | + |
| 48 | + def run(self): |
| 49 | + env = self.state.document.settings.env |
| 50 | + |
| 51 | + rawcode = '\n'.join(self.content) + '\n' |
| 52 | + |
| 53 | + # Find the trap |
| 54 | + if (trap := re.search(r'\(\*\s*\$\s*\*\)\s*', rawcode, re.MULTILINE)) is None: |
| 55 | + raise se.SphinxError('Cannot find the trap') |
| 56 | + code = rawcode[:trap.start()] + rawcode[trap.end():] |
| 57 | + |
| 58 | + # Find the trap sentence number |
| 59 | + sentences = [ |
| 60 | + m.end() - 1 |
| 61 | + for m in re.finditer(r'\.(\s+|\$)', code) |
| 62 | + ] |
| 63 | + sentence = bisect.bisect_left(sentences, trap.start()) |
| 64 | + |
| 65 | + # Run EasyCrypt and extract the proof trace |
| 66 | + with tempfile.TemporaryDirectory(delete = False) as tmpdir: |
| 67 | + ecfile = os.path.join(tmpdir, 'input.ec') |
| 68 | + ecofile = os.path.join(tmpdir, 'input.eco') |
| 69 | + with open(ecfile, 'w') as ecstream: |
| 70 | + ecstream.write(code) |
| 71 | + subp.check_call( |
| 72 | + ['easycrypt', 'compile', '-pragmas', 'Proofs:weak', '-trace', ecfile], |
| 73 | + stdout = subp.DEVNULL, |
| 74 | + stderr = subp.DEVNULL, |
| 75 | + ) |
| 76 | + with open(ecofile) as ecostream: |
| 77 | + eco = json.load(ecostream) |
| 78 | + |
| 79 | + serial = env.new_serialno("proofnav") |
| 80 | + uid = f"proofnav-{serial}" |
| 81 | + |
| 82 | + # Create widget metadata |
| 83 | + data = dict() |
| 84 | + |
| 85 | + data["source"] = code |
| 86 | + data["sentenceEnds"] = [x["position"] for x in eco["trace"][1:]] |
| 87 | + data["sentences"] = [ |
| 88 | + dict(goals = x["goals"], message = x["messages"]) |
| 89 | + for x in eco["trace"][1:] |
| 90 | + ] |
| 91 | + data["initialSentence"] = sentence - 1 |
| 92 | + |
| 93 | + if 'title' in self.options: |
| 94 | + data['title'] = self.options['title'] |
| 95 | + |
| 96 | + node = ProofnavNode() |
| 97 | + node["uid"] = uid |
| 98 | + node["json"] = json.dumps(data, ensure_ascii = False, separators = (",", ":")) |
| 99 | + |
| 100 | + return [node] |
| 101 | + |
| 102 | +# ====================================================================== |
| 103 | +def on_builder_inited(app: sa.Sphinx): |
| 104 | + out_dir = os.path.join(app.outdir, "_static", "proofnav") |
| 105 | + os.makedirs(out_dir, exist_ok = True) |
| 106 | + |
| 107 | + js = os.path.join(ROOT, "proofnav", "dist", "proofnav.bundle.js") |
| 108 | + css = os.path.join(ROOT, "proofnav", "proofnav.css") |
| 109 | + |
| 110 | + if not os.path.exists(js): |
| 111 | + raise se.SphinxError( |
| 112 | + "proofnav: bundle not found. Run the frontend build to generate " |
| 113 | + f"{js}" |
| 114 | + ) |
| 115 | + |
| 116 | + su.fileutil.copy_asset(js, out_dir) |
| 117 | + su.fileutil.copy_asset(js + ".map", out_dir) |
| 118 | + su.fileutil.copy_asset(css, out_dir) |
| 119 | + |
| 120 | +# ====================================================================== |
| 121 | +def setup(app: sa.Sphinx) -> su.typing.ExtensionMetadata: |
| 122 | + app.add_node( |
| 123 | + ProofnavNode, |
| 124 | + html = ( |
| 125 | + ProofnavNode.visit_proofnav_node_html, |
| 126 | + ProofnavNode.depart_proofnav_node_html, |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + app.add_js_file("proofnav/proofnav.bundle.js", defer = "defer") |
| 131 | + app.add_css_file("proofnav/proofnav.css") |
| 132 | + |
| 133 | + app.connect("builder-inited", on_builder_inited) |
| 134 | + |
| 135 | + app.add_directive('ecproof', EasyCryptProofDirective) |
| 136 | + |
| 137 | + return { |
| 138 | + 'version': '0.1', |
| 139 | + 'parallel_read_safe': True, |
| 140 | + 'parallel_write_safe': True, |
| 141 | + } |
0 commit comments