Add method to dump FSM to dot

This commit is contained in:
_Frky 2021-12-31 18:09:50 +01:00
parent 0b41a6cd08
commit 6ad5491b24

View file

@ -1,4 +1,5 @@
use std::mem; use std::mem;
use std::convert::TryFrom;
use crate::smack::smack_constants::*; use crate::smack::smack_constants::*;
use crate::smack::smack_pattern::SmackPattern; use crate::smack::smack_pattern::SmackPattern;
@ -344,6 +345,50 @@ impl Smack {
} }
(idx - px_start, row) (idx - px_start, row)
} }
fn show(&self) {
println!("digraph D {{");
for row in 0..self.m_state_count {
for i in 1..self.symbol_count {
let s = self.symbol_to_char[i];
let from = {
if row == BASE_STATE {
String::from("BASE")
} else if row == UNANCHORED_STATE {
String::from("UNANCHORED")
} else if row >= self.m_match_limit {
format!("\"{}\"", std::str::from_utf8(&self.m_pattern_list[self.m_match[row].m_ids[0]].pattern).unwrap())
} else {
format!("{}", row).into()
}
};
let to = {
let dst = self.goto(row, s);
if dst == BASE_STATE {
String::from("BASE")
} else if dst == UNANCHORED_STATE {
String::from("UNANCHORED")
} else if dst >= self.m_match_limit {
format!("\"{}\"", std::str::from_utf8(&self.m_pattern_list[self.m_match[dst].m_ids[0]].pattern).unwrap())
} else {
format!("{}", dst).into()
}
};
let c = {
if s == CHAR_ANCHOR_START {
String::from("^")
} else if s == CHAR_ANCHOR_END {
String::from("$")
} else if s == b'*'.into() {
String::from("ANY")
} else {
format!("{}", u8::try_from(s).unwrap() as char)
}
};
println!(" {} -> {} [label=\"{}\"]", from, to, c);
}
}
println!("}}");
}
fn inner_match_shift7(&self, px: Vec<u8>, length: usize, state: usize) -> (usize, usize) { fn inner_match_shift7(&self, px: Vec<u8>, length: usize, state: usize) -> (usize, usize) {
let px_start = 0; let px_start = 0;
let px_end = length; let px_end = length;