Skip to main content

NfaFragment

Struct NfaFragment 

Source
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: usize

Entry point state index (into states vector)

§end: usize

Exit point state index (into states vector)

§counter_defs: Vec<CounterDef>

Counter definitions for counted loops within this fragment

§nullable: bool

Whether 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

Source

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)

Source

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

Source

pub fn start_state(&self) -> &NfaState

Get the start state

Source

pub fn end_state(&self) -> &NfaState

Get the end state

Source

pub fn get_state_mut(&mut self, index: usize) -> Option<&mut NfaState>

Get a mutable reference to a state by local index

Source

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.

Source

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.

Source

pub fn optional(self) -> NfaFragment

Make fragment optional: self?

Adds an epsilon transition from start to end, allowing the fragment to be skipped entirely.

Source

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.

Source

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).

Source

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.

Source

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]
Source

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

Source§

fn clone(&self) -> NfaFragment

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NfaFragment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.