1use crate::ast::{BinOp, Expr, Program, Stmt, UnaryOp};
7use crate::lexer::Lexer;
8use crate::token::Token;
9
10#[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#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
107enum Precedence {
108 Lowest = 0,
109 Or = 1, And = 2, Equals = 3, Comparison = 4, Sum = 5, Product = 6, Prefix = 7, Call = 8, Index = 9, }
119
120pub 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, peek_had_whitespace: bool, }
130
131impl Parser {
132 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 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 fn skip_newlines(&mut self) {
165 while self.current_token == Token::Newline {
166 self.next_token();
167 }
168 }
169
170 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 fn validate_identifier(&self, name: &str) -> Result<(), ParseError> {
187 self.validate_identifier_internal(name, false)
188 }
189
190 fn validate_identifier_internal(&self, name: &str, is_param: bool) -> Result<(), ParseError> {
193 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 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 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 fn current_precedence(&self) -> Precedence {
240 self.token_precedence(&self.current_token)
241 }
242
243 #[allow(dead_code)]
245 fn peek_precedence(&self) -> Precedence {
246 self.token_precedence(&self.peek_token)
247 }
248
249 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 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 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 fn parse_set_statement(&mut self) -> Result<Stmt, ParseError> {
304 self.next_token(); 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(); if self.current_token == Token::LeftBracket {
334 let has_space_before_bracket = self.current_had_whitespace;
337
338 if has_space_before_bracket {
339 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 self.next_token(); let index = self.parse_expression(Precedence::Lowest)?;
352
353 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(); 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 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 fn parse_func_definition(&mut self) -> Result<Stmt, ParseError> {
391 self.next_token(); let name = match &self.current_token {
394 Token::Identifier(name) => {
395 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(); 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 fn parse_generator_definition(&mut self) -> Result<Stmt, ParseError> {
427 self.next_token(); 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 fn parse_lazy_definition(&mut self) -> Result<Stmt, ParseError> {
459 self.next_token(); 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 fn parse_return_statement(&mut self) -> Result<Stmt, ParseError> {
489 self.next_token(); 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 fn parse_yield_statement(&mut self) -> Result<Stmt, ParseError> {
507 self.next_token(); 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 fn parse_break_statement(&mut self) -> Result<Stmt, ParseError> {
525 self.next_token(); if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
528 self.next_token();
529 }
530
531 Ok(Stmt::Break)
532 }
533
534 fn parse_continue_statement(&mut self) -> Result<Stmt, ParseError> {
536 self.next_token(); if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
539 self.next_token();
540 }
541
542 Ok(Stmt::Continue)
543 }
544
545 fn parse_while_statement(&mut self) -> Result<Stmt, ParseError> {
547 self.next_token(); 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 fn parse_for_statement(&mut self) -> Result<Stmt, ParseError> {
565 self.next_token(); 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 if self.current_token == Token::Comma {
583 self.next_token(); 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 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 fn parse_switch_statement(&mut self) -> Result<Stmt, ParseError> {
638 self.next_token(); 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 fn parse_import_statement(&mut self) -> Result<Stmt, ParseError> {
698 self.next_token(); let mut names = Vec::new();
701 let mut aliases = Vec::new();
702
703 if self.current_token == Token::LeftBrace {
705 self.next_token();
706 self.skip_newlines();
707
708 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
709 let name = match &self.current_token {
710 Token::Identifier(n) => n.clone(),
711 _ => {
712 return Err(ParseError::UnexpectedToken {
713 expected: "identifier".to_string(),
714 found: self.current_token.clone(),
715 line: self.current_line,
716 column: self.current_column,
717 });
718 }
719 };
720
721 self.next_token();
722
723 let alias = if self.current_token == Token::As {
725 self.next_token();
726 if let Token::Identifier(a) = &self.current_token.clone() {
727 let alias_name = a.clone();
728 self.next_token();
729 Some(alias_name)
730 } else {
731 None
732 }
733 } else {
734 None
735 };
736
737 names.push(name);
738 aliases.push(alias);
739
740 if self.current_token == Token::Comma {
741 self.next_token();
742 self.skip_newlines();
743 } else {
744 break;
745 }
746 }
747
748 self.expect_token(Token::RightBrace)?;
749 } else {
750 let name = match &self.current_token {
752 Token::Identifier(n) => n.clone(),
753 _ => {
754 return Err(ParseError::UnexpectedToken {
755 expected: "identifier".to_string(),
756 found: self.current_token.clone(),
757 line: self.current_line,
758 column: self.current_column,
759 });
760 }
761 };
762 self.next_token();
763
764 let alias = if self.current_token == Token::As {
765 self.next_token();
766 if let Token::Identifier(a) = &self.current_token.clone() {
767 let alias_name = a.clone();
768 self.next_token();
769 Some(alias_name)
770 } else {
771 None
772 }
773 } else {
774 None
775 };
776
777 names.push(name);
778 aliases.push(alias);
779 }
780
781 self.expect_token(Token::From)?;
782
783 let path = match &self.current_token {
784 Token::String(p) => p.clone(),
785 _ => {
786 return Err(ParseError::UnexpectedToken {
787 expected: "string".to_string(),
788 found: self.current_token.clone(),
789 line: self.current_line,
790 column: self.current_column,
791 });
792 }
793 };
794
795 self.next_token();
796
797 if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
798 self.next_token();
799 }
800
801 Ok(Stmt::Import {
802 names,
803 path,
804 aliases,
805 })
806 }
807
808 fn parse_export_statement(&mut self) -> Result<Stmt, ParseError> {
810 self.next_token(); let name = match &self.current_token {
813 Token::Identifier(n) => n.clone(),
814 _ => {
815 return Err(ParseError::UnexpectedToken {
816 expected: "identifier".to_string(),
817 found: self.current_token.clone(),
818 line: self.current_line,
819 column: self.current_column,
820 });
821 }
822 };
823
824 self.next_token();
825
826 if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
827 self.next_token();
828 }
829
830 Ok(Stmt::Export(name))
831 }
832
833 fn parse_throw_statement(&mut self) -> Result<Stmt, ParseError> {
835 self.next_token(); let expr = self.parse_expression(Precedence::Lowest)?;
838
839 if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
840 self.next_token();
841 }
842
843 Ok(Stmt::Throw(expr))
844 }
845
846 fn parse_expression_statement(&mut self) -> Result<Stmt, ParseError> {
848 let expr = self.parse_expression(Precedence::Lowest)?;
849
850 if self.current_token == Token::Newline || self.current_token == Token::Semicolon {
851 self.next_token();
852 }
853
854 Ok(Stmt::Expression(expr))
855 }
856
857 fn parse_parameter_list(&mut self) -> Result<Vec<String>, ParseError> {
859 let mut params = Vec::new();
860
861 if self.current_token == Token::RightParen {
862 return Ok(params);
863 }
864
865 while let Token::Identifier(name) = &self.current_token {
866 self.validate_identifier_internal(name, true)?;
868 params.push(name.clone());
869 self.next_token();
870
871 if self.current_token == Token::Comma {
872 self.next_token();
873 } else {
874 break;
875 }
876 }
877
878 Ok(params)
879 }
880
881 fn parse_block(&mut self) -> Result<Vec<Stmt>, ParseError> {
883 let mut statements = Vec::new();
884
885 self.skip_newlines();
886
887 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
888 statements.push(self.parse_statement()?);
889 self.skip_newlines();
890 }
891
892 Ok(statements)
893 }
894
895 fn parse_expression(&mut self, precedence: Precedence) -> Result<Expr, ParseError> {
897 let mut left = self.parse_prefix()?;
898
899 while precedence < self.current_precedence()
901 && self.current_token != Token::Newline
902 && self.current_token != Token::Semicolon
903 && self.current_token != Token::EOF
904 && self.current_token != Token::RightParen
905 && self.current_token != Token::RightBracket
906 && self.current_token != Token::RightBrace
907 && self.current_token != Token::Comma
908 && self.current_token != Token::Colon
909 {
910 left = self.parse_infix(left)?;
911 }
912
913 Ok(left)
914 }
915
916 fn parse_prefix(&mut self) -> Result<Expr, ParseError> {
918 match &self.current_token.clone() {
919 Token::Number(n) => {
920 let num = *n;
921 self.next_token();
922 Ok(Expr::Number(num))
923 }
924 Token::BigInteger(s) => {
925 let big_int_str = s.clone();
926 self.next_token();
927 Ok(Expr::BigInteger(big_int_str))
928 }
929 Token::String(s) => {
930 let string = s.clone();
931 self.next_token();
932 Ok(Expr::String(string))
933 }
934 Token::Boolean(b) => {
935 let bool_val = *b;
936 self.next_token();
937 Ok(Expr::Boolean(bool_val))
938 }
939 Token::Null => {
940 self.next_token();
941 Ok(Expr::Null)
942 }
943 Token::Identifier(name) => {
944 let ident = name.clone();
945 self.next_token();
946 Ok(Expr::Identifier(ident))
947 }
948 Token::LeftParen => self.parse_grouped_expression(),
949 Token::LeftBracket => self.parse_array_literal(),
950 Token::LeftBrace => self.parse_dict_literal(),
951 Token::Minus => self.parse_unary_expression(UnaryOp::Minus),
952 Token::Not => self.parse_unary_expression(UnaryOp::Not),
953 Token::If => self.parse_if_expression(),
954 Token::Func => self.parse_lambda_expression(),
955 Token::Lambda => self.parse_lambda_arrow_expression(),
956 _ => Err(ParseError::InvalidExpression {
957 message: "Unexpected token in expression".to_string(),
958 line: self.current_line,
959 column: self.current_column,
960 }),
961 }
962 }
963
964 fn parse_infix(&mut self, left: Expr) -> Result<Expr, ParseError> {
966 match &self.current_token {
967 Token::Plus
968 | Token::Minus
969 | Token::Multiply
970 | Token::Divide
971 | Token::Modulo
972 | Token::Equal
973 | Token::NotEqual
974 | Token::Less
975 | Token::LessEqual
976 | Token::Greater
977 | Token::GreaterEqual
978 | Token::And
979 | Token::Or => self.parse_binary_expression(left),
980 Token::LeftParen => self.parse_call_expression(left),
981 Token::LeftBracket => self.parse_index_expression(left),
982 _ => Ok(left),
983 }
984 }
985
986 fn parse_grouped_expression(&mut self) -> Result<Expr, ParseError> {
988 self.next_token(); let expr = self.parse_expression(Precedence::Lowest)?;
991
992 if self.current_token == Token::RightParen {
995 self.next_token(); Ok(expr)
997 } else {
998 Err(ParseError::UnexpectedToken {
999 expected: "RightParen".to_string(),
1000 found: self.current_token.clone(),
1001 line: self.current_line,
1002 column: self.current_column,
1003 })
1004 }
1005 }
1006 fn parse_array_literal(&mut self) -> Result<Expr, ParseError> {
1008 self.next_token(); let mut elements = Vec::new();
1011
1012 self.skip_newlines();
1013
1014 while self.current_token != Token::RightBracket && self.current_token != Token::EOF {
1015 elements.push(self.parse_expression(Precedence::Lowest)?);
1016
1017 self.skip_newlines();
1018
1019 if self.current_token == Token::Comma {
1020 self.next_token();
1021 self.skip_newlines();
1022 } else if self.current_token == Token::RightBracket {
1023 break;
1024 }
1025 }
1026
1027 self.expect_token(Token::RightBracket)?;
1028
1029 Ok(Expr::Array(elements))
1030 }
1031
1032 fn parse_dict_literal(&mut self) -> Result<Expr, ParseError> {
1034 self.next_token(); let mut pairs = Vec::new();
1037
1038 self.skip_newlines();
1039
1040 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
1041 let key = match &self.current_token {
1042 Token::Identifier(k) => k.clone(),
1043 Token::String(k) => k.clone(),
1044 _ => {
1045 return Err(ParseError::UnexpectedToken {
1046 expected: "identifier or string".to_string(),
1047 found: self.current_token.clone(),
1048 line: self.current_line,
1049 column: self.current_column,
1050 });
1051 }
1052 };
1053
1054 self.next_token();
1055 self.expect_token(Token::Colon)?;
1056
1057 let value = self.parse_expression(Precedence::Lowest)?;
1058
1059 pairs.push((key, value));
1060
1061 self.skip_newlines();
1062
1063 if self.current_token == Token::Comma {
1064 self.next_token();
1065 self.skip_newlines();
1066 } else if self.current_token == Token::RightBrace {
1067 break;
1068 }
1069 }
1070
1071 self.expect_token(Token::RightBrace)?;
1072
1073 Ok(Expr::Dict(pairs))
1074 }
1075
1076 fn parse_unary_expression(&mut self, op: UnaryOp) -> Result<Expr, ParseError> {
1078 self.next_token(); let expr = self.parse_expression(Precedence::Prefix)?;
1081
1082 Ok(Expr::unary(op, expr))
1083 }
1084
1085 fn parse_binary_expression(&mut self, left: Expr) -> Result<Expr, ParseError> {
1087 let op = match &self.current_token {
1088 Token::Plus => BinOp::Add,
1089 Token::Minus => BinOp::Subtract,
1090 Token::Multiply => BinOp::Multiply,
1091 Token::Divide => BinOp::Divide,
1092 Token::Modulo => BinOp::Modulo,
1093 Token::Equal => BinOp::Equal,
1094 Token::NotEqual => BinOp::NotEqual,
1095 Token::Less => BinOp::Less,
1096 Token::LessEqual => BinOp::LessEqual,
1097 Token::Greater => BinOp::Greater,
1098 Token::GreaterEqual => BinOp::GreaterEqual,
1099 Token::And => BinOp::And,
1100 Token::Or => BinOp::Or,
1101 _ => {
1102 return Err(ParseError::InvalidExpression {
1103 message: "Invalid binary operator".to_string(),
1104 line: self.current_line,
1105 column: self.current_column,
1106 });
1107 }
1108 };
1109
1110 let precedence = self.current_precedence();
1111 self.next_token();
1112
1113 let right = self.parse_expression(precedence)?;
1114
1115 Ok(Expr::binary(left, op, right))
1116 }
1117
1118 fn parse_call_expression(&mut self, func: Expr) -> Result<Expr, ParseError> {
1120 self.next_token(); let mut args = Vec::new();
1123
1124 self.skip_newlines();
1125
1126 while self.current_token != Token::RightParen && self.current_token != Token::EOF {
1127 args.push(self.parse_expression(Precedence::Lowest)?);
1128
1129 if self.current_token == Token::Comma {
1130 self.next_token();
1131 self.skip_newlines();
1132 } else {
1133 break;
1134 }
1135 }
1136
1137 self.expect_token(Token::RightParen)?;
1138
1139 Ok(Expr::call(func, args))
1140 }
1141
1142 fn parse_index_expression(&mut self, object: Expr) -> Result<Expr, ParseError> {
1144 self.next_token(); let index = self.parse_expression(Precedence::Lowest)?;
1147
1148 self.expect_token(Token::RightBracket)?;
1149
1150 Ok(Expr::index(object, index))
1151 }
1152
1153 fn parse_if_expression(&mut self) -> Result<Expr, ParseError> {
1155 self.next_token(); self.expect_token(Token::LeftParen)?;
1157
1158 let condition = self.parse_expression(Precedence::Lowest)?;
1159
1160 self.expect_token(Token::RightParen)?;
1161 self.skip_newlines();
1162 self.expect_token(Token::LeftBrace)?;
1163
1164 let then_branch = self.parse_block()?;
1165
1166 self.expect_token(Token::RightBrace)?;
1167 self.skip_newlines();
1168
1169 let mut elif_branches = Vec::new();
1170 while self.current_token == Token::Elif {
1171 self.next_token();
1172 self.expect_token(Token::LeftParen)?;
1173
1174 let elif_cond = self.parse_expression(Precedence::Lowest)?;
1175
1176 self.expect_token(Token::RightParen)?;
1177 self.skip_newlines();
1178 self.expect_token(Token::LeftBrace)?;
1179
1180 let elif_body = self.parse_block()?;
1181
1182 self.expect_token(Token::RightBrace)?;
1183 self.skip_newlines();
1184
1185 elif_branches.push((elif_cond, elif_body));
1186 }
1187
1188 let else_branch = if self.current_token == Token::Else {
1189 self.next_token();
1190 self.skip_newlines();
1191 self.expect_token(Token::LeftBrace)?;
1192
1193 let else_body = self.parse_block()?;
1194
1195 self.expect_token(Token::RightBrace)?;
1196
1197 Some(else_body)
1198 } else {
1199 None
1200 };
1201
1202 Ok(Expr::If {
1203 condition: Box::new(condition),
1204 then_branch,
1205 elif_branches,
1206 else_branch,
1207 })
1208 }
1209
1210 fn parse_lambda_expression(&mut self) -> Result<Expr, ParseError> {
1212 self.next_token(); self.expect_token(Token::LeftParen)?;
1214
1215 let params = self.parse_parameter_list()?;
1216
1217 self.expect_token(Token::RightParen)?;
1218 self.skip_newlines();
1219 self.expect_token(Token::LeftBrace)?;
1220
1221 let body = self.parse_block()?;
1222
1223 self.expect_token(Token::RightBrace)?;
1224
1225 Ok(Expr::Lambda { params, body })
1226 }
1227
1228 fn parse_lambda_arrow_expression(&mut self) -> Result<Expr, ParseError> {
1230 self.next_token(); let params = if self.current_token == Token::LeftParen {
1233 self.next_token(); let params = self.parse_parameter_list()?;
1236 self.expect_token(Token::RightParen)?;
1237 params
1238 } else {
1239 match &self.current_token {
1241 Token::Identifier(name) => {
1242 self.validate_identifier_internal(name, true)?;
1243 let param = name.clone();
1244 self.next_token();
1245 vec![param]
1246 }
1247 _ => {
1248 return Err(ParseError::UnexpectedToken {
1249 expected: "identifier or '('".to_string(),
1250 found: self.current_token.clone(),
1251 line: self.current_line,
1252 column: self.current_column,
1253 });
1254 }
1255 }
1256 };
1257
1258 self.expect_token(Token::Arrow)?;
1260
1261 let expr = self.parse_expression(Precedence::Lowest)?;
1263
1264 let body = vec![Stmt::Return(expr)];
1266
1267 Ok(Expr::Lambda { params, body })
1268 }
1269}
1270
1271#[cfg(test)]
1272mod tests {
1273 use super::*;
1274
1275 #[test]
1276 fn test_parse_set_statement() {
1277 let input = "Set X 10";
1278 let mut parser = Parser::new(input);
1279 let program = parser.parse_program().unwrap();
1280
1281 assert_eq!(program.len(), 1);
1282 match &program[0] {
1283 Stmt::Set { name, value } => {
1284 assert_eq!(name, "X");
1285 assert_eq!(*value, Expr::Number(10.0));
1286 }
1287 _ => panic!("Expected Set statement"),
1288 }
1289 }
1290
1291 #[test]
1292 fn test_parse_arithmetic() {
1293 let input = "Set X (5 + 3 * 2)";
1294 let mut parser = Parser::new(input);
1295 let program = parser.parse_program().unwrap();
1296
1297 assert_eq!(program.len(), 1);
1298 match &program[0] {
1299 Stmt::Set { name, value } => {
1300 assert_eq!(name, "X");
1301 match value {
1303 Expr::Binary { left, op, right } => {
1304 assert_eq!(**left, Expr::Number(5.0));
1305 assert_eq!(*op, BinOp::Add);
1306 match &**right {
1307 Expr::Binary { left, op, right } => {
1308 assert_eq!(**left, Expr::Number(3.0));
1309 assert_eq!(*op, BinOp::Multiply);
1310 assert_eq!(**right, Expr::Number(2.0));
1311 }
1312 _ => panic!("Expected binary expression"),
1313 }
1314 }
1315 _ => panic!("Expected binary expression"),
1316 }
1317 }
1318 _ => panic!("Expected Set statement"),
1319 }
1320 }
1321
1322 #[test]
1323 fn test_parse_function_definition() {
1324 let input = r#"
1325 Func ADD (A, B) {
1326 Return (A + B)
1327 }
1328 "#;
1329 let mut parser = Parser::new(input);
1330 let program = parser.parse_program().unwrap();
1331
1332 assert_eq!(program.len(), 1);
1333 match &program[0] {
1334 Stmt::FuncDef { name, params, body } => {
1335 assert_eq!(name, "ADD");
1336 assert_eq!(params, &vec!["A".to_string(), "B".to_string()]);
1337 assert_eq!(body.len(), 1);
1338 }
1339 _ => panic!("Expected FuncDef"),
1340 }
1341 }
1342
1343 #[test]
1344 fn test_parse_function_call() {
1345 let input = "ADD(5, 3)";
1346 let mut parser = Parser::new(input);
1347 let program = parser.parse_program().unwrap();
1348
1349 assert_eq!(program.len(), 1);
1350 match &program[0] {
1351 Stmt::Expression(Expr::Call { func, args }) => {
1352 assert_eq!(**func, Expr::Identifier("ADD".to_string()));
1353 assert_eq!(args.len(), 2);
1354 assert_eq!(args[0], Expr::Number(5.0));
1355 assert_eq!(args[1], Expr::Number(3.0));
1356 }
1357 _ => panic!("Expected function call"),
1358 }
1359 }
1360
1361 #[test]
1362 fn test_parse_array_literal() {
1363 let input = "Set ARR [1, 2, 3]";
1364 let mut parser = Parser::new(input);
1365 let program = parser.parse_program().unwrap();
1366
1367 assert_eq!(program.len(), 1);
1368 match &program[0] {
1369 Stmt::Set { name, value } => {
1370 assert_eq!(name, "ARR");
1371 match value {
1372 Expr::Array(elements) => {
1373 assert_eq!(elements.len(), 3);
1374 assert_eq!(elements[0], Expr::Number(1.0));
1375 assert_eq!(elements[1], Expr::Number(2.0));
1376 assert_eq!(elements[2], Expr::Number(3.0));
1377 }
1378 _ => panic!("Expected array"),
1379 }
1380 }
1381 _ => panic!("Expected Set statement"),
1382 }
1383 }
1384
1385 #[test]
1386 fn test_parse_if_expression() {
1387 let input = r#"
1388 If (X > 0) {
1389 Set Y 1
1390 } Else {
1391 Set Y 0
1392 }
1393 "#;
1394 let mut parser = Parser::new(input);
1395 let program = parser.parse_program().unwrap();
1396
1397 assert_eq!(program.len(), 1);
1398 match &program[0] {
1399 Stmt::Expression(Expr::If {
1400 condition,
1401 then_branch,
1402 else_branch,
1403 ..
1404 }) => {
1405 assert!(matches!(**condition, Expr::Binary { .. }));
1406 assert_eq!(then_branch.len(), 1);
1407 assert!(else_branch.is_some());
1408 }
1409 _ => panic!("Expected If expression"),
1410 }
1411 }
1412
1413 #[test]
1414 fn test_parse_for_loop() {
1415 let input = r#"
1416 For I In RANGE(0, 10) {
1417 PRINT(I)
1418 }
1419 "#;
1420 let mut parser = Parser::new(input);
1421 let program = parser.parse_program().unwrap();
1422
1423 eprintln!("Program length: {}", program.len());
1425 for (i, stmt) in program.iter().enumerate() {
1426 eprintln!("Statement {}: {:?}", i, stmt);
1427 }
1428
1429 assert_eq!(program.len(), 1);
1430 match &program[0] {
1431 Stmt::For {
1432 var,
1433 iterable,
1434 body,
1435 } => {
1436 assert_eq!(var, "I");
1437 assert!(matches!(iterable, Expr::Call { .. }));
1438 assert_eq!(body.len(), 1);
1439 }
1440 _ => panic!("Expected For statement"),
1441 }
1442 }
1443}