I would like to be able to convert python code (a string) into a parse tree, modify it at the tree level, then convert the tree into code (a string). When converting to a parse tree and back into code without any tree-level modification, the resulting code should match the original input code exactly.
I would like to use python for this. I found the ast and parser python modules, however ast trees lose information about the original code. As for the parser module, I can't seem to figure out how to manipulate the parse tree or convert it into code.
Here's what I have so far.
import ast
import astor # pip install astor
import parser
code = 'hi = 0'
ast_tree = ast.parse(code)
code_from_ast = astor.to_source(tree) # 'hi = 0\n'
parser_tree = parser.suite(code)
code_from_parser = ???