diff --git a/src/error/LineColumnException.java b/src/error/LineColumnException.java new file mode 100644 index 0000000..76a27b1 --- /dev/null +++ b/src/error/LineColumnException.java @@ -0,0 +1,29 @@ +package error; + +import java.text.MessageFormat; + +import file.FilePosition; + +public abstract class LineColumnException extends LispException { + + private static final long serialVersionUID = 1L; + private FilePosition position; + + public LineColumnException(FilePosition position) { + this.position = position; + } + + @Override + public int getSeverity() { + return 0; + } + + @Override + public String getMessage() { + return MessageFormat.format("{0} - line {1}, column {2}", getMessagePrefix(), position.getLineNumber(), + position.getColumnNumber()); + } + + public abstract String getMessagePrefix(); + +} \ No newline at end of file diff --git a/src/scanner/LispInputStream.java b/src/scanner/LispInputStream.java index d2f93d4..5f97669 100644 --- a/src/scanner/LispInputStream.java +++ b/src/scanner/LispInputStream.java @@ -24,7 +24,6 @@ public interface LispInputStream { public static class UncheckedIOException extends LispException { private static final long serialVersionUID = 1L; - private IOException ioException; public UncheckedIOException(IOException ioException) { diff --git a/src/scanner/LispScanner.java b/src/scanner/LispScanner.java index c55bb9a..cc23df6 100644 --- a/src/scanner/LispScanner.java +++ b/src/scanner/LispScanner.java @@ -3,15 +3,12 @@ package scanner; import static util.Characters.*; import java.io.InputStream; -import java.text.MessageFormat; import java.util.function.Function; -import error.LispException; +import error.LineColumnException; import file.FilePosition; import file.FilePositionTracker; -import token.Token; -import token.TokenFactory; -import token.TokenFactoryImpl; +import token.*; import util.Characters; /** @@ -152,24 +149,17 @@ public class LispScanner { } } - public static class UnterminatedStringException extends LispException { + public static class UnterminatedStringException extends LineColumnException { private static final long serialVersionUID = 1L; - private FilePosition position; public UnterminatedStringException(FilePosition position) { - this.position = position; + super(position); } @Override - public int getSeverity() { - return 0; - } - - @Override - public String getMessage() { - return MessageFormat.format("unterminated quoted string - line {0}, column {1}", position.getLineNumber(), - position.getColumnNumber()); + public String getMessagePrefix() { + return "unterminated quoted string"; } } diff --git a/src/sexpression/LispNumber.java b/src/sexpression/LispNumber.java index a34e6c0..07eb263 100644 --- a/src/sexpression/LispNumber.java +++ b/src/sexpression/LispNumber.java @@ -35,9 +35,8 @@ public class LispNumber extends Atom { public class InvalidNumberException extends LispException { private static final long serialVersionUID = 1L; - private String text; - + public InvalidNumberException(String text) { this.text = text; } @@ -46,7 +45,7 @@ public class LispNumber extends Atom { public int getSeverity() { return 0; } - + @Override public String getMessage() { return MessageFormat.format("{0} is not a valid integer", text); diff --git a/src/token/Eof.java b/src/token/Eof.java index 9302858..6f3ff55 100644 --- a/src/token/Eof.java +++ b/src/token/Eof.java @@ -2,9 +2,9 @@ package token; import java.util.function.Supplier; +import error.LineColumnException; import file.FilePosition; import sexpression.SExpression; -import token.ParseException.EofEncounteredException; public class Eof extends Token { @@ -17,4 +17,17 @@ public class Eof extends Token { throw new EofEncounteredException(this); } + public static class EofEncounteredException extends LineColumnException { + + private static final long serialVersionUID = 1L; + + public EofEncounteredException(Token token) { + super(token.getPosition()); + } + + public String getMessagePrefix() { + return "end-of-file encountered"; + } + } + } diff --git a/src/token/ParseException.java b/src/token/ParseException.java deleted file mode 100644 index 7c765ce..0000000 --- a/src/token/ParseException.java +++ /dev/null @@ -1,55 +0,0 @@ -package token; - -import java.text.MessageFormat; - -import error.LispException; - -public abstract class ParseException extends LispException { - - private static final long serialVersionUID = 1L; - private Token token; - - public ParseException(Token token) { - this.token = token; - } - - @Override - public int getSeverity() { - return 0; - } - - @Override - public String getMessage() { - return MessageFormat.format("{0} - line {1}, column {2}", getMessagePrefix(), token.getLine(), - token.getColumn()); - } - - public abstract String getMessagePrefix(); - - public static class EofEncounteredException extends ParseException { - - private static final long serialVersionUID = 1L; - - public EofEncounteredException(Token token) { - super(token); - } - - public String getMessagePrefix() { - return "end-of-file encountered"; - } - } - - public static class StartsWithRightParenthesisException extends ParseException { - - private static final long serialVersionUID = 1L; - - public StartsWithRightParenthesisException(Token token) { - super(token); - } - - public String getMessagePrefix() { - return "Expression begins with ')'"; - } - } - -} \ No newline at end of file diff --git a/src/token/RightParenthesis.java b/src/token/RightParenthesis.java index fca5cc3..7b716b0 100644 --- a/src/token/RightParenthesis.java +++ b/src/token/RightParenthesis.java @@ -2,10 +2,10 @@ package token; import java.util.function.Supplier; +import error.LineColumnException; import file.FilePosition; import sexpression.Nil; import sexpression.SExpression; -import token.ParseException.StartsWithRightParenthesisException; public class RightParenthesis extends Token { @@ -23,4 +23,17 @@ public class RightParenthesis extends Token { return Nil.getUniqueInstance(); } + public static class StartsWithRightParenthesisException extends LineColumnException { + + private static final long serialVersionUID = 1L; + + public StartsWithRightParenthesisException(Token token) { + super(token.getPosition()); + } + + public String getMessagePrefix() { + return "Expression begins with ')'"; + } + } + } diff --git a/src/token/Token.java b/src/token/Token.java index b2dd533..5988585 100644 --- a/src/token/Token.java +++ b/src/token/Token.java @@ -22,6 +22,10 @@ public abstract class Token { public String getText() { return text; } + + public FilePosition getPosition() { + return position; + } public String getFileName() { return position.getFileName(); diff --git a/src/token/TokenFactory.java b/src/token/TokenFactory.java index 6bbc750..8c5517b 100644 --- a/src/token/TokenFactory.java +++ b/src/token/TokenFactory.java @@ -2,7 +2,7 @@ package token; import java.text.MessageFormat; -import error.LispException; +import error.LineColumnException; import file.FilePosition; public interface TokenFactory { @@ -11,28 +11,20 @@ public interface TokenFactory { Token createEOFToken(FilePosition position); - public static class BadCharacterException extends LispException { + public static class BadCharacterException extends LineColumnException { private static final long serialVersionUID = 1L; - private String text; - private FilePosition position; public BadCharacterException(String text, FilePosition position) { + super(position); this.text = text; - this.position = position; } @Override - public int getSeverity() { - return 0; + public String getMessagePrefix() { + return MessageFormat.format("illegal character >>{0}<<", text); } - - @Override - public String getMessage() { - return MessageFormat.format("illegal character >>{0}<< - line {1}, column {2}", text, - position.getLineNumber(), position.getColumnNumber()); - } - } + } diff --git a/test/parser/LispParserTester.java b/test/parser/LispParserTester.java index 8d961dd..241d294 100644 --- a/test/parser/LispParserTester.java +++ b/test/parser/LispParserTester.java @@ -13,8 +13,8 @@ import error.ErrorManager; import error.LispException; import scanner.LispInputStream.UncheckedIOException; import scanner.LispScanner.UnterminatedStringException; -import token.ParseException.EofEncounteredException; -import token.ParseException.StartsWithRightParenthesisException; +import token.Eof.EofEncounteredException; +import token.RightParenthesis.StartsWithRightParenthesisException; import token.TokenFactory.BadCharacterException; public class LispParserTester {