error.h 3.0 KB

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