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
/
types.go
179 lines (153 loc) · 5.08 KB
/
types.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
// Copyright 2017 Sourced Technologies SL
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//go:generate proteus -f $GOPATH/src -p gopkg.in/bblfsh/sdk.v1/protocol -p gopkg.in/bblfsh/sdk.v1/uast
//go:generate stringer -type=Status,Encoding -output stringer.go
package protocol
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"gopkg.in/bblfsh/sdk.v1/uast"
)
// DefaultService is the default service used to process requests.
var DefaultService Service
// Service can parse code to UAST or AST.
type Service interface {
Parse(*ParseRequest) *ParseResponse
NativeParse(*NativeParseRequest) *NativeParseResponse
Version(*VersionRequest) *VersionResponse
}
// Status is the status of a response.
//proteus:generate
type Status byte
const (
// Ok status code.
Ok Status = iota
// Error status code. It is replied when the driver has got the AST with errors.
Error
// Fatal status code. It is replied when the driver hasn't could get the AST.
Fatal
)
// Encoding is the encoding used for the content string. Currently only
// UTF-8 or Base64 encodings are supported. You should use UTF-8 if you can
// and Base64 as a fallback.
//proteus:generate
type Encoding byte
const (
// UTF8 encoding
UTF8 Encoding = iota
// Base64 encoding
Base64
)
// Response basic response, never used directly.
type Response struct {
// Status is the status of the parsing request.
Status Status `json:"status"`
// Status is the status of the parsing request.
Errors []string `json:"errors"`
// Elapsed is the amount of time consume processing the request.
Elapsed time.Duration `json:"elapsed"`
}
// ParseRequest is a request to parse a file and get its UAST.
//proteus:generate
type ParseRequest struct {
// Filename is the name of the file containing the source code. Used for
// language detection. Only filename is required, path might be used but
// ignored. This is optional.
Filename string `json:"filename"`
// Language. If specified, it will override language detection. This is
// optional.
Language string `json:"language"`
// Content is the source code to be parsed.
Content string `json:"content"`
// Encoding is the encoding that the Content uses. Currently only UTF-8 and
// Base64 are supported.
Encoding Encoding `json:"encoding"`
// Timeout amount of time for wait until the request is proccessed.
Timeout time.Duration `json:"timeout"`
}
// ParseResponse is the reply to ParseRequest.
//proteus:generate
type ParseResponse struct {
Response
// UAST contains the UAST from the parsed code.
UAST *uast.Node `json:"uast"`
}
func (r *ParseResponse) String() string {
buf := bytes.NewBuffer(nil)
fmt.Fprintln(buf, "Status: ", strings.ToLower(r.Status.String()))
fmt.Fprintln(buf, "Errors: ")
for _, err := range r.Errors {
fmt.Fprintln(buf, " . ", err)
}
fmt.Fprintln(buf, "UAST: ")
fmt.Fprintln(buf, r.UAST.String())
return buf.String()
}
// NativeParseRequest is a request to parse a file and get its native AST.
//proteus:generate
type NativeParseRequest ParseRequest
// NativeParseResponse is the reply to NativeParseRequest by the native parser.
//proteus:generate
type NativeParseResponse struct {
Response
// AST contains the AST from the parsed code in json format.
AST string `json:"ast"`
}
func (r *NativeParseResponse) String() string {
var s struct {
Status string `json:"status"`
Errors []string `json:"errors"`
AST interface{} `json:"ast"`
}
s.Status = strings.ToLower(r.Status.String())
s.Status = strings.ToLower(r.Status.String())
s.Errors = r.Errors
s.Status = strings.ToLower(r.Status.String())
if len(s.Errors) == 0 {
s.Errors = make([]string, 0)
}
s.Status = strings.ToLower(r.Status.String())
if len(r.AST) > 0 {
err := json.Unmarshal([]byte(r.AST), &s.AST)
if err != nil {
return err.Error()
}
}
s.Status = strings.ToLower(r.Status.String())
buf := bytes.NewBuffer(nil)
e := json.NewEncoder(buf)
e.SetIndent("", " ")
e.SetEscapeHTML(false)
s.Status = strings.ToLower(r.Status.String())
err := e.Encode(s)
if err != nil {
return err.Error()
}
s.Status = strings.ToLower(r.Status.String())
return buf.String()
}
// VersionRequest is a request to get server version
//proteus:generate
type VersionRequest struct{}
// VersionResponse is the reply to VersionRequest
//proteus:generate
type VersionResponse struct {
Response
// 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"`
}