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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
//! # `LoadResString` Function
//!
//! Returns a string from a resource (.res) file.
//!
//! ## Syntax
//!
//! ```vb
//! LoadResString(index)
//! ```
//!
//! ## Parameters
//!
//! - `index` (Required): Integer identifying the string resource
//!   - Must be a numeric ID (string names not supported for string resources)
//!   - Must match the ID used when the resource was compiled
//!   - Typically ranges from 1 to 65535
//!
//! ## Return Value
//!
//! Returns a String:
//! - String containing the text from the resource file
//! - Empty string ("") if resource not found (in some VB versions)
//! - May raise error 32813 if resource not found
//! - Preserves all formatting including line breaks
//! - Unicode strings supported in VB6
//!
//! ## Remarks
//!
//! The `LoadResString` function loads text from embedded resources:
//!
//! - Loads strings from compiled resource (.res) files
//! - Resource file must be linked to project at compile time
//! - Primary method for internationalization (i18n) in VB6
//! - Allows localizing applications without code changes
//! - Strings can be translated by replacing resource file
//! - No external text files needed at runtime
//! - Embedded in compiled EXE/DLL
//! - Only one resource file per project
//! - Resource file added via Project > Add File
//! - Resource files created with Resource Editor or RC.EXE
//! - Index must be numeric (string names not supported)
//! - Common for error messages, prompts, labels
//! - Supports Unicode in VB6
//! - Error 32813: "Resource not found" if ID doesn't exist
//! - Error 48: "Error loading from file" if resource file corrupt
//! - More maintainable than hardcoded strings
//! - Easier to update text without recompiling code
//! - Standard practice for multi-language applications
//! - Can store long text passages
//! - Supports special characters and formatting
//!
//! ## Typical Uses
//!
//! 1. **Load Error Message**
//!    ```vb
//!    MsgBox LoadResString(1001), vbCritical
//!    ```
//!
//! 2. **Load Form Caption**
//!    ```vb
//!    Me.Caption = LoadResString(2001)
//!    ```
//!
//! 3. **Load Label Text**
//!    ```vb
//!    lblWelcome.Caption = LoadResString(3001)
//!    ```
//!
//! 4. **Load Menu Caption**
//!    ```vb
//!    mnuFile.Caption = LoadResString(4001)
//!    ```
//!
//! 5. **Load Button Caption**
//!    ```vb
//!    cmdOK.Caption = LoadResString(5001)
//!    ```
//!
//! 6. **Load `MessageBox` Text**
//!    ```vb
//!    MsgBox LoadResString(6001), vbInformation
//!    ```
//!
//! 7. **Load `StatusBar` Text**
//!    ```vb
//!    StatusBar1.SimpleText = LoadResString(7001)
//!    ```
//!
//! 8. **Load `ToolTip` Text**
//!    ```vb
//!    cmdSave.ToolTipText = LoadResString(8001)
//!    ```
//!
//! ## Basic Examples
//!
//! ### Example 1: Loading Messages
//! ```vb
//! ' Load various UI strings from resources
//! Me.Caption = LoadResString(1001)          ' "My Application"
//! lblTitle.Caption = LoadResString(1002)    ' "Welcome!"
//! cmdOK.Caption = LoadResString(1003)       ' "OK"
//! cmdCancel.Caption = LoadResString(1004)   ' "Cancel"
//! ```
//!
//! ### Example 2: Error Messages
//! ```vb
//! ' Use resource strings for error messages
//! If Not fileExists Then
//!     MsgBox LoadResString(2001), vbCritical  ' "File not found"
//! End If
//!
//! If accessDenied Then
//!     MsgBox LoadResString(2002), vbCritical  ' "Access denied"
//! End If
//! ```
//!
//! ### Example 3: Error Handling
//! ```vb
//! On Error Resume Next
//! Dim msg As String
//! msg = LoadResString(9999)
//! If Err.Number = 32813 Then
//!     msg = "String resource not found!"
//!     Err.Clear
//! End If
//! MsgBox msg
//! ```
//!
//! ### Example 4: Form Initialization
//! ```vb
//! Private Sub Form_Load()
//!     ' Load all UI strings from resources
//!     Me.Caption = LoadResString(1001)
//!     lblName.Caption = LoadResString(1002)
//!     lblAddress.Caption = LoadResString(1003)
//!     cmdSave.Caption = LoadResString(1004)
//!     cmdCancel.Caption = LoadResString(1005)
//! End Sub
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `SafeLoadResString`
//! ```vb
//! Function SafeLoadResString(ByVal resID As Integer, _
//!                            Optional ByVal defaultText As String = "") As String
//!     On Error Resume Next
//!     SafeLoadResString = LoadResString(resID)
//!     If Err.Number <> 0 Then
//!         SafeLoadResString = defaultText
//!         Err.Clear
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 2: `LoadFormStrings`
//! ```vb
//! Sub LoadFormStrings(frm As Form, ByVal baseID As Integer)
//!     Dim ctrl As Control
//!     Dim id As Integer
//!     
//!     On Error Resume Next
//!     frm.Caption = LoadResString(baseID)
//!     
//!     id = baseID + 1
//!     For Each ctrl In frm.Controls
//!         If TypeOf ctrl Is Label Or TypeOf ctrl Is CommandButton Then
//!             ctrl.Caption = LoadResString(id)
//!             id = id + 1
//!         End If
//!     Next ctrl
//! End Sub
//! ```
//!
//! ### Pattern 3: `FormatResString`
//! ```vb
//! Function FormatResString(ByVal resID As Integer, _
//!                          ParamArray args()) As String
//!     Dim template As String
//!     Dim i As Long
//!     
//!     template = LoadResString(resID)
//!     
//!     For i = LBound(args) To UBound(args)
//!         template = Replace(template, "{" & i & "}", CStr(args(i)))
//!     Next i
//!     
//!     FormatResString = template
//! End Function
//! ```
//!
//! ### Pattern 4: `GetErrorMessage`
//! ```vb
//! Function GetErrorMessage(ByVal errorCode As Long) As String
//!     Const BASE_ERROR_ID = 10000
//!     On Error Resume Next
//!     
//!     GetErrorMessage = LoadResString(BASE_ERROR_ID + errorCode)
//!     If Err.Number <> 0 Then
//!         GetErrorMessage = "Unknown error: " & errorCode
//!         Err.Clear
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 5: `LoadMenuStrings`
//! ```vb
//! Sub LoadMenuStrings()
//!     Const MENU_BASE = 4000
//!     
//!     mnuFile.Caption = LoadResString(MENU_BASE + 1)      ' "&File"
//!     mnuFileNew.Caption = LoadResString(MENU_BASE + 2)   ' "&New"
//!     mnuFileOpen.Caption = LoadResString(MENU_BASE + 3)  ' "&Open"
//!     mnuFileSave.Caption = LoadResString(MENU_BASE + 4)  ' "&Save"
//!     mnuFileExit.Caption = LoadResString(MENU_BASE + 5)  ' "E&xit"
//! End Sub
//! ```
//!
//! ### Pattern 6: `CachedResString`
//! ```vb
//! Dim resStringCache As New Collection
//!
//! Function CachedLoadResString(ByVal resID As Integer) As String
//!     Dim key As String
//!     On Error Resume Next
//!     
//!     key = "RES_" & resID
//!     CachedLoadResString = resStringCache(key)
//!     
//!     If Err.Number <> 0 Then
//!         Err.Clear
//!         CachedLoadResString = LoadResString(resID)
//!         resStringCache.Add CachedLoadResString, key
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 7: `ResStringExists`
//! ```vb
//! Function ResStringExists(ByVal resID As Integer) As Boolean
//!     On Error Resume Next
//!     Dim s As String
//!     s = LoadResString(resID)
//!     ResStringExists = (Err.Number = 0)
//!     Err.Clear
//! End Function
//! ```
//!
//! ### Pattern 8: `LoadResStringArray`
//! ```vb
//! Function LoadResStringArray(ByVal startID As Integer, _
//!                             ByVal count As Integer) As String()
//!     Dim result() As String
//!     Dim i As Integer
//!     
//!     ReDim result(0 To count - 1)
//!     
//!     On Error Resume Next
//!     For i = 0 To count - 1
//!         result(i) = LoadResString(startID + i)
//!         If Err.Number <> 0 Then
//!             result(i) = ""
//!             Err.Clear
//!         End If
//!     Next i
//!     
//!     LoadResStringArray = result
//! End Function
//! ```
//!
//! ### Pattern 9: `ShowResMessage`
//! ```vb
//! Sub ShowResMessage(ByVal resID As Integer, _
//!                    Optional ByVal icon As VbMsgBoxStyle = vbInformation)
//!     On Error Resume Next
//!     Dim msg As String
//!     msg = LoadResString(resID)
//!     
//!     If Err.Number = 0 Then
//!         MsgBox msg, icon
//!     Else
//!         MsgBox "Message resource " & resID & " not found", vbCritical
//!         Err.Clear
//!     End If
//! End Sub
//! ```
//!
//! ### Pattern 10: `MultiLineResString`
//! ```vb
//! Function MultiLineResString(ByVal resID As Integer) As String
//!     Dim text As String
//!     text = LoadResString(resID)
//!     ' Resource strings preserve line breaks
//!     MultiLineResString = text
//! End Function
//! ```
//!
//! ## Advanced Examples
//!
//! ### Example 1: Localization Manager
//! ```vb
//! ' Class: LocalizationManager
//! Private m_cache As Collection
//! Private m_languageID As Integer
//!
//! Private Sub Class_Initialize()
//!     Set m_cache = New Collection
//!     m_languageID = 1033 ' Default to English (US)
//! End Sub
//!
//! Public Property Let LanguageID(ByVal newLanguage As Integer)
//!     m_languageID = newLanguage
//!     ClearCache
//! End Property
//!
//! Public Function GetString(ByVal baseID As Integer) As String
//!     Dim resID As Integer
//!     Dim key As String
//!     
//!     On Error Resume Next
//!     resID = baseID + m_languageID
//!     key = "STR_" & resID
//!     
//!     GetString = m_cache(key)
//!     If Err.Number <> 0 Then
//!         Err.Clear
//!         GetString = LoadResString(resID)
//!         If Err.Number = 0 Then
//!             m_cache.Add GetString, key
//!         Else
//!             ' Fallback to default language
//!             GetString = LoadResString(baseID + 1033)
//!             Err.Clear
//!         End If
//!     End If
//! End Function
//!
//! Public Sub LocalizeForm(frm As Form)
//!     Dim ctrl As Control
//!     On Error Resume Next
//!     
//!     ' Load form caption
//!     frm.Caption = GetString(GetFormBaseID(frm))
//!     
//!     ' Load control captions
//!     For Each ctrl In frm.Controls
//!         If HasCaption(ctrl) Then
//!             ctrl.Caption = GetString(GetControlID(ctrl))
//!         End If
//!     Next ctrl
//! End Sub
//!
//! Private Sub ClearCache()
//!     Set m_cache = New Collection
//! End Sub
//!
//! Private Function HasCaption(ctrl As Control) As Boolean
//!     HasCaption = TypeOf ctrl Is Label Or _
//!                  TypeOf ctrl Is CommandButton Or _
//!                  TypeOf ctrl Is CheckBox Or _
//!                  TypeOf ctrl Is OptionButton
//! End Function
//!
//! Private Sub Class_Terminate()
//!     Set m_cache = Nothing
//! End Sub
//! ```
//!
//! ### Example 2: Error Message System
//! ```vb
//! ' Module: ErrorMessages
//! Private Const ERR_BASE = 20000
//!
//! Public Enum AppError
//!     errFileNotFound = 1
//!     errAccessDenied = 2
//!     errInvalidFormat = 3
//!     errNetworkError = 4
//!     errDatabaseError = 5
//! End Enum
//!
//! Public Sub ShowError(ByVal errorType As AppError, _
//!                      Optional ByVal additionalInfo As String = "")
//!     Dim msg As String
//!     On Error Resume Next
//!     
//!     msg = LoadResString(ERR_BASE + errorType)
//!     If Err.Number <> 0 Then
//!         msg = "Unknown error occurred"
//!         Err.Clear
//!     End If
//!     
//!     If Len(additionalInfo) > 0 Then
//!         msg = msg & vbCrLf & vbCrLf & additionalInfo
//!     End If
//!     
//!     MsgBox msg, vbCritical, LoadResString(ERR_BASE)
//! End Sub
//!
//! Public Function GetErrorText(ByVal errorType As AppError) As String
//!     On Error Resume Next
//!     GetErrorText = LoadResString(ERR_BASE + errorType)
//!     If Err.Number <> 0 Then
//!         GetErrorText = "Unknown error"
//!         Err.Clear
//!     End If
//! End Function
//! ```
//!
//! ### Example 3: Multi-Language Application
//! ```vb
//! ' Form with language selection
//! Public Enum Language
//!     langEnglish = 0
//!     langSpanish = 1000
//!     langFrench = 2000
//!     langGerman = 3000
//! End Enum
//!
//! Private currentLanguage As Language
//!
//! Private Sub Form_Load()
//!     ' Default to English
//!     currentLanguage = langEnglish
//!     LoadLanguage
//! End Sub
//!
//! Private Sub cboLanguage_Click()
//!     Select Case cboLanguage.ListIndex
//!         Case 0: currentLanguage = langEnglish
//!         Case 1: currentLanguage = langSpanish
//!         Case 2: currentLanguage = langFrench
//!         Case 3: currentLanguage = langGerman
//!     End Select
//!     LoadLanguage
//! End Sub
//!
//! Private Sub LoadLanguage()
//!     Dim baseID As Integer
//!     baseID = 10000 + currentLanguage
//!     
//!     On Error Resume Next
//!     Me.Caption = LoadResString(baseID + 1)
//!     lblWelcome.Caption = LoadResString(baseID + 2)
//!     lblInstructions.Caption = LoadResString(baseID + 3)
//!     cmdStart.Caption = LoadResString(baseID + 4)
//!     cmdExit.Caption = LoadResString(baseID + 5)
//!     
//!     ' Update menu
//!     mnuFile.Caption = LoadResString(baseID + 10)
//!     mnuHelp.Caption = LoadResString(baseID + 11)
//! End Sub
//! ```
//!
//! ### Example 4: String Template System
//! ```vb
//! ' Module: StringTemplates
//! Private Const TEMPLATE_BASE = 30000
//!
//! Public Function GetFormattedString(ByVal templateID As Integer, _
//!                                    ParamArray values()) As String
//!     Dim template As String
//!     Dim result As String
//!     Dim i As Long
//!     
//!     On Error Resume Next
//!     template = LoadResString(TEMPLATE_BASE + templateID)
//!     If Err.Number <> 0 Then
//!         GetFormattedString = ""
//!         Err.Clear
//!         Exit Function
//!     End If
//!     
//!     result = template
//!     For i = LBound(values) To UBound(values)
//!         result = Replace(result, "{" & i & "}", CStr(values(i)))
//!     Next i
//!     
//!     GetFormattedString = result
//! End Function
//!
//! Public Function GetWelcomeMessage(ByVal userName As String) As String
//!     ' Template: "Welcome, {0}! You have {1} new messages."
//!     GetWelcomeMessage = GetFormattedString(1, userName, GetMessageCount())
//! End Function
//!
//! Public Function GetSaveConfirmation(ByVal filename As String) As String
//!     ' Template: "Do you want to save changes to {0}?"
//!     GetSaveConfirmation = GetFormattedString(2, filename)
//! End Function
//!
//! Private Function GetMessageCount() As Long
//!     ' Implementation would return actual message count
//!     GetMessageCount = 5
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! ' Error 32813: Resource not found
//! On Error Resume Next
//! Dim msg As String
//! msg = LoadResString(9999)
//! If Err.Number = 32813 Then
//!     MsgBox "String resource not found!"
//! End If
//!
//! ' Error 48: Error loading from file
//! msg = LoadResString(1001)
//! If Err.Number = 48 Then
//!     MsgBox "Resource file is corrupt or missing!"
//! End If
//!
//! ' Safe loading pattern
//! Function TryLoadResString(ByVal resID As Integer, _
//!                           ByRef outString As String) As Boolean
//!     On Error Resume Next
//!     outString = LoadResString(resID)
//!     TryLoadResString = (Err.Number = 0)
//!     If Err.Number <> 0 Then
//!         outString = ""
//!     End If
//!     Err.Clear
//! End Function
//! ```
//!
//! ## Performance Considerations
//!
//! - **Fast Access**: Strings embedded in EXE (very fast loading)
//! - **No File I/O**: No disk access required
//! - **No Caching**: Each call loads fresh copy (implement caching if needed)
//! - **Memory Efficient**: Strings only loaded when accessed
//! - **Cache Strategy**: For frequently used strings, cache in Collection or array
//! - **Startup Time**: Loading many strings at startup may slow `Form_Load`
//!
//! ## Best Practices
//!
//! 1. **Use constants** for string IDs for maintainability
//! 2. **Group by category** using ID ranges (1000s for errors, 2000s for menus, etc.)
//! 3. **Cache frequently used strings** to improve performance
//! 4. **Always handle errors** - resource might not exist
//! 5. **Document string IDs** in code or separate file
//! 6. **Use templates** with placeholders for dynamic content
//! 7. **Organize by language** using ID offsets (English: 0, Spanish: +1000, etc.)
//! 8. **Test all languages** before deployment
//! 9. **Provide fallbacks** for missing strings
//! 10. **Keep strings updated** in sync with code changes
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Return Type | Data Type |
//! |----------|---------|-------------|-----------|
//! | **`LoadResString`** | Load string from resources | String | Text strings |
//! | **`LoadResPicture`** | Load image from resources | `StdPicture` | Images |
//! | **`LoadResData`** | Load binary data from resources | Byte array | Binary data |
//! | **`LoadString`** (API) | Windows API alternative | String | Text strings |
//!
//! ## `LoadResString` vs Hardcoded Strings
//!
//! ```vb
//! ' Hardcoded - difficult to localize
//! MsgBox "File not found", vbCritical
//!
//! ' Resource string - easy to localize
//! MsgBox LoadResString(1001), vbCritical
//! ```
//!
//! **Advantages of `LoadResString`:**
//! - Easy localization (just replace .res file)
//! - Centralized string management
//! - No code changes needed for translations
//! - Consistent messaging across application
//!
//! ## String ID Organization
//!
//! ```vb
//! ' Recommended ID ranges
//! Const STR_APP_BASE = 1000         ' Application strings
//! Const STR_ERROR_BASE = 2000       ' Error messages
//! Const STR_MENU_BASE = 3000        ' Menu items
//! Const STR_DIALOG_BASE = 4000      ' Dialog messages
//! Const STR_STATUS_BASE = 5000      ' Status messages
//! Const STR_HELP_BASE = 6000        ' Help text
//!
//! ' Language offsets
//! Const LANG_ENGLISH = 0
//! Const LANG_SPANISH = 10000
//! Const LANG_FRENCH = 20000
//! ```
//!
//! ## Platform Notes
//!
//! - Available in VB6 (not in early VB versions)
//! - Requires resource file (.res) linked to project
//! - Resource file created with Resource Editor or RC.EXE
//! - Only one resource file per project
//! - Resources embedded in compiled EXE/DLL
//! - Supports Unicode strings in VB6
//! - Index must be Integer (1-65535)
//! - String names not supported (numeric IDs only)
//! - Standard method for internationalization
//! - Preserves formatting including line breaks
//!
//! ## Limitations
//!
//! - **One Resource File**: Only one .res file per project
//! - **Numeric IDs Only**: Cannot use string names for string resources
//! - **Compile Time**: Must recompile to update strings
//! - **No Modification**: Cannot modify resources at runtime
//! - **Limited Editor**: VB6 Resource Editor is basic
//! - **ID Range**: Limited to 1-65535
//! - **No Encryption**: Strings easily extractable from EXE
//! - **No Formatting**: No printf-style formatting (must implement manually)
//! - **No Pluralization**: No built-in plural form handling
//! - **No Context**: All strings in flat namespace
//!
//! ## Related Functions
//!
//! - `LoadResPicture`: Load picture from resource file
//! - `LoadResData`: Load binary data from resource file
//! - `LoadPicture`: Load picture from external file
//! - `Format`: Format strings with values
//! - `Replace`: Replace placeholders in strings

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

    #[test]
    fn loadresstring_basic() {
        let source = r"
            Dim msg As String
            msg = LoadResString(1001)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_msgbox() {
        let source = r"
            MsgBox LoadResString(2001), vbCritical
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_if_statement() {
        let source = r"
            If Not fileExists Then
                MsgBox LoadResString(3001), vbCritical
            End If
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_caption() {
        let source = r"
            Me.Caption = LoadResString(4001)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_form_load() {
        let source = r"
            Private Sub Form_Load()
                lblWelcome.Caption = LoadResString(5001)
            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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_for_loop() {
        let source = r"
            For i = 1 To 5
                labels(i).Caption = LoadResString(6000 + i)
            Next i
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_function_return() {
        let source = r"
            Function GetErrorMessage() As String
                GetErrorMessage = LoadResString(7001)
            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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_error_handling() {
        let source = r#"
            On Error Resume Next
            msg = LoadResString(9999)
            If Err.Number = 32813 Then
                msg = "Resource not found"
            End If
        "#;
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_with_statement() {
        let source = r"
            With lblStatus
                .Caption = LoadResString(8001)
            End With
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_select_case() {
        let source = r"
            Select Case errorType
                Case 1
                    msg = LoadResString(9001)
                Case 2
                    msg = LoadResString(9002)
            End Select
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_elseif() {
        let source = r#"
            If lang = "en" Then
                msg = LoadResString(10001)
            ElseIf lang = "es" Then
                msg = LoadResString(11001)
            End If
        "#;
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_concatenation() {
        let source = r"
            Dim fullMsg As String
            fullMsg = LoadResString(12001) & vbCrLf & details
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_parentheses() {
        let source = r"
            msg = (LoadResString(13001))
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_iif() {
        let source = r"
            msg = IIf(success, LoadResString(14001), LoadResString(14002))
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_in_class() {
        let source = r"
            Private Sub Class_Initialize()
                m_errorMsg = LoadResString(15001)
            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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_function_argument() {
        let source = r"
            Call ShowMessage(LoadResString(16001))
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_property_assignment() {
        let source = r"
            MyObject.Message = LoadResString(17001)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_array_assignment() {
        let source = r"
            messages(i) = LoadResString(18000 + i)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_while_wend() {
        let source = r"
            While index < maxStrings
                text = LoadResString(19000 + index)
                index = index + 1
            Wend
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_do_while() {
        let source = r"
            Do While hasMore
                currentMsg = LoadResString(GetNextID())
            Loop
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_do_until() {
        let source = r"
            Do Until loaded
                On Error Resume Next
                msg = LoadResString(resID)
                loaded = (Err.Number = 0)
            Loop
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_constants() {
        let source = r"
            Const MSG_ERROR = 20001
            MsgBox LoadResString(MSG_ERROR), vbCritical
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_addition() {
        let source = r"
            Dim baseID As Integer
            baseID = 21000
            msg = LoadResString(baseID + offset)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_replace() {
        let source = r#"
            Dim template As String
            template = LoadResString(22001)
            msg = Replace(template, "{0}", userName)
        "#;
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_collection_add() {
        let source = r#"
            messages.Add LoadResString(23001), "WelcomeMsg"
        "#;
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_debug_print() {
        let source = r"
            Debug.Print LoadResString(24001)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn loadresstring_tooltip() {
        let source = r"
            cmdSave.ToolTipText = LoadResString(25001)
        ";
        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/resources/loadresstring",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}