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 _ => Err(ParseError::InvalidExpression {
960 message: "Unexpected token in expression".to_string(),
961 line: self.current_line,
962 column: self.current_column,
963 }),
964 }
965 }
966
967 fn parse_infix(&mut self, left: Expr) -> Result<Expr, ParseError> {
969 match &self.current_token {
970 Token::Plus
971 | Token::Minus
972 | Token::Multiply
973 | Token::Divide
974 | Token::Modulo
975 | Token::Equal
976 | Token::NotEqual
977 | Token::Less
978 | Token::LessEqual
979 | Token::Greater
980 | Token::GreaterEqual
981 | Token::And
982 | Token::Or => self.parse_binary_expression(left),
983 Token::LeftParen => self.parse_call_expression(left),
984 Token::LeftBracket => self.parse_index_expression(left),
985 _ => Ok(left),
986 }
987 }
988
989 fn parse_grouped_expression(&mut self) -> Result<Expr, ParseError> {
991 self.next_token(); let expr = self.parse_expression(Precedence::Lowest)?;
994
995 if self.current_token == Token::RightParen {
998 self.next_token(); Ok(expr)
1000 } else {
1001 Err(ParseError::UnexpectedToken {
1002 expected: "RightParen".to_string(),
1003 found: self.current_token.clone(),
1004 line: self.current_line,
1005 column: self.current_column,
1006 })
1007 }
1008 }
1009 fn parse_array_literal(&mut self) -> Result<Expr, ParseError> {
1011 self.next_token(); let mut elements = Vec::new();
1014
1015 self.skip_newlines();
1016
1017 while self.current_token != Token::RightBracket && self.current_token != Token::EOF {
1018 elements.push(self.parse_expression(Precedence::Lowest)?);
1019
1020 if self.current_token == Token::Comma {
1021 self.next_token();
1022 self.skip_newlines();
1023 } else {
1024 break;
1025 }
1026 }
1027
1028 self.expect_token(Token::RightBracket)?;
1029
1030 Ok(Expr::Array(elements))
1031 }
1032
1033 fn parse_dict_literal(&mut self) -> Result<Expr, ParseError> {
1035 self.next_token(); let mut pairs = Vec::new();
1038
1039 self.skip_newlines();
1040
1041 while self.current_token != Token::RightBrace && self.current_token != Token::EOF {
1042 let key = match &self.current_token {
1043 Token::Identifier(k) => k.clone(),
1044 Token::String(k) => k.clone(),
1045 _ => {
1046 return Err(ParseError::UnexpectedToken {
1047 expected: "identifier or string".to_string(),
1048 found: self.current_token.clone(),
1049 line: self.current_line,
1050 column: self.current_column,
1051 })
1052 }
1053 };
1054
1055 self.next_token();
1056 self.expect_token(Token::Colon)?;
1057
1058 let value = self.parse_expression(Precedence::Lowest)?;
1059
1060 pairs.push((key, value));
1061
1062 if self.current_token == Token::Comma {
1063 self.next_token();
1064 self.skip_newlines();
1065 } else {
1066 break;
1067 }
1068 }
1069
1070 self.expect_token(Token::RightBrace)?;
1071
1072 Ok(Expr::Dict(pairs))
1073 }
1074
1075 fn parse_unary_expression(&mut self, op: UnaryOp) -> Result<Expr, ParseError> {
1077 self.next_token(); let expr = self.parse_expression(Precedence::Prefix)?;
1080
1081 Ok(Expr::unary(op, expr))
1082 }
1083
1084 fn parse_binary_expression(&mut self, left: Expr) -> Result<Expr, ParseError> {
1086 let op = match &self.current_token {
1087 Token::Plus => BinOp::Add,
1088 Token::Minus => BinOp::Subtract,
1089 Token::Multiply => BinOp::Multiply,
1090 Token::Divide => BinOp::Divide,
1091 Token::Modulo => BinOp::Modulo,
1092 Token::Equal => BinOp::Equal,
1093 Token::NotEqual => BinOp::NotEqual,
1094 Token::Less => BinOp::Less,
1095 Token::LessEqual => BinOp::LessEqual,
1096 Token::Greater => BinOp::Greater,
1097 Token::GreaterEqual => BinOp::GreaterEqual,
1098 Token::And => BinOp::And,
1099 Token::Or => BinOp::Or,
1100 _ => {
1101 return Err(ParseError::InvalidExpression {
1102 message: "Invalid binary operator".to_string(),
1103 line: self.current_line,
1104 column: self.current_column,
1105 })
1106 }
1107 };
1108
1109 let precedence = self.current_precedence();
1110 self.next_token();
1111
1112 let right = self.parse_expression(precedence)?;
1113
1114 Ok(Expr::binary(left, op, right))
1115 }
1116
1117 fn parse_call_expression(&mut self, func: Expr) -> Result<Expr, ParseError> {
1119 self.next_token(); let mut args = Vec::new();
1122
1123 self.skip_newlines();
1124
1125 while self.current_token != Token::RightParen && self.current_token != Token::EOF {
1126 args.push(self.parse_expression(Precedence::Lowest)?);
1127
1128 if self.current_token == Token::Comma {
1129 self.next_token();
1130 self.skip_newlines();
1131 } else {
1132 break;
1133 }
1134 }
1135
1136 self.expect_token(Token::RightParen)?;
1137
1138 Ok(Expr::call(func, args))
1139 }
1140
1141 fn parse_index_expression(&mut self, object: Expr) -> Result<Expr, ParseError> {
1143 self.next_token(); let index = self.parse_expression(Precedence::Lowest)?;
1146
1147 self.expect_token(Token::RightBracket)?;
1148
1149 Ok(Expr::index(object, index))
1150 }
1151
1152 fn parse_if_expression(&mut self) -> Result<Expr, ParseError> {
1154 self.next_token(); self.expect_token(Token::LeftParen)?;
1156
1157 let condition = self.parse_expression(Precedence::Lowest)?;
1158
1159 self.expect_token(Token::RightParen)?;
1160 self.skip_newlines();
1161 self.expect_token(Token::LeftBrace)?;
1162
1163 let then_branch = self.parse_block()?;
1164
1165 self.expect_token(Token::RightBrace)?;
1166 self.skip_newlines();
1167
1168 let mut elif_branches = Vec::new();
1169 while self.current_token == Token::Elif {
1170 self.next_token();
1171 self.expect_token(Token::LeftParen)?;
1172
1173 let elif_cond = self.parse_expression(Precedence::Lowest)?;
1174
1175 self.expect_token(Token::RightParen)?;
1176 self.skip_newlines();
1177 self.expect_token(Token::LeftBrace)?;
1178
1179 let elif_body = self.parse_block()?;
1180
1181 self.expect_token(Token::RightBrace)?;
1182 self.skip_newlines();
1183
1184 elif_branches.push((elif_cond, elif_body));
1185 }
1186
1187 let else_branch = if self.current_token == Token::Else {
1188 self.next_token();
1189 self.skip_newlines();
1190 self.expect_token(Token::LeftBrace)?;
1191
1192 let else_body = self.parse_block()?;
1193
1194 self.expect_token(Token::RightBrace)?;
1195
1196 Some(else_body)
1197 } else {
1198 None
1199 };
1200
1201 Ok(Expr::If {
1202 condition: Box::new(condition),
1203 then_branch,
1204 elif_branches,
1205 else_branch,
1206 })
1207 }
1208}
1209
1210#[cfg(test)]
1211mod tests {
1212 use super::*;
1213
1214 #[test]
1215 fn test_parse_set_statement() {
1216 let input = "Set X 10";
1217 let mut parser = Parser::new(input);
1218 let program = parser.parse_program().unwrap();
1219
1220 assert_eq!(program.len(), 1);
1221 match &program[0] {
1222 Stmt::Set { name, value } => {
1223 assert_eq!(name, "X");
1224 assert_eq!(*value, Expr::Number(10.0));
1225 }
1226 _ => panic!("Expected Set statement"),
1227 }
1228 }
1229
1230 #[test]
1231 fn test_parse_arithmetic() {
1232 let input = "Set X (5 + 3 * 2)";
1233 let mut parser = Parser::new(input);
1234 let program = parser.parse_program().unwrap();
1235
1236 assert_eq!(program.len(), 1);
1237 match &program[0] {
1238 Stmt::Set { name, value } => {
1239 assert_eq!(name, "X");
1240 match value {
1242 Expr::Binary { left, op, right } => {
1243 assert_eq!(**left, Expr::Number(5.0));
1244 assert_eq!(*op, BinOp::Add);
1245 match &**right {
1246 Expr::Binary { left, op, right } => {
1247 assert_eq!(**left, Expr::Number(3.0));
1248 assert_eq!(*op, BinOp::Multiply);
1249 assert_eq!(**right, Expr::Number(2.0));
1250 }
1251 _ => panic!("Expected binary expression"),
1252 }
1253 }
1254 _ => panic!("Expected binary expression"),
1255 }
1256 }
1257 _ => panic!("Expected Set statement"),
1258 }
1259 }
1260
1261 #[test]
1262 fn test_parse_function_definition() {
1263 let input = r#"
1264 Func ADD (A, B) {
1265 Return (A + B)
1266 }
1267 "#;
1268 let mut parser = Parser::new(input);
1269 let program = parser.parse_program().unwrap();
1270
1271 assert_eq!(program.len(), 1);
1272 match &program[0] {
1273 Stmt::FuncDef { name, params, body } => {
1274 assert_eq!(name, "ADD");
1275 assert_eq!(params, &vec!["A".to_string(), "B".to_string()]);
1276 assert_eq!(body.len(), 1);
1277 }
1278 _ => panic!("Expected FuncDef"),
1279 }
1280 }
1281
1282 #[test]
1283 fn test_parse_function_call() {
1284 let input = "ADD(5, 3)";
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::Expression(Expr::Call { func, args }) => {
1291 assert_eq!(**func, Expr::Identifier("ADD".to_string()));
1292 assert_eq!(args.len(), 2);
1293 assert_eq!(args[0], Expr::Number(5.0));
1294 assert_eq!(args[1], Expr::Number(3.0));
1295 }
1296 _ => panic!("Expected function call"),
1297 }
1298 }
1299
1300 #[test]
1301 fn test_parse_array_literal() {
1302 let input = "Set ARR [1, 2, 3]";
1303 let mut parser = Parser::new(input);
1304 let program = parser.parse_program().unwrap();
1305
1306 assert_eq!(program.len(), 1);
1307 match &program[0] {
1308 Stmt::Set { name, value } => {
1309 assert_eq!(name, "ARR");
1310 match value {
1311 Expr::Array(elements) => {
1312 assert_eq!(elements.len(), 3);
1313 assert_eq!(elements[0], Expr::Number(1.0));
1314 assert_eq!(elements[1], Expr::Number(2.0));
1315 assert_eq!(elements[2], Expr::Number(3.0));
1316 }
1317 _ => panic!("Expected array"),
1318 }
1319 }
1320 _ => panic!("Expected Set statement"),
1321 }
1322 }
1323
1324 #[test]
1325 fn test_parse_if_expression() {
1326 let input = r#"
1327 If (X > 0) {
1328 Set Y 1
1329 } Else {
1330 Set Y 0
1331 }
1332 "#;
1333 let mut parser = Parser::new(input);
1334 let program = parser.parse_program().unwrap();
1335
1336 assert_eq!(program.len(), 1);
1337 match &program[0] {
1338 Stmt::Expression(Expr::If {
1339 condition,
1340 then_branch,
1341 else_branch,
1342 ..
1343 }) => {
1344 assert!(matches!(**condition, Expr::Binary { .. }));
1345 assert_eq!(then_branch.len(), 1);
1346 assert!(else_branch.is_some());
1347 }
1348 _ => panic!("Expected If expression"),
1349 }
1350 }
1351
1352 #[test]
1353 fn test_parse_for_loop() {
1354 let input = r#"
1355 For I In RANGE(0, 10) {
1356 PRINT(I)
1357 }
1358 "#;
1359 let mut parser = Parser::new(input);
1360 let program = parser.parse_program().unwrap();
1361
1362 eprintln!("Program length: {}", program.len());
1364 for (i, stmt) in program.iter().enumerate() {
1365 eprintln!("Statement {}: {:?}", i, stmt);
1366 }
1367
1368 assert_eq!(program.len(), 1);
1369 match &program[0] {
1370 Stmt::For {
1371 var,
1372 iterable,
1373 body,
1374 } => {
1375 assert_eq!(var, "I");
1376 assert!(matches!(iterable, Expr::Call { .. }));
1377 assert_eq!(body.len(), 1);
1378 }
1379 _ => panic!("Expected For statement"),
1380 }
1381 }
1382}