From 4e5e218d3e43d554c3e7cf6ed78c435006ab119b Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 13 Jan 2022 14:50:38 +0200 Subject: [PATCH] Fix #94. Introduce a new error type for illegal parenthese expressions such as `4(5)`. --- CHANGELOG.md | 4 ++++ src/error/display.rs | 6 ++++++ src/error/mod.rs | 5 +++++ src/token/mod.rs | 2 +- src/tree/mod.rs | 48 +++++++++++++++++++++++++++++++++++++++++--- tests/integration.rs | 28 ++++++++++++++++++++------ 6 files changed, 83 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dffdde0..ea439fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,12 @@ ### Changed + * Made the `EvalexprError` enum non_exhaustive. + ### Fixed + * Expressions that have dangling parenthese expressions such as `4(5)` now produce an error. + ### Deprecated ### Contributors diff --git a/src/error/display.rs b/src/error/display.rs index 3de998f..61e53b9 100644 --- a/src/error/display.rs +++ b/src/error/display.rs @@ -69,6 +69,12 @@ impl fmt::Display for EvalexprError { ), UnmatchedLBrace => write!(f, "Found an unmatched opening parenthesis '('."), UnmatchedRBrace => write!(f, "Found an unmatched closing parenthesis ')'."), + MissingOperatorOutsideOfBrace { .. } => write!( + f, + "Found an opening parenthesis that is preceded by something that does not take \ + any arguments on the right, or found a closing parenthesis that is succeeded by \ + something that does not take any arguments on the left." + ), UnmatchedPartialToken { first, second } => { if let Some(second) = second { write!( diff --git a/src/error/mod.rs b/src/error/mod.rs index 73f8d47..46cd116 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -15,6 +15,7 @@ mod display; /// Errors used in this crate. #[derive(Debug, PartialEq)] +#[non_exhaustive] pub enum EvalexprError { /// An operator was called with a wrong amount of arguments. WrongOperatorArgumentAmount { @@ -128,6 +129,10 @@ pub enum EvalexprError { /// A closing brace without a matching opening brace was found. UnmatchedRBrace, + /// Left of an opening brace or right of a closing brace is a token that does not expect the brace next to it. + /// For example, writing `4(5)` would yield this error, as the `4` does not have any operands. + MissingOperatorOutsideOfBrace, + /// A `PartialToken` is unmatched, such that it cannot be combined into a full `Token`. /// This happens if for example a single `=` is found, surrounded by whitespace. /// It is not a token, but it is part of the string representation of some tokens. diff --git a/src/token/mod.rs b/src/token/mod.rs index 2dd0f22..0cde4e4 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -372,7 +372,7 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult Some(Token::Identifier(literal.to_string())), } } diff --git a/src/tree/mod.rs b/src/tree/mod.rs index d173ad5..69f4494 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -406,6 +406,14 @@ impl Node { Some(self.children().len()) == self.operator().max_argument_amount() } + fn has_too_many_children(&self) -> bool { + if let Some(max_argument_amount) = self.operator().max_argument_amount() { + self.children().len() > max_argument_amount + } else { + false + } + } + fn insert_back_prioritized(&mut self, node: Node, is_root_node: bool) -> EvalexprResult<()> { // println!("Inserting {:?} into {:?}", node.operator, self.operator()); if self.operator().precedence() < node.operator().precedence() || is_root_node @@ -415,7 +423,7 @@ impl Node { if self.operator().is_leaf() { Err(EvalexprError::AppendedToLeafNode) } else if self.has_enough_children() { - // Unwrap cannot fail because of has_enough_children + // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child let last_child_operator = self.children.last().unwrap().operator(); if last_child_operator.precedence() @@ -425,7 +433,7 @@ impl Node { == node.operator().precedence() && !last_child_operator.is_left_to_right() && !node.operator().is_left_to_right()) { // println!("Recursing into {:?}", self.children.last().unwrap().operator()); - // Unwrap cannot fail because of has_enough_children + // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child self.children .last_mut() .unwrap() @@ -436,11 +444,35 @@ impl Node { return Err(EvalexprError::AppendedToLeafNode); } - // Unwrap cannot fail because of has_enough_children + // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child let last_child = self.children.pop().unwrap(); + // Root nodes have at most one child + // TODO I am not sure if this is the correct error + if self.operator() == &Operator::RootNode && !self.children().is_empty() { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } + // Do not insert root nodes into root nodes. + // TODO I am not sure if this is the correct error + if self.operator() == &Operator::RootNode + && node.operator() == &Operator::RootNode + { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } self.children.push(node); let node = self.children.last_mut().unwrap(); + // Root nodes have at most one child + // TODO I am not sure if this is the correct error + if node.operator() == &Operator::RootNode && !node.children().is_empty() { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } + // Do not insert root nodes into root nodes. + // TODO I am not sure if this is the correct error + if node.operator() == &Operator::RootNode + && last_child.operator() == &Operator::RootNode + { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } node.children.push(last_child); Ok(()) } @@ -492,6 +524,11 @@ fn collapse_all_sequences(root_stack: &mut Vec) -> EvalexprResult<()> { loop { // println!("Root is: {:?}", root); if root.operator() == &Operator::RootNode { + // This should fire if parsing something like `4(5)` + if root.has_too_many_children() { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } + root_stack.push(root); break; } @@ -501,6 +538,11 @@ fn collapse_all_sequences(root_stack: &mut Vec) -> EvalexprResult<()> { potential_higher_root.children.push(root); root = potential_higher_root; } else { + // This should fire if parsing something like `4(5)` + if root.has_too_many_children() { + return Err(EvalexprError::MissingOperatorOutsideOfBrace); + } + root_stack.push(potential_higher_root); root_stack.push(root); break; diff --git a/tests/integration.rs b/tests/integration.rs index d6697b2..5bf6c24 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1810,10 +1810,26 @@ fn test_value_type() { #[test] fn test_parenthese_combinations() { // These are from issue #94 - dbg!(build_operator_tree("123(1*2)").unwrap()); - assert!(dbg!(eval("123(1*2)")).is_err()); - assert!(dbg!(eval("1()")).is_err()); - assert!(dbg!(eval("1()()()()")).is_err()); - assert!(dbg!(eval("1()()()(9)()()")).is_err()); - assert!(dbg!(eval_with_context("a+100+(a*2)", &context_map! {"a" => 4}.unwrap())).is_err()); + assert_eq!( + eval("123(1*2)"), + Err(EvalexprError::MissingOperatorOutsideOfBrace) + ); + assert_eq!( + eval("1()"), + Err(EvalexprError::MissingOperatorOutsideOfBrace) + ); + assert_eq!( + eval("1()()()()"), + Err(EvalexprError::MissingOperatorOutsideOfBrace) + ); + assert_eq!( + eval("1()()()(9)()()"), + Err(EvalexprError::MissingOperatorOutsideOfBrace) + ); + assert_eq!( + eval_with_context("a+100(a*2)", &context_map! {"a" => 4}.unwrap()), + Err(EvalexprError::MissingOperatorOutsideOfBrace) + ); + assert_eq!(eval_int("(((1+2)*(3+4)+(5-(6)))/((7-8)))"), Ok(-20)); + assert_eq!(eval_int("(((((5)))))"), Ok(5)); }