-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
structs.go
160 lines (139 loc) · 3.27 KB
/
structs.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package processor
import (
"bytes"
"sync"
)
const (
T_STRING int = iota + 1
T_SLCOMMENT
T_MLCOMMENT
T_COMPLEXITY
)
type Language struct {
LineComment []string `json:"line_comment"`
ComplexityChecks []string `json:"complexitychecks"`
Extensions []string `json:"extensions"`
ExtensionFile bool `json:"extensionFile"`
MultiLine [][]string `json:"multi_line"`
Quotes [][]string `json:"quotes"`
NestedMultiLine bool `json:"nestedmultiline"`
}
type LanguageFeature struct {
Complexity *Trie
MultiLineComments *Trie
SingleLineComments *Trie
Strings *Trie
Tokens *Trie
Nested bool
ComplexityCheckMask byte
SingleLineCommentMask byte
MultiLineCommentMask byte
StringCheckMask byte
ProcessMask byte
}
// FileJobCallback is an interface that FileJobs can implement to get a per line callback with the line type
type FileJobCallback interface {
// ProcessLine should return true to continue processing or false to stop further processing and return
ProcessLine(job *FileJob, currentLine int64, lineType LineType) bool
}
type FileJob struct {
Language string
Filename string
Extension string
Location string
Content []byte
Bytes int64
Lines int64
Code int64
Comment int64
Blank int64
Complexity int64
WeightedComplexity float64
Hash []byte
Callback FileJobCallback
Binary bool
}
type LanguageSummary struct {
Name string
Bytes int64
Lines int64
Code int64
Comment int64
Blank int64
Complexity int64
Count int64
WeightedComplexity float64
Files []*FileJob
}
type OpenClose struct {
Open []byte
Close []byte
}
type CheckDuplicates struct {
hashes map[int64][][]byte
mux sync.Mutex
}
func (c *CheckDuplicates) Add(key int64, hash []byte) {
c.mux.Lock()
defer c.mux.Unlock()
hashes, ok := c.hashes[key]
if ok {
c.hashes[key] = append(hashes, hash)
} else {
c.hashes[key] = [][]byte{hash}
}
}
func (c *CheckDuplicates) Check(key int64, hash []byte) bool {
c.mux.Lock()
defer c.mux.Unlock()
hashes, ok := c.hashes[key]
if ok {
for _, h := range hashes {
if bytes.Equal(h, hash) {
return true
}
}
}
return false
}
type Trie struct {
Type int
Close []byte
Table [256]*Trie
}
func (root *Trie) Insert(tokenType int, token []byte) {
var node *Trie
node = root
for _, c := range token {
if node.Table[c] == nil {
node.Table[c] = &Trie{}
}
node = node.Table[c]
}
node.Type = tokenType
}
func (root *Trie) InsertClose(tokenType int, openToken, closeToken []byte) {
var node *Trie
node = root
for _, c := range openToken {
if node.Table[c] == nil {
node.Table[c] = &Trie{}
}
node = node.Table[c]
}
node.Type = tokenType
node.Close = closeToken
}
func (root *Trie) Match(token []byte) (int, int, []byte) {
var node *Trie
var depth int
var c byte
node = root
for depth, c = range token {
if node.Table[c] == nil {
return node.Type, depth, node.Close
}
node = node.Table[c]
}
return node.Type, depth, node.Close
}