| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
"""A visitor for ANTLR notation ASTs, producing Sphinx nodes. |
| 11 |
|
| 12 |
Unlike the HTML visitor, this produces Sphinx-friendly nodes that can be used by |
| 13 |
all backends. If you just want HTML output, use the HTML visitor. |
| 14 |
""" |
| 15 |
|
| 16 |
from .parsing import parse |
| 17 |
from .TacticNotationsParser import TacticNotationsParser |
| 18 |
from .TacticNotationsVisitor import TacticNotationsVisitor |
| 19 |
|
| 20 |
from docutils import nodes |
| 21 |
from sphinx import addnodes |
| 22 |
|
| 23 |
class TacticNotationsToSphinxVisitor(TacticNotationsVisitor): |
| 24 |
def defaultResult(self): |
| 25 |
return [] |
| 26 |
|
| 27 |
def aggregateResult(self, aggregate, nextResult): |
| 28 |
if nextResult: |
| 29 |
aggregate.extend(nextResult) |
| 30 |
return aggregate |
| 31 |
|
| 32 |
def visitAlternative(self, ctx:TacticNotationsParser.AlternativeContext): |
| 33 |
return [nodes.inline('', '', *self.visitChildren(ctx), classes=['alternative'])] |
| 34 |
|
| 35 |
def visitAltblock(self, ctx:TacticNotationsParser.AltblockContext): |
| 36 |
return [nodes.inline('', '', *self.visitChildren(ctx), classes=['alternative-block'])] |
| 37 |
|
| 38 |
def visitAltsep(self, ctx:TacticNotationsParser.AltsepContext): |
| 39 |
return [nodes.inline('|', '\u200b', classes=['alternative-separator'])] |
| 40 |
|
| 41 |
@staticmethod |
| 42 |
def is_alternative(node): |
| 43 |
return isinstance(node, nodes.inline) and node['classes'] == ['alternative'] |
| 44 |
|
| 45 |
def visitRepeat(self, ctx:TacticNotationsParser.RepeatContext): |
| 46 |
|
| 47 |
|
| 48 |
separator = ctx.ATOM() or ctx.PIPE() |
| 49 |
|
| 50 |
|
| 51 |
classes = 'repeat-wrapper-with-sub' if separator else 'repeat-wrapper' |
| 52 |
wrapper = nodes.inline('', '', classes=[classes]) |
| 53 |
|
| 54 |
children = self.visitChildren(ctx) |
| 55 |
if len(children) == 1 and self.is_alternative(children[0]): |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
children[0]['classes'] = ['repeated-alternative'] |
| 60 |
wrapper += nodes.inline('', '', *children, classes=["repeat"]) |
| 61 |
|
| 62 |
repeat_marker = ctx.LGROUP().getText()[1] |
| 63 |
wrapper += nodes.inline(repeat_marker, repeat_marker, classes=['notation-sup']) |
| 64 |
|
| 65 |
if separator: |
| 66 |
sep = separator.getText() |
| 67 |
wrapper += nodes.inline(sep, sep, classes=['notation-sub']) |
| 68 |
|
| 69 |
return [wrapper] |
| 70 |
|
| 71 |
def visitCurlies(self, ctx:TacticNotationsParser.CurliesContext): |
| 72 |
sp = nodes.inline('', '', classes=["curlies"]) |
| 73 |
sp += nodes.Text("{") |
| 74 |
sp.extend(self.visitChildren(ctx)) |
| 75 |
sp += nodes.Text("}") |
| 76 |
return [sp] |
| 77 |
|
| 78 |
def escape(self, atom): |
| 79 |
node = nodes.inline("","") |
| 80 |
while atom != "": |
| 81 |
if atom[0] == "'": |
| 82 |
node += nodes.raw("\\textquotesingle{}", "\\textquotesingle{}", format="latex") |
| 83 |
node += nodes.raw("'", "'", format="html") |
| 84 |
atom = atom[1:] |
| 85 |
elif atom[0] == "`": |
| 86 |
node += nodes.raw("\\`{}", "\\`{}", format="latex") |
| 87 |
node += nodes.raw("`", "`", format="html") |
| 88 |
atom = atom[1:] |
| 89 |
else: |
| 90 |
index_ap = atom.find("'") |
| 91 |
index_bt = atom.find("`") |
| 92 |
if index_ap == -1: |
| 93 |
index = index_bt |
| 94 |
elif index_bt == -1: |
| 95 |
index = index_ap |
| 96 |
else: |
| 97 |
index = min(index_ap, index_bt) |
| 98 |
lit = atom if index == -1 else atom[:index] |
| 99 |
node += nodes.inline(lit, lit) |
| 100 |
atom = atom[len(lit):] |
| 101 |
return node |
| 102 |
|
| 103 |
def visitAtomic(self, ctx:TacticNotationsParser.AtomicContext): |
| 104 |
atom = ctx.ATOM().getText() |
| 105 |
sub = ctx.SUB() |
| 106 |
node = self.escape(atom) |
| 107 |
|
| 108 |
if sub: |
| 109 |
sub_index = sub.getText()[2:] |
| 110 |
node += nodes.subscript(sub_index, sub_index) |
| 111 |
|
| 112 |
return [node] |
| 113 |
|
| 114 |
def visitPipe(self, ctx:TacticNotationsParser.PipeContext): |
| 115 |
return [nodes.Text("|")] |
| 116 |
|
| 117 |
def visitHole(self, ctx:TacticNotationsParser.HoleContext): |
| 118 |
hole = ctx.ID().getText() |
| 119 |
token_name = hole[1:] |
| 120 |
node = nodes.inline(hole, token_name, classes=["hole"]) |
| 121 |
|
| 122 |
sub = ctx.SUB() |
| 123 |
if sub: |
| 124 |
sub_index = sub.getText()[2:] |
| 125 |
node += nodes.subscript(sub_index, sub_index) |
| 126 |
|
| 127 |
return [addnodes.pending_xref(token_name, node, reftype='token', |
| 128 |
refdomain='std', reftarget=token_name)] |
| 129 |
|
| 130 |
def visitEscaped(self, ctx:TacticNotationsParser.EscapedContext): |
| 131 |
escaped = ctx.ESCAPED().getText() |
| 132 |
return [self.escape(escaped.replace("%", ""))] |
| 133 |
|
| 134 |
def visitWhitespace(self, ctx:TacticNotationsParser.WhitespaceContext): |
| 135 |
return [nodes.Text(" ")] |
| 136 |
|
| 137 |
def sphinxify(notation): |
| 138 |
"""Translate a notation into a Sphinx document tree.""" |
| 139 |
vs = TacticNotationsToSphinxVisitor() |
| 140 |
return vs.visit(parse(notation)) |
| 141 |
|
| 142 |
def main(): |
| 143 |
print(sphinxify("a := {+, b {+ c}}")) |
| 144 |
|
| 145 |
if __name__ == '__main__': |
| 146 |
main() |