Lexer.h 1.2 KB

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