From 8de17f0adef1d91b2e035e87a21f335903c16a0e Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 25 May 2014 01:13:16 -0400 Subject: [PATCH] Refactored the constant gathering program a bit. --- experiments/windowsconstgen.go | 47 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/experiments/windowsconstgen.go b/experiments/windowsconstgen.go index e09bee7..a1411aa 100644 --- a/experiments/windowsconstgen.go +++ b/experiments/windowsconstgen.go @@ -2,7 +2,7 @@ package main import ( -// "fmt" + "fmt" "os" "strings" "go/token" @@ -10,18 +10,12 @@ import ( "go/parser" ) -func main() { - var pkg *ast.Package - - importpath := os.Args[1] - - fileset := token.NewFileSet() - +func getPackage(path string) (pkg *ast.Package) { + fileset := token.NewFileSet() // parser.ParseDir() actually writes to this; not sure why it doesn't return one instead filter := func(i os.FileInfo) bool { return strings.HasSuffix(i.Name(), "_windows.go") } - pkgs, err := parser.ParseDir(fileset, importpath, - filter, parser.AllErrors) + pkgs, err := parser.ParseDir(fileset, path, filter, parser.AllErrors) if err != nil { panic(err) } @@ -31,15 +25,7 @@ func main() { for k, _ := range pkgs { // get the sole key pkg = pkgs[k] } - - do(pkg) - if len(unknown) > 0 { - s := "error: the following are still unknown!" - for k, _ := range unknown { - s += "\n" + k - } - panic(s) - } + return pkg } type walker struct { @@ -67,7 +53,7 @@ func (w *walker) Visit(node ast.Node) ast.Visitor { return w } -func do(pkg *ast.Package) { +func gatherNames(pkg *ast.Package) { desired := func(name string) bool { if strings.HasPrefix(name, "_") && len(name) > 1 { return !strings.ContainsAny(name, @@ -81,3 +67,24 @@ func do(pkg *ast.Package) { } } } + +func main() { + if len(os.Args) != 2 { + panic("usage: " + os.Args[0] + " path") + } + + pkg := getPackage(os.Args[1]) + gatherNames(pkg) + + if len(unknown) > 0 { + s := "error: the following are still unknown!" + for k, _ := range unknown { + s += "\n" + k + } + panic(s) + } + + for ident, kind := range known { + fmt.Printf("%-30s %s\n", ident, kind) + } +}