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
use crate::{
working_nfa::{EpsilonType, WorkingNFA},
working_u8_nfa::U8NFA,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Tag {
/// Marks that the start of capture group `{0}` should be updated to the current offset.
StartCapture(usize),
/// Marks that the end of capture group `{0}` should be updated to the current offset.
EndCapture(usize),
}
impl Tag {
/// Returns the capture group number this tag is associated with.
pub fn capture_group(&self) -> usize {
return match self {
Tag::StartCapture(group_num) | Tag::EndCapture(group_num) => *group_num,
};
}
}
/// Represents a squashed sequence of epsilon transitions
#[derive(Clone, PartialEq, Eq)]
pub struct EpsilonPropogation<T = Tag>
where
T: Clone + PartialEq + Eq,
{
/// The state it ends up at
pub state: usize,
/// Changes to tags
pub update_tags: Vec<T>,
/// If it contained a start anchor
pub start_only: bool,
/// If it contained an end anchor
pub end_only: bool,
}
impl EpsilonPropogation {
/// ## Params
/// - `nfa` is the original nfa
/// - `state` is the index of the state in `nfa.states`
pub fn calculate_epsilon_propogations_char(
nfa: &WorkingNFA,
state: usize,
) -> Vec<EpsilonPropogation<Tag>> {
let WorkingNFA { states } = nfa;
// reduce epsilons to occur in a single step
let mut new_threads = vec![];
fn traverse(
thread: EpsilonPropogation,
states: &Vec<crate::working_nfa::WorkingState>,
out: &mut Vec<EpsilonPropogation>,
) {
out.push(thread.clone());
for e in &states[thread.state].epsilons {
let mut new_thread = thread.clone();
new_thread.state = e.to;
match e.special {
EpsilonType::None => {}
EpsilonType::StartAnchor => new_thread.start_only = true,
EpsilonType::EndAnchor => new_thread.end_only = true,
EpsilonType::StartCapture(capture_group) => {
if !new_thread
.update_tags
.contains(&Tag::StartCapture(capture_group))
{
new_thread
.update_tags
.push(Tag::StartCapture(capture_group));
}
}
EpsilonType::EndCapture(capture_group) => {
if !new_thread
.update_tags
.contains(&Tag::EndCapture(capture_group))
{
new_thread.update_tags.push(Tag::EndCapture(capture_group));
}
}
}
if !out.contains(&new_thread) {
traverse(new_thread, states, out);
}
}
}
traverse(
EpsilonPropogation {
state,
update_tags: Vec::new(),
start_only: false,
end_only: false,
},
states,
&mut new_threads,
);
return new_threads;
}
/// Will maintain priority order
///
/// ## Params
/// - `nfa` is the original nfa
/// - `state` is the index of the state in `nfa.states`
pub fn calculate_epsilon_propogations_u8(
nfa: &U8NFA,
state: usize,
) -> Vec<EpsilonPropogation<Tag>> {
let U8NFA { states } = nfa;
// reduce epsilons to occur in a single step
let mut new_threads = vec![];
fn traverse(
thread: EpsilonPropogation,
states: &Vec<crate::working_u8_nfa::U8State>,
out: &mut Vec<EpsilonPropogation>,
) {
out.push(thread.clone());
for e in &states[thread.state].epsilons {
let mut new_thread = thread.clone();
new_thread.state = e.to;
match e.special {
EpsilonType::None => {}
EpsilonType::StartAnchor => new_thread.start_only = true,
EpsilonType::EndAnchor => new_thread.end_only = true,
EpsilonType::StartCapture(capture_group) => {
if !new_thread
.update_tags
.contains(&Tag::StartCapture(capture_group))
{
new_thread
.update_tags
.push(Tag::StartCapture(capture_group));
}
}
EpsilonType::EndCapture(capture_group) => {
if !new_thread
.update_tags
.contains(&Tag::EndCapture(capture_group))
{
new_thread.update_tags.push(Tag::EndCapture(capture_group));
}
}
}
if !out.contains(&new_thread) {
traverse(new_thread, states, out);
}
}
}
traverse(
EpsilonPropogation {
state,
update_tags: Vec::new(),
start_only: false,
end_only: false,
},
states,
&mut new_threads,
);
return new_threads;
}
}