File: sphinx.py

package info (click to toggle)
coq 8.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,468 kB
  • sloc: ml: 210,451; sh: 3,345; python: 3,008; ansic: 2,482; makefile: 793; lisp: 224; javascript: 63; xml: 24; sed: 2
file content (146 lines) | stat: -rw-r--r-- 5,858 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
##########################################################################
##         #   The Coq Proof Assistant / The Coq Development Team       ##
##  v      #         Copyright INRIA, CNRS and contributors             ##
## <O___,, # (see version control and CREDITS file for authors & dates) ##
##   \VV/  ###############################################################
##    //   #    This file is distributed under the terms of the         ##
##         #     GNU Lesser General Public License Version 2.1          ##
##         #     (see LICENSE file for the text of the license)         ##
##########################################################################
"""A visitor for ANTLR notation ASTs, producing Sphinx nodes.

Unlike the HTML visitor, this produces Sphinx-friendly nodes that can be used by
all backends. If you just want HTML output, use the HTML visitor.
"""

from .parsing import parse
from .TacticNotationsParser import TacticNotationsParser
from .TacticNotationsVisitor import TacticNotationsVisitor

from docutils import nodes
from sphinx import addnodes

class TacticNotationsToSphinxVisitor(TacticNotationsVisitor):
    def defaultResult(self):
        return []

    def aggregateResult(self, aggregate, nextResult):
        if nextResult:
            aggregate.extend(nextResult)
        return aggregate

    def visitAlternative(self, ctx:TacticNotationsParser.AlternativeContext):
        return [nodes.inline('', '', *self.visitChildren(ctx), classes=['alternative'])]

    def visitAltblock(self, ctx:TacticNotationsParser.AltblockContext):
        return [nodes.inline('', '', *self.visitChildren(ctx), classes=['alternative-block'])]

    def visitAltsep(self, ctx:TacticNotationsParser.AltsepContext):
        return [nodes.inline('|', '\u200b', classes=['alternative-separator'])]

    @staticmethod
    def is_alternative(node):
        return isinstance(node, nodes.inline) and node['classes'] == ['alternative']

    def visitRepeat(self, ctx:TacticNotationsParser.RepeatContext):
        # Uses inline nodes instead of subscript and superscript to ensure that
        # we get the right customization hooks at the LaTeX level
        separator = ctx.ATOM() or ctx.PIPE()
        # I wanted to have 2 independent classes "repeat-wrapper" and "with-sub" here,
        # but that breaks the latex build (invalid .tex file)
        classes = 'repeat-wrapper-with-sub' if separator  else 'repeat-wrapper'
        wrapper = nodes.inline('', '', classes=[classes])

        children = self.visitChildren(ctx)
        if len(children) == 1 and self.is_alternative(children[0]):
            # Use a custom style if an alternative is nested in a repeat.
            # (We could detect this in CSS, but it's much harder in LaTeX.)

            children[0]['classes'] = ['repeated-alternative']
        wrapper += nodes.inline('', '', *children, classes=["repeat"])

        repeat_marker = ctx.LGROUP().getText()[1]
        wrapper += nodes.inline(repeat_marker, repeat_marker, classes=['notation-sup'])

        if separator:
            sep = separator.getText()
            wrapper += nodes.inline(sep, sep, classes=['notation-sub'])

        return [wrapper]

    def visitCurlies(self, ctx:TacticNotationsParser.CurliesContext):
        sp = nodes.inline('', '', classes=["curlies"])
        sp += nodes.Text("{")
        sp.extend(self.visitChildren(ctx))
        sp += nodes.Text("}")
        return [sp]

    def escape(self, atom):
        node = nodes.inline("","")
        while atom != "":
            if atom[0] == "'":
                node += nodes.raw("\\textquotesingle{}", "\\textquotesingle{}", format="latex")
                node += nodes.raw("'", "'", format="html")
                atom = atom[1:]
            elif atom[0] == "`":
                node += nodes.raw("\\`{}", "\\`{}", format="latex")
                node += nodes.raw("`", "`", format="html")
                atom = atom[1:]
            else:
                index_ap = atom.find("'")
                index_bt = atom.find("`")
                if index_ap == -1:
                    index = index_bt
                elif index_bt == -1:
                    index = index_ap
                else:
                    index = min(index_ap, index_bt)
                lit = atom if index == -1 else atom[:index]
                node += nodes.inline(lit, lit)
                atom = atom[len(lit):]
        return node

    def visitAtomic(self, ctx:TacticNotationsParser.AtomicContext):
        atom = ctx.ATOM().getText()
        sub = ctx.SUB()
        node = self.escape(atom)

        if sub:
            sub_index = sub.getText()[2:]
            node += nodes.subscript(sub_index, sub_index)

        return [node]

    def visitPipe(self, ctx:TacticNotationsParser.PipeContext):
        return [nodes.Text("|")]

    def visitHole(self, ctx:TacticNotationsParser.HoleContext):
        hole = ctx.ID().getText()
        token_name = hole[1:]
        node = nodes.inline(hole, token_name, classes=["hole"])

        sub = ctx.SUB()
        if sub:
            sub_index = sub.getText()[2:]
            node += nodes.subscript(sub_index, sub_index)

        return [addnodes.pending_xref(token_name, node, reftype='token',
                                 refdomain='std', reftarget=token_name)]

    def visitEscaped(self, ctx:TacticNotationsParser.EscapedContext):
        escaped = ctx.ESCAPED().getText()
        return [self.escape(escaped.replace("%", ""))]

    def visitWhitespace(self, ctx:TacticNotationsParser.WhitespaceContext):
        return [nodes.Text(" ")]

def sphinxify(notation):
    """Translate a notation into a Sphinx document tree."""
    vs = TacticNotationsToSphinxVisitor()
    return vs.visit(parse(notation))

def main():
    print(sphinxify("a := {+, b {+ c}}"))

if __name__ == '__main__':
    main()