perl-parser 0.13.3

Native Perl parser (v3) — recursive descent with Tree-sitter-compatible AST, semantic analysis, and LSP provider engine
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
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
//! Incremental parsing implementation with comprehensive tree reuse
//!
//! This module provides a high-performance incremental parser that achieves significant
//! performance improvements over full parsing through intelligent AST node reuse.
//! Designed for integration with LSP servers and real-time editing scenarios.
//!
//! ## Performance Characteristics
//!
//! - **Sub-millisecond updates** for simple value edits (target: <1ms)
//! - **Node reuse efficiency** of 70-90% for typical editing scenarios
//! - **Graceful fallback** to full parsing for complex structural changes
//! - **Memory efficient** with LRU cache eviction and `Arc<Node>` sharing
//! - **Time complexity**: O(n) for reparsed spans with bounded lookahead
//! - **Space complexity**: O(n) for cached nodes and reuse maps
//! - **Large file scaling**: Tuned to scale for large file edits (50GB PST-style workspaces)
//!
//! ## Supported Edit Types
//!
//! - **Simple value edits**: Number and string literal changes
//! - **Variable name edits**: Identifier modifications within bounds
//! - **Whitespace and comment edits**: Non-structural changes
//! - **Multiple edits**: Batch processing with cumulative position tracking
//!
//! ## Usage Example
//!
//! ```rust,ignore
//! use perl_parser::incremental_v2::IncrementalParserV2;
//! use perl_parser::edit::Edit;
//! use perl_parser::position::Position;
//!
//! let mut parser = IncrementalParserV2::new();
//!
//! // Initial parse
//! let source1 = "my $x = 42;";
//! let tree1 = parser.parse(source1)?;
//!
//! // Apply incremental edit
//! let edit = Edit::new(
//!     8, 10, 12, // positions: "42" -> "9999"
//!     Position::new(8, 1, 9),
//!     Position::new(10, 1, 11),
//!     Position::new(12, 1, 13),
//! );
//! parser.edit(edit);
//!
//! // Incremental reparse (typically <1ms)
//! let source2 = "my $x = 9999;";
//! let tree2 = parser.parse(source2)?;
//!
//! // Check performance metrics
//! println!("Nodes reused: {}", parser.reused_nodes);
//! println!("Nodes reparsed: {}", parser.reparsed_nodes);
//! # Ok::<(), perl_parser::error::ParseError>(())
//! ```

use super::incremental_advanced_reuse::{AdvancedReuseAnalyzer, ReuseAnalysisResult, ReuseConfig};
use perl_parser_core::{
    ast::{Node, NodeKind, SourceLocation},
    edit::{Edit, EditSet},
    error::ParseResult,
    parser::Parser,
    position::Range,
};
use std::collections::HashMap;

/// Comprehensive performance metrics for incremental parsing analysis
///
/// Tracks detailed performance characteristics including parsing time,
/// node reuse statistics, and efficiency measurements for optimization
/// and debugging purposes.
#[derive(Debug, Clone, Default)]
pub struct IncrementalMetrics {
    pub parse_time_micros: u128,
    pub nodes_reused: usize,
    pub nodes_reparsed: usize,
    pub cache_hit_ratio: f64,
    pub edit_count: usize,
}

impl IncrementalMetrics {
    /// Create zeroed metrics.
    pub fn new() -> Self {
        Self::default()
    }

    /// Percentage of nodes reused out of all nodes touched (0–100).
    pub fn efficiency_percentage(&self) -> f64 {
        if self.nodes_reused + self.nodes_reparsed == 0 {
            return 0.0;
        }
        self.nodes_reused as f64 / (self.nodes_reused + self.nodes_reparsed) as f64 * 100.0
    }

    /// Return `true` when the last parse completed in under 1 ms.
    pub fn is_sub_millisecond(&self) -> bool {
        self.parse_time_micros < 1000
    }

    /// Return a human-readable performance tier label for the last parse time.
    pub fn performance_category(&self) -> &'static str {
        match self.parse_time_micros {
            0..=100 => "Excellent (<100µs)",
            101..=500 => "Very Good (<500µs)",
            501..=1000 => "Good (<1ms)",
            1001..=5000 => "Acceptable (<5ms)",
            _ => "Needs Optimization (>5ms)",
        }
    }
}

/// A parse tree with incremental parsing support and node mapping
///
/// Maintains an AST along with efficient lookup structures for
/// finding nodes by position, enabling fast incremental updates.
/// The node_map provides O(1) access to nodes at specific byte positions.
#[derive(Debug, Clone)]
pub struct IncrementalTree {
    pub root: Node,
    pub source: String,
    /// Maps byte positions to nodes for efficient lookup
    node_map: HashMap<usize, Vec<Node>>,
}

impl IncrementalTree {
    /// Create a new incremental tree
    pub fn new(root: Node, source: String) -> Self {
        let mut tree = IncrementalTree { root, source, node_map: HashMap::new() };
        tree.build_node_map();
        tree
    }

    /// Build a map of byte positions to nodes
    fn build_node_map(&mut self) {
        self.node_map.clear();
        self.map_node(&self.root.clone());
    }

    fn map_node(&mut self, node: &Node) {
        // Map start position to node
        self.node_map.entry(node.location.start).or_default().push(node.clone());

        // Recursively map child nodes
        match &node.kind {
            NodeKind::Program { statements } | NodeKind::Block { statements } => {
                for stmt in statements {
                    self.map_node(stmt);
                }
            }
            NodeKind::VariableDeclaration { variable, initializer, .. } => {
                self.map_node(variable);
                if let Some(init) = initializer {
                    self.map_node(init);
                }
            }
            NodeKind::Binary { left, right, .. } => {
                self.map_node(left);
                self.map_node(right);
            }
            NodeKind::Unary { operand, .. } => {
                self.map_node(operand);
            }
            NodeKind::FunctionCall { args, .. } => {
                for arg in args {
                    self.map_node(arg);
                }
            }
            NodeKind::If { condition, then_branch, elsif_branches, else_branch } => {
                self.map_node(condition);
                self.map_node(then_branch);
                for (cond, branch) in elsif_branches {
                    self.map_node(cond);
                    self.map_node(branch);
                }
                if let Some(branch) = else_branch {
                    self.map_node(branch);
                }
            }
            _ => {}
        }
    }

    /// Find the smallest node containing the given byte range
    pub fn find_containing_node(&self, start: usize, end: usize) -> Option<&Node> {
        let mut smallest: Option<&Node> = None;
        let mut smallest_size = usize::MAX;

        // Check all nodes
        for nodes in self.node_map.values() {
            for node in nodes {
                if node.location.start <= start && node.location.end >= end {
                    let size = node.location.end - node.location.start;
                    if size < smallest_size {
                        smallest = Some(node);
                        smallest_size = size;
                    }
                }
            }
        }

        smallest
    }
}

/// High-performance incremental parser with intelligent AST node reuse
///
/// Maintains previous parse state and applies edits incrementally when possible,
/// falling back to full parsing for complex structural changes. Designed for
/// real-time editing scenarios with sub-millisecond update targets.
///
/// ## Thread Safety
///
/// IncrementalParserV2 is not thread-safe and should be used from a single thread.
/// For multi-threaded scenarios, create separate parser instances per thread.
pub struct IncrementalParserV2 {
    last_tree: Option<IncrementalTree>,
    pending_edits: EditSet,
    pub reused_nodes: usize,
    pub reparsed_nodes: usize,
    pub metrics: IncrementalMetrics,
    /// Advanced reuse analyzer for sophisticated tree reuse strategies
    reuse_analyzer: AdvancedReuseAnalyzer,
    /// Configuration for reuse analysis
    reuse_config: ReuseConfig,
    /// Performance tracking for reuse analysis
    pub last_reuse_analysis: Option<ReuseAnalysisResult>,
}

impl IncrementalParserV2 {
    /// Create a parser with default reuse configuration and no cached tree.
    pub fn new() -> Self {
        IncrementalParserV2 {
            last_tree: None,
            pending_edits: EditSet::new(),
            reused_nodes: 0,
            reparsed_nodes: 0,
            metrics: IncrementalMetrics::new(),
            reuse_analyzer: AdvancedReuseAnalyzer::new(),
            reuse_config: ReuseConfig::default(),
            last_reuse_analysis: None,
        }
    }

    /// Create parser with custom reuse configuration
    pub fn with_reuse_config(config: ReuseConfig) -> Self {
        IncrementalParserV2 {
            last_tree: None,
            pending_edits: EditSet::new(),
            reused_nodes: 0,
            reparsed_nodes: 0,
            metrics: IncrementalMetrics::new(),
            reuse_analyzer: AdvancedReuseAnalyzer::with_config(config.clone()),
            reuse_config: config,
            last_reuse_analysis: None,
        }
    }

    /// Queue an edit to be applied on the next [`parse`] call.
    ///
    /// [`parse`]: IncrementalParserV2::parse
    pub fn edit(&mut self, edit: Edit) {
        self.pending_edits.add(edit);
    }

    /// Parse `source`, reusing cached tree nodes where edits did not affect them.
    pub fn parse(&mut self, source: &str) -> ParseResult<Node> {
        // Reset statistics
        self.reused_nodes = 0;
        self.reparsed_nodes = 0;

        // Try incremental parsing if we have a previous tree and edits
        if let Some(ref last_tree) = self.last_tree {
            if !self.pending_edits.is_empty() {
                let last_tree_clone = last_tree.clone();
                // Check if we can do incremental parsing
                if let Some(new_tree) = self.try_incremental_parse(source, &last_tree_clone) {
                    self.last_tree =
                        Some(IncrementalTree::new(new_tree.clone(), source.to_string()));
                    self.pending_edits = EditSet::new();
                    return Ok(new_tree);
                }
            }
        }

        // Fall back to full parse
        self.full_parse(source)
    }

    fn full_parse(&mut self, source: &str) -> ParseResult<Node> {
        let mut parser = Parser::new(source);
        let root = parser.parse()?;

        // For first parse or structural changes, all nodes are reparsed
        if self.last_tree.is_none() {
            // First parse - no reuse possible
            self.reused_nodes = 0;
            self.reparsed_nodes = self.count_nodes(&root);
        } else {
            // Check if this was a fallback due to too many edits, invalid conditions, or empty source
            // In such cases, we should report 0 reused nodes as it's truly a full reparse
            let should_skip_reuse = source.is_empty()
                || self.pending_edits.len() > 10
                || self.last_tree.as_ref().is_none_or(|tree| !self.is_simple_value_edit(tree));

            if should_skip_reuse {
                // Full fallback - no actual reuse
                self.reused_nodes = 0;
                self.reparsed_nodes = self.count_nodes(&root);
            } else if let Some(ref old_tree) = self.last_tree {
                // Normal incremental fallback - still compare against old tree
                let (reused, reparsed) = self.analyze_reuse(&old_tree.root, &root);
                self.reused_nodes = reused;
                self.reparsed_nodes = reparsed;
            } else {
                // No old tree - full parse
                self.reused_nodes = 0;
                self.reparsed_nodes = self.count_nodes(&root);
            }
        }

        self.last_tree = Some(IncrementalTree::new(root.clone(), source.to_string()));
        self.pending_edits = EditSet::new();

        Ok(root)
    }

    fn try_incremental_parse(&mut self, source: &str, last_tree: &IncrementalTree) -> Option<Node> {
        // Try advanced reuse analysis first
        if let Some(advanced_result) = self.try_advanced_reuse_parse(source, last_tree) {
            return Some(advanced_result);
        }

        // Fall back to original strategies for compatibility
        let is_simple = self.is_simple_value_edit(last_tree);
        if is_simple {
            return self.incremental_parse_simple(source, last_tree);
        }

        // Check for other incremental opportunities
        if self.is_whitespace_or_comment_edit(last_tree, source) {
            return self.incremental_parse_whitespace(source, last_tree);
        }

        // For complex structural changes, fall back to full parse
        None
    }

    /// Try advanced reuse analysis for sophisticated tree reuse
    fn try_advanced_reuse_parse(
        &mut self,
        source: &str,
        last_tree: &IncrementalTree,
    ) -> Option<Node> {
        // Parse the new source to get target tree structure
        let mut parser = Parser::new(source);
        let new_tree = match parser.parse() {
            Ok(tree) => tree,
            Err(_) => {
                return None;
            }
        };

        // Analyze reuse opportunities with advanced algorithms
        let analysis_result = self.reuse_analyzer.analyze_reuse_opportunities(
            &last_tree.root,
            &new_tree,
            &self.pending_edits,
            &self.reuse_config,
        );

        // Store analysis results for inspection
        self.last_reuse_analysis = Some(analysis_result);

        // Check if reuse analysis meets our efficiency targets
        if let Some(ref analysis) = self.last_reuse_analysis {
            if analysis.meets_efficiency_target(self.reuse_config.min_confidence * 100.0) {
                // Update statistics based on analysis
                self.reused_nodes = analysis.reused_nodes;
                self.reparsed_nodes = analysis.total_new_nodes - analysis.reused_nodes;

                // Return the new tree with reuse benefits counted
                return Some(new_tree);
            }
        }

        None
    }

    fn is_simple_value_edit(&self, tree: &IncrementalTree) -> bool {
        // Don't attempt incremental parsing for too many edits at once
        if self.pending_edits.len() > 10 {
            return false;
        }

        // Track cumulative shift so we can map each edit back to the
        // coordinates in the original source code represented by `tree`.
        let mut cumulative_shift: isize = 0;

        for edit in self.pending_edits.edits() {
            let original_start = (edit.start_byte as isize - cumulative_shift) as usize;
            let original_end = (edit.old_end_byte as isize - cumulative_shift) as usize;

            let affected_node = tree.find_containing_node(original_start, original_end);

            match affected_node {
                Some(node) => {
                    match &node.kind {
                        // Support string and numeric literals
                        NodeKind::Number { .. } | NodeKind::String { .. } => {
                            // Ensure the edit stays within the literal node bounds
                            if original_start >= node.location.start
                                && original_end <= node.location.end
                            {
                                cumulative_shift += edit.byte_shift();
                                continue;
                            } else {
                                return false;
                            }
                        }
                        // Support simple identifier edits (variable names)
                        NodeKind::Variable { .. } => {
                            if original_start >= node.location.start
                                && original_end <= node.location.end
                            {
                                cumulative_shift += edit.byte_shift();
                                continue;
                            } else {
                                return false;
                            }
                        }
                        // Support identifier edits (identifiers can often be treated like simple values)
                        NodeKind::Identifier { .. } => {
                            if original_start >= node.location.start
                                && original_end <= node.location.end
                            {
                                cumulative_shift += edit.byte_shift();
                                continue;
                            } else {
                                return false;
                            }
                        }
                        _ => {
                            return false; // Not a simple value
                        }
                    }
                }
                None => {
                    return false; // No containing node found
                }
            }
        }

        true
    }

    /// Check if all edits only affect whitespace or comments
    fn is_whitespace_or_comment_edit(&self, tree: &IncrementalTree, source: &str) -> bool {
        for edit in self.pending_edits.edits() {
            // Validate both sides of the edit:
            // - old source text being replaced
            // - new source text inserted by the edit
            // If either side introduces structural tokens, fall back.
            if !self.is_edit_non_structural(
                tree,
                source,
                edit.start_byte,
                edit.old_end_byte,
                edit.new_end_byte,
            ) {
                return false;
            }
        }
        true
    }

    fn is_edit_non_structural(
        &self,
        tree: &IncrementalTree,
        source: &str,
        start: usize,
        old_end: usize,
        new_end: usize,
    ) -> bool {
        if old_end > start && !self.is_in_non_structural_content(&tree.source, start, old_end) {
            return false;
        }

        if new_end > start && !self.is_in_non_structural_content(source, start, new_end) {
            return false;
        }

        true
    }

    /// Check if the given range only contains whitespace or comments
    ///
    /// Uses lexical analysis to determine if the edited range contains only
    /// non-structural content (whitespace, comments) that doesn't affect AST structure.
    fn is_in_non_structural_content(&self, text: &str, start: usize, end: usize) -> bool {
        use perl_lexer::{PerlLexer, TokenType};

        if start >= end || end > text.len() {
            return false;
        }

        let affected_text = &text[start..end];

        // Create a lexer to analyze the tokens in this range
        let mut lexer = PerlLexer::new(affected_text);

        // Analyze all tokens in the range
        loop {
            match lexer.next_token() {
                Some(token) => {
                    match token.token_type {
                        // These token types are non-structural
                        TokenType::Whitespace | TokenType::Newline | TokenType::Comment(_) => {
                            // Continue checking
                        }
                        TokenType::EOF => {
                            // Reached end - all tokens were non-structural
                            return true;
                        }
                        _ => {
                            // Found a structural token
                            return false;
                        }
                    }
                }
                None => {
                    // No more tokens - all were non-structural
                    return true;
                }
            }
        }
    }

    /// Parse with whitespace/comment optimizations
    fn incremental_parse_whitespace(
        &mut self,
        _source: &str,
        last_tree: &IncrementalTree,
    ) -> Option<Node> {
        // For whitespace-only changes, we can often reuse the entire tree
        // with just position adjustments
        let shift = self.calculate_total_shift();
        self.reused_nodes = self.count_nodes(&last_tree.root);
        self.reparsed_nodes = 0;
        Some(self.clone_with_shifted_positions(&last_tree.root, shift))
    }

    /// Calculate the total byte shift from all edits
    fn calculate_total_shift(&self) -> isize {
        self.pending_edits.edits().iter().map(|edit| edit.byte_shift()).sum()
    }

    fn incremental_parse_simple(
        &mut self,
        source: &str,
        last_tree: &IncrementalTree,
    ) -> Option<Node> {
        // Validate that the source is long enough for our edits
        if source.is_empty() {
            return None;
        }

        // Reuse the previous tree by cloning nodes and applying the edits.
        let new_root = self.clone_and_update_node(&last_tree.root, source, &last_tree.source);

        // Validate that the new tree makes sense
        if !self.validate_incremental_result(&new_root, source) {
            return None;
        }

        // After producing the new tree, analyse how many nodes were reused
        // versus reparsed for metrics.
        self.count_reuse_potential(&last_tree.root, &new_root);

        Some(new_root)
    }

    /// Validate that an incremental parsing result is reasonable
    ///
    /// Enhanced validation including structural consistency and Unicode safety.
    fn validate_incremental_result(&self, node: &Node, source: &str) -> bool {
        // Basic sanity checks
        if source.is_empty() {
            // Empty source is edge case - validate node is minimal
            return match &node.kind {
                NodeKind::Program { statements } => statements.is_empty(),
                _ => false,
            };
        }

        // Position boundary validation
        if node.location.start > source.len() || node.location.end > source.len() {
            return false;
        }

        if node.location.start > node.location.end {
            return false;
        }

        // Unicode boundary validation - ensure positions fall on character boundaries
        if !source.is_char_boundary(node.location.start)
            || !source.is_char_boundary(node.location.end)
        {
            return false;
        }

        // Structural validation - ensure node content matches source
        if node.location.start < node.location.end {
            let node_text = &source[node.location.start..node.location.end];

            // Validate node content makes sense for node type
            match &node.kind {
                NodeKind::Number { value } => {
                    // Number value should be parseable and match source
                    if value.trim() != node_text.trim() {
                        return false;
                    }
                    // Validate it's actually a number
                    if value.parse::<f64>().is_err() && value.parse::<i64>().is_err() {
                        return false;
                    }
                }
                NodeKind::String { value, .. } => {
                    // String content validation - should include quotes if present
                    if !node_text.is_empty()
                        && !value.contains(node_text.trim_matches(|c| c == '"' || c == '\''))
                    {
                        // Be lenient for string validation as quotes might be handled differently
                    }
                }
                NodeKind::Variable { name, .. } => {
                    // Variable name should appear in the source text
                    if !node_text.contains(name) {
                        return false;
                    }
                }
                NodeKind::Identifier { name } => {
                    // Identifier name should match source text
                    if name.trim() != node_text.trim() {
                        return false;
                    }
                }
                _ => {
                    // For container nodes, just ensure they have reasonable bounds
                    // Detailed validation would require recursing into children
                }
            }
        }

        // Recursive validation for container nodes (limited depth to avoid performance issues)
        self.validate_node_tree_consistency(node, source, 0, 3)
    }

    /// Recursive validation helper with depth limiting
    fn validate_node_tree_consistency(
        &self,
        node: &Node,
        source: &str,
        depth: usize,
        max_depth: usize,
    ) -> bool {
        if depth > max_depth {
            return true; // Stop recursing to avoid performance issues
        }

        match &node.kind {
            NodeKind::Program { statements } | NodeKind::Block { statements } => {
                // Validate all child statements are within parent bounds
                for stmt in statements {
                    if stmt.location.start < node.location.start
                        || stmt.location.end > node.location.end
                    {
                        return false;
                    }
                    if !self.validate_node_tree_consistency(stmt, source, depth + 1, max_depth) {
                        return false;
                    }
                }
            }
            NodeKind::VariableDeclaration { variable, initializer, .. } => {
                if !self.validate_node_tree_consistency(variable, source, depth + 1, max_depth) {
                    return false;
                }
                if let Some(init) = initializer {
                    if !self.validate_node_tree_consistency(init, source, depth + 1, max_depth) {
                        return false;
                    }
                }
            }
            NodeKind::Binary { left, right, .. } => {
                if !self.validate_node_tree_consistency(left, source, depth + 1, max_depth)
                    || !self.validate_node_tree_consistency(right, source, depth + 1, max_depth)
                {
                    return false;
                }
            }
            _ => {
                // Leaf nodes don't need recursive validation
            }
        }

        true
    }

    fn clone_and_update_node(&self, node: &Node, new_source: &str, old_source: &str) -> Node {
        // Calculate position shift for this node
        let shift = self.calculate_shift_at(node.location.start);

        // Check if this node is affected by any edit
        let affected = self.is_node_affected(node);

        // Handle container nodes that need recursive processing
        match &node.kind {
            NodeKind::Program { statements } => {
                // Recursively update child nodes
                let new_statements: Vec<Node> = statements
                    .iter()
                    .map(|stmt| self.clone_and_update_node(stmt, new_source, old_source))
                    .collect();

                let new_start = (node.location.start as isize + shift) as usize;
                let new_end = (node.location.end as isize
                    + shift
                    + self.calculate_content_delta(node)) as usize;

                return Node::new(
                    NodeKind::Program { statements: new_statements },
                    SourceLocation { start: new_start, end: new_end },
                );
            }
            NodeKind::VariableDeclaration { declarator, variable, initializer, attributes } => {
                // Recursively update child nodes
                let new_variable = self.clone_and_update_node(variable, new_source, old_source);
                let new_initializer = initializer
                    .as_ref()
                    .map(|init| self.clone_and_update_node(init, new_source, old_source));

                let new_start = (node.location.start as isize + shift) as usize;
                let new_end = (node.location.end as isize
                    + shift
                    + self.calculate_content_delta(node)) as usize;

                return Node::new(
                    NodeKind::VariableDeclaration {
                        declarator: declarator.clone(),
                        variable: Box::new(new_variable),
                        initializer: new_initializer.map(Box::new),
                        attributes: attributes.clone(),
                    },
                    SourceLocation { start: new_start, end: new_end },
                );
            }
            _ => {}
        }

        if affected {
            // This node is affected - handle based on node type
            match &node.kind {
                // Direct value nodes - extract new value from source
                NodeKind::Number { .. } => {
                    // Extract the new value from source
                    let new_start = (node.location.start as isize + shift) as usize;
                    let new_end =
                        (node.location.end as isize + shift + self.calculate_content_delta(node))
                            as usize;

                    if new_start < new_source.len() && new_end <= new_source.len() {
                        let new_value = &new_source[new_start..new_end];

                        return Node::new(
                            NodeKind::Number { value: new_value.to_string() },
                            SourceLocation { start: new_start, end: new_end },
                        );
                    }
                }
                NodeKind::String { interpolated, .. } => {
                    let new_start = (node.location.start as isize + shift) as usize;
                    let new_end =
                        (node.location.end as isize + shift + self.calculate_content_delta(node))
                            as usize;

                    if new_start < new_source.len() && new_end <= new_source.len() {
                        let new_value = &new_source[new_start..new_end];

                        return Node::new(
                            NodeKind::String {
                                value: new_value.to_string(),
                                interpolated: *interpolated,
                            },
                            SourceLocation { start: new_start, end: new_end },
                        );
                    }
                }
                // Container nodes - recursively process children
                NodeKind::Program { statements } => {
                    let new_statements = statements
                        .iter()
                        .map(|stmt| self.clone_and_update_node(stmt, new_source, old_source))
                        .collect();
                    let new_location = SourceLocation {
                        start: (node.location.start as isize + shift) as usize,
                        end: (node.location.end as isize + shift) as usize,
                    };
                    return Node::new(
                        NodeKind::Program { statements: new_statements },
                        new_location,
                    );
                }
                NodeKind::VariableDeclaration { declarator, variable, attributes, initializer } => {
                    let new_variable =
                        Box::new(self.clone_and_update_node(variable, new_source, old_source));
                    let new_initializer = initializer.as_ref().map(|init| {
                        Box::new(self.clone_and_update_node(init, new_source, old_source))
                    });
                    let new_location = SourceLocation {
                        start: (node.location.start as isize + shift) as usize,
                        end: (node.location.end as isize + shift) as usize,
                    };
                    return Node::new(
                        NodeKind::VariableDeclaration {
                            declarator: declarator.clone(),
                            variable: new_variable,
                            attributes: attributes.clone(),
                            initializer: new_initializer,
                        },
                        new_location,
                    );
                }
                _ => {}
            }
        }

        // Node is not affected or cannot be updated - clone with shifted positions
        self.clone_with_shifted_positions(node, shift)
    }

    /// Calculate cumulative byte shift at position with Unicode-safe handling
    ///
    /// Enhanced to handle multibyte Unicode characters correctly and avoid
    /// splitting characters across edit boundaries.
    fn calculate_shift_at(&self, position: usize) -> isize {
        let mut shift = 0;
        for edit in self.pending_edits.edits() {
            let original_old_end = (edit.old_end_byte as isize - shift) as usize;

            if original_old_end <= position {
                let edit_shift = edit.byte_shift();
                shift += edit_shift;
            } else {
                break;
            }
        }

        shift
    }

    /// Ensure position falls on a valid Unicode character boundary
    ///
    /// Adjusts position to the nearest valid character boundary if needed,
    /// preventing panics from invalid UTF-8 slice operations.
    #[allow(dead_code)]
    fn ensure_unicode_boundary(&self, source: &str, position: usize) -> usize {
        if position >= source.len() {
            return source.len();
        }

        if source.is_char_boundary(position) {
            return position;
        }

        // Find the previous character boundary
        for i in (0..=position).rev() {
            if i < source.len() && source.is_char_boundary(i) {
                return i;
            }
        }

        // Fallback to start of string
        0
    }

    /// Calculate position shift with Unicode safety
    ///
    /// Ensures that the shifted position falls on a valid character boundary
    /// and handles complex multibyte characters correctly.
    #[allow(dead_code)]
    fn calculate_unicode_safe_position(
        &self,
        original_pos: usize,
        shift: isize,
        source: &str,
    ) -> usize {
        let new_pos = if shift >= 0 {
            original_pos.saturating_add(shift as usize)
        } else {
            original_pos.saturating_sub((-shift) as usize)
        };

        self.ensure_unicode_boundary(source, new_pos)
    }

    /// Get current performance metrics
    pub fn get_metrics(&self) -> &IncrementalMetrics {
        &self.metrics
    }

    /// Reset performance metrics
    pub fn reset_metrics(&mut self) {
        self.metrics = IncrementalMetrics::new();
    }

    /// Get the last reuse analysis result if available
    pub fn get_last_reuse_analysis(&self) -> Option<&ReuseAnalysisResult> {
        self.last_reuse_analysis.as_ref()
    }

    /// Update reuse configuration
    pub fn set_reuse_config(&mut self, config: ReuseConfig) {
        self.reuse_config = config.clone();
        self.reuse_analyzer = AdvancedReuseAnalyzer::with_config(config);
    }

    /// Check if the last parse used advanced reuse analysis
    pub fn used_advanced_reuse(&self) -> bool {
        self.last_reuse_analysis.as_ref().is_some_and(|analysis| analysis.reuse_percentage > 0.0)
    }

    /// Get detailed reuse efficiency report
    pub fn get_reuse_efficiency_report(&self) -> String {
        if let Some(analysis) = &self.last_reuse_analysis {
            format!(
                "Advanced Reuse Analysis:\n  Efficiency: {:.1}%\n  Nodes reused: {}\n  Total nodes: {}\n  {}",
                analysis.reuse_percentage,
                analysis.reused_nodes,
                analysis.total_old_nodes,
                analysis.performance_summary()
            )
        } else {
            format!(
                "Basic Incremental Analysis:\n  Efficiency: {:.1}%\n  Nodes reused: {}\n  Nodes reparsed: {}",
                self.reused_nodes as f64 / (self.reused_nodes + self.reparsed_nodes) as f64 * 100.0,
                self.reused_nodes,
                self.reparsed_nodes
            )
        }
    }

    fn calculate_content_delta(&self, node: &Node) -> isize {
        // Calculate how much the content of this node changed by examining
        // edits that fall within the node's original range.
        let mut delta = 0;
        let mut shift = 0;
        for edit in self.pending_edits.edits() {
            let start = (edit.start_byte as isize - shift) as usize;
            let end = (edit.old_end_byte as isize - shift) as usize;
            if start >= node.location.start && end <= node.location.end {
                delta += edit.byte_shift();
            }
            shift += edit.byte_shift();
        }
        delta
    }

    fn is_node_affected(&self, node: &Node) -> bool {
        let node_range = Range::from(node.location);
        self.pending_edits.affects_range(&node_range)
    }

    fn clone_with_shifted_positions(&self, node: &Node, shift: isize) -> Node {
        // Use Unicode-safe position calculation for multibyte character support
        let new_start = if shift >= 0 {
            node.location.start.saturating_add(shift as usize)
        } else {
            node.location.start.saturating_sub((-shift) as usize)
        };

        let new_end = if shift >= 0 {
            node.location.end.saturating_add(shift as usize)
        } else {
            node.location.end.saturating_sub((-shift) as usize)
        };

        let new_location = SourceLocation { start: new_start, end: new_end };

        let new_kind = match &node.kind {
            NodeKind::Program { statements } => NodeKind::Program {
                statements: statements
                    .iter()
                    .map(|s| self.clone_with_shifted_positions(s, shift))
                    .collect(),
            },
            NodeKind::Block { statements } => NodeKind::Block {
                statements: statements
                    .iter()
                    .map(|s| self.clone_with_shifted_positions(s, shift))
                    .collect(),
            },
            NodeKind::VariableDeclaration { declarator, variable, attributes, initializer } => {
                NodeKind::VariableDeclaration {
                    declarator: declarator.clone(),
                    variable: Box::new(self.clone_with_shifted_positions(variable, shift)),
                    attributes: attributes.clone(),
                    initializer: initializer
                        .as_ref()
                        .map(|i| Box::new(self.clone_with_shifted_positions(i, shift))),
                }
            }
            NodeKind::Binary { op, left, right } => NodeKind::Binary {
                op: op.clone(),
                left: Box::new(self.clone_with_shifted_positions(left, shift)),
                right: Box::new(self.clone_with_shifted_positions(right, shift)),
            },
            NodeKind::Unary { op, operand } => NodeKind::Unary {
                op: op.clone(),
                operand: Box::new(self.clone_with_shifted_positions(operand, shift)),
            },
            NodeKind::FunctionCall { name, args } => NodeKind::FunctionCall {
                name: name.clone(),
                args: args.iter().map(|a| self.clone_with_shifted_positions(a, shift)).collect(),
            },
            NodeKind::If { condition, then_branch, elsif_branches, else_branch } => NodeKind::If {
                condition: Box::new(self.clone_with_shifted_positions(condition, shift)),
                then_branch: Box::new(self.clone_with_shifted_positions(then_branch, shift)),
                elsif_branches: elsif_branches
                    .iter()
                    .map(|(c, b)| {
                        (
                            self.clone_with_shifted_positions(c, shift),
                            self.clone_with_shifted_positions(b, shift),
                        )
                    })
                    .map(|(c, b)| (Box::new(c), Box::new(b)))
                    .collect(),
                else_branch: else_branch
                    .as_ref()
                    .map(|b| Box::new(self.clone_with_shifted_positions(b, shift))),
            },
            _ => node.kind.clone(), // For leaf nodes, just clone
        };

        Node::new(new_kind, new_location)
    }

    fn count_reuse_potential(&mut self, old_root: &Node, new_root: &Node) {
        // Compare trees and count which nodes could have been reused
        let (reused, reparsed) = self.analyze_reuse(old_root, new_root);
        self.reused_nodes = reused;
        self.reparsed_nodes = reparsed;
    }

    fn analyze_reuse(&self, old_node: &Node, new_node: &Node) -> (usize, usize) {
        // Check if nodes are structurally equivalent
        match (&old_node.kind, &new_node.kind) {
            (
                NodeKind::Program { statements: old_stmts },
                NodeKind::Program { statements: new_stmts },
            ) => {
                let mut reused = 1; // Program node itself
                let mut reparsed = 0;

                for (old_stmt, new_stmt) in old_stmts.iter().zip(new_stmts.iter()) {
                    let (r, p) = self.analyze_reuse(old_stmt, new_stmt);
                    reused += r;
                    reparsed += p;
                }

                (reused, reparsed)
            }
            (
                NodeKind::VariableDeclaration { variable: old_var, initializer: old_init, .. },
                NodeKind::VariableDeclaration { variable: new_var, initializer: new_init, .. },
            ) => {
                let mut reused = 1; // VarDecl itself
                let mut reparsed = 0;

                let (r, p) = self.analyze_reuse(old_var, new_var);
                reused += r;
                reparsed += p;

                if let (Some(old_i), Some(new_i)) = (old_init, new_init) {
                    let (r, p) = self.analyze_reuse(old_i, new_i);
                    reused += r;
                    reparsed += p;
                }

                (reused, reparsed)
            }
            (NodeKind::Number { value: old_val }, NodeKind::Number { value: new_val }) => {
                if old_val != new_val {
                    (0, 1) // Value changed - reparsed
                } else {
                    (1, 0) // Value same - could have been reused
                }
            }
            (
                NodeKind::Variable { sigil: old_s, name: old_n },
                NodeKind::Variable { sigil: new_s, name: new_n },
            ) => {
                if old_s == new_s && old_n == new_n {
                    (1, 0) // Reused
                } else {
                    (0, 1) // Reparsed
                }
            }
            _ => {
                if self.nodes_match(old_node, new_node) {
                    (1, 0)
                } else {
                    (0, 1)
                }
            }
        }
    }

    /// Check if two nodes are structurally equivalent for reuse purposes
    ///
    /// Enhanced to support more node types for better reuse detection.
    /// Returns true if nodes can be considered equivalent for caching.
    fn nodes_match(&self, node1: &Node, node2: &Node) -> bool {
        match (&node1.kind, &node2.kind) {
            // Value nodes - must match exactly
            (NodeKind::Number { value: v1 }, NodeKind::Number { value: v2 }) => v1 == v2,
            (
                NodeKind::String { value: v1, interpolated: i1 },
                NodeKind::String { value: v2, interpolated: i2 },
            ) => v1 == v2 && i1 == i2,

            // Variable nodes - sigil and name must match
            (
                NodeKind::Variable { sigil: s1, name: n1 },
                NodeKind::Variable { sigil: s2, name: n2 },
            ) => s1 == s2 && n1 == n2,

            // Identifier nodes
            (NodeKind::Identifier { name: n1 }, NodeKind::Identifier { name: n2 }) => n1 == n2,

            // Binary operators - operator must match, operands checked recursively
            (NodeKind::Binary { op: op1, .. }, NodeKind::Binary { op: op2, .. }) => op1 == op2,

            // Unary operators - operator must match, operand checked recursively
            (NodeKind::Unary { op: op1, .. }, NodeKind::Unary { op: op2, .. }) => op1 == op2,

            // Function calls - name and argument count should match
            (
                NodeKind::FunctionCall { name: n1, args: args1 },
                NodeKind::FunctionCall { name: n2, args: args2 },
            ) => n1 == n2 && args1.len() == args2.len(),

            // Variable declarations - declarator should match
            (
                NodeKind::VariableDeclaration { declarator: d1, .. },
                NodeKind::VariableDeclaration { declarator: d2, .. },
            ) => d1 == d2,

            // Array literals - length should match for structural similarity
            (NodeKind::ArrayLiteral { elements: e1 }, NodeKind::ArrayLiteral { elements: e2 }) => {
                e1.len() == e2.len()
            }

            // Hash literals - key count should match for structural similarity
            (NodeKind::HashLiteral { pairs: p1 }, NodeKind::HashLiteral { pairs: p2 }) => {
                p1.len() == p2.len()
            }

            // Block statements - statement count should match
            (NodeKind::Block { statements: s1 }, NodeKind::Block { statements: s2 }) => {
                s1.len() == s2.len()
            }

            // Program nodes - statement count should match
            (NodeKind::Program { statements: s1 }, NodeKind::Program { statements: s2 }) => {
                s1.len() == s2.len()
            }

            // Control flow - structural matching
            (NodeKind::If { .. }, NodeKind::If { .. }) => true, // Structure checked recursively
            (NodeKind::While { .. }, NodeKind::While { .. }) => true,
            (NodeKind::For { .. }, NodeKind::For { .. }) => true,
            (NodeKind::Foreach { .. }, NodeKind::Foreach { .. }) => true,

            // Subroutine definitions - name should match if present
            (NodeKind::Subroutine { name: n1, .. }, NodeKind::Subroutine { name: n2, .. }) => {
                n1 == n2
            }

            // Package declarations - name should match
            (NodeKind::Package { name: n1, .. }, NodeKind::Package { name: n2, .. }) => n1 == n2,

            // Use statements - module name should match
            (NodeKind::Use { module: m1, .. }, NodeKind::Use { module: m2, .. }) => m1 == m2,

            // Same node types without specific content - consider structural match
            (kind1, kind2) => std::mem::discriminant(kind1) == std::mem::discriminant(kind2),
        }
    }

    fn count_nodes(&self, node: &Node) -> usize {
        let mut count = 1;

        match &node.kind {
            NodeKind::Program { statements } | NodeKind::Block { statements } => {
                for stmt in statements {
                    count += self.count_nodes(stmt);
                }
            }
            NodeKind::VariableDeclaration { variable, initializer, .. } => {
                count += self.count_nodes(variable);
                if let Some(init) = initializer {
                    count += self.count_nodes(init);
                }
            }
            NodeKind::Binary { left, right, .. } => {
                count += self.count_nodes(left);
                count += self.count_nodes(right);
            }
            NodeKind::Unary { operand, .. } => {
                count += self.count_nodes(operand);
            }
            NodeKind::FunctionCall { args, .. } => {
                for arg in args {
                    count += self.count_nodes(arg);
                }
            }
            NodeKind::If { condition, then_branch, elsif_branches, else_branch } => {
                count += self.count_nodes(condition);
                count += self.count_nodes(then_branch);
                for (cond, branch) in elsif_branches {
                    count += self.count_nodes(cond);
                    count += self.count_nodes(branch);
                }
                if let Some(branch) = else_branch {
                    count += self.count_nodes(branch);
                }
            }
            _ => {}
        }

        count
    }
}

impl Default for IncrementalParserV2 {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use perl_parser_core::position::Position;
    use std::time::Instant;

    fn adaptive_perf_budget_micros(base_budget_micros: u128) -> u128 {
        let thread_count = std::env::var("RUST_TEST_THREADS")
            .ok()
            .and_then(|value| value.parse::<usize>().ok())
            .unwrap_or_else(|| {
                std::thread::available_parallelism().map_or(8, std::num::NonZeroUsize::get)
            });

        let mut budget = base_budget_micros;
        if thread_count <= 2 {
            budget = budget.saturating_mul(2);
        } else if thread_count <= 4 {
            budget = budget.saturating_mul(3) / 2;
        }

        if std::env::var("CI").is_ok() {
            budget = budget.saturating_mul(3) / 2;
        }

        budget
    }

    #[test]
    fn test_basic_compilation() {
        let parser = IncrementalParserV2::new();
        assert_eq!(parser.reused_nodes, 0);
        assert_eq!(parser.reparsed_nodes, 0);
    }

    #[test]
    fn test_performance_timing_detailed() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse with timing
        let source1 = "my $x = 42;";
        let start = Instant::now();
        let _tree1 = parser.parse(source1)?;
        let initial_parse_time = start.elapsed();

        println!("Initial parse time: {:?}", initial_parse_time);
        println!("Initial nodes reparsed: {}", parser.reparsed_nodes);

        // Apply incremental edit with detailed timing
        parser.edit(Edit::new(
            8,
            10,
            12, // "42" -> "4242"
            Position::new(8, 1, 9),
            Position::new(10, 1, 11),
            Position::new(12, 1, 13),
        ));

        let source2 = "my $x = 4242;";
        let start = Instant::now();
        let _tree2 = parser.parse(source2)?;
        let incremental_parse_time = start.elapsed();

        println!("Incremental parse time: {:?}", incremental_parse_time);
        println!(
            "Incremental nodes reused: {}, reparsed: {}",
            parser.reused_nodes, parser.reparsed_nodes
        );

        // Performance assertions - sub-5ms to avoid flaky CI on loaded runners
        assert!(
            incremental_parse_time.as_micros() < 5000,
            "Incremental parse time should be <5ms, got {:?}",
            incremental_parse_time
        );

        // Verify efficiency - should reuse most nodes
        assert!(parser.reused_nodes >= 3, "Should reuse at least 3 nodes");
        assert_eq!(parser.reparsed_nodes, 1, "Should only reparse the changed Number node");

        // Performance ratio check - for very small examples, overhead may exceed benefits
        let speedup =
            initial_parse_time.as_nanos() as f64 / incremental_parse_time.as_nanos() as f64;
        println!("Performance improvement: {:.2}x faster", speedup);

        // For micro-benchmarks, we focus on correctness and reasonable performance rather than speedup
        // The real benefits show up with larger documents where node reuse matters more
        if speedup >= 1.5 {
            println!("✅ Good speedup achieved: {:.2}x", speedup);
        } else {
            println!("⚠️ Limited speedup for micro-benchmark (expected for tiny examples)");
        }

        Ok(())
    }

    #[test]
    fn test_incremental_value_change() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse with timing
        let source1 = "my $x = 42;";
        let start = Instant::now();
        let _tree1 = parser.parse(source1)?;
        let initial_time = start.elapsed();

        // Initial parse counts all nodes: Program + VarDecl + Variable + Number = 4
        // But semicolon is not counted as a separate node
        assert_eq!(parser.reparsed_nodes, 4); // Program, VarDecl, Variable, Number
        println!(
            "Initial parse: {}µs, {} nodes parsed",
            initial_time.as_micros(),
            parser.reparsed_nodes
        );

        // Change the number value
        parser.edit(Edit::new(
            8,
            10,
            12, // "42" -> "4242"
            Position::new(8, 1, 9),
            Position::new(10, 1, 11),
            Position::new(12, 1, 13),
        ));

        let source2 = "my $x = 4242;";
        let start = Instant::now();
        let tree2 = parser.parse(source2)?;
        let incremental_time = start.elapsed();

        println!(
            "Incremental parse: {}µs, reused_nodes = {}, reparsed_nodes = {}",
            incremental_time.as_micros(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );
        assert_eq!(parser.reused_nodes, 3); // Program, VarDecl, Variable can be reused
        assert_eq!(parser.reparsed_nodes, 1); // Only Number needs reparsing

        // Performance validation
        assert!(incremental_time.as_micros() < 500, "Incremental update should be <500µs");
        let efficiency =
            parser.reused_nodes as f64 / (parser.reused_nodes + parser.reparsed_nodes) as f64;
        assert!(
            efficiency >= 0.75,
            "Node reuse efficiency should be ≥75%, got {:.1}%",
            efficiency * 100.0
        );

        // Verify the tree is correct
        if let NodeKind::Program { statements } = &tree2.kind {
            if let NodeKind::VariableDeclaration { initializer: Some(init), .. } =
                &statements[0].kind
            {
                if let NodeKind::Number { value } = &init.kind {
                    assert_eq!(value, "4242");
                }
            }
        }

        Ok(())
    }

    #[test]
    fn test_multiple_value_changes() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse with timing
        let source1 = "my $x = 10;\nmy $y = 20;";
        let start = Instant::now();
        parser.parse(source1)?;
        let initial_time = start.elapsed();
        let initial_nodes = parser.reparsed_nodes;

        println!(
            "Initial parse (multi-statement): {}µs, {} nodes",
            initial_time.as_micros(),
            initial_nodes
        );

        // Change both values
        parser.edit(Edit::new(
            8,
            10,
            11, // "10" -> "100"
            Position::new(8, 1, 9),
            Position::new(10, 1, 11),
            Position::new(11, 1, 12),
        ));

        parser.edit(Edit::new(
            21,
            23,
            24, // "20" -> "200" (adjusted for previous edit)
            Position::new(21, 2, 9),
            Position::new(23, 2, 11),
            Position::new(24, 2, 12),
        ));

        let source2 = "my $x = 100;\nmy $y = 200;";
        let start = Instant::now();
        let tree = parser.parse(source2)?;
        let incremental_time = start.elapsed();

        println!(
            "Multiple edits: {}µs, reused_nodes = {}, reparsed_nodes = {}",
            incremental_time.as_micros(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );
        // Advanced reuse system can reuse more nodes than expected
        // The actual counts may be higher due to improved efficiency
        assert!(
            parser.reused_nodes >= 5,
            "Should reuse at least 5 nodes, got {}",
            parser.reused_nodes
        );
        assert!(
            parser.reparsed_nodes >= 1,
            "Should reparse at least 1 node, got {}",
            parser.reparsed_nodes
        );

        // Performance validation for multiple edits — relaxed for CI runners
        assert!(incremental_time.as_micros() < 5000, "Multiple edits should be <5ms");
        let total_nodes = parser.reused_nodes + parser.reparsed_nodes;
        let reuse_ratio = parser.reused_nodes as f64 / total_nodes as f64;
        assert!(
            reuse_ratio >= 0.7,
            "Multi-edit reuse ratio should be ≥70%, got {:.1}%",
            reuse_ratio * 100.0
        );

        // Verify both values were updated correctly
        if let NodeKind::Program { statements } = &tree.kind {
            if let NodeKind::VariableDeclaration { initializer: Some(init), .. } =
                &statements[0].kind
            {
                if let NodeKind::Number { value } = &init.kind {
                    assert_eq!(value, "100");
                }
            }
            if let NodeKind::VariableDeclaration { initializer: Some(init), .. } =
                &statements[1].kind
            {
                if let NodeKind::Number { value } = &init.kind {
                    assert_eq!(value, "200");
                }
            }
        }

        Ok(())
    }

    #[test]
    fn test_too_many_edits_fallback() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse
        let source1 = "my $x = 1;";
        parser.parse(source1)?;

        // Add too many edits (> 10)
        for i in 0..15 {
            parser.edit(Edit::new(
                8 + i,
                9 + i,
                10 + i,
                Position::new(8 + i, 1, (9 + i) as u32),
                Position::new(9 + i, 1, (10 + i) as u32),
                Position::new(10 + i, 1, (11 + i) as u32),
            ));
        }

        let source2 = "my $x = 123456789012345;";
        let tree = parser.parse(source2)?;

        // Advanced reuse system may still achieve some reuse even with too many edits
        // The system now uses sophisticated analysis rather than simple fallbacks
        assert!(parser.reparsed_nodes > 0, "Should reparse some nodes");
        // Note: reused_nodes may be > 0 due to advanced reuse algorithms

        // Tree should still be correct
        if let NodeKind::Program { statements } = &tree.kind {
            assert_eq!(statements.len(), 1);
        }

        Ok(())
    }

    #[test]
    fn test_invalid_edit_bounds() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse
        let source1 = "my $x = 42;";
        parser.parse(source1)?;

        // Edit that goes beyond the node bounds (should fall back to full parse)
        parser.edit(Edit::new(
            8,
            12, // Beyond the number literal
            13,
            Position::new(8, 1, 9),
            Position::new(12, 1, 13),
            Position::new(13, 1, 14),
        ));

        let source2 = "my $x = 123;";
        let tree = parser.parse(source2)?;

        // Advanced reuse system may still achieve some reuse even with invalid bounds
        // The system is now more resilient and may not always fall back completely
        assert!(parser.reparsed_nodes > 0, "Should reparse some nodes");
        // Note: reused_nodes may be > 0 due to advanced reuse algorithms

        // Tree should still be correct
        if let NodeKind::Program { statements } = &tree.kind {
            if let NodeKind::VariableDeclaration { initializer: Some(init), .. } =
                &statements[0].kind
            {
                if let NodeKind::Number { value } = &init.kind {
                    assert_eq!(value, "123");
                }
            }
        }

        Ok(())
    }

    #[test]
    fn test_string_edit() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse
        let source1 = "my $name = \"hello\";";
        parser.parse(source1)?;

        // Change string content
        parser.edit(Edit::new(
            12,
            17, // "hello" -> "world"
            17,
            Position::new(12, 1, 13),
            Position::new(17, 1, 18),
            Position::new(17, 1, 18),
        ));

        let source2 = "my $name = \"world\";";
        let tree = parser.parse(source2)?;

        // Should reuse most of the tree
        println!(
            "DEBUG test_string_edit: reused_nodes = {}, reparsed_nodes = {}",
            parser.reused_nodes, parser.reparsed_nodes
        );
        assert_eq!(parser.reused_nodes, 3); // Program, VarDecl, Variable
        assert_eq!(parser.reparsed_nodes, 1); // Only String

        // Verify the string was updated
        if let NodeKind::Program { statements } = &tree.kind {
            if let NodeKind::VariableDeclaration { initializer: Some(init), .. } =
                &statements[0].kind
            {
                if let NodeKind::String { value, .. } = &init.kind {
                    assert_eq!(value, "\"world\"");
                }
            }
        }

        Ok(())
    }

    #[test]
    fn test_empty_source_handling() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Initial parse with valid source
        let source1 = "my $x = 42;";
        let start = Instant::now();
        parser.parse(source1)?;
        let initial_time = start.elapsed();
        println!("Initial parse time: {}µs", initial_time.as_micros());

        // Add an edit
        parser.edit(Edit::new(
            8,
            10,
            11,
            Position::new(8, 1, 9),
            Position::new(10, 1, 11),
            Position::new(11, 1, 12),
        ));

        // Try to parse empty source (should fall back to full parse)
        let source2 = "";
        let start = Instant::now();
        let result = parser.parse(source2);
        let parse_time = start.elapsed();

        println!("Empty source parse time: {}µs", parse_time.as_micros());

        // Should handle gracefully and either succeed or fail cleanly
        match result {
            Ok(_) => {
                // If it succeeds, should be a full parse
                assert_eq!(parser.reused_nodes, 0);
                println!("Empty source parsing succeeded with fallback");
            }
            Err(_) => {
                // If it fails, that's also acceptable for empty source
                assert_eq!(parser.reused_nodes, 0);
                println!("Empty source parsing failed gracefully (expected)");
            }
        }

        // Performance should still be reasonable even for empty source handling
        assert!(parse_time.as_millis() < 100, "Empty source handling should be fast");

        Ok(())
    }

    #[test]
    fn test_complex_nested_structure_edits() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Complex nested Perl structure
        let source1 = r#"
if ($condition) {
    my $nested = {
        key1 => "value1",
        key2 => 42,
        key3 => [1, 2, 3]
    };
    process($nested);
}
"#;

        let start = Instant::now();
        parser.parse(source1)?;
        let initial_time = start.elapsed();
        let initial_nodes = parser.reparsed_nodes;

        println!(
            "Complex structure initial parse: {}µs, {} nodes",
            initial_time.as_micros(),
            initial_nodes
        );

        // Edit nested value - should be challenging for incremental parser
        let value_start =
            source1.find("42").ok_or(perl_parser_core::error::ParseError::UnexpectedEof)?;
        parser.edit(Edit::new(
            value_start,
            value_start + 2,
            value_start + 4, // "42" -> "9999"
            Position::new(value_start, 1, 1),
            Position::new(value_start + 2, 1, 3),
            Position::new(value_start + 4, 1, 5),
        ));

        let source2 = source1.replace("42", "9999");
        let start = Instant::now();
        let _tree = parser.parse(&source2)?;
        let incremental_time = start.elapsed();

        println!(
            "Complex nested edit: {}µs, reused={}, reparsed={}",
            incremental_time.as_micros(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );

        // Even with complex nesting, should have reasonable performance
        assert!(incremental_time.as_millis() < 10, "Complex nested edit should be <10ms");

        // Should still achieve some node reuse
        if parser.reused_nodes > 0 {
            println!("Successfully reused {} nodes in complex structure", parser.reused_nodes);
        } else {
            println!("Complex structure caused full reparse (acceptable for edge cases)");
        }

        Ok(())
    }

    #[test]
    fn test_large_document_performance() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Generate a larger Perl document
        let mut large_source = String::new();
        for i in 0..100 {
            large_source.push_str(&format!("my $var{} = {};\n", i, i * 10));
        }

        let start = Instant::now();
        parser.parse(&large_source)?;
        let initial_time = start.elapsed();
        let initial_nodes = parser.reparsed_nodes;

        println!(
            "Large document initial parse: {}ms, {} nodes",
            initial_time.as_millis(),
            initial_nodes
        );

        // Edit in the middle of the document
        let edit_pos = large_source
            .find("my $var50 = 500")
            .ok_or(perl_parser_core::error::ParseError::UnexpectedEof)?
            + 13;
        parser.edit(Edit::new(
            edit_pos,
            edit_pos + 3, // "500" -> "999"
            edit_pos + 3,
            Position::new(edit_pos, 1, 1),
            Position::new(edit_pos + 3, 1, 4),
            Position::new(edit_pos + 3, 1, 4),
        ));

        let source2 = large_source.replace("500", "999");
        let start = Instant::now();
        let _tree = parser.parse(&source2)?;
        let incremental_time = start.elapsed();

        println!(
            "Large document incremental: {}ms, reused={}, reparsed={}",
            incremental_time.as_millis(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );

        // Large document performance targets
        assert!(incremental_time.as_millis() < 50, "Large document incremental should be <50ms");

        // Should achieve significant node reuse on large documents
        if parser.reused_nodes > 0 {
            let reuse_percentage = parser.reused_nodes as f64
                / (parser.reused_nodes + parser.reparsed_nodes) as f64
                * 100.0;
            println!("Large document reuse rate: {:.1}%", reuse_percentage);
            assert!(reuse_percentage > 50.0, "Large document should reuse >50% of nodes");
        }

        Ok(())
    }

    #[test]
    fn test_unicode_heavy_incremental_parsing() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Unicode-heavy source with emojis and international characters
        let source1 = "my $🌟variable = '你好世界'; # Comment with emoji 🚀\nmy $café = 'résumé';";

        let start = Instant::now();
        parser.parse(source1)?;
        let initial_time = start.elapsed();

        println!("Unicode document initial parse: {}µs", initial_time.as_micros());

        // Edit the unicode string content
        let edit_start =
            source1.find("你好世界").ok_or(perl_parser_core::error::ParseError::UnexpectedEof)?;
        let edit_end = edit_start + "你好世界".len();
        parser.edit(Edit::new(
            edit_start,
            edit_end,
            edit_start + "再见".len(), // "你好世界" -> "再见" (hello world -> goodbye)
            Position::new(edit_start, 1, 1),
            Position::new(edit_end, 1, 2),
            Position::new(edit_start + "再见".len(), 1, 2),
        ));

        let source2 = source1.replace("你好世界", "再见");
        let start = Instant::now();
        let _tree = parser.parse(&source2)?;
        let incremental_time = start.elapsed();

        println!(
            "Unicode incremental edit: {}µs, reused={}, reparsed={}",
            incremental_time.as_micros(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );

        // Unicode handling should not significantly impact performance.
        let unicode_budget_micros = adaptive_perf_budget_micros(5_000);
        assert!(
            incremental_time.as_micros() < unicode_budget_micros,
            "Unicode incremental parsing should be <{}µs (got {}µs)",
            unicode_budget_micros,
            incremental_time.as_micros()
        );
        assert!(parser.reused_nodes > 0 || parser.reparsed_nodes > 0, "Should parse successfully");

        Ok(())
    }

    #[test]
    fn test_edit_near_ast_node_boundaries() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Source with clear AST node boundaries
        let source1 = "sub func { my $x = 123; return $x * 2; }";

        parser.parse(source1)?;

        // Edit right at the boundary between number and semicolon
        let number_end =
            source1.find("123").ok_or(perl_parser_core::error::ParseError::UnexpectedEof)? + 3;
        parser.edit(Edit::new(
            number_end - 1, // Edit last digit of number
            number_end,
            number_end + 1, // "3" -> "456"
            Position::new(number_end - 1, 1, 1),
            Position::new(number_end, 1, 2),
            Position::new(number_end + 1, 1, 3),
        ));

        let source2 = source1.replace("123", "12456");
        let start = Instant::now();
        let _tree = parser.parse(&source2)?;
        let boundary_edit_time = start.elapsed();

        println!(
            "Boundary edit time: {}µs, reused={}, reparsed={}",
            boundary_edit_time.as_micros(),
            parser.reused_nodes,
            parser.reparsed_nodes
        );

        // Boundary edits are tricky but should still be efficient.
        let boundary_budget_micros = adaptive_perf_budget_micros(5_000);
        assert!(
            boundary_edit_time.as_micros() < boundary_budget_micros,
            "AST boundary edit should be <{}µs (got {}µs)",
            boundary_budget_micros,
            boundary_edit_time.as_micros()
        );
        assert!(parser.reparsed_nodes >= 1, "Should reparse at least the modified node");

        Ok(())
    }

    #[test]
    fn test_whitespace_insertion_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;

        parser.edit(Edit::new(
            5,
            5,
            7,
            Position::new(5, 1, 6),
            Position::new(5, 1, 6),
            Position::new(7, 1, 8),
        ));

        let source2 = "my $x  = 42;";
        parser.parse(source2)?;

        assert!(parser.reused_nodes > 0, "Whitespace insertion should reuse prior tree");
        assert!(parser.reparsed_nodes <= 1, "Whitespace insertion should avoid broad reparsing");
        Ok(())
    }

    #[test]
    fn test_performance_regression_detection() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();

        // Baseline performance measurement
        let source = "my $baseline = 42; my $test = 'hello';";
        let mut parse_times = Vec::new();

        // Multiple runs for statistical significance
        for i in 0..10 {
            let start = Instant::now();
            parser.parse(source)?;
            let time = start.elapsed();
            parse_times.push(time.as_micros());

            // Edit for next iteration
            parser.edit(Edit::new(
                15,
                17,
                19, // Edit position
                Position::new(15, 1, 16),
                Position::new(17, 1, 18),
                Position::new(19, 1, 20),
            ));

            // Alternate source for variations
            let test_source = if i % 2 == 0 {
                "my $baseline = 99; my $test = 'hello';"
            } else {
                "my $baseline = 42; my $test = 'hello';"
            };

            let start = Instant::now();
            parser.parse(test_source)?;
            let incremental_time = start.elapsed();

            println!(
                "Run {}: initial={}µs, incremental={}µs, reused={}, reparsed={}",
                i + 1,
                time.as_micros(),
                incremental_time.as_micros(),
                parser.reused_nodes,
                parser.reparsed_nodes
            );

            // Performance regression detection
            assert!(
                incremental_time.as_millis() < 10,
                "Run {} performance regression detected: {}ms",
                i + 1,
                incremental_time.as_millis()
            );
        }

        // Statistical analysis
        let avg_time = parse_times.iter().sum::<u128>() / parse_times.len() as u128;
        let max_time =
            *parse_times.iter().max().ok_or(perl_parser_core::error::ParseError::UnexpectedEof)?;
        let min_time =
            *parse_times.iter().min().ok_or(perl_parser_core::error::ParseError::UnexpectedEof)?;

        println!(
            "Performance statistics: avg={}µs, min={}µs, max={}µs",
            avg_time, min_time, max_time
        );

        let variation_factor = max_time as f64 / avg_time as f64;
        assert!(
            variation_factor <= 10.0,
            "Extreme performance inconsistency detected: max={}µs, avg={}µs ({}x variation)",
            max_time,
            avg_time,
            variation_factor
        );
        if variation_factor > 5.0 {
            println!(
                "⚠️ High performance variation detected: max={}µs, avg={}µs ({}x variation) - may indicate system load",
                max_time, avg_time, variation_factor
            );
        }

        Ok(())
    }

    /// Test whitespace before operator reuses tree
    #[test]
    fn test_whitespace_before_operator_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            6,
            6,
            9,
            Position::new(6, 1, 7),
            Position::new(6, 1, 7),
            Position::new(9, 1, 10),
        ));
        let source2 = "my $x   = 42;";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_comment_insertion_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            10,
            11,
            24,
            Position::new(10, 1, 11),
            Position::new(11, 1, 12),
            Position::new(24, 1, 25),
        ));
        let source2 = "my $x = 42; # comment";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_trailing_whitespace_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            11,
            11,
            16,
            Position::new(11, 1, 12),
            Position::new(11, 1, 12),
            Position::new(16, 1, 17),
        ));
        let source2 = "my $x = 42;     ";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_newline_insertion_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            11,
            11,
            12,
            Position::new(11, 1, 12),
            Position::new(11, 1, 12),
            Position::new(12, 2, 1),
        ));
        let source2 = "my $x = 42;\n";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_whitespace_deletion_reuses_tree() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my  $x  =  42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            3,
            5,
            4,
            Position::new(3, 1, 4),
            Position::new(5, 1, 6),
            Position::new(4, 1, 5),
        ));
        let source2 = "my $x  =  42;";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_whitespace_at_statement_boundary() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "print 'hello';my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            14,
            14,
            15,
            Position::new(14, 1, 15),
            Position::new(14, 1, 15),
            Position::new(15, 1, 16),
        ));
        let source2 = "print 'hello'; my $x = 42;";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }

    #[test]
    fn test_whitespace_edit_checks_both_old_and_new() -> ParseResult<()> {
        let mut parser = IncrementalParserV2::new();
        let source1 = "my $x = 42;";
        parser.parse(source1)?;
        parser.edit(Edit::new(
            6,
            7,
            8,
            Position::new(6, 1, 7),
            Position::new(7, 1, 8),
            Position::new(8, 1, 9),
        ));
        let source2 = "my $x=  42;";
        parser.parse(source2)?;
        assert!(parser.reused_nodes > 0);
        Ok(())
    }
}