Skip to content

Commit

Permalink
Add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
f-forcher committed Jul 28, 2024
1 parent b943616 commit 98dcbe4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
34 changes: 32 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
fn main() {
println!("Hello, world!");
use regex_automata::{dfa::{dense, Automaton}, Anchored, Input};

Check warning on line 1 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/structured-gen-rust/structured-gen-rust/src/main.rs
use anyhow::Result;

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();

Check warning on line 18 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/structured-gen-rust/structured-gen-rust/src/main.rs
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);

Check warning on line 29 in src/main.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/structured-gen-rust/structured-gen-rust/src/main.rs
assert!(dfa.is_match_state(state));

return Ok(())

Check failure on line 32 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement
}

0 comments on commit 98dcbe4

Please sign in to comment.