-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisit.go
126 lines (113 loc) · 3.41 KB
/
visit.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
package rule
import (
"github.com/antlr4-go/antlr/v4"
parser "github.com/sky93/go-rule/internal/antlr4"
"strings"
)
// queryVisitor is the main AST visitor used by ParseQuery to build an exprTree of logical expressions.
//
// See parse.go for queryVisitor's parseValue / parseTypedValue and other helper methods.
type queryVisitor struct {
antlr.ParseTreeVisitor
parameters []Parameter
parser.BaseSCIMQueryVisitor
}
// visitRoot is an entry point for the parse tree's RootContext, returning an exprTree or error.
func (v *queryVisitor) visitRoot(ctx *parser.RootContext) (any, error) {
return v.visit(ctx.Query())
}
// visit is a dispatcher that calls one of the typed visit methods below, based on the parse node type.
func (v *queryVisitor) visit(ctx parser.IQueryContext) (any, error) {
switch actual := ctx.(type) {
case *parser.ParenExpContext:
return v.visitParenExp(actual)
case *parser.LogicalExpContext:
return v.visitLogicalExp(actual)
case *parser.PresentExpContext:
return v.visitPresentExp(actual)
case *parser.CompareExpContext:
return v.visitCompareExp(actual)
default:
return nil, ErrorInvalidExpression
}
}
// visitParenExp handles parentheses, e.g. (someExp). A NOT token may prefix the group.
func (v *queryVisitor) visitParenExp(ctx *parser.ParenExpContext) (*exprTree, error) {
sub, err := v.visit(ctx.Query())
if err != nil {
return nil, err
}
subExp := sub.(*exprTree)
notToken := ctx.NOT()
if notToken != nil {
subExp.not = !subExp.not
}
return subExp, nil
}
// visitPresentExp handles a "pr" operator, e.g. "attribute pr" meaning "attribute is present".
func (v *queryVisitor) visitPresentExp(ctx *parser.PresentExpContext) (*exprTree, error) {
name := v.getAttrName(ctx.AttrPath())
p := Parameter{
id: len(v.parameters),
Name: name,
InputType: Expression,
operator: "pr",
}
v.parameters = append(v.parameters, p)
return &exprTree{param: &p}, nil
}
// visitLogicalExp handles expressions joined by "and" / "or", e.g. "query and query".
func (v *queryVisitor) visitLogicalExp(ctx *parser.LogicalExpContext) (*exprTree, error) {
leftAny, err := v.visit(ctx.Query(0))
if err != nil {
return nil, err
}
rightAny, err := v.visit(ctx.Query(1))
if err != nil {
return nil, err
}
leftNode := leftAny.(*exprTree)
rightNode := rightAny.(*exprTree)
op := strings.ToLower(ctx.LOGICAL_OPERATOR().GetText())
return &exprTree{
op: op,
left: leftNode,
right: rightNode,
}, nil
}
// visitCompareExp handles a single comparison: (attrPath|functionCall) operator value
func (v *queryVisitor) visitCompareExp(ctx *parser.CompareExpContext) (*exprTree, error) {
isFunc := ctx.AttrPath().FunctionCall() != nil
var name string
var err error
var funcArgs []FunctionArgument
if isFunc {
name, funcArgs, err = v.parseFunctionCall(ctx.AttrPath().FunctionCall())
if err != nil {
return nil, err
}
} else {
name = v.getAttrName(ctx.AttrPath())
}
opText := strings.ToLower(ctx.GetOp().GetText())
valCtx := ctx.Value()
val, valType, strict, err := v.parseValue(valCtx)
if err != nil {
return nil, err
}
p := Parameter{
id: len(v.parameters),
Name: name,
operator: opText,
compareValue: val,
InputType: Expression,
Expression: valType,
strictTypeCheck: strict,
}
if isFunc {
p.InputType = FunctionCall
p.FunctionArguments = funcArgs
}
v.parameters = append(v.parameters, p)
return &exprTree{param: &p}, nil
}