| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
""" |
| 11 |
Use rocq doc to highlight Rocq snippets |
| 12 |
==================================== |
| 13 |
|
| 14 |
Pygment's Rocq parser isn't the best; instead, use rocq doc to highlight snippets. |
| 15 |
Only works for full, syntactically valid sentences; on shorter snippets, rocq doc |
| 16 |
swallows parts of the input. |
| 17 |
|
| 18 |
Works by reparsing rocq doc's output into the output that Sphinx expects from a |
| 19 |
lexer. |
| 20 |
""" |
| 21 |
|
| 22 |
import os |
| 23 |
import platform |
| 24 |
import pexpect |
| 25 |
from tempfile import mkstemp |
| 26 |
from subprocess import check_output |
| 27 |
|
| 28 |
from bs4 import BeautifulSoup |
| 29 |
from bs4.element import NavigableString |
| 30 |
|
| 31 |
ROCQDOC_OPTIONS = ['--body-only', '--no-glob', '--no-index', '--no-externals', |
| 32 |
'-s', '--html', '--stdout', '--utf8', '--parse-comments'] |
| 33 |
|
| 34 |
ROCQDOC_SYMBOLS = ["->", "<-", "<->", "=>", "<=", ">=", "<>", "~", "/\\", "\\/", "|-", "*", "forall", "exists"] |
| 35 |
ROCQDOC_HEADER = "".join("(** remove printing {} *)".format(s) for s in ROCQDOC_SYMBOLS) |
| 36 |
|
| 37 |
def rocqdoc(rocq_code, rocqbin=None): |
| 38 |
"""Get the output of rocq doc on rocq_code.""" |
| 39 |
rocqbin = rocqbin or os.path.join(os.getenv("COQBIN", ""), "rocq") |
| 40 |
if not pexpect.utils.which(rocqbin): |
| 41 |
raise ValueError("'{}: not found".format(rocqbin)) |
| 42 |
args = [rocqbin, "doc"] |
| 43 |
fd, filename = mkstemp(prefix="rocqdoc_", suffix=".v") |
| 44 |
if platform.system().startswith("CYGWIN"): |
| 45 |
|
| 46 |
filename = check_output(["cygpath", "-w", filename]).decode("utf-8").strip() |
| 47 |
try: |
| 48 |
os.write(fd, ROCQDOC_HEADER.encode("utf-8")) |
| 49 |
os.write(fd, rocq_code.encode("utf-8")) |
| 50 |
os.close(fd) |
| 51 |
return check_output(args + ROCQDOC_OPTIONS + [filename], timeout = 10).decode("utf-8") |
| 52 |
finally: |
| 53 |
os.remove(filename) |
| 54 |
|
| 55 |
def first_string_node(node): |
| 56 |
"""Return the first string node, or None if does not exist""" |
| 57 |
while node.children: |
| 58 |
node = next(node.children) |
| 59 |
if isinstance(node, NavigableString): |
| 60 |
return node |
| 61 |
|
| 62 |
def lex(source): |
| 63 |
"""Convert source into a stream of (css_classes, token_string).""" |
| 64 |
rocqdoc_output = rocqdoc(source) |
| 65 |
soup = BeautifulSoup(rocqdoc_output, "html.parser") |
| 66 |
root = soup.find(class_='code') |
| 67 |
|
| 68 |
first = first_string_node(root) |
| 69 |
if first and first.string[0] == '\n': |
| 70 |
first.string.replace_with(first.string[1:]) |
| 71 |
for elem in root.children: |
| 72 |
if isinstance(elem, NavigableString): |
| 73 |
yield [], elem |
| 74 |
elif elem.name == "span": |
| 75 |
if elem.string: |
| 76 |
cls = "rocqdoc-{}".format(elem.get("title", "comment")) |
| 77 |
yield [cls], elem.string |
| 78 |
else: |
| 79 |
|
| 80 |
children = list(elem.children) |
| 81 |
mlc = children[0].startswith("(*") and children[-1].endswith ("*)") |
| 82 |
for elem2 in children: |
| 83 |
if isinstance(elem2, NavigableString): |
| 84 |
cls = ["rocqdoc-comment"] if mlc else [] |
| 85 |
yield cls, elem2 |
| 86 |
elif elem2.name == 'br': |
| 87 |
pass |
| 88 |
elif elem.name == 'br': |
| 89 |
pass |
| 90 |
else: |
| 91 |
raise ValueError(elem) |
| 92 |
|
| 93 |
def main(): |
| 94 |
"""Lex stdin (for testing purposes)""" |
| 95 |
import sys |
| 96 |
for classes, text in lex(sys.stdin.read()): |
| 97 |
print(repr(text) + "\t" ' '.join(classes)) |
| 98 |
|
| 99 |
if __name__ == '__main__': |
| 100 |
main() |