/
funcs.go
214 lines (153 loc) · 3.66 KB
/
funcs.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
package gslang
import (
"github.com/gsrpc/gslang/ast"
"github.com/gsrpc/gslang/lexer"
)
// Extra key list
const (
ExtraStartPos = "start"
ExtraEndPos = "end"
ExtraComment = "comment"
ExtraAnnotation = "annotation"
)
func _setNodePos(node ast.Node, start lexer.Position, end lexer.Position) {
node.SetExtra(ExtraStartPos, start)
node.SetExtra(ExtraEndPos, end)
}
func _AttachComment(node ast.Node, comment *ast.Comment) bool {
nodeStart, nodeEnd := Pos(node)
commentStart, commentEnd := Pos(comment)
if nodeStart.Lines == commentEnd.Lines ||
nodeStart.Lines == commentEnd.Lines+1 ||
nodeEnd.Lines == commentStart.Lines {
node.SetExtra(ExtraComment, comment)
return true
}
return false
}
func _AttachAnnotation(node ast.Node, annotations ...*ast.Annotation) {
anns := Annotations(node)
anns = append(anns, annotations...)
node.SetExtra(ExtraAnnotation, anns)
}
// Annotations .
func Annotations(node ast.Node) (anns []*ast.Annotation) {
val, ok := node.GetExtra(ExtraAnnotation)
if ok {
anns = val.([]*ast.Annotation)
}
return
}
// Annotations .
func _RemoveAnnotation(node ast.Node, annotation *ast.Annotation) {
anns := Annotations(node)
var newanns []*ast.Annotation
for _, ann := range anns {
if ann == annotation {
continue
}
newanns = append(newanns, ann)
}
node.SetExtra(ExtraAnnotation, anns)
}
// FindAnnotation .
func FindAnnotation(node ast.Node, name string) (*ast.Annotation, bool) {
val, ok := node.GetExtra(ExtraAnnotation)
if ok {
anns := val.([]*ast.Annotation)
for _, ann := range anns {
if ann.Type.Ref == nil {
continue
}
if ann.Type.Ref.FullName() == name {
return ann, true
}
}
}
return nil, false
}
// FindAnnotations .
func FindAnnotations(node ast.Node, name string) (retval []*ast.Annotation) {
val, ok := node.GetExtra(ExtraAnnotation)
if ok {
anns := val.([]*ast.Annotation)
for _, ann := range anns {
if ann.Type.Ref == nil {
continue
}
if ann.Type.Ref.FullName() == name {
retval = append(retval, ann)
}
}
}
return
}
// Pos .
func Pos(node ast.Node) (start lexer.Position, end lexer.Position) {
val, ok := node.GetExtra(ExtraStartPos)
if ok {
start = val.(lexer.Position)
}
val, ok = node.GetExtra(ExtraEndPos)
if ok {
end = val.(lexer.Position)
}
return
}
// EnumSize get enum binary size
func EnumSize(typeDecl ast.Type) int {
_, ok := FindAnnotation(typeDecl, "gslang.Flag")
if ok {
return 4
}
return 1
}
// EnumType get enum token type
func EnumType(typeDecl ast.Type) lexer.TokenType {
_, ok := FindAnnotation(typeDecl, "gslang.Flag")
if ok {
return lexer.KeyUInt32
}
return lexer.KeyByte
}
// IsBuiltin check if target type is builtin type
func IsBuiltin(typeDecl ast.Type) bool {
_, ok := typeDecl.(*ast.BuiltinType)
return ok
}
// IsVoid check if target type is builtin type Void
func IsVoid(typeDecl ast.Type) bool {
builtinType, ok := typeDecl.(*ast.BuiltinType)
if ok && builtinType.Type == lexer.KeyVoid {
return true
}
return false
}
// NotVoid check if target type is not builtin type Void
func NotVoid(typeDecl ast.Type) bool {
return !IsVoid(typeDecl)
}
// IsPOD check if target type is POD table
func IsPOD(typeDecl ast.Type) bool {
_, ok := FindAnnotation(typeDecl, "gslang.POD")
if ok {
return true
}
return false
}
// IsAsync check if target method is async method
func IsAsync(method *ast.Method) bool {
_, ok := FindAnnotation(method, "gslang.Async")
if ok {
return true
}
return false
}
// IsException check if target type is exception table
func IsException(typeDecl ast.Type) bool {
_, ok := FindAnnotation(typeDecl, "gslang.Exception")
if ok {
return true
}
return false
}