Skip to content

Commit

Permalink
docs: Added documentation for remaining stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphereye committed Apr 7, 2024
1 parent 467b9e6 commit 32da297
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gregex"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = ["Saphereye <adarshdas950@gmail.com>"]
license = "MIT"
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ use translation::node::*;

type Regex = NFA;

/// Translates a regular expression tree to a NFA. This NFA can then be called to simulate inputs.
pub fn regex(regex_tree: &Node) -> Regex {
let prefix_set = &prefix_set(regex_tree);
let suffix_set = &suffix_set(regex_tree);
let factors_set = &factors_set(regex_tree);
NFA::set_to_nfa(prefix_set, suffix_set, factors_set)
}

/// Keeps count of the terminals created. This is used to create unique terminals.
static TERMINAL_COUNT: AtomicU32 = AtomicU32::new(0);

/// Represents the `concatenation` action in regex. Can concatenate multiple nodes.
///
/// Regex: ab
/// Gregex: concatenate!(terminal('a'), terminal('b'))
#[macro_export]
macro_rules! concatenate {
($($node:expr),+ $(,)?) => {
Expand All @@ -51,11 +57,19 @@ macro_rules! concatenate {
};
}

/// Represents a `terminal` in regex. This is a single character.
///
/// Regex: a
/// Gregex: terminal('a')
pub fn terminal(symbol: char) -> Node {
let count = TERMINAL_COUNT.fetch_add(1, Ordering::SeqCst);
Node::Terminal(symbol, count)
}

/// Represents the `or`` action in regex. Can 'or' multiple nodes.
///
/// Regex: a|b
/// Gregex: or!(terminal('a'), terminal('b'))
#[macro_export]
macro_rules! or {
($($node:expr),+ $(,)?) => {
Expand All @@ -68,6 +82,10 @@ macro_rules! or {
};
}

/// Represents the `production` action in regex. This is a single node.
///
/// Regex: a*
/// Gregex: production!(terminal('a'))
#[macro_export]
macro_rules! production {
($child:expr) => {
Expand Down
4 changes: 4 additions & 0 deletions src/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ use std::collections::{HashMap, HashSet};
/// The `NFA` struct represents a non-deterministic finite automaton.
#[derive(Debug, Default)]
pub struct NFA {
/// Set of all possible states of the NFA.
states: HashSet<u32>,
/// Set of all accepting states. If the NFA ends at any one if these the simulation is succesful.
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 NFA {
/// Simulates the NFA with the given input.
pub fn simulate(&self, input: &str) -> bool {
let mut current_states = HashSet::new();
current_states.insert(0);
Expand All @@ -29,6 +32,7 @@ impl NFA {
!current_states.is_disjoint(&self.accept)
}

/// Converts the prefix, suffix and factors sets to a NFA.
pub fn set_to_nfa(
prefix_set: &HashSet<SetTerminal>,
suffix_set: &HashSet<SetTerminal>,
Expand Down
11 changes: 7 additions & 4 deletions src/translation/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use std::collections::HashSet;
/// The `Node` enum represents the different types of nodes that can be used in a regular expression tree.
#[derive(Debug, PartialEq, Eq)]
pub enum Node {
/// Represents an operation on one or two nodes.
Operation(Operator, Box<Node>, Option<Box<Node>>),
/// `char` represents the character, `u32` represent the unique identifier of the node.
Terminal(char, u32),
}

/// The `nullability_set` function returns the set of nullability of a regular expression tree.
/// The `nullability_set` function returns the set of [SetTerminal] that are nullable in a regular expression tree.
pub fn nullability_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Expand All @@ -38,7 +39,7 @@ pub fn nullability_set(regex_tree: &Node) -> HashSet<SetTerminal> {
set
}

/// The `prefix_set` function returns the set of prefixes of a regular expression tree.
/// The `prefix_set` function returns the set of [SetTerminal] that are prefixes of a regular expression tree.
pub fn prefix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn prefix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
set
}

/// The `suffix_set` function returns the set of suffixes of a regular expression tree.
/// The `suffix_set` function returns the set of [SetTerminal] that are suffixes of a regular expression tree.
pub fn suffix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Expand Down Expand Up @@ -108,7 +109,9 @@ pub fn suffix_set(regex_tree: &Node) -> HashSet<SetTerminal> {
set
}

/// The `factors_set` function returns the set of factors of a regular expression tree.
/// The `factors_set` function returns the set of [SetTerminal] that are factors of a regular expression tree.
///
/// Factors in this scenario mean the set of terminals that can be produced by the regular expression.
pub fn factors_set(regex_tree: &Node) -> HashSet<SetTerminal> {
let mut set = HashSet::new();
match regex_tree {
Expand Down

0 comments on commit 32da297

Please sign in to comment.