ErrorReporting.h 2.8 KB

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