transcendental-lisp/error/ErrorManager.java

51 lines
1.5 KiB
Java

/*
* Name: Mike Cifelli
* Course: CIS 443 - Programming Languages
* Assignment: Lisp Interpreter Phase 1 - Lexical Analysis
*/
package error;
import java.text.MessageFormat;
/**
* <code>ErrorManager</code> is an error handling class for a Lisp interpreter.
*/
public class ErrorManager {
/**
* The lowest "criticality" level of an error that will cause the currently
* running program to terminate.
*/
public static final int CRITICAL_LEVEL = 3;
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_PURPLE = "\u001B[35m";
/**
* Prints out the specified error message to the console and decides
* whether or not to terminate the currently running program.
*
* @param message
* the error message
* @param level
* the "criticality" level of the error
* @postcondition
* If <code>level &gt;= CRITICAL_LEVEL</code> the currently running
* program has been terminated.
*/
public static void generateError(String message, int level) {
String color = (level >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED;
String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, message, ANSI_RESET);
System.out.println(formattedMessage);
if (level >= CRITICAL_LEVEL) {
System.exit(1);
}
}
}