regex-anre 2.1.0

Regex-anre is a full-featured, zero-dependency regular expression engine that supports both standard and ANRE regular expressions.
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
// Copyright (c) 2025 Hemashushu <hippospark@gmail.com>, All rights reserved.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License version 2.0 and additional exceptions.
// For more details, see the LICENSE, LICENSE.additional, and CONTRIBUTING files.

use crate::{
    ast::{
        BackReference, CharRange, CharSet, CharSetElement, Expression, FunctionArgument,
        FunctionCall, FunctionName, Literal, PresetCharSetName, Program,
    },
    error::AnreError,
    peekable_iter::PeekableIter,
    range::Range,
};

use super::{
    lexer::lex_from_str,
    token::{Repetition, Token, TokenWithRange},
};

pub fn parse_from_str(s: &str) -> Result<Program, AnreError> {
    let tokens = lex_from_str(s)?;
    let mut token_iter = tokens.into_iter();
    let mut peekable_token_iter = PeekableIter::new(&mut token_iter);
    let mut parser = Parser::new(&mut peekable_token_iter);
    parser.parse_program()
}

pub struct Parser<'a> {
    upstream: &'a mut PeekableIter<'a, TokenWithRange>,

    /// Range of the most recently consumed token.
    pub last_range: Range,
}

impl<'a> Parser<'a> {
    fn new(upstream: &'a mut PeekableIter<'a, TokenWithRange>) -> Self {
        Self {
            upstream,
            last_range: Range::default(),
        }
    }

    fn next_token(&mut self) -> Option<Token> {
        match self.next_token_with_range() {
            Some(TokenWithRange { token, range }) => {
                self.last_range = range;
                Some(token)
            }
            None => None,
        }
    }

    fn next_token_with_range(&mut self) -> Option<TokenWithRange> {
        match self.upstream.next() {
            Some(token_with_range) => {
                self.last_range = token_with_range.range;
                Some(token_with_range)
            }
            None => None,
        }
    }

    fn peek_token(&self, offset: usize) -> Option<&Token> {
        match self.upstream.peek(offset) {
            Some(TokenWithRange { token, .. }) => Some(token),
            None => None,
        }
    }

    fn peek_range(&self, offset: usize) -> Option<&Range> {
        match self.upstream.peek(offset) {
            Some(TokenWithRange { range, .. }) => Some(range),
            None => None,
        }
    }

    // Consumes one token and requires it to match `expected_token`.
    fn consume_token_and_assert(
        &mut self,
        expected_token: &Token,
        token_description: &str,
    ) -> Result<(), AnreError> {
        match self.next_token() {
            Some(token) => {
                if &token == expected_token {
                    Ok(())
                } else {
                    Err(AnreError::MessageWithRange(
                        format!("Expected token: {}.", token_description),
                        self.last_range,
                    ))
                }
            }
            None => Err(AnreError::UnexpectedEndOfDocument(format!(
                "Expected token: {}.",
                token_description
            ))),
        }
    }

    // Consumes `]`.
    fn consume_closing_bracket(&mut self) -> Result<(), AnreError> {
        self.consume_token_and_assert(&Token::CharSetEnd, "closing bracket")
    }

    // Consumes `)`.
    fn consume_closing_parenthese(&mut self) -> Result<(), AnreError> {
        self.consume_token_and_assert(&Token::GroupEnd, "closing parenthese \")\"")
    }
}

impl Parser<'_> {
    pub fn parse_program(&mut self) -> Result<Program, AnreError> {
        let expression = self.parse_expression()?;

        if self.peek_token(0).is_some() {
            return Err(AnreError::MessageWithRange(
                "Only one top-level expression is allowed. Wrap multiple expressions in a group."
                    .to_owned(),
                *self.peek_range(0).unwrap(),
            ));
        }

        Ok(Program { expression })
    }

    fn parse_expression(&mut self) -> Result<Expression, AnreError> {
        // ```diagram
        // token ...
        // -----
        // ^
        // |__ current, None or Some(...)
        // ```

        self.parse_logic_or()
    }

    fn parse_logic_or(&mut self) -> Result<Expression, AnreError> {
        // ```diagram
        // expression || expression
        // ```

        let mut left = self.parse_consequent_expression()?;

        // In the traditional regular expressions, "groups" are implied
        // on both sides of the "logic or" operator ("|").
        //
        // The | operator has the lowest precedence in a regular expression.
        // If you want to use a disjunction as a part of a bigger pattern,
        // you must group it.
        //
        // For example:
        //
        // "ab|cd" == "(ab)|(cd)" != "a(b|c)d"
        //
        // ref:
        // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Disjunction
        while let Some(Token::LogicOr) = self.peek_token(0) {
            self.next_token(); // consume "||"

            // Operator associativity:
            //
            // - https://en.wikipedia.org/wiki/Operator_associativity
            // - https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
            //
            // Representation:
            // - left-associative (left-to-right associative)
            //   `a || b || c -> (a || b) || c`
            // - right-associative (right-to-left associative)
            //   `a || b || c -> a || (b || c)`
            //
            // Call `parse_expression` for right-to-left associative parsing, for example:
            // `let right = self.parse_expression()?;`
            // Or call `parse_consequent_expression` for left-to-right associative parsing, for example:
            // `let right = self.parse_consequent_expression()?;`
            //
            // currently right-associative is adopted for efficiency.

            let right = self.parse_expression()?;
            let expression = Expression::Or(Box::new(left), Box::new(right));
            left = expression;
        }

        Ok(left)
    }

    fn parse_consequent_expression(&mut self) -> Result<Expression, AnreError> {
        // ```diagram
        // token ...
        // -----
        // ^
        // | current, None or Some(...)
        // ```

        // In the traditional regular expressions, consecutive expressions
        // are implicit groups.
        //
        // For example:
        //
        // - `abc` = `('a', 'b', 'c')`
        // - `0[xX][0-9a-fA-F]+` = `('0', ['x', 'X'], one_or_more(['0'..'9', 'a'..'f', 'A'..'F']))`
        // - `ab|cd` = `('a', 'b') || ('c', 'd')`

        let mut expressions = vec![];
        while let Some(token) = self.peek_token(0) {
            match token {
                // collect expressions until the next token is a logic or operator
                // or a group end, which cannot be implicitly grouped with
                // the previous expressions.
                Token::LogicOr | Token::GroupEnd => {
                    break;
                }
                _ => {
                    let expression = self.parse_look_ahead_assertion()?;
                    expressions.push(expression);
                }
            }
        }

        if expressions.is_empty() {
            return Err(AnreError::MessageWithRange(
                "Encountered an empty expression.".to_owned(),
                self.last_range,
            ));
        }

        // Merge continuous `Literal::Char` to `Literal::String`
        let mut cursor = expressions.len() - 1;
        while cursor > 0 {
            if matches!(expressions[cursor], Expression::Literal(Literal::Char(_))) {
                // check previous expressions until the first non-`Literal::Char` expression
                let mut begin_index = cursor;
                while begin_index > 0 {
                    if !matches!(
                        expressions[begin_index - 1],
                        Expression::Literal(Literal::Char(_))
                    ) {
                        break;
                    }
                    begin_index -= 1;
                }

                // found continuous chars
                if cursor - begin_index > 0 {
                    let s: String = expressions
                        .drain(begin_index..=cursor)
                        .map(|item| {
                            if let Expression::Literal(Literal::Char(c)) = item {
                                c
                            } else {
                                unreachable!()
                            }
                        })
                        .collect();
                    expressions.insert(begin_index, Expression::Literal(Literal::String(s)));

                    if begin_index == 0 {
                        // reached the beginning of the expression list
                        break;
                    } else {
                        // search for the next continuous chars
                        cursor = begin_index - 1;
                    }
                } else {
                    cursor -= 1;
                }
            } else {
                cursor -= 1;
            }
        }

        // escape the group if it contains only one element
        if expressions.len() == 1 {
            let expression = expressions.remove(0);
            Ok(expression)
        } else {
            // create an implicit group
            Ok(Expression::Group(expressions))
        }
    }

    fn parse_look_ahead_assertion(&mut self) -> Result<Expression, AnreError> {
        // look around assertions:
        // - `(?=...)`  Positive lookahead
        // - `(?!...)`  Negative lookahead
        //
        // For example:
        //
        // `a(?=b)` = `is_before('a', 'b')`, `'a'.is_before('b')`
        //
        // means:
        //
        // "match 'a' only if it's followed by 'b'"

        let mut expression = self.parse_notation()?;

        if let Some(token @ (Token::LookAheadGroupStart | Token::LookAheadNegativeGroupStart)) =
            self.peek_token(0)
        {
            let name = match token {
                Token::LookAheadGroupStart => FunctionName::IsBefore,
                Token::LookAheadNegativeGroupStart => FunctionName::IsNotBefore,
                _ => unreachable!(),
            };

            let mut args = vec![];

            self.next_token(); // consume "(?=" or "(?!"
            let arg0 = self.parse_expression()?;
            self.consume_closing_parenthese()?; // consume ")"

            args.push(FunctionArgument::Expression(expression));
            args.push(FunctionArgument::Expression(arg0));

            let function_call = FunctionCall { name, args };
            expression = Expression::FunctionCall(Box::new(function_call));
        }

        Ok(expression)
    }

    fn parse_notation(&mut self) -> Result<Expression, AnreError> {
        // ```diagram
        // expression [ "?" | "+" | "*" | "{N}" | "{N,}" | "{N,M}" ]
        // expression [ "??" | "+?" | "*?" | "{N}?" | "{N,}?" | "{N,M}?" ]
        // ```
        let mut expression = self.parse_primary_expression()?;

        while let Some(token) = self.peek_token(0) {
            match token {
                Token::Optional
                | Token::LazyOptional
                | Token::OneOrMore
                | Token::LazyOneOrMore
                | Token::ZeroOrMore
                | Token::LazyZeroOrMore => {
                    // quantifier, for example: `foo?`, `foo+`, `foo*`, `foo??`, `foo+?`, `foo*?`, etc.

                    let name = match token {
                        // Greedy quantifier
                        Token::Optional => FunctionName::Optional,
                        Token::OneOrMore => FunctionName::OneOrMore,
                        Token::ZeroOrMore => FunctionName::ZeroOrMore,
                        // Lazy quantifier
                        Token::LazyOptional => FunctionName::LazyOptional,
                        Token::LazyOneOrMore => FunctionName::LazyOneOrMore,
                        Token::LazyZeroOrMore => FunctionName::LazyZeroOrMore,
                        _ => unreachable!(),
                    };

                    let function_call = FunctionCall {
                        name,
                        args: vec![FunctionArgument::Expression(expression)],
                    };
                    expression = Expression::FunctionCall(Box::new(function_call));

                    self.next_token(); // consume notation
                }
                Token::Repetition(repetition, lazy) => {
                    // repetition, for example `foo{3}`, `foo{3,}`, `foo{3,5}`, `foo{3}?`, `foo{3,}?`, `foo{3,5}?`, etc.

                    let mut args = vec![];
                    args.push(FunctionArgument::Expression(expression));

                    let name = match repetition {
                        Repetition::Repeat(n) => {
                            args.push(FunctionArgument::Number(*n));

                            if *lazy {
                                FunctionName::LazyRepeat
                            } else {
                                FunctionName::Repeat
                            }
                        }
                        Repetition::RepeatFrom(n) => {
                            args.push(FunctionArgument::Number(*n));

                            if *lazy {
                                FunctionName::LazyRepeatFrom
                            } else {
                                FunctionName::RepeatFrom
                            }
                        }
                        Repetition::RepeatRange(m, n) => {
                            // `{m..m}` is equivalent to a fixed repetition, so it reuses
                            // the same AST form as `{m}`.
                            if m > n {
                                let range = self.peek_range(0).unwrap();
                                return Err(AnreError::MessageWithRange(
                                    format!(
                                        "Invalid repetition range: {{{},{}}}. The start of the range must be less than or equal to the end.",
                                        m, n
                                    ),
                                    *range,
                                ));
                            }

                            args.push(FunctionArgument::Number(*m));
                            args.push(FunctionArgument::Number(*n));

                            if *lazy {
                                FunctionName::LazyRepeatRange
                            } else {
                                FunctionName::RepeatRange
                            }
                        }
                    };

                    let function_call = FunctionCall { name, args };
                    expression = Expression::FunctionCall(Box::new(function_call));

                    self.next_token(); // consume notation
                }

                _ => {
                    break;
                }
            }
        }

        Ok(expression)
    }

    fn parse_primary_expression(&mut self) -> Result<Expression, AnreError> {
        // primary expressions:
        // - literal
        // - line assertion
        // - boundary assertion
        // - look around assertion
        // - explicit group
        // - back reference
        let expression = match self.peek_token(0).unwrap() {
            Token::LineBoundaryAssertionStart => {
                self.next_token(); // consume '^'
                Expression::FunctionCall(Box::new(FunctionCall {
                    name: FunctionName::IsStart,
                    args: vec![],
                }))
            }
            Token::LineBoundaryAssertionEnd => {
                self.next_token(); // consume '$'
                Expression::FunctionCall(Box::new(FunctionCall {
                    name: FunctionName::IsEnd,
                    args: vec![],
                }))
            }
            Token::WordBoundaryAssertion(b) => {
                let negative = *b;
                self.next_token(); // consume boundary assertion

                if negative {
                    Expression::FunctionCall(Box::new(FunctionCall {
                        name: FunctionName::IsNotBound,
                        args: vec![],
                    }))
                } else {
                    Expression::FunctionCall(Box::new(FunctionCall {
                        name: FunctionName::IsBound,
                        args: vec![],
                    }))
                }
            }
            token @ (Token::LookBehindGroupStart | Token::LookBehindNegativeGroupStart) => {
                // look around assertions:
                // - `(?<=...)` Positive lookbehind
                // - `(?<!...)` Negative lookbehind
                //
                // For example:
                //
                // `(?<=a)b` = `is_after('b', 'a')`, `'b'.is_after('a')`
                //
                // means:
                //
                // "match 'b' only if it's preceded by 'a'"
                let name = match token {
                    Token::LookBehindGroupStart => FunctionName::IsAfter,
                    Token::LookBehindNegativeGroupStart => FunctionName::IsNotAfter,
                    _ => unreachable!(),
                };

                let mut args = vec![];

                self.next_token(); // consume "(?<=" or "(?<!"
                let arg0 = self.parse_expression()?;
                self.consume_closing_parenthese()?; // consume ")"

                let expression = self.parse_expression()?;
                args.push(FunctionArgument::Expression(expression));
                args.push(FunctionArgument::Expression(arg0));

                let function_call = FunctionCall { name, args };
                Expression::FunctionCall(Box::new(function_call))
            }
            Token::GroupStart => {
                // generic group (indexed capture group)
                // `( expression... )`
                self.next_token().unwrap(); // consume "("
                let expression = self.parse_expression()?;
                self.consume_closing_parenthese()?; // consume ")"

                let function_call = FunctionCall {
                    name: FunctionName::Index,
                    args: vec![FunctionArgument::Expression(expression)],
                };
                Expression::FunctionCall(Box::new(function_call))
            }
            Token::NonCaptureGroupStart => {
                // non-capturing group
                // `(?: expression )`
                self.next_token().unwrap(); // consume "("
                let expression = self.parse_expression()?;
                self.consume_closing_parenthese()?; // consume ")"

                expression
            }
            Token::NamedCaptureGroupStart(name_ref) => {
                // named capturing group
                // `(?<...> expression )`
                let name = name_ref.to_owned();
                self.next_token().unwrap(); // consume "("
                let expression = self.parse_expression()?;
                self.consume_closing_parenthese()?; // consume ")"

                let function_call = FunctionCall {
                    name: FunctionName::Name,
                    args: vec![
                        FunctionArgument::Expression(expression),
                        FunctionArgument::Identifier(name),
                    ],
                };
                Expression::FunctionCall(Box::new(function_call))
            }
            Token::BackReferenceNumber(index_ref) => {
                let index = *index_ref;
                self.next_token(); // consume '\num'
                Expression::BackReference(BackReference::Index(index))
            }
            Token::BackReferenceName(name_ref) => {
                let name = name_ref.to_owned();
                self.next_token(); // consume '\k<name>'
                Expression::BackReference(BackReference::Name(name))
            }
            _ => {
                let literal = self.parse_literal()?;
                Expression::Literal(literal)
            }
        };

        Ok(expression)
    }

    fn parse_literal(&mut self) -> Result<Literal, AnreError> {
        // literals:
        // - `.` (any character)
        // - char
        // - charset
        // - preset charset

        let literal = match self.peek_token(0).unwrap() {
            Token::CharSetStart | Token::CharSetStartNegative => {
                let charset = self.parse_charset()?;
                Literal::CharSet(charset)
            }
            Token::Char(char_ref) => {
                let c = *char_ref;
                self.next_token(); // consume char
                Literal::Char(c)
            }
            Token::PresetCharSet(preset_charset_name_ref) => {
                let preset_charset_name =
                    PresetCharSetName::try_from(*preset_charset_name_ref).unwrap();
                self.next_token(); // consume preset charset
                Literal::PresetCharSet(preset_charset_name)
            }
            Token::Dot => {
                self.next_token(); // consume special char
                Literal::AnyChar
            }
            _ => {
                return Err(AnreError::MessageWithRange(
                    "Expected a literal.".to_owned(),
                    self.last_range,
                ));
            }
        };

        Ok(literal)
    }

    fn parse_charset(&mut self) -> Result<CharSet, AnreError> {
        // ```diagram
        // [ {^} {char | char_range | preset_charset} ] ?
        // -                                            -
        // ^                                            ^__ to here
        // |__ current, validated
        // ```

        let opening_token = self.next_token().unwrap(); // consume '[' or '[^'
        let negative = matches!(opening_token, Token::CharSetStartNegative);

        let mut elements = vec![];
        while let Some(token) = self.peek_token(0) {
            if token == &Token::CharSetEnd {
                break;
            }

            match token {
                Token::Char(c_ref) => {
                    // char
                    let c = *c_ref;
                    self.next_token(); // consume char
                    elements.push(CharSetElement::Char(c));
                }
                Token::CharRange(from, to) => {
                    // character range
                    let char_range = CharRange {
                        start: *from,
                        end_inclusive: *to,
                    };
                    self.next_token(); // consume char range
                    elements.push(CharSetElement::CharRange(char_range));
                }
                Token::PresetCharSet(preset_charset_name_ref) => {
                    // preset charset
                    let preset_charset_name =
                        PresetCharSetName::try_from(*preset_charset_name_ref).unwrap();
                    self.next_token(); // consume preset charset
                    elements.push(CharSetElement::PresetCharSet(preset_charset_name));
                }
                _ => {
                    return Err(AnreError::MessageWithRange(
                        "Unsupported character set element.".to_owned(),
                        *self.peek_range(0).unwrap(),
                    ));
                }
            }
        }

        self.consume_closing_bracket()?;

        let charset = CharSet { negative, elements };

        Ok(charset)
    }
}

impl TryFrom<char> for PresetCharSetName {
    type Error = ();

    fn try_from(value: char) -> Result<Self, Self::Error> {
        match value {
            'w' => Ok(PresetCharSetName::CharWord),
            'W' => Ok(PresetCharSetName::CharNotWord),
            's' => Ok(PresetCharSetName::CharSpace),
            'S' => Ok(PresetCharSetName::CharNotSpace),
            'd' => Ok(PresetCharSetName::CharDigit),
            'D' => Ok(PresetCharSetName::CharNotDigit),
            _ => Err(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use crate::{
        ast::{
            CharRange, CharSet, CharSetElement, Expression, Literal, PresetCharSetName, Program,
        },
        error::AnreError,
        position::Position,
        range::Range,
    };

    use super::parse_from_str;

    #[test]
    fn test_parse_literal() {
        {
            let program = parse_from_str(r#"a"#).unwrap();

            assert_eq!(
                program,
                Program {
                    expression: Expression::Literal(Literal::Char('a')),
                }
            );

            assert_eq!(program.to_string(), r#"'a'"#);
        }

        // Merge continuous chars.
        {
            let program = parse_from_str(r#".foo"#).unwrap();

            assert_eq!(
                program,
                Program {
                    expression: Expression::Group(vec![
                        Expression::Literal(Literal::AnyChar),
                        Expression::Literal(Literal::String("foo".to_owned())),
                    ])
                }
            );

            assert_eq!(program.to_string(), r#"(char_any, "foo")"#);
        }
    }

    #[test]
    fn test_parse_literal_preset_charset() {
        let program = parse_from_str(r#"\w\W\d\D\s\S"#).unwrap();

        assert_eq!(
            program,
            Program {
                expression: Expression::Group(vec![
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharWord)),
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharNotWord)),
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharDigit)),
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharNotDigit)),
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharSpace)),
                    Expression::Literal(Literal::PresetCharSet(PresetCharSetName::CharNotSpace)),
                ])
            }
        );

        assert_eq!(
            program.to_string(),
            r#"(char_word, char_not_word, char_digit, char_not_digit, char_space, char_not_space)"#
        );
    }

    #[test]
    fn test_parse_literal_charset() {
        let program = parse_from_str(r#"[a0-9\w]"#).unwrap();

        assert_eq!(
            program,
            Program {
                expression: Expression::Literal(Literal::CharSet(CharSet {
                    negative: false,
                    elements: vec![
                        CharSetElement::Char('a'),
                        CharSetElement::CharRange(CharRange {
                            start: '0',
                            end_inclusive: '9'
                        }),
                        CharSetElement::PresetCharSet(PresetCharSetName::CharWord),
                    ]
                }))
            }
        );

        assert_eq!(program.to_string(), r#"['a', '0'..'9', char_word]"#);

        // negative charset
        assert_eq!(
            parse_from_str(r#"[^a-z\s]"#,).unwrap().to_string(),
            r#"!['a'..'z', char_space]"#
        );

        assert_eq!(
            parse_from_str(r#"[-a-f0-9]"#,).unwrap().to_string(),
            r#"['-', 'a'..'f', '0'..'9']"#
        );
    }

    #[test]
    fn test_parse_quantifier() {
        assert_eq!(
            parse_from_str(r#"a?b+c*x??y+?z*?"#,).unwrap().to_string(),
            r#"(optional('a'), one_or_more('b'), zero_or_more('c'), lazy_optional('x'), lazy_one_or_more('y'), lazy_zero_or_more('z'))"#
        );
    }

    #[test]
    fn test_parse_repetition() {
        assert_eq!(
            parse_from_str(r#"a{3}b{5,7}c{11,}x{3}?y{5,7}?z{11,}?"#,)
                .unwrap()
                .to_string(),
            r#"(repeat('a', 3), repeat_range('b', 5, 7), repeat_from('c', 11), lazy_repeat('x', 3), lazy_repeat_range('y', 5, 7), lazy_repeat_from('z', 11))"#
        );

        // Special case: zero repetition and range with the same start and end
        assert_eq!(
            parse_from_str(r#"a{0}b{3,3}c{5,5}?"#,).unwrap().to_string(),
            r#"(repeat('a', 0), repeat_range('b', 3, 3), lazy_repeat_range('c', 5, 5))"#
        );

        // err: invalid repetition range, `{m,n}` is invalid if m > n
        assert!(matches!(
            parse_from_str(r#"a{5,3}"#,),
            Err(AnreError::MessageWithRange(
                _,
                Range {
                    start: Position {
                        index: 1,
                        line: 0,
                        column: 1
                    },
                    end_inclusive: Position {
                        index: 5,
                        line: 0,
                        column: 5
                    }
                }
            ))
        ));
    }

    #[test]
    fn test_parse_line_assertions() {
        assert_eq!(
            parse_from_str(r#"^a$"#,).unwrap().to_string(),
            r#"(is_start(), 'a', is_end())"#
        );
    }

    #[test]
    fn test_parse_boundary_assertions() {
        assert_eq!(
            parse_from_str(r#"\ba\B"#,).unwrap().to_string(),
            r#"(is_bound(), 'a', is_not_bound())"#
        );
    }

    #[test]
    fn test_parse_index_capture_and_backreference() {
        assert_eq!(
            parse_from_str(r#"(a)"#,).unwrap().to_string(),
            r#"index('a')"#
        );

        assert_eq!(
            parse_from_str(r#"(a\d)"#,).unwrap().to_string(),
            r#"index(('a', char_digit))"#
        );

        assert_eq!(
            parse_from_str(r#"(\d+)\.\1"#,).unwrap().to_string(),
            r#"(index(one_or_more(char_digit)), '.', ^1)"#
        );
    }

    #[test]
    fn test_parse_name_capture_and_backreference() {
        assert_eq!(
            parse_from_str(r#"(?<x>a)"#,).unwrap().to_string(),
            r#"name('a', x)"#
        );

        assert_eq!(
            parse_from_str(r#"(?<x>a\d)"#,).unwrap().to_string(),
            r#"name(('a', char_digit), x)"#
        );

        assert_eq!(
            parse_from_str(r#"(?<a>\d)x\k<a>"#,).unwrap().to_string(),
            r#"(name(char_digit, a), 'x', a)"#
        );
    }

    #[test]
    fn test_parse_logic_or() {
        {
            let program = parse_from_str(r#"a|b"#).unwrap();

            assert_eq!(
                program,
                Program {
                    expression: Expression::Or(
                        Box::new(Expression::Literal(Literal::Char('a'))),
                        Box::new(Expression::Literal(Literal::Char('b'))),
                    )
                }
            );

            assert_eq!(program.to_string(), r#"'a' || 'b'"#);
        }

        // multiple operands
        {
            let program = parse_from_str(r#"a|b|c"#).unwrap();

            assert_eq!(
                program,
                Program {
                    expression: Expression::Or(
                        Box::new(Expression::Literal(Literal::Char('a'))),
                        Box::new(Expression::Or(
                            Box::new(Expression::Literal(Literal::Char('b'))),
                            Box::new(Expression::Literal(Literal::Char('c'))),
                        )),
                    )
                }
            );

            assert_eq!(program.to_string(), r#"'a' || ('b' || 'c')"#);
        }

        // expression as operand
        assert_eq!(
            parse_from_str(r#"\d+|[\w-]+"#,).unwrap().to_string(),
            r#"one_or_more(char_digit) || one_or_more([char_word, '-'])"#
        );

        // group + logic or
        assert_eq!(
            parse_from_str(r#"(a|b)|c"#,).unwrap().to_string(),
            r#"index('a' || 'b') || 'c'"#
        );

        // group + logic or + group
        assert_eq!(
            parse_from_str(r#"(a\w)|(b\d)"#,).unwrap().to_string(),
            r#"index(('a', char_word)) || index(('b', char_digit))"#
        );

        // string + logic or
        assert_eq!(
            parse_from_str(r#"ab|cd"#,).unwrap().to_string(),
            r#""ab" || "cd""#
        );
    }

    #[test]
    fn test_parse_group() {
        // groups are indexed capturing groups by default

        assert_eq!(
            parse_from_str(r#"(?:foo\d)(?:b(?:bar\d))$"#,)
                .unwrap()
                .to_string(),
            r#"(("foo", char_digit), ('b', ("bar", char_digit)), is_end())"#
        );

        // nested groups
        assert_eq!(
            parse_from_str(r#"(?:foo\d){3}(?:b(?:bar){5})$"#,)
                .unwrap()
                .to_string(),
            r#"(repeat(("foo", char_digit), 3), ('b', repeat("bar", 5)), is_end())"#
        );

        // escape nested groups
        assert_eq!(
            parse_from_str(r#"(?:(?:a\db))"#,).unwrap().to_string(),
            r#"('a', char_digit, 'b')"#
        );
    }

    #[test]
    fn test_parse_operate_on_group() {
        // Quantifier on group
        assert_eq!(
            parse_from_str(r#"a(?:b)?(?:x\d)?"#,).unwrap().to_string(),
            r#"('a', optional('b'), optional(('x', char_digit)))"#
        );

        // Repetition on group
        assert_eq!(
            parse_from_str(r#"a(?:b){3,5}(?:x\d){7,11}"#,)
                .unwrap()
                .to_string(),
            r#"('a', repeat_range('b', 3, 5), repeat_range(('x', char_digit), 7, 11))"#
        );

        // Quantifier on indexed capturing group
        assert_eq!(
            parse_from_str(r#"a(b?)((?:x\d)?)"#,).unwrap().to_string(),
            r#"('a', index(optional('b')), index(optional(('x', char_digit))))"#
        );

        // Repetition on indexed capturing group
        assert_eq!(
            parse_from_str(r#"a(b{3,5})((?:x\d){7,11})"#,)
                .unwrap()
                .to_string(),
            r#"('a', index(repeat_range('b', 3, 5)), index(repeat_range(('x', char_digit), 7, 11)))"#
        );

        // Quantifier on named capturing group
        assert_eq!(
            parse_from_str(r#"a(?<foo>b)?(?<bar>x\d)?"#,)
                .unwrap()
                .to_string(),
            r#"('a', optional(name('b', foo)), optional(name(('x', char_digit), bar)))"#
        );

        // Repetition on named capturing group
        assert_eq!(
            parse_from_str(r#"a(?<foo>b){3,5}(?<bar>x\d){7,11}"#,)
                .unwrap()
                .to_string(),
            r#"('a', repeat_range(name('b', foo), 3, 5), repeat_range(name(('x', char_digit), bar), 7, 11))"#
        );

        // Precedence of group and quantifier
        assert_eq!(
            parse_from_str(r#"(x)?(y?)"#,).unwrap().to_string(),
            r#"(optional(index('x')), index(optional('y')))"#
        );
    }

    #[test]
    fn test_parse_lookaround_assertion() {
        assert_eq!(
            parse_from_str(r#"(?<=a)b"#,).unwrap().to_string(),
            r#"is_after('b', 'a')"#
        );

        assert_eq!(
            parse_from_str(r#"(?<!a)b"#,).unwrap().to_string(),
            r#"is_not_after('b', 'a')"#
        );

        assert_eq!(
            parse_from_str(r#"a(?=b)"#,).unwrap().to_string(),
            r#"is_before('a', 'b')"#
        );

        assert_eq!(
            parse_from_str(r#"a(?!b)"#,).unwrap().to_string(),
            r#"is_not_before('a', 'b')"#
        );
    }

    #[test]
    fn test_parse_examples() {
        assert_eq!(
            parse_from_str(r#"\d+"#,).unwrap().to_string(),
            "one_or_more(char_digit)"
        );

        assert_eq!(
            parse_from_str(r#"0x[0-9a-fA-F]+"#,).unwrap().to_string(),
            "(\"0x\", one_or_more(['0'..'9', 'a'..'f', 'A'..'F']))"
        );

        assert_eq!(
            parse_from_str(r#"^[\w.-]+(\+[\w-]+)?@([a-zA-Z0-9-]+\.)+[a-z]{2,}$"#,)
                .unwrap()
                .to_string(),
            "(is_start(), \
one_or_more([char_word, '.', '-']), \
optional(index(('+', one_or_more([char_word, '-'])))), \
'@', \
one_or_more(index((one_or_more(['a'..'z', 'A'..'Z', '0'..'9', '-']), '.'))), \
repeat_from(['a'..'z'], 2), \
is_end())"
        );

        let ipv4_regex = parse_from_str(
            r#"^((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$"#,
        )
        .unwrap()
        .to_string();

        let expected_part_str = r#"index(("25", ['0'..'5']) || (('2', ['0'..'4'], char_digit) || (('1', char_digit, char_digit) || ((['1'..'9'], char_digit) || char_digit))))"#;
        let expected_str = format!(
            "(is_start(), repeat(index(({}, '.')), 3), {}, is_end())",
            expected_part_str, expected_part_str
        );

        assert_eq!(ipv4_regex, expected_str);

        assert_eq!(
            parse_from_str(r#"<(?<tag_name>\w+)(\s\w+(="\w+")?)*>.+?</\k<tag_name>>"#,)
                .unwrap()
                .to_string(),
            "(\
'<', \
name(one_or_more(char_word), tag_name), \
zero_or_more(index((char_space, one_or_more(char_word), optional(index((\"=\\\"\", one_or_more(char_word), '\\\"')))))), \
'>', \
lazy_one_or_more(char_any), \
\"</\", tag_name, '>'\
)"
        );
    }
}