regex_anre/route.rs
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
// Copyright (c) 2024 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,
// more details in file LICENSE, LICENSE.additional and CONTRIBUTING.
use crate::transition::Transition;
pub const MAIN_LINE_INDEX: usize = 0;
// route
// |-- line --\
// | |-- node --\
// | | |- transition item
// | | |- transition item
// | | |- ...
// | | \- transition item
// | |-- node
// | |-- ...
// | |-- node
// |
// |-- line
// the compile target
#[derive(Debug)]
pub struct Route {
pub lines: Vec<Line>,
pub capture_groups: Vec<CaptureGroup>,
}
#[derive(Debug)]
pub struct Line {
pub nodes: Vec<Node>,
pub start_node_index: usize,
pub end_node_index: usize,
// it is true when the expression starts with `^`,
// or encounters is_after and is_before sub-thread.
// it's used to optimise efficiency by directly exiting
// the thread looop when a thread fails.
pub fixed_start_position: bool,
}
// every node has one or more transitions,
// except the exit node has no transition.
#[derive(Debug)]
pub struct Node {
pub transition_items: Vec<TransitionItem>,
}
#[derive(Debug)]
pub struct TransitionItem {
pub transition: Transition, // the type of transition
pub target_node_index: usize, // the index of next node
}
#[derive(Debug)]
pub struct CaptureGroup {
pub name: Option<String>,
}
impl Route {
pub fn new() -> Self {
Route {
lines: vec![],
capture_groups: vec![],
}
}
pub fn new_line(&mut self) -> usize {
let line = Line {
nodes: vec![],
start_node_index: 0,
end_node_index: 0,
fixed_start_position: false,
};
let idx = self.lines.len();
self.lines.push(line);
idx
}
pub fn new_capture_group(&mut self, name: Option<String>) -> usize {
let idx = self.capture_groups.len();
self.capture_groups.push(CaptureGroup { name });
idx
}
pub fn get_capture_group_index_by_name(&self, name: &str) -> Option<usize> {
self.capture_groups.iter().position(|e| match &e.name {
Some(n) => n == name,
None => false,
})
}
pub fn get_capture_group_name_by_index(&self, index:usize) -> Option<&str> {
let n = &self.capture_groups[index].name;
if let Some(m) = n {
Some(m.as_str())
}else {
None
}
}
// pub fn get_capture_group_names(&self) -> Vec<Option<&String>> {
// self.capture_groups
// .iter()
// .map(|item| {
// if let Some(name) = &item.name {
// Some(name)
// } else {
// None
// }
// })
// .collect()
// }
// pub fn get_number_of_capture_groups(&self) -> usize {
// self.capture_groups.len()
// }
// pub fn get_number_of_counters(&self) -> usize {
// self.number_of_counters
// }
// for debug
pub fn get_debug_text(&self) -> String {
let mut ss = vec![];
// lines
if self.lines.len() == 1 {
ss.push(self.lines[0].get_debug_text());
} else {
for (node_index, line) in self.lines.iter().enumerate() {
ss.push(format!("= ${}", node_index));
ss.push(line.get_debug_text());
}
}
// capture groups
for (capture_group_index, capture_group) in self.capture_groups.iter().enumerate() {
let s = if let Some(name) = &capture_group.name {
format!("# {{{}}}, {}", capture_group_index, name)
} else {
format!("# {{{}}}", capture_group_index)
};
ss.push(s);
}
ss.join("\n")
}
}
impl Default for Route {
fn default() -> Self {
Self::new()
}
}
impl Line {
// return the index of the new node
pub fn new_node(&mut self) -> usize {
let node = Node {
transition_items: vec![],
};
let idx = self.nodes.len();
self.nodes.push(node);
idx
}
pub fn append_transition(
&mut self,
source_node_index: usize,
target_node_index: usize,
transition: Transition,
) -> usize {
let transition_node = TransitionItem {
transition,
target_node_index,
};
let idx = self.nodes[source_node_index].transition_items.len();
self.nodes[source_node_index]
.transition_items
.push(transition_node);
idx
}
// for debug
pub fn get_debug_text(&self) -> String {
let mut ss = vec![];
for (node_index, node) in self.nodes.iter().enumerate() {
// node
let prefix = if node_index == self.start_node_index {
'>'
} else if node_index == self.end_node_index {
'<'
} else {
'-'
};
let s = format!("{} {}", prefix, node_index);
ss.push(s);
// transition items
for transition_item in &node.transition_items {
let s = format!(
" -> {}, {}",
transition_item.target_node_index, transition_item.transition
);
ss.push(s);
}
}
ss.join("\n")
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::{assert_eq, assert_str_eq};
use crate::{
route::Route,
transition::{CharTransition, Transition},
};
#[test]
fn test_route_new_line() {
let mut route = Route::new();
// create a line
{
let line_index = route.new_line();
let line = &mut route.lines[line_index];
// create a node
line.new_node();
assert_str_eq!(
line.get_debug_text(),
"\
> 0"
);
// create other nodes
let idx1 = line.new_node();
let _idx2 = line.new_node();
let idx3 = line.new_node();
line.start_node_index = idx1;
line.end_node_index = idx3;
assert_str_eq!(
line.get_debug_text(),
"\
- 0
> 1
- 2
< 3"
);
}
// create another line
{
let line_index = route.new_line();
let line = &mut route.lines[line_index];
// create a node
line.new_node();
assert_str_eq!(
route.get_debug_text(),
"\
= $0
- 0
> 1
- 2
< 3
= $1
> 0"
);
}
}
#[test]
fn test_route_new_capture_group() {
let mut route = Route::new();
let line_index = route.new_line();
let line = &mut route.lines[line_index];
line.new_node();
line.new_node();
route.new_capture_group(None);
route.new_capture_group(Some("foo".to_owned()));
route.new_capture_group(None);
assert_str_eq!(
route.get_debug_text(),
"\
> 0
- 1
# {0}
# {1}, foo
# {2}"
);
assert_eq!(route.get_capture_group_index_by_name("foo"), Some(1));
assert!(route.get_capture_group_index_by_name("bar").is_none());
}
#[test]
fn test_line_append_transition() {
let mut route = Route::new();
let line_index = route.new_line();
let line = &mut route.lines[line_index];
let node_idx0 = line.new_node();
let node_idx1 = line.new_node();
let node_idx2 = line.new_node();
let node_idx3 = line.new_node();
let trans_idx0 = line.append_transition(
node_idx0,
node_idx1,
Transition::Char(CharTransition::new('a')),
);
assert_str_eq!(
line.get_debug_text(),
"\
> 0
-> 1, Char 'a'
- 1
- 2
- 3"
);
assert_eq!(trans_idx0, 0);
let trans_idx1 = line.append_transition(
node_idx0,
node_idx2,
Transition::Char(CharTransition::new('b')),
);
let trans_idx2 = line.append_transition(
node_idx0,
node_idx3,
Transition::Char(CharTransition::new('c')),
);
assert_str_eq!(
line.get_debug_text(),
"\
> 0
-> 1, Char 'a'
-> 2, Char 'b'
-> 3, Char 'c'
- 1
- 2
- 3"
);
assert_eq!(trans_idx1, 1);
assert_eq!(trans_idx2, 2);
let trans_idx3 = line.append_transition(
node_idx1,
node_idx2,
Transition::Char(CharTransition::new('x')),
);
assert_str_eq!(
line.get_debug_text(),
"\
> 0
-> 1, Char 'a'
-> 2, Char 'b'
-> 3, Char 'c'
- 1
-> 2, Char 'x'
- 2
- 3"
);
assert_eq!(trans_idx3, 0);
}
}