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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
#######################################################################
# Name: export.py
# Purpose: Export support for arpeggio
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2009 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import io
from arpeggio import Terminal
class Exporter(object):
"""
Base class for all Exporters.
"""
def __init__(self):
super(Exporter, self).__init__()
# Export initialization
self._render_set = set() # Used in rendering to prevent
# rendering
# of the same node multiple times
self._adapter_map = {} # Used as a registry of adapters to
# ensure that the same adapter is
# returned for the same adaptee object
def export(self, obj):
"""
Export of an obj to a string.
"""
self._outf = io.StringIO()
self._export(obj)
return self._outf.getvalue()
def exportFile(self, obj, file_name):
"""
Export of obj to a file.
"""
self._outf = open(file_name, "w")
self._export(obj)
self._outf.close()
def _export(self, obj):
self._outf.write(self._start())
self._render_node(obj)
self._outf.write(self._end())
def _start(self):
"""
Override this to specify the beginning of the graph representation.
"""
return ""
def _end(self):
"""
Override this to specify the end of the graph representation.
"""
return ""
class ExportAdapter(object):
"""
Base adapter class for the export support.
Adapter should be defined for every export and graph type.
Attributes:
adaptee: A node to adapt.
export: An export object used as a context of the export.
"""
def __init__(self, node, export):
self.adaptee = node # adaptee is adapted graph node
self.export = export
# -------------------------------------------------------------------------
# Support for DOT language
class DOTExportAdapter(ExportAdapter):
"""
Base adapter class for the DOT export support.
"""
@property
def id(self):
"""
Graph node unique identification.
"""
raise NotImplementedError()
@property
def desc(self):
"""
Graph node textual description.
"""
raise NotImplementedError()
@property
def neighbours(self):
"""
A set of adjacent graph nodes.
"""
raise NotImplementedError()
class PMDOTExportAdapter(DOTExportAdapter):
"""
Adapter for ParsingExpression graph types (parser model).
"""
@property
def id(self):
return id(self.adaptee)
@property
def desc(self):
return self.adaptee.desc
@property
def neighbours(self):
if not hasattr(self, "_neighbours"):
self._neighbours= []
# Registry of adapters used in this export
adapter_map = self.export._adapter_map
for c, n in enumerate(self.adaptee.nodes):
if isinstance(n, PMDOTExportAdapter):
# if the neighbour node is already adapted use that adapter
self._neighbours.append((str(c + 1), n))
elif id(n) in adapter_map:
# current node is adaptee -> there is registered adapter
self._neighbours.append((str(c + 1), adapter_map[id(n)]))
else:
# Create new adapter
adapter = PMDOTExportAdapter(n, self.export)
self._neighbours.append((str(c + 1), adapter))
adapter_map[adapter.id] = adapter
return self._neighbours
class PTDOTExportAdapter(PMDOTExportAdapter):
"""
Adapter for ParseTreeNode graph types.
"""
@property
def neighbours(self):
if isinstance(self.adaptee, Terminal):
return []
else:
if not hasattr(self, "_neighbours"):
self._neighbours = []
for c, n in enumerate(self.adaptee):
adapter = PTDOTExportAdapter(n, self.export)
self._neighbours.append((str(c + 1), adapter))
return self._neighbours
class DOTExporter(Exporter):
"""
Export to DOT language (part of GraphViz, see http://www.graphviz.org/)
"""
def _render_node(self, node):
if not node in self._render_set:
self._render_set.add(node)
self._outf.write('\n%s [label="%s"];' %
(node.id, self._dot_label_esc(node.desc)))
#TODO Comment handling
# if hasattr(node, "comments") and root.comments:
# retval += self.node(root.comments)
# retval += '\n%s->%s [label="comment"]' % \
#(id(root), id(root.comments))
for name, n in node.neighbours:
self._outf.write('\n%s->%s [label="%s"]' %
(node.id, n.id, name))
self._outf.write('\n')
self._render_node(n)
def _start(self):
return "digraph arpeggio_graph {"
def _end(self):
return "\n}"
def _dot_label_esc(self, to_esc):
to_esc = to_esc.replace("\\", "\\\\")
to_esc = to_esc.replace('\"', '\\"')
to_esc = to_esc.replace('\n', '\\n')
return to_esc
class PMDOTExporter(DOTExporter):
"""
A convenience DOTExport extension that uses ParserExpressionDOTExportAdapter
"""
def export(self, obj):
return super(PMDOTExporter, self).\
export(PMDOTExportAdapter(obj, self))
def exportFile(self, obj, file_name):
return super(PMDOTExporter, self).\
exportFile(PMDOTExportAdapter(obj, self), file_name)
class PTDOTExporter(DOTExporter):
"""
A convenience DOTExport extension that uses PTDOTExportAdapter
"""
def export(self, obj):
return super(PTDOTExporter, self).\
export(PTDOTExportAdapter(obj, self))
def exportFile(self, obj, file_name):
return super(PTDOTExporter, self).\
exportFile(PTDOTExportAdapter(obj, self), file_name)