Simplify Quantifier Enum with Greediness Flag #3

Closed
opened 2026-03-11 17:59:57 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 17:59:57 +03:00 (Migrated from github.com)

Goal: Reduce code duplication and improve maintainability of the Quantifier enum.

Currently, ast.rs defines many separate variants (e.g., ZeroOrMore, ZeroOrMoreLazy, Exactly, AtLeastLazy). This leads to repetitive match arms in multiple places.

Proposed Change:
Refactor Quantifier to separate the quantifier kind from its greediness:

pub enum QuantifierKind {
    ZeroOrMore,
    OneOrMore,
    Optional,
    Exactly(u32),
    AtLeast(u32),
    Between(u32, u32),
}

pub struct Quantifier {
    kind: QuantifierKind,
    greedy: bool, // true for greedy, false for lazy
}

Benefits:

  • Simplifies pattern matching (e.g., in to_regex_string and NFA construction).
  • Easier to add new quantifier modifiers (like possessive *+) later.
  • Reduces the total number of enum variants.

Implementation Steps:

  1. Modify ast.rs to replace Quantifier enum with the new struct.
  2. Update all usages in ast.rs, nfa.rs, and engine.rs to handle greedy flag.
  3. Ensure to_regex_string outputs *?, +?, etc., based on greedy.
  4. Update tests to cover both greedy and lazy quantifiers.
**Goal**: Reduce code duplication and improve maintainability of the `Quantifier` enum. Currently, `ast.rs` defines many separate variants (e.g., `ZeroOrMore`, `ZeroOrMoreLazy`, `Exactly`, `AtLeastLazy`). This leads to repetitive match arms in multiple places. **Proposed Change**: Refactor `Quantifier` to separate the quantifier kind from its greediness: ```rust pub enum QuantifierKind { ZeroOrMore, OneOrMore, Optional, Exactly(u32), AtLeast(u32), Between(u32, u32), } pub struct Quantifier { kind: QuantifierKind, greedy: bool, // true for greedy, false for lazy } ``` **Benefits**: - Simplifies pattern matching (e.g., in to_regex_string and NFA construction). - Easier to add new quantifier modifiers (like possessive *+) later. - Reduces the total number of enum variants. **Implementation Steps**: 1. Modify ast.rs to replace Quantifier enum with the new struct. 2. Update all usages in ast.rs, nfa.rs, and engine.rs to handle greedy flag. 3. Ensure to_regex_string outputs *?, +?, etc., based on greedy. 4. Update tests to cover both greedy and lazy quantifiers.
Sign in to join this conversation.
No description provided.