aboutsummaryrefslogtreecommitdiff
path: root/calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
diff options
context:
space:
mode:
authorMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 20:55:12 +0200
committerMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 20:55:12 +0200
commitfed70f3ac817ed4943a230cb901fcd65dc6fdd0c (patch)
treeaea4de81656c677d33e4af6eb11375303d1dfdda /calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java
parent3baa44f1c8459713a96f4db994ee9905cc4e8df1 (diff)
[~] First totaly functional Calculator, reorganised the files, verified and added error messages
Diffstat (limited to 'calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java')
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java36
1 files changed, 17 insertions, 19 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 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<Parser> parsers = new ArrayList<>();
protected ArrayList<Token> 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