def p_ForStmt7(self, p): 'ForStmt : FOR DeclStmt SEMIC SEMIC Block' p[0] = Node("For")\ .addkid(Node("DeclExpr").addkid(p[2]))\ .addkid(Node("BooleanExpr"))\ .addkid((Node("UpdateExpr")))\ .addkid(p[5])
def p_AExpr_1(self, t): ''' AExpr : ID EQUAL ID | ID EQUAL ID COMMA | ID EQUAL ID SEMI ''' t[0] = Node('=').addkid(Node(t[1])).addkid(Node(t[3]))
def p_ForStmt5(self, p): 'ForStmt : FOR SEMIC SEMIC AssignStmt Block' p[0] = Node("For")\ .addkid(Node("DeclExpr"))\ .addkid(Node("BooleanExpr"))\ .addkid(Node("UpdateExpr").addkid(p[4]))\ .addkid(p[5])
def p_ForStmt6(self, p): 'ForStmt : FOR SEMIC BooleanExpr SEMIC Block' p[0] = Node("For")\ .addkid(Node("DeclExpr"))\ .addkid(Node("BooleanExpr").addkid(p[3]))\ .addkid(Node("UpdateExpr"))\ .addkid(p[5])
def p_ForStmt(self, p): 'ForStmt : FOR DeclStmt SEMIC BooleanExpr SEMIC AssignStmt Block' p[0] = Node("For")\ .addkid(Node("DeclExpr").addkid(p[2]))\ .addkid(Node("BooleanExpr").addkid(p[4]))\ .addkid(Node("UpdateExpr").addkid(p[6]))\ .addkid(p[7])
def p_WhileStmt2(self, p): 'WhileStmt : WHILE Block' p[0] = Node("For")\ .addkid(Node("DeclExpr"))\ .addkid(Node("BooleanExpr"))\ .addkid(Node("UpdateExpr"))\ .addkid(p[2])
def p_FuncDefStmt3(self, p): 'FuncDefStmt : FUNC NAME LPAREN RPAREN TypeSpec Block' p[0] = Node("FuncDef") \ .addkid(Node("Name," + p[2])) \ .addkid(Node("ParamDecls")) \ .addkid(Node("ReturnType").addkid(p[5])) \ .addkid(p[6])
def p_FuncDefStmt2(self, p): 'FuncDefStmt : FUNC NAME LPAREN ParamDecls RPAREN Block' p[0] = Node("FuncDef") \ .addkid(Node("Name," + p[2])) \ .addkid(p[4]) \ .addkid(Node("ReturnType", [Node("Type", [Node("TypeName,unit")])])) \ .addkid(p[6])
def p_StmtList_2(self, t): ''' StmtList : Stmt StmtList | Stmt SEMI StmtList ''' stmts = t[len(t)-1] kids = [t[1]] + stmts.children types = dict() for kid in kids: items = types.get(kid.label, list()) items += kid.children types[kid.label] = items children = list() for k,v in types.iteritems(): children.append(Node(k, children=v)) t[0] = Node('Stmts', children=children)
def p_EdgeStmt_1(self, t): ''' EdgeStmt : EdgeReciever EdgeRHS | EdgeReciever EdgeRHS AttrList ''' p = t[1] edges = list() for op, n in t[2]: e = Node(op.label).addkid(p).addkid(n) if len(t) == 4: e.addkid(Node('Attrs', children=t[3])) edges.append(e) p = n t[0] = Node('Edges', children=edges)
def p_EdgeOp_1(self, t): ''' EdgeOp : ARROW | DDASH ''' t[0] = Node(t[1])
def p_AttrStmt_1(self, t): 'AttrStmt : AttrType AttrList' t[0] = Node(t[1]+'Attrs', children=t[2])
def p_GraphStmt_4(self, t): 'GraphStmt : STRICT GraphType ID GraphBody' t[0] = Node('Graph').addkid(t[2].addkid(Node('strict'))).addkid(Node(t[3])).addkid(t[4])
def p_GraphStmt_2(self, t): 'GraphStmt : GraphType ID GraphBody' t[0] = Node('Graph').addkid(t[1]).addkid(Node(t[2])).addkid(t[3])
def p_Graph_2(self, t): 'Graph : COMMENT' t[0] = Node('Comment').addkid(Node(t[1]))
def p_Graphs_1(self, t): 'Graphs : Graph' t[0] = Node('Graphs').addkid(t[1])
def p_SubGraph_2(self, t): 'SubGraph : SUBGRAPH GraphBody' t[0] = Node('SubGraph').addkid(Node(self.next_g_id())).addkid(t[2])
def p_Stmt_6(self, t): 'Stmt : COMMENT' t[0] = Node('Comments').addkid(t[1])
def p_NodeStmt_1(self, t): 'NodeStmt : NodeId' t[0] = Node('Node').addkid(t[1])
def p_SubGraph_1(self, t): 'SubGraph : GraphBody' t[0] = Node('SubGraph').addkid(Node(self.next_g_id())).addkid(t[1])
def p_NodeStmt_2(self, t): 'NodeStmt : NodeId AttrList' t[0] = Node('Node').addkid(t[1]).addkid(Node('Attrs', children=t[2]))
def p_SubGraph_3(self, t): 'SubGraph : SUBGRAPH ID GraphBody' t[0] = Node('SubGraph').addkid(Node(t[2])).addkid(t[3])
def p_NodeId_1(self, t): 'NodeId : ID' t[0] = Node(t[1])
def p_Graphs_3(self, t): 'Graphs : Graphs Graph' t[0] = Node('Graphs', children=t[1].children+[t[2]])
def p_NodeId_2(self, t): 'NodeId : ID Port' t[0] = Node(t[1]).addkid(t[2])
def p_GraphStmt_1(self, t): 'GraphStmt : GraphType GraphBody' t[0] = Node('Graph').addkid(t[1]).addkid(Node(self.next_g_id())).addkid(t[2])
def p_Port_1(self,t): 'Port : COLON ID' t[0] = Node('Port').addkid(Node(t[2]))
def p_GraphStmt_3(self, t): 'GraphStmt : STRICT GraphType GraphBody' t[0] = Node('Graph').addkid(t[2].addkid(Node('strict'))).addkid(Node(self.next_g_id())).addkid(t[3])
def p_Port_2(self,t): 'Port : COLON ID COLON ID' # where the last id is a compass_pt if t[4] not in ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'c', '_']: raise Exception("ID %s is not a compass point" % t[4]) t[0] = Node('Port').addkid(Node(t[2])).addkid(Node(t[4]))
def p_GraphType_1(self, t): ''' GraphType : GRAPH | DIGRAPH ''' t[0] = Node(t[1])
def construct(pynode): name = pynode.__class__.__name__ node = Node(name) for attr_name, attrs in ast.iter_fields(pynode): if attrs is None: continue if hasattr(attrs, '__len__') and len(attrs) == 0: continue attr_node = Node(attr_name) node.addkid(attr_node) if hasattr(attrs, '__iter__'): for attr in attrs: if issubclass(attr.__class__, ast.AST): attr_node.addkid(construct(attr)) else: attr_node.addkid(Node(str(attr))) elif issubclass(attrs.__class__, ast.AST): attr_node.addkid(construct(attrs)) else: if isinstance(attrs, unicode): attrs = attrs.encode('utf8') attr_node.addkid(Node(str(attrs))) return node