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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Compatibility layer with the standard `regex` crate.
//!
//! This module provides API compatibility with the widely-used `regex` crate,
//! allowing existing code to use our lightweight Thompson NFA implementation
//! as a drop-in replacement.
//!
//! ## Supported API Surface
//!
//! **Core Types:**
//! - `LightRegex` - Drop-in replacement for `regex::Regex`
//! - `RegexBuilder` - Builder pattern for regex construction
//! - `Error` - Error type compatible with `regex::Error`
//! - `Match` - Match result compatible with `regex::Match`
//!
//! **Key Methods:**
//! - `new()` - Compile pattern
//! - `is_match()` - Test for match
//! - `find()` - Find first match  
//! - `find_iter()` - Iterate over all matches
//! - `captures()` - Extract captures (basic support)
//! - `replace()` / `replace_all()` - String replacement
//! - `split()` - Split by regex
//!
//! ## Migration Strategy
//!
//! 1. **Phase 1**: Implement core matching API (80% coverage)
//! 2. **Phase 2**: Add capture groups and advanced features
//! 3. **Phase 3**: Performance optimizations and edge cases
//!
//! ## Limitations
//!
//! - No Unicode property support (use ASCII classes only)
//! - No look-around assertions
//! - No backreferences
//! - Basic capture group support only
//! - No regex sets or multi-pattern matching

use std::fmt;
use std::borrow::Cow;
use crate::regex::{NfaEngine, PatternParser, Matcher};
use crate::regex::matcher::Match as InternalMatch;

/// Error type compatible with `regex::Error`.
#[derive(Debug, Clone)]
pub enum Error {
    /// Syntax error in regex pattern
    Syntax(String),
    /// Compilation error
    CompiledTooBig(usize),
    /// Internal error
    Internal(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Syntax(msg) => write!(f, "regex parse error: {msg}"),
            Error::CompiledTooBig(limit) => write!(f, "compiled regex too big: {limit}"),
            Error::Internal(msg) => write!(f, "regex internal error: {msg}"),
        }
    }
}

impl std::error::Error for Error {}

impl From<crate::regex::parser::PatternError> for Error {
    fn from(err: crate::regex::parser::PatternError) -> Self {
        Error::Syntax(err.to_string())
    }
}

impl From<crate::regex::engine::EngineError> for Error {
    fn from(err: crate::regex::engine::EngineError) -> Self {
        match err {
            crate::regex::engine::EngineError::TooComplex(msg) => Error::CompiledTooBig(1000),
            crate::regex::engine::EngineError::UnsupportedFeature(msg) => Error::Syntax(msg),
            crate::regex::engine::EngineError::InternalError(msg) => Error::Internal(msg),
        }
    }
}

/// Match result compatible with `regex::Match`.
#[derive(Debug, Clone, Copy)]
pub struct Match<'t> {
    text: &'t str,
    start: usize,
    end: usize,
}

impl<'t> Match<'t> {
    /// Creates a new match.
    pub fn new(text: &'t str, start: usize, end: usize) -> Self {
        Self { text, start, end }
    }
    
    /// Returns the start position of the match.
    pub fn start(&self) -> usize {
        self.start
    }
    
    /// Returns the end position of the match.
    pub fn end(&self) -> usize {
        self.end
    }
    
    /// Returns the matched text.
    pub fn as_str(&self) -> &'t str {
        &self.text[self.start..self.end]
    }
    
    /// 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
    }
    
    /// Returns the range of the match.
    pub fn range(&self) -> std::ops::Range<usize> {
        self.start..self.end
    }
}

/// Captures type for compatibility (basic implementation).
#[derive(Debug, Clone)]
pub struct Captures<'t> {
    text: &'t str,
    matches: Vec<Option<Match<'t>>>,
}

impl<'t> Captures<'t> {
    /// Creates new captures.
    pub fn new(text: &'t str) -> Self {
        Self {
            text,
            matches: Vec::new(),
        }
    }
    
    /// Gets the full match.
    pub fn get(&self, i: usize) -> Option<Match<'t>> {
        self.matches.get(i).copied().flatten()
    }
    
    /// Gets the full match by name (not yet supported).
    pub fn name(&self, _name: &str) -> Option<Match<'t>> {
        None // Not implemented in Phase 1
    }
    
    /// Returns an iterator over all matches.
    pub fn iter(&self) -> impl Iterator<Item = Option<Match<'t>>> + '_ {
        self.matches.iter().copied()
    }
    
    /// Returns the number of captured groups.
    pub fn len(&self) -> usize {
        self.matches.len()
    }
    
    /// Tests if there are no captures.
    pub fn is_empty(&self) -> bool {
        self.matches.is_empty()
    }
}

/// Lightweight regex compatible with `regex::Regex`.
#[derive(Debug, Clone)]
pub struct LightRegex {
    engine: NfaEngine,
    pattern: String,
}

impl LightRegex {
    /// Compiles a regular expression.
    ///
    /// # Examples
    /// ```ignore
    /// use lambdust::regex::compat::LightRegex;
    /// let re = LightRegex::new(r"\d+").unwrap();
    /// assert!(re.is_match("123"));
    /// ```
    pub fn new(pattern: &str) -> Result<Self, Error> {
        let parsed = PatternParser::new(pattern).parse()?;
        let engine = NfaEngine::from_pattern(&parsed)?;
        
        Ok(Self {
            engine,
            pattern: pattern.to_string(),
        })
    }
    
    /// Tests if the regex matches anywhere in the text.
    pub fn is_match(&self, text: &str) -> bool {
        let mut matcher = Matcher::new(&self.engine);
        matcher.find(text).is_some()
    }
    
    /// Finds the first match in the text.
    pub fn find<'t>(&self, text: &'t str) -> Option<Match<'t>> {
        let mut matcher = Matcher::new(&self.engine);
        if let Some(internal_match) = matcher.find(text) {
            Some(Match::new(text, internal_match.start, internal_match.end))
        } else {
            None
        }
    }
    
    /// Returns an iterator over all non-overlapping matches.
    pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindMatches<'r, 't> {
        FindMatches::new(self, text)
    }
    
    /// Finds all matches and returns an iterator over capture groups.
    pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>> {
        if let Some(m) = self.find(text) {
            let mut captures = Captures::new(text);
            captures.matches.push(Some(m));
            Some(captures)
        } else {
            None
        }
    }
    
    /// Returns an iterator over all capture groups in the text.
    pub fn captures_iter<'r, 't>(&'r self, text: &'t str) -> CaptureMatches<'r, 't> {
        CaptureMatches::new(self, text)
    }
    
    /// Replaces the first match with replacement text.
    pub fn replace<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str> {
        if let Some(m) = self.find(text) {
            let mut result = String::with_capacity(text.len());
            result.push_str(&text[..m.start()]);
            result.push_str(rep);
            result.push_str(&text[m.end()..]);
            Cow::Owned(result)
        } else {
            Cow::Borrowed(text)
        }
    }
    
    /// Replaces all matches with replacement text.
    pub fn replace_all<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str> {
        let mut result = String::new();
        let mut last_end = 0;
        let mut found_any = false;
        
        for m in self.find_iter(text) {
            found_any = true;
            result.push_str(&text[last_end..m.start()]);
            result.push_str(rep);
            last_end = m.end();
        }
        
        if found_any {
            result.push_str(&text[last_end..]);
            Cow::Owned(result)
        } else {
            Cow::Borrowed(text)
        }
    }
    
    /// Replaces all matches using a replacer function.
    pub fn replace_all_fn<'t, F>(&self, text: &'t str, mut replacer: F) -> Cow<'t, str>
    where
        F: FnMut(&Match<'_>) -> String,
    {
        let mut result = String::new();
        let mut last_end = 0;
        let mut found_any = false;
        
        for m in self.find_iter(text) {
            found_any = true;
            result.push_str(&text[last_end..m.start()]);
            result.push_str(&replacer(&m));
            last_end = m.end();
        }
        
        if found_any {
            result.push_str(&text[last_end..]);
            Cow::Owned(result)
        } else {
            Cow::Borrowed(text)
        }
    }
    
    /// Splits text by the regex.
    pub fn split<'r, 't>(&'r self, text: &'t str) -> Split<'r, 't> {
        Split::new(self, text)
    }
    
    /// Splits text by the regex with a limit.
    pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: usize) -> SplitN<'r, 't> {
        SplitN::new(self, text, limit)
    }
    
    /// Returns the original pattern string.
    pub fn as_str(&self) -> &str {
        &self.pattern
    }
}

/// Iterator over all matches in text.
pub struct FindMatches<'r, 't> {
    regex: &'r LightRegex,
    text: &'t str,
    last_end: usize,
}

impl<'r, 't> FindMatches<'r, 't> {
    fn new(regex: &'r LightRegex, text: &'t str) -> Self {
        Self {
            regex,
            text,
            last_end: 0,
        }
    }
}

impl<'r, 't> Iterator for FindMatches<'r, 't> {
    type Item = Match<'t>;
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.last_end > self.text.len() {
            return None;
        }
        
        let mut matcher = Matcher::new(&self.regex.engine);
        for start_pos in self.last_end..=self.text.len() {
            if let Some(internal_match) = matcher.find_at(self.text, start_pos) {
                let m = Match::new(self.text, internal_match.start, internal_match.end);
                self.last_end = m.end().max(self.last_end + 1);
                return Some(m);
            }
        }
        None
    }
}

/// Iterator over all capture groups in text.
pub struct CaptureMatches<'r, 't> {
    matches: FindMatches<'r, 't>,
}

impl<'r, 't> CaptureMatches<'r, 't> {
    fn new(regex: &'r LightRegex, text: &'t str) -> Self {
        Self {
            matches: FindMatches::new(regex, text),
        }
    }
}

impl<'r, 't> Iterator for CaptureMatches<'r, 't> {
    type Item = Captures<'t>;
    
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(m) = self.matches.next() {
            let mut captures = Captures::new(self.matches.text);
            captures.matches.push(Some(m));
            Some(captures)
        } else {
            None
        }
    }
}

/// Iterator over split text segments.
pub struct Split<'r, 't> {
    finder: FindMatches<'r, 't>,
    last: usize,
    finished: bool,
}

impl<'r, 't> Split<'r, 't> {
    fn new(regex: &'r LightRegex, text: &'t str) -> Self {
        Self {
            finder: FindMatches::new(regex, text),
            last: 0,
            finished: false,
        }
    }
}

impl<'r, 't> Iterator for Split<'r, 't> {
    type Item = &'t str;
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }
        
        if let Some(m) = self.finder.next() {
            let text = &self.finder.text[self.last..m.start()];
            self.last = m.end();
            Some(text)
        } else {
            self.finished = true;
            Some(&self.finder.text[self.last..])
        }
    }
}

/// Iterator over split text segments with limit.
pub struct SplitN<'r, 't> {
    split: Split<'r, 't>,
    limit: usize,
    count: usize,
}

impl<'r, 't> SplitN<'r, 't> {
    fn new(regex: &'r LightRegex, text: &'t str, limit: usize) -> Self {
        Self {
            split: Split::new(regex, text),
            limit,
            count: 0,
        }
    }
}

impl<'r, 't> Iterator for SplitN<'r, 't> {
    type Item = &'t str;
    
    fn next(&mut self) -> Option<Self::Item> {
        if self.count >= self.limit {
            // Return rest of string as final segment
            if self.count == self.limit {
                self.count += 1;
                Some(&self.split.finder.text[self.split.last..])
            } else {
                None
            }
        } else {
            self.count += 1;
            self.split.next()
        }
    }
}

/// Regex builder for advanced configuration.
pub struct RegexBuilder {
    pattern: String,
    case_insensitive: bool,
    multi_line: bool,
    dot_matches_new_line: bool,
    unicode: bool,
}

impl RegexBuilder {
    /// Creates a new regex builder.
    pub fn new(pattern: &str) -> Self {
        Self {
            pattern: pattern.to_string(),
            case_insensitive: false,
            multi_line: false,
            dot_matches_new_line: false,
            unicode: true,
        }
    }
    
    /// Configures case-insensitive matching.
    pub fn case_insensitive(mut self, yes: bool) -> Self {
        self.case_insensitive = yes;
        self
    }
    
    /// Configures multi-line mode.
    pub fn multi_line(mut self, yes: bool) -> Self {
        self.multi_line = yes;
        self
    }
    
    /// Configures whether . matches newlines.
    pub fn dot_matches_new_line(mut self, yes: bool) -> Self {
        self.dot_matches_new_line = yes;
        self
    }
    
    /// Configures Unicode mode.
    pub fn unicode(mut self, yes: bool) -> Self {
        self.unicode = yes;
        self
    }
    
    /// Builds the regex.
    pub fn build(self) -> Result<LightRegex, Error> {
        // For Phase 1, ignore most flags and use basic compilation
        // TODO: Implement flag support in Phase 2
        LightRegex::new(&self.pattern)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_matching() {
        let re = LightRegex::new("hello").unwrap();
        assert!(re.is_match("hello world"));
        assert!(!re.is_match("goodbye world"));
    }
    
    #[test]
    fn test_find_match() {
        let re = LightRegex::new(r"\d+").unwrap();
        let m = re.find("abc123def").unwrap();
        assert_eq!(m.start(), 3);
        assert_eq!(m.end(), 6);
        assert_eq!(m.as_str(), "123");
    }
    
    #[test]
    fn test_find_iter() {
        let re = LightRegex::new(r"\d+").unwrap();
        let matches: Vec<_> = re.find_iter("a1b23c456").collect();
        assert_eq!(matches.len(), 3);
        assert_eq!(matches[0].as_str(), "1");
        assert_eq!(matches[1].as_str(), "23");
        assert_eq!(matches[2].as_str(), "456");
    }
    
    #[test]
    fn test_replace() {
        let re = LightRegex::new(r"\d+").unwrap();
        let result = re.replace("abc123def", "XXX");
        assert_eq!(result, "abcXXXdef");
    }
    
    #[test]
    fn test_replace_all() {
        let re = LightRegex::new(r"\d+").unwrap();
        let result = re.replace_all("a1b2c3", "X");
        assert_eq!(result, "aXbXcX");
    }
    
    #[test]
    fn test_split() {
        let re = LightRegex::new(r"\s+").unwrap();
        let parts: Vec<_> = re.split("a  b   c").collect();
        assert_eq!(parts, vec!["a", "b", "c", ""]);
    }
    
    #[test]
    fn test_splitn() {
        let re = LightRegex::new(r"\s+").unwrap();
        let parts: Vec<_> = re.splitn("a b c d", 2).collect();
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0], "a");
        assert_eq!(parts[1], "b c d");
    }
    
    #[test]
    fn test_captures() {
        let re = LightRegex::new(r"\d+").unwrap();
        let caps = re.captures("abc123def").unwrap();
        let m = caps.get(0).unwrap();
        assert_eq!(m.as_str(), "123");
    }
    
    #[test]
    fn test_regex_builder() {
        let re = RegexBuilder::new("hello")
            .case_insensitive(true)
            .build()
            .unwrap();
        
        // For now, flags are ignored, but API should work
        assert!(re.is_match("hello"));
    }
}