package dspl.frontend;
/*
 * This is the Exception class thrown when the compiler's semantic analysis
 * 	detects an error in the source code
 * @author Mike Lynch
 * @date 12/15/06
 */
public class SemanticAnalysisException extends Exception {
	//exception requires this variable to be serializable, so I'm using a ranom long
	private static final long serialVersionUID = 1629540432690778181L;
	private DSPLAST ast;
	private String errMsg;
	
	/*
	 * Generates a new SemanticAnalysisException
	 * @param ast An AST generated from processed source code
	 * @param errMsg A string message describing the exception that occurred
	 */
	public SemanticAnalysisException(DSPLAST ast, String errMsg) {
		super(generateErrorMessage(ast, errMsg));
		this.ast = ast;
		this.errMsg = errMsg;
	}
	
	/*
	 * Generate a human readable error message based on an ast and an error
	 * 	message
	 * @param ast An AST generated from processed source code
	 * @param message a message describing what error occurred
	 * @return the formatted error message as a string
	 */
	public static String generateErrorMessage(DSPLAST ast, String message) {
		try{
			return message + " at line " + ast.getLine() +
			", col " + ast.getColumn();
		}
		catch(NullPointerException e)
		{
			return message;
		}

	}
	
	/*
	 * Prints the details of this exception to the user
	 */
	public void printMessage() {
		System.out.println(generateErrorMessage(ast, errMsg));
	}
}
