perl-regex 0.13.1

Regex parsing and validation helpers for Perl syntax
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
//! Perl regex validation and analysis
//!
//! This module provides tools to validate Perl regular expressions
//! and detect potential security or performance issues like catastrophic backtracking.

use thiserror::Error;

/// Error type for Perl regex validation failures.
#[derive(Error, Debug, Clone, PartialEq)]
pub enum RegexError {
    /// Syntax error at a specific byte offset in the regex pattern.
    #[error("{message} at offset {offset}")]
    Syntax {
        /// Human-readable description of the syntax issue.
        message: String,
        /// Byte offset where the error was detected.
        offset: usize,
    },
}

impl RegexError {
    /// Create a new syntax error with a message and byte offset.
    pub fn syntax(message: impl Into<String>, offset: usize) -> Self {
        RegexError::Syntax { message: message.into(), offset }
    }
}

/// Validator for Perl regular expressions to prevent security and performance issues
pub struct RegexValidator {
    max_nesting: usize,
    max_unicode_properties: usize,
}

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

impl RegexValidator {
    /// Create a new validator with default safety limits
    pub fn new() -> Self {
        Self {
            // Default limits from issue #461
            max_nesting: 10,
            // Limit from issue #460
            max_unicode_properties: 50,
        }
    }

    /// Validate a regex pattern for potential performance or security risks
    pub fn validate(&self, pattern: &str, start_pos: usize) -> Result<(), RegexError> {
        self.check_complexity(pattern, start_pos)
    }

    /// Check if the pattern contains embedded code constructs (?{...}) or (??{...})
    pub fn detects_code_execution(&self, pattern: &str) -> bool {
        let bytes = pattern.as_bytes();
        let mut i = 0;
        let len = bytes.len();
        while i < len {
            let ch = bytes[i];
            if ch == b'\\' {
                i += 2; // skip escaped
                continue;
            }
            if ch == b'[' {
                // Skip character class content so literals like [(?{] are not
                // misclassified as embedded code execution.
                i += 1;
                while i < len {
                    let class_ch = bytes[i];
                    if class_ch == b'\\' {
                        i += 2; // skip escaped char inside class
                    } else if class_ch == b']' {
                        i += 1;
                        break;
                    } else {
                        i += 1;
                    }
                }
                continue;
            }
            if ch == b'(' {
                if i + 1 < len && bytes[i + 1] == b'?' {
                    i += 2; // consume '(' and '?'
                    // Check for { or ?{
                    if i < len {
                        if bytes[i] == b'{' {
                            return true; // (?{
                        } else if bytes[i] == b'?' {
                            if i + 1 < len && bytes[i + 1] == b'{' {
                                return true; // (??{
                            }
                        }
                    }
                    continue;
                }
            }
            i += 1;
        }
        false
    }

    /// Check for nested quantifiers that can cause catastrophic backtracking
    /// e.g. (a+)+, (a*)*, (a?)*
    pub fn detect_nested_quantifiers(&self, pattern: &str) -> bool {
        // This is a heuristic check for nested quantifiers
        // It looks for a quantifier character following a group that ends with a quantifier
        // e.g. ")+" in "...)+"
        // Real implementation would need a full regex parser, but this heuristic
        // covers common cases like (a+)+

        let bytes = pattern.as_bytes();
        let mut i = 0;
        let len = bytes.len();
        let mut group_stack = Vec::new();

        // Track the last significant character index and its type
        // Type: 0=other, 1=quantifier, 2=group_end
        let mut last_type = 0;

        while i < len {
            let ch = bytes[i];
            match ch {
                b'\\' => {
                    i += 2; // skip escaped
                    last_type = 0;
                    continue;
                }
                b'(' => {
                    // Check if non-capturing or other special group
                    if i + 1 < len && bytes[i + 1] == b'?' {
                        i += 2; // consume '(' and '?'
                        // Skip group-type specifier so it doesn't reach the
                        // quantifier match arm (mirrors check_complexity logic)
                        if i < len
                            && matches!(
                                bytes[i],
                                b':' | b'=' | b'!' | b'<' | b'>' | b'|' | b'P' | b'#'
                            )
                        {
                            i += 1;
                        }
                    } else {
                        i += 1;
                    }
                    group_stack.push(false); // false = no quantifier inside yet
                    last_type = 0;
                    continue;
                }
                b')' => {
                    if let Some(has_quantifier) = group_stack.pop() {
                        if has_quantifier {
                            last_type = 2; // group end with internal quantifier
                        } else {
                            last_type = 0;
                        }
                    }
                }
                b'+' | b'*' | b'?' | b'{' => {
                    // If we just closed a group that had a quantifier inside,
                    // and now we see another quantifier, that's a nested quantifier!
                    if last_type == 2 {
                        // Check if it's really a quantifier or literal {
                        if ch == b'{' {
                            // Only count as quantifier if it looks like {n} or {n,m}.
                            let mut peek_i = i + 1;
                            if Self::is_brace_quantifier(bytes, &mut peek_i) {
                                return true;
                            } else {
                                // Important fix: If it's not a brace quantifier, do NOT
                                // advance i using peek_i. It's just a literal '{'
                                last_type = 0;
                                i += 1;
                                continue;
                            }
                        } else {
                            return true;
                        }
                    }

                    // Mark current group as having a quantifier
                    if let Some(last) = group_stack.last_mut() {
                        *last = true;
                    }
                    last_type = 1;
                }
                _ => {
                    last_type = 0;
                }
            }
            i += 1;
        }
        false
    }

    fn is_brace_quantifier(bytes: &[u8], i: &mut usize) -> bool {
        // Require at least one digit after '{'
        let mut has_digit = false;
        let mut has_comma = false;
        let len = bytes.len();

        while *i < len {
            let ch = bytes[*i];
            *i += 1;
            if ch.is_ascii_digit() {
                has_digit = true;
            } else if ch == b',' && !has_comma {
                has_comma = true;
            } else if ch == b'}' && has_digit {
                return true;
            } else {
                break;
            }
        }

        false // Should have returned true at '}' if valid
    }

    fn check_complexity(&self, pattern: &str, start_pos: usize) -> Result<(), RegexError> {
        // NOTE: Nested quantifier detection (detect_nested_quantifiers) is intentionally
        // NOT called here. The heuristic produces too many false positives on valid Perl
        // patterns such as (?:/\.)+, (\w+)*, (?:pattern)+. Callers that want an advisory
        // check can invoke detect_nested_quantifiers() directly and surface the result
        // as a non-fatal diagnostic.

        let bytes = pattern.as_bytes();
        let mut i = 0;
        let len = bytes.len();

        // Stack stores the type of the current group
        let mut stack: Vec<GroupType> = Vec::new();
        let mut unicode_property_count = 0;

        while i < len {
            let ch = bytes[i];
            match ch {
                b'\\' => {
                    // Check for escaped character
                    if i + 1 < len {
                        let next_char = bytes[i + 1];
                        match next_char {
                            b'p' | b'P' => {
                                // Unicode property start \p or \P
                                // We consume the 'p'/'P'
                                i += 2;

                                // Check if it's followed by {
                                if i < len && bytes[i] == b'{' {
                                    unicode_property_count += 1;
                                    if unicode_property_count > self.max_unicode_properties {
                                        return Err(RegexError::syntax(
                                            "Too many Unicode properties in regex (max 50)",
                                            start_pos + i - 2, // approximate original idx
                                        ));
                                    }
                                }
                                continue;
                            }
                            _ => {
                                // Just skip other escaped chars
                                i += 2;
                                continue;
                            }
                        }
                    }
                }
                b'[' => {
                    // Need to skip character classes
                    i += 1;
                    while i < len {
                        if bytes[i] == b'\\' {
                            i += 2;
                        } else if bytes[i] == b']' {
                            break;
                        } else {
                            i += 1;
                        }
                    }
                }
                b'(' => {
                    let mut group_type = GroupType::Normal;

                    // Check for extension syntax (?...)
                    if i + 1 < len && bytes[i + 1] == b'?' {
                        i += 2; // consume '(' and '?'

                        // Check for < (lookbehind or named capture)
                        if i < len && bytes[i] == b'<' {
                            i += 1; // consume <

                            // Check for = or ! (lookbehind)
                            if i < len && (bytes[i] == b'=' || bytes[i] == b'!') {
                                i += 1; // consume = or !
                                group_type = GroupType::Lookbehind;
                            }
                            // Otherwise it's likely a named capture (?<name>...) or condition (?<...)
                            // which we treat as a normal group
                        } else if i < len && bytes[i] == b'|' {
                            i += 1; // consume |
                            group_type = GroupType::BranchReset { branch_count: 1 };
                        }
                    } else {
                        i += 1;
                    }

                    match group_type {
                        GroupType::Lookbehind => {
                            // Calculate current lookbehind depth
                            let lookbehind_depth =
                                stack.iter().filter(|g| matches!(g, GroupType::Lookbehind)).count();
                            if lookbehind_depth >= self.max_nesting {
                                return Err(RegexError::syntax(
                                    "Regex lookbehind nesting too deep",
                                    start_pos + i - 1, // rough idx
                                ));
                            }
                        }
                        GroupType::BranchReset { .. } => {
                            // Calculate current branch reset nesting
                            let reset_depth = stack
                                .iter()
                                .filter(|g| matches!(g, GroupType::BranchReset { .. }))
                                .count();
                            if reset_depth >= self.max_nesting {
                                // Use same nesting limit for now
                                return Err(RegexError::syntax(
                                    "Regex branch reset nesting too deep",
                                    start_pos + i - 1,
                                ));
                            }
                        }
                        _ => {}
                    }
                    stack.push(group_type);
                    continue;
                }
                b'|' => {
                    // Check if we are in a branch reset group
                    if let Some(GroupType::BranchReset { branch_count }) = stack.last_mut() {
                        *branch_count += 1;
                        if *branch_count > 50 {
                            // Max 50 branches
                            return Err(RegexError::syntax(
                                "Too many branches in branch reset group (max 50)",
                                start_pos + i,
                            ));
                        }
                    }
                }
                b')' => {
                    // Pop group from stack
                    stack.pop();
                }
                _ => {}
            }
            i += 1;
        }

        Ok(())
    }
}

enum GroupType {
    Normal,
    Lookbehind,
    BranchReset { branch_count: usize },
}

/// A named capture group extracted from a regex pattern.

#[derive(Debug, Clone, PartialEq)]
pub struct CaptureGroup {
    /// The capture group name from `(?<name>...)`.
    pub name: String,
    /// One-based capture index (counting all capturing groups left to right).
    pub index: usize,
    /// The sub-pattern inside the capture group.
    pub pattern: String,
}

/// Analysis utilities for Perl regex patterns: capture extraction and hover text.
pub struct RegexAnalyzer;

impl RegexAnalyzer {
    /// Extract all named capture groups from a Perl regex pattern.
    ///
    /// Scans the pattern for `(?<name>...)` groups and returns them in left-to-right
    /// order. Non-capturing groups (`(?:...)`), lookaheads, and lookbehinds do not
    /// increment the capture index. Escaped parentheses (`\(`) are skipped.
    ///
    /// # Example
    /// ```
    /// use perl_regex::RegexAnalyzer;
    /// let caps = RegexAnalyzer::extract_named_captures("(?<year>\\d{4})-(?<month>\\d{2})");
    /// assert_eq!(caps.len(), 2);
    /// assert_eq!(caps[0].name, "year");
    /// assert_eq!(caps[0].index, 1);
    /// ```
    pub fn extract_named_captures(pattern: &str) -> Vec<CaptureGroup> {
        let mut result = Vec::new();
        let mut capture_index = 0usize;
        let bytes = pattern.as_bytes();
        let len = bytes.len();
        let mut i = 0;

        while i < len {
            // Skip escaped characters.
            if bytes[i] == b'\\' {
                i += 2;
                continue;
            }

            // Skip character classes [...] entirely.
            if bytes[i] == b'[' {
                i += 1;
                while i < len {
                    if bytes[i] == b'\\' {
                        i += 2;
                    } else if bytes[i] == b']' {
                        i += 1;
                        break;
                    } else {
                        i += 1;
                    }
                }
                continue;
            }

            if bytes[i] == b'(' {
                i += 1;

                // Determine the group kind.
                if i < len && bytes[i] == b'?' {
                    i += 1; // consume '?'

                    if i < len && bytes[i] == b'<' {
                        i += 1; // consume '<'

                        // Lookbehind: (?<= or (?<!  — not a capture.
                        if i < len && (bytes[i] == b'=' || bytes[i] == b'!') {
                            i += 1;
                            continue;
                        }

                        if let Some((name, next_pos)) =
                            parse_named_capture_name_from(bytes, i, b'>')
                        {
                            capture_index += 1;
                            i = next_pos;

                            // Collect the sub-pattern up to the matching ')'.
                            let pattern_start = i;
                            let mut depth = 1usize;
                            while i < len && depth > 0 {
                                if bytes[i] == b'\\' {
                                    i += 2;
                                    continue;
                                }
                                if bytes[i] == b'[' {
                                    i += 1;
                                    while i < len {
                                        if bytes[i] == b'\\' {
                                            i += 2;
                                        } else if bytes[i] == b']' {
                                            i += 1;
                                            break;
                                        } else {
                                            i += 1;
                                        }
                                    }
                                    continue;
                                }
                                if bytes[i] == b'(' {
                                    depth += 1;
                                } else if bytes[i] == b')' {
                                    depth -= 1;
                                }
                                i += 1;
                            }
                            // The ')' was consumed above; sub-pattern ends before it.
                            let sub: String = if i > 0 && pattern_start < i - 1 {
                                // Since we parsed byte by byte matching ASCII mostly,
                                // the slice boundaries should be valid UTF-8.
                                // If not, String::from_utf8_lossy covers it safely.
                                String::from_utf8_lossy(&bytes[pattern_start..i - 1]).into_owned()
                            } else {
                                String::new()
                            };

                            result.push(CaptureGroup { name, index: capture_index, pattern: sub });
                            continue;
                        }
                    } else if i < len && bytes[i] == b'\'' {
                        if let Some((name, next_pos)) =
                            parse_named_capture_name(bytes, i, b'\'', b'\'')
                        {
                            capture_index += 1;
                            i = next_pos;

                            // Collect the sub-pattern up to the matching ')'.
                            let pattern_start = i;
                            let mut depth = 1usize;
                            while i < len && depth > 0 {
                                if bytes[i] == b'\\' {
                                    i += 2;
                                    continue;
                                }
                                if bytes[i] == b'[' {
                                    i += 1;
                                    while i < len {
                                        if bytes[i] == b'\\' {
                                            i += 2;
                                        } else if bytes[i] == b']' {
                                            i += 1;
                                            break;
                                        } else {
                                            i += 1;
                                        }
                                    }
                                    continue;
                                }
                                if bytes[i] == b'(' {
                                    depth += 1;
                                } else if bytes[i] == b')' {
                                    depth -= 1;
                                }
                                i += 1;
                            }
                            // The ')' was consumed above; sub-pattern ends before it.
                            let sub: String = if i > 0 && pattern_start < i - 1 {
                                String::from_utf8_lossy(&bytes[pattern_start..i - 1]).into_owned()
                            } else {
                                String::new()
                            };

                            result.push(CaptureGroup { name, index: capture_index, pattern: sub });
                            continue;
                        }
                    } else if i < len
                        && matches!(bytes[i], b':' | b'=' | b'!' | b'>' | b'|' | b'P' | b'#')
                    {
                        // Non-capturing group: (?:...), (?=...), (?!...), (?|...), etc.
                        // Does not increment capture_index; just move on (fall through to
                        // normal scanning — the loop will handle nested parens naturally).
                        continue;
                    }
                    // Any other (?...) — treat as non-capturing for index purposes.
                    continue;
                }

                // Plain capturing group `(...)`.
                capture_index += 1;
                continue;
            }

            i += 1;
        }

        result
    }

    /// Generate hover text for a Perl regex pattern and its modifiers.
    ///
    /// Summarises the named capture groups and explains the meaning of each
    /// modifier flag (`i`, `m`, `s`, `x`, `g`, `a`, `d`, `l`, `u`, `n`,
    /// `p`, `r`, `c`, `o`, `e`). Repeated modifiers are deduplicated.
    /// Unknown modifier flags are collected and appended as
    /// `Unknown modifiers: \`…\`` at the end of the hover text.
    ///
    /// # Example
    /// ```
    /// use perl_regex::RegexAnalyzer;
    /// let text = RegexAnalyzer::hover_text_for_regex("(?<id>\\d+)", "i");
    /// assert!(text.contains("id"));
    /// assert!(text.contains("case"));
    /// ```
    pub fn hover_text_for_regex(pattern: &str, modifiers: &str) -> String {
        let mut parts: Vec<String> = Vec::new();

        if !pattern.is_empty() {
            parts.push(format!("Regex: `{pattern}`"));
        }

        // Named captures section.
        let captures = Self::extract_named_captures(pattern);
        if !captures.is_empty() {
            parts.push("Named captures:".to_string());
            for cap in &captures {
                parts.push(format!(
                    "  ${{{name}}} (capture {index}): `{pat}`",
                    name = cap.name,
                    index = cap.index,
                    pat = cap.pattern,
                ));
            }
        }

        // Modifier explanations.
        let mut seen_modifiers: Vec<char> = Vec::new();
        let mut modifier_notes: Vec<&str> = Vec::new();
        let mut unknown_modifiers: Vec<char> = Vec::new();
        for modifier in modifiers.chars() {
            if seen_modifiers.contains(&modifier) {
                continue;
            }
            seen_modifiers.push(modifier);
            match describe_modifier(modifier) {
                Some(description) => modifier_notes.push(description),
                None => {
                    unknown_modifiers.push(modifier);
                }
            }
        }

        if !modifier_notes.is_empty() {
            parts.push("Modifiers:".to_string());
            for note in modifier_notes {
                parts.push(format!("  {note}"));
            }
        }

        if !unknown_modifiers.is_empty() {
            let unknown: String = unknown_modifiers.into_iter().collect();
            parts.push(format!("Unknown modifiers: `{unknown}`"));
        }

        parts.join("\n")
    }
}

fn describe_modifier(modifier: char) -> Option<&'static str> {
    match modifier {
        'i' => Some("case-insensitive matching"),
        'm' => Some("multiline mode: ^ and $ match line boundaries"),
        's' => Some("single-line mode: dot matches newline"),
        'x' => Some("extended mode: whitespace and comments allowed"),
        'g' => Some("global: match all occurrences"),
        'a' => Some("ASCII-safe character classes"),
        'd' => Some("native platform character set semantics"),
        'l' => Some("locale-dependent character semantics"),
        'u' => Some("Unicode character semantics"),
        'n' => Some("non-capturing by default for unnamed groups"),
        'p' => Some("preserve string for ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH}"),
        'r' => Some("non-destructive substitution result"),
        'c' => Some("keep current match position for /g scans"),
        'o' => Some("compile pattern only once"),
        'e' => Some("evaluate replacement as code in substitutions"),
        _ => None,
    }
}

fn parse_named_capture_name(
    bytes: &[u8],
    pos: usize,
    open_delim: u8,
    close_delim: u8,
) -> Option<(String, usize)> {
    if pos >= bytes.len() || bytes[pos] != open_delim {
        return None;
    }

    let mut i = pos + 1;
    let name_start = i;
    while i < bytes.len() && bytes[i] != close_delim {
        i += 1;
    }

    if i == name_start || i >= bytes.len() {
        return None;
    }

    let name = String::from_utf8_lossy(&bytes[name_start..i]).into_owned();
    Some((name, i + 1))
}

fn parse_named_capture_name_from(
    bytes: &[u8],
    start: usize,
    close_delim: u8,
) -> Option<(String, usize)> {
    if start >= bytes.len() {
        return None;
    }

    let mut i = start;
    while i < bytes.len() && bytes[i] != close_delim {
        i += 1;
    }

    if i == start || i >= bytes.len() {
        return None;
    }

    let name = String::from_utf8_lossy(&bytes[start..i]).into_owned();
    Some((name, i + 1))
}

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

    // --- RegexError ---

    #[test]
    fn regex_error_syntax_stores_message_and_offset() {
        let err = RegexError::syntax("unexpected char", 7);
        match &err {
            RegexError::Syntax { message, offset } => {
                assert_eq!(message, "unexpected char");
                assert_eq!(*offset, 7);
            }
        }
        assert!(err.to_string().contains("7"));
        assert!(err.to_string().contains("unexpected char"));
    }

    #[test]
    fn regex_error_implements_clone_and_partialeq() {
        let e1 = RegexError::syntax("msg", 3);
        let e2 = e1.clone();
        assert_eq!(e1, e2);
    }

    // --- RegexValidator::validate (valid patterns) ---

    #[test]
    fn validate_simple_pattern_ok() {
        let v = RegexValidator::new();
        assert!(v.validate("hello", 0).is_ok());
        assert!(v.validate("", 0).is_ok());
        assert!(v.validate("(a|b)+", 0).is_ok());
    }

    #[test]
    fn validate_unicode_property_within_limit_ok() {
        let v = RegexValidator::new();
        // 50 unicode properties is the limit
        let pattern = r"\p{L}".repeat(50);
        assert!(v.validate(&pattern, 0).is_ok());
    }

    #[test]
    fn validate_too_many_unicode_properties_errors() {
        let v = RegexValidator::new();
        let pattern = r"\p{L}".repeat(51);
        let err = v.validate(&pattern, 0).unwrap_err();
        assert!(err.to_string().contains("Unicode"));
    }

    #[test]
    fn validate_unicode_property_offset_propagated() {
        let v = RegexValidator::new();
        let prefix = "x";
        let pattern = format!("{}{}", prefix, r"\p{L}".repeat(51));
        let err = v.validate(&pattern, 10).unwrap_err();
        // The reported offset should be >= 10 (start_pos)
        match err {
            RegexError::Syntax { offset, .. } => assert!(offset >= 10),
        }
    }

    #[test]
    fn validate_lookbehind_within_limit_ok() {
        let v = RegexValidator::new();
        // 10 is the limit; 9 nested lookbehinds should be fine
        let mut pattern = String::from("foo");
        for _ in 0..9 {
            pattern = format!("(?<={})", pattern);
        }
        assert!(v.validate(&pattern, 0).is_ok());
    }

    #[test]
    fn validate_lookbehind_nesting_too_deep_errors() {
        let v = RegexValidator::new();
        // Build 11 nested lookbehinds to exceed the depth limit of 10
        let mut pattern = String::from("a");
        for _ in 0..11 {
            pattern = format!("(?<={})", pattern);
        }
        let err = v.validate(&pattern, 0).unwrap_err();
        assert!(err.to_string().contains("lookbehind") || err.to_string().contains("nesting"));
    }

    #[test]
    fn validate_branch_reset_nesting_too_deep_errors() {
        let v = RegexValidator::new();
        let mut pattern = String::from("a");
        for _ in 0..11 {
            pattern = format!("(?|{})", pattern);
        }
        let err = v.validate(&pattern, 0).unwrap_err();
        assert!(err.to_string().contains("branch reset") || err.to_string().contains("nesting"));
    }

    #[test]
    fn validate_too_many_branches_in_reset_group_errors() {
        let v = RegexValidator::new();
        // 51 alternatives in one (?| ... ) group exceeds max 50 branches
        let alts = (0u32..51).map(|i| format!("a{i}")).collect::<Vec<_>>().join("|");
        let pattern = format!("(?|{alts})");
        let err = v.validate(&pattern, 0).unwrap_err();
        assert!(err.to_string().contains("branch") || err.to_string().contains("50"));
    }

    #[test]
    fn validate_character_class_skipped() {
        // `[(?{]` should not trigger embedded code detection in validate()
        let v = RegexValidator::new();
        assert!(v.validate("[(?{]", 0).is_ok());
    }

    // --- RegexValidator::detects_code_execution ---

    #[test]
    fn detects_code_execution_with_code_block() {
        let v = RegexValidator::new();
        assert!(v.detects_code_execution("(?{ print 'hi' })"));
    }

    #[test]
    fn detects_code_execution_with_deferred_code_block() {
        let v = RegexValidator::new();
        assert!(v.detects_code_execution("(??{ some_code() })"));
    }

    #[test]
    fn detects_code_execution_false_for_non_capturing() {
        let v = RegexValidator::new();
        assert!(!v.detects_code_execution("(?:foo)"));
        assert!(!v.detects_code_execution("(?=ahead)"));
        assert!(!v.detects_code_execution("(?!not)"));
    }

    #[test]
    fn detects_code_execution_escaped_paren_not_detected() {
        let v = RegexValidator::new();
        assert!(!v.detects_code_execution(r"\(?{"));
    }

    #[test]
    fn detects_code_execution_in_char_class_not_detected() {
        let v = RegexValidator::new();
        assert!(!v.detects_code_execution("[(?{]"));
    }

    #[test]
    fn detects_code_execution_empty_pattern() {
        let v = RegexValidator::new();
        assert!(!v.detects_code_execution(""));
    }

    // --- RegexValidator::detect_nested_quantifiers ---

    #[test]
    fn detect_nested_quantifiers_finds_plus_plus() {
        let v = RegexValidator::new();
        assert!(v.detect_nested_quantifiers("(a+)+"));
    }

    #[test]
    fn detect_nested_quantifiers_finds_star_star() {
        let v = RegexValidator::new();
        assert!(v.detect_nested_quantifiers("(a*)*"));
    }

    #[test]
    fn detect_nested_quantifiers_finds_brace_quantifier() {
        let v = RegexValidator::new();
        assert!(v.detect_nested_quantifiers("(a+){2,5}"));
    }

    #[test]
    fn detect_nested_quantifiers_safe_patterns() {
        let v = RegexValidator::new();
        assert!(!v.detect_nested_quantifiers("(abc)+")); // no inner quantifier
        assert!(!v.detect_nested_quantifiers("[a-z]+")); // character class, not group
        assert!(!v.detect_nested_quantifiers("a+b+")); // quantifiers outside groups
    }

    // --- RegexValidator::Default ---

    #[test]
    fn default_is_same_as_new() {
        let v: RegexValidator = Default::default();
        assert!(v.validate("simple", 0).is_ok());
    }

    // --- RegexAnalyzer::extract_named_captures ---

    #[test]
    fn extract_named_captures_angle_bracket_syntax() {
        let caps = RegexAnalyzer::extract_named_captures(r"(?<year>\d{4})-(?<month>\d{2})");
        assert_eq!(caps.len(), 2);
        assert_eq!(caps[0].name, "year");
        assert_eq!(caps[0].index, 1);
        assert_eq!(caps[1].name, "month");
        assert_eq!(caps[1].index, 2);
    }

    #[test]
    fn extract_named_captures_single_quote_syntax() {
        let caps = RegexAnalyzer::extract_named_captures(r"(?'name'\w+)");
        assert_eq!(caps.len(), 1);
        assert_eq!(caps[0].name, "name");
        assert_eq!(caps[0].index, 1);
    }

    #[test]
    fn extract_named_captures_no_captures() {
        let caps = RegexAnalyzer::extract_named_captures(r"\d+\.\d+");
        assert!(caps.is_empty());
    }

    #[test]
    fn extract_named_captures_non_capturing_group_not_counted() {
        let caps = RegexAnalyzer::extract_named_captures(r"(?:foo)(?<bar>baz)");
        assert_eq!(caps.len(), 1);
        assert_eq!(caps[0].name, "bar");
        assert_eq!(caps[0].index, 1); // plain capturing groups before it still count
    }

    #[test]
    fn extract_named_captures_lookbehind_not_counted() {
        // (?<= ...) is lookbehind, not a named capture
        let caps = RegexAnalyzer::extract_named_captures(r"(?<=foo)(?<word>\w+)");
        assert_eq!(caps.len(), 1);
        assert_eq!(caps[0].name, "word");
    }

    #[test]
    fn extract_named_captures_escaped_paren_skipped() {
        let caps = RegexAnalyzer::extract_named_captures(r"\((?<x>\d)\)");
        assert_eq!(caps.len(), 1);
        assert_eq!(caps[0].name, "x");
    }

    #[test]
    fn extract_named_captures_stores_subpattern() {
        let caps = RegexAnalyzer::extract_named_captures(r"(?<id>\d+)");
        assert_eq!(caps.len(), 1);
        assert_eq!(caps[0].pattern, r"\d+");
    }

    // --- RegexAnalyzer::hover_text_for_regex ---

    #[test]
    fn hover_text_includes_pattern_and_captures() {
        let text = RegexAnalyzer::hover_text_for_regex(r"(?<id>\d+)", "i");
        assert!(text.contains("id"));
        assert!(text.contains("case"));
    }

    #[test]
    fn hover_text_modifier_explanations() {
        let text = RegexAnalyzer::hover_text_for_regex("foo", "imsx");
        assert!(text.contains("case-insensitive"));
        assert!(text.contains("multiline"));
        assert!(text.contains("single-line"));
        assert!(text.contains("extended"));
    }

    #[test]
    fn hover_text_global_modifier() {
        let text = RegexAnalyzer::hover_text_for_regex("foo", "g");
        assert!(text.contains("global"));
    }

    #[test]
    fn hover_text_no_modifiers() {
        let text = RegexAnalyzer::hover_text_for_regex("hello", "");
        assert!(text.contains("hello"));
        assert!(!text.contains("Modifiers"));
    }

    #[test]
    fn hover_text_empty_pattern() {
        let text = RegexAnalyzer::hover_text_for_regex("", "");
        assert!(text.is_empty());
    }

    #[test]
    fn hover_text_unknown_modifier_ignored() {
        let text = RegexAnalyzer::hover_text_for_regex("x", "z");
        // z is not a known modifier, so no modifier section
        assert!(!text.contains("Modifiers"));
    }
}