Lexer.cpp 8.7 KB

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