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
//! # `RightB$` Function
//!
//! The `RightB$` function in Visual Basic 6 returns a string containing a specified number of
//! bytes from the right side (end) of a string. Unlike `Right$` which works with characters,
//! `RightB$` operates at the byte level, making it essential for binary data processing and
//! working with DBCS (Double-Byte Character Set) strings.
//!
//! ## Syntax
//!
//! ```vb6
//! RightB$(string, length)
//! ```
//!
//! ## Parameters
//!
//! - `string` - Required. String expression from which the rightmost bytes are returned.
//!   If `string` contains `Null`, `Null` is returned.
//! - `length` - Required. Numeric expression indicating how many bytes to return. If 0,
//!   a zero-length string ("") is returned. If greater than or equal to the number of bytes
//!   in `string`, the entire string is returned.
//!
//! ## Return Value
//!
//! Returns a `String` containing the rightmost `length` bytes of `string`.
//!
//! ## Behavior and Characteristics
//!
//! ### Byte vs. Character Operation
//!
//! - `RightB$` counts bytes, not characters
//! - In VB6, each character is stored as 2 bytes (UCS-2/Unicode)
//! - Extracting an odd number of bytes from Unicode text may split a character
//! - DBCS characters (e.g., Japanese, Chinese) may require 2 bytes per character
//!
//! ### Length Handling
//!
//! - If `length` = 0: Returns an empty string ("")
//! - If `length` >= `LenB(string)`: Returns the entire string
//! - If `length` < 0: Generates a runtime error (Invalid procedure call or argument)
//! - If `string` is empty (""): Returns an empty string regardless of `length`
//!
//! ### Character Set Considerations
//!
//! - VB6 stores strings internally as Unicode (UCS-2)
//! - Each character typically occupies 2 bytes
//! - Extracting an odd number of bytes can result in incomplete characters
//! - Use `Right$` for character-based extraction in most cases
//!
//! ## Common Usage Patterns
//!
//! ### 1. Extract Binary Data Suffix
//!
//! ```vb6
//! Function GetBinarySuffix(data As String, numBytes As Integer) As String
//!     GetBinarySuffix = RightB$(data, numBytes)
//! End Function
//!
//! Dim suffix As String
//! suffix = GetBinarySuffix(binaryData, 4)  ' Get last 4 bytes
//! ```
//!
//! ### 2. Read File Trailer
//!
//! ```vb6
//! Function ReadFileTrailer(fileData As String) As String
//!     ' Get last 8 bytes of file (e.g., signature or checksum)
//!     ReadFileTrailer = RightB$(fileData, 8)
//! End Function
//! ```
//!
//! ### 3. Extract Protocol Footer
//!
//! ```vb6
//! Function GetProtocolFooter(packet As String) As String
//!     ' Get 2-byte footer from network packet
//!     GetProtocolFooter = RightB$(packet, 2)
//! End Function
//! ```
//!
//! ### 4. Process Binary Structures
//!
//! ```vb6
//! Type FileHeader
//!     signature As String * 4
//!     version As String * 2
//! End Type
//!
//! Function GetVersion(headerData As String) As String
//!     ' Extract last 2 bytes as version info
//!     GetVersion = RightB$(headerData, 2)
//! End Function
//! ```
//!
//! ### 5. Network Packet Checksum
//!
//! ```vb6
//! Function ExtractChecksum(packet As String) As String
//!     ' Network packets often have 2-4 byte checksums at end
//!     ExtractChecksum = RightB$(packet, 4)
//! End Function
//! ```
//!
//! ### 6. GUID/UUID Component Extraction
//!
//! ```vb6
//! Function GetGuidSuffix(guidData As String) As String
//!     ' Extract last 6 bytes of GUID (node portion)
//!     GetGuidSuffix = RightB$(guidData, 6)
//! End Function
//! ```
//!
//! ### 7. File Format Magic Bytes (Trailer)
//!
//! ```vb6
//! Function CheckFileTrailer(fileData As String, expectedTrailer As String) As Boolean
//!     Dim trailer As String
//!     trailer = RightB$(fileData, LenB(expectedTrailer))
//!     CheckFileTrailer = (trailer = expectedTrailer)
//! End Function
//! ```
//!
//! ### 8. Extract Record Suffix
//!
//! ```vb6
//! Function GetRecordSuffix(record As String) As String
//!     ' Fixed-length binary record with 4-byte suffix
//!     GetRecordSuffix = RightB$(record, 4)
//! End Function
//! ```
//!
//! ### 9. Image Data Footer
//!
//! ```vb6
//! Function GetImageFooter(imageData As String) As String
//!     ' Some image formats have trailers (e.g., JPEG end marker)
//!     GetImageFooter = RightB$(imageData, 2)
//! End Function
//! ```
//!
//! ### 10. Database Record Alignment
//!
//! ```vb6
//! Function AlignRecordEnd(record As String, alignment As Integer) As String
//!     ' Get aligned portion from end of record
//!     Dim alignedSize As Integer
//!     alignedSize = (LenB(record) \ alignment) * alignment
//!     If alignedSize > 0 Then
//!         AlignRecordEnd = RightB$(record, alignedSize)
//!     Else
//!         AlignRecordEnd = record
//!     End If
//! End Function
//! ```
//!
//! ## Related Functions
//!
//! - `Right$()` - Returns a specified number of characters from the right (character-based)
//! - `LeftB$()` - Returns a specified number of bytes from the left side of a string
//! - `MidB$()` - Returns a specified number of bytes from any position in a string
//! - `LenB()` - Returns the number of bytes used to represent a string in memory
//! - `RightB()` - Variant version that can return `Null`
//! - `InStrB()` - Finds the byte position of a substring
//! - `AscB()` - Returns the byte value of the first byte in a string
//! - `ChrB$()` - Returns a string containing a single-byte character
//!
//! ## Best Practices
//!
//! ### When to Use `RightB$` vs `Right$`
//!
//! ```vb6
//! ' Use Right$ for text/character operations
//! Dim fileExt As String
//! fileExt = Right$(fileName, 3)  ' Get last 3 characters
//!
//! ' Use RightB$ for binary data operations
//! Dim checksum As String
//! checksum = RightB$(binaryData, 4)  ' Get last 4 bytes
//! ```
//!
//! ### Validate Byte Count
//!
//! ```vb6
//! Function SafeRightB(data As String, numBytes As Integer) As String
//!     If numBytes < 0 Then
//!         SafeRightB = ""
//!     ElseIf numBytes >= LenB(data) Then
//!         SafeRightB = data
//!     Else
//!         SafeRightB = RightB$(data, numBytes)
//!     End If
//! End Function
//! ```
//!
//! ### Use with `LenB` for Byte Counting
//!
//! ```vb6
//! ' Always use LenB when working with RightB$
//! Dim totalBytes As Long
//! Dim footer As String
//! totalBytes = LenB(binaryData)
//! footer = RightB$(binaryData, 8)
//! ```
//!
//! ### Combine with `AscB` for Byte Values
//!
//! ```vb6
//! Function GetLastByte(data As String) As Byte
//!     Dim lastByte As String
//!     lastByte = RightB$(data, 1)
//!     GetLastByte = AscB(lastByte)
//! End Function
//! ```
//!
//! ### Binary Structure Parsing
//!
//! ```vb6
//! ' Extract multiple fields from end of structure
//! Dim checksum As String
//! Dim version As String
//! checksum = RightB$(structData, 4)
//! version = RightB$(LeftB$(structData, LenB(structData) - 4), 2)
//! ```
//!
//! ## Performance Considerations
//!
//! - `RightB$` is efficient for binary data operations
//! - Slightly faster than `Right$` for byte-aligned operations
//! - Avoid using in tight loops with string concatenation
//! - Consider byte arrays for large binary data processing
//!
//! ```vb6
//! ' Less efficient: multiple RightB$ calls
//! For i = 1 To 1000
//!     result = result & RightB$(data, 4)
//! Next i
//!
//! ' More efficient: single operation or byte array
//! Dim parts() As String
//! ReDim parts(1 To 1000)
//! For i = 1 To 1000
//!     parts(i) = RightB$(data, 4)
//! Next i
//! result = Join(parts, "")
//! ```
//!
//! ## Character Encoding and VB6
//!
//! ### Unicode String Storage
//!
//! VB6 stores strings internally as Unicode (UCS-2):
//!
//! ```vb6
//! Dim text As String
//! text = "AB"
//! Debug.Print Len(text)   ' Prints 2 (characters)
//! Debug.Print LenB(text)  ' Prints 4 (bytes: 2 bytes per character)
//!
//! Dim lastChar As String
//! lastChar = Right$(text, 1)   ' Returns "B" (1 character)
//! Dim lastTwoBytes As String
//! lastTwoBytes = RightB$(text, 2)  ' Returns "B" (last 2 bytes = 1 character)
//! ```
//!
//! ### DBCS Considerations
//!
//! When working with Double-Byte Character Sets:
//!
//! ```vb6
//! ' Be careful with DBCS text - extracting odd number of bytes
//! ' can split multi-byte characters
//! Dim japaneseText As String
//! japaneseText = "こんにちは"
//!
//! ' Right$ extracts by characters (safe)
//! Dim lastChar As String
//! lastChar = Right$(japaneseText, 1)  ' Gets last character properly
//!
//! ' RightB$ extracts by bytes (may split characters)
//! Dim lastByte As String
//! lastByte = RightB$(japaneseText, 1)  ' May get incomplete character!
//! ```
//!
//! ## Common Pitfalls
//!
//! ### 1. Confusing Bytes with Characters
//!
//! ```vb6
//! Dim text As String
//! text = "Hello"
//!
//! ' Wrong: thinking RightB$ works like Right$
//! result = RightB$(text, 3)  ' Gets last 3 BYTES (not characters)
//! ' With Unicode, 3 bytes = 1.5 characters (1 complete + half of another)
//!
//! ' Correct: use Right$ for character operations
//! result = Right$(text, 3)  ' Gets "llo"
//! ```
//!
//! ### 2. Odd Byte Counts with Unicode
//!
//! ```vb6
//! ' Problematic: odd number of bytes splits Unicode characters
//! Dim text As String
//! text = "Test"
//! Dim partial As String
//! partial = RightB$(text, 3)  ' 3 bytes = 1 character + half a character
//! ' Result may be unexpected or corrupted
//!
//! ' Better: use even byte counts for Unicode
//! Dim proper As String
//! proper = RightB$(text, 4)  ' 4 bytes = 2 complete characters
//! ```
//!
//! ### 3. Not Using `LenB` for Length
//!
//! ```vb6
//! ' Wrong: using Len instead of LenB
//! If RightB$(data, Len(data) - 4) Then  ' Incorrect!
//!
//! ' Correct: use LenB for byte operations
//! If RightB$(data, LenB(data) - 4) Then
//! ```
//!
//! ### 4. Negative Length Values
//!
//! ```vb6
//! ' Runtime error: Invalid procedure call or argument
//! result = RightB$("Hello", -1)  ' ERROR!
//!
//! ' Validate first
//! If numBytes >= 0 Then
//!     result = RightB$(data, numBytes)
//! End If
//! ```
//!
//! ### 5. Assuming ASCII/ANSI Encoding
//!
//! ```vb6
//! ' Wrong: assuming 1 byte = 1 character
//! Dim data As String
//! data = "ABCD"
//! Dim lastByte As String
//! lastByte = RightB$(data, 1)  ' Gets 1 byte, not last character
//! ' In Unicode, this is half of the last character
//!
//! ' Correct: use Right$ or account for 2 bytes per character
//! Dim lastChar As String
//! lastChar = Right$(data, 1)   ' Gets "D"
//! ' OR
//! lastChar = RightB$(data, 2)  ' Gets last 2 bytes = "D"
//! ```
//!
//! ## Limitations
//!
//! - Cannot handle `Null` values (use `RightB` variant function instead)
//! - Works with bytes, not characters (can split multi-byte characters)
//! - Negative `length` values cause runtime errors
//! - Limited usefulness for Unicode text (use `Right$` instead)
//! - No built-in validation for character boundaries
//! - Extracting odd byte counts from Unicode strings can produce invalid characters
//! - Less intuitive than `Right$` for general string processing

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

    #[test]
    fn rightb_dollar_simple() {
        let source = r#"
Sub Main()
    result = RightB$("Hello", 4)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_assignment() {
        let source = r"
Sub Main()
    Dim suffix As String
    suffix = RightB$(binaryData, 8)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_variable() {
        let source = r#"
Sub Main()
    Dim data As String
    Dim result As String
    data = "Sample"
    result = RightB$(data, 6)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_binary_suffix() {
        let source = r"
Function GetBinarySuffix(data As String, numBytes As Integer) As String
    GetBinarySuffix = RightB$(data, numBytes)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_file_trailer() {
        let source = r"
Function ReadFileTrailer(fileData As String) As String
    ReadFileTrailer = RightB$(fileData, 8)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_protocol_footer() {
        let source = r"
Function GetProtocolFooter(packet As String) As String
    GetProtocolFooter = RightB$(packet, 2)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_in_condition() {
        let source = r#"
Sub Main()
    If RightB$(packet, 2) = trailer Then
        Debug.Print "Valid packet"
    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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_checksum() {
        let source = r"
Function ExtractChecksum(packet As String) As String
    ExtractChecksum = RightB$(packet, 4)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_guid_suffix() {
        let source = r"
Function GetGuidSuffix(guidData As String) As String
    GetGuidSuffix = RightB$(guidData, 6)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_with_lenb() {
        let source = r"
Sub Main()
    Dim totalBytes As Long
    Dim footer As String
    totalBytes = LenB(binaryData)
    footer = RightB$(binaryData, 8)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_multiple_uses() {
        let source = r"
Sub ProcessBinary()
    Dim checksum As String
    Dim version As String
    checksum = RightB$(structData, 4)
    version = RightB$(headerData, 2)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_select_case() {
        let source = r#"
Sub Main()
    Select Case RightB$(packet, 2)
        Case trailer1
            Debug.Print "Type 1"
        Case trailer2
            Debug.Print "Type 2"
    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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_expression_arg() {
        let source = r"
Sub Main()
    Dim result As String
    result = RightB$(data, LenB(data) - 4)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_concatenation() {
        let source = r#"
Sub Main()
    Dim output As String
    output = "Footer: " & RightB$(data, 4)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_record_suffix() {
        let source = r"
Function GetRecordSuffix(record As String) As String
    GetRecordSuffix = RightB$(record, 4)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_image_footer() {
        let source = r"
Function GetImageFooter(imageData As String) As String
    GetImageFooter = RightB$(imageData, 2)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_zero_bytes() {
        let source = r#"
Sub Main()
    Dim empty As String
    empty = RightB$("Hello", 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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_full_data() {
        let source = r#"
Sub Main()
    Dim full As String
    full = RightB$("Hello", 100)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_with_ascb() {
        let source = r"
Function GetLastByte(data As String) As Byte
    Dim lastByte As String
    lastByte = RightB$(data, 1)
    GetLastByte = AscB(lastByte)
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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rightb_dollar_loop_processing() {
        let source = r"
Sub ProcessRecords()
    Dim i As Integer
    Dim suffix As String
    For i = 1 To 10
        suffix = RightB$(records(i), 4)
        Debug.Print suffix
    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/rightb_dollar",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}