-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary.go
More file actions
98 lines (95 loc) · 2.66 KB
/
binary.go
File metadata and controls
98 lines (95 loc) · 2.66 KB
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
package sqlparser
import (
"github.com/viant/parsly"
"github.com/viant/parsly/matcher"
"github.com/viant/sqlparser/expr"
)
func parseBinaryExpr(cursor *parsly.Cursor, binary *expr.Binary) error {
var err error
if binary.X == nil {
binary.X, err = expectOperand(cursor)
if err != nil || binary.X == nil {
return err
}
}
//fmt.Printf("After op %v,: %s\n", binary.Op, cursor.Input[cursor.Pos:])
pos := cursor.Pos
if binary.Op == "" {
match := cursor.MatchAfterOptional(whitespaceMatcher, betweenKeywordMatcher, binaryOperatorMatcher, logicalOperatorMatcher, placeholderMatcher)
switch match.Code {
case logicalOperator:
if !matcher.IsWhiteSpace(cursor.Input[cursor.Pos]) {
cursor.Pos = pos
return nil
}
binary.Op = match.Text(cursor)
case binaryOperator:
binary.Op = match.Text(cursor)
case betweenToken:
binary.Op = match.Text(cursor)
rng := &expr.Range{}
if rng.Min, err = expectOperand(cursor); err != nil {
return err
}
match := cursor.MatchAfterOptional(whitespaceMatcher, rangeOperatorMatcher)
if match.Code != rangeOperator {
return cursor.NewError(rangeOperatorMatcher)
}
if rng.Max, err = expectOperand(cursor); err != nil {
return err
}
yExpr := &expr.Binary{X: rng}
if err := parseBinaryExpr(cursor, yExpr); err != nil {
return err
}
if yExpr.Y == nil {
binary.Y = rng
} else {
binary.Y = yExpr
}
return nil
case placeholderTokenCode:
binary.Op = ""
if binary.X == nil {
binary.X = &expr.Placeholder{Name: match.Text(cursor)}
} else {
placeholder := &expr.Placeholder{Name: match.Text(cursor)}
binary.Y = placeholder
prevPos := cursor.Pos
match = cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, logicalOperatorMatcher, groupByMatcher, groupByMatcher, havingKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
switch match.Code {
case logicalOperator:
additionalExpr := &expr.Binary{X: binary.Y, Op: match.Text(cursor)}
if err := parseBinaryExpr(cursor, additionalExpr); err != nil {
return err
}
binary.Y = additionalExpr
case parenthesesCode:
placeholder.Name += match.Text(cursor)
case groupByKeyword, havingKeyword, orderByKeyword, windowTokenCode, unionKeyword:
cursor.Pos = prevPos
return nil
case parsly.Invalid:
binary.Y = nil
cursor.Pos = pos
return nil
}
}
default:
return nil
}
}
if binary.Y == nil {
yExpr := &expr.Binary{}
if err := parseBinaryExpr(cursor, yExpr); err != nil {
return err
}
if yExpr.X != nil {
binary.Y = yExpr
}
if yExpr.Op == "" && yExpr.Y == nil {
binary.Y = yExpr.X
}
}
return nil
}