This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
driver.go
192 lines (170 loc) · 5.25 KB
/
driver.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
package protocol
import (
"bytes"
"context"
"errors"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/opentracing/opentracing-go"
xcontext "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
serrors "gopkg.in/src-d/go-errors.v1"
"github.com/bblfsh/sdk/v3/driver"
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/bblfsh/sdk/v3/uast/nodes/nodesproto"
)
//go:generate protoc --proto_path=$GOPATH/src:. --gogo_out=plugins=grpc:. ./driver.proto
const (
mb = 1 << 20
// DefaultGRPCMaxMessageBytes is maximum msg size for gRPC.
DefaultGRPCMaxMessageBytes = 100 * mb
)
// ServerOptions returns a set of common options that should be used in bblfsh server.
//
// It automatically enables OpenTrace if a global tracer is set.
func ServerOptions() []grpc.ServerOption {
opts := []grpc.ServerOption{
grpc.MaxSendMsgSize(DefaultGRPCMaxMessageBytes),
grpc.MaxRecvMsgSize(DefaultGRPCMaxMessageBytes),
}
tracer := opentracing.GlobalTracer()
if _, ok := tracer.(opentracing.NoopTracer); ok {
return opts
}
opts = append(opts,
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
)
return opts
}
// DialOptions returns a set of common options that should be used when dialing bblfsh server.
//
// It automatically enables OpenTrace if a global tracer is set.
func DialOptions() []grpc.DialOption {
opts := []grpc.DialOption{grpc.WithDefaultCallOptions(
grpc.MaxCallSendMsgSize(DefaultGRPCMaxMessageBytes),
grpc.MaxCallRecvMsgSize(DefaultGRPCMaxMessageBytes),
)}
tracer := opentracing.GlobalTracer()
if _, ok := tracer.(opentracing.NoopTracer); ok {
return opts
}
opts = append(opts,
grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(tracer)),
grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(tracer)),
)
return opts
}
func RegisterDriver(srv *grpc.Server, d driver.Driver) {
RegisterDriverServer(srv, &driverServer{d: d})
}
func AsDriver(cc *grpc.ClientConn) driver.Driver {
return &client{c: NewDriverClient(cc)}
}
func toParseErrors(err error) []*ParseError {
if e, ok := err.(*driver.ErrMulti); ok {
errs := make([]*ParseError, 0, len(e.Errors))
for _, e := range e.Errors {
errs = append(errs, &ParseError{Text: e.Error()})
}
return errs
}
return []*ParseError{
{Text: err.Error()},
}
}
type driverServer struct {
d driver.Driver
}
// Parse implements DriverServer.
func (s *driverServer) Parse(rctx xcontext.Context, req *ParseRequest) (*ParseResponse, error) {
sp, ctx := opentracing.StartSpanFromContext(rctx, "bblfsh.server.Parse")
defer sp.Finish()
opts := &driver.ParseOptions{
Mode: driver.Mode(req.Mode),
Language: req.Language,
Filename: req.Filename,
}
var resp ParseResponse
n, err := s.d.Parse(ctx, req.Content, opts)
resp.Language = opts.Language // can be set during the call
if e, ok := err.(*serrors.Error); ok {
cause := e.Cause()
if driver.ErrDriverFailure.Is(err) {
return nil, status.Error(codes.Internal, cause.Error())
} else if driver.ErrTransformFailure.Is(err) {
return nil, status.Error(codes.FailedPrecondition, cause.Error())
} else if driver.ErrModeNotSupported.Is(err) {
return nil, status.Error(codes.InvalidArgument, cause.Error())
}
if !driver.ErrSyntax.Is(err) {
return nil, err // unknown error
}
// partial parse or syntax error; we will send an OK status code, but will fill Errors field
resp.Errors = toParseErrors(cause)
}
dsp, _ := opentracing.StartSpanFromContext(ctx, "uast.Encode")
defer dsp.Finish()
buf := bytes.NewBuffer(nil)
err = nodesproto.WriteTo(buf, n)
if err != nil {
return nil, err // unknown error = server failure
}
resp.Uast = buf.Bytes()
return &resp, nil
}
type client struct {
c DriverClient
}
// Parse implements DriverClient.
func (c *client) Parse(rctx context.Context, src string, opts *driver.ParseOptions) (nodes.Node, error) {
sp, ctx := opentracing.StartSpanFromContext(rctx, "bblfsh.client.Parse")
defer sp.Finish()
req := &ParseRequest{Content: src}
if opts != nil {
req.Mode = Mode(opts.Mode)
req.Language = opts.Language
req.Filename = opts.Filename
}
resp, err := c.c.Parse(ctx, req)
if s, ok := status.FromError(err); ok {
var kind *serrors.Kind
switch s.Code() {
case codes.Internal:
kind = driver.ErrDriverFailure
case codes.FailedPrecondition:
kind = driver.ErrTransformFailure
case codes.InvalidArgument:
kind = driver.ErrModeNotSupported
}
if kind != nil {
return nil, kind.Wrap(errors.New(s.Message()))
}
}
if err != nil {
return nil, err // server or network error
}
if opts != nil && opts.Language == "" {
opts.Language = resp.Language
}
dsp, _ := opentracing.StartSpanFromContext(ctx, "uast.Decode")
defer dsp.Finish()
// it may be still a parsing error
return resp.Nodes()
}
func (m *ParseResponse) Nodes() (nodes.Node, error) {
ast, err := nodesproto.ReadTree(bytes.NewReader(m.Uast))
if err != nil {
return nil, err
}
if len(m.Errors) != 0 {
var errs []error
for _, e := range m.Errors {
errs = append(errs, errors.New(e.Text))
}
// syntax error or partial parse - return both UAST and an error
err = driver.ErrSyntax.Wrap(driver.JoinErrors(errs))
}
return ast, err
}