lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! NFA execution engine for pattern matching.
//!
//! This module implements the Thompson NFA simulation algorithm for efficient
//! regular expression matching. It uses the classical approach of maintaining
//! a set of active states and processing input characters one by one.
//!
//! ## Algorithm: NFA Simulation
//!
//! The matcher maintains two sets of states:
//! 1. **Current States**: States active before processing current character
//! 2. **Next States**: States active after processing current character
//!
//! For each input character:
//! 1. Start with current state set (initially epsilon closure of start state)
//! 2. For each state in current set, follow matching transitions
//! 3. Compute epsilon closure of resulting states → next state set
//! 4. Swap current ↔ next, continue with next character
//!
//! **Time Complexity**: O(nm) where n = text length, m = NFA size
//! **Space Complexity**: O(m) for state sets
//!
//! ## Features
//!
//! - **Epsilon Closure**: Handles epsilon transitions efficiently
//! - **Anchors**: Supports ^ (start) and $ (end) anchors  
//! - **Character Classes**: Efficient character class matching
//! - **Match Positions**: Tracks start/end positions of matches

use std::collections::HashSet;
use crate::regex::engine::{Nfa, NfaEngine, StateId, Transition};

/// Match result containing position and matched text.
#[derive(Debug, Clone, PartialEq)]
pub struct Match {
    /// Start position in input text (byte offset)
    pub start: usize,
    /// End position in input text (byte offset) 
    pub end: usize,
    /// Matched text slice
    text: String,
}

impl Match {
    /// Creates a new match result.
    pub fn new(start: usize, end: usize, text: String) -> Self {
        Self { start, end, text }
    }
    
    /// Returns the matched text as a string slice.
    pub fn as_str(&self) -> &str {
        &self.text
    }
    
    /// Returns the length of the match.
    pub fn len(&self) -> usize {
        self.end - self.start
    }
    
    /// Tests if the match is empty.
    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }
}

/// Complete match result including potential captures.
#[derive(Debug, Clone)]
pub struct MatchResult {
    /// Primary match
    pub full_match: Match,
    /// Captured groups (for future extension)
    pub captures: Vec<Option<Match>>,
}

/// NFA matcher for executing pattern matching.
pub struct Matcher<'nfa> {
    /// NFA to execute
    nfa: &'nfa Nfa,
    /// Current active states
    current_states: HashSet<StateId>,
    /// Next active states  
    next_states: HashSet<StateId>,
    /// Whether we're at start of input
    at_start: bool,
}

impl<'nfa> Matcher<'nfa> {
    /// Creates a new matcher for the given NFA.
    pub fn new(engine: &'nfa NfaEngine) -> Self {
        Self {
            nfa: engine.nfa(),
            current_states: HashSet::new(),
            next_states: HashSet::new(),
            at_start: true,
        }
    }
    
    /// Finds the first match in the input text.
    pub fn find(&mut self, text: &str) -> Option<Match> {
        for start_pos in 0..=text.len() {
            if let Some(m) = self.find_at(text, start_pos) {
                return Some(m);
            }
        }
        None
    }
    
    /// Attempts to find a match starting at the specified position.
    pub fn find_at(&mut self, text: &str, start_pos: usize) -> Option<Match> {
        if start_pos > text.len() {
            return None;
        }
        
        // Initialize state sets
        self.current_states.clear();
        self.next_states.clear();
        self.at_start = start_pos == 0;
        
        // Start with epsilon closure of start state
        self.current_states.insert(self.nfa.start_state);
        Matcher::epsilon_closure_for_nfa(self.nfa, &mut self.current_states);
        
        // Check for immediate accept (empty match)
        if self.has_accept_state(&self.current_states) {
            return Some(Match::new(start_pos, start_pos, String::new()));
        }
        
        // Process each character
        let text_bytes = text.as_bytes();
        let mut pos = start_pos;
        
        while pos < text.len() {
            let ch = text_bytes[pos] as char; // Simplified: assume ASCII
            self.next_states.clear();
            
            // Process transitions for current character
            for &state_id in &self.current_states {
                if let Some(state) = self.nfa.states.get(&state_id) {
                    for (transition, target) in &state.transitions {
                        if self.transition_matches(transition, ch, pos, text.len()) {
                            self.next_states.insert(*target);
                        }
                    }
                }
            }
            
            // Compute epsilon closure of next states
            Matcher::epsilon_closure_for_nfa(self.nfa, &mut self.next_states);
            
            // Check for match
            if self.has_accept_state(&self.next_states) {
                let match_text = text[start_pos..=pos].to_string();
                return Some(Match::new(start_pos, pos + 1, match_text));
            }
            
            // If no active states, matching failed
            if self.next_states.is_empty() {
                break;
            }
            
            // Swap state sets for next iteration
            std::mem::swap(&mut self.current_states, &mut self.next_states);
            pos += 1;
            self.at_start = false;
        }
        
        // Handle end-of-input anchors
        if !self.current_states.is_empty() {
            self.next_states.clear();
            
            for &state_id in &self.current_states {
                if let Some(state) = self.nfa.states.get(&state_id) {
                    for (transition, target) in &state.transitions {
                        if matches!(transition, Transition::End) {
                            self.next_states.insert(*target);
                        }
                    }
                }
            }
            
            Matcher::epsilon_closure_for_nfa(self.nfa, &mut self.next_states);
            
            if self.has_accept_state(&self.next_states) {
                let match_text = text[start_pos..pos].to_string();
                return Some(Match::new(start_pos, pos, match_text));
            }
        }
        
        None
    }
    
    /// Tests if a transition matches the current character and context.
    fn transition_matches(&self, transition: &Transition, ch: char, pos: usize, text_len: usize) -> bool {
        match transition {
            Transition::Epsilon => false, // Handled separately
            Transition::Char(expected) => ch == *expected,
            Transition::CharClass(class) => class.matches(ch),
            Transition::Any => ch != '\n', // . doesn't match newline by default
            Transition::Start => pos == 0 || self.at_start,
            Transition::End => pos == text_len,
        }
    }
    
    /// Computes epsilon closure of a state set in-place.
    fn epsilon_closure(&self, states: &mut HashSet<StateId>) {
        Matcher::epsilon_closure_for_nfa(self.nfa, states);
    }
    
    /// Static helper for computing epsilon closure to avoid borrowing issues.
    fn epsilon_closure_for_nfa(nfa: &Nfa, states: &mut HashSet<StateId>) {
        let mut stack: Vec<StateId> = states.iter().copied().collect();
        
        while let Some(state_id) = stack.pop() {
            if let Some(state) = nfa.states.get(&state_id) {
                for (transition, target) in &state.transitions {
                    if matches!(transition, Transition::Epsilon)
                        && states.insert(*target) {
                            // New state added, explore it
                            stack.push(*target);
                        }
                }
            }
        }
    }
    
    /// Tests if any state in the set is an accept state.
    fn has_accept_state(&self, states: &HashSet<StateId>) -> bool {
        states.iter().any(|&state_id| {
            self.nfa.accept_states.contains(&state_id)
        })
    }
    
    /// Resets the matcher for reuse.
    pub fn reset(&mut self) {
        self.current_states.clear();
        self.next_states.clear();
        self.at_start = true;
    }
}

/// Iterator for finding all matches in text.
pub struct FindMatches<'m, 't> {
    matcher: &'m mut Matcher<'t>,
    text: &'t str,
    pos: usize,
}

impl<'m, 't> FindMatches<'m, 't> {
    /// Creates a new find iterator.
    pub fn new(matcher: &'m mut Matcher<'t>, text: &'t str) -> Self {
        Self {
            matcher,
            text,
            pos: 0,
        }
    }
}

impl<'m, 't> Iterator for FindMatches<'m, 't> {
    type Item = Match;
    
    fn next(&mut self) -> Option<Self::Item> {
        while self.pos <= self.text.len() {
            if let Some(m) = self.matcher.find_at(self.text, self.pos) {
                self.pos = m.end.max(self.pos + 1); // Ensure progress
                return Some(m);
            } else {
                self.pos += 1;
            }
        }
        None
    }
}

/// Helper function to create a match iterator.
pub fn find_iter<'t>(engine: &'t NfaEngine, text: &'t str) -> impl Iterator<Item = Match> + 't {
    let mut matcher = Matcher::new(engine);
    let mut pos = 0;
    
    std::iter::from_fn(move || {
        while pos <= text.len() {
            if let Some(m) = matcher.find_at(text, pos) {
                pos = m.end.max(pos + 1);
                return Some(m);
            } else {
                pos += 1;
            }
        }
        None
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::regex::parser::PatternParser;

    fn create_engine(pattern: &str) -> NfaEngine {
        let parsed = PatternParser::new(pattern).parse().unwrap();
        NfaEngine::from_pattern(&parsed).unwrap()
    }

    fn create_matcher(pattern: &str) -> (NfaEngine, Matcher) {
        let engine = create_engine(pattern);
        let matcher = Matcher::new(&engine);
        (engine, matcher)
    }

    #[test]
    fn test_simple_char_match() {
        let engine = create_engine("a");
        let mut matcher = Matcher::new(&engine);
        
        assert!(matcher.find("a").is_some());
        assert!(matcher.find("abc").is_some());
        assert!(matcher.find("bac").is_some());
        assert!(matcher.find("xyz").is_none());
    }
    
    #[test]
    fn test_concatenation_match() {
        let (_engine, mut matcher) = create_matcher("abc");
        
        let m = matcher.find("abc").unwrap();
        assert_eq!(m.start, 0);
        assert_eq!(m.end, 3);
        assert_eq!(m.as_str(), "abc");
        
        let m2 = matcher.find("xyzabc").unwrap();
        assert_eq!(m2.start, 3);
        assert_eq!(m2.end, 6);
        
        assert!(matcher.find("ab").is_none());
        assert!(matcher.find("acb").is_none());
    }
    
    #[test]
    fn test_alternation_match() {
        let (_engine, mut matcher) = create_matcher("a|b");
        
        assert!(matcher.find("a").is_some());
        assert!(matcher.find("b").is_some());
        assert!(matcher.find("c").is_none());
        
        let m = matcher.find("ba").unwrap();
        assert_eq!(m.as_str(), "b");
        assert_eq!(m.start, 0);
    }
    
    #[test]
    fn test_star_quantifier() {
        let (_engine, mut matcher) = create_matcher("a*");
        
        // Should match empty string
        let m = matcher.find("").unwrap();
        assert_eq!(m.start, 0);
        assert_eq!(m.end, 0);
        
        // Should match "a"
        let m2 = matcher.find("a").unwrap();
        assert_eq!(m2.as_str(), "a");
        
        // Should match "aaa"  
        let m3 = matcher.find("aaa").unwrap();
        assert_eq!(m3.as_str(), "aaa");
        
        // Should match at start of "baa"
        let m4 = matcher.find("baa").unwrap();
        assert_eq!(m4.start, 0);
        assert_eq!(m4.end, 0); // Empty match at start
    }
    
    #[test]
    fn test_plus_quantifier() {
        let (_engine, mut matcher) = create_matcher("a+");
        
        // Should not match empty string
        assert!(matcher.find("").is_none());
        
        // Should match "a"
        let m = matcher.find("a").unwrap();
        assert_eq!(m.as_str(), "a");
        
        // Should match "aaa"
        let m2 = matcher.find("aaa").unwrap();
        assert_eq!(m2.as_str(), "aaa");
        
        // Should not match at start of "baa"
        let m3 = matcher.find("baa").unwrap();
        assert_eq!(m3.start, 1); // Matches "aa" part
        assert_eq!(m3.as_str(), "aa");
    }
    
    #[test]
    fn test_question_quantifier() {
        let (_engine, mut matcher) = create_matcher("a?");
        
        // Should match empty string
        let m = matcher.find("").unwrap();
        assert_eq!(m.start, 0);
        assert_eq!(m.end, 0);
        
        // Should match "a"
        let m2 = matcher.find("a").unwrap();
        assert_eq!(m2.as_str(), "a");
        
        // Should match first "a" in "aa"
        let m3 = matcher.find("aa").unwrap();
        assert_eq!(m3.as_str(), "a");
        assert_eq!(m3.start, 0);
        assert_eq!(m3.end, 1);
    }
    
    #[test]
    fn test_any_char() {
        let (_engine, mut matcher) = create_matcher(".");
        
        assert!(matcher.find("a").is_some());
        assert!(matcher.find("1").is_some());
        assert!(matcher.find("@").is_some());
        assert!(matcher.find("").is_none());
        
        // Should not match newline by default
        assert!(matcher.find("\n").is_none());
    }
    
    #[test]
    fn test_character_class() {
        let (_engine, mut matcher) = create_matcher("[abc]");
        
        assert!(matcher.find("a").is_some());
        assert!(matcher.find("b").is_some());
        assert!(matcher.find("c").is_some());
        assert!(matcher.find("d").is_none());
        assert!(matcher.find("xay").is_some()); // Should find 'a'
    }
    
    #[test]
    fn test_digit_class() {
        let (_engine, mut matcher) = create_matcher(r"\d");
        
        assert!(matcher.find("5").is_some());
        assert!(matcher.find("0").is_some());
        assert!(matcher.find("9").is_some());
        assert!(matcher.find("a").is_none());
        
        let m = matcher.find("abc123").unwrap();
        assert_eq!(m.as_str(), "1");
        assert_eq!(m.start, 3);
    }
    
    #[test]
    fn test_word_class() {
        let (_engine, mut matcher) = create_matcher(r"\w");
        
        assert!(matcher.find("a").is_some());
        assert!(matcher.find("Z").is_some());
        assert!(matcher.find("5").is_some());
        assert!(matcher.find("_").is_some());
        assert!(matcher.find("@").is_none());
        assert!(matcher.find(" ").is_none());
    }
    
    #[test]
    fn test_complex_pattern() {
        let (_engine, mut matcher) = create_matcher(r"\d+\.\d*");
        
        let m = matcher.find("3.14").unwrap();
        assert_eq!(m.as_str(), "3.14");
        
        let m2 = matcher.find("42.").unwrap();
        assert_eq!(m2.as_str(), "42.");
        
        assert!(matcher.find("3").is_none());
        assert!(matcher.find(".14").is_none());
    }
    
    #[test] 
    fn test_find_positions() {
        let (_engine, mut matcher) = create_matcher("ab");
        
        let m = matcher.find("xyzab123").unwrap();
        assert_eq!(m.start, 3);
        assert_eq!(m.end, 5);
        assert_eq!(m.as_str(), "ab");
    }
}