diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-12 18:49:07 +0200 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-12 18:49:07 +0200 |
commit | 0a7958a014eb746390c1e602b0e67c1a43f94a33 (patch) | |
tree | 5c19165dd3cd5715133a13a9912f607267df09be | |
parent | ed51d561ea8f4cd4c9187205d639cd891e893f25 (diff) |
[~] Added empty expression error messages
5 files changed, 5 insertions, 9 deletions
diff --git a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java index e68983b..91effbd 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java @@ -71,6 +71,8 @@ class ExpressionParser extends Parser { continue loop; case Token.PAR: if (lastToken != null && lastToken.type == Token.PAL) { // if equal to '(' it means it was created by a Factor + if(parsers.size() == 0) + throw new ParserException("Empty parenthesis were found."); lastToken = token; break loop; } else diff --git a/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java b/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java index fd54cf4..282a05e 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java @@ -83,6 +83,7 @@ class FactorParser extends Parser { @Override public double getValue() { + if (expression != null) if (isNegative) return expression.getValue() * (-1); diff --git a/calculator-java/src/main/java/ch/bfh/parser/Parser.java b/calculator-java/src/main/java/ch/bfh/parser/Parser.java index 6a03fe1..496dadf 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/Parser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/Parser.java @@ -11,7 +11,7 @@ abstract class Parser{ protected CalculatorLexer cl; protected Token lastToken; - protected double value; + protected Double value = null; protected abstract void parse(); public abstract double getValue(); diff --git a/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java b/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java index 7bd0d14..0897df7 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java @@ -66,7 +66,7 @@ public class StatementParser extends Parser{ @Override public double getValue() { if (parsedExpression == null) - return 0.0; + throw new ParserException("Empty statement."); return parsedExpression.getValue(); } }
\ No newline at end of file diff --git a/calculator-java/src/test/java/StatementParserTest.java b/calculator-java/src/test/java/StatementParserTest.java index ee4ab6b..ea47e4c 100644 --- a/calculator-java/src/test/java/StatementParserTest.java +++ b/calculator-java/src/test/java/StatementParserTest.java @@ -6,12 +6,6 @@ import static org.junit.Assert.assertEquals; public class StatementParserTest { StatementParser sp = new StatementParser(); - // Only testing correct expressions first - @Test - public void emptyExpression() { - sp.parseStatement(""); - assertEquals(0.0, sp.getValue(), 0.0); - } @Test public void oneFactor() { @@ -123,6 +117,5 @@ public class StatementParserTest { sp.parseStatement("let a = b+a"); assertEquals(255.0, sp.getValue(), 0.0); } - //TODO - test error detection } |