Refactored some of the exception classes.

This commit is contained in:
Mike Cifelli 2016-12-16 14:00:34 -05:00
parent a137e41a14
commit 6f4a319dab
10 changed files with 77 additions and 93 deletions

View File

@ -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();
}

View File

@ -24,7 +24,6 @@ public interface LispInputStream {
public static class UncheckedIOException extends LispException { public static class UncheckedIOException extends LispException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private IOException ioException; private IOException ioException;
public UncheckedIOException(IOException ioException) { public UncheckedIOException(IOException ioException) {

View File

@ -3,15 +3,12 @@ package scanner;
import static util.Characters.*; import static util.Characters.*;
import java.io.InputStream; import java.io.InputStream;
import java.text.MessageFormat;
import java.util.function.Function; import java.util.function.Function;
import error.LispException; import error.LineColumnException;
import file.FilePosition; import file.FilePosition;
import file.FilePositionTracker; import file.FilePositionTracker;
import token.Token; import token.*;
import token.TokenFactory;
import token.TokenFactoryImpl;
import util.Characters; 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 static final long serialVersionUID = 1L;
private FilePosition position;
public UnterminatedStringException(FilePosition position) { public UnterminatedStringException(FilePosition position) {
this.position = position; super(position);
} }
@Override @Override
public int getSeverity() { public String getMessagePrefix() {
return 0; return "unterminated quoted string";
}
@Override
public String getMessage() {
return MessageFormat.format("unterminated quoted string - line {0}, column {1}", position.getLineNumber(),
position.getColumnNumber());
} }
} }

View File

@ -35,7 +35,6 @@ public class LispNumber extends Atom {
public class InvalidNumberException extends LispException { public class InvalidNumberException extends LispException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String text; private String text;
public InvalidNumberException(String text) { public InvalidNumberException(String text) {

View File

@ -2,9 +2,9 @@ package token;
import java.util.function.Supplier; import java.util.function.Supplier;
import error.LineColumnException;
import file.FilePosition; import file.FilePosition;
import sexpression.SExpression; import sexpression.SExpression;
import token.ParseException.EofEncounteredException;
public class Eof extends Token { public class Eof extends Token {
@ -17,4 +17,17 @@ public class Eof extends Token {
throw new EofEncounteredException(this); 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";
}
}
} }

View File

@ -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 ')'";
}
}
}

View File

@ -2,10 +2,10 @@ package token;
import java.util.function.Supplier; import java.util.function.Supplier;
import error.LineColumnException;
import file.FilePosition; import file.FilePosition;
import sexpression.Nil; import sexpression.Nil;
import sexpression.SExpression; import sexpression.SExpression;
import token.ParseException.StartsWithRightParenthesisException;
public class RightParenthesis extends Token { public class RightParenthesis extends Token {
@ -23,4 +23,17 @@ public class RightParenthesis extends Token {
return Nil.getUniqueInstance(); 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 ')'";
}
}
} }

View File

@ -23,6 +23,10 @@ public abstract class Token {
return text; return text;
} }
public FilePosition getPosition() {
return position;
}
public String getFileName() { public String getFileName() {
return position.getFileName(); return position.getFileName();
} }

View File

@ -2,7 +2,7 @@ package token;
import java.text.MessageFormat; import java.text.MessageFormat;
import error.LispException; import error.LineColumnException;
import file.FilePosition; import file.FilePosition;
public interface TokenFactory { public interface TokenFactory {
@ -11,28 +11,20 @@ public interface TokenFactory {
Token createEOFToken(FilePosition position); Token createEOFToken(FilePosition position);
public static class BadCharacterException extends LispException { public static class BadCharacterException extends LineColumnException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String text; private String text;
private FilePosition position;
public BadCharacterException(String text, FilePosition position) { public BadCharacterException(String text, FilePosition position) {
super(position);
this.text = text; this.text = text;
this.position = position;
} }
@Override @Override
public int getSeverity() { public String getMessagePrefix() {
return 0; 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());
}
}
} }

View File

@ -13,8 +13,8 @@ import error.ErrorManager;
import error.LispException; import error.LispException;
import scanner.LispInputStream.UncheckedIOException; import scanner.LispInputStream.UncheckedIOException;
import scanner.LispScanner.UnterminatedStringException; import scanner.LispScanner.UnterminatedStringException;
import token.ParseException.EofEncounteredException; import token.Eof.EofEncounteredException;
import token.ParseException.StartsWithRightParenthesisException; import token.RightParenthesis.StartsWithRightParenthesisException;
import token.TokenFactory.BadCharacterException; import token.TokenFactory.BadCharacterException;
public class LispParserTester { public class LispParserTester {