diff --git a/build.xml b/build.xml index 39aa7fc..6c9a99d 100644 --- a/build.xml +++ b/build.xml @@ -1,32 +1,31 @@ - - - - - - + + + + + + - - - - + + + + - - - - + + + + - - - - - - - + + + + + + + - + diff --git a/src/error/LispException.java b/src/error/LispException.java index e0feb9e..41e499b 100644 --- a/src/error/LispException.java +++ b/src/error/LispException.java @@ -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; } diff --git a/src/function/builtin/LOAD.java b/src/function/builtin/LOAD.java index d50c46a..f5f83c2 100644 --- a/src/function/builtin/LOAD.java +++ b/src/function/builtin/LOAD.java @@ -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; } } diff --git a/src/interpreter/LispInterpreter.java b/src/interpreter/LispInterpreter.java index 006aa1b..a10621f 100644 --- a/src/interpreter/LispInterpreter.java +++ b/src/interpreter/LispInterpreter.java @@ -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)); } } diff --git a/src/token/TokenFactory.java b/src/token/TokenFactory.java index 8c5517b..3123aba 100644 --- a/src/token/TokenFactory.java +++ b/src/token/TokenFactory.java @@ -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; diff --git a/src/token/TokenFactoryImpl.java b/src/token/TokenFactoryImpl.java index 796e147..eb22e69 100644 --- a/src/token/TokenFactoryImpl.java +++ b/src/token/TokenFactoryImpl.java @@ -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) { diff --git a/test/function/builtin/APPLYTester.java b/test/function/builtin/APPLYTester.java index 3547ab3..90a4aa0 100644 --- a/test/function/builtin/APPLYTester.java +++ b/test/function/builtin/APPLYTester.java @@ -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))"); } diff --git a/test/function/builtin/SYMBOL_FUNCTIONTester.java b/test/function/builtin/SYMBOL_FUNCTIONTester.java index 1cf33f4..3a892be 100644 --- a/test/function/builtin/SYMBOL_FUNCTIONTester.java +++ b/test/function/builtin/SYMBOL_FUNCTIONTester.java @@ -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)"; diff --git a/test/token/TokenFactoryTester.java b/test/token/TokenFactoryTester.java index dfec019..6f24bb7 100644 --- a/test/token/TokenFactoryTester.java +++ b/test/token/TokenFactoryTester.java @@ -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]"); } }