INILexer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "INILexer.h"
  27. #include <AK/Vector.h>
  28. #include <ctype.h>
  29. namespace GUI {
  30. IniLexer::IniLexer(const StringView& input)
  31. : m_input(input)
  32. {
  33. }
  34. char IniLexer::peek(size_t offset) const
  35. {
  36. if ((m_index + offset) >= m_input.length())
  37. return 0;
  38. return m_input[m_index + offset];
  39. }
  40. char IniLexer::consume()
  41. {
  42. ASSERT(m_index < m_input.length());
  43. char ch = m_input[m_index++];
  44. m_previous_position = m_position;
  45. if (ch == '\n') {
  46. m_position.line++;
  47. m_position.column = 0;
  48. } else {
  49. m_position.column++;
  50. }
  51. return ch;
  52. }
  53. Vector<IniToken> IniLexer::lex()
  54. {
  55. Vector<IniToken> tokens;
  56. size_t token_start_index = 0;
  57. IniPosition token_start_position;
  58. auto emit_token = [&](auto type) {
  59. IniToken token;
  60. token.m_type = type;
  61. token.m_start = m_position;
  62. token.m_end = m_position;
  63. tokens.append(token);
  64. consume();
  65. };
  66. auto begin_token = [&] {
  67. token_start_index = m_index;
  68. token_start_position = m_position;
  69. };
  70. auto commit_token = [&](auto type) {
  71. IniToken token;
  72. token.m_type = type;
  73. token.m_start = token_start_position;
  74. token.m_end = m_previous_position;
  75. tokens.append(token);
  76. };
  77. while (m_index < m_input.length()) {
  78. auto ch = peek();
  79. if (isspace(ch)) {
  80. begin_token();
  81. while (isspace(peek()))
  82. consume();
  83. commit_token(IniToken::Type::Whitespace);
  84. continue;
  85. }
  86. // ;Comment
  87. if (ch == ';') {
  88. begin_token();
  89. while (peek() && peek() != '\n')
  90. consume();
  91. commit_token(IniToken::Type::Comment);
  92. continue;
  93. }
  94. // [Section]
  95. if (ch == '[') {
  96. // [ Token
  97. begin_token();
  98. consume();
  99. commit_token(IniToken::Type::LeftBracket);
  100. // Section
  101. begin_token();
  102. while (peek() && !(peek() == ']' || peek() == '\n'))
  103. consume();
  104. commit_token(IniToken::Type::section);
  105. // ] Token
  106. if (peek() && peek() == ']') {
  107. begin_token();
  108. consume();
  109. commit_token(IniToken::Type::RightBracket);
  110. }
  111. continue;
  112. }
  113. // Empty Line
  114. if (ch == '\n') {
  115. consume();
  116. emit_token(IniToken::Type::Unknown);
  117. continue;
  118. }
  119. // Name=Value
  120. begin_token();
  121. while (peek() && !(peek() == '=' || peek() == '\n'))
  122. consume();
  123. commit_token(IniToken::Type::Name);
  124. if (peek() && peek() == '=') {
  125. begin_token();
  126. consume();
  127. commit_token(IniToken::Type::Equal);
  128. }
  129. if (peek()) {
  130. begin_token();
  131. while (peek() && peek() != '\n')
  132. consume();
  133. commit_token(IniToken::Type::Value);
  134. }
  135. }
  136. return tokens;
  137. }
  138. }