Last active
September 7, 2018 07:25
-
-
Save tyru/6ce948f7b88bae6cc29c7b85d7528a8f to your computer and use it in GitHub Desktop.
go/ast walk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node = (*ast.File) &{Doc:<nil> Package:1 Name:main Decls:[0xc42006e2d0 0xc42006e3c0] Scope:scope 0xc420054220 { | |
func f | |
func g | |
} | |
Imports:[] Unresolved:[int] Comments:[]} | |
node = (*ast.Ident) main | |
node = (<nil>) <nil> | |
node = (*ast.FuncDecl) &{Doc:<nil> Recv:<nil> Name:f Type:0xc42000a180 Body:0xc42006e2a0} | |
node = (*ast.Ident) f | |
node = (<nil>) <nil> | |
node = (*ast.FuncType) &{Func:15 Params:0xc42006e210 Results:<nil>} | |
node = (*ast.FieldList) &{Opening:21 List:[] Closing:22} | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (*ast.BlockStmt) &{Lbrace:24 List:[0xc420054270] Rbrace:36} | |
node = (*ast.ExprStmt) &{X:0xc42005e100} | |
node = (*ast.CallExpr) &{Fun:g Lparen:28 Args:[0xc42006e270] Ellipsis:0 Rparen:34} | |
node = (*ast.Ident) g | |
node = (<nil>) <nil> | |
node = (*ast.BinaryExpr) &{X:0xc42000a140 OpPos:31 Op:+ Y:0xc42000a160} | |
node = (*ast.BasicLit) &{ValuePos:29 Kind:INT Value:1} | |
node = (<nil>) <nil> | |
node = (*ast.BasicLit) &{ValuePos:33 Kind:INT Value:1} | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (*ast.FuncDecl) &{Doc:<nil> Recv:<nil> Name:g Type:0xc42000a200 Body:0xc42006e390} | |
node = (*ast.Ident) g | |
node = (<nil>) <nil> | |
node = (*ast.FuncType) &{Func:39 Params:0xc42006e330 Results:<nil>} | |
node = (*ast.FieldList) &{Opening:45 List:[0xc42005e140] Closing:51} | |
node = (*ast.Field) &{Doc:<nil> Names:[n] Type:int Tag:<nil> Comment:<nil>} | |
node = (*ast.Ident) n | |
node = (<nil>) <nil> | |
node = (*ast.Ident) int | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (*ast.BlockStmt) &{Lbrace:53 List:[] Rbrace:54} | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> | |
node = (<nil>) <nil> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func f() { | |
g(1 + 1) | |
} | |
func g(n int) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"go/ast" | |
"go/parser" | |
"go/token" | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Usage: <prog> <file>.go") | |
return | |
} | |
fset := token.NewFileSet() | |
src, err := ioutil.ReadFile(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
node, err := parser.ParseFile(fset, os.Args[1], string(src), 0) | |
if err != nil { | |
panic(err) | |
} | |
ast.Walk(&visitor{}, node) | |
} | |
type visitor struct { | |
indent int | |
} | |
func (v *visitor) Visit(node ast.Node) ast.Visitor { | |
indent := strings.Repeat(" ", v.indent) | |
fmt.Printf("%snode = (%T) %+v\n", indent, node, node) | |
if node == nil { | |
v.indent-- | |
} else { | |
v.indent++ | |
} | |
return v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment