diff --git a/Cargo.lock b/Cargo.lock index 5ce011f..2da0efa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,46 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "git+https://github.com/f-forcher/regex?branch=expose-state-iter#cdc3eb10549fc9ce0da2dd6a047c1e26aebdc9ef" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "git+https://github.com/f-forcher/regex?branch=expose-state-iter#cdc3eb10549fc9ce0da2dd6a047c1e26aebdc9ef" + [[package]] name = "structured-gen-rust" version = "0.1.0" +dependencies = [ + "anyhow", + "regex-automata", +] diff --git a/Cargo.toml b/Cargo.toml index b48d2f5..885619b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = "1.0.86" +regex-automata = "0.4.7" + +[patch.crates-io] +regex-automata = { git = 'https://github.com/f-forcher/regex', branch = 'expose-state-iter' } diff --git a/src/main.rs b/src/main.rs index e7a11a9..85d9ecb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,34 @@ -fn main() { - println!("Hello, world!"); +use anyhow::Result; +use regex_automata::{ + dfa::{dense, Automaton}, + Anchored, Input, +}; + +fn main() -> Result<()> { + let dfa = dense::DFA::new(r"[0-9]*\.?[0-9]*")?; + let haystack = "1.34"; + + // The start state is determined by inspecting the position and the + // initial bytes of the haystack. + let mut state = dfa.start_state_forward(&Input::new(haystack).anchored(Anchored::Yes))?; + // Walk all the bytes in the haystack. + for &b in haystack.as_bytes().iter() { + state = dfa.next_state(state, b); + } + + let states_num = dfa.tt.len(); + + println!("There are {} states in this dfa", states_num); + + let states_ids: Vec<_> = dfa.tt.states().map(|state| state.id()).collect(); + + println!("Their IDs are {:?}", states_ids); + + // DFAs in this crate require an explicit + // end-of-input transition if a search reaches + // the end of a haystack. + state = dfa.next_eoi_state(state); + assert!(dfa.is_match_state(state)); + + Ok(()) }