func main() { flag.Parse() fset := token.NewFileSet() f, err := parser.ParseFile(fset, "stdin.go", os.Stdin, 0) if err != nil { log.Fatal(err) } a := gotranslate.NewAST(fset) ast.Walk(a, f) var b []byte if *pretty { b, err = json.MarshalIndent(a.RootBlock, "", " ") } else { b, err = json.Marshal(a.RootBlock) } if err != nil { log.Fatalf("Could not generate json: %v", err) } if _, err = os.Stdout.Write(b); err != nil { log.Fatalf("Could not output json: %v", err) } fmt.Println() }
func encodeFromGo(r io.Reader, w io.Writer) error { fset := token.NewFileSet() f, err := parser.ParseFile(fset, "in.go", r, 0) if err != nil { return err } a := gotranslate.NewAST(fset) ast.Walk(a, f) return json.NewEncoder(w).Encode(a.RootBlock) }