Lexer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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 "Lexer.h"
  27. #include <AK/Debug.h>
  28. #include <ctype.h>
  29. namespace SQL {
  30. HashMap<String, TokenType> Lexer::s_keywords;
  31. HashMap<char, TokenType> Lexer::s_one_char_tokens;
  32. HashMap<String, TokenType> Lexer::s_two_char_tokens;
  33. Lexer::Lexer(StringView source)
  34. : m_source(source)
  35. {
  36. if (s_keywords.is_empty()) {
  37. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  38. if (TokenCategory::category == TokenCategory::Keyword) \
  39. s_keywords.set(value, TokenType::type);
  40. ENUMERATE_SQL_TOKENS
  41. #undef __ENUMERATE_SQL_TOKEN
  42. }
  43. if (s_one_char_tokens.is_empty()) {
  44. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  45. if (TokenCategory::category != TokenCategory::Keyword && StringView(value).length() == 1) \
  46. s_one_char_tokens.set(value[0], TokenType::type);
  47. ENUMERATE_SQL_TOKENS
  48. #undef __ENUMERATE_SQL_TOKEN
  49. }
  50. if (s_two_char_tokens.is_empty()) {
  51. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  52. if (TokenCategory::category != TokenCategory::Keyword && StringView(value).length() == 2) \
  53. s_two_char_tokens.set(value, TokenType::type);
  54. ENUMERATE_SQL_TOKENS
  55. #undef __ENUMERATE_SQL_TOKEN
  56. }
  57. consume();
  58. }
  59. Token Lexer::next()
  60. {
  61. bool found_invalid_comment = consume_whitespace_and_comments();
  62. size_t value_start = m_position;
  63. size_t value_start_line_number = m_line_number;
  64. size_t value_start_column_number = m_line_column;
  65. auto token_type = TokenType::Invalid;
  66. if (is_eof()) {
  67. token_type = found_invalid_comment ? TokenType::Invalid : TokenType::Eof;
  68. } else if (is_identifier_start()) {
  69. do {
  70. consume();
  71. } while (is_identifier_middle());
  72. if (auto it = s_keywords.find(m_source.substring_view(value_start - 1, m_position - value_start)); it != s_keywords.end()) {
  73. token_type = it->value;
  74. } else {
  75. token_type = TokenType::Identifier;
  76. }
  77. } else if (is_numeric_literal_start()) {
  78. token_type = TokenType::NumericLiteral;
  79. if (!consume_numeric_literal())
  80. token_type = TokenType::Invalid;
  81. } else {
  82. bool found_two_char_token = false;
  83. if (m_position < m_source.length()) {
  84. if (auto it = s_two_char_tokens.find(m_source.substring_view(m_position - 1, 2)); it != s_two_char_tokens.end()) {
  85. found_two_char_token = true;
  86. token_type = it->value;
  87. consume();
  88. consume();
  89. }
  90. }
  91. bool found_one_char_token = false;
  92. if (!found_two_char_token) {
  93. if (auto it = s_one_char_tokens.find(m_current_char); it != s_one_char_tokens.end()) {
  94. found_one_char_token = true;
  95. token_type = it->value;
  96. consume();
  97. }
  98. }
  99. if (!found_two_char_token && !found_one_char_token) {
  100. token_type = TokenType::Invalid;
  101. consume();
  102. }
  103. }
  104. Token token(token_type, m_source.substring_view(value_start - 1, m_position - value_start), value_start_line_number, value_start_column_number);
  105. if constexpr (SQL_DEBUG) {
  106. dbgln("------------------------------");
  107. dbgln("Token: {}", token.name());
  108. dbgln("Value: {}", token.value());
  109. dbgln("Line: {}, Column: {}", token.line_number(), token.line_column());
  110. dbgln("------------------------------");
  111. }
  112. return token;
  113. }
  114. void Lexer::consume()
  115. {
  116. auto did_reach_eof = [this] {
  117. if (m_position != m_source.length())
  118. return false;
  119. m_current_char = EOF;
  120. ++m_line_column;
  121. ++m_position;
  122. return true;
  123. };
  124. if (m_position > m_source.length())
  125. return;
  126. if (did_reach_eof())
  127. return;
  128. if (is_line_break()) {
  129. ++m_line_number;
  130. m_line_column = 1;
  131. } else {
  132. ++m_line_column;
  133. }
  134. m_current_char = m_source[m_position++];
  135. }
  136. bool Lexer::consume_whitespace_and_comments()
  137. {
  138. bool found_invalid_comment = false;
  139. while (true) {
  140. if (isspace(m_current_char)) {
  141. do {
  142. consume();
  143. } while (isspace(m_current_char));
  144. } else if (is_line_comment_start()) {
  145. consume();
  146. do {
  147. consume();
  148. } while (!is_eof() && !is_line_break());
  149. } else if (is_block_comment_start()) {
  150. consume();
  151. do {
  152. consume();
  153. } while (!is_eof() && !is_block_comment_end());
  154. if (is_eof())
  155. found_invalid_comment = true;
  156. consume(); // consume *
  157. if (is_eof())
  158. found_invalid_comment = true;
  159. consume(); // consume /
  160. } else {
  161. break;
  162. }
  163. }
  164. return found_invalid_comment;
  165. }
  166. bool Lexer::consume_numeric_literal()
  167. {
  168. // https://www.sqlite.org/draft/syntax/numeric-literal.html
  169. bool is_valid_numeric_literal = true;
  170. if (m_current_char == '0') {
  171. consume();
  172. if (m_current_char == '.') {
  173. consume();
  174. while (isdigit(m_current_char))
  175. consume();
  176. if (m_current_char == 'e' || m_current_char == 'E')
  177. is_valid_numeric_literal = consume_exponent();
  178. } else if (m_current_char == 'e' || m_current_char == 'E') {
  179. is_valid_numeric_literal = consume_exponent();
  180. } else if (m_current_char == 'x' || m_current_char == 'X') {
  181. is_valid_numeric_literal = consume_hexadecimal_number();
  182. } else if (isdigit(m_current_char)) {
  183. do {
  184. consume();
  185. } while (isdigit(m_current_char));
  186. }
  187. } else {
  188. do {
  189. consume();
  190. } while (isdigit(m_current_char));
  191. if (m_current_char == '.') {
  192. consume();
  193. while (isdigit(m_current_char))
  194. consume();
  195. }
  196. if (m_current_char == 'e' || m_current_char == 'E')
  197. is_valid_numeric_literal = consume_exponent();
  198. }
  199. return is_valid_numeric_literal;
  200. }
  201. bool Lexer::consume_exponent()
  202. {
  203. consume();
  204. if (m_current_char == '-' || m_current_char == '+')
  205. consume();
  206. if (!isdigit(m_current_char))
  207. return false;
  208. while (isdigit(m_current_char)) {
  209. consume();
  210. }
  211. return true;
  212. }
  213. bool Lexer::consume_hexadecimal_number()
  214. {
  215. consume();
  216. if (!isxdigit(m_current_char))
  217. return false;
  218. while (isxdigit(m_current_char))
  219. consume();
  220. return true;
  221. }
  222. bool Lexer::match(char a, char b) const
  223. {
  224. if (m_position >= m_source.length())
  225. return false;
  226. return m_current_char == a
  227. && m_source[m_position] == b;
  228. }
  229. bool Lexer::is_identifier_start() const
  230. {
  231. return isalpha(m_current_char) || m_current_char == '_';
  232. }
  233. bool Lexer::is_identifier_middle() const
  234. {
  235. return is_identifier_start() || isdigit(m_current_char);
  236. }
  237. bool Lexer::is_numeric_literal_start() const
  238. {
  239. return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
  240. }
  241. bool Lexer::is_line_comment_start() const
  242. {
  243. return match('-', '-');
  244. }
  245. bool Lexer::is_block_comment_start() const
  246. {
  247. return match('/', '*');
  248. }
  249. bool Lexer::is_block_comment_end() const
  250. {
  251. return match('*', '/');
  252. }
  253. bool Lexer::is_line_break() const
  254. {
  255. return m_current_char == '\n';
  256. }
  257. bool Lexer::is_eof() const
  258. {
  259. return m_current_char == EOF;
  260. }
  261. }