-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathuast_utils.go
189 lines (152 loc) · 3.64 KB
/
uast_utils.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
package function
import (
"bytes"
"fmt"
"hash"
"hash/crc64"
"github.com/bblfsh/go-client/v4/tools"
"github.com/bblfsh/sdk/v3/uast/nodes/nodesproto"
bblfsh "github.com/bblfsh/go-client/v4"
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/sirupsen/logrus"
"github.com/src-d/gitbase"
"github.com/src-d/go-mysql-server/sql"
errors "gopkg.in/src-d/go-errors.v1"
)
var (
// ErrParseBlob is returned when the blob can't be parsed with bblfsh.
ErrParseBlob = errors.NewKind("unable to parse the given blob using bblfsh: %s")
// ErrUnmarshalUAST is returned when an error arises unmarshaling UASTs.
ErrUnmarshalUAST = errors.NewKind("error unmarshaling UAST: %s")
// ErrMarshalUAST is returned when an error arises marshaling UASTs.
ErrMarshalUAST = errors.NewKind("error marshaling uast node: %s")
)
func exprToString(
ctx *sql.Context,
e sql.Expression,
r sql.Row,
) (string, error) {
if e == nil {
return "", nil
}
x, err := e.Eval(ctx, r)
if err != nil {
return "", err
}
if x == nil {
return "", nil
}
x, err = sql.Text.Convert(x)
if err != nil {
return "", err
}
return x.(string), nil
}
var crcTable = crc64.MakeTable(crc64.ISO)
func newHash() hash.Hash64 {
return crc64.New(crcTable)
}
func computeKey(h hash.Hash64, mode, lang string, blob []byte) (uint64, error) {
h.Reset()
if err := writeToHash(h, [][]byte{
[]byte(mode),
[]byte(lang),
blob,
}); err != nil {
return 0, err
}
return h.Sum64(), nil
}
func writeToHash(h hash.Hash, elements [][]byte) error {
for _, e := range elements {
n, err := h.Write(e)
if err != nil {
return err
}
if n != len(e) {
return fmt.Errorf("cache key hash: " +
"couldn't write all the content")
}
}
return nil
}
func getUASTFromBblfsh(ctx *sql.Context,
blob []byte,
lang, xpath string,
mode bblfsh.Mode,
) (nodes.Node, error) {
session, ok := ctx.Session.(*gitbase.Session)
if !ok {
return nil, gitbase.ErrInvalidGitbaseSession.New(ctx.Session)
}
client, err := session.BblfshClient()
if err != nil {
return nil, err
}
// If we have a language we must check if it's supported. If we don't, bblfsh
// is the one that will have to identify the language.
if lang != "" {
ok, err = client.IsLanguageSupported(ctx, lang)
if err != nil {
return nil, err
}
if !ok {
return nil, ErrParseBlob.New(
fmt.Errorf("unsupported language %q", lang))
}
}
node, _, err := client.ParseWithMode(ctx, mode, lang, blob)
if err != nil {
err := ErrParseBlob.New(err)
logrus.Warn(err)
ctx.Warn(0, err.Error())
return nil, err
}
return node, nil
}
func applyXpath(n nodes.Node, query string) (nodes.Array, error) {
var filtered nodes.Array
it, err := tools.Filter(n, query)
if err != nil {
return nil, err
}
for n := range tools.Iterate(it) {
filtered = append(filtered, n)
}
return filtered, nil
}
func marshalNodes(arr nodes.Array) (interface{}, error) {
if len(arr) == 0 {
return nil, nil
}
var buf bytes.Buffer
if err := nodesproto.WriteTo(&buf, arr); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func getNodes(data interface{}) (nodes.Array, error) {
if data == nil {
return nil, nil
}
raw, ok := data.([]byte)
if !ok {
return nil, ErrUnmarshalUAST.New("wrong underlying UAST format")
}
return unmarshalNodes(raw)
}
func unmarshalNodes(data []byte) (nodes.Array, error) {
if len(data) == 0 {
return nil, nil
}
buf := bytes.NewReader(data)
n, err := nodesproto.ReadTree(buf)
if err != nil {
return nil, err
}
if n.Kind() != nodes.KindArray {
return nil, fmt.Errorf("unmarshal: wrong kind of node found %q, expected %q",
n.Kind(), nodes.KindArray.String())
}
return n.(nodes.Array), nil
}