annotate python/astviewer.py @ 192:6cd6260789a1

Added more tests for parser generator
author Windel Bouwman
date Sun, 26 May 2013 23:19:27 +0200
parents ee0d30533dae
children 8b2f20aae086
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 *
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
3 from c3 import Visitor, astnodes
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):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
7 self.functionIco = QIcon(QPixmap('functionicon.png').scaled(32, 32))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
8 self.variableIco = QIcon(QPixmap('variableicon.png').scaled(32, 32))
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
9 self.visitor = Visitor(self.p1, self.p2)
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
10 self.model = QStandardItemModel()
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
11 self.model.setHorizontalHeaderLabels(['Object', 'Type'])
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
12 def build(self, pkg):
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
13 #self.model.clear()
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
14 c = self.model.rowCount()
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
15 self.model.removeRows(0, c)
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
16 self.curItem = self.model.invisibleRootItem()
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
17 self.visitor.visit(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
18 def p1(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
19 if type(node) is astnodes.Variable:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
20 i = QStandardItem(self.variableIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
21 elif type(node) is astnodes.Function:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
22 i = QStandardItem(self.functionIco, str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
23 elif type(node) is astnodes.Package:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
24 i = QStandardItem(str(node))
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
25 else:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
26 return
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
27 typ = str(node.typ) if hasattr(node, 'typ') else ''
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
28 ti = QStandardItem(str(typ))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
29 ti.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
30 i.setData(node)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
31 self.curItem.appendRow([i, ti])
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
32 self.curItem = i
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
33 def p2(self, node):
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
34 if type(node) in [astnodes.Variable, astnodes.Function, astnodes.Package]:
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
35 self.curItem = self.curItem.parent()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 # The actual widget:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 class AstViewer(QTreeView):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
39 sigNodeSelected = pyqtSignal(object)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 super(AstViewer, self).__init__(parent)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
42 self.clicked.connect(self.selectHandler)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
43 self.modelBuilder = AstModelBuilder()
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
44 self.setModel(self.modelBuilder.model)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 def setAst(self, ast):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 """ Create a new model and add all ast elements to it """
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
48 self.modelBuilder.build(ast)
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 165
diff changeset
49 self.expandAll()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
51 def selectHandler(self, index):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 if not index.isValid():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 return
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
54 model = self.model()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
55 item = model.itemFromIndex(index)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
56 node = item.data()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
57 self.sigNodeSelected.emit(node)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59