-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
201 lines (157 loc) · 4.3 KB
/
analyzer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package sonic
import (
"context"
"crypto/sha1"
"fmt"
"github.com/mloncode/sonic/src/sound"
"github.com/rakyll/portmidi"
"gopkg.in/src-d/lookout-sdk.v0/pb"
"github.com/src-d/lookout"
"gopkg.in/bblfsh/client-go.v2/tools"
"gopkg.in/bblfsh/sdk.v1/uast"
log "gopkg.in/src-d/go-log.v1"
)
type Analyzer struct {
DataClient *lookout.DataClient
DeviceID portmidi.DeviceID
}
var _ lookout.AnalyzerServer = &Analyzer{}
var m1 = sound.NewMarkov("song1.midi")
var m2 = sound.NewMarkov("song2.midi")
func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (*pb.EventResponse, error) {
changes, err := a.DataClient.GetChanges(ctx, &lookout.ChangesRequest{
Head: &e.Head,
Base: &e.Base,
WantContents: true,
WantLanguage: true,
WantUAST: true,
ExcludeVendored: true,
})
if err != nil {
log.Errorf(err, "failed to GetChanges from a DataService")
return nil, err
}
var total int
for changes.Next() {
log.Infof("got change")
change := changes.Change()
var baseNodes []sonicNode
var headNodes []sonicNode
if change.Base != nil && change.Base.UAST != nil {
baseNodes = toSonicNodes(change.Base)
}
if change.Head != nil && change.Head.UAST != nil {
headNodes = toSonicNodes(change.Head)
}
deleted, added, changed := diffNodes(baseNodes, headNodes)
printNodes("deleted:", deleted)
printNodes("added:", added)
printNodes("changed:", changed)
deletedSeq := sound.NewSequence("prophet", ConvertMarkov(m2, deleted))
deletedSeq.Play(a.DeviceID)
addedSeq := sound.NewSequence("prophet", ConvertMarkov(m2, added))
addedSeq.Play(a.DeviceID)
total += len(deleted) + len(added) + len(changed)
}
fmt.Println("total nodes:", total)
if changes.Err() != nil {
log.Errorf(changes.Err(), "failed to get a file from DataServer")
}
return &pb.EventResponse{}, nil
}
type sonicNode struct {
Type string
Token string
Lenght uint32
Hash [20]byte
}
func (n *sonicNode) Key() string {
return fmt.Sprintf("%s%s", n.Type, n.Token)
}
func printNodes(header string, nodes []sonicNode) {
fmt.Println(header)
for _, n := range nodes {
fmt.Println(n.Type, n.Token, n.Lenght)
}
}
var uastQuery = "//*[(@roleDeclaration or @roleIdentifier or @roleLiteral) and @startOffset and @endOffset]"
func toSonicNodes(file *lookout.File) []sonicNode {
nodes, err := tools.Filter(file.UAST, uastQuery)
if err != nil {
return nil
}
var result []sonicNode
for _, node := range nodes {
length := node.EndPosition.Offset - node.StartPosition.Offset
if length == 0 {
continue
}
token := getFirstToken(node)
if token == "" {
continue
}
content := file.Content[node.StartPosition.Offset:node.EndPosition.Offset]
result = append(result, sonicNode{
Type: node.InternalType,
Token: token,
Lenght: length,
Hash: sha1.Sum(content),
})
}
return result
}
func getFirstToken(node *uast.Node) string {
if node.Token != "" {
return node.Token
}
var n *uast.Node
nodesToVisit := []*uast.Node{node}
for len(nodesToVisit) > 0 {
n, nodesToVisit = nodesToVisit[0], nodesToVisit[1:]
for i := 0; i < len(n.Children); i++ {
nodesToVisit = append(nodesToVisit, n.Children...)
}
if hasRole(n.Roles, uast.Name) && n.Token != "" {
return n.Token
}
}
return ""
}
func hasRole(roles []uast.Role, role uast.Role) bool {
for _, r := range roles {
if r == role {
return true
}
}
return false
}
func diffNodes(oldNodes, newNodes []sonicNode) ([]sonicNode, []sonicNode, []sonicNode) {
oldMap := make(map[string]sonicNode, len(oldNodes))
for _, n := range oldNodes {
oldMap[n.Key()] = n
}
newMap := make(map[string]sonicNode, len(newNodes))
for _, n := range newNodes {
newMap[n.Key()] = n
}
var deletedNodes []sonicNode
for key, n := range oldMap {
if _, ok := newMap[key]; !ok {
deletedNodes = append(deletedNodes, n)
}
}
var addNodes []sonicNode
var modifiedNodes []sonicNode
for key, n := range newMap {
if oldN, ok := oldMap[key]; !ok {
addNodes = append(addNodes, n)
} else if oldN.Hash != n.Hash {
modifiedNodes = append(modifiedNodes, n)
}
}
return deletedNodes, addNodes, modifiedNodes
}
// we don't need code below but need to satisfy interface
func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *pb.PushEvent) (*pb.EventResponse, error) {
return &pb.EventResponse{}, nil
}