GMLLexer.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 "GMLLexer.h"
  27. #include <AK/Vector.h>
  28. #include <ctype.h>
  29. namespace GUI {
  30. GMLLexer::GMLLexer(const StringView& input)
  31. : m_input(input)
  32. {
  33. }
  34. char GMLLexer::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 GMLLexer::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. static bool is_valid_identifier_start(char ch)
  54. {
  55. return isalpha(ch) || ch == '_';
  56. }
  57. static bool is_valid_identifier_character(char ch)
  58. {
  59. return isalnum(ch) || ch == '_';
  60. }
  61. static bool is_valid_class_start(char ch)
  62. {
  63. return isalpha(ch) || ch == '_';
  64. }
  65. static bool is_valid_class_character(char ch)
  66. {
  67. return isalnum(ch) || ch == '_' || ch == ':';
  68. }
  69. Vector<GMLToken> GMLLexer::lex()
  70. {
  71. Vector<GMLToken> tokens;
  72. size_t token_start_index = 0;
  73. GMLPosition token_start_position;
  74. auto begin_token = [&] {
  75. token_start_index = m_index;
  76. token_start_position = m_position;
  77. };
  78. auto commit_token = [&](auto type) {
  79. GMLToken token;
  80. token.m_view = m_input.substring_view(token_start_index, m_index - token_start_index);
  81. token.m_type = type;
  82. token.m_start = token_start_position;
  83. token.m_end = m_previous_position;
  84. tokens.append(token);
  85. };
  86. auto consume_class = [&] {
  87. begin_token();
  88. consume();
  89. commit_token(GMLToken::Type::ClassMarker);
  90. begin_token();
  91. while (is_valid_class_character(peek()))
  92. consume();
  93. commit_token(GMLToken::Type::ClassName);
  94. };
  95. while (m_index < m_input.length()) {
  96. if (isspace(peek(0))) {
  97. begin_token();
  98. while (isspace(peek()))
  99. consume();
  100. continue;
  101. }
  102. // C++ style comments
  103. if (peek(0) && peek(0) == '/' && peek(1) == '/') {
  104. begin_token();
  105. while (peek() && peek() != '\n')
  106. consume();
  107. commit_token(GMLToken::Type::Comment);
  108. continue;
  109. }
  110. if (peek(0) == '{') {
  111. begin_token();
  112. consume();
  113. commit_token(GMLToken::Type::LeftCurly);
  114. continue;
  115. }
  116. if (peek(0) == '}') {
  117. begin_token();
  118. consume();
  119. commit_token(GMLToken::Type::RightCurly);
  120. continue;
  121. }
  122. if (peek(0) == '@' && is_valid_class_start(peek(1))) {
  123. consume_class();
  124. continue;
  125. }
  126. if (is_valid_identifier_start(peek(0))) {
  127. begin_token();
  128. consume();
  129. while (is_valid_identifier_character(peek(0)))
  130. consume();
  131. commit_token(GMLToken::Type::Identifier);
  132. continue;
  133. }
  134. if (peek(0) == ':') {
  135. begin_token();
  136. consume();
  137. commit_token(GMLToken::Type::Colon);
  138. while (isspace(peek()))
  139. consume();
  140. if (peek(0) == '@' && is_valid_class_start(peek(1))) {
  141. consume_class();
  142. } else {
  143. begin_token();
  144. while (peek() && peek() != '\n')
  145. consume();
  146. commit_token(GMLToken::Type::JsonValue);
  147. }
  148. continue;
  149. }
  150. consume();
  151. commit_token(GMLToken::Type::Unknown);
  152. }
  153. return tokens;
  154. }
  155. }