From fed70f3ac817ed4943a230cb901fcd65dc6fdd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Fri, 11 Jun 2021 20:55:12 +0200 Subject: [~] First totaly functional Calculator, reorganised the files, verified and added error messages --- .../main/java/ch/bfh/parser/ExpressionParser.java | 36 ++++++++++------------ 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java') 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 3ec0194..e68983b 100644 --- a/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java +++ b/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java @@ -1,22 +1,15 @@ package ch.bfh.parser; -import ch.bfh.CalculatorLexer; -import ch.bfh.Token; -import ch.bfh.exceptions.ParserException; - +import ch.bfh.lexer.CalculatorLexer; +import ch.bfh.lexer.Token; import java.util.ArrayList; -public class ExpressionParser extends Parser { +class ExpressionParser extends Parser { protected ArrayList parsers = new ArrayList<>(); protected ArrayList ops = new ArrayList<>(); private boolean isParenthesised = false; - public ExpressionParser(CalculatorLexer cl) { - this.cl = cl; - parse(); - } - protected ExpressionParser(CalculatorLexer cl, Token lastToken, boolean isParenthesised) { this.cl = cl; this.lastToken = lastToken; @@ -31,8 +24,8 @@ public class ExpressionParser extends Parser { token = lastToken; else token = cl.nextToken(); - Parser l = null; - Parser r = null; // left and right expressions + TermParser l = null; + TermParser r = null; // left and right Terms Token op = null; // operation of the current l and right expressions loop: @@ -48,9 +41,9 @@ public class ExpressionParser extends Parser { op = token; this.ops.add(op); }else if (l == null) - throw new ParserException("Factor cannot start with '+'."); + throw new ParserException("Redundant use of '+' is forbidden."); else - throw new ParserException("Invalid use of '+' was found in the Expression."); + throw new ParserException(token, "repetition of operator -> a term was expected."); break; case Token.SUB: if (l != null && op == null) { // if the '-' sign is between two term (op) @@ -82,14 +75,19 @@ public class ExpressionParser extends Parser { break loop; } else throw new ParserException("No matching opening parenthesis were found."); - default: - //TODO - Replace default by each individual case - throw new ParserException("Malformed Expression."); + case Token.MUL: + case Token.DIV: + throw new ParserException(token,"a term was expected."); + case Token.EQU: + case Token.LET: + throw new ParserException("The inputted token '"+token.str+"' can only be placed in a variable declaration context (let var = Expression)."); + case Token.END: + throw new ParserException("The keyword 'exit' can only be placed at the beginning of an expression."); } token = cl.nextToken(); } if (op != null && r == null) - throw new ParserException("Missing expression after last operation '"+op.str+"'"); + throw new ParserException("Missing term after the last operator '"+op.str+"'."); } @Override @@ -111,4 +109,4 @@ public class ExpressionParser extends Parser { } return result; } -} +} \ No newline at end of file -- cgit v1.2.3