def build_graph(self, graph, tokens):
subgraph = None<EOL>for element in tokens:<EOL><INDENT>cmd = element[<NUM_LIT:0>]<EOL>if cmd == ADD_NODE:<EOL><INDENT>cmd, nodename, opts = element<EOL>graph.add_node(nodename, **opts)<EOL><DEDENT>elif cmd == ADD_EDGE:<EOL><INDENT>cmd, src, dest, opts = element<EOL>srcport = destport = "<STR_LIT>"<EOL>if isinstance(src,tuple):<EOL><INDENT>srcport = src[<NUM_LIT:1>]<EOL>src = src[<NUM_LIT:0>]<EOL><DEDENT>if isinstance(dest,tuple):<EOL><INDENT>destport = dest[<NUM_LIT:1>]<EOL>dest = dest[<NUM_LIT:0>]<EOL><DEDENT>graph.add_edge(src, dest, tailport=srcport, headport=destport,<EOL>**opts)<EOL><DEDENT>elif cmd in [ADD_GRAPH_TO_NODE_EDGE,<EOL>ADD_GRAPH_TO_GRAPH_EDGE,<EOL>ADD_NODE_TO_GRAPH_EDGE]:<EOL><INDENT>cmd, src, dest, opts = element<EOL>srcport = destport = "<STR_LIT>"<EOL>if isinstance(src,tuple):<EOL><INDENT>srcport = src[<NUM_LIT:1>]<EOL><DEDENT>if isinstance(dest,tuple):<EOL><INDENT>destport = dest[<NUM_LIT:1>]<EOL><DEDENT>if not (cmd == ADD_NODE_TO_GRAPH_EDGE):<EOL><INDENT>if cmd == ADD_GRAPH_TO_NODE_EDGE:<EOL><INDENT>src = subgraph<EOL><DEDENT>else:<EOL><INDENT>src = prev_subgraph<EOL>dest = subgraph<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dest = subgraph<EOL><DEDENT>src_is_graph = isinstance(src, (Subgraph, Cluster))<EOL>dst_is_graph = isinstance(dst, (Subgraph, Cluster))<EOL>if src_is_graph:<EOL><INDENT>src_nodes = src.nodes<EOL><DEDENT>else:<EOL><INDENT>src_nodes = [src]<EOL><DEDENT>if dst_is_graph:<EOL><INDENT>dst_nodes = dst.nodes<EOL><DEDENT>else:<EOL><INDENT>dst_nodes = [dst]<EOL><DEDENT>for src_node in src_nodes:<EOL><INDENT>for dst_node in dst_nodes:<EOL><INDENT>graph.add_edge(from_node=src_node, to_node=dst_node,<EOL>tailport=srcport, headport=destport,<EOL>**kwds)<EOL><DEDENT><DEDENT><DEDENT>elif cmd == SET_GRAPH_ATTR:<EOL><INDENT>graph.set( **element[<NUM_LIT:1>] )<EOL><DEDENT>elif cmd == SET_DEF_NODE_ATTR:<EOL><INDENT>graph.default_node.set( **element[<NUM_LIT:1>] )<EOL><DEDENT>elif cmd == SET_DEF_EDGE_ATTR:<EOL><INDENT>graph.default_edge.set( **element[<NUM_LIT:1>] )<EOL><DEDENT>elif cmd == SET_DEF_GRAPH_ATTR:<EOL><INDENT>graph.default_graph.set( **element[<NUM_LIT:1>] )<EOL><DEDENT>elif cmd == ADD_SUBGRAPH:<EOL><INDENT>cmd, name, elements = element<EOL>if subgraph:<EOL><INDENT>prev_subgraph = subgraph<EOL><DEDENT>if name.startswith("<STR_LIT>"):<EOL><INDENT>cluster = Cluster(ID=name)<EOL>cluster = self.build_graph(cluster, elements)<EOL>graph.add_cluster(cluster)<EOL><DEDENT>else:<EOL><INDENT>subgraph = Subgraph(ID=name)<EOL>subgraph = self.build_graph(subgraph, elements)<EOL>graph.add_subgraph(subgraph)<EOL><DEDENT><DEDENT><DEDENT>return graph<EOL>
Builds a Godot graph.