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
//! Literal expression parsing
//!
//! Handles parsing of primitive literal values:
//! - Integers with optional type suffixes (42, 100i32, 0xFF)
//! - Floats (3.15, 1e-5)
//! - Strings (regular and raw strings)
//! - F-strings with interpolation
//! - Characters ('a', '\n')
//! - Bytes (b'x')
//! - Booleans (true, false)
//!
//! Extracted from expressions.rs to improve maintainability (TDG Structural improvement).

use crate::frontend::ast::{Expr, ExprKind, Literal, Span};
use crate::frontend::error_recovery::ParseError;
use crate::frontend::lexer::Token;
use crate::frontend::parser::{bail, ParserState, Result};

// Import f-string parsing from string_operations module
use super::string_operations::parse_fstring_into_parts;

/// Parse literal tokens into expressions
///
/// Handles all primitive literal types with proper type suffix parsing for integers.
///
/// # Examples
/// ```ruchy
/// 42          // Integer
/// 3.15        // Float
/// "hello"     // String
/// 'a'         // Char
/// true        // Bool
/// f"x={x}"    // F-string
/// ```
pub(in crate::frontend::parser) fn parse_literal_token(
    state: &mut ParserState,
    token: &Token,
    span: Span,
) -> Result<Expr> {
    match token {
        Token::Integer(value_str) => {
            state.tokens.advance();
            // Parse integer value and optional type suffix
            let (num_part, type_suffix) =
                if let Some(pos) = value_str.find(|c: char| c.is_alphabetic()) {
                    (&value_str[..pos], Some(value_str[pos..].to_string()))
                } else {
                    (value_str.as_str(), None)
                };
            let value = num_part.parse::<i64>().map_err(|_| {
                ParseError::new(format!("Invalid integer literal: {num_part}"), span)
            })?;
            Ok(Expr::new(
                ExprKind::Literal(Literal::Integer(value, type_suffix)),
                span,
            ))
        }
        // Issue #168: Hexadecimal literal support (0xFF, 0x1A2B, etc.)
        Token::HexInteger(value_str) => {
            state.tokens.advance();
            // Parse hex value: strip 0x/0X prefix and optional type suffix
            let without_prefix = &value_str[2..]; // Skip "0x" or "0X"
            let (hex_part, type_suffix) = if let Some(pos) = without_prefix.find(['i', 'u']) {
                (
                    &without_prefix[..pos],
                    Some(without_prefix[pos..].to_string()),
                )
            } else {
                (without_prefix, None)
            };
            let value = i64::from_str_radix(hex_part, 16).map_err(|_| {
                ParseError::new(format!("Invalid hexadecimal literal: {value_str}"), span)
            })?;
            Ok(Expr::new(
                ExprKind::Literal(Literal::Integer(value, type_suffix)),
                span,
            ))
        }
        Token::Float(value) => {
            state.tokens.advance();
            Ok(Expr::new(ExprKind::Literal(Literal::Float(*value)), span))
        }
        Token::String(value) => {
            state.tokens.advance();
            Ok(Expr::new(
                ExprKind::Literal(Literal::String(value.clone())),
                span,
            ))
        }
        Token::RawString(value) => {
            state.tokens.advance();
            Ok(Expr::new(
                ExprKind::Literal(Literal::String(value.clone())),
                span,
            ))
        }
        Token::FString(template) => {
            state.tokens.advance();
            // Parse f-string template into parts with proper interpolation
            let parts = parse_fstring_into_parts(template)?;
            Ok(Expr::new(ExprKind::StringInterpolation { parts }, span))
        }
        Token::Char(value) => {
            state.tokens.advance();
            Ok(Expr::new(ExprKind::Literal(Literal::Char(*value)), span))
        }
        Token::Byte(value) => {
            state.tokens.advance();
            Ok(Expr::new(ExprKind::Literal(Literal::Byte(*value)), span))
        }
        Token::Bool(value) => {
            state.tokens.advance();
            Ok(Expr::new(ExprKind::Literal(Literal::Bool(*value)), span))
        }
        Token::Atom(value) => {
            state.tokens.advance();
            Ok(Expr::new(
                ExprKind::Literal(Literal::Atom(value.clone())),
                span,
            ))
        }
        _ => bail!("Expected literal token, got: {token:?}"),
    }
}

/// Parse Null literal
pub(in crate::frontend::parser) fn parse_null(state: &mut ParserState, span: Span) -> Result<Expr> {
    state.tokens.advance();
    Ok(Expr::new(ExprKind::Literal(Literal::Null), span))
}

/// Parse None literal
pub(in crate::frontend::parser) fn parse_none(state: &mut ParserState, span: Span) -> Result<Expr> {
    state.tokens.advance();
    Ok(Expr::new(ExprKind::None, span))
}

/// Parse Some constructor: Some(value)
pub(in crate::frontend::parser) fn parse_some_constructor(
    state: &mut ParserState,
    span: Span,
) -> Result<Expr> {
    use crate::frontend::parser::parse_expr_with_precedence_recursive;

    state.tokens.advance();
    if !matches!(state.tokens.peek(), Some((Token::LeftParen, _))) {
        bail!("Expected '(' after Some");
    }
    state.tokens.advance();
    let value = parse_expr_with_precedence_recursive(state, 0)?;
    if !matches!(state.tokens.peek(), Some((Token::RightParen, _))) {
        bail!("Expected ')' after Some value");
    }
    state.tokens.advance();
    Ok(Expr::new(
        ExprKind::Some {
            value: Box::new(value),
        },
        span,
    ))
}

#[cfg(test)]
mod tests {

    use crate::frontend::parser::Parser;

    #[test]
    fn test_integer_literal() {
        let code = "42";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Integer literal should parse");
    }

    #[test]
    fn test_integer_with_type_suffix() {
        let code = "100i32";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Integer with type suffix should parse");
    }

    #[test]
    fn test_float_literal() {
        let code = "3.15";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Float literal should parse");
    }

    #[test]
    fn test_string_literal() {
        let code = "\"hello world\"";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "String literal should parse");
    }

    #[test]
    fn test_char_literal() {
        let code = "'a'";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Char literal should parse");
    }

    #[test]
    fn test_bool_literal() {
        let code = "true";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Bool literal should parse");
    }

    #[test]
    fn test_atom_literal() {
        let code = ":status";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "Atom literal should parse");
    }

    #[test]
    fn test_fstring_literal() {
        let code = "f\"x={42}\"";
        let result = Parser::new(code).parse();
        assert!(result.is_ok(), "F-string should parse");
    }

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

    use crate::frontend::ast::{Expr, ExprKind, Literal};
    use crate::frontend::parser::Result;

    fn parse(code: &str) -> Result<Expr> {
        Parser::new(code).parse()
    }

    fn get_block_exprs(expr: &Expr) -> Option<&Vec<Expr>> {
        match &expr.kind {
            ExprKind::Block(exprs) => Some(exprs),
            _ => None,
        }
    }

    // ============================================================
    // Integer literals
    // ============================================================

    #[test]
    fn test_integer_zero() {
        let result = parse("0");
        assert!(result.is_ok(), "Zero should parse");
    }

    #[test]
    fn test_integer_one() {
        let result = parse("1");
        assert!(result.is_ok(), "One should parse");
    }

    #[test]
    fn test_integer_large() {
        let result = parse("999999999");
        assert!(result.is_ok(), "Large integer should parse");
    }

    #[test]
    fn test_integer_negative() {
        let result = parse("-42");
        assert!(result.is_ok(), "Negative integer should parse");
    }

    #[test]
    fn test_integer_suffix_i8() {
        let result = parse("42i8");
        assert!(result.is_ok(), "i8 suffix should parse");
    }

    #[test]
    fn test_integer_suffix_i16() {
        let result = parse("42i16");
        assert!(result.is_ok(), "i16 suffix should parse");
    }

    #[test]
    fn test_integer_suffix_i64() {
        let result = parse("42i64");
        assert!(result.is_ok(), "i64 suffix should parse");
    }

    #[test]
    fn test_integer_suffix_u8() {
        let result = parse("42u8");
        assert!(result.is_ok(), "u8 suffix should parse");
    }

    #[test]
    fn test_integer_suffix_u32() {
        let result = parse("42u32");
        assert!(result.is_ok(), "u32 suffix should parse");
    }

    #[test]
    fn test_integer_suffix_u64() {
        let result = parse("42u64");
        assert!(result.is_ok(), "u64 suffix should parse");
    }

    // ============================================================
    // Hex literals
    // ============================================================

    #[test]
    fn test_hex_zero() {
        let result = parse("0x0");
        assert!(result.is_ok(), "Hex zero should parse");
    }

    #[test]
    fn test_hex_lowercase() {
        let result = parse("0xff");
        assert!(result.is_ok(), "Hex lowercase should parse");
    }

    #[test]
    fn test_hex_uppercase() {
        let result = parse("0xFF");
        assert!(result.is_ok(), "Hex uppercase should parse");
    }

    #[test]
    fn test_hex_mixed_case() {
        let result = parse("0xAbCd");
        assert!(result.is_ok(), "Hex mixed case should parse");
    }

    #[test]
    fn test_hex_long() {
        let result = parse("0x123456");
        assert!(result.is_ok(), "Hex long should parse");
    }

    // ============================================================
    // Float literals
    // ============================================================

    #[test]
    fn test_float_zero() {
        let result = parse("0.0");
        assert!(result.is_ok(), "Float zero should parse");
    }

    #[test]
    fn test_float_one() {
        let result = parse("1.0");
        assert!(result.is_ok(), "Float one should parse");
    }

    #[test]
    fn test_float_pi() {
        let result = parse("3.14159");
        assert!(result.is_ok(), "Float pi should parse");
    }

    #[test]
    fn test_float_small() {
        let result = parse("0.001");
        assert!(result.is_ok(), "Small float should parse");
    }

    #[test]
    fn test_float_negative() {
        let result = parse("-3.14");
        assert!(result.is_ok(), "Negative float should parse");
    }

    // ============================================================
    // String literals
    // ============================================================

    #[test]
    fn test_string_empty() {
        let result = parse("\"\"");
        assert!(result.is_ok(), "Empty string should parse");
    }

    #[test]
    fn test_string_single_char() {
        let result = parse("\"a\"");
        assert!(result.is_ok(), "Single char string should parse");
    }

    #[test]
    fn test_string_with_spaces() {
        let result = parse("\"hello world\"");
        assert!(result.is_ok(), "String with spaces should parse");
    }

    #[test]
    fn test_string_with_numbers() {
        let result = parse("\"test123\"");
        assert!(result.is_ok(), "String with numbers should parse");
    }

    // ============================================================
    // Char literals
    // ============================================================

    #[test]
    fn test_char_letter() {
        let result = parse("'x'");
        assert!(result.is_ok(), "Char letter should parse");
    }

    #[test]
    fn test_char_digit() {
        let result = parse("'5'");
        assert!(result.is_ok(), "Char digit should parse");
    }

    #[test]
    fn test_char_space() {
        let result = parse("' '");
        assert!(result.is_ok(), "Char space should parse");
    }

    // ============================================================
    // Bool literals
    // ============================================================

    #[test]
    fn test_bool_true() {
        let result = parse("true");
        assert!(result.is_ok(), "True should parse");
    }

    #[test]
    fn test_bool_false() {
        let result = parse("false");
        assert!(result.is_ok(), "False should parse");
    }

    // ============================================================
    // None and Some
    // ============================================================

    #[test]
    fn test_none_literal() {
        let result = parse("None");
        assert!(result.is_ok(), "None should parse");
    }

    #[test]
    fn test_some_integer() {
        let result = parse("Some(42)");
        assert!(result.is_ok(), "Some with integer should parse");
    }

    #[test]
    fn test_some_string() {
        let result = parse("Some(\"hello\")");
        assert!(result.is_ok(), "Some with string should parse");
    }

    #[test]
    fn test_some_variable() {
        let result = parse("Some(x)");
        assert!(result.is_ok(), "Some with variable should parse");
    }

    // ============================================================
    // Atom literals
    // ============================================================

    #[test]
    fn test_atom_short() {
        let result = parse(":ok");
        assert!(result.is_ok(), "Short atom should parse");
    }

    #[test]
    fn test_atom_long() {
        let result = parse(":my_atom_name");
        assert!(result.is_ok(), "Long atom should parse");
    }

    #[test]
    fn test_atom_with_numbers() {
        let result = parse(":status_200");
        assert!(result.is_ok(), "Atom with numbers should parse");
    }

    // ============================================================
    // F-string literals
    // ============================================================

    #[test]
    fn test_fstring_simple() {
        let result = parse("f\"hello\"");
        assert!(result.is_ok(), "Simple f-string should parse");
    }

    #[test]
    fn test_fstring_with_var() {
        let result = parse("f\"hello {name}\"");
        assert!(result.is_ok(), "F-string with var should parse");
    }

    #[test]
    fn test_fstring_with_expr() {
        let result = parse("f\"sum = {a + b}\"");
        assert!(result.is_ok(), "F-string with expr should parse");
    }

    #[test]
    fn test_fstring_multiple() {
        let result = parse("f\"{x} + {y} = {z}\"");
        assert!(result.is_ok(), "F-string multiple should parse");
    }

    // ============================================================
    // Literal produces ExprKind::Literal
    // ============================================================

    #[test]
    fn test_integer_produces_literal_exprkind() {
        let expr = parse("42").unwrap();
        if let Some(exprs) = get_block_exprs(&expr) {
            assert!(
                matches!(&exprs[0].kind, ExprKind::Literal(Literal::Integer(42, _))),
                "Should produce Literal Integer"
            );
        }
    }

    #[test]
    fn test_bool_produces_literal_exprkind() {
        let expr = parse("true").unwrap();
        if let Some(exprs) = get_block_exprs(&expr) {
            assert!(
                matches!(&exprs[0].kind, ExprKind::Literal(Literal::Bool(true))),
                "Should produce Literal Bool"
            );
        }
    }

    // Property tests for literals
    #[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_integers_never_panic(n in any::<i32>()) {
                let code = format!("{n}");
                let _ = Parser::new(&code).parse(); // Should not panic
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_floats_never_panic(f in any::<f64>().prop_filter("finite", |x| x.is_finite())) {
                let code = format!("{f}");
                let _ = Parser::new(&code).parse(); // Should not panic
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_strings_never_panic(s in "\\PC*") {
                let code = format!("\"{}\"", s.replace('"', "\\\""));
                let _ = Parser::new(&code).parse(); // Should not panic
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_bools_always_parse(b in any::<bool>()) {
                let code = format!("{b}");
                let result = Parser::new(&code).parse();
                prop_assert!(result.is_ok());
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_integer_type_suffixes(n in any::<i32>(),
                                          suffix in prop::sample::select(vec!["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64"])) {
                let code = format!("{n}{suffix}");
                let result = Parser::new(&code).parse();
                prop_assert!(result.is_ok(), "Integer with suffix {} should parse", suffix);
            }

            #[test]
            #[ignore = "Property tests run with --ignored flag"]
            fn prop_hex_integers_parse(n in 0u32..=0xFFFF) {
                let code = format!("0x{n:X}");
                let result = Parser::new(&code).parse();
                prop_assert!(result.is_ok(), "Hex integer 0x{:X} should parse", n);
            }
        }
    }
}