-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtokens.go
72 lines (64 loc) · 1.27 KB
/
tokens.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"go/scanner"
"go/token"
"io/ioutil"
"os"
)
type TokenSet map[string]struct{}
// GoTokens gets the tokens from set of source files.
func GoTokens(srcpaths []string) (toks TokenSet, err error) {
tokc := make(chan []string)
errc := make(chan error)
fs := token.NewFileSet()
files := 0
for _, path := range srcpaths {
st, serr := os.Stat(path)
if serr != nil {
break
}
f := fs.AddFile(path, fs.Base(), int(st.Size()))
go func(tf *token.File) {
s, e := fileTokens(tf)
tokc <- s
errc <- e
}(f)
files++
}
toks = make(TokenSet)
for i := 0; i < files; i++ {
if curToks := <-tokc; curToks != nil {
for _, tok := range curToks {
toks[tok] = struct{}{}
}
}
if curErr := <-errc; curErr != nil {
err = curErr
}
}
return toks, err
}
func fileTokens(tf *token.File) (toks []string, err error) {
src, err := ioutil.ReadFile(tf.Name())
if err != nil {
return nil, err
}
s := &scanner.Scanner{}
s.Init(tf, src, nil, 0)
tokmap := make(TokenSet)
for {
_, tok, lit := s.Scan()
if tok == token.EOF {
break
}
if tok == token.STRING {
// XXX: what if strings are misspelled?
lit = lit[1 : len(lit)-1]
}
tokmap[lit] = struct{}{}
}
for k, _ := range tokmap {
toks = append(toks, k)
}
return toks, nil
}