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
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
// Parser - Windjammer Language Parser
//
// This file contains the complete parser for Windjammer. It is organized into the following sections:
//
// 1. AST TYPES (lines ~3-340)
//    - Type, TypeParam, Parameter, FunctionDecl, StructDecl, EnumDecl, TraitDecl, ImplBlock
//    - Expression, Statement, Pattern, Item, Program
//
// 2. PARSER CORE (lines ~344-400)
//    - Parser struct
//    - Basic utilities: new(), current_token(), advance(), expect(), peek()
//    - Helper: type_to_string()
//
// 3. TOP-LEVEL PARSING (lines ~400-700)
//    - parse() - main entry point
//    - parse_item() - dispatches to item parsers
//    - parse_const_or_static()
//    - parse_use()
//    - parse_decorator() and parse_decorator_arguments()
//
// 4. ITEM PARSING (lines ~700-1500)
//    - parse_impl() - impl blocks with generics and trait impls
//    - parse_trait() - trait definitions
//    - parse_function() - function declarations
//    - parse_parameters()
//    - parse_struct() - struct definitions
//    - parse_enum() - enum definitions with generics
//    - parse_type_params() - generic type parameters with bounds
//    - parse_where_clause() - where clauses
//
// 5. STATEMENT PARSING (lines ~1500-1900)
//    - parse_block_statements()
//    - parse_statement() - dispatches to statement parsers
//    - parse_const_statement(), parse_static_statement()
//    - parse_let(), parse_return()
//    - parse_if(), parse_match()
//    - parse_for(), parse_loop(), parse_while()
//    - parse_go(), parse_defer()
//
// 6. PATTERN PARSING (lines ~1900-2000)
//    - parse_pattern_with_or() - OR patterns
//    - parse_pattern() - all pattern types including enum variants
//
// 7. EXPRESSION PARSING (lines ~2000-2800)
//    - parse_expression() - entry point
//    - parse_ternary_expression() - ternary operator
//    - parse_match_value() - match value with special handling
//    - parse_binary_expression() - operator precedence climbing
//    - get_binary_op() - operator precedence table
//    - parse_primary_expression() - literals, identifiers, calls, etc.
//    - parse_postfix_expression() - method calls, field access, indexing, turbofish
//    - parse_arguments()
//    - parse_closure()
//
// 8. TYPE PARSING (lines ~2800+)
//    - parse_type() - all type variants
//
// TODO: Split this into modules:
//   - parser/mod.rs - Parser struct and utilities
//   - parser/types.rs - Type parsing
//   - parser/patterns.rs - Pattern parsing
//   - parser/expressions.rs - Expression parsing
//   - parser/statements.rs - Statement parsing
//   - parser/items.rs - Top-level item parsing

use crate::lexer::Token;
use typed_arena::Arena;

// Import all AST types from the new parser::ast module
pub use crate::parser::ast::*;

// ============================================================================
// SECTION 2: PARSER CORE
// ============================================================================

/// A structured diagnostic emitted during parsing.
#[derive(Debug, Clone)]
pub struct ParseWarning {
    pub message: String,
    pub file: Option<String>,
    pub line: Option<usize>,
    pub column: Option<usize>,
    /// When true, this diagnostic is an error that should halt compilation.
    pub is_error: bool,
}

pub struct Parser {
    pub(crate) tokens: Vec<crate::lexer::TokenWithLocation>,
    pub(crate) position: usize,
    pub(crate) filename: String,
    #[allow(dead_code)]
    pub(crate) source: String,
    pub(crate) warnings: Vec<ParseWarning>,
    /// True when parsing inside an `extern fn` declaration (FFI boundary).
    /// Suppresses W0010 warnings since FFI signatures must match Rust types exactly.
    pub(crate) in_extern_fn: bool,
    // Arena allocators for AST nodes (eliminates recursive Drop)
    // When Parser is dropped, these arenas drop all allocated AST nodes at once
    // without recursive calls to Drop, solving the Windows stack overflow issue
    //
    // SAFETY: We use 'static here because the arena owns the memory. The actual
    // lifetime is tied to Parser through the parse() method signature. References
    // returned from parse() will have lifetime 'parser tied to &'parser self.
    pub(crate) expr_arena: Arena<Expression<'static>>,
    pub(crate) stmt_arena: Arena<Statement<'static>>,
    pub(crate) pattern_arena: Arena<Pattern<'static>>,
}

impl Parser {
    /// Check if there was a newline before the current token (for ASI - Automatic Semicolon Insertion)
    pub(crate) fn had_newline_before_current(&self) -> bool {
        if self.position == 0 {
            return false; // No previous token
        }

        let prev_token = self.tokens.get(self.position - 1);
        let curr_token = self.tokens.get(self.position);

        match (prev_token, curr_token) {
            (Some(prev), Some(curr)) => {
                // If the line number changed, there was a newline
                curr.line > prev.line
            }
            _ => false,
        }
    }
    pub fn new(tokens: Vec<crate::lexer::TokenWithLocation>) -> Self {
        Parser {
            tokens,
            position: 0,
            filename: String::new(),
            source: String::new(),
            warnings: Vec::new(),
            in_extern_fn: false,
            expr_arena: Arena::new(),
            stmt_arena: Arena::new(),
            pattern_arena: Arena::new(),
        }
    }

    pub fn new_with_source(
        tokens: Vec<crate::lexer::TokenWithLocation>,
        filename: String,
        source: String,
    ) -> Self {
        Parser {
            tokens,
            position: 0,
            filename,
            source,
            warnings: Vec::new(),
            in_extern_fn: false,
            expr_arena: Arena::new(),
            stmt_arena: Arena::new(),
            pattern_arena: Arena::new(),
        }
    }

    pub fn warnings(&self) -> &[ParseWarning] {
        &self.warnings
    }

    pub(crate) fn emit_warning(
        &mut self,
        message: String,
        file: Option<String>,
        line: Option<usize>,
        column: Option<usize>,
    ) {
        self.warnings.push(ParseWarning {
            message,
            file,
            line,
            column,
            is_error: false,
        });
    }

    pub(crate) fn emit_error_diagnostic(
        &mut self,
        message: String,
        file: Option<String>,
        line: Option<usize>,
        column: Option<usize>,
    ) {
        self.warnings.push(ParseWarning {
            message,
            file,
            line,
            column,
            is_error: true,
        });
    }

    /// Allocate an expression in the arena
    /// SAFETY: We transmute the lifetime from 'static (arena storage) to 'ast (result lifetime)
    /// This is safe because:
    /// 1. Parser owns the arena, so references live as long as Parser does
    /// 2. We use a separate 'ast lifetime (not tied to &self borrow) to allow multiple allocations
    /// 3. The arena uses interior mutability (Cell), so &self is sufficient
    pub(crate) fn alloc_expr<'ast>(&self, expr: Expression<'static>) -> &'ast Expression<'ast> {
        unsafe {
            let ptr = self.expr_arena.alloc(expr);
            std::mem::transmute(ptr)
        }
    }

    /// Allocate a statement in the arena
    /// SAFETY: Same as alloc_expr
    pub(crate) fn alloc_stmt<'ast>(&self, stmt: Statement<'static>) -> &'ast Statement<'ast> {
        unsafe {
            let ptr = self.stmt_arena.alloc(stmt);
            std::mem::transmute(ptr)
        }
    }

    /// Allocate a pattern in the arena
    /// SAFETY: Same as alloc_expr
    pub(crate) fn alloc_pattern<'ast>(&self, pattern: Pattern<'static>) -> &'ast Pattern<'ast> {
        unsafe {
            let ptr = self.pattern_arena.alloc(pattern);
            std::mem::transmute(ptr)
        }
    }

    pub(crate) fn current_token(&self) -> &Token {
        self.tokens
            .get(self.position)
            .map(|t| &t.token)
            .unwrap_or(&Token::Eof)
    }

    /// Get the current token's location for source mapping
    pub(crate) fn current_location(&self) -> Option<crate::source_map::Location> {
        self.tokens
            .get(self.position)
            .map(|t| crate::source_map::Location {
                file: std::path::PathBuf::from(&self.filename),
                line: t.line,
                column: t.column,
            })
    }

    pub(crate) fn advance(&mut self) {
        if self.position < self.tokens.len() {
            self.position += 1;
        }
    }

    pub(crate) fn expect(&mut self, expected: Token) -> Result<(), String> {
        if self.current_token() == &expected {
            self.advance();
            Ok(())
        } else {
            Err(format!(
                "Expected {:?}, got {:?} (at token position {})",
                expected,
                self.current_token(),
                self.position
            ))
        }
    }

    /// Handle closing of nested generic types where `>>` should be treated as two `>` tokens
    pub(crate) fn expect_gt_or_split_shr(&mut self) -> Result<bool, String> {
        match self.current_token() {
            Token::Gt => {
                self.advance();
                Ok(false) // Not a split, normal >
            }
            Token::Shr => {
                // Split >> into two > tokens
                // We consume one > and insert another > after it
                let current_location = self.tokens[self.position].clone();
                let mut gt_token = current_location.clone();
                gt_token.token = Token::Gt;

                // Replace current Shr with Gt
                self.tokens[self.position] = gt_token.clone();

                // Insert another Gt right after this position
                self.tokens.insert(self.position + 1, gt_token);

                // Advance to consume the first >
                self.advance();

                Ok(true) // Was a split >>
            }
            _ => Err(format!(
                "Expected '>' or '>>', got {:?} (at token position {})",
                self.current_token(),
                self.position
            )),
        }
    }

    // ========================================================================
    // SECTION 3: TOP-LEVEL PARSING
    // ========================================================================

    pub fn parse(&mut self) -> Result<Program<'static>, String> {
        let mut items = Vec::new();

        while self.current_token() != &Token::Eof {
            items.push(self.parse_item()?);
        }

        Ok(Program { items })
    }

    pub(crate) fn parse_item(&mut self) -> Result<Item<'static>, String> {
        // Skip leading blank lines so indented r#"\\n    @derive(...)"# still attaches decorators.
        while matches!(self.current_token(), Token::Newline) {
            self.advance();
        }

        // Collect doc comments (/// lines) that appear before the item
        let mut doc_lines = Vec::new();
        while let Token::DocComment(content) = self.current_token() {
            doc_lines.push(content.clone());
            self.advance();
        }
        let mut doc_comment = if doc_lines.is_empty() {
            None
        } else {
            Some(doc_lines.join("\n"))
        };

        while matches!(self.current_token(), Token::Newline) {
            self.advance();
        }

        // Check for decorators
        let mut decorators = Vec::new();
        while let Token::Decorator(_) = self.current_token() {
            decorators.push(self.parse_decorator()?);
        }

        while matches!(self.current_token(), Token::Newline) {
            self.advance();
        }

        // Doc comments may appear after @derive and before the item (e.g. @derive(Clone)\n/// doc\npub struct S)
        let mut doc_lines_after = Vec::new();
        while let Token::DocComment(content) = self.current_token() {
            doc_lines_after.push(content.clone());
            self.advance();
        }
        if !doc_lines_after.is_empty() {
            doc_comment = Some(doc_lines_after.join("\n"));
        }

        while matches!(self.current_token(), Token::Newline) {
            self.advance();
        }

        // Check for pub keyword (for module functions)
        let is_pub = if self.current_token() == &Token::Pub {
            self.advance();
            true
        } else {
            false
        };

        match self.current_token() {
            Token::Fn => {
                self.advance(); // Consume the Fn token
                let mut func = self.parse_function()?;
                func.decorators = decorators.clone();
                func.is_pub = is_pub;
                func.doc_comment = doc_comment;
                // Check if @async decorator is present
                if decorators.iter().any(|d| d.name == "async") {
                    func.is_async = true;
                }
                Ok(Item::Function {
                    decl: func,
                    location: self.current_location(),
                })
            }
            Token::Async => {
                self.advance();
                self.expect(Token::Fn)?;
                let mut func = self.parse_function()?;
                func.is_async = true;
                func.is_pub = is_pub;
                func.decorators = decorators;
                func.doc_comment = doc_comment;
                Ok(Item::Function {
                    decl: func,
                    location: self.current_location(),
                })
            }
            Token::Struct => {
                self.advance();
                let mut struct_decl = self.parse_struct(false)?;
                struct_decl.decorators = decorators;
                struct_decl.is_pub = is_pub;
                struct_decl.doc_comment = doc_comment;
                Ok(Item::Struct {
                    decl: struct_decl,
                    location: self.current_location(),
                })
            }
            Token::Enum => {
                self.advance();
                let mut enum_decl = self.parse_enum()?;
                enum_decl.is_pub = is_pub;
                enum_decl.doc_comment = doc_comment;
                Ok(Item::Enum {
                    decl: enum_decl,
                    location: self.current_location(),
                })
            }
            Token::Trait => {
                self.advance();
                let mut trait_decl = self.parse_trait()?;
                trait_decl.doc_comment = doc_comment;
                Ok(Item::Trait {
                    decl: trait_decl,
                    location: self.current_location(),
                })
            }
            Token::Impl => {
                self.advance();
                let mut impl_block = self.parse_impl(false)?;
                impl_block.decorators = decorators;
                Ok(Item::Impl {
                    block: impl_block,
                    location: self.current_location(),
                })
            }
            Token::Const => {
                self.advance();
                let (name, type_, value) = self.parse_const_or_static()?;
                Ok(Item::Const {
                    name,
                    is_pub,
                    type_,
                    value,
                    location: self.current_location(),
                })
            }
            Token::Static => {
                self.advance();
                let mutable = if self.current_token() == &Token::Mut {
                    self.advance();
                    true
                } else {
                    false
                };
                let (name, type_, value) = self.parse_const_or_static()?;
                Ok(Item::Static {
                    name,
                    mutable,
                    type_,
                    value,
                    location: self.current_location(),
                })
            }
            Token::Extern => {
                // `extern let` (GPU) | `extern struct` / `extern impl` (FFI types) | `extern fn` (FFI)
                if self.peek(1) == Some(&Token::Let) {
                    self.advance(); // consume extern
                    self.advance(); // consume let

                    let name = if let Token::Ident(n) = self.current_token() {
                        let n = n.clone();
                        self.advance();
                        n
                    } else {
                        return Err("Expected variable name after extern let".to_string());
                    };

                    self.expect(Token::Colon)?;
                    let type_ = self.parse_type()?;

                    // Semicolon optional (ASI)
                    if self.current_token() == &Token::Semicolon {
                        self.advance();
                    }

                    Ok(Item::ExternLet {
                        name,
                        type_,
                        decorators,
                        is_pub,
                        location: self.current_location(),
                    })
                } else {
                    self.advance(); // consume Extern
                    match self.current_token() {
                        Token::Struct => {
                            self.advance();
                            let mut struct_decl = self.parse_struct(true)?;
                            struct_decl.decorators = decorators;
                            struct_decl.is_pub = is_pub;
                            struct_decl.doc_comment = doc_comment;
                            Ok(Item::Struct {
                                decl: struct_decl,
                                location: self.current_location(),
                            })
                        }
                        Token::Impl => {
                            self.advance();
                            let mut impl_block = self.parse_impl(true)?;
                            impl_block.decorators = decorators;
                            Ok(Item::Impl {
                                block: impl_block,
                                location: self.current_location(),
                            })
                        }
                        Token::Fn => {
                            self.expect(Token::Fn)?; // Expect fn after extern
                            self.in_extern_fn = true;
                            let mut func = self.parse_function()?;
                            self.in_extern_fn = false;
                            func.is_extern = true; // Mark as extern function
                            func.is_pub = is_pub;
                            func.decorators = decorators;
                            func.doc_comment = doc_comment;
                            Ok(Item::Function {
                                decl: func,
                                location: self.current_location(),
                            })
                        }
                        _ => {
                            Err("expected `let`, `struct`, `impl`, or `fn` after `extern`"
                                .to_string())
                        }
                    }
                }
            }
            Token::Use => {
                self.advance(); // consume 'use'
                let (path, alias) = self.parse_use()?;
                // Consume optional semicolon (ASI - automatic semicolon insertion)
                if self.current_token() == &Token::Semicolon {
                    self.advance();
                }
                Ok(Item::Use {
                    path,
                    alias,
                    is_pub, // THE WINDJAMMER WAY: Track pub use for re-exports
                    location: self.current_location(),
                })
            }
            Token::Bound => {
                self.advance(); // consume 'bound'
                self.parse_bound_alias()
            }
            Token::Mod => {
                self.advance(); // consume 'mod'
                let (name, items, _) = self.parse_mod()?;
                Ok(Item::Mod {
                    name,
                    items,
                    is_public: is_pub,
                    location: self.current_location(),
                })
            }
            Token::Type => {
                self.advance(); // consume 'type'
                let name = if let Token::Ident(n) = self.current_token() {
                    let name = n.clone();
                    self.advance();
                    name
                } else {
                    return Err("Expected type alias name".to_string());
                };
                self.expect(Token::Assign)?;
                let target = self.parse_type()?;
                // Semicolon optional (ASI)
                if self.current_token() == &Token::Semicolon {
                    self.advance();
                }
                Ok(Item::TypeAlias {
                    name,
                    target,
                    is_pub,
                    location: self.current_location(),
                })
            }
            _ => Err(format!(
                "Unexpected token: {:?} (at token position {})",
                self.current_token(),
                self.position
            )),
        }
    }

    fn parse_bound_alias(&mut self) -> Result<Item<'static>, String> {
        // bound Name = Trait + Trait + ...
        let name = if let Token::Ident(n) = self.current_token() {
            let name = n.clone();
            self.advance();
            name
        } else {
            return Err("Expected bound alias name".to_string());
        };

        self.expect(Token::Assign)?;

        // Parse trait list: Trait + Trait + ...
        let mut traits = Vec::new();
        loop {
            if let Token::Ident(trait_name) = self.current_token() {
                traits.push(trait_name.clone());
                self.advance();
            } else {
                return Err("Expected trait name in bound alias".to_string());
            }

            if self.current_token() == &Token::Plus {
                self.advance(); // consume +
            } else {
                break;
            }
        }

        Ok(Item::BoundAlias {
            name,
            traits,
            location: self.current_location(),
        })
    }

    pub(crate) fn parse_const_or_static(
        &mut self,
    ) -> Result<(String, Type, &'static Expression<'static>), String> {
        let name = if let Token::Ident(n) = self.current_token() {
            let name = n.clone();
            self.advance();
            name
        } else {
            return Err("Expected const/static name".to_string());
        };

        self.expect(Token::Colon)?;
        let type_ = self.parse_type()?;

        self.expect(Token::Assign)?;
        let value = self.parse_expression()?;

        Ok((name, type_, value))
    }

    // Helper: Extract a name from a pattern for backward compatibility

    // Public wrapper methods for component compiler
    pub fn parse_expression_public(&mut self) -> Result<&'static Expression<'static>, String> {
        self.parse_expression()
    }

    pub fn parse_function_public(&mut self) -> Result<FunctionDecl<'static>, String> {
        self.parse_function()
    }
}