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
//! VB6 `Year` Function
//!
//! The `Year` function returns an Integer representing the year of a specified date.
//!
//! ## Syntax
//! ```vb6
//! Year(date)
//! ```
//!
//! ## Parameters
//! - `date`: Required. Any Variant, numeric expression, string expression, or any combination that can represent a date. If `date` contains Null, Null is returned.
//!
//! ## Returns
//! Returns an `Integer` representing the year (a whole number between 100 and 9999, inclusive).
//!
//! ## Remarks
//! The `Year` function extracts the year component from a date value:
//!
//! - **Return range**: Returns values from 100 to 9999
//! - **Null handling**: If the date argument is Null, the function returns Null
//! - **Date validation**: Invalid dates cause Error 13 (Type mismatch)
//! - **String dates**: Accepts string representations of dates (e.g., "12/25/2023")
//! - **Numeric dates**: Accepts numeric date values (serial dates)
//! - **Date literals**: Accepts date literals (e.g., #12/25/2023#)
//! - **Current year**: Use `Year(Date)` or `Year(Now)` to get current year
//! - **Leap year calculation**: Can be used to determine leap years
//! - **Year arithmetic**: Often combined with `DateSerial` for date calculations
//! - **Four-digit years**: Always returns full four-digit year (not two-digit)
//!
//! ### Date Components Family
//! The `Year` function is part of a family of date component extraction functions:
//! - `Year(date)` - Returns the year (100-9999)
//! - `Month(date)` - Returns the month (1-12)
//! - `Day(date)` - Returns the day (1-31)
//! - `Weekday(date)` - Returns the day of week (1-7)
//! - `Hour(date)` - Returns the hour (0-23)
//! - `Minute(date)` - Returns the minute (0-59)
//! - `Second(date)` - Returns the second (0-59)
//!
//! ### Leap Year Detection
//! ```vb6
//! Function IsLeapYear(yr As Integer) As Boolean
//!     IsLeapYear = ((yr Mod 4 = 0) And (yr Mod 100 <> 0)) Or (yr Mod 400 = 0)
//! End Function
//! ```
//!
//! ### Combining with `DateSerial`
//! ```vb6
//! ' Get first day of current year
//! firstDay = DateSerial(Year(Date), 1, 1)
//!
//! ' Get last day of current year
//! lastDay = DateSerial(Year(Date), 12, 31)
//! ```
//!
//! ## Typical Uses
//! 1. **Extract Year**: Get the year component from a date
//! 2. **Age Calculation**: Calculate age from birth date
//! 3. **Fiscal Year**: Determine fiscal year for financial reporting
//! 4. **Year Filtering**: Filter data by year
//! 5. **Year Validation**: Validate year ranges in data entry
//! 6. **Archive Organization**: Organize files/records by year
//! 7. **Year Comparison**: Compare dates across different years
//! 8. **Report Grouping**: Group data by year for reports
//!
//! ## Basic Examples
//!
//! ### Example 1: Get Current Year
//! ```vb6
//! Sub ShowCurrentYear()
//!     Dim currentYear As Integer
//!     currentYear = Year(Date)
//!     MsgBox "Current year: " & currentYear
//! End Sub
//! ```
//!
//! ### Example 2: Calculate Age
//! ```vb6
//! Function CalculateAge(birthDate As Date) As Integer
//!     Dim age As Integer
//!     age = Year(Date) - Year(birthDate)
//!     
//!     ' Adjust if birthday hasn't occurred this year
//!     If Month(Date) < Month(birthDate) Or _
//!        (Month(Date) = Month(birthDate) And Day(Date) < Day(birthDate)) Then
//!         age = age - 1
//!     End If
//!     
//!     CalculateAge = age
//! End Function
//! ```
//!
//! ### Example 3: Get Years Between Dates
//! ```vb6
//! Function YearsBetween(startDate As Date, endDate As Date) As Integer
//!     YearsBetween = Year(endDate) - Year(startDate)
//! End Function
//! ```
//!
//! ### Example 4: Check If Leap Year
//! ```vb6
//! Function IsLeapYear(dt As Date) As Boolean
//!     Dim yr As Integer
//!     yr = Year(dt)
//!     IsLeapYear = ((yr Mod 4 = 0) And (yr Mod 100 <> 0)) Or (yr Mod 400 = 0)
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Get First Day of Year
//! ```vb6
//! Function GetFirstDayOfYear(dt As Date) As Date
//!     GetFirstDayOfYear = DateSerial(Year(dt), 1, 1)
//! End Function
//! ```
//!
//! ### Pattern 2: Get Last Day of Year
//! ```vb6
//! Function GetLastDayOfYear(dt As Date) As Date
//!     GetLastDayOfYear = DateSerial(Year(dt), 12, 31)
//! End Function
//! ```
//!
//! ### Pattern 3: Same Year Check
//! ```vb6
//! Function IsSameYear(date1 As Date, date2 As Date) As Boolean
//!     IsSameYear = (Year(date1) = Year(date2))
//! End Function
//! ```
//!
//! ### Pattern 4: Get Fiscal Year
//! ```vb6
//! Function GetFiscalYear(dt As Date, fiscalStartMonth As Integer) As Integer
//!     If Month(dt) >= fiscalStartMonth Then
//!         GetFiscalYear = Year(dt)
//!     Else
//!         GetFiscalYear = Year(dt) - 1
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 5: Format Year Display
//! ```vb6
//! Function FormatYearDisplay(dt As Date) As String
//!     FormatYearDisplay = "Year " & Year(dt)
//! End Function
//! ```
//!
//! ### Pattern 6: Year Difference
//! ```vb6
//! Function GetYearDifference(startDate As Date, endDate As Date) As Integer
//!     GetYearDifference = Abs(Year(endDate) - Year(startDate))
//! End Function
//! ```
//!
//! ### Pattern 7: Validate Year Range
//! ```vb6
//! Function IsYearInRange(dt As Date, minYear As Integer, maxYear As Integer) As Boolean
//!     Dim yr As Integer
//!     yr = Year(dt)
//!     IsYearInRange = (yr >= minYear And yr <= maxYear)
//! End Function
//! ```
//!
//! ### Pattern 8: Get Years Until Date
//! ```vb6
//! Function YearsUntil(targetDate As Date) As Integer
//!     YearsUntil = Year(targetDate) - Year(Date)
//! End Function
//! ```
//!
//! ### Pattern 9: Add Years to Date
//! ```vb6
//! Function AddYears(dt As Date, years As Integer) As Date
//!     AddYears = DateSerial(Year(dt) + years, Month(dt), Day(dt))
//! End Function
//! ```
//!
//! ### Pattern 10: Get Year-to-Date Range
//! ```vb6
//! Sub GetYTDRange(ByRef startDate As Date, ByRef endDate As Date)
//!     startDate = DateSerial(Year(Date), 1, 1)
//!     endDate = Date
//! End Sub
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Age Calculator Class
//! ```vb6
//! ' Class: AgeCalculator
//! ' Calculates precise age with various options
//! Option Explicit
//!
//! Public Function GetAge(birthDate As Date, Optional asOfDate As Variant) As Integer
//!     Dim referenceDate As Date
//!     Dim age As Integer
//!     
//!     If IsMissing(asOfDate) Then
//!         referenceDate = Date
//!     Else
//!         referenceDate = CDate(asOfDate)
//!     End If
//!     
//!     age = Year(referenceDate) - Year(birthDate)
//!     
//!     ' Adjust if birthday hasn't occurred yet
//!     If Month(referenceDate) < Month(birthDate) Or _
//!        (Month(referenceDate) = Month(birthDate) And _
//!         Day(referenceDate) < Day(birthDate)) Then
//!         age = age - 1
//!     End If
//!     
//!     GetAge = age
//! End Function
//!
//! Public Function GetAgeInYearsAndMonths(birthDate As Date) As String
//!     Dim years As Integer
//!     Dim months As Integer
//!     Dim tempDate As Date
//!     
//!     years = GetAge(birthDate)
//!     tempDate = DateSerial(Year(birthDate) + years, Month(birthDate), Day(birthDate))
//!     months = DateDiff("m", tempDate, Date)
//!     
//!     GetAgeInYearsAndMonths = years & " years, " & months & " months"
//! End Function
//!
//! Public Function WillBeBirthdayThisYear(birthDate As Date) As Boolean
//!     Dim birthdayThisYear As Date
//!     birthdayThisYear = DateSerial(Year(Date), Month(birthDate), Day(birthDate))
//!     WillBeBirthdayThisYear = (birthdayThisYear >= Date)
//! End Function
//!
//! Public Function GetAgeAtDate(birthDate As Date, targetDate As Date) As Integer
//!     GetAgeAtDate = GetAge(birthDate, targetDate)
//! End Function
//! ```
//!
//! ### Example 2: Fiscal Year Manager Module
//! ```vb6
//! ' Module: FiscalYearManager
//! ' Manages fiscal year calculations
//! Option Explicit
//!
//! Private m_FiscalStartMonth As Integer
//!
//! Public Sub SetFiscalYearStart(startMonth As Integer)
//!     If startMonth < 1 Or startMonth > 12 Then
//!         Err.Raise 5, , "Start month must be between 1 and 12"
//!     End If
//!     m_FiscalStartMonth = startMonth
//! End Sub
//!
//! Public Function GetFiscalYear(dt As Date) As Integer
//!     If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
//!     
//!     If Month(dt) >= m_FiscalStartMonth Then
//!         GetFiscalYear = Year(dt)
//!     Else
//!         GetFiscalYear = Year(dt) - 1
//!     End If
//! End Function
//!
//! Public Function GetFiscalYearStart(fiscalYear As Integer) As Date
//!     If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
//!     GetFiscalYearStart = DateSerial(fiscalYear, m_FiscalStartMonth, 1)
//! End Function
//!
//! Public Function GetFiscalYearEnd(fiscalYear As Integer) As Date
//!     Dim endMonth As Integer
//!     Dim endYear As Integer
//!     
//!     If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
//!     
//!     endMonth = m_FiscalStartMonth - 1
//!     If endMonth = 0 Then endMonth = 12
//!     
//!     If m_FiscalStartMonth = 1 Then
//!         endYear = fiscalYear
//!     Else
//!         endYear = fiscalYear + 1
//!     End If
//!     
//!     GetFiscalYearEnd = DateSerial(endYear, endMonth, Day(DateSerial(endYear, endMonth + 1, 0)))
//! End Function
//!
//! Public Function FormatFiscalYear(fiscalYear As Integer) As String
//!     If m_FiscalStartMonth = 1 Then
//!         FormatFiscalYear = "FY" & fiscalYear
//!     Else
//!         FormatFiscalYear = "FY" & fiscalYear & "-" & Right$(CStr(fiscalYear + 1), 2)
//!     End If
//! End Function
//! ```
//!
//! ### Example 3: Year Range Analyzer Class
//! ```vb6
//! ' Class: YearRangeAnalyzer
//! ' Analyzes year ranges in date collections
//! Option Explicit
//!
//! Public Function GetYearRange(dates() As Date) As String
//!     Dim minYear As Integer
//!     Dim maxYear As Integer
//!     Dim i As Long
//!     Dim yr As Integer
//!     
//!     If UBound(dates) < LBound(dates) Then
//!         GetYearRange = "No dates"
//!         Exit Function
//!     End If
//!     
//!     minYear = Year(dates(LBound(dates)))
//!     maxYear = minYear
//!     
//!     For i = LBound(dates) To UBound(dates)
//!         yr = Year(dates(i))
//!         If yr < minYear Then minYear = yr
//!         If yr > maxYear Then maxYear = yr
//!     Next i
//!     
//!     If minYear = maxYear Then
//!         GetYearRange = CStr(minYear)
//!     Else
//!         GetYearRange = minYear & "-" & maxYear
//!     End If
//! End Function
//!
//! Public Function GetYearDistribution(dates() As Date) As Collection
//!     Dim distribution As New Collection
//!     Dim i As Long
//!     Dim yr As Integer
//!     Dim yearKey As String
//!     Dim count As Long
//!     
//!     For i = LBound(dates) To UBound(dates)
//!         yr = Year(dates(i))
//!         yearKey = CStr(yr)
//!         
//!         On Error Resume Next
//!         count = distribution(yearKey)
//!         If Err.Number <> 0 Then
//!             distribution.Add 1, yearKey
//!             Err.Clear
//!         Else
//!             distribution.Remove yearKey
//!             distribution.Add count + 1, yearKey
//!         End If
//!         On Error GoTo 0
//!     Next i
//!     
//!     Set GetYearDistribution = distribution
//! End Function
//!
//! Public Function GetMostCommonYear(dates() As Date) As Integer
//!     Dim distribution As Collection
//!     Dim maxCount As Long
//!     Dim maxYear As Integer
//!     Dim yr As Variant
//!     
//!     Set distribution = GetYearDistribution(dates)
//!     
//!     maxCount = 0
//!     For Each yr In distribution
//!         If distribution(CStr(yr)) > maxCount Then
//!             maxCount = distribution(CStr(yr))
//!             maxYear = CInt(yr)
//!         End If
//!     Next yr
//!     
//!     GetMostCommonYear = maxYear
//! End Function
//!
//! Public Function GetUniqueYears(dates() As Date) As Collection
//!     Dim years As New Collection
//!     Dim i As Long
//!     Dim yr As Integer
//!     Dim yearKey As String
//!     
//!     For i = LBound(dates) To UBound(dates)
//!         yr = Year(dates(i))
//!         yearKey = CStr(yr)
//!         
//!         On Error Resume Next
//!         years.Add yr, yearKey
//!         On Error GoTo 0
//!     Next i
//!     
//!     Set GetUniqueYears = years
//! End Function
//! ```
//!
//! ### Example 4: Date Archive Organizer Module
//! ```vb6
//! ' Module: DateArchiveOrganizer
//! ' Organizes files and data by year
//! Option Explicit
//!
//! Public Function GetArchivePath(baseFolder As String, dt As Date) As String
//!     Dim yearFolder As String
//!     yearFolder = baseFolder
//!     If Right$(yearFolder, 1) <> "\" Then yearFolder = yearFolder & "\"
//!     yearFolder = yearFolder & Year(dt) & "\"
//!     GetArchivePath = yearFolder
//! End Function
//!
//! Public Function CreateYearlyArchiveFolders(baseFolder As String, _
//!                                            startYear As Integer, _
//!                                            endYear As Integer) As Long
//!     Dim yr As Integer
//!     Dim folderPath As String
//!     Dim count As Long
//!     
//!     count = 0
//!     For yr = startYear To endYear
//!         folderPath = baseFolder
//!         If Right$(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
//!         folderPath = folderPath & yr & "\"
//!         
//!         On Error Resume Next
//!         MkDir folderPath
//!         If Err.Number = 0 Then count = count + 1
//!         Err.Clear
//!         On Error GoTo 0
//!     Next yr
//!     
//!     CreateYearlyArchiveFolders = count
//! End Function
//!
//! Public Function GetYearFromFilename(filename As String) As Integer
//!     Dim parts() As String
//!     Dim part As Variant
//!     Dim yr As Integer
//!     
//!     parts = Split(filename, "_")
//!     For Each part In parts
//!         If IsNumeric(part) Then
//!             yr = CInt(part)
//!             If yr >= 1900 And yr <= 9999 Then
//!                 GetYearFromFilename = yr
//!                 Exit Function
//!             End If
//!         End If
//!     Next part
//!     
//!     GetYearFromFilename = 0
//! End Function
//!
//! Public Function GenerateYearlyReport(data As Collection, reportYear As Integer) As String
//!     Dim report As String
//!     Dim item As Variant
//!     Dim itemDate As Date
//!     Dim count As Long
//!     
//!     report = "Year " & reportYear & " Report" & vbCrLf
//!     report = report & String$(50, "=") & vbCrLf
//!     
//!     count = 0
//!     For Each item In data
//!         itemDate = CDate(item)
//!         If Year(itemDate) = reportYear Then
//!             count = count + 1
//!         End If
//!     Next item
//!     
//!     report = report & "Total items: " & count & vbCrLf
//!     GenerateYearlyReport = report
//! End Function
//! ```
//!
//! ## Error Handling
//! The `Year` function can raise the following errors:
//!
//! - **Error 13 (Type mismatch)**: If the argument cannot be interpreted as a date
//! - **Error 5 (Invalid procedure call)**: If the date is outside the valid range
//! - **Returns Null**: If the input is Null (not an error)
//!
//! ## Performance Notes
//! - Very fast operation - direct extraction from date value
//! - Constant time O(1) complexity
//! - No performance penalty for different date formats
//! - Safe to call repeatedly in loops
//! - Consider caching if used extensively with same date
//!
//! ## Best Practices
//! 1. **Validate input** before calling if date source is uncertain
//! 2. **Handle Null** explicitly when working with nullable date fields
//! 3. **Use `DateSerial`** with Year for date construction/manipulation
//! 4. **Combine with Month/Day** for complete date component extraction
//! 5. **Cache results** when using same date repeatedly
//! 6. **Use fiscal year functions** for business date calculations
//! 7. **Consider leap years** when performing year-based calculations
//! 8. **Use `DateDiff`** for accurate year differences accounting for partial years
//! 9. **Test edge cases** like end-of-year dates and leap year boundaries
//! 10. **Document assumptions** about calendar systems and year ranges
//!
//! ## Comparison Table
//!
//! | Function | Returns | Range | Purpose |
//! |----------|---------|-------|---------|
//! | `Year` | Integer | 100-9999 | Year component |
//! | `Month` | Integer | 1-12 | Month component |
//! | `Day` | Integer | 1-31 | Day component |
//! | `DatePart` | Variant | Varies | Any date part |
//! | `Format$` | String | N/A | Formatted date |
//!
//! ## Platform Notes
//! - Available in VB6, VBA, and `VBScript`
//! - Consistent behavior across platforms
//! - Always returns four-digit year (not affected by regional settings)
//! - Date range: January 1, 100 to December 31, 9999
//! - Dates before year 100 or after year 9999 cause errors
//!
//! ## Limitations
//! - Cannot return two-digit year (always four digits)
//! - Cannot handle dates before year 100
//! - Cannot handle dates after year 9999
//! - No built-in fiscal year calculation (requires custom function)
//! - Does not account for different calendar systems
//! - No built-in leap year detection (requires separate function)
//! - Cannot extract century separately (must calculate from year)

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

    #[test]
    fn year_basic() {
        let source = r"
Sub Test()
    currentYear = Year(Date)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_variable_assignment() {
        let source = r"
Sub Test()
    Dim yr As Integer
    yr = Year(someDate)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_function_return() {
        let source = r"
Function GetYear(dt As Date) As Integer
    GetYear = Year(dt)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_if_statement() {
        let source = r"
Sub Test()
    If Year(dt) = 2023 Then
        Process
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_comparison() {
        let source = r"
Sub Test()
    If Year(date1) > Year(date2) Then
        Later
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_arithmetic() {
        let source = r"
Sub Test()
    age = Year(Date) - Year(birthDate)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Year: " & Year(Now)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_debug_print() {
        let source = r"
Sub Test()
    Debug.Print Year(targetDate)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_for_loop() {
        let source = r"
Sub Test()
    For i = Year(startDate) To Year(endDate)
        ProcessYear 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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_select_case() {
        let source = r"
Sub Test()
    Select Case Year(dt)
        Case 2020
            DoLeapYear
        Case 2021
            DoNormal
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_function_argument() {
        let source = r"
Sub Test()
    Call ProcessYear(Year(recordDate))
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_dateserial() {
        let source = r"
Sub Test()
    firstDay = DateSerial(Year(dt), 1, 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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_property_assignment() {
        let source = r"
Sub Test()
    obj.Year = Year(obj.Date)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_with_statement() {
        let source = r"
Sub Test()
    With dateInfo
        .Year = Year(.DateValue)
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_array_assignment() {
        let source = r"
Sub Test()
    years(i) = Year(dates(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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_concatenation() {
        let source = r#"
Sub Test()
    display = "Year " & Year(dt) & " Report"
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_parentheses() {
        let source = r"
Sub Test()
    result = (Year(dt))
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_error_handling() {
        let source = r"
Sub Test()
    On Error Resume Next
    yr = Year(userInput)
    If Err.Number <> 0 Then
        yr = 0
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_print_statement() {
        let source = r"
Sub Test()
    Print #1, Year(recordDate)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_class_usage() {
        let source = r"
Sub Test()
    Set analyzer = New YearAnalyzer
    analyzer.CurrentYear = Year(Date)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_elseif() {
        let source = r"
Sub Test()
    If x = 1 Then
        y = 1
    ElseIf Year(dt) = 2023 Then
        y = 2
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_do_while() {
        let source = r#"
Sub Test()
    Do While Year(dt) < 2025
        dt = DateAdd("yyyy", 1, dt)
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_do_until() {
        let source = r#"
Sub Test()
    Do Until Year(dt) >= targetYear
        dt = DateAdd("yyyy", 1, dt)
    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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_while_wend() {
        let source = r#"
Sub Test()
    While Year(dt) < 2030
        ProcessYear Year(dt)
        dt = DateAdd("yyyy", 1, dt)
    Wend
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_iif() {
        let source = r#"
Sub Test()
    display = IIf(Year(dt) = Year(Date), "This year", "Other year")
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_mod_operation() {
        let source = r"
Sub Test()
    isLeap = (Year(dt) Mod 4 = 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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_format() {
        let source = r#"
Sub Test()
    formatted = Format$(dt, "mmmm d, ") & Year(dt)
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn year_cstr() {
        let source = r"
Sub Test()
    yearString = CStr(Year(dt))
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/datetime/year");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}