error.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "pocketpy/error.h"
  2. namespace pkpy{
  3. SourceData::SourceData(const Str& source, const Str& filename, CompileMode mode) {
  4. int index = 0;
  5. // Skip utf8 BOM if there is any.
  6. if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
  7. // Drop all '\r'
  8. std::stringstream ss;
  9. while(index < source.length()){
  10. if(source[index] != '\r') ss << source[index];
  11. index++;
  12. }
  13. this->filename = filename;
  14. this->source = ss.str();
  15. line_starts.push_back(this->source.c_str());
  16. this->mode = mode;
  17. }
  18. std::pair<const char*,const char*> SourceData::get_line(int lineno) const {
  19. if(lineno == -1) return {nullptr, nullptr};
  20. lineno -= 1;
  21. if(lineno < 0) lineno = 0;
  22. const char* _start = line_starts.at(lineno);
  23. const char* i = _start;
  24. while(*i != '\n' && *i != '\0') i++;
  25. return {_start, i};
  26. }
  27. Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name){
  28. std::stringstream ss;
  29. ss << " " << "File \"" << filename << "\", line " << lineno;
  30. if(!name.empty()) ss << ", in " << name;
  31. ss << '\n';
  32. std::pair<const char*,const char*> pair = get_line(lineno);
  33. Str line = "<?>";
  34. int removed_spaces = 0;
  35. if(pair.first && pair.second){
  36. line = Str(pair.first, pair.second-pair.first).lstrip();
  37. removed_spaces = pair.second - pair.first - line.length();
  38. if(line.empty()) line = "<?>";
  39. }
  40. ss << " " << line;
  41. if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
  42. auto column = cursor - pair.first - removed_spaces;
  43. if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
  44. }
  45. return ss.str();
  46. }
  47. void Exception::st_push(Str&& snapshot){
  48. if(stacktrace.size() >= 8) return;
  49. stacktrace.push(std::move(snapshot));
  50. }
  51. Str Exception::summary() const {
  52. stack<Str> st(stacktrace);
  53. std::stringstream ss;
  54. if(is_re) ss << "Traceback (most recent call last):\n";
  55. while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
  56. if (!msg.empty()) ss << type.sv() << ": " << msg;
  57. else ss << type.sv();
  58. return ss.str();
  59. }
  60. } // namespace pkpy