Refactored some of the error management code

This commit is contained in:
Mike Cifelli 2016-12-14 14:55:54 -05:00
parent ad2375e2bd
commit c4e3740dfb
2 changed files with 41 additions and 29 deletions

View File

@ -3,7 +3,7 @@ package error;
import java.text.MessageFormat;
/**
* Prints error messages.
* Prints error messages and potentially terminates the application.
*/
public class ErrorManager {
@ -15,27 +15,13 @@ public class ErrorManager {
public static final String ANSI_PURPLE = "\u001B[35m";
public static void generateError(LispException lispException) {
generateError(lispException.getMessage(), lispException.getSeverity());
}
/**
* 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);
String color = (lispException.getSeverity() >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED;
String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, lispException.getMessage(),
ANSI_RESET);
System.out.println(formattedMessage);
if (level >= CRITICAL_LEVEL) {
if (lispException.getSeverity() >= CRITICAL_LEVEL) {
System.exit(1);
}
}

View File

@ -32,8 +32,8 @@ public class LispInterpreter {
/**
* Evaluate the S-expressions found in the file given as a command-line
* argument and print the results to the console. If no file name was
* given, retrieve the S-expressions from standard input.
* argument and print the results to the console. If no file name was given,
* retrieve the S-expressions from standard input.
*
* @param args
* the command-line arguments:
@ -51,7 +51,20 @@ public class LispInterpreter {
try {
parser = new LispParser(new FileInputStream(args[0]), args[0]);
} catch (FileNotFoundException e) {
ErrorManager.generateError(e.getMessage(), ErrorManager.CRITICAL_LEVEL);
ErrorManager.generateError(new LispException() {
private static final long serialVersionUID = 1L;
@Override
public int getSeverity() {
return ErrorManager.CRITICAL_LEVEL;
}
@Override
public String getMessage() {
return e.getMessage();
}
});
}
} else {
// no file name was given, create a 'LispParser' on standard input
@ -63,7 +76,7 @@ public class LispInterpreter {
System.out.print(PROMPT);
}
while (! parser.isEof()) {
while (!parser.isEof()) {
try {
SExpression sexpr = parser.getNextSExpression();
String result = MessageFormat.format("{0}{1}{2}", ANSI_GREEN, EVAL.eval(sexpr), ANSI_RESET);
@ -75,7 +88,20 @@ public class LispInterpreter {
ErrorManager.generateError(e);
} catch (RuntimeException e) {
LispInterpreter.erasePrompt(interactive);
ErrorManager.generateError(e.getMessage(), 0);
ErrorManager.generateError(new LispException() {
private static final long serialVersionUID = 1L;
@Override
public int getSeverity() {
return 0;
}
@Override
public String getMessage() {
return e.getMessage();
}
});
}
if (interactive) {