File: decl.go

package info (click to toggle)
goda 0.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: makefile: 9
file content (51 lines) | stat: -rw-r--r-- 783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package stat

import (
	"go/ast"
	"go/token"
)

// Decls stats about top-level declarations.
type Decls struct {
	Func  int64
	Type  int64
	Const int64
	Var   int64
	Other int64
}

func (s *Decls) Add(b Decls) {
	s.Func += b.Func
	s.Type += b.Type
	s.Const += b.Const
	s.Var += b.Var
	s.Other += b.Other
}

func (s *Decls) Total() int64 {
	return s.Func + s.Type + s.Const + s.Var + s.Other
}

func DeclsFromAst(f *ast.File) Decls {
	stat := Decls{}
	for _, decl := range f.Decls {
		switch decl := decl.(type) {
		case *ast.GenDecl:
			switch decl.Tok {
			case token.TYPE:
				stat.Type++
			case token.VAR:
				stat.Var++
			case token.CONST:
				stat.Const++
			default:
				stat.Other++
			}
		case *ast.FuncDecl:
			stat.Func++
		default:
			stat.Other++
		}
	}
	return stat
}