-
Notifications
You must be signed in to change notification settings - Fork 59
/
CFunc.cls
576 lines (447 loc) · 16.4 KB
/
CFunc.cls
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CFunc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'Author: [email protected]
'Site: http://sandsprite.com
'
' this class is part of the refactoring code. it can really make your brain hurt.
Public OrgName As String
Public NewName As String
Public OrgArgs As New Collection
Public ArgCount As Long
Public OrgText As String
Public CleanText As String
Public Index As Long
Public OrgVars As New Collection
Public ParentForm As frmRefactor
Public Saved As Boolean
Public OverRides As New Collection
Public IsGlobal As Boolean
Public OverRideScript As String
Public UseOriginalText As Boolean
Public debugout As Boolean
Public UseOriginalFuncName As Boolean
Public logger As ListBox
Private tokenizedScript As String
Private my_tokens As New Collection
Function ApplyOverrides() As String
'ApplyOverrides = FinalReplace(tokenizedScript)
OverRideScript = Me.CleanText
For Each v In OverRides
tmp = Split(v, "->")
While InStr(OverRideScript, tmp(0)) > 0
OverRideScript = Replace(OverRideScript, tmp(0), tmp(1))
Wend
Next
ApplyOverrides = OverRideScript
End Function
Sub ResetParse()
Set OrgArgs = New Collection
Set OrgVars = New Collection
Set my_tokens = New Collection
End Sub
Sub ParseName()
x = Me.OrgText
p1 = InStr(x, "(")
p2 = InStr(x, ")")
sp = InStrRev(x, " ", p1) 'first space (between fun name and agrs)
Me.OrgName = Trim(Mid(x, sp, p1 - sp))
Me.NewName = "func_" & SafeFuncIndex(Me.Index)
If Not Me.logger Is Nothing Then logger.AddItem "Found Function " & Me.OrgName & " -> " & Me.NewName
End Sub
Sub ParseAsGlobal()
Dim newVar As String
x = Me.OrgText
debugout = True
'On Error GoTo hell
body = x
tmp = Split(body, vbCrLf)
For Each Y In tmp
a = InStr(Y, "var ")
If a > 0 Then
b1 = InStr(a + 4, Y, "-") 'todo change to scan for next nontext
b2 = InStr(a + 4, Y, "+")
b3 = InStr(a + 4, Y, "^")
b4 = InStr(a + 4, Y, "=")
'b5 = InStr(a + 4, y, " ")
b = GetLowest(b1, b2, b3, b4)
If b > 0 Then
newVar = Trim(Mid(Y, a + 4, b - a - 4))
If Not tokenExists(newVar) Then
OrgVars.Add newVar
AddToken newVar
If debugout And Not Me.logger Is Nothing Then logger.AddItem "Adding g orgvar: " & Trim(Mid(Y, a + 4, b - a - 4))
End If
End If
End If
Next
For i = 0 To UBound(tmp) 'now we replace tokens line by line by parsing each line
tmp(i) = ReplaceTokens(tmp(i))
Next
tokenizedScript = Join(tmp, vbCrLf)
For i = 0 To UBound(tmp)
tmp(i) = FinalReplace(tmp(i))
Next
x = Join(tmp, vbCrLf)
hell:
Me.CleanText = x
End Sub
Sub ParseSelf() 'as function w/prototype
Dim newVar As String
If Me.IsGlobal Then Exit Sub
x = Me.OrgText
debugout = True
'On Error GoTo hell
p1 = InStr(x, "(")
p2 = InStr(x, ")")
sp = InStrRev(x, " ", p1) 'first space (between fun name and agrs)
n = Mid(x, p1 + 1, p2 - p1 - 1) 'args between parathensis
tmp = Split(n, ",")
If Not AryIsEmpty(tmp) Then
For Each Y In tmp
If Trim(Len(Y)) > 0 Then
OrgArgs.Add Trim(Y)
End If
Next
End If
'now we parse the function body line by line to extract explicitly var declared variables
body = Mid(x, p2 + 1) 'from first { to last } --> BUG CHECK: JUST ADDED THE +1 HERE !!
tmp = Split(body, vbCrLf)
For Each Y In tmp
DoEvents
a = InStr(Y, "var ")
If a > 0 Then
b1 = InStr(a + 4, Y, "-") 'todo change to scan for next nontext
b2 = InStr(a + 4, Y, "+")
b3 = InStr(a + 4, Y, "^")
b4 = InStr(a + 4, Y, "=")
b = GetLowest(b1, b2, b3, b4)
If b > 0 Then
newVar = Trim(Mid(Y, a + 4, b - a - 4))
If Not tokenExists(newVar) Then
OrgVars.Add newVar
AddToken newVar
If debugout And Not logger Is Nothing Then logger.AddItem Me.OrgName & " ParseSelf adding new token: " & newVar
End If
End If
End If
Next
'this is to general?
head = Replace(Mid(x, 1, p2), OrgName, Me.NewName) 'clean function name first
args = Mid(x, p1 + 1, p2 - p1 - 1)
newargs = FinalReplace(ReplaceTokens(args))
For i = 0 To UBound(tmp) 'now we replace tokens line by line by parsing each line
tmp(i) = ReplaceTokens(tmp(i))
Next
tokenizedScript = Replace(head, args, newargs) & Join(tmp, vbCrLf)
For i = 0 To UBound(tmp)
tmp(i) = FinalReplace(tmp(i))
Next
x = Replace(head, args, newargs) & Join(tmp, vbCrLf)
hell:
Me.CleanText = x
End Sub
Private Function FinalReplace(ByVal x)
Dim overridden_varName
Dim aIndex As Long
For aIndex = 0 To 254
If InStr(x, "__arg_" & aIndex) > 0 Then
x = Replace(x, "__arg_" & aIndex, "arg_" & aIndex)
End If
If InStr(x, "__var_" & aIndex) > 0 Then
If IsGlobal Then
x = Replace(x, "__var_" & aIndex, "gvar_" & aIndex)
Else
x = Replace(x, "__var_" & aIndex, "v" & aIndex)
End If
End If
If InStr(x, "__func_" & SafeFuncIndex(aIndex)) > 0 Then
x = Replace(x, "__func_" & SafeFuncIndex(aIndex), "func_" & SafeFuncIndex(aIndex))
End If
Next
FinalReplace = x
End Function
Function OverrideExists(varIndex) As String
Dim x
For Each x In OverRides
tmp = Split(x, "->")
vnew = IIf(Me.IsGlobal, "gvar_" & varIndex, "v" & varIndex)
If tmp(0) = vnew Then
OverrideExists = tmp(1)
Exit Function
End If
Next
End Function
Private Function ReplaceTokens(ByVal l, Optional startPos As Long = 1) 'l = a line of data
Dim i As Long
Dim buf As String
Dim tokenstart
Dim tokenend
Dim inQuotes As Boolean
Dim outOfQuotes As Boolean
Dim f As CFunc
Dim debugout As Boolean
debugout = False
If debugout And startPos = 1 Then Debug.Print "In ReplaceTokens line: " & l
'If InStr(l, "sc.replace(re, kc)") > 0 Then Stop
If Len(l) = 0 Then Exit Function
'special chars 5C=\ 27=' 22="
i = startPos
While i < Len(l) + 1 'extract & replace tokens as found
DoEvents
c = Mid(l, i, 1)
c2 = Empty
If outOfQuotes Then
outOfQuotes = False
inQuotes = False
End If
If i + 1 <= Len(l) + 1 Then
c2 = Mid(l, i + 1, 1)
End If
If c = "'" Or c = """" Then
If inQuotes = True Then
If c2 <> "\" Then outOfQuotes = True
Else
inQuotes = True
End If
End If
If Not inQuotes Then
If isTextRange(Asc(c), IIf(Len(buf) = 0, False, True)) Then
buf = buf & c
Else
'we have a complete token now
If Len(buf) = 0 Or buf = "var" Then GoTo skip_token
'If buf = "replace" Then Stop
If debugout Then Debug.Print "complete token: " & buf
'If Me.IsGlobal And buf = s Then Stop
j = 0
For Each mvar In Me.OrgVars
If buf = mvar Then
a = Empty
b = Empty
newtoken = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
newtoken = "__var_" & j
l = a & newtoken & b
ReplaceTokens = ReplaceTokens(l, Len(a) + Len(newtoken))
Exit Function
End If
j = j + 1
Next
j = 0
For Each mvar In Me.OrgArgs
If buf = mvar Then
a = Empty
b = Empty
newtoken = Empty
If tokenstart > 1 Then a = Mid(l, 1, tokenstart)
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
newtoken = "__arg_" & j
l = a & newtoken & b
ReplaceTokens = ReplaceTokens(l, Len(a) + Len(newtoken))
Exit Function
End If
j = j + 1
Next
If Me.IsGlobal = False Then
j = 0
For Each mvar In Me.ParentForm.global_script.OrgVars
If buf = mvar Then
a = Empty
b = Empty
newtoken = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
newtoken = "__gvar_" & j
l = a & newtoken & b
ReplaceTokens = ReplaceTokens(l, Len(a) + Len(newtoken))
Exit Function
End If
j = j + 1
Next
End If
For Each f In Me.ParentForm.funcs
If buf = f.OrgName Then
a = Empty
b = Empty
newtoken = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
newtoken = "__func_" & SafeFuncIndex(f.Index)
l = a & newtoken & b
ReplaceTokens = ReplaceTokens(l, Len(a) + Len(newtoken))
Exit Function
End If
Next
skip_token:
buf = Empty
tokenstart = i
End If
End If
i = i + 1
Wend
'now do final replace
For Each mvar In Me.OrgVars
If buf = mvar Then
a = Empty
b = Empty
newtoken = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
Else
Debug.Print "?"
End If
l = a & "__var_" & j & b
End If
Next
j = 0
For Each mvar In Me.OrgArgs
If buf = mvar Then
a = Empty
b = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
l = a & "__arg_" & j & b
End If
j = j + 1
Next
If Me.IsGlobal = False Then
j = 0
For Each mvar In Me.ParentForm.global_script.OrgVars
If buf = mvar Then
a = Empty
b = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
l = a & "__gvar_" & j & b
End If
j = j + 1
Next
End If
For Each f In Me.ParentForm.funcs
If buf = f.OrgName Then
a = Empty
b = Empty
If tokenstart > 1 Then
a = Mid(l, 1, tokenstart)
End If
If tokenstart + Len(buf) + 1 < Len(l) Then
b = Mid(l, tokenstart + Len(buf) + 1)
End If
l = a & "__func_" & SafeFuncIndex(f.Index) & b
End If
Next
If debugout Then Debug.Print "Ending ReplaceTOkens final line: " & l
ReplaceTokens = l
End Function
Function SafeFuncIndex(x As Long) As String
SafeFuncIndex = IIf(x < 10, "0" & x, x)
End Function
Function isTextRange(x As Integer, Optional includeNumeric As Boolean = False) As Boolean
'61=a 7A=z 41=A 5A=Z 5F=_
'variables an not start with numeric
If includeNumeric Then If x >= &H30 And x <= &H39 Then isTextRange = True '0-9
If x >= &H61 And x <= &H7A Then isTextRange = True 'a-z
If x >= &H41 And x <= &H5A Then isTextRange = True 'A-Z
If x = &H5F Then isTextRange = True '_
End Function
Function SortVars(ByVal c As Collection) As Collection
Dim n As New Collection
longest = 0
longindex = 0
i = 0
While c.Count > 0
longest = 0
For i = 1 To c.Count
x = Len(c(i))
If x > longest Then
longest = x
longindex = i
End If
Next
n.Add c(longindex)
c.Remove longindex
Wend
Set SortVars = n
End Function
Function tokenExists(t As String) As Boolean
On Error GoTo hell
x = my_tokens("token:" & t)
tokenExists = True
Exit Function
hell:
End Function
Function AddToken(t As String) As Boolean
On Error GoTo hell
my_tokens.Add t, "token:" & t
AddToken = True
Exit Function
hell:
End Function
Function GetLowest(ParamArray args())
Dim lowest As Long
lowest = -1
For Each Y In args
If Y > 0 Then
If lowest = -1 Then lowest = Y
If Y < lowest Then lowest = Y
End If
Next
GetLowest = lowest
End Function
Function GetCount(s, c) As Long
On Error Resume Next
tmp = Split(s, c)
GetCount = UBound(tmp)
End Function
Private Function AryIsEmpty(ary) As Boolean
On Error GoTo oops
i = UBound(ary) '<- throws error if not initalized
AryIsEmpty = False
Exit Function
oops: AryIsEmpty = True
End Function
Private Sub push(ary, Value) 'this modifies parent ary object
On Error GoTo init
x = UBound(ary) '<-throws Error If Not initalized
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary)) = Value
Exit Sub
init: ReDim ary(0): ary(0) = Value
End Sub