-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.go
130 lines (111 loc) · 2.94 KB
/
utils.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
/*
Copyright 2021 The tKeel Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tdtl
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/tkeel-io/tdtl/parser"
)
//func Parse(expr string) Expr {
// ex, err := ParseWithError(expr)
// if err != nil {
// return nil
// }
// return ex
//}
func Parse(expr string) (Expr, error) {
parse, listener := parse(expr)
antlr.ParseTreeWalkerDefault.Walk(listener, parse.Root())
err := listener.error()
if err != nil {
return nil, err
}
return listener.Expr(), nil
}
func ParseField(expr string) (Expr, error) {
parse, listener := parse(expr)
antlr.ParseTreeWalkerDefault.Walk(listener, parse.Fields())
return listener.Expr(), listener.error()
}
func ParseFilter(expr string) (Expr, error) {
parse, listener := parse(expr)
antlr.ParseTreeWalkerDefault.Walk(listener, parse.Filter())
return listener.Expr(), listener.error()
}
func ParseExpr(expr string) (Expr, error) {
parse, listener := parse(expr)
antlr.ParseTreeWalkerDefault.Walk(listener, parse.Expr())
err := listener.error()
if err != nil {
return nil, err
}
return listener.Expr(), nil
}
func parse(expr string) (*parser.TDTLParser, *TDTLListener) {
//expr = strings.ReplaceAll(expr, "\n", " ")
// Setup the input
is := antlr.NewInputStream(expr)
// Create the Lexer
lexer := parser.NewTDTLLexer(is)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
// Create the Parser
parse := parser.NewTDTLParser(stream)
parse.RemoveErrorListeners()
// Finally parseField the expression (by walking the tree)
var listener TDTLListener
parse.AddErrorListener(&listener)
return parse, &listener
}
func ParseFunc(x Expr) []*CallExpr {
ret := &callList{make([]*CallExpr, 0)}
ret.walkFunc(x)
return ret.list
}
type callList struct {
list []*CallExpr
}
func (c *callList) walkFunc(x Expr) {
switch x := x.(type) {
case *SelectStatementExpr:
c.walkFunc(x.fields)
c.walkFunc(x.filter)
case FieldsExpr:
for i, n := 0, len(x); i < n; i++ {
if elem := x[i]; true {
c.walkFunc(elem)
}
}
case *FieldExpr:
c.walkFunc(x.exp)
case *FilterExpr:
c.walkFunc(x.exp)
case *BinaryExpr:
c.walkFunc(x.LHS)
c.walkFunc(x.RHS)
case *SwitchExpr:
c.walkFunc(x.exp)
for i, n := 0, len(x.list); i < n; i++ {
elem := x.list[i]
c.walkFunc(x.exp)
c.walkFunc(elem.when)
c.walkFunc(elem.then)
}
case *CaseExpr:
c.walkFunc(x.then)
case *CallExpr:
c.list = append(c.list, x)
default:
// default
fmt.Printf("%v", x)
}
}