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
use std::{collections::HashMap, fmt, ops::RangeInclusive};

use regex::internal::Inst;
pub use regex::internal::{Compiler, Program};

#[derive(Clone)]
pub struct StepCase {
    pub char_range: RangeInclusive<u8>,
    pub next_case: CasePattern,
}

impl fmt::Debug for StepCase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?} => {:?}", self.char_range, self.next_case)
    }
}

#[derive(Clone)]
pub enum CasePattern {
    Step(usize),
    Match(usize),
}

impl fmt::Debug for CasePattern {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Step(next_step) => write!(f, "step = {next_step}"),
            Self::Match(match_index) => write!(f, "return {match_index}"),
        }
    }
}

/// A state machine that can be used to match a regex.
#[derive(Default)]
pub struct StateMachine {
    /// The next step to be added to the state machine.
    next_step: usize,
    /// A map of split instructions to the step they should skip.
    split_skips: HashMap<usize, usize>,
    /// A map of steps to the pattern index they should return.
    end_patterns: HashMap<usize, usize>,
}

impl StateMachine {
    /// Parse a regex program into a state machine.
    ///
    /// # Panics
    ///
    /// Panics if the program contains a [`Inst::EmptyLook`] instruction.
    pub fn parse_inst(
        &mut self,
        position: usize,
        program: &Program,
    ) -> Vec<(usize, Vec<StepCase>)> {
        match &program[position] {
            Inst::Split(inst_split) => {
                let goto1 = program.skip(inst_split.goto1);
                let goto2 = program.skip(inst_split.goto2);

                let current_step = self.next_step;

                if let Inst::Match(match_index) = program[goto1] {
                    self.end_patterns.insert(current_step, match_index);
                }

                if let Inst::Match(match_index) = program[goto2] {
                    self.end_patterns.insert(current_step, match_index);
                }

                if self.split_skips.contains_key(&position) {
                    return vec![];
                }

                let mut first_steps = Vec::new();
                let mut steps = Vec::new();

                self.split_skips.insert(position, current_step);

                let mut steps1 = self.parse_inst(goto1, program).into_iter();

                self.split_skips.remove(&position);

                if let Some((_, step_cases)) = steps1.next() {
                    first_steps.extend(step_cases);

                    steps.extend(steps1);
                }

                self.split_skips.insert(position, current_step);

                let mut steps2 = self.parse_inst(goto2, program).into_iter();

                self.split_skips.remove(&position);

                if let Some((_, step_cases)) = steps2.next() {
                    first_steps.extend(step_cases);

                    steps.extend(steps2);
                }

                steps.insert(0, (current_step, first_steps.clone()));

                steps
            }
            Inst::Match(..) => vec![],
            Inst::Save(..) => unreachable!(),
            Inst::EmptyLook(..) => todo!(),
            Inst::Char(inst_char) => {
                let mut steps = vec![(
                    self.next_step,
                    vec![StepCase {
                        char_range: (inst_char.c as u8)..=(inst_char.c as u8),
                        next_case: self.next_case(inst_char.goto, program),
                    }],
                )];

                steps.extend(self.parse_inst(program.skip(inst_char.goto), program));

                steps
            }
            Inst::Ranges(inst_ranges) => {
                let mut steps = vec![(self.next_step, Vec::new())];

                for (start, end) in inst_ranges.ranges.iter() {
                    let (_, step_cases) = &mut steps[0];

                    step_cases.push(StepCase {
                        char_range: (*start as u8)..=(*end as u8),
                        next_case: self.next_case(inst_ranges.goto, program),
                    });

                    steps.extend(self.parse_inst(program.skip(inst_ranges.goto), program));
                }

                steps
            }
            Inst::Bytes(inst_bytes) => {
                let mut steps = vec![(
                    self.next_step,
                    vec![StepCase {
                        char_range: inst_bytes.start..=inst_bytes.end,
                        next_case: self.next_case(inst_bytes.goto, program),
                    }],
                )];

                steps.extend(self.parse_inst(program.skip(inst_bytes.goto), program));

                steps
            }
        }
    }

    fn next_case(&mut self, goto: usize, program: &Program) -> CasePattern {
        let goto = program.skip(goto);

        if let Inst::Match(match_index) = program[goto] {
            CasePattern::Match(match_index)
        } else {
            if let Some(next_step) = self.split_skips.get(&goto) {
                return CasePattern::Step(*next_step);
            }

            self.next_step += 1;

            CasePattern::Step(self.next_step)
        }
    }

    #[must_use]
    pub fn end_patterns(self) -> HashMap<usize, usize> {
        self.end_patterns
    }

    pub fn extend_steps(
        &mut self,
        steps: Vec<(usize, Vec<StepCase>)>,
    ) -> Vec<(usize, Vec<StepCase>)> {
        let mut steps1 = steps.clone();
        let steps2 = steps.clone();

        for (step, mut step_cases) in steps {
            extend_split_steps(step, &mut step_cases, &mut steps1, &steps2);
        }

        steps1
    }
}

fn extend_split_steps(
    step: usize,
    first_steps: &mut [StepCase],
    steps1: &mut [(usize, Vec<StepCase>)],
    steps2: &[(usize, Vec<StepCase>)],
) {
    for (i, step_case1) in first_steps.iter().enumerate() {
        for step_case2 in first_steps.iter().skip(i + 1) {
            let overlaps = step_case1
                .char_range
                .clone()
                .any(|c1| step_case2.char_range.clone().any(|c2| c1 == c2));

            if overlaps {
                if let (CasePattern::Step(next_step1), CasePattern::Step(next_step2)) =
                    (&step_case1.next_case, &step_case2.next_case)
                {
                    if next_step1 != next_step2 && *next_step2 != step {
                        if let Some((step1, step_cases1)) =
                            steps1.iter_mut().find(|(step, _)| step == next_step1)
                        {
                            if *next_step1 != step {
                                if let Some((_, step_cases2)) =
                                    steps2.iter().find(|(step, _)| step == next_step2)
                                {
                                    step_cases1.extend(step_cases2.clone());

                                    extend_split_steps(
                                        *step1,
                                        &mut step_cases1.clone(),
                                        steps1,
                                        steps2,
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}