SourceCode.h 948 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Vector.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Position.h>
  11. namespace JS {
  12. class SourceCode : public RefCounted<SourceCode> {
  13. public:
  14. static NonnullRefPtr<SourceCode const> create(String filename, String code);
  15. String const& filename() const;
  16. String const& code() const;
  17. SourceRange range_from_offsets(u32 start_offset, u32 end_offset) const;
  18. private:
  19. SourceCode(String filename, String code);
  20. String m_filename;
  21. String m_code;
  22. // For fast mapping of offsets to line/column numbers, we build a list of
  23. // starting points (with byte offsets into the source string) and which
  24. // line:column they map to. This can then be binary-searched.
  25. void fill_position_cache() const;
  26. Vector<Position> mutable m_cached_positions;
  27. };
  28. }