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
//! Unit tests for parser combinators
//!
//! This module contains comprehensive unit tests for all parser combinators,
//! ensuring correctness, performance, and R7RS compliance.

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::combinators::{
        primitive,
        combinator::ParserCombinator,
        scheme,
        types::*,
    };
    
    // Helper functions from the primitive module
    use primitive::{char, tag, digit, satisfy};
    // Helper functions from the scheme module  
    use scheme::{scheme_number, scheme_string, scheme_character, scheme_symbol, scheme_sexp};
    // Helper functions from the combinator module
    use combinator::{whitespace0, whitespace1};

    /// Test basic character parsing
    #[test]
    fn test_char_parsing() {
        let parser = char('a');
        
        // Successful parsing
        match parser.parse("abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "bc");
                assert_eq!(parsed, 'a');
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Failed parsing - wrong character
        match parser.parse("xyz") {
            Ok(_) => panic!("Expected parse error for wrong character"),
            Err(e) => {
                assert_eq!(e.actual, "x".to_string());
                assert!(e.expected.contains(&"a".to_string()));
            }
        }
        
        // Failed parsing - empty input
        match parser.parse("") {
            Ok(_) => panic!("Expected parse error for empty input"),
            Err(e) => {
                assert_eq!(e.actual, "EOF".to_string());
                assert!(e.expected.contains(&"a".to_string()));
            }
        }
    }

    /// Test string tag parsing
    #[test]
    fn test_tag_parsing() {
        let parser = tag("hello");
        
        // Successful parsing
        match parser.parse("hello world") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " world");
                assert_eq!(parsed, "hello");
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Failed parsing - partial match
        match parser.parse("help") {
            Ok(_) => panic!("Expected parse error for partial match"),
            Err(e) => {
                assert_eq!(e.actual, "help".to_string());
                assert!(e.expected.contains(&"hello".to_string()));
            }
        }
    }

    /// Test digit parsing
    #[test]
    fn test_digit_parsing() {
        let parser = digit();
        
        // Successful parsing
        match parser.parse("5abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "abc");
                assert_eq!(parsed, '5');
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Failed parsing - non-digit
        match parser.parse("abc") {
            Ok(_) => panic!("Expected parse error for non-digit"),
            Err(e) => {
                assert_eq!(e.actual, "a".to_string());
                assert!(e.expected.contains(&"digit".to_string()));
            }
        }
    }

    /// Test satisfy predicate parsing
    #[test]
    fn test_satisfy_parsing() {
        let parser = satisfy(|c: &char| c.is_alphabetic());
        
        // Successful parsing
        match parser.parse("abc123") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "bc123");
                assert_eq!(parsed, 'a');
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Failed parsing - predicate fails
        match parser.parse("123abc") {
            Ok(_) => panic!("Expected parse error when predicate fails"),
            Err(e) => {
                assert_eq!(e.actual, "1".to_string());
                assert!(e.message.contains("predicate"));
            }
        }
    }

    /// Test parser mapping
    #[test]
    fn test_map_combinator() {
        let parser = digit().map(|c| c.to_digit(10).unwrap() as i32);
        
        match parser.parse("7xyz") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "xyz");
                assert_eq!(parsed, 7);
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
    }

    /// Test parser alternatives (or combinator)
    #[test]
    fn test_or_combinator() {
        let parser = char('a').or(char('b'));
        
        // First alternative succeeds
        match parser.parse("abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "bc");
                assert_eq!(parsed, 'a');
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Second alternative succeeds
        match parser.parse("bac") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "ac");
                assert_eq!(parsed, 'b');
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Both alternatives fail
        match parser.parse("xyz") {
            Ok(_) => panic!("Expected parse error when both alternatives fail"),
            Err(e) => {
                assert_eq!(e.actual, "x".to_string());
                assert!(e.expected.len() >= 1); // Should have multiple expected values
            }
        }
    }

    /// Test many combinator (zero or more)
    #[test]
    fn test_many_combinator() {
        let parser = digit().many();
        
        // Multiple matches
        match parser.parse("123abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "abc");
                assert_eq!(parsed, vec!['1', '2', '3']);
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Zero matches (should still succeed)
        match parser.parse("abc123") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "abc123");
                assert_eq!(parsed, Vec::<char>::new());
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
    }

    /// Test many1 combinator (one or more)
    #[test]
    fn test_many1_combinator() {
        let parser = digit().many1();
        
        // Multiple matches
        match parser.parse("123abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "abc");
                assert_eq!(parsed, vec!['1', '2', '3']);
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Zero matches (should fail)
        match parser.parse("abc123") {
            Ok(_) => panic!("Expected parse error when no matches for many1"),
            Err(e) => {
                assert!(e.message.contains("at least one"));
            }
        }
    }

    /// Test optional combinator
    #[test]
    fn test_optional_combinator() {
        let parser = char('a').optional();
        
        // Present value
        match parser.parse("abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "bc");
                assert_eq!(parsed, Some('a'));
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
        
        // Absent value (should still succeed with None)
        match parser.parse("xyz") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "xyz");
                assert_eq!(parsed, None);
            }
            Err(e) => panic!("Expected successful parse, got error: {:?}", e),
        }
    }

    /// Test Scheme number parsing
    #[test]
    fn test_scheme_number_parsing() {
        let parser = scheme_number();
        
        // Integer
        match parser.parse("42 ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                // Verify it's parsed as a number (exact type depends on implementation)
            }
            Err(e) => panic!("Expected successful integer parse, got error: {:?}", e),
        }
        
        // Negative integer
        match parser.parse("-123 ") {
            Ok((remaining, _parsed)) => {
                assert_eq!(remaining, " ");
            }
            Err(e) => panic!("Expected successful negative integer parse, got error: {:?}", e),
        }
        
        // Floating point
        match parser.parse("3.14159 ") {
            Ok((remaining, _parsed)) => {
                assert_eq!(remaining, " ");
            }
            Err(e) => panic!("Expected successful float parse, got error: {:?}", e),
        }
        
        // Rational number (if supported)
        match parser.parse("22/7 ") {
            Ok((remaining, _parsed)) => {
                assert_eq!(remaining, " ");
            }
            Err(e) => panic!("Expected successful rational parse, got error: {:?}", e),
        }
    }

    /// Test Scheme string parsing
    #[test]
    fn test_scheme_string_parsing() {
        let parser = scheme_string();
        
        // Simple string
        match parser.parse("\"hello world\" ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "hello world");
            }
            Err(e) => panic!("Expected successful string parse, got error: {:?}", e),
        }
        
        // String with escape sequences
        match parser.parse("\"line1\\nline2\\t\\\"quoted\\\"\" ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "line1\nline2\t\"quoted\"");
            }
            Err(e) => panic!("Expected successful escaped string parse, got error: {:?}", e),
        }
        
        // Empty string
        match parser.parse("\"\" ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "");
            }
            Err(e) => panic!("Expected successful empty string parse, got error: {:?}", e),
        }
        
        // Unterminated string (should fail)
        match parser.parse("\"unterminated") {
            Ok(_) => panic!("Expected parse error for unterminated string"),
            Err(e) => {
                assert!(e.message.contains("unterminated") || e.message.contains("expected"));
            }
        }
    }

    /// Test Scheme character parsing
    #[test]
    fn test_scheme_character_parsing() {
        let parser = scheme_character();
        
        // Simple character
        match parser.parse("#\\a ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, 'a');
            }
            Err(e) => panic!("Expected successful character parse, got error: {:?}", e),
        }
        
        // Named character - newline
        match parser.parse("#\\newline ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, '\n');
            }
            Err(e) => panic!("Expected successful newline parse, got error: {:?}", e),
        }
        
        // Named character - space
        match parser.parse("#\\space ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, ' ');
            }
            Err(e) => panic!("Expected successful space parse, got error: {:?}", e),
        }
        
        // Named character - tab
        match parser.parse("#\\tab ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, '\t');
            }
            Err(e) => panic!("Expected successful tab parse, got error: {:?}", e),
        }
    }

    /// Test Scheme symbol parsing
    #[test]
    fn test_scheme_symbol_parsing() {
        let parser = scheme_symbol();
        
        // Simple symbol
        match parser.parse("hello ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "hello");
            }
            Err(e) => panic!("Expected successful symbol parse, got error: {:?}", e),
        }
        
        // Symbol with special characters
        match parser.parse("my-var? ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "my-var?");
            }
            Err(e) => panic!("Expected successful symbol with special chars parse, got error: {:?}", e),
        }
        
        // Symbol with numbers (not at start)
        match parser.parse("var123 ") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, " ");
                assert_eq!(parsed, "var123");
            }
            Err(e) => panic!("Expected successful symbol with numbers parse, got error: {:?}", e),
        }
        
        // Symbol starting with number (should fail)
        match parser.parse("123var") {
            Ok(_) => panic!("Expected parse error for symbol starting with number"),
            Err(_e) => {
                // Expected to fail
            }
        }
    }

    /// Test comment skipping
    #[test]
    fn test_comment_parsing() {
        let parser = CommentSkipper::new();
        
        // Line comment
        match parser.parse("; this is a comment\nrest") {
            Ok((remaining, _)) => {
                assert_eq!(remaining, "rest");
            }
            Err(e) => panic!("Expected successful comment skip, got error: {:?}", e),
        }
        
        // Block comment
        match parser.parse("#| block comment |# rest") {
            Ok((remaining, _)) => {
                assert_eq!(remaining, " rest");
            }
            Err(e) => panic!("Expected successful block comment skip, got error: {:?}", e),
        }
    }

    /// Test whitespace handling
    #[test]
    fn test_whitespace_parsing() {
        let parser = whitespace1();
        
        // Multiple whitespace characters
        match parser.parse("   \t\n  abc") {
            Ok((remaining, parsed)) => {
                assert_eq!(remaining, "abc");
                assert_eq!(parsed.len(), 7); // Should capture all whitespace
            }
            Err(e) => panic!("Expected successful whitespace parse, got error: {:?}", e),
        }
        
        // No whitespace (should fail for whitespace1)
        match parser.parse("abc") {
            Ok(_) => panic!("Expected parse error when no whitespace for whitespace1"),
            Err(_e) => {
                // Expected to fail
            }
        }
    }

    /// Test error span information
    #[test]
    fn test_error_span_information() {
        let parser = tag("hello");
        
        match parser.parse("help") {
            Ok(_) => panic!("Expected parse error"),
            Err(e) => {
                assert_eq!(e.span.start, 0);
                assert_eq!(e.span.end, 4); // Should span the entire input
                assert!(e.message.len() > 0);
                assert!(e.expected.len() > 0);
                assert_eq!(e.actual, "help");
            }
        }
    }

    /// Integration test: Parse a simple S-expression
    #[test]
    fn test_simple_sexp_integration() {
        let parser = whitespace0().and_then(|_| scheme_sexp());
        
        // Simple list
        match parser.parse("(+ 1 2)") {
            Ok((remaining, _parsed)) => {
                assert_eq!(remaining.trim(), "");
                // S-expression should be parsed correctly
            }
            Err(e) => panic!("Expected successful S-exp parse, got error: {:?}", e),
        }
        
        // Nested list
        match parser.parse("(if (> x 0) x (- x))") {
            Ok((remaining, _parsed)) => {
                assert_eq!(remaining.trim(), "");
            }
            Err(e) => panic!("Expected successful nested S-exp parse, got error: {:?}", e),
        }
    }

    /// Performance test: Parse large input efficiently
    #[test]
    fn test_performance_large_input() {
        let parser = digit().many();
        let large_input = "1".repeat(10000);
        
        let start_time = std::time::Instant::now();
        match parser.parse(&large_input) {
            Ok((remaining, parsed)) => {
                let duration = start_time.elapsed();
                assert_eq!(remaining, "");
                assert_eq!(parsed.len(), 10000);
                
                // Should complete in reasonable time (adjust threshold as needed)
                assert!(duration.as_millis() < 100, "Parse took too long: {:?}", duration);
            }
            Err(e) => panic!("Expected successful large input parse, got error: {:?}", e),
        }
    }
}