From 8f84c00978dc05007873a9fe1de6a18511dc3cf8 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 28 Jun 2016 21:09:43 +0300 Subject: Restructure project for use with maven --- .../borker/exception/InterpreterException.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/main/com/jantuomi/borker/exception/InterpreterException.java (limited to 'src/main/com/jantuomi/borker/exception/InterpreterException.java') diff --git a/src/main/com/jantuomi/borker/exception/InterpreterException.java b/src/main/com/jantuomi/borker/exception/InterpreterException.java new file mode 100644 index 0000000..153da0a --- /dev/null +++ b/src/main/com/jantuomi/borker/exception/InterpreterException.java @@ -0,0 +1,53 @@ +package com.jantuomi.borker.exception; + + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by jan on 12.6.2016. + */ +public class InterpreterException extends Exception { + + public enum ExceptionType { + IllegalTokenError, + SyntaxError, + TypeError, + ArgumentError, + UndeclaredSymbolError + } + + public static Map 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.ArgumentError, "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, parameters: [%s]."); + } + + private ExceptionType exceptionType; + private String completeMessage; + + public InterpreterException(ExceptionType exceptionType, int line, String... args) { + super(exceptionType.toString(), null, false, false); + this.exceptionType = exceptionType; + this.completeMessage = formatMessage(exceptionType.toString() + ": " + what(exceptionType), line, args); + } + + private static String formatMessage(String message, int line, String... args) { + + message = String.format(message, args); + 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); + } +} -- cgit v1.3