示例#1
0
    def show_with_dot(self):
        from ctree.visual.dot_manager import DotManager
        # start_node = self.currentItem().ast_node
        #
        # def find_appropriate_node(node):
        #     if not node:
        #         return None
        #     if isinstance(node, ast.AST):
        #         return node
        #     if hasattr(node, 'parent'):
        #         return find_appropriate_node(node.parent)
        #     return None
        #
        # start_node = find_appropriate_node(start_node)
        start_item = self.currentItem()

        def find_appropriate_node(item):
            if not item:
                return None
            if hasattr(item, 'ast_node') and isinstance(item.ast_node, ast.AST):
                return item.ast_node
            if hasattr(item, 'parent'):
                return find_appropriate_node(item.parent())
            print("bad node %s" % item)
            import pprint
            pprint(dir(item))
            return None

        start_node = find_appropriate_node(start_item)
        if not start_node:
            self.code_presenter.show_error("Sorry, cannot find an ast node to begin graph")
            return

        file_name = os.path.join(tempfile.gettempdir(), 'tree_%s.png' % self.tab_name)
        DotManager.dot_ast_to_browser(start_node, file_name)
示例#2
0
def browser_show_ast(tree, file_name):
    """
    convenience method to display an AST in ipython
    converts tree in place to a dot format
    then renders that into a png file
    """
    return DotManager.dot_ast_to_browser(tree, file_name)
示例#3
0
def browser_show_ast(tree, file_name):
    """
    convenience method to display an AST in ipython
    converts tree in place to a dot format
    then renders that into a png file
    """
    return DotManager.dot_ast_to_browser(tree, file_name)
示例#4
0
def ipython_show_ast(tree):
    """
    convenience method to display an AST in ipython
    converts tree in place to a dot format
    then renders that into a png file
    """
    import ctree.dotgen
    return DotManager.dot_ast_to_image(tree)
示例#5
0
文件: __init__.py 项目: i-Zaak/ctree
def ipython_show_ast(tree):
    """
    convenience method to display an AST in ipython
    converts tree in place to a dot format
    then renders that into a png file
    """
    import ctree.dotgen
    return DotManager.dot_ast_to_image(tree)
示例#6
0
 def ast_command(self, command):
     fields = command.split()
     if len(fields) < 2 or fields[1].startswith("list"):
         self.show_asts()
     elif fields[1].startswith("del"):
         if len(fields) > 2 and self.delete_ast(fields[2]):
             pass
         else:
             print("error: delete command must specify index of tree to delete")
     elif fields[1].startswith("cle"):
         self.controller.ast_tree_manager.clear()
     elif fields[1].startswith("sho"):
         try:
             index = int(fields[2])
             from ctree.visual.dot_manager import DotManager
             print("calling open")
             DotManager.dot_ast_to_browser(self.controller.ast_tree_manager[index].ast_tree, "tree%d.png" % index)
         except IOError as e:
             print("ast show requires numeric index of ast, msg %s" % e.message)
     else:
         print("unknown ast command")
示例#7
0
    def show_with_dot(self):
        from ctree.visual.dot_manager import DotManager
        # start_node = self.currentItem().ast_node
        #
        # def find_appropriate_node(node):
        #     if not node:
        #         return None
        #     if isinstance(node, ast.AST):
        #         return node
        #     if hasattr(node, 'parent'):
        #         return find_appropriate_node(node.parent)
        #     return None
        #
        # start_node = find_appropriate_node(start_node)
        start_item = self.currentItem()

        def find_appropriate_node(item):
            if not item:
                return None
            if hasattr(item, 'ast_node') and isinstance(
                    item.ast_node, ast.AST):
                return item.ast_node
            if hasattr(item, 'parent'):
                return find_appropriate_node(item.parent())
            print("bad node %s" % item)
            import pprint
            pprint(dir(item))
            return None

        start_node = find_appropriate_node(start_item)
        if not start_node:
            self.code_presenter.show_error(
                "Sorry, cannot find an ast node to begin graph")
            return

        file_name = os.path.join(tempfile.gettempdir(),
                                 'tree_%s.png' % self.tab_name)
        DotManager.dot_ast_to_browser(start_node, file_name)
示例#8
0
 def test_c_identity(self):
     tree = get_ast(square_of)
     DotManager.run_dot(tree.to_dot())
示例#9
0
 def test_c_identity(self):
     tree = get_ast(square_of)
     DotManager.run_dot(tree.to_dot())
示例#10
0
__author__ = "Polyarnyi Nickolay"

from recursive_parsing.grammars import pascal_vars_grammar
from recursive_parsing.grammars.grammar import Grammar
from recursive_parsing.lexical_analyzers.pascal_lexical_analyzer import PascalLexicalAnalyzer
from recursive_parsing.syntax_analyzer import syntax_analyze

from IPython.display import Image
from ctree.visual.dot_manager import DotManager

rules = pascal_vars_grammar.rules
terminals = pascal_vars_grammar.terminals
grammar = Grammar(pascal_vars_grammar.start, rules, terminals)

string = '''
var
    a, b: integer;
'''

tree = syntax_analyze(string, PascalLexicalAnalyzer(), grammar)

labels = tree._to_text_labels()
connections = tree._to_text_connections()
text = 'digraph mytree {' + labels + connections + '}'

Image(DotManager.run_dot(text), embed=True)