-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtplfuncs.go
320 lines (283 loc) · 6.41 KB
/
tplfuncs.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Apache License 2.0
//
//----------------------------------------
package main
import (
"bytes"
"html/template"
"strings"
"./ast"
)
func templateIsEmpty(s string) bool {
return s == ""
}
func templateCovType(s string) template.HTML {
if v, ok := typeDict[s]; ok {
return template.HTML(v)
}
return template.HTML(s)
}
// 不包含字符串的转换
func templateCovType2(s string) template.HTML {
if s == "string" {
if v, ok := typeDict["string2"]; ok {
return template.HTML(v)
}
return template.HTML(s)
}
if v, ok := typeDict[s]; ok {
return template.HTML(v)
}
return template.HTML(s)
}
func templateCovKeyWord(s string) string {
if v, ok := keyWordsDict[s]; ok {
return v
}
return s
}
func templateIsObject(s string) bool {
s = strings.TrimSpace(s)
if s == "" {
return false
}
_, ok := Objs[s]
return ok
}
func templateDec(val int) int {
return val - 1
}
func templateNextType(obj []ast.TType, idx int) ast.TType {
if idx+1 < len(obj) {
return obj[idx+1]
}
return ast.TType{}
}
func templatePrevType(obj []ast.TType, idx int) ast.TType {
if idx-1 > 0 {
//fmt.Println("index:", idx, obj[idx-1], obj[idx])
return obj[idx-1]
}
return ast.TType{}
}
func templateIsBaseObject(s string) bool {
return s == "TObject" || s == "TComponent" || s == "TControl" || s == "TWinControl"
}
func templateRMObjectT(s string) string {
if len(s) > 0 && s[0] == 'T' {
return s[1:]
}
return s
}
func templateParamsEmpty(ps []ast.TFuncParam) bool {
return len(ps) == 0
}
func templatePropGetName(fn ast.TFunction) string {
if !fn.IsMethod {
if strings.HasPrefix(fn.RealName, "Get") {
return strings.TrimPrefix(fn.RealName, "Get")
}
}
return fn.RealName
}
func recursive(name, mm string) bool {
name, ok := Objs[name] // 查基类
if !ok {
return true
} else {
if dict, ok := methodDict[name]; ok {
if _, ok := dict[mm]; ok {
return false
}
}
}
return recursive(Objs[name], mm)
}
func templateIsBaseMethod(className, method string) bool {
// 这里偷下懒吧,懒得改了
switch className {
case "TStrings", "TStringList":
if method == "Equals" {
return true
}
}
return recursive(className, method)
}
func templateText(s string) template.HTML {
return template.HTML(s)
}
func templateCPsZero(params []ast.TFuncParam) string {
if len(params) > 0 {
ns := ""
for i := 0; i < 12-len(params); i++ {
ns += " ,0"
}
return ns
}
return " ,0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0"
}
func templateDelDChar(s string) string {
if len(s) > 0 {
if s[0] == 'D' && !strings.Contains(s, "_Instance") {
return s[1:]
}
}
return s
}
func templateGetLastParam(pss []ast.TFuncParam) ast.TFuncParam {
if len(pss) == 0 {
return ast.TFuncParam{}
}
return pss[len(pss)-1]
}
func templateCanOutParam(mm ast.TFunction, idx int) bool {
if !mm.LastIsReturn {
return true
}
return idx < len(mm.Params)-1
}
func templateIsProp(mm ast.TFunction) bool {
if !mm.IsMethod {
return true
}
return false
}
//func templateIsGetter(mm ast.TFunction) bool {
// if !mm.IsMethod && strings.HasPrefix(mm.RealName, "Get") {
// return true
// }
// return false
//}
//
//func templateIsSetter(mm ast.TFunction) bool {
// if !mm.IsMethod && strings.HasPrefix(mm.RealName, "Set") {
// return true
// }
// return false
//}
//
//func templateGetRealName(mm ast.TFunction) string {
// if !mm.IsMethod && (strings.HasPrefix(mm.RealName, "Get") || strings.HasPrefix(mm.RealName, "Set")) {
// return mm.RealName[3:]
// }
// if mm.IsMethod && mm.IsOverload {
// return mm.OverloadName
// }
// return mm.RealName
//}
func templateGetRealName2(mm ast.TFunction) string {
if !mm.IsMethod && strings.HasPrefix(mm.RealName, "Get") {
return mm.RealName[3:]
}
return mm.RealName
}
func templateInStrArray(s string, args ...string) bool {
for _, l := range args {
if l == s {
return true
}
}
return false
}
func templateGetConstVal2(s string) string {
p1 := strings.Index(s, "(")
if p1 > 0 {
p2 := strings.Index(s, ")")
if p2 > 0 {
return s[p1+1 : p2]
}
}
return s
}
func templateIsIntf(s string) bool {
switch s {
case "TObject", "TComponent", "TControl", "TWinControl", "TStrings", "TStream", "TGraphic":
return true
}
return false
}
func templateGetIntfName(s string) string {
if templateIsIntf(s) {
return "I" + s[1:]
}
return s
}
func templateCovIntf(s string) string {
if len(s) > 0 && (s[0] == 'T' || s == "Exception") {
if s[0] == 'T' {
return "I" + s[1:]
} else {
return "I" + s
}
}
return s
}
func templateHaveFree(mts []ast.TFunction) bool {
for _, m := range mts {
if m.RealName == "Free" {
return true
}
}
return false
}
func templateFirstLowerCase(s string) string {
if len(s) > 0 {
return strings.ToLower(string(s[0])) + s[1:]
}
return s
}
func templateNewBuffer() *TMyBuffer {
b := new(TMyBuffer)
b.buff = bytes.NewBuffer(nil)
return b
}
//func templateUnescape(s string) string {
// return html.UnescapeString(s)
//}
func templateMultiply(v1, v2 int) int {
return v1 * v2
}
var templateFuncs = template.FuncMap{
"isEmpty": templateIsEmpty,
"covType": templateCovType,
"covType2": templateCovType2,
"isObject": templateIsObject,
"Dec": templateDec,
"covKeyword": templateCovKeyWord,
"nextType": templateNextType,
"prevType": templatePrevType,
"isBaseObj": templateIsBaseObject,
"rmObjectT": templateRMObjectT,
"paramsEmpty": templateParamsEmpty,
"propGetName": templatePropGetName,
"isBaseMethod": templateIsBaseMethod,
"html": templateText,
"cPsZero": templateCPsZero,
"delDChar": templateDelDChar,
"lastParam": templateGetLastParam,
"canOutParam": templateCanOutParam,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"trim": strings.TrimSpace,
"contains": strings.Contains,
"isProp": templateIsProp,
//"isGetter": templateIsGetter,
//"isSetter": templateIsSetter,
//"getRealName": templateGetRealName,
"inStrArray": templateInStrArray,
"getConstVal2": templateGetConstVal2,
"getIntfName": templateGetIntfName,
"isIntf": templateIsIntf,
//"getRealName2": templateGetRealName2,
"haveFree": templateHaveFree,
"fLowCase": templateFirstLowerCase,
"newBuffer": templateNewBuffer,
//"unescape": templateUnescape,
"multiply": templateMultiply,
"covIntf": templateCovIntf,
}
//Add, subtract, multiply, divide