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().map_or(false, |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 loop {
866 match &self.current_token {
867 Token::Identifier(name) => {
868 self.validate_identifier_internal(name, true)?;
870 params.push(name.clone());
871 self.next_token();
872
873 if self.current_token == Token::Comma {
874 self.next_token();
875 } else {
876 break;
877 }
878 }
879 _ => break,
880 }
881 }
882
883 Ok(params)
884 }
885
886 fn parse_block(&mut self) -> Result<Vec<Stmt>, ParseError> {
888 let mut statements = Vec::new();
889
890 self.skip_newlines();
891
892 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
893 statements.push(self.parse_statement()?);
894 self.skip_newlines();
895 }
896
897 Ok(statements)
898 }
899
900 fn parse_expression(&mut self, precedence: Precedence) -> Result<Expr, ParseError> {
902 let mut left = self.parse_prefix()?;
903
904 while precedence < self.current_precedence()
906 && self.current_token != Token::Newline
907 && self.current_token != Token::Semicolon
908 && self.current_token != Token::EOF
909 && self.current_token != Token::RightParen
910 && self.current_token != Token::RightBracket
911 && self.current_token != Token::RightBrace
912 && self.current_token != Token::Comma
913 && self.current_token != Token::Colon
914 {
915 left = self.parse_infix(left)?;
916 }
917
918 Ok(left)
919 }
920
921 fn parse_prefix(&mut self) -> Result<Expr, ParseError> {
923 match &self.current_token.clone() {
924 Token::Number(n) => {
925 let num = *n;
926 self.next_token();
927 Ok(Expr::Number(num))
928 }
929 Token::BigInteger(s) => {
930 let big_int_str = s.clone();
931 self.next_token();
932 Ok(Expr::BigInteger(big_int_str))
933 }
934 Token::String(s) => {
935 let string = s.clone();
936 self.next_token();
937 Ok(Expr::String(string))
938 }
939 Token::Boolean(b) => {
940 let bool_val = *b;
941 self.next_token();
942 Ok(Expr::Boolean(bool_val))
943 }
944 Token::Null => {
945 self.next_token();
946 Ok(Expr::Null)
947 }
948 Token::Identifier(name) => {
949 let ident = name.clone();
950 self.next_token();
951 Ok(Expr::Identifier(ident))
952 }
953 Token::LeftParen => self.parse_grouped_expression(),
954 Token::LeftBracket => self.parse_array_literal(),
955 Token::LeftBrace => self.parse_dict_literal(),
956 Token::Minus => self.parse_unary_expression(UnaryOp::Minus),
957 Token::Not => self.parse_unary_expression(UnaryOp::Not),
958 Token::If => self.parse_if_expression(),
959 Token::Func => self.parse_lambda_expression(),
960 Token::Lambda => self.parse_lambda_arrow_expression(),
961 _ => Err(ParseError::InvalidExpression {
962 message: "Unexpected token in expression".to_string(),
963 line: self.current_line,
964 column: self.current_column,
965 }),
966 }
967 }
968
969 fn parse_infix(&mut self, left: Expr) -> Result<Expr, ParseError> {
971 match &self.current_token {
972 Token::Plus
973 | Token::Minus
974 | Token::Multiply
975 | Token::Divide
976 | Token::Modulo
977 | Token::Equal
978 | Token::NotEqual
979 | Token::Less
980 | Token::LessEqual
981 | Token::Greater
982 | Token::GreaterEqual
983 | Token::And
984 | Token::Or => self.parse_binary_expression(left),
985 Token::LeftParen => self.parse_call_expression(left),
986 Token::LeftBracket => self.parse_index_expression(left),
987 _ => Ok(left),
988 }
989 }
990
991 fn parse_grouped_expression(&mut self) -> Result<Expr, ParseError> {
993 self.next_token(); let expr = self.parse_expression(Precedence::Lowest)?;
996
997 if self.current_token == Token::RightParen {
1000 self.next_token(); Ok(expr)
1002 } else {
1003 Err(ParseError::UnexpectedToken {
1004 expected: "RightParen".to_string(),
1005 found: self.current_token.clone(),
1006 line: self.current_line,
1007 column: self.current_column,
1008 })
1009 }
1010 }
1011 fn parse_array_literal(&mut self) -> Result<Expr, ParseError> {
1013 self.next_token(); let mut elements = Vec::new();
1016
1017 self.skip_newlines();
1018
1019 while self.current_token != Token::RightBracket && self.current_token != Token::EOF {
1020 elements.push(self.parse_expression(Precedence::Lowest)?);
1021
1022 self.skip_newlines();
1023
1024 if self.current_token == Token::Comma {
1025 self.next_token();
1026 self.skip_newlines();
1027 } else if self.current_token == Token::RightBracket {
1028 break;
1029 }
1030 }
1031
1032 self.expect_token(Token::RightBracket)?;
1033
1034 Ok(Expr::Array(elements))
1035 }
1036
1037 fn parse_dict_literal(&mut self) -> Result<Expr, ParseError> {
1039 self.next_token(); let mut pairs = Vec::new();
1042
1043 self.skip_newlines();
1044
1045 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
1046 let key = match &self.current_token {
1047 Token::Identifier(k) => k.clone(),
1048 Token::String(k) => k.clone(),
1049 _ => {
1050 return Err(ParseError::UnexpectedToken {
1051 expected: "identifier or string".to_string(),
1052 found: self.current_token.clone(),
1053 line: self.current_line,
1054 column: self.current_column,
1055 });
1056 }
1057 };
1058
1059 self.next_token();
1060 self.expect_token(Token::Colon)?;
1061
1062 let value = self.parse_expression(Precedence::Lowest)?;
1063
1064 pairs.push((key, value));
1065
1066 self.skip_newlines();
1067
1068 if self.current_token == Token::Comma {
1069 self.next_token();
1070 self.skip_newlines();
1071 } else if self.current_token == Token::RightBrace {
1072 break;
1073 }
1074 }
1075
1076 self.expect_token(Token::RightBrace)?;
1077
1078 Ok(Expr::Dict(pairs))
1079 }
1080
1081 fn parse_unary_expression(&mut self, op: UnaryOp) -> Result<Expr, ParseError> {
1083 self.next_token(); let expr = self.parse_expression(Precedence::Prefix)?;
1086
1087 Ok(Expr::unary(op, expr))
1088 }
1089
1090 fn parse_binary_expression(&mut self, left: Expr) -> Result<Expr, ParseError> {
1092 let op = match &self.current_token {
1093 Token::Plus => BinOp::Add,
1094 Token::Minus => BinOp::Subtract,
1095 Token::Multiply => BinOp::Multiply,
1096 Token::Divide => BinOp::Divide,
1097 Token::Modulo => BinOp::Modulo,
1098 Token::Equal => BinOp::Equal,
1099 Token::NotEqual => BinOp::NotEqual,
1100 Token::Less => BinOp::Less,
1101 Token::LessEqual => BinOp::LessEqual,
1102 Token::Greater => BinOp::Greater,
1103 Token::GreaterEqual => BinOp::GreaterEqual,
1104 Token::And => BinOp::And,
1105 Token::Or => BinOp::Or,
1106 _ => {
1107 return Err(ParseError::InvalidExpression {
1108 message: "Invalid binary operator".to_string(),
1109 line: self.current_line,
1110 column: self.current_column,
1111 });
1112 }
1113 };
1114
1115 let precedence = self.current_precedence();
1116 self.next_token();
1117
1118 let right = self.parse_expression(precedence)?;
1119
1120 Ok(Expr::binary(left, op, right))
1121 }
1122
1123 fn parse_call_expression(&mut self, func: Expr) -> Result<Expr, ParseError> {
1125 self.next_token(); let mut args = Vec::new();
1128
1129 self.skip_newlines();
1130
1131 while self.current_token != Token::RightParen && self.current_token != Token::EOF {
1132 args.push(self.parse_expression(Precedence::Lowest)?);
1133
1134 if self.current_token == Token::Comma {
1135 self.next_token();
1136 self.skip_newlines();
1137 } else {
1138 break;
1139 }
1140 }
1141
1142 self.expect_token(Token::RightParen)?;
1143
1144 Ok(Expr::call(func, args))
1145 }
1146
1147 fn parse_index_expression(&mut self, object: Expr) -> Result<Expr, ParseError> {
1149 self.next_token(); let index = self.parse_expression(Precedence::Lowest)?;
1152
1153 self.expect_token(Token::RightBracket)?;
1154
1155 Ok(Expr::index(object, index))
1156 }
1157
1158 fn parse_if_expression(&mut self) -> Result<Expr, ParseError> {
1160 self.next_token(); self.expect_token(Token::LeftParen)?;
1162
1163 let condition = self.parse_expression(Precedence::Lowest)?;
1164
1165 self.expect_token(Token::RightParen)?;
1166 self.skip_newlines();
1167 self.expect_token(Token::LeftBrace)?;
1168
1169 let then_branch = self.parse_block()?;
1170
1171 self.expect_token(Token::RightBrace)?;
1172 self.skip_newlines();
1173
1174 let mut elif_branches = Vec::new();
1175 while self.current_token == Token::Elif {
1176 self.next_token();
1177 self.expect_token(Token::LeftParen)?;
1178
1179 let elif_cond = self.parse_expression(Precedence::Lowest)?;
1180
1181 self.expect_token(Token::RightParen)?;
1182 self.skip_newlines();
1183 self.expect_token(Token::LeftBrace)?;
1184
1185 let elif_body = self.parse_block()?;
1186
1187 self.expect_token(Token::RightBrace)?;
1188 self.skip_newlines();
1189
1190 elif_branches.push((elif_cond, elif_body));
1191 }
1192
1193 let else_branch = if self.current_token == Token::Else {
1194 self.next_token();
1195 self.skip_newlines();
1196 self.expect_token(Token::LeftBrace)?;
1197
1198 let else_body = self.parse_block()?;
1199
1200 self.expect_token(Token::RightBrace)?;
1201
1202 Some(else_body)
1203 } else {
1204 None
1205 };
1206
1207 Ok(Expr::If {
1208 condition: Box::new(condition),
1209 then_branch,
1210 elif_branches,
1211 else_branch,
1212 })
1213 }
1214
1215 fn parse_lambda_expression(&mut self) -> Result<Expr, ParseError> {
1217 self.next_token(); self.expect_token(Token::LeftParen)?;
1219
1220 let params = self.parse_parameter_list()?;
1221
1222 self.expect_token(Token::RightParen)?;
1223 self.skip_newlines();
1224 self.expect_token(Token::LeftBrace)?;
1225
1226 let body = self.parse_block()?;
1227
1228 self.expect_token(Token::RightBrace)?;
1229
1230 Ok(Expr::Lambda { params, body })
1231 }
1232
1233 fn parse_lambda_arrow_expression(&mut self) -> Result<Expr, ParseError> {
1235 self.next_token(); let params = if self.current_token == Token::LeftParen {
1238 self.next_token(); let params = self.parse_parameter_list()?;
1241 self.expect_token(Token::RightParen)?;
1242 params
1243 } else {
1244 match &self.current_token {
1246 Token::Identifier(name) => {
1247 self.validate_identifier_internal(name, true)?;
1248 let param = name.clone();
1249 self.next_token();
1250 vec![param]
1251 }
1252 _ => {
1253 return Err(ParseError::UnexpectedToken {
1254 expected: "identifier or '('".to_string(),
1255 found: self.current_token.clone(),
1256 line: self.current_line,
1257 column: self.current_column,
1258 });
1259 }
1260 }
1261 };
1262
1263 self.expect_token(Token::Arrow)?;
1265
1266 let expr = self.parse_expression(Precedence::Lowest)?;
1268
1269 let body = vec![Stmt::Return(expr)];
1271
1272 Ok(Expr::Lambda { params, body })
1273 }
1274}
1275
1276#[cfg(test)]
1277mod tests {
1278 use super::*;
1279
1280 #[test]
1281 fn test_parse_set_statement() {
1282 let input = "Set X 10";
1283 let mut parser = Parser::new(input);
1284 let program = parser.parse_program().unwrap();
1285
1286 assert_eq!(program.len(), 1);
1287 match &program[0] {
1288 Stmt::Set { name, value } => {
1289 assert_eq!(name, "X");
1290 assert_eq!(*value, Expr::Number(10.0));
1291 }
1292 _ => panic!("Expected Set statement"),
1293 }
1294 }
1295
1296 #[test]
1297 fn test_parse_arithmetic() {
1298 let input = "Set X (5 + 3 * 2)";
1299 let mut parser = Parser::new(input);
1300 let program = parser.parse_program().unwrap();
1301
1302 assert_eq!(program.len(), 1);
1303 match &program[0] {
1304 Stmt::Set { name, value } => {
1305 assert_eq!(name, "X");
1306 match value {
1308 Expr::Binary { left, op, right } => {
1309 assert_eq!(**left, Expr::Number(5.0));
1310 assert_eq!(*op, BinOp::Add);
1311 match &**right {
1312 Expr::Binary { left, op, right } => {
1313 assert_eq!(**left, Expr::Number(3.0));
1314 assert_eq!(*op, BinOp::Multiply);
1315 assert_eq!(**right, Expr::Number(2.0));
1316 }
1317 _ => panic!("Expected binary expression"),
1318 }
1319 }
1320 _ => panic!("Expected binary expression"),
1321 }
1322 }
1323 _ => panic!("Expected Set statement"),
1324 }
1325 }
1326
1327 #[test]
1328 fn test_parse_function_definition() {
1329 let input = r#"
1330 Func ADD (A, B) {
1331 Return (A + B)
1332 }
1333 "#;
1334 let mut parser = Parser::new(input);
1335 let program = parser.parse_program().unwrap();
1336
1337 assert_eq!(program.len(), 1);
1338 match &program[0] {
1339 Stmt::FuncDef { name, params, body } => {
1340 assert_eq!(name, "ADD");
1341 assert_eq!(params, &vec!["A".to_string(), "B".to_string()]);
1342 assert_eq!(body.len(), 1);
1343 }
1344 _ => panic!("Expected FuncDef"),
1345 }
1346 }
1347
1348 #[test]
1349 fn test_parse_function_call() {
1350 let input = "ADD(5, 3)";
1351 let mut parser = Parser::new(input);
1352 let program = parser.parse_program().unwrap();
1353
1354 assert_eq!(program.len(), 1);
1355 match &program[0] {
1356 Stmt::Expression(Expr::Call { func, args }) => {
1357 assert_eq!(**func, Expr::Identifier("ADD".to_string()));
1358 assert_eq!(args.len(), 2);
1359 assert_eq!(args[0], Expr::Number(5.0));
1360 assert_eq!(args[1], Expr::Number(3.0));
1361 }
1362 _ => panic!("Expected function call"),
1363 }
1364 }
1365
1366 #[test]
1367 fn test_parse_array_literal() {
1368 let input = "Set ARR [1, 2, 3]";
1369 let mut parser = Parser::new(input);
1370 let program = parser.parse_program().unwrap();
1371
1372 assert_eq!(program.len(), 1);
1373 match &program[0] {
1374 Stmt::Set { name, value } => {
1375 assert_eq!(name, "ARR");
1376 match value {
1377 Expr::Array(elements) => {
1378 assert_eq!(elements.len(), 3);
1379 assert_eq!(elements[0], Expr::Number(1.0));
1380 assert_eq!(elements[1], Expr::Number(2.0));
1381 assert_eq!(elements[2], Expr::Number(3.0));
1382 }
1383 _ => panic!("Expected array"),
1384 }
1385 }
1386 _ => panic!("Expected Set statement"),
1387 }
1388 }
1389
1390 #[test]
1391 fn test_parse_if_expression() {
1392 let input = r#"
1393 If (X > 0) {
1394 Set Y 1
1395 } Else {
1396 Set Y 0
1397 }
1398 "#;
1399 let mut parser = Parser::new(input);
1400 let program = parser.parse_program().unwrap();
1401
1402 assert_eq!(program.len(), 1);
1403 match &program[0] {
1404 Stmt::Expression(Expr::If {
1405 condition,
1406 then_branch,
1407 else_branch,
1408 ..
1409 }) => {
1410 assert!(matches!(**condition, Expr::Binary { .. }));
1411 assert_eq!(then_branch.len(), 1);
1412 assert!(else_branch.is_some());
1413 }
1414 _ => panic!("Expected If expression"),
1415 }
1416 }
1417
1418 #[test]
1419 fn test_parse_for_loop() {
1420 let input = r#"
1421 For I In RANGE(0, 10) {
1422 PRINT(I)
1423 }
1424 "#;
1425 let mut parser = Parser::new(input);
1426 let program = parser.parse_program().unwrap();
1427
1428 eprintln!("Program length: {}", program.len());
1430 for (i, stmt) in program.iter().enumerate() {
1431 eprintln!("Statement {}: {:?}", i, stmt);
1432 }
1433
1434 assert_eq!(program.len(), 1);
1435 match &program[0] {
1436 Stmt::For {
1437 var,
1438 iterable,
1439 body,
1440 } => {
1441 assert_eq!(var, "I");
1442 assert!(matches!(iterable, Expr::Call { .. }));
1443 assert_eq!(body.len(), 1);
1444 }
1445 _ => panic!("Expected For statement"),
1446 }
1447 }
1448}