sourcedata.hpp 878 B

12345678910111213141516171819202122232425262728293031
  1. #pragma once
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/common/str.hpp"
  4. #include "pocketpy/objects/sourcedata.h"
  5. namespace pkpy {
  6. struct SourceData : public pkpy_SourceData {
  7. SourceData(std::string_view source, const Str& filename, CompileMode mode) {
  8. pkpy_SourceData__ctor(this, {source.data(), (int)source.size()}, &filename, mode);
  9. }
  10. ~SourceData() {
  11. pkpy_SourceData__dtor(this);
  12. }
  13. std::string_view get_line(int lineno) const {
  14. const char *st, *ed;
  15. if (pkpy_SourceData__get_line(this, lineno, &st, &ed)) {
  16. return std::string_view(st, ed - st);
  17. }
  18. return "<?>";
  19. }
  20. Str snapshot(int lineno, const char* cursor, std::string_view name) const {
  21. return pkpy_SourceData__snapshot(this, lineno, cursor, name.empty() ? nullptr : name.data());
  22. }
  23. };
  24. } // namespace pkpy