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 20
/
request.go
233 lines (204 loc) · 6.18 KB
/
request.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package bblfsh
import (
"context"
"errors"
"io/ioutil"
"path/filepath"
"time"
"github.com/bblfsh/sdk/v3/driver"
derrors "github.com/bblfsh/sdk/v3/driver/errors"
"github.com/bblfsh/sdk/v3/driver/manifest"
protocol2 "github.com/bblfsh/sdk/v3/protocol"
"github.com/bblfsh/sdk/v3/uast/nodes"
protocol1 "gopkg.in/bblfsh/sdk.v1/protocol"
)
var (
// ErrDriverFailure is returned when the driver is malfunctioning.
ErrDriverFailure = derrors.ErrDriverFailure
// ErrSyntax is returned when driver cannot parse the source file.
// Can be omitted for native driver implementations.
ErrSyntax = derrors.ErrSyntax
)
func errorStrings(str []string) error {
errs := make([]error, 0, len(str))
for _, e := range str {
errs = append(errs, errors.New(e))
}
return derrors.Join(errs)
}
// Mode controls the level of transformation applied to UAST.
type Mode = protocol2.Mode
const (
Native = protocol2.Mode_Native
Annotated = protocol2.Mode_Annotated
Semantic = protocol2.Mode_Semantic
)
// Parse mode parses a UAST mode string to an enum value.
func ParseMode(mode string) (Mode, error) {
m, err := driver.ParseMode(mode)
if err != nil {
return 0, err
}
return Mode(m), nil
}
// ParseRequest is a parsing request to get the UAST.
type ParseRequest struct {
ctx context.Context
content string
options driver.ParseOptions
client *Client
err error
}
// Language sets the language of the given source file to parse. if missing
// will be guess from the filename and the content.
func (r *ParseRequest) Language(language string) *ParseRequest {
r.options.Language = language
return r
}
// ReadFile loads a file given a local path and sets the content and the
// filename of the request.
func (r *ParseRequest) ReadFile(fp string) *ParseRequest {
data, err := ioutil.ReadFile(fp)
if err != nil {
r.err = err
} else {
r.content = string(data)
r.options.Filename = filepath.Base(fp)
}
return r
}
// Content sets the content of the parse request. It should be the source code
// that wants to be parsed.
func (r *ParseRequest) Content(content string) *ParseRequest {
r.content = content
return r
}
// Filename sets the filename of the content.
func (r *ParseRequest) Filename(filename string) *ParseRequest {
r.options.Filename = filename
return r
}
// Mode controls the level of transformation applied to UAST.
func (r *ParseRequest) Mode(mode Mode) *ParseRequest {
r.options.Mode = driver.Mode(mode)
return r
}
// Context sets a cancellation context for this request.
func (r *ParseRequest) Context(ctx context.Context) *ParseRequest {
r.ctx = ctx
return r
}
// Do performs the actual parsing by serializing the request, sending it to
// bblfshd and waiting for the response.
//
// It's the caller's responsibility to interpret errors properly.
//
// Deprecated: use UAST() instead
func (r *ParseRequest) Do() (*protocol2.ParseResponse, error) {
if r.err != nil {
return nil, r.err
}
return r.client.driver2.Parse(r.ctx, &protocol2.ParseRequest{
Content: r.content,
Mode: protocol2.Mode(r.options.Mode),
Language: r.options.Language,
Filename: r.options.Filename,
})
}
// Node is a generic UAST node.
type Node = nodes.Node
// UAST send the request and returns decoded UAST and the language.
//
// If a file contains syntax error, the ErrSyntax is returned and the UAST may be nil or partial in this case.
//
// ErrDriverFailure is returned if the native driver is malfunctioning.
func (r *ParseRequest) UAST() (Node, string, error) {
ast, err := r.client.driver.Parse(r.ctx, r.content, &r.options)
return ast, r.options.Language, err
}
// VersionRequest is a request to retrieve the version of the server.
type VersionRequest struct {
ctx context.Context
client *Client
err error
}
// Context sets a cancellation context for this request.
func (r *VersionRequest) Context(ctx context.Context) *VersionRequest {
r.ctx = ctx
return r
}
// VersionResponse contains information about Babelfish version.
type VersionResponse struct {
// Version is the server version. If is a local compilation the version
// follows the pattern dev-<short-commit>[-dirty], dirty means that was
// compile from a repository with un-committed changes.
Version string `json:"version"`
// Build contains the timestamp at the time of the build.
Build time.Time `json:"build"`
}
// Do performs the actual parsing by serializing the request, sending it to
// bblfsd and waiting for the response.
func (r *VersionRequest) Do() (*VersionResponse, error) {
if r.err != nil {
return nil, r.err
}
resp, err := r.client.driver.Version(r.ctx)
if err != nil {
return nil, err
}
return &VersionResponse{
Version: resp.Version,
Build: resp.Build,
}, nil
}
// SupportedLanguagesRequest is a request to retrieve the supported languages.
type SupportedLanguagesRequest struct {
ctx context.Context
client *Client
err error
}
// Context sets a cancellation context for this request.
func (r *SupportedLanguagesRequest) Context(ctx context.Context) *SupportedLanguagesRequest {
r.ctx = ctx
return r
}
// DriverManifest contains an information about a single Babelfish driver.
//
// Deprecated: see DoV2.
type DriverManifest = protocol1.DriverManifest
// Do performs the supported languages request and return information about available drivers.
//
// Deprecated: use DoV2 instead.
func (r *SupportedLanguagesRequest) Do() ([]DriverManifest, error) {
if r.err != nil {
return nil, r.err
}
list, err := r.client.driver.Languages(r.ctx)
if err != nil {
return nil, err
}
out := make([]DriverManifest, 0, len(list))
for _, m := range list {
dm := DriverManifest{
Name: m.Name,
Language: m.Language,
Version: m.Version,
Status: string(m.Status),
Features: make([]string, 0, len(m.Features)),
}
for _, f := range m.Features {
dm.Features = append(dm.Features, string(f))
}
out = append(out, dm)
}
return out, nil
}
// DriverManifestV2 contains an information about a single Babelfish driver.
type DriverManifestV2 = manifest.Manifest
// DoV2 performs the supported languages request and return information about available drivers.
func (r *SupportedLanguagesRequest) DoV2() ([]DriverManifestV2, error) {
if r.err != nil {
return nil, r.err
}
return r.client.driver.Languages(r.ctx)
}