Created
May 26, 2014 16:48
-
-
Save tpott/c2ed972a6437da66465e to your computer and use it in GitHub Desktop.
simple go parser
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
// self_parsing.go | |
// Mon May 26 09:47:10 PDT 2014 | |
package main | |
import ( | |
"os" | |
"fmt" | |
"reflect" | |
"go/ast" | |
"go/parser" | |
"go/token" | |
) | |
type MyVisitor struct { | |
} | |
func (v *MyVisitor) Visit(n ast.Node) (w ast.Visitor) { | |
if n != nil { | |
fmt.Println(reflect.TypeOf(n), n.Pos(), n.End()) | |
} | |
return v | |
} | |
func main() { | |
var filename = os.Args[1] | |
fmt.Println("filename=", filename) | |
tree, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0) | |
if err != nil { | |
fmt.Println("err! ", err) | |
os.Exit(1) | |
} | |
ast.Walk(new(MyVisitor), tree) | |
} |
Author
tpott
commented
May 26, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment