aboutsummaryrefslogtreecommitdiffstats
path: root/main/exception/InterpreterException.java
blob: 26e39fd5917915c6789aba8655bf3413ce19c9f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 extends Exception {

    public enum ExceptionType {
        IllegalTokenError,
        UnknownOperatorError,
        SyntaxError,
        TypeError
    }

    public static Map<ExceptionType, String> errorTexts = new HashMap<>();

    static {
        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 ExceptionType exceptionType;

    public InterpreterException(ExceptionType exceptionType, int line, List<String> args) {
        super(formatMessage(exceptionType.toString() + what(exceptionType), line, args));
        this.exceptionType = exceptionType;
    }

    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);
    }
}