From e0bc36482c93c6a509b4c4c0655c59d114f51a8a Mon Sep 17 00:00:00 2001 From: bakk Date: Thu, 3 Jun 2021 11:16:36 +0200 Subject: [PATCH] Fixed crash when missing closing ceil, floor, | --- kalk/src/parser.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kalk/src/parser.rs b/kalk/src/parser.rs index 0ca3811..e25d4ec 100644 --- a/kalk/src/parser.rs +++ b/kalk/src/parser.rs @@ -97,6 +97,7 @@ impl Default for Context { /// Error that occured during parsing or evaluation. #[derive(Debug, Clone, PartialEq)] pub enum CalcError { + Expected(String), ExpectedDx, ExpectedIf, IncorrectAmountOfArguments(usize, String, usize), @@ -118,6 +119,7 @@ pub enum CalcError { impl ToString for CalcError { fn to_string(&self) -> String { match self { + CalcError::Expected(description) => format!("Expected: {}", description), CalcError::ExpectedDx => format!("Expected eg. dx, to specify for which variable the operation is being done to. Example with integration: ∫(0, 1, x dx) or ∫(0, 1, x, dx). You may need to put parenthesis around the expression before dx/dy/du/etc."), CalcError::ExpectedIf => format!("Expected 'if', with a condition after it."), CalcError::IncorrectAmountOfArguments(expected, func, got) => format!( @@ -569,6 +571,12 @@ fn parse_group_fn(context: &mut Context) -> Result { let expr = parse_expr(context)?; advance(context); + if peek(context).kind == TokenKind::EOF { + return Err(CalcError::Expected(String::from( + "Closing group symbol, eg. ⌋", + ))); + } + Ok(Expr::FnCall(Identifier::from_full_name(name), vec![expr])) } @@ -769,7 +777,11 @@ fn build_var(context: &Context, name: &str) -> Expr { } fn peek(context: &Context) -> &Token { - &context.tokens[context.pos] + if context.pos >= context.tokens.len() { + &context.tokens.last().unwrap() // EOF + } else { + &context.tokens[context.pos] + } } fn peek_next(context: &Context) -> &Token {