aboutsummaryrefslogtreecommitdiffstats
path: root/main/exception
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2016-06-16 20:21:25 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2016-07-26 19:47:02 +0300
commitd35a37a449d31e6f4b43b937abf7b0e1fc497828 (patch)
tree8d65ac1068dbf09de480e7cae0fb39ec21d330ec /main/exception
parentf441022881945db08846e09a8f3bfee2f8cfa95d (diff)
Grammar now uses reverse polish notation
Diffstat (limited to 'main/exception')
-rw-r--r--main/exception/ExceptionManager.java10
-rw-r--r--main/exception/InterpreterException.java33
2 files changed, 24 insertions, 19 deletions
diff --git a/main/exception/ExceptionManager.java b/main/exception/ExceptionManager.java
index 0ceb25b..dbb59d3 100644
--- a/main/exception/ExceptionManager.java
+++ b/main/exception/ExceptionManager.java
@@ -16,13 +16,9 @@ public class ExceptionManager {
return instance;
}
- public static void raise(InterpreterException.Exception ex, int line, List<String> args) {
- InterpreterException e = new InterpreterException(ex);
- String output = e.what();
- for (String arg : args) {
- output = String.format(output, arg);
- }
- System.err.println(String.format("[%s] line: %d %s", ex.toString(), line, output));
+ public static void raise(InterpreterException.ExceptionType ex, int line, List<String> args) throws InterpreterException {
+ InterpreterException e = new InterpreterException(ex, line, args);
+ throw e;
}
diff --git a/main/exception/InterpreterException.java b/main/exception/InterpreterException.java
index 6ba5a7b..26e39fd 100644
--- a/main/exception/InterpreterException.java
+++ b/main/exception/InterpreterException.java
@@ -2,36 +2,45 @@ package com.jantuomi.interpreter.main.exception;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
/**
* Created by jan on 12.6.2016.
*/
-public class InterpreterException {
+public class InterpreterException extends Exception {
- public enum Exception {
+ public enum ExceptionType {
IllegalTokenError,
UnknownOperatorError,
SyntaxError,
TypeError
}
- public static Map<Exception, String> errorTexts = new HashMap<>();
+ public static Map<ExceptionType, String> errorTexts = new HashMap<>();
static {
- errorTexts.put(Exception.IllegalTokenError, "Illegal token %s found.");
- errorTexts.put(Exception.UnknownOperatorError, "Unexpected operator %s found.");
- errorTexts.put(Exception.SyntaxError, "Unexpected %s.");
- errorTexts.put(Exception.TypeError, "Incompatible types %s and %s.");
+ errorTexts.put(ExceptionType.IllegalTokenError, "Illegal token %s found.");
+ errorTexts.put(ExceptionType.UnknownOperatorError, "Unexpected operator %s found.");
+ errorTexts.put(ExceptionType.SyntaxError, "Unexpected %s.");
+ errorTexts.put(ExceptionType.TypeError, "Incompatible types %s and %s.");
}
- private Exception exception;
+ private ExceptionType exceptionType;
- public InterpreterException(Exception e) {
- exception = e;
+ public InterpreterException(ExceptionType exceptionType, int line, List<String> args) {
+ super(formatMessage(exceptionType.toString() + what(exceptionType), line, args));
+ this.exceptionType = exceptionType;
}
- public String what() {
- return errorTexts.get(exception);
+ private static String formatMessage(String message, int line, List<String> args) {
+ for (String arg : args) {
+ message = String.format(message, arg);
+ }
+ return String.format("line %d: ", line) + message;
+ }
+
+ public static String what(ExceptionType type) {
+ return errorTexts.get(type);
}
}