-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_common.go
314 lines (278 loc) · 6.61 KB
/
parser_common.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package parser
import (
"encoding/json"
"github.com/pboyd04/godata/filter/lexer"
)
type Parser struct {
lexer *lexer.Lexer
tokens []*lexer.Token
op *Operation
}
type IParser interface {
GetDBQuery(p *Parser) (interface{}, error)
GetDBQueryWithReplacement(p *Parser, a ...interface{}) (interface{}, error)
}
//nolint:gochecknoglobals // This is a map of parsers, required to let other parsers register themselves
var parsers = map[string]IParser{}
func NewParser(input string) (*Parser, error) {
myLexer := lexer.NewLexer(input)
ret := &Parser{lexer: myLexer, tokens: []*lexer.Token{}}
token, err := myLexer.NextToken()
if err != nil {
return nil, err
}
for token != nil {
ret.tokens = append(ret.tokens, token)
token, err = myLexer.NextToken()
if err != nil {
return nil, err
}
}
return ret, nil
}
type Operator int
const (
NoOp Operator = -1
)
type Operand interface {
GetData() (interface{}, error)
}
type SliceOperand struct {
Slice []Operand
}
type ObjectOperand struct {
Properties string
}
type Operation struct {
Operator Operator `json:"operator"`
Operands []Operand `json:"operands,omitempty"`
}
type tokenComparer func(*lexer.Token) bool
func (p *Parser) GetOperation() (*Operation, error) {
if p.op != nil {
return p.op, nil
}
// Make all the tokens one big group...
tokenGroup := newTokenGroup(p.tokens)
// Handle parentheses
tokenGroup.parentheses()
// Group {}
err := tokenGroup.handleObjects()
if err != nil {
return nil, err
}
// Start figuring out ops...
err = tokenGroup.processTokenGroupOps()
if err != nil {
return nil, err
}
return tokenGroup.flatten(nil)
}
func (o *Operation) flatten() error {
for i := 0; i < len(o.Operands); i++ {
switch operand := o.Operands[i].(type) {
case *tokenGroup:
if o.Operator.hasParameters() {
// This is a method call
o.Operands = append(o.Operands, operand.children...)
// Remove all token groups from the parent
for i := 0; i < len(o.Operands); i++ {
_, ok := o.Operands[i].(*tokenGroup)
if ok {
o.Operands = append(o.Operands[:i], o.Operands[i+1:]...)
}
}
return o.flatten()
}
op, err := operand.flatten(o)
if err != nil {
return operand.handleFlattenError(o, err)
}
o.Operands[i] = op
case *Operation:
err := operand.flatten()
if err != nil {
return err
}
}
}
return nil
}
func (o *Operation) unary() error {
for i := 0; i < len(o.Operands); i++ {
switch operand := o.Operands[i].(type) {
case *tokenGroup:
err := operand.unary()
if err != nil {
return err
}
case *Operation:
err := operand.unary()
if err != nil {
return err
}
}
}
return nil
}
func (o *Operation) methodCalls() error {
for i := 0; i < len(o.Operands); i++ {
switch operand := o.Operands[i].(type) {
case *tokenGroup:
err := operand.methodCalls()
if err != nil {
return err
}
case *Operation:
err := operand.methodCalls()
if err != nil {
return err
}
}
}
return nil
}
func multiplicativeCompare(token *lexer.Token) bool {
return token.IsMultiplicative()
}
func additiveCompare(token *lexer.Token) bool {
return token.IsAdditive()
}
func relationalCompare(token *lexer.Token) bool {
return token.IsRelational()
}
func equalityCompare(token *lexer.Token) bool {
return token.IsEquality()
}
func conjunctionCompare(token *lexer.Token) bool {
return token.Type == lexer.And || token.Type == lexer.Or
}
func (o *Operation) beforeAndAfterOpProcess(compareFn tokenComparer) error {
for i := 0; i < len(o.Operands); i++ {
switch operand := o.Operands[i].(type) {
case *tokenGroup:
err := operand.beforeAndAfterOpProcess(compareFn)
if err != nil {
return err
}
case *Operation:
err := operand.beforeAndAfterOpProcess(compareFn)
if err != nil {
return err
}
}
}
return nil
}
func (o *Operation) GetData() (interface{}, error) {
return o, nil
}
func (s *SliceOperand) GetData() (interface{}, error) {
return s.Slice, nil
}
func (o *ObjectOperand) GetData() (interface{}, error) {
data := make(map[string]interface{})
err := json.Unmarshal([]byte(o.Properties), &data)
if err != nil {
return nil, err
}
return data, nil
}
func (o *Operator) hasParameters() bool {
return lexer.TokenKey(*o).HasParameters()
}
func (o *Operation) ReplaceOperands(a ...interface{}) (*Operation, error) {
newOp, err := o.deepClone()
if err != nil {
return nil, err
}
for i, operand := range a {
err := newOp.replaceOperand(i, operand)
if err != nil {
return nil, err
}
}
return newOp, nil
}
func (o *Operation) deepClone() (*Operation, error) {
newOp := &Operation{Operator: o.Operator, Operands: make([]Operand, len(o.Operands))}
for i, operand := range o.Operands {
switch op := operand.(type) {
case *lexer.Token:
newOp.Operands[i] = &lexer.Token{Type: op.Type, Text: op.Text}
case *Operation:
newChild, err := op.deepClone()
if err != nil {
return nil, err
}
newOp.Operands[i] = newChild
default:
return nil, newParserError("unknown type: %T", operand)
}
}
return newOp, nil
}
func (o *Operation) replaceOperand(i int, operand interface{}) error {
for _, inOperand := range o.Operands {
switch op := inOperand.(type) {
case *lexer.Token:
if op.IsCorrectReplacement(i) {
err := op.Replace(operand)
if err != nil {
return err
}
}
case *Operation:
err := op.replaceOperand(i, operand)
if err != nil {
return err
}
default:
return newParserError("unknown type: %T", inOperand)
}
}
return nil
}
func newSliceOperand(tokens []Operand) *SliceOperand {
ret := &SliceOperand{Slice: make([]Operand, 0)}
for _, token := range tokens {
token, ok := token.(*lexer.Token)
if ok && token.Type == lexer.Comma {
// Skip commas
continue
}
ret.Slice = append(ret.Slice, token)
}
return ret
}
func (p *Parser) GetDBQuery(language string) (interface{}, error) {
parser, ok := parsers[language]
if !ok {
return nil, ErrNoSuchLanguage
}
return parser.GetDBQuery(p)
}
func (p *Parser) GetDBQueryWithReplacement(language string, a ...interface{}) (interface{}, error) {
parser, ok := parsers[language]
if !ok {
return nil, ErrNoSuchLanguage
}
return parser.GetDBQueryWithReplacement(p, a...)
}
func (p *Parser) ReplaceOperands(a ...interface{}) (*Parser, error) {
op, err := p.GetOperation()
if err != nil {
return nil, err
}
op, err = op.ReplaceOperands(a...)
if err != nil {
return nil, err
}
return &Parser{lexer: p.lexer, tokens: nil, op: op}, nil
}
func RegisterParser(name string, parser IParser) {
parsers[name] = parser
}
func (o *Operator) MarshalJSON() ([]byte, error) {
return json.Marshal(lexer.TokenKey(*o).String())
}