Debian Codegraph @

doc/tools/rocqrst/notations/plain.py

raw symbols SCIP
1 ##########################################################################
2 ## # The Rocq Prover / The Rocq Development Team ##
3 ## v # Copyright INRIA, CNRS and contributors ##
4 ## <O___,, # (see version control and CREDITS file for authors & dates) ##
5 ## \VV/ ###############################################################
6 ## // # This file is distributed under the terms of the ##
7 ## # GNU Lesser General Public License Version 2.1 ##
8 ## # (see LICENSE file for the text of the license) ##
9 ##########################################################################
10 """A visitor for ANTLR notation ASTs, producing plain text with ellipses.
11
12 Somewhat-closely approximates the rendering of the original manual.
13 """
14
15 from io import StringIO
16
17 from .parsing import parse
18 from .TacticNotationsParser import TacticNotationsParser
19 from .TacticNotationsVisitor import TacticNotationsVisitor
20
21 class TacticNotationsToDotsVisitor(TacticNotationsVisitor):
22 def __init__(self):
23 self.buffer = StringIO()
24
25 def visitAlternative(self, ctx:TacticNotationsParser.AlternativeContext):
26 self.buffer.write("[")
27 self.visitChildren(ctx)
28 self.buffer.write("]")
29
30 def visitAltsep(self, ctx:TacticNotationsParser.AltsepContext):
31 self.buffer.write("|")
32
33 def visitRepeat(self, ctx:TacticNotationsParser.RepeatContext):
34 separator = ctx.ATOM() or ctx.PIPE()
35 self.visitChildren(ctx)
36 if ctx.LGROUP().getText()[1] == "+":
37 spacer = (separator.getText() + " " if separator else "")
38 self.buffer.write(spacer + "…" + spacer)
39 self.visitChildren(ctx)
40
41 def visitCurlies(self, ctx:TacticNotationsParser.CurliesContext):
42 self.buffer.write("{")
43 self.visitChildren(ctx)
44 self.buffer.write("}")
45
46 def visitAtomic(self, ctx:TacticNotationsParser.AtomicContext):
47 self.buffer.write(ctx.ATOM().getText())
48
49 def visitPipe(self, ctx:TacticNotationsParser.PipeContext):
50 self.buffer.write("|")
51
52 def visitHole(self, ctx:TacticNotationsParser.HoleContext):
53 self.buffer.write("‘{}’".format(ctx.ID().getText()[1:]))
54
55 def visitEscaped(self, ctx:TacticNotationsParser.EscapedContext):
56 self.buffer.write(ctx.ESCAPED().getText().replace("%", ""))
57
58 def visitWhitespace(self, ctx:TacticNotationsParser.WhitespaceContext):
59 self.buffer.write(" ")
60
61 def stringify_with_ellipses(notation):
62 vs = TacticNotationsToDotsVisitor()
63 vs.visit(parse(notation))
64 return vs.buffer.getvalue()