123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #ifndef QLOW_ERROR_REPORTING
- #define QLOW_ERROR_REPORTING
- #include "Printer.h"
- namespace qlow
- {
- struct CodePosition;
-
- class InternalError;
- class CompileError;
- class SyntaxError;
-
- class SemanticError;
- class NotLValue;
-
- void reportError(const CompileError& ce) noexcept;
- void reportError(const std::string& message) noexcept;
- void printError(Printer& printer, const std::string& message) noexcept;
- void printError(Printer& printer, const std::string& message, const CodePosition& where) noexcept;
- }
- /*!
- * \brief bison-compatible location struct
- */
- struct qlow::CodePosition
- {
- std::string filename = "";
- int first_line;
- int last_line;
- int first_column;
- int last_column;
-
- inline bool isMultiline(void) const { return first_line != last_line; }
- std::string getReportFormat(void) const noexcept;
- };
- class qlow::InternalError
- {
- public:
- enum ErrorCode
- {
- OUT_OF_MEMORY,
- /// initialization of the flex lexer failed
- PARSER_INIT_FAILED,
- /// destruction of the flex lexer failed
- PARSER_DEST_FAILED,
- /// bison routine returned error value
- PARSER_FAILED,
- /// tried to determine the kind of an invalid type
- INVALID_TYPE
- };
- private:
- ErrorCode errorCode;
- public:
- InternalError(ErrorCode ec) :
- errorCode{ ec } {}
- void print(Printer& printer = Printer::getInstance()) const noexcept;
- const std::string& getMessage(void) const noexcept;
- };
- class qlow::CompileError
- {
- protected:
- CodePosition where;
- public:
- inline CompileError(const CodePosition& where) :
- where{ where }
- {
- }
-
- virtual ~CompileError(void);
- virtual void print(Printer& printer = Printer::getInstance()) const noexcept = 0;
-
- void underlineError(Printer& printer = Printer::getInstance()) const noexcept;
- };
- class qlow::SyntaxError : public CompileError
- {
- std::string message;
- public:
- inline SyntaxError(const CodePosition& where) :
- CompileError{ where }
- {
- }
-
- inline SyntaxError(const std::string& message, const CodePosition& where) :
- CompileError{ where },
- message{ message }
- {
- }
-
- virtual void print(Printer&) const noexcept override;
- };
- class qlow::SemanticError : public CompileError
- {
- std::string message;
- public:
- enum ErrorCode
- {
- UNKNOWN_TYPE,
- DUPLICATE_CLASS_DEFINITION,
- DUPLICATE_FIELD_DECLARATION,
- DUPLICATE_METHOD_DEFINITION,
-
- OPERATOR_NOT_FOUND,
- FEATURE_NOT_FOUND,
- WRONG_NUMBER_OF_ARGUMENTS,
- TYPE_MISMATCH,
- INVALID_RETURN_TYPE,
- };
-
-
- ErrorCode errorCode;
- public:
- inline SemanticError(ErrorCode ec, const std::string& arg, const
- qlow::CodePosition& where) :
- CompileError{ where },
- message{ arg },
- errorCode{ ec }
- {
- }
-
- inline SemanticError(const CodePosition& where) :
- CompileError{ where }
- {
- }
- virtual void print(Printer& p = Printer::getInstance()) const noexcept override;
- virtual std::string getMessage(void) const noexcept;
- };
- class qlow::NotLValue : public SemanticError
- {
- std::string type;
- public:
- inline NotLValue(const std::string& type, const CodePosition& where) noexcept :
- SemanticError{ where },
- type{ type }
- {
- }
-
- inline virtual std::string getMessage(void) const noexcept override
- {
- return "Can't take address of non-lvalue value of type '" +
- type + "'";
- }
- };
- #endif // QLOW_ERROR_REPORTING
|