INILexer.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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(Unknown) \
  11. __TOKEN(Comment) \
  12. __TOKEN(Whitespace) \
  13. __TOKEN(Section) \
  14. __TOKEN(LeftBracket) \
  15. __TOKEN(RightBracket) \
  16. __TOKEN(Name) \
  17. __TOKEN(Value) \
  18. __TOKEN(Equal)
  19. struct IniPosition {
  20. size_t line;
  21. size_t column;
  22. };
  23. struct IniToken {
  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. IniPosition m_start;
  42. IniPosition m_end;
  43. };
  44. class IniLexer {
  45. public:
  46. IniLexer(StringView);
  47. Vector<IniToken> lex();
  48. private:
  49. char peek(size_t offset = 0) const;
  50. char consume();
  51. StringView m_input;
  52. size_t m_index { 0 };
  53. IniPosition m_position { 0, 0 };
  54. };
  55. }
  56. #undef FOR_EACH_TOKEN_TYPE