I assume that you created AST in some other way than direct code analysis. One simple solution could be
- convert all or parts of AST back to python code line
- manipulate this line
parse() result
to get the new effective AST equivalent with the new correct row numbers and column offsets.
For example, go through the full AST and for each body , convert it to python code, replace all newline characters with a semicolon, parse() result and replace the original body with the result.
Edit: Here is a simple demo using the astunparse module:
from ast import * from astunparse import * a1 = parse("1 == 2\n1 >= 2") print map(lambda x: (x.lineno, x.col_offset), a1.body)
source share