| 1 |
import py |
| 2 |
from rpython.rlib.objectmodel import not_rpython |
| 3 |
|
| 4 |
class Node(object): |
| 5 |
def view(self): |
| 6 |
from dotviewer import graphclient |
| 7 |
content = ["digraph G{"] |
| 8 |
content.extend(self.dot()) |
| 9 |
content.append("}") |
| 10 |
try: |
| 11 |
p = py.test.ensuretemp("automaton").join("temp.dot") |
| 12 |
remove = False |
| 13 |
except AttributeError: |
| 14 |
p = py.path.local.mkdtemp().join('automaton.dot') |
| 15 |
remove = True |
| 16 |
p.write("\n".join(content)) |
| 17 |
graphclient.display_dot_file(str(p)) |
| 18 |
if remove: |
| 19 |
p.dirpath().remove() |
| 20 |
|
| 21 |
class Symbol(Node): |
| 22 |
|
| 23 |
def __init__(self, symbol, additional_info, token): |
| 24 |
self.symbol = symbol |
| 25 |
self.additional_info = additional_info |
| 26 |
self.token = token |
| 27 |
|
| 28 |
def getsourcepos(self): |
| 29 |
return self.token.source_pos |
| 30 |
|
| 31 |
def __repr__(self): |
| 32 |
return "Symbol(%r, %r)" % (self.symbol, self.additional_info) |
| 33 |
|
| 34 |
def dot(self): |
| 35 |
symbol = (self.symbol.replace("\\", "\\\\").replace('"', '\\"') |
| 36 |
.replace('\n', '\\l')) |
| 37 |
addinfo = str(self.additional_info).replace('"', "'") or "_" |
| 38 |
yield ('"%s" [shape=box,label="%s\\n%s"];' % ( |
| 39 |
id(self), symbol, |
| 40 |
repr(addinfo).replace('"', '').replace("\\", "\\\\"))) |
| 41 |
|
| 42 |
@not_rpython |
| 43 |
def visit(self, visitor): |
| 44 |
if isinstance(visitor, RPythonVisitor): |
| 45 |
return visitor.dispatch(self) |
| 46 |
method = getattr(visitor, "visit_" + self.symbol, None) |
| 47 |
if method is None: |
| 48 |
return self |
| 49 |
return method(self) |
| 50 |
|
| 51 |
class Nonterminal(Node): |
| 52 |
def __init__(self, symbol, children): |
| 53 |
self.children = children |
| 54 |
self.symbol = symbol |
| 55 |
|
| 56 |
def getsourcepos(self): |
| 57 |
try: |
| 58 |
return self.children[0].getsourcepos() |
| 59 |
except IndexError: |
| 60 |
raise |
| 61 |
|
| 62 |
def __str__(self): |
| 63 |
return "%s(%s)" % (self.symbol, ", ".join([str(c) for c in self.children])) |
| 64 |
|
| 65 |
def __repr__(self): |
| 66 |
return "Nonterminal(%r, %r)" % (self.symbol, self.children) |
| 67 |
|
| 68 |
def dot(self): |
| 69 |
yield '"%s" [label="%s"];' % (id(self), self.symbol) |
| 70 |
for child in self.children: |
| 71 |
yield '"%s" -> "%s";' % (id(self), id(child)) |
| 72 |
if isinstance(child, Node): |
| 73 |
for line in child.dot(): |
| 74 |
yield line |
| 75 |
else: |
| 76 |
yield '"%s" [label="%s"];' % ( |
| 77 |
id(child), |
| 78 |
repr(child).replace('"', '').replace("\\", "\\\\")) |
| 79 |
|
| 80 |
@not_rpython |
| 81 |
def visit(self, visitor): |
| 82 |
if isinstance(visitor, RPythonVisitor): |
| 83 |
return visitor.dispatch(self) |
| 84 |
general = getattr(visitor, "visit", None) |
| 85 |
if general is None: |
| 86 |
return getattr(visitor, "visit_" + self.symbol)(self) |
| 87 |
else: |
| 88 |
specific = getattr(visitor, "visit_" + self.symbol, None) |
| 89 |
if specific is None: |
| 90 |
return general(self) |
| 91 |
else: |
| 92 |
return specific(self) |
| 93 |
|
| 94 |
class VisitError(Exception): |
| 95 |
def __init__(self, node): |
| 96 |
self.node = node |
| 97 |
self.args = (node, ) |
| 98 |
|
| 99 |
def __str__(self): |
| 100 |
return "could not visit %s" % (self.node, ) |
| 101 |
|
| 102 |
def make_dispatch_function(__general_nonterminal_visit=None, |
| 103 |
__general_symbol_visit=None, |
| 104 |
__general_visit=None, |
| 105 |
**dispatch_table): |
| 106 |
def dispatch(self, node): |
| 107 |
if isinstance(node, Nonterminal): |
| 108 |
func = dispatch_table.get(node.symbol, None) |
| 109 |
if func is None: |
| 110 |
if __general_nonterminal_visit: |
| 111 |
return __general_nonterminal_visit(self, node) |
| 112 |
else: |
| 113 |
return func(self, node) |
| 114 |
elif isinstance(node, Symbol): |
| 115 |
func = dispatch_table.get(node.symbol, None) |
| 116 |
if func is None: |
| 117 |
if __general_symbol_visit: |
| 118 |
return __general_symbol_visit(self, node) |
| 119 |
else: |
| 120 |
return func(self, node) |
| 121 |
if __general_visit: |
| 122 |
return __general_visit(self, node) |
| 123 |
raise VisitError(node) |
| 124 |
return dispatch |
| 125 |
|
| 126 |
class CreateDispatchDictionaryMetaclass(type): |
| 127 |
def __new__(cls, name_, bases, dct): |
| 128 |
dispatch_table = {} |
| 129 |
for name, value in dct.iteritems(): |
| 130 |
if name.startswith("visit_"): |
| 131 |
dispatch_table[name[len("visit_"):]] = value |
| 132 |
for special in ["general_symbol_visit", |
| 133 |
"general_nonterminal_visit", |
| 134 |
"general_visit"]: |
| 135 |
if special in dct: |
| 136 |
dispatch_table["__" + special] = dct[special] |
| 137 |
dct["dispatch"] = make_dispatch_function(**dispatch_table) |
| 138 |
return type.__new__(cls, name_, bases, dct) |
| 139 |
|
| 140 |
class RPythonVisitor(object): |
| 141 |
__metaclass__ = CreateDispatchDictionaryMetaclass |