pub struct NfaFragment {
pub states: Vec<NfaState>,
pub start: usize,
pub end: usize,
pub counter_defs: Vec<CounterDef>,
pub nullable: bool,
}Expand description
A composable NFA fragment with single entry and exit points
Fragments are the building blocks for constructing complex NFAs using Thompson’s construction. They maintain the invariant of having exactly one start state and one end state, which enables easy composition.
Fields§
§states: Vec<NfaState>All states in this fragment (indices are local to fragment)
start: usizeEntry point state index (into states vector)
end: usizeExit point state index (into states vector)
counter_defs: Vec<CounterDef>Counter definitions for counted loops within this fragment
nullable: boolWhether this fragment can match the empty string (end reachable from
start without consuming any input). Tracked incrementally through
all composition operations and used to set CounterDef::body_nullable.
Implementations§
Source§impl NfaFragment
impl NfaFragment
Sourcepub fn new(states: Vec<NfaState>, start: usize, end: usize) -> Self
pub fn new(states: Vec<NfaState>, start: usize, end: usize) -> Self
Create a new fragment from states with specified start/end (no counters, not nullable)
Sourcepub fn with_counters(
states: Vec<NfaState>,
start: usize,
end: usize,
counter_defs: Vec<CounterDef>,
nullable: bool,
) -> Self
pub fn with_counters( states: Vec<NfaState>, start: usize, end: usize, counter_defs: Vec<CounterDef>, nullable: bool, ) -> Self
Create a new fragment with counter definitions
Sourcepub fn start_state(&self) -> &NfaState
pub fn start_state(&self) -> &NfaState
Get the start state
Sourcepub fn get_state_mut(&mut self, index: usize) -> Option<&mut NfaState>
pub fn get_state_mut(&mut self, index: usize) -> Option<&mut NfaState>
Get a mutable reference to a state by local index
Sourcepub fn concat(self, other: NfaFragment) -> NfaFragment
pub fn concat(self, other: NfaFragment) -> NfaFragment
Concatenate two fragments: self followed by other
Creates an epsilon transition from self’s end state to other’s start state. The resulting fragment starts at self’s start and ends at other’s end.
Sourcepub fn alternate(self, other: NfaFragment) -> NfaFragment
pub fn alternate(self, other: NfaFragment) -> NfaFragment
Alternate two fragments: self | other
Creates a new start state with epsilon transitions to both fragments, and a new end state that both fragments converge to.
Sourcepub fn optional(self) -> NfaFragment
pub fn optional(self) -> NfaFragment
Make fragment optional: self?
Adds an epsilon transition from start to end, allowing the fragment to be skipped entirely.
Sourcepub fn repeat_star(self) -> NfaFragment
pub fn repeat_star(self) -> NfaFragment
Kleene star: self*
Allows zero or more repetitions of the fragment. Adds loop back from end to start, plus makes it optional.
Sourcepub fn repeat_plus(self) -> NfaFragment
pub fn repeat_plus(self) -> NfaFragment
Plus repetition: self+
Requires at least one occurrence, then allows more. Adds loop back from end to start (no optional bypass).
Sourcepub fn repeat_exact(self, n: u32) -> NfaFragment
pub fn repeat_exact(self, n: u32) -> NfaFragment
Repeat exactly n times: self{n}
Creates n concatenated copies of the fragment. For n=0, returns an epsilon fragment.
Sourcepub fn repeat_counted(self, min: u32, max: u32) -> NfaFragment
pub fn repeat_counted(self, min: u32, max: u32) -> NfaFragment
Counted repeat: uses counter transitions for self{min,max}.
Produces a compact loop structure with 3 extra states (entry, guard, exit) regardless of min/max values. Counter tracks completed iterations.
Structure:
entry --CounterReset(c)--> body_start
body_end --CounterIncrement(c)--> guard
guard --CounterMaxGuard(c)--> body_start [loop if count < max]
guard --CounterMinGuard(c)--> exit [exit if count >= min]
[if min == 0: entry --Epsilon--> exit] [bypass]Sourcepub fn repeat_range(self, min: u32, max: Option<u32>) -> NfaFragment
pub fn repeat_range(self, min: u32, max: Option<u32>) -> NfaFragment
Repeat between min and max times: self{min,max}
Creates min mandatory copies followed by (max-min) optional copies. If max is None, creates min copies followed by a star.
Trait Implementations§
Source§impl Clone for NfaFragment
impl Clone for NfaFragment
Source§fn clone(&self) -> NfaFragment
fn clone(&self) -> NfaFragment
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more