dd-sensitive-data-scanner 0.0.0

Core Sensitive Data Scanner library for detecting and redacting sensitive information.
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
use crate::parser::ast::{
    AsciiClassKind as SdsAsciiClassKind, AsciiClassKind, AssertionType as SdsAssertionType,
    Ast as SdsAst, BracketCharacterClassItem as SdsBracketCharacterClassItem,
    BracketCharacterClassItem, CharacterClass, Flag as SdsFlag, Flags as SdsFlags,
    Group as SdsGroup, Literal, PerlCharacterClass as SdsPerlCharacterClass,
    Quantifier as SdsQuantifier, QuantifierKind as SdsQuantifierKind, Repetition as SdsRepetition,
    UnicodePropertyClass as SdsUnicodePropertyClass,
};
use crate::parser::error::ParseError;
use crate::parser::regex_parser::parse_regex_pattern;
use regex_syntax::ast::{
    Alternation as RegexAlternation, Assertion as RegexAssertion,
    AssertionKind as RegexAssertionKind, Ast as RegexAst, CaptureName as RegexCaptureName,
    Class as RegexClass, ClassAscii as RegexClassAscii, ClassAsciiKind as RegexClassAsciiKind,
    ClassBracketed as RegexClassBracketed, ClassSet as RegexClassSet,
    ClassSetItem as RegexClassSetItem, ClassSetRange as RegexClassSetRange,
    ClassSetUnion as RegexClassSetUnion, ClassUnicode as RegexClassUnicode,
    ClassUnicodeKind as RegexClassUnicodeKind, Concat as RegexConcat, Flag as RegexFlag,
    Flags as RegexFlags, FlagsItem as RegexFlagsItem, FlagsItemKind as RegexFlagsItemKind,
    Group as RegexGroup, GroupKind as RegexGroupKind, HexLiteralKind as RegexHexLiteralKind,
    Literal as RegexLiteral, LiteralKind as RegexLiteralKind, Position,
    Repetition as RegexRepetition, RepetitionKind as RegexRepetitionKind,
    RepetitionOp as RegexRepetitionOp, RepetitionRange, SetFlags as RegexSetFlags, Span,
};
use std::rc::Rc;

const ASCII_WHITESPACE_CHARS: &[char] = &['\x0D', '\x0A', '\x09', '\x0C', '\x0B', '\x20'];
pub const QUANTIFIER_LIMIT: u32 = 3000;
/// This takes an SDS style regex pattern and converts it to a pattern
/// compatible with the Rust `regex` crate.
///
/// If this function returns successfully, the provided String is not necessarily
/// a valid SDS regex pattern. For example, complexity limits, empty matching, .etc are
/// not things that are checked here.
///
/// This conversion guarantees that the matching behavior is identical, but it
/// makes no attempt to preserve the exact syntax used. This is only intended
/// to feed directly into a regex engine, and not intended for human readability.
pub fn convert_to_rust_regex(pattern: &str) -> Result<String, ParseError> {
    convert_to_rust_regex_ast(pattern).map(|s| s.to_string())
}

pub fn convert_to_rust_regex_ast(pattern: &str) -> Result<RegexAst, ParseError> {
    let sds_ast = parse_regex_pattern(pattern)?;
    let regex_ast = convert_ast(&sds_ast)?;
    Ok(regex_ast)
}

// This is private since only ASTs generated from the parser are supported.
// (Manually crafted ASTs may cause issues).
fn convert_ast(sds_ast: &SdsAst) -> Result<RegexAst, ParseError> {
    Ok(match sds_ast {
        SdsAst::Empty => RegexAst::Empty(span()),
        SdsAst::Literal(c) => RegexAst::Literal(convert_literal(*c, LiteralKind::Normal)),
        SdsAst::Concat(list) => RegexAst::Concat(RegexConcat {
            span: span(),
            asts: list.iter().map(convert_ast).collect::<Result<_, _>>()?,
        }),
        SdsAst::Group(group) => {
            match group.as_ref() {
                SdsGroup::Capturing(capturing_group) => {
                    RegexAst::Group(RegexGroup {
                        span: span(),
                        // The group index is not used for conversion, so a dummy "0" is provided
                        kind: RegexGroupKind::CaptureIndex(0),
                        ast: Box::new(convert_ast(&capturing_group.inner)?),
                    })
                }
                SdsGroup::NonCapturing(non_capturing) => RegexAst::Group(RegexGroup {
                    span: span(),
                    kind: RegexGroupKind::NonCapturing(convert_flags(&non_capturing.flags)),
                    ast: Box::new(convert_ast(&non_capturing.inner)?),
                }),
                SdsGroup::NamedCapturing(named_capturing) => RegexAst::Group(RegexGroup {
                    span: span(),
                    kind: RegexGroupKind::CaptureName {
                        starts_with_p: false,
                        name: RegexCaptureName {
                            span: span(),
                            name: named_capturing.name.clone(),
                            // The group index is not used for conversion, so a dummy "0" is provided
                            index: 0,
                        },
                    },
                    ast: Box::new(convert_ast(&named_capturing.inner)?),
                }),
            }
        }
        SdsAst::Alternation(list) => RegexAst::Alternation(RegexAlternation {
            span: span(),
            asts: list.iter().map(convert_ast).collect::<Result<_, _>>()?,
        }),
        SdsAst::Flags(flags) => RegexAst::Flags(RegexSetFlags {
            span: span(),
            flags: convert_flags(flags),
        }),
        SdsAst::Repetition(repetition) => RegexAst::Repetition(RegexRepetition {
            span: span(),
            op: RegexRepetitionOp {
                span: span(),
                kind: match repetition.quantifier.kind {
                    SdsQuantifierKind::ZeroOrMore => RegexRepetitionKind::ZeroOrMore,
                    SdsQuantifierKind::RangeExact(exact) => {
                        if exact > QUANTIFIER_LIMIT {
                            return Err(ParseError::ExceededQuantifierLimit);
                        }
                        RegexRepetitionKind::Range(RepetitionRange::Exactly(exact))
                    }
                    SdsQuantifierKind::RangeMinMax(min, max) => {
                        if min > QUANTIFIER_LIMIT || max > QUANTIFIER_LIMIT {
                            return Err(ParseError::ExceededQuantifierLimit);
                        }
                        RegexRepetitionKind::Range(RepetitionRange::Bounded(min, max))
                    }
                    SdsQuantifierKind::RangeMin(min) => {
                        if min > QUANTIFIER_LIMIT {
                            return Err(ParseError::ExceededQuantifierLimit);
                        }
                        RegexRepetitionKind::Range(RepetitionRange::AtLeast(min))
                    }
                    SdsQuantifierKind::ZeroOrOne => RegexRepetitionKind::ZeroOrOne,
                    SdsQuantifierKind::OneOrMore => RegexRepetitionKind::OneOrMore,
                },
            },
            greedy: !repetition.quantifier.lazy,
            ast: Box::new(convert_ast(&repetition.inner)?),
        }),
        SdsAst::Assertion(assertion_type) => match assertion_type {
            SdsAssertionType::WordBoundary => {
                // The "Unicode" flag is disabled to disable the equivalent of Hyperscans UCP flag
                RegexAst::Group(RegexGroup {
                    span: span(),
                    kind: RegexGroupKind::NonCapturing(RegexFlags {
                        span: span(),
                        items: vec![
                            RegexFlagsItem {
                                span: span(),
                                kind: RegexFlagsItemKind::Negation,
                            },
                            RegexFlagsItem {
                                span: span(),
                                kind: RegexFlagsItemKind::Flag(RegexFlag::Unicode),
                            },
                        ],
                    }),
                    ast: Box::new(RegexAst::Assertion(RegexAssertion {
                        span: span(),
                        kind: RegexAssertionKind::WordBoundary,
                    })),
                })
            }
            SdsAssertionType::NotWordBoundary => {
                // The "Unicode" flag is disabled to disable the equivalent of Hyperscans UCP flag
                RegexAst::Group(RegexGroup {
                    span: span(),
                    kind: RegexGroupKind::NonCapturing(RegexFlags {
                        span: span(),
                        items: vec![
                            RegexFlagsItem {
                                span: span(),
                                kind: RegexFlagsItemKind::Negation,
                            },
                            RegexFlagsItem {
                                span: span(),
                                kind: RegexFlagsItemKind::Flag(RegexFlag::Unicode),
                            },
                        ],
                    }),
                    ast: Box::new(RegexAst::Assertion(RegexAssertion {
                        span: span(),
                        kind: RegexAssertionKind::NotWordBoundary,
                    })),
                })
            }
            SdsAssertionType::StartLine => RegexAst::Assertion(RegexAssertion {
                span: span(),
                kind: RegexAssertionKind::StartLine,
            }),
            SdsAssertionType::EndLine => {
                // This is not directly supported in Rust. (Rust does not allow the optional \n)
                // $ is converted to \n?$
                RegexAst::Concat(RegexConcat {
                    span: span(),
                    asts: vec![
                        RegexAst::Repetition(RegexRepetition {
                            span: span(),
                            op: RegexRepetitionOp {
                                span: span(),
                                kind: RegexRepetitionKind::ZeroOrOne,
                            },
                            greedy: true,
                            ast: Box::new(RegexAst::Literal(RegexLiteral {
                                span: span(),
                                kind: RegexLiteralKind::HexBrace(RegexHexLiteralKind::X),
                                c: '\n',
                            })),
                        }),
                        RegexAst::Assertion(RegexAssertion {
                            span: span(),
                            kind: RegexAssertionKind::EndLine,
                        }),
                    ],
                })
            }
            SdsAssertionType::StartText => RegexAst::Assertion(RegexAssertion {
                span: span(),
                kind: RegexAssertionKind::StartText,
            }),
            SdsAssertionType::EndText => RegexAst::Assertion(RegexAssertion {
                span: span(),
                kind: RegexAssertionKind::EndText,
            }),
            SdsAssertionType::EndTextOptionalNewline => {
                // This is not directly supported in Rust.
                // \Z is converted to \n?\z
                convert_ast(&SdsAst::Concat(vec![
                    SdsAst::Repetition(SdsRepetition {
                        quantifier: SdsQuantifier {
                            lazy: false,
                            kind: SdsQuantifierKind::ZeroOrOne,
                        },
                        inner: Rc::new(SdsAst::Literal(Literal {
                            c: '\n',
                            // the `x` flag should not ignore this, so it's escaped
                            escaped: true,
                        })),
                    }),
                    SdsAst::Assertion(SdsAssertionType::EndText),
                ]))?
            }
        },
        SdsAst::CharacterClass(class) => match class {
            CharacterClass::Bracket(bracket) => {
                let items = convert_bracket_items(&bracket.items);

                RegexAst::Class(RegexClass::Bracketed(RegexClassBracketed {
                    span: span(),
                    negated: bracket.negated,
                    kind: RegexClassSet::Item(RegexClassSetItem::Union(RegexClassSetUnion {
                        span: span(),
                        items,
                    })),
                }))
            }
            CharacterClass::Perl(perl) => {
                RegexAst::Class(RegexClass::Bracketed(convert_perl_class(perl)))
            }
            CharacterClass::Dot => RegexAst::Dot(span()),
            CharacterClass::UnicodeProperty(class) => {
                RegexAst::Class(RegexClass::Unicode(convert_unicode_class(class)))
            }
            CharacterClass::HorizontalWhitespace => {
                RegexAst::Class(RegexClass::Bracketed(horizontal_whitespace(false)))
            }
            CharacterClass::NotHorizontalWhitespace => {
                RegexAst::Class(RegexClass::Bracketed(horizontal_whitespace(true)))
            }
            CharacterClass::VerticalWhitespace => {
                RegexAst::Class(RegexClass::Bracketed(vertical_whitespace(false)))
            }
            CharacterClass::NotVerticalWhitespace => {
                RegexAst::Class(RegexClass::Bracketed(vertical_whitespace(true)))
            }
        },
    })
}

fn horizontal_whitespace(negated: bool) -> RegexClassBracketed {
    RegexClassBracketed {
        span: span(),
        negated,
        kind: RegexClassSet::Item(RegexClassSetItem::Union(RegexClassSetUnion {
            span: span(),
            items: vec![
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: ' ',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\t',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
            ],
        })),
    }
}

fn vertical_whitespace(negated: bool) -> RegexClassBracketed {
    RegexClassBracketed {
        span: span(),
        negated,
        kind: RegexClassSet::Item(RegexClassSetItem::Union(RegexClassSetUnion {
            span: span(),
            items: vec![
                RegexClassSetItem::Literal(convert_literal(
                    // vertical tab
                    Literal {
                        c: '\x0B',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\n',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    // form feed
                    Literal {
                        c: '\x0C',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\r',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
            ],
        })),
    }
}

fn convert_bracket_items(items: &[SdsBracketCharacterClassItem]) -> Vec<RegexClassSetItem> {
    let mut output: Vec<RegexClassSetItem> = vec![];

    for item in items {
        match item {
            BracketCharacterClassItem::Literal(c) => {
                output.push(RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: *c,
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )));
            }
            BracketCharacterClassItem::Range(start, end) => {
                output.push(RegexClassSetItem::Range(RegexClassSetRange {
                    span: span(),
                    start: convert_literal(
                        Literal {
                            c: *start,
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                    end: convert_literal(
                        Literal {
                            c: *end,
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                }))
            }
            BracketCharacterClassItem::PerlCharacterClass(class) => {
                output.push(RegexClassSetItem::Bracketed(Box::new(convert_perl_class(
                    class,
                ))));
            }
            BracketCharacterClassItem::UnicodeProperty(class) => {
                output.push(RegexClassSetItem::Unicode(convert_unicode_class(class)))
            }
            BracketCharacterClassItem::AsciiClass(ascii_class) => {
                output.push(RegexClassSetItem::Ascii(RegexClassAscii {
                    span: span(),
                    kind: convert_ascii_class_kind(&ascii_class.kind),
                    negated: ascii_class.negated,
                }))
            }
            BracketCharacterClassItem::HorizontalWhitespace => {
                output.push(RegexClassSetItem::Bracketed(Box::new(
                    horizontal_whitespace(false),
                )));
            }
            BracketCharacterClassItem::NotHorizontalWhitespace => {
                output.push(RegexClassSetItem::Bracketed(Box::new(
                    horizontal_whitespace(true),
                )));
            }
            BracketCharacterClassItem::VerticalWhitespace => {
                output.push(RegexClassSetItem::Bracketed(Box::new(vertical_whitespace(
                    false,
                ))));
            }
            BracketCharacterClassItem::NotVerticalWhitespace => {
                output.push(RegexClassSetItem::Bracketed(Box::new(vertical_whitespace(
                    true,
                ))));
            }
        };
    }
    output
}

fn convert_ascii_class_kind(kind: &SdsAsciiClassKind) -> RegexClassAsciiKind {
    match kind {
        AsciiClassKind::Alnum => RegexClassAsciiKind::Alnum,
        AsciiClassKind::Alpha => RegexClassAsciiKind::Alpha,
        AsciiClassKind::Ascii => RegexClassAsciiKind::Ascii,
        AsciiClassKind::Blank => RegexClassAsciiKind::Blank,
        AsciiClassKind::Cntrl => RegexClassAsciiKind::Cntrl,
        AsciiClassKind::Digit => RegexClassAsciiKind::Digit,
        AsciiClassKind::Graph => RegexClassAsciiKind::Graph,
        AsciiClassKind::Lower => RegexClassAsciiKind::Lower,
        AsciiClassKind::Print => RegexClassAsciiKind::Print,
        AsciiClassKind::Punct => RegexClassAsciiKind::Punct,
        AsciiClassKind::Space => RegexClassAsciiKind::Space,
        AsciiClassKind::Upper => RegexClassAsciiKind::Upper,
        AsciiClassKind::Word => RegexClassAsciiKind::Word,
        AsciiClassKind::Xdigit => RegexClassAsciiKind::Xdigit,
    }
}

fn convert_unicode_class(class: &SdsUnicodePropertyClass) -> RegexClassUnicode {
    RegexClassUnicode {
        span: span(),
        negated: class.negate,
        kind: RegexClassUnicodeKind::Named(class.name.clone()),
    }
}

fn convert_literal(literal: Literal, kind: LiteralKind) -> RegexLiteral {
    let kind = if regex_syntax::is_meta_character(literal.c) {
        RegexLiteralKind::Meta
    } else if kind == LiteralKind::BracketedCharacterClass && literal.c.is_whitespace() {
        // Rust's "x" flag ignores _all_ whitespace, where in the SDS standard syntax (and PCRE2)
        // "x" doesn't ignore whitespace in bracketed character classes. If the literal is being
        // used in a bracketed character class, all whitespace is hex encoded to ensure it
        // is never ignored.
        RegexLiteralKind::HexBrace(RegexHexLiteralKind::X)
    } else if kind == LiteralKind::Normal && ASCII_WHITESPACE_CHARS.contains(&literal.c) {
        // The HexBrace kind is usually preferred since it makes whitespace characters easier
        // to read, but escaping whitespace can change the behavior of the `x` (extended / verbose)
        // flag, so the format is kept for those
        if literal.escaped {
            RegexLiteralKind::HexBrace(RegexHexLiteralKind::X)
        } else {
            RegexLiteralKind::Verbatim
        }
    } else {
        // escape non-printable characters
        if (literal.c as u32) < 32 && regex_syntax::is_escapeable_character(literal.c) {
            RegexLiteralKind::HexBrace(RegexHexLiteralKind::X)
        } else {
            RegexLiteralKind::Verbatim
        }
    };
    RegexLiteral {
        span: span(),
        kind,
        c: literal.c,
    }
}

// These are converted to a bracketed character class to remove the automatic UCP semantics.
fn convert_perl_class(class: &SdsPerlCharacterClass) -> RegexClassBracketed {
    match class {
        SdsPerlCharacterClass::Digit => perl_class_digit(false),
        SdsPerlCharacterClass::Space => perl_class_space(false),
        SdsPerlCharacterClass::Word => perl_class_word(false),
        SdsPerlCharacterClass::NonDigit => perl_class_digit(true),
        SdsPerlCharacterClass::NonSpace => perl_class_space(true),
        SdsPerlCharacterClass::NonWord => perl_class_word(true),
    }
}

fn perl_class_digit(negated: bool) -> RegexClassBracketed {
    RegexClassBracketed {
        // [0-9]
        span: span(),
        negated,
        kind: RegexClassSet::Item(RegexClassSetItem::Range(RegexClassSetRange {
            span: span(),
            start: convert_literal(
                Literal {
                    c: '0',
                    escaped: false,
                },
                LiteralKind::BracketedCharacterClass,
            ),
            end: convert_literal(
                Literal {
                    c: '9',
                    escaped: false,
                },
                LiteralKind::BracketedCharacterClass,
            ),
        })),
    }
}

fn perl_class_space(negated: bool) -> RegexClassBracketed {
    RegexClassBracketed {
        // [\r\n\t\f\v ]
        span: span(),
        negated,
        kind: RegexClassSet::Item(RegexClassSetItem::Union(RegexClassSetUnion {
            span: span(),
            items: vec![
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\r',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\n',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\t',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\x0C',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '\x0B',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: ' ',
                        escaped: true,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
            ],
        })),
    }
}

fn perl_class_word(negated: bool) -> RegexClassBracketed {
    RegexClassBracketed {
        // [a-zA-Z0-9_]
        span: span(),
        negated,
        kind: RegexClassSet::Item(RegexClassSetItem::Union(RegexClassSetUnion {
            span: span(),
            items: vec![
                RegexClassSetItem::Range(RegexClassSetRange {
                    span: span(),
                    start: convert_literal(
                        Literal {
                            c: 'a',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                    end: convert_literal(
                        Literal {
                            c: 'z',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                }),
                RegexClassSetItem::Range(RegexClassSetRange {
                    span: span(),
                    start: convert_literal(
                        Literal {
                            c: 'A',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                    end: convert_literal(
                        Literal {
                            c: 'Z',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                }),
                RegexClassSetItem::Range(RegexClassSetRange {
                    span: span(),
                    start: convert_literal(
                        Literal {
                            c: '0',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                    end: convert_literal(
                        Literal {
                            c: '9',
                            escaped: false,
                        },
                        LiteralKind::BracketedCharacterClass,
                    ),
                }),
                RegexClassSetItem::Literal(convert_literal(
                    Literal {
                        c: '_',
                        escaped: false,
                    },
                    LiteralKind::BracketedCharacterClass,
                )),
            ],
        })),
    }
}

fn convert_flags(flags: &SdsFlags) -> RegexFlags {
    let mut items = vec![];

    // Rust doesnt allow the same flag to show up twice. Adds and removes are deduplicated,
    // and if they are in both add and remove, the add is removed.

    let mut add_flags = flags.add.clone();
    add_flags.sort();
    add_flags.dedup();

    let mut remove_flags = flags.remove.clone();
    remove_flags.sort();
    remove_flags.dedup();

    let mut flags_in_both = vec![];

    for flag in &remove_flags {
        if add_flags.contains(flag) {
            flags_in_both.push(*flag);
        }
    }

    // If a flag is both added and removed, they are processed in order, so only the removal
    // is kept.
    for flag in flags_in_both {
        add_flags.retain(|x| *x != flag);
    }

    for flag in &add_flags {
        items.push(RegexFlagsItem {
            span: span(),
            kind: RegexFlagsItemKind::Flag(convert_flag(flag)),
        });
    }
    if !remove_flags.is_empty() {
        items.push(RegexFlagsItem {
            span: span(),
            kind: RegexFlagsItemKind::Negation,
        });

        for flag in &remove_flags {
            items.push(RegexFlagsItem {
                span: span(),
                kind: RegexFlagsItemKind::Flag(convert_flag(flag)),
            });
        }
    }

    RegexFlags {
        span: span(),
        items,
    }
}

fn convert_flag(flag: &SdsFlag) -> RegexFlag {
    match flag {
        SdsFlag::CaseInsensitive => RegexFlag::CaseInsensitive,
        SdsFlag::MultiLine => RegexFlag::MultiLine,
        SdsFlag::DotMatchesNewLine => RegexFlag::DotMatchesNewLine,
        SdsFlag::IgnoreWhitespace => RegexFlag::IgnoreWhitespace,
    }
}

#[cfg(test)]
mod test {
    use crate::normalization::rust_regex_adapter::{QUANTIFIER_LIMIT, convert_to_rust_regex};
    use crate::parser::error::ParseError;
    use crate::parser::unicode_property_names::UNICODE_PROPERTY_NAMES;
    use regex::Regex;
    use std::panic::catch_unwind;

    #[test]
    fn test_conversion() {
        // a list of inputs / expected outputs
        let test_cases = [
            // cases that are different
            ("\\a", "\\x{7}"),
            ("\\i", "i"),
            ("(?P<name>foo)", "(?<name>foo)"),
            ("(?'name'foo)", "(?<name>foo)"),
            ("x{,3}", "x\\{,3\\}"),
            ("{}", "\\{\\}"),
            ("\\Z", "\\x{A}?\\z"),
            ("[]]", "[\\]]"),
            ("[{}]", "[\\{\\}]"),
            ("[-]", "[\\-]"),
            ("[a-]", "[a\\-]"),
            ("[--a]", "[\\--a]"),
            ("\\d", "[0-9]"),
            ("\\w", "[a-zA-Z0-9_]"),
            ("\\s", "[\\x{D}\\x{A}\\x{9}\\x{C}\\x{B}\\x{20}]"),
            ("\\D", "[^0-9]"),
            ("\\W", "[^a-zA-Z0-9_]"),
            ("\\S", "[^\\x{D}\\x{A}\\x{9}\\x{C}\\x{B}\\x{20}]"),
            ("\\b", "(?-u:\\b)"),
            ("\\B", "(?-u:\\B)"),
            ("(?ii:foo)", "(?i:foo)"),
            ("(?-ii:foo)", "(?-i:foo)"),
            ("(?i-i:foo)", "(?-i:foo)"),
            ("\\<", "<"),
            ("\\>", ">"),
            ("[ ]", "[\\x{20}]"),
            ("[\\Qfoo\\E]", "[foo]"),
            ("[\\Q]\\E]", "[\\]]"),
            ("\\Qfoo\\E", "foo"),
            ("\\Q\\E", ""),
            ("\\Q([x])\\E", "\\(\\[x\\]\\)"),
            ("a\\Q|\\Eb", "a\\|b"),
            ("[a&&b]", "[a\\&\\&b]"),
            ("\\cA", "\\x{1}"),
            ("\\ca", "\\x{1}"),
            ("\\cZ", "\\x{1A}"),
            ("\\cz", "\\x{1A}"),
            ("[\\b]", "[\\x{8}]"),
            ("\\e", "\\x{1B}"),
            ("\\f", "\\x{C}"),
            ("\\n", "\\x{A}"),
            ("\\r", "\\x{D}"),
            ("\\t", "\\x{9}"),
            ("\\x", "\\x{0}"),
            ("\\x1", "\\x{1}"),
            ("\\x012", "\\x{1}2"),
            ("\\h", "[\\x{20}\\x{9}]"),
            ("\\H", "[^\\x{20}\\x{9}]"),
            ("\\v", "[\\x{B}\\x{A}\\x{C}\\x{D}]"),
            ("[\\v]", "[[\\x{B}\\x{A}\\x{C}\\x{D}]]"),
            ("[\\V]", "[[^\\x{B}\\x{A}\\x{C}\\x{D}]]"),
            ("\\V", "[^\\x{B}\\x{A}\\x{C}\\x{D}]"),
            ("[.]", "[\\.]"),
            ("[\\s]", "[[\\x{D}\\x{A}\\x{9}\\x{C}\\x{B}\\x{20}]]"),
            ("[\\habc]", "[[\\x{20}\\x{9}]abc]"),
            ("x{3}?", "x{3}?"),
            ("$", "\\x{A}?$"),
            // cases that are the same
            ("", ""),
            ("^", "^"),
            ("a b", "a b"),
            ("a", "a"),
            ("😏", "😏"),
            ("\\*", "\\*"),
            ("abc", "abc"),
            ("(?:abc)", "(?:abc)"),
            ("(?i:abc)", "(?i:abc)"),
            ("(?imsx:abc)", "(?imsx:abc)"),
            ("(a)", "(a)"),
            ("(a(b))", "(a(b))"),
            ("(?<name>foo)", "(?<name>foo)"),
            ("a|b|c", "a|b|c"),
            ("(?imsx)foo", "(?imsx)foo"),
            ("x+", "x+"),
            ("x*", "x*"),
            ("x?", "x?"),
            ("x+?", "x+?"),
            ("x*?", "x*?"),
            ("x??", "x??"),
            ("x{3}", "x{3}"),
            ("x{3,4}", "x{3,4}"),
            ("x{3,}", "x{3,}"),
            ("\\^", "\\^"),
            ("\\$", "\\$"),
            ("\\A", "\\A"),
            ("\\z", "\\z"),
            (".", "."),
            ("[x]", "[x]"),
            ("[^x]", "[^x]"),
            ("[a-z]", "[a-z]"),
            ("[[:alnum:]]", "[[:alnum:]]"),
            ("[[:alpha:]]", "[[:alpha:]]"),
            ("[[:ascii:]]", "[[:ascii:]]"),
            ("[[:blank:]]", "[[:blank:]]"),
            ("[[:cntrl:]]", "[[:cntrl:]]"),
            ("[[:digit:]]", "[[:digit:]]"),
            ("[[:graph:]]", "[[:graph:]]"),
            ("[[:lower:]]", "[[:lower:]]"),
            ("[[:print:]]", "[[:print:]]"),
            ("[[:punct:]]", "[[:punct:]]"),
            ("[[:space:]]", "[[:space:]]"),
            ("[[:upper:]]", "[[:upper:]]"),
            ("[[:word:]]", "[[:word:]]"),
            ("[[:xdigit:]]", "[[:xdigit:]]"),
            ("[[:^alnum:]]", "[[:^alnum:]]"),
            ("[[:^alpha:]]", "[[:^alpha:]]"),
            ("[[:^ascii:]]", "[[:^ascii:]]"),
            ("[[:^blank:]]", "[[:^blank:]]"),
            ("[[:^cntrl:]]", "[[:^cntrl:]]"),
            ("[[:^digit:]]", "[[:^digit:]]"),
            ("[[:^graph:]]", "[[:^graph:]]"),
            ("[[:^lower:]]", "[[:^lower:]]"),
            ("[[:^print:]]", "[[:^print:]]"),
            ("[[:^punct:]]", "[[:^punct:]]"),
            ("[[:^space:]]", "[[:^space:]]"),
            ("[[:^upper:]]", "[[:^upper:]]"),
            ("[[:^word:]]", "[[:^word:]]"),
            ("[[:^xdigit:]]", "[[:^xdigit:]]"),
            ("(?:x|)y", "(?:x|)y"),
        ];

        let mut dynamic_test_cases = vec![];

        // add all unicode categories to tests
        for property_name in UNICODE_PROPERTY_NAMES {
            let pattern = format!("\\p{{{property_name}}}");
            dynamic_test_cases.push((pattern.clone(), pattern));
        }

        for (input, expected_output) in test_cases
            .map(|(a, b)| (a.to_string(), b.to_string()))
            .into_iter()
            .chain(dynamic_test_cases)
        {
            let actual_output = match catch_unwind(|| convert_to_rust_regex(&input).unwrap()) {
                Ok(x) => x,
                Err(err) => {
                    println!("Input caused a panic: {input:?}");
                    panic!("{err:?}");
                }
            };

            if actual_output != expected_output {
                println!("  Actual bytes: {:?}", actual_output.clone().into_bytes());
                println!(
                    "Expected bytes: {:?}",
                    expected_output.to_string().into_bytes()
                );
                panic!(
                    "Conversion failed for input: {input}\n  Actual: {actual_output}\nExpected: {expected_output}"
                );
            }

            // ensure it's actually a valid regex
            Regex::new(&actual_output).unwrap();
        }
    }

    #[test]
    fn test_validation() {
        // exact repetition
        assert!(convert_to_rust_regex(&format!("x{{{QUANTIFIER_LIMIT}}}")).is_ok());
        assert_eq!(
            convert_to_rust_regex(&format!("x{{{}}}", QUANTIFIER_LIMIT + 1)),
            Err(ParseError::ExceededQuantifierLimit)
        );

        // range repetition (only the max needs to be tested since it must be larger than the min)
        assert!(
            convert_to_rust_regex(&format!(
                "x{{{},{}}}",
                QUANTIFIER_LIMIT - 1,
                QUANTIFIER_LIMIT
            ))
            .is_ok()
        );
        assert_eq!(
            convert_to_rust_regex(&format!(
                "x{{{},{}}}",
                QUANTIFIER_LIMIT,
                QUANTIFIER_LIMIT + 1
            )),
            Err(ParseError::ExceededQuantifierLimit)
        );

        // min range repetition
        assert!(convert_to_rust_regex(&format!("x{{{QUANTIFIER_LIMIT},}}")).is_ok());
        assert_eq!(
            convert_to_rust_regex(&format!("x{{{},}}", QUANTIFIER_LIMIT + 1)),
            Err(ParseError::ExceededQuantifierLimit)
        );
    }
}

#[derive(Copy, Clone, PartialEq, Debug)]
enum LiteralKind {
    Normal,
    BracketedCharacterClass,
}

// creates a dummy span (which isn't used here, but is required for the RegexAst)
fn span() -> Span {
    Span::new(Position::new(0, 0, 0), Position::new(0, 0, 0))
}