diff --git a/kalk/src/parser.rs b/kalk/src/parser.rs index 6baef7e..a4e155d 100644 --- a/kalk/src/parser.rs +++ b/kalk/src/parser.rs @@ -448,7 +448,7 @@ fn parse_primary(context: &mut Context) -> Result { TokenKind::OpenParenthesis => parse_group(context)?, TokenKind::Pipe | TokenKind::OpenCeil | TokenKind::OpenFloor => parse_group_fn(context)?, TokenKind::Identifier => parse_identifier(context)?, - TokenKind::Literal => Expr::Literal(string_to_num(&advance(context).value)), + TokenKind::Literal => Expr::Literal(string_to_num(&advance(context).value)?), _ => return Err(CalcError::UnableToParseExpression), }; @@ -656,8 +656,12 @@ fn is_at_end(context: &Context) -> bool { context.pos >= context.tokens.len() || peek(context).kind == TokenKind::EOF } -fn string_to_num(value: &str) -> f64 { - value.replace(" ", "").parse::().unwrap() +fn string_to_num(value: &str) -> Result { + if let Ok(result) = value.replace(" ", "").parse::() { + Ok(result) + } else { + Err(CalcError::InvalidNumberLiteral(value.into())) + } } #[cfg(test)]