ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! String operation parsing
//!
//! Handles parsing of string interpolation and f-string expressions.
//! F-strings allow embedding Ruchy expressions within string literals using `{}` syntax.
//!
//! # Syntax
//! ```ruchy
//! f"Hello {name}"                    // Simple interpolation
//! f"Value: {x + 1}"                  // Expression interpolation
//! f"Formatted: {value:precision}"    // With format specifier
//! f"Escaped: {{"                     // Escaped braces
//! ```
//!
//! # Features
//! - Expression interpolation: `{expr}`
//! - Format specifiers: `{expr:spec}`
//! - Escaped braces: `{{` and `}}`
//! - Nested expressions with proper brace matching
//!
//! Extracted from expressions.rs to improve maintainability (TDG Structural improvement).

use crate::frontend::ast::StringPart;
use crate::frontend::parser::Result;
use anyhow::bail;

/// Parse f-string into interpolation parts
///
/// Converts an f-string template into a sequence of text and expression parts.
/// Handles brace escaping and nested expressions.
///
/// # Examples
/// ```ruchy
/// f"Hello {name}"           // Text("Hello "), Expr(name)
/// f"Value {{literal}}"      // Text("Value {literal}")
/// f"Sum: {a + b}"           // Text("Sum: "), Expr(a + b)
/// ```
pub(in crate::frontend::parser) fn parse_fstring_into_parts(
    input: &str,
) -> Result<Vec<StringPart>> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '{' => handle_opening_brace(&mut chars, &mut parts, &mut current)?,
            '}' => handle_closing_brace(&mut chars, &mut current)?,
            _ => current.push(ch),
        }
    }

    if !current.is_empty() {
        parts.push(StringPart::Text(current));
    }

    Ok(parts)
}

/// Handle opening brace in f-string
///
/// Distinguishes between:
/// - `{{` - Escaped literal brace
/// - `{expr}` - Expression interpolation
fn handle_opening_brace(
    chars: &mut std::iter::Peekable<std::str::Chars>,
    parts: &mut Vec<StringPart>,
    current: &mut String,
) -> Result<()> {
    if chars.peek() == Some(&'{') {
        // Escaped brace: {{ → {
        chars.next();
        current.push('{');
    } else {
        // Start interpolation
        flush_text_part(parts, current);
        let expr_str = extract_fstring_expr(chars)?;
        parts.push(parse_interpolation(&expr_str)?);
    }
    Ok(())
}

/// Handle closing brace in f-string
///
/// Distinguishes between:
/// - `}}` - Escaped literal brace
/// - `}` - Unmatched closing brace (error)
fn handle_closing_brace(
    chars: &mut std::iter::Peekable<std::str::Chars>,
    current: &mut String,
) -> Result<()> {
    if chars.peek() == Some(&'}') {
        // Escaped brace: }} → }
        chars.next();
        current.push('}');
        Ok(())
    } else {
        bail!("Unmatched '}}' in f-string")
    }
}

/// Flush accumulated text as a `StringPart`
fn flush_text_part(parts: &mut Vec<StringPart>, current: &mut String) {
    if !current.is_empty() {
        parts.push(StringPart::Text(current.clone()));
        current.clear();
    }
}

/// Parse interpolation expression with optional format specifier
///
/// Supports:
/// - `{expr}` - Simple expression
/// - `{expr:format}` - Expression with format specifier
/// - `{}` - Empty placeholder (positional argument)
///
/// Note: Don't confuse `::` (path separator) with `:` (format specifier)
fn parse_interpolation(expr_str: &str) -> Result<StringPart> {
    use crate::frontend::parser::Parser;

    // DEFECT-PARSER-012 FIX: Handle empty placeholders {} for positional arguments
    if expr_str.is_empty() {
        // Empty {} is a positional placeholder - create a placeholder expression
        return Ok(StringPart::Text("{}".to_string()));
    }

    // Find single colon (format specifier) while ignoring :: (path separator)
    let colon_pos = find_format_specifier_colon(expr_str);

    if let Some(pos) = colon_pos {
        // Expression with format specifier: {expr:spec}
        let expr_part = &expr_str[..pos];
        let format_spec = &expr_str[pos..];
        let mut parser = Parser::new(expr_part);
        let expr = parser.parse_expr()?;
        Ok(StringPart::ExprWithFormat {
            expr: Box::new(expr),
            format_spec: format_spec.to_string(),
        })
    } else {
        // Simple expression: {expr}
        let mut parser = Parser::new(expr_str);
        let expr = parser.parse_expr()?;
        Ok(StringPart::Expr(Box::new(expr)))
    }
}

/// Find the position of a format specifier colon, ignoring :: path separators
fn find_format_specifier_colon(expr_str: &str) -> Option<usize> {
    let mut chars = expr_str.char_indices().peekable();

    while let Some((i, ch)) = chars.next() {
        if ch == ':' {
            // Check if next char is also ':' (path separator ::)
            if let Some((_, next_ch)) = chars.peek() {
                if *next_ch == ':' {
                    // This is ::, skip both colons
                    chars.next();
                    continue;
                }
            }
            // Single colon found - this is a format specifier
            // But only if the part before it doesn't contain quotes
            let before_colon = &expr_str[..i];
            if !before_colon.contains('"') && !before_colon.contains('\'') {
                return Some(i);
            }
        }
    }
    None
}

/// Extract expression from f-string interpolation
///
/// Handles nested braces by tracking depth.
/// Extracts text between `{` and matching `}`.
fn extract_fstring_expr(chars: &mut std::iter::Peekable<std::str::Chars>) -> Result<String> {
    let mut expr = String::new();
    let mut depth = 1;
    for ch in chars.by_ref() {
        if ch == '{' {
            depth += 1;
            expr.push(ch);
        } else if ch == '}' {
            depth -= 1;
            if depth == 0 {
                return Ok(expr);
            }
            expr.push(ch);
        } else {
            expr.push(ch);
        }
    }
    bail!("Unclosed interpolation in f-string")
}

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

    #[test]
    fn test_simple_text() {
        let result = parse_fstring_into_parts("Hello World");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 1);
        assert!(matches!(parts[0], StringPart::Text(_)));
    }

    #[test]
    fn test_simple_interpolation() {
        let result = parse_fstring_into_parts("Hello {name}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 2);
        assert!(matches!(parts[0], StringPart::Text(_)));
        assert!(matches!(parts[1], StringPart::Expr(_)));
    }

    #[test]
    fn test_escaped_braces() {
        let result = parse_fstring_into_parts("Value {{literal}}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 1);
        if let StringPart::Text(text) = &parts[0] {
            assert_eq!(text, "Value {literal}");
        } else {
            panic!("Expected Text part");
        }
    }

    #[test]
    fn test_format_specifier() {
        let result = parse_fstring_into_parts("{value:.2f}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 1);
        assert!(matches!(parts[0], StringPart::ExprWithFormat { .. }));
    }

    #[test]
    fn test_empty_placeholder() {
        let result = parse_fstring_into_parts("Value: {}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 2);
        assert!(matches!(parts[0], StringPart::Text(_)));
        assert!(matches!(parts[1], StringPart::Text(_)));
    }

    #[test]
    fn test_multiple_interpolations() {
        let result = parse_fstring_into_parts("{a} + {b} = {c}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 5); // expr, text, expr, text, expr
    }

    #[test]
    fn test_unmatched_closing_brace() {
        let result = parse_fstring_into_parts("Value }");
        assert!(result.is_err());
    }

    #[test]
    fn test_unclosed_interpolation() {
        let result = parse_fstring_into_parts("Value {name");
        assert!(result.is_err());
    }

    #[test]
    fn test_nested_braces() {
        let result = parse_fstring_into_parts("Result: {obj.method()}");
        assert!(result.is_ok());
        let parts = result.expect("operation should succeed in test");
        assert_eq!(parts.len(), 2);
    }

    // ============================================================
    // Additional comprehensive tests for EXTREME TDD coverage
    // ============================================================

    // ============================================================
    // Text-only variations
    // ============================================================

    #[test]
    fn test_empty_string() {
        let result = parse_fstring_into_parts("");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert_eq!(parts.len(), 0);
    }

    #[test]
    fn test_single_char() {
        let result = parse_fstring_into_parts("a");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert_eq!(parts.len(), 1);
    }

    #[test]
    fn test_long_text() {
        let result = parse_fstring_into_parts("This is a very long string with many words");
        assert!(result.is_ok());
    }

    #[test]
    fn test_text_with_spaces() {
        let result = parse_fstring_into_parts("   spaced   ");
        assert!(result.is_ok());
    }

    #[test]
    fn test_text_with_numbers() {
        let result = parse_fstring_into_parts("Value 123 and 456");
        assert!(result.is_ok());
    }

    #[test]
    fn test_text_with_punctuation() {
        let result = parse_fstring_into_parts("Hello, world! How are you?");
        assert!(result.is_ok());
    }

    // ============================================================
    // Single interpolation variations
    // ============================================================

    #[test]
    fn test_interpolation_at_start() {
        let result = parse_fstring_into_parts("{name} is here");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert!(matches!(parts[0], StringPart::Expr(_)));
    }

    #[test]
    fn test_interpolation_at_end() {
        let result = parse_fstring_into_parts("Hello {name}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_only() {
        let result = parse_fstring_into_parts("{name}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert_eq!(parts.len(), 1);
    }

    #[test]
    fn test_interpolation_single_letter() {
        let result = parse_fstring_into_parts("{x}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_long_name() {
        let result = parse_fstring_into_parts("{very_long_variable_name}");
        assert!(result.is_ok());
    }

    // ============================================================
    // Multiple interpolations
    // ============================================================

    #[test]
    fn test_two_interpolations() {
        let result = parse_fstring_into_parts("{a} and {b}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert_eq!(parts.len(), 3); // expr, text, expr
    }

    #[test]
    fn test_three_interpolations() {
        let result = parse_fstring_into_parts("{a}{b}{c}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert_eq!(parts.len(), 3); // expr, expr, expr
    }

    #[test]
    fn test_adjacent_interpolations() {
        let result = parse_fstring_into_parts("{x}{y}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_spaced_interpolations() {
        let result = parse_fstring_into_parts("{a} {b} {c}");
        assert!(result.is_ok());
    }

    // ============================================================
    // Expression interpolations
    // ============================================================

    #[test]
    fn test_interpolation_addition() {
        let result = parse_fstring_into_parts("{a + b}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_multiplication() {
        let result = parse_fstring_into_parts("{x * 2}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_comparison() {
        let result = parse_fstring_into_parts("{x > 0}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_function_call() {
        let result = parse_fstring_into_parts("{foo()}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_method_call() {
        let result = parse_fstring_into_parts("{obj.method()}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_interpolation_field_access() {
        let result = parse_fstring_into_parts("{obj.field}");
        assert!(result.is_ok());
    }

    // ============================================================
    // Format specifiers
    // ============================================================

    #[test]
    fn test_format_decimal() {
        let result = parse_fstring_into_parts("{x:.2f}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        assert!(matches!(parts[0], StringPart::ExprWithFormat { .. }));
    }

    #[test]
    fn test_format_width() {
        let result = parse_fstring_into_parts("{x:10}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_format_zero_pad() {
        let result = parse_fstring_into_parts("{x:05}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_format_hex() {
        let result = parse_fstring_into_parts("{x:x}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_format_binary() {
        let result = parse_fstring_into_parts("{x:b}");
        assert!(result.is_ok());
    }

    // ============================================================
    // Escaped braces
    // ============================================================

    #[test]
    fn test_escaped_open_brace() {
        let result = parse_fstring_into_parts("{{");
        assert!(result.is_ok());
        let parts = result.unwrap();
        if let StringPart::Text(t) = &parts[0] {
            assert_eq!(t, "{");
        }
    }

    #[test]
    fn test_escaped_close_brace() {
        let result = parse_fstring_into_parts("}}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        if let StringPart::Text(t) = &parts[0] {
            assert_eq!(t, "}");
        }
    }

    #[test]
    fn test_escaped_both() {
        let result = parse_fstring_into_parts("{{}}");
        assert!(result.is_ok());
        let parts = result.unwrap();
        if let StringPart::Text(t) = &parts[0] {
            assert_eq!(t, "{}");
        }
    }

    #[test]
    fn test_escaped_with_text() {
        let result = parse_fstring_into_parts("a{{b}}c");
        assert!(result.is_ok());
    }

    #[test]
    fn test_escaped_multiple() {
        let result = parse_fstring_into_parts("{{{{}}}}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_escaped_mixed_with_interpolation() {
        let result = parse_fstring_into_parts("{{x}} = {x}");
        assert!(result.is_ok());
    }

    // ============================================================
    // Error cases
    // ============================================================

    #[test]
    fn test_unmatched_open() {
        let result = parse_fstring_into_parts("{x");
        assert!(result.is_err());
    }

    #[test]
    fn test_unmatched_close() {
        let result = parse_fstring_into_parts("x}");
        assert!(result.is_err());
    }

    #[test]
    fn test_unmatched_close_middle() {
        let result = parse_fstring_into_parts("a } b");
        assert!(result.is_err());
    }

    // Property tests for string operations
    #[cfg(test)]
    mod property_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            #[ignore = "Property tests run with --ignored flag"] // Run with: cargo test property_tests -- --ignored
            fn prop_plain_text_always_parses(text in "[a-zA-Z0-9 ]{0,100}") {
                let result = parse_fstring_into_parts(&text);
                prop_assert!(result.is_ok(), "Plain text should always parse");
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_escaped_braces_parse(n in 0..10usize) {
                let text = "{{".repeat(n);
                let result = parse_fstring_into_parts(&text);
                prop_assert!(result.is_ok(), "Escaped braces should parse");
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_simple_variable_interpolation(var in "[a-z][a-z0-9]{0,10}") {
                let text = format!("Value: {{{var}}}");
                let result = parse_fstring_into_parts(&text);
                prop_assert!(result.is_ok(), "Variable interpolation {} should parse", text);
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_multiple_interpolations(n in 1..5usize) {
                let text = (0..n).map(|i| format!("{{x{i}}}")).collect::<Vec<_>>().join(" ");
                let result = parse_fstring_into_parts(&text);
                prop_assert!(result.is_ok(), "Multiple interpolations should parse");
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_unmatched_closing_brace_fails(text in "[a-zA-Z0-9]{0,10}") {
                let bad_text = format!("{text}}}");
                let result = parse_fstring_into_parts(&bad_text);
                prop_assert!(result.is_err(), "Unmatched }} should fail");
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_empty_string_parses(_n in 0..100) {
                let result = parse_fstring_into_parts("");
                prop_assert!(result.is_ok(), "Empty string should parse");
                let parts = result.expect("operation should succeed in test");
                prop_assert_eq!(parts.len(), 0, "Empty string should have no parts");
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_escaped_sequences_have_half_braces(n in 1..10usize) {
                let input = "{{".repeat(n);
                let result = parse_fstring_into_parts(&input);
                prop_assert!(result.is_ok());
                let parts = result.expect("operation should succeed in test");
                if let Some(StringPart::Text(text)) = parts.first() {
                    prop_assert_eq!(text.len(), n, "Escaped {{{{ should produce half as many braces");
                }
            }
        }
    }
}