def transforming_parser(code): assert type(code) is str print_stmt_tree = parser.ast2tuple(parser.suite(code), line_info=1) print_func_tree = convert_print_statments(print_stmt_tree) return transformer.Transformer().transform(print_func_tree)
def transform(self, tree): """Transform an AST into a modified parse tree.""" if not (isinstance(tree, tuple) or isinstance(tree, list)): tree = parser.ast2tuple(tree, line_info=1) return self.compile_node(tree)
yield tree if type(tree[1]) is tuple: for child in tree[1:]: for result in _find_nodes(child, node_type): yield result def get1(items): items = list(items) assert len(items) == 1, items return items[0] #TODO: Fix line_info in template EXPR_TEMPLATE = get1(_find_nodes(parser.ast2tuple(parser.suite("""\ __repace_me__(__replace_me_too__) """), line_info=1), symbol.expr_stmt)) def convert_print_statments(parsed, name="__print__", file="__print_file__", comma="__print_comma__", file_comma="__print_file_comma__"): func_name = name if type(parsed[1]) is tuple: if parsed[0] == symbol.print_stmt: names = {(False, False): name, (True, False): file, (False, True): comma, (True, True): file_comma} if len(parsed) > 2 and parsed[2][0] == token.RIGHTSHIFT: