Refactored the constant gathering program a bit.

This commit is contained in:
Pietro Gagliardi 2014-05-25 01:13:16 -04:00
parent b27caae7c3
commit 8de17f0ade
1 changed files with 27 additions and 20 deletions

View File

@ -2,7 +2,7 @@
package main package main
import ( import (
// "fmt" "fmt"
"os" "os"
"strings" "strings"
"go/token" "go/token"
@ -10,18 +10,12 @@ import (
"go/parser" "go/parser"
) )
func main() { func getPackage(path string) (pkg *ast.Package) {
var pkg *ast.Package fileset := token.NewFileSet() // parser.ParseDir() actually writes to this; not sure why it doesn't return one instead
importpath := os.Args[1]
fileset := token.NewFileSet()
filter := func(i os.FileInfo) bool { filter := func(i os.FileInfo) bool {
return strings.HasSuffix(i.Name(), "_windows.go") return strings.HasSuffix(i.Name(), "_windows.go")
} }
pkgs, err := parser.ParseDir(fileset, importpath, pkgs, err := parser.ParseDir(fileset, path, filter, parser.AllErrors)
filter, parser.AllErrors)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -31,15 +25,7 @@ func main() {
for k, _ := range pkgs { // get the sole key for k, _ := range pkgs { // get the sole key
pkg = pkgs[k] pkg = pkgs[k]
} }
return pkg
do(pkg)
if len(unknown) > 0 {
s := "error: the following are still unknown!"
for k, _ := range unknown {
s += "\n" + k
}
panic(s)
}
} }
type walker struct { type walker struct {
@ -67,7 +53,7 @@ func (w *walker) Visit(node ast.Node) ast.Visitor {
return w return w
} }
func do(pkg *ast.Package) { func gatherNames(pkg *ast.Package) {
desired := func(name string) bool { desired := func(name string) bool {
if strings.HasPrefix(name, "_") && len(name) > 1 { if strings.HasPrefix(name, "_") && len(name) > 1 {
return !strings.ContainsAny(name, 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)
}
}