Skip to content

Commit

Permalink
Update mod.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed May 31, 2024
1 parent 8f4d603 commit 759e0c4
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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<Program> {
tracing::trace!("[parse_program] current_token = {:?}", self.current_token);
let mut program = Program::new();
Expand All @@ -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<Statement> {
tracing::trace!("[parse_statement] current_token = {:?}", self.current_token);
match self.current_token.token_type() {
Expand All @@ -157,89 +160,75 @@ impl<'a> Parser<'a> {
/// 如何解析表达式后会返回来替换这里的代码。
///
/// # 解析let 语句
#[tracing::instrument(name = "parse_let_statement", skip(self), level = "debug")]
fn parse_let_statement(&mut self) -> anyhow::Result<LetStatement> {
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<ReturnStatement> {
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<ExpressionStatement> {
// un_trace(trace("parseExpressionStatement".into()));
tracing::trace!(
"[parse_expression_statement] current_token = {:?}",
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<Expression> {
tracing::trace!(
"[parse_expression] current_token = {:?}",
Expand Down Expand Up @@ -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<Expression> {
Ok(StringLiteral::new(
self.current_token.clone(),
Expand All @@ -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<Expression> {
Ok(Identifier::new(
self.current_token.clone(),
Expand All @@ -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<Expression> {
Ok(Boolean::new(
self.current_token.clone(),
Expand All @@ -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<Expression> {
// un_trace(trace("parseIntegerLiteral".into()));

Expand All @@ -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<Expression> {
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<Expression> {
let mut expression = Infix::new(
self.current_token.clone(),
Expand All @@ -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<Expression> {
self.next_token()?;

Expand All @@ -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<Expression> {
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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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<BlockStatement> {
let mut block = BlockStatement::new(self.current_token.clone());

Expand All @@ -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<Expression> {
let mut lit = FunctionLiteral::new(self.current_token.clone());

Expand All @@ -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<Vec<Identifier>> {
let mut identifiers = Vec::<Identifier>::new();

Expand Down Expand Up @@ -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<Expression> {
let mut exp = Call::new(self.current_token.clone(), function);

Expand All @@ -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<Expression> {
let mut exp = Index::new(self.current_token.clone(), left);

Expand All @@ -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<Expression> {
let mut array = ArrayLiteral::new(self.current_token.clone());

Expand All @@ -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<Vec<Expression>> {
let mut args: Vec<Expression> = vec![];

Expand Down Expand Up @@ -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<Expression> {
let mut hash = HashLiteral::new(self.current_token.clone());

Expand Down

0 comments on commit 759e0c4

Please sign in to comment.