aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/com/jantuomi/tunkki/exception
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-08-02 23:07:23 +0300
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-08-02 23:07:23 +0300
commit2132d8d8ead5619974976ca430d91ee760e0551b (patch)
treee81eefc3afaea7395409687faabc8d8d0186f922 /src/main/com/jantuomi/tunkki/exception
parent5ac4b67b7eba39bbb098b9b184dcacffe99baa2a (diff)
Refactor TunkkiError to subclasses
Make TunkkiError abstract and create a subclass for all error types Change all TunkkiError throws to throw subclasses instead
Diffstat (limited to 'src/main/com/jantuomi/tunkki/exception')
-rw-r--r--src/main/com/jantuomi/tunkki/exception/ExceptionManager.java7
-rw-r--r--src/main/com/jantuomi/tunkki/exception/TunkkiError.java81
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/CastTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/DivisionByZeroTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/ExpectedDifferentTokenTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/FunctionArgumentTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/GeneralTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/IllegalTokenTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/IncludeTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/NadaTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/SyntaxTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/TunkkiError.java72
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/TypeTunkkiError.java20
-rw-r--r--src/main/com/jantuomi/tunkki/exception/types/UndeclaredSymbolTunkkiError.java20
14 files changed, 294 insertions, 86 deletions
diff --git a/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java b/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java
index 9cfdd2e..b26c3b0 100644
--- a/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java
+++ b/src/main/com/jantuomi/tunkki/exception/ExceptionManager.java
@@ -1,5 +1,7 @@
package com.jantuomi.tunkki.exception;
+import com.jantuomi.tunkki.exception.types.TunkkiError;
+
/**
* Created by jan on 10.6.2016.
*/
@@ -13,9 +15,4 @@ public class ExceptionManager {
public static ExceptionManager getInstance() {
return instance;
}
-
- public static void raise(TunkkiError.ExceptionType ex, int line, String... args) throws TunkkiError {
- TunkkiError e = new TunkkiError(ex, line, args);
- throw e;
- }
}
diff --git a/src/main/com/jantuomi/tunkki/exception/TunkkiError.java b/src/main/com/jantuomi/tunkki/exception/TunkkiError.java
deleted file mode 100644
index b5a36a7..0000000
--- a/src/main/com/jantuomi/tunkki/exception/TunkkiError.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package com.jantuomi.tunkki.exception;
-
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Created by jan on 12.6.2016.
- */
-public class TunkkiError extends Exception {
-
- public enum ExceptionType {
- IllegalTokenError,
- SyntaxError,
- TypeError,
- FunctionArgumentError,
- ExpectedDifferentTokenError,
- UndeclaredSymbolError,
- IncludeError,
- GeneralError,
- CastError,
- DivisionByZeroError,
- NadaError
- }
-
- private static Map<ExceptionType, String> errorTexts = new HashMap<>();
-
- static {
- errorTexts.put(ExceptionType.IllegalTokenError, "Unknown or illegal token %s found.");
- errorTexts.put(ExceptionType.SyntaxError, "Unexpected %s.");
- errorTexts.put(ExceptionType.TypeError, "Incompatible types %s and %s.");
- errorTexts.put(ExceptionType.ExpectedDifferentTokenError, "Token '%s' expects a different token stack.");
- errorTexts.put(ExceptionType.FunctionArgumentError, "The parameter list given to function %s is either of wrong length or the parameters are of wrong type. Actual: %s");
- errorTexts.put(ExceptionType.UndeclaredSymbolError, "No symbol %s defined in the current scope.");
- errorTexts.put(ExceptionType.IncludeError, "File or module %s could not be included.");
- errorTexts.put(ExceptionType.GeneralError, "%s");
- errorTexts.put(ExceptionType.NadaError, "Can't do operation with a nada value.");
- errorTexts.put(ExceptionType.CastError, "Illegal cast to type %s from value %s.");
- errorTexts.put(ExceptionType.DivisionByZeroError, "Division by zero.");
- }
-
- private ExceptionType exceptionType;
- private String completeMessage;
- private List<String> arguments;
-
- public TunkkiError(ExceptionType exceptionType, int line, String... args) {
- super(exceptionType.toString(), null, false, false);
- this.exceptionType = exceptionType;
- this.arguments = Arrays.asList(args);
- this.completeMessage = formatMessage(exceptionType.toString() + ": " + what(exceptionType), line, args);
- }
-
- private static String formatMessage(String message, int line, String... parameters) {
-
- message = String.format(message, parameters);
- if (line == -1) {
- return "\n" + message;
- } else {
- return String.format("\nLine #%d: ", line) + message;
- }
- }
-
- public static String what(ExceptionType type) {
- return errorTexts.get(type);
- }
-
- @Override
- public void printStackTrace() {
- System.err.println(completeMessage);
- }
-
- public ExceptionType getType() {
- return exceptionType;
- }
-
- public List<String> getArguments() {
- return this.arguments;
- }
-}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/CastTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/CastTunkkiError.java
new file mode 100644
index 0000000..f5312d8
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/CastTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class CastTunkkiError extends TunkkiError {
+ public CastTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Illegal cast to type %s from value %s.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.CastError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/DivisionByZeroTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/DivisionByZeroTunkkiError.java
new file mode 100644
index 0000000..0fb0767
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/DivisionByZeroTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class DivisionByZeroTunkkiError extends TunkkiError {
+ public DivisionByZeroTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Division by zero.";
+ }
+
+ @Override
+ public ExceptionType getType() {
+ return ExceptionType.DivisionByZeroError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/ExpectedDifferentTokenTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/ExpectedDifferentTokenTunkkiError.java
new file mode 100644
index 0000000..c56a011
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/ExpectedDifferentTokenTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class ExpectedDifferentTokenTunkkiError extends TunkkiError {
+ public ExpectedDifferentTokenTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Token '%s' expects a different token stack.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.ExpectedDifferentTokenError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/FunctionArgumentTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/FunctionArgumentTunkkiError.java
new file mode 100644
index 0000000..1b61c86
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/FunctionArgumentTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class FunctionArgumentTunkkiError extends TunkkiError {
+ public FunctionArgumentTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "The parameter list given to function %s is either of wrong length or the parameters are of wrong type. Actual: %s";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.FunctionArgumentError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/GeneralTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/GeneralTunkkiError.java
new file mode 100644
index 0000000..b49c404
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/GeneralTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class GeneralTunkkiError extends TunkkiError {
+ public GeneralTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "%s";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.GeneralError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/IllegalTokenTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/IllegalTokenTunkkiError.java
new file mode 100644
index 0000000..dc19911
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/IllegalTokenTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class IllegalTokenTunkkiError extends TunkkiError {
+ public IllegalTokenTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Unknown or illegal token %s found.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.IllegalTokenError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/IncludeTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/IncludeTunkkiError.java
new file mode 100644
index 0000000..db95110
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/IncludeTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class IncludeTunkkiError extends TunkkiError {
+ public IncludeTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "File or module %s could not be included.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.IncludeError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/NadaTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/NadaTunkkiError.java
new file mode 100644
index 0000000..2edc13b
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/NadaTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class NadaTunkkiError extends TunkkiError {
+ public NadaTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Can't do operation with a nada value.";
+ }
+
+ @Override
+ public ExceptionType getType() {
+ return ExceptionType.NadaError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/SyntaxTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/SyntaxTunkkiError.java
new file mode 100644
index 0000000..08d230b
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/SyntaxTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class SyntaxTunkkiError extends TunkkiError {
+ public SyntaxTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Unexpected %s.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.SyntaxError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/TunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/TunkkiError.java
new file mode 100644
index 0000000..0124d37
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/TunkkiError.java
@@ -0,0 +1,72 @@
+package com.jantuomi.tunkki.exception.types;
+
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by jan on 12.6.2016.
+ */
+abstract public class TunkkiError extends Exception {
+
+ public enum ExceptionType {
+ IllegalTokenError,
+ SyntaxError,
+ TypeError,
+ FunctionArgumentError,
+ ExpectedDifferentTokenError,
+ UndeclaredSymbolError,
+ IncludeError,
+ GeneralError,
+ CastError,
+ DivisionByZeroError,
+ NadaError
+ }
+
+ private static Map<ExceptionType, String> errorTexts = new HashMap<>();
+
+
+ private List<String> arguments;
+ private int line;
+ private String[] args;
+
+ public TunkkiError(int line, String... args) {
+ super();
+ this.arguments = Arrays.asList(args);
+ this.line = line;
+ this.args = args;
+ }
+
+ private static String formatMessage(String message, int line, String... parameters) {
+
+ message = String.format(message, parameters);
+ if (line == -1) {
+ return "\n" + message;
+ } else {
+ return String.format("\nLine #%d: ", line) + message;
+ }
+ }
+
+ abstract public String what();
+
+ abstract ExceptionType getType();
+
+ public void setLine(int line) {
+ this.line = line;
+ }
+
+ protected String completeMessage() {
+ return formatMessage(getType().toString() + ": " + what(), line, args);
+ }
+
+ @Override
+ public void printStackTrace() {
+ System.err.println(completeMessage());
+ }
+
+ public List<String> getArguments() {
+ return this.arguments;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/TypeTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/TypeTunkkiError.java
new file mode 100644
index 0000000..a90483c
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/TypeTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class TypeTunkkiError extends TunkkiError {
+ public TypeTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "Incompatible types %s and %s.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.TypeError;
+ }
+}
diff --git a/src/main/com/jantuomi/tunkki/exception/types/UndeclaredSymbolTunkkiError.java b/src/main/com/jantuomi/tunkki/exception/types/UndeclaredSymbolTunkkiError.java
new file mode 100644
index 0000000..5b50ca8
--- /dev/null
+++ b/src/main/com/jantuomi/tunkki/exception/types/UndeclaredSymbolTunkkiError.java
@@ -0,0 +1,20 @@
+package com.jantuomi.tunkki.exception.types;
+
+/**
+ * Created by jan on 2.8.2016.
+ */
+public class UndeclaredSymbolTunkkiError extends TunkkiError {
+ public UndeclaredSymbolTunkkiError(int line, String... args) {
+ super(line, args);
+ }
+
+ @Override
+ public String what() {
+ return "No symbol %s defined in the current scope.";
+ }
+
+ @Override
+ ExceptionType getType() {
+ return ExceptionType.UndeclaredSymbolError;
+ }
+}