feat(argus): don't just panic on failed number parsing

This commit is contained in:
Anand Balakrishnan 2023-10-13 13:37:07 -07:00
parent 141f7d3983
commit a7431ce424
2 changed files with 17 additions and 3 deletions

View file

@ -98,15 +98,27 @@ pub fn lexer<'src>() -> impl Parser<'src, &'src str, Output<'src>, Error<'src>>
// .then(frac.or_not())
// .then(exp.or_not())
.to_slice()
.map(|s: &str| Token::Float(s.parse().unwrap()))
.try_map_with(|s: &str, e| {
s.parse()
.map(Token::Float)
.map_err(|err| Rich::custom(e.span(), format!("Unable to parse as 64-bit float: {}", err)))
})
.boxed();
let signed_int = one_of("+-")
// .or_not()
.then(digits)
.to_slice()
.map(|s: &str| Token::Int(s.parse().unwrap()));
let unsigned_int = digits.to_slice().map(|s: &str| Token::UInt(s.parse().unwrap()));
.try_map_with(|s: &str, e| {
s.parse()
.map(Token::Int)
.map_err(|err| Rich::custom(e.span(), format!("Unable to parse as 64-bit signed int: {}", err)))
});
let unsigned_int = digits.to_slice().try_map_with(|s: &str, e| {
s.parse()
.map(Token::UInt)
.map_err(|err| Rich::custom(e.span(), format!("Unable to parse as 64-bit unsigned int: {}", err)))
});
let number = choice((floating_number, signed_int, unsigned_int));

View file

@ -14,6 +14,8 @@ def test_correct_expr(data: st.DataObject) -> None:
try:
_ = argus.parse_expr(spec)
except ValueError as e:
if "Unable to parse as 64-bit" in str(e):
return
logging.critical(f"unable to parse expr: {spec}")
raise e
except BaseException as e: