Skip to content

up9inc/gopygo

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

gopygo

Pure Python Go parser, AST and unparser library

Installation

$ pip3 install gopygo

Usage

Parse and unparse the Go Hello, World! example:

>>> import gopygo
>>> program = """
... package main
... 
... import "fmt"
... 
... func main() {
...     fmt.Println("Hello, World!")
... }
... """
>>> program = program.lstrip()
>>> tree = gopygo.parse(program)
>>> tree
<gopygo.ast.File object at 0x7f0b5dddb6d0>
>>> tree.__dict__
{'name': <gopygo.ast.Package object at 0x7f0b5dddb0a0>, 'imports': [<gopygo.ast.ImportSpec object at 0x7f0b5dddb190>], 'decls': [<gopygo.ast.FuncDecl object at 0x7f0b5dddb520>]}
>>> text = gopygo.unparse(tree)
>>> print(text)
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

>>> assert program == text

Roadmap

Implement the AST nodes specified in here and the parser, unparser libraries accordingly.

Tokens are defined in here.

Development

Here is an example access Go AST using original go/parser and go/ast packages:

package main

import "fmt"
import "go/token"
import "go/parser"
import "go/ast"

func main() {
	src := `
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
	`
	x, err := parser.ParseFile(token.NewFileSet(), "", src, 0)
	fmt.Println(x.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Args[0].(*ast.BasicLit).Value)
	fmt.Println(err)
	fmt.Printf("type: %T\n", x.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Args[0].(*ast.BasicLit).Value)
}

Gives this output:

"Hello, World!"
<nil>
type: string

About

Pure Python Go parser, AST and unparser library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages