ruchy 4.1.1

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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//! Import and export parsing functions
//!
//! This module contains all import/export parsing logic extracted from utils.rs
//! to reduce file complexity and improve maintainability.

use super::super::{bail, Expr, ExprKind, ImportItem, ParserState, Result, Span, Token};
use super::url_validation::validate_url_import;

/// Parse import statement (legacy import parser)
///
/// NOTE: This is the legacy import parser. New imports are parsed in expressions.rs
#[allow(dead_code)]
pub fn parse_import_legacy(state: &mut ParserState) -> Result<Expr> {
    // Consume the Import token first (required by new parser)
    state.tokens.expect(&Token::Import)?;
    // Check if it's JS-style import
    if matches!(state.tokens.peek(), Some((Token::LeftBrace, _))) {
        crate::frontend::parser::imports::parse_js_style_import(state)
    } else {
        // Delegate to the new import parser in expressions.rs
        crate::frontend::parser::imports::parse_import_statement(state)
    }
}

/// Parse URL import statement (complexity: 6)
pub fn parse_url_import(state: &mut ParserState, url: &str, start_span: Span) -> Result<Expr> {
    // Validate URL format
    if !url.starts_with("https://") && !url.starts_with("http://") {
        bail!("URL imports must start with 'https://' or 'http://'");
    }
    // Safety validation for URL imports
    validate_url_import(url)?;
    state.tokens.advance();
    // URL imports become simple module imports
    Ok(Expr::new(
        ExprKind::Import {
            module: url.to_string(),
            items: None, // URL imports import everything
        },
        start_span,
    ))
}

/// Parse module path components
pub fn parse_module_path(state: &mut ParserState) -> Result<Vec<String>> {
    let Some((Token::Identifier(_), _)) = state.tokens.peek() else {
        return Ok(Vec::new());
    };

    let mut path_parts = vec![consume_module_identifier(state)?];
    parse_additional_path_segments(state, &mut path_parts)?;
    Ok(path_parts)
}

/// Consume and return module identifier
fn consume_module_identifier(state: &mut ParserState) -> Result<String> {
    let Some((Token::Identifier(name), _)) = state.tokens.peek() else {
        bail!("Expected identifier");
    };

    let name = name.clone();
    state.tokens.advance();
    Ok(name)
}

/// Parse additional path segments after initial identifier
fn parse_additional_path_segments(
    state: &mut ParserState,
    path_parts: &mut Vec<String>,
) -> Result<()> {
    while matches!(state.tokens.peek(), Some((Token::ColonColon, _))) {
        state.tokens.advance(); // consume ::

        if is_import_items_start(state) {
            break;
        }

        path_parts.push(parse_path_segment(state)?);
    }
    Ok(())
}

/// Check if current position is start of import items (complexity: 2)
fn is_import_items_start(state: &mut ParserState) -> bool {
    matches!(
        state.tokens.peek(),
        Some((Token::Star | Token::LeftBrace, _))
    )
}

/// Parse single path segment after :: (complexity: 3)
fn parse_path_segment(state: &mut ParserState) -> Result<String> {
    if let Some((Token::Identifier(name), _)) = state.tokens.peek() {
        let name = name.clone();
        state.tokens.advance();
        Ok(name)
    } else {
        bail!("Expected identifier, '*', or '{{' after '::'");
    }
}

/// Parse import items (wildcard, braced list, or simple) (complexity: 9)
pub fn parse_import_items(
    state: &mut ParserState,
    path_parts: &[String],
) -> Result<Vec<ImportItem>> {
    if matches!(state.tokens.peek(), Some((Token::Star, _))) {
        parse_wildcard_import(state)
    } else if matches!(state.tokens.peek(), Some((Token::LeftBrace, _))) {
        parse_braced_import_list(state)
    } else {
        parse_simple_import(state, path_parts)
    }
}

/// Parse wildcard import (* syntax) (complexity: 2)
fn parse_wildcard_import(state: &mut ParserState) -> Result<Vec<ImportItem>> {
    state.tokens.advance(); // consume *
    Ok(vec![ImportItem::Wildcard])
}

/// Parse braced import list ({item1, item2, ...})
pub fn parse_braced_import_list(state: &mut ParserState) -> Result<Vec<ImportItem>> {
    state.tokens.expect(&Token::LeftBrace)?;
    let items = parse_import_item_list(state)?;
    state.tokens.expect(&Token::RightBrace)?;
    Ok(items)
}

/// Parse list of import items (extracted to reduce nesting)
fn parse_import_item_list(state: &mut ParserState) -> Result<Vec<ImportItem>> {
    let mut items = Vec::new();
    while !matches!(state.tokens.peek(), Some((Token::RightBrace, _))) {
        items.push(parse_single_import_item(state)?);

        if !try_consume_item_separator(state)? {
            break;
        }
    }
    Ok(items)
}

/// Parse a single import item
fn parse_single_import_item(state: &mut ParserState) -> Result<ImportItem> {
    let Some((Token::Identifier(name), _)) = state.tokens.peek() else {
        if matches!(state.tokens.peek(), Some((Token::RightBrace, _))) {
            bail!("Expected identifier or '}}' in import list");
        }
        bail!("Expected identifier in import list");
    };

    let name = name.clone();
    state.tokens.advance();
    parse_item_with_optional_alias(state, name)
}

/// Parse import item with optional alias
fn parse_item_with_optional_alias(state: &mut ParserState, name: String) -> Result<ImportItem> {
    if !matches!(state.tokens.peek(), Some((Token::As, _))) {
        return Ok(ImportItem::Named(name));
    }

    state.tokens.advance(); // consume as
    let Some((Token::Identifier(alias), _)) = state.tokens.peek() else {
        bail!("Expected alias name after 'as'");
    };

    let alias = alias.clone();
    state.tokens.advance();
    Ok(ImportItem::Aliased { name, alias })
}

/// Try to consume item separator (returns false if at end)
fn try_consume_item_separator(state: &mut ParserState) -> Result<bool> {
    match state.tokens.peek() {
        Some((Token::Comma, _)) => {
            state.tokens.advance();
            Ok(true)
        }
        Some((Token::RightBrace, _)) => Ok(false),
        _ => bail!("Expected ',' or '}}' in import list"),
    }
}

/// Parse simple import (path or path as alias) (complexity: 8)
fn parse_simple_import(state: &mut ParserState, path_parts: &[String]) -> Result<Vec<ImportItem>> {
    if matches!(state.tokens.peek(), Some((Token::As, _))) {
        parse_simple_import_with_alias(state, path_parts)
    } else {
        parse_simple_import_without_alias(path_parts)
    }
}

/// Parse simple import with alias (complexity: 5)
fn parse_simple_import_with_alias(
    state: &mut ParserState,
    path_parts: &[String],
) -> Result<Vec<ImportItem>> {
    state.tokens.advance(); // consume as
    if let Some((Token::Identifier(alias), _)) = state.tokens.peek() {
        let alias = alias.clone();
        state.tokens.advance();
        Ok(vec![ImportItem::Aliased {
            name: path_parts.last().unwrap_or(&String::new()).clone(),
            alias,
        }])
    } else {
        bail!("Expected alias name after 'as'");
    }
}

/// Parse simple import without alias (complexity: 5)
fn parse_simple_import_without_alias(path_parts: &[String]) -> Result<Vec<ImportItem>> {
    if path_parts.is_empty() {
        Ok(Vec::new())
    } else if path_parts.len() == 1 {
        // Single segment - treat as wildcard
        Ok(Vec::new())
    } else {
        // Multi-segment - import the last part
        Ok(vec![ImportItem::Named(
            path_parts
                .last()
                .expect("checked: !path_parts.is_empty()")
                .clone(),
        )])
    }
}

/// Create final import expression (complexity: 4)
pub fn create_import_expression(
    path_parts: Vec<String>,
    _items: Vec<ImportItem>,
    start_span: Span,
) -> Result<Expr> {
    let module = path_parts.join("::");
    // Validate that we have a module
    if module.is_empty() {
        bail!("Expected import path after 'import'");
    }
    // Legacy import - convert to simple module import
    Ok(Expr::new(
        ExprKind::Import {
            module,
            items: None, // Legacy imports use None for now
        },
        start_span,
    ))
}

// Export parsing functions

/// Parse export statement
///
/// # Errors
///
/// Returns an error if the export statement is malformed or contains invalid syntax.
pub fn parse_export(state: &mut ParserState) -> Result<Expr> {
    let start_span = state.tokens.advance().expect("checked by parser logic").1;

    match state.tokens.peek() {
        Some((Token::Default, _)) => parse_export_default(state, start_span),
        Some((Token::LeftBrace, _)) => parse_export_list(state, start_span),
        Some((Token::Fun | Token::Const | Token::Let | Token::Class | Token::Struct, _)) => {
            parse_export_declaration(state, start_span)
        }
        _ => bail!("Invalid export statement"),
    }
}

/// Parse export default statement
fn parse_export_default(state: &mut ParserState, start_span: Span) -> Result<Expr> {
    state.tokens.advance(); // consume default
    let expr = crate::frontend::parser::parse_expr_with_precedence_recursive(state, 0)?;
    Ok(Expr::new(
        ExprKind::ExportDefault {
            expr: Box::new(expr),
        },
        start_span,
    ))
}

/// Parse export list statement
fn parse_export_list(state: &mut ParserState, start_span: Span) -> Result<Expr> {
    state.tokens.advance(); // consume {
    let items = parse_export_identifier_list(state)?;
    state.tokens.expect(&Token::RightBrace)?;

    create_export_or_reexport(state, items, start_span)
}

/// Parse list of export identifiers
fn parse_export_identifier_list(state: &mut ParserState) -> Result<Vec<String>> {
    let mut items = Vec::new();

    while !matches!(state.tokens.peek(), Some((Token::RightBrace, _))) {
        items.push(parse_export_identifier(state)?);
        try_consume_export_comma(state);
    }

    Ok(items)
}

/// Parse single export identifier
fn parse_export_identifier(state: &mut ParserState) -> Result<String> {
    let Some((Token::Identifier(name), _)) = state.tokens.peek() else {
        bail!("Expected identifier in export list");
    };

    let name = name.clone();
    state.tokens.advance();
    Ok(name)
}

/// Try to consume optional comma in export list
fn try_consume_export_comma(state: &mut ParserState) {
    if matches!(state.tokens.peek(), Some((Token::Comma, _))) {
        state.tokens.advance();
    }
}

/// Create export or re-export expression based on 'from' keyword
fn create_export_or_reexport(
    state: &mut ParserState,
    items: Vec<String>,
    start_span: Span,
) -> Result<Expr> {
    if !matches!(state.tokens.peek(), Some((Token::From, _))) {
        return Ok(Expr::new(ExprKind::ExportList { names: items }, start_span));
    }

    state.tokens.advance();
    let module = parse_module_specifier(state)?;
    Ok(Expr::new(ExprKind::ReExport { items, module }, start_span))
}

/// Parse module specifier (for re-exports)
fn parse_module_specifier(state: &mut ParserState) -> Result<String> {
    match state.tokens.peek() {
        Some((Token::String(module), _)) => {
            let module = module.clone();
            state.tokens.advance();
            Ok(module)
        }
        Some((Token::Identifier(module), _)) => {
            let module = module.clone();
            state.tokens.advance();
            Ok(module)
        }
        _ => bail!("Expected module path after 'from'"),
    }
}

/// Parse export declaration
fn parse_export_declaration(state: &mut ParserState, start_span: Span) -> Result<Expr> {
    let expr = crate::frontend::parser::parse_expr_with_precedence_recursive(state, 0)?;
    Ok(Expr::new(
        ExprKind::Export {
            expr: Box::new(expr),
            is_default: false,
        },
        start_span,
    ))
}

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

    // Helper to create parser state from source
    fn create_state(source: &str) -> ParserState {
        ParserState::new(source)
    }

    // parse_module_path tests
    #[test]
    fn test_parse_module_path_simple() {
        let mut state = create_state("foo");
        let path = parse_module_path(&mut state).expect("should succeed");
        assert_eq!(path, vec!["foo"]);
    }

    #[test]
    fn test_parse_module_path_nested() {
        let mut state = create_state("foo::bar::baz");
        let path = parse_module_path(&mut state).expect("should succeed");
        assert_eq!(path, vec!["foo", "bar", "baz"]);
    }

    #[test]
    fn test_parse_module_path_empty() {
        let mut state = create_state("");
        let path = parse_module_path(&mut state).expect("should succeed");
        assert!(path.is_empty());
    }

    #[test]
    fn test_parse_module_path_stops_at_star() {
        let mut state = create_state("foo::*");
        let path = parse_module_path(&mut state).expect("should succeed");
        assert_eq!(path, vec!["foo"]);
    }

    #[test]
    fn test_parse_module_path_stops_at_brace() {
        let mut state = create_state("foo::{bar}");
        let path = parse_module_path(&mut state).expect("should succeed");
        assert_eq!(path, vec!["foo"]);
    }

    // parse_import_items tests
    #[test]
    fn test_parse_import_items_wildcard() {
        let mut state = create_state("*");
        let items = parse_import_items(&mut state, &[]).expect("should succeed");
        assert_eq!(items.len(), 1);
        assert!(matches!(items[0], ImportItem::Wildcard));
    }

    #[test]
    fn test_parse_import_items_braced_single() {
        let mut state = create_state("{foo}");
        let items = parse_import_items(&mut state, &[]).expect("should succeed");
        assert_eq!(items.len(), 1);
        assert!(matches!(&items[0], ImportItem::Named(n) if n == "foo"));
    }

    #[test]
    fn test_parse_import_items_braced_multiple() {
        let mut state = create_state("{foo, bar, baz}");
        let items = parse_import_items(&mut state, &[]).expect("should succeed");
        assert_eq!(items.len(), 3);
    }

    #[test]
    fn test_parse_import_items_braced_with_alias() {
        let mut state = create_state("{foo as f}");
        let items = parse_import_items(&mut state, &[]).expect("should succeed");
        assert_eq!(items.len(), 1);
        if let ImportItem::Aliased { name, alias } = &items[0] {
            assert_eq!(name, "foo");
            assert_eq!(alias, "f");
        } else {
            panic!("Expected Aliased import");
        }
    }

    #[test]
    fn test_parse_import_items_simple_with_path() {
        let mut state = create_state("");
        let items = parse_import_items(&mut state, &["foo".to_string(), "bar".to_string()])
            .expect("should succeed");
        assert_eq!(items.len(), 1);
        assert!(matches!(&items[0], ImportItem::Named(n) if n == "bar"));
    }

    #[test]
    fn test_parse_import_items_simple_empty_path() {
        let mut state = create_state("");
        let items = parse_import_items(&mut state, &[]).expect("should succeed");
        assert!(items.is_empty());
    }

    #[test]
    fn test_parse_import_items_single_segment_path() {
        let mut state = create_state("");
        let items = parse_import_items(&mut state, &["foo".to_string()]).expect("should succeed");
        assert!(items.is_empty()); // Single segment treated as wildcard
    }

    // parse_braced_import_list tests
    #[test]
    fn test_parse_braced_import_list_empty() {
        let mut state = create_state("{}");
        let items = parse_braced_import_list(&mut state).expect("should succeed");
        assert!(items.is_empty());
    }

    #[test]
    fn test_parse_braced_import_list_trailing_comma() {
        let mut state = create_state("{foo,}");
        // This may or may not be valid depending on grammar
        let result = parse_braced_import_list(&mut state);
        // Either succeeds or errors predictably
        assert!(result.is_ok() || result.is_err());
    }

    // create_import_expression tests
    #[test]
    fn test_create_import_expression_simple() {
        let path = vec!["foo".to_string()];
        let items = vec![];
        let span = Span::default();
        let expr = create_import_expression(path, items, span).expect("should succeed");
        if let ExprKind::Import { module, .. } = &expr.kind {
            assert_eq!(module, "foo");
        } else {
            panic!("Expected Import expression");
        }
    }

    #[test]
    fn test_create_import_expression_nested() {
        let path = vec!["foo".to_string(), "bar".to_string(), "baz".to_string()];
        let items = vec![];
        let span = Span::default();
        let expr = create_import_expression(path, items, span).expect("should succeed");
        if let ExprKind::Import { module, .. } = &expr.kind {
            assert_eq!(module, "foo::bar::baz");
        } else {
            panic!("Expected Import expression");
        }
    }

    #[test]
    fn test_create_import_expression_empty_path_error() {
        let path = vec![];
        let items = vec![];
        let span = Span::default();
        let result = create_import_expression(path, items, span);
        assert!(result.is_err());
    }

    // parse_url_import tests
    #[test]
    fn test_parse_url_import_invalid_scheme() {
        let mut state = create_state("");
        let result = parse_url_import(&mut state, "ftp://example.com", Span::default());
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("https://") || err.contains("http://"));
    }

    // parse_export tests
    #[test]
    fn test_parse_export_default() {
        let mut state = create_state("export default 42");
        let expr = parse_export(&mut state).expect("should succeed");
        assert!(matches!(expr.kind, ExprKind::ExportDefault { .. }));
    }

    #[test]
    fn test_parse_export_list_simple() {
        let mut state = create_state("export {foo}");
        let expr = parse_export(&mut state).expect("should succeed");
        if let ExprKind::ExportList { names } = &expr.kind {
            assert_eq!(names, &vec!["foo".to_string()]);
        } else {
            panic!("Expected ExportList");
        }
    }

    #[test]
    fn test_parse_export_list_multiple() {
        let mut state = create_state("export {foo, bar}");
        let expr = parse_export(&mut state).expect("should succeed");
        if let ExprKind::ExportList { names } = &expr.kind {
            assert_eq!(names.len(), 2);
        } else {
            panic!("Expected ExportList");
        }
    }

    #[test]
    fn test_parse_export_invalid() {
        let mut state = create_state("export 123");
        let result = parse_export(&mut state);
        // Number literal is not a valid export target
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_export_function() {
        let mut state = create_state("export fun foo() { 42 }");
        let expr = parse_export(&mut state).expect("should succeed");
        assert!(matches!(expr.kind, ExprKind::Export { .. }));
    }

    // is_import_items_start tests
    #[test]
    fn test_is_import_items_start_star() {
        let mut state = create_state("*");
        assert!(is_import_items_start(&mut state));
    }

    #[test]
    fn test_is_import_items_start_brace() {
        let mut state = create_state("{");
        assert!(is_import_items_start(&mut state));
    }

    #[test]
    fn test_is_import_items_start_identifier() {
        let mut state = create_state("foo");
        assert!(!is_import_items_start(&mut state));
    }

    // parse_path_segment tests
    #[test]
    fn test_parse_path_segment_valid() {
        let mut state = create_state("foo");
        let segment = parse_path_segment(&mut state).expect("should succeed");
        assert_eq!(segment, "foo");
    }

    #[test]
    fn test_parse_path_segment_invalid() {
        let mut state = create_state("123");
        let result = parse_path_segment(&mut state);
        assert!(result.is_err());
    }

    // parse_simple_import_without_alias tests
    #[test]
    fn test_parse_simple_import_without_alias_empty() {
        let result = parse_simple_import_without_alias(&[]).expect("should succeed");
        assert!(result.is_empty());
    }

    #[test]
    fn test_parse_simple_import_without_alias_single() {
        let result =
            parse_simple_import_without_alias(&["foo".to_string()]).expect("should succeed");
        assert!(result.is_empty());
    }

    #[test]
    fn test_parse_simple_import_without_alias_multi() {
        let result = parse_simple_import_without_alias(&["foo".to_string(), "bar".to_string()])
            .expect("should succeed");
        assert_eq!(result.len(), 1);
        assert!(matches!(&result[0], ImportItem::Named(n) if n == "bar"));
    }

    // parse_simple_import tests
    #[test]
    fn test_parse_simple_import_no_alias() {
        let mut state = create_state("");
        let result = parse_simple_import(&mut state, &["foo".to_string(), "bar".to_string()])
            .expect("should succeed");
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_parse_simple_import_with_alias() {
        let mut state = create_state("as alias");
        let result = parse_simple_import(&mut state, &["foo".to_string(), "bar".to_string()])
            .expect("should succeed");
        assert_eq!(result.len(), 1);
        if let ImportItem::Aliased { name, alias } = &result[0] {
            assert_eq!(name, "bar");
            assert_eq!(alias, "alias");
        } else {
            panic!("Expected Aliased import");
        }
    }

    // consume_module_identifier tests
    #[test]
    fn test_consume_module_identifier_valid() {
        let mut state = create_state("my_module");
        let name = consume_module_identifier(&mut state).expect("should succeed");
        assert_eq!(name, "my_module");
    }

    #[test]
    fn test_consume_module_identifier_invalid() {
        let mut state = create_state("123");
        let result = consume_module_identifier(&mut state);
        assert!(result.is_err());
    }

    // try_consume_item_separator tests
    #[test]
    fn test_try_consume_item_separator_comma() {
        let mut state = create_state(",");
        let result = try_consume_item_separator(&mut state).expect("should succeed");
        assert!(result);
    }

    #[test]
    fn test_try_consume_item_separator_brace() {
        let mut state = create_state("}");
        let result = try_consume_item_separator(&mut state).expect("should succeed");
        assert!(!result);
    }

    #[test]
    fn test_try_consume_item_separator_invalid() {
        let mut state = create_state("foo");
        let result = try_consume_item_separator(&mut state);
        assert!(result.is_err());
    }

    // parse_item_with_optional_alias tests
    #[test]
    fn test_parse_item_with_optional_alias_no_alias() {
        let mut state = create_state("");
        let item =
            parse_item_with_optional_alias(&mut state, "foo".to_string()).expect("should succeed");
        assert!(matches!(item, ImportItem::Named(n) if n == "foo"));
    }

    #[test]
    fn test_parse_item_with_optional_alias_with_alias() {
        let mut state = create_state("as bar");
        let item =
            parse_item_with_optional_alias(&mut state, "foo".to_string()).expect("should succeed");
        if let ImportItem::Aliased { name, alias } = item {
            assert_eq!(name, "foo");
            assert_eq!(alias, "bar");
        } else {
            panic!("Expected Aliased");
        }
    }

    #[test]
    fn test_parse_item_with_optional_alias_missing_alias_name() {
        let mut state = create_state("as 123");
        let result = parse_item_with_optional_alias(&mut state, "foo".to_string());
        assert!(result.is_err());
    }

    // parse_single_import_item tests
    #[test]
    fn test_parse_single_import_item_simple() {
        let mut state = create_state("foo");
        let item = parse_single_import_item(&mut state).expect("should succeed");
        assert!(matches!(item, ImportItem::Named(n) if n == "foo"));
    }

    // parse_wildcard_import tests
    #[test]
    fn test_parse_wildcard_import() {
        let mut state = create_state("*");
        let items = parse_wildcard_import(&mut state).expect("should succeed");
        assert_eq!(items.len(), 1);
        assert!(matches!(items[0], ImportItem::Wildcard));
    }

    // parse_export_identifier tests
    #[test]
    fn test_parse_export_identifier_valid() {
        let mut state = create_state("foo");
        let name = parse_export_identifier(&mut state).expect("should succeed");
        assert_eq!(name, "foo");
    }

    #[test]
    fn test_parse_export_identifier_invalid() {
        let mut state = create_state("123");
        let result = parse_export_identifier(&mut state);
        assert!(result.is_err());
    }

    // try_consume_export_comma tests
    #[test]
    fn test_try_consume_export_comma_present() {
        let mut state = create_state(",foo");
        try_consume_export_comma(&mut state);
        // Should have advanced past the comma
        assert!(matches!(
            state.tokens.peek(),
            Some((Token::Identifier(_), _))
        ));
    }

    #[test]
    fn test_try_consume_export_comma_absent() {
        let mut state = create_state("foo");
        try_consume_export_comma(&mut state);
        // Should not have advanced
        assert!(matches!(
            state.tokens.peek(),
            Some((Token::Identifier(n), _)) if n == "foo"
        ));
    }

    // parse_module_specifier tests
    #[test]
    fn test_parse_module_specifier_string() {
        let mut state = create_state("\"my_module\"");
        let module = parse_module_specifier(&mut state).expect("should succeed");
        assert_eq!(module, "my_module");
    }

    #[test]
    fn test_parse_module_specifier_identifier() {
        let mut state = create_state("my_module");
        let module = parse_module_specifier(&mut state).expect("should succeed");
        assert_eq!(module, "my_module");
    }

    #[test]
    fn test_parse_module_specifier_invalid() {
        let mut state = create_state("123");
        let result = parse_module_specifier(&mut state);
        assert!(result.is_err());
    }

    // create_export_or_reexport tests
    #[test]
    fn test_create_export_or_reexport_no_from() {
        let mut state = create_state("");
        let expr = create_export_or_reexport(&mut state, vec!["foo".to_string()], Span::default())
            .expect("should succeed");
        assert!(matches!(expr.kind, ExprKind::ExportList { .. }));
    }

    #[test]
    fn test_create_export_or_reexport_with_from() {
        let mut state = create_state("from \"module\"");
        let expr = create_export_or_reexport(&mut state, vec!["foo".to_string()], Span::default())
            .expect("should succeed");
        if let ExprKind::ReExport { items, module } = &expr.kind {
            assert_eq!(items, &vec!["foo".to_string()]);
            assert_eq!(module, "module");
        } else {
            panic!("Expected ReExport");
        }
    }
}