12345678910111213141516171819202122232425262728293031323334 |
- package main
- import (
- "fmt"
- "go/ast"
- "go/token"
- )
- func parseFile(f *ast.File) {
- loc := f.Name.String()
- for _, d := range f.Decls {
- switch d := d.(type) {
- case *ast.GenDecl:
- parseGenDecl(loc, d)
- case *ast.FuncDecl:
- parseFuncDecl(loc, d)
- default:
- fmt.Printf("Unknown decl type in %s: %T\n", loc, d)
- }
- }
- }
- // type specification: "type [=] <typ>"
- func parseTypeSpec(loc string, s *ast.TypeSpec) {
- // type foo bool: .Name + .Type / .Assign = NoPos (== 0)
- // type foo = bool: .Name + .Type / .Assign = <pos>
- name := s.Name.String()
- if s.Assign == token.NoPos {
- fmt.Printf("%s/Defined %s: ", loc, name)
- } else {
- fmt.Printf("%s/Aliased %s: ", loc, name)
- }
- parseTypeExpr(loc, s.Type, true)
- }
|