您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

100 行
4.1 KiB

  1. #pragma once
  2. #include "lexer.h"
  3. #include <sstream>
  4. #include <stdexcept>
  5. namespace molasses {
  6. namespace details {
  7. template<typename... RestT>
  8. std::string concatenate_builder_impl(std::stringstream& stream)
  9. requires(sizeof...(RestT) == 0)
  10. {
  11. return stream.str();
  12. }
  13. template<typename FirstT, typename... RestT>
  14. std::string concatenate_builder_impl(std::stringstream& stream, FirstT first, RestT... rest) {
  15. stream << first;
  16. return concatenate_builder_impl(stream, rest...);
  17. }
  18. template<typename... RestT>
  19. std::string concatenate_builder(RestT... rest) {
  20. std::stringstream stream{};
  21. return concatenate_builder_impl(stream, rest...);
  22. }
  23. }
  24. struct build_system_error : std::runtime_error {
  25. explicit build_system_error(const std::string& message)
  26. : std::runtime_error(message) {}
  27. };
  28. struct interpreter_error : std::runtime_error {
  29. explicit interpreter_error(const std::string& message)
  30. : std::runtime_error(message) {}
  31. };
  32. struct parser_error : public std::runtime_error {
  33. explicit parser_error(const std::string& str)
  34. : std::runtime_error(str) {}
  35. };
  36. struct type_input_error : public parser_error {
  37. type_input_error()
  38. : parser_error("Bad type provided") {}
  39. // TODO: Better error message
  40. };
  41. struct value_missing_error : public parser_error {
  42. value_missing_error()
  43. : parser_error("Expected value, none provided") {}
  44. // TODO: Better error message
  45. };
  46. struct procedure_stack_error : public parser_error {
  47. procedure_stack_error()
  48. : parser_error("Expected the stack to look like the return stack upon completion\n") {}
  49. // TODO: Better error message
  50. };
  51. struct unexpected_token_error : public parser_error {
  52. unexpected_token_error(const symbol& sym, const std::string& found, const std::string& expected)
  53. : parser_error(details::concatenate_builder(
  54. "Unexpected token encountered\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column, "\n",
  55. "\tExpected ", expected, "\n", "\tFound ", found, "\n"
  56. )) {}
  57. };
  58. struct duplicate_label_error : public parser_error {
  59. duplicate_label_error(const symbol& sym, const std::string& found)
  60. : parser_error(details::concatenate_builder(
  61. "Unexpected label duplication\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column, "\n",
  62. "\tFound ", found, " duplicated label\n"
  63. )) {}
  64. };
  65. struct expecting_token_error : public parser_error {
  66. expecting_token_error(const std::string& expected, const std::string& context)
  67. : parser_error(details::concatenate_builder(
  68. "An expected token has not been encountered before the end of the input\n", "\tExpected ", expected,
  69. "\n", "\t", context, "\n"
  70. )) {}
  71. // TODO: Better error message
  72. };
  73. struct orphan_goto_error : public parser_error {
  74. orphan_goto_error(const symbol& sym, const std::string& label_name)
  75. : parser_error(details::concatenate_builder(
  76. "A __GOTO__ has been found to be without a label\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column,
  77. "\n", "\tExpected a label ", label_name,
  78. "\n"
  79. )) {}
  80. // TODO: Better error message
  81. };
  82. struct unknown_token_error : public parser_error {
  83. explicit unknown_token_error(const symbol& sym)
  84. : parser_error(details::concatenate_builder(
  85. "An unknown token has been encountered\n", "\tAt ", sym.file_name, ":", sym.line, ":", sym.column,
  86. "\n"
  87. )) {}
  88. // TODO: Better error message
  89. };
  90. struct type_expected_with_modifier_error : public parser_error {
  91. type_expected_with_modifier_error()
  92. : parser_error("A type is expected before a modifier") {}
  93. // TODO: Better error message
  94. };
  95. }