package parser;
/**
* Class to act as the superclass of all Parser execeptions.
*
* @author Bryan J. Higgs
*/
public abstract class ParserException extends Exception
{
/**
* Gets the message associated with an exception.
* (Abstract method)
*/
@Override
public abstract String getMessage();
/**
* Gets the Line of source where the exception occurred
*/
public String getLine()
{
return m_line;
}
/**
* Gets the character offset within the line where the
* exception occurred.
*/
public int getOffset()
{
return m_offset;
}
/**
* Gets a formatted message of the form:
*
* <message>
* <contents of line>
* ^
* (the above caret is aligned with the character
* corresponding to the offset within the line).
*/
public String getFormattedMessage()
{
String msg = getMessage() + "\n";
msg += getLine() + "\n";
for (int i = 0; i < getOffset(); i++)
{
msg += " ";
}
msg += "^\n"; // The caret (^) should point to the error location.
return msg;
}
/**
* Constructor
* (protected because it's only used by derived classes)
*/
protected ParserException(String line, int offset)
{
m_line = line;
m_offset = offset;
}
/// Private data ///
private String m_line; // The line containing the expression
private int m_offset; // The offset within the line
}
|