Skip to content

brings the ASTs from quantifiedcode.com in a human readable format

License

Notifications You must be signed in to change notification settings

vogelsgesang/qc-ast-beautifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Yaml AST beautifier

The script astdumper.py contained in this repository pretty prints the AST trees used by Quantified Code. They are representing the AST as a YAML documents. But unfortunately the map elements within this YAML are unsorted which makes it hard to read the syntax trees.

How to use

First install the following requirements:

  • Python 3
  • pyyaml (pip install pyyaml)

Run the script astdumper.py. It accepts the input on stdin. Alternatively you might specify a file name as first parameter.

Integration with VIM

If you use VIM as your text editor, you can use the piping functionality using the following key sequence:

:%!astdumper.py

(assuming that astdumper.py is in your PATH environment variable)

Usage with xsel

In order to pretty print an AST which is saved in the clipboard buffer, you might use the following bash command:

xsel -b | astdumper.py

Example

The AST for

def greet(who):
  print("Hello " + who + "!")

greet("World")

generated by QuantifiedCode is

- args:
    args:
      - ctx:
          node_type: param
        id: who
        node_type: name
    defaults: []
    kwarg: null
    node_type: arguments
    vararg: null
  body:
    - dest: null
      nl: true
      node_type: print
      values:
        - left:
            left:
              node_type: str
              s: 'Hello '
            node_type: binop
            op:
              node_type: add
            right:
              ctx:
                node_type: load
              id: who
              node_type: name
          node_type: binop
          op:
            node_type: add
          right:
            node_type: str
            s: '!'
  decorator_list: []
  docstring: null
  name: greet
  node_type: functiondef
- node_type: expr
  value:
    args:
      - node_type: str
        s: World
    func:
      ctx:
        node_type: load
      id: greet
      node_type: name
    keywords: []
    kwargs: null
    node_type: call
    starargs: null

This tool transforms the AST into:

- node_type: functiondef
  name: greet
  args:
    node_type: arguments
    args:
    - node_type: name
      id: who
      ctx: {node_type: param}
    defaults: []
    kwarg: null
    vararg: null
  decorator_list: []
  docstring: null
  body:
  - node_type: print
    dest: null
    nl: true
    values:
    - node_type: binop
      left:
        node_type: binop
        left: {node_type: str, s: 'Hello '}
        op: {node_type: add}
        right:
          node_type: name
          id: who
          ctx: {node_type: load}
      op: {node_type: add}
      right: {node_type: str, s: '!'}
- node_type: expr
  value:
    node_type: call
    func:
      node_type: name
      id: greet
      ctx: {node_type: load}
    args:
    - {node_type: str, s: World}
    starargs: null
    keywords: []
    kwargs: null

Both YAML documents are semantically equivalent but I consider the second one much more suitable for human beings.

About

brings the ASTs from quantifiedcode.com in a human readable format

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages