vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
//! # `InStrRev` Function
//!
//! Returns the position of an occurrence of one string within another, from the end of string.
//!
//! ## Syntax
//!
//! ```vb
//! InStrRev(stringcheck, stringmatch[, start[, compare]])
//! ```
//!
//! ## Parameters
//!
//! - `stringcheck` (Required): `String` expression being searched
//! - `stringmatch` (Required): `String` expression to search for
//! - `start` (Optional): Numeric expression that sets the starting position for each search. If omitted, -1 is used, which means the search begins at the last character position. If start contains Null, an error occurs
//! - `compare` (Optional): Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed
//!
//! ### Compare Parameter Values
//!
//! - `vbUseCompareOption` (-1): Performs a comparison using the setting of the `Option Compare` statement
//! - `vbBinaryCompare` (0): Performs a binary comparison (case-sensitive)
//! - `vbTextCompare` (1): Performs a textual comparison (case-insensitive)
//! - `vbDatabaseCompare` (2): Microsoft Access only. Performs a comparison based on information in your database
//!
//! ## Return Value
//!
//! Returns a Long:
//! - If stringcheck is zero-length: Returns 0
//! - If stringcheck is `Null`: Returns `Null`
//! - If stringmatch is zero-length: Returns start
//! - If stringmatch is `Null`: Returns `Null`
//! - If stringmatch is not found: Returns 0
//! - If stringmatch is found within stringcheck: Returns position where match begins
//! - If start is greater than length of stringcheck: Search begins at the last character position
//!
//! ## Remarks
//!
//! The `InStrRev` function searches from the end of the string backward:
//!
//! - Searches from right to left (reverse direction)
//! - Returns the character position of the LAST occurrence (1-based indexing)
//! - Default start position is -1 (end of string) if omitted
//! - Search is case-sensitive by default (`vbBinaryCompare`)
//! - Use `vbTextCompare` for case-insensitive searching
//! - Unlike `InStr`, start parameter comes after the strings being compared
//! - The returned position is still counted from the beginning (1-based), not from the end
//! - Useful for finding file extensions, last delimiters, etc.
//! - More efficient than repeated `InStr` calls for finding last occurrence
//! - The compare parameter affects performance (binary is faster than text)
//! - Returns 0 if substring not found (test with > 0 for found)
//!
//! ## Typical Uses
//!
//! 1. **Find Last Occurrence**: Locate the last instance of a substring
//! 2. **File Extensions**: Extract file extension by finding last dot
//! 3. **Path Parsing**: Find last path separator in file paths
//! 4. **File Names**: Extract filename from full path
//! 5. **Domain Extraction**: Find last dot in domain names
//! 6. **String Truncation**: Find last delimiter for truncation
//! 7. **Reverse Parsing**: Parse from end of string
//! 8. **Last Word Extraction**: Find last space in sentences
//!
//! ## Basic Usage Examples
//!
//! ```vb
//! ' Example 1: Find last occurrence
//! Dim text As String
//! Dim pos As Long
//! text = "apple,banana,apple,orange"
//! pos = InStrRev(text, "apple")
//! Debug.Print pos  ' Prints: 14 (last apple)
//!
//! ' Example 2: Get file extension
//! Dim fileName As String
//! Dim pos As Long
//! Dim extension As String
//! fileName = "document.backup.txt"
//! pos = InStrRev(fileName, ".")
//! extension = Mid$(fileName, pos)
//! Debug.Print extension  ' Prints: .txt
//!
//! ' Example 3: Extract filename from path
//! Dim fullPath As String
//! Dim pos As Long
//! Dim fileName As String
//! fullPath = "C:\Projects\MyApp\MainForm.frm"
//! pos = InStrRev(fullPath, "\")
//! fileName = Mid$(fullPath, pos + 1)
//! Debug.Print fileName  ' Prints: MainForm.frm
//!
//! ' Example 4: Search with start position
//! Dim text As String
//! Dim pos As Long
//! text = "one,two,three,four"
//! pos = InStrRev(text, ",", 10)  ' Search up to position 10
//! Debug.Print pos  ' Prints: 8 (comma after "two")
//! ```
//!
//! ## Common Patterns
//!
//! ```vb
//! ' Pattern 1: Get file extension
//! Function GetFileExtension(fileName As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(fileName, ".")
//!     If pos > 0 Then
//!         GetFileExtension = Mid$(fileName, pos + 1)
//!     Else
//!         GetFileExtension = ""
//!     End If
//! End Function
//!
//! ' Pattern 2: Get filename from path
//! Function GetFileNameFromPath(fullPath As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(fullPath, "\")
//!     If pos = 0 Then pos = InStrRev(fullPath, "/")
//!     
//!     If pos > 0 Then
//!         GetFileNameFromPath = Mid$(fullPath, pos + 1)
//!     Else
//!         GetFileNameFromPath = fullPath
//!     End If
//! End Function
//!
//! ' Pattern 3: Get directory from path
//! Function GetDirectoryFromPath(fullPath As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(fullPath, "\")
//!     If pos = 0 Then pos = InStrRev(fullPath, "/")
//!     
//!     If pos > 0 Then
//!         GetDirectoryFromPath = Left$(fullPath, pos - 1)
//!     Else
//!         GetDirectoryFromPath = ""
//!     End If
//! End Function
//!
//! ' Pattern 4: Remove file extension
//! Function RemoveFileExtension(fileName As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(fileName, ".")
//!     If pos > 0 Then
//!         RemoveFileExtension = Left$(fileName, pos - 1)
//!     Else
//!         RemoveFileExtension = fileName
//!     End If
//! End Function
//!
//! ' Pattern 5: Get last word
//! Function GetLastWord(text As String) As String
//!     Dim pos As Long
//!     
//!     text = Trim$(text)
//!     pos = InStrRev(text, " ")
//!     
//!     If pos > 0 Then
//!         GetLastWord = Mid$(text, pos + 1)
//!     Else
//!         GetLastWord = text
//!     End If
//! End Function
//!
//! ' Pattern 6: Truncate at last delimiter
//! Function TruncateAtLastDelimiter(text As String, delimiter As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(text, delimiter)
//!     If pos > 0 Then
//!         TruncateAtLastDelimiter = Left$(text, pos - 1)
//!     Else
//!         TruncateAtLastDelimiter = text
//!     End If
//! End Function
//!
//! ' Pattern 7: Get parent directory
//! Function GetParentDirectory(path As String) As String
//!     Dim pos As Long
//!     
//!     ' Remove trailing slash if present
//!     If Right$(path, 1) = "\" Then
//!         path = Left$(path, Len(path) - 1)
//!     End If
//!     
//!     pos = InStrRev(path, "\")
//!     If pos > 0 Then
//!         GetParentDirectory = Left$(path, pos - 1)
//!     Else
//!         GetParentDirectory = ""
//!     End If
//! End Function
//!
//! ' Pattern 8: Extract domain without subdomain
//! Function GetRootDomain(url As String) As String
//!     Dim domain As String
//!     Dim pos As Long
//!     Dim dotPos As Long
//!     
//!     ' Extract domain first (simplified)
//!     pos = InStr(url, "://")
//!     If pos > 0 Then
//!         domain = Mid$(url, pos + 3)
//!     Else
//!         domain = url
//!     End If
//!     
//!     ' Remove path
//!     pos = InStr(domain, "/")
//!     If pos > 0 Then
//!         domain = Left$(domain, pos - 1)
//!     End If
//!     
//!     ' Find last two parts (domain.tld)
//!     dotPos = InStrRev(domain, ".")
//!     If dotPos > 0 Then
//!         ' Find second-to-last dot
//!         pos = InStrRev(domain, ".", dotPos - 1)
//!         If pos > 0 Then
//!             GetRootDomain = Mid$(domain, pos + 1)
//!         Else
//!             GetRootDomain = domain
//!         End If
//!     Else
//!         GetRootDomain = domain
//!     End If
//! End Function
//!
//! ' Pattern 9: Replace last occurrence
//! Function ReplaceLastOccurrence(text As String, findText As String, replaceText As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(text, findText)
//!     If pos > 0 Then
//!         ReplaceLastOccurrence = Left$(text, pos - 1) & replaceText & _
//!                                 Mid$(text, pos + Len(findText))
//!     Else
//!         ReplaceLastOccurrence = text
//!     End If
//! End Function
//!
//! ' Pattern 10: Get text before last occurrence
//! Function GetBeforeLastOccurrence(text As String, delimiter As String) As String
//!     Dim pos As Long
//!     
//!     pos = InStrRev(text, delimiter)
//!     If pos > 0 Then
//!         GetBeforeLastOccurrence = Left$(text, pos - 1)
//!     Else
//!         GetBeforeLastOccurrence = text
//!     End If
//! End Function
//! ```
//!
//! ## Advanced Usage Examples
//!
//! ```vb
//! ' Example 1: Path utilities class
//! Public Class PathUtils
//!     Public Function GetFileName(fullPath As String) As String
//!         Dim pos As Long
//!         pos = InStrRev(fullPath, "\")
//!         If pos = 0 Then pos = InStrRev(fullPath, "/")
//!         
//!         If pos > 0 Then
//!             GetFileName = Mid$(fullPath, pos + 1)
//!         Else
//!             GetFileName = fullPath
//!         End If
//!     End Function
//!     
//!     Public Function GetFileNameWithoutExtension(fullPath As String) As String
//!         Dim fileName As String
//!         Dim pos As Long
//!         
//!         fileName = GetFileName(fullPath)
//!         pos = InStrRev(fileName, ".")
//!         
//!         If pos > 0 Then
//!             GetFileNameWithoutExtension = Left$(fileName, pos - 1)
//!         Else
//!             GetFileNameWithoutExtension = fileName
//!         End If
//!     End Function
//!     
//!     Public Function GetExtension(fullPath As String) As String
//!         Dim pos As Long
//!         pos = InStrRev(fullPath, ".")
//!         
//!         If pos > 0 Then
//!             GetExtension = Mid$(fullPath, pos + 1)
//!         Else
//!             GetExtension = ""
//!         End If
//!     End Function
//!     
//!     Public Function GetDirectory(fullPath As String) As String
//!         Dim pos As Long
//!         pos = InStrRev(fullPath, "\")
//!         If pos = 0 Then pos = InStrRev(fullPath, "/")
//!         
//!         If pos > 0 Then
//!             GetDirectory = Left$(fullPath, pos - 1)
//!         Else
//!             GetDirectory = ""
//!         End If
//!     End Function
//!     
//!     Public Function ChangeExtension(fullPath As String, newExtension As String) As String
//!         Dim pos As Long
//!         pos = InStrRev(fullPath, ".")
//!         
//!         If pos > 0 Then
//!             ChangeExtension = Left$(fullPath, pos - 1) & "." & newExtension
//!         Else
//!             ChangeExtension = fullPath & "." & newExtension
//!         End If
//!     End Function
//! End Class
//!
//! ' Example 2: URL parser
//! Public Class URLParser
//!     Private m_url As String
//!     
//!     Public Sub Parse(url As String)
//!         m_url = url
//!     End Sub
//!     
//!     Public Function GetProtocol() As String
//!         Dim pos As Long
//!         pos = InStr(m_url, "://")
//!         
//!         If pos > 0 Then
//!             GetProtocol = Left$(m_url, pos - 1)
//!         Else
//!             GetProtocol = ""
//!         End If
//!     End Function
//!     
//!     Public Function GetDomain() As String
//!         Dim startPos As Long
//!         Dim endPos As Long
//!         
//!         startPos = InStr(m_url, "://")
//!         If startPos > 0 Then
//!             startPos = startPos + 3
//!         Else
//!             startPos = 1
//!         End If
//!         
//!         endPos = InStr(startPos, m_url, "/")
//!         If endPos > 0 Then
//!             GetDomain = Mid$(m_url, startPos, endPos - startPos)
//!         Else
//!             GetDomain = Mid$(m_url, startPos)
//!         End If
//!     End Function
//!     
//!     Public Function GetPath() As String
//!         Dim startPos As Long
//!         Dim endPos As Long
//!         
//!         startPos = InStr(m_url, "://")
//!         If startPos > 0 Then
//!             startPos = InStr(startPos + 3, m_url, "/")
//!         Else
//!             startPos = InStr(m_url, "/")
//!         End If
//!         
//!         If startPos > 0 Then
//!             endPos = InStr(startPos, m_url, "?")
//!             If endPos > 0 Then
//!                 GetPath = Mid$(m_url, startPos, endPos - startPos)
//!             Else
//!                 GetPath = Mid$(m_url, startPos)
//!             End If
//!         Else
//!             GetPath = ""
//!         End If
//!     End Function
//!     
//!     Public Function GetLastPathSegment() As String
//!         Dim path As String
//!         Dim pos As Long
//!         
//!         path = GetPath()
//!         
//!         ' Remove trailing slash
//!         If Right$(path, 1) = "/" Then
//!             path = Left$(path, Len(path) - 1)
//!         End If
//!         
//!         pos = InStrRev(path, "/")
//!         If pos > 0 Then
//!             GetLastPathSegment = Mid$(path, pos + 1)
//!         Else
//!             GetLastPathSegment = path
//!         End If
//!     End Function
//! End Class
//!
//! ' Example 3: String truncation utility
//! Function TruncateWithEllipsis(text As String, maxLength As Long, _
//!                               Optional breakAtWord As Boolean = True) As String
//!     If Len(text) <= maxLength Then
//!         TruncateWithEllipsis = text
//!         Exit Function
//!     End If
//!     
//!     If breakAtWord Then
//!         Dim truncated As String
//!         Dim pos As Long
//!         
//!         truncated = Left$(text, maxLength - 3)
//!         pos = InStrRev(truncated, " ")
//!         
//!         If pos > 0 Then
//!             TruncateWithEllipsis = Left$(truncated, pos - 1) & "..."
//!         Else
//!             TruncateWithEllipsis = truncated & "..."
//!         End If
//!     Else
//!         TruncateWithEllipsis = Left$(text, maxLength - 3) & "..."
//!     End If
//! End Function
//!
//! ' Example 4: Email domain extractor
//! Function GetEmailDomain(email As String) As String
//!     Dim atPos As Long
//!     
//!     atPos = InStrRev(email, "@")
//!     If atPos > 0 And atPos < Len(email) Then
//!         GetEmailDomain = Mid$(email, atPos + 1)
//!     Else
//!         GetEmailDomain = ""
//!     End If
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! The `InStrRev` function can raise errors or return `Null`:
//!
//! - **Type Mismatch (Error 13)**: If arguments are not string-compatible or numeric where expected
//! - **Invalid use of Null (Error 94)**: If `stringcheck` or `stringmatch` is `Null` and result is assigned to non-Variant
//! - **Invalid procedure call (Error 5)**: If start is 0 or negative (except -1)
//!
//! ```vb
//! On Error GoTo ErrorHandler
//! Dim pos As Long
//! Dim fileName As String
//!
//! fileName = "document.txt"
//! pos = InStrRev(fileName, ".")
//!
//! If pos > 0 Then
//!     Debug.Print "Extension: " & Mid$(fileName, pos + 1)
//! Else
//!     Debug.Print "No extension found"
//! End If
//! Exit Sub
//!
//! ErrorHandler:
//!     MsgBox "Error in InStrRev: " & Err.Description, vbCritical
//! ```
//!
//! ## Performance Considerations
//!
//! - **Binary vs Text Compare**: Binary comparison (vbBinaryCompare) is faster than text comparison
//! - **String Length**: Performance degrades with very long strings
//! - **Start Position**: Using specific start position can improve performance for partial searches
//! - **Reverse Search**: More efficient than repeated `InStr` calls to find last occurrence
//! - **Alternative**: For complex pattern matching, consider regular expressions (VBScript.RegExp)
//!
//! ## Best Practices
//!
//! 1. **Test for Found**: Always check if result > 0 before using position
//! 2. **Default Start**: Use -1 or omit start parameter to search from end
//! 3. **Path Operations**: Prefer `InStrRev` for file path operations (finding last separator)
//! 4. **Extension Extraction**: `InStrRev` is ideal for getting file extensions
//! 5. **Null Handling**: Use `Variant` for result if strings might be `Null`
//! 6. **Parameter Order**: Remember `InStrRev` has different parameter order than `InStr`
//! 7. **Return Value**: Position is still counted from beginning (not from end)
//!
//! ## Comparison with Other Functions
//!
//! | Function | Purpose | Search Direction | Start Parameter |
//! |----------|---------|------------------|-----------------|
//! | `InStr` | Find substring | Left to right | Optional, before strings |
//! | `InStrRev` | Find substring | Right to left | Optional, after strings |
//! | `Like` | Pattern matching | N/A | N/A |
//! | `StrComp` | Compare strings | N/A | N/A |
//!
//! ## Platform and Version Notes
//!
//! - Available in VB6 and VBA (not in earlier VB versions)
//! - Returns `Long` (32-bit integer), not `Integer`
//! - 1-based indexing (first character is position 1)
//! - Position returned is from the start of string, not from the end
//! - Default start is -1 (search from end)
//! - Parameter order differs from `InStr` (strings first, then start)
//! - Maximum string length is approximately 2GB
//!
//! ## Limitations
//!
//! - Finds only one occurrence (the last one)
//! - No built-in regex or wildcard support
//! - Case-insensitive search (`vbTextCompare`) is slower
//! - Different parameter order than `InStr` can cause confusion
//! - Cannot search for multiple substrings in single call
//! - Performance can degrade with very long strings
//!
//! ## Related Functions
//!
//! - `InStr`: Search from beginning of string
//! - `Mid`, `Left`, `Right`: Extract substrings
//! - `Replace`: Find and replace text
//! - `Split`: Split string into array
//! - `StrReverse`: Reverse a string

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn instrrev_basic() {
        let source = r#"
Sub Test()
    pos = InStrRev("C:\Projects\file.txt", "\")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_with_start() {
        let source = r#"
Sub Test()
    pos = InStrRev(fileName, ".", 10)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_with_compare() {
        let source = r#"
Sub Test()
    pos = InStrRev(text, "SEARCH", -1, vbTextCompare)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_file_extension() {
        let source = r#"
Sub Test()
    dotPos = InStrRev(fileName, ".")
    If dotPos > 0 Then
        ext = Mid$(fileName, dotPos + 1)
    End If
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_if_statement() {
        let source = r#"
Sub Test()
    If InStrRev(fullPath, "\") > 0 Then
        Debug.Print "Path separator found"
    End If
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_function_return() {
        let source = r#"
Function GetLastSlashPos(path As String) As Long
    GetLastSlashPos = InStrRev(path, "\")
End Function
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_with_mid() {
        let source = r#"
Sub Test()
    pos = InStrRev(fullPath, "\")
    fileName = Mid$(fullPath, pos + 1)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_with_left() {
        let source = r#"
Sub Test()
    pos = InStrRev(fileName, ".")
    baseName = Left$(fileName, pos - 1)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_comparison() {
        let source = r#"
Sub Test()
    If InStrRev(url, "/") = 0 Then
        MsgBox "No path separator"
    End If
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_select_case() {
        let source = r#"
Sub Test()
    Select Case InStrRev(fileName, ".")
        Case 0
            Debug.Print "No extension"
        Case Is > 0
            Debug.Print "Has extension"
    End Select
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_debug_print() {
        let source = r#"
Sub Test()
    Debug.Print "Last position: " & InStrRev(text, delimiter)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_array_assignment() {
        let source = r#"
Sub Test()
    positions(i) = InStrRev(lines(i), ",")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_property_assignment() {
        let source = r"
Sub Test()
    obj.LastDelimiterPos = InStrRev(obj.Text, obj.Delimiter)
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_in_class() {
        let source = r#"
Private Sub Class_Initialize()
    m_lastDotPos = InStrRev(m_fileName, ".")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_with_statement() {
        let source = r#"
Sub Test()
    With pathInfo
        .LastSlashPos = InStrRev(.FullPath, "\")
    End With
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_function_argument() {
        let source = r"
Sub Test()
    Call ProcessLastPosition(InStrRev(data, marker))
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_concatenation() {
        let source = r#"
Sub Test()
    result = "Last at: " & InStrRev(text, searchTerm)
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_math_expression() {
        let source = r#"
Sub Test()
    beforeExtension = InStrRev(fileName, ".") - 1
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_iif() {
        let source = r#"
Sub Test()
    extension = IIf(InStrRev(fileName, ".") > 0, Mid$(fileName, InStrRev(fileName, ".") + 1), "")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Position: " & InStrRev(path, "\")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_collection_add() {
        let source = r#"
Sub Test()
    positions.Add InStrRev(files(i), ".")
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_boolean_expression() {
        let source = r#"
Sub Test()
    hasExtension = InStrRev(fileName, ".") > 0 And InStrRev(fileName, "\") = 0
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_nested_call() {
        let source = r#"
Sub Test()
    extension = UCase$(Mid$(fileName, InStrRev(fileName, ".") + 1))
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_for_loop() {
        let source = r#"
Sub Test()
    Dim i As Long
    For i = 1 To fileCount
        lastDotPos(i) = InStrRev(fileNames(i), ".")
    Next i
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_do_loop() {
        let source = r#"
Sub Test()
    Do While InStrRev(path, "\") > 0
        path = Left$(path, InStrRev(path, "\") - 1)
    Loop
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_right_function() {
        let source = r#"
Sub Test()
    extension = Right$(fileName, Len(fileName) - InStrRev(fileName, "."))
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn instrrev_parentheses() {
        let source = r"
Sub Test()
    pos = (InStrRev(text, searchText))
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/string/instrrev");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}