annotate python/astviewer.py @ 165:598d3888a11c

Added front class and fided AST view
author Windel Bouwman
date Fri, 22 Mar 2013 15:12:38 +0100
parents e023d3ce1d63
children 49f1ab80d040
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 from PyQt4.QtCore import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 from PyQt4.QtGui import *
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 97
diff changeset
3 from c3 import Visitor
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
5 class AstModelBuilder:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
6 def __init__(self):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
7 self.visitor = Visitor(self.p1, self.p2)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
8 def build(self, pkg):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
9 model = QStandardItemModel()
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
10 model.setHorizontalHeaderLabels(['Object', 'Type'])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
11 self.curItem = model.invisibleRootItem()
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
12 self.visitor.visit(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
13 return model
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
14 def p1(self, node):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
15 i = QStandardItem(str(node))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
16 typ = str(node.typ) if hasattr(node, 'typ') else ''
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
17 ti = QStandardItem(str(typ))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
18 ti.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
19 i.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
20 self.curItem.appendRow([i, ti])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
21 self.curItem = i
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
22 def p2(self, node):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
23 self.curItem = self.curItem.parent()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 # The actual widget:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 class AstViewer(QTreeView):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
27 sigNodeSelected = pyqtSignal(object)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 super(AstViewer, self).__init__(parent)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
30 self.clicked.connect(self.selectHandler)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
31 self.modelBuilder = AstModelBuilder()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 def setAst(self, ast):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 """ Create a new model and add all ast elements to it """
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 97
diff changeset
35 print(ast)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 if ast:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
37 self.setModel(self.modelBuilder.build(ast))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
38 self.expandAll()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
40 def selectHandler(self, index):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 if not index.isValid():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 return
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
43 model = self.model()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
44 item = model.itemFromIndex(index)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
45 node = item.data()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
46 self.sigNodeSelected.emit(node)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48