forked from tkeel-io/tdtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
42 lines (34 loc) · 848 Bytes
/
expression.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
package tdtl
import "github.com/antlr/antlr4/runtime/Go/antlr"
type Expression interface {
Eval(map[string]Node) Node
Sources() map[string][]string
}
type expr struct {
sources map[string][]string
extFunc map[string]ContextFunc
listener *TDTLListener
}
func NewExpr(sql string, extFunc map[string]ContextFunc) (Expression, error) {
parse, listener := parse(sql)
antlr.ParseTreeWalkerDefault.Walk(listener, parse.Field_elem())
err := listener.error()
if err != nil {
return nil, err
}
return &expr{
listener: listener,
sources: listener.sources,
extFunc: extFunc,
}, nil
}
func (e *expr) expr() Expr {
return e.listener.Expr()
}
func (e *expr) Eval(in map[string]Node) Node {
ctx := NewMapContext(in, e.extFunc)
return EvalRuleQL(ctx, e.expr())
}
func (e *expr) Sources() map[string][]string {
return e.sources
}