alex / rply

An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
BSD 3-Clause "New" or "Revised" License
381 stars 60 forks source link

Print AST for debugging purposes #112

Open Doomsk opened 2 years ago

Doomsk commented 2 years ago

Does anyone know how to get the AST instead of evaluating it directly so one can debug it?

nobodxbodon commented 2 years ago

I wrote a helper method to walk through the AST and print out the summary in a tree-like format like below:

Module(
  body=[Assign(
      targets=[Name(
          id='a'
          ctx=Store()
          lineno=1
          col_offset=1
        )]
      value=Num(
        n=2
        lineno=1
        col_offset=3
      )
      lineno=1
      col_offset=1
    )Expr(
      value=Call(
        func=Name(
          id='print'
          ctx=Load()
          lineno=2
          col_offset=1
        )
        args=[Name(
            id='a'
            ctx=Load()
            lineno=2
            col_offset=7
          )]
        keywords=[]
        lineno=2
        col_offset=1
      )
      lineno=2
      col_offset=1
    )]
)