error.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "namedict.h"
  3. #include "str.h"
  4. #include "tuplelist.h"
  5. namespace pkpy{
  6. struct NeedMoreLines {
  7. NeedMoreLines(bool is_compiling_class) : is_compiling_class(is_compiling_class) {}
  8. bool is_compiling_class;
  9. };
  10. struct HandledException {};
  11. struct UnhandledException {};
  12. struct ToBeRaisedException {};
  13. enum CompileMode {
  14. EXEC_MODE,
  15. EVAL_MODE,
  16. REPL_MODE,
  17. JSON_MODE,
  18. };
  19. struct SourceData {
  20. std::string source;
  21. Str filename;
  22. std::vector<const char*> line_starts;
  23. CompileMode mode;
  24. std::pair<const char*,const char*> get_line(int lineno) const {
  25. if(lineno == -1) return {nullptr, nullptr};
  26. lineno -= 1;
  27. if(lineno < 0) lineno = 0;
  28. const char* _start = line_starts.at(lineno);
  29. const char* i = _start;
  30. while(*i != '\n' && *i != '\0') i++;
  31. return {_start, i};
  32. }
  33. SourceData(const Str& source, const Str& filename, CompileMode mode) {
  34. int index = 0;
  35. // Skip utf8 BOM if there is any.
  36. if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
  37. // Remove all '\r'
  38. std::stringstream ss;
  39. while(index < source.length()){
  40. if(source[index] != '\r') ss << source[index];
  41. index++;
  42. }
  43. this->filename = filename;
  44. this->source = ss.str();
  45. line_starts.push_back(this->source.c_str());
  46. this->mode = mode;
  47. }
  48. Str snapshot(int lineno, const char* cursor=nullptr){
  49. std::stringstream ss;
  50. ss << " " << "File \"" << filename << "\", line " << lineno << '\n';
  51. std::pair<const char*,const char*> pair = get_line(lineno);
  52. Str line = "<?>";
  53. int removed_spaces = 0;
  54. if(pair.first && pair.second){
  55. line = Str(pair.first, pair.second-pair.first).lstrip();
  56. removed_spaces = pair.second - pair.first - line.length();
  57. if(line.empty()) line = "<?>";
  58. }
  59. ss << " " << line;
  60. if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
  61. auto column = cursor - pair.first - removed_spaces;
  62. if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
  63. }
  64. return ss.str();
  65. }
  66. };
  67. class Exception {
  68. using StackTrace = stack<Str>;
  69. StrName type;
  70. Str msg;
  71. StackTrace stacktrace;
  72. public:
  73. Exception(StrName type, Str msg): type(type), msg(msg) {}
  74. bool match_type(StrName type) const { return this->type == type;}
  75. bool is_re = true;
  76. void st_push(Str snapshot){
  77. if(stacktrace.size() >= 8) return;
  78. stacktrace.push(snapshot);
  79. }
  80. Str summary() const {
  81. StackTrace st(stacktrace);
  82. std::stringstream ss;
  83. if(is_re) ss << "Traceback (most recent call last):\n";
  84. while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
  85. if (!msg.empty()) ss << type.sv() << ": " << msg;
  86. else ss << type.sv();
  87. return ss.str();
  88. }
  89. };
  90. } // namespace pkpy