error.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. SStream 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().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. // max 200 chars
  25. while(*i != '\n' && *i != '\0' && i-_start < 200) i++;
  26. return {_start, i};
  27. }
  28. Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name) const{
  29. SStream ss;
  30. ss << " " << "File \"" << filename << "\", line " << lineno;
  31. if(!name.empty()) ss << ", in " << name;
  32. ss << '\n';
  33. std::pair<const char*,const char*> pair = get_line(lineno);
  34. Str line = "<?>";
  35. int removed_spaces = 0;
  36. if(pair.first && pair.second){
  37. line = Str(pair.first, pair.second-pair.first).lstrip();
  38. removed_spaces = pair.second - pair.first - line.length();
  39. if(line.empty()) line = "<?>";
  40. }
  41. ss << " " << line;
  42. if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
  43. auto column = cursor - pair.first - removed_spaces;
  44. if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
  45. }
  46. return ss.str();
  47. }
  48. Str Exception::summary() const {
  49. stack<ExceptionLine> st(stacktrace);
  50. SStream ss;
  51. if(is_re) ss << "Traceback (most recent call last):\n";
  52. while(!st.empty()) {
  53. ss << st.top().snapshot() << '\n';
  54. st.pop();
  55. }
  56. if (!msg.empty()) ss << type.sv() << ": " << msg;
  57. else ss << type.sv();
  58. return ss.str();
  59. }
  60. } // namespace pkpy