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
|
use std::cmp;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::iter::repeat_with;
use std::ops::RangeInclusive;
use regex_syntax::hir::{Class, Hir, HirKind, Literal, RepetitionKind, RepetitionRange};
#[derive(Eq, PartialEq, Clone)]
pub struct Range(pub RangeInclusive<char>);
impl PartialOrd for Range {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Range {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let start_order = self.0.start().cmp(other.0.start());
start_order.then_with(|| self.0.end().cmp(other.0.end()))
}
}
/// A nondeterministic finite automaton.
/// By convention, always starts in state 0.
#[derive(Default)]
pub struct NFA {
pub state_count: usize,
pub transition_table: BTreeMap<usize, BTreeMap<Option<Range>, BTreeSet<usize>>>,
pub accept_states: BTreeSet<usize>,
}
impl From<&Hir> for NFA {
fn from(hir: &Hir) -> Self {
match hir.kind() {
HirKind::Empty => {
// Conventionally, this matches only the empty string,
// but the regex crate has implicit `.*?` at the front and `.*`
// at the back of every regex.
// TODO figure out what to do with that
let mut result = NFA::default();
result.state_count = 1;
result.accept_states.insert(0);
result
}
HirKind::Literal(literal) => match literal {
Literal::Unicode(ch) => {
let ch = *ch;
let mut result = NFA::default();
result.state_count = 2;
result.accept_states.insert(1);
let range = Range(ch..=ch);
result
.transition_table
.entry(0)
.or_default()
.entry(Some(range))
.or_default()
.insert(1);
result
}
l => todo!("{:?}", l),
},
HirKind::Class(class) => match class {
Class::Unicode(class) => {
let mut result = NFA::default();
result.state_count = 2;
result.accept_states.insert(1);
for range in class.iter() {
let range = Range(range.start()..=range.end());
result
.transition_table
.entry(0)
.or_default()
.entry(Some(range))
.or_default()
.insert(1);
}
result
}
c => todo!("{:?}", c),
},
HirKind::Anchor(_) => {
// TODO actually fucking implement this
let mut result = NFA::default();
result.state_count = 1;
result.accept_states.insert(0);
result
}
HirKind::Repetition(rep) => match &rep.kind {
RepetitionKind::Range(range) => match range {
RepetitionRange::Exactly(n) => repeat_with(|| NFA::from(rep.hir.as_ref()))
.take(*n as usize)
.reduce(NFA::concat)
.expect("exactly 0 of something"),
r => todo!("{:?}", r),
},
k => todo!("{:?}", k),
},
HirKind::Concat(elements) => elements
.iter()
.map(NFA::from)
.reduce(NFA::concat)
.expect("empty HirKind::Concat"),
k => todo!("{:?}", k),
}
}
}
impl NFA {
fn concat(mut self, other: NFA) -> NFA {
// absorb the other automaton's states
let other_state_offset = self.state_count;
self.state_count += other.state_count;
self.transition_table
.extend(
other
.transition_table
.into_iter()
.map(|(state, table_for_state)| {
(
state + other_state_offset,
table_for_state
.into_iter()
.map(|(char_range, next_states)| {
(
char_range,
next_states
.into_iter()
.map(|s| s + other_state_offset)
.collect(),
)
})
.collect(),
)
}),
);
// for all the accept states of this machine...
for &self_accept in &self.accept_states {
// add an ε-transition from that state to the start of the other machine
self.transition_table
.entry(self_accept)
.or_default()
.entry(None)
.or_default()
.insert(other_state_offset);
}
// steal the other machine's accept states
self.accept_states = other
.accept_states
.into_iter()
.map(|x| x + other_state_offset)
.collect();
self
}
pub fn epsilon_closure(&self, state: usize) -> BTreeSet<usize> {
let mut set = BTreeSet::new();
set.insert(state);
self.epsilon_closure_all(set)
}
pub fn epsilon_closure_all(&self, states: impl IntoIterator<Item = usize>) -> BTreeSet<usize> {
let mut result = BTreeSet::new();
let mut queue: VecDeque<usize> = VecDeque::new();
queue.extend(states);
while let Some(next_state) = queue.pop_front() {
result.insert(next_state);
let states_after_that = self
.transition_table
.get(&next_state)
.and_then(|state_table| state_table.get(&None))
.map(|next_states| next_states.iter())
.into_iter()
.flatten()
.copied()
.filter(|s| !result.contains(s));
queue.extend(states_after_that);
}
result
}
}
|