parse.go 722 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "go/token"
  6. )
  7. func parseFile(f *ast.File) {
  8. loc := f.Name.String()
  9. for _, d := range f.Decls {
  10. switch d := d.(type) {
  11. case *ast.GenDecl:
  12. parseGenDecl(loc, d)
  13. case *ast.FuncDecl:
  14. parseFuncDecl(loc, d)
  15. default:
  16. fmt.Printf("Unknown decl type in %s: %T\n", loc, d)
  17. }
  18. }
  19. }
  20. // type specification: "type [=] <typ>"
  21. func parseTypeSpec(loc string, s *ast.TypeSpec) {
  22. // type foo bool: .Name + .Type / .Assign = NoPos (== 0)
  23. // type foo = bool: .Name + .Type / .Assign = <pos>
  24. name := s.Name.String()
  25. if s.Assign == token.NoPos {
  26. fmt.Printf("%s/Defined %s: ", loc, name)
  27. } else {
  28. fmt.Printf("%s/Aliased %s: ", loc, name)
  29. }
  30. parseTypeExpr(loc, s.Type, true)
  31. }