生成 Python 的 AST 的文本表示

Generating a text representation of Python's AST

使用 Clang 我们可以做到:

clang -cc1 -ast-dump j.c

TranslationUnitDecl 0x7fbcfc00f608 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x7fbcfc00fea0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x7fbcfc00fba0 '__int128'
|-TypedefDecl 0x7fbcfc00ff08 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x7fbcfc00fbc0 'unsigned __int128'
|-TypedefDecl 0x7fbcfc0101b8 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x7fbcfc00ffd0 'struct __NSConstantString_tag'
|   `-Record 0x7fbcfc00ff58 '__NSConstantString_tag'
|-TypedefDecl 0x7fbcfc010250 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x7fbcfc010210 'char *'
|   `-BuiltinType 0x7fbcfc00f6a0 'char'
|-TypedefDecl 0x7fbcfc0104f8 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x7fbcfc0104a0 'struct __va_list_tag [1]' 1
|   `-RecordType 0x7fbcfc010320 'struct __va_list_tag'
|     `-Record 0x7fbcfc0102a0 '__va_list_tag'
|-FunctionDecl 0x7fbcfb844200 <j.c:3:1, line:12:1> line:3:5 main 'int ()'
| `-CompoundStmt 0x7fbcfb8447b8 <col:12, line:12:1>
|   |-DeclStmt 0x7fbcfb844350 <line:4:3, col:8>
|   | `-VarDecl 0x7fbcfb8442f0 <col:3, col:7> col:7 used e 'int'
....

有没有办法用 Python 的 AST 做到这一点?

我找到了 astdump:https://pypi.org/project/astdump/

但它不打印标记的文字:

>>> import astdump
>>> astdump.indented('2+3')
Module
  Expr
    BinOp
      Num
      Add
      Num

我需要能够从 AST 重构整个代码。

来自 Python 标准库的 astpretty library seems to be suitable for your purpose. This library has a pretty-print function pprint which renders the tree structure of an AST including node types and contents in a readable format. You need to combine this with ast.parse

pprint 的默认行为更详细,包括每个节点的行号和列偏移量,但这可以通过参数 show_offsets=False 禁用。下面的用法示例来自 astpretty 库的自述文件。

>>> astpretty.pprint(ast.parse('x += 5').body[0], show_offsets=False)
AugAssign(
    target=Name(id='x', ctx=Store()),
    op=Add(),
    value=Num(n=5),
)

请注意,如果您不需要漂亮的打印,那么标准库的 ast.dump 将起作用。输出将有点可读性但不太可读,因为它没有缩进以显示树结构:

>>> print(ast.dump(ast.parse('x += 5').body[0]))
AugAssign(target=Name(id='x', ctx=Store()), op=Add(), value=Num(n=5))