# -*- coding: utf-8 -*-


cdef class AMPLException(Exception):
    """
    Represent an exception generated by AMPL.
    """

    def __init__(self, source_name, line_number, offset, message):
        super(AMPLException, self).__init__()
        self.source_name = source_name
        self.line_number = line_number
        self.offset = offset
        self.message = message

    def get_source_name(self):
        """
        Get the name of the file where the error was detected.
        """
        return self.source_name

    def get_line_number(self):
        """
        Get the row where the error is located.
        """
        return self.line_number

    def get_offset(self):
        """
        Get the offset where the error is located.
        """
        return self.offset

    def get_message(self):
        """
        Get the error message.
        """
        return self.message

    def __str__(self):
        if self.offset == -1:
            return self.message
        elif self.source_name == "-":
            return "line " + str(self.line_number) + " offset " + str(self.offset) + "\n" + self.message
        else:
            return self.source_name + "\nline " + str(self.line_number) + " offset " + str(self.offset) + "\n" + self.message

    # Aliases
    getLineNumber = get_line_number
    getMessage = get_message
    getOffset = get_offset
    getSourceName = get_source_name


class PresolveException(RuntimeError):
    pass


class InfeasibilityException(RuntimeError):
    pass
