示例#1
0
文件: viz.py 项目: irskep/T-Reg
def graphify(roots, f, name="AST", top_level=[]):
    """
    Convert a tree of nodes following the L{Node} interface into a graphviz document
    
    @param root: Root node
    @param path: Path to the output file
    @param name: Name of the graph (optional)
    """
    if not isinstance(roots, list):
        roots = [roots]
    
    f.write(header_string % name)
    
    node_set = set()
    node_count = 0
    for root in roots:
        node_count, node_set = tag_and_rank(root, 0, node_count, node_set)
    
    f.write(subgraph_prefix)
    for node in top_level:
        if hasattr(node,"graph_color"):
            f.write(node_string_color % (node.graph_id, graphviz_escape(str(node)),
                    node.graph_color))
        else:
            f.write(node_string_nocolor % (node.graph_id, graphviz_escape(str(node))))
    f.write(subgraph_postfix)
    
    for node in node_set:
        if node not in top_level:
            if hasattr(node,"graph_color"):
                f.write(node_string_color % (node.graph_id, graphviz_escape(str(node)),
                        node.graph_color))
            else:
                f.write(node_string_nocolor % (node.graph_id, graphviz_escape(str(node))))
    
    for node in node_set:
        for parent in node.parents:
            if parent.point_to(node):
                f.write(edge_string % (parent.graph_id, node.graph_id))
            else:
                f.write(edge_color_string % (parent.graph_id, node.graph_id))
    
    for a, b in list(moreitertools.all_adjacent(top_level)):
        f.write(invisible_edge_string % (a.graph_id, b.graph_id))
    
    f.write(footer_string)
示例#2
0
 def _connect_chunks(self):
     for a, b in moreitertools.all_adjacent(self.chunks):
         a.next = b
         b.prev = a
示例#3
0
文件: node.py 项目: irskep/T-Reg
import retree
import retools
import moreitertools

special_matches = [(_, re.compile(r)) for _, r in [
    (r'\s', r'^\s+$'),
    (r'\d', r'^\d+$'),
    (r'\w', r'^\w+$'),
    # (r'(\w+\s*)', r'^(\w|\s)+$'),
    # (r'(\s*\w+)', r'^(\s|\w)+$'),
    (r'\D', r'^\D+$'),
    (None,  r'^.+$'),
]]

special_succession = {a[0]: b for a, b in moreitertools.all_adjacent(special_matches)}
special_succession[None] = (None, re.compile(r'^.+$'))

def special_for_char_set(char_set):
    for special, regex in special_matches:
        if regex.match(char_set):
            return special

def next_special(s, char_set):
    while s:
        s, regex = special_succession[s]
        if regex.match(char_set):
            return s
    return None

class Node(object):