-
Notifications
You must be signed in to change notification settings - Fork 27
/
uast.go
69 lines (54 loc) · 1.66 KB
/
uast.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
package service
import (
"bytes"
"fmt"
bblfsh "gopkg.in/bblfsh/client-go.v3"
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
"gopkg.in/bblfsh/sdk.v2/uast/nodes/nodesproto"
errors "gopkg.in/src-d/go-errors.v1"
)
// need to move to service to avoid circular imports
// TODO (carlosms): Duplicated code from gitbase,
// (internal/function/uast_utils.go) we should reuse that instead
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")
)
// UnmarshalNodes returns UAST nodes from data marshaled by gitbase
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
}
type ParseResponse struct {
UAST nodes.Node `json:"uast"`
Lang string `json:"language"`
}
type Language struct {
ID string `json:"id"`
Name string `json:"name"`
}
func DriverManifestsToLangs(drivers []bblfsh.DriverManifest) []Language {
result := make([]Language, len(drivers))
for i, driver := range drivers {
result[i] = Language{
ID: driver.Language,
Name: driver.Name,
}
}
return result
}