-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_builder.go
437 lines (375 loc) · 10.2 KB
/
tree_builder.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package gal
import (
"strings"
"github.com/pkg/errors"
)
type TreeBuilder struct{}
func NewTreeBuilder() *TreeBuilder {
return &TreeBuilder{}
}
// nolint: gocognit,gocyclo,cyclop
func (tb TreeBuilder) FromExpr(expr string) (Tree, error) {
tree := Tree{}
for idx := 0; idx < len(expr); {
part, ptype, length, err := extractPart(expr[idx:])
if err != nil {
return nil, err
}
switch ptype {
case numericalType:
v, err := NewNumberFromString(part)
if err != nil {
return nil, err
}
tree = append(tree, v)
case stringType:
v := NewString(part)
tree = append(tree, v)
case boolType:
v, err := NewBoolFromString(part)
if err != nil {
return nil, err
}
tree = append(tree, v)
case operatorType:
switch part {
case Plus.String():
tree = append(tree, Plus)
case Minus.String():
tree = append(tree, Minus)
case Multiply.String():
tree = append(tree, Multiply)
case Divide.String():
tree = append(tree, Divide)
case Modulus.String():
tree = append(tree, Modulus)
case Power.String():
tree = append(tree, Power)
case LessThan.String():
tree = append(tree, LessThan)
case LessThanOrEqual.String():
tree = append(tree, LessThanOrEqual)
case EqualTo.String():
tree = append(tree, EqualTo)
case NotEqualTo.String():
tree = append(tree, NotEqualTo)
case GreaterThan.String():
tree = append(tree, GreaterThan)
case GreaterThanOrEqual.String():
tree = append(tree, GreaterThanOrEqual)
case LShift.String():
tree = append(tree, LShift)
case RShift.String():
tree = append(tree, RShift)
case And.String():
tree = append(tree, And)
case And2.String():
tree = append(tree, And2) // NOTE: re-route to And?
case Or.String():
tree = append(tree, Or)
case Or2.String():
tree = append(tree, Or2) // NOTE: re-route to Or?
default:
return nil, errors.Errorf("unknown operator: '%s'", part)
}
case functionType:
fname, l, _ := readNamedExpressionType(part) //nolint: errcheck // ignore err: we already parsed the function name when in extractPart()
v, err := tb.FromExpr(part[l+1 : len(part)-1]) // exclude leading '(' and trailing ')'
if err != nil {
return nil, err
}
if fname == "" {
// parenthesis grouping, not a real function per-se.
// conceptually, parenthesis grouping is a special case of anonymous identity function
tree = append(tree, v)
} else {
bodyFn := BuiltInFunction(fname)
tree = append(tree, NewFunction(fname, bodyFn, v.Split()...))
}
case variableType:
v := NewVariable(part)
tree = append(tree, v)
case blankType:
// only returned when the entire expression is blank or only contains blanks.
return tree, nil
default:
return nil, errors.Errorf("internal error: unknown expression part type '%v'", ptype)
}
idx += length
}
// adjust trees that start with "Plus" or "Minus" followed by a "Numberer"
if tree.TrunkLen() >= 2 {
switch tree[0] {
case Plus:
return tree[1:], nil
case Minus:
return append(Tree{NewNumberFromInt(-1), Multiply}, tree[1:]...), nil
}
}
return tree, nil
}
// returns the part extracted as string, the type extracted, the cursor position
// after extraction or an error.
//
// nolint: gocognit,gocyclo,cyclop
func extractPart(expr string) (string, exprType, int, error) {
// left trim blanks
pos := 0
for _, r := range expr {
if !isBlankSpace(r) {
break
}
pos++
}
// blank: no part
if pos == len(expr) {
return "", blankType, pos, nil
}
// read part - "string"
if expr[pos] == '"' {
s, l, err := readString(expr[pos:])
if err != nil {
return "", unknownType, 0, err
}
return s, stringType, pos + l, nil
}
// read part - "boolean"
// TODO: we can probably merge this with the logic that goes for functions
// ...and call it something like "textual expression type" which can be
// ...functions, variables, object properties, object functions or constants
{ // make s, l and ok local scope
s, l, ok := readBool(expr[pos:])
if ok {
// it's a bool
return s, boolType, pos + l, nil
}
}
// read part - :variable:
if expr[pos] == ':' {
s, l, err := readVariable(expr[pos:])
if err != nil {
return "", unknownType, 0, err
}
return s, variableType, pos + l, nil
}
// read part - function(...) / constant / (brackets...) / object.property / object.function()
// conceptually, parenthesis grouping is a special case of anonymous identity function
// NOTE: name expression types that contain a '.' are reserved for Object's only.
if expr[pos] == '(' || (expr[pos] >= 'a' && expr[pos] <= 'z') || (expr[pos] >= 'A' && expr[pos] <= 'Z') {
fname, lf, err := readNamedExpressionType(expr[pos:])
switch {
case errors.Is(err, errFunctionNameWithoutParens):
if strings.Contains(fname, ".") {
// object property found: act like a variable
// TODO: could create a new objectPropertyType
return fname, variableType, pos + lf, nil
}
// allow to continue so we can check alphanumerical operator names such as "And", "Or", etc
// TODO: before we try for alphanum operators, we will need to check if we have a defined constant
// e.g. Phi (golden ratio), etc user-defined or built-in (True, False)
case err != nil:
return "", unknownType, 0, err
default:
// TODO: if name contains `.` it's an object function - could create a new objectFunctionType
fargs, la, err := readFunctionArguments(expr[pos+lf:])
if err != nil {
return "", unknownType, 0, err
}
return fname + fargs, functionType, pos + lf + la, nil
}
}
// read part - operator
if s, l := readOperator(expr[pos:]); l != 0 {
if s == "+" || s == "-" {
s, l = squashPlusMinusChain(expr[pos:]) // TODO: move this into readOperator()?
}
return s, operatorType, pos + l, nil
}
// read part - number
// TODO: complex numbers are not supported - could be "native" or via function or perhaps even a specialised MultiValue?
s, l, err := readNumber(expr[pos:])
if err != nil {
return "", unknownType, 0, err
}
return s, numericalType, pos + l, nil
}
func readString(expr string) (string, int, error) {
to := 1 // keep leading double-quotes
escapes := 0
for i, r := range expr[1:] {
to++
if expr[i] == '\\' {
escapes += 1
continue
}
if r == '"' && (escapes == 0 || escapes&1 == 0) {
break
}
// TODO: perhaps we should collapse the `\`'s, here?
escapes = 0
}
if expr[to-1] != '"' {
return "", 0, errors.Errorf("syntax error: non-terminated string '%s'", expr[:to])
}
return expr[1 : to-1], to, nil
}
func readVariable(expr string) (string, int, error) {
to := 1 // keep leading ':'
for _, r := range expr[1:] {
to++
if r == ':' {
break
}
if isBlankSpace(r) {
return "", 0, errors.Errorf("syntax error: invalid character '%c' for variable name '%s'", r, expr[:to])
}
}
if expr[to-1] != ':' {
return "", 0, errors.Errorf("syntax error: missing ':' to end variable '%s'", expr[:to])
}
return expr[:to], to, nil
}
// the bool is an `ok` type bool, it is set to true if we successfull read a Bool
func readBool(expr string) (string, int, bool) {
to := 0
readString:
for _, r := range expr {
to++
switch {
case r >= 'a' && r <= 'z',
r >= 'A' && r <= 'Z':
continue
case isBlankSpace(r):
// we read a potential bool
to-- // eject the space character we just read
break readString
default:
// not a bool
return "", 0, false
}
}
switch expr[:to] {
case "True", "False":
// it's a Bool
return expr[:to], to, true
default:
// it isn't a Bool
return "", 0, false
}
}
var errFunctionNameWithoutParens = errors.New("function with Parenthesis")
func readNamedExpressionType(expr string) (string, int, error) {
to := 0 // this could be an anonymous identity function (i.e. simple case of parenthesis grouping)
for _, r := range expr {
if r == '(' {
return expr[:to], to, nil
}
if isBlankSpace(r) {
break
}
to++
}
return expr[:to], to, errFunctionNameWithoutParens
}
func readFunctionArguments(expr string) (string, int, error) {
to := 1
bktCount := 1 // the currently opened bracket
for i := 1; i < len(expr); i++ {
r := expr[i]
if r == '"' {
_, l, err := readString(expr[to:])
if err != nil {
return "", 0, err
}
to += l
i += l - 1
continue
}
to++
if r == '(' {
bktCount++
continue
}
if r == ')' {
bktCount--
if bktCount == 0 {
return expr[:to], to, nil
}
}
}
return "", 0, errors.Errorf("syntax error: missing ')' for function arguments '%s'", expr[:to])
}
func readNumber(expr string) (string, int, error) {
to := 0
isFloat := false
for i, r := range expr {
if isBlankSpace(r) || isOperator(expr[i:]) {
break
}
to++
if r == '.' && !isFloat {
isFloat = true
continue
}
if r >= '0' && r <= '9' {
continue
}
return "", 0, errors.Errorf("syntax error: invalid character '%c' for number '%s'", r, expr[:to])
}
return expr[:to], to, nil
}
func squashPlusMinusChain(expr string) (string, int) {
to := 0
outcomeSign := 1
for _, r := range expr {
// if isBlankSpace(r) {
// break
// }
if r != '+' && r != '-' && !isBlankSpace(r) {
break
}
if r == '-' {
outcomeSign = -outcomeSign
}
to++
}
sign := "-"
if outcomeSign == 1 {
sign = "+"
}
return sign, to
}
func isBlankSpace(r rune) bool {
return r == ' ' || r == '\t' || r == '\n'
}
func readOperator(s string) (string, int) {
switch {
case strings.HasPrefix(s, And.String()):
return s[:3], 3
case strings.HasPrefix(s, Power.String()),
strings.HasPrefix(s, LShift.String()),
strings.HasPrefix(s, RShift.String()),
strings.HasPrefix(s, EqualTo.String()),
strings.HasPrefix(s, NotEqualTo.String()),
strings.HasPrefix(s, GreaterThanOrEqual.String()),
strings.HasPrefix(s, LessThanOrEqual.String()),
strings.HasPrefix(s, And2.String()),
strings.HasPrefix(s, Or.String()),
strings.HasPrefix(s, Or2.String()):
return s[:2], 2
case strings.HasPrefix(s, Plus.String()),
strings.HasPrefix(s, Minus.String()),
strings.HasPrefix(s, Divide.String()),
strings.HasPrefix(s, Multiply.String()),
strings.HasPrefix(s, Modulus.String()),
strings.HasPrefix(s, GreaterThan.String()),
strings.HasPrefix(s, LessThan.String()):
return s[:1], 1
default:
return "", 0
}
}
func isOperator(s string) bool {
_, l := readOperator(s)
return l != 0
}