Skip to content

Commit

Permalink
refactor: Fixed clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphereye committed Feb 16, 2024
1 parent 18c79e3 commit b6bf605
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gregex"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
authors = ["Saphereye <adarshdas950@gmail.com>"]
license = "MIT"
Expand All @@ -9,7 +9,7 @@ keywords = ["regex", "nfa", "automata"]
categories = ["text-processing"]
documentation = "https://docs.rs/gregex"
exclude = [
".github/workflows/rust.yml",
".github/",
".gitignore",
"LICENSE",
"README.md",
Expand Down
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
//! ```rust
//! use::gregex::regex;
//!
//! fn main() {
//! let regex = regex("(a.b)*");
//! assert!(regex.simulate("abab"));
//! }
//! let regex = regex("(a.b)*");
//! assert!(regex.simulate("abab"));
//! ```
//!
//! The regex function uses the regular expression string to create a NFA that can be used to simulate the regular expression.
Expand Down Expand Up @@ -38,10 +36,8 @@ type Regex = NFA;
/// ```rust
/// use::gregex::regex;
///
/// fn main() {
/// let regex = regex("(a.b)*");
/// assert!(regex.simulate("abab"));
/// }
/// let regex = regex("(a.b)*");
/// assert!(regex.simulate("abab"));
/// ```
pub fn regex(regex_string: &str) -> Regex {
let regex_tree = linearize(regex_string);
Expand Down
26 changes: 2 additions & 24 deletions src/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,15 @@ use core::panic;
use std::collections::{HashMap, HashSet};

/// The `NFA` struct represents a non-deterministic finite automaton.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct NFA {
states: HashSet<u32>,
accept: HashSet<u32>,
/// The transition function is a map from a pair of a state and a character to a set of states.
transition_function: HashMap<(u32, char), HashSet<u32>>,
}

impl Default for NFA {
fn default() -> Self {
NFA {
states: HashSet::new(),
accept: HashSet::new(),
transition_function: HashMap::new(),
}
}
}

impl NFA {
fn new(
states: HashSet<u32>,
accept: HashSet<u32>,
transition_function: HashMap<(u32, char), HashSet<u32>>,
) -> NFA {
NFA {
states,
accept,
transition_function,
}
}

pub fn simulate(&self, input: &str) -> bool {
let mut current_states = HashSet::new();
current_states.insert(0);
Expand Down Expand Up @@ -90,7 +68,7 @@ impl NFA {
SetTerminal::DoubleElement(_, index1, symbol2, index2) => {
nfa.states.insert(index1);
nfa.states.insert(index2);
nfa.transition_function.entry((index1, symbol2)).or_insert_with(|| HashSet::new()).insert(index2);
nfa.transition_function.entry((index1, symbol2)).or_insert_with(HashSet::new).insert(index2);
}
SetTerminal::SingleElement(_, _) => {
panic!("SingleElement not supported")
Expand Down
4 changes: 0 additions & 4 deletions src/translation/linearize.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
//! Converts input regex to its linear form. Then it converts it into a Node tree.
use std::collections::HashMap;
use std::process::Child;

use crate::translation::node::Node;
use crate::translation::operator::Operator;

Expand Down
24 changes: 12 additions & 12 deletions src/translation/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ pub enum Node {
pub fn nullability_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Node::Terminal(symbol, code) => {
Node::Terminal(_, _) => {
set.insert(SetTerminal::Empty);
}
Node::Operation(op, left, right) => match op {
Operator::Or => {
set.extend(nullability_set(left));
set.extend(nullability_set(&*right.as_ref().unwrap()));
set.extend(nullability_set(right.as_ref().unwrap()));
}
Operator::Concat => {
set.extend(nullability_set(left));
let right_set = nullability_set(&*right.as_ref().unwrap());
let right_set = nullability_set(right.as_ref().unwrap());
set.extend(right_set);
}
Operator::Production => {
Expand All @@ -47,14 +47,14 @@ pub fn prefix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
Node::Operation(op, left, right) => match op {
Operator::Or => {
let left_set = prefix_set(left);
let right_set = prefix_set(&*right.as_ref().unwrap());
let right_set = prefix_set(right.as_ref().unwrap());
set.extend(left_set);
set.extend(right_set);
}
Operator::Concat => {
let left_set = prefix_set(left);
set.extend(left_set);
let right_set = prefix_set(&*right.as_ref().unwrap());
let right_set = prefix_set(right.as_ref().unwrap());
let nullable_set = nullability_set(left);

// If the left expression is nullable, include the first set of the right expression
Expand Down Expand Up @@ -82,15 +82,15 @@ pub fn suffix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
Node::Operation(op, left, right) => match op {
Operator::Or => {
let left_set = suffix_set(left);
let right_set = suffix_set(&*right.as_ref().unwrap());
let right_set = suffix_set(right.as_ref().unwrap());
set.extend(left_set);
set.extend(right_set);
}
Operator::Concat => {
let left_set = suffix_set(&*right.as_ref().unwrap());
let left_set = suffix_set(right.as_ref().unwrap());
set.extend(left_set);
let right_set = suffix_set(left);
let nullable_set = nullability_set(&*right.as_ref().unwrap());
let nullable_set = nullability_set(right.as_ref().unwrap());

// If the left expression is nullable, include the first set of the right expression
if nullable_set.contains(&SetTerminal::Epsilon) {
Expand All @@ -111,21 +111,21 @@ pub fn suffix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
pub fn factors_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Node::Terminal(symbol, code) => {
Node::Terminal(_, _) => {
set.insert(SetTerminal::Empty);
}
Node::Operation(op, left, right) => match op {
Operator::Or => {
let left_set = factors_set(left);
let right_set = factors_set(&*right.as_ref().unwrap());
let right_set = factors_set(right.as_ref().unwrap());
set.extend(left_set);
set.extend(right_set);
}
Operator::Concat => {
let left_set = factors_set(left);
let right_set = factors_set(&*right.as_ref().unwrap());
let right_set = factors_set(right.as_ref().unwrap());
let suffix_set = suffix_set(left);
let prefix_set = prefix_set(&*right.as_ref().unwrap());
let prefix_set = prefix_set(right.as_ref().unwrap());
set.extend(left_set);
set.extend(right_set);
for i in suffix_set {
Expand Down

0 comments on commit b6bf605

Please sign in to comment.