From 759e0c47c100cd5932529ed5ca2bcd19e61a1d67 Mon Sep 17 00:00:00 2001 From: Davirain Date: Fri, 31 May 2024 11:47:43 +0800 Subject: [PATCH] Update mod.rs --- src/parser/mod.rs | 54 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 71c8585..ab01895 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -103,6 +103,7 @@ impl<'a> Parser<'a> { } // TODO 因为使用 PrefixParseFn 和InferParseFn 的原因,其中的第一个参数是parser + #[tracing::instrument(name = "parse_program", skip(self, parse), level = "debug")] fn update_parser(&mut self, parse: Parser<'a>) { self.lexer = parse.lexer; self.current_token = parse.current_token; @@ -111,6 +112,7 @@ impl<'a> Parser<'a> { self.infix_parse_fns = parse.infix_parse_fns; } + #[tracing::instrument(name = "parse_program", skip(self), level = "debug")] fn next_token(&mut self) -> anyhow::Result<()> { self.current_token = self.peek_token.clone(); if self.current_position < self.lexer.len() { @@ -122,7 +124,7 @@ impl<'a> Parser<'a> { Ok(()) } - #[tracing::instrument(name = "parse_identifier", skip(self))] + #[tracing::instrument(name = "parse_identifier", skip(self), level = "debug")] pub fn parse_program(&mut self) -> anyhow::Result { tracing::trace!("[parse_program] current_token = {:?}", self.current_token); let mut program = Program::new(); @@ -137,6 +139,7 @@ impl<'a> Parser<'a> { Ok(program) } + #[tracing::instrument(name = "parse_statement", skip(self), level = "debug")] fn parse_statement(&mut self) -> anyhow::Result { tracing::trace!("[parse_statement] current_token = {:?}", self.current_token); match self.current_token.token_type() { @@ -157,67 +160,57 @@ impl<'a> Parser<'a> { /// 如何解析表达式后会返回来替换这里的代码。 /// /// # 解析let 语句 + #[tracing::instrument(name = "parse_let_statement", skip(self), level = "debug")] fn parse_let_statement(&mut self) -> anyhow::Result { tracing::trace!( "[parse_let_statement] current_token = {:?}", self.current_token ); let mut stmt = LetStatement::new(self.current_token.clone()); - tracing::trace!("[parse_let_statement] stmt = {stmt}"); - if self.expect_peek(TokenType::IDENT).is_err() { return Err(Error::CannotFindTokenType { ty: "IDENT".into() }.into()); } - *stmt.name_mut() = Identifier::new( self.current_token.clone(), self.current_token.literal().into(), ); tracing::trace!("[parse_let_statement] stmt = {stmt}"); - if self.expect_peek(TokenType::ASSIGN).is_err() { return Err(Error::CannotFindTokenType { ty: "ASSIGN".into(), } .into()); } - self.next_token()?; - *stmt.value_mut() = self.parse_expression(LOWEST)?; - while !self.cur_token_is(TokenType::SEMICOLON) { self.next_token()?; } - tracing::trace!("stmt = {stmt}"); - Ok(stmt) } /// 解析return 语句 + #[tracing::instrument(name = "parse_return_statement", skip(self), level = "debug")] fn parse_return_statement(&mut self) -> anyhow::Result { tracing::trace!( "[parse_return_statement] current_token = {:?}", self.current_token ); let mut stmt = ReturnStatement::new(self.current_token.clone()); - self.next_token()?; - // add equal expression *stmt.return_value_mut() = self.parse_expression(LOWEST)?.into(); - while !self.cur_token_is(TokenType::SEMICOLON) { self.next_token()?; } - Ok(stmt) } /// 解析表达式语句 /// 这是因为表达式语句不是真正的语句,而是仅由表达式构成的语句,相当于一层封装 + #[tracing::instrument(name = "parse_expression_statement", skip(self), level = "debug")] fn parse_expression_statement(&mut self) -> anyhow::Result { // un_trace(trace("parseExpressionStatement".into())); tracing::trace!( @@ -225,21 +218,17 @@ impl<'a> Parser<'a> { self.current_token ); let mut stmt = ExpressionStatement::new(self.current_token.clone()); - tracing::trace!("[parse_expression_statement] >> before ExpressionStatement = {stmt}"); - *stmt.expression_mut() = self.parse_expression(LOWEST)?; - if self.peek_token_is(TokenType::SEMICOLON) { self.next_token()?; } - tracing::trace!("[parse_expression_statement] >> after ExpressionStatement = {stmt}"); - Ok(stmt) } /// parse expression + #[tracing::instrument(name = "parse_expression", skip(self, precedence), level = "debug")] fn parse_expression(&mut self, precedence: OperatorPriority) -> anyhow::Result { tracing::trace!( "[parse_expression] current_token = {:?}", @@ -301,6 +290,7 @@ impl<'a> Parser<'a> { } /// parse string + #[tracing::instrument(name = "parse_string", skip(self), level = "debug")] fn parse_string(&mut self) -> anyhow::Result { Ok(StringLiteral::new( self.current_token.clone(), @@ -310,6 +300,7 @@ impl<'a> Parser<'a> { } /// parse identifier + #[tracing::instrument(name = "parse_identifier", skip(self), level = "debug")] fn parse_identifier(&mut self) -> anyhow::Result { Ok(Identifier::new( self.current_token.clone(), @@ -318,6 +309,7 @@ impl<'a> Parser<'a> { .into()) } + #[tracing::instrument(name = "parse_boolean", skip(self), level = "debug")] fn parse_boolean(&mut self) -> anyhow::Result { Ok(Boolean::new( self.current_token.clone(), @@ -327,6 +319,7 @@ impl<'a> Parser<'a> { } /// parse integer literal + #[tracing::instrument(name = "parse_integer_literal", skip(self), level = "debug")] fn parser_integer_literal(&mut self) -> anyhow::Result { // un_trace(trace("parseIntegerLiteral".into())); @@ -338,20 +331,19 @@ impl<'a> Parser<'a> { } /// parse prefix expression + #[tracing::instrument(name = "parse_prefix_expression", skip(self), level = "debug")] fn parse_prefix_expression(&mut self) -> anyhow::Result { let mut expression = Prefix::new( self.current_token.clone(), self.current_token.literal().into(), ); - self.next_token()?; - *expression.right_mut() = Box::new(self.parse_expression(PREFIX)?); - Ok(expression.into()) } /// parse infix expression + #[tracing::instrument(name = "parse_infix_expression", skip(self, left_exp), level = "debug")] fn parse_infix_expression(&mut self, left_exp: Expression) -> anyhow::Result { let mut expression = Infix::new( self.current_token.clone(), @@ -373,6 +365,7 @@ impl<'a> Parser<'a> { } /// parse ground expression + #[tracing::instrument(name = "parse_grouped_expression", skip(self), level = "debug")] fn parse_grouped_expression(&mut self) -> anyhow::Result { self.next_token()?; @@ -389,12 +382,13 @@ impl<'a> Parser<'a> { } /// parse if expression + #[tracing::instrument(name = "parse_if_expression", skip(self), level = "debug")] fn parse_if_expression(&mut self) -> anyhow::Result { let mut expression = If::new(self.current_token.clone()); if self.expect_peek(TokenType::LPAREN).is_err() { return Err(Error::CannotFindTokenType { - ty: "LPAREN".into(), + ty: TokenType::LPAREN.to_string(), } .into()); } @@ -405,14 +399,14 @@ impl<'a> Parser<'a> { if self.expect_peek(TokenType::RPAREN).is_err() { return Err(Error::CannotFindTokenType { - ty: "RPAREN".into(), + ty: TokenType::RPAREN.to_string(), } .into()); } if self.expect_peek(TokenType::LBRACE).is_err() { return Err(Error::CannotFindTokenType { - ty: "LBRACE".into(), + ty: TokenType::LBRACE.to_string(), } .into()); } @@ -424,7 +418,7 @@ impl<'a> Parser<'a> { if self.expect_peek(TokenType::LBRACE).is_err() { return Err(Error::CannotFindTokenType { - ty: "LBRACE".into(), + ty: TokenType::LBRACE.to_string(), } .into()); } @@ -436,6 +430,7 @@ impl<'a> Parser<'a> { } /// parse block statement + #[tracing::instrument(name = "parse_block_statement", skip(self), level = "debug")] fn parse_block_statement(&mut self) -> anyhow::Result { let mut block = BlockStatement::new(self.current_token.clone()); @@ -452,6 +447,7 @@ impl<'a> Parser<'a> { } /// parse function literals + #[tracing::instrument(name = "parse_function_literal", skip(self), level = "debug")] fn parse_function_literal(&mut self) -> anyhow::Result { let mut lit = FunctionLiteral::new(self.current_token.clone()); @@ -471,6 +467,7 @@ impl<'a> Parser<'a> { Ok(Expression::FunctionLiteral(lit)) } + #[tracing::instrument(name = "parse_function_parameters", skip(self), level = "debug")] fn parse_function_parameters(&mut self) -> anyhow::Result> { let mut identifiers = Vec::::new(); @@ -537,6 +534,7 @@ impl<'a> Parser<'a> { Ok(identifiers) } + #[tracing::instrument(name = "parser_call_expression", skip(self), level = "debug")] fn parser_call_expression(&mut self, function: Expression) -> anyhow::Result { let mut exp = Call::new(self.current_token.clone(), function); @@ -545,6 +543,7 @@ impl<'a> Parser<'a> { Ok(Expression::Call(exp)) } + #[tracing::instrument(name = "parse_index_expression", skip(self), level = "debug")] fn parse_index_expression(&mut self, left: Expression) -> anyhow::Result { let mut exp = Index::new(self.current_token.clone(), left); @@ -562,6 +561,7 @@ impl<'a> Parser<'a> { Ok(exp.into()) } + #[tracing::instrument(name = "parse_array_literal", skip(self), level = "debug")] fn parse_array_literal(&mut self) -> anyhow::Result { let mut array = ArrayLiteral::new(self.current_token.clone()); @@ -570,6 +570,7 @@ impl<'a> Parser<'a> { Ok(array.into()) } + #[tracing::instrument(name = "parse_expression_list", skip(self), level = "debug")] fn parse_expression_list(&mut self, end: TokenType) -> anyhow::Result> { let mut args: Vec = vec![]; @@ -597,6 +598,7 @@ impl<'a> Parser<'a> { Ok(args) } + #[tracing::instrument(name = "parse_hash_literal", skip(self), level = "debug")] fn parse_hash_literal(&mut self) -> anyhow::Result { let mut hash = HashLiteral::new(self.current_token.clone());