ErrorReporting.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef QLOW_ERROR_REPORTING
  2. #define QLOW_ERROR_REPORTING
  3. #include "Logging.h"
  4. namespace qlow
  5. {
  6. struct CodePosition;
  7. class CompileError;
  8. class SyntaxError;
  9. class SemanticError;
  10. class NotLValue;
  11. void reportError(const CompileError& ce);
  12. void reportError(const std::string& message);
  13. }
  14. /*!
  15. * \brief bison-compatible location struct
  16. */
  17. struct qlow::CodePosition
  18. {
  19. const char* filename = "";
  20. int first_line;
  21. int last_line;
  22. int first_column;
  23. int last_column;
  24. inline bool isMultiline(void) const { return first_line != last_line; }
  25. };
  26. class qlow::CompileError
  27. {
  28. protected:
  29. CodePosition where;
  30. public:
  31. inline CompileError(const CodePosition& where) :
  32. where{ where }
  33. {
  34. }
  35. virtual ~CompileError(void);
  36. virtual void print(Logger& logger) const = 0;
  37. void underlineError(Logger& logger) const;
  38. };
  39. class qlow::SyntaxError : public CompileError
  40. {
  41. std::string message;
  42. public:
  43. inline SyntaxError(const CodePosition& where) :
  44. CompileError{ where }
  45. {
  46. }
  47. inline SyntaxError(const std::string& message, const CodePosition& where) :
  48. CompileError{ where },
  49. message{ message }
  50. {
  51. }
  52. virtual void print(Logger&) const override;
  53. };
  54. class qlow::SemanticError : public CompileError
  55. {
  56. std::string message;
  57. public:
  58. enum ErrorCode
  59. {
  60. UNKNOWN_TYPE,
  61. DUPLICATE_CLASS_DEFINITION,
  62. DUPLICATE_FIELD_DECLARATION,
  63. DUPLICATE_METHOD_DEFINITION,
  64. OPERATOR_NOT_FOUND,
  65. FEATURE_NOT_FOUND,
  66. WRONG_NUMBER_OF_ARGUMENTS,
  67. TYPE_MISMATCH,
  68. INVALID_RETURN_TYPE,
  69. };
  70. ErrorCode errorCode;
  71. public:
  72. inline SemanticError(ErrorCode ec, const std::string& arg, const
  73. qlow::CodePosition& where) :
  74. CompileError{ where },
  75. message{ arg },
  76. errorCode{ ec }
  77. {
  78. }
  79. inline SemanticError(const CodePosition& where) :
  80. CompileError{ where }
  81. {
  82. }
  83. virtual void print(Logger&) const override;
  84. virtual std::string getMessage(void) const;
  85. };
  86. class qlow::NotLValue : public SemanticError
  87. {
  88. std::string type;
  89. public:
  90. inline NotLValue(const std::string& type, const CodePosition& where) :
  91. SemanticError{ where },
  92. type{ type }
  93. {
  94. }
  95. inline virtual std::string getMessage(void) const override
  96. {
  97. return "Can't take address of non-lvalue value of type '" +
  98. type + "'";
  99. }
  100. };
  101. #endif // QLOW_ERROR_REPORTING