File: token.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 (39 lines) | stat: -rw-r--r-- 543 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
package stat

import "go/ast"

type Tokens struct {
	Code    int64
	Comment int64
	Basic   int64
}

func (stat *Tokens) Add(b Tokens) {
	stat.Code += b.Code
	stat.Comment += b.Comment
	stat.Basic += b.Basic
}

func TokensFromAst(f *ast.File) Tokens {
	stat := Tokens{}

	ast.Inspect(f, func(n ast.Node) bool {
		if n == nil {
			return true
		}

		switch n.(type) {
		default:
			stat.Code++
		case *ast.BasicLit:
			stat.Basic++
		case *ast.CommentGroup, *ast.Comment:
			stat.Comment++
			return false
		}

		return true
	})

	return stat
}