error.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. PK_ALWAYS_PASS_BY_POINTER(SourceData)
  22. Str filename;
  23. CompileMode mode;
  24. Str source;
  25. pod_vector<const char*> line_starts;
  26. bool is_precompiled;
  27. Str _precompiled_tokens;
  28. SourceData(std::string_view source, const Str& filename, CompileMode mode);
  29. SourceData(const Str& filename, CompileMode mode);
  30. std::pair<const char*,const char*> _get_line(int lineno) const;
  31. Str snapshot(int lineno, const char* cursor, std::string_view name) const;
  32. };
  33. struct ExceptionLine{
  34. std::shared_ptr<SourceData> src;
  35. int lineno;
  36. const char* cursor;
  37. std::string name;
  38. Str snapshot() const { return src->snapshot(lineno, cursor, name); }
  39. ExceptionLine(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
  40. src(src), lineno(lineno), cursor(cursor), name(name) {}
  41. };
  42. struct Exception {
  43. StrName type;
  44. Str msg;
  45. bool is_re;
  46. int _ip_on_error;
  47. void* _code_on_error;
  48. PyObject* _self; // weak reference
  49. stack<ExceptionLine> stacktrace;
  50. Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {}
  51. PyObject* self() const{
  52. PK_ASSERT(_self != nullptr);
  53. return _self;
  54. }
  55. template<typename... Args>
  56. void st_push(Args&&... args){
  57. if(stacktrace.size() >= 7) return;
  58. stacktrace.emplace(std::forward<Args>(args)...);
  59. }
  60. Str summary() const;
  61. };
  62. } // namespace pkpy