windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "parser_tests",
))]

//! Comprehensive Parser Item Tests
//!
//! These tests verify that the parser correctly parses all item types.
//! Items are top-level declarations: functions, structs, enums, traits, impls, etc.

use windjammer::lexer::Lexer;
use windjammer::parser::ast::*;
use windjammer::parser_impl::Parser;

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn parse_program(input: &str) -> Program<'_> {
    let mut lexer = Lexer::new(input);
    let tokens = lexer.tokenize_with_locations();
    let mut parser = Parser::new(tokens);
    parser.parse().expect("Failed to parse program")
}

fn first_item(input: &str) -> Item<'_> {
    let program = parse_program(input);
    program.items.into_iter().next().expect("No items parsed")
}

// ============================================================================
// FUNCTION DECLARATIONS
// ============================================================================

#[test]
fn test_fn_simple() {
    let item = first_item("fn foo() { }");
    assert!(matches!(item, Item::Function { .. }));
}

#[test]
fn test_fn_with_params() {
    let item = first_item("fn foo(x: i32, y: i32) { }");
    if let Item::Function { decl, .. } = item {
        assert_eq!(decl.parameters.len(), 2);
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_with_return_type() {
    let item = first_item("fn foo() -> i32 { 42 }");
    if let Item::Function { decl, .. } = item {
        assert!(decl.return_type.is_some());
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_with_body() {
    let item = first_item("fn foo() { let x = 1; x + 1 }");
    if let Item::Function { decl, .. } = item {
        assert!(!decl.body.is_empty());
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_pub() {
    let item = first_item("pub fn foo() { }");
    if let Item::Function { decl, .. } = item {
        assert!(decl.is_pub);
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_async() {
    let item = first_item("async fn foo() { }");
    if let Item::Function { decl, .. } = item {
        assert!(decl.is_async);
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_generic() {
    let item = first_item("fn foo<T>(x: T) -> T { x }");
    if let Item::Function { decl, .. } = item {
        assert!(!decl.type_params.is_empty());
    } else {
        panic!("Expected Function");
    }
}

#[test]
fn test_fn_with_self() {
    let code = r#"
    impl Foo {
        fn bar(self) { }
    }
    "#;
    let program = parse_program(code);
    assert!(!program.items.is_empty());
}

#[test]
fn test_fn_with_mut_self() {
    let code = r#"
    impl Foo {
        fn bar(self) { }
    }
    "#;
    let program = parse_program(code);
    assert!(!program.items.is_empty());
}

// ============================================================================
// STRUCT DECLARATIONS
// ============================================================================

#[test]
fn test_struct_empty() {
    let item = first_item("struct Empty { }");
    assert!(matches!(item, Item::Struct { .. }));
}

#[test]
fn test_struct_with_fields() {
    let item = first_item("struct Point { x: i32, y: i32 }");
    if let Item::Struct { decl, .. } = item {
        assert_eq!(decl.fields.len(), 2);
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_struct_pub() {
    let item = first_item("pub struct Point { x: i32, y: i32 }");
    if let Item::Struct { decl, .. } = item {
        assert!(decl.is_pub);
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_struct_generic() {
    let item = first_item("struct Container<T> { value: T }");
    if let Item::Struct { decl, .. } = item {
        assert!(!decl.type_params.is_empty());
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_struct_with_decorator() {
    let item = first_item("@auto struct Point { x: i32, y: i32 }");
    if let Item::Struct { decl, .. } = item {
        assert!(!decl.decorators.is_empty());
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_struct_with_derive() {
    let item = first_item("@derive(Clone, Debug) struct Point { x: i32, y: i32 }");
    if let Item::Struct { decl, .. } = item {
        assert!(!decl.decorators.is_empty());
    } else {
        panic!("Expected Struct");
    }
}

#[test]
fn test_struct_doc_comment_after_derive() {
    // Bug: Parser rejected @derive(Clone)\n/// doc\npub struct S with "Unexpected token: DocComment"
    // Doc comments between @derive and struct should be accepted (prefab_system.wj pattern)
    let code = r#"
@derive(Clone)
/// A reusable entity template
pub struct Prefab {
    pub id: i32,
    pub name: string,
}
"#;
    let item = first_item(code);
    if let Item::Struct { decl, .. } = item {
        assert_eq!(decl.name, "Prefab");
        assert!(!decl.decorators.is_empty());
        assert_eq!(
            decl.doc_comment.as_deref(),
            Some("A reusable entity template")
        );
    } else {
        panic!("Expected Struct, got {:?}", item);
    }
}

#[test]
fn test_struct_nested_type() {
    let item = first_item("struct Container { items: Vec<String> }");
    assert!(matches!(item, Item::Struct { .. }));
}

// ============================================================================
// ENUM DECLARATIONS
// ============================================================================

#[test]
fn test_enum_simple() {
    let item = first_item("enum Color { Red, Green, Blue }");
    assert!(matches!(item, Item::Enum { .. }));
}

#[test]
fn test_enum_with_data() {
    let item = first_item("enum Option<T> { Some(T), None }");
    if let Item::Enum { decl, .. } = item {
        assert_eq!(decl.variants.len(), 2);
    } else {
        panic!("Expected Enum");
    }
}

#[test]
fn test_enum_struct_variants() {
    let item = first_item("enum Message { Text { content: String }, Image { url: String } }");
    assert!(matches!(item, Item::Enum { .. }));
}

#[test]
fn test_enum_pub() {
    let item = first_item("pub enum Color { Red, Green, Blue }");
    if let Item::Enum { decl, .. } = item {
        assert!(decl.is_pub);
    } else {
        panic!("Expected Enum");
    }
}

#[test]
fn test_enum_generic() {
    let item = first_item("enum Result<T, E> { Ok(T), Err(E) }");
    if let Item::Enum { decl, .. } = item {
        assert!(!decl.type_params.is_empty());
    } else {
        panic!("Expected Enum");
    }
}

// ============================================================================
// IMPL BLOCKS
// ============================================================================

#[test]
fn test_impl_simple() {
    let code = r#"
    impl Point {
        fn new() -> Point {
            Point { x: 0, y: 0 }
        }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Impl { .. }));
}

#[test]
fn test_impl_with_methods() {
    let code = r#"
    impl Point {
        fn new() -> Point { Point { x: 0, y: 0 } }
        fn x(self) -> i32 { self.x }
        fn set_x(self, x: i32) { self.x = x }
    }
    "#;
    let item = first_item(code);
    if let Item::Impl { block, .. } = item {
        assert_eq!(block.functions.len(), 3);
    } else {
        panic!("Expected Impl");
    }
}

#[test]
fn test_impl_trait() {
    let code = r#"
    impl Display for Point {
        fn fmt(self) -> string {
            "point"
        }
    }
    "#;
    let item = first_item(code);
    if let Item::Impl { block, .. } = item {
        assert!(block.trait_name.is_some());
    } else {
        panic!("Expected Impl");
    }
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_impl_generic() {
    let code = r#"
    impl<T> Container<T> {
        fn new(value: T) -> Container<T> {
            Container { value: value }
        }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Impl { .. }));
}

// ============================================================================
// TRAIT DECLARATIONS
// ============================================================================

#[test]
fn test_trait_simple() {
    // Trait methods with default implementations
    let code = r#"
    trait Drawable {
        fn draw(self) { }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Trait { .. }));
}

#[test]
fn test_trait_with_methods() {
    let code = r#"
    trait Animal {
        fn speak(self) { }
        fn name(self) -> string { "unknown" }
    }
    "#;
    let item = first_item(code);
    if let Item::Trait { decl, .. } = item {
        assert_eq!(decl.methods.len(), 2);
    } else {
        panic!("Expected Trait");
    }
}

#[test]
fn test_trait_with_default_impl() {
    let code = r#"
    trait Greet {
        fn greet(self) {
            println!("Hello!")
        }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Trait { .. }));
}

#[test]
fn test_trait_generic() {
    // Trait methods need default implementations in Windjammer
    let code = r#"
    trait Into<T> {
        fn into(self) -> T { self }
    }
    "#;
    let item = first_item(code);
    if let Item::Trait { decl, .. } = item {
        assert!(!decl.generics.is_empty());
    } else {
        panic!("Expected Trait");
    }
}

// ============================================================================
// USE STATEMENTS
// ============================================================================

#[test]
fn test_use_simple() {
    let item = first_item("use std::io");
    assert!(matches!(item, Item::Use { .. }));
}

#[test]
fn test_use_nested() {
    let item = first_item("use std::collections::HashMap");
    assert!(matches!(item, Item::Use { .. }));
}

#[test]
fn test_use_glob() {
    let item = first_item("use std::io::*");
    assert!(matches!(item, Item::Use { .. }));
}

#[test]
fn test_use_alias() {
    let item = first_item("use std::collections::HashMap as Map");
    assert!(matches!(item, Item::Use { .. }));
}

#[test]
fn test_use_multiple() {
    let item = first_item("use std::io::{Read, Write}");
    assert!(matches!(item, Item::Use { .. }));
}

// ============================================================================
// MOD DECLARATIONS
// ============================================================================

#[test]
fn test_mod_simple() {
    let item = first_item("mod utils");
    assert!(matches!(item, Item::Mod { .. }));
}

#[test]
fn test_mod_inline() {
    let code = r#"
    mod utils {
        fn helper() { }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Mod { .. }));
}

#[test]
fn test_mod_pub() {
    let item = first_item("pub mod utils");
    if let Item::Mod { is_public, .. } = item {
        assert!(is_public);
    } else {
        panic!("Expected Mod");
    }
}

// ============================================================================
// CONST AND STATIC DECLARATIONS
// ============================================================================

#[test]
fn test_const_simple() {
    let item = first_item("const MAX: i32 = 100");
    assert!(matches!(item, Item::Const { .. }));
}

#[test]
fn test_const_with_name() {
    let item = first_item("const MAX: i32 = 100");
    if let Item::Const { name, .. } = item {
        assert_eq!(name, "MAX");
    } else {
        panic!("Expected Const");
    }
}

#[test]
fn test_static_simple() {
    let item = first_item("static COUNTER: i32 = 0");
    assert!(matches!(item, Item::Static { .. }));
}

#[test]
fn test_static_mut() {
    let item = first_item("static mut COUNTER: i32 = 0");
    if let Item::Static { mutable, .. } = item {
        assert!(mutable);
    } else {
        panic!("Expected Static");
    }
}

// ============================================================================
// MULTIPLE ITEMS
// ============================================================================

#[test]
fn test_multiple_functions() {
    let code = r#"
    fn foo() { }
    fn bar() { }
    fn baz() { }
    "#;
    let program = parse_program(code);
    assert_eq!(program.items.len(), 3);
}

#[test]
fn test_struct_and_impl() {
    let code = r#"
    struct Point { x: i32, y: i32 }
    
    impl Point {
        fn new(x: i32, y: i32) -> Point {
            Point { x: x, y: y }
        }
    }
    "#;
    let program = parse_program(code);
    assert_eq!(program.items.len(), 2);
}

#[test]
fn test_trait_and_impl() {
    let code = r#"
    trait Drawable {
        fn draw(self) { }
    }
    
    struct Circle { radius: f32 }
    
    impl Drawable for Circle {
        fn draw(self) { }
    }
    "#;
    let program = parse_program(code);
    assert_eq!(program.items.len(), 3);
}

// ============================================================================
// COMPLEX ITEMS
// ============================================================================

#[test]
fn test_generic_struct_with_bounds() {
    let code = "struct Container<T: Clone> { value: T }";
    let item = first_item(code);
    assert!(matches!(item, Item::Struct { .. }));
}

#[test]
fn test_impl_with_where_clause() {
    let code = r#"
    impl<T> Container<T> where T: Clone {
        fn clone_value(self) -> T {
            self.value
        }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Impl { .. }));
}

#[test]
fn test_async_trait_method() {
    // Async trait methods need default implementations
    let code = r#"
    trait AsyncReader {
        async fn read(self) -> string { "data" }
    }
    "#;
    let item = first_item(code);
    assert!(matches!(item, Item::Trait { .. }));
}