-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
179 lines (171 loc) · 5.22 KB
/
parser.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
package simplequery
import (
"errors"
"fmt"
)
type Node interface {
String() string
Value() string
Children() (Node, Node)
}
type (
oneSubNode struct{ node Node }
twoSubNodes struct{ node1, node2 Node }
valueNode struct{ nodeValue string }
)
func (_ oneSubNode) Value() string { return "" }
func (_ twoSubNodes) Value() string { return "" }
func (n valueNode) Value() string { return n.nodeValue }
func (n oneSubNode) Children() (Node, Node) { return n.node, nil }
func (n twoSubNodes) Children() (Node, Node) { return n.node1, n.node2 }
func (_ valueNode) Children() (Node, Node) { return nil, nil }
type (
AND struct{ twoSubNodes }
OR struct{ twoSubNodes }
NOT struct{ oneSubNode }
LT struct{ twoSubNodes }
LTE struct{ twoSubNodes }
GT struct{ twoSubNodes }
GTE struct{ twoSubNodes }
EQ struct{ twoSubNodes }
NE struct{ twoSubNodes }
ID struct{ valueNode }
VAL struct{ valueNode }
)
func (n AND) String() string { return fmt.Sprintf("AND{%s,%s}", n.node1, n.node2) }
func (n OR) String() string { return fmt.Sprintf("OR{%s,%s}", n.node1, n.node2) }
func (n NOT) String() string { return fmt.Sprintf("NOT{%s}", n.node) }
func (n LT) String() string { return fmt.Sprintf("LT{%s,%s}", n.node1, n.node2) }
func (n LTE) String() string { return fmt.Sprintf("LTE{%s,%s}", n.node1, n.node2) }
func (n GT) String() string { return fmt.Sprintf("GT{%s,%s}", n.node1, n.node2) }
func (n GTE) String() string { return fmt.Sprintf("GTE{%s,%s}", n.node1, n.node2) }
func (n EQ) String() string { return fmt.Sprintf("EQ{%s,%s}", n.node1, n.node2) }
func (n NE) String() string { return fmt.Sprintf("NE{%s,%s}", n.node1, n.node2) }
func (n ID) String() string { return fmt.Sprintf("ID{%q}", n.nodeValue) }
func (n VAL) String() string { return fmt.Sprintf("VAL{%q}", n.nodeValue) }
func parse(tokens []token) (Node, error) {
res := make([]interface{}, len(tokens))
for i, token := range tokens {
res[i] = token
}
// identify subexpressions with parentheses
for {
lPos := findToken(tokens, tokenTypeLPAR)
if lPos < 0 {
break
}
if lPos >= len(tokens)-1 {
return nil, errors.New("condition ends with an opening parentheses")
}
var (
rPos = -1
offset = lPos + 1
)
for {
if offset >= len(tokens)-1 {
return nil, errors.New("missing matching closing parentheses")
}
rPos = findToken(tokens[offset:], tokenTypeRPAR)
if rPos < 0 {
return nil, errors.New("missing matching closing parentheses")
}
rPos += offset
if lPos+1 == rPos {
return nil, errors.New("empty subexpression found")
}
if pos := findToken(tokens[offset:rPos], tokenTypeLPAR); pos < 0 {
break
}
offset = rPos + 1
}
subNode, err := parse(tokens[lPos+1 : rPos])
if err != nil {
return nil, fmt.Errorf("could not parse subexpression: %s", err)
}
res = append(res[:lPos], append([]interface{}{subNode}, res[rPos+1:]...)...)
tokens = append(tokens[:lPos], append([]token{{tokenType: tokenTypeNONE}}, tokens[rPos+1:]...)...)
}
for _, tokenType := range []tokenType{
tokenTypeID,
tokenTypeVAL,
tokenTypeEQ,
tokenTypeNE,
tokenTypeGT,
tokenTypeGTE,
tokenTypeLT,
tokenTypeLTE,
tokenTypeNOT,
tokenTypeAND,
tokenTypeOR,
} {
var tokenFound = true
for tokenFound {
tokenFound = false
for i, elem := range res {
token, _ := elem.(token)
if token.tokenType != tokenType {
continue
}
tokenFound = true
switch tokenType {
case tokenTypeID:
res[i] = ID{valueNode{nodeValue: token.matched}}
case tokenTypeVAL:
res[i] = VAL{valueNode{nodeValue: token.matched}}
default:
if i+1 >= len(res) {
return nil, fmt.Errorf("missing parameter for %s operator", tokenTypeString[tokenType])
}
subNode1, ok := res[i+1].(Node)
if !ok {
return nil, fmt.Errorf("parameter for %s operator is not a node (1), got: %s", tokenTypeString[tokenType], res[i+1])
}
res = append(res[:i+1], res[i+2:]...) // remove the (i+1)th element because it has become a sub node
switch tokenType {
case tokenTypeNOT:
res[i] = NOT{oneSubNode{node: subNode1}}
default:
if i == 0 {
return nil, fmt.Errorf("missing parameter for %s operator", tokenTypeString[tokenType])
}
subNode2, ok := res[i-1].(Node)
if !ok {
return nil, fmt.Errorf("parameter for %s operator is not a node (2)", tokenTypeString[tokenType])
}
n := twoSubNodes{subNode2, subNode1}
switch tokenType {
case tokenTypeEQ:
res[i] = EQ{n}
case tokenTypeNE:
res[i] = NE{n}
case tokenTypeGT:
res[i] = GT{n}
case tokenTypeGTE:
res[i] = GTE{n}
case tokenTypeLT:
res[i] = LT{n}
case tokenTypeLTE:
res[i] = LTE{n}
case tokenTypeAND:
res[i] = AND{n}
case tokenTypeOR:
res[i] = OR{n}
default:
return nil, fmt.Errorf("invalid token type: %s", tokenTypeString[tokenType])
}
res = append(res[:i-1], res[i:]...) // remove the (i-1)the element because it has become a sub node
}
break
}
}
}
}
if len(res) != 1 {
return nil, errors.New("parse tree must have exactly one start node")
}
startNode, ok := res[0].(Node)
if !ok {
return nil, errors.New("start node is not a node")
}
return startNode, nil
}