def generate_lib2to3_ast(code): from lib2to3.pgen2.driver import Driver from lib2to3.pgen2 import token as pgen2_token from lib2to3.pygram import python_symbols, python_grammar from lib2to3 import pytree from io import StringIO token_types = list(python_symbols.__dict__.items()) token_types += list(pgen2_token.__dict__.items()) def transform_ast(ast): transformed = { "node_type": next(n for n, t in token_types if t == ast.type) } if ast.children: transformed["children"] = [ transform_ast(child) for child in ast.children ] if isinstance(ast, pytree.Leaf): if ast.value != "": transformed["value"] = ast.value if ast._prefix != "": transformed["prefix"] = ast._prefix return transformed driver = Driver(python_grammar, convert=pytree.convert) return transform_ast(driver.parse_stream(StringIO(code)))
def generate_lib2to3_ast(code): from lib2to3.pgen2.driver import Driver from lib2to3.pgen2 import token as pgen2_token from lib2to3.pygram import python_symbols, python_grammar from lib2to3 import pytree from io import StringIO token_types = list(python_symbols.__dict__.items()) token_types += list(pgen2_token.__dict__.items()) def transform_ast(ast): transformed = {"node_type": next(n for n, t in token_types if t == ast.type)} if ast.children: transformed["children"] = [transform_ast(child) for child in ast.children] if isinstance(ast, pytree.Leaf): if ast.value != "": transformed["value"] = ast.value if ast._prefix != "": transformed["prefix"] = ast._prefix return transformed driver = Driver(python_grammar, convert=pytree.convert) return transform_ast(driver.parse_stream(StringIO(code)))