aether/
parser.rs

1// src/parser.rs
2//! Parser for the Aether language
3//!
4//! Converts a stream of tokens into an Abstract Syntax Tree (AST)
5
6use crate::ast::{BinOp, Expr, Program, Stmt, UnaryOp};
7use crate::lexer::Lexer;
8use crate::token::Token;
9
10/// Parse errors with location information
11#[derive(Debug, Clone, PartialEq)]
12pub enum ParseError {
13    UnexpectedToken {
14        expected: String,
15        found: Token,
16        line: usize,
17        column: usize,
18    },
19    UnexpectedEOF {
20        line: usize,
21        column: usize,
22    },
23    InvalidNumber(String),
24    InvalidExpression {
25        message: String,
26        line: usize,
27        column: usize,
28    },
29    InvalidStatement {
30        message: String,
31        line: usize,
32        column: usize,
33    },
34    InvalidIdentifier {
35        name: String,
36        reason: String,
37        line: usize,
38        column: usize,
39    },
40}
41
42impl std::fmt::Display for ParseError {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        match self {
45            ParseError::UnexpectedToken {
46                expected,
47                found,
48                line,
49                column,
50            } => {
51                write!(
52                    f,
53                    "Parse error at line {}, column {}: Expected {}, found {:?}",
54                    line, column, expected, found
55                )
56            }
57            ParseError::UnexpectedEOF { line, column } => {
58                write!(
59                    f,
60                    "Parse error at line {}, column {}: Unexpected end of file",
61                    line, column
62                )
63            }
64            ParseError::InvalidNumber(s) => write!(f, "Parse error: Invalid number: {}", s),
65            ParseError::InvalidExpression {
66                message,
67                line,
68                column,
69            } => {
70                write!(
71                    f,
72                    "Parse error at line {}, column {}: Invalid expression - {}",
73                    line, column, message
74                )
75            }
76            ParseError::InvalidStatement {
77                message,
78                line,
79                column,
80            } => {
81                write!(
82                    f,
83                    "Parse error at line {}, column {}: Invalid statement - {}",
84                    line, column, message
85                )
86            }
87            ParseError::InvalidIdentifier {
88                name,
89                reason,
90                line,
91                column,
92            } => {
93                write!(
94                    f,
95                    "Parse error at line {}, column {}: Invalid identifier '{}' - {}",
96                    line, column, name, reason
97                )
98            }
99        }
100    }
101}
102
103impl std::error::Error for ParseError {}
104
105/// Operator precedence (higher number = higher precedence)
106#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
107enum Precedence {
108    Lowest = 0,
109    Or = 1,         // ||
110    And = 2,        // &&
111    Equals = 3,     // ==, !=
112    Comparison = 4, // <, <=, >, >=
113    Sum = 5,        // +, -
114    Product = 6,    // *, /, %
115    Prefix = 7,     // -, !
116    Call = 8,       // func()
117    Index = 9,      // array[index]
118}
119
120/// Parser state
121pub struct Parser {
122    lexer: Lexer,
123    current_token: Token,
124    peek_token: Token,
125    current_line: usize,
126    current_column: usize,
127    current_had_whitespace: bool, // whether whitespace preceded current_token
128    peek_had_whitespace: bool,    // whether whitespace preceded peek_token
129}
130
131impl Parser {
132    /// Create a new parser from source code
133    pub fn new(input: &str) -> Self {
134        let mut lexer = Lexer::new(input);
135        let current = lexer.next_token();
136        let current_ws = lexer.had_whitespace();
137        let peek = lexer.next_token();
138        let peek_ws = lexer.had_whitespace();
139        let line = lexer.line();
140        let column = lexer.column();
141
142        Parser {
143            lexer,
144            current_token: current,
145            peek_token: peek,
146            current_line: line,
147            current_column: column,
148            current_had_whitespace: current_ws,
149            peek_had_whitespace: peek_ws,
150        }
151    }
152
153    /// Advance to the next token
154    fn next_token(&mut self) {
155        self.current_token = self.peek_token.clone();
156        self.current_had_whitespace = self.peek_had_whitespace;
157        self.peek_token = self.lexer.next_token();
158        self.peek_had_whitespace = self.lexer.had_whitespace();
159        self.current_line = self.lexer.line();
160        self.current_column = self.lexer.column();
161    }
162
163    /// Skip newline tokens (they're optional in many places)
164    fn skip_newlines(&mut self) {
165        while self.current_token == Token::Newline {
166            self.next_token();
167        }
168    }
169
170    /// Check if current token matches expected, advance if true
171    fn expect_token(&mut self, expected: Token) -> Result<(), ParseError> {
172        if self.current_token == expected {
173            self.next_token();
174            Ok(())
175        } else {
176            Err(ParseError::UnexpectedToken {
177                expected: format!("{:?}", expected),
178                found: self.current_token.clone(),
179                line: self.current_line,
180                column: self.current_column,
181            })
182        }
183    }
184
185    /// Helper to check if identifier follows naming convention (UPPER_SNAKE_CASE)
186    fn validate_identifier(&self, name: &str) -> Result<(), ParseError> {
187        self.validate_identifier_internal(name, false)
188    }
189
190    /// Helper to check if identifier follows naming convention
191    /// For function parameters, we allow more flexible naming (can use lowercase)
192    fn validate_identifier_internal(&self, name: &str, is_param: bool) -> Result<(), ParseError> {
193        // Check it doesn't start with a number
194        if name.chars().next().is_some_and(|c| c.is_numeric()) {
195            return Err(ParseError::InvalidIdentifier {
196                name: name.to_string(),
197                reason: "标识符不能以数字开头".to_string(),
198                line: self.current_line,
199                column: self.current_column,
200            });
201        }
202
203        // For function parameters, allow lowercase letters
204        if is_param {
205            let is_valid = name
206                .chars()
207                .all(|c| c.is_alphabetic() || c.is_numeric() || c == '_');
208
209            if !is_valid {
210                return Err(ParseError::InvalidIdentifier {
211                    name: name.to_string(),
212                    reason: "参数名只能包含字母、数字和下划线".to_string(),
213                    line: self.current_line,
214                    column: self.current_column,
215                });
216            }
217        } else {
218            // For variables and function names, require uppercase
219            let is_valid = name
220                .chars()
221                .all(|c| c.is_uppercase() || c.is_numeric() || c == '_');
222
223            if !is_valid {
224                return Err(ParseError::InvalidIdentifier {
225                    name: name.to_string(),
226                    reason:
227                        "变量名和函数名必须使用全大写字母和下划线(例如:MY_VAR, CALCULATE_SUM)"
228                            .to_string(),
229                    line: self.current_line,
230                    column: self.current_column,
231                });
232            }
233        }
234
235        Ok(())
236    }
237
238    /// Get precedence of current token
239    fn current_precedence(&self) -> Precedence {
240        self.token_precedence(&self.current_token)
241    }
242
243    /// Get precedence of peek token
244    #[allow(dead_code)]
245    fn peek_precedence(&self) -> Precedence {
246        self.token_precedence(&self.peek_token)
247    }
248
249    /// Get precedence of a token
250    fn token_precedence(&self, token: &Token) -> Precedence {
251        match token {
252            Token::Or => Precedence::Or,
253            Token::And => Precedence::And,
254            Token::Equal | Token::NotEqual => Precedence::Equals,
255            Token::Less | Token::LessEqual | Token::Greater | Token::GreaterEqual => {
256                Precedence::Comparison
257            }
258            Token::Plus | Token::Minus => Precedence::Sum,
259            Token::Multiply | Token::Divide | Token::Modulo => Precedence::Product,
260            Token::LeftParen => Precedence::Call,
261            Token::LeftBracket => Precedence::Index,
262            _ => Precedence::Lowest,
263        }
264    }
265
266    /// Parse a complete program
267    pub fn parse_program(&mut self) -> Result<Program, ParseError> {
268        let mut statements = Vec::new();
269
270        self.skip_newlines();
271
272        while self.current_token != Token::EOF {
273            let stmt = self.parse_statement()?;
274            statements.push(stmt);
275            self.skip_newlines();
276        }
277
278        Ok(statements)
279    }
280
281    /// Parse a statement
282    fn parse_statement(&mut self) -> Result<Stmt, ParseError> {
283        match &self.current_token {
284            Token::Set => self.parse_set_statement(),
285            Token::Func => self.parse_func_definition(),
286            Token::Generator => self.parse_generator_definition(),
287            Token::Lazy => self.parse_lazy_definition(),
288            Token::Return => self.parse_return_statement(),
289            Token::Yield => self.parse_yield_statement(),
290            Token::Break => self.parse_break_statement(),
291            Token::Continue => self.parse_continue_statement(),
292            Token::While => self.parse_while_statement(),
293            Token::For => self.parse_for_statement(),
294            Token::Switch => self.parse_switch_statement(),
295            Token::Import => self.parse_import_statement(),
296            Token::Export => self.parse_export_statement(),
297            Token::Throw => self.parse_throw_statement(),
298            _ => self.parse_expression_statement(),
299        }
300    }
301
302    /// Parse: Set NAME value
303    fn parse_set_statement(&mut self) -> Result<Stmt, ParseError> {
304        self.next_token(); // skip 'Set'
305
306        // Parse the left-hand side (target)
307        // This can be either an identifier or an index expression
308        // We manually parse this to avoid consuming array literals as part of the target
309
310        let name = match &self.current_token {
311            Token::Identifier(n) => {
312                self.validate_identifier(n)?;
313                n.clone()
314            }
315            _ => {
316                return Err(ParseError::UnexpectedToken {
317                    expected: "identifier".to_string(),
318                    found: self.current_token.clone(),
319                    line: self.current_line,
320                    column: self.current_column,
321                });
322            }
323        };
324
325        self.next_token(); // move past identifier
326
327        // Check if followed by '[' for index access
328        // CRITICAL: Distinguish between:
329        // 1. Set NAME[index] value  -> index assignment (NO space before '[')
330        // 2. Set NAME [array]       -> array literal assignment (space before '[')
331        //
332        // We check if there was whitespace before the '[' token
333        if self.current_token == Token::LeftBracket {
334            // IMPORTANT: Check whitespace BEFORE calling next_token()
335            // because had_whitespace() reflects the whitespace before current_token
336            let has_space_before_bracket = self.current_had_whitespace;
337
338            if has_space_before_bracket {
339                // Space before '[' means this is: Set NAME [array_literal]
340                // Parse the whole thing as a value expression
341                let value = self.parse_expression(Precedence::Lowest)?;
342                if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
343                    self.next_token();
344                }
345                return Ok(Stmt::Set { name, value });
346            }
347
348            // No space before '[' means this is: Set NAME[index] value
349            // This is an index assignment
350            self.next_token(); // skip '['            // Parse the index expression
351            let index = self.parse_expression(Precedence::Lowest)?;
352
353            // Expect ']'
354            if self.current_token != Token::RightBracket {
355                return Err(ParseError::UnexpectedToken {
356                    expected: "']' for index access".to_string(),
357                    found: self.current_token.clone(),
358                    line: self.current_line,
359                    column: self.current_column,
360                });
361            }
362
363            self.next_token(); // skip ']'
364
365            // Now parse the value to assign
366            let value = self.parse_expression(Precedence::Lowest)?;
367
368            if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
369                self.next_token();
370            }
371
372            return Ok(Stmt::SetIndex {
373                object: Box::new(Expr::Identifier(name)),
374                index: Box::new(index),
375                value,
376            });
377        }
378
379        // Regular Set statement: Set NAME value
380        let value = self.parse_expression(Precedence::Lowest)?;
381
382        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
383            self.next_token();
384        }
385
386        Ok(Stmt::Set { name, value })
387    }
388
389    /// Parse: Func NAME (params) { body }
390    fn parse_func_definition(&mut self) -> Result<Stmt, ParseError> {
391        self.next_token(); // skip 'Func'
392
393        let name = match &self.current_token {
394            Token::Identifier(name) => {
395                // Validate function name
396                self.validate_identifier(name)?;
397                name.clone()
398            }
399            _ => {
400                return Err(ParseError::UnexpectedToken {
401                    expected: "identifier".to_string(),
402                    found: self.current_token.clone(),
403                    line: self.current_line,
404                    column: self.current_column,
405                });
406            }
407        };
408
409        self.next_token(); // move to '('
410        self.expect_token(Token::LeftParen)?;
411
412        let params = self.parse_parameter_list()?;
413
414        self.expect_token(Token::RightParen)?;
415        self.skip_newlines();
416        self.expect_token(Token::LeftBrace)?;
417
418        let body = self.parse_block()?;
419
420        self.expect_token(Token::RightBrace)?;
421
422        Ok(Stmt::FuncDef { name, params, body })
423    }
424
425    /// Parse: Generator NAME (params) { body }
426    fn parse_generator_definition(&mut self) -> Result<Stmt, ParseError> {
427        self.next_token(); // skip 'Generator'
428
429        let name = match &self.current_token {
430            Token::Identifier(name) => name.clone(),
431            _ => {
432                return Err(ParseError::UnexpectedToken {
433                    expected: "identifier".to_string(),
434                    found: self.current_token.clone(),
435                    line: self.current_line,
436                    column: self.current_column,
437                });
438            }
439        };
440
441        self.next_token();
442        self.expect_token(Token::LeftParen)?;
443
444        let params = self.parse_parameter_list()?;
445
446        self.expect_token(Token::RightParen)?;
447        self.skip_newlines();
448        self.expect_token(Token::LeftBrace)?;
449
450        let body = self.parse_block()?;
451
452        self.expect_token(Token::RightBrace)?;
453
454        Ok(Stmt::GeneratorDef { name, params, body })
455    }
456
457    /// Parse: Lazy NAME (expr)
458    fn parse_lazy_definition(&mut self) -> Result<Stmt, ParseError> {
459        self.next_token(); // skip 'Lazy'
460
461        let name = match &self.current_token {
462            Token::Identifier(name) => name.clone(),
463            _ => {
464                return Err(ParseError::UnexpectedToken {
465                    expected: "identifier".to_string(),
466                    found: self.current_token.clone(),
467                    line: self.current_line,
468                    column: self.current_column,
469                });
470            }
471        };
472
473        self.next_token();
474        self.expect_token(Token::LeftParen)?;
475
476        let expr = self.parse_expression(Precedence::Lowest)?;
477
478        self.expect_token(Token::RightParen)?;
479
480        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
481            self.next_token();
482        }
483
484        Ok(Stmt::LazyDef { name, expr })
485    }
486
487    /// Parse: Return expr
488    fn parse_return_statement(&mut self) -> Result<Stmt, ParseError> {
489        self.next_token(); // skip 'Return'
490
491        let expr =
492            if self.current_token == Token::Newline || self.current_token == Token::RightBrace {
493                Expr::Null
494            } else {
495                self.parse_expression(Precedence::Lowest)?
496            };
497
498        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
499            self.next_token();
500        }
501
502        Ok(Stmt::Return(expr))
503    }
504
505    /// Parse: Yield expr
506    fn parse_yield_statement(&mut self) -> Result<Stmt, ParseError> {
507        self.next_token(); // skip 'Yield'
508
509        let expr =
510            if self.current_token == Token::Newline || self.current_token == Token::RightBrace {
511                Expr::Null
512            } else {
513                self.parse_expression(Precedence::Lowest)?
514            };
515
516        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
517            self.next_token();
518        }
519
520        Ok(Stmt::Yield(expr))
521    }
522
523    /// Parse: Break
524    fn parse_break_statement(&mut self) -> Result<Stmt, ParseError> {
525        self.next_token(); // skip 'Break'
526
527        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
528            self.next_token();
529        }
530
531        Ok(Stmt::Break)
532    }
533
534    /// Parse: Continue
535    fn parse_continue_statement(&mut self) -> Result<Stmt, ParseError> {
536        self.next_token(); // skip 'Continue'
537
538        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
539            self.next_token();
540        }
541
542        Ok(Stmt::Continue)
543    }
544
545    /// Parse: While (condition) { body }
546    fn parse_while_statement(&mut self) -> Result<Stmt, ParseError> {
547        self.next_token(); // skip 'While'
548        self.expect_token(Token::LeftParen)?;
549
550        let condition = self.parse_expression(Precedence::Lowest)?;
551
552        self.expect_token(Token::RightParen)?;
553        self.skip_newlines();
554        self.expect_token(Token::LeftBrace)?;
555
556        let body = self.parse_block()?;
557
558        self.expect_token(Token::RightBrace)?;
559
560        Ok(Stmt::While { condition, body })
561    }
562
563    /// Parse: For VAR In ITERABLE { body }
564    fn parse_for_statement(&mut self) -> Result<Stmt, ParseError> {
565        self.next_token(); // skip 'For'
566
567        let first_var = match &self.current_token {
568            Token::Identifier(name) => name.clone(),
569            _ => {
570                return Err(ParseError::UnexpectedToken {
571                    expected: "identifier".to_string(),
572                    found: self.current_token.clone(),
573                    line: self.current_line,
574                    column: self.current_column,
575                });
576            }
577        };
578
579        self.next_token();
580
581        // Check for indexed for loop: For INDEX, VALUE In ...
582        if self.current_token == Token::Comma {
583            self.next_token(); // skip comma
584
585            let second_var = match &self.current_token {
586                Token::Identifier(name) => name.clone(),
587                _ => {
588                    return Err(ParseError::UnexpectedToken {
589                        expected: "identifier".to_string(),
590                        found: self.current_token.clone(),
591                        line: self.current_line,
592                        column: self.current_column,
593                    });
594                }
595            };
596
597            self.next_token();
598            self.expect_token(Token::In)?;
599
600            let iterable = self.parse_expression(Precedence::Lowest)?;
601
602            self.skip_newlines();
603            self.expect_token(Token::LeftBrace)?;
604
605            let body = self.parse_block()?;
606
607            self.expect_token(Token::RightBrace)?;
608
609            return Ok(Stmt::ForIndexed {
610                index_var: first_var,
611                value_var: second_var,
612                iterable,
613                body,
614            });
615        }
616
617        // Simple for loop: For VAR In ...
618        self.expect_token(Token::In)?;
619
620        let iterable = self.parse_expression(Precedence::Lowest)?;
621
622        self.skip_newlines();
623        self.expect_token(Token::LeftBrace)?;
624
625        let body = self.parse_block()?;
626
627        self.expect_token(Token::RightBrace)?;
628
629        Ok(Stmt::For {
630            var: first_var,
631            iterable,
632            body,
633        })
634    }
635
636    /// Parse: Switch (expr) { Case val: ... Default: ... }
637    fn parse_switch_statement(&mut self) -> Result<Stmt, ParseError> {
638        self.next_token(); // skip 'Switch'
639        self.expect_token(Token::LeftParen)?;
640
641        let expr = self.parse_expression(Precedence::Lowest)?;
642
643        self.expect_token(Token::RightParen)?;
644        self.skip_newlines();
645        self.expect_token(Token::LeftBrace)?;
646        self.skip_newlines();
647
648        let mut cases = Vec::new();
649        let mut default = None;
650
651        while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
652            if self.current_token == Token::Case {
653                self.next_token();
654                let case_expr = self.parse_expression(Precedence::Lowest)?;
655                self.expect_token(Token::Colon)?;
656                self.skip_newlines();
657
658                let mut case_body = Vec::new();
659                while self.current_token != Token::Case
660                    && self.current_token != Token::Default
661                    && self.current_token != Token::RightBrace
662                    && self.current_token != Token::EOF
663                {
664                    case_body.push(self.parse_statement()?);
665                    self.skip_newlines();
666                }
667
668                cases.push((case_expr, case_body));
669            } else if self.current_token == Token::Default {
670                self.next_token();
671                self.expect_token(Token::Colon)?;
672                self.skip_newlines();
673
674                let mut default_body = Vec::new();
675                while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
676                    default_body.push(self.parse_statement()?);
677                    self.skip_newlines();
678                }
679
680                default = Some(default_body);
681                break;
682            } else {
683                self.next_token();
684            }
685        }
686
687        self.expect_token(Token::RightBrace)?;
688
689        Ok(Stmt::Switch {
690            expr,
691            cases,
692            default,
693        })
694    }
695
696    /// Parse:
697    /// - Import {NAME1, NAME2} From "path"
698    /// - Import NAME As ALIAS From "path"
699    /// - Import NS From "path" (namespace import)
700    fn parse_import_statement(&mut self) -> Result<Stmt, ParseError> {
701        self.next_token(); // skip 'Import'
702
703        let mut names = Vec::new();
704        let mut aliases = Vec::new();
705        let mut namespace: Option<String> = None;
706
707        // Import {NAME1, NAME2, ...}
708        if self.current_token == Token::LeftBrace {
709            self.next_token();
710            self.skip_newlines();
711
712            while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
713                let name = match &self.current_token {
714                    Token::Identifier(n) => n.clone(),
715                    _ => {
716                        return Err(ParseError::UnexpectedToken {
717                            expected: "identifier".to_string(),
718                            found: self.current_token.clone(),
719                            line: self.current_line,
720                            column: self.current_column,
721                        });
722                    }
723                };
724
725                self.next_token();
726
727                // Check for alias: as ALIAS
728                let alias = if self.current_token == Token::As {
729                    self.next_token();
730                    if let Token::Identifier(a) = &self.current_token.clone() {
731                        let alias_name = a.clone();
732                        self.next_token();
733                        Some(alias_name)
734                    } else {
735                        None
736                    }
737                } else {
738                    None
739                };
740
741                names.push(name);
742                aliases.push(alias);
743
744                if self.current_token == Token::Comma {
745                    self.next_token();
746                    self.skip_newlines();
747                } else {
748                    break;
749                }
750            }
751
752            self.expect_token(Token::RightBrace)?;
753        } else {
754            // Import NAME ...
755            let name = match &self.current_token {
756                Token::Identifier(n) => n.clone(),
757                _ => {
758                    return Err(ParseError::UnexpectedToken {
759                        expected: "identifier".to_string(),
760                        found: self.current_token.clone(),
761                        line: self.current_line,
762                        column: self.current_column,
763                    });
764                }
765            };
766            self.next_token();
767
768            // If `As` present: named import with alias.
769            // Otherwise, if directly followed by `From`: namespace import.
770            if self.current_token == Token::As {
771                self.next_token();
772                let alias = if let Token::Identifier(a) = &self.current_token.clone() {
773                    let alias_name = a.clone();
774                    self.next_token();
775                    Some(alias_name)
776                } else {
777                    None
778                };
779                names.push(name);
780                aliases.push(alias);
781            } else {
782                // Namespace import
783                namespace = Some(name);
784            }
785        }
786
787        self.expect_token(Token::From)?;
788
789        let path = match &self.current_token {
790            Token::String(p) => p.clone(),
791            _ => {
792                return Err(ParseError::UnexpectedToken {
793                    expected: "string".to_string(),
794                    found: self.current_token.clone(),
795                    line: self.current_line,
796                    column: self.current_column,
797                });
798            }
799        };
800
801        self.next_token();
802
803        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
804            self.next_token();
805        }
806
807        Ok(Stmt::Import {
808            names,
809            path,
810            aliases,
811            namespace,
812        })
813    }
814
815    /// Parse: Export NAME
816    fn parse_export_statement(&mut self) -> Result<Stmt, ParseError> {
817        self.next_token(); // skip 'Export'
818
819        let name = match &self.current_token {
820            Token::Identifier(n) => n.clone(),
821            _ => {
822                return Err(ParseError::UnexpectedToken {
823                    expected: "identifier".to_string(),
824                    found: self.current_token.clone(),
825                    line: self.current_line,
826                    column: self.current_column,
827                });
828            }
829        };
830
831        self.next_token();
832
833        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
834            self.next_token();
835        }
836
837        Ok(Stmt::Export(name))
838    }
839
840    /// Parse: Throw expr
841    fn parse_throw_statement(&mut self) -> Result<Stmt, ParseError> {
842        self.next_token(); // skip 'Throw'
843
844        let expr = self.parse_expression(Precedence::Lowest)?;
845
846        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
847            self.next_token();
848        }
849
850        Ok(Stmt::Throw(expr))
851    }
852
853    /// Parse expression as statement
854    fn parse_expression_statement(&mut self) -> Result<Stmt, ParseError> {
855        let expr = self.parse_expression(Precedence::Lowest)?;
856
857        if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
858            self.next_token();
859        }
860
861        Ok(Stmt::Expression(expr))
862    }
863
864    /// Parse parameter list: (A, B, C)
865    fn parse_parameter_list(&mut self) -> Result<Vec<String>, ParseError> {
866        let mut params = Vec::new();
867
868        if self.current_token == Token::RightParen {
869            return Ok(params);
870        }
871
872        while let Token::Identifier(name) = &self.current_token {
873            // Validate parameter name (allow flexible naming)
874            self.validate_identifier_internal(name, true)?;
875            params.push(name.clone());
876            self.next_token();
877
878            if self.current_token == Token::Comma {
879                self.next_token();
880            } else {
881                break;
882            }
883        }
884
885        Ok(params)
886    }
887
888    /// Parse a block of statements: { stmt1 stmt2 ... }
889    fn parse_block(&mut self) -> Result<Vec<Stmt>, ParseError> {
890        let mut statements = Vec::new();
891
892        self.skip_newlines();
893
894        while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
895            statements.push(self.parse_statement()?);
896            self.skip_newlines();
897        }
898
899        Ok(statements)
900    }
901
902    /// Parse an expression using Pratt parsing
903    fn parse_expression(&mut self, precedence: Precedence) -> Result<Expr, ParseError> {
904        let mut left = self.parse_prefix()?;
905
906        // After parse_prefix, current_token is at the first token after the prefix expression
907        while precedence < self.current_precedence()
908            && self.current_token != Token::Newline
909            && self.current_token != Token::Semicolon
910            && self.current_token != Token::EOF
911            && self.current_token != Token::RightParen
912            && self.current_token != Token::RightBracket
913            && self.current_token != Token::RightBrace
914            && self.current_token != Token::Comma
915            && self.current_token != Token::Colon
916        {
917            left = self.parse_infix(left)?;
918        }
919
920        Ok(left)
921    }
922
923    /// Parse prefix expressions
924    fn parse_prefix(&mut self) -> Result<Expr, ParseError> {
925        match &self.current_token.clone() {
926            Token::Number(n) => {
927                let num = *n;
928                self.next_token();
929                Ok(Expr::Number(num))
930            }
931            Token::BigInteger(s) => {
932                let big_int_str = s.clone();
933                self.next_token();
934                Ok(Expr::BigInteger(big_int_str))
935            }
936            Token::String(s) => {
937                let string = s.clone();
938                self.next_token();
939                Ok(Expr::String(string))
940            }
941            Token::Boolean(b) => {
942                let bool_val = *b;
943                self.next_token();
944                Ok(Expr::Boolean(bool_val))
945            }
946            Token::Null => {
947                self.next_token();
948                Ok(Expr::Null)
949            }
950            Token::Identifier(name) => {
951                let ident = name.clone();
952                self.next_token();
953                Ok(Expr::Identifier(ident))
954            }
955            Token::LeftParen => self.parse_grouped_expression(),
956            Token::LeftBracket => self.parse_array_literal(),
957            Token::LeftBrace => self.parse_dict_literal(),
958            Token::Minus => self.parse_unary_expression(UnaryOp::Minus),
959            Token::Not => self.parse_unary_expression(UnaryOp::Not),
960            Token::If => self.parse_if_expression(),
961            Token::Func => self.parse_lambda_expression(),
962            Token::Lambda => self.parse_lambda_arrow_expression(),
963            _ => Err(ParseError::InvalidExpression {
964                message: "Unexpected token in expression".to_string(),
965                line: self.current_line,
966                column: self.current_column,
967            }),
968        }
969    }
970
971    /// Parse infix expressions
972    fn parse_infix(&mut self, left: Expr) -> Result<Expr, ParseError> {
973        match &self.current_token {
974            Token::Plus
975            | Token::Minus
976            | Token::Multiply
977            | Token::Divide
978            | Token::Modulo
979            | Token::Equal
980            | Token::NotEqual
981            | Token::Less
982            | Token::LessEqual
983            | Token::Greater
984            | Token::GreaterEqual
985            | Token::And
986            | Token::Or => self.parse_binary_expression(left),
987            Token::LeftParen => self.parse_call_expression(left),
988            Token::LeftBracket => self.parse_index_expression(left),
989            _ => Ok(left),
990        }
991    }
992
993    /// Parse grouped expression: (expr)
994    fn parse_grouped_expression(&mut self) -> Result<Expr, ParseError> {
995        self.next_token(); // skip '('
996
997        let expr = self.parse_expression(Precedence::Lowest)?;
998
999        // parse_expression returns with current_token at the first token after the expression
1000        // which should be ')'
1001        if self.current_token == Token::RightParen {
1002            self.next_token(); // move past ')'
1003            Ok(expr)
1004        } else {
1005            Err(ParseError::UnexpectedToken {
1006                expected: "RightParen".to_string(),
1007                found: self.current_token.clone(),
1008                line: self.current_line,
1009                column: self.current_column,
1010            })
1011        }
1012    }
1013    /// Parse array literal: [1, 2, 3]
1014    fn parse_array_literal(&mut self) -> Result<Expr, ParseError> {
1015        self.next_token(); // skip '['
1016
1017        let mut elements = Vec::new();
1018
1019        self.skip_newlines();
1020
1021        while self.current_token != Token::RightBracket && self.current_token != Token::EOF {
1022            elements.push(self.parse_expression(Precedence::Lowest)?);
1023
1024            self.skip_newlines();
1025
1026            if self.current_token == Token::Comma {
1027                self.next_token();
1028                self.skip_newlines();
1029            } else if self.current_token == Token::RightBracket {
1030                break;
1031            }
1032        }
1033
1034        self.expect_token(Token::RightBracket)?;
1035
1036        Ok(Expr::Array(elements))
1037    }
1038
1039    /// Parse dictionary literal: {key: value, ...}
1040    fn parse_dict_literal(&mut self) -> Result<Expr, ParseError> {
1041        self.next_token(); // skip '{'
1042
1043        let mut pairs = Vec::new();
1044
1045        self.skip_newlines();
1046
1047        while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
1048            let key = match &self.current_token {
1049                Token::Identifier(k) => k.clone(),
1050                Token::String(k) => k.clone(),
1051                _ => {
1052                    return Err(ParseError::UnexpectedToken {
1053                        expected: "identifier or string".to_string(),
1054                        found: self.current_token.clone(),
1055                        line: self.current_line,
1056                        column: self.current_column,
1057                    });
1058                }
1059            };
1060
1061            self.next_token();
1062            self.expect_token(Token::Colon)?;
1063
1064            let value = self.parse_expression(Precedence::Lowest)?;
1065
1066            pairs.push((key, value));
1067
1068            self.skip_newlines();
1069
1070            if self.current_token == Token::Comma {
1071                self.next_token();
1072                self.skip_newlines();
1073            } else if self.current_token == Token::RightBrace {
1074                break;
1075            }
1076        }
1077
1078        self.expect_token(Token::RightBrace)?;
1079
1080        Ok(Expr::Dict(pairs))
1081    }
1082
1083    /// Parse unary expression: -expr or !expr
1084    fn parse_unary_expression(&mut self, op: UnaryOp) -> Result<Expr, ParseError> {
1085        self.next_token(); // skip operator
1086
1087        let expr = self.parse_expression(Precedence::Prefix)?;
1088
1089        Ok(Expr::unary(op, expr))
1090    }
1091
1092    /// Parse binary expression: left op right
1093    fn parse_binary_expression(&mut self, left: Expr) -> Result<Expr, ParseError> {
1094        let op = match &self.current_token {
1095            Token::Plus => BinOp::Add,
1096            Token::Minus => BinOp::Subtract,
1097            Token::Multiply => BinOp::Multiply,
1098            Token::Divide => BinOp::Divide,
1099            Token::Modulo => BinOp::Modulo,
1100            Token::Equal => BinOp::Equal,
1101            Token::NotEqual => BinOp::NotEqual,
1102            Token::Less => BinOp::Less,
1103            Token::LessEqual => BinOp::LessEqual,
1104            Token::Greater => BinOp::Greater,
1105            Token::GreaterEqual => BinOp::GreaterEqual,
1106            Token::And => BinOp::And,
1107            Token::Or => BinOp::Or,
1108            _ => {
1109                return Err(ParseError::InvalidExpression {
1110                    message: "Invalid binary operator".to_string(),
1111                    line: self.current_line,
1112                    column: self.current_column,
1113                });
1114            }
1115        };
1116
1117        let precedence = self.current_precedence();
1118        self.next_token();
1119
1120        let right = self.parse_expression(precedence)?;
1121
1122        Ok(Expr::binary(left, op, right))
1123    }
1124
1125    /// Parse function call: func(arg1, arg2, ...)
1126    fn parse_call_expression(&mut self, func: Expr) -> Result<Expr, ParseError> {
1127        self.next_token(); // skip '('
1128
1129        let mut args = Vec::new();
1130
1131        self.skip_newlines();
1132
1133        while self.current_token != Token::RightParen && self.current_token != Token::EOF {
1134            args.push(self.parse_expression(Precedence::Lowest)?);
1135
1136            if self.current_token == Token::Comma {
1137                self.next_token();
1138                self.skip_newlines();
1139            } else {
1140                break;
1141            }
1142        }
1143
1144        self.expect_token(Token::RightParen)?;
1145
1146        Ok(Expr::call(func, args))
1147    }
1148
1149    /// Parse index expression: object[index]
1150    fn parse_index_expression(&mut self, object: Expr) -> Result<Expr, ParseError> {
1151        self.next_token(); // skip '['
1152
1153        let index = self.parse_expression(Precedence::Lowest)?;
1154
1155        self.expect_token(Token::RightBracket)?;
1156
1157        Ok(Expr::index(object, index))
1158    }
1159
1160    /// Parse if expression: If (cond) { ... } Elif (cond) { ... } Else { ... }
1161    fn parse_if_expression(&mut self) -> Result<Expr, ParseError> {
1162        self.next_token(); // skip 'If'
1163        self.expect_token(Token::LeftParen)?;
1164
1165        let condition = self.parse_expression(Precedence::Lowest)?;
1166
1167        self.expect_token(Token::RightParen)?;
1168        self.skip_newlines();
1169        self.expect_token(Token::LeftBrace)?;
1170
1171        let then_branch = self.parse_block()?;
1172
1173        self.expect_token(Token::RightBrace)?;
1174        self.skip_newlines();
1175
1176        let mut elif_branches = Vec::new();
1177        while self.current_token == Token::Elif {
1178            self.next_token();
1179            self.expect_token(Token::LeftParen)?;
1180
1181            let elif_cond = self.parse_expression(Precedence::Lowest)?;
1182
1183            self.expect_token(Token::RightParen)?;
1184            self.skip_newlines();
1185            self.expect_token(Token::LeftBrace)?;
1186
1187            let elif_body = self.parse_block()?;
1188
1189            self.expect_token(Token::RightBrace)?;
1190            self.skip_newlines();
1191
1192            elif_branches.push((elif_cond, elif_body));
1193        }
1194
1195        let else_branch = if self.current_token == Token::Else {
1196            self.next_token();
1197            self.skip_newlines();
1198            self.expect_token(Token::LeftBrace)?;
1199
1200            let else_body = self.parse_block()?;
1201
1202            self.expect_token(Token::RightBrace)?;
1203
1204            Some(else_body)
1205        } else {
1206            None
1207        };
1208
1209        Ok(Expr::If {
1210            condition: Box::new(condition),
1211            then_branch,
1212            elif_branches,
1213            else_branch,
1214        })
1215    }
1216
1217    /// Parse lambda expression: Func(params) { body }
1218    fn parse_lambda_expression(&mut self) -> Result<Expr, ParseError> {
1219        self.next_token(); // skip 'Func'
1220        self.expect_token(Token::LeftParen)?;
1221
1222        let params = self.parse_parameter_list()?;
1223
1224        self.expect_token(Token::RightParen)?;
1225        self.skip_newlines();
1226        self.expect_token(Token::LeftBrace)?;
1227
1228        let body = self.parse_block()?;
1229
1230        self.expect_token(Token::RightBrace)?;
1231
1232        Ok(Expr::Lambda { params, body })
1233    }
1234
1235    /// Parse lambda arrow expression: Lambda X -> expr or Lambda (X, Y) -> expr
1236    fn parse_lambda_arrow_expression(&mut self) -> Result<Expr, ParseError> {
1237        self.next_token(); // skip 'Lambda'
1238
1239        let params = if self.current_token == Token::LeftParen {
1240            // Multiple parameters: Lambda (X, Y) -> expr
1241            self.next_token(); // skip '('
1242            let params = self.parse_parameter_list()?;
1243            self.expect_token(Token::RightParen)?;
1244            params
1245        } else {
1246            // Single parameter: Lambda X -> expr
1247            match &self.current_token {
1248                Token::Identifier(name) => {
1249                    self.validate_identifier_internal(name, true)?;
1250                    let param = name.clone();
1251                    self.next_token();
1252                    vec![param]
1253                }
1254                _ => {
1255                    return Err(ParseError::UnexpectedToken {
1256                        expected: "identifier or '('".to_string(),
1257                        found: self.current_token.clone(),
1258                        line: self.current_line,
1259                        column: self.current_column,
1260                    });
1261                }
1262            }
1263        };
1264
1265        // Expect arrow
1266        self.expect_token(Token::Arrow)?;
1267
1268        // Parse the expression body
1269        let expr = self.parse_expression(Precedence::Lowest)?;
1270
1271        // Wrap the expression in a Return statement
1272        let body = vec![Stmt::Return(expr)];
1273
1274        Ok(Expr::Lambda { params, body })
1275    }
1276}