Removed RuntimeExceptions and associated logic

This commit is contained in:
Mike Cifelli 2017-02-09 12:09:51 -05:00
parent 1a25ddc35f
commit bdcb2227c9
9 changed files with 79 additions and 60 deletions

View File

@ -15,8 +15,7 @@
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
<javac srcdir="${src.dir}" destdir="${classes.dir}"
includeantruntime="false" />
</target>

View File

@ -4,18 +4,6 @@ public abstract class LispException extends RuntimeException {
private static final long serialVersionUID = 1L;
public static final LispException convertToLispException(RuntimeException e) {
return new LispException() {
private static final long serialVersionUID = 1L;
@Override
public String getMessage() {
return e.getMessage();
}
};
}
public int getSeverity() {
return 0;
}

View File

@ -1,7 +1,5 @@
package function.builtin;
import static error.LispException.convertToLispException;
import java.io.*;
import java.text.MessageFormat;
@ -67,9 +65,6 @@ public class LOAD extends LispFunction {
} catch (LispException e) {
errorManager.generateError(e);
return false;
} catch (RuntimeException e) {
errorManager.generateError(convertToLispException(e));
return false;
}
}

View File

@ -1,7 +1,5 @@
package interpreter;
import static error.LispException.convertToLispException;
import java.text.MessageFormat;
import environment.RuntimeEnvironment;
@ -44,9 +42,6 @@ public class LispInterpreter {
} catch (LispException e) {
erasePrompt();
errorManager.generateError(e);
} catch (RuntimeException e) {
erasePrompt();
errorManager.generateError(convertToLispException(e));
}
}

View File

@ -2,7 +2,7 @@ package token;
import java.text.MessageFormat;
import error.LineColumnException;
import error.*;
import file.FilePosition;
public interface TokenFactory {
@ -11,6 +11,25 @@ public interface TokenFactory {
Token createEOFToken(FilePosition position);
public static class EmptyTokenTextException extends LineColumnException {
private static final long serialVersionUID = 1L;
public EmptyTokenTextException(FilePosition position) {
super(position);
}
@Override
public int getSeverity() {
return ErrorManager.CRITICAL_LEVEL;
}
@Override
public String getMessagePrefix() {
return "empty token";
}
}
public static class BadCharacterException extends LineColumnException {
private static final long serialVersionUID = 1L;

View File

@ -8,6 +8,9 @@ import util.Characters;
public class TokenFactoryImpl implements TokenFactory {
public Token createToken(String text, FilePosition position) {
if (text.length() == 0)
throw new EmptyTokenTextException(position);
char firstCharacter = text.charAt(0);
switch (firstCharacter) {

View File

@ -5,6 +5,7 @@ import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.*;
import function.builtin.EVAL.UndefinedFunctionException;
import sexpression.Cons;
public class APPLYTester {
@ -38,7 +39,7 @@ public class APPLYTester {
assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList));
}
@Test(expected = RuntimeException.class)
@Test(expected = UndefinedFunctionException.class)
public void testApplyWithUndefinedFunction() {
evaluateString("(apply 'f '(1 2 3))");
}

View File

@ -38,7 +38,7 @@ public class SYMBOL_FUNCTIONTester {
assertEquals("(Y (N M) (+ N M))", evaluateString(input).toString());
}
@Test(expected = RuntimeException.class)
@Test(expected = UndefinedSymbolFunctionException.class)
public void testSymbolFunction_NonFunction() {
String input = "(symbol-function 'a)";

View File

@ -1,11 +1,12 @@
package token;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import org.junit.*;
import error.ErrorManager;
import file.FilePosition;
import token.TokenFactory.BadCharacterException;
import token.TokenFactory.*;
public class TokenFactoryTester {
@ -29,12 +30,6 @@ public class TokenFactoryTester {
assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof);
}
@Test(expected = RuntimeException.class)
public void testEmptyString_ThrowsException() {
String text = "";
createToken(text);
}
@Test
public void testLeftParenthesisCreation() {
String text = "(";
@ -71,10 +66,34 @@ public class TokenFactoryTester {
assertTrue(createToken(text) instanceof QuotedString);
}
@Test(expected = EmptyTokenTextException.class)
public void testEmptyString_ThrowsException() {
createToken("");
}
@Test
public void EmptyTokenTextException_ContainsCorrectSeverity() {
try {
createToken("");
} catch (EmptyTokenTextException e) {
assertTrue(e.getSeverity() == ErrorManager.CRITICAL_LEVEL);
}
}
@Test
public void EmptyTokenTextException_ContainsMessage() {
try {
createToken("");
} catch (EmptyTokenTextException e) {
String message = e.getMessage();
assertNotNull(message);
assertTrue(message.length() > 0);
}
}
@Test(expected = BadCharacterException.class)
public void testBadCharacter() {
String text = "[abc]";
tokenFactory.createToken(text, testPosition);
public void testBadCharacter_ThrowsException() {
createToken("[abc]");
}
}