GitCommitLexer.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Brian Gianforcaro <bgianf@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. namespace GUI {
  9. #define FOR_EACH_TOKEN_TYPE \
  10. __TOKEN(Comment) \
  11. __TOKEN(Unknown)
  12. struct GitCommitPosition {
  13. size_t line;
  14. size_t column;
  15. };
  16. struct GitCommitToken {
  17. enum class Type {
  18. #define __TOKEN(x) x,
  19. FOR_EACH_TOKEN_TYPE
  20. #undef __TOKEN
  21. };
  22. char const* to_string() const
  23. {
  24. switch (m_type) {
  25. #define __TOKEN(x) \
  26. case Type::x: \
  27. return #x;
  28. FOR_EACH_TOKEN_TYPE
  29. #undef __TOKEN
  30. }
  31. VERIFY_NOT_REACHED();
  32. }
  33. Type m_type { Type::Unknown };
  34. StringView m_view;
  35. GitCommitPosition m_start;
  36. GitCommitPosition m_end;
  37. };
  38. class GitCommitLexer {
  39. public:
  40. GitCommitLexer(StringView);
  41. Vector<GitCommitToken> lex();
  42. private:
  43. char peek(size_t offset = 0) const;
  44. char consume();
  45. StringView m_input;
  46. size_t m_index { 0 };
  47. GitCommitPosition m_position { 0, 0 };
  48. };
  49. }
  50. #undef FOR_EACH_TOKEN_TYPE