-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
277 lines (241 loc) · 7.45 KB
/
parse.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
package rule
import (
"fmt"
"github.com/antlr4-go/antlr/v4"
"github.com/shopspring/decimal"
parser "github.com/sky93/go-rule/internal/antlr4"
"strconv"
"strings"
)
// SyntaxError captures ANTLR syntax errors, storing them in errorListener.
func (l *errorListener) SyntaxError(_ antlr.Recognizer, _ any, line, column int, msg string, _ antlr.RecognitionException) {
l.hasErrors = true
l.errMsg = newSyntaxError(fmt.Sprintf("%d:%d: %s", line, column, msg))
}
// evaluate traverses the exprTree to evaluate the final boolean outcome, given a map
// of param.id -> actual value. (This is used by Rule.Evaluate.)
func (e *exprTree) evaluate(values map[int]any, debugMode bool) (bool, error) {
if e == nil {
return false, ErrorNoExpression
}
// Handle logical operator nodes
switch e.op {
case "and":
lRes, lErr := e.left.evaluate(values, debugMode)
if lErr != nil {
return false, lErr
}
rRes, rErr := e.right.evaluate(values, debugMode)
if rErr != nil {
return false, rErr
}
res := lRes && rRes
if e.not {
return !res, nil
}
return res, nil
case "or":
lRes, lErr := e.left.evaluate(values, debugMode)
if lErr != nil {
return false, lErr
}
rRes, rErr := e.right.evaluate(values, debugMode)
if rErr != nil {
return false, rErr
}
res := lRes || rRes
if e.not {
return !res, nil
}
return res, nil
}
// Leaf node => do param-based comparison
p := e.param
val, ok := values[p.id]
if !ok {
// If operator is "pr", presence is false => no value
if p.operator == "pr" {
return e.not == true, nil
}
// Otherwise treat missing param as false
if e.not {
return true, nil
}
return false, nil
}
out, err := compareOperator(val, p.operator, p.compareValue, p.strictTypeCheck)
if debugMode {
fmt.Printf(
"Name: %s, left Value: %v<%T>, Operator:%s, right Value: %v<%T>, Strict Type Check: %t, Result: %t\n",
p.Name, val, val, p.operator, p.compareValue, p.compareValue, p.strictTypeCheck, out,
)
}
if err != nil {
return false, err
}
if e.not {
return !out, nil
}
return out, nil
}
// parseFunctionCall extracts the function name and arguments from the parse context.
func (v *queryVisitor) parseFunctionCall(ctx parser.IFunctionCallContext) (string, []FunctionArgument, error) {
if ctx == nil {
return "", nil, ErrorInvalidFunctionCall
}
name := ctx.ATTRNAME().GetText()
var args []FunctionArgument
argList := ctx.ArgList()
if argList != nil {
vals := argList.AllValue()
for _, vc := range vals {
val, t, _, err := v.parseValue(vc)
if err != nil {
return "", nil, err
}
args = append(args, FunctionArgument{
ArgumentType: t,
Value: val,
})
}
}
return name, args, nil
}
// getAttrName returns the full text of an attrPath node (e.g. "user.age" or "account.details").
func (v *queryVisitor) getAttrName(ctx parser.IAttrPathContext) string {
return ctx.GetText()
}
// parseTypedValue inspects the typedValue parse tree, applying user type annotations if present.
func (v *queryVisitor) parseTypedValue(tv parser.ITypedValueContext) (any, ArgumentType, bool, error) {
switch typedNode := tv.(type) {
case *parser.TypedStringContext:
userType := "[s]"
strictTypeCheck := false
if ann := typedNode.TypeAnnotation(); ann != nil {
userType = ann.GetText()
strictTypeCheck = true
}
value, argType, err := v.applyUserType(unquoteString(typedNode.STRING().GetText()), userType)
return value, argType, strictTypeCheck, err
case *parser.TypedDoubleContext:
userType := "[f64]"
strictTypeCheck := false
if ann := typedNode.TypeAnnotation(); ann != nil {
userType = ann.GetText()
strictTypeCheck = true
}
value, argType, err := v.applyUserType(typedNode.DOUBLE().GetText(), userType)
return value, argType, strictTypeCheck, err
case *parser.TypedIntegerContext:
userType := "[i64]"
strictTypeCheck := false
if ann := typedNode.TypeAnnotation(); ann != nil {
userType = ann.GetText()
strictTypeCheck = true
}
value, argType, err := v.applyUserType(typedNode.GetText(), userType)
return value, argType, strictTypeCheck, err
default:
return nil, ArgTypeUnknown, false, ErrorUnknownDecimalOperator
}
}
// applyUserType parses rawVal (string) according to the user-specified annotation in userType
// (e.g. "f64", "i32", etc.) then returns the typed value plus its ArgumentType.
func (v *queryVisitor) applyUserType(rawVal string, userType string) (any, ArgumentType, error) {
if len(userType) >= 2 && strings.HasPrefix(userType, "[") && strings.HasSuffix(userType, "]") {
userType = userType[1 : len(userType)-1] // e.g. "f64" or "i64"
}
switch userType {
case "f64":
fl, err := strconv.ParseFloat(rawVal, 64)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return fl, ArgTypeFloat64, nil
case "d":
dec, _ := decimal.NewFromString(fmt.Sprint(rawVal))
return dec, ArgTypeDecimal, nil
case "i":
i, err := strconv.Atoi(rawVal)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return i, ArgTypeInteger, nil
case "ui":
u64, err := strconv.ParseUint(rawVal, 10, 64)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return uint(u64), ArgTypeUnsignedInteger, nil
case "i64":
i64, err := strconv.ParseInt(rawVal, 10, 64)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return i64, ArgTypeInteger64, nil
case "ui64":
u64, err := strconv.ParseUint(rawVal, 10, 64)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return u64, ArgTypeUnsignedInteger64, nil
case "ui32":
u32, err := strconv.ParseUint(rawVal, 10, 32)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return uint32(u32), ArgTypeUnsignedInteger32, nil
case "i32":
i32, err := strconv.ParseInt(rawVal, 10, 32)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return int32(i32), ArgTypeInteger32, nil
case "s":
return rawVal, ArgTypeString, nil
case "f32":
fl, err := strconv.ParseFloat(rawVal, 32)
if err != nil {
return nil, ArgTypeUnknown, ErrorInvalidValue
}
return float32(fl), ArgTypeFloat32, nil
default:
return nil, ArgTypeUnknown, ErrorUnknownType
}
}
// parseValue handles the different parse-tree "Value" possibilities:
// typedVal, boolean, null, listOfInts, listOfDoubles, listOfStrings.
//
// If typedVal has a user annotation (e.g. [f64]"123.45"), it enforces strictTypeCheck for Evaluate.
func (v *queryVisitor) parseValue(valCtx parser.IValueContext) (any, ArgumentType, bool, error) {
switch node := valCtx.(type) {
case *parser.TypedValContext:
return v.parseTypedValue(node.TypedValue())
case *parser.BooleanContext:
txt := strings.ToLower(valCtx.GetText())
if txt == "true" {
return true, ArgTypeBoolean, true, nil
}
return false, ArgTypeBoolean, true, nil
case *parser.NullContext:
return nil, ArgTypeNull, false, nil
case *parser.ListOfIntsContext:
return valCtx.GetText(), ArgTypeList, false, nil
case *parser.ListOfDoublesContext:
return valCtx.GetText(), ArgTypeList, false, nil
case *parser.ListOfStringsContext:
return valCtx.GetText(), ArgTypeList, false, nil
default:
return "", ArgTypeUnknown, false, ErrorInvalidValue
}
}
// unquoteString removes the surrounding quotes of a string literal, plus minimal un-escaping.
func unquoteString(s string) string {
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
inner := s[1 : len(s)-1]
inner = strings.ReplaceAll(inner, `\"`, `"`)
inner = strings.ReplaceAll(inner, `\\`, `\`)
return inner
}
return s
}