aboutsummaryrefslogtreecommitdiff
path: root/calculator-java/src/main/java/ch/bfh/parser
diff options
context:
space:
mode:
Diffstat (limited to 'calculator-java/src/main/java/ch/bfh/parser')
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/ExpressionParser.java36
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/FactorParser.java15
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/Parser.java7
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/ParserException.java8
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/StatementParser.java13
-rw-r--r--calculator-java/src/main/java/ch/bfh/parser/TermParser.java13
6 files changed, 49 insertions, 43 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
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 8bb5ef7..fd54cf4 100644
--- a/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java
+++ b/calculator-java/src/main/java/ch/bfh/parser/FactorParser.java
@@ -1,8 +1,7 @@
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;
class FactorParser extends Parser {
@@ -72,9 +71,11 @@ class FactorParser extends Parser {
lastToken = token;
else throw new ParserException("Missing expression before closing the parenthesis.");
break loop;
- default:
- //TODO - Replace default by each individual case
- throw new ParserException("Malformed Expression.");
+ 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();
}
@@ -91,4 +92,4 @@ class FactorParser extends Parser {
return value * (-1);
return value;
}
-}
+} \ No newline at end of file
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 333b942..6a03fe1 100644
--- a/calculator-java/src/main/java/ch/bfh/parser/Parser.java
+++ b/calculator-java/src/main/java/ch/bfh/parser/Parser.java
@@ -1,14 +1,13 @@
package ch.bfh.parser;
-import ch.bfh.CalculatorLexer;
-import ch.bfh.Token;
-
+import ch.bfh.lexer.CalculatorLexer;
+import ch.bfh.lexer.Token;
import java.util.HashMap;
import java.util.Map;
abstract class Parser{
- protected static Map<String, ExpressionParser> variables = new HashMap<>(); //'Persisted' Expressions when 'let' token was specified
+ protected static Map<String, ExpressionParser> variables = new HashMap<>(); //Persisted Expressions aka variables
protected CalculatorLexer cl;
protected Token lastToken;
diff --git a/calculator-java/src/main/java/ch/bfh/parser/ParserException.java b/calculator-java/src/main/java/ch/bfh/parser/ParserException.java
new file mode 100644
index 0000000..a85e3b2
--- /dev/null
+++ b/calculator-java/src/main/java/ch/bfh/parser/ParserException.java
@@ -0,0 +1,8 @@
+package ch.bfh.parser;
+
+import ch.bfh.lexer.Token;
+
+public class ParserException extends RuntimeException {
+ public ParserException(String s) { super(s); }
+ public ParserException(Token token, String s) { super("Last read token: '"+token.str+"', "+s); }
+} \ No newline at end of file
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 c85398b..482ef34 100644
--- a/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java
+++ b/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java
@@ -1,8 +1,7 @@
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;
public class StatementParser extends Parser{
@@ -27,15 +26,15 @@ public class StatementParser extends Parser{
switch (token.type) {
case Token.LET:
if (lastToken != null)
- throw new ParserException("The keyword 'let' cannot be placed anywhere else than at the beginning of an expression.");
+ throw new ParserException("The keyword 'let' can only be placed at the beginning of an expression.");
break;
case Token.EQU:
if (lastToken == null || lastToken.type != Token.LET || variableName == null)
- throw new ParserException("The inputted token '=' can only be placed in a variable declaration context (let variable = Expression).");
+ throw new ParserException("The token '=' can only be placed in a variable declaration context (let var = Expression).");
break;
case Token.END:
if (lastToken != null)
- throw new ParserException("The keyword 'exit' cannot be placed anywhere else than at the beginning of an expression.");
+ throw new ParserException("The keyword 'exit' can only be placed at the beginning of an expression.");
else
System.exit(0);
case Token.ID:
@@ -58,7 +57,7 @@ public class StatementParser extends Parser{
if (lastToken == null)
return;
if (lastToken.type == Token.LET || lastToken.type == Token.EQU)
- throw new ParserException("Incomplete variable declaration");
+ throw new ParserException("Incomplete variable declaration. Expected: let var = Expression.");
}
@Override
diff --git a/calculator-java/src/main/java/ch/bfh/parser/TermParser.java b/calculator-java/src/main/java/ch/bfh/parser/TermParser.java
index 9a97659..9e49fa8 100644
--- a/calculator-java/src/main/java/ch/bfh/parser/TermParser.java
+++ b/calculator-java/src/main/java/ch/bfh/parser/TermParser.java
@@ -1,8 +1,7 @@
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;
class TermParser extends ExpressionParser {
@@ -61,9 +60,11 @@ class TermParser extends ExpressionParser {
case Token.PAR: // Going as high as possible --> possibly the end of an expression created by a Factor :)
lastToken = token;
break loop;
- default:
- //TODO - Replace default by each individual case
- throw new ParserException("Malformed Expression.");
+ 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();
}