1
0

error.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. enum class InternalExceptionType: int{
  11. Null, Handled, Unhandled, ToBeRaised
  12. };
  13. struct InternalException final{
  14. InternalExceptionType type;
  15. int arg;
  16. InternalException(): type(InternalExceptionType::Null), arg(-1) {}
  17. InternalException(InternalExceptionType type, int arg=-1): type(type), arg(arg) {}
  18. };
  19. enum CompileMode {
  20. EXEC_MODE,
  21. EVAL_MODE,
  22. REPL_MODE,
  23. JSON_MODE,
  24. CELL_MODE
  25. };
  26. struct SourceData {
  27. PK_ALWAYS_PASS_BY_POINTER(SourceData)
  28. Str filename;
  29. CompileMode mode;
  30. Str source;
  31. pod_vector<const char*> line_starts;
  32. bool is_precompiled;
  33. std::vector<Str> _precompiled_tokens;
  34. SourceData(std::string_view source, const Str& filename, CompileMode mode);
  35. SourceData(const Str& filename, CompileMode mode);
  36. std::pair<const char*,const char*> _get_line(int lineno) const;
  37. std::string_view get_line(int lineno) const;
  38. Str snapshot(int lineno, const char* cursor, std::string_view name) const;
  39. };
  40. struct ExceptionLine{
  41. std::shared_ptr<SourceData> src;
  42. int lineno;
  43. const char* cursor;
  44. std::string name;
  45. Str snapshot() const { return src->snapshot(lineno, cursor, name); }
  46. ExceptionLine(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
  47. src(src), lineno(lineno), cursor(cursor), name(name) {}
  48. };
  49. struct Exception {
  50. StrName type;
  51. Str msg;
  52. bool is_re;
  53. int _ip_on_error;
  54. void* _code_on_error;
  55. PyVar _self; // weak reference
  56. stack<ExceptionLine> stacktrace;
  57. Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {}
  58. PyVar self() const{
  59. PK_ASSERT(_self != nullptr);
  60. return _self;
  61. }
  62. template<typename... Args>
  63. void st_push(Args&&... args){
  64. if(stacktrace.size() >= 7) return;
  65. stacktrace.emplace(std::forward<Args>(args)...);
  66. }
  67. Str summary() const;
  68. };
  69. } // namespace pkpy