print the AST object
OrVazana opened this issue · 1 comments
OrVazana commented
hey,
I want to know if there is a way to get a printable output of the Ast.
For example for the hello World code example:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
the json output should be like this : ( I tested it on this site https://astexplorer.net/ ):
Json output :
{
"Comments": [],
"Decls": [
{
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 26,
"Line": 3,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 14,
"Line": 3,
"Column": 1
}
},
"Lparen": 0,
"Rparen": 0,
"Specs": [
{
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 26,
"Line": 3,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 21,
"Line": 3,
"Column": 8
}
},
"Path": {
"Kind": "STRING",
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 26,
"Line": 3,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 21,
"Line": 3,
"Column": 8
}
},
"Value": "\"fmt\"",
"_type": "BasicLit"
},
"_type": "ImportSpec"
}
],
"Tok": "import",
"_type": "GenDecl"
},
{
"Body": {
"Lbrace": 41,
"List": [
{
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 74,
"Line": 6,
"Column": 33
},
"Start": {
"Filename": "main.go",
"Offset": 46,
"Line": 6,
"Column": 5
}
},
"X": {
"Args": [
{
"Kind": "STRING",
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 73,
"Line": 6,
"Column": 32
},
"Start": {
"Filename": "main.go",
"Offset": 58,
"Line": 6,
"Column": 17
}
},
"Value": "\"Hello, World!\"",
"_type": "BasicLit"
}
],
"Ellipsis": 0,
"Fun": {
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 57,
"Line": 6,
"Column": 16
},
"Start": {
"Filename": "main.go",
"Offset": 46,
"Line": 6,
"Column": 5
}
},
"Sel": {
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 57,
"Line": 6,
"Column": 16
},
"Start": {
"Filename": "main.go",
"Offset": 50,
"Line": 6,
"Column": 9
}
},
"Name": "Println",
"_type": "Ident"
},
"X": {
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 49,
"Line": 6,
"Column": 8
},
"Start": {
"Filename": "main.go",
"Offset": 46,
"Line": 6,
"Column": 5
}
},
"Name": "fmt",
"_type": "Ident"
},
"_type": "SelectorExpr"
},
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 74,
"Line": 6,
"Column": 33
},
"Start": {
"Filename": "main.go",
"Offset": 46,
"Line": 6,
"Column": 5
}
},
"Lparen": 58,
"Rparen": 74,
"_type": "CallExpr"
},
"_type": "ExprStmt"
}
],
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 76,
"Line": 7,
"Column": 2
},
"Start": {
"Filename": "main.go",
"Offset": 40,
"Line": 5,
"Column": 13
}
},
"Rbrace": 76,
"_type": "BlockStmt"
},
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 76,
"Line": 7,
"Column": 2
},
"Start": {
"Filename": "main.go",
"Offset": 28,
"Line": 5,
"Column": 1
}
},
"Name": {
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 37,
"Line": 5,
"Column": 10
},
"Start": {
"Filename": "main.go",
"Offset": 33,
"Line": 5,
"Column": 6
}
},
"Name": "main",
"_type": "Ident"
},
"Type": {
"Func": 29,
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 39,
"Line": 5,
"Column": 12
},
"Start": {
"Filename": "main.go",
"Offset": 28,
"Line": 5,
"Column": 1
}
},
"Params": {
"Closing": 39,
"List": [],
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 39,
"Line": 5,
"Column": 12
},
"Start": {
"Filename": "main.go",
"Offset": 37,
"Line": 5,
"Column": 10
}
},
"Opening": 38,
"_type": "FieldList"
},
"_type": "FuncType"
},
"_type": "FuncDecl"
}
],
"Imports": [
{
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 26,
"Line": 3,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 21,
"Line": 3,
"Column": 8
}
},
"Path": {
"Kind": "STRING",
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 26,
"Line": 3,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 21,
"Line": 3,
"Column": 8
}
},
"Value": "\"fmt\"",
"_type": "BasicLit"
},
"_type": "ImportSpec"
}
],
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 76,
"Line": 7,
"Column": 2
},
"Start": {
"Filename": "main.go",
"Offset": 0,
"Line": 1,
"Column": 1
}
},
"Name": {
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 12,
"Line": 1,
"Column": 13
},
"Start": {
"Filename": "main.go",
"Offset": 8,
"Line": 1,
"Column": 9
}
},
"Name": "main",
"_type": "Ident"
},
"Package": 1,
"Unresolved": [
{
"Loc": {
"End": {
"Filename": "main.go",
"Offset": 49,
"Line": 6,
"Column": 8
},
"Start": {
"Filename": "main.go",
"Offset": 46,
"Line": 6,
"Column": 5
}
},
"Name": "fmt",
"_type": "Ident"
}
],
"_type": "File"
}
But all I could get was:
<gopygo.ast.File object at 0x7fb90cd8f970>
So the question is is there any way to print the AST from the gopygo.ast.File
object?
Thank you
OrVazana commented
Hey, so I found a way to print all the AST
import gopygo
import jsonpickle
import json
program=open("./helloWorld.go").read()
tree=gopygo.parse(program)
serialized = jsonpickle.encode(tree)
print(json.dumps(json.loads(serialized), indent=4))
and the json output is:
Json output
{
"py/object": "gopygo.ast.File",
"name": {
"py/object": "gopygo.ast.Package",
"name": "main"
},
"imports": [],
"decls": [
{
"py/object": "gopygo.ast.GenDecl",
"tok": "import",
"specs": [
{
"py/object": "gopygo.ast.ImportSpec",
"name": null,
"path": {
"py/object": "gopygo.ast.BasicLit",
"kind": {
"py/reduce": [
{
"py/type": "gopygo.enums.Token"
},
{
"py/tuple": [
9
]
}
]
},
"value": "fmt"
}
}
]
},
{
"py/object": "gopygo.ast.FuncDecl",
"name": "main",
"type": {
"py/object": "gopygo.ast.FuncType",
"params": {
"py/object": "gopygo.ast.FieldList",
"list": []
},
"results": {
"py/object": "gopygo.ast.FieldList",
"list": []
}
},
"body": {
"py/object": "gopygo.ast.BlockStmt",
"list": [
{
"py/object": "gopygo.ast.ExprStmt",
"expr": {
"py/object": "gopygo.ast.CallExpr",
"fun": {
"py/object": "gopygo.ast.SelectorExpr",
"x": "fmt",
"sel": "Println"
},
"args": [
{
"py/object": "gopygo.ast.BasicLit",
"kind": {
"py/id": 8
},
"value": "Hello, World!"
}
],
"ellipsis": false
}
}
]
},
"recv": null
}
]
}
Thank you