From 368557cb17433565b8ec5fe19be4029fd2117435 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 10 Nov 2024 14:56:21 +0800 Subject: [PATCH] refact: use `to_` instead of `as_` for function name. --- src/parser/grammar_parser.rs | 26 ++++++++++++++------------ src/tokenizer/lexical_token.rs | 4 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/parser/grammar_parser.rs b/src/parser/grammar_parser.rs index 40ac0dc..5fb0196 100644 --- a/src/parser/grammar_parser.rs +++ b/src/parser/grammar_parser.rs @@ -52,7 +52,7 @@ fn integer_node_parser( if let LexicalTokenType::ConstInteger(number) = t.span[0].token_type { SyntaxNode::const_integer(number) } else { - panic!("Illegal integer constant: {}", t.as_str()) + panic!("Illegal integer constant: {}", t.to_string()) } })(cursor) } @@ -64,7 +64,7 @@ fn float_node_parser( if let LexicalTokenType::ConstFloat(number) = t.span[0].token_type { SyntaxNode::const_float(number) } else { - panic!("Illegal float constant: {}", t.as_str()) + panic!("Illegal float constant: {}", t.to_string()) } })(cursor) } @@ -73,12 +73,12 @@ fn literal_string_node_parser( cursor: LexicalTokenSpan, ) -> IResult>> { map(lexical!(String), |t: LexicalTokenSpan| { - SyntaxNode::literal_string(t.as_str()) + SyntaxNode::literal_string(t.to_string()) })(cursor) } fn identifier_parser(cursor: LexicalTokenSpan) -> IResult { - map(lexical!(Identifier), |t: LexicalTokenSpan| t.as_str())(cursor) + map(lexical!(Identifier), |t: LexicalTokenSpan| t.to_string())(cursor) } type LeftValueParseType<'a> = ( @@ -144,6 +144,8 @@ fn primary_parser(curser: LexicalTokenSpan) -> IResult = (LexicalTokenSpan<'a>, Rc>); + fn unary_parser(cursor: LexicalTokenSpan) -> IResult>> { // unary_parser -> primary_parser | (+ | - | !) unary_parser alt(( @@ -157,8 +159,8 @@ fn unary_parser(cursor: LexicalTokenSpan) -> IResult UnaryNodeType::Plus, "-" => UnaryNodeType::Minus, @@ -196,7 +198,7 @@ fn multiply_parser(cursor: LexicalTokenSpan) -> IResult BinaryNodeType::Multiply, "/" => BinaryNodeType::Divide, @@ -226,7 +228,7 @@ fn add_parser(cursor: LexicalTokenSpan) -> IResult BinaryNodeType::Add, @@ -262,7 +264,7 @@ fn relation_parser(cursor: LexicalTokenSpan) -> IResult=" => BinaryNodeType::GreaterEqual, @@ -294,7 +296,7 @@ fn equal_parser(cursor: LexicalTokenSpan) -> IResult BinaryNodeType::Equal, @@ -321,7 +323,7 @@ fn and_parser(cursor: LexicalTokenSpan) -> IResult BinaryNodeType::And, @@ -347,7 +349,7 @@ fn or_parser(cursor: LexicalTokenSpan) -> IResult BinaryNodeType::Or, diff --git a/src/tokenizer/lexical_token.rs b/src/tokenizer/lexical_token.rs index a0606dc..5ca7ee8 100644 --- a/src/tokenizer/lexical_token.rs +++ b/src/tokenizer/lexical_token.rs @@ -11,7 +11,7 @@ impl<'a> LexicalToken<'a> { } /// 获得当前词法令牌的字面值 - pub fn as_str(&self) -> String { + pub fn to_string(&self) -> String { self.literal_value.to_owned() } } @@ -22,7 +22,7 @@ impl<'a> LexicalTokenSpan<'a> { } /// 获词法令牌切片表示的源代码 - pub fn as_str(&self) -> String { + pub fn to_string(&self) -> String { self.span.iter().map(|token| token.literal_value).collect() } }