Lexer.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_eof = true;
  108. m_current_char = '\0';
  109. ++m_line_column;
  110. ++m_position;
  111. return true;
  112. };
  113. if (m_position > m_source.length())
  114. return;
  115. if (did_reach_eof())
  116. return;
  117. if (is_line_break()) {
  118. ++m_line_number;
  119. m_line_column = 1;
  120. } else {
  121. ++m_line_column;
  122. }
  123. m_current_char = m_source[m_position++];
  124. }
  125. bool Lexer::consume_whitespace_and_comments()
  126. {
  127. bool found_invalid_comment = false;
  128. while (true) {
  129. if (isspace(m_current_char)) {
  130. do {
  131. consume();
  132. } while (isspace(m_current_char));
  133. } else if (is_line_comment_start()) {
  134. consume();
  135. do {
  136. consume();
  137. } while (!is_eof() && !is_line_break());
  138. } else if (is_block_comment_start()) {
  139. consume();
  140. do {
  141. consume();
  142. } while (!is_eof() && !is_block_comment_end());
  143. if (is_eof())
  144. found_invalid_comment = true;
  145. consume(); // consume *
  146. if (is_eof())
  147. found_invalid_comment = true;
  148. consume(); // consume /
  149. } else {
  150. break;
  151. }
  152. }
  153. return found_invalid_comment;
  154. }
  155. bool Lexer::consume_numeric_literal()
  156. {
  157. // https://sqlite.org/syntax/numeric-literal.html
  158. bool is_valid_numeric_literal = true;
  159. if (m_current_char == '0') {
  160. consume();
  161. if (m_current_char == '.') {
  162. consume();
  163. while (isdigit(m_current_char))
  164. consume();
  165. if (m_current_char == 'e' || m_current_char == 'E')
  166. is_valid_numeric_literal = consume_exponent();
  167. } else if (m_current_char == 'e' || m_current_char == 'E') {
  168. is_valid_numeric_literal = consume_exponent();
  169. } else if (m_current_char == 'x' || m_current_char == 'X') {
  170. is_valid_numeric_literal = consume_hexadecimal_number();
  171. } else if (isdigit(m_current_char)) {
  172. do {
  173. consume();
  174. } while (isdigit(m_current_char));
  175. }
  176. } else {
  177. do {
  178. consume();
  179. } while (isdigit(m_current_char));
  180. if (m_current_char == '.') {
  181. consume();
  182. while (isdigit(m_current_char))
  183. consume();
  184. }
  185. if (m_current_char == 'e' || m_current_char == 'E')
  186. is_valid_numeric_literal = consume_exponent();
  187. }
  188. return is_valid_numeric_literal;
  189. }
  190. bool Lexer::consume_string_literal()
  191. {
  192. // https://sqlite.org/lang_expr.html - See "3. Literal Values (Constants)"
  193. bool is_valid_string_literal = true;
  194. consume();
  195. while (!is_eof() && !is_string_literal_end())
  196. consume();
  197. if (is_eof())
  198. is_valid_string_literal = false;
  199. consume();
  200. return is_valid_string_literal;
  201. }
  202. bool Lexer::consume_blob_literal()
  203. {
  204. // https://sqlite.org/lang_expr.html - See "3. Literal Values (Constants)"
  205. consume();
  206. return consume_string_literal();
  207. }
  208. bool Lexer::consume_exponent()
  209. {
  210. consume();
  211. if (m_current_char == '-' || m_current_char == '+')
  212. consume();
  213. if (!isdigit(m_current_char))
  214. return false;
  215. while (isdigit(m_current_char)) {
  216. consume();
  217. }
  218. return true;
  219. }
  220. bool Lexer::consume_hexadecimal_number()
  221. {
  222. consume();
  223. if (!isxdigit(m_current_char))
  224. return false;
  225. while (isxdigit(m_current_char))
  226. consume();
  227. return true;
  228. }
  229. bool Lexer::match(char a, char b) const
  230. {
  231. if (m_position >= m_source.length())
  232. return false;
  233. return m_current_char == a && m_source[m_position] == b;
  234. }
  235. bool Lexer::is_identifier_start() const
  236. {
  237. return isalpha(m_current_char) || m_current_char == '_';
  238. }
  239. bool Lexer::is_identifier_middle() const
  240. {
  241. return is_identifier_start() || isdigit(m_current_char);
  242. }
  243. bool Lexer::is_numeric_literal_start() const
  244. {
  245. return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
  246. }
  247. bool Lexer::is_string_literal_start() const
  248. {
  249. return m_current_char == '\'';
  250. }
  251. bool Lexer::is_string_literal_end() const
  252. {
  253. return m_current_char == '\'' && !(m_position < m_source.length() && m_source[m_position] == '\'');
  254. }
  255. bool Lexer::is_blob_literal_start() const
  256. {
  257. return match('x', '\'') || match('X', '\'');
  258. }
  259. bool Lexer::is_line_comment_start() const
  260. {
  261. return match('-', '-');
  262. }
  263. bool Lexer::is_block_comment_start() const
  264. {
  265. return match('/', '*');
  266. }
  267. bool Lexer::is_block_comment_end() const
  268. {
  269. return match('*', '/');
  270. }
  271. bool Lexer::is_line_break() const
  272. {
  273. return m_current_char == '\n';
  274. }
  275. bool Lexer::is_eof() const
  276. {
  277. return m_eof;
  278. }
  279. }