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> {
701 self.next_token(); let mut names = Vec::new();
704 let mut aliases = Vec::new();
705 let mut namespace: Option<String> = None;
706
707 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 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 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 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 = 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 fn parse_export_statement(&mut self) -> Result<Stmt, ParseError> {
817 self.next_token(); 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 fn parse_throw_statement(&mut self) -> Result<Stmt, ParseError> {
842 self.next_token(); 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 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 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 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 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 fn parse_expression(&mut self, precedence: Precedence) -> Result<Expr, ParseError> {
904 let mut left = self.parse_prefix()?;
905
906 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 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 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 fn parse_grouped_expression(&mut self) -> Result<Expr, ParseError> {
995 self.next_token(); let expr = self.parse_expression(Precedence::Lowest)?;
998
999 if self.current_token == Token::RightParen {
1002 self.next_token(); 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 fn parse_array_literal(&mut self) -> Result<Expr, ParseError> {
1015 self.next_token(); 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 fn parse_dict_literal(&mut self) -> Result<Expr, ParseError> {
1041 self.next_token(); 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 fn parse_unary_expression(&mut self, op: UnaryOp) -> Result<Expr, ParseError> {
1085 self.next_token(); let expr = self.parse_expression(Precedence::Prefix)?;
1088
1089 Ok(Expr::unary(op, expr))
1090 }
1091
1092 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 fn parse_call_expression(&mut self, func: Expr) -> Result<Expr, ParseError> {
1127 self.next_token(); 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 fn parse_index_expression(&mut self, object: Expr) -> Result<Expr, ParseError> {
1151 self.next_token(); let index = self.parse_expression(Precedence::Lowest)?;
1154
1155 self.expect_token(Token::RightBracket)?;
1156
1157 Ok(Expr::index(object, index))
1158 }
1159
1160 fn parse_if_expression(&mut self) -> Result<Expr, ParseError> {
1162 self.next_token(); 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 fn parse_lambda_expression(&mut self) -> Result<Expr, ParseError> {
1219 self.next_token(); 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 fn parse_lambda_arrow_expression(&mut self) -> Result<Expr, ParseError> {
1237 self.next_token(); let params = if self.current_token == Token::LeftParen {
1240 self.next_token(); let params = self.parse_parameter_list()?;
1243 self.expect_token(Token::RightParen)?;
1244 params
1245 } else {
1246 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 self.expect_token(Token::Arrow)?;
1267
1268 let expr = self.parse_expression(Precedence::Lowest)?;
1270
1271 let body = vec![Stmt::Return(expr)];
1273
1274 Ok(Expr::Lambda { params, body })
1275 }
1276}
1277
1278#[cfg(test)]
1279mod tests {
1280 use super::*;
1281
1282 #[test]
1283 fn test_parse_set_statement() {
1284 let input = "Set X 10";
1285 let mut parser = Parser::new(input);
1286 let program = parser.parse_program().unwrap();
1287
1288 assert_eq!(program.len(), 1);
1289 match &program[0] {
1290 Stmt::Set { name, value } => {
1291 assert_eq!(name, "X");
1292 assert_eq!(*value, Expr::Number(10.0));
1293 }
1294 _ => panic!("Expected Set statement"),
1295 }
1296 }
1297
1298 #[test]
1299 fn test_parse_arithmetic() {
1300 let input = "Set X (5 + 3 * 2)";
1301 let mut parser = Parser::new(input);
1302 let program = parser.parse_program().unwrap();
1303
1304 assert_eq!(program.len(), 1);
1305 match &program[0] {
1306 Stmt::Set { name, value } => {
1307 assert_eq!(name, "X");
1308 match value {
1310 Expr::Binary { left, op, right } => {
1311 assert_eq!(**left, Expr::Number(5.0));
1312 assert_eq!(*op, BinOp::Add);
1313 match &**right {
1314 Expr::Binary { left, op, right } => {
1315 assert_eq!(**left, Expr::Number(3.0));
1316 assert_eq!(*op, BinOp::Multiply);
1317 assert_eq!(**right, Expr::Number(2.0));
1318 }
1319 _ => panic!("Expected binary expression"),
1320 }
1321 }
1322 _ => panic!("Expected binary expression"),
1323 }
1324 }
1325 _ => panic!("Expected Set statement"),
1326 }
1327 }
1328
1329 #[test]
1330 fn test_parse_function_definition() {
1331 let input = r#"
1332 Func ADD (A, B) {
1333 Return (A + B)
1334 }
1335 "#;
1336 let mut parser = Parser::new(input);
1337 let program = parser.parse_program().unwrap();
1338
1339 assert_eq!(program.len(), 1);
1340 match &program[0] {
1341 Stmt::FuncDef { name, params, body } => {
1342 assert_eq!(name, "ADD");
1343 assert_eq!(params, &vec!["A".to_string(), "B".to_string()]);
1344 assert_eq!(body.len(), 1);
1345 }
1346 _ => panic!("Expected FuncDef"),
1347 }
1348 }
1349
1350 #[test]
1351 fn test_parse_function_call() {
1352 let input = "ADD(5, 3)";
1353 let mut parser = Parser::new(input);
1354 let program = parser.parse_program().unwrap();
1355
1356 assert_eq!(program.len(), 1);
1357 match &program[0] {
1358 Stmt::Expression(Expr::Call { func, args }) => {
1359 assert_eq!(**func, Expr::Identifier("ADD".to_string()));
1360 assert_eq!(args.len(), 2);
1361 assert_eq!(args[0], Expr::Number(5.0));
1362 assert_eq!(args[1], Expr::Number(3.0));
1363 }
1364 _ => panic!("Expected function call"),
1365 }
1366 }
1367
1368 #[test]
1369 fn test_parse_array_literal() {
1370 let input = "Set ARR [1, 2, 3]";
1371 let mut parser = Parser::new(input);
1372 let program = parser.parse_program().unwrap();
1373
1374 assert_eq!(program.len(), 1);
1375 match &program[0] {
1376 Stmt::Set { name, value } => {
1377 assert_eq!(name, "ARR");
1378 match value {
1379 Expr::Array(elements) => {
1380 assert_eq!(elements.len(), 3);
1381 assert_eq!(elements[0], Expr::Number(1.0));
1382 assert_eq!(elements[1], Expr::Number(2.0));
1383 assert_eq!(elements[2], Expr::Number(3.0));
1384 }
1385 _ => panic!("Expected array"),
1386 }
1387 }
1388 _ => panic!("Expected Set statement"),
1389 }
1390 }
1391
1392 #[test]
1393 fn test_parse_if_expression() {
1394 let input = r#"
1395 If (X > 0) {
1396 Set Y 1
1397 } Else {
1398 Set Y 0
1399 }
1400 "#;
1401 let mut parser = Parser::new(input);
1402 let program = parser.parse_program().unwrap();
1403
1404 assert_eq!(program.len(), 1);
1405 match &program[0] {
1406 Stmt::Expression(Expr::If {
1407 condition,
1408 then_branch,
1409 else_branch,
1410 ..
1411 }) => {
1412 assert!(matches!(**condition, Expr::Binary { .. }));
1413 assert_eq!(then_branch.len(), 1);
1414 assert!(else_branch.is_some());
1415 }
1416 _ => panic!("Expected If expression"),
1417 }
1418 }
1419
1420 #[test]
1421 fn test_parse_for_loop() {
1422 let input = r#"
1423 For I In RANGE(0, 10) {
1424 PRINT(I)
1425 }
1426 "#;
1427 let mut parser = Parser::new(input);
1428 let program = parser.parse_program().unwrap();
1429
1430 eprintln!("Program length: {}", program.len());
1432 for (i, stmt) in program.iter().enumerate() {
1433 eprintln!("Statement {}: {:?}", i, stmt);
1434 }
1435
1436 assert_eq!(program.len(), 1);
1437 match &program[0] {
1438 Stmt::For {
1439 var,
1440 iterable,
1441 body,
1442 } => {
1443 assert_eq!(var, "I");
1444 assert!(matches!(iterable, Expr::Call { .. }));
1445 assert_eq!(body.len(), 1);
1446 }
1447 _ => panic!("Expected For statement"),
1448 }
1449 }
1450}