TextPosition.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Format.h>
  9. namespace Syntax {
  10. class TextPosition {
  11. public:
  12. TextPosition() = default;
  13. TextPosition(size_t line, size_t column)
  14. : m_line(line)
  15. , m_column(column)
  16. {
  17. }
  18. bool is_valid() const { return m_line != 0xffffffffu && m_column != 0xffffffffu; }
  19. size_t line() const { return m_line; }
  20. size_t column() const { return m_column; }
  21. void set_line(size_t line) { m_line = line; }
  22. void set_column(size_t column) { m_column = column; }
  23. bool operator==(TextPosition const& other) const { return m_line == other.m_line && m_column == other.m_column; }
  24. bool operator!=(TextPosition const& other) const { return m_line != other.m_line || m_column != other.m_column; }
  25. bool operator<(TextPosition const& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  26. bool operator>(TextPosition const& other) const { return *this != other && !(*this < other); }
  27. bool operator>=(TextPosition const& other) const { return *this > other || (*this == other); }
  28. private:
  29. size_t m_line { 0xffffffff };
  30. size_t m_column { 0xffffffff };
  31. };
  32. }
  33. template<>
  34. struct AK::Formatter<Syntax::TextPosition> : AK::Formatter<FormatString> {
  35. ErrorOr<void> format(FormatBuilder& builder, Syntax::TextPosition const& value)
  36. {
  37. if (value.is_valid())
  38. return Formatter<FormatString>::format(builder, "({},{})"sv, value.line(), value.column());
  39. return builder.put_string("TextPosition(Invalid)"sv);
  40. }
  41. };