ferroni 2.1.2

Oniguruma's regex engine in pure Rust: full Oniguruma syntax, a vscode-oniguruma-compatible TextMate scanner, no C toolchain
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
// regparse_types.rs - Port of regparse.h
// AST node types, token types, parse environment, and supporting structures.

#![allow(non_upper_case_globals)]

use std::collections::HashMap;

use crate::oniguruma::*;
use crate::regenc::OnigEncoding;
use crate::regint::*;

// === Node Type Enum ===
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum NodeType {
    String = 0,
    CClass = 1,
    CType = 2,
    BackRef = 3,
    Quant = 4,
    Bag = 5,
    Anchor = 6,
    List = 7,
    Alt = 8,
    Call = 9,
    Gimmick = 10,
}

#[inline]
pub(crate) fn nd_type2bit(t: NodeType) -> u32 {
    1 << (t as u32)
}

pub(crate) const ND_BIT_STRING: u32 = 1 << 0;
pub(crate) const ND_BIT_CCLASS: u32 = 1 << 1;
pub(crate) const ND_BIT_CTYPE: u32 = 1 << 2;
pub(crate) const ND_BIT_BACKREF: u32 = 1 << 3;
pub(crate) const ND_BIT_QUANT: u32 = 1 << 4;
pub(crate) const ND_BIT_BAG: u32 = 1 << 5;
pub(crate) const ND_BIT_ANCHOR: u32 = 1 << 6;
pub(crate) const ND_BIT_LIST: u32 = 1 << 7;
pub(crate) const ND_BIT_ALT: u32 = 1 << 8;
pub(crate) const ND_BIT_CALL: u32 = 1 << 9;
pub(crate) const ND_BIT_GIMMICK: u32 = 1 << 10;

// === Bag Type ===
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub(crate) enum BagType {
    Memory = 0,
    Option = 1,
    StopBacktrack = 2,
    IfElse = 3,
}

// === Gimmick Type ===
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub(crate) enum GimmickType {
    Fail = 0,
    Save = 1,
    UpdateVar = 2,
    Callout = 3,
}

// === Body Empty Type ===
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub(crate) enum BodyEmptyType {
    NotEmpty = 0,
    MayBeEmpty = 1,
    MayBeEmptyMem = 2,
    MayBeEmptyRec = 3,
}

// === Size Constants ===
pub(crate) const ND_STRING_MARGIN: usize = 16;
pub(crate) const ND_STRING_BUF_SIZE: usize = 24;
pub(crate) const ND_BACKREFS_SIZE: usize = 6;
pub(crate) const PARSEENV_MEMENV_SIZE: usize = 8;
pub(crate) const CTYPE_ANYCHAR: i32 = -1;

// === Node Status Flags ===
pub(crate) const ND_ST_FIXED_MIN: u32 = 1 << 0;
pub(crate) const ND_ST_FIXED_MAX: u32 = 1 << 1;
pub(crate) const ND_ST_FIXED_CLEN: u32 = 1 << 2;
pub(crate) const ND_ST_MARK1: u32 = 1 << 3;
pub(crate) const ND_ST_MARK2: u32 = 1 << 4;
pub(crate) const ND_ST_STRICT_REAL_REPEAT: u32 = 1 << 5;
pub(crate) const ND_ST_RECURSION: u32 = 1 << 6;
pub(crate) const ND_ST_CALLED: u32 = 1 << 7;
pub(crate) const ND_ST_FIXED_ADDR: u32 = 1 << 8;
pub(crate) const ND_ST_NAMED_GROUP: u32 = 1 << 9;
pub(crate) const ND_ST_IN_REAL_REPEAT: u32 = 1 << 10;
pub(crate) const ND_ST_IN_ZERO_REPEAT: u32 = 1 << 11;
pub(crate) const ND_ST_IN_MULTI_ENTRY: u32 = 1 << 12;
pub(crate) const ND_ST_NEST_LEVEL: u32 = 1 << 13;
pub(crate) const ND_ST_BY_NUMBER: u32 = 1 << 14;
pub(crate) const ND_ST_BY_NAME: u32 = 1 << 15;
pub(crate) const ND_ST_BACKREF: u32 = 1 << 16;
pub(crate) const ND_ST_CHECKER: u32 = 1 << 17;
pub(crate) const ND_ST_PROHIBIT_RECURSION: u32 = 1 << 18;
pub(crate) const ND_ST_SUPER: u32 = 1 << 19;
pub(crate) const ND_ST_EMPTY_STATUS_CHECK: u32 = 1 << 20;
pub(crate) const ND_ST_IGNORECASE: u32 = 1 << 21;
pub(crate) const ND_ST_MULTILINE: u32 = 1 << 22;
pub(crate) const ND_ST_TEXT_SEGMENT_WORD: u32 = 1 << 23;
pub(crate) const ND_ST_ABSENT_WITH_SIDE_EFFECTS: u32 = 1 << 24;
pub(crate) const ND_ST_FIXED_CLEN_MIN_SURE: u32 = 1 << 25;
pub(crate) const ND_ST_REFERENCED: u32 = 1 << 26;
pub(crate) const ND_ST_INPEEK: u32 = 1 << 27;
pub(crate) const ND_ST_WHOLE_OPTIONS: u32 = 1 << 28;
pub(crate) const ND_ST_LITERAL_ALT: u32 = 1 << 29;
/// Rust-only: a primitive ASCII class repeat proven possessive by the opt-in pass.
pub(crate) const ND_ST_POSSESSIVE_CLASS_REPEAT: u32 = 1 << 30;
/// Rust-only: a decimal tail whose prefix can be evaluated deterministically.
pub(crate) const ND_ST_DECIMAL_TAIL_PREFIX: u32 = 1 << 31;

// === String Node Flags ===
pub(crate) const ND_STRING_CRUDE: u32 = 1 << 0;
pub(crate) const ND_STRING_CASE_EXPANDED: u32 = 1 << 1;

// === Memory Called States ===
pub(crate) const CALL_DELTA_UNREFERENCED: i32 = 0;
pub(crate) const CALL_DELTA_REFERENCED: i32 = 1;
pub(crate) const CALL_DELTA_CALLED: i32 = 2;

// === Parse Environment Flags ===
pub(crate) const PE_FLAG_HAS_CALL_ZERO: u32 = 1 << 0;
pub(crate) const PE_FLAG_HAS_WHOLE_OPTIONS: u32 = 1 << 1;
pub(crate) const PE_FLAG_HAS_ABSENT_STOPPER: u32 = 1 << 2;

// === BBuf (Byte Buffer) ===
// In C: struct { UChar* p; unsigned int used; unsigned int alloc; }
// In Rust: Vec<u8> handles allocation automatically.

#[derive(Clone, Debug)]
pub(crate) struct BBuf {
    pub data: Vec<u8>,
}

impl BBuf {
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn new() -> Self {
        BBuf { data: Vec::new() }
    }

    pub(crate) fn with_capacity(cap: usize) -> Self {
        BBuf {
            data: Vec::with_capacity(cap),
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn used(&self) -> usize {
        self.data.len()
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn write(&mut self, bytes: &[u8]) {
        self.data.extend_from_slice(bytes);
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn write_u32(&mut self, val: u32) {
        self.data.extend_from_slice(&val.to_ne_bytes());
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn clone_from(other: &BBuf) -> Self {
        BBuf {
            data: other.data.clone(),
        }
    }
}

impl Default for BBuf {
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn default() -> Self {
        Self::new()
    }
}

// === AST Node ===
// C uses a union with common base fields (node_type, status, parent, body).
// Rust: outer struct for common fields + inner enum for variant data.

pub(crate) struct Node {
    pub status: u32,
    pub parent: *mut Node,
    pub inner: NodeInner,
}

pub(crate) enum NodeInner {
    String(StrNode),
    CClass(CClassNode),
    CType(CtypeNode),
    BackRef(BackRefNode),
    Quant(QuantNode),
    Bag(BagNode),
    Anchor(AnchorNode),
    List(ConsAltNode),
    Alt(ConsAltNode),
    Call(CallNode),
    Gimmick(GimmickNode),
}

// === Node Helper Methods ===

impl Node {
    pub(crate) fn node_type(&self) -> NodeType {
        match &self.inner {
            NodeInner::String(_) => NodeType::String,
            NodeInner::CClass(_) => NodeType::CClass,
            NodeInner::CType(_) => NodeType::CType,
            NodeInner::BackRef(_) => NodeType::BackRef,
            NodeInner::Quant(_) => NodeType::Quant,
            NodeInner::Bag(_) => NodeType::Bag,
            NodeInner::Anchor(_) => NodeType::Anchor,
            NodeInner::List(_) => NodeType::List,
            NodeInner::Alt(_) => NodeType::Alt,
            NodeInner::Call(_) => NodeType::Call,
            NodeInner::Gimmick(_) => NodeType::Gimmick,
        }
    }

    pub(crate) fn node_type_bit(&self) -> u32 {
        nd_type2bit(self.node_type())
    }

    // Status helpers (matching C macros ND_STATUS_ADD, ND_STATUS_REMOVE)
    pub(crate) fn status_add(&mut self, flag: u32) {
        self.status |= flag;
    }

    pub(crate) fn status_remove(&mut self, flag: u32) {
        self.status &= !flag;
    }

    pub(crate) fn has_status(&self, flag: u32) -> bool {
        (self.status & flag) != 0
    }

    // Body access (only Quant, Bag, Anchor, Call have body)
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn body(&self) -> Option<&Node> {
        match &self.inner {
            NodeInner::Quant(n) => n.body.as_ref().map(|b| b.as_ref()),
            NodeInner::Bag(n) => n.body.as_ref().map(|b| b.as_ref()),
            NodeInner::Anchor(n) => n.body.as_ref().map(|b| b.as_ref()),
            NodeInner::Call(n) => n.body.as_ref().map(|b| b.as_ref()),
            _ => None,
        }
    }

    pub(crate) fn is_anychar(&self) -> bool {
        if let NodeInner::CType(ct) = &self.inner {
            ct.ctype == CTYPE_ANYCHAR
        } else {
            false
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn body_mut(&mut self) -> Option<&mut Node> {
        match &mut self.inner {
            NodeInner::Quant(n) => n.body.as_mut().map(|b| b.as_mut()),
            NodeInner::Bag(n) => n.body.as_mut().map(|b| b.as_mut()),
            NodeInner::Anchor(n) => n.body.as_mut().map(|b| b.as_mut()),
            NodeInner::Call(n) => n.body.as_mut().map(|b| b.as_mut()),
            _ => None,
        }
    }

    pub(crate) fn set_body(&mut self, body: Option<Box<Node>>) {
        match &mut self.inner {
            NodeInner::Quant(n) => n.body = body,
            NodeInner::Bag(n) => n.body = body,
            NodeInner::Anchor(n) => n.body = body,
            NodeInner::Call(n) => n.body = body,
            _ => {}
        }
    }

    pub(crate) fn take_body(&mut self) -> Option<Box<Node>> {
        match &mut self.inner {
            NodeInner::Quant(n) => n.body.take(),
            NodeInner::Bag(n) => n.body.take(),
            NodeInner::Anchor(n) => n.body.take(),
            NodeInner::Call(n) => n.body.take(),
            _ => None,
        }
    }

    // Variant accessors (matching C macros STR_, CCLASS_, etc.)
    pub(crate) fn as_str(&self) -> Option<&StrNode> {
        match &self.inner {
            NodeInner::String(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_str_mut(&mut self) -> Option<&mut StrNode> {
        match &mut self.inner {
            NodeInner::String(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_cclass(&self) -> Option<&CClassNode> {
        match &self.inner {
            NodeInner::CClass(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_cclass_mut(&mut self) -> Option<&mut CClassNode> {
        match &mut self.inner {
            NodeInner::CClass(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_ctype(&self) -> Option<&CtypeNode> {
        match &self.inner {
            NodeInner::CType(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_quant(&self) -> Option<&QuantNode> {
        match &self.inner {
            NodeInner::Quant(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_quant_mut(&mut self) -> Option<&mut QuantNode> {
        match &mut self.inner {
            NodeInner::Quant(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_bag(&self) -> Option<&BagNode> {
        match &self.inner {
            NodeInner::Bag(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_bag_mut(&mut self) -> Option<&mut BagNode> {
        match &mut self.inner {
            NodeInner::Bag(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_anchor(&self) -> Option<&AnchorNode> {
        match &self.inner {
            NodeInner::Anchor(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_anchor_mut(&mut self) -> Option<&mut AnchorNode> {
        match &mut self.inner {
            NodeInner::Anchor(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_backref(&self) -> Option<&BackRefNode> {
        match &self.inner {
            NodeInner::BackRef(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_backref_mut(&mut self) -> Option<&mut BackRefNode> {
        match &mut self.inner {
            NodeInner::BackRef(n) => Some(n),
            _ => None,
        }
    }

    pub(crate) fn as_cons(&self) -> Option<&ConsAltNode> {
        match &self.inner {
            NodeInner::List(n) | NodeInner::Alt(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_cons_mut(&mut self) -> Option<&mut ConsAltNode> {
        match &mut self.inner {
            NodeInner::List(n) | NodeInner::Alt(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_call(&self) -> Option<&CallNode> {
        match &self.inner {
            NodeInner::Call(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_call_mut(&mut self) -> Option<&mut CallNode> {
        match &mut self.inner {
            NodeInner::Call(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_gimmick(&self) -> Option<&GimmickNode> {
        match &self.inner {
            NodeInner::Gimmick(n) => Some(n),
            _ => None,
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_gimmick_mut(&mut self) -> Option<&mut GimmickNode> {
        match &mut self.inner {
            NodeInner::Gimmick(n) => Some(n),
            _ => None,
        }
    }

    // ConsAltNode shortcuts (matching C macros ND_CAR, ND_CDR)
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn car(&self) -> Option<&Node> {
        self.as_cons().map(|c| c.car.as_ref())
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn cdr(&self) -> Option<&Node> {
        self.as_cons()
            .and_then(|c| c.cdr.as_ref().map(|b| b.as_ref()))
    }
}

// Raw pointers require manual Send/Sync impls.
//
// SAFETY: the raw pointers in a Node (`parent`, and `CallNode::target_node`)
// are non-owning links to other nodes inside the same AST, which is owned as
// a single `Box<Node>` tree. Sending the tree to another thread transfers all
// of the pointees along with it, so the internal pointers stay valid and no
// two threads ever own the same node. This would break if a Node were sent
// while its `parent`/`target_node` pointed into a tree left behind on the
// original thread — the parser never does that: trees are built, compiled,
// and dropped whole.
unsafe impl Send for Node {}
// SAFETY: no safe method dereferences the raw pointers, so `&Node` shared
// between threads permits no data race through them; any dereference happens
// in crate-internal unsafe code during parse/compile, which is single-threaded
// (the tree is confined to the one thread running onig_new-style compilation).
// Note this impl is trusting rather than airtight: `parent`/`target_node` can
// alias other nodes in the same tree, so unsafe code that mutated through them
// while another thread held any `&Node` into the tree would be a data race.
// Kept to mirror the C code, where node trees are likewise thread-confined.
unsafe impl Sync for Node {}

// === Node Variant Structs ===

pub(crate) struct StrNode {
    pub s: Vec<u8>,
    pub flag: u32,
}

impl StrNode {
    pub(crate) fn is_crude(&self) -> bool {
        (self.flag & ND_STRING_CRUDE) != 0
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn is_case_expanded(&self) -> bool {
        (self.flag & ND_STRING_CASE_EXPANDED) != 0
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn set_crude(&mut self) {
        self.flag |= ND_STRING_CRUDE;
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn clear_crude(&mut self) {
        self.flag &= !ND_STRING_CRUDE;
    }
}

pub(crate) struct CClassNode {
    pub flags: u32,
    pub bs: BitSet,
    pub mbuf: Option<BBuf>,
}

impl CClassNode {
    pub(crate) fn is_not(&self) -> bool {
        (self.flags & FLAG_NCCLASS_NOT) != 0
    }

    pub(crate) fn set_not(&mut self) {
        self.flags |= FLAG_NCCLASS_NOT;
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn clear_not(&mut self) {
        self.flags &= !FLAG_NCCLASS_NOT;
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn is_share(&self) -> bool {
        (self.flags & FLAG_NCCLASS_SHARE) != 0
    }
}

pub(crate) struct CtypeNode {
    pub ctype: i32,
    pub not: bool,
    pub ascii_mode: bool,
}

pub(crate) struct BackRefNode {
    pub back_num: i32,
    pub back_static: [i32; ND_BACKREFS_SIZE],
    pub back_dynamic: Option<Vec<i32>>,
    pub nest_level: i32,
}

impl BackRefNode {
    /// Get the backreference number array
    pub(crate) fn back_refs(&self) -> &[i32] {
        if let Some(ref dyn_refs) = self.back_dynamic {
            dyn_refs
        } else {
            &self.back_static[..self.back_num as usize]
        }
    }
}

pub(crate) struct QuantNode {
    pub body: Option<Box<Node>>,
    pub lower: i32,
    pub upper: i32,
    pub greedy: bool,
    pub emptiness: BodyEmptyType,
    pub head_exact: Option<u8>,
    pub next_head_exact: Option<u8>,
    pub include_referred: i32,
    pub empty_status_mem: MemStatusType,
}

pub(crate) struct BagNode {
    pub body: Option<Box<Node>>,
    pub bag_type: BagType,
    pub bag_data: BagData,
    pub min_len: OnigLen,
    pub max_len: OnigLen,
    pub min_char_len: OnigLen,
    pub max_char_len: OnigLen,
    pub opt_count: i32,
}

pub(crate) enum BagData {
    Memory {
        regnum: i32,
        called_addr: AbsAddrType,
        entry_count: i32,
        called_state: i32,
    },
    Option {
        options: OnigOptionType,
    },
    StopBacktrack,
    IfElse {
        then_node: Option<Box<Node>>,
        else_node: Option<Box<Node>>,
    },
}

impl BagNode {
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn as_memory(&self) -> Option<(i32, AbsAddrType, i32, i32)> {
        match &self.bag_data {
            BagData::Memory {
                regnum,
                called_addr,
                entry_count,
                called_state,
            } => Some((*regnum, *called_addr, *entry_count, *called_state)),
            _ => None,
        }
    }

    pub(crate) fn regnum(&self) -> i32 {
        match &self.bag_data {
            BagData::Memory { regnum, .. } => *regnum,
            _ => 0,
        }
    }
}

pub(crate) struct AnchorNode {
    pub body: Option<Box<Node>>,
    pub anchor_type: i32,
    pub char_min_len: OnigLen,
    pub char_max_len: OnigLen,
    pub ascii_mode: bool,
    pub lead_node: Option<Box<Node>>,
}

pub(crate) struct ConsAltNode {
    pub car: Box<Node>,
    pub cdr: Option<Box<Node>>,
}

pub(crate) struct CallNode {
    pub body: Option<Box<Node>>,
    pub by_number: bool,
    pub called_gnum: i32,
    pub name: Vec<u8>,
    pub entry_count: i32,
    /// Raw pointer to the target BAG_MEMORY node (non-owning, for recursion detection)
    pub target_node: *mut Node,
}

pub(crate) struct GimmickNode {
    pub gimmick_type: GimmickType,
    pub detail_type: i32,
    pub num: i32,
    pub id: i32,
}

// === Token Types ===

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i32)]
pub(crate) enum TokenType {
    Eot = 0,
    CrudeByte = 1,
    Char = 2,
    String = 3,
    CodePoint = 4,
    AnyChar = 5,
    CharType = 6,
    Backref = 7,
    Call = 8,
    Anchor = 9,
    Repeat = 10,
    Interval = 11,
    AnyCharAnytime = 12,
    Alt = 13,
    SubexpOpen = 14,
    SubexpClose = 15,
    OpenCC = 16,
    QuoteOpen = 17,
    CharProperty = 18,
    Keep = 19,
    GeneralNewline = 20,
    NoNewline = 21,
    TrueAnychar = 22,
    TextSegment = 23,
    // In character class context
    CcClose = 24,
    CcRange = 25,
    CcPosixBracketOpen = 26,
    CcAnd = 27,
    CcOpenCC = 28,
}

// === PToken (Parser Token) ===
// Flat struct matching C union approach: fields are only valid when token_type matches.
// This avoids needing to destructure an enum on every access (the C freely writes to
// different union members as it determines the token type).

pub(crate) struct PToken {
    pub token_type: TokenType,
    pub escaped: bool,
    pub code_point_continue: bool,
    pub base_num: i32,
    pub backp: usize,
    // Union field: code / byte (valid for TK_CHAR, TK_CODE_POINT, TK_CRUDE_BYTE)
    pub code: OnigCodePoint,
    // Union field: anchor / subtype (valid for TK_ANCHOR)
    pub anchor: i32,
    // Union field: repeat (valid for TK_REPEAT, TK_INTERVAL)
    pub repeat_lower: i32,
    pub repeat_upper: i32,
    pub repeat_greedy: bool,
    pub repeat_possessive: bool,
    // Union field: backref (valid for TK_BACKREF)
    pub backref_num: i32,
    pub backref_ref1: i32,
    pub backref_refs: Vec<i32>,
    pub backref_by_name: bool,
    pub backref_exist_level: bool,
    pub backref_level: i32,
    // Union field: call (valid for TK_CALL)
    pub call_name_start: usize,
    pub call_name_end: usize,
    pub call_gnum: i32,
    pub call_by_number: bool,
    // Union field: prop (valid for TK_CHAR_TYPE, TK_CHAR_PROPERTY)
    pub prop_ctype: i32,
    pub prop_not: bool,
    pub prop_braces: bool,
}

impl PToken {
    pub(crate) fn new() -> Self {
        PToken {
            token_type: TokenType::Eot,
            escaped: false,
            code_point_continue: false,
            base_num: 0,
            backp: 0,
            code: 0,
            anchor: 0,
            repeat_lower: 0,
            repeat_upper: 0,
            repeat_greedy: false,
            repeat_possessive: false,
            backref_num: 0,
            backref_ref1: 0,
            backref_refs: Vec::new(),
            backref_by_name: false,
            backref_exist_level: false,
            backref_level: 0,
            call_name_start: 0,
            call_name_end: 0,
            call_gnum: 0,
            call_by_number: false,
            prop_ctype: 0,
            prop_not: false,
            prop_braces: false,
        }
    }

    pub(crate) fn init(&mut self) {
        self.code_point_continue = false;
    }
}

impl Default for PToken {
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn default() -> Self {
        Self::new()
    }
}

// === Group Number Map ===

pub(crate) struct GroupNumMap {
    pub new_val: i32,
}

// === Memory Environment ===

pub(crate) struct MemEnv {
    pub mem_node: *mut Node,
    pub empty_repeat_node: *mut Node,
}

impl Default for MemEnv {
    fn default() -> Self {
        MemEnv {
            mem_node: std::ptr::null_mut(),
            empty_repeat_node: std::ptr::null_mut(),
        }
    }
}

// SAFETY: `mem_node` and `empty_repeat_node` are non-owning pointers to
// BAG_MEMORY / quantifier nodes inside the parse tree of the regex currently
// being compiled. A MemEnv lives inside a ParseEnv, which exists only for the
// duration of a single-threaded parse/compile pass and is never handed to
// another thread while those pointers are live (they dangle once the tree is
// dropped). Sending a MemEnv with live pointers to a thread that outlives the
// tree would break this invariant.
unsafe impl Send for MemEnv {}
// SAFETY: nothing dereferences these pointers through `&MemEnv` outside the
// single compilation thread; sharing across threads never happens in practice
// because the owning ParseEnv is parser-scoped. Concurrent mutation of the
// pointees from another thread would be a data race and would break this.
unsafe impl Sync for MemEnv {}

// === Save Item ===

pub(crate) struct SaveItem {
    pub save_type: SaveType,
}

// === Unset Address (for USE_CALL) ===

pub(crate) struct UnsetAddr {
    pub offset: i32,
    pub target: *mut Node,
}

// SAFETY: `target` is a non-owning pointer to a BAG_MEMORY node in the parse
// tree, recorded so the compiler can patch call addresses later in the same
// compilation pass. The list of UnsetAddr entries lives in the parser-scoped
// ParseEnv and is consumed on the same thread before the tree is dropped;
// sending an entry to a thread that outlives the tree would break this.
unsafe impl Send for UnsetAddr {}
// SAFETY: `target` is never dereferenced through `&UnsetAddr` outside the
// single compilation thread, and UnsetAddr values are not shared across
// threads in practice (parser-scoped, single-threaded use during
// compilation). Concurrent mutation of the pointee would be a data race.
unsafe impl Sync for UnsetAddr {}

// === Parse Environment (ScanEnv in C) ===

/// The parts of the `RegexType` under construction that the parser writes.
/// C reaches them through `env->reg`. The port moves them into the
/// `ParseEnv` for the duration of `onig_parse_tree` and moves them back on
/// every return path, so the parser holds no pointer to the regex.
pub(crate) struct RegexParseState {
    /// The regex's options, including the whole-option flags that
    /// `set_whole_options` adds during the parse.
    pub options: OnigOptionType,
    pub case_fold_flag: OnigCaseFoldType,
    pub name_table: Option<NameTable>,
    pub extp: Option<RegexExt>,
}

impl Default for RegexParseState {
    fn default() -> Self {
        Self {
            options: OnigOptionType::NONE,
            case_fold_flag: 0,
            name_table: None,
            extp: None,
        }
    }
}

pub(crate) struct ParseEnv {
    pub options: OnigOptionType,
    pub case_fold_flag: OnigCaseFoldType,
    pub enc: OnigEncoding,
    pub syntax: OnigSyntaxType,
    pub cap_history: MemStatusType,
    pub backtrack_mem: MemStatusType,
    pub backrefed_mem: MemStatusType,
    /// C's `env->pattern`. Recorded for parity and never dereferenced.
    pub pattern: *const u8,
    /// C's `env->pattern_end`. Recorded for parity and never dereferenced.
    pub pattern_end: *const u8,
    /// The name an error refers to, recorded by `set_error_string` and
    /// reported through `OnigErrorInfo`. C keeps it as the pointer pair
    /// `error`/`error_end` into the pattern (or into a node's name); an owned
    /// copy of the bytes serves both without borrowing from either.
    pub error: Option<Vec<u8>>,
    /// C's `env->reg`. Recorded for parity and never dereferenced; the regex
    /// state the parser writes is in `reg_state`.
    pub reg: *mut RegexType,
    /// The regex state moved in from `reg` for the duration of the parse.
    pub reg_state: RegexParseState,
    pub num_call: i32,
    pub num_mem: i32,
    pub num_named: i32,
    pub mem_alloc: i32,
    pub mem_env_static: [MemEnv; PARSEENV_MEMENV_SIZE],
    pub mem_env_dynamic: Option<Vec<MemEnv>>,
    pub backref_num: i32,
    pub keep_num: i32,
    pub id_num: i32,
    pub save_alloc_num: i32,
    pub saves: Option<Vec<SaveItem>>,
    pub unset_addr_list: Option<Vec<UnsetAddr>>,
    pub parse_depth: u32,
    /// Number of AST-producing expressions accepted in this parse.
    pub ast_node_count: u32,
    pub flags: u32,
    /// Recursive groups by group number, set after the call-graph analysis.
    /// C reads ND_IS_RECURSION on a call's target node; the port keeps it
    /// here so passes need not follow the call's raw target pointer.
    pub recursive_mem: Vec<bool>,
    /// Minimum byte length of each group's body, by group number, filled
    /// before tune_tree and again after it when the pattern has calls or
    /// back-references. C caches the same value in BAG_(node)->min_len
    /// (ND_ST_FIXED_MIN) and reads it for a call or a back-reference.
    pub group_min_len: Vec<OnigLen>,
    /// Maximum byte length of each group's body, by group number, filled
    /// after tune_tree when the pattern has back-references. C caches the
    /// same value in BAG_(node)->max_len (ND_ST_FIXED_MAX) and reads it for
    /// a back-reference.
    pub group_max_len: Vec<OnigLen>,
}

// ParseEnv is neither Send nor Sync: it holds the raw pointers `pattern`,
// `pattern_end` and `reg`, which are recorded for C parity and never
// dereferenced. Nothing requires it to be Send or Sync.

// === Node Creation Helper Functions ===

pub(crate) fn node_new(inner: NodeInner) -> Box<Node> {
    Box::new(Node {
        status: 0,
        parent: std::ptr::null_mut(),
        inner,
    })
}

pub(crate) fn node_new_str(s: &[u8]) -> Box<Node> {
    node_new(NodeInner::String(StrNode {
        s: s.to_vec(),
        flag: 0,
    }))
}

/// Port of node_new_str_with_options from regparse.c.
pub(crate) fn node_new_str_with_options(s: &[u8], options: OnigOptionType) -> Box<Node> {
    let mut node = node_new_str(s);
    if opton_ignorecase(options) {
        node.status_add(ND_ST_IGNORECASE);
    }
    node
}

/// Port of node_new_str_crude from regparse.c.
pub(crate) fn node_new_str_crude(s: &[u8], options: OnigOptionType) -> Box<Node> {
    let mut node = node_new_str_with_options(s, options);
    if let Some(sn) = node.as_str_mut() {
        sn.set_crude();
    }
    node
}

/// Port of node_new_str_crude_char from regparse.c.
pub(crate) fn node_new_str_crude_char(c: u8, options: OnigOptionType) -> Box<Node> {
    node_new_str_crude(&[c], options)
}

pub(crate) fn node_new_empty() -> Box<Node> {
    node_new(NodeInner::String(StrNode {
        s: Vec::new(),
        flag: 0,
    }))
}

pub(crate) fn node_new_cclass() -> Box<Node> {
    node_new(NodeInner::CClass(CClassNode {
        flags: 0,
        bs: [0; BITSET_REAL_SIZE],
        mbuf: None,
    }))
}

pub(crate) fn node_new_ctype(ctype: i32, not: bool, ascii_mode: bool) -> Box<Node> {
    node_new(NodeInner::CType(CtypeNode {
        ctype,
        not,
        ascii_mode,
    }))
}

pub(crate) fn node_new_anychar() -> Box<Node> {
    node_new_ctype(CTYPE_ANYCHAR, false, false)
}

/// Port of C's `node_new_backref`, without the steps that need the parse
/// environment (see `node_new_backref_in_env`). As in C, the NEST_LEVEL
/// status follows whether a level was written (`\k<n+0>` included), not the
/// level's value.
pub(crate) fn node_new_backref(
    back_num: i32,
    backrefs: &[i32],
    by_name: bool,
    exist_level: bool,
    nest_level: i32,
) -> Box<Node> {
    let mut back_static = [0i32; ND_BACKREFS_SIZE];
    let back_dynamic = if backrefs.len() <= ND_BACKREFS_SIZE {
        for (i, &r) in backrefs.iter().enumerate() {
            back_static[i] = r;
        }
        None
    } else {
        Some(backrefs.to_vec())
    };

    let mut node = node_new(NodeInner::BackRef(BackRefNode {
        back_num,
        back_static,
        back_dynamic,
        nest_level: if exist_level { nest_level } else { 0 },
    }));
    if by_name {
        node.status_add(ND_ST_BY_NAME);
    }
    if exist_level {
        node.status_add(ND_ST_NEST_LEVEL);
    }
    node
}

/// C's `node_new_backref` in full: `node_new_backref` plus the steps that
/// read the parse environment. A reference to a group that is still open
/// (`/...(\1).../`) gets the RECURSION status. Kept apart so that
/// `node_new_backref` keeps its public signature.
pub(crate) fn node_new_backref_in_env(
    back_num: i32,
    backrefs: &[i32],
    by_name: bool,
    exist_level: bool,
    nest_level: i32,
    env: &mut ParseEnv,
) -> Box<Node> {
    let mut node = node_new_backref(back_num, backrefs, by_name, exist_level, nest_level);

    if opton_ignorecase(env.options) {
        node.status_add(ND_ST_IGNORECASE);
    }

    if backrefs
        .iter()
        .any(|&b| (0..=env.num_mem).contains(&b) && env.mem_env(b as usize).mem_node.is_null())
    {
        node.status_add(ND_ST_RECURSION); /* /...(\1).../ */
    }

    env.backref_num += 1;
    node
}

pub(crate) fn node_new_quantifier(lower: i32, upper: i32, greedy: bool) -> Box<Node> {
    node_new(NodeInner::Quant(QuantNode {
        body: None,
        lower,
        upper,
        greedy,
        emptiness: BodyEmptyType::NotEmpty,
        head_exact: None,
        next_head_exact: None,
        include_referred: 0,
        empty_status_mem: 0,
    }))
}

pub(crate) fn node_new_bag(bag_type: BagType) -> Box<Node> {
    let bag_data = match bag_type {
        BagType::Memory => BagData::Memory {
            regnum: 0,
            called_addr: -1,
            entry_count: 0,
            called_state: 0,
        },
        BagType::Option => BagData::Option {
            options: ONIG_OPTION_NONE,
        },
        BagType::StopBacktrack => BagData::StopBacktrack,
        BagType::IfElse => BagData::IfElse {
            then_node: None,
            else_node: None,
        },
    };

    node_new(NodeInner::Bag(BagNode {
        body: None,
        bag_type,
        bag_data,
        min_len: 0,
        max_len: 0,
        min_char_len: 0,
        max_char_len: 0,
        opt_count: 0,
    }))
}

pub(crate) fn node_new_bag_memory(regnum: i32) -> Box<Node> {
    let mut node = node_new_bag(BagType::Memory);
    if let NodeInner::Bag(ref mut bn) = node.inner {
        bn.bag_data = BagData::Memory {
            regnum,
            called_addr: -1,
            entry_count: 0,
            called_state: 0,
        };
    }
    node
}

pub(crate) fn node_new_bag_if_else(
    cond: Box<Node>,
    then_node: Option<Box<Node>>,
    else_node: Option<Box<Node>>,
) -> Box<Node> {
    let mut node = node_new(NodeInner::Bag(BagNode {
        body: Some(cond),
        bag_type: BagType::IfElse,
        bag_data: BagData::IfElse {
            then_node,
            else_node,
        },
        min_len: 0,
        max_len: 0,
        min_char_len: 0,
        max_char_len: 0,
        opt_count: 0,
    }));
    node.status = 0;
    node
}

pub(crate) fn node_new_option(options: OnigOptionType) -> Box<Node> {
    let mut node = node_new_bag(BagType::Option);
    if let NodeInner::Bag(ref mut bn) = node.inner {
        bn.bag_data = BagData::Option { options };
    }
    node
}

pub(crate) fn node_new_anchor(anchor_type: i32) -> Box<Node> {
    node_new(NodeInner::Anchor(AnchorNode {
        body: None,
        anchor_type,
        char_min_len: 0,
        char_max_len: 0,
        ascii_mode: false,
        lead_node: None,
    }))
}

pub(crate) fn node_new_anchor_with_options(anchor_type: i32, options: OnigOptionType) -> Box<Node> {
    let mut node = node_new_anchor(anchor_type);
    if onig_is_option_on(options, ONIG_OPTION_IGNORECASE) {
        node.status_add(ND_ST_IGNORECASE);
    }
    if onig_is_option_on(options, ONIG_OPTION_MULTILINE) {
        node.status_add(ND_ST_MULTILINE);
    }
    if onig_is_option_on(options, ONIG_OPTION_TEXT_SEGMENT_WORD) {
        node.status_add(ND_ST_TEXT_SEGMENT_WORD);
    }
    node
}

pub(crate) fn node_new_list(car: Box<Node>, cdr: Option<Box<Node>>) -> Box<Node> {
    node_new(NodeInner::List(ConsAltNode { car, cdr }))
}

pub(crate) fn node_new_alt(car: Box<Node>, cdr: Option<Box<Node>>) -> Box<Node> {
    node_new(NodeInner::Alt(ConsAltNode { car, cdr }))
}

pub(crate) fn node_new_call(name: &[u8], gnum: i32, by_number: bool) -> Box<Node> {
    let mut node = node_new(NodeInner::Call(CallNode {
        body: None,
        by_number,
        called_gnum: gnum,
        name: name.to_vec(),
        entry_count: 0,
        target_node: std::ptr::null_mut(),
    }));
    if by_number {
        node.status_add(ND_ST_BY_NUMBER);
    }
    node
}

pub(crate) fn node_new_fail() -> Box<Node> {
    node_new(NodeInner::Gimmick(GimmickNode {
        gimmick_type: GimmickType::Fail,
        detail_type: 0,
        num: 0,
        id: 0,
    }))
}

/// Turn `node` into an empty string node in place (C: onig_node_reset_empty).
/// Like C, the status bits and the parent link stay.
pub(crate) fn onig_node_reset_empty(node: &mut Node) {
    node.inner = node_new_empty().inner;
}

/// Turn `node` into a FAIL gimmick in place (C: onig_node_reset_fail).
/// Like C, the status bits and the parent link stay.
pub(crate) fn onig_node_reset_fail(node: &mut Node) {
    node.inner = node_new_fail().inner;
}

pub(crate) fn node_new_callout(of: i32, num: i32, id: i32) -> Box<Node> {
    node_new(NodeInner::Gimmick(GimmickNode {
        gimmick_type: GimmickType::Callout,
        detail_type: of, // 0=CONTENTS, 1=NAME
        num,             // callout list index
        id,              // builtin id for name callouts, -1 for contents
    }))
}

pub(crate) fn node_new_save_gimmick(save_type: SaveType, id: i32) -> Box<Node> {
    node_new(NodeInner::Gimmick(GimmickNode {
        gimmick_type: GimmickType::Save,
        detail_type: save_type as i32,
        num: 0,
        id,
    }))
}

pub(crate) fn node_new_update_var_gimmick(var_type: UpdateVarType, id: i32) -> Box<Node> {
    node_new(NodeInner::Gimmick(GimmickNode {
        gimmick_type: GimmickType::UpdateVar,
        detail_type: var_type as i32,
        num: 0,
        id,
    }))
}

// === Utility: make_list / make_alt ===

/// Create a 2-node list: (a . (b . nil))
pub(crate) fn make_list(a: Box<Node>, b: Box<Node>) -> Box<Node> {
    let tail = node_new_list(b, None);
    node_new_list(a, Some(tail))
}

/// Create a 2-node alternation: (a | b)
pub(crate) fn make_alt(a: Box<Node>, b: Box<Node>) -> Box<Node> {
    let tail = node_new_alt(b, None);
    node_new_alt(a, Some(tail))
}

/// Create a right-linked List chain: `List(ns[0], List(ns[1], ... List(ns[n-1], nil)))`
// The elements are boxed nodes already; the vector is the call site's shape.
#[allow(clippy::vec_box)]
pub(crate) fn make_list_n(mut nodes: Vec<Box<Node>>) -> Box<Node> {
    assert!(!nodes.is_empty());
    let mut result = node_new_list(nodes.pop().unwrap(), None);
    while let Some(n) = nodes.pop() {
        result = node_new_list(n, Some(result));
    }
    result
}

/// Create a right-linked Alt chain: `Alt(ns[0], Alt(ns[1], ... Alt(ns[n-1], nil)))`
#[cfg_attr(coverage_nightly, coverage(off))]
// The elements are boxed nodes already; the vector is the call site's shape.
#[allow(clippy::vec_box)]
pub(crate) fn make_alt_n(mut nodes: Vec<Box<Node>>) -> Box<Node> {
    assert!(!nodes.is_empty());
    let mut result = node_new_alt(nodes.pop().unwrap(), None);
    while let Some(n) = nodes.pop() {
        result = node_new_alt(n, Some(result));
    }
    result
}

/// Anychar that matches newlines (C: node_new_true_anychar)
pub(crate) fn node_new_true_anychar() -> Box<Node> {
    let mut n = node_new_anychar();
    n.status_add(ND_ST_MULTILINE);
    n
}

/// Copy a String, CClass or CType node (C: onig_node_copy).
/// C returns ONIGERR_TYPE_BUG for the other node types, which have links
/// that C leaves to the caller; here that is `None`. The copy keeps the
/// status bits but has no parent, like a freshly allocated node.
pub(crate) fn onig_node_copy(from: &Node) -> Option<Box<Node>> {
    let inner = match &from.inner {
        NodeInner::String(sn) => NodeInner::String(StrNode {
            s: sn.s.clone(),
            flag: sn.flag,
        }),
        NodeInner::CClass(cc) => NodeInner::CClass(CClassNode {
            flags: cc.flags,
            bs: cc.bs,
            mbuf: cc.mbuf.clone(),
        }),
        NodeInner::CType(ct) => NodeInner::CType(CtypeNode {
            ctype: ct.ctype,
            not: ct.not,
            ascii_mode: ct.ascii_mode,
        }),
        _ => return None,
    };
    let mut copy = node_new(inner);
    copy.status = from.status;
    Some(copy)
}

// === Bitset Utility Functions (from regparse.c) ===

pub(crate) fn bitset_set_range(bs: &mut BitSet, from: usize, to: usize) {
    for i in from..=to {
        bitset_set_bit(bs, i);
    }
}

pub(crate) fn bitset_invert(bs: &mut BitSet) {
    for slot in bs.iter_mut() {
        *slot = !*slot;
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn bitset_invert_to(from: &BitSet, to: &mut BitSet) {
    for i in 0..BITSET_REAL_SIZE {
        to[i] = !from[i];
    }
}

pub(crate) fn bitset_and(dest: &mut BitSet, src: &BitSet) {
    for i in 0..BITSET_REAL_SIZE {
        dest[i] &= src[i];
    }
}

pub(crate) fn bitset_or(dest: &mut BitSet, src: &BitSet) {
    for i in 0..BITSET_REAL_SIZE {
        dest[i] |= src[i];
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn bitset_copy(dest: &mut BitSet, src: &BitSet) {
    dest.copy_from_slice(src);
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn bitset_is_empty(bs: &BitSet) -> bool {
    bs.iter().all(|&b| b == 0)
}

// === String Node Helpers ===

/// Append bytes to a string node
pub(crate) fn node_str_cat(node: &mut Node, s: &[u8]) -> i32 {
    if let Some(sn) = node.as_str_mut() {
        sn.s.extend_from_slice(s);
        ONIG_NORMAL
    } else {
        ONIGERR_TYPE_BUG
    }
}

/// Set string node content
#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn node_str_set(node: &mut Node, s: &[u8]) -> i32 {
    if let Some(sn) = node.as_str_mut() {
        sn.s.clear();
        sn.s.extend_from_slice(s);
        ONIG_NORMAL
    } else {
        ONIGERR_TYPE_BUG
    }
}

/// Clear string node
#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn node_str_clear(node: &mut Node) {
    if let Some(sn) = node.as_str_mut() {
        sn.s.clear();
    }
}

/// Append a single code point to a string node (encoding-aware)
#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn node_str_cat_codepoint(
    node: &mut Node,
    enc: OnigEncoding,
    code: OnigCodePoint,
) -> i32 {
    let mut buf = [0u8; ONIGENC_CODE_TO_MBC_MAXLEN];
    let len = enc.code_to_mbc(code, &mut buf);
    if len < 0 {
        return len;
    }
    node_str_cat(node, &buf[..len as usize])
}

// === Name Table (port of C's NameEntry + hash table) ===

pub(crate) struct NameEntry {
    pub name: Vec<u8>,
    pub back_num: i32,
    pub back_refs: Vec<i32>,
}

pub(crate) struct NameTable {
    pub entries: HashMap<Vec<u8>, NameEntry>,
}

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

impl NameTable {
    pub(crate) fn new() -> Self {
        NameTable {
            entries: HashMap::new(),
        }
    }

    pub(crate) fn find(&self, name: &[u8]) -> Option<&NameEntry> {
        self.entries.get(name)
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn find_mut(&mut self, name: &[u8]) -> Option<&mut NameEntry> {
        self.entries.get_mut(name)
    }

    pub(crate) fn add(
        &mut self,
        name: &[u8],
        backref: i32,
        allow_multiplex: bool,
    ) -> Result<(), i32> {
        if name.is_empty() {
            return Err(ONIGERR_EMPTY_GROUP_NAME);
        }

        if let Some(e) = self.entries.get_mut(name) {
            if e.back_num >= 1 && !allow_multiplex {
                return Err(ONIGERR_MULTIPLEX_DEFINED_NAME);
            }
            e.back_num += 1;
            e.back_refs.push(backref);
            Ok(())
        } else {
            self.entries.insert(
                name.to_vec(),
                NameEntry {
                    name: name.to_vec(),
                    back_num: 1,
                    back_refs: vec![backref],
                },
            );
            Ok(())
        }
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn num_entries(&self) -> i32 {
        self.entries.len() as i32
    }

    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn clear(&mut self) {
        self.entries.clear();
    }

    pub(crate) fn name_to_group_numbers(&self, name: &[u8]) -> Option<&[i32]> {
        self.entries.get(name).map(|e| e.back_refs.as_slice())
    }
}

// === Reduce Quantifier Type Table ===