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

@ -1,32 +1,31 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<project name="LispInterpreter" basedir="." default="compile"> <project name="LispInterpreter" basedir="." default="compile">
<property name="src.dir" value="src" /> <property name="src.dir" value="src" />
<property name="build.dir" value="build" /> <property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" /> <property name="classes.dir" value="${build.dir}/classes" />
<property name="jar.dir" value="jar" /> <property name="jar.dir" value="jar" />
<property name="jar-file" value="${jar.dir}/LispInterpreter.jar" /> <property name="jar-file" value="${jar.dir}/LispInterpreter.jar" />
<property name="main-class" value="main.LispMain" /> <property name="main-class" value="main.LispMain" />
<target name="clean"> <target name="clean">
<delete dir="${build.dir}" /> <delete dir="${build.dir}" />
<delete dir="${jar.dir}" /> <delete dir="${jar.dir}" />
</target> </target>
<target name="compile"> <target name="compile">
<mkdir dir="${classes.dir}" /> <mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" <javac srcdir="${src.dir}" destdir="${classes.dir}"
destdir="${classes.dir}" includeantruntime="false" />
includeantruntime="false" /> </target>
</target>
<target name="jar" depends="compile"> <target name="jar" depends="compile">
<jar destfile="${jar-file}" basedir="${classes.dir}"> <jar destfile="${jar-file}" basedir="${classes.dir}">
<manifest> <manifest>
<attribute name="Main-Class" value="${main-class}" /> <attribute name="Main-Class" value="${main-class}" />
</manifest> </manifest>
</jar> </jar>
</target> </target>
<target name="clean-jar" depends="clean, jar" /> <target name="clean-jar" depends="clean, jar" />
</project> </project>

View File

@ -4,18 +4,6 @@ public abstract class LispException extends RuntimeException {
private static final long serialVersionUID = 1L; 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() { public int getSeverity() {
return 0; return 0;
} }

View File

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

View File

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

View File

@ -2,7 +2,7 @@ package token;
import java.text.MessageFormat; import java.text.MessageFormat;
import error.LineColumnException; import error.*;
import file.FilePosition; import file.FilePosition;
public interface TokenFactory { public interface TokenFactory {
@ -11,6 +11,25 @@ public interface TokenFactory {
Token createEOFToken(FilePosition position); 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 { public static class BadCharacterException extends LineColumnException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

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

View File

@ -5,6 +5,7 @@ import static testutil.TestUtilities.*;
import org.junit.Test; import org.junit.Test;
import function.ArgumentValidator.*; import function.ArgumentValidator.*;
import function.builtin.EVAL.UndefinedFunctionException;
import sexpression.Cons; import sexpression.Cons;
public class APPLYTester { public class APPLYTester {
@ -38,7 +39,7 @@ public class APPLYTester {
assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList)); assertSExpressionsMatch(parseString("35"), APPLY.apply(parsedArgumentList));
} }
@Test(expected = RuntimeException.class) @Test(expected = UndefinedFunctionException.class)
public void testApplyWithUndefinedFunction() { public void testApplyWithUndefinedFunction() {
evaluateString("(apply 'f '(1 2 3))"); 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()); assertEquals("(Y (N M) (+ N M))", evaluateString(input).toString());
} }
@Test(expected = RuntimeException.class) @Test(expected = UndefinedSymbolFunctionException.class)
public void testSymbolFunction_NonFunction() { public void testSymbolFunction_NonFunction() {
String input = "(symbol-function 'a)"; String input = "(symbol-function 'a)";

View File

@ -1,11 +1,12 @@
package token; package token;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.*;
import org.junit.*; import org.junit.*;
import error.ErrorManager;
import file.FilePosition; import file.FilePosition;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.*;
public class TokenFactoryTester { public class TokenFactoryTester {
@ -29,12 +30,6 @@ public class TokenFactoryTester {
assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof); assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof);
} }
@Test(expected = RuntimeException.class)
public void testEmptyString_ThrowsException() {
String text = "";
createToken(text);
}
@Test @Test
public void testLeftParenthesisCreation() { public void testLeftParenthesisCreation() {
String text = "("; String text = "(";
@ -71,10 +66,34 @@ public class TokenFactoryTester {
assertTrue(createToken(text) instanceof QuotedString); 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) @Test(expected = BadCharacterException.class)
public void testBadCharacter() { public void testBadCharacter_ThrowsException() {
String text = "[abc]"; createToken("[abc]");
tokenFactory.createToken(text, testPosition);
} }
} }