Skip to content

Commit

Permalink
fix: the panic output information of "invalid number" is formatted us…
Browse files Browse the repository at this point in the history
…ing PanicInfo. (#51)

1. call the method "struct_token_error" in "parse_num_expr" to format the panic message.
  • Loading branch information
zong-zhe authored May 30, 2022
1 parent c3362b8 commit afd2c14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
17 changes: 14 additions & 3 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 14 additions & 13 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::panic::catch_unwind;
use std::panic::{catch_unwind, set_hook};

use crate::*;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit afd2c14

Please sign in to comment.