-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtsl.go
196 lines (175 loc) · 5.27 KB
/
tsl.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
// Copyright 2018 Yaacov Zamir <[email protected]>
// and other contributors.
//
// 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.
// //go:generate bash ../parser/build.sh
package tsl
/*
#include <stdlib.h>
#include "tsl.h"
*/
import "C"
import (
"time"
"unsafe"
)
// TSLNode represents an tree search language AST (Abstract Syntax Tree) node
type TSLNode struct {
cNode *C.ast_node
}
// TSLExpressionOp represents both unary and binary operations
type TSLExpressionOp struct {
Operator Operator
Left, Right *TSLNode
}
// TSLArrayLiteral represents an array of nodes
type TSLArrayLiteral struct {
Values []*TSLNode
}
// ParseTSL parses a TSL expression and returns the AST root node
func ParseTSL(input string) (*TSLNode, error) {
cInput := C.CString(input)
defer C.free(unsafe.Pointer(cInput))
cNode := C.parse_input_string(cInput)
if cNode == nil {
errStr := C.GoString(C.get_error_string())
errPos := int(C.get_error_position())
errCtx := C.GoString(C.get_input_string_at_error())
// Cleanup parser memory after getting error information
C.cleanup_parser_memory()
return nil, &SyntaxError{
Message: errStr,
Position: errPos,
Context: errCtx,
Input: input,
}
}
// Cleanup parser memory after successful parse
C.cleanup_parser_memory()
return &TSLNode{cNode: cNode}, nil
}
// Free releases the memory allocated for the AST
func (n *TSLNode) Free() {
if n.cNode != nil {
C.ast_free(n.cNode)
n.cNode = nil
}
}
// Clone creates a deep copy of the TSLNode and its children
func (n *TSLNode) Clone() *TSLNode {
if n == nil || n.cNode == nil {
return nil
}
return &TSLNode{cNode: C.ast_clone(n.cNode)}
}
// Type returns the type of the node
func (n *TSLNode) Type() Kind {
return Kind(n.cNode._type)
}
// Value returns the node's value based on its type
func (n *TSLNode) Value() interface{} {
switch n.Type() {
case KindBooleanLiteral:
return C.get_boolean_value(n.cNode) != 0
case KindNumericLiteral:
// Keep numeric values as float64 consistently
return float64(C.get_number_value(n.cNode))
case KindStringLiteral, KindIdentifier:
return C.GoString(C.get_string_value(n.cNode))
case KindDateLiteral, KindTimestampLiteral:
// Parse date strings into time.Time
dateStr := C.GoString(C.get_string_value(n.cNode))
if t, err := time.Parse(time.RFC3339, dateStr); err == nil {
return t
}
// Fallback to returning raw string if parsing fails
return dateStr
case KindBinaryExpr:
return TSLExpressionOp{
Operator: Operator(C.get_binary_op(n.cNode)),
Left: &TSLNode{cNode: C.get_binary_left(n.cNode)},
Right: &TSLNode{cNode: C.get_binary_right(n.cNode)},
}
case KindUnaryExpr:
return TSLExpressionOp{
Operator: Operator(C.get_unary_op(n.cNode)),
Left: nil,
Right: &TSLNode{cNode: C.get_unary_child(n.cNode)},
}
case KindArrayLiteral:
size := int(C.get_array_size(n.cNode))
values := make([]*TSLNode, size)
for i := 0; i < size; i++ {
values[i] = &TSLNode{cNode: C.get_array_element(n.cNode, C.int(i))}
}
return TSLArrayLiteral{Values: values}
case KindNullLiteral:
return "NULL"
default:
return nil
}
}
// DetachLeft safely detaches and returns the left child of a binary expression node
func (n *TSLNode) DetachLeft() *TSLNode {
if n == nil || n.Type() != KindBinaryExpr {
return nil
}
cNode := C.ast_detach_binary_left(n.cNode)
if cNode == nil {
return nil
}
return &TSLNode{cNode: cNode}
}
// DetachRight safely detaches and returns the right child of a binary expression node
func (n *TSLNode) DetachRight() *TSLNode {
if n == nil || n.Type() != KindBinaryExpr {
return nil
}
cNode := C.ast_detach_binary_right(n.cNode)
if cNode == nil {
return nil
}
return &TSLNode{cNode: cNode}
}
// DetachChild safely detaches and returns the child of a unary expression node
func (n *TSLNode) DetachChild() *TSLNode {
if n == nil || n.Type() != KindUnaryExpr {
return nil
}
cNode := C.ast_detach_unary_child(n.cNode)
if cNode == nil {
return nil
}
return &TSLNode{cNode: cNode}
}
// AttachLeft safely attaches a child node as the left child of a binary expression node
func (n *TSLNode) AttachLeft(child *TSLNode) bool {
if n == nil || child == nil || n.Type() != KindBinaryExpr {
return false
}
return C.ast_attach_binary_left(n.cNode, child.cNode) != 0
}
// AttachRight safely attaches a child node as the right child of a binary expression node
func (n *TSLNode) AttachRight(child *TSLNode) bool {
if n == nil || child == nil || n.Type() != KindBinaryExpr {
return false
}
return C.ast_attach_binary_right(n.cNode, child.cNode) != 0
}
// AttachChild safely attaches a child node to a unary expression node
func (n *TSLNode) AttachChild(child *TSLNode) bool {
if n == nil || child == nil || n.Type() != KindUnaryExpr {
return false
}
return C.ast_attach_unary_child(n.cNode, child.cNode) != 0
}