SourceRange.h 912 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Types.h>
  11. #include <LibJS/Position.h>
  12. #include <LibJS/SourceCode.h>
  13. namespace JS {
  14. struct SourceRange {
  15. [[nodiscard]] bool contains(Position const& position) const { return position.offset <= end.offset && position.offset >= start.offset; }
  16. NonnullRefPtr<SourceCode const> code;
  17. Position start;
  18. Position end;
  19. DeprecatedString filename() const;
  20. };
  21. struct UnrealizedSourceRange {
  22. [[nodiscard]] SourceRange realize() const
  23. {
  24. VERIFY(source_code);
  25. return source_code->range_from_offsets(start_offset, end_offset);
  26. }
  27. RefPtr<SourceCode const> source_code;
  28. u32 start_offset { 0 };
  29. u32 end_offset { 0 };
  30. };
  31. }