-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
262 lines (240 loc) · 5.5 KB
/
service.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package thrifter
type Service struct {
NodeCommonField
Ident string
Elems []*Function
Extends string
Options []*Option
ElemsMap map[string]*Function // startToken hash => Function node
}
func NewService(start *Token, parent Node) *Service {
return &Service{
NodeCommonField: NodeCommonField{
Parent: parent,
StartToken: start,
},
ElemsMap: map[string]*Function{},
}
}
func (r *Service) NodeType() string {
return "Service"
}
func (r *Service) NodeValue() interface{} {
return *r
}
func (r *Service) String() string {
return toString(r.StartToken, r.EndToken)
}
func (r *Service) parse(p *Parser) (err error) {
p.peekNonWhitespace()
identTok := p.nextIdent(false)
r.Ident = identTok.Raw
tok := p.nextNonWhitespace()
if tok.Type == T_LEFTCURLY {
r.Elems, err = r.parseFunctions(p)
if err != nil {
return err
}
// parse options
ru := p.peekNonWhitespace()
if toToken(string(ru)) != T_LEFTPAREN {
return
}
p.next() // consume (
r.Options, r.EndToken, err = parseOptions(p, r)
if err != nil {
return err
}
} else if tok.Value == "extends" {
identTok := p.nextIdent(false)
r.Extends = identTok.Raw
tok := p.nextNonWhitespace()
if tok.Type == T_LEFTCURLY {
r.Elems, err = r.parseFunctions(p)
if err != nil {
return err
}
} else {
return p.unexpected(tok.Value, "{")
}
} else {
return p.unexpected(tok.Value, "extends or {")
}
return
}
func (r *Service) parseFunctions(p *Parser) (funcs []*Function, err error) {
for {
if p.buf != nil && p.buf.Type == T_RIGHTCURLY {
r.EndToken = p.buf
p.buf = nil
break
}
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_RIGHTCURLY {
r.EndToken = p.next()
break
}
elem := NewFunction(r)
if err = elem.parse(p); err != nil {
return nil, err
}
elem.patchToParentMap()
funcs = append(funcs, elem)
}
return
}
const (
FIELD_PARENT_TYPE_ARGS = iota + 1
FIELD_PARENT_TYPE_THROWS
)
type Function struct {
NodeCommonField
Ident string
Throws []*Field
Oneway bool
FunctionType *FieldType
Void bool
Args []*Field
Options []*Option
ArgsMap map[string]*Field // startToken hash => Argument Field
ThrowsMap map[string]*Field // startToken hash => Throws Field
}
func NewFunction(parent Node) *Function {
return &Function{
NodeCommonField: NodeCommonField{
Parent: parent,
},
ArgsMap: map[string]*Field{},
ThrowsMap: map[string]*Field{},
}
}
func (r *Function) NodeType() string {
return "Function"
}
func (r *Function) NodeValue() interface{} {
return *r
}
func (r *Function) String() string {
return toString(r.StartToken, r.EndToken)
}
func (r *Function) patchToParentMap() {
hash := GenTokenHash(r.StartToken)
parent := r.Parent.(*Service)
parent.ElemsMap[hash] = r
}
func (r *Function) parse(p *Parser) (err error) {
p.peekNonWhitespace()
identTok := p.nextIdent(true)
if identTok.Raw == "oneway" {
r.StartToken = identTok
r.Oneway = true
p.peekNonWhitespace()
identTok := p.nextIdent(true)
if identTok.Raw == "void" {
r.Void = true
} else {
r.FunctionType = NewFieldType(r)
if err = r.FunctionType.parse(p); err != nil {
return err
}
}
} else if identTok.Raw == "void" {
r.StartToken = identTok
r.Void = true
} else {
p.buf = identTok
r.FunctionType = NewFieldType(r)
if err = r.FunctionType.parse(p); err != nil {
return err
}
r.StartToken = r.FunctionType.StartToken
}
p.peekNonWhitespace()
identTok = p.nextIdent(false)
r.Ident = identTok.Raw
// parse argument fields
var rightParenTok *Token
r.Args, rightParenTok, err = r.parseFields(p, FIELD_PARENT_TYPE_ARGS)
if err != nil {
return err
}
// parse options
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_LEFTPAREN {
r.Options, rightParenTok, err = r.parseOptions(p)
if err != nil {
return err
}
}
// parse throws
tok := p.nextNonWhitespace()
if tok.Type == T_THROWS {
r.Throws, rightParenTok, err = r.parseFields(p, FIELD_PARENT_TYPE_THROWS)
if err != nil {
return err
}
tok := p.nextNonWhitespace()
if tok.Type == T_COMMA || tok.Type == T_SEMICOLON {
r.EndToken = tok
} else {
p.buf = tok
r.EndToken = rightParenTok
}
} else if tok.Type == T_COMMA || tok.Type == T_SEMICOLON {
r.EndToken = tok
} else {
p.buf = tok
r.EndToken = rightParenTok
}
return
}
func (r *Function) parseOptions(p *Parser) (res []*Option, rightParenTok *Token, err error) {
p.next() // consume (
for {
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_RIGHTPAREN {
rightParenTok = p.next()
break
}
elem := NewOption(r)
if err = elem.parse(p); err != nil {
return nil, nil, err
}
res = append(res, elem)
ru = p.peekNonWhitespace()
if toToken(string(ru)) == T_COMMA {
p.next() // consume comma
}
}
return
}
func (r *Function) patchFieldToMap(t int, node *Field) {
hash := GenTokenHash(node.StartToken)
switch t {
case FIELD_PARENT_TYPE_ARGS:
r.ArgsMap[hash] = node
case FIELD_PARENT_TYPE_THROWS:
r.ThrowsMap[hash] = node
}
}
func (r *Function) parseFields(p *Parser, t int) (fields []*Field, rightParenTok *Token, err error) {
ru := p.peekNonWhitespace()
if toToken(string(ru)) != T_LEFTPAREN {
return nil, nil, p.unexpected(string(ru), "(")
}
p.next() // consume (
for {
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_RIGHTPAREN {
rightParenTok = p.next()
break
}
elem := NewField(r)
if err = elem.parse(p); err != nil {
return nil, nil, err
}
r.patchFieldToMap(t, elem)
fields = append(fields, elem)
}
return
}