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
//! A regex parser and compiler.

mod extend;
pub mod parse;

use std::collections::{BTreeMap, HashMap};

use parse::{Program, StepCase};

/// A state machine that can be used to match a regex.
#[derive(Default, Debug)]
pub struct StateMachine {
    /// A list of steps, each containing a list of cases.
    pub steps: BTreeMap<usize, Vec<StepCase>>,
    /// A map of steps to the pattern index they should return.
    pub ends: HashMap<usize, usize>,
}

impl StateMachine {
    #[inline]
    #[must_use]
    pub fn new(program: &Program) -> Self {
        let mut state_machine = Self::default();
        state_machine.parse(program);

        state_machine
    }

    /// # Panics
    ///
    /// Panics if the state machine is empty.
    #[inline]
    #[must_use]
    pub fn first_step(&self) -> usize {
        let (&position, _) = self
            .steps
            .first_key_value()
            .expect("state machine should not be empty");

        position
    }

    /// # Panics
    ///
    /// Panics if the state machine is empty.
    #[inline]
    #[must_use]
    pub fn last_step(&self) -> usize {
        let (&position, _) = self
            .steps
            .last_key_value()
            .expect("state machine should not be empty");

        position
    }

    #[inline]
    #[must_use]
    pub fn step_size(&self) -> u8 {
        let last_step = self.last_step();

        if last_step == 0 {
            8
        } else {
            2_u8.pow((last_step.ilog2() / 8) + 3)
        }
    }
}