Skip to content

fix: handle epsilon transitions #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ package-lock.json
yarn.lock

index.node
.yarn/
File renamed without changes.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
},
"engines": {
"yarn": "^1.22.0"
}
}
},
"packageManager": "[email protected]"
}
16 changes: 16 additions & 0 deletions packages/compiler/src/dfa_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,21 @@
"regex": "^[a-zA-Z]{2,}\\s[a-zA-Z]{1,}'?-?[a-zA-Z]{2,}\\s?([a-zA-Z]{1,})?$",
"pass": ["John Doe", "Mary Jane", "Robert O'Neill", "Sarah Jane-Smith"],
"fail": ["J D", "John", "John Doe", "12John Doe"]
},
{
"regex": "(\r\n|^)to:([^\r\n]+<)?([^@]{1,3})([^@]*)@([a-zA-Z0-9.-]+)>?\r\n",
"pass": [
"to:[email protected]\r\n",
"to:\"John Doe\" <[email protected]>\r\n",
"to:[email protected]\r\n",
"to:[email protected]\r\n",
"\r\nto:[email protected]\r\n"
],
"fail": [
"to:@example.com\r\n",
"to:user@\r\n",
"from:[email protected]\r\n",
"to:[email protected]"
]
}
]
12 changes: 10 additions & 2 deletions packages/compiler/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,11 @@ pub(crate) fn get_regex_and_dfa(
.map(|regex| regex.regex_def.as_str())
.collect::<String>();

// Remove epsilon transitions
for state in &mut net_dfa_graph.states {
state.transitions.retain(|_, chars| !chars.is_empty());
}

Ok(RegexAndDFA {
regex_pattern: regex_str,
dfa: net_dfa_graph,
Expand Down Expand Up @@ -1033,6 +1038,7 @@ fn create_dfa_graph_from_regex(regex: &str) -> Result<DFAGraph, CompilerError> {
/// # Returns
///
/// A boolean indicating whether the input string matches the regex pattern.
#[cfg(test)]
fn match_string_with_dfa_graph(graph: &DFAGraph, input: &str) -> bool {
let mut current_state = 0;

Expand Down Expand Up @@ -1132,10 +1138,12 @@ pub(crate) fn get_max_state(dfa: &DFAGraph) -> usize {
.unwrap_or_default()
}

#[cfg(test)]
mod dfa_test {
use crate::regex::{create_dfa_graph_from_regex, match_string_with_dfa_graph};
use super::create_dfa_graph_from_regex;
use crate::regex::match_string_with_dfa_graph;
use serde::{Deserialize, Serialize};
use std::{env, fs::File, io::BufReader, path::PathBuf};
use std::{fs::File, io::BufReader, path::PathBuf};

#[derive(Debug, Deserialize, Serialize)]
struct RegexTestCase {
Expand Down
Loading