Repository: kcl-lang/kcl (Rust)

Issue:
An unhandled syntax error was found in the parse_num_expr.
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

take str "8_________i" as the input parameter of method "kclvm-parser.parse_expr".
```
parse_expr("8_________i");
````

### 2. What did you expect to see? (Required)
kclvm output syntax error message.
### 3. What did you see instead (Required)
the rust panic message.
```
thread '...' panicked at '{"__kcl_PanicInfo__":true,"rust_file":"","rust_line":0,"rust_col":0,"kcl_pkgpath":"","kcl_file":"","kcl_line":1,"kcl_col":0,"kcl_arg_msg":"","kcl_config_meta_file":"","kcl_config_meta_line":0,"kcl_config_meta_col":0,"kcl_config_meta_arg_msg":"","message":"UnexpectedToken { expected: [\"int\"], got: \"8_________i\" }","err_type_code":5,"is_warning":false}', src/session/mod.rs:48:9
stack backtrace:
...
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
UnexpectedToken { expected: ["int"], got: "8_________i" }
```
### 4. What is your KusionStack components version? (Required)

kclvm version is 0.4.2; checksum: e07ed7af0d9bd1e86a3131714e4bd20c



Discussion:

Patch:
diff --git a/kclvm/parser/src/parser/expr.rs b/kclvm/parser/src/parser/expr.rs
index e14ca5756..d99de0f51 100644
--- a/kclvm/parser/src/parser/expr.rs
+++ b/kclvm/parser/src/parser/expr.rs
@@ -1988,15 +1988,26 @@ impl<'a> Parser<'a> {
 
         let (binary_suffix, value) = match lk.kind {
             token::LitKind::Integer => {
-                let value = bytes_to_int(lk.symbol.as_str().as_bytes(), 0).unwrap();
-
+                let result = bytes_to_int(lk.symbol.as_str().as_bytes(), 0);
+                let value = match result {
+                    Some(value) => value,
+                    None => {
+                        self.sess.struct_token_error(&[token::LitKind::Integer.into()], token);
+                    }
+                };
                 match lk.suffix {
                     Some(suffix) => (suffix.as_str().try_into().ok(), NumberLitValue::Int(value)),
                     None => (None, NumberLitValue::Int(value)),
                 }
             }
             token::LitKind::Float => {
-                let value = lk.symbol.as_str().parse().unwrap();
+                let result = lk.symbol.as_str().parse();
+                let value = match result {
+                    Ok(value) => value,
+                    _ => {
+                        self.sess.struct_token_error(&[token::LitKind::Float.into()], token);
+                    }
+                };
                 (None, NumberLitValue::Float(value))
             }
             _ => self.sess.struct_token_error(


Test patch:
diff --git a/kclvm/parser/src/tests.rs b/kclvm/parser/src/tests.rs
index e81550767..536497874 100644
--- a/kclvm/parser/src/tests.rs
+++ b/kclvm/parser/src/tests.rs
@@ -1,4 +1,4 @@
-use std::panic::catch_unwind;
+use std::panic::{catch_unwind, set_hook};
 
 use crate::*;
 
@@ -89,18 +89,19 @@ pub fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>){
     };
 }
 
-#[test]
-pub fn test_parse_expr_invalid_binary_expr() {
-    let result = catch_unwind(|| {
-        parse_expr("fs1_i1re1~s");
-    });
-    check_result_panic_info(result);
-}
+const PARSE_EXPR_INVALID_TEST_CASES: &[&'static str; 3] = &[
+    "fs1_i1re1~s",
+    "fh==-h==-",
+    "8_________i"
+];
 
 #[test]
-pub fn test_parse_expr_invalid_arr_out_of_bound_for_token_minus(){
-    let result = catch_unwind(|| {
-        parse_expr("fh==-h==-");
-    });
-    check_result_panic_info(result);
+pub fn test_parse_expr_invalid() {
+    for case in PARSE_EXPR_INVALID_TEST_CASES{
+        set_hook(Box::new(|_| {}));
+        let result = catch_unwind(|| {
+            parse_expr(&case);
+        });
+        check_result_panic_info(result);
+    }
 }
\ No newline at end of file