GMLLexer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_character(char ch)
  62. {
  63. return isalnum(ch) || ch == '_' || ch == ':';
  64. }
  65. Vector<GMLToken> GMLLexer::lex()
  66. {
  67. Vector<GMLToken> tokens;
  68. size_t token_start_index = 0;
  69. GMLPosition token_start_position;
  70. auto begin_token = [&] {
  71. token_start_index = m_index;
  72. token_start_position = m_position;
  73. };
  74. auto commit_token = [&](auto type) {
  75. GMLToken token;
  76. token.m_view = m_input.substring_view(token_start_index, m_index - token_start_index);
  77. token.m_type = type;
  78. token.m_start = token_start_position;
  79. token.m_end = m_previous_position;
  80. tokens.append(token);
  81. };
  82. auto consume_class = [&] {
  83. begin_token();
  84. consume();
  85. commit_token(GMLToken::Type::ClassMarker);
  86. begin_token();
  87. while (is_valid_class_character(peek()))
  88. consume();
  89. commit_token(GMLToken::Type::ClassName);
  90. };
  91. while (m_index < m_input.length()) {
  92. if (isspace(peek(0))) {
  93. begin_token();
  94. while (isspace(peek()))
  95. consume();
  96. continue;
  97. }
  98. // C++ style comments
  99. if (peek(0) && peek(0) == '/' && peek(1) == '/') {
  100. begin_token();
  101. while (peek() && peek() != '\n')
  102. consume();
  103. commit_token(GMLToken::Type::Comment);
  104. continue;
  105. }
  106. if (peek(0) == '{') {
  107. begin_token();
  108. consume();
  109. commit_token(GMLToken::Type::LeftCurly);
  110. continue;
  111. }
  112. if (peek(0) == '}') {
  113. begin_token();
  114. consume();
  115. commit_token(GMLToken::Type::RightCurly);
  116. continue;
  117. }
  118. if (peek(0) == '@') {
  119. consume_class();
  120. continue;
  121. }
  122. if (is_valid_identifier_start(peek(0))) {
  123. begin_token();
  124. consume();
  125. while (is_valid_identifier_character(peek(0)))
  126. consume();
  127. commit_token(GMLToken::Type::Identifier);
  128. continue;
  129. }
  130. if (peek(0) == ':') {
  131. begin_token();
  132. consume();
  133. commit_token(GMLToken::Type::Colon);
  134. while (isspace(peek()))
  135. consume();
  136. if (peek(0) == '@') {
  137. consume_class();
  138. } else {
  139. begin_token();
  140. while (peek() && peek() != '\n')
  141. consume();
  142. commit_token(GMLToken::Type::JsonValue);
  143. }
  144. continue;
  145. }
  146. consume();
  147. commit_token(GMLToken::Type::Unknown);
  148. }
  149. return tokens;
  150. }
  151. }